drm: Destroy the planes prior to destroying the associated CRTC
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc.c
1 /*
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>
33 #include <linux/slab.h>
34 #include <linux/export.h>
35 #include "drm.h"
36 #include "drmP.h"
37 #include "drm_crtc.h"
38 #include "drm_edid.h"
39 #include "drm_fourcc.h"
40
41 /* Avoid boilerplate. I'm tired of typing. */
42 #define DRM_ENUM_NAME_FN(fnname, list) \
43 char *fnname(int val) \
44 { \
45 int i; \
46 for (i = 0; i < ARRAY_SIZE(list); i++) { \
47 if (list[i].type == val) \
48 return list[i].name; \
49 } \
50 return "(unknown)"; \
51 }
52
53 /*
54 * Global properties
55 */
56 static struct drm_prop_enum_list drm_dpms_enum_list[] =
57 { { DRM_MODE_DPMS_ON, "On" },
58 { DRM_MODE_DPMS_STANDBY, "Standby" },
59 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
60 { DRM_MODE_DPMS_OFF, "Off" }
61 };
62
63 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
64
65 /*
66 * Optional properties
67 */
68 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
69 {
70 { DRM_MODE_SCALE_NONE, "None" },
71 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
72 { DRM_MODE_SCALE_CENTER, "Center" },
73 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
74 };
75
76 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
77 {
78 { DRM_MODE_DITHERING_OFF, "Off" },
79 { DRM_MODE_DITHERING_ON, "On" },
80 { DRM_MODE_DITHERING_AUTO, "Automatic" },
81 };
82
83 /*
84 * Non-global properties, but "required" for certain connectors.
85 */
86 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
87 {
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
91 };
92
93 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
94
95 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
96 {
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
100 };
101
102 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
104
105 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
106 {
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
111 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
112 };
113
114 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115
116 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117 {
118 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
119 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
121 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
122 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
123 };
124
125 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
126 drm_tv_subconnector_enum_list)
127
128 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
129 { DRM_MODE_DIRTY_OFF, "Off" },
130 { DRM_MODE_DIRTY_ON, "On" },
131 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
132 };
133
134 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
135 drm_dirty_info_enum_list)
136
137 struct drm_conn_prop_enum_list {
138 int type;
139 char *name;
140 int count;
141 };
142
143 /*
144 * Connector and encoder types.
145 */
146 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
147 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
148 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
149 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
150 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
151 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
152 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
153 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
154 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
155 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
156 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
157 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
158 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
159 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
160 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
161 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
162 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
163 };
164
165 static struct drm_prop_enum_list drm_encoder_enum_list[] =
166 { { DRM_MODE_ENCODER_NONE, "None" },
167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
172 };
173
174 char *drm_get_encoder_name(struct drm_encoder *encoder)
175 {
176 static char buf[32];
177
178 snprintf(buf, 32, "%s-%d",
179 drm_encoder_enum_list[encoder->encoder_type].name,
180 encoder->base.id);
181 return buf;
182 }
183 EXPORT_SYMBOL(drm_get_encoder_name);
184
185 char *drm_get_connector_name(struct drm_connector *connector)
186 {
187 static char buf[32];
188
189 snprintf(buf, 32, "%s-%d",
190 drm_connector_enum_list[connector->connector_type].name,
191 connector->connector_type_id);
192 return buf;
193 }
194 EXPORT_SYMBOL(drm_get_connector_name);
195
196 char *drm_get_connector_status_name(enum drm_connector_status status)
197 {
198 if (status == connector_status_connected)
199 return "connected";
200 else if (status == connector_status_disconnected)
201 return "disconnected";
202 else
203 return "unknown";
204 }
205
206 /**
207 * drm_mode_object_get - allocate a new identifier
208 * @dev: DRM device
209 * @ptr: object pointer, used to generate unique ID
210 * @type: object type
211 *
212 * LOCKING:
213 *
214 * Create a unique identifier based on @ptr in @dev's identifier space. Used
215 * for tracking modes, CRTCs and connectors.
216 *
217 * RETURNS:
218 * New unique (relative to other objects in @dev) integer identifier for the
219 * object.
220 */
221 static int drm_mode_object_get(struct drm_device *dev,
222 struct drm_mode_object *obj, uint32_t obj_type)
223 {
224 int new_id = 0;
225 int ret;
226
227 again:
228 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
229 DRM_ERROR("Ran out memory getting a mode number\n");
230 return -ENOMEM;
231 }
232
233 mutex_lock(&dev->mode_config.idr_mutex);
234 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
235 mutex_unlock(&dev->mode_config.idr_mutex);
236 if (ret == -EAGAIN)
237 goto again;
238 else if (ret)
239 return ret;
240
241 obj->id = new_id;
242 obj->type = obj_type;
243 return 0;
244 }
245
246 /**
247 * drm_mode_object_put - free an identifer
248 * @dev: DRM device
249 * @id: ID to free
250 *
251 * LOCKING:
252 * Caller must hold DRM mode_config lock.
253 *
254 * Free @id from @dev's unique identifier pool.
255 */
256 static void drm_mode_object_put(struct drm_device *dev,
257 struct drm_mode_object *object)
258 {
259 mutex_lock(&dev->mode_config.idr_mutex);
260 idr_remove(&dev->mode_config.crtc_idr, object->id);
261 mutex_unlock(&dev->mode_config.idr_mutex);
262 }
263
264 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
265 uint32_t id, uint32_t type)
266 {
267 struct drm_mode_object *obj = NULL;
268
269 mutex_lock(&dev->mode_config.idr_mutex);
270 obj = idr_find(&dev->mode_config.crtc_idr, id);
271 if (!obj || (obj->type != type) || (obj->id != id))
272 obj = NULL;
273 mutex_unlock(&dev->mode_config.idr_mutex);
274
275 return obj;
276 }
277 EXPORT_SYMBOL(drm_mode_object_find);
278
279 /**
280 * drm_framebuffer_init - initialize a framebuffer
281 * @dev: DRM device
282 *
283 * LOCKING:
284 * Caller must hold mode config lock.
285 *
286 * Allocates an ID for the framebuffer's parent mode object, sets its mode
287 * functions & device file and adds it to the master fd list.
288 *
289 * RETURNS:
290 * Zero on success, error code on failure.
291 */
292 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
293 const struct drm_framebuffer_funcs *funcs)
294 {
295 int ret;
296
297 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
298 if (ret)
299 return ret;
300
301 fb->dev = dev;
302 fb->funcs = funcs;
303 dev->mode_config.num_fb++;
304 list_add(&fb->head, &dev->mode_config.fb_list);
305
306 return 0;
307 }
308 EXPORT_SYMBOL(drm_framebuffer_init);
309
310 /**
311 * drm_framebuffer_cleanup - remove a framebuffer object
312 * @fb: framebuffer to remove
313 *
314 * LOCKING:
315 * Caller must hold mode config lock.
316 *
317 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
318 * it, setting it to NULL.
319 */
320 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
321 {
322 struct drm_device *dev = fb->dev;
323 struct drm_crtc *crtc;
324 struct drm_plane *plane;
325 struct drm_mode_set set;
326 int ret;
327
328 /* remove from any CRTC */
329 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
330 if (crtc->fb == fb) {
331 /* should turn off the crtc */
332 memset(&set, 0, sizeof(struct drm_mode_set));
333 set.crtc = crtc;
334 set.fb = NULL;
335 ret = crtc->funcs->set_config(&set);
336 if (ret)
337 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
338 }
339 }
340
341 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
342 if (plane->fb == fb) {
343 /* should turn off the crtc */
344 ret = plane->funcs->disable_plane(plane);
345 if (ret)
346 DRM_ERROR("failed to disable plane with busy fb\n");
347 /* disconnect the plane from the fb and crtc: */
348 plane->fb = NULL;
349 plane->crtc = NULL;
350 }
351 }
352
353 drm_mode_object_put(dev, &fb->base);
354 list_del(&fb->head);
355 dev->mode_config.num_fb--;
356 }
357 EXPORT_SYMBOL(drm_framebuffer_cleanup);
358
359 /**
360 * drm_crtc_init - Initialise a new CRTC object
361 * @dev: DRM device
362 * @crtc: CRTC object to init
363 * @funcs: callbacks for the new CRTC
364 *
365 * LOCKING:
366 * Takes mode_config lock.
367 *
368 * Inits a new object created as base part of an driver crtc object.
369 *
370 * RETURNS:
371 * Zero on success, error code on failure.
372 */
373 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
374 const struct drm_crtc_funcs *funcs)
375 {
376 int ret;
377
378 crtc->dev = dev;
379 crtc->funcs = funcs;
380
381 mutex_lock(&dev->mode_config.mutex);
382
383 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
384 if (ret)
385 goto out;
386
387 crtc->base.properties = &crtc->properties;
388
389 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
390 dev->mode_config.num_crtc++;
391
392 out:
393 mutex_unlock(&dev->mode_config.mutex);
394
395 return ret;
396 }
397 EXPORT_SYMBOL(drm_crtc_init);
398
399 /**
400 * drm_crtc_cleanup - Cleans up the core crtc usage.
401 * @crtc: CRTC to cleanup
402 *
403 * LOCKING:
404 * Caller must hold mode config lock.
405 *
406 * Cleanup @crtc. Removes from drm modesetting space
407 * does NOT free object, caller does that.
408 */
409 void drm_crtc_cleanup(struct drm_crtc *crtc)
410 {
411 struct drm_device *dev = crtc->dev;
412
413 if (crtc->gamma_store) {
414 kfree(crtc->gamma_store);
415 crtc->gamma_store = NULL;
416 }
417
418 drm_mode_object_put(dev, &crtc->base);
419 list_del(&crtc->head);
420 dev->mode_config.num_crtc--;
421 }
422 EXPORT_SYMBOL(drm_crtc_cleanup);
423
424 /**
425 * drm_mode_probed_add - add a mode to a connector's probed mode list
426 * @connector: connector the new mode
427 * @mode: mode data
428 *
429 * LOCKING:
430 * Caller must hold mode config lock.
431 *
432 * Add @mode to @connector's mode list for later use.
433 */
434 void drm_mode_probed_add(struct drm_connector *connector,
435 struct drm_display_mode *mode)
436 {
437 list_add(&mode->head, &connector->probed_modes);
438 }
439 EXPORT_SYMBOL(drm_mode_probed_add);
440
441 /**
442 * drm_mode_remove - remove and free a mode
443 * @connector: connector list to modify
444 * @mode: mode to remove
445 *
446 * LOCKING:
447 * Caller must hold mode config lock.
448 *
449 * Remove @mode from @connector's mode list, then free it.
450 */
451 void drm_mode_remove(struct drm_connector *connector,
452 struct drm_display_mode *mode)
453 {
454 list_del(&mode->head);
455 drm_mode_destroy(connector->dev, mode);
456 }
457 EXPORT_SYMBOL(drm_mode_remove);
458
459 /**
460 * drm_connector_init - Init a preallocated connector
461 * @dev: DRM device
462 * @connector: the connector to init
463 * @funcs: callbacks for this connector
464 * @name: user visible name of the connector
465 *
466 * LOCKING:
467 * Takes mode config lock.
468 *
469 * Initialises a preallocated connector. Connectors should be
470 * subclassed as part of driver connector objects.
471 *
472 * RETURNS:
473 * Zero on success, error code on failure.
474 */
475 int drm_connector_init(struct drm_device *dev,
476 struct drm_connector *connector,
477 const struct drm_connector_funcs *funcs,
478 int connector_type)
479 {
480 int ret;
481
482 mutex_lock(&dev->mode_config.mutex);
483
484 ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
485 if (ret)
486 goto out;
487
488 connector->base.properties = &connector->properties;
489 connector->dev = dev;
490 connector->funcs = funcs;
491 connector->connector_type = connector_type;
492 connector->connector_type_id =
493 ++drm_connector_enum_list[connector_type].count; /* TODO */
494 INIT_LIST_HEAD(&connector->user_modes);
495 INIT_LIST_HEAD(&connector->probed_modes);
496 INIT_LIST_HEAD(&connector->modes);
497 connector->edid_blob_ptr = NULL;
498
499 list_add_tail(&connector->head, &dev->mode_config.connector_list);
500 dev->mode_config.num_connector++;
501
502 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
503 drm_connector_attach_property(connector,
504 dev->mode_config.edid_property,
505 0);
506
507 drm_connector_attach_property(connector,
508 dev->mode_config.dpms_property, 0);
509
510 out:
511 mutex_unlock(&dev->mode_config.mutex);
512
513 return ret;
514 }
515 EXPORT_SYMBOL(drm_connector_init);
516
517 /**
518 * drm_connector_cleanup - cleans up an initialised connector
519 * @connector: connector to cleanup
520 *
521 * LOCKING:
522 * Takes mode config lock.
523 *
524 * Cleans up the connector but doesn't free the object.
525 */
526 void drm_connector_cleanup(struct drm_connector *connector)
527 {
528 struct drm_device *dev = connector->dev;
529 struct drm_display_mode *mode, *t;
530
531 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
532 drm_mode_remove(connector, mode);
533
534 list_for_each_entry_safe(mode, t, &connector->modes, head)
535 drm_mode_remove(connector, mode);
536
537 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
538 drm_mode_remove(connector, mode);
539
540 mutex_lock(&dev->mode_config.mutex);
541 drm_mode_object_put(dev, &connector->base);
542 list_del(&connector->head);
543 dev->mode_config.num_connector--;
544 mutex_unlock(&dev->mode_config.mutex);
545 }
546 EXPORT_SYMBOL(drm_connector_cleanup);
547
548 void drm_connector_unplug_all(struct drm_device *dev)
549 {
550 struct drm_connector *connector;
551
552 /* taking the mode config mutex ends up in a clash with sysfs */
553 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
554 drm_sysfs_connector_remove(connector);
555
556 }
557 EXPORT_SYMBOL(drm_connector_unplug_all);
558
559 int drm_encoder_init(struct drm_device *dev,
560 struct drm_encoder *encoder,
561 const struct drm_encoder_funcs *funcs,
562 int encoder_type)
563 {
564 int ret;
565
566 mutex_lock(&dev->mode_config.mutex);
567
568 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
569 if (ret)
570 goto out;
571
572 encoder->dev = dev;
573 encoder->encoder_type = encoder_type;
574 encoder->funcs = funcs;
575
576 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
577 dev->mode_config.num_encoder++;
578
579 out:
580 mutex_unlock(&dev->mode_config.mutex);
581
582 return ret;
583 }
584 EXPORT_SYMBOL(drm_encoder_init);
585
586 void drm_encoder_cleanup(struct drm_encoder *encoder)
587 {
588 struct drm_device *dev = encoder->dev;
589 mutex_lock(&dev->mode_config.mutex);
590 drm_mode_object_put(dev, &encoder->base);
591 list_del(&encoder->head);
592 dev->mode_config.num_encoder--;
593 mutex_unlock(&dev->mode_config.mutex);
594 }
595 EXPORT_SYMBOL(drm_encoder_cleanup);
596
597 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
598 unsigned long possible_crtcs,
599 const struct drm_plane_funcs *funcs,
600 const uint32_t *formats, uint32_t format_count,
601 bool priv)
602 {
603 int ret;
604
605 mutex_lock(&dev->mode_config.mutex);
606
607 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
608 if (ret)
609 goto out;
610
611 plane->base.properties = &plane->properties;
612 plane->dev = dev;
613 plane->funcs = funcs;
614 plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
615 GFP_KERNEL);
616 if (!plane->format_types) {
617 DRM_DEBUG_KMS("out of memory when allocating plane\n");
618 drm_mode_object_put(dev, &plane->base);
619 ret = -ENOMEM;
620 goto out;
621 }
622
623 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
624 plane->format_count = format_count;
625 plane->possible_crtcs = possible_crtcs;
626
627 /* private planes are not exposed to userspace, but depending on
628 * display hardware, might be convenient to allow sharing programming
629 * for the scanout engine with the crtc implementation.
630 */
631 if (!priv) {
632 list_add_tail(&plane->head, &dev->mode_config.plane_list);
633 dev->mode_config.num_plane++;
634 } else {
635 INIT_LIST_HEAD(&plane->head);
636 }
637
638 out:
639 mutex_unlock(&dev->mode_config.mutex);
640
641 return ret;
642 }
643 EXPORT_SYMBOL(drm_plane_init);
644
645 void drm_plane_cleanup(struct drm_plane *plane)
646 {
647 struct drm_device *dev = plane->dev;
648
649 mutex_lock(&dev->mode_config.mutex);
650 kfree(plane->format_types);
651 drm_mode_object_put(dev, &plane->base);
652 /* if not added to a list, it must be a private plane */
653 if (!list_empty(&plane->head)) {
654 list_del(&plane->head);
655 dev->mode_config.num_plane--;
656 }
657 mutex_unlock(&dev->mode_config.mutex);
658 }
659 EXPORT_SYMBOL(drm_plane_cleanup);
660
661 /**
662 * drm_mode_create - create a new display mode
663 * @dev: DRM device
664 *
665 * LOCKING:
666 * Caller must hold DRM mode_config lock.
667 *
668 * Create a new drm_display_mode, give it an ID, and return it.
669 *
670 * RETURNS:
671 * Pointer to new mode on success, NULL on error.
672 */
673 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
674 {
675 struct drm_display_mode *nmode;
676
677 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
678 if (!nmode)
679 return NULL;
680
681 if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
682 kfree(nmode);
683 return NULL;
684 }
685
686 return nmode;
687 }
688 EXPORT_SYMBOL(drm_mode_create);
689
690 /**
691 * drm_mode_destroy - remove a mode
692 * @dev: DRM device
693 * @mode: mode to remove
694 *
695 * LOCKING:
696 * Caller must hold mode config lock.
697 *
698 * Free @mode's unique identifier, then free it.
699 */
700 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
701 {
702 if (!mode)
703 return;
704
705 drm_mode_object_put(dev, &mode->base);
706
707 kfree(mode);
708 }
709 EXPORT_SYMBOL(drm_mode_destroy);
710
711 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
712 {
713 struct drm_property *edid;
714 struct drm_property *dpms;
715
716 /*
717 * Standard properties (apply to all connectors)
718 */
719 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
720 DRM_MODE_PROP_IMMUTABLE,
721 "EDID", 0);
722 dev->mode_config.edid_property = edid;
723
724 dpms = drm_property_create_enum(dev, 0,
725 "DPMS", drm_dpms_enum_list,
726 ARRAY_SIZE(drm_dpms_enum_list));
727 dev->mode_config.dpms_property = dpms;
728
729 return 0;
730 }
731
732 /**
733 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
734 * @dev: DRM device
735 *
736 * Called by a driver the first time a DVI-I connector is made.
737 */
738 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
739 {
740 struct drm_property *dvi_i_selector;
741 struct drm_property *dvi_i_subconnector;
742
743 if (dev->mode_config.dvi_i_select_subconnector_property)
744 return 0;
745
746 dvi_i_selector =
747 drm_property_create_enum(dev, 0,
748 "select subconnector",
749 drm_dvi_i_select_enum_list,
750 ARRAY_SIZE(drm_dvi_i_select_enum_list));
751 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
752
753 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
754 "subconnector",
755 drm_dvi_i_subconnector_enum_list,
756 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
757 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
758
759 return 0;
760 }
761 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
762
763 /**
764 * drm_create_tv_properties - create TV specific connector properties
765 * @dev: DRM device
766 * @num_modes: number of different TV formats (modes) supported
767 * @modes: array of pointers to strings containing name of each format
768 *
769 * Called by a driver's TV initialization routine, this function creates
770 * the TV specific connector properties for a given device. Caller is
771 * responsible for allocating a list of format names and passing them to
772 * this routine.
773 */
774 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
775 char *modes[])
776 {
777 struct drm_property *tv_selector;
778 struct drm_property *tv_subconnector;
779 int i;
780
781 if (dev->mode_config.tv_select_subconnector_property)
782 return 0;
783
784 /*
785 * Basic connector properties
786 */
787 tv_selector = drm_property_create_enum(dev, 0,
788 "select subconnector",
789 drm_tv_select_enum_list,
790 ARRAY_SIZE(drm_tv_select_enum_list));
791 dev->mode_config.tv_select_subconnector_property = tv_selector;
792
793 tv_subconnector =
794 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
795 "subconnector",
796 drm_tv_subconnector_enum_list,
797 ARRAY_SIZE(drm_tv_subconnector_enum_list));
798 dev->mode_config.tv_subconnector_property = tv_subconnector;
799
800 /*
801 * Other, TV specific properties: margins & TV modes.
802 */
803 dev->mode_config.tv_left_margin_property =
804 drm_property_create_range(dev, 0, "left margin", 0, 100);
805
806 dev->mode_config.tv_right_margin_property =
807 drm_property_create_range(dev, 0, "right margin", 0, 100);
808
809 dev->mode_config.tv_top_margin_property =
810 drm_property_create_range(dev, 0, "top margin", 0, 100);
811
812 dev->mode_config.tv_bottom_margin_property =
813 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
814
815 dev->mode_config.tv_mode_property =
816 drm_property_create(dev, DRM_MODE_PROP_ENUM,
817 "mode", num_modes);
818 for (i = 0; i < num_modes; i++)
819 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
820 i, modes[i]);
821
822 dev->mode_config.tv_brightness_property =
823 drm_property_create_range(dev, 0, "brightness", 0, 100);
824
825 dev->mode_config.tv_contrast_property =
826 drm_property_create_range(dev, 0, "contrast", 0, 100);
827
828 dev->mode_config.tv_flicker_reduction_property =
829 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
830
831 dev->mode_config.tv_overscan_property =
832 drm_property_create_range(dev, 0, "overscan", 0, 100);
833
834 dev->mode_config.tv_saturation_property =
835 drm_property_create_range(dev, 0, "saturation", 0, 100);
836
837 dev->mode_config.tv_hue_property =
838 drm_property_create_range(dev, 0, "hue", 0, 100);
839
840 return 0;
841 }
842 EXPORT_SYMBOL(drm_mode_create_tv_properties);
843
844 /**
845 * drm_mode_create_scaling_mode_property - create scaling mode property
846 * @dev: DRM device
847 *
848 * Called by a driver the first time it's needed, must be attached to desired
849 * connectors.
850 */
851 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
852 {
853 struct drm_property *scaling_mode;
854
855 if (dev->mode_config.scaling_mode_property)
856 return 0;
857
858 scaling_mode =
859 drm_property_create_enum(dev, 0, "scaling mode",
860 drm_scaling_mode_enum_list,
861 ARRAY_SIZE(drm_scaling_mode_enum_list));
862
863 dev->mode_config.scaling_mode_property = scaling_mode;
864
865 return 0;
866 }
867 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
868
869 /**
870 * drm_mode_create_dithering_property - create dithering property
871 * @dev: DRM device
872 *
873 * Called by a driver the first time it's needed, must be attached to desired
874 * connectors.
875 */
876 int drm_mode_create_dithering_property(struct drm_device *dev)
877 {
878 struct drm_property *dithering_mode;
879
880 if (dev->mode_config.dithering_mode_property)
881 return 0;
882
883 dithering_mode =
884 drm_property_create_enum(dev, 0, "dithering",
885 drm_dithering_mode_enum_list,
886 ARRAY_SIZE(drm_dithering_mode_enum_list));
887 dev->mode_config.dithering_mode_property = dithering_mode;
888
889 return 0;
890 }
891 EXPORT_SYMBOL(drm_mode_create_dithering_property);
892
893 /**
894 * drm_mode_create_dirty_property - create dirty property
895 * @dev: DRM device
896 *
897 * Called by a driver the first time it's needed, must be attached to desired
898 * connectors.
899 */
900 int drm_mode_create_dirty_info_property(struct drm_device *dev)
901 {
902 struct drm_property *dirty_info;
903
904 if (dev->mode_config.dirty_info_property)
905 return 0;
906
907 dirty_info =
908 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
909 "dirty",
910 drm_dirty_info_enum_list,
911 ARRAY_SIZE(drm_dirty_info_enum_list));
912 dev->mode_config.dirty_info_property = dirty_info;
913
914 return 0;
915 }
916 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
917
918 /**
919 * drm_mode_config_init - initialize DRM mode_configuration structure
920 * @dev: DRM device
921 *
922 * LOCKING:
923 * None, should happen single threaded at init time.
924 *
925 * Initialize @dev's mode_config structure, used for tracking the graphics
926 * configuration of @dev.
927 */
928 void drm_mode_config_init(struct drm_device *dev)
929 {
930 mutex_init(&dev->mode_config.mutex);
931 mutex_init(&dev->mode_config.idr_mutex);
932 INIT_LIST_HEAD(&dev->mode_config.fb_list);
933 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
934 INIT_LIST_HEAD(&dev->mode_config.connector_list);
935 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
936 INIT_LIST_HEAD(&dev->mode_config.property_list);
937 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
938 INIT_LIST_HEAD(&dev->mode_config.plane_list);
939 idr_init(&dev->mode_config.crtc_idr);
940
941 mutex_lock(&dev->mode_config.mutex);
942 drm_mode_create_standard_connector_properties(dev);
943 mutex_unlock(&dev->mode_config.mutex);
944
945 /* Just to be sure */
946 dev->mode_config.num_fb = 0;
947 dev->mode_config.num_connector = 0;
948 dev->mode_config.num_crtc = 0;
949 dev->mode_config.num_encoder = 0;
950 }
951 EXPORT_SYMBOL(drm_mode_config_init);
952
953 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
954 {
955 uint32_t total_objects = 0;
956
957 total_objects += dev->mode_config.num_crtc;
958 total_objects += dev->mode_config.num_connector;
959 total_objects += dev->mode_config.num_encoder;
960
961 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
962 if (!group->id_list)
963 return -ENOMEM;
964
965 group->num_crtcs = 0;
966 group->num_connectors = 0;
967 group->num_encoders = 0;
968 return 0;
969 }
970
971 int drm_mode_group_init_legacy_group(struct drm_device *dev,
972 struct drm_mode_group *group)
973 {
974 struct drm_crtc *crtc;
975 struct drm_encoder *encoder;
976 struct drm_connector *connector;
977 int ret;
978
979 if ((ret = drm_mode_group_init(dev, group)))
980 return ret;
981
982 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
983 group->id_list[group->num_crtcs++] = crtc->base.id;
984
985 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
986 group->id_list[group->num_crtcs + group->num_encoders++] =
987 encoder->base.id;
988
989 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
990 group->id_list[group->num_crtcs + group->num_encoders +
991 group->num_connectors++] = connector->base.id;
992
993 return 0;
994 }
995 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
996
997 /**
998 * drm_mode_config_cleanup - free up DRM mode_config info
999 * @dev: DRM device
1000 *
1001 * LOCKING:
1002 * Caller must hold mode config lock.
1003 *
1004 * Free up all the connectors and CRTCs associated with this DRM device, then
1005 * free up the framebuffers and associated buffer objects.
1006 *
1007 * FIXME: cleanup any dangling user buffer objects too
1008 */
1009 void drm_mode_config_cleanup(struct drm_device *dev)
1010 {
1011 struct drm_connector *connector, *ot;
1012 struct drm_crtc *crtc, *ct;
1013 struct drm_encoder *encoder, *enct;
1014 struct drm_framebuffer *fb, *fbt;
1015 struct drm_property *property, *pt;
1016 struct drm_plane *plane, *plt;
1017
1018 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1019 head) {
1020 encoder->funcs->destroy(encoder);
1021 }
1022
1023 list_for_each_entry_safe(connector, ot,
1024 &dev->mode_config.connector_list, head) {
1025 connector->funcs->destroy(connector);
1026 }
1027
1028 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1029 head) {
1030 drm_property_destroy(dev, property);
1031 }
1032
1033 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
1034 fb->funcs->destroy(fb);
1035 }
1036
1037 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1038 head) {
1039 plane->funcs->destroy(plane);
1040 }
1041
1042 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1043 crtc->funcs->destroy(crtc);
1044 }
1045
1046 idr_remove_all(&dev->mode_config.crtc_idr);
1047 idr_destroy(&dev->mode_config.crtc_idr);
1048 }
1049 EXPORT_SYMBOL(drm_mode_config_cleanup);
1050
1051 /**
1052 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
1053 * @out: drm_mode_modeinfo struct to return to the user
1054 * @in: drm_display_mode to use
1055 *
1056 * LOCKING:
1057 * None.
1058 *
1059 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
1060 * the user.
1061 */
1062 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
1063 const struct drm_display_mode *in)
1064 {
1065 WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
1066 in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
1067 in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
1068 in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
1069 in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
1070 "timing values too large for mode info\n");
1071
1072 out->clock = in->clock;
1073 out->hdisplay = in->hdisplay;
1074 out->hsync_start = in->hsync_start;
1075 out->hsync_end = in->hsync_end;
1076 out->htotal = in->htotal;
1077 out->hskew = in->hskew;
1078 out->vdisplay = in->vdisplay;
1079 out->vsync_start = in->vsync_start;
1080 out->vsync_end = in->vsync_end;
1081 out->vtotal = in->vtotal;
1082 out->vscan = in->vscan;
1083 out->vrefresh = in->vrefresh;
1084 out->flags = in->flags;
1085 out->type = in->type;
1086 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1087 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1088 }
1089
1090 /**
1091 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1092 * @out: drm_display_mode to return to the user
1093 * @in: drm_mode_modeinfo to use
1094 *
1095 * LOCKING:
1096 * None.
1097 *
1098 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1099 * the caller.
1100 *
1101 * RETURNS:
1102 * Zero on success, errno on failure.
1103 */
1104 static int drm_crtc_convert_umode(struct drm_display_mode *out,
1105 const struct drm_mode_modeinfo *in)
1106 {
1107 if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
1108 return -ERANGE;
1109
1110 out->clock = in->clock;
1111 out->hdisplay = in->hdisplay;
1112 out->hsync_start = in->hsync_start;
1113 out->hsync_end = in->hsync_end;
1114 out->htotal = in->htotal;
1115 out->hskew = in->hskew;
1116 out->vdisplay = in->vdisplay;
1117 out->vsync_start = in->vsync_start;
1118 out->vsync_end = in->vsync_end;
1119 out->vtotal = in->vtotal;
1120 out->vscan = in->vscan;
1121 out->vrefresh = in->vrefresh;
1122 out->flags = in->flags;
1123 out->type = in->type;
1124 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1125 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1126
1127 return 0;
1128 }
1129
1130 /**
1131 * drm_mode_getresources - get graphics configuration
1132 * @inode: inode from the ioctl
1133 * @filp: file * from the ioctl
1134 * @cmd: cmd from ioctl
1135 * @arg: arg from ioctl
1136 *
1137 * LOCKING:
1138 * Takes mode config lock.
1139 *
1140 * Construct a set of configuration description structures and return
1141 * them to the user, including CRTC, connector and framebuffer configuration.
1142 *
1143 * Called by the user via ioctl.
1144 *
1145 * RETURNS:
1146 * Zero on success, errno on failure.
1147 */
1148 int drm_mode_getresources(struct drm_device *dev, void *data,
1149 struct drm_file *file_priv)
1150 {
1151 struct drm_mode_card_res *card_res = data;
1152 struct list_head *lh;
1153 struct drm_framebuffer *fb;
1154 struct drm_connector *connector;
1155 struct drm_crtc *crtc;
1156 struct drm_encoder *encoder;
1157 int ret = 0;
1158 int connector_count = 0;
1159 int crtc_count = 0;
1160 int fb_count = 0;
1161 int encoder_count = 0;
1162 int copied = 0, i;
1163 uint32_t __user *fb_id;
1164 uint32_t __user *crtc_id;
1165 uint32_t __user *connector_id;
1166 uint32_t __user *encoder_id;
1167 struct drm_mode_group *mode_group;
1168
1169 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1170 return -EINVAL;
1171
1172 mutex_lock(&dev->mode_config.mutex);
1173
1174 /*
1175 * For the non-control nodes we need to limit the list of resources
1176 * by IDs in the group list for this node
1177 */
1178 list_for_each(lh, &file_priv->fbs)
1179 fb_count++;
1180
1181 mode_group = &file_priv->master->minor->mode_group;
1182 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1183
1184 list_for_each(lh, &dev->mode_config.crtc_list)
1185 crtc_count++;
1186
1187 list_for_each(lh, &dev->mode_config.connector_list)
1188 connector_count++;
1189
1190 list_for_each(lh, &dev->mode_config.encoder_list)
1191 encoder_count++;
1192 } else {
1193
1194 crtc_count = mode_group->num_crtcs;
1195 connector_count = mode_group->num_connectors;
1196 encoder_count = mode_group->num_encoders;
1197 }
1198
1199 card_res->max_height = dev->mode_config.max_height;
1200 card_res->min_height = dev->mode_config.min_height;
1201 card_res->max_width = dev->mode_config.max_width;
1202 card_res->min_width = dev->mode_config.min_width;
1203
1204 /* handle this in 4 parts */
1205 /* FBs */
1206 if (card_res->count_fbs >= fb_count) {
1207 copied = 0;
1208 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1209 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1210 if (put_user(fb->base.id, fb_id + copied)) {
1211 ret = -EFAULT;
1212 goto out;
1213 }
1214 copied++;
1215 }
1216 }
1217 card_res->count_fbs = fb_count;
1218
1219 /* CRTCs */
1220 if (card_res->count_crtcs >= crtc_count) {
1221 copied = 0;
1222 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1223 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1224 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1225 head) {
1226 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1227 if (put_user(crtc->base.id, crtc_id + copied)) {
1228 ret = -EFAULT;
1229 goto out;
1230 }
1231 copied++;
1232 }
1233 } else {
1234 for (i = 0; i < mode_group->num_crtcs; i++) {
1235 if (put_user(mode_group->id_list[i],
1236 crtc_id + copied)) {
1237 ret = -EFAULT;
1238 goto out;
1239 }
1240 copied++;
1241 }
1242 }
1243 }
1244 card_res->count_crtcs = crtc_count;
1245
1246 /* Encoders */
1247 if (card_res->count_encoders >= encoder_count) {
1248 copied = 0;
1249 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1250 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1251 list_for_each_entry(encoder,
1252 &dev->mode_config.encoder_list,
1253 head) {
1254 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1255 drm_get_encoder_name(encoder));
1256 if (put_user(encoder->base.id, encoder_id +
1257 copied)) {
1258 ret = -EFAULT;
1259 goto out;
1260 }
1261 copied++;
1262 }
1263 } else {
1264 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1265 if (put_user(mode_group->id_list[i],
1266 encoder_id + copied)) {
1267 ret = -EFAULT;
1268 goto out;
1269 }
1270 copied++;
1271 }
1272
1273 }
1274 }
1275 card_res->count_encoders = encoder_count;
1276
1277 /* Connectors */
1278 if (card_res->count_connectors >= connector_count) {
1279 copied = 0;
1280 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1281 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1282 list_for_each_entry(connector,
1283 &dev->mode_config.connector_list,
1284 head) {
1285 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1286 connector->base.id,
1287 drm_get_connector_name(connector));
1288 if (put_user(connector->base.id,
1289 connector_id + copied)) {
1290 ret = -EFAULT;
1291 goto out;
1292 }
1293 copied++;
1294 }
1295 } else {
1296 int start = mode_group->num_crtcs +
1297 mode_group->num_encoders;
1298 for (i = start; i < start + mode_group->num_connectors; i++) {
1299 if (put_user(mode_group->id_list[i],
1300 connector_id + copied)) {
1301 ret = -EFAULT;
1302 goto out;
1303 }
1304 copied++;
1305 }
1306 }
1307 }
1308 card_res->count_connectors = connector_count;
1309
1310 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1311 card_res->count_connectors, card_res->count_encoders);
1312
1313 out:
1314 mutex_unlock(&dev->mode_config.mutex);
1315 return ret;
1316 }
1317
1318 /**
1319 * drm_mode_getcrtc - get CRTC configuration
1320 * @inode: inode from the ioctl
1321 * @filp: file * from the ioctl
1322 * @cmd: cmd from ioctl
1323 * @arg: arg from ioctl
1324 *
1325 * LOCKING:
1326 * Takes mode config lock.
1327 *
1328 * Construct a CRTC configuration structure to return to the user.
1329 *
1330 * Called by the user via ioctl.
1331 *
1332 * RETURNS:
1333 * Zero on success, errno on failure.
1334 */
1335 int drm_mode_getcrtc(struct drm_device *dev,
1336 void *data, struct drm_file *file_priv)
1337 {
1338 struct drm_mode_crtc *crtc_resp = data;
1339 struct drm_crtc *crtc;
1340 struct drm_mode_object *obj;
1341 int ret = 0;
1342
1343 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1344 return -EINVAL;
1345
1346 mutex_lock(&dev->mode_config.mutex);
1347
1348 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1349 DRM_MODE_OBJECT_CRTC);
1350 if (!obj) {
1351 ret = -EINVAL;
1352 goto out;
1353 }
1354 crtc = obj_to_crtc(obj);
1355
1356 crtc_resp->x = crtc->x;
1357 crtc_resp->y = crtc->y;
1358 crtc_resp->gamma_size = crtc->gamma_size;
1359 if (crtc->fb)
1360 crtc_resp->fb_id = crtc->fb->base.id;
1361 else
1362 crtc_resp->fb_id = 0;
1363
1364 if (crtc->enabled) {
1365
1366 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1367 crtc_resp->mode_valid = 1;
1368
1369 } else {
1370 crtc_resp->mode_valid = 0;
1371 }
1372
1373 out:
1374 mutex_unlock(&dev->mode_config.mutex);
1375 return ret;
1376 }
1377
1378 /**
1379 * drm_mode_getconnector - get connector configuration
1380 * @inode: inode from the ioctl
1381 * @filp: file * from the ioctl
1382 * @cmd: cmd from ioctl
1383 * @arg: arg from ioctl
1384 *
1385 * LOCKING:
1386 * Takes mode config lock.
1387 *
1388 * Construct a connector configuration structure to return to the user.
1389 *
1390 * Called by the user via ioctl.
1391 *
1392 * RETURNS:
1393 * Zero on success, errno on failure.
1394 */
1395 int drm_mode_getconnector(struct drm_device *dev, void *data,
1396 struct drm_file *file_priv)
1397 {
1398 struct drm_mode_get_connector *out_resp = data;
1399 struct drm_mode_object *obj;
1400 struct drm_connector *connector;
1401 struct drm_display_mode *mode;
1402 int mode_count = 0;
1403 int props_count = 0;
1404 int encoders_count = 0;
1405 int ret = 0;
1406 int copied = 0;
1407 int i;
1408 struct drm_mode_modeinfo u_mode;
1409 struct drm_mode_modeinfo __user *mode_ptr;
1410 uint32_t __user *prop_ptr;
1411 uint64_t __user *prop_values;
1412 uint32_t __user *encoder_ptr;
1413
1414 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1415 return -EINVAL;
1416
1417 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1418
1419 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1420
1421 mutex_lock(&dev->mode_config.mutex);
1422
1423 obj = drm_mode_object_find(dev, out_resp->connector_id,
1424 DRM_MODE_OBJECT_CONNECTOR);
1425 if (!obj) {
1426 ret = -EINVAL;
1427 goto out;
1428 }
1429 connector = obj_to_connector(obj);
1430
1431 props_count = connector->properties.count;
1432
1433 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1434 if (connector->encoder_ids[i] != 0) {
1435 encoders_count++;
1436 }
1437 }
1438
1439 if (out_resp->count_modes == 0) {
1440 connector->funcs->fill_modes(connector,
1441 dev->mode_config.max_width,
1442 dev->mode_config.max_height);
1443 }
1444
1445 /* delayed so we get modes regardless of pre-fill_modes state */
1446 list_for_each_entry(mode, &connector->modes, head)
1447 mode_count++;
1448
1449 out_resp->connector_id = connector->base.id;
1450 out_resp->connector_type = connector->connector_type;
1451 out_resp->connector_type_id = connector->connector_type_id;
1452 out_resp->mm_width = connector->display_info.width_mm;
1453 out_resp->mm_height = connector->display_info.height_mm;
1454 out_resp->subpixel = connector->display_info.subpixel_order;
1455 out_resp->connection = connector->status;
1456 if (connector->encoder)
1457 out_resp->encoder_id = connector->encoder->base.id;
1458 else
1459 out_resp->encoder_id = 0;
1460
1461 /*
1462 * This ioctl is called twice, once to determine how much space is
1463 * needed, and the 2nd time to fill it.
1464 */
1465 if ((out_resp->count_modes >= mode_count) && mode_count) {
1466 copied = 0;
1467 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
1468 list_for_each_entry(mode, &connector->modes, head) {
1469 drm_crtc_convert_to_umode(&u_mode, mode);
1470 if (copy_to_user(mode_ptr + copied,
1471 &u_mode, sizeof(u_mode))) {
1472 ret = -EFAULT;
1473 goto out;
1474 }
1475 copied++;
1476 }
1477 }
1478 out_resp->count_modes = mode_count;
1479
1480 if ((out_resp->count_props >= props_count) && props_count) {
1481 copied = 0;
1482 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
1483 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
1484 for (i = 0; i < connector->properties.count; i++) {
1485 if (put_user(connector->properties.ids[i],
1486 prop_ptr + copied)) {
1487 ret = -EFAULT;
1488 goto out;
1489 }
1490
1491 if (put_user(connector->properties.values[i],
1492 prop_values + copied)) {
1493 ret = -EFAULT;
1494 goto out;
1495 }
1496 copied++;
1497 }
1498 }
1499 out_resp->count_props = props_count;
1500
1501 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1502 copied = 0;
1503 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
1504 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1505 if (connector->encoder_ids[i] != 0) {
1506 if (put_user(connector->encoder_ids[i],
1507 encoder_ptr + copied)) {
1508 ret = -EFAULT;
1509 goto out;
1510 }
1511 copied++;
1512 }
1513 }
1514 }
1515 out_resp->count_encoders = encoders_count;
1516
1517 out:
1518 mutex_unlock(&dev->mode_config.mutex);
1519 return ret;
1520 }
1521
1522 int drm_mode_getencoder(struct drm_device *dev, void *data,
1523 struct drm_file *file_priv)
1524 {
1525 struct drm_mode_get_encoder *enc_resp = data;
1526 struct drm_mode_object *obj;
1527 struct drm_encoder *encoder;
1528 int ret = 0;
1529
1530 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1531 return -EINVAL;
1532
1533 mutex_lock(&dev->mode_config.mutex);
1534 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1535 DRM_MODE_OBJECT_ENCODER);
1536 if (!obj) {
1537 ret = -EINVAL;
1538 goto out;
1539 }
1540 encoder = obj_to_encoder(obj);
1541
1542 if (encoder->crtc)
1543 enc_resp->crtc_id = encoder->crtc->base.id;
1544 else
1545 enc_resp->crtc_id = 0;
1546 enc_resp->encoder_type = encoder->encoder_type;
1547 enc_resp->encoder_id = encoder->base.id;
1548 enc_resp->possible_crtcs = encoder->possible_crtcs;
1549 enc_resp->possible_clones = encoder->possible_clones;
1550
1551 out:
1552 mutex_unlock(&dev->mode_config.mutex);
1553 return ret;
1554 }
1555
1556 /**
1557 * drm_mode_getplane_res - get plane info
1558 * @dev: DRM device
1559 * @data: ioctl data
1560 * @file_priv: DRM file info
1561 *
1562 * LOCKING:
1563 * Takes mode config lock.
1564 *
1565 * Return an plane count and set of IDs.
1566 */
1567 int drm_mode_getplane_res(struct drm_device *dev, void *data,
1568 struct drm_file *file_priv)
1569 {
1570 struct drm_mode_get_plane_res *plane_resp = data;
1571 struct drm_mode_config *config;
1572 struct drm_plane *plane;
1573 uint32_t __user *plane_ptr;
1574 int copied = 0, ret = 0;
1575
1576 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1577 return -EINVAL;
1578
1579 mutex_lock(&dev->mode_config.mutex);
1580 config = &dev->mode_config;
1581
1582 /*
1583 * This ioctl is called twice, once to determine how much space is
1584 * needed, and the 2nd time to fill it.
1585 */
1586 if (config->num_plane &&
1587 (plane_resp->count_planes >= config->num_plane)) {
1588 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
1589
1590 list_for_each_entry(plane, &config->plane_list, head) {
1591 if (put_user(plane->base.id, plane_ptr + copied)) {
1592 ret = -EFAULT;
1593 goto out;
1594 }
1595 copied++;
1596 }
1597 }
1598 plane_resp->count_planes = config->num_plane;
1599
1600 out:
1601 mutex_unlock(&dev->mode_config.mutex);
1602 return ret;
1603 }
1604
1605 /**
1606 * drm_mode_getplane - get plane info
1607 * @dev: DRM device
1608 * @data: ioctl data
1609 * @file_priv: DRM file info
1610 *
1611 * LOCKING:
1612 * Takes mode config lock.
1613 *
1614 * Return plane info, including formats supported, gamma size, any
1615 * current fb, etc.
1616 */
1617 int drm_mode_getplane(struct drm_device *dev, void *data,
1618 struct drm_file *file_priv)
1619 {
1620 struct drm_mode_get_plane *plane_resp = data;
1621 struct drm_mode_object *obj;
1622 struct drm_plane *plane;
1623 uint32_t __user *format_ptr;
1624 int ret = 0;
1625
1626 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1627 return -EINVAL;
1628
1629 mutex_lock(&dev->mode_config.mutex);
1630 obj = drm_mode_object_find(dev, plane_resp->plane_id,
1631 DRM_MODE_OBJECT_PLANE);
1632 if (!obj) {
1633 ret = -ENOENT;
1634 goto out;
1635 }
1636 plane = obj_to_plane(obj);
1637
1638 if (plane->crtc)
1639 plane_resp->crtc_id = plane->crtc->base.id;
1640 else
1641 plane_resp->crtc_id = 0;
1642
1643 if (plane->fb)
1644 plane_resp->fb_id = plane->fb->base.id;
1645 else
1646 plane_resp->fb_id = 0;
1647
1648 plane_resp->plane_id = plane->base.id;
1649 plane_resp->possible_crtcs = plane->possible_crtcs;
1650 plane_resp->gamma_size = plane->gamma_size;
1651
1652 /*
1653 * This ioctl is called twice, once to determine how much space is
1654 * needed, and the 2nd time to fill it.
1655 */
1656 if (plane->format_count &&
1657 (plane_resp->count_format_types >= plane->format_count)) {
1658 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
1659 if (copy_to_user(format_ptr,
1660 plane->format_types,
1661 sizeof(uint32_t) * plane->format_count)) {
1662 ret = -EFAULT;
1663 goto out;
1664 }
1665 }
1666 plane_resp->count_format_types = plane->format_count;
1667
1668 out:
1669 mutex_unlock(&dev->mode_config.mutex);
1670 return ret;
1671 }
1672
1673 /**
1674 * drm_mode_setplane - set up or tear down an plane
1675 * @dev: DRM device
1676 * @data: ioctl data*
1677 * @file_prive: DRM file info
1678 *
1679 * LOCKING:
1680 * Takes mode config lock.
1681 *
1682 * Set plane info, including placement, fb, scaling, and other factors.
1683 * Or pass a NULL fb to disable.
1684 */
1685 int drm_mode_setplane(struct drm_device *dev, void *data,
1686 struct drm_file *file_priv)
1687 {
1688 struct drm_mode_set_plane *plane_req = data;
1689 struct drm_mode_object *obj;
1690 struct drm_plane *plane;
1691 struct drm_crtc *crtc;
1692 struct drm_framebuffer *fb;
1693 int ret = 0;
1694 unsigned int fb_width, fb_height;
1695 int i;
1696
1697 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1698 return -EINVAL;
1699
1700 mutex_lock(&dev->mode_config.mutex);
1701
1702 /*
1703 * First, find the plane, crtc, and fb objects. If not available,
1704 * we don't bother to call the driver.
1705 */
1706 obj = drm_mode_object_find(dev, plane_req->plane_id,
1707 DRM_MODE_OBJECT_PLANE);
1708 if (!obj) {
1709 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1710 plane_req->plane_id);
1711 ret = -ENOENT;
1712 goto out;
1713 }
1714 plane = obj_to_plane(obj);
1715
1716 /* No fb means shut it down */
1717 if (!plane_req->fb_id) {
1718 plane->funcs->disable_plane(plane);
1719 plane->crtc = NULL;
1720 plane->fb = NULL;
1721 goto out;
1722 }
1723
1724 obj = drm_mode_object_find(dev, plane_req->crtc_id,
1725 DRM_MODE_OBJECT_CRTC);
1726 if (!obj) {
1727 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1728 plane_req->crtc_id);
1729 ret = -ENOENT;
1730 goto out;
1731 }
1732 crtc = obj_to_crtc(obj);
1733
1734 obj = drm_mode_object_find(dev, plane_req->fb_id,
1735 DRM_MODE_OBJECT_FB);
1736 if (!obj) {
1737 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1738 plane_req->fb_id);
1739 ret = -ENOENT;
1740 goto out;
1741 }
1742 fb = obj_to_fb(obj);
1743
1744 /* Check whether this plane supports the fb pixel format. */
1745 for (i = 0; i < plane->format_count; i++)
1746 if (fb->pixel_format == plane->format_types[i])
1747 break;
1748 if (i == plane->format_count) {
1749 DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
1750 ret = -EINVAL;
1751 goto out;
1752 }
1753
1754 fb_width = fb->width << 16;
1755 fb_height = fb->height << 16;
1756
1757 /* Make sure source coordinates are inside the fb. */
1758 if (plane_req->src_w > fb_width ||
1759 plane_req->src_x > fb_width - plane_req->src_w ||
1760 plane_req->src_h > fb_height ||
1761 plane_req->src_y > fb_height - plane_req->src_h) {
1762 DRM_DEBUG_KMS("Invalid source coordinates "
1763 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1764 plane_req->src_w >> 16,
1765 ((plane_req->src_w & 0xffff) * 15625) >> 10,
1766 plane_req->src_h >> 16,
1767 ((plane_req->src_h & 0xffff) * 15625) >> 10,
1768 plane_req->src_x >> 16,
1769 ((plane_req->src_x & 0xffff) * 15625) >> 10,
1770 plane_req->src_y >> 16,
1771 ((plane_req->src_y & 0xffff) * 15625) >> 10);
1772 ret = -ENOSPC;
1773 goto out;
1774 }
1775
1776 /* Give drivers some help against integer overflows */
1777 if (plane_req->crtc_w > INT_MAX ||
1778 plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
1779 plane_req->crtc_h > INT_MAX ||
1780 plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
1781 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1782 plane_req->crtc_w, plane_req->crtc_h,
1783 plane_req->crtc_x, plane_req->crtc_y);
1784 ret = -ERANGE;
1785 goto out;
1786 }
1787
1788 ret = plane->funcs->update_plane(plane, crtc, fb,
1789 plane_req->crtc_x, plane_req->crtc_y,
1790 plane_req->crtc_w, plane_req->crtc_h,
1791 plane_req->src_x, plane_req->src_y,
1792 plane_req->src_w, plane_req->src_h);
1793 if (!ret) {
1794 plane->crtc = crtc;
1795 plane->fb = fb;
1796 }
1797
1798 out:
1799 mutex_unlock(&dev->mode_config.mutex);
1800
1801 return ret;
1802 }
1803
1804 /**
1805 * drm_mode_setcrtc - set CRTC configuration
1806 * @inode: inode from the ioctl
1807 * @filp: file * from the ioctl
1808 * @cmd: cmd from ioctl
1809 * @arg: arg from ioctl
1810 *
1811 * LOCKING:
1812 * Takes mode config lock.
1813 *
1814 * Build a new CRTC configuration based on user request.
1815 *
1816 * Called by the user via ioctl.
1817 *
1818 * RETURNS:
1819 * Zero on success, errno on failure.
1820 */
1821 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1822 struct drm_file *file_priv)
1823 {
1824 struct drm_mode_config *config = &dev->mode_config;
1825 struct drm_mode_crtc *crtc_req = data;
1826 struct drm_mode_object *obj;
1827 struct drm_crtc *crtc;
1828 struct drm_connector **connector_set = NULL, *connector;
1829 struct drm_framebuffer *fb = NULL;
1830 struct drm_display_mode *mode = NULL;
1831 struct drm_mode_set set;
1832 uint32_t __user *set_connectors_ptr;
1833 int ret;
1834 int i;
1835
1836 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1837 return -EINVAL;
1838
1839 /* For some reason crtc x/y offsets are signed internally. */
1840 if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
1841 return -ERANGE;
1842
1843 mutex_lock(&dev->mode_config.mutex);
1844 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1845 DRM_MODE_OBJECT_CRTC);
1846 if (!obj) {
1847 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1848 ret = -EINVAL;
1849 goto out;
1850 }
1851 crtc = obj_to_crtc(obj);
1852 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1853
1854 if (crtc_req->mode_valid) {
1855 /* If we have a mode we need a framebuffer. */
1856 /* If we pass -1, set the mode with the currently bound fb */
1857 if (crtc_req->fb_id == -1) {
1858 if (!crtc->fb) {
1859 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1860 ret = -EINVAL;
1861 goto out;
1862 }
1863 fb = crtc->fb;
1864 } else {
1865 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1866 DRM_MODE_OBJECT_FB);
1867 if (!obj) {
1868 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1869 crtc_req->fb_id);
1870 ret = -EINVAL;
1871 goto out;
1872 }
1873 fb = obj_to_fb(obj);
1874 }
1875
1876 mode = drm_mode_create(dev);
1877 if (!mode) {
1878 ret = -ENOMEM;
1879 goto out;
1880 }
1881
1882 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
1883 if (ret) {
1884 DRM_DEBUG_KMS("Invalid mode\n");
1885 goto out;
1886 }
1887
1888 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1889
1890 if (mode->hdisplay > fb->width ||
1891 mode->vdisplay > fb->height ||
1892 crtc_req->x > fb->width - mode->hdisplay ||
1893 crtc_req->y > fb->height - mode->vdisplay) {
1894 DRM_DEBUG_KMS("Invalid CRTC viewport %ux%u+%u+%u for fb size %ux%u.\n",
1895 mode->hdisplay, mode->vdisplay,
1896 crtc_req->x, crtc_req->y,
1897 fb->width, fb->height);
1898 ret = -ENOSPC;
1899 goto out;
1900 }
1901 }
1902
1903 if (crtc_req->count_connectors == 0 && mode) {
1904 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1905 ret = -EINVAL;
1906 goto out;
1907 }
1908
1909 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1910 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1911 crtc_req->count_connectors);
1912 ret = -EINVAL;
1913 goto out;
1914 }
1915
1916 if (crtc_req->count_connectors > 0) {
1917 u32 out_id;
1918
1919 /* Avoid unbounded kernel memory allocation */
1920 if (crtc_req->count_connectors > config->num_connector) {
1921 ret = -EINVAL;
1922 goto out;
1923 }
1924
1925 connector_set = kmalloc(crtc_req->count_connectors *
1926 sizeof(struct drm_connector *),
1927 GFP_KERNEL);
1928 if (!connector_set) {
1929 ret = -ENOMEM;
1930 goto out;
1931 }
1932
1933 for (i = 0; i < crtc_req->count_connectors; i++) {
1934 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1935 if (get_user(out_id, &set_connectors_ptr[i])) {
1936 ret = -EFAULT;
1937 goto out;
1938 }
1939
1940 obj = drm_mode_object_find(dev, out_id,
1941 DRM_MODE_OBJECT_CONNECTOR);
1942 if (!obj) {
1943 DRM_DEBUG_KMS("Connector id %d unknown\n",
1944 out_id);
1945 ret = -EINVAL;
1946 goto out;
1947 }
1948 connector = obj_to_connector(obj);
1949 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1950 connector->base.id,
1951 drm_get_connector_name(connector));
1952
1953 connector_set[i] = connector;
1954 }
1955 }
1956
1957 set.crtc = crtc;
1958 set.x = crtc_req->x;
1959 set.y = crtc_req->y;
1960 set.mode = mode;
1961 set.connectors = connector_set;
1962 set.num_connectors = crtc_req->count_connectors;
1963 set.fb = fb;
1964 ret = crtc->funcs->set_config(&set);
1965
1966 out:
1967 kfree(connector_set);
1968 drm_mode_destroy(dev, mode);
1969 mutex_unlock(&dev->mode_config.mutex);
1970 return ret;
1971 }
1972
1973 int drm_mode_cursor_ioctl(struct drm_device *dev,
1974 void *data, struct drm_file *file_priv)
1975 {
1976 struct drm_mode_cursor *req = data;
1977 struct drm_mode_object *obj;
1978 struct drm_crtc *crtc;
1979 int ret = 0;
1980
1981 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1982 return -EINVAL;
1983
1984 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1985 return -EINVAL;
1986
1987 mutex_lock(&dev->mode_config.mutex);
1988 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1989 if (!obj) {
1990 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1991 ret = -EINVAL;
1992 goto out;
1993 }
1994 crtc = obj_to_crtc(obj);
1995
1996 if (req->flags & DRM_MODE_CURSOR_BO) {
1997 if (!crtc->funcs->cursor_set) {
1998 ret = -ENXIO;
1999 goto out;
2000 }
2001 /* Turns off the cursor if handle is 0 */
2002 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2003 req->width, req->height);
2004 }
2005
2006 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2007 if (crtc->funcs->cursor_move) {
2008 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2009 } else {
2010 ret = -EFAULT;
2011 goto out;
2012 }
2013 }
2014 out:
2015 mutex_unlock(&dev->mode_config.mutex);
2016 return ret;
2017 }
2018
2019 /* Original addfb only supported RGB formats, so figure out which one */
2020 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2021 {
2022 uint32_t fmt;
2023
2024 switch (bpp) {
2025 case 8:
2026 fmt = DRM_FORMAT_RGB332;
2027 break;
2028 case 16:
2029 if (depth == 15)
2030 fmt = DRM_FORMAT_XRGB1555;
2031 else
2032 fmt = DRM_FORMAT_RGB565;
2033 break;
2034 case 24:
2035 fmt = DRM_FORMAT_RGB888;
2036 break;
2037 case 32:
2038 if (depth == 24)
2039 fmt = DRM_FORMAT_XRGB8888;
2040 else if (depth == 30)
2041 fmt = DRM_FORMAT_XRGB2101010;
2042 else
2043 fmt = DRM_FORMAT_ARGB8888;
2044 break;
2045 default:
2046 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
2047 fmt = DRM_FORMAT_XRGB8888;
2048 break;
2049 }
2050
2051 return fmt;
2052 }
2053 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
2054
2055 /**
2056 * drm_mode_addfb - add an FB to the graphics configuration
2057 * @inode: inode from the ioctl
2058 * @filp: file * from the ioctl
2059 * @cmd: cmd from ioctl
2060 * @arg: arg from ioctl
2061 *
2062 * LOCKING:
2063 * Takes mode config lock.
2064 *
2065 * Add a new FB to the specified CRTC, given a user request.
2066 *
2067 * Called by the user via ioctl.
2068 *
2069 * RETURNS:
2070 * Zero on success, errno on failure.
2071 */
2072 int drm_mode_addfb(struct drm_device *dev,
2073 void *data, struct drm_file *file_priv)
2074 {
2075 struct drm_mode_fb_cmd *or = data;
2076 struct drm_mode_fb_cmd2 r = {};
2077 struct drm_mode_config *config = &dev->mode_config;
2078 struct drm_framebuffer *fb;
2079 int ret = 0;
2080
2081 /* Use new struct with format internally */
2082 r.fb_id = or->fb_id;
2083 r.width = or->width;
2084 r.height = or->height;
2085 r.pitches[0] = or->pitch;
2086 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
2087 r.handles[0] = or->handle;
2088
2089 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2090 return -EINVAL;
2091
2092 if ((config->min_width > r.width) || (r.width > config->max_width))
2093 return -EINVAL;
2094
2095 if ((config->min_height > r.height) || (r.height > config->max_height))
2096 return -EINVAL;
2097
2098 mutex_lock(&dev->mode_config.mutex);
2099
2100 /* TODO check buffer is sufficiently large */
2101 /* TODO setup destructor callback */
2102
2103 fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
2104 if (IS_ERR(fb)) {
2105 DRM_DEBUG_KMS("could not create framebuffer\n");
2106 ret = PTR_ERR(fb);
2107 goto out;
2108 }
2109
2110 or->fb_id = fb->base.id;
2111 list_add(&fb->filp_head, &file_priv->fbs);
2112 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2113
2114 out:
2115 mutex_unlock(&dev->mode_config.mutex);
2116 return ret;
2117 }
2118
2119 static int format_check(const struct drm_mode_fb_cmd2 *r)
2120 {
2121 uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
2122
2123 switch (format) {
2124 case DRM_FORMAT_C8:
2125 case DRM_FORMAT_RGB332:
2126 case DRM_FORMAT_BGR233:
2127 case DRM_FORMAT_XRGB4444:
2128 case DRM_FORMAT_XBGR4444:
2129 case DRM_FORMAT_RGBX4444:
2130 case DRM_FORMAT_BGRX4444:
2131 case DRM_FORMAT_ARGB4444:
2132 case DRM_FORMAT_ABGR4444:
2133 case DRM_FORMAT_RGBA4444:
2134 case DRM_FORMAT_BGRA4444:
2135 case DRM_FORMAT_XRGB1555:
2136 case DRM_FORMAT_XBGR1555:
2137 case DRM_FORMAT_RGBX5551:
2138 case DRM_FORMAT_BGRX5551:
2139 case DRM_FORMAT_ARGB1555:
2140 case DRM_FORMAT_ABGR1555:
2141 case DRM_FORMAT_RGBA5551:
2142 case DRM_FORMAT_BGRA5551:
2143 case DRM_FORMAT_RGB565:
2144 case DRM_FORMAT_BGR565:
2145 case DRM_FORMAT_RGB888:
2146 case DRM_FORMAT_BGR888:
2147 case DRM_FORMAT_XRGB8888:
2148 case DRM_FORMAT_XBGR8888:
2149 case DRM_FORMAT_RGBX8888:
2150 case DRM_FORMAT_BGRX8888:
2151 case DRM_FORMAT_ARGB8888:
2152 case DRM_FORMAT_ABGR8888:
2153 case DRM_FORMAT_RGBA8888:
2154 case DRM_FORMAT_BGRA8888:
2155 case DRM_FORMAT_XRGB2101010:
2156 case DRM_FORMAT_XBGR2101010:
2157 case DRM_FORMAT_RGBX1010102:
2158 case DRM_FORMAT_BGRX1010102:
2159 case DRM_FORMAT_ARGB2101010:
2160 case DRM_FORMAT_ABGR2101010:
2161 case DRM_FORMAT_RGBA1010102:
2162 case DRM_FORMAT_BGRA1010102:
2163 case DRM_FORMAT_YUYV:
2164 case DRM_FORMAT_YVYU:
2165 case DRM_FORMAT_UYVY:
2166 case DRM_FORMAT_VYUY:
2167 case DRM_FORMAT_AYUV:
2168 case DRM_FORMAT_NV12:
2169 case DRM_FORMAT_NV21:
2170 case DRM_FORMAT_NV16:
2171 case DRM_FORMAT_NV61:
2172 case DRM_FORMAT_NV24:
2173 case DRM_FORMAT_NV42:
2174 case DRM_FORMAT_YUV410:
2175 case DRM_FORMAT_YVU410:
2176 case DRM_FORMAT_YUV411:
2177 case DRM_FORMAT_YVU411:
2178 case DRM_FORMAT_YUV420:
2179 case DRM_FORMAT_YVU420:
2180 case DRM_FORMAT_YUV422:
2181 case DRM_FORMAT_YVU422:
2182 case DRM_FORMAT_YUV444:
2183 case DRM_FORMAT_YVU444:
2184 return 0;
2185 default:
2186 return -EINVAL;
2187 }
2188 }
2189
2190 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
2191 {
2192 int ret, hsub, vsub, num_planes, i;
2193
2194 ret = format_check(r);
2195 if (ret) {
2196 DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
2197 return ret;
2198 }
2199
2200 hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
2201 vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
2202 num_planes = drm_format_num_planes(r->pixel_format);
2203
2204 if (r->width == 0 || r->width % hsub) {
2205 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
2206 return -EINVAL;
2207 }
2208
2209 if (r->height == 0 || r->height % vsub) {
2210 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
2211 return -EINVAL;
2212 }
2213
2214 for (i = 0; i < num_planes; i++) {
2215 unsigned int width = r->width / (i != 0 ? hsub : 1);
2216
2217 if (!r->handles[i]) {
2218 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
2219 return -EINVAL;
2220 }
2221
2222 if (r->pitches[i] < drm_format_plane_cpp(r->pixel_format, i) * width) {
2223 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
2224 return -EINVAL;
2225 }
2226 }
2227
2228 return 0;
2229 }
2230
2231 /**
2232 * drm_mode_addfb2 - add an FB to the graphics configuration
2233 * @inode: inode from the ioctl
2234 * @filp: file * from the ioctl
2235 * @cmd: cmd from ioctl
2236 * @arg: arg from ioctl
2237 *
2238 * LOCKING:
2239 * Takes mode config lock.
2240 *
2241 * Add a new FB to the specified CRTC, given a user request with format.
2242 *
2243 * Called by the user via ioctl.
2244 *
2245 * RETURNS:
2246 * Zero on success, errno on failure.
2247 */
2248 int drm_mode_addfb2(struct drm_device *dev,
2249 void *data, struct drm_file *file_priv)
2250 {
2251 struct drm_mode_fb_cmd2 *r = data;
2252 struct drm_mode_config *config = &dev->mode_config;
2253 struct drm_framebuffer *fb;
2254 int ret;
2255
2256 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2257 return -EINVAL;
2258
2259 if ((config->min_width > r->width) || (r->width > config->max_width)) {
2260 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
2261 r->width, config->min_width, config->max_width);
2262 return -EINVAL;
2263 }
2264 if ((config->min_height > r->height) || (r->height > config->max_height)) {
2265 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
2266 r->height, config->min_height, config->max_height);
2267 return -EINVAL;
2268 }
2269
2270 ret = framebuffer_check(r);
2271 if (ret)
2272 return ret;
2273
2274 mutex_lock(&dev->mode_config.mutex);
2275
2276 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
2277 if (IS_ERR(fb)) {
2278 DRM_DEBUG_KMS("could not create framebuffer\n");
2279 ret = PTR_ERR(fb);
2280 goto out;
2281 }
2282
2283 r->fb_id = fb->base.id;
2284 list_add(&fb->filp_head, &file_priv->fbs);
2285 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
2286
2287 out:
2288 mutex_unlock(&dev->mode_config.mutex);
2289 return ret;
2290 }
2291
2292 /**
2293 * drm_mode_rmfb - remove an FB from the configuration
2294 * @inode: inode from the ioctl
2295 * @filp: file * from the ioctl
2296 * @cmd: cmd from ioctl
2297 * @arg: arg from ioctl
2298 *
2299 * LOCKING:
2300 * Takes mode config lock.
2301 *
2302 * Remove the FB specified by the user.
2303 *
2304 * Called by the user via ioctl.
2305 *
2306 * RETURNS:
2307 * Zero on success, errno on failure.
2308 */
2309 int drm_mode_rmfb(struct drm_device *dev,
2310 void *data, struct drm_file *file_priv)
2311 {
2312 struct drm_mode_object *obj;
2313 struct drm_framebuffer *fb = NULL;
2314 struct drm_framebuffer *fbl = NULL;
2315 uint32_t *id = data;
2316 int ret = 0;
2317 int found = 0;
2318
2319 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2320 return -EINVAL;
2321
2322 mutex_lock(&dev->mode_config.mutex);
2323 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
2324 /* TODO check that we really get a framebuffer back. */
2325 if (!obj) {
2326 ret = -EINVAL;
2327 goto out;
2328 }
2329 fb = obj_to_fb(obj);
2330
2331 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
2332 if (fb == fbl)
2333 found = 1;
2334
2335 if (!found) {
2336 ret = -EINVAL;
2337 goto out;
2338 }
2339
2340 /* TODO release all crtc connected to the framebuffer */
2341 /* TODO unhock the destructor from the buffer object */
2342
2343 list_del(&fb->filp_head);
2344 fb->funcs->destroy(fb);
2345
2346 out:
2347 mutex_unlock(&dev->mode_config.mutex);
2348 return ret;
2349 }
2350
2351 /**
2352 * drm_mode_getfb - get FB info
2353 * @inode: inode from the ioctl
2354 * @filp: file * from the ioctl
2355 * @cmd: cmd from ioctl
2356 * @arg: arg from ioctl
2357 *
2358 * LOCKING:
2359 * Takes mode config lock.
2360 *
2361 * Lookup the FB given its ID and return info about it.
2362 *
2363 * Called by the user via ioctl.
2364 *
2365 * RETURNS:
2366 * Zero on success, errno on failure.
2367 */
2368 int drm_mode_getfb(struct drm_device *dev,
2369 void *data, struct drm_file *file_priv)
2370 {
2371 struct drm_mode_fb_cmd *r = data;
2372 struct drm_mode_object *obj;
2373 struct drm_framebuffer *fb;
2374 int ret = 0;
2375
2376 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2377 return -EINVAL;
2378
2379 mutex_lock(&dev->mode_config.mutex);
2380 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2381 if (!obj) {
2382 ret = -EINVAL;
2383 goto out;
2384 }
2385 fb = obj_to_fb(obj);
2386
2387 r->height = fb->height;
2388 r->width = fb->width;
2389 r->depth = fb->depth;
2390 r->bpp = fb->bits_per_pixel;
2391 r->pitch = fb->pitches[0];
2392 fb->funcs->create_handle(fb, file_priv, &r->handle);
2393
2394 out:
2395 mutex_unlock(&dev->mode_config.mutex);
2396 return ret;
2397 }
2398
2399 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
2400 void *data, struct drm_file *file_priv)
2401 {
2402 struct drm_clip_rect __user *clips_ptr;
2403 struct drm_clip_rect *clips = NULL;
2404 struct drm_mode_fb_dirty_cmd *r = data;
2405 struct drm_mode_object *obj;
2406 struct drm_framebuffer *fb;
2407 unsigned flags;
2408 int num_clips;
2409 int ret;
2410
2411 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2412 return -EINVAL;
2413
2414 mutex_lock(&dev->mode_config.mutex);
2415 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
2416 if (!obj) {
2417 ret = -EINVAL;
2418 goto out_err1;
2419 }
2420 fb = obj_to_fb(obj);
2421
2422 num_clips = r->num_clips;
2423 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
2424
2425 if (!num_clips != !clips_ptr) {
2426 ret = -EINVAL;
2427 goto out_err1;
2428 }
2429
2430 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
2431
2432 /* If userspace annotates copy, clips must come in pairs */
2433 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
2434 ret = -EINVAL;
2435 goto out_err1;
2436 }
2437
2438 if (num_clips && clips_ptr) {
2439 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
2440 ret = -EINVAL;
2441 goto out_err1;
2442 }
2443 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
2444 if (!clips) {
2445 ret = -ENOMEM;
2446 goto out_err1;
2447 }
2448
2449 ret = copy_from_user(clips, clips_ptr,
2450 num_clips * sizeof(*clips));
2451 if (ret) {
2452 ret = -EFAULT;
2453 goto out_err2;
2454 }
2455 }
2456
2457 if (fb->funcs->dirty) {
2458 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
2459 clips, num_clips);
2460 } else {
2461 ret = -ENOSYS;
2462 goto out_err2;
2463 }
2464
2465 out_err2:
2466 kfree(clips);
2467 out_err1:
2468 mutex_unlock(&dev->mode_config.mutex);
2469 return ret;
2470 }
2471
2472
2473 /**
2474 * drm_fb_release - remove and free the FBs on this file
2475 * @filp: file * from the ioctl
2476 *
2477 * LOCKING:
2478 * Takes mode config lock.
2479 *
2480 * Destroy all the FBs associated with @filp.
2481 *
2482 * Called by the user via ioctl.
2483 *
2484 * RETURNS:
2485 * Zero on success, errno on failure.
2486 */
2487 void drm_fb_release(struct drm_file *priv)
2488 {
2489 struct drm_device *dev = priv->minor->dev;
2490 struct drm_framebuffer *fb, *tfb;
2491
2492 mutex_lock(&dev->mode_config.mutex);
2493 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
2494 list_del(&fb->filp_head);
2495 fb->funcs->destroy(fb);
2496 }
2497 mutex_unlock(&dev->mode_config.mutex);
2498 }
2499
2500 /**
2501 * drm_mode_attachmode - add a mode to the user mode list
2502 * @dev: DRM device
2503 * @connector: connector to add the mode to
2504 * @mode: mode to add
2505 *
2506 * Add @mode to @connector's user mode list.
2507 */
2508 static void drm_mode_attachmode(struct drm_device *dev,
2509 struct drm_connector *connector,
2510 struct drm_display_mode *mode)
2511 {
2512 list_add_tail(&mode->head, &connector->user_modes);
2513 }
2514
2515 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
2516 const struct drm_display_mode *mode)
2517 {
2518 struct drm_connector *connector;
2519 int ret = 0;
2520 struct drm_display_mode *dup_mode, *next;
2521 LIST_HEAD(list);
2522
2523 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2524 if (!connector->encoder)
2525 continue;
2526 if (connector->encoder->crtc == crtc) {
2527 dup_mode = drm_mode_duplicate(dev, mode);
2528 if (!dup_mode) {
2529 ret = -ENOMEM;
2530 goto out;
2531 }
2532 list_add_tail(&dup_mode->head, &list);
2533 }
2534 }
2535
2536 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2537 if (!connector->encoder)
2538 continue;
2539 if (connector->encoder->crtc == crtc)
2540 list_move_tail(list.next, &connector->user_modes);
2541 }
2542
2543 WARN_ON(!list_empty(&list));
2544
2545 out:
2546 list_for_each_entry_safe(dup_mode, next, &list, head)
2547 drm_mode_destroy(dev, dup_mode);
2548
2549 return ret;
2550 }
2551 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
2552
2553 static int drm_mode_detachmode(struct drm_device *dev,
2554 struct drm_connector *connector,
2555 struct drm_display_mode *mode)
2556 {
2557 int found = 0;
2558 int ret = 0;
2559 struct drm_display_mode *match_mode, *t;
2560
2561 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2562 if (drm_mode_equal(match_mode, mode)) {
2563 list_del(&match_mode->head);
2564 drm_mode_destroy(dev, match_mode);
2565 found = 1;
2566 break;
2567 }
2568 }
2569
2570 if (!found)
2571 ret = -EINVAL;
2572
2573 return ret;
2574 }
2575
2576 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2577 {
2578 struct drm_connector *connector;
2579
2580 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2581 drm_mode_detachmode(dev, connector, mode);
2582 }
2583 return 0;
2584 }
2585 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2586
2587 /**
2588 * drm_fb_attachmode - Attach a user mode to an connector
2589 * @inode: inode from the ioctl
2590 * @filp: file * from the ioctl
2591 * @cmd: cmd from ioctl
2592 * @arg: arg from ioctl
2593 *
2594 * This attaches a user specified mode to an connector.
2595 * Called by the user via ioctl.
2596 *
2597 * RETURNS:
2598 * Zero on success, errno on failure.
2599 */
2600 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2601 void *data, struct drm_file *file_priv)
2602 {
2603 struct drm_mode_mode_cmd *mode_cmd = data;
2604 struct drm_connector *connector;
2605 struct drm_display_mode *mode;
2606 struct drm_mode_object *obj;
2607 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2608 int ret;
2609
2610 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2611 return -EINVAL;
2612
2613 mutex_lock(&dev->mode_config.mutex);
2614
2615 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2616 if (!obj) {
2617 ret = -EINVAL;
2618 goto out;
2619 }
2620 connector = obj_to_connector(obj);
2621
2622 mode = drm_mode_create(dev);
2623 if (!mode) {
2624 ret = -ENOMEM;
2625 goto out;
2626 }
2627
2628 ret = drm_crtc_convert_umode(mode, umode);
2629 if (ret) {
2630 DRM_DEBUG_KMS("Invalid mode\n");
2631 drm_mode_destroy(dev, mode);
2632 goto out;
2633 }
2634
2635 drm_mode_attachmode(dev, connector, mode);
2636 out:
2637 mutex_unlock(&dev->mode_config.mutex);
2638 return ret;
2639 }
2640
2641
2642 /**
2643 * drm_fb_detachmode - Detach a user specified mode from an connector
2644 * @inode: inode from the ioctl
2645 * @filp: file * from the ioctl
2646 * @cmd: cmd from ioctl
2647 * @arg: arg from ioctl
2648 *
2649 * Called by the user via ioctl.
2650 *
2651 * RETURNS:
2652 * Zero on success, errno on failure.
2653 */
2654 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2655 void *data, struct drm_file *file_priv)
2656 {
2657 struct drm_mode_object *obj;
2658 struct drm_mode_mode_cmd *mode_cmd = data;
2659 struct drm_connector *connector;
2660 struct drm_display_mode mode;
2661 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2662 int ret;
2663
2664 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2665 return -EINVAL;
2666
2667 mutex_lock(&dev->mode_config.mutex);
2668
2669 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2670 if (!obj) {
2671 ret = -EINVAL;
2672 goto out;
2673 }
2674 connector = obj_to_connector(obj);
2675
2676 ret = drm_crtc_convert_umode(&mode, umode);
2677 if (ret) {
2678 DRM_DEBUG_KMS("Invalid mode\n");
2679 goto out;
2680 }
2681
2682 ret = drm_mode_detachmode(dev, connector, &mode);
2683 out:
2684 mutex_unlock(&dev->mode_config.mutex);
2685 return ret;
2686 }
2687
2688 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2689 const char *name, int num_values)
2690 {
2691 struct drm_property *property = NULL;
2692 int ret;
2693
2694 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2695 if (!property)
2696 return NULL;
2697
2698 if (num_values) {
2699 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2700 if (!property->values)
2701 goto fail;
2702 }
2703
2704 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2705 if (ret)
2706 goto fail;
2707
2708 property->flags = flags;
2709 property->num_values = num_values;
2710 INIT_LIST_HEAD(&property->enum_blob_list);
2711
2712 if (name) {
2713 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2714 property->name[DRM_PROP_NAME_LEN-1] = '\0';
2715 }
2716
2717 list_add_tail(&property->head, &dev->mode_config.property_list);
2718 return property;
2719 fail:
2720 kfree(property->values);
2721 kfree(property);
2722 return NULL;
2723 }
2724 EXPORT_SYMBOL(drm_property_create);
2725
2726 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
2727 const char *name,
2728 const struct drm_prop_enum_list *props,
2729 int num_values)
2730 {
2731 struct drm_property *property;
2732 int i, ret;
2733
2734 flags |= DRM_MODE_PROP_ENUM;
2735
2736 property = drm_property_create(dev, flags, name, num_values);
2737 if (!property)
2738 return NULL;
2739
2740 for (i = 0; i < num_values; i++) {
2741 ret = drm_property_add_enum(property, i,
2742 props[i].type,
2743 props[i].name);
2744 if (ret) {
2745 drm_property_destroy(dev, property);
2746 return NULL;
2747 }
2748 }
2749
2750 return property;
2751 }
2752 EXPORT_SYMBOL(drm_property_create_enum);
2753
2754 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
2755 int flags, const char *name,
2756 const struct drm_prop_enum_list *props,
2757 int num_values)
2758 {
2759 struct drm_property *property;
2760 int i, ret;
2761
2762 flags |= DRM_MODE_PROP_BITMASK;
2763
2764 property = drm_property_create(dev, flags, name, num_values);
2765 if (!property)
2766 return NULL;
2767
2768 for (i = 0; i < num_values; i++) {
2769 ret = drm_property_add_enum(property, i,
2770 props[i].type,
2771 props[i].name);
2772 if (ret) {
2773 drm_property_destroy(dev, property);
2774 return NULL;
2775 }
2776 }
2777
2778 return property;
2779 }
2780 EXPORT_SYMBOL(drm_property_create_bitmask);
2781
2782 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
2783 const char *name,
2784 uint64_t min, uint64_t max)
2785 {
2786 struct drm_property *property;
2787
2788 flags |= DRM_MODE_PROP_RANGE;
2789
2790 property = drm_property_create(dev, flags, name, 2);
2791 if (!property)
2792 return NULL;
2793
2794 property->values[0] = min;
2795 property->values[1] = max;
2796
2797 return property;
2798 }
2799 EXPORT_SYMBOL(drm_property_create_range);
2800
2801 int drm_property_add_enum(struct drm_property *property, int index,
2802 uint64_t value, const char *name)
2803 {
2804 struct drm_property_enum *prop_enum;
2805
2806 if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
2807 return -EINVAL;
2808
2809 /*
2810 * Bitmask enum properties have the additional constraint of values
2811 * from 0 to 63
2812 */
2813 if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
2814 return -EINVAL;
2815
2816 if (!list_empty(&property->enum_blob_list)) {
2817 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2818 if (prop_enum->value == value) {
2819 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2820 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2821 return 0;
2822 }
2823 }
2824 }
2825
2826 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2827 if (!prop_enum)
2828 return -ENOMEM;
2829
2830 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2831 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2832 prop_enum->value = value;
2833
2834 property->values[index] = value;
2835 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2836 return 0;
2837 }
2838 EXPORT_SYMBOL(drm_property_add_enum);
2839
2840 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2841 {
2842 struct drm_property_enum *prop_enum, *pt;
2843
2844 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2845 list_del(&prop_enum->head);
2846 kfree(prop_enum);
2847 }
2848
2849 if (property->num_values)
2850 kfree(property->values);
2851 drm_mode_object_put(dev, &property->base);
2852 list_del(&property->head);
2853 kfree(property);
2854 }
2855 EXPORT_SYMBOL(drm_property_destroy);
2856
2857 void drm_connector_attach_property(struct drm_connector *connector,
2858 struct drm_property *property, uint64_t init_val)
2859 {
2860 drm_object_attach_property(&connector->base, property, init_val);
2861 }
2862 EXPORT_SYMBOL(drm_connector_attach_property);
2863
2864 int drm_connector_property_set_value(struct drm_connector *connector,
2865 struct drm_property *property, uint64_t value)
2866 {
2867 return drm_object_property_set_value(&connector->base, property, value);
2868 }
2869 EXPORT_SYMBOL(drm_connector_property_set_value);
2870
2871 int drm_connector_property_get_value(struct drm_connector *connector,
2872 struct drm_property *property, uint64_t *val)
2873 {
2874 return drm_object_property_get_value(&connector->base, property, val);
2875 }
2876 EXPORT_SYMBOL(drm_connector_property_get_value);
2877
2878 void drm_object_attach_property(struct drm_mode_object *obj,
2879 struct drm_property *property,
2880 uint64_t init_val)
2881 {
2882 int count = obj->properties->count;
2883
2884 if (count == DRM_OBJECT_MAX_PROPERTY) {
2885 WARN(1, "Failed to attach object property (type: 0x%x). Please "
2886 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
2887 "you see this message on the same object type.\n",
2888 obj->type);
2889 return;
2890 }
2891
2892 obj->properties->ids[count] = property->base.id;
2893 obj->properties->values[count] = init_val;
2894 obj->properties->count++;
2895 }
2896 EXPORT_SYMBOL(drm_object_attach_property);
2897
2898 int drm_object_property_set_value(struct drm_mode_object *obj,
2899 struct drm_property *property, uint64_t val)
2900 {
2901 int i;
2902
2903 for (i = 0; i < obj->properties->count; i++) {
2904 if (obj->properties->ids[i] == property->base.id) {
2905 obj->properties->values[i] = val;
2906 return 0;
2907 }
2908 }
2909
2910 return -EINVAL;
2911 }
2912 EXPORT_SYMBOL(drm_object_property_set_value);
2913
2914 int drm_object_property_get_value(struct drm_mode_object *obj,
2915 struct drm_property *property, uint64_t *val)
2916 {
2917 int i;
2918
2919 for (i = 0; i < obj->properties->count; i++) {
2920 if (obj->properties->ids[i] == property->base.id) {
2921 *val = obj->properties->values[i];
2922 return 0;
2923 }
2924 }
2925
2926 return -EINVAL;
2927 }
2928 EXPORT_SYMBOL(drm_object_property_get_value);
2929
2930 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2931 void *data, struct drm_file *file_priv)
2932 {
2933 struct drm_mode_object *obj;
2934 struct drm_mode_get_property *out_resp = data;
2935 struct drm_property *property;
2936 int enum_count = 0;
2937 int blob_count = 0;
2938 int value_count = 0;
2939 int ret = 0, i;
2940 int copied;
2941 struct drm_property_enum *prop_enum;
2942 struct drm_mode_property_enum __user *enum_ptr;
2943 struct drm_property_blob *prop_blob;
2944 uint32_t __user *blob_id_ptr;
2945 uint64_t __user *values_ptr;
2946 uint32_t __user *blob_length_ptr;
2947
2948 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2949 return -EINVAL;
2950
2951 mutex_lock(&dev->mode_config.mutex);
2952 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2953 if (!obj) {
2954 ret = -EINVAL;
2955 goto done;
2956 }
2957 property = obj_to_property(obj);
2958
2959 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2960 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2961 enum_count++;
2962 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2963 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2964 blob_count++;
2965 }
2966
2967 value_count = property->num_values;
2968
2969 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2970 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2971 out_resp->flags = property->flags;
2972
2973 if ((out_resp->count_values >= value_count) && value_count) {
2974 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
2975 for (i = 0; i < value_count; i++) {
2976 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2977 ret = -EFAULT;
2978 goto done;
2979 }
2980 }
2981 }
2982 out_resp->count_values = value_count;
2983
2984 if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
2985 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2986 copied = 0;
2987 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
2988 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2989
2990 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2991 ret = -EFAULT;
2992 goto done;
2993 }
2994
2995 if (copy_to_user(&enum_ptr[copied].name,
2996 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2997 ret = -EFAULT;
2998 goto done;
2999 }
3000 copied++;
3001 }
3002 }
3003 out_resp->count_enum_blobs = enum_count;
3004 }
3005
3006 if (property->flags & DRM_MODE_PROP_BLOB) {
3007 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
3008 copied = 0;
3009 blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
3010 blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
3011
3012 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
3013 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
3014 ret = -EFAULT;
3015 goto done;
3016 }
3017
3018 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
3019 ret = -EFAULT;
3020 goto done;
3021 }
3022
3023 copied++;
3024 }
3025 }
3026 out_resp->count_enum_blobs = blob_count;
3027 }
3028 done:
3029 mutex_unlock(&dev->mode_config.mutex);
3030 return ret;
3031 }
3032
3033 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
3034 void *data)
3035 {
3036 struct drm_property_blob *blob;
3037 int ret;
3038
3039 if (!length || !data)
3040 return NULL;
3041
3042 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3043 if (!blob)
3044 return NULL;
3045
3046 ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
3047 if (ret) {
3048 kfree(blob);
3049 return NULL;
3050 }
3051
3052 blob->length = length;
3053
3054 memcpy(blob->data, data, length);
3055
3056 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
3057 return blob;
3058 }
3059
3060 static void drm_property_destroy_blob(struct drm_device *dev,
3061 struct drm_property_blob *blob)
3062 {
3063 drm_mode_object_put(dev, &blob->base);
3064 list_del(&blob->head);
3065 kfree(blob);
3066 }
3067
3068 int drm_mode_getblob_ioctl(struct drm_device *dev,
3069 void *data, struct drm_file *file_priv)
3070 {
3071 struct drm_mode_object *obj;
3072 struct drm_mode_get_blob *out_resp = data;
3073 struct drm_property_blob *blob;
3074 int ret = 0;
3075 void __user *blob_ptr;
3076
3077 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3078 return -EINVAL;
3079
3080 mutex_lock(&dev->mode_config.mutex);
3081 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
3082 if (!obj) {
3083 ret = -EINVAL;
3084 goto done;
3085 }
3086 blob = obj_to_blob(obj);
3087
3088 if (out_resp->length == blob->length) {
3089 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3090 if (copy_to_user(blob_ptr, blob->data, blob->length)){
3091 ret = -EFAULT;
3092 goto done;
3093 }
3094 }
3095 out_resp->length = blob->length;
3096
3097 done:
3098 mutex_unlock(&dev->mode_config.mutex);
3099 return ret;
3100 }
3101
3102 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
3103 struct edid *edid)
3104 {
3105 struct drm_device *dev = connector->dev;
3106 int ret, size;
3107
3108 if (connector->edid_blob_ptr)
3109 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
3110
3111 /* Delete edid, when there is none. */
3112 if (!edid) {
3113 connector->edid_blob_ptr = NULL;
3114 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
3115 return ret;
3116 }
3117
3118 size = EDID_LENGTH * (1 + edid->extensions);
3119 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
3120 size, edid);
3121
3122 ret = drm_connector_property_set_value(connector,
3123 dev->mode_config.edid_property,
3124 connector->edid_blob_ptr->base.id);
3125
3126 return ret;
3127 }
3128 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
3129
3130 static bool drm_property_change_is_valid(struct drm_property *property,
3131 uint64_t value)
3132 {
3133 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
3134 return false;
3135 if (property->flags & DRM_MODE_PROP_RANGE) {
3136 if (value < property->values[0] || value > property->values[1])
3137 return false;
3138 return true;
3139 } else if (property->flags & DRM_MODE_PROP_BITMASK) {
3140 int i;
3141 uint64_t valid_mask = 0;
3142 for (i = 0; i < property->num_values; i++)
3143 valid_mask |= (1ULL << property->values[i]);
3144 return !(value & ~valid_mask);
3145 } else {
3146 int i;
3147 for (i = 0; i < property->num_values; i++)
3148 if (property->values[i] == value)
3149 return true;
3150 return false;
3151 }
3152 }
3153
3154 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
3155 void *data, struct drm_file *file_priv)
3156 {
3157 struct drm_mode_connector_set_property *conn_set_prop = data;
3158 struct drm_mode_obj_set_property obj_set_prop = {
3159 .value = conn_set_prop->value,
3160 .prop_id = conn_set_prop->prop_id,
3161 .obj_id = conn_set_prop->connector_id,
3162 .obj_type = DRM_MODE_OBJECT_CONNECTOR
3163 };
3164
3165 /* It does all the locking and checking we need */
3166 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
3167 }
3168
3169 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
3170 struct drm_property *property,
3171 uint64_t value)
3172 {
3173 int ret = -EINVAL;
3174 struct drm_connector *connector = obj_to_connector(obj);
3175
3176 /* Do DPMS ourselves */
3177 if (property == connector->dev->mode_config.dpms_property) {
3178 if (connector->funcs->dpms)
3179 (*connector->funcs->dpms)(connector, (int)value);
3180 ret = 0;
3181 } else if (connector->funcs->set_property)
3182 ret = connector->funcs->set_property(connector, property, value);
3183
3184 /* store the property value if successful */
3185 if (!ret)
3186 drm_connector_property_set_value(connector, property, value);
3187 return ret;
3188 }
3189
3190 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
3191 struct drm_property *property,
3192 uint64_t value)
3193 {
3194 int ret = -EINVAL;
3195 struct drm_crtc *crtc = obj_to_crtc(obj);
3196
3197 if (crtc->funcs->set_property)
3198 ret = crtc->funcs->set_property(crtc, property, value);
3199 if (!ret)
3200 drm_object_property_set_value(obj, property, value);
3201
3202 return ret;
3203 }
3204
3205 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
3206 struct drm_property *property,
3207 uint64_t value)
3208 {
3209 int ret = -EINVAL;
3210 struct drm_plane *plane = obj_to_plane(obj);
3211
3212 if (plane->funcs->set_property)
3213 ret = plane->funcs->set_property(plane, property, value);
3214 if (!ret)
3215 drm_object_property_set_value(obj, property, value);
3216
3217 return ret;
3218 }
3219
3220 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
3221 struct drm_file *file_priv)
3222 {
3223 struct drm_mode_obj_get_properties *arg = data;
3224 struct drm_mode_object *obj;
3225 int ret = 0;
3226 int i;
3227 int copied = 0;
3228 int props_count = 0;
3229 uint32_t __user *props_ptr;
3230 uint64_t __user *prop_values_ptr;
3231
3232 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3233 return -EINVAL;
3234
3235 mutex_lock(&dev->mode_config.mutex);
3236
3237 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3238 if (!obj) {
3239 ret = -EINVAL;
3240 goto out;
3241 }
3242 if (!obj->properties) {
3243 ret = -EINVAL;
3244 goto out;
3245 }
3246
3247 props_count = obj->properties->count;
3248
3249 /* This ioctl is called twice, once to determine how much space is
3250 * needed, and the 2nd time to fill it. */
3251 if ((arg->count_props >= props_count) && props_count) {
3252 copied = 0;
3253 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
3254 prop_values_ptr = (uint64_t __user *)(unsigned long)
3255 (arg->prop_values_ptr);
3256 for (i = 0; i < props_count; i++) {
3257 if (put_user(obj->properties->ids[i],
3258 props_ptr + copied)) {
3259 ret = -EFAULT;
3260 goto out;
3261 }
3262 if (put_user(obj->properties->values[i],
3263 prop_values_ptr + copied)) {
3264 ret = -EFAULT;
3265 goto out;
3266 }
3267 copied++;
3268 }
3269 }
3270 arg->count_props = props_count;
3271 out:
3272 mutex_unlock(&dev->mode_config.mutex);
3273 return ret;
3274 }
3275
3276 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
3277 struct drm_file *file_priv)
3278 {
3279 struct drm_mode_obj_set_property *arg = data;
3280 struct drm_mode_object *arg_obj;
3281 struct drm_mode_object *prop_obj;
3282 struct drm_property *property;
3283 int ret = -EINVAL;
3284 int i;
3285
3286 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3287 return -EINVAL;
3288
3289 mutex_lock(&dev->mode_config.mutex);
3290
3291 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
3292 if (!arg_obj)
3293 goto out;
3294 if (!arg_obj->properties)
3295 goto out;
3296
3297 for (i = 0; i < arg_obj->properties->count; i++)
3298 if (arg_obj->properties->ids[i] == arg->prop_id)
3299 break;
3300
3301 if (i == arg_obj->properties->count)
3302 goto out;
3303
3304 prop_obj = drm_mode_object_find(dev, arg->prop_id,
3305 DRM_MODE_OBJECT_PROPERTY);
3306 if (!prop_obj)
3307 goto out;
3308 property = obj_to_property(prop_obj);
3309
3310 if (!drm_property_change_is_valid(property, arg->value))
3311 goto out;
3312
3313 switch (arg_obj->type) {
3314 case DRM_MODE_OBJECT_CONNECTOR:
3315 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
3316 arg->value);
3317 break;
3318 case DRM_MODE_OBJECT_CRTC:
3319 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
3320 break;
3321 case DRM_MODE_OBJECT_PLANE:
3322 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
3323 break;
3324 }
3325
3326 out:
3327 mutex_unlock(&dev->mode_config.mutex);
3328 return ret;
3329 }
3330
3331 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
3332 struct drm_encoder *encoder)
3333 {
3334 int i;
3335
3336 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3337 if (connector->encoder_ids[i] == 0) {
3338 connector->encoder_ids[i] = encoder->base.id;
3339 return 0;
3340 }
3341 }
3342 return -ENOMEM;
3343 }
3344 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
3345
3346 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
3347 struct drm_encoder *encoder)
3348 {
3349 int i;
3350 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
3351 if (connector->encoder_ids[i] == encoder->base.id) {
3352 connector->encoder_ids[i] = 0;
3353 if (connector->encoder == encoder)
3354 connector->encoder = NULL;
3355 break;
3356 }
3357 }
3358 }
3359 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
3360
3361 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
3362 int gamma_size)
3363 {
3364 crtc->gamma_size = gamma_size;
3365
3366 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
3367 if (!crtc->gamma_store) {
3368 crtc->gamma_size = 0;
3369 return -ENOMEM;
3370 }
3371
3372 return 0;
3373 }
3374 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
3375
3376 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
3377 void *data, struct drm_file *file_priv)
3378 {
3379 struct drm_mode_crtc_lut *crtc_lut = data;
3380 struct drm_mode_object *obj;
3381 struct drm_crtc *crtc;
3382 void *r_base, *g_base, *b_base;
3383 int size;
3384 int ret = 0;
3385
3386 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3387 return -EINVAL;
3388
3389 mutex_lock(&dev->mode_config.mutex);
3390 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3391 if (!obj) {
3392 ret = -EINVAL;
3393 goto out;
3394 }
3395 crtc = obj_to_crtc(obj);
3396
3397 if (crtc->funcs->gamma_set == NULL) {
3398 ret = -ENOSYS;
3399 goto out;
3400 }
3401
3402 /* memcpy into gamma store */
3403 if (crtc_lut->gamma_size != crtc->gamma_size) {
3404 ret = -EINVAL;
3405 goto out;
3406 }
3407
3408 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3409 r_base = crtc->gamma_store;
3410 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
3411 ret = -EFAULT;
3412 goto out;
3413 }
3414
3415 g_base = r_base + size;
3416 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
3417 ret = -EFAULT;
3418 goto out;
3419 }
3420
3421 b_base = g_base + size;
3422 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
3423 ret = -EFAULT;
3424 goto out;
3425 }
3426
3427 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
3428
3429 out:
3430 mutex_unlock(&dev->mode_config.mutex);
3431 return ret;
3432
3433 }
3434
3435 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
3436 void *data, struct drm_file *file_priv)
3437 {
3438 struct drm_mode_crtc_lut *crtc_lut = data;
3439 struct drm_mode_object *obj;
3440 struct drm_crtc *crtc;
3441 void *r_base, *g_base, *b_base;
3442 int size;
3443 int ret = 0;
3444
3445 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3446 return -EINVAL;
3447
3448 mutex_lock(&dev->mode_config.mutex);
3449 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
3450 if (!obj) {
3451 ret = -EINVAL;
3452 goto out;
3453 }
3454 crtc = obj_to_crtc(obj);
3455
3456 /* memcpy into gamma store */
3457 if (crtc_lut->gamma_size != crtc->gamma_size) {
3458 ret = -EINVAL;
3459 goto out;
3460 }
3461
3462 size = crtc_lut->gamma_size * (sizeof(uint16_t));
3463 r_base = crtc->gamma_store;
3464 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
3465 ret = -EFAULT;
3466 goto out;
3467 }
3468
3469 g_base = r_base + size;
3470 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
3471 ret = -EFAULT;
3472 goto out;
3473 }
3474
3475 b_base = g_base + size;
3476 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
3477 ret = -EFAULT;
3478 goto out;
3479 }
3480 out:
3481 mutex_unlock(&dev->mode_config.mutex);
3482 return ret;
3483 }
3484
3485 int drm_mode_page_flip_ioctl(struct drm_device *dev,
3486 void *data, struct drm_file *file_priv)
3487 {
3488 struct drm_mode_crtc_page_flip *page_flip = data;
3489 struct drm_mode_object *obj;
3490 struct drm_crtc *crtc;
3491 struct drm_framebuffer *fb;
3492 struct drm_pending_vblank_event *e = NULL;
3493 unsigned long flags;
3494 int ret = -EINVAL;
3495
3496 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
3497 page_flip->reserved != 0)
3498 return -EINVAL;
3499
3500 mutex_lock(&dev->mode_config.mutex);
3501 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
3502 if (!obj)
3503 goto out;
3504 crtc = obj_to_crtc(obj);
3505
3506 if (crtc->fb == NULL) {
3507 /* The framebuffer is currently unbound, presumably
3508 * due to a hotplug event, that userspace has not
3509 * yet discovered.
3510 */
3511 ret = -EBUSY;
3512 goto out;
3513 }
3514
3515 if (crtc->funcs->page_flip == NULL)
3516 goto out;
3517
3518 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
3519 if (!obj)
3520 goto out;
3521 fb = obj_to_fb(obj);
3522
3523 if (crtc->mode.hdisplay > fb->width ||
3524 crtc->mode.vdisplay > fb->height ||
3525 crtc->x > fb->width - crtc->mode.hdisplay ||
3526 crtc->y > fb->height - crtc->mode.vdisplay) {
3527 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d.\n",
3528 fb->width, fb->height,
3529 crtc->mode.hdisplay, crtc->mode.vdisplay,
3530 crtc->x, crtc->y);
3531 ret = -ENOSPC;
3532 goto out;
3533 }
3534
3535 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3536 ret = -ENOMEM;
3537 spin_lock_irqsave(&dev->event_lock, flags);
3538 if (file_priv->event_space < sizeof e->event) {
3539 spin_unlock_irqrestore(&dev->event_lock, flags);
3540 goto out;
3541 }
3542 file_priv->event_space -= sizeof e->event;
3543 spin_unlock_irqrestore(&dev->event_lock, flags);
3544
3545 e = kzalloc(sizeof *e, GFP_KERNEL);
3546 if (e == NULL) {
3547 spin_lock_irqsave(&dev->event_lock, flags);
3548 file_priv->event_space += sizeof e->event;
3549 spin_unlock_irqrestore(&dev->event_lock, flags);
3550 goto out;
3551 }
3552
3553 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
3554 e->event.base.length = sizeof e->event;
3555 e->event.user_data = page_flip->user_data;
3556 e->base.event = &e->event.base;
3557 e->base.file_priv = file_priv;
3558 e->base.destroy =
3559 (void (*) (struct drm_pending_event *)) kfree;
3560 }
3561
3562 ret = crtc->funcs->page_flip(crtc, fb, e);
3563 if (ret) {
3564 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
3565 spin_lock_irqsave(&dev->event_lock, flags);
3566 file_priv->event_space += sizeof e->event;
3567 spin_unlock_irqrestore(&dev->event_lock, flags);
3568 kfree(e);
3569 }
3570 }
3571
3572 out:
3573 mutex_unlock(&dev->mode_config.mutex);
3574 return ret;
3575 }
3576
3577 void drm_mode_config_reset(struct drm_device *dev)
3578 {
3579 struct drm_crtc *crtc;
3580 struct drm_encoder *encoder;
3581 struct drm_connector *connector;
3582
3583 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3584 if (crtc->funcs->reset)
3585 crtc->funcs->reset(crtc);
3586
3587 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
3588 if (encoder->funcs->reset)
3589 encoder->funcs->reset(encoder);
3590
3591 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
3592 if (connector->funcs->reset)
3593 connector->funcs->reset(connector);
3594 }
3595 EXPORT_SYMBOL(drm_mode_config_reset);
3596
3597 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
3598 void *data, struct drm_file *file_priv)
3599 {
3600 struct drm_mode_create_dumb *args = data;
3601
3602 if (!dev->driver->dumb_create)
3603 return -ENOSYS;
3604 return dev->driver->dumb_create(file_priv, dev, args);
3605 }
3606
3607 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
3608 void *data, struct drm_file *file_priv)
3609 {
3610 struct drm_mode_map_dumb *args = data;
3611
3612 /* call driver ioctl to get mmap offset */
3613 if (!dev->driver->dumb_map_offset)
3614 return -ENOSYS;
3615
3616 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
3617 }
3618
3619 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
3620 void *data, struct drm_file *file_priv)
3621 {
3622 struct drm_mode_destroy_dumb *args = data;
3623
3624 if (!dev->driver->dumb_destroy)
3625 return -ENOSYS;
3626
3627 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
3628 }
3629
3630 /*
3631 * Just need to support RGB formats here for compat with code that doesn't
3632 * use pixel formats directly yet.
3633 */
3634 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
3635 int *bpp)
3636 {
3637 switch (format) {
3638 case DRM_FORMAT_RGB332:
3639 case DRM_FORMAT_BGR233:
3640 *depth = 8;
3641 *bpp = 8;
3642 break;
3643 case DRM_FORMAT_XRGB1555:
3644 case DRM_FORMAT_XBGR1555:
3645 case DRM_FORMAT_RGBX5551:
3646 case DRM_FORMAT_BGRX5551:
3647 case DRM_FORMAT_ARGB1555:
3648 case DRM_FORMAT_ABGR1555:
3649 case DRM_FORMAT_RGBA5551:
3650 case DRM_FORMAT_BGRA5551:
3651 *depth = 15;
3652 *bpp = 16;
3653 break;
3654 case DRM_FORMAT_RGB565:
3655 case DRM_FORMAT_BGR565:
3656 *depth = 16;
3657 *bpp = 16;
3658 break;
3659 case DRM_FORMAT_RGB888:
3660 case DRM_FORMAT_BGR888:
3661 *depth = 24;
3662 *bpp = 24;
3663 break;
3664 case DRM_FORMAT_XRGB8888:
3665 case DRM_FORMAT_XBGR8888:
3666 case DRM_FORMAT_RGBX8888:
3667 case DRM_FORMAT_BGRX8888:
3668 *depth = 24;
3669 *bpp = 32;
3670 break;
3671 case DRM_FORMAT_XRGB2101010:
3672 case DRM_FORMAT_XBGR2101010:
3673 case DRM_FORMAT_RGBX1010102:
3674 case DRM_FORMAT_BGRX1010102:
3675 case DRM_FORMAT_ARGB2101010:
3676 case DRM_FORMAT_ABGR2101010:
3677 case DRM_FORMAT_RGBA1010102:
3678 case DRM_FORMAT_BGRA1010102:
3679 *depth = 30;
3680 *bpp = 32;
3681 break;
3682 case DRM_FORMAT_ARGB8888:
3683 case DRM_FORMAT_ABGR8888:
3684 case DRM_FORMAT_RGBA8888:
3685 case DRM_FORMAT_BGRA8888:
3686 *depth = 32;
3687 *bpp = 32;
3688 break;
3689 default:
3690 DRM_DEBUG_KMS("unsupported pixel format\n");
3691 *depth = 0;
3692 *bpp = 0;
3693 break;
3694 }
3695 }
3696 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
3697
3698 /**
3699 * drm_format_num_planes - get the number of planes for format
3700 * @format: pixel format (DRM_FORMAT_*)
3701 *
3702 * RETURNS:
3703 * The number of planes used by the specified pixel format.
3704 */
3705 int drm_format_num_planes(uint32_t format)
3706 {
3707 switch (format) {
3708 case DRM_FORMAT_YUV410:
3709 case DRM_FORMAT_YVU410:
3710 case DRM_FORMAT_YUV411:
3711 case DRM_FORMAT_YVU411:
3712 case DRM_FORMAT_YUV420:
3713 case DRM_FORMAT_YVU420:
3714 case DRM_FORMAT_YUV422:
3715 case DRM_FORMAT_YVU422:
3716 case DRM_FORMAT_YUV444:
3717 case DRM_FORMAT_YVU444:
3718 return 3;
3719 case DRM_FORMAT_NV12:
3720 case DRM_FORMAT_NV21:
3721 case DRM_FORMAT_NV16:
3722 case DRM_FORMAT_NV61:
3723 case DRM_FORMAT_NV24:
3724 case DRM_FORMAT_NV42:
3725 return 2;
3726 default:
3727 return 1;
3728 }
3729 }
3730 EXPORT_SYMBOL(drm_format_num_planes);
3731
3732 /**
3733 * drm_format_plane_cpp - determine the bytes per pixel value
3734 * @format: pixel format (DRM_FORMAT_*)
3735 * @plane: plane index
3736 *
3737 * RETURNS:
3738 * The bytes per pixel value for the specified plane.
3739 */
3740 int drm_format_plane_cpp(uint32_t format, int plane)
3741 {
3742 unsigned int depth;
3743 int bpp;
3744
3745 if (plane >= drm_format_num_planes(format))
3746 return 0;
3747
3748 switch (format) {
3749 case DRM_FORMAT_YUYV:
3750 case DRM_FORMAT_YVYU:
3751 case DRM_FORMAT_UYVY:
3752 case DRM_FORMAT_VYUY:
3753 return 2;
3754 case DRM_FORMAT_NV12:
3755 case DRM_FORMAT_NV21:
3756 case DRM_FORMAT_NV16:
3757 case DRM_FORMAT_NV61:
3758 case DRM_FORMAT_NV24:
3759 case DRM_FORMAT_NV42:
3760 return plane ? 2 : 1;
3761 case DRM_FORMAT_YUV410:
3762 case DRM_FORMAT_YVU410:
3763 case DRM_FORMAT_YUV411:
3764 case DRM_FORMAT_YVU411:
3765 case DRM_FORMAT_YUV420:
3766 case DRM_FORMAT_YVU420:
3767 case DRM_FORMAT_YUV422:
3768 case DRM_FORMAT_YVU422:
3769 case DRM_FORMAT_YUV444:
3770 case DRM_FORMAT_YVU444:
3771 return 1;
3772 default:
3773 drm_fb_get_bpp_depth(format, &depth, &bpp);
3774 return bpp >> 3;
3775 }
3776 }
3777 EXPORT_SYMBOL(drm_format_plane_cpp);
3778
3779 /**
3780 * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
3781 * @format: pixel format (DRM_FORMAT_*)
3782 *
3783 * RETURNS:
3784 * The horizontal chroma subsampling factor for the
3785 * specified pixel format.
3786 */
3787 int drm_format_horz_chroma_subsampling(uint32_t format)
3788 {
3789 switch (format) {
3790 case DRM_FORMAT_YUV411:
3791 case DRM_FORMAT_YVU411:
3792 case DRM_FORMAT_YUV410:
3793 case DRM_FORMAT_YVU410:
3794 return 4;
3795 case DRM_FORMAT_YUYV:
3796 case DRM_FORMAT_YVYU:
3797 case DRM_FORMAT_UYVY:
3798 case DRM_FORMAT_VYUY:
3799 case DRM_FORMAT_NV12:
3800 case DRM_FORMAT_NV21:
3801 case DRM_FORMAT_NV16:
3802 case DRM_FORMAT_NV61:
3803 case DRM_FORMAT_YUV422:
3804 case DRM_FORMAT_YVU422:
3805 case DRM_FORMAT_YUV420:
3806 case DRM_FORMAT_YVU420:
3807 return 2;
3808 default:
3809 return 1;
3810 }
3811 }
3812 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
3813
3814 /**
3815 * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
3816 * @format: pixel format (DRM_FORMAT_*)
3817 *
3818 * RETURNS:
3819 * The vertical chroma subsampling factor for the
3820 * specified pixel format.
3821 */
3822 int drm_format_vert_chroma_subsampling(uint32_t format)
3823 {
3824 switch (format) {
3825 case DRM_FORMAT_YUV410:
3826 case DRM_FORMAT_YVU410:
3827 return 4;
3828 case DRM_FORMAT_YUV420:
3829 case DRM_FORMAT_YVU420:
3830 case DRM_FORMAT_NV12:
3831 case DRM_FORMAT_NV21:
3832 return 2;
3833 default:
3834 return 1;
3835 }
3836 }
3837 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
This page took 0.111432 seconds and 5 git commands to generate.