blob: 6dc75ee100793d9bacab43810b50cbfbef135ef9 [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
520 * passed-in framebuffer.
Rob Clarkf7eff602012-09-05 21:48:38 +0000521 */
522void drm_framebuffer_remove(struct drm_framebuffer *fb)
523{
524 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800525 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800526 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000527 struct drm_mode_set set;
528 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800529
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100530 WARN_ON(!drm_modeset_is_locked(dev));
Daniel Vetter4b096ac2012-12-10 21:19:18 +0100531 WARN_ON(!list_empty(&fb->filp_head));
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100532
Dave Airlief453ba02008-11-07 14:05:41 -0800533 /* remove from any CRTC */
534 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000535 if (crtc->fb == fb) {
536 /* should turn off the crtc */
537 memset(&set, 0, sizeof(struct drm_mode_set));
538 set.crtc = crtc;
539 set.fb = NULL;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100540 ret = drm_mode_set_config_internal(&set);
Dave Airlie5ef5f722009-08-17 13:11:23 +1000541 if (ret)
542 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
543 }
Dave Airlief453ba02008-11-07 14:05:41 -0800544 }
545
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800546 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
547 if (plane->fb == fb) {
548 /* should turn off the crtc */
549 ret = plane->funcs->disable_plane(plane);
550 if (ret)
551 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600552 /* disconnect the plane from the fb and crtc: */
Daniel Vetter6c2a7532012-12-11 00:59:24 +0100553 __drm_framebuffer_unreference(plane->fb);
Rob Clarka9971152011-12-13 20:19:35 -0600554 plane->fb = NULL;
555 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800556 }
557 }
558
Rob Clarkf7eff602012-09-05 21:48:38 +0000559 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800560}
Rob Clarkf7eff602012-09-05 21:48:38 +0000561EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800562
563/**
564 * drm_crtc_init - Initialise a new CRTC object
565 * @dev: DRM device
566 * @crtc: CRTC object to init
567 * @funcs: callbacks for the new CRTC
568 *
Dave Airlief453ba02008-11-07 14:05:41 -0800569 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200570 *
571 * RETURNS:
572 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800573 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200574int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800575 const struct drm_crtc_funcs *funcs)
576{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200577 int ret;
578
Dave Airlief453ba02008-11-07 14:05:41 -0800579 crtc->dev = dev;
580 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000581 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800582
Daniel Vetter84849902012-12-02 00:28:11 +0100583 drm_modeset_lock_all(dev);
Daniel Vetter29494c12012-12-02 02:18:25 +0100584 mutex_init(&crtc->mutex);
585 mutex_lock_nest_lock(&crtc->mutex, &dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200586
587 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
588 if (ret)
589 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800590
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300591 crtc->base.properties = &crtc->properties;
592
Dave Airlief453ba02008-11-07 14:05:41 -0800593 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
594 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200595
596 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100597 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200598
599 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800600}
601EXPORT_SYMBOL(drm_crtc_init);
602
603/**
604 * drm_crtc_cleanup - Cleans up the core crtc usage.
605 * @crtc: CRTC to cleanup
606 *
Dave Airlief453ba02008-11-07 14:05:41 -0800607 * Cleanup @crtc. Removes from drm modesetting space
608 * does NOT free object, caller does that.
609 */
610void drm_crtc_cleanup(struct drm_crtc *crtc)
611{
612 struct drm_device *dev = crtc->dev;
613
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000614 kfree(crtc->gamma_store);
615 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800616
617 drm_mode_object_put(dev, &crtc->base);
618 list_del(&crtc->head);
619 dev->mode_config.num_crtc--;
620}
621EXPORT_SYMBOL(drm_crtc_cleanup);
622
623/**
624 * drm_mode_probed_add - add a mode to a connector's probed mode list
625 * @connector: connector the new mode
626 * @mode: mode data
627 *
Dave Airlief453ba02008-11-07 14:05:41 -0800628 * Add @mode to @connector's mode list for later use.
629 */
630void drm_mode_probed_add(struct drm_connector *connector,
631 struct drm_display_mode *mode)
632{
633 list_add(&mode->head, &connector->probed_modes);
634}
635EXPORT_SYMBOL(drm_mode_probed_add);
636
637/**
638 * drm_mode_remove - remove and free a mode
639 * @connector: connector list to modify
640 * @mode: mode to remove
641 *
Dave Airlief453ba02008-11-07 14:05:41 -0800642 * Remove @mode from @connector's mode list, then free it.
643 */
644void drm_mode_remove(struct drm_connector *connector,
645 struct drm_display_mode *mode)
646{
647 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100648 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800649}
650EXPORT_SYMBOL(drm_mode_remove);
651
652/**
653 * drm_connector_init - Init a preallocated connector
654 * @dev: DRM device
655 * @connector: the connector to init
656 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100657 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800658 *
Dave Airlief453ba02008-11-07 14:05:41 -0800659 * Initialises a preallocated connector. Connectors should be
660 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200661 *
662 * RETURNS:
663 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800664 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200665int drm_connector_init(struct drm_device *dev,
666 struct drm_connector *connector,
667 const struct drm_connector_funcs *funcs,
668 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800669{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200670 int ret;
671
Daniel Vetter84849902012-12-02 00:28:11 +0100672 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800673
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200674 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
675 if (ret)
676 goto out;
677
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300678 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800679 connector->dev = dev;
680 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800681 connector->connector_type = connector_type;
682 connector->connector_type_id =
683 ++drm_connector_enum_list[connector_type].count; /* TODO */
684 INIT_LIST_HEAD(&connector->user_modes);
685 INIT_LIST_HEAD(&connector->probed_modes);
686 INIT_LIST_HEAD(&connector->modes);
687 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000688 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800689
690 list_add_tail(&connector->head, &dev->mode_config.connector_list);
691 dev->mode_config.num_connector++;
692
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200693 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500694 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200695 dev->mode_config.edid_property,
696 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800697
Rob Clark58495562012-10-11 20:50:56 -0500698 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800699 dev->mode_config.dpms_property, 0);
700
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200701 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100702 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200703
704 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800705}
706EXPORT_SYMBOL(drm_connector_init);
707
708/**
709 * drm_connector_cleanup - cleans up an initialised connector
710 * @connector: connector to cleanup
711 *
Dave Airlief453ba02008-11-07 14:05:41 -0800712 * Cleans up the connector but doesn't free the object.
713 */
714void drm_connector_cleanup(struct drm_connector *connector)
715{
716 struct drm_device *dev = connector->dev;
717 struct drm_display_mode *mode, *t;
718
719 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
720 drm_mode_remove(connector, mode);
721
722 list_for_each_entry_safe(mode, t, &connector->modes, head)
723 drm_mode_remove(connector, mode);
724
725 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
726 drm_mode_remove(connector, mode);
727
Dave Airlief453ba02008-11-07 14:05:41 -0800728 drm_mode_object_put(dev, &connector->base);
729 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000730 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800731}
732EXPORT_SYMBOL(drm_connector_cleanup);
733
Dave Airliecbc7e222012-02-20 14:16:40 +0000734void drm_connector_unplug_all(struct drm_device *dev)
735{
736 struct drm_connector *connector;
737
738 /* taking the mode config mutex ends up in a clash with sysfs */
739 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
740 drm_sysfs_connector_remove(connector);
741
742}
743EXPORT_SYMBOL(drm_connector_unplug_all);
744
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200745int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000746 struct drm_encoder *encoder,
747 const struct drm_encoder_funcs *funcs,
748 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800749{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200750 int ret;
751
Daniel Vetter84849902012-12-02 00:28:11 +0100752 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800753
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200754 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
755 if (ret)
756 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800757
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200758 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800759 encoder->encoder_type = encoder_type;
760 encoder->funcs = funcs;
761
762 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
763 dev->mode_config.num_encoder++;
764
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200765 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100766 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200767
768 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800769}
770EXPORT_SYMBOL(drm_encoder_init);
771
772void drm_encoder_cleanup(struct drm_encoder *encoder)
773{
774 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100775 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800776 drm_mode_object_put(dev, &encoder->base);
777 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000778 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100779 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800780}
781EXPORT_SYMBOL(drm_encoder_cleanup);
782
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800783int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
784 unsigned long possible_crtcs,
785 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600786 const uint32_t *formats, uint32_t format_count,
787 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800788{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200789 int ret;
790
Daniel Vetter84849902012-12-02 00:28:11 +0100791 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800792
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200793 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
794 if (ret)
795 goto out;
796
Rob Clark4d939142012-05-17 02:23:27 -0600797 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800798 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800799 plane->funcs = funcs;
800 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
801 GFP_KERNEL);
802 if (!plane->format_types) {
803 DRM_DEBUG_KMS("out of memory when allocating plane\n");
804 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200805 ret = -ENOMEM;
806 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800807 }
808
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800809 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800810 plane->format_count = format_count;
811 plane->possible_crtcs = possible_crtcs;
812
Rob Clark0a7eb242011-12-13 20:19:36 -0600813 /* private planes are not exposed to userspace, but depending on
814 * display hardware, might be convenient to allow sharing programming
815 * for the scanout engine with the crtc implementation.
816 */
817 if (!priv) {
818 list_add_tail(&plane->head, &dev->mode_config.plane_list);
819 dev->mode_config.num_plane++;
820 } else {
821 INIT_LIST_HEAD(&plane->head);
822 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800823
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200824 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100825 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800826
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200827 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800828}
829EXPORT_SYMBOL(drm_plane_init);
830
831void drm_plane_cleanup(struct drm_plane *plane)
832{
833 struct drm_device *dev = plane->dev;
834
Daniel Vetter84849902012-12-02 00:28:11 +0100835 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800836 kfree(plane->format_types);
837 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600838 /* if not added to a list, it must be a private plane */
839 if (!list_empty(&plane->head)) {
840 list_del(&plane->head);
841 dev->mode_config.num_plane--;
842 }
Daniel Vetter84849902012-12-02 00:28:11 +0100843 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800844}
845EXPORT_SYMBOL(drm_plane_cleanup);
846
Dave Airlief453ba02008-11-07 14:05:41 -0800847/**
848 * drm_mode_create - create a new display mode
849 * @dev: DRM device
850 *
Dave Airlief453ba02008-11-07 14:05:41 -0800851 * Create a new drm_display_mode, give it an ID, and return it.
852 *
853 * RETURNS:
854 * Pointer to new mode on success, NULL on error.
855 */
856struct drm_display_mode *drm_mode_create(struct drm_device *dev)
857{
858 struct drm_display_mode *nmode;
859
860 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
861 if (!nmode)
862 return NULL;
863
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200864 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
865 kfree(nmode);
866 return NULL;
867 }
868
Dave Airlief453ba02008-11-07 14:05:41 -0800869 return nmode;
870}
871EXPORT_SYMBOL(drm_mode_create);
872
873/**
874 * drm_mode_destroy - remove a mode
875 * @dev: DRM device
876 * @mode: mode to remove
877 *
Dave Airlief453ba02008-11-07 14:05:41 -0800878 * Free @mode's unique identifier, then free it.
879 */
880void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
881{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200882 if (!mode)
883 return;
884
Dave Airlief453ba02008-11-07 14:05:41 -0800885 drm_mode_object_put(dev, &mode->base);
886
887 kfree(mode);
888}
889EXPORT_SYMBOL(drm_mode_destroy);
890
891static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
892{
893 struct drm_property *edid;
894 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800895
896 /*
897 * Standard properties (apply to all connectors)
898 */
899 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
900 DRM_MODE_PROP_IMMUTABLE,
901 "EDID", 0);
902 dev->mode_config.edid_property = edid;
903
Sascha Hauer4a67d392012-02-06 10:58:17 +0100904 dpms = drm_property_create_enum(dev, 0,
905 "DPMS", drm_dpms_enum_list,
906 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800907 dev->mode_config.dpms_property = dpms;
908
909 return 0;
910}
911
912/**
913 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
914 * @dev: DRM device
915 *
916 * Called by a driver the first time a DVI-I connector is made.
917 */
918int drm_mode_create_dvi_i_properties(struct drm_device *dev)
919{
920 struct drm_property *dvi_i_selector;
921 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800922
923 if (dev->mode_config.dvi_i_select_subconnector_property)
924 return 0;
925
926 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100927 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800928 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100929 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800930 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800931 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
932
Sascha Hauer4a67d392012-02-06 10:58:17 +0100933 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800934 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100935 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800936 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800937 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
938
939 return 0;
940}
941EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
942
943/**
944 * drm_create_tv_properties - create TV specific connector properties
945 * @dev: DRM device
946 * @num_modes: number of different TV formats (modes) supported
947 * @modes: array of pointers to strings containing name of each format
948 *
949 * Called by a driver's TV initialization routine, this function creates
950 * the TV specific connector properties for a given device. Caller is
951 * responsible for allocating a list of format names and passing them to
952 * this routine.
953 */
954int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
955 char *modes[])
956{
957 struct drm_property *tv_selector;
958 struct drm_property *tv_subconnector;
959 int i;
960
961 if (dev->mode_config.tv_select_subconnector_property)
962 return 0;
963
964 /*
965 * Basic connector properties
966 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100967 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800968 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100969 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800970 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800971 dev->mode_config.tv_select_subconnector_property = tv_selector;
972
973 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100974 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
975 "subconnector",
976 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800977 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800978 dev->mode_config.tv_subconnector_property = tv_subconnector;
979
980 /*
981 * Other, TV specific properties: margins & TV modes.
982 */
983 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100984 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800985
986 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100987 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800988
989 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100990 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800991
992 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100993 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800994
995 dev->mode_config.tv_mode_property =
996 drm_property_create(dev, DRM_MODE_PROP_ENUM,
997 "mode", num_modes);
998 for (i = 0; i < num_modes; i++)
999 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1000 i, modes[i]);
1001
Francisco Jerezb6b79022009-08-02 04:19:20 +02001002 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001003 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001004
1005 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001006 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001007
1008 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001009 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +02001010
Francisco Jereza75f0232009-08-12 02:30:10 +02001011 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001012 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001013
1014 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001015 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001016
1017 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001018 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +02001019
Dave Airlief453ba02008-11-07 14:05:41 -08001020 return 0;
1021}
1022EXPORT_SYMBOL(drm_mode_create_tv_properties);
1023
1024/**
1025 * drm_mode_create_scaling_mode_property - create scaling mode property
1026 * @dev: DRM device
1027 *
1028 * Called by a driver the first time it's needed, must be attached to desired
1029 * connectors.
1030 */
1031int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1032{
1033 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001034
1035 if (dev->mode_config.scaling_mode_property)
1036 return 0;
1037
1038 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001039 drm_property_create_enum(dev, 0, "scaling mode",
1040 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001041 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001042
1043 dev->mode_config.scaling_mode_property = scaling_mode;
1044
1045 return 0;
1046}
1047EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1048
1049/**
1050 * drm_mode_create_dithering_property - create dithering property
1051 * @dev: DRM device
1052 *
1053 * Called by a driver the first time it's needed, must be attached to desired
1054 * connectors.
1055 */
1056int drm_mode_create_dithering_property(struct drm_device *dev)
1057{
1058 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001059
1060 if (dev->mode_config.dithering_mode_property)
1061 return 0;
1062
1063 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001064 drm_property_create_enum(dev, 0, "dithering",
1065 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001066 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001067 dev->mode_config.dithering_mode_property = dithering_mode;
1068
1069 return 0;
1070}
1071EXPORT_SYMBOL(drm_mode_create_dithering_property);
1072
1073/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001074 * drm_mode_create_dirty_property - create dirty property
1075 * @dev: DRM device
1076 *
1077 * Called by a driver the first time it's needed, must be attached to desired
1078 * connectors.
1079 */
1080int drm_mode_create_dirty_info_property(struct drm_device *dev)
1081{
1082 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001083
1084 if (dev->mode_config.dirty_info_property)
1085 return 0;
1086
1087 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001088 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001089 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001090 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001091 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00001092 dev->mode_config.dirty_info_property = dirty_info;
1093
1094 return 0;
1095}
1096EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
1097
1098/**
Dave Airlief453ba02008-11-07 14:05:41 -08001099 * drm_mode_config_init - initialize DRM mode_configuration structure
1100 * @dev: DRM device
1101 *
Dave Airlief453ba02008-11-07 14:05:41 -08001102 * Initialize @dev's mode_config structure, used for tracking the graphics
1103 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001104 *
1105 * Since this initializes the modeset locks, no locking is possible. Which is no
1106 * problem, since this should happen single threaded at init time. It is the
1107 * driver's problem to ensure this guarantee.
1108 *
Dave Airlief453ba02008-11-07 14:05:41 -08001109 */
1110void drm_mode_config_init(struct drm_device *dev)
1111{
1112 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +10001113 mutex_init(&dev->mode_config.idr_mutex);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001114 mutex_init(&dev->mode_config.fb_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001115 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001116 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1117 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1118 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1119 INIT_LIST_HEAD(&dev->mode_config.property_list);
1120 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001121 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001122 idr_init(&dev->mode_config.crtc_idr);
1123
Daniel Vetter84849902012-12-02 00:28:11 +01001124 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001125 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +01001126 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001127
1128 /* Just to be sure */
1129 dev->mode_config.num_fb = 0;
1130 dev->mode_config.num_connector = 0;
1131 dev->mode_config.num_crtc = 0;
1132 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001133}
1134EXPORT_SYMBOL(drm_mode_config_init);
1135
1136int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1137{
1138 uint32_t total_objects = 0;
1139
1140 total_objects += dev->mode_config.num_crtc;
1141 total_objects += dev->mode_config.num_connector;
1142 total_objects += dev->mode_config.num_encoder;
1143
Dave Airlief453ba02008-11-07 14:05:41 -08001144 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1145 if (!group->id_list)
1146 return -ENOMEM;
1147
1148 group->num_crtcs = 0;
1149 group->num_connectors = 0;
1150 group->num_encoders = 0;
1151 return 0;
1152}
1153
1154int drm_mode_group_init_legacy_group(struct drm_device *dev,
1155 struct drm_mode_group *group)
1156{
1157 struct drm_crtc *crtc;
1158 struct drm_encoder *encoder;
1159 struct drm_connector *connector;
1160 int ret;
1161
1162 if ((ret = drm_mode_group_init(dev, group)))
1163 return ret;
1164
1165 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1166 group->id_list[group->num_crtcs++] = crtc->base.id;
1167
1168 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1169 group->id_list[group->num_crtcs + group->num_encoders++] =
1170 encoder->base.id;
1171
1172 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1173 group->id_list[group->num_crtcs + group->num_encoders +
1174 group->num_connectors++] = connector->base.id;
1175
1176 return 0;
1177}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001178EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001179
1180/**
1181 * drm_mode_config_cleanup - free up DRM mode_config info
1182 * @dev: DRM device
1183 *
Dave Airlief453ba02008-11-07 14:05:41 -08001184 * Free up all the connectors and CRTCs associated with this DRM device, then
1185 * free up the framebuffers and associated buffer objects.
1186 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001187 * Note that since this /should/ happen single-threaded at driver/device
1188 * teardown time, no locking is required. It's the driver's job to ensure that
1189 * this guarantee actually holds true.
1190 *
Dave Airlief453ba02008-11-07 14:05:41 -08001191 * FIXME: cleanup any dangling user buffer objects too
1192 */
1193void drm_mode_config_cleanup(struct drm_device *dev)
1194{
1195 struct drm_connector *connector, *ot;
1196 struct drm_crtc *crtc, *ct;
1197 struct drm_encoder *encoder, *enct;
1198 struct drm_framebuffer *fb, *fbt;
1199 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001200 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001201
1202 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1203 head) {
1204 encoder->funcs->destroy(encoder);
1205 }
1206
1207 list_for_each_entry_safe(connector, ot,
1208 &dev->mode_config.connector_list, head) {
1209 connector->funcs->destroy(connector);
1210 }
1211
1212 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1213 head) {
1214 drm_property_destroy(dev, property);
1215 }
1216
Daniel Vetter2b677e82012-12-10 21:16:05 +01001217 /*
1218 * Single-threaded teardown context, so it's not required to grab the
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001219 * fb_lock to protect against concurrent fb_list access. Contrary, it
Daniel Vetter2b677e82012-12-10 21:16:05 +01001220 * would actually deadlock with the drm_framebuffer_cleanup function.
1221 *
1222 * Also, if there are any framebuffers left, that's a driver leak now,
1223 * so politely WARN about this.
1224 */
1225 WARN_ON(!list_empty(&dev->mode_config.fb_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001226 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001227 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001228 }
1229
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001230 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1231 head) {
1232 plane->funcs->destroy(plane);
1233 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001234
Chris Wilson31840092012-09-17 09:38:03 +00001235 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1236 crtc->funcs->destroy(crtc);
1237 }
1238
Sascha Hauer59ce0622012-02-01 11:38:20 +01001239 idr_remove_all(&dev->mode_config.crtc_idr);
1240 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001241}
1242EXPORT_SYMBOL(drm_mode_config_cleanup);
1243
Dave Airlief453ba02008-11-07 14:05:41 -08001244/**
1245 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1246 * @out: drm_mode_modeinfo struct to return to the user
1247 * @in: drm_display_mode to use
1248 *
Dave Airlief453ba02008-11-07 14:05:41 -08001249 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1250 * the user.
1251 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001252static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1253 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001254{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001255 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1256 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1257 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1258 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1259 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1260 "timing values too large for mode info\n");
1261
Dave Airlief453ba02008-11-07 14:05:41 -08001262 out->clock = in->clock;
1263 out->hdisplay = in->hdisplay;
1264 out->hsync_start = in->hsync_start;
1265 out->hsync_end = in->hsync_end;
1266 out->htotal = in->htotal;
1267 out->hskew = in->hskew;
1268 out->vdisplay = in->vdisplay;
1269 out->vsync_start = in->vsync_start;
1270 out->vsync_end = in->vsync_end;
1271 out->vtotal = in->vtotal;
1272 out->vscan = in->vscan;
1273 out->vrefresh = in->vrefresh;
1274 out->flags = in->flags;
1275 out->type = in->type;
1276 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1277 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1278}
1279
1280/**
1281 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1282 * @out: drm_display_mode to return to the user
1283 * @in: drm_mode_modeinfo to use
1284 *
Dave Airlief453ba02008-11-07 14:05:41 -08001285 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1286 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001287 *
1288 * RETURNS:
1289 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001290 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001291static int drm_crtc_convert_umode(struct drm_display_mode *out,
1292 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001293{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001294 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1295 return -ERANGE;
1296
Dave Airlief453ba02008-11-07 14:05:41 -08001297 out->clock = in->clock;
1298 out->hdisplay = in->hdisplay;
1299 out->hsync_start = in->hsync_start;
1300 out->hsync_end = in->hsync_end;
1301 out->htotal = in->htotal;
1302 out->hskew = in->hskew;
1303 out->vdisplay = in->vdisplay;
1304 out->vsync_start = in->vsync_start;
1305 out->vsync_end = in->vsync_end;
1306 out->vtotal = in->vtotal;
1307 out->vscan = in->vscan;
1308 out->vrefresh = in->vrefresh;
1309 out->flags = in->flags;
1310 out->type = in->type;
1311 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1312 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001313
1314 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001315}
1316
1317/**
1318 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001319 * @dev: drm device for the ioctl
1320 * @data: data pointer for the ioctl
1321 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001322 *
Dave Airlief453ba02008-11-07 14:05:41 -08001323 * Construct a set of configuration description structures and return
1324 * them to the user, including CRTC, connector and framebuffer configuration.
1325 *
1326 * Called by the user via ioctl.
1327 *
1328 * RETURNS:
1329 * Zero on success, errno on failure.
1330 */
1331int drm_mode_getresources(struct drm_device *dev, void *data,
1332 struct drm_file *file_priv)
1333{
1334 struct drm_mode_card_res *card_res = data;
1335 struct list_head *lh;
1336 struct drm_framebuffer *fb;
1337 struct drm_connector *connector;
1338 struct drm_crtc *crtc;
1339 struct drm_encoder *encoder;
1340 int ret = 0;
1341 int connector_count = 0;
1342 int crtc_count = 0;
1343 int fb_count = 0;
1344 int encoder_count = 0;
1345 int copied = 0, i;
1346 uint32_t __user *fb_id;
1347 uint32_t __user *crtc_id;
1348 uint32_t __user *connector_id;
1349 uint32_t __user *encoder_id;
1350 struct drm_mode_group *mode_group;
1351
Dave Airliefb3b06c2011-02-08 13:55:21 +10001352 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1353 return -EINVAL;
1354
Dave Airlief453ba02008-11-07 14:05:41 -08001355
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001356 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001357 /*
1358 * For the non-control nodes we need to limit the list of resources
1359 * by IDs in the group list for this node
1360 */
1361 list_for_each(lh, &file_priv->fbs)
1362 fb_count++;
1363
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001364 /* handle this in 4 parts */
1365 /* FBs */
1366 if (card_res->count_fbs >= fb_count) {
1367 copied = 0;
1368 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1369 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1370 if (put_user(fb->base.id, fb_id + copied)) {
1371 mutex_unlock(&file_priv->fbs_lock);
1372 return -EFAULT;
1373 }
1374 copied++;
1375 }
1376 }
1377 card_res->count_fbs = fb_count;
1378 mutex_unlock(&file_priv->fbs_lock);
1379
1380 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001381 mode_group = &file_priv->master->minor->mode_group;
1382 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1383
1384 list_for_each(lh, &dev->mode_config.crtc_list)
1385 crtc_count++;
1386
1387 list_for_each(lh, &dev->mode_config.connector_list)
1388 connector_count++;
1389
1390 list_for_each(lh, &dev->mode_config.encoder_list)
1391 encoder_count++;
1392 } else {
1393
1394 crtc_count = mode_group->num_crtcs;
1395 connector_count = mode_group->num_connectors;
1396 encoder_count = mode_group->num_encoders;
1397 }
1398
1399 card_res->max_height = dev->mode_config.max_height;
1400 card_res->min_height = dev->mode_config.min_height;
1401 card_res->max_width = dev->mode_config.max_width;
1402 card_res->min_width = dev->mode_config.min_width;
1403
Dave Airlief453ba02008-11-07 14:05:41 -08001404 /* CRTCs */
1405 if (card_res->count_crtcs >= crtc_count) {
1406 copied = 0;
1407 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1408 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1409 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1410 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001411 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001412 if (put_user(crtc->base.id, crtc_id + copied)) {
1413 ret = -EFAULT;
1414 goto out;
1415 }
1416 copied++;
1417 }
1418 } else {
1419 for (i = 0; i < mode_group->num_crtcs; i++) {
1420 if (put_user(mode_group->id_list[i],
1421 crtc_id + copied)) {
1422 ret = -EFAULT;
1423 goto out;
1424 }
1425 copied++;
1426 }
1427 }
1428 }
1429 card_res->count_crtcs = crtc_count;
1430
1431 /* Encoders */
1432 if (card_res->count_encoders >= encoder_count) {
1433 copied = 0;
1434 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1435 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1436 list_for_each_entry(encoder,
1437 &dev->mode_config.encoder_list,
1438 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001439 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1440 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001441 if (put_user(encoder->base.id, encoder_id +
1442 copied)) {
1443 ret = -EFAULT;
1444 goto out;
1445 }
1446 copied++;
1447 }
1448 } else {
1449 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1450 if (put_user(mode_group->id_list[i],
1451 encoder_id + copied)) {
1452 ret = -EFAULT;
1453 goto out;
1454 }
1455 copied++;
1456 }
1457
1458 }
1459 }
1460 card_res->count_encoders = encoder_count;
1461
1462 /* Connectors */
1463 if (card_res->count_connectors >= connector_count) {
1464 copied = 0;
1465 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1466 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1467 list_for_each_entry(connector,
1468 &dev->mode_config.connector_list,
1469 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001470 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1471 connector->base.id,
1472 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001473 if (put_user(connector->base.id,
1474 connector_id + copied)) {
1475 ret = -EFAULT;
1476 goto out;
1477 }
1478 copied++;
1479 }
1480 } else {
1481 int start = mode_group->num_crtcs +
1482 mode_group->num_encoders;
1483 for (i = start; i < start + mode_group->num_connectors; i++) {
1484 if (put_user(mode_group->id_list[i],
1485 connector_id + copied)) {
1486 ret = -EFAULT;
1487 goto out;
1488 }
1489 copied++;
1490 }
1491 }
1492 }
1493 card_res->count_connectors = connector_count;
1494
Jerome Glisse94401062010-07-15 15:43:25 -04001495 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001496 card_res->count_connectors, card_res->count_encoders);
1497
1498out:
Daniel Vetter84849902012-12-02 00:28:11 +01001499 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001500 return ret;
1501}
1502
1503/**
1504 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001505 * @dev: drm device for the ioctl
1506 * @data: data pointer for the ioctl
1507 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001508 *
Dave Airlief453ba02008-11-07 14:05:41 -08001509 * Construct a CRTC configuration structure to return to the user.
1510 *
1511 * Called by the user via ioctl.
1512 *
1513 * RETURNS:
1514 * Zero on success, errno on failure.
1515 */
1516int drm_mode_getcrtc(struct drm_device *dev,
1517 void *data, struct drm_file *file_priv)
1518{
1519 struct drm_mode_crtc *crtc_resp = data;
1520 struct drm_crtc *crtc;
1521 struct drm_mode_object *obj;
1522 int ret = 0;
1523
Dave Airliefb3b06c2011-02-08 13:55:21 +10001524 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1525 return -EINVAL;
1526
Daniel Vetter84849902012-12-02 00:28:11 +01001527 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001528
1529 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1530 DRM_MODE_OBJECT_CRTC);
1531 if (!obj) {
1532 ret = -EINVAL;
1533 goto out;
1534 }
1535 crtc = obj_to_crtc(obj);
1536
1537 crtc_resp->x = crtc->x;
1538 crtc_resp->y = crtc->y;
1539 crtc_resp->gamma_size = crtc->gamma_size;
1540 if (crtc->fb)
1541 crtc_resp->fb_id = crtc->fb->base.id;
1542 else
1543 crtc_resp->fb_id = 0;
1544
1545 if (crtc->enabled) {
1546
1547 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1548 crtc_resp->mode_valid = 1;
1549
1550 } else {
1551 crtc_resp->mode_valid = 0;
1552 }
1553
1554out:
Daniel Vetter84849902012-12-02 00:28:11 +01001555 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001556 return ret;
1557}
1558
1559/**
1560 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001561 * @dev: drm device for the ioctl
1562 * @data: data pointer for the ioctl
1563 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001564 *
Dave Airlief453ba02008-11-07 14:05:41 -08001565 * Construct a connector configuration structure to return to the user.
1566 *
1567 * Called by the user via ioctl.
1568 *
1569 * RETURNS:
1570 * Zero on success, errno on failure.
1571 */
1572int drm_mode_getconnector(struct drm_device *dev, void *data,
1573 struct drm_file *file_priv)
1574{
1575 struct drm_mode_get_connector *out_resp = data;
1576 struct drm_mode_object *obj;
1577 struct drm_connector *connector;
1578 struct drm_display_mode *mode;
1579 int mode_count = 0;
1580 int props_count = 0;
1581 int encoders_count = 0;
1582 int ret = 0;
1583 int copied = 0;
1584 int i;
1585 struct drm_mode_modeinfo u_mode;
1586 struct drm_mode_modeinfo __user *mode_ptr;
1587 uint32_t __user *prop_ptr;
1588 uint64_t __user *prop_values;
1589 uint32_t __user *encoder_ptr;
1590
Dave Airliefb3b06c2011-02-08 13:55:21 +10001591 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1592 return -EINVAL;
1593
Dave Airlief453ba02008-11-07 14:05:41 -08001594 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1595
Jerome Glisse94401062010-07-15 15:43:25 -04001596 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001597
Daniel Vetter84849902012-12-02 00:28:11 +01001598 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001599
1600 obj = drm_mode_object_find(dev, out_resp->connector_id,
1601 DRM_MODE_OBJECT_CONNECTOR);
1602 if (!obj) {
1603 ret = -EINVAL;
1604 goto out;
1605 }
1606 connector = obj_to_connector(obj);
1607
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001608 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001609
1610 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1611 if (connector->encoder_ids[i] != 0) {
1612 encoders_count++;
1613 }
1614 }
1615
1616 if (out_resp->count_modes == 0) {
1617 connector->funcs->fill_modes(connector,
1618 dev->mode_config.max_width,
1619 dev->mode_config.max_height);
1620 }
1621
1622 /* delayed so we get modes regardless of pre-fill_modes state */
1623 list_for_each_entry(mode, &connector->modes, head)
1624 mode_count++;
1625
1626 out_resp->connector_id = connector->base.id;
1627 out_resp->connector_type = connector->connector_type;
1628 out_resp->connector_type_id = connector->connector_type_id;
1629 out_resp->mm_width = connector->display_info.width_mm;
1630 out_resp->mm_height = connector->display_info.height_mm;
1631 out_resp->subpixel = connector->display_info.subpixel_order;
1632 out_resp->connection = connector->status;
1633 if (connector->encoder)
1634 out_resp->encoder_id = connector->encoder->base.id;
1635 else
1636 out_resp->encoder_id = 0;
1637
1638 /*
1639 * This ioctl is called twice, once to determine how much space is
1640 * needed, and the 2nd time to fill it.
1641 */
1642 if ((out_resp->count_modes >= mode_count) && mode_count) {
1643 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001644 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001645 list_for_each_entry(mode, &connector->modes, head) {
1646 drm_crtc_convert_to_umode(&u_mode, mode);
1647 if (copy_to_user(mode_ptr + copied,
1648 &u_mode, sizeof(u_mode))) {
1649 ret = -EFAULT;
1650 goto out;
1651 }
1652 copied++;
1653 }
1654 }
1655 out_resp->count_modes = mode_count;
1656
1657 if ((out_resp->count_props >= props_count) && props_count) {
1658 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001659 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1660 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001661 for (i = 0; i < connector->properties.count; i++) {
1662 if (put_user(connector->properties.ids[i],
1663 prop_ptr + copied)) {
1664 ret = -EFAULT;
1665 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001666 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001667
1668 if (put_user(connector->properties.values[i],
1669 prop_values + copied)) {
1670 ret = -EFAULT;
1671 goto out;
1672 }
1673 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001674 }
1675 }
1676 out_resp->count_props = props_count;
1677
1678 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1679 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001680 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001681 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1682 if (connector->encoder_ids[i] != 0) {
1683 if (put_user(connector->encoder_ids[i],
1684 encoder_ptr + copied)) {
1685 ret = -EFAULT;
1686 goto out;
1687 }
1688 copied++;
1689 }
1690 }
1691 }
1692 out_resp->count_encoders = encoders_count;
1693
1694out:
Daniel Vetter84849902012-12-02 00:28:11 +01001695 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001696 return ret;
1697}
1698
1699int drm_mode_getencoder(struct drm_device *dev, void *data,
1700 struct drm_file *file_priv)
1701{
1702 struct drm_mode_get_encoder *enc_resp = data;
1703 struct drm_mode_object *obj;
1704 struct drm_encoder *encoder;
1705 int ret = 0;
1706
Dave Airliefb3b06c2011-02-08 13:55:21 +10001707 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1708 return -EINVAL;
1709
Daniel Vetter84849902012-12-02 00:28:11 +01001710 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001711 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1712 DRM_MODE_OBJECT_ENCODER);
1713 if (!obj) {
1714 ret = -EINVAL;
1715 goto out;
1716 }
1717 encoder = obj_to_encoder(obj);
1718
1719 if (encoder->crtc)
1720 enc_resp->crtc_id = encoder->crtc->base.id;
1721 else
1722 enc_resp->crtc_id = 0;
1723 enc_resp->encoder_type = encoder->encoder_type;
1724 enc_resp->encoder_id = encoder->base.id;
1725 enc_resp->possible_crtcs = encoder->possible_crtcs;
1726 enc_resp->possible_clones = encoder->possible_clones;
1727
1728out:
Daniel Vetter84849902012-12-02 00:28:11 +01001729 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001730 return ret;
1731}
1732
1733/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001734 * drm_mode_getplane_res - get plane info
1735 * @dev: DRM device
1736 * @data: ioctl data
1737 * @file_priv: DRM file info
1738 *
1739 * Return an plane count and set of IDs.
1740 */
1741int drm_mode_getplane_res(struct drm_device *dev, void *data,
1742 struct drm_file *file_priv)
1743{
1744 struct drm_mode_get_plane_res *plane_resp = data;
1745 struct drm_mode_config *config;
1746 struct drm_plane *plane;
1747 uint32_t __user *plane_ptr;
1748 int copied = 0, ret = 0;
1749
1750 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1751 return -EINVAL;
1752
Daniel Vetter84849902012-12-02 00:28:11 +01001753 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001754 config = &dev->mode_config;
1755
1756 /*
1757 * This ioctl is called twice, once to determine how much space is
1758 * needed, and the 2nd time to fill it.
1759 */
1760 if (config->num_plane &&
1761 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001762 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001763
1764 list_for_each_entry(plane, &config->plane_list, head) {
1765 if (put_user(plane->base.id, plane_ptr + copied)) {
1766 ret = -EFAULT;
1767 goto out;
1768 }
1769 copied++;
1770 }
1771 }
1772 plane_resp->count_planes = config->num_plane;
1773
1774out:
Daniel Vetter84849902012-12-02 00:28:11 +01001775 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001776 return ret;
1777}
1778
1779/**
1780 * drm_mode_getplane - get plane info
1781 * @dev: DRM device
1782 * @data: ioctl data
1783 * @file_priv: DRM file info
1784 *
1785 * Return plane info, including formats supported, gamma size, any
1786 * current fb, etc.
1787 */
1788int drm_mode_getplane(struct drm_device *dev, void *data,
1789 struct drm_file *file_priv)
1790{
1791 struct drm_mode_get_plane *plane_resp = data;
1792 struct drm_mode_object *obj;
1793 struct drm_plane *plane;
1794 uint32_t __user *format_ptr;
1795 int ret = 0;
1796
1797 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1798 return -EINVAL;
1799
Daniel Vetter84849902012-12-02 00:28:11 +01001800 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001801 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1802 DRM_MODE_OBJECT_PLANE);
1803 if (!obj) {
1804 ret = -ENOENT;
1805 goto out;
1806 }
1807 plane = obj_to_plane(obj);
1808
1809 if (plane->crtc)
1810 plane_resp->crtc_id = plane->crtc->base.id;
1811 else
1812 plane_resp->crtc_id = 0;
1813
1814 if (plane->fb)
1815 plane_resp->fb_id = plane->fb->base.id;
1816 else
1817 plane_resp->fb_id = 0;
1818
1819 plane_resp->plane_id = plane->base.id;
1820 plane_resp->possible_crtcs = plane->possible_crtcs;
1821 plane_resp->gamma_size = plane->gamma_size;
1822
1823 /*
1824 * This ioctl is called twice, once to determine how much space is
1825 * needed, and the 2nd time to fill it.
1826 */
1827 if (plane->format_count &&
1828 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001829 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001830 if (copy_to_user(format_ptr,
1831 plane->format_types,
1832 sizeof(uint32_t) * plane->format_count)) {
1833 ret = -EFAULT;
1834 goto out;
1835 }
1836 }
1837 plane_resp->count_format_types = plane->format_count;
1838
1839out:
Daniel Vetter84849902012-12-02 00:28:11 +01001840 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001841 return ret;
1842}
1843
1844/**
1845 * drm_mode_setplane - set up or tear down an plane
1846 * @dev: DRM device
1847 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001848 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001849 *
1850 * Set plane info, including placement, fb, scaling, and other factors.
1851 * Or pass a NULL fb to disable.
1852 */
1853int drm_mode_setplane(struct drm_device *dev, void *data,
1854 struct drm_file *file_priv)
1855{
1856 struct drm_mode_set_plane *plane_req = data;
1857 struct drm_mode_object *obj;
1858 struct drm_plane *plane;
1859 struct drm_crtc *crtc;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001860 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001861 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001862 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001863 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001864
1865 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1866 return -EINVAL;
1867
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001868 /*
1869 * First, find the plane, crtc, and fb objects. If not available,
1870 * we don't bother to call the driver.
1871 */
1872 obj = drm_mode_object_find(dev, plane_req->plane_id,
1873 DRM_MODE_OBJECT_PLANE);
1874 if (!obj) {
1875 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1876 plane_req->plane_id);
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001877 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001878 }
1879 plane = obj_to_plane(obj);
1880
1881 /* No fb means shut it down */
1882 if (!plane_req->fb_id) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001883 drm_modeset_lock_all(dev);
1884 old_fb = plane->fb;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001885 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001886 plane->crtc = NULL;
1887 plane->fb = NULL;
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001888 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001889 goto out;
1890 }
1891
1892 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1893 DRM_MODE_OBJECT_CRTC);
1894 if (!obj) {
1895 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1896 plane_req->crtc_id);
1897 ret = -ENOENT;
1898 goto out;
1899 }
1900 crtc = obj_to_crtc(obj);
1901
Daniel Vetter786b99e2012-12-02 21:53:40 +01001902 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1903 if (!fb) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001904 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1905 plane_req->fb_id);
1906 ret = -ENOENT;
1907 goto out;
1908 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001909
Ville Syrjälä62443be2011-12-20 00:06:47 +02001910 /* Check whether this plane supports the fb pixel format. */
1911 for (i = 0; i < plane->format_count; i++)
1912 if (fb->pixel_format == plane->format_types[i])
1913 break;
1914 if (i == plane->format_count) {
1915 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1916 ret = -EINVAL;
1917 goto out;
1918 }
1919
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001920 fb_width = fb->width << 16;
1921 fb_height = fb->height << 16;
1922
1923 /* Make sure source coordinates are inside the fb. */
1924 if (plane_req->src_w > fb_width ||
1925 plane_req->src_x > fb_width - plane_req->src_w ||
1926 plane_req->src_h > fb_height ||
1927 plane_req->src_y > fb_height - plane_req->src_h) {
1928 DRM_DEBUG_KMS("Invalid source coordinates "
1929 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1930 plane_req->src_w >> 16,
1931 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1932 plane_req->src_h >> 16,
1933 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1934 plane_req->src_x >> 16,
1935 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1936 plane_req->src_y >> 16,
1937 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1938 ret = -ENOSPC;
1939 goto out;
1940 }
1941
Ville Syrjälä687a0402011-12-20 00:06:45 +02001942 /* Give drivers some help against integer overflows */
1943 if (plane_req->crtc_w > INT_MAX ||
1944 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1945 plane_req->crtc_h > INT_MAX ||
1946 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1947 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1948 plane_req->crtc_w, plane_req->crtc_h,
1949 plane_req->crtc_x, plane_req->crtc_y);
1950 ret = -ERANGE;
1951 goto out;
1952 }
1953
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001954 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001955 ret = plane->funcs->update_plane(plane, crtc, fb,
1956 plane_req->crtc_x, plane_req->crtc_y,
1957 plane_req->crtc_w, plane_req->crtc_h,
1958 plane_req->src_x, plane_req->src_y,
1959 plane_req->src_w, plane_req->src_h);
1960 if (!ret) {
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001961 old_fb = plane->fb;
1962 fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001963 plane->crtc = crtc;
1964 plane->fb = fb;
1965 }
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001966 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001967
1968out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01001969 if (fb)
1970 drm_framebuffer_unreference(fb);
1971 if (old_fb)
1972 drm_framebuffer_unreference(old_fb);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001973
1974 return ret;
1975}
1976
1977/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001978 * drm_mode_set_config_internal - helper to call ->set_config
1979 * @set: modeset config to set
1980 *
1981 * This is a little helper to wrap internal calls to the ->set_config driver
1982 * interface. The only thing it adds is correct refcounting dance.
1983 */
1984int drm_mode_set_config_internal(struct drm_mode_set *set)
1985{
1986 struct drm_crtc *crtc = set->crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01001987 struct drm_framebuffer *fb, *old_fb;
1988 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001989
Daniel Vetterb0d12322012-12-11 01:07:12 +01001990 old_fb = crtc->fb;
1991 fb = set->fb;
1992
1993 ret = crtc->funcs->set_config(set);
1994 if (ret == 0) {
1995 if (old_fb)
1996 drm_framebuffer_unreference(old_fb);
1997 if (fb)
1998 drm_framebuffer_reference(fb);
1999 }
2000
2001 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002002}
2003EXPORT_SYMBOL(drm_mode_set_config_internal);
2004
2005/**
Dave Airlief453ba02008-11-07 14:05:41 -08002006 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002007 * @dev: drm device for the ioctl
2008 * @data: data pointer for the ioctl
2009 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002010 *
Dave Airlief453ba02008-11-07 14:05:41 -08002011 * Build a new CRTC configuration based on user request.
2012 *
2013 * Called by the user via ioctl.
2014 *
2015 * RETURNS:
2016 * Zero on success, errno on failure.
2017 */
2018int drm_mode_setcrtc(struct drm_device *dev, void *data,
2019 struct drm_file *file_priv)
2020{
2021 struct drm_mode_config *config = &dev->mode_config;
2022 struct drm_mode_crtc *crtc_req = data;
2023 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002024 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002025 struct drm_connector **connector_set = NULL, *connector;
2026 struct drm_framebuffer *fb = NULL;
2027 struct drm_display_mode *mode = NULL;
2028 struct drm_mode_set set;
2029 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002030 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002031 int i;
2032
Dave Airliefb3b06c2011-02-08 13:55:21 +10002033 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2034 return -EINVAL;
2035
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002036 /* For some reason crtc x/y offsets are signed internally. */
2037 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
2038 return -ERANGE;
2039
Daniel Vetter84849902012-12-02 00:28:11 +01002040 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002041 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
2042 DRM_MODE_OBJECT_CRTC);
2043 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002044 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002045 ret = -EINVAL;
2046 goto out;
2047 }
2048 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002049 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002050
2051 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00002052 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08002053 /* If we have a mode we need a framebuffer. */
2054 /* If we pass -1, set the mode with the currently bound fb */
2055 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002056 if (!crtc->fb) {
2057 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2058 ret = -EINVAL;
2059 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002060 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002061 fb = crtc->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002062 /* Make refcounting symmetric with the lookup path. */
2063 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002064 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002065 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2066 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002067 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2068 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002069 ret = -EINVAL;
2070 goto out;
2071 }
Dave Airlief453ba02008-11-07 14:05:41 -08002072 }
2073
2074 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002075 if (!mode) {
2076 ret = -ENOMEM;
2077 goto out;
2078 }
2079
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002080 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
2081 if (ret) {
2082 DRM_DEBUG_KMS("Invalid mode\n");
2083 goto out;
2084 }
2085
Dave Airlief453ba02008-11-07 14:05:41 -08002086 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002087
Rob Clark7c80e122012-09-04 16:35:56 +00002088 hdisplay = mode->hdisplay;
2089 vdisplay = mode->vdisplay;
2090
2091 if (crtc->invert_dimensions)
2092 swap(hdisplay, vdisplay);
2093
2094 if (hdisplay > fb->width ||
2095 vdisplay > fb->height ||
2096 crtc_req->x > fb->width - hdisplay ||
2097 crtc_req->y > fb->height - vdisplay) {
2098 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
2099 fb->width, fb->height,
2100 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
2101 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002102 ret = -ENOSPC;
2103 goto out;
2104 }
Dave Airlief453ba02008-11-07 14:05:41 -08002105 }
2106
2107 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002108 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002109 ret = -EINVAL;
2110 goto out;
2111 }
2112
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002113 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002114 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002115 crtc_req->count_connectors);
2116 ret = -EINVAL;
2117 goto out;
2118 }
2119
2120 if (crtc_req->count_connectors > 0) {
2121 u32 out_id;
2122
2123 /* Avoid unbounded kernel memory allocation */
2124 if (crtc_req->count_connectors > config->num_connector) {
2125 ret = -EINVAL;
2126 goto out;
2127 }
2128
2129 connector_set = kmalloc(crtc_req->count_connectors *
2130 sizeof(struct drm_connector *),
2131 GFP_KERNEL);
2132 if (!connector_set) {
2133 ret = -ENOMEM;
2134 goto out;
2135 }
2136
2137 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002138 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002139 if (get_user(out_id, &set_connectors_ptr[i])) {
2140 ret = -EFAULT;
2141 goto out;
2142 }
2143
2144 obj = drm_mode_object_find(dev, out_id,
2145 DRM_MODE_OBJECT_CONNECTOR);
2146 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002147 DRM_DEBUG_KMS("Connector id %d unknown\n",
2148 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002149 ret = -EINVAL;
2150 goto out;
2151 }
2152 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002153 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2154 connector->base.id,
2155 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002156
2157 connector_set[i] = connector;
2158 }
2159 }
2160
2161 set.crtc = crtc;
2162 set.x = crtc_req->x;
2163 set.y = crtc_req->y;
2164 set.mode = mode;
2165 set.connectors = connector_set;
2166 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002167 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002168 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002169
2170out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002171 if (fb)
2172 drm_framebuffer_unreference(fb);
2173
Dave Airlief453ba02008-11-07 14:05:41 -08002174 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002175 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002176 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002177 return ret;
2178}
2179
2180int drm_mode_cursor_ioctl(struct drm_device *dev,
2181 void *data, struct drm_file *file_priv)
2182{
2183 struct drm_mode_cursor *req = data;
2184 struct drm_mode_object *obj;
2185 struct drm_crtc *crtc;
2186 int ret = 0;
2187
Dave Airliefb3b06c2011-02-08 13:55:21 +10002188 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2189 return -EINVAL;
2190
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002191 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002192 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002193
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002194 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002195 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002196 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Daniel Vetterdac35662012-12-02 15:24:10 +01002197 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002198 }
2199 crtc = obj_to_crtc(obj);
2200
Daniel Vetterdac35662012-12-02 15:24:10 +01002201 mutex_lock(&crtc->mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08002202 if (req->flags & DRM_MODE_CURSOR_BO) {
2203 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002204 ret = -ENXIO;
2205 goto out;
2206 }
2207 /* Turns off the cursor if handle is 0 */
2208 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2209 req->width, req->height);
2210 }
2211
2212 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2213 if (crtc->funcs->cursor_move) {
2214 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2215 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002216 ret = -EFAULT;
2217 goto out;
2218 }
2219 }
2220out:
Daniel Vetterdac35662012-12-02 15:24:10 +01002221 mutex_unlock(&crtc->mutex);
2222
Dave Airlief453ba02008-11-07 14:05:41 -08002223 return ret;
2224}
2225
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002226/* Original addfb only supported RGB formats, so figure out which one */
2227uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2228{
2229 uint32_t fmt;
2230
2231 switch (bpp) {
2232 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002233 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002234 break;
2235 case 16:
2236 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002237 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002238 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002239 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002240 break;
2241 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002242 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002243 break;
2244 case 32:
2245 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002246 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002247 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002248 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002249 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002250 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002251 break;
2252 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002253 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2254 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002255 break;
2256 }
2257
2258 return fmt;
2259}
2260EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2261
Dave Airlief453ba02008-11-07 14:05:41 -08002262/**
2263 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002264 * @dev: drm device for the ioctl
2265 * @data: data pointer for the ioctl
2266 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002267 *
Dave Airlief453ba02008-11-07 14:05:41 -08002268 * Add a new FB to the specified CRTC, given a user request.
2269 *
2270 * Called by the user via ioctl.
2271 *
2272 * RETURNS:
2273 * Zero on success, errno on failure.
2274 */
2275int drm_mode_addfb(struct drm_device *dev,
2276 void *data, struct drm_file *file_priv)
2277{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002278 struct drm_mode_fb_cmd *or = data;
2279 struct drm_mode_fb_cmd2 r = {};
2280 struct drm_mode_config *config = &dev->mode_config;
2281 struct drm_framebuffer *fb;
2282 int ret = 0;
2283
2284 /* Use new struct with format internally */
2285 r.fb_id = or->fb_id;
2286 r.width = or->width;
2287 r.height = or->height;
2288 r.pitches[0] = or->pitch;
2289 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2290 r.handles[0] = or->handle;
2291
2292 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2293 return -EINVAL;
2294
Jesse Barnesacb4b992011-11-07 12:03:23 -08002295 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002296 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002297
2298 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002299 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002300
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002301 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2302 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002303 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002304 drm_modeset_unlock_all(dev);
2305 return PTR_ERR(fb);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002306 }
2307
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002308 mutex_lock(&file_priv->fbs_lock);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002309 or->fb_id = fb->base.id;
2310 list_add(&fb->filp_head, &file_priv->fbs);
2311 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002312 mutex_unlock(&file_priv->fbs_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002313
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002314 return ret;
2315}
2316
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002317static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002318{
2319 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2320
2321 switch (format) {
2322 case DRM_FORMAT_C8:
2323 case DRM_FORMAT_RGB332:
2324 case DRM_FORMAT_BGR233:
2325 case DRM_FORMAT_XRGB4444:
2326 case DRM_FORMAT_XBGR4444:
2327 case DRM_FORMAT_RGBX4444:
2328 case DRM_FORMAT_BGRX4444:
2329 case DRM_FORMAT_ARGB4444:
2330 case DRM_FORMAT_ABGR4444:
2331 case DRM_FORMAT_RGBA4444:
2332 case DRM_FORMAT_BGRA4444:
2333 case DRM_FORMAT_XRGB1555:
2334 case DRM_FORMAT_XBGR1555:
2335 case DRM_FORMAT_RGBX5551:
2336 case DRM_FORMAT_BGRX5551:
2337 case DRM_FORMAT_ARGB1555:
2338 case DRM_FORMAT_ABGR1555:
2339 case DRM_FORMAT_RGBA5551:
2340 case DRM_FORMAT_BGRA5551:
2341 case DRM_FORMAT_RGB565:
2342 case DRM_FORMAT_BGR565:
2343 case DRM_FORMAT_RGB888:
2344 case DRM_FORMAT_BGR888:
2345 case DRM_FORMAT_XRGB8888:
2346 case DRM_FORMAT_XBGR8888:
2347 case DRM_FORMAT_RGBX8888:
2348 case DRM_FORMAT_BGRX8888:
2349 case DRM_FORMAT_ARGB8888:
2350 case DRM_FORMAT_ABGR8888:
2351 case DRM_FORMAT_RGBA8888:
2352 case DRM_FORMAT_BGRA8888:
2353 case DRM_FORMAT_XRGB2101010:
2354 case DRM_FORMAT_XBGR2101010:
2355 case DRM_FORMAT_RGBX1010102:
2356 case DRM_FORMAT_BGRX1010102:
2357 case DRM_FORMAT_ARGB2101010:
2358 case DRM_FORMAT_ABGR2101010:
2359 case DRM_FORMAT_RGBA1010102:
2360 case DRM_FORMAT_BGRA1010102:
2361 case DRM_FORMAT_YUYV:
2362 case DRM_FORMAT_YVYU:
2363 case DRM_FORMAT_UYVY:
2364 case DRM_FORMAT_VYUY:
2365 case DRM_FORMAT_AYUV:
2366 case DRM_FORMAT_NV12:
2367 case DRM_FORMAT_NV21:
2368 case DRM_FORMAT_NV16:
2369 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002370 case DRM_FORMAT_NV24:
2371 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002372 case DRM_FORMAT_YUV410:
2373 case DRM_FORMAT_YVU410:
2374 case DRM_FORMAT_YUV411:
2375 case DRM_FORMAT_YVU411:
2376 case DRM_FORMAT_YUV420:
2377 case DRM_FORMAT_YVU420:
2378 case DRM_FORMAT_YUV422:
2379 case DRM_FORMAT_YVU422:
2380 case DRM_FORMAT_YUV444:
2381 case DRM_FORMAT_YVU444:
2382 return 0;
2383 default:
2384 return -EINVAL;
2385 }
2386}
2387
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002388static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002389{
2390 int ret, hsub, vsub, num_planes, i;
2391
2392 ret = format_check(r);
2393 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002394 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002395 return ret;
2396 }
2397
2398 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2399 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2400 num_planes = drm_format_num_planes(r->pixel_format);
2401
2402 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002403 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002404 return -EINVAL;
2405 }
2406
2407 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002408 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002409 return -EINVAL;
2410 }
2411
2412 for (i = 0; i < num_planes; i++) {
2413 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002414 unsigned int height = r->height / (i != 0 ? vsub : 1);
2415 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002416
2417 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002418 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002419 return -EINVAL;
2420 }
2421
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002422 if ((uint64_t) width * cpp > UINT_MAX)
2423 return -ERANGE;
2424
2425 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2426 return -ERANGE;
2427
2428 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002429 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002430 return -EINVAL;
2431 }
2432 }
2433
2434 return 0;
2435}
2436
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002437/**
2438 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002439 * @dev: drm device for the ioctl
2440 * @data: data pointer for the ioctl
2441 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002442 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002443 * Add a new FB to the specified CRTC, given a user request with format.
2444 *
2445 * Called by the user via ioctl.
2446 *
2447 * RETURNS:
2448 * Zero on success, errno on failure.
2449 */
2450int drm_mode_addfb2(struct drm_device *dev,
2451 void *data, struct drm_file *file_priv)
2452{
2453 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002454 struct drm_mode_config *config = &dev->mode_config;
2455 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002456 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002457
Dave Airliefb3b06c2011-02-08 13:55:21 +10002458 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2459 return -EINVAL;
2460
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002461 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2462 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2463 return -EINVAL;
2464 }
2465
Dave Airlief453ba02008-11-07 14:05:41 -08002466 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002467 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002468 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002469 return -EINVAL;
2470 }
2471 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002472 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002473 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002474 return -EINVAL;
2475 }
2476
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002477 ret = framebuffer_check(r);
2478 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002479 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002480
Dave Airlief453ba02008-11-07 14:05:41 -08002481 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002482 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002483 DRM_DEBUG_KMS("could not create framebuffer\n");
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002484 drm_modeset_unlock_all(dev);
2485 return PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002486 }
2487
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002488 mutex_lock(&file_priv->fbs_lock);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002489 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002490 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002491 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002492 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002493
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002494
Dave Airlief453ba02008-11-07 14:05:41 -08002495 return ret;
2496}
2497
2498/**
2499 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002500 * @dev: drm device for the ioctl
2501 * @data: data pointer for the ioctl
2502 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002503 *
Dave Airlief453ba02008-11-07 14:05:41 -08002504 * Remove the FB specified by the user.
2505 *
2506 * Called by the user via ioctl.
2507 *
2508 * RETURNS:
2509 * Zero on success, errno on failure.
2510 */
2511int drm_mode_rmfb(struct drm_device *dev,
2512 void *data, struct drm_file *file_priv)
2513{
Dave Airlief453ba02008-11-07 14:05:41 -08002514 struct drm_framebuffer *fb = NULL;
2515 struct drm_framebuffer *fbl = NULL;
2516 uint32_t *id = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002517 int found = 0;
2518
Dave Airliefb3b06c2011-02-08 13:55:21 +10002519 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2520 return -EINVAL;
2521
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002522 mutex_lock(&file_priv->fbs_lock);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002523 mutex_lock(&dev->mode_config.fb_lock);
2524 fb = __drm_framebuffer_lookup(dev, *id);
2525 if (!fb)
2526 goto fail_lookup;
2527
Dave Airlief453ba02008-11-07 14:05:41 -08002528 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2529 if (fb == fbl)
2530 found = 1;
Daniel Vetter2b677e82012-12-10 21:16:05 +01002531 if (!found)
2532 goto fail_lookup;
2533
2534 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2535 __drm_framebuffer_unregister(dev, fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002536
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002537 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002538 mutex_unlock(&dev->mode_config.fb_lock);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002539 mutex_unlock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002540
Daniel Vetter2b677e82012-12-10 21:16:05 +01002541 drm_modeset_lock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002542 drm_framebuffer_remove(fb);
Daniel Vetter84849902012-12-02 00:28:11 +01002543 drm_modeset_unlock_all(dev);
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002544
Daniel Vetter2b677e82012-12-10 21:16:05 +01002545 return 0;
2546
2547fail_lookup:
2548 mutex_unlock(&dev->mode_config.fb_lock);
2549 mutex_unlock(&file_priv->fbs_lock);
2550
2551 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002552}
2553
2554/**
2555 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002556 * @dev: drm device for the ioctl
2557 * @data: data pointer for the ioctl
2558 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002559 *
Dave Airlief453ba02008-11-07 14:05:41 -08002560 * Lookup the FB given its ID and return info about it.
2561 *
2562 * Called by the user via ioctl.
2563 *
2564 * RETURNS:
2565 * Zero on success, errno on failure.
2566 */
2567int drm_mode_getfb(struct drm_device *dev,
2568 void *data, struct drm_file *file_priv)
2569{
2570 struct drm_mode_fb_cmd *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002571 struct drm_framebuffer *fb;
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002572 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002573
Dave Airliefb3b06c2011-02-08 13:55:21 +10002574 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2575 return -EINVAL;
2576
Daniel Vetter786b99e2012-12-02 21:53:40 +01002577 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002578 if (!fb)
2579 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002580
2581 r->height = fb->height;
2582 r->width = fb->width;
2583 r->depth = fb->depth;
2584 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002585 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002586 if (fb->funcs->create_handle)
2587 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2588 else
2589 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002590
Daniel Vetter58c0dca2012-12-13 23:06:08 +01002591 drm_framebuffer_unreference(fb);
2592
Dave Airlief453ba02008-11-07 14:05:41 -08002593 return ret;
2594}
2595
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002596int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2597 void *data, struct drm_file *file_priv)
2598{
2599 struct drm_clip_rect __user *clips_ptr;
2600 struct drm_clip_rect *clips = NULL;
2601 struct drm_mode_fb_dirty_cmd *r = data;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002602 struct drm_framebuffer *fb;
2603 unsigned flags;
2604 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002605 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002606
Dave Airliefb3b06c2011-02-08 13:55:21 +10002607 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2608 return -EINVAL;
2609
Daniel Vetter786b99e2012-12-02 21:53:40 +01002610 fb = drm_framebuffer_lookup(dev, r->fb_id);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002611 if (!fb)
2612 return -EINVAL;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002613
2614 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002615 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002616
2617 if (!num_clips != !clips_ptr) {
2618 ret = -EINVAL;
2619 goto out_err1;
2620 }
2621
2622 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2623
2624 /* If userspace annotates copy, clips must come in pairs */
2625 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2626 ret = -EINVAL;
2627 goto out_err1;
2628 }
2629
2630 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002631 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2632 ret = -EINVAL;
2633 goto out_err1;
2634 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002635 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2636 if (!clips) {
2637 ret = -ENOMEM;
2638 goto out_err1;
2639 }
2640
2641 ret = copy_from_user(clips, clips_ptr,
2642 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002643 if (ret) {
2644 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002645 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002646 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002647 }
2648
2649 if (fb->funcs->dirty) {
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002650 drm_modeset_lock_all(dev);
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002651 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2652 clips, num_clips);
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002653 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002654 } else {
2655 ret = -ENOSYS;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002656 }
2657
2658out_err2:
2659 kfree(clips);
2660out_err1:
Daniel Vetter4ccf0972012-12-11 00:38:18 +01002661 drm_framebuffer_unreference(fb);
2662
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002663 return ret;
2664}
2665
2666
Dave Airlief453ba02008-11-07 14:05:41 -08002667/**
2668 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002669 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002670 *
Dave Airlief453ba02008-11-07 14:05:41 -08002671 * Destroy all the FBs associated with @filp.
2672 *
2673 * Called by the user via ioctl.
2674 *
2675 * RETURNS:
2676 * Zero on success, errno on failure.
2677 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002678void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002679{
Dave Airlief453ba02008-11-07 14:05:41 -08002680 struct drm_device *dev = priv->minor->dev;
2681 struct drm_framebuffer *fb, *tfb;
2682
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002683 mutex_lock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002684 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Daniel Vetter2b677e82012-12-10 21:16:05 +01002685
2686 mutex_lock(&dev->mode_config.fb_lock);
2687 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
2688 __drm_framebuffer_unregister(dev, fb);
2689 mutex_unlock(&dev->mode_config.fb_lock);
2690
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002691 list_del_init(&fb->filp_head);
Daniel Vetter2b677e82012-12-10 21:16:05 +01002692
2693 /* This will also drop the fpriv->fbs reference. */
Daniel Vetter7d331592012-12-10 23:44:22 +01002694 drm_modeset_lock_all(dev);
Rob Clarkf7eff602012-09-05 21:48:38 +00002695 drm_framebuffer_remove(fb);
Daniel Vetter7d331592012-12-10 23:44:22 +01002696 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002697 }
Daniel Vetter4b096ac2012-12-10 21:19:18 +01002698 mutex_unlock(&priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08002699}
2700
2701/**
2702 * drm_mode_attachmode - add a mode to the user mode list
2703 * @dev: DRM device
2704 * @connector: connector to add the mode to
2705 * @mode: mode to add
2706 *
2707 * Add @mode to @connector's user mode list.
2708 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002709static void drm_mode_attachmode(struct drm_device *dev,
2710 struct drm_connector *connector,
2711 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002712{
Dave Airlief453ba02008-11-07 14:05:41 -08002713 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002714}
2715
2716int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002717 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002718{
2719 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002720 int ret = 0;
2721 struct drm_display_mode *dup_mode, *next;
2722 LIST_HEAD(list);
2723
Dave Airlief453ba02008-11-07 14:05:41 -08002724 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2725 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002726 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002727 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002728 dup_mode = drm_mode_duplicate(dev, mode);
2729 if (!dup_mode) {
2730 ret = -ENOMEM;
2731 goto out;
2732 }
2733 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002734 }
2735 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002736
2737 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2738 if (!connector->encoder)
2739 continue;
2740 if (connector->encoder->crtc == crtc)
2741 list_move_tail(list.next, &connector->user_modes);
2742 }
2743
2744 WARN_ON(!list_empty(&list));
2745
2746 out:
2747 list_for_each_entry_safe(dup_mode, next, &list, head)
2748 drm_mode_destroy(dev, dup_mode);
2749
2750 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002751}
2752EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2753
2754static int drm_mode_detachmode(struct drm_device *dev,
2755 struct drm_connector *connector,
2756 struct drm_display_mode *mode)
2757{
2758 int found = 0;
2759 int ret = 0;
2760 struct drm_display_mode *match_mode, *t;
2761
2762 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2763 if (drm_mode_equal(match_mode, mode)) {
2764 list_del(&match_mode->head);
2765 drm_mode_destroy(dev, match_mode);
2766 found = 1;
2767 break;
2768 }
2769 }
2770
2771 if (!found)
2772 ret = -EINVAL;
2773
2774 return ret;
2775}
2776
2777int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2778{
2779 struct drm_connector *connector;
2780
2781 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2782 drm_mode_detachmode(dev, connector, mode);
2783 }
2784 return 0;
2785}
2786EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2787
2788/**
2789 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002790 * @dev: drm device for the ioctl
2791 * @data: data pointer for the ioctl
2792 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002793 *
2794 * This attaches a user specified mode to an connector.
2795 * Called by the user via ioctl.
2796 *
2797 * RETURNS:
2798 * Zero on success, errno on failure.
2799 */
2800int drm_mode_attachmode_ioctl(struct drm_device *dev,
2801 void *data, struct drm_file *file_priv)
2802{
2803 struct drm_mode_mode_cmd *mode_cmd = data;
2804 struct drm_connector *connector;
2805 struct drm_display_mode *mode;
2806 struct drm_mode_object *obj;
2807 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002808 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002809
Dave Airliefb3b06c2011-02-08 13:55:21 +10002810 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2811 return -EINVAL;
2812
Daniel Vetter84849902012-12-02 00:28:11 +01002813 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002814
2815 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2816 if (!obj) {
2817 ret = -EINVAL;
2818 goto out;
2819 }
2820 connector = obj_to_connector(obj);
2821
2822 mode = drm_mode_create(dev);
2823 if (!mode) {
2824 ret = -ENOMEM;
2825 goto out;
2826 }
2827
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002828 ret = drm_crtc_convert_umode(mode, umode);
2829 if (ret) {
2830 DRM_DEBUG_KMS("Invalid mode\n");
2831 drm_mode_destroy(dev, mode);
2832 goto out;
2833 }
Dave Airlief453ba02008-11-07 14:05:41 -08002834
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002835 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002836out:
Daniel Vetter84849902012-12-02 00:28:11 +01002837 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002838 return ret;
2839}
2840
2841
2842/**
2843 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002844 * @dev: drm device for the ioctl
2845 * @data: data pointer for the ioctl
2846 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002847 *
2848 * Called by the user via ioctl.
2849 *
2850 * RETURNS:
2851 * Zero on success, errno on failure.
2852 */
2853int drm_mode_detachmode_ioctl(struct drm_device *dev,
2854 void *data, struct drm_file *file_priv)
2855{
2856 struct drm_mode_object *obj;
2857 struct drm_mode_mode_cmd *mode_cmd = data;
2858 struct drm_connector *connector;
2859 struct drm_display_mode mode;
2860 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002861 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002862
Dave Airliefb3b06c2011-02-08 13:55:21 +10002863 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2864 return -EINVAL;
2865
Daniel Vetter84849902012-12-02 00:28:11 +01002866 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002867
2868 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2869 if (!obj) {
2870 ret = -EINVAL;
2871 goto out;
2872 }
2873 connector = obj_to_connector(obj);
2874
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002875 ret = drm_crtc_convert_umode(&mode, umode);
2876 if (ret) {
2877 DRM_DEBUG_KMS("Invalid mode\n");
2878 goto out;
2879 }
2880
Dave Airlief453ba02008-11-07 14:05:41 -08002881 ret = drm_mode_detachmode(dev, connector, &mode);
2882out:
Daniel Vetter84849902012-12-02 00:28:11 +01002883 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002884 return ret;
2885}
2886
2887struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2888 const char *name, int num_values)
2889{
2890 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002891 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002892
2893 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2894 if (!property)
2895 return NULL;
2896
2897 if (num_values) {
2898 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2899 if (!property->values)
2900 goto fail;
2901 }
2902
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002903 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2904 if (ret)
2905 goto fail;
2906
Dave Airlief453ba02008-11-07 14:05:41 -08002907 property->flags = flags;
2908 property->num_values = num_values;
2909 INIT_LIST_HEAD(&property->enum_blob_list);
2910
Vinson Lee471dd2e2011-11-10 11:55:40 -08002911 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002912 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002913 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2914 }
Dave Airlief453ba02008-11-07 14:05:41 -08002915
2916 list_add_tail(&property->head, &dev->mode_config.property_list);
2917 return property;
2918fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002919 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002920 kfree(property);
2921 return NULL;
2922}
2923EXPORT_SYMBOL(drm_property_create);
2924
Sascha Hauer4a67d392012-02-06 10:58:17 +01002925struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2926 const char *name,
2927 const struct drm_prop_enum_list *props,
2928 int num_values)
2929{
2930 struct drm_property *property;
2931 int i, ret;
2932
2933 flags |= DRM_MODE_PROP_ENUM;
2934
2935 property = drm_property_create(dev, flags, name, num_values);
2936 if (!property)
2937 return NULL;
2938
2939 for (i = 0; i < num_values; i++) {
2940 ret = drm_property_add_enum(property, i,
2941 props[i].type,
2942 props[i].name);
2943 if (ret) {
2944 drm_property_destroy(dev, property);
2945 return NULL;
2946 }
2947 }
2948
2949 return property;
2950}
2951EXPORT_SYMBOL(drm_property_create_enum);
2952
Rob Clark49e27542012-05-17 02:23:26 -06002953struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2954 int flags, const char *name,
2955 const struct drm_prop_enum_list *props,
2956 int num_values)
2957{
2958 struct drm_property *property;
2959 int i, ret;
2960
2961 flags |= DRM_MODE_PROP_BITMASK;
2962
2963 property = drm_property_create(dev, flags, name, num_values);
2964 if (!property)
2965 return NULL;
2966
2967 for (i = 0; i < num_values; i++) {
2968 ret = drm_property_add_enum(property, i,
2969 props[i].type,
2970 props[i].name);
2971 if (ret) {
2972 drm_property_destroy(dev, property);
2973 return NULL;
2974 }
2975 }
2976
2977 return property;
2978}
2979EXPORT_SYMBOL(drm_property_create_bitmask);
2980
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002981struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2982 const char *name,
2983 uint64_t min, uint64_t max)
2984{
2985 struct drm_property *property;
2986
2987 flags |= DRM_MODE_PROP_RANGE;
2988
2989 property = drm_property_create(dev, flags, name, 2);
2990 if (!property)
2991 return NULL;
2992
2993 property->values[0] = min;
2994 property->values[1] = max;
2995
2996 return property;
2997}
2998EXPORT_SYMBOL(drm_property_create_range);
2999
Dave Airlief453ba02008-11-07 14:05:41 -08003000int drm_property_add_enum(struct drm_property *property, int index,
3001 uint64_t value, const char *name)
3002{
3003 struct drm_property_enum *prop_enum;
3004
Rob Clark49e27542012-05-17 02:23:26 -06003005 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
3006 return -EINVAL;
3007
3008 /*
3009 * Bitmask enum properties have the additional constraint of values
3010 * from 0 to 63
3011 */
3012 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003013 return -EINVAL;
3014
3015 if (!list_empty(&property->enum_blob_list)) {
3016 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3017 if (prop_enum->value == value) {
3018 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3019 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3020 return 0;
3021 }
3022 }
3023 }
3024
3025 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3026 if (!prop_enum)
3027 return -ENOMEM;
3028
3029 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3030 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3031 prop_enum->value = value;
3032
3033 property->values[index] = value;
3034 list_add_tail(&prop_enum->head, &property->enum_blob_list);
3035 return 0;
3036}
3037EXPORT_SYMBOL(drm_property_add_enum);
3038
3039void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3040{
3041 struct drm_property_enum *prop_enum, *pt;
3042
3043 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
3044 list_del(&prop_enum->head);
3045 kfree(prop_enum);
3046 }
3047
3048 if (property->num_values)
3049 kfree(property->values);
3050 drm_mode_object_put(dev, &property->base);
3051 list_del(&property->head);
3052 kfree(property);
3053}
3054EXPORT_SYMBOL(drm_property_destroy);
3055
Paulo Zanonic5431882012-05-15 18:09:02 -03003056void drm_object_attach_property(struct drm_mode_object *obj,
3057 struct drm_property *property,
3058 uint64_t init_val)
3059{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003060 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003061
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003062 if (count == DRM_OBJECT_MAX_PROPERTY) {
3063 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3064 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3065 "you see this message on the same object type.\n",
3066 obj->type);
3067 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003068 }
3069
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003070 obj->properties->ids[count] = property->base.id;
3071 obj->properties->values[count] = init_val;
3072 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003073}
3074EXPORT_SYMBOL(drm_object_attach_property);
3075
3076int drm_object_property_set_value(struct drm_mode_object *obj,
3077 struct drm_property *property, uint64_t val)
3078{
3079 int i;
3080
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003081 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003082 if (obj->properties->ids[i] == property->base.id) {
3083 obj->properties->values[i] = val;
3084 return 0;
3085 }
3086 }
3087
3088 return -EINVAL;
3089}
3090EXPORT_SYMBOL(drm_object_property_set_value);
3091
3092int drm_object_property_get_value(struct drm_mode_object *obj,
3093 struct drm_property *property, uint64_t *val)
3094{
3095 int i;
3096
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003097 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003098 if (obj->properties->ids[i] == property->base.id) {
3099 *val = obj->properties->values[i];
3100 return 0;
3101 }
3102 }
3103
3104 return -EINVAL;
3105}
3106EXPORT_SYMBOL(drm_object_property_get_value);
3107
Dave Airlief453ba02008-11-07 14:05:41 -08003108int drm_mode_getproperty_ioctl(struct drm_device *dev,
3109 void *data, struct drm_file *file_priv)
3110{
3111 struct drm_mode_object *obj;
3112 struct drm_mode_get_property *out_resp = data;
3113 struct drm_property *property;
3114 int enum_count = 0;
3115 int blob_count = 0;
3116 int value_count = 0;
3117 int ret = 0, i;
3118 int copied;
3119 struct drm_property_enum *prop_enum;
3120 struct drm_mode_property_enum __user *enum_ptr;
3121 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003122 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003123 uint64_t __user *values_ptr;
3124 uint32_t __user *blob_length_ptr;
3125
Dave Airliefb3b06c2011-02-08 13:55:21 +10003126 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3127 return -EINVAL;
3128
Daniel Vetter84849902012-12-02 00:28:11 +01003129 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003130 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3131 if (!obj) {
3132 ret = -EINVAL;
3133 goto done;
3134 }
3135 property = obj_to_property(obj);
3136
Rob Clark49e27542012-05-17 02:23:26 -06003137 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003138 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3139 enum_count++;
3140 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3141 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3142 blob_count++;
3143 }
3144
3145 value_count = property->num_values;
3146
3147 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3148 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3149 out_resp->flags = property->flags;
3150
3151 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003152 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003153 for (i = 0; i < value_count; i++) {
3154 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3155 ret = -EFAULT;
3156 goto done;
3157 }
3158 }
3159 }
3160 out_resp->count_values = value_count;
3161
Rob Clark49e27542012-05-17 02:23:26 -06003162 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003163 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3164 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003165 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003166 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3167
3168 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3169 ret = -EFAULT;
3170 goto done;
3171 }
3172
3173 if (copy_to_user(&enum_ptr[copied].name,
3174 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3175 ret = -EFAULT;
3176 goto done;
3177 }
3178 copied++;
3179 }
3180 }
3181 out_resp->count_enum_blobs = enum_count;
3182 }
3183
3184 if (property->flags & DRM_MODE_PROP_BLOB) {
3185 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3186 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003187 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3188 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003189
3190 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3191 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3192 ret = -EFAULT;
3193 goto done;
3194 }
3195
3196 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3197 ret = -EFAULT;
3198 goto done;
3199 }
3200
3201 copied++;
3202 }
3203 }
3204 out_resp->count_enum_blobs = blob_count;
3205 }
3206done:
Daniel Vetter84849902012-12-02 00:28:11 +01003207 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003208 return ret;
3209}
3210
3211static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3212 void *data)
3213{
3214 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003215 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003216
3217 if (!length || !data)
3218 return NULL;
3219
3220 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3221 if (!blob)
3222 return NULL;
3223
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003224 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3225 if (ret) {
3226 kfree(blob);
3227 return NULL;
3228 }
3229
Dave Airlief453ba02008-11-07 14:05:41 -08003230 blob->length = length;
3231
3232 memcpy(blob->data, data, length);
3233
Dave Airlief453ba02008-11-07 14:05:41 -08003234 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3235 return blob;
3236}
3237
3238static void drm_property_destroy_blob(struct drm_device *dev,
3239 struct drm_property_blob *blob)
3240{
3241 drm_mode_object_put(dev, &blob->base);
3242 list_del(&blob->head);
3243 kfree(blob);
3244}
3245
3246int drm_mode_getblob_ioctl(struct drm_device *dev,
3247 void *data, struct drm_file *file_priv)
3248{
3249 struct drm_mode_object *obj;
3250 struct drm_mode_get_blob *out_resp = data;
3251 struct drm_property_blob *blob;
3252 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003253 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003254
Dave Airliefb3b06c2011-02-08 13:55:21 +10003255 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3256 return -EINVAL;
3257
Daniel Vetter84849902012-12-02 00:28:11 +01003258 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003259 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3260 if (!obj) {
3261 ret = -EINVAL;
3262 goto done;
3263 }
3264 blob = obj_to_blob(obj);
3265
3266 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003267 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003268 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3269 ret = -EFAULT;
3270 goto done;
3271 }
3272 }
3273 out_resp->length = blob->length;
3274
3275done:
Daniel Vetter84849902012-12-02 00:28:11 +01003276 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003277 return ret;
3278}
3279
3280int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3281 struct edid *edid)
3282{
3283 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003284 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003285
3286 if (connector->edid_blob_ptr)
3287 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3288
3289 /* Delete edid, when there is none. */
3290 if (!edid) {
3291 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003292 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003293 return ret;
3294 }
3295
Adam Jackson7466f4c2010-03-29 21:43:23 +00003296 size = EDID_LENGTH * (1 + edid->extensions);
3297 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3298 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003299 if (!connector->edid_blob_ptr)
3300 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003301
Rob Clark58495562012-10-11 20:50:56 -05003302 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003303 dev->mode_config.edid_property,
3304 connector->edid_blob_ptr->base.id);
3305
3306 return ret;
3307}
3308EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3309
Paulo Zanoni26a34812012-05-15 18:08:59 -03003310static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003311 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003312{
3313 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3314 return false;
3315 if (property->flags & DRM_MODE_PROP_RANGE) {
3316 if (value < property->values[0] || value > property->values[1])
3317 return false;
3318 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003319 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3320 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003321 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003322 for (i = 0; i < property->num_values; i++)
3323 valid_mask |= (1ULL << property->values[i]);
3324 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003325 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3326 /* Only the driver knows */
3327 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003328 } else {
3329 int i;
3330 for (i = 0; i < property->num_values; i++)
3331 if (property->values[i] == value)
3332 return true;
3333 return false;
3334 }
3335}
3336
Dave Airlief453ba02008-11-07 14:05:41 -08003337int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3338 void *data, struct drm_file *file_priv)
3339{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003340 struct drm_mode_connector_set_property *conn_set_prop = data;
3341 struct drm_mode_obj_set_property obj_set_prop = {
3342 .value = conn_set_prop->value,
3343 .prop_id = conn_set_prop->prop_id,
3344 .obj_id = conn_set_prop->connector_id,
3345 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3346 };
Dave Airlief453ba02008-11-07 14:05:41 -08003347
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003348 /* It does all the locking and checking we need */
3349 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003350}
3351
Paulo Zanonic5431882012-05-15 18:09:02 -03003352static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3353 struct drm_property *property,
3354 uint64_t value)
3355{
3356 int ret = -EINVAL;
3357 struct drm_connector *connector = obj_to_connector(obj);
3358
3359 /* Do DPMS ourselves */
3360 if (property == connector->dev->mode_config.dpms_property) {
3361 if (connector->funcs->dpms)
3362 (*connector->funcs->dpms)(connector, (int)value);
3363 ret = 0;
3364 } else if (connector->funcs->set_property)
3365 ret = connector->funcs->set_property(connector, property, value);
3366
3367 /* store the property value if successful */
3368 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003369 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003370 return ret;
3371}
3372
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003373static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3374 struct drm_property *property,
3375 uint64_t value)
3376{
3377 int ret = -EINVAL;
3378 struct drm_crtc *crtc = obj_to_crtc(obj);
3379
3380 if (crtc->funcs->set_property)
3381 ret = crtc->funcs->set_property(crtc, property, value);
3382 if (!ret)
3383 drm_object_property_set_value(obj, property, value);
3384
3385 return ret;
3386}
3387
Rob Clark4d939142012-05-17 02:23:27 -06003388static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3389 struct drm_property *property,
3390 uint64_t value)
3391{
3392 int ret = -EINVAL;
3393 struct drm_plane *plane = obj_to_plane(obj);
3394
3395 if (plane->funcs->set_property)
3396 ret = plane->funcs->set_property(plane, property, value);
3397 if (!ret)
3398 drm_object_property_set_value(obj, property, value);
3399
3400 return ret;
3401}
3402
Paulo Zanonic5431882012-05-15 18:09:02 -03003403int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3404 struct drm_file *file_priv)
3405{
3406 struct drm_mode_obj_get_properties *arg = data;
3407 struct drm_mode_object *obj;
3408 int ret = 0;
3409 int i;
3410 int copied = 0;
3411 int props_count = 0;
3412 uint32_t __user *props_ptr;
3413 uint64_t __user *prop_values_ptr;
3414
3415 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3416 return -EINVAL;
3417
Daniel Vetter84849902012-12-02 00:28:11 +01003418 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003419
3420 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3421 if (!obj) {
3422 ret = -EINVAL;
3423 goto out;
3424 }
3425 if (!obj->properties) {
3426 ret = -EINVAL;
3427 goto out;
3428 }
3429
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003430 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003431
3432 /* This ioctl is called twice, once to determine how much space is
3433 * needed, and the 2nd time to fill it. */
3434 if ((arg->count_props >= props_count) && props_count) {
3435 copied = 0;
3436 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3437 prop_values_ptr = (uint64_t __user *)(unsigned long)
3438 (arg->prop_values_ptr);
3439 for (i = 0; i < props_count; i++) {
3440 if (put_user(obj->properties->ids[i],
3441 props_ptr + copied)) {
3442 ret = -EFAULT;
3443 goto out;
3444 }
3445 if (put_user(obj->properties->values[i],
3446 prop_values_ptr + copied)) {
3447 ret = -EFAULT;
3448 goto out;
3449 }
3450 copied++;
3451 }
3452 }
3453 arg->count_props = props_count;
3454out:
Daniel Vetter84849902012-12-02 00:28:11 +01003455 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003456 return ret;
3457}
3458
3459int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3460 struct drm_file *file_priv)
3461{
3462 struct drm_mode_obj_set_property *arg = data;
3463 struct drm_mode_object *arg_obj;
3464 struct drm_mode_object *prop_obj;
3465 struct drm_property *property;
3466 int ret = -EINVAL;
3467 int i;
3468
3469 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3470 return -EINVAL;
3471
Daniel Vetter84849902012-12-02 00:28:11 +01003472 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003473
3474 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3475 if (!arg_obj)
3476 goto out;
3477 if (!arg_obj->properties)
3478 goto out;
3479
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003480 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003481 if (arg_obj->properties->ids[i] == arg->prop_id)
3482 break;
3483
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003484 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003485 goto out;
3486
3487 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3488 DRM_MODE_OBJECT_PROPERTY);
3489 if (!prop_obj)
3490 goto out;
3491 property = obj_to_property(prop_obj);
3492
3493 if (!drm_property_change_is_valid(property, arg->value))
3494 goto out;
3495
3496 switch (arg_obj->type) {
3497 case DRM_MODE_OBJECT_CONNECTOR:
3498 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3499 arg->value);
3500 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003501 case DRM_MODE_OBJECT_CRTC:
3502 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3503 break;
Rob Clark4d939142012-05-17 02:23:27 -06003504 case DRM_MODE_OBJECT_PLANE:
3505 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3506 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003507 }
3508
3509out:
Daniel Vetter84849902012-12-02 00:28:11 +01003510 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003511 return ret;
3512}
3513
Dave Airlief453ba02008-11-07 14:05:41 -08003514int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3515 struct drm_encoder *encoder)
3516{
3517 int i;
3518
3519 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3520 if (connector->encoder_ids[i] == 0) {
3521 connector->encoder_ids[i] = encoder->base.id;
3522 return 0;
3523 }
3524 }
3525 return -ENOMEM;
3526}
3527EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3528
3529void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3530 struct drm_encoder *encoder)
3531{
3532 int i;
3533 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3534 if (connector->encoder_ids[i] == encoder->base.id) {
3535 connector->encoder_ids[i] = 0;
3536 if (connector->encoder == encoder)
3537 connector->encoder = NULL;
3538 break;
3539 }
3540 }
3541}
3542EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3543
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003544int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003545 int gamma_size)
3546{
3547 crtc->gamma_size = gamma_size;
3548
3549 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3550 if (!crtc->gamma_store) {
3551 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003552 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003553 }
3554
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003555 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003556}
3557EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3558
3559int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3560 void *data, struct drm_file *file_priv)
3561{
3562 struct drm_mode_crtc_lut *crtc_lut = data;
3563 struct drm_mode_object *obj;
3564 struct drm_crtc *crtc;
3565 void *r_base, *g_base, *b_base;
3566 int size;
3567 int ret = 0;
3568
Dave Airliefb3b06c2011-02-08 13:55:21 +10003569 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3570 return -EINVAL;
3571
Daniel Vetter84849902012-12-02 00:28:11 +01003572 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003573 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3574 if (!obj) {
3575 ret = -EINVAL;
3576 goto out;
3577 }
3578 crtc = obj_to_crtc(obj);
3579
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003580 if (crtc->funcs->gamma_set == NULL) {
3581 ret = -ENOSYS;
3582 goto out;
3583 }
3584
Dave Airlief453ba02008-11-07 14:05:41 -08003585 /* memcpy into gamma store */
3586 if (crtc_lut->gamma_size != crtc->gamma_size) {
3587 ret = -EINVAL;
3588 goto out;
3589 }
3590
3591 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3592 r_base = crtc->gamma_store;
3593 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3594 ret = -EFAULT;
3595 goto out;
3596 }
3597
3598 g_base = r_base + size;
3599 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3600 ret = -EFAULT;
3601 goto out;
3602 }
3603
3604 b_base = g_base + size;
3605 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3606 ret = -EFAULT;
3607 goto out;
3608 }
3609
James Simmons72034252010-08-03 01:33:19 +01003610 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003611
3612out:
Daniel Vetter84849902012-12-02 00:28:11 +01003613 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003614 return ret;
3615
3616}
3617
3618int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3619 void *data, struct drm_file *file_priv)
3620{
3621 struct drm_mode_crtc_lut *crtc_lut = data;
3622 struct drm_mode_object *obj;
3623 struct drm_crtc *crtc;
3624 void *r_base, *g_base, *b_base;
3625 int size;
3626 int ret = 0;
3627
Dave Airliefb3b06c2011-02-08 13:55:21 +10003628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3629 return -EINVAL;
3630
Daniel Vetter84849902012-12-02 00:28:11 +01003631 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003632 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3633 if (!obj) {
3634 ret = -EINVAL;
3635 goto out;
3636 }
3637 crtc = obj_to_crtc(obj);
3638
3639 /* memcpy into gamma store */
3640 if (crtc_lut->gamma_size != crtc->gamma_size) {
3641 ret = -EINVAL;
3642 goto out;
3643 }
3644
3645 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3646 r_base = crtc->gamma_store;
3647 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3648 ret = -EFAULT;
3649 goto out;
3650 }
3651
3652 g_base = r_base + size;
3653 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3654 ret = -EFAULT;
3655 goto out;
3656 }
3657
3658 b_base = g_base + size;
3659 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3660 ret = -EFAULT;
3661 goto out;
3662 }
3663out:
Daniel Vetter84849902012-12-02 00:28:11 +01003664 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003665 return ret;
3666}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003667
3668int drm_mode_page_flip_ioctl(struct drm_device *dev,
3669 void *data, struct drm_file *file_priv)
3670{
3671 struct drm_mode_crtc_page_flip *page_flip = data;
3672 struct drm_mode_object *obj;
3673 struct drm_crtc *crtc;
Daniel Vetterb0d12322012-12-11 01:07:12 +01003674 struct drm_framebuffer *fb = NULL, *old_fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003675 struct drm_pending_vblank_event *e = NULL;
3676 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003677 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003678 int ret = -EINVAL;
3679
3680 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3681 page_flip->reserved != 0)
3682 return -EINVAL;
3683
Daniel Vetter84849902012-12-02 00:28:11 +01003684 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003685 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3686 if (!obj)
3687 goto out;
3688 crtc = obj_to_crtc(obj);
3689
Chris Wilson90c1efd2010-07-17 20:23:26 +01003690 if (crtc->fb == NULL) {
3691 /* The framebuffer is currently unbound, presumably
3692 * due to a hotplug event, that userspace has not
3693 * yet discovered.
3694 */
3695 ret = -EBUSY;
3696 goto out;
3697 }
3698
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003699 if (crtc->funcs->page_flip == NULL)
3700 goto out;
3701
Daniel Vetter786b99e2012-12-02 21:53:40 +01003702 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
3703 if (!fb)
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003704 goto out;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003705
Rob Clark7c80e122012-09-04 16:35:56 +00003706 hdisplay = crtc->mode.hdisplay;
3707 vdisplay = crtc->mode.vdisplay;
3708
3709 if (crtc->invert_dimensions)
3710 swap(hdisplay, vdisplay);
3711
3712 if (hdisplay > fb->width ||
3713 vdisplay > fb->height ||
3714 crtc->x > fb->width - hdisplay ||
3715 crtc->y > fb->height - vdisplay) {
3716 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3717 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3718 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003719 ret = -ENOSPC;
3720 goto out;
3721 }
3722
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003723 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3724 ret = -ENOMEM;
3725 spin_lock_irqsave(&dev->event_lock, flags);
3726 if (file_priv->event_space < sizeof e->event) {
3727 spin_unlock_irqrestore(&dev->event_lock, flags);
3728 goto out;
3729 }
3730 file_priv->event_space -= sizeof e->event;
3731 spin_unlock_irqrestore(&dev->event_lock, flags);
3732
3733 e = kzalloc(sizeof *e, GFP_KERNEL);
3734 if (e == NULL) {
3735 spin_lock_irqsave(&dev->event_lock, flags);
3736 file_priv->event_space += sizeof e->event;
3737 spin_unlock_irqrestore(&dev->event_lock, flags);
3738 goto out;
3739 }
3740
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003741 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003742 e->event.base.length = sizeof e->event;
3743 e->event.user_data = page_flip->user_data;
3744 e->base.event = &e->event.base;
3745 e->base.file_priv = file_priv;
3746 e->base.destroy =
3747 (void (*) (struct drm_pending_event *)) kfree;
3748 }
3749
Daniel Vetterb0d12322012-12-11 01:07:12 +01003750 old_fb = crtc->fb;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003751 ret = crtc->funcs->page_flip(crtc, fb, e);
3752 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003753 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3754 spin_lock_irqsave(&dev->event_lock, flags);
3755 file_priv->event_space += sizeof e->event;
3756 spin_unlock_irqrestore(&dev->event_lock, flags);
3757 kfree(e);
3758 }
Daniel Vetterb0d12322012-12-11 01:07:12 +01003759 /* Keep the old fb, don't unref it. */
3760 old_fb = NULL;
3761 } else {
3762 /* Unref only the old framebuffer. */
3763 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003764 }
3765
3766out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01003767 if (fb)
3768 drm_framebuffer_unreference(fb);
3769 if (old_fb)
3770 drm_framebuffer_unreference(old_fb);
Daniel Vetter84849902012-12-02 00:28:11 +01003771 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003772 return ret;
3773}
Chris Wilsoneb0335562011-01-24 15:11:08 +00003774
3775void drm_mode_config_reset(struct drm_device *dev)
3776{
3777 struct drm_crtc *crtc;
3778 struct drm_encoder *encoder;
3779 struct drm_connector *connector;
3780
3781 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3782 if (crtc->funcs->reset)
3783 crtc->funcs->reset(crtc);
3784
3785 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3786 if (encoder->funcs->reset)
3787 encoder->funcs->reset(encoder);
3788
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003789 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3790 connector->status = connector_status_unknown;
3791
Chris Wilsoneb0335562011-01-24 15:11:08 +00003792 if (connector->funcs->reset)
3793 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003794 }
Chris Wilsoneb0335562011-01-24 15:11:08 +00003795}
3796EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003797
3798int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3799 void *data, struct drm_file *file_priv)
3800{
3801 struct drm_mode_create_dumb *args = data;
3802
3803 if (!dev->driver->dumb_create)
3804 return -ENOSYS;
3805 return dev->driver->dumb_create(file_priv, dev, args);
3806}
3807
3808int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3809 void *data, struct drm_file *file_priv)
3810{
3811 struct drm_mode_map_dumb *args = data;
3812
3813 /* call driver ioctl to get mmap offset */
3814 if (!dev->driver->dumb_map_offset)
3815 return -ENOSYS;
3816
3817 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3818}
3819
3820int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3821 void *data, struct drm_file *file_priv)
3822{
3823 struct drm_mode_destroy_dumb *args = data;
3824
3825 if (!dev->driver->dumb_destroy)
3826 return -ENOSYS;
3827
3828 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3829}
Dave Airlie248dbc22011-11-29 20:02:54 +00003830
3831/*
3832 * Just need to support RGB formats here for compat with code that doesn't
3833 * use pixel formats directly yet.
3834 */
3835void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3836 int *bpp)
3837{
3838 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003839 case DRM_FORMAT_RGB332:
3840 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003841 *depth = 8;
3842 *bpp = 8;
3843 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003844 case DRM_FORMAT_XRGB1555:
3845 case DRM_FORMAT_XBGR1555:
3846 case DRM_FORMAT_RGBX5551:
3847 case DRM_FORMAT_BGRX5551:
3848 case DRM_FORMAT_ARGB1555:
3849 case DRM_FORMAT_ABGR1555:
3850 case DRM_FORMAT_RGBA5551:
3851 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003852 *depth = 15;
3853 *bpp = 16;
3854 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003855 case DRM_FORMAT_RGB565:
3856 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003857 *depth = 16;
3858 *bpp = 16;
3859 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003860 case DRM_FORMAT_RGB888:
3861 case DRM_FORMAT_BGR888:
3862 *depth = 24;
3863 *bpp = 24;
3864 break;
3865 case DRM_FORMAT_XRGB8888:
3866 case DRM_FORMAT_XBGR8888:
3867 case DRM_FORMAT_RGBX8888:
3868 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003869 *depth = 24;
3870 *bpp = 32;
3871 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003872 case DRM_FORMAT_XRGB2101010:
3873 case DRM_FORMAT_XBGR2101010:
3874 case DRM_FORMAT_RGBX1010102:
3875 case DRM_FORMAT_BGRX1010102:
3876 case DRM_FORMAT_ARGB2101010:
3877 case DRM_FORMAT_ABGR2101010:
3878 case DRM_FORMAT_RGBA1010102:
3879 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003880 *depth = 30;
3881 *bpp = 32;
3882 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003883 case DRM_FORMAT_ARGB8888:
3884 case DRM_FORMAT_ABGR8888:
3885 case DRM_FORMAT_RGBA8888:
3886 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003887 *depth = 32;
3888 *bpp = 32;
3889 break;
3890 default:
3891 DRM_DEBUG_KMS("unsupported pixel format\n");
3892 *depth = 0;
3893 *bpp = 0;
3894 break;
3895 }
3896}
3897EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003898
3899/**
3900 * drm_format_num_planes - get the number of planes for format
3901 * @format: pixel format (DRM_FORMAT_*)
3902 *
3903 * RETURNS:
3904 * The number of planes used by the specified pixel format.
3905 */
3906int drm_format_num_planes(uint32_t format)
3907{
3908 switch (format) {
3909 case DRM_FORMAT_YUV410:
3910 case DRM_FORMAT_YVU410:
3911 case DRM_FORMAT_YUV411:
3912 case DRM_FORMAT_YVU411:
3913 case DRM_FORMAT_YUV420:
3914 case DRM_FORMAT_YVU420:
3915 case DRM_FORMAT_YUV422:
3916 case DRM_FORMAT_YVU422:
3917 case DRM_FORMAT_YUV444:
3918 case DRM_FORMAT_YVU444:
3919 return 3;
3920 case DRM_FORMAT_NV12:
3921 case DRM_FORMAT_NV21:
3922 case DRM_FORMAT_NV16:
3923 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003924 case DRM_FORMAT_NV24:
3925 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003926 return 2;
3927 default:
3928 return 1;
3929 }
3930}
3931EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003932
3933/**
3934 * drm_format_plane_cpp - determine the bytes per pixel value
3935 * @format: pixel format (DRM_FORMAT_*)
3936 * @plane: plane index
3937 *
3938 * RETURNS:
3939 * The bytes per pixel value for the specified plane.
3940 */
3941int drm_format_plane_cpp(uint32_t format, int plane)
3942{
3943 unsigned int depth;
3944 int bpp;
3945
3946 if (plane >= drm_format_num_planes(format))
3947 return 0;
3948
3949 switch (format) {
3950 case DRM_FORMAT_YUYV:
3951 case DRM_FORMAT_YVYU:
3952 case DRM_FORMAT_UYVY:
3953 case DRM_FORMAT_VYUY:
3954 return 2;
3955 case DRM_FORMAT_NV12:
3956 case DRM_FORMAT_NV21:
3957 case DRM_FORMAT_NV16:
3958 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003959 case DRM_FORMAT_NV24:
3960 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003961 return plane ? 2 : 1;
3962 case DRM_FORMAT_YUV410:
3963 case DRM_FORMAT_YVU410:
3964 case DRM_FORMAT_YUV411:
3965 case DRM_FORMAT_YVU411:
3966 case DRM_FORMAT_YUV420:
3967 case DRM_FORMAT_YVU420:
3968 case DRM_FORMAT_YUV422:
3969 case DRM_FORMAT_YVU422:
3970 case DRM_FORMAT_YUV444:
3971 case DRM_FORMAT_YVU444:
3972 return 1;
3973 default:
3974 drm_fb_get_bpp_depth(format, &depth, &bpp);
3975 return bpp >> 3;
3976 }
3977}
3978EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003979
3980/**
3981 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3982 * @format: pixel format (DRM_FORMAT_*)
3983 *
3984 * RETURNS:
3985 * The horizontal chroma subsampling factor for the
3986 * specified pixel format.
3987 */
3988int drm_format_horz_chroma_subsampling(uint32_t format)
3989{
3990 switch (format) {
3991 case DRM_FORMAT_YUV411:
3992 case DRM_FORMAT_YVU411:
3993 case DRM_FORMAT_YUV410:
3994 case DRM_FORMAT_YVU410:
3995 return 4;
3996 case DRM_FORMAT_YUYV:
3997 case DRM_FORMAT_YVYU:
3998 case DRM_FORMAT_UYVY:
3999 case DRM_FORMAT_VYUY:
4000 case DRM_FORMAT_NV12:
4001 case DRM_FORMAT_NV21:
4002 case DRM_FORMAT_NV16:
4003 case DRM_FORMAT_NV61:
4004 case DRM_FORMAT_YUV422:
4005 case DRM_FORMAT_YVU422:
4006 case DRM_FORMAT_YUV420:
4007 case DRM_FORMAT_YVU420:
4008 return 2;
4009 default:
4010 return 1;
4011 }
4012}
4013EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
4014
4015/**
4016 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
4017 * @format: pixel format (DRM_FORMAT_*)
4018 *
4019 * RETURNS:
4020 * The vertical chroma subsampling factor for the
4021 * specified pixel format.
4022 */
4023int drm_format_vert_chroma_subsampling(uint32_t format)
4024{
4025 switch (format) {
4026 case DRM_FORMAT_YUV410:
4027 case DRM_FORMAT_YVU410:
4028 return 4;
4029 case DRM_FORMAT_YUV420:
4030 case DRM_FORMAT_YVU420:
4031 case DRM_FORMAT_NV12:
4032 case DRM_FORMAT_NV21:
4033 return 2;
4034 default:
4035 return 1;
4036 }
4037}
4038EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);