blob: 6fdaf6fe94eb57dfcc36c6c23a454c1ca8519d80 [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");
230 return -EINVAL;
231 }
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;
238
239 obj->id = new_id;
240 obj->type = obj_type;
241 return 0;
242}
243
244/**
245 * drm_mode_object_put - free an identifer
246 * @dev: DRM device
247 * @id: ID to free
248 *
249 * LOCKING:
250 * Caller must hold DRM mode_config lock.
251 *
252 * Free @id from @dev's unique identifier pool.
253 */
254static void drm_mode_object_put(struct drm_device *dev,
255 struct drm_mode_object *object)
256{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000257 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800258 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000259 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800260}
261
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200262struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800264{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000265 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800266
Jesse Barnesad2563c2009-01-19 17:21:45 +1000267 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800268 obj = idr_find(&dev->mode_config.crtc_idr, id);
269 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000270 obj = NULL;
271 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800272
273 return obj;
274}
275EXPORT_SYMBOL(drm_mode_object_find);
276
277/**
Dave Airlief453ba02008-11-07 14:05:41 -0800278 * drm_framebuffer_init - initialize a framebuffer
279 * @dev: DRM device
280 *
281 * LOCKING:
282 * Caller must hold mode config lock.
283 *
284 * Allocates an ID for the framebuffer's parent mode object, sets its mode
285 * functions & device file and adds it to the master fd list.
286 *
287 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200288 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800289 */
290int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
291 const struct drm_framebuffer_funcs *funcs)
292{
293 int ret;
294
295 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
296 if (ret) {
297 return ret;
298 }
299
300 fb->dev = dev;
301 fb->funcs = funcs;
302 dev->mode_config.num_fb++;
303 list_add(&fb->head, &dev->mode_config.fb_list);
304
305 return 0;
306}
307EXPORT_SYMBOL(drm_framebuffer_init);
308
309/**
310 * drm_framebuffer_cleanup - remove a framebuffer object
311 * @fb: framebuffer to remove
312 *
313 * LOCKING:
314 * Caller must hold mode config lock.
315 *
316 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
317 * it, setting it to NULL.
318 */
319void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
320{
321 struct drm_device *dev = fb->dev;
322 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800323 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000324 struct drm_mode_set set;
325 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800326
327 /* remove from any CRTC */
328 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000329 if (crtc->fb == fb) {
330 /* should turn off the crtc */
331 memset(&set, 0, sizeof(struct drm_mode_set));
332 set.crtc = crtc;
333 set.fb = NULL;
334 ret = crtc->funcs->set_config(&set);
335 if (ret)
336 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
337 }
Dave Airlief453ba02008-11-07 14:05:41 -0800338 }
339
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800340 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
341 if (plane->fb == fb) {
342 /* should turn off the crtc */
343 ret = plane->funcs->disable_plane(plane);
344 if (ret)
345 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600346 /* disconnect the plane from the fb and crtc: */
347 plane->fb = NULL;
348 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800349 }
350 }
351
Dave Airlief453ba02008-11-07 14:05:41 -0800352 drm_mode_object_put(dev, &fb->base);
353 list_del(&fb->head);
354 dev->mode_config.num_fb--;
355}
356EXPORT_SYMBOL(drm_framebuffer_cleanup);
357
358/**
359 * drm_crtc_init - Initialise a new CRTC object
360 * @dev: DRM device
361 * @crtc: CRTC object to init
362 * @funcs: callbacks for the new CRTC
363 *
364 * LOCKING:
365 * Caller must hold mode config lock.
366 *
367 * Inits a new object created as base part of an driver crtc object.
368 */
369void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
370 const struct drm_crtc_funcs *funcs)
371{
372 crtc->dev = dev;
373 crtc->funcs = funcs;
374
375 mutex_lock(&dev->mode_config.mutex);
376 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
377
378 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
379 dev->mode_config.num_crtc++;
380 mutex_unlock(&dev->mode_config.mutex);
381}
382EXPORT_SYMBOL(drm_crtc_init);
383
384/**
385 * drm_crtc_cleanup - Cleans up the core crtc usage.
386 * @crtc: CRTC to cleanup
387 *
388 * LOCKING:
389 * Caller must hold mode config lock.
390 *
391 * Cleanup @crtc. Removes from drm modesetting space
392 * does NOT free object, caller does that.
393 */
394void drm_crtc_cleanup(struct drm_crtc *crtc)
395{
396 struct drm_device *dev = crtc->dev;
397
398 if (crtc->gamma_store) {
399 kfree(crtc->gamma_store);
400 crtc->gamma_store = NULL;
401 }
402
403 drm_mode_object_put(dev, &crtc->base);
404 list_del(&crtc->head);
405 dev->mode_config.num_crtc--;
406}
407EXPORT_SYMBOL(drm_crtc_cleanup);
408
409/**
410 * drm_mode_probed_add - add a mode to a connector's probed mode list
411 * @connector: connector the new mode
412 * @mode: mode data
413 *
414 * LOCKING:
415 * Caller must hold mode config lock.
416 *
417 * Add @mode to @connector's mode list for later use.
418 */
419void drm_mode_probed_add(struct drm_connector *connector,
420 struct drm_display_mode *mode)
421{
422 list_add(&mode->head, &connector->probed_modes);
423}
424EXPORT_SYMBOL(drm_mode_probed_add);
425
426/**
427 * drm_mode_remove - remove and free a mode
428 * @connector: connector list to modify
429 * @mode: mode to remove
430 *
431 * LOCKING:
432 * Caller must hold mode config lock.
433 *
434 * Remove @mode from @connector's mode list, then free it.
435 */
436void drm_mode_remove(struct drm_connector *connector,
437 struct drm_display_mode *mode)
438{
439 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100440 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800441}
442EXPORT_SYMBOL(drm_mode_remove);
443
444/**
445 * drm_connector_init - Init a preallocated connector
446 * @dev: DRM device
447 * @connector: the connector to init
448 * @funcs: callbacks for this connector
449 * @name: user visible name of the connector
450 *
451 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100452 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800453 *
454 * Initialises a preallocated connector. Connectors should be
455 * subclassed as part of driver connector objects.
456 */
457void drm_connector_init(struct drm_device *dev,
458 struct drm_connector *connector,
459 const struct drm_connector_funcs *funcs,
460 int connector_type)
461{
462 mutex_lock(&dev->mode_config.mutex);
463
464 connector->dev = dev;
465 connector->funcs = funcs;
466 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
467 connector->connector_type = connector_type;
468 connector->connector_type_id =
469 ++drm_connector_enum_list[connector_type].count; /* TODO */
470 INIT_LIST_HEAD(&connector->user_modes);
471 INIT_LIST_HEAD(&connector->probed_modes);
472 INIT_LIST_HEAD(&connector->modes);
473 connector->edid_blob_ptr = NULL;
474
475 list_add_tail(&connector->head, &dev->mode_config.connector_list);
476 dev->mode_config.num_connector++;
477
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200478 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
479 drm_connector_attach_property(connector,
480 dev->mode_config.edid_property,
481 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800482
483 drm_connector_attach_property(connector,
484 dev->mode_config.dpms_property, 0);
485
486 mutex_unlock(&dev->mode_config.mutex);
487}
488EXPORT_SYMBOL(drm_connector_init);
489
490/**
491 * drm_connector_cleanup - cleans up an initialised connector
492 * @connector: connector to cleanup
493 *
494 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100495 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800496 *
497 * Cleans up the connector but doesn't free the object.
498 */
499void drm_connector_cleanup(struct drm_connector *connector)
500{
501 struct drm_device *dev = connector->dev;
502 struct drm_display_mode *mode, *t;
503
504 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
505 drm_mode_remove(connector, mode);
506
507 list_for_each_entry_safe(mode, t, &connector->modes, head)
508 drm_mode_remove(connector, mode);
509
510 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
511 drm_mode_remove(connector, mode);
512
513 mutex_lock(&dev->mode_config.mutex);
514 drm_mode_object_put(dev, &connector->base);
515 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000516 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800517 mutex_unlock(&dev->mode_config.mutex);
518}
519EXPORT_SYMBOL(drm_connector_cleanup);
520
521void drm_encoder_init(struct drm_device *dev,
522 struct drm_encoder *encoder,
523 const struct drm_encoder_funcs *funcs,
524 int encoder_type)
525{
526 mutex_lock(&dev->mode_config.mutex);
527
528 encoder->dev = dev;
529
530 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
531 encoder->encoder_type = encoder_type;
532 encoder->funcs = funcs;
533
534 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
535 dev->mode_config.num_encoder++;
536
537 mutex_unlock(&dev->mode_config.mutex);
538}
539EXPORT_SYMBOL(drm_encoder_init);
540
541void drm_encoder_cleanup(struct drm_encoder *encoder)
542{
543 struct drm_device *dev = encoder->dev;
544 mutex_lock(&dev->mode_config.mutex);
545 drm_mode_object_put(dev, &encoder->base);
546 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000547 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800548 mutex_unlock(&dev->mode_config.mutex);
549}
550EXPORT_SYMBOL(drm_encoder_cleanup);
551
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800552int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
553 unsigned long possible_crtcs,
554 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600555 const uint32_t *formats, uint32_t format_count,
556 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800557{
558 mutex_lock(&dev->mode_config.mutex);
559
560 plane->dev = dev;
561 drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
562 plane->funcs = funcs;
563 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
564 GFP_KERNEL);
565 if (!plane->format_types) {
566 DRM_DEBUG_KMS("out of memory when allocating plane\n");
567 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä10bf5732011-12-20 00:06:41 +0200568 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800569 return -ENOMEM;
570 }
571
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800572 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800573 plane->format_count = format_count;
574 plane->possible_crtcs = possible_crtcs;
575
Rob Clark0a7eb242011-12-13 20:19:36 -0600576 /* private planes are not exposed to userspace, but depending on
577 * display hardware, might be convenient to allow sharing programming
578 * for the scanout engine with the crtc implementation.
579 */
580 if (!priv) {
581 list_add_tail(&plane->head, &dev->mode_config.plane_list);
582 dev->mode_config.num_plane++;
583 } else {
584 INIT_LIST_HEAD(&plane->head);
585 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800586
587 mutex_unlock(&dev->mode_config.mutex);
588
589 return 0;
590}
591EXPORT_SYMBOL(drm_plane_init);
592
593void drm_plane_cleanup(struct drm_plane *plane)
594{
595 struct drm_device *dev = plane->dev;
596
597 mutex_lock(&dev->mode_config.mutex);
598 kfree(plane->format_types);
599 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600600 /* if not added to a list, it must be a private plane */
601 if (!list_empty(&plane->head)) {
602 list_del(&plane->head);
603 dev->mode_config.num_plane--;
604 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800605 mutex_unlock(&dev->mode_config.mutex);
606}
607EXPORT_SYMBOL(drm_plane_cleanup);
608
Dave Airlief453ba02008-11-07 14:05:41 -0800609/**
610 * drm_mode_create - create a new display mode
611 * @dev: DRM device
612 *
613 * LOCKING:
614 * Caller must hold DRM mode_config lock.
615 *
616 * Create a new drm_display_mode, give it an ID, and return it.
617 *
618 * RETURNS:
619 * Pointer to new mode on success, NULL on error.
620 */
621struct drm_display_mode *drm_mode_create(struct drm_device *dev)
622{
623 struct drm_display_mode *nmode;
624
625 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
626 if (!nmode)
627 return NULL;
628
629 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
630 return nmode;
631}
632EXPORT_SYMBOL(drm_mode_create);
633
634/**
635 * drm_mode_destroy - remove a mode
636 * @dev: DRM device
637 * @mode: mode to remove
638 *
639 * LOCKING:
640 * Caller must hold mode config lock.
641 *
642 * Free @mode's unique identifier, then free it.
643 */
644void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
645{
646 drm_mode_object_put(dev, &mode->base);
647
648 kfree(mode);
649}
650EXPORT_SYMBOL(drm_mode_destroy);
651
652static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
653{
654 struct drm_property *edid;
655 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800656
657 /*
658 * Standard properties (apply to all connectors)
659 */
660 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
661 DRM_MODE_PROP_IMMUTABLE,
662 "EDID", 0);
663 dev->mode_config.edid_property = edid;
664
Sascha Hauer4a67d392012-02-06 10:58:17 +0100665 dpms = drm_property_create_enum(dev, 0,
666 "DPMS", drm_dpms_enum_list,
667 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800668 dev->mode_config.dpms_property = dpms;
669
670 return 0;
671}
672
673/**
674 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
675 * @dev: DRM device
676 *
677 * Called by a driver the first time a DVI-I connector is made.
678 */
679int drm_mode_create_dvi_i_properties(struct drm_device *dev)
680{
681 struct drm_property *dvi_i_selector;
682 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800683
684 if (dev->mode_config.dvi_i_select_subconnector_property)
685 return 0;
686
687 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100688 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800689 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100690 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800691 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800692 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
693
Sascha Hauer4a67d392012-02-06 10:58:17 +0100694 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800695 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100696 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800697 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800698 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
699
700 return 0;
701}
702EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
703
704/**
705 * drm_create_tv_properties - create TV specific connector properties
706 * @dev: DRM device
707 * @num_modes: number of different TV formats (modes) supported
708 * @modes: array of pointers to strings containing name of each format
709 *
710 * Called by a driver's TV initialization routine, this function creates
711 * the TV specific connector properties for a given device. Caller is
712 * responsible for allocating a list of format names and passing them to
713 * this routine.
714 */
715int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
716 char *modes[])
717{
718 struct drm_property *tv_selector;
719 struct drm_property *tv_subconnector;
720 int i;
721
722 if (dev->mode_config.tv_select_subconnector_property)
723 return 0;
724
725 /*
726 * Basic connector properties
727 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100728 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800729 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100730 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800731 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800732 dev->mode_config.tv_select_subconnector_property = tv_selector;
733
734 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100735 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
736 "subconnector",
737 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800738 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800739 dev->mode_config.tv_subconnector_property = tv_subconnector;
740
741 /*
742 * Other, TV specific properties: margins & TV modes.
743 */
744 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100745 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800746
747 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100748 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800749
750 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100751 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800752
753 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100754 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800755
756 dev->mode_config.tv_mode_property =
757 drm_property_create(dev, DRM_MODE_PROP_ENUM,
758 "mode", num_modes);
759 for (i = 0; i < num_modes; i++)
760 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
761 i, modes[i]);
762
Francisco Jerezb6b79022009-08-02 04:19:20 +0200763 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100764 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200765
766 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100767 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200768
769 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100770 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200771
Francisco Jereza75f0232009-08-12 02:30:10 +0200772 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100773 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200774
775 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100776 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200777
778 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100779 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200780
Dave Airlief453ba02008-11-07 14:05:41 -0800781 return 0;
782}
783EXPORT_SYMBOL(drm_mode_create_tv_properties);
784
785/**
786 * drm_mode_create_scaling_mode_property - create scaling mode property
787 * @dev: DRM device
788 *
789 * Called by a driver the first time it's needed, must be attached to desired
790 * connectors.
791 */
792int drm_mode_create_scaling_mode_property(struct drm_device *dev)
793{
794 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800795
796 if (dev->mode_config.scaling_mode_property)
797 return 0;
798
799 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100800 drm_property_create_enum(dev, 0, "scaling mode",
801 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800802 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800803
804 dev->mode_config.scaling_mode_property = scaling_mode;
805
806 return 0;
807}
808EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
809
810/**
811 * drm_mode_create_dithering_property - create dithering property
812 * @dev: DRM device
813 *
814 * Called by a driver the first time it's needed, must be attached to desired
815 * connectors.
816 */
817int drm_mode_create_dithering_property(struct drm_device *dev)
818{
819 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800820
821 if (dev->mode_config.dithering_mode_property)
822 return 0;
823
824 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100825 drm_property_create_enum(dev, 0, "dithering",
826 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800827 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800828 dev->mode_config.dithering_mode_property = dithering_mode;
829
830 return 0;
831}
832EXPORT_SYMBOL(drm_mode_create_dithering_property);
833
834/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000835 * drm_mode_create_dirty_property - create dirty property
836 * @dev: DRM device
837 *
838 * Called by a driver the first time it's needed, must be attached to desired
839 * connectors.
840 */
841int drm_mode_create_dirty_info_property(struct drm_device *dev)
842{
843 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000844
845 if (dev->mode_config.dirty_info_property)
846 return 0;
847
848 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100849 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000850 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100851 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000852 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000853 dev->mode_config.dirty_info_property = dirty_info;
854
855 return 0;
856}
857EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
858
859/**
Dave Airlief453ba02008-11-07 14:05:41 -0800860 * drm_mode_config_init - initialize DRM mode_configuration structure
861 * @dev: DRM device
862 *
863 * LOCKING:
864 * None, should happen single threaded at init time.
865 *
866 * Initialize @dev's mode_config structure, used for tracking the graphics
867 * configuration of @dev.
868 */
869void drm_mode_config_init(struct drm_device *dev)
870{
871 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000872 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800873 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800874 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
875 INIT_LIST_HEAD(&dev->mode_config.connector_list);
876 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
877 INIT_LIST_HEAD(&dev->mode_config.property_list);
878 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800879 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800880 idr_init(&dev->mode_config.crtc_idr);
881
882 mutex_lock(&dev->mode_config.mutex);
883 drm_mode_create_standard_connector_properties(dev);
884 mutex_unlock(&dev->mode_config.mutex);
885
886 /* Just to be sure */
887 dev->mode_config.num_fb = 0;
888 dev->mode_config.num_connector = 0;
889 dev->mode_config.num_crtc = 0;
890 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800891}
892EXPORT_SYMBOL(drm_mode_config_init);
893
894int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
895{
896 uint32_t total_objects = 0;
897
898 total_objects += dev->mode_config.num_crtc;
899 total_objects += dev->mode_config.num_connector;
900 total_objects += dev->mode_config.num_encoder;
901
Dave Airlief453ba02008-11-07 14:05:41 -0800902 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
903 if (!group->id_list)
904 return -ENOMEM;
905
906 group->num_crtcs = 0;
907 group->num_connectors = 0;
908 group->num_encoders = 0;
909 return 0;
910}
911
912int drm_mode_group_init_legacy_group(struct drm_device *dev,
913 struct drm_mode_group *group)
914{
915 struct drm_crtc *crtc;
916 struct drm_encoder *encoder;
917 struct drm_connector *connector;
918 int ret;
919
920 if ((ret = drm_mode_group_init(dev, group)))
921 return ret;
922
923 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
924 group->id_list[group->num_crtcs++] = crtc->base.id;
925
926 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
927 group->id_list[group->num_crtcs + group->num_encoders++] =
928 encoder->base.id;
929
930 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
931 group->id_list[group->num_crtcs + group->num_encoders +
932 group->num_connectors++] = connector->base.id;
933
934 return 0;
935}
936
937/**
938 * drm_mode_config_cleanup - free up DRM mode_config info
939 * @dev: DRM device
940 *
941 * LOCKING:
942 * Caller must hold mode config lock.
943 *
944 * Free up all the connectors and CRTCs associated with this DRM device, then
945 * free up the framebuffers and associated buffer objects.
946 *
947 * FIXME: cleanup any dangling user buffer objects too
948 */
949void drm_mode_config_cleanup(struct drm_device *dev)
950{
951 struct drm_connector *connector, *ot;
952 struct drm_crtc *crtc, *ct;
953 struct drm_encoder *encoder, *enct;
954 struct drm_framebuffer *fb, *fbt;
955 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800956 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -0800957
958 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
959 head) {
960 encoder->funcs->destroy(encoder);
961 }
962
963 list_for_each_entry_safe(connector, ot,
964 &dev->mode_config.connector_list, head) {
965 connector->funcs->destroy(connector);
966 }
967
968 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
969 head) {
970 drm_property_destroy(dev, property);
971 }
972
973 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
974 fb->funcs->destroy(fb);
975 }
976
977 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
978 crtc->funcs->destroy(crtc);
979 }
980
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800981 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
982 head) {
983 plane->funcs->destroy(plane);
984 }
Sascha Hauer59ce0622012-02-01 11:38:20 +0100985
986 idr_remove_all(&dev->mode_config.crtc_idr);
987 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -0800988}
989EXPORT_SYMBOL(drm_mode_config_cleanup);
990
Dave Airlief453ba02008-11-07 14:05:41 -0800991/**
992 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
993 * @out: drm_mode_modeinfo struct to return to the user
994 * @in: drm_display_mode to use
995 *
996 * LOCKING:
997 * None.
998 *
999 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1000 * the user.
1001 */
1002void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1003 struct drm_display_mode *in)
1004{
1005 out->clock = in->clock;
1006 out->hdisplay = in->hdisplay;
1007 out->hsync_start = in->hsync_start;
1008 out->hsync_end = in->hsync_end;
1009 out->htotal = in->htotal;
1010 out->hskew = in->hskew;
1011 out->vdisplay = in->vdisplay;
1012 out->vsync_start = in->vsync_start;
1013 out->vsync_end = in->vsync_end;
1014 out->vtotal = in->vtotal;
1015 out->vscan = in->vscan;
1016 out->vrefresh = in->vrefresh;
1017 out->flags = in->flags;
1018 out->type = in->type;
1019 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1020 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1021}
1022
1023/**
1024 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1025 * @out: drm_display_mode to return to the user
1026 * @in: drm_mode_modeinfo to use
1027 *
1028 * LOCKING:
1029 * None.
1030 *
1031 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1032 * the caller.
1033 */
1034void drm_crtc_convert_umode(struct drm_display_mode *out,
1035 struct drm_mode_modeinfo *in)
1036{
1037 out->clock = in->clock;
1038 out->hdisplay = in->hdisplay;
1039 out->hsync_start = in->hsync_start;
1040 out->hsync_end = in->hsync_end;
1041 out->htotal = in->htotal;
1042 out->hskew = in->hskew;
1043 out->vdisplay = in->vdisplay;
1044 out->vsync_start = in->vsync_start;
1045 out->vsync_end = in->vsync_end;
1046 out->vtotal = in->vtotal;
1047 out->vscan = in->vscan;
1048 out->vrefresh = in->vrefresh;
1049 out->flags = in->flags;
1050 out->type = in->type;
1051 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1052 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1053}
1054
1055/**
1056 * drm_mode_getresources - get graphics configuration
1057 * @inode: inode from the ioctl
1058 * @filp: file * from the ioctl
1059 * @cmd: cmd from ioctl
1060 * @arg: arg from ioctl
1061 *
1062 * LOCKING:
1063 * Takes mode config lock.
1064 *
1065 * Construct a set of configuration description structures and return
1066 * them to the user, including CRTC, connector and framebuffer configuration.
1067 *
1068 * Called by the user via ioctl.
1069 *
1070 * RETURNS:
1071 * Zero on success, errno on failure.
1072 */
1073int drm_mode_getresources(struct drm_device *dev, void *data,
1074 struct drm_file *file_priv)
1075{
1076 struct drm_mode_card_res *card_res = data;
1077 struct list_head *lh;
1078 struct drm_framebuffer *fb;
1079 struct drm_connector *connector;
1080 struct drm_crtc *crtc;
1081 struct drm_encoder *encoder;
1082 int ret = 0;
1083 int connector_count = 0;
1084 int crtc_count = 0;
1085 int fb_count = 0;
1086 int encoder_count = 0;
1087 int copied = 0, i;
1088 uint32_t __user *fb_id;
1089 uint32_t __user *crtc_id;
1090 uint32_t __user *connector_id;
1091 uint32_t __user *encoder_id;
1092 struct drm_mode_group *mode_group;
1093
Dave Airliefb3b06c2011-02-08 13:55:21 +10001094 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1095 return -EINVAL;
1096
Dave Airlief453ba02008-11-07 14:05:41 -08001097 mutex_lock(&dev->mode_config.mutex);
1098
1099 /*
1100 * For the non-control nodes we need to limit the list of resources
1101 * by IDs in the group list for this node
1102 */
1103 list_for_each(lh, &file_priv->fbs)
1104 fb_count++;
1105
1106 mode_group = &file_priv->master->minor->mode_group;
1107 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1108
1109 list_for_each(lh, &dev->mode_config.crtc_list)
1110 crtc_count++;
1111
1112 list_for_each(lh, &dev->mode_config.connector_list)
1113 connector_count++;
1114
1115 list_for_each(lh, &dev->mode_config.encoder_list)
1116 encoder_count++;
1117 } else {
1118
1119 crtc_count = mode_group->num_crtcs;
1120 connector_count = mode_group->num_connectors;
1121 encoder_count = mode_group->num_encoders;
1122 }
1123
1124 card_res->max_height = dev->mode_config.max_height;
1125 card_res->min_height = dev->mode_config.min_height;
1126 card_res->max_width = dev->mode_config.max_width;
1127 card_res->min_width = dev->mode_config.min_width;
1128
1129 /* handle this in 4 parts */
1130 /* FBs */
1131 if (card_res->count_fbs >= fb_count) {
1132 copied = 0;
1133 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001134 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001135 if (put_user(fb->base.id, fb_id + copied)) {
1136 ret = -EFAULT;
1137 goto out;
1138 }
1139 copied++;
1140 }
1141 }
1142 card_res->count_fbs = fb_count;
1143
1144 /* CRTCs */
1145 if (card_res->count_crtcs >= crtc_count) {
1146 copied = 0;
1147 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1148 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1149 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1150 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001151 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001152 if (put_user(crtc->base.id, crtc_id + copied)) {
1153 ret = -EFAULT;
1154 goto out;
1155 }
1156 copied++;
1157 }
1158 } else {
1159 for (i = 0; i < mode_group->num_crtcs; i++) {
1160 if (put_user(mode_group->id_list[i],
1161 crtc_id + copied)) {
1162 ret = -EFAULT;
1163 goto out;
1164 }
1165 copied++;
1166 }
1167 }
1168 }
1169 card_res->count_crtcs = crtc_count;
1170
1171 /* Encoders */
1172 if (card_res->count_encoders >= encoder_count) {
1173 copied = 0;
1174 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1175 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1176 list_for_each_entry(encoder,
1177 &dev->mode_config.encoder_list,
1178 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001179 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1180 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001181 if (put_user(encoder->base.id, encoder_id +
1182 copied)) {
1183 ret = -EFAULT;
1184 goto out;
1185 }
1186 copied++;
1187 }
1188 } else {
1189 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1190 if (put_user(mode_group->id_list[i],
1191 encoder_id + copied)) {
1192 ret = -EFAULT;
1193 goto out;
1194 }
1195 copied++;
1196 }
1197
1198 }
1199 }
1200 card_res->count_encoders = encoder_count;
1201
1202 /* Connectors */
1203 if (card_res->count_connectors >= connector_count) {
1204 copied = 0;
1205 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1206 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1207 list_for_each_entry(connector,
1208 &dev->mode_config.connector_list,
1209 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001210 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1211 connector->base.id,
1212 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001213 if (put_user(connector->base.id,
1214 connector_id + copied)) {
1215 ret = -EFAULT;
1216 goto out;
1217 }
1218 copied++;
1219 }
1220 } else {
1221 int start = mode_group->num_crtcs +
1222 mode_group->num_encoders;
1223 for (i = start; i < start + mode_group->num_connectors; i++) {
1224 if (put_user(mode_group->id_list[i],
1225 connector_id + copied)) {
1226 ret = -EFAULT;
1227 goto out;
1228 }
1229 copied++;
1230 }
1231 }
1232 }
1233 card_res->count_connectors = connector_count;
1234
Jerome Glisse94401062010-07-15 15:43:25 -04001235 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001236 card_res->count_connectors, card_res->count_encoders);
1237
1238out:
1239 mutex_unlock(&dev->mode_config.mutex);
1240 return ret;
1241}
1242
1243/**
1244 * drm_mode_getcrtc - get CRTC configuration
1245 * @inode: inode from the ioctl
1246 * @filp: file * from the ioctl
1247 * @cmd: cmd from ioctl
1248 * @arg: arg from ioctl
1249 *
1250 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001251 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001252 *
1253 * Construct a CRTC configuration structure to return to the user.
1254 *
1255 * Called by the user via ioctl.
1256 *
1257 * RETURNS:
1258 * Zero on success, errno on failure.
1259 */
1260int drm_mode_getcrtc(struct drm_device *dev,
1261 void *data, struct drm_file *file_priv)
1262{
1263 struct drm_mode_crtc *crtc_resp = data;
1264 struct drm_crtc *crtc;
1265 struct drm_mode_object *obj;
1266 int ret = 0;
1267
Dave Airliefb3b06c2011-02-08 13:55:21 +10001268 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1269 return -EINVAL;
1270
Dave Airlief453ba02008-11-07 14:05:41 -08001271 mutex_lock(&dev->mode_config.mutex);
1272
1273 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1274 DRM_MODE_OBJECT_CRTC);
1275 if (!obj) {
1276 ret = -EINVAL;
1277 goto out;
1278 }
1279 crtc = obj_to_crtc(obj);
1280
1281 crtc_resp->x = crtc->x;
1282 crtc_resp->y = crtc->y;
1283 crtc_resp->gamma_size = crtc->gamma_size;
1284 if (crtc->fb)
1285 crtc_resp->fb_id = crtc->fb->base.id;
1286 else
1287 crtc_resp->fb_id = 0;
1288
1289 if (crtc->enabled) {
1290
1291 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1292 crtc_resp->mode_valid = 1;
1293
1294 } else {
1295 crtc_resp->mode_valid = 0;
1296 }
1297
1298out:
1299 mutex_unlock(&dev->mode_config.mutex);
1300 return ret;
1301}
1302
1303/**
1304 * drm_mode_getconnector - get connector configuration
1305 * @inode: inode from the ioctl
1306 * @filp: file * from the ioctl
1307 * @cmd: cmd from ioctl
1308 * @arg: arg from ioctl
1309 *
1310 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001311 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001312 *
1313 * Construct a connector configuration structure to return to the user.
1314 *
1315 * Called by the user via ioctl.
1316 *
1317 * RETURNS:
1318 * Zero on success, errno on failure.
1319 */
1320int drm_mode_getconnector(struct drm_device *dev, void *data,
1321 struct drm_file *file_priv)
1322{
1323 struct drm_mode_get_connector *out_resp = data;
1324 struct drm_mode_object *obj;
1325 struct drm_connector *connector;
1326 struct drm_display_mode *mode;
1327 int mode_count = 0;
1328 int props_count = 0;
1329 int encoders_count = 0;
1330 int ret = 0;
1331 int copied = 0;
1332 int i;
1333 struct drm_mode_modeinfo u_mode;
1334 struct drm_mode_modeinfo __user *mode_ptr;
1335 uint32_t __user *prop_ptr;
1336 uint64_t __user *prop_values;
1337 uint32_t __user *encoder_ptr;
1338
Dave Airliefb3b06c2011-02-08 13:55:21 +10001339 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1340 return -EINVAL;
1341
Dave Airlief453ba02008-11-07 14:05:41 -08001342 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1343
Jerome Glisse94401062010-07-15 15:43:25 -04001344 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001345
1346 mutex_lock(&dev->mode_config.mutex);
1347
1348 obj = drm_mode_object_find(dev, out_resp->connector_id,
1349 DRM_MODE_OBJECT_CONNECTOR);
1350 if (!obj) {
1351 ret = -EINVAL;
1352 goto out;
1353 }
1354 connector = obj_to_connector(obj);
1355
1356 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1357 if (connector->property_ids[i] != 0) {
1358 props_count++;
1359 }
1360 }
1361
1362 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1363 if (connector->encoder_ids[i] != 0) {
1364 encoders_count++;
1365 }
1366 }
1367
1368 if (out_resp->count_modes == 0) {
1369 connector->funcs->fill_modes(connector,
1370 dev->mode_config.max_width,
1371 dev->mode_config.max_height);
1372 }
1373
1374 /* delayed so we get modes regardless of pre-fill_modes state */
1375 list_for_each_entry(mode, &connector->modes, head)
1376 mode_count++;
1377
1378 out_resp->connector_id = connector->base.id;
1379 out_resp->connector_type = connector->connector_type;
1380 out_resp->connector_type_id = connector->connector_type_id;
1381 out_resp->mm_width = connector->display_info.width_mm;
1382 out_resp->mm_height = connector->display_info.height_mm;
1383 out_resp->subpixel = connector->display_info.subpixel_order;
1384 out_resp->connection = connector->status;
1385 if (connector->encoder)
1386 out_resp->encoder_id = connector->encoder->base.id;
1387 else
1388 out_resp->encoder_id = 0;
1389
1390 /*
1391 * This ioctl is called twice, once to determine how much space is
1392 * needed, and the 2nd time to fill it.
1393 */
1394 if ((out_resp->count_modes >= mode_count) && mode_count) {
1395 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001396 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001397 list_for_each_entry(mode, &connector->modes, head) {
1398 drm_crtc_convert_to_umode(&u_mode, mode);
1399 if (copy_to_user(mode_ptr + copied,
1400 &u_mode, sizeof(u_mode))) {
1401 ret = -EFAULT;
1402 goto out;
1403 }
1404 copied++;
1405 }
1406 }
1407 out_resp->count_modes = mode_count;
1408
1409 if ((out_resp->count_props >= props_count) && props_count) {
1410 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001411 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1412 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001413 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1414 if (connector->property_ids[i] != 0) {
1415 if (put_user(connector->property_ids[i],
1416 prop_ptr + copied)) {
1417 ret = -EFAULT;
1418 goto out;
1419 }
1420
1421 if (put_user(connector->property_values[i],
1422 prop_values + copied)) {
1423 ret = -EFAULT;
1424 goto out;
1425 }
1426 copied++;
1427 }
1428 }
1429 }
1430 out_resp->count_props = props_count;
1431
1432 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1433 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001434 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001435 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1436 if (connector->encoder_ids[i] != 0) {
1437 if (put_user(connector->encoder_ids[i],
1438 encoder_ptr + copied)) {
1439 ret = -EFAULT;
1440 goto out;
1441 }
1442 copied++;
1443 }
1444 }
1445 }
1446 out_resp->count_encoders = encoders_count;
1447
1448out:
1449 mutex_unlock(&dev->mode_config.mutex);
1450 return ret;
1451}
1452
1453int drm_mode_getencoder(struct drm_device *dev, void *data,
1454 struct drm_file *file_priv)
1455{
1456 struct drm_mode_get_encoder *enc_resp = data;
1457 struct drm_mode_object *obj;
1458 struct drm_encoder *encoder;
1459 int ret = 0;
1460
Dave Airliefb3b06c2011-02-08 13:55:21 +10001461 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1462 return -EINVAL;
1463
Dave Airlief453ba02008-11-07 14:05:41 -08001464 mutex_lock(&dev->mode_config.mutex);
1465 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1466 DRM_MODE_OBJECT_ENCODER);
1467 if (!obj) {
1468 ret = -EINVAL;
1469 goto out;
1470 }
1471 encoder = obj_to_encoder(obj);
1472
1473 if (encoder->crtc)
1474 enc_resp->crtc_id = encoder->crtc->base.id;
1475 else
1476 enc_resp->crtc_id = 0;
1477 enc_resp->encoder_type = encoder->encoder_type;
1478 enc_resp->encoder_id = encoder->base.id;
1479 enc_resp->possible_crtcs = encoder->possible_crtcs;
1480 enc_resp->possible_clones = encoder->possible_clones;
1481
1482out:
1483 mutex_unlock(&dev->mode_config.mutex);
1484 return ret;
1485}
1486
1487/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001488 * drm_mode_getplane_res - get plane info
1489 * @dev: DRM device
1490 * @data: ioctl data
1491 * @file_priv: DRM file info
1492 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001493 * LOCKING:
1494 * Takes mode config lock.
1495 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001496 * Return an plane count and set of IDs.
1497 */
1498int drm_mode_getplane_res(struct drm_device *dev, void *data,
1499 struct drm_file *file_priv)
1500{
1501 struct drm_mode_get_plane_res *plane_resp = data;
1502 struct drm_mode_config *config;
1503 struct drm_plane *plane;
1504 uint32_t __user *plane_ptr;
1505 int copied = 0, ret = 0;
1506
1507 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1508 return -EINVAL;
1509
1510 mutex_lock(&dev->mode_config.mutex);
1511 config = &dev->mode_config;
1512
1513 /*
1514 * This ioctl is called twice, once to determine how much space is
1515 * needed, and the 2nd time to fill it.
1516 */
1517 if (config->num_plane &&
1518 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001519 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001520
1521 list_for_each_entry(plane, &config->plane_list, head) {
1522 if (put_user(plane->base.id, plane_ptr + copied)) {
1523 ret = -EFAULT;
1524 goto out;
1525 }
1526 copied++;
1527 }
1528 }
1529 plane_resp->count_planes = config->num_plane;
1530
1531out:
1532 mutex_unlock(&dev->mode_config.mutex);
1533 return ret;
1534}
1535
1536/**
1537 * drm_mode_getplane - get plane info
1538 * @dev: DRM device
1539 * @data: ioctl data
1540 * @file_priv: DRM file info
1541 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001542 * LOCKING:
1543 * Takes mode config lock.
1544 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001545 * Return plane info, including formats supported, gamma size, any
1546 * current fb, etc.
1547 */
1548int drm_mode_getplane(struct drm_device *dev, void *data,
1549 struct drm_file *file_priv)
1550{
1551 struct drm_mode_get_plane *plane_resp = data;
1552 struct drm_mode_object *obj;
1553 struct drm_plane *plane;
1554 uint32_t __user *format_ptr;
1555 int ret = 0;
1556
1557 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1558 return -EINVAL;
1559
1560 mutex_lock(&dev->mode_config.mutex);
1561 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1562 DRM_MODE_OBJECT_PLANE);
1563 if (!obj) {
1564 ret = -ENOENT;
1565 goto out;
1566 }
1567 plane = obj_to_plane(obj);
1568
1569 if (plane->crtc)
1570 plane_resp->crtc_id = plane->crtc->base.id;
1571 else
1572 plane_resp->crtc_id = 0;
1573
1574 if (plane->fb)
1575 plane_resp->fb_id = plane->fb->base.id;
1576 else
1577 plane_resp->fb_id = 0;
1578
1579 plane_resp->plane_id = plane->base.id;
1580 plane_resp->possible_crtcs = plane->possible_crtcs;
1581 plane_resp->gamma_size = plane->gamma_size;
1582
1583 /*
1584 * This ioctl is called twice, once to determine how much space is
1585 * needed, and the 2nd time to fill it.
1586 */
1587 if (plane->format_count &&
1588 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001589 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001590 if (copy_to_user(format_ptr,
1591 plane->format_types,
1592 sizeof(uint32_t) * plane->format_count)) {
1593 ret = -EFAULT;
1594 goto out;
1595 }
1596 }
1597 plane_resp->count_format_types = plane->format_count;
1598
1599out:
1600 mutex_unlock(&dev->mode_config.mutex);
1601 return ret;
1602}
1603
1604/**
1605 * drm_mode_setplane - set up or tear down an plane
1606 * @dev: DRM device
1607 * @data: ioctl data*
1608 * @file_prive: DRM file info
1609 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001610 * LOCKING:
1611 * Takes mode config lock.
1612 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001613 * Set plane info, including placement, fb, scaling, and other factors.
1614 * Or pass a NULL fb to disable.
1615 */
1616int drm_mode_setplane(struct drm_device *dev, void *data,
1617 struct drm_file *file_priv)
1618{
1619 struct drm_mode_set_plane *plane_req = data;
1620 struct drm_mode_object *obj;
1621 struct drm_plane *plane;
1622 struct drm_crtc *crtc;
1623 struct drm_framebuffer *fb;
1624 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001625 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001626 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001627
1628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1629 return -EINVAL;
1630
1631 mutex_lock(&dev->mode_config.mutex);
1632
1633 /*
1634 * First, find the plane, crtc, and fb objects. If not available,
1635 * we don't bother to call the driver.
1636 */
1637 obj = drm_mode_object_find(dev, plane_req->plane_id,
1638 DRM_MODE_OBJECT_PLANE);
1639 if (!obj) {
1640 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1641 plane_req->plane_id);
1642 ret = -ENOENT;
1643 goto out;
1644 }
1645 plane = obj_to_plane(obj);
1646
1647 /* No fb means shut it down */
1648 if (!plane_req->fb_id) {
1649 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001650 plane->crtc = NULL;
1651 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001652 goto out;
1653 }
1654
1655 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1656 DRM_MODE_OBJECT_CRTC);
1657 if (!obj) {
1658 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1659 plane_req->crtc_id);
1660 ret = -ENOENT;
1661 goto out;
1662 }
1663 crtc = obj_to_crtc(obj);
1664
1665 obj = drm_mode_object_find(dev, plane_req->fb_id,
1666 DRM_MODE_OBJECT_FB);
1667 if (!obj) {
1668 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1669 plane_req->fb_id);
1670 ret = -ENOENT;
1671 goto out;
1672 }
1673 fb = obj_to_fb(obj);
1674
Ville Syrjälä62443be2011-12-20 00:06:47 +02001675 /* Check whether this plane supports the fb pixel format. */
1676 for (i = 0; i < plane->format_count; i++)
1677 if (fb->pixel_format == plane->format_types[i])
1678 break;
1679 if (i == plane->format_count) {
1680 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1681 ret = -EINVAL;
1682 goto out;
1683 }
1684
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001685 fb_width = fb->width << 16;
1686 fb_height = fb->height << 16;
1687
1688 /* Make sure source coordinates are inside the fb. */
1689 if (plane_req->src_w > fb_width ||
1690 plane_req->src_x > fb_width - plane_req->src_w ||
1691 plane_req->src_h > fb_height ||
1692 plane_req->src_y > fb_height - plane_req->src_h) {
1693 DRM_DEBUG_KMS("Invalid source coordinates "
1694 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1695 plane_req->src_w >> 16,
1696 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1697 plane_req->src_h >> 16,
1698 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1699 plane_req->src_x >> 16,
1700 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1701 plane_req->src_y >> 16,
1702 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1703 ret = -ENOSPC;
1704 goto out;
1705 }
1706
Ville Syrjälä687a0402011-12-20 00:06:45 +02001707 /* Give drivers some help against integer overflows */
1708 if (plane_req->crtc_w > INT_MAX ||
1709 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1710 plane_req->crtc_h > INT_MAX ||
1711 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1712 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1713 plane_req->crtc_w, plane_req->crtc_h,
1714 plane_req->crtc_x, plane_req->crtc_y);
1715 ret = -ERANGE;
1716 goto out;
1717 }
1718
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001719 ret = plane->funcs->update_plane(plane, crtc, fb,
1720 plane_req->crtc_x, plane_req->crtc_y,
1721 plane_req->crtc_w, plane_req->crtc_h,
1722 plane_req->src_x, plane_req->src_y,
1723 plane_req->src_w, plane_req->src_h);
1724 if (!ret) {
1725 plane->crtc = crtc;
1726 plane->fb = fb;
1727 }
1728
1729out:
1730 mutex_unlock(&dev->mode_config.mutex);
1731
1732 return ret;
1733}
1734
1735/**
Dave Airlief453ba02008-11-07 14:05:41 -08001736 * drm_mode_setcrtc - set CRTC configuration
1737 * @inode: inode from the ioctl
1738 * @filp: file * from the ioctl
1739 * @cmd: cmd from ioctl
1740 * @arg: arg from ioctl
1741 *
1742 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001743 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001744 *
1745 * Build a new CRTC configuration based on user request.
1746 *
1747 * Called by the user via ioctl.
1748 *
1749 * RETURNS:
1750 * Zero on success, errno on failure.
1751 */
1752int drm_mode_setcrtc(struct drm_device *dev, void *data,
1753 struct drm_file *file_priv)
1754{
1755 struct drm_mode_config *config = &dev->mode_config;
1756 struct drm_mode_crtc *crtc_req = data;
1757 struct drm_mode_object *obj;
1758 struct drm_crtc *crtc, *crtcfb;
1759 struct drm_connector **connector_set = NULL, *connector;
1760 struct drm_framebuffer *fb = NULL;
1761 struct drm_display_mode *mode = NULL;
1762 struct drm_mode_set set;
1763 uint32_t __user *set_connectors_ptr;
1764 int ret = 0;
1765 int i;
1766
Dave Airliefb3b06c2011-02-08 13:55:21 +10001767 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1768 return -EINVAL;
1769
Dave Airlief453ba02008-11-07 14:05:41 -08001770 mutex_lock(&dev->mode_config.mutex);
1771 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1772 DRM_MODE_OBJECT_CRTC);
1773 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001774 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001775 ret = -EINVAL;
1776 goto out;
1777 }
1778 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001779 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001780
1781 if (crtc_req->mode_valid) {
1782 /* If we have a mode we need a framebuffer. */
1783 /* If we pass -1, set the mode with the currently bound fb */
1784 if (crtc_req->fb_id == -1) {
1785 list_for_each_entry(crtcfb,
1786 &dev->mode_config.crtc_list, head) {
1787 if (crtcfb == crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001788 DRM_DEBUG_KMS("Using current fb for "
1789 "setmode\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001790 fb = crtc->fb;
1791 }
1792 }
1793 } else {
1794 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1795 DRM_MODE_OBJECT_FB);
1796 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001797 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1798 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001799 ret = -EINVAL;
1800 goto out;
1801 }
1802 fb = obj_to_fb(obj);
1803 }
1804
1805 mode = drm_mode_create(dev);
1806 drm_crtc_convert_umode(mode, &crtc_req->mode);
1807 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1808 }
1809
1810 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001811 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001812 ret = -EINVAL;
1813 goto out;
1814 }
1815
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001816 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001817 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001818 crtc_req->count_connectors);
1819 ret = -EINVAL;
1820 goto out;
1821 }
1822
1823 if (crtc_req->count_connectors > 0) {
1824 u32 out_id;
1825
1826 /* Avoid unbounded kernel memory allocation */
1827 if (crtc_req->count_connectors > config->num_connector) {
1828 ret = -EINVAL;
1829 goto out;
1830 }
1831
1832 connector_set = kmalloc(crtc_req->count_connectors *
1833 sizeof(struct drm_connector *),
1834 GFP_KERNEL);
1835 if (!connector_set) {
1836 ret = -ENOMEM;
1837 goto out;
1838 }
1839
1840 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001841 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001842 if (get_user(out_id, &set_connectors_ptr[i])) {
1843 ret = -EFAULT;
1844 goto out;
1845 }
1846
1847 obj = drm_mode_object_find(dev, out_id,
1848 DRM_MODE_OBJECT_CONNECTOR);
1849 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001850 DRM_DEBUG_KMS("Connector id %d unknown\n",
1851 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001852 ret = -EINVAL;
1853 goto out;
1854 }
1855 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001856 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1857 connector->base.id,
1858 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001859
1860 connector_set[i] = connector;
1861 }
1862 }
1863
1864 set.crtc = crtc;
1865 set.x = crtc_req->x;
1866 set.y = crtc_req->y;
1867 set.mode = mode;
1868 set.connectors = connector_set;
1869 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001870 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001871 ret = crtc->funcs->set_config(&set);
1872
1873out:
1874 kfree(connector_set);
1875 mutex_unlock(&dev->mode_config.mutex);
1876 return ret;
1877}
1878
1879int drm_mode_cursor_ioctl(struct drm_device *dev,
1880 void *data, struct drm_file *file_priv)
1881{
1882 struct drm_mode_cursor *req = data;
1883 struct drm_mode_object *obj;
1884 struct drm_crtc *crtc;
1885 int ret = 0;
1886
Dave Airliefb3b06c2011-02-08 13:55:21 +10001887 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1888 return -EINVAL;
1889
Jesse Barnesacb4b992011-11-07 12:03:23 -08001890 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001891 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001892
1893 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001894 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001895 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001896 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001897 ret = -EINVAL;
1898 goto out;
1899 }
1900 crtc = obj_to_crtc(obj);
1901
1902 if (req->flags & DRM_MODE_CURSOR_BO) {
1903 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001904 ret = -ENXIO;
1905 goto out;
1906 }
1907 /* Turns off the cursor if handle is 0 */
1908 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1909 req->width, req->height);
1910 }
1911
1912 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1913 if (crtc->funcs->cursor_move) {
1914 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1915 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08001916 ret = -EFAULT;
1917 goto out;
1918 }
1919 }
1920out:
1921 mutex_unlock(&dev->mode_config.mutex);
1922 return ret;
1923}
1924
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001925/* Original addfb only supported RGB formats, so figure out which one */
1926uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1927{
1928 uint32_t fmt;
1929
1930 switch (bpp) {
1931 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001932 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001933 break;
1934 case 16:
1935 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001936 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001937 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001938 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001939 break;
1940 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001941 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001942 break;
1943 case 32:
1944 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001945 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001946 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001947 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001948 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001949 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001950 break;
1951 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001952 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1953 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001954 break;
1955 }
1956
1957 return fmt;
1958}
1959EXPORT_SYMBOL(drm_mode_legacy_fb_format);
1960
Dave Airlief453ba02008-11-07 14:05:41 -08001961/**
1962 * drm_mode_addfb - add an FB to the graphics configuration
1963 * @inode: inode from the ioctl
1964 * @filp: file * from the ioctl
1965 * @cmd: cmd from ioctl
1966 * @arg: arg from ioctl
1967 *
1968 * LOCKING:
1969 * Takes mode config lock.
1970 *
1971 * Add a new FB to the specified CRTC, given a user request.
1972 *
1973 * Called by the user via ioctl.
1974 *
1975 * RETURNS:
1976 * Zero on success, errno on failure.
1977 */
1978int drm_mode_addfb(struct drm_device *dev,
1979 void *data, struct drm_file *file_priv)
1980{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001981 struct drm_mode_fb_cmd *or = data;
1982 struct drm_mode_fb_cmd2 r = {};
1983 struct drm_mode_config *config = &dev->mode_config;
1984 struct drm_framebuffer *fb;
1985 int ret = 0;
1986
1987 /* Use new struct with format internally */
1988 r.fb_id = or->fb_id;
1989 r.width = or->width;
1990 r.height = or->height;
1991 r.pitches[0] = or->pitch;
1992 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
1993 r.handles[0] = or->handle;
1994
1995 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1996 return -EINVAL;
1997
Jesse Barnesacb4b992011-11-07 12:03:23 -08001998 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001999 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002000
2001 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002002 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002003
2004 mutex_lock(&dev->mode_config.mutex);
2005
2006 /* TODO check buffer is sufficiently large */
2007 /* TODO setup destructor callback */
2008
2009 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2010 if (IS_ERR(fb)) {
2011 DRM_ERROR("could not create framebuffer\n");
2012 ret = PTR_ERR(fb);
2013 goto out;
2014 }
2015
2016 or->fb_id = fb->base.id;
2017 list_add(&fb->filp_head, &file_priv->fbs);
2018 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2019
2020out:
2021 mutex_unlock(&dev->mode_config.mutex);
2022 return ret;
2023}
2024
Ville Syrjälä935b5972011-12-20 00:06:48 +02002025static int format_check(struct drm_mode_fb_cmd2 *r)
2026{
2027 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2028
2029 switch (format) {
2030 case DRM_FORMAT_C8:
2031 case DRM_FORMAT_RGB332:
2032 case DRM_FORMAT_BGR233:
2033 case DRM_FORMAT_XRGB4444:
2034 case DRM_FORMAT_XBGR4444:
2035 case DRM_FORMAT_RGBX4444:
2036 case DRM_FORMAT_BGRX4444:
2037 case DRM_FORMAT_ARGB4444:
2038 case DRM_FORMAT_ABGR4444:
2039 case DRM_FORMAT_RGBA4444:
2040 case DRM_FORMAT_BGRA4444:
2041 case DRM_FORMAT_XRGB1555:
2042 case DRM_FORMAT_XBGR1555:
2043 case DRM_FORMAT_RGBX5551:
2044 case DRM_FORMAT_BGRX5551:
2045 case DRM_FORMAT_ARGB1555:
2046 case DRM_FORMAT_ABGR1555:
2047 case DRM_FORMAT_RGBA5551:
2048 case DRM_FORMAT_BGRA5551:
2049 case DRM_FORMAT_RGB565:
2050 case DRM_FORMAT_BGR565:
2051 case DRM_FORMAT_RGB888:
2052 case DRM_FORMAT_BGR888:
2053 case DRM_FORMAT_XRGB8888:
2054 case DRM_FORMAT_XBGR8888:
2055 case DRM_FORMAT_RGBX8888:
2056 case DRM_FORMAT_BGRX8888:
2057 case DRM_FORMAT_ARGB8888:
2058 case DRM_FORMAT_ABGR8888:
2059 case DRM_FORMAT_RGBA8888:
2060 case DRM_FORMAT_BGRA8888:
2061 case DRM_FORMAT_XRGB2101010:
2062 case DRM_FORMAT_XBGR2101010:
2063 case DRM_FORMAT_RGBX1010102:
2064 case DRM_FORMAT_BGRX1010102:
2065 case DRM_FORMAT_ARGB2101010:
2066 case DRM_FORMAT_ABGR2101010:
2067 case DRM_FORMAT_RGBA1010102:
2068 case DRM_FORMAT_BGRA1010102:
2069 case DRM_FORMAT_YUYV:
2070 case DRM_FORMAT_YVYU:
2071 case DRM_FORMAT_UYVY:
2072 case DRM_FORMAT_VYUY:
2073 case DRM_FORMAT_AYUV:
2074 case DRM_FORMAT_NV12:
2075 case DRM_FORMAT_NV21:
2076 case DRM_FORMAT_NV16:
2077 case DRM_FORMAT_NV61:
2078 case DRM_FORMAT_YUV410:
2079 case DRM_FORMAT_YVU410:
2080 case DRM_FORMAT_YUV411:
2081 case DRM_FORMAT_YVU411:
2082 case DRM_FORMAT_YUV420:
2083 case DRM_FORMAT_YVU420:
2084 case DRM_FORMAT_YUV422:
2085 case DRM_FORMAT_YVU422:
2086 case DRM_FORMAT_YUV444:
2087 case DRM_FORMAT_YVU444:
2088 return 0;
2089 default:
2090 return -EINVAL;
2091 }
2092}
2093
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002094/**
2095 * drm_mode_addfb2 - add an FB to the graphics configuration
2096 * @inode: inode from the ioctl
2097 * @filp: file * from the ioctl
2098 * @cmd: cmd from ioctl
2099 * @arg: arg from ioctl
2100 *
2101 * LOCKING:
2102 * Takes mode config lock.
2103 *
2104 * Add a new FB to the specified CRTC, given a user request with format.
2105 *
2106 * Called by the user via ioctl.
2107 *
2108 * RETURNS:
2109 * Zero on success, errno on failure.
2110 */
2111int drm_mode_addfb2(struct drm_device *dev,
2112 void *data, struct drm_file *file_priv)
2113{
2114 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002115 struct drm_mode_config *config = &dev->mode_config;
2116 struct drm_framebuffer *fb;
2117 int ret = 0;
2118
Dave Airliefb3b06c2011-02-08 13:55:21 +10002119 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2120 return -EINVAL;
2121
Dave Airlief453ba02008-11-07 14:05:41 -08002122 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002123 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2124 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002125 return -EINVAL;
2126 }
2127 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002128 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2129 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002130 return -EINVAL;
2131 }
2132
Ville Syrjälä935b5972011-12-20 00:06:48 +02002133 ret = format_check(r);
2134 if (ret) {
2135 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2136 return ret;
2137 }
2138
Dave Airlief453ba02008-11-07 14:05:41 -08002139 mutex_lock(&dev->mode_config.mutex);
2140
Dave Airlief453ba02008-11-07 14:05:41 -08002141 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002142 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002143 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002144 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002145 goto out;
2146 }
2147
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002148 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002149 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002150 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002151
2152out:
2153 mutex_unlock(&dev->mode_config.mutex);
2154 return ret;
2155}
2156
2157/**
2158 * drm_mode_rmfb - remove an FB from the configuration
2159 * @inode: inode from the ioctl
2160 * @filp: file * from the ioctl
2161 * @cmd: cmd from ioctl
2162 * @arg: arg from ioctl
2163 *
2164 * LOCKING:
2165 * Takes mode config lock.
2166 *
2167 * Remove the FB specified by the user.
2168 *
2169 * Called by the user via ioctl.
2170 *
2171 * RETURNS:
2172 * Zero on success, errno on failure.
2173 */
2174int drm_mode_rmfb(struct drm_device *dev,
2175 void *data, struct drm_file *file_priv)
2176{
2177 struct drm_mode_object *obj;
2178 struct drm_framebuffer *fb = NULL;
2179 struct drm_framebuffer *fbl = NULL;
2180 uint32_t *id = data;
2181 int ret = 0;
2182 int found = 0;
2183
Dave Airliefb3b06c2011-02-08 13:55:21 +10002184 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2185 return -EINVAL;
2186
Dave Airlief453ba02008-11-07 14:05:41 -08002187 mutex_lock(&dev->mode_config.mutex);
2188 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002189 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002190 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002191 ret = -EINVAL;
2192 goto out;
2193 }
2194 fb = obj_to_fb(obj);
2195
2196 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2197 if (fb == fbl)
2198 found = 1;
2199
2200 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002201 ret = -EINVAL;
2202 goto out;
2203 }
2204
2205 /* TODO release all crtc connected to the framebuffer */
2206 /* TODO unhock the destructor from the buffer object */
2207
2208 list_del(&fb->filp_head);
2209 fb->funcs->destroy(fb);
2210
2211out:
2212 mutex_unlock(&dev->mode_config.mutex);
2213 return ret;
2214}
2215
2216/**
2217 * drm_mode_getfb - get FB info
2218 * @inode: inode from the ioctl
2219 * @filp: file * from the ioctl
2220 * @cmd: cmd from ioctl
2221 * @arg: arg from ioctl
2222 *
2223 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002224 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002225 *
2226 * Lookup the FB given its ID and return info about it.
2227 *
2228 * Called by the user via ioctl.
2229 *
2230 * RETURNS:
2231 * Zero on success, errno on failure.
2232 */
2233int drm_mode_getfb(struct drm_device *dev,
2234 void *data, struct drm_file *file_priv)
2235{
2236 struct drm_mode_fb_cmd *r = data;
2237 struct drm_mode_object *obj;
2238 struct drm_framebuffer *fb;
2239 int ret = 0;
2240
Dave Airliefb3b06c2011-02-08 13:55:21 +10002241 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2242 return -EINVAL;
2243
Dave Airlief453ba02008-11-07 14:05:41 -08002244 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002245 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002246 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002247 ret = -EINVAL;
2248 goto out;
2249 }
2250 fb = obj_to_fb(obj);
2251
2252 r->height = fb->height;
2253 r->width = fb->width;
2254 r->depth = fb->depth;
2255 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002256 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002257 fb->funcs->create_handle(fb, file_priv, &r->handle);
2258
2259out:
2260 mutex_unlock(&dev->mode_config.mutex);
2261 return ret;
2262}
2263
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002264int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2265 void *data, struct drm_file *file_priv)
2266{
2267 struct drm_clip_rect __user *clips_ptr;
2268 struct drm_clip_rect *clips = NULL;
2269 struct drm_mode_fb_dirty_cmd *r = data;
2270 struct drm_mode_object *obj;
2271 struct drm_framebuffer *fb;
2272 unsigned flags;
2273 int num_clips;
2274 int ret = 0;
2275
Dave Airliefb3b06c2011-02-08 13:55:21 +10002276 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2277 return -EINVAL;
2278
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002279 mutex_lock(&dev->mode_config.mutex);
2280 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2281 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002282 ret = -EINVAL;
2283 goto out_err1;
2284 }
2285 fb = obj_to_fb(obj);
2286
2287 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002288 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002289
2290 if (!num_clips != !clips_ptr) {
2291 ret = -EINVAL;
2292 goto out_err1;
2293 }
2294
2295 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2296
2297 /* If userspace annotates copy, clips must come in pairs */
2298 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2299 ret = -EINVAL;
2300 goto out_err1;
2301 }
2302
2303 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002304 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2305 ret = -EINVAL;
2306 goto out_err1;
2307 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002308 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2309 if (!clips) {
2310 ret = -ENOMEM;
2311 goto out_err1;
2312 }
2313
2314 ret = copy_from_user(clips, clips_ptr,
2315 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002316 if (ret) {
2317 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002318 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002319 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002320 }
2321
2322 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002323 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2324 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002325 } else {
2326 ret = -ENOSYS;
2327 goto out_err2;
2328 }
2329
2330out_err2:
2331 kfree(clips);
2332out_err1:
2333 mutex_unlock(&dev->mode_config.mutex);
2334 return ret;
2335}
2336
2337
Dave Airlief453ba02008-11-07 14:05:41 -08002338/**
2339 * drm_fb_release - remove and free the FBs on this file
2340 * @filp: file * from the ioctl
2341 *
2342 * LOCKING:
2343 * Takes mode config lock.
2344 *
2345 * Destroy all the FBs associated with @filp.
2346 *
2347 * Called by the user via ioctl.
2348 *
2349 * RETURNS:
2350 * Zero on success, errno on failure.
2351 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002352void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002353{
Dave Airlief453ba02008-11-07 14:05:41 -08002354 struct drm_device *dev = priv->minor->dev;
2355 struct drm_framebuffer *fb, *tfb;
2356
2357 mutex_lock(&dev->mode_config.mutex);
2358 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2359 list_del(&fb->filp_head);
2360 fb->funcs->destroy(fb);
2361 }
2362 mutex_unlock(&dev->mode_config.mutex);
2363}
2364
2365/**
2366 * drm_mode_attachmode - add a mode to the user mode list
2367 * @dev: DRM device
2368 * @connector: connector to add the mode to
2369 * @mode: mode to add
2370 *
2371 * Add @mode to @connector's user mode list.
2372 */
2373static int drm_mode_attachmode(struct drm_device *dev,
2374 struct drm_connector *connector,
2375 struct drm_display_mode *mode)
2376{
2377 int ret = 0;
2378
2379 list_add_tail(&mode->head, &connector->user_modes);
2380 return ret;
2381}
2382
2383int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2384 struct drm_display_mode *mode)
2385{
2386 struct drm_connector *connector;
2387 int ret = 0;
2388 struct drm_display_mode *dup_mode;
2389 int need_dup = 0;
2390 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2391 if (!connector->encoder)
2392 break;
2393 if (connector->encoder->crtc == crtc) {
2394 if (need_dup)
2395 dup_mode = drm_mode_duplicate(dev, mode);
2396 else
2397 dup_mode = mode;
2398 ret = drm_mode_attachmode(dev, connector, dup_mode);
2399 if (ret)
2400 return ret;
2401 need_dup = 1;
2402 }
2403 }
2404 return 0;
2405}
2406EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2407
2408static int drm_mode_detachmode(struct drm_device *dev,
2409 struct drm_connector *connector,
2410 struct drm_display_mode *mode)
2411{
2412 int found = 0;
2413 int ret = 0;
2414 struct drm_display_mode *match_mode, *t;
2415
2416 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2417 if (drm_mode_equal(match_mode, mode)) {
2418 list_del(&match_mode->head);
2419 drm_mode_destroy(dev, match_mode);
2420 found = 1;
2421 break;
2422 }
2423 }
2424
2425 if (!found)
2426 ret = -EINVAL;
2427
2428 return ret;
2429}
2430
2431int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2432{
2433 struct drm_connector *connector;
2434
2435 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2436 drm_mode_detachmode(dev, connector, mode);
2437 }
2438 return 0;
2439}
2440EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2441
2442/**
2443 * drm_fb_attachmode - Attach a user mode to an connector
2444 * @inode: inode from the ioctl
2445 * @filp: file * from the ioctl
2446 * @cmd: cmd from ioctl
2447 * @arg: arg from ioctl
2448 *
2449 * This attaches a user specified mode to an connector.
2450 * Called by the user via ioctl.
2451 *
2452 * RETURNS:
2453 * Zero on success, errno on failure.
2454 */
2455int drm_mode_attachmode_ioctl(struct drm_device *dev,
2456 void *data, struct drm_file *file_priv)
2457{
2458 struct drm_mode_mode_cmd *mode_cmd = data;
2459 struct drm_connector *connector;
2460 struct drm_display_mode *mode;
2461 struct drm_mode_object *obj;
2462 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2463 int ret = 0;
2464
Dave Airliefb3b06c2011-02-08 13:55:21 +10002465 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2466 return -EINVAL;
2467
Dave Airlief453ba02008-11-07 14:05:41 -08002468 mutex_lock(&dev->mode_config.mutex);
2469
2470 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2471 if (!obj) {
2472 ret = -EINVAL;
2473 goto out;
2474 }
2475 connector = obj_to_connector(obj);
2476
2477 mode = drm_mode_create(dev);
2478 if (!mode) {
2479 ret = -ENOMEM;
2480 goto out;
2481 }
2482
2483 drm_crtc_convert_umode(mode, umode);
2484
2485 ret = drm_mode_attachmode(dev, connector, mode);
2486out:
2487 mutex_unlock(&dev->mode_config.mutex);
2488 return ret;
2489}
2490
2491
2492/**
2493 * drm_fb_detachmode - Detach a user specified mode from an connector
2494 * @inode: inode from the ioctl
2495 * @filp: file * from the ioctl
2496 * @cmd: cmd from ioctl
2497 * @arg: arg from ioctl
2498 *
2499 * Called by the user via ioctl.
2500 *
2501 * RETURNS:
2502 * Zero on success, errno on failure.
2503 */
2504int drm_mode_detachmode_ioctl(struct drm_device *dev,
2505 void *data, struct drm_file *file_priv)
2506{
2507 struct drm_mode_object *obj;
2508 struct drm_mode_mode_cmd *mode_cmd = data;
2509 struct drm_connector *connector;
2510 struct drm_display_mode mode;
2511 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2512 int ret = 0;
2513
Dave Airliefb3b06c2011-02-08 13:55:21 +10002514 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2515 return -EINVAL;
2516
Dave Airlief453ba02008-11-07 14:05:41 -08002517 mutex_lock(&dev->mode_config.mutex);
2518
2519 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2520 if (!obj) {
2521 ret = -EINVAL;
2522 goto out;
2523 }
2524 connector = obj_to_connector(obj);
2525
2526 drm_crtc_convert_umode(&mode, umode);
2527 ret = drm_mode_detachmode(dev, connector, &mode);
2528out:
2529 mutex_unlock(&dev->mode_config.mutex);
2530 return ret;
2531}
2532
2533struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2534 const char *name, int num_values)
2535{
2536 struct drm_property *property = NULL;
2537
2538 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2539 if (!property)
2540 return NULL;
2541
2542 if (num_values) {
2543 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2544 if (!property->values)
2545 goto fail;
2546 }
2547
2548 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2549 property->flags = flags;
2550 property->num_values = num_values;
2551 INIT_LIST_HEAD(&property->enum_blob_list);
2552
Vinson Lee471dd2e2011-11-10 11:55:40 -08002553 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002554 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002555 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2556 }
Dave Airlief453ba02008-11-07 14:05:41 -08002557
2558 list_add_tail(&property->head, &dev->mode_config.property_list);
2559 return property;
2560fail:
2561 kfree(property);
2562 return NULL;
2563}
2564EXPORT_SYMBOL(drm_property_create);
2565
Sascha Hauer4a67d392012-02-06 10:58:17 +01002566struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2567 const char *name,
2568 const struct drm_prop_enum_list *props,
2569 int num_values)
2570{
2571 struct drm_property *property;
2572 int i, ret;
2573
2574 flags |= DRM_MODE_PROP_ENUM;
2575
2576 property = drm_property_create(dev, flags, name, num_values);
2577 if (!property)
2578 return NULL;
2579
2580 for (i = 0; i < num_values; i++) {
2581 ret = drm_property_add_enum(property, i,
2582 props[i].type,
2583 props[i].name);
2584 if (ret) {
2585 drm_property_destroy(dev, property);
2586 return NULL;
2587 }
2588 }
2589
2590 return property;
2591}
2592EXPORT_SYMBOL(drm_property_create_enum);
2593
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002594struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2595 const char *name,
2596 uint64_t min, uint64_t max)
2597{
2598 struct drm_property *property;
2599
2600 flags |= DRM_MODE_PROP_RANGE;
2601
2602 property = drm_property_create(dev, flags, name, 2);
2603 if (!property)
2604 return NULL;
2605
2606 property->values[0] = min;
2607 property->values[1] = max;
2608
2609 return property;
2610}
2611EXPORT_SYMBOL(drm_property_create_range);
2612
Dave Airlief453ba02008-11-07 14:05:41 -08002613int drm_property_add_enum(struct drm_property *property, int index,
2614 uint64_t value, const char *name)
2615{
2616 struct drm_property_enum *prop_enum;
2617
2618 if (!(property->flags & DRM_MODE_PROP_ENUM))
2619 return -EINVAL;
2620
2621 if (!list_empty(&property->enum_blob_list)) {
2622 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2623 if (prop_enum->value == value) {
2624 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2625 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2626 return 0;
2627 }
2628 }
2629 }
2630
2631 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2632 if (!prop_enum)
2633 return -ENOMEM;
2634
2635 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2636 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2637 prop_enum->value = value;
2638
2639 property->values[index] = value;
2640 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2641 return 0;
2642}
2643EXPORT_SYMBOL(drm_property_add_enum);
2644
2645void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2646{
2647 struct drm_property_enum *prop_enum, *pt;
2648
2649 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2650 list_del(&prop_enum->head);
2651 kfree(prop_enum);
2652 }
2653
2654 if (property->num_values)
2655 kfree(property->values);
2656 drm_mode_object_put(dev, &property->base);
2657 list_del(&property->head);
2658 kfree(property);
2659}
2660EXPORT_SYMBOL(drm_property_destroy);
2661
2662int drm_connector_attach_property(struct drm_connector *connector,
2663 struct drm_property *property, uint64_t init_val)
2664{
2665 int i;
2666
2667 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2668 if (connector->property_ids[i] == 0) {
2669 connector->property_ids[i] = property->base.id;
2670 connector->property_values[i] = init_val;
2671 break;
2672 }
2673 }
2674
2675 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2676 return -EINVAL;
2677 return 0;
2678}
2679EXPORT_SYMBOL(drm_connector_attach_property);
2680
2681int drm_connector_property_set_value(struct drm_connector *connector,
2682 struct drm_property *property, uint64_t value)
2683{
2684 int i;
2685
2686 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2687 if (connector->property_ids[i] == property->base.id) {
2688 connector->property_values[i] = value;
2689 break;
2690 }
2691 }
2692
2693 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2694 return -EINVAL;
2695 return 0;
2696}
2697EXPORT_SYMBOL(drm_connector_property_set_value);
2698
2699int drm_connector_property_get_value(struct drm_connector *connector,
2700 struct drm_property *property, uint64_t *val)
2701{
2702 int i;
2703
2704 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2705 if (connector->property_ids[i] == property->base.id) {
2706 *val = connector->property_values[i];
2707 break;
2708 }
2709 }
2710
2711 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2712 return -EINVAL;
2713 return 0;
2714}
2715EXPORT_SYMBOL(drm_connector_property_get_value);
2716
2717int drm_mode_getproperty_ioctl(struct drm_device *dev,
2718 void *data, struct drm_file *file_priv)
2719{
2720 struct drm_mode_object *obj;
2721 struct drm_mode_get_property *out_resp = data;
2722 struct drm_property *property;
2723 int enum_count = 0;
2724 int blob_count = 0;
2725 int value_count = 0;
2726 int ret = 0, i;
2727 int copied;
2728 struct drm_property_enum *prop_enum;
2729 struct drm_mode_property_enum __user *enum_ptr;
2730 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002731 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002732 uint64_t __user *values_ptr;
2733 uint32_t __user *blob_length_ptr;
2734
Dave Airliefb3b06c2011-02-08 13:55:21 +10002735 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2736 return -EINVAL;
2737
Dave Airlief453ba02008-11-07 14:05:41 -08002738 mutex_lock(&dev->mode_config.mutex);
2739 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2740 if (!obj) {
2741 ret = -EINVAL;
2742 goto done;
2743 }
2744 property = obj_to_property(obj);
2745
2746 if (property->flags & DRM_MODE_PROP_ENUM) {
2747 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2748 enum_count++;
2749 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2750 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2751 blob_count++;
2752 }
2753
2754 value_count = property->num_values;
2755
2756 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2757 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2758 out_resp->flags = property->flags;
2759
2760 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002761 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002762 for (i = 0; i < value_count; i++) {
2763 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2764 ret = -EFAULT;
2765 goto done;
2766 }
2767 }
2768 }
2769 out_resp->count_values = value_count;
2770
2771 if (property->flags & DRM_MODE_PROP_ENUM) {
2772 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2773 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002774 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002775 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2776
2777 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2778 ret = -EFAULT;
2779 goto done;
2780 }
2781
2782 if (copy_to_user(&enum_ptr[copied].name,
2783 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2784 ret = -EFAULT;
2785 goto done;
2786 }
2787 copied++;
2788 }
2789 }
2790 out_resp->count_enum_blobs = enum_count;
2791 }
2792
2793 if (property->flags & DRM_MODE_PROP_BLOB) {
2794 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2795 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002796 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2797 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002798
2799 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2800 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2801 ret = -EFAULT;
2802 goto done;
2803 }
2804
2805 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2806 ret = -EFAULT;
2807 goto done;
2808 }
2809
2810 copied++;
2811 }
2812 }
2813 out_resp->count_enum_blobs = blob_count;
2814 }
2815done:
2816 mutex_unlock(&dev->mode_config.mutex);
2817 return ret;
2818}
2819
2820static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2821 void *data)
2822{
2823 struct drm_property_blob *blob;
2824
2825 if (!length || !data)
2826 return NULL;
2827
2828 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2829 if (!blob)
2830 return NULL;
2831
2832 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2833 blob->length = length;
2834
2835 memcpy(blob->data, data, length);
2836
2837 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2838
2839 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2840 return blob;
2841}
2842
2843static void drm_property_destroy_blob(struct drm_device *dev,
2844 struct drm_property_blob *blob)
2845{
2846 drm_mode_object_put(dev, &blob->base);
2847 list_del(&blob->head);
2848 kfree(blob);
2849}
2850
2851int drm_mode_getblob_ioctl(struct drm_device *dev,
2852 void *data, struct drm_file *file_priv)
2853{
2854 struct drm_mode_object *obj;
2855 struct drm_mode_get_blob *out_resp = data;
2856 struct drm_property_blob *blob;
2857 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002858 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002859
Dave Airliefb3b06c2011-02-08 13:55:21 +10002860 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2861 return -EINVAL;
2862
Dave Airlief453ba02008-11-07 14:05:41 -08002863 mutex_lock(&dev->mode_config.mutex);
2864 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2865 if (!obj) {
2866 ret = -EINVAL;
2867 goto done;
2868 }
2869 blob = obj_to_blob(obj);
2870
2871 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002872 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08002873 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2874 ret = -EFAULT;
2875 goto done;
2876 }
2877 }
2878 out_resp->length = blob->length;
2879
2880done:
2881 mutex_unlock(&dev->mode_config.mutex);
2882 return ret;
2883}
2884
2885int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2886 struct edid *edid)
2887{
2888 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00002889 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08002890
2891 if (connector->edid_blob_ptr)
2892 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2893
2894 /* Delete edid, when there is none. */
2895 if (!edid) {
2896 connector->edid_blob_ptr = NULL;
2897 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2898 return ret;
2899 }
2900
Adam Jackson7466f4c2010-03-29 21:43:23 +00002901 size = EDID_LENGTH * (1 + edid->extensions);
2902 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2903 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08002904
2905 ret = drm_connector_property_set_value(connector,
2906 dev->mode_config.edid_property,
2907 connector->edid_blob_ptr->base.id);
2908
2909 return ret;
2910}
2911EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2912
2913int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2914 void *data, struct drm_file *file_priv)
2915{
2916 struct drm_mode_connector_set_property *out_resp = data;
2917 struct drm_mode_object *obj;
2918 struct drm_property *property;
2919 struct drm_connector *connector;
2920 int ret = -EINVAL;
2921 int i;
2922
Dave Airliefb3b06c2011-02-08 13:55:21 +10002923 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2924 return -EINVAL;
2925
Dave Airlief453ba02008-11-07 14:05:41 -08002926 mutex_lock(&dev->mode_config.mutex);
2927
2928 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2929 if (!obj) {
2930 goto out;
2931 }
2932 connector = obj_to_connector(obj);
2933
2934 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2935 if (connector->property_ids[i] == out_resp->prop_id)
2936 break;
2937 }
2938
2939 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2940 goto out;
2941 }
2942
2943 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2944 if (!obj) {
2945 goto out;
2946 }
2947 property = obj_to_property(obj);
2948
2949 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2950 goto out;
2951
2952 if (property->flags & DRM_MODE_PROP_RANGE) {
2953 if (out_resp->value < property->values[0])
2954 goto out;
2955
2956 if (out_resp->value > property->values[1])
2957 goto out;
2958 } else {
2959 int found = 0;
2960 for (i = 0; i < property->num_values; i++) {
2961 if (property->values[i] == out_resp->value) {
2962 found = 1;
2963 break;
2964 }
2965 }
2966 if (!found) {
2967 goto out;
2968 }
2969 }
2970
Keith Packardc9fb15f2009-05-30 20:42:28 -07002971 /* Do DPMS ourselves */
2972 if (property == connector->dev->mode_config.dpms_property) {
2973 if (connector->funcs->dpms)
2974 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2975 ret = 0;
2976 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08002977 ret = connector->funcs->set_property(connector, property, out_resp->value);
2978
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002979 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08002980 if (!ret)
2981 drm_connector_property_set_value(connector, property, out_resp->value);
2982out:
2983 mutex_unlock(&dev->mode_config.mutex);
2984 return ret;
2985}
2986
Dave Airlief453ba02008-11-07 14:05:41 -08002987int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2988 struct drm_encoder *encoder)
2989{
2990 int i;
2991
2992 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2993 if (connector->encoder_ids[i] == 0) {
2994 connector->encoder_ids[i] = encoder->base.id;
2995 return 0;
2996 }
2997 }
2998 return -ENOMEM;
2999}
3000EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3001
3002void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3003 struct drm_encoder *encoder)
3004{
3005 int i;
3006 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3007 if (connector->encoder_ids[i] == encoder->base.id) {
3008 connector->encoder_ids[i] = 0;
3009 if (connector->encoder == encoder)
3010 connector->encoder = NULL;
3011 break;
3012 }
3013 }
3014}
3015EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3016
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003017int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003018 int gamma_size)
3019{
3020 crtc->gamma_size = gamma_size;
3021
3022 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3023 if (!crtc->gamma_store) {
3024 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003025 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003026 }
3027
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003028 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003029}
3030EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3031
3032int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3033 void *data, struct drm_file *file_priv)
3034{
3035 struct drm_mode_crtc_lut *crtc_lut = data;
3036 struct drm_mode_object *obj;
3037 struct drm_crtc *crtc;
3038 void *r_base, *g_base, *b_base;
3039 int size;
3040 int ret = 0;
3041
Dave Airliefb3b06c2011-02-08 13:55:21 +10003042 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3043 return -EINVAL;
3044
Dave Airlief453ba02008-11-07 14:05:41 -08003045 mutex_lock(&dev->mode_config.mutex);
3046 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3047 if (!obj) {
3048 ret = -EINVAL;
3049 goto out;
3050 }
3051 crtc = obj_to_crtc(obj);
3052
3053 /* memcpy into gamma store */
3054 if (crtc_lut->gamma_size != crtc->gamma_size) {
3055 ret = -EINVAL;
3056 goto out;
3057 }
3058
3059 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3060 r_base = crtc->gamma_store;
3061 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3062 ret = -EFAULT;
3063 goto out;
3064 }
3065
3066 g_base = r_base + size;
3067 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3068 ret = -EFAULT;
3069 goto out;
3070 }
3071
3072 b_base = g_base + size;
3073 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3074 ret = -EFAULT;
3075 goto out;
3076 }
3077
James Simmons72034252010-08-03 01:33:19 +01003078 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003079
3080out:
3081 mutex_unlock(&dev->mode_config.mutex);
3082 return ret;
3083
3084}
3085
3086int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3087 void *data, struct drm_file *file_priv)
3088{
3089 struct drm_mode_crtc_lut *crtc_lut = data;
3090 struct drm_mode_object *obj;
3091 struct drm_crtc *crtc;
3092 void *r_base, *g_base, *b_base;
3093 int size;
3094 int ret = 0;
3095
Dave Airliefb3b06c2011-02-08 13:55:21 +10003096 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3097 return -EINVAL;
3098
Dave Airlief453ba02008-11-07 14:05:41 -08003099 mutex_lock(&dev->mode_config.mutex);
3100 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3101 if (!obj) {
3102 ret = -EINVAL;
3103 goto out;
3104 }
3105 crtc = obj_to_crtc(obj);
3106
3107 /* memcpy into gamma store */
3108 if (crtc_lut->gamma_size != crtc->gamma_size) {
3109 ret = -EINVAL;
3110 goto out;
3111 }
3112
3113 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3114 r_base = crtc->gamma_store;
3115 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3116 ret = -EFAULT;
3117 goto out;
3118 }
3119
3120 g_base = r_base + size;
3121 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3122 ret = -EFAULT;
3123 goto out;
3124 }
3125
3126 b_base = g_base + size;
3127 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3128 ret = -EFAULT;
3129 goto out;
3130 }
3131out:
3132 mutex_unlock(&dev->mode_config.mutex);
3133 return ret;
3134}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003135
3136int drm_mode_page_flip_ioctl(struct drm_device *dev,
3137 void *data, struct drm_file *file_priv)
3138{
3139 struct drm_mode_crtc_page_flip *page_flip = data;
3140 struct drm_mode_object *obj;
3141 struct drm_crtc *crtc;
3142 struct drm_framebuffer *fb;
3143 struct drm_pending_vblank_event *e = NULL;
3144 unsigned long flags;
3145 int ret = -EINVAL;
3146
3147 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3148 page_flip->reserved != 0)
3149 return -EINVAL;
3150
3151 mutex_lock(&dev->mode_config.mutex);
3152 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3153 if (!obj)
3154 goto out;
3155 crtc = obj_to_crtc(obj);
3156
Chris Wilson90c1efd2010-07-17 20:23:26 +01003157 if (crtc->fb == NULL) {
3158 /* The framebuffer is currently unbound, presumably
3159 * due to a hotplug event, that userspace has not
3160 * yet discovered.
3161 */
3162 ret = -EBUSY;
3163 goto out;
3164 }
3165
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003166 if (crtc->funcs->page_flip == NULL)
3167 goto out;
3168
3169 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3170 if (!obj)
3171 goto out;
3172 fb = obj_to_fb(obj);
3173
3174 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3175 ret = -ENOMEM;
3176 spin_lock_irqsave(&dev->event_lock, flags);
3177 if (file_priv->event_space < sizeof e->event) {
3178 spin_unlock_irqrestore(&dev->event_lock, flags);
3179 goto out;
3180 }
3181 file_priv->event_space -= sizeof e->event;
3182 spin_unlock_irqrestore(&dev->event_lock, flags);
3183
3184 e = kzalloc(sizeof *e, GFP_KERNEL);
3185 if (e == NULL) {
3186 spin_lock_irqsave(&dev->event_lock, flags);
3187 file_priv->event_space += sizeof e->event;
3188 spin_unlock_irqrestore(&dev->event_lock, flags);
3189 goto out;
3190 }
3191
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003192 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003193 e->event.base.length = sizeof e->event;
3194 e->event.user_data = page_flip->user_data;
3195 e->base.event = &e->event.base;
3196 e->base.file_priv = file_priv;
3197 e->base.destroy =
3198 (void (*) (struct drm_pending_event *)) kfree;
3199 }
3200
3201 ret = crtc->funcs->page_flip(crtc, fb, e);
3202 if (ret) {
3203 spin_lock_irqsave(&dev->event_lock, flags);
3204 file_priv->event_space += sizeof e->event;
3205 spin_unlock_irqrestore(&dev->event_lock, flags);
3206 kfree(e);
3207 }
3208
3209out:
3210 mutex_unlock(&dev->mode_config.mutex);
3211 return ret;
3212}
Chris Wilsoneb033552011-01-24 15:11:08 +00003213
3214void drm_mode_config_reset(struct drm_device *dev)
3215{
3216 struct drm_crtc *crtc;
3217 struct drm_encoder *encoder;
3218 struct drm_connector *connector;
3219
3220 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3221 if (crtc->funcs->reset)
3222 crtc->funcs->reset(crtc);
3223
3224 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3225 if (encoder->funcs->reset)
3226 encoder->funcs->reset(encoder);
3227
3228 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3229 if (connector->funcs->reset)
3230 connector->funcs->reset(connector);
3231}
3232EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003233
3234int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3235 void *data, struct drm_file *file_priv)
3236{
3237 struct drm_mode_create_dumb *args = data;
3238
3239 if (!dev->driver->dumb_create)
3240 return -ENOSYS;
3241 return dev->driver->dumb_create(file_priv, dev, args);
3242}
3243
3244int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3245 void *data, struct drm_file *file_priv)
3246{
3247 struct drm_mode_map_dumb *args = data;
3248
3249 /* call driver ioctl to get mmap offset */
3250 if (!dev->driver->dumb_map_offset)
3251 return -ENOSYS;
3252
3253 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3254}
3255
3256int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3257 void *data, struct drm_file *file_priv)
3258{
3259 struct drm_mode_destroy_dumb *args = data;
3260
3261 if (!dev->driver->dumb_destroy)
3262 return -ENOSYS;
3263
3264 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3265}
Dave Airlie248dbc22011-11-29 20:02:54 +00003266
3267/*
3268 * Just need to support RGB formats here for compat with code that doesn't
3269 * use pixel formats directly yet.
3270 */
3271void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3272 int *bpp)
3273{
3274 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003275 case DRM_FORMAT_RGB332:
3276 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003277 *depth = 8;
3278 *bpp = 8;
3279 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003280 case DRM_FORMAT_XRGB1555:
3281 case DRM_FORMAT_XBGR1555:
3282 case DRM_FORMAT_RGBX5551:
3283 case DRM_FORMAT_BGRX5551:
3284 case DRM_FORMAT_ARGB1555:
3285 case DRM_FORMAT_ABGR1555:
3286 case DRM_FORMAT_RGBA5551:
3287 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003288 *depth = 15;
3289 *bpp = 16;
3290 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003291 case DRM_FORMAT_RGB565:
3292 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003293 *depth = 16;
3294 *bpp = 16;
3295 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003296 case DRM_FORMAT_RGB888:
3297 case DRM_FORMAT_BGR888:
3298 *depth = 24;
3299 *bpp = 24;
3300 break;
3301 case DRM_FORMAT_XRGB8888:
3302 case DRM_FORMAT_XBGR8888:
3303 case DRM_FORMAT_RGBX8888:
3304 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003305 *depth = 24;
3306 *bpp = 32;
3307 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003308 case DRM_FORMAT_XRGB2101010:
3309 case DRM_FORMAT_XBGR2101010:
3310 case DRM_FORMAT_RGBX1010102:
3311 case DRM_FORMAT_BGRX1010102:
3312 case DRM_FORMAT_ARGB2101010:
3313 case DRM_FORMAT_ABGR2101010:
3314 case DRM_FORMAT_RGBA1010102:
3315 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003316 *depth = 30;
3317 *bpp = 32;
3318 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003319 case DRM_FORMAT_ARGB8888:
3320 case DRM_FORMAT_ABGR8888:
3321 case DRM_FORMAT_RGBA8888:
3322 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003323 *depth = 32;
3324 *bpp = 32;
3325 break;
3326 default:
3327 DRM_DEBUG_KMS("unsupported pixel format\n");
3328 *depth = 0;
3329 *bpp = 0;
3330 break;
3331 }
3332}
3333EXPORT_SYMBOL(drm_fb_get_bpp_depth);