blob: 9c346a50379f65de7ac8ce2787fae639936d097c [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>
Dave Airlief453ba02008-11-07 14:05:41 -080035#include "drm.h"
36#include "drmP.h"
37#include "drm_crtc.h"
Adam Jackson7466f4c2010-03-29 21:43:23 +000038#include "drm_edid.h"
Jesse Barnes308e5bc2011-11-14 14:51:28 -080039#include "drm_fourcc.h"
Dave Airlief453ba02008-11-07 14:05:41 -080040
Dave Airlief453ba02008-11-07 14:05:41 -080041/* Avoid boilerplate. I'm tired of typing. */
42#define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
44 { \
45 int i; \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
49 } \
50 return "(unknown)"; \
51 }
52
53/*
54 * Global properties
55 */
56static struct drm_prop_enum_list drm_dpms_enum_list[] =
57{ { DRM_MODE_DPMS_ON, "On" },
58 { DRM_MODE_DPMS_STANDBY, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
60 { DRM_MODE_DPMS_OFF, "Off" }
61};
62
63DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
64
65/*
66 * Optional properties
67 */
68static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69{
Jesse Barnes53bd8382009-07-01 10:04:40 -070070 { DRM_MODE_SCALE_NONE, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
72 { DRM_MODE_SCALE_CENTER, "Center" },
73 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080074};
75
76static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77{
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100080 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080081};
82
83/*
84 * Non-global properties, but "required" for certain connectors.
85 */
86static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87{
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
91};
92
93DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96{
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
100};
101
102DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106{
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800112};
113
114DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117{
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000128static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
132};
133
134DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
Dave Airlief453ba02008-11-07 14:05:41 -0800137struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141};
142
143/*
144 * Connector and encoder types.
145 */
146static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500156 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800163};
164
165static struct drm_prop_enum_list drm_encoder_enum_list[] =
166{ { DRM_MODE_ENCODER_NONE, "None" },
167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800172};
173
174char *drm_get_encoder_name(struct drm_encoder *encoder)
175{
176 static char buf[32];
177
178 snprintf(buf, 32, "%s-%d",
179 drm_encoder_enum_list[encoder->encoder_type].name,
180 encoder->base.id);
181 return buf;
182}
Dave Airlie13a81952009-09-07 15:45:33 +1000183EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800184
185char *drm_get_connector_name(struct drm_connector *connector)
186{
187 static char buf[32];
188
189 snprintf(buf, 32, "%s-%d",
190 drm_connector_enum_list[connector->connector_type].name,
191 connector->connector_type_id);
192 return buf;
193}
194EXPORT_SYMBOL(drm_get_connector_name);
195
196char *drm_get_connector_status_name(enum drm_connector_status status)
197{
198 if (status == connector_status_connected)
199 return "connected";
200 else if (status == connector_status_disconnected)
201 return "disconnected";
202 else
203 return "unknown";
204}
205
206/**
207 * drm_mode_object_get - allocate a new identifier
208 * @dev: DRM device
209 * @ptr: object pointer, used to generate unique ID
210 * @type: object type
211 *
212 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800213 *
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
216 *
217 * RETURNS:
218 * New unique (relative to other objects in @dev) integer identifier for the
219 * object.
220 */
221static int drm_mode_object_get(struct drm_device *dev,
222 struct drm_mode_object *obj, uint32_t obj_type)
223{
224 int new_id = 0;
225 int ret;
226
Dave Airlief453ba02008-11-07 14:05:41 -0800227again:
228 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
229 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200230 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800231 }
232
Jesse Barnesad2563c2009-01-19 17:21:45 +1000233 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800234 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000235 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800236 if (ret == -EAGAIN)
237 goto again;
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200238 else if (ret)
239 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800240
241 obj->id = new_id;
242 obj->type = obj_type;
243 return 0;
244}
245
246/**
247 * drm_mode_object_put - free an identifer
248 * @dev: DRM device
249 * @id: ID to free
250 *
251 * LOCKING:
252 * Caller must hold DRM mode_config lock.
253 *
254 * Free @id from @dev's unique identifier pool.
255 */
256static void drm_mode_object_put(struct drm_device *dev,
257 struct drm_mode_object *object)
258{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000259 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800260 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000261 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800262}
263
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200264struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
265 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800266{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000267 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800268
Jesse Barnesad2563c2009-01-19 17:21:45 +1000269 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800270 obj = idr_find(&dev->mode_config.crtc_idr, id);
271 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000272 obj = NULL;
273 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800274
275 return obj;
276}
277EXPORT_SYMBOL(drm_mode_object_find);
278
279/**
Dave Airlief453ba02008-11-07 14:05:41 -0800280 * drm_framebuffer_init - initialize a framebuffer
281 * @dev: DRM device
282 *
283 * LOCKING:
284 * Caller must hold mode config lock.
285 *
286 * Allocates an ID for the framebuffer's parent mode object, sets its mode
287 * functions & device file and adds it to the master fd list.
288 *
289 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200290 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800291 */
292int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
293 const struct drm_framebuffer_funcs *funcs)
294{
295 int ret;
296
Rob Clarkf7eff602012-09-05 21:48:38 +0000297 kref_init(&fb->refcount);
298
Dave Airlief453ba02008-11-07 14:05:41 -0800299 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200300 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800301 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800302
303 fb->dev = dev;
304 fb->funcs = funcs;
305 dev->mode_config.num_fb++;
306 list_add(&fb->head, &dev->mode_config.fb_list);
307
308 return 0;
309}
310EXPORT_SYMBOL(drm_framebuffer_init);
311
Rob Clarkf7eff602012-09-05 21:48:38 +0000312static void drm_framebuffer_free(struct kref *kref)
313{
314 struct drm_framebuffer *fb =
315 container_of(kref, struct drm_framebuffer, refcount);
316 fb->funcs->destroy(fb);
317}
318
319/**
320 * drm_framebuffer_unreference - unref a framebuffer
321 *
322 * LOCKING:
323 * Caller must hold mode config lock.
324 */
325void drm_framebuffer_unreference(struct drm_framebuffer *fb)
326{
327 struct drm_device *dev = fb->dev;
328 DRM_DEBUG("FB ID: %d\n", fb->base.id);
329 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
330 kref_put(&fb->refcount, drm_framebuffer_free);
331}
332EXPORT_SYMBOL(drm_framebuffer_unreference);
333
334/**
335 * drm_framebuffer_reference - incr the fb refcnt
336 */
337void drm_framebuffer_reference(struct drm_framebuffer *fb)
338{
339 DRM_DEBUG("FB ID: %d\n", fb->base.id);
340 kref_get(&fb->refcount);
341}
342EXPORT_SYMBOL(drm_framebuffer_reference);
343
Dave Airlief453ba02008-11-07 14:05:41 -0800344/**
345 * drm_framebuffer_cleanup - remove a framebuffer object
346 * @fb: framebuffer to remove
347 *
348 * LOCKING:
349 * Caller must hold mode config lock.
350 *
351 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
352 * it, setting it to NULL.
353 */
354void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
355{
356 struct drm_device *dev = fb->dev;
Rob Clarkf7eff602012-09-05 21:48:38 +0000357 /*
358 * This could be moved to drm_framebuffer_remove(), but for
359 * debugging is nice to keep around the list of fb's that are
360 * no longer associated w/ a drm_file but are not unreferenced
361 * yet. (i915 and omapdrm have debugfs files which will show
362 * this.)
363 */
364 drm_mode_object_put(dev, &fb->base);
365 list_del(&fb->head);
366 dev->mode_config.num_fb--;
367}
368EXPORT_SYMBOL(drm_framebuffer_cleanup);
369
370/**
371 * drm_framebuffer_remove - remove and unreference a framebuffer object
372 * @fb: framebuffer to remove
373 *
374 * LOCKING:
375 * Caller must hold mode config lock.
376 *
377 * Scans all the CRTCs and planes in @dev's mode_config. If they're
378 * using @fb, removes it, setting it to NULL.
379 */
380void drm_framebuffer_remove(struct drm_framebuffer *fb)
381{
382 struct drm_device *dev = fb->dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800383 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800384 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000385 struct drm_mode_set set;
386 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800387
388 /* remove from any CRTC */
389 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000390 if (crtc->fb == fb) {
391 /* should turn off the crtc */
392 memset(&set, 0, sizeof(struct drm_mode_set));
393 set.crtc = crtc;
394 set.fb = NULL;
395 ret = crtc->funcs->set_config(&set);
396 if (ret)
397 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
398 }
Dave Airlief453ba02008-11-07 14:05:41 -0800399 }
400
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800401 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
402 if (plane->fb == fb) {
403 /* should turn off the crtc */
404 ret = plane->funcs->disable_plane(plane);
405 if (ret)
406 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600407 /* disconnect the plane from the fb and crtc: */
408 plane->fb = NULL;
409 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800410 }
411 }
412
Rob Clarkf7eff602012-09-05 21:48:38 +0000413 list_del(&fb->filp_head);
414
415 drm_framebuffer_unreference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -0800416}
Rob Clarkf7eff602012-09-05 21:48:38 +0000417EXPORT_SYMBOL(drm_framebuffer_remove);
Dave Airlief453ba02008-11-07 14:05:41 -0800418
419/**
420 * drm_crtc_init - Initialise a new CRTC object
421 * @dev: DRM device
422 * @crtc: CRTC object to init
423 * @funcs: callbacks for the new CRTC
424 *
425 * LOCKING:
Laurent Pinchart3b02ab82012-05-17 13:27:20 +0200426 * Takes mode_config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800427 *
428 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200429 *
430 * RETURNS:
431 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800432 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200433int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800434 const struct drm_crtc_funcs *funcs)
435{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200436 int ret;
437
Dave Airlief453ba02008-11-07 14:05:41 -0800438 crtc->dev = dev;
439 crtc->funcs = funcs;
Rob Clark7c80e122012-09-04 16:35:56 +0000440 crtc->invert_dimensions = false;
Dave Airlief453ba02008-11-07 14:05:41 -0800441
442 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200443
444 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
445 if (ret)
446 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800447
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300448 crtc->base.properties = &crtc->properties;
449
Dave Airlief453ba02008-11-07 14:05:41 -0800450 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
451 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200452
453 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800454 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200455
456 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800457}
458EXPORT_SYMBOL(drm_crtc_init);
459
460/**
461 * drm_crtc_cleanup - Cleans up the core crtc usage.
462 * @crtc: CRTC to cleanup
463 *
464 * LOCKING:
465 * Caller must hold mode config lock.
466 *
467 * Cleanup @crtc. Removes from drm modesetting space
468 * does NOT free object, caller does that.
469 */
470void drm_crtc_cleanup(struct drm_crtc *crtc)
471{
472 struct drm_device *dev = crtc->dev;
473
474 if (crtc->gamma_store) {
475 kfree(crtc->gamma_store);
476 crtc->gamma_store = NULL;
477 }
478
479 drm_mode_object_put(dev, &crtc->base);
480 list_del(&crtc->head);
481 dev->mode_config.num_crtc--;
482}
483EXPORT_SYMBOL(drm_crtc_cleanup);
484
485/**
486 * drm_mode_probed_add - add a mode to a connector's probed mode list
487 * @connector: connector the new mode
488 * @mode: mode data
489 *
490 * LOCKING:
491 * Caller must hold mode config lock.
492 *
493 * Add @mode to @connector's mode list for later use.
494 */
495void drm_mode_probed_add(struct drm_connector *connector,
496 struct drm_display_mode *mode)
497{
498 list_add(&mode->head, &connector->probed_modes);
499}
500EXPORT_SYMBOL(drm_mode_probed_add);
501
502/**
503 * drm_mode_remove - remove and free a mode
504 * @connector: connector list to modify
505 * @mode: mode to remove
506 *
507 * LOCKING:
508 * Caller must hold mode config lock.
509 *
510 * Remove @mode from @connector's mode list, then free it.
511 */
512void drm_mode_remove(struct drm_connector *connector,
513 struct drm_display_mode *mode)
514{
515 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100516 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800517}
518EXPORT_SYMBOL(drm_mode_remove);
519
520/**
521 * drm_connector_init - Init a preallocated connector
522 * @dev: DRM device
523 * @connector: the connector to init
524 * @funcs: callbacks for this connector
525 * @name: user visible name of the connector
526 *
527 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100528 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800529 *
530 * Initialises a preallocated connector. Connectors should be
531 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200532 *
533 * RETURNS:
534 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800535 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200536int drm_connector_init(struct drm_device *dev,
537 struct drm_connector *connector,
538 const struct drm_connector_funcs *funcs,
539 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800540{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200541 int ret;
542
Dave Airlief453ba02008-11-07 14:05:41 -0800543 mutex_lock(&dev->mode_config.mutex);
544
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200545 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
546 if (ret)
547 goto out;
548
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300549 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800550 connector->dev = dev;
551 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800552 connector->connector_type = connector_type;
553 connector->connector_type_id =
554 ++drm_connector_enum_list[connector_type].count; /* TODO */
555 INIT_LIST_HEAD(&connector->user_modes);
556 INIT_LIST_HEAD(&connector->probed_modes);
557 INIT_LIST_HEAD(&connector->modes);
558 connector->edid_blob_ptr = NULL;
559
560 list_add_tail(&connector->head, &dev->mode_config.connector_list);
561 dev->mode_config.num_connector++;
562
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200563 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
564 drm_connector_attach_property(connector,
565 dev->mode_config.edid_property,
566 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800567
568 drm_connector_attach_property(connector,
569 dev->mode_config.dpms_property, 0);
570
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200571 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800572 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200573
574 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800575}
576EXPORT_SYMBOL(drm_connector_init);
577
578/**
579 * drm_connector_cleanup - cleans up an initialised connector
580 * @connector: connector to cleanup
581 *
582 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100583 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800584 *
585 * Cleans up the connector but doesn't free the object.
586 */
587void drm_connector_cleanup(struct drm_connector *connector)
588{
589 struct drm_device *dev = connector->dev;
590 struct drm_display_mode *mode, *t;
591
592 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
593 drm_mode_remove(connector, mode);
594
595 list_for_each_entry_safe(mode, t, &connector->modes, head)
596 drm_mode_remove(connector, mode);
597
598 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
599 drm_mode_remove(connector, mode);
600
601 mutex_lock(&dev->mode_config.mutex);
602 drm_mode_object_put(dev, &connector->base);
603 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000604 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800605 mutex_unlock(&dev->mode_config.mutex);
606}
607EXPORT_SYMBOL(drm_connector_cleanup);
608
Dave Airliecbc7e222012-02-20 14:16:40 +0000609void drm_connector_unplug_all(struct drm_device *dev)
610{
611 struct drm_connector *connector;
612
613 /* taking the mode config mutex ends up in a clash with sysfs */
614 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
615 drm_sysfs_connector_remove(connector);
616
617}
618EXPORT_SYMBOL(drm_connector_unplug_all);
619
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200620int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000621 struct drm_encoder *encoder,
622 const struct drm_encoder_funcs *funcs,
623 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800624{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200625 int ret;
626
Dave Airlief453ba02008-11-07 14:05:41 -0800627 mutex_lock(&dev->mode_config.mutex);
628
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200629 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
630 if (ret)
631 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800632
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200633 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800634 encoder->encoder_type = encoder_type;
635 encoder->funcs = funcs;
636
637 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
638 dev->mode_config.num_encoder++;
639
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200640 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800641 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200642
643 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800644}
645EXPORT_SYMBOL(drm_encoder_init);
646
647void drm_encoder_cleanup(struct drm_encoder *encoder)
648{
649 struct drm_device *dev = encoder->dev;
650 mutex_lock(&dev->mode_config.mutex);
651 drm_mode_object_put(dev, &encoder->base);
652 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000653 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800654 mutex_unlock(&dev->mode_config.mutex);
655}
656EXPORT_SYMBOL(drm_encoder_cleanup);
657
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800658int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
659 unsigned long possible_crtcs,
660 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600661 const uint32_t *formats, uint32_t format_count,
662 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800663{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200664 int ret;
665
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800666 mutex_lock(&dev->mode_config.mutex);
667
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200668 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
669 if (ret)
670 goto out;
671
Rob Clark4d939142012-05-17 02:23:27 -0600672 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800673 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800674 plane->funcs = funcs;
675 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
676 GFP_KERNEL);
677 if (!plane->format_types) {
678 DRM_DEBUG_KMS("out of memory when allocating plane\n");
679 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200680 ret = -ENOMEM;
681 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800682 }
683
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800684 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800685 plane->format_count = format_count;
686 plane->possible_crtcs = possible_crtcs;
687
Rob Clark0a7eb242011-12-13 20:19:36 -0600688 /* private planes are not exposed to userspace, but depending on
689 * display hardware, might be convenient to allow sharing programming
690 * for the scanout engine with the crtc implementation.
691 */
692 if (!priv) {
693 list_add_tail(&plane->head, &dev->mode_config.plane_list);
694 dev->mode_config.num_plane++;
695 } else {
696 INIT_LIST_HEAD(&plane->head);
697 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800698
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200699 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800700 mutex_unlock(&dev->mode_config.mutex);
701
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200702 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800703}
704EXPORT_SYMBOL(drm_plane_init);
705
706void drm_plane_cleanup(struct drm_plane *plane)
707{
708 struct drm_device *dev = plane->dev;
709
710 mutex_lock(&dev->mode_config.mutex);
711 kfree(plane->format_types);
712 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600713 /* if not added to a list, it must be a private plane */
714 if (!list_empty(&plane->head)) {
715 list_del(&plane->head);
716 dev->mode_config.num_plane--;
717 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800718 mutex_unlock(&dev->mode_config.mutex);
719}
720EXPORT_SYMBOL(drm_plane_cleanup);
721
Dave Airlief453ba02008-11-07 14:05:41 -0800722/**
723 * drm_mode_create - create a new display mode
724 * @dev: DRM device
725 *
726 * LOCKING:
727 * Caller must hold DRM mode_config lock.
728 *
729 * Create a new drm_display_mode, give it an ID, and return it.
730 *
731 * RETURNS:
732 * Pointer to new mode on success, NULL on error.
733 */
734struct drm_display_mode *drm_mode_create(struct drm_device *dev)
735{
736 struct drm_display_mode *nmode;
737
738 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
739 if (!nmode)
740 return NULL;
741
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200742 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
743 kfree(nmode);
744 return NULL;
745 }
746
Dave Airlief453ba02008-11-07 14:05:41 -0800747 return nmode;
748}
749EXPORT_SYMBOL(drm_mode_create);
750
751/**
752 * drm_mode_destroy - remove a mode
753 * @dev: DRM device
754 * @mode: mode to remove
755 *
756 * LOCKING:
757 * Caller must hold mode config lock.
758 *
759 * Free @mode's unique identifier, then free it.
760 */
761void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
762{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200763 if (!mode)
764 return;
765
Dave Airlief453ba02008-11-07 14:05:41 -0800766 drm_mode_object_put(dev, &mode->base);
767
768 kfree(mode);
769}
770EXPORT_SYMBOL(drm_mode_destroy);
771
772static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
773{
774 struct drm_property *edid;
775 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800776
777 /*
778 * Standard properties (apply to all connectors)
779 */
780 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
781 DRM_MODE_PROP_IMMUTABLE,
782 "EDID", 0);
783 dev->mode_config.edid_property = edid;
784
Sascha Hauer4a67d392012-02-06 10:58:17 +0100785 dpms = drm_property_create_enum(dev, 0,
786 "DPMS", drm_dpms_enum_list,
787 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800788 dev->mode_config.dpms_property = dpms;
789
790 return 0;
791}
792
793/**
794 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
795 * @dev: DRM device
796 *
797 * Called by a driver the first time a DVI-I connector is made.
798 */
799int drm_mode_create_dvi_i_properties(struct drm_device *dev)
800{
801 struct drm_property *dvi_i_selector;
802 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800803
804 if (dev->mode_config.dvi_i_select_subconnector_property)
805 return 0;
806
807 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100808 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800809 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100810 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800811 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800812 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
813
Sascha Hauer4a67d392012-02-06 10:58:17 +0100814 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800815 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100816 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800817 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800818 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
819
820 return 0;
821}
822EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
823
824/**
825 * drm_create_tv_properties - create TV specific connector properties
826 * @dev: DRM device
827 * @num_modes: number of different TV formats (modes) supported
828 * @modes: array of pointers to strings containing name of each format
829 *
830 * Called by a driver's TV initialization routine, this function creates
831 * the TV specific connector properties for a given device. Caller is
832 * responsible for allocating a list of format names and passing them to
833 * this routine.
834 */
835int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
836 char *modes[])
837{
838 struct drm_property *tv_selector;
839 struct drm_property *tv_subconnector;
840 int i;
841
842 if (dev->mode_config.tv_select_subconnector_property)
843 return 0;
844
845 /*
846 * Basic connector properties
847 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100848 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800849 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100850 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800851 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800852 dev->mode_config.tv_select_subconnector_property = tv_selector;
853
854 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100855 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
856 "subconnector",
857 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800858 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800859 dev->mode_config.tv_subconnector_property = tv_subconnector;
860
861 /*
862 * Other, TV specific properties: margins & TV modes.
863 */
864 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100865 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800866
867 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100868 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800869
870 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100871 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800872
873 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100874 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800875
876 dev->mode_config.tv_mode_property =
877 drm_property_create(dev, DRM_MODE_PROP_ENUM,
878 "mode", num_modes);
879 for (i = 0; i < num_modes; i++)
880 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
881 i, modes[i]);
882
Francisco Jerezb6b79022009-08-02 04:19:20 +0200883 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100884 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200885
886 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100887 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200888
889 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100890 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200891
Francisco Jereza75f0232009-08-12 02:30:10 +0200892 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100893 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200894
895 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100896 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200897
898 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100899 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200900
Dave Airlief453ba02008-11-07 14:05:41 -0800901 return 0;
902}
903EXPORT_SYMBOL(drm_mode_create_tv_properties);
904
905/**
906 * drm_mode_create_scaling_mode_property - create scaling mode property
907 * @dev: DRM device
908 *
909 * Called by a driver the first time it's needed, must be attached to desired
910 * connectors.
911 */
912int drm_mode_create_scaling_mode_property(struct drm_device *dev)
913{
914 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800915
916 if (dev->mode_config.scaling_mode_property)
917 return 0;
918
919 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100920 drm_property_create_enum(dev, 0, "scaling mode",
921 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800922 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800923
924 dev->mode_config.scaling_mode_property = scaling_mode;
925
926 return 0;
927}
928EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
929
930/**
931 * drm_mode_create_dithering_property - create dithering property
932 * @dev: DRM device
933 *
934 * Called by a driver the first time it's needed, must be attached to desired
935 * connectors.
936 */
937int drm_mode_create_dithering_property(struct drm_device *dev)
938{
939 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800940
941 if (dev->mode_config.dithering_mode_property)
942 return 0;
943
944 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100945 drm_property_create_enum(dev, 0, "dithering",
946 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800947 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800948 dev->mode_config.dithering_mode_property = dithering_mode;
949
950 return 0;
951}
952EXPORT_SYMBOL(drm_mode_create_dithering_property);
953
954/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000955 * drm_mode_create_dirty_property - create dirty property
956 * @dev: DRM device
957 *
958 * Called by a driver the first time it's needed, must be attached to desired
959 * connectors.
960 */
961int drm_mode_create_dirty_info_property(struct drm_device *dev)
962{
963 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000964
965 if (dev->mode_config.dirty_info_property)
966 return 0;
967
968 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100969 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000970 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100971 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000972 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000973 dev->mode_config.dirty_info_property = dirty_info;
974
975 return 0;
976}
977EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
978
979/**
Dave Airlief453ba02008-11-07 14:05:41 -0800980 * drm_mode_config_init - initialize DRM mode_configuration structure
981 * @dev: DRM device
982 *
983 * LOCKING:
984 * None, should happen single threaded at init time.
985 *
986 * Initialize @dev's mode_config structure, used for tracking the graphics
987 * configuration of @dev.
988 */
989void drm_mode_config_init(struct drm_device *dev)
990{
991 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000992 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800993 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800994 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
995 INIT_LIST_HEAD(&dev->mode_config.connector_list);
996 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
997 INIT_LIST_HEAD(&dev->mode_config.property_list);
998 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800999 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -08001000 idr_init(&dev->mode_config.crtc_idr);
1001
1002 mutex_lock(&dev->mode_config.mutex);
1003 drm_mode_create_standard_connector_properties(dev);
1004 mutex_unlock(&dev->mode_config.mutex);
1005
1006 /* Just to be sure */
1007 dev->mode_config.num_fb = 0;
1008 dev->mode_config.num_connector = 0;
1009 dev->mode_config.num_crtc = 0;
1010 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001011}
1012EXPORT_SYMBOL(drm_mode_config_init);
1013
1014int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
1015{
1016 uint32_t total_objects = 0;
1017
1018 total_objects += dev->mode_config.num_crtc;
1019 total_objects += dev->mode_config.num_connector;
1020 total_objects += dev->mode_config.num_encoder;
1021
Dave Airlief453ba02008-11-07 14:05:41 -08001022 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
1023 if (!group->id_list)
1024 return -ENOMEM;
1025
1026 group->num_crtcs = 0;
1027 group->num_connectors = 0;
1028 group->num_encoders = 0;
1029 return 0;
1030}
1031
1032int drm_mode_group_init_legacy_group(struct drm_device *dev,
1033 struct drm_mode_group *group)
1034{
1035 struct drm_crtc *crtc;
1036 struct drm_encoder *encoder;
1037 struct drm_connector *connector;
1038 int ret;
1039
1040 if ((ret = drm_mode_group_init(dev, group)))
1041 return ret;
1042
1043 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
1044 group->id_list[group->num_crtcs++] = crtc->base.id;
1045
1046 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
1047 group->id_list[group->num_crtcs + group->num_encoders++] =
1048 encoder->base.id;
1049
1050 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
1051 group->id_list[group->num_crtcs + group->num_encoders +
1052 group->num_connectors++] = connector->base.id;
1053
1054 return 0;
1055}
Dave Airlie9c1dfc52012-03-20 06:59:29 +00001056EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -08001057
1058/**
1059 * drm_mode_config_cleanup - free up DRM mode_config info
1060 * @dev: DRM device
1061 *
1062 * LOCKING:
1063 * Caller must hold mode config lock.
1064 *
1065 * Free up all the connectors and CRTCs associated with this DRM device, then
1066 * free up the framebuffers and associated buffer objects.
1067 *
1068 * FIXME: cleanup any dangling user buffer objects too
1069 */
1070void drm_mode_config_cleanup(struct drm_device *dev)
1071{
1072 struct drm_connector *connector, *ot;
1073 struct drm_crtc *crtc, *ct;
1074 struct drm_encoder *encoder, *enct;
1075 struct drm_framebuffer *fb, *fbt;
1076 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001077 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001078
1079 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1080 head) {
1081 encoder->funcs->destroy(encoder);
1082 }
1083
1084 list_for_each_entry_safe(connector, ot,
1085 &dev->mode_config.connector_list, head) {
1086 connector->funcs->destroy(connector);
1087 }
1088
1089 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1090 head) {
1091 drm_property_destroy(dev, property);
1092 }
1093
1094 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00001095 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08001096 }
1097
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001098 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1099 head) {
1100 plane->funcs->destroy(plane);
1101 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001102
Chris Wilson31840092012-09-17 09:38:03 +00001103 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1104 crtc->funcs->destroy(crtc);
1105 }
1106
Sascha Hauer59ce0622012-02-01 11:38:20 +01001107 idr_remove_all(&dev->mode_config.crtc_idr);
1108 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001109}
1110EXPORT_SYMBOL(drm_mode_config_cleanup);
1111
Dave Airlief453ba02008-11-07 14:05:41 -08001112/**
1113 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1114 * @out: drm_mode_modeinfo struct to return to the user
1115 * @in: drm_display_mode to use
1116 *
1117 * LOCKING:
1118 * None.
1119 *
1120 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1121 * the user.
1122 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001123static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1124 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001125{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001126 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1127 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1128 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1129 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1130 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1131 "timing values too large for mode info\n");
1132
Dave Airlief453ba02008-11-07 14:05:41 -08001133 out->clock = in->clock;
1134 out->hdisplay = in->hdisplay;
1135 out->hsync_start = in->hsync_start;
1136 out->hsync_end = in->hsync_end;
1137 out->htotal = in->htotal;
1138 out->hskew = in->hskew;
1139 out->vdisplay = in->vdisplay;
1140 out->vsync_start = in->vsync_start;
1141 out->vsync_end = in->vsync_end;
1142 out->vtotal = in->vtotal;
1143 out->vscan = in->vscan;
1144 out->vrefresh = in->vrefresh;
1145 out->flags = in->flags;
1146 out->type = in->type;
1147 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1148 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1149}
1150
1151/**
1152 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1153 * @out: drm_display_mode to return to the user
1154 * @in: drm_mode_modeinfo to use
1155 *
1156 * LOCKING:
1157 * None.
1158 *
1159 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1160 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001161 *
1162 * RETURNS:
1163 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001164 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001165static int drm_crtc_convert_umode(struct drm_display_mode *out,
1166 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001167{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001168 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1169 return -ERANGE;
1170
Dave Airlief453ba02008-11-07 14:05:41 -08001171 out->clock = in->clock;
1172 out->hdisplay = in->hdisplay;
1173 out->hsync_start = in->hsync_start;
1174 out->hsync_end = in->hsync_end;
1175 out->htotal = in->htotal;
1176 out->hskew = in->hskew;
1177 out->vdisplay = in->vdisplay;
1178 out->vsync_start = in->vsync_start;
1179 out->vsync_end = in->vsync_end;
1180 out->vtotal = in->vtotal;
1181 out->vscan = in->vscan;
1182 out->vrefresh = in->vrefresh;
1183 out->flags = in->flags;
1184 out->type = in->type;
1185 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1186 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001187
1188 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001189}
1190
1191/**
1192 * drm_mode_getresources - get graphics configuration
1193 * @inode: inode from the ioctl
1194 * @filp: file * from the ioctl
1195 * @cmd: cmd from ioctl
1196 * @arg: arg from ioctl
1197 *
1198 * LOCKING:
1199 * Takes mode config lock.
1200 *
1201 * Construct a set of configuration description structures and return
1202 * them to the user, including CRTC, connector and framebuffer configuration.
1203 *
1204 * Called by the user via ioctl.
1205 *
1206 * RETURNS:
1207 * Zero on success, errno on failure.
1208 */
1209int drm_mode_getresources(struct drm_device *dev, void *data,
1210 struct drm_file *file_priv)
1211{
1212 struct drm_mode_card_res *card_res = data;
1213 struct list_head *lh;
1214 struct drm_framebuffer *fb;
1215 struct drm_connector *connector;
1216 struct drm_crtc *crtc;
1217 struct drm_encoder *encoder;
1218 int ret = 0;
1219 int connector_count = 0;
1220 int crtc_count = 0;
1221 int fb_count = 0;
1222 int encoder_count = 0;
1223 int copied = 0, i;
1224 uint32_t __user *fb_id;
1225 uint32_t __user *crtc_id;
1226 uint32_t __user *connector_id;
1227 uint32_t __user *encoder_id;
1228 struct drm_mode_group *mode_group;
1229
Dave Airliefb3b06c2011-02-08 13:55:21 +10001230 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1231 return -EINVAL;
1232
Dave Airlief453ba02008-11-07 14:05:41 -08001233 mutex_lock(&dev->mode_config.mutex);
1234
1235 /*
1236 * For the non-control nodes we need to limit the list of resources
1237 * by IDs in the group list for this node
1238 */
1239 list_for_each(lh, &file_priv->fbs)
1240 fb_count++;
1241
1242 mode_group = &file_priv->master->minor->mode_group;
1243 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1244
1245 list_for_each(lh, &dev->mode_config.crtc_list)
1246 crtc_count++;
1247
1248 list_for_each(lh, &dev->mode_config.connector_list)
1249 connector_count++;
1250
1251 list_for_each(lh, &dev->mode_config.encoder_list)
1252 encoder_count++;
1253 } else {
1254
1255 crtc_count = mode_group->num_crtcs;
1256 connector_count = mode_group->num_connectors;
1257 encoder_count = mode_group->num_encoders;
1258 }
1259
1260 card_res->max_height = dev->mode_config.max_height;
1261 card_res->min_height = dev->mode_config.min_height;
1262 card_res->max_width = dev->mode_config.max_width;
1263 card_res->min_width = dev->mode_config.min_width;
1264
1265 /* handle this in 4 parts */
1266 /* FBs */
1267 if (card_res->count_fbs >= fb_count) {
1268 copied = 0;
1269 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001270 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001271 if (put_user(fb->base.id, fb_id + copied)) {
1272 ret = -EFAULT;
1273 goto out;
1274 }
1275 copied++;
1276 }
1277 }
1278 card_res->count_fbs = fb_count;
1279
1280 /* CRTCs */
1281 if (card_res->count_crtcs >= crtc_count) {
1282 copied = 0;
1283 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1284 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1285 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1286 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001287 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001288 if (put_user(crtc->base.id, crtc_id + copied)) {
1289 ret = -EFAULT;
1290 goto out;
1291 }
1292 copied++;
1293 }
1294 } else {
1295 for (i = 0; i < mode_group->num_crtcs; i++) {
1296 if (put_user(mode_group->id_list[i],
1297 crtc_id + copied)) {
1298 ret = -EFAULT;
1299 goto out;
1300 }
1301 copied++;
1302 }
1303 }
1304 }
1305 card_res->count_crtcs = crtc_count;
1306
1307 /* Encoders */
1308 if (card_res->count_encoders >= encoder_count) {
1309 copied = 0;
1310 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1311 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1312 list_for_each_entry(encoder,
1313 &dev->mode_config.encoder_list,
1314 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001315 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1316 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001317 if (put_user(encoder->base.id, encoder_id +
1318 copied)) {
1319 ret = -EFAULT;
1320 goto out;
1321 }
1322 copied++;
1323 }
1324 } else {
1325 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1326 if (put_user(mode_group->id_list[i],
1327 encoder_id + copied)) {
1328 ret = -EFAULT;
1329 goto out;
1330 }
1331 copied++;
1332 }
1333
1334 }
1335 }
1336 card_res->count_encoders = encoder_count;
1337
1338 /* Connectors */
1339 if (card_res->count_connectors >= connector_count) {
1340 copied = 0;
1341 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1342 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1343 list_for_each_entry(connector,
1344 &dev->mode_config.connector_list,
1345 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001346 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1347 connector->base.id,
1348 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001349 if (put_user(connector->base.id,
1350 connector_id + copied)) {
1351 ret = -EFAULT;
1352 goto out;
1353 }
1354 copied++;
1355 }
1356 } else {
1357 int start = mode_group->num_crtcs +
1358 mode_group->num_encoders;
1359 for (i = start; i < start + mode_group->num_connectors; i++) {
1360 if (put_user(mode_group->id_list[i],
1361 connector_id + copied)) {
1362 ret = -EFAULT;
1363 goto out;
1364 }
1365 copied++;
1366 }
1367 }
1368 }
1369 card_res->count_connectors = connector_count;
1370
Jerome Glisse94401062010-07-15 15:43:25 -04001371 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001372 card_res->count_connectors, card_res->count_encoders);
1373
1374out:
1375 mutex_unlock(&dev->mode_config.mutex);
1376 return ret;
1377}
1378
1379/**
1380 * drm_mode_getcrtc - get CRTC configuration
1381 * @inode: inode from the ioctl
1382 * @filp: file * from the ioctl
1383 * @cmd: cmd from ioctl
1384 * @arg: arg from ioctl
1385 *
1386 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001387 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001388 *
1389 * Construct a CRTC configuration structure to return to the user.
1390 *
1391 * Called by the user via ioctl.
1392 *
1393 * RETURNS:
1394 * Zero on success, errno on failure.
1395 */
1396int drm_mode_getcrtc(struct drm_device *dev,
1397 void *data, struct drm_file *file_priv)
1398{
1399 struct drm_mode_crtc *crtc_resp = data;
1400 struct drm_crtc *crtc;
1401 struct drm_mode_object *obj;
1402 int ret = 0;
1403
Dave Airliefb3b06c2011-02-08 13:55:21 +10001404 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1405 return -EINVAL;
1406
Dave Airlief453ba02008-11-07 14:05:41 -08001407 mutex_lock(&dev->mode_config.mutex);
1408
1409 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1410 DRM_MODE_OBJECT_CRTC);
1411 if (!obj) {
1412 ret = -EINVAL;
1413 goto out;
1414 }
1415 crtc = obj_to_crtc(obj);
1416
1417 crtc_resp->x = crtc->x;
1418 crtc_resp->y = crtc->y;
1419 crtc_resp->gamma_size = crtc->gamma_size;
1420 if (crtc->fb)
1421 crtc_resp->fb_id = crtc->fb->base.id;
1422 else
1423 crtc_resp->fb_id = 0;
1424
1425 if (crtc->enabled) {
1426
1427 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1428 crtc_resp->mode_valid = 1;
1429
1430 } else {
1431 crtc_resp->mode_valid = 0;
1432 }
1433
1434out:
1435 mutex_unlock(&dev->mode_config.mutex);
1436 return ret;
1437}
1438
1439/**
1440 * drm_mode_getconnector - get connector configuration
1441 * @inode: inode from the ioctl
1442 * @filp: file * from the ioctl
1443 * @cmd: cmd from ioctl
1444 * @arg: arg from ioctl
1445 *
1446 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001447 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001448 *
1449 * Construct a connector configuration structure to return to the user.
1450 *
1451 * Called by the user via ioctl.
1452 *
1453 * RETURNS:
1454 * Zero on success, errno on failure.
1455 */
1456int drm_mode_getconnector(struct drm_device *dev, void *data,
1457 struct drm_file *file_priv)
1458{
1459 struct drm_mode_get_connector *out_resp = data;
1460 struct drm_mode_object *obj;
1461 struct drm_connector *connector;
1462 struct drm_display_mode *mode;
1463 int mode_count = 0;
1464 int props_count = 0;
1465 int encoders_count = 0;
1466 int ret = 0;
1467 int copied = 0;
1468 int i;
1469 struct drm_mode_modeinfo u_mode;
1470 struct drm_mode_modeinfo __user *mode_ptr;
1471 uint32_t __user *prop_ptr;
1472 uint64_t __user *prop_values;
1473 uint32_t __user *encoder_ptr;
1474
Dave Airliefb3b06c2011-02-08 13:55:21 +10001475 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1476 return -EINVAL;
1477
Dave Airlief453ba02008-11-07 14:05:41 -08001478 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1479
Jerome Glisse94401062010-07-15 15:43:25 -04001480 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001481
1482 mutex_lock(&dev->mode_config.mutex);
1483
1484 obj = drm_mode_object_find(dev, out_resp->connector_id,
1485 DRM_MODE_OBJECT_CONNECTOR);
1486 if (!obj) {
1487 ret = -EINVAL;
1488 goto out;
1489 }
1490 connector = obj_to_connector(obj);
1491
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001492 props_count = connector->properties.count;
Dave Airlief453ba02008-11-07 14:05:41 -08001493
1494 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1495 if (connector->encoder_ids[i] != 0) {
1496 encoders_count++;
1497 }
1498 }
1499
1500 if (out_resp->count_modes == 0) {
1501 connector->funcs->fill_modes(connector,
1502 dev->mode_config.max_width,
1503 dev->mode_config.max_height);
1504 }
1505
1506 /* delayed so we get modes regardless of pre-fill_modes state */
1507 list_for_each_entry(mode, &connector->modes, head)
1508 mode_count++;
1509
1510 out_resp->connector_id = connector->base.id;
1511 out_resp->connector_type = connector->connector_type;
1512 out_resp->connector_type_id = connector->connector_type_id;
1513 out_resp->mm_width = connector->display_info.width_mm;
1514 out_resp->mm_height = connector->display_info.height_mm;
1515 out_resp->subpixel = connector->display_info.subpixel_order;
1516 out_resp->connection = connector->status;
1517 if (connector->encoder)
1518 out_resp->encoder_id = connector->encoder->base.id;
1519 else
1520 out_resp->encoder_id = 0;
1521
1522 /*
1523 * This ioctl is called twice, once to determine how much space is
1524 * needed, and the 2nd time to fill it.
1525 */
1526 if ((out_resp->count_modes >= mode_count) && mode_count) {
1527 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001528 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001529 list_for_each_entry(mode, &connector->modes, head) {
1530 drm_crtc_convert_to_umode(&u_mode, mode);
1531 if (copy_to_user(mode_ptr + copied,
1532 &u_mode, sizeof(u_mode))) {
1533 ret = -EFAULT;
1534 goto out;
1535 }
1536 copied++;
1537 }
1538 }
1539 out_resp->count_modes = mode_count;
1540
1541 if ((out_resp->count_props >= props_count) && props_count) {
1542 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001543 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1544 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001545 for (i = 0; i < connector->properties.count; i++) {
1546 if (put_user(connector->properties.ids[i],
1547 prop_ptr + copied)) {
1548 ret = -EFAULT;
1549 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001550 }
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03001551
1552 if (put_user(connector->properties.values[i],
1553 prop_values + copied)) {
1554 ret = -EFAULT;
1555 goto out;
1556 }
1557 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001558 }
1559 }
1560 out_resp->count_props = props_count;
1561
1562 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1563 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001564 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001565 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1566 if (connector->encoder_ids[i] != 0) {
1567 if (put_user(connector->encoder_ids[i],
1568 encoder_ptr + copied)) {
1569 ret = -EFAULT;
1570 goto out;
1571 }
1572 copied++;
1573 }
1574 }
1575 }
1576 out_resp->count_encoders = encoders_count;
1577
1578out:
1579 mutex_unlock(&dev->mode_config.mutex);
1580 return ret;
1581}
1582
1583int drm_mode_getencoder(struct drm_device *dev, void *data,
1584 struct drm_file *file_priv)
1585{
1586 struct drm_mode_get_encoder *enc_resp = data;
1587 struct drm_mode_object *obj;
1588 struct drm_encoder *encoder;
1589 int ret = 0;
1590
Dave Airliefb3b06c2011-02-08 13:55:21 +10001591 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1592 return -EINVAL;
1593
Dave Airlief453ba02008-11-07 14:05:41 -08001594 mutex_lock(&dev->mode_config.mutex);
1595 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1596 DRM_MODE_OBJECT_ENCODER);
1597 if (!obj) {
1598 ret = -EINVAL;
1599 goto out;
1600 }
1601 encoder = obj_to_encoder(obj);
1602
1603 if (encoder->crtc)
1604 enc_resp->crtc_id = encoder->crtc->base.id;
1605 else
1606 enc_resp->crtc_id = 0;
1607 enc_resp->encoder_type = encoder->encoder_type;
1608 enc_resp->encoder_id = encoder->base.id;
1609 enc_resp->possible_crtcs = encoder->possible_crtcs;
1610 enc_resp->possible_clones = encoder->possible_clones;
1611
1612out:
1613 mutex_unlock(&dev->mode_config.mutex);
1614 return ret;
1615}
1616
1617/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001618 * drm_mode_getplane_res - get plane info
1619 * @dev: DRM device
1620 * @data: ioctl data
1621 * @file_priv: DRM file info
1622 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001623 * LOCKING:
1624 * Takes mode config lock.
1625 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001626 * Return an plane count and set of IDs.
1627 */
1628int drm_mode_getplane_res(struct drm_device *dev, void *data,
1629 struct drm_file *file_priv)
1630{
1631 struct drm_mode_get_plane_res *plane_resp = data;
1632 struct drm_mode_config *config;
1633 struct drm_plane *plane;
1634 uint32_t __user *plane_ptr;
1635 int copied = 0, ret = 0;
1636
1637 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1638 return -EINVAL;
1639
1640 mutex_lock(&dev->mode_config.mutex);
1641 config = &dev->mode_config;
1642
1643 /*
1644 * This ioctl is called twice, once to determine how much space is
1645 * needed, and the 2nd time to fill it.
1646 */
1647 if (config->num_plane &&
1648 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001649 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001650
1651 list_for_each_entry(plane, &config->plane_list, head) {
1652 if (put_user(plane->base.id, plane_ptr + copied)) {
1653 ret = -EFAULT;
1654 goto out;
1655 }
1656 copied++;
1657 }
1658 }
1659 plane_resp->count_planes = config->num_plane;
1660
1661out:
1662 mutex_unlock(&dev->mode_config.mutex);
1663 return ret;
1664}
1665
1666/**
1667 * drm_mode_getplane - get plane info
1668 * @dev: DRM device
1669 * @data: ioctl data
1670 * @file_priv: DRM file info
1671 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001672 * LOCKING:
1673 * Takes mode config lock.
1674 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001675 * Return plane info, including formats supported, gamma size, any
1676 * current fb, etc.
1677 */
1678int drm_mode_getplane(struct drm_device *dev, void *data,
1679 struct drm_file *file_priv)
1680{
1681 struct drm_mode_get_plane *plane_resp = data;
1682 struct drm_mode_object *obj;
1683 struct drm_plane *plane;
1684 uint32_t __user *format_ptr;
1685 int ret = 0;
1686
1687 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1688 return -EINVAL;
1689
1690 mutex_lock(&dev->mode_config.mutex);
1691 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1692 DRM_MODE_OBJECT_PLANE);
1693 if (!obj) {
1694 ret = -ENOENT;
1695 goto out;
1696 }
1697 plane = obj_to_plane(obj);
1698
1699 if (plane->crtc)
1700 plane_resp->crtc_id = plane->crtc->base.id;
1701 else
1702 plane_resp->crtc_id = 0;
1703
1704 if (plane->fb)
1705 plane_resp->fb_id = plane->fb->base.id;
1706 else
1707 plane_resp->fb_id = 0;
1708
1709 plane_resp->plane_id = plane->base.id;
1710 plane_resp->possible_crtcs = plane->possible_crtcs;
1711 plane_resp->gamma_size = plane->gamma_size;
1712
1713 /*
1714 * This ioctl is called twice, once to determine how much space is
1715 * needed, and the 2nd time to fill it.
1716 */
1717 if (plane->format_count &&
1718 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001719 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001720 if (copy_to_user(format_ptr,
1721 plane->format_types,
1722 sizeof(uint32_t) * plane->format_count)) {
1723 ret = -EFAULT;
1724 goto out;
1725 }
1726 }
1727 plane_resp->count_format_types = plane->format_count;
1728
1729out:
1730 mutex_unlock(&dev->mode_config.mutex);
1731 return ret;
1732}
1733
1734/**
1735 * drm_mode_setplane - set up or tear down an plane
1736 * @dev: DRM device
1737 * @data: ioctl data*
1738 * @file_prive: DRM file info
1739 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001740 * LOCKING:
1741 * Takes mode config lock.
1742 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001743 * Set plane info, including placement, fb, scaling, and other factors.
1744 * Or pass a NULL fb to disable.
1745 */
1746int drm_mode_setplane(struct drm_device *dev, void *data,
1747 struct drm_file *file_priv)
1748{
1749 struct drm_mode_set_plane *plane_req = data;
1750 struct drm_mode_object *obj;
1751 struct drm_plane *plane;
1752 struct drm_crtc *crtc;
1753 struct drm_framebuffer *fb;
1754 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001755 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001756 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001757
1758 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1759 return -EINVAL;
1760
1761 mutex_lock(&dev->mode_config.mutex);
1762
1763 /*
1764 * First, find the plane, crtc, and fb objects. If not available,
1765 * we don't bother to call the driver.
1766 */
1767 obj = drm_mode_object_find(dev, plane_req->plane_id,
1768 DRM_MODE_OBJECT_PLANE);
1769 if (!obj) {
1770 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1771 plane_req->plane_id);
1772 ret = -ENOENT;
1773 goto out;
1774 }
1775 plane = obj_to_plane(obj);
1776
1777 /* No fb means shut it down */
1778 if (!plane_req->fb_id) {
1779 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001780 plane->crtc = NULL;
1781 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001782 goto out;
1783 }
1784
1785 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1786 DRM_MODE_OBJECT_CRTC);
1787 if (!obj) {
1788 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1789 plane_req->crtc_id);
1790 ret = -ENOENT;
1791 goto out;
1792 }
1793 crtc = obj_to_crtc(obj);
1794
1795 obj = drm_mode_object_find(dev, plane_req->fb_id,
1796 DRM_MODE_OBJECT_FB);
1797 if (!obj) {
1798 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1799 plane_req->fb_id);
1800 ret = -ENOENT;
1801 goto out;
1802 }
1803 fb = obj_to_fb(obj);
1804
Ville Syrjälä62443be2011-12-20 00:06:47 +02001805 /* Check whether this plane supports the fb pixel format. */
1806 for (i = 0; i < plane->format_count; i++)
1807 if (fb->pixel_format == plane->format_types[i])
1808 break;
1809 if (i == plane->format_count) {
1810 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1811 ret = -EINVAL;
1812 goto out;
1813 }
1814
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001815 fb_width = fb->width << 16;
1816 fb_height = fb->height << 16;
1817
1818 /* Make sure source coordinates are inside the fb. */
1819 if (plane_req->src_w > fb_width ||
1820 plane_req->src_x > fb_width - plane_req->src_w ||
1821 plane_req->src_h > fb_height ||
1822 plane_req->src_y > fb_height - plane_req->src_h) {
1823 DRM_DEBUG_KMS("Invalid source coordinates "
1824 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1825 plane_req->src_w >> 16,
1826 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1827 plane_req->src_h >> 16,
1828 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1829 plane_req->src_x >> 16,
1830 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1831 plane_req->src_y >> 16,
1832 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1833 ret = -ENOSPC;
1834 goto out;
1835 }
1836
Ville Syrjälä687a0402011-12-20 00:06:45 +02001837 /* Give drivers some help against integer overflows */
1838 if (plane_req->crtc_w > INT_MAX ||
1839 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1840 plane_req->crtc_h > INT_MAX ||
1841 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1842 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1843 plane_req->crtc_w, plane_req->crtc_h,
1844 plane_req->crtc_x, plane_req->crtc_y);
1845 ret = -ERANGE;
1846 goto out;
1847 }
1848
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001849 ret = plane->funcs->update_plane(plane, crtc, fb,
1850 plane_req->crtc_x, plane_req->crtc_y,
1851 plane_req->crtc_w, plane_req->crtc_h,
1852 plane_req->src_x, plane_req->src_y,
1853 plane_req->src_w, plane_req->src_h);
1854 if (!ret) {
1855 plane->crtc = crtc;
1856 plane->fb = fb;
1857 }
1858
1859out:
1860 mutex_unlock(&dev->mode_config.mutex);
1861
1862 return ret;
1863}
1864
1865/**
Dave Airlief453ba02008-11-07 14:05:41 -08001866 * drm_mode_setcrtc - set CRTC configuration
1867 * @inode: inode from the ioctl
1868 * @filp: file * from the ioctl
1869 * @cmd: cmd from ioctl
1870 * @arg: arg from ioctl
1871 *
1872 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001873 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001874 *
1875 * Build a new CRTC configuration based on user request.
1876 *
1877 * Called by the user via ioctl.
1878 *
1879 * RETURNS:
1880 * Zero on success, errno on failure.
1881 */
1882int drm_mode_setcrtc(struct drm_device *dev, void *data,
1883 struct drm_file *file_priv)
1884{
1885 struct drm_mode_config *config = &dev->mode_config;
1886 struct drm_mode_crtc *crtc_req = data;
1887 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001888 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001889 struct drm_connector **connector_set = NULL, *connector;
1890 struct drm_framebuffer *fb = NULL;
1891 struct drm_display_mode *mode = NULL;
1892 struct drm_mode_set set;
1893 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001894 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001895 int i;
1896
Dave Airliefb3b06c2011-02-08 13:55:21 +10001897 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1898 return -EINVAL;
1899
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001900 /* For some reason crtc x/y offsets are signed internally. */
1901 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1902 return -ERANGE;
1903
Dave Airlief453ba02008-11-07 14:05:41 -08001904 mutex_lock(&dev->mode_config.mutex);
1905 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1906 DRM_MODE_OBJECT_CRTC);
1907 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001908 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001909 ret = -EINVAL;
1910 goto out;
1911 }
1912 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001913 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001914
1915 if (crtc_req->mode_valid) {
Rob Clark7c80e122012-09-04 16:35:56 +00001916 int hdisplay, vdisplay;
Dave Airlief453ba02008-11-07 14:05:41 -08001917 /* If we have a mode we need a framebuffer. */
1918 /* If we pass -1, set the mode with the currently bound fb */
1919 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001920 if (!crtc->fb) {
1921 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1922 ret = -EINVAL;
1923 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001924 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001925 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001926 } else {
1927 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1928 DRM_MODE_OBJECT_FB);
1929 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001930 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1931 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001932 ret = -EINVAL;
1933 goto out;
1934 }
1935 fb = obj_to_fb(obj);
1936 }
1937
1938 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001939 if (!mode) {
1940 ret = -ENOMEM;
1941 goto out;
1942 }
1943
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001944 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1945 if (ret) {
1946 DRM_DEBUG_KMS("Invalid mode\n");
1947 goto out;
1948 }
1949
Dave Airlief453ba02008-11-07 14:05:41 -08001950 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001951
Rob Clark7c80e122012-09-04 16:35:56 +00001952 hdisplay = mode->hdisplay;
1953 vdisplay = mode->vdisplay;
1954
1955 if (crtc->invert_dimensions)
1956 swap(hdisplay, vdisplay);
1957
1958 if (hdisplay > fb->width ||
1959 vdisplay > fb->height ||
1960 crtc_req->x > fb->width - hdisplay ||
1961 crtc_req->y > fb->height - vdisplay) {
1962 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
1963 fb->width, fb->height,
1964 hdisplay, vdisplay, crtc_req->x, crtc_req->y,
1965 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001966 ret = -ENOSPC;
1967 goto out;
1968 }
Dave Airlief453ba02008-11-07 14:05:41 -08001969 }
1970
1971 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001972 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001973 ret = -EINVAL;
1974 goto out;
1975 }
1976
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001977 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001978 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001979 crtc_req->count_connectors);
1980 ret = -EINVAL;
1981 goto out;
1982 }
1983
1984 if (crtc_req->count_connectors > 0) {
1985 u32 out_id;
1986
1987 /* Avoid unbounded kernel memory allocation */
1988 if (crtc_req->count_connectors > config->num_connector) {
1989 ret = -EINVAL;
1990 goto out;
1991 }
1992
1993 connector_set = kmalloc(crtc_req->count_connectors *
1994 sizeof(struct drm_connector *),
1995 GFP_KERNEL);
1996 if (!connector_set) {
1997 ret = -ENOMEM;
1998 goto out;
1999 }
2000
2001 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002002 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002003 if (get_user(out_id, &set_connectors_ptr[i])) {
2004 ret = -EFAULT;
2005 goto out;
2006 }
2007
2008 obj = drm_mode_object_find(dev, out_id,
2009 DRM_MODE_OBJECT_CONNECTOR);
2010 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002011 DRM_DEBUG_KMS("Connector id %d unknown\n",
2012 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002013 ret = -EINVAL;
2014 goto out;
2015 }
2016 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04002017 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2018 connector->base.id,
2019 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08002020
2021 connector_set[i] = connector;
2022 }
2023 }
2024
2025 set.crtc = crtc;
2026 set.x = crtc_req->x;
2027 set.y = crtc_req->y;
2028 set.mode = mode;
2029 set.connectors = connector_set;
2030 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002031 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002032 ret = crtc->funcs->set_config(&set);
2033
2034out:
2035 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002036 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002037 mutex_unlock(&dev->mode_config.mutex);
2038 return ret;
2039}
2040
2041int drm_mode_cursor_ioctl(struct drm_device *dev,
2042 void *data, struct drm_file *file_priv)
2043{
2044 struct drm_mode_cursor *req = data;
2045 struct drm_mode_object *obj;
2046 struct drm_crtc *crtc;
2047 int ret = 0;
2048
Dave Airliefb3b06c2011-02-08 13:55:21 +10002049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2050 return -EINVAL;
2051
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002052 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002053 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002054
2055 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002056 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08002057 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002058 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08002059 ret = -EINVAL;
2060 goto out;
2061 }
2062 crtc = obj_to_crtc(obj);
2063
2064 if (req->flags & DRM_MODE_CURSOR_BO) {
2065 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002066 ret = -ENXIO;
2067 goto out;
2068 }
2069 /* Turns off the cursor if handle is 0 */
2070 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2071 req->width, req->height);
2072 }
2073
2074 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2075 if (crtc->funcs->cursor_move) {
2076 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2077 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002078 ret = -EFAULT;
2079 goto out;
2080 }
2081 }
2082out:
2083 mutex_unlock(&dev->mode_config.mutex);
2084 return ret;
2085}
2086
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002087/* Original addfb only supported RGB formats, so figure out which one */
2088uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2089{
2090 uint32_t fmt;
2091
2092 switch (bpp) {
2093 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002094 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002095 break;
2096 case 16:
2097 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002098 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002099 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002100 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002101 break;
2102 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002103 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002104 break;
2105 case 32:
2106 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002107 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002108 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002109 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002110 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002111 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002112 break;
2113 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002114 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2115 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002116 break;
2117 }
2118
2119 return fmt;
2120}
2121EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2122
Dave Airlief453ba02008-11-07 14:05:41 -08002123/**
2124 * drm_mode_addfb - add an FB to the graphics configuration
2125 * @inode: inode from the ioctl
2126 * @filp: file * from the ioctl
2127 * @cmd: cmd from ioctl
2128 * @arg: arg from ioctl
2129 *
2130 * LOCKING:
2131 * Takes mode config lock.
2132 *
2133 * Add a new FB to the specified CRTC, given a user request.
2134 *
2135 * Called by the user via ioctl.
2136 *
2137 * RETURNS:
2138 * Zero on success, errno on failure.
2139 */
2140int drm_mode_addfb(struct drm_device *dev,
2141 void *data, struct drm_file *file_priv)
2142{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002143 struct drm_mode_fb_cmd *or = data;
2144 struct drm_mode_fb_cmd2 r = {};
2145 struct drm_mode_config *config = &dev->mode_config;
2146 struct drm_framebuffer *fb;
2147 int ret = 0;
2148
2149 /* Use new struct with format internally */
2150 r.fb_id = or->fb_id;
2151 r.width = or->width;
2152 r.height = or->height;
2153 r.pitches[0] = or->pitch;
2154 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2155 r.handles[0] = or->handle;
2156
2157 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2158 return -EINVAL;
2159
Jesse Barnesacb4b992011-11-07 12:03:23 -08002160 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002161 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002162
2163 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002164 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002165
2166 mutex_lock(&dev->mode_config.mutex);
2167
2168 /* TODO check buffer is sufficiently large */
2169 /* TODO setup destructor callback */
2170
2171 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2172 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002173 DRM_DEBUG_KMS("could not create framebuffer\n");
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002174 ret = PTR_ERR(fb);
2175 goto out;
2176 }
2177
2178 or->fb_id = fb->base.id;
2179 list_add(&fb->filp_head, &file_priv->fbs);
2180 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2181
2182out:
2183 mutex_unlock(&dev->mode_config.mutex);
2184 return ret;
2185}
2186
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002187static int format_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002188{
2189 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2190
2191 switch (format) {
2192 case DRM_FORMAT_C8:
2193 case DRM_FORMAT_RGB332:
2194 case DRM_FORMAT_BGR233:
2195 case DRM_FORMAT_XRGB4444:
2196 case DRM_FORMAT_XBGR4444:
2197 case DRM_FORMAT_RGBX4444:
2198 case DRM_FORMAT_BGRX4444:
2199 case DRM_FORMAT_ARGB4444:
2200 case DRM_FORMAT_ABGR4444:
2201 case DRM_FORMAT_RGBA4444:
2202 case DRM_FORMAT_BGRA4444:
2203 case DRM_FORMAT_XRGB1555:
2204 case DRM_FORMAT_XBGR1555:
2205 case DRM_FORMAT_RGBX5551:
2206 case DRM_FORMAT_BGRX5551:
2207 case DRM_FORMAT_ARGB1555:
2208 case DRM_FORMAT_ABGR1555:
2209 case DRM_FORMAT_RGBA5551:
2210 case DRM_FORMAT_BGRA5551:
2211 case DRM_FORMAT_RGB565:
2212 case DRM_FORMAT_BGR565:
2213 case DRM_FORMAT_RGB888:
2214 case DRM_FORMAT_BGR888:
2215 case DRM_FORMAT_XRGB8888:
2216 case DRM_FORMAT_XBGR8888:
2217 case DRM_FORMAT_RGBX8888:
2218 case DRM_FORMAT_BGRX8888:
2219 case DRM_FORMAT_ARGB8888:
2220 case DRM_FORMAT_ABGR8888:
2221 case DRM_FORMAT_RGBA8888:
2222 case DRM_FORMAT_BGRA8888:
2223 case DRM_FORMAT_XRGB2101010:
2224 case DRM_FORMAT_XBGR2101010:
2225 case DRM_FORMAT_RGBX1010102:
2226 case DRM_FORMAT_BGRX1010102:
2227 case DRM_FORMAT_ARGB2101010:
2228 case DRM_FORMAT_ABGR2101010:
2229 case DRM_FORMAT_RGBA1010102:
2230 case DRM_FORMAT_BGRA1010102:
2231 case DRM_FORMAT_YUYV:
2232 case DRM_FORMAT_YVYU:
2233 case DRM_FORMAT_UYVY:
2234 case DRM_FORMAT_VYUY:
2235 case DRM_FORMAT_AYUV:
2236 case DRM_FORMAT_NV12:
2237 case DRM_FORMAT_NV21:
2238 case DRM_FORMAT_NV16:
2239 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02002240 case DRM_FORMAT_NV24:
2241 case DRM_FORMAT_NV42:
Ville Syrjälä935b5972011-12-20 00:06:48 +02002242 case DRM_FORMAT_YUV410:
2243 case DRM_FORMAT_YVU410:
2244 case DRM_FORMAT_YUV411:
2245 case DRM_FORMAT_YVU411:
2246 case DRM_FORMAT_YUV420:
2247 case DRM_FORMAT_YVU420:
2248 case DRM_FORMAT_YUV422:
2249 case DRM_FORMAT_YVU422:
2250 case DRM_FORMAT_YUV444:
2251 case DRM_FORMAT_YVU444:
2252 return 0;
2253 default:
2254 return -EINVAL;
2255 }
2256}
2257
Ville Syrjäläcff91b62012-05-24 20:54:00 +03002258static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002259{
2260 int ret, hsub, vsub, num_planes, i;
2261
2262 ret = format_check(r);
2263 if (ret) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002264 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002265 return ret;
2266 }
2267
2268 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2269 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2270 num_planes = drm_format_num_planes(r->pixel_format);
2271
2272 if (r->width == 0 || r->width % hsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002273 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002274 return -EINVAL;
2275 }
2276
2277 if (r->height == 0 || r->height % vsub) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002278 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002279 return -EINVAL;
2280 }
2281
2282 for (i = 0; i < num_planes; i++) {
2283 unsigned int width = r->width / (i != 0 ? hsub : 1);
2284
2285 if (!r->handles[i]) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002286 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002287 return -EINVAL;
2288 }
2289
2290 if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002291 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002292 return -EINVAL;
2293 }
2294 }
2295
2296 return 0;
2297}
2298
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002299/**
2300 * drm_mode_addfb2 - add an FB to the graphics configuration
2301 * @inode: inode from the ioctl
2302 * @filp: file * from the ioctl
2303 * @cmd: cmd from ioctl
2304 * @arg: arg from ioctl
2305 *
2306 * LOCKING:
2307 * Takes mode config lock.
2308 *
2309 * Add a new FB to the specified CRTC, given a user request with format.
2310 *
2311 * Called by the user via ioctl.
2312 *
2313 * RETURNS:
2314 * Zero on success, errno on failure.
2315 */
2316int drm_mode_addfb2(struct drm_device *dev,
2317 void *data, struct drm_file *file_priv)
2318{
2319 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002320 struct drm_mode_config *config = &dev->mode_config;
2321 struct drm_framebuffer *fb;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002322 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002323
Dave Airliefb3b06c2011-02-08 13:55:21 +10002324 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2325 return -EINVAL;
2326
Dave Airlief453ba02008-11-07 14:05:41 -08002327 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002328 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002329 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002330 return -EINVAL;
2331 }
2332 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002333 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002334 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002335 return -EINVAL;
2336 }
2337
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002338 ret = framebuffer_check(r);
2339 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002340 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002341
Dave Airlief453ba02008-11-07 14:05:41 -08002342 mutex_lock(&dev->mode_config.mutex);
2343
Dave Airlief453ba02008-11-07 14:05:41 -08002344 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002345 if (IS_ERR(fb)) {
Dave Airlie1aa1b112012-05-01 17:38:35 +01002346 DRM_DEBUG_KMS("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002347 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002348 goto out;
2349 }
2350
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002351 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002352 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002353 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002354
2355out:
2356 mutex_unlock(&dev->mode_config.mutex);
2357 return ret;
2358}
2359
2360/**
2361 * drm_mode_rmfb - remove an FB from the configuration
2362 * @inode: inode from the ioctl
2363 * @filp: file * from the ioctl
2364 * @cmd: cmd from ioctl
2365 * @arg: arg from ioctl
2366 *
2367 * LOCKING:
2368 * Takes mode config lock.
2369 *
2370 * Remove the FB specified by the user.
2371 *
2372 * Called by the user via ioctl.
2373 *
2374 * RETURNS:
2375 * Zero on success, errno on failure.
2376 */
2377int drm_mode_rmfb(struct drm_device *dev,
2378 void *data, struct drm_file *file_priv)
2379{
2380 struct drm_mode_object *obj;
2381 struct drm_framebuffer *fb = NULL;
2382 struct drm_framebuffer *fbl = NULL;
2383 uint32_t *id = data;
2384 int ret = 0;
2385 int found = 0;
2386
Dave Airliefb3b06c2011-02-08 13:55:21 +10002387 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2388 return -EINVAL;
2389
Dave Airlief453ba02008-11-07 14:05:41 -08002390 mutex_lock(&dev->mode_config.mutex);
2391 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002392 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002393 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002394 ret = -EINVAL;
2395 goto out;
2396 }
2397 fb = obj_to_fb(obj);
2398
2399 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2400 if (fb == fbl)
2401 found = 1;
2402
2403 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002404 ret = -EINVAL;
2405 goto out;
2406 }
2407
Rob Clarkf7eff602012-09-05 21:48:38 +00002408 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002409
2410out:
2411 mutex_unlock(&dev->mode_config.mutex);
2412 return ret;
2413}
2414
2415/**
2416 * drm_mode_getfb - get FB info
2417 * @inode: inode from the ioctl
2418 * @filp: file * from the ioctl
2419 * @cmd: cmd from ioctl
2420 * @arg: arg from ioctl
2421 *
2422 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002423 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002424 *
2425 * Lookup the FB given its ID and return info about it.
2426 *
2427 * Called by the user via ioctl.
2428 *
2429 * RETURNS:
2430 * Zero on success, errno on failure.
2431 */
2432int drm_mode_getfb(struct drm_device *dev,
2433 void *data, struct drm_file *file_priv)
2434{
2435 struct drm_mode_fb_cmd *r = data;
2436 struct drm_mode_object *obj;
2437 struct drm_framebuffer *fb;
2438 int ret = 0;
2439
Dave Airliefb3b06c2011-02-08 13:55:21 +10002440 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2441 return -EINVAL;
2442
Dave Airlief453ba02008-11-07 14:05:41 -08002443 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002444 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002445 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002446 ret = -EINVAL;
2447 goto out;
2448 }
2449 fb = obj_to_fb(obj);
2450
2451 r->height = fb->height;
2452 r->width = fb->width;
2453 r->depth = fb->depth;
2454 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002455 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002456 fb->funcs->create_handle(fb, file_priv, &r->handle);
2457
2458out:
2459 mutex_unlock(&dev->mode_config.mutex);
2460 return ret;
2461}
2462
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002463int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2464 void *data, struct drm_file *file_priv)
2465{
2466 struct drm_clip_rect __user *clips_ptr;
2467 struct drm_clip_rect *clips = NULL;
2468 struct drm_mode_fb_dirty_cmd *r = data;
2469 struct drm_mode_object *obj;
2470 struct drm_framebuffer *fb;
2471 unsigned flags;
2472 int num_clips;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002473 int ret;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002474
Dave Airliefb3b06c2011-02-08 13:55:21 +10002475 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2476 return -EINVAL;
2477
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002478 mutex_lock(&dev->mode_config.mutex);
2479 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2480 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002481 ret = -EINVAL;
2482 goto out_err1;
2483 }
2484 fb = obj_to_fb(obj);
2485
2486 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002487 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002488
2489 if (!num_clips != !clips_ptr) {
2490 ret = -EINVAL;
2491 goto out_err1;
2492 }
2493
2494 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2495
2496 /* If userspace annotates copy, clips must come in pairs */
2497 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2498 ret = -EINVAL;
2499 goto out_err1;
2500 }
2501
2502 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002503 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2504 ret = -EINVAL;
2505 goto out_err1;
2506 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002507 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2508 if (!clips) {
2509 ret = -ENOMEM;
2510 goto out_err1;
2511 }
2512
2513 ret = copy_from_user(clips, clips_ptr,
2514 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002515 if (ret) {
2516 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002517 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002518 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002519 }
2520
2521 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002522 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2523 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002524 } else {
2525 ret = -ENOSYS;
2526 goto out_err2;
2527 }
2528
2529out_err2:
2530 kfree(clips);
2531out_err1:
2532 mutex_unlock(&dev->mode_config.mutex);
2533 return ret;
2534}
2535
2536
Dave Airlief453ba02008-11-07 14:05:41 -08002537/**
2538 * drm_fb_release - remove and free the FBs on this file
2539 * @filp: file * from the ioctl
2540 *
2541 * LOCKING:
2542 * Takes mode config lock.
2543 *
2544 * Destroy all the FBs associated with @filp.
2545 *
2546 * Called by the user via ioctl.
2547 *
2548 * RETURNS:
2549 * Zero on success, errno on failure.
2550 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002551void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002552{
Dave Airlief453ba02008-11-07 14:05:41 -08002553 struct drm_device *dev = priv->minor->dev;
2554 struct drm_framebuffer *fb, *tfb;
2555
2556 mutex_lock(&dev->mode_config.mutex);
2557 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
Rob Clarkf7eff602012-09-05 21:48:38 +00002558 drm_framebuffer_remove(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002559 }
2560 mutex_unlock(&dev->mode_config.mutex);
2561}
2562
2563/**
2564 * drm_mode_attachmode - add a mode to the user mode list
2565 * @dev: DRM device
2566 * @connector: connector to add the mode to
2567 * @mode: mode to add
2568 *
2569 * Add @mode to @connector's user mode list.
2570 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002571static void drm_mode_attachmode(struct drm_device *dev,
2572 struct drm_connector *connector,
2573 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002574{
Dave Airlief453ba02008-11-07 14:05:41 -08002575 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002576}
2577
2578int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002579 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002580{
2581 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002582 int ret = 0;
2583 struct drm_display_mode *dup_mode, *next;
2584 LIST_HEAD(list);
2585
Dave Airlief453ba02008-11-07 14:05:41 -08002586 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2587 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002588 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002589 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002590 dup_mode = drm_mode_duplicate(dev, mode);
2591 if (!dup_mode) {
2592 ret = -ENOMEM;
2593 goto out;
2594 }
2595 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002596 }
2597 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002598
2599 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2600 if (!connector->encoder)
2601 continue;
2602 if (connector->encoder->crtc == crtc)
2603 list_move_tail(list.next, &connector->user_modes);
2604 }
2605
2606 WARN_ON(!list_empty(&list));
2607
2608 out:
2609 list_for_each_entry_safe(dup_mode, next, &list, head)
2610 drm_mode_destroy(dev, dup_mode);
2611
2612 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002613}
2614EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2615
2616static int drm_mode_detachmode(struct drm_device *dev,
2617 struct drm_connector *connector,
2618 struct drm_display_mode *mode)
2619{
2620 int found = 0;
2621 int ret = 0;
2622 struct drm_display_mode *match_mode, *t;
2623
2624 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2625 if (drm_mode_equal(match_mode, mode)) {
2626 list_del(&match_mode->head);
2627 drm_mode_destroy(dev, match_mode);
2628 found = 1;
2629 break;
2630 }
2631 }
2632
2633 if (!found)
2634 ret = -EINVAL;
2635
2636 return ret;
2637}
2638
2639int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2640{
2641 struct drm_connector *connector;
2642
2643 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2644 drm_mode_detachmode(dev, connector, mode);
2645 }
2646 return 0;
2647}
2648EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2649
2650/**
2651 * drm_fb_attachmode - Attach a user mode to an connector
2652 * @inode: inode from the ioctl
2653 * @filp: file * from the ioctl
2654 * @cmd: cmd from ioctl
2655 * @arg: arg from ioctl
2656 *
2657 * This attaches a user specified mode to an connector.
2658 * Called by the user via ioctl.
2659 *
2660 * RETURNS:
2661 * Zero on success, errno on failure.
2662 */
2663int drm_mode_attachmode_ioctl(struct drm_device *dev,
2664 void *data, struct drm_file *file_priv)
2665{
2666 struct drm_mode_mode_cmd *mode_cmd = data;
2667 struct drm_connector *connector;
2668 struct drm_display_mode *mode;
2669 struct drm_mode_object *obj;
2670 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002671 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002672
Dave Airliefb3b06c2011-02-08 13:55:21 +10002673 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2674 return -EINVAL;
2675
Dave Airlief453ba02008-11-07 14:05:41 -08002676 mutex_lock(&dev->mode_config.mutex);
2677
2678 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2679 if (!obj) {
2680 ret = -EINVAL;
2681 goto out;
2682 }
2683 connector = obj_to_connector(obj);
2684
2685 mode = drm_mode_create(dev);
2686 if (!mode) {
2687 ret = -ENOMEM;
2688 goto out;
2689 }
2690
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002691 ret = drm_crtc_convert_umode(mode, umode);
2692 if (ret) {
2693 DRM_DEBUG_KMS("Invalid mode\n");
2694 drm_mode_destroy(dev, mode);
2695 goto out;
2696 }
Dave Airlief453ba02008-11-07 14:05:41 -08002697
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002698 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002699out:
2700 mutex_unlock(&dev->mode_config.mutex);
2701 return ret;
2702}
2703
2704
2705/**
2706 * drm_fb_detachmode - Detach a user specified mode from an connector
2707 * @inode: inode from the ioctl
2708 * @filp: file * from the ioctl
2709 * @cmd: cmd from ioctl
2710 * @arg: arg from ioctl
2711 *
2712 * Called by the user via ioctl.
2713 *
2714 * RETURNS:
2715 * Zero on success, errno on failure.
2716 */
2717int drm_mode_detachmode_ioctl(struct drm_device *dev,
2718 void *data, struct drm_file *file_priv)
2719{
2720 struct drm_mode_object *obj;
2721 struct drm_mode_mode_cmd *mode_cmd = data;
2722 struct drm_connector *connector;
2723 struct drm_display_mode mode;
2724 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002725 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002726
Dave Airliefb3b06c2011-02-08 13:55:21 +10002727 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2728 return -EINVAL;
2729
Dave Airlief453ba02008-11-07 14:05:41 -08002730 mutex_lock(&dev->mode_config.mutex);
2731
2732 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2733 if (!obj) {
2734 ret = -EINVAL;
2735 goto out;
2736 }
2737 connector = obj_to_connector(obj);
2738
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002739 ret = drm_crtc_convert_umode(&mode, umode);
2740 if (ret) {
2741 DRM_DEBUG_KMS("Invalid mode\n");
2742 goto out;
2743 }
2744
Dave Airlief453ba02008-11-07 14:05:41 -08002745 ret = drm_mode_detachmode(dev, connector, &mode);
2746out:
2747 mutex_unlock(&dev->mode_config.mutex);
2748 return ret;
2749}
2750
2751struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2752 const char *name, int num_values)
2753{
2754 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002755 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002756
2757 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2758 if (!property)
2759 return NULL;
2760
2761 if (num_values) {
2762 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2763 if (!property->values)
2764 goto fail;
2765 }
2766
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002767 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2768 if (ret)
2769 goto fail;
2770
Dave Airlief453ba02008-11-07 14:05:41 -08002771 property->flags = flags;
2772 property->num_values = num_values;
2773 INIT_LIST_HEAD(&property->enum_blob_list);
2774
Vinson Lee471dd2e2011-11-10 11:55:40 -08002775 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002776 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002777 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2778 }
Dave Airlief453ba02008-11-07 14:05:41 -08002779
2780 list_add_tail(&property->head, &dev->mode_config.property_list);
2781 return property;
2782fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002783 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002784 kfree(property);
2785 return NULL;
2786}
2787EXPORT_SYMBOL(drm_property_create);
2788
Sascha Hauer4a67d392012-02-06 10:58:17 +01002789struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2790 const char *name,
2791 const struct drm_prop_enum_list *props,
2792 int num_values)
2793{
2794 struct drm_property *property;
2795 int i, ret;
2796
2797 flags |= DRM_MODE_PROP_ENUM;
2798
2799 property = drm_property_create(dev, flags, name, num_values);
2800 if (!property)
2801 return NULL;
2802
2803 for (i = 0; i < num_values; i++) {
2804 ret = drm_property_add_enum(property, i,
2805 props[i].type,
2806 props[i].name);
2807 if (ret) {
2808 drm_property_destroy(dev, property);
2809 return NULL;
2810 }
2811 }
2812
2813 return property;
2814}
2815EXPORT_SYMBOL(drm_property_create_enum);
2816
Rob Clark49e27542012-05-17 02:23:26 -06002817struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2818 int flags, const char *name,
2819 const struct drm_prop_enum_list *props,
2820 int num_values)
2821{
2822 struct drm_property *property;
2823 int i, ret;
2824
2825 flags |= DRM_MODE_PROP_BITMASK;
2826
2827 property = drm_property_create(dev, flags, name, num_values);
2828 if (!property)
2829 return NULL;
2830
2831 for (i = 0; i < num_values; i++) {
2832 ret = drm_property_add_enum(property, i,
2833 props[i].type,
2834 props[i].name);
2835 if (ret) {
2836 drm_property_destroy(dev, property);
2837 return NULL;
2838 }
2839 }
2840
2841 return property;
2842}
2843EXPORT_SYMBOL(drm_property_create_bitmask);
2844
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002845struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2846 const char *name,
2847 uint64_t min, uint64_t max)
2848{
2849 struct drm_property *property;
2850
2851 flags |= DRM_MODE_PROP_RANGE;
2852
2853 property = drm_property_create(dev, flags, name, 2);
2854 if (!property)
2855 return NULL;
2856
2857 property->values[0] = min;
2858 property->values[1] = max;
2859
2860 return property;
2861}
2862EXPORT_SYMBOL(drm_property_create_range);
2863
Dave Airlief453ba02008-11-07 14:05:41 -08002864int drm_property_add_enum(struct drm_property *property, int index,
2865 uint64_t value, const char *name)
2866{
2867 struct drm_property_enum *prop_enum;
2868
Rob Clark49e27542012-05-17 02:23:26 -06002869 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2870 return -EINVAL;
2871
2872 /*
2873 * Bitmask enum properties have the additional constraint of values
2874 * from 0 to 63
2875 */
2876 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08002877 return -EINVAL;
2878
2879 if (!list_empty(&property->enum_blob_list)) {
2880 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2881 if (prop_enum->value == value) {
2882 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2883 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2884 return 0;
2885 }
2886 }
2887 }
2888
2889 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2890 if (!prop_enum)
2891 return -ENOMEM;
2892
2893 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2894 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2895 prop_enum->value = value;
2896
2897 property->values[index] = value;
2898 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2899 return 0;
2900}
2901EXPORT_SYMBOL(drm_property_add_enum);
2902
2903void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2904{
2905 struct drm_property_enum *prop_enum, *pt;
2906
2907 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2908 list_del(&prop_enum->head);
2909 kfree(prop_enum);
2910 }
2911
2912 if (property->num_values)
2913 kfree(property->values);
2914 drm_mode_object_put(dev, &property->base);
2915 list_del(&property->head);
2916 kfree(property);
2917}
2918EXPORT_SYMBOL(drm_property_destroy);
2919
Paulo Zanoniafea2ad2012-05-15 18:09:00 -03002920void drm_connector_attach_property(struct drm_connector *connector,
Dave Airlief453ba02008-11-07 14:05:41 -08002921 struct drm_property *property, uint64_t init_val)
2922{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002923 drm_object_attach_property(&connector->base, property, init_val);
Dave Airlief453ba02008-11-07 14:05:41 -08002924}
2925EXPORT_SYMBOL(drm_connector_attach_property);
2926
2927int drm_connector_property_set_value(struct drm_connector *connector,
2928 struct drm_property *property, uint64_t value)
2929{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002930 return drm_object_property_set_value(&connector->base, property, value);
Dave Airlief453ba02008-11-07 14:05:41 -08002931}
2932EXPORT_SYMBOL(drm_connector_property_set_value);
2933
2934int drm_connector_property_get_value(struct drm_connector *connector,
2935 struct drm_property *property, uint64_t *val)
2936{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03002937 return drm_object_property_get_value(&connector->base, property, val);
Dave Airlief453ba02008-11-07 14:05:41 -08002938}
2939EXPORT_SYMBOL(drm_connector_property_get_value);
2940
Paulo Zanonic5431882012-05-15 18:09:02 -03002941void drm_object_attach_property(struct drm_mode_object *obj,
2942 struct drm_property *property,
2943 uint64_t init_val)
2944{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002945 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03002946
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002947 if (count == DRM_OBJECT_MAX_PROPERTY) {
2948 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2949 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2950 "you see this message on the same object type.\n",
2951 obj->type);
2952 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03002953 }
2954
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002955 obj->properties->ids[count] = property->base.id;
2956 obj->properties->values[count] = init_val;
2957 obj->properties->count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03002958}
2959EXPORT_SYMBOL(drm_object_attach_property);
2960
2961int drm_object_property_set_value(struct drm_mode_object *obj,
2962 struct drm_property *property, uint64_t val)
2963{
2964 int i;
2965
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002966 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002967 if (obj->properties->ids[i] == property->base.id) {
2968 obj->properties->values[i] = val;
2969 return 0;
2970 }
2971 }
2972
2973 return -EINVAL;
2974}
2975EXPORT_SYMBOL(drm_object_property_set_value);
2976
2977int drm_object_property_get_value(struct drm_mode_object *obj,
2978 struct drm_property *property, uint64_t *val)
2979{
2980 int i;
2981
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03002982 for (i = 0; i < obj->properties->count; i++) {
Paulo Zanonic5431882012-05-15 18:09:02 -03002983 if (obj->properties->ids[i] == property->base.id) {
2984 *val = obj->properties->values[i];
2985 return 0;
2986 }
2987 }
2988
2989 return -EINVAL;
2990}
2991EXPORT_SYMBOL(drm_object_property_get_value);
2992
Dave Airlief453ba02008-11-07 14:05:41 -08002993int drm_mode_getproperty_ioctl(struct drm_device *dev,
2994 void *data, struct drm_file *file_priv)
2995{
2996 struct drm_mode_object *obj;
2997 struct drm_mode_get_property *out_resp = data;
2998 struct drm_property *property;
2999 int enum_count = 0;
3000 int blob_count = 0;
3001 int value_count = 0;
3002 int ret = 0, i;
3003 int copied;
3004 struct drm_property_enum *prop_enum;
3005 struct drm_mode_property_enum __user *enum_ptr;
3006 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003007 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003008 uint64_t __user *values_ptr;
3009 uint32_t __user *blob_length_ptr;
3010
Dave Airliefb3b06c2011-02-08 13:55:21 +10003011 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3012 return -EINVAL;
3013
Dave Airlief453ba02008-11-07 14:05:41 -08003014 mutex_lock(&dev->mode_config.mutex);
3015 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3016 if (!obj) {
3017 ret = -EINVAL;
3018 goto done;
3019 }
3020 property = obj_to_property(obj);
3021
Rob Clark49e27542012-05-17 02:23:26 -06003022 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003023 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
3024 enum_count++;
3025 } else if (property->flags & DRM_MODE_PROP_BLOB) {
3026 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
3027 blob_count++;
3028 }
3029
3030 value_count = property->num_values;
3031
3032 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3033 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3034 out_resp->flags = property->flags;
3035
3036 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003037 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003038 for (i = 0; i < value_count; i++) {
3039 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3040 ret = -EFAULT;
3041 goto done;
3042 }
3043 }
3044 }
3045 out_resp->count_values = value_count;
3046
Rob Clark49e27542012-05-17 02:23:26 -06003047 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003048 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3049 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003050 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003051 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
3052
3053 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3054 ret = -EFAULT;
3055 goto done;
3056 }
3057
3058 if (copy_to_user(&enum_ptr[copied].name,
3059 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3060 ret = -EFAULT;
3061 goto done;
3062 }
3063 copied++;
3064 }
3065 }
3066 out_resp->count_enum_blobs = enum_count;
3067 }
3068
3069 if (property->flags & DRM_MODE_PROP_BLOB) {
3070 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3071 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003072 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3073 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003074
3075 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3076 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3077 ret = -EFAULT;
3078 goto done;
3079 }
3080
3081 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3082 ret = -EFAULT;
3083 goto done;
3084 }
3085
3086 copied++;
3087 }
3088 }
3089 out_resp->count_enum_blobs = blob_count;
3090 }
3091done:
3092 mutex_unlock(&dev->mode_config.mutex);
3093 return ret;
3094}
3095
3096static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3097 void *data)
3098{
3099 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003100 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003101
3102 if (!length || !data)
3103 return NULL;
3104
3105 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3106 if (!blob)
3107 return NULL;
3108
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003109 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3110 if (ret) {
3111 kfree(blob);
3112 return NULL;
3113 }
3114
Dave Airlief453ba02008-11-07 14:05:41 -08003115 blob->length = length;
3116
3117 memcpy(blob->data, data, length);
3118
Dave Airlief453ba02008-11-07 14:05:41 -08003119 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3120 return blob;
3121}
3122
3123static void drm_property_destroy_blob(struct drm_device *dev,
3124 struct drm_property_blob *blob)
3125{
3126 drm_mode_object_put(dev, &blob->base);
3127 list_del(&blob->head);
3128 kfree(blob);
3129}
3130
3131int drm_mode_getblob_ioctl(struct drm_device *dev,
3132 void *data, struct drm_file *file_priv)
3133{
3134 struct drm_mode_object *obj;
3135 struct drm_mode_get_blob *out_resp = data;
3136 struct drm_property_blob *blob;
3137 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003138 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003139
Dave Airliefb3b06c2011-02-08 13:55:21 +10003140 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3141 return -EINVAL;
3142
Dave Airlief453ba02008-11-07 14:05:41 -08003143 mutex_lock(&dev->mode_config.mutex);
3144 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3145 if (!obj) {
3146 ret = -EINVAL;
3147 goto done;
3148 }
3149 blob = obj_to_blob(obj);
3150
3151 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003152 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003153 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3154 ret = -EFAULT;
3155 goto done;
3156 }
3157 }
3158 out_resp->length = blob->length;
3159
3160done:
3161 mutex_unlock(&dev->mode_config.mutex);
3162 return ret;
3163}
3164
3165int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3166 struct edid *edid)
3167{
3168 struct drm_device *dev = connector->dev;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02003169 int ret, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003170
3171 if (connector->edid_blob_ptr)
3172 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3173
3174 /* Delete edid, when there is none. */
3175 if (!edid) {
3176 connector->edid_blob_ptr = NULL;
3177 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3178 return ret;
3179 }
3180
Adam Jackson7466f4c2010-03-29 21:43:23 +00003181 size = EDID_LENGTH * (1 + edid->extensions);
3182 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3183 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08003184
3185 ret = drm_connector_property_set_value(connector,
3186 dev->mode_config.edid_property,
3187 connector->edid_blob_ptr->base.id);
3188
3189 return ret;
3190}
3191EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3192
Paulo Zanoni26a34812012-05-15 18:08:59 -03003193static bool drm_property_change_is_valid(struct drm_property *property,
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003194 uint64_t value)
Paulo Zanoni26a34812012-05-15 18:08:59 -03003195{
3196 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3197 return false;
3198 if (property->flags & DRM_MODE_PROP_RANGE) {
3199 if (value < property->values[0] || value > property->values[1])
3200 return false;
3201 return true;
Rob Clark49e27542012-05-17 02:23:26 -06003202 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3203 int i;
Ville Syrjälä592c20e2012-05-24 20:53:58 +03003204 uint64_t valid_mask = 0;
Rob Clark49e27542012-05-17 02:23:26 -06003205 for (i = 0; i < property->num_values; i++)
3206 valid_mask |= (1ULL << property->values[i]);
3207 return !(value & ~valid_mask);
Paulo Zanoni26a34812012-05-15 18:08:59 -03003208 } else {
3209 int i;
3210 for (i = 0; i < property->num_values; i++)
3211 if (property->values[i] == value)
3212 return true;
3213 return false;
3214 }
3215}
3216
Dave Airlief453ba02008-11-07 14:05:41 -08003217int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3218 void *data, struct drm_file *file_priv)
3219{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003220 struct drm_mode_connector_set_property *conn_set_prop = data;
3221 struct drm_mode_obj_set_property obj_set_prop = {
3222 .value = conn_set_prop->value,
3223 .prop_id = conn_set_prop->prop_id,
3224 .obj_id = conn_set_prop->connector_id,
3225 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3226 };
Dave Airlief453ba02008-11-07 14:05:41 -08003227
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03003228 /* It does all the locking and checking we need */
3229 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08003230}
3231
Paulo Zanonic5431882012-05-15 18:09:02 -03003232static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3233 struct drm_property *property,
3234 uint64_t value)
3235{
3236 int ret = -EINVAL;
3237 struct drm_connector *connector = obj_to_connector(obj);
3238
3239 /* Do DPMS ourselves */
3240 if (property == connector->dev->mode_config.dpms_property) {
3241 if (connector->funcs->dpms)
3242 (*connector->funcs->dpms)(connector, (int)value);
3243 ret = 0;
3244 } else if (connector->funcs->set_property)
3245 ret = connector->funcs->set_property(connector, property, value);
3246
3247 /* store the property value if successful */
3248 if (!ret)
3249 drm_connector_property_set_value(connector, property, value);
3250 return ret;
3251}
3252
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003253static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3254 struct drm_property *property,
3255 uint64_t value)
3256{
3257 int ret = -EINVAL;
3258 struct drm_crtc *crtc = obj_to_crtc(obj);
3259
3260 if (crtc->funcs->set_property)
3261 ret = crtc->funcs->set_property(crtc, property, value);
3262 if (!ret)
3263 drm_object_property_set_value(obj, property, value);
3264
3265 return ret;
3266}
3267
Rob Clark4d939142012-05-17 02:23:27 -06003268static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3269 struct drm_property *property,
3270 uint64_t value)
3271{
3272 int ret = -EINVAL;
3273 struct drm_plane *plane = obj_to_plane(obj);
3274
3275 if (plane->funcs->set_property)
3276 ret = plane->funcs->set_property(plane, property, value);
3277 if (!ret)
3278 drm_object_property_set_value(obj, property, value);
3279
3280 return ret;
3281}
3282
Paulo Zanonic5431882012-05-15 18:09:02 -03003283int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3284 struct drm_file *file_priv)
3285{
3286 struct drm_mode_obj_get_properties *arg = data;
3287 struct drm_mode_object *obj;
3288 int ret = 0;
3289 int i;
3290 int copied = 0;
3291 int props_count = 0;
3292 uint32_t __user *props_ptr;
3293 uint64_t __user *prop_values_ptr;
3294
3295 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3296 return -EINVAL;
3297
3298 mutex_lock(&dev->mode_config.mutex);
3299
3300 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3301 if (!obj) {
3302 ret = -EINVAL;
3303 goto out;
3304 }
3305 if (!obj->properties) {
3306 ret = -EINVAL;
3307 goto out;
3308 }
3309
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003310 props_count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003311
3312 /* This ioctl is called twice, once to determine how much space is
3313 * needed, and the 2nd time to fill it. */
3314 if ((arg->count_props >= props_count) && props_count) {
3315 copied = 0;
3316 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3317 prop_values_ptr = (uint64_t __user *)(unsigned long)
3318 (arg->prop_values_ptr);
3319 for (i = 0; i < props_count; i++) {
3320 if (put_user(obj->properties->ids[i],
3321 props_ptr + copied)) {
3322 ret = -EFAULT;
3323 goto out;
3324 }
3325 if (put_user(obj->properties->values[i],
3326 prop_values_ptr + copied)) {
3327 ret = -EFAULT;
3328 goto out;
3329 }
3330 copied++;
3331 }
3332 }
3333 arg->count_props = props_count;
3334out:
3335 mutex_unlock(&dev->mode_config.mutex);
3336 return ret;
3337}
3338
3339int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3340 struct drm_file *file_priv)
3341{
3342 struct drm_mode_obj_set_property *arg = data;
3343 struct drm_mode_object *arg_obj;
3344 struct drm_mode_object *prop_obj;
3345 struct drm_property *property;
3346 int ret = -EINVAL;
3347 int i;
3348
3349 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3350 return -EINVAL;
3351
3352 mutex_lock(&dev->mode_config.mutex);
3353
3354 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3355 if (!arg_obj)
3356 goto out;
3357 if (!arg_obj->properties)
3358 goto out;
3359
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003360 for (i = 0; i < arg_obj->properties->count; i++)
Paulo Zanonic5431882012-05-15 18:09:02 -03003361 if (arg_obj->properties->ids[i] == arg->prop_id)
3362 break;
3363
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003364 if (i == arg_obj->properties->count)
Paulo Zanonic5431882012-05-15 18:09:02 -03003365 goto out;
3366
3367 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3368 DRM_MODE_OBJECT_PROPERTY);
3369 if (!prop_obj)
3370 goto out;
3371 property = obj_to_property(prop_obj);
3372
3373 if (!drm_property_change_is_valid(property, arg->value))
3374 goto out;
3375
3376 switch (arg_obj->type) {
3377 case DRM_MODE_OBJECT_CONNECTOR:
3378 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3379 arg->value);
3380 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03003381 case DRM_MODE_OBJECT_CRTC:
3382 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3383 break;
Rob Clark4d939142012-05-17 02:23:27 -06003384 case DRM_MODE_OBJECT_PLANE:
3385 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3386 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03003387 }
3388
3389out:
3390 mutex_unlock(&dev->mode_config.mutex);
3391 return ret;
3392}
3393
Dave Airlief453ba02008-11-07 14:05:41 -08003394int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3395 struct drm_encoder *encoder)
3396{
3397 int i;
3398
3399 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3400 if (connector->encoder_ids[i] == 0) {
3401 connector->encoder_ids[i] = encoder->base.id;
3402 return 0;
3403 }
3404 }
3405 return -ENOMEM;
3406}
3407EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3408
3409void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3410 struct drm_encoder *encoder)
3411{
3412 int i;
3413 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3414 if (connector->encoder_ids[i] == encoder->base.id) {
3415 connector->encoder_ids[i] = 0;
3416 if (connector->encoder == encoder)
3417 connector->encoder = NULL;
3418 break;
3419 }
3420 }
3421}
3422EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3423
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003424int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003425 int gamma_size)
3426{
3427 crtc->gamma_size = gamma_size;
3428
3429 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3430 if (!crtc->gamma_store) {
3431 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003432 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003433 }
3434
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003435 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003436}
3437EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3438
3439int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3440 void *data, struct drm_file *file_priv)
3441{
3442 struct drm_mode_crtc_lut *crtc_lut = data;
3443 struct drm_mode_object *obj;
3444 struct drm_crtc *crtc;
3445 void *r_base, *g_base, *b_base;
3446 int size;
3447 int ret = 0;
3448
Dave Airliefb3b06c2011-02-08 13:55:21 +10003449 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3450 return -EINVAL;
3451
Dave Airlief453ba02008-11-07 14:05:41 -08003452 mutex_lock(&dev->mode_config.mutex);
3453 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3454 if (!obj) {
3455 ret = -EINVAL;
3456 goto out;
3457 }
3458 crtc = obj_to_crtc(obj);
3459
Laurent Pinchartebe0f242012-05-17 13:27:24 +02003460 if (crtc->funcs->gamma_set == NULL) {
3461 ret = -ENOSYS;
3462 goto out;
3463 }
3464
Dave Airlief453ba02008-11-07 14:05:41 -08003465 /* memcpy into gamma store */
3466 if (crtc_lut->gamma_size != crtc->gamma_size) {
3467 ret = -EINVAL;
3468 goto out;
3469 }
3470
3471 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3472 r_base = crtc->gamma_store;
3473 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3474 ret = -EFAULT;
3475 goto out;
3476 }
3477
3478 g_base = r_base + size;
3479 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3480 ret = -EFAULT;
3481 goto out;
3482 }
3483
3484 b_base = g_base + size;
3485 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3486 ret = -EFAULT;
3487 goto out;
3488 }
3489
James Simmons72034252010-08-03 01:33:19 +01003490 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003491
3492out:
3493 mutex_unlock(&dev->mode_config.mutex);
3494 return ret;
3495
3496}
3497
3498int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3499 void *data, struct drm_file *file_priv)
3500{
3501 struct drm_mode_crtc_lut *crtc_lut = data;
3502 struct drm_mode_object *obj;
3503 struct drm_crtc *crtc;
3504 void *r_base, *g_base, *b_base;
3505 int size;
3506 int ret = 0;
3507
Dave Airliefb3b06c2011-02-08 13:55:21 +10003508 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3509 return -EINVAL;
3510
Dave Airlief453ba02008-11-07 14:05:41 -08003511 mutex_lock(&dev->mode_config.mutex);
3512 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3513 if (!obj) {
3514 ret = -EINVAL;
3515 goto out;
3516 }
3517 crtc = obj_to_crtc(obj);
3518
3519 /* memcpy into gamma store */
3520 if (crtc_lut->gamma_size != crtc->gamma_size) {
3521 ret = -EINVAL;
3522 goto out;
3523 }
3524
3525 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3526 r_base = crtc->gamma_store;
3527 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3528 ret = -EFAULT;
3529 goto out;
3530 }
3531
3532 g_base = r_base + size;
3533 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3534 ret = -EFAULT;
3535 goto out;
3536 }
3537
3538 b_base = g_base + size;
3539 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3540 ret = -EFAULT;
3541 goto out;
3542 }
3543out:
3544 mutex_unlock(&dev->mode_config.mutex);
3545 return ret;
3546}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003547
3548int drm_mode_page_flip_ioctl(struct drm_device *dev,
3549 void *data, struct drm_file *file_priv)
3550{
3551 struct drm_mode_crtc_page_flip *page_flip = data;
3552 struct drm_mode_object *obj;
3553 struct drm_crtc *crtc;
3554 struct drm_framebuffer *fb;
3555 struct drm_pending_vblank_event *e = NULL;
3556 unsigned long flags;
Rob Clark7c80e122012-09-04 16:35:56 +00003557 int hdisplay, vdisplay;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003558 int ret = -EINVAL;
3559
3560 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3561 page_flip->reserved != 0)
3562 return -EINVAL;
3563
3564 mutex_lock(&dev->mode_config.mutex);
3565 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3566 if (!obj)
3567 goto out;
3568 crtc = obj_to_crtc(obj);
3569
Chris Wilson90c1efd2010-07-17 20:23:26 +01003570 if (crtc->fb == NULL) {
3571 /* The framebuffer is currently unbound, presumably
3572 * due to a hotplug event, that userspace has not
3573 * yet discovered.
3574 */
3575 ret = -EBUSY;
3576 goto out;
3577 }
3578
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003579 if (crtc->funcs->page_flip == NULL)
3580 goto out;
3581
3582 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3583 if (!obj)
3584 goto out;
3585 fb = obj_to_fb(obj);
3586
Rob Clark7c80e122012-09-04 16:35:56 +00003587 hdisplay = crtc->mode.hdisplay;
3588 vdisplay = crtc->mode.vdisplay;
3589
3590 if (crtc->invert_dimensions)
3591 swap(hdisplay, vdisplay);
3592
3593 if (hdisplay > fb->width ||
3594 vdisplay > fb->height ||
3595 crtc->x > fb->width - hdisplay ||
3596 crtc->y > fb->height - vdisplay) {
3597 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
3598 fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
3599 crtc->invert_dimensions ? " (inverted)" : "");
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003600 ret = -ENOSPC;
3601 goto out;
3602 }
3603
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003604 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3605 ret = -ENOMEM;
3606 spin_lock_irqsave(&dev->event_lock, flags);
3607 if (file_priv->event_space < sizeof e->event) {
3608 spin_unlock_irqrestore(&dev->event_lock, flags);
3609 goto out;
3610 }
3611 file_priv->event_space -= sizeof e->event;
3612 spin_unlock_irqrestore(&dev->event_lock, flags);
3613
3614 e = kzalloc(sizeof *e, GFP_KERNEL);
3615 if (e == NULL) {
3616 spin_lock_irqsave(&dev->event_lock, flags);
3617 file_priv->event_space += sizeof e->event;
3618 spin_unlock_irqrestore(&dev->event_lock, flags);
3619 goto out;
3620 }
3621
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003622 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003623 e->event.base.length = sizeof e->event;
3624 e->event.user_data = page_flip->user_data;
3625 e->base.event = &e->event.base;
3626 e->base.file_priv = file_priv;
3627 e->base.destroy =
3628 (void (*) (struct drm_pending_event *)) kfree;
3629 }
3630
3631 ret = crtc->funcs->page_flip(crtc, fb, e);
3632 if (ret) {
Joonyoung Shimaef6a7e2012-04-18 13:47:02 +09003633 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3634 spin_lock_irqsave(&dev->event_lock, flags);
3635 file_priv->event_space += sizeof e->event;
3636 spin_unlock_irqrestore(&dev->event_lock, flags);
3637 kfree(e);
3638 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003639 }
3640
3641out:
3642 mutex_unlock(&dev->mode_config.mutex);
3643 return ret;
3644}
Chris Wilsoneb033552011-01-24 15:11:08 +00003645
3646void drm_mode_config_reset(struct drm_device *dev)
3647{
3648 struct drm_crtc *crtc;
3649 struct drm_encoder *encoder;
3650 struct drm_connector *connector;
3651
3652 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3653 if (crtc->funcs->reset)
3654 crtc->funcs->reset(crtc);
3655
3656 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3657 if (encoder->funcs->reset)
3658 encoder->funcs->reset(encoder);
3659
3660 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3661 if (connector->funcs->reset)
3662 connector->funcs->reset(connector);
3663}
3664EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003665
3666int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3667 void *data, struct drm_file *file_priv)
3668{
3669 struct drm_mode_create_dumb *args = data;
3670
3671 if (!dev->driver->dumb_create)
3672 return -ENOSYS;
3673 return dev->driver->dumb_create(file_priv, dev, args);
3674}
3675
3676int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3677 void *data, struct drm_file *file_priv)
3678{
3679 struct drm_mode_map_dumb *args = data;
3680
3681 /* call driver ioctl to get mmap offset */
3682 if (!dev->driver->dumb_map_offset)
3683 return -ENOSYS;
3684
3685 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3686}
3687
3688int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3689 void *data, struct drm_file *file_priv)
3690{
3691 struct drm_mode_destroy_dumb *args = data;
3692
3693 if (!dev->driver->dumb_destroy)
3694 return -ENOSYS;
3695
3696 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3697}
Dave Airlie248dbc22011-11-29 20:02:54 +00003698
3699/*
3700 * Just need to support RGB formats here for compat with code that doesn't
3701 * use pixel formats directly yet.
3702 */
3703void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3704 int *bpp)
3705{
3706 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003707 case DRM_FORMAT_RGB332:
3708 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003709 *depth = 8;
3710 *bpp = 8;
3711 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003712 case DRM_FORMAT_XRGB1555:
3713 case DRM_FORMAT_XBGR1555:
3714 case DRM_FORMAT_RGBX5551:
3715 case DRM_FORMAT_BGRX5551:
3716 case DRM_FORMAT_ARGB1555:
3717 case DRM_FORMAT_ABGR1555:
3718 case DRM_FORMAT_RGBA5551:
3719 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003720 *depth = 15;
3721 *bpp = 16;
3722 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003723 case DRM_FORMAT_RGB565:
3724 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003725 *depth = 16;
3726 *bpp = 16;
3727 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003728 case DRM_FORMAT_RGB888:
3729 case DRM_FORMAT_BGR888:
3730 *depth = 24;
3731 *bpp = 24;
3732 break;
3733 case DRM_FORMAT_XRGB8888:
3734 case DRM_FORMAT_XBGR8888:
3735 case DRM_FORMAT_RGBX8888:
3736 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003737 *depth = 24;
3738 *bpp = 32;
3739 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003740 case DRM_FORMAT_XRGB2101010:
3741 case DRM_FORMAT_XBGR2101010:
3742 case DRM_FORMAT_RGBX1010102:
3743 case DRM_FORMAT_BGRX1010102:
3744 case DRM_FORMAT_ARGB2101010:
3745 case DRM_FORMAT_ABGR2101010:
3746 case DRM_FORMAT_RGBA1010102:
3747 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003748 *depth = 30;
3749 *bpp = 32;
3750 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003751 case DRM_FORMAT_ARGB8888:
3752 case DRM_FORMAT_ABGR8888:
3753 case DRM_FORMAT_RGBA8888:
3754 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003755 *depth = 32;
3756 *bpp = 32;
3757 break;
3758 default:
3759 DRM_DEBUG_KMS("unsupported pixel format\n");
3760 *depth = 0;
3761 *bpp = 0;
3762 break;
3763 }
3764}
3765EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003766
3767/**
3768 * drm_format_num_planes - get the number of planes for format
3769 * @format: pixel format (DRM_FORMAT_*)
3770 *
3771 * RETURNS:
3772 * The number of planes used by the specified pixel format.
3773 */
3774int drm_format_num_planes(uint32_t format)
3775{
3776 switch (format) {
3777 case DRM_FORMAT_YUV410:
3778 case DRM_FORMAT_YVU410:
3779 case DRM_FORMAT_YUV411:
3780 case DRM_FORMAT_YVU411:
3781 case DRM_FORMAT_YUV420:
3782 case DRM_FORMAT_YVU420:
3783 case DRM_FORMAT_YUV422:
3784 case DRM_FORMAT_YVU422:
3785 case DRM_FORMAT_YUV444:
3786 case DRM_FORMAT_YVU444:
3787 return 3;
3788 case DRM_FORMAT_NV12:
3789 case DRM_FORMAT_NV21:
3790 case DRM_FORMAT_NV16:
3791 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003792 case DRM_FORMAT_NV24:
3793 case DRM_FORMAT_NV42:
Ville Syrjälä141670e2012-04-05 21:35:15 +03003794 return 2;
3795 default:
3796 return 1;
3797 }
3798}
3799EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003800
3801/**
3802 * drm_format_plane_cpp - determine the bytes per pixel value
3803 * @format: pixel format (DRM_FORMAT_*)
3804 * @plane: plane index
3805 *
3806 * RETURNS:
3807 * The bytes per pixel value for the specified plane.
3808 */
3809int drm_format_plane_cpp(uint32_t format, int plane)
3810{
3811 unsigned int depth;
3812 int bpp;
3813
3814 if (plane >= drm_format_num_planes(format))
3815 return 0;
3816
3817 switch (format) {
3818 case DRM_FORMAT_YUYV:
3819 case DRM_FORMAT_YVYU:
3820 case DRM_FORMAT_UYVY:
3821 case DRM_FORMAT_VYUY:
3822 return 2;
3823 case DRM_FORMAT_NV12:
3824 case DRM_FORMAT_NV21:
3825 case DRM_FORMAT_NV16:
3826 case DRM_FORMAT_NV61:
Laurent Pinchartba623f62012-05-18 23:47:40 +02003827 case DRM_FORMAT_NV24:
3828 case DRM_FORMAT_NV42:
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003829 return plane ? 2 : 1;
3830 case DRM_FORMAT_YUV410:
3831 case DRM_FORMAT_YVU410:
3832 case DRM_FORMAT_YUV411:
3833 case DRM_FORMAT_YVU411:
3834 case DRM_FORMAT_YUV420:
3835 case DRM_FORMAT_YVU420:
3836 case DRM_FORMAT_YUV422:
3837 case DRM_FORMAT_YVU422:
3838 case DRM_FORMAT_YUV444:
3839 case DRM_FORMAT_YVU444:
3840 return 1;
3841 default:
3842 drm_fb_get_bpp_depth(format, &depth, &bpp);
3843 return bpp >> 3;
3844 }
3845}
3846EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003847
3848/**
3849 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3850 * @format: pixel format (DRM_FORMAT_*)
3851 *
3852 * RETURNS:
3853 * The horizontal chroma subsampling factor for the
3854 * specified pixel format.
3855 */
3856int drm_format_horz_chroma_subsampling(uint32_t format)
3857{
3858 switch (format) {
3859 case DRM_FORMAT_YUV411:
3860 case DRM_FORMAT_YVU411:
3861 case DRM_FORMAT_YUV410:
3862 case DRM_FORMAT_YVU410:
3863 return 4;
3864 case DRM_FORMAT_YUYV:
3865 case DRM_FORMAT_YVYU:
3866 case DRM_FORMAT_UYVY:
3867 case DRM_FORMAT_VYUY:
3868 case DRM_FORMAT_NV12:
3869 case DRM_FORMAT_NV21:
3870 case DRM_FORMAT_NV16:
3871 case DRM_FORMAT_NV61:
3872 case DRM_FORMAT_YUV422:
3873 case DRM_FORMAT_YVU422:
3874 case DRM_FORMAT_YUV420:
3875 case DRM_FORMAT_YVU420:
3876 return 2;
3877 default:
3878 return 1;
3879 }
3880}
3881EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3882
3883/**
3884 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3885 * @format: pixel format (DRM_FORMAT_*)
3886 *
3887 * RETURNS:
3888 * The vertical chroma subsampling factor for the
3889 * specified pixel format.
3890 */
3891int drm_format_vert_chroma_subsampling(uint32_t format)
3892{
3893 switch (format) {
3894 case DRM_FORMAT_YUV410:
3895 case DRM_FORMAT_YVU410:
3896 return 4;
3897 case DRM_FORMAT_YUV420:
3898 case DRM_FORMAT_YVU420:
3899 case DRM_FORMAT_NV12:
3900 case DRM_FORMAT_NV21:
3901 return 2;
3902 default:
3903 return 1;
3904 }
3905}
3906EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);