blob: d3aaeb6ae2362167f360d3a8d1aa846028714fdd [file] [log] [blame]
Dave Airlief453ba02008-11-07 14:05:41 -08001/*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
5 *
6 * DRM core CRTC related functions
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
17 *
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 *
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
31 */
32#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040034#include <linux/export.h>
Dave Airlief453ba02008-11-07 14:05:41 -080035#include "drm.h"
36#include "drmP.h"
37#include "drm_crtc.h"
Adam Jackson7466f4c2010-03-29 21:43:23 +000038#include "drm_edid.h"
Jesse Barnes308e5bc2011-11-14 14:51:28 -080039#include "drm_fourcc.h"
Dave Airlief453ba02008-11-07 14:05:41 -080040
Dave Airlief453ba02008-11-07 14:05:41 -080041/* Avoid boilerplate. I'm tired of typing. */
42#define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
44 { \
45 int i; \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
49 } \
50 return "(unknown)"; \
51 }
52
53/*
54 * Global properties
55 */
56static struct drm_prop_enum_list drm_dpms_enum_list[] =
57{ { DRM_MODE_DPMS_ON, "On" },
58 { DRM_MODE_DPMS_STANDBY, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
60 { DRM_MODE_DPMS_OFF, "Off" }
61};
62
63DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
64
65/*
66 * Optional properties
67 */
68static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69{
Jesse Barnes53bd8382009-07-01 10:04:40 -070070 { DRM_MODE_SCALE_NONE, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
72 { DRM_MODE_SCALE_CENTER, "Center" },
73 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080074};
75
76static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77{
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100080 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080081};
82
83/*
84 * Non-global properties, but "required" for certain connectors.
85 */
86static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87{
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
91};
92
93DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96{
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
100};
101
102DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106{
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800112};
113
114DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117{
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800123};
124
125DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000128static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
132};
133
134DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
Dave Airlief453ba02008-11-07 14:05:41 -0800137struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141};
142
143/*
144 * Connector and encoder types.
145 */
146static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500156 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800163};
164
165static struct drm_prop_enum_list drm_encoder_enum_list[] =
166{ { DRM_MODE_ENCODER_NONE, "None" },
167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800172};
173
174char *drm_get_encoder_name(struct drm_encoder *encoder)
175{
176 static char buf[32];
177
178 snprintf(buf, 32, "%s-%d",
179 drm_encoder_enum_list[encoder->encoder_type].name,
180 encoder->base.id);
181 return buf;
182}
Dave Airlie13a81952009-09-07 15:45:33 +1000183EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800184
185char *drm_get_connector_name(struct drm_connector *connector)
186{
187 static char buf[32];
188
189 snprintf(buf, 32, "%s-%d",
190 drm_connector_enum_list[connector->connector_type].name,
191 connector->connector_type_id);
192 return buf;
193}
194EXPORT_SYMBOL(drm_get_connector_name);
195
196char *drm_get_connector_status_name(enum drm_connector_status status)
197{
198 if (status == connector_status_connected)
199 return "connected";
200 else if (status == connector_status_disconnected)
201 return "disconnected";
202 else
203 return "unknown";
204}
205
206/**
207 * drm_mode_object_get - allocate a new identifier
208 * @dev: DRM device
209 * @ptr: object pointer, used to generate unique ID
210 * @type: object type
211 *
212 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800213 *
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
216 *
217 * RETURNS:
218 * New unique (relative to other objects in @dev) integer identifier for the
219 * object.
220 */
221static int drm_mode_object_get(struct drm_device *dev,
222 struct drm_mode_object *obj, uint32_t obj_type)
223{
224 int new_id = 0;
225 int ret;
226
Dave Airlief453ba02008-11-07 14:05:41 -0800227again:
228 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
229 DRM_ERROR("Ran out memory getting a mode number\n");
230 return -EINVAL;
231 }
232
Jesse Barnesad2563c2009-01-19 17:21:45 +1000233 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800234 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000235 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800236 if (ret == -EAGAIN)
237 goto again;
238
239 obj->id = new_id;
240 obj->type = obj_type;
241 return 0;
242}
243
244/**
245 * drm_mode_object_put - free an identifer
246 * @dev: DRM device
247 * @id: ID to free
248 *
249 * LOCKING:
250 * Caller must hold DRM mode_config lock.
251 *
252 * Free @id from @dev's unique identifier pool.
253 */
254static void drm_mode_object_put(struct drm_device *dev,
255 struct drm_mode_object *object)
256{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000257 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800258 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000259 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800260}
261
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200262struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
263 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800264{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000265 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800266
Jesse Barnesad2563c2009-01-19 17:21:45 +1000267 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800268 obj = idr_find(&dev->mode_config.crtc_idr, id);
269 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000270 obj = NULL;
271 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800272
273 return obj;
274}
275EXPORT_SYMBOL(drm_mode_object_find);
276
277/**
Dave Airlief453ba02008-11-07 14:05:41 -0800278 * drm_framebuffer_init - initialize a framebuffer
279 * @dev: DRM device
280 *
281 * LOCKING:
282 * Caller must hold mode config lock.
283 *
284 * Allocates an ID for the framebuffer's parent mode object, sets its mode
285 * functions & device file and adds it to the master fd list.
286 *
287 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200288 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800289 */
290int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
291 const struct drm_framebuffer_funcs *funcs)
292{
293 int ret;
294
295 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200296 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -0800297 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800298
299 fb->dev = dev;
300 fb->funcs = funcs;
301 dev->mode_config.num_fb++;
302 list_add(&fb->head, &dev->mode_config.fb_list);
303
304 return 0;
305}
306EXPORT_SYMBOL(drm_framebuffer_init);
307
308/**
309 * drm_framebuffer_cleanup - remove a framebuffer object
310 * @fb: framebuffer to remove
311 *
312 * LOCKING:
313 * Caller must hold mode config lock.
314 *
315 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
316 * it, setting it to NULL.
317 */
318void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
319{
320 struct drm_device *dev = fb->dev;
321 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800322 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000323 struct drm_mode_set set;
324 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800325
326 /* remove from any CRTC */
327 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000328 if (crtc->fb == fb) {
329 /* should turn off the crtc */
330 memset(&set, 0, sizeof(struct drm_mode_set));
331 set.crtc = crtc;
332 set.fb = NULL;
333 ret = crtc->funcs->set_config(&set);
334 if (ret)
335 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
336 }
Dave Airlief453ba02008-11-07 14:05:41 -0800337 }
338
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800339 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
340 if (plane->fb == fb) {
341 /* should turn off the crtc */
342 ret = plane->funcs->disable_plane(plane);
343 if (ret)
344 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600345 /* disconnect the plane from the fb and crtc: */
346 plane->fb = NULL;
347 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800348 }
349 }
350
Dave Airlief453ba02008-11-07 14:05:41 -0800351 drm_mode_object_put(dev, &fb->base);
352 list_del(&fb->head);
353 dev->mode_config.num_fb--;
354}
355EXPORT_SYMBOL(drm_framebuffer_cleanup);
356
357/**
358 * drm_crtc_init - Initialise a new CRTC object
359 * @dev: DRM device
360 * @crtc: CRTC object to init
361 * @funcs: callbacks for the new CRTC
362 *
363 * LOCKING:
364 * Caller must hold mode config lock.
365 *
366 * Inits a new object created as base part of an driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200367 *
368 * RETURNS:
369 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800370 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200371int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -0800372 const struct drm_crtc_funcs *funcs)
373{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200374 int ret;
375
Dave Airlief453ba02008-11-07 14:05:41 -0800376 crtc->dev = dev;
377 crtc->funcs = funcs;
378
379 mutex_lock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200380
381 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
382 if (ret)
383 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800384
385 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
386 dev->mode_config.num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200387
388 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800389 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200390
391 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800392}
393EXPORT_SYMBOL(drm_crtc_init);
394
395/**
396 * drm_crtc_cleanup - Cleans up the core crtc usage.
397 * @crtc: CRTC to cleanup
398 *
399 * LOCKING:
400 * Caller must hold mode config lock.
401 *
402 * Cleanup @crtc. Removes from drm modesetting space
403 * does NOT free object, caller does that.
404 */
405void drm_crtc_cleanup(struct drm_crtc *crtc)
406{
407 struct drm_device *dev = crtc->dev;
408
409 if (crtc->gamma_store) {
410 kfree(crtc->gamma_store);
411 crtc->gamma_store = NULL;
412 }
413
414 drm_mode_object_put(dev, &crtc->base);
415 list_del(&crtc->head);
416 dev->mode_config.num_crtc--;
417}
418EXPORT_SYMBOL(drm_crtc_cleanup);
419
420/**
421 * drm_mode_probed_add - add a mode to a connector's probed mode list
422 * @connector: connector the new mode
423 * @mode: mode data
424 *
425 * LOCKING:
426 * Caller must hold mode config lock.
427 *
428 * Add @mode to @connector's mode list for later use.
429 */
430void drm_mode_probed_add(struct drm_connector *connector,
431 struct drm_display_mode *mode)
432{
433 list_add(&mode->head, &connector->probed_modes);
434}
435EXPORT_SYMBOL(drm_mode_probed_add);
436
437/**
438 * drm_mode_remove - remove and free a mode
439 * @connector: connector list to modify
440 * @mode: mode to remove
441 *
442 * LOCKING:
443 * Caller must hold mode config lock.
444 *
445 * Remove @mode from @connector's mode list, then free it.
446 */
447void drm_mode_remove(struct drm_connector *connector,
448 struct drm_display_mode *mode)
449{
450 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100451 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800452}
453EXPORT_SYMBOL(drm_mode_remove);
454
455/**
456 * drm_connector_init - Init a preallocated connector
457 * @dev: DRM device
458 * @connector: the connector to init
459 * @funcs: callbacks for this connector
460 * @name: user visible name of the connector
461 *
462 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100463 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800464 *
465 * Initialises a preallocated connector. Connectors should be
466 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200467 *
468 * RETURNS:
469 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800470 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200471int drm_connector_init(struct drm_device *dev,
472 struct drm_connector *connector,
473 const struct drm_connector_funcs *funcs,
474 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800475{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200476 int ret;
477
Dave Airlief453ba02008-11-07 14:05:41 -0800478 mutex_lock(&dev->mode_config.mutex);
479
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200480 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
481 if (ret)
482 goto out;
483
Dave Airlief453ba02008-11-07 14:05:41 -0800484 connector->dev = dev;
485 connector->funcs = funcs;
Dave Airlief453ba02008-11-07 14:05:41 -0800486 connector->connector_type = connector_type;
487 connector->connector_type_id =
488 ++drm_connector_enum_list[connector_type].count; /* TODO */
489 INIT_LIST_HEAD(&connector->user_modes);
490 INIT_LIST_HEAD(&connector->probed_modes);
491 INIT_LIST_HEAD(&connector->modes);
492 connector->edid_blob_ptr = NULL;
493
494 list_add_tail(&connector->head, &dev->mode_config.connector_list);
495 dev->mode_config.num_connector++;
496
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200497 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
498 drm_connector_attach_property(connector,
499 dev->mode_config.edid_property,
500 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800501
502 drm_connector_attach_property(connector,
503 dev->mode_config.dpms_property, 0);
504
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200505 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800506 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200507
508 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800509}
510EXPORT_SYMBOL(drm_connector_init);
511
512/**
513 * drm_connector_cleanup - cleans up an initialised connector
514 * @connector: connector to cleanup
515 *
516 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +0100517 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -0800518 *
519 * Cleans up the connector but doesn't free the object.
520 */
521void drm_connector_cleanup(struct drm_connector *connector)
522{
523 struct drm_device *dev = connector->dev;
524 struct drm_display_mode *mode, *t;
525
526 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
527 drm_mode_remove(connector, mode);
528
529 list_for_each_entry_safe(mode, t, &connector->modes, head)
530 drm_mode_remove(connector, mode);
531
532 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
533 drm_mode_remove(connector, mode);
534
535 mutex_lock(&dev->mode_config.mutex);
536 drm_mode_object_put(dev, &connector->base);
537 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000538 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800539 mutex_unlock(&dev->mode_config.mutex);
540}
541EXPORT_SYMBOL(drm_connector_cleanup);
542
Dave Airliecbc7e222012-02-20 14:16:40 +0000543void drm_connector_unplug_all(struct drm_device *dev)
544{
545 struct drm_connector *connector;
546
547 /* taking the mode config mutex ends up in a clash with sysfs */
548 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
549 drm_sysfs_connector_remove(connector);
550
551}
552EXPORT_SYMBOL(drm_connector_unplug_all);
553
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200554int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000555 struct drm_encoder *encoder,
556 const struct drm_encoder_funcs *funcs,
557 int encoder_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800558{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200559 int ret;
560
Dave Airlief453ba02008-11-07 14:05:41 -0800561 mutex_lock(&dev->mode_config.mutex);
562
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200563 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
564 if (ret)
565 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -0800566
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200567 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800568 encoder->encoder_type = encoder_type;
569 encoder->funcs = funcs;
570
571 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
572 dev->mode_config.num_encoder++;
573
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200574 out:
Dave Airlief453ba02008-11-07 14:05:41 -0800575 mutex_unlock(&dev->mode_config.mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200576
577 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800578}
579EXPORT_SYMBOL(drm_encoder_init);
580
581void drm_encoder_cleanup(struct drm_encoder *encoder)
582{
583 struct drm_device *dev = encoder->dev;
584 mutex_lock(&dev->mode_config.mutex);
585 drm_mode_object_put(dev, &encoder->base);
586 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000587 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800588 mutex_unlock(&dev->mode_config.mutex);
589}
590EXPORT_SYMBOL(drm_encoder_cleanup);
591
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800592int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
593 unsigned long possible_crtcs,
594 const struct drm_plane_funcs *funcs,
Rob Clark0a7eb242011-12-13 20:19:36 -0600595 const uint32_t *formats, uint32_t format_count,
596 bool priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800597{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200598 int ret;
599
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800600 mutex_lock(&dev->mode_config.mutex);
601
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200602 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
603 if (ret)
604 goto out;
605
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800606 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800607 plane->funcs = funcs;
608 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
609 GFP_KERNEL);
610 if (!plane->format_types) {
611 DRM_DEBUG_KMS("out of memory when allocating plane\n");
612 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200613 ret = -ENOMEM;
614 goto out;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800615 }
616
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800617 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800618 plane->format_count = format_count;
619 plane->possible_crtcs = possible_crtcs;
620
Rob Clark0a7eb242011-12-13 20:19:36 -0600621 /* private planes are not exposed to userspace, but depending on
622 * display hardware, might be convenient to allow sharing programming
623 * for the scanout engine with the crtc implementation.
624 */
625 if (!priv) {
626 list_add_tail(&plane->head, &dev->mode_config.plane_list);
627 dev->mode_config.num_plane++;
628 } else {
629 INIT_LIST_HEAD(&plane->head);
630 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800631
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200632 out:
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800633 mutex_unlock(&dev->mode_config.mutex);
634
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200635 return ret;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800636}
637EXPORT_SYMBOL(drm_plane_init);
638
639void drm_plane_cleanup(struct drm_plane *plane)
640{
641 struct drm_device *dev = plane->dev;
642
643 mutex_lock(&dev->mode_config.mutex);
644 kfree(plane->format_types);
645 drm_mode_object_put(dev, &plane->base);
Rob Clark0a7eb242011-12-13 20:19:36 -0600646 /* if not added to a list, it must be a private plane */
647 if (!list_empty(&plane->head)) {
648 list_del(&plane->head);
649 dev->mode_config.num_plane--;
650 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800651 mutex_unlock(&dev->mode_config.mutex);
652}
653EXPORT_SYMBOL(drm_plane_cleanup);
654
Dave Airlief453ba02008-11-07 14:05:41 -0800655/**
656 * drm_mode_create - create a new display mode
657 * @dev: DRM device
658 *
659 * LOCKING:
660 * Caller must hold DRM mode_config lock.
661 *
662 * Create a new drm_display_mode, give it an ID, and return it.
663 *
664 * RETURNS:
665 * Pointer to new mode on success, NULL on error.
666 */
667struct drm_display_mode *drm_mode_create(struct drm_device *dev)
668{
669 struct drm_display_mode *nmode;
670
671 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
672 if (!nmode)
673 return NULL;
674
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200675 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
676 kfree(nmode);
677 return NULL;
678 }
679
Dave Airlief453ba02008-11-07 14:05:41 -0800680 return nmode;
681}
682EXPORT_SYMBOL(drm_mode_create);
683
684/**
685 * drm_mode_destroy - remove a mode
686 * @dev: DRM device
687 * @mode: mode to remove
688 *
689 * LOCKING:
690 * Caller must hold mode config lock.
691 *
692 * Free @mode's unique identifier, then free it.
693 */
694void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
695{
Ville Syrjäläee34ab52012-03-13 12:35:43 +0200696 if (!mode)
697 return;
698
Dave Airlief453ba02008-11-07 14:05:41 -0800699 drm_mode_object_put(dev, &mode->base);
700
701 kfree(mode);
702}
703EXPORT_SYMBOL(drm_mode_destroy);
704
705static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
706{
707 struct drm_property *edid;
708 struct drm_property *dpms;
Dave Airlief453ba02008-11-07 14:05:41 -0800709
710 /*
711 * Standard properties (apply to all connectors)
712 */
713 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
714 DRM_MODE_PROP_IMMUTABLE,
715 "EDID", 0);
716 dev->mode_config.edid_property = edid;
717
Sascha Hauer4a67d392012-02-06 10:58:17 +0100718 dpms = drm_property_create_enum(dev, 0,
719 "DPMS", drm_dpms_enum_list,
720 ARRAY_SIZE(drm_dpms_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800721 dev->mode_config.dpms_property = dpms;
722
723 return 0;
724}
725
726/**
727 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
728 * @dev: DRM device
729 *
730 * Called by a driver the first time a DVI-I connector is made.
731 */
732int drm_mode_create_dvi_i_properties(struct drm_device *dev)
733{
734 struct drm_property *dvi_i_selector;
735 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -0800736
737 if (dev->mode_config.dvi_i_select_subconnector_property)
738 return 0;
739
740 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100741 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800742 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100743 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800744 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800745 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
746
Sascha Hauer4a67d392012-02-06 10:58:17 +0100747 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -0800748 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100749 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800750 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800751 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
752
753 return 0;
754}
755EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
756
757/**
758 * drm_create_tv_properties - create TV specific connector properties
759 * @dev: DRM device
760 * @num_modes: number of different TV formats (modes) supported
761 * @modes: array of pointers to strings containing name of each format
762 *
763 * Called by a driver's TV initialization routine, this function creates
764 * the TV specific connector properties for a given device. Caller is
765 * responsible for allocating a list of format names and passing them to
766 * this routine.
767 */
768int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
769 char *modes[])
770{
771 struct drm_property *tv_selector;
772 struct drm_property *tv_subconnector;
773 int i;
774
775 if (dev->mode_config.tv_select_subconnector_property)
776 return 0;
777
778 /*
779 * Basic connector properties
780 */
Sascha Hauer4a67d392012-02-06 10:58:17 +0100781 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -0800782 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100783 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800784 ARRAY_SIZE(drm_tv_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800785 dev->mode_config.tv_select_subconnector_property = tv_selector;
786
787 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100788 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
789 "subconnector",
790 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800791 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800792 dev->mode_config.tv_subconnector_property = tv_subconnector;
793
794 /*
795 * Other, TV specific properties: margins & TV modes.
796 */
797 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100798 drm_property_create_range(dev, 0, "left margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800799
800 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100801 drm_property_create_range(dev, 0, "right margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800802
803 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100804 drm_property_create_range(dev, 0, "top margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800805
806 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100807 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Dave Airlief453ba02008-11-07 14:05:41 -0800808
809 dev->mode_config.tv_mode_property =
810 drm_property_create(dev, DRM_MODE_PROP_ENUM,
811 "mode", num_modes);
812 for (i = 0; i < num_modes; i++)
813 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
814 i, modes[i]);
815
Francisco Jerezb6b79022009-08-02 04:19:20 +0200816 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100817 drm_property_create_range(dev, 0, "brightness", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200818
819 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100820 drm_property_create_range(dev, 0, "contrast", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200821
822 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100823 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Francisco Jerezb6b79022009-08-02 04:19:20 +0200824
Francisco Jereza75f0232009-08-12 02:30:10 +0200825 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100826 drm_property_create_range(dev, 0, "overscan", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200827
828 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100829 drm_property_create_range(dev, 0, "saturation", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200830
831 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +0100832 drm_property_create_range(dev, 0, "hue", 0, 100);
Francisco Jereza75f0232009-08-12 02:30:10 +0200833
Dave Airlief453ba02008-11-07 14:05:41 -0800834 return 0;
835}
836EXPORT_SYMBOL(drm_mode_create_tv_properties);
837
838/**
839 * drm_mode_create_scaling_mode_property - create scaling mode property
840 * @dev: DRM device
841 *
842 * Called by a driver the first time it's needed, must be attached to desired
843 * connectors.
844 */
845int drm_mode_create_scaling_mode_property(struct drm_device *dev)
846{
847 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800848
849 if (dev->mode_config.scaling_mode_property)
850 return 0;
851
852 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100853 drm_property_create_enum(dev, 0, "scaling mode",
854 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800855 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800856
857 dev->mode_config.scaling_mode_property = scaling_mode;
858
859 return 0;
860}
861EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
862
863/**
864 * drm_mode_create_dithering_property - create dithering property
865 * @dev: DRM device
866 *
867 * Called by a driver the first time it's needed, must be attached to desired
868 * connectors.
869 */
870int drm_mode_create_dithering_property(struct drm_device *dev)
871{
872 struct drm_property *dithering_mode;
Dave Airlief453ba02008-11-07 14:05:41 -0800873
874 if (dev->mode_config.dithering_mode_property)
875 return 0;
876
877 dithering_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100878 drm_property_create_enum(dev, 0, "dithering",
879 drm_dithering_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -0800880 ARRAY_SIZE(drm_dithering_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -0800881 dev->mode_config.dithering_mode_property = dithering_mode;
882
883 return 0;
884}
885EXPORT_SYMBOL(drm_mode_create_dithering_property);
886
887/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000888 * drm_mode_create_dirty_property - create dirty property
889 * @dev: DRM device
890 *
891 * Called by a driver the first time it's needed, must be attached to desired
892 * connectors.
893 */
894int drm_mode_create_dirty_info_property(struct drm_device *dev)
895{
896 struct drm_property *dirty_info;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000897
898 if (dev->mode_config.dirty_info_property)
899 return 0;
900
901 dirty_info =
Sascha Hauer4a67d392012-02-06 10:58:17 +0100902 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000903 "dirty",
Sascha Hauer4a67d392012-02-06 10:58:17 +0100904 drm_dirty_info_enum_list,
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000905 ARRAY_SIZE(drm_dirty_info_enum_list));
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000906 dev->mode_config.dirty_info_property = dirty_info;
907
908 return 0;
909}
910EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
911
912/**
Dave Airlief453ba02008-11-07 14:05:41 -0800913 * drm_mode_config_init - initialize DRM mode_configuration structure
914 * @dev: DRM device
915 *
916 * LOCKING:
917 * None, should happen single threaded at init time.
918 *
919 * Initialize @dev's mode_config structure, used for tracking the graphics
920 * configuration of @dev.
921 */
922void drm_mode_config_init(struct drm_device *dev)
923{
924 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000925 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800926 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800927 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
928 INIT_LIST_HEAD(&dev->mode_config.connector_list);
929 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
930 INIT_LIST_HEAD(&dev->mode_config.property_list);
931 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800932 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800933 idr_init(&dev->mode_config.crtc_idr);
934
935 mutex_lock(&dev->mode_config.mutex);
936 drm_mode_create_standard_connector_properties(dev);
937 mutex_unlock(&dev->mode_config.mutex);
938
939 /* Just to be sure */
940 dev->mode_config.num_fb = 0;
941 dev->mode_config.num_connector = 0;
942 dev->mode_config.num_crtc = 0;
943 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800944}
945EXPORT_SYMBOL(drm_mode_config_init);
946
947int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
948{
949 uint32_t total_objects = 0;
950
951 total_objects += dev->mode_config.num_crtc;
952 total_objects += dev->mode_config.num_connector;
953 total_objects += dev->mode_config.num_encoder;
954
Dave Airlief453ba02008-11-07 14:05:41 -0800955 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
956 if (!group->id_list)
957 return -ENOMEM;
958
959 group->num_crtcs = 0;
960 group->num_connectors = 0;
961 group->num_encoders = 0;
962 return 0;
963}
964
965int drm_mode_group_init_legacy_group(struct drm_device *dev,
966 struct drm_mode_group *group)
967{
968 struct drm_crtc *crtc;
969 struct drm_encoder *encoder;
970 struct drm_connector *connector;
971 int ret;
972
973 if ((ret = drm_mode_group_init(dev, group)))
974 return ret;
975
976 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
977 group->id_list[group->num_crtcs++] = crtc->base.id;
978
979 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
980 group->id_list[group->num_crtcs + group->num_encoders++] =
981 encoder->base.id;
982
983 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
984 group->id_list[group->num_crtcs + group->num_encoders +
985 group->num_connectors++] = connector->base.id;
986
987 return 0;
988}
Dave Airlie9c1dfc52012-03-20 06:59:29 +0000989EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
Dave Airlief453ba02008-11-07 14:05:41 -0800990
991/**
992 * drm_mode_config_cleanup - free up DRM mode_config info
993 * @dev: DRM device
994 *
995 * LOCKING:
996 * Caller must hold mode config lock.
997 *
998 * Free up all the connectors and CRTCs associated with this DRM device, then
999 * free up the framebuffers and associated buffer objects.
1000 *
1001 * FIXME: cleanup any dangling user buffer objects too
1002 */
1003void drm_mode_config_cleanup(struct drm_device *dev)
1004{
1005 struct drm_connector *connector, *ot;
1006 struct drm_crtc *crtc, *ct;
1007 struct drm_encoder *encoder, *enct;
1008 struct drm_framebuffer *fb, *fbt;
1009 struct drm_property *property, *pt;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001010 struct drm_plane *plane, *plt;
Dave Airlief453ba02008-11-07 14:05:41 -08001011
1012 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1013 head) {
1014 encoder->funcs->destroy(encoder);
1015 }
1016
1017 list_for_each_entry_safe(connector, ot,
1018 &dev->mode_config.connector_list, head) {
1019 connector->funcs->destroy(connector);
1020 }
1021
1022 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1023 head) {
1024 drm_property_destroy(dev, property);
1025 }
1026
1027 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1028 fb->funcs->destroy(fb);
1029 }
1030
1031 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1032 crtc->funcs->destroy(crtc);
1033 }
1034
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001035 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1036 head) {
1037 plane->funcs->destroy(plane);
1038 }
Sascha Hauer59ce0622012-02-01 11:38:20 +01001039
1040 idr_remove_all(&dev->mode_config.crtc_idr);
1041 idr_destroy(&dev->mode_config.crtc_idr);
Dave Airlief453ba02008-11-07 14:05:41 -08001042}
1043EXPORT_SYMBOL(drm_mode_config_cleanup);
1044
Dave Airlief453ba02008-11-07 14:05:41 -08001045/**
1046 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1047 * @out: drm_mode_modeinfo struct to return to the user
1048 * @in: drm_display_mode to use
1049 *
1050 * LOCKING:
1051 * None.
1052 *
1053 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1054 * the user.
1055 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001056static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1057 const struct drm_display_mode *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001058{
Ville Syrjäläe36fae32012-03-13 12:35:40 +02001059 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1060 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1061 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1062 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1063 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1064 "timing values too large for mode info\n");
1065
Dave Airlief453ba02008-11-07 14:05:41 -08001066 out->clock = in->clock;
1067 out->hdisplay = in->hdisplay;
1068 out->hsync_start = in->hsync_start;
1069 out->hsync_end = in->hsync_end;
1070 out->htotal = in->htotal;
1071 out->hskew = in->hskew;
1072 out->vdisplay = in->vdisplay;
1073 out->vsync_start = in->vsync_start;
1074 out->vsync_end = in->vsync_end;
1075 out->vtotal = in->vtotal;
1076 out->vscan = in->vscan;
1077 out->vrefresh = in->vrefresh;
1078 out->flags = in->flags;
1079 out->type = in->type;
1080 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1081 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1082}
1083
1084/**
1085 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1086 * @out: drm_display_mode to return to the user
1087 * @in: drm_mode_modeinfo to use
1088 *
1089 * LOCKING:
1090 * None.
1091 *
1092 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1093 * the caller.
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001094 *
1095 * RETURNS:
1096 * Zero on success, errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001097 */
Ville Syrjälä93bbf6d2012-03-13 12:35:47 +02001098static int drm_crtc_convert_umode(struct drm_display_mode *out,
1099 const struct drm_mode_modeinfo *in)
Dave Airlief453ba02008-11-07 14:05:41 -08001100{
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001101 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1102 return -ERANGE;
1103
Dave Airlief453ba02008-11-07 14:05:41 -08001104 out->clock = in->clock;
1105 out->hdisplay = in->hdisplay;
1106 out->hsync_start = in->hsync_start;
1107 out->hsync_end = in->hsync_end;
1108 out->htotal = in->htotal;
1109 out->hskew = in->hskew;
1110 out->vdisplay = in->vdisplay;
1111 out->vsync_start = in->vsync_start;
1112 out->vsync_end = in->vsync_end;
1113 out->vtotal = in->vtotal;
1114 out->vscan = in->vscan;
1115 out->vrefresh = in->vrefresh;
1116 out->flags = in->flags;
1117 out->type = in->type;
1118 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1119 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001120
1121 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001122}
1123
1124/**
1125 * drm_mode_getresources - get graphics configuration
1126 * @inode: inode from the ioctl
1127 * @filp: file * from the ioctl
1128 * @cmd: cmd from ioctl
1129 * @arg: arg from ioctl
1130 *
1131 * LOCKING:
1132 * Takes mode config lock.
1133 *
1134 * Construct a set of configuration description structures and return
1135 * them to the user, including CRTC, connector and framebuffer configuration.
1136 *
1137 * Called by the user via ioctl.
1138 *
1139 * RETURNS:
1140 * Zero on success, errno on failure.
1141 */
1142int drm_mode_getresources(struct drm_device *dev, void *data,
1143 struct drm_file *file_priv)
1144{
1145 struct drm_mode_card_res *card_res = data;
1146 struct list_head *lh;
1147 struct drm_framebuffer *fb;
1148 struct drm_connector *connector;
1149 struct drm_crtc *crtc;
1150 struct drm_encoder *encoder;
1151 int ret = 0;
1152 int connector_count = 0;
1153 int crtc_count = 0;
1154 int fb_count = 0;
1155 int encoder_count = 0;
1156 int copied = 0, i;
1157 uint32_t __user *fb_id;
1158 uint32_t __user *crtc_id;
1159 uint32_t __user *connector_id;
1160 uint32_t __user *encoder_id;
1161 struct drm_mode_group *mode_group;
1162
Dave Airliefb3b06c2011-02-08 13:55:21 +10001163 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1164 return -EINVAL;
1165
Dave Airlief453ba02008-11-07 14:05:41 -08001166 mutex_lock(&dev->mode_config.mutex);
1167
1168 /*
1169 * For the non-control nodes we need to limit the list of resources
1170 * by IDs in the group list for this node
1171 */
1172 list_for_each(lh, &file_priv->fbs)
1173 fb_count++;
1174
1175 mode_group = &file_priv->master->minor->mode_group;
1176 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1177
1178 list_for_each(lh, &dev->mode_config.crtc_list)
1179 crtc_count++;
1180
1181 list_for_each(lh, &dev->mode_config.connector_list)
1182 connector_count++;
1183
1184 list_for_each(lh, &dev->mode_config.encoder_list)
1185 encoder_count++;
1186 } else {
1187
1188 crtc_count = mode_group->num_crtcs;
1189 connector_count = mode_group->num_connectors;
1190 encoder_count = mode_group->num_encoders;
1191 }
1192
1193 card_res->max_height = dev->mode_config.max_height;
1194 card_res->min_height = dev->mode_config.min_height;
1195 card_res->max_width = dev->mode_config.max_width;
1196 card_res->min_width = dev->mode_config.min_width;
1197
1198 /* handle this in 4 parts */
1199 /* FBs */
1200 if (card_res->count_fbs >= fb_count) {
1201 copied = 0;
1202 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001203 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001204 if (put_user(fb->base.id, fb_id + copied)) {
1205 ret = -EFAULT;
1206 goto out;
1207 }
1208 copied++;
1209 }
1210 }
1211 card_res->count_fbs = fb_count;
1212
1213 /* CRTCs */
1214 if (card_res->count_crtcs >= crtc_count) {
1215 copied = 0;
1216 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1217 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1218 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1219 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001220 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001221 if (put_user(crtc->base.id, crtc_id + copied)) {
1222 ret = -EFAULT;
1223 goto out;
1224 }
1225 copied++;
1226 }
1227 } else {
1228 for (i = 0; i < mode_group->num_crtcs; i++) {
1229 if (put_user(mode_group->id_list[i],
1230 crtc_id + copied)) {
1231 ret = -EFAULT;
1232 goto out;
1233 }
1234 copied++;
1235 }
1236 }
1237 }
1238 card_res->count_crtcs = crtc_count;
1239
1240 /* Encoders */
1241 if (card_res->count_encoders >= encoder_count) {
1242 copied = 0;
1243 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1244 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1245 list_for_each_entry(encoder,
1246 &dev->mode_config.encoder_list,
1247 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001248 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1249 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001250 if (put_user(encoder->base.id, encoder_id +
1251 copied)) {
1252 ret = -EFAULT;
1253 goto out;
1254 }
1255 copied++;
1256 }
1257 } else {
1258 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1259 if (put_user(mode_group->id_list[i],
1260 encoder_id + copied)) {
1261 ret = -EFAULT;
1262 goto out;
1263 }
1264 copied++;
1265 }
1266
1267 }
1268 }
1269 card_res->count_encoders = encoder_count;
1270
1271 /* Connectors */
1272 if (card_res->count_connectors >= connector_count) {
1273 copied = 0;
1274 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1275 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1276 list_for_each_entry(connector,
1277 &dev->mode_config.connector_list,
1278 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001279 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1280 connector->base.id,
1281 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001282 if (put_user(connector->base.id,
1283 connector_id + copied)) {
1284 ret = -EFAULT;
1285 goto out;
1286 }
1287 copied++;
1288 }
1289 } else {
1290 int start = mode_group->num_crtcs +
1291 mode_group->num_encoders;
1292 for (i = start; i < start + mode_group->num_connectors; i++) {
1293 if (put_user(mode_group->id_list[i],
1294 connector_id + copied)) {
1295 ret = -EFAULT;
1296 goto out;
1297 }
1298 copied++;
1299 }
1300 }
1301 }
1302 card_res->count_connectors = connector_count;
1303
Jerome Glisse94401062010-07-15 15:43:25 -04001304 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001305 card_res->count_connectors, card_res->count_encoders);
1306
1307out:
1308 mutex_unlock(&dev->mode_config.mutex);
1309 return ret;
1310}
1311
1312/**
1313 * drm_mode_getcrtc - get CRTC configuration
1314 * @inode: inode from the ioctl
1315 * @filp: file * from the ioctl
1316 * @cmd: cmd from ioctl
1317 * @arg: arg from ioctl
1318 *
1319 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001320 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001321 *
1322 * Construct a CRTC configuration structure to return to the user.
1323 *
1324 * Called by the user via ioctl.
1325 *
1326 * RETURNS:
1327 * Zero on success, errno on failure.
1328 */
1329int drm_mode_getcrtc(struct drm_device *dev,
1330 void *data, struct drm_file *file_priv)
1331{
1332 struct drm_mode_crtc *crtc_resp = data;
1333 struct drm_crtc *crtc;
1334 struct drm_mode_object *obj;
1335 int ret = 0;
1336
Dave Airliefb3b06c2011-02-08 13:55:21 +10001337 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1338 return -EINVAL;
1339
Dave Airlief453ba02008-11-07 14:05:41 -08001340 mutex_lock(&dev->mode_config.mutex);
1341
1342 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1343 DRM_MODE_OBJECT_CRTC);
1344 if (!obj) {
1345 ret = -EINVAL;
1346 goto out;
1347 }
1348 crtc = obj_to_crtc(obj);
1349
1350 crtc_resp->x = crtc->x;
1351 crtc_resp->y = crtc->y;
1352 crtc_resp->gamma_size = crtc->gamma_size;
1353 if (crtc->fb)
1354 crtc_resp->fb_id = crtc->fb->base.id;
1355 else
1356 crtc_resp->fb_id = 0;
1357
1358 if (crtc->enabled) {
1359
1360 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1361 crtc_resp->mode_valid = 1;
1362
1363 } else {
1364 crtc_resp->mode_valid = 0;
1365 }
1366
1367out:
1368 mutex_unlock(&dev->mode_config.mutex);
1369 return ret;
1370}
1371
1372/**
1373 * drm_mode_getconnector - get connector configuration
1374 * @inode: inode from the ioctl
1375 * @filp: file * from the ioctl
1376 * @cmd: cmd from ioctl
1377 * @arg: arg from ioctl
1378 *
1379 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001380 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001381 *
1382 * Construct a connector configuration structure to return to the user.
1383 *
1384 * Called by the user via ioctl.
1385 *
1386 * RETURNS:
1387 * Zero on success, errno on failure.
1388 */
1389int drm_mode_getconnector(struct drm_device *dev, void *data,
1390 struct drm_file *file_priv)
1391{
1392 struct drm_mode_get_connector *out_resp = data;
1393 struct drm_mode_object *obj;
1394 struct drm_connector *connector;
1395 struct drm_display_mode *mode;
1396 int mode_count = 0;
1397 int props_count = 0;
1398 int encoders_count = 0;
1399 int ret = 0;
1400 int copied = 0;
1401 int i;
1402 struct drm_mode_modeinfo u_mode;
1403 struct drm_mode_modeinfo __user *mode_ptr;
1404 uint32_t __user *prop_ptr;
1405 uint64_t __user *prop_values;
1406 uint32_t __user *encoder_ptr;
1407
Dave Airliefb3b06c2011-02-08 13:55:21 +10001408 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1409 return -EINVAL;
1410
Dave Airlief453ba02008-11-07 14:05:41 -08001411 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1412
Jerome Glisse94401062010-07-15 15:43:25 -04001413 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001414
1415 mutex_lock(&dev->mode_config.mutex);
1416
1417 obj = drm_mode_object_find(dev, out_resp->connector_id,
1418 DRM_MODE_OBJECT_CONNECTOR);
1419 if (!obj) {
1420 ret = -EINVAL;
1421 goto out;
1422 }
1423 connector = obj_to_connector(obj);
1424
1425 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1426 if (connector->property_ids[i] != 0) {
1427 props_count++;
1428 }
1429 }
1430
1431 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1432 if (connector->encoder_ids[i] != 0) {
1433 encoders_count++;
1434 }
1435 }
1436
1437 if (out_resp->count_modes == 0) {
1438 connector->funcs->fill_modes(connector,
1439 dev->mode_config.max_width,
1440 dev->mode_config.max_height);
1441 }
1442
1443 /* delayed so we get modes regardless of pre-fill_modes state */
1444 list_for_each_entry(mode, &connector->modes, head)
1445 mode_count++;
1446
1447 out_resp->connector_id = connector->base.id;
1448 out_resp->connector_type = connector->connector_type;
1449 out_resp->connector_type_id = connector->connector_type_id;
1450 out_resp->mm_width = connector->display_info.width_mm;
1451 out_resp->mm_height = connector->display_info.height_mm;
1452 out_resp->subpixel = connector->display_info.subpixel_order;
1453 out_resp->connection = connector->status;
1454 if (connector->encoder)
1455 out_resp->encoder_id = connector->encoder->base.id;
1456 else
1457 out_resp->encoder_id = 0;
1458
1459 /*
1460 * This ioctl is called twice, once to determine how much space is
1461 * needed, and the 2nd time to fill it.
1462 */
1463 if ((out_resp->count_modes >= mode_count) && mode_count) {
1464 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001465 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001466 list_for_each_entry(mode, &connector->modes, head) {
1467 drm_crtc_convert_to_umode(&u_mode, mode);
1468 if (copy_to_user(mode_ptr + copied,
1469 &u_mode, sizeof(u_mode))) {
1470 ret = -EFAULT;
1471 goto out;
1472 }
1473 copied++;
1474 }
1475 }
1476 out_resp->count_modes = mode_count;
1477
1478 if ((out_resp->count_props >= props_count) && props_count) {
1479 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001480 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1481 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001482 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1483 if (connector->property_ids[i] != 0) {
1484 if (put_user(connector->property_ids[i],
1485 prop_ptr + copied)) {
1486 ret = -EFAULT;
1487 goto out;
1488 }
1489
1490 if (put_user(connector->property_values[i],
1491 prop_values + copied)) {
1492 ret = -EFAULT;
1493 goto out;
1494 }
1495 copied++;
1496 }
1497 }
1498 }
1499 out_resp->count_props = props_count;
1500
1501 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1502 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001503 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001504 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1505 if (connector->encoder_ids[i] != 0) {
1506 if (put_user(connector->encoder_ids[i],
1507 encoder_ptr + copied)) {
1508 ret = -EFAULT;
1509 goto out;
1510 }
1511 copied++;
1512 }
1513 }
1514 }
1515 out_resp->count_encoders = encoders_count;
1516
1517out:
1518 mutex_unlock(&dev->mode_config.mutex);
1519 return ret;
1520}
1521
1522int drm_mode_getencoder(struct drm_device *dev, void *data,
1523 struct drm_file *file_priv)
1524{
1525 struct drm_mode_get_encoder *enc_resp = data;
1526 struct drm_mode_object *obj;
1527 struct drm_encoder *encoder;
1528 int ret = 0;
1529
Dave Airliefb3b06c2011-02-08 13:55:21 +10001530 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1531 return -EINVAL;
1532
Dave Airlief453ba02008-11-07 14:05:41 -08001533 mutex_lock(&dev->mode_config.mutex);
1534 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1535 DRM_MODE_OBJECT_ENCODER);
1536 if (!obj) {
1537 ret = -EINVAL;
1538 goto out;
1539 }
1540 encoder = obj_to_encoder(obj);
1541
1542 if (encoder->crtc)
1543 enc_resp->crtc_id = encoder->crtc->base.id;
1544 else
1545 enc_resp->crtc_id = 0;
1546 enc_resp->encoder_type = encoder->encoder_type;
1547 enc_resp->encoder_id = encoder->base.id;
1548 enc_resp->possible_crtcs = encoder->possible_crtcs;
1549 enc_resp->possible_clones = encoder->possible_clones;
1550
1551out:
1552 mutex_unlock(&dev->mode_config.mutex);
1553 return ret;
1554}
1555
1556/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001557 * drm_mode_getplane_res - get plane info
1558 * @dev: DRM device
1559 * @data: ioctl data
1560 * @file_priv: DRM file info
1561 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001562 * LOCKING:
1563 * Takes mode config lock.
1564 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001565 * Return an plane count and set of IDs.
1566 */
1567int drm_mode_getplane_res(struct drm_device *dev, void *data,
1568 struct drm_file *file_priv)
1569{
1570 struct drm_mode_get_plane_res *plane_resp = data;
1571 struct drm_mode_config *config;
1572 struct drm_plane *plane;
1573 uint32_t __user *plane_ptr;
1574 int copied = 0, ret = 0;
1575
1576 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1577 return -EINVAL;
1578
1579 mutex_lock(&dev->mode_config.mutex);
1580 config = &dev->mode_config;
1581
1582 /*
1583 * This ioctl is called twice, once to determine how much space is
1584 * needed, and the 2nd time to fill it.
1585 */
1586 if (config->num_plane &&
1587 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001588 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001589
1590 list_for_each_entry(plane, &config->plane_list, head) {
1591 if (put_user(plane->base.id, plane_ptr + copied)) {
1592 ret = -EFAULT;
1593 goto out;
1594 }
1595 copied++;
1596 }
1597 }
1598 plane_resp->count_planes = config->num_plane;
1599
1600out:
1601 mutex_unlock(&dev->mode_config.mutex);
1602 return ret;
1603}
1604
1605/**
1606 * drm_mode_getplane - get plane info
1607 * @dev: DRM device
1608 * @data: ioctl data
1609 * @file_priv: DRM file info
1610 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001611 * LOCKING:
1612 * Takes mode config lock.
1613 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001614 * Return plane info, including formats supported, gamma size, any
1615 * current fb, etc.
1616 */
1617int drm_mode_getplane(struct drm_device *dev, void *data,
1618 struct drm_file *file_priv)
1619{
1620 struct drm_mode_get_plane *plane_resp = data;
1621 struct drm_mode_object *obj;
1622 struct drm_plane *plane;
1623 uint32_t __user *format_ptr;
1624 int ret = 0;
1625
1626 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1627 return -EINVAL;
1628
1629 mutex_lock(&dev->mode_config.mutex);
1630 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1631 DRM_MODE_OBJECT_PLANE);
1632 if (!obj) {
1633 ret = -ENOENT;
1634 goto out;
1635 }
1636 plane = obj_to_plane(obj);
1637
1638 if (plane->crtc)
1639 plane_resp->crtc_id = plane->crtc->base.id;
1640 else
1641 plane_resp->crtc_id = 0;
1642
1643 if (plane->fb)
1644 plane_resp->fb_id = plane->fb->base.id;
1645 else
1646 plane_resp->fb_id = 0;
1647
1648 plane_resp->plane_id = plane->base.id;
1649 plane_resp->possible_crtcs = plane->possible_crtcs;
1650 plane_resp->gamma_size = plane->gamma_size;
1651
1652 /*
1653 * This ioctl is called twice, once to determine how much space is
1654 * needed, and the 2nd time to fill it.
1655 */
1656 if (plane->format_count &&
1657 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001658 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001659 if (copy_to_user(format_ptr,
1660 plane->format_types,
1661 sizeof(uint32_t) * plane->format_count)) {
1662 ret = -EFAULT;
1663 goto out;
1664 }
1665 }
1666 plane_resp->count_format_types = plane->format_count;
1667
1668out:
1669 mutex_unlock(&dev->mode_config.mutex);
1670 return ret;
1671}
1672
1673/**
1674 * drm_mode_setplane - set up or tear down an plane
1675 * @dev: DRM device
1676 * @data: ioctl data*
1677 * @file_prive: DRM file info
1678 *
Sascha Hauerb20f3862012-02-01 11:38:31 +01001679 * LOCKING:
1680 * Takes mode config lock.
1681 *
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001682 * Set plane info, including placement, fb, scaling, and other factors.
1683 * Or pass a NULL fb to disable.
1684 */
1685int drm_mode_setplane(struct drm_device *dev, void *data,
1686 struct drm_file *file_priv)
1687{
1688 struct drm_mode_set_plane *plane_req = data;
1689 struct drm_mode_object *obj;
1690 struct drm_plane *plane;
1691 struct drm_crtc *crtc;
1692 struct drm_framebuffer *fb;
1693 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001694 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001695 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001696
1697 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1698 return -EINVAL;
1699
1700 mutex_lock(&dev->mode_config.mutex);
1701
1702 /*
1703 * First, find the plane, crtc, and fb objects. If not available,
1704 * we don't bother to call the driver.
1705 */
1706 obj = drm_mode_object_find(dev, plane_req->plane_id,
1707 DRM_MODE_OBJECT_PLANE);
1708 if (!obj) {
1709 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1710 plane_req->plane_id);
1711 ret = -ENOENT;
1712 goto out;
1713 }
1714 plane = obj_to_plane(obj);
1715
1716 /* No fb means shut it down */
1717 if (!plane_req->fb_id) {
1718 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001719 plane->crtc = NULL;
1720 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001721 goto out;
1722 }
1723
1724 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1725 DRM_MODE_OBJECT_CRTC);
1726 if (!obj) {
1727 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1728 plane_req->crtc_id);
1729 ret = -ENOENT;
1730 goto out;
1731 }
1732 crtc = obj_to_crtc(obj);
1733
1734 obj = drm_mode_object_find(dev, plane_req->fb_id,
1735 DRM_MODE_OBJECT_FB);
1736 if (!obj) {
1737 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1738 plane_req->fb_id);
1739 ret = -ENOENT;
1740 goto out;
1741 }
1742 fb = obj_to_fb(obj);
1743
Ville Syrjälä62443be2011-12-20 00:06:47 +02001744 /* Check whether this plane supports the fb pixel format. */
1745 for (i = 0; i < plane->format_count; i++)
1746 if (fb->pixel_format == plane->format_types[i])
1747 break;
1748 if (i == plane->format_count) {
1749 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1750 ret = -EINVAL;
1751 goto out;
1752 }
1753
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001754 fb_width = fb->width << 16;
1755 fb_height = fb->height << 16;
1756
1757 /* Make sure source coordinates are inside the fb. */
1758 if (plane_req->src_w > fb_width ||
1759 plane_req->src_x > fb_width - plane_req->src_w ||
1760 plane_req->src_h > fb_height ||
1761 plane_req->src_y > fb_height - plane_req->src_h) {
1762 DRM_DEBUG_KMS("Invalid source coordinates "
1763 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1764 plane_req->src_w >> 16,
1765 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1766 plane_req->src_h >> 16,
1767 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1768 plane_req->src_x >> 16,
1769 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1770 plane_req->src_y >> 16,
1771 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1772 ret = -ENOSPC;
1773 goto out;
1774 }
1775
Ville Syrjälä687a0402011-12-20 00:06:45 +02001776 /* Give drivers some help against integer overflows */
1777 if (plane_req->crtc_w > INT_MAX ||
1778 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1779 plane_req->crtc_h > INT_MAX ||
1780 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1781 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1782 plane_req->crtc_w, plane_req->crtc_h,
1783 plane_req->crtc_x, plane_req->crtc_y);
1784 ret = -ERANGE;
1785 goto out;
1786 }
1787
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001788 ret = plane->funcs->update_plane(plane, crtc, fb,
1789 plane_req->crtc_x, plane_req->crtc_y,
1790 plane_req->crtc_w, plane_req->crtc_h,
1791 plane_req->src_x, plane_req->src_y,
1792 plane_req->src_w, plane_req->src_h);
1793 if (!ret) {
1794 plane->crtc = crtc;
1795 plane->fb = fb;
1796 }
1797
1798out:
1799 mutex_unlock(&dev->mode_config.mutex);
1800
1801 return ret;
1802}
1803
1804/**
Dave Airlief453ba02008-11-07 14:05:41 -08001805 * drm_mode_setcrtc - set CRTC configuration
1806 * @inode: inode from the ioctl
1807 * @filp: file * from the ioctl
1808 * @cmd: cmd from ioctl
1809 * @arg: arg from ioctl
1810 *
1811 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01001812 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08001813 *
1814 * Build a new CRTC configuration based on user request.
1815 *
1816 * Called by the user via ioctl.
1817 *
1818 * RETURNS:
1819 * Zero on success, errno on failure.
1820 */
1821int drm_mode_setcrtc(struct drm_device *dev, void *data,
1822 struct drm_file *file_priv)
1823{
1824 struct drm_mode_config *config = &dev->mode_config;
1825 struct drm_mode_crtc *crtc_req = data;
1826 struct drm_mode_object *obj;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001827 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001828 struct drm_connector **connector_set = NULL, *connector;
1829 struct drm_framebuffer *fb = NULL;
1830 struct drm_display_mode *mode = NULL;
1831 struct drm_mode_set set;
1832 uint32_t __user *set_connectors_ptr;
1833 int ret = 0;
1834 int i;
1835
Dave Airliefb3b06c2011-02-08 13:55:21 +10001836 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1837 return -EINVAL;
1838
Ville Syrjälä1d97e912012-03-13 12:35:41 +02001839 /* For some reason crtc x/y offsets are signed internally. */
1840 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1841 return -ERANGE;
1842
Dave Airlief453ba02008-11-07 14:05:41 -08001843 mutex_lock(&dev->mode_config.mutex);
1844 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1845 DRM_MODE_OBJECT_CRTC);
1846 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001847 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001848 ret = -EINVAL;
1849 goto out;
1850 }
1851 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001852 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001853
1854 if (crtc_req->mode_valid) {
1855 /* If we have a mode we need a framebuffer. */
1856 /* If we pass -1, set the mode with the currently bound fb */
1857 if (crtc_req->fb_id == -1) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001858 if (!crtc->fb) {
1859 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1860 ret = -EINVAL;
1861 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001862 }
Ville Syrjälä6653cc82012-03-13 12:35:38 +02001863 fb = crtc->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001864 } else {
1865 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1866 DRM_MODE_OBJECT_FB);
1867 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001868 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1869 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001870 ret = -EINVAL;
1871 goto out;
1872 }
1873 fb = obj_to_fb(obj);
1874 }
1875
1876 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001877 if (!mode) {
1878 ret = -ENOMEM;
1879 goto out;
1880 }
1881
Ville Syrjälä90367bf2012-03-13 12:35:44 +02001882 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1883 if (ret) {
1884 DRM_DEBUG_KMS("Invalid mode\n");
1885 goto out;
1886 }
1887
Dave Airlief453ba02008-11-07 14:05:41 -08001888 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02001889
1890 if (mode->hdisplay > fb->width ||
1891 mode->vdisplay > fb->height ||
1892 crtc_req->x > fb->width - mode->hdisplay ||
1893 crtc_req->y > fb->height - mode->vdisplay) {
1894 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1895 mode->hdisplay, mode->vdisplay,
1896 crtc_req->x, crtc_req->y,
1897 fb->width, fb->height);
1898 ret = -ENOSPC;
1899 goto out;
1900 }
Dave Airlief453ba02008-11-07 14:05:41 -08001901 }
1902
1903 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001904 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001905 ret = -EINVAL;
1906 goto out;
1907 }
1908
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001909 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001910 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001911 crtc_req->count_connectors);
1912 ret = -EINVAL;
1913 goto out;
1914 }
1915
1916 if (crtc_req->count_connectors > 0) {
1917 u32 out_id;
1918
1919 /* Avoid unbounded kernel memory allocation */
1920 if (crtc_req->count_connectors > config->num_connector) {
1921 ret = -EINVAL;
1922 goto out;
1923 }
1924
1925 connector_set = kmalloc(crtc_req->count_connectors *
1926 sizeof(struct drm_connector *),
1927 GFP_KERNEL);
1928 if (!connector_set) {
1929 ret = -ENOMEM;
1930 goto out;
1931 }
1932
1933 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001934 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001935 if (get_user(out_id, &set_connectors_ptr[i])) {
1936 ret = -EFAULT;
1937 goto out;
1938 }
1939
1940 obj = drm_mode_object_find(dev, out_id,
1941 DRM_MODE_OBJECT_CONNECTOR);
1942 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001943 DRM_DEBUG_KMS("Connector id %d unknown\n",
1944 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001945 ret = -EINVAL;
1946 goto out;
1947 }
1948 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001949 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1950 connector->base.id,
1951 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001952
1953 connector_set[i] = connector;
1954 }
1955 }
1956
1957 set.crtc = crtc;
1958 set.x = crtc_req->x;
1959 set.y = crtc_req->y;
1960 set.mode = mode;
1961 set.connectors = connector_set;
1962 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001963 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001964 ret = crtc->funcs->set_config(&set);
1965
1966out:
1967 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02001968 drm_mode_destroy(dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08001969 mutex_unlock(&dev->mode_config.mutex);
1970 return ret;
1971}
1972
1973int drm_mode_cursor_ioctl(struct drm_device *dev,
1974 void *data, struct drm_file *file_priv)
1975{
1976 struct drm_mode_cursor *req = data;
1977 struct drm_mode_object *obj;
1978 struct drm_crtc *crtc;
1979 int ret = 0;
1980
Dave Airliefb3b06c2011-02-08 13:55:21 +10001981 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1982 return -EINVAL;
1983
Jesse Barnesacb4b992011-11-07 12:03:23 -08001984 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001985 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001986
1987 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001988 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001989 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001990 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001991 ret = -EINVAL;
1992 goto out;
1993 }
1994 crtc = obj_to_crtc(obj);
1995
1996 if (req->flags & DRM_MODE_CURSOR_BO) {
1997 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001998 ret = -ENXIO;
1999 goto out;
2000 }
2001 /* Turns off the cursor if handle is 0 */
2002 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2003 req->width, req->height);
2004 }
2005
2006 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2007 if (crtc->funcs->cursor_move) {
2008 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2009 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002010 ret = -EFAULT;
2011 goto out;
2012 }
2013 }
2014out:
2015 mutex_unlock(&dev->mode_config.mutex);
2016 return ret;
2017}
2018
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002019/* Original addfb only supported RGB formats, so figure out which one */
2020uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2021{
2022 uint32_t fmt;
2023
2024 switch (bpp) {
2025 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002026 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002027 break;
2028 case 16:
2029 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002030 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002031 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002032 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002033 break;
2034 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002035 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002036 break;
2037 case 32:
2038 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002039 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002040 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002041 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002042 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002043 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 break;
2045 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002046 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2047 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002048 break;
2049 }
2050
2051 return fmt;
2052}
2053EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2054
Dave Airlief453ba02008-11-07 14:05:41 -08002055/**
2056 * drm_mode_addfb - add an FB to the graphics configuration
2057 * @inode: inode from the ioctl
2058 * @filp: file * from the ioctl
2059 * @cmd: cmd from ioctl
2060 * @arg: arg from ioctl
2061 *
2062 * LOCKING:
2063 * Takes mode config lock.
2064 *
2065 * Add a new FB to the specified CRTC, given a user request.
2066 *
2067 * Called by the user via ioctl.
2068 *
2069 * RETURNS:
2070 * Zero on success, errno on failure.
2071 */
2072int drm_mode_addfb(struct drm_device *dev,
2073 void *data, struct drm_file *file_priv)
2074{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002075 struct drm_mode_fb_cmd *or = data;
2076 struct drm_mode_fb_cmd2 r = {};
2077 struct drm_mode_config *config = &dev->mode_config;
2078 struct drm_framebuffer *fb;
2079 int ret = 0;
2080
2081 /* Use new struct with format internally */
2082 r.fb_id = or->fb_id;
2083 r.width = or->width;
2084 r.height = or->height;
2085 r.pitches[0] = or->pitch;
2086 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2087 r.handles[0] = or->handle;
2088
2089 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2090 return -EINVAL;
2091
Jesse Barnesacb4b992011-11-07 12:03:23 -08002092 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002093 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002094
2095 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002096 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002097
2098 mutex_lock(&dev->mode_config.mutex);
2099
2100 /* TODO check buffer is sufficiently large */
2101 /* TODO setup destructor callback */
2102
2103 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2104 if (IS_ERR(fb)) {
2105 DRM_ERROR("could not create framebuffer\n");
2106 ret = PTR_ERR(fb);
2107 goto out;
2108 }
2109
2110 or->fb_id = fb->base.id;
2111 list_add(&fb->filp_head, &file_priv->fbs);
2112 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2113
2114out:
2115 mutex_unlock(&dev->mode_config.mutex);
2116 return ret;
2117}
2118
Ville Syrjälä935b5972011-12-20 00:06:48 +02002119static int format_check(struct drm_mode_fb_cmd2 *r)
2120{
2121 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2122
2123 switch (format) {
2124 case DRM_FORMAT_C8:
2125 case DRM_FORMAT_RGB332:
2126 case DRM_FORMAT_BGR233:
2127 case DRM_FORMAT_XRGB4444:
2128 case DRM_FORMAT_XBGR4444:
2129 case DRM_FORMAT_RGBX4444:
2130 case DRM_FORMAT_BGRX4444:
2131 case DRM_FORMAT_ARGB4444:
2132 case DRM_FORMAT_ABGR4444:
2133 case DRM_FORMAT_RGBA4444:
2134 case DRM_FORMAT_BGRA4444:
2135 case DRM_FORMAT_XRGB1555:
2136 case DRM_FORMAT_XBGR1555:
2137 case DRM_FORMAT_RGBX5551:
2138 case DRM_FORMAT_BGRX5551:
2139 case DRM_FORMAT_ARGB1555:
2140 case DRM_FORMAT_ABGR1555:
2141 case DRM_FORMAT_RGBA5551:
2142 case DRM_FORMAT_BGRA5551:
2143 case DRM_FORMAT_RGB565:
2144 case DRM_FORMAT_BGR565:
2145 case DRM_FORMAT_RGB888:
2146 case DRM_FORMAT_BGR888:
2147 case DRM_FORMAT_XRGB8888:
2148 case DRM_FORMAT_XBGR8888:
2149 case DRM_FORMAT_RGBX8888:
2150 case DRM_FORMAT_BGRX8888:
2151 case DRM_FORMAT_ARGB8888:
2152 case DRM_FORMAT_ABGR8888:
2153 case DRM_FORMAT_RGBA8888:
2154 case DRM_FORMAT_BGRA8888:
2155 case DRM_FORMAT_XRGB2101010:
2156 case DRM_FORMAT_XBGR2101010:
2157 case DRM_FORMAT_RGBX1010102:
2158 case DRM_FORMAT_BGRX1010102:
2159 case DRM_FORMAT_ARGB2101010:
2160 case DRM_FORMAT_ABGR2101010:
2161 case DRM_FORMAT_RGBA1010102:
2162 case DRM_FORMAT_BGRA1010102:
2163 case DRM_FORMAT_YUYV:
2164 case DRM_FORMAT_YVYU:
2165 case DRM_FORMAT_UYVY:
2166 case DRM_FORMAT_VYUY:
2167 case DRM_FORMAT_AYUV:
2168 case DRM_FORMAT_NV12:
2169 case DRM_FORMAT_NV21:
2170 case DRM_FORMAT_NV16:
2171 case DRM_FORMAT_NV61:
2172 case DRM_FORMAT_YUV410:
2173 case DRM_FORMAT_YVU410:
2174 case DRM_FORMAT_YUV411:
2175 case DRM_FORMAT_YVU411:
2176 case DRM_FORMAT_YUV420:
2177 case DRM_FORMAT_YVU420:
2178 case DRM_FORMAT_YUV422:
2179 case DRM_FORMAT_YVU422:
2180 case DRM_FORMAT_YUV444:
2181 case DRM_FORMAT_YVU444:
2182 return 0;
2183 default:
2184 return -EINVAL;
2185 }
2186}
2187
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002188/**
2189 * drm_mode_addfb2 - add an FB to the graphics configuration
2190 * @inode: inode from the ioctl
2191 * @filp: file * from the ioctl
2192 * @cmd: cmd from ioctl
2193 * @arg: arg from ioctl
2194 *
2195 * LOCKING:
2196 * Takes mode config lock.
2197 *
2198 * Add a new FB to the specified CRTC, given a user request with format.
2199 *
2200 * Called by the user via ioctl.
2201 *
2202 * RETURNS:
2203 * Zero on success, errno on failure.
2204 */
2205int drm_mode_addfb2(struct drm_device *dev,
2206 void *data, struct drm_file *file_priv)
2207{
2208 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002209 struct drm_mode_config *config = &dev->mode_config;
2210 struct drm_framebuffer *fb;
2211 int ret = 0;
2212
Dave Airliefb3b06c2011-02-08 13:55:21 +10002213 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2214 return -EINVAL;
2215
Dave Airlief453ba02008-11-07 14:05:41 -08002216 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002217 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2218 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002219 return -EINVAL;
2220 }
2221 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002222 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2223 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002224 return -EINVAL;
2225 }
2226
Ville Syrjälä935b5972011-12-20 00:06:48 +02002227 ret = format_check(r);
2228 if (ret) {
2229 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2230 return ret;
2231 }
2232
Dave Airlief453ba02008-11-07 14:05:41 -08002233 mutex_lock(&dev->mode_config.mutex);
2234
Dave Airlief453ba02008-11-07 14:05:41 -08002235 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002236 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002237 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002238 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002239 goto out;
2240 }
2241
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002242 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002243 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002244 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002245
2246out:
2247 mutex_unlock(&dev->mode_config.mutex);
2248 return ret;
2249}
2250
2251/**
2252 * drm_mode_rmfb - remove an FB from the configuration
2253 * @inode: inode from the ioctl
2254 * @filp: file * from the ioctl
2255 * @cmd: cmd from ioctl
2256 * @arg: arg from ioctl
2257 *
2258 * LOCKING:
2259 * Takes mode config lock.
2260 *
2261 * Remove the FB specified by the user.
2262 *
2263 * Called by the user via ioctl.
2264 *
2265 * RETURNS:
2266 * Zero on success, errno on failure.
2267 */
2268int drm_mode_rmfb(struct drm_device *dev,
2269 void *data, struct drm_file *file_priv)
2270{
2271 struct drm_mode_object *obj;
2272 struct drm_framebuffer *fb = NULL;
2273 struct drm_framebuffer *fbl = NULL;
2274 uint32_t *id = data;
2275 int ret = 0;
2276 int found = 0;
2277
Dave Airliefb3b06c2011-02-08 13:55:21 +10002278 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2279 return -EINVAL;
2280
Dave Airlief453ba02008-11-07 14:05:41 -08002281 mutex_lock(&dev->mode_config.mutex);
2282 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002283 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002284 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002285 ret = -EINVAL;
2286 goto out;
2287 }
2288 fb = obj_to_fb(obj);
2289
2290 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2291 if (fb == fbl)
2292 found = 1;
2293
2294 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002295 ret = -EINVAL;
2296 goto out;
2297 }
2298
2299 /* TODO release all crtc connected to the framebuffer */
2300 /* TODO unhock the destructor from the buffer object */
2301
2302 list_del(&fb->filp_head);
2303 fb->funcs->destroy(fb);
2304
2305out:
2306 mutex_unlock(&dev->mode_config.mutex);
2307 return ret;
2308}
2309
2310/**
2311 * drm_mode_getfb - get FB info
2312 * @inode: inode from the ioctl
2313 * @filp: file * from the ioctl
2314 * @cmd: cmd from ioctl
2315 * @arg: arg from ioctl
2316 *
2317 * LOCKING:
Sascha Hauerb20f3862012-02-01 11:38:31 +01002318 * Takes mode config lock.
Dave Airlief453ba02008-11-07 14:05:41 -08002319 *
2320 * Lookup the FB given its ID and return info about it.
2321 *
2322 * Called by the user via ioctl.
2323 *
2324 * RETURNS:
2325 * Zero on success, errno on failure.
2326 */
2327int drm_mode_getfb(struct drm_device *dev,
2328 void *data, struct drm_file *file_priv)
2329{
2330 struct drm_mode_fb_cmd *r = data;
2331 struct drm_mode_object *obj;
2332 struct drm_framebuffer *fb;
2333 int ret = 0;
2334
Dave Airliefb3b06c2011-02-08 13:55:21 +10002335 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2336 return -EINVAL;
2337
Dave Airlief453ba02008-11-07 14:05:41 -08002338 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002339 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002340 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002341 ret = -EINVAL;
2342 goto out;
2343 }
2344 fb = obj_to_fb(obj);
2345
2346 r->height = fb->height;
2347 r->width = fb->width;
2348 r->depth = fb->depth;
2349 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002350 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002351 fb->funcs->create_handle(fb, file_priv, &r->handle);
2352
2353out:
2354 mutex_unlock(&dev->mode_config.mutex);
2355 return ret;
2356}
2357
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002358int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2359 void *data, struct drm_file *file_priv)
2360{
2361 struct drm_clip_rect __user *clips_ptr;
2362 struct drm_clip_rect *clips = NULL;
2363 struct drm_mode_fb_dirty_cmd *r = data;
2364 struct drm_mode_object *obj;
2365 struct drm_framebuffer *fb;
2366 unsigned flags;
2367 int num_clips;
2368 int ret = 0;
2369
Dave Airliefb3b06c2011-02-08 13:55:21 +10002370 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2371 return -EINVAL;
2372
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002373 mutex_lock(&dev->mode_config.mutex);
2374 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2375 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002376 ret = -EINVAL;
2377 goto out_err1;
2378 }
2379 fb = obj_to_fb(obj);
2380
2381 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002382 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002383
2384 if (!num_clips != !clips_ptr) {
2385 ret = -EINVAL;
2386 goto out_err1;
2387 }
2388
2389 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2390
2391 /* If userspace annotates copy, clips must come in pairs */
2392 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2393 ret = -EINVAL;
2394 goto out_err1;
2395 }
2396
2397 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002398 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2399 ret = -EINVAL;
2400 goto out_err1;
2401 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002402 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2403 if (!clips) {
2404 ret = -ENOMEM;
2405 goto out_err1;
2406 }
2407
2408 ret = copy_from_user(clips, clips_ptr,
2409 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002410 if (ret) {
2411 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002412 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002413 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002414 }
2415
2416 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002417 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2418 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002419 } else {
2420 ret = -ENOSYS;
2421 goto out_err2;
2422 }
2423
2424out_err2:
2425 kfree(clips);
2426out_err1:
2427 mutex_unlock(&dev->mode_config.mutex);
2428 return ret;
2429}
2430
2431
Dave Airlief453ba02008-11-07 14:05:41 -08002432/**
2433 * drm_fb_release - remove and free the FBs on this file
2434 * @filp: file * from the ioctl
2435 *
2436 * LOCKING:
2437 * Takes mode config lock.
2438 *
2439 * Destroy all the FBs associated with @filp.
2440 *
2441 * Called by the user via ioctl.
2442 *
2443 * RETURNS:
2444 * Zero on success, errno on failure.
2445 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002446void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002447{
Dave Airlief453ba02008-11-07 14:05:41 -08002448 struct drm_device *dev = priv->minor->dev;
2449 struct drm_framebuffer *fb, *tfb;
2450
2451 mutex_lock(&dev->mode_config.mutex);
2452 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2453 list_del(&fb->filp_head);
2454 fb->funcs->destroy(fb);
2455 }
2456 mutex_unlock(&dev->mode_config.mutex);
2457}
2458
2459/**
2460 * drm_mode_attachmode - add a mode to the user mode list
2461 * @dev: DRM device
2462 * @connector: connector to add the mode to
2463 * @mode: mode to add
2464 *
2465 * Add @mode to @connector's user mode list.
2466 */
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002467static void drm_mode_attachmode(struct drm_device *dev,
2468 struct drm_connector *connector,
2469 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002470{
Dave Airlief453ba02008-11-07 14:05:41 -08002471 list_add_tail(&mode->head, &connector->user_modes);
Dave Airlief453ba02008-11-07 14:05:41 -08002472}
2473
2474int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
Ville Syrjäläac235da2012-03-13 12:35:46 +02002475 const struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -08002476{
2477 struct drm_connector *connector;
Ville Syrjäläac235da2012-03-13 12:35:46 +02002478 int ret = 0;
2479 struct drm_display_mode *dup_mode, *next;
2480 LIST_HEAD(list);
2481
Dave Airlief453ba02008-11-07 14:05:41 -08002482 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2483 if (!connector->encoder)
Ville Syrjäläac235da2012-03-13 12:35:46 +02002484 continue;
Dave Airlief453ba02008-11-07 14:05:41 -08002485 if (connector->encoder->crtc == crtc) {
Ville Syrjäläac235da2012-03-13 12:35:46 +02002486 dup_mode = drm_mode_duplicate(dev, mode);
2487 if (!dup_mode) {
2488 ret = -ENOMEM;
2489 goto out;
2490 }
2491 list_add_tail(&dup_mode->head, &list);
Dave Airlief453ba02008-11-07 14:05:41 -08002492 }
2493 }
Ville Syrjäläac235da2012-03-13 12:35:46 +02002494
2495 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2496 if (!connector->encoder)
2497 continue;
2498 if (connector->encoder->crtc == crtc)
2499 list_move_tail(list.next, &connector->user_modes);
2500 }
2501
2502 WARN_ON(!list_empty(&list));
2503
2504 out:
2505 list_for_each_entry_safe(dup_mode, next, &list, head)
2506 drm_mode_destroy(dev, dup_mode);
2507
2508 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002509}
2510EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2511
2512static int drm_mode_detachmode(struct drm_device *dev,
2513 struct drm_connector *connector,
2514 struct drm_display_mode *mode)
2515{
2516 int found = 0;
2517 int ret = 0;
2518 struct drm_display_mode *match_mode, *t;
2519
2520 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2521 if (drm_mode_equal(match_mode, mode)) {
2522 list_del(&match_mode->head);
2523 drm_mode_destroy(dev, match_mode);
2524 found = 1;
2525 break;
2526 }
2527 }
2528
2529 if (!found)
2530 ret = -EINVAL;
2531
2532 return ret;
2533}
2534
2535int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2536{
2537 struct drm_connector *connector;
2538
2539 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2540 drm_mode_detachmode(dev, connector, mode);
2541 }
2542 return 0;
2543}
2544EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2545
2546/**
2547 * drm_fb_attachmode - Attach a user mode to an connector
2548 * @inode: inode from the ioctl
2549 * @filp: file * from the ioctl
2550 * @cmd: cmd from ioctl
2551 * @arg: arg from ioctl
2552 *
2553 * This attaches a user specified mode to an connector.
2554 * Called by the user via ioctl.
2555 *
2556 * RETURNS:
2557 * Zero on success, errno on failure.
2558 */
2559int drm_mode_attachmode_ioctl(struct drm_device *dev,
2560 void *data, struct drm_file *file_priv)
2561{
2562 struct drm_mode_mode_cmd *mode_cmd = data;
2563 struct drm_connector *connector;
2564 struct drm_display_mode *mode;
2565 struct drm_mode_object *obj;
2566 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2567 int ret = 0;
2568
Dave Airliefb3b06c2011-02-08 13:55:21 +10002569 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2570 return -EINVAL;
2571
Dave Airlief453ba02008-11-07 14:05:41 -08002572 mutex_lock(&dev->mode_config.mutex);
2573
2574 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2575 if (!obj) {
2576 ret = -EINVAL;
2577 goto out;
2578 }
2579 connector = obj_to_connector(obj);
2580
2581 mode = drm_mode_create(dev);
2582 if (!mode) {
2583 ret = -ENOMEM;
2584 goto out;
2585 }
2586
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002587 ret = drm_crtc_convert_umode(mode, umode);
2588 if (ret) {
2589 DRM_DEBUG_KMS("Invalid mode\n");
2590 drm_mode_destroy(dev, mode);
2591 goto out;
2592 }
Dave Airlief453ba02008-11-07 14:05:41 -08002593
Ville Syrjälä1dd6c8b2012-03-13 12:35:42 +02002594 drm_mode_attachmode(dev, connector, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002595out:
2596 mutex_unlock(&dev->mode_config.mutex);
2597 return ret;
2598}
2599
2600
2601/**
2602 * drm_fb_detachmode - Detach a user specified mode from an connector
2603 * @inode: inode from the ioctl
2604 * @filp: file * from the ioctl
2605 * @cmd: cmd from ioctl
2606 * @arg: arg from ioctl
2607 *
2608 * Called by the user via ioctl.
2609 *
2610 * RETURNS:
2611 * Zero on success, errno on failure.
2612 */
2613int drm_mode_detachmode_ioctl(struct drm_device *dev,
2614 void *data, struct drm_file *file_priv)
2615{
2616 struct drm_mode_object *obj;
2617 struct drm_mode_mode_cmd *mode_cmd = data;
2618 struct drm_connector *connector;
2619 struct drm_display_mode mode;
2620 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2621 int ret = 0;
2622
Dave Airliefb3b06c2011-02-08 13:55:21 +10002623 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2624 return -EINVAL;
2625
Dave Airlief453ba02008-11-07 14:05:41 -08002626 mutex_lock(&dev->mode_config.mutex);
2627
2628 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2629 if (!obj) {
2630 ret = -EINVAL;
2631 goto out;
2632 }
2633 connector = obj_to_connector(obj);
2634
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002635 ret = drm_crtc_convert_umode(&mode, umode);
2636 if (ret) {
2637 DRM_DEBUG_KMS("Invalid mode\n");
2638 goto out;
2639 }
2640
Dave Airlief453ba02008-11-07 14:05:41 -08002641 ret = drm_mode_detachmode(dev, connector, &mode);
2642out:
2643 mutex_unlock(&dev->mode_config.mutex);
2644 return ret;
2645}
2646
2647struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2648 const char *name, int num_values)
2649{
2650 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002651 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002652
2653 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2654 if (!property)
2655 return NULL;
2656
2657 if (num_values) {
2658 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2659 if (!property->values)
2660 goto fail;
2661 }
2662
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002663 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2664 if (ret)
2665 goto fail;
2666
Dave Airlief453ba02008-11-07 14:05:41 -08002667 property->flags = flags;
2668 property->num_values = num_values;
2669 INIT_LIST_HEAD(&property->enum_blob_list);
2670
Vinson Lee471dd2e2011-11-10 11:55:40 -08002671 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002672 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002673 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2674 }
Dave Airlief453ba02008-11-07 14:05:41 -08002675
2676 list_add_tail(&property->head, &dev->mode_config.property_list);
2677 return property;
2678fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002679 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08002680 kfree(property);
2681 return NULL;
2682}
2683EXPORT_SYMBOL(drm_property_create);
2684
Sascha Hauer4a67d392012-02-06 10:58:17 +01002685struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2686 const char *name,
2687 const struct drm_prop_enum_list *props,
2688 int num_values)
2689{
2690 struct drm_property *property;
2691 int i, ret;
2692
2693 flags |= DRM_MODE_PROP_ENUM;
2694
2695 property = drm_property_create(dev, flags, name, num_values);
2696 if (!property)
2697 return NULL;
2698
2699 for (i = 0; i < num_values; i++) {
2700 ret = drm_property_add_enum(property, i,
2701 props[i].type,
2702 props[i].name);
2703 if (ret) {
2704 drm_property_destroy(dev, property);
2705 return NULL;
2706 }
2707 }
2708
2709 return property;
2710}
2711EXPORT_SYMBOL(drm_property_create_enum);
2712
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01002713struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2714 const char *name,
2715 uint64_t min, uint64_t max)
2716{
2717 struct drm_property *property;
2718
2719 flags |= DRM_MODE_PROP_RANGE;
2720
2721 property = drm_property_create(dev, flags, name, 2);
2722 if (!property)
2723 return NULL;
2724
2725 property->values[0] = min;
2726 property->values[1] = max;
2727
2728 return property;
2729}
2730EXPORT_SYMBOL(drm_property_create_range);
2731
Dave Airlief453ba02008-11-07 14:05:41 -08002732int drm_property_add_enum(struct drm_property *property, int index,
2733 uint64_t value, const char *name)
2734{
2735 struct drm_property_enum *prop_enum;
2736
2737 if (!(property->flags & DRM_MODE_PROP_ENUM))
2738 return -EINVAL;
2739
2740 if (!list_empty(&property->enum_blob_list)) {
2741 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2742 if (prop_enum->value == value) {
2743 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2744 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2745 return 0;
2746 }
2747 }
2748 }
2749
2750 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2751 if (!prop_enum)
2752 return -ENOMEM;
2753
2754 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2755 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2756 prop_enum->value = value;
2757
2758 property->values[index] = value;
2759 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2760 return 0;
2761}
2762EXPORT_SYMBOL(drm_property_add_enum);
2763
2764void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2765{
2766 struct drm_property_enum *prop_enum, *pt;
2767
2768 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2769 list_del(&prop_enum->head);
2770 kfree(prop_enum);
2771 }
2772
2773 if (property->num_values)
2774 kfree(property->values);
2775 drm_mode_object_put(dev, &property->base);
2776 list_del(&property->head);
2777 kfree(property);
2778}
2779EXPORT_SYMBOL(drm_property_destroy);
2780
2781int drm_connector_attach_property(struct drm_connector *connector,
2782 struct drm_property *property, uint64_t init_val)
2783{
2784 int i;
2785
2786 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2787 if (connector->property_ids[i] == 0) {
2788 connector->property_ids[i] = property->base.id;
2789 connector->property_values[i] = init_val;
2790 break;
2791 }
2792 }
2793
2794 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2795 return -EINVAL;
2796 return 0;
2797}
2798EXPORT_SYMBOL(drm_connector_attach_property);
2799
2800int drm_connector_property_set_value(struct drm_connector *connector,
2801 struct drm_property *property, uint64_t value)
2802{
2803 int i;
2804
2805 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2806 if (connector->property_ids[i] == property->base.id) {
2807 connector->property_values[i] = value;
2808 break;
2809 }
2810 }
2811
2812 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2813 return -EINVAL;
2814 return 0;
2815}
2816EXPORT_SYMBOL(drm_connector_property_set_value);
2817
2818int drm_connector_property_get_value(struct drm_connector *connector,
2819 struct drm_property *property, uint64_t *val)
2820{
2821 int i;
2822
2823 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2824 if (connector->property_ids[i] == property->base.id) {
2825 *val = connector->property_values[i];
2826 break;
2827 }
2828 }
2829
2830 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2831 return -EINVAL;
2832 return 0;
2833}
2834EXPORT_SYMBOL(drm_connector_property_get_value);
2835
2836int drm_mode_getproperty_ioctl(struct drm_device *dev,
2837 void *data, struct drm_file *file_priv)
2838{
2839 struct drm_mode_object *obj;
2840 struct drm_mode_get_property *out_resp = data;
2841 struct drm_property *property;
2842 int enum_count = 0;
2843 int blob_count = 0;
2844 int value_count = 0;
2845 int ret = 0, i;
2846 int copied;
2847 struct drm_property_enum *prop_enum;
2848 struct drm_mode_property_enum __user *enum_ptr;
2849 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002850 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002851 uint64_t __user *values_ptr;
2852 uint32_t __user *blob_length_ptr;
2853
Dave Airliefb3b06c2011-02-08 13:55:21 +10002854 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2855 return -EINVAL;
2856
Dave Airlief453ba02008-11-07 14:05:41 -08002857 mutex_lock(&dev->mode_config.mutex);
2858 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2859 if (!obj) {
2860 ret = -EINVAL;
2861 goto done;
2862 }
2863 property = obj_to_property(obj);
2864
2865 if (property->flags & DRM_MODE_PROP_ENUM) {
2866 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2867 enum_count++;
2868 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2869 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2870 blob_count++;
2871 }
2872
2873 value_count = property->num_values;
2874
2875 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2876 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2877 out_resp->flags = property->flags;
2878
2879 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002880 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002881 for (i = 0; i < value_count; i++) {
2882 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2883 ret = -EFAULT;
2884 goto done;
2885 }
2886 }
2887 }
2888 out_resp->count_values = value_count;
2889
2890 if (property->flags & DRM_MODE_PROP_ENUM) {
2891 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2892 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002893 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002894 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2895
2896 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2897 ret = -EFAULT;
2898 goto done;
2899 }
2900
2901 if (copy_to_user(&enum_ptr[copied].name,
2902 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2903 ret = -EFAULT;
2904 goto done;
2905 }
2906 copied++;
2907 }
2908 }
2909 out_resp->count_enum_blobs = enum_count;
2910 }
2911
2912 if (property->flags & DRM_MODE_PROP_BLOB) {
2913 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2914 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002915 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2916 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002917
2918 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2919 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2920 ret = -EFAULT;
2921 goto done;
2922 }
2923
2924 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2925 ret = -EFAULT;
2926 goto done;
2927 }
2928
2929 copied++;
2930 }
2931 }
2932 out_resp->count_enum_blobs = blob_count;
2933 }
2934done:
2935 mutex_unlock(&dev->mode_config.mutex);
2936 return ret;
2937}
2938
2939static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2940 void *data)
2941{
2942 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002943 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002944
2945 if (!length || !data)
2946 return NULL;
2947
2948 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2949 if (!blob)
2950 return NULL;
2951
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02002952 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2953 if (ret) {
2954 kfree(blob);
2955 return NULL;
2956 }
2957
Dave Airlief453ba02008-11-07 14:05:41 -08002958 blob->length = length;
2959
2960 memcpy(blob->data, data, length);
2961
Dave Airlief453ba02008-11-07 14:05:41 -08002962 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2963 return blob;
2964}
2965
2966static void drm_property_destroy_blob(struct drm_device *dev,
2967 struct drm_property_blob *blob)
2968{
2969 drm_mode_object_put(dev, &blob->base);
2970 list_del(&blob->head);
2971 kfree(blob);
2972}
2973
2974int drm_mode_getblob_ioctl(struct drm_device *dev,
2975 void *data, struct drm_file *file_priv)
2976{
2977 struct drm_mode_object *obj;
2978 struct drm_mode_get_blob *out_resp = data;
2979 struct drm_property_blob *blob;
2980 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002981 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002982
Dave Airliefb3b06c2011-02-08 13:55:21 +10002983 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2984 return -EINVAL;
2985
Dave Airlief453ba02008-11-07 14:05:41 -08002986 mutex_lock(&dev->mode_config.mutex);
2987 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2988 if (!obj) {
2989 ret = -EINVAL;
2990 goto done;
2991 }
2992 blob = obj_to_blob(obj);
2993
2994 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002995 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08002996 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2997 ret = -EFAULT;
2998 goto done;
2999 }
3000 }
3001 out_resp->length = blob->length;
3002
3003done:
3004 mutex_unlock(&dev->mode_config.mutex);
3005 return ret;
3006}
3007
3008int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3009 struct edid *edid)
3010{
3011 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00003012 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08003013
3014 if (connector->edid_blob_ptr)
3015 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3016
3017 /* Delete edid, when there is none. */
3018 if (!edid) {
3019 connector->edid_blob_ptr = NULL;
3020 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3021 return ret;
3022 }
3023
Adam Jackson7466f4c2010-03-29 21:43:23 +00003024 size = EDID_LENGTH * (1 + edid->extensions);
3025 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3026 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08003027
3028 ret = drm_connector_property_set_value(connector,
3029 dev->mode_config.edid_property,
3030 connector->edid_blob_ptr->base.id);
3031
3032 return ret;
3033}
3034EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3035
3036int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3037 void *data, struct drm_file *file_priv)
3038{
3039 struct drm_mode_connector_set_property *out_resp = data;
3040 struct drm_mode_object *obj;
3041 struct drm_property *property;
3042 struct drm_connector *connector;
3043 int ret = -EINVAL;
3044 int i;
3045
Dave Airliefb3b06c2011-02-08 13:55:21 +10003046 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3047 return -EINVAL;
3048
Dave Airlief453ba02008-11-07 14:05:41 -08003049 mutex_lock(&dev->mode_config.mutex);
3050
3051 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
3052 if (!obj) {
3053 goto out;
3054 }
3055 connector = obj_to_connector(obj);
3056
3057 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
3058 if (connector->property_ids[i] == out_resp->prop_id)
3059 break;
3060 }
3061
3062 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
3063 goto out;
3064 }
3065
3066 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
3067 if (!obj) {
3068 goto out;
3069 }
3070 property = obj_to_property(obj);
3071
3072 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3073 goto out;
3074
3075 if (property->flags & DRM_MODE_PROP_RANGE) {
3076 if (out_resp->value < property->values[0])
3077 goto out;
3078
3079 if (out_resp->value > property->values[1])
3080 goto out;
3081 } else {
3082 int found = 0;
3083 for (i = 0; i < property->num_values; i++) {
3084 if (property->values[i] == out_resp->value) {
3085 found = 1;
3086 break;
3087 }
3088 }
3089 if (!found) {
3090 goto out;
3091 }
3092 }
3093
Keith Packardc9fb15f2009-05-30 20:42:28 -07003094 /* Do DPMS ourselves */
3095 if (property == connector->dev->mode_config.dpms_property) {
3096 if (connector->funcs->dpms)
3097 (*connector->funcs->dpms)(connector, (int) out_resp->value);
3098 ret = 0;
3099 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08003100 ret = connector->funcs->set_property(connector, property, out_resp->value);
3101
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003102 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08003103 if (!ret)
3104 drm_connector_property_set_value(connector, property, out_resp->value);
3105out:
3106 mutex_unlock(&dev->mode_config.mutex);
3107 return ret;
3108}
3109
Dave Airlief453ba02008-11-07 14:05:41 -08003110int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3111 struct drm_encoder *encoder)
3112{
3113 int i;
3114
3115 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3116 if (connector->encoder_ids[i] == 0) {
3117 connector->encoder_ids[i] = encoder->base.id;
3118 return 0;
3119 }
3120 }
3121 return -ENOMEM;
3122}
3123EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3124
3125void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3126 struct drm_encoder *encoder)
3127{
3128 int i;
3129 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3130 if (connector->encoder_ids[i] == encoder->base.id) {
3131 connector->encoder_ids[i] = 0;
3132 if (connector->encoder == encoder)
3133 connector->encoder = NULL;
3134 break;
3135 }
3136 }
3137}
3138EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3139
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003140int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Dave Airlief453ba02008-11-07 14:05:41 -08003141 int gamma_size)
3142{
3143 crtc->gamma_size = gamma_size;
3144
3145 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3146 if (!crtc->gamma_store) {
3147 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003148 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08003149 }
3150
Sascha Hauer4cae5b82012-02-01 11:38:23 +01003151 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003152}
3153EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3154
3155int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3156 void *data, struct drm_file *file_priv)
3157{
3158 struct drm_mode_crtc_lut *crtc_lut = data;
3159 struct drm_mode_object *obj;
3160 struct drm_crtc *crtc;
3161 void *r_base, *g_base, *b_base;
3162 int size;
3163 int ret = 0;
3164
Dave Airliefb3b06c2011-02-08 13:55:21 +10003165 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3166 return -EINVAL;
3167
Dave Airlief453ba02008-11-07 14:05:41 -08003168 mutex_lock(&dev->mode_config.mutex);
3169 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3170 if (!obj) {
3171 ret = -EINVAL;
3172 goto out;
3173 }
3174 crtc = obj_to_crtc(obj);
3175
3176 /* memcpy into gamma store */
3177 if (crtc_lut->gamma_size != crtc->gamma_size) {
3178 ret = -EINVAL;
3179 goto out;
3180 }
3181
3182 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3183 r_base = crtc->gamma_store;
3184 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3185 ret = -EFAULT;
3186 goto out;
3187 }
3188
3189 g_base = r_base + size;
3190 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3191 ret = -EFAULT;
3192 goto out;
3193 }
3194
3195 b_base = g_base + size;
3196 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3197 ret = -EFAULT;
3198 goto out;
3199 }
3200
James Simmons72034252010-08-03 01:33:19 +01003201 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003202
3203out:
3204 mutex_unlock(&dev->mode_config.mutex);
3205 return ret;
3206
3207}
3208
3209int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3210 void *data, struct drm_file *file_priv)
3211{
3212 struct drm_mode_crtc_lut *crtc_lut = data;
3213 struct drm_mode_object *obj;
3214 struct drm_crtc *crtc;
3215 void *r_base, *g_base, *b_base;
3216 int size;
3217 int ret = 0;
3218
Dave Airliefb3b06c2011-02-08 13:55:21 +10003219 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3220 return -EINVAL;
3221
Dave Airlief453ba02008-11-07 14:05:41 -08003222 mutex_lock(&dev->mode_config.mutex);
3223 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3224 if (!obj) {
3225 ret = -EINVAL;
3226 goto out;
3227 }
3228 crtc = obj_to_crtc(obj);
3229
3230 /* memcpy into gamma store */
3231 if (crtc_lut->gamma_size != crtc->gamma_size) {
3232 ret = -EINVAL;
3233 goto out;
3234 }
3235
3236 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3237 r_base = crtc->gamma_store;
3238 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3239 ret = -EFAULT;
3240 goto out;
3241 }
3242
3243 g_base = r_base + size;
3244 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3245 ret = -EFAULT;
3246 goto out;
3247 }
3248
3249 b_base = g_base + size;
3250 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3251 ret = -EFAULT;
3252 goto out;
3253 }
3254out:
3255 mutex_unlock(&dev->mode_config.mutex);
3256 return ret;
3257}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003258
3259int drm_mode_page_flip_ioctl(struct drm_device *dev,
3260 void *data, struct drm_file *file_priv)
3261{
3262 struct drm_mode_crtc_page_flip *page_flip = data;
3263 struct drm_mode_object *obj;
3264 struct drm_crtc *crtc;
3265 struct drm_framebuffer *fb;
3266 struct drm_pending_vblank_event *e = NULL;
3267 unsigned long flags;
3268 int ret = -EINVAL;
3269
3270 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3271 page_flip->reserved != 0)
3272 return -EINVAL;
3273
3274 mutex_lock(&dev->mode_config.mutex);
3275 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3276 if (!obj)
3277 goto out;
3278 crtc = obj_to_crtc(obj);
3279
Chris Wilson90c1efd2010-07-17 20:23:26 +01003280 if (crtc->fb == NULL) {
3281 /* The framebuffer is currently unbound, presumably
3282 * due to a hotplug event, that userspace has not
3283 * yet discovered.
3284 */
3285 ret = -EBUSY;
3286 goto out;
3287 }
3288
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003289 if (crtc->funcs->page_flip == NULL)
3290 goto out;
3291
3292 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3293 if (!obj)
3294 goto out;
3295 fb = obj_to_fb(obj);
3296
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02003297 if (crtc->mode.hdisplay > fb->width ||
3298 crtc->mode.vdisplay > fb->height ||
3299 crtc->x > fb->width - crtc->mode.hdisplay ||
3300 crtc->y > fb->height - crtc->mode.vdisplay) {
3301 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3302 fb->width, fb->height,
3303 crtc->mode.hdisplay, crtc->mode.vdisplay,
3304 crtc->x, crtc->y);
3305 ret = -ENOSPC;
3306 goto out;
3307 }
3308
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003309 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3310 ret = -ENOMEM;
3311 spin_lock_irqsave(&dev->event_lock, flags);
3312 if (file_priv->event_space < sizeof e->event) {
3313 spin_unlock_irqrestore(&dev->event_lock, flags);
3314 goto out;
3315 }
3316 file_priv->event_space -= sizeof e->event;
3317 spin_unlock_irqrestore(&dev->event_lock, flags);
3318
3319 e = kzalloc(sizeof *e, GFP_KERNEL);
3320 if (e == NULL) {
3321 spin_lock_irqsave(&dev->event_lock, flags);
3322 file_priv->event_space += sizeof e->event;
3323 spin_unlock_irqrestore(&dev->event_lock, flags);
3324 goto out;
3325 }
3326
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003327 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003328 e->event.base.length = sizeof e->event;
3329 e->event.user_data = page_flip->user_data;
3330 e->base.event = &e->event.base;
3331 e->base.file_priv = file_priv;
3332 e->base.destroy =
3333 (void (*) (struct drm_pending_event *)) kfree;
3334 }
3335
3336 ret = crtc->funcs->page_flip(crtc, fb, e);
3337 if (ret) {
3338 spin_lock_irqsave(&dev->event_lock, flags);
3339 file_priv->event_space += sizeof e->event;
3340 spin_unlock_irqrestore(&dev->event_lock, flags);
3341 kfree(e);
3342 }
3343
3344out:
3345 mutex_unlock(&dev->mode_config.mutex);
3346 return ret;
3347}
Chris Wilsoneb033552011-01-24 15:11:08 +00003348
3349void drm_mode_config_reset(struct drm_device *dev)
3350{
3351 struct drm_crtc *crtc;
3352 struct drm_encoder *encoder;
3353 struct drm_connector *connector;
3354
3355 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3356 if (crtc->funcs->reset)
3357 crtc->funcs->reset(crtc);
3358
3359 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3360 if (encoder->funcs->reset)
3361 encoder->funcs->reset(encoder);
3362
3363 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3364 if (connector->funcs->reset)
3365 connector->funcs->reset(connector);
3366}
3367EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003368
3369int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3370 void *data, struct drm_file *file_priv)
3371{
3372 struct drm_mode_create_dumb *args = data;
3373
3374 if (!dev->driver->dumb_create)
3375 return -ENOSYS;
3376 return dev->driver->dumb_create(file_priv, dev, args);
3377}
3378
3379int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3380 void *data, struct drm_file *file_priv)
3381{
3382 struct drm_mode_map_dumb *args = data;
3383
3384 /* call driver ioctl to get mmap offset */
3385 if (!dev->driver->dumb_map_offset)
3386 return -ENOSYS;
3387
3388 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3389}
3390
3391int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3392 void *data, struct drm_file *file_priv)
3393{
3394 struct drm_mode_destroy_dumb *args = data;
3395
3396 if (!dev->driver->dumb_destroy)
3397 return -ENOSYS;
3398
3399 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3400}
Dave Airlie248dbc22011-11-29 20:02:54 +00003401
3402/*
3403 * Just need to support RGB formats here for compat with code that doesn't
3404 * use pixel formats directly yet.
3405 */
3406void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3407 int *bpp)
3408{
3409 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003410 case DRM_FORMAT_RGB332:
3411 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003412 *depth = 8;
3413 *bpp = 8;
3414 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003415 case DRM_FORMAT_XRGB1555:
3416 case DRM_FORMAT_XBGR1555:
3417 case DRM_FORMAT_RGBX5551:
3418 case DRM_FORMAT_BGRX5551:
3419 case DRM_FORMAT_ARGB1555:
3420 case DRM_FORMAT_ABGR1555:
3421 case DRM_FORMAT_RGBA5551:
3422 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003423 *depth = 15;
3424 *bpp = 16;
3425 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003426 case DRM_FORMAT_RGB565:
3427 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003428 *depth = 16;
3429 *bpp = 16;
3430 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003431 case DRM_FORMAT_RGB888:
3432 case DRM_FORMAT_BGR888:
3433 *depth = 24;
3434 *bpp = 24;
3435 break;
3436 case DRM_FORMAT_XRGB8888:
3437 case DRM_FORMAT_XBGR8888:
3438 case DRM_FORMAT_RGBX8888:
3439 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003440 *depth = 24;
3441 *bpp = 32;
3442 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003443 case DRM_FORMAT_XRGB2101010:
3444 case DRM_FORMAT_XBGR2101010:
3445 case DRM_FORMAT_RGBX1010102:
3446 case DRM_FORMAT_BGRX1010102:
3447 case DRM_FORMAT_ARGB2101010:
3448 case DRM_FORMAT_ABGR2101010:
3449 case DRM_FORMAT_RGBA1010102:
3450 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003451 *depth = 30;
3452 *bpp = 32;
3453 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003454 case DRM_FORMAT_ARGB8888:
3455 case DRM_FORMAT_ABGR8888:
3456 case DRM_FORMAT_RGBA8888:
3457 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003458 *depth = 32;
3459 *bpp = 32;
3460 break;
3461 default:
3462 DRM_DEBUG_KMS("unsupported pixel format\n");
3463 *depth = 0;
3464 *bpp = 0;
3465 break;
3466 }
3467}
3468EXPORT_SYMBOL(drm_fb_get_bpp_depth);