blob: 792c3e3795cae819caedaf12121052b47ad34a89 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
36#include <drm/drm_crtc.h>
37#include <drm/drm_edid.h>
38#include <drm/drm_fourcc.h>
Dave Airlief453ba02008-11-07 14:05:41 -080039
Daniel Vetter84849902012-12-02 00:28:11 +010040/**
41 * drm_modeset_lock_all - take all modeset locks
42 * @dev: drm device
43 *
44 * This function takes all modeset locks, suitable where a more fine-grained
45 * scheme isn't (yet) implemented.
46 */
47void drm_modeset_lock_all(struct drm_device *dev)
48{
Daniel Vetter29494c12012-12-02 02:18:25 +010049 struct drm_crtc *crtc;
50
Daniel Vetter84849902012-12-02 00:28:11 +010051 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter29494c12012-12-02 02:18:25 +010052
53 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
54 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Daniel Vetter84849902012-12-02 00:28:11 +010055}
56EXPORT_SYMBOL(drm_modeset_lock_all);
57
58/**
59 * drm_modeset_unlock_all - drop all modeset locks
60 * @dev: device
61 */
62void drm_modeset_unlock_all(struct drm_device *dev)
63{
Daniel Vetter29494c12012-12-02 02:18:25 +010064 struct drm_crtc *crtc;
65
66 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
67 mutex_unlock(&crtc->mutex);
68
Daniel Vetter84849902012-12-02 00:28:11 +010069 mutex_unlock(&dev->mode_config.mutex);
70}
71EXPORT_SYMBOL(drm_modeset_unlock_all);
72
Daniel Vetter6aed8ec2013-01-20 17:32:21 +010073/**
74 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
75 * @dev: device
76 */
77void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
78{
79 struct drm_crtc *crtc;
80
81 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
82 WARN_ON(!mutex_is_locked(&crtc->mutex));
83
84 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
85}
86EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
87
Dave Airlief453ba02008-11-07 14:05:41 -080088/* Avoid boilerplate. I'm tired of typing. */
89#define DRM_ENUM_NAME_FN(fnname, list) \
90 char *fnname(int val) \
91 { \
92 int i; \
93 for (i = 0; i < ARRAY_SIZE(list); i++) { \
94 if (list[i].type == val) \
95 return list[i].name; \
96 } \
97 return "(unknown)"; \
98 }
99
100/*
101 * Global properties
102 */
103static struct drm_prop_enum_list drm_dpms_enum_list[] =
104{ { DRM_MODE_DPMS_ON, "On" },
105 { DRM_MODE_DPMS_STANDBY, "Standby" },
106 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
107 { DRM_MODE_DPMS_OFF, "Off" }
108};
109
110DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
111
112/*
113 * Optional properties
114 */
115static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
116{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700117 { DRM_MODE_SCALE_NONE, "None" },
118 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
119 { DRM_MODE_SCALE_CENTER, "Center" },
120 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800121};
122
123static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
124{
125 { DRM_MODE_DITHERING_OFF, "Off" },
126 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000127 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800128};
129
130/*
131 * Non-global properties, but "required" for certain connectors.
132 */
133static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
134{
135 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
136 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
137 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
138};
139
140DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
141
142static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
143{
144 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
145 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
146 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
147};
148
149DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
150 drm_dvi_i_subconnector_enum_list)
151
152static struct drm_prop_enum_list drm_tv_select_enum_list[] =
153{
154 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
155 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
156 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
157 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200158 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800159};
160
161DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
162
163static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
164{
165 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
166 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
167 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
168 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200169 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800170};
171
172DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
173 drm_tv_subconnector_enum_list)
174
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000175static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
176 { DRM_MODE_DIRTY_OFF, "Off" },
177 { DRM_MODE_DIRTY_ON, "On" },
178 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
179};
180
181DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
182 drm_dirty_info_enum_list)
183
Dave Airlief453ba02008-11-07 14:05:41 -0800184struct drm_conn_prop_enum_list {
185 int type;
186 char *name;
187 int count;
188};
189
190/*
191 * Connector and encoder types.
192 */
193static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
194{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
195 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
196 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
197 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
198 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
199 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
200 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
201 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
202 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500203 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
204 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
205 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
206 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200207 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500208 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200209 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800210};
211
212static struct drm_prop_enum_list drm_encoder_enum_list[] =
213{ { DRM_MODE_ENCODER_NONE, "None" },
214 { DRM_MODE_ENCODER_DAC, "DAC" },
215 { DRM_MODE_ENCODER_TMDS, "TMDS" },
216 { DRM_MODE_ENCODER_LVDS, "LVDS" },
217 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200218 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800219};
220
221char *drm_get_encoder_name(struct drm_encoder *encoder)
222{
223 static char buf[32];
224
225 snprintf(buf, 32, "%s-%d",
226 drm_encoder_enum_list[encoder->encoder_type].name,
227 encoder->base.id);
228 return buf;
229}
Dave Airlie13a81952009-09-07 15:45:33 +1000230EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800231
232char *drm_get_connector_name(struct drm_connector *connector)
233{
234 static char buf[32];
235
236 snprintf(buf, 32, "%s-%d",
237 drm_connector_enum_list[connector->connector_type].name,
238 connector->connector_type_id);
239 return buf;
240}
241EXPORT_SYMBOL(drm_get_connector_name);
242
243char *drm_get_connector_status_name(enum drm_connector_status status)
244{
245 if (status == connector_status_connected)
246 return "connected";
247 else if (status == connector_status_disconnected)
248 return "disconnected";
249 else
250 return "unknown";
251}
252
253/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100254 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800255 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100256 * @obj: object pointer, used to generate unique ID
257 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800258 *
Dave Airlief453ba02008-11-07 14:05:41 -0800259 * Create a unique identifier based on @ptr in @dev's identifier space. Used
260 * for tracking modes, CRTCs and connectors.
261 *
262 * RETURNS:
263 * New unique (relative to other objects in @dev) integer identifier for the
264 * object.
265 */
266static int drm_mode_object_get(struct drm_device *dev,
267 struct drm_mode_object *obj, uint32_t obj_type)
268{
Dave Airlief453ba02008-11-07 14:05:41 -0800269 int ret;
270
Jesse Barnesad2563c2009-01-19 17:21:45 +1000271 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800272 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
273 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100274 /*
275 * Set up the object linking under the protection of the idr
276 * lock so that other users can't see inconsistent state.
277 */
Tejun Heo2e928812013-02-27 17:04:08 -0800278 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100279 obj->type = obj_type;
280 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000281 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100282
Tejun Heo2e928812013-02-27 17:04:08 -0800283 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800284}
285
286/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100287 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800288 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100289 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800290 *
Dave Airlief453ba02008-11-07 14:05:41 -0800291 * Free @id from @dev's unique identifier pool.
292 */
293static void drm_mode_object_put(struct drm_device *dev,
294 struct drm_mode_object *object)
295{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000296 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800297 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000298 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800299}
300
Daniel Vetter786b99e2012-12-02 21:53:40 +0100301/**
302 * drm_mode_object_find - look up a drm object with static lifetime
303 * @dev: drm device
304 * @id: id of the mode object
305 * @type: type of the mode object
306 *
307 * Note that framebuffers cannot be looked up with this functions - since those
308 * are reference counted, they need special treatment.
309 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200310struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
311 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800312{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000313 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800314
Daniel Vetter786b99e2012-12-02 21:53:40 +0100315 /* Framebuffers are reference counted and need their own lookup
316 * function.*/
317 WARN_ON(type == DRM_MODE_OBJECT_FB);
318
Jesse Barnesad2563c2009-01-19 17:21:45 +1000319 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800320 obj = idr_find(&dev->mode_config.crtc_idr, id);
321 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000322 obj = NULL;
323 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800324
325 return obj;
326}
327EXPORT_SYMBOL(drm_mode_object_find);
328
329/**
Dave Airlief453ba02008-11-07 14:05:41 -0800330 * drm_framebuffer_init - initialize a framebuffer
331 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100332 * @fb: framebuffer to be initialized
333 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800334 *
Dave Airlief453ba02008-11-07 14:05:41 -0800335 * Allocates an ID for the framebuffer's parent mode object, sets its mode
336 * functions & device file and adds it to the master fd list.
337 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100338 * IMPORTANT:
339 * This functions publishes the fb and makes it available for concurrent access
340 * by other users. Which means by this point the fb _must_ be fully set up -
341 * since all the fb attributes are invariant over its lifetime, no further
342 * locking but only correct reference counting is required.
343 *
Dave Airlief453ba02008-11-07 14:05:41 -0800344 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200345 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800346 */
347int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
348 const struct drm_framebuffer_funcs *funcs)
349{
350 int ret;
351
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100352 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000353 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100354 INIT_LIST_HEAD(&fb->filp_head);
355 fb->dev = dev;
356 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000357
Dave Airlief453ba02008-11-07 14:05:41 -0800358 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200359 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100360 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800361
Daniel Vetter2b677e82012-12-10 21:16:05 +0100362 /* Grab the idr reference. */
363 drm_framebuffer_reference(fb);
364
Dave Airlief453ba02008-11-07 14:05:41 -0800365 dev->mode_config.num_fb++;
366 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100367out:
368 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800369
370 return 0;
371}
372EXPORT_SYMBOL(drm_framebuffer_init);
373
Rob Clarkf7eff602012-09-05 21:48:38 +0000374static void drm_framebuffer_free(struct kref *kref)
375{
376 struct drm_framebuffer *fb =
377 container_of(kref, struct drm_framebuffer, refcount);
378 fb->funcs->destroy(fb);
379}
380
Daniel Vetter2b677e82012-12-10 21:16:05 +0100381static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
382 uint32_t id)
383{
384 struct drm_mode_object *obj = NULL;
385 struct drm_framebuffer *fb;
386
387 mutex_lock(&dev->mode_config.idr_mutex);
388 obj = idr_find(&dev->mode_config.crtc_idr, id);
389 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
390 fb = NULL;
391 else
392 fb = obj_to_fb(obj);
393 mutex_unlock(&dev->mode_config.idr_mutex);
394
395 return fb;
396}
397
Rob Clarkf7eff602012-09-05 21:48:38 +0000398/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100399 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
400 * @dev: drm device
401 * @id: id of the fb object
402 *
403 * If successful, this grabs an additional reference to the framebuffer -
404 * callers need to make sure to eventually unreference the returned framebuffer
405 * again.
406 */
407struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
408 uint32_t id)
409{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100410 struct drm_framebuffer *fb;
411
412 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100413 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100414 if (fb)
415 kref_get(&fb->refcount);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100416 mutex_unlock(&dev->mode_config.fb_lock);
417
418 return fb;
419}
420EXPORT_SYMBOL(drm_framebuffer_lookup);
421
422/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000423 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100424 * @fb: framebuffer to unref
425 *
426 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000427 */
428void drm_framebuffer_unreference(struct drm_framebuffer *fb)
429{
Rob Clarkf7eff602012-09-05 21:48:38 +0000430 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000431 kref_put(&fb->refcount, drm_framebuffer_free);
432}
433EXPORT_SYMBOL(drm_framebuffer_unreference);
434
435/**
436 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100437 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000438 */
439void drm_framebuffer_reference(struct drm_framebuffer *fb)
440{
441 DRM_DEBUG("FB ID: %d\n", fb->base.id);
442 kref_get(&fb->refcount);
443}
444EXPORT_SYMBOL(drm_framebuffer_reference);
445
Daniel Vetter2b677e82012-12-10 21:16:05 +0100446static void drm_framebuffer_free_bug(struct kref *kref)
447{
448 BUG();
449}
450
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100451static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
452{
453 DRM_DEBUG("FB ID: %d\n", fb->base.id);
454 kref_put(&fb->refcount, drm_framebuffer_free_bug);
455}
456
Daniel Vetter2b677e82012-12-10 21:16:05 +0100457/* dev->mode_config.fb_lock must be held! */
458static void __drm_framebuffer_unregister(struct drm_device *dev,
459 struct drm_framebuffer *fb)
460{
461 mutex_lock(&dev->mode_config.idr_mutex);
462 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
463 mutex_unlock(&dev->mode_config.idr_mutex);
464
465 fb->base.id = 0;
466
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100467 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100468}
469
Dave Airlief453ba02008-11-07 14:05:41 -0800470/**
Daniel Vetter36206362012-12-10 20:42:17 +0100471 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
472 * @fb: fb to unregister
473 *
474 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
475 * those used for fbdev. Note that the caller must hold a reference of it's own,
476 * i.e. the object may not be destroyed through this call (since it'll lead to a
477 * locking inversion).
478 */
479void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
480{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100481 struct drm_device *dev = fb->dev;
482
483 mutex_lock(&dev->mode_config.fb_lock);
484 /* Mark fb as reaped and drop idr ref. */
485 __drm_framebuffer_unregister(dev, fb);
486 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100487}
488EXPORT_SYMBOL(drm_framebuffer_unregister_private);
489
490/**
Dave Airlief453ba02008-11-07 14:05:41 -0800491 * drm_framebuffer_cleanup - remove a framebuffer object
492 * @fb: framebuffer to remove
493 *
Daniel Vetter36206362012-12-10 20:42:17 +0100494 * Cleanup references to a user-created framebuffer. This function is intended
495 * to be used from the drivers ->destroy callback.
496 *
497 * Note that this function does not remove the fb from active usuage - if it is
498 * still used anywhere, hilarity can ensue since userspace could call getfb on
499 * the id and get back -EINVAL. Obviously no concern at driver unload time.
500 *
501 * Also, the framebuffer will not be removed from the lookup idr - for
502 * user-created framebuffers this will happen in in the rmfb ioctl. For
503 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
504 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800505 */
506void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
507{
508 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100509
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100510 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000511 list_del(&fb->head);
512 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100513 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000514}
515EXPORT_SYMBOL(drm_framebuffer_cleanup);
516
517/**
518 * drm_framebuffer_remove - remove and unreference a framebuffer object
519 * @fb: framebuffer to remove
520 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000521 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100522 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100523 * passed-in framebuffer. Might take the modeset locks.
524 *
525 * Note that this function optimizes the cleanup away if the caller holds the
526 * last reference to the framebuffer. It is also guaranteed to not take the
527 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000528 */
529void drm_framebuffer_remove(struct drm_framebuffer *fb)
530{
531 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800532 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800533 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000534 struct drm_mode_set set;
535 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800536
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100537 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100538
Daniel Vetterb62584e2012-12-11 16:51:35 +0100539 /*
540 * drm ABI mandates that we remove any deleted framebuffers from active
541 * useage. But since most sane clients only remove framebuffers they no
542 * longer need, try to optimize this away.
543 *
544 * Since we're holding a reference ourselves, observing a refcount of 1
545 * means that we're the last holder and can skip it. Also, the refcount
546 * can never increase from 1 again, so we don't need any barriers or
547 * locks.
548 *
549 * Note that userspace could try to race with use and instate a new
550 * usage _after_ we've cleared all current ones. End result will be an
551 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
552 * in this manner.
553 */
554 if (atomic_read(&fb->refcount.refcount) > 1) {
555 drm_modeset_lock_all(dev);
556 /* remove from any CRTC */
557 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
558 if (crtc->fb == fb) {
559 /* should turn off the crtc */
560 memset(&set, 0, sizeof(struct drm_mode_set));
561 set.crtc = crtc;
562 set.fb = NULL;
563 ret = drm_mode_set_config_internal(&set);
564 if (ret)
565 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
566 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000567 }
Dave Airlief453ba02008-11-07 14:05:41 -0800568
Daniel Vetterb62584e2012-12-11 16:51:35 +0100569 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
570 if (plane->fb == fb) {
571 /* should turn off the crtc */
572 ret = plane->funcs->disable_plane(plane);
573 if (ret)
574 DRM_ERROR("failed to disable plane with busy fb\n");
575 /* disconnect the plane from the fb and crtc: */
576 __drm_framebuffer_unreference(plane->fb);
577 plane->fb = NULL;
578 plane->crtc = NULL;
579 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800580 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100581 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800582 }
583
Rob Clarkf7eff602012-09-05 21:48:38 +0000584 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800585}
Rob Clarkf7eff602012-09-05 21:48:38 +0000586EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800587
588/**
589 * drm_crtc_init - Initialise a new CRTC object
590 * @dev: DRM device
591 * @crtc: CRTC object to init
592 * @funcs: callbacks for the new CRTC
593 *
Dave Airlief453ba02008-11-07 14:05:41 -0800594 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200595 *
596 * RETURNS:
597 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800598 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200599int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800600 const struct drm_crtc_funcs *funcs)
601{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200602 int ret;
603
Dave Airlief453ba02008-11-07 14:05:41 -0800604 crtc->dev = dev;
605 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000606 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800607
Daniel Vetter84849902012-12-02 00:28:11 +0100608 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100609 mutex_init(&crtc->mutex);
610 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200611
612 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
613 if (ret)
614 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800615
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300616 crtc->base.properties = &crtc->properties;
617
Dave Airlief453ba02008-11-07 14:05:41 -0800618 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
619 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200620
621 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100622 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200623
624 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800625}
626EXPORT_SYMBOL(drm_crtc_init);
627
628/**
629 * drm_crtc_cleanup - Cleans up the core crtc usage.
630 * @crtc: CRTC to cleanup
631 *
Dave Airlief453ba02008-11-07 14:05:41 -0800632 * Cleanup @crtc. Removes from drm modesetting space
633 * does NOT free object, caller does that.
634 */
635void drm_crtc_cleanup(struct drm_crtc *crtc)
636{
637 struct drm_device *dev = crtc->dev;
638
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000639 kfree(crtc->gamma_store);
640 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800641
642 drm_mode_object_put(dev, &crtc->base);
643 list_del(&crtc->head);
644 dev->mode_config.num_crtc--;
645}
646EXPORT_SYMBOL(drm_crtc_cleanup);
647
648/**
649 * drm_mode_probed_add - add a mode to a connector's probed mode list
650 * @connector: connector the new mode
651 * @mode: mode data
652 *
Dave Airlief453ba02008-11-07 14:05:41 -0800653 * Add @mode to @connector's mode list for later use.
654 */
655void drm_mode_probed_add(struct drm_connector *connector,
656 struct drm_display_mode *mode)
657{
658 list_add(&mode->head, &connector->probed_modes);
659}
660EXPORT_SYMBOL(drm_mode_probed_add);
661
662/**
663 * drm_mode_remove - remove and free a mode
664 * @connector: connector list to modify
665 * @mode: mode to remove
666 *
Dave Airlief453ba02008-11-07 14:05:41 -0800667 * Remove @mode from @connector's mode list, then free it.
668 */
669void drm_mode_remove(struct drm_connector *connector,
670 struct drm_display_mode *mode)
671{
672 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100673 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800674}
675EXPORT_SYMBOL(drm_mode_remove);
676
677/**
678 * drm_connector_init - Init a preallocated connector
679 * @dev: DRM device
680 * @connector: the connector to init
681 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100682 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800683 *
Dave Airlief453ba02008-11-07 14:05:41 -0800684 * Initialises a preallocated connector. Connectors should be
685 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200686 *
687 * RETURNS:
688 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800689 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200690int drm_connector_init(struct drm_device *dev,
691 struct drm_connector *connector,
692 const struct drm_connector_funcs *funcs,
693 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800694{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200695 int ret;
696
Daniel Vetter84849902012-12-02 00:28:11 +0100697 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800698
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200699 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
700 if (ret)
701 goto out;
702
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300703 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800704 connector->dev = dev;
705 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800706 connector->connector_type = connector_type;
707 connector->connector_type_id =
708 ++drm_connector_enum_list[connector_type].count; /* TODO */
709 INIT_LIST_HEAD(&connector->user_modes);
710 INIT_LIST_HEAD(&connector->probed_modes);
711 INIT_LIST_HEAD(&connector->modes);
712 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000713 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800714
715 list_add_tail(&connector->head, &dev->mode_config.connector_list);
716 dev->mode_config.num_connector++;
717
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200718 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500719 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200720 dev->mode_config.edid_property,
721 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800722
Rob Clark58495562012-10-11 20:50:56 -0500723 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800724 dev->mode_config.dpms_property, 0);
725
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200726 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100727 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200728
729 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800730}
731EXPORT_SYMBOL(drm_connector_init);
732
733/**
734 * drm_connector_cleanup - cleans up an initialised connector
735 * @connector: connector to cleanup
736 *
Dave Airlief453ba02008-11-07 14:05:41 -0800737 * Cleans up the connector but doesn't free the object.
738 */
739void drm_connector_cleanup(struct drm_connector *connector)
740{
741 struct drm_device *dev = connector->dev;
742 struct drm_display_mode *mode, *t;
743
744 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
745 drm_mode_remove(connector, mode);
746
747 list_for_each_entry_safe(mode, t, &connector->modes, head)
748 drm_mode_remove(connector, mode);
749
750 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
751 drm_mode_remove(connector, mode);
752
Dave Airlief453ba02008-11-07 14:05:41 -0800753 drm_mode_object_put(dev, &connector->base);
754 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000755 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800756}
757EXPORT_SYMBOL(drm_connector_cleanup);
758
Dave Airliecbc7e222012-02-20 14:16:40 +0000759void drm_connector_unplug_all(struct drm_device *dev)
760{
761 struct drm_connector *connector;
762
763 /* taking the mode config mutex ends up in a clash with sysfs */
764 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
765 drm_sysfs_connector_remove(connector);
766
767}
768EXPORT_SYMBOL(drm_connector_unplug_all);
769
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200770int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000771 struct drm_encoder *encoder,
772 const struct drm_encoder_funcs *funcs,
773 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800774{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200775 int ret;
776
Daniel Vetter84849902012-12-02 00:28:11 +0100777 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800778
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200779 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
780 if (ret)
781 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800782
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200783 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800784 encoder->encoder_type = encoder_type;
785 encoder->funcs = funcs;
786
787 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
788 dev->mode_config.num_encoder++;
789
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200790 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100791 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200792
793 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800794}
795EXPORT_SYMBOL(drm_encoder_init);
796
797void drm_encoder_cleanup(struct drm_encoder *encoder)
798{
799 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100800 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800801 drm_mode_object_put(dev, &encoder->base);
802 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000803 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100804 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800805}
806EXPORT_SYMBOL(drm_encoder_cleanup);
807
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800808int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
809 unsigned long possible_crtcs,
810 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600811 const uint32_t *formats, uint32_t format_count,
812 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800813{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200814 int ret;
815
Daniel Vetter84849902012-12-02 00:28:11 +0100816 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800817
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200818 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
819 if (ret)
820 goto out;
821
Rob Clark4d939142012-05-17 02:23:27 -0600822 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800823 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800824 plane->funcs = funcs;
825 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
826 GFP_KERNEL);
827 if (!plane->format_types) {
828 DRM_DEBUG_KMS("out of memory when allocating plane\n");
829 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200830 ret = -ENOMEM;
831 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800832 }
833
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800834 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800835 plane->format_count = format_count;
836 plane->possible_crtcs = possible_crtcs;
837
Rob Clark0a7eb242011-12-13 20:19:36 -0600838 /* private planes are not exposed to userspace, but depending on
839 * display hardware, might be convenient to allow sharing programming
840 * for the scanout engine with the crtc implementation.
841 */
842 if (!priv) {
843 list_add_tail(&plane->head, &dev->mode_config.plane_list);
844 dev->mode_config.num_plane++;
845 } else {
846 INIT_LIST_HEAD(&plane->head);
847 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800848
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200849 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100850 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800851
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200852 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800853}
854EXPORT_SYMBOL(drm_plane_init);
855
856void drm_plane_cleanup(struct drm_plane *plane)
857{
858 struct drm_device *dev = plane->dev;
859
Daniel Vetter84849902012-12-02 00:28:11 +0100860 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800861 kfree(plane->format_types);
862 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600863 /* if not added to a list, it must be a private plane */
864 if (!list_empty(&plane->head)) {
865 list_del(&plane->head);
866 dev->mode_config.num_plane--;
867 }
Daniel Vetter84849902012-12-02 00:28:11 +0100868 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800869}
870EXPORT_SYMBOL(drm_plane_cleanup);
871
Dave Airlief453ba02008-11-07 14:05:41 -0800872/**
873 * drm_mode_create - create a new display mode
874 * @dev: DRM device
875 *
Dave Airlief453ba02008-11-07 14:05:41 -0800876 * Create a new drm_display_mode, give it an ID, and return it.
877 *
878 * RETURNS:
879 * Pointer to new mode on success, NULL on error.
880 */
881struct drm_display_mode *drm_mode_create(struct drm_device *dev)
882{
883 struct drm_display_mode *nmode;
884
885 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
886 if (!nmode)
887 return NULL;
888
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200889 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
890 kfree(nmode);
891 return NULL;
892 }
893
Dave Airlief453ba02008-11-07 14:05:41 -0800894 return nmode;
895}
896EXPORT_SYMBOL(drm_mode_create);
897
898/**
899 * drm_mode_destroy - remove a mode
900 * @dev: DRM device
901 * @mode: mode to remove
902 *
Dave Airlief453ba02008-11-07 14:05:41 -0800903 * Free @mode's unique identifier, then free it.
904 */
905void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
906{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200907 if (!mode)
908 return;
909
Dave Airlief453ba02008-11-07 14:05:41 -0800910 drm_mode_object_put(dev, &mode->base);
911
912 kfree(mode);
913}
914EXPORT_SYMBOL(drm_mode_destroy);
915
916static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
917{
918 struct drm_property *edid;
919 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800920
921 /*
922 * Standard properties (apply to all connectors)
923 */
924 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
925 DRM_MODE_PROP_IMMUTABLE,
926 "EDID", 0);
927 dev->mode_config.edid_property = edid;
928
Sascha Hauer4a67d392012-02-06 10:58:17 +0100929 dpms = drm_property_create_enum(dev, 0,
930 "DPMS", drm_dpms_enum_list,
931 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800932 dev->mode_config.dpms_property = dpms;
933
934 return 0;
935}
936
937/**
938 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
939 * @dev: DRM device
940 *
941 * Called by a driver the first time a DVI-I connector is made.
942 */
943int drm_mode_create_dvi_i_properties(struct drm_device *dev)
944{
945 struct drm_property *dvi_i_selector;
946 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800947
948 if (dev->mode_config.dvi_i_select_subconnector_property)
949 return 0;
950
951 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100952 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800953 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100954 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800955 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800956 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
957
Sascha Hauer4a67d392012-02-06 10:58:17 +0100958 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800959 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100960 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800961 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800962 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
963
964 return 0;
965}
966EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
967
968/**
969 * drm_create_tv_properties - create TV specific connector properties
970 * @dev: DRM device
971 * @num_modes: number of different TV formats (modes) supported
972 * @modes: array of pointers to strings containing name of each format
973 *
974 * Called by a driver's TV initialization routine, this function creates
975 * the TV specific connector properties for a given device. Caller is
976 * responsible for allocating a list of format names and passing them to
977 * this routine.
978 */
979int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
980 char *modes[])
981{
982 struct drm_property *tv_selector;
983 struct drm_property *tv_subconnector;
984 int i;
985
986 if (dev->mode_config.tv_select_subconnector_property)
987 return 0;
988
989 /*
990 * Basic connector properties
991 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100992 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800993 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100994 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800995 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800996 dev->mode_config.tv_select_subconnector_property = tv_selector;
997
998 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100999 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1000 "subconnector",
1001 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001002 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001003 dev->mode_config.tv_subconnector_property = tv_subconnector;
1004
1005 /*
1006 * Other, TV specific properties: margins & TV modes.
1007 */
1008 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001009 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001010
1011 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001012 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001013
1014 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001015 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001016
1017 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001018 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001019
1020 dev->mode_config.tv_mode_property =
1021 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1022 "mode", num_modes);
1023 for (i = 0; i < num_modes; i++)
1024 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1025 i, modes[i]);
1026
Francisco Jerezb6b79022009-08-02 04:19:20 +02001027 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001028 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001029
1030 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001031 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001032
1033 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001034 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001035
Francisco Jereza75f0232009-08-12 02:30:10 +02001036 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001037 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001038
1039 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001040 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001041
1042 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001043 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001044
Dave Airlief453ba02008-11-07 14:05:41 -08001045 return 0;
1046}
1047EXPORT_SYMBOL(drm_mode_create_tv_properties);
1048
1049/**
1050 * drm_mode_create_scaling_mode_property - create scaling mode property
1051 * @dev: DRM device
1052 *
1053 * Called by a driver the first time it's needed, must be attached to desired
1054 * connectors.
1055 */
1056int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1057{
1058 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001059
1060 if (dev->mode_config.scaling_mode_property)
1061 return 0;
1062
1063 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001064 drm_property_create_enum(dev, 0, "scaling mode",
1065 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001066 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001067
1068 dev->mode_config.scaling_mode_property = scaling_mode;
1069
1070 return 0;
1071}
1072EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1073
1074/**
1075 * drm_mode_create_dithering_property - create dithering property
1076 * @dev: DRM device
1077 *
1078 * Called by a driver the first time it's needed, must be attached to desired
1079 * connectors.
1080 */
1081int drm_mode_create_dithering_property(struct drm_device *dev)
1082{
1083 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001084
1085 if (dev->mode_config.dithering_mode_property)
1086 return 0;
1087
1088 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001089 drm_property_create_enum(dev, 0, "dithering",
1090 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001091 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001092 dev->mode_config.dithering_mode_property = dithering_mode;
1093
1094 return 0;
1095}
1096EXPORT_SYMBOL(drm_mode_create_dithering_property);
1097
1098/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001099 * drm_mode_create_dirty_property - create dirty property
1100 * @dev: DRM device
1101 *
1102 * Called by a driver the first time it's needed, must be attached to desired
1103 * connectors.
1104 */
1105int drm_mode_create_dirty_info_property(struct drm_device *dev)
1106{
1107 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001108
1109 if (dev->mode_config.dirty_info_property)
1110 return 0;
1111
1112 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001113 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001114 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001115 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001116 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001117 dev->mode_config.dirty_info_property = dirty_info;
1118
1119 return 0;
1120}
1121EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1122
1123/**
Dave Airlief453ba02008-11-07 14:05:41 -08001124 * drm_mode_config_init - initialize DRM mode_configuration structure
1125 * @dev: DRM device
1126 *
Dave Airlief453ba02008-11-07 14:05:41 -08001127 * Initialize @dev's mode_config structure, used for tracking the graphics
1128 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001129 *
1130 * Since this initializes the modeset locks, no locking is possible. Which is no
1131 * problem, since this should happen single threaded at init time. It is the
1132 * driver's problem to ensure this guarantee.
1133 *
Dave Airlief453ba02008-11-07 14:05:41 -08001134 */
1135void drm_mode_config_init(struct drm_device *dev)
1136{
1137 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001138 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001139 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001140 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001141 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1142 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1143 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1144 INIT_LIST_HEAD(&dev->mode_config.property_list);
1145 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001146 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001147 idr_init(&dev->mode_config.crtc_idr);
1148
Daniel Vetter84849902012-12-02 00:28:11 +01001149 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001150 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001151 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001152
1153 /* Just to be sure */
1154 dev->mode_config.num_fb = 0;
1155 dev->mode_config.num_connector = 0;
1156 dev->mode_config.num_crtc = 0;
1157 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001158}
1159EXPORT_SYMBOL(drm_mode_config_init);
1160
1161int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1162{
1163 uint32_t total_objects = 0;
1164
1165 total_objects += dev->mode_config.num_crtc;
1166 total_objects += dev->mode_config.num_connector;
1167 total_objects += dev->mode_config.num_encoder;
1168
Dave Airlief453ba02008-11-07 14:05:41 -08001169 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1170 if (!group->id_list)
1171 return -ENOMEM;
1172
1173 group->num_crtcs = 0;
1174 group->num_connectors = 0;
1175 group->num_encoders = 0;
1176 return 0;
1177}
1178
1179int drm_mode_group_init_legacy_group(struct drm_device *dev,
1180 struct drm_mode_group *group)
1181{
1182 struct drm_crtc *crtc;
1183 struct drm_encoder *encoder;
1184 struct drm_connector *connector;
1185 int ret;
1186
1187 if ((ret = drm_mode_group_init(dev, group)))
1188 return ret;
1189
1190 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1191 group->id_list[group->num_crtcs++] = crtc->base.id;
1192
1193 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1194 group->id_list[group->num_crtcs + group->num_encoders++] =
1195 encoder->base.id;
1196
1197 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1198 group->id_list[group->num_crtcs + group->num_encoders +
1199 group->num_connectors++] = connector->base.id;
1200
1201 return 0;
1202}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001203EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001204
1205/**
1206 * drm_mode_config_cleanup - free up DRM mode_config info
1207 * @dev: DRM device
1208 *
Dave Airlief453ba02008-11-07 14:05:41 -08001209 * Free up all the connectors and CRTCs associated with this DRM device, then
1210 * free up the framebuffers and associated buffer objects.
1211 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001212 * Note that since this /should/ happen single-threaded at driver/device
1213 * teardown time, no locking is required. It's the driver's job to ensure that
1214 * this guarantee actually holds true.
1215 *
Dave Airlief453ba02008-11-07 14:05:41 -08001216 * FIXME: cleanup any dangling user buffer objects too
1217 */
1218void drm_mode_config_cleanup(struct drm_device *dev)
1219{
1220 struct drm_connector *connector, *ot;
1221 struct drm_crtc *crtc, *ct;
1222 struct drm_encoder *encoder, *enct;
1223 struct drm_framebuffer *fb, *fbt;
1224 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001225 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001226
1227 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1228 head) {
1229 encoder->funcs->destroy(encoder);
1230 }
1231
1232 list_for_each_entry_safe(connector, ot,
1233 &dev->mode_config.connector_list, head) {
1234 connector->funcs->destroy(connector);
1235 }
1236
1237 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1238 head) {
1239 drm_property_destroy(dev, property);
1240 }
1241
Daniel Vetter2b677e82012-12-10 21:16:05 +01001242 /*
1243 * Single-threaded teardown context, so it's not required to grab the
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001244 * fb_lock to protect against concurrent fb_list access. Contrary, it
Daniel Vetter2b677e82012-12-10 21:16:05 +01001245 * would actually deadlock with the drm_framebuffer_cleanup function.
1246 *
1247 * Also, if there are any framebuffers left, that's a driver leak now,
1248 * so politely WARN about this.
1249 */
1250 WARN_ON(!list_empty(&dev->mode_config.fb_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001251 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001252 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001253 }
1254
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001255 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1256 head) {
1257 plane->funcs->destroy(plane);
1258 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001259
Chris Wilson31840092012-09-17 09:38:03 +00001260 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1261 crtc->funcs->destroy(crtc);
1262 }
1263
Sascha Hauer59ce0622012-02-01 11:38:20 +01001264 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001265}
1266EXPORT_SYMBOL(drm_mode_config_cleanup);
1267
Dave Airlief453ba02008-11-07 14:05:41 -08001268/**
1269 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1270 * @out: drm_mode_modeinfo struct to return to the user
1271 * @in: drm_display_mode to use
1272 *
Dave Airlief453ba02008-11-07 14:05:41 -08001273 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1274 * the user.
1275 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001276static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1277 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001278{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001279 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1280 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1281 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1282 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1283 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1284 "timing values too large for mode info\n");
1285
Dave Airlief453ba02008-11-07 14:05:41 -08001286 out->clock = in->clock;
1287 out->hdisplay = in->hdisplay;
1288 out->hsync_start = in->hsync_start;
1289 out->hsync_end = in->hsync_end;
1290 out->htotal = in->htotal;
1291 out->hskew = in->hskew;
1292 out->vdisplay = in->vdisplay;
1293 out->vsync_start = in->vsync_start;
1294 out->vsync_end = in->vsync_end;
1295 out->vtotal = in->vtotal;
1296 out->vscan = in->vscan;
1297 out->vrefresh = in->vrefresh;
1298 out->flags = in->flags;
1299 out->type = in->type;
1300 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1301 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1302}
1303
1304/**
1305 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1306 * @out: drm_display_mode to return to the user
1307 * @in: drm_mode_modeinfo to use
1308 *
Dave Airlief453ba02008-11-07 14:05:41 -08001309 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1310 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001311 *
1312 * RETURNS:
1313 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001314 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001315static int drm_crtc_convert_umode(struct drm_display_mode *out,
1316 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001317{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001318 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1319 return -ERANGE;
1320
Dave Airlief453ba02008-11-07 14:05:41 -08001321 out->clock = in->clock;
1322 out->hdisplay = in->hdisplay;
1323 out->hsync_start = in->hsync_start;
1324 out->hsync_end = in->hsync_end;
1325 out->htotal = in->htotal;
1326 out->hskew = in->hskew;
1327 out->vdisplay = in->vdisplay;
1328 out->vsync_start = in->vsync_start;
1329 out->vsync_end = in->vsync_end;
1330 out->vtotal = in->vtotal;
1331 out->vscan = in->vscan;
1332 out->vrefresh = in->vrefresh;
1333 out->flags = in->flags;
1334 out->type = in->type;
1335 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1336 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001337
1338 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001339}
1340
1341/**
1342 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001343 * @dev: drm device for the ioctl
1344 * @data: data pointer for the ioctl
1345 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001346 *
Dave Airlief453ba02008-11-07 14:05:41 -08001347 * Construct a set of configuration description structures and return
1348 * them to the user, including CRTC, connector and framebuffer configuration.
1349 *
1350 * Called by the user via ioctl.
1351 *
1352 * RETURNS:
1353 * Zero on success, errno on failure.
1354 */
1355int drm_mode_getresources(struct drm_device *dev, void *data,
1356 struct drm_file *file_priv)
1357{
1358 struct drm_mode_card_res *card_res = data;
1359 struct list_head *lh;
1360 struct drm_framebuffer *fb;
1361 struct drm_connector *connector;
1362 struct drm_crtc *crtc;
1363 struct drm_encoder *encoder;
1364 int ret = 0;
1365 int connector_count = 0;
1366 int crtc_count = 0;
1367 int fb_count = 0;
1368 int encoder_count = 0;
1369 int copied = 0, i;
1370 uint32_t __user *fb_id;
1371 uint32_t __user *crtc_id;
1372 uint32_t __user *connector_id;
1373 uint32_t __user *encoder_id;
1374 struct drm_mode_group *mode_group;
1375
Dave Airliefb3b06c2011-02-08 13:55:21 +10001376 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1377 return -EINVAL;
1378
Dave Airlief453ba02008-11-07 14:05:41 -08001379
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001380 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001381 /*
1382 * For the non-control nodes we need to limit the list of resources
1383 * by IDs in the group list for this node
1384 */
1385 list_for_each(lh, &file_priv->fbs)
1386 fb_count++;
1387
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001388 /* handle this in 4 parts */
1389 /* FBs */
1390 if (card_res->count_fbs >= fb_count) {
1391 copied = 0;
1392 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1393 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1394 if (put_user(fb->base.id, fb_id + copied)) {
1395 mutex_unlock(&file_priv->fbs_lock);
1396 return -EFAULT;
1397 }
1398 copied++;
1399 }
1400 }
1401 card_res->count_fbs = fb_count;
1402 mutex_unlock(&file_priv->fbs_lock);
1403
1404 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001405 mode_group = &file_priv->master->minor->mode_group;
1406 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1407
1408 list_for_each(lh, &dev->mode_config.crtc_list)
1409 crtc_count++;
1410
1411 list_for_each(lh, &dev->mode_config.connector_list)
1412 connector_count++;
1413
1414 list_for_each(lh, &dev->mode_config.encoder_list)
1415 encoder_count++;
1416 } else {
1417
1418 crtc_count = mode_group->num_crtcs;
1419 connector_count = mode_group->num_connectors;
1420 encoder_count = mode_group->num_encoders;
1421 }
1422
1423 card_res->max_height = dev->mode_config.max_height;
1424 card_res->min_height = dev->mode_config.min_height;
1425 card_res->max_width = dev->mode_config.max_width;
1426 card_res->min_width = dev->mode_config.min_width;
1427
Dave Airlief453ba02008-11-07 14:05:41 -08001428 /* CRTCs */
1429 if (card_res->count_crtcs >= crtc_count) {
1430 copied = 0;
1431 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1432 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1433 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1434 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001435 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001436 if (put_user(crtc->base.id, crtc_id + copied)) {
1437 ret = -EFAULT;
1438 goto out;
1439 }
1440 copied++;
1441 }
1442 } else {
1443 for (i = 0; i < mode_group->num_crtcs; i++) {
1444 if (put_user(mode_group->id_list[i],
1445 crtc_id + copied)) {
1446 ret = -EFAULT;
1447 goto out;
1448 }
1449 copied++;
1450 }
1451 }
1452 }
1453 card_res->count_crtcs = crtc_count;
1454
1455 /* Encoders */
1456 if (card_res->count_encoders >= encoder_count) {
1457 copied = 0;
1458 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1459 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1460 list_for_each_entry(encoder,
1461 &dev->mode_config.encoder_list,
1462 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001463 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1464 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001465 if (put_user(encoder->base.id, encoder_id +
1466 copied)) {
1467 ret = -EFAULT;
1468 goto out;
1469 }
1470 copied++;
1471 }
1472 } else {
1473 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1474 if (put_user(mode_group->id_list[i],
1475 encoder_id + copied)) {
1476 ret = -EFAULT;
1477 goto out;
1478 }
1479 copied++;
1480 }
1481
1482 }
1483 }
1484 card_res->count_encoders = encoder_count;
1485
1486 /* Connectors */
1487 if (card_res->count_connectors >= connector_count) {
1488 copied = 0;
1489 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1490 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1491 list_for_each_entry(connector,
1492 &dev->mode_config.connector_list,
1493 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001494 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1495 connector->base.id,
1496 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001497 if (put_user(connector->base.id,
1498 connector_id + copied)) {
1499 ret = -EFAULT;
1500 goto out;
1501 }
1502 copied++;
1503 }
1504 } else {
1505 int start = mode_group->num_crtcs +
1506 mode_group->num_encoders;
1507 for (i = start; i < start + mode_group->num_connectors; i++) {
1508 if (put_user(mode_group->id_list[i],
1509 connector_id + copied)) {
1510 ret = -EFAULT;
1511 goto out;
1512 }
1513 copied++;
1514 }
1515 }
1516 }
1517 card_res->count_connectors = connector_count;
1518
Jerome Glisse94401062010-07-15 15:43:25 -04001519 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001520 card_res->count_connectors, card_res->count_encoders);
1521
1522out:
Daniel Vetter84849902012-12-02 00:28:11 +01001523 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001524 return ret;
1525}
1526
1527/**
1528 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001529 * @dev: drm device for the ioctl
1530 * @data: data pointer for the ioctl
1531 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001532 *
Dave Airlief453ba02008-11-07 14:05:41 -08001533 * Construct a CRTC configuration structure to return to the user.
1534 *
1535 * Called by the user via ioctl.
1536 *
1537 * RETURNS:
1538 * Zero on success, errno on failure.
1539 */
1540int drm_mode_getcrtc(struct drm_device *dev,
1541 void *data, struct drm_file *file_priv)
1542{
1543 struct drm_mode_crtc *crtc_resp = data;
1544 struct drm_crtc *crtc;
1545 struct drm_mode_object *obj;
1546 int ret = 0;
1547
Dave Airliefb3b06c2011-02-08 13:55:21 +10001548 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1549 return -EINVAL;
1550
Daniel Vetter84849902012-12-02 00:28:11 +01001551 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001552
1553 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1554 DRM_MODE_OBJECT_CRTC);
1555 if (!obj) {
1556 ret = -EINVAL;
1557 goto out;
1558 }
1559 crtc = obj_to_crtc(obj);
1560
1561 crtc_resp->x = crtc->x;
1562 crtc_resp->y = crtc->y;
1563 crtc_resp->gamma_size = crtc->gamma_size;
1564 if (crtc->fb)
1565 crtc_resp->fb_id = crtc->fb->base.id;
1566 else
1567 crtc_resp->fb_id = 0;
1568
1569 if (crtc->enabled) {
1570
1571 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1572 crtc_resp->mode_valid = 1;
1573
1574 } else {
1575 crtc_resp->mode_valid = 0;
1576 }
1577
1578out:
Daniel Vetter84849902012-12-02 00:28:11 +01001579 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001580 return ret;
1581}
1582
1583/**
1584 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001585 * @dev: drm device for the ioctl
1586 * @data: data pointer for the ioctl
1587 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001588 *
Dave Airlief453ba02008-11-07 14:05:41 -08001589 * Construct a connector configuration structure to return to the user.
1590 *
1591 * Called by the user via ioctl.
1592 *
1593 * RETURNS:
1594 * Zero on success, errno on failure.
1595 */
1596int drm_mode_getconnector(struct drm_device *dev, void *data,
1597 struct drm_file *file_priv)
1598{
1599 struct drm_mode_get_connector *out_resp = data;
1600 struct drm_mode_object *obj;
1601 struct drm_connector *connector;
1602 struct drm_display_mode *mode;
1603 int mode_count = 0;
1604 int props_count = 0;
1605 int encoders_count = 0;
1606 int ret = 0;
1607 int copied = 0;
1608 int i;
1609 struct drm_mode_modeinfo u_mode;
1610 struct drm_mode_modeinfo __user *mode_ptr;
1611 uint32_t __user *prop_ptr;
1612 uint64_t __user *prop_values;
1613 uint32_t __user *encoder_ptr;
1614
Dave Airliefb3b06c2011-02-08 13:55:21 +10001615 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1616 return -EINVAL;
1617
Dave Airlief453ba02008-11-07 14:05:41 -08001618 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1619
Jerome Glisse94401062010-07-15 15:43:25 -04001620 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001621
Daniel Vetter7b240562012-12-12 00:35:33 +01001622 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001623
1624 obj = drm_mode_object_find(dev, out_resp->connector_id,
1625 DRM_MODE_OBJECT_CONNECTOR);
1626 if (!obj) {
1627 ret = -EINVAL;
1628 goto out;
1629 }
1630 connector = obj_to_connector(obj);
1631
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001632 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001633
1634 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1635 if (connector->encoder_ids[i] != 0) {
1636 encoders_count++;
1637 }
1638 }
1639
1640 if (out_resp->count_modes == 0) {
1641 connector->funcs->fill_modes(connector,
1642 dev->mode_config.max_width,
1643 dev->mode_config.max_height);
1644 }
1645
1646 /* delayed so we get modes regardless of pre-fill_modes state */
1647 list_for_each_entry(mode, &connector->modes, head)
1648 mode_count++;
1649
1650 out_resp->connector_id = connector->base.id;
1651 out_resp->connector_type = connector->connector_type;
1652 out_resp->connector_type_id = connector->connector_type_id;
1653 out_resp->mm_width = connector->display_info.width_mm;
1654 out_resp->mm_height = connector->display_info.height_mm;
1655 out_resp->subpixel = connector->display_info.subpixel_order;
1656 out_resp->connection = connector->status;
1657 if (connector->encoder)
1658 out_resp->encoder_id = connector->encoder->base.id;
1659 else
1660 out_resp->encoder_id = 0;
1661
1662 /*
1663 * This ioctl is called twice, once to determine how much space is
1664 * needed, and the 2nd time to fill it.
1665 */
1666 if ((out_resp->count_modes >= mode_count) && mode_count) {
1667 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001668 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001669 list_for_each_entry(mode, &connector->modes, head) {
1670 drm_crtc_convert_to_umode(&u_mode, mode);
1671 if (copy_to_user(mode_ptr + copied,
1672 &u_mode, sizeof(u_mode))) {
1673 ret = -EFAULT;
1674 goto out;
1675 }
1676 copied++;
1677 }
1678 }
1679 out_resp->count_modes = mode_count;
1680
1681 if ((out_resp->count_props >= props_count) && props_count) {
1682 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001683 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1684 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001685 for (i = 0; i < connector->properties.count; i++) {
1686 if (put_user(connector->properties.ids[i],
1687 prop_ptr + copied)) {
1688 ret = -EFAULT;
1689 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001690 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001691
1692 if (put_user(connector->properties.values[i],
1693 prop_values + copied)) {
1694 ret = -EFAULT;
1695 goto out;
1696 }
1697 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001698 }
1699 }
1700 out_resp->count_props = props_count;
1701
1702 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1703 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001704 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001705 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1706 if (connector->encoder_ids[i] != 0) {
1707 if (put_user(connector->encoder_ids[i],
1708 encoder_ptr + copied)) {
1709 ret = -EFAULT;
1710 goto out;
1711 }
1712 copied++;
1713 }
1714 }
1715 }
1716 out_resp->count_encoders = encoders_count;
1717
1718out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001719 mutex_unlock(&dev->mode_config.mutex);
1720
Dave Airlief453ba02008-11-07 14:05:41 -08001721 return ret;
1722}
1723
1724int drm_mode_getencoder(struct drm_device *dev, void *data,
1725 struct drm_file *file_priv)
1726{
1727 struct drm_mode_get_encoder *enc_resp = data;
1728 struct drm_mode_object *obj;
1729 struct drm_encoder *encoder;
1730 int ret = 0;
1731
Dave Airliefb3b06c2011-02-08 13:55:21 +10001732 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1733 return -EINVAL;
1734
Daniel Vetter84849902012-12-02 00:28:11 +01001735 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001736 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1737 DRM_MODE_OBJECT_ENCODER);
1738 if (!obj) {
1739 ret = -EINVAL;
1740 goto out;
1741 }
1742 encoder = obj_to_encoder(obj);
1743
1744 if (encoder->crtc)
1745 enc_resp->crtc_id = encoder->crtc->base.id;
1746 else
1747 enc_resp->crtc_id = 0;
1748 enc_resp->encoder_type = encoder->encoder_type;
1749 enc_resp->encoder_id = encoder->base.id;
1750 enc_resp->possible_crtcs = encoder->possible_crtcs;
1751 enc_resp->possible_clones = encoder->possible_clones;
1752
1753out:
Daniel Vetter84849902012-12-02 00:28:11 +01001754 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001755 return ret;
1756}
1757
1758/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001759 * drm_mode_getplane_res - get plane info
1760 * @dev: DRM device
1761 * @data: ioctl data
1762 * @file_priv: DRM file info
1763 *
1764 * Return an plane count and set of IDs.
1765 */
1766int drm_mode_getplane_res(struct drm_device *dev, void *data,
1767 struct drm_file *file_priv)
1768{
1769 struct drm_mode_get_plane_res *plane_resp = data;
1770 struct drm_mode_config *config;
1771 struct drm_plane *plane;
1772 uint32_t __user *plane_ptr;
1773 int copied = 0, ret = 0;
1774
1775 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1776 return -EINVAL;
1777
Daniel Vetter84849902012-12-02 00:28:11 +01001778 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001779 config = &dev->mode_config;
1780
1781 /*
1782 * This ioctl is called twice, once to determine how much space is
1783 * needed, and the 2nd time to fill it.
1784 */
1785 if (config->num_plane &&
1786 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001787 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001788
1789 list_for_each_entry(plane, &config->plane_list, head) {
1790 if (put_user(plane->base.id, plane_ptr + copied)) {
1791 ret = -EFAULT;
1792 goto out;
1793 }
1794 copied++;
1795 }
1796 }
1797 plane_resp->count_planes = config->num_plane;
1798
1799out:
Daniel Vetter84849902012-12-02 00:28:11 +01001800 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001801 return ret;
1802}
1803
1804/**
1805 * drm_mode_getplane - get plane info
1806 * @dev: DRM device
1807 * @data: ioctl data
1808 * @file_priv: DRM file info
1809 *
1810 * Return plane info, including formats supported, gamma size, any
1811 * current fb, etc.
1812 */
1813int drm_mode_getplane(struct drm_device *dev, void *data,
1814 struct drm_file *file_priv)
1815{
1816 struct drm_mode_get_plane *plane_resp = data;
1817 struct drm_mode_object *obj;
1818 struct drm_plane *plane;
1819 uint32_t __user *format_ptr;
1820 int ret = 0;
1821
1822 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1823 return -EINVAL;
1824
Daniel Vetter84849902012-12-02 00:28:11 +01001825 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001826 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1827 DRM_MODE_OBJECT_PLANE);
1828 if (!obj) {
1829 ret = -ENOENT;
1830 goto out;
1831 }
1832 plane = obj_to_plane(obj);
1833
1834 if (plane->crtc)
1835 plane_resp->crtc_id = plane->crtc->base.id;
1836 else
1837 plane_resp->crtc_id = 0;
1838
1839 if (plane->fb)
1840 plane_resp->fb_id = plane->fb->base.id;
1841 else
1842 plane_resp->fb_id = 0;
1843
1844 plane_resp->plane_id = plane->base.id;
1845 plane_resp->possible_crtcs = plane->possible_crtcs;
1846 plane_resp->gamma_size = plane->gamma_size;
1847
1848 /*
1849 * This ioctl is called twice, once to determine how much space is
1850 * needed, and the 2nd time to fill it.
1851 */
1852 if (plane->format_count &&
1853 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001854 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001855 if (copy_to_user(format_ptr,
1856 plane->format_types,
1857 sizeof(uint32_t) * plane->format_count)) {
1858 ret = -EFAULT;
1859 goto out;
1860 }
1861 }
1862 plane_resp->count_format_types = plane->format_count;
1863
1864out:
Daniel Vetter84849902012-12-02 00:28:11 +01001865 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001866 return ret;
1867}
1868
1869/**
1870 * drm_mode_setplane - set up or tear down an plane
1871 * @dev: DRM device
1872 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001873 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001874 *
1875 * Set plane info, including placement, fb, scaling, and other factors.
1876 * Or pass a NULL fb to disable.
1877 */
1878int drm_mode_setplane(struct drm_device *dev, void *data,
1879 struct drm_file *file_priv)
1880{
1881 struct drm_mode_set_plane *plane_req = data;
1882 struct drm_mode_object *obj;
1883 struct drm_plane *plane;
1884 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001885 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001886 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001887 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001888 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001889
1890 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1891 return -EINVAL;
1892
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001893 /*
1894 * First, find the plane, crtc, and fb objects. If not available,
1895 * we don't bother to call the driver.
1896 */
1897 obj = drm_mode_object_find(dev, plane_req->plane_id,
1898 DRM_MODE_OBJECT_PLANE);
1899 if (!obj) {
1900 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1901 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001902 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001903 }
1904 plane = obj_to_plane(obj);
1905
1906 /* No fb means shut it down */
1907 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001908 drm_modeset_lock_all(dev);
1909 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001910 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001911 plane->crtc = NULL;
1912 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001913 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001914 goto out;
1915 }
1916
1917 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1918 DRM_MODE_OBJECT_CRTC);
1919 if (!obj) {
1920 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1921 plane_req->crtc_id);
1922 ret = -ENOENT;
1923 goto out;
1924 }
1925 crtc = obj_to_crtc(obj);
1926
Daniel Vetter786b99e2012-12-02 21:53:40 +01001927 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1928 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001929 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1930 plane_req->fb_id);
1931 ret = -ENOENT;
1932 goto out;
1933 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001934
Ville Syrjälä62443be2011-12-20 00:06:47 +02001935 /* Check whether this plane supports the fb pixel format. */
1936 for (i = 0; i < plane->format_count; i++)
1937 if (fb->pixel_format == plane->format_types[i])
1938 break;
1939 if (i == plane->format_count) {
1940 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1941 ret = -EINVAL;
1942 goto out;
1943 }
1944
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001945 fb_width = fb->width << 16;
1946 fb_height = fb->height << 16;
1947
1948 /* Make sure source coordinates are inside the fb. */
1949 if (plane_req->src_w > fb_width ||
1950 plane_req->src_x > fb_width - plane_req->src_w ||
1951 plane_req->src_h > fb_height ||
1952 plane_req->src_y > fb_height - plane_req->src_h) {
1953 DRM_DEBUG_KMS("Invalid source coordinates "
1954 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1955 plane_req->src_w >> 16,
1956 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1957 plane_req->src_h >> 16,
1958 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1959 plane_req->src_x >> 16,
1960 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1961 plane_req->src_y >> 16,
1962 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1963 ret = -ENOSPC;
1964 goto out;
1965 }
1966
Ville Syrjälä687a0402011-12-20 00:06:45 +02001967 /* Give drivers some help against integer overflows */
1968 if (plane_req->crtc_w > INT_MAX ||
1969 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1970 plane_req->crtc_h > INT_MAX ||
1971 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1972 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1973 plane_req->crtc_w, plane_req->crtc_h,
1974 plane_req->crtc_x, plane_req->crtc_y);
1975 ret = -ERANGE;
1976 goto out;
1977 }
1978
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001979 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001980 ret = plane->funcs->update_plane(plane, crtc, fb,
1981 plane_req->crtc_x, plane_req->crtc_y,
1982 plane_req->crtc_w, plane_req->crtc_h,
1983 plane_req->src_x, plane_req->src_y,
1984 plane_req->src_w, plane_req->src_h);
1985 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001986 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001987 plane->crtc = crtc;
1988 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001989 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001990 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001991 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001992
1993out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001994 if (fb)
1995 drm_framebuffer_unreference(fb);
1996 if (old_fb)
1997 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001998
1999 return ret;
2000}
2001
2002/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002003 * drm_mode_set_config_internal - helper to call ->set_config
2004 * @set: modeset config to set
2005 *
2006 * This is a little helper to wrap internal calls to the ->set_config driver
2007 * interface. The only thing it adds is correct refcounting dance.
2008 */
2009int drm_mode_set_config_internal(struct drm_mode_set *set)
2010{
2011 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002012 struct drm_framebuffer *fb, *old_fb;
2013 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002014
Daniel Vetterb0d12322012-12-11 01:07:12 +01002015 old_fb = crtc->fb;
2016 fb = set->fb;
2017
2018 ret = crtc->funcs->set_config(set);
2019 if (ret == 0) {
2020 if (old_fb)
2021 drm_framebuffer_unreference(old_fb);
2022 if (fb)
2023 drm_framebuffer_reference(fb);
2024 }
2025
2026 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002027}
2028EXPORT_SYMBOL(drm_mode_set_config_internal);
2029
2030/**
Dave Airlief453ba02008-11-07 14:05:41 -08002031 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002032 * @dev: drm device for the ioctl
2033 * @data: data pointer for the ioctl
2034 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002035 *
Dave Airlief453ba02008-11-07 14:05:41 -08002036 * Build a new CRTC configuration based on user request.
2037 *
2038 * Called by the user via ioctl.
2039 *
2040 * RETURNS:
2041 * Zero on success, errno on failure.
2042 */
2043int drm_mode_setcrtc(struct drm_device *dev, void *data,
2044 struct drm_file *file_priv)
2045{
2046 struct drm_mode_config *config = &dev->mode_config;
2047 struct drm_mode_crtc *crtc_req = data;
2048 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002049 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002050 struct drm_connector **connector_set = NULL, *connector;
2051 struct drm_framebuffer *fb = NULL;
2052 struct drm_display_mode *mode = NULL;
2053 struct drm_mode_set set;
2054 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002055 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002056 int i;
2057
Dave Airliefb3b06c2011-02-08 13:55:21 +10002058 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2059 return -EINVAL;
2060
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002061 /* For some reason crtc x/y offsets are signed internally. */
2062 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2063 return -ERANGE;
2064
Daniel Vetter84849902012-12-02 00:28:11 +01002065 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002066 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2067 DRM_MODE_OBJECT_CRTC);
2068 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002069 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002070 ret = -EINVAL;
2071 goto out;
2072 }
2073 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002074 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002075
2076 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002077 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002078 /* If we have a mode we need a framebuffer. */
2079 /* If we pass -1, set the mode with the currently bound fb */
2080 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002081 if (!crtc->fb) {
2082 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2083 ret = -EINVAL;
2084 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002085 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002086 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002087 /* Make refcounting symmetric with the lookup path. */
2088 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002089 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002090 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2091 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002092 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2093 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002094 ret = -EINVAL;
2095 goto out;
2096 }
Dave Airlief453ba02008-11-07 14:05:41 -08002097 }
2098
2099 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002100 if (!mode) {
2101 ret = -ENOMEM;
2102 goto out;
2103 }
2104
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002105 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2106 if (ret) {
2107 DRM_DEBUG_KMS("Invalid mode\n");
2108 goto out;
2109 }
2110
Dave Airlief453ba02008-11-07 14:05:41 -08002111 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002112
Rob Clark7c80e122012-09-04 16:35:56 +00002113 hdisplay = mode->hdisplay;
2114 vdisplay = mode->vdisplay;
2115
2116 if (crtc->invert_dimensions)
2117 swap(hdisplay, vdisplay);
2118
2119 if (hdisplay > fb->width ||
2120 vdisplay > fb->height ||
2121 crtc_req->x > fb->width - hdisplay ||
2122 crtc_req->y > fb->height - vdisplay) {
2123 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2124 fb->width, fb->height,
2125 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2126 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002127 ret = -ENOSPC;
2128 goto out;
2129 }
Dave Airlief453ba02008-11-07 14:05:41 -08002130 }
2131
2132 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002133 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002134 ret = -EINVAL;
2135 goto out;
2136 }
2137
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002138 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002139 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002140 crtc_req->count_connectors);
2141 ret = -EINVAL;
2142 goto out;
2143 }
2144
2145 if (crtc_req->count_connectors > 0) {
2146 u32 out_id;
2147
2148 /* Avoid unbounded kernel memory allocation */
2149 if (crtc_req->count_connectors > config->num_connector) {
2150 ret = -EINVAL;
2151 goto out;
2152 }
2153
2154 connector_set = kmalloc(crtc_req->count_connectors *
2155 sizeof(struct drm_connector *),
2156 GFP_KERNEL);
2157 if (!connector_set) {
2158 ret = -ENOMEM;
2159 goto out;
2160 }
2161
2162 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002163 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002164 if (get_user(out_id, &set_connectors_ptr[i])) {
2165 ret = -EFAULT;
2166 goto out;
2167 }
2168
2169 obj = drm_mode_object_find(dev, out_id,
2170 DRM_MODE_OBJECT_CONNECTOR);
2171 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002172 DRM_DEBUG_KMS("Connector id %d unknown\n",
2173 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002174 ret = -EINVAL;
2175 goto out;
2176 }
2177 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002178 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2179 connector->base.id,
2180 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002181
2182 connector_set[i] = connector;
2183 }
2184 }
2185
2186 set.crtc = crtc;
2187 set.x = crtc_req->x;
2188 set.y = crtc_req->y;
2189 set.mode = mode;
2190 set.connectors = connector_set;
2191 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002192 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002193 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002194
2195out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002196 if (fb)
2197 drm_framebuffer_unreference(fb);
2198
Dave Airlief453ba02008-11-07 14:05:41 -08002199 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002200 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002201 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002202 return ret;
2203}
2204
2205int drm_mode_cursor_ioctl(struct drm_device *dev,
2206 void *data, struct drm_file *file_priv)
2207{
2208 struct drm_mode_cursor *req = data;
2209 struct drm_mode_object *obj;
2210 struct drm_crtc *crtc;
2211 int ret = 0;
2212
Dave Airliefb3b06c2011-02-08 13:55:21 +10002213 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2214 return -EINVAL;
2215
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002216 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002217 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002218
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002219 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002220 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002221 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002222 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002223 }
2224 crtc = obj_to_crtc(obj);
2225
Daniel Vetterdac35662012-12-02 15:24:10 +01002226 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002227 if (req->flags & DRM_MODE_CURSOR_BO) {
2228 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002229 ret = -ENXIO;
2230 goto out;
2231 }
2232 /* Turns off the cursor if handle is 0 */
2233 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2234 req->width, req->height);
2235 }
2236
2237 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2238 if (crtc->funcs->cursor_move) {
2239 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2240 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002241 ret = -EFAULT;
2242 goto out;
2243 }
2244 }
2245out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002246 mutex_unlock(&crtc->mutex);
2247
Dave Airlief453ba02008-11-07 14:05:41 -08002248 return ret;
2249}
2250
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002251/* Original addfb only supported RGB formats, so figure out which one */
2252uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2253{
2254 uint32_t fmt;
2255
2256 switch (bpp) {
2257 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002258 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002259 break;
2260 case 16:
2261 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002262 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002263 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002264 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002265 break;
2266 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002267 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002268 break;
2269 case 32:
2270 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002271 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002272 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002273 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002274 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002275 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002276 break;
2277 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002278 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2279 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002280 break;
2281 }
2282
2283 return fmt;
2284}
2285EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2286
Dave Airlief453ba02008-11-07 14:05:41 -08002287/**
2288 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002289 * @dev: drm device for the ioctl
2290 * @data: data pointer for the ioctl
2291 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002292 *
Dave Airlief453ba02008-11-07 14:05:41 -08002293 * Add a new FB to the specified CRTC, given a user request.
2294 *
2295 * Called by the user via ioctl.
2296 *
2297 * RETURNS:
2298 * Zero on success, errno on failure.
2299 */
2300int drm_mode_addfb(struct drm_device *dev,
2301 void *data, struct drm_file *file_priv)
2302{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002303 struct drm_mode_fb_cmd *or = data;
2304 struct drm_mode_fb_cmd2 r = {};
2305 struct drm_mode_config *config = &dev->mode_config;
2306 struct drm_framebuffer *fb;
2307 int ret = 0;
2308
2309 /* Use new struct with format internally */
2310 r.fb_id = or->fb_id;
2311 r.width = or->width;
2312 r.height = or->height;
2313 r.pitches[0] = or->pitch;
2314 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2315 r.handles[0] = or->handle;
2316
2317 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2318 return -EINVAL;
2319
Jesse Barnesacb4b992011-11-07 12:03:23 -08002320 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002321 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002322
2323 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002324 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002325
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002326 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2327 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002328 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002329 drm_modeset_unlock_all(dev);
2330 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002331 }
2332
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002333 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002334 or->fb_id = fb->base.id;
2335 list_add(&fb->filp_head, &file_priv->fbs);
2336 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002337 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002338
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002339 return ret;
2340}
2341
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002342static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002343{
2344 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2345
2346 switch (format) {
2347 case DRM_FORMAT_C8:
2348 case DRM_FORMAT_RGB332:
2349 case DRM_FORMAT_BGR233:
2350 case DRM_FORMAT_XRGB4444:
2351 case DRM_FORMAT_XBGR4444:
2352 case DRM_FORMAT_RGBX4444:
2353 case DRM_FORMAT_BGRX4444:
2354 case DRM_FORMAT_ARGB4444:
2355 case DRM_FORMAT_ABGR4444:
2356 case DRM_FORMAT_RGBA4444:
2357 case DRM_FORMAT_BGRA4444:
2358 case DRM_FORMAT_XRGB1555:
2359 case DRM_FORMAT_XBGR1555:
2360 case DRM_FORMAT_RGBX5551:
2361 case DRM_FORMAT_BGRX5551:
2362 case DRM_FORMAT_ARGB1555:
2363 case DRM_FORMAT_ABGR1555:
2364 case DRM_FORMAT_RGBA5551:
2365 case DRM_FORMAT_BGRA5551:
2366 case DRM_FORMAT_RGB565:
2367 case DRM_FORMAT_BGR565:
2368 case DRM_FORMAT_RGB888:
2369 case DRM_FORMAT_BGR888:
2370 case DRM_FORMAT_XRGB8888:
2371 case DRM_FORMAT_XBGR8888:
2372 case DRM_FORMAT_RGBX8888:
2373 case DRM_FORMAT_BGRX8888:
2374 case DRM_FORMAT_ARGB8888:
2375 case DRM_FORMAT_ABGR8888:
2376 case DRM_FORMAT_RGBA8888:
2377 case DRM_FORMAT_BGRA8888:
2378 case DRM_FORMAT_XRGB2101010:
2379 case DRM_FORMAT_XBGR2101010:
2380 case DRM_FORMAT_RGBX1010102:
2381 case DRM_FORMAT_BGRX1010102:
2382 case DRM_FORMAT_ARGB2101010:
2383 case DRM_FORMAT_ABGR2101010:
2384 case DRM_FORMAT_RGBA1010102:
2385 case DRM_FORMAT_BGRA1010102:
2386 case DRM_FORMAT_YUYV:
2387 case DRM_FORMAT_YVYU:
2388 case DRM_FORMAT_UYVY:
2389 case DRM_FORMAT_VYUY:
2390 case DRM_FORMAT_AYUV:
2391 case DRM_FORMAT_NV12:
2392 case DRM_FORMAT_NV21:
2393 case DRM_FORMAT_NV16:
2394 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002395 case DRM_FORMAT_NV24:
2396 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002397 case DRM_FORMAT_YUV410:
2398 case DRM_FORMAT_YVU410:
2399 case DRM_FORMAT_YUV411:
2400 case DRM_FORMAT_YVU411:
2401 case DRM_FORMAT_YUV420:
2402 case DRM_FORMAT_YVU420:
2403 case DRM_FORMAT_YUV422:
2404 case DRM_FORMAT_YVU422:
2405 case DRM_FORMAT_YUV444:
2406 case DRM_FORMAT_YVU444:
2407 return 0;
2408 default:
2409 return -EINVAL;
2410 }
2411}
2412
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002413static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002414{
2415 int ret, hsub, vsub, num_planes, i;
2416
2417 ret = format_check(r);
2418 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002419 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002420 return ret;
2421 }
2422
2423 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2424 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2425 num_planes = drm_format_num_planes(r->pixel_format);
2426
2427 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002428 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002429 return -EINVAL;
2430 }
2431
2432 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002433 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002434 return -EINVAL;
2435 }
2436
2437 for (i = 0; i < num_planes; i++) {
2438 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002439 unsigned int height = r->height / (i != 0 ? vsub : 1);
2440 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002441
2442 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002443 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002444 return -EINVAL;
2445 }
2446
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002447 if ((uint64_t) width * cpp > UINT_MAX)
2448 return -ERANGE;
2449
2450 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2451 return -ERANGE;
2452
2453 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002454 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002455 return -EINVAL;
2456 }
2457 }
2458
2459 return 0;
2460}
2461
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002462/**
2463 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002464 * @dev: drm device for the ioctl
2465 * @data: data pointer for the ioctl
2466 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002467 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002468 * Add a new FB to the specified CRTC, given a user request with format.
2469 *
2470 * Called by the user via ioctl.
2471 *
2472 * RETURNS:
2473 * Zero on success, errno on failure.
2474 */
2475int drm_mode_addfb2(struct drm_device *dev,
2476 void *data, struct drm_file *file_priv)
2477{
2478 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002479 struct drm_mode_config *config = &dev->mode_config;
2480 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002481 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002482
Dave Airliefb3b06c2011-02-08 13:55:21 +10002483 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2484 return -EINVAL;
2485
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002486 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2487 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2488 return -EINVAL;
2489 }
2490
Dave Airlief453ba02008-11-07 14:05:41 -08002491 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002492 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002493 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002494 return -EINVAL;
2495 }
2496 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002497 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002498 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002499 return -EINVAL;
2500 }
2501
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002502 ret = framebuffer_check(r);
2503 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002504 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002505
Dave Airlief453ba02008-11-07 14:05:41 -08002506 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002507 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002508 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002509 drm_modeset_unlock_all(dev);
2510 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002511 }
2512
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002513 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002514 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002515 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002516 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002517 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002518
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002519
Dave Airlief453ba02008-11-07 14:05:41 -08002520 return ret;
2521}
2522
2523/**
2524 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002525 * @dev: drm device for the ioctl
2526 * @data: data pointer for the ioctl
2527 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002528 *
Dave Airlief453ba02008-11-07 14:05:41 -08002529 * Remove the FB specified by the user.
2530 *
2531 * Called by the user via ioctl.
2532 *
2533 * RETURNS:
2534 * Zero on success, errno on failure.
2535 */
2536int drm_mode_rmfb(struct drm_device *dev,
2537 void *data, struct drm_file *file_priv)
2538{
Dave Airlief453ba02008-11-07 14:05:41 -08002539 struct drm_framebuffer *fb = NULL;
2540 struct drm_framebuffer *fbl = NULL;
2541 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002542 int found = 0;
2543
Dave Airliefb3b06c2011-02-08 13:55:21 +10002544 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2545 return -EINVAL;
2546
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002547 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002548 mutex_lock(&dev->mode_config.fb_lock);
2549 fb = __drm_framebuffer_lookup(dev, *id);
2550 if (!fb)
2551 goto fail_lookup;
2552
Dave Airlief453ba02008-11-07 14:05:41 -08002553 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2554 if (fb == fbl)
2555 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002556 if (!found)
2557 goto fail_lookup;
2558
2559 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2560 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002561
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002562 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002563 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002564 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002565
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002566 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002567
Daniel Vetter2b677e82012-12-10 21:16:05 +01002568 return 0;
2569
2570fail_lookup:
2571 mutex_unlock(&dev->mode_config.fb_lock);
2572 mutex_unlock(&file_priv->fbs_lock);
2573
2574 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002575}
2576
2577/**
2578 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002579 * @dev: drm device for the ioctl
2580 * @data: data pointer for the ioctl
2581 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002582 *
Dave Airlief453ba02008-11-07 14:05:41 -08002583 * Lookup the FB given its ID and return info about it.
2584 *
2585 * Called by the user via ioctl.
2586 *
2587 * RETURNS:
2588 * Zero on success, errno on failure.
2589 */
2590int drm_mode_getfb(struct drm_device *dev,
2591 void *data, struct drm_file *file_priv)
2592{
2593 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002594 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002595 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002596
Dave Airliefb3b06c2011-02-08 13:55:21 +10002597 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2598 return -EINVAL;
2599
Daniel Vetter786b99e2012-12-02 21:53:40 +01002600 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002601 if (!fb)
2602 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002603
2604 r->height = fb->height;
2605 r->width = fb->width;
2606 r->depth = fb->depth;
2607 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002608 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002609 if (fb->funcs->create_handle)
2610 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2611 else
2612 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002613
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002614 drm_framebuffer_unreference(fb);
2615
Dave Airlief453ba02008-11-07 14:05:41 -08002616 return ret;
2617}
2618
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002619int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2620 void *data, struct drm_file *file_priv)
2621{
2622 struct drm_clip_rect __user *clips_ptr;
2623 struct drm_clip_rect *clips = NULL;
2624 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002625 struct drm_framebuffer *fb;
2626 unsigned flags;
2627 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002628 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002629
Dave Airliefb3b06c2011-02-08 13:55:21 +10002630 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2631 return -EINVAL;
2632
Daniel Vetter786b99e2012-12-02 21:53:40 +01002633 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002634 if (!fb)
2635 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002636
2637 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002638 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002639
2640 if (!num_clips != !clips_ptr) {
2641 ret = -EINVAL;
2642 goto out_err1;
2643 }
2644
2645 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2646
2647 /* If userspace annotates copy, clips must come in pairs */
2648 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2649 ret = -EINVAL;
2650 goto out_err1;
2651 }
2652
2653 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002654 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2655 ret = -EINVAL;
2656 goto out_err1;
2657 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002658 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2659 if (!clips) {
2660 ret = -ENOMEM;
2661 goto out_err1;
2662 }
2663
2664 ret = copy_from_user(clips, clips_ptr,
2665 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002666 if (ret) {
2667 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002668 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002669 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002670 }
2671
2672 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002673 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002674 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2675 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002676 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002677 } else {
2678 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002679 }
2680
2681out_err2:
2682 kfree(clips);
2683out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002684 drm_framebuffer_unreference(fb);
2685
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002686 return ret;
2687}
2688
2689
Dave Airlief453ba02008-11-07 14:05:41 -08002690/**
2691 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002692 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002693 *
Dave Airlief453ba02008-11-07 14:05:41 -08002694 * Destroy all the FBs associated with @filp.
2695 *
2696 * Called by the user via ioctl.
2697 *
2698 * RETURNS:
2699 * Zero on success, errno on failure.
2700 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002701void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002702{
Dave Airlief453ba02008-11-07 14:05:41 -08002703 struct drm_device *dev = priv->minor->dev;
2704 struct drm_framebuffer *fb, *tfb;
2705
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002706 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002707 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002708
2709 mutex_lock(&dev->mode_config.fb_lock);
2710 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2711 __drm_framebuffer_unregister(dev, fb);
2712 mutex_unlock(&dev->mode_config.fb_lock);
2713
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002714 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002715
2716 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002717 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002718 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002719 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002720}
2721
2722/**
2723 * drm_mode_attachmode - add a mode to the user mode list
2724 * @dev: DRM device
2725 * @connector: connector to add the mode to
2726 * @mode: mode to add
2727 *
2728 * Add @mode to @connector's user mode list.
2729 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002730static void drm_mode_attachmode(struct drm_device *dev,
2731 struct drm_connector *connector,
2732 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002733{
Dave Airlief453ba02008-11-07 14:05:41 -08002734 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002735}
2736
2737int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002738 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002739{
2740 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002741 int ret = 0;
2742 struct drm_display_mode *dup_mode, *next;
2743 LIST_HEAD(list);
2744
Dave Airlief453ba02008-11-07 14:05:41 -08002745 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2746 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002747 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002748 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002749 dup_mode = drm_mode_duplicate(dev, mode);
2750 if (!dup_mode) {
2751 ret = -ENOMEM;
2752 goto out;
2753 }
2754 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002755 }
2756 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002757
2758 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2759 if (!connector->encoder)
2760 continue;
2761 if (connector->encoder->crtc == crtc)
2762 list_move_tail(list.next, &connector->user_modes);
2763 }
2764
2765 WARN_ON(!list_empty(&list));
2766
2767 out:
2768 list_for_each_entry_safe(dup_mode, next, &list, head)
2769 drm_mode_destroy(dev, dup_mode);
2770
2771 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002772}
2773EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2774
2775static int drm_mode_detachmode(struct drm_device *dev,
2776 struct drm_connector *connector,
2777 struct drm_display_mode *mode)
2778{
2779 int found = 0;
2780 int ret = 0;
2781 struct drm_display_mode *match_mode, *t;
2782
2783 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2784 if (drm_mode_equal(match_mode, mode)) {
2785 list_del(&match_mode->head);
2786 drm_mode_destroy(dev, match_mode);
2787 found = 1;
2788 break;
2789 }
2790 }
2791
2792 if (!found)
2793 ret = -EINVAL;
2794
2795 return ret;
2796}
2797
2798int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2799{
2800 struct drm_connector *connector;
2801
2802 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2803 drm_mode_detachmode(dev, connector, mode);
2804 }
2805 return 0;
2806}
2807EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2808
2809/**
2810 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002811 * @dev: drm device for the ioctl
2812 * @data: data pointer for the ioctl
2813 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002814 *
2815 * This attaches a user specified mode to an connector.
2816 * Called by the user via ioctl.
2817 *
2818 * RETURNS:
2819 * Zero on success, errno on failure.
2820 */
2821int drm_mode_attachmode_ioctl(struct drm_device *dev,
2822 void *data, struct drm_file *file_priv)
2823{
2824 struct drm_mode_mode_cmd *mode_cmd = data;
2825 struct drm_connector *connector;
2826 struct drm_display_mode *mode;
2827 struct drm_mode_object *obj;
2828 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002829 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002830
Dave Airliefb3b06c2011-02-08 13:55:21 +10002831 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2832 return -EINVAL;
2833
Daniel Vetter84849902012-12-02 00:28:11 +01002834 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002835
2836 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2837 if (!obj) {
2838 ret = -EINVAL;
2839 goto out;
2840 }
2841 connector = obj_to_connector(obj);
2842
2843 mode = drm_mode_create(dev);
2844 if (!mode) {
2845 ret = -ENOMEM;
2846 goto out;
2847 }
2848
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002849 ret = drm_crtc_convert_umode(mode, umode);
2850 if (ret) {
2851 DRM_DEBUG_KMS("Invalid mode\n");
2852 drm_mode_destroy(dev, mode);
2853 goto out;
2854 }
Dave Airlief453ba02008-11-07 14:05:41 -08002855
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002856 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002857out:
Daniel Vetter84849902012-12-02 00:28:11 +01002858 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002859 return ret;
2860}
2861
2862
2863/**
2864 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002865 * @dev: drm device for the ioctl
2866 * @data: data pointer for the ioctl
2867 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002868 *
2869 * Called by the user via ioctl.
2870 *
2871 * RETURNS:
2872 * Zero on success, errno on failure.
2873 */
2874int drm_mode_detachmode_ioctl(struct drm_device *dev,
2875 void *data, struct drm_file *file_priv)
2876{
2877 struct drm_mode_object *obj;
2878 struct drm_mode_mode_cmd *mode_cmd = data;
2879 struct drm_connector *connector;
2880 struct drm_display_mode mode;
2881 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002882 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002883
Dave Airliefb3b06c2011-02-08 13:55:21 +10002884 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2885 return -EINVAL;
2886
Daniel Vetter84849902012-12-02 00:28:11 +01002887 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002888
2889 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2890 if (!obj) {
2891 ret = -EINVAL;
2892 goto out;
2893 }
2894 connector = obj_to_connector(obj);
2895
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002896 ret = drm_crtc_convert_umode(&mode, umode);
2897 if (ret) {
2898 DRM_DEBUG_KMS("Invalid mode\n");
2899 goto out;
2900 }
2901
Dave Airlief453ba02008-11-07 14:05:41 -08002902 ret = drm_mode_detachmode(dev, connector, &mode);
2903out:
Daniel Vetter84849902012-12-02 00:28:11 +01002904 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002905 return ret;
2906}
2907
2908struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2909 const char *name, int num_values)
2910{
2911 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002912 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002913
2914 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2915 if (!property)
2916 return NULL;
2917
2918 if (num_values) {
2919 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2920 if (!property->values)
2921 goto fail;
2922 }
2923
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002924 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2925 if (ret)
2926 goto fail;
2927
Dave Airlief453ba02008-11-07 14:05:41 -08002928 property->flags = flags;
2929 property->num_values = num_values;
2930 INIT_LIST_HEAD(&property->enum_blob_list);
2931
Vinson Lee471dd2e2011-11-10 11:55:40 -08002932 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002933 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002934 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2935 }
Dave Airlief453ba02008-11-07 14:05:41 -08002936
2937 list_add_tail(&property->head, &dev->mode_config.property_list);
2938 return property;
2939fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002940 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002941 kfree(property);
2942 return NULL;
2943}
2944EXPORT_SYMBOL(drm_property_create);
2945
Sascha Hauer4a67d392012-02-06 10:58:17 +01002946struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2947 const char *name,
2948 const struct drm_prop_enum_list *props,
2949 int num_values)
2950{
2951 struct drm_property *property;
2952 int i, ret;
2953
2954 flags |= DRM_MODE_PROP_ENUM;
2955
2956 property = drm_property_create(dev, flags, name, num_values);
2957 if (!property)
2958 return NULL;
2959
2960 for (i = 0; i < num_values; i++) {
2961 ret = drm_property_add_enum(property, i,
2962 props[i].type,
2963 props[i].name);
2964 if (ret) {
2965 drm_property_destroy(dev, property);
2966 return NULL;
2967 }
2968 }
2969
2970 return property;
2971}
2972EXPORT_SYMBOL(drm_property_create_enum);
2973
Rob Clark49e27542012-05-17 02:23:26 -06002974struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2975 int flags, const char *name,
2976 const struct drm_prop_enum_list *props,
2977 int num_values)
2978{
2979 struct drm_property *property;
2980 int i, ret;
2981
2982 flags |= DRM_MODE_PROP_BITMASK;
2983
2984 property = drm_property_create(dev, flags, name, num_values);
2985 if (!property)
2986 return NULL;
2987
2988 for (i = 0; i < num_values; i++) {
2989 ret = drm_property_add_enum(property, i,
2990 props[i].type,
2991 props[i].name);
2992 if (ret) {
2993 drm_property_destroy(dev, property);
2994 return NULL;
2995 }
2996 }
2997
2998 return property;
2999}
3000EXPORT_SYMBOL(drm_property_create_bitmask);
3001
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003002struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3003 const char *name,
3004 uint64_t min, uint64_t max)
3005{
3006 struct drm_property *property;
3007
3008 flags |= DRM_MODE_PROP_RANGE;
3009
3010 property = drm_property_create(dev, flags, name, 2);
3011 if (!property)
3012 return NULL;
3013
3014 property->values[0] = min;
3015 property->values[1] = max;
3016
3017 return property;
3018}
3019EXPORT_SYMBOL(drm_property_create_range);
3020
Dave Airlief453ba02008-11-07 14:05:41 -08003021int drm_property_add_enum(struct drm_property *property, int index,
3022 uint64_t value, const char *name)
3023{
3024 struct drm_property_enum *prop_enum;
3025
Rob Clark49e27542012-05-17 02:23:26 -06003026 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3027 return -EINVAL;
3028
3029 /*
3030 * Bitmask enum properties have the additional constraint of values
3031 * from 0 to 63
3032 */
3033 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003034 return -EINVAL;
3035
3036 if (!list_empty(&property->enum_blob_list)) {
3037 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3038 if (prop_enum->value == value) {
3039 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3040 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3041 return 0;
3042 }
3043 }
3044 }
3045
3046 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3047 if (!prop_enum)
3048 return -ENOMEM;
3049
3050 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3051 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3052 prop_enum->value = value;
3053
3054 property->values[index] = value;
3055 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3056 return 0;
3057}
3058EXPORT_SYMBOL(drm_property_add_enum);
3059
3060void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3061{
3062 struct drm_property_enum *prop_enum, *pt;
3063
3064 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3065 list_del(&prop_enum->head);
3066 kfree(prop_enum);
3067 }
3068
3069 if (property->num_values)
3070 kfree(property->values);
3071 drm_mode_object_put(dev, &property->base);
3072 list_del(&property->head);
3073 kfree(property);
3074}
3075EXPORT_SYMBOL(drm_property_destroy);
3076
Paulo Zanonic5431882012-05-15 18:09:02 -03003077void drm_object_attach_property(struct drm_mode_object *obj,
3078 struct drm_property *property,
3079 uint64_t init_val)
3080{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003081 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003082
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003083 if (count == DRM_OBJECT_MAX_PROPERTY) {
3084 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3085 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3086 "you see this message on the same object type.\n",
3087 obj->type);
3088 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003089 }
3090
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003091 obj->properties->ids[count] = property->base.id;
3092 obj->properties->values[count] = init_val;
3093 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003094}
3095EXPORT_SYMBOL(drm_object_attach_property);
3096
3097int drm_object_property_set_value(struct drm_mode_object *obj,
3098 struct drm_property *property, uint64_t val)
3099{
3100 int i;
3101
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003102 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003103 if (obj->properties->ids[i] == property->base.id) {
3104 obj->properties->values[i] = val;
3105 return 0;
3106 }
3107 }
3108
3109 return -EINVAL;
3110}
3111EXPORT_SYMBOL(drm_object_property_set_value);
3112
3113int drm_object_property_get_value(struct drm_mode_object *obj,
3114 struct drm_property *property, uint64_t *val)
3115{
3116 int i;
3117
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003118 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003119 if (obj->properties->ids[i] == property->base.id) {
3120 *val = obj->properties->values[i];
3121 return 0;
3122 }
3123 }
3124
3125 return -EINVAL;
3126}
3127EXPORT_SYMBOL(drm_object_property_get_value);
3128
Dave Airlief453ba02008-11-07 14:05:41 -08003129int drm_mode_getproperty_ioctl(struct drm_device *dev,
3130 void *data, struct drm_file *file_priv)
3131{
3132 struct drm_mode_object *obj;
3133 struct drm_mode_get_property *out_resp = data;
3134 struct drm_property *property;
3135 int enum_count = 0;
3136 int blob_count = 0;
3137 int value_count = 0;
3138 int ret = 0, i;
3139 int copied;
3140 struct drm_property_enum *prop_enum;
3141 struct drm_mode_property_enum __user *enum_ptr;
3142 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003143 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003144 uint64_t __user *values_ptr;
3145 uint32_t __user *blob_length_ptr;
3146
Dave Airliefb3b06c2011-02-08 13:55:21 +10003147 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3148 return -EINVAL;
3149
Daniel Vetter84849902012-12-02 00:28:11 +01003150 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003151 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3152 if (!obj) {
3153 ret = -EINVAL;
3154 goto done;
3155 }
3156 property = obj_to_property(obj);
3157
Rob Clark49e27542012-05-17 02:23:26 -06003158 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003159 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3160 enum_count++;
3161 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3162 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3163 blob_count++;
3164 }
3165
3166 value_count = property->num_values;
3167
3168 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3169 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3170 out_resp->flags = property->flags;
3171
3172 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003173 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003174 for (i = 0; i < value_count; i++) {
3175 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3176 ret = -EFAULT;
3177 goto done;
3178 }
3179 }
3180 }
3181 out_resp->count_values = value_count;
3182
Rob Clark49e27542012-05-17 02:23:26 -06003183 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003184 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3185 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003186 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003187 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3188
3189 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3190 ret = -EFAULT;
3191 goto done;
3192 }
3193
3194 if (copy_to_user(&enum_ptr[copied].name,
3195 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3196 ret = -EFAULT;
3197 goto done;
3198 }
3199 copied++;
3200 }
3201 }
3202 out_resp->count_enum_blobs = enum_count;
3203 }
3204
3205 if (property->flags & DRM_MODE_PROP_BLOB) {
3206 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3207 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003208 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3209 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003210
3211 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3212 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3213 ret = -EFAULT;
3214 goto done;
3215 }
3216
3217 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3218 ret = -EFAULT;
3219 goto done;
3220 }
3221
3222 copied++;
3223 }
3224 }
3225 out_resp->count_enum_blobs = blob_count;
3226 }
3227done:
Daniel Vetter84849902012-12-02 00:28:11 +01003228 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003229 return ret;
3230}
3231
3232static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3233 void *data)
3234{
3235 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003236 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003237
3238 if (!length || !data)
3239 return NULL;
3240
3241 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3242 if (!blob)
3243 return NULL;
3244
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003245 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3246 if (ret) {
3247 kfree(blob);
3248 return NULL;
3249 }
3250
Dave Airlief453ba02008-11-07 14:05:41 -08003251 blob->length = length;
3252
3253 memcpy(blob->data, data, length);
3254
Dave Airlief453ba02008-11-07 14:05:41 -08003255 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3256 return blob;
3257}
3258
3259static void drm_property_destroy_blob(struct drm_device *dev,
3260 struct drm_property_blob *blob)
3261{
3262 drm_mode_object_put(dev, &blob->base);
3263 list_del(&blob->head);
3264 kfree(blob);
3265}
3266
3267int drm_mode_getblob_ioctl(struct drm_device *dev,
3268 void *data, struct drm_file *file_priv)
3269{
3270 struct drm_mode_object *obj;
3271 struct drm_mode_get_blob *out_resp = data;
3272 struct drm_property_blob *blob;
3273 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003274 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003275
Dave Airliefb3b06c2011-02-08 13:55:21 +10003276 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3277 return -EINVAL;
3278
Daniel Vetter84849902012-12-02 00:28:11 +01003279 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003280 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3281 if (!obj) {
3282 ret = -EINVAL;
3283 goto done;
3284 }
3285 blob = obj_to_blob(obj);
3286
3287 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003288 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003289 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3290 ret = -EFAULT;
3291 goto done;
3292 }
3293 }
3294 out_resp->length = blob->length;
3295
3296done:
Daniel Vetter84849902012-12-02 00:28:11 +01003297 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003298 return ret;
3299}
3300
3301int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3302 struct edid *edid)
3303{
3304 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003305 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003306
3307 if (connector->edid_blob_ptr)
3308 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3309
3310 /* Delete edid, when there is none. */
3311 if (!edid) {
3312 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003313 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003314 return ret;
3315 }
3316
Adam Jackson7466f4c2010-03-29 21:43:23 +00003317 size = EDID_LENGTH * (1 + edid->extensions);
3318 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3319 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003320 if (!connector->edid_blob_ptr)
3321 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003322
Rob Clark58495562012-10-11 20:50:56 -05003323 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003324 dev->mode_config.edid_property,
3325 connector->edid_blob_ptr->base.id);
3326
3327 return ret;
3328}
3329EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3330
Paulo Zanoni26a34812012-05-15 18:08:59 -03003331static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003332 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003333{
3334 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3335 return false;
3336 if (property->flags & DRM_MODE_PROP_RANGE) {
3337 if (value < property->values[0] || value > property->values[1])
3338 return false;
3339 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003340 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3341 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003342 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003343 for (i = 0; i < property->num_values; i++)
3344 valid_mask |= (1ULL << property->values[i]);
3345 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003346 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3347 /* Only the driver knows */
3348 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003349 } else {
3350 int i;
3351 for (i = 0; i < property->num_values; i++)
3352 if (property->values[i] == value)
3353 return true;
3354 return false;
3355 }
3356}
3357
Dave Airlief453ba02008-11-07 14:05:41 -08003358int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3359 void *data, struct drm_file *file_priv)
3360{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003361 struct drm_mode_connector_set_property *conn_set_prop = data;
3362 struct drm_mode_obj_set_property obj_set_prop = {
3363 .value = conn_set_prop->value,
3364 .prop_id = conn_set_prop->prop_id,
3365 .obj_id = conn_set_prop->connector_id,
3366 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3367 };
Dave Airlief453ba02008-11-07 14:05:41 -08003368
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003369 /* It does all the locking and checking we need */
3370 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003371}
3372
Paulo Zanonic5431882012-05-15 18:09:02 -03003373static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3374 struct drm_property *property,
3375 uint64_t value)
3376{
3377 int ret = -EINVAL;
3378 struct drm_connector *connector = obj_to_connector(obj);
3379
3380 /* Do DPMS ourselves */
3381 if (property == connector->dev->mode_config.dpms_property) {
3382 if (connector->funcs->dpms)
3383 (*connector->funcs->dpms)(connector, (int)value);
3384 ret = 0;
3385 } else if (connector->funcs->set_property)
3386 ret = connector->funcs->set_property(connector, property, value);
3387
3388 /* store the property value if successful */
3389 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003390 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003391 return ret;
3392}
3393
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003394static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3395 struct drm_property *property,
3396 uint64_t value)
3397{
3398 int ret = -EINVAL;
3399 struct drm_crtc *crtc = obj_to_crtc(obj);
3400
3401 if (crtc->funcs->set_property)
3402 ret = crtc->funcs->set_property(crtc, property, value);
3403 if (!ret)
3404 drm_object_property_set_value(obj, property, value);
3405
3406 return ret;
3407}
3408
Rob Clark4d939142012-05-17 02:23:27 -06003409static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3410 struct drm_property *property,
3411 uint64_t value)
3412{
3413 int ret = -EINVAL;
3414 struct drm_plane *plane = obj_to_plane(obj);
3415
3416 if (plane->funcs->set_property)
3417 ret = plane->funcs->set_property(plane, property, value);
3418 if (!ret)
3419 drm_object_property_set_value(obj, property, value);
3420
3421 return ret;
3422}
3423
Paulo Zanonic5431882012-05-15 18:09:02 -03003424int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3425 struct drm_file *file_priv)
3426{
3427 struct drm_mode_obj_get_properties *arg = data;
3428 struct drm_mode_object *obj;
3429 int ret = 0;
3430 int i;
3431 int copied = 0;
3432 int props_count = 0;
3433 uint32_t __user *props_ptr;
3434 uint64_t __user *prop_values_ptr;
3435
3436 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3437 return -EINVAL;
3438
Daniel Vetter84849902012-12-02 00:28:11 +01003439 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003440
3441 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3442 if (!obj) {
3443 ret = -EINVAL;
3444 goto out;
3445 }
3446 if (!obj->properties) {
3447 ret = -EINVAL;
3448 goto out;
3449 }
3450
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003451 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003452
3453 /* This ioctl is called twice, once to determine how much space is
3454 * needed, and the 2nd time to fill it. */
3455 if ((arg->count_props >= props_count) && props_count) {
3456 copied = 0;
3457 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3458 prop_values_ptr = (uint64_t __user *)(unsigned long)
3459 (arg->prop_values_ptr);
3460 for (i = 0; i < props_count; i++) {
3461 if (put_user(obj->properties->ids[i],
3462 props_ptr + copied)) {
3463 ret = -EFAULT;
3464 goto out;
3465 }
3466 if (put_user(obj->properties->values[i],
3467 prop_values_ptr + copied)) {
3468 ret = -EFAULT;
3469 goto out;
3470 }
3471 copied++;
3472 }
3473 }
3474 arg->count_props = props_count;
3475out:
Daniel Vetter84849902012-12-02 00:28:11 +01003476 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003477 return ret;
3478}
3479
3480int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3481 struct drm_file *file_priv)
3482{
3483 struct drm_mode_obj_set_property *arg = data;
3484 struct drm_mode_object *arg_obj;
3485 struct drm_mode_object *prop_obj;
3486 struct drm_property *property;
3487 int ret = -EINVAL;
3488 int i;
3489
3490 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3491 return -EINVAL;
3492
Daniel Vetter84849902012-12-02 00:28:11 +01003493 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003494
3495 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3496 if (!arg_obj)
3497 goto out;
3498 if (!arg_obj->properties)
3499 goto out;
3500
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003501 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003502 if (arg_obj->properties->ids[i] == arg->prop_id)
3503 break;
3504
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003505 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003506 goto out;
3507
3508 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3509 DRM_MODE_OBJECT_PROPERTY);
3510 if (!prop_obj)
3511 goto out;
3512 property = obj_to_property(prop_obj);
3513
3514 if (!drm_property_change_is_valid(property, arg->value))
3515 goto out;
3516
3517 switch (arg_obj->type) {
3518 case DRM_MODE_OBJECT_CONNECTOR:
3519 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3520 arg->value);
3521 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003522 case DRM_MODE_OBJECT_CRTC:
3523 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3524 break;
Rob Clark4d939142012-05-17 02:23:27 -06003525 case DRM_MODE_OBJECT_PLANE:
3526 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3527 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003528 }
3529
3530out:
Daniel Vetter84849902012-12-02 00:28:11 +01003531 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003532 return ret;
3533}
3534
Dave Airlief453ba02008-11-07 14:05:41 -08003535int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3536 struct drm_encoder *encoder)
3537{
3538 int i;
3539
3540 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3541 if (connector->encoder_ids[i] == 0) {
3542 connector->encoder_ids[i] = encoder->base.id;
3543 return 0;
3544 }
3545 }
3546 return -ENOMEM;
3547}
3548EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3549
3550void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3551 struct drm_encoder *encoder)
3552{
3553 int i;
3554 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3555 if (connector->encoder_ids[i] == encoder->base.id) {
3556 connector->encoder_ids[i] = 0;
3557 if (connector->encoder == encoder)
3558 connector->encoder = NULL;
3559 break;
3560 }
3561 }
3562}
3563EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3564
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003565int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003566 int gamma_size)
3567{
3568 crtc->gamma_size = gamma_size;
3569
3570 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3571 if (!crtc->gamma_store) {
3572 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003573 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003574 }
3575
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003576 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003577}
3578EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3579
3580int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3581 void *data, struct drm_file *file_priv)
3582{
3583 struct drm_mode_crtc_lut *crtc_lut = data;
3584 struct drm_mode_object *obj;
3585 struct drm_crtc *crtc;
3586 void *r_base, *g_base, *b_base;
3587 int size;
3588 int ret = 0;
3589
Dave Airliefb3b06c2011-02-08 13:55:21 +10003590 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3591 return -EINVAL;
3592
Daniel Vetter84849902012-12-02 00:28:11 +01003593 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003594 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3595 if (!obj) {
3596 ret = -EINVAL;
3597 goto out;
3598 }
3599 crtc = obj_to_crtc(obj);
3600
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003601 if (crtc->funcs->gamma_set == NULL) {
3602 ret = -ENOSYS;
3603 goto out;
3604 }
3605
Dave Airlief453ba02008-11-07 14:05:41 -08003606 /* memcpy into gamma store */
3607 if (crtc_lut->gamma_size != crtc->gamma_size) {
3608 ret = -EINVAL;
3609 goto out;
3610 }
3611
3612 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3613 r_base = crtc->gamma_store;
3614 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3615 ret = -EFAULT;
3616 goto out;
3617 }
3618
3619 g_base = r_base + size;
3620 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3621 ret = -EFAULT;
3622 goto out;
3623 }
3624
3625 b_base = g_base + size;
3626 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3627 ret = -EFAULT;
3628 goto out;
3629 }
3630
James Simmons72034252010-08-03 01:33:19 +01003631 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003632
3633out:
Daniel Vetter84849902012-12-02 00:28:11 +01003634 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003635 return ret;
3636
3637}
3638
3639int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3640 void *data, struct drm_file *file_priv)
3641{
3642 struct drm_mode_crtc_lut *crtc_lut = data;
3643 struct drm_mode_object *obj;
3644 struct drm_crtc *crtc;
3645 void *r_base, *g_base, *b_base;
3646 int size;
3647 int ret = 0;
3648
Dave Airliefb3b06c2011-02-08 13:55:21 +10003649 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3650 return -EINVAL;
3651
Daniel Vetter84849902012-12-02 00:28:11 +01003652 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003653 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3654 if (!obj) {
3655 ret = -EINVAL;
3656 goto out;
3657 }
3658 crtc = obj_to_crtc(obj);
3659
3660 /* memcpy into gamma store */
3661 if (crtc_lut->gamma_size != crtc->gamma_size) {
3662 ret = -EINVAL;
3663 goto out;
3664 }
3665
3666 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3667 r_base = crtc->gamma_store;
3668 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3669 ret = -EFAULT;
3670 goto out;
3671 }
3672
3673 g_base = r_base + size;
3674 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3675 ret = -EFAULT;
3676 goto out;
3677 }
3678
3679 b_base = g_base + size;
3680 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3681 ret = -EFAULT;
3682 goto out;
3683 }
3684out:
Daniel Vetter84849902012-12-02 00:28:11 +01003685 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003686 return ret;
3687}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003688
3689int drm_mode_page_flip_ioctl(struct drm_device *dev,
3690 void *data, struct drm_file *file_priv)
3691{
3692 struct drm_mode_crtc_page_flip *page_flip = data;
3693 struct drm_mode_object *obj;
3694 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003695 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003696 struct drm_pending_vblank_event *e = NULL;
3697 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003698 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003699 int ret = -EINVAL;
3700
3701 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3702 page_flip->reserved != 0)
3703 return -EINVAL;
3704
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003705 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3706 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003707 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003708 crtc = obj_to_crtc(obj);
3709
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003710 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003711 if (crtc->fb == NULL) {
3712 /* The framebuffer is currently unbound, presumably
3713 * due to a hotplug event, that userspace has not
3714 * yet discovered.
3715 */
3716 ret = -EBUSY;
3717 goto out;
3718 }
3719
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003720 if (crtc->funcs->page_flip == NULL)
3721 goto out;
3722
Daniel Vetter786b99e2012-12-02 21:53:40 +01003723 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3724 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003725 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003726
Rob Clark7c80e122012-09-04 16:35:56 +00003727 hdisplay = crtc->mode.hdisplay;
3728 vdisplay = crtc->mode.vdisplay;
3729
3730 if (crtc->invert_dimensions)
3731 swap(hdisplay, vdisplay);
3732
3733 if (hdisplay > fb->width ||
3734 vdisplay > fb->height ||
3735 crtc->x > fb->width - hdisplay ||
3736 crtc->y > fb->height - vdisplay) {
3737 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3738 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3739 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003740 ret = -ENOSPC;
3741 goto out;
3742 }
3743
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003744 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3745 ret = -ENOMEM;
3746 spin_lock_irqsave(&dev->event_lock, flags);
3747 if (file_priv->event_space < sizeof e->event) {
3748 spin_unlock_irqrestore(&dev->event_lock, flags);
3749 goto out;
3750 }
3751 file_priv->event_space -= sizeof e->event;
3752 spin_unlock_irqrestore(&dev->event_lock, flags);
3753
3754 e = kzalloc(sizeof *e, GFP_KERNEL);
3755 if (e == NULL) {
3756 spin_lock_irqsave(&dev->event_lock, flags);
3757 file_priv->event_space += sizeof e->event;
3758 spin_unlock_irqrestore(&dev->event_lock, flags);
3759 goto out;
3760 }
3761
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003762 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003763 e->event.base.length = sizeof e->event;
3764 e->event.user_data = page_flip->user_data;
3765 e->base.event = &e->event.base;
3766 e->base.file_priv = file_priv;
3767 e->base.destroy =
3768 (void (*) (struct drm_pending_event *)) kfree;
3769 }
3770
Daniel Vetterb0d12322012-12-11 01:07:12 +01003771 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003772 ret = crtc->funcs->page_flip(crtc, fb, e);
3773 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003774 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3775 spin_lock_irqsave(&dev->event_lock, flags);
3776 file_priv->event_space += sizeof e->event;
3777 spin_unlock_irqrestore(&dev->event_lock, flags);
3778 kfree(e);
3779 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003780 /* Keep the old fb, don't unref it. */
3781 old_fb = NULL;
3782 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003783 /*
3784 * Warn if the driver hasn't properly updated the crtc->fb
3785 * field to reflect that the new framebuffer is now used.
3786 * Failing to do so will screw with the reference counting
3787 * on framebuffers.
3788 */
3789 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003790 /* Unref only the old framebuffer. */
3791 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003792 }
3793
3794out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003795 if (fb)
3796 drm_framebuffer_unreference(fb);
3797 if (old_fb)
3798 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003799 mutex_unlock(&crtc->mutex);
3800
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003801 return ret;
3802}
Chris Wilsoneb033552011-01-24 15:11:08 +00003803
3804void drm_mode_config_reset(struct drm_device *dev)
3805{
3806 struct drm_crtc *crtc;
3807 struct drm_encoder *encoder;
3808 struct drm_connector *connector;
3809
3810 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3811 if (crtc->funcs->reset)
3812 crtc->funcs->reset(crtc);
3813
3814 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3815 if (encoder->funcs->reset)
3816 encoder->funcs->reset(encoder);
3817
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003818 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3819 connector->status = connector_status_unknown;
3820
Chris Wilsoneb033552011-01-24 15:11:08 +00003821 if (connector->funcs->reset)
3822 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003823 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003824}
3825EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003826
3827int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3828 void *data, struct drm_file *file_priv)
3829{
3830 struct drm_mode_create_dumb *args = data;
3831
3832 if (!dev->driver->dumb_create)
3833 return -ENOSYS;
3834 return dev->driver->dumb_create(file_priv, dev, args);
3835}
3836
3837int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3838 void *data, struct drm_file *file_priv)
3839{
3840 struct drm_mode_map_dumb *args = data;
3841
3842 /* call driver ioctl to get mmap offset */
3843 if (!dev->driver->dumb_map_offset)
3844 return -ENOSYS;
3845
3846 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3847}
3848
3849int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3850 void *data, struct drm_file *file_priv)
3851{
3852 struct drm_mode_destroy_dumb *args = data;
3853
3854 if (!dev->driver->dumb_destroy)
3855 return -ENOSYS;
3856
3857 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3858}
Dave Airlie248dbc22011-11-29 20:02:54 +00003859
3860/*
3861 * Just need to support RGB formats here for compat with code that doesn't
3862 * use pixel formats directly yet.
3863 */
3864void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3865 int *bpp)
3866{
3867 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003868 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003869 case DRM_FORMAT_RGB332:
3870 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003871 *depth = 8;
3872 *bpp = 8;
3873 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003874 case DRM_FORMAT_XRGB1555:
3875 case DRM_FORMAT_XBGR1555:
3876 case DRM_FORMAT_RGBX5551:
3877 case DRM_FORMAT_BGRX5551:
3878 case DRM_FORMAT_ARGB1555:
3879 case DRM_FORMAT_ABGR1555:
3880 case DRM_FORMAT_RGBA5551:
3881 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003882 *depth = 15;
3883 *bpp = 16;
3884 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003885 case DRM_FORMAT_RGB565:
3886 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003887 *depth = 16;
3888 *bpp = 16;
3889 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003890 case DRM_FORMAT_RGB888:
3891 case DRM_FORMAT_BGR888:
3892 *depth = 24;
3893 *bpp = 24;
3894 break;
3895 case DRM_FORMAT_XRGB8888:
3896 case DRM_FORMAT_XBGR8888:
3897 case DRM_FORMAT_RGBX8888:
3898 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003899 *depth = 24;
3900 *bpp = 32;
3901 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003902 case DRM_FORMAT_XRGB2101010:
3903 case DRM_FORMAT_XBGR2101010:
3904 case DRM_FORMAT_RGBX1010102:
3905 case DRM_FORMAT_BGRX1010102:
3906 case DRM_FORMAT_ARGB2101010:
3907 case DRM_FORMAT_ABGR2101010:
3908 case DRM_FORMAT_RGBA1010102:
3909 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003910 *depth = 30;
3911 *bpp = 32;
3912 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003913 case DRM_FORMAT_ARGB8888:
3914 case DRM_FORMAT_ABGR8888:
3915 case DRM_FORMAT_RGBA8888:
3916 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003917 *depth = 32;
3918 *bpp = 32;
3919 break;
3920 default:
3921 DRM_DEBUG_KMS("unsupported pixel format\n");
3922 *depth = 0;
3923 *bpp = 0;
3924 break;
3925 }
3926}
3927EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003928
3929/**
3930 * drm_format_num_planes - get the number of planes for format
3931 * @format: pixel format (DRM_FORMAT_*)
3932 *
3933 * RETURNS:
3934 * The number of planes used by the specified pixel format.
3935 */
3936int drm_format_num_planes(uint32_t format)
3937{
3938 switch (format) {
3939 case DRM_FORMAT_YUV410:
3940 case DRM_FORMAT_YVU410:
3941 case DRM_FORMAT_YUV411:
3942 case DRM_FORMAT_YVU411:
3943 case DRM_FORMAT_YUV420:
3944 case DRM_FORMAT_YVU420:
3945 case DRM_FORMAT_YUV422:
3946 case DRM_FORMAT_YVU422:
3947 case DRM_FORMAT_YUV444:
3948 case DRM_FORMAT_YVU444:
3949 return 3;
3950 case DRM_FORMAT_NV12:
3951 case DRM_FORMAT_NV21:
3952 case DRM_FORMAT_NV16:
3953 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003954 case DRM_FORMAT_NV24:
3955 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003956 return 2;
3957 default:
3958 return 1;
3959 }
3960}
3961EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003962
3963/**
3964 * drm_format_plane_cpp - determine the bytes per pixel value
3965 * @format: pixel format (DRM_FORMAT_*)
3966 * @plane: plane index
3967 *
3968 * RETURNS:
3969 * The bytes per pixel value for the specified plane.
3970 */
3971int drm_format_plane_cpp(uint32_t format, int plane)
3972{
3973 unsigned int depth;
3974 int bpp;
3975
3976 if (plane >= drm_format_num_planes(format))
3977 return 0;
3978
3979 switch (format) {
3980 case DRM_FORMAT_YUYV:
3981 case DRM_FORMAT_YVYU:
3982 case DRM_FORMAT_UYVY:
3983 case DRM_FORMAT_VYUY:
3984 return 2;
3985 case DRM_FORMAT_NV12:
3986 case DRM_FORMAT_NV21:
3987 case DRM_FORMAT_NV16:
3988 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003989 case DRM_FORMAT_NV24:
3990 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003991 return plane ? 2 : 1;
3992 case DRM_FORMAT_YUV410:
3993 case DRM_FORMAT_YVU410:
3994 case DRM_FORMAT_YUV411:
3995 case DRM_FORMAT_YVU411:
3996 case DRM_FORMAT_YUV420:
3997 case DRM_FORMAT_YVU420:
3998 case DRM_FORMAT_YUV422:
3999 case DRM_FORMAT_YVU422:
4000 case DRM_FORMAT_YUV444:
4001 case DRM_FORMAT_YVU444:
4002 return 1;
4003 default:
4004 drm_fb_get_bpp_depth(format, &depth, &bpp);
4005 return bpp >> 3;
4006 }
4007}
4008EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03004009
4010/**
4011 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4012 * @format: pixel format (DRM_FORMAT_*)
4013 *
4014 * RETURNS:
4015 * The horizontal chroma subsampling factor for the
4016 * specified pixel format.
4017 */
4018int drm_format_horz_chroma_subsampling(uint32_t format)
4019{
4020 switch (format) {
4021 case DRM_FORMAT_YUV411:
4022 case DRM_FORMAT_YVU411:
4023 case DRM_FORMAT_YUV410:
4024 case DRM_FORMAT_YVU410:
4025 return 4;
4026 case DRM_FORMAT_YUYV:
4027 case DRM_FORMAT_YVYU:
4028 case DRM_FORMAT_UYVY:
4029 case DRM_FORMAT_VYUY:
4030 case DRM_FORMAT_NV12:
4031 case DRM_FORMAT_NV21:
4032 case DRM_FORMAT_NV16:
4033 case DRM_FORMAT_NV61:
4034 case DRM_FORMAT_YUV422:
4035 case DRM_FORMAT_YVU422:
4036 case DRM_FORMAT_YUV420:
4037 case DRM_FORMAT_YVU420:
4038 return 2;
4039 default:
4040 return 1;
4041 }
4042}
4043EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4044
4045/**
4046 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4047 * @format: pixel format (DRM_FORMAT_*)
4048 *
4049 * RETURNS:
4050 * The vertical chroma subsampling factor for the
4051 * specified pixel format.
4052 */
4053int drm_format_vert_chroma_subsampling(uint32_t format)
4054{
4055 switch (format) {
4056 case DRM_FORMAT_YUV410:
4057 case DRM_FORMAT_YVU410:
4058 return 4;
4059 case DRM_FORMAT_YUV420:
4060 case DRM_FORMAT_YVU420:
4061 case DRM_FORMAT_NV12:
4062 case DRM_FORMAT_NV21:
4063 return 2;
4064 default:
4065 return 1;
4066 }
4067}
4068EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);