blob: 7710bcb4bd8d7d5403ed80bb5124901ea0100759 [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
41struct drm_prop_enum_list {
42 int type;
43 char *name;
44};
45
46/* Avoid boilerplate. I'm tired of typing. */
47#define DRM_ENUM_NAME_FN(fnname, list) \
48 char *fnname(int val) \
49 { \
50 int i; \
51 for (i = 0; i < ARRAY_SIZE(list); i++) { \
52 if (list[i].type == val) \
53 return list[i].name; \
54 } \
55 return "(unknown)"; \
56 }
57
58/*
59 * Global properties
60 */
61static struct drm_prop_enum_list drm_dpms_enum_list[] =
62{ { DRM_MODE_DPMS_ON, "On" },
63 { DRM_MODE_DPMS_STANDBY, "Standby" },
64 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
65 { DRM_MODE_DPMS_OFF, "Off" }
66};
67
68DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
69
70/*
71 * Optional properties
72 */
73static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
74{
Jesse Barnes53bd8382009-07-01 10:04:40 -070075 { DRM_MODE_SCALE_NONE, "None" },
76 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
77 { DRM_MODE_SCALE_CENTER, "Center" },
78 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080079};
80
81static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
82{
83 { DRM_MODE_DITHERING_OFF, "Off" },
84 { DRM_MODE_DITHERING_ON, "On" },
Ben Skeggs92897b52010-07-16 15:09:17 +100085 { DRM_MODE_DITHERING_AUTO, "Automatic" },
Dave Airlief453ba02008-11-07 14:05:41 -080086};
87
88/*
89 * Non-global properties, but "required" for certain connectors.
90 */
91static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
92{
93 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
94 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
95 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
96};
97
98DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
99
100static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
101{
102 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
103 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
104 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
105};
106
107DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
108 drm_dvi_i_subconnector_enum_list)
109
110static struct drm_prop_enum_list drm_tv_select_enum_list[] =
111{
112 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
113 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
114 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
115 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200116 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800117};
118
119DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
120
121static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
122{
123 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
124 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
125 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
126 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200127 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800128};
129
130DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
131 drm_tv_subconnector_enum_list)
132
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000133static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
134 { DRM_MODE_DIRTY_OFF, "Off" },
135 { DRM_MODE_DIRTY_ON, "On" },
136 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
137};
138
139DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
140 drm_dirty_info_enum_list)
141
Dave Airlief453ba02008-11-07 14:05:41 -0800142struct drm_conn_prop_enum_list {
143 int type;
144 char *name;
145 int count;
146};
147
148/*
149 * Connector and encoder types.
150 */
151static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
152{ { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
153 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
154 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
155 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
156 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
157 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
158 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
159 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
160 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500161 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
162 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
163 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
164 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
Francisco Jerez74bd3c22009-08-02 04:19:18 +0200165 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
Alex Deuchere76116c2010-12-08 19:09:42 -0500166 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200167 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
Dave Airlief453ba02008-11-07 14:05:41 -0800168};
169
170static struct drm_prop_enum_list drm_encoder_enum_list[] =
171{ { DRM_MODE_ENCODER_NONE, "None" },
172 { DRM_MODE_ENCODER_DAC, "DAC" },
173 { DRM_MODE_ENCODER_TMDS, "TMDS" },
174 { DRM_MODE_ENCODER_LVDS, "LVDS" },
175 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200176 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Dave Airlief453ba02008-11-07 14:05:41 -0800177};
178
179char *drm_get_encoder_name(struct drm_encoder *encoder)
180{
181 static char buf[32];
182
183 snprintf(buf, 32, "%s-%d",
184 drm_encoder_enum_list[encoder->encoder_type].name,
185 encoder->base.id);
186 return buf;
187}
Dave Airlie13a81952009-09-07 15:45:33 +1000188EXPORT_SYMBOL(drm_get_encoder_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800189
190char *drm_get_connector_name(struct drm_connector *connector)
191{
192 static char buf[32];
193
194 snprintf(buf, 32, "%s-%d",
195 drm_connector_enum_list[connector->connector_type].name,
196 connector->connector_type_id);
197 return buf;
198}
199EXPORT_SYMBOL(drm_get_connector_name);
200
201char *drm_get_connector_status_name(enum drm_connector_status status)
202{
203 if (status == connector_status_connected)
204 return "connected";
205 else if (status == connector_status_disconnected)
206 return "disconnected";
207 else
208 return "unknown";
209}
210
211/**
212 * drm_mode_object_get - allocate a new identifier
213 * @dev: DRM device
214 * @ptr: object pointer, used to generate unique ID
215 * @type: object type
216 *
217 * LOCKING:
Dave Airlief453ba02008-11-07 14:05:41 -0800218 *
219 * Create a unique identifier based on @ptr in @dev's identifier space. Used
220 * for tracking modes, CRTCs and connectors.
221 *
222 * RETURNS:
223 * New unique (relative to other objects in @dev) integer identifier for the
224 * object.
225 */
226static int drm_mode_object_get(struct drm_device *dev,
227 struct drm_mode_object *obj, uint32_t obj_type)
228{
229 int new_id = 0;
230 int ret;
231
Dave Airlief453ba02008-11-07 14:05:41 -0800232again:
233 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
234 DRM_ERROR("Ran out memory getting a mode number\n");
235 return -EINVAL;
236 }
237
Jesse Barnesad2563c2009-01-19 17:21:45 +1000238 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800239 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000240 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800241 if (ret == -EAGAIN)
242 goto again;
243
244 obj->id = new_id;
245 obj->type = obj_type;
246 return 0;
247}
248
249/**
250 * drm_mode_object_put - free an identifer
251 * @dev: DRM device
252 * @id: ID to free
253 *
254 * LOCKING:
255 * Caller must hold DRM mode_config lock.
256 *
257 * Free @id from @dev's unique identifier pool.
258 */
259static void drm_mode_object_put(struct drm_device *dev,
260 struct drm_mode_object *object)
261{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000262 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800263 idr_remove(&dev->mode_config.crtc_idr, object->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000264 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800265}
266
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200267struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
268 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800269{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000270 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800271
Jesse Barnesad2563c2009-01-19 17:21:45 +1000272 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800273 obj = idr_find(&dev->mode_config.crtc_idr, id);
274 if (!obj || (obj->type != type) || (obj->id != id))
Jesse Barnesad2563c2009-01-19 17:21:45 +1000275 obj = NULL;
276 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800277
278 return obj;
279}
280EXPORT_SYMBOL(drm_mode_object_find);
281
282/**
Dave Airlief453ba02008-11-07 14:05:41 -0800283 * drm_framebuffer_init - initialize a framebuffer
284 * @dev: DRM device
285 *
286 * LOCKING:
287 * Caller must hold mode config lock.
288 *
289 * Allocates an ID for the framebuffer's parent mode object, sets its mode
290 * functions & device file and adds it to the master fd list.
291 *
292 * RETURNS:
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200293 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800294 */
295int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
296 const struct drm_framebuffer_funcs *funcs)
297{
298 int ret;
299
300 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
301 if (ret) {
302 return ret;
303 }
304
305 fb->dev = dev;
306 fb->funcs = funcs;
307 dev->mode_config.num_fb++;
308 list_add(&fb->head, &dev->mode_config.fb_list);
309
310 return 0;
311}
312EXPORT_SYMBOL(drm_framebuffer_init);
313
314/**
315 * drm_framebuffer_cleanup - remove a framebuffer object
316 * @fb: framebuffer to remove
317 *
318 * LOCKING:
319 * Caller must hold mode config lock.
320 *
321 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
322 * it, setting it to NULL.
323 */
324void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
325{
326 struct drm_device *dev = fb->dev;
327 struct drm_crtc *crtc;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800328 struct drm_plane *plane;
Dave Airlie5ef5f722009-08-17 13:11:23 +1000329 struct drm_mode_set set;
330 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800331
332 /* remove from any CRTC */
333 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Dave Airlie5ef5f722009-08-17 13:11:23 +1000334 if (crtc->fb == fb) {
335 /* should turn off the crtc */
336 memset(&set, 0, sizeof(struct drm_mode_set));
337 set.crtc = crtc;
338 set.fb = NULL;
339 ret = crtc->funcs->set_config(&set);
340 if (ret)
341 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
342 }
Dave Airlief453ba02008-11-07 14:05:41 -0800343 }
344
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800345 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
346 if (plane->fb == fb) {
347 /* should turn off the crtc */
348 ret = plane->funcs->disable_plane(plane);
349 if (ret)
350 DRM_ERROR("failed to disable plane with busy fb\n");
Rob Clarka9971152011-12-13 20:19:35 -0600351 /* disconnect the plane from the fb and crtc: */
352 plane->fb = NULL;
353 plane->crtc = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800354 }
355 }
356
Dave Airlief453ba02008-11-07 14:05:41 -0800357 drm_mode_object_put(dev, &fb->base);
358 list_del(&fb->head);
359 dev->mode_config.num_fb--;
360}
361EXPORT_SYMBOL(drm_framebuffer_cleanup);
362
363/**
364 * drm_crtc_init - Initialise a new CRTC object
365 * @dev: DRM device
366 * @crtc: CRTC object to init
367 * @funcs: callbacks for the new CRTC
368 *
369 * LOCKING:
370 * Caller must hold mode config lock.
371 *
372 * Inits a new object created as base part of an driver crtc object.
373 */
374void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
375 const struct drm_crtc_funcs *funcs)
376{
377 crtc->dev = dev;
378 crtc->funcs = funcs;
379
380 mutex_lock(&dev->mode_config.mutex);
381 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
382
383 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
384 dev->mode_config.num_crtc++;
385 mutex_unlock(&dev->mode_config.mutex);
386}
387EXPORT_SYMBOL(drm_crtc_init);
388
389/**
390 * drm_crtc_cleanup - Cleans up the core crtc usage.
391 * @crtc: CRTC to cleanup
392 *
393 * LOCKING:
394 * Caller must hold mode config lock.
395 *
396 * Cleanup @crtc. Removes from drm modesetting space
397 * does NOT free object, caller does that.
398 */
399void drm_crtc_cleanup(struct drm_crtc *crtc)
400{
401 struct drm_device *dev = crtc->dev;
402
403 if (crtc->gamma_store) {
404 kfree(crtc->gamma_store);
405 crtc->gamma_store = NULL;
406 }
407
408 drm_mode_object_put(dev, &crtc->base);
409 list_del(&crtc->head);
410 dev->mode_config.num_crtc--;
411}
412EXPORT_SYMBOL(drm_crtc_cleanup);
413
414/**
415 * drm_mode_probed_add - add a mode to a connector's probed mode list
416 * @connector: connector the new mode
417 * @mode: mode data
418 *
419 * LOCKING:
420 * Caller must hold mode config lock.
421 *
422 * Add @mode to @connector's mode list for later use.
423 */
424void drm_mode_probed_add(struct drm_connector *connector,
425 struct drm_display_mode *mode)
426{
427 list_add(&mode->head, &connector->probed_modes);
428}
429EXPORT_SYMBOL(drm_mode_probed_add);
430
431/**
432 * drm_mode_remove - remove and free a mode
433 * @connector: connector list to modify
434 * @mode: mode to remove
435 *
436 * LOCKING:
437 * Caller must hold mode config lock.
438 *
439 * Remove @mode from @connector's mode list, then free it.
440 */
441void drm_mode_remove(struct drm_connector *connector,
442 struct drm_display_mode *mode)
443{
444 list_del(&mode->head);
445 kfree(mode);
446}
447EXPORT_SYMBOL(drm_mode_remove);
448
449/**
450 * drm_connector_init - Init a preallocated connector
451 * @dev: DRM device
452 * @connector: the connector to init
453 * @funcs: callbacks for this connector
454 * @name: user visible name of the connector
455 *
456 * LOCKING:
457 * Caller must hold @dev's mode_config lock.
458 *
459 * Initialises a preallocated connector. Connectors should be
460 * subclassed as part of driver connector objects.
461 */
462void drm_connector_init(struct drm_device *dev,
463 struct drm_connector *connector,
464 const struct drm_connector_funcs *funcs,
465 int connector_type)
466{
467 mutex_lock(&dev->mode_config.mutex);
468
469 connector->dev = dev;
470 connector->funcs = funcs;
471 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
472 connector->connector_type = connector_type;
473 connector->connector_type_id =
474 ++drm_connector_enum_list[connector_type].count; /* TODO */
475 INIT_LIST_HEAD(&connector->user_modes);
476 INIT_LIST_HEAD(&connector->probed_modes);
477 INIT_LIST_HEAD(&connector->modes);
478 connector->edid_blob_ptr = NULL;
479
480 list_add_tail(&connector->head, &dev->mode_config.connector_list);
481 dev->mode_config.num_connector++;
482
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200483 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
484 drm_connector_attach_property(connector,
485 dev->mode_config.edid_property,
486 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800487
488 drm_connector_attach_property(connector,
489 dev->mode_config.dpms_property, 0);
490
491 mutex_unlock(&dev->mode_config.mutex);
492}
493EXPORT_SYMBOL(drm_connector_init);
494
495/**
496 * drm_connector_cleanup - cleans up an initialised connector
497 * @connector: connector to cleanup
498 *
499 * LOCKING:
500 * Caller must hold @dev's mode_config lock.
501 *
502 * Cleans up the connector but doesn't free the object.
503 */
504void drm_connector_cleanup(struct drm_connector *connector)
505{
506 struct drm_device *dev = connector->dev;
507 struct drm_display_mode *mode, *t;
508
509 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
510 drm_mode_remove(connector, mode);
511
512 list_for_each_entry_safe(mode, t, &connector->modes, head)
513 drm_mode_remove(connector, mode);
514
515 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
516 drm_mode_remove(connector, mode);
517
518 mutex_lock(&dev->mode_config.mutex);
519 drm_mode_object_put(dev, &connector->base);
520 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000521 dev->mode_config.num_connector--;
Dave Airlief453ba02008-11-07 14:05:41 -0800522 mutex_unlock(&dev->mode_config.mutex);
523}
524EXPORT_SYMBOL(drm_connector_cleanup);
525
526void drm_encoder_init(struct drm_device *dev,
527 struct drm_encoder *encoder,
528 const struct drm_encoder_funcs *funcs,
529 int encoder_type)
530{
531 mutex_lock(&dev->mode_config.mutex);
532
533 encoder->dev = dev;
534
535 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
536 encoder->encoder_type = encoder_type;
537 encoder->funcs = funcs;
538
539 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
540 dev->mode_config.num_encoder++;
541
542 mutex_unlock(&dev->mode_config.mutex);
543}
544EXPORT_SYMBOL(drm_encoder_init);
545
546void drm_encoder_cleanup(struct drm_encoder *encoder)
547{
548 struct drm_device *dev = encoder->dev;
549 mutex_lock(&dev->mode_config.mutex);
550 drm_mode_object_put(dev, &encoder->base);
551 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000552 dev->mode_config.num_encoder--;
Dave Airlief453ba02008-11-07 14:05:41 -0800553 mutex_unlock(&dev->mode_config.mutex);
554}
555EXPORT_SYMBOL(drm_encoder_cleanup);
556
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800557int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
558 unsigned long possible_crtcs,
559 const struct drm_plane_funcs *funcs,
Ville Syrjälä22cd7c62011-12-20 00:06:46 +0200560 const uint32_t *formats, uint32_t format_count)
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800561{
562 mutex_lock(&dev->mode_config.mutex);
563
564 plane->dev = dev;
565 drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
566 plane->funcs = funcs;
567 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
568 GFP_KERNEL);
569 if (!plane->format_types) {
570 DRM_DEBUG_KMS("out of memory when allocating plane\n");
571 drm_mode_object_put(dev, &plane->base);
Ville Syrjälä10bf5732011-12-20 00:06:41 +0200572 mutex_unlock(&dev->mode_config.mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800573 return -ENOMEM;
574 }
575
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800576 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800577 plane->format_count = format_count;
578 plane->possible_crtcs = possible_crtcs;
579
580 list_add_tail(&plane->head, &dev->mode_config.plane_list);
581 dev->mode_config.num_plane++;
582
583 mutex_unlock(&dev->mode_config.mutex);
584
585 return 0;
586}
587EXPORT_SYMBOL(drm_plane_init);
588
589void drm_plane_cleanup(struct drm_plane *plane)
590{
591 struct drm_device *dev = plane->dev;
592
593 mutex_lock(&dev->mode_config.mutex);
594 kfree(plane->format_types);
595 drm_mode_object_put(dev, &plane->base);
596 list_del(&plane->head);
597 dev->mode_config.num_plane--;
598 mutex_unlock(&dev->mode_config.mutex);
599}
600EXPORT_SYMBOL(drm_plane_cleanup);
601
Dave Airlief453ba02008-11-07 14:05:41 -0800602/**
603 * drm_mode_create - create a new display mode
604 * @dev: DRM device
605 *
606 * LOCKING:
607 * Caller must hold DRM mode_config lock.
608 *
609 * Create a new drm_display_mode, give it an ID, and return it.
610 *
611 * RETURNS:
612 * Pointer to new mode on success, NULL on error.
613 */
614struct drm_display_mode *drm_mode_create(struct drm_device *dev)
615{
616 struct drm_display_mode *nmode;
617
618 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
619 if (!nmode)
620 return NULL;
621
622 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
623 return nmode;
624}
625EXPORT_SYMBOL(drm_mode_create);
626
627/**
628 * drm_mode_destroy - remove a mode
629 * @dev: DRM device
630 * @mode: mode to remove
631 *
632 * LOCKING:
633 * Caller must hold mode config lock.
634 *
635 * Free @mode's unique identifier, then free it.
636 */
637void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
638{
639 drm_mode_object_put(dev, &mode->base);
640
641 kfree(mode);
642}
643EXPORT_SYMBOL(drm_mode_destroy);
644
645static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
646{
647 struct drm_property *edid;
648 struct drm_property *dpms;
649 int i;
650
651 /*
652 * Standard properties (apply to all connectors)
653 */
654 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
655 DRM_MODE_PROP_IMMUTABLE,
656 "EDID", 0);
657 dev->mode_config.edid_property = edid;
658
659 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
660 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
661 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
662 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
663 drm_dpms_enum_list[i].name);
664 dev->mode_config.dpms_property = dpms;
665
666 return 0;
667}
668
669/**
670 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
671 * @dev: DRM device
672 *
673 * Called by a driver the first time a DVI-I connector is made.
674 */
675int drm_mode_create_dvi_i_properties(struct drm_device *dev)
676{
677 struct drm_property *dvi_i_selector;
678 struct drm_property *dvi_i_subconnector;
679 int i;
680
681 if (dev->mode_config.dvi_i_select_subconnector_property)
682 return 0;
683
684 dvi_i_selector =
685 drm_property_create(dev, DRM_MODE_PROP_ENUM,
686 "select subconnector",
687 ARRAY_SIZE(drm_dvi_i_select_enum_list));
688 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
689 drm_property_add_enum(dvi_i_selector, i,
690 drm_dvi_i_select_enum_list[i].type,
691 drm_dvi_i_select_enum_list[i].name);
692 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
693
694 dvi_i_subconnector =
695 drm_property_create(dev, DRM_MODE_PROP_ENUM |
696 DRM_MODE_PROP_IMMUTABLE,
697 "subconnector",
698 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
699 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
700 drm_property_add_enum(dvi_i_subconnector, i,
701 drm_dvi_i_subconnector_enum_list[i].type,
702 drm_dvi_i_subconnector_enum_list[i].name);
703 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
704
705 return 0;
706}
707EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
708
709/**
710 * drm_create_tv_properties - create TV specific connector properties
711 * @dev: DRM device
712 * @num_modes: number of different TV formats (modes) supported
713 * @modes: array of pointers to strings containing name of each format
714 *
715 * Called by a driver's TV initialization routine, this function creates
716 * the TV specific connector properties for a given device. Caller is
717 * responsible for allocating a list of format names and passing them to
718 * this routine.
719 */
720int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
721 char *modes[])
722{
723 struct drm_property *tv_selector;
724 struct drm_property *tv_subconnector;
725 int i;
726
727 if (dev->mode_config.tv_select_subconnector_property)
728 return 0;
729
730 /*
731 * Basic connector properties
732 */
733 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
734 "select subconnector",
735 ARRAY_SIZE(drm_tv_select_enum_list));
736 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
737 drm_property_add_enum(tv_selector, i,
738 drm_tv_select_enum_list[i].type,
739 drm_tv_select_enum_list[i].name);
740 dev->mode_config.tv_select_subconnector_property = tv_selector;
741
742 tv_subconnector =
743 drm_property_create(dev, DRM_MODE_PROP_ENUM |
744 DRM_MODE_PROP_IMMUTABLE, "subconnector",
745 ARRAY_SIZE(drm_tv_subconnector_enum_list));
746 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
747 drm_property_add_enum(tv_subconnector, i,
748 drm_tv_subconnector_enum_list[i].type,
749 drm_tv_subconnector_enum_list[i].name);
750 dev->mode_config.tv_subconnector_property = tv_subconnector;
751
752 /*
753 * Other, TV specific properties: margins & TV modes.
754 */
755 dev->mode_config.tv_left_margin_property =
756 drm_property_create(dev, DRM_MODE_PROP_RANGE,
757 "left margin", 2);
758 dev->mode_config.tv_left_margin_property->values[0] = 0;
759 dev->mode_config.tv_left_margin_property->values[1] = 100;
760
761 dev->mode_config.tv_right_margin_property =
762 drm_property_create(dev, DRM_MODE_PROP_RANGE,
763 "right margin", 2);
764 dev->mode_config.tv_right_margin_property->values[0] = 0;
765 dev->mode_config.tv_right_margin_property->values[1] = 100;
766
767 dev->mode_config.tv_top_margin_property =
768 drm_property_create(dev, DRM_MODE_PROP_RANGE,
769 "top margin", 2);
770 dev->mode_config.tv_top_margin_property->values[0] = 0;
771 dev->mode_config.tv_top_margin_property->values[1] = 100;
772
773 dev->mode_config.tv_bottom_margin_property =
774 drm_property_create(dev, DRM_MODE_PROP_RANGE,
775 "bottom margin", 2);
776 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
777 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
778
779 dev->mode_config.tv_mode_property =
780 drm_property_create(dev, DRM_MODE_PROP_ENUM,
781 "mode", num_modes);
782 for (i = 0; i < num_modes; i++)
783 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
784 i, modes[i]);
785
Francisco Jerezb6b79022009-08-02 04:19:20 +0200786 dev->mode_config.tv_brightness_property =
787 drm_property_create(dev, DRM_MODE_PROP_RANGE,
788 "brightness", 2);
789 dev->mode_config.tv_brightness_property->values[0] = 0;
790 dev->mode_config.tv_brightness_property->values[1] = 100;
791
792 dev->mode_config.tv_contrast_property =
793 drm_property_create(dev, DRM_MODE_PROP_RANGE,
794 "contrast", 2);
795 dev->mode_config.tv_contrast_property->values[0] = 0;
796 dev->mode_config.tv_contrast_property->values[1] = 100;
797
798 dev->mode_config.tv_flicker_reduction_property =
799 drm_property_create(dev, DRM_MODE_PROP_RANGE,
800 "flicker reduction", 2);
801 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
802 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
803
Francisco Jereza75f0232009-08-12 02:30:10 +0200804 dev->mode_config.tv_overscan_property =
805 drm_property_create(dev, DRM_MODE_PROP_RANGE,
806 "overscan", 2);
807 dev->mode_config.tv_overscan_property->values[0] = 0;
808 dev->mode_config.tv_overscan_property->values[1] = 100;
809
810 dev->mode_config.tv_saturation_property =
811 drm_property_create(dev, DRM_MODE_PROP_RANGE,
812 "saturation", 2);
813 dev->mode_config.tv_saturation_property->values[0] = 0;
814 dev->mode_config.tv_saturation_property->values[1] = 100;
815
816 dev->mode_config.tv_hue_property =
817 drm_property_create(dev, DRM_MODE_PROP_RANGE,
818 "hue", 2);
819 dev->mode_config.tv_hue_property->values[0] = 0;
820 dev->mode_config.tv_hue_property->values[1] = 100;
821
Dave Airlief453ba02008-11-07 14:05:41 -0800822 return 0;
823}
824EXPORT_SYMBOL(drm_mode_create_tv_properties);
825
826/**
827 * drm_mode_create_scaling_mode_property - create scaling mode property
828 * @dev: DRM device
829 *
830 * Called by a driver the first time it's needed, must be attached to desired
831 * connectors.
832 */
833int drm_mode_create_scaling_mode_property(struct drm_device *dev)
834{
835 struct drm_property *scaling_mode;
836 int i;
837
838 if (dev->mode_config.scaling_mode_property)
839 return 0;
840
841 scaling_mode =
842 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
843 ARRAY_SIZE(drm_scaling_mode_enum_list));
844 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
845 drm_property_add_enum(scaling_mode, i,
846 drm_scaling_mode_enum_list[i].type,
847 drm_scaling_mode_enum_list[i].name);
848
849 dev->mode_config.scaling_mode_property = scaling_mode;
850
851 return 0;
852}
853EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
854
855/**
856 * drm_mode_create_dithering_property - create dithering property
857 * @dev: DRM device
858 *
859 * Called by a driver the first time it's needed, must be attached to desired
860 * connectors.
861 */
862int drm_mode_create_dithering_property(struct drm_device *dev)
863{
864 struct drm_property *dithering_mode;
865 int i;
866
867 if (dev->mode_config.dithering_mode_property)
868 return 0;
869
870 dithering_mode =
871 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
872 ARRAY_SIZE(drm_dithering_mode_enum_list));
873 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
874 drm_property_add_enum(dithering_mode, i,
875 drm_dithering_mode_enum_list[i].type,
876 drm_dithering_mode_enum_list[i].name);
877 dev->mode_config.dithering_mode_property = dithering_mode;
878
879 return 0;
880}
881EXPORT_SYMBOL(drm_mode_create_dithering_property);
882
883/**
Jakob Bornecrantz884840a2009-12-03 23:25:47 +0000884 * drm_mode_create_dirty_property - create dirty property
885 * @dev: DRM device
886 *
887 * Called by a driver the first time it's needed, must be attached to desired
888 * connectors.
889 */
890int drm_mode_create_dirty_info_property(struct drm_device *dev)
891{
892 struct drm_property *dirty_info;
893 int i;
894
895 if (dev->mode_config.dirty_info_property)
896 return 0;
897
898 dirty_info =
899 drm_property_create(dev, DRM_MODE_PROP_ENUM |
900 DRM_MODE_PROP_IMMUTABLE,
901 "dirty",
902 ARRAY_SIZE(drm_dirty_info_enum_list));
903 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
904 drm_property_add_enum(dirty_info, i,
905 drm_dirty_info_enum_list[i].type,
906 drm_dirty_info_enum_list[i].name);
907 dev->mode_config.dirty_info_property = dirty_info;
908
909 return 0;
910}
911EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
912
913/**
Dave Airlief453ba02008-11-07 14:05:41 -0800914 * drm_mode_config_init - initialize DRM mode_configuration structure
915 * @dev: DRM device
916 *
917 * LOCKING:
918 * None, should happen single threaded at init time.
919 *
920 * Initialize @dev's mode_config structure, used for tracking the graphics
921 * configuration of @dev.
922 */
923void drm_mode_config_init(struct drm_device *dev)
924{
925 mutex_init(&dev->mode_config.mutex);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000926 mutex_init(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800927 INIT_LIST_HEAD(&dev->mode_config.fb_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800928 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
929 INIT_LIST_HEAD(&dev->mode_config.connector_list);
930 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
931 INIT_LIST_HEAD(&dev->mode_config.property_list);
932 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
Jesse Barnes8cf5c912011-11-14 14:51:27 -0800933 INIT_LIST_HEAD(&dev->mode_config.plane_list);
Dave Airlief453ba02008-11-07 14:05:41 -0800934 idr_init(&dev->mode_config.crtc_idr);
935
936 mutex_lock(&dev->mode_config.mutex);
937 drm_mode_create_standard_connector_properties(dev);
938 mutex_unlock(&dev->mode_config.mutex);
939
940 /* Just to be sure */
941 dev->mode_config.num_fb = 0;
942 dev->mode_config.num_connector = 0;
943 dev->mode_config.num_crtc = 0;
944 dev->mode_config.num_encoder = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800945}
946EXPORT_SYMBOL(drm_mode_config_init);
947
948int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
949{
950 uint32_t total_objects = 0;
951
952 total_objects += dev->mode_config.num_crtc;
953 total_objects += dev->mode_config.num_connector;
954 total_objects += dev->mode_config.num_encoder;
955
Dave Airlief453ba02008-11-07 14:05:41 -0800956 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
957 if (!group->id_list)
958 return -ENOMEM;
959
960 group->num_crtcs = 0;
961 group->num_connectors = 0;
962 group->num_encoders = 0;
963 return 0;
964}
965
966int drm_mode_group_init_legacy_group(struct drm_device *dev,
967 struct drm_mode_group *group)
968{
969 struct drm_crtc *crtc;
970 struct drm_encoder *encoder;
971 struct drm_connector *connector;
972 int ret;
973
974 if ((ret = drm_mode_group_init(dev, group)))
975 return ret;
976
977 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
978 group->id_list[group->num_crtcs++] = crtc->base.id;
979
980 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
981 group->id_list[group->num_crtcs + group->num_encoders++] =
982 encoder->base.id;
983
984 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
985 group->id_list[group->num_crtcs + group->num_encoders +
986 group->num_connectors++] = connector->base.id;
987
988 return 0;
989}
990
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 }
Dave Airlief453ba02008-11-07 14:05:41 -08001039}
1040EXPORT_SYMBOL(drm_mode_config_cleanup);
1041
Dave Airlief453ba02008-11-07 14:05:41 -08001042/**
1043 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1044 * @out: drm_mode_modeinfo struct to return to the user
1045 * @in: drm_display_mode to use
1046 *
1047 * LOCKING:
1048 * None.
1049 *
1050 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1051 * the user.
1052 */
1053void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1054 struct drm_display_mode *in)
1055{
1056 out->clock = in->clock;
1057 out->hdisplay = in->hdisplay;
1058 out->hsync_start = in->hsync_start;
1059 out->hsync_end = in->hsync_end;
1060 out->htotal = in->htotal;
1061 out->hskew = in->hskew;
1062 out->vdisplay = in->vdisplay;
1063 out->vsync_start = in->vsync_start;
1064 out->vsync_end = in->vsync_end;
1065 out->vtotal = in->vtotal;
1066 out->vscan = in->vscan;
1067 out->vrefresh = in->vrefresh;
1068 out->flags = in->flags;
1069 out->type = in->type;
1070 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1071 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1072}
1073
1074/**
1075 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1076 * @out: drm_display_mode to return to the user
1077 * @in: drm_mode_modeinfo to use
1078 *
1079 * LOCKING:
1080 * None.
1081 *
1082 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1083 * the caller.
1084 */
1085void drm_crtc_convert_umode(struct drm_display_mode *out,
1086 struct drm_mode_modeinfo *in)
1087{
1088 out->clock = in->clock;
1089 out->hdisplay = in->hdisplay;
1090 out->hsync_start = in->hsync_start;
1091 out->hsync_end = in->hsync_end;
1092 out->htotal = in->htotal;
1093 out->hskew = in->hskew;
1094 out->vdisplay = in->vdisplay;
1095 out->vsync_start = in->vsync_start;
1096 out->vsync_end = in->vsync_end;
1097 out->vtotal = in->vtotal;
1098 out->vscan = in->vscan;
1099 out->vrefresh = in->vrefresh;
1100 out->flags = in->flags;
1101 out->type = in->type;
1102 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1103 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1104}
1105
1106/**
1107 * drm_mode_getresources - get graphics configuration
1108 * @inode: inode from the ioctl
1109 * @filp: file * from the ioctl
1110 * @cmd: cmd from ioctl
1111 * @arg: arg from ioctl
1112 *
1113 * LOCKING:
1114 * Takes mode config lock.
1115 *
1116 * Construct a set of configuration description structures and return
1117 * them to the user, including CRTC, connector and framebuffer configuration.
1118 *
1119 * Called by the user via ioctl.
1120 *
1121 * RETURNS:
1122 * Zero on success, errno on failure.
1123 */
1124int drm_mode_getresources(struct drm_device *dev, void *data,
1125 struct drm_file *file_priv)
1126{
1127 struct drm_mode_card_res *card_res = data;
1128 struct list_head *lh;
1129 struct drm_framebuffer *fb;
1130 struct drm_connector *connector;
1131 struct drm_crtc *crtc;
1132 struct drm_encoder *encoder;
1133 int ret = 0;
1134 int connector_count = 0;
1135 int crtc_count = 0;
1136 int fb_count = 0;
1137 int encoder_count = 0;
1138 int copied = 0, i;
1139 uint32_t __user *fb_id;
1140 uint32_t __user *crtc_id;
1141 uint32_t __user *connector_id;
1142 uint32_t __user *encoder_id;
1143 struct drm_mode_group *mode_group;
1144
Dave Airliefb3b06c2011-02-08 13:55:21 +10001145 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1146 return -EINVAL;
1147
Dave Airlief453ba02008-11-07 14:05:41 -08001148 mutex_lock(&dev->mode_config.mutex);
1149
1150 /*
1151 * For the non-control nodes we need to limit the list of resources
1152 * by IDs in the group list for this node
1153 */
1154 list_for_each(lh, &file_priv->fbs)
1155 fb_count++;
1156
1157 mode_group = &file_priv->master->minor->mode_group;
1158 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1159
1160 list_for_each(lh, &dev->mode_config.crtc_list)
1161 crtc_count++;
1162
1163 list_for_each(lh, &dev->mode_config.connector_list)
1164 connector_count++;
1165
1166 list_for_each(lh, &dev->mode_config.encoder_list)
1167 encoder_count++;
1168 } else {
1169
1170 crtc_count = mode_group->num_crtcs;
1171 connector_count = mode_group->num_connectors;
1172 encoder_count = mode_group->num_encoders;
1173 }
1174
1175 card_res->max_height = dev->mode_config.max_height;
1176 card_res->min_height = dev->mode_config.min_height;
1177 card_res->max_width = dev->mode_config.max_width;
1178 card_res->min_width = dev->mode_config.min_width;
1179
1180 /* handle this in 4 parts */
1181 /* FBs */
1182 if (card_res->count_fbs >= fb_count) {
1183 copied = 0;
1184 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
Sascha Hauer618c75e2011-06-03 12:54:14 +02001185 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
Dave Airlief453ba02008-11-07 14:05:41 -08001186 if (put_user(fb->base.id, fb_id + copied)) {
1187 ret = -EFAULT;
1188 goto out;
1189 }
1190 copied++;
1191 }
1192 }
1193 card_res->count_fbs = fb_count;
1194
1195 /* CRTCs */
1196 if (card_res->count_crtcs >= crtc_count) {
1197 copied = 0;
1198 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1199 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1200 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1201 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001202 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001203 if (put_user(crtc->base.id, crtc_id + copied)) {
1204 ret = -EFAULT;
1205 goto out;
1206 }
1207 copied++;
1208 }
1209 } else {
1210 for (i = 0; i < mode_group->num_crtcs; i++) {
1211 if (put_user(mode_group->id_list[i],
1212 crtc_id + copied)) {
1213 ret = -EFAULT;
1214 goto out;
1215 }
1216 copied++;
1217 }
1218 }
1219 }
1220 card_res->count_crtcs = crtc_count;
1221
1222 /* Encoders */
1223 if (card_res->count_encoders >= encoder_count) {
1224 copied = 0;
1225 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1226 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1227 list_for_each_entry(encoder,
1228 &dev->mode_config.encoder_list,
1229 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001230 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1231 drm_get_encoder_name(encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001232 if (put_user(encoder->base.id, encoder_id +
1233 copied)) {
1234 ret = -EFAULT;
1235 goto out;
1236 }
1237 copied++;
1238 }
1239 } else {
1240 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1241 if (put_user(mode_group->id_list[i],
1242 encoder_id + copied)) {
1243 ret = -EFAULT;
1244 goto out;
1245 }
1246 copied++;
1247 }
1248
1249 }
1250 }
1251 card_res->count_encoders = encoder_count;
1252
1253 /* Connectors */
1254 if (card_res->count_connectors >= connector_count) {
1255 copied = 0;
1256 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1257 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1258 list_for_each_entry(connector,
1259 &dev->mode_config.connector_list,
1260 head) {
Jerome Glisse94401062010-07-15 15:43:25 -04001261 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1262 connector->base.id,
1263 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001264 if (put_user(connector->base.id,
1265 connector_id + copied)) {
1266 ret = -EFAULT;
1267 goto out;
1268 }
1269 copied++;
1270 }
1271 } else {
1272 int start = mode_group->num_crtcs +
1273 mode_group->num_encoders;
1274 for (i = start; i < start + mode_group->num_connectors; i++) {
1275 if (put_user(mode_group->id_list[i],
1276 connector_id + copied)) {
1277 ret = -EFAULT;
1278 goto out;
1279 }
1280 copied++;
1281 }
1282 }
1283 }
1284 card_res->count_connectors = connector_count;
1285
Jerome Glisse94401062010-07-15 15:43:25 -04001286 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
Dave Airlief453ba02008-11-07 14:05:41 -08001287 card_res->count_connectors, card_res->count_encoders);
1288
1289out:
1290 mutex_unlock(&dev->mode_config.mutex);
1291 return ret;
1292}
1293
1294/**
1295 * drm_mode_getcrtc - get CRTC configuration
1296 * @inode: inode from the ioctl
1297 * @filp: file * from the ioctl
1298 * @cmd: cmd from ioctl
1299 * @arg: arg from ioctl
1300 *
1301 * LOCKING:
1302 * Caller? (FIXME)
1303 *
1304 * Construct a CRTC configuration structure to return to the user.
1305 *
1306 * Called by the user via ioctl.
1307 *
1308 * RETURNS:
1309 * Zero on success, errno on failure.
1310 */
1311int drm_mode_getcrtc(struct drm_device *dev,
1312 void *data, struct drm_file *file_priv)
1313{
1314 struct drm_mode_crtc *crtc_resp = data;
1315 struct drm_crtc *crtc;
1316 struct drm_mode_object *obj;
1317 int ret = 0;
1318
Dave Airliefb3b06c2011-02-08 13:55:21 +10001319 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1320 return -EINVAL;
1321
Dave Airlief453ba02008-11-07 14:05:41 -08001322 mutex_lock(&dev->mode_config.mutex);
1323
1324 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1325 DRM_MODE_OBJECT_CRTC);
1326 if (!obj) {
1327 ret = -EINVAL;
1328 goto out;
1329 }
1330 crtc = obj_to_crtc(obj);
1331
1332 crtc_resp->x = crtc->x;
1333 crtc_resp->y = crtc->y;
1334 crtc_resp->gamma_size = crtc->gamma_size;
1335 if (crtc->fb)
1336 crtc_resp->fb_id = crtc->fb->base.id;
1337 else
1338 crtc_resp->fb_id = 0;
1339
1340 if (crtc->enabled) {
1341
1342 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1343 crtc_resp->mode_valid = 1;
1344
1345 } else {
1346 crtc_resp->mode_valid = 0;
1347 }
1348
1349out:
1350 mutex_unlock(&dev->mode_config.mutex);
1351 return ret;
1352}
1353
1354/**
1355 * drm_mode_getconnector - get connector configuration
1356 * @inode: inode from the ioctl
1357 * @filp: file * from the ioctl
1358 * @cmd: cmd from ioctl
1359 * @arg: arg from ioctl
1360 *
1361 * LOCKING:
1362 * Caller? (FIXME)
1363 *
1364 * Construct a connector configuration structure to return to the user.
1365 *
1366 * Called by the user via ioctl.
1367 *
1368 * RETURNS:
1369 * Zero on success, errno on failure.
1370 */
1371int drm_mode_getconnector(struct drm_device *dev, void *data,
1372 struct drm_file *file_priv)
1373{
1374 struct drm_mode_get_connector *out_resp = data;
1375 struct drm_mode_object *obj;
1376 struct drm_connector *connector;
1377 struct drm_display_mode *mode;
1378 int mode_count = 0;
1379 int props_count = 0;
1380 int encoders_count = 0;
1381 int ret = 0;
1382 int copied = 0;
1383 int i;
1384 struct drm_mode_modeinfo u_mode;
1385 struct drm_mode_modeinfo __user *mode_ptr;
1386 uint32_t __user *prop_ptr;
1387 uint64_t __user *prop_values;
1388 uint32_t __user *encoder_ptr;
1389
Dave Airliefb3b06c2011-02-08 13:55:21 +10001390 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1391 return -EINVAL;
1392
Dave Airlief453ba02008-11-07 14:05:41 -08001393 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1394
Jerome Glisse94401062010-07-15 15:43:25 -04001395 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001396
1397 mutex_lock(&dev->mode_config.mutex);
1398
1399 obj = drm_mode_object_find(dev, out_resp->connector_id,
1400 DRM_MODE_OBJECT_CONNECTOR);
1401 if (!obj) {
1402 ret = -EINVAL;
1403 goto out;
1404 }
1405 connector = obj_to_connector(obj);
1406
1407 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1408 if (connector->property_ids[i] != 0) {
1409 props_count++;
1410 }
1411 }
1412
1413 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1414 if (connector->encoder_ids[i] != 0) {
1415 encoders_count++;
1416 }
1417 }
1418
1419 if (out_resp->count_modes == 0) {
1420 connector->funcs->fill_modes(connector,
1421 dev->mode_config.max_width,
1422 dev->mode_config.max_height);
1423 }
1424
1425 /* delayed so we get modes regardless of pre-fill_modes state */
1426 list_for_each_entry(mode, &connector->modes, head)
1427 mode_count++;
1428
1429 out_resp->connector_id = connector->base.id;
1430 out_resp->connector_type = connector->connector_type;
1431 out_resp->connector_type_id = connector->connector_type_id;
1432 out_resp->mm_width = connector->display_info.width_mm;
1433 out_resp->mm_height = connector->display_info.height_mm;
1434 out_resp->subpixel = connector->display_info.subpixel_order;
1435 out_resp->connection = connector->status;
1436 if (connector->encoder)
1437 out_resp->encoder_id = connector->encoder->base.id;
1438 else
1439 out_resp->encoder_id = 0;
1440
1441 /*
1442 * This ioctl is called twice, once to determine how much space is
1443 * needed, and the 2nd time to fill it.
1444 */
1445 if ((out_resp->count_modes >= mode_count) && mode_count) {
1446 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001447 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001448 list_for_each_entry(mode, &connector->modes, head) {
1449 drm_crtc_convert_to_umode(&u_mode, mode);
1450 if (copy_to_user(mode_ptr + copied,
1451 &u_mode, sizeof(u_mode))) {
1452 ret = -EFAULT;
1453 goto out;
1454 }
1455 copied++;
1456 }
1457 }
1458 out_resp->count_modes = mode_count;
1459
1460 if ((out_resp->count_props >= props_count) && props_count) {
1461 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001462 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1463 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001464 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1465 if (connector->property_ids[i] != 0) {
1466 if (put_user(connector->property_ids[i],
1467 prop_ptr + copied)) {
1468 ret = -EFAULT;
1469 goto out;
1470 }
1471
1472 if (put_user(connector->property_values[i],
1473 prop_values + copied)) {
1474 ret = -EFAULT;
1475 goto out;
1476 }
1477 copied++;
1478 }
1479 }
1480 }
1481 out_resp->count_props = props_count;
1482
1483 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1484 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001485 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08001486 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1487 if (connector->encoder_ids[i] != 0) {
1488 if (put_user(connector->encoder_ids[i],
1489 encoder_ptr + copied)) {
1490 ret = -EFAULT;
1491 goto out;
1492 }
1493 copied++;
1494 }
1495 }
1496 }
1497 out_resp->count_encoders = encoders_count;
1498
1499out:
1500 mutex_unlock(&dev->mode_config.mutex);
1501 return ret;
1502}
1503
1504int drm_mode_getencoder(struct drm_device *dev, void *data,
1505 struct drm_file *file_priv)
1506{
1507 struct drm_mode_get_encoder *enc_resp = data;
1508 struct drm_mode_object *obj;
1509 struct drm_encoder *encoder;
1510 int ret = 0;
1511
Dave Airliefb3b06c2011-02-08 13:55:21 +10001512 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1513 return -EINVAL;
1514
Dave Airlief453ba02008-11-07 14:05:41 -08001515 mutex_lock(&dev->mode_config.mutex);
1516 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1517 DRM_MODE_OBJECT_ENCODER);
1518 if (!obj) {
1519 ret = -EINVAL;
1520 goto out;
1521 }
1522 encoder = obj_to_encoder(obj);
1523
1524 if (encoder->crtc)
1525 enc_resp->crtc_id = encoder->crtc->base.id;
1526 else
1527 enc_resp->crtc_id = 0;
1528 enc_resp->encoder_type = encoder->encoder_type;
1529 enc_resp->encoder_id = encoder->base.id;
1530 enc_resp->possible_crtcs = encoder->possible_crtcs;
1531 enc_resp->possible_clones = encoder->possible_clones;
1532
1533out:
1534 mutex_unlock(&dev->mode_config.mutex);
1535 return ret;
1536}
1537
1538/**
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001539 * drm_mode_getplane_res - get plane info
1540 * @dev: DRM device
1541 * @data: ioctl data
1542 * @file_priv: DRM file info
1543 *
1544 * Return an plane count and set of IDs.
1545 */
1546int drm_mode_getplane_res(struct drm_device *dev, void *data,
1547 struct drm_file *file_priv)
1548{
1549 struct drm_mode_get_plane_res *plane_resp = data;
1550 struct drm_mode_config *config;
1551 struct drm_plane *plane;
1552 uint32_t __user *plane_ptr;
1553 int copied = 0, ret = 0;
1554
1555 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1556 return -EINVAL;
1557
1558 mutex_lock(&dev->mode_config.mutex);
1559 config = &dev->mode_config;
1560
1561 /*
1562 * This ioctl is called twice, once to determine how much space is
1563 * needed, and the 2nd time to fill it.
1564 */
1565 if (config->num_plane &&
1566 (plane_resp->count_planes >= config->num_plane)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001567 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001568
1569 list_for_each_entry(plane, &config->plane_list, head) {
1570 if (put_user(plane->base.id, plane_ptr + copied)) {
1571 ret = -EFAULT;
1572 goto out;
1573 }
1574 copied++;
1575 }
1576 }
1577 plane_resp->count_planes = config->num_plane;
1578
1579out:
1580 mutex_unlock(&dev->mode_config.mutex);
1581 return ret;
1582}
1583
1584/**
1585 * drm_mode_getplane - get plane info
1586 * @dev: DRM device
1587 * @data: ioctl data
1588 * @file_priv: DRM file info
1589 *
1590 * Return plane info, including formats supported, gamma size, any
1591 * current fb, etc.
1592 */
1593int drm_mode_getplane(struct drm_device *dev, void *data,
1594 struct drm_file *file_priv)
1595{
1596 struct drm_mode_get_plane *plane_resp = data;
1597 struct drm_mode_object *obj;
1598 struct drm_plane *plane;
1599 uint32_t __user *format_ptr;
1600 int ret = 0;
1601
1602 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1603 return -EINVAL;
1604
1605 mutex_lock(&dev->mode_config.mutex);
1606 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1607 DRM_MODE_OBJECT_PLANE);
1608 if (!obj) {
1609 ret = -ENOENT;
1610 goto out;
1611 }
1612 plane = obj_to_plane(obj);
1613
1614 if (plane->crtc)
1615 plane_resp->crtc_id = plane->crtc->base.id;
1616 else
1617 plane_resp->crtc_id = 0;
1618
1619 if (plane->fb)
1620 plane_resp->fb_id = plane->fb->base.id;
1621 else
1622 plane_resp->fb_id = 0;
1623
1624 plane_resp->plane_id = plane->base.id;
1625 plane_resp->possible_crtcs = plane->possible_crtcs;
1626 plane_resp->gamma_size = plane->gamma_size;
1627
1628 /*
1629 * This ioctl is called twice, once to determine how much space is
1630 * needed, and the 2nd time to fill it.
1631 */
1632 if (plane->format_count &&
1633 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001634 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001635 if (copy_to_user(format_ptr,
1636 plane->format_types,
1637 sizeof(uint32_t) * plane->format_count)) {
1638 ret = -EFAULT;
1639 goto out;
1640 }
1641 }
1642 plane_resp->count_format_types = plane->format_count;
1643
1644out:
1645 mutex_unlock(&dev->mode_config.mutex);
1646 return ret;
1647}
1648
1649/**
1650 * drm_mode_setplane - set up or tear down an plane
1651 * @dev: DRM device
1652 * @data: ioctl data*
1653 * @file_prive: DRM file info
1654 *
1655 * Set plane info, including placement, fb, scaling, and other factors.
1656 * Or pass a NULL fb to disable.
1657 */
1658int drm_mode_setplane(struct drm_device *dev, void *data,
1659 struct drm_file *file_priv)
1660{
1661 struct drm_mode_set_plane *plane_req = data;
1662 struct drm_mode_object *obj;
1663 struct drm_plane *plane;
1664 struct drm_crtc *crtc;
1665 struct drm_framebuffer *fb;
1666 int ret = 0;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001667 unsigned int fb_width, fb_height;
Ville Syrjälä62443be2011-12-20 00:06:47 +02001668 int i;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001669
1670 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1671 return -EINVAL;
1672
1673 mutex_lock(&dev->mode_config.mutex);
1674
1675 /*
1676 * First, find the plane, crtc, and fb objects. If not available,
1677 * we don't bother to call the driver.
1678 */
1679 obj = drm_mode_object_find(dev, plane_req->plane_id,
1680 DRM_MODE_OBJECT_PLANE);
1681 if (!obj) {
1682 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1683 plane_req->plane_id);
1684 ret = -ENOENT;
1685 goto out;
1686 }
1687 plane = obj_to_plane(obj);
1688
1689 /* No fb means shut it down */
1690 if (!plane_req->fb_id) {
1691 plane->funcs->disable_plane(plane);
Ville Syrjäläe5e3b442011-12-20 00:06:43 +02001692 plane->crtc = NULL;
1693 plane->fb = NULL;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001694 goto out;
1695 }
1696
1697 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1698 DRM_MODE_OBJECT_CRTC);
1699 if (!obj) {
1700 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1701 plane_req->crtc_id);
1702 ret = -ENOENT;
1703 goto out;
1704 }
1705 crtc = obj_to_crtc(obj);
1706
1707 obj = drm_mode_object_find(dev, plane_req->fb_id,
1708 DRM_MODE_OBJECT_FB);
1709 if (!obj) {
1710 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1711 plane_req->fb_id);
1712 ret = -ENOENT;
1713 goto out;
1714 }
1715 fb = obj_to_fb(obj);
1716
Ville Syrjälä62443be2011-12-20 00:06:47 +02001717 /* Check whether this plane supports the fb pixel format. */
1718 for (i = 0; i < plane->format_count; i++)
1719 if (fb->pixel_format == plane->format_types[i])
1720 break;
1721 if (i == plane->format_count) {
1722 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1723 ret = -EINVAL;
1724 goto out;
1725 }
1726
Ville Syrjälä42ef8782011-12-20 00:06:44 +02001727 fb_width = fb->width << 16;
1728 fb_height = fb->height << 16;
1729
1730 /* Make sure source coordinates are inside the fb. */
1731 if (plane_req->src_w > fb_width ||
1732 plane_req->src_x > fb_width - plane_req->src_w ||
1733 plane_req->src_h > fb_height ||
1734 plane_req->src_y > fb_height - plane_req->src_h) {
1735 DRM_DEBUG_KMS("Invalid source coordinates "
1736 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1737 plane_req->src_w >> 16,
1738 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1739 plane_req->src_h >> 16,
1740 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1741 plane_req->src_x >> 16,
1742 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1743 plane_req->src_y >> 16,
1744 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1745 ret = -ENOSPC;
1746 goto out;
1747 }
1748
Ville Syrjälä687a0402011-12-20 00:06:45 +02001749 /* Give drivers some help against integer overflows */
1750 if (plane_req->crtc_w > INT_MAX ||
1751 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1752 plane_req->crtc_h > INT_MAX ||
1753 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1754 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1755 plane_req->crtc_w, plane_req->crtc_h,
1756 plane_req->crtc_x, plane_req->crtc_y);
1757 ret = -ERANGE;
1758 goto out;
1759 }
1760
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001761 ret = plane->funcs->update_plane(plane, crtc, fb,
1762 plane_req->crtc_x, plane_req->crtc_y,
1763 plane_req->crtc_w, plane_req->crtc_h,
1764 plane_req->src_x, plane_req->src_y,
1765 plane_req->src_w, plane_req->src_h);
1766 if (!ret) {
1767 plane->crtc = crtc;
1768 plane->fb = fb;
1769 }
1770
1771out:
1772 mutex_unlock(&dev->mode_config.mutex);
1773
1774 return ret;
1775}
1776
1777/**
Dave Airlief453ba02008-11-07 14:05:41 -08001778 * drm_mode_setcrtc - set CRTC configuration
1779 * @inode: inode from the ioctl
1780 * @filp: file * from the ioctl
1781 * @cmd: cmd from ioctl
1782 * @arg: arg from ioctl
1783 *
1784 * LOCKING:
1785 * Caller? (FIXME)
1786 *
1787 * Build a new CRTC configuration based on user request.
1788 *
1789 * Called by the user via ioctl.
1790 *
1791 * RETURNS:
1792 * Zero on success, errno on failure.
1793 */
1794int drm_mode_setcrtc(struct drm_device *dev, void *data,
1795 struct drm_file *file_priv)
1796{
1797 struct drm_mode_config *config = &dev->mode_config;
1798 struct drm_mode_crtc *crtc_req = data;
1799 struct drm_mode_object *obj;
1800 struct drm_crtc *crtc, *crtcfb;
1801 struct drm_connector **connector_set = NULL, *connector;
1802 struct drm_framebuffer *fb = NULL;
1803 struct drm_display_mode *mode = NULL;
1804 struct drm_mode_set set;
1805 uint32_t __user *set_connectors_ptr;
1806 int ret = 0;
1807 int i;
1808
Dave Airliefb3b06c2011-02-08 13:55:21 +10001809 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1810 return -EINVAL;
1811
Dave Airlief453ba02008-11-07 14:05:41 -08001812 mutex_lock(&dev->mode_config.mutex);
1813 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1814 DRM_MODE_OBJECT_CRTC);
1815 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001816 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001817 ret = -EINVAL;
1818 goto out;
1819 }
1820 crtc = obj_to_crtc(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001821 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08001822
1823 if (crtc_req->mode_valid) {
1824 /* If we have a mode we need a framebuffer. */
1825 /* If we pass -1, set the mode with the currently bound fb */
1826 if (crtc_req->fb_id == -1) {
1827 list_for_each_entry(crtcfb,
1828 &dev->mode_config.crtc_list, head) {
1829 if (crtcfb == crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001830 DRM_DEBUG_KMS("Using current fb for "
1831 "setmode\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001832 fb = crtc->fb;
1833 }
1834 }
1835 } else {
1836 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1837 DRM_MODE_OBJECT_FB);
1838 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001839 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1840 crtc_req->fb_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001841 ret = -EINVAL;
1842 goto out;
1843 }
1844 fb = obj_to_fb(obj);
1845 }
1846
1847 mode = drm_mode_create(dev);
1848 drm_crtc_convert_umode(mode, &crtc_req->mode);
1849 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1850 }
1851
1852 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001853 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08001854 ret = -EINVAL;
1855 goto out;
1856 }
1857
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01001858 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001859 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08001860 crtc_req->count_connectors);
1861 ret = -EINVAL;
1862 goto out;
1863 }
1864
1865 if (crtc_req->count_connectors > 0) {
1866 u32 out_id;
1867
1868 /* Avoid unbounded kernel memory allocation */
1869 if (crtc_req->count_connectors > config->num_connector) {
1870 ret = -EINVAL;
1871 goto out;
1872 }
1873
1874 connector_set = kmalloc(crtc_req->count_connectors *
1875 sizeof(struct drm_connector *),
1876 GFP_KERNEL);
1877 if (!connector_set) {
1878 ret = -ENOMEM;
1879 goto out;
1880 }
1881
1882 for (i = 0; i < crtc_req->count_connectors; i++) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02001883 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001884 if (get_user(out_id, &set_connectors_ptr[i])) {
1885 ret = -EFAULT;
1886 goto out;
1887 }
1888
1889 obj = drm_mode_object_find(dev, out_id,
1890 DRM_MODE_OBJECT_CONNECTOR);
1891 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001892 DRM_DEBUG_KMS("Connector id %d unknown\n",
1893 out_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001894 ret = -EINVAL;
1895 goto out;
1896 }
1897 connector = obj_to_connector(obj);
Jerome Glisse94401062010-07-15 15:43:25 -04001898 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1899 connector->base.id,
1900 drm_get_connector_name(connector));
Dave Airlief453ba02008-11-07 14:05:41 -08001901
1902 connector_set[i] = connector;
1903 }
1904 }
1905
1906 set.crtc = crtc;
1907 set.x = crtc_req->x;
1908 set.y = crtc_req->y;
1909 set.mode = mode;
1910 set.connectors = connector_set;
1911 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10001912 set.fb = fb;
Dave Airlief453ba02008-11-07 14:05:41 -08001913 ret = crtc->funcs->set_config(&set);
1914
1915out:
1916 kfree(connector_set);
1917 mutex_unlock(&dev->mode_config.mutex);
1918 return ret;
1919}
1920
1921int drm_mode_cursor_ioctl(struct drm_device *dev,
1922 void *data, struct drm_file *file_priv)
1923{
1924 struct drm_mode_cursor *req = data;
1925 struct drm_mode_object *obj;
1926 struct drm_crtc *crtc;
1927 int ret = 0;
1928
Dave Airliefb3b06c2011-02-08 13:55:21 +10001929 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1930 return -EINVAL;
1931
Jesse Barnesacb4b992011-11-07 12:03:23 -08001932 if (!req->flags)
Dave Airlief453ba02008-11-07 14:05:41 -08001933 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08001934
1935 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10001936 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
Dave Airlief453ba02008-11-07 14:05:41 -08001937 if (!obj) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08001938 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Dave Airlief453ba02008-11-07 14:05:41 -08001939 ret = -EINVAL;
1940 goto out;
1941 }
1942 crtc = obj_to_crtc(obj);
1943
1944 if (req->flags & DRM_MODE_CURSOR_BO) {
1945 if (!crtc->funcs->cursor_set) {
Dave Airlief453ba02008-11-07 14:05:41 -08001946 ret = -ENXIO;
1947 goto out;
1948 }
1949 /* Turns off the cursor if handle is 0 */
1950 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1951 req->width, req->height);
1952 }
1953
1954 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1955 if (crtc->funcs->cursor_move) {
1956 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1957 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08001958 ret = -EFAULT;
1959 goto out;
1960 }
1961 }
1962out:
1963 mutex_unlock(&dev->mode_config.mutex);
1964 return ret;
1965}
1966
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001967/* Original addfb only supported RGB formats, so figure out which one */
1968uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
1969{
1970 uint32_t fmt;
1971
1972 switch (bpp) {
1973 case 8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001974 fmt = DRM_FORMAT_RGB332;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001975 break;
1976 case 16:
1977 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001978 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001979 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001980 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001981 break;
1982 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001983 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001984 break;
1985 case 32:
1986 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001987 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001988 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02001989 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001990 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02001991 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001992 break;
1993 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02001994 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
1995 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001996 break;
1997 }
1998
1999 return fmt;
2000}
2001EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2002
Dave Airlief453ba02008-11-07 14:05:41 -08002003/**
2004 * drm_mode_addfb - add an FB to the graphics configuration
2005 * @inode: inode from the ioctl
2006 * @filp: file * from the ioctl
2007 * @cmd: cmd from ioctl
2008 * @arg: arg from ioctl
2009 *
2010 * LOCKING:
2011 * Takes mode config lock.
2012 *
2013 * Add a new FB to the specified CRTC, given a user request.
2014 *
2015 * Called by the user via ioctl.
2016 *
2017 * RETURNS:
2018 * Zero on success, errno on failure.
2019 */
2020int drm_mode_addfb(struct drm_device *dev,
2021 void *data, struct drm_file *file_priv)
2022{
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002023 struct drm_mode_fb_cmd *or = data;
2024 struct drm_mode_fb_cmd2 r = {};
2025 struct drm_mode_config *config = &dev->mode_config;
2026 struct drm_framebuffer *fb;
2027 int ret = 0;
2028
2029 /* Use new struct with format internally */
2030 r.fb_id = or->fb_id;
2031 r.width = or->width;
2032 r.height = or->height;
2033 r.pitches[0] = or->pitch;
2034 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2035 r.handles[0] = or->handle;
2036
2037 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2038 return -EINVAL;
2039
Jesse Barnesacb4b992011-11-07 12:03:23 -08002040 if ((config->min_width > r.width) || (r.width > config->max_width))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002041 return -EINVAL;
Jesse Barnesacb4b992011-11-07 12:03:23 -08002042
2043 if ((config->min_height > r.height) || (r.height > config->max_height))
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002044 return -EINVAL;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002045
2046 mutex_lock(&dev->mode_config.mutex);
2047
2048 /* TODO check buffer is sufficiently large */
2049 /* TODO setup destructor callback */
2050
2051 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2052 if (IS_ERR(fb)) {
2053 DRM_ERROR("could not create framebuffer\n");
2054 ret = PTR_ERR(fb);
2055 goto out;
2056 }
2057
2058 or->fb_id = fb->base.id;
2059 list_add(&fb->filp_head, &file_priv->fbs);
2060 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2061
2062out:
2063 mutex_unlock(&dev->mode_config.mutex);
2064 return ret;
2065}
2066
Ville Syrjälä935b5972011-12-20 00:06:48 +02002067static int format_check(struct drm_mode_fb_cmd2 *r)
2068{
2069 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2070
2071 switch (format) {
2072 case DRM_FORMAT_C8:
2073 case DRM_FORMAT_RGB332:
2074 case DRM_FORMAT_BGR233:
2075 case DRM_FORMAT_XRGB4444:
2076 case DRM_FORMAT_XBGR4444:
2077 case DRM_FORMAT_RGBX4444:
2078 case DRM_FORMAT_BGRX4444:
2079 case DRM_FORMAT_ARGB4444:
2080 case DRM_FORMAT_ABGR4444:
2081 case DRM_FORMAT_RGBA4444:
2082 case DRM_FORMAT_BGRA4444:
2083 case DRM_FORMAT_XRGB1555:
2084 case DRM_FORMAT_XBGR1555:
2085 case DRM_FORMAT_RGBX5551:
2086 case DRM_FORMAT_BGRX5551:
2087 case DRM_FORMAT_ARGB1555:
2088 case DRM_FORMAT_ABGR1555:
2089 case DRM_FORMAT_RGBA5551:
2090 case DRM_FORMAT_BGRA5551:
2091 case DRM_FORMAT_RGB565:
2092 case DRM_FORMAT_BGR565:
2093 case DRM_FORMAT_RGB888:
2094 case DRM_FORMAT_BGR888:
2095 case DRM_FORMAT_XRGB8888:
2096 case DRM_FORMAT_XBGR8888:
2097 case DRM_FORMAT_RGBX8888:
2098 case DRM_FORMAT_BGRX8888:
2099 case DRM_FORMAT_ARGB8888:
2100 case DRM_FORMAT_ABGR8888:
2101 case DRM_FORMAT_RGBA8888:
2102 case DRM_FORMAT_BGRA8888:
2103 case DRM_FORMAT_XRGB2101010:
2104 case DRM_FORMAT_XBGR2101010:
2105 case DRM_FORMAT_RGBX1010102:
2106 case DRM_FORMAT_BGRX1010102:
2107 case DRM_FORMAT_ARGB2101010:
2108 case DRM_FORMAT_ABGR2101010:
2109 case DRM_FORMAT_RGBA1010102:
2110 case DRM_FORMAT_BGRA1010102:
2111 case DRM_FORMAT_YUYV:
2112 case DRM_FORMAT_YVYU:
2113 case DRM_FORMAT_UYVY:
2114 case DRM_FORMAT_VYUY:
2115 case DRM_FORMAT_AYUV:
2116 case DRM_FORMAT_NV12:
2117 case DRM_FORMAT_NV21:
2118 case DRM_FORMAT_NV16:
2119 case DRM_FORMAT_NV61:
2120 case DRM_FORMAT_YUV410:
2121 case DRM_FORMAT_YVU410:
2122 case DRM_FORMAT_YUV411:
2123 case DRM_FORMAT_YVU411:
2124 case DRM_FORMAT_YUV420:
2125 case DRM_FORMAT_YVU420:
2126 case DRM_FORMAT_YUV422:
2127 case DRM_FORMAT_YVU422:
2128 case DRM_FORMAT_YUV444:
2129 case DRM_FORMAT_YVU444:
2130 return 0;
2131 default:
2132 return -EINVAL;
2133 }
2134}
2135
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002136/**
2137 * drm_mode_addfb2 - add an FB to the graphics configuration
2138 * @inode: inode from the ioctl
2139 * @filp: file * from the ioctl
2140 * @cmd: cmd from ioctl
2141 * @arg: arg from ioctl
2142 *
2143 * LOCKING:
2144 * Takes mode config lock.
2145 *
2146 * Add a new FB to the specified CRTC, given a user request with format.
2147 *
2148 * Called by the user via ioctl.
2149 *
2150 * RETURNS:
2151 * Zero on success, errno on failure.
2152 */
2153int drm_mode_addfb2(struct drm_device *dev,
2154 void *data, struct drm_file *file_priv)
2155{
2156 struct drm_mode_fb_cmd2 *r = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002157 struct drm_mode_config *config = &dev->mode_config;
2158 struct drm_framebuffer *fb;
2159 int ret = 0;
2160
Dave Airliefb3b06c2011-02-08 13:55:21 +10002161 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2162 return -EINVAL;
2163
Dave Airlief453ba02008-11-07 14:05:41 -08002164 if ((config->min_width > r->width) || (r->width > config->max_width)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002165 DRM_ERROR("bad framebuffer width %d, should be >= %d && <= %d\n",
2166 r->width, config->min_width, config->max_width);
Dave Airlief453ba02008-11-07 14:05:41 -08002167 return -EINVAL;
2168 }
2169 if ((config->min_height > r->height) || (r->height > config->max_height)) {
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002170 DRM_ERROR("bad framebuffer height %d, should be >= %d && <= %d\n",
2171 r->height, config->min_height, config->max_height);
Dave Airlief453ba02008-11-07 14:05:41 -08002172 return -EINVAL;
2173 }
2174
Ville Syrjälä935b5972011-12-20 00:06:48 +02002175 ret = format_check(r);
2176 if (ret) {
2177 DRM_ERROR("bad framebuffer format 0x%08x\n", r->pixel_format);
2178 return ret;
2179 }
2180
Dave Airlief453ba02008-11-07 14:05:41 -08002181 mutex_lock(&dev->mode_config.mutex);
2182
Dave Airlief453ba02008-11-07 14:05:41 -08002183 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002184 if (IS_ERR(fb)) {
Dave Airlief453ba02008-11-07 14:05:41 -08002185 DRM_ERROR("could not create framebuffer\n");
Chris Wilsoncce13ff2010-08-08 13:36:38 +01002186 ret = PTR_ERR(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002187 goto out;
2188 }
2189
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002190 r->fb_id = fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002191 list_add(&fb->filp_head, &file_priv->fbs);
Jerome Glisse94401062010-07-15 15:43:25 -04002192 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
Dave Airlief453ba02008-11-07 14:05:41 -08002193
2194out:
2195 mutex_unlock(&dev->mode_config.mutex);
2196 return ret;
2197}
2198
2199/**
2200 * drm_mode_rmfb - remove an FB from the configuration
2201 * @inode: inode from the ioctl
2202 * @filp: file * from the ioctl
2203 * @cmd: cmd from ioctl
2204 * @arg: arg from ioctl
2205 *
2206 * LOCKING:
2207 * Takes mode config lock.
2208 *
2209 * Remove the FB specified by the user.
2210 *
2211 * Called by the user via ioctl.
2212 *
2213 * RETURNS:
2214 * Zero on success, errno on failure.
2215 */
2216int drm_mode_rmfb(struct drm_device *dev,
2217 void *data, struct drm_file *file_priv)
2218{
2219 struct drm_mode_object *obj;
2220 struct drm_framebuffer *fb = NULL;
2221 struct drm_framebuffer *fbl = NULL;
2222 uint32_t *id = data;
2223 int ret = 0;
2224 int found = 0;
2225
Dave Airliefb3b06c2011-02-08 13:55:21 +10002226 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2227 return -EINVAL;
2228
Dave Airlief453ba02008-11-07 14:05:41 -08002229 mutex_lock(&dev->mode_config.mutex);
2230 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002231 /* TODO check that we really get a framebuffer back. */
Dave Airlief453ba02008-11-07 14:05:41 -08002232 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002233 ret = -EINVAL;
2234 goto out;
2235 }
2236 fb = obj_to_fb(obj);
2237
2238 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2239 if (fb == fbl)
2240 found = 1;
2241
2242 if (!found) {
Dave Airlief453ba02008-11-07 14:05:41 -08002243 ret = -EINVAL;
2244 goto out;
2245 }
2246
2247 /* TODO release all crtc connected to the framebuffer */
2248 /* TODO unhock the destructor from the buffer object */
2249
2250 list_del(&fb->filp_head);
2251 fb->funcs->destroy(fb);
2252
2253out:
2254 mutex_unlock(&dev->mode_config.mutex);
2255 return ret;
2256}
2257
2258/**
2259 * drm_mode_getfb - get FB info
2260 * @inode: inode from the ioctl
2261 * @filp: file * from the ioctl
2262 * @cmd: cmd from ioctl
2263 * @arg: arg from ioctl
2264 *
2265 * LOCKING:
2266 * Caller? (FIXME)
2267 *
2268 * Lookup the FB given its ID and return info about it.
2269 *
2270 * Called by the user via ioctl.
2271 *
2272 * RETURNS:
2273 * Zero on success, errno on failure.
2274 */
2275int drm_mode_getfb(struct drm_device *dev,
2276 void *data, struct drm_file *file_priv)
2277{
2278 struct drm_mode_fb_cmd *r = data;
2279 struct drm_mode_object *obj;
2280 struct drm_framebuffer *fb;
2281 int ret = 0;
2282
Dave Airliefb3b06c2011-02-08 13:55:21 +10002283 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2284 return -EINVAL;
2285
Dave Airlief453ba02008-11-07 14:05:41 -08002286 mutex_lock(&dev->mode_config.mutex);
Jakob Bornecrantze0c84632008-12-19 14:50:50 +10002287 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
Dave Airlief453ba02008-11-07 14:05:41 -08002288 if (!obj) {
Dave Airlief453ba02008-11-07 14:05:41 -08002289 ret = -EINVAL;
2290 goto out;
2291 }
2292 fb = obj_to_fb(obj);
2293
2294 r->height = fb->height;
2295 r->width = fb->width;
2296 r->depth = fb->depth;
2297 r->bpp = fb->bits_per_pixel;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002298 r->pitch = fb->pitches[0];
Dave Airlief453ba02008-11-07 14:05:41 -08002299 fb->funcs->create_handle(fb, file_priv, &r->handle);
2300
2301out:
2302 mutex_unlock(&dev->mode_config.mutex);
2303 return ret;
2304}
2305
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002306int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2307 void *data, struct drm_file *file_priv)
2308{
2309 struct drm_clip_rect __user *clips_ptr;
2310 struct drm_clip_rect *clips = NULL;
2311 struct drm_mode_fb_dirty_cmd *r = data;
2312 struct drm_mode_object *obj;
2313 struct drm_framebuffer *fb;
2314 unsigned flags;
2315 int num_clips;
2316 int ret = 0;
2317
Dave Airliefb3b06c2011-02-08 13:55:21 +10002318 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2319 return -EINVAL;
2320
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002321 mutex_lock(&dev->mode_config.mutex);
2322 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2323 if (!obj) {
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002324 ret = -EINVAL;
2325 goto out_err1;
2326 }
2327 fb = obj_to_fb(obj);
2328
2329 num_clips = r->num_clips;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002330 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002331
2332 if (!num_clips != !clips_ptr) {
2333 ret = -EINVAL;
2334 goto out_err1;
2335 }
2336
2337 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2338
2339 /* If userspace annotates copy, clips must come in pairs */
2340 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2341 ret = -EINVAL;
2342 goto out_err1;
2343 }
2344
2345 if (num_clips && clips_ptr) {
Xi Wanga5cd3352011-11-23 01:12:01 -05002346 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2347 ret = -EINVAL;
2348 goto out_err1;
2349 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002350 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2351 if (!clips) {
2352 ret = -ENOMEM;
2353 goto out_err1;
2354 }
2355
2356 ret = copy_from_user(clips, clips_ptr,
2357 num_clips * sizeof(*clips));
Dan Carpentere902a352010-06-04 12:23:21 +02002358 if (ret) {
2359 ret = -EFAULT;
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002360 goto out_err2;
Dan Carpentere902a352010-06-04 12:23:21 +02002361 }
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002362 }
2363
2364 if (fb->funcs->dirty) {
Thomas Hellstrom02b00162010-10-05 12:43:02 +02002365 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2366 clips, num_clips);
Jakob Bornecrantz884840a2009-12-03 23:25:47 +00002367 } else {
2368 ret = -ENOSYS;
2369 goto out_err2;
2370 }
2371
2372out_err2:
2373 kfree(clips);
2374out_err1:
2375 mutex_unlock(&dev->mode_config.mutex);
2376 return ret;
2377}
2378
2379
Dave Airlief453ba02008-11-07 14:05:41 -08002380/**
2381 * drm_fb_release - remove and free the FBs on this file
2382 * @filp: file * from the ioctl
2383 *
2384 * LOCKING:
2385 * Takes mode config lock.
2386 *
2387 * Destroy all the FBs associated with @filp.
2388 *
2389 * Called by the user via ioctl.
2390 *
2391 * RETURNS:
2392 * Zero on success, errno on failure.
2393 */
Kristian Høgsbergea39f832009-02-12 14:37:56 -05002394void drm_fb_release(struct drm_file *priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002395{
Dave Airlief453ba02008-11-07 14:05:41 -08002396 struct drm_device *dev = priv->minor->dev;
2397 struct drm_framebuffer *fb, *tfb;
2398
2399 mutex_lock(&dev->mode_config.mutex);
2400 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2401 list_del(&fb->filp_head);
2402 fb->funcs->destroy(fb);
2403 }
2404 mutex_unlock(&dev->mode_config.mutex);
2405}
2406
2407/**
2408 * drm_mode_attachmode - add a mode to the user mode list
2409 * @dev: DRM device
2410 * @connector: connector to add the mode to
2411 * @mode: mode to add
2412 *
2413 * Add @mode to @connector's user mode list.
2414 */
2415static int drm_mode_attachmode(struct drm_device *dev,
2416 struct drm_connector *connector,
2417 struct drm_display_mode *mode)
2418{
2419 int ret = 0;
2420
2421 list_add_tail(&mode->head, &connector->user_modes);
2422 return ret;
2423}
2424
2425int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2426 struct drm_display_mode *mode)
2427{
2428 struct drm_connector *connector;
2429 int ret = 0;
2430 struct drm_display_mode *dup_mode;
2431 int need_dup = 0;
2432 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2433 if (!connector->encoder)
2434 break;
2435 if (connector->encoder->crtc == crtc) {
2436 if (need_dup)
2437 dup_mode = drm_mode_duplicate(dev, mode);
2438 else
2439 dup_mode = mode;
2440 ret = drm_mode_attachmode(dev, connector, dup_mode);
2441 if (ret)
2442 return ret;
2443 need_dup = 1;
2444 }
2445 }
2446 return 0;
2447}
2448EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2449
2450static int drm_mode_detachmode(struct drm_device *dev,
2451 struct drm_connector *connector,
2452 struct drm_display_mode *mode)
2453{
2454 int found = 0;
2455 int ret = 0;
2456 struct drm_display_mode *match_mode, *t;
2457
2458 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2459 if (drm_mode_equal(match_mode, mode)) {
2460 list_del(&match_mode->head);
2461 drm_mode_destroy(dev, match_mode);
2462 found = 1;
2463 break;
2464 }
2465 }
2466
2467 if (!found)
2468 ret = -EINVAL;
2469
2470 return ret;
2471}
2472
2473int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2474{
2475 struct drm_connector *connector;
2476
2477 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2478 drm_mode_detachmode(dev, connector, mode);
2479 }
2480 return 0;
2481}
2482EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2483
2484/**
2485 * drm_fb_attachmode - Attach a user mode to an connector
2486 * @inode: inode from the ioctl
2487 * @filp: file * from the ioctl
2488 * @cmd: cmd from ioctl
2489 * @arg: arg from ioctl
2490 *
2491 * This attaches a user specified mode to an connector.
2492 * Called by the user via ioctl.
2493 *
2494 * RETURNS:
2495 * Zero on success, errno on failure.
2496 */
2497int drm_mode_attachmode_ioctl(struct drm_device *dev,
2498 void *data, struct drm_file *file_priv)
2499{
2500 struct drm_mode_mode_cmd *mode_cmd = data;
2501 struct drm_connector *connector;
2502 struct drm_display_mode *mode;
2503 struct drm_mode_object *obj;
2504 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2505 int ret = 0;
2506
Dave Airliefb3b06c2011-02-08 13:55:21 +10002507 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2508 return -EINVAL;
2509
Dave Airlief453ba02008-11-07 14:05:41 -08002510 mutex_lock(&dev->mode_config.mutex);
2511
2512 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2513 if (!obj) {
2514 ret = -EINVAL;
2515 goto out;
2516 }
2517 connector = obj_to_connector(obj);
2518
2519 mode = drm_mode_create(dev);
2520 if (!mode) {
2521 ret = -ENOMEM;
2522 goto out;
2523 }
2524
2525 drm_crtc_convert_umode(mode, umode);
2526
2527 ret = drm_mode_attachmode(dev, connector, mode);
2528out:
2529 mutex_unlock(&dev->mode_config.mutex);
2530 return ret;
2531}
2532
2533
2534/**
2535 * drm_fb_detachmode - Detach a user specified mode from an connector
2536 * @inode: inode from the ioctl
2537 * @filp: file * from the ioctl
2538 * @cmd: cmd from ioctl
2539 * @arg: arg from ioctl
2540 *
2541 * Called by the user via ioctl.
2542 *
2543 * RETURNS:
2544 * Zero on success, errno on failure.
2545 */
2546int drm_mode_detachmode_ioctl(struct drm_device *dev,
2547 void *data, struct drm_file *file_priv)
2548{
2549 struct drm_mode_object *obj;
2550 struct drm_mode_mode_cmd *mode_cmd = data;
2551 struct drm_connector *connector;
2552 struct drm_display_mode mode;
2553 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2554 int ret = 0;
2555
Dave Airliefb3b06c2011-02-08 13:55:21 +10002556 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2557 return -EINVAL;
2558
Dave Airlief453ba02008-11-07 14:05:41 -08002559 mutex_lock(&dev->mode_config.mutex);
2560
2561 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2562 if (!obj) {
2563 ret = -EINVAL;
2564 goto out;
2565 }
2566 connector = obj_to_connector(obj);
2567
2568 drm_crtc_convert_umode(&mode, umode);
2569 ret = drm_mode_detachmode(dev, connector, &mode);
2570out:
2571 mutex_unlock(&dev->mode_config.mutex);
2572 return ret;
2573}
2574
2575struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2576 const char *name, int num_values)
2577{
2578 struct drm_property *property = NULL;
2579
2580 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2581 if (!property)
2582 return NULL;
2583
2584 if (num_values) {
2585 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2586 if (!property->values)
2587 goto fail;
2588 }
2589
2590 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2591 property->flags = flags;
2592 property->num_values = num_values;
2593 INIT_LIST_HEAD(&property->enum_blob_list);
2594
Vinson Lee471dd2e2011-11-10 11:55:40 -08002595 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08002596 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08002597 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2598 }
Dave Airlief453ba02008-11-07 14:05:41 -08002599
2600 list_add_tail(&property->head, &dev->mode_config.property_list);
2601 return property;
2602fail:
2603 kfree(property);
2604 return NULL;
2605}
2606EXPORT_SYMBOL(drm_property_create);
2607
2608int drm_property_add_enum(struct drm_property *property, int index,
2609 uint64_t value, const char *name)
2610{
2611 struct drm_property_enum *prop_enum;
2612
2613 if (!(property->flags & DRM_MODE_PROP_ENUM))
2614 return -EINVAL;
2615
2616 if (!list_empty(&property->enum_blob_list)) {
2617 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2618 if (prop_enum->value == value) {
2619 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2620 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2621 return 0;
2622 }
2623 }
2624 }
2625
2626 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2627 if (!prop_enum)
2628 return -ENOMEM;
2629
2630 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2631 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2632 prop_enum->value = value;
2633
2634 property->values[index] = value;
2635 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2636 return 0;
2637}
2638EXPORT_SYMBOL(drm_property_add_enum);
2639
2640void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2641{
2642 struct drm_property_enum *prop_enum, *pt;
2643
2644 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2645 list_del(&prop_enum->head);
2646 kfree(prop_enum);
2647 }
2648
2649 if (property->num_values)
2650 kfree(property->values);
2651 drm_mode_object_put(dev, &property->base);
2652 list_del(&property->head);
2653 kfree(property);
2654}
2655EXPORT_SYMBOL(drm_property_destroy);
2656
2657int drm_connector_attach_property(struct drm_connector *connector,
2658 struct drm_property *property, uint64_t init_val)
2659{
2660 int i;
2661
2662 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2663 if (connector->property_ids[i] == 0) {
2664 connector->property_ids[i] = property->base.id;
2665 connector->property_values[i] = init_val;
2666 break;
2667 }
2668 }
2669
2670 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2671 return -EINVAL;
2672 return 0;
2673}
2674EXPORT_SYMBOL(drm_connector_attach_property);
2675
2676int drm_connector_property_set_value(struct drm_connector *connector,
2677 struct drm_property *property, uint64_t value)
2678{
2679 int i;
2680
2681 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2682 if (connector->property_ids[i] == property->base.id) {
2683 connector->property_values[i] = value;
2684 break;
2685 }
2686 }
2687
2688 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2689 return -EINVAL;
2690 return 0;
2691}
2692EXPORT_SYMBOL(drm_connector_property_set_value);
2693
2694int drm_connector_property_get_value(struct drm_connector *connector,
2695 struct drm_property *property, uint64_t *val)
2696{
2697 int i;
2698
2699 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2700 if (connector->property_ids[i] == property->base.id) {
2701 *val = connector->property_values[i];
2702 break;
2703 }
2704 }
2705
2706 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2707 return -EINVAL;
2708 return 0;
2709}
2710EXPORT_SYMBOL(drm_connector_property_get_value);
2711
2712int drm_mode_getproperty_ioctl(struct drm_device *dev,
2713 void *data, struct drm_file *file_priv)
2714{
2715 struct drm_mode_object *obj;
2716 struct drm_mode_get_property *out_resp = data;
2717 struct drm_property *property;
2718 int enum_count = 0;
2719 int blob_count = 0;
2720 int value_count = 0;
2721 int ret = 0, i;
2722 int copied;
2723 struct drm_property_enum *prop_enum;
2724 struct drm_mode_property_enum __user *enum_ptr;
2725 struct drm_property_blob *prop_blob;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002726 uint32_t __user *blob_id_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002727 uint64_t __user *values_ptr;
2728 uint32_t __user *blob_length_ptr;
2729
Dave Airliefb3b06c2011-02-08 13:55:21 +10002730 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2731 return -EINVAL;
2732
Dave Airlief453ba02008-11-07 14:05:41 -08002733 mutex_lock(&dev->mode_config.mutex);
2734 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2735 if (!obj) {
2736 ret = -EINVAL;
2737 goto done;
2738 }
2739 property = obj_to_property(obj);
2740
2741 if (property->flags & DRM_MODE_PROP_ENUM) {
2742 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2743 enum_count++;
2744 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2745 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2746 blob_count++;
2747 }
2748
2749 value_count = property->num_values;
2750
2751 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2752 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2753 out_resp->flags = property->flags;
2754
2755 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002756 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002757 for (i = 0; i < value_count; i++) {
2758 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2759 ret = -EFAULT;
2760 goto done;
2761 }
2762 }
2763 }
2764 out_resp->count_values = value_count;
2765
2766 if (property->flags & DRM_MODE_PROP_ENUM) {
2767 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2768 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002769 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002770 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2771
2772 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2773 ret = -EFAULT;
2774 goto done;
2775 }
2776
2777 if (copy_to_user(&enum_ptr[copied].name,
2778 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2779 ret = -EFAULT;
2780 goto done;
2781 }
2782 copied++;
2783 }
2784 }
2785 out_resp->count_enum_blobs = enum_count;
2786 }
2787
2788 if (property->flags & DRM_MODE_PROP_BLOB) {
2789 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2790 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002791 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
2792 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002793
2794 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2795 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2796 ret = -EFAULT;
2797 goto done;
2798 }
2799
2800 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2801 ret = -EFAULT;
2802 goto done;
2803 }
2804
2805 copied++;
2806 }
2807 }
2808 out_resp->count_enum_blobs = blob_count;
2809 }
2810done:
2811 mutex_unlock(&dev->mode_config.mutex);
2812 return ret;
2813}
2814
2815static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2816 void *data)
2817{
2818 struct drm_property_blob *blob;
2819
2820 if (!length || !data)
2821 return NULL;
2822
2823 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2824 if (!blob)
2825 return NULL;
2826
2827 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2828 blob->length = length;
2829
2830 memcpy(blob->data, data, length);
2831
2832 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2833
2834 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2835 return blob;
2836}
2837
2838static void drm_property_destroy_blob(struct drm_device *dev,
2839 struct drm_property_blob *blob)
2840{
2841 drm_mode_object_put(dev, &blob->base);
2842 list_del(&blob->head);
2843 kfree(blob);
2844}
2845
2846int drm_mode_getblob_ioctl(struct drm_device *dev,
2847 void *data, struct drm_file *file_priv)
2848{
2849 struct drm_mode_object *obj;
2850 struct drm_mode_get_blob *out_resp = data;
2851 struct drm_property_blob *blob;
2852 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002853 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002854
Dave Airliefb3b06c2011-02-08 13:55:21 +10002855 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2856 return -EINVAL;
2857
Dave Airlief453ba02008-11-07 14:05:41 -08002858 mutex_lock(&dev->mode_config.mutex);
2859 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2860 if (!obj) {
2861 ret = -EINVAL;
2862 goto done;
2863 }
2864 blob = obj_to_blob(obj);
2865
2866 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002867 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Dave Airlief453ba02008-11-07 14:05:41 -08002868 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2869 ret = -EFAULT;
2870 goto done;
2871 }
2872 }
2873 out_resp->length = blob->length;
2874
2875done:
2876 mutex_unlock(&dev->mode_config.mutex);
2877 return ret;
2878}
2879
2880int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2881 struct edid *edid)
2882{
2883 struct drm_device *dev = connector->dev;
Adam Jackson7466f4c2010-03-29 21:43:23 +00002884 int ret = 0, size;
Dave Airlief453ba02008-11-07 14:05:41 -08002885
2886 if (connector->edid_blob_ptr)
2887 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2888
2889 /* Delete edid, when there is none. */
2890 if (!edid) {
2891 connector->edid_blob_ptr = NULL;
2892 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2893 return ret;
2894 }
2895
Adam Jackson7466f4c2010-03-29 21:43:23 +00002896 size = EDID_LENGTH * (1 + edid->extensions);
2897 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2898 size, edid);
Dave Airlief453ba02008-11-07 14:05:41 -08002899
2900 ret = drm_connector_property_set_value(connector,
2901 dev->mode_config.edid_property,
2902 connector->edid_blob_ptr->base.id);
2903
2904 return ret;
2905}
2906EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2907
2908int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2909 void *data, struct drm_file *file_priv)
2910{
2911 struct drm_mode_connector_set_property *out_resp = data;
2912 struct drm_mode_object *obj;
2913 struct drm_property *property;
2914 struct drm_connector *connector;
2915 int ret = -EINVAL;
2916 int i;
2917
Dave Airliefb3b06c2011-02-08 13:55:21 +10002918 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2919 return -EINVAL;
2920
Dave Airlief453ba02008-11-07 14:05:41 -08002921 mutex_lock(&dev->mode_config.mutex);
2922
2923 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2924 if (!obj) {
2925 goto out;
2926 }
2927 connector = obj_to_connector(obj);
2928
2929 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2930 if (connector->property_ids[i] == out_resp->prop_id)
2931 break;
2932 }
2933
2934 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2935 goto out;
2936 }
2937
2938 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2939 if (!obj) {
2940 goto out;
2941 }
2942 property = obj_to_property(obj);
2943
2944 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2945 goto out;
2946
2947 if (property->flags & DRM_MODE_PROP_RANGE) {
2948 if (out_resp->value < property->values[0])
2949 goto out;
2950
2951 if (out_resp->value > property->values[1])
2952 goto out;
2953 } else {
2954 int found = 0;
2955 for (i = 0; i < property->num_values; i++) {
2956 if (property->values[i] == out_resp->value) {
2957 found = 1;
2958 break;
2959 }
2960 }
2961 if (!found) {
2962 goto out;
2963 }
2964 }
2965
Keith Packardc9fb15f2009-05-30 20:42:28 -07002966 /* Do DPMS ourselves */
2967 if (property == connector->dev->mode_config.dpms_property) {
2968 if (connector->funcs->dpms)
2969 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2970 ret = 0;
2971 } else if (connector->funcs->set_property)
Dave Airlief453ba02008-11-07 14:05:41 -08002972 ret = connector->funcs->set_property(connector, property, out_resp->value);
2973
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002974 /* store the property value if successful */
Dave Airlief453ba02008-11-07 14:05:41 -08002975 if (!ret)
2976 drm_connector_property_set_value(connector, property, out_resp->value);
2977out:
2978 mutex_unlock(&dev->mode_config.mutex);
2979 return ret;
2980}
2981
Dave Airlief453ba02008-11-07 14:05:41 -08002982int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2983 struct drm_encoder *encoder)
2984{
2985 int i;
2986
2987 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2988 if (connector->encoder_ids[i] == 0) {
2989 connector->encoder_ids[i] = encoder->base.id;
2990 return 0;
2991 }
2992 }
2993 return -ENOMEM;
2994}
2995EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2996
2997void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2998 struct drm_encoder *encoder)
2999{
3000 int i;
3001 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3002 if (connector->encoder_ids[i] == encoder->base.id) {
3003 connector->encoder_ids[i] = 0;
3004 if (connector->encoder == encoder)
3005 connector->encoder = NULL;
3006 break;
3007 }
3008 }
3009}
3010EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3011
3012bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3013 int gamma_size)
3014{
3015 crtc->gamma_size = gamma_size;
3016
3017 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3018 if (!crtc->gamma_store) {
3019 crtc->gamma_size = 0;
3020 return false;
3021 }
3022
3023 return true;
3024}
3025EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3026
3027int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3028 void *data, struct drm_file *file_priv)
3029{
3030 struct drm_mode_crtc_lut *crtc_lut = data;
3031 struct drm_mode_object *obj;
3032 struct drm_crtc *crtc;
3033 void *r_base, *g_base, *b_base;
3034 int size;
3035 int ret = 0;
3036
Dave Airliefb3b06c2011-02-08 13:55:21 +10003037 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3038 return -EINVAL;
3039
Dave Airlief453ba02008-11-07 14:05:41 -08003040 mutex_lock(&dev->mode_config.mutex);
3041 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3042 if (!obj) {
3043 ret = -EINVAL;
3044 goto out;
3045 }
3046 crtc = obj_to_crtc(obj);
3047
3048 /* memcpy into gamma store */
3049 if (crtc_lut->gamma_size != crtc->gamma_size) {
3050 ret = -EINVAL;
3051 goto out;
3052 }
3053
3054 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3055 r_base = crtc->gamma_store;
3056 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3057 ret = -EFAULT;
3058 goto out;
3059 }
3060
3061 g_base = r_base + size;
3062 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3063 ret = -EFAULT;
3064 goto out;
3065 }
3066
3067 b_base = g_base + size;
3068 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3069 ret = -EFAULT;
3070 goto out;
3071 }
3072
James Simmons72034252010-08-03 01:33:19 +01003073 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08003074
3075out:
3076 mutex_unlock(&dev->mode_config.mutex);
3077 return ret;
3078
3079}
3080
3081int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3082 void *data, struct drm_file *file_priv)
3083{
3084 struct drm_mode_crtc_lut *crtc_lut = data;
3085 struct drm_mode_object *obj;
3086 struct drm_crtc *crtc;
3087 void *r_base, *g_base, *b_base;
3088 int size;
3089 int ret = 0;
3090
Dave Airliefb3b06c2011-02-08 13:55:21 +10003091 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3092 return -EINVAL;
3093
Dave Airlief453ba02008-11-07 14:05:41 -08003094 mutex_lock(&dev->mode_config.mutex);
3095 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3096 if (!obj) {
3097 ret = -EINVAL;
3098 goto out;
3099 }
3100 crtc = obj_to_crtc(obj);
3101
3102 /* memcpy into gamma store */
3103 if (crtc_lut->gamma_size != crtc->gamma_size) {
3104 ret = -EINVAL;
3105 goto out;
3106 }
3107
3108 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3109 r_base = crtc->gamma_store;
3110 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3111 ret = -EFAULT;
3112 goto out;
3113 }
3114
3115 g_base = r_base + size;
3116 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3117 ret = -EFAULT;
3118 goto out;
3119 }
3120
3121 b_base = g_base + size;
3122 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3123 ret = -EFAULT;
3124 goto out;
3125 }
3126out:
3127 mutex_unlock(&dev->mode_config.mutex);
3128 return ret;
3129}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003130
3131int drm_mode_page_flip_ioctl(struct drm_device *dev,
3132 void *data, struct drm_file *file_priv)
3133{
3134 struct drm_mode_crtc_page_flip *page_flip = data;
3135 struct drm_mode_object *obj;
3136 struct drm_crtc *crtc;
3137 struct drm_framebuffer *fb;
3138 struct drm_pending_vblank_event *e = NULL;
3139 unsigned long flags;
3140 int ret = -EINVAL;
3141
3142 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3143 page_flip->reserved != 0)
3144 return -EINVAL;
3145
3146 mutex_lock(&dev->mode_config.mutex);
3147 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3148 if (!obj)
3149 goto out;
3150 crtc = obj_to_crtc(obj);
3151
Chris Wilson90c1efd2010-07-17 20:23:26 +01003152 if (crtc->fb == NULL) {
3153 /* The framebuffer is currently unbound, presumably
3154 * due to a hotplug event, that userspace has not
3155 * yet discovered.
3156 */
3157 ret = -EBUSY;
3158 goto out;
3159 }
3160
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003161 if (crtc->funcs->page_flip == NULL)
3162 goto out;
3163
3164 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3165 if (!obj)
3166 goto out;
3167 fb = obj_to_fb(obj);
3168
3169 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3170 ret = -ENOMEM;
3171 spin_lock_irqsave(&dev->event_lock, flags);
3172 if (file_priv->event_space < sizeof e->event) {
3173 spin_unlock_irqrestore(&dev->event_lock, flags);
3174 goto out;
3175 }
3176 file_priv->event_space -= sizeof e->event;
3177 spin_unlock_irqrestore(&dev->event_lock, flags);
3178
3179 e = kzalloc(sizeof *e, GFP_KERNEL);
3180 if (e == NULL) {
3181 spin_lock_irqsave(&dev->event_lock, flags);
3182 file_priv->event_space += sizeof e->event;
3183 spin_unlock_irqrestore(&dev->event_lock, flags);
3184 goto out;
3185 }
3186
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08003187 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05003188 e->event.base.length = sizeof e->event;
3189 e->event.user_data = page_flip->user_data;
3190 e->base.event = &e->event.base;
3191 e->base.file_priv = file_priv;
3192 e->base.destroy =
3193 (void (*) (struct drm_pending_event *)) kfree;
3194 }
3195
3196 ret = crtc->funcs->page_flip(crtc, fb, e);
3197 if (ret) {
3198 spin_lock_irqsave(&dev->event_lock, flags);
3199 file_priv->event_space += sizeof e->event;
3200 spin_unlock_irqrestore(&dev->event_lock, flags);
3201 kfree(e);
3202 }
3203
3204out:
3205 mutex_unlock(&dev->mode_config.mutex);
3206 return ret;
3207}
Chris Wilsoneb033552011-01-24 15:11:08 +00003208
3209void drm_mode_config_reset(struct drm_device *dev)
3210{
3211 struct drm_crtc *crtc;
3212 struct drm_encoder *encoder;
3213 struct drm_connector *connector;
3214
3215 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3216 if (crtc->funcs->reset)
3217 crtc->funcs->reset(crtc);
3218
3219 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3220 if (encoder->funcs->reset)
3221 encoder->funcs->reset(encoder);
3222
3223 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3224 if (connector->funcs->reset)
3225 connector->funcs->reset(connector);
3226}
3227EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10003228
3229int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3230 void *data, struct drm_file *file_priv)
3231{
3232 struct drm_mode_create_dumb *args = data;
3233
3234 if (!dev->driver->dumb_create)
3235 return -ENOSYS;
3236 return dev->driver->dumb_create(file_priv, dev, args);
3237}
3238
3239int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3240 void *data, struct drm_file *file_priv)
3241{
3242 struct drm_mode_map_dumb *args = data;
3243
3244 /* call driver ioctl to get mmap offset */
3245 if (!dev->driver->dumb_map_offset)
3246 return -ENOSYS;
3247
3248 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3249}
3250
3251int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3252 void *data, struct drm_file *file_priv)
3253{
3254 struct drm_mode_destroy_dumb *args = data;
3255
3256 if (!dev->driver->dumb_destroy)
3257 return -ENOSYS;
3258
3259 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3260}
Dave Airlie248dbc22011-11-29 20:02:54 +00003261
3262/*
3263 * Just need to support RGB formats here for compat with code that doesn't
3264 * use pixel formats directly yet.
3265 */
3266void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3267 int *bpp)
3268{
3269 switch (format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02003270 case DRM_FORMAT_RGB332:
3271 case DRM_FORMAT_BGR233:
Dave Airlie248dbc22011-11-29 20:02:54 +00003272 *depth = 8;
3273 *bpp = 8;
3274 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003275 case DRM_FORMAT_XRGB1555:
3276 case DRM_FORMAT_XBGR1555:
3277 case DRM_FORMAT_RGBX5551:
3278 case DRM_FORMAT_BGRX5551:
3279 case DRM_FORMAT_ARGB1555:
3280 case DRM_FORMAT_ABGR1555:
3281 case DRM_FORMAT_RGBA5551:
3282 case DRM_FORMAT_BGRA5551:
Dave Airlie248dbc22011-11-29 20:02:54 +00003283 *depth = 15;
3284 *bpp = 16;
3285 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003286 case DRM_FORMAT_RGB565:
3287 case DRM_FORMAT_BGR565:
Dave Airlie248dbc22011-11-29 20:02:54 +00003288 *depth = 16;
3289 *bpp = 16;
3290 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003291 case DRM_FORMAT_RGB888:
3292 case DRM_FORMAT_BGR888:
3293 *depth = 24;
3294 *bpp = 24;
3295 break;
3296 case DRM_FORMAT_XRGB8888:
3297 case DRM_FORMAT_XBGR8888:
3298 case DRM_FORMAT_RGBX8888:
3299 case DRM_FORMAT_BGRX8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003300 *depth = 24;
3301 *bpp = 32;
3302 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003303 case DRM_FORMAT_XRGB2101010:
3304 case DRM_FORMAT_XBGR2101010:
3305 case DRM_FORMAT_RGBX1010102:
3306 case DRM_FORMAT_BGRX1010102:
3307 case DRM_FORMAT_ARGB2101010:
3308 case DRM_FORMAT_ABGR2101010:
3309 case DRM_FORMAT_RGBA1010102:
3310 case DRM_FORMAT_BGRA1010102:
Dave Airlie248dbc22011-11-29 20:02:54 +00003311 *depth = 30;
3312 *bpp = 32;
3313 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02003314 case DRM_FORMAT_ARGB8888:
3315 case DRM_FORMAT_ABGR8888:
3316 case DRM_FORMAT_RGBA8888:
3317 case DRM_FORMAT_BGRA8888:
Dave Airlie248dbc22011-11-29 20:02:54 +00003318 *depth = 32;
3319 *bpp = 32;
3320 break;
3321 default:
3322 DRM_DEBUG_KMS("unsupported pixel format\n");
3323 *depth = 0;
3324 *bpp = 0;
3325 break;
3326 }
3327}
3328EXPORT_SYMBOL(drm_fb_get_bpp_depth);