blob: a82ec05dee2360c26622fdc9468e517332288602 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
36#include <drm/drm_crtc.h>
37#include <drm/drm_edid.h>
38#include <drm/drm_fourcc.h>
Dave Airlief453ba02008-11-07 14:05:41 -080039
Daniel Vetter84849902012-12-02 00:28:11 +010040/**
41 * drm_modeset_lock_all - take all modeset locks
42 * @dev: drm device
43 *
44 * This function takes all modeset locks, suitable where a more fine-grained
45 * scheme isn't (yet) implemented.
46 */
47void drm_modeset_lock_all(struct drm_device *dev)
48{
49 mutex_lock(&dev->mode_config.mutex);
50}
51EXPORT_SYMBOL(drm_modeset_lock_all);
52
53/**
54 * drm_modeset_unlock_all - drop all modeset locks
55 * @dev: device
56 */
57void drm_modeset_unlock_all(struct drm_device *dev)
58{
59 mutex_unlock(&dev->mode_config.mutex);
60}
61EXPORT_SYMBOL(drm_modeset_unlock_all);
62
Dave Airlief453ba02008-11-07 14:05:41 -080063/* Avoid boilerplate. I'm tired of typing. */
64#define DRM_ENUM_NAME_FN(fnname, list) \
65 char *fnname(int val) \
66 { \
67 int i; \
68 for (i = 0; i < ARRAY_SIZE(list); i++) { \
69 if (list[i].type == val) \
70 return list[i].name; \
71 } \
72 return "(unknown)"; \
73 }
74
75/*
76 * Global properties
77 */
78static struct drm_prop_enum_list drm_dpms_enum_list[] =
79{ { DRM_MODE_DPMS_ON, "On" },
80 { DRM_MODE_DPMS_STANDBY, "Standby" },
81 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
82 { DRM_MODE_DPMS_OFF, "Off" }
83};
84
85DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
86
87/*
88 * Optional properties
89 */
90static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
91{
Jesse Barnes53bd8382009-07-01 10:04:40 -070092 { DRM_MODE_SCALE_NONE, "None" },
93 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
94 { DRM_MODE_SCALE_CENTER, "Center" },
95 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080096};
97
98static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
99{
100 { DRM_MODE_DITHERING_OFF, "Off" },
101 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +1000102 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -0800103};
104
105/*
106 * Non-global properties, but "required" for certain connectors.
107 */
108static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
109{
110 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
111 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
112 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
113};
114
115DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
116
117static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
118{
119 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
120 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
121 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
122};
123
124DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
125 drm_dvi_i_subconnector_enum_list)
126
127static struct drm_prop_enum_list drm_tv_select_enum_list[] =
128{
129 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
130 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
131 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
132 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200133 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800134};
135
136DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
137
138static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
139{
140 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
141 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
142 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
143 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200144 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800145};
146
147DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
148 drm_tv_subconnector_enum_list)
149
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000150static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
151 { DRM_MODE_DIRTY_OFF, "Off" },
152 { DRM_MODE_DIRTY_ON, "On" },
153 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
154};
155
156DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
157 drm_dirty_info_enum_list)
158
Dave Airlief453ba02008-11-07 14:05:41 -0800159struct drm_conn_prop_enum_list {
160 int type;
161 char *name;
162 int count;
163};
164
165/*
166 * Connector and encoder types.
167 */
168static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
169{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
170 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
171 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
172 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
173 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
174 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
175 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
176 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
177 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500178 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
179 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
180 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
181 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200182 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500183 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200184 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800185};
186
187static struct drm_prop_enum_list drm_encoder_enum_list[] =
188{ { DRM_MODE_ENCODER_NONE, "None" },
189 { DRM_MODE_ENCODER_DAC, "DAC" },
190 { DRM_MODE_ENCODER_TMDS, "TMDS" },
191 { DRM_MODE_ENCODER_LVDS, "LVDS" },
192 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200193 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800194};
195
196char *drm_get_encoder_name(struct drm_encoder *encoder)
197{
198 static char buf[32];
199
200 snprintf(buf, 32, "%s-%d",
201 drm_encoder_enum_list[encoder->encoder_type].name,
202 encoder->base.id);
203 return buf;
204}
Dave Airlie13a81952009-09-07 15:45:33 +1000205EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800206
207char *drm_get_connector_name(struct drm_connector *connector)
208{
209 static char buf[32];
210
211 snprintf(buf, 32, "%s-%d",
212 drm_connector_enum_list[connector->connector_type].name,
213 connector->connector_type_id);
214 return buf;
215}
216EXPORT_SYMBOL(drm_get_connector_name);
217
218char *drm_get_connector_status_name(enum drm_connector_status status)
219{
220 if (status == connector_status_connected)
221 return "connected";
222 else if (status == connector_status_disconnected)
223 return "disconnected";
224 else
225 return "unknown";
226}
227
228/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100229 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800230 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100231 * @obj: object pointer, used to generate unique ID
232 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800233 *
Dave Airlief453ba02008-11-07 14:05:41 -0800234 * Create a unique identifier based on @ptr in @dev's identifier space. Used
235 * for tracking modes, CRTCs and connectors.
236 *
237 * RETURNS:
238 * New unique (relative to other objects in @dev) integer identifier for the
239 * object.
240 */
241static int drm_mode_object_get(struct drm_device *dev,
242 struct drm_mode_object *obj, uint32_t obj_type)
243{
244 int new_id = 0;
245 int ret;
246
Dave Airlief453ba02008-11-07 14:05:41 -0800247again:
248 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
249 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200250 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800251 }
252
Jesse Barnesad2563c2009-01-19 17:21:45 +1000253 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800254 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000255 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800256 if (ret == -EAGAIN)
257 goto again;
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200258 else if (ret)
259 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800260
261 obj->id = new_id;
262 obj->type = obj_type;
263 return 0;
264}
265
266/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100267 * drm_mode_object_put - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800268 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100269 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800270 *
Dave Airlief453ba02008-11-07 14:05:41 -0800271 * Free @id from @dev's unique identifier pool.
272 */
273static void drm_mode_object_put(struct drm_device *dev,
274 struct drm_mode_object *object)
275{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000276 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800277 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000278 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800279}
280
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200281struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
282 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800283{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000284 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800285
Jesse Barnesad2563c2009-01-19 17:21:45 +1000286 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800287 obj = idr_find(&dev->mode_config.crtc_idr, id);
288 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000289 obj = NULL;
290 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800291
292 return obj;
293}
294EXPORT_SYMBOL(drm_mode_object_find);
295
296/**
Dave Airlief453ba02008-11-07 14:05:41 -0800297 * drm_framebuffer_init - initialize a framebuffer
298 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100299 * @fb: framebuffer to be initialized
300 * @funcs: ... with these functions
Dave Airlief453ba02008-11-07 14:05:41 -0800301 *
Dave Airlief453ba02008-11-07 14:05:41 -0800302 * Allocates an ID for the framebuffer's parent mode object, sets its mode
303 * functions & device file and adds it to the master fd list.
304 *
305 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200306 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800307 */
308int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
309 const struct drm_framebuffer_funcs *funcs)
310{
311 int ret;
312
Rob Clarkf7eff602012-09-05 21:48:38 +0000313 kref_init(&fb->refcount);
314
Dave Airlief453ba02008-11-07 14:05:41 -0800315 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200316 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800317 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800318
319 fb->dev = dev;
320 fb->funcs = funcs;
321 dev->mode_config.num_fb++;
322 list_add(&fb->head, &dev->mode_config.fb_list);
323
324 return 0;
325}
326EXPORT_SYMBOL(drm_framebuffer_init);
327
Rob Clarkf7eff602012-09-05 21:48:38 +0000328static void drm_framebuffer_free(struct kref *kref)
329{
330 struct drm_framebuffer *fb =
331 container_of(kref, struct drm_framebuffer, refcount);
332 fb->funcs->destroy(fb);
333}
334
335/**
336 * drm_framebuffer_unreference - unref a framebuffer
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100337 * @fb: framebuffer to unref
338 *
339 * This functions decrements the fb's refcount and frees it if it drops to zero.
Rob Clarkf7eff602012-09-05 21:48:38 +0000340 */
341void drm_framebuffer_unreference(struct drm_framebuffer *fb)
342{
Rob Clarkf7eff602012-09-05 21:48:38 +0000343 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Rob Clarkf7eff602012-09-05 21:48:38 +0000344 kref_put(&fb->refcount, drm_framebuffer_free);
345}
346EXPORT_SYMBOL(drm_framebuffer_unreference);
347
348/**
349 * drm_framebuffer_reference - incr the fb refcnt
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100350 * @fb: framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000351 */
352void drm_framebuffer_reference(struct drm_framebuffer *fb)
353{
354 DRM_DEBUG("FB ID: %d\n", fb->base.id);
355 kref_get(&fb->refcount);
356}
357EXPORT_SYMBOL(drm_framebuffer_reference);
358
Dave Airlief453ba02008-11-07 14:05:41 -0800359/**
360 * drm_framebuffer_cleanup - remove a framebuffer object
361 * @fb: framebuffer to remove
362 *
Dave Airlief453ba02008-11-07 14:05:41 -0800363 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
364 * it, setting it to NULL.
365 */
366void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
367{
368 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100369
Rob Clarkf7eff602012-09-05 21:48:38 +0000370 /*
371 * This could be moved to drm_framebuffer_remove(), but for
372 * debugging is nice to keep around the list of fb's that are
373 * no longer associated w/ a drm_file but are not unreferenced
374 * yet. (i915 and omapdrm have debugfs files which will show
375 * this.)
376 */
377 drm_mode_object_put(dev, &fb->base);
378 list_del(&fb->head);
379 dev->mode_config.num_fb--;
380}
381EXPORT_SYMBOL(drm_framebuffer_cleanup);
382
383/**
384 * drm_framebuffer_remove - remove and unreference a framebuffer object
385 * @fb: framebuffer to remove
386 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000387 * Scans all the CRTCs and planes in @dev's mode_config. If they're
388 * using @fb, removes it, setting it to NULL.
389 */
390void drm_framebuffer_remove(struct drm_framebuffer *fb)
391{
392 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800393 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800394 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000395 struct drm_mode_set set;
396 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800397
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100398 WARN_ON(!drm_modeset_is_locked(dev));
399
Dave Airlief453ba02008-11-07 14:05:41 -0800400 /* remove from any CRTC */
401 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000402 if (crtc->fb == fb) {
403 /* should turn off the crtc */
404 memset(&set, 0, sizeof(struct drm_mode_set));
405 set.crtc = crtc;
406 set.fb = NULL;
Daniel Vetter2d13b672012-12-11 13:47:23 +0100407 ret = drm_mode_set_config_internal(&set);
Dave Airlie5ef5f722009-08-17 13:11:23 +1000408 if (ret)
409 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
410 }
Dave Airlief453ba02008-11-07 14:05:41 -0800411 }
412
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800413 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
414 if (plane->fb == fb) {
415 /* should turn off the crtc */
416 ret = plane->funcs->disable_plane(plane);
417 if (ret)
418 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600419 /* disconnect the plane from the fb and crtc: */
420 plane->fb = NULL;
421 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800422 }
423 }
424
Rob Clarkf7eff602012-09-05 21:48:38 +0000425 list_del(&fb->filp_head);
426
427 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800428}
Rob Clarkf7eff602012-09-05 21:48:38 +0000429EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800430
431/**
432 * drm_crtc_init - Initialise a new CRTC object
433 * @dev: DRM device
434 * @crtc: CRTC object to init
435 * @funcs: callbacks for the new CRTC
436 *
Dave Airlief453ba02008-11-07 14:05:41 -0800437 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200438 *
439 * RETURNS:
440 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800441 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200442int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800443 const struct drm_crtc_funcs *funcs)
444{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200445 int ret;
446
Dave Airlief453ba02008-11-07 14:05:41 -0800447 crtc->dev = dev;
448 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000449 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800450
Daniel Vetter84849902012-12-02 00:28:11 +0100451 drm_modeset_lock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200452
453 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
454 if (ret)
455 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800456
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300457 crtc->base.properties = &crtc->properties;
458
Dave Airlief453ba02008-11-07 14:05:41 -0800459 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
460 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200461
462 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100463 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200464
465 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800466}
467EXPORT_SYMBOL(drm_crtc_init);
468
469/**
470 * drm_crtc_cleanup - Cleans up the core crtc usage.
471 * @crtc: CRTC to cleanup
472 *
Dave Airlief453ba02008-11-07 14:05:41 -0800473 * Cleanup @crtc. Removes from drm modesetting space
474 * does NOT free object, caller does that.
475 */
476void drm_crtc_cleanup(struct drm_crtc *crtc)
477{
478 struct drm_device *dev = crtc->dev;
479
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000480 kfree(crtc->gamma_store);
481 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800482
483 drm_mode_object_put(dev, &crtc->base);
484 list_del(&crtc->head);
485 dev->mode_config.num_crtc--;
486}
487EXPORT_SYMBOL(drm_crtc_cleanup);
488
489/**
490 * drm_mode_probed_add - add a mode to a connector's probed mode list
491 * @connector: connector the new mode
492 * @mode: mode data
493 *
Dave Airlief453ba02008-11-07 14:05:41 -0800494 * Add @mode to @connector's mode list for later use.
495 */
496void drm_mode_probed_add(struct drm_connector *connector,
497 struct drm_display_mode *mode)
498{
499 list_add(&mode->head, &connector->probed_modes);
500}
501EXPORT_SYMBOL(drm_mode_probed_add);
502
503/**
504 * drm_mode_remove - remove and free a mode
505 * @connector: connector list to modify
506 * @mode: mode to remove
507 *
Dave Airlief453ba02008-11-07 14:05:41 -0800508 * Remove @mode from @connector's mode list, then free it.
509 */
510void drm_mode_remove(struct drm_connector *connector,
511 struct drm_display_mode *mode)
512{
513 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100514 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800515}
516EXPORT_SYMBOL(drm_mode_remove);
517
518/**
519 * drm_connector_init - Init a preallocated connector
520 * @dev: DRM device
521 * @connector: the connector to init
522 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100523 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800524 *
Dave Airlief453ba02008-11-07 14:05:41 -0800525 * Initialises a preallocated connector. Connectors should be
526 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200527 *
528 * RETURNS:
529 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800530 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200531int drm_connector_init(struct drm_device *dev,
532 struct drm_connector *connector,
533 const struct drm_connector_funcs *funcs,
534 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800535{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200536 int ret;
537
Daniel Vetter84849902012-12-02 00:28:11 +0100538 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800539
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200540 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
541 if (ret)
542 goto out;
543
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300544 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800545 connector->dev = dev;
546 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800547 connector->connector_type = connector_type;
548 connector->connector_type_id =
549 ++drm_connector_enum_list[connector_type].count; /* TODO */
550 INIT_LIST_HEAD(&connector->user_modes);
551 INIT_LIST_HEAD(&connector->probed_modes);
552 INIT_LIST_HEAD(&connector->modes);
553 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000554 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800555
556 list_add_tail(&connector->head, &dev->mode_config.connector_list);
557 dev->mode_config.num_connector++;
558
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200559 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500560 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200561 dev->mode_config.edid_property,
562 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800563
Rob Clark58495562012-10-11 20:50:56 -0500564 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800565 dev->mode_config.dpms_property, 0);
566
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200567 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100568 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200569
570 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800571}
572EXPORT_SYMBOL(drm_connector_init);
573
574/**
575 * drm_connector_cleanup - cleans up an initialised connector
576 * @connector: connector to cleanup
577 *
Dave Airlief453ba02008-11-07 14:05:41 -0800578 * Cleans up the connector but doesn't free the object.
579 */
580void drm_connector_cleanup(struct drm_connector *connector)
581{
582 struct drm_device *dev = connector->dev;
583 struct drm_display_mode *mode, *t;
584
585 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
586 drm_mode_remove(connector, mode);
587
588 list_for_each_entry_safe(mode, t, &connector->modes, head)
589 drm_mode_remove(connector, mode);
590
591 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
592 drm_mode_remove(connector, mode);
593
Dave Airlief453ba02008-11-07 14:05:41 -0800594 drm_mode_object_put(dev, &connector->base);
595 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000596 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800597}
598EXPORT_SYMBOL(drm_connector_cleanup);
599
Dave Airliecbc7e222012-02-20 14:16:40 +0000600void drm_connector_unplug_all(struct drm_device *dev)
601{
602 struct drm_connector *connector;
603
604 /* taking the mode config mutex ends up in a clash with sysfs */
605 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
606 drm_sysfs_connector_remove(connector);
607
608}
609EXPORT_SYMBOL(drm_connector_unplug_all);
610
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200611int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000612 struct drm_encoder *encoder,
613 const struct drm_encoder_funcs *funcs,
614 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800615{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200616 int ret;
617
Daniel Vetter84849902012-12-02 00:28:11 +0100618 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800619
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200620 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
621 if (ret)
622 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800623
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200624 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800625 encoder->encoder_type = encoder_type;
626 encoder->funcs = funcs;
627
628 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
629 dev->mode_config.num_encoder++;
630
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200631 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100632 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200633
634 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800635}
636EXPORT_SYMBOL(drm_encoder_init);
637
638void drm_encoder_cleanup(struct drm_encoder *encoder)
639{
640 struct drm_device *dev = encoder->dev;
Daniel Vetter84849902012-12-02 00:28:11 +0100641 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800642 drm_mode_object_put(dev, &encoder->base);
643 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000644 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +0100645 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800646}
647EXPORT_SYMBOL(drm_encoder_cleanup);
648
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800649int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
650 unsigned long possible_crtcs,
651 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600652 const uint32_t *formats, uint32_t format_count,
653 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800654{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200655 int ret;
656
Daniel Vetter84849902012-12-02 00:28:11 +0100657 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800658
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200659 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
660 if (ret)
661 goto out;
662
Rob Clark4d939142012-05-17 02:23:27 -0600663 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800664 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800665 plane->funcs = funcs;
666 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
667 GFP_KERNEL);
668 if (!plane->format_types) {
669 DRM_DEBUG_KMS("out of memory when allocating plane\n");
670 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200671 ret = -ENOMEM;
672 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800673 }
674
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800675 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800676 plane->format_count = format_count;
677 plane->possible_crtcs = possible_crtcs;
678
Rob Clark0a7eb242011-12-13 20:19:36 -0600679 /* private planes are not exposed to userspace, but depending on
680 * display hardware, might be convenient to allow sharing programming
681 * for the scanout engine with the crtc implementation.
682 */
683 if (!priv) {
684 list_add_tail(&plane->head, &dev->mode_config.plane_list);
685 dev->mode_config.num_plane++;
686 } else {
687 INIT_LIST_HEAD(&plane->head);
688 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800689
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200690 out:
Daniel Vetter84849902012-12-02 00:28:11 +0100691 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800692
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200693 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800694}
695EXPORT_SYMBOL(drm_plane_init);
696
697void drm_plane_cleanup(struct drm_plane *plane)
698{
699 struct drm_device *dev = plane->dev;
700
Daniel Vetter84849902012-12-02 00:28:11 +0100701 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800702 kfree(plane->format_types);
703 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600704 /* if not added to a list, it must be a private plane */
705 if (!list_empty(&plane->head)) {
706 list_del(&plane->head);
707 dev->mode_config.num_plane--;
708 }
Daniel Vetter84849902012-12-02 00:28:11 +0100709 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800710}
711EXPORT_SYMBOL(drm_plane_cleanup);
712
Dave Airlief453ba02008-11-07 14:05:41 -0800713/**
714 * drm_mode_create - create a new display mode
715 * @dev: DRM device
716 *
Dave Airlief453ba02008-11-07 14:05:41 -0800717 * Create a new drm_display_mode, give it an ID, and return it.
718 *
719 * RETURNS:
720 * Pointer to new mode on success, NULL on error.
721 */
722struct drm_display_mode *drm_mode_create(struct drm_device *dev)
723{
724 struct drm_display_mode *nmode;
725
726 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
727 if (!nmode)
728 return NULL;
729
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200730 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
731 kfree(nmode);
732 return NULL;
733 }
734
Dave Airlief453ba02008-11-07 14:05:41 -0800735 return nmode;
736}
737EXPORT_SYMBOL(drm_mode_create);
738
739/**
740 * drm_mode_destroy - remove a mode
741 * @dev: DRM device
742 * @mode: mode to remove
743 *
Dave Airlief453ba02008-11-07 14:05:41 -0800744 * Free @mode's unique identifier, then free it.
745 */
746void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
747{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200748 if (!mode)
749 return;
750
Dave Airlief453ba02008-11-07 14:05:41 -0800751 drm_mode_object_put(dev, &mode->base);
752
753 kfree(mode);
754}
755EXPORT_SYMBOL(drm_mode_destroy);
756
757static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
758{
759 struct drm_property *edid;
760 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800761
762 /*
763 * Standard properties (apply to all connectors)
764 */
765 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
766 DRM_MODE_PROP_IMMUTABLE,
767 "EDID", 0);
768 dev->mode_config.edid_property = edid;
769
Sascha Hauer4a67d392012-02-06 10:58:17 +0100770 dpms = drm_property_create_enum(dev, 0,
771 "DPMS", drm_dpms_enum_list,
772 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800773 dev->mode_config.dpms_property = dpms;
774
775 return 0;
776}
777
778/**
779 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
780 * @dev: DRM device
781 *
782 * Called by a driver the first time a DVI-I connector is made.
783 */
784int drm_mode_create_dvi_i_properties(struct drm_device *dev)
785{
786 struct drm_property *dvi_i_selector;
787 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800788
789 if (dev->mode_config.dvi_i_select_subconnector_property)
790 return 0;
791
792 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100793 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800794 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100795 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800796 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800797 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
798
Sascha Hauer4a67d392012-02-06 10:58:17 +0100799 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800800 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100801 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800802 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800803 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
804
805 return 0;
806}
807EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
808
809/**
810 * drm_create_tv_properties - create TV specific connector properties
811 * @dev: DRM device
812 * @num_modes: number of different TV formats (modes) supported
813 * @modes: array of pointers to strings containing name of each format
814 *
815 * Called by a driver's TV initialization routine, this function creates
816 * the TV specific connector properties for a given device. Caller is
817 * responsible for allocating a list of format names and passing them to
818 * this routine.
819 */
820int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
821 char *modes[])
822{
823 struct drm_property *tv_selector;
824 struct drm_property *tv_subconnector;
825 int i;
826
827 if (dev->mode_config.tv_select_subconnector_property)
828 return 0;
829
830 /*
831 * Basic connector properties
832 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100833 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800834 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100835 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800836 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800837 dev->mode_config.tv_select_subconnector_property = tv_selector;
838
839 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100840 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
841 "subconnector",
842 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800843 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800844 dev->mode_config.tv_subconnector_property = tv_subconnector;
845
846 /*
847 * Other, TV specific properties: margins & TV modes.
848 */
849 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100850 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800851
852 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100853 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800854
855 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100856 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800857
858 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100859 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800860
861 dev->mode_config.tv_mode_property =
862 drm_property_create(dev, DRM_MODE_PROP_ENUM,
863 "mode", num_modes);
864 for (i = 0; i < num_modes; i++)
865 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
866 i, modes[i]);
867
Francisco Jerezb6b79022009-08-02 04:19:20 +0200868 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100869 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200870
871 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100872 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200873
874 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100875 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200876
Francisco Jereza75f0232009-08-12 02:30:10 +0200877 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100878 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200879
880 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100881 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200882
883 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100884 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200885
Dave Airlief453ba02008-11-07 14:05:41 -0800886 return 0;
887}
888EXPORT_SYMBOL(drm_mode_create_tv_properties);
889
890/**
891 * drm_mode_create_scaling_mode_property - create scaling mode property
892 * @dev: DRM device
893 *
894 * Called by a driver the first time it's needed, must be attached to desired
895 * connectors.
896 */
897int drm_mode_create_scaling_mode_property(struct drm_device *dev)
898{
899 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800900
901 if (dev->mode_config.scaling_mode_property)
902 return 0;
903
904 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100905 drm_property_create_enum(dev, 0, "scaling mode",
906 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800907 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800908
909 dev->mode_config.scaling_mode_property = scaling_mode;
910
911 return 0;
912}
913EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
914
915/**
916 * drm_mode_create_dithering_property - create dithering property
917 * @dev: DRM device
918 *
919 * Called by a driver the first time it's needed, must be attached to desired
920 * connectors.
921 */
922int drm_mode_create_dithering_property(struct drm_device *dev)
923{
924 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800925
926 if (dev->mode_config.dithering_mode_property)
927 return 0;
928
929 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100930 drm_property_create_enum(dev, 0, "dithering",
931 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800932 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800933 dev->mode_config.dithering_mode_property = dithering_mode;
934
935 return 0;
936}
937EXPORT_SYMBOL(drm_mode_create_dithering_property);
938
939/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000940 * drm_mode_create_dirty_property - create dirty property
941 * @dev: DRM device
942 *
943 * Called by a driver the first time it's needed, must be attached to desired
944 * connectors.
945 */
946int drm_mode_create_dirty_info_property(struct drm_device *dev)
947{
948 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000949
950 if (dev->mode_config.dirty_info_property)
951 return 0;
952
953 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100954 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000955 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100956 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000957 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000958 dev->mode_config.dirty_info_property = dirty_info;
959
960 return 0;
961}
962EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
963
964/**
Dave Airlief453ba02008-11-07 14:05:41 -0800965 * drm_mode_config_init - initialize DRM mode_configuration structure
966 * @dev: DRM device
967 *
Dave Airlief453ba02008-11-07 14:05:41 -0800968 * Initialize @dev's mode_config structure, used for tracking the graphics
969 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100970 *
971 * Since this initializes the modeset locks, no locking is possible. Which is no
972 * problem, since this should happen single threaded at init time. It is the
973 * driver's problem to ensure this guarantee.
974 *
Dave Airlief453ba02008-11-07 14:05:41 -0800975 */
976void drm_mode_config_init(struct drm_device *dev)
977{
978 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000979 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800980 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800981 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
982 INIT_LIST_HEAD(&dev->mode_config.connector_list);
983 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
984 INIT_LIST_HEAD(&dev->mode_config.property_list);
985 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800986 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800987 idr_init(&dev->mode_config.crtc_idr);
988
Daniel Vetter84849902012-12-02 00:28:11 +0100989 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800990 drm_mode_create_standard_connector_properties(dev);
Daniel Vetter84849902012-12-02 00:28:11 +0100991 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800992
993 /* Just to be sure */
994 dev->mode_config.num_fb = 0;
995 dev->mode_config.num_connector = 0;
996 dev->mode_config.num_crtc = 0;
997 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800998}
999EXPORT_SYMBOL(drm_mode_config_init);
1000
1001int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1002{
1003 uint32_t total_objects = 0;
1004
1005 total_objects += dev->mode_config.num_crtc;
1006 total_objects += dev->mode_config.num_connector;
1007 total_objects += dev->mode_config.num_encoder;
1008
Dave Airlief453ba02008-11-07 14:05:41 -08001009 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1010 if (!group->id_list)
1011 return -ENOMEM;
1012
1013 group->num_crtcs = 0;
1014 group->num_connectors = 0;
1015 group->num_encoders = 0;
1016 return 0;
1017}
1018
1019int drm_mode_group_init_legacy_group(struct drm_device *dev,
1020 struct drm_mode_group *group)
1021{
1022 struct drm_crtc *crtc;
1023 struct drm_encoder *encoder;
1024 struct drm_connector *connector;
1025 int ret;
1026
1027 if ((ret = drm_mode_group_init(dev, group)))
1028 return ret;
1029
1030 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1031 group->id_list[group->num_crtcs++] = crtc->base.id;
1032
1033 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1034 group->id_list[group->num_crtcs + group->num_encoders++] =
1035 encoder->base.id;
1036
1037 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1038 group->id_list[group->num_crtcs + group->num_encoders +
1039 group->num_connectors++] = connector->base.id;
1040
1041 return 0;
1042}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001043EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001044
1045/**
1046 * drm_mode_config_cleanup - free up DRM mode_config info
1047 * @dev: DRM device
1048 *
Dave Airlief453ba02008-11-07 14:05:41 -08001049 * Free up all the connectors and CRTCs associated with this DRM device, then
1050 * free up the framebuffers and associated buffer objects.
1051 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001052 * Note that since this /should/ happen single-threaded at driver/device
1053 * teardown time, no locking is required. It's the driver's job to ensure that
1054 * this guarantee actually holds true.
1055 *
Dave Airlief453ba02008-11-07 14:05:41 -08001056 * FIXME: cleanup any dangling user buffer objects too
1057 */
1058void drm_mode_config_cleanup(struct drm_device *dev)
1059{
1060 struct drm_connector *connector, *ot;
1061 struct drm_crtc *crtc, *ct;
1062 struct drm_encoder *encoder, *enct;
1063 struct drm_framebuffer *fb, *fbt;
1064 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001065 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001066
1067 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1068 head) {
1069 encoder->funcs->destroy(encoder);
1070 }
1071
1072 list_for_each_entry_safe(connector, ot,
1073 &dev->mode_config.connector_list, head) {
1074 connector->funcs->destroy(connector);
1075 }
1076
1077 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1078 head) {
1079 drm_property_destroy(dev, property);
1080 }
1081
1082 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001083 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001084 }
1085
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001086 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1087 head) {
1088 plane->funcs->destroy(plane);
1089 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001090
Chris Wilson31840092012-09-17 09:38:03 +00001091 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1092 crtc->funcs->destroy(crtc);
1093 }
1094
Sascha Hauer59ce0622012-02-01 11:38:20 +01001095 idr_remove_all(&dev->mode_config.crtc_idr);
1096 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001097}
1098EXPORT_SYMBOL(drm_mode_config_cleanup);
1099
Dave Airlief453ba02008-11-07 14:05:41 -08001100/**
1101 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1102 * @out: drm_mode_modeinfo struct to return to the user
1103 * @in: drm_display_mode to use
1104 *
Dave Airlief453ba02008-11-07 14:05:41 -08001105 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1106 * the user.
1107 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001108static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1109 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001110{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001111 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1112 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1113 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1114 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1115 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1116 "timing values too large for mode info\n");
1117
Dave Airlief453ba02008-11-07 14:05:41 -08001118 out->clock = in->clock;
1119 out->hdisplay = in->hdisplay;
1120 out->hsync_start = in->hsync_start;
1121 out->hsync_end = in->hsync_end;
1122 out->htotal = in->htotal;
1123 out->hskew = in->hskew;
1124 out->vdisplay = in->vdisplay;
1125 out->vsync_start = in->vsync_start;
1126 out->vsync_end = in->vsync_end;
1127 out->vtotal = in->vtotal;
1128 out->vscan = in->vscan;
1129 out->vrefresh = in->vrefresh;
1130 out->flags = in->flags;
1131 out->type = in->type;
1132 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1133 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1134}
1135
1136/**
1137 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1138 * @out: drm_display_mode to return to the user
1139 * @in: drm_mode_modeinfo to use
1140 *
Dave Airlief453ba02008-11-07 14:05:41 -08001141 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1142 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001143 *
1144 * RETURNS:
1145 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001146 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001147static int drm_crtc_convert_umode(struct drm_display_mode *out,
1148 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001149{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001150 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1151 return -ERANGE;
1152
Dave Airlief453ba02008-11-07 14:05:41 -08001153 out->clock = in->clock;
1154 out->hdisplay = in->hdisplay;
1155 out->hsync_start = in->hsync_start;
1156 out->hsync_end = in->hsync_end;
1157 out->htotal = in->htotal;
1158 out->hskew = in->hskew;
1159 out->vdisplay = in->vdisplay;
1160 out->vsync_start = in->vsync_start;
1161 out->vsync_end = in->vsync_end;
1162 out->vtotal = in->vtotal;
1163 out->vscan = in->vscan;
1164 out->vrefresh = in->vrefresh;
1165 out->flags = in->flags;
1166 out->type = in->type;
1167 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1168 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001169
1170 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001171}
1172
1173/**
1174 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001175 * @dev: drm device for the ioctl
1176 * @data: data pointer for the ioctl
1177 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001178 *
Dave Airlief453ba02008-11-07 14:05:41 -08001179 * Construct a set of configuration description structures and return
1180 * them to the user, including CRTC, connector and framebuffer configuration.
1181 *
1182 * Called by the user via ioctl.
1183 *
1184 * RETURNS:
1185 * Zero on success, errno on failure.
1186 */
1187int drm_mode_getresources(struct drm_device *dev, void *data,
1188 struct drm_file *file_priv)
1189{
1190 struct drm_mode_card_res *card_res = data;
1191 struct list_head *lh;
1192 struct drm_framebuffer *fb;
1193 struct drm_connector *connector;
1194 struct drm_crtc *crtc;
1195 struct drm_encoder *encoder;
1196 int ret = 0;
1197 int connector_count = 0;
1198 int crtc_count = 0;
1199 int fb_count = 0;
1200 int encoder_count = 0;
1201 int copied = 0, i;
1202 uint32_t __user *fb_id;
1203 uint32_t __user *crtc_id;
1204 uint32_t __user *connector_id;
1205 uint32_t __user *encoder_id;
1206 struct drm_mode_group *mode_group;
1207
Dave Airliefb3b06c2011-02-08 13:55:21 +10001208 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1209 return -EINVAL;
1210
Daniel Vetter84849902012-12-02 00:28:11 +01001211 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001212
1213 /*
1214 * For the non-control nodes we need to limit the list of resources
1215 * by IDs in the group list for this node
1216 */
1217 list_for_each(lh, &file_priv->fbs)
1218 fb_count++;
1219
1220 mode_group = &file_priv->master->minor->mode_group;
1221 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1222
1223 list_for_each(lh, &dev->mode_config.crtc_list)
1224 crtc_count++;
1225
1226 list_for_each(lh, &dev->mode_config.connector_list)
1227 connector_count++;
1228
1229 list_for_each(lh, &dev->mode_config.encoder_list)
1230 encoder_count++;
1231 } else {
1232
1233 crtc_count = mode_group->num_crtcs;
1234 connector_count = mode_group->num_connectors;
1235 encoder_count = mode_group->num_encoders;
1236 }
1237
1238 card_res->max_height = dev->mode_config.max_height;
1239 card_res->min_height = dev->mode_config.min_height;
1240 card_res->max_width = dev->mode_config.max_width;
1241 card_res->min_width = dev->mode_config.min_width;
1242
1243 /* handle this in 4 parts */
1244 /* FBs */
1245 if (card_res->count_fbs >= fb_count) {
1246 copied = 0;
1247 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001248 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001249 if (put_user(fb->base.id, fb_id + copied)) {
1250 ret = -EFAULT;
1251 goto out;
1252 }
1253 copied++;
1254 }
1255 }
1256 card_res->count_fbs = fb_count;
1257
1258 /* CRTCs */
1259 if (card_res->count_crtcs >= crtc_count) {
1260 copied = 0;
1261 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1262 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1263 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1264 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001265 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001266 if (put_user(crtc->base.id, crtc_id + copied)) {
1267 ret = -EFAULT;
1268 goto out;
1269 }
1270 copied++;
1271 }
1272 } else {
1273 for (i = 0; i < mode_group->num_crtcs; i++) {
1274 if (put_user(mode_group->id_list[i],
1275 crtc_id + copied)) {
1276 ret = -EFAULT;
1277 goto out;
1278 }
1279 copied++;
1280 }
1281 }
1282 }
1283 card_res->count_crtcs = crtc_count;
1284
1285 /* Encoders */
1286 if (card_res->count_encoders >= encoder_count) {
1287 copied = 0;
1288 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1289 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1290 list_for_each_entry(encoder,
1291 &dev->mode_config.encoder_list,
1292 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001293 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1294 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001295 if (put_user(encoder->base.id, encoder_id +
1296 copied)) {
1297 ret = -EFAULT;
1298 goto out;
1299 }
1300 copied++;
1301 }
1302 } else {
1303 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1304 if (put_user(mode_group->id_list[i],
1305 encoder_id + copied)) {
1306 ret = -EFAULT;
1307 goto out;
1308 }
1309 copied++;
1310 }
1311
1312 }
1313 }
1314 card_res->count_encoders = encoder_count;
1315
1316 /* Connectors */
1317 if (card_res->count_connectors >= connector_count) {
1318 copied = 0;
1319 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1320 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1321 list_for_each_entry(connector,
1322 &dev->mode_config.connector_list,
1323 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001324 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1325 connector->base.id,
1326 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001327 if (put_user(connector->base.id,
1328 connector_id + copied)) {
1329 ret = -EFAULT;
1330 goto out;
1331 }
1332 copied++;
1333 }
1334 } else {
1335 int start = mode_group->num_crtcs +
1336 mode_group->num_encoders;
1337 for (i = start; i < start + mode_group->num_connectors; i++) {
1338 if (put_user(mode_group->id_list[i],
1339 connector_id + copied)) {
1340 ret = -EFAULT;
1341 goto out;
1342 }
1343 copied++;
1344 }
1345 }
1346 }
1347 card_res->count_connectors = connector_count;
1348
Jerome Glisse94401062010-07-15 15:43:25 -04001349 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001350 card_res->count_connectors, card_res->count_encoders);
1351
1352out:
Daniel Vetter84849902012-12-02 00:28:11 +01001353 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001354 return ret;
1355}
1356
1357/**
1358 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001359 * @dev: drm device for the ioctl
1360 * @data: data pointer for the ioctl
1361 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001362 *
Dave Airlief453ba02008-11-07 14:05:41 -08001363 * Construct a CRTC configuration structure to return to the user.
1364 *
1365 * Called by the user via ioctl.
1366 *
1367 * RETURNS:
1368 * Zero on success, errno on failure.
1369 */
1370int drm_mode_getcrtc(struct drm_device *dev,
1371 void *data, struct drm_file *file_priv)
1372{
1373 struct drm_mode_crtc *crtc_resp = data;
1374 struct drm_crtc *crtc;
1375 struct drm_mode_object *obj;
1376 int ret = 0;
1377
Dave Airliefb3b06c2011-02-08 13:55:21 +10001378 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1379 return -EINVAL;
1380
Daniel Vetter84849902012-12-02 00:28:11 +01001381 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001382
1383 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1384 DRM_MODE_OBJECT_CRTC);
1385 if (!obj) {
1386 ret = -EINVAL;
1387 goto out;
1388 }
1389 crtc = obj_to_crtc(obj);
1390
1391 crtc_resp->x = crtc->x;
1392 crtc_resp->y = crtc->y;
1393 crtc_resp->gamma_size = crtc->gamma_size;
1394 if (crtc->fb)
1395 crtc_resp->fb_id = crtc->fb->base.id;
1396 else
1397 crtc_resp->fb_id = 0;
1398
1399 if (crtc->enabled) {
1400
1401 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1402 crtc_resp->mode_valid = 1;
1403
1404 } else {
1405 crtc_resp->mode_valid = 0;
1406 }
1407
1408out:
Daniel Vetter84849902012-12-02 00:28:11 +01001409 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001410 return ret;
1411}
1412
1413/**
1414 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001415 * @dev: drm device for the ioctl
1416 * @data: data pointer for the ioctl
1417 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001418 *
Dave Airlief453ba02008-11-07 14:05:41 -08001419 * Construct a connector configuration structure to return to the user.
1420 *
1421 * Called by the user via ioctl.
1422 *
1423 * RETURNS:
1424 * Zero on success, errno on failure.
1425 */
1426int drm_mode_getconnector(struct drm_device *dev, void *data,
1427 struct drm_file *file_priv)
1428{
1429 struct drm_mode_get_connector *out_resp = data;
1430 struct drm_mode_object *obj;
1431 struct drm_connector *connector;
1432 struct drm_display_mode *mode;
1433 int mode_count = 0;
1434 int props_count = 0;
1435 int encoders_count = 0;
1436 int ret = 0;
1437 int copied = 0;
1438 int i;
1439 struct drm_mode_modeinfo u_mode;
1440 struct drm_mode_modeinfo __user *mode_ptr;
1441 uint32_t __user *prop_ptr;
1442 uint64_t __user *prop_values;
1443 uint32_t __user *encoder_ptr;
1444
Dave Airliefb3b06c2011-02-08 13:55:21 +10001445 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1446 return -EINVAL;
1447
Dave Airlief453ba02008-11-07 14:05:41 -08001448 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1449
Jerome Glisse94401062010-07-15 15:43:25 -04001450 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001451
Daniel Vetter84849902012-12-02 00:28:11 +01001452 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001453
1454 obj = drm_mode_object_find(dev, out_resp->connector_id,
1455 DRM_MODE_OBJECT_CONNECTOR);
1456 if (!obj) {
1457 ret = -EINVAL;
1458 goto out;
1459 }
1460 connector = obj_to_connector(obj);
1461
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001462 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001463
1464 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1465 if (connector->encoder_ids[i] != 0) {
1466 encoders_count++;
1467 }
1468 }
1469
1470 if (out_resp->count_modes == 0) {
1471 connector->funcs->fill_modes(connector,
1472 dev->mode_config.max_width,
1473 dev->mode_config.max_height);
1474 }
1475
1476 /* delayed so we get modes regardless of pre-fill_modes state */
1477 list_for_each_entry(mode, &connector->modes, head)
1478 mode_count++;
1479
1480 out_resp->connector_id = connector->base.id;
1481 out_resp->connector_type = connector->connector_type;
1482 out_resp->connector_type_id = connector->connector_type_id;
1483 out_resp->mm_width = connector->display_info.width_mm;
1484 out_resp->mm_height = connector->display_info.height_mm;
1485 out_resp->subpixel = connector->display_info.subpixel_order;
1486 out_resp->connection = connector->status;
1487 if (connector->encoder)
1488 out_resp->encoder_id = connector->encoder->base.id;
1489 else
1490 out_resp->encoder_id = 0;
1491
1492 /*
1493 * This ioctl is called twice, once to determine how much space is
1494 * needed, and the 2nd time to fill it.
1495 */
1496 if ((out_resp->count_modes >= mode_count) && mode_count) {
1497 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001498 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001499 list_for_each_entry(mode, &connector->modes, head) {
1500 drm_crtc_convert_to_umode(&u_mode, mode);
1501 if (copy_to_user(mode_ptr + copied,
1502 &u_mode, sizeof(u_mode))) {
1503 ret = -EFAULT;
1504 goto out;
1505 }
1506 copied++;
1507 }
1508 }
1509 out_resp->count_modes = mode_count;
1510
1511 if ((out_resp->count_props >= props_count) && props_count) {
1512 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001513 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1514 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001515 for (i = 0; i < connector->properties.count; i++) {
1516 if (put_user(connector->properties.ids[i],
1517 prop_ptr + copied)) {
1518 ret = -EFAULT;
1519 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001520 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001521
1522 if (put_user(connector->properties.values[i],
1523 prop_values + copied)) {
1524 ret = -EFAULT;
1525 goto out;
1526 }
1527 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001528 }
1529 }
1530 out_resp->count_props = props_count;
1531
1532 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1533 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001534 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001535 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1536 if (connector->encoder_ids[i] != 0) {
1537 if (put_user(connector->encoder_ids[i],
1538 encoder_ptr + copied)) {
1539 ret = -EFAULT;
1540 goto out;
1541 }
1542 copied++;
1543 }
1544 }
1545 }
1546 out_resp->count_encoders = encoders_count;
1547
1548out:
Daniel Vetter84849902012-12-02 00:28:11 +01001549 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001550 return ret;
1551}
1552
1553int drm_mode_getencoder(struct drm_device *dev, void *data,
1554 struct drm_file *file_priv)
1555{
1556 struct drm_mode_get_encoder *enc_resp = data;
1557 struct drm_mode_object *obj;
1558 struct drm_encoder *encoder;
1559 int ret = 0;
1560
Dave Airliefb3b06c2011-02-08 13:55:21 +10001561 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1562 return -EINVAL;
1563
Daniel Vetter84849902012-12-02 00:28:11 +01001564 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001565 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1566 DRM_MODE_OBJECT_ENCODER);
1567 if (!obj) {
1568 ret = -EINVAL;
1569 goto out;
1570 }
1571 encoder = obj_to_encoder(obj);
1572
1573 if (encoder->crtc)
1574 enc_resp->crtc_id = encoder->crtc->base.id;
1575 else
1576 enc_resp->crtc_id = 0;
1577 enc_resp->encoder_type = encoder->encoder_type;
1578 enc_resp->encoder_id = encoder->base.id;
1579 enc_resp->possible_crtcs = encoder->possible_crtcs;
1580 enc_resp->possible_clones = encoder->possible_clones;
1581
1582out:
Daniel Vetter84849902012-12-02 00:28:11 +01001583 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001584 return ret;
1585}
1586
1587/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001588 * drm_mode_getplane_res - get plane info
1589 * @dev: DRM device
1590 * @data: ioctl data
1591 * @file_priv: DRM file info
1592 *
1593 * Return an plane count and set of IDs.
1594 */
1595int drm_mode_getplane_res(struct drm_device *dev, void *data,
1596 struct drm_file *file_priv)
1597{
1598 struct drm_mode_get_plane_res *plane_resp = data;
1599 struct drm_mode_config *config;
1600 struct drm_plane *plane;
1601 uint32_t __user *plane_ptr;
1602 int copied = 0, ret = 0;
1603
1604 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1605 return -EINVAL;
1606
Daniel Vetter84849902012-12-02 00:28:11 +01001607 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001608 config = &dev->mode_config;
1609
1610 /*
1611 * This ioctl is called twice, once to determine how much space is
1612 * needed, and the 2nd time to fill it.
1613 */
1614 if (config->num_plane &&
1615 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001616 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001617
1618 list_for_each_entry(plane, &config->plane_list, head) {
1619 if (put_user(plane->base.id, plane_ptr + copied)) {
1620 ret = -EFAULT;
1621 goto out;
1622 }
1623 copied++;
1624 }
1625 }
1626 plane_resp->count_planes = config->num_plane;
1627
1628out:
Daniel Vetter84849902012-12-02 00:28:11 +01001629 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001630 return ret;
1631}
1632
1633/**
1634 * drm_mode_getplane - get plane info
1635 * @dev: DRM device
1636 * @data: ioctl data
1637 * @file_priv: DRM file info
1638 *
1639 * Return plane info, including formats supported, gamma size, any
1640 * current fb, etc.
1641 */
1642int drm_mode_getplane(struct drm_device *dev, void *data,
1643 struct drm_file *file_priv)
1644{
1645 struct drm_mode_get_plane *plane_resp = data;
1646 struct drm_mode_object *obj;
1647 struct drm_plane *plane;
1648 uint32_t __user *format_ptr;
1649 int ret = 0;
1650
1651 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1652 return -EINVAL;
1653
Daniel Vetter84849902012-12-02 00:28:11 +01001654 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001655 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1656 DRM_MODE_OBJECT_PLANE);
1657 if (!obj) {
1658 ret = -ENOENT;
1659 goto out;
1660 }
1661 plane = obj_to_plane(obj);
1662
1663 if (plane->crtc)
1664 plane_resp->crtc_id = plane->crtc->base.id;
1665 else
1666 plane_resp->crtc_id = 0;
1667
1668 if (plane->fb)
1669 plane_resp->fb_id = plane->fb->base.id;
1670 else
1671 plane_resp->fb_id = 0;
1672
1673 plane_resp->plane_id = plane->base.id;
1674 plane_resp->possible_crtcs = plane->possible_crtcs;
1675 plane_resp->gamma_size = plane->gamma_size;
1676
1677 /*
1678 * This ioctl is called twice, once to determine how much space is
1679 * needed, and the 2nd time to fill it.
1680 */
1681 if (plane->format_count &&
1682 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001683 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001684 if (copy_to_user(format_ptr,
1685 plane->format_types,
1686 sizeof(uint32_t) * plane->format_count)) {
1687 ret = -EFAULT;
1688 goto out;
1689 }
1690 }
1691 plane_resp->count_format_types = plane->format_count;
1692
1693out:
Daniel Vetter84849902012-12-02 00:28:11 +01001694 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001695 return ret;
1696}
1697
1698/**
1699 * drm_mode_setplane - set up or tear down an plane
1700 * @dev: DRM device
1701 * @data: ioctl data*
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001702 * @file_priv: DRM file info
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001703 *
1704 * Set plane info, including placement, fb, scaling, and other factors.
1705 * Or pass a NULL fb to disable.
1706 */
1707int drm_mode_setplane(struct drm_device *dev, void *data,
1708 struct drm_file *file_priv)
1709{
1710 struct drm_mode_set_plane *plane_req = data;
1711 struct drm_mode_object *obj;
1712 struct drm_plane *plane;
1713 struct drm_crtc *crtc;
1714 struct drm_framebuffer *fb;
1715 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001716 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001717 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001718
1719 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1720 return -EINVAL;
1721
Daniel Vetter84849902012-12-02 00:28:11 +01001722 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001723
1724 /*
1725 * First, find the plane, crtc, and fb objects. If not available,
1726 * we don't bother to call the driver.
1727 */
1728 obj = drm_mode_object_find(dev, plane_req->plane_id,
1729 DRM_MODE_OBJECT_PLANE);
1730 if (!obj) {
1731 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1732 plane_req->plane_id);
1733 ret = -ENOENT;
1734 goto out;
1735 }
1736 plane = obj_to_plane(obj);
1737
1738 /* No fb means shut it down */
1739 if (!plane_req->fb_id) {
1740 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001741 plane->crtc = NULL;
1742 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001743 goto out;
1744 }
1745
1746 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1747 DRM_MODE_OBJECT_CRTC);
1748 if (!obj) {
1749 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1750 plane_req->crtc_id);
1751 ret = -ENOENT;
1752 goto out;
1753 }
1754 crtc = obj_to_crtc(obj);
1755
1756 obj = drm_mode_object_find(dev, plane_req->fb_id,
1757 DRM_MODE_OBJECT_FB);
1758 if (!obj) {
1759 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1760 plane_req->fb_id);
1761 ret = -ENOENT;
1762 goto out;
1763 }
1764 fb = obj_to_fb(obj);
1765
Ville Syrjälä62443be2011-12-20 00:06:47 +02001766 /* Check whether this plane supports the fb pixel format. */
1767 for (i = 0; i < plane->format_count; i++)
1768 if (fb->pixel_format == plane->format_types[i])
1769 break;
1770 if (i == plane->format_count) {
1771 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1772 ret = -EINVAL;
1773 goto out;
1774 }
1775
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001776 fb_width = fb->width << 16;
1777 fb_height = fb->height << 16;
1778
1779 /* Make sure source coordinates are inside the fb. */
1780 if (plane_req->src_w > fb_width ||
1781 plane_req->src_x > fb_width - plane_req->src_w ||
1782 plane_req->src_h > fb_height ||
1783 plane_req->src_y > fb_height - plane_req->src_h) {
1784 DRM_DEBUG_KMS("Invalid source coordinates "
1785 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1786 plane_req->src_w >> 16,
1787 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1788 plane_req->src_h >> 16,
1789 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1790 plane_req->src_x >> 16,
1791 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1792 plane_req->src_y >> 16,
1793 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1794 ret = -ENOSPC;
1795 goto out;
1796 }
1797
Ville Syrjälä687a0402011-12-20 00:06:45 +02001798 /* Give drivers some help against integer overflows */
1799 if (plane_req->crtc_w > INT_MAX ||
1800 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1801 plane_req->crtc_h > INT_MAX ||
1802 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1803 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1804 plane_req->crtc_w, plane_req->crtc_h,
1805 plane_req->crtc_x, plane_req->crtc_y);
1806 ret = -ERANGE;
1807 goto out;
1808 }
1809
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001810 ret = plane->funcs->update_plane(plane, crtc, fb,
1811 plane_req->crtc_x, plane_req->crtc_y,
1812 plane_req->crtc_w, plane_req->crtc_h,
1813 plane_req->src_x, plane_req->src_y,
1814 plane_req->src_w, plane_req->src_h);
1815 if (!ret) {
1816 plane->crtc = crtc;
1817 plane->fb = fb;
1818 }
1819
1820out:
Daniel Vetter84849902012-12-02 00:28:11 +01001821 drm_modeset_unlock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001822
1823 return ret;
1824}
1825
1826/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01001827 * drm_mode_set_config_internal - helper to call ->set_config
1828 * @set: modeset config to set
1829 *
1830 * This is a little helper to wrap internal calls to the ->set_config driver
1831 * interface. The only thing it adds is correct refcounting dance.
1832 */
1833int drm_mode_set_config_internal(struct drm_mode_set *set)
1834{
1835 struct drm_crtc *crtc = set->crtc;
1836
1837 return crtc->funcs->set_config(set);
1838}
1839EXPORT_SYMBOL(drm_mode_set_config_internal);
1840
1841/**
Dave Airlief453ba02008-11-07 14:05:41 -08001842 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001843 * @dev: drm device for the ioctl
1844 * @data: data pointer for the ioctl
1845 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001846 *
Dave Airlief453ba02008-11-07 14:05:41 -08001847 * Build a new CRTC configuration based on user request.
1848 *
1849 * Called by the user via ioctl.
1850 *
1851 * RETURNS:
1852 * Zero on success, errno on failure.
1853 */
1854int drm_mode_setcrtc(struct drm_device *dev, void *data,
1855 struct drm_file *file_priv)
1856{
1857 struct drm_mode_config *config = &dev->mode_config;
1858 struct drm_mode_crtc *crtc_req = data;
1859 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001860 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001861 struct drm_connector **connector_set = NULL, *connector;
1862 struct drm_framebuffer *fb = NULL;
1863 struct drm_display_mode *mode = NULL;
1864 struct drm_mode_set set;
1865 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001866 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001867 int i;
1868
Dave Airliefb3b06c2011-02-08 13:55:21 +10001869 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1870 return -EINVAL;
1871
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001872 /* For some reason crtc x/y offsets are signed internally. */
1873 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1874 return -ERANGE;
1875
Daniel Vetter84849902012-12-02 00:28:11 +01001876 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08001877 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1878 DRM_MODE_OBJECT_CRTC);
1879 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001880 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001881 ret = -EINVAL;
1882 goto out;
1883 }
1884 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001885 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001886
1887 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001888 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001889 /* If we have a mode we need a framebuffer. */
1890 /* If we pass -1, set the mode with the currently bound fb */
1891 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001892 if (!crtc->fb) {
1893 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1894 ret = -EINVAL;
1895 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001896 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001897 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001898 } else {
1899 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1900 DRM_MODE_OBJECT_FB);
1901 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001902 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1903 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001904 ret = -EINVAL;
1905 goto out;
1906 }
1907 fb = obj_to_fb(obj);
1908 }
1909
1910 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001911 if (!mode) {
1912 ret = -ENOMEM;
1913 goto out;
1914 }
1915
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001916 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1917 if (ret) {
1918 DRM_DEBUG_KMS("Invalid mode\n");
1919 goto out;
1920 }
1921
Dave Airlief453ba02008-11-07 14:05:41 -08001922 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001923
Rob Clark7c80e122012-09-04 16:35:56 +00001924 hdisplay = mode->hdisplay;
1925 vdisplay = mode->vdisplay;
1926
1927 if (crtc->invert_dimensions)
1928 swap(hdisplay, vdisplay);
1929
1930 if (hdisplay > fb->width ||
1931 vdisplay > fb->height ||
1932 crtc_req->x > fb->width - hdisplay ||
1933 crtc_req->y > fb->height - vdisplay) {
1934 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1935 fb->width, fb->height,
1936 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1937 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001938 ret = -ENOSPC;
1939 goto out;
1940 }
Dave Airlief453ba02008-11-07 14:05:41 -08001941 }
1942
1943 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001944 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001945 ret = -EINVAL;
1946 goto out;
1947 }
1948
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001949 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001950 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001951 crtc_req->count_connectors);
1952 ret = -EINVAL;
1953 goto out;
1954 }
1955
1956 if (crtc_req->count_connectors > 0) {
1957 u32 out_id;
1958
1959 /* Avoid unbounded kernel memory allocation */
1960 if (crtc_req->count_connectors > config->num_connector) {
1961 ret = -EINVAL;
1962 goto out;
1963 }
1964
1965 connector_set = kmalloc(crtc_req->count_connectors *
1966 sizeof(struct drm_connector *),
1967 GFP_KERNEL);
1968 if (!connector_set) {
1969 ret = -ENOMEM;
1970 goto out;
1971 }
1972
1973 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001974 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001975 if (get_user(out_id, &set_connectors_ptr[i])) {
1976 ret = -EFAULT;
1977 goto out;
1978 }
1979
1980 obj = drm_mode_object_find(dev, out_id,
1981 DRM_MODE_OBJECT_CONNECTOR);
1982 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001983 DRM_DEBUG_KMS("Connector id %d unknown\n",
1984 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001985 ret = -EINVAL;
1986 goto out;
1987 }
1988 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001989 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1990 connector->base.id,
1991 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001992
1993 connector_set[i] = connector;
1994 }
1995 }
1996
1997 set.crtc = crtc;
1998 set.x = crtc_req->x;
1999 set.y = crtc_req->y;
2000 set.mode = mode;
2001 set.connectors = connector_set;
2002 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002003 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002004 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002005
2006out:
2007 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002008 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002009 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002010 return ret;
2011}
2012
2013int drm_mode_cursor_ioctl(struct drm_device *dev,
2014 void *data, struct drm_file *file_priv)
2015{
2016 struct drm_mode_cursor *req = data;
2017 struct drm_mode_object *obj;
2018 struct drm_crtc *crtc;
2019 int ret = 0;
2020
Dave Airliefb3b06c2011-02-08 13:55:21 +10002021 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2022 return -EINVAL;
2023
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002024 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002025 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002026
Daniel Vetter84849902012-12-02 00:28:11 +01002027 drm_modeset_lock_all(dev);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002028 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002029 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002030 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002031 ret = -EINVAL;
2032 goto out;
2033 }
2034 crtc = obj_to_crtc(obj);
2035
2036 if (req->flags & DRM_MODE_CURSOR_BO) {
2037 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002038 ret = -ENXIO;
2039 goto out;
2040 }
2041 /* Turns off the cursor if handle is 0 */
2042 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2043 req->width, req->height);
2044 }
2045
2046 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2047 if (crtc->funcs->cursor_move) {
2048 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2049 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002050 ret = -EFAULT;
2051 goto out;
2052 }
2053 }
2054out:
Daniel Vetter84849902012-12-02 00:28:11 +01002055 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002056 return ret;
2057}
2058
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002059/* Original addfb only supported RGB formats, so figure out which one */
2060uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2061{
2062 uint32_t fmt;
2063
2064 switch (bpp) {
2065 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002066 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002067 break;
2068 case 16:
2069 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002070 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002071 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002072 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002073 break;
2074 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002075 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002076 break;
2077 case 32:
2078 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002079 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002080 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002081 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002082 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002083 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002084 break;
2085 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002086 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2087 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002088 break;
2089 }
2090
2091 return fmt;
2092}
2093EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2094
Dave Airlief453ba02008-11-07 14:05:41 -08002095/**
2096 * drm_mode_addfb - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002097 * @dev: drm device for the ioctl
2098 * @data: data pointer for the ioctl
2099 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002100 *
Dave Airlief453ba02008-11-07 14:05:41 -08002101 * Add a new FB to the specified CRTC, given a user request.
2102 *
2103 * Called by the user via ioctl.
2104 *
2105 * RETURNS:
2106 * Zero on success, errno on failure.
2107 */
2108int drm_mode_addfb(struct drm_device *dev,
2109 void *data, struct drm_file *file_priv)
2110{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002111 struct drm_mode_fb_cmd *or = data;
2112 struct drm_mode_fb_cmd2 r = {};
2113 struct drm_mode_config *config = &dev->mode_config;
2114 struct drm_framebuffer *fb;
2115 int ret = 0;
2116
2117 /* Use new struct with format internally */
2118 r.fb_id = or->fb_id;
2119 r.width = or->width;
2120 r.height = or->height;
2121 r.pitches[0] = or->pitch;
2122 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2123 r.handles[0] = or->handle;
2124
2125 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2126 return -EINVAL;
2127
Jesse Barnesacb4b992011-11-07 12:03:23 -08002128 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002129 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002130
2131 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002132 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002133
Daniel Vetter84849902012-12-02 00:28:11 +01002134 drm_modeset_lock_all(dev);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002135
2136 /* TODO check buffer is sufficiently large */
2137 /* TODO setup destructor callback */
2138
2139 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2140 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002141 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002142 ret = PTR_ERR(fb);
2143 goto out;
2144 }
2145
2146 or->fb_id = fb->base.id;
2147 list_add(&fb->filp_head, &file_priv->fbs);
2148 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2149
2150out:
Daniel Vetter84849902012-12-02 00:28:11 +01002151 drm_modeset_unlock_all(dev);
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002152 return ret;
2153}
2154
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002155static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002156{
2157 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2158
2159 switch (format) {
2160 case DRM_FORMAT_C8:
2161 case DRM_FORMAT_RGB332:
2162 case DRM_FORMAT_BGR233:
2163 case DRM_FORMAT_XRGB4444:
2164 case DRM_FORMAT_XBGR4444:
2165 case DRM_FORMAT_RGBX4444:
2166 case DRM_FORMAT_BGRX4444:
2167 case DRM_FORMAT_ARGB4444:
2168 case DRM_FORMAT_ABGR4444:
2169 case DRM_FORMAT_RGBA4444:
2170 case DRM_FORMAT_BGRA4444:
2171 case DRM_FORMAT_XRGB1555:
2172 case DRM_FORMAT_XBGR1555:
2173 case DRM_FORMAT_RGBX5551:
2174 case DRM_FORMAT_BGRX5551:
2175 case DRM_FORMAT_ARGB1555:
2176 case DRM_FORMAT_ABGR1555:
2177 case DRM_FORMAT_RGBA5551:
2178 case DRM_FORMAT_BGRA5551:
2179 case DRM_FORMAT_RGB565:
2180 case DRM_FORMAT_BGR565:
2181 case DRM_FORMAT_RGB888:
2182 case DRM_FORMAT_BGR888:
2183 case DRM_FORMAT_XRGB8888:
2184 case DRM_FORMAT_XBGR8888:
2185 case DRM_FORMAT_RGBX8888:
2186 case DRM_FORMAT_BGRX8888:
2187 case DRM_FORMAT_ARGB8888:
2188 case DRM_FORMAT_ABGR8888:
2189 case DRM_FORMAT_RGBA8888:
2190 case DRM_FORMAT_BGRA8888:
2191 case DRM_FORMAT_XRGB2101010:
2192 case DRM_FORMAT_XBGR2101010:
2193 case DRM_FORMAT_RGBX1010102:
2194 case DRM_FORMAT_BGRX1010102:
2195 case DRM_FORMAT_ARGB2101010:
2196 case DRM_FORMAT_ABGR2101010:
2197 case DRM_FORMAT_RGBA1010102:
2198 case DRM_FORMAT_BGRA1010102:
2199 case DRM_FORMAT_YUYV:
2200 case DRM_FORMAT_YVYU:
2201 case DRM_FORMAT_UYVY:
2202 case DRM_FORMAT_VYUY:
2203 case DRM_FORMAT_AYUV:
2204 case DRM_FORMAT_NV12:
2205 case DRM_FORMAT_NV21:
2206 case DRM_FORMAT_NV16:
2207 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002208 case DRM_FORMAT_NV24:
2209 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002210 case DRM_FORMAT_YUV410:
2211 case DRM_FORMAT_YVU410:
2212 case DRM_FORMAT_YUV411:
2213 case DRM_FORMAT_YVU411:
2214 case DRM_FORMAT_YUV420:
2215 case DRM_FORMAT_YVU420:
2216 case DRM_FORMAT_YUV422:
2217 case DRM_FORMAT_YVU422:
2218 case DRM_FORMAT_YUV444:
2219 case DRM_FORMAT_YVU444:
2220 return 0;
2221 default:
2222 return -EINVAL;
2223 }
2224}
2225
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002226static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002227{
2228 int ret, hsub, vsub, num_planes, i;
2229
2230 ret = format_check(r);
2231 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002232 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002233 return ret;
2234 }
2235
2236 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2237 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2238 num_planes = drm_format_num_planes(r->pixel_format);
2239
2240 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002241 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002242 return -EINVAL;
2243 }
2244
2245 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002246 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002247 return -EINVAL;
2248 }
2249
2250 for (i = 0; i < num_planes; i++) {
2251 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002252 unsigned int height = r->height / (i != 0 ? vsub : 1);
2253 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002254
2255 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002256 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002257 return -EINVAL;
2258 }
2259
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002260 if ((uint64_t) width * cpp > UINT_MAX)
2261 return -ERANGE;
2262
2263 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2264 return -ERANGE;
2265
2266 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002267 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002268 return -EINVAL;
2269 }
2270 }
2271
2272 return 0;
2273}
2274
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002275/**
2276 * drm_mode_addfb2 - add an FB to the graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002277 * @dev: drm device for the ioctl
2278 * @data: data pointer for the ioctl
2279 * @file_priv: drm file for the ioctl call
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002280 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002281 * Add a new FB to the specified CRTC, given a user request with format.
2282 *
2283 * Called by the user via ioctl.
2284 *
2285 * RETURNS:
2286 * Zero on success, errno on failure.
2287 */
2288int drm_mode_addfb2(struct drm_device *dev,
2289 void *data, struct drm_file *file_priv)
2290{
2291 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002292 struct drm_mode_config *config = &dev->mode_config;
2293 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002294 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002295
Dave Airliefb3b06c2011-02-08 13:55:21 +10002296 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2297 return -EINVAL;
2298
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002299 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2300 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2301 return -EINVAL;
2302 }
2303
Dave Airlief453ba02008-11-07 14:05:41 -08002304 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002305 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002306 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002307 return -EINVAL;
2308 }
2309 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002310 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002311 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002312 return -EINVAL;
2313 }
2314
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002315 ret = framebuffer_check(r);
2316 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002317 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002318
Daniel Vetter84849902012-12-02 00:28:11 +01002319 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002320
Dave Airlief453ba02008-11-07 14:05:41 -08002321 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002322 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002323 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002324 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002325 goto out;
2326 }
2327
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002328 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002329 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002330 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002331
2332out:
Daniel Vetter84849902012-12-02 00:28:11 +01002333 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002334 return ret;
2335}
2336
2337/**
2338 * drm_mode_rmfb - remove an FB from the configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002339 * @dev: drm device for the ioctl
2340 * @data: data pointer for the ioctl
2341 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002342 *
Dave Airlief453ba02008-11-07 14:05:41 -08002343 * Remove the FB specified by the user.
2344 *
2345 * Called by the user via ioctl.
2346 *
2347 * RETURNS:
2348 * Zero on success, errno on failure.
2349 */
2350int drm_mode_rmfb(struct drm_device *dev,
2351 void *data, struct drm_file *file_priv)
2352{
2353 struct drm_mode_object *obj;
2354 struct drm_framebuffer *fb = NULL;
2355 struct drm_framebuffer *fbl = NULL;
2356 uint32_t *id = data;
2357 int ret = 0;
2358 int found = 0;
2359
Dave Airliefb3b06c2011-02-08 13:55:21 +10002360 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2361 return -EINVAL;
2362
Daniel Vetter84849902012-12-02 00:28:11 +01002363 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002364 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002365 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002366 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002367 ret = -EINVAL;
2368 goto out;
2369 }
2370 fb = obj_to_fb(obj);
2371
2372 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2373 if (fb == fbl)
2374 found = 1;
2375
2376 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002377 ret = -EINVAL;
2378 goto out;
2379 }
2380
Rob Clarkf7eff602012-09-05 21:48:38 +00002381 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002382
2383out:
Daniel Vetter84849902012-12-02 00:28:11 +01002384 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002385 return ret;
2386}
2387
2388/**
2389 * drm_mode_getfb - get FB info
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002390 * @dev: drm device for the ioctl
2391 * @data: data pointer for the ioctl
2392 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002393 *
Dave Airlief453ba02008-11-07 14:05:41 -08002394 * Lookup the FB given its ID and return info about it.
2395 *
2396 * Called by the user via ioctl.
2397 *
2398 * RETURNS:
2399 * Zero on success, errno on failure.
2400 */
2401int drm_mode_getfb(struct drm_device *dev,
2402 void *data, struct drm_file *file_priv)
2403{
2404 struct drm_mode_fb_cmd *r = data;
2405 struct drm_mode_object *obj;
2406 struct drm_framebuffer *fb;
2407 int ret = 0;
2408
Dave Airliefb3b06c2011-02-08 13:55:21 +10002409 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2410 return -EINVAL;
2411
Daniel Vetter84849902012-12-02 00:28:11 +01002412 drm_modeset_lock_all(dev);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002413 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002414 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002415 ret = -EINVAL;
2416 goto out;
2417 }
2418 fb = obj_to_fb(obj);
2419
2420 r->height = fb->height;
2421 r->width = fb->width;
2422 r->depth = fb->depth;
2423 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002424 r->pitch = fb->pitches[0];
Daniel Vetteraf26ef32012-12-13 23:07:50 +01002425 if (fb->funcs->create_handle)
2426 ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
2427 else
2428 ret = -ENODEV;
Dave Airlief453ba02008-11-07 14:05:41 -08002429
2430out:
Daniel Vetter84849902012-12-02 00:28:11 +01002431 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002432 return ret;
2433}
2434
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002435int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2436 void *data, struct drm_file *file_priv)
2437{
2438 struct drm_clip_rect __user *clips_ptr;
2439 struct drm_clip_rect *clips = NULL;
2440 struct drm_mode_fb_dirty_cmd *r = data;
2441 struct drm_mode_object *obj;
2442 struct drm_framebuffer *fb;
2443 unsigned flags;
2444 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002445 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002446
Dave Airliefb3b06c2011-02-08 13:55:21 +10002447 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2448 return -EINVAL;
2449
Daniel Vetter84849902012-12-02 00:28:11 +01002450 drm_modeset_lock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002451 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2452 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002453 ret = -EINVAL;
2454 goto out_err1;
2455 }
2456 fb = obj_to_fb(obj);
2457
2458 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002459 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002460
2461 if (!num_clips != !clips_ptr) {
2462 ret = -EINVAL;
2463 goto out_err1;
2464 }
2465
2466 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2467
2468 /* If userspace annotates copy, clips must come in pairs */
2469 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2470 ret = -EINVAL;
2471 goto out_err1;
2472 }
2473
2474 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002475 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2476 ret = -EINVAL;
2477 goto out_err1;
2478 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002479 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2480 if (!clips) {
2481 ret = -ENOMEM;
2482 goto out_err1;
2483 }
2484
2485 ret = copy_from_user(clips, clips_ptr,
2486 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002487 if (ret) {
2488 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002489 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002490 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002491 }
2492
2493 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002494 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2495 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002496 } else {
2497 ret = -ENOSYS;
2498 goto out_err2;
2499 }
2500
2501out_err2:
2502 kfree(clips);
2503out_err1:
Daniel Vetter84849902012-12-02 00:28:11 +01002504 drm_modeset_unlock_all(dev);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002505 return ret;
2506}
2507
2508
Dave Airlief453ba02008-11-07 14:05:41 -08002509/**
2510 * drm_fb_release - remove and free the FBs on this file
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002511 * @priv: drm file for the ioctl
Dave Airlief453ba02008-11-07 14:05:41 -08002512 *
Dave Airlief453ba02008-11-07 14:05:41 -08002513 * Destroy all the FBs associated with @filp.
2514 *
2515 * Called by the user via ioctl.
2516 *
2517 * RETURNS:
2518 * Zero on success, errno on failure.
2519 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002520void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002521{
Dave Airlief453ba02008-11-07 14:05:41 -08002522 struct drm_device *dev = priv->minor->dev;
2523 struct drm_framebuffer *fb, *tfb;
2524
Daniel Vetter84849902012-12-02 00:28:11 +01002525 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002526 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00002527 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002528 }
Daniel Vetter84849902012-12-02 00:28:11 +01002529 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002530}
2531
2532/**
2533 * drm_mode_attachmode - add a mode to the user mode list
2534 * @dev: DRM device
2535 * @connector: connector to add the mode to
2536 * @mode: mode to add
2537 *
2538 * Add @mode to @connector's user mode list.
2539 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002540static void drm_mode_attachmode(struct drm_device *dev,
2541 struct drm_connector *connector,
2542 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002543{
Dave Airlief453ba02008-11-07 14:05:41 -08002544 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002545}
2546
2547int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002548 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002549{
2550 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002551 int ret = 0;
2552 struct drm_display_mode *dup_mode, *next;
2553 LIST_HEAD(list);
2554
Dave Airlief453ba02008-11-07 14:05:41 -08002555 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2556 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002557 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002558 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002559 dup_mode = drm_mode_duplicate(dev, mode);
2560 if (!dup_mode) {
2561 ret = -ENOMEM;
2562 goto out;
2563 }
2564 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002565 }
2566 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002567
2568 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2569 if (!connector->encoder)
2570 continue;
2571 if (connector->encoder->crtc == crtc)
2572 list_move_tail(list.next, &connector->user_modes);
2573 }
2574
2575 WARN_ON(!list_empty(&list));
2576
2577 out:
2578 list_for_each_entry_safe(dup_mode, next, &list, head)
2579 drm_mode_destroy(dev, dup_mode);
2580
2581 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002582}
2583EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2584
2585static int drm_mode_detachmode(struct drm_device *dev,
2586 struct drm_connector *connector,
2587 struct drm_display_mode *mode)
2588{
2589 int found = 0;
2590 int ret = 0;
2591 struct drm_display_mode *match_mode, *t;
2592
2593 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2594 if (drm_mode_equal(match_mode, mode)) {
2595 list_del(&match_mode->head);
2596 drm_mode_destroy(dev, match_mode);
2597 found = 1;
2598 break;
2599 }
2600 }
2601
2602 if (!found)
2603 ret = -EINVAL;
2604
2605 return ret;
2606}
2607
2608int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2609{
2610 struct drm_connector *connector;
2611
2612 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2613 drm_mode_detachmode(dev, connector, mode);
2614 }
2615 return 0;
2616}
2617EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2618
2619/**
2620 * drm_fb_attachmode - Attach a user mode to an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002621 * @dev: drm device for the ioctl
2622 * @data: data pointer for the ioctl
2623 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002624 *
2625 * This attaches a user specified mode to an connector.
2626 * Called by the user via ioctl.
2627 *
2628 * RETURNS:
2629 * Zero on success, errno on failure.
2630 */
2631int drm_mode_attachmode_ioctl(struct drm_device *dev,
2632 void *data, struct drm_file *file_priv)
2633{
2634 struct drm_mode_mode_cmd *mode_cmd = data;
2635 struct drm_connector *connector;
2636 struct drm_display_mode *mode;
2637 struct drm_mode_object *obj;
2638 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002639 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002640
Dave Airliefb3b06c2011-02-08 13:55:21 +10002641 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2642 return -EINVAL;
2643
Daniel Vetter84849902012-12-02 00:28:11 +01002644 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002645
2646 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2647 if (!obj) {
2648 ret = -EINVAL;
2649 goto out;
2650 }
2651 connector = obj_to_connector(obj);
2652
2653 mode = drm_mode_create(dev);
2654 if (!mode) {
2655 ret = -ENOMEM;
2656 goto out;
2657 }
2658
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002659 ret = drm_crtc_convert_umode(mode, umode);
2660 if (ret) {
2661 DRM_DEBUG_KMS("Invalid mode\n");
2662 drm_mode_destroy(dev, mode);
2663 goto out;
2664 }
Dave Airlief453ba02008-11-07 14:05:41 -08002665
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002666 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002667out:
Daniel Vetter84849902012-12-02 00:28:11 +01002668 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002669 return ret;
2670}
2671
2672
2673/**
2674 * drm_fb_detachmode - Detach a user specified mode from an connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002675 * @dev: drm device for the ioctl
2676 * @data: data pointer for the ioctl
2677 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002678 *
2679 * Called by the user via ioctl.
2680 *
2681 * RETURNS:
2682 * Zero on success, errno on failure.
2683 */
2684int drm_mode_detachmode_ioctl(struct drm_device *dev,
2685 void *data, struct drm_file *file_priv)
2686{
2687 struct drm_mode_object *obj;
2688 struct drm_mode_mode_cmd *mode_cmd = data;
2689 struct drm_connector *connector;
2690 struct drm_display_mode mode;
2691 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002692 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002693
Dave Airliefb3b06c2011-02-08 13:55:21 +10002694 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2695 return -EINVAL;
2696
Daniel Vetter84849902012-12-02 00:28:11 +01002697 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002698
2699 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2700 if (!obj) {
2701 ret = -EINVAL;
2702 goto out;
2703 }
2704 connector = obj_to_connector(obj);
2705
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002706 ret = drm_crtc_convert_umode(&mode, umode);
2707 if (ret) {
2708 DRM_DEBUG_KMS("Invalid mode\n");
2709 goto out;
2710 }
2711
Dave Airlief453ba02008-11-07 14:05:41 -08002712 ret = drm_mode_detachmode(dev, connector, &mode);
2713out:
Daniel Vetter84849902012-12-02 00:28:11 +01002714 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002715 return ret;
2716}
2717
2718struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2719 const char *name, int num_values)
2720{
2721 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002722 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002723
2724 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2725 if (!property)
2726 return NULL;
2727
2728 if (num_values) {
2729 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2730 if (!property->values)
2731 goto fail;
2732 }
2733
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002734 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2735 if (ret)
2736 goto fail;
2737
Dave Airlief453ba02008-11-07 14:05:41 -08002738 property->flags = flags;
2739 property->num_values = num_values;
2740 INIT_LIST_HEAD(&property->enum_blob_list);
2741
Vinson Lee471dd2e2011-11-10 11:55:40 -08002742 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002743 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002744 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2745 }
Dave Airlief453ba02008-11-07 14:05:41 -08002746
2747 list_add_tail(&property->head, &dev->mode_config.property_list);
2748 return property;
2749fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002750 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002751 kfree(property);
2752 return NULL;
2753}
2754EXPORT_SYMBOL(drm_property_create);
2755
Sascha Hauer4a67d392012-02-06 10:58:17 +01002756struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2757 const char *name,
2758 const struct drm_prop_enum_list *props,
2759 int num_values)
2760{
2761 struct drm_property *property;
2762 int i, ret;
2763
2764 flags |= DRM_MODE_PROP_ENUM;
2765
2766 property = drm_property_create(dev, flags, name, num_values);
2767 if (!property)
2768 return NULL;
2769
2770 for (i = 0; i < num_values; i++) {
2771 ret = drm_property_add_enum(property, i,
2772 props[i].type,
2773 props[i].name);
2774 if (ret) {
2775 drm_property_destroy(dev, property);
2776 return NULL;
2777 }
2778 }
2779
2780 return property;
2781}
2782EXPORT_SYMBOL(drm_property_create_enum);
2783
Rob Clark49e27542012-05-17 02:23:26 -06002784struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2785 int flags, const char *name,
2786 const struct drm_prop_enum_list *props,
2787 int num_values)
2788{
2789 struct drm_property *property;
2790 int i, ret;
2791
2792 flags |= DRM_MODE_PROP_BITMASK;
2793
2794 property = drm_property_create(dev, flags, name, num_values);
2795 if (!property)
2796 return NULL;
2797
2798 for (i = 0; i < num_values; i++) {
2799 ret = drm_property_add_enum(property, i,
2800 props[i].type,
2801 props[i].name);
2802 if (ret) {
2803 drm_property_destroy(dev, property);
2804 return NULL;
2805 }
2806 }
2807
2808 return property;
2809}
2810EXPORT_SYMBOL(drm_property_create_bitmask);
2811
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002812struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2813 const char *name,
2814 uint64_t min, uint64_t max)
2815{
2816 struct drm_property *property;
2817
2818 flags |= DRM_MODE_PROP_RANGE;
2819
2820 property = drm_property_create(dev, flags, name, 2);
2821 if (!property)
2822 return NULL;
2823
2824 property->values[0] = min;
2825 property->values[1] = max;
2826
2827 return property;
2828}
2829EXPORT_SYMBOL(drm_property_create_range);
2830
Dave Airlief453ba02008-11-07 14:05:41 -08002831int drm_property_add_enum(struct drm_property *property, int index,
2832 uint64_t value, const char *name)
2833{
2834 struct drm_property_enum *prop_enum;
2835
Rob Clark49e27542012-05-17 02:23:26 -06002836 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2837 return -EINVAL;
2838
2839 /*
2840 * Bitmask enum properties have the additional constraint of values
2841 * from 0 to 63
2842 */
2843 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002844 return -EINVAL;
2845
2846 if (!list_empty(&property->enum_blob_list)) {
2847 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2848 if (prop_enum->value == value) {
2849 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2850 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2851 return 0;
2852 }
2853 }
2854 }
2855
2856 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2857 if (!prop_enum)
2858 return -ENOMEM;
2859
2860 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2861 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2862 prop_enum->value = value;
2863
2864 property->values[index] = value;
2865 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2866 return 0;
2867}
2868EXPORT_SYMBOL(drm_property_add_enum);
2869
2870void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2871{
2872 struct drm_property_enum *prop_enum, *pt;
2873
2874 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2875 list_del(&prop_enum->head);
2876 kfree(prop_enum);
2877 }
2878
2879 if (property->num_values)
2880 kfree(property->values);
2881 drm_mode_object_put(dev, &property->base);
2882 list_del(&property->head);
2883 kfree(property);
2884}
2885EXPORT_SYMBOL(drm_property_destroy);
2886
Paulo Zanonic5431882012-05-15 18:09:02 -03002887void drm_object_attach_property(struct drm_mode_object *obj,
2888 struct drm_property *property,
2889 uint64_t init_val)
2890{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002891 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002892
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002893 if (count == DRM_OBJECT_MAX_PROPERTY) {
2894 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2895 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2896 "you see this message on the same object type.\n",
2897 obj->type);
2898 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002899 }
2900
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002901 obj->properties->ids[count] = property->base.id;
2902 obj->properties->values[count] = init_val;
2903 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002904}
2905EXPORT_SYMBOL(drm_object_attach_property);
2906
2907int drm_object_property_set_value(struct drm_mode_object *obj,
2908 struct drm_property *property, uint64_t val)
2909{
2910 int i;
2911
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002912 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002913 if (obj->properties->ids[i] == property->base.id) {
2914 obj->properties->values[i] = val;
2915 return 0;
2916 }
2917 }
2918
2919 return -EINVAL;
2920}
2921EXPORT_SYMBOL(drm_object_property_set_value);
2922
2923int drm_object_property_get_value(struct drm_mode_object *obj,
2924 struct drm_property *property, uint64_t *val)
2925{
2926 int i;
2927
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002928 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002929 if (obj->properties->ids[i] == property->base.id) {
2930 *val = obj->properties->values[i];
2931 return 0;
2932 }
2933 }
2934
2935 return -EINVAL;
2936}
2937EXPORT_SYMBOL(drm_object_property_get_value);
2938
Dave Airlief453ba02008-11-07 14:05:41 -08002939int drm_mode_getproperty_ioctl(struct drm_device *dev,
2940 void *data, struct drm_file *file_priv)
2941{
2942 struct drm_mode_object *obj;
2943 struct drm_mode_get_property *out_resp = data;
2944 struct drm_property *property;
2945 int enum_count = 0;
2946 int blob_count = 0;
2947 int value_count = 0;
2948 int ret = 0, i;
2949 int copied;
2950 struct drm_property_enum *prop_enum;
2951 struct drm_mode_property_enum __user *enum_ptr;
2952 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002953 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002954 uint64_t __user *values_ptr;
2955 uint32_t __user *blob_length_ptr;
2956
Dave Airliefb3b06c2011-02-08 13:55:21 +10002957 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2958 return -EINVAL;
2959
Daniel Vetter84849902012-12-02 00:28:11 +01002960 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002961 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2962 if (!obj) {
2963 ret = -EINVAL;
2964 goto done;
2965 }
2966 property = obj_to_property(obj);
2967
Rob Clark49e27542012-05-17 02:23:26 -06002968 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002969 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2970 enum_count++;
2971 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2972 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2973 blob_count++;
2974 }
2975
2976 value_count = property->num_values;
2977
2978 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2979 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2980 out_resp->flags = property->flags;
2981
2982 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002983 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002984 for (i = 0; i < value_count; i++) {
2985 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2986 ret = -EFAULT;
2987 goto done;
2988 }
2989 }
2990 }
2991 out_resp->count_values = value_count;
2992
Rob Clark49e27542012-05-17 02:23:26 -06002993 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002994 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2995 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002996 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002997 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2998
2999 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3000 ret = -EFAULT;
3001 goto done;
3002 }
3003
3004 if (copy_to_user(&enum_ptr[copied].name,
3005 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3006 ret = -EFAULT;
3007 goto done;
3008 }
3009 copied++;
3010 }
3011 }
3012 out_resp->count_enum_blobs = enum_count;
3013 }
3014
3015 if (property->flags & DRM_MODE_PROP_BLOB) {
3016 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3017 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003018 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3019 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003020
3021 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3022 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3023 ret = -EFAULT;
3024 goto done;
3025 }
3026
3027 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3028 ret = -EFAULT;
3029 goto done;
3030 }
3031
3032 copied++;
3033 }
3034 }
3035 out_resp->count_enum_blobs = blob_count;
3036 }
3037done:
Daniel Vetter84849902012-12-02 00:28:11 +01003038 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003039 return ret;
3040}
3041
3042static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3043 void *data)
3044{
3045 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003046 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003047
3048 if (!length || !data)
3049 return NULL;
3050
3051 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3052 if (!blob)
3053 return NULL;
3054
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003055 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3056 if (ret) {
3057 kfree(blob);
3058 return NULL;
3059 }
3060
Dave Airlief453ba02008-11-07 14:05:41 -08003061 blob->length = length;
3062
3063 memcpy(blob->data, data, length);
3064
Dave Airlief453ba02008-11-07 14:05:41 -08003065 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3066 return blob;
3067}
3068
3069static void drm_property_destroy_blob(struct drm_device *dev,
3070 struct drm_property_blob *blob)
3071{
3072 drm_mode_object_put(dev, &blob->base);
3073 list_del(&blob->head);
3074 kfree(blob);
3075}
3076
3077int drm_mode_getblob_ioctl(struct drm_device *dev,
3078 void *data, struct drm_file *file_priv)
3079{
3080 struct drm_mode_object *obj;
3081 struct drm_mode_get_blob *out_resp = data;
3082 struct drm_property_blob *blob;
3083 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003084 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003085
Dave Airliefb3b06c2011-02-08 13:55:21 +10003086 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3087 return -EINVAL;
3088
Daniel Vetter84849902012-12-02 00:28:11 +01003089 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003090 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3091 if (!obj) {
3092 ret = -EINVAL;
3093 goto done;
3094 }
3095 blob = obj_to_blob(obj);
3096
3097 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003098 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003099 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3100 ret = -EFAULT;
3101 goto done;
3102 }
3103 }
3104 out_resp->length = blob->length;
3105
3106done:
Daniel Vetter84849902012-12-02 00:28:11 +01003107 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003108 return ret;
3109}
3110
3111int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3112 struct edid *edid)
3113{
3114 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003115 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003116
3117 if (connector->edid_blob_ptr)
3118 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3119
3120 /* Delete edid, when there is none. */
3121 if (!edid) {
3122 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003123 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003124 return ret;
3125 }
3126
Adam Jackson7466f4c2010-03-29 21:43:23 +00003127 size = EDID_LENGTH * (1 + edid->extensions);
3128 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3129 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003130 if (!connector->edid_blob_ptr)
3131 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003132
Rob Clark58495562012-10-11 20:50:56 -05003133 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003134 dev->mode_config.edid_property,
3135 connector->edid_blob_ptr->base.id);
3136
3137 return ret;
3138}
3139EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3140
Paulo Zanoni26a34812012-05-15 18:08:59 -03003141static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003142 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003143{
3144 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3145 return false;
3146 if (property->flags & DRM_MODE_PROP_RANGE) {
3147 if (value < property->values[0] || value > property->values[1])
3148 return false;
3149 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003150 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3151 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003152 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003153 for (i = 0; i < property->num_values; i++)
3154 valid_mask |= (1ULL << property->values[i]);
3155 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003156 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3157 /* Only the driver knows */
3158 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003159 } else {
3160 int i;
3161 for (i = 0; i < property->num_values; i++)
3162 if (property->values[i] == value)
3163 return true;
3164 return false;
3165 }
3166}
3167
Dave Airlief453ba02008-11-07 14:05:41 -08003168int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3169 void *data, struct drm_file *file_priv)
3170{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003171 struct drm_mode_connector_set_property *conn_set_prop = data;
3172 struct drm_mode_obj_set_property obj_set_prop = {
3173 .value = conn_set_prop->value,
3174 .prop_id = conn_set_prop->prop_id,
3175 .obj_id = conn_set_prop->connector_id,
3176 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3177 };
Dave Airlief453ba02008-11-07 14:05:41 -08003178
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003179 /* It does all the locking and checking we need */
3180 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003181}
3182
Paulo Zanonic5431882012-05-15 18:09:02 -03003183static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3184 struct drm_property *property,
3185 uint64_t value)
3186{
3187 int ret = -EINVAL;
3188 struct drm_connector *connector = obj_to_connector(obj);
3189
3190 /* Do DPMS ourselves */
3191 if (property == connector->dev->mode_config.dpms_property) {
3192 if (connector->funcs->dpms)
3193 (*connector->funcs->dpms)(connector, (int)value);
3194 ret = 0;
3195 } else if (connector->funcs->set_property)
3196 ret = connector->funcs->set_property(connector, property, value);
3197
3198 /* store the property value if successful */
3199 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003200 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003201 return ret;
3202}
3203
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003204static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3205 struct drm_property *property,
3206 uint64_t value)
3207{
3208 int ret = -EINVAL;
3209 struct drm_crtc *crtc = obj_to_crtc(obj);
3210
3211 if (crtc->funcs->set_property)
3212 ret = crtc->funcs->set_property(crtc, property, value);
3213 if (!ret)
3214 drm_object_property_set_value(obj, property, value);
3215
3216 return ret;
3217}
3218
Rob Clark4d939142012-05-17 02:23:27 -06003219static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3220 struct drm_property *property,
3221 uint64_t value)
3222{
3223 int ret = -EINVAL;
3224 struct drm_plane *plane = obj_to_plane(obj);
3225
3226 if (plane->funcs->set_property)
3227 ret = plane->funcs->set_property(plane, property, value);
3228 if (!ret)
3229 drm_object_property_set_value(obj, property, value);
3230
3231 return ret;
3232}
3233
Paulo Zanonic5431882012-05-15 18:09:02 -03003234int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3235 struct drm_file *file_priv)
3236{
3237 struct drm_mode_obj_get_properties *arg = data;
3238 struct drm_mode_object *obj;
3239 int ret = 0;
3240 int i;
3241 int copied = 0;
3242 int props_count = 0;
3243 uint32_t __user *props_ptr;
3244 uint64_t __user *prop_values_ptr;
3245
3246 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3247 return -EINVAL;
3248
Daniel Vetter84849902012-12-02 00:28:11 +01003249 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003250
3251 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3252 if (!obj) {
3253 ret = -EINVAL;
3254 goto out;
3255 }
3256 if (!obj->properties) {
3257 ret = -EINVAL;
3258 goto out;
3259 }
3260
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003261 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003262
3263 /* This ioctl is called twice, once to determine how much space is
3264 * needed, and the 2nd time to fill it. */
3265 if ((arg->count_props >= props_count) && props_count) {
3266 copied = 0;
3267 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3268 prop_values_ptr = (uint64_t __user *)(unsigned long)
3269 (arg->prop_values_ptr);
3270 for (i = 0; i < props_count; i++) {
3271 if (put_user(obj->properties->ids[i],
3272 props_ptr + copied)) {
3273 ret = -EFAULT;
3274 goto out;
3275 }
3276 if (put_user(obj->properties->values[i],
3277 prop_values_ptr + copied)) {
3278 ret = -EFAULT;
3279 goto out;
3280 }
3281 copied++;
3282 }
3283 }
3284 arg->count_props = props_count;
3285out:
Daniel Vetter84849902012-12-02 00:28:11 +01003286 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003287 return ret;
3288}
3289
3290int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3291 struct drm_file *file_priv)
3292{
3293 struct drm_mode_obj_set_property *arg = data;
3294 struct drm_mode_object *arg_obj;
3295 struct drm_mode_object *prop_obj;
3296 struct drm_property *property;
3297 int ret = -EINVAL;
3298 int i;
3299
3300 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3301 return -EINVAL;
3302
Daniel Vetter84849902012-12-02 00:28:11 +01003303 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003304
3305 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3306 if (!arg_obj)
3307 goto out;
3308 if (!arg_obj->properties)
3309 goto out;
3310
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003311 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003312 if (arg_obj->properties->ids[i] == arg->prop_id)
3313 break;
3314
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003315 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003316 goto out;
3317
3318 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3319 DRM_MODE_OBJECT_PROPERTY);
3320 if (!prop_obj)
3321 goto out;
3322 property = obj_to_property(prop_obj);
3323
3324 if (!drm_property_change_is_valid(property, arg->value))
3325 goto out;
3326
3327 switch (arg_obj->type) {
3328 case DRM_MODE_OBJECT_CONNECTOR:
3329 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3330 arg->value);
3331 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003332 case DRM_MODE_OBJECT_CRTC:
3333 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3334 break;
Rob Clark4d939142012-05-17 02:23:27 -06003335 case DRM_MODE_OBJECT_PLANE:
3336 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3337 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003338 }
3339
3340out:
Daniel Vetter84849902012-12-02 00:28:11 +01003341 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03003342 return ret;
3343}
3344
Dave Airlief453ba02008-11-07 14:05:41 -08003345int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3346 struct drm_encoder *encoder)
3347{
3348 int i;
3349
3350 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3351 if (connector->encoder_ids[i] == 0) {
3352 connector->encoder_ids[i] = encoder->base.id;
3353 return 0;
3354 }
3355 }
3356 return -ENOMEM;
3357}
3358EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3359
3360void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3361 struct drm_encoder *encoder)
3362{
3363 int i;
3364 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3365 if (connector->encoder_ids[i] == encoder->base.id) {
3366 connector->encoder_ids[i] = 0;
3367 if (connector->encoder == encoder)
3368 connector->encoder = NULL;
3369 break;
3370 }
3371 }
3372}
3373EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3374
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003375int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003376 int gamma_size)
3377{
3378 crtc->gamma_size = gamma_size;
3379
3380 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3381 if (!crtc->gamma_store) {
3382 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003383 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003384 }
3385
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003386 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003387}
3388EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3389
3390int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3391 void *data, struct drm_file *file_priv)
3392{
3393 struct drm_mode_crtc_lut *crtc_lut = data;
3394 struct drm_mode_object *obj;
3395 struct drm_crtc *crtc;
3396 void *r_base, *g_base, *b_base;
3397 int size;
3398 int ret = 0;
3399
Dave Airliefb3b06c2011-02-08 13:55:21 +10003400 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3401 return -EINVAL;
3402
Daniel Vetter84849902012-12-02 00:28:11 +01003403 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003404 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3405 if (!obj) {
3406 ret = -EINVAL;
3407 goto out;
3408 }
3409 crtc = obj_to_crtc(obj);
3410
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003411 if (crtc->funcs->gamma_set == NULL) {
3412 ret = -ENOSYS;
3413 goto out;
3414 }
3415
Dave Airlief453ba02008-11-07 14:05:41 -08003416 /* memcpy into gamma store */
3417 if (crtc_lut->gamma_size != crtc->gamma_size) {
3418 ret = -EINVAL;
3419 goto out;
3420 }
3421
3422 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3423 r_base = crtc->gamma_store;
3424 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3425 ret = -EFAULT;
3426 goto out;
3427 }
3428
3429 g_base = r_base + size;
3430 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3431 ret = -EFAULT;
3432 goto out;
3433 }
3434
3435 b_base = g_base + size;
3436 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3437 ret = -EFAULT;
3438 goto out;
3439 }
3440
James Simmons72034252010-08-03 01:33:19 +01003441 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003442
3443out:
Daniel Vetter84849902012-12-02 00:28:11 +01003444 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003445 return ret;
3446
3447}
3448
3449int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3450 void *data, struct drm_file *file_priv)
3451{
3452 struct drm_mode_crtc_lut *crtc_lut = data;
3453 struct drm_mode_object *obj;
3454 struct drm_crtc *crtc;
3455 void *r_base, *g_base, *b_base;
3456 int size;
3457 int ret = 0;
3458
Dave Airliefb3b06c2011-02-08 13:55:21 +10003459 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3460 return -EINVAL;
3461
Daniel Vetter84849902012-12-02 00:28:11 +01003462 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003463 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3464 if (!obj) {
3465 ret = -EINVAL;
3466 goto out;
3467 }
3468 crtc = obj_to_crtc(obj);
3469
3470 /* memcpy into gamma store */
3471 if (crtc_lut->gamma_size != crtc->gamma_size) {
3472 ret = -EINVAL;
3473 goto out;
3474 }
3475
3476 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3477 r_base = crtc->gamma_store;
3478 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3479 ret = -EFAULT;
3480 goto out;
3481 }
3482
3483 g_base = r_base + size;
3484 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3485 ret = -EFAULT;
3486 goto out;
3487 }
3488
3489 b_base = g_base + size;
3490 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3491 ret = -EFAULT;
3492 goto out;
3493 }
3494out:
Daniel Vetter84849902012-12-02 00:28:11 +01003495 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003496 return ret;
3497}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003498
3499int drm_mode_page_flip_ioctl(struct drm_device *dev,
3500 void *data, struct drm_file *file_priv)
3501{
3502 struct drm_mode_crtc_page_flip *page_flip = data;
3503 struct drm_mode_object *obj;
3504 struct drm_crtc *crtc;
3505 struct drm_framebuffer *fb;
3506 struct drm_pending_vblank_event *e = NULL;
3507 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003508 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003509 int ret = -EINVAL;
3510
3511 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3512 page_flip->reserved != 0)
3513 return -EINVAL;
3514
Daniel Vetter84849902012-12-02 00:28:11 +01003515 drm_modeset_lock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003516 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3517 if (!obj)
3518 goto out;
3519 crtc = obj_to_crtc(obj);
3520
Chris Wilson90c1efd2010-07-17 20:23:26 +01003521 if (crtc->fb == NULL) {
3522 /* The framebuffer is currently unbound, presumably
3523 * due to a hotplug event, that userspace has not
3524 * yet discovered.
3525 */
3526 ret = -EBUSY;
3527 goto out;
3528 }
3529
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003530 if (crtc->funcs->page_flip == NULL)
3531 goto out;
3532
3533 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3534 if (!obj)
3535 goto out;
3536 fb = obj_to_fb(obj);
3537
Rob Clark7c80e122012-09-04 16:35:56 +00003538 hdisplay = crtc->mode.hdisplay;
3539 vdisplay = crtc->mode.vdisplay;
3540
3541 if (crtc->invert_dimensions)
3542 swap(hdisplay, vdisplay);
3543
3544 if (hdisplay > fb->width ||
3545 vdisplay > fb->height ||
3546 crtc->x > fb->width - hdisplay ||
3547 crtc->y > fb->height - vdisplay) {
3548 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3549 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3550 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003551 ret = -ENOSPC;
3552 goto out;
3553 }
3554
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003555 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3556 ret = -ENOMEM;
3557 spin_lock_irqsave(&dev->event_lock, flags);
3558 if (file_priv->event_space < sizeof e->event) {
3559 spin_unlock_irqrestore(&dev->event_lock, flags);
3560 goto out;
3561 }
3562 file_priv->event_space -= sizeof e->event;
3563 spin_unlock_irqrestore(&dev->event_lock, flags);
3564
3565 e = kzalloc(sizeof *e, GFP_KERNEL);
3566 if (e == NULL) {
3567 spin_lock_irqsave(&dev->event_lock, flags);
3568 file_priv->event_space += sizeof e->event;
3569 spin_unlock_irqrestore(&dev->event_lock, flags);
3570 goto out;
3571 }
3572
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003573 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003574 e->event.base.length = sizeof e->event;
3575 e->event.user_data = page_flip->user_data;
3576 e->base.event = &e->event.base;
3577 e->base.file_priv = file_priv;
3578 e->base.destroy =
3579 (void (*) (struct drm_pending_event *)) kfree;
3580 }
3581
3582 ret = crtc->funcs->page_flip(crtc, fb, e);
3583 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003584 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3585 spin_lock_irqsave(&dev->event_lock, flags);
3586 file_priv->event_space += sizeof e->event;
3587 spin_unlock_irqrestore(&dev->event_lock, flags);
3588 kfree(e);
3589 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003590 }
3591
3592out:
Daniel Vetter84849902012-12-02 00:28:11 +01003593 drm_modeset_unlock_all(dev);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003594 return ret;
3595}
Chris Wilsoneb033552011-01-24 15:11:08 +00003596
3597void drm_mode_config_reset(struct drm_device *dev)
3598{
3599 struct drm_crtc *crtc;
3600 struct drm_encoder *encoder;
3601 struct drm_connector *connector;
3602
3603 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3604 if (crtc->funcs->reset)
3605 crtc->funcs->reset(crtc);
3606
3607 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3608 if (encoder->funcs->reset)
3609 encoder->funcs->reset(encoder);
3610
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003611 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3612 connector->status = connector_status_unknown;
3613
Chris Wilsoneb033552011-01-24 15:11:08 +00003614 if (connector->funcs->reset)
3615 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003616 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003617}
3618EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003619
3620int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3621 void *data, struct drm_file *file_priv)
3622{
3623 struct drm_mode_create_dumb *args = data;
3624
3625 if (!dev->driver->dumb_create)
3626 return -ENOSYS;
3627 return dev->driver->dumb_create(file_priv, dev, args);
3628}
3629
3630int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3631 void *data, struct drm_file *file_priv)
3632{
3633 struct drm_mode_map_dumb *args = data;
3634
3635 /* call driver ioctl to get mmap offset */
3636 if (!dev->driver->dumb_map_offset)
3637 return -ENOSYS;
3638
3639 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3640}
3641
3642int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3643 void *data, struct drm_file *file_priv)
3644{
3645 struct drm_mode_destroy_dumb *args = data;
3646
3647 if (!dev->driver->dumb_destroy)
3648 return -ENOSYS;
3649
3650 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3651}
Dave Airlie248dbc22011-11-29 20:02:54 +00003652
3653/*
3654 * Just need to support RGB formats here for compat with code that doesn't
3655 * use pixel formats directly yet.
3656 */
3657void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3658 int *bpp)
3659{
3660 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003661 case DRM_FORMAT_RGB332:
3662 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003663 *depth = 8;
3664 *bpp = 8;
3665 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003666 case DRM_FORMAT_XRGB1555:
3667 case DRM_FORMAT_XBGR1555:
3668 case DRM_FORMAT_RGBX5551:
3669 case DRM_FORMAT_BGRX5551:
3670 case DRM_FORMAT_ARGB1555:
3671 case DRM_FORMAT_ABGR1555:
3672 case DRM_FORMAT_RGBA5551:
3673 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003674 *depth = 15;
3675 *bpp = 16;
3676 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003677 case DRM_FORMAT_RGB565:
3678 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003679 *depth = 16;
3680 *bpp = 16;
3681 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003682 case DRM_FORMAT_RGB888:
3683 case DRM_FORMAT_BGR888:
3684 *depth = 24;
3685 *bpp = 24;
3686 break;
3687 case DRM_FORMAT_XRGB8888:
3688 case DRM_FORMAT_XBGR8888:
3689 case DRM_FORMAT_RGBX8888:
3690 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003691 *depth = 24;
3692 *bpp = 32;
3693 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003694 case DRM_FORMAT_XRGB2101010:
3695 case DRM_FORMAT_XBGR2101010:
3696 case DRM_FORMAT_RGBX1010102:
3697 case DRM_FORMAT_BGRX1010102:
3698 case DRM_FORMAT_ARGB2101010:
3699 case DRM_FORMAT_ABGR2101010:
3700 case DRM_FORMAT_RGBA1010102:
3701 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003702 *depth = 30;
3703 *bpp = 32;
3704 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003705 case DRM_FORMAT_ARGB8888:
3706 case DRM_FORMAT_ABGR8888:
3707 case DRM_FORMAT_RGBA8888:
3708 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003709 *depth = 32;
3710 *bpp = 32;
3711 break;
3712 default:
3713 DRM_DEBUG_KMS("unsupported pixel format\n");
3714 *depth = 0;
3715 *bpp = 0;
3716 break;
3717 }
3718}
3719EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003720
3721/**
3722 * drm_format_num_planes - get the number of planes for format
3723 * @format: pixel format (DRM_FORMAT_*)
3724 *
3725 * RETURNS:
3726 * The number of planes used by the specified pixel format.
3727 */
3728int drm_format_num_planes(uint32_t format)
3729{
3730 switch (format) {
3731 case DRM_FORMAT_YUV410:
3732 case DRM_FORMAT_YVU410:
3733 case DRM_FORMAT_YUV411:
3734 case DRM_FORMAT_YVU411:
3735 case DRM_FORMAT_YUV420:
3736 case DRM_FORMAT_YVU420:
3737 case DRM_FORMAT_YUV422:
3738 case DRM_FORMAT_YVU422:
3739 case DRM_FORMAT_YUV444:
3740 case DRM_FORMAT_YVU444:
3741 return 3;
3742 case DRM_FORMAT_NV12:
3743 case DRM_FORMAT_NV21:
3744 case DRM_FORMAT_NV16:
3745 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003746 case DRM_FORMAT_NV24:
3747 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003748 return 2;
3749 default:
3750 return 1;
3751 }
3752}
3753EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003754
3755/**
3756 * drm_format_plane_cpp - determine the bytes per pixel value
3757 * @format: pixel format (DRM_FORMAT_*)
3758 * @plane: plane index
3759 *
3760 * RETURNS:
3761 * The bytes per pixel value for the specified plane.
3762 */
3763int drm_format_plane_cpp(uint32_t format, int plane)
3764{
3765 unsigned int depth;
3766 int bpp;
3767
3768 if (plane >= drm_format_num_planes(format))
3769 return 0;
3770
3771 switch (format) {
3772 case DRM_FORMAT_YUYV:
3773 case DRM_FORMAT_YVYU:
3774 case DRM_FORMAT_UYVY:
3775 case DRM_FORMAT_VYUY:
3776 return 2;
3777 case DRM_FORMAT_NV12:
3778 case DRM_FORMAT_NV21:
3779 case DRM_FORMAT_NV16:
3780 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003781 case DRM_FORMAT_NV24:
3782 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003783 return plane ? 2 : 1;
3784 case DRM_FORMAT_YUV410:
3785 case DRM_FORMAT_YVU410:
3786 case DRM_FORMAT_YUV411:
3787 case DRM_FORMAT_YVU411:
3788 case DRM_FORMAT_YUV420:
3789 case DRM_FORMAT_YVU420:
3790 case DRM_FORMAT_YUV422:
3791 case DRM_FORMAT_YVU422:
3792 case DRM_FORMAT_YUV444:
3793 case DRM_FORMAT_YVU444:
3794 return 1;
3795 default:
3796 drm_fb_get_bpp_depth(format, &depth, &bpp);
3797 return bpp >> 3;
3798 }
3799}
3800EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003801
3802/**
3803 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3804 * @format: pixel format (DRM_FORMAT_*)
3805 *
3806 * RETURNS:
3807 * The horizontal chroma subsampling factor for the
3808 * specified pixel format.
3809 */
3810int drm_format_horz_chroma_subsampling(uint32_t format)
3811{
3812 switch (format) {
3813 case DRM_FORMAT_YUV411:
3814 case DRM_FORMAT_YVU411:
3815 case DRM_FORMAT_YUV410:
3816 case DRM_FORMAT_YVU410:
3817 return 4;
3818 case DRM_FORMAT_YUYV:
3819 case DRM_FORMAT_YVYU:
3820 case DRM_FORMAT_UYVY:
3821 case DRM_FORMAT_VYUY:
3822 case DRM_FORMAT_NV12:
3823 case DRM_FORMAT_NV21:
3824 case DRM_FORMAT_NV16:
3825 case DRM_FORMAT_NV61:
3826 case DRM_FORMAT_YUV422:
3827 case DRM_FORMAT_YVU422:
3828 case DRM_FORMAT_YUV420:
3829 case DRM_FORMAT_YVU420:
3830 return 2;
3831 default:
3832 return 1;
3833 }
3834}
3835EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3836
3837/**
3838 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3839 * @format: pixel format (DRM_FORMAT_*)
3840 *
3841 * RETURNS:
3842 * The vertical chroma subsampling factor for the
3843 * specified pixel format.
3844 */
3845int drm_format_vert_chroma_subsampling(uint32_t format)
3846{
3847 switch (format) {
3848 case DRM_FORMAT_YUV410:
3849 case DRM_FORMAT_YVU410:
3850 return 4;
3851 case DRM_FORMAT_YUV420:
3852 case DRM_FORMAT_YVU420:
3853 case DRM_FORMAT_NV12:
3854 case DRM_FORMAT_NV21:
3855 return 2;
3856 default:
3857 return 1;
3858 }
3859}
3860EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);