blob: b970e41478621fc5f1fad9ad958c5581d727c382 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
36#include <drm/drm_crtc.h>
37#include <drm/drm_edid.h>
38#include <drm/drm_fourcc.h>
Dave Airlief453ba02008-11-07 14:05:41 -080039
Dave Airlief453ba02008-11-07 14:05:41 -080040/* Avoid boilerplate. I'm tired of typing. */
41#define DRM_ENUM_NAME_FN(fnname, list) \
42 char *fnname(int val) \
43 { \
44 int i; \
45 for (i = 0; i < ARRAY_SIZE(list); i++) { \
46 if (list[i].type == val) \
47 return list[i].name; \
48 } \
49 return "(unknown)"; \
50 }
51
52/*
53 * Global properties
54 */
55static struct drm_prop_enum_list drm_dpms_enum_list[] =
56{ { DRM_MODE_DPMS_ON, "On" },
57 { DRM_MODE_DPMS_STANDBY, "Standby" },
58 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
59 { DRM_MODE_DPMS_OFF, "Off" }
60};
61
62DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
63
64/*
65 * Optional properties
66 */
67static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
68{
Jesse Barnes53bd8382009-07-01 10:04:40 -070069 { DRM_MODE_SCALE_NONE, "None" },
70 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
71 { DRM_MODE_SCALE_CENTER, "Center" },
72 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080073};
74
75static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
76{
77 { DRM_MODE_DITHERING_OFF, "Off" },
78 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100079 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080080};
81
82/*
83 * Non-global properties, but "required" for certain connectors.
84 */
85static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
86{
87 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
88 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
89 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
90};
91
92DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
93
94static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
95{
96 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
97 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
98 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
99};
100
101DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
102 drm_dvi_i_subconnector_enum_list)
103
104static struct drm_prop_enum_list drm_tv_select_enum_list[] =
105{
106 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
107 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
108 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200110 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800111};
112
113DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
114
115static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
116{
117 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200121 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800122};
123
124DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
125 drm_tv_subconnector_enum_list)
126
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000127static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
128 { DRM_MODE_DIRTY_OFF, "Off" },
129 { DRM_MODE_DIRTY_ON, "On" },
130 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
131};
132
133DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
134 drm_dirty_info_enum_list)
135
Dave Airlief453ba02008-11-07 14:05:41 -0800136struct drm_conn_prop_enum_list {
137 int type;
138 char *name;
139 int count;
140};
141
142/*
143 * Connector and encoder types.
144 */
145static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
146{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
147 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
148 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
149 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
150 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
151 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
152 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
153 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
154 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500155 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
156 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
157 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
158 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200159 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500160 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200161 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800162};
163
164static struct drm_prop_enum_list drm_encoder_enum_list[] =
165{ { DRM_MODE_ENCODER_NONE, "None" },
166 { DRM_MODE_ENCODER_DAC, "DAC" },
167 { DRM_MODE_ENCODER_TMDS, "TMDS" },
168 { DRM_MODE_ENCODER_LVDS, "LVDS" },
169 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200170 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800171};
172
173char *drm_get_encoder_name(struct drm_encoder *encoder)
174{
175 static char buf[32];
176
177 snprintf(buf, 32, "%s-%d",
178 drm_encoder_enum_list[encoder->encoder_type].name,
179 encoder->base.id);
180 return buf;
181}
Dave Airlie13a81952009-09-07 15:45:33 +1000182EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800183
184char *drm_get_connector_name(struct drm_connector *connector)
185{
186 static char buf[32];
187
188 snprintf(buf, 32, "%s-%d",
189 drm_connector_enum_list[connector->connector_type].name,
190 connector->connector_type_id);
191 return buf;
192}
193EXPORT_SYMBOL(drm_get_connector_name);
194
195char *drm_get_connector_status_name(enum drm_connector_status status)
196{
197 if (status == connector_status_connected)
198 return "connected";
199 else if (status == connector_status_disconnected)
200 return "disconnected";
201 else
202 return "unknown";
203}
204
205/**
206 * drm_mode_object_get - allocate a new identifier
207 * @dev: DRM device
208 * @ptr: object pointer, used to generate unique ID
209 * @type: object type
210 *
Dave Airlief453ba02008-11-07 14:05:41 -0800211 * Create a unique identifier based on @ptr in @dev's identifier space. Used
212 * for tracking modes, CRTCs and connectors.
213 *
214 * RETURNS:
215 * New unique (relative to other objects in @dev) integer identifier for the
216 * object.
217 */
218static int drm_mode_object_get(struct drm_device *dev,
219 struct drm_mode_object *obj, uint32_t obj_type)
220{
221 int new_id = 0;
222 int ret;
223
Dave Airlief453ba02008-11-07 14:05:41 -0800224again:
225 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
226 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200227 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800228 }
229
Jesse Barnesad2563c2009-01-19 17:21:45 +1000230 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800231 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000232 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800233 if (ret == -EAGAIN)
234 goto again;
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200235 else if (ret)
236 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800237
238 obj->id = new_id;
239 obj->type = obj_type;
240 return 0;
241}
242
243/**
244 * drm_mode_object_put - free an identifer
245 * @dev: DRM device
246 * @id: ID to free
247 *
Dave Airlief453ba02008-11-07 14:05:41 -0800248 * Free @id from @dev's unique identifier pool.
249 */
250static void drm_mode_object_put(struct drm_device *dev,
251 struct drm_mode_object *object)
252{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000253 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800254 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000255 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800256}
257
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200258struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
259 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800260{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000261 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800262
Jesse Barnesad2563c2009-01-19 17:21:45 +1000263 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800264 obj = idr_find(&dev->mode_config.crtc_idr, id);
265 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000266 obj = NULL;
267 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800268
269 return obj;
270}
271EXPORT_SYMBOL(drm_mode_object_find);
272
273/**
Dave Airlief453ba02008-11-07 14:05:41 -0800274 * drm_framebuffer_init - initialize a framebuffer
275 * @dev: DRM device
276 *
Dave Airlief453ba02008-11-07 14:05:41 -0800277 * Allocates an ID for the framebuffer's parent mode object, sets its mode
278 * functions & device file and adds it to the master fd list.
279 *
280 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200281 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800282 */
283int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
284 const struct drm_framebuffer_funcs *funcs)
285{
286 int ret;
287
Rob Clarkf7eff602012-09-05 21:48:38 +0000288 kref_init(&fb->refcount);
289
Dave Airlief453ba02008-11-07 14:05:41 -0800290 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200291 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800292 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800293
294 fb->dev = dev;
295 fb->funcs = funcs;
296 dev->mode_config.num_fb++;
297 list_add(&fb->head, &dev->mode_config.fb_list);
298
299 return 0;
300}
301EXPORT_SYMBOL(drm_framebuffer_init);
302
Rob Clarkf7eff602012-09-05 21:48:38 +0000303static void drm_framebuffer_free(struct kref *kref)
304{
305 struct drm_framebuffer *fb =
306 container_of(kref, struct drm_framebuffer, refcount);
307 fb->funcs->destroy(fb);
308}
309
310/**
311 * drm_framebuffer_unreference - unref a framebuffer
Rob Clarkf7eff602012-09-05 21:48:38 +0000312 */
313void drm_framebuffer_unreference(struct drm_framebuffer *fb)
314{
315 struct drm_device *dev = fb->dev;
316 DRM_DEBUG("FB ID: %d\n", fb->base.id);
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100317 WARN_ON(!drm_modeset_is_locked(dev));
Rob Clarkf7eff602012-09-05 21:48:38 +0000318 kref_put(&fb->refcount, drm_framebuffer_free);
319}
320EXPORT_SYMBOL(drm_framebuffer_unreference);
321
322/**
323 * drm_framebuffer_reference - incr the fb refcnt
324 */
325void drm_framebuffer_reference(struct drm_framebuffer *fb)
326{
327 DRM_DEBUG("FB ID: %d\n", fb->base.id);
328 kref_get(&fb->refcount);
329}
330EXPORT_SYMBOL(drm_framebuffer_reference);
331
Dave Airlief453ba02008-11-07 14:05:41 -0800332/**
333 * drm_framebuffer_cleanup - remove a framebuffer object
334 * @fb: framebuffer to remove
335 *
Dave Airlief453ba02008-11-07 14:05:41 -0800336 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
337 * it, setting it to NULL.
338 */
339void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
340{
341 struct drm_device *dev = fb->dev;
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100342
Rob Clarkf7eff602012-09-05 21:48:38 +0000343 /*
344 * This could be moved to drm_framebuffer_remove(), but for
345 * debugging is nice to keep around the list of fb's that are
346 * no longer associated w/ a drm_file but are not unreferenced
347 * yet. (i915 and omapdrm have debugfs files which will show
348 * this.)
349 */
350 drm_mode_object_put(dev, &fb->base);
351 list_del(&fb->head);
352 dev->mode_config.num_fb--;
353}
354EXPORT_SYMBOL(drm_framebuffer_cleanup);
355
356/**
357 * drm_framebuffer_remove - remove and unreference a framebuffer object
358 * @fb: framebuffer to remove
359 *
Rob Clarkf7eff602012-09-05 21:48:38 +0000360 * Scans all the CRTCs and planes in @dev's mode_config. If they're
361 * using @fb, removes it, setting it to NULL.
362 */
363void drm_framebuffer_remove(struct drm_framebuffer *fb)
364{
365 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800366 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800367 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000368 struct drm_mode_set set;
369 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800370
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100371 WARN_ON(!drm_modeset_is_locked(dev));
372
Dave Airlief453ba02008-11-07 14:05:41 -0800373 /* remove from any CRTC */
374 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000375 if (crtc->fb == fb) {
376 /* should turn off the crtc */
377 memset(&set, 0, sizeof(struct drm_mode_set));
378 set.crtc = crtc;
379 set.fb = NULL;
380 ret = crtc->funcs->set_config(&set);
381 if (ret)
382 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
383 }
Dave Airlief453ba02008-11-07 14:05:41 -0800384 }
385
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800386 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
387 if (plane->fb == fb) {
388 /* should turn off the crtc */
389 ret = plane->funcs->disable_plane(plane);
390 if (ret)
391 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600392 /* disconnect the plane from the fb and crtc: */
393 plane->fb = NULL;
394 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800395 }
396 }
397
Rob Clarkf7eff602012-09-05 21:48:38 +0000398 list_del(&fb->filp_head);
399
400 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800401}
Rob Clarkf7eff602012-09-05 21:48:38 +0000402EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800403
404/**
405 * drm_crtc_init - Initialise a new CRTC object
406 * @dev: DRM device
407 * @crtc: CRTC object to init
408 * @funcs: callbacks for the new CRTC
409 *
Dave Airlief453ba02008-11-07 14:05:41 -0800410 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200411 *
412 * RETURNS:
413 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800414 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200415int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800416 const struct drm_crtc_funcs *funcs)
417{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200418 int ret;
419
Dave Airlief453ba02008-11-07 14:05:41 -0800420 crtc->dev = dev;
421 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000422 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800423
424 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200425
426 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
427 if (ret)
428 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800429
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300430 crtc->base.properties = &crtc->properties;
431
Dave Airlief453ba02008-11-07 14:05:41 -0800432 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
433 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200434
435 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800436 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200437
438 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800439}
440EXPORT_SYMBOL(drm_crtc_init);
441
442/**
443 * drm_crtc_cleanup - Cleans up the core crtc usage.
444 * @crtc: CRTC to cleanup
445 *
Dave Airlief453ba02008-11-07 14:05:41 -0800446 * Cleanup @crtc. Removes from drm modesetting space
447 * does NOT free object, caller does that.
448 */
449void drm_crtc_cleanup(struct drm_crtc *crtc)
450{
451 struct drm_device *dev = crtc->dev;
452
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000453 kfree(crtc->gamma_store);
454 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800455
456 drm_mode_object_put(dev, &crtc->base);
457 list_del(&crtc->head);
458 dev->mode_config.num_crtc--;
459}
460EXPORT_SYMBOL(drm_crtc_cleanup);
461
462/**
463 * drm_mode_probed_add - add a mode to a connector's probed mode list
464 * @connector: connector the new mode
465 * @mode: mode data
466 *
Dave Airlief453ba02008-11-07 14:05:41 -0800467 * Add @mode to @connector's mode list for later use.
468 */
469void drm_mode_probed_add(struct drm_connector *connector,
470 struct drm_display_mode *mode)
471{
472 list_add(&mode->head, &connector->probed_modes);
473}
474EXPORT_SYMBOL(drm_mode_probed_add);
475
476/**
477 * drm_mode_remove - remove and free a mode
478 * @connector: connector list to modify
479 * @mode: mode to remove
480 *
Dave Airlief453ba02008-11-07 14:05:41 -0800481 * Remove @mode from @connector's mode list, then free it.
482 */
483void drm_mode_remove(struct drm_connector *connector,
484 struct drm_display_mode *mode)
485{
486 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100487 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800488}
489EXPORT_SYMBOL(drm_mode_remove);
490
491/**
492 * drm_connector_init - Init a preallocated connector
493 * @dev: DRM device
494 * @connector: the connector to init
495 * @funcs: callbacks for this connector
496 * @name: user visible name of the connector
497 *
Dave Airlief453ba02008-11-07 14:05:41 -0800498 * Initialises a preallocated connector. Connectors should be
499 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200500 *
501 * RETURNS:
502 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800503 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200504int drm_connector_init(struct drm_device *dev,
505 struct drm_connector *connector,
506 const struct drm_connector_funcs *funcs,
507 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800508{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200509 int ret;
510
Dave Airlief453ba02008-11-07 14:05:41 -0800511 mutex_lock(&dev->mode_config.mutex);
512
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200513 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
514 if (ret)
515 goto out;
516
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300517 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800518 connector->dev = dev;
519 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800520 connector->connector_type = connector_type;
521 connector->connector_type_id =
522 ++drm_connector_enum_list[connector_type].count; /* TODO */
523 INIT_LIST_HEAD(&connector->user_modes);
524 INIT_LIST_HEAD(&connector->probed_modes);
525 INIT_LIST_HEAD(&connector->modes);
526 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000527 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800528
529 list_add_tail(&connector->head, &dev->mode_config.connector_list);
530 dev->mode_config.num_connector++;
531
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200532 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500533 drm_object_attach_property(&connector->base,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200534 dev->mode_config.edid_property,
535 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800536
Rob Clark58495562012-10-11 20:50:56 -0500537 drm_object_attach_property(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -0800538 dev->mode_config.dpms_property, 0);
539
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200540 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800541 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200542
543 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800544}
545EXPORT_SYMBOL(drm_connector_init);
546
547/**
548 * drm_connector_cleanup - cleans up an initialised connector
549 * @connector: connector to cleanup
550 *
Dave Airlief453ba02008-11-07 14:05:41 -0800551 * Cleans up the connector but doesn't free the object.
552 */
553void drm_connector_cleanup(struct drm_connector *connector)
554{
555 struct drm_device *dev = connector->dev;
556 struct drm_display_mode *mode, *t;
557
558 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
559 drm_mode_remove(connector, mode);
560
561 list_for_each_entry_safe(mode, t, &connector->modes, head)
562 drm_mode_remove(connector, mode);
563
564 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
565 drm_mode_remove(connector, mode);
566
Dave Airlief453ba02008-11-07 14:05:41 -0800567 drm_mode_object_put(dev, &connector->base);
568 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000569 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800570}
571EXPORT_SYMBOL(drm_connector_cleanup);
572
Dave Airliecbc7e222012-02-20 14:16:40 +0000573void drm_connector_unplug_all(struct drm_device *dev)
574{
575 struct drm_connector *connector;
576
577 /* taking the mode config mutex ends up in a clash with sysfs */
578 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
579 drm_sysfs_connector_remove(connector);
580
581}
582EXPORT_SYMBOL(drm_connector_unplug_all);
583
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200584int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000585 struct drm_encoder *encoder,
586 const struct drm_encoder_funcs *funcs,
587 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800588{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200589 int ret;
590
Dave Airlief453ba02008-11-07 14:05:41 -0800591 mutex_lock(&dev->mode_config.mutex);
592
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200593 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
594 if (ret)
595 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800596
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200597 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800598 encoder->encoder_type = encoder_type;
599 encoder->funcs = funcs;
600
601 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
602 dev->mode_config.num_encoder++;
603
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200604 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800605 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200606
607 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800608}
609EXPORT_SYMBOL(drm_encoder_init);
610
611void drm_encoder_cleanup(struct drm_encoder *encoder)
612{
613 struct drm_device *dev = encoder->dev;
614 mutex_lock(&dev->mode_config.mutex);
615 drm_mode_object_put(dev, &encoder->base);
616 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000617 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800618 mutex_unlock(&dev->mode_config.mutex);
619}
620EXPORT_SYMBOL(drm_encoder_cleanup);
621
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800622int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
623 unsigned long possible_crtcs,
624 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600625 const uint32_t *formats, uint32_t format_count,
626 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800627{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200628 int ret;
629
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800630 mutex_lock(&dev->mode_config.mutex);
631
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200632 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
633 if (ret)
634 goto out;
635
Rob Clark4d939142012-05-17 02:23:27 -0600636 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800637 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800638 plane->funcs = funcs;
639 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
640 GFP_KERNEL);
641 if (!plane->format_types) {
642 DRM_DEBUG_KMS("out of memory when allocating plane\n");
643 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200644 ret = -ENOMEM;
645 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800646 }
647
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800648 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800649 plane->format_count = format_count;
650 plane->possible_crtcs = possible_crtcs;
651
Rob Clark0a7eb242011-12-13 20:19:36 -0600652 /* private planes are not exposed to userspace, but depending on
653 * display hardware, might be convenient to allow sharing programming
654 * for the scanout engine with the crtc implementation.
655 */
656 if (!priv) {
657 list_add_tail(&plane->head, &dev->mode_config.plane_list);
658 dev->mode_config.num_plane++;
659 } else {
660 INIT_LIST_HEAD(&plane->head);
661 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800662
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200663 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800664 mutex_unlock(&dev->mode_config.mutex);
665
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200666 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800667}
668EXPORT_SYMBOL(drm_plane_init);
669
670void drm_plane_cleanup(struct drm_plane *plane)
671{
672 struct drm_device *dev = plane->dev;
673
674 mutex_lock(&dev->mode_config.mutex);
675 kfree(plane->format_types);
676 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600677 /* if not added to a list, it must be a private plane */
678 if (!list_empty(&plane->head)) {
679 list_del(&plane->head);
680 dev->mode_config.num_plane--;
681 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800682 mutex_unlock(&dev->mode_config.mutex);
683}
684EXPORT_SYMBOL(drm_plane_cleanup);
685
Dave Airlief453ba02008-11-07 14:05:41 -0800686/**
687 * drm_mode_create - create a new display mode
688 * @dev: DRM device
689 *
Dave Airlief453ba02008-11-07 14:05:41 -0800690 * Create a new drm_display_mode, give it an ID, and return it.
691 *
692 * RETURNS:
693 * Pointer to new mode on success, NULL on error.
694 */
695struct drm_display_mode *drm_mode_create(struct drm_device *dev)
696{
697 struct drm_display_mode *nmode;
698
699 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
700 if (!nmode)
701 return NULL;
702
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200703 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
704 kfree(nmode);
705 return NULL;
706 }
707
Dave Airlief453ba02008-11-07 14:05:41 -0800708 return nmode;
709}
710EXPORT_SYMBOL(drm_mode_create);
711
712/**
713 * drm_mode_destroy - remove a mode
714 * @dev: DRM device
715 * @mode: mode to remove
716 *
Dave Airlief453ba02008-11-07 14:05:41 -0800717 * Free @mode's unique identifier, then free it.
718 */
719void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
720{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200721 if (!mode)
722 return;
723
Dave Airlief453ba02008-11-07 14:05:41 -0800724 drm_mode_object_put(dev, &mode->base);
725
726 kfree(mode);
727}
728EXPORT_SYMBOL(drm_mode_destroy);
729
730static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
731{
732 struct drm_property *edid;
733 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800734
735 /*
736 * Standard properties (apply to all connectors)
737 */
738 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
739 DRM_MODE_PROP_IMMUTABLE,
740 "EDID", 0);
741 dev->mode_config.edid_property = edid;
742
Sascha Hauer4a67d392012-02-06 10:58:17 +0100743 dpms = drm_property_create_enum(dev, 0,
744 "DPMS", drm_dpms_enum_list,
745 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800746 dev->mode_config.dpms_property = dpms;
747
748 return 0;
749}
750
751/**
752 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
753 * @dev: DRM device
754 *
755 * Called by a driver the first time a DVI-I connector is made.
756 */
757int drm_mode_create_dvi_i_properties(struct drm_device *dev)
758{
759 struct drm_property *dvi_i_selector;
760 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800761
762 if (dev->mode_config.dvi_i_select_subconnector_property)
763 return 0;
764
765 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100766 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800767 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100768 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800769 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800770 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
771
Sascha Hauer4a67d392012-02-06 10:58:17 +0100772 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800773 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100774 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800775 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800776 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
777
778 return 0;
779}
780EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
781
782/**
783 * drm_create_tv_properties - create TV specific connector properties
784 * @dev: DRM device
785 * @num_modes: number of different TV formats (modes) supported
786 * @modes: array of pointers to strings containing name of each format
787 *
788 * Called by a driver's TV initialization routine, this function creates
789 * the TV specific connector properties for a given device. Caller is
790 * responsible for allocating a list of format names and passing them to
791 * this routine.
792 */
793int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
794 char *modes[])
795{
796 struct drm_property *tv_selector;
797 struct drm_property *tv_subconnector;
798 int i;
799
800 if (dev->mode_config.tv_select_subconnector_property)
801 return 0;
802
803 /*
804 * Basic connector properties
805 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100806 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800807 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100808 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800809 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800810 dev->mode_config.tv_select_subconnector_property = tv_selector;
811
812 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100813 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
814 "subconnector",
815 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800816 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800817 dev->mode_config.tv_subconnector_property = tv_subconnector;
818
819 /*
820 * Other, TV specific properties: margins & TV modes.
821 */
822 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100823 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800824
825 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100826 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800827
828 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100829 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800830
831 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100832 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800833
834 dev->mode_config.tv_mode_property =
835 drm_property_create(dev, DRM_MODE_PROP_ENUM,
836 "mode", num_modes);
837 for (i = 0; i < num_modes; i++)
838 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
839 i, modes[i]);
840
Francisco Jerezb6b79022009-08-02 04:19:20 +0200841 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100842 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200843
844 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100845 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200846
847 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100848 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200849
Francisco Jereza75f0232009-08-12 02:30:10 +0200850 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100851 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200852
853 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100854 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200855
856 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100857 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200858
Dave Airlief453ba02008-11-07 14:05:41 -0800859 return 0;
860}
861EXPORT_SYMBOL(drm_mode_create_tv_properties);
862
863/**
864 * drm_mode_create_scaling_mode_property - create scaling mode property
865 * @dev: DRM device
866 *
867 * Called by a driver the first time it's needed, must be attached to desired
868 * connectors.
869 */
870int drm_mode_create_scaling_mode_property(struct drm_device *dev)
871{
872 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800873
874 if (dev->mode_config.scaling_mode_property)
875 return 0;
876
877 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100878 drm_property_create_enum(dev, 0, "scaling mode",
879 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800880 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800881
882 dev->mode_config.scaling_mode_property = scaling_mode;
883
884 return 0;
885}
886EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
887
888/**
889 * drm_mode_create_dithering_property - create dithering property
890 * @dev: DRM device
891 *
892 * Called by a driver the first time it's needed, must be attached to desired
893 * connectors.
894 */
895int drm_mode_create_dithering_property(struct drm_device *dev)
896{
897 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800898
899 if (dev->mode_config.dithering_mode_property)
900 return 0;
901
902 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100903 drm_property_create_enum(dev, 0, "dithering",
904 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800905 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800906 dev->mode_config.dithering_mode_property = dithering_mode;
907
908 return 0;
909}
910EXPORT_SYMBOL(drm_mode_create_dithering_property);
911
912/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000913 * drm_mode_create_dirty_property - create dirty property
914 * @dev: DRM device
915 *
916 * Called by a driver the first time it's needed, must be attached to desired
917 * connectors.
918 */
919int drm_mode_create_dirty_info_property(struct drm_device *dev)
920{
921 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000922
923 if (dev->mode_config.dirty_info_property)
924 return 0;
925
926 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100927 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000928 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100929 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000930 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000931 dev->mode_config.dirty_info_property = dirty_info;
932
933 return 0;
934}
935EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
936
937/**
Dave Airlief453ba02008-11-07 14:05:41 -0800938 * drm_mode_config_init - initialize DRM mode_configuration structure
939 * @dev: DRM device
940 *
Dave Airlief453ba02008-11-07 14:05:41 -0800941 * Initialize @dev's mode_config structure, used for tracking the graphics
942 * configuration of @dev.
Daniel Vetter8faf6b12012-12-01 23:43:11 +0100943 *
944 * Since this initializes the modeset locks, no locking is possible. Which is no
945 * problem, since this should happen single threaded at init time. It is the
946 * driver's problem to ensure this guarantee.
947 *
Dave Airlief453ba02008-11-07 14:05:41 -0800948 */
949void drm_mode_config_init(struct drm_device *dev)
950{
951 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000952 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800953 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800954 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
955 INIT_LIST_HEAD(&dev->mode_config.connector_list);
956 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
957 INIT_LIST_HEAD(&dev->mode_config.property_list);
958 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800959 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800960 idr_init(&dev->mode_config.crtc_idr);
961
962 mutex_lock(&dev->mode_config.mutex);
963 drm_mode_create_standard_connector_properties(dev);
964 mutex_unlock(&dev->mode_config.mutex);
965
966 /* Just to be sure */
967 dev->mode_config.num_fb = 0;
968 dev->mode_config.num_connector = 0;
969 dev->mode_config.num_crtc = 0;
970 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800971}
972EXPORT_SYMBOL(drm_mode_config_init);
973
974int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
975{
976 uint32_t total_objects = 0;
977
978 total_objects += dev->mode_config.num_crtc;
979 total_objects += dev->mode_config.num_connector;
980 total_objects += dev->mode_config.num_encoder;
981
Dave Airlief453ba02008-11-07 14:05:41 -0800982 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
983 if (!group->id_list)
984 return -ENOMEM;
985
986 group->num_crtcs = 0;
987 group->num_connectors = 0;
988 group->num_encoders = 0;
989 return 0;
990}
991
992int drm_mode_group_init_legacy_group(struct drm_device *dev,
993 struct drm_mode_group *group)
994{
995 struct drm_crtc *crtc;
996 struct drm_encoder *encoder;
997 struct drm_connector *connector;
998 int ret;
999
1000 if ((ret = drm_mode_group_init(dev, group)))
1001 return ret;
1002
1003 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1004 group->id_list[group->num_crtcs++] = crtc->base.id;
1005
1006 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1007 group->id_list[group->num_crtcs + group->num_encoders++] =
1008 encoder->base.id;
1009
1010 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1011 group->id_list[group->num_crtcs + group->num_encoders +
1012 group->num_connectors++] = connector->base.id;
1013
1014 return 0;
1015}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001016EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001017
1018/**
1019 * drm_mode_config_cleanup - free up DRM mode_config info
1020 * @dev: DRM device
1021 *
Dave Airlief453ba02008-11-07 14:05:41 -08001022 * Free up all the connectors and CRTCs associated with this DRM device, then
1023 * free up the framebuffers and associated buffer objects.
1024 *
Daniel Vetter8faf6b12012-12-01 23:43:11 +01001025 * Note that since this /should/ happen single-threaded at driver/device
1026 * teardown time, no locking is required. It's the driver's job to ensure that
1027 * this guarantee actually holds true.
1028 *
Dave Airlief453ba02008-11-07 14:05:41 -08001029 * FIXME: cleanup any dangling user buffer objects too
1030 */
1031void drm_mode_config_cleanup(struct drm_device *dev)
1032{
1033 struct drm_connector *connector, *ot;
1034 struct drm_crtc *crtc, *ct;
1035 struct drm_encoder *encoder, *enct;
1036 struct drm_framebuffer *fb, *fbt;
1037 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001038 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001039
1040 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1041 head) {
1042 encoder->funcs->destroy(encoder);
1043 }
1044
1045 list_for_each_entry_safe(connector, ot,
1046 &dev->mode_config.connector_list, head) {
1047 connector->funcs->destroy(connector);
1048 }
1049
1050 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1051 head) {
1052 drm_property_destroy(dev, property);
1053 }
1054
1055 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001056 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001057 }
1058
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001059 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1060 head) {
1061 plane->funcs->destroy(plane);
1062 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001063
Chris Wilson31840092012-09-17 09:38:03 +00001064 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1065 crtc->funcs->destroy(crtc);
1066 }
1067
Sascha Hauer59ce0622012-02-01 11:38:20 +01001068 idr_remove_all(&dev->mode_config.crtc_idr);
1069 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001070}
1071EXPORT_SYMBOL(drm_mode_config_cleanup);
1072
Dave Airlief453ba02008-11-07 14:05:41 -08001073/**
1074 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1075 * @out: drm_mode_modeinfo struct to return to the user
1076 * @in: drm_display_mode to use
1077 *
Dave Airlief453ba02008-11-07 14:05:41 -08001078 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1079 * the user.
1080 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001081static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1082 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001083{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001084 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1085 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1086 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1087 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1088 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1089 "timing values too large for mode info\n");
1090
Dave Airlief453ba02008-11-07 14:05:41 -08001091 out->clock = in->clock;
1092 out->hdisplay = in->hdisplay;
1093 out->hsync_start = in->hsync_start;
1094 out->hsync_end = in->hsync_end;
1095 out->htotal = in->htotal;
1096 out->hskew = in->hskew;
1097 out->vdisplay = in->vdisplay;
1098 out->vsync_start = in->vsync_start;
1099 out->vsync_end = in->vsync_end;
1100 out->vtotal = in->vtotal;
1101 out->vscan = in->vscan;
1102 out->vrefresh = in->vrefresh;
1103 out->flags = in->flags;
1104 out->type = in->type;
1105 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1106 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1107}
1108
1109/**
1110 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1111 * @out: drm_display_mode to return to the user
1112 * @in: drm_mode_modeinfo to use
1113 *
Dave Airlief453ba02008-11-07 14:05:41 -08001114 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1115 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001116 *
1117 * RETURNS:
1118 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001119 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001120static int drm_crtc_convert_umode(struct drm_display_mode *out,
1121 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001122{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001123 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1124 return -ERANGE;
1125
Dave Airlief453ba02008-11-07 14:05:41 -08001126 out->clock = in->clock;
1127 out->hdisplay = in->hdisplay;
1128 out->hsync_start = in->hsync_start;
1129 out->hsync_end = in->hsync_end;
1130 out->htotal = in->htotal;
1131 out->hskew = in->hskew;
1132 out->vdisplay = in->vdisplay;
1133 out->vsync_start = in->vsync_start;
1134 out->vsync_end = in->vsync_end;
1135 out->vtotal = in->vtotal;
1136 out->vscan = in->vscan;
1137 out->vrefresh = in->vrefresh;
1138 out->flags = in->flags;
1139 out->type = in->type;
1140 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1141 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001142
1143 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001144}
1145
1146/**
1147 * drm_mode_getresources - get graphics configuration
1148 * @inode: inode from the ioctl
1149 * @filp: file * from the ioctl
1150 * @cmd: cmd from ioctl
1151 * @arg: arg from ioctl
1152 *
Dave Airlief453ba02008-11-07 14:05:41 -08001153 * Construct a set of configuration description structures and return
1154 * them to the user, including CRTC, connector and framebuffer configuration.
1155 *
1156 * Called by the user via ioctl.
1157 *
1158 * RETURNS:
1159 * Zero on success, errno on failure.
1160 */
1161int drm_mode_getresources(struct drm_device *dev, void *data,
1162 struct drm_file *file_priv)
1163{
1164 struct drm_mode_card_res *card_res = data;
1165 struct list_head *lh;
1166 struct drm_framebuffer *fb;
1167 struct drm_connector *connector;
1168 struct drm_crtc *crtc;
1169 struct drm_encoder *encoder;
1170 int ret = 0;
1171 int connector_count = 0;
1172 int crtc_count = 0;
1173 int fb_count = 0;
1174 int encoder_count = 0;
1175 int copied = 0, i;
1176 uint32_t __user *fb_id;
1177 uint32_t __user *crtc_id;
1178 uint32_t __user *connector_id;
1179 uint32_t __user *encoder_id;
1180 struct drm_mode_group *mode_group;
1181
Dave Airliefb3b06c2011-02-08 13:55:21 +10001182 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1183 return -EINVAL;
1184
Dave Airlief453ba02008-11-07 14:05:41 -08001185 mutex_lock(&dev->mode_config.mutex);
1186
1187 /*
1188 * For the non-control nodes we need to limit the list of resources
1189 * by IDs in the group list for this node
1190 */
1191 list_for_each(lh, &file_priv->fbs)
1192 fb_count++;
1193
1194 mode_group = &file_priv->master->minor->mode_group;
1195 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1196
1197 list_for_each(lh, &dev->mode_config.crtc_list)
1198 crtc_count++;
1199
1200 list_for_each(lh, &dev->mode_config.connector_list)
1201 connector_count++;
1202
1203 list_for_each(lh, &dev->mode_config.encoder_list)
1204 encoder_count++;
1205 } else {
1206
1207 crtc_count = mode_group->num_crtcs;
1208 connector_count = mode_group->num_connectors;
1209 encoder_count = mode_group->num_encoders;
1210 }
1211
1212 card_res->max_height = dev->mode_config.max_height;
1213 card_res->min_height = dev->mode_config.min_height;
1214 card_res->max_width = dev->mode_config.max_width;
1215 card_res->min_width = dev->mode_config.min_width;
1216
1217 /* handle this in 4 parts */
1218 /* FBs */
1219 if (card_res->count_fbs >= fb_count) {
1220 copied = 0;
1221 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001222 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001223 if (put_user(fb->base.id, fb_id + copied)) {
1224 ret = -EFAULT;
1225 goto out;
1226 }
1227 copied++;
1228 }
1229 }
1230 card_res->count_fbs = fb_count;
1231
1232 /* CRTCs */
1233 if (card_res->count_crtcs >= crtc_count) {
1234 copied = 0;
1235 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1236 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1237 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1238 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001239 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001240 if (put_user(crtc->base.id, crtc_id + copied)) {
1241 ret = -EFAULT;
1242 goto out;
1243 }
1244 copied++;
1245 }
1246 } else {
1247 for (i = 0; i < mode_group->num_crtcs; i++) {
1248 if (put_user(mode_group->id_list[i],
1249 crtc_id + copied)) {
1250 ret = -EFAULT;
1251 goto out;
1252 }
1253 copied++;
1254 }
1255 }
1256 }
1257 card_res->count_crtcs = crtc_count;
1258
1259 /* Encoders */
1260 if (card_res->count_encoders >= encoder_count) {
1261 copied = 0;
1262 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1263 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1264 list_for_each_entry(encoder,
1265 &dev->mode_config.encoder_list,
1266 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001267 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1268 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001269 if (put_user(encoder->base.id, encoder_id +
1270 copied)) {
1271 ret = -EFAULT;
1272 goto out;
1273 }
1274 copied++;
1275 }
1276 } else {
1277 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1278 if (put_user(mode_group->id_list[i],
1279 encoder_id + copied)) {
1280 ret = -EFAULT;
1281 goto out;
1282 }
1283 copied++;
1284 }
1285
1286 }
1287 }
1288 card_res->count_encoders = encoder_count;
1289
1290 /* Connectors */
1291 if (card_res->count_connectors >= connector_count) {
1292 copied = 0;
1293 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1294 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1295 list_for_each_entry(connector,
1296 &dev->mode_config.connector_list,
1297 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001298 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1299 connector->base.id,
1300 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001301 if (put_user(connector->base.id,
1302 connector_id + copied)) {
1303 ret = -EFAULT;
1304 goto out;
1305 }
1306 copied++;
1307 }
1308 } else {
1309 int start = mode_group->num_crtcs +
1310 mode_group->num_encoders;
1311 for (i = start; i < start + mode_group->num_connectors; i++) {
1312 if (put_user(mode_group->id_list[i],
1313 connector_id + copied)) {
1314 ret = -EFAULT;
1315 goto out;
1316 }
1317 copied++;
1318 }
1319 }
1320 }
1321 card_res->count_connectors = connector_count;
1322
Jerome Glisse94401062010-07-15 15:43:25 -04001323 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001324 card_res->count_connectors, card_res->count_encoders);
1325
1326out:
1327 mutex_unlock(&dev->mode_config.mutex);
1328 return ret;
1329}
1330
1331/**
1332 * drm_mode_getcrtc - get CRTC configuration
1333 * @inode: inode from the ioctl
1334 * @filp: file * from the ioctl
1335 * @cmd: cmd from ioctl
1336 * @arg: arg from ioctl
1337 *
Dave Airlief453ba02008-11-07 14:05:41 -08001338 * Construct a CRTC configuration structure to return to the user.
1339 *
1340 * Called by the user via ioctl.
1341 *
1342 * RETURNS:
1343 * Zero on success, errno on failure.
1344 */
1345int drm_mode_getcrtc(struct drm_device *dev,
1346 void *data, struct drm_file *file_priv)
1347{
1348 struct drm_mode_crtc *crtc_resp = data;
1349 struct drm_crtc *crtc;
1350 struct drm_mode_object *obj;
1351 int ret = 0;
1352
Dave Airliefb3b06c2011-02-08 13:55:21 +10001353 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1354 return -EINVAL;
1355
Dave Airlief453ba02008-11-07 14:05:41 -08001356 mutex_lock(&dev->mode_config.mutex);
1357
1358 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1359 DRM_MODE_OBJECT_CRTC);
1360 if (!obj) {
1361 ret = -EINVAL;
1362 goto out;
1363 }
1364 crtc = obj_to_crtc(obj);
1365
1366 crtc_resp->x = crtc->x;
1367 crtc_resp->y = crtc->y;
1368 crtc_resp->gamma_size = crtc->gamma_size;
1369 if (crtc->fb)
1370 crtc_resp->fb_id = crtc->fb->base.id;
1371 else
1372 crtc_resp->fb_id = 0;
1373
1374 if (crtc->enabled) {
1375
1376 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1377 crtc_resp->mode_valid = 1;
1378
1379 } else {
1380 crtc_resp->mode_valid = 0;
1381 }
1382
1383out:
1384 mutex_unlock(&dev->mode_config.mutex);
1385 return ret;
1386}
1387
1388/**
1389 * drm_mode_getconnector - get connector configuration
1390 * @inode: inode from the ioctl
1391 * @filp: file * from the ioctl
1392 * @cmd: cmd from ioctl
1393 * @arg: arg from ioctl
1394 *
Dave Airlief453ba02008-11-07 14:05:41 -08001395 * Construct a connector configuration structure to return to the user.
1396 *
1397 * Called by the user via ioctl.
1398 *
1399 * RETURNS:
1400 * Zero on success, errno on failure.
1401 */
1402int drm_mode_getconnector(struct drm_device *dev, void *data,
1403 struct drm_file *file_priv)
1404{
1405 struct drm_mode_get_connector *out_resp = data;
1406 struct drm_mode_object *obj;
1407 struct drm_connector *connector;
1408 struct drm_display_mode *mode;
1409 int mode_count = 0;
1410 int props_count = 0;
1411 int encoders_count = 0;
1412 int ret = 0;
1413 int copied = 0;
1414 int i;
1415 struct drm_mode_modeinfo u_mode;
1416 struct drm_mode_modeinfo __user *mode_ptr;
1417 uint32_t __user *prop_ptr;
1418 uint64_t __user *prop_values;
1419 uint32_t __user *encoder_ptr;
1420
Dave Airliefb3b06c2011-02-08 13:55:21 +10001421 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1422 return -EINVAL;
1423
Dave Airlief453ba02008-11-07 14:05:41 -08001424 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1425
Jerome Glisse94401062010-07-15 15:43:25 -04001426 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001427
1428 mutex_lock(&dev->mode_config.mutex);
1429
1430 obj = drm_mode_object_find(dev, out_resp->connector_id,
1431 DRM_MODE_OBJECT_CONNECTOR);
1432 if (!obj) {
1433 ret = -EINVAL;
1434 goto out;
1435 }
1436 connector = obj_to_connector(obj);
1437
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001438 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001439
1440 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1441 if (connector->encoder_ids[i] != 0) {
1442 encoders_count++;
1443 }
1444 }
1445
1446 if (out_resp->count_modes == 0) {
1447 connector->funcs->fill_modes(connector,
1448 dev->mode_config.max_width,
1449 dev->mode_config.max_height);
1450 }
1451
1452 /* delayed so we get modes regardless of pre-fill_modes state */
1453 list_for_each_entry(mode, &connector->modes, head)
1454 mode_count++;
1455
1456 out_resp->connector_id = connector->base.id;
1457 out_resp->connector_type = connector->connector_type;
1458 out_resp->connector_type_id = connector->connector_type_id;
1459 out_resp->mm_width = connector->display_info.width_mm;
1460 out_resp->mm_height = connector->display_info.height_mm;
1461 out_resp->subpixel = connector->display_info.subpixel_order;
1462 out_resp->connection = connector->status;
1463 if (connector->encoder)
1464 out_resp->encoder_id = connector->encoder->base.id;
1465 else
1466 out_resp->encoder_id = 0;
1467
1468 /*
1469 * This ioctl is called twice, once to determine how much space is
1470 * needed, and the 2nd time to fill it.
1471 */
1472 if ((out_resp->count_modes >= mode_count) && mode_count) {
1473 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001474 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001475 list_for_each_entry(mode, &connector->modes, head) {
1476 drm_crtc_convert_to_umode(&u_mode, mode);
1477 if (copy_to_user(mode_ptr + copied,
1478 &u_mode, sizeof(u_mode))) {
1479 ret = -EFAULT;
1480 goto out;
1481 }
1482 copied++;
1483 }
1484 }
1485 out_resp->count_modes = mode_count;
1486
1487 if ((out_resp->count_props >= props_count) && props_count) {
1488 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001489 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1490 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001491 for (i = 0; i < connector->properties.count; i++) {
1492 if (put_user(connector->properties.ids[i],
1493 prop_ptr + copied)) {
1494 ret = -EFAULT;
1495 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001496 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001497
1498 if (put_user(connector->properties.values[i],
1499 prop_values + copied)) {
1500 ret = -EFAULT;
1501 goto out;
1502 }
1503 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001504 }
1505 }
1506 out_resp->count_props = props_count;
1507
1508 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1509 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001510 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001511 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1512 if (connector->encoder_ids[i] != 0) {
1513 if (put_user(connector->encoder_ids[i],
1514 encoder_ptr + copied)) {
1515 ret = -EFAULT;
1516 goto out;
1517 }
1518 copied++;
1519 }
1520 }
1521 }
1522 out_resp->count_encoders = encoders_count;
1523
1524out:
1525 mutex_unlock(&dev->mode_config.mutex);
1526 return ret;
1527}
1528
1529int drm_mode_getencoder(struct drm_device *dev, void *data,
1530 struct drm_file *file_priv)
1531{
1532 struct drm_mode_get_encoder *enc_resp = data;
1533 struct drm_mode_object *obj;
1534 struct drm_encoder *encoder;
1535 int ret = 0;
1536
Dave Airliefb3b06c2011-02-08 13:55:21 +10001537 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1538 return -EINVAL;
1539
Dave Airlief453ba02008-11-07 14:05:41 -08001540 mutex_lock(&dev->mode_config.mutex);
1541 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1542 DRM_MODE_OBJECT_ENCODER);
1543 if (!obj) {
1544 ret = -EINVAL;
1545 goto out;
1546 }
1547 encoder = obj_to_encoder(obj);
1548
1549 if (encoder->crtc)
1550 enc_resp->crtc_id = encoder->crtc->base.id;
1551 else
1552 enc_resp->crtc_id = 0;
1553 enc_resp->encoder_type = encoder->encoder_type;
1554 enc_resp->encoder_id = encoder->base.id;
1555 enc_resp->possible_crtcs = encoder->possible_crtcs;
1556 enc_resp->possible_clones = encoder->possible_clones;
1557
1558out:
1559 mutex_unlock(&dev->mode_config.mutex);
1560 return ret;
1561}
1562
1563/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001564 * drm_mode_getplane_res - get plane info
1565 * @dev: DRM device
1566 * @data: ioctl data
1567 * @file_priv: DRM file info
1568 *
1569 * Return an plane count and set of IDs.
1570 */
1571int drm_mode_getplane_res(struct drm_device *dev, void *data,
1572 struct drm_file *file_priv)
1573{
1574 struct drm_mode_get_plane_res *plane_resp = data;
1575 struct drm_mode_config *config;
1576 struct drm_plane *plane;
1577 uint32_t __user *plane_ptr;
1578 int copied = 0, ret = 0;
1579
1580 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1581 return -EINVAL;
1582
1583 mutex_lock(&dev->mode_config.mutex);
1584 config = &dev->mode_config;
1585
1586 /*
1587 * This ioctl is called twice, once to determine how much space is
1588 * needed, and the 2nd time to fill it.
1589 */
1590 if (config->num_plane &&
1591 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001592 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001593
1594 list_for_each_entry(plane, &config->plane_list, head) {
1595 if (put_user(plane->base.id, plane_ptr + copied)) {
1596 ret = -EFAULT;
1597 goto out;
1598 }
1599 copied++;
1600 }
1601 }
1602 plane_resp->count_planes = config->num_plane;
1603
1604out:
1605 mutex_unlock(&dev->mode_config.mutex);
1606 return ret;
1607}
1608
1609/**
1610 * drm_mode_getplane - get plane info
1611 * @dev: DRM device
1612 * @data: ioctl data
1613 * @file_priv: DRM file info
1614 *
1615 * Return plane info, including formats supported, gamma size, any
1616 * current fb, etc.
1617 */
1618int drm_mode_getplane(struct drm_device *dev, void *data,
1619 struct drm_file *file_priv)
1620{
1621 struct drm_mode_get_plane *plane_resp = data;
1622 struct drm_mode_object *obj;
1623 struct drm_plane *plane;
1624 uint32_t __user *format_ptr;
1625 int ret = 0;
1626
1627 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1628 return -EINVAL;
1629
1630 mutex_lock(&dev->mode_config.mutex);
1631 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1632 DRM_MODE_OBJECT_PLANE);
1633 if (!obj) {
1634 ret = -ENOENT;
1635 goto out;
1636 }
1637 plane = obj_to_plane(obj);
1638
1639 if (plane->crtc)
1640 plane_resp->crtc_id = plane->crtc->base.id;
1641 else
1642 plane_resp->crtc_id = 0;
1643
1644 if (plane->fb)
1645 plane_resp->fb_id = plane->fb->base.id;
1646 else
1647 plane_resp->fb_id = 0;
1648
1649 plane_resp->plane_id = plane->base.id;
1650 plane_resp->possible_crtcs = plane->possible_crtcs;
1651 plane_resp->gamma_size = plane->gamma_size;
1652
1653 /*
1654 * This ioctl is called twice, once to determine how much space is
1655 * needed, and the 2nd time to fill it.
1656 */
1657 if (plane->format_count &&
1658 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001659 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001660 if (copy_to_user(format_ptr,
1661 plane->format_types,
1662 sizeof(uint32_t) * plane->format_count)) {
1663 ret = -EFAULT;
1664 goto out;
1665 }
1666 }
1667 plane_resp->count_format_types = plane->format_count;
1668
1669out:
1670 mutex_unlock(&dev->mode_config.mutex);
1671 return ret;
1672}
1673
1674/**
1675 * drm_mode_setplane - set up or tear down an plane
1676 * @dev: DRM device
1677 * @data: ioctl data*
1678 * @file_prive: DRM file info
1679 *
1680 * Set plane info, including placement, fb, scaling, and other factors.
1681 * Or pass a NULL fb to disable.
1682 */
1683int drm_mode_setplane(struct drm_device *dev, void *data,
1684 struct drm_file *file_priv)
1685{
1686 struct drm_mode_set_plane *plane_req = data;
1687 struct drm_mode_object *obj;
1688 struct drm_plane *plane;
1689 struct drm_crtc *crtc;
1690 struct drm_framebuffer *fb;
1691 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001692 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001693 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001694
1695 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1696 return -EINVAL;
1697
1698 mutex_lock(&dev->mode_config.mutex);
1699
1700 /*
1701 * First, find the plane, crtc, and fb objects. If not available,
1702 * we don't bother to call the driver.
1703 */
1704 obj = drm_mode_object_find(dev, plane_req->plane_id,
1705 DRM_MODE_OBJECT_PLANE);
1706 if (!obj) {
1707 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1708 plane_req->plane_id);
1709 ret = -ENOENT;
1710 goto out;
1711 }
1712 plane = obj_to_plane(obj);
1713
1714 /* No fb means shut it down */
1715 if (!plane_req->fb_id) {
1716 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001717 plane->crtc = NULL;
1718 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001719 goto out;
1720 }
1721
1722 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1723 DRM_MODE_OBJECT_CRTC);
1724 if (!obj) {
1725 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1726 plane_req->crtc_id);
1727 ret = -ENOENT;
1728 goto out;
1729 }
1730 crtc = obj_to_crtc(obj);
1731
1732 obj = drm_mode_object_find(dev, plane_req->fb_id,
1733 DRM_MODE_OBJECT_FB);
1734 if (!obj) {
1735 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1736 plane_req->fb_id);
1737 ret = -ENOENT;
1738 goto out;
1739 }
1740 fb = obj_to_fb(obj);
1741
Ville Syrjälä62443be2011-12-20 00:06:47 +02001742 /* Check whether this plane supports the fb pixel format. */
1743 for (i = 0; i < plane->format_count; i++)
1744 if (fb->pixel_format == plane->format_types[i])
1745 break;
1746 if (i == plane->format_count) {
1747 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1748 ret = -EINVAL;
1749 goto out;
1750 }
1751
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001752 fb_width = fb->width << 16;
1753 fb_height = fb->height << 16;
1754
1755 /* Make sure source coordinates are inside the fb. */
1756 if (plane_req->src_w > fb_width ||
1757 plane_req->src_x > fb_width - plane_req->src_w ||
1758 plane_req->src_h > fb_height ||
1759 plane_req->src_y > fb_height - plane_req->src_h) {
1760 DRM_DEBUG_KMS("Invalid source coordinates "
1761 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1762 plane_req->src_w >> 16,
1763 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1764 plane_req->src_h >> 16,
1765 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1766 plane_req->src_x >> 16,
1767 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1768 plane_req->src_y >> 16,
1769 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1770 ret = -ENOSPC;
1771 goto out;
1772 }
1773
Ville Syrjälä687a0402011-12-20 00:06:45 +02001774 /* Give drivers some help against integer overflows */
1775 if (plane_req->crtc_w > INT_MAX ||
1776 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1777 plane_req->crtc_h > INT_MAX ||
1778 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1779 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1780 plane_req->crtc_w, plane_req->crtc_h,
1781 plane_req->crtc_x, plane_req->crtc_y);
1782 ret = -ERANGE;
1783 goto out;
1784 }
1785
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001786 ret = plane->funcs->update_plane(plane, crtc, fb,
1787 plane_req->crtc_x, plane_req->crtc_y,
1788 plane_req->crtc_w, plane_req->crtc_h,
1789 plane_req->src_x, plane_req->src_y,
1790 plane_req->src_w, plane_req->src_h);
1791 if (!ret) {
1792 plane->crtc = crtc;
1793 plane->fb = fb;
1794 }
1795
1796out:
1797 mutex_unlock(&dev->mode_config.mutex);
1798
1799 return ret;
1800}
1801
1802/**
Dave Airlief453ba02008-11-07 14:05:41 -08001803 * drm_mode_setcrtc - set CRTC configuration
1804 * @inode: inode from the ioctl
1805 * @filp: file * from the ioctl
1806 * @cmd: cmd from ioctl
1807 * @arg: arg from ioctl
1808 *
Dave Airlief453ba02008-11-07 14:05:41 -08001809 * Build a new CRTC configuration based on user request.
1810 *
1811 * Called by the user via ioctl.
1812 *
1813 * RETURNS:
1814 * Zero on success, errno on failure.
1815 */
1816int drm_mode_setcrtc(struct drm_device *dev, void *data,
1817 struct drm_file *file_priv)
1818{
1819 struct drm_mode_config *config = &dev->mode_config;
1820 struct drm_mode_crtc *crtc_req = data;
1821 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001822 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001823 struct drm_connector **connector_set = NULL, *connector;
1824 struct drm_framebuffer *fb = NULL;
1825 struct drm_display_mode *mode = NULL;
1826 struct drm_mode_set set;
1827 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001828 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001829 int i;
1830
Dave Airliefb3b06c2011-02-08 13:55:21 +10001831 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1832 return -EINVAL;
1833
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001834 /* For some reason crtc x/y offsets are signed internally. */
1835 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1836 return -ERANGE;
1837
Dave Airlief453ba02008-11-07 14:05:41 -08001838 mutex_lock(&dev->mode_config.mutex);
1839 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1840 DRM_MODE_OBJECT_CRTC);
1841 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001842 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001843 ret = -EINVAL;
1844 goto out;
1845 }
1846 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001847 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001848
1849 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001850 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001851 /* If we have a mode we need a framebuffer. */
1852 /* If we pass -1, set the mode with the currently bound fb */
1853 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001854 if (!crtc->fb) {
1855 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1856 ret = -EINVAL;
1857 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001858 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001859 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001860 } else {
1861 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1862 DRM_MODE_OBJECT_FB);
1863 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001864 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1865 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001866 ret = -EINVAL;
1867 goto out;
1868 }
1869 fb = obj_to_fb(obj);
1870 }
1871
1872 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001873 if (!mode) {
1874 ret = -ENOMEM;
1875 goto out;
1876 }
1877
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001878 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1879 if (ret) {
1880 DRM_DEBUG_KMS("Invalid mode\n");
1881 goto out;
1882 }
1883
Dave Airlief453ba02008-11-07 14:05:41 -08001884 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001885
Rob Clark7c80e122012-09-04 16:35:56 +00001886 hdisplay = mode->hdisplay;
1887 vdisplay = mode->vdisplay;
1888
1889 if (crtc->invert_dimensions)
1890 swap(hdisplay, vdisplay);
1891
1892 if (hdisplay > fb->width ||
1893 vdisplay > fb->height ||
1894 crtc_req->x > fb->width - hdisplay ||
1895 crtc_req->y > fb->height - vdisplay) {
1896 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1897 fb->width, fb->height,
1898 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1899 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001900 ret = -ENOSPC;
1901 goto out;
1902 }
Dave Airlief453ba02008-11-07 14:05:41 -08001903 }
1904
1905 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001906 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001907 ret = -EINVAL;
1908 goto out;
1909 }
1910
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001911 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001912 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001913 crtc_req->count_connectors);
1914 ret = -EINVAL;
1915 goto out;
1916 }
1917
1918 if (crtc_req->count_connectors > 0) {
1919 u32 out_id;
1920
1921 /* Avoid unbounded kernel memory allocation */
1922 if (crtc_req->count_connectors > config->num_connector) {
1923 ret = -EINVAL;
1924 goto out;
1925 }
1926
1927 connector_set = kmalloc(crtc_req->count_connectors *
1928 sizeof(struct drm_connector *),
1929 GFP_KERNEL);
1930 if (!connector_set) {
1931 ret = -ENOMEM;
1932 goto out;
1933 }
1934
1935 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001936 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001937 if (get_user(out_id, &set_connectors_ptr[i])) {
1938 ret = -EFAULT;
1939 goto out;
1940 }
1941
1942 obj = drm_mode_object_find(dev, out_id,
1943 DRM_MODE_OBJECT_CONNECTOR);
1944 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001945 DRM_DEBUG_KMS("Connector id %d unknown\n",
1946 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001947 ret = -EINVAL;
1948 goto out;
1949 }
1950 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001951 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1952 connector->base.id,
1953 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001954
1955 connector_set[i] = connector;
1956 }
1957 }
1958
1959 set.crtc = crtc;
1960 set.x = crtc_req->x;
1961 set.y = crtc_req->y;
1962 set.mode = mode;
1963 set.connectors = connector_set;
1964 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001965 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001966 ret = crtc->funcs->set_config(&set);
1967
1968out:
1969 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001970 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001971 mutex_unlock(&dev->mode_config.mutex);
1972 return ret;
1973}
1974
1975int drm_mode_cursor_ioctl(struct drm_device *dev,
1976 void *data, struct drm_file *file_priv)
1977{
1978 struct drm_mode_cursor *req = data;
1979 struct drm_mode_object *obj;
1980 struct drm_crtc *crtc;
1981 int ret = 0;
1982
Dave Airliefb3b06c2011-02-08 13:55:21 +10001983 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1984 return -EINVAL;
1985
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00001986 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08001987 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001988
1989 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001990 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001992 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001993 ret = -EINVAL;
1994 goto out;
1995 }
1996 crtc = obj_to_crtc(obj);
1997
1998 if (req->flags & DRM_MODE_CURSOR_BO) {
1999 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002000 ret = -ENXIO;
2001 goto out;
2002 }
2003 /* Turns off the cursor if handle is 0 */
2004 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2005 req->width, req->height);
2006 }
2007
2008 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2009 if (crtc->funcs->cursor_move) {
2010 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2011 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002012 ret = -EFAULT;
2013 goto out;
2014 }
2015 }
2016out:
2017 mutex_unlock(&dev->mode_config.mutex);
2018 return ret;
2019}
2020
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002021/* Original addfb only supported RGB formats, so figure out which one */
2022uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2023{
2024 uint32_t fmt;
2025
2026 switch (bpp) {
2027 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002028 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002029 break;
2030 case 16:
2031 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002032 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002033 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002034 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002035 break;
2036 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002037 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002038 break;
2039 case 32:
2040 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002041 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002042 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002045 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002046 break;
2047 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002048 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2049 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002050 break;
2051 }
2052
2053 return fmt;
2054}
2055EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2056
Dave Airlief453ba02008-11-07 14:05:41 -08002057/**
2058 * drm_mode_addfb - add an FB to the graphics configuration
2059 * @inode: inode from the ioctl
2060 * @filp: file * from the ioctl
2061 * @cmd: cmd from ioctl
2062 * @arg: arg from ioctl
2063 *
Dave Airlief453ba02008-11-07 14:05:41 -08002064 * Add a new FB to the specified CRTC, given a user request.
2065 *
2066 * Called by the user via ioctl.
2067 *
2068 * RETURNS:
2069 * Zero on success, errno on failure.
2070 */
2071int drm_mode_addfb(struct drm_device *dev,
2072 void *data, struct drm_file *file_priv)
2073{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002074 struct drm_mode_fb_cmd *or = data;
2075 struct drm_mode_fb_cmd2 r = {};
2076 struct drm_mode_config *config = &dev->mode_config;
2077 struct drm_framebuffer *fb;
2078 int ret = 0;
2079
2080 /* Use new struct with format internally */
2081 r.fb_id = or->fb_id;
2082 r.width = or->width;
2083 r.height = or->height;
2084 r.pitches[0] = or->pitch;
2085 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2086 r.handles[0] = or->handle;
2087
2088 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2089 return -EINVAL;
2090
Jesse Barnesacb4b992011-11-07 12:03:23 -08002091 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002092 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002093
2094 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002095 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002096
2097 mutex_lock(&dev->mode_config.mutex);
2098
2099 /* TODO check buffer is sufficiently large */
2100 /* TODO setup destructor callback */
2101
2102 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2103 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002104 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002105 ret = PTR_ERR(fb);
2106 goto out;
2107 }
2108
2109 or->fb_id = fb->base.id;
2110 list_add(&fb->filp_head, &file_priv->fbs);
2111 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2112
2113out:
2114 mutex_unlock(&dev->mode_config.mutex);
2115 return ret;
2116}
2117
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002118static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002119{
2120 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2121
2122 switch (format) {
2123 case DRM_FORMAT_C8:
2124 case DRM_FORMAT_RGB332:
2125 case DRM_FORMAT_BGR233:
2126 case DRM_FORMAT_XRGB4444:
2127 case DRM_FORMAT_XBGR4444:
2128 case DRM_FORMAT_RGBX4444:
2129 case DRM_FORMAT_BGRX4444:
2130 case DRM_FORMAT_ARGB4444:
2131 case DRM_FORMAT_ABGR4444:
2132 case DRM_FORMAT_RGBA4444:
2133 case DRM_FORMAT_BGRA4444:
2134 case DRM_FORMAT_XRGB1555:
2135 case DRM_FORMAT_XBGR1555:
2136 case DRM_FORMAT_RGBX5551:
2137 case DRM_FORMAT_BGRX5551:
2138 case DRM_FORMAT_ARGB1555:
2139 case DRM_FORMAT_ABGR1555:
2140 case DRM_FORMAT_RGBA5551:
2141 case DRM_FORMAT_BGRA5551:
2142 case DRM_FORMAT_RGB565:
2143 case DRM_FORMAT_BGR565:
2144 case DRM_FORMAT_RGB888:
2145 case DRM_FORMAT_BGR888:
2146 case DRM_FORMAT_XRGB8888:
2147 case DRM_FORMAT_XBGR8888:
2148 case DRM_FORMAT_RGBX8888:
2149 case DRM_FORMAT_BGRX8888:
2150 case DRM_FORMAT_ARGB8888:
2151 case DRM_FORMAT_ABGR8888:
2152 case DRM_FORMAT_RGBA8888:
2153 case DRM_FORMAT_BGRA8888:
2154 case DRM_FORMAT_XRGB2101010:
2155 case DRM_FORMAT_XBGR2101010:
2156 case DRM_FORMAT_RGBX1010102:
2157 case DRM_FORMAT_BGRX1010102:
2158 case DRM_FORMAT_ARGB2101010:
2159 case DRM_FORMAT_ABGR2101010:
2160 case DRM_FORMAT_RGBA1010102:
2161 case DRM_FORMAT_BGRA1010102:
2162 case DRM_FORMAT_YUYV:
2163 case DRM_FORMAT_YVYU:
2164 case DRM_FORMAT_UYVY:
2165 case DRM_FORMAT_VYUY:
2166 case DRM_FORMAT_AYUV:
2167 case DRM_FORMAT_NV12:
2168 case DRM_FORMAT_NV21:
2169 case DRM_FORMAT_NV16:
2170 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002171 case DRM_FORMAT_NV24:
2172 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002173 case DRM_FORMAT_YUV410:
2174 case DRM_FORMAT_YVU410:
2175 case DRM_FORMAT_YUV411:
2176 case DRM_FORMAT_YVU411:
2177 case DRM_FORMAT_YUV420:
2178 case DRM_FORMAT_YVU420:
2179 case DRM_FORMAT_YUV422:
2180 case DRM_FORMAT_YVU422:
2181 case DRM_FORMAT_YUV444:
2182 case DRM_FORMAT_YVU444:
2183 return 0;
2184 default:
2185 return -EINVAL;
2186 }
2187}
2188
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002189static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002190{
2191 int ret, hsub, vsub, num_planes, i;
2192
2193 ret = format_check(r);
2194 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002195 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002196 return ret;
2197 }
2198
2199 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2200 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2201 num_planes = drm_format_num_planes(r->pixel_format);
2202
2203 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002204 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002205 return -EINVAL;
2206 }
2207
2208 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002209 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002210 return -EINVAL;
2211 }
2212
2213 for (i = 0; i < num_planes; i++) {
2214 unsigned int width = r->width / (i != 0 ? hsub : 1);
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002215 unsigned int height = r->height / (i != 0 ? vsub : 1);
2216 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002217
2218 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002219 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002220 return -EINVAL;
2221 }
2222
Ville Syrjäläb180b5d2012-10-25 18:05:04 +00002223 if ((uint64_t) width * cpp > UINT_MAX)
2224 return -ERANGE;
2225
2226 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
2227 return -ERANGE;
2228
2229 if (r->pitches[i] < width * cpp) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002230 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002231 return -EINVAL;
2232 }
2233 }
2234
2235 return 0;
2236}
2237
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002238/**
2239 * drm_mode_addfb2 - add an FB to the graphics configuration
2240 * @inode: inode from the ioctl
2241 * @filp: file * from the ioctl
2242 * @cmd: cmd from ioctl
2243 * @arg: arg from ioctl
2244 *
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002245 * Add a new FB to the specified CRTC, given a user request with format.
2246 *
2247 * Called by the user via ioctl.
2248 *
2249 * RETURNS:
2250 * Zero on success, errno on failure.
2251 */
2252int drm_mode_addfb2(struct drm_device *dev,
2253 void *data, struct drm_file *file_priv)
2254{
2255 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002256 struct drm_mode_config *config = &dev->mode_config;
2257 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002258 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002259
Dave Airliefb3b06c2011-02-08 13:55:21 +10002260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2261 return -EINVAL;
2262
Ville Syrjäläe3cc3522012-11-08 09:09:42 +00002263 if (r->flags & ~DRM_MODE_FB_INTERLACED) {
2264 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
2265 return -EINVAL;
2266 }
2267
Dave Airlief453ba02008-11-07 14:05:41 -08002268 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002269 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002270 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002271 return -EINVAL;
2272 }
2273 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002274 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002275 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002276 return -EINVAL;
2277 }
2278
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002279 ret = framebuffer_check(r);
2280 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002281 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002282
Dave Airlief453ba02008-11-07 14:05:41 -08002283 mutex_lock(&dev->mode_config.mutex);
2284
Dave Airlief453ba02008-11-07 14:05:41 -08002285 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002286 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002287 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002288 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002289 goto out;
2290 }
2291
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002292 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002293 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002294 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002295
2296out:
2297 mutex_unlock(&dev->mode_config.mutex);
2298 return ret;
2299}
2300
2301/**
2302 * drm_mode_rmfb - remove an FB from the configuration
2303 * @inode: inode from the ioctl
2304 * @filp: file * from the ioctl
2305 * @cmd: cmd from ioctl
2306 * @arg: arg from ioctl
2307 *
Dave Airlief453ba02008-11-07 14:05:41 -08002308 * Remove the FB specified by the user.
2309 *
2310 * Called by the user via ioctl.
2311 *
2312 * RETURNS:
2313 * Zero on success, errno on failure.
2314 */
2315int drm_mode_rmfb(struct drm_device *dev,
2316 void *data, struct drm_file *file_priv)
2317{
2318 struct drm_mode_object *obj;
2319 struct drm_framebuffer *fb = NULL;
2320 struct drm_framebuffer *fbl = NULL;
2321 uint32_t *id = data;
2322 int ret = 0;
2323 int found = 0;
2324
Dave Airliefb3b06c2011-02-08 13:55:21 +10002325 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2326 return -EINVAL;
2327
Dave Airlief453ba02008-11-07 14:05:41 -08002328 mutex_lock(&dev->mode_config.mutex);
2329 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002330 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002331 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002332 ret = -EINVAL;
2333 goto out;
2334 }
2335 fb = obj_to_fb(obj);
2336
2337 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2338 if (fb == fbl)
2339 found = 1;
2340
2341 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002342 ret = -EINVAL;
2343 goto out;
2344 }
2345
Rob Clarkf7eff602012-09-05 21:48:38 +00002346 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002347
2348out:
2349 mutex_unlock(&dev->mode_config.mutex);
2350 return ret;
2351}
2352
2353/**
2354 * drm_mode_getfb - get FB info
2355 * @inode: inode from the ioctl
2356 * @filp: file * from the ioctl
2357 * @cmd: cmd from ioctl
2358 * @arg: arg from ioctl
2359 *
Dave Airlief453ba02008-11-07 14:05:41 -08002360 * Lookup the FB given its ID and return info about it.
2361 *
2362 * Called by the user via ioctl.
2363 *
2364 * RETURNS:
2365 * Zero on success, errno on failure.
2366 */
2367int drm_mode_getfb(struct drm_device *dev,
2368 void *data, struct drm_file *file_priv)
2369{
2370 struct drm_mode_fb_cmd *r = data;
2371 struct drm_mode_object *obj;
2372 struct drm_framebuffer *fb;
2373 int ret = 0;
2374
Dave Airliefb3b06c2011-02-08 13:55:21 +10002375 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2376 return -EINVAL;
2377
Dave Airlief453ba02008-11-07 14:05:41 -08002378 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002379 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002380 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002381 ret = -EINVAL;
2382 goto out;
2383 }
2384 fb = obj_to_fb(obj);
2385
2386 r->height = fb->height;
2387 r->width = fb->width;
2388 r->depth = fb->depth;
2389 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002390 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002391 fb->funcs->create_handle(fb, file_priv, &r->handle);
2392
2393out:
2394 mutex_unlock(&dev->mode_config.mutex);
2395 return ret;
2396}
2397
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002398int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2399 void *data, struct drm_file *file_priv)
2400{
2401 struct drm_clip_rect __user *clips_ptr;
2402 struct drm_clip_rect *clips = NULL;
2403 struct drm_mode_fb_dirty_cmd *r = data;
2404 struct drm_mode_object *obj;
2405 struct drm_framebuffer *fb;
2406 unsigned flags;
2407 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002408 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002409
Dave Airliefb3b06c2011-02-08 13:55:21 +10002410 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2411 return -EINVAL;
2412
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002413 mutex_lock(&dev->mode_config.mutex);
2414 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2415 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002416 ret = -EINVAL;
2417 goto out_err1;
2418 }
2419 fb = obj_to_fb(obj);
2420
2421 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002422 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002423
2424 if (!num_clips != !clips_ptr) {
2425 ret = -EINVAL;
2426 goto out_err1;
2427 }
2428
2429 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2430
2431 /* If userspace annotates copy, clips must come in pairs */
2432 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2433 ret = -EINVAL;
2434 goto out_err1;
2435 }
2436
2437 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002438 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2439 ret = -EINVAL;
2440 goto out_err1;
2441 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002442 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2443 if (!clips) {
2444 ret = -ENOMEM;
2445 goto out_err1;
2446 }
2447
2448 ret = copy_from_user(clips, clips_ptr,
2449 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002450 if (ret) {
2451 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002452 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002453 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002454 }
2455
2456 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002457 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2458 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002459 } else {
2460 ret = -ENOSYS;
2461 goto out_err2;
2462 }
2463
2464out_err2:
2465 kfree(clips);
2466out_err1:
2467 mutex_unlock(&dev->mode_config.mutex);
2468 return ret;
2469}
2470
2471
Dave Airlief453ba02008-11-07 14:05:41 -08002472/**
2473 * drm_fb_release - remove and free the FBs on this file
2474 * @filp: file * from the ioctl
2475 *
Dave Airlief453ba02008-11-07 14:05:41 -08002476 * Destroy all the FBs associated with @filp.
2477 *
2478 * Called by the user via ioctl.
2479 *
2480 * RETURNS:
2481 * Zero on success, errno on failure.
2482 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002483void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002484{
Dave Airlief453ba02008-11-07 14:05:41 -08002485 struct drm_device *dev = priv->minor->dev;
2486 struct drm_framebuffer *fb, *tfb;
2487
2488 mutex_lock(&dev->mode_config.mutex);
2489 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00002490 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002491 }
2492 mutex_unlock(&dev->mode_config.mutex);
2493}
2494
2495/**
2496 * drm_mode_attachmode - add a mode to the user mode list
2497 * @dev: DRM device
2498 * @connector: connector to add the mode to
2499 * @mode: mode to add
2500 *
2501 * Add @mode to @connector's user mode list.
2502 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002503static void drm_mode_attachmode(struct drm_device *dev,
2504 struct drm_connector *connector,
2505 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002506{
Dave Airlief453ba02008-11-07 14:05:41 -08002507 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002508}
2509
2510int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002511 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002512{
2513 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002514 int ret = 0;
2515 struct drm_display_mode *dup_mode, *next;
2516 LIST_HEAD(list);
2517
Dave Airlief453ba02008-11-07 14:05:41 -08002518 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2519 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002520 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002521 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002522 dup_mode = drm_mode_duplicate(dev, mode);
2523 if (!dup_mode) {
2524 ret = -ENOMEM;
2525 goto out;
2526 }
2527 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002528 }
2529 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002530
2531 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2532 if (!connector->encoder)
2533 continue;
2534 if (connector->encoder->crtc == crtc)
2535 list_move_tail(list.next, &connector->user_modes);
2536 }
2537
2538 WARN_ON(!list_empty(&list));
2539
2540 out:
2541 list_for_each_entry_safe(dup_mode, next, &list, head)
2542 drm_mode_destroy(dev, dup_mode);
2543
2544 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002545}
2546EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2547
2548static int drm_mode_detachmode(struct drm_device *dev,
2549 struct drm_connector *connector,
2550 struct drm_display_mode *mode)
2551{
2552 int found = 0;
2553 int ret = 0;
2554 struct drm_display_mode *match_mode, *t;
2555
2556 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2557 if (drm_mode_equal(match_mode, mode)) {
2558 list_del(&match_mode->head);
2559 drm_mode_destroy(dev, match_mode);
2560 found = 1;
2561 break;
2562 }
2563 }
2564
2565 if (!found)
2566 ret = -EINVAL;
2567
2568 return ret;
2569}
2570
2571int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2572{
2573 struct drm_connector *connector;
2574
2575 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2576 drm_mode_detachmode(dev, connector, mode);
2577 }
2578 return 0;
2579}
2580EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2581
2582/**
2583 * drm_fb_attachmode - Attach a user mode to an connector
2584 * @inode: inode from the ioctl
2585 * @filp: file * from the ioctl
2586 * @cmd: cmd from ioctl
2587 * @arg: arg from ioctl
2588 *
2589 * This attaches a user specified mode to an connector.
2590 * Called by the user via ioctl.
2591 *
2592 * RETURNS:
2593 * Zero on success, errno on failure.
2594 */
2595int drm_mode_attachmode_ioctl(struct drm_device *dev,
2596 void *data, struct drm_file *file_priv)
2597{
2598 struct drm_mode_mode_cmd *mode_cmd = data;
2599 struct drm_connector *connector;
2600 struct drm_display_mode *mode;
2601 struct drm_mode_object *obj;
2602 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002603 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002604
Dave Airliefb3b06c2011-02-08 13:55:21 +10002605 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2606 return -EINVAL;
2607
Dave Airlief453ba02008-11-07 14:05:41 -08002608 mutex_lock(&dev->mode_config.mutex);
2609
2610 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2611 if (!obj) {
2612 ret = -EINVAL;
2613 goto out;
2614 }
2615 connector = obj_to_connector(obj);
2616
2617 mode = drm_mode_create(dev);
2618 if (!mode) {
2619 ret = -ENOMEM;
2620 goto out;
2621 }
2622
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002623 ret = drm_crtc_convert_umode(mode, umode);
2624 if (ret) {
2625 DRM_DEBUG_KMS("Invalid mode\n");
2626 drm_mode_destroy(dev, mode);
2627 goto out;
2628 }
Dave Airlief453ba02008-11-07 14:05:41 -08002629
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002630 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002631out:
2632 mutex_unlock(&dev->mode_config.mutex);
2633 return ret;
2634}
2635
2636
2637/**
2638 * drm_fb_detachmode - Detach a user specified mode from an connector
2639 * @inode: inode from the ioctl
2640 * @filp: file * from the ioctl
2641 * @cmd: cmd from ioctl
2642 * @arg: arg from ioctl
2643 *
2644 * Called by the user via ioctl.
2645 *
2646 * RETURNS:
2647 * Zero on success, errno on failure.
2648 */
2649int drm_mode_detachmode_ioctl(struct drm_device *dev,
2650 void *data, struct drm_file *file_priv)
2651{
2652 struct drm_mode_object *obj;
2653 struct drm_mode_mode_cmd *mode_cmd = data;
2654 struct drm_connector *connector;
2655 struct drm_display_mode mode;
2656 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002657 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002658
Dave Airliefb3b06c2011-02-08 13:55:21 +10002659 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2660 return -EINVAL;
2661
Dave Airlief453ba02008-11-07 14:05:41 -08002662 mutex_lock(&dev->mode_config.mutex);
2663
2664 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2665 if (!obj) {
2666 ret = -EINVAL;
2667 goto out;
2668 }
2669 connector = obj_to_connector(obj);
2670
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002671 ret = drm_crtc_convert_umode(&mode, umode);
2672 if (ret) {
2673 DRM_DEBUG_KMS("Invalid mode\n");
2674 goto out;
2675 }
2676
Dave Airlief453ba02008-11-07 14:05:41 -08002677 ret = drm_mode_detachmode(dev, connector, &mode);
2678out:
2679 mutex_unlock(&dev->mode_config.mutex);
2680 return ret;
2681}
2682
2683struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2684 const char *name, int num_values)
2685{
2686 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002687 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002688
2689 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2690 if (!property)
2691 return NULL;
2692
2693 if (num_values) {
2694 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2695 if (!property->values)
2696 goto fail;
2697 }
2698
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002699 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2700 if (ret)
2701 goto fail;
2702
Dave Airlief453ba02008-11-07 14:05:41 -08002703 property->flags = flags;
2704 property->num_values = num_values;
2705 INIT_LIST_HEAD(&property->enum_blob_list);
2706
Vinson Lee471dd2e2011-11-10 11:55:40 -08002707 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002708 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002709 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2710 }
Dave Airlief453ba02008-11-07 14:05:41 -08002711
2712 list_add_tail(&property->head, &dev->mode_config.property_list);
2713 return property;
2714fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002715 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002716 kfree(property);
2717 return NULL;
2718}
2719EXPORT_SYMBOL(drm_property_create);
2720
Sascha Hauer4a67d392012-02-06 10:58:17 +01002721struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2722 const char *name,
2723 const struct drm_prop_enum_list *props,
2724 int num_values)
2725{
2726 struct drm_property *property;
2727 int i, ret;
2728
2729 flags |= DRM_MODE_PROP_ENUM;
2730
2731 property = drm_property_create(dev, flags, name, num_values);
2732 if (!property)
2733 return NULL;
2734
2735 for (i = 0; i < num_values; i++) {
2736 ret = drm_property_add_enum(property, i,
2737 props[i].type,
2738 props[i].name);
2739 if (ret) {
2740 drm_property_destroy(dev, property);
2741 return NULL;
2742 }
2743 }
2744
2745 return property;
2746}
2747EXPORT_SYMBOL(drm_property_create_enum);
2748
Rob Clark49e27542012-05-17 02:23:26 -06002749struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2750 int flags, const char *name,
2751 const struct drm_prop_enum_list *props,
2752 int num_values)
2753{
2754 struct drm_property *property;
2755 int i, ret;
2756
2757 flags |= DRM_MODE_PROP_BITMASK;
2758
2759 property = drm_property_create(dev, flags, name, num_values);
2760 if (!property)
2761 return NULL;
2762
2763 for (i = 0; i < num_values; i++) {
2764 ret = drm_property_add_enum(property, i,
2765 props[i].type,
2766 props[i].name);
2767 if (ret) {
2768 drm_property_destroy(dev, property);
2769 return NULL;
2770 }
2771 }
2772
2773 return property;
2774}
2775EXPORT_SYMBOL(drm_property_create_bitmask);
2776
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002777struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2778 const char *name,
2779 uint64_t min, uint64_t max)
2780{
2781 struct drm_property *property;
2782
2783 flags |= DRM_MODE_PROP_RANGE;
2784
2785 property = drm_property_create(dev, flags, name, 2);
2786 if (!property)
2787 return NULL;
2788
2789 property->values[0] = min;
2790 property->values[1] = max;
2791
2792 return property;
2793}
2794EXPORT_SYMBOL(drm_property_create_range);
2795
Dave Airlief453ba02008-11-07 14:05:41 -08002796int drm_property_add_enum(struct drm_property *property, int index,
2797 uint64_t value, const char *name)
2798{
2799 struct drm_property_enum *prop_enum;
2800
Rob Clark49e27542012-05-17 02:23:26 -06002801 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2802 return -EINVAL;
2803
2804 /*
2805 * Bitmask enum properties have the additional constraint of values
2806 * from 0 to 63
2807 */
2808 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002809 return -EINVAL;
2810
2811 if (!list_empty(&property->enum_blob_list)) {
2812 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2813 if (prop_enum->value == value) {
2814 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2815 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2816 return 0;
2817 }
2818 }
2819 }
2820
2821 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2822 if (!prop_enum)
2823 return -ENOMEM;
2824
2825 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2826 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2827 prop_enum->value = value;
2828
2829 property->values[index] = value;
2830 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2831 return 0;
2832}
2833EXPORT_SYMBOL(drm_property_add_enum);
2834
2835void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2836{
2837 struct drm_property_enum *prop_enum, *pt;
2838
2839 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2840 list_del(&prop_enum->head);
2841 kfree(prop_enum);
2842 }
2843
2844 if (property->num_values)
2845 kfree(property->values);
2846 drm_mode_object_put(dev, &property->base);
2847 list_del(&property->head);
2848 kfree(property);
2849}
2850EXPORT_SYMBOL(drm_property_destroy);
2851
Paulo Zanonic5431882012-05-15 18:09:02 -03002852void drm_object_attach_property(struct drm_mode_object *obj,
2853 struct drm_property *property,
2854 uint64_t init_val)
2855{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002856 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002857
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002858 if (count == DRM_OBJECT_MAX_PROPERTY) {
2859 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2860 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2861 "you see this message on the same object type.\n",
2862 obj->type);
2863 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002864 }
2865
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002866 obj->properties->ids[count] = property->base.id;
2867 obj->properties->values[count] = init_val;
2868 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002869}
2870EXPORT_SYMBOL(drm_object_attach_property);
2871
2872int drm_object_property_set_value(struct drm_mode_object *obj,
2873 struct drm_property *property, uint64_t val)
2874{
2875 int i;
2876
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002877 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002878 if (obj->properties->ids[i] == property->base.id) {
2879 obj->properties->values[i] = val;
2880 return 0;
2881 }
2882 }
2883
2884 return -EINVAL;
2885}
2886EXPORT_SYMBOL(drm_object_property_set_value);
2887
2888int drm_object_property_get_value(struct drm_mode_object *obj,
2889 struct drm_property *property, uint64_t *val)
2890{
2891 int i;
2892
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002893 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002894 if (obj->properties->ids[i] == property->base.id) {
2895 *val = obj->properties->values[i];
2896 return 0;
2897 }
2898 }
2899
2900 return -EINVAL;
2901}
2902EXPORT_SYMBOL(drm_object_property_get_value);
2903
Dave Airlief453ba02008-11-07 14:05:41 -08002904int drm_mode_getproperty_ioctl(struct drm_device *dev,
2905 void *data, struct drm_file *file_priv)
2906{
2907 struct drm_mode_object *obj;
2908 struct drm_mode_get_property *out_resp = data;
2909 struct drm_property *property;
2910 int enum_count = 0;
2911 int blob_count = 0;
2912 int value_count = 0;
2913 int ret = 0, i;
2914 int copied;
2915 struct drm_property_enum *prop_enum;
2916 struct drm_mode_property_enum __user *enum_ptr;
2917 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002918 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002919 uint64_t __user *values_ptr;
2920 uint32_t __user *blob_length_ptr;
2921
Dave Airliefb3b06c2011-02-08 13:55:21 +10002922 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2923 return -EINVAL;
2924
Dave Airlief453ba02008-11-07 14:05:41 -08002925 mutex_lock(&dev->mode_config.mutex);
2926 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2927 if (!obj) {
2928 ret = -EINVAL;
2929 goto done;
2930 }
2931 property = obj_to_property(obj);
2932
Rob Clark49e27542012-05-17 02:23:26 -06002933 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002934 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2935 enum_count++;
2936 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2937 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2938 blob_count++;
2939 }
2940
2941 value_count = property->num_values;
2942
2943 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2944 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2945 out_resp->flags = property->flags;
2946
2947 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002948 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002949 for (i = 0; i < value_count; i++) {
2950 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2951 ret = -EFAULT;
2952 goto done;
2953 }
2954 }
2955 }
2956 out_resp->count_values = value_count;
2957
Rob Clark49e27542012-05-17 02:23:26 -06002958 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002959 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2960 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002961 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002962 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2963
2964 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2965 ret = -EFAULT;
2966 goto done;
2967 }
2968
2969 if (copy_to_user(&enum_ptr[copied].name,
2970 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2971 ret = -EFAULT;
2972 goto done;
2973 }
2974 copied++;
2975 }
2976 }
2977 out_resp->count_enum_blobs = enum_count;
2978 }
2979
2980 if (property->flags & DRM_MODE_PROP_BLOB) {
2981 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2982 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002983 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2984 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002985
2986 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2987 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2988 ret = -EFAULT;
2989 goto done;
2990 }
2991
2992 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2993 ret = -EFAULT;
2994 goto done;
2995 }
2996
2997 copied++;
2998 }
2999 }
3000 out_resp->count_enum_blobs = blob_count;
3001 }
3002done:
3003 mutex_unlock(&dev->mode_config.mutex);
3004 return ret;
3005}
3006
3007static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3008 void *data)
3009{
3010 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003011 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003012
3013 if (!length || !data)
3014 return NULL;
3015
3016 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3017 if (!blob)
3018 return NULL;
3019
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003020 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3021 if (ret) {
3022 kfree(blob);
3023 return NULL;
3024 }
3025
Dave Airlief453ba02008-11-07 14:05:41 -08003026 blob->length = length;
3027
3028 memcpy(blob->data, data, length);
3029
Dave Airlief453ba02008-11-07 14:05:41 -08003030 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3031 return blob;
3032}
3033
3034static void drm_property_destroy_blob(struct drm_device *dev,
3035 struct drm_property_blob *blob)
3036{
3037 drm_mode_object_put(dev, &blob->base);
3038 list_del(&blob->head);
3039 kfree(blob);
3040}
3041
3042int drm_mode_getblob_ioctl(struct drm_device *dev,
3043 void *data, struct drm_file *file_priv)
3044{
3045 struct drm_mode_object *obj;
3046 struct drm_mode_get_blob *out_resp = data;
3047 struct drm_property_blob *blob;
3048 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003049 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003050
Dave Airliefb3b06c2011-02-08 13:55:21 +10003051 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3052 return -EINVAL;
3053
Dave Airlief453ba02008-11-07 14:05:41 -08003054 mutex_lock(&dev->mode_config.mutex);
3055 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3056 if (!obj) {
3057 ret = -EINVAL;
3058 goto done;
3059 }
3060 blob = obj_to_blob(obj);
3061
3062 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003063 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003064 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3065 ret = -EFAULT;
3066 goto done;
3067 }
3068 }
3069 out_resp->length = blob->length;
3070
3071done:
3072 mutex_unlock(&dev->mode_config.mutex);
3073 return ret;
3074}
3075
3076int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3077 struct edid *edid)
3078{
3079 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003080 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003081
3082 if (connector->edid_blob_ptr)
3083 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3084
3085 /* Delete edid, when there is none. */
3086 if (!edid) {
3087 connector->edid_blob_ptr = NULL;
Rob Clark58495562012-10-11 20:50:56 -05003088 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
Dave Airlief453ba02008-11-07 14:05:41 -08003089 return ret;
3090 }
3091
Adam Jackson7466f4c2010-03-29 21:43:23 +00003092 size = EDID_LENGTH * (1 + edid->extensions);
3093 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3094 size, edid);
Sachin Kamate655d122012-11-19 09:44:57 +00003095 if (!connector->edid_blob_ptr)
3096 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08003097
Rob Clark58495562012-10-11 20:50:56 -05003098 ret = drm_object_property_set_value(&connector->base,
Dave Airlief453ba02008-11-07 14:05:41 -08003099 dev->mode_config.edid_property,
3100 connector->edid_blob_ptr->base.id);
3101
3102 return ret;
3103}
3104EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3105
Paulo Zanoni26a34812012-05-15 18:08:59 -03003106static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003107 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003108{
3109 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3110 return false;
3111 if (property->flags & DRM_MODE_PROP_RANGE) {
3112 if (value < property->values[0] || value > property->values[1])
3113 return false;
3114 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003115 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3116 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003117 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003118 for (i = 0; i < property->num_values; i++)
3119 valid_mask |= (1ULL << property->values[i]);
3120 return !(value & ~valid_mask);
Ville Syrjäläc4a56752012-10-25 18:05:06 +00003121 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3122 /* Only the driver knows */
3123 return true;
Paulo Zanoni26a34812012-05-15 18:08:59 -03003124 } else {
3125 int i;
3126 for (i = 0; i < property->num_values; i++)
3127 if (property->values[i] == value)
3128 return true;
3129 return false;
3130 }
3131}
3132
Dave Airlief453ba02008-11-07 14:05:41 -08003133int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3134 void *data, struct drm_file *file_priv)
3135{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003136 struct drm_mode_connector_set_property *conn_set_prop = data;
3137 struct drm_mode_obj_set_property obj_set_prop = {
3138 .value = conn_set_prop->value,
3139 .prop_id = conn_set_prop->prop_id,
3140 .obj_id = conn_set_prop->connector_id,
3141 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3142 };
Dave Airlief453ba02008-11-07 14:05:41 -08003143
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003144 /* It does all the locking and checking we need */
3145 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003146}
3147
Paulo Zanonic5431882012-05-15 18:09:02 -03003148static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3149 struct drm_property *property,
3150 uint64_t value)
3151{
3152 int ret = -EINVAL;
3153 struct drm_connector *connector = obj_to_connector(obj);
3154
3155 /* Do DPMS ourselves */
3156 if (property == connector->dev->mode_config.dpms_property) {
3157 if (connector->funcs->dpms)
3158 (*connector->funcs->dpms)(connector, (int)value);
3159 ret = 0;
3160 } else if (connector->funcs->set_property)
3161 ret = connector->funcs->set_property(connector, property, value);
3162
3163 /* store the property value if successful */
3164 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05003165 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03003166 return ret;
3167}
3168
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003169static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3170 struct drm_property *property,
3171 uint64_t value)
3172{
3173 int ret = -EINVAL;
3174 struct drm_crtc *crtc = obj_to_crtc(obj);
3175
3176 if (crtc->funcs->set_property)
3177 ret = crtc->funcs->set_property(crtc, property, value);
3178 if (!ret)
3179 drm_object_property_set_value(obj, property, value);
3180
3181 return ret;
3182}
3183
Rob Clark4d939142012-05-17 02:23:27 -06003184static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3185 struct drm_property *property,
3186 uint64_t value)
3187{
3188 int ret = -EINVAL;
3189 struct drm_plane *plane = obj_to_plane(obj);
3190
3191 if (plane->funcs->set_property)
3192 ret = plane->funcs->set_property(plane, property, value);
3193 if (!ret)
3194 drm_object_property_set_value(obj, property, value);
3195
3196 return ret;
3197}
3198
Paulo Zanonic5431882012-05-15 18:09:02 -03003199int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3200 struct drm_file *file_priv)
3201{
3202 struct drm_mode_obj_get_properties *arg = data;
3203 struct drm_mode_object *obj;
3204 int ret = 0;
3205 int i;
3206 int copied = 0;
3207 int props_count = 0;
3208 uint32_t __user *props_ptr;
3209 uint64_t __user *prop_values_ptr;
3210
3211 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3212 return -EINVAL;
3213
3214 mutex_lock(&dev->mode_config.mutex);
3215
3216 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3217 if (!obj) {
3218 ret = -EINVAL;
3219 goto out;
3220 }
3221 if (!obj->properties) {
3222 ret = -EINVAL;
3223 goto out;
3224 }
3225
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003226 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003227
3228 /* This ioctl is called twice, once to determine how much space is
3229 * needed, and the 2nd time to fill it. */
3230 if ((arg->count_props >= props_count) && props_count) {
3231 copied = 0;
3232 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3233 prop_values_ptr = (uint64_t __user *)(unsigned long)
3234 (arg->prop_values_ptr);
3235 for (i = 0; i < props_count; i++) {
3236 if (put_user(obj->properties->ids[i],
3237 props_ptr + copied)) {
3238 ret = -EFAULT;
3239 goto out;
3240 }
3241 if (put_user(obj->properties->values[i],
3242 prop_values_ptr + copied)) {
3243 ret = -EFAULT;
3244 goto out;
3245 }
3246 copied++;
3247 }
3248 }
3249 arg->count_props = props_count;
3250out:
3251 mutex_unlock(&dev->mode_config.mutex);
3252 return ret;
3253}
3254
3255int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3256 struct drm_file *file_priv)
3257{
3258 struct drm_mode_obj_set_property *arg = data;
3259 struct drm_mode_object *arg_obj;
3260 struct drm_mode_object *prop_obj;
3261 struct drm_property *property;
3262 int ret = -EINVAL;
3263 int i;
3264
3265 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3266 return -EINVAL;
3267
3268 mutex_lock(&dev->mode_config.mutex);
3269
3270 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3271 if (!arg_obj)
3272 goto out;
3273 if (!arg_obj->properties)
3274 goto out;
3275
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003276 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003277 if (arg_obj->properties->ids[i] == arg->prop_id)
3278 break;
3279
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003280 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003281 goto out;
3282
3283 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3284 DRM_MODE_OBJECT_PROPERTY);
3285 if (!prop_obj)
3286 goto out;
3287 property = obj_to_property(prop_obj);
3288
3289 if (!drm_property_change_is_valid(property, arg->value))
3290 goto out;
3291
3292 switch (arg_obj->type) {
3293 case DRM_MODE_OBJECT_CONNECTOR:
3294 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3295 arg->value);
3296 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003297 case DRM_MODE_OBJECT_CRTC:
3298 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3299 break;
Rob Clark4d939142012-05-17 02:23:27 -06003300 case DRM_MODE_OBJECT_PLANE:
3301 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3302 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003303 }
3304
3305out:
3306 mutex_unlock(&dev->mode_config.mutex);
3307 return ret;
3308}
3309
Dave Airlief453ba02008-11-07 14:05:41 -08003310int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3311 struct drm_encoder *encoder)
3312{
3313 int i;
3314
3315 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3316 if (connector->encoder_ids[i] == 0) {
3317 connector->encoder_ids[i] = encoder->base.id;
3318 return 0;
3319 }
3320 }
3321 return -ENOMEM;
3322}
3323EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3324
3325void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3326 struct drm_encoder *encoder)
3327{
3328 int i;
3329 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3330 if (connector->encoder_ids[i] == encoder->base.id) {
3331 connector->encoder_ids[i] = 0;
3332 if (connector->encoder == encoder)
3333 connector->encoder = NULL;
3334 break;
3335 }
3336 }
3337}
3338EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3339
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003340int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003341 int gamma_size)
3342{
3343 crtc->gamma_size = gamma_size;
3344
3345 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3346 if (!crtc->gamma_store) {
3347 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003348 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003349 }
3350
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003351 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003352}
3353EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3354
3355int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3356 void *data, struct drm_file *file_priv)
3357{
3358 struct drm_mode_crtc_lut *crtc_lut = data;
3359 struct drm_mode_object *obj;
3360 struct drm_crtc *crtc;
3361 void *r_base, *g_base, *b_base;
3362 int size;
3363 int ret = 0;
3364
Dave Airliefb3b06c2011-02-08 13:55:21 +10003365 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3366 return -EINVAL;
3367
Dave Airlief453ba02008-11-07 14:05:41 -08003368 mutex_lock(&dev->mode_config.mutex);
3369 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3370 if (!obj) {
3371 ret = -EINVAL;
3372 goto out;
3373 }
3374 crtc = obj_to_crtc(obj);
3375
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003376 if (crtc->funcs->gamma_set == NULL) {
3377 ret = -ENOSYS;
3378 goto out;
3379 }
3380
Dave Airlief453ba02008-11-07 14:05:41 -08003381 /* memcpy into gamma store */
3382 if (crtc_lut->gamma_size != crtc->gamma_size) {
3383 ret = -EINVAL;
3384 goto out;
3385 }
3386
3387 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3388 r_base = crtc->gamma_store;
3389 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3390 ret = -EFAULT;
3391 goto out;
3392 }
3393
3394 g_base = r_base + size;
3395 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3396 ret = -EFAULT;
3397 goto out;
3398 }
3399
3400 b_base = g_base + size;
3401 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3402 ret = -EFAULT;
3403 goto out;
3404 }
3405
James Simmons72034252010-08-03 01:33:19 +01003406 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003407
3408out:
3409 mutex_unlock(&dev->mode_config.mutex);
3410 return ret;
3411
3412}
3413
3414int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3415 void *data, struct drm_file *file_priv)
3416{
3417 struct drm_mode_crtc_lut *crtc_lut = data;
3418 struct drm_mode_object *obj;
3419 struct drm_crtc *crtc;
3420 void *r_base, *g_base, *b_base;
3421 int size;
3422 int ret = 0;
3423
Dave Airliefb3b06c2011-02-08 13:55:21 +10003424 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3425 return -EINVAL;
3426
Dave Airlief453ba02008-11-07 14:05:41 -08003427 mutex_lock(&dev->mode_config.mutex);
3428 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3429 if (!obj) {
3430 ret = -EINVAL;
3431 goto out;
3432 }
3433 crtc = obj_to_crtc(obj);
3434
3435 /* memcpy into gamma store */
3436 if (crtc_lut->gamma_size != crtc->gamma_size) {
3437 ret = -EINVAL;
3438 goto out;
3439 }
3440
3441 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3442 r_base = crtc->gamma_store;
3443 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3444 ret = -EFAULT;
3445 goto out;
3446 }
3447
3448 g_base = r_base + size;
3449 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3450 ret = -EFAULT;
3451 goto out;
3452 }
3453
3454 b_base = g_base + size;
3455 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3456 ret = -EFAULT;
3457 goto out;
3458 }
3459out:
3460 mutex_unlock(&dev->mode_config.mutex);
3461 return ret;
3462}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003463
3464int drm_mode_page_flip_ioctl(struct drm_device *dev,
3465 void *data, struct drm_file *file_priv)
3466{
3467 struct drm_mode_crtc_page_flip *page_flip = data;
3468 struct drm_mode_object *obj;
3469 struct drm_crtc *crtc;
3470 struct drm_framebuffer *fb;
3471 struct drm_pending_vblank_event *e = NULL;
3472 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003473 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003474 int ret = -EINVAL;
3475
3476 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3477 page_flip->reserved != 0)
3478 return -EINVAL;
3479
3480 mutex_lock(&dev->mode_config.mutex);
3481 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3482 if (!obj)
3483 goto out;
3484 crtc = obj_to_crtc(obj);
3485
Chris Wilson90c1efd2010-07-17 20:23:26 +01003486 if (crtc->fb == NULL) {
3487 /* The framebuffer is currently unbound, presumably
3488 * due to a hotplug event, that userspace has not
3489 * yet discovered.
3490 */
3491 ret = -EBUSY;
3492 goto out;
3493 }
3494
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003495 if (crtc->funcs->page_flip == NULL)
3496 goto out;
3497
3498 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3499 if (!obj)
3500 goto out;
3501 fb = obj_to_fb(obj);
3502
Rob Clark7c80e122012-09-04 16:35:56 +00003503 hdisplay = crtc->mode.hdisplay;
3504 vdisplay = crtc->mode.vdisplay;
3505
3506 if (crtc->invert_dimensions)
3507 swap(hdisplay, vdisplay);
3508
3509 if (hdisplay > fb->width ||
3510 vdisplay > fb->height ||
3511 crtc->x > fb->width - hdisplay ||
3512 crtc->y > fb->height - vdisplay) {
3513 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3514 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3515 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003516 ret = -ENOSPC;
3517 goto out;
3518 }
3519
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003520 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3521 ret = -ENOMEM;
3522 spin_lock_irqsave(&dev->event_lock, flags);
3523 if (file_priv->event_space < sizeof e->event) {
3524 spin_unlock_irqrestore(&dev->event_lock, flags);
3525 goto out;
3526 }
3527 file_priv->event_space -= sizeof e->event;
3528 spin_unlock_irqrestore(&dev->event_lock, flags);
3529
3530 e = kzalloc(sizeof *e, GFP_KERNEL);
3531 if (e == NULL) {
3532 spin_lock_irqsave(&dev->event_lock, flags);
3533 file_priv->event_space += sizeof e->event;
3534 spin_unlock_irqrestore(&dev->event_lock, flags);
3535 goto out;
3536 }
3537
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003538 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003539 e->event.base.length = sizeof e->event;
3540 e->event.user_data = page_flip->user_data;
3541 e->base.event = &e->event.base;
3542 e->base.file_priv = file_priv;
3543 e->base.destroy =
3544 (void (*) (struct drm_pending_event *)) kfree;
3545 }
3546
3547 ret = crtc->funcs->page_flip(crtc, fb, e);
3548 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003549 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3550 spin_lock_irqsave(&dev->event_lock, flags);
3551 file_priv->event_space += sizeof e->event;
3552 spin_unlock_irqrestore(&dev->event_lock, flags);
3553 kfree(e);
3554 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003555 }
3556
3557out:
3558 mutex_unlock(&dev->mode_config.mutex);
3559 return ret;
3560}
Chris Wilsoneb033552011-01-24 15:11:08 +00003561
3562void drm_mode_config_reset(struct drm_device *dev)
3563{
3564 struct drm_crtc *crtc;
3565 struct drm_encoder *encoder;
3566 struct drm_connector *connector;
3567
3568 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3569 if (crtc->funcs->reset)
3570 crtc->funcs->reset(crtc);
3571
3572 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3573 if (encoder->funcs->reset)
3574 encoder->funcs->reset(encoder);
3575
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003576 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3577 connector->status = connector_status_unknown;
3578
Chris Wilsoneb033552011-01-24 15:11:08 +00003579 if (connector->funcs->reset)
3580 connector->funcs->reset(connector);
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +00003581 }
Chris Wilsoneb033552011-01-24 15:11:08 +00003582}
3583EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003584
3585int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3586 void *data, struct drm_file *file_priv)
3587{
3588 struct drm_mode_create_dumb *args = data;
3589
3590 if (!dev->driver->dumb_create)
3591 return -ENOSYS;
3592 return dev->driver->dumb_create(file_priv, dev, args);
3593}
3594
3595int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3596 void *data, struct drm_file *file_priv)
3597{
3598 struct drm_mode_map_dumb *args = data;
3599
3600 /* call driver ioctl to get mmap offset */
3601 if (!dev->driver->dumb_map_offset)
3602 return -ENOSYS;
3603
3604 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3605}
3606
3607int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3608 void *data, struct drm_file *file_priv)
3609{
3610 struct drm_mode_destroy_dumb *args = data;
3611
3612 if (!dev->driver->dumb_destroy)
3613 return -ENOSYS;
3614
3615 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3616}
Dave Airlie248dbc22011-11-29 20:02:54 +00003617
3618/*
3619 * Just need to support RGB formats here for compat with code that doesn't
3620 * use pixel formats directly yet.
3621 */
3622void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3623 int *bpp)
3624{
3625 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003626 case DRM_FORMAT_RGB332:
3627 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003628 *depth = 8;
3629 *bpp = 8;
3630 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003631 case DRM_FORMAT_XRGB1555:
3632 case DRM_FORMAT_XBGR1555:
3633 case DRM_FORMAT_RGBX5551:
3634 case DRM_FORMAT_BGRX5551:
3635 case DRM_FORMAT_ARGB1555:
3636 case DRM_FORMAT_ABGR1555:
3637 case DRM_FORMAT_RGBA5551:
3638 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003639 *depth = 15;
3640 *bpp = 16;
3641 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003642 case DRM_FORMAT_RGB565:
3643 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003644 *depth = 16;
3645 *bpp = 16;
3646 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003647 case DRM_FORMAT_RGB888:
3648 case DRM_FORMAT_BGR888:
3649 *depth = 24;
3650 *bpp = 24;
3651 break;
3652 case DRM_FORMAT_XRGB8888:
3653 case DRM_FORMAT_XBGR8888:
3654 case DRM_FORMAT_RGBX8888:
3655 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003656 *depth = 24;
3657 *bpp = 32;
3658 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003659 case DRM_FORMAT_XRGB2101010:
3660 case DRM_FORMAT_XBGR2101010:
3661 case DRM_FORMAT_RGBX1010102:
3662 case DRM_FORMAT_BGRX1010102:
3663 case DRM_FORMAT_ARGB2101010:
3664 case DRM_FORMAT_ABGR2101010:
3665 case DRM_FORMAT_RGBA1010102:
3666 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003667 *depth = 30;
3668 *bpp = 32;
3669 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003670 case DRM_FORMAT_ARGB8888:
3671 case DRM_FORMAT_ABGR8888:
3672 case DRM_FORMAT_RGBA8888:
3673 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003674 *depth = 32;
3675 *bpp = 32;
3676 break;
3677 default:
3678 DRM_DEBUG_KMS("unsupported pixel format\n");
3679 *depth = 0;
3680 *bpp = 0;
3681 break;
3682 }
3683}
3684EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003685
3686/**
3687 * drm_format_num_planes - get the number of planes for format
3688 * @format: pixel format (DRM_FORMAT_*)
3689 *
3690 * RETURNS:
3691 * The number of planes used by the specified pixel format.
3692 */
3693int drm_format_num_planes(uint32_t format)
3694{
3695 switch (format) {
3696 case DRM_FORMAT_YUV410:
3697 case DRM_FORMAT_YVU410:
3698 case DRM_FORMAT_YUV411:
3699 case DRM_FORMAT_YVU411:
3700 case DRM_FORMAT_YUV420:
3701 case DRM_FORMAT_YVU420:
3702 case DRM_FORMAT_YUV422:
3703 case DRM_FORMAT_YVU422:
3704 case DRM_FORMAT_YUV444:
3705 case DRM_FORMAT_YVU444:
3706 return 3;
3707 case DRM_FORMAT_NV12:
3708 case DRM_FORMAT_NV21:
3709 case DRM_FORMAT_NV16:
3710 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003711 case DRM_FORMAT_NV24:
3712 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003713 return 2;
3714 default:
3715 return 1;
3716 }
3717}
3718EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003719
3720/**
3721 * drm_format_plane_cpp - determine the bytes per pixel value
3722 * @format: pixel format (DRM_FORMAT_*)
3723 * @plane: plane index
3724 *
3725 * RETURNS:
3726 * The bytes per pixel value for the specified plane.
3727 */
3728int drm_format_plane_cpp(uint32_t format, int plane)
3729{
3730 unsigned int depth;
3731 int bpp;
3732
3733 if (plane >= drm_format_num_planes(format))
3734 return 0;
3735
3736 switch (format) {
3737 case DRM_FORMAT_YUYV:
3738 case DRM_FORMAT_YVYU:
3739 case DRM_FORMAT_UYVY:
3740 case DRM_FORMAT_VYUY:
3741 return 2;
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ä5a86bd52012-04-05 21:35:16 +03003748 return plane ? 2 : 1;
3749 case DRM_FORMAT_YUV410:
3750 case DRM_FORMAT_YVU410:
3751 case DRM_FORMAT_YUV411:
3752 case DRM_FORMAT_YVU411:
3753 case DRM_FORMAT_YUV420:
3754 case DRM_FORMAT_YVU420:
3755 case DRM_FORMAT_YUV422:
3756 case DRM_FORMAT_YVU422:
3757 case DRM_FORMAT_YUV444:
3758 case DRM_FORMAT_YVU444:
3759 return 1;
3760 default:
3761 drm_fb_get_bpp_depth(format, &depth, &bpp);
3762 return bpp >> 3;
3763 }
3764}
3765EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003766
3767/**
3768 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3769 * @format: pixel format (DRM_FORMAT_*)
3770 *
3771 * RETURNS:
3772 * The horizontal chroma subsampling factor for the
3773 * specified pixel format.
3774 */
3775int drm_format_horz_chroma_subsampling(uint32_t format)
3776{
3777 switch (format) {
3778 case DRM_FORMAT_YUV411:
3779 case DRM_FORMAT_YVU411:
3780 case DRM_FORMAT_YUV410:
3781 case DRM_FORMAT_YVU410:
3782 return 4;
3783 case DRM_FORMAT_YUYV:
3784 case DRM_FORMAT_YVYU:
3785 case DRM_FORMAT_UYVY:
3786 case DRM_FORMAT_VYUY:
3787 case DRM_FORMAT_NV12:
3788 case DRM_FORMAT_NV21:
3789 case DRM_FORMAT_NV16:
3790 case DRM_FORMAT_NV61:
3791 case DRM_FORMAT_YUV422:
3792 case DRM_FORMAT_YVU422:
3793 case DRM_FORMAT_YUV420:
3794 case DRM_FORMAT_YVU420:
3795 return 2;
3796 default:
3797 return 1;
3798 }
3799}
3800EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3801
3802/**
3803 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3804 * @format: pixel format (DRM_FORMAT_*)
3805 *
3806 * RETURNS:
3807 * The vertical chroma subsampling factor for the
3808 * specified pixel format.
3809 */
3810int drm_format_vert_chroma_subsampling(uint32_t format)
3811{
3812 switch (format) {
3813 case DRM_FORMAT_YUV410:
3814 case DRM_FORMAT_YVU410:
3815 return 4;
3816 case DRM_FORMAT_YUV420:
3817 case DRM_FORMAT_YVU420:
3818 case DRM_FORMAT_NV12:
3819 case DRM_FORMAT_NV21:
3820 return 2;
3821 default:
3822 return 1;
3823 }
3824}
3825EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);