blob: 3a8f7e6db2950fc175759e558c7663ae84d3702f [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
Dave Airlief453ba02008-11-07 14:05:41 -0800181struct drm_conn_prop_enum_list {
182 int type;
183 char *name;
184 int count;
185};
186
187/*
188 * Connector and encoder types.
189 */
190static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
191{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
192 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
193 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
194 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
195 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
196 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
197 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
198 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
199 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500200 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
201 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
202 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
203 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200204 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500205 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200206 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800207};
208
209static struct drm_prop_enum_list drm_encoder_enum_list[] =
210{ { DRM_MODE_ENCODER_NONE, "None" },
211 { DRM_MODE_ENCODER_DAC, "DAC" },
212 { DRM_MODE_ENCODER_TMDS, "TMDS" },
213 { DRM_MODE_ENCODER_LVDS, "LVDS" },
214 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200215 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800216};
217
218char *drm_get_encoder_name(struct drm_encoder *encoder)
219{
220 static char buf[32];
221
222 snprintf(buf, 32, "%s-%d",
223 drm_encoder_enum_list[encoder->encoder_type].name,
224 encoder->base.id);
225 return buf;
226}
Dave Airlie13a81952009-09-07 15:45:33 +1000227EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800228
229char *drm_get_connector_name(struct drm_connector *connector)
230{
231 static char buf[32];
232
233 snprintf(buf, 32, "%s-%d",
234 drm_connector_enum_list[connector->connector_type].name,
235 connector->connector_type_id);
236 return buf;
237}
238EXPORT_SYMBOL(drm_get_connector_name);
239
240char *drm_get_connector_status_name(enum drm_connector_status status)
241{
242 if (status == connector_status_connected)
243 return "connected";
244 else if (status == connector_status_disconnected)
245 return "disconnected";
246 else
247 return "unknown";
248}
249
250/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100251 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800252 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100253 * @obj: object pointer, used to generate unique ID
254 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800255 *
Dave Airlief453ba02008-11-07 14:05:41 -0800256 * Create a unique identifier based on @ptr in @dev's identifier space. Used
257 * for tracking modes, CRTCs and connectors.
258 *
259 * RETURNS:
260 * New unique (relative to other objects in @dev) integer identifier for the
261 * object.
262 */
263static int drm_mode_object_get(struct drm_device *dev,
264 struct drm_mode_object *obj, uint32_t obj_type)
265{
Dave Airlief453ba02008-11-07 14:05:41 -0800266 int ret;
267
Jesse Barnesad2563c2009-01-19 17:21:45 +1000268 mutex_lock(&dev->mode_config.idr_mutex);
Tejun Heo2e928812013-02-27 17:04:08 -0800269 ret = idr_alloc(&dev->mode_config.crtc_idr, obj, 1, 0, GFP_KERNEL);
270 if (ret >= 0) {
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100271 /*
272 * Set up the object linking under the protection of the idr
273 * lock so that other users can't see inconsistent state.
274 */
Tejun Heo2e928812013-02-27 17:04:08 -0800275 obj->id = ret;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100276 obj->type = obj_type;
277 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000278 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100279
Tejun Heo2e928812013-02-27 17:04:08 -0800280 return ret < 0 ? ret : 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800281}
282
283/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100284 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800285 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100286 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800287 *
Dave Airlief453ba02008-11-07 14:05:41 -0800288 * Free @id from @dev's unique identifier pool.
289 */
290static void drm_mode_object_put(struct drm_device *dev,
291 struct drm_mode_object *object)
292{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000293 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800294 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000295 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800296}
297
Daniel Vetter786b99e2012-12-02 21:53:40 +0100298/**
299 * drm_mode_object_find - look up a drm object with static lifetime
300 * @dev: drm device
301 * @id: id of the mode object
302 * @type: type of the mode object
303 *
304 * Note that framebuffers cannot be looked up with this functions - since those
305 * are reference counted, they need special treatment.
306 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200307struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
308 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800309{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000310 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800311
Daniel Vetter786b99e2012-12-02 21:53:40 +0100312 /* Framebuffers are reference counted and need their own lookup
313 * function.*/
314 WARN_ON(type == DRM_MODE_OBJECT_FB);
315
Jesse Barnesad2563c2009-01-19 17:21:45 +1000316 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800317 obj = idr_find(&dev->mode_config.crtc_idr, id);
318 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000319 obj = NULL;
320 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800321
322 return obj;
323}
324EXPORT_SYMBOL(drm_mode_object_find);
325
326/**
Dave Airlief453ba02008-11-07 14:05:41 -0800327 * drm_framebuffer_init - initialize a framebuffer
328 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100329 * @fb: framebuffer to be initialized
330 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800331 *
Dave Airlief453ba02008-11-07 14:05:41 -0800332 * Allocates an ID for the framebuffer's parent mode object, sets its mode
333 * functions & device file and adds it to the master fd list.
334 *
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100335 * IMPORTANT:
336 * This functions publishes the fb and makes it available for concurrent access
337 * by other users. Which means by this point the fb _must_ be fully set up -
338 * since all the fb attributes are invariant over its lifetime, no further
339 * locking but only correct reference counting is required.
340 *
Dave Airlief453ba02008-11-07 14:05:41 -0800341 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200342 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800343 */
344int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
345 const struct drm_framebuffer_funcs *funcs)
346{
347 int ret;
348
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100349 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000350 kref_init(&fb->refcount);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100351 INIT_LIST_HEAD(&fb->filp_head);
352 fb->dev = dev;
353 fb->funcs = funcs;
Rob Clarkf7eff602012-09-05 21:48:38 +0000354
Dave Airlief453ba02008-11-07 14:05:41 -0800355 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200356 if (ret)
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100357 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800358
Daniel Vetter2b677e82012-12-10 21:16:05 +0100359 /* Grab the idr reference. */
360 drm_framebuffer_reference(fb);
361
Dave Airlief453ba02008-11-07 14:05:41 -0800362 dev->mode_config.num_fb++;
363 list_add(&fb->head, &dev->mode_config.fb_list);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100364out:
365 mutex_unlock(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -0800366
367 return 0;
368}
369EXPORT_SYMBOL(drm_framebuffer_init);
370
Rob Clarkf7eff602012-09-05 21:48:38 +0000371static void drm_framebuffer_free(struct kref *kref)
372{
373 struct drm_framebuffer *fb =
374 container_of(kref, struct drm_framebuffer, refcount);
375 fb->funcs->destroy(fb);
376}
377
Daniel Vetter2b677e82012-12-10 21:16:05 +0100378static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
379 uint32_t id)
380{
381 struct drm_mode_object *obj = NULL;
382 struct drm_framebuffer *fb;
383
384 mutex_lock(&dev->mode_config.idr_mutex);
385 obj = idr_find(&dev->mode_config.crtc_idr, id);
386 if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
387 fb = NULL;
388 else
389 fb = obj_to_fb(obj);
390 mutex_unlock(&dev->mode_config.idr_mutex);
391
392 return fb;
393}
394
Rob Clarkf7eff602012-09-05 21:48:38 +0000395/**
Daniel Vetter786b99e2012-12-02 21:53:40 +0100396 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
397 * @dev: drm device
398 * @id: id of the fb object
399 *
400 * If successful, this grabs an additional reference to the framebuffer -
401 * callers need to make sure to eventually unreference the returned framebuffer
402 * again.
403 */
404struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
405 uint32_t id)
406{
Daniel Vetter786b99e2012-12-02 21:53:40 +0100407 struct drm_framebuffer *fb;
408
409 mutex_lock(&dev->mode_config.fb_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100410 fb = __drm_framebuffer_lookup(dev, id);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100411 if (fb)
archit taneja9131d3d2013-04-10 08:59:39 +0000412 drm_framebuffer_reference(fb);
Daniel Vetter786b99e2012-12-02 21:53:40 +0100413 mutex_unlock(&dev->mode_config.fb_lock);
414
415 return fb;
416}
417EXPORT_SYMBOL(drm_framebuffer_lookup);
418
419/**
Rob Clarkf7eff602012-09-05 21:48:38 +0000420 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100421 * @fb: framebuffer to unref
422 *
423 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000424 */
425void drm_framebuffer_unreference(struct drm_framebuffer *fb)
426{
Rob Clarkf7eff602012-09-05 21:48:38 +0000427 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000428 kref_put(&fb->refcount, drm_framebuffer_free);
429}
430EXPORT_SYMBOL(drm_framebuffer_unreference);
431
432/**
433 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100434 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000435 */
436void drm_framebuffer_reference(struct drm_framebuffer *fb)
437{
438 DRM_DEBUG("FB ID: %d\n", fb->base.id);
439 kref_get(&fb->refcount);
440}
441EXPORT_SYMBOL(drm_framebuffer_reference);
442
Daniel Vetter2b677e82012-12-10 21:16:05 +0100443static void drm_framebuffer_free_bug(struct kref *kref)
444{
445 BUG();
446}
447
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100448static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
449{
450 DRM_DEBUG("FB ID: %d\n", fb->base.id);
451 kref_put(&fb->refcount, drm_framebuffer_free_bug);
452}
453
Daniel Vetter2b677e82012-12-10 21:16:05 +0100454/* dev->mode_config.fb_lock must be held! */
455static void __drm_framebuffer_unregister(struct drm_device *dev,
456 struct drm_framebuffer *fb)
457{
458 mutex_lock(&dev->mode_config.idr_mutex);
459 idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
460 mutex_unlock(&dev->mode_config.idr_mutex);
461
462 fb->base.id = 0;
463
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100464 __drm_framebuffer_unreference(fb);
Daniel Vetter2b677e82012-12-10 21:16:05 +0100465}
466
Dave Airlief453ba02008-11-07 14:05:41 -0800467/**
Daniel Vetter36206362012-12-10 20:42:17 +0100468 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
469 * @fb: fb to unregister
470 *
471 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
472 * those used for fbdev. Note that the caller must hold a reference of it's own,
473 * i.e. the object may not be destroyed through this call (since it'll lead to a
474 * locking inversion).
475 */
476void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
477{
Daniel Vetter2b677e82012-12-10 21:16:05 +0100478 struct drm_device *dev = fb->dev;
479
480 mutex_lock(&dev->mode_config.fb_lock);
481 /* Mark fb as reaped and drop idr ref. */
482 __drm_framebuffer_unregister(dev, fb);
483 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter36206362012-12-10 20:42:17 +0100484}
485EXPORT_SYMBOL(drm_framebuffer_unregister_private);
486
487/**
Dave Airlief453ba02008-11-07 14:05:41 -0800488 * drm_framebuffer_cleanup - remove a framebuffer object
489 * @fb: framebuffer to remove
490 *
Daniel Vetter36206362012-12-10 20:42:17 +0100491 * Cleanup references to a user-created framebuffer. This function is intended
492 * to be used from the drivers ->destroy callback.
493 *
494 * Note that this function does not remove the fb from active usuage - if it is
495 * still used anywhere, hilarity can ensue since userspace could call getfb on
496 * the id and get back -EINVAL. Obviously no concern at driver unload time.
497 *
498 * Also, the framebuffer will not be removed from the lookup idr - for
499 * user-created framebuffers this will happen in in the rmfb ioctl. For
500 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
501 * drm_framebuffer_unregister_private.
Dave Airlief453ba02008-11-07 14:05:41 -0800502 */
503void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
504{
505 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100506
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100507 mutex_lock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000508 list_del(&fb->head);
509 dev->mode_config.num_fb--;
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100510 mutex_unlock(&dev->mode_config.fb_lock);
Rob Clarkf7eff602012-09-05 21:48:38 +0000511}
512EXPORT_SYMBOL(drm_framebuffer_cleanup);
513
514/**
515 * drm_framebuffer_remove - remove and unreference a framebuffer object
516 * @fb: framebuffer to remove
517 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000518 * Scans all the CRTCs and planes in @dev's mode_config. If they're
Daniel Vetter36206362012-12-10 20:42:17 +0100519 * using @fb, removes it, setting it to NULL. Then drops the reference to the
Daniel Vetterb62584e2012-12-11 16:51:35 +0100520 * passed-in framebuffer. Might take the modeset locks.
521 *
522 * Note that this function optimizes the cleanup away if the caller holds the
523 * last reference to the framebuffer. It is also guaranteed to not take the
524 * modeset locks in this case.
Rob Clarkf7eff602012-09-05 21:48:38 +0000525 */
526void drm_framebuffer_remove(struct drm_framebuffer *fb)
527{
528 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800529 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800530 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000531 struct drm_mode_set set;
532 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800533
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100534 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100535
Daniel Vetterb62584e2012-12-11 16:51:35 +0100536 /*
537 * drm ABI mandates that we remove any deleted framebuffers from active
538 * useage. But since most sane clients only remove framebuffers they no
539 * longer need, try to optimize this away.
540 *
541 * Since we're holding a reference ourselves, observing a refcount of 1
542 * means that we're the last holder and can skip it. Also, the refcount
543 * can never increase from 1 again, so we don't need any barriers or
544 * locks.
545 *
546 * Note that userspace could try to race with use and instate a new
547 * usage _after_ we've cleared all current ones. End result will be an
548 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
549 * in this manner.
550 */
551 if (atomic_read(&fb->refcount.refcount) > 1) {
552 drm_modeset_lock_all(dev);
553 /* remove from any CRTC */
554 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
555 if (crtc->fb == fb) {
556 /* should turn off the crtc */
557 memset(&set, 0, sizeof(struct drm_mode_set));
558 set.crtc = crtc;
559 set.fb = NULL;
560 ret = drm_mode_set_config_internal(&set);
561 if (ret)
562 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
563 }
Dave Airlie5ef5f722009-08-17 13:11:23 +1000564 }
Dave Airlief453ba02008-11-07 14:05:41 -0800565
Daniel Vetterb62584e2012-12-11 16:51:35 +0100566 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
567 if (plane->fb == fb) {
568 /* should turn off the crtc */
569 ret = plane->funcs->disable_plane(plane);
570 if (ret)
571 DRM_ERROR("failed to disable plane with busy fb\n");
572 /* disconnect the plane from the fb and crtc: */
573 __drm_framebuffer_unreference(plane->fb);
574 plane->fb = NULL;
575 plane->crtc = NULL;
576 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800577 }
Daniel Vetterb62584e2012-12-11 16:51:35 +0100578 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800579 }
580
Rob Clarkf7eff602012-09-05 21:48:38 +0000581 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800582}
Rob Clarkf7eff602012-09-05 21:48:38 +0000583EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800584
585/**
586 * drm_crtc_init - Initialise a new CRTC object
587 * @dev: DRM device
588 * @crtc: CRTC object to init
589 * @funcs: callbacks for the new CRTC
590 *
Dave Airlief453ba02008-11-07 14:05:41 -0800591 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200592 *
593 * RETURNS:
594 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800595 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200596int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800597 const struct drm_crtc_funcs *funcs)
598{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200599 int ret;
600
Dave Airlief453ba02008-11-07 14:05:41 -0800601 crtc->dev = dev;
602 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000603 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800604
Daniel Vetter84849902012-12-02 00:28:11 +0100605 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100606 mutex_init(&crtc->mutex);
607 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200608
609 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
610 if (ret)
611 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800612
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300613 crtc->base.properties = &crtc->properties;
614
Dave Airlief453ba02008-11-07 14:05:41 -0800615 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
616 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200617
618 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100619 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200620
621 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800622}
623EXPORT_SYMBOL(drm_crtc_init);
624
625/**
626 * drm_crtc_cleanup - Cleans up the core crtc usage.
627 * @crtc: CRTC to cleanup
628 *
Dave Airlief453ba02008-11-07 14:05:41 -0800629 * Cleanup @crtc. Removes from drm modesetting space
630 * does NOT free object, caller does that.
631 */
632void drm_crtc_cleanup(struct drm_crtc *crtc)
633{
634 struct drm_device *dev = crtc->dev;
635
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000636 kfree(crtc->gamma_store);
637 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800638
639 drm_mode_object_put(dev, &crtc->base);
640 list_del(&crtc->head);
641 dev->mode_config.num_crtc--;
642}
643EXPORT_SYMBOL(drm_crtc_cleanup);
644
645/**
646 * drm_mode_probed_add - add a mode to a connector's probed mode list
647 * @connector: connector the new mode
648 * @mode: mode data
649 *
Dave Airlief453ba02008-11-07 14:05:41 -0800650 * Add @mode to @connector's mode list for later use.
651 */
652void drm_mode_probed_add(struct drm_connector *connector,
653 struct drm_display_mode *mode)
654{
655 list_add(&mode->head, &connector->probed_modes);
656}
657EXPORT_SYMBOL(drm_mode_probed_add);
658
659/**
660 * drm_mode_remove - remove and free a mode
661 * @connector: connector list to modify
662 * @mode: mode to remove
663 *
Dave Airlief453ba02008-11-07 14:05:41 -0800664 * Remove @mode from @connector's mode list, then free it.
665 */
666void drm_mode_remove(struct drm_connector *connector,
667 struct drm_display_mode *mode)
668{
669 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100670 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800671}
672EXPORT_SYMBOL(drm_mode_remove);
673
674/**
675 * drm_connector_init - Init a preallocated connector
676 * @dev: DRM device
677 * @connector: the connector to init
678 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100679 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800680 *
Dave Airlief453ba02008-11-07 14:05:41 -0800681 * Initialises a preallocated connector. Connectors should be
682 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200683 *
684 * RETURNS:
685 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800686 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200687int drm_connector_init(struct drm_device *dev,
688 struct drm_connector *connector,
689 const struct drm_connector_funcs *funcs,
690 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800691{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200692 int ret;
693
Daniel Vetter84849902012-12-02 00:28:11 +0100694 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800695
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200696 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
697 if (ret)
698 goto out;
699
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300700 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800701 connector->dev = dev;
702 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800703 connector->connector_type = connector_type;
704 connector->connector_type_id =
705 ++drm_connector_enum_list[connector_type].count; /* TODO */
Dave Airlief453ba02008-11-07 14:05:41 -0800706 INIT_LIST_HEAD(&connector->probed_modes);
707 INIT_LIST_HEAD(&connector->modes);
708 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000709 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800710
711 list_add_tail(&connector->head, &dev->mode_config.connector_list);
712 dev->mode_config.num_connector++;
713
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200714 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500715 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200716 dev->mode_config.edid_property,
717 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800718
Rob Clark58495562012-10-11 20:50:56 -0500719 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800720 dev->mode_config.dpms_property, 0);
721
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200722 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100723 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200724
725 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800726}
727EXPORT_SYMBOL(drm_connector_init);
728
729/**
730 * drm_connector_cleanup - cleans up an initialised connector
731 * @connector: connector to cleanup
732 *
Dave Airlief453ba02008-11-07 14:05:41 -0800733 * Cleans up the connector but doesn't free the object.
734 */
735void drm_connector_cleanup(struct drm_connector *connector)
736{
737 struct drm_device *dev = connector->dev;
738 struct drm_display_mode *mode, *t;
739
740 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
741 drm_mode_remove(connector, mode);
742
743 list_for_each_entry_safe(mode, t, &connector->modes, head)
744 drm_mode_remove(connector, mode);
745
Dave Airlief453ba02008-11-07 14:05:41 -0800746 drm_mode_object_put(dev, &connector->base);
747 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000748 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800749}
750EXPORT_SYMBOL(drm_connector_cleanup);
751
Dave Airliecbc7e222012-02-20 14:16:40 +0000752void drm_connector_unplug_all(struct drm_device *dev)
753{
754 struct drm_connector *connector;
755
756 /* taking the mode config mutex ends up in a clash with sysfs */
757 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
758 drm_sysfs_connector_remove(connector);
759
760}
761EXPORT_SYMBOL(drm_connector_unplug_all);
762
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200763int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000764 struct drm_encoder *encoder,
765 const struct drm_encoder_funcs *funcs,
766 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800767{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200768 int ret;
769
Daniel Vetter84849902012-12-02 00:28:11 +0100770 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800771
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200772 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
773 if (ret)
774 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800775
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200776 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800777 encoder->encoder_type = encoder_type;
778 encoder->funcs = funcs;
779
780 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
781 dev->mode_config.num_encoder++;
782
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200783 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100784 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200785
786 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800787}
788EXPORT_SYMBOL(drm_encoder_init);
789
790void drm_encoder_cleanup(struct drm_encoder *encoder)
791{
792 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100793 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800794 drm_mode_object_put(dev, &encoder->base);
795 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000796 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100797 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800798}
799EXPORT_SYMBOL(drm_encoder_cleanup);
800
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800801int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
802 unsigned long possible_crtcs,
803 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600804 const uint32_t *formats, uint32_t format_count,
805 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800806{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200807 int ret;
808
Daniel Vetter84849902012-12-02 00:28:11 +0100809 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800810
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200811 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
812 if (ret)
813 goto out;
814
Rob Clark4d939142012-05-17 02:23:27 -0600815 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800816 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800817 plane->funcs = funcs;
818 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
819 GFP_KERNEL);
820 if (!plane->format_types) {
821 DRM_DEBUG_KMS("out of memory when allocating plane\n");
822 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200823 ret = -ENOMEM;
824 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800825 }
826
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800827 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800828 plane->format_count = format_count;
829 plane->possible_crtcs = possible_crtcs;
830
Rob Clark0a7eb242011-12-13 20:19:36 -0600831 /* private planes are not exposed to userspace, but depending on
832 * display hardware, might be convenient to allow sharing programming
833 * for the scanout engine with the crtc implementation.
834 */
835 if (!priv) {
836 list_add_tail(&plane->head, &dev->mode_config.plane_list);
837 dev->mode_config.num_plane++;
838 } else {
839 INIT_LIST_HEAD(&plane->head);
840 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800841
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200842 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100843 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800844
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200845 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800846}
847EXPORT_SYMBOL(drm_plane_init);
848
849void drm_plane_cleanup(struct drm_plane *plane)
850{
851 struct drm_device *dev = plane->dev;
852
Daniel Vetter84849902012-12-02 00:28:11 +0100853 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800854 kfree(plane->format_types);
855 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600856 /* if not added to a list, it must be a private plane */
857 if (!list_empty(&plane->head)) {
858 list_del(&plane->head);
859 dev->mode_config.num_plane--;
860 }
Daniel Vetter84849902012-12-02 00:28:11 +0100861 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800862}
863EXPORT_SYMBOL(drm_plane_cleanup);
864
Dave Airlief453ba02008-11-07 14:05:41 -0800865/**
866 * drm_mode_create - create a new display mode
867 * @dev: DRM device
868 *
Dave Airlief453ba02008-11-07 14:05:41 -0800869 * Create a new drm_display_mode, give it an ID, and return it.
870 *
871 * RETURNS:
872 * Pointer to new mode on success, NULL on error.
873 */
874struct drm_display_mode *drm_mode_create(struct drm_device *dev)
875{
876 struct drm_display_mode *nmode;
877
878 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
879 if (!nmode)
880 return NULL;
881
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200882 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
883 kfree(nmode);
884 return NULL;
885 }
886
Dave Airlief453ba02008-11-07 14:05:41 -0800887 return nmode;
888}
889EXPORT_SYMBOL(drm_mode_create);
890
891/**
892 * drm_mode_destroy - remove a mode
893 * @dev: DRM device
894 * @mode: mode to remove
895 *
Dave Airlief453ba02008-11-07 14:05:41 -0800896 * Free @mode's unique identifier, then free it.
897 */
898void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
899{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200900 if (!mode)
901 return;
902
Dave Airlief453ba02008-11-07 14:05:41 -0800903 drm_mode_object_put(dev, &mode->base);
904
905 kfree(mode);
906}
907EXPORT_SYMBOL(drm_mode_destroy);
908
909static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
910{
911 struct drm_property *edid;
912 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800913
914 /*
915 * Standard properties (apply to all connectors)
916 */
917 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
918 DRM_MODE_PROP_IMMUTABLE,
919 "EDID", 0);
920 dev->mode_config.edid_property = edid;
921
Sascha Hauer4a67d392012-02-06 10:58:17 +0100922 dpms = drm_property_create_enum(dev, 0,
923 "DPMS", drm_dpms_enum_list,
924 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800925 dev->mode_config.dpms_property = dpms;
926
927 return 0;
928}
929
930/**
931 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
932 * @dev: DRM device
933 *
934 * Called by a driver the first time a DVI-I connector is made.
935 */
936int drm_mode_create_dvi_i_properties(struct drm_device *dev)
937{
938 struct drm_property *dvi_i_selector;
939 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800940
941 if (dev->mode_config.dvi_i_select_subconnector_property)
942 return 0;
943
944 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100945 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800946 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100947 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800948 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800949 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
950
Sascha Hauer4a67d392012-02-06 10:58:17 +0100951 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800952 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100953 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800954 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800955 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
956
957 return 0;
958}
959EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
960
961/**
962 * drm_create_tv_properties - create TV specific connector properties
963 * @dev: DRM device
964 * @num_modes: number of different TV formats (modes) supported
965 * @modes: array of pointers to strings containing name of each format
966 *
967 * Called by a driver's TV initialization routine, this function creates
968 * the TV specific connector properties for a given device. Caller is
969 * responsible for allocating a list of format names and passing them to
970 * this routine.
971 */
972int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
973 char *modes[])
974{
975 struct drm_property *tv_selector;
976 struct drm_property *tv_subconnector;
977 int i;
978
979 if (dev->mode_config.tv_select_subconnector_property)
980 return 0;
981
982 /*
983 * Basic connector properties
984 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100985 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800986 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100987 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800988 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800989 dev->mode_config.tv_select_subconnector_property = tv_selector;
990
991 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100992 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
993 "subconnector",
994 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800995 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800996 dev->mode_config.tv_subconnector_property = tv_subconnector;
997
998 /*
999 * Other, TV specific properties: margins & TV modes.
1000 */
1001 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001002 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001003
1004 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001005 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001006
1007 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001008 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001009
1010 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001011 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001012
1013 dev->mode_config.tv_mode_property =
1014 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1015 "mode", num_modes);
1016 for (i = 0; i < num_modes; i++)
1017 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1018 i, modes[i]);
1019
Francisco Jerezb6b79022009-08-02 04:19:20 +02001020 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001021 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001022
1023 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001024 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001025
1026 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001027 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001028
Francisco Jereza75f0232009-08-12 02:30:10 +02001029 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001030 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001031
1032 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001033 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001034
1035 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001036 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001037
Dave Airlief453ba02008-11-07 14:05:41 -08001038 return 0;
1039}
1040EXPORT_SYMBOL(drm_mode_create_tv_properties);
1041
1042/**
1043 * drm_mode_create_scaling_mode_property - create scaling mode property
1044 * @dev: DRM device
1045 *
1046 * Called by a driver the first time it's needed, must be attached to desired
1047 * connectors.
1048 */
1049int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1050{
1051 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001052
1053 if (dev->mode_config.scaling_mode_property)
1054 return 0;
1055
1056 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001057 drm_property_create_enum(dev, 0, "scaling mode",
1058 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001059 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001060
1061 dev->mode_config.scaling_mode_property = scaling_mode;
1062
1063 return 0;
1064}
1065EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1066
1067/**
1068 * drm_mode_create_dithering_property - create dithering property
1069 * @dev: DRM device
1070 *
1071 * Called by a driver the first time it's needed, must be attached to desired
1072 * connectors.
1073 */
1074int drm_mode_create_dithering_property(struct drm_device *dev)
1075{
1076 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001077
1078 if (dev->mode_config.dithering_mode_property)
1079 return 0;
1080
1081 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001082 drm_property_create_enum(dev, 0, "dithering",
1083 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001084 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001085 dev->mode_config.dithering_mode_property = dithering_mode;
1086
1087 return 0;
1088}
1089EXPORT_SYMBOL(drm_mode_create_dithering_property);
1090
1091/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001092 * drm_mode_create_dirty_property - create dirty property
1093 * @dev: DRM device
1094 *
1095 * Called by a driver the first time it's needed, must be attached to desired
1096 * connectors.
1097 */
1098int drm_mode_create_dirty_info_property(struct drm_device *dev)
1099{
1100 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001101
1102 if (dev->mode_config.dirty_info_property)
1103 return 0;
1104
1105 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001106 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001107 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001108 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001109 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001110 dev->mode_config.dirty_info_property = dirty_info;
1111
1112 return 0;
1113}
1114EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1115
Ville Syrjäläea9cbb02013-04-25 20:09:20 +03001116static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
Dave Airlief453ba02008-11-07 14:05:41 -08001117{
1118 uint32_t total_objects = 0;
1119
1120 total_objects += dev->mode_config.num_crtc;
1121 total_objects += dev->mode_config.num_connector;
1122 total_objects += dev->mode_config.num_encoder;
1123
Dave Airlief453ba02008-11-07 14:05:41 -08001124 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1125 if (!group->id_list)
1126 return -ENOMEM;
1127
1128 group->num_crtcs = 0;
1129 group->num_connectors = 0;
1130 group->num_encoders = 0;
1131 return 0;
1132}
1133
1134int drm_mode_group_init_legacy_group(struct drm_device *dev,
1135 struct drm_mode_group *group)
1136{
1137 struct drm_crtc *crtc;
1138 struct drm_encoder *encoder;
1139 struct drm_connector *connector;
1140 int ret;
1141
1142 if ((ret = drm_mode_group_init(dev, group)))
1143 return ret;
1144
1145 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1146 group->id_list[group->num_crtcs++] = crtc->base.id;
1147
1148 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1149 group->id_list[group->num_crtcs + group->num_encoders++] =
1150 encoder->base.id;
1151
1152 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1153 group->id_list[group->num_crtcs + group->num_encoders +
1154 group->num_connectors++] = connector->base.id;
1155
1156 return 0;
1157}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001158EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001159
1160/**
Dave Airlief453ba02008-11-07 14:05:41 -08001161 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1162 * @out: drm_mode_modeinfo struct to return to the user
1163 * @in: drm_display_mode to use
1164 *
Dave Airlief453ba02008-11-07 14:05:41 -08001165 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1166 * the user.
1167 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001168static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1169 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001170{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001171 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1172 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1173 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1174 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1175 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1176 "timing values too large for mode info\n");
1177
Dave Airlief453ba02008-11-07 14:05:41 -08001178 out->clock = in->clock;
1179 out->hdisplay = in->hdisplay;
1180 out->hsync_start = in->hsync_start;
1181 out->hsync_end = in->hsync_end;
1182 out->htotal = in->htotal;
1183 out->hskew = in->hskew;
1184 out->vdisplay = in->vdisplay;
1185 out->vsync_start = in->vsync_start;
1186 out->vsync_end = in->vsync_end;
1187 out->vtotal = in->vtotal;
1188 out->vscan = in->vscan;
1189 out->vrefresh = in->vrefresh;
1190 out->flags = in->flags;
1191 out->type = in->type;
1192 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1193 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1194}
1195
1196/**
1197 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1198 * @out: drm_display_mode to return to the user
1199 * @in: drm_mode_modeinfo to use
1200 *
Dave Airlief453ba02008-11-07 14:05:41 -08001201 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1202 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001203 *
1204 * RETURNS:
1205 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001206 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001207static int drm_crtc_convert_umode(struct drm_display_mode *out,
1208 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001209{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001210 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1211 return -ERANGE;
1212
Dave Airlief453ba02008-11-07 14:05:41 -08001213 out->clock = in->clock;
1214 out->hdisplay = in->hdisplay;
1215 out->hsync_start = in->hsync_start;
1216 out->hsync_end = in->hsync_end;
1217 out->htotal = in->htotal;
1218 out->hskew = in->hskew;
1219 out->vdisplay = in->vdisplay;
1220 out->vsync_start = in->vsync_start;
1221 out->vsync_end = in->vsync_end;
1222 out->vtotal = in->vtotal;
1223 out->vscan = in->vscan;
1224 out->vrefresh = in->vrefresh;
1225 out->flags = in->flags;
1226 out->type = in->type;
1227 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1228 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001229
1230 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001231}
1232
1233/**
1234 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001235 * @dev: drm device for the ioctl
1236 * @data: data pointer for the ioctl
1237 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001238 *
Dave Airlief453ba02008-11-07 14:05:41 -08001239 * Construct a set of configuration description structures and return
1240 * them to the user, including CRTC, connector and framebuffer configuration.
1241 *
1242 * Called by the user via ioctl.
1243 *
1244 * RETURNS:
1245 * Zero on success, errno on failure.
1246 */
1247int drm_mode_getresources(struct drm_device *dev, void *data,
1248 struct drm_file *file_priv)
1249{
1250 struct drm_mode_card_res *card_res = data;
1251 struct list_head *lh;
1252 struct drm_framebuffer *fb;
1253 struct drm_connector *connector;
1254 struct drm_crtc *crtc;
1255 struct drm_encoder *encoder;
1256 int ret = 0;
1257 int connector_count = 0;
1258 int crtc_count = 0;
1259 int fb_count = 0;
1260 int encoder_count = 0;
1261 int copied = 0, i;
1262 uint32_t __user *fb_id;
1263 uint32_t __user *crtc_id;
1264 uint32_t __user *connector_id;
1265 uint32_t __user *encoder_id;
1266 struct drm_mode_group *mode_group;
1267
Dave Airliefb3b06c2011-02-08 13:55:21 +10001268 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1269 return -EINVAL;
1270
Dave Airlief453ba02008-11-07 14:05:41 -08001271
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001272 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001273 /*
1274 * For the non-control nodes we need to limit the list of resources
1275 * by IDs in the group list for this node
1276 */
1277 list_for_each(lh, &file_priv->fbs)
1278 fb_count++;
1279
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001280 /* handle this in 4 parts */
1281 /* FBs */
1282 if (card_res->count_fbs >= fb_count) {
1283 copied = 0;
1284 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1285 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1286 if (put_user(fb->base.id, fb_id + copied)) {
1287 mutex_unlock(&file_priv->fbs_lock);
1288 return -EFAULT;
1289 }
1290 copied++;
1291 }
1292 }
1293 card_res->count_fbs = fb_count;
1294 mutex_unlock(&file_priv->fbs_lock);
1295
1296 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001297 mode_group = &file_priv->master->minor->mode_group;
1298 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1299
1300 list_for_each(lh, &dev->mode_config.crtc_list)
1301 crtc_count++;
1302
1303 list_for_each(lh, &dev->mode_config.connector_list)
1304 connector_count++;
1305
1306 list_for_each(lh, &dev->mode_config.encoder_list)
1307 encoder_count++;
1308 } else {
1309
1310 crtc_count = mode_group->num_crtcs;
1311 connector_count = mode_group->num_connectors;
1312 encoder_count = mode_group->num_encoders;
1313 }
1314
1315 card_res->max_height = dev->mode_config.max_height;
1316 card_res->min_height = dev->mode_config.min_height;
1317 card_res->max_width = dev->mode_config.max_width;
1318 card_res->min_width = dev->mode_config.min_width;
1319
Dave Airlief453ba02008-11-07 14:05:41 -08001320 /* CRTCs */
1321 if (card_res->count_crtcs >= crtc_count) {
1322 copied = 0;
1323 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1324 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1325 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1326 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001327 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001328 if (put_user(crtc->base.id, crtc_id + copied)) {
1329 ret = -EFAULT;
1330 goto out;
1331 }
1332 copied++;
1333 }
1334 } else {
1335 for (i = 0; i < mode_group->num_crtcs; i++) {
1336 if (put_user(mode_group->id_list[i],
1337 crtc_id + copied)) {
1338 ret = -EFAULT;
1339 goto out;
1340 }
1341 copied++;
1342 }
1343 }
1344 }
1345 card_res->count_crtcs = crtc_count;
1346
1347 /* Encoders */
1348 if (card_res->count_encoders >= encoder_count) {
1349 copied = 0;
1350 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1351 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1352 list_for_each_entry(encoder,
1353 &dev->mode_config.encoder_list,
1354 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001355 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1356 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001357 if (put_user(encoder->base.id, encoder_id +
1358 copied)) {
1359 ret = -EFAULT;
1360 goto out;
1361 }
1362 copied++;
1363 }
1364 } else {
1365 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1366 if (put_user(mode_group->id_list[i],
1367 encoder_id + copied)) {
1368 ret = -EFAULT;
1369 goto out;
1370 }
1371 copied++;
1372 }
1373
1374 }
1375 }
1376 card_res->count_encoders = encoder_count;
1377
1378 /* Connectors */
1379 if (card_res->count_connectors >= connector_count) {
1380 copied = 0;
1381 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1382 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1383 list_for_each_entry(connector,
1384 &dev->mode_config.connector_list,
1385 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001386 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1387 connector->base.id,
1388 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001389 if (put_user(connector->base.id,
1390 connector_id + copied)) {
1391 ret = -EFAULT;
1392 goto out;
1393 }
1394 copied++;
1395 }
1396 } else {
1397 int start = mode_group->num_crtcs +
1398 mode_group->num_encoders;
1399 for (i = start; i < start + mode_group->num_connectors; i++) {
1400 if (put_user(mode_group->id_list[i],
1401 connector_id + copied)) {
1402 ret = -EFAULT;
1403 goto out;
1404 }
1405 copied++;
1406 }
1407 }
1408 }
1409 card_res->count_connectors = connector_count;
1410
Jerome Glisse94401062010-07-15 15:43:25 -04001411 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001412 card_res->count_connectors, card_res->count_encoders);
1413
1414out:
Daniel Vetter84849902012-12-02 00:28:11 +01001415 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001416 return ret;
1417}
1418
1419/**
1420 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001421 * @dev: drm device for the ioctl
1422 * @data: data pointer for the ioctl
1423 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001424 *
Dave Airlief453ba02008-11-07 14:05:41 -08001425 * Construct a CRTC configuration structure to return to the user.
1426 *
1427 * Called by the user via ioctl.
1428 *
1429 * RETURNS:
1430 * Zero on success, errno on failure.
1431 */
1432int drm_mode_getcrtc(struct drm_device *dev,
1433 void *data, struct drm_file *file_priv)
1434{
1435 struct drm_mode_crtc *crtc_resp = data;
1436 struct drm_crtc *crtc;
1437 struct drm_mode_object *obj;
1438 int ret = 0;
1439
Dave Airliefb3b06c2011-02-08 13:55:21 +10001440 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1441 return -EINVAL;
1442
Daniel Vetter84849902012-12-02 00:28:11 +01001443 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001444
1445 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1446 DRM_MODE_OBJECT_CRTC);
1447 if (!obj) {
1448 ret = -EINVAL;
1449 goto out;
1450 }
1451 crtc = obj_to_crtc(obj);
1452
1453 crtc_resp->x = crtc->x;
1454 crtc_resp->y = crtc->y;
1455 crtc_resp->gamma_size = crtc->gamma_size;
1456 if (crtc->fb)
1457 crtc_resp->fb_id = crtc->fb->base.id;
1458 else
1459 crtc_resp->fb_id = 0;
1460
1461 if (crtc->enabled) {
1462
1463 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1464 crtc_resp->mode_valid = 1;
1465
1466 } else {
1467 crtc_resp->mode_valid = 0;
1468 }
1469
1470out:
Daniel Vetter84849902012-12-02 00:28:11 +01001471 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001472 return ret;
1473}
1474
1475/**
1476 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001477 * @dev: drm device for the ioctl
1478 * @data: data pointer for the ioctl
1479 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001480 *
Dave Airlief453ba02008-11-07 14:05:41 -08001481 * Construct a connector configuration structure to return to the user.
1482 *
1483 * Called by the user via ioctl.
1484 *
1485 * RETURNS:
1486 * Zero on success, errno on failure.
1487 */
1488int drm_mode_getconnector(struct drm_device *dev, void *data,
1489 struct drm_file *file_priv)
1490{
1491 struct drm_mode_get_connector *out_resp = data;
1492 struct drm_mode_object *obj;
1493 struct drm_connector *connector;
1494 struct drm_display_mode *mode;
1495 int mode_count = 0;
1496 int props_count = 0;
1497 int encoders_count = 0;
1498 int ret = 0;
1499 int copied = 0;
1500 int i;
1501 struct drm_mode_modeinfo u_mode;
1502 struct drm_mode_modeinfo __user *mode_ptr;
1503 uint32_t __user *prop_ptr;
1504 uint64_t __user *prop_values;
1505 uint32_t __user *encoder_ptr;
1506
Dave Airliefb3b06c2011-02-08 13:55:21 +10001507 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1508 return -EINVAL;
1509
Dave Airlief453ba02008-11-07 14:05:41 -08001510 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1511
Jerome Glisse94401062010-07-15 15:43:25 -04001512 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001513
Daniel Vetter7b240562012-12-12 00:35:33 +01001514 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001515
1516 obj = drm_mode_object_find(dev, out_resp->connector_id,
1517 DRM_MODE_OBJECT_CONNECTOR);
1518 if (!obj) {
1519 ret = -EINVAL;
1520 goto out;
1521 }
1522 connector = obj_to_connector(obj);
1523
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001524 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001525
1526 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1527 if (connector->encoder_ids[i] != 0) {
1528 encoders_count++;
1529 }
1530 }
1531
1532 if (out_resp->count_modes == 0) {
1533 connector->funcs->fill_modes(connector,
1534 dev->mode_config.max_width,
1535 dev->mode_config.max_height);
1536 }
1537
1538 /* delayed so we get modes regardless of pre-fill_modes state */
1539 list_for_each_entry(mode, &connector->modes, head)
1540 mode_count++;
1541
1542 out_resp->connector_id = connector->base.id;
1543 out_resp->connector_type = connector->connector_type;
1544 out_resp->connector_type_id = connector->connector_type_id;
1545 out_resp->mm_width = connector->display_info.width_mm;
1546 out_resp->mm_height = connector->display_info.height_mm;
1547 out_resp->subpixel = connector->display_info.subpixel_order;
1548 out_resp->connection = connector->status;
1549 if (connector->encoder)
1550 out_resp->encoder_id = connector->encoder->base.id;
1551 else
1552 out_resp->encoder_id = 0;
1553
1554 /*
1555 * This ioctl is called twice, once to determine how much space is
1556 * needed, and the 2nd time to fill it.
1557 */
1558 if ((out_resp->count_modes >= mode_count) && mode_count) {
1559 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001560 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001561 list_for_each_entry(mode, &connector->modes, head) {
1562 drm_crtc_convert_to_umode(&u_mode, mode);
1563 if (copy_to_user(mode_ptr + copied,
1564 &u_mode, sizeof(u_mode))) {
1565 ret = -EFAULT;
1566 goto out;
1567 }
1568 copied++;
1569 }
1570 }
1571 out_resp->count_modes = mode_count;
1572
1573 if ((out_resp->count_props >= props_count) && props_count) {
1574 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001575 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1576 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001577 for (i = 0; i < connector->properties.count; i++) {
1578 if (put_user(connector->properties.ids[i],
1579 prop_ptr + copied)) {
1580 ret = -EFAULT;
1581 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001582 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001583
1584 if (put_user(connector->properties.values[i],
1585 prop_values + copied)) {
1586 ret = -EFAULT;
1587 goto out;
1588 }
1589 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001590 }
1591 }
1592 out_resp->count_props = props_count;
1593
1594 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1595 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001596 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001597 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1598 if (connector->encoder_ids[i] != 0) {
1599 if (put_user(connector->encoder_ids[i],
1600 encoder_ptr + copied)) {
1601 ret = -EFAULT;
1602 goto out;
1603 }
1604 copied++;
1605 }
1606 }
1607 }
1608 out_resp->count_encoders = encoders_count;
1609
1610out:
Daniel Vetter7b240562012-12-12 00:35:33 +01001611 mutex_unlock(&dev->mode_config.mutex);
1612
Dave Airlief453ba02008-11-07 14:05:41 -08001613 return ret;
1614}
1615
1616int drm_mode_getencoder(struct drm_device *dev, void *data,
1617 struct drm_file *file_priv)
1618{
1619 struct drm_mode_get_encoder *enc_resp = data;
1620 struct drm_mode_object *obj;
1621 struct drm_encoder *encoder;
1622 int ret = 0;
1623
Dave Airliefb3b06c2011-02-08 13:55:21 +10001624 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1625 return -EINVAL;
1626
Daniel Vetter84849902012-12-02 00:28:11 +01001627 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001628 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1629 DRM_MODE_OBJECT_ENCODER);
1630 if (!obj) {
1631 ret = -EINVAL;
1632 goto out;
1633 }
1634 encoder = obj_to_encoder(obj);
1635
1636 if (encoder->crtc)
1637 enc_resp->crtc_id = encoder->crtc->base.id;
1638 else
1639 enc_resp->crtc_id = 0;
1640 enc_resp->encoder_type = encoder->encoder_type;
1641 enc_resp->encoder_id = encoder->base.id;
1642 enc_resp->possible_crtcs = encoder->possible_crtcs;
1643 enc_resp->possible_clones = encoder->possible_clones;
1644
1645out:
Daniel Vetter84849902012-12-02 00:28:11 +01001646 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001647 return ret;
1648}
1649
1650/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001651 * drm_mode_getplane_res - get plane info
1652 * @dev: DRM device
1653 * @data: ioctl data
1654 * @file_priv: DRM file info
1655 *
1656 * Return an plane count and set of IDs.
1657 */
1658int drm_mode_getplane_res(struct drm_device *dev, void *data,
1659 struct drm_file *file_priv)
1660{
1661 struct drm_mode_get_plane_res *plane_resp = data;
1662 struct drm_mode_config *config;
1663 struct drm_plane *plane;
1664 uint32_t __user *plane_ptr;
1665 int copied = 0, ret = 0;
1666
1667 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1668 return -EINVAL;
1669
Daniel Vetter84849902012-12-02 00:28:11 +01001670 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001671 config = &dev->mode_config;
1672
1673 /*
1674 * This ioctl is called twice, once to determine how much space is
1675 * needed, and the 2nd time to fill it.
1676 */
1677 if (config->num_plane &&
1678 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001679 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001680
1681 list_for_each_entry(plane, &config->plane_list, head) {
1682 if (put_user(plane->base.id, plane_ptr + copied)) {
1683 ret = -EFAULT;
1684 goto out;
1685 }
1686 copied++;
1687 }
1688 }
1689 plane_resp->count_planes = config->num_plane;
1690
1691out:
Daniel Vetter84849902012-12-02 00:28:11 +01001692 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001693 return ret;
1694}
1695
1696/**
1697 * drm_mode_getplane - get plane info
1698 * @dev: DRM device
1699 * @data: ioctl data
1700 * @file_priv: DRM file info
1701 *
1702 * Return plane info, including formats supported, gamma size, any
1703 * current fb, etc.
1704 */
1705int drm_mode_getplane(struct drm_device *dev, void *data,
1706 struct drm_file *file_priv)
1707{
1708 struct drm_mode_get_plane *plane_resp = data;
1709 struct drm_mode_object *obj;
1710 struct drm_plane *plane;
1711 uint32_t __user *format_ptr;
1712 int ret = 0;
1713
1714 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1715 return -EINVAL;
1716
Daniel Vetter84849902012-12-02 00:28:11 +01001717 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001718 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1719 DRM_MODE_OBJECT_PLANE);
1720 if (!obj) {
1721 ret = -ENOENT;
1722 goto out;
1723 }
1724 plane = obj_to_plane(obj);
1725
1726 if (plane->crtc)
1727 plane_resp->crtc_id = plane->crtc->base.id;
1728 else
1729 plane_resp->crtc_id = 0;
1730
1731 if (plane->fb)
1732 plane_resp->fb_id = plane->fb->base.id;
1733 else
1734 plane_resp->fb_id = 0;
1735
1736 plane_resp->plane_id = plane->base.id;
1737 plane_resp->possible_crtcs = plane->possible_crtcs;
1738 plane_resp->gamma_size = plane->gamma_size;
1739
1740 /*
1741 * This ioctl is called twice, once to determine how much space is
1742 * needed, and the 2nd time to fill it.
1743 */
1744 if (plane->format_count &&
1745 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001746 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001747 if (copy_to_user(format_ptr,
1748 plane->format_types,
1749 sizeof(uint32_t) * plane->format_count)) {
1750 ret = -EFAULT;
1751 goto out;
1752 }
1753 }
1754 plane_resp->count_format_types = plane->format_count;
1755
1756out:
Daniel Vetter84849902012-12-02 00:28:11 +01001757 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001758 return ret;
1759}
1760
1761/**
1762 * drm_mode_setplane - set up or tear down an plane
1763 * @dev: DRM device
1764 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001765 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001766 *
1767 * Set plane info, including placement, fb, scaling, and other factors.
1768 * Or pass a NULL fb to disable.
1769 */
1770int drm_mode_setplane(struct drm_device *dev, void *data,
1771 struct drm_file *file_priv)
1772{
1773 struct drm_mode_set_plane *plane_req = data;
1774 struct drm_mode_object *obj;
1775 struct drm_plane *plane;
1776 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001777 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001778 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001779 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001780 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001781
1782 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1783 return -EINVAL;
1784
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001785 /*
1786 * First, find the plane, crtc, and fb objects. If not available,
1787 * we don't bother to call the driver.
1788 */
1789 obj = drm_mode_object_find(dev, plane_req->plane_id,
1790 DRM_MODE_OBJECT_PLANE);
1791 if (!obj) {
1792 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1793 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001794 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001795 }
1796 plane = obj_to_plane(obj);
1797
1798 /* No fb means shut it down */
1799 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001800 drm_modeset_lock_all(dev);
1801 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001802 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001803 plane->crtc = NULL;
1804 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001805 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001806 goto out;
1807 }
1808
1809 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1810 DRM_MODE_OBJECT_CRTC);
1811 if (!obj) {
1812 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1813 plane_req->crtc_id);
1814 ret = -ENOENT;
1815 goto out;
1816 }
1817 crtc = obj_to_crtc(obj);
1818
Daniel Vetter786b99e2012-12-02 21:53:40 +01001819 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1820 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001821 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1822 plane_req->fb_id);
1823 ret = -ENOENT;
1824 goto out;
1825 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001826
Ville Syrjälä62443be2011-12-20 00:06:47 +02001827 /* Check whether this plane supports the fb pixel format. */
1828 for (i = 0; i < plane->format_count; i++)
1829 if (fb->pixel_format == plane->format_types[i])
1830 break;
1831 if (i == plane->format_count) {
1832 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1833 ret = -EINVAL;
1834 goto out;
1835 }
1836
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001837 fb_width = fb->width << 16;
1838 fb_height = fb->height << 16;
1839
1840 /* Make sure source coordinates are inside the fb. */
1841 if (plane_req->src_w > fb_width ||
1842 plane_req->src_x > fb_width - plane_req->src_w ||
1843 plane_req->src_h > fb_height ||
1844 plane_req->src_y > fb_height - plane_req->src_h) {
1845 DRM_DEBUG_KMS("Invalid source coordinates "
1846 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1847 plane_req->src_w >> 16,
1848 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1849 plane_req->src_h >> 16,
1850 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1851 plane_req->src_x >> 16,
1852 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1853 plane_req->src_y >> 16,
1854 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1855 ret = -ENOSPC;
1856 goto out;
1857 }
1858
Ville Syrjälä687a0402011-12-20 00:06:45 +02001859 /* Give drivers some help against integer overflows */
1860 if (plane_req->crtc_w > INT_MAX ||
1861 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1862 plane_req->crtc_h > INT_MAX ||
1863 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1864 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1865 plane_req->crtc_w, plane_req->crtc_h,
1866 plane_req->crtc_x, plane_req->crtc_y);
1867 ret = -ERANGE;
1868 goto out;
1869 }
1870
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001871 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001872 ret = plane->funcs->update_plane(plane, crtc, fb,
1873 plane_req->crtc_x, plane_req->crtc_y,
1874 plane_req->crtc_w, plane_req->crtc_h,
1875 plane_req->src_x, plane_req->src_y,
1876 plane_req->src_w, plane_req->src_h);
1877 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001878 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001879 plane->crtc = crtc;
1880 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01001881 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001882 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001883 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001884
1885out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001886 if (fb)
1887 drm_framebuffer_unreference(fb);
1888 if (old_fb)
1889 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001890
1891 return ret;
1892}
1893
1894/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001895 * drm_mode_set_config_internal - helper to call ->set_config
1896 * @set: modeset config to set
1897 *
1898 * This is a little helper to wrap internal calls to the ->set_config driver
1899 * interface. The only thing it adds is correct refcounting dance.
1900 */
1901int drm_mode_set_config_internal(struct drm_mode_set *set)
1902{
1903 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001904 struct drm_framebuffer *fb, *old_fb;
1905 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001906
Daniel Vetterb0d12322012-12-11 01:07:12 +01001907 old_fb = crtc->fb;
1908 fb = set->fb;
1909
1910 ret = crtc->funcs->set_config(set);
1911 if (ret == 0) {
1912 if (old_fb)
1913 drm_framebuffer_unreference(old_fb);
1914 if (fb)
1915 drm_framebuffer_reference(fb);
1916 }
1917
1918 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001919}
1920EXPORT_SYMBOL(drm_mode_set_config_internal);
1921
1922/**
Dave Airlief453ba02008-11-07 14:05:41 -08001923 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001924 * @dev: drm device for the ioctl
1925 * @data: data pointer for the ioctl
1926 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001927 *
Dave Airlief453ba02008-11-07 14:05:41 -08001928 * Build a new CRTC configuration based on user request.
1929 *
1930 * Called by the user via ioctl.
1931 *
1932 * RETURNS:
1933 * Zero on success, errno on failure.
1934 */
1935int drm_mode_setcrtc(struct drm_device *dev, void *data,
1936 struct drm_file *file_priv)
1937{
1938 struct drm_mode_config *config = &dev->mode_config;
1939 struct drm_mode_crtc *crtc_req = data;
1940 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001941 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001942 struct drm_connector **connector_set = NULL, *connector;
1943 struct drm_framebuffer *fb = NULL;
1944 struct drm_display_mode *mode = NULL;
1945 struct drm_mode_set set;
1946 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001947 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001948 int i;
1949
Dave Airliefb3b06c2011-02-08 13:55:21 +10001950 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1951 return -EINVAL;
1952
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001953 /* For some reason crtc x/y offsets are signed internally. */
1954 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1955 return -ERANGE;
1956
Daniel Vetter84849902012-12-02 00:28:11 +01001957 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001958 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1959 DRM_MODE_OBJECT_CRTC);
1960 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001961 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001962 ret = -EINVAL;
1963 goto out;
1964 }
1965 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001966 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001967
1968 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001969 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001970 /* If we have a mode we need a framebuffer. */
1971 /* If we pass -1, set the mode with the currently bound fb */
1972 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001973 if (!crtc->fb) {
1974 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1975 ret = -EINVAL;
1976 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001977 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001978 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001979 /* Make refcounting symmetric with the lookup path. */
1980 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001981 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01001982 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1983 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001984 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1985 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001986 ret = -EINVAL;
1987 goto out;
1988 }
Dave Airlief453ba02008-11-07 14:05:41 -08001989 }
1990
1991 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001992 if (!mode) {
1993 ret = -ENOMEM;
1994 goto out;
1995 }
1996
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001997 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1998 if (ret) {
1999 DRM_DEBUG_KMS("Invalid mode\n");
2000 goto out;
2001 }
2002
Dave Airlief453ba02008-11-07 14:05:41 -08002003 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002004
Rob Clark7c80e122012-09-04 16:35:56 +00002005 hdisplay = mode->hdisplay;
2006 vdisplay = mode->vdisplay;
2007
2008 if (crtc->invert_dimensions)
2009 swap(hdisplay, vdisplay);
2010
2011 if (hdisplay > fb->width ||
2012 vdisplay > fb->height ||
2013 crtc_req->x > fb->width - hdisplay ||
2014 crtc_req->y > fb->height - vdisplay) {
2015 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2016 fb->width, fb->height,
2017 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2018 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002019 ret = -ENOSPC;
2020 goto out;
2021 }
Dave Airlief453ba02008-11-07 14:05:41 -08002022 }
2023
2024 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002025 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002026 ret = -EINVAL;
2027 goto out;
2028 }
2029
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002030 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002031 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002032 crtc_req->count_connectors);
2033 ret = -EINVAL;
2034 goto out;
2035 }
2036
2037 if (crtc_req->count_connectors > 0) {
2038 u32 out_id;
2039
2040 /* Avoid unbounded kernel memory allocation */
2041 if (crtc_req->count_connectors > config->num_connector) {
2042 ret = -EINVAL;
2043 goto out;
2044 }
2045
2046 connector_set = kmalloc(crtc_req->count_connectors *
2047 sizeof(struct drm_connector *),
2048 GFP_KERNEL);
2049 if (!connector_set) {
2050 ret = -ENOMEM;
2051 goto out;
2052 }
2053
2054 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002055 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002056 if (get_user(out_id, &set_connectors_ptr[i])) {
2057 ret = -EFAULT;
2058 goto out;
2059 }
2060
2061 obj = drm_mode_object_find(dev, out_id,
2062 DRM_MODE_OBJECT_CONNECTOR);
2063 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002064 DRM_DEBUG_KMS("Connector id %d unknown\n",
2065 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002066 ret = -EINVAL;
2067 goto out;
2068 }
2069 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002070 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2071 connector->base.id,
2072 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002073
2074 connector_set[i] = connector;
2075 }
2076 }
2077
2078 set.crtc = crtc;
2079 set.x = crtc_req->x;
2080 set.y = crtc_req->y;
2081 set.mode = mode;
2082 set.connectors = connector_set;
2083 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002084 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002085 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002086
2087out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002088 if (fb)
2089 drm_framebuffer_unreference(fb);
2090
Dave Airlief453ba02008-11-07 14:05:41 -08002091 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002092 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002093 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002094 return ret;
2095}
2096
2097int drm_mode_cursor_ioctl(struct drm_device *dev,
2098 void *data, struct drm_file *file_priv)
2099{
2100 struct drm_mode_cursor *req = data;
2101 struct drm_mode_object *obj;
2102 struct drm_crtc *crtc;
2103 int ret = 0;
2104
Dave Airliefb3b06c2011-02-08 13:55:21 +10002105 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2106 return -EINVAL;
2107
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002108 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002109 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002110
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002111 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002112 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002113 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002114 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002115 }
2116 crtc = obj_to_crtc(obj);
2117
Daniel Vetterdac35662012-12-02 15:24:10 +01002118 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002119 if (req->flags & DRM_MODE_CURSOR_BO) {
2120 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002121 ret = -ENXIO;
2122 goto out;
2123 }
2124 /* Turns off the cursor if handle is 0 */
2125 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2126 req->width, req->height);
2127 }
2128
2129 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2130 if (crtc->funcs->cursor_move) {
2131 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2132 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002133 ret = -EFAULT;
2134 goto out;
2135 }
2136 }
2137out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002138 mutex_unlock(&crtc->mutex);
2139
Dave Airlief453ba02008-11-07 14:05:41 -08002140 return ret;
2141}
2142
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002143/* Original addfb only supported RGB formats, so figure out which one */
2144uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2145{
2146 uint32_t fmt;
2147
2148 switch (bpp) {
2149 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002150 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002151 break;
2152 case 16:
2153 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002154 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002155 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002156 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002157 break;
2158 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002159 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002160 break;
2161 case 32:
2162 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002163 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002164 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002165 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002166 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002167 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002168 break;
2169 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002170 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2171 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002172 break;
2173 }
2174
2175 return fmt;
2176}
2177EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2178
Dave Airlief453ba02008-11-07 14:05:41 -08002179/**
2180 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002181 * @dev: drm device for the ioctl
2182 * @data: data pointer for the ioctl
2183 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002184 *
Dave Airlief453ba02008-11-07 14:05:41 -08002185 * Add a new FB to the specified CRTC, given a user request.
2186 *
2187 * Called by the user via ioctl.
2188 *
2189 * RETURNS:
2190 * Zero on success, errno on failure.
2191 */
2192int drm_mode_addfb(struct drm_device *dev,
2193 void *data, struct drm_file *file_priv)
2194{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002195 struct drm_mode_fb_cmd *or = data;
2196 struct drm_mode_fb_cmd2 r = {};
2197 struct drm_mode_config *config = &dev->mode_config;
2198 struct drm_framebuffer *fb;
2199 int ret = 0;
2200
2201 /* Use new struct with format internally */
2202 r.fb_id = or->fb_id;
2203 r.width = or->width;
2204 r.height = or->height;
2205 r.pitches[0] = or->pitch;
2206 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2207 r.handles[0] = or->handle;
2208
2209 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2210 return -EINVAL;
2211
Jesse Barnesacb4b992011-11-07 12:03:23 -08002212 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002213 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002214
2215 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002216 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002217
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002218 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2219 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002220 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002221 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002222 }
2223
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002224 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002225 or->fb_id = fb->base.id;
2226 list_add(&fb->filp_head, &file_priv->fbs);
2227 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002228 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002229
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002230 return ret;
2231}
2232
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002233static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002234{
2235 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2236
2237 switch (format) {
2238 case DRM_FORMAT_C8:
2239 case DRM_FORMAT_RGB332:
2240 case DRM_FORMAT_BGR233:
2241 case DRM_FORMAT_XRGB4444:
2242 case DRM_FORMAT_XBGR4444:
2243 case DRM_FORMAT_RGBX4444:
2244 case DRM_FORMAT_BGRX4444:
2245 case DRM_FORMAT_ARGB4444:
2246 case DRM_FORMAT_ABGR4444:
2247 case DRM_FORMAT_RGBA4444:
2248 case DRM_FORMAT_BGRA4444:
2249 case DRM_FORMAT_XRGB1555:
2250 case DRM_FORMAT_XBGR1555:
2251 case DRM_FORMAT_RGBX5551:
2252 case DRM_FORMAT_BGRX5551:
2253 case DRM_FORMAT_ARGB1555:
2254 case DRM_FORMAT_ABGR1555:
2255 case DRM_FORMAT_RGBA5551:
2256 case DRM_FORMAT_BGRA5551:
2257 case DRM_FORMAT_RGB565:
2258 case DRM_FORMAT_BGR565:
2259 case DRM_FORMAT_RGB888:
2260 case DRM_FORMAT_BGR888:
2261 case DRM_FORMAT_XRGB8888:
2262 case DRM_FORMAT_XBGR8888:
2263 case DRM_FORMAT_RGBX8888:
2264 case DRM_FORMAT_BGRX8888:
2265 case DRM_FORMAT_ARGB8888:
2266 case DRM_FORMAT_ABGR8888:
2267 case DRM_FORMAT_RGBA8888:
2268 case DRM_FORMAT_BGRA8888:
2269 case DRM_FORMAT_XRGB2101010:
2270 case DRM_FORMAT_XBGR2101010:
2271 case DRM_FORMAT_RGBX1010102:
2272 case DRM_FORMAT_BGRX1010102:
2273 case DRM_FORMAT_ARGB2101010:
2274 case DRM_FORMAT_ABGR2101010:
2275 case DRM_FORMAT_RGBA1010102:
2276 case DRM_FORMAT_BGRA1010102:
2277 case DRM_FORMAT_YUYV:
2278 case DRM_FORMAT_YVYU:
2279 case DRM_FORMAT_UYVY:
2280 case DRM_FORMAT_VYUY:
2281 case DRM_FORMAT_AYUV:
2282 case DRM_FORMAT_NV12:
2283 case DRM_FORMAT_NV21:
2284 case DRM_FORMAT_NV16:
2285 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002286 case DRM_FORMAT_NV24:
2287 case DRM_FORMAT_NV42:
Ville Syrjälä935b59772011-12-20 00:06:48 +02002288 case DRM_FORMAT_YUV410:
2289 case DRM_FORMAT_YVU410:
2290 case DRM_FORMAT_YUV411:
2291 case DRM_FORMAT_YVU411:
2292 case DRM_FORMAT_YUV420:
2293 case DRM_FORMAT_YVU420:
2294 case DRM_FORMAT_YUV422:
2295 case DRM_FORMAT_YVU422:
2296 case DRM_FORMAT_YUV444:
2297 case DRM_FORMAT_YVU444:
2298 return 0;
2299 default:
2300 return -EINVAL;
2301 }
2302}
2303
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002304static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002305{
2306 int ret, hsub, vsub, num_planes, i;
2307
2308 ret = format_check(r);
2309 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002310 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002311 return ret;
2312 }
2313
2314 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2315 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2316 num_planes = drm_format_num_planes(r->pixel_format);
2317
2318 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002319 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002320 return -EINVAL;
2321 }
2322
2323 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002324 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002325 return -EINVAL;
2326 }
2327
2328 for (i = 0; i < num_planes; i++) {
2329 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002330 unsigned int height = r->height / (i != 0 ? vsub : 1);
2331 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002332
2333 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002334 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002335 return -EINVAL;
2336 }
2337
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002338 if ((uint64_t) width * cpp > UINT_MAX)
2339 return -ERANGE;
2340
2341 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2342 return -ERANGE;
2343
2344 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002345 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002346 return -EINVAL;
2347 }
2348 }
2349
2350 return 0;
2351}
2352
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002353/**
2354 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002355 * @dev: drm device for the ioctl
2356 * @data: data pointer for the ioctl
2357 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002358 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002359 * Add a new FB to the specified CRTC, given a user request with format.
2360 *
2361 * Called by the user via ioctl.
2362 *
2363 * RETURNS:
2364 * Zero on success, errno on failure.
2365 */
2366int drm_mode_addfb2(struct drm_device *dev,
2367 void *data, struct drm_file *file_priv)
2368{
2369 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002370 struct drm_mode_config *config = &dev->mode_config;
2371 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002372 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002373
Dave Airliefb3b06c2011-02-08 13:55:21 +10002374 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2375 return -EINVAL;
2376
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002377 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2378 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2379 return -EINVAL;
2380 }
2381
Dave Airlief453ba02008-11-07 14:05:41 -08002382 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002383 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002384 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002385 return -EINVAL;
2386 }
2387 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002388 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002389 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002390 return -EINVAL;
2391 }
2392
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002393 ret = framebuffer_check(r);
2394 if (ret)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002395 return ret;
Ville Syrjälä935b59772011-12-20 00:06:48 +02002396
Dave Airlief453ba02008-11-07 14:05:41 -08002397 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002398 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002399 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002400 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002401 }
2402
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002403 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002404 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002405 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002406 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002407 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002408
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002409
Dave Airlief453ba02008-11-07 14:05:41 -08002410 return ret;
2411}
2412
2413/**
2414 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002415 * @dev: drm device for the ioctl
2416 * @data: data pointer for the ioctl
2417 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002418 *
Dave Airlief453ba02008-11-07 14:05:41 -08002419 * Remove the FB specified by the user.
2420 *
2421 * Called by the user via ioctl.
2422 *
2423 * RETURNS:
2424 * Zero on success, errno on failure.
2425 */
2426int drm_mode_rmfb(struct drm_device *dev,
2427 void *data, struct drm_file *file_priv)
2428{
Dave Airlief453ba02008-11-07 14:05:41 -08002429 struct drm_framebuffer *fb = NULL;
2430 struct drm_framebuffer *fbl = NULL;
2431 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002432 int found = 0;
2433
Dave Airliefb3b06c2011-02-08 13:55:21 +10002434 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2435 return -EINVAL;
2436
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002437 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002438 mutex_lock(&dev->mode_config.fb_lock);
2439 fb = __drm_framebuffer_lookup(dev, *id);
2440 if (!fb)
2441 goto fail_lookup;
2442
Dave Airlief453ba02008-11-07 14:05:41 -08002443 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2444 if (fb == fbl)
2445 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002446 if (!found)
2447 goto fail_lookup;
2448
2449 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2450 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002451
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002452 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002453 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002454 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002455
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002456 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002457
Daniel Vetter2b677e82012-12-10 21:16:05 +01002458 return 0;
2459
2460fail_lookup:
2461 mutex_unlock(&dev->mode_config.fb_lock);
2462 mutex_unlock(&file_priv->fbs_lock);
2463
2464 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002465}
2466
2467/**
2468 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002469 * @dev: drm device for the ioctl
2470 * @data: data pointer for the ioctl
2471 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002472 *
Dave Airlief453ba02008-11-07 14:05:41 -08002473 * Lookup the FB given its ID and return info about it.
2474 *
2475 * Called by the user via ioctl.
2476 *
2477 * RETURNS:
2478 * Zero on success, errno on failure.
2479 */
2480int drm_mode_getfb(struct drm_device *dev,
2481 void *data, struct drm_file *file_priv)
2482{
2483 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002484 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002485 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002486
Dave Airliefb3b06c2011-02-08 13:55:21 +10002487 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2488 return -EINVAL;
2489
Daniel Vetter786b99e2012-12-02 21:53:40 +01002490 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002491 if (!fb)
2492 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002493
2494 r->height = fb->height;
2495 r->width = fb->width;
2496 r->depth = fb->depth;
2497 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002498 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002499 if (fb->funcs->create_handle)
2500 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2501 else
2502 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002503
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002504 drm_framebuffer_unreference(fb);
2505
Dave Airlief453ba02008-11-07 14:05:41 -08002506 return ret;
2507}
2508
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002509int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2510 void *data, struct drm_file *file_priv)
2511{
2512 struct drm_clip_rect __user *clips_ptr;
2513 struct drm_clip_rect *clips = NULL;
2514 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002515 struct drm_framebuffer *fb;
2516 unsigned flags;
2517 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002518 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002519
Dave Airliefb3b06c2011-02-08 13:55:21 +10002520 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2521 return -EINVAL;
2522
Daniel Vetter786b99e2012-12-02 21:53:40 +01002523 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002524 if (!fb)
2525 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002526
2527 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002528 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002529
2530 if (!num_clips != !clips_ptr) {
2531 ret = -EINVAL;
2532 goto out_err1;
2533 }
2534
2535 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2536
2537 /* If userspace annotates copy, clips must come in pairs */
2538 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2539 ret = -EINVAL;
2540 goto out_err1;
2541 }
2542
2543 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002544 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2545 ret = -EINVAL;
2546 goto out_err1;
2547 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002548 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2549 if (!clips) {
2550 ret = -ENOMEM;
2551 goto out_err1;
2552 }
2553
2554 ret = copy_from_user(clips, clips_ptr,
2555 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002556 if (ret) {
2557 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002558 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002559 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002560 }
2561
2562 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002563 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002564 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2565 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002566 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002567 } else {
2568 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002569 }
2570
2571out_err2:
2572 kfree(clips);
2573out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002574 drm_framebuffer_unreference(fb);
2575
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002576 return ret;
2577}
2578
2579
Dave Airlief453ba02008-11-07 14:05:41 -08002580/**
2581 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002582 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002583 *
Dave Airlief453ba02008-11-07 14:05:41 -08002584 * Destroy all the FBs associated with @filp.
2585 *
2586 * Called by the user via ioctl.
2587 *
2588 * RETURNS:
2589 * Zero on success, errno on failure.
2590 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002591void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002592{
Dave Airlief453ba02008-11-07 14:05:41 -08002593 struct drm_device *dev = priv->minor->dev;
2594 struct drm_framebuffer *fb, *tfb;
2595
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002596 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002597 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002598
2599 mutex_lock(&dev->mode_config.fb_lock);
2600 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2601 __drm_framebuffer_unregister(dev, fb);
2602 mutex_unlock(&dev->mode_config.fb_lock);
2603
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002604 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002605
2606 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002607 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002608 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002609 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002610}
2611
Dave Airlief453ba02008-11-07 14:05:41 -08002612struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2613 const char *name, int num_values)
2614{
2615 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002616 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002617
2618 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2619 if (!property)
2620 return NULL;
2621
2622 if (num_values) {
2623 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2624 if (!property->values)
2625 goto fail;
2626 }
2627
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002628 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2629 if (ret)
2630 goto fail;
2631
Dave Airlief453ba02008-11-07 14:05:41 -08002632 property->flags = flags;
2633 property->num_values = num_values;
2634 INIT_LIST_HEAD(&property->enum_blob_list);
2635
Vinson Lee471dd2e2011-11-10 11:55:40 -08002636 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002637 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002638 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2639 }
Dave Airlief453ba02008-11-07 14:05:41 -08002640
2641 list_add_tail(&property->head, &dev->mode_config.property_list);
2642 return property;
2643fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002644 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002645 kfree(property);
2646 return NULL;
2647}
2648EXPORT_SYMBOL(drm_property_create);
2649
Sascha Hauer4a67d392012-02-06 10:58:17 +01002650struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2651 const char *name,
2652 const struct drm_prop_enum_list *props,
2653 int num_values)
2654{
2655 struct drm_property *property;
2656 int i, ret;
2657
2658 flags |= DRM_MODE_PROP_ENUM;
2659
2660 property = drm_property_create(dev, flags, name, num_values);
2661 if (!property)
2662 return NULL;
2663
2664 for (i = 0; i < num_values; i++) {
2665 ret = drm_property_add_enum(property, i,
2666 props[i].type,
2667 props[i].name);
2668 if (ret) {
2669 drm_property_destroy(dev, property);
2670 return NULL;
2671 }
2672 }
2673
2674 return property;
2675}
2676EXPORT_SYMBOL(drm_property_create_enum);
2677
Rob Clark49e27542012-05-17 02:23:26 -06002678struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2679 int flags, const char *name,
2680 const struct drm_prop_enum_list *props,
2681 int num_values)
2682{
2683 struct drm_property *property;
2684 int i, ret;
2685
2686 flags |= DRM_MODE_PROP_BITMASK;
2687
2688 property = drm_property_create(dev, flags, name, num_values);
2689 if (!property)
2690 return NULL;
2691
2692 for (i = 0; i < num_values; i++) {
2693 ret = drm_property_add_enum(property, i,
2694 props[i].type,
2695 props[i].name);
2696 if (ret) {
2697 drm_property_destroy(dev, property);
2698 return NULL;
2699 }
2700 }
2701
2702 return property;
2703}
2704EXPORT_SYMBOL(drm_property_create_bitmask);
2705
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002706struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2707 const char *name,
2708 uint64_t min, uint64_t max)
2709{
2710 struct drm_property *property;
2711
2712 flags |= DRM_MODE_PROP_RANGE;
2713
2714 property = drm_property_create(dev, flags, name, 2);
2715 if (!property)
2716 return NULL;
2717
2718 property->values[0] = min;
2719 property->values[1] = max;
2720
2721 return property;
2722}
2723EXPORT_SYMBOL(drm_property_create_range);
2724
Dave Airlief453ba02008-11-07 14:05:41 -08002725int drm_property_add_enum(struct drm_property *property, int index,
2726 uint64_t value, const char *name)
2727{
2728 struct drm_property_enum *prop_enum;
2729
Rob Clark49e27542012-05-17 02:23:26 -06002730 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2731 return -EINVAL;
2732
2733 /*
2734 * Bitmask enum properties have the additional constraint of values
2735 * from 0 to 63
2736 */
2737 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002738 return -EINVAL;
2739
2740 if (!list_empty(&property->enum_blob_list)) {
2741 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2742 if (prop_enum->value == value) {
2743 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2744 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2745 return 0;
2746 }
2747 }
2748 }
2749
2750 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2751 if (!prop_enum)
2752 return -ENOMEM;
2753
2754 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2755 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2756 prop_enum->value = value;
2757
2758 property->values[index] = value;
2759 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2760 return 0;
2761}
2762EXPORT_SYMBOL(drm_property_add_enum);
2763
2764void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2765{
2766 struct drm_property_enum *prop_enum, *pt;
2767
2768 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2769 list_del(&prop_enum->head);
2770 kfree(prop_enum);
2771 }
2772
2773 if (property->num_values)
2774 kfree(property->values);
2775 drm_mode_object_put(dev, &property->base);
2776 list_del(&property->head);
2777 kfree(property);
2778}
2779EXPORT_SYMBOL(drm_property_destroy);
2780
Paulo Zanonic5431882012-05-15 18:09:02 -03002781void drm_object_attach_property(struct drm_mode_object *obj,
2782 struct drm_property *property,
2783 uint64_t init_val)
2784{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002785 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002786
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002787 if (count == DRM_OBJECT_MAX_PROPERTY) {
2788 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2789 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2790 "you see this message on the same object type.\n",
2791 obj->type);
2792 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002793 }
2794
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002795 obj->properties->ids[count] = property->base.id;
2796 obj->properties->values[count] = init_val;
2797 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002798}
2799EXPORT_SYMBOL(drm_object_attach_property);
2800
2801int drm_object_property_set_value(struct drm_mode_object *obj,
2802 struct drm_property *property, uint64_t val)
2803{
2804 int i;
2805
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002806 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002807 if (obj->properties->ids[i] == property->base.id) {
2808 obj->properties->values[i] = val;
2809 return 0;
2810 }
2811 }
2812
2813 return -EINVAL;
2814}
2815EXPORT_SYMBOL(drm_object_property_set_value);
2816
2817int drm_object_property_get_value(struct drm_mode_object *obj,
2818 struct drm_property *property, uint64_t *val)
2819{
2820 int i;
2821
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002822 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002823 if (obj->properties->ids[i] == property->base.id) {
2824 *val = obj->properties->values[i];
2825 return 0;
2826 }
2827 }
2828
2829 return -EINVAL;
2830}
2831EXPORT_SYMBOL(drm_object_property_get_value);
2832
Dave Airlief453ba02008-11-07 14:05:41 -08002833int drm_mode_getproperty_ioctl(struct drm_device *dev,
2834 void *data, struct drm_file *file_priv)
2835{
2836 struct drm_mode_object *obj;
2837 struct drm_mode_get_property *out_resp = data;
2838 struct drm_property *property;
2839 int enum_count = 0;
2840 int blob_count = 0;
2841 int value_count = 0;
2842 int ret = 0, i;
2843 int copied;
2844 struct drm_property_enum *prop_enum;
2845 struct drm_mode_property_enum __user *enum_ptr;
2846 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002847 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002848 uint64_t __user *values_ptr;
2849 uint32_t __user *blob_length_ptr;
2850
Dave Airliefb3b06c2011-02-08 13:55:21 +10002851 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2852 return -EINVAL;
2853
Daniel Vetter84849902012-12-02 00:28:11 +01002854 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002855 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2856 if (!obj) {
2857 ret = -EINVAL;
2858 goto done;
2859 }
2860 property = obj_to_property(obj);
2861
Rob Clark49e27542012-05-17 02:23:26 -06002862 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002863 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2864 enum_count++;
2865 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2866 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2867 blob_count++;
2868 }
2869
2870 value_count = property->num_values;
2871
2872 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2873 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2874 out_resp->flags = property->flags;
2875
2876 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002877 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002878 for (i = 0; i < value_count; i++) {
2879 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2880 ret = -EFAULT;
2881 goto done;
2882 }
2883 }
2884 }
2885 out_resp->count_values = value_count;
2886
Rob Clark49e27542012-05-17 02:23:26 -06002887 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002888 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2889 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002890 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002891 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2892
2893 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2894 ret = -EFAULT;
2895 goto done;
2896 }
2897
2898 if (copy_to_user(&enum_ptr[copied].name,
2899 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2900 ret = -EFAULT;
2901 goto done;
2902 }
2903 copied++;
2904 }
2905 }
2906 out_resp->count_enum_blobs = enum_count;
2907 }
2908
2909 if (property->flags & DRM_MODE_PROP_BLOB) {
2910 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2911 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002912 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2913 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002914
2915 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2916 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2917 ret = -EFAULT;
2918 goto done;
2919 }
2920
2921 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2922 ret = -EFAULT;
2923 goto done;
2924 }
2925
2926 copied++;
2927 }
2928 }
2929 out_resp->count_enum_blobs = blob_count;
2930 }
2931done:
Daniel Vetter84849902012-12-02 00:28:11 +01002932 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002933 return ret;
2934}
2935
2936static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2937 void *data)
2938{
2939 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002940 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002941
2942 if (!length || !data)
2943 return NULL;
2944
2945 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2946 if (!blob)
2947 return NULL;
2948
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002949 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2950 if (ret) {
2951 kfree(blob);
2952 return NULL;
2953 }
2954
Dave Airlief453ba02008-11-07 14:05:41 -08002955 blob->length = length;
2956
2957 memcpy(blob->data, data, length);
2958
Dave Airlief453ba02008-11-07 14:05:41 -08002959 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2960 return blob;
2961}
2962
2963static void drm_property_destroy_blob(struct drm_device *dev,
2964 struct drm_property_blob *blob)
2965{
2966 drm_mode_object_put(dev, &blob->base);
2967 list_del(&blob->head);
2968 kfree(blob);
2969}
2970
2971int drm_mode_getblob_ioctl(struct drm_device *dev,
2972 void *data, struct drm_file *file_priv)
2973{
2974 struct drm_mode_object *obj;
2975 struct drm_mode_get_blob *out_resp = data;
2976 struct drm_property_blob *blob;
2977 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002978 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002979
Dave Airliefb3b06c2011-02-08 13:55:21 +10002980 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2981 return -EINVAL;
2982
Daniel Vetter84849902012-12-02 00:28:11 +01002983 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002984 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2985 if (!obj) {
2986 ret = -EINVAL;
2987 goto done;
2988 }
2989 blob = obj_to_blob(obj);
2990
2991 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002992 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08002993 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2994 ret = -EFAULT;
2995 goto done;
2996 }
2997 }
2998 out_resp->length = blob->length;
2999
3000done:
Daniel Vetter84849902012-12-02 00:28:11 +01003001 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003002 return ret;
3003}
3004
3005int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3006 struct edid *edid)
3007{
3008 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003009 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003010
3011 if (connector->edid_blob_ptr)
3012 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3013
3014 /* Delete edid, when there is none. */
3015 if (!edid) {
3016 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003017 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003018 return ret;
3019 }
3020
Adam Jackson7466f4c2010-03-29 21:43:23 +00003021 size = EDID_LENGTH * (1 + edid->extensions);
3022 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3023 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003024 if (!connector->edid_blob_ptr)
3025 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003026
Rob Clark58495562012-10-11 20:50:56 -05003027 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003028 dev->mode_config.edid_property,
3029 connector->edid_blob_ptr->base.id);
3030
3031 return ret;
3032}
3033EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3034
Paulo Zanoni26a34812012-05-15 18:08:59 -03003035static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003036 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003037{
3038 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3039 return false;
3040 if (property->flags & DRM_MODE_PROP_RANGE) {
3041 if (value < property->values[0] || value > property->values[1])
3042 return false;
3043 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003044 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3045 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003046 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003047 for (i = 0; i < property->num_values; i++)
3048 valid_mask |= (1ULL << property->values[i]);
3049 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003050 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3051 /* Only the driver knows */
3052 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003053 } else {
3054 int i;
3055 for (i = 0; i < property->num_values; i++)
3056 if (property->values[i] == value)
3057 return true;
3058 return false;
3059 }
3060}
3061
Dave Airlief453ba02008-11-07 14:05:41 -08003062int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3063 void *data, struct drm_file *file_priv)
3064{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003065 struct drm_mode_connector_set_property *conn_set_prop = data;
3066 struct drm_mode_obj_set_property obj_set_prop = {
3067 .value = conn_set_prop->value,
3068 .prop_id = conn_set_prop->prop_id,
3069 .obj_id = conn_set_prop->connector_id,
3070 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3071 };
Dave Airlief453ba02008-11-07 14:05:41 -08003072
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003073 /* It does all the locking and checking we need */
3074 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003075}
3076
Paulo Zanonic5431882012-05-15 18:09:02 -03003077static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3078 struct drm_property *property,
3079 uint64_t value)
3080{
3081 int ret = -EINVAL;
3082 struct drm_connector *connector = obj_to_connector(obj);
3083
3084 /* Do DPMS ourselves */
3085 if (property == connector->dev->mode_config.dpms_property) {
3086 if (connector->funcs->dpms)
3087 (*connector->funcs->dpms)(connector, (int)value);
3088 ret = 0;
3089 } else if (connector->funcs->set_property)
3090 ret = connector->funcs->set_property(connector, property, value);
3091
3092 /* store the property value if successful */
3093 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003094 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003095 return ret;
3096}
3097
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003098static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3099 struct drm_property *property,
3100 uint64_t value)
3101{
3102 int ret = -EINVAL;
3103 struct drm_crtc *crtc = obj_to_crtc(obj);
3104
3105 if (crtc->funcs->set_property)
3106 ret = crtc->funcs->set_property(crtc, property, value);
3107 if (!ret)
3108 drm_object_property_set_value(obj, property, value);
3109
3110 return ret;
3111}
3112
Rob Clark4d939142012-05-17 02:23:27 -06003113static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3114 struct drm_property *property,
3115 uint64_t value)
3116{
3117 int ret = -EINVAL;
3118 struct drm_plane *plane = obj_to_plane(obj);
3119
3120 if (plane->funcs->set_property)
3121 ret = plane->funcs->set_property(plane, property, value);
3122 if (!ret)
3123 drm_object_property_set_value(obj, property, value);
3124
3125 return ret;
3126}
3127
Paulo Zanonic5431882012-05-15 18:09:02 -03003128int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3129 struct drm_file *file_priv)
3130{
3131 struct drm_mode_obj_get_properties *arg = data;
3132 struct drm_mode_object *obj;
3133 int ret = 0;
3134 int i;
3135 int copied = 0;
3136 int props_count = 0;
3137 uint32_t __user *props_ptr;
3138 uint64_t __user *prop_values_ptr;
3139
3140 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3141 return -EINVAL;
3142
Daniel Vetter84849902012-12-02 00:28:11 +01003143 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003144
3145 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3146 if (!obj) {
3147 ret = -EINVAL;
3148 goto out;
3149 }
3150 if (!obj->properties) {
3151 ret = -EINVAL;
3152 goto out;
3153 }
3154
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003155 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003156
3157 /* This ioctl is called twice, once to determine how much space is
3158 * needed, and the 2nd time to fill it. */
3159 if ((arg->count_props >= props_count) && props_count) {
3160 copied = 0;
3161 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3162 prop_values_ptr = (uint64_t __user *)(unsigned long)
3163 (arg->prop_values_ptr);
3164 for (i = 0; i < props_count; i++) {
3165 if (put_user(obj->properties->ids[i],
3166 props_ptr + copied)) {
3167 ret = -EFAULT;
3168 goto out;
3169 }
3170 if (put_user(obj->properties->values[i],
3171 prop_values_ptr + copied)) {
3172 ret = -EFAULT;
3173 goto out;
3174 }
3175 copied++;
3176 }
3177 }
3178 arg->count_props = props_count;
3179out:
Daniel Vetter84849902012-12-02 00:28:11 +01003180 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003181 return ret;
3182}
3183
3184int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3185 struct drm_file *file_priv)
3186{
3187 struct drm_mode_obj_set_property *arg = data;
3188 struct drm_mode_object *arg_obj;
3189 struct drm_mode_object *prop_obj;
3190 struct drm_property *property;
3191 int ret = -EINVAL;
3192 int i;
3193
3194 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3195 return -EINVAL;
3196
Daniel Vetter84849902012-12-02 00:28:11 +01003197 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003198
3199 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3200 if (!arg_obj)
3201 goto out;
3202 if (!arg_obj->properties)
3203 goto out;
3204
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003205 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003206 if (arg_obj->properties->ids[i] == arg->prop_id)
3207 break;
3208
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003209 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003210 goto out;
3211
3212 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3213 DRM_MODE_OBJECT_PROPERTY);
3214 if (!prop_obj)
3215 goto out;
3216 property = obj_to_property(prop_obj);
3217
3218 if (!drm_property_change_is_valid(property, arg->value))
3219 goto out;
3220
3221 switch (arg_obj->type) {
3222 case DRM_MODE_OBJECT_CONNECTOR:
3223 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3224 arg->value);
3225 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003226 case DRM_MODE_OBJECT_CRTC:
3227 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3228 break;
Rob Clark4d939142012-05-17 02:23:27 -06003229 case DRM_MODE_OBJECT_PLANE:
3230 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3231 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003232 }
3233
3234out:
Daniel Vetter84849902012-12-02 00:28:11 +01003235 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003236 return ret;
3237}
3238
Dave Airlief453ba02008-11-07 14:05:41 -08003239int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3240 struct drm_encoder *encoder)
3241{
3242 int i;
3243
3244 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3245 if (connector->encoder_ids[i] == 0) {
3246 connector->encoder_ids[i] = encoder->base.id;
3247 return 0;
3248 }
3249 }
3250 return -ENOMEM;
3251}
3252EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3253
3254void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3255 struct drm_encoder *encoder)
3256{
3257 int i;
3258 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3259 if (connector->encoder_ids[i] == encoder->base.id) {
3260 connector->encoder_ids[i] = 0;
3261 if (connector->encoder == encoder)
3262 connector->encoder = NULL;
3263 break;
3264 }
3265 }
3266}
3267EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3268
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003269int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003270 int gamma_size)
3271{
3272 crtc->gamma_size = gamma_size;
3273
3274 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3275 if (!crtc->gamma_store) {
3276 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003277 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003278 }
3279
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003280 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003281}
3282EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3283
3284int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3285 void *data, struct drm_file *file_priv)
3286{
3287 struct drm_mode_crtc_lut *crtc_lut = data;
3288 struct drm_mode_object *obj;
3289 struct drm_crtc *crtc;
3290 void *r_base, *g_base, *b_base;
3291 int size;
3292 int ret = 0;
3293
Dave Airliefb3b06c2011-02-08 13:55:21 +10003294 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3295 return -EINVAL;
3296
Daniel Vetter84849902012-12-02 00:28:11 +01003297 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003298 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3299 if (!obj) {
3300 ret = -EINVAL;
3301 goto out;
3302 }
3303 crtc = obj_to_crtc(obj);
3304
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003305 if (crtc->funcs->gamma_set == NULL) {
3306 ret = -ENOSYS;
3307 goto out;
3308 }
3309
Dave Airlief453ba02008-11-07 14:05:41 -08003310 /* memcpy into gamma store */
3311 if (crtc_lut->gamma_size != crtc->gamma_size) {
3312 ret = -EINVAL;
3313 goto out;
3314 }
3315
3316 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3317 r_base = crtc->gamma_store;
3318 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3319 ret = -EFAULT;
3320 goto out;
3321 }
3322
3323 g_base = r_base + size;
3324 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3325 ret = -EFAULT;
3326 goto out;
3327 }
3328
3329 b_base = g_base + size;
3330 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3331 ret = -EFAULT;
3332 goto out;
3333 }
3334
James Simmons72034252010-08-03 01:33:19 +01003335 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003336
3337out:
Daniel Vetter84849902012-12-02 00:28:11 +01003338 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003339 return ret;
3340
3341}
3342
3343int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3344 void *data, struct drm_file *file_priv)
3345{
3346 struct drm_mode_crtc_lut *crtc_lut = data;
3347 struct drm_mode_object *obj;
3348 struct drm_crtc *crtc;
3349 void *r_base, *g_base, *b_base;
3350 int size;
3351 int ret = 0;
3352
Dave Airliefb3b06c2011-02-08 13:55:21 +10003353 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3354 return -EINVAL;
3355
Daniel Vetter84849902012-12-02 00:28:11 +01003356 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003357 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3358 if (!obj) {
3359 ret = -EINVAL;
3360 goto out;
3361 }
3362 crtc = obj_to_crtc(obj);
3363
3364 /* memcpy into gamma store */
3365 if (crtc_lut->gamma_size != crtc->gamma_size) {
3366 ret = -EINVAL;
3367 goto out;
3368 }
3369
3370 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3371 r_base = crtc->gamma_store;
3372 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3373 ret = -EFAULT;
3374 goto out;
3375 }
3376
3377 g_base = r_base + size;
3378 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3379 ret = -EFAULT;
3380 goto out;
3381 }
3382
3383 b_base = g_base + size;
3384 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3385 ret = -EFAULT;
3386 goto out;
3387 }
3388out:
Daniel Vetter84849902012-12-02 00:28:11 +01003389 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003390 return ret;
3391}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003392
3393int drm_mode_page_flip_ioctl(struct drm_device *dev,
3394 void *data, struct drm_file *file_priv)
3395{
3396 struct drm_mode_crtc_page_flip *page_flip = data;
3397 struct drm_mode_object *obj;
3398 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003399 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003400 struct drm_pending_vblank_event *e = NULL;
3401 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003402 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003403 int ret = -EINVAL;
3404
3405 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3406 page_flip->reserved != 0)
3407 return -EINVAL;
3408
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003409 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3410 if (!obj)
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003411 return -EINVAL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003412 crtc = obj_to_crtc(obj);
3413
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003414 mutex_lock(&crtc->mutex);
Chris Wilson90c1efd2010-07-17 20:23:26 +01003415 if (crtc->fb == NULL) {
3416 /* The framebuffer is currently unbound, presumably
3417 * due to a hotplug event, that userspace has not
3418 * yet discovered.
3419 */
3420 ret = -EBUSY;
3421 goto out;
3422 }
3423
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003424 if (crtc->funcs->page_flip == NULL)
3425 goto out;
3426
Daniel Vetter786b99e2012-12-02 21:53:40 +01003427 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3428 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003429 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003430
Rob Clark7c80e122012-09-04 16:35:56 +00003431 hdisplay = crtc->mode.hdisplay;
3432 vdisplay = crtc->mode.vdisplay;
3433
3434 if (crtc->invert_dimensions)
3435 swap(hdisplay, vdisplay);
3436
3437 if (hdisplay > fb->width ||
3438 vdisplay > fb->height ||
3439 crtc->x > fb->width - hdisplay ||
3440 crtc->y > fb->height - vdisplay) {
3441 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3442 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3443 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003444 ret = -ENOSPC;
3445 goto out;
3446 }
3447
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02003448 if (crtc->fb->pixel_format != fb->pixel_format) {
3449 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
3450 ret = -EINVAL;
3451 goto out;
3452 }
3453
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003454 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3455 ret = -ENOMEM;
3456 spin_lock_irqsave(&dev->event_lock, flags);
3457 if (file_priv->event_space < sizeof e->event) {
3458 spin_unlock_irqrestore(&dev->event_lock, flags);
3459 goto out;
3460 }
3461 file_priv->event_space -= sizeof e->event;
3462 spin_unlock_irqrestore(&dev->event_lock, flags);
3463
3464 e = kzalloc(sizeof *e, GFP_KERNEL);
3465 if (e == NULL) {
3466 spin_lock_irqsave(&dev->event_lock, flags);
3467 file_priv->event_space += sizeof e->event;
3468 spin_unlock_irqrestore(&dev->event_lock, flags);
3469 goto out;
3470 }
3471
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003472 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003473 e->event.base.length = sizeof e->event;
3474 e->event.user_data = page_flip->user_data;
3475 e->base.event = &e->event.base;
3476 e->base.file_priv = file_priv;
3477 e->base.destroy =
3478 (void (*) (struct drm_pending_event *)) kfree;
3479 }
3480
Daniel Vetterb0d12322012-12-11 01:07:12 +01003481 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003482 ret = crtc->funcs->page_flip(crtc, fb, e);
3483 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003484 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3485 spin_lock_irqsave(&dev->event_lock, flags);
3486 file_priv->event_space += sizeof e->event;
3487 spin_unlock_irqrestore(&dev->event_lock, flags);
3488 kfree(e);
3489 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003490 /* Keep the old fb, don't unref it. */
3491 old_fb = NULL;
3492 } else {
Thierry Reding8cf1e982013-02-13 16:08:33 +01003493 /*
3494 * Warn if the driver hasn't properly updated the crtc->fb
3495 * field to reflect that the new framebuffer is now used.
3496 * Failing to do so will screw with the reference counting
3497 * on framebuffers.
3498 */
3499 WARN_ON(crtc->fb != fb);
Daniel Vetterb0d12322012-12-11 01:07:12 +01003500 /* Unref only the old framebuffer. */
3501 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003502 }
3503
3504out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003505 if (fb)
3506 drm_framebuffer_unreference(fb);
3507 if (old_fb)
3508 drm_framebuffer_unreference(old_fb);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01003509 mutex_unlock(&crtc->mutex);
3510
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003511 return ret;
3512}
Chris Wilsoneb033552011-01-24 15:11:08 +00003513
3514void drm_mode_config_reset(struct drm_device *dev)
3515{
3516 struct drm_crtc *crtc;
3517 struct drm_encoder *encoder;
3518 struct drm_connector *connector;
3519
3520 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3521 if (crtc->funcs->reset)
3522 crtc->funcs->reset(crtc);
3523
3524 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3525 if (encoder->funcs->reset)
3526 encoder->funcs->reset(encoder);
3527
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003528 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3529 connector->status = connector_status_unknown;
3530
Chris Wilsoneb033552011-01-24 15:11:08 +00003531 if (connector->funcs->reset)
3532 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003533 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003534}
3535EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003536
3537int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3538 void *data, struct drm_file *file_priv)
3539{
3540 struct drm_mode_create_dumb *args = data;
3541
3542 if (!dev->driver->dumb_create)
3543 return -ENOSYS;
3544 return dev->driver->dumb_create(file_priv, dev, args);
3545}
3546
3547int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3548 void *data, struct drm_file *file_priv)
3549{
3550 struct drm_mode_map_dumb *args = data;
3551
3552 /* call driver ioctl to get mmap offset */
3553 if (!dev->driver->dumb_map_offset)
3554 return -ENOSYS;
3555
3556 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3557}
3558
3559int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3560 void *data, struct drm_file *file_priv)
3561{
3562 struct drm_mode_destroy_dumb *args = data;
3563
3564 if (!dev->driver->dumb_destroy)
3565 return -ENOSYS;
3566
3567 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3568}
Dave Airlie248dbc22011-11-29 20:02:54 +00003569
3570/*
3571 * Just need to support RGB formats here for compat with code that doesn't
3572 * use pixel formats directly yet.
3573 */
3574void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3575 int *bpp)
3576{
3577 switch (format) {
Ville Syrjäläc51a6bc2013-01-31 19:43:37 +02003578 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003579 case DRM_FORMAT_RGB332:
3580 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003581 *depth = 8;
3582 *bpp = 8;
3583 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003584 case DRM_FORMAT_XRGB1555:
3585 case DRM_FORMAT_XBGR1555:
3586 case DRM_FORMAT_RGBX5551:
3587 case DRM_FORMAT_BGRX5551:
3588 case DRM_FORMAT_ARGB1555:
3589 case DRM_FORMAT_ABGR1555:
3590 case DRM_FORMAT_RGBA5551:
3591 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003592 *depth = 15;
3593 *bpp = 16;
3594 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003595 case DRM_FORMAT_RGB565:
3596 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003597 *depth = 16;
3598 *bpp = 16;
3599 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003600 case DRM_FORMAT_RGB888:
3601 case DRM_FORMAT_BGR888:
3602 *depth = 24;
3603 *bpp = 24;
3604 break;
3605 case DRM_FORMAT_XRGB8888:
3606 case DRM_FORMAT_XBGR8888:
3607 case DRM_FORMAT_RGBX8888:
3608 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003609 *depth = 24;
3610 *bpp = 32;
3611 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003612 case DRM_FORMAT_XRGB2101010:
3613 case DRM_FORMAT_XBGR2101010:
3614 case DRM_FORMAT_RGBX1010102:
3615 case DRM_FORMAT_BGRX1010102:
3616 case DRM_FORMAT_ARGB2101010:
3617 case DRM_FORMAT_ABGR2101010:
3618 case DRM_FORMAT_RGBA1010102:
3619 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003620 *depth = 30;
3621 *bpp = 32;
3622 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003623 case DRM_FORMAT_ARGB8888:
3624 case DRM_FORMAT_ABGR8888:
3625 case DRM_FORMAT_RGBA8888:
3626 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003627 *depth = 32;
3628 *bpp = 32;
3629 break;
3630 default:
3631 DRM_DEBUG_KMS("unsupported pixel format\n");
3632 *depth = 0;
3633 *bpp = 0;
3634 break;
3635 }
3636}
3637EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003638
3639/**
3640 * drm_format_num_planes - get the number of planes for format
3641 * @format: pixel format (DRM_FORMAT_*)
3642 *
3643 * RETURNS:
3644 * The number of planes used by the specified pixel format.
3645 */
3646int drm_format_num_planes(uint32_t format)
3647{
3648 switch (format) {
3649 case DRM_FORMAT_YUV410:
3650 case DRM_FORMAT_YVU410:
3651 case DRM_FORMAT_YUV411:
3652 case DRM_FORMAT_YVU411:
3653 case DRM_FORMAT_YUV420:
3654 case DRM_FORMAT_YVU420:
3655 case DRM_FORMAT_YUV422:
3656 case DRM_FORMAT_YVU422:
3657 case DRM_FORMAT_YUV444:
3658 case DRM_FORMAT_YVU444:
3659 return 3;
3660 case DRM_FORMAT_NV12:
3661 case DRM_FORMAT_NV21:
3662 case DRM_FORMAT_NV16:
3663 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003664 case DRM_FORMAT_NV24:
3665 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003666 return 2;
3667 default:
3668 return 1;
3669 }
3670}
3671EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003672
3673/**
3674 * drm_format_plane_cpp - determine the bytes per pixel value
3675 * @format: pixel format (DRM_FORMAT_*)
3676 * @plane: plane index
3677 *
3678 * RETURNS:
3679 * The bytes per pixel value for the specified plane.
3680 */
3681int drm_format_plane_cpp(uint32_t format, int plane)
3682{
3683 unsigned int depth;
3684 int bpp;
3685
3686 if (plane >= drm_format_num_planes(format))
3687 return 0;
3688
3689 switch (format) {
3690 case DRM_FORMAT_YUYV:
3691 case DRM_FORMAT_YVYU:
3692 case DRM_FORMAT_UYVY:
3693 case DRM_FORMAT_VYUY:
3694 return 2;
3695 case DRM_FORMAT_NV12:
3696 case DRM_FORMAT_NV21:
3697 case DRM_FORMAT_NV16:
3698 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003699 case DRM_FORMAT_NV24:
3700 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003701 return plane ? 2 : 1;
3702 case DRM_FORMAT_YUV410:
3703 case DRM_FORMAT_YVU410:
3704 case DRM_FORMAT_YUV411:
3705 case DRM_FORMAT_YVU411:
3706 case DRM_FORMAT_YUV420:
3707 case DRM_FORMAT_YVU420:
3708 case DRM_FORMAT_YUV422:
3709 case DRM_FORMAT_YVU422:
3710 case DRM_FORMAT_YUV444:
3711 case DRM_FORMAT_YVU444:
3712 return 1;
3713 default:
3714 drm_fb_get_bpp_depth(format, &depth, &bpp);
3715 return bpp >> 3;
3716 }
3717}
3718EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003719
3720/**
3721 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3722 * @format: pixel format (DRM_FORMAT_*)
3723 *
3724 * RETURNS:
3725 * The horizontal chroma subsampling factor for the
3726 * specified pixel format.
3727 */
3728int drm_format_horz_chroma_subsampling(uint32_t format)
3729{
3730 switch (format) {
3731 case DRM_FORMAT_YUV411:
3732 case DRM_FORMAT_YVU411:
3733 case DRM_FORMAT_YUV410:
3734 case DRM_FORMAT_YVU410:
3735 return 4;
3736 case DRM_FORMAT_YUYV:
3737 case DRM_FORMAT_YVYU:
3738 case DRM_FORMAT_UYVY:
3739 case DRM_FORMAT_VYUY:
3740 case DRM_FORMAT_NV12:
3741 case DRM_FORMAT_NV21:
3742 case DRM_FORMAT_NV16:
3743 case DRM_FORMAT_NV61:
3744 case DRM_FORMAT_YUV422:
3745 case DRM_FORMAT_YVU422:
3746 case DRM_FORMAT_YUV420:
3747 case DRM_FORMAT_YVU420:
3748 return 2;
3749 default:
3750 return 1;
3751 }
3752}
3753EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3754
3755/**
3756 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3757 * @format: pixel format (DRM_FORMAT_*)
3758 *
3759 * RETURNS:
3760 * The vertical chroma subsampling factor for the
3761 * specified pixel format.
3762 */
3763int drm_format_vert_chroma_subsampling(uint32_t format)
3764{
3765 switch (format) {
3766 case DRM_FORMAT_YUV410:
3767 case DRM_FORMAT_YVU410:
3768 return 4;
3769 case DRM_FORMAT_YUV420:
3770 case DRM_FORMAT_YVU420:
3771 case DRM_FORMAT_NV12:
3772 case DRM_FORMAT_NV21:
3773 return 2;
3774 default:
3775 return 1;
3776 }
3777}
3778EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02003779
3780/**
3781 * drm_mode_config_init - initialize DRM mode_configuration structure
3782 * @dev: DRM device
3783 *
3784 * Initialize @dev's mode_config structure, used for tracking the graphics
3785 * configuration of @dev.
3786 *
3787 * Since this initializes the modeset locks, no locking is possible. Which is no
3788 * problem, since this should happen single threaded at init time. It is the
3789 * driver's problem to ensure this guarantee.
3790 *
3791 */
3792void drm_mode_config_init(struct drm_device *dev)
3793{
3794 mutex_init(&dev->mode_config.mutex);
3795 mutex_init(&dev->mode_config.idr_mutex);
3796 mutex_init(&dev->mode_config.fb_lock);
3797 INIT_LIST_HEAD(&dev->mode_config.fb_list);
3798 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
3799 INIT_LIST_HEAD(&dev->mode_config.connector_list);
3800 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
3801 INIT_LIST_HEAD(&dev->mode_config.property_list);
3802 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
3803 INIT_LIST_HEAD(&dev->mode_config.plane_list);
3804 idr_init(&dev->mode_config.crtc_idr);
3805
3806 drm_modeset_lock_all(dev);
3807 drm_mode_create_standard_connector_properties(dev);
3808 drm_modeset_unlock_all(dev);
3809
3810 /* Just to be sure */
3811 dev->mode_config.num_fb = 0;
3812 dev->mode_config.num_connector = 0;
3813 dev->mode_config.num_crtc = 0;
3814 dev->mode_config.num_encoder = 0;
3815}
3816EXPORT_SYMBOL(drm_mode_config_init);
3817
3818/**
3819 * drm_mode_config_cleanup - free up DRM mode_config info
3820 * @dev: DRM device
3821 *
3822 * Free up all the connectors and CRTCs associated with this DRM device, then
3823 * free up the framebuffers and associated buffer objects.
3824 *
3825 * Note that since this /should/ happen single-threaded at driver/device
3826 * teardown time, no locking is required. It's the driver's job to ensure that
3827 * this guarantee actually holds true.
3828 *
3829 * FIXME: cleanup any dangling user buffer objects too
3830 */
3831void drm_mode_config_cleanup(struct drm_device *dev)
3832{
3833 struct drm_connector *connector, *ot;
3834 struct drm_crtc *crtc, *ct;
3835 struct drm_encoder *encoder, *enct;
3836 struct drm_framebuffer *fb, *fbt;
3837 struct drm_property *property, *pt;
3838 struct drm_property_blob *blob, *bt;
3839 struct drm_plane *plane, *plt;
3840
3841 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
3842 head) {
3843 encoder->funcs->destroy(encoder);
3844 }
3845
3846 list_for_each_entry_safe(connector, ot,
3847 &dev->mode_config.connector_list, head) {
3848 connector->funcs->destroy(connector);
3849 }
3850
3851 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
3852 head) {
3853 drm_property_destroy(dev, property);
3854 }
3855
3856 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
3857 head) {
3858 drm_property_destroy_blob(dev, blob);
3859 }
3860
3861 /*
3862 * Single-threaded teardown context, so it's not required to grab the
3863 * fb_lock to protect against concurrent fb_list access. Contrary, it
3864 * would actually deadlock with the drm_framebuffer_cleanup function.
3865 *
3866 * Also, if there are any framebuffers left, that's a driver leak now,
3867 * so politely WARN about this.
3868 */
3869 WARN_ON(!list_empty(&dev->mode_config.fb_list));
3870 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
3871 drm_framebuffer_remove(fb);
3872 }
3873
3874 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
3875 head) {
3876 plane->funcs->destroy(plane);
3877 }
3878
3879 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
3880 crtc->funcs->destroy(crtc);
3881 }
3882
3883 idr_destroy(&dev->mode_config.crtc_idr);
3884}
3885EXPORT_SYMBOL(drm_mode_config_cleanup);