blob: 7ac3952f74fc5ed5f861885e0ef8f50ebed9e7c2 [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/**
206 * drm_mode_object_get - allocate a new identifier
207 * @dev: DRM device
208 * @ptr: object pointer, used to generate unique ID
209 * @type: object type
210 *
211 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800212 *
213 * Create a unique identifier based on @ptr in @dev's identifier space. Used
214 * for tracking modes, CRTCs and connectors.
215 *
216 * RETURNS:
217 * New unique (relative to other objects in @dev) integer identifier for the
218 * object.
219 */
220static int drm_mode_object_get(struct drm_device *dev,
221 struct drm_mode_object *obj, uint32_t obj_type)
222{
223 int new_id = 0;
224 int ret;
225
Dave Airlief453ba02008-11-07 14:05:41 -0800226again:
227 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
228 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200229 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800230 }
231
Jesse Barnesad2563c2009-01-19 17:21:45 +1000232 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800233 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000234 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800235 if (ret == -EAGAIN)
236 goto again;
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200237 else if (ret)
238 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800239
240 obj->id = new_id;
241 obj->type = obj_type;
242 return 0;
243}
244
245/**
246 * drm_mode_object_put - free an identifer
247 * @dev: DRM device
248 * @id: ID to free
249 *
250 * LOCKING:
251 * Caller must hold DRM mode_config lock.
252 *
253 * Free @id from @dev's unique identifier pool.
254 */
255static void drm_mode_object_put(struct drm_device *dev,
256 struct drm_mode_object *object)
257{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000258 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800259 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000260 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800261}
262
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200263struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
264 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800265{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000266 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800267
Jesse Barnesad2563c2009-01-19 17:21:45 +1000268 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800269 obj = idr_find(&dev->mode_config.crtc_idr, id);
270 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000271 obj = NULL;
272 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800273
274 return obj;
275}
276EXPORT_SYMBOL(drm_mode_object_find);
277
278/**
Dave Airlief453ba02008-11-07 14:05:41 -0800279 * drm_framebuffer_init - initialize a framebuffer
280 * @dev: DRM device
281 *
282 * LOCKING:
283 * Caller must hold mode config lock.
284 *
285 * Allocates an ID for the framebuffer's parent mode object, sets its mode
286 * functions & device file and adds it to the master fd list.
287 *
288 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200289 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800290 */
291int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
292 const struct drm_framebuffer_funcs *funcs)
293{
294 int ret;
295
Rob Clarkf7eff602012-09-05 21:48:38 +0000296 kref_init(&fb->refcount);
297
Dave Airlief453ba02008-11-07 14:05:41 -0800298 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200299 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800300 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800301
302 fb->dev = dev;
303 fb->funcs = funcs;
304 dev->mode_config.num_fb++;
305 list_add(&fb->head, &dev->mode_config.fb_list);
306
307 return 0;
308}
309EXPORT_SYMBOL(drm_framebuffer_init);
310
Rob Clarkf7eff602012-09-05 21:48:38 +0000311static void drm_framebuffer_free(struct kref *kref)
312{
313 struct drm_framebuffer *fb =
314 container_of(kref, struct drm_framebuffer, refcount);
315 fb->funcs->destroy(fb);
316}
317
318/**
319 * drm_framebuffer_unreference - unref a framebuffer
320 *
321 * LOCKING:
322 * Caller must hold mode config lock.
323 */
324void drm_framebuffer_unreference(struct drm_framebuffer *fb)
325{
326 struct drm_device *dev = fb->dev;
327 DRM_DEBUG("FB ID: %d\n", fb->base.id);
328 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
329 kref_put(&fb->refcount, drm_framebuffer_free);
330}
331EXPORT_SYMBOL(drm_framebuffer_unreference);
332
333/**
334 * drm_framebuffer_reference - incr the fb refcnt
335 */
336void drm_framebuffer_reference(struct drm_framebuffer *fb)
337{
338 DRM_DEBUG("FB ID: %d\n", fb->base.id);
339 kref_get(&fb->refcount);
340}
341EXPORT_SYMBOL(drm_framebuffer_reference);
342
Dave Airlief453ba02008-11-07 14:05:41 -0800343/**
344 * drm_framebuffer_cleanup - remove a framebuffer object
345 * @fb: framebuffer to remove
346 *
347 * LOCKING:
348 * Caller must hold mode config lock.
349 *
350 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
351 * it, setting it to NULL.
352 */
353void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
354{
355 struct drm_device *dev = fb->dev;
Rob Clarkf7eff602012-09-05 21:48:38 +0000356 /*
357 * This could be moved to drm_framebuffer_remove(), but for
358 * debugging is nice to keep around the list of fb's that are
359 * no longer associated w/ a drm_file but are not unreferenced
360 * yet. (i915 and omapdrm have debugfs files which will show
361 * this.)
362 */
363 drm_mode_object_put(dev, &fb->base);
364 list_del(&fb->head);
365 dev->mode_config.num_fb--;
366}
367EXPORT_SYMBOL(drm_framebuffer_cleanup);
368
369/**
370 * drm_framebuffer_remove - remove and unreference a framebuffer object
371 * @fb: framebuffer to remove
372 *
373 * LOCKING:
374 * Caller must hold mode config lock.
375 *
376 * Scans all the CRTCs and planes in @dev's mode_config. If they're
377 * using @fb, removes it, setting it to NULL.
378 */
379void drm_framebuffer_remove(struct drm_framebuffer *fb)
380{
381 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800382 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800383 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000384 struct drm_mode_set set;
385 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800386
387 /* remove from any CRTC */
388 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000389 if (crtc->fb == fb) {
390 /* should turn off the crtc */
391 memset(&set, 0, sizeof(struct drm_mode_set));
392 set.crtc = crtc;
393 set.fb = NULL;
394 ret = crtc->funcs->set_config(&set);
395 if (ret)
396 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
397 }
Dave Airlief453ba02008-11-07 14:05:41 -0800398 }
399
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800400 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
401 if (plane->fb == fb) {
402 /* should turn off the crtc */
403 ret = plane->funcs->disable_plane(plane);
404 if (ret)
405 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600406 /* disconnect the plane from the fb and crtc: */
407 plane->fb = NULL;
408 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800409 }
410 }
411
Rob Clarkf7eff602012-09-05 21:48:38 +0000412 list_del(&fb->filp_head);
413
414 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800415}
Rob Clarkf7eff602012-09-05 21:48:38 +0000416EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800417
418/**
419 * drm_crtc_init - Initialise a new CRTC object
420 * @dev: DRM device
421 * @crtc: CRTC object to init
422 * @funcs: callbacks for the new CRTC
423 *
424 * LOCKING:
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200425 * Takes mode_config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800426 *
427 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200428 *
429 * RETURNS:
430 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800431 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200432int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800433 const struct drm_crtc_funcs *funcs)
434{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200435 int ret;
436
Dave Airlief453ba02008-11-07 14:05:41 -0800437 crtc->dev = dev;
438 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000439 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800440
441 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200442
443 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
444 if (ret)
445 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800446
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300447 crtc->base.properties = &crtc->properties;
448
Dave Airlief453ba02008-11-07 14:05:41 -0800449 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
450 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200451
452 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800453 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200454
455 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800456}
457EXPORT_SYMBOL(drm_crtc_init);
458
459/**
460 * drm_crtc_cleanup - Cleans up the core crtc usage.
461 * @crtc: CRTC to cleanup
462 *
463 * LOCKING:
464 * Caller must hold mode config lock.
465 *
466 * Cleanup @crtc. Removes from drm modesetting space
467 * does NOT free object, caller does that.
468 */
469void drm_crtc_cleanup(struct drm_crtc *crtc)
470{
471 struct drm_device *dev = crtc->dev;
472
473 if (crtc->gamma_store) {
474 kfree(crtc->gamma_store);
475 crtc->gamma_store = NULL;
476 }
477
478 drm_mode_object_put(dev, &crtc->base);
479 list_del(&crtc->head);
480 dev->mode_config.num_crtc--;
481}
482EXPORT_SYMBOL(drm_crtc_cleanup);
483
484/**
485 * drm_mode_probed_add - add a mode to a connector's probed mode list
486 * @connector: connector the new mode
487 * @mode: mode data
488 *
489 * LOCKING:
490 * Caller must hold mode config lock.
491 *
492 * Add @mode to @connector's mode list for later use.
493 */
494void drm_mode_probed_add(struct drm_connector *connector,
495 struct drm_display_mode *mode)
496{
497 list_add(&mode->head, &connector->probed_modes);
498}
499EXPORT_SYMBOL(drm_mode_probed_add);
500
501/**
502 * drm_mode_remove - remove and free a mode
503 * @connector: connector list to modify
504 * @mode: mode to remove
505 *
506 * LOCKING:
507 * Caller must hold mode config lock.
508 *
509 * Remove @mode from @connector's mode list, then free it.
510 */
511void drm_mode_remove(struct drm_connector *connector,
512 struct drm_display_mode *mode)
513{
514 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100515 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800516}
517EXPORT_SYMBOL(drm_mode_remove);
518
519/**
520 * drm_connector_init - Init a preallocated connector
521 * @dev: DRM device
522 * @connector: the connector to init
523 * @funcs: callbacks for this connector
524 * @name: user visible name of the connector
525 *
526 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100527 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800528 *
529 * Initialises a preallocated connector. Connectors should be
530 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200531 *
532 * RETURNS:
533 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800534 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200535int drm_connector_init(struct drm_device *dev,
536 struct drm_connector *connector,
537 const struct drm_connector_funcs *funcs,
538 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800539{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200540 int ret;
541
Dave Airlief453ba02008-11-07 14:05:41 -0800542 mutex_lock(&dev->mode_config.mutex);
543
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200544 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
545 if (ret)
546 goto out;
547
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300548 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800549 connector->dev = dev;
550 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800551 connector->connector_type = connector_type;
552 connector->connector_type_id =
553 ++drm_connector_enum_list[connector_type].count; /* TODO */
554 INIT_LIST_HEAD(&connector->user_modes);
555 INIT_LIST_HEAD(&connector->probed_modes);
556 INIT_LIST_HEAD(&connector->modes);
557 connector->edid_blob_ptr = NULL;
558
559 list_add_tail(&connector->head, &dev->mode_config.connector_list);
560 dev->mode_config.num_connector++;
561
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200562 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
563 drm_connector_attach_property(connector,
564 dev->mode_config.edid_property,
565 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800566
567 drm_connector_attach_property(connector,
568 dev->mode_config.dpms_property, 0);
569
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200570 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800571 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200572
573 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800574}
575EXPORT_SYMBOL(drm_connector_init);
576
577/**
578 * drm_connector_cleanup - cleans up an initialised connector
579 * @connector: connector to cleanup
580 *
581 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100582 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800583 *
584 * Cleans up the connector but doesn't free the object.
585 */
586void drm_connector_cleanup(struct drm_connector *connector)
587{
588 struct drm_device *dev = connector->dev;
589 struct drm_display_mode *mode, *t;
590
591 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
592 drm_mode_remove(connector, mode);
593
594 list_for_each_entry_safe(mode, t, &connector->modes, head)
595 drm_mode_remove(connector, mode);
596
597 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
598 drm_mode_remove(connector, mode);
599
600 mutex_lock(&dev->mode_config.mutex);
601 drm_mode_object_put(dev, &connector->base);
602 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000603 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800604 mutex_unlock(&dev->mode_config.mutex);
605}
606EXPORT_SYMBOL(drm_connector_cleanup);
607
Dave Airliecbc7e222012-02-20 14:16:40 +0000608void drm_connector_unplug_all(struct drm_device *dev)
609{
610 struct drm_connector *connector;
611
612 /* taking the mode config mutex ends up in a clash with sysfs */
613 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
614 drm_sysfs_connector_remove(connector);
615
616}
617EXPORT_SYMBOL(drm_connector_unplug_all);
618
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200619int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000620 struct drm_encoder *encoder,
621 const struct drm_encoder_funcs *funcs,
622 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800623{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200624 int ret;
625
Dave Airlief453ba02008-11-07 14:05:41 -0800626 mutex_lock(&dev->mode_config.mutex);
627
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200628 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
629 if (ret)
630 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800631
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200632 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800633 encoder->encoder_type = encoder_type;
634 encoder->funcs = funcs;
635
636 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
637 dev->mode_config.num_encoder++;
638
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200639 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800640 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200641
642 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800643}
644EXPORT_SYMBOL(drm_encoder_init);
645
646void drm_encoder_cleanup(struct drm_encoder *encoder)
647{
648 struct drm_device *dev = encoder->dev;
649 mutex_lock(&dev->mode_config.mutex);
650 drm_mode_object_put(dev, &encoder->base);
651 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000652 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800653 mutex_unlock(&dev->mode_config.mutex);
654}
655EXPORT_SYMBOL(drm_encoder_cleanup);
656
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800657int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
658 unsigned long possible_crtcs,
659 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600660 const uint32_t *formats, uint32_t format_count,
661 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800662{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200663 int ret;
664
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800665 mutex_lock(&dev->mode_config.mutex);
666
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200667 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
668 if (ret)
669 goto out;
670
Rob Clark4d939142012-05-17 02:23:27 -0600671 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800672 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800673 plane->funcs = funcs;
674 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
675 GFP_KERNEL);
676 if (!plane->format_types) {
677 DRM_DEBUG_KMS("out of memory when allocating plane\n");
678 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200679 ret = -ENOMEM;
680 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800681 }
682
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800683 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800684 plane->format_count = format_count;
685 plane->possible_crtcs = possible_crtcs;
686
Rob Clark0a7eb242011-12-13 20:19:36 -0600687 /* private planes are not exposed to userspace, but depending on
688 * display hardware, might be convenient to allow sharing programming
689 * for the scanout engine with the crtc implementation.
690 */
691 if (!priv) {
692 list_add_tail(&plane->head, &dev->mode_config.plane_list);
693 dev->mode_config.num_plane++;
694 } else {
695 INIT_LIST_HEAD(&plane->head);
696 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800697
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200698 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800699 mutex_unlock(&dev->mode_config.mutex);
700
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200701 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800702}
703EXPORT_SYMBOL(drm_plane_init);
704
705void drm_plane_cleanup(struct drm_plane *plane)
706{
707 struct drm_device *dev = plane->dev;
708
709 mutex_lock(&dev->mode_config.mutex);
710 kfree(plane->format_types);
711 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600712 /* if not added to a list, it must be a private plane */
713 if (!list_empty(&plane->head)) {
714 list_del(&plane->head);
715 dev->mode_config.num_plane--;
716 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800717 mutex_unlock(&dev->mode_config.mutex);
718}
719EXPORT_SYMBOL(drm_plane_cleanup);
720
Dave Airlief453ba02008-11-07 14:05:41 -0800721/**
722 * drm_mode_create - create a new display mode
723 * @dev: DRM device
724 *
725 * LOCKING:
726 * Caller must hold DRM mode_config lock.
727 *
728 * Create a new drm_display_mode, give it an ID, and return it.
729 *
730 * RETURNS:
731 * Pointer to new mode on success, NULL on error.
732 */
733struct drm_display_mode *drm_mode_create(struct drm_device *dev)
734{
735 struct drm_display_mode *nmode;
736
737 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
738 if (!nmode)
739 return NULL;
740
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200741 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
742 kfree(nmode);
743 return NULL;
744 }
745
Dave Airlief453ba02008-11-07 14:05:41 -0800746 return nmode;
747}
748EXPORT_SYMBOL(drm_mode_create);
749
750/**
751 * drm_mode_destroy - remove a mode
752 * @dev: DRM device
753 * @mode: mode to remove
754 *
755 * LOCKING:
756 * Caller must hold mode config lock.
757 *
758 * Free @mode's unique identifier, then free it.
759 */
760void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
761{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200762 if (!mode)
763 return;
764
Dave Airlief453ba02008-11-07 14:05:41 -0800765 drm_mode_object_put(dev, &mode->base);
766
767 kfree(mode);
768}
769EXPORT_SYMBOL(drm_mode_destroy);
770
771static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
772{
773 struct drm_property *edid;
774 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800775
776 /*
777 * Standard properties (apply to all connectors)
778 */
779 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
780 DRM_MODE_PROP_IMMUTABLE,
781 "EDID", 0);
782 dev->mode_config.edid_property = edid;
783
Sascha Hauer4a67d392012-02-06 10:58:17 +0100784 dpms = drm_property_create_enum(dev, 0,
785 "DPMS", drm_dpms_enum_list,
786 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800787 dev->mode_config.dpms_property = dpms;
788
789 return 0;
790}
791
792/**
793 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
794 * @dev: DRM device
795 *
796 * Called by a driver the first time a DVI-I connector is made.
797 */
798int drm_mode_create_dvi_i_properties(struct drm_device *dev)
799{
800 struct drm_property *dvi_i_selector;
801 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800802
803 if (dev->mode_config.dvi_i_select_subconnector_property)
804 return 0;
805
806 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100807 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800808 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100809 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800810 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800811 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
812
Sascha Hauer4a67d392012-02-06 10:58:17 +0100813 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800814 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100815 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800816 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800817 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
818
819 return 0;
820}
821EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
822
823/**
824 * drm_create_tv_properties - create TV specific connector properties
825 * @dev: DRM device
826 * @num_modes: number of different TV formats (modes) supported
827 * @modes: array of pointers to strings containing name of each format
828 *
829 * Called by a driver's TV initialization routine, this function creates
830 * the TV specific connector properties for a given device. Caller is
831 * responsible for allocating a list of format names and passing them to
832 * this routine.
833 */
834int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
835 char *modes[])
836{
837 struct drm_property *tv_selector;
838 struct drm_property *tv_subconnector;
839 int i;
840
841 if (dev->mode_config.tv_select_subconnector_property)
842 return 0;
843
844 /*
845 * Basic connector properties
846 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100847 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800848 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100849 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800850 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800851 dev->mode_config.tv_select_subconnector_property = tv_selector;
852
853 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100854 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
855 "subconnector",
856 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800857 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800858 dev->mode_config.tv_subconnector_property = tv_subconnector;
859
860 /*
861 * Other, TV specific properties: margins & TV modes.
862 */
863 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100864 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800865
866 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100867 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800868
869 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100870 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800871
872 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100873 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800874
875 dev->mode_config.tv_mode_property =
876 drm_property_create(dev, DRM_MODE_PROP_ENUM,
877 "mode", num_modes);
878 for (i = 0; i < num_modes; i++)
879 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
880 i, modes[i]);
881
Francisco Jerezb6b79022009-08-02 04:19:20 +0200882 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100883 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200884
885 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100886 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200887
888 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100889 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200890
Francisco Jereza75f0232009-08-12 02:30:10 +0200891 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100892 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200893
894 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100895 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200896
897 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100898 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200899
Dave Airlief453ba02008-11-07 14:05:41 -0800900 return 0;
901}
902EXPORT_SYMBOL(drm_mode_create_tv_properties);
903
904/**
905 * drm_mode_create_scaling_mode_property - create scaling mode property
906 * @dev: DRM device
907 *
908 * Called by a driver the first time it's needed, must be attached to desired
909 * connectors.
910 */
911int drm_mode_create_scaling_mode_property(struct drm_device *dev)
912{
913 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800914
915 if (dev->mode_config.scaling_mode_property)
916 return 0;
917
918 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100919 drm_property_create_enum(dev, 0, "scaling mode",
920 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800921 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800922
923 dev->mode_config.scaling_mode_property = scaling_mode;
924
925 return 0;
926}
927EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
928
929/**
930 * drm_mode_create_dithering_property - create dithering property
931 * @dev: DRM device
932 *
933 * Called by a driver the first time it's needed, must be attached to desired
934 * connectors.
935 */
936int drm_mode_create_dithering_property(struct drm_device *dev)
937{
938 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800939
940 if (dev->mode_config.dithering_mode_property)
941 return 0;
942
943 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100944 drm_property_create_enum(dev, 0, "dithering",
945 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800946 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800947 dev->mode_config.dithering_mode_property = dithering_mode;
948
949 return 0;
950}
951EXPORT_SYMBOL(drm_mode_create_dithering_property);
952
953/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000954 * drm_mode_create_dirty_property - create dirty property
955 * @dev: DRM device
956 *
957 * Called by a driver the first time it's needed, must be attached to desired
958 * connectors.
959 */
960int drm_mode_create_dirty_info_property(struct drm_device *dev)
961{
962 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000963
964 if (dev->mode_config.dirty_info_property)
965 return 0;
966
967 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100968 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000969 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100970 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000971 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000972 dev->mode_config.dirty_info_property = dirty_info;
973
974 return 0;
975}
976EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
977
978/**
Dave Airlief453ba02008-11-07 14:05:41 -0800979 * drm_mode_config_init - initialize DRM mode_configuration structure
980 * @dev: DRM device
981 *
982 * LOCKING:
983 * None, should happen single threaded at init time.
984 *
985 * Initialize @dev's mode_config structure, used for tracking the graphics
986 * configuration of @dev.
987 */
988void drm_mode_config_init(struct drm_device *dev)
989{
990 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000991 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800992 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800993 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
994 INIT_LIST_HEAD(&dev->mode_config.connector_list);
995 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
996 INIT_LIST_HEAD(&dev->mode_config.property_list);
997 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800998 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800999 idr_init(&dev->mode_config.crtc_idr);
1000
1001 mutex_lock(&dev->mode_config.mutex);
1002 drm_mode_create_standard_connector_properties(dev);
1003 mutex_unlock(&dev->mode_config.mutex);
1004
1005 /* Just to be sure */
1006 dev->mode_config.num_fb = 0;
1007 dev->mode_config.num_connector = 0;
1008 dev->mode_config.num_crtc = 0;
1009 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001010}
1011EXPORT_SYMBOL(drm_mode_config_init);
1012
1013int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1014{
1015 uint32_t total_objects = 0;
1016
1017 total_objects += dev->mode_config.num_crtc;
1018 total_objects += dev->mode_config.num_connector;
1019 total_objects += dev->mode_config.num_encoder;
1020
Dave Airlief453ba02008-11-07 14:05:41 -08001021 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1022 if (!group->id_list)
1023 return -ENOMEM;
1024
1025 group->num_crtcs = 0;
1026 group->num_connectors = 0;
1027 group->num_encoders = 0;
1028 return 0;
1029}
1030
1031int drm_mode_group_init_legacy_group(struct drm_device *dev,
1032 struct drm_mode_group *group)
1033{
1034 struct drm_crtc *crtc;
1035 struct drm_encoder *encoder;
1036 struct drm_connector *connector;
1037 int ret;
1038
1039 if ((ret = drm_mode_group_init(dev, group)))
1040 return ret;
1041
1042 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1043 group->id_list[group->num_crtcs++] = crtc->base.id;
1044
1045 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1046 group->id_list[group->num_crtcs + group->num_encoders++] =
1047 encoder->base.id;
1048
1049 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1050 group->id_list[group->num_crtcs + group->num_encoders +
1051 group->num_connectors++] = connector->base.id;
1052
1053 return 0;
1054}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001055EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001056
1057/**
1058 * drm_mode_config_cleanup - free up DRM mode_config info
1059 * @dev: DRM device
1060 *
1061 * LOCKING:
1062 * Caller must hold mode config lock.
1063 *
1064 * Free up all the connectors and CRTCs associated with this DRM device, then
1065 * free up the framebuffers and associated buffer objects.
1066 *
1067 * FIXME: cleanup any dangling user buffer objects too
1068 */
1069void drm_mode_config_cleanup(struct drm_device *dev)
1070{
1071 struct drm_connector *connector, *ot;
1072 struct drm_crtc *crtc, *ct;
1073 struct drm_encoder *encoder, *enct;
1074 struct drm_framebuffer *fb, *fbt;
1075 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001076 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001077
1078 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1079 head) {
1080 encoder->funcs->destroy(encoder);
1081 }
1082
1083 list_for_each_entry_safe(connector, ot,
1084 &dev->mode_config.connector_list, head) {
1085 connector->funcs->destroy(connector);
1086 }
1087
1088 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1089 head) {
1090 drm_property_destroy(dev, property);
1091 }
1092
1093 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001094 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001095 }
1096
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001097 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1098 head) {
1099 plane->funcs->destroy(plane);
1100 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001101
Chris Wilson31840092012-09-17 09:38:03 +00001102 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1103 crtc->funcs->destroy(crtc);
1104 }
1105
Sascha Hauer59ce0622012-02-01 11:38:20 +01001106 idr_remove_all(&dev->mode_config.crtc_idr);
1107 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001108}
1109EXPORT_SYMBOL(drm_mode_config_cleanup);
1110
Dave Airlief453ba02008-11-07 14:05:41 -08001111/**
1112 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1113 * @out: drm_mode_modeinfo struct to return to the user
1114 * @in: drm_display_mode to use
1115 *
1116 * LOCKING:
1117 * None.
1118 *
1119 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1120 * the user.
1121 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001122static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1123 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001124{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001125 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1126 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1127 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1128 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1129 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1130 "timing values too large for mode info\n");
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;
1148}
1149
1150/**
1151 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1152 * @out: drm_display_mode to return to the user
1153 * @in: drm_mode_modeinfo to use
1154 *
1155 * LOCKING:
1156 * None.
1157 *
1158 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1159 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001160 *
1161 * RETURNS:
1162 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001163 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001164static int drm_crtc_convert_umode(struct drm_display_mode *out,
1165 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001166{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001167 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1168 return -ERANGE;
1169
Dave Airlief453ba02008-11-07 14:05:41 -08001170 out->clock = in->clock;
1171 out->hdisplay = in->hdisplay;
1172 out->hsync_start = in->hsync_start;
1173 out->hsync_end = in->hsync_end;
1174 out->htotal = in->htotal;
1175 out->hskew = in->hskew;
1176 out->vdisplay = in->vdisplay;
1177 out->vsync_start = in->vsync_start;
1178 out->vsync_end = in->vsync_end;
1179 out->vtotal = in->vtotal;
1180 out->vscan = in->vscan;
1181 out->vrefresh = in->vrefresh;
1182 out->flags = in->flags;
1183 out->type = in->type;
1184 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1185 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001186
1187 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001188}
1189
1190/**
1191 * drm_mode_getresources - get graphics configuration
1192 * @inode: inode from the ioctl
1193 * @filp: file * from the ioctl
1194 * @cmd: cmd from ioctl
1195 * @arg: arg from ioctl
1196 *
1197 * LOCKING:
1198 * Takes mode config lock.
1199 *
1200 * Construct a set of configuration description structures and return
1201 * them to the user, including CRTC, connector and framebuffer configuration.
1202 *
1203 * Called by the user via ioctl.
1204 *
1205 * RETURNS:
1206 * Zero on success, errno on failure.
1207 */
1208int drm_mode_getresources(struct drm_device *dev, void *data,
1209 struct drm_file *file_priv)
1210{
1211 struct drm_mode_card_res *card_res = data;
1212 struct list_head *lh;
1213 struct drm_framebuffer *fb;
1214 struct drm_connector *connector;
1215 struct drm_crtc *crtc;
1216 struct drm_encoder *encoder;
1217 int ret = 0;
1218 int connector_count = 0;
1219 int crtc_count = 0;
1220 int fb_count = 0;
1221 int encoder_count = 0;
1222 int copied = 0, i;
1223 uint32_t __user *fb_id;
1224 uint32_t __user *crtc_id;
1225 uint32_t __user *connector_id;
1226 uint32_t __user *encoder_id;
1227 struct drm_mode_group *mode_group;
1228
Dave Airliefb3b06c2011-02-08 13:55:21 +10001229 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1230 return -EINVAL;
1231
Dave Airlief453ba02008-11-07 14:05:41 -08001232 mutex_lock(&dev->mode_config.mutex);
1233
1234 /*
1235 * For the non-control nodes we need to limit the list of resources
1236 * by IDs in the group list for this node
1237 */
1238 list_for_each(lh, &file_priv->fbs)
1239 fb_count++;
1240
1241 mode_group = &file_priv->master->minor->mode_group;
1242 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1243
1244 list_for_each(lh, &dev->mode_config.crtc_list)
1245 crtc_count++;
1246
1247 list_for_each(lh, &dev->mode_config.connector_list)
1248 connector_count++;
1249
1250 list_for_each(lh, &dev->mode_config.encoder_list)
1251 encoder_count++;
1252 } else {
1253
1254 crtc_count = mode_group->num_crtcs;
1255 connector_count = mode_group->num_connectors;
1256 encoder_count = mode_group->num_encoders;
1257 }
1258
1259 card_res->max_height = dev->mode_config.max_height;
1260 card_res->min_height = dev->mode_config.min_height;
1261 card_res->max_width = dev->mode_config.max_width;
1262 card_res->min_width = dev->mode_config.min_width;
1263
1264 /* handle this in 4 parts */
1265 /* FBs */
1266 if (card_res->count_fbs >= fb_count) {
1267 copied = 0;
1268 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001269 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001270 if (put_user(fb->base.id, fb_id + copied)) {
1271 ret = -EFAULT;
1272 goto out;
1273 }
1274 copied++;
1275 }
1276 }
1277 card_res->count_fbs = fb_count;
1278
1279 /* CRTCs */
1280 if (card_res->count_crtcs >= crtc_count) {
1281 copied = 0;
1282 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1283 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1284 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1285 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001286 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001287 if (put_user(crtc->base.id, crtc_id + copied)) {
1288 ret = -EFAULT;
1289 goto out;
1290 }
1291 copied++;
1292 }
1293 } else {
1294 for (i = 0; i < mode_group->num_crtcs; i++) {
1295 if (put_user(mode_group->id_list[i],
1296 crtc_id + copied)) {
1297 ret = -EFAULT;
1298 goto out;
1299 }
1300 copied++;
1301 }
1302 }
1303 }
1304 card_res->count_crtcs = crtc_count;
1305
1306 /* Encoders */
1307 if (card_res->count_encoders >= encoder_count) {
1308 copied = 0;
1309 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1310 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1311 list_for_each_entry(encoder,
1312 &dev->mode_config.encoder_list,
1313 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001314 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1315 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001316 if (put_user(encoder->base.id, encoder_id +
1317 copied)) {
1318 ret = -EFAULT;
1319 goto out;
1320 }
1321 copied++;
1322 }
1323 } else {
1324 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1325 if (put_user(mode_group->id_list[i],
1326 encoder_id + copied)) {
1327 ret = -EFAULT;
1328 goto out;
1329 }
1330 copied++;
1331 }
1332
1333 }
1334 }
1335 card_res->count_encoders = encoder_count;
1336
1337 /* Connectors */
1338 if (card_res->count_connectors >= connector_count) {
1339 copied = 0;
1340 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1341 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1342 list_for_each_entry(connector,
1343 &dev->mode_config.connector_list,
1344 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001345 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1346 connector->base.id,
1347 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001348 if (put_user(connector->base.id,
1349 connector_id + copied)) {
1350 ret = -EFAULT;
1351 goto out;
1352 }
1353 copied++;
1354 }
1355 } else {
1356 int start = mode_group->num_crtcs +
1357 mode_group->num_encoders;
1358 for (i = start; i < start + mode_group->num_connectors; i++) {
1359 if (put_user(mode_group->id_list[i],
1360 connector_id + copied)) {
1361 ret = -EFAULT;
1362 goto out;
1363 }
1364 copied++;
1365 }
1366 }
1367 }
1368 card_res->count_connectors = connector_count;
1369
Jerome Glisse94401062010-07-15 15:43:25 -04001370 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001371 card_res->count_connectors, card_res->count_encoders);
1372
1373out:
1374 mutex_unlock(&dev->mode_config.mutex);
1375 return ret;
1376}
1377
1378/**
1379 * drm_mode_getcrtc - get CRTC configuration
1380 * @inode: inode from the ioctl
1381 * @filp: file * from the ioctl
1382 * @cmd: cmd from ioctl
1383 * @arg: arg from ioctl
1384 *
1385 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001386 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001387 *
1388 * Construct a CRTC configuration structure to return to the user.
1389 *
1390 * Called by the user via ioctl.
1391 *
1392 * RETURNS:
1393 * Zero on success, errno on failure.
1394 */
1395int drm_mode_getcrtc(struct drm_device *dev,
1396 void *data, struct drm_file *file_priv)
1397{
1398 struct drm_mode_crtc *crtc_resp = data;
1399 struct drm_crtc *crtc;
1400 struct drm_mode_object *obj;
1401 int ret = 0;
1402
Dave Airliefb3b06c2011-02-08 13:55:21 +10001403 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1404 return -EINVAL;
1405
Dave Airlief453ba02008-11-07 14:05:41 -08001406 mutex_lock(&dev->mode_config.mutex);
1407
1408 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1409 DRM_MODE_OBJECT_CRTC);
1410 if (!obj) {
1411 ret = -EINVAL;
1412 goto out;
1413 }
1414 crtc = obj_to_crtc(obj);
1415
1416 crtc_resp->x = crtc->x;
1417 crtc_resp->y = crtc->y;
1418 crtc_resp->gamma_size = crtc->gamma_size;
1419 if (crtc->fb)
1420 crtc_resp->fb_id = crtc->fb->base.id;
1421 else
1422 crtc_resp->fb_id = 0;
1423
1424 if (crtc->enabled) {
1425
1426 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1427 crtc_resp->mode_valid = 1;
1428
1429 } else {
1430 crtc_resp->mode_valid = 0;
1431 }
1432
1433out:
1434 mutex_unlock(&dev->mode_config.mutex);
1435 return ret;
1436}
1437
1438/**
1439 * drm_mode_getconnector - get connector configuration
1440 * @inode: inode from the ioctl
1441 * @filp: file * from the ioctl
1442 * @cmd: cmd from ioctl
1443 * @arg: arg from ioctl
1444 *
1445 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001446 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001447 *
1448 * Construct a connector configuration structure to return to the user.
1449 *
1450 * Called by the user via ioctl.
1451 *
1452 * RETURNS:
1453 * Zero on success, errno on failure.
1454 */
1455int drm_mode_getconnector(struct drm_device *dev, void *data,
1456 struct drm_file *file_priv)
1457{
1458 struct drm_mode_get_connector *out_resp = data;
1459 struct drm_mode_object *obj;
1460 struct drm_connector *connector;
1461 struct drm_display_mode *mode;
1462 int mode_count = 0;
1463 int props_count = 0;
1464 int encoders_count = 0;
1465 int ret = 0;
1466 int copied = 0;
1467 int i;
1468 struct drm_mode_modeinfo u_mode;
1469 struct drm_mode_modeinfo __user *mode_ptr;
1470 uint32_t __user *prop_ptr;
1471 uint64_t __user *prop_values;
1472 uint32_t __user *encoder_ptr;
1473
Dave Airliefb3b06c2011-02-08 13:55:21 +10001474 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1475 return -EINVAL;
1476
Dave Airlief453ba02008-11-07 14:05:41 -08001477 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1478
Jerome Glisse94401062010-07-15 15:43:25 -04001479 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001480
1481 mutex_lock(&dev->mode_config.mutex);
1482
1483 obj = drm_mode_object_find(dev, out_resp->connector_id,
1484 DRM_MODE_OBJECT_CONNECTOR);
1485 if (!obj) {
1486 ret = -EINVAL;
1487 goto out;
1488 }
1489 connector = obj_to_connector(obj);
1490
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001491 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001492
1493 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1494 if (connector->encoder_ids[i] != 0) {
1495 encoders_count++;
1496 }
1497 }
1498
1499 if (out_resp->count_modes == 0) {
1500 connector->funcs->fill_modes(connector,
1501 dev->mode_config.max_width,
1502 dev->mode_config.max_height);
1503 }
1504
1505 /* delayed so we get modes regardless of pre-fill_modes state */
1506 list_for_each_entry(mode, &connector->modes, head)
1507 mode_count++;
1508
1509 out_resp->connector_id = connector->base.id;
1510 out_resp->connector_type = connector->connector_type;
1511 out_resp->connector_type_id = connector->connector_type_id;
1512 out_resp->mm_width = connector->display_info.width_mm;
1513 out_resp->mm_height = connector->display_info.height_mm;
1514 out_resp->subpixel = connector->display_info.subpixel_order;
1515 out_resp->connection = connector->status;
1516 if (connector->encoder)
1517 out_resp->encoder_id = connector->encoder->base.id;
1518 else
1519 out_resp->encoder_id = 0;
1520
1521 /*
1522 * This ioctl is called twice, once to determine how much space is
1523 * needed, and the 2nd time to fill it.
1524 */
1525 if ((out_resp->count_modes >= mode_count) && mode_count) {
1526 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001527 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001528 list_for_each_entry(mode, &connector->modes, head) {
1529 drm_crtc_convert_to_umode(&u_mode, mode);
1530 if (copy_to_user(mode_ptr + copied,
1531 &u_mode, sizeof(u_mode))) {
1532 ret = -EFAULT;
1533 goto out;
1534 }
1535 copied++;
1536 }
1537 }
1538 out_resp->count_modes = mode_count;
1539
1540 if ((out_resp->count_props >= props_count) && props_count) {
1541 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001542 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1543 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001544 for (i = 0; i < connector->properties.count; i++) {
1545 if (put_user(connector->properties.ids[i],
1546 prop_ptr + copied)) {
1547 ret = -EFAULT;
1548 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001549 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001550
1551 if (put_user(connector->properties.values[i],
1552 prop_values + copied)) {
1553 ret = -EFAULT;
1554 goto out;
1555 }
1556 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001557 }
1558 }
1559 out_resp->count_props = props_count;
1560
1561 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1562 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001563 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001564 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1565 if (connector->encoder_ids[i] != 0) {
1566 if (put_user(connector->encoder_ids[i],
1567 encoder_ptr + copied)) {
1568 ret = -EFAULT;
1569 goto out;
1570 }
1571 copied++;
1572 }
1573 }
1574 }
1575 out_resp->count_encoders = encoders_count;
1576
1577out:
1578 mutex_unlock(&dev->mode_config.mutex);
1579 return ret;
1580}
1581
1582int drm_mode_getencoder(struct drm_device *dev, void *data,
1583 struct drm_file *file_priv)
1584{
1585 struct drm_mode_get_encoder *enc_resp = data;
1586 struct drm_mode_object *obj;
1587 struct drm_encoder *encoder;
1588 int ret = 0;
1589
Dave Airliefb3b06c2011-02-08 13:55:21 +10001590 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1591 return -EINVAL;
1592
Dave Airlief453ba02008-11-07 14:05:41 -08001593 mutex_lock(&dev->mode_config.mutex);
1594 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1595 DRM_MODE_OBJECT_ENCODER);
1596 if (!obj) {
1597 ret = -EINVAL;
1598 goto out;
1599 }
1600 encoder = obj_to_encoder(obj);
1601
1602 if (encoder->crtc)
1603 enc_resp->crtc_id = encoder->crtc->base.id;
1604 else
1605 enc_resp->crtc_id = 0;
1606 enc_resp->encoder_type = encoder->encoder_type;
1607 enc_resp->encoder_id = encoder->base.id;
1608 enc_resp->possible_crtcs = encoder->possible_crtcs;
1609 enc_resp->possible_clones = encoder->possible_clones;
1610
1611out:
1612 mutex_unlock(&dev->mode_config.mutex);
1613 return ret;
1614}
1615
1616/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001617 * drm_mode_getplane_res - get plane info
1618 * @dev: DRM device
1619 * @data: ioctl data
1620 * @file_priv: DRM file info
1621 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001622 * LOCKING:
1623 * Takes mode config lock.
1624 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001625 * Return an plane count and set of IDs.
1626 */
1627int drm_mode_getplane_res(struct drm_device *dev, void *data,
1628 struct drm_file *file_priv)
1629{
1630 struct drm_mode_get_plane_res *plane_resp = data;
1631 struct drm_mode_config *config;
1632 struct drm_plane *plane;
1633 uint32_t __user *plane_ptr;
1634 int copied = 0, ret = 0;
1635
1636 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1637 return -EINVAL;
1638
1639 mutex_lock(&dev->mode_config.mutex);
1640 config = &dev->mode_config;
1641
1642 /*
1643 * This ioctl is called twice, once to determine how much space is
1644 * needed, and the 2nd time to fill it.
1645 */
1646 if (config->num_plane &&
1647 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001648 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001649
1650 list_for_each_entry(plane, &config->plane_list, head) {
1651 if (put_user(plane->base.id, plane_ptr + copied)) {
1652 ret = -EFAULT;
1653 goto out;
1654 }
1655 copied++;
1656 }
1657 }
1658 plane_resp->count_planes = config->num_plane;
1659
1660out:
1661 mutex_unlock(&dev->mode_config.mutex);
1662 return ret;
1663}
1664
1665/**
1666 * drm_mode_getplane - get plane info
1667 * @dev: DRM device
1668 * @data: ioctl data
1669 * @file_priv: DRM file info
1670 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001671 * LOCKING:
1672 * Takes mode config lock.
1673 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001674 * Return plane info, including formats supported, gamma size, any
1675 * current fb, etc.
1676 */
1677int drm_mode_getplane(struct drm_device *dev, void *data,
1678 struct drm_file *file_priv)
1679{
1680 struct drm_mode_get_plane *plane_resp = data;
1681 struct drm_mode_object *obj;
1682 struct drm_plane *plane;
1683 uint32_t __user *format_ptr;
1684 int ret = 0;
1685
1686 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1687 return -EINVAL;
1688
1689 mutex_lock(&dev->mode_config.mutex);
1690 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1691 DRM_MODE_OBJECT_PLANE);
1692 if (!obj) {
1693 ret = -ENOENT;
1694 goto out;
1695 }
1696 plane = obj_to_plane(obj);
1697
1698 if (plane->crtc)
1699 plane_resp->crtc_id = plane->crtc->base.id;
1700 else
1701 plane_resp->crtc_id = 0;
1702
1703 if (plane->fb)
1704 plane_resp->fb_id = plane->fb->base.id;
1705 else
1706 plane_resp->fb_id = 0;
1707
1708 plane_resp->plane_id = plane->base.id;
1709 plane_resp->possible_crtcs = plane->possible_crtcs;
1710 plane_resp->gamma_size = plane->gamma_size;
1711
1712 /*
1713 * This ioctl is called twice, once to determine how much space is
1714 * needed, and the 2nd time to fill it.
1715 */
1716 if (plane->format_count &&
1717 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001718 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001719 if (copy_to_user(format_ptr,
1720 plane->format_types,
1721 sizeof(uint32_t) * plane->format_count)) {
1722 ret = -EFAULT;
1723 goto out;
1724 }
1725 }
1726 plane_resp->count_format_types = plane->format_count;
1727
1728out:
1729 mutex_unlock(&dev->mode_config.mutex);
1730 return ret;
1731}
1732
1733/**
1734 * drm_mode_setplane - set up or tear down an plane
1735 * @dev: DRM device
1736 * @data: ioctl data*
1737 * @file_prive: DRM file info
1738 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001739 * LOCKING:
1740 * Takes mode config lock.
1741 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001742 * Set plane info, including placement, fb, scaling, and other factors.
1743 * Or pass a NULL fb to disable.
1744 */
1745int drm_mode_setplane(struct drm_device *dev, void *data,
1746 struct drm_file *file_priv)
1747{
1748 struct drm_mode_set_plane *plane_req = data;
1749 struct drm_mode_object *obj;
1750 struct drm_plane *plane;
1751 struct drm_crtc *crtc;
1752 struct drm_framebuffer *fb;
1753 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001754 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001755 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001756
1757 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1758 return -EINVAL;
1759
1760 mutex_lock(&dev->mode_config.mutex);
1761
1762 /*
1763 * First, find the plane, crtc, and fb objects. If not available,
1764 * we don't bother to call the driver.
1765 */
1766 obj = drm_mode_object_find(dev, plane_req->plane_id,
1767 DRM_MODE_OBJECT_PLANE);
1768 if (!obj) {
1769 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1770 plane_req->plane_id);
1771 ret = -ENOENT;
1772 goto out;
1773 }
1774 plane = obj_to_plane(obj);
1775
1776 /* No fb means shut it down */
1777 if (!plane_req->fb_id) {
1778 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001779 plane->crtc = NULL;
1780 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001781 goto out;
1782 }
1783
1784 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1785 DRM_MODE_OBJECT_CRTC);
1786 if (!obj) {
1787 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1788 plane_req->crtc_id);
1789 ret = -ENOENT;
1790 goto out;
1791 }
1792 crtc = obj_to_crtc(obj);
1793
1794 obj = drm_mode_object_find(dev, plane_req->fb_id,
1795 DRM_MODE_OBJECT_FB);
1796 if (!obj) {
1797 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1798 plane_req->fb_id);
1799 ret = -ENOENT;
1800 goto out;
1801 }
1802 fb = obj_to_fb(obj);
1803
Ville Syrjälä62443be2011-12-20 00:06:47 +02001804 /* Check whether this plane supports the fb pixel format. */
1805 for (i = 0; i < plane->format_count; i++)
1806 if (fb->pixel_format == plane->format_types[i])
1807 break;
1808 if (i == plane->format_count) {
1809 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1810 ret = -EINVAL;
1811 goto out;
1812 }
1813
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001814 fb_width = fb->width << 16;
1815 fb_height = fb->height << 16;
1816
1817 /* Make sure source coordinates are inside the fb. */
1818 if (plane_req->src_w > fb_width ||
1819 plane_req->src_x > fb_width - plane_req->src_w ||
1820 plane_req->src_h > fb_height ||
1821 plane_req->src_y > fb_height - plane_req->src_h) {
1822 DRM_DEBUG_KMS("Invalid source coordinates "
1823 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1824 plane_req->src_w >> 16,
1825 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1826 plane_req->src_h >> 16,
1827 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1828 plane_req->src_x >> 16,
1829 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1830 plane_req->src_y >> 16,
1831 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1832 ret = -ENOSPC;
1833 goto out;
1834 }
1835
Ville Syrjälä687a0402011-12-20 00:06:45 +02001836 /* Give drivers some help against integer overflows */
1837 if (plane_req->crtc_w > INT_MAX ||
1838 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1839 plane_req->crtc_h > INT_MAX ||
1840 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1841 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1842 plane_req->crtc_w, plane_req->crtc_h,
1843 plane_req->crtc_x, plane_req->crtc_y);
1844 ret = -ERANGE;
1845 goto out;
1846 }
1847
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001848 ret = plane->funcs->update_plane(plane, crtc, fb,
1849 plane_req->crtc_x, plane_req->crtc_y,
1850 plane_req->crtc_w, plane_req->crtc_h,
1851 plane_req->src_x, plane_req->src_y,
1852 plane_req->src_w, plane_req->src_h);
1853 if (!ret) {
1854 plane->crtc = crtc;
1855 plane->fb = fb;
1856 }
1857
1858out:
1859 mutex_unlock(&dev->mode_config.mutex);
1860
1861 return ret;
1862}
1863
1864/**
Dave Airlief453ba02008-11-07 14:05:41 -08001865 * drm_mode_setcrtc - set CRTC configuration
1866 * @inode: inode from the ioctl
1867 * @filp: file * from the ioctl
1868 * @cmd: cmd from ioctl
1869 * @arg: arg from ioctl
1870 *
1871 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001872 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001873 *
1874 * Build a new CRTC configuration based on user request.
1875 *
1876 * Called by the user via ioctl.
1877 *
1878 * RETURNS:
1879 * Zero on success, errno on failure.
1880 */
1881int drm_mode_setcrtc(struct drm_device *dev, void *data,
1882 struct drm_file *file_priv)
1883{
1884 struct drm_mode_config *config = &dev->mode_config;
1885 struct drm_mode_crtc *crtc_req = data;
1886 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001887 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001888 struct drm_connector **connector_set = NULL, *connector;
1889 struct drm_framebuffer *fb = NULL;
1890 struct drm_display_mode *mode = NULL;
1891 struct drm_mode_set set;
1892 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001893 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001894 int i;
1895
Dave Airliefb3b06c2011-02-08 13:55:21 +10001896 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1897 return -EINVAL;
1898
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001899 /* For some reason crtc x/y offsets are signed internally. */
1900 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1901 return -ERANGE;
1902
Dave Airlief453ba02008-11-07 14:05:41 -08001903 mutex_lock(&dev->mode_config.mutex);
1904 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1905 DRM_MODE_OBJECT_CRTC);
1906 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001907 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001908 ret = -EINVAL;
1909 goto out;
1910 }
1911 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001912 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001913
1914 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001915 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001916 /* If we have a mode we need a framebuffer. */
1917 /* If we pass -1, set the mode with the currently bound fb */
1918 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001919 if (!crtc->fb) {
1920 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1921 ret = -EINVAL;
1922 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001923 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001924 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001925 } else {
1926 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1927 DRM_MODE_OBJECT_FB);
1928 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001929 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1930 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001931 ret = -EINVAL;
1932 goto out;
1933 }
1934 fb = obj_to_fb(obj);
1935 }
1936
1937 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001938 if (!mode) {
1939 ret = -ENOMEM;
1940 goto out;
1941 }
1942
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001943 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1944 if (ret) {
1945 DRM_DEBUG_KMS("Invalid mode\n");
1946 goto out;
1947 }
1948
Dave Airlief453ba02008-11-07 14:05:41 -08001949 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001950
Rob Clark7c80e122012-09-04 16:35:56 +00001951 hdisplay = mode->hdisplay;
1952 vdisplay = mode->vdisplay;
1953
1954 if (crtc->invert_dimensions)
1955 swap(hdisplay, vdisplay);
1956
1957 if (hdisplay > fb->width ||
1958 vdisplay > fb->height ||
1959 crtc_req->x > fb->width - hdisplay ||
1960 crtc_req->y > fb->height - vdisplay) {
1961 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1962 fb->width, fb->height,
1963 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1964 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001965 ret = -ENOSPC;
1966 goto out;
1967 }
Dave Airlief453ba02008-11-07 14:05:41 -08001968 }
1969
1970 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001971 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001972 ret = -EINVAL;
1973 goto out;
1974 }
1975
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001976 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001977 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001978 crtc_req->count_connectors);
1979 ret = -EINVAL;
1980 goto out;
1981 }
1982
1983 if (crtc_req->count_connectors > 0) {
1984 u32 out_id;
1985
1986 /* Avoid unbounded kernel memory allocation */
1987 if (crtc_req->count_connectors > config->num_connector) {
1988 ret = -EINVAL;
1989 goto out;
1990 }
1991
1992 connector_set = kmalloc(crtc_req->count_connectors *
1993 sizeof(struct drm_connector *),
1994 GFP_KERNEL);
1995 if (!connector_set) {
1996 ret = -ENOMEM;
1997 goto out;
1998 }
1999
2000 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002001 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002002 if (get_user(out_id, &set_connectors_ptr[i])) {
2003 ret = -EFAULT;
2004 goto out;
2005 }
2006
2007 obj = drm_mode_object_find(dev, out_id,
2008 DRM_MODE_OBJECT_CONNECTOR);
2009 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002010 DRM_DEBUG_KMS("Connector id %d unknown\n",
2011 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002012 ret = -EINVAL;
2013 goto out;
2014 }
2015 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002016 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2017 connector->base.id,
2018 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002019
2020 connector_set[i] = connector;
2021 }
2022 }
2023
2024 set.crtc = crtc;
2025 set.x = crtc_req->x;
2026 set.y = crtc_req->y;
2027 set.mode = mode;
2028 set.connectors = connector_set;
2029 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002030 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002031 ret = crtc->funcs->set_config(&set);
2032
2033out:
2034 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002035 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002036 mutex_unlock(&dev->mode_config.mutex);
2037 return ret;
2038}
2039
2040int drm_mode_cursor_ioctl(struct drm_device *dev,
2041 void *data, struct drm_file *file_priv)
2042{
2043 struct drm_mode_cursor *req = data;
2044 struct drm_mode_object *obj;
2045 struct drm_crtc *crtc;
2046 int ret = 0;
2047
Dave Airliefb3b06c2011-02-08 13:55:21 +10002048 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2049 return -EINVAL;
2050
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002051 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002052 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002053
2054 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002055 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002056 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002057 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002058 ret = -EINVAL;
2059 goto out;
2060 }
2061 crtc = obj_to_crtc(obj);
2062
2063 if (req->flags & DRM_MODE_CURSOR_BO) {
2064 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002065 ret = -ENXIO;
2066 goto out;
2067 }
2068 /* Turns off the cursor if handle is 0 */
2069 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2070 req->width, req->height);
2071 }
2072
2073 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2074 if (crtc->funcs->cursor_move) {
2075 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2076 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002077 ret = -EFAULT;
2078 goto out;
2079 }
2080 }
2081out:
2082 mutex_unlock(&dev->mode_config.mutex);
2083 return ret;
2084}
2085
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002086/* Original addfb only supported RGB formats, so figure out which one */
2087uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2088{
2089 uint32_t fmt;
2090
2091 switch (bpp) {
2092 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002093 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002094 break;
2095 case 16:
2096 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002097 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002098 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002099 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002100 break;
2101 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002102 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002103 break;
2104 case 32:
2105 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002106 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002107 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002108 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002109 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002110 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002111 break;
2112 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002113 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2114 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002115 break;
2116 }
2117
2118 return fmt;
2119}
2120EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2121
Dave Airlief453ba02008-11-07 14:05:41 -08002122/**
2123 * drm_mode_addfb - add an FB to the graphics configuration
2124 * @inode: inode from the ioctl
2125 * @filp: file * from the ioctl
2126 * @cmd: cmd from ioctl
2127 * @arg: arg from ioctl
2128 *
2129 * LOCKING:
2130 * Takes mode config lock.
2131 *
2132 * Add a new FB to the specified CRTC, given a user request.
2133 *
2134 * Called by the user via ioctl.
2135 *
2136 * RETURNS:
2137 * Zero on success, errno on failure.
2138 */
2139int drm_mode_addfb(struct drm_device *dev,
2140 void *data, struct drm_file *file_priv)
2141{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002142 struct drm_mode_fb_cmd *or = data;
2143 struct drm_mode_fb_cmd2 r = {};
2144 struct drm_mode_config *config = &dev->mode_config;
2145 struct drm_framebuffer *fb;
2146 int ret = 0;
2147
2148 /* Use new struct with format internally */
2149 r.fb_id = or->fb_id;
2150 r.width = or->width;
2151 r.height = or->height;
2152 r.pitches[0] = or->pitch;
2153 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2154 r.handles[0] = or->handle;
2155
2156 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2157 return -EINVAL;
2158
Jesse Barnesacb4b992011-11-07 12:03:23 -08002159 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002160 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002161
2162 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002163 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002164
2165 mutex_lock(&dev->mode_config.mutex);
2166
2167 /* TODO check buffer is sufficiently large */
2168 /* TODO setup destructor callback */
2169
2170 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2171 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002172 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002173 ret = PTR_ERR(fb);
2174 goto out;
2175 }
2176
2177 or->fb_id = fb->base.id;
2178 list_add(&fb->filp_head, &file_priv->fbs);
2179 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2180
2181out:
2182 mutex_unlock(&dev->mode_config.mutex);
2183 return ret;
2184}
2185
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002186static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002187{
2188 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2189
2190 switch (format) {
2191 case DRM_FORMAT_C8:
2192 case DRM_FORMAT_RGB332:
2193 case DRM_FORMAT_BGR233:
2194 case DRM_FORMAT_XRGB4444:
2195 case DRM_FORMAT_XBGR4444:
2196 case DRM_FORMAT_RGBX4444:
2197 case DRM_FORMAT_BGRX4444:
2198 case DRM_FORMAT_ARGB4444:
2199 case DRM_FORMAT_ABGR4444:
2200 case DRM_FORMAT_RGBA4444:
2201 case DRM_FORMAT_BGRA4444:
2202 case DRM_FORMAT_XRGB1555:
2203 case DRM_FORMAT_XBGR1555:
2204 case DRM_FORMAT_RGBX5551:
2205 case DRM_FORMAT_BGRX5551:
2206 case DRM_FORMAT_ARGB1555:
2207 case DRM_FORMAT_ABGR1555:
2208 case DRM_FORMAT_RGBA5551:
2209 case DRM_FORMAT_BGRA5551:
2210 case DRM_FORMAT_RGB565:
2211 case DRM_FORMAT_BGR565:
2212 case DRM_FORMAT_RGB888:
2213 case DRM_FORMAT_BGR888:
2214 case DRM_FORMAT_XRGB8888:
2215 case DRM_FORMAT_XBGR8888:
2216 case DRM_FORMAT_RGBX8888:
2217 case DRM_FORMAT_BGRX8888:
2218 case DRM_FORMAT_ARGB8888:
2219 case DRM_FORMAT_ABGR8888:
2220 case DRM_FORMAT_RGBA8888:
2221 case DRM_FORMAT_BGRA8888:
2222 case DRM_FORMAT_XRGB2101010:
2223 case DRM_FORMAT_XBGR2101010:
2224 case DRM_FORMAT_RGBX1010102:
2225 case DRM_FORMAT_BGRX1010102:
2226 case DRM_FORMAT_ARGB2101010:
2227 case DRM_FORMAT_ABGR2101010:
2228 case DRM_FORMAT_RGBA1010102:
2229 case DRM_FORMAT_BGRA1010102:
2230 case DRM_FORMAT_YUYV:
2231 case DRM_FORMAT_YVYU:
2232 case DRM_FORMAT_UYVY:
2233 case DRM_FORMAT_VYUY:
2234 case DRM_FORMAT_AYUV:
2235 case DRM_FORMAT_NV12:
2236 case DRM_FORMAT_NV21:
2237 case DRM_FORMAT_NV16:
2238 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002239 case DRM_FORMAT_NV24:
2240 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002241 case DRM_FORMAT_YUV410:
2242 case DRM_FORMAT_YVU410:
2243 case DRM_FORMAT_YUV411:
2244 case DRM_FORMAT_YVU411:
2245 case DRM_FORMAT_YUV420:
2246 case DRM_FORMAT_YVU420:
2247 case DRM_FORMAT_YUV422:
2248 case DRM_FORMAT_YVU422:
2249 case DRM_FORMAT_YUV444:
2250 case DRM_FORMAT_YVU444:
2251 return 0;
2252 default:
2253 return -EINVAL;
2254 }
2255}
2256
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002257static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002258{
2259 int ret, hsub, vsub, num_planes, i;
2260
2261 ret = format_check(r);
2262 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002263 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002264 return ret;
2265 }
2266
2267 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2268 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2269 num_planes = drm_format_num_planes(r->pixel_format);
2270
2271 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002272 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002273 return -EINVAL;
2274 }
2275
2276 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002277 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002278 return -EINVAL;
2279 }
2280
2281 for (i = 0; i < num_planes; i++) {
2282 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002283 unsigned int height = r->height / (i != 0 ? vsub : 1);
2284 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002285
2286 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002287 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002288 return -EINVAL;
2289 }
2290
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002291 if ((uint64_t) width * cpp > UINT_MAX)
2292 return -ERANGE;
2293
2294 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2295 return -ERANGE;
2296
2297 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002298 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002299 return -EINVAL;
2300 }
2301 }
2302
2303 return 0;
2304}
2305
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002306/**
2307 * drm_mode_addfb2 - add an FB to the graphics configuration
2308 * @inode: inode from the ioctl
2309 * @filp: file * from the ioctl
2310 * @cmd: cmd from ioctl
2311 * @arg: arg from ioctl
2312 *
2313 * LOCKING:
2314 * Takes mode config lock.
2315 *
2316 * Add a new FB to the specified CRTC, given a user request with format.
2317 *
2318 * Called by the user via ioctl.
2319 *
2320 * RETURNS:
2321 * Zero on success, errno on failure.
2322 */
2323int drm_mode_addfb2(struct drm_device *dev,
2324 void *data, struct drm_file *file_priv)
2325{
2326 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002327 struct drm_mode_config *config = &dev->mode_config;
2328 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002329 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002330
Dave Airliefb3b06c2011-02-08 13:55:21 +10002331 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2332 return -EINVAL;
2333
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002334 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2335 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2336 return -EINVAL;
2337 }
2338
Dave Airlief453ba02008-11-07 14:05:41 -08002339 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002340 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002341 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002342 return -EINVAL;
2343 }
2344 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002345 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002346 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002347 return -EINVAL;
2348 }
2349
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002350 ret = framebuffer_check(r);
2351 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002352 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002353
Dave Airlief453ba02008-11-07 14:05:41 -08002354 mutex_lock(&dev->mode_config.mutex);
2355
Dave Airlief453ba02008-11-07 14:05:41 -08002356 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002357 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002358 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002359 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002360 goto out;
2361 }
2362
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002363 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002364 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002365 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002366
2367out:
2368 mutex_unlock(&dev->mode_config.mutex);
2369 return ret;
2370}
2371
2372/**
2373 * drm_mode_rmfb - remove an FB from the configuration
2374 * @inode: inode from the ioctl
2375 * @filp: file * from the ioctl
2376 * @cmd: cmd from ioctl
2377 * @arg: arg from ioctl
2378 *
2379 * LOCKING:
2380 * Takes mode config lock.
2381 *
2382 * Remove the FB specified by the user.
2383 *
2384 * Called by the user via ioctl.
2385 *
2386 * RETURNS:
2387 * Zero on success, errno on failure.
2388 */
2389int drm_mode_rmfb(struct drm_device *dev,
2390 void *data, struct drm_file *file_priv)
2391{
2392 struct drm_mode_object *obj;
2393 struct drm_framebuffer *fb = NULL;
2394 struct drm_framebuffer *fbl = NULL;
2395 uint32_t *id = data;
2396 int ret = 0;
2397 int found = 0;
2398
Dave Airliefb3b06c2011-02-08 13:55:21 +10002399 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2400 return -EINVAL;
2401
Dave Airlief453ba02008-11-07 14:05:41 -08002402 mutex_lock(&dev->mode_config.mutex);
2403 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002404 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002405 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002406 ret = -EINVAL;
2407 goto out;
2408 }
2409 fb = obj_to_fb(obj);
2410
2411 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2412 if (fb == fbl)
2413 found = 1;
2414
2415 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002416 ret = -EINVAL;
2417 goto out;
2418 }
2419
Rob Clarkf7eff602012-09-05 21:48:38 +00002420 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002421
2422out:
2423 mutex_unlock(&dev->mode_config.mutex);
2424 return ret;
2425}
2426
2427/**
2428 * drm_mode_getfb - get FB info
2429 * @inode: inode from the ioctl
2430 * @filp: file * from the ioctl
2431 * @cmd: cmd from ioctl
2432 * @arg: arg from ioctl
2433 *
2434 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002435 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002436 *
2437 * Lookup the FB given its ID and return info about it.
2438 *
2439 * Called by the user via ioctl.
2440 *
2441 * RETURNS:
2442 * Zero on success, errno on failure.
2443 */
2444int drm_mode_getfb(struct drm_device *dev,
2445 void *data, struct drm_file *file_priv)
2446{
2447 struct drm_mode_fb_cmd *r = data;
2448 struct drm_mode_object *obj;
2449 struct drm_framebuffer *fb;
2450 int ret = 0;
2451
Dave Airliefb3b06c2011-02-08 13:55:21 +10002452 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2453 return -EINVAL;
2454
Dave Airlief453ba02008-11-07 14:05:41 -08002455 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002456 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002457 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002458 ret = -EINVAL;
2459 goto out;
2460 }
2461 fb = obj_to_fb(obj);
2462
2463 r->height = fb->height;
2464 r->width = fb->width;
2465 r->depth = fb->depth;
2466 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002467 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002468 fb->funcs->create_handle(fb, file_priv, &r->handle);
2469
2470out:
2471 mutex_unlock(&dev->mode_config.mutex);
2472 return ret;
2473}
2474
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002475int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2476 void *data, struct drm_file *file_priv)
2477{
2478 struct drm_clip_rect __user *clips_ptr;
2479 struct drm_clip_rect *clips = NULL;
2480 struct drm_mode_fb_dirty_cmd *r = data;
2481 struct drm_mode_object *obj;
2482 struct drm_framebuffer *fb;
2483 unsigned flags;
2484 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002485 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002486
Dave Airliefb3b06c2011-02-08 13:55:21 +10002487 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2488 return -EINVAL;
2489
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002490 mutex_lock(&dev->mode_config.mutex);
2491 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2492 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002493 ret = -EINVAL;
2494 goto out_err1;
2495 }
2496 fb = obj_to_fb(obj);
2497
2498 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002499 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002500
2501 if (!num_clips != !clips_ptr) {
2502 ret = -EINVAL;
2503 goto out_err1;
2504 }
2505
2506 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2507
2508 /* If userspace annotates copy, clips must come in pairs */
2509 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2510 ret = -EINVAL;
2511 goto out_err1;
2512 }
2513
2514 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002515 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2516 ret = -EINVAL;
2517 goto out_err1;
2518 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002519 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2520 if (!clips) {
2521 ret = -ENOMEM;
2522 goto out_err1;
2523 }
2524
2525 ret = copy_from_user(clips, clips_ptr,
2526 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002527 if (ret) {
2528 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002529 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002530 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002531 }
2532
2533 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002534 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2535 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002536 } else {
2537 ret = -ENOSYS;
2538 goto out_err2;
2539 }
2540
2541out_err2:
2542 kfree(clips);
2543out_err1:
2544 mutex_unlock(&dev->mode_config.mutex);
2545 return ret;
2546}
2547
2548
Dave Airlief453ba02008-11-07 14:05:41 -08002549/**
2550 * drm_fb_release - remove and free the FBs on this file
2551 * @filp: file * from the ioctl
2552 *
2553 * LOCKING:
2554 * Takes mode config lock.
2555 *
2556 * Destroy all the FBs associated with @filp.
2557 *
2558 * Called by the user via ioctl.
2559 *
2560 * RETURNS:
2561 * Zero on success, errno on failure.
2562 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002563void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002564{
Dave Airlief453ba02008-11-07 14:05:41 -08002565 struct drm_device *dev = priv->minor->dev;
2566 struct drm_framebuffer *fb, *tfb;
2567
2568 mutex_lock(&dev->mode_config.mutex);
2569 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00002570 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002571 }
2572 mutex_unlock(&dev->mode_config.mutex);
2573}
2574
2575/**
2576 * drm_mode_attachmode - add a mode to the user mode list
2577 * @dev: DRM device
2578 * @connector: connector to add the mode to
2579 * @mode: mode to add
2580 *
2581 * Add @mode to @connector's user mode list.
2582 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002583static void drm_mode_attachmode(struct drm_device *dev,
2584 struct drm_connector *connector,
2585 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002586{
Dave Airlief453ba02008-11-07 14:05:41 -08002587 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002588}
2589
2590int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002591 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002592{
2593 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002594 int ret = 0;
2595 struct drm_display_mode *dup_mode, *next;
2596 LIST_HEAD(list);
2597
Dave Airlief453ba02008-11-07 14:05:41 -08002598 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2599 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002600 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002601 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002602 dup_mode = drm_mode_duplicate(dev, mode);
2603 if (!dup_mode) {
2604 ret = -ENOMEM;
2605 goto out;
2606 }
2607 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002608 }
2609 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002610
2611 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2612 if (!connector->encoder)
2613 continue;
2614 if (connector->encoder->crtc == crtc)
2615 list_move_tail(list.next, &connector->user_modes);
2616 }
2617
2618 WARN_ON(!list_empty(&list));
2619
2620 out:
2621 list_for_each_entry_safe(dup_mode, next, &list, head)
2622 drm_mode_destroy(dev, dup_mode);
2623
2624 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002625}
2626EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2627
2628static int drm_mode_detachmode(struct drm_device *dev,
2629 struct drm_connector *connector,
2630 struct drm_display_mode *mode)
2631{
2632 int found = 0;
2633 int ret = 0;
2634 struct drm_display_mode *match_mode, *t;
2635
2636 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2637 if (drm_mode_equal(match_mode, mode)) {
2638 list_del(&match_mode->head);
2639 drm_mode_destroy(dev, match_mode);
2640 found = 1;
2641 break;
2642 }
2643 }
2644
2645 if (!found)
2646 ret = -EINVAL;
2647
2648 return ret;
2649}
2650
2651int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2652{
2653 struct drm_connector *connector;
2654
2655 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2656 drm_mode_detachmode(dev, connector, mode);
2657 }
2658 return 0;
2659}
2660EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2661
2662/**
2663 * drm_fb_attachmode - Attach a user mode to an connector
2664 * @inode: inode from the ioctl
2665 * @filp: file * from the ioctl
2666 * @cmd: cmd from ioctl
2667 * @arg: arg from ioctl
2668 *
2669 * This attaches a user specified mode to an connector.
2670 * Called by the user via ioctl.
2671 *
2672 * RETURNS:
2673 * Zero on success, errno on failure.
2674 */
2675int drm_mode_attachmode_ioctl(struct drm_device *dev,
2676 void *data, struct drm_file *file_priv)
2677{
2678 struct drm_mode_mode_cmd *mode_cmd = data;
2679 struct drm_connector *connector;
2680 struct drm_display_mode *mode;
2681 struct drm_mode_object *obj;
2682 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002683 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002684
Dave Airliefb3b06c2011-02-08 13:55:21 +10002685 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2686 return -EINVAL;
2687
Dave Airlief453ba02008-11-07 14:05:41 -08002688 mutex_lock(&dev->mode_config.mutex);
2689
2690 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2691 if (!obj) {
2692 ret = -EINVAL;
2693 goto out;
2694 }
2695 connector = obj_to_connector(obj);
2696
2697 mode = drm_mode_create(dev);
2698 if (!mode) {
2699 ret = -ENOMEM;
2700 goto out;
2701 }
2702
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002703 ret = drm_crtc_convert_umode(mode, umode);
2704 if (ret) {
2705 DRM_DEBUG_KMS("Invalid mode\n");
2706 drm_mode_destroy(dev, mode);
2707 goto out;
2708 }
Dave Airlief453ba02008-11-07 14:05:41 -08002709
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002710 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002711out:
2712 mutex_unlock(&dev->mode_config.mutex);
2713 return ret;
2714}
2715
2716
2717/**
2718 * drm_fb_detachmode - Detach a user specified mode from an connector
2719 * @inode: inode from the ioctl
2720 * @filp: file * from the ioctl
2721 * @cmd: cmd from ioctl
2722 * @arg: arg from ioctl
2723 *
2724 * Called by the user via ioctl.
2725 *
2726 * RETURNS:
2727 * Zero on success, errno on failure.
2728 */
2729int drm_mode_detachmode_ioctl(struct drm_device *dev,
2730 void *data, struct drm_file *file_priv)
2731{
2732 struct drm_mode_object *obj;
2733 struct drm_mode_mode_cmd *mode_cmd = data;
2734 struct drm_connector *connector;
2735 struct drm_display_mode mode;
2736 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002737 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002738
Dave Airliefb3b06c2011-02-08 13:55:21 +10002739 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2740 return -EINVAL;
2741
Dave Airlief453ba02008-11-07 14:05:41 -08002742 mutex_lock(&dev->mode_config.mutex);
2743
2744 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2745 if (!obj) {
2746 ret = -EINVAL;
2747 goto out;
2748 }
2749 connector = obj_to_connector(obj);
2750
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002751 ret = drm_crtc_convert_umode(&mode, umode);
2752 if (ret) {
2753 DRM_DEBUG_KMS("Invalid mode\n");
2754 goto out;
2755 }
2756
Dave Airlief453ba02008-11-07 14:05:41 -08002757 ret = drm_mode_detachmode(dev, connector, &mode);
2758out:
2759 mutex_unlock(&dev->mode_config.mutex);
2760 return ret;
2761}
2762
2763struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2764 const char *name, int num_values)
2765{
2766 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002767 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002768
2769 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2770 if (!property)
2771 return NULL;
2772
2773 if (num_values) {
2774 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2775 if (!property->values)
2776 goto fail;
2777 }
2778
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002779 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2780 if (ret)
2781 goto fail;
2782
Dave Airlief453ba02008-11-07 14:05:41 -08002783 property->flags = flags;
2784 property->num_values = num_values;
2785 INIT_LIST_HEAD(&property->enum_blob_list);
2786
Vinson Lee471dd2e2011-11-10 11:55:40 -08002787 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002788 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002789 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2790 }
Dave Airlief453ba02008-11-07 14:05:41 -08002791
2792 list_add_tail(&property->head, &dev->mode_config.property_list);
2793 return property;
2794fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002795 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002796 kfree(property);
2797 return NULL;
2798}
2799EXPORT_SYMBOL(drm_property_create);
2800
Sascha Hauer4a67d392012-02-06 10:58:17 +01002801struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2802 const char *name,
2803 const struct drm_prop_enum_list *props,
2804 int num_values)
2805{
2806 struct drm_property *property;
2807 int i, ret;
2808
2809 flags |= DRM_MODE_PROP_ENUM;
2810
2811 property = drm_property_create(dev, flags, name, num_values);
2812 if (!property)
2813 return NULL;
2814
2815 for (i = 0; i < num_values; i++) {
2816 ret = drm_property_add_enum(property, i,
2817 props[i].type,
2818 props[i].name);
2819 if (ret) {
2820 drm_property_destroy(dev, property);
2821 return NULL;
2822 }
2823 }
2824
2825 return property;
2826}
2827EXPORT_SYMBOL(drm_property_create_enum);
2828
Rob Clark49e27542012-05-17 02:23:26 -06002829struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2830 int flags, const char *name,
2831 const struct drm_prop_enum_list *props,
2832 int num_values)
2833{
2834 struct drm_property *property;
2835 int i, ret;
2836
2837 flags |= DRM_MODE_PROP_BITMASK;
2838
2839 property = drm_property_create(dev, flags, name, num_values);
2840 if (!property)
2841 return NULL;
2842
2843 for (i = 0; i < num_values; i++) {
2844 ret = drm_property_add_enum(property, i,
2845 props[i].type,
2846 props[i].name);
2847 if (ret) {
2848 drm_property_destroy(dev, property);
2849 return NULL;
2850 }
2851 }
2852
2853 return property;
2854}
2855EXPORT_SYMBOL(drm_property_create_bitmask);
2856
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002857struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2858 const char *name,
2859 uint64_t min, uint64_t max)
2860{
2861 struct drm_property *property;
2862
2863 flags |= DRM_MODE_PROP_RANGE;
2864
2865 property = drm_property_create(dev, flags, name, 2);
2866 if (!property)
2867 return NULL;
2868
2869 property->values[0] = min;
2870 property->values[1] = max;
2871
2872 return property;
2873}
2874EXPORT_SYMBOL(drm_property_create_range);
2875
Dave Airlief453ba02008-11-07 14:05:41 -08002876int drm_property_add_enum(struct drm_property *property, int index,
2877 uint64_t value, const char *name)
2878{
2879 struct drm_property_enum *prop_enum;
2880
Rob Clark49e27542012-05-17 02:23:26 -06002881 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2882 return -EINVAL;
2883
2884 /*
2885 * Bitmask enum properties have the additional constraint of values
2886 * from 0 to 63
2887 */
2888 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002889 return -EINVAL;
2890
2891 if (!list_empty(&property->enum_blob_list)) {
2892 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2893 if (prop_enum->value == value) {
2894 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2895 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2896 return 0;
2897 }
2898 }
2899 }
2900
2901 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2902 if (!prop_enum)
2903 return -ENOMEM;
2904
2905 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2906 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2907 prop_enum->value = value;
2908
2909 property->values[index] = value;
2910 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2911 return 0;
2912}
2913EXPORT_SYMBOL(drm_property_add_enum);
2914
2915void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2916{
2917 struct drm_property_enum *prop_enum, *pt;
2918
2919 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2920 list_del(&prop_enum->head);
2921 kfree(prop_enum);
2922 }
2923
2924 if (property->num_values)
2925 kfree(property->values);
2926 drm_mode_object_put(dev, &property->base);
2927 list_del(&property->head);
2928 kfree(property);
2929}
2930EXPORT_SYMBOL(drm_property_destroy);
2931
Paulo Zanoniafea2ad2012-05-15 18:09:00 -03002932void drm_connector_attach_property(struct drm_connector *connector,
Dave Airlief453ba02008-11-07 14:05:41 -08002933 struct drm_property *property, uint64_t init_val)
2934{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002935 drm_object_attach_property(&connector->base, property, init_val);
Dave Airlief453ba02008-11-07 14:05:41 -08002936}
2937EXPORT_SYMBOL(drm_connector_attach_property);
2938
2939int drm_connector_property_set_value(struct drm_connector *connector,
2940 struct drm_property *property, uint64_t value)
2941{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002942 return drm_object_property_set_value(&connector->base, property, value);
Dave Airlief453ba02008-11-07 14:05:41 -08002943}
2944EXPORT_SYMBOL(drm_connector_property_set_value);
2945
2946int drm_connector_property_get_value(struct drm_connector *connector,
2947 struct drm_property *property, uint64_t *val)
2948{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002949 return drm_object_property_get_value(&connector->base, property, val);
Dave Airlief453ba02008-11-07 14:05:41 -08002950}
2951EXPORT_SYMBOL(drm_connector_property_get_value);
2952
Paulo Zanonic5431882012-05-15 18:09:02 -03002953void drm_object_attach_property(struct drm_mode_object *obj,
2954 struct drm_property *property,
2955 uint64_t init_val)
2956{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002957 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002958
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002959 if (count == DRM_OBJECT_MAX_PROPERTY) {
2960 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2961 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2962 "you see this message on the same object type.\n",
2963 obj->type);
2964 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002965 }
2966
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002967 obj->properties->ids[count] = property->base.id;
2968 obj->properties->values[count] = init_val;
2969 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002970}
2971EXPORT_SYMBOL(drm_object_attach_property);
2972
2973int drm_object_property_set_value(struct drm_mode_object *obj,
2974 struct drm_property *property, uint64_t val)
2975{
2976 int i;
2977
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002978 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002979 if (obj->properties->ids[i] == property->base.id) {
2980 obj->properties->values[i] = val;
2981 return 0;
2982 }
2983 }
2984
2985 return -EINVAL;
2986}
2987EXPORT_SYMBOL(drm_object_property_set_value);
2988
2989int drm_object_property_get_value(struct drm_mode_object *obj,
2990 struct drm_property *property, uint64_t *val)
2991{
2992 int i;
2993
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002994 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002995 if (obj->properties->ids[i] == property->base.id) {
2996 *val = obj->properties->values[i];
2997 return 0;
2998 }
2999 }
3000
3001 return -EINVAL;
3002}
3003EXPORT_SYMBOL(drm_object_property_get_value);
3004
Dave Airlief453ba02008-11-07 14:05:41 -08003005int drm_mode_getproperty_ioctl(struct drm_device *dev,
3006 void *data, struct drm_file *file_priv)
3007{
3008 struct drm_mode_object *obj;
3009 struct drm_mode_get_property *out_resp = data;
3010 struct drm_property *property;
3011 int enum_count = 0;
3012 int blob_count = 0;
3013 int value_count = 0;
3014 int ret = 0, i;
3015 int copied;
3016 struct drm_property_enum *prop_enum;
3017 struct drm_mode_property_enum __user *enum_ptr;
3018 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003019 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003020 uint64_t __user *values_ptr;
3021 uint32_t __user *blob_length_ptr;
3022
Dave Airliefb3b06c2011-02-08 13:55:21 +10003023 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3024 return -EINVAL;
3025
Dave Airlief453ba02008-11-07 14:05:41 -08003026 mutex_lock(&dev->mode_config.mutex);
3027 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3028 if (!obj) {
3029 ret = -EINVAL;
3030 goto done;
3031 }
3032 property = obj_to_property(obj);
3033
Rob Clark49e27542012-05-17 02:23:26 -06003034 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003035 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3036 enum_count++;
3037 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3038 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3039 blob_count++;
3040 }
3041
3042 value_count = property->num_values;
3043
3044 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3045 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3046 out_resp->flags = property->flags;
3047
3048 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003049 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003050 for (i = 0; i < value_count; i++) {
3051 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3052 ret = -EFAULT;
3053 goto done;
3054 }
3055 }
3056 }
3057 out_resp->count_values = value_count;
3058
Rob Clark49e27542012-05-17 02:23:26 -06003059 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003060 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3061 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003062 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003063 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3064
3065 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3066 ret = -EFAULT;
3067 goto done;
3068 }
3069
3070 if (copy_to_user(&enum_ptr[copied].name,
3071 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3072 ret = -EFAULT;
3073 goto done;
3074 }
3075 copied++;
3076 }
3077 }
3078 out_resp->count_enum_blobs = enum_count;
3079 }
3080
3081 if (property->flags & DRM_MODE_PROP_BLOB) {
3082 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3083 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003084 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3085 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003086
3087 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3088 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3089 ret = -EFAULT;
3090 goto done;
3091 }
3092
3093 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3094 ret = -EFAULT;
3095 goto done;
3096 }
3097
3098 copied++;
3099 }
3100 }
3101 out_resp->count_enum_blobs = blob_count;
3102 }
3103done:
3104 mutex_unlock(&dev->mode_config.mutex);
3105 return ret;
3106}
3107
3108static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3109 void *data)
3110{
3111 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003112 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003113
3114 if (!length || !data)
3115 return NULL;
3116
3117 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3118 if (!blob)
3119 return NULL;
3120
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003121 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3122 if (ret) {
3123 kfree(blob);
3124 return NULL;
3125 }
3126
Dave Airlief453ba02008-11-07 14:05:41 -08003127 blob->length = length;
3128
3129 memcpy(blob->data, data, length);
3130
Dave Airlief453ba02008-11-07 14:05:41 -08003131 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3132 return blob;
3133}
3134
3135static void drm_property_destroy_blob(struct drm_device *dev,
3136 struct drm_property_blob *blob)
3137{
3138 drm_mode_object_put(dev, &blob->base);
3139 list_del(&blob->head);
3140 kfree(blob);
3141}
3142
3143int drm_mode_getblob_ioctl(struct drm_device *dev,
3144 void *data, struct drm_file *file_priv)
3145{
3146 struct drm_mode_object *obj;
3147 struct drm_mode_get_blob *out_resp = data;
3148 struct drm_property_blob *blob;
3149 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003150 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003151
Dave Airliefb3b06c2011-02-08 13:55:21 +10003152 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3153 return -EINVAL;
3154
Dave Airlief453ba02008-11-07 14:05:41 -08003155 mutex_lock(&dev->mode_config.mutex);
3156 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3157 if (!obj) {
3158 ret = -EINVAL;
3159 goto done;
3160 }
3161 blob = obj_to_blob(obj);
3162
3163 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003164 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003165 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3166 ret = -EFAULT;
3167 goto done;
3168 }
3169 }
3170 out_resp->length = blob->length;
3171
3172done:
3173 mutex_unlock(&dev->mode_config.mutex);
3174 return ret;
3175}
3176
3177int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3178 struct edid *edid)
3179{
3180 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003181 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003182
3183 if (connector->edid_blob_ptr)
3184 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3185
3186 /* Delete edid, when there is none. */
3187 if (!edid) {
3188 connector->edid_blob_ptr = NULL;
3189 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3190 return ret;
3191 }
3192
Adam Jackson7466f4c2010-03-29 21:43:23 +00003193 size = EDID_LENGTH * (1 + edid->extensions);
3194 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3195 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08003196
3197 ret = drm_connector_property_set_value(connector,
3198 dev->mode_config.edid_property,
3199 connector->edid_blob_ptr->base.id);
3200
3201 return ret;
3202}
3203EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3204
Paulo Zanoni26a34812012-05-15 18:08:59 -03003205static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003206 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003207{
3208 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3209 return false;
3210 if (property->flags & DRM_MODE_PROP_RANGE) {
3211 if (value < property->values[0] || value > property->values[1])
3212 return false;
3213 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003214 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3215 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003216 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003217 for (i = 0; i < property->num_values; i++)
3218 valid_mask |= (1ULL << property->values[i]);
3219 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003220 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3221 /* Only the driver knows */
3222 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003223 } else {
3224 int i;
3225 for (i = 0; i < property->num_values; i++)
3226 if (property->values[i] == value)
3227 return true;
3228 return false;
3229 }
3230}
3231
Dave Airlief453ba02008-11-07 14:05:41 -08003232int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3233 void *data, struct drm_file *file_priv)
3234{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003235 struct drm_mode_connector_set_property *conn_set_prop = data;
3236 struct drm_mode_obj_set_property obj_set_prop = {
3237 .value = conn_set_prop->value,
3238 .prop_id = conn_set_prop->prop_id,
3239 .obj_id = conn_set_prop->connector_id,
3240 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3241 };
Dave Airlief453ba02008-11-07 14:05:41 -08003242
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003243 /* It does all the locking and checking we need */
3244 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003245}
3246
Paulo Zanonic5431882012-05-15 18:09:02 -03003247static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3248 struct drm_property *property,
3249 uint64_t value)
3250{
3251 int ret = -EINVAL;
3252 struct drm_connector *connector = obj_to_connector(obj);
3253
3254 /* Do DPMS ourselves */
3255 if (property == connector->dev->mode_config.dpms_property) {
3256 if (connector->funcs->dpms)
3257 (*connector->funcs->dpms)(connector, (int)value);
3258 ret = 0;
3259 } else if (connector->funcs->set_property)
3260 ret = connector->funcs->set_property(connector, property, value);
3261
3262 /* store the property value if successful */
3263 if (!ret)
3264 drm_connector_property_set_value(connector, property, value);
3265 return ret;
3266}
3267
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003268static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3269 struct drm_property *property,
3270 uint64_t value)
3271{
3272 int ret = -EINVAL;
3273 struct drm_crtc *crtc = obj_to_crtc(obj);
3274
3275 if (crtc->funcs->set_property)
3276 ret = crtc->funcs->set_property(crtc, property, value);
3277 if (!ret)
3278 drm_object_property_set_value(obj, property, value);
3279
3280 return ret;
3281}
3282
Rob Clark4d939142012-05-17 02:23:27 -06003283static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3284 struct drm_property *property,
3285 uint64_t value)
3286{
3287 int ret = -EINVAL;
3288 struct drm_plane *plane = obj_to_plane(obj);
3289
3290 if (plane->funcs->set_property)
3291 ret = plane->funcs->set_property(plane, property, value);
3292 if (!ret)
3293 drm_object_property_set_value(obj, property, value);
3294
3295 return ret;
3296}
3297
Paulo Zanonic5431882012-05-15 18:09:02 -03003298int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3299 struct drm_file *file_priv)
3300{
3301 struct drm_mode_obj_get_properties *arg = data;
3302 struct drm_mode_object *obj;
3303 int ret = 0;
3304 int i;
3305 int copied = 0;
3306 int props_count = 0;
3307 uint32_t __user *props_ptr;
3308 uint64_t __user *prop_values_ptr;
3309
3310 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3311 return -EINVAL;
3312
3313 mutex_lock(&dev->mode_config.mutex);
3314
3315 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3316 if (!obj) {
3317 ret = -EINVAL;
3318 goto out;
3319 }
3320 if (!obj->properties) {
3321 ret = -EINVAL;
3322 goto out;
3323 }
3324
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003325 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003326
3327 /* This ioctl is called twice, once to determine how much space is
3328 * needed, and the 2nd time to fill it. */
3329 if ((arg->count_props >= props_count) && props_count) {
3330 copied = 0;
3331 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3332 prop_values_ptr = (uint64_t __user *)(unsigned long)
3333 (arg->prop_values_ptr);
3334 for (i = 0; i < props_count; i++) {
3335 if (put_user(obj->properties->ids[i],
3336 props_ptr + copied)) {
3337 ret = -EFAULT;
3338 goto out;
3339 }
3340 if (put_user(obj->properties->values[i],
3341 prop_values_ptr + copied)) {
3342 ret = -EFAULT;
3343 goto out;
3344 }
3345 copied++;
3346 }
3347 }
3348 arg->count_props = props_count;
3349out:
3350 mutex_unlock(&dev->mode_config.mutex);
3351 return ret;
3352}
3353
3354int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3355 struct drm_file *file_priv)
3356{
3357 struct drm_mode_obj_set_property *arg = data;
3358 struct drm_mode_object *arg_obj;
3359 struct drm_mode_object *prop_obj;
3360 struct drm_property *property;
3361 int ret = -EINVAL;
3362 int i;
3363
3364 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3365 return -EINVAL;
3366
3367 mutex_lock(&dev->mode_config.mutex);
3368
3369 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3370 if (!arg_obj)
3371 goto out;
3372 if (!arg_obj->properties)
3373 goto out;
3374
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003375 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003376 if (arg_obj->properties->ids[i] == arg->prop_id)
3377 break;
3378
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003379 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003380 goto out;
3381
3382 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3383 DRM_MODE_OBJECT_PROPERTY);
3384 if (!prop_obj)
3385 goto out;
3386 property = obj_to_property(prop_obj);
3387
3388 if (!drm_property_change_is_valid(property, arg->value))
3389 goto out;
3390
3391 switch (arg_obj->type) {
3392 case DRM_MODE_OBJECT_CONNECTOR:
3393 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3394 arg->value);
3395 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003396 case DRM_MODE_OBJECT_CRTC:
3397 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3398 break;
Rob Clark4d939142012-05-17 02:23:27 -06003399 case DRM_MODE_OBJECT_PLANE:
3400 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3401 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003402 }
3403
3404out:
3405 mutex_unlock(&dev->mode_config.mutex);
3406 return ret;
3407}
3408
Dave Airlief453ba02008-11-07 14:05:41 -08003409int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3410 struct drm_encoder *encoder)
3411{
3412 int i;
3413
3414 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3415 if (connector->encoder_ids[i] == 0) {
3416 connector->encoder_ids[i] = encoder->base.id;
3417 return 0;
3418 }
3419 }
3420 return -ENOMEM;
3421}
3422EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3423
3424void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3425 struct drm_encoder *encoder)
3426{
3427 int i;
3428 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3429 if (connector->encoder_ids[i] == encoder->base.id) {
3430 connector->encoder_ids[i] = 0;
3431 if (connector->encoder == encoder)
3432 connector->encoder = NULL;
3433 break;
3434 }
3435 }
3436}
3437EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3438
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003439int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003440 int gamma_size)
3441{
3442 crtc->gamma_size = gamma_size;
3443
3444 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3445 if (!crtc->gamma_store) {
3446 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003447 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003448 }
3449
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003450 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003451}
3452EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3453
3454int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3455 void *data, struct drm_file *file_priv)
3456{
3457 struct drm_mode_crtc_lut *crtc_lut = data;
3458 struct drm_mode_object *obj;
3459 struct drm_crtc *crtc;
3460 void *r_base, *g_base, *b_base;
3461 int size;
3462 int ret = 0;
3463
Dave Airliefb3b06c2011-02-08 13:55:21 +10003464 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3465 return -EINVAL;
3466
Dave Airlief453ba02008-11-07 14:05:41 -08003467 mutex_lock(&dev->mode_config.mutex);
3468 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3469 if (!obj) {
3470 ret = -EINVAL;
3471 goto out;
3472 }
3473 crtc = obj_to_crtc(obj);
3474
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003475 if (crtc->funcs->gamma_set == NULL) {
3476 ret = -ENOSYS;
3477 goto out;
3478 }
3479
Dave Airlief453ba02008-11-07 14:05:41 -08003480 /* memcpy into gamma store */
3481 if (crtc_lut->gamma_size != crtc->gamma_size) {
3482 ret = -EINVAL;
3483 goto out;
3484 }
3485
3486 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3487 r_base = crtc->gamma_store;
3488 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3489 ret = -EFAULT;
3490 goto out;
3491 }
3492
3493 g_base = r_base + size;
3494 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3495 ret = -EFAULT;
3496 goto out;
3497 }
3498
3499 b_base = g_base + size;
3500 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3501 ret = -EFAULT;
3502 goto out;
3503 }
3504
James Simmons72034252010-08-03 01:33:19 +01003505 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003506
3507out:
3508 mutex_unlock(&dev->mode_config.mutex);
3509 return ret;
3510
3511}
3512
3513int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3514 void *data, struct drm_file *file_priv)
3515{
3516 struct drm_mode_crtc_lut *crtc_lut = data;
3517 struct drm_mode_object *obj;
3518 struct drm_crtc *crtc;
3519 void *r_base, *g_base, *b_base;
3520 int size;
3521 int ret = 0;
3522
Dave Airliefb3b06c2011-02-08 13:55:21 +10003523 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3524 return -EINVAL;
3525
Dave Airlief453ba02008-11-07 14:05:41 -08003526 mutex_lock(&dev->mode_config.mutex);
3527 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3528 if (!obj) {
3529 ret = -EINVAL;
3530 goto out;
3531 }
3532 crtc = obj_to_crtc(obj);
3533
3534 /* memcpy into gamma store */
3535 if (crtc_lut->gamma_size != crtc->gamma_size) {
3536 ret = -EINVAL;
3537 goto out;
3538 }
3539
3540 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3541 r_base = crtc->gamma_store;
3542 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3543 ret = -EFAULT;
3544 goto out;
3545 }
3546
3547 g_base = r_base + size;
3548 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3549 ret = -EFAULT;
3550 goto out;
3551 }
3552
3553 b_base = g_base + size;
3554 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3555 ret = -EFAULT;
3556 goto out;
3557 }
3558out:
3559 mutex_unlock(&dev->mode_config.mutex);
3560 return ret;
3561}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003562
3563int drm_mode_page_flip_ioctl(struct drm_device *dev,
3564 void *data, struct drm_file *file_priv)
3565{
3566 struct drm_mode_crtc_page_flip *page_flip = data;
3567 struct drm_mode_object *obj;
3568 struct drm_crtc *crtc;
3569 struct drm_framebuffer *fb;
3570 struct drm_pending_vblank_event *e = NULL;
3571 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003572 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003573 int ret = -EINVAL;
3574
3575 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3576 page_flip->reserved != 0)
3577 return -EINVAL;
3578
3579 mutex_lock(&dev->mode_config.mutex);
3580 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3581 if (!obj)
3582 goto out;
3583 crtc = obj_to_crtc(obj);
3584
Chris Wilson90c1efd2010-07-17 20:23:26 +01003585 if (crtc->fb == NULL) {
3586 /* The framebuffer is currently unbound, presumably
3587 * due to a hotplug event, that userspace has not
3588 * yet discovered.
3589 */
3590 ret = -EBUSY;
3591 goto out;
3592 }
3593
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003594 if (crtc->funcs->page_flip == NULL)
3595 goto out;
3596
3597 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3598 if (!obj)
3599 goto out;
3600 fb = obj_to_fb(obj);
3601
Rob Clark7c80e122012-09-04 16:35:56 +00003602 hdisplay = crtc->mode.hdisplay;
3603 vdisplay = crtc->mode.vdisplay;
3604
3605 if (crtc->invert_dimensions)
3606 swap(hdisplay, vdisplay);
3607
3608 if (hdisplay > fb->width ||
3609 vdisplay > fb->height ||
3610 crtc->x > fb->width - hdisplay ||
3611 crtc->y > fb->height - vdisplay) {
3612 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3613 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3614 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003615 ret = -ENOSPC;
3616 goto out;
3617 }
3618
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003619 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3620 ret = -ENOMEM;
3621 spin_lock_irqsave(&dev->event_lock, flags);
3622 if (file_priv->event_space < sizeof e->event) {
3623 spin_unlock_irqrestore(&dev->event_lock, flags);
3624 goto out;
3625 }
3626 file_priv->event_space -= sizeof e->event;
3627 spin_unlock_irqrestore(&dev->event_lock, flags);
3628
3629 e = kzalloc(sizeof *e, GFP_KERNEL);
3630 if (e == NULL) {
3631 spin_lock_irqsave(&dev->event_lock, flags);
3632 file_priv->event_space += sizeof e->event;
3633 spin_unlock_irqrestore(&dev->event_lock, flags);
3634 goto out;
3635 }
3636
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003637 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003638 e->event.base.length = sizeof e->event;
3639 e->event.user_data = page_flip->user_data;
3640 e->base.event = &e->event.base;
3641 e->base.file_priv = file_priv;
3642 e->base.destroy =
3643 (void (*) (struct drm_pending_event *)) kfree;
3644 }
3645
3646 ret = crtc->funcs->page_flip(crtc, fb, e);
3647 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003648 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3649 spin_lock_irqsave(&dev->event_lock, flags);
3650 file_priv->event_space += sizeof e->event;
3651 spin_unlock_irqrestore(&dev->event_lock, flags);
3652 kfree(e);
3653 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003654 }
3655
3656out:
3657 mutex_unlock(&dev->mode_config.mutex);
3658 return ret;
3659}
Chris Wilsoneb033552011-01-24 15:11:08 +00003660
3661void drm_mode_config_reset(struct drm_device *dev)
3662{
3663 struct drm_crtc *crtc;
3664 struct drm_encoder *encoder;
3665 struct drm_connector *connector;
3666
3667 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3668 if (crtc->funcs->reset)
3669 crtc->funcs->reset(crtc);
3670
3671 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3672 if (encoder->funcs->reset)
3673 encoder->funcs->reset(encoder);
3674
3675 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3676 if (connector->funcs->reset)
3677 connector->funcs->reset(connector);
3678}
3679EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003680
3681int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3682 void *data, struct drm_file *file_priv)
3683{
3684 struct drm_mode_create_dumb *args = data;
3685
3686 if (!dev->driver->dumb_create)
3687 return -ENOSYS;
3688 return dev->driver->dumb_create(file_priv, dev, args);
3689}
3690
3691int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3692 void *data, struct drm_file *file_priv)
3693{
3694 struct drm_mode_map_dumb *args = data;
3695
3696 /* call driver ioctl to get mmap offset */
3697 if (!dev->driver->dumb_map_offset)
3698 return -ENOSYS;
3699
3700 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3701}
3702
3703int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3704 void *data, struct drm_file *file_priv)
3705{
3706 struct drm_mode_destroy_dumb *args = data;
3707
3708 if (!dev->driver->dumb_destroy)
3709 return -ENOSYS;
3710
3711 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3712}
Dave Airlie248dbc22011-11-29 20:02:54 +00003713
3714/*
3715 * Just need to support RGB formats here for compat with code that doesn't
3716 * use pixel formats directly yet.
3717 */
3718void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3719 int *bpp)
3720{
3721 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003722 case DRM_FORMAT_RGB332:
3723 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003724 *depth = 8;
3725 *bpp = 8;
3726 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003727 case DRM_FORMAT_XRGB1555:
3728 case DRM_FORMAT_XBGR1555:
3729 case DRM_FORMAT_RGBX5551:
3730 case DRM_FORMAT_BGRX5551:
3731 case DRM_FORMAT_ARGB1555:
3732 case DRM_FORMAT_ABGR1555:
3733 case DRM_FORMAT_RGBA5551:
3734 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003735 *depth = 15;
3736 *bpp = 16;
3737 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003738 case DRM_FORMAT_RGB565:
3739 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003740 *depth = 16;
3741 *bpp = 16;
3742 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003743 case DRM_FORMAT_RGB888:
3744 case DRM_FORMAT_BGR888:
3745 *depth = 24;
3746 *bpp = 24;
3747 break;
3748 case DRM_FORMAT_XRGB8888:
3749 case DRM_FORMAT_XBGR8888:
3750 case DRM_FORMAT_RGBX8888:
3751 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003752 *depth = 24;
3753 *bpp = 32;
3754 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003755 case DRM_FORMAT_XRGB2101010:
3756 case DRM_FORMAT_XBGR2101010:
3757 case DRM_FORMAT_RGBX1010102:
3758 case DRM_FORMAT_BGRX1010102:
3759 case DRM_FORMAT_ARGB2101010:
3760 case DRM_FORMAT_ABGR2101010:
3761 case DRM_FORMAT_RGBA1010102:
3762 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003763 *depth = 30;
3764 *bpp = 32;
3765 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003766 case DRM_FORMAT_ARGB8888:
3767 case DRM_FORMAT_ABGR8888:
3768 case DRM_FORMAT_RGBA8888:
3769 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003770 *depth = 32;
3771 *bpp = 32;
3772 break;
3773 default:
3774 DRM_DEBUG_KMS("unsupported pixel format\n");
3775 *depth = 0;
3776 *bpp = 0;
3777 break;
3778 }
3779}
3780EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003781
3782/**
3783 * drm_format_num_planes - get the number of planes for format
3784 * @format: pixel format (DRM_FORMAT_*)
3785 *
3786 * RETURNS:
3787 * The number of planes used by the specified pixel format.
3788 */
3789int drm_format_num_planes(uint32_t format)
3790{
3791 switch (format) {
3792 case DRM_FORMAT_YUV410:
3793 case DRM_FORMAT_YVU410:
3794 case DRM_FORMAT_YUV411:
3795 case DRM_FORMAT_YVU411:
3796 case DRM_FORMAT_YUV420:
3797 case DRM_FORMAT_YVU420:
3798 case DRM_FORMAT_YUV422:
3799 case DRM_FORMAT_YVU422:
3800 case DRM_FORMAT_YUV444:
3801 case DRM_FORMAT_YVU444:
3802 return 3;
3803 case DRM_FORMAT_NV12:
3804 case DRM_FORMAT_NV21:
3805 case DRM_FORMAT_NV16:
3806 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003807 case DRM_FORMAT_NV24:
3808 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003809 return 2;
3810 default:
3811 return 1;
3812 }
3813}
3814EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003815
3816/**
3817 * drm_format_plane_cpp - determine the bytes per pixel value
3818 * @format: pixel format (DRM_FORMAT_*)
3819 * @plane: plane index
3820 *
3821 * RETURNS:
3822 * The bytes per pixel value for the specified plane.
3823 */
3824int drm_format_plane_cpp(uint32_t format, int plane)
3825{
3826 unsigned int depth;
3827 int bpp;
3828
3829 if (plane >= drm_format_num_planes(format))
3830 return 0;
3831
3832 switch (format) {
3833 case DRM_FORMAT_YUYV:
3834 case DRM_FORMAT_YVYU:
3835 case DRM_FORMAT_UYVY:
3836 case DRM_FORMAT_VYUY:
3837 return 2;
3838 case DRM_FORMAT_NV12:
3839 case DRM_FORMAT_NV21:
3840 case DRM_FORMAT_NV16:
3841 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003842 case DRM_FORMAT_NV24:
3843 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003844 return plane ? 2 : 1;
3845 case DRM_FORMAT_YUV410:
3846 case DRM_FORMAT_YVU410:
3847 case DRM_FORMAT_YUV411:
3848 case DRM_FORMAT_YVU411:
3849 case DRM_FORMAT_YUV420:
3850 case DRM_FORMAT_YVU420:
3851 case DRM_FORMAT_YUV422:
3852 case DRM_FORMAT_YVU422:
3853 case DRM_FORMAT_YUV444:
3854 case DRM_FORMAT_YVU444:
3855 return 1;
3856 default:
3857 drm_fb_get_bpp_depth(format, &depth, &bpp);
3858 return bpp >> 3;
3859 }
3860}
3861EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003862
3863/**
3864 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3865 * @format: pixel format (DRM_FORMAT_*)
3866 *
3867 * RETURNS:
3868 * The horizontal chroma subsampling factor for the
3869 * specified pixel format.
3870 */
3871int drm_format_horz_chroma_subsampling(uint32_t format)
3872{
3873 switch (format) {
3874 case DRM_FORMAT_YUV411:
3875 case DRM_FORMAT_YVU411:
3876 case DRM_FORMAT_YUV410:
3877 case DRM_FORMAT_YVU410:
3878 return 4;
3879 case DRM_FORMAT_YUYV:
3880 case DRM_FORMAT_YVYU:
3881 case DRM_FORMAT_UYVY:
3882 case DRM_FORMAT_VYUY:
3883 case DRM_FORMAT_NV12:
3884 case DRM_FORMAT_NV21:
3885 case DRM_FORMAT_NV16:
3886 case DRM_FORMAT_NV61:
3887 case DRM_FORMAT_YUV422:
3888 case DRM_FORMAT_YVU422:
3889 case DRM_FORMAT_YUV420:
3890 case DRM_FORMAT_YVU420:
3891 return 2;
3892 default:
3893 return 1;
3894 }
3895}
3896EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3897
3898/**
3899 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3900 * @format: pixel format (DRM_FORMAT_*)
3901 *
3902 * RETURNS:
3903 * The vertical chroma subsampling factor for the
3904 * specified pixel format.
3905 */
3906int drm_format_vert_chroma_subsampling(uint32_t format)
3907{
3908 switch (format) {
3909 case DRM_FORMAT_YUV410:
3910 case DRM_FORMAT_YVU410:
3911 return 4;
3912 case DRM_FORMAT_YUV420:
3913 case DRM_FORMAT_YVU420:
3914 case DRM_FORMAT_NV12:
3915 case DRM_FORMAT_NV21:
3916 return 2;
3917 default:
3918 return 1;
3919 }
3920}
3921EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);