blob: 7ca2f28348e609b540b5e55897f39bddc09f4f4c [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
Dave Airlief453ba02008-11-07 14:05:41 -080040/* Avoid boilerplate. I'm tired of typing. */
41#define DRM_ENUM_NAME_FN(fnname, list) \
42 char *fnname(int val) \
43 { \
44 int i; \
45 for (i = 0; i < ARRAY_SIZE(list); i++) { \
46 if (list[i].type == val) \
47 return list[i].name; \
48 } \
49 return "(unknown)"; \
50 }
51
52/*
53 * Global properties
54 */
55static struct drm_prop_enum_list drm_dpms_enum_list[] =
56{ { DRM_MODE_DPMS_ON, "On" },
57 { DRM_MODE_DPMS_STANDBY, "Standby" },
58 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
59 { DRM_MODE_DPMS_OFF, "Off" }
60};
61
62DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
63
64/*
65 * Optional properties
66 */
67static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
68{
Jesse Barnes53bd8382009-07-01 10:04:40 -070069 { DRM_MODE_SCALE_NONE, "None" },
70 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
71 { DRM_MODE_SCALE_CENTER, "Center" },
72 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080073};
74
75static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
76{
77 { DRM_MODE_DITHERING_OFF, "Off" },
78 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100079 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080080};
81
82/*
83 * Non-global properties, but "required" for certain connectors.
84 */
85static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
86{
87 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
88 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
89 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
90};
91
92DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
93
94static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
95{
96 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
97 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
98 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
99};
100
101DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
102 drm_dvi_i_subconnector_enum_list)
103
104static struct drm_prop_enum_list drm_tv_select_enum_list[] =
105{
106 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
107 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
108 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200110 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800111};
112
113DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
114
115static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
116{
117 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200121 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800122};
123
124DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
125 drm_tv_subconnector_enum_list)
126
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000127static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
128 { DRM_MODE_DIRTY_OFF, "Off" },
129 { DRM_MODE_DIRTY_ON, "On" },
130 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
131};
132
133DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
134 drm_dirty_info_enum_list)
135
Dave Airlief453ba02008-11-07 14:05:41 -0800136struct drm_conn_prop_enum_list {
137 int type;
138 char *name;
139 int count;
140};
141
142/*
143 * Connector and encoder types.
144 */
145static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
146{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
147 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
148 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
149 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
150 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
151 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
152 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
153 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
154 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500155 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
156 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
157 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
158 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200159 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500160 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200161 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800162};
163
164static struct drm_prop_enum_list drm_encoder_enum_list[] =
165{ { DRM_MODE_ENCODER_NONE, "None" },
166 { DRM_MODE_ENCODER_DAC, "DAC" },
167 { DRM_MODE_ENCODER_TMDS, "TMDS" },
168 { DRM_MODE_ENCODER_LVDS, "LVDS" },
169 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200170 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800171};
172
173char *drm_get_encoder_name(struct drm_encoder *encoder)
174{
175 static char buf[32];
176
177 snprintf(buf, 32, "%s-%d",
178 drm_encoder_enum_list[encoder->encoder_type].name,
179 encoder->base.id);
180 return buf;
181}
Dave Airlie13a81952009-09-07 15:45:33 +1000182EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800183
184char *drm_get_connector_name(struct drm_connector *connector)
185{
186 static char buf[32];
187
188 snprintf(buf, 32, "%s-%d",
189 drm_connector_enum_list[connector->connector_type].name,
190 connector->connector_type_id);
191 return buf;
192}
193EXPORT_SYMBOL(drm_get_connector_name);
194
195char *drm_get_connector_status_name(enum drm_connector_status status)
196{
197 if (status == connector_status_connected)
198 return "connected";
199 else if (status == connector_status_disconnected)
200 return "disconnected";
201 else
202 return "unknown";
203}
204
205/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100206 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800207 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100208 * @obj: object pointer, used to generate unique ID
209 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800210 *
Dave Airlief453ba02008-11-07 14:05:41 -0800211 * Create a unique identifier based on @ptr in @dev's identifier space. Used
212 * for tracking modes, CRTCs and connectors.
213 *
214 * RETURNS:
215 * New unique (relative to other objects in @dev) integer identifier for the
216 * object.
217 */
218static int drm_mode_object_get(struct drm_device *dev,
219 struct drm_mode_object *obj, uint32_t obj_type)
220{
221 int new_id = 0;
222 int ret;
223
Dave Airlief453ba02008-11-07 14:05:41 -0800224again:
225 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
226 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200227 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800228 }
229
Jesse Barnesad2563c2009-01-19 17:21:45 +1000230 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800231 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000232 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800233 if (ret == -EAGAIN)
234 goto again;
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200235 else if (ret)
236 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800237
238 obj->id = new_id;
239 obj->type = obj_type;
240 return 0;
241}
242
243/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100244 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800245 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100246 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800247 *
Dave Airlief453ba02008-11-07 14:05:41 -0800248 * Free @id from @dev's unique identifier pool.
249 */
250static void drm_mode_object_put(struct drm_device *dev,
251 struct drm_mode_object *object)
252{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000253 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800254 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000255 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800256}
257
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200258struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
259 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800260{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000261 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800262
Jesse Barnesad2563c2009-01-19 17:21:45 +1000263 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800264 obj = idr_find(&dev->mode_config.crtc_idr, id);
265 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000266 obj = NULL;
267 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800268
269 return obj;
270}
271EXPORT_SYMBOL(drm_mode_object_find);
272
273/**
Dave Airlief453ba02008-11-07 14:05:41 -0800274 * drm_framebuffer_init - initialize a framebuffer
275 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100276 * @fb: framebuffer to be initialized
277 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800278 *
Dave Airlief453ba02008-11-07 14:05:41 -0800279 * Allocates an ID for the framebuffer's parent mode object, sets its mode
280 * functions & device file and adds it to the master fd list.
281 *
282 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200283 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800284 */
285int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
286 const struct drm_framebuffer_funcs *funcs)
287{
288 int ret;
289
Rob Clarkf7eff602012-09-05 21:48:38 +0000290 kref_init(&fb->refcount);
291
Dave Airlief453ba02008-11-07 14:05:41 -0800292 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200293 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800294 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800295
296 fb->dev = dev;
297 fb->funcs = funcs;
298 dev->mode_config.num_fb++;
299 list_add(&fb->head, &dev->mode_config.fb_list);
300
301 return 0;
302}
303EXPORT_SYMBOL(drm_framebuffer_init);
304
Rob Clarkf7eff602012-09-05 21:48:38 +0000305static void drm_framebuffer_free(struct kref *kref)
306{
307 struct drm_framebuffer *fb =
308 container_of(kref, struct drm_framebuffer, refcount);
309 fb->funcs->destroy(fb);
310}
311
312/**
313 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100314 * @fb: framebuffer to unref
315 *
316 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000317 */
318void drm_framebuffer_unreference(struct drm_framebuffer *fb)
319{
Rob Clarkf7eff602012-09-05 21:48:38 +0000320 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000321 kref_put(&fb->refcount, drm_framebuffer_free);
322}
323EXPORT_SYMBOL(drm_framebuffer_unreference);
324
325/**
326 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100327 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000328 */
329void drm_framebuffer_reference(struct drm_framebuffer *fb)
330{
331 DRM_DEBUG("FB ID: %d\n", fb->base.id);
332 kref_get(&fb->refcount);
333}
334EXPORT_SYMBOL(drm_framebuffer_reference);
335
Dave Airlief453ba02008-11-07 14:05:41 -0800336/**
337 * drm_framebuffer_cleanup - remove a framebuffer object
338 * @fb: framebuffer to remove
339 *
Dave Airlief453ba02008-11-07 14:05:41 -0800340 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
341 * it, setting it to NULL.
342 */
343void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
344{
345 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100346
Rob Clarkf7eff602012-09-05 21:48:38 +0000347 /*
348 * This could be moved to drm_framebuffer_remove(), but for
349 * debugging is nice to keep around the list of fb's that are
350 * no longer associated w/ a drm_file but are not unreferenced
351 * yet. (i915 and omapdrm have debugfs files which will show
352 * this.)
353 */
354 drm_mode_object_put(dev, &fb->base);
355 list_del(&fb->head);
356 dev->mode_config.num_fb--;
357}
358EXPORT_SYMBOL(drm_framebuffer_cleanup);
359
360/**
361 * drm_framebuffer_remove - remove and unreference a framebuffer object
362 * @fb: framebuffer to remove
363 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000364 * Scans all the CRTCs and planes in @dev's mode_config. If they're
365 * using @fb, removes it, setting it to NULL.
366 */
367void drm_framebuffer_remove(struct drm_framebuffer *fb)
368{
369 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800370 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800371 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000372 struct drm_mode_set set;
373 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800374
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100375 WARN_ON(!drm_modeset_is_locked(dev));
376
Dave Airlief453ba02008-11-07 14:05:41 -0800377 /* remove from any CRTC */
378 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000379 if (crtc->fb == fb) {
380 /* should turn off the crtc */
381 memset(&set, 0, sizeof(struct drm_mode_set));
382 set.crtc = crtc;
383 set.fb = NULL;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100384 ret = drm_mode_set_config_internal(&set);
Dave Airlie5ef5f722009-08-17 13:11:23 +1000385 if (ret)
386 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
387 }
Dave Airlief453ba02008-11-07 14:05:41 -0800388 }
389
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800390 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
391 if (plane->fb == fb) {
392 /* should turn off the crtc */
393 ret = plane->funcs->disable_plane(plane);
394 if (ret)
395 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600396 /* disconnect the plane from the fb and crtc: */
397 plane->fb = NULL;
398 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800399 }
400 }
401
Rob Clarkf7eff602012-09-05 21:48:38 +0000402 list_del(&fb->filp_head);
403
404 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800405}
Rob Clarkf7eff602012-09-05 21:48:38 +0000406EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800407
408/**
409 * drm_crtc_init - Initialise a new CRTC object
410 * @dev: DRM device
411 * @crtc: CRTC object to init
412 * @funcs: callbacks for the new CRTC
413 *
Dave Airlief453ba02008-11-07 14:05:41 -0800414 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200415 *
416 * RETURNS:
417 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800418 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200419int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800420 const struct drm_crtc_funcs *funcs)
421{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200422 int ret;
423
Dave Airlief453ba02008-11-07 14:05:41 -0800424 crtc->dev = dev;
425 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000426 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800427
428 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200429
430 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
431 if (ret)
432 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800433
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300434 crtc->base.properties = &crtc->properties;
435
Dave Airlief453ba02008-11-07 14:05:41 -0800436 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
437 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200438
439 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800440 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200441
442 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800443}
444EXPORT_SYMBOL(drm_crtc_init);
445
446/**
447 * drm_crtc_cleanup - Cleans up the core crtc usage.
448 * @crtc: CRTC to cleanup
449 *
Dave Airlief453ba02008-11-07 14:05:41 -0800450 * Cleanup @crtc. Removes from drm modesetting space
451 * does NOT free object, caller does that.
452 */
453void drm_crtc_cleanup(struct drm_crtc *crtc)
454{
455 struct drm_device *dev = crtc->dev;
456
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000457 kfree(crtc->gamma_store);
458 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800459
460 drm_mode_object_put(dev, &crtc->base);
461 list_del(&crtc->head);
462 dev->mode_config.num_crtc--;
463}
464EXPORT_SYMBOL(drm_crtc_cleanup);
465
466/**
467 * drm_mode_probed_add - add a mode to a connector's probed mode list
468 * @connector: connector the new mode
469 * @mode: mode data
470 *
Dave Airlief453ba02008-11-07 14:05:41 -0800471 * Add @mode to @connector's mode list for later use.
472 */
473void drm_mode_probed_add(struct drm_connector *connector,
474 struct drm_display_mode *mode)
475{
476 list_add(&mode->head, &connector->probed_modes);
477}
478EXPORT_SYMBOL(drm_mode_probed_add);
479
480/**
481 * drm_mode_remove - remove and free a mode
482 * @connector: connector list to modify
483 * @mode: mode to remove
484 *
Dave Airlief453ba02008-11-07 14:05:41 -0800485 * Remove @mode from @connector's mode list, then free it.
486 */
487void drm_mode_remove(struct drm_connector *connector,
488 struct drm_display_mode *mode)
489{
490 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100491 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800492}
493EXPORT_SYMBOL(drm_mode_remove);
494
495/**
496 * drm_connector_init - Init a preallocated connector
497 * @dev: DRM device
498 * @connector: the connector to init
499 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100500 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800501 *
Dave Airlief453ba02008-11-07 14:05:41 -0800502 * Initialises a preallocated connector. Connectors should be
503 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200504 *
505 * RETURNS:
506 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800507 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200508int drm_connector_init(struct drm_device *dev,
509 struct drm_connector *connector,
510 const struct drm_connector_funcs *funcs,
511 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800512{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200513 int ret;
514
Dave Airlief453ba02008-11-07 14:05:41 -0800515 mutex_lock(&dev->mode_config.mutex);
516
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200517 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
518 if (ret)
519 goto out;
520
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300521 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800522 connector->dev = dev;
523 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800524 connector->connector_type = connector_type;
525 connector->connector_type_id =
526 ++drm_connector_enum_list[connector_type].count; /* TODO */
527 INIT_LIST_HEAD(&connector->user_modes);
528 INIT_LIST_HEAD(&connector->probed_modes);
529 INIT_LIST_HEAD(&connector->modes);
530 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000531 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800532
533 list_add_tail(&connector->head, &dev->mode_config.connector_list);
534 dev->mode_config.num_connector++;
535
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200536 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500537 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200538 dev->mode_config.edid_property,
539 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800540
Rob Clark58495562012-10-11 20:50:56 -0500541 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800542 dev->mode_config.dpms_property, 0);
543
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200544 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800545 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200546
547 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800548}
549EXPORT_SYMBOL(drm_connector_init);
550
551/**
552 * drm_connector_cleanup - cleans up an initialised connector
553 * @connector: connector to cleanup
554 *
Dave Airlief453ba02008-11-07 14:05:41 -0800555 * Cleans up the connector but doesn't free the object.
556 */
557void drm_connector_cleanup(struct drm_connector *connector)
558{
559 struct drm_device *dev = connector->dev;
560 struct drm_display_mode *mode, *t;
561
562 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
563 drm_mode_remove(connector, mode);
564
565 list_for_each_entry_safe(mode, t, &connector->modes, head)
566 drm_mode_remove(connector, mode);
567
568 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
569 drm_mode_remove(connector, mode);
570
Dave Airlief453ba02008-11-07 14:05:41 -0800571 drm_mode_object_put(dev, &connector->base);
572 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000573 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800574}
575EXPORT_SYMBOL(drm_connector_cleanup);
576
Dave Airliecbc7e222012-02-20 14:16:40 +0000577void drm_connector_unplug_all(struct drm_device *dev)
578{
579 struct drm_connector *connector;
580
581 /* taking the mode config mutex ends up in a clash with sysfs */
582 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
583 drm_sysfs_connector_remove(connector);
584
585}
586EXPORT_SYMBOL(drm_connector_unplug_all);
587
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200588int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000589 struct drm_encoder *encoder,
590 const struct drm_encoder_funcs *funcs,
591 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800592{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200593 int ret;
594
Dave Airlief453ba02008-11-07 14:05:41 -0800595 mutex_lock(&dev->mode_config.mutex);
596
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200597 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
598 if (ret)
599 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800600
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200601 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800602 encoder->encoder_type = encoder_type;
603 encoder->funcs = funcs;
604
605 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
606 dev->mode_config.num_encoder++;
607
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200608 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800609 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200610
611 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800612}
613EXPORT_SYMBOL(drm_encoder_init);
614
615void drm_encoder_cleanup(struct drm_encoder *encoder)
616{
617 struct drm_device *dev = encoder->dev;
618 mutex_lock(&dev->mode_config.mutex);
619 drm_mode_object_put(dev, &encoder->base);
620 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000621 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800622 mutex_unlock(&dev->mode_config.mutex);
623}
624EXPORT_SYMBOL(drm_encoder_cleanup);
625
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800626int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
627 unsigned long possible_crtcs,
628 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600629 const uint32_t *formats, uint32_t format_count,
630 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800631{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200632 int ret;
633
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800634 mutex_lock(&dev->mode_config.mutex);
635
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200636 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
637 if (ret)
638 goto out;
639
Rob Clark4d939142012-05-17 02:23:27 -0600640 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800641 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800642 plane->funcs = funcs;
643 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
644 GFP_KERNEL);
645 if (!plane->format_types) {
646 DRM_DEBUG_KMS("out of memory when allocating plane\n");
647 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200648 ret = -ENOMEM;
649 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800650 }
651
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800652 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800653 plane->format_count = format_count;
654 plane->possible_crtcs = possible_crtcs;
655
Rob Clark0a7eb242011-12-13 20:19:36 -0600656 /* private planes are not exposed to userspace, but depending on
657 * display hardware, might be convenient to allow sharing programming
658 * for the scanout engine with the crtc implementation.
659 */
660 if (!priv) {
661 list_add_tail(&plane->head, &dev->mode_config.plane_list);
662 dev->mode_config.num_plane++;
663 } else {
664 INIT_LIST_HEAD(&plane->head);
665 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800666
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200667 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800668 mutex_unlock(&dev->mode_config.mutex);
669
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200670 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800671}
672EXPORT_SYMBOL(drm_plane_init);
673
674void drm_plane_cleanup(struct drm_plane *plane)
675{
676 struct drm_device *dev = plane->dev;
677
678 mutex_lock(&dev->mode_config.mutex);
679 kfree(plane->format_types);
680 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600681 /* if not added to a list, it must be a private plane */
682 if (!list_empty(&plane->head)) {
683 list_del(&plane->head);
684 dev->mode_config.num_plane--;
685 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800686 mutex_unlock(&dev->mode_config.mutex);
687}
688EXPORT_SYMBOL(drm_plane_cleanup);
689
Dave Airlief453ba02008-11-07 14:05:41 -0800690/**
691 * drm_mode_create - create a new display mode
692 * @dev: DRM device
693 *
Dave Airlief453ba02008-11-07 14:05:41 -0800694 * Create a new drm_display_mode, give it an ID, and return it.
695 *
696 * RETURNS:
697 * Pointer to new mode on success, NULL on error.
698 */
699struct drm_display_mode *drm_mode_create(struct drm_device *dev)
700{
701 struct drm_display_mode *nmode;
702
703 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
704 if (!nmode)
705 return NULL;
706
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200707 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
708 kfree(nmode);
709 return NULL;
710 }
711
Dave Airlief453ba02008-11-07 14:05:41 -0800712 return nmode;
713}
714EXPORT_SYMBOL(drm_mode_create);
715
716/**
717 * drm_mode_destroy - remove a mode
718 * @dev: DRM device
719 * @mode: mode to remove
720 *
Dave Airlief453ba02008-11-07 14:05:41 -0800721 * Free @mode's unique identifier, then free it.
722 */
723void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
724{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200725 if (!mode)
726 return;
727
Dave Airlief453ba02008-11-07 14:05:41 -0800728 drm_mode_object_put(dev, &mode->base);
729
730 kfree(mode);
731}
732EXPORT_SYMBOL(drm_mode_destroy);
733
734static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
735{
736 struct drm_property *edid;
737 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800738
739 /*
740 * Standard properties (apply to all connectors)
741 */
742 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
743 DRM_MODE_PROP_IMMUTABLE,
744 "EDID", 0);
745 dev->mode_config.edid_property = edid;
746
Sascha Hauer4a67d392012-02-06 10:58:17 +0100747 dpms = drm_property_create_enum(dev, 0,
748 "DPMS", drm_dpms_enum_list,
749 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800750 dev->mode_config.dpms_property = dpms;
751
752 return 0;
753}
754
755/**
756 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
757 * @dev: DRM device
758 *
759 * Called by a driver the first time a DVI-I connector is made.
760 */
761int drm_mode_create_dvi_i_properties(struct drm_device *dev)
762{
763 struct drm_property *dvi_i_selector;
764 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800765
766 if (dev->mode_config.dvi_i_select_subconnector_property)
767 return 0;
768
769 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100770 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800771 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100772 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800773 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800774 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
775
Sascha Hauer4a67d392012-02-06 10:58:17 +0100776 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800777 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100778 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800779 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800780 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
781
782 return 0;
783}
784EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
785
786/**
787 * drm_create_tv_properties - create TV specific connector properties
788 * @dev: DRM device
789 * @num_modes: number of different TV formats (modes) supported
790 * @modes: array of pointers to strings containing name of each format
791 *
792 * Called by a driver's TV initialization routine, this function creates
793 * the TV specific connector properties for a given device. Caller is
794 * responsible for allocating a list of format names and passing them to
795 * this routine.
796 */
797int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
798 char *modes[])
799{
800 struct drm_property *tv_selector;
801 struct drm_property *tv_subconnector;
802 int i;
803
804 if (dev->mode_config.tv_select_subconnector_property)
805 return 0;
806
807 /*
808 * Basic connector properties
809 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100810 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800811 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100812 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800813 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800814 dev->mode_config.tv_select_subconnector_property = tv_selector;
815
816 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100817 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
818 "subconnector",
819 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800820 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800821 dev->mode_config.tv_subconnector_property = tv_subconnector;
822
823 /*
824 * Other, TV specific properties: margins & TV modes.
825 */
826 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100827 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800828
829 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100830 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800831
832 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100833 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800834
835 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100836 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800837
838 dev->mode_config.tv_mode_property =
839 drm_property_create(dev, DRM_MODE_PROP_ENUM,
840 "mode", num_modes);
841 for (i = 0; i < num_modes; i++)
842 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
843 i, modes[i]);
844
Francisco Jerezb6b79022009-08-02 04:19:20 +0200845 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100846 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200847
848 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100849 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200850
851 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100852 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200853
Francisco Jereza75f0232009-08-12 02:30:10 +0200854 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100855 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200856
857 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100858 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200859
860 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100861 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200862
Dave Airlief453ba02008-11-07 14:05:41 -0800863 return 0;
864}
865EXPORT_SYMBOL(drm_mode_create_tv_properties);
866
867/**
868 * drm_mode_create_scaling_mode_property - create scaling mode property
869 * @dev: DRM device
870 *
871 * Called by a driver the first time it's needed, must be attached to desired
872 * connectors.
873 */
874int drm_mode_create_scaling_mode_property(struct drm_device *dev)
875{
876 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800877
878 if (dev->mode_config.scaling_mode_property)
879 return 0;
880
881 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100882 drm_property_create_enum(dev, 0, "scaling mode",
883 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800884 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800885
886 dev->mode_config.scaling_mode_property = scaling_mode;
887
888 return 0;
889}
890EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
891
892/**
893 * drm_mode_create_dithering_property - create dithering property
894 * @dev: DRM device
895 *
896 * Called by a driver the first time it's needed, must be attached to desired
897 * connectors.
898 */
899int drm_mode_create_dithering_property(struct drm_device *dev)
900{
901 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800902
903 if (dev->mode_config.dithering_mode_property)
904 return 0;
905
906 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100907 drm_property_create_enum(dev, 0, "dithering",
908 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800909 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800910 dev->mode_config.dithering_mode_property = dithering_mode;
911
912 return 0;
913}
914EXPORT_SYMBOL(drm_mode_create_dithering_property);
915
916/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000917 * drm_mode_create_dirty_property - create dirty property
918 * @dev: DRM device
919 *
920 * Called by a driver the first time it's needed, must be attached to desired
921 * connectors.
922 */
923int drm_mode_create_dirty_info_property(struct drm_device *dev)
924{
925 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000926
927 if (dev->mode_config.dirty_info_property)
928 return 0;
929
930 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100931 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000932 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100933 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000934 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000935 dev->mode_config.dirty_info_property = dirty_info;
936
937 return 0;
938}
939EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
940
941/**
Dave Airlief453ba02008-11-07 14:05:41 -0800942 * drm_mode_config_init - initialize DRM mode_configuration structure
943 * @dev: DRM device
944 *
Dave Airlief453ba02008-11-07 14:05:41 -0800945 * Initialize @dev's mode_config structure, used for tracking the graphics
946 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100947 *
948 * Since this initializes the modeset locks, no locking is possible. Which is no
949 * problem, since this should happen single threaded at init time. It is the
950 * driver's problem to ensure this guarantee.
951 *
Dave Airlief453ba02008-11-07 14:05:41 -0800952 */
953void drm_mode_config_init(struct drm_device *dev)
954{
955 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000956 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800957 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800958 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
959 INIT_LIST_HEAD(&dev->mode_config.connector_list);
960 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
961 INIT_LIST_HEAD(&dev->mode_config.property_list);
962 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800963 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800964 idr_init(&dev->mode_config.crtc_idr);
965
966 mutex_lock(&dev->mode_config.mutex);
967 drm_mode_create_standard_connector_properties(dev);
968 mutex_unlock(&dev->mode_config.mutex);
969
970 /* Just to be sure */
971 dev->mode_config.num_fb = 0;
972 dev->mode_config.num_connector = 0;
973 dev->mode_config.num_crtc = 0;
974 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800975}
976EXPORT_SYMBOL(drm_mode_config_init);
977
978int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
979{
980 uint32_t total_objects = 0;
981
982 total_objects += dev->mode_config.num_crtc;
983 total_objects += dev->mode_config.num_connector;
984 total_objects += dev->mode_config.num_encoder;
985
Dave Airlief453ba02008-11-07 14:05:41 -0800986 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
987 if (!group->id_list)
988 return -ENOMEM;
989
990 group->num_crtcs = 0;
991 group->num_connectors = 0;
992 group->num_encoders = 0;
993 return 0;
994}
995
996int drm_mode_group_init_legacy_group(struct drm_device *dev,
997 struct drm_mode_group *group)
998{
999 struct drm_crtc *crtc;
1000 struct drm_encoder *encoder;
1001 struct drm_connector *connector;
1002 int ret;
1003
1004 if ((ret = drm_mode_group_init(dev, group)))
1005 return ret;
1006
1007 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1008 group->id_list[group->num_crtcs++] = crtc->base.id;
1009
1010 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1011 group->id_list[group->num_crtcs + group->num_encoders++] =
1012 encoder->base.id;
1013
1014 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1015 group->id_list[group->num_crtcs + group->num_encoders +
1016 group->num_connectors++] = connector->base.id;
1017
1018 return 0;
1019}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001020EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001021
1022/**
1023 * drm_mode_config_cleanup - free up DRM mode_config info
1024 * @dev: DRM device
1025 *
Dave Airlief453ba02008-11-07 14:05:41 -08001026 * Free up all the connectors and CRTCs associated with this DRM device, then
1027 * free up the framebuffers and associated buffer objects.
1028 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001029 * Note that since this /should/ happen single-threaded at driver/device
1030 * teardown time, no locking is required. It's the driver's job to ensure that
1031 * this guarantee actually holds true.
1032 *
Dave Airlief453ba02008-11-07 14:05:41 -08001033 * FIXME: cleanup any dangling user buffer objects too
1034 */
1035void drm_mode_config_cleanup(struct drm_device *dev)
1036{
1037 struct drm_connector *connector, *ot;
1038 struct drm_crtc *crtc, *ct;
1039 struct drm_encoder *encoder, *enct;
1040 struct drm_framebuffer *fb, *fbt;
1041 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001042 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001043
1044 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1045 head) {
1046 encoder->funcs->destroy(encoder);
1047 }
1048
1049 list_for_each_entry_safe(connector, ot,
1050 &dev->mode_config.connector_list, head) {
1051 connector->funcs->destroy(connector);
1052 }
1053
1054 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1055 head) {
1056 drm_property_destroy(dev, property);
1057 }
1058
1059 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001060 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001061 }
1062
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001063 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1064 head) {
1065 plane->funcs->destroy(plane);
1066 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001067
Chris Wilson31840092012-09-17 09:38:03 +00001068 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1069 crtc->funcs->destroy(crtc);
1070 }
1071
Sascha Hauer59ce0622012-02-01 11:38:20 +01001072 idr_remove_all(&dev->mode_config.crtc_idr);
1073 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001074}
1075EXPORT_SYMBOL(drm_mode_config_cleanup);
1076
Dave Airlief453ba02008-11-07 14:05:41 -08001077/**
1078 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1079 * @out: drm_mode_modeinfo struct to return to the user
1080 * @in: drm_display_mode to use
1081 *
Dave Airlief453ba02008-11-07 14:05:41 -08001082 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1083 * the user.
1084 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001085static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1086 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001087{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001088 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1089 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1090 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1091 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1092 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1093 "timing values too large for mode info\n");
1094
Dave Airlief453ba02008-11-07 14:05:41 -08001095 out->clock = in->clock;
1096 out->hdisplay = in->hdisplay;
1097 out->hsync_start = in->hsync_start;
1098 out->hsync_end = in->hsync_end;
1099 out->htotal = in->htotal;
1100 out->hskew = in->hskew;
1101 out->vdisplay = in->vdisplay;
1102 out->vsync_start = in->vsync_start;
1103 out->vsync_end = in->vsync_end;
1104 out->vtotal = in->vtotal;
1105 out->vscan = in->vscan;
1106 out->vrefresh = in->vrefresh;
1107 out->flags = in->flags;
1108 out->type = in->type;
1109 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1110 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1111}
1112
1113/**
1114 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1115 * @out: drm_display_mode to return to the user
1116 * @in: drm_mode_modeinfo to use
1117 *
Dave Airlief453ba02008-11-07 14:05:41 -08001118 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1119 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001120 *
1121 * RETURNS:
1122 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001123 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001124static int drm_crtc_convert_umode(struct drm_display_mode *out,
1125 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001126{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001127 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1128 return -ERANGE;
1129
Dave Airlief453ba02008-11-07 14:05:41 -08001130 out->clock = in->clock;
1131 out->hdisplay = in->hdisplay;
1132 out->hsync_start = in->hsync_start;
1133 out->hsync_end = in->hsync_end;
1134 out->htotal = in->htotal;
1135 out->hskew = in->hskew;
1136 out->vdisplay = in->vdisplay;
1137 out->vsync_start = in->vsync_start;
1138 out->vsync_end = in->vsync_end;
1139 out->vtotal = in->vtotal;
1140 out->vscan = in->vscan;
1141 out->vrefresh = in->vrefresh;
1142 out->flags = in->flags;
1143 out->type = in->type;
1144 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1145 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001146
1147 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001148}
1149
1150/**
1151 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001152 * @dev: drm device for the ioctl
1153 * @data: data pointer for the ioctl
1154 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001155 *
Dave Airlief453ba02008-11-07 14:05:41 -08001156 * Construct a set of configuration description structures and return
1157 * them to the user, including CRTC, connector and framebuffer configuration.
1158 *
1159 * Called by the user via ioctl.
1160 *
1161 * RETURNS:
1162 * Zero on success, errno on failure.
1163 */
1164int drm_mode_getresources(struct drm_device *dev, void *data,
1165 struct drm_file *file_priv)
1166{
1167 struct drm_mode_card_res *card_res = data;
1168 struct list_head *lh;
1169 struct drm_framebuffer *fb;
1170 struct drm_connector *connector;
1171 struct drm_crtc *crtc;
1172 struct drm_encoder *encoder;
1173 int ret = 0;
1174 int connector_count = 0;
1175 int crtc_count = 0;
1176 int fb_count = 0;
1177 int encoder_count = 0;
1178 int copied = 0, i;
1179 uint32_t __user *fb_id;
1180 uint32_t __user *crtc_id;
1181 uint32_t __user *connector_id;
1182 uint32_t __user *encoder_id;
1183 struct drm_mode_group *mode_group;
1184
Dave Airliefb3b06c2011-02-08 13:55:21 +10001185 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1186 return -EINVAL;
1187
Dave Airlief453ba02008-11-07 14:05:41 -08001188 mutex_lock(&dev->mode_config.mutex);
1189
1190 /*
1191 * For the non-control nodes we need to limit the list of resources
1192 * by IDs in the group list for this node
1193 */
1194 list_for_each(lh, &file_priv->fbs)
1195 fb_count++;
1196
1197 mode_group = &file_priv->master->minor->mode_group;
1198 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1199
1200 list_for_each(lh, &dev->mode_config.crtc_list)
1201 crtc_count++;
1202
1203 list_for_each(lh, &dev->mode_config.connector_list)
1204 connector_count++;
1205
1206 list_for_each(lh, &dev->mode_config.encoder_list)
1207 encoder_count++;
1208 } else {
1209
1210 crtc_count = mode_group->num_crtcs;
1211 connector_count = mode_group->num_connectors;
1212 encoder_count = mode_group->num_encoders;
1213 }
1214
1215 card_res->max_height = dev->mode_config.max_height;
1216 card_res->min_height = dev->mode_config.min_height;
1217 card_res->max_width = dev->mode_config.max_width;
1218 card_res->min_width = dev->mode_config.min_width;
1219
1220 /* handle this in 4 parts */
1221 /* FBs */
1222 if (card_res->count_fbs >= fb_count) {
1223 copied = 0;
1224 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001225 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001226 if (put_user(fb->base.id, fb_id + copied)) {
1227 ret = -EFAULT;
1228 goto out;
1229 }
1230 copied++;
1231 }
1232 }
1233 card_res->count_fbs = fb_count;
1234
1235 /* CRTCs */
1236 if (card_res->count_crtcs >= crtc_count) {
1237 copied = 0;
1238 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1239 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1240 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1241 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001242 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001243 if (put_user(crtc->base.id, crtc_id + copied)) {
1244 ret = -EFAULT;
1245 goto out;
1246 }
1247 copied++;
1248 }
1249 } else {
1250 for (i = 0; i < mode_group->num_crtcs; i++) {
1251 if (put_user(mode_group->id_list[i],
1252 crtc_id + copied)) {
1253 ret = -EFAULT;
1254 goto out;
1255 }
1256 copied++;
1257 }
1258 }
1259 }
1260 card_res->count_crtcs = crtc_count;
1261
1262 /* Encoders */
1263 if (card_res->count_encoders >= encoder_count) {
1264 copied = 0;
1265 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1266 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1267 list_for_each_entry(encoder,
1268 &dev->mode_config.encoder_list,
1269 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001270 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1271 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001272 if (put_user(encoder->base.id, encoder_id +
1273 copied)) {
1274 ret = -EFAULT;
1275 goto out;
1276 }
1277 copied++;
1278 }
1279 } else {
1280 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1281 if (put_user(mode_group->id_list[i],
1282 encoder_id + copied)) {
1283 ret = -EFAULT;
1284 goto out;
1285 }
1286 copied++;
1287 }
1288
1289 }
1290 }
1291 card_res->count_encoders = encoder_count;
1292
1293 /* Connectors */
1294 if (card_res->count_connectors >= connector_count) {
1295 copied = 0;
1296 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1297 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1298 list_for_each_entry(connector,
1299 &dev->mode_config.connector_list,
1300 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001301 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1302 connector->base.id,
1303 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001304 if (put_user(connector->base.id,
1305 connector_id + copied)) {
1306 ret = -EFAULT;
1307 goto out;
1308 }
1309 copied++;
1310 }
1311 } else {
1312 int start = mode_group->num_crtcs +
1313 mode_group->num_encoders;
1314 for (i = start; i < start + mode_group->num_connectors; i++) {
1315 if (put_user(mode_group->id_list[i],
1316 connector_id + copied)) {
1317 ret = -EFAULT;
1318 goto out;
1319 }
1320 copied++;
1321 }
1322 }
1323 }
1324 card_res->count_connectors = connector_count;
1325
Jerome Glisse94401062010-07-15 15:43:25 -04001326 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001327 card_res->count_connectors, card_res->count_encoders);
1328
1329out:
1330 mutex_unlock(&dev->mode_config.mutex);
1331 return ret;
1332}
1333
1334/**
1335 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001336 * @dev: drm device for the ioctl
1337 * @data: data pointer for the ioctl
1338 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001339 *
Dave Airlief453ba02008-11-07 14:05:41 -08001340 * Construct a CRTC configuration structure to return to the user.
1341 *
1342 * Called by the user via ioctl.
1343 *
1344 * RETURNS:
1345 * Zero on success, errno on failure.
1346 */
1347int drm_mode_getcrtc(struct drm_device *dev,
1348 void *data, struct drm_file *file_priv)
1349{
1350 struct drm_mode_crtc *crtc_resp = data;
1351 struct drm_crtc *crtc;
1352 struct drm_mode_object *obj;
1353 int ret = 0;
1354
Dave Airliefb3b06c2011-02-08 13:55:21 +10001355 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1356 return -EINVAL;
1357
Dave Airlief453ba02008-11-07 14:05:41 -08001358 mutex_lock(&dev->mode_config.mutex);
1359
1360 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1361 DRM_MODE_OBJECT_CRTC);
1362 if (!obj) {
1363 ret = -EINVAL;
1364 goto out;
1365 }
1366 crtc = obj_to_crtc(obj);
1367
1368 crtc_resp->x = crtc->x;
1369 crtc_resp->y = crtc->y;
1370 crtc_resp->gamma_size = crtc->gamma_size;
1371 if (crtc->fb)
1372 crtc_resp->fb_id = crtc->fb->base.id;
1373 else
1374 crtc_resp->fb_id = 0;
1375
1376 if (crtc->enabled) {
1377
1378 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1379 crtc_resp->mode_valid = 1;
1380
1381 } else {
1382 crtc_resp->mode_valid = 0;
1383 }
1384
1385out:
1386 mutex_unlock(&dev->mode_config.mutex);
1387 return ret;
1388}
1389
1390/**
1391 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001392 * @dev: drm device for the ioctl
1393 * @data: data pointer for the ioctl
1394 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001395 *
Dave Airlief453ba02008-11-07 14:05:41 -08001396 * Construct a connector configuration structure to return to the user.
1397 *
1398 * Called by the user via ioctl.
1399 *
1400 * RETURNS:
1401 * Zero on success, errno on failure.
1402 */
1403int drm_mode_getconnector(struct drm_device *dev, void *data,
1404 struct drm_file *file_priv)
1405{
1406 struct drm_mode_get_connector *out_resp = data;
1407 struct drm_mode_object *obj;
1408 struct drm_connector *connector;
1409 struct drm_display_mode *mode;
1410 int mode_count = 0;
1411 int props_count = 0;
1412 int encoders_count = 0;
1413 int ret = 0;
1414 int copied = 0;
1415 int i;
1416 struct drm_mode_modeinfo u_mode;
1417 struct drm_mode_modeinfo __user *mode_ptr;
1418 uint32_t __user *prop_ptr;
1419 uint64_t __user *prop_values;
1420 uint32_t __user *encoder_ptr;
1421
Dave Airliefb3b06c2011-02-08 13:55:21 +10001422 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1423 return -EINVAL;
1424
Dave Airlief453ba02008-11-07 14:05:41 -08001425 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1426
Jerome Glisse94401062010-07-15 15:43:25 -04001427 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001428
1429 mutex_lock(&dev->mode_config.mutex);
1430
1431 obj = drm_mode_object_find(dev, out_resp->connector_id,
1432 DRM_MODE_OBJECT_CONNECTOR);
1433 if (!obj) {
1434 ret = -EINVAL;
1435 goto out;
1436 }
1437 connector = obj_to_connector(obj);
1438
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001439 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001440
1441 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1442 if (connector->encoder_ids[i] != 0) {
1443 encoders_count++;
1444 }
1445 }
1446
1447 if (out_resp->count_modes == 0) {
1448 connector->funcs->fill_modes(connector,
1449 dev->mode_config.max_width,
1450 dev->mode_config.max_height);
1451 }
1452
1453 /* delayed so we get modes regardless of pre-fill_modes state */
1454 list_for_each_entry(mode, &connector->modes, head)
1455 mode_count++;
1456
1457 out_resp->connector_id = connector->base.id;
1458 out_resp->connector_type = connector->connector_type;
1459 out_resp->connector_type_id = connector->connector_type_id;
1460 out_resp->mm_width = connector->display_info.width_mm;
1461 out_resp->mm_height = connector->display_info.height_mm;
1462 out_resp->subpixel = connector->display_info.subpixel_order;
1463 out_resp->connection = connector->status;
1464 if (connector->encoder)
1465 out_resp->encoder_id = connector->encoder->base.id;
1466 else
1467 out_resp->encoder_id = 0;
1468
1469 /*
1470 * This ioctl is called twice, once to determine how much space is
1471 * needed, and the 2nd time to fill it.
1472 */
1473 if ((out_resp->count_modes >= mode_count) && mode_count) {
1474 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001475 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001476 list_for_each_entry(mode, &connector->modes, head) {
1477 drm_crtc_convert_to_umode(&u_mode, mode);
1478 if (copy_to_user(mode_ptr + copied,
1479 &u_mode, sizeof(u_mode))) {
1480 ret = -EFAULT;
1481 goto out;
1482 }
1483 copied++;
1484 }
1485 }
1486 out_resp->count_modes = mode_count;
1487
1488 if ((out_resp->count_props >= props_count) && props_count) {
1489 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001490 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1491 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001492 for (i = 0; i < connector->properties.count; i++) {
1493 if (put_user(connector->properties.ids[i],
1494 prop_ptr + copied)) {
1495 ret = -EFAULT;
1496 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001497 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001498
1499 if (put_user(connector->properties.values[i],
1500 prop_values + copied)) {
1501 ret = -EFAULT;
1502 goto out;
1503 }
1504 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001505 }
1506 }
1507 out_resp->count_props = props_count;
1508
1509 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1510 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001511 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001512 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1513 if (connector->encoder_ids[i] != 0) {
1514 if (put_user(connector->encoder_ids[i],
1515 encoder_ptr + copied)) {
1516 ret = -EFAULT;
1517 goto out;
1518 }
1519 copied++;
1520 }
1521 }
1522 }
1523 out_resp->count_encoders = encoders_count;
1524
1525out:
1526 mutex_unlock(&dev->mode_config.mutex);
1527 return ret;
1528}
1529
1530int drm_mode_getencoder(struct drm_device *dev, void *data,
1531 struct drm_file *file_priv)
1532{
1533 struct drm_mode_get_encoder *enc_resp = data;
1534 struct drm_mode_object *obj;
1535 struct drm_encoder *encoder;
1536 int ret = 0;
1537
Dave Airliefb3b06c2011-02-08 13:55:21 +10001538 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1539 return -EINVAL;
1540
Dave Airlief453ba02008-11-07 14:05:41 -08001541 mutex_lock(&dev->mode_config.mutex);
1542 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1543 DRM_MODE_OBJECT_ENCODER);
1544 if (!obj) {
1545 ret = -EINVAL;
1546 goto out;
1547 }
1548 encoder = obj_to_encoder(obj);
1549
1550 if (encoder->crtc)
1551 enc_resp->crtc_id = encoder->crtc->base.id;
1552 else
1553 enc_resp->crtc_id = 0;
1554 enc_resp->encoder_type = encoder->encoder_type;
1555 enc_resp->encoder_id = encoder->base.id;
1556 enc_resp->possible_crtcs = encoder->possible_crtcs;
1557 enc_resp->possible_clones = encoder->possible_clones;
1558
1559out:
1560 mutex_unlock(&dev->mode_config.mutex);
1561 return ret;
1562}
1563
1564/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001565 * drm_mode_getplane_res - get plane info
1566 * @dev: DRM device
1567 * @data: ioctl data
1568 * @file_priv: DRM file info
1569 *
1570 * Return an plane count and set of IDs.
1571 */
1572int drm_mode_getplane_res(struct drm_device *dev, void *data,
1573 struct drm_file *file_priv)
1574{
1575 struct drm_mode_get_plane_res *plane_resp = data;
1576 struct drm_mode_config *config;
1577 struct drm_plane *plane;
1578 uint32_t __user *plane_ptr;
1579 int copied = 0, ret = 0;
1580
1581 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1582 return -EINVAL;
1583
1584 mutex_lock(&dev->mode_config.mutex);
1585 config = &dev->mode_config;
1586
1587 /*
1588 * This ioctl is called twice, once to determine how much space is
1589 * needed, and the 2nd time to fill it.
1590 */
1591 if (config->num_plane &&
1592 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001593 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001594
1595 list_for_each_entry(plane, &config->plane_list, head) {
1596 if (put_user(plane->base.id, plane_ptr + copied)) {
1597 ret = -EFAULT;
1598 goto out;
1599 }
1600 copied++;
1601 }
1602 }
1603 plane_resp->count_planes = config->num_plane;
1604
1605out:
1606 mutex_unlock(&dev->mode_config.mutex);
1607 return ret;
1608}
1609
1610/**
1611 * drm_mode_getplane - get plane info
1612 * @dev: DRM device
1613 * @data: ioctl data
1614 * @file_priv: DRM file info
1615 *
1616 * Return plane info, including formats supported, gamma size, any
1617 * current fb, etc.
1618 */
1619int drm_mode_getplane(struct drm_device *dev, void *data,
1620 struct drm_file *file_priv)
1621{
1622 struct drm_mode_get_plane *plane_resp = data;
1623 struct drm_mode_object *obj;
1624 struct drm_plane *plane;
1625 uint32_t __user *format_ptr;
1626 int ret = 0;
1627
1628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1629 return -EINVAL;
1630
1631 mutex_lock(&dev->mode_config.mutex);
1632 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1633 DRM_MODE_OBJECT_PLANE);
1634 if (!obj) {
1635 ret = -ENOENT;
1636 goto out;
1637 }
1638 plane = obj_to_plane(obj);
1639
1640 if (plane->crtc)
1641 plane_resp->crtc_id = plane->crtc->base.id;
1642 else
1643 plane_resp->crtc_id = 0;
1644
1645 if (plane->fb)
1646 plane_resp->fb_id = plane->fb->base.id;
1647 else
1648 plane_resp->fb_id = 0;
1649
1650 plane_resp->plane_id = plane->base.id;
1651 plane_resp->possible_crtcs = plane->possible_crtcs;
1652 plane_resp->gamma_size = plane->gamma_size;
1653
1654 /*
1655 * This ioctl is called twice, once to determine how much space is
1656 * needed, and the 2nd time to fill it.
1657 */
1658 if (plane->format_count &&
1659 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001660 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001661 if (copy_to_user(format_ptr,
1662 plane->format_types,
1663 sizeof(uint32_t) * plane->format_count)) {
1664 ret = -EFAULT;
1665 goto out;
1666 }
1667 }
1668 plane_resp->count_format_types = plane->format_count;
1669
1670out:
1671 mutex_unlock(&dev->mode_config.mutex);
1672 return ret;
1673}
1674
1675/**
1676 * drm_mode_setplane - set up or tear down an plane
1677 * @dev: DRM device
1678 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001679 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001680 *
1681 * Set plane info, including placement, fb, scaling, and other factors.
1682 * Or pass a NULL fb to disable.
1683 */
1684int drm_mode_setplane(struct drm_device *dev, void *data,
1685 struct drm_file *file_priv)
1686{
1687 struct drm_mode_set_plane *plane_req = data;
1688 struct drm_mode_object *obj;
1689 struct drm_plane *plane;
1690 struct drm_crtc *crtc;
1691 struct drm_framebuffer *fb;
1692 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001693 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001694 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001695
1696 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1697 return -EINVAL;
1698
1699 mutex_lock(&dev->mode_config.mutex);
1700
1701 /*
1702 * First, find the plane, crtc, and fb objects. If not available,
1703 * we don't bother to call the driver.
1704 */
1705 obj = drm_mode_object_find(dev, plane_req->plane_id,
1706 DRM_MODE_OBJECT_PLANE);
1707 if (!obj) {
1708 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1709 plane_req->plane_id);
1710 ret = -ENOENT;
1711 goto out;
1712 }
1713 plane = obj_to_plane(obj);
1714
1715 /* No fb means shut it down */
1716 if (!plane_req->fb_id) {
1717 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001718 plane->crtc = NULL;
1719 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001720 goto out;
1721 }
1722
1723 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1724 DRM_MODE_OBJECT_CRTC);
1725 if (!obj) {
1726 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1727 plane_req->crtc_id);
1728 ret = -ENOENT;
1729 goto out;
1730 }
1731 crtc = obj_to_crtc(obj);
1732
1733 obj = drm_mode_object_find(dev, plane_req->fb_id,
1734 DRM_MODE_OBJECT_FB);
1735 if (!obj) {
1736 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1737 plane_req->fb_id);
1738 ret = -ENOENT;
1739 goto out;
1740 }
1741 fb = obj_to_fb(obj);
1742
Ville Syrjälä62443be2011-12-20 00:06:47 +02001743 /* Check whether this plane supports the fb pixel format. */
1744 for (i = 0; i < plane->format_count; i++)
1745 if (fb->pixel_format == plane->format_types[i])
1746 break;
1747 if (i == plane->format_count) {
1748 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1749 ret = -EINVAL;
1750 goto out;
1751 }
1752
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001753 fb_width = fb->width << 16;
1754 fb_height = fb->height << 16;
1755
1756 /* Make sure source coordinates are inside the fb. */
1757 if (plane_req->src_w > fb_width ||
1758 plane_req->src_x > fb_width - plane_req->src_w ||
1759 plane_req->src_h > fb_height ||
1760 plane_req->src_y > fb_height - plane_req->src_h) {
1761 DRM_DEBUG_KMS("Invalid source coordinates "
1762 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1763 plane_req->src_w >> 16,
1764 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1765 plane_req->src_h >> 16,
1766 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1767 plane_req->src_x >> 16,
1768 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1769 plane_req->src_y >> 16,
1770 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1771 ret = -ENOSPC;
1772 goto out;
1773 }
1774
Ville Syrjälä687a0402011-12-20 00:06:45 +02001775 /* Give drivers some help against integer overflows */
1776 if (plane_req->crtc_w > INT_MAX ||
1777 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1778 plane_req->crtc_h > INT_MAX ||
1779 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1780 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1781 plane_req->crtc_w, plane_req->crtc_h,
1782 plane_req->crtc_x, plane_req->crtc_y);
1783 ret = -ERANGE;
1784 goto out;
1785 }
1786
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001787 ret = plane->funcs->update_plane(plane, crtc, fb,
1788 plane_req->crtc_x, plane_req->crtc_y,
1789 plane_req->crtc_w, plane_req->crtc_h,
1790 plane_req->src_x, plane_req->src_y,
1791 plane_req->src_w, plane_req->src_h);
1792 if (!ret) {
1793 plane->crtc = crtc;
1794 plane->fb = fb;
1795 }
1796
1797out:
1798 mutex_unlock(&dev->mode_config.mutex);
1799
1800 return ret;
1801}
1802
1803/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001804 * drm_mode_set_config_internal - helper to call ->set_config
1805 * @set: modeset config to set
1806 *
1807 * This is a little helper to wrap internal calls to the ->set_config driver
1808 * interface. The only thing it adds is correct refcounting dance.
1809 */
1810int drm_mode_set_config_internal(struct drm_mode_set *set)
1811{
1812 struct drm_crtc *crtc = set->crtc;
1813
1814 return crtc->funcs->set_config(set);
1815}
1816EXPORT_SYMBOL(drm_mode_set_config_internal);
1817
1818/**
Dave Airlief453ba02008-11-07 14:05:41 -08001819 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001820 * @dev: drm device for the ioctl
1821 * @data: data pointer for the ioctl
1822 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001823 *
Dave Airlief453ba02008-11-07 14:05:41 -08001824 * Build a new CRTC configuration based on user request.
1825 *
1826 * Called by the user via ioctl.
1827 *
1828 * RETURNS:
1829 * Zero on success, errno on failure.
1830 */
1831int drm_mode_setcrtc(struct drm_device *dev, void *data,
1832 struct drm_file *file_priv)
1833{
1834 struct drm_mode_config *config = &dev->mode_config;
1835 struct drm_mode_crtc *crtc_req = data;
1836 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001837 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001838 struct drm_connector **connector_set = NULL, *connector;
1839 struct drm_framebuffer *fb = NULL;
1840 struct drm_display_mode *mode = NULL;
1841 struct drm_mode_set set;
1842 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001843 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001844 int i;
1845
Dave Airliefb3b06c2011-02-08 13:55:21 +10001846 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1847 return -EINVAL;
1848
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001849 /* For some reason crtc x/y offsets are signed internally. */
1850 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1851 return -ERANGE;
1852
Dave Airlief453ba02008-11-07 14:05:41 -08001853 mutex_lock(&dev->mode_config.mutex);
1854 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1855 DRM_MODE_OBJECT_CRTC);
1856 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001857 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001858 ret = -EINVAL;
1859 goto out;
1860 }
1861 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001862 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001863
1864 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001865 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001866 /* If we have a mode we need a framebuffer. */
1867 /* If we pass -1, set the mode with the currently bound fb */
1868 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001869 if (!crtc->fb) {
1870 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1871 ret = -EINVAL;
1872 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001873 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001874 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001875 } else {
1876 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1877 DRM_MODE_OBJECT_FB);
1878 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001879 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1880 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001881 ret = -EINVAL;
1882 goto out;
1883 }
1884 fb = obj_to_fb(obj);
1885 }
1886
1887 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001888 if (!mode) {
1889 ret = -ENOMEM;
1890 goto out;
1891 }
1892
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001893 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1894 if (ret) {
1895 DRM_DEBUG_KMS("Invalid mode\n");
1896 goto out;
1897 }
1898
Dave Airlief453ba02008-11-07 14:05:41 -08001899 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001900
Rob Clark7c80e122012-09-04 16:35:56 +00001901 hdisplay = mode->hdisplay;
1902 vdisplay = mode->vdisplay;
1903
1904 if (crtc->invert_dimensions)
1905 swap(hdisplay, vdisplay);
1906
1907 if (hdisplay > fb->width ||
1908 vdisplay > fb->height ||
1909 crtc_req->x > fb->width - hdisplay ||
1910 crtc_req->y > fb->height - vdisplay) {
1911 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1912 fb->width, fb->height,
1913 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1914 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001915 ret = -ENOSPC;
1916 goto out;
1917 }
Dave Airlief453ba02008-11-07 14:05:41 -08001918 }
1919
1920 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001921 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001922 ret = -EINVAL;
1923 goto out;
1924 }
1925
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001926 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001927 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001928 crtc_req->count_connectors);
1929 ret = -EINVAL;
1930 goto out;
1931 }
1932
1933 if (crtc_req->count_connectors > 0) {
1934 u32 out_id;
1935
1936 /* Avoid unbounded kernel memory allocation */
1937 if (crtc_req->count_connectors > config->num_connector) {
1938 ret = -EINVAL;
1939 goto out;
1940 }
1941
1942 connector_set = kmalloc(crtc_req->count_connectors *
1943 sizeof(struct drm_connector *),
1944 GFP_KERNEL);
1945 if (!connector_set) {
1946 ret = -ENOMEM;
1947 goto out;
1948 }
1949
1950 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001951 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001952 if (get_user(out_id, &set_connectors_ptr[i])) {
1953 ret = -EFAULT;
1954 goto out;
1955 }
1956
1957 obj = drm_mode_object_find(dev, out_id,
1958 DRM_MODE_OBJECT_CONNECTOR);
1959 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001960 DRM_DEBUG_KMS("Connector id %d unknown\n",
1961 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001962 ret = -EINVAL;
1963 goto out;
1964 }
1965 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001966 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1967 connector->base.id,
1968 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001969
1970 connector_set[i] = connector;
1971 }
1972 }
1973
1974 set.crtc = crtc;
1975 set.x = crtc_req->x;
1976 set.y = crtc_req->y;
1977 set.mode = mode;
1978 set.connectors = connector_set;
1979 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001980 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01001981 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08001982
1983out:
1984 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001985 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001986 mutex_unlock(&dev->mode_config.mutex);
1987 return ret;
1988}
1989
1990int drm_mode_cursor_ioctl(struct drm_device *dev,
1991 void *data, struct drm_file *file_priv)
1992{
1993 struct drm_mode_cursor *req = data;
1994 struct drm_mode_object *obj;
1995 struct drm_crtc *crtc;
1996 int ret = 0;
1997
Dave Airliefb3b06c2011-02-08 13:55:21 +10001998 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1999 return -EINVAL;
2000
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002001 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002002 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002003
2004 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002005 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002006 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002007 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002008 ret = -EINVAL;
2009 goto out;
2010 }
2011 crtc = obj_to_crtc(obj);
2012
2013 if (req->flags & DRM_MODE_CURSOR_BO) {
2014 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002015 ret = -ENXIO;
2016 goto out;
2017 }
2018 /* Turns off the cursor if handle is 0 */
2019 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2020 req->width, req->height);
2021 }
2022
2023 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2024 if (crtc->funcs->cursor_move) {
2025 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2026 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002027 ret = -EFAULT;
2028 goto out;
2029 }
2030 }
2031out:
2032 mutex_unlock(&dev->mode_config.mutex);
2033 return ret;
2034}
2035
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002036/* Original addfb only supported RGB formats, so figure out which one */
2037uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2038{
2039 uint32_t fmt;
2040
2041 switch (bpp) {
2042 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 break;
2045 case 16:
2046 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002047 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002048 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002049 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002050 break;
2051 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002052 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002053 break;
2054 case 32:
2055 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002056 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002057 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002058 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002059 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002060 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002061 break;
2062 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002063 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2064 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002065 break;
2066 }
2067
2068 return fmt;
2069}
2070EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2071
Dave Airlief453ba02008-11-07 14:05:41 -08002072/**
2073 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002074 * @dev: drm device for the ioctl
2075 * @data: data pointer for the ioctl
2076 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002077 *
Dave Airlief453ba02008-11-07 14:05:41 -08002078 * Add a new FB to the specified CRTC, given a user request.
2079 *
2080 * Called by the user via ioctl.
2081 *
2082 * RETURNS:
2083 * Zero on success, errno on failure.
2084 */
2085int drm_mode_addfb(struct drm_device *dev,
2086 void *data, struct drm_file *file_priv)
2087{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002088 struct drm_mode_fb_cmd *or = data;
2089 struct drm_mode_fb_cmd2 r = {};
2090 struct drm_mode_config *config = &dev->mode_config;
2091 struct drm_framebuffer *fb;
2092 int ret = 0;
2093
2094 /* Use new struct with format internally */
2095 r.fb_id = or->fb_id;
2096 r.width = or->width;
2097 r.height = or->height;
2098 r.pitches[0] = or->pitch;
2099 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2100 r.handles[0] = or->handle;
2101
2102 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2103 return -EINVAL;
2104
Jesse Barnesacb4b992011-11-07 12:03:23 -08002105 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002106 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002107
2108 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002109 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002110
2111 mutex_lock(&dev->mode_config.mutex);
2112
2113 /* TODO check buffer is sufficiently large */
2114 /* TODO setup destructor callback */
2115
2116 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2117 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002118 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002119 ret = PTR_ERR(fb);
2120 goto out;
2121 }
2122
2123 or->fb_id = fb->base.id;
2124 list_add(&fb->filp_head, &file_priv->fbs);
2125 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2126
2127out:
2128 mutex_unlock(&dev->mode_config.mutex);
2129 return ret;
2130}
2131
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002132static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002133{
2134 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2135
2136 switch (format) {
2137 case DRM_FORMAT_C8:
2138 case DRM_FORMAT_RGB332:
2139 case DRM_FORMAT_BGR233:
2140 case DRM_FORMAT_XRGB4444:
2141 case DRM_FORMAT_XBGR4444:
2142 case DRM_FORMAT_RGBX4444:
2143 case DRM_FORMAT_BGRX4444:
2144 case DRM_FORMAT_ARGB4444:
2145 case DRM_FORMAT_ABGR4444:
2146 case DRM_FORMAT_RGBA4444:
2147 case DRM_FORMAT_BGRA4444:
2148 case DRM_FORMAT_XRGB1555:
2149 case DRM_FORMAT_XBGR1555:
2150 case DRM_FORMAT_RGBX5551:
2151 case DRM_FORMAT_BGRX5551:
2152 case DRM_FORMAT_ARGB1555:
2153 case DRM_FORMAT_ABGR1555:
2154 case DRM_FORMAT_RGBA5551:
2155 case DRM_FORMAT_BGRA5551:
2156 case DRM_FORMAT_RGB565:
2157 case DRM_FORMAT_BGR565:
2158 case DRM_FORMAT_RGB888:
2159 case DRM_FORMAT_BGR888:
2160 case DRM_FORMAT_XRGB8888:
2161 case DRM_FORMAT_XBGR8888:
2162 case DRM_FORMAT_RGBX8888:
2163 case DRM_FORMAT_BGRX8888:
2164 case DRM_FORMAT_ARGB8888:
2165 case DRM_FORMAT_ABGR8888:
2166 case DRM_FORMAT_RGBA8888:
2167 case DRM_FORMAT_BGRA8888:
2168 case DRM_FORMAT_XRGB2101010:
2169 case DRM_FORMAT_XBGR2101010:
2170 case DRM_FORMAT_RGBX1010102:
2171 case DRM_FORMAT_BGRX1010102:
2172 case DRM_FORMAT_ARGB2101010:
2173 case DRM_FORMAT_ABGR2101010:
2174 case DRM_FORMAT_RGBA1010102:
2175 case DRM_FORMAT_BGRA1010102:
2176 case DRM_FORMAT_YUYV:
2177 case DRM_FORMAT_YVYU:
2178 case DRM_FORMAT_UYVY:
2179 case DRM_FORMAT_VYUY:
2180 case DRM_FORMAT_AYUV:
2181 case DRM_FORMAT_NV12:
2182 case DRM_FORMAT_NV21:
2183 case DRM_FORMAT_NV16:
2184 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002185 case DRM_FORMAT_NV24:
2186 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002187 case DRM_FORMAT_YUV410:
2188 case DRM_FORMAT_YVU410:
2189 case DRM_FORMAT_YUV411:
2190 case DRM_FORMAT_YVU411:
2191 case DRM_FORMAT_YUV420:
2192 case DRM_FORMAT_YVU420:
2193 case DRM_FORMAT_YUV422:
2194 case DRM_FORMAT_YVU422:
2195 case DRM_FORMAT_YUV444:
2196 case DRM_FORMAT_YVU444:
2197 return 0;
2198 default:
2199 return -EINVAL;
2200 }
2201}
2202
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002203static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002204{
2205 int ret, hsub, vsub, num_planes, i;
2206
2207 ret = format_check(r);
2208 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002209 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002210 return ret;
2211 }
2212
2213 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2214 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2215 num_planes = drm_format_num_planes(r->pixel_format);
2216
2217 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002218 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002219 return -EINVAL;
2220 }
2221
2222 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002223 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002224 return -EINVAL;
2225 }
2226
2227 for (i = 0; i < num_planes; i++) {
2228 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002229 unsigned int height = r->height / (i != 0 ? vsub : 1);
2230 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002231
2232 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002233 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002234 return -EINVAL;
2235 }
2236
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002237 if ((uint64_t) width * cpp > UINT_MAX)
2238 return -ERANGE;
2239
2240 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2241 return -ERANGE;
2242
2243 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002244 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002245 return -EINVAL;
2246 }
2247 }
2248
2249 return 0;
2250}
2251
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002252/**
2253 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002254 * @dev: drm device for the ioctl
2255 * @data: data pointer for the ioctl
2256 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002257 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002258 * Add a new FB to the specified CRTC, given a user request with format.
2259 *
2260 * Called by the user via ioctl.
2261 *
2262 * RETURNS:
2263 * Zero on success, errno on failure.
2264 */
2265int drm_mode_addfb2(struct drm_device *dev,
2266 void *data, struct drm_file *file_priv)
2267{
2268 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002269 struct drm_mode_config *config = &dev->mode_config;
2270 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002271 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002272
Dave Airliefb3b06c2011-02-08 13:55:21 +10002273 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2274 return -EINVAL;
2275
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002276 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2277 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2278 return -EINVAL;
2279 }
2280
Dave Airlief453ba02008-11-07 14:05:41 -08002281 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002282 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002283 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002284 return -EINVAL;
2285 }
2286 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002287 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002288 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002289 return -EINVAL;
2290 }
2291
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002292 ret = framebuffer_check(r);
2293 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002294 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002295
Dave Airlief453ba02008-11-07 14:05:41 -08002296 mutex_lock(&dev->mode_config.mutex);
2297
Dave Airlief453ba02008-11-07 14:05:41 -08002298 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002299 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002300 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002301 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002302 goto out;
2303 }
2304
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002305 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002306 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002307 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002308
2309out:
2310 mutex_unlock(&dev->mode_config.mutex);
2311 return ret;
2312}
2313
2314/**
2315 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002316 * @dev: drm device for the ioctl
2317 * @data: data pointer for the ioctl
2318 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002319 *
Dave Airlief453ba02008-11-07 14:05:41 -08002320 * Remove the FB specified by the user.
2321 *
2322 * Called by the user via ioctl.
2323 *
2324 * RETURNS:
2325 * Zero on success, errno on failure.
2326 */
2327int drm_mode_rmfb(struct drm_device *dev,
2328 void *data, struct drm_file *file_priv)
2329{
2330 struct drm_mode_object *obj;
2331 struct drm_framebuffer *fb = NULL;
2332 struct drm_framebuffer *fbl = NULL;
2333 uint32_t *id = data;
2334 int ret = 0;
2335 int found = 0;
2336
Dave Airliefb3b06c2011-02-08 13:55:21 +10002337 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2338 return -EINVAL;
2339
Dave Airlief453ba02008-11-07 14:05:41 -08002340 mutex_lock(&dev->mode_config.mutex);
2341 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002342 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002343 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002344 ret = -EINVAL;
2345 goto out;
2346 }
2347 fb = obj_to_fb(obj);
2348
2349 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2350 if (fb == fbl)
2351 found = 1;
2352
2353 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002354 ret = -EINVAL;
2355 goto out;
2356 }
2357
Rob Clarkf7eff602012-09-05 21:48:38 +00002358 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002359
2360out:
2361 mutex_unlock(&dev->mode_config.mutex);
2362 return ret;
2363}
2364
2365/**
2366 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002367 * @dev: drm device for the ioctl
2368 * @data: data pointer for the ioctl
2369 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002370 *
Dave Airlief453ba02008-11-07 14:05:41 -08002371 * Lookup the FB given its ID and return info about it.
2372 *
2373 * Called by the user via ioctl.
2374 *
2375 * RETURNS:
2376 * Zero on success, errno on failure.
2377 */
2378int drm_mode_getfb(struct drm_device *dev,
2379 void *data, struct drm_file *file_priv)
2380{
2381 struct drm_mode_fb_cmd *r = data;
2382 struct drm_mode_object *obj;
2383 struct drm_framebuffer *fb;
2384 int ret = 0;
2385
Dave Airliefb3b06c2011-02-08 13:55:21 +10002386 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2387 return -EINVAL;
2388
Dave Airlief453ba02008-11-07 14:05:41 -08002389 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002390 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002391 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002392 ret = -EINVAL;
2393 goto out;
2394 }
2395 fb = obj_to_fb(obj);
2396
2397 r->height = fb->height;
2398 r->width = fb->width;
2399 r->depth = fb->depth;
2400 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002401 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002402 if (fb->funcs->create_handle)
2403 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2404 else
2405 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002406
2407out:
2408 mutex_unlock(&dev->mode_config.mutex);
2409 return ret;
2410}
2411
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002412int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2413 void *data, struct drm_file *file_priv)
2414{
2415 struct drm_clip_rect __user *clips_ptr;
2416 struct drm_clip_rect *clips = NULL;
2417 struct drm_mode_fb_dirty_cmd *r = data;
2418 struct drm_mode_object *obj;
2419 struct drm_framebuffer *fb;
2420 unsigned flags;
2421 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002422 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002423
Dave Airliefb3b06c2011-02-08 13:55:21 +10002424 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2425 return -EINVAL;
2426
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002427 mutex_lock(&dev->mode_config.mutex);
2428 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2429 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002430 ret = -EINVAL;
2431 goto out_err1;
2432 }
2433 fb = obj_to_fb(obj);
2434
2435 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002436 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002437
2438 if (!num_clips != !clips_ptr) {
2439 ret = -EINVAL;
2440 goto out_err1;
2441 }
2442
2443 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2444
2445 /* If userspace annotates copy, clips must come in pairs */
2446 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2447 ret = -EINVAL;
2448 goto out_err1;
2449 }
2450
2451 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002452 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2453 ret = -EINVAL;
2454 goto out_err1;
2455 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002456 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2457 if (!clips) {
2458 ret = -ENOMEM;
2459 goto out_err1;
2460 }
2461
2462 ret = copy_from_user(clips, clips_ptr,
2463 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002464 if (ret) {
2465 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002466 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002467 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002468 }
2469
2470 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002471 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2472 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002473 } else {
2474 ret = -ENOSYS;
2475 goto out_err2;
2476 }
2477
2478out_err2:
2479 kfree(clips);
2480out_err1:
2481 mutex_unlock(&dev->mode_config.mutex);
2482 return ret;
2483}
2484
2485
Dave Airlief453ba02008-11-07 14:05:41 -08002486/**
2487 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002488 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002489 *
Dave Airlief453ba02008-11-07 14:05:41 -08002490 * Destroy all the FBs associated with @filp.
2491 *
2492 * Called by the user via ioctl.
2493 *
2494 * RETURNS:
2495 * Zero on success, errno on failure.
2496 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002497void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002498{
Dave Airlief453ba02008-11-07 14:05:41 -08002499 struct drm_device *dev = priv->minor->dev;
2500 struct drm_framebuffer *fb, *tfb;
2501
2502 mutex_lock(&dev->mode_config.mutex);
2503 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00002504 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002505 }
2506 mutex_unlock(&dev->mode_config.mutex);
2507}
2508
2509/**
2510 * drm_mode_attachmode - add a mode to the user mode list
2511 * @dev: DRM device
2512 * @connector: connector to add the mode to
2513 * @mode: mode to add
2514 *
2515 * Add @mode to @connector's user mode list.
2516 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002517static void drm_mode_attachmode(struct drm_device *dev,
2518 struct drm_connector *connector,
2519 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002520{
Dave Airlief453ba02008-11-07 14:05:41 -08002521 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002522}
2523
2524int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002525 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002526{
2527 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002528 int ret = 0;
2529 struct drm_display_mode *dup_mode, *next;
2530 LIST_HEAD(list);
2531
Dave Airlief453ba02008-11-07 14:05:41 -08002532 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2533 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002534 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002535 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002536 dup_mode = drm_mode_duplicate(dev, mode);
2537 if (!dup_mode) {
2538 ret = -ENOMEM;
2539 goto out;
2540 }
2541 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002542 }
2543 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002544
2545 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2546 if (!connector->encoder)
2547 continue;
2548 if (connector->encoder->crtc == crtc)
2549 list_move_tail(list.next, &connector->user_modes);
2550 }
2551
2552 WARN_ON(!list_empty(&list));
2553
2554 out:
2555 list_for_each_entry_safe(dup_mode, next, &list, head)
2556 drm_mode_destroy(dev, dup_mode);
2557
2558 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002559}
2560EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2561
2562static int drm_mode_detachmode(struct drm_device *dev,
2563 struct drm_connector *connector,
2564 struct drm_display_mode *mode)
2565{
2566 int found = 0;
2567 int ret = 0;
2568 struct drm_display_mode *match_mode, *t;
2569
2570 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2571 if (drm_mode_equal(match_mode, mode)) {
2572 list_del(&match_mode->head);
2573 drm_mode_destroy(dev, match_mode);
2574 found = 1;
2575 break;
2576 }
2577 }
2578
2579 if (!found)
2580 ret = -EINVAL;
2581
2582 return ret;
2583}
2584
2585int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2586{
2587 struct drm_connector *connector;
2588
2589 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2590 drm_mode_detachmode(dev, connector, mode);
2591 }
2592 return 0;
2593}
2594EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2595
2596/**
2597 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002598 * @dev: drm device for the ioctl
2599 * @data: data pointer for the ioctl
2600 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002601 *
2602 * This attaches a user specified mode to an connector.
2603 * Called by the user via ioctl.
2604 *
2605 * RETURNS:
2606 * Zero on success, errno on failure.
2607 */
2608int drm_mode_attachmode_ioctl(struct drm_device *dev,
2609 void *data, struct drm_file *file_priv)
2610{
2611 struct drm_mode_mode_cmd *mode_cmd = data;
2612 struct drm_connector *connector;
2613 struct drm_display_mode *mode;
2614 struct drm_mode_object *obj;
2615 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002616 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002617
Dave Airliefb3b06c2011-02-08 13:55:21 +10002618 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2619 return -EINVAL;
2620
Dave Airlief453ba02008-11-07 14:05:41 -08002621 mutex_lock(&dev->mode_config.mutex);
2622
2623 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2624 if (!obj) {
2625 ret = -EINVAL;
2626 goto out;
2627 }
2628 connector = obj_to_connector(obj);
2629
2630 mode = drm_mode_create(dev);
2631 if (!mode) {
2632 ret = -ENOMEM;
2633 goto out;
2634 }
2635
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002636 ret = drm_crtc_convert_umode(mode, umode);
2637 if (ret) {
2638 DRM_DEBUG_KMS("Invalid mode\n");
2639 drm_mode_destroy(dev, mode);
2640 goto out;
2641 }
Dave Airlief453ba02008-11-07 14:05:41 -08002642
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002643 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002644out:
2645 mutex_unlock(&dev->mode_config.mutex);
2646 return ret;
2647}
2648
2649
2650/**
2651 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002652 * @dev: drm device for the ioctl
2653 * @data: data pointer for the ioctl
2654 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002655 *
2656 * Called by the user via ioctl.
2657 *
2658 * RETURNS:
2659 * Zero on success, errno on failure.
2660 */
2661int drm_mode_detachmode_ioctl(struct drm_device *dev,
2662 void *data, struct drm_file *file_priv)
2663{
2664 struct drm_mode_object *obj;
2665 struct drm_mode_mode_cmd *mode_cmd = data;
2666 struct drm_connector *connector;
2667 struct drm_display_mode mode;
2668 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002669 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002670
Dave Airliefb3b06c2011-02-08 13:55:21 +10002671 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2672 return -EINVAL;
2673
Dave Airlief453ba02008-11-07 14:05:41 -08002674 mutex_lock(&dev->mode_config.mutex);
2675
2676 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2677 if (!obj) {
2678 ret = -EINVAL;
2679 goto out;
2680 }
2681 connector = obj_to_connector(obj);
2682
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002683 ret = drm_crtc_convert_umode(&mode, umode);
2684 if (ret) {
2685 DRM_DEBUG_KMS("Invalid mode\n");
2686 goto out;
2687 }
2688
Dave Airlief453ba02008-11-07 14:05:41 -08002689 ret = drm_mode_detachmode(dev, connector, &mode);
2690out:
2691 mutex_unlock(&dev->mode_config.mutex);
2692 return ret;
2693}
2694
2695struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2696 const char *name, int num_values)
2697{
2698 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002699 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002700
2701 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2702 if (!property)
2703 return NULL;
2704
2705 if (num_values) {
2706 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2707 if (!property->values)
2708 goto fail;
2709 }
2710
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002711 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2712 if (ret)
2713 goto fail;
2714
Dave Airlief453ba02008-11-07 14:05:41 -08002715 property->flags = flags;
2716 property->num_values = num_values;
2717 INIT_LIST_HEAD(&property->enum_blob_list);
2718
Vinson Lee471dd2e2011-11-10 11:55:40 -08002719 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002720 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002721 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2722 }
Dave Airlief453ba02008-11-07 14:05:41 -08002723
2724 list_add_tail(&property->head, &dev->mode_config.property_list);
2725 return property;
2726fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002727 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002728 kfree(property);
2729 return NULL;
2730}
2731EXPORT_SYMBOL(drm_property_create);
2732
Sascha Hauer4a67d392012-02-06 10:58:17 +01002733struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2734 const char *name,
2735 const struct drm_prop_enum_list *props,
2736 int num_values)
2737{
2738 struct drm_property *property;
2739 int i, ret;
2740
2741 flags |= DRM_MODE_PROP_ENUM;
2742
2743 property = drm_property_create(dev, flags, name, num_values);
2744 if (!property)
2745 return NULL;
2746
2747 for (i = 0; i < num_values; i++) {
2748 ret = drm_property_add_enum(property, i,
2749 props[i].type,
2750 props[i].name);
2751 if (ret) {
2752 drm_property_destroy(dev, property);
2753 return NULL;
2754 }
2755 }
2756
2757 return property;
2758}
2759EXPORT_SYMBOL(drm_property_create_enum);
2760
Rob Clark49e27542012-05-17 02:23:26 -06002761struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2762 int flags, const char *name,
2763 const struct drm_prop_enum_list *props,
2764 int num_values)
2765{
2766 struct drm_property *property;
2767 int i, ret;
2768
2769 flags |= DRM_MODE_PROP_BITMASK;
2770
2771 property = drm_property_create(dev, flags, name, num_values);
2772 if (!property)
2773 return NULL;
2774
2775 for (i = 0; i < num_values; i++) {
2776 ret = drm_property_add_enum(property, i,
2777 props[i].type,
2778 props[i].name);
2779 if (ret) {
2780 drm_property_destroy(dev, property);
2781 return NULL;
2782 }
2783 }
2784
2785 return property;
2786}
2787EXPORT_SYMBOL(drm_property_create_bitmask);
2788
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002789struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2790 const char *name,
2791 uint64_t min, uint64_t max)
2792{
2793 struct drm_property *property;
2794
2795 flags |= DRM_MODE_PROP_RANGE;
2796
2797 property = drm_property_create(dev, flags, name, 2);
2798 if (!property)
2799 return NULL;
2800
2801 property->values[0] = min;
2802 property->values[1] = max;
2803
2804 return property;
2805}
2806EXPORT_SYMBOL(drm_property_create_range);
2807
Dave Airlief453ba02008-11-07 14:05:41 -08002808int drm_property_add_enum(struct drm_property *property, int index,
2809 uint64_t value, const char *name)
2810{
2811 struct drm_property_enum *prop_enum;
2812
Rob Clark49e27542012-05-17 02:23:26 -06002813 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2814 return -EINVAL;
2815
2816 /*
2817 * Bitmask enum properties have the additional constraint of values
2818 * from 0 to 63
2819 */
2820 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002821 return -EINVAL;
2822
2823 if (!list_empty(&property->enum_blob_list)) {
2824 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2825 if (prop_enum->value == value) {
2826 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2827 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2828 return 0;
2829 }
2830 }
2831 }
2832
2833 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2834 if (!prop_enum)
2835 return -ENOMEM;
2836
2837 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2838 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2839 prop_enum->value = value;
2840
2841 property->values[index] = value;
2842 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2843 return 0;
2844}
2845EXPORT_SYMBOL(drm_property_add_enum);
2846
2847void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2848{
2849 struct drm_property_enum *prop_enum, *pt;
2850
2851 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2852 list_del(&prop_enum->head);
2853 kfree(prop_enum);
2854 }
2855
2856 if (property->num_values)
2857 kfree(property->values);
2858 drm_mode_object_put(dev, &property->base);
2859 list_del(&property->head);
2860 kfree(property);
2861}
2862EXPORT_SYMBOL(drm_property_destroy);
2863
Paulo Zanonic5431882012-05-15 18:09:02 -03002864void drm_object_attach_property(struct drm_mode_object *obj,
2865 struct drm_property *property,
2866 uint64_t init_val)
2867{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002868 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002869
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002870 if (count == DRM_OBJECT_MAX_PROPERTY) {
2871 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2872 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2873 "you see this message on the same object type.\n",
2874 obj->type);
2875 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002876 }
2877
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002878 obj->properties->ids[count] = property->base.id;
2879 obj->properties->values[count] = init_val;
2880 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002881}
2882EXPORT_SYMBOL(drm_object_attach_property);
2883
2884int drm_object_property_set_value(struct drm_mode_object *obj,
2885 struct drm_property *property, uint64_t val)
2886{
2887 int i;
2888
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002889 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002890 if (obj->properties->ids[i] == property->base.id) {
2891 obj->properties->values[i] = val;
2892 return 0;
2893 }
2894 }
2895
2896 return -EINVAL;
2897}
2898EXPORT_SYMBOL(drm_object_property_set_value);
2899
2900int drm_object_property_get_value(struct drm_mode_object *obj,
2901 struct drm_property *property, uint64_t *val)
2902{
2903 int i;
2904
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002905 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002906 if (obj->properties->ids[i] == property->base.id) {
2907 *val = obj->properties->values[i];
2908 return 0;
2909 }
2910 }
2911
2912 return -EINVAL;
2913}
2914EXPORT_SYMBOL(drm_object_property_get_value);
2915
Dave Airlief453ba02008-11-07 14:05:41 -08002916int drm_mode_getproperty_ioctl(struct drm_device *dev,
2917 void *data, struct drm_file *file_priv)
2918{
2919 struct drm_mode_object *obj;
2920 struct drm_mode_get_property *out_resp = data;
2921 struct drm_property *property;
2922 int enum_count = 0;
2923 int blob_count = 0;
2924 int value_count = 0;
2925 int ret = 0, i;
2926 int copied;
2927 struct drm_property_enum *prop_enum;
2928 struct drm_mode_property_enum __user *enum_ptr;
2929 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002930 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002931 uint64_t __user *values_ptr;
2932 uint32_t __user *blob_length_ptr;
2933
Dave Airliefb3b06c2011-02-08 13:55:21 +10002934 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2935 return -EINVAL;
2936
Dave Airlief453ba02008-11-07 14:05:41 -08002937 mutex_lock(&dev->mode_config.mutex);
2938 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2939 if (!obj) {
2940 ret = -EINVAL;
2941 goto done;
2942 }
2943 property = obj_to_property(obj);
2944
Rob Clark49e27542012-05-17 02:23:26 -06002945 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002946 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2947 enum_count++;
2948 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2949 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2950 blob_count++;
2951 }
2952
2953 value_count = property->num_values;
2954
2955 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2956 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2957 out_resp->flags = property->flags;
2958
2959 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002960 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002961 for (i = 0; i < value_count; i++) {
2962 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2963 ret = -EFAULT;
2964 goto done;
2965 }
2966 }
2967 }
2968 out_resp->count_values = value_count;
2969
Rob Clark49e27542012-05-17 02:23:26 -06002970 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002971 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2972 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002973 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002974 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2975
2976 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2977 ret = -EFAULT;
2978 goto done;
2979 }
2980
2981 if (copy_to_user(&enum_ptr[copied].name,
2982 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2983 ret = -EFAULT;
2984 goto done;
2985 }
2986 copied++;
2987 }
2988 }
2989 out_resp->count_enum_blobs = enum_count;
2990 }
2991
2992 if (property->flags & DRM_MODE_PROP_BLOB) {
2993 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2994 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002995 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2996 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002997
2998 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2999 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3000 ret = -EFAULT;
3001 goto done;
3002 }
3003
3004 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3005 ret = -EFAULT;
3006 goto done;
3007 }
3008
3009 copied++;
3010 }
3011 }
3012 out_resp->count_enum_blobs = blob_count;
3013 }
3014done:
3015 mutex_unlock(&dev->mode_config.mutex);
3016 return ret;
3017}
3018
3019static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3020 void *data)
3021{
3022 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003023 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003024
3025 if (!length || !data)
3026 return NULL;
3027
3028 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3029 if (!blob)
3030 return NULL;
3031
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003032 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3033 if (ret) {
3034 kfree(blob);
3035 return NULL;
3036 }
3037
Dave Airlief453ba02008-11-07 14:05:41 -08003038 blob->length = length;
3039
3040 memcpy(blob->data, data, length);
3041
Dave Airlief453ba02008-11-07 14:05:41 -08003042 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3043 return blob;
3044}
3045
3046static void drm_property_destroy_blob(struct drm_device *dev,
3047 struct drm_property_blob *blob)
3048{
3049 drm_mode_object_put(dev, &blob->base);
3050 list_del(&blob->head);
3051 kfree(blob);
3052}
3053
3054int drm_mode_getblob_ioctl(struct drm_device *dev,
3055 void *data, struct drm_file *file_priv)
3056{
3057 struct drm_mode_object *obj;
3058 struct drm_mode_get_blob *out_resp = data;
3059 struct drm_property_blob *blob;
3060 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003061 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003062
Dave Airliefb3b06c2011-02-08 13:55:21 +10003063 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3064 return -EINVAL;
3065
Dave Airlief453ba02008-11-07 14:05:41 -08003066 mutex_lock(&dev->mode_config.mutex);
3067 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3068 if (!obj) {
3069 ret = -EINVAL;
3070 goto done;
3071 }
3072 blob = obj_to_blob(obj);
3073
3074 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003075 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003076 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3077 ret = -EFAULT;
3078 goto done;
3079 }
3080 }
3081 out_resp->length = blob->length;
3082
3083done:
3084 mutex_unlock(&dev->mode_config.mutex);
3085 return ret;
3086}
3087
3088int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3089 struct edid *edid)
3090{
3091 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003092 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003093
3094 if (connector->edid_blob_ptr)
3095 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3096
3097 /* Delete edid, when there is none. */
3098 if (!edid) {
3099 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003100 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003101 return ret;
3102 }
3103
Adam Jackson7466f4c2010-03-29 21:43:23 +00003104 size = EDID_LENGTH * (1 + edid->extensions);
3105 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3106 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003107 if (!connector->edid_blob_ptr)
3108 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003109
Rob Clark58495562012-10-11 20:50:56 -05003110 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003111 dev->mode_config.edid_property,
3112 connector->edid_blob_ptr->base.id);
3113
3114 return ret;
3115}
3116EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3117
Paulo Zanoni26a34812012-05-15 18:08:59 -03003118static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003119 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003120{
3121 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3122 return false;
3123 if (property->flags & DRM_MODE_PROP_RANGE) {
3124 if (value < property->values[0] || value > property->values[1])
3125 return false;
3126 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003127 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3128 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003129 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003130 for (i = 0; i < property->num_values; i++)
3131 valid_mask |= (1ULL << property->values[i]);
3132 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003133 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3134 /* Only the driver knows */
3135 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003136 } else {
3137 int i;
3138 for (i = 0; i < property->num_values; i++)
3139 if (property->values[i] == value)
3140 return true;
3141 return false;
3142 }
3143}
3144
Dave Airlief453ba02008-11-07 14:05:41 -08003145int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3146 void *data, struct drm_file *file_priv)
3147{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003148 struct drm_mode_connector_set_property *conn_set_prop = data;
3149 struct drm_mode_obj_set_property obj_set_prop = {
3150 .value = conn_set_prop->value,
3151 .prop_id = conn_set_prop->prop_id,
3152 .obj_id = conn_set_prop->connector_id,
3153 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3154 };
Dave Airlief453ba02008-11-07 14:05:41 -08003155
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003156 /* It does all the locking and checking we need */
3157 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003158}
3159
Paulo Zanonic5431882012-05-15 18:09:02 -03003160static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3161 struct drm_property *property,
3162 uint64_t value)
3163{
3164 int ret = -EINVAL;
3165 struct drm_connector *connector = obj_to_connector(obj);
3166
3167 /* Do DPMS ourselves */
3168 if (property == connector->dev->mode_config.dpms_property) {
3169 if (connector->funcs->dpms)
3170 (*connector->funcs->dpms)(connector, (int)value);
3171 ret = 0;
3172 } else if (connector->funcs->set_property)
3173 ret = connector->funcs->set_property(connector, property, value);
3174
3175 /* store the property value if successful */
3176 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003177 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003178 return ret;
3179}
3180
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003181static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3182 struct drm_property *property,
3183 uint64_t value)
3184{
3185 int ret = -EINVAL;
3186 struct drm_crtc *crtc = obj_to_crtc(obj);
3187
3188 if (crtc->funcs->set_property)
3189 ret = crtc->funcs->set_property(crtc, property, value);
3190 if (!ret)
3191 drm_object_property_set_value(obj, property, value);
3192
3193 return ret;
3194}
3195
Rob Clark4d939142012-05-17 02:23:27 -06003196static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3197 struct drm_property *property,
3198 uint64_t value)
3199{
3200 int ret = -EINVAL;
3201 struct drm_plane *plane = obj_to_plane(obj);
3202
3203 if (plane->funcs->set_property)
3204 ret = plane->funcs->set_property(plane, property, value);
3205 if (!ret)
3206 drm_object_property_set_value(obj, property, value);
3207
3208 return ret;
3209}
3210
Paulo Zanonic5431882012-05-15 18:09:02 -03003211int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3212 struct drm_file *file_priv)
3213{
3214 struct drm_mode_obj_get_properties *arg = data;
3215 struct drm_mode_object *obj;
3216 int ret = 0;
3217 int i;
3218 int copied = 0;
3219 int props_count = 0;
3220 uint32_t __user *props_ptr;
3221 uint64_t __user *prop_values_ptr;
3222
3223 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3224 return -EINVAL;
3225
3226 mutex_lock(&dev->mode_config.mutex);
3227
3228 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3229 if (!obj) {
3230 ret = -EINVAL;
3231 goto out;
3232 }
3233 if (!obj->properties) {
3234 ret = -EINVAL;
3235 goto out;
3236 }
3237
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003238 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003239
3240 /* This ioctl is called twice, once to determine how much space is
3241 * needed, and the 2nd time to fill it. */
3242 if ((arg->count_props >= props_count) && props_count) {
3243 copied = 0;
3244 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3245 prop_values_ptr = (uint64_t __user *)(unsigned long)
3246 (arg->prop_values_ptr);
3247 for (i = 0; i < props_count; i++) {
3248 if (put_user(obj->properties->ids[i],
3249 props_ptr + copied)) {
3250 ret = -EFAULT;
3251 goto out;
3252 }
3253 if (put_user(obj->properties->values[i],
3254 prop_values_ptr + copied)) {
3255 ret = -EFAULT;
3256 goto out;
3257 }
3258 copied++;
3259 }
3260 }
3261 arg->count_props = props_count;
3262out:
3263 mutex_unlock(&dev->mode_config.mutex);
3264 return ret;
3265}
3266
3267int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3268 struct drm_file *file_priv)
3269{
3270 struct drm_mode_obj_set_property *arg = data;
3271 struct drm_mode_object *arg_obj;
3272 struct drm_mode_object *prop_obj;
3273 struct drm_property *property;
3274 int ret = -EINVAL;
3275 int i;
3276
3277 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3278 return -EINVAL;
3279
3280 mutex_lock(&dev->mode_config.mutex);
3281
3282 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3283 if (!arg_obj)
3284 goto out;
3285 if (!arg_obj->properties)
3286 goto out;
3287
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003288 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003289 if (arg_obj->properties->ids[i] == arg->prop_id)
3290 break;
3291
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003292 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003293 goto out;
3294
3295 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3296 DRM_MODE_OBJECT_PROPERTY);
3297 if (!prop_obj)
3298 goto out;
3299 property = obj_to_property(prop_obj);
3300
3301 if (!drm_property_change_is_valid(property, arg->value))
3302 goto out;
3303
3304 switch (arg_obj->type) {
3305 case DRM_MODE_OBJECT_CONNECTOR:
3306 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3307 arg->value);
3308 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003309 case DRM_MODE_OBJECT_CRTC:
3310 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3311 break;
Rob Clark4d939142012-05-17 02:23:27 -06003312 case DRM_MODE_OBJECT_PLANE:
3313 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3314 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003315 }
3316
3317out:
3318 mutex_unlock(&dev->mode_config.mutex);
3319 return ret;
3320}
3321
Dave Airlief453ba02008-11-07 14:05:41 -08003322int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3323 struct drm_encoder *encoder)
3324{
3325 int i;
3326
3327 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3328 if (connector->encoder_ids[i] == 0) {
3329 connector->encoder_ids[i] = encoder->base.id;
3330 return 0;
3331 }
3332 }
3333 return -ENOMEM;
3334}
3335EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3336
3337void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3338 struct drm_encoder *encoder)
3339{
3340 int i;
3341 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3342 if (connector->encoder_ids[i] == encoder->base.id) {
3343 connector->encoder_ids[i] = 0;
3344 if (connector->encoder == encoder)
3345 connector->encoder = NULL;
3346 break;
3347 }
3348 }
3349}
3350EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3351
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003352int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003353 int gamma_size)
3354{
3355 crtc->gamma_size = gamma_size;
3356
3357 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3358 if (!crtc->gamma_store) {
3359 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003360 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003361 }
3362
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003363 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003364}
3365EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3366
3367int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3368 void *data, struct drm_file *file_priv)
3369{
3370 struct drm_mode_crtc_lut *crtc_lut = data;
3371 struct drm_mode_object *obj;
3372 struct drm_crtc *crtc;
3373 void *r_base, *g_base, *b_base;
3374 int size;
3375 int ret = 0;
3376
Dave Airliefb3b06c2011-02-08 13:55:21 +10003377 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3378 return -EINVAL;
3379
Dave Airlief453ba02008-11-07 14:05:41 -08003380 mutex_lock(&dev->mode_config.mutex);
3381 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3382 if (!obj) {
3383 ret = -EINVAL;
3384 goto out;
3385 }
3386 crtc = obj_to_crtc(obj);
3387
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003388 if (crtc->funcs->gamma_set == NULL) {
3389 ret = -ENOSYS;
3390 goto out;
3391 }
3392
Dave Airlief453ba02008-11-07 14:05:41 -08003393 /* memcpy into gamma store */
3394 if (crtc_lut->gamma_size != crtc->gamma_size) {
3395 ret = -EINVAL;
3396 goto out;
3397 }
3398
3399 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3400 r_base = crtc->gamma_store;
3401 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3402 ret = -EFAULT;
3403 goto out;
3404 }
3405
3406 g_base = r_base + size;
3407 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3408 ret = -EFAULT;
3409 goto out;
3410 }
3411
3412 b_base = g_base + size;
3413 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3414 ret = -EFAULT;
3415 goto out;
3416 }
3417
James Simmons72034252010-08-03 01:33:19 +01003418 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003419
3420out:
3421 mutex_unlock(&dev->mode_config.mutex);
3422 return ret;
3423
3424}
3425
3426int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3427 void *data, struct drm_file *file_priv)
3428{
3429 struct drm_mode_crtc_lut *crtc_lut = data;
3430 struct drm_mode_object *obj;
3431 struct drm_crtc *crtc;
3432 void *r_base, *g_base, *b_base;
3433 int size;
3434 int ret = 0;
3435
Dave Airliefb3b06c2011-02-08 13:55:21 +10003436 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3437 return -EINVAL;
3438
Dave Airlief453ba02008-11-07 14:05:41 -08003439 mutex_lock(&dev->mode_config.mutex);
3440 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3441 if (!obj) {
3442 ret = -EINVAL;
3443 goto out;
3444 }
3445 crtc = obj_to_crtc(obj);
3446
3447 /* memcpy into gamma store */
3448 if (crtc_lut->gamma_size != crtc->gamma_size) {
3449 ret = -EINVAL;
3450 goto out;
3451 }
3452
3453 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3454 r_base = crtc->gamma_store;
3455 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3456 ret = -EFAULT;
3457 goto out;
3458 }
3459
3460 g_base = r_base + size;
3461 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3462 ret = -EFAULT;
3463 goto out;
3464 }
3465
3466 b_base = g_base + size;
3467 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3468 ret = -EFAULT;
3469 goto out;
3470 }
3471out:
3472 mutex_unlock(&dev->mode_config.mutex);
3473 return ret;
3474}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003475
3476int drm_mode_page_flip_ioctl(struct drm_device *dev,
3477 void *data, struct drm_file *file_priv)
3478{
3479 struct drm_mode_crtc_page_flip *page_flip = data;
3480 struct drm_mode_object *obj;
3481 struct drm_crtc *crtc;
3482 struct drm_framebuffer *fb;
3483 struct drm_pending_vblank_event *e = NULL;
3484 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003485 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003486 int ret = -EINVAL;
3487
3488 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3489 page_flip->reserved != 0)
3490 return -EINVAL;
3491
3492 mutex_lock(&dev->mode_config.mutex);
3493 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3494 if (!obj)
3495 goto out;
3496 crtc = obj_to_crtc(obj);
3497
Chris Wilson90c1efd2010-07-17 20:23:26 +01003498 if (crtc->fb == NULL) {
3499 /* The framebuffer is currently unbound, presumably
3500 * due to a hotplug event, that userspace has not
3501 * yet discovered.
3502 */
3503 ret = -EBUSY;
3504 goto out;
3505 }
3506
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003507 if (crtc->funcs->page_flip == NULL)
3508 goto out;
3509
3510 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3511 if (!obj)
3512 goto out;
3513 fb = obj_to_fb(obj);
3514
Rob Clark7c80e122012-09-04 16:35:56 +00003515 hdisplay = crtc->mode.hdisplay;
3516 vdisplay = crtc->mode.vdisplay;
3517
3518 if (crtc->invert_dimensions)
3519 swap(hdisplay, vdisplay);
3520
3521 if (hdisplay > fb->width ||
3522 vdisplay > fb->height ||
3523 crtc->x > fb->width - hdisplay ||
3524 crtc->y > fb->height - vdisplay) {
3525 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3526 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3527 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003528 ret = -ENOSPC;
3529 goto out;
3530 }
3531
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003532 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3533 ret = -ENOMEM;
3534 spin_lock_irqsave(&dev->event_lock, flags);
3535 if (file_priv->event_space < sizeof e->event) {
3536 spin_unlock_irqrestore(&dev->event_lock, flags);
3537 goto out;
3538 }
3539 file_priv->event_space -= sizeof e->event;
3540 spin_unlock_irqrestore(&dev->event_lock, flags);
3541
3542 e = kzalloc(sizeof *e, GFP_KERNEL);
3543 if (e == NULL) {
3544 spin_lock_irqsave(&dev->event_lock, flags);
3545 file_priv->event_space += sizeof e->event;
3546 spin_unlock_irqrestore(&dev->event_lock, flags);
3547 goto out;
3548 }
3549
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003550 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003551 e->event.base.length = sizeof e->event;
3552 e->event.user_data = page_flip->user_data;
3553 e->base.event = &e->event.base;
3554 e->base.file_priv = file_priv;
3555 e->base.destroy =
3556 (void (*) (struct drm_pending_event *)) kfree;
3557 }
3558
3559 ret = crtc->funcs->page_flip(crtc, fb, e);
3560 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003561 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3562 spin_lock_irqsave(&dev->event_lock, flags);
3563 file_priv->event_space += sizeof e->event;
3564 spin_unlock_irqrestore(&dev->event_lock, flags);
3565 kfree(e);
3566 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003567 }
3568
3569out:
3570 mutex_unlock(&dev->mode_config.mutex);
3571 return ret;
3572}
Chris Wilsoneb0335562011-01-24 15:11:08 +00003573
3574void drm_mode_config_reset(struct drm_device *dev)
3575{
3576 struct drm_crtc *crtc;
3577 struct drm_encoder *encoder;
3578 struct drm_connector *connector;
3579
3580 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3581 if (crtc->funcs->reset)
3582 crtc->funcs->reset(crtc);
3583
3584 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3585 if (encoder->funcs->reset)
3586 encoder->funcs->reset(encoder);
3587
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003588 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3589 connector->status = connector_status_unknown;
3590
Chris Wilsoneb0335562011-01-24 15:11:08 +00003591 if (connector->funcs->reset)
3592 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003593 }
Chris Wilsoneb0335562011-01-24 15:11:08 +00003594}
3595EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003596
3597int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3598 void *data, struct drm_file *file_priv)
3599{
3600 struct drm_mode_create_dumb *args = data;
3601
3602 if (!dev->driver->dumb_create)
3603 return -ENOSYS;
3604 return dev->driver->dumb_create(file_priv, dev, args);
3605}
3606
3607int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3608 void *data, struct drm_file *file_priv)
3609{
3610 struct drm_mode_map_dumb *args = data;
3611
3612 /* call driver ioctl to get mmap offset */
3613 if (!dev->driver->dumb_map_offset)
3614 return -ENOSYS;
3615
3616 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3617}
3618
3619int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3620 void *data, struct drm_file *file_priv)
3621{
3622 struct drm_mode_destroy_dumb *args = data;
3623
3624 if (!dev->driver->dumb_destroy)
3625 return -ENOSYS;
3626
3627 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3628}
Dave Airlie248dbc22011-11-29 20:02:54 +00003629
3630/*
3631 * Just need to support RGB formats here for compat with code that doesn't
3632 * use pixel formats directly yet.
3633 */
3634void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3635 int *bpp)
3636{
3637 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003638 case DRM_FORMAT_RGB332:
3639 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003640 *depth = 8;
3641 *bpp = 8;
3642 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003643 case DRM_FORMAT_XRGB1555:
3644 case DRM_FORMAT_XBGR1555:
3645 case DRM_FORMAT_RGBX5551:
3646 case DRM_FORMAT_BGRX5551:
3647 case DRM_FORMAT_ARGB1555:
3648 case DRM_FORMAT_ABGR1555:
3649 case DRM_FORMAT_RGBA5551:
3650 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003651 *depth = 15;
3652 *bpp = 16;
3653 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003654 case DRM_FORMAT_RGB565:
3655 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003656 *depth = 16;
3657 *bpp = 16;
3658 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003659 case DRM_FORMAT_RGB888:
3660 case DRM_FORMAT_BGR888:
3661 *depth = 24;
3662 *bpp = 24;
3663 break;
3664 case DRM_FORMAT_XRGB8888:
3665 case DRM_FORMAT_XBGR8888:
3666 case DRM_FORMAT_RGBX8888:
3667 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003668 *depth = 24;
3669 *bpp = 32;
3670 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003671 case DRM_FORMAT_XRGB2101010:
3672 case DRM_FORMAT_XBGR2101010:
3673 case DRM_FORMAT_RGBX1010102:
3674 case DRM_FORMAT_BGRX1010102:
3675 case DRM_FORMAT_ARGB2101010:
3676 case DRM_FORMAT_ABGR2101010:
3677 case DRM_FORMAT_RGBA1010102:
3678 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003679 *depth = 30;
3680 *bpp = 32;
3681 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003682 case DRM_FORMAT_ARGB8888:
3683 case DRM_FORMAT_ABGR8888:
3684 case DRM_FORMAT_RGBA8888:
3685 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003686 *depth = 32;
3687 *bpp = 32;
3688 break;
3689 default:
3690 DRM_DEBUG_KMS("unsupported pixel format\n");
3691 *depth = 0;
3692 *bpp = 0;
3693 break;
3694 }
3695}
3696EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003697
3698/**
3699 * drm_format_num_planes - get the number of planes for format
3700 * @format: pixel format (DRM_FORMAT_*)
3701 *
3702 * RETURNS:
3703 * The number of planes used by the specified pixel format.
3704 */
3705int drm_format_num_planes(uint32_t format)
3706{
3707 switch (format) {
3708 case DRM_FORMAT_YUV410:
3709 case DRM_FORMAT_YVU410:
3710 case DRM_FORMAT_YUV411:
3711 case DRM_FORMAT_YVU411:
3712 case DRM_FORMAT_YUV420:
3713 case DRM_FORMAT_YVU420:
3714 case DRM_FORMAT_YUV422:
3715 case DRM_FORMAT_YVU422:
3716 case DRM_FORMAT_YUV444:
3717 case DRM_FORMAT_YVU444:
3718 return 3;
3719 case DRM_FORMAT_NV12:
3720 case DRM_FORMAT_NV21:
3721 case DRM_FORMAT_NV16:
3722 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003723 case DRM_FORMAT_NV24:
3724 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003725 return 2;
3726 default:
3727 return 1;
3728 }
3729}
3730EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003731
3732/**
3733 * drm_format_plane_cpp - determine the bytes per pixel value
3734 * @format: pixel format (DRM_FORMAT_*)
3735 * @plane: plane index
3736 *
3737 * RETURNS:
3738 * The bytes per pixel value for the specified plane.
3739 */
3740int drm_format_plane_cpp(uint32_t format, int plane)
3741{
3742 unsigned int depth;
3743 int bpp;
3744
3745 if (plane >= drm_format_num_planes(format))
3746 return 0;
3747
3748 switch (format) {
3749 case DRM_FORMAT_YUYV:
3750 case DRM_FORMAT_YVYU:
3751 case DRM_FORMAT_UYVY:
3752 case DRM_FORMAT_VYUY:
3753 return 2;
3754 case DRM_FORMAT_NV12:
3755 case DRM_FORMAT_NV21:
3756 case DRM_FORMAT_NV16:
3757 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003758 case DRM_FORMAT_NV24:
3759 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003760 return plane ? 2 : 1;
3761 case DRM_FORMAT_YUV410:
3762 case DRM_FORMAT_YVU410:
3763 case DRM_FORMAT_YUV411:
3764 case DRM_FORMAT_YVU411:
3765 case DRM_FORMAT_YUV420:
3766 case DRM_FORMAT_YVU420:
3767 case DRM_FORMAT_YUV422:
3768 case DRM_FORMAT_YVU422:
3769 case DRM_FORMAT_YUV444:
3770 case DRM_FORMAT_YVU444:
3771 return 1;
3772 default:
3773 drm_fb_get_bpp_depth(format, &depth, &bpp);
3774 return bpp >> 3;
3775 }
3776}
3777EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003778
3779/**
3780 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3781 * @format: pixel format (DRM_FORMAT_*)
3782 *
3783 * RETURNS:
3784 * The horizontal chroma subsampling factor for the
3785 * specified pixel format.
3786 */
3787int drm_format_horz_chroma_subsampling(uint32_t format)
3788{
3789 switch (format) {
3790 case DRM_FORMAT_YUV411:
3791 case DRM_FORMAT_YVU411:
3792 case DRM_FORMAT_YUV410:
3793 case DRM_FORMAT_YVU410:
3794 return 4;
3795 case DRM_FORMAT_YUYV:
3796 case DRM_FORMAT_YVYU:
3797 case DRM_FORMAT_UYVY:
3798 case DRM_FORMAT_VYUY:
3799 case DRM_FORMAT_NV12:
3800 case DRM_FORMAT_NV21:
3801 case DRM_FORMAT_NV16:
3802 case DRM_FORMAT_NV61:
3803 case DRM_FORMAT_YUV422:
3804 case DRM_FORMAT_YVU422:
3805 case DRM_FORMAT_YUV420:
3806 case DRM_FORMAT_YVU420:
3807 return 2;
3808 default:
3809 return 1;
3810 }
3811}
3812EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3813
3814/**
3815 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3816 * @format: pixel format (DRM_FORMAT_*)
3817 *
3818 * RETURNS:
3819 * The vertical chroma subsampling factor for the
3820 * specified pixel format.
3821 */
3822int drm_format_vert_chroma_subsampling(uint32_t format)
3823{
3824 switch (format) {
3825 case DRM_FORMAT_YUV410:
3826 case DRM_FORMAT_YVU410:
3827 return 4;
3828 case DRM_FORMAT_YUV420:
3829 case DRM_FORMAT_YVU420:
3830 case DRM_FORMAT_NV12:
3831 case DRM_FORMAT_NV21:
3832 return 2;
3833 default:
3834 return 1;
3835 }
3836}
3837EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);