blob: 81545540b2dff6a96e57817b97c39bcea409269e [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{
320 struct drm_device *dev = fb->dev;
321 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100322 WARN_ON(!drm_modeset_is_locked(dev));
Rob Clarkf7eff602012-09-05 21:48:38 +0000323 kref_put(&fb->refcount, drm_framebuffer_free);
324}
325EXPORT_SYMBOL(drm_framebuffer_unreference);
326
327/**
328 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100329 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000330 */
331void drm_framebuffer_reference(struct drm_framebuffer *fb)
332{
333 DRM_DEBUG("FB ID: %d\n", fb->base.id);
334 kref_get(&fb->refcount);
335}
336EXPORT_SYMBOL(drm_framebuffer_reference);
337
Dave Airlief453ba02008-11-07 14:05:41 -0800338/**
339 * drm_framebuffer_cleanup - remove a framebuffer object
340 * @fb: framebuffer to remove
341 *
Dave Airlief453ba02008-11-07 14:05:41 -0800342 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
343 * it, setting it to NULL.
344 */
345void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
346{
347 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100348
Rob Clarkf7eff602012-09-05 21:48:38 +0000349 /*
350 * This could be moved to drm_framebuffer_remove(), but for
351 * debugging is nice to keep around the list of fb's that are
352 * no longer associated w/ a drm_file but are not unreferenced
353 * yet. (i915 and omapdrm have debugfs files which will show
354 * this.)
355 */
356 drm_mode_object_put(dev, &fb->base);
357 list_del(&fb->head);
358 dev->mode_config.num_fb--;
359}
360EXPORT_SYMBOL(drm_framebuffer_cleanup);
361
362/**
363 * drm_framebuffer_remove - remove and unreference a framebuffer object
364 * @fb: framebuffer to remove
365 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000366 * Scans all the CRTCs and planes in @dev's mode_config. If they're
367 * using @fb, removes it, setting it to NULL.
368 */
369void drm_framebuffer_remove(struct drm_framebuffer *fb)
370{
371 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800372 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800373 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000374 struct drm_mode_set set;
375 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800376
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100377 WARN_ON(!drm_modeset_is_locked(dev));
378
Dave Airlief453ba02008-11-07 14:05:41 -0800379 /* remove from any CRTC */
380 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000381 if (crtc->fb == fb) {
382 /* should turn off the crtc */
383 memset(&set, 0, sizeof(struct drm_mode_set));
384 set.crtc = crtc;
385 set.fb = NULL;
386 ret = crtc->funcs->set_config(&set);
387 if (ret)
388 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
389 }
Dave Airlief453ba02008-11-07 14:05:41 -0800390 }
391
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800392 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
393 if (plane->fb == fb) {
394 /* should turn off the crtc */
395 ret = plane->funcs->disable_plane(plane);
396 if (ret)
397 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600398 /* disconnect the plane from the fb and crtc: */
399 plane->fb = NULL;
400 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800401 }
402 }
403
Rob Clarkf7eff602012-09-05 21:48:38 +0000404 list_del(&fb->filp_head);
405
406 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800407}
Rob Clarkf7eff602012-09-05 21:48:38 +0000408EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800409
410/**
411 * drm_crtc_init - Initialise a new CRTC object
412 * @dev: DRM device
413 * @crtc: CRTC object to init
414 * @funcs: callbacks for the new CRTC
415 *
Dave Airlief453ba02008-11-07 14:05:41 -0800416 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200417 *
418 * RETURNS:
419 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800420 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200421int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800422 const struct drm_crtc_funcs *funcs)
423{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200424 int ret;
425
Dave Airlief453ba02008-11-07 14:05:41 -0800426 crtc->dev = dev;
427 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000428 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800429
430 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200431
432 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
433 if (ret)
434 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800435
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300436 crtc->base.properties = &crtc->properties;
437
Dave Airlief453ba02008-11-07 14:05:41 -0800438 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
439 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200440
441 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800442 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200443
444 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800445}
446EXPORT_SYMBOL(drm_crtc_init);
447
448/**
449 * drm_crtc_cleanup - Cleans up the core crtc usage.
450 * @crtc: CRTC to cleanup
451 *
Dave Airlief453ba02008-11-07 14:05:41 -0800452 * Cleanup @crtc. Removes from drm modesetting space
453 * does NOT free object, caller does that.
454 */
455void drm_crtc_cleanup(struct drm_crtc *crtc)
456{
457 struct drm_device *dev = crtc->dev;
458
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000459 kfree(crtc->gamma_store);
460 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800461
462 drm_mode_object_put(dev, &crtc->base);
463 list_del(&crtc->head);
464 dev->mode_config.num_crtc--;
465}
466EXPORT_SYMBOL(drm_crtc_cleanup);
467
468/**
469 * drm_mode_probed_add - add a mode to a connector's probed mode list
470 * @connector: connector the new mode
471 * @mode: mode data
472 *
Dave Airlief453ba02008-11-07 14:05:41 -0800473 * Add @mode to @connector's mode list for later use.
474 */
475void drm_mode_probed_add(struct drm_connector *connector,
476 struct drm_display_mode *mode)
477{
478 list_add(&mode->head, &connector->probed_modes);
479}
480EXPORT_SYMBOL(drm_mode_probed_add);
481
482/**
483 * drm_mode_remove - remove and free a mode
484 * @connector: connector list to modify
485 * @mode: mode to remove
486 *
Dave Airlief453ba02008-11-07 14:05:41 -0800487 * Remove @mode from @connector's mode list, then free it.
488 */
489void drm_mode_remove(struct drm_connector *connector,
490 struct drm_display_mode *mode)
491{
492 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100493 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800494}
495EXPORT_SYMBOL(drm_mode_remove);
496
497/**
498 * drm_connector_init - Init a preallocated connector
499 * @dev: DRM device
500 * @connector: the connector to init
501 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100502 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800503 *
Dave Airlief453ba02008-11-07 14:05:41 -0800504 * Initialises a preallocated connector. Connectors should be
505 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200506 *
507 * RETURNS:
508 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800509 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200510int drm_connector_init(struct drm_device *dev,
511 struct drm_connector *connector,
512 const struct drm_connector_funcs *funcs,
513 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800514{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200515 int ret;
516
Dave Airlief453ba02008-11-07 14:05:41 -0800517 mutex_lock(&dev->mode_config.mutex);
518
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200519 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
520 if (ret)
521 goto out;
522
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300523 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800524 connector->dev = dev;
525 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800526 connector->connector_type = connector_type;
527 connector->connector_type_id =
528 ++drm_connector_enum_list[connector_type].count; /* TODO */
529 INIT_LIST_HEAD(&connector->user_modes);
530 INIT_LIST_HEAD(&connector->probed_modes);
531 INIT_LIST_HEAD(&connector->modes);
532 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000533 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800534
535 list_add_tail(&connector->head, &dev->mode_config.connector_list);
536 dev->mode_config.num_connector++;
537
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200538 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500539 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200540 dev->mode_config.edid_property,
541 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800542
Rob Clark58495562012-10-11 20:50:56 -0500543 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800544 dev->mode_config.dpms_property, 0);
545
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200546 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800547 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200548
549 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800550}
551EXPORT_SYMBOL(drm_connector_init);
552
553/**
554 * drm_connector_cleanup - cleans up an initialised connector
555 * @connector: connector to cleanup
556 *
Dave Airlief453ba02008-11-07 14:05:41 -0800557 * Cleans up the connector but doesn't free the object.
558 */
559void drm_connector_cleanup(struct drm_connector *connector)
560{
561 struct drm_device *dev = connector->dev;
562 struct drm_display_mode *mode, *t;
563
564 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
565 drm_mode_remove(connector, mode);
566
567 list_for_each_entry_safe(mode, t, &connector->modes, head)
568 drm_mode_remove(connector, mode);
569
570 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
571 drm_mode_remove(connector, mode);
572
Dave Airlief453ba02008-11-07 14:05:41 -0800573 drm_mode_object_put(dev, &connector->base);
574 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000575 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800576}
577EXPORT_SYMBOL(drm_connector_cleanup);
578
Dave Airliecbc7e222012-02-20 14:16:40 +0000579void drm_connector_unplug_all(struct drm_device *dev)
580{
581 struct drm_connector *connector;
582
583 /* taking the mode config mutex ends up in a clash with sysfs */
584 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
585 drm_sysfs_connector_remove(connector);
586
587}
588EXPORT_SYMBOL(drm_connector_unplug_all);
589
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200590int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000591 struct drm_encoder *encoder,
592 const struct drm_encoder_funcs *funcs,
593 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800594{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200595 int ret;
596
Dave Airlief453ba02008-11-07 14:05:41 -0800597 mutex_lock(&dev->mode_config.mutex);
598
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200599 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
600 if (ret)
601 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800602
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200603 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800604 encoder->encoder_type = encoder_type;
605 encoder->funcs = funcs;
606
607 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
608 dev->mode_config.num_encoder++;
609
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200610 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800611 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200612
613 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800614}
615EXPORT_SYMBOL(drm_encoder_init);
616
617void drm_encoder_cleanup(struct drm_encoder *encoder)
618{
619 struct drm_device *dev = encoder->dev;
620 mutex_lock(&dev->mode_config.mutex);
621 drm_mode_object_put(dev, &encoder->base);
622 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000623 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800624 mutex_unlock(&dev->mode_config.mutex);
625}
626EXPORT_SYMBOL(drm_encoder_cleanup);
627
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800628int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
629 unsigned long possible_crtcs,
630 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600631 const uint32_t *formats, uint32_t format_count,
632 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800633{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200634 int ret;
635
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800636 mutex_lock(&dev->mode_config.mutex);
637
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200638 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
639 if (ret)
640 goto out;
641
Rob Clark4d939142012-05-17 02:23:27 -0600642 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800643 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800644 plane->funcs = funcs;
645 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
646 GFP_KERNEL);
647 if (!plane->format_types) {
648 DRM_DEBUG_KMS("out of memory when allocating plane\n");
649 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200650 ret = -ENOMEM;
651 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800652 }
653
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800654 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800655 plane->format_count = format_count;
656 plane->possible_crtcs = possible_crtcs;
657
Rob Clark0a7eb242011-12-13 20:19:36 -0600658 /* private planes are not exposed to userspace, but depending on
659 * display hardware, might be convenient to allow sharing programming
660 * for the scanout engine with the crtc implementation.
661 */
662 if (!priv) {
663 list_add_tail(&plane->head, &dev->mode_config.plane_list);
664 dev->mode_config.num_plane++;
665 } else {
666 INIT_LIST_HEAD(&plane->head);
667 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800668
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200669 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800670 mutex_unlock(&dev->mode_config.mutex);
671
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200672 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800673}
674EXPORT_SYMBOL(drm_plane_init);
675
676void drm_plane_cleanup(struct drm_plane *plane)
677{
678 struct drm_device *dev = plane->dev;
679
680 mutex_lock(&dev->mode_config.mutex);
681 kfree(plane->format_types);
682 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600683 /* if not added to a list, it must be a private plane */
684 if (!list_empty(&plane->head)) {
685 list_del(&plane->head);
686 dev->mode_config.num_plane--;
687 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800688 mutex_unlock(&dev->mode_config.mutex);
689}
690EXPORT_SYMBOL(drm_plane_cleanup);
691
Dave Airlief453ba02008-11-07 14:05:41 -0800692/**
693 * drm_mode_create - create a new display mode
694 * @dev: DRM device
695 *
Dave Airlief453ba02008-11-07 14:05:41 -0800696 * Create a new drm_display_mode, give it an ID, and return it.
697 *
698 * RETURNS:
699 * Pointer to new mode on success, NULL on error.
700 */
701struct drm_display_mode *drm_mode_create(struct drm_device *dev)
702{
703 struct drm_display_mode *nmode;
704
705 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
706 if (!nmode)
707 return NULL;
708
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200709 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
710 kfree(nmode);
711 return NULL;
712 }
713
Dave Airlief453ba02008-11-07 14:05:41 -0800714 return nmode;
715}
716EXPORT_SYMBOL(drm_mode_create);
717
718/**
719 * drm_mode_destroy - remove a mode
720 * @dev: DRM device
721 * @mode: mode to remove
722 *
Dave Airlief453ba02008-11-07 14:05:41 -0800723 * Free @mode's unique identifier, then free it.
724 */
725void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
726{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200727 if (!mode)
728 return;
729
Dave Airlief453ba02008-11-07 14:05:41 -0800730 drm_mode_object_put(dev, &mode->base);
731
732 kfree(mode);
733}
734EXPORT_SYMBOL(drm_mode_destroy);
735
736static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
737{
738 struct drm_property *edid;
739 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800740
741 /*
742 * Standard properties (apply to all connectors)
743 */
744 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
745 DRM_MODE_PROP_IMMUTABLE,
746 "EDID", 0);
747 dev->mode_config.edid_property = edid;
748
Sascha Hauer4a67d392012-02-06 10:58:17 +0100749 dpms = drm_property_create_enum(dev, 0,
750 "DPMS", drm_dpms_enum_list,
751 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800752 dev->mode_config.dpms_property = dpms;
753
754 return 0;
755}
756
757/**
758 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
759 * @dev: DRM device
760 *
761 * Called by a driver the first time a DVI-I connector is made.
762 */
763int drm_mode_create_dvi_i_properties(struct drm_device *dev)
764{
765 struct drm_property *dvi_i_selector;
766 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800767
768 if (dev->mode_config.dvi_i_select_subconnector_property)
769 return 0;
770
771 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100772 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800773 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100774 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800775 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800776 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
777
Sascha Hauer4a67d392012-02-06 10:58:17 +0100778 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800779 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100780 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800781 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800782 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
783
784 return 0;
785}
786EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
787
788/**
789 * drm_create_tv_properties - create TV specific connector properties
790 * @dev: DRM device
791 * @num_modes: number of different TV formats (modes) supported
792 * @modes: array of pointers to strings containing name of each format
793 *
794 * Called by a driver's TV initialization routine, this function creates
795 * the TV specific connector properties for a given device. Caller is
796 * responsible for allocating a list of format names and passing them to
797 * this routine.
798 */
799int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
800 char *modes[])
801{
802 struct drm_property *tv_selector;
803 struct drm_property *tv_subconnector;
804 int i;
805
806 if (dev->mode_config.tv_select_subconnector_property)
807 return 0;
808
809 /*
810 * Basic connector properties
811 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100812 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800813 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100814 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800815 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800816 dev->mode_config.tv_select_subconnector_property = tv_selector;
817
818 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100819 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
820 "subconnector",
821 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800822 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800823 dev->mode_config.tv_subconnector_property = tv_subconnector;
824
825 /*
826 * Other, TV specific properties: margins & TV modes.
827 */
828 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100829 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800830
831 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100832 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800833
834 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100835 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800836
837 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100838 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800839
840 dev->mode_config.tv_mode_property =
841 drm_property_create(dev, DRM_MODE_PROP_ENUM,
842 "mode", num_modes);
843 for (i = 0; i < num_modes; i++)
844 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
845 i, modes[i]);
846
Francisco Jerezb6b79022009-08-02 04:19:20 +0200847 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100848 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200849
850 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100851 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200852
853 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100854 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200855
Francisco Jereza75f0232009-08-12 02:30:10 +0200856 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100857 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200858
859 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100860 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200861
862 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100863 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200864
Dave Airlief453ba02008-11-07 14:05:41 -0800865 return 0;
866}
867EXPORT_SYMBOL(drm_mode_create_tv_properties);
868
869/**
870 * drm_mode_create_scaling_mode_property - create scaling mode property
871 * @dev: DRM device
872 *
873 * Called by a driver the first time it's needed, must be attached to desired
874 * connectors.
875 */
876int drm_mode_create_scaling_mode_property(struct drm_device *dev)
877{
878 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800879
880 if (dev->mode_config.scaling_mode_property)
881 return 0;
882
883 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100884 drm_property_create_enum(dev, 0, "scaling mode",
885 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800886 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800887
888 dev->mode_config.scaling_mode_property = scaling_mode;
889
890 return 0;
891}
892EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
893
894/**
895 * drm_mode_create_dithering_property - create dithering property
896 * @dev: DRM device
897 *
898 * Called by a driver the first time it's needed, must be attached to desired
899 * connectors.
900 */
901int drm_mode_create_dithering_property(struct drm_device *dev)
902{
903 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800904
905 if (dev->mode_config.dithering_mode_property)
906 return 0;
907
908 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100909 drm_property_create_enum(dev, 0, "dithering",
910 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800911 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800912 dev->mode_config.dithering_mode_property = dithering_mode;
913
914 return 0;
915}
916EXPORT_SYMBOL(drm_mode_create_dithering_property);
917
918/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000919 * drm_mode_create_dirty_property - create dirty property
920 * @dev: DRM device
921 *
922 * Called by a driver the first time it's needed, must be attached to desired
923 * connectors.
924 */
925int drm_mode_create_dirty_info_property(struct drm_device *dev)
926{
927 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000928
929 if (dev->mode_config.dirty_info_property)
930 return 0;
931
932 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100933 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000934 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100935 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000936 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000937 dev->mode_config.dirty_info_property = dirty_info;
938
939 return 0;
940}
941EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
942
943/**
Dave Airlief453ba02008-11-07 14:05:41 -0800944 * drm_mode_config_init - initialize DRM mode_configuration structure
945 * @dev: DRM device
946 *
Dave Airlief453ba02008-11-07 14:05:41 -0800947 * Initialize @dev's mode_config structure, used for tracking the graphics
948 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100949 *
950 * Since this initializes the modeset locks, no locking is possible. Which is no
951 * problem, since this should happen single threaded at init time. It is the
952 * driver's problem to ensure this guarantee.
953 *
Dave Airlief453ba02008-11-07 14:05:41 -0800954 */
955void drm_mode_config_init(struct drm_device *dev)
956{
957 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000958 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800959 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800960 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
961 INIT_LIST_HEAD(&dev->mode_config.connector_list);
962 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
963 INIT_LIST_HEAD(&dev->mode_config.property_list);
964 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800965 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800966 idr_init(&dev->mode_config.crtc_idr);
967
968 mutex_lock(&dev->mode_config.mutex);
969 drm_mode_create_standard_connector_properties(dev);
970 mutex_unlock(&dev->mode_config.mutex);
971
972 /* Just to be sure */
973 dev->mode_config.num_fb = 0;
974 dev->mode_config.num_connector = 0;
975 dev->mode_config.num_crtc = 0;
976 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800977}
978EXPORT_SYMBOL(drm_mode_config_init);
979
980int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
981{
982 uint32_t total_objects = 0;
983
984 total_objects += dev->mode_config.num_crtc;
985 total_objects += dev->mode_config.num_connector;
986 total_objects += dev->mode_config.num_encoder;
987
Dave Airlief453ba02008-11-07 14:05:41 -0800988 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
989 if (!group->id_list)
990 return -ENOMEM;
991
992 group->num_crtcs = 0;
993 group->num_connectors = 0;
994 group->num_encoders = 0;
995 return 0;
996}
997
998int drm_mode_group_init_legacy_group(struct drm_device *dev,
999 struct drm_mode_group *group)
1000{
1001 struct drm_crtc *crtc;
1002 struct drm_encoder *encoder;
1003 struct drm_connector *connector;
1004 int ret;
1005
1006 if ((ret = drm_mode_group_init(dev, group)))
1007 return ret;
1008
1009 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1010 group->id_list[group->num_crtcs++] = crtc->base.id;
1011
1012 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1013 group->id_list[group->num_crtcs + group->num_encoders++] =
1014 encoder->base.id;
1015
1016 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1017 group->id_list[group->num_crtcs + group->num_encoders +
1018 group->num_connectors++] = connector->base.id;
1019
1020 return 0;
1021}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001022EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001023
1024/**
1025 * drm_mode_config_cleanup - free up DRM mode_config info
1026 * @dev: DRM device
1027 *
Dave Airlief453ba02008-11-07 14:05:41 -08001028 * Free up all the connectors and CRTCs associated with this DRM device, then
1029 * free up the framebuffers and associated buffer objects.
1030 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001031 * Note that since this /should/ happen single-threaded at driver/device
1032 * teardown time, no locking is required. It's the driver's job to ensure that
1033 * this guarantee actually holds true.
1034 *
Dave Airlief453ba02008-11-07 14:05:41 -08001035 * FIXME: cleanup any dangling user buffer objects too
1036 */
1037void drm_mode_config_cleanup(struct drm_device *dev)
1038{
1039 struct drm_connector *connector, *ot;
1040 struct drm_crtc *crtc, *ct;
1041 struct drm_encoder *encoder, *enct;
1042 struct drm_framebuffer *fb, *fbt;
1043 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001044 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001045
1046 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1047 head) {
1048 encoder->funcs->destroy(encoder);
1049 }
1050
1051 list_for_each_entry_safe(connector, ot,
1052 &dev->mode_config.connector_list, head) {
1053 connector->funcs->destroy(connector);
1054 }
1055
1056 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1057 head) {
1058 drm_property_destroy(dev, property);
1059 }
1060
1061 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001062 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001063 }
1064
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001065 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1066 head) {
1067 plane->funcs->destroy(plane);
1068 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001069
Chris Wilson31840092012-09-17 09:38:03 +00001070 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1071 crtc->funcs->destroy(crtc);
1072 }
1073
Sascha Hauer59ce0622012-02-01 11:38:20 +01001074 idr_remove_all(&dev->mode_config.crtc_idr);
1075 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001076}
1077EXPORT_SYMBOL(drm_mode_config_cleanup);
1078
Dave Airlief453ba02008-11-07 14:05:41 -08001079/**
1080 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1081 * @out: drm_mode_modeinfo struct to return to the user
1082 * @in: drm_display_mode to use
1083 *
Dave Airlief453ba02008-11-07 14:05:41 -08001084 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1085 * the user.
1086 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001087static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1088 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001089{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001090 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1091 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1092 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1093 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1094 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1095 "timing values too large for mode info\n");
1096
Dave Airlief453ba02008-11-07 14:05:41 -08001097 out->clock = in->clock;
1098 out->hdisplay = in->hdisplay;
1099 out->hsync_start = in->hsync_start;
1100 out->hsync_end = in->hsync_end;
1101 out->htotal = in->htotal;
1102 out->hskew = in->hskew;
1103 out->vdisplay = in->vdisplay;
1104 out->vsync_start = in->vsync_start;
1105 out->vsync_end = in->vsync_end;
1106 out->vtotal = in->vtotal;
1107 out->vscan = in->vscan;
1108 out->vrefresh = in->vrefresh;
1109 out->flags = in->flags;
1110 out->type = in->type;
1111 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1112 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1113}
1114
1115/**
1116 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1117 * @out: drm_display_mode to return to the user
1118 * @in: drm_mode_modeinfo to use
1119 *
Dave Airlief453ba02008-11-07 14:05:41 -08001120 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1121 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001122 *
1123 * RETURNS:
1124 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001125 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001126static int drm_crtc_convert_umode(struct drm_display_mode *out,
1127 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001128{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001129 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1130 return -ERANGE;
1131
Dave Airlief453ba02008-11-07 14:05:41 -08001132 out->clock = in->clock;
1133 out->hdisplay = in->hdisplay;
1134 out->hsync_start = in->hsync_start;
1135 out->hsync_end = in->hsync_end;
1136 out->htotal = in->htotal;
1137 out->hskew = in->hskew;
1138 out->vdisplay = in->vdisplay;
1139 out->vsync_start = in->vsync_start;
1140 out->vsync_end = in->vsync_end;
1141 out->vtotal = in->vtotal;
1142 out->vscan = in->vscan;
1143 out->vrefresh = in->vrefresh;
1144 out->flags = in->flags;
1145 out->type = in->type;
1146 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1147 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001148
1149 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001150}
1151
1152/**
1153 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001154 * @dev: drm device for the ioctl
1155 * @data: data pointer for the ioctl
1156 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001157 *
Dave Airlief453ba02008-11-07 14:05:41 -08001158 * Construct a set of configuration description structures and return
1159 * them to the user, including CRTC, connector and framebuffer configuration.
1160 *
1161 * Called by the user via ioctl.
1162 *
1163 * RETURNS:
1164 * Zero on success, errno on failure.
1165 */
1166int drm_mode_getresources(struct drm_device *dev, void *data,
1167 struct drm_file *file_priv)
1168{
1169 struct drm_mode_card_res *card_res = data;
1170 struct list_head *lh;
1171 struct drm_framebuffer *fb;
1172 struct drm_connector *connector;
1173 struct drm_crtc *crtc;
1174 struct drm_encoder *encoder;
1175 int ret = 0;
1176 int connector_count = 0;
1177 int crtc_count = 0;
1178 int fb_count = 0;
1179 int encoder_count = 0;
1180 int copied = 0, i;
1181 uint32_t __user *fb_id;
1182 uint32_t __user *crtc_id;
1183 uint32_t __user *connector_id;
1184 uint32_t __user *encoder_id;
1185 struct drm_mode_group *mode_group;
1186
Dave Airliefb3b06c2011-02-08 13:55:21 +10001187 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1188 return -EINVAL;
1189
Dave Airlief453ba02008-11-07 14:05:41 -08001190 mutex_lock(&dev->mode_config.mutex);
1191
1192 /*
1193 * For the non-control nodes we need to limit the list of resources
1194 * by IDs in the group list for this node
1195 */
1196 list_for_each(lh, &file_priv->fbs)
1197 fb_count++;
1198
1199 mode_group = &file_priv->master->minor->mode_group;
1200 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1201
1202 list_for_each(lh, &dev->mode_config.crtc_list)
1203 crtc_count++;
1204
1205 list_for_each(lh, &dev->mode_config.connector_list)
1206 connector_count++;
1207
1208 list_for_each(lh, &dev->mode_config.encoder_list)
1209 encoder_count++;
1210 } else {
1211
1212 crtc_count = mode_group->num_crtcs;
1213 connector_count = mode_group->num_connectors;
1214 encoder_count = mode_group->num_encoders;
1215 }
1216
1217 card_res->max_height = dev->mode_config.max_height;
1218 card_res->min_height = dev->mode_config.min_height;
1219 card_res->max_width = dev->mode_config.max_width;
1220 card_res->min_width = dev->mode_config.min_width;
1221
1222 /* handle this in 4 parts */
1223 /* FBs */
1224 if (card_res->count_fbs >= fb_count) {
1225 copied = 0;
1226 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001227 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001228 if (put_user(fb->base.id, fb_id + copied)) {
1229 ret = -EFAULT;
1230 goto out;
1231 }
1232 copied++;
1233 }
1234 }
1235 card_res->count_fbs = fb_count;
1236
1237 /* CRTCs */
1238 if (card_res->count_crtcs >= crtc_count) {
1239 copied = 0;
1240 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1241 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1242 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1243 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001244 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001245 if (put_user(crtc->base.id, crtc_id + copied)) {
1246 ret = -EFAULT;
1247 goto out;
1248 }
1249 copied++;
1250 }
1251 } else {
1252 for (i = 0; i < mode_group->num_crtcs; i++) {
1253 if (put_user(mode_group->id_list[i],
1254 crtc_id + copied)) {
1255 ret = -EFAULT;
1256 goto out;
1257 }
1258 copied++;
1259 }
1260 }
1261 }
1262 card_res->count_crtcs = crtc_count;
1263
1264 /* Encoders */
1265 if (card_res->count_encoders >= encoder_count) {
1266 copied = 0;
1267 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1268 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1269 list_for_each_entry(encoder,
1270 &dev->mode_config.encoder_list,
1271 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001272 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1273 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001274 if (put_user(encoder->base.id, encoder_id +
1275 copied)) {
1276 ret = -EFAULT;
1277 goto out;
1278 }
1279 copied++;
1280 }
1281 } else {
1282 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1283 if (put_user(mode_group->id_list[i],
1284 encoder_id + copied)) {
1285 ret = -EFAULT;
1286 goto out;
1287 }
1288 copied++;
1289 }
1290
1291 }
1292 }
1293 card_res->count_encoders = encoder_count;
1294
1295 /* Connectors */
1296 if (card_res->count_connectors >= connector_count) {
1297 copied = 0;
1298 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1299 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1300 list_for_each_entry(connector,
1301 &dev->mode_config.connector_list,
1302 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001303 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1304 connector->base.id,
1305 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001306 if (put_user(connector->base.id,
1307 connector_id + copied)) {
1308 ret = -EFAULT;
1309 goto out;
1310 }
1311 copied++;
1312 }
1313 } else {
1314 int start = mode_group->num_crtcs +
1315 mode_group->num_encoders;
1316 for (i = start; i < start + mode_group->num_connectors; i++) {
1317 if (put_user(mode_group->id_list[i],
1318 connector_id + copied)) {
1319 ret = -EFAULT;
1320 goto out;
1321 }
1322 copied++;
1323 }
1324 }
1325 }
1326 card_res->count_connectors = connector_count;
1327
Jerome Glisse94401062010-07-15 15:43:25 -04001328 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001329 card_res->count_connectors, card_res->count_encoders);
1330
1331out:
1332 mutex_unlock(&dev->mode_config.mutex);
1333 return ret;
1334}
1335
1336/**
1337 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001338 * @dev: drm device for the ioctl
1339 * @data: data pointer for the ioctl
1340 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001341 *
Dave Airlief453ba02008-11-07 14:05:41 -08001342 * Construct a CRTC configuration structure to return to the user.
1343 *
1344 * Called by the user via ioctl.
1345 *
1346 * RETURNS:
1347 * Zero on success, errno on failure.
1348 */
1349int drm_mode_getcrtc(struct drm_device *dev,
1350 void *data, struct drm_file *file_priv)
1351{
1352 struct drm_mode_crtc *crtc_resp = data;
1353 struct drm_crtc *crtc;
1354 struct drm_mode_object *obj;
1355 int ret = 0;
1356
Dave Airliefb3b06c2011-02-08 13:55:21 +10001357 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1358 return -EINVAL;
1359
Dave Airlief453ba02008-11-07 14:05:41 -08001360 mutex_lock(&dev->mode_config.mutex);
1361
1362 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1363 DRM_MODE_OBJECT_CRTC);
1364 if (!obj) {
1365 ret = -EINVAL;
1366 goto out;
1367 }
1368 crtc = obj_to_crtc(obj);
1369
1370 crtc_resp->x = crtc->x;
1371 crtc_resp->y = crtc->y;
1372 crtc_resp->gamma_size = crtc->gamma_size;
1373 if (crtc->fb)
1374 crtc_resp->fb_id = crtc->fb->base.id;
1375 else
1376 crtc_resp->fb_id = 0;
1377
1378 if (crtc->enabled) {
1379
1380 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1381 crtc_resp->mode_valid = 1;
1382
1383 } else {
1384 crtc_resp->mode_valid = 0;
1385 }
1386
1387out:
1388 mutex_unlock(&dev->mode_config.mutex);
1389 return ret;
1390}
1391
1392/**
1393 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001394 * @dev: drm device for the ioctl
1395 * @data: data pointer for the ioctl
1396 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001397 *
Dave Airlief453ba02008-11-07 14:05:41 -08001398 * Construct a connector configuration structure to return to the user.
1399 *
1400 * Called by the user via ioctl.
1401 *
1402 * RETURNS:
1403 * Zero on success, errno on failure.
1404 */
1405int drm_mode_getconnector(struct drm_device *dev, void *data,
1406 struct drm_file *file_priv)
1407{
1408 struct drm_mode_get_connector *out_resp = data;
1409 struct drm_mode_object *obj;
1410 struct drm_connector *connector;
1411 struct drm_display_mode *mode;
1412 int mode_count = 0;
1413 int props_count = 0;
1414 int encoders_count = 0;
1415 int ret = 0;
1416 int copied = 0;
1417 int i;
1418 struct drm_mode_modeinfo u_mode;
1419 struct drm_mode_modeinfo __user *mode_ptr;
1420 uint32_t __user *prop_ptr;
1421 uint64_t __user *prop_values;
1422 uint32_t __user *encoder_ptr;
1423
Dave Airliefb3b06c2011-02-08 13:55:21 +10001424 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1425 return -EINVAL;
1426
Dave Airlief453ba02008-11-07 14:05:41 -08001427 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1428
Jerome Glisse94401062010-07-15 15:43:25 -04001429 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001430
1431 mutex_lock(&dev->mode_config.mutex);
1432
1433 obj = drm_mode_object_find(dev, out_resp->connector_id,
1434 DRM_MODE_OBJECT_CONNECTOR);
1435 if (!obj) {
1436 ret = -EINVAL;
1437 goto out;
1438 }
1439 connector = obj_to_connector(obj);
1440
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001441 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001442
1443 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1444 if (connector->encoder_ids[i] != 0) {
1445 encoders_count++;
1446 }
1447 }
1448
1449 if (out_resp->count_modes == 0) {
1450 connector->funcs->fill_modes(connector,
1451 dev->mode_config.max_width,
1452 dev->mode_config.max_height);
1453 }
1454
1455 /* delayed so we get modes regardless of pre-fill_modes state */
1456 list_for_each_entry(mode, &connector->modes, head)
1457 mode_count++;
1458
1459 out_resp->connector_id = connector->base.id;
1460 out_resp->connector_type = connector->connector_type;
1461 out_resp->connector_type_id = connector->connector_type_id;
1462 out_resp->mm_width = connector->display_info.width_mm;
1463 out_resp->mm_height = connector->display_info.height_mm;
1464 out_resp->subpixel = connector->display_info.subpixel_order;
1465 out_resp->connection = connector->status;
1466 if (connector->encoder)
1467 out_resp->encoder_id = connector->encoder->base.id;
1468 else
1469 out_resp->encoder_id = 0;
1470
1471 /*
1472 * This ioctl is called twice, once to determine how much space is
1473 * needed, and the 2nd time to fill it.
1474 */
1475 if ((out_resp->count_modes >= mode_count) && mode_count) {
1476 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001477 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001478 list_for_each_entry(mode, &connector->modes, head) {
1479 drm_crtc_convert_to_umode(&u_mode, mode);
1480 if (copy_to_user(mode_ptr + copied,
1481 &u_mode, sizeof(u_mode))) {
1482 ret = -EFAULT;
1483 goto out;
1484 }
1485 copied++;
1486 }
1487 }
1488 out_resp->count_modes = mode_count;
1489
1490 if ((out_resp->count_props >= props_count) && props_count) {
1491 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001492 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1493 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001494 for (i = 0; i < connector->properties.count; i++) {
1495 if (put_user(connector->properties.ids[i],
1496 prop_ptr + copied)) {
1497 ret = -EFAULT;
1498 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001499 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001500
1501 if (put_user(connector->properties.values[i],
1502 prop_values + copied)) {
1503 ret = -EFAULT;
1504 goto out;
1505 }
1506 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001507 }
1508 }
1509 out_resp->count_props = props_count;
1510
1511 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1512 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001513 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001514 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1515 if (connector->encoder_ids[i] != 0) {
1516 if (put_user(connector->encoder_ids[i],
1517 encoder_ptr + copied)) {
1518 ret = -EFAULT;
1519 goto out;
1520 }
1521 copied++;
1522 }
1523 }
1524 }
1525 out_resp->count_encoders = encoders_count;
1526
1527out:
1528 mutex_unlock(&dev->mode_config.mutex);
1529 return ret;
1530}
1531
1532int drm_mode_getencoder(struct drm_device *dev, void *data,
1533 struct drm_file *file_priv)
1534{
1535 struct drm_mode_get_encoder *enc_resp = data;
1536 struct drm_mode_object *obj;
1537 struct drm_encoder *encoder;
1538 int ret = 0;
1539
Dave Airliefb3b06c2011-02-08 13:55:21 +10001540 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1541 return -EINVAL;
1542
Dave Airlief453ba02008-11-07 14:05:41 -08001543 mutex_lock(&dev->mode_config.mutex);
1544 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1545 DRM_MODE_OBJECT_ENCODER);
1546 if (!obj) {
1547 ret = -EINVAL;
1548 goto out;
1549 }
1550 encoder = obj_to_encoder(obj);
1551
1552 if (encoder->crtc)
1553 enc_resp->crtc_id = encoder->crtc->base.id;
1554 else
1555 enc_resp->crtc_id = 0;
1556 enc_resp->encoder_type = encoder->encoder_type;
1557 enc_resp->encoder_id = encoder->base.id;
1558 enc_resp->possible_crtcs = encoder->possible_crtcs;
1559 enc_resp->possible_clones = encoder->possible_clones;
1560
1561out:
1562 mutex_unlock(&dev->mode_config.mutex);
1563 return ret;
1564}
1565
1566/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001567 * drm_mode_getplane_res - get plane info
1568 * @dev: DRM device
1569 * @data: ioctl data
1570 * @file_priv: DRM file info
1571 *
1572 * Return an plane count and set of IDs.
1573 */
1574int drm_mode_getplane_res(struct drm_device *dev, void *data,
1575 struct drm_file *file_priv)
1576{
1577 struct drm_mode_get_plane_res *plane_resp = data;
1578 struct drm_mode_config *config;
1579 struct drm_plane *plane;
1580 uint32_t __user *plane_ptr;
1581 int copied = 0, ret = 0;
1582
1583 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1584 return -EINVAL;
1585
1586 mutex_lock(&dev->mode_config.mutex);
1587 config = &dev->mode_config;
1588
1589 /*
1590 * This ioctl is called twice, once to determine how much space is
1591 * needed, and the 2nd time to fill it.
1592 */
1593 if (config->num_plane &&
1594 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001595 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001596
1597 list_for_each_entry(plane, &config->plane_list, head) {
1598 if (put_user(plane->base.id, plane_ptr + copied)) {
1599 ret = -EFAULT;
1600 goto out;
1601 }
1602 copied++;
1603 }
1604 }
1605 plane_resp->count_planes = config->num_plane;
1606
1607out:
1608 mutex_unlock(&dev->mode_config.mutex);
1609 return ret;
1610}
1611
1612/**
1613 * drm_mode_getplane - get plane info
1614 * @dev: DRM device
1615 * @data: ioctl data
1616 * @file_priv: DRM file info
1617 *
1618 * Return plane info, including formats supported, gamma size, any
1619 * current fb, etc.
1620 */
1621int drm_mode_getplane(struct drm_device *dev, void *data,
1622 struct drm_file *file_priv)
1623{
1624 struct drm_mode_get_plane *plane_resp = data;
1625 struct drm_mode_object *obj;
1626 struct drm_plane *plane;
1627 uint32_t __user *format_ptr;
1628 int ret = 0;
1629
1630 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1631 return -EINVAL;
1632
1633 mutex_lock(&dev->mode_config.mutex);
1634 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1635 DRM_MODE_OBJECT_PLANE);
1636 if (!obj) {
1637 ret = -ENOENT;
1638 goto out;
1639 }
1640 plane = obj_to_plane(obj);
1641
1642 if (plane->crtc)
1643 plane_resp->crtc_id = plane->crtc->base.id;
1644 else
1645 plane_resp->crtc_id = 0;
1646
1647 if (plane->fb)
1648 plane_resp->fb_id = plane->fb->base.id;
1649 else
1650 plane_resp->fb_id = 0;
1651
1652 plane_resp->plane_id = plane->base.id;
1653 plane_resp->possible_crtcs = plane->possible_crtcs;
1654 plane_resp->gamma_size = plane->gamma_size;
1655
1656 /*
1657 * This ioctl is called twice, once to determine how much space is
1658 * needed, and the 2nd time to fill it.
1659 */
1660 if (plane->format_count &&
1661 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001662 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001663 if (copy_to_user(format_ptr,
1664 plane->format_types,
1665 sizeof(uint32_t) * plane->format_count)) {
1666 ret = -EFAULT;
1667 goto out;
1668 }
1669 }
1670 plane_resp->count_format_types = plane->format_count;
1671
1672out:
1673 mutex_unlock(&dev->mode_config.mutex);
1674 return ret;
1675}
1676
1677/**
1678 * drm_mode_setplane - set up or tear down an plane
1679 * @dev: DRM device
1680 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001681 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001682 *
1683 * Set plane info, including placement, fb, scaling, and other factors.
1684 * Or pass a NULL fb to disable.
1685 */
1686int drm_mode_setplane(struct drm_device *dev, void *data,
1687 struct drm_file *file_priv)
1688{
1689 struct drm_mode_set_plane *plane_req = data;
1690 struct drm_mode_object *obj;
1691 struct drm_plane *plane;
1692 struct drm_crtc *crtc;
1693 struct drm_framebuffer *fb;
1694 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001695 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001696 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001697
1698 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1699 return -EINVAL;
1700
1701 mutex_lock(&dev->mode_config.mutex);
1702
1703 /*
1704 * First, find the plane, crtc, and fb objects. If not available,
1705 * we don't bother to call the driver.
1706 */
1707 obj = drm_mode_object_find(dev, plane_req->plane_id,
1708 DRM_MODE_OBJECT_PLANE);
1709 if (!obj) {
1710 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1711 plane_req->plane_id);
1712 ret = -ENOENT;
1713 goto out;
1714 }
1715 plane = obj_to_plane(obj);
1716
1717 /* No fb means shut it down */
1718 if (!plane_req->fb_id) {
1719 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001720 plane->crtc = NULL;
1721 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001722 goto out;
1723 }
1724
1725 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1726 DRM_MODE_OBJECT_CRTC);
1727 if (!obj) {
1728 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1729 plane_req->crtc_id);
1730 ret = -ENOENT;
1731 goto out;
1732 }
1733 crtc = obj_to_crtc(obj);
1734
1735 obj = drm_mode_object_find(dev, plane_req->fb_id,
1736 DRM_MODE_OBJECT_FB);
1737 if (!obj) {
1738 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1739 plane_req->fb_id);
1740 ret = -ENOENT;
1741 goto out;
1742 }
1743 fb = obj_to_fb(obj);
1744
Ville Syrjälä62443be2011-12-20 00:06:47 +02001745 /* Check whether this plane supports the fb pixel format. */
1746 for (i = 0; i < plane->format_count; i++)
1747 if (fb->pixel_format == plane->format_types[i])
1748 break;
1749 if (i == plane->format_count) {
1750 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1751 ret = -EINVAL;
1752 goto out;
1753 }
1754
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001755 fb_width = fb->width << 16;
1756 fb_height = fb->height << 16;
1757
1758 /* Make sure source coordinates are inside the fb. */
1759 if (plane_req->src_w > fb_width ||
1760 plane_req->src_x > fb_width - plane_req->src_w ||
1761 plane_req->src_h > fb_height ||
1762 plane_req->src_y > fb_height - plane_req->src_h) {
1763 DRM_DEBUG_KMS("Invalid source coordinates "
1764 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1765 plane_req->src_w >> 16,
1766 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1767 plane_req->src_h >> 16,
1768 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1769 plane_req->src_x >> 16,
1770 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1771 plane_req->src_y >> 16,
1772 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1773 ret = -ENOSPC;
1774 goto out;
1775 }
1776
Ville Syrjälä687a0402011-12-20 00:06:45 +02001777 /* Give drivers some help against integer overflows */
1778 if (plane_req->crtc_w > INT_MAX ||
1779 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1780 plane_req->crtc_h > INT_MAX ||
1781 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1782 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1783 plane_req->crtc_w, plane_req->crtc_h,
1784 plane_req->crtc_x, plane_req->crtc_y);
1785 ret = -ERANGE;
1786 goto out;
1787 }
1788
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001789 ret = plane->funcs->update_plane(plane, crtc, fb,
1790 plane_req->crtc_x, plane_req->crtc_y,
1791 plane_req->crtc_w, plane_req->crtc_h,
1792 plane_req->src_x, plane_req->src_y,
1793 plane_req->src_w, plane_req->src_h);
1794 if (!ret) {
1795 plane->crtc = crtc;
1796 plane->fb = fb;
1797 }
1798
1799out:
1800 mutex_unlock(&dev->mode_config.mutex);
1801
1802 return ret;
1803}
1804
1805/**
Dave Airlief453ba02008-11-07 14:05:41 -08001806 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001807 * @dev: drm device for the ioctl
1808 * @data: data pointer for the ioctl
1809 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001810 *
Dave Airlief453ba02008-11-07 14:05:41 -08001811 * Build a new CRTC configuration based on user request.
1812 *
1813 * Called by the user via ioctl.
1814 *
1815 * RETURNS:
1816 * Zero on success, errno on failure.
1817 */
1818int drm_mode_setcrtc(struct drm_device *dev, void *data,
1819 struct drm_file *file_priv)
1820{
1821 struct drm_mode_config *config = &dev->mode_config;
1822 struct drm_mode_crtc *crtc_req = data;
1823 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001824 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001825 struct drm_connector **connector_set = NULL, *connector;
1826 struct drm_framebuffer *fb = NULL;
1827 struct drm_display_mode *mode = NULL;
1828 struct drm_mode_set set;
1829 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001830 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001831 int i;
1832
Dave Airliefb3b06c2011-02-08 13:55:21 +10001833 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1834 return -EINVAL;
1835
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001836 /* For some reason crtc x/y offsets are signed internally. */
1837 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1838 return -ERANGE;
1839
Dave Airlief453ba02008-11-07 14:05:41 -08001840 mutex_lock(&dev->mode_config.mutex);
1841 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1842 DRM_MODE_OBJECT_CRTC);
1843 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001844 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001845 ret = -EINVAL;
1846 goto out;
1847 }
1848 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001849 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001850
1851 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001852 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001853 /* If we have a mode we need a framebuffer. */
1854 /* If we pass -1, set the mode with the currently bound fb */
1855 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001856 if (!crtc->fb) {
1857 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1858 ret = -EINVAL;
1859 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001860 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001861 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001862 } else {
1863 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1864 DRM_MODE_OBJECT_FB);
1865 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001866 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1867 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001868 ret = -EINVAL;
1869 goto out;
1870 }
1871 fb = obj_to_fb(obj);
1872 }
1873
1874 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001875 if (!mode) {
1876 ret = -ENOMEM;
1877 goto out;
1878 }
1879
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001880 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1881 if (ret) {
1882 DRM_DEBUG_KMS("Invalid mode\n");
1883 goto out;
1884 }
1885
Dave Airlief453ba02008-11-07 14:05:41 -08001886 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001887
Rob Clark7c80e122012-09-04 16:35:56 +00001888 hdisplay = mode->hdisplay;
1889 vdisplay = mode->vdisplay;
1890
1891 if (crtc->invert_dimensions)
1892 swap(hdisplay, vdisplay);
1893
1894 if (hdisplay > fb->width ||
1895 vdisplay > fb->height ||
1896 crtc_req->x > fb->width - hdisplay ||
1897 crtc_req->y > fb->height - vdisplay) {
1898 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1899 fb->width, fb->height,
1900 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1901 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001902 ret = -ENOSPC;
1903 goto out;
1904 }
Dave Airlief453ba02008-11-07 14:05:41 -08001905 }
1906
1907 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001908 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001909 ret = -EINVAL;
1910 goto out;
1911 }
1912
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001913 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001914 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001915 crtc_req->count_connectors);
1916 ret = -EINVAL;
1917 goto out;
1918 }
1919
1920 if (crtc_req->count_connectors > 0) {
1921 u32 out_id;
1922
1923 /* Avoid unbounded kernel memory allocation */
1924 if (crtc_req->count_connectors > config->num_connector) {
1925 ret = -EINVAL;
1926 goto out;
1927 }
1928
1929 connector_set = kmalloc(crtc_req->count_connectors *
1930 sizeof(struct drm_connector *),
1931 GFP_KERNEL);
1932 if (!connector_set) {
1933 ret = -ENOMEM;
1934 goto out;
1935 }
1936
1937 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001938 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001939 if (get_user(out_id, &set_connectors_ptr[i])) {
1940 ret = -EFAULT;
1941 goto out;
1942 }
1943
1944 obj = drm_mode_object_find(dev, out_id,
1945 DRM_MODE_OBJECT_CONNECTOR);
1946 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001947 DRM_DEBUG_KMS("Connector id %d unknown\n",
1948 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001949 ret = -EINVAL;
1950 goto out;
1951 }
1952 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001953 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1954 connector->base.id,
1955 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001956
1957 connector_set[i] = connector;
1958 }
1959 }
1960
1961 set.crtc = crtc;
1962 set.x = crtc_req->x;
1963 set.y = crtc_req->y;
1964 set.mode = mode;
1965 set.connectors = connector_set;
1966 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001967 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001968 ret = crtc->funcs->set_config(&set);
1969
1970out:
1971 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001972 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001973 mutex_unlock(&dev->mode_config.mutex);
1974 return ret;
1975}
1976
1977int drm_mode_cursor_ioctl(struct drm_device *dev,
1978 void *data, struct drm_file *file_priv)
1979{
1980 struct drm_mode_cursor *req = data;
1981 struct drm_mode_object *obj;
1982 struct drm_crtc *crtc;
1983 int ret = 0;
1984
Dave Airliefb3b06c2011-02-08 13:55:21 +10001985 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1986 return -EINVAL;
1987
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00001988 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08001989 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001990
1991 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001992 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001993 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001994 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001995 ret = -EINVAL;
1996 goto out;
1997 }
1998 crtc = obj_to_crtc(obj);
1999
2000 if (req->flags & DRM_MODE_CURSOR_BO) {
2001 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002002 ret = -ENXIO;
2003 goto out;
2004 }
2005 /* Turns off the cursor if handle is 0 */
2006 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2007 req->width, req->height);
2008 }
2009
2010 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2011 if (crtc->funcs->cursor_move) {
2012 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2013 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002014 ret = -EFAULT;
2015 goto out;
2016 }
2017 }
2018out:
2019 mutex_unlock(&dev->mode_config.mutex);
2020 return ret;
2021}
2022
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002023/* Original addfb only supported RGB formats, so figure out which one */
2024uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2025{
2026 uint32_t fmt;
2027
2028 switch (bpp) {
2029 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002030 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002031 break;
2032 case 16:
2033 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002034 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002035 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002036 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002037 break;
2038 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002039 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002040 break;
2041 case 32:
2042 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002045 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002046 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002047 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002048 break;
2049 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002050 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2051 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002052 break;
2053 }
2054
2055 return fmt;
2056}
2057EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2058
Dave Airlief453ba02008-11-07 14:05:41 -08002059/**
2060 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002061 * @dev: drm device for the ioctl
2062 * @data: data pointer for the ioctl
2063 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002064 *
Dave Airlief453ba02008-11-07 14:05:41 -08002065 * Add a new FB to the specified CRTC, given a user request.
2066 *
2067 * Called by the user via ioctl.
2068 *
2069 * RETURNS:
2070 * Zero on success, errno on failure.
2071 */
2072int drm_mode_addfb(struct drm_device *dev,
2073 void *data, struct drm_file *file_priv)
2074{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002075 struct drm_mode_fb_cmd *or = data;
2076 struct drm_mode_fb_cmd2 r = {};
2077 struct drm_mode_config *config = &dev->mode_config;
2078 struct drm_framebuffer *fb;
2079 int ret = 0;
2080
2081 /* Use new struct with format internally */
2082 r.fb_id = or->fb_id;
2083 r.width = or->width;
2084 r.height = or->height;
2085 r.pitches[0] = or->pitch;
2086 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2087 r.handles[0] = or->handle;
2088
2089 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2090 return -EINVAL;
2091
Jesse Barnesacb4b992011-11-07 12:03:23 -08002092 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002093 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002094
2095 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002096 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002097
2098 mutex_lock(&dev->mode_config.mutex);
2099
2100 /* TODO check buffer is sufficiently large */
2101 /* TODO setup destructor callback */
2102
2103 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2104 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002105 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002106 ret = PTR_ERR(fb);
2107 goto out;
2108 }
2109
2110 or->fb_id = fb->base.id;
2111 list_add(&fb->filp_head, &file_priv->fbs);
2112 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2113
2114out:
2115 mutex_unlock(&dev->mode_config.mutex);
2116 return ret;
2117}
2118
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002119static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002120{
2121 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2122
2123 switch (format) {
2124 case DRM_FORMAT_C8:
2125 case DRM_FORMAT_RGB332:
2126 case DRM_FORMAT_BGR233:
2127 case DRM_FORMAT_XRGB4444:
2128 case DRM_FORMAT_XBGR4444:
2129 case DRM_FORMAT_RGBX4444:
2130 case DRM_FORMAT_BGRX4444:
2131 case DRM_FORMAT_ARGB4444:
2132 case DRM_FORMAT_ABGR4444:
2133 case DRM_FORMAT_RGBA4444:
2134 case DRM_FORMAT_BGRA4444:
2135 case DRM_FORMAT_XRGB1555:
2136 case DRM_FORMAT_XBGR1555:
2137 case DRM_FORMAT_RGBX5551:
2138 case DRM_FORMAT_BGRX5551:
2139 case DRM_FORMAT_ARGB1555:
2140 case DRM_FORMAT_ABGR1555:
2141 case DRM_FORMAT_RGBA5551:
2142 case DRM_FORMAT_BGRA5551:
2143 case DRM_FORMAT_RGB565:
2144 case DRM_FORMAT_BGR565:
2145 case DRM_FORMAT_RGB888:
2146 case DRM_FORMAT_BGR888:
2147 case DRM_FORMAT_XRGB8888:
2148 case DRM_FORMAT_XBGR8888:
2149 case DRM_FORMAT_RGBX8888:
2150 case DRM_FORMAT_BGRX8888:
2151 case DRM_FORMAT_ARGB8888:
2152 case DRM_FORMAT_ABGR8888:
2153 case DRM_FORMAT_RGBA8888:
2154 case DRM_FORMAT_BGRA8888:
2155 case DRM_FORMAT_XRGB2101010:
2156 case DRM_FORMAT_XBGR2101010:
2157 case DRM_FORMAT_RGBX1010102:
2158 case DRM_FORMAT_BGRX1010102:
2159 case DRM_FORMAT_ARGB2101010:
2160 case DRM_FORMAT_ABGR2101010:
2161 case DRM_FORMAT_RGBA1010102:
2162 case DRM_FORMAT_BGRA1010102:
2163 case DRM_FORMAT_YUYV:
2164 case DRM_FORMAT_YVYU:
2165 case DRM_FORMAT_UYVY:
2166 case DRM_FORMAT_VYUY:
2167 case DRM_FORMAT_AYUV:
2168 case DRM_FORMAT_NV12:
2169 case DRM_FORMAT_NV21:
2170 case DRM_FORMAT_NV16:
2171 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002172 case DRM_FORMAT_NV24:
2173 case DRM_FORMAT_NV42:
Ville Syrjälä935b59772011-12-20 00:06:48 +02002174 case DRM_FORMAT_YUV410:
2175 case DRM_FORMAT_YVU410:
2176 case DRM_FORMAT_YUV411:
2177 case DRM_FORMAT_YVU411:
2178 case DRM_FORMAT_YUV420:
2179 case DRM_FORMAT_YVU420:
2180 case DRM_FORMAT_YUV422:
2181 case DRM_FORMAT_YVU422:
2182 case DRM_FORMAT_YUV444:
2183 case DRM_FORMAT_YVU444:
2184 return 0;
2185 default:
2186 return -EINVAL;
2187 }
2188}
2189
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002190static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002191{
2192 int ret, hsub, vsub, num_planes, i;
2193
2194 ret = format_check(r);
2195 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002196 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002197 return ret;
2198 }
2199
2200 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2201 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2202 num_planes = drm_format_num_planes(r->pixel_format);
2203
2204 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002205 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002206 return -EINVAL;
2207 }
2208
2209 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002210 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002211 return -EINVAL;
2212 }
2213
2214 for (i = 0; i < num_planes; i++) {
2215 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002216 unsigned int height = r->height / (i != 0 ? vsub : 1);
2217 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002218
2219 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002220 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002221 return -EINVAL;
2222 }
2223
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002224 if ((uint64_t) width * cpp > UINT_MAX)
2225 return -ERANGE;
2226
2227 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2228 return -ERANGE;
2229
2230 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002231 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002232 return -EINVAL;
2233 }
2234 }
2235
2236 return 0;
2237}
2238
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002239/**
2240 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002241 * @dev: drm device for the ioctl
2242 * @data: data pointer for the ioctl
2243 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002244 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002245 * Add a new FB to the specified CRTC, given a user request with format.
2246 *
2247 * Called by the user via ioctl.
2248 *
2249 * RETURNS:
2250 * Zero on success, errno on failure.
2251 */
2252int drm_mode_addfb2(struct drm_device *dev,
2253 void *data, struct drm_file *file_priv)
2254{
2255 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002256 struct drm_mode_config *config = &dev->mode_config;
2257 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002258 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002259
Dave Airliefb3b06c2011-02-08 13:55:21 +10002260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2261 return -EINVAL;
2262
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002263 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2264 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2265 return -EINVAL;
2266 }
2267
Dave Airlief453ba02008-11-07 14:05:41 -08002268 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002269 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002270 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002271 return -EINVAL;
2272 }
2273 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002274 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002275 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002276 return -EINVAL;
2277 }
2278
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002279 ret = framebuffer_check(r);
2280 if (ret)
Ville Syrjälä935b59772011-12-20 00:06:48 +02002281 return ret;
Ville Syrjälä935b59772011-12-20 00:06:48 +02002282
Dave Airlief453ba02008-11-07 14:05:41 -08002283 mutex_lock(&dev->mode_config.mutex);
2284
Dave Airlief453ba02008-11-07 14:05:41 -08002285 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002286 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002287 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002288 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002289 goto out;
2290 }
2291
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002292 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002293 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002294 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002295
2296out:
2297 mutex_unlock(&dev->mode_config.mutex);
2298 return ret;
2299}
2300
2301/**
2302 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002303 * @dev: drm device for the ioctl
2304 * @data: data pointer for the ioctl
2305 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002306 *
Dave Airlief453ba02008-11-07 14:05:41 -08002307 * Remove the FB specified by the user.
2308 *
2309 * Called by the user via ioctl.
2310 *
2311 * RETURNS:
2312 * Zero on success, errno on failure.
2313 */
2314int drm_mode_rmfb(struct drm_device *dev,
2315 void *data, struct drm_file *file_priv)
2316{
2317 struct drm_mode_object *obj;
2318 struct drm_framebuffer *fb = NULL;
2319 struct drm_framebuffer *fbl = NULL;
2320 uint32_t *id = data;
2321 int ret = 0;
2322 int found = 0;
2323
Dave Airliefb3b06c2011-02-08 13:55:21 +10002324 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2325 return -EINVAL;
2326
Dave Airlief453ba02008-11-07 14:05:41 -08002327 mutex_lock(&dev->mode_config.mutex);
2328 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002329 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002330 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002331 ret = -EINVAL;
2332 goto out;
2333 }
2334 fb = obj_to_fb(obj);
2335
2336 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2337 if (fb == fbl)
2338 found = 1;
2339
2340 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002341 ret = -EINVAL;
2342 goto out;
2343 }
2344
Rob Clarkf7eff602012-09-05 21:48:38 +00002345 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002346
2347out:
2348 mutex_unlock(&dev->mode_config.mutex);
2349 return ret;
2350}
2351
2352/**
2353 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002354 * @dev: drm device for the ioctl
2355 * @data: data pointer for the ioctl
2356 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002357 *
Dave Airlief453ba02008-11-07 14:05:41 -08002358 * Lookup the FB given its ID and return info about it.
2359 *
2360 * Called by the user via ioctl.
2361 *
2362 * RETURNS:
2363 * Zero on success, errno on failure.
2364 */
2365int drm_mode_getfb(struct drm_device *dev,
2366 void *data, struct drm_file *file_priv)
2367{
2368 struct drm_mode_fb_cmd *r = data;
2369 struct drm_mode_object *obj;
2370 struct drm_framebuffer *fb;
2371 int ret = 0;
2372
Dave Airliefb3b06c2011-02-08 13:55:21 +10002373 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2374 return -EINVAL;
2375
Dave Airlief453ba02008-11-07 14:05:41 -08002376 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002377 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002378 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002379 ret = -EINVAL;
2380 goto out;
2381 }
2382 fb = obj_to_fb(obj);
2383
2384 r->height = fb->height;
2385 r->width = fb->width;
2386 r->depth = fb->depth;
2387 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002388 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002389 fb->funcs->create_handle(fb, file_priv, &r->handle);
2390
2391out:
2392 mutex_unlock(&dev->mode_config.mutex);
2393 return ret;
2394}
2395
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002396int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2397 void *data, struct drm_file *file_priv)
2398{
2399 struct drm_clip_rect __user *clips_ptr;
2400 struct drm_clip_rect *clips = NULL;
2401 struct drm_mode_fb_dirty_cmd *r = data;
2402 struct drm_mode_object *obj;
2403 struct drm_framebuffer *fb;
2404 unsigned flags;
2405 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002406 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002407
Dave Airliefb3b06c2011-02-08 13:55:21 +10002408 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2409 return -EINVAL;
2410
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002411 mutex_lock(&dev->mode_config.mutex);
2412 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2413 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002414 ret = -EINVAL;
2415 goto out_err1;
2416 }
2417 fb = obj_to_fb(obj);
2418
2419 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002420 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002421
2422 if (!num_clips != !clips_ptr) {
2423 ret = -EINVAL;
2424 goto out_err1;
2425 }
2426
2427 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2428
2429 /* If userspace annotates copy, clips must come in pairs */
2430 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2431 ret = -EINVAL;
2432 goto out_err1;
2433 }
2434
2435 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002436 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2437 ret = -EINVAL;
2438 goto out_err1;
2439 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002440 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2441 if (!clips) {
2442 ret = -ENOMEM;
2443 goto out_err1;
2444 }
2445
2446 ret = copy_from_user(clips, clips_ptr,
2447 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002448 if (ret) {
2449 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002450 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002451 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002452 }
2453
2454 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002455 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2456 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002457 } else {
2458 ret = -ENOSYS;
2459 goto out_err2;
2460 }
2461
2462out_err2:
2463 kfree(clips);
2464out_err1:
2465 mutex_unlock(&dev->mode_config.mutex);
2466 return ret;
2467}
2468
2469
Dave Airlief453ba02008-11-07 14:05:41 -08002470/**
2471 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002472 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002473 *
Dave Airlief453ba02008-11-07 14:05:41 -08002474 * Destroy all the FBs associated with @filp.
2475 *
2476 * Called by the user via ioctl.
2477 *
2478 * RETURNS:
2479 * Zero on success, errno on failure.
2480 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002481void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002482{
Dave Airlief453ba02008-11-07 14:05:41 -08002483 struct drm_device *dev = priv->minor->dev;
2484 struct drm_framebuffer *fb, *tfb;
2485
2486 mutex_lock(&dev->mode_config.mutex);
2487 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00002488 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002489 }
2490 mutex_unlock(&dev->mode_config.mutex);
2491}
2492
2493/**
2494 * drm_mode_attachmode - add a mode to the user mode list
2495 * @dev: DRM device
2496 * @connector: connector to add the mode to
2497 * @mode: mode to add
2498 *
2499 * Add @mode to @connector's user mode list.
2500 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002501static void drm_mode_attachmode(struct drm_device *dev,
2502 struct drm_connector *connector,
2503 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002504{
Dave Airlief453ba02008-11-07 14:05:41 -08002505 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002506}
2507
2508int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002509 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002510{
2511 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002512 int ret = 0;
2513 struct drm_display_mode *dup_mode, *next;
2514 LIST_HEAD(list);
2515
Dave Airlief453ba02008-11-07 14:05:41 -08002516 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2517 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002518 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002519 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002520 dup_mode = drm_mode_duplicate(dev, mode);
2521 if (!dup_mode) {
2522 ret = -ENOMEM;
2523 goto out;
2524 }
2525 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002526 }
2527 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002528
2529 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2530 if (!connector->encoder)
2531 continue;
2532 if (connector->encoder->crtc == crtc)
2533 list_move_tail(list.next, &connector->user_modes);
2534 }
2535
2536 WARN_ON(!list_empty(&list));
2537
2538 out:
2539 list_for_each_entry_safe(dup_mode, next, &list, head)
2540 drm_mode_destroy(dev, dup_mode);
2541
2542 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002543}
2544EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2545
2546static int drm_mode_detachmode(struct drm_device *dev,
2547 struct drm_connector *connector,
2548 struct drm_display_mode *mode)
2549{
2550 int found = 0;
2551 int ret = 0;
2552 struct drm_display_mode *match_mode, *t;
2553
2554 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2555 if (drm_mode_equal(match_mode, mode)) {
2556 list_del(&match_mode->head);
2557 drm_mode_destroy(dev, match_mode);
2558 found = 1;
2559 break;
2560 }
2561 }
2562
2563 if (!found)
2564 ret = -EINVAL;
2565
2566 return ret;
2567}
2568
2569int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2570{
2571 struct drm_connector *connector;
2572
2573 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2574 drm_mode_detachmode(dev, connector, mode);
2575 }
2576 return 0;
2577}
2578EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2579
2580/**
2581 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002582 * @dev: drm device for the ioctl
2583 * @data: data pointer for the ioctl
2584 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002585 *
2586 * This attaches a user specified mode to an connector.
2587 * Called by the user via ioctl.
2588 *
2589 * RETURNS:
2590 * Zero on success, errno on failure.
2591 */
2592int drm_mode_attachmode_ioctl(struct drm_device *dev,
2593 void *data, struct drm_file *file_priv)
2594{
2595 struct drm_mode_mode_cmd *mode_cmd = data;
2596 struct drm_connector *connector;
2597 struct drm_display_mode *mode;
2598 struct drm_mode_object *obj;
2599 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002600 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002601
Dave Airliefb3b06c2011-02-08 13:55:21 +10002602 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2603 return -EINVAL;
2604
Dave Airlief453ba02008-11-07 14:05:41 -08002605 mutex_lock(&dev->mode_config.mutex);
2606
2607 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2608 if (!obj) {
2609 ret = -EINVAL;
2610 goto out;
2611 }
2612 connector = obj_to_connector(obj);
2613
2614 mode = drm_mode_create(dev);
2615 if (!mode) {
2616 ret = -ENOMEM;
2617 goto out;
2618 }
2619
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002620 ret = drm_crtc_convert_umode(mode, umode);
2621 if (ret) {
2622 DRM_DEBUG_KMS("Invalid mode\n");
2623 drm_mode_destroy(dev, mode);
2624 goto out;
2625 }
Dave Airlief453ba02008-11-07 14:05:41 -08002626
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002627 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002628out:
2629 mutex_unlock(&dev->mode_config.mutex);
2630 return ret;
2631}
2632
2633
2634/**
2635 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002636 * @dev: drm device for the ioctl
2637 * @data: data pointer for the ioctl
2638 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002639 *
2640 * Called by the user via ioctl.
2641 *
2642 * RETURNS:
2643 * Zero on success, errno on failure.
2644 */
2645int drm_mode_detachmode_ioctl(struct drm_device *dev,
2646 void *data, struct drm_file *file_priv)
2647{
2648 struct drm_mode_object *obj;
2649 struct drm_mode_mode_cmd *mode_cmd = data;
2650 struct drm_connector *connector;
2651 struct drm_display_mode mode;
2652 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002653 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002654
Dave Airliefb3b06c2011-02-08 13:55:21 +10002655 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2656 return -EINVAL;
2657
Dave Airlief453ba02008-11-07 14:05:41 -08002658 mutex_lock(&dev->mode_config.mutex);
2659
2660 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2661 if (!obj) {
2662 ret = -EINVAL;
2663 goto out;
2664 }
2665 connector = obj_to_connector(obj);
2666
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002667 ret = drm_crtc_convert_umode(&mode, umode);
2668 if (ret) {
2669 DRM_DEBUG_KMS("Invalid mode\n");
2670 goto out;
2671 }
2672
Dave Airlief453ba02008-11-07 14:05:41 -08002673 ret = drm_mode_detachmode(dev, connector, &mode);
2674out:
2675 mutex_unlock(&dev->mode_config.mutex);
2676 return ret;
2677}
2678
2679struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2680 const char *name, int num_values)
2681{
2682 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002683 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002684
2685 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2686 if (!property)
2687 return NULL;
2688
2689 if (num_values) {
2690 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2691 if (!property->values)
2692 goto fail;
2693 }
2694
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002695 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2696 if (ret)
2697 goto fail;
2698
Dave Airlief453ba02008-11-07 14:05:41 -08002699 property->flags = flags;
2700 property->num_values = num_values;
2701 INIT_LIST_HEAD(&property->enum_blob_list);
2702
Vinson Lee471dd2e2011-11-10 11:55:40 -08002703 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002704 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002705 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2706 }
Dave Airlief453ba02008-11-07 14:05:41 -08002707
2708 list_add_tail(&property->head, &dev->mode_config.property_list);
2709 return property;
2710fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002711 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002712 kfree(property);
2713 return NULL;
2714}
2715EXPORT_SYMBOL(drm_property_create);
2716
Sascha Hauer4a67d392012-02-06 10:58:17 +01002717struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2718 const char *name,
2719 const struct drm_prop_enum_list *props,
2720 int num_values)
2721{
2722 struct drm_property *property;
2723 int i, ret;
2724
2725 flags |= DRM_MODE_PROP_ENUM;
2726
2727 property = drm_property_create(dev, flags, name, num_values);
2728 if (!property)
2729 return NULL;
2730
2731 for (i = 0; i < num_values; i++) {
2732 ret = drm_property_add_enum(property, i,
2733 props[i].type,
2734 props[i].name);
2735 if (ret) {
2736 drm_property_destroy(dev, property);
2737 return NULL;
2738 }
2739 }
2740
2741 return property;
2742}
2743EXPORT_SYMBOL(drm_property_create_enum);
2744
Rob Clark49e27542012-05-17 02:23:26 -06002745struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2746 int flags, const char *name,
2747 const struct drm_prop_enum_list *props,
2748 int num_values)
2749{
2750 struct drm_property *property;
2751 int i, ret;
2752
2753 flags |= DRM_MODE_PROP_BITMASK;
2754
2755 property = drm_property_create(dev, flags, name, num_values);
2756 if (!property)
2757 return NULL;
2758
2759 for (i = 0; i < num_values; i++) {
2760 ret = drm_property_add_enum(property, i,
2761 props[i].type,
2762 props[i].name);
2763 if (ret) {
2764 drm_property_destroy(dev, property);
2765 return NULL;
2766 }
2767 }
2768
2769 return property;
2770}
2771EXPORT_SYMBOL(drm_property_create_bitmask);
2772
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002773struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2774 const char *name,
2775 uint64_t min, uint64_t max)
2776{
2777 struct drm_property *property;
2778
2779 flags |= DRM_MODE_PROP_RANGE;
2780
2781 property = drm_property_create(dev, flags, name, 2);
2782 if (!property)
2783 return NULL;
2784
2785 property->values[0] = min;
2786 property->values[1] = max;
2787
2788 return property;
2789}
2790EXPORT_SYMBOL(drm_property_create_range);
2791
Dave Airlief453ba02008-11-07 14:05:41 -08002792int drm_property_add_enum(struct drm_property *property, int index,
2793 uint64_t value, const char *name)
2794{
2795 struct drm_property_enum *prop_enum;
2796
Rob Clark49e27542012-05-17 02:23:26 -06002797 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2798 return -EINVAL;
2799
2800 /*
2801 * Bitmask enum properties have the additional constraint of values
2802 * from 0 to 63
2803 */
2804 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002805 return -EINVAL;
2806
2807 if (!list_empty(&property->enum_blob_list)) {
2808 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2809 if (prop_enum->value == value) {
2810 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2811 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2812 return 0;
2813 }
2814 }
2815 }
2816
2817 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2818 if (!prop_enum)
2819 return -ENOMEM;
2820
2821 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2822 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2823 prop_enum->value = value;
2824
2825 property->values[index] = value;
2826 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2827 return 0;
2828}
2829EXPORT_SYMBOL(drm_property_add_enum);
2830
2831void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2832{
2833 struct drm_property_enum *prop_enum, *pt;
2834
2835 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2836 list_del(&prop_enum->head);
2837 kfree(prop_enum);
2838 }
2839
2840 if (property->num_values)
2841 kfree(property->values);
2842 drm_mode_object_put(dev, &property->base);
2843 list_del(&property->head);
2844 kfree(property);
2845}
2846EXPORT_SYMBOL(drm_property_destroy);
2847
Paulo Zanonic5431882012-05-15 18:09:02 -03002848void drm_object_attach_property(struct drm_mode_object *obj,
2849 struct drm_property *property,
2850 uint64_t init_val)
2851{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002852 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002853
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002854 if (count == DRM_OBJECT_MAX_PROPERTY) {
2855 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2856 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2857 "you see this message on the same object type.\n",
2858 obj->type);
2859 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002860 }
2861
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002862 obj->properties->ids[count] = property->base.id;
2863 obj->properties->values[count] = init_val;
2864 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002865}
2866EXPORT_SYMBOL(drm_object_attach_property);
2867
2868int drm_object_property_set_value(struct drm_mode_object *obj,
2869 struct drm_property *property, uint64_t val)
2870{
2871 int i;
2872
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002873 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002874 if (obj->properties->ids[i] == property->base.id) {
2875 obj->properties->values[i] = val;
2876 return 0;
2877 }
2878 }
2879
2880 return -EINVAL;
2881}
2882EXPORT_SYMBOL(drm_object_property_set_value);
2883
2884int drm_object_property_get_value(struct drm_mode_object *obj,
2885 struct drm_property *property, uint64_t *val)
2886{
2887 int i;
2888
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002889 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002890 if (obj->properties->ids[i] == property->base.id) {
2891 *val = obj->properties->values[i];
2892 return 0;
2893 }
2894 }
2895
2896 return -EINVAL;
2897}
2898EXPORT_SYMBOL(drm_object_property_get_value);
2899
Dave Airlief453ba02008-11-07 14:05:41 -08002900int drm_mode_getproperty_ioctl(struct drm_device *dev,
2901 void *data, struct drm_file *file_priv)
2902{
2903 struct drm_mode_object *obj;
2904 struct drm_mode_get_property *out_resp = data;
2905 struct drm_property *property;
2906 int enum_count = 0;
2907 int blob_count = 0;
2908 int value_count = 0;
2909 int ret = 0, i;
2910 int copied;
2911 struct drm_property_enum *prop_enum;
2912 struct drm_mode_property_enum __user *enum_ptr;
2913 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002914 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002915 uint64_t __user *values_ptr;
2916 uint32_t __user *blob_length_ptr;
2917
Dave Airliefb3b06c2011-02-08 13:55:21 +10002918 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2919 return -EINVAL;
2920
Dave Airlief453ba02008-11-07 14:05:41 -08002921 mutex_lock(&dev->mode_config.mutex);
2922 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2923 if (!obj) {
2924 ret = -EINVAL;
2925 goto done;
2926 }
2927 property = obj_to_property(obj);
2928
Rob Clark49e27542012-05-17 02:23:26 -06002929 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002930 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2931 enum_count++;
2932 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2933 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2934 blob_count++;
2935 }
2936
2937 value_count = property->num_values;
2938
2939 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2940 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2941 out_resp->flags = property->flags;
2942
2943 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002944 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002945 for (i = 0; i < value_count; i++) {
2946 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2947 ret = -EFAULT;
2948 goto done;
2949 }
2950 }
2951 }
2952 out_resp->count_values = value_count;
2953
Rob Clark49e27542012-05-17 02:23:26 -06002954 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002955 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2956 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002957 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002958 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2959
2960 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2961 ret = -EFAULT;
2962 goto done;
2963 }
2964
2965 if (copy_to_user(&enum_ptr[copied].name,
2966 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2967 ret = -EFAULT;
2968 goto done;
2969 }
2970 copied++;
2971 }
2972 }
2973 out_resp->count_enum_blobs = enum_count;
2974 }
2975
2976 if (property->flags & DRM_MODE_PROP_BLOB) {
2977 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2978 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002979 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2980 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002981
2982 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2983 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2984 ret = -EFAULT;
2985 goto done;
2986 }
2987
2988 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2989 ret = -EFAULT;
2990 goto done;
2991 }
2992
2993 copied++;
2994 }
2995 }
2996 out_resp->count_enum_blobs = blob_count;
2997 }
2998done:
2999 mutex_unlock(&dev->mode_config.mutex);
3000 return ret;
3001}
3002
3003static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3004 void *data)
3005{
3006 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003007 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003008
3009 if (!length || !data)
3010 return NULL;
3011
3012 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3013 if (!blob)
3014 return NULL;
3015
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003016 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3017 if (ret) {
3018 kfree(blob);
3019 return NULL;
3020 }
3021
Dave Airlief453ba02008-11-07 14:05:41 -08003022 blob->length = length;
3023
3024 memcpy(blob->data, data, length);
3025
Dave Airlief453ba02008-11-07 14:05:41 -08003026 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3027 return blob;
3028}
3029
3030static void drm_property_destroy_blob(struct drm_device *dev,
3031 struct drm_property_blob *blob)
3032{
3033 drm_mode_object_put(dev, &blob->base);
3034 list_del(&blob->head);
3035 kfree(blob);
3036}
3037
3038int drm_mode_getblob_ioctl(struct drm_device *dev,
3039 void *data, struct drm_file *file_priv)
3040{
3041 struct drm_mode_object *obj;
3042 struct drm_mode_get_blob *out_resp = data;
3043 struct drm_property_blob *blob;
3044 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003045 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003046
Dave Airliefb3b06c2011-02-08 13:55:21 +10003047 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3048 return -EINVAL;
3049
Dave Airlief453ba02008-11-07 14:05:41 -08003050 mutex_lock(&dev->mode_config.mutex);
3051 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3052 if (!obj) {
3053 ret = -EINVAL;
3054 goto done;
3055 }
3056 blob = obj_to_blob(obj);
3057
3058 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003059 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003060 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3061 ret = -EFAULT;
3062 goto done;
3063 }
3064 }
3065 out_resp->length = blob->length;
3066
3067done:
3068 mutex_unlock(&dev->mode_config.mutex);
3069 return ret;
3070}
3071
3072int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3073 struct edid *edid)
3074{
3075 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003076 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003077
3078 if (connector->edid_blob_ptr)
3079 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3080
3081 /* Delete edid, when there is none. */
3082 if (!edid) {
3083 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003084 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003085 return ret;
3086 }
3087
Adam Jackson7466f4c2010-03-29 21:43:23 +00003088 size = EDID_LENGTH * (1 + edid->extensions);
3089 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3090 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003091 if (!connector->edid_blob_ptr)
3092 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003093
Rob Clark58495562012-10-11 20:50:56 -05003094 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003095 dev->mode_config.edid_property,
3096 connector->edid_blob_ptr->base.id);
3097
3098 return ret;
3099}
3100EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3101
Paulo Zanoni26a34812012-05-15 18:08:59 -03003102static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003103 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003104{
3105 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3106 return false;
3107 if (property->flags & DRM_MODE_PROP_RANGE) {
3108 if (value < property->values[0] || value > property->values[1])
3109 return false;
3110 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003111 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3112 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003113 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003114 for (i = 0; i < property->num_values; i++)
3115 valid_mask |= (1ULL << property->values[i]);
3116 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003117 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3118 /* Only the driver knows */
3119 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003120 } else {
3121 int i;
3122 for (i = 0; i < property->num_values; i++)
3123 if (property->values[i] == value)
3124 return true;
3125 return false;
3126 }
3127}
3128
Dave Airlief453ba02008-11-07 14:05:41 -08003129int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3130 void *data, struct drm_file *file_priv)
3131{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003132 struct drm_mode_connector_set_property *conn_set_prop = data;
3133 struct drm_mode_obj_set_property obj_set_prop = {
3134 .value = conn_set_prop->value,
3135 .prop_id = conn_set_prop->prop_id,
3136 .obj_id = conn_set_prop->connector_id,
3137 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3138 };
Dave Airlief453ba02008-11-07 14:05:41 -08003139
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003140 /* It does all the locking and checking we need */
3141 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003142}
3143
Paulo Zanonic5431882012-05-15 18:09:02 -03003144static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3145 struct drm_property *property,
3146 uint64_t value)
3147{
3148 int ret = -EINVAL;
3149 struct drm_connector *connector = obj_to_connector(obj);
3150
3151 /* Do DPMS ourselves */
3152 if (property == connector->dev->mode_config.dpms_property) {
3153 if (connector->funcs->dpms)
3154 (*connector->funcs->dpms)(connector, (int)value);
3155 ret = 0;
3156 } else if (connector->funcs->set_property)
3157 ret = connector->funcs->set_property(connector, property, value);
3158
3159 /* store the property value if successful */
3160 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003161 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003162 return ret;
3163}
3164
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003165static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3166 struct drm_property *property,
3167 uint64_t value)
3168{
3169 int ret = -EINVAL;
3170 struct drm_crtc *crtc = obj_to_crtc(obj);
3171
3172 if (crtc->funcs->set_property)
3173 ret = crtc->funcs->set_property(crtc, property, value);
3174 if (!ret)
3175 drm_object_property_set_value(obj, property, value);
3176
3177 return ret;
3178}
3179
Rob Clark4d939142012-05-17 02:23:27 -06003180static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3181 struct drm_property *property,
3182 uint64_t value)
3183{
3184 int ret = -EINVAL;
3185 struct drm_plane *plane = obj_to_plane(obj);
3186
3187 if (plane->funcs->set_property)
3188 ret = plane->funcs->set_property(plane, property, value);
3189 if (!ret)
3190 drm_object_property_set_value(obj, property, value);
3191
3192 return ret;
3193}
3194
Paulo Zanonic5431882012-05-15 18:09:02 -03003195int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3196 struct drm_file *file_priv)
3197{
3198 struct drm_mode_obj_get_properties *arg = data;
3199 struct drm_mode_object *obj;
3200 int ret = 0;
3201 int i;
3202 int copied = 0;
3203 int props_count = 0;
3204 uint32_t __user *props_ptr;
3205 uint64_t __user *prop_values_ptr;
3206
3207 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3208 return -EINVAL;
3209
3210 mutex_lock(&dev->mode_config.mutex);
3211
3212 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3213 if (!obj) {
3214 ret = -EINVAL;
3215 goto out;
3216 }
3217 if (!obj->properties) {
3218 ret = -EINVAL;
3219 goto out;
3220 }
3221
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003222 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003223
3224 /* This ioctl is called twice, once to determine how much space is
3225 * needed, and the 2nd time to fill it. */
3226 if ((arg->count_props >= props_count) && props_count) {
3227 copied = 0;
3228 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3229 prop_values_ptr = (uint64_t __user *)(unsigned long)
3230 (arg->prop_values_ptr);
3231 for (i = 0; i < props_count; i++) {
3232 if (put_user(obj->properties->ids[i],
3233 props_ptr + copied)) {
3234 ret = -EFAULT;
3235 goto out;
3236 }
3237 if (put_user(obj->properties->values[i],
3238 prop_values_ptr + copied)) {
3239 ret = -EFAULT;
3240 goto out;
3241 }
3242 copied++;
3243 }
3244 }
3245 arg->count_props = props_count;
3246out:
3247 mutex_unlock(&dev->mode_config.mutex);
3248 return ret;
3249}
3250
3251int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3252 struct drm_file *file_priv)
3253{
3254 struct drm_mode_obj_set_property *arg = data;
3255 struct drm_mode_object *arg_obj;
3256 struct drm_mode_object *prop_obj;
3257 struct drm_property *property;
3258 int ret = -EINVAL;
3259 int i;
3260
3261 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3262 return -EINVAL;
3263
3264 mutex_lock(&dev->mode_config.mutex);
3265
3266 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3267 if (!arg_obj)
3268 goto out;
3269 if (!arg_obj->properties)
3270 goto out;
3271
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003272 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003273 if (arg_obj->properties->ids[i] == arg->prop_id)
3274 break;
3275
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003276 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003277 goto out;
3278
3279 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3280 DRM_MODE_OBJECT_PROPERTY);
3281 if (!prop_obj)
3282 goto out;
3283 property = obj_to_property(prop_obj);
3284
3285 if (!drm_property_change_is_valid(property, arg->value))
3286 goto out;
3287
3288 switch (arg_obj->type) {
3289 case DRM_MODE_OBJECT_CONNECTOR:
3290 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3291 arg->value);
3292 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003293 case DRM_MODE_OBJECT_CRTC:
3294 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3295 break;
Rob Clark4d939142012-05-17 02:23:27 -06003296 case DRM_MODE_OBJECT_PLANE:
3297 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3298 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003299 }
3300
3301out:
3302 mutex_unlock(&dev->mode_config.mutex);
3303 return ret;
3304}
3305
Dave Airlief453ba02008-11-07 14:05:41 -08003306int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3307 struct drm_encoder *encoder)
3308{
3309 int i;
3310
3311 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3312 if (connector->encoder_ids[i] == 0) {
3313 connector->encoder_ids[i] = encoder->base.id;
3314 return 0;
3315 }
3316 }
3317 return -ENOMEM;
3318}
3319EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3320
3321void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3322 struct drm_encoder *encoder)
3323{
3324 int i;
3325 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3326 if (connector->encoder_ids[i] == encoder->base.id) {
3327 connector->encoder_ids[i] = 0;
3328 if (connector->encoder == encoder)
3329 connector->encoder = NULL;
3330 break;
3331 }
3332 }
3333}
3334EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3335
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003336int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003337 int gamma_size)
3338{
3339 crtc->gamma_size = gamma_size;
3340
3341 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3342 if (!crtc->gamma_store) {
3343 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003344 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003345 }
3346
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003347 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003348}
3349EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3350
3351int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3352 void *data, struct drm_file *file_priv)
3353{
3354 struct drm_mode_crtc_lut *crtc_lut = data;
3355 struct drm_mode_object *obj;
3356 struct drm_crtc *crtc;
3357 void *r_base, *g_base, *b_base;
3358 int size;
3359 int ret = 0;
3360
Dave Airliefb3b06c2011-02-08 13:55:21 +10003361 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3362 return -EINVAL;
3363
Dave Airlief453ba02008-11-07 14:05:41 -08003364 mutex_lock(&dev->mode_config.mutex);
3365 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3366 if (!obj) {
3367 ret = -EINVAL;
3368 goto out;
3369 }
3370 crtc = obj_to_crtc(obj);
3371
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003372 if (crtc->funcs->gamma_set == NULL) {
3373 ret = -ENOSYS;
3374 goto out;
3375 }
3376
Dave Airlief453ba02008-11-07 14:05:41 -08003377 /* memcpy into gamma store */
3378 if (crtc_lut->gamma_size != crtc->gamma_size) {
3379 ret = -EINVAL;
3380 goto out;
3381 }
3382
3383 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3384 r_base = crtc->gamma_store;
3385 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3386 ret = -EFAULT;
3387 goto out;
3388 }
3389
3390 g_base = r_base + size;
3391 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3392 ret = -EFAULT;
3393 goto out;
3394 }
3395
3396 b_base = g_base + size;
3397 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3398 ret = -EFAULT;
3399 goto out;
3400 }
3401
James Simmons72034252010-08-03 01:33:19 +01003402 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003403
3404out:
3405 mutex_unlock(&dev->mode_config.mutex);
3406 return ret;
3407
3408}
3409
3410int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3411 void *data, struct drm_file *file_priv)
3412{
3413 struct drm_mode_crtc_lut *crtc_lut = data;
3414 struct drm_mode_object *obj;
3415 struct drm_crtc *crtc;
3416 void *r_base, *g_base, *b_base;
3417 int size;
3418 int ret = 0;
3419
Dave Airliefb3b06c2011-02-08 13:55:21 +10003420 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3421 return -EINVAL;
3422
Dave Airlief453ba02008-11-07 14:05:41 -08003423 mutex_lock(&dev->mode_config.mutex);
3424 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3425 if (!obj) {
3426 ret = -EINVAL;
3427 goto out;
3428 }
3429 crtc = obj_to_crtc(obj);
3430
3431 /* memcpy into gamma store */
3432 if (crtc_lut->gamma_size != crtc->gamma_size) {
3433 ret = -EINVAL;
3434 goto out;
3435 }
3436
3437 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3438 r_base = crtc->gamma_store;
3439 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3440 ret = -EFAULT;
3441 goto out;
3442 }
3443
3444 g_base = r_base + size;
3445 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3446 ret = -EFAULT;
3447 goto out;
3448 }
3449
3450 b_base = g_base + size;
3451 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3452 ret = -EFAULT;
3453 goto out;
3454 }
3455out:
3456 mutex_unlock(&dev->mode_config.mutex);
3457 return ret;
3458}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003459
3460int drm_mode_page_flip_ioctl(struct drm_device *dev,
3461 void *data, struct drm_file *file_priv)
3462{
3463 struct drm_mode_crtc_page_flip *page_flip = data;
3464 struct drm_mode_object *obj;
3465 struct drm_crtc *crtc;
3466 struct drm_framebuffer *fb;
3467 struct drm_pending_vblank_event *e = NULL;
3468 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003469 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003470 int ret = -EINVAL;
3471
3472 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3473 page_flip->reserved != 0)
3474 return -EINVAL;
3475
3476 mutex_lock(&dev->mode_config.mutex);
3477 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3478 if (!obj)
3479 goto out;
3480 crtc = obj_to_crtc(obj);
3481
Chris Wilson90c1efd2010-07-17 20:23:26 +01003482 if (crtc->fb == NULL) {
3483 /* The framebuffer is currently unbound, presumably
3484 * due to a hotplug event, that userspace has not
3485 * yet discovered.
3486 */
3487 ret = -EBUSY;
3488 goto out;
3489 }
3490
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003491 if (crtc->funcs->page_flip == NULL)
3492 goto out;
3493
3494 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3495 if (!obj)
3496 goto out;
3497 fb = obj_to_fb(obj);
3498
Rob Clark7c80e122012-09-04 16:35:56 +00003499 hdisplay = crtc->mode.hdisplay;
3500 vdisplay = crtc->mode.vdisplay;
3501
3502 if (crtc->invert_dimensions)
3503 swap(hdisplay, vdisplay);
3504
3505 if (hdisplay > fb->width ||
3506 vdisplay > fb->height ||
3507 crtc->x > fb->width - hdisplay ||
3508 crtc->y > fb->height - vdisplay) {
3509 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3510 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3511 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003512 ret = -ENOSPC;
3513 goto out;
3514 }
3515
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003516 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3517 ret = -ENOMEM;
3518 spin_lock_irqsave(&dev->event_lock, flags);
3519 if (file_priv->event_space < sizeof e->event) {
3520 spin_unlock_irqrestore(&dev->event_lock, flags);
3521 goto out;
3522 }
3523 file_priv->event_space -= sizeof e->event;
3524 spin_unlock_irqrestore(&dev->event_lock, flags);
3525
3526 e = kzalloc(sizeof *e, GFP_KERNEL);
3527 if (e == NULL) {
3528 spin_lock_irqsave(&dev->event_lock, flags);
3529 file_priv->event_space += sizeof e->event;
3530 spin_unlock_irqrestore(&dev->event_lock, flags);
3531 goto out;
3532 }
3533
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003534 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003535 e->event.base.length = sizeof e->event;
3536 e->event.user_data = page_flip->user_data;
3537 e->base.event = &e->event.base;
3538 e->base.file_priv = file_priv;
3539 e->base.destroy =
3540 (void (*) (struct drm_pending_event *)) kfree;
3541 }
3542
3543 ret = crtc->funcs->page_flip(crtc, fb, e);
3544 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003545 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3546 spin_lock_irqsave(&dev->event_lock, flags);
3547 file_priv->event_space += sizeof e->event;
3548 spin_unlock_irqrestore(&dev->event_lock, flags);
3549 kfree(e);
3550 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003551 }
3552
3553out:
3554 mutex_unlock(&dev->mode_config.mutex);
3555 return ret;
3556}
Chris Wilsoneb033552011-01-24 15:11:08 +00003557
3558void drm_mode_config_reset(struct drm_device *dev)
3559{
3560 struct drm_crtc *crtc;
3561 struct drm_encoder *encoder;
3562 struct drm_connector *connector;
3563
3564 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3565 if (crtc->funcs->reset)
3566 crtc->funcs->reset(crtc);
3567
3568 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3569 if (encoder->funcs->reset)
3570 encoder->funcs->reset(encoder);
3571
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003572 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3573 connector->status = connector_status_unknown;
3574
Chris Wilsoneb033552011-01-24 15:11:08 +00003575 if (connector->funcs->reset)
3576 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003577 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003578}
3579EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003580
3581int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3582 void *data, struct drm_file *file_priv)
3583{
3584 struct drm_mode_create_dumb *args = data;
3585
3586 if (!dev->driver->dumb_create)
3587 return -ENOSYS;
3588 return dev->driver->dumb_create(file_priv, dev, args);
3589}
3590
3591int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3592 void *data, struct drm_file *file_priv)
3593{
3594 struct drm_mode_map_dumb *args = data;
3595
3596 /* call driver ioctl to get mmap offset */
3597 if (!dev->driver->dumb_map_offset)
3598 return -ENOSYS;
3599
3600 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3601}
3602
3603int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3604 void *data, struct drm_file *file_priv)
3605{
3606 struct drm_mode_destroy_dumb *args = data;
3607
3608 if (!dev->driver->dumb_destroy)
3609 return -ENOSYS;
3610
3611 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3612}
Dave Airlie248dbc22011-11-29 20:02:54 +00003613
3614/*
3615 * Just need to support RGB formats here for compat with code that doesn't
3616 * use pixel formats directly yet.
3617 */
3618void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3619 int *bpp)
3620{
3621 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003622 case DRM_FORMAT_RGB332:
3623 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003624 *depth = 8;
3625 *bpp = 8;
3626 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003627 case DRM_FORMAT_XRGB1555:
3628 case DRM_FORMAT_XBGR1555:
3629 case DRM_FORMAT_RGBX5551:
3630 case DRM_FORMAT_BGRX5551:
3631 case DRM_FORMAT_ARGB1555:
3632 case DRM_FORMAT_ABGR1555:
3633 case DRM_FORMAT_RGBA5551:
3634 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003635 *depth = 15;
3636 *bpp = 16;
3637 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003638 case DRM_FORMAT_RGB565:
3639 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003640 *depth = 16;
3641 *bpp = 16;
3642 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003643 case DRM_FORMAT_RGB888:
3644 case DRM_FORMAT_BGR888:
3645 *depth = 24;
3646 *bpp = 24;
3647 break;
3648 case DRM_FORMAT_XRGB8888:
3649 case DRM_FORMAT_XBGR8888:
3650 case DRM_FORMAT_RGBX8888:
3651 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003652 *depth = 24;
3653 *bpp = 32;
3654 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003655 case DRM_FORMAT_XRGB2101010:
3656 case DRM_FORMAT_XBGR2101010:
3657 case DRM_FORMAT_RGBX1010102:
3658 case DRM_FORMAT_BGRX1010102:
3659 case DRM_FORMAT_ARGB2101010:
3660 case DRM_FORMAT_ABGR2101010:
3661 case DRM_FORMAT_RGBA1010102:
3662 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003663 *depth = 30;
3664 *bpp = 32;
3665 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003666 case DRM_FORMAT_ARGB8888:
3667 case DRM_FORMAT_ABGR8888:
3668 case DRM_FORMAT_RGBA8888:
3669 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003670 *depth = 32;
3671 *bpp = 32;
3672 break;
3673 default:
3674 DRM_DEBUG_KMS("unsupported pixel format\n");
3675 *depth = 0;
3676 *bpp = 0;
3677 break;
3678 }
3679}
3680EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003681
3682/**
3683 * drm_format_num_planes - get the number of planes for format
3684 * @format: pixel format (DRM_FORMAT_*)
3685 *
3686 * RETURNS:
3687 * The number of planes used by the specified pixel format.
3688 */
3689int drm_format_num_planes(uint32_t format)
3690{
3691 switch (format) {
3692 case DRM_FORMAT_YUV410:
3693 case DRM_FORMAT_YVU410:
3694 case DRM_FORMAT_YUV411:
3695 case DRM_FORMAT_YVU411:
3696 case DRM_FORMAT_YUV420:
3697 case DRM_FORMAT_YVU420:
3698 case DRM_FORMAT_YUV422:
3699 case DRM_FORMAT_YVU422:
3700 case DRM_FORMAT_YUV444:
3701 case DRM_FORMAT_YVU444:
3702 return 3;
3703 case DRM_FORMAT_NV12:
3704 case DRM_FORMAT_NV21:
3705 case DRM_FORMAT_NV16:
3706 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003707 case DRM_FORMAT_NV24:
3708 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003709 return 2;
3710 default:
3711 return 1;
3712 }
3713}
3714EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003715
3716/**
3717 * drm_format_plane_cpp - determine the bytes per pixel value
3718 * @format: pixel format (DRM_FORMAT_*)
3719 * @plane: plane index
3720 *
3721 * RETURNS:
3722 * The bytes per pixel value for the specified plane.
3723 */
3724int drm_format_plane_cpp(uint32_t format, int plane)
3725{
3726 unsigned int depth;
3727 int bpp;
3728
3729 if (plane >= drm_format_num_planes(format))
3730 return 0;
3731
3732 switch (format) {
3733 case DRM_FORMAT_YUYV:
3734 case DRM_FORMAT_YVYU:
3735 case DRM_FORMAT_UYVY:
3736 case DRM_FORMAT_VYUY:
3737 return 2;
3738 case DRM_FORMAT_NV12:
3739 case DRM_FORMAT_NV21:
3740 case DRM_FORMAT_NV16:
3741 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003742 case DRM_FORMAT_NV24:
3743 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003744 return plane ? 2 : 1;
3745 case DRM_FORMAT_YUV410:
3746 case DRM_FORMAT_YVU410:
3747 case DRM_FORMAT_YUV411:
3748 case DRM_FORMAT_YVU411:
3749 case DRM_FORMAT_YUV420:
3750 case DRM_FORMAT_YVU420:
3751 case DRM_FORMAT_YUV422:
3752 case DRM_FORMAT_YVU422:
3753 case DRM_FORMAT_YUV444:
3754 case DRM_FORMAT_YVU444:
3755 return 1;
3756 default:
3757 drm_fb_get_bpp_depth(format, &depth, &bpp);
3758 return bpp >> 3;
3759 }
3760}
3761EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003762
3763/**
3764 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3765 * @format: pixel format (DRM_FORMAT_*)
3766 *
3767 * RETURNS:
3768 * The horizontal chroma subsampling factor for the
3769 * specified pixel format.
3770 */
3771int drm_format_horz_chroma_subsampling(uint32_t format)
3772{
3773 switch (format) {
3774 case DRM_FORMAT_YUV411:
3775 case DRM_FORMAT_YVU411:
3776 case DRM_FORMAT_YUV410:
3777 case DRM_FORMAT_YVU410:
3778 return 4;
3779 case DRM_FORMAT_YUYV:
3780 case DRM_FORMAT_YVYU:
3781 case DRM_FORMAT_UYVY:
3782 case DRM_FORMAT_VYUY:
3783 case DRM_FORMAT_NV12:
3784 case DRM_FORMAT_NV21:
3785 case DRM_FORMAT_NV16:
3786 case DRM_FORMAT_NV61:
3787 case DRM_FORMAT_YUV422:
3788 case DRM_FORMAT_YVU422:
3789 case DRM_FORMAT_YUV420:
3790 case DRM_FORMAT_YVU420:
3791 return 2;
3792 default:
3793 return 1;
3794 }
3795}
3796EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3797
3798/**
3799 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3800 * @format: pixel format (DRM_FORMAT_*)
3801 *
3802 * RETURNS:
3803 * The vertical chroma subsampling factor for the
3804 * specified pixel format.
3805 */
3806int drm_format_vert_chroma_subsampling(uint32_t format)
3807{
3808 switch (format) {
3809 case DRM_FORMAT_YUV410:
3810 case DRM_FORMAT_YVU410:
3811 return 4;
3812 case DRM_FORMAT_YUV420:
3813 case DRM_FORMAT_YVU420:
3814 case DRM_FORMAT_NV12:
3815 case DRM_FORMAT_NV21:
3816 return 2;
3817 default:
3818 return 1;
3819 }
3820}
3821EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);