blob: a9ca1b80fc28aa9e330f407accc750455906b180 [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
Dave Airlief453ba02008-11-07 14:05:41 -080035#include "drm.h"
36#include "drmP.h"
37#include "drm_crtc.h"
Adam Jackson7466f4c2010-03-29 21:43:23 +000038#include "drm_edid.h"
Jesse Barnes308e5bc2011-11-14 14:51:28 -080039#include "drm_fourcc.h"
Dave Airlief453ba02008-11-07 14:05:41 -080040
Dave Airlief453ba02008-11-07 14:05:41 -080041/* Avoid boilerplate. I'm tired of typing. */
42#define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
44 { \
45 int i; \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
49 } \
50 return "(unknown)"; \
51 }
52
53/*
54 * Global properties
55 */
56static struct drm_prop_enum_list drm_dpms_enum_list[] =
57{ { DRM_MODE_DPMS_ON, "On" },
58 { DRM_MODE_DPMS_STANDBY, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
60 { DRM_MODE_DPMS_OFF, "Off" }
61};
62
63DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
64
65/*
66 * Optional properties
67 */
68static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69{
Jesse Barnes53bd8382009-07-01 10:04:40 -070070 { DRM_MODE_SCALE_NONE, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
72 { DRM_MODE_SCALE_CENTER, "Center" },
73 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080074};
75
76static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77{
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100080 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080081};
82
83/*
84 * Non-global properties, but "required" for certain connectors.
85 */
86static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87{
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
91};
92
93DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96{
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
100};
101
102DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106{
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800112};
113
114DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117{
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000128static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
132};
133
134DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
Dave Airlief453ba02008-11-07 14:05:41 -0800137struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141};
142
143/*
144 * Connector and encoder types.
145 */
146static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500156 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800163};
164
165static struct drm_prop_enum_list drm_encoder_enum_list[] =
166{ { DRM_MODE_ENCODER_NONE, "None" },
167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800172};
173
174char *drm_get_encoder_name(struct drm_encoder *encoder)
175{
176 static char buf[32];
177
178 snprintf(buf, 32, "%s-%d",
179 drm_encoder_enum_list[encoder->encoder_type].name,
180 encoder->base.id);
181 return buf;
182}
Dave Airlie13a81952009-09-07 15:45:33 +1000183EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800184
185char *drm_get_connector_name(struct drm_connector *connector)
186{
187 static char buf[32];
188
189 snprintf(buf, 32, "%s-%d",
190 drm_connector_enum_list[connector->connector_type].name,
191 connector->connector_type_id);
192 return buf;
193}
194EXPORT_SYMBOL(drm_get_connector_name);
195
196char *drm_get_connector_status_name(enum drm_connector_status status)
197{
198 if (status == connector_status_connected)
199 return "connected";
200 else if (status == connector_status_disconnected)
201 return "disconnected";
202 else
203 return "unknown";
204}
205
206/**
207 * drm_mode_object_get - allocate a new identifier
208 * @dev: DRM device
209 * @ptr: object pointer, used to generate unique ID
210 * @type: object type
211 *
212 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800213 *
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
216 *
217 * RETURNS:
218 * New unique (relative to other objects in @dev) integer identifier for the
219 * object.
220 */
221static int drm_mode_object_get(struct drm_device *dev,
222 struct drm_mode_object *obj, uint32_t obj_type)
223{
224 int new_id = 0;
225 int ret;
226
Dave Airlief453ba02008-11-07 14:05:41 -0800227again:
228 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
229 DRM_ERROR("Ran out memory getting a mode number\n");
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200230 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -0800231 }
232
Jesse Barnesad2563c2009-01-19 17:21:45 +1000233 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800234 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000235 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800236 if (ret == -EAGAIN)
237 goto again;
Ville Syrjäläf1ae1262012-03-15 19:58:31 +0200238 else if (ret)
239 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800240
241 obj->id = new_id;
242 obj->type = obj_type;
243 return 0;
244}
245
246/**
247 * drm_mode_object_put - free an identifer
248 * @dev: DRM device
249 * @id: ID to free
250 *
251 * LOCKING:
252 * Caller must hold DRM mode_config lock.
253 *
254 * Free @id from @dev's unique identifier pool.
255 */
256static void drm_mode_object_put(struct drm_device *dev,
257 struct drm_mode_object *object)
258{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000259 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800260 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000261 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800262}
263
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200264struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
265 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800266{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000267 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800268
Jesse Barnesad2563c2009-01-19 17:21:45 +1000269 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800270 obj = idr_find(&dev->mode_config.crtc_idr, id);
271 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000272 obj = NULL;
273 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800274
275 return obj;
276}
277EXPORT_SYMBOL(drm_mode_object_find);
278
279/**
Dave Airlief453ba02008-11-07 14:05:41 -0800280 * drm_framebuffer_init - initialize a framebuffer
281 * @dev: DRM device
282 *
283 * LOCKING:
284 * Caller must hold mode config lock.
285 *
286 * Allocates an ID for the framebuffer's parent mode object, sets its mode
287 * functions & device file and adds it to the master fd list.
288 *
289 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200290 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800291 */
292int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
293 const struct drm_framebuffer_funcs *funcs)
294{
295 int ret;
296
297 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200298 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800299 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800300
301 fb->dev = dev;
302 fb->funcs = funcs;
303 dev->mode_config.num_fb++;
304 list_add(&fb->head, &dev->mode_config.fb_list);
305
306 return 0;
307}
308EXPORT_SYMBOL(drm_framebuffer_init);
309
310/**
311 * drm_framebuffer_cleanup - remove a framebuffer object
312 * @fb: framebuffer to remove
313 *
314 * LOCKING:
315 * Caller must hold mode config lock.
316 *
317 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
318 * it, setting it to NULL.
319 */
320void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
321{
322 struct drm_device *dev = fb->dev;
323 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800324 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000325 struct drm_mode_set set;
326 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800327
328 /* remove from any CRTC */
329 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000330 if (crtc->fb == fb) {
331 /* should turn off the crtc */
332 memset(&set, 0, sizeof(struct drm_mode_set));
333 set.crtc = crtc;
334 set.fb = NULL;
335 ret = crtc->funcs->set_config(&set);
336 if (ret)
337 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
338 }
Dave Airlief453ba02008-11-07 14:05:41 -0800339 }
340
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800341 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
342 if (plane->fb == fb) {
343 /* should turn off the crtc */
344 ret = plane->funcs->disable_plane(plane);
345 if (ret)
346 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600347 /* disconnect the plane from the fb and crtc: */
348 plane->fb = NULL;
349 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800350 }
351 }
352
Dave Airlief453ba02008-11-07 14:05:41 -0800353 drm_mode_object_put(dev, &fb->base);
354 list_del(&fb->head);
355 dev->mode_config.num_fb--;
356}
357EXPORT_SYMBOL(drm_framebuffer_cleanup);
358
359/**
360 * drm_crtc_init - Initialise a new CRTC object
361 * @dev: DRM device
362 * @crtc: CRTC object to init
363 * @funcs: callbacks for the new CRTC
364 *
365 * LOCKING:
366 * Caller must hold mode config lock.
367 *
368 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200369 *
370 * RETURNS:
371 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800372 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200373int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800374 const struct drm_crtc_funcs *funcs)
375{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200376 int ret;
377
Dave Airlief453ba02008-11-07 14:05:41 -0800378 crtc->dev = dev;
379 crtc->funcs = funcs;
380
381 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200382
383 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
384 if (ret)
385 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800386
387 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
388 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200389
390 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800391 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200392
393 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800394}
395EXPORT_SYMBOL(drm_crtc_init);
396
397/**
398 * drm_crtc_cleanup - Cleans up the core crtc usage.
399 * @crtc: CRTC to cleanup
400 *
401 * LOCKING:
402 * Caller must hold mode config lock.
403 *
404 * Cleanup @crtc. Removes from drm modesetting space
405 * does NOT free object, caller does that.
406 */
407void drm_crtc_cleanup(struct drm_crtc *crtc)
408{
409 struct drm_device *dev = crtc->dev;
410
411 if (crtc->gamma_store) {
412 kfree(crtc->gamma_store);
413 crtc->gamma_store = NULL;
414 }
415
416 drm_mode_object_put(dev, &crtc->base);
417 list_del(&crtc->head);
418 dev->mode_config.num_crtc--;
419}
420EXPORT_SYMBOL(drm_crtc_cleanup);
421
422/**
423 * drm_mode_probed_add - add a mode to a connector's probed mode list
424 * @connector: connector the new mode
425 * @mode: mode data
426 *
427 * LOCKING:
428 * Caller must hold mode config lock.
429 *
430 * Add @mode to @connector's mode list for later use.
431 */
432void drm_mode_probed_add(struct drm_connector *connector,
433 struct drm_display_mode *mode)
434{
435 list_add(&mode->head, &connector->probed_modes);
436}
437EXPORT_SYMBOL(drm_mode_probed_add);
438
439/**
440 * drm_mode_remove - remove and free a mode
441 * @connector: connector list to modify
442 * @mode: mode to remove
443 *
444 * LOCKING:
445 * Caller must hold mode config lock.
446 *
447 * Remove @mode from @connector's mode list, then free it.
448 */
449void drm_mode_remove(struct drm_connector *connector,
450 struct drm_display_mode *mode)
451{
452 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100453 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800454}
455EXPORT_SYMBOL(drm_mode_remove);
456
457/**
458 * drm_connector_init - Init a preallocated connector
459 * @dev: DRM device
460 * @connector: the connector to init
461 * @funcs: callbacks for this connector
462 * @name: user visible name of the connector
463 *
464 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100465 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800466 *
467 * Initialises a preallocated connector. Connectors should be
468 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200469 *
470 * RETURNS:
471 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800472 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200473int drm_connector_init(struct drm_device *dev,
474 struct drm_connector *connector,
475 const struct drm_connector_funcs *funcs,
476 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800477{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200478 int ret;
479
Dave Airlief453ba02008-11-07 14:05:41 -0800480 mutex_lock(&dev->mode_config.mutex);
481
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200482 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
483 if (ret)
484 goto out;
485
Dave Airlief453ba02008-11-07 14:05:41 -0800486 connector->dev = dev;
487 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800488 connector->connector_type = connector_type;
489 connector->connector_type_id =
490 ++drm_connector_enum_list[connector_type].count; /* TODO */
491 INIT_LIST_HEAD(&connector->user_modes);
492 INIT_LIST_HEAD(&connector->probed_modes);
493 INIT_LIST_HEAD(&connector->modes);
494 connector->edid_blob_ptr = NULL;
495
496 list_add_tail(&connector->head, &dev->mode_config.connector_list);
497 dev->mode_config.num_connector++;
498
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200499 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
500 drm_connector_attach_property(connector,
501 dev->mode_config.edid_property,
502 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800503
504 drm_connector_attach_property(connector,
505 dev->mode_config.dpms_property, 0);
506
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200507 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800508 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200509
510 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800511}
512EXPORT_SYMBOL(drm_connector_init);
513
514/**
515 * drm_connector_cleanup - cleans up an initialised connector
516 * @connector: connector to cleanup
517 *
518 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100519 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800520 *
521 * Cleans up the connector but doesn't free the object.
522 */
523void drm_connector_cleanup(struct drm_connector *connector)
524{
525 struct drm_device *dev = connector->dev;
526 struct drm_display_mode *mode, *t;
527
528 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
529 drm_mode_remove(connector, mode);
530
531 list_for_each_entry_safe(mode, t, &connector->modes, head)
532 drm_mode_remove(connector, mode);
533
534 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
535 drm_mode_remove(connector, mode);
536
537 mutex_lock(&dev->mode_config.mutex);
538 drm_mode_object_put(dev, &connector->base);
539 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000540 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800541 mutex_unlock(&dev->mode_config.mutex);
542}
543EXPORT_SYMBOL(drm_connector_cleanup);
544
Dave Airliecbc7e222012-02-20 14:16:40 +0000545void drm_connector_unplug_all(struct drm_device *dev)
546{
547 struct drm_connector *connector;
548
549 /* taking the mode config mutex ends up in a clash with sysfs */
550 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
551 drm_sysfs_connector_remove(connector);
552
553}
554EXPORT_SYMBOL(drm_connector_unplug_all);
555
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200556int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000557 struct drm_encoder *encoder,
558 const struct drm_encoder_funcs *funcs,
559 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800560{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200561 int ret;
562
Dave Airlief453ba02008-11-07 14:05:41 -0800563 mutex_lock(&dev->mode_config.mutex);
564
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200565 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
566 if (ret)
567 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800568
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200569 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800570 encoder->encoder_type = encoder_type;
571 encoder->funcs = funcs;
572
573 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
574 dev->mode_config.num_encoder++;
575
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200576 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800577 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200578
579 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800580}
581EXPORT_SYMBOL(drm_encoder_init);
582
583void drm_encoder_cleanup(struct drm_encoder *encoder)
584{
585 struct drm_device *dev = encoder->dev;
586 mutex_lock(&dev->mode_config.mutex);
587 drm_mode_object_put(dev, &encoder->base);
588 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000589 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800590 mutex_unlock(&dev->mode_config.mutex);
591}
592EXPORT_SYMBOL(drm_encoder_cleanup);
593
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800594int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
595 unsigned long possible_crtcs,
596 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600597 const uint32_t *formats, uint32_t format_count,
598 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800599{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200600 int ret;
601
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800602 mutex_lock(&dev->mode_config.mutex);
603
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200604 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
605 if (ret)
606 goto out;
607
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800608 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800609 plane->funcs = funcs;
610 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
611 GFP_KERNEL);
612 if (!plane->format_types) {
613 DRM_DEBUG_KMS("out of memory when allocating plane\n");
614 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200615 ret = -ENOMEM;
616 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800617 }
618
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800619 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800620 plane->format_count = format_count;
621 plane->possible_crtcs = possible_crtcs;
622
Rob Clark0a7eb242011-12-13 20:19:36 -0600623 /* private planes are not exposed to userspace, but depending on
624 * display hardware, might be convenient to allow sharing programming
625 * for the scanout engine with the crtc implementation.
626 */
627 if (!priv) {
628 list_add_tail(&plane->head, &dev->mode_config.plane_list);
629 dev->mode_config.num_plane++;
630 } else {
631 INIT_LIST_HEAD(&plane->head);
632 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800633
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200634 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800635 mutex_unlock(&dev->mode_config.mutex);
636
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200637 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800638}
639EXPORT_SYMBOL(drm_plane_init);
640
641void drm_plane_cleanup(struct drm_plane *plane)
642{
643 struct drm_device *dev = plane->dev;
644
645 mutex_lock(&dev->mode_config.mutex);
646 kfree(plane->format_types);
647 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600648 /* if not added to a list, it must be a private plane */
649 if (!list_empty(&plane->head)) {
650 list_del(&plane->head);
651 dev->mode_config.num_plane--;
652 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800653 mutex_unlock(&dev->mode_config.mutex);
654}
655EXPORT_SYMBOL(drm_plane_cleanup);
656
Dave Airlief453ba02008-11-07 14:05:41 -0800657/**
658 * drm_mode_create - create a new display mode
659 * @dev: DRM device
660 *
661 * LOCKING:
662 * Caller must hold DRM mode_config lock.
663 *
664 * Create a new drm_display_mode, give it an ID, and return it.
665 *
666 * RETURNS:
667 * Pointer to new mode on success, NULL on error.
668 */
669struct drm_display_mode *drm_mode_create(struct drm_device *dev)
670{
671 struct drm_display_mode *nmode;
672
673 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
674 if (!nmode)
675 return NULL;
676
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200677 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
678 kfree(nmode);
679 return NULL;
680 }
681
Dave Airlief453ba02008-11-07 14:05:41 -0800682 return nmode;
683}
684EXPORT_SYMBOL(drm_mode_create);
685
686/**
687 * drm_mode_destroy - remove a mode
688 * @dev: DRM device
689 * @mode: mode to remove
690 *
691 * LOCKING:
692 * Caller must hold mode config lock.
693 *
694 * Free @mode's unique identifier, then free it.
695 */
696void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
697{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200698 if (!mode)
699 return;
700
Dave Airlief453ba02008-11-07 14:05:41 -0800701 drm_mode_object_put(dev, &mode->base);
702
703 kfree(mode);
704}
705EXPORT_SYMBOL(drm_mode_destroy);
706
707static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
708{
709 struct drm_property *edid;
710 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800711
712 /*
713 * Standard properties (apply to all connectors)
714 */
715 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
716 DRM_MODE_PROP_IMMUTABLE,
717 "EDID", 0);
718 dev->mode_config.edid_property = edid;
719
Sascha Hauer4a67d392012-02-06 10:58:17 +0100720 dpms = drm_property_create_enum(dev, 0,
721 "DPMS", drm_dpms_enum_list,
722 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800723 dev->mode_config.dpms_property = dpms;
724
725 return 0;
726}
727
728/**
729 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
730 * @dev: DRM device
731 *
732 * Called by a driver the first time a DVI-I connector is made.
733 */
734int drm_mode_create_dvi_i_properties(struct drm_device *dev)
735{
736 struct drm_property *dvi_i_selector;
737 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800738
739 if (dev->mode_config.dvi_i_select_subconnector_property)
740 return 0;
741
742 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100743 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800744 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100745 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800746 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800747 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
748
Sascha Hauer4a67d392012-02-06 10:58:17 +0100749 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800750 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100751 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800752 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800753 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
754
755 return 0;
756}
757EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
758
759/**
760 * drm_create_tv_properties - create TV specific connector properties
761 * @dev: DRM device
762 * @num_modes: number of different TV formats (modes) supported
763 * @modes: array of pointers to strings containing name of each format
764 *
765 * Called by a driver's TV initialization routine, this function creates
766 * the TV specific connector properties for a given device. Caller is
767 * responsible for allocating a list of format names and passing them to
768 * this routine.
769 */
770int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
771 char *modes[])
772{
773 struct drm_property *tv_selector;
774 struct drm_property *tv_subconnector;
775 int i;
776
777 if (dev->mode_config.tv_select_subconnector_property)
778 return 0;
779
780 /*
781 * Basic connector properties
782 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100783 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800784 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100785 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800786 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800787 dev->mode_config.tv_select_subconnector_property = tv_selector;
788
789 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100790 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
791 "subconnector",
792 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800793 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800794 dev->mode_config.tv_subconnector_property = tv_subconnector;
795
796 /*
797 * Other, TV specific properties: margins & TV modes.
798 */
799 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100800 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800801
802 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100803 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800804
805 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100806 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800807
808 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100809 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800810
811 dev->mode_config.tv_mode_property =
812 drm_property_create(dev, DRM_MODE_PROP_ENUM,
813 "mode", num_modes);
814 for (i = 0; i < num_modes; i++)
815 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
816 i, modes[i]);
817
Francisco Jerezb6b79022009-08-02 04:19:20 +0200818 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100819 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200820
821 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100822 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200823
824 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100825 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200826
Francisco Jereza75f0232009-08-12 02:30:10 +0200827 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100828 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200829
830 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100831 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200832
833 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100834 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200835
Dave Airlief453ba02008-11-07 14:05:41 -0800836 return 0;
837}
838EXPORT_SYMBOL(drm_mode_create_tv_properties);
839
840/**
841 * drm_mode_create_scaling_mode_property - create scaling mode property
842 * @dev: DRM device
843 *
844 * Called by a driver the first time it's needed, must be attached to desired
845 * connectors.
846 */
847int drm_mode_create_scaling_mode_property(struct drm_device *dev)
848{
849 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800850
851 if (dev->mode_config.scaling_mode_property)
852 return 0;
853
854 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100855 drm_property_create_enum(dev, 0, "scaling mode",
856 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800857 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800858
859 dev->mode_config.scaling_mode_property = scaling_mode;
860
861 return 0;
862}
863EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
864
865/**
866 * drm_mode_create_dithering_property - create dithering property
867 * @dev: DRM device
868 *
869 * Called by a driver the first time it's needed, must be attached to desired
870 * connectors.
871 */
872int drm_mode_create_dithering_property(struct drm_device *dev)
873{
874 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800875
876 if (dev->mode_config.dithering_mode_property)
877 return 0;
878
879 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100880 drm_property_create_enum(dev, 0, "dithering",
881 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800882 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800883 dev->mode_config.dithering_mode_property = dithering_mode;
884
885 return 0;
886}
887EXPORT_SYMBOL(drm_mode_create_dithering_property);
888
889/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000890 * drm_mode_create_dirty_property - create dirty property
891 * @dev: DRM device
892 *
893 * Called by a driver the first time it's needed, must be attached to desired
894 * connectors.
895 */
896int drm_mode_create_dirty_info_property(struct drm_device *dev)
897{
898 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000899
900 if (dev->mode_config.dirty_info_property)
901 return 0;
902
903 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100904 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000905 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100906 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000907 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000908 dev->mode_config.dirty_info_property = dirty_info;
909
910 return 0;
911}
912EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
913
914/**
Dave Airlief453ba02008-11-07 14:05:41 -0800915 * drm_mode_config_init - initialize DRM mode_configuration structure
916 * @dev: DRM device
917 *
918 * LOCKING:
919 * None, should happen single threaded at init time.
920 *
921 * Initialize @dev's mode_config structure, used for tracking the graphics
922 * configuration of @dev.
923 */
924void drm_mode_config_init(struct drm_device *dev)
925{
926 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000927 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800928 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800929 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
930 INIT_LIST_HEAD(&dev->mode_config.connector_list);
931 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
932 INIT_LIST_HEAD(&dev->mode_config.property_list);
933 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800934 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800935 idr_init(&dev->mode_config.crtc_idr);
936
937 mutex_lock(&dev->mode_config.mutex);
938 drm_mode_create_standard_connector_properties(dev);
939 mutex_unlock(&dev->mode_config.mutex);
940
941 /* Just to be sure */
942 dev->mode_config.num_fb = 0;
943 dev->mode_config.num_connector = 0;
944 dev->mode_config.num_crtc = 0;
945 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800946}
947EXPORT_SYMBOL(drm_mode_config_init);
948
949int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
950{
951 uint32_t total_objects = 0;
952
953 total_objects += dev->mode_config.num_crtc;
954 total_objects += dev->mode_config.num_connector;
955 total_objects += dev->mode_config.num_encoder;
956
Dave Airlief453ba02008-11-07 14:05:41 -0800957 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
958 if (!group->id_list)
959 return -ENOMEM;
960
961 group->num_crtcs = 0;
962 group->num_connectors = 0;
963 group->num_encoders = 0;
964 return 0;
965}
966
967int drm_mode_group_init_legacy_group(struct drm_device *dev,
968 struct drm_mode_group *group)
969{
970 struct drm_crtc *crtc;
971 struct drm_encoder *encoder;
972 struct drm_connector *connector;
973 int ret;
974
975 if ((ret = drm_mode_group_init(dev, group)))
976 return ret;
977
978 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
979 group->id_list[group->num_crtcs++] = crtc->base.id;
980
981 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
982 group->id_list[group->num_crtcs + group->num_encoders++] =
983 encoder->base.id;
984
985 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
986 group->id_list[group->num_crtcs + group->num_encoders +
987 group->num_connectors++] = connector->base.id;
988
989 return 0;
990}
Dave Airlie9c1dfc52012-03-20 06:59:29 +0000991EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -0800992
993/**
994 * drm_mode_config_cleanup - free up DRM mode_config info
995 * @dev: DRM device
996 *
997 * LOCKING:
998 * Caller must hold mode config lock.
999 *
1000 * Free up all the connectors and CRTCs associated with this DRM device, then
1001 * free up the framebuffers and associated buffer objects.
1002 *
1003 * FIXME: cleanup any dangling user buffer objects too
1004 */
1005void drm_mode_config_cleanup(struct drm_device *dev)
1006{
1007 struct drm_connector *connector, *ot;
1008 struct drm_crtc *crtc, *ct;
1009 struct drm_encoder *encoder, *enct;
1010 struct drm_framebuffer *fb, *fbt;
1011 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001012 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001013
1014 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1015 head) {
1016 encoder->funcs->destroy(encoder);
1017 }
1018
1019 list_for_each_entry_safe(connector, ot,
1020 &dev->mode_config.connector_list, head) {
1021 connector->funcs->destroy(connector);
1022 }
1023
1024 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1025 head) {
1026 drm_property_destroy(dev, property);
1027 }
1028
1029 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1030 fb->funcs->destroy(fb);
1031 }
1032
1033 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1034 crtc->funcs->destroy(crtc);
1035 }
1036
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001037 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1038 head) {
1039 plane->funcs->destroy(plane);
1040 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001041
1042 idr_remove_all(&dev->mode_config.crtc_idr);
1043 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001044}
1045EXPORT_SYMBOL(drm_mode_config_cleanup);
1046
Dave Airlief453ba02008-11-07 14:05:41 -08001047/**
1048 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1049 * @out: drm_mode_modeinfo struct to return to the user
1050 * @in: drm_display_mode to use
1051 *
1052 * LOCKING:
1053 * None.
1054 *
1055 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1056 * the user.
1057 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001058static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1059 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001060{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001061 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1062 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1063 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1064 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1065 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1066 "timing values too large for mode info\n");
1067
Dave Airlief453ba02008-11-07 14:05:41 -08001068 out->clock = in->clock;
1069 out->hdisplay = in->hdisplay;
1070 out->hsync_start = in->hsync_start;
1071 out->hsync_end = in->hsync_end;
1072 out->htotal = in->htotal;
1073 out->hskew = in->hskew;
1074 out->vdisplay = in->vdisplay;
1075 out->vsync_start = in->vsync_start;
1076 out->vsync_end = in->vsync_end;
1077 out->vtotal = in->vtotal;
1078 out->vscan = in->vscan;
1079 out->vrefresh = in->vrefresh;
1080 out->flags = in->flags;
1081 out->type = in->type;
1082 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1083 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1084}
1085
1086/**
1087 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1088 * @out: drm_display_mode to return to the user
1089 * @in: drm_mode_modeinfo to use
1090 *
1091 * LOCKING:
1092 * None.
1093 *
1094 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1095 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001096 *
1097 * RETURNS:
1098 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001099 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001100static int drm_crtc_convert_umode(struct drm_display_mode *out,
1101 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001102{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001103 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1104 return -ERANGE;
1105
Dave Airlief453ba02008-11-07 14:05:41 -08001106 out->clock = in->clock;
1107 out->hdisplay = in->hdisplay;
1108 out->hsync_start = in->hsync_start;
1109 out->hsync_end = in->hsync_end;
1110 out->htotal = in->htotal;
1111 out->hskew = in->hskew;
1112 out->vdisplay = in->vdisplay;
1113 out->vsync_start = in->vsync_start;
1114 out->vsync_end = in->vsync_end;
1115 out->vtotal = in->vtotal;
1116 out->vscan = in->vscan;
1117 out->vrefresh = in->vrefresh;
1118 out->flags = in->flags;
1119 out->type = in->type;
1120 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1121 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001122
1123 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001124}
1125
1126/**
1127 * drm_mode_getresources - get graphics configuration
1128 * @inode: inode from the ioctl
1129 * @filp: file * from the ioctl
1130 * @cmd: cmd from ioctl
1131 * @arg: arg from ioctl
1132 *
1133 * LOCKING:
1134 * Takes mode config lock.
1135 *
1136 * Construct a set of configuration description structures and return
1137 * them to the user, including CRTC, connector and framebuffer configuration.
1138 *
1139 * Called by the user via ioctl.
1140 *
1141 * RETURNS:
1142 * Zero on success, errno on failure.
1143 */
1144int drm_mode_getresources(struct drm_device *dev, void *data,
1145 struct drm_file *file_priv)
1146{
1147 struct drm_mode_card_res *card_res = data;
1148 struct list_head *lh;
1149 struct drm_framebuffer *fb;
1150 struct drm_connector *connector;
1151 struct drm_crtc *crtc;
1152 struct drm_encoder *encoder;
1153 int ret = 0;
1154 int connector_count = 0;
1155 int crtc_count = 0;
1156 int fb_count = 0;
1157 int encoder_count = 0;
1158 int copied = 0, i;
1159 uint32_t __user *fb_id;
1160 uint32_t __user *crtc_id;
1161 uint32_t __user *connector_id;
1162 uint32_t __user *encoder_id;
1163 struct drm_mode_group *mode_group;
1164
Dave Airliefb3b06c2011-02-08 13:55:21 +10001165 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1166 return -EINVAL;
1167
Dave Airlief453ba02008-11-07 14:05:41 -08001168 mutex_lock(&dev->mode_config.mutex);
1169
1170 /*
1171 * For the non-control nodes we need to limit the list of resources
1172 * by IDs in the group list for this node
1173 */
1174 list_for_each(lh, &file_priv->fbs)
1175 fb_count++;
1176
1177 mode_group = &file_priv->master->minor->mode_group;
1178 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1179
1180 list_for_each(lh, &dev->mode_config.crtc_list)
1181 crtc_count++;
1182
1183 list_for_each(lh, &dev->mode_config.connector_list)
1184 connector_count++;
1185
1186 list_for_each(lh, &dev->mode_config.encoder_list)
1187 encoder_count++;
1188 } else {
1189
1190 crtc_count = mode_group->num_crtcs;
1191 connector_count = mode_group->num_connectors;
1192 encoder_count = mode_group->num_encoders;
1193 }
1194
1195 card_res->max_height = dev->mode_config.max_height;
1196 card_res->min_height = dev->mode_config.min_height;
1197 card_res->max_width = dev->mode_config.max_width;
1198 card_res->min_width = dev->mode_config.min_width;
1199
1200 /* handle this in 4 parts */
1201 /* FBs */
1202 if (card_res->count_fbs >= fb_count) {
1203 copied = 0;
1204 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001205 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001206 if (put_user(fb->base.id, fb_id + copied)) {
1207 ret = -EFAULT;
1208 goto out;
1209 }
1210 copied++;
1211 }
1212 }
1213 card_res->count_fbs = fb_count;
1214
1215 /* CRTCs */
1216 if (card_res->count_crtcs >= crtc_count) {
1217 copied = 0;
1218 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1219 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1220 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1221 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001222 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001223 if (put_user(crtc->base.id, crtc_id + copied)) {
1224 ret = -EFAULT;
1225 goto out;
1226 }
1227 copied++;
1228 }
1229 } else {
1230 for (i = 0; i < mode_group->num_crtcs; i++) {
1231 if (put_user(mode_group->id_list[i],
1232 crtc_id + copied)) {
1233 ret = -EFAULT;
1234 goto out;
1235 }
1236 copied++;
1237 }
1238 }
1239 }
1240 card_res->count_crtcs = crtc_count;
1241
1242 /* Encoders */
1243 if (card_res->count_encoders >= encoder_count) {
1244 copied = 0;
1245 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1246 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1247 list_for_each_entry(encoder,
1248 &dev->mode_config.encoder_list,
1249 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001250 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1251 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001252 if (put_user(encoder->base.id, encoder_id +
1253 copied)) {
1254 ret = -EFAULT;
1255 goto out;
1256 }
1257 copied++;
1258 }
1259 } else {
1260 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1261 if (put_user(mode_group->id_list[i],
1262 encoder_id + copied)) {
1263 ret = -EFAULT;
1264 goto out;
1265 }
1266 copied++;
1267 }
1268
1269 }
1270 }
1271 card_res->count_encoders = encoder_count;
1272
1273 /* Connectors */
1274 if (card_res->count_connectors >= connector_count) {
1275 copied = 0;
1276 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1277 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1278 list_for_each_entry(connector,
1279 &dev->mode_config.connector_list,
1280 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001281 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1282 connector->base.id,
1283 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001284 if (put_user(connector->base.id,
1285 connector_id + copied)) {
1286 ret = -EFAULT;
1287 goto out;
1288 }
1289 copied++;
1290 }
1291 } else {
1292 int start = mode_group->num_crtcs +
1293 mode_group->num_encoders;
1294 for (i = start; i < start + mode_group->num_connectors; i++) {
1295 if (put_user(mode_group->id_list[i],
1296 connector_id + copied)) {
1297 ret = -EFAULT;
1298 goto out;
1299 }
1300 copied++;
1301 }
1302 }
1303 }
1304 card_res->count_connectors = connector_count;
1305
Jerome Glisse94401062010-07-15 15:43:25 -04001306 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001307 card_res->count_connectors, card_res->count_encoders);
1308
1309out:
1310 mutex_unlock(&dev->mode_config.mutex);
1311 return ret;
1312}
1313
1314/**
1315 * drm_mode_getcrtc - get CRTC configuration
1316 * @inode: inode from the ioctl
1317 * @filp: file * from the ioctl
1318 * @cmd: cmd from ioctl
1319 * @arg: arg from ioctl
1320 *
1321 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001322 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001323 *
1324 * Construct a CRTC configuration structure to return to the user.
1325 *
1326 * Called by the user via ioctl.
1327 *
1328 * RETURNS:
1329 * Zero on success, errno on failure.
1330 */
1331int drm_mode_getcrtc(struct drm_device *dev,
1332 void *data, struct drm_file *file_priv)
1333{
1334 struct drm_mode_crtc *crtc_resp = data;
1335 struct drm_crtc *crtc;
1336 struct drm_mode_object *obj;
1337 int ret = 0;
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 mutex_lock(&dev->mode_config.mutex);
1343
1344 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1345 DRM_MODE_OBJECT_CRTC);
1346 if (!obj) {
1347 ret = -EINVAL;
1348 goto out;
1349 }
1350 crtc = obj_to_crtc(obj);
1351
1352 crtc_resp->x = crtc->x;
1353 crtc_resp->y = crtc->y;
1354 crtc_resp->gamma_size = crtc->gamma_size;
1355 if (crtc->fb)
1356 crtc_resp->fb_id = crtc->fb->base.id;
1357 else
1358 crtc_resp->fb_id = 0;
1359
1360 if (crtc->enabled) {
1361
1362 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1363 crtc_resp->mode_valid = 1;
1364
1365 } else {
1366 crtc_resp->mode_valid = 0;
1367 }
1368
1369out:
1370 mutex_unlock(&dev->mode_config.mutex);
1371 return ret;
1372}
1373
1374/**
1375 * drm_mode_getconnector - get connector configuration
1376 * @inode: inode from the ioctl
1377 * @filp: file * from the ioctl
1378 * @cmd: cmd from ioctl
1379 * @arg: arg from ioctl
1380 *
1381 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001382 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001383 *
1384 * Construct a connector configuration structure to return to the user.
1385 *
1386 * Called by the user via ioctl.
1387 *
1388 * RETURNS:
1389 * Zero on success, errno on failure.
1390 */
1391int drm_mode_getconnector(struct drm_device *dev, void *data,
1392 struct drm_file *file_priv)
1393{
1394 struct drm_mode_get_connector *out_resp = data;
1395 struct drm_mode_object *obj;
1396 struct drm_connector *connector;
1397 struct drm_display_mode *mode;
1398 int mode_count = 0;
1399 int props_count = 0;
1400 int encoders_count = 0;
1401 int ret = 0;
1402 int copied = 0;
1403 int i;
1404 struct drm_mode_modeinfo u_mode;
1405 struct drm_mode_modeinfo __user *mode_ptr;
1406 uint32_t __user *prop_ptr;
1407 uint64_t __user *prop_values;
1408 uint32_t __user *encoder_ptr;
1409
Dave Airliefb3b06c2011-02-08 13:55:21 +10001410 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1411 return -EINVAL;
1412
Dave Airlief453ba02008-11-07 14:05:41 -08001413 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1414
Jerome Glisse94401062010-07-15 15:43:25 -04001415 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001416
1417 mutex_lock(&dev->mode_config.mutex);
1418
1419 obj = drm_mode_object_find(dev, out_resp->connector_id,
1420 DRM_MODE_OBJECT_CONNECTOR);
1421 if (!obj) {
1422 ret = -EINVAL;
1423 goto out;
1424 }
1425 connector = obj_to_connector(obj);
1426
1427 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1428 if (connector->property_ids[i] != 0) {
1429 props_count++;
1430 }
1431 }
1432
1433 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1434 if (connector->encoder_ids[i] != 0) {
1435 encoders_count++;
1436 }
1437 }
1438
1439 if (out_resp->count_modes == 0) {
1440 connector->funcs->fill_modes(connector,
1441 dev->mode_config.max_width,
1442 dev->mode_config.max_height);
1443 }
1444
1445 /* delayed so we get modes regardless of pre-fill_modes state */
1446 list_for_each_entry(mode, &connector->modes, head)
1447 mode_count++;
1448
1449 out_resp->connector_id = connector->base.id;
1450 out_resp->connector_type = connector->connector_type;
1451 out_resp->connector_type_id = connector->connector_type_id;
1452 out_resp->mm_width = connector->display_info.width_mm;
1453 out_resp->mm_height = connector->display_info.height_mm;
1454 out_resp->subpixel = connector->display_info.subpixel_order;
1455 out_resp->connection = connector->status;
1456 if (connector->encoder)
1457 out_resp->encoder_id = connector->encoder->base.id;
1458 else
1459 out_resp->encoder_id = 0;
1460
1461 /*
1462 * This ioctl is called twice, once to determine how much space is
1463 * needed, and the 2nd time to fill it.
1464 */
1465 if ((out_resp->count_modes >= mode_count) && mode_count) {
1466 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001467 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001468 list_for_each_entry(mode, &connector->modes, head) {
1469 drm_crtc_convert_to_umode(&u_mode, mode);
1470 if (copy_to_user(mode_ptr + copied,
1471 &u_mode, sizeof(u_mode))) {
1472 ret = -EFAULT;
1473 goto out;
1474 }
1475 copied++;
1476 }
1477 }
1478 out_resp->count_modes = mode_count;
1479
1480 if ((out_resp->count_props >= props_count) && props_count) {
1481 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001482 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1483 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001484 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1485 if (connector->property_ids[i] != 0) {
1486 if (put_user(connector->property_ids[i],
1487 prop_ptr + copied)) {
1488 ret = -EFAULT;
1489 goto out;
1490 }
1491
1492 if (put_user(connector->property_values[i],
1493 prop_values + copied)) {
1494 ret = -EFAULT;
1495 goto out;
1496 }
1497 copied++;
1498 }
1499 }
1500 }
1501 out_resp->count_props = props_count;
1502
1503 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1504 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001505 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001506 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1507 if (connector->encoder_ids[i] != 0) {
1508 if (put_user(connector->encoder_ids[i],
1509 encoder_ptr + copied)) {
1510 ret = -EFAULT;
1511 goto out;
1512 }
1513 copied++;
1514 }
1515 }
1516 }
1517 out_resp->count_encoders = encoders_count;
1518
1519out:
1520 mutex_unlock(&dev->mode_config.mutex);
1521 return ret;
1522}
1523
1524int drm_mode_getencoder(struct drm_device *dev, void *data,
1525 struct drm_file *file_priv)
1526{
1527 struct drm_mode_get_encoder *enc_resp = data;
1528 struct drm_mode_object *obj;
1529 struct drm_encoder *encoder;
1530 int ret = 0;
1531
Dave Airliefb3b06c2011-02-08 13:55:21 +10001532 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1533 return -EINVAL;
1534
Dave Airlief453ba02008-11-07 14:05:41 -08001535 mutex_lock(&dev->mode_config.mutex);
1536 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1537 DRM_MODE_OBJECT_ENCODER);
1538 if (!obj) {
1539 ret = -EINVAL;
1540 goto out;
1541 }
1542 encoder = obj_to_encoder(obj);
1543
1544 if (encoder->crtc)
1545 enc_resp->crtc_id = encoder->crtc->base.id;
1546 else
1547 enc_resp->crtc_id = 0;
1548 enc_resp->encoder_type = encoder->encoder_type;
1549 enc_resp->encoder_id = encoder->base.id;
1550 enc_resp->possible_crtcs = encoder->possible_crtcs;
1551 enc_resp->possible_clones = encoder->possible_clones;
1552
1553out:
1554 mutex_unlock(&dev->mode_config.mutex);
1555 return ret;
1556}
1557
1558/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001559 * drm_mode_getplane_res - get plane info
1560 * @dev: DRM device
1561 * @data: ioctl data
1562 * @file_priv: DRM file info
1563 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001564 * LOCKING:
1565 * Takes mode config lock.
1566 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001567 * Return an plane count and set of IDs.
1568 */
1569int drm_mode_getplane_res(struct drm_device *dev, void *data,
1570 struct drm_file *file_priv)
1571{
1572 struct drm_mode_get_plane_res *plane_resp = data;
1573 struct drm_mode_config *config;
1574 struct drm_plane *plane;
1575 uint32_t __user *plane_ptr;
1576 int copied = 0, ret = 0;
1577
1578 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1579 return -EINVAL;
1580
1581 mutex_lock(&dev->mode_config.mutex);
1582 config = &dev->mode_config;
1583
1584 /*
1585 * This ioctl is called twice, once to determine how much space is
1586 * needed, and the 2nd time to fill it.
1587 */
1588 if (config->num_plane &&
1589 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001590 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001591
1592 list_for_each_entry(plane, &config->plane_list, head) {
1593 if (put_user(plane->base.id, plane_ptr + copied)) {
1594 ret = -EFAULT;
1595 goto out;
1596 }
1597 copied++;
1598 }
1599 }
1600 plane_resp->count_planes = config->num_plane;
1601
1602out:
1603 mutex_unlock(&dev->mode_config.mutex);
1604 return ret;
1605}
1606
1607/**
1608 * drm_mode_getplane - get plane info
1609 * @dev: DRM device
1610 * @data: ioctl data
1611 * @file_priv: DRM file info
1612 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001613 * LOCKING:
1614 * Takes mode config lock.
1615 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001616 * Return plane info, including formats supported, gamma size, any
1617 * current fb, etc.
1618 */
1619int drm_mode_getplane(struct drm_device *dev, void *data,
1620 struct drm_file *file_priv)
1621{
1622 struct drm_mode_get_plane *plane_resp = data;
1623 struct drm_mode_object *obj;
1624 struct drm_plane *plane;
1625 uint32_t __user *format_ptr;
1626 int ret = 0;
1627
1628 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1629 return -EINVAL;
1630
1631 mutex_lock(&dev->mode_config.mutex);
1632 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1633 DRM_MODE_OBJECT_PLANE);
1634 if (!obj) {
1635 ret = -ENOENT;
1636 goto out;
1637 }
1638 plane = obj_to_plane(obj);
1639
1640 if (plane->crtc)
1641 plane_resp->crtc_id = plane->crtc->base.id;
1642 else
1643 plane_resp->crtc_id = 0;
1644
1645 if (plane->fb)
1646 plane_resp->fb_id = plane->fb->base.id;
1647 else
1648 plane_resp->fb_id = 0;
1649
1650 plane_resp->plane_id = plane->base.id;
1651 plane_resp->possible_crtcs = plane->possible_crtcs;
1652 plane_resp->gamma_size = plane->gamma_size;
1653
1654 /*
1655 * This ioctl is called twice, once to determine how much space is
1656 * needed, and the 2nd time to fill it.
1657 */
1658 if (plane->format_count &&
1659 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001660 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001661 if (copy_to_user(format_ptr,
1662 plane->format_types,
1663 sizeof(uint32_t) * plane->format_count)) {
1664 ret = -EFAULT;
1665 goto out;
1666 }
1667 }
1668 plane_resp->count_format_types = plane->format_count;
1669
1670out:
1671 mutex_unlock(&dev->mode_config.mutex);
1672 return ret;
1673}
1674
1675/**
1676 * drm_mode_setplane - set up or tear down an plane
1677 * @dev: DRM device
1678 * @data: ioctl data*
1679 * @file_prive: DRM file info
1680 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001681 * LOCKING:
1682 * Takes mode config lock.
1683 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001684 * Set plane info, including placement, fb, scaling, and other factors.
1685 * Or pass a NULL fb to disable.
1686 */
1687int drm_mode_setplane(struct drm_device *dev, void *data,
1688 struct drm_file *file_priv)
1689{
1690 struct drm_mode_set_plane *plane_req = data;
1691 struct drm_mode_object *obj;
1692 struct drm_plane *plane;
1693 struct drm_crtc *crtc;
1694 struct drm_framebuffer *fb;
1695 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001696 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001697 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001698
1699 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1700 return -EINVAL;
1701
1702 mutex_lock(&dev->mode_config.mutex);
1703
1704 /*
1705 * First, find the plane, crtc, and fb objects. If not available,
1706 * we don't bother to call the driver.
1707 */
1708 obj = drm_mode_object_find(dev, plane_req->plane_id,
1709 DRM_MODE_OBJECT_PLANE);
1710 if (!obj) {
1711 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1712 plane_req->plane_id);
1713 ret = -ENOENT;
1714 goto out;
1715 }
1716 plane = obj_to_plane(obj);
1717
1718 /* No fb means shut it down */
1719 if (!plane_req->fb_id) {
1720 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001721 plane->crtc = NULL;
1722 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001723 goto out;
1724 }
1725
1726 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1727 DRM_MODE_OBJECT_CRTC);
1728 if (!obj) {
1729 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1730 plane_req->crtc_id);
1731 ret = -ENOENT;
1732 goto out;
1733 }
1734 crtc = obj_to_crtc(obj);
1735
1736 obj = drm_mode_object_find(dev, plane_req->fb_id,
1737 DRM_MODE_OBJECT_FB);
1738 if (!obj) {
1739 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1740 plane_req->fb_id);
1741 ret = -ENOENT;
1742 goto out;
1743 }
1744 fb = obj_to_fb(obj);
1745
Ville Syrjälä62443be2011-12-20 00:06:47 +02001746 /* Check whether this plane supports the fb pixel format. */
1747 for (i = 0; i < plane->format_count; i++)
1748 if (fb->pixel_format == plane->format_types[i])
1749 break;
1750 if (i == plane->format_count) {
1751 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1752 ret = -EINVAL;
1753 goto out;
1754 }
1755
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001756 fb_width = fb->width << 16;
1757 fb_height = fb->height << 16;
1758
1759 /* Make sure source coordinates are inside the fb. */
1760 if (plane_req->src_w > fb_width ||
1761 plane_req->src_x > fb_width - plane_req->src_w ||
1762 plane_req->src_h > fb_height ||
1763 plane_req->src_y > fb_height - plane_req->src_h) {
1764 DRM_DEBUG_KMS("Invalid source coordinates "
1765 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1766 plane_req->src_w >> 16,
1767 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1768 plane_req->src_h >> 16,
1769 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1770 plane_req->src_x >> 16,
1771 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1772 plane_req->src_y >> 16,
1773 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1774 ret = -ENOSPC;
1775 goto out;
1776 }
1777
Ville Syrjälä687a0402011-12-20 00:06:45 +02001778 /* Give drivers some help against integer overflows */
1779 if (plane_req->crtc_w > INT_MAX ||
1780 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1781 plane_req->crtc_h > INT_MAX ||
1782 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1783 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1784 plane_req->crtc_w, plane_req->crtc_h,
1785 plane_req->crtc_x, plane_req->crtc_y);
1786 ret = -ERANGE;
1787 goto out;
1788 }
1789
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001790 ret = plane->funcs->update_plane(plane, crtc, fb,
1791 plane_req->crtc_x, plane_req->crtc_y,
1792 plane_req->crtc_w, plane_req->crtc_h,
1793 plane_req->src_x, plane_req->src_y,
1794 plane_req->src_w, plane_req->src_h);
1795 if (!ret) {
1796 plane->crtc = crtc;
1797 plane->fb = fb;
1798 }
1799
1800out:
1801 mutex_unlock(&dev->mode_config.mutex);
1802
1803 return ret;
1804}
1805
1806/**
Dave Airlief453ba02008-11-07 14:05:41 -08001807 * drm_mode_setcrtc - set CRTC configuration
1808 * @inode: inode from the ioctl
1809 * @filp: file * from the ioctl
1810 * @cmd: cmd from ioctl
1811 * @arg: arg from ioctl
1812 *
1813 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001814 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001815 *
1816 * Build a new CRTC configuration based on user request.
1817 *
1818 * Called by the user via ioctl.
1819 *
1820 * RETURNS:
1821 * Zero on success, errno on failure.
1822 */
1823int drm_mode_setcrtc(struct drm_device *dev, void *data,
1824 struct drm_file *file_priv)
1825{
1826 struct drm_mode_config *config = &dev->mode_config;
1827 struct drm_mode_crtc *crtc_req = data;
1828 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001829 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001830 struct drm_connector **connector_set = NULL, *connector;
1831 struct drm_framebuffer *fb = NULL;
1832 struct drm_display_mode *mode = NULL;
1833 struct drm_mode_set set;
1834 uint32_t __user *set_connectors_ptr;
1835 int ret = 0;
1836 int i;
1837
Dave Airliefb3b06c2011-02-08 13:55:21 +10001838 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1839 return -EINVAL;
1840
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001841 /* For some reason crtc x/y offsets are signed internally. */
1842 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1843 return -ERANGE;
1844
Dave Airlief453ba02008-11-07 14:05:41 -08001845 mutex_lock(&dev->mode_config.mutex);
1846 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1847 DRM_MODE_OBJECT_CRTC);
1848 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001849 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001850 ret = -EINVAL;
1851 goto out;
1852 }
1853 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001854 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001855
1856 if (crtc_req->mode_valid) {
1857 /* If we have a mode we need a framebuffer. */
1858 /* If we pass -1, set the mode with the currently bound fb */
1859 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001860 if (!crtc->fb) {
1861 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1862 ret = -EINVAL;
1863 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001864 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001865 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001866 } else {
1867 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1868 DRM_MODE_OBJECT_FB);
1869 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001870 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1871 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001872 ret = -EINVAL;
1873 goto out;
1874 }
1875 fb = obj_to_fb(obj);
1876 }
1877
1878 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001879 if (!mode) {
1880 ret = -ENOMEM;
1881 goto out;
1882 }
1883
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001884 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1885 if (ret) {
1886 DRM_DEBUG_KMS("Invalid mode\n");
1887 goto out;
1888 }
1889
Dave Airlief453ba02008-11-07 14:05:41 -08001890 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001891
1892 if (mode->hdisplay > fb->width ||
1893 mode->vdisplay > fb->height ||
1894 crtc_req->x > fb->width - mode->hdisplay ||
1895 crtc_req->y > fb->height - mode->vdisplay) {
1896 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1897 mode->hdisplay, mode->vdisplay,
1898 crtc_req->x, crtc_req->y,
1899 fb->width, fb->height);
1900 ret = -ENOSPC;
1901 goto out;
1902 }
Dave Airlief453ba02008-11-07 14:05:41 -08001903 }
1904
1905 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001906 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001907 ret = -EINVAL;
1908 goto out;
1909 }
1910
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001911 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001912 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001913 crtc_req->count_connectors);
1914 ret = -EINVAL;
1915 goto out;
1916 }
1917
1918 if (crtc_req->count_connectors > 0) {
1919 u32 out_id;
1920
1921 /* Avoid unbounded kernel memory allocation */
1922 if (crtc_req->count_connectors > config->num_connector) {
1923 ret = -EINVAL;
1924 goto out;
1925 }
1926
1927 connector_set = kmalloc(crtc_req->count_connectors *
1928 sizeof(struct drm_connector *),
1929 GFP_KERNEL);
1930 if (!connector_set) {
1931 ret = -ENOMEM;
1932 goto out;
1933 }
1934
1935 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001936 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001937 if (get_user(out_id, &set_connectors_ptr[i])) {
1938 ret = -EFAULT;
1939 goto out;
1940 }
1941
1942 obj = drm_mode_object_find(dev, out_id,
1943 DRM_MODE_OBJECT_CONNECTOR);
1944 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001945 DRM_DEBUG_KMS("Connector id %d unknown\n",
1946 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001947 ret = -EINVAL;
1948 goto out;
1949 }
1950 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001951 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1952 connector->base.id,
1953 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001954
1955 connector_set[i] = connector;
1956 }
1957 }
1958
1959 set.crtc = crtc;
1960 set.x = crtc_req->x;
1961 set.y = crtc_req->y;
1962 set.mode = mode;
1963 set.connectors = connector_set;
1964 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001965 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001966 ret = crtc->funcs->set_config(&set);
1967
1968out:
1969 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001970 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001971 mutex_unlock(&dev->mode_config.mutex);
1972 return ret;
1973}
1974
1975int drm_mode_cursor_ioctl(struct drm_device *dev,
1976 void *data, struct drm_file *file_priv)
1977{
1978 struct drm_mode_cursor *req = data;
1979 struct drm_mode_object *obj;
1980 struct drm_crtc *crtc;
1981 int ret = 0;
1982
Dave Airliefb3b06c2011-02-08 13:55:21 +10001983 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1984 return -EINVAL;
1985
Jesse Barnesacb4b992011-11-07 12:03:23 -08001986 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001987 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001988
1989 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001990 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001992 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001993 ret = -EINVAL;
1994 goto out;
1995 }
1996 crtc = obj_to_crtc(obj);
1997
1998 if (req->flags & DRM_MODE_CURSOR_BO) {
1999 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08002000 ret = -ENXIO;
2001 goto out;
2002 }
2003 /* Turns off the cursor if handle is 0 */
2004 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2005 req->width, req->height);
2006 }
2007
2008 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2009 if (crtc->funcs->cursor_move) {
2010 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2011 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002012 ret = -EFAULT;
2013 goto out;
2014 }
2015 }
2016out:
2017 mutex_unlock(&dev->mode_config.mutex);
2018 return ret;
2019}
2020
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002021/* Original addfb only supported RGB formats, so figure out which one */
2022uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2023{
2024 uint32_t fmt;
2025
2026 switch (bpp) {
2027 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002028 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002029 break;
2030 case 16:
2031 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002032 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002033 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002034 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002035 break;
2036 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002037 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002038 break;
2039 case 32:
2040 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002041 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002042 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002045 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002046 break;
2047 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002048 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2049 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002050 break;
2051 }
2052
2053 return fmt;
2054}
2055EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2056
Dave Airlief453ba02008-11-07 14:05:41 -08002057/**
2058 * drm_mode_addfb - add an FB to the graphics configuration
2059 * @inode: inode from the ioctl
2060 * @filp: file * from the ioctl
2061 * @cmd: cmd from ioctl
2062 * @arg: arg from ioctl
2063 *
2064 * LOCKING:
2065 * Takes mode config lock.
2066 *
2067 * Add a new FB to the specified CRTC, given a user request.
2068 *
2069 * Called by the user via ioctl.
2070 *
2071 * RETURNS:
2072 * Zero on success, errno on failure.
2073 */
2074int drm_mode_addfb(struct drm_device *dev,
2075 void *data, struct drm_file *file_priv)
2076{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002077 struct drm_mode_fb_cmd *or = data;
2078 struct drm_mode_fb_cmd2 r = {};
2079 struct drm_mode_config *config = &dev->mode_config;
2080 struct drm_framebuffer *fb;
2081 int ret = 0;
2082
2083 /* Use new struct with format internally */
2084 r.fb_id = or->fb_id;
2085 r.width = or->width;
2086 r.height = or->height;
2087 r.pitches[0] = or->pitch;
2088 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2089 r.handles[0] = or->handle;
2090
2091 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2092 return -EINVAL;
2093
Jesse Barnesacb4b992011-11-07 12:03:23 -08002094 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002095 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002096
2097 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002098 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002099
2100 mutex_lock(&dev->mode_config.mutex);
2101
2102 /* TODO check buffer is sufficiently large */
2103 /* TODO setup destructor callback */
2104
2105 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2106 if (IS_ERR(fb)) {
2107 DRM_ERROR("could not create framebuffer\n");
2108 ret = PTR_ERR(fb);
2109 goto out;
2110 }
2111
2112 or->fb_id = fb->base.id;
2113 list_add(&fb->filp_head, &file_priv->fbs);
2114 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2115
2116out:
2117 mutex_unlock(&dev->mode_config.mutex);
2118 return ret;
2119}
2120
Ville Syrjälä935b5972011-12-20 00:06:48 +02002121static int format_check(struct drm_mode_fb_cmd2 *r)
2122{
2123 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2124
2125 switch (format) {
2126 case DRM_FORMAT_C8:
2127 case DRM_FORMAT_RGB332:
2128 case DRM_FORMAT_BGR233:
2129 case DRM_FORMAT_XRGB4444:
2130 case DRM_FORMAT_XBGR4444:
2131 case DRM_FORMAT_RGBX4444:
2132 case DRM_FORMAT_BGRX4444:
2133 case DRM_FORMAT_ARGB4444:
2134 case DRM_FORMAT_ABGR4444:
2135 case DRM_FORMAT_RGBA4444:
2136 case DRM_FORMAT_BGRA4444:
2137 case DRM_FORMAT_XRGB1555:
2138 case DRM_FORMAT_XBGR1555:
2139 case DRM_FORMAT_RGBX5551:
2140 case DRM_FORMAT_BGRX5551:
2141 case DRM_FORMAT_ARGB1555:
2142 case DRM_FORMAT_ABGR1555:
2143 case DRM_FORMAT_RGBA5551:
2144 case DRM_FORMAT_BGRA5551:
2145 case DRM_FORMAT_RGB565:
2146 case DRM_FORMAT_BGR565:
2147 case DRM_FORMAT_RGB888:
2148 case DRM_FORMAT_BGR888:
2149 case DRM_FORMAT_XRGB8888:
2150 case DRM_FORMAT_XBGR8888:
2151 case DRM_FORMAT_RGBX8888:
2152 case DRM_FORMAT_BGRX8888:
2153 case DRM_FORMAT_ARGB8888:
2154 case DRM_FORMAT_ABGR8888:
2155 case DRM_FORMAT_RGBA8888:
2156 case DRM_FORMAT_BGRA8888:
2157 case DRM_FORMAT_XRGB2101010:
2158 case DRM_FORMAT_XBGR2101010:
2159 case DRM_FORMAT_RGBX1010102:
2160 case DRM_FORMAT_BGRX1010102:
2161 case DRM_FORMAT_ARGB2101010:
2162 case DRM_FORMAT_ABGR2101010:
2163 case DRM_FORMAT_RGBA1010102:
2164 case DRM_FORMAT_BGRA1010102:
2165 case DRM_FORMAT_YUYV:
2166 case DRM_FORMAT_YVYU:
2167 case DRM_FORMAT_UYVY:
2168 case DRM_FORMAT_VYUY:
2169 case DRM_FORMAT_AYUV:
2170 case DRM_FORMAT_NV12:
2171 case DRM_FORMAT_NV21:
2172 case DRM_FORMAT_NV16:
2173 case DRM_FORMAT_NV61:
2174 case DRM_FORMAT_YUV410:
2175 case DRM_FORMAT_YVU410:
2176 case DRM_FORMAT_YUV411:
2177 case DRM_FORMAT_YVU411:
2178 case DRM_FORMAT_YUV420:
2179 case DRM_FORMAT_YVU420:
2180 case DRM_FORMAT_YUV422:
2181 case DRM_FORMAT_YVU422:
2182 case DRM_FORMAT_YUV444:
2183 case DRM_FORMAT_YVU444:
2184 return 0;
2185 default:
2186 return -EINVAL;
2187 }
2188}
2189
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002190static int framebuffer_check(struct drm_mode_fb_cmd2 *r)
2191{
2192 int ret, hsub, vsub, num_planes, i;
2193
2194 ret = format_check(r);
2195 if (ret) {
2196 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2197 return ret;
2198 }
2199
2200 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2201 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2202 num_planes = drm_format_num_planes(r->pixel_format);
2203
2204 if (r->width == 0 || r->width % hsub) {
2205 DRM_ERROR("bad framebuffer width %u\n", r->height);
2206 return -EINVAL;
2207 }
2208
2209 if (r->height == 0 || r->height % vsub) {
2210 DRM_ERROR("bad framebuffer height %u\n", r->height);
2211 return -EINVAL;
2212 }
2213
2214 for (i = 0; i < num_planes; i++) {
2215 unsigned int width = r->width / (i != 0 ? hsub : 1);
2216
2217 if (!r->handles[i]) {
2218 DRM_ERROR("no buffer object handle for plane %d\n", i);
2219 return -EINVAL;
2220 }
2221
2222 if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) {
2223 DRM_ERROR("bad pitch %u for plane %d\n", r->pitches[i], i);
2224 return -EINVAL;
2225 }
2226 }
2227
2228 return 0;
2229}
2230
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002231/**
2232 * drm_mode_addfb2 - add an FB to the graphics configuration
2233 * @inode: inode from the ioctl
2234 * @filp: file * from the ioctl
2235 * @cmd: cmd from ioctl
2236 * @arg: arg from ioctl
2237 *
2238 * LOCKING:
2239 * Takes mode config lock.
2240 *
2241 * Add a new FB to the specified CRTC, given a user request with format.
2242 *
2243 * Called by the user via ioctl.
2244 *
2245 * RETURNS:
2246 * Zero on success, errno on failure.
2247 */
2248int drm_mode_addfb2(struct drm_device *dev,
2249 void *data, struct drm_file *file_priv)
2250{
2251 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002252 struct drm_mode_config *config = &dev->mode_config;
2253 struct drm_framebuffer *fb;
2254 int ret = 0;
2255
Dave Airliefb3b06c2011-02-08 13:55:21 +10002256 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2257 return -EINVAL;
2258
Dave Airlief453ba02008-11-07 14:05:41 -08002259 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002260 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2261 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002262 return -EINVAL;
2263 }
2264 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002265 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2266 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002267 return -EINVAL;
2268 }
2269
Ville Syrjäläd1b45d52012-04-05 21:35:18 +03002270 ret = framebuffer_check(r);
2271 if (ret)
Ville Syrjälä935b5972011-12-20 00:06:48 +02002272 return ret;
Ville Syrjälä935b5972011-12-20 00:06:48 +02002273
Dave Airlief453ba02008-11-07 14:05:41 -08002274 mutex_lock(&dev->mode_config.mutex);
2275
Dave Airlief453ba02008-11-07 14:05:41 -08002276 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002277 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002278 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002279 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002280 goto out;
2281 }
2282
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002283 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002284 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002285 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002286
2287out:
2288 mutex_unlock(&dev->mode_config.mutex);
2289 return ret;
2290}
2291
2292/**
2293 * drm_mode_rmfb - remove an FB from the configuration
2294 * @inode: inode from the ioctl
2295 * @filp: file * from the ioctl
2296 * @cmd: cmd from ioctl
2297 * @arg: arg from ioctl
2298 *
2299 * LOCKING:
2300 * Takes mode config lock.
2301 *
2302 * Remove the FB specified by the user.
2303 *
2304 * Called by the user via ioctl.
2305 *
2306 * RETURNS:
2307 * Zero on success, errno on failure.
2308 */
2309int drm_mode_rmfb(struct drm_device *dev,
2310 void *data, struct drm_file *file_priv)
2311{
2312 struct drm_mode_object *obj;
2313 struct drm_framebuffer *fb = NULL;
2314 struct drm_framebuffer *fbl = NULL;
2315 uint32_t *id = data;
2316 int ret = 0;
2317 int found = 0;
2318
Dave Airliefb3b06c2011-02-08 13:55:21 +10002319 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2320 return -EINVAL;
2321
Dave Airlief453ba02008-11-07 14:05:41 -08002322 mutex_lock(&dev->mode_config.mutex);
2323 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002324 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002325 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002326 ret = -EINVAL;
2327 goto out;
2328 }
2329 fb = obj_to_fb(obj);
2330
2331 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2332 if (fb == fbl)
2333 found = 1;
2334
2335 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002336 ret = -EINVAL;
2337 goto out;
2338 }
2339
2340 /* TODO release all crtc connected to the framebuffer */
2341 /* TODO unhock the destructor from the buffer object */
2342
2343 list_del(&fb->filp_head);
2344 fb->funcs->destroy(fb);
2345
2346out:
2347 mutex_unlock(&dev->mode_config.mutex);
2348 return ret;
2349}
2350
2351/**
2352 * drm_mode_getfb - get FB info
2353 * @inode: inode from the ioctl
2354 * @filp: file * from the ioctl
2355 * @cmd: cmd from ioctl
2356 * @arg: arg from ioctl
2357 *
2358 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002359 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002360 *
2361 * Lookup the FB given its ID and return info about it.
2362 *
2363 * Called by the user via ioctl.
2364 *
2365 * RETURNS:
2366 * Zero on success, errno on failure.
2367 */
2368int drm_mode_getfb(struct drm_device *dev,
2369 void *data, struct drm_file *file_priv)
2370{
2371 struct drm_mode_fb_cmd *r = data;
2372 struct drm_mode_object *obj;
2373 struct drm_framebuffer *fb;
2374 int ret = 0;
2375
Dave Airliefb3b06c2011-02-08 13:55:21 +10002376 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2377 return -EINVAL;
2378
Dave Airlief453ba02008-11-07 14:05:41 -08002379 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002380 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002381 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002382 ret = -EINVAL;
2383 goto out;
2384 }
2385 fb = obj_to_fb(obj);
2386
2387 r->height = fb->height;
2388 r->width = fb->width;
2389 r->depth = fb->depth;
2390 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002391 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002392 fb->funcs->create_handle(fb, file_priv, &r->handle);
2393
2394out:
2395 mutex_unlock(&dev->mode_config.mutex);
2396 return ret;
2397}
2398
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002399int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2400 void *data, struct drm_file *file_priv)
2401{
2402 struct drm_clip_rect __user *clips_ptr;
2403 struct drm_clip_rect *clips = NULL;
2404 struct drm_mode_fb_dirty_cmd *r = data;
2405 struct drm_mode_object *obj;
2406 struct drm_framebuffer *fb;
2407 unsigned flags;
2408 int num_clips;
2409 int ret = 0;
2410
Dave Airliefb3b06c2011-02-08 13:55:21 +10002411 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2412 return -EINVAL;
2413
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002414 mutex_lock(&dev->mode_config.mutex);
2415 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2416 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002417 ret = -EINVAL;
2418 goto out_err1;
2419 }
2420 fb = obj_to_fb(obj);
2421
2422 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002423 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002424
2425 if (!num_clips != !clips_ptr) {
2426 ret = -EINVAL;
2427 goto out_err1;
2428 }
2429
2430 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2431
2432 /* If userspace annotates copy, clips must come in pairs */
2433 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2434 ret = -EINVAL;
2435 goto out_err1;
2436 }
2437
2438 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002439 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2440 ret = -EINVAL;
2441 goto out_err1;
2442 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002443 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2444 if (!clips) {
2445 ret = -ENOMEM;
2446 goto out_err1;
2447 }
2448
2449 ret = copy_from_user(clips, clips_ptr,
2450 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002451 if (ret) {
2452 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002453 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002454 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002455 }
2456
2457 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002458 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2459 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002460 } else {
2461 ret = -ENOSYS;
2462 goto out_err2;
2463 }
2464
2465out_err2:
2466 kfree(clips);
2467out_err1:
2468 mutex_unlock(&dev->mode_config.mutex);
2469 return ret;
2470}
2471
2472
Dave Airlief453ba02008-11-07 14:05:41 -08002473/**
2474 * drm_fb_release - remove and free the FBs on this file
2475 * @filp: file * from the ioctl
2476 *
2477 * LOCKING:
2478 * Takes mode config lock.
2479 *
2480 * Destroy all the FBs associated with @filp.
2481 *
2482 * Called by the user via ioctl.
2483 *
2484 * RETURNS:
2485 * Zero on success, errno on failure.
2486 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002487void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002488{
Dave Airlief453ba02008-11-07 14:05:41 -08002489 struct drm_device *dev = priv->minor->dev;
2490 struct drm_framebuffer *fb, *tfb;
2491
2492 mutex_lock(&dev->mode_config.mutex);
2493 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2494 list_del(&fb->filp_head);
2495 fb->funcs->destroy(fb);
2496 }
2497 mutex_unlock(&dev->mode_config.mutex);
2498}
2499
2500/**
2501 * drm_mode_attachmode - add a mode to the user mode list
2502 * @dev: DRM device
2503 * @connector: connector to add the mode to
2504 * @mode: mode to add
2505 *
2506 * Add @mode to @connector's user mode list.
2507 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002508static void drm_mode_attachmode(struct drm_device *dev,
2509 struct drm_connector *connector,
2510 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002511{
Dave Airlief453ba02008-11-07 14:05:41 -08002512 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002513}
2514
2515int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002516 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002517{
2518 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002519 int ret = 0;
2520 struct drm_display_mode *dup_mode, *next;
2521 LIST_HEAD(list);
2522
Dave Airlief453ba02008-11-07 14:05:41 -08002523 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2524 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002525 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002526 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002527 dup_mode = drm_mode_duplicate(dev, mode);
2528 if (!dup_mode) {
2529 ret = -ENOMEM;
2530 goto out;
2531 }
2532 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002533 }
2534 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002535
2536 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2537 if (!connector->encoder)
2538 continue;
2539 if (connector->encoder->crtc == crtc)
2540 list_move_tail(list.next, &connector->user_modes);
2541 }
2542
2543 WARN_ON(!list_empty(&list));
2544
2545 out:
2546 list_for_each_entry_safe(dup_mode, next, &list, head)
2547 drm_mode_destroy(dev, dup_mode);
2548
2549 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002550}
2551EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2552
2553static int drm_mode_detachmode(struct drm_device *dev,
2554 struct drm_connector *connector,
2555 struct drm_display_mode *mode)
2556{
2557 int found = 0;
2558 int ret = 0;
2559 struct drm_display_mode *match_mode, *t;
2560
2561 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2562 if (drm_mode_equal(match_mode, mode)) {
2563 list_del(&match_mode->head);
2564 drm_mode_destroy(dev, match_mode);
2565 found = 1;
2566 break;
2567 }
2568 }
2569
2570 if (!found)
2571 ret = -EINVAL;
2572
2573 return ret;
2574}
2575
2576int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2577{
2578 struct drm_connector *connector;
2579
2580 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2581 drm_mode_detachmode(dev, connector, mode);
2582 }
2583 return 0;
2584}
2585EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2586
2587/**
2588 * drm_fb_attachmode - Attach a user mode to an connector
2589 * @inode: inode from the ioctl
2590 * @filp: file * from the ioctl
2591 * @cmd: cmd from ioctl
2592 * @arg: arg from ioctl
2593 *
2594 * This attaches a user specified mode to an connector.
2595 * Called by the user via ioctl.
2596 *
2597 * RETURNS:
2598 * Zero on success, errno on failure.
2599 */
2600int drm_mode_attachmode_ioctl(struct drm_device *dev,
2601 void *data, struct drm_file *file_priv)
2602{
2603 struct drm_mode_mode_cmd *mode_cmd = data;
2604 struct drm_connector *connector;
2605 struct drm_display_mode *mode;
2606 struct drm_mode_object *obj;
2607 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2608 int ret = 0;
2609
Dave Airliefb3b06c2011-02-08 13:55:21 +10002610 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2611 return -EINVAL;
2612
Dave Airlief453ba02008-11-07 14:05:41 -08002613 mutex_lock(&dev->mode_config.mutex);
2614
2615 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2616 if (!obj) {
2617 ret = -EINVAL;
2618 goto out;
2619 }
2620 connector = obj_to_connector(obj);
2621
2622 mode = drm_mode_create(dev);
2623 if (!mode) {
2624 ret = -ENOMEM;
2625 goto out;
2626 }
2627
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002628 ret = drm_crtc_convert_umode(mode, umode);
2629 if (ret) {
2630 DRM_DEBUG_KMS("Invalid mode\n");
2631 drm_mode_destroy(dev, mode);
2632 goto out;
2633 }
Dave Airlief453ba02008-11-07 14:05:41 -08002634
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002635 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002636out:
2637 mutex_unlock(&dev->mode_config.mutex);
2638 return ret;
2639}
2640
2641
2642/**
2643 * drm_fb_detachmode - Detach a user specified mode from an connector
2644 * @inode: inode from the ioctl
2645 * @filp: file * from the ioctl
2646 * @cmd: cmd from ioctl
2647 * @arg: arg from ioctl
2648 *
2649 * Called by the user via ioctl.
2650 *
2651 * RETURNS:
2652 * Zero on success, errno on failure.
2653 */
2654int drm_mode_detachmode_ioctl(struct drm_device *dev,
2655 void *data, struct drm_file *file_priv)
2656{
2657 struct drm_mode_object *obj;
2658 struct drm_mode_mode_cmd *mode_cmd = data;
2659 struct drm_connector *connector;
2660 struct drm_display_mode mode;
2661 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2662 int ret = 0;
2663
Dave Airliefb3b06c2011-02-08 13:55:21 +10002664 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2665 return -EINVAL;
2666
Dave Airlief453ba02008-11-07 14:05:41 -08002667 mutex_lock(&dev->mode_config.mutex);
2668
2669 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2670 if (!obj) {
2671 ret = -EINVAL;
2672 goto out;
2673 }
2674 connector = obj_to_connector(obj);
2675
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002676 ret = drm_crtc_convert_umode(&mode, umode);
2677 if (ret) {
2678 DRM_DEBUG_KMS("Invalid mode\n");
2679 goto out;
2680 }
2681
Dave Airlief453ba02008-11-07 14:05:41 -08002682 ret = drm_mode_detachmode(dev, connector, &mode);
2683out:
2684 mutex_unlock(&dev->mode_config.mutex);
2685 return ret;
2686}
2687
2688struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2689 const char *name, int num_values)
2690{
2691 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002692 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002693
2694 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2695 if (!property)
2696 return NULL;
2697
2698 if (num_values) {
2699 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2700 if (!property->values)
2701 goto fail;
2702 }
2703
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002704 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2705 if (ret)
2706 goto fail;
2707
Dave Airlief453ba02008-11-07 14:05:41 -08002708 property->flags = flags;
2709 property->num_values = num_values;
2710 INIT_LIST_HEAD(&property->enum_blob_list);
2711
Vinson Lee471dd2e2011-11-10 11:55:40 -08002712 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002713 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002714 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2715 }
Dave Airlief453ba02008-11-07 14:05:41 -08002716
2717 list_add_tail(&property->head, &dev->mode_config.property_list);
2718 return property;
2719fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002720 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002721 kfree(property);
2722 return NULL;
2723}
2724EXPORT_SYMBOL(drm_property_create);
2725
Sascha Hauer4a67d392012-02-06 10:58:17 +01002726struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2727 const char *name,
2728 const struct drm_prop_enum_list *props,
2729 int num_values)
2730{
2731 struct drm_property *property;
2732 int i, ret;
2733
2734 flags |= DRM_MODE_PROP_ENUM;
2735
2736 property = drm_property_create(dev, flags, name, num_values);
2737 if (!property)
2738 return NULL;
2739
2740 for (i = 0; i < num_values; i++) {
2741 ret = drm_property_add_enum(property, i,
2742 props[i].type,
2743 props[i].name);
2744 if (ret) {
2745 drm_property_destroy(dev, property);
2746 return NULL;
2747 }
2748 }
2749
2750 return property;
2751}
2752EXPORT_SYMBOL(drm_property_create_enum);
2753
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002754struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2755 const char *name,
2756 uint64_t min, uint64_t max)
2757{
2758 struct drm_property *property;
2759
2760 flags |= DRM_MODE_PROP_RANGE;
2761
2762 property = drm_property_create(dev, flags, name, 2);
2763 if (!property)
2764 return NULL;
2765
2766 property->values[0] = min;
2767 property->values[1] = max;
2768
2769 return property;
2770}
2771EXPORT_SYMBOL(drm_property_create_range);
2772
Dave Airlief453ba02008-11-07 14:05:41 -08002773int drm_property_add_enum(struct drm_property *property, int index,
2774 uint64_t value, const char *name)
2775{
2776 struct drm_property_enum *prop_enum;
2777
2778 if (!(property->flags & DRM_MODE_PROP_ENUM))
2779 return -EINVAL;
2780
2781 if (!list_empty(&property->enum_blob_list)) {
2782 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2783 if (prop_enum->value == value) {
2784 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2785 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2786 return 0;
2787 }
2788 }
2789 }
2790
2791 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2792 if (!prop_enum)
2793 return -ENOMEM;
2794
2795 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2796 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2797 prop_enum->value = value;
2798
2799 property->values[index] = value;
2800 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2801 return 0;
2802}
2803EXPORT_SYMBOL(drm_property_add_enum);
2804
2805void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2806{
2807 struct drm_property_enum *prop_enum, *pt;
2808
2809 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2810 list_del(&prop_enum->head);
2811 kfree(prop_enum);
2812 }
2813
2814 if (property->num_values)
2815 kfree(property->values);
2816 drm_mode_object_put(dev, &property->base);
2817 list_del(&property->head);
2818 kfree(property);
2819}
2820EXPORT_SYMBOL(drm_property_destroy);
2821
2822int drm_connector_attach_property(struct drm_connector *connector,
2823 struct drm_property *property, uint64_t init_val)
2824{
2825 int i;
2826
2827 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2828 if (connector->property_ids[i] == 0) {
2829 connector->property_ids[i] = property->base.id;
2830 connector->property_values[i] = init_val;
2831 break;
2832 }
2833 }
2834
2835 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2836 return -EINVAL;
2837 return 0;
2838}
2839EXPORT_SYMBOL(drm_connector_attach_property);
2840
2841int drm_connector_property_set_value(struct drm_connector *connector,
2842 struct drm_property *property, uint64_t value)
2843{
2844 int i;
2845
2846 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2847 if (connector->property_ids[i] == property->base.id) {
2848 connector->property_values[i] = value;
2849 break;
2850 }
2851 }
2852
2853 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2854 return -EINVAL;
2855 return 0;
2856}
2857EXPORT_SYMBOL(drm_connector_property_set_value);
2858
2859int drm_connector_property_get_value(struct drm_connector *connector,
2860 struct drm_property *property, uint64_t *val)
2861{
2862 int i;
2863
2864 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2865 if (connector->property_ids[i] == property->base.id) {
2866 *val = connector->property_values[i];
2867 break;
2868 }
2869 }
2870
2871 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2872 return -EINVAL;
2873 return 0;
2874}
2875EXPORT_SYMBOL(drm_connector_property_get_value);
2876
2877int drm_mode_getproperty_ioctl(struct drm_device *dev,
2878 void *data, struct drm_file *file_priv)
2879{
2880 struct drm_mode_object *obj;
2881 struct drm_mode_get_property *out_resp = data;
2882 struct drm_property *property;
2883 int enum_count = 0;
2884 int blob_count = 0;
2885 int value_count = 0;
2886 int ret = 0, i;
2887 int copied;
2888 struct drm_property_enum *prop_enum;
2889 struct drm_mode_property_enum __user *enum_ptr;
2890 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002891 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002892 uint64_t __user *values_ptr;
2893 uint32_t __user *blob_length_ptr;
2894
Dave Airliefb3b06c2011-02-08 13:55:21 +10002895 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2896 return -EINVAL;
2897
Dave Airlief453ba02008-11-07 14:05:41 -08002898 mutex_lock(&dev->mode_config.mutex);
2899 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2900 if (!obj) {
2901 ret = -EINVAL;
2902 goto done;
2903 }
2904 property = obj_to_property(obj);
2905
2906 if (property->flags & DRM_MODE_PROP_ENUM) {
2907 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2908 enum_count++;
2909 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2910 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2911 blob_count++;
2912 }
2913
2914 value_count = property->num_values;
2915
2916 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2917 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2918 out_resp->flags = property->flags;
2919
2920 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002921 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002922 for (i = 0; i < value_count; i++) {
2923 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2924 ret = -EFAULT;
2925 goto done;
2926 }
2927 }
2928 }
2929 out_resp->count_values = value_count;
2930
2931 if (property->flags & DRM_MODE_PROP_ENUM) {
2932 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2933 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002934 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002935 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2936
2937 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2938 ret = -EFAULT;
2939 goto done;
2940 }
2941
2942 if (copy_to_user(&enum_ptr[copied].name,
2943 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2944 ret = -EFAULT;
2945 goto done;
2946 }
2947 copied++;
2948 }
2949 }
2950 out_resp->count_enum_blobs = enum_count;
2951 }
2952
2953 if (property->flags & DRM_MODE_PROP_BLOB) {
2954 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2955 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002956 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2957 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002958
2959 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2960 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2961 ret = -EFAULT;
2962 goto done;
2963 }
2964
2965 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2966 ret = -EFAULT;
2967 goto done;
2968 }
2969
2970 copied++;
2971 }
2972 }
2973 out_resp->count_enum_blobs = blob_count;
2974 }
2975done:
2976 mutex_unlock(&dev->mode_config.mutex);
2977 return ret;
2978}
2979
2980static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2981 void *data)
2982{
2983 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002984 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002985
2986 if (!length || !data)
2987 return NULL;
2988
2989 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2990 if (!blob)
2991 return NULL;
2992
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002993 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2994 if (ret) {
2995 kfree(blob);
2996 return NULL;
2997 }
2998
Dave Airlief453ba02008-11-07 14:05:41 -08002999 blob->length = length;
3000
3001 memcpy(blob->data, data, length);
3002
Dave Airlief453ba02008-11-07 14:05:41 -08003003 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3004 return blob;
3005}
3006
3007static void drm_property_destroy_blob(struct drm_device *dev,
3008 struct drm_property_blob *blob)
3009{
3010 drm_mode_object_put(dev, &blob->base);
3011 list_del(&blob->head);
3012 kfree(blob);
3013}
3014
3015int drm_mode_getblob_ioctl(struct drm_device *dev,
3016 void *data, struct drm_file *file_priv)
3017{
3018 struct drm_mode_object *obj;
3019 struct drm_mode_get_blob *out_resp = data;
3020 struct drm_property_blob *blob;
3021 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003022 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003023
Dave Airliefb3b06c2011-02-08 13:55:21 +10003024 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3025 return -EINVAL;
3026
Dave Airlief453ba02008-11-07 14:05:41 -08003027 mutex_lock(&dev->mode_config.mutex);
3028 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3029 if (!obj) {
3030 ret = -EINVAL;
3031 goto done;
3032 }
3033 blob = obj_to_blob(obj);
3034
3035 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003036 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08003037 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3038 ret = -EFAULT;
3039 goto done;
3040 }
3041 }
3042 out_resp->length = blob->length;
3043
3044done:
3045 mutex_unlock(&dev->mode_config.mutex);
3046 return ret;
3047}
3048
3049int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3050 struct edid *edid)
3051{
3052 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00003053 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003054
3055 if (connector->edid_blob_ptr)
3056 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3057
3058 /* Delete edid, when there is none. */
3059 if (!edid) {
3060 connector->edid_blob_ptr = NULL;
3061 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3062 return ret;
3063 }
3064
Adam Jackson7466f4c2010-03-29 21:43:23 +00003065 size = EDID_LENGTH * (1 + edid->extensions);
3066 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3067 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08003068
3069 ret = drm_connector_property_set_value(connector,
3070 dev->mode_config.edid_property,
3071 connector->edid_blob_ptr->base.id);
3072
3073 return ret;
3074}
3075EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3076
3077int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3078 void *data, struct drm_file *file_priv)
3079{
3080 struct drm_mode_connector_set_property *out_resp = data;
3081 struct drm_mode_object *obj;
3082 struct drm_property *property;
3083 struct drm_connector *connector;
3084 int ret = -EINVAL;
3085 int i;
3086
Dave Airliefb3b06c2011-02-08 13:55:21 +10003087 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3088 return -EINVAL;
3089
Dave Airlief453ba02008-11-07 14:05:41 -08003090 mutex_lock(&dev->mode_config.mutex);
3091
3092 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
3093 if (!obj) {
3094 goto out;
3095 }
3096 connector = obj_to_connector(obj);
3097
3098 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
3099 if (connector->property_ids[i] == out_resp->prop_id)
3100 break;
3101 }
3102
3103 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
3104 goto out;
3105 }
3106
3107 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3108 if (!obj) {
3109 goto out;
3110 }
3111 property = obj_to_property(obj);
3112
3113 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3114 goto out;
3115
3116 if (property->flags & DRM_MODE_PROP_RANGE) {
3117 if (out_resp->value < property->values[0])
3118 goto out;
3119
3120 if (out_resp->value > property->values[1])
3121 goto out;
3122 } else {
3123 int found = 0;
3124 for (i = 0; i < property->num_values; i++) {
3125 if (property->values[i] == out_resp->value) {
3126 found = 1;
3127 break;
3128 }
3129 }
3130 if (!found) {
3131 goto out;
3132 }
3133 }
3134
Keith Packardc9fb15f2009-05-30 20:42:28 -07003135 /* Do DPMS ourselves */
3136 if (property == connector->dev->mode_config.dpms_property) {
3137 if (connector->funcs->dpms)
3138 (*connector->funcs->dpms)(connector, (int) out_resp->value);
3139 ret = 0;
3140 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08003141 ret = connector->funcs->set_property(connector, property, out_resp->value);
3142
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003143 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08003144 if (!ret)
3145 drm_connector_property_set_value(connector, property, out_resp->value);
3146out:
3147 mutex_unlock(&dev->mode_config.mutex);
3148 return ret;
3149}
3150
Dave Airlief453ba02008-11-07 14:05:41 -08003151int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3152 struct drm_encoder *encoder)
3153{
3154 int i;
3155
3156 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3157 if (connector->encoder_ids[i] == 0) {
3158 connector->encoder_ids[i] = encoder->base.id;
3159 return 0;
3160 }
3161 }
3162 return -ENOMEM;
3163}
3164EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3165
3166void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3167 struct drm_encoder *encoder)
3168{
3169 int i;
3170 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3171 if (connector->encoder_ids[i] == encoder->base.id) {
3172 connector->encoder_ids[i] = 0;
3173 if (connector->encoder == encoder)
3174 connector->encoder = NULL;
3175 break;
3176 }
3177 }
3178}
3179EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3180
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003181int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003182 int gamma_size)
3183{
3184 crtc->gamma_size = gamma_size;
3185
3186 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3187 if (!crtc->gamma_store) {
3188 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003189 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003190 }
3191
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003192 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003193}
3194EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3195
3196int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3197 void *data, struct drm_file *file_priv)
3198{
3199 struct drm_mode_crtc_lut *crtc_lut = data;
3200 struct drm_mode_object *obj;
3201 struct drm_crtc *crtc;
3202 void *r_base, *g_base, *b_base;
3203 int size;
3204 int ret = 0;
3205
Dave Airliefb3b06c2011-02-08 13:55:21 +10003206 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3207 return -EINVAL;
3208
Dave Airlief453ba02008-11-07 14:05:41 -08003209 mutex_lock(&dev->mode_config.mutex);
3210 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3211 if (!obj) {
3212 ret = -EINVAL;
3213 goto out;
3214 }
3215 crtc = obj_to_crtc(obj);
3216
3217 /* memcpy into gamma store */
3218 if (crtc_lut->gamma_size != crtc->gamma_size) {
3219 ret = -EINVAL;
3220 goto out;
3221 }
3222
3223 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3224 r_base = crtc->gamma_store;
3225 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3226 ret = -EFAULT;
3227 goto out;
3228 }
3229
3230 g_base = r_base + size;
3231 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3232 ret = -EFAULT;
3233 goto out;
3234 }
3235
3236 b_base = g_base + size;
3237 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3238 ret = -EFAULT;
3239 goto out;
3240 }
3241
James Simmons72034252010-08-03 01:33:19 +01003242 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003243
3244out:
3245 mutex_unlock(&dev->mode_config.mutex);
3246 return ret;
3247
3248}
3249
3250int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3251 void *data, struct drm_file *file_priv)
3252{
3253 struct drm_mode_crtc_lut *crtc_lut = data;
3254 struct drm_mode_object *obj;
3255 struct drm_crtc *crtc;
3256 void *r_base, *g_base, *b_base;
3257 int size;
3258 int ret = 0;
3259
Dave Airliefb3b06c2011-02-08 13:55:21 +10003260 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3261 return -EINVAL;
3262
Dave Airlief453ba02008-11-07 14:05:41 -08003263 mutex_lock(&dev->mode_config.mutex);
3264 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3265 if (!obj) {
3266 ret = -EINVAL;
3267 goto out;
3268 }
3269 crtc = obj_to_crtc(obj);
3270
3271 /* memcpy into gamma store */
3272 if (crtc_lut->gamma_size != crtc->gamma_size) {
3273 ret = -EINVAL;
3274 goto out;
3275 }
3276
3277 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3278 r_base = crtc->gamma_store;
3279 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3280 ret = -EFAULT;
3281 goto out;
3282 }
3283
3284 g_base = r_base + size;
3285 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3286 ret = -EFAULT;
3287 goto out;
3288 }
3289
3290 b_base = g_base + size;
3291 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3292 ret = -EFAULT;
3293 goto out;
3294 }
3295out:
3296 mutex_unlock(&dev->mode_config.mutex);
3297 return ret;
3298}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003299
3300int drm_mode_page_flip_ioctl(struct drm_device *dev,
3301 void *data, struct drm_file *file_priv)
3302{
3303 struct drm_mode_crtc_page_flip *page_flip = data;
3304 struct drm_mode_object *obj;
3305 struct drm_crtc *crtc;
3306 struct drm_framebuffer *fb;
3307 struct drm_pending_vblank_event *e = NULL;
3308 unsigned long flags;
3309 int ret = -EINVAL;
3310
3311 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3312 page_flip->reserved != 0)
3313 return -EINVAL;
3314
3315 mutex_lock(&dev->mode_config.mutex);
3316 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3317 if (!obj)
3318 goto out;
3319 crtc = obj_to_crtc(obj);
3320
Chris Wilson90c1efd2010-07-17 20:23:26 +01003321 if (crtc->fb == NULL) {
3322 /* The framebuffer is currently unbound, presumably
3323 * due to a hotplug event, that userspace has not
3324 * yet discovered.
3325 */
3326 ret = -EBUSY;
3327 goto out;
3328 }
3329
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003330 if (crtc->funcs->page_flip == NULL)
3331 goto out;
3332
3333 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3334 if (!obj)
3335 goto out;
3336 fb = obj_to_fb(obj);
3337
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003338 if (crtc->mode.hdisplay > fb->width ||
3339 crtc->mode.vdisplay > fb->height ||
3340 crtc->x > fb->width - crtc->mode.hdisplay ||
3341 crtc->y > fb->height - crtc->mode.vdisplay) {
3342 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3343 fb->width, fb->height,
3344 crtc->mode.hdisplay, crtc->mode.vdisplay,
3345 crtc->x, crtc->y);
3346 ret = -ENOSPC;
3347 goto out;
3348 }
3349
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003350 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3351 ret = -ENOMEM;
3352 spin_lock_irqsave(&dev->event_lock, flags);
3353 if (file_priv->event_space < sizeof e->event) {
3354 spin_unlock_irqrestore(&dev->event_lock, flags);
3355 goto out;
3356 }
3357 file_priv->event_space -= sizeof e->event;
3358 spin_unlock_irqrestore(&dev->event_lock, flags);
3359
3360 e = kzalloc(sizeof *e, GFP_KERNEL);
3361 if (e == NULL) {
3362 spin_lock_irqsave(&dev->event_lock, flags);
3363 file_priv->event_space += sizeof e->event;
3364 spin_unlock_irqrestore(&dev->event_lock, flags);
3365 goto out;
3366 }
3367
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003368 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003369 e->event.base.length = sizeof e->event;
3370 e->event.user_data = page_flip->user_data;
3371 e->base.event = &e->event.base;
3372 e->base.file_priv = file_priv;
3373 e->base.destroy =
3374 (void (*) (struct drm_pending_event *)) kfree;
3375 }
3376
3377 ret = crtc->funcs->page_flip(crtc, fb, e);
3378 if (ret) {
3379 spin_lock_irqsave(&dev->event_lock, flags);
3380 file_priv->event_space += sizeof e->event;
3381 spin_unlock_irqrestore(&dev->event_lock, flags);
3382 kfree(e);
3383 }
3384
3385out:
3386 mutex_unlock(&dev->mode_config.mutex);
3387 return ret;
3388}
Chris Wilsoneb033552011-01-24 15:11:08 +00003389
3390void drm_mode_config_reset(struct drm_device *dev)
3391{
3392 struct drm_crtc *crtc;
3393 struct drm_encoder *encoder;
3394 struct drm_connector *connector;
3395
3396 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3397 if (crtc->funcs->reset)
3398 crtc->funcs->reset(crtc);
3399
3400 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3401 if (encoder->funcs->reset)
3402 encoder->funcs->reset(encoder);
3403
3404 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3405 if (connector->funcs->reset)
3406 connector->funcs->reset(connector);
3407}
3408EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003409
3410int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3411 void *data, struct drm_file *file_priv)
3412{
3413 struct drm_mode_create_dumb *args = data;
3414
3415 if (!dev->driver->dumb_create)
3416 return -ENOSYS;
3417 return dev->driver->dumb_create(file_priv, dev, args);
3418}
3419
3420int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3421 void *data, struct drm_file *file_priv)
3422{
3423 struct drm_mode_map_dumb *args = data;
3424
3425 /* call driver ioctl to get mmap offset */
3426 if (!dev->driver->dumb_map_offset)
3427 return -ENOSYS;
3428
3429 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3430}
3431
3432int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3433 void *data, struct drm_file *file_priv)
3434{
3435 struct drm_mode_destroy_dumb *args = data;
3436
3437 if (!dev->driver->dumb_destroy)
3438 return -ENOSYS;
3439
3440 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3441}
Dave Airlie248dbc22011-11-29 20:02:54 +00003442
3443/*
3444 * Just need to support RGB formats here for compat with code that doesn't
3445 * use pixel formats directly yet.
3446 */
3447void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3448 int *bpp)
3449{
3450 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003451 case DRM_FORMAT_RGB332:
3452 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003453 *depth = 8;
3454 *bpp = 8;
3455 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003456 case DRM_FORMAT_XRGB1555:
3457 case DRM_FORMAT_XBGR1555:
3458 case DRM_FORMAT_RGBX5551:
3459 case DRM_FORMAT_BGRX5551:
3460 case DRM_FORMAT_ARGB1555:
3461 case DRM_FORMAT_ABGR1555:
3462 case DRM_FORMAT_RGBA5551:
3463 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003464 *depth = 15;
3465 *bpp = 16;
3466 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003467 case DRM_FORMAT_RGB565:
3468 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003469 *depth = 16;
3470 *bpp = 16;
3471 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003472 case DRM_FORMAT_RGB888:
3473 case DRM_FORMAT_BGR888:
3474 *depth = 24;
3475 *bpp = 24;
3476 break;
3477 case DRM_FORMAT_XRGB8888:
3478 case DRM_FORMAT_XBGR8888:
3479 case DRM_FORMAT_RGBX8888:
3480 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003481 *depth = 24;
3482 *bpp = 32;
3483 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003484 case DRM_FORMAT_XRGB2101010:
3485 case DRM_FORMAT_XBGR2101010:
3486 case DRM_FORMAT_RGBX1010102:
3487 case DRM_FORMAT_BGRX1010102:
3488 case DRM_FORMAT_ARGB2101010:
3489 case DRM_FORMAT_ABGR2101010:
3490 case DRM_FORMAT_RGBA1010102:
3491 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003492 *depth = 30;
3493 *bpp = 32;
3494 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003495 case DRM_FORMAT_ARGB8888:
3496 case DRM_FORMAT_ABGR8888:
3497 case DRM_FORMAT_RGBA8888:
3498 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003499 *depth = 32;
3500 *bpp = 32;
3501 break;
3502 default:
3503 DRM_DEBUG_KMS("unsupported pixel format\n");
3504 *depth = 0;
3505 *bpp = 0;
3506 break;
3507 }
3508}
3509EXPORT_SYMBOL(drm_fb_get_bpp_depth);
Ville Syrjälä141670e2012-04-05 21:35:15 +03003510
3511/**
3512 * drm_format_num_planes - get the number of planes for format
3513 * @format: pixel format (DRM_FORMAT_*)
3514 *
3515 * RETURNS:
3516 * The number of planes used by the specified pixel format.
3517 */
3518int drm_format_num_planes(uint32_t format)
3519{
3520 switch (format) {
3521 case DRM_FORMAT_YUV410:
3522 case DRM_FORMAT_YVU410:
3523 case DRM_FORMAT_YUV411:
3524 case DRM_FORMAT_YVU411:
3525 case DRM_FORMAT_YUV420:
3526 case DRM_FORMAT_YVU420:
3527 case DRM_FORMAT_YUV422:
3528 case DRM_FORMAT_YVU422:
3529 case DRM_FORMAT_YUV444:
3530 case DRM_FORMAT_YVU444:
3531 return 3;
3532 case DRM_FORMAT_NV12:
3533 case DRM_FORMAT_NV21:
3534 case DRM_FORMAT_NV16:
3535 case DRM_FORMAT_NV61:
3536 return 2;
3537 default:
3538 return 1;
3539 }
3540}
3541EXPORT_SYMBOL(drm_format_num_planes);
Ville Syrjälä5a86bd52012-04-05 21:35:16 +03003542
3543/**
3544 * drm_format_plane_cpp - determine the bytes per pixel value
3545 * @format: pixel format (DRM_FORMAT_*)
3546 * @plane: plane index
3547 *
3548 * RETURNS:
3549 * The bytes per pixel value for the specified plane.
3550 */
3551int drm_format_plane_cpp(uint32_t format, int plane)
3552{
3553 unsigned int depth;
3554 int bpp;
3555
3556 if (plane >= drm_format_num_planes(format))
3557 return 0;
3558
3559 switch (format) {
3560 case DRM_FORMAT_YUYV:
3561 case DRM_FORMAT_YVYU:
3562 case DRM_FORMAT_UYVY:
3563 case DRM_FORMAT_VYUY:
3564 return 2;
3565 case DRM_FORMAT_NV12:
3566 case DRM_FORMAT_NV21:
3567 case DRM_FORMAT_NV16:
3568 case DRM_FORMAT_NV61:
3569 return plane ? 2 : 1;
3570 case DRM_FORMAT_YUV410:
3571 case DRM_FORMAT_YVU410:
3572 case DRM_FORMAT_YUV411:
3573 case DRM_FORMAT_YVU411:
3574 case DRM_FORMAT_YUV420:
3575 case DRM_FORMAT_YVU420:
3576 case DRM_FORMAT_YUV422:
3577 case DRM_FORMAT_YVU422:
3578 case DRM_FORMAT_YUV444:
3579 case DRM_FORMAT_YVU444:
3580 return 1;
3581 default:
3582 drm_fb_get_bpp_depth(format, &depth, &bpp);
3583 return bpp >> 3;
3584 }
3585}
3586EXPORT_SYMBOL(drm_format_plane_cpp);
Ville Syrjälä01b68b02012-04-05 21:35:17 +03003587
3588/**
3589 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3590 * @format: pixel format (DRM_FORMAT_*)
3591 *
3592 * RETURNS:
3593 * The horizontal chroma subsampling factor for the
3594 * specified pixel format.
3595 */
3596int drm_format_horz_chroma_subsampling(uint32_t format)
3597{
3598 switch (format) {
3599 case DRM_FORMAT_YUV411:
3600 case DRM_FORMAT_YVU411:
3601 case DRM_FORMAT_YUV410:
3602 case DRM_FORMAT_YVU410:
3603 return 4;
3604 case DRM_FORMAT_YUYV:
3605 case DRM_FORMAT_YVYU:
3606 case DRM_FORMAT_UYVY:
3607 case DRM_FORMAT_VYUY:
3608 case DRM_FORMAT_NV12:
3609 case DRM_FORMAT_NV21:
3610 case DRM_FORMAT_NV16:
3611 case DRM_FORMAT_NV61:
3612 case DRM_FORMAT_YUV422:
3613 case DRM_FORMAT_YVU422:
3614 case DRM_FORMAT_YUV420:
3615 case DRM_FORMAT_YVU420:
3616 return 2;
3617 default:
3618 return 1;
3619 }
3620}
3621EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3622
3623/**
3624 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3625 * @format: pixel format (DRM_FORMAT_*)
3626 *
3627 * RETURNS:
3628 * The vertical chroma subsampling factor for the
3629 * specified pixel format.
3630 */
3631int drm_format_vert_chroma_subsampling(uint32_t format)
3632{
3633 switch (format) {
3634 case DRM_FORMAT_YUV410:
3635 case DRM_FORMAT_YVU410:
3636 return 4;
3637 case DRM_FORMAT_YUV420:
3638 case DRM_FORMAT_YVU420:
3639 case DRM_FORMAT_NV12:
3640 case DRM_FORMAT_NV21:
3641 return 2;
3642 default:
3643 return 1;
3644 }
3645}
3646EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);