blob: 9492e7ec9d9d86600da9e34124d8b0a340e7ecab [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}
Daniel Vetter36206362012-12-10 20:42:17 +010071
Daniel Vetter84849902012-12-02 00:28:11 +010072EXPORT_SYMBOL(drm_modeset_unlock_all);
73
Dave Airlief453ba02008-11-07 14:05:41 -080074/* Avoid boilerplate. I'm tired of typing. */
75#define DRM_ENUM_NAME_FN(fnname, list) \
76 char *fnname(int val) \
77 { \
78 int i; \
79 for (i = 0; i < ARRAY_SIZE(list); i++) { \
80 if (list[i].type == val) \
81 return list[i].name; \
82 } \
83 return "(unknown)"; \
84 }
85
86/*
87 * Global properties
88 */
89static struct drm_prop_enum_list drm_dpms_enum_list[] =
90{ { DRM_MODE_DPMS_ON, "On" },
91 { DRM_MODE_DPMS_STANDBY, "Standby" },
92 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
93 { DRM_MODE_DPMS_OFF, "Off" }
94};
95
96DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
97
98/*
99 * Optional properties
100 */
101static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
102{
Jesse Barnes53bd8382009-07-01 10:04:40 -0700103 { DRM_MODE_SCALE_NONE, "None" },
104 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
105 { DRM_MODE_SCALE_CENTER, "Center" },
106 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -0800107};
108
109static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
110{
111 { DRM_MODE_DITHERING_OFF, "Off" },
112 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000113 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800114};
115
116/*
117 * Non-global properties, but "required" for certain connectors.
118 */
119static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
120{
121 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
122 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
123 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
124};
125
126DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
127
128static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
129{
130 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
131 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
132 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
133};
134
135DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
136 drm_dvi_i_subconnector_enum_list)
137
138static struct drm_prop_enum_list drm_tv_select_enum_list[] =
139{
140 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
141 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
142 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
143 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200144 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800145};
146
147DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
148
149static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
150{
151 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
152 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
153 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
154 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200155 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800156};
157
158DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
159 drm_tv_subconnector_enum_list)
160
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000161static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
162 { DRM_MODE_DIRTY_OFF, "Off" },
163 { DRM_MODE_DIRTY_ON, "On" },
164 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
165};
166
167DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
168 drm_dirty_info_enum_list)
169
Dave Airlief453ba02008-11-07 14:05:41 -0800170struct drm_conn_prop_enum_list {
171 int type;
172 char *name;
173 int count;
174};
175
176/*
177 * Connector and encoder types.
178 */
179static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
180{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
181 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
182 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
183 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
184 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
185 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
186 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
187 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
188 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500189 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
190 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
191 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
192 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200193 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500194 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200195 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800196};
197
198static struct drm_prop_enum_list drm_encoder_enum_list[] =
199{ { DRM_MODE_ENCODER_NONE, "None" },
200 { DRM_MODE_ENCODER_DAC, "DAC" },
201 { DRM_MODE_ENCODER_TMDS, "TMDS" },
202 { DRM_MODE_ENCODER_LVDS, "LVDS" },
203 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200204 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800205};
206
207char *drm_get_encoder_name(struct drm_encoder *encoder)
208{
209 static char buf[32];
210
211 snprintf(buf, 32, "%s-%d",
212 drm_encoder_enum_list[encoder->encoder_type].name,
213 encoder->base.id);
214 return buf;
215}
Dave Airlie13a81952009-09-07 15:45:33 +1000216EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800217
218char *drm_get_connector_name(struct drm_connector *connector)
219{
220 static char buf[32];
221
222 snprintf(buf, 32, "%s-%d",
223 drm_connector_enum_list[connector->connector_type].name,
224 connector->connector_type_id);
225 return buf;
226}
227EXPORT_SYMBOL(drm_get_connector_name);
228
229char *drm_get_connector_status_name(enum drm_connector_status status)
230{
231 if (status == connector_status_connected)
232 return "connected";
233 else if (status == connector_status_disconnected)
234 return "disconnected";
235 else
236 return "unknown";
237}
238
239/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100240 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800241 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100242 * @obj: object pointer, used to generate unique ID
243 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800244 *
Dave Airlief453ba02008-11-07 14:05:41 -0800245 * Create a unique identifier based on @ptr in @dev's identifier space. Used
246 * for tracking modes, CRTCs and connectors.
247 *
248 * RETURNS:
249 * New unique (relative to other objects in @dev) integer identifier for the
250 * object.
251 */
252static int drm_mode_object_get(struct drm_device *dev,
253 struct drm_mode_object *obj, uint32_t obj_type)
254{
255 int new_id = 0;
256 int ret;
257
Dave Airlief453ba02008-11-07 14:05:41 -0800258again:
259 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
260 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200261 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800262 }
263
Jesse Barnesad2563c2009-01-19 17:21:45 +1000264 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800265 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100266
267 if (!ret) {
268 /*
269 * Set up the object linking under the protection of the idr
270 * lock so that other users can't see inconsistent state.
271 */
272 obj->id = new_id;
273 obj->type = obj_type;
274 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000275 mutex_unlock(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100276
Dave Airlief453ba02008-11-07 14:05:41 -0800277 if (ret == -EAGAIN)
278 goto again;
279
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100280 return ret;
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)
412 kref_get(&fb->refcount);
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 */
706 INIT_LIST_HEAD(&connector->user_modes);
707 INIT_LIST_HEAD(&connector->probed_modes);
708 INIT_LIST_HEAD(&connector->modes);
709 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000710 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800711
712 list_add_tail(&connector->head, &dev->mode_config.connector_list);
713 dev->mode_config.num_connector++;
714
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200715 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500716 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200717 dev->mode_config.edid_property,
718 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800719
Rob Clark58495562012-10-11 20:50:56 -0500720 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800721 dev->mode_config.dpms_property, 0);
722
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200723 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100724 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200725
726 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800727}
728EXPORT_SYMBOL(drm_connector_init);
729
730/**
731 * drm_connector_cleanup - cleans up an initialised connector
732 * @connector: connector to cleanup
733 *
Dave Airlief453ba02008-11-07 14:05:41 -0800734 * Cleans up the connector but doesn't free the object.
735 */
736void drm_connector_cleanup(struct drm_connector *connector)
737{
738 struct drm_device *dev = connector->dev;
739 struct drm_display_mode *mode, *t;
740
741 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
742 drm_mode_remove(connector, mode);
743
744 list_for_each_entry_safe(mode, t, &connector->modes, head)
745 drm_mode_remove(connector, mode);
746
747 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
748 drm_mode_remove(connector, mode);
749
Dave Airlief453ba02008-11-07 14:05:41 -0800750 drm_mode_object_put(dev, &connector->base);
751 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000752 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800753}
754EXPORT_SYMBOL(drm_connector_cleanup);
755
Dave Airliecbc7e222012-02-20 14:16:40 +0000756void drm_connector_unplug_all(struct drm_device *dev)
757{
758 struct drm_connector *connector;
759
760 /* taking the mode config mutex ends up in a clash with sysfs */
761 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
762 drm_sysfs_connector_remove(connector);
763
764}
765EXPORT_SYMBOL(drm_connector_unplug_all);
766
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200767int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000768 struct drm_encoder *encoder,
769 const struct drm_encoder_funcs *funcs,
770 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800771{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200772 int ret;
773
Daniel Vetter84849902012-12-02 00:28:11 +0100774 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800775
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200776 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
777 if (ret)
778 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800779
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200780 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800781 encoder->encoder_type = encoder_type;
782 encoder->funcs = funcs;
783
784 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
785 dev->mode_config.num_encoder++;
786
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200787 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100788 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200789
790 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800791}
792EXPORT_SYMBOL(drm_encoder_init);
793
794void drm_encoder_cleanup(struct drm_encoder *encoder)
795{
796 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100797 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800798 drm_mode_object_put(dev, &encoder->base);
799 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000800 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100801 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800802}
803EXPORT_SYMBOL(drm_encoder_cleanup);
804
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800805int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
806 unsigned long possible_crtcs,
807 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600808 const uint32_t *formats, uint32_t format_count,
809 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800810{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200811 int ret;
812
Daniel Vetter84849902012-12-02 00:28:11 +0100813 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800814
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200815 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
816 if (ret)
817 goto out;
818
Rob Clark4d939142012-05-17 02:23:27 -0600819 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800820 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800821 plane->funcs = funcs;
822 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
823 GFP_KERNEL);
824 if (!plane->format_types) {
825 DRM_DEBUG_KMS("out of memory when allocating plane\n");
826 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200827 ret = -ENOMEM;
828 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800829 }
830
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800831 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800832 plane->format_count = format_count;
833 plane->possible_crtcs = possible_crtcs;
834
Rob Clark0a7eb242011-12-13 20:19:36 -0600835 /* private planes are not exposed to userspace, but depending on
836 * display hardware, might be convenient to allow sharing programming
837 * for the scanout engine with the crtc implementation.
838 */
839 if (!priv) {
840 list_add_tail(&plane->head, &dev->mode_config.plane_list);
841 dev->mode_config.num_plane++;
842 } else {
843 INIT_LIST_HEAD(&plane->head);
844 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800845
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200846 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100847 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800848
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200849 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800850}
851EXPORT_SYMBOL(drm_plane_init);
852
853void drm_plane_cleanup(struct drm_plane *plane)
854{
855 struct drm_device *dev = plane->dev;
856
Daniel Vetter84849902012-12-02 00:28:11 +0100857 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800858 kfree(plane->format_types);
859 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600860 /* if not added to a list, it must be a private plane */
861 if (!list_empty(&plane->head)) {
862 list_del(&plane->head);
863 dev->mode_config.num_plane--;
864 }
Daniel Vetter84849902012-12-02 00:28:11 +0100865 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800866}
867EXPORT_SYMBOL(drm_plane_cleanup);
868
Dave Airlief453ba02008-11-07 14:05:41 -0800869/**
870 * drm_mode_create - create a new display mode
871 * @dev: DRM device
872 *
Dave Airlief453ba02008-11-07 14:05:41 -0800873 * Create a new drm_display_mode, give it an ID, and return it.
874 *
875 * RETURNS:
876 * Pointer to new mode on success, NULL on error.
877 */
878struct drm_display_mode *drm_mode_create(struct drm_device *dev)
879{
880 struct drm_display_mode *nmode;
881
882 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
883 if (!nmode)
884 return NULL;
885
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200886 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
887 kfree(nmode);
888 return NULL;
889 }
890
Dave Airlief453ba02008-11-07 14:05:41 -0800891 return nmode;
892}
893EXPORT_SYMBOL(drm_mode_create);
894
895/**
896 * drm_mode_destroy - remove a mode
897 * @dev: DRM device
898 * @mode: mode to remove
899 *
Dave Airlief453ba02008-11-07 14:05:41 -0800900 * Free @mode's unique identifier, then free it.
901 */
902void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
903{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200904 if (!mode)
905 return;
906
Dave Airlief453ba02008-11-07 14:05:41 -0800907 drm_mode_object_put(dev, &mode->base);
908
909 kfree(mode);
910}
911EXPORT_SYMBOL(drm_mode_destroy);
912
913static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
914{
915 struct drm_property *edid;
916 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800917
918 /*
919 * Standard properties (apply to all connectors)
920 */
921 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
922 DRM_MODE_PROP_IMMUTABLE,
923 "EDID", 0);
924 dev->mode_config.edid_property = edid;
925
Sascha Hauer4a67d392012-02-06 10:58:17 +0100926 dpms = drm_property_create_enum(dev, 0,
927 "DPMS", drm_dpms_enum_list,
928 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800929 dev->mode_config.dpms_property = dpms;
930
931 return 0;
932}
933
934/**
935 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
936 * @dev: DRM device
937 *
938 * Called by a driver the first time a DVI-I connector is made.
939 */
940int drm_mode_create_dvi_i_properties(struct drm_device *dev)
941{
942 struct drm_property *dvi_i_selector;
943 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800944
945 if (dev->mode_config.dvi_i_select_subconnector_property)
946 return 0;
947
948 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100949 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800950 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100951 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800952 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800953 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
954
Sascha Hauer4a67d392012-02-06 10:58:17 +0100955 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800956 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100957 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800958 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800959 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
960
961 return 0;
962}
963EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
964
965/**
966 * drm_create_tv_properties - create TV specific connector properties
967 * @dev: DRM device
968 * @num_modes: number of different TV formats (modes) supported
969 * @modes: array of pointers to strings containing name of each format
970 *
971 * Called by a driver's TV initialization routine, this function creates
972 * the TV specific connector properties for a given device. Caller is
973 * responsible for allocating a list of format names and passing them to
974 * this routine.
975 */
976int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
977 char *modes[])
978{
979 struct drm_property *tv_selector;
980 struct drm_property *tv_subconnector;
981 int i;
982
983 if (dev->mode_config.tv_select_subconnector_property)
984 return 0;
985
986 /*
987 * Basic connector properties
988 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100989 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800990 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100991 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800992 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800993 dev->mode_config.tv_select_subconnector_property = tv_selector;
994
995 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100996 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
997 "subconnector",
998 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800999 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001000 dev->mode_config.tv_subconnector_property = tv_subconnector;
1001
1002 /*
1003 * Other, TV specific properties: margins & TV modes.
1004 */
1005 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001006 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001007
1008 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001009 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001010
1011 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001012 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001013
1014 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001015 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -08001016
1017 dev->mode_config.tv_mode_property =
1018 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1019 "mode", num_modes);
1020 for (i = 0; i < num_modes; i++)
1021 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1022 i, modes[i]);
1023
Francisco Jerezb6b79022009-08-02 04:19:20 +02001024 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001025 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001026
1027 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001028 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001029
1030 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001031 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001032
Francisco Jereza75f0232009-08-12 02:30:10 +02001033 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001034 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001035
1036 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001037 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001038
1039 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001040 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001041
Dave Airlief453ba02008-11-07 14:05:41 -08001042 return 0;
1043}
1044EXPORT_SYMBOL(drm_mode_create_tv_properties);
1045
1046/**
1047 * drm_mode_create_scaling_mode_property - create scaling mode property
1048 * @dev: DRM device
1049 *
1050 * Called by a driver the first time it's needed, must be attached to desired
1051 * connectors.
1052 */
1053int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1054{
1055 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001056
1057 if (dev->mode_config.scaling_mode_property)
1058 return 0;
1059
1060 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001061 drm_property_create_enum(dev, 0, "scaling mode",
1062 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001063 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001064
1065 dev->mode_config.scaling_mode_property = scaling_mode;
1066
1067 return 0;
1068}
1069EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1070
1071/**
1072 * drm_mode_create_dithering_property - create dithering property
1073 * @dev: DRM device
1074 *
1075 * Called by a driver the first time it's needed, must be attached to desired
1076 * connectors.
1077 */
1078int drm_mode_create_dithering_property(struct drm_device *dev)
1079{
1080 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001081
1082 if (dev->mode_config.dithering_mode_property)
1083 return 0;
1084
1085 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001086 drm_property_create_enum(dev, 0, "dithering",
1087 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001088 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001089 dev->mode_config.dithering_mode_property = dithering_mode;
1090
1091 return 0;
1092}
1093EXPORT_SYMBOL(drm_mode_create_dithering_property);
1094
1095/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001096 * drm_mode_create_dirty_property - create dirty property
1097 * @dev: DRM device
1098 *
1099 * Called by a driver the first time it's needed, must be attached to desired
1100 * connectors.
1101 */
1102int drm_mode_create_dirty_info_property(struct drm_device *dev)
1103{
1104 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001105
1106 if (dev->mode_config.dirty_info_property)
1107 return 0;
1108
1109 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001110 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001111 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001112 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001113 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001114 dev->mode_config.dirty_info_property = dirty_info;
1115
1116 return 0;
1117}
1118EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1119
1120/**
Dave Airlief453ba02008-11-07 14:05:41 -08001121 * drm_mode_config_init - initialize DRM mode_configuration structure
1122 * @dev: DRM device
1123 *
Dave Airlief453ba02008-11-07 14:05:41 -08001124 * Initialize @dev's mode_config structure, used for tracking the graphics
1125 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001126 *
1127 * Since this initializes the modeset locks, no locking is possible. Which is no
1128 * problem, since this should happen single threaded at init time. It is the
1129 * driver's problem to ensure this guarantee.
1130 *
Dave Airlief453ba02008-11-07 14:05:41 -08001131 */
1132void drm_mode_config_init(struct drm_device *dev)
1133{
1134 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001135 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001136 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001137 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001138 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1139 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1140 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1141 INIT_LIST_HEAD(&dev->mode_config.property_list);
1142 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001143 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001144 idr_init(&dev->mode_config.crtc_idr);
1145
Daniel Vetter84849902012-12-02 00:28:11 +01001146 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001147 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001148 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001149
1150 /* Just to be sure */
1151 dev->mode_config.num_fb = 0;
1152 dev->mode_config.num_connector = 0;
1153 dev->mode_config.num_crtc = 0;
1154 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001155}
1156EXPORT_SYMBOL(drm_mode_config_init);
1157
1158int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1159{
1160 uint32_t total_objects = 0;
1161
1162 total_objects += dev->mode_config.num_crtc;
1163 total_objects += dev->mode_config.num_connector;
1164 total_objects += dev->mode_config.num_encoder;
1165
Dave Airlief453ba02008-11-07 14:05:41 -08001166 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1167 if (!group->id_list)
1168 return -ENOMEM;
1169
1170 group->num_crtcs = 0;
1171 group->num_connectors = 0;
1172 group->num_encoders = 0;
1173 return 0;
1174}
1175
1176int drm_mode_group_init_legacy_group(struct drm_device *dev,
1177 struct drm_mode_group *group)
1178{
1179 struct drm_crtc *crtc;
1180 struct drm_encoder *encoder;
1181 struct drm_connector *connector;
1182 int ret;
1183
1184 if ((ret = drm_mode_group_init(dev, group)))
1185 return ret;
1186
1187 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1188 group->id_list[group->num_crtcs++] = crtc->base.id;
1189
1190 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1191 group->id_list[group->num_crtcs + group->num_encoders++] =
1192 encoder->base.id;
1193
1194 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1195 group->id_list[group->num_crtcs + group->num_encoders +
1196 group->num_connectors++] = connector->base.id;
1197
1198 return 0;
1199}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001200EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001201
1202/**
1203 * drm_mode_config_cleanup - free up DRM mode_config info
1204 * @dev: DRM device
1205 *
Dave Airlief453ba02008-11-07 14:05:41 -08001206 * Free up all the connectors and CRTCs associated with this DRM device, then
1207 * free up the framebuffers and associated buffer objects.
1208 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001209 * Note that since this /should/ happen single-threaded at driver/device
1210 * teardown time, no locking is required. It's the driver's job to ensure that
1211 * this guarantee actually holds true.
1212 *
Dave Airlief453ba02008-11-07 14:05:41 -08001213 * FIXME: cleanup any dangling user buffer objects too
1214 */
1215void drm_mode_config_cleanup(struct drm_device *dev)
1216{
1217 struct drm_connector *connector, *ot;
1218 struct drm_crtc *crtc, *ct;
1219 struct drm_encoder *encoder, *enct;
1220 struct drm_framebuffer *fb, *fbt;
1221 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001222 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001223
1224 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1225 head) {
1226 encoder->funcs->destroy(encoder);
1227 }
1228
1229 list_for_each_entry_safe(connector, ot,
1230 &dev->mode_config.connector_list, head) {
1231 connector->funcs->destroy(connector);
1232 }
1233
1234 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1235 head) {
1236 drm_property_destroy(dev, property);
1237 }
1238
Daniel Vetter2b677e82012-12-10 21:16:05 +01001239 /*
1240 * Single-threaded teardown context, so it's not required to grab the
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001241 * fb_lock to protect against concurrent fb_list access. Contrary, it
Daniel Vetter2b677e82012-12-10 21:16:05 +01001242 * would actually deadlock with the drm_framebuffer_cleanup function.
1243 *
1244 * Also, if there are any framebuffers left, that's a driver leak now,
1245 * so politely WARN about this.
1246 */
1247 WARN_ON(!list_empty(&dev->mode_config.fb_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001248 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001249 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001250 }
1251
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001252 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1253 head) {
1254 plane->funcs->destroy(plane);
1255 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001256
Chris Wilson31840092012-09-17 09:38:03 +00001257 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1258 crtc->funcs->destroy(crtc);
1259 }
1260
Sascha Hauer59ce0622012-02-01 11:38:20 +01001261 idr_remove_all(&dev->mode_config.crtc_idr);
1262 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001263}
1264EXPORT_SYMBOL(drm_mode_config_cleanup);
1265
Dave Airlief453ba02008-11-07 14:05:41 -08001266/**
1267 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1268 * @out: drm_mode_modeinfo struct to return to the user
1269 * @in: drm_display_mode to use
1270 *
Dave Airlief453ba02008-11-07 14:05:41 -08001271 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1272 * the user.
1273 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001274static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1275 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001276{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001277 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1278 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1279 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1280 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1281 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1282 "timing values too large for mode info\n");
1283
Dave Airlief453ba02008-11-07 14:05:41 -08001284 out->clock = in->clock;
1285 out->hdisplay = in->hdisplay;
1286 out->hsync_start = in->hsync_start;
1287 out->hsync_end = in->hsync_end;
1288 out->htotal = in->htotal;
1289 out->hskew = in->hskew;
1290 out->vdisplay = in->vdisplay;
1291 out->vsync_start = in->vsync_start;
1292 out->vsync_end = in->vsync_end;
1293 out->vtotal = in->vtotal;
1294 out->vscan = in->vscan;
1295 out->vrefresh = in->vrefresh;
1296 out->flags = in->flags;
1297 out->type = in->type;
1298 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1299 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1300}
1301
1302/**
1303 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1304 * @out: drm_display_mode to return to the user
1305 * @in: drm_mode_modeinfo to use
1306 *
Dave Airlief453ba02008-11-07 14:05:41 -08001307 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1308 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001309 *
1310 * RETURNS:
1311 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001312 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001313static int drm_crtc_convert_umode(struct drm_display_mode *out,
1314 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001315{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001316 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1317 return -ERANGE;
1318
Dave Airlief453ba02008-11-07 14:05:41 -08001319 out->clock = in->clock;
1320 out->hdisplay = in->hdisplay;
1321 out->hsync_start = in->hsync_start;
1322 out->hsync_end = in->hsync_end;
1323 out->htotal = in->htotal;
1324 out->hskew = in->hskew;
1325 out->vdisplay = in->vdisplay;
1326 out->vsync_start = in->vsync_start;
1327 out->vsync_end = in->vsync_end;
1328 out->vtotal = in->vtotal;
1329 out->vscan = in->vscan;
1330 out->vrefresh = in->vrefresh;
1331 out->flags = in->flags;
1332 out->type = in->type;
1333 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1334 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001335
1336 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001337}
1338
1339/**
1340 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001341 * @dev: drm device for the ioctl
1342 * @data: data pointer for the ioctl
1343 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001344 *
Dave Airlief453ba02008-11-07 14:05:41 -08001345 * Construct a set of configuration description structures and return
1346 * them to the user, including CRTC, connector and framebuffer configuration.
1347 *
1348 * Called by the user via ioctl.
1349 *
1350 * RETURNS:
1351 * Zero on success, errno on failure.
1352 */
1353int drm_mode_getresources(struct drm_device *dev, void *data,
1354 struct drm_file *file_priv)
1355{
1356 struct drm_mode_card_res *card_res = data;
1357 struct list_head *lh;
1358 struct drm_framebuffer *fb;
1359 struct drm_connector *connector;
1360 struct drm_crtc *crtc;
1361 struct drm_encoder *encoder;
1362 int ret = 0;
1363 int connector_count = 0;
1364 int crtc_count = 0;
1365 int fb_count = 0;
1366 int encoder_count = 0;
1367 int copied = 0, i;
1368 uint32_t __user *fb_id;
1369 uint32_t __user *crtc_id;
1370 uint32_t __user *connector_id;
1371 uint32_t __user *encoder_id;
1372 struct drm_mode_group *mode_group;
1373
Dave Airliefb3b06c2011-02-08 13:55:21 +10001374 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1375 return -EINVAL;
1376
Dave Airlief453ba02008-11-07 14:05:41 -08001377
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001378 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001379 /*
1380 * For the non-control nodes we need to limit the list of resources
1381 * by IDs in the group list for this node
1382 */
1383 list_for_each(lh, &file_priv->fbs)
1384 fb_count++;
1385
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001386 /* handle this in 4 parts */
1387 /* FBs */
1388 if (card_res->count_fbs >= fb_count) {
1389 copied = 0;
1390 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1391 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1392 if (put_user(fb->base.id, fb_id + copied)) {
1393 mutex_unlock(&file_priv->fbs_lock);
1394 return -EFAULT;
1395 }
1396 copied++;
1397 }
1398 }
1399 card_res->count_fbs = fb_count;
1400 mutex_unlock(&file_priv->fbs_lock);
1401
1402 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001403 mode_group = &file_priv->master->minor->mode_group;
1404 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1405
1406 list_for_each(lh, &dev->mode_config.crtc_list)
1407 crtc_count++;
1408
1409 list_for_each(lh, &dev->mode_config.connector_list)
1410 connector_count++;
1411
1412 list_for_each(lh, &dev->mode_config.encoder_list)
1413 encoder_count++;
1414 } else {
1415
1416 crtc_count = mode_group->num_crtcs;
1417 connector_count = mode_group->num_connectors;
1418 encoder_count = mode_group->num_encoders;
1419 }
1420
1421 card_res->max_height = dev->mode_config.max_height;
1422 card_res->min_height = dev->mode_config.min_height;
1423 card_res->max_width = dev->mode_config.max_width;
1424 card_res->min_width = dev->mode_config.min_width;
1425
Dave Airlief453ba02008-11-07 14:05:41 -08001426 /* CRTCs */
1427 if (card_res->count_crtcs >= crtc_count) {
1428 copied = 0;
1429 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1430 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1431 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1432 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001433 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001434 if (put_user(crtc->base.id, crtc_id + copied)) {
1435 ret = -EFAULT;
1436 goto out;
1437 }
1438 copied++;
1439 }
1440 } else {
1441 for (i = 0; i < mode_group->num_crtcs; i++) {
1442 if (put_user(mode_group->id_list[i],
1443 crtc_id + copied)) {
1444 ret = -EFAULT;
1445 goto out;
1446 }
1447 copied++;
1448 }
1449 }
1450 }
1451 card_res->count_crtcs = crtc_count;
1452
1453 /* Encoders */
1454 if (card_res->count_encoders >= encoder_count) {
1455 copied = 0;
1456 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1457 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1458 list_for_each_entry(encoder,
1459 &dev->mode_config.encoder_list,
1460 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001461 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1462 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001463 if (put_user(encoder->base.id, encoder_id +
1464 copied)) {
1465 ret = -EFAULT;
1466 goto out;
1467 }
1468 copied++;
1469 }
1470 } else {
1471 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1472 if (put_user(mode_group->id_list[i],
1473 encoder_id + copied)) {
1474 ret = -EFAULT;
1475 goto out;
1476 }
1477 copied++;
1478 }
1479
1480 }
1481 }
1482 card_res->count_encoders = encoder_count;
1483
1484 /* Connectors */
1485 if (card_res->count_connectors >= connector_count) {
1486 copied = 0;
1487 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1488 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1489 list_for_each_entry(connector,
1490 &dev->mode_config.connector_list,
1491 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001492 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1493 connector->base.id,
1494 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001495 if (put_user(connector->base.id,
1496 connector_id + copied)) {
1497 ret = -EFAULT;
1498 goto out;
1499 }
1500 copied++;
1501 }
1502 } else {
1503 int start = mode_group->num_crtcs +
1504 mode_group->num_encoders;
1505 for (i = start; i < start + mode_group->num_connectors; i++) {
1506 if (put_user(mode_group->id_list[i],
1507 connector_id + copied)) {
1508 ret = -EFAULT;
1509 goto out;
1510 }
1511 copied++;
1512 }
1513 }
1514 }
1515 card_res->count_connectors = connector_count;
1516
Jerome Glisse94401062010-07-15 15:43:25 -04001517 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001518 card_res->count_connectors, card_res->count_encoders);
1519
1520out:
Daniel Vetter84849902012-12-02 00:28:11 +01001521 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001522 return ret;
1523}
1524
1525/**
1526 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001527 * @dev: drm device for the ioctl
1528 * @data: data pointer for the ioctl
1529 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001530 *
Dave Airlief453ba02008-11-07 14:05:41 -08001531 * Construct a CRTC configuration structure to return to the user.
1532 *
1533 * Called by the user via ioctl.
1534 *
1535 * RETURNS:
1536 * Zero on success, errno on failure.
1537 */
1538int drm_mode_getcrtc(struct drm_device *dev,
1539 void *data, struct drm_file *file_priv)
1540{
1541 struct drm_mode_crtc *crtc_resp = data;
1542 struct drm_crtc *crtc;
1543 struct drm_mode_object *obj;
1544 int ret = 0;
1545
Dave Airliefb3b06c2011-02-08 13:55:21 +10001546 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1547 return -EINVAL;
1548
Daniel Vetter84849902012-12-02 00:28:11 +01001549 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001550
1551 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1552 DRM_MODE_OBJECT_CRTC);
1553 if (!obj) {
1554 ret = -EINVAL;
1555 goto out;
1556 }
1557 crtc = obj_to_crtc(obj);
1558
1559 crtc_resp->x = crtc->x;
1560 crtc_resp->y = crtc->y;
1561 crtc_resp->gamma_size = crtc->gamma_size;
1562 if (crtc->fb)
1563 crtc_resp->fb_id = crtc->fb->base.id;
1564 else
1565 crtc_resp->fb_id = 0;
1566
1567 if (crtc->enabled) {
1568
1569 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1570 crtc_resp->mode_valid = 1;
1571
1572 } else {
1573 crtc_resp->mode_valid = 0;
1574 }
1575
1576out:
Daniel Vetter84849902012-12-02 00:28:11 +01001577 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001578 return ret;
1579}
1580
1581/**
1582 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001583 * @dev: drm device for the ioctl
1584 * @data: data pointer for the ioctl
1585 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001586 *
Dave Airlief453ba02008-11-07 14:05:41 -08001587 * Construct a connector configuration structure to return to the user.
1588 *
1589 * Called by the user via ioctl.
1590 *
1591 * RETURNS:
1592 * Zero on success, errno on failure.
1593 */
1594int drm_mode_getconnector(struct drm_device *dev, void *data,
1595 struct drm_file *file_priv)
1596{
1597 struct drm_mode_get_connector *out_resp = data;
1598 struct drm_mode_object *obj;
1599 struct drm_connector *connector;
1600 struct drm_display_mode *mode;
1601 int mode_count = 0;
1602 int props_count = 0;
1603 int encoders_count = 0;
1604 int ret = 0;
1605 int copied = 0;
1606 int i;
1607 struct drm_mode_modeinfo u_mode;
1608 struct drm_mode_modeinfo __user *mode_ptr;
1609 uint32_t __user *prop_ptr;
1610 uint64_t __user *prop_values;
1611 uint32_t __user *encoder_ptr;
1612
Dave Airliefb3b06c2011-02-08 13:55:21 +10001613 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1614 return -EINVAL;
1615
Dave Airlief453ba02008-11-07 14:05:41 -08001616 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1617
Jerome Glisse94401062010-07-15 15:43:25 -04001618 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001619
Daniel Vetter84849902012-12-02 00:28:11 +01001620 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001621
1622 obj = drm_mode_object_find(dev, out_resp->connector_id,
1623 DRM_MODE_OBJECT_CONNECTOR);
1624 if (!obj) {
1625 ret = -EINVAL;
1626 goto out;
1627 }
1628 connector = obj_to_connector(obj);
1629
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001630 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001631
1632 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1633 if (connector->encoder_ids[i] != 0) {
1634 encoders_count++;
1635 }
1636 }
1637
1638 if (out_resp->count_modes == 0) {
1639 connector->funcs->fill_modes(connector,
1640 dev->mode_config.max_width,
1641 dev->mode_config.max_height);
1642 }
1643
1644 /* delayed so we get modes regardless of pre-fill_modes state */
1645 list_for_each_entry(mode, &connector->modes, head)
1646 mode_count++;
1647
1648 out_resp->connector_id = connector->base.id;
1649 out_resp->connector_type = connector->connector_type;
1650 out_resp->connector_type_id = connector->connector_type_id;
1651 out_resp->mm_width = connector->display_info.width_mm;
1652 out_resp->mm_height = connector->display_info.height_mm;
1653 out_resp->subpixel = connector->display_info.subpixel_order;
1654 out_resp->connection = connector->status;
1655 if (connector->encoder)
1656 out_resp->encoder_id = connector->encoder->base.id;
1657 else
1658 out_resp->encoder_id = 0;
1659
1660 /*
1661 * This ioctl is called twice, once to determine how much space is
1662 * needed, and the 2nd time to fill it.
1663 */
1664 if ((out_resp->count_modes >= mode_count) && mode_count) {
1665 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001666 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001667 list_for_each_entry(mode, &connector->modes, head) {
1668 drm_crtc_convert_to_umode(&u_mode, mode);
1669 if (copy_to_user(mode_ptr + copied,
1670 &u_mode, sizeof(u_mode))) {
1671 ret = -EFAULT;
1672 goto out;
1673 }
1674 copied++;
1675 }
1676 }
1677 out_resp->count_modes = mode_count;
1678
1679 if ((out_resp->count_props >= props_count) && props_count) {
1680 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001681 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1682 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001683 for (i = 0; i < connector->properties.count; i++) {
1684 if (put_user(connector->properties.ids[i],
1685 prop_ptr + copied)) {
1686 ret = -EFAULT;
1687 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001688 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001689
1690 if (put_user(connector->properties.values[i],
1691 prop_values + copied)) {
1692 ret = -EFAULT;
1693 goto out;
1694 }
1695 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001696 }
1697 }
1698 out_resp->count_props = props_count;
1699
1700 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1701 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001702 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001703 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1704 if (connector->encoder_ids[i] != 0) {
1705 if (put_user(connector->encoder_ids[i],
1706 encoder_ptr + copied)) {
1707 ret = -EFAULT;
1708 goto out;
1709 }
1710 copied++;
1711 }
1712 }
1713 }
1714 out_resp->count_encoders = encoders_count;
1715
1716out:
Daniel Vetter84849902012-12-02 00:28:11 +01001717 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001718 return ret;
1719}
1720
1721int drm_mode_getencoder(struct drm_device *dev, void *data,
1722 struct drm_file *file_priv)
1723{
1724 struct drm_mode_get_encoder *enc_resp = data;
1725 struct drm_mode_object *obj;
1726 struct drm_encoder *encoder;
1727 int ret = 0;
1728
Dave Airliefb3b06c2011-02-08 13:55:21 +10001729 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1730 return -EINVAL;
1731
Daniel Vetter84849902012-12-02 00:28:11 +01001732 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001733 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1734 DRM_MODE_OBJECT_ENCODER);
1735 if (!obj) {
1736 ret = -EINVAL;
1737 goto out;
1738 }
1739 encoder = obj_to_encoder(obj);
1740
1741 if (encoder->crtc)
1742 enc_resp->crtc_id = encoder->crtc->base.id;
1743 else
1744 enc_resp->crtc_id = 0;
1745 enc_resp->encoder_type = encoder->encoder_type;
1746 enc_resp->encoder_id = encoder->base.id;
1747 enc_resp->possible_crtcs = encoder->possible_crtcs;
1748 enc_resp->possible_clones = encoder->possible_clones;
1749
1750out:
Daniel Vetter84849902012-12-02 00:28:11 +01001751 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001752 return ret;
1753}
1754
1755/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001756 * drm_mode_getplane_res - get plane info
1757 * @dev: DRM device
1758 * @data: ioctl data
1759 * @file_priv: DRM file info
1760 *
1761 * Return an plane count and set of IDs.
1762 */
1763int drm_mode_getplane_res(struct drm_device *dev, void *data,
1764 struct drm_file *file_priv)
1765{
1766 struct drm_mode_get_plane_res *plane_resp = data;
1767 struct drm_mode_config *config;
1768 struct drm_plane *plane;
1769 uint32_t __user *plane_ptr;
1770 int copied = 0, ret = 0;
1771
1772 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1773 return -EINVAL;
1774
Daniel Vetter84849902012-12-02 00:28:11 +01001775 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001776 config = &dev->mode_config;
1777
1778 /*
1779 * This ioctl is called twice, once to determine how much space is
1780 * needed, and the 2nd time to fill it.
1781 */
1782 if (config->num_plane &&
1783 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001784 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001785
1786 list_for_each_entry(plane, &config->plane_list, head) {
1787 if (put_user(plane->base.id, plane_ptr + copied)) {
1788 ret = -EFAULT;
1789 goto out;
1790 }
1791 copied++;
1792 }
1793 }
1794 plane_resp->count_planes = config->num_plane;
1795
1796out:
Daniel Vetter84849902012-12-02 00:28:11 +01001797 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001798 return ret;
1799}
1800
1801/**
1802 * drm_mode_getplane - get plane info
1803 * @dev: DRM device
1804 * @data: ioctl data
1805 * @file_priv: DRM file info
1806 *
1807 * Return plane info, including formats supported, gamma size, any
1808 * current fb, etc.
1809 */
1810int drm_mode_getplane(struct drm_device *dev, void *data,
1811 struct drm_file *file_priv)
1812{
1813 struct drm_mode_get_plane *plane_resp = data;
1814 struct drm_mode_object *obj;
1815 struct drm_plane *plane;
1816 uint32_t __user *format_ptr;
1817 int ret = 0;
1818
1819 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1820 return -EINVAL;
1821
Daniel Vetter84849902012-12-02 00:28:11 +01001822 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001823 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1824 DRM_MODE_OBJECT_PLANE);
1825 if (!obj) {
1826 ret = -ENOENT;
1827 goto out;
1828 }
1829 plane = obj_to_plane(obj);
1830
1831 if (plane->crtc)
1832 plane_resp->crtc_id = plane->crtc->base.id;
1833 else
1834 plane_resp->crtc_id = 0;
1835
1836 if (plane->fb)
1837 plane_resp->fb_id = plane->fb->base.id;
1838 else
1839 plane_resp->fb_id = 0;
1840
1841 plane_resp->plane_id = plane->base.id;
1842 plane_resp->possible_crtcs = plane->possible_crtcs;
1843 plane_resp->gamma_size = plane->gamma_size;
1844
1845 /*
1846 * This ioctl is called twice, once to determine how much space is
1847 * needed, and the 2nd time to fill it.
1848 */
1849 if (plane->format_count &&
1850 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001851 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001852 if (copy_to_user(format_ptr,
1853 plane->format_types,
1854 sizeof(uint32_t) * plane->format_count)) {
1855 ret = -EFAULT;
1856 goto out;
1857 }
1858 }
1859 plane_resp->count_format_types = plane->format_count;
1860
1861out:
Daniel Vetter84849902012-12-02 00:28:11 +01001862 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001863 return ret;
1864}
1865
1866/**
1867 * drm_mode_setplane - set up or tear down an plane
1868 * @dev: DRM device
1869 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001870 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001871 *
1872 * Set plane info, including placement, fb, scaling, and other factors.
1873 * Or pass a NULL fb to disable.
1874 */
1875int drm_mode_setplane(struct drm_device *dev, void *data,
1876 struct drm_file *file_priv)
1877{
1878 struct drm_mode_set_plane *plane_req = data;
1879 struct drm_mode_object *obj;
1880 struct drm_plane *plane;
1881 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001882 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001883 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001884 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001885 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001886
1887 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1888 return -EINVAL;
1889
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001890 /*
1891 * First, find the plane, crtc, and fb objects. If not available,
1892 * we don't bother to call the driver.
1893 */
1894 obj = drm_mode_object_find(dev, plane_req->plane_id,
1895 DRM_MODE_OBJECT_PLANE);
1896 if (!obj) {
1897 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1898 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001899 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001900 }
1901 plane = obj_to_plane(obj);
1902
1903 /* No fb means shut it down */
1904 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001905 drm_modeset_lock_all(dev);
1906 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001907 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001908 plane->crtc = NULL;
1909 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001910 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001911 goto out;
1912 }
1913
1914 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1915 DRM_MODE_OBJECT_CRTC);
1916 if (!obj) {
1917 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1918 plane_req->crtc_id);
1919 ret = -ENOENT;
1920 goto out;
1921 }
1922 crtc = obj_to_crtc(obj);
1923
Daniel Vetter786b99e2012-12-02 21:53:40 +01001924 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1925 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001926 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1927 plane_req->fb_id);
1928 ret = -ENOENT;
1929 goto out;
1930 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001931
Ville Syrjälä62443be2011-12-20 00:06:47 +02001932 /* Check whether this plane supports the fb pixel format. */
1933 for (i = 0; i < plane->format_count; i++)
1934 if (fb->pixel_format == plane->format_types[i])
1935 break;
1936 if (i == plane->format_count) {
1937 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1938 ret = -EINVAL;
1939 goto out;
1940 }
1941
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001942 fb_width = fb->width << 16;
1943 fb_height = fb->height << 16;
1944
1945 /* Make sure source coordinates are inside the fb. */
1946 if (plane_req->src_w > fb_width ||
1947 plane_req->src_x > fb_width - plane_req->src_w ||
1948 plane_req->src_h > fb_height ||
1949 plane_req->src_y > fb_height - plane_req->src_h) {
1950 DRM_DEBUG_KMS("Invalid source coordinates "
1951 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1952 plane_req->src_w >> 16,
1953 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1954 plane_req->src_h >> 16,
1955 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1956 plane_req->src_x >> 16,
1957 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1958 plane_req->src_y >> 16,
1959 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1960 ret = -ENOSPC;
1961 goto out;
1962 }
1963
Ville Syrjälä687a0402011-12-20 00:06:45 +02001964 /* Give drivers some help against integer overflows */
1965 if (plane_req->crtc_w > INT_MAX ||
1966 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1967 plane_req->crtc_h > INT_MAX ||
1968 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1969 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1970 plane_req->crtc_w, plane_req->crtc_h,
1971 plane_req->crtc_x, plane_req->crtc_y);
1972 ret = -ERANGE;
1973 goto out;
1974 }
1975
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001976 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001977 ret = plane->funcs->update_plane(plane, crtc, fb,
1978 plane_req->crtc_x, plane_req->crtc_y,
1979 plane_req->crtc_w, plane_req->crtc_h,
1980 plane_req->src_x, plane_req->src_y,
1981 plane_req->src_w, plane_req->src_h);
1982 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001983 old_fb = plane->fb;
1984 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001985 plane->crtc = crtc;
1986 plane->fb = fb;
1987 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001988 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001989
1990out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001991 if (fb)
1992 drm_framebuffer_unreference(fb);
1993 if (old_fb)
1994 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001995
1996 return ret;
1997}
1998
1999/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002000 * drm_mode_set_config_internal - helper to call ->set_config
2001 * @set: modeset config to set
2002 *
2003 * This is a little helper to wrap internal calls to the ->set_config driver
2004 * interface. The only thing it adds is correct refcounting dance.
2005 */
2006int drm_mode_set_config_internal(struct drm_mode_set *set)
2007{
2008 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002009 struct drm_framebuffer *fb, *old_fb;
2010 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002011
Daniel Vetterb0d12322012-12-11 01:07:12 +01002012 old_fb = crtc->fb;
2013 fb = set->fb;
2014
2015 ret = crtc->funcs->set_config(set);
2016 if (ret == 0) {
2017 if (old_fb)
2018 drm_framebuffer_unreference(old_fb);
2019 if (fb)
2020 drm_framebuffer_reference(fb);
2021 }
2022
2023 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002024}
2025EXPORT_SYMBOL(drm_mode_set_config_internal);
2026
2027/**
Dave Airlief453ba02008-11-07 14:05:41 -08002028 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002029 * @dev: drm device for the ioctl
2030 * @data: data pointer for the ioctl
2031 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002032 *
Dave Airlief453ba02008-11-07 14:05:41 -08002033 * Build a new CRTC configuration based on user request.
2034 *
2035 * Called by the user via ioctl.
2036 *
2037 * RETURNS:
2038 * Zero on success, errno on failure.
2039 */
2040int drm_mode_setcrtc(struct drm_device *dev, void *data,
2041 struct drm_file *file_priv)
2042{
2043 struct drm_mode_config *config = &dev->mode_config;
2044 struct drm_mode_crtc *crtc_req = data;
2045 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002046 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002047 struct drm_connector **connector_set = NULL, *connector;
2048 struct drm_framebuffer *fb = NULL;
2049 struct drm_display_mode *mode = NULL;
2050 struct drm_mode_set set;
2051 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002052 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002053 int i;
2054
Dave Airliefb3b06c2011-02-08 13:55:21 +10002055 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2056 return -EINVAL;
2057
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002058 /* For some reason crtc x/y offsets are signed internally. */
2059 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2060 return -ERANGE;
2061
Daniel Vetter84849902012-12-02 00:28:11 +01002062 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002063 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2064 DRM_MODE_OBJECT_CRTC);
2065 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002066 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002067 ret = -EINVAL;
2068 goto out;
2069 }
2070 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002071 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002072
2073 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002074 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002075 /* If we have a mode we need a framebuffer. */
2076 /* If we pass -1, set the mode with the currently bound fb */
2077 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002078 if (!crtc->fb) {
2079 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2080 ret = -EINVAL;
2081 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002082 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002083 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002084 /* Make refcounting symmetric with the lookup path. */
2085 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002086 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002087 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2088 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002089 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2090 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002091 ret = -EINVAL;
2092 goto out;
2093 }
Dave Airlief453ba02008-11-07 14:05:41 -08002094 }
2095
2096 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002097 if (!mode) {
2098 ret = -ENOMEM;
2099 goto out;
2100 }
2101
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002102 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2103 if (ret) {
2104 DRM_DEBUG_KMS("Invalid mode\n");
2105 goto out;
2106 }
2107
Dave Airlief453ba02008-11-07 14:05:41 -08002108 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002109
Rob Clark7c80e122012-09-04 16:35:56 +00002110 hdisplay = mode->hdisplay;
2111 vdisplay = mode->vdisplay;
2112
2113 if (crtc->invert_dimensions)
2114 swap(hdisplay, vdisplay);
2115
2116 if (hdisplay > fb->width ||
2117 vdisplay > fb->height ||
2118 crtc_req->x > fb->width - hdisplay ||
2119 crtc_req->y > fb->height - vdisplay) {
2120 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2121 fb->width, fb->height,
2122 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2123 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002124 ret = -ENOSPC;
2125 goto out;
2126 }
Dave Airlief453ba02008-11-07 14:05:41 -08002127 }
2128
2129 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002130 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002131 ret = -EINVAL;
2132 goto out;
2133 }
2134
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002135 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002136 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002137 crtc_req->count_connectors);
2138 ret = -EINVAL;
2139 goto out;
2140 }
2141
2142 if (crtc_req->count_connectors > 0) {
2143 u32 out_id;
2144
2145 /* Avoid unbounded kernel memory allocation */
2146 if (crtc_req->count_connectors > config->num_connector) {
2147 ret = -EINVAL;
2148 goto out;
2149 }
2150
2151 connector_set = kmalloc(crtc_req->count_connectors *
2152 sizeof(struct drm_connector *),
2153 GFP_KERNEL);
2154 if (!connector_set) {
2155 ret = -ENOMEM;
2156 goto out;
2157 }
2158
2159 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002160 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002161 if (get_user(out_id, &set_connectors_ptr[i])) {
2162 ret = -EFAULT;
2163 goto out;
2164 }
2165
2166 obj = drm_mode_object_find(dev, out_id,
2167 DRM_MODE_OBJECT_CONNECTOR);
2168 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002169 DRM_DEBUG_KMS("Connector id %d unknown\n",
2170 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002171 ret = -EINVAL;
2172 goto out;
2173 }
2174 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002175 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2176 connector->base.id,
2177 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002178
2179 connector_set[i] = connector;
2180 }
2181 }
2182
2183 set.crtc = crtc;
2184 set.x = crtc_req->x;
2185 set.y = crtc_req->y;
2186 set.mode = mode;
2187 set.connectors = connector_set;
2188 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002189 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002190 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002191
2192out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002193 if (fb)
2194 drm_framebuffer_unreference(fb);
2195
Dave Airlief453ba02008-11-07 14:05:41 -08002196 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002197 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002198 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002199 return ret;
2200}
2201
2202int drm_mode_cursor_ioctl(struct drm_device *dev,
2203 void *data, struct drm_file *file_priv)
2204{
2205 struct drm_mode_cursor *req = data;
2206 struct drm_mode_object *obj;
2207 struct drm_crtc *crtc;
2208 int ret = 0;
2209
Dave Airliefb3b06c2011-02-08 13:55:21 +10002210 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2211 return -EINVAL;
2212
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002213 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002214 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002215
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002216 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002217 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002218 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002219 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002220 }
2221 crtc = obj_to_crtc(obj);
2222
Daniel Vetterdac35662012-12-02 15:24:10 +01002223 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002224 if (req->flags & DRM_MODE_CURSOR_BO) {
2225 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002226 ret = -ENXIO;
2227 goto out;
2228 }
2229 /* Turns off the cursor if handle is 0 */
2230 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2231 req->width, req->height);
2232 }
2233
2234 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2235 if (crtc->funcs->cursor_move) {
2236 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2237 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002238 ret = -EFAULT;
2239 goto out;
2240 }
2241 }
2242out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002243 mutex_unlock(&crtc->mutex);
2244
Dave Airlief453ba02008-11-07 14:05:41 -08002245 return ret;
2246}
2247
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002248/* Original addfb only supported RGB formats, so figure out which one */
2249uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2250{
2251 uint32_t fmt;
2252
2253 switch (bpp) {
2254 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002255 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002256 break;
2257 case 16:
2258 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002259 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002260 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002261 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002262 break;
2263 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002264 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002265 break;
2266 case 32:
2267 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002268 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002269 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002270 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002271 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002272 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002273 break;
2274 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002275 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2276 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002277 break;
2278 }
2279
2280 return fmt;
2281}
2282EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2283
Dave Airlief453ba02008-11-07 14:05:41 -08002284/**
2285 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002286 * @dev: drm device for the ioctl
2287 * @data: data pointer for the ioctl
2288 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002289 *
Dave Airlief453ba02008-11-07 14:05:41 -08002290 * Add a new FB to the specified CRTC, given a user request.
2291 *
2292 * Called by the user via ioctl.
2293 *
2294 * RETURNS:
2295 * Zero on success, errno on failure.
2296 */
2297int drm_mode_addfb(struct drm_device *dev,
2298 void *data, struct drm_file *file_priv)
2299{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002300 struct drm_mode_fb_cmd *or = data;
2301 struct drm_mode_fb_cmd2 r = {};
2302 struct drm_mode_config *config = &dev->mode_config;
2303 struct drm_framebuffer *fb;
2304 int ret = 0;
2305
2306 /* Use new struct with format internally */
2307 r.fb_id = or->fb_id;
2308 r.width = or->width;
2309 r.height = or->height;
2310 r.pitches[0] = or->pitch;
2311 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2312 r.handles[0] = or->handle;
2313
2314 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2315 return -EINVAL;
2316
Jesse Barnesacb4b992011-11-07 12:03:23 -08002317 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002318 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002319
2320 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002321 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002322
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002323 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2324 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002325 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002326 drm_modeset_unlock_all(dev);
2327 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002328 }
2329
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002330 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002331 or->fb_id = fb->base.id;
2332 list_add(&fb->filp_head, &file_priv->fbs);
2333 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002334 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002335
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002336 return ret;
2337}
2338
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002339static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002340{
2341 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2342
2343 switch (format) {
2344 case DRM_FORMAT_C8:
2345 case DRM_FORMAT_RGB332:
2346 case DRM_FORMAT_BGR233:
2347 case DRM_FORMAT_XRGB4444:
2348 case DRM_FORMAT_XBGR4444:
2349 case DRM_FORMAT_RGBX4444:
2350 case DRM_FORMAT_BGRX4444:
2351 case DRM_FORMAT_ARGB4444:
2352 case DRM_FORMAT_ABGR4444:
2353 case DRM_FORMAT_RGBA4444:
2354 case DRM_FORMAT_BGRA4444:
2355 case DRM_FORMAT_XRGB1555:
2356 case DRM_FORMAT_XBGR1555:
2357 case DRM_FORMAT_RGBX5551:
2358 case DRM_FORMAT_BGRX5551:
2359 case DRM_FORMAT_ARGB1555:
2360 case DRM_FORMAT_ABGR1555:
2361 case DRM_FORMAT_RGBA5551:
2362 case DRM_FORMAT_BGRA5551:
2363 case DRM_FORMAT_RGB565:
2364 case DRM_FORMAT_BGR565:
2365 case DRM_FORMAT_RGB888:
2366 case DRM_FORMAT_BGR888:
2367 case DRM_FORMAT_XRGB8888:
2368 case DRM_FORMAT_XBGR8888:
2369 case DRM_FORMAT_RGBX8888:
2370 case DRM_FORMAT_BGRX8888:
2371 case DRM_FORMAT_ARGB8888:
2372 case DRM_FORMAT_ABGR8888:
2373 case DRM_FORMAT_RGBA8888:
2374 case DRM_FORMAT_BGRA8888:
2375 case DRM_FORMAT_XRGB2101010:
2376 case DRM_FORMAT_XBGR2101010:
2377 case DRM_FORMAT_RGBX1010102:
2378 case DRM_FORMAT_BGRX1010102:
2379 case DRM_FORMAT_ARGB2101010:
2380 case DRM_FORMAT_ABGR2101010:
2381 case DRM_FORMAT_RGBA1010102:
2382 case DRM_FORMAT_BGRA1010102:
2383 case DRM_FORMAT_YUYV:
2384 case DRM_FORMAT_YVYU:
2385 case DRM_FORMAT_UYVY:
2386 case DRM_FORMAT_VYUY:
2387 case DRM_FORMAT_AYUV:
2388 case DRM_FORMAT_NV12:
2389 case DRM_FORMAT_NV21:
2390 case DRM_FORMAT_NV16:
2391 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002392 case DRM_FORMAT_NV24:
2393 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002394 case DRM_FORMAT_YUV410:
2395 case DRM_FORMAT_YVU410:
2396 case DRM_FORMAT_YUV411:
2397 case DRM_FORMAT_YVU411:
2398 case DRM_FORMAT_YUV420:
2399 case DRM_FORMAT_YVU420:
2400 case DRM_FORMAT_YUV422:
2401 case DRM_FORMAT_YVU422:
2402 case DRM_FORMAT_YUV444:
2403 case DRM_FORMAT_YVU444:
2404 return 0;
2405 default:
2406 return -EINVAL;
2407 }
2408}
2409
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002410static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002411{
2412 int ret, hsub, vsub, num_planes, i;
2413
2414 ret = format_check(r);
2415 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002416 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002417 return ret;
2418 }
2419
2420 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2421 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2422 num_planes = drm_format_num_planes(r->pixel_format);
2423
2424 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002425 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002426 return -EINVAL;
2427 }
2428
2429 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002430 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002431 return -EINVAL;
2432 }
2433
2434 for (i = 0; i < num_planes; i++) {
2435 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002436 unsigned int height = r->height / (i != 0 ? vsub : 1);
2437 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002438
2439 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002440 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002441 return -EINVAL;
2442 }
2443
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002444 if ((uint64_t) width * cpp > UINT_MAX)
2445 return -ERANGE;
2446
2447 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2448 return -ERANGE;
2449
2450 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002451 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002452 return -EINVAL;
2453 }
2454 }
2455
2456 return 0;
2457}
2458
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002459/**
2460 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002461 * @dev: drm device for the ioctl
2462 * @data: data pointer for the ioctl
2463 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002464 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002465 * Add a new FB to the specified CRTC, given a user request with format.
2466 *
2467 * Called by the user via ioctl.
2468 *
2469 * RETURNS:
2470 * Zero on success, errno on failure.
2471 */
2472int drm_mode_addfb2(struct drm_device *dev,
2473 void *data, struct drm_file *file_priv)
2474{
2475 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002476 struct drm_mode_config *config = &dev->mode_config;
2477 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002478 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002479
Dave Airliefb3b06c2011-02-08 13:55:21 +10002480 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2481 return -EINVAL;
2482
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002483 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2484 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2485 return -EINVAL;
2486 }
2487
Dave Airlief453ba02008-11-07 14:05:41 -08002488 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002489 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002490 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002491 return -EINVAL;
2492 }
2493 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002494 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002495 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002496 return -EINVAL;
2497 }
2498
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002499 ret = framebuffer_check(r);
2500 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002501 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002502
Dave Airlief453ba02008-11-07 14:05:41 -08002503 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002504 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002505 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002506 drm_modeset_unlock_all(dev);
2507 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002508 }
2509
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002510 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002511 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002512 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002513 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002514 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002515
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002516
Dave Airlief453ba02008-11-07 14:05:41 -08002517 return ret;
2518}
2519
2520/**
2521 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002522 * @dev: drm device for the ioctl
2523 * @data: data pointer for the ioctl
2524 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002525 *
Dave Airlief453ba02008-11-07 14:05:41 -08002526 * Remove the FB specified by the user.
2527 *
2528 * Called by the user via ioctl.
2529 *
2530 * RETURNS:
2531 * Zero on success, errno on failure.
2532 */
2533int drm_mode_rmfb(struct drm_device *dev,
2534 void *data, struct drm_file *file_priv)
2535{
Dave Airlief453ba02008-11-07 14:05:41 -08002536 struct drm_framebuffer *fb = NULL;
2537 struct drm_framebuffer *fbl = NULL;
2538 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002539 int found = 0;
2540
Dave Airliefb3b06c2011-02-08 13:55:21 +10002541 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2542 return -EINVAL;
2543
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002544 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002545 mutex_lock(&dev->mode_config.fb_lock);
2546 fb = __drm_framebuffer_lookup(dev, *id);
2547 if (!fb)
2548 goto fail_lookup;
2549
Dave Airlief453ba02008-11-07 14:05:41 -08002550 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2551 if (fb == fbl)
2552 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002553 if (!found)
2554 goto fail_lookup;
2555
2556 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2557 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002558
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002559 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002560 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002561 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002562
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002563 drm_framebuffer_remove(fb);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002564
Daniel Vetter2b677e82012-12-10 21:16:05 +01002565 return 0;
2566
2567fail_lookup:
2568 mutex_unlock(&dev->mode_config.fb_lock);
2569 mutex_unlock(&file_priv->fbs_lock);
2570
2571 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002572}
2573
2574/**
2575 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002576 * @dev: drm device for the ioctl
2577 * @data: data pointer for the ioctl
2578 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002579 *
Dave Airlief453ba02008-11-07 14:05:41 -08002580 * Lookup the FB given its ID and return info about it.
2581 *
2582 * Called by the user via ioctl.
2583 *
2584 * RETURNS:
2585 * Zero on success, errno on failure.
2586 */
2587int drm_mode_getfb(struct drm_device *dev,
2588 void *data, struct drm_file *file_priv)
2589{
2590 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002591 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002592 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002593
Dave Airliefb3b06c2011-02-08 13:55:21 +10002594 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2595 return -EINVAL;
2596
Daniel Vetter786b99e2012-12-02 21:53:40 +01002597 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002598 if (!fb)
2599 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002600
2601 r->height = fb->height;
2602 r->width = fb->width;
2603 r->depth = fb->depth;
2604 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002605 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002606 if (fb->funcs->create_handle)
2607 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2608 else
2609 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002610
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002611 drm_framebuffer_unreference(fb);
2612
Dave Airlief453ba02008-11-07 14:05:41 -08002613 return ret;
2614}
2615
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002616int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2617 void *data, struct drm_file *file_priv)
2618{
2619 struct drm_clip_rect __user *clips_ptr;
2620 struct drm_clip_rect *clips = NULL;
2621 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002622 struct drm_framebuffer *fb;
2623 unsigned flags;
2624 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002625 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002626
Dave Airliefb3b06c2011-02-08 13:55:21 +10002627 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2628 return -EINVAL;
2629
Daniel Vetter786b99e2012-12-02 21:53:40 +01002630 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002631 if (!fb)
2632 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002633
2634 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002635 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002636
2637 if (!num_clips != !clips_ptr) {
2638 ret = -EINVAL;
2639 goto out_err1;
2640 }
2641
2642 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2643
2644 /* If userspace annotates copy, clips must come in pairs */
2645 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2646 ret = -EINVAL;
2647 goto out_err1;
2648 }
2649
2650 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002651 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2652 ret = -EINVAL;
2653 goto out_err1;
2654 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002655 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2656 if (!clips) {
2657 ret = -ENOMEM;
2658 goto out_err1;
2659 }
2660
2661 ret = copy_from_user(clips, clips_ptr,
2662 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002663 if (ret) {
2664 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002665 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002666 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002667 }
2668
2669 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002670 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002671 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2672 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002673 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002674 } else {
2675 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002676 }
2677
2678out_err2:
2679 kfree(clips);
2680out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002681 drm_framebuffer_unreference(fb);
2682
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002683 return ret;
2684}
2685
2686
Dave Airlief453ba02008-11-07 14:05:41 -08002687/**
2688 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002689 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002690 *
Dave Airlief453ba02008-11-07 14:05:41 -08002691 * Destroy all the FBs associated with @filp.
2692 *
2693 * Called by the user via ioctl.
2694 *
2695 * RETURNS:
2696 * Zero on success, errno on failure.
2697 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002698void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002699{
Dave Airlief453ba02008-11-07 14:05:41 -08002700 struct drm_device *dev = priv->minor->dev;
2701 struct drm_framebuffer *fb, *tfb;
2702
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002703 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002704 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002705
2706 mutex_lock(&dev->mode_config.fb_lock);
2707 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2708 __drm_framebuffer_unregister(dev, fb);
2709 mutex_unlock(&dev->mode_config.fb_lock);
2710
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002711 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002712
2713 /* This will also drop the fpriv->fbs reference. */
Rob Clarkf7eff602012-09-05 21:48:38 +00002714 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002715 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002716 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002717}
2718
2719/**
2720 * drm_mode_attachmode - add a mode to the user mode list
2721 * @dev: DRM device
2722 * @connector: connector to add the mode to
2723 * @mode: mode to add
2724 *
2725 * Add @mode to @connector's user mode list.
2726 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002727static void drm_mode_attachmode(struct drm_device *dev,
2728 struct drm_connector *connector,
2729 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002730{
Dave Airlief453ba02008-11-07 14:05:41 -08002731 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002732}
2733
2734int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002735 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002736{
2737 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002738 int ret = 0;
2739 struct drm_display_mode *dup_mode, *next;
2740 LIST_HEAD(list);
2741
Dave Airlief453ba02008-11-07 14:05:41 -08002742 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2743 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002744 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002745 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002746 dup_mode = drm_mode_duplicate(dev, mode);
2747 if (!dup_mode) {
2748 ret = -ENOMEM;
2749 goto out;
2750 }
2751 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002752 }
2753 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002754
2755 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2756 if (!connector->encoder)
2757 continue;
2758 if (connector->encoder->crtc == crtc)
2759 list_move_tail(list.next, &connector->user_modes);
2760 }
2761
2762 WARN_ON(!list_empty(&list));
2763
2764 out:
2765 list_for_each_entry_safe(dup_mode, next, &list, head)
2766 drm_mode_destroy(dev, dup_mode);
2767
2768 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002769}
2770EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2771
2772static int drm_mode_detachmode(struct drm_device *dev,
2773 struct drm_connector *connector,
2774 struct drm_display_mode *mode)
2775{
2776 int found = 0;
2777 int ret = 0;
2778 struct drm_display_mode *match_mode, *t;
2779
2780 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2781 if (drm_mode_equal(match_mode, mode)) {
2782 list_del(&match_mode->head);
2783 drm_mode_destroy(dev, match_mode);
2784 found = 1;
2785 break;
2786 }
2787 }
2788
2789 if (!found)
2790 ret = -EINVAL;
2791
2792 return ret;
2793}
2794
2795int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2796{
2797 struct drm_connector *connector;
2798
2799 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2800 drm_mode_detachmode(dev, connector, mode);
2801 }
2802 return 0;
2803}
2804EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2805
2806/**
2807 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002808 * @dev: drm device for the ioctl
2809 * @data: data pointer for the ioctl
2810 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002811 *
2812 * This attaches a user specified mode to an connector.
2813 * Called by the user via ioctl.
2814 *
2815 * RETURNS:
2816 * Zero on success, errno on failure.
2817 */
2818int drm_mode_attachmode_ioctl(struct drm_device *dev,
2819 void *data, struct drm_file *file_priv)
2820{
2821 struct drm_mode_mode_cmd *mode_cmd = data;
2822 struct drm_connector *connector;
2823 struct drm_display_mode *mode;
2824 struct drm_mode_object *obj;
2825 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002826 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002827
Dave Airliefb3b06c2011-02-08 13:55:21 +10002828 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2829 return -EINVAL;
2830
Daniel Vetter84849902012-12-02 00:28:11 +01002831 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002832
2833 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2834 if (!obj) {
2835 ret = -EINVAL;
2836 goto out;
2837 }
2838 connector = obj_to_connector(obj);
2839
2840 mode = drm_mode_create(dev);
2841 if (!mode) {
2842 ret = -ENOMEM;
2843 goto out;
2844 }
2845
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002846 ret = drm_crtc_convert_umode(mode, umode);
2847 if (ret) {
2848 DRM_DEBUG_KMS("Invalid mode\n");
2849 drm_mode_destroy(dev, mode);
2850 goto out;
2851 }
Dave Airlief453ba02008-11-07 14:05:41 -08002852
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002853 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002854out:
Daniel Vetter84849902012-12-02 00:28:11 +01002855 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002856 return ret;
2857}
2858
2859
2860/**
2861 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002862 * @dev: drm device for the ioctl
2863 * @data: data pointer for the ioctl
2864 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002865 *
2866 * Called by the user via ioctl.
2867 *
2868 * RETURNS:
2869 * Zero on success, errno on failure.
2870 */
2871int drm_mode_detachmode_ioctl(struct drm_device *dev,
2872 void *data, struct drm_file *file_priv)
2873{
2874 struct drm_mode_object *obj;
2875 struct drm_mode_mode_cmd *mode_cmd = data;
2876 struct drm_connector *connector;
2877 struct drm_display_mode mode;
2878 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002879 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002880
Dave Airliefb3b06c2011-02-08 13:55:21 +10002881 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2882 return -EINVAL;
2883
Daniel Vetter84849902012-12-02 00:28:11 +01002884 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002885
2886 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2887 if (!obj) {
2888 ret = -EINVAL;
2889 goto out;
2890 }
2891 connector = obj_to_connector(obj);
2892
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002893 ret = drm_crtc_convert_umode(&mode, umode);
2894 if (ret) {
2895 DRM_DEBUG_KMS("Invalid mode\n");
2896 goto out;
2897 }
2898
Dave Airlief453ba02008-11-07 14:05:41 -08002899 ret = drm_mode_detachmode(dev, connector, &mode);
2900out:
Daniel Vetter84849902012-12-02 00:28:11 +01002901 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002902 return ret;
2903}
2904
2905struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2906 const char *name, int num_values)
2907{
2908 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002909 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002910
2911 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2912 if (!property)
2913 return NULL;
2914
2915 if (num_values) {
2916 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2917 if (!property->values)
2918 goto fail;
2919 }
2920
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002921 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2922 if (ret)
2923 goto fail;
2924
Dave Airlief453ba02008-11-07 14:05:41 -08002925 property->flags = flags;
2926 property->num_values = num_values;
2927 INIT_LIST_HEAD(&property->enum_blob_list);
2928
Vinson Lee471dd2e2011-11-10 11:55:40 -08002929 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002930 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002931 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2932 }
Dave Airlief453ba02008-11-07 14:05:41 -08002933
2934 list_add_tail(&property->head, &dev->mode_config.property_list);
2935 return property;
2936fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002937 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002938 kfree(property);
2939 return NULL;
2940}
2941EXPORT_SYMBOL(drm_property_create);
2942
Sascha Hauer4a67d392012-02-06 10:58:17 +01002943struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2944 const char *name,
2945 const struct drm_prop_enum_list *props,
2946 int num_values)
2947{
2948 struct drm_property *property;
2949 int i, ret;
2950
2951 flags |= DRM_MODE_PROP_ENUM;
2952
2953 property = drm_property_create(dev, flags, name, num_values);
2954 if (!property)
2955 return NULL;
2956
2957 for (i = 0; i < num_values; i++) {
2958 ret = drm_property_add_enum(property, i,
2959 props[i].type,
2960 props[i].name);
2961 if (ret) {
2962 drm_property_destroy(dev, property);
2963 return NULL;
2964 }
2965 }
2966
2967 return property;
2968}
2969EXPORT_SYMBOL(drm_property_create_enum);
2970
Rob Clark49e27542012-05-17 02:23:26 -06002971struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2972 int flags, const char *name,
2973 const struct drm_prop_enum_list *props,
2974 int num_values)
2975{
2976 struct drm_property *property;
2977 int i, ret;
2978
2979 flags |= DRM_MODE_PROP_BITMASK;
2980
2981 property = drm_property_create(dev, flags, name, num_values);
2982 if (!property)
2983 return NULL;
2984
2985 for (i = 0; i < num_values; i++) {
2986 ret = drm_property_add_enum(property, i,
2987 props[i].type,
2988 props[i].name);
2989 if (ret) {
2990 drm_property_destroy(dev, property);
2991 return NULL;
2992 }
2993 }
2994
2995 return property;
2996}
2997EXPORT_SYMBOL(drm_property_create_bitmask);
2998
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002999struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3000 const char *name,
3001 uint64_t min, uint64_t max)
3002{
3003 struct drm_property *property;
3004
3005 flags |= DRM_MODE_PROP_RANGE;
3006
3007 property = drm_property_create(dev, flags, name, 2);
3008 if (!property)
3009 return NULL;
3010
3011 property->values[0] = min;
3012 property->values[1] = max;
3013
3014 return property;
3015}
3016EXPORT_SYMBOL(drm_property_create_range);
3017
Dave Airlief453ba02008-11-07 14:05:41 -08003018int drm_property_add_enum(struct drm_property *property, int index,
3019 uint64_t value, const char *name)
3020{
3021 struct drm_property_enum *prop_enum;
3022
Rob Clark49e27542012-05-17 02:23:26 -06003023 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3024 return -EINVAL;
3025
3026 /*
3027 * Bitmask enum properties have the additional constraint of values
3028 * from 0 to 63
3029 */
3030 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003031 return -EINVAL;
3032
3033 if (!list_empty(&property->enum_blob_list)) {
3034 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3035 if (prop_enum->value == value) {
3036 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3037 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3038 return 0;
3039 }
3040 }
3041 }
3042
3043 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3044 if (!prop_enum)
3045 return -ENOMEM;
3046
3047 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3048 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3049 prop_enum->value = value;
3050
3051 property->values[index] = value;
3052 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3053 return 0;
3054}
3055EXPORT_SYMBOL(drm_property_add_enum);
3056
3057void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3058{
3059 struct drm_property_enum *prop_enum, *pt;
3060
3061 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3062 list_del(&prop_enum->head);
3063 kfree(prop_enum);
3064 }
3065
3066 if (property->num_values)
3067 kfree(property->values);
3068 drm_mode_object_put(dev, &property->base);
3069 list_del(&property->head);
3070 kfree(property);
3071}
3072EXPORT_SYMBOL(drm_property_destroy);
3073
Paulo Zanonic5431882012-05-15 18:09:02 -03003074void drm_object_attach_property(struct drm_mode_object *obj,
3075 struct drm_property *property,
3076 uint64_t init_val)
3077{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003078 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003079
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003080 if (count == DRM_OBJECT_MAX_PROPERTY) {
3081 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3082 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3083 "you see this message on the same object type.\n",
3084 obj->type);
3085 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003086 }
3087
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003088 obj->properties->ids[count] = property->base.id;
3089 obj->properties->values[count] = init_val;
3090 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003091}
3092EXPORT_SYMBOL(drm_object_attach_property);
3093
3094int drm_object_property_set_value(struct drm_mode_object *obj,
3095 struct drm_property *property, uint64_t val)
3096{
3097 int i;
3098
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003099 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003100 if (obj->properties->ids[i] == property->base.id) {
3101 obj->properties->values[i] = val;
3102 return 0;
3103 }
3104 }
3105
3106 return -EINVAL;
3107}
3108EXPORT_SYMBOL(drm_object_property_set_value);
3109
3110int drm_object_property_get_value(struct drm_mode_object *obj,
3111 struct drm_property *property, uint64_t *val)
3112{
3113 int i;
3114
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003115 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003116 if (obj->properties->ids[i] == property->base.id) {
3117 *val = obj->properties->values[i];
3118 return 0;
3119 }
3120 }
3121
3122 return -EINVAL;
3123}
3124EXPORT_SYMBOL(drm_object_property_get_value);
3125
Dave Airlief453ba02008-11-07 14:05:41 -08003126int drm_mode_getproperty_ioctl(struct drm_device *dev,
3127 void *data, struct drm_file *file_priv)
3128{
3129 struct drm_mode_object *obj;
3130 struct drm_mode_get_property *out_resp = data;
3131 struct drm_property *property;
3132 int enum_count = 0;
3133 int blob_count = 0;
3134 int value_count = 0;
3135 int ret = 0, i;
3136 int copied;
3137 struct drm_property_enum *prop_enum;
3138 struct drm_mode_property_enum __user *enum_ptr;
3139 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003140 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003141 uint64_t __user *values_ptr;
3142 uint32_t __user *blob_length_ptr;
3143
Dave Airliefb3b06c2011-02-08 13:55:21 +10003144 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3145 return -EINVAL;
3146
Daniel Vetter84849902012-12-02 00:28:11 +01003147 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003148 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3149 if (!obj) {
3150 ret = -EINVAL;
3151 goto done;
3152 }
3153 property = obj_to_property(obj);
3154
Rob Clark49e27542012-05-17 02:23:26 -06003155 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003156 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3157 enum_count++;
3158 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3159 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3160 blob_count++;
3161 }
3162
3163 value_count = property->num_values;
3164
3165 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3166 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3167 out_resp->flags = property->flags;
3168
3169 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003170 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003171 for (i = 0; i < value_count; i++) {
3172 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3173 ret = -EFAULT;
3174 goto done;
3175 }
3176 }
3177 }
3178 out_resp->count_values = value_count;
3179
Rob Clark49e27542012-05-17 02:23:26 -06003180 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003181 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3182 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003183 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003184 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3185
3186 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3187 ret = -EFAULT;
3188 goto done;
3189 }
3190
3191 if (copy_to_user(&enum_ptr[copied].name,
3192 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3193 ret = -EFAULT;
3194 goto done;
3195 }
3196 copied++;
3197 }
3198 }
3199 out_resp->count_enum_blobs = enum_count;
3200 }
3201
3202 if (property->flags & DRM_MODE_PROP_BLOB) {
3203 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3204 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003205 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3206 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003207
3208 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3209 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3210 ret = -EFAULT;
3211 goto done;
3212 }
3213
3214 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3215 ret = -EFAULT;
3216 goto done;
3217 }
3218
3219 copied++;
3220 }
3221 }
3222 out_resp->count_enum_blobs = blob_count;
3223 }
3224done:
Daniel Vetter84849902012-12-02 00:28:11 +01003225 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003226 return ret;
3227}
3228
3229static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3230 void *data)
3231{
3232 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003233 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003234
3235 if (!length || !data)
3236 return NULL;
3237
3238 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3239 if (!blob)
3240 return NULL;
3241
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003242 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3243 if (ret) {
3244 kfree(blob);
3245 return NULL;
3246 }
3247
Dave Airlief453ba02008-11-07 14:05:41 -08003248 blob->length = length;
3249
3250 memcpy(blob->data, data, length);
3251
Dave Airlief453ba02008-11-07 14:05:41 -08003252 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3253 return blob;
3254}
3255
3256static void drm_property_destroy_blob(struct drm_device *dev,
3257 struct drm_property_blob *blob)
3258{
3259 drm_mode_object_put(dev, &blob->base);
3260 list_del(&blob->head);
3261 kfree(blob);
3262}
3263
3264int drm_mode_getblob_ioctl(struct drm_device *dev,
3265 void *data, struct drm_file *file_priv)
3266{
3267 struct drm_mode_object *obj;
3268 struct drm_mode_get_blob *out_resp = data;
3269 struct drm_property_blob *blob;
3270 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003271 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003272
Dave Airliefb3b06c2011-02-08 13:55:21 +10003273 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3274 return -EINVAL;
3275
Daniel Vetter84849902012-12-02 00:28:11 +01003276 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003277 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3278 if (!obj) {
3279 ret = -EINVAL;
3280 goto done;
3281 }
3282 blob = obj_to_blob(obj);
3283
3284 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003285 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003286 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3287 ret = -EFAULT;
3288 goto done;
3289 }
3290 }
3291 out_resp->length = blob->length;
3292
3293done:
Daniel Vetter84849902012-12-02 00:28:11 +01003294 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003295 return ret;
3296}
3297
3298int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3299 struct edid *edid)
3300{
3301 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003302 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003303
3304 if (connector->edid_blob_ptr)
3305 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3306
3307 /* Delete edid, when there is none. */
3308 if (!edid) {
3309 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003310 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003311 return ret;
3312 }
3313
Adam Jackson7466f4c2010-03-29 21:43:23 +00003314 size = EDID_LENGTH * (1 + edid->extensions);
3315 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3316 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003317 if (!connector->edid_blob_ptr)
3318 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003319
Rob Clark58495562012-10-11 20:50:56 -05003320 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003321 dev->mode_config.edid_property,
3322 connector->edid_blob_ptr->base.id);
3323
3324 return ret;
3325}
3326EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3327
Paulo Zanoni26a34812012-05-15 18:08:59 -03003328static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003329 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003330{
3331 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3332 return false;
3333 if (property->flags & DRM_MODE_PROP_RANGE) {
3334 if (value < property->values[0] || value > property->values[1])
3335 return false;
3336 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003337 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3338 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003339 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003340 for (i = 0; i < property->num_values; i++)
3341 valid_mask |= (1ULL << property->values[i]);
3342 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003343 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3344 /* Only the driver knows */
3345 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003346 } else {
3347 int i;
3348 for (i = 0; i < property->num_values; i++)
3349 if (property->values[i] == value)
3350 return true;
3351 return false;
3352 }
3353}
3354
Dave Airlief453ba02008-11-07 14:05:41 -08003355int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3356 void *data, struct drm_file *file_priv)
3357{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003358 struct drm_mode_connector_set_property *conn_set_prop = data;
3359 struct drm_mode_obj_set_property obj_set_prop = {
3360 .value = conn_set_prop->value,
3361 .prop_id = conn_set_prop->prop_id,
3362 .obj_id = conn_set_prop->connector_id,
3363 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3364 };
Dave Airlief453ba02008-11-07 14:05:41 -08003365
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003366 /* It does all the locking and checking we need */
3367 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003368}
3369
Paulo Zanonic5431882012-05-15 18:09:02 -03003370static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3371 struct drm_property *property,
3372 uint64_t value)
3373{
3374 int ret = -EINVAL;
3375 struct drm_connector *connector = obj_to_connector(obj);
3376
3377 /* Do DPMS ourselves */
3378 if (property == connector->dev->mode_config.dpms_property) {
3379 if (connector->funcs->dpms)
3380 (*connector->funcs->dpms)(connector, (int)value);
3381 ret = 0;
3382 } else if (connector->funcs->set_property)
3383 ret = connector->funcs->set_property(connector, property, value);
3384
3385 /* store the property value if successful */
3386 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003387 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003388 return ret;
3389}
3390
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003391static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3392 struct drm_property *property,
3393 uint64_t value)
3394{
3395 int ret = -EINVAL;
3396 struct drm_crtc *crtc = obj_to_crtc(obj);
3397
3398 if (crtc->funcs->set_property)
3399 ret = crtc->funcs->set_property(crtc, property, value);
3400 if (!ret)
3401 drm_object_property_set_value(obj, property, value);
3402
3403 return ret;
3404}
3405
Rob Clark4d939142012-05-17 02:23:27 -06003406static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3407 struct drm_property *property,
3408 uint64_t value)
3409{
3410 int ret = -EINVAL;
3411 struct drm_plane *plane = obj_to_plane(obj);
3412
3413 if (plane->funcs->set_property)
3414 ret = plane->funcs->set_property(plane, property, value);
3415 if (!ret)
3416 drm_object_property_set_value(obj, property, value);
3417
3418 return ret;
3419}
3420
Paulo Zanonic5431882012-05-15 18:09:02 -03003421int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3422 struct drm_file *file_priv)
3423{
3424 struct drm_mode_obj_get_properties *arg = data;
3425 struct drm_mode_object *obj;
3426 int ret = 0;
3427 int i;
3428 int copied = 0;
3429 int props_count = 0;
3430 uint32_t __user *props_ptr;
3431 uint64_t __user *prop_values_ptr;
3432
3433 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3434 return -EINVAL;
3435
Daniel Vetter84849902012-12-02 00:28:11 +01003436 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003437
3438 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3439 if (!obj) {
3440 ret = -EINVAL;
3441 goto out;
3442 }
3443 if (!obj->properties) {
3444 ret = -EINVAL;
3445 goto out;
3446 }
3447
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003448 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003449
3450 /* This ioctl is called twice, once to determine how much space is
3451 * needed, and the 2nd time to fill it. */
3452 if ((arg->count_props >= props_count) && props_count) {
3453 copied = 0;
3454 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3455 prop_values_ptr = (uint64_t __user *)(unsigned long)
3456 (arg->prop_values_ptr);
3457 for (i = 0; i < props_count; i++) {
3458 if (put_user(obj->properties->ids[i],
3459 props_ptr + copied)) {
3460 ret = -EFAULT;
3461 goto out;
3462 }
3463 if (put_user(obj->properties->values[i],
3464 prop_values_ptr + copied)) {
3465 ret = -EFAULT;
3466 goto out;
3467 }
3468 copied++;
3469 }
3470 }
3471 arg->count_props = props_count;
3472out:
Daniel Vetter84849902012-12-02 00:28:11 +01003473 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003474 return ret;
3475}
3476
3477int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3478 struct drm_file *file_priv)
3479{
3480 struct drm_mode_obj_set_property *arg = data;
3481 struct drm_mode_object *arg_obj;
3482 struct drm_mode_object *prop_obj;
3483 struct drm_property *property;
3484 int ret = -EINVAL;
3485 int i;
3486
3487 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3488 return -EINVAL;
3489
Daniel Vetter84849902012-12-02 00:28:11 +01003490 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003491
3492 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3493 if (!arg_obj)
3494 goto out;
3495 if (!arg_obj->properties)
3496 goto out;
3497
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003498 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003499 if (arg_obj->properties->ids[i] == arg->prop_id)
3500 break;
3501
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003502 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003503 goto out;
3504
3505 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3506 DRM_MODE_OBJECT_PROPERTY);
3507 if (!prop_obj)
3508 goto out;
3509 property = obj_to_property(prop_obj);
3510
3511 if (!drm_property_change_is_valid(property, arg->value))
3512 goto out;
3513
3514 switch (arg_obj->type) {
3515 case DRM_MODE_OBJECT_CONNECTOR:
3516 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3517 arg->value);
3518 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003519 case DRM_MODE_OBJECT_CRTC:
3520 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3521 break;
Rob Clark4d939142012-05-17 02:23:27 -06003522 case DRM_MODE_OBJECT_PLANE:
3523 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3524 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003525 }
3526
3527out:
Daniel Vetter84849902012-12-02 00:28:11 +01003528 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003529 return ret;
3530}
3531
Dave Airlief453ba02008-11-07 14:05:41 -08003532int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3533 struct drm_encoder *encoder)
3534{
3535 int i;
3536
3537 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3538 if (connector->encoder_ids[i] == 0) {
3539 connector->encoder_ids[i] = encoder->base.id;
3540 return 0;
3541 }
3542 }
3543 return -ENOMEM;
3544}
3545EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3546
3547void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3548 struct drm_encoder *encoder)
3549{
3550 int i;
3551 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3552 if (connector->encoder_ids[i] == encoder->base.id) {
3553 connector->encoder_ids[i] = 0;
3554 if (connector->encoder == encoder)
3555 connector->encoder = NULL;
3556 break;
3557 }
3558 }
3559}
3560EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3561
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003562int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003563 int gamma_size)
3564{
3565 crtc->gamma_size = gamma_size;
3566
3567 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3568 if (!crtc->gamma_store) {
3569 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003570 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003571 }
3572
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003573 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003574}
3575EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3576
3577int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3578 void *data, struct drm_file *file_priv)
3579{
3580 struct drm_mode_crtc_lut *crtc_lut = data;
3581 struct drm_mode_object *obj;
3582 struct drm_crtc *crtc;
3583 void *r_base, *g_base, *b_base;
3584 int size;
3585 int ret = 0;
3586
Dave Airliefb3b06c2011-02-08 13:55:21 +10003587 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3588 return -EINVAL;
3589
Daniel Vetter84849902012-12-02 00:28:11 +01003590 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003591 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3592 if (!obj) {
3593 ret = -EINVAL;
3594 goto out;
3595 }
3596 crtc = obj_to_crtc(obj);
3597
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003598 if (crtc->funcs->gamma_set == NULL) {
3599 ret = -ENOSYS;
3600 goto out;
3601 }
3602
Dave Airlief453ba02008-11-07 14:05:41 -08003603 /* memcpy into gamma store */
3604 if (crtc_lut->gamma_size != crtc->gamma_size) {
3605 ret = -EINVAL;
3606 goto out;
3607 }
3608
3609 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3610 r_base = crtc->gamma_store;
3611 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3612 ret = -EFAULT;
3613 goto out;
3614 }
3615
3616 g_base = r_base + size;
3617 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3618 ret = -EFAULT;
3619 goto out;
3620 }
3621
3622 b_base = g_base + size;
3623 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3624 ret = -EFAULT;
3625 goto out;
3626 }
3627
James Simmons72034252010-08-03 01:33:19 +01003628 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003629
3630out:
Daniel Vetter84849902012-12-02 00:28:11 +01003631 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003632 return ret;
3633
3634}
3635
3636int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3637 void *data, struct drm_file *file_priv)
3638{
3639 struct drm_mode_crtc_lut *crtc_lut = data;
3640 struct drm_mode_object *obj;
3641 struct drm_crtc *crtc;
3642 void *r_base, *g_base, *b_base;
3643 int size;
3644 int ret = 0;
3645
Dave Airliefb3b06c2011-02-08 13:55:21 +10003646 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3647 return -EINVAL;
3648
Daniel Vetter84849902012-12-02 00:28:11 +01003649 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003650 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3651 if (!obj) {
3652 ret = -EINVAL;
3653 goto out;
3654 }
3655 crtc = obj_to_crtc(obj);
3656
3657 /* memcpy into gamma store */
3658 if (crtc_lut->gamma_size != crtc->gamma_size) {
3659 ret = -EINVAL;
3660 goto out;
3661 }
3662
3663 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3664 r_base = crtc->gamma_store;
3665 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3666 ret = -EFAULT;
3667 goto out;
3668 }
3669
3670 g_base = r_base + size;
3671 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3672 ret = -EFAULT;
3673 goto out;
3674 }
3675
3676 b_base = g_base + size;
3677 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3678 ret = -EFAULT;
3679 goto out;
3680 }
3681out:
Daniel Vetter84849902012-12-02 00:28:11 +01003682 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003683 return ret;
3684}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003685
3686int drm_mode_page_flip_ioctl(struct drm_device *dev,
3687 void *data, struct drm_file *file_priv)
3688{
3689 struct drm_mode_crtc_page_flip *page_flip = data;
3690 struct drm_mode_object *obj;
3691 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003692 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003693 struct drm_pending_vblank_event *e = NULL;
3694 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003695 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003696 int ret = -EINVAL;
3697
3698 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3699 page_flip->reserved != 0)
3700 return -EINVAL;
3701
Daniel Vetter84849902012-12-02 00:28:11 +01003702 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003703 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3704 if (!obj)
3705 goto out;
3706 crtc = obj_to_crtc(obj);
3707
Chris Wilson90c1efd2010-07-17 20:23:26 +01003708 if (crtc->fb == NULL) {
3709 /* The framebuffer is currently unbound, presumably
3710 * due to a hotplug event, that userspace has not
3711 * yet discovered.
3712 */
3713 ret = -EBUSY;
3714 goto out;
3715 }
3716
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003717 if (crtc->funcs->page_flip == NULL)
3718 goto out;
3719
Daniel Vetter786b99e2012-12-02 21:53:40 +01003720 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3721 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003722 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003723
Rob Clark7c80e122012-09-04 16:35:56 +00003724 hdisplay = crtc->mode.hdisplay;
3725 vdisplay = crtc->mode.vdisplay;
3726
3727 if (crtc->invert_dimensions)
3728 swap(hdisplay, vdisplay);
3729
3730 if (hdisplay > fb->width ||
3731 vdisplay > fb->height ||
3732 crtc->x > fb->width - hdisplay ||
3733 crtc->y > fb->height - vdisplay) {
3734 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3735 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3736 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003737 ret = -ENOSPC;
3738 goto out;
3739 }
3740
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003741 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3742 ret = -ENOMEM;
3743 spin_lock_irqsave(&dev->event_lock, flags);
3744 if (file_priv->event_space < sizeof e->event) {
3745 spin_unlock_irqrestore(&dev->event_lock, flags);
3746 goto out;
3747 }
3748 file_priv->event_space -= sizeof e->event;
3749 spin_unlock_irqrestore(&dev->event_lock, flags);
3750
3751 e = kzalloc(sizeof *e, GFP_KERNEL);
3752 if (e == NULL) {
3753 spin_lock_irqsave(&dev->event_lock, flags);
3754 file_priv->event_space += sizeof e->event;
3755 spin_unlock_irqrestore(&dev->event_lock, flags);
3756 goto out;
3757 }
3758
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003759 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003760 e->event.base.length = sizeof e->event;
3761 e->event.user_data = page_flip->user_data;
3762 e->base.event = &e->event.base;
3763 e->base.file_priv = file_priv;
3764 e->base.destroy =
3765 (void (*) (struct drm_pending_event *)) kfree;
3766 }
3767
Daniel Vetterb0d12322012-12-11 01:07:12 +01003768 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003769 ret = crtc->funcs->page_flip(crtc, fb, e);
3770 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003771 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3772 spin_lock_irqsave(&dev->event_lock, flags);
3773 file_priv->event_space += sizeof e->event;
3774 spin_unlock_irqrestore(&dev->event_lock, flags);
3775 kfree(e);
3776 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003777 /* Keep the old fb, don't unref it. */
3778 old_fb = NULL;
3779 } else {
3780 /* Unref only the old framebuffer. */
3781 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003782 }
3783
3784out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003785 if (fb)
3786 drm_framebuffer_unreference(fb);
3787 if (old_fb)
3788 drm_framebuffer_unreference(old_fb);
Daniel Vetter84849902012-12-02 00:28:11 +01003789 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003790 return ret;
3791}
Chris Wilsoneb0335562011-01-24 15:11:08 +00003792
3793void drm_mode_config_reset(struct drm_device *dev)
3794{
3795 struct drm_crtc *crtc;
3796 struct drm_encoder *encoder;
3797 struct drm_connector *connector;
3798
3799 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3800 if (crtc->funcs->reset)
3801 crtc->funcs->reset(crtc);
3802
3803 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3804 if (encoder->funcs->reset)
3805 encoder->funcs->reset(encoder);
3806
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003807 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3808 connector->status = connector_status_unknown;
3809
Chris Wilsoneb0335562011-01-24 15:11:08 +00003810 if (connector->funcs->reset)
3811 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003812 }
Chris Wilsoneb0335562011-01-24 15:11:08 +00003813}
3814EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003815
3816int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3817 void *data, struct drm_file *file_priv)
3818{
3819 struct drm_mode_create_dumb *args = data;
3820
3821 if (!dev->driver->dumb_create)
3822 return -ENOSYS;
3823 return dev->driver->dumb_create(file_priv, dev, args);
3824}
3825
3826int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3827 void *data, struct drm_file *file_priv)
3828{
3829 struct drm_mode_map_dumb *args = data;
3830
3831 /* call driver ioctl to get mmap offset */
3832 if (!dev->driver->dumb_map_offset)
3833 return -ENOSYS;
3834
3835 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3836}
3837
3838int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3839 void *data, struct drm_file *file_priv)
3840{
3841 struct drm_mode_destroy_dumb *args = data;
3842
3843 if (!dev->driver->dumb_destroy)
3844 return -ENOSYS;
3845
3846 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3847}
Dave Airlie248dbc22011-11-29 20:02:54 +00003848
3849/*
3850 * Just need to support RGB formats here for compat with code that doesn't
3851 * use pixel formats directly yet.
3852 */
3853void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3854 int *bpp)
3855{
3856 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003857 case DRM_FORMAT_RGB332:
3858 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003859 *depth = 8;
3860 *bpp = 8;
3861 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003862 case DRM_FORMAT_XRGB1555:
3863 case DRM_FORMAT_XBGR1555:
3864 case DRM_FORMAT_RGBX5551:
3865 case DRM_FORMAT_BGRX5551:
3866 case DRM_FORMAT_ARGB1555:
3867 case DRM_FORMAT_ABGR1555:
3868 case DRM_FORMAT_RGBA5551:
3869 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003870 *depth = 15;
3871 *bpp = 16;
3872 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003873 case DRM_FORMAT_RGB565:
3874 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003875 *depth = 16;
3876 *bpp = 16;
3877 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003878 case DRM_FORMAT_RGB888:
3879 case DRM_FORMAT_BGR888:
3880 *depth = 24;
3881 *bpp = 24;
3882 break;
3883 case DRM_FORMAT_XRGB8888:
3884 case DRM_FORMAT_XBGR8888:
3885 case DRM_FORMAT_RGBX8888:
3886 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003887 *depth = 24;
3888 *bpp = 32;
3889 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003890 case DRM_FORMAT_XRGB2101010:
3891 case DRM_FORMAT_XBGR2101010:
3892 case DRM_FORMAT_RGBX1010102:
3893 case DRM_FORMAT_BGRX1010102:
3894 case DRM_FORMAT_ARGB2101010:
3895 case DRM_FORMAT_ABGR2101010:
3896 case DRM_FORMAT_RGBA1010102:
3897 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003898 *depth = 30;
3899 *bpp = 32;
3900 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003901 case DRM_FORMAT_ARGB8888:
3902 case DRM_FORMAT_ABGR8888:
3903 case DRM_FORMAT_RGBA8888:
3904 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003905 *depth = 32;
3906 *bpp = 32;
3907 break;
3908 default:
3909 DRM_DEBUG_KMS("unsupported pixel format\n");
3910 *depth = 0;
3911 *bpp = 0;
3912 break;
3913 }
3914}
3915EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003916
3917/**
3918 * drm_format_num_planes - get the number of planes for format
3919 * @format: pixel format (DRM_FORMAT_*)
3920 *
3921 * RETURNS:
3922 * The number of planes used by the specified pixel format.
3923 */
3924int drm_format_num_planes(uint32_t format)
3925{
3926 switch (format) {
3927 case DRM_FORMAT_YUV410:
3928 case DRM_FORMAT_YVU410:
3929 case DRM_FORMAT_YUV411:
3930 case DRM_FORMAT_YVU411:
3931 case DRM_FORMAT_YUV420:
3932 case DRM_FORMAT_YVU420:
3933 case DRM_FORMAT_YUV422:
3934 case DRM_FORMAT_YVU422:
3935 case DRM_FORMAT_YUV444:
3936 case DRM_FORMAT_YVU444:
3937 return 3;
3938 case DRM_FORMAT_NV12:
3939 case DRM_FORMAT_NV21:
3940 case DRM_FORMAT_NV16:
3941 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003942 case DRM_FORMAT_NV24:
3943 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003944 return 2;
3945 default:
3946 return 1;
3947 }
3948}
3949EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003950
3951/**
3952 * drm_format_plane_cpp - determine the bytes per pixel value
3953 * @format: pixel format (DRM_FORMAT_*)
3954 * @plane: plane index
3955 *
3956 * RETURNS:
3957 * The bytes per pixel value for the specified plane.
3958 */
3959int drm_format_plane_cpp(uint32_t format, int plane)
3960{
3961 unsigned int depth;
3962 int bpp;
3963
3964 if (plane >= drm_format_num_planes(format))
3965 return 0;
3966
3967 switch (format) {
3968 case DRM_FORMAT_YUYV:
3969 case DRM_FORMAT_YVYU:
3970 case DRM_FORMAT_UYVY:
3971 case DRM_FORMAT_VYUY:
3972 return 2;
3973 case DRM_FORMAT_NV12:
3974 case DRM_FORMAT_NV21:
3975 case DRM_FORMAT_NV16:
3976 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003977 case DRM_FORMAT_NV24:
3978 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003979 return plane ? 2 : 1;
3980 case DRM_FORMAT_YUV410:
3981 case DRM_FORMAT_YVU410:
3982 case DRM_FORMAT_YUV411:
3983 case DRM_FORMAT_YVU411:
3984 case DRM_FORMAT_YUV420:
3985 case DRM_FORMAT_YVU420:
3986 case DRM_FORMAT_YUV422:
3987 case DRM_FORMAT_YVU422:
3988 case DRM_FORMAT_YUV444:
3989 case DRM_FORMAT_YVU444:
3990 return 1;
3991 default:
3992 drm_fb_get_bpp_depth(format, &depth, &bpp);
3993 return bpp >> 3;
3994 }
3995}
3996EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003997
3998/**
3999 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
4000 * @format: pixel format (DRM_FORMAT_*)
4001 *
4002 * RETURNS:
4003 * The horizontal chroma subsampling factor for the
4004 * specified pixel format.
4005 */
4006int drm_format_horz_chroma_subsampling(uint32_t format)
4007{
4008 switch (format) {
4009 case DRM_FORMAT_YUV411:
4010 case DRM_FORMAT_YVU411:
4011 case DRM_FORMAT_YUV410:
4012 case DRM_FORMAT_YVU410:
4013 return 4;
4014 case DRM_FORMAT_YUYV:
4015 case DRM_FORMAT_YVYU:
4016 case DRM_FORMAT_UYVY:
4017 case DRM_FORMAT_VYUY:
4018 case DRM_FORMAT_NV12:
4019 case DRM_FORMAT_NV21:
4020 case DRM_FORMAT_NV16:
4021 case DRM_FORMAT_NV61:
4022 case DRM_FORMAT_YUV422:
4023 case DRM_FORMAT_YVU422:
4024 case DRM_FORMAT_YUV420:
4025 case DRM_FORMAT_YVU420:
4026 return 2;
4027 default:
4028 return 1;
4029 }
4030}
4031EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4032
4033/**
4034 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4035 * @format: pixel format (DRM_FORMAT_*)
4036 *
4037 * RETURNS:
4038 * The vertical chroma subsampling factor for the
4039 * specified pixel format.
4040 */
4041int drm_format_vert_chroma_subsampling(uint32_t format)
4042{
4043 switch (format) {
4044 case DRM_FORMAT_YUV410:
4045 case DRM_FORMAT_YVU410:
4046 return 4;
4047 case DRM_FORMAT_YUV420:
4048 case DRM_FORMAT_YVU420:
4049 case DRM_FORMAT_NV12:
4050 case DRM_FORMAT_NV21:
4051 return 2;
4052 default:
4053 return 1;
4054 }
4055}
4056EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);