blob: a9abf49bb3eff7bd66698e8d8b7701234e4ed514 [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;
384 ret = crtc->funcs->set_config(&set);
385 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/**
Dave Airlief453ba02008-11-07 14:05:41 -08001804 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001805 * @dev: drm device for the ioctl
1806 * @data: data pointer for the ioctl
1807 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001808 *
Dave Airlief453ba02008-11-07 14:05:41 -08001809 * Build a new CRTC configuration based on user request.
1810 *
1811 * Called by the user via ioctl.
1812 *
1813 * RETURNS:
1814 * Zero on success, errno on failure.
1815 */
1816int drm_mode_setcrtc(struct drm_device *dev, void *data,
1817 struct drm_file *file_priv)
1818{
1819 struct drm_mode_config *config = &dev->mode_config;
1820 struct drm_mode_crtc *crtc_req = data;
1821 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001822 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001823 struct drm_connector **connector_set = NULL, *connector;
1824 struct drm_framebuffer *fb = NULL;
1825 struct drm_display_mode *mode = NULL;
1826 struct drm_mode_set set;
1827 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001828 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001829 int i;
1830
Dave Airliefb3b06c2011-02-08 13:55:21 +10001831 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1832 return -EINVAL;
1833
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001834 /* For some reason crtc x/y offsets are signed internally. */
1835 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1836 return -ERANGE;
1837
Dave Airlief453ba02008-11-07 14:05:41 -08001838 mutex_lock(&dev->mode_config.mutex);
1839 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1840 DRM_MODE_OBJECT_CRTC);
1841 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001842 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001843 ret = -EINVAL;
1844 goto out;
1845 }
1846 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001847 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001848
1849 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001850 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001851 /* If we have a mode we need a framebuffer. */
1852 /* If we pass -1, set the mode with the currently bound fb */
1853 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001854 if (!crtc->fb) {
1855 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1856 ret = -EINVAL;
1857 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001858 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001859 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001860 } else {
1861 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1862 DRM_MODE_OBJECT_FB);
1863 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001864 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1865 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001866 ret = -EINVAL;
1867 goto out;
1868 }
1869 fb = obj_to_fb(obj);
1870 }
1871
1872 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001873 if (!mode) {
1874 ret = -ENOMEM;
1875 goto out;
1876 }
1877
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001878 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1879 if (ret) {
1880 DRM_DEBUG_KMS("Invalid mode\n");
1881 goto out;
1882 }
1883
Dave Airlief453ba02008-11-07 14:05:41 -08001884 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001885
Rob Clark7c80e122012-09-04 16:35:56 +00001886 hdisplay = mode->hdisplay;
1887 vdisplay = mode->vdisplay;
1888
1889 if (crtc->invert_dimensions)
1890 swap(hdisplay, vdisplay);
1891
1892 if (hdisplay > fb->width ||
1893 vdisplay > fb->height ||
1894 crtc_req->x > fb->width - hdisplay ||
1895 crtc_req->y > fb->height - vdisplay) {
1896 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1897 fb->width, fb->height,
1898 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1899 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001900 ret = -ENOSPC;
1901 goto out;
1902 }
Dave Airlief453ba02008-11-07 14:05:41 -08001903 }
1904
1905 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001906 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001907 ret = -EINVAL;
1908 goto out;
1909 }
1910
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001911 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001912 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001913 crtc_req->count_connectors);
1914 ret = -EINVAL;
1915 goto out;
1916 }
1917
1918 if (crtc_req->count_connectors > 0) {
1919 u32 out_id;
1920
1921 /* Avoid unbounded kernel memory allocation */
1922 if (crtc_req->count_connectors > config->num_connector) {
1923 ret = -EINVAL;
1924 goto out;
1925 }
1926
1927 connector_set = kmalloc(crtc_req->count_connectors *
1928 sizeof(struct drm_connector *),
1929 GFP_KERNEL);
1930 if (!connector_set) {
1931 ret = -ENOMEM;
1932 goto out;
1933 }
1934
1935 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001936 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001937 if (get_user(out_id, &set_connectors_ptr[i])) {
1938 ret = -EFAULT;
1939 goto out;
1940 }
1941
1942 obj = drm_mode_object_find(dev, out_id,
1943 DRM_MODE_OBJECT_CONNECTOR);
1944 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001945 DRM_DEBUG_KMS("Connector id %d unknown\n",
1946 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001947 ret = -EINVAL;
1948 goto out;
1949 }
1950 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001951 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1952 connector->base.id,
1953 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001954
1955 connector_set[i] = connector;
1956 }
1957 }
1958
1959 set.crtc = crtc;
1960 set.x = crtc_req->x;
1961 set.y = crtc_req->y;
1962 set.mode = mode;
1963 set.connectors = connector_set;
1964 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001965 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001966 ret = crtc->funcs->set_config(&set);
1967
1968out:
1969 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001970 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001971 mutex_unlock(&dev->mode_config.mutex);
1972 return ret;
1973}
1974
1975int drm_mode_cursor_ioctl(struct drm_device *dev,
1976 void *data, struct drm_file *file_priv)
1977{
1978 struct drm_mode_cursor *req = data;
1979 struct drm_mode_object *obj;
1980 struct drm_crtc *crtc;
1981 int ret = 0;
1982
Dave Airliefb3b06c2011-02-08 13:55:21 +10001983 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1984 return -EINVAL;
1985
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00001986 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08001987 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001988
1989 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001990 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001992 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001993 ret = -EINVAL;
1994 goto out;
1995 }
1996 crtc = obj_to_crtc(obj);
1997
1998 if (req->flags & DRM_MODE_CURSOR_BO) {
1999 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002000 ret = -ENXIO;
2001 goto out;
2002 }
2003 /* Turns off the cursor if handle is 0 */
2004 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2005 req->width, req->height);
2006 }
2007
2008 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2009 if (crtc->funcs->cursor_move) {
2010 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2011 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002012 ret = -EFAULT;
2013 goto out;
2014 }
2015 }
2016out:
2017 mutex_unlock(&dev->mode_config.mutex);
2018 return ret;
2019}
2020
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002021/* Original addfb only supported RGB formats, so figure out which one */
2022uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2023{
2024 uint32_t fmt;
2025
2026 switch (bpp) {
2027 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002028 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002029 break;
2030 case 16:
2031 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002032 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002033 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002034 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002035 break;
2036 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002037 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002038 break;
2039 case 32:
2040 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002041 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002042 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002045 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002046 break;
2047 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002048 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2049 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002050 break;
2051 }
2052
2053 return fmt;
2054}
2055EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2056
Dave Airlief453ba02008-11-07 14:05:41 -08002057/**
2058 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002059 * @dev: drm device for the ioctl
2060 * @data: data pointer for the ioctl
2061 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002062 *
Dave Airlief453ba02008-11-07 14:05:41 -08002063 * Add a new FB to the specified CRTC, given a user request.
2064 *
2065 * Called by the user via ioctl.
2066 *
2067 * RETURNS:
2068 * Zero on success, errno on failure.
2069 */
2070int drm_mode_addfb(struct drm_device *dev,
2071 void *data, struct drm_file *file_priv)
2072{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002073 struct drm_mode_fb_cmd *or = data;
2074 struct drm_mode_fb_cmd2 r = {};
2075 struct drm_mode_config *config = &dev->mode_config;
2076 struct drm_framebuffer *fb;
2077 int ret = 0;
2078
2079 /* Use new struct with format internally */
2080 r.fb_id = or->fb_id;
2081 r.width = or->width;
2082 r.height = or->height;
2083 r.pitches[0] = or->pitch;
2084 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2085 r.handles[0] = or->handle;
2086
2087 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2088 return -EINVAL;
2089
Jesse Barnesacb4b992011-11-07 12:03:23 -08002090 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002091 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002092
2093 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002094 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002095
2096 mutex_lock(&dev->mode_config.mutex);
2097
2098 /* TODO check buffer is sufficiently large */
2099 /* TODO setup destructor callback */
2100
2101 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2102 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002103 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002104 ret = PTR_ERR(fb);
2105 goto out;
2106 }
2107
2108 or->fb_id = fb->base.id;
2109 list_add(&fb->filp_head, &file_priv->fbs);
2110 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2111
2112out:
2113 mutex_unlock(&dev->mode_config.mutex);
2114 return ret;
2115}
2116
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002117static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002118{
2119 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2120
2121 switch (format) {
2122 case DRM_FORMAT_C8:
2123 case DRM_FORMAT_RGB332:
2124 case DRM_FORMAT_BGR233:
2125 case DRM_FORMAT_XRGB4444:
2126 case DRM_FORMAT_XBGR4444:
2127 case DRM_FORMAT_RGBX4444:
2128 case DRM_FORMAT_BGRX4444:
2129 case DRM_FORMAT_ARGB4444:
2130 case DRM_FORMAT_ABGR4444:
2131 case DRM_FORMAT_RGBA4444:
2132 case DRM_FORMAT_BGRA4444:
2133 case DRM_FORMAT_XRGB1555:
2134 case DRM_FORMAT_XBGR1555:
2135 case DRM_FORMAT_RGBX5551:
2136 case DRM_FORMAT_BGRX5551:
2137 case DRM_FORMAT_ARGB1555:
2138 case DRM_FORMAT_ABGR1555:
2139 case DRM_FORMAT_RGBA5551:
2140 case DRM_FORMAT_BGRA5551:
2141 case DRM_FORMAT_RGB565:
2142 case DRM_FORMAT_BGR565:
2143 case DRM_FORMAT_RGB888:
2144 case DRM_FORMAT_BGR888:
2145 case DRM_FORMAT_XRGB8888:
2146 case DRM_FORMAT_XBGR8888:
2147 case DRM_FORMAT_RGBX8888:
2148 case DRM_FORMAT_BGRX8888:
2149 case DRM_FORMAT_ARGB8888:
2150 case DRM_FORMAT_ABGR8888:
2151 case DRM_FORMAT_RGBA8888:
2152 case DRM_FORMAT_BGRA8888:
2153 case DRM_FORMAT_XRGB2101010:
2154 case DRM_FORMAT_XBGR2101010:
2155 case DRM_FORMAT_RGBX1010102:
2156 case DRM_FORMAT_BGRX1010102:
2157 case DRM_FORMAT_ARGB2101010:
2158 case DRM_FORMAT_ABGR2101010:
2159 case DRM_FORMAT_RGBA1010102:
2160 case DRM_FORMAT_BGRA1010102:
2161 case DRM_FORMAT_YUYV:
2162 case DRM_FORMAT_YVYU:
2163 case DRM_FORMAT_UYVY:
2164 case DRM_FORMAT_VYUY:
2165 case DRM_FORMAT_AYUV:
2166 case DRM_FORMAT_NV12:
2167 case DRM_FORMAT_NV21:
2168 case DRM_FORMAT_NV16:
2169 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002170 case DRM_FORMAT_NV24:
2171 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002172 case DRM_FORMAT_YUV410:
2173 case DRM_FORMAT_YVU410:
2174 case DRM_FORMAT_YUV411:
2175 case DRM_FORMAT_YVU411:
2176 case DRM_FORMAT_YUV420:
2177 case DRM_FORMAT_YVU420:
2178 case DRM_FORMAT_YUV422:
2179 case DRM_FORMAT_YVU422:
2180 case DRM_FORMAT_YUV444:
2181 case DRM_FORMAT_YVU444:
2182 return 0;
2183 default:
2184 return -EINVAL;
2185 }
2186}
2187
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002188static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002189{
2190 int ret, hsub, vsub, num_planes, i;
2191
2192 ret = format_check(r);
2193 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002194 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002195 return ret;
2196 }
2197
2198 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2199 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2200 num_planes = drm_format_num_planes(r->pixel_format);
2201
2202 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002203 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002204 return -EINVAL;
2205 }
2206
2207 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002208 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002209 return -EINVAL;
2210 }
2211
2212 for (i = 0; i < num_planes; i++) {
2213 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002214 unsigned int height = r->height / (i != 0 ? vsub : 1);
2215 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002216
2217 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002218 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002219 return -EINVAL;
2220 }
2221
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002222 if ((uint64_t) width * cpp > UINT_MAX)
2223 return -ERANGE;
2224
2225 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2226 return -ERANGE;
2227
2228 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002229 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002230 return -EINVAL;
2231 }
2232 }
2233
2234 return 0;
2235}
2236
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002237/**
2238 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002239 * @dev: drm device for the ioctl
2240 * @data: data pointer for the ioctl
2241 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002242 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002243 * Add a new FB to the specified CRTC, given a user request with format.
2244 *
2245 * Called by the user via ioctl.
2246 *
2247 * RETURNS:
2248 * Zero on success, errno on failure.
2249 */
2250int drm_mode_addfb2(struct drm_device *dev,
2251 void *data, struct drm_file *file_priv)
2252{
2253 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002254 struct drm_mode_config *config = &dev->mode_config;
2255 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002256 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002257
Dave Airliefb3b06c2011-02-08 13:55:21 +10002258 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2259 return -EINVAL;
2260
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002261 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2262 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2263 return -EINVAL;
2264 }
2265
Dave Airlief453ba02008-11-07 14:05:41 -08002266 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002267 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002268 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002269 return -EINVAL;
2270 }
2271 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002272 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002273 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002274 return -EINVAL;
2275 }
2276
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002277 ret = framebuffer_check(r);
2278 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002279 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002280
Dave Airlief453ba02008-11-07 14:05:41 -08002281 mutex_lock(&dev->mode_config.mutex);
2282
Dave Airlief453ba02008-11-07 14:05:41 -08002283 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002284 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002285 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002286 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002287 goto out;
2288 }
2289
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002290 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002291 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002292 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002293
2294out:
2295 mutex_unlock(&dev->mode_config.mutex);
2296 return ret;
2297}
2298
2299/**
2300 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002301 * @dev: drm device for the ioctl
2302 * @data: data pointer for the ioctl
2303 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002304 *
Dave Airlief453ba02008-11-07 14:05:41 -08002305 * Remove the FB specified by the user.
2306 *
2307 * Called by the user via ioctl.
2308 *
2309 * RETURNS:
2310 * Zero on success, errno on failure.
2311 */
2312int drm_mode_rmfb(struct drm_device *dev,
2313 void *data, struct drm_file *file_priv)
2314{
2315 struct drm_mode_object *obj;
2316 struct drm_framebuffer *fb = NULL;
2317 struct drm_framebuffer *fbl = NULL;
2318 uint32_t *id = data;
2319 int ret = 0;
2320 int found = 0;
2321
Dave Airliefb3b06c2011-02-08 13:55:21 +10002322 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2323 return -EINVAL;
2324
Dave Airlief453ba02008-11-07 14:05:41 -08002325 mutex_lock(&dev->mode_config.mutex);
2326 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002327 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002328 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002329 ret = -EINVAL;
2330 goto out;
2331 }
2332 fb = obj_to_fb(obj);
2333
2334 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2335 if (fb == fbl)
2336 found = 1;
2337
2338 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002339 ret = -EINVAL;
2340 goto out;
2341 }
2342
Rob Clarkf7eff602012-09-05 21:48:38 +00002343 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002344
2345out:
2346 mutex_unlock(&dev->mode_config.mutex);
2347 return ret;
2348}
2349
2350/**
2351 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002352 * @dev: drm device for the ioctl
2353 * @data: data pointer for the ioctl
2354 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002355 *
Dave Airlief453ba02008-11-07 14:05:41 -08002356 * Lookup the FB given its ID and return info about it.
2357 *
2358 * Called by the user via ioctl.
2359 *
2360 * RETURNS:
2361 * Zero on success, errno on failure.
2362 */
2363int drm_mode_getfb(struct drm_device *dev,
2364 void *data, struct drm_file *file_priv)
2365{
2366 struct drm_mode_fb_cmd *r = data;
2367 struct drm_mode_object *obj;
2368 struct drm_framebuffer *fb;
2369 int ret = 0;
2370
Dave Airliefb3b06c2011-02-08 13:55:21 +10002371 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2372 return -EINVAL;
2373
Dave Airlief453ba02008-11-07 14:05:41 -08002374 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002375 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002376 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002377 ret = -EINVAL;
2378 goto out;
2379 }
2380 fb = obj_to_fb(obj);
2381
2382 r->height = fb->height;
2383 r->width = fb->width;
2384 r->depth = fb->depth;
2385 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002386 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002387 if (fb->funcs->create_handle)
2388 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2389 else
2390 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002391
2392out:
2393 mutex_unlock(&dev->mode_config.mutex);
2394 return ret;
2395}
2396
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002397int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2398 void *data, struct drm_file *file_priv)
2399{
2400 struct drm_clip_rect __user *clips_ptr;
2401 struct drm_clip_rect *clips = NULL;
2402 struct drm_mode_fb_dirty_cmd *r = data;
2403 struct drm_mode_object *obj;
2404 struct drm_framebuffer *fb;
2405 unsigned flags;
2406 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002407 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002408
Dave Airliefb3b06c2011-02-08 13:55:21 +10002409 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2410 return -EINVAL;
2411
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002412 mutex_lock(&dev->mode_config.mutex);
2413 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2414 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002415 ret = -EINVAL;
2416 goto out_err1;
2417 }
2418 fb = obj_to_fb(obj);
2419
2420 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002421 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002422
2423 if (!num_clips != !clips_ptr) {
2424 ret = -EINVAL;
2425 goto out_err1;
2426 }
2427
2428 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2429
2430 /* If userspace annotates copy, clips must come in pairs */
2431 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2432 ret = -EINVAL;
2433 goto out_err1;
2434 }
2435
2436 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002437 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2438 ret = -EINVAL;
2439 goto out_err1;
2440 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002441 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2442 if (!clips) {
2443 ret = -ENOMEM;
2444 goto out_err1;
2445 }
2446
2447 ret = copy_from_user(clips, clips_ptr,
2448 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002449 if (ret) {
2450 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002451 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002452 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002453 }
2454
2455 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002456 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2457 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002458 } else {
2459 ret = -ENOSYS;
2460 goto out_err2;
2461 }
2462
2463out_err2:
2464 kfree(clips);
2465out_err1:
2466 mutex_unlock(&dev->mode_config.mutex);
2467 return ret;
2468}
2469
2470
Dave Airlief453ba02008-11-07 14:05:41 -08002471/**
2472 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002473 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002474 *
Dave Airlief453ba02008-11-07 14:05:41 -08002475 * Destroy all the FBs associated with @filp.
2476 *
2477 * Called by the user via ioctl.
2478 *
2479 * RETURNS:
2480 * Zero on success, errno on failure.
2481 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002482void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002483{
Dave Airlief453ba02008-11-07 14:05:41 -08002484 struct drm_device *dev = priv->minor->dev;
2485 struct drm_framebuffer *fb, *tfb;
2486
2487 mutex_lock(&dev->mode_config.mutex);
2488 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00002489 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002490 }
2491 mutex_unlock(&dev->mode_config.mutex);
2492}
2493
2494/**
2495 * drm_mode_attachmode - add a mode to the user mode list
2496 * @dev: DRM device
2497 * @connector: connector to add the mode to
2498 * @mode: mode to add
2499 *
2500 * Add @mode to @connector's user mode list.
2501 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002502static void drm_mode_attachmode(struct drm_device *dev,
2503 struct drm_connector *connector,
2504 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002505{
Dave Airlief453ba02008-11-07 14:05:41 -08002506 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002507}
2508
2509int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002510 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002511{
2512 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002513 int ret = 0;
2514 struct drm_display_mode *dup_mode, *next;
2515 LIST_HEAD(list);
2516
Dave Airlief453ba02008-11-07 14:05:41 -08002517 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2518 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002519 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002520 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002521 dup_mode = drm_mode_duplicate(dev, mode);
2522 if (!dup_mode) {
2523 ret = -ENOMEM;
2524 goto out;
2525 }
2526 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002527 }
2528 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002529
2530 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2531 if (!connector->encoder)
2532 continue;
2533 if (connector->encoder->crtc == crtc)
2534 list_move_tail(list.next, &connector->user_modes);
2535 }
2536
2537 WARN_ON(!list_empty(&list));
2538
2539 out:
2540 list_for_each_entry_safe(dup_mode, next, &list, head)
2541 drm_mode_destroy(dev, dup_mode);
2542
2543 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002544}
2545EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2546
2547static int drm_mode_detachmode(struct drm_device *dev,
2548 struct drm_connector *connector,
2549 struct drm_display_mode *mode)
2550{
2551 int found = 0;
2552 int ret = 0;
2553 struct drm_display_mode *match_mode, *t;
2554
2555 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2556 if (drm_mode_equal(match_mode, mode)) {
2557 list_del(&match_mode->head);
2558 drm_mode_destroy(dev, match_mode);
2559 found = 1;
2560 break;
2561 }
2562 }
2563
2564 if (!found)
2565 ret = -EINVAL;
2566
2567 return ret;
2568}
2569
2570int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2571{
2572 struct drm_connector *connector;
2573
2574 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2575 drm_mode_detachmode(dev, connector, mode);
2576 }
2577 return 0;
2578}
2579EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2580
2581/**
2582 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002583 * @dev: drm device for the ioctl
2584 * @data: data pointer for the ioctl
2585 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002586 *
2587 * This attaches a user specified mode to an connector.
2588 * Called by the user via ioctl.
2589 *
2590 * RETURNS:
2591 * Zero on success, errno on failure.
2592 */
2593int drm_mode_attachmode_ioctl(struct drm_device *dev,
2594 void *data, struct drm_file *file_priv)
2595{
2596 struct drm_mode_mode_cmd *mode_cmd = data;
2597 struct drm_connector *connector;
2598 struct drm_display_mode *mode;
2599 struct drm_mode_object *obj;
2600 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002601 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002602
Dave Airliefb3b06c2011-02-08 13:55:21 +10002603 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2604 return -EINVAL;
2605
Dave Airlief453ba02008-11-07 14:05:41 -08002606 mutex_lock(&dev->mode_config.mutex);
2607
2608 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2609 if (!obj) {
2610 ret = -EINVAL;
2611 goto out;
2612 }
2613 connector = obj_to_connector(obj);
2614
2615 mode = drm_mode_create(dev);
2616 if (!mode) {
2617 ret = -ENOMEM;
2618 goto out;
2619 }
2620
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002621 ret = drm_crtc_convert_umode(mode, umode);
2622 if (ret) {
2623 DRM_DEBUG_KMS("Invalid mode\n");
2624 drm_mode_destroy(dev, mode);
2625 goto out;
2626 }
Dave Airlief453ba02008-11-07 14:05:41 -08002627
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002628 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002629out:
2630 mutex_unlock(&dev->mode_config.mutex);
2631 return ret;
2632}
2633
2634
2635/**
2636 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002637 * @dev: drm device for the ioctl
2638 * @data: data pointer for the ioctl
2639 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002640 *
2641 * Called by the user via ioctl.
2642 *
2643 * RETURNS:
2644 * Zero on success, errno on failure.
2645 */
2646int drm_mode_detachmode_ioctl(struct drm_device *dev,
2647 void *data, struct drm_file *file_priv)
2648{
2649 struct drm_mode_object *obj;
2650 struct drm_mode_mode_cmd *mode_cmd = data;
2651 struct drm_connector *connector;
2652 struct drm_display_mode mode;
2653 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002654 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002655
Dave Airliefb3b06c2011-02-08 13:55:21 +10002656 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2657 return -EINVAL;
2658
Dave Airlief453ba02008-11-07 14:05:41 -08002659 mutex_lock(&dev->mode_config.mutex);
2660
2661 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2662 if (!obj) {
2663 ret = -EINVAL;
2664 goto out;
2665 }
2666 connector = obj_to_connector(obj);
2667
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002668 ret = drm_crtc_convert_umode(&mode, umode);
2669 if (ret) {
2670 DRM_DEBUG_KMS("Invalid mode\n");
2671 goto out;
2672 }
2673
Dave Airlief453ba02008-11-07 14:05:41 -08002674 ret = drm_mode_detachmode(dev, connector, &mode);
2675out:
2676 mutex_unlock(&dev->mode_config.mutex);
2677 return ret;
2678}
2679
2680struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2681 const char *name, int num_values)
2682{
2683 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002684 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002685
2686 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2687 if (!property)
2688 return NULL;
2689
2690 if (num_values) {
2691 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2692 if (!property->values)
2693 goto fail;
2694 }
2695
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002696 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2697 if (ret)
2698 goto fail;
2699
Dave Airlief453ba02008-11-07 14:05:41 -08002700 property->flags = flags;
2701 property->num_values = num_values;
2702 INIT_LIST_HEAD(&property->enum_blob_list);
2703
Vinson Lee471dd2e2011-11-10 11:55:40 -08002704 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002705 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002706 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2707 }
Dave Airlief453ba02008-11-07 14:05:41 -08002708
2709 list_add_tail(&property->head, &dev->mode_config.property_list);
2710 return property;
2711fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002712 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002713 kfree(property);
2714 return NULL;
2715}
2716EXPORT_SYMBOL(drm_property_create);
2717
Sascha Hauer4a67d392012-02-06 10:58:17 +01002718struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2719 const char *name,
2720 const struct drm_prop_enum_list *props,
2721 int num_values)
2722{
2723 struct drm_property *property;
2724 int i, ret;
2725
2726 flags |= DRM_MODE_PROP_ENUM;
2727
2728 property = drm_property_create(dev, flags, name, num_values);
2729 if (!property)
2730 return NULL;
2731
2732 for (i = 0; i < num_values; i++) {
2733 ret = drm_property_add_enum(property, i,
2734 props[i].type,
2735 props[i].name);
2736 if (ret) {
2737 drm_property_destroy(dev, property);
2738 return NULL;
2739 }
2740 }
2741
2742 return property;
2743}
2744EXPORT_SYMBOL(drm_property_create_enum);
2745
Rob Clark49e27542012-05-17 02:23:26 -06002746struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2747 int flags, const char *name,
2748 const struct drm_prop_enum_list *props,
2749 int num_values)
2750{
2751 struct drm_property *property;
2752 int i, ret;
2753
2754 flags |= DRM_MODE_PROP_BITMASK;
2755
2756 property = drm_property_create(dev, flags, name, num_values);
2757 if (!property)
2758 return NULL;
2759
2760 for (i = 0; i < num_values; i++) {
2761 ret = drm_property_add_enum(property, i,
2762 props[i].type,
2763 props[i].name);
2764 if (ret) {
2765 drm_property_destroy(dev, property);
2766 return NULL;
2767 }
2768 }
2769
2770 return property;
2771}
2772EXPORT_SYMBOL(drm_property_create_bitmask);
2773
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002774struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2775 const char *name,
2776 uint64_t min, uint64_t max)
2777{
2778 struct drm_property *property;
2779
2780 flags |= DRM_MODE_PROP_RANGE;
2781
2782 property = drm_property_create(dev, flags, name, 2);
2783 if (!property)
2784 return NULL;
2785
2786 property->values[0] = min;
2787 property->values[1] = max;
2788
2789 return property;
2790}
2791EXPORT_SYMBOL(drm_property_create_range);
2792
Dave Airlief453ba02008-11-07 14:05:41 -08002793int drm_property_add_enum(struct drm_property *property, int index,
2794 uint64_t value, const char *name)
2795{
2796 struct drm_property_enum *prop_enum;
2797
Rob Clark49e27542012-05-17 02:23:26 -06002798 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2799 return -EINVAL;
2800
2801 /*
2802 * Bitmask enum properties have the additional constraint of values
2803 * from 0 to 63
2804 */
2805 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002806 return -EINVAL;
2807
2808 if (!list_empty(&property->enum_blob_list)) {
2809 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2810 if (prop_enum->value == value) {
2811 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2812 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2813 return 0;
2814 }
2815 }
2816 }
2817
2818 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2819 if (!prop_enum)
2820 return -ENOMEM;
2821
2822 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2823 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2824 prop_enum->value = value;
2825
2826 property->values[index] = value;
2827 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2828 return 0;
2829}
2830EXPORT_SYMBOL(drm_property_add_enum);
2831
2832void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2833{
2834 struct drm_property_enum *prop_enum, *pt;
2835
2836 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2837 list_del(&prop_enum->head);
2838 kfree(prop_enum);
2839 }
2840
2841 if (property->num_values)
2842 kfree(property->values);
2843 drm_mode_object_put(dev, &property->base);
2844 list_del(&property->head);
2845 kfree(property);
2846}
2847EXPORT_SYMBOL(drm_property_destroy);
2848
Paulo Zanonic5431882012-05-15 18:09:02 -03002849void drm_object_attach_property(struct drm_mode_object *obj,
2850 struct drm_property *property,
2851 uint64_t init_val)
2852{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002853 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002854
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002855 if (count == DRM_OBJECT_MAX_PROPERTY) {
2856 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2857 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2858 "you see this message on the same object type.\n",
2859 obj->type);
2860 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002861 }
2862
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002863 obj->properties->ids[count] = property->base.id;
2864 obj->properties->values[count] = init_val;
2865 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002866}
2867EXPORT_SYMBOL(drm_object_attach_property);
2868
2869int drm_object_property_set_value(struct drm_mode_object *obj,
2870 struct drm_property *property, uint64_t val)
2871{
2872 int i;
2873
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002874 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002875 if (obj->properties->ids[i] == property->base.id) {
2876 obj->properties->values[i] = val;
2877 return 0;
2878 }
2879 }
2880
2881 return -EINVAL;
2882}
2883EXPORT_SYMBOL(drm_object_property_set_value);
2884
2885int drm_object_property_get_value(struct drm_mode_object *obj,
2886 struct drm_property *property, uint64_t *val)
2887{
2888 int i;
2889
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002890 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002891 if (obj->properties->ids[i] == property->base.id) {
2892 *val = obj->properties->values[i];
2893 return 0;
2894 }
2895 }
2896
2897 return -EINVAL;
2898}
2899EXPORT_SYMBOL(drm_object_property_get_value);
2900
Dave Airlief453ba02008-11-07 14:05:41 -08002901int drm_mode_getproperty_ioctl(struct drm_device *dev,
2902 void *data, struct drm_file *file_priv)
2903{
2904 struct drm_mode_object *obj;
2905 struct drm_mode_get_property *out_resp = data;
2906 struct drm_property *property;
2907 int enum_count = 0;
2908 int blob_count = 0;
2909 int value_count = 0;
2910 int ret = 0, i;
2911 int copied;
2912 struct drm_property_enum *prop_enum;
2913 struct drm_mode_property_enum __user *enum_ptr;
2914 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002915 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002916 uint64_t __user *values_ptr;
2917 uint32_t __user *blob_length_ptr;
2918
Dave Airliefb3b06c2011-02-08 13:55:21 +10002919 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2920 return -EINVAL;
2921
Dave Airlief453ba02008-11-07 14:05:41 -08002922 mutex_lock(&dev->mode_config.mutex);
2923 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2924 if (!obj) {
2925 ret = -EINVAL;
2926 goto done;
2927 }
2928 property = obj_to_property(obj);
2929
Rob Clark49e27542012-05-17 02:23:26 -06002930 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002931 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2932 enum_count++;
2933 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2934 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2935 blob_count++;
2936 }
2937
2938 value_count = property->num_values;
2939
2940 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2941 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2942 out_resp->flags = property->flags;
2943
2944 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002945 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002946 for (i = 0; i < value_count; i++) {
2947 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2948 ret = -EFAULT;
2949 goto done;
2950 }
2951 }
2952 }
2953 out_resp->count_values = value_count;
2954
Rob Clark49e27542012-05-17 02:23:26 -06002955 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002956 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2957 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002958 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002959 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2960
2961 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2962 ret = -EFAULT;
2963 goto done;
2964 }
2965
2966 if (copy_to_user(&enum_ptr[copied].name,
2967 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2968 ret = -EFAULT;
2969 goto done;
2970 }
2971 copied++;
2972 }
2973 }
2974 out_resp->count_enum_blobs = enum_count;
2975 }
2976
2977 if (property->flags & DRM_MODE_PROP_BLOB) {
2978 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2979 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002980 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2981 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002982
2983 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2984 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2985 ret = -EFAULT;
2986 goto done;
2987 }
2988
2989 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2990 ret = -EFAULT;
2991 goto done;
2992 }
2993
2994 copied++;
2995 }
2996 }
2997 out_resp->count_enum_blobs = blob_count;
2998 }
2999done:
3000 mutex_unlock(&dev->mode_config.mutex);
3001 return ret;
3002}
3003
3004static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3005 void *data)
3006{
3007 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003008 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003009
3010 if (!length || !data)
3011 return NULL;
3012
3013 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3014 if (!blob)
3015 return NULL;
3016
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003017 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3018 if (ret) {
3019 kfree(blob);
3020 return NULL;
3021 }
3022
Dave Airlief453ba02008-11-07 14:05:41 -08003023 blob->length = length;
3024
3025 memcpy(blob->data, data, length);
3026
Dave Airlief453ba02008-11-07 14:05:41 -08003027 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3028 return blob;
3029}
3030
3031static void drm_property_destroy_blob(struct drm_device *dev,
3032 struct drm_property_blob *blob)
3033{
3034 drm_mode_object_put(dev, &blob->base);
3035 list_del(&blob->head);
3036 kfree(blob);
3037}
3038
3039int drm_mode_getblob_ioctl(struct drm_device *dev,
3040 void *data, struct drm_file *file_priv)
3041{
3042 struct drm_mode_object *obj;
3043 struct drm_mode_get_blob *out_resp = data;
3044 struct drm_property_blob *blob;
3045 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003046 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003047
Dave Airliefb3b06c2011-02-08 13:55:21 +10003048 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3049 return -EINVAL;
3050
Dave Airlief453ba02008-11-07 14:05:41 -08003051 mutex_lock(&dev->mode_config.mutex);
3052 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3053 if (!obj) {
3054 ret = -EINVAL;
3055 goto done;
3056 }
3057 blob = obj_to_blob(obj);
3058
3059 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003060 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003061 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3062 ret = -EFAULT;
3063 goto done;
3064 }
3065 }
3066 out_resp->length = blob->length;
3067
3068done:
3069 mutex_unlock(&dev->mode_config.mutex);
3070 return ret;
3071}
3072
3073int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3074 struct edid *edid)
3075{
3076 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003077 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003078
3079 if (connector->edid_blob_ptr)
3080 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3081
3082 /* Delete edid, when there is none. */
3083 if (!edid) {
3084 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003085 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003086 return ret;
3087 }
3088
Adam Jackson7466f4c2010-03-29 21:43:23 +00003089 size = EDID_LENGTH * (1 + edid->extensions);
3090 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3091 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003092 if (!connector->edid_blob_ptr)
3093 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003094
Rob Clark58495562012-10-11 20:50:56 -05003095 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003096 dev->mode_config.edid_property,
3097 connector->edid_blob_ptr->base.id);
3098
3099 return ret;
3100}
3101EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3102
Paulo Zanoni26a34812012-05-15 18:08:59 -03003103static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003104 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003105{
3106 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3107 return false;
3108 if (property->flags & DRM_MODE_PROP_RANGE) {
3109 if (value < property->values[0] || value > property->values[1])
3110 return false;
3111 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003112 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3113 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003114 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003115 for (i = 0; i < property->num_values; i++)
3116 valid_mask |= (1ULL << property->values[i]);
3117 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003118 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3119 /* Only the driver knows */
3120 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003121 } else {
3122 int i;
3123 for (i = 0; i < property->num_values; i++)
3124 if (property->values[i] == value)
3125 return true;
3126 return false;
3127 }
3128}
3129
Dave Airlief453ba02008-11-07 14:05:41 -08003130int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3131 void *data, struct drm_file *file_priv)
3132{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003133 struct drm_mode_connector_set_property *conn_set_prop = data;
3134 struct drm_mode_obj_set_property obj_set_prop = {
3135 .value = conn_set_prop->value,
3136 .prop_id = conn_set_prop->prop_id,
3137 .obj_id = conn_set_prop->connector_id,
3138 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3139 };
Dave Airlief453ba02008-11-07 14:05:41 -08003140
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003141 /* It does all the locking and checking we need */
3142 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003143}
3144
Paulo Zanonic5431882012-05-15 18:09:02 -03003145static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3146 struct drm_property *property,
3147 uint64_t value)
3148{
3149 int ret = -EINVAL;
3150 struct drm_connector *connector = obj_to_connector(obj);
3151
3152 /* Do DPMS ourselves */
3153 if (property == connector->dev->mode_config.dpms_property) {
3154 if (connector->funcs->dpms)
3155 (*connector->funcs->dpms)(connector, (int)value);
3156 ret = 0;
3157 } else if (connector->funcs->set_property)
3158 ret = connector->funcs->set_property(connector, property, value);
3159
3160 /* store the property value if successful */
3161 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003162 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003163 return ret;
3164}
3165
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003166static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3167 struct drm_property *property,
3168 uint64_t value)
3169{
3170 int ret = -EINVAL;
3171 struct drm_crtc *crtc = obj_to_crtc(obj);
3172
3173 if (crtc->funcs->set_property)
3174 ret = crtc->funcs->set_property(crtc, property, value);
3175 if (!ret)
3176 drm_object_property_set_value(obj, property, value);
3177
3178 return ret;
3179}
3180
Rob Clark4d939142012-05-17 02:23:27 -06003181static int drm_mode_plane_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_plane *plane = obj_to_plane(obj);
3187
3188 if (plane->funcs->set_property)
3189 ret = plane->funcs->set_property(plane, property, value);
3190 if (!ret)
3191 drm_object_property_set_value(obj, property, value);
3192
3193 return ret;
3194}
3195
Paulo Zanonic5431882012-05-15 18:09:02 -03003196int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3197 struct drm_file *file_priv)
3198{
3199 struct drm_mode_obj_get_properties *arg = data;
3200 struct drm_mode_object *obj;
3201 int ret = 0;
3202 int i;
3203 int copied = 0;
3204 int props_count = 0;
3205 uint32_t __user *props_ptr;
3206 uint64_t __user *prop_values_ptr;
3207
3208 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3209 return -EINVAL;
3210
3211 mutex_lock(&dev->mode_config.mutex);
3212
3213 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3214 if (!obj) {
3215 ret = -EINVAL;
3216 goto out;
3217 }
3218 if (!obj->properties) {
3219 ret = -EINVAL;
3220 goto out;
3221 }
3222
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003223 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003224
3225 /* This ioctl is called twice, once to determine how much space is
3226 * needed, and the 2nd time to fill it. */
3227 if ((arg->count_props >= props_count) && props_count) {
3228 copied = 0;
3229 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3230 prop_values_ptr = (uint64_t __user *)(unsigned long)
3231 (arg->prop_values_ptr);
3232 for (i = 0; i < props_count; i++) {
3233 if (put_user(obj->properties->ids[i],
3234 props_ptr + copied)) {
3235 ret = -EFAULT;
3236 goto out;
3237 }
3238 if (put_user(obj->properties->values[i],
3239 prop_values_ptr + copied)) {
3240 ret = -EFAULT;
3241 goto out;
3242 }
3243 copied++;
3244 }
3245 }
3246 arg->count_props = props_count;
3247out:
3248 mutex_unlock(&dev->mode_config.mutex);
3249 return ret;
3250}
3251
3252int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3253 struct drm_file *file_priv)
3254{
3255 struct drm_mode_obj_set_property *arg = data;
3256 struct drm_mode_object *arg_obj;
3257 struct drm_mode_object *prop_obj;
3258 struct drm_property *property;
3259 int ret = -EINVAL;
3260 int i;
3261
3262 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3263 return -EINVAL;
3264
3265 mutex_lock(&dev->mode_config.mutex);
3266
3267 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3268 if (!arg_obj)
3269 goto out;
3270 if (!arg_obj->properties)
3271 goto out;
3272
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003273 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003274 if (arg_obj->properties->ids[i] == arg->prop_id)
3275 break;
3276
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003277 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003278 goto out;
3279
3280 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3281 DRM_MODE_OBJECT_PROPERTY);
3282 if (!prop_obj)
3283 goto out;
3284 property = obj_to_property(prop_obj);
3285
3286 if (!drm_property_change_is_valid(property, arg->value))
3287 goto out;
3288
3289 switch (arg_obj->type) {
3290 case DRM_MODE_OBJECT_CONNECTOR:
3291 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3292 arg->value);
3293 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003294 case DRM_MODE_OBJECT_CRTC:
3295 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3296 break;
Rob Clark4d939142012-05-17 02:23:27 -06003297 case DRM_MODE_OBJECT_PLANE:
3298 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3299 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003300 }
3301
3302out:
3303 mutex_unlock(&dev->mode_config.mutex);
3304 return ret;
3305}
3306
Dave Airlief453ba02008-11-07 14:05:41 -08003307int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3308 struct drm_encoder *encoder)
3309{
3310 int i;
3311
3312 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3313 if (connector->encoder_ids[i] == 0) {
3314 connector->encoder_ids[i] = encoder->base.id;
3315 return 0;
3316 }
3317 }
3318 return -ENOMEM;
3319}
3320EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3321
3322void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3323 struct drm_encoder *encoder)
3324{
3325 int i;
3326 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3327 if (connector->encoder_ids[i] == encoder->base.id) {
3328 connector->encoder_ids[i] = 0;
3329 if (connector->encoder == encoder)
3330 connector->encoder = NULL;
3331 break;
3332 }
3333 }
3334}
3335EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3336
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003337int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003338 int gamma_size)
3339{
3340 crtc->gamma_size = gamma_size;
3341
3342 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3343 if (!crtc->gamma_store) {
3344 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003345 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003346 }
3347
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003348 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003349}
3350EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3351
3352int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3353 void *data, struct drm_file *file_priv)
3354{
3355 struct drm_mode_crtc_lut *crtc_lut = data;
3356 struct drm_mode_object *obj;
3357 struct drm_crtc *crtc;
3358 void *r_base, *g_base, *b_base;
3359 int size;
3360 int ret = 0;
3361
Dave Airliefb3b06c2011-02-08 13:55:21 +10003362 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3363 return -EINVAL;
3364
Dave Airlief453ba02008-11-07 14:05:41 -08003365 mutex_lock(&dev->mode_config.mutex);
3366 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3367 if (!obj) {
3368 ret = -EINVAL;
3369 goto out;
3370 }
3371 crtc = obj_to_crtc(obj);
3372
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003373 if (crtc->funcs->gamma_set == NULL) {
3374 ret = -ENOSYS;
3375 goto out;
3376 }
3377
Dave Airlief453ba02008-11-07 14:05:41 -08003378 /* memcpy into gamma store */
3379 if (crtc_lut->gamma_size != crtc->gamma_size) {
3380 ret = -EINVAL;
3381 goto out;
3382 }
3383
3384 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3385 r_base = crtc->gamma_store;
3386 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3387 ret = -EFAULT;
3388 goto out;
3389 }
3390
3391 g_base = r_base + size;
3392 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3393 ret = -EFAULT;
3394 goto out;
3395 }
3396
3397 b_base = g_base + size;
3398 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3399 ret = -EFAULT;
3400 goto out;
3401 }
3402
James Simmons72034252010-08-03 01:33:19 +01003403 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003404
3405out:
3406 mutex_unlock(&dev->mode_config.mutex);
3407 return ret;
3408
3409}
3410
3411int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3412 void *data, struct drm_file *file_priv)
3413{
3414 struct drm_mode_crtc_lut *crtc_lut = data;
3415 struct drm_mode_object *obj;
3416 struct drm_crtc *crtc;
3417 void *r_base, *g_base, *b_base;
3418 int size;
3419 int ret = 0;
3420
Dave Airliefb3b06c2011-02-08 13:55:21 +10003421 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3422 return -EINVAL;
3423
Dave Airlief453ba02008-11-07 14:05:41 -08003424 mutex_lock(&dev->mode_config.mutex);
3425 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3426 if (!obj) {
3427 ret = -EINVAL;
3428 goto out;
3429 }
3430 crtc = obj_to_crtc(obj);
3431
3432 /* memcpy into gamma store */
3433 if (crtc_lut->gamma_size != crtc->gamma_size) {
3434 ret = -EINVAL;
3435 goto out;
3436 }
3437
3438 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3439 r_base = crtc->gamma_store;
3440 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3441 ret = -EFAULT;
3442 goto out;
3443 }
3444
3445 g_base = r_base + size;
3446 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3447 ret = -EFAULT;
3448 goto out;
3449 }
3450
3451 b_base = g_base + size;
3452 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3453 ret = -EFAULT;
3454 goto out;
3455 }
3456out:
3457 mutex_unlock(&dev->mode_config.mutex);
3458 return ret;
3459}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003460
3461int drm_mode_page_flip_ioctl(struct drm_device *dev,
3462 void *data, struct drm_file *file_priv)
3463{
3464 struct drm_mode_crtc_page_flip *page_flip = data;
3465 struct drm_mode_object *obj;
3466 struct drm_crtc *crtc;
3467 struct drm_framebuffer *fb;
3468 struct drm_pending_vblank_event *e = NULL;
3469 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003470 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003471 int ret = -EINVAL;
3472
3473 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3474 page_flip->reserved != 0)
3475 return -EINVAL;
3476
3477 mutex_lock(&dev->mode_config.mutex);
3478 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3479 if (!obj)
3480 goto out;
3481 crtc = obj_to_crtc(obj);
3482
Chris Wilson90c1efd2010-07-17 20:23:26 +01003483 if (crtc->fb == NULL) {
3484 /* The framebuffer is currently unbound, presumably
3485 * due to a hotplug event, that userspace has not
3486 * yet discovered.
3487 */
3488 ret = -EBUSY;
3489 goto out;
3490 }
3491
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003492 if (crtc->funcs->page_flip == NULL)
3493 goto out;
3494
3495 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3496 if (!obj)
3497 goto out;
3498 fb = obj_to_fb(obj);
3499
Rob Clark7c80e122012-09-04 16:35:56 +00003500 hdisplay = crtc->mode.hdisplay;
3501 vdisplay = crtc->mode.vdisplay;
3502
3503 if (crtc->invert_dimensions)
3504 swap(hdisplay, vdisplay);
3505
3506 if (hdisplay > fb->width ||
3507 vdisplay > fb->height ||
3508 crtc->x > fb->width - hdisplay ||
3509 crtc->y > fb->height - vdisplay) {
3510 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3511 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3512 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003513 ret = -ENOSPC;
3514 goto out;
3515 }
3516
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003517 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3518 ret = -ENOMEM;
3519 spin_lock_irqsave(&dev->event_lock, flags);
3520 if (file_priv->event_space < sizeof e->event) {
3521 spin_unlock_irqrestore(&dev->event_lock, flags);
3522 goto out;
3523 }
3524 file_priv->event_space -= sizeof e->event;
3525 spin_unlock_irqrestore(&dev->event_lock, flags);
3526
3527 e = kzalloc(sizeof *e, GFP_KERNEL);
3528 if (e == NULL) {
3529 spin_lock_irqsave(&dev->event_lock, flags);
3530 file_priv->event_space += sizeof e->event;
3531 spin_unlock_irqrestore(&dev->event_lock, flags);
3532 goto out;
3533 }
3534
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003535 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003536 e->event.base.length = sizeof e->event;
3537 e->event.user_data = page_flip->user_data;
3538 e->base.event = &e->event.base;
3539 e->base.file_priv = file_priv;
3540 e->base.destroy =
3541 (void (*) (struct drm_pending_event *)) kfree;
3542 }
3543
3544 ret = crtc->funcs->page_flip(crtc, fb, e);
3545 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003546 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3547 spin_lock_irqsave(&dev->event_lock, flags);
3548 file_priv->event_space += sizeof e->event;
3549 spin_unlock_irqrestore(&dev->event_lock, flags);
3550 kfree(e);
3551 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003552 }
3553
3554out:
3555 mutex_unlock(&dev->mode_config.mutex);
3556 return ret;
3557}
Chris Wilsoneb0335562011-01-24 15:11:08 +00003558
3559void drm_mode_config_reset(struct drm_device *dev)
3560{
3561 struct drm_crtc *crtc;
3562 struct drm_encoder *encoder;
3563 struct drm_connector *connector;
3564
3565 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3566 if (crtc->funcs->reset)
3567 crtc->funcs->reset(crtc);
3568
3569 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3570 if (encoder->funcs->reset)
3571 encoder->funcs->reset(encoder);
3572
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003573 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3574 connector->status = connector_status_unknown;
3575
Chris Wilsoneb0335562011-01-24 15:11:08 +00003576 if (connector->funcs->reset)
3577 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003578 }
Chris Wilsoneb0335562011-01-24 15:11:08 +00003579}
3580EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003581
3582int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3583 void *data, struct drm_file *file_priv)
3584{
3585 struct drm_mode_create_dumb *args = data;
3586
3587 if (!dev->driver->dumb_create)
3588 return -ENOSYS;
3589 return dev->driver->dumb_create(file_priv, dev, args);
3590}
3591
3592int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3593 void *data, struct drm_file *file_priv)
3594{
3595 struct drm_mode_map_dumb *args = data;
3596
3597 /* call driver ioctl to get mmap offset */
3598 if (!dev->driver->dumb_map_offset)
3599 return -ENOSYS;
3600
3601 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3602}
3603
3604int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3605 void *data, struct drm_file *file_priv)
3606{
3607 struct drm_mode_destroy_dumb *args = data;
3608
3609 if (!dev->driver->dumb_destroy)
3610 return -ENOSYS;
3611
3612 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3613}
Dave Airlie248dbc22011-11-29 20:02:54 +00003614
3615/*
3616 * Just need to support RGB formats here for compat with code that doesn't
3617 * use pixel formats directly yet.
3618 */
3619void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3620 int *bpp)
3621{
3622 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003623 case DRM_FORMAT_RGB332:
3624 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003625 *depth = 8;
3626 *bpp = 8;
3627 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003628 case DRM_FORMAT_XRGB1555:
3629 case DRM_FORMAT_XBGR1555:
3630 case DRM_FORMAT_RGBX5551:
3631 case DRM_FORMAT_BGRX5551:
3632 case DRM_FORMAT_ARGB1555:
3633 case DRM_FORMAT_ABGR1555:
3634 case DRM_FORMAT_RGBA5551:
3635 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003636 *depth = 15;
3637 *bpp = 16;
3638 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003639 case DRM_FORMAT_RGB565:
3640 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003641 *depth = 16;
3642 *bpp = 16;
3643 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003644 case DRM_FORMAT_RGB888:
3645 case DRM_FORMAT_BGR888:
3646 *depth = 24;
3647 *bpp = 24;
3648 break;
3649 case DRM_FORMAT_XRGB8888:
3650 case DRM_FORMAT_XBGR8888:
3651 case DRM_FORMAT_RGBX8888:
3652 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003653 *depth = 24;
3654 *bpp = 32;
3655 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003656 case DRM_FORMAT_XRGB2101010:
3657 case DRM_FORMAT_XBGR2101010:
3658 case DRM_FORMAT_RGBX1010102:
3659 case DRM_FORMAT_BGRX1010102:
3660 case DRM_FORMAT_ARGB2101010:
3661 case DRM_FORMAT_ABGR2101010:
3662 case DRM_FORMAT_RGBA1010102:
3663 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003664 *depth = 30;
3665 *bpp = 32;
3666 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003667 case DRM_FORMAT_ARGB8888:
3668 case DRM_FORMAT_ABGR8888:
3669 case DRM_FORMAT_RGBA8888:
3670 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003671 *depth = 32;
3672 *bpp = 32;
3673 break;
3674 default:
3675 DRM_DEBUG_KMS("unsupported pixel format\n");
3676 *depth = 0;
3677 *bpp = 0;
3678 break;
3679 }
3680}
3681EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003682
3683/**
3684 * drm_format_num_planes - get the number of planes for format
3685 * @format: pixel format (DRM_FORMAT_*)
3686 *
3687 * RETURNS:
3688 * The number of planes used by the specified pixel format.
3689 */
3690int drm_format_num_planes(uint32_t format)
3691{
3692 switch (format) {
3693 case DRM_FORMAT_YUV410:
3694 case DRM_FORMAT_YVU410:
3695 case DRM_FORMAT_YUV411:
3696 case DRM_FORMAT_YVU411:
3697 case DRM_FORMAT_YUV420:
3698 case DRM_FORMAT_YVU420:
3699 case DRM_FORMAT_YUV422:
3700 case DRM_FORMAT_YVU422:
3701 case DRM_FORMAT_YUV444:
3702 case DRM_FORMAT_YVU444:
3703 return 3;
3704 case DRM_FORMAT_NV12:
3705 case DRM_FORMAT_NV21:
3706 case DRM_FORMAT_NV16:
3707 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003708 case DRM_FORMAT_NV24:
3709 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003710 return 2;
3711 default:
3712 return 1;
3713 }
3714}
3715EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003716
3717/**
3718 * drm_format_plane_cpp - determine the bytes per pixel value
3719 * @format: pixel format (DRM_FORMAT_*)
3720 * @plane: plane index
3721 *
3722 * RETURNS:
3723 * The bytes per pixel value for the specified plane.
3724 */
3725int drm_format_plane_cpp(uint32_t format, int plane)
3726{
3727 unsigned int depth;
3728 int bpp;
3729
3730 if (plane >= drm_format_num_planes(format))
3731 return 0;
3732
3733 switch (format) {
3734 case DRM_FORMAT_YUYV:
3735 case DRM_FORMAT_YVYU:
3736 case DRM_FORMAT_UYVY:
3737 case DRM_FORMAT_VYUY:
3738 return 2;
3739 case DRM_FORMAT_NV12:
3740 case DRM_FORMAT_NV21:
3741 case DRM_FORMAT_NV16:
3742 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003743 case DRM_FORMAT_NV24:
3744 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003745 return plane ? 2 : 1;
3746 case DRM_FORMAT_YUV410:
3747 case DRM_FORMAT_YVU410:
3748 case DRM_FORMAT_YUV411:
3749 case DRM_FORMAT_YVU411:
3750 case DRM_FORMAT_YUV420:
3751 case DRM_FORMAT_YVU420:
3752 case DRM_FORMAT_YUV422:
3753 case DRM_FORMAT_YVU422:
3754 case DRM_FORMAT_YUV444:
3755 case DRM_FORMAT_YVU444:
3756 return 1;
3757 default:
3758 drm_fb_get_bpp_depth(format, &depth, &bpp);
3759 return bpp >> 3;
3760 }
3761}
3762EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003763
3764/**
3765 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3766 * @format: pixel format (DRM_FORMAT_*)
3767 *
3768 * RETURNS:
3769 * The horizontal chroma subsampling factor for the
3770 * specified pixel format.
3771 */
3772int drm_format_horz_chroma_subsampling(uint32_t format)
3773{
3774 switch (format) {
3775 case DRM_FORMAT_YUV411:
3776 case DRM_FORMAT_YVU411:
3777 case DRM_FORMAT_YUV410:
3778 case DRM_FORMAT_YVU410:
3779 return 4;
3780 case DRM_FORMAT_YUYV:
3781 case DRM_FORMAT_YVYU:
3782 case DRM_FORMAT_UYVY:
3783 case DRM_FORMAT_VYUY:
3784 case DRM_FORMAT_NV12:
3785 case DRM_FORMAT_NV21:
3786 case DRM_FORMAT_NV16:
3787 case DRM_FORMAT_NV61:
3788 case DRM_FORMAT_YUV422:
3789 case DRM_FORMAT_YVU422:
3790 case DRM_FORMAT_YUV420:
3791 case DRM_FORMAT_YVU420:
3792 return 2;
3793 default:
3794 return 1;
3795 }
3796}
3797EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3798
3799/**
3800 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3801 * @format: pixel format (DRM_FORMAT_*)
3802 *
3803 * RETURNS:
3804 * The vertical chroma subsampling factor for the
3805 * specified pixel format.
3806 */
3807int drm_format_vert_chroma_subsampling(uint32_t format)
3808{
3809 switch (format) {
3810 case DRM_FORMAT_YUV410:
3811 case DRM_FORMAT_YVU410:
3812 return 4;
3813 case DRM_FORMAT_YUV420:
3814 case DRM_FORMAT_YVU420:
3815 case DRM_FORMAT_NV12:
3816 case DRM_FORMAT_NV21:
3817 return 2;
3818 default:
3819 return 1;
3820 }
3821}
3822EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);