Merge remote-tracking branch 'md/for-next'
[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/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <drm/drmP.h>
37 #include <drm/drm_crtc.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_fourcc.h>
40 #include <drm/drm_modeset_lock.h>
41 #include <drm/drm_atomic.h>
42 #include <drm/drm_auth.h>
43 #include <drm/drm_framebuffer.h>
44
45 #include "drm_crtc_internal.h"
46 #include "drm_internal.h"
47
48 /*
49 * Global properties
50 */
51 static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
52 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
53 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
54 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
55 };
56
57 /*
58 * Optional properties
59 */
60 /**
61 * drm_crtc_force_disable - Forcibly turn off a CRTC
62 * @crtc: CRTC to turn off
63 *
64 * Returns:
65 * Zero on success, error code on failure.
66 */
67 int drm_crtc_force_disable(struct drm_crtc *crtc)
68 {
69 struct drm_mode_set set = {
70 .crtc = crtc,
71 };
72
73 return drm_mode_set_config_internal(&set);
74 }
75 EXPORT_SYMBOL(drm_crtc_force_disable);
76
77 /**
78 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
79 * @dev: DRM device whose CRTCs to turn off
80 *
81 * Drivers may want to call this on unload to ensure that all displays are
82 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
83 *
84 * Returns:
85 * Zero on success, error code on failure.
86 */
87 int drm_crtc_force_disable_all(struct drm_device *dev)
88 {
89 struct drm_crtc *crtc;
90 int ret = 0;
91
92 drm_modeset_lock_all(dev);
93 drm_for_each_crtc(crtc, dev)
94 if (crtc->enabled) {
95 ret = drm_crtc_force_disable(crtc);
96 if (ret)
97 goto out;
98 }
99 out:
100 drm_modeset_unlock_all(dev);
101 return ret;
102 }
103 EXPORT_SYMBOL(drm_crtc_force_disable_all);
104
105 DEFINE_WW_CLASS(crtc_ww_class);
106
107 static unsigned int drm_num_crtcs(struct drm_device *dev)
108 {
109 unsigned int num = 0;
110 struct drm_crtc *tmp;
111
112 drm_for_each_crtc(tmp, dev) {
113 num++;
114 }
115
116 return num;
117 }
118
119 static int drm_crtc_register_all(struct drm_device *dev)
120 {
121 struct drm_crtc *crtc;
122 int ret = 0;
123
124 drm_for_each_crtc(crtc, dev) {
125 if (crtc->funcs->late_register)
126 ret = crtc->funcs->late_register(crtc);
127 if (ret)
128 return ret;
129 }
130
131 return 0;
132 }
133
134 static void drm_crtc_unregister_all(struct drm_device *dev)
135 {
136 struct drm_crtc *crtc;
137
138 drm_for_each_crtc(crtc, dev) {
139 if (crtc->funcs->early_unregister)
140 crtc->funcs->early_unregister(crtc);
141 }
142 }
143
144 /**
145 * drm_crtc_init_with_planes - Initialise a new CRTC object with
146 * specified primary and cursor planes.
147 * @dev: DRM device
148 * @crtc: CRTC object to init
149 * @primary: Primary plane for CRTC
150 * @cursor: Cursor plane for CRTC
151 * @funcs: callbacks for the new CRTC
152 * @name: printf style format string for the CRTC name, or NULL for default name
153 *
154 * Inits a new object created as base part of a driver crtc object.
155 *
156 * Returns:
157 * Zero on success, error code on failure.
158 */
159 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
160 struct drm_plane *primary,
161 struct drm_plane *cursor,
162 const struct drm_crtc_funcs *funcs,
163 const char *name, ...)
164 {
165 struct drm_mode_config *config = &dev->mode_config;
166 int ret;
167
168 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
169 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
170
171 crtc->dev = dev;
172 crtc->funcs = funcs;
173
174 INIT_LIST_HEAD(&crtc->commit_list);
175 spin_lock_init(&crtc->commit_lock);
176
177 drm_modeset_lock_init(&crtc->mutex);
178 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
179 if (ret)
180 return ret;
181
182 if (name) {
183 va_list ap;
184
185 va_start(ap, name);
186 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
187 va_end(ap);
188 } else {
189 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
190 drm_num_crtcs(dev));
191 }
192 if (!crtc->name) {
193 drm_mode_object_unregister(dev, &crtc->base);
194 return -ENOMEM;
195 }
196
197 crtc->base.properties = &crtc->properties;
198
199 list_add_tail(&crtc->head, &config->crtc_list);
200 crtc->index = config->num_crtc++;
201
202 crtc->primary = primary;
203 crtc->cursor = cursor;
204 if (primary)
205 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
206 if (cursor)
207 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
208
209 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
210 drm_object_attach_property(&crtc->base, config->prop_active, 0);
211 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
212 }
213
214 return 0;
215 }
216 EXPORT_SYMBOL(drm_crtc_init_with_planes);
217
218 /**
219 * drm_crtc_cleanup - Clean up the core crtc usage
220 * @crtc: CRTC to cleanup
221 *
222 * This function cleans up @crtc and removes it from the DRM mode setting
223 * core. Note that the function does *not* free the crtc structure itself,
224 * this is the responsibility of the caller.
225 */
226 void drm_crtc_cleanup(struct drm_crtc *crtc)
227 {
228 struct drm_device *dev = crtc->dev;
229
230 /* Note that the crtc_list is considered to be static; should we
231 * remove the drm_crtc at runtime we would have to decrement all
232 * the indices on the drm_crtc after us in the crtc_list.
233 */
234
235 kfree(crtc->gamma_store);
236 crtc->gamma_store = NULL;
237
238 drm_modeset_lock_fini(&crtc->mutex);
239
240 drm_mode_object_unregister(dev, &crtc->base);
241 list_del(&crtc->head);
242 dev->mode_config.num_crtc--;
243
244 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
245 if (crtc->state && crtc->funcs->atomic_destroy_state)
246 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
247
248 kfree(crtc->name);
249
250 memset(crtc, 0, sizeof(*crtc));
251 }
252 EXPORT_SYMBOL(drm_crtc_cleanup);
253
254 static unsigned int drm_num_planes(struct drm_device *dev)
255 {
256 unsigned int num = 0;
257 struct drm_plane *tmp;
258
259 drm_for_each_plane(tmp, dev) {
260 num++;
261 }
262
263 return num;
264 }
265
266 /**
267 * drm_universal_plane_init - Initialize a new universal plane object
268 * @dev: DRM device
269 * @plane: plane object to init
270 * @possible_crtcs: bitmask of possible CRTCs
271 * @funcs: callbacks for the new plane
272 * @formats: array of supported formats (DRM_FORMAT\_\*)
273 * @format_count: number of elements in @formats
274 * @type: type of plane (overlay, primary, cursor)
275 * @name: printf style format string for the plane name, or NULL for default name
276 *
277 * Initializes a plane object of type @type.
278 *
279 * Returns:
280 * Zero on success, error code on failure.
281 */
282 int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
283 unsigned long possible_crtcs,
284 const struct drm_plane_funcs *funcs,
285 const uint32_t *formats, unsigned int format_count,
286 enum drm_plane_type type,
287 const char *name, ...)
288 {
289 struct drm_mode_config *config = &dev->mode_config;
290 int ret;
291
292 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
293 if (ret)
294 return ret;
295
296 drm_modeset_lock_init(&plane->mutex);
297
298 plane->base.properties = &plane->properties;
299 plane->dev = dev;
300 plane->funcs = funcs;
301 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
302 GFP_KERNEL);
303 if (!plane->format_types) {
304 DRM_DEBUG_KMS("out of memory when allocating plane\n");
305 drm_mode_object_unregister(dev, &plane->base);
306 return -ENOMEM;
307 }
308
309 if (name) {
310 va_list ap;
311
312 va_start(ap, name);
313 plane->name = kvasprintf(GFP_KERNEL, name, ap);
314 va_end(ap);
315 } else {
316 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
317 drm_num_planes(dev));
318 }
319 if (!plane->name) {
320 kfree(plane->format_types);
321 drm_mode_object_unregister(dev, &plane->base);
322 return -ENOMEM;
323 }
324
325 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
326 plane->format_count = format_count;
327 plane->possible_crtcs = possible_crtcs;
328 plane->type = type;
329
330 list_add_tail(&plane->head, &config->plane_list);
331 plane->index = config->num_total_plane++;
332 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
333 config->num_overlay_plane++;
334
335 drm_object_attach_property(&plane->base,
336 config->plane_type_property,
337 plane->type);
338
339 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
340 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
341 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
342 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
343 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
344 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
345 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
346 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
347 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
348 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
349 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
350 }
351
352 return 0;
353 }
354 EXPORT_SYMBOL(drm_universal_plane_init);
355
356 static int drm_plane_register_all(struct drm_device *dev)
357 {
358 struct drm_plane *plane;
359 int ret = 0;
360
361 drm_for_each_plane(plane, dev) {
362 if (plane->funcs->late_register)
363 ret = plane->funcs->late_register(plane);
364 if (ret)
365 return ret;
366 }
367
368 return 0;
369 }
370
371 static void drm_plane_unregister_all(struct drm_device *dev)
372 {
373 struct drm_plane *plane;
374
375 drm_for_each_plane(plane, dev) {
376 if (plane->funcs->early_unregister)
377 plane->funcs->early_unregister(plane);
378 }
379 }
380
381 /**
382 * drm_plane_init - Initialize a legacy plane
383 * @dev: DRM device
384 * @plane: plane object to init
385 * @possible_crtcs: bitmask of possible CRTCs
386 * @funcs: callbacks for the new plane
387 * @formats: array of supported formats (DRM_FORMAT\_\*)
388 * @format_count: number of elements in @formats
389 * @is_primary: plane type (primary vs overlay)
390 *
391 * Legacy API to initialize a DRM plane.
392 *
393 * New drivers should call drm_universal_plane_init() instead.
394 *
395 * Returns:
396 * Zero on success, error code on failure.
397 */
398 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
399 unsigned long possible_crtcs,
400 const struct drm_plane_funcs *funcs,
401 const uint32_t *formats, unsigned int format_count,
402 bool is_primary)
403 {
404 enum drm_plane_type type;
405
406 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
407 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
408 formats, format_count, type, NULL);
409 }
410 EXPORT_SYMBOL(drm_plane_init);
411
412 /**
413 * drm_plane_cleanup - Clean up the core plane usage
414 * @plane: plane to cleanup
415 *
416 * This function cleans up @plane and removes it from the DRM mode setting
417 * core. Note that the function does *not* free the plane structure itself,
418 * this is the responsibility of the caller.
419 */
420 void drm_plane_cleanup(struct drm_plane *plane)
421 {
422 struct drm_device *dev = plane->dev;
423
424 drm_modeset_lock_all(dev);
425 kfree(plane->format_types);
426 drm_mode_object_unregister(dev, &plane->base);
427
428 BUG_ON(list_empty(&plane->head));
429
430 /* Note that the plane_list is considered to be static; should we
431 * remove the drm_plane at runtime we would have to decrement all
432 * the indices on the drm_plane after us in the plane_list.
433 */
434
435 list_del(&plane->head);
436 dev->mode_config.num_total_plane--;
437 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
438 dev->mode_config.num_overlay_plane--;
439 drm_modeset_unlock_all(dev);
440
441 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
442 if (plane->state && plane->funcs->atomic_destroy_state)
443 plane->funcs->atomic_destroy_state(plane, plane->state);
444
445 kfree(plane->name);
446
447 memset(plane, 0, sizeof(*plane));
448 }
449 EXPORT_SYMBOL(drm_plane_cleanup);
450
451 /**
452 * drm_plane_from_index - find the registered plane at an index
453 * @dev: DRM device
454 * @idx: index of registered plane to find for
455 *
456 * Given a plane index, return the registered plane from DRM device's
457 * list of planes with matching index.
458 */
459 struct drm_plane *
460 drm_plane_from_index(struct drm_device *dev, int idx)
461 {
462 struct drm_plane *plane;
463
464 drm_for_each_plane(plane, dev)
465 if (idx == plane->index)
466 return plane;
467
468 return NULL;
469 }
470 EXPORT_SYMBOL(drm_plane_from_index);
471
472 /**
473 * drm_plane_force_disable - Forcibly disable a plane
474 * @plane: plane to disable
475 *
476 * Forces the plane to be disabled.
477 *
478 * Used when the plane's current framebuffer is destroyed,
479 * and when restoring fbdev mode.
480 */
481 void drm_plane_force_disable(struct drm_plane *plane)
482 {
483 int ret;
484
485 if (!plane->fb)
486 return;
487
488 plane->old_fb = plane->fb;
489 ret = plane->funcs->disable_plane(plane);
490 if (ret) {
491 DRM_ERROR("failed to disable plane with busy fb\n");
492 plane->old_fb = NULL;
493 return;
494 }
495 /* disconnect the plane from the fb and crtc: */
496 drm_framebuffer_unreference(plane->old_fb);
497 plane->old_fb = NULL;
498 plane->fb = NULL;
499 plane->crtc = NULL;
500 }
501 EXPORT_SYMBOL(drm_plane_force_disable);
502
503 int drm_modeset_register_all(struct drm_device *dev)
504 {
505 int ret;
506
507 ret = drm_plane_register_all(dev);
508 if (ret)
509 goto err_plane;
510
511 ret = drm_crtc_register_all(dev);
512 if (ret)
513 goto err_crtc;
514
515 ret = drm_encoder_register_all(dev);
516 if (ret)
517 goto err_encoder;
518
519 ret = drm_connector_register_all(dev);
520 if (ret)
521 goto err_connector;
522
523 return 0;
524
525 err_connector:
526 drm_encoder_unregister_all(dev);
527 err_encoder:
528 drm_crtc_unregister_all(dev);
529 err_crtc:
530 drm_plane_unregister_all(dev);
531 err_plane:
532 return ret;
533 }
534
535 void drm_modeset_unregister_all(struct drm_device *dev)
536 {
537 drm_connector_unregister_all(dev);
538 drm_encoder_unregister_all(dev);
539 drm_crtc_unregister_all(dev);
540 drm_plane_unregister_all(dev);
541 }
542
543 static int drm_mode_create_standard_properties(struct drm_device *dev)
544 {
545 struct drm_property *prop;
546 int ret;
547
548 ret = drm_connector_create_standard_properties(dev);
549 if (ret)
550 return ret;
551
552 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
553 "type", drm_plane_type_enum_list,
554 ARRAY_SIZE(drm_plane_type_enum_list));
555 if (!prop)
556 return -ENOMEM;
557 dev->mode_config.plane_type_property = prop;
558
559 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
560 "SRC_X", 0, UINT_MAX);
561 if (!prop)
562 return -ENOMEM;
563 dev->mode_config.prop_src_x = prop;
564
565 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
566 "SRC_Y", 0, UINT_MAX);
567 if (!prop)
568 return -ENOMEM;
569 dev->mode_config.prop_src_y = prop;
570
571 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
572 "SRC_W", 0, UINT_MAX);
573 if (!prop)
574 return -ENOMEM;
575 dev->mode_config.prop_src_w = prop;
576
577 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
578 "SRC_H", 0, UINT_MAX);
579 if (!prop)
580 return -ENOMEM;
581 dev->mode_config.prop_src_h = prop;
582
583 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
584 "CRTC_X", INT_MIN, INT_MAX);
585 if (!prop)
586 return -ENOMEM;
587 dev->mode_config.prop_crtc_x = prop;
588
589 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
590 "CRTC_Y", INT_MIN, INT_MAX);
591 if (!prop)
592 return -ENOMEM;
593 dev->mode_config.prop_crtc_y = prop;
594
595 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
596 "CRTC_W", 0, INT_MAX);
597 if (!prop)
598 return -ENOMEM;
599 dev->mode_config.prop_crtc_w = prop;
600
601 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
602 "CRTC_H", 0, INT_MAX);
603 if (!prop)
604 return -ENOMEM;
605 dev->mode_config.prop_crtc_h = prop;
606
607 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
608 "FB_ID", DRM_MODE_OBJECT_FB);
609 if (!prop)
610 return -ENOMEM;
611 dev->mode_config.prop_fb_id = prop;
612
613 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
614 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
615 if (!prop)
616 return -ENOMEM;
617 dev->mode_config.prop_crtc_id = prop;
618
619 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
620 "ACTIVE");
621 if (!prop)
622 return -ENOMEM;
623 dev->mode_config.prop_active = prop;
624
625 prop = drm_property_create(dev,
626 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
627 "MODE_ID", 0);
628 if (!prop)
629 return -ENOMEM;
630 dev->mode_config.prop_mode_id = prop;
631
632 prop = drm_property_create(dev,
633 DRM_MODE_PROP_BLOB,
634 "DEGAMMA_LUT", 0);
635 if (!prop)
636 return -ENOMEM;
637 dev->mode_config.degamma_lut_property = prop;
638
639 prop = drm_property_create_range(dev,
640 DRM_MODE_PROP_IMMUTABLE,
641 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
642 if (!prop)
643 return -ENOMEM;
644 dev->mode_config.degamma_lut_size_property = prop;
645
646 prop = drm_property_create(dev,
647 DRM_MODE_PROP_BLOB,
648 "CTM", 0);
649 if (!prop)
650 return -ENOMEM;
651 dev->mode_config.ctm_property = prop;
652
653 prop = drm_property_create(dev,
654 DRM_MODE_PROP_BLOB,
655 "GAMMA_LUT", 0);
656 if (!prop)
657 return -ENOMEM;
658 dev->mode_config.gamma_lut_property = prop;
659
660 prop = drm_property_create_range(dev,
661 DRM_MODE_PROP_IMMUTABLE,
662 "GAMMA_LUT_SIZE", 0, UINT_MAX);
663 if (!prop)
664 return -ENOMEM;
665 dev->mode_config.gamma_lut_size_property = prop;
666
667 return 0;
668 }
669
670 /**
671 * drm_mode_getresources - get graphics configuration
672 * @dev: drm device for the ioctl
673 * @data: data pointer for the ioctl
674 * @file_priv: drm file for the ioctl call
675 *
676 * Construct a set of configuration description structures and return
677 * them to the user, including CRTC, connector and framebuffer configuration.
678 *
679 * Called by the user via ioctl.
680 *
681 * Returns:
682 * Zero on success, negative errno on failure.
683 */
684 int drm_mode_getresources(struct drm_device *dev, void *data,
685 struct drm_file *file_priv)
686 {
687 struct drm_mode_card_res *card_res = data;
688 struct list_head *lh;
689 struct drm_framebuffer *fb;
690 struct drm_connector *connector;
691 struct drm_crtc *crtc;
692 struct drm_encoder *encoder;
693 int ret = 0;
694 int connector_count = 0;
695 int crtc_count = 0;
696 int fb_count = 0;
697 int encoder_count = 0;
698 int copied = 0;
699 uint32_t __user *fb_id;
700 uint32_t __user *crtc_id;
701 uint32_t __user *connector_id;
702 uint32_t __user *encoder_id;
703
704 if (!drm_core_check_feature(dev, DRIVER_MODESET))
705 return -EINVAL;
706
707
708 mutex_lock(&file_priv->fbs_lock);
709 /*
710 * For the non-control nodes we need to limit the list of resources
711 * by IDs in the group list for this node
712 */
713 list_for_each(lh, &file_priv->fbs)
714 fb_count++;
715
716 /* handle this in 4 parts */
717 /* FBs */
718 if (card_res->count_fbs >= fb_count) {
719 copied = 0;
720 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
721 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
722 if (put_user(fb->base.id, fb_id + copied)) {
723 mutex_unlock(&file_priv->fbs_lock);
724 return -EFAULT;
725 }
726 copied++;
727 }
728 }
729 card_res->count_fbs = fb_count;
730 mutex_unlock(&file_priv->fbs_lock);
731
732 /* mode_config.mutex protects the connector list against e.g. DP MST
733 * connector hot-adding. CRTC/Plane lists are invariant. */
734 mutex_lock(&dev->mode_config.mutex);
735 drm_for_each_crtc(crtc, dev)
736 crtc_count++;
737
738 drm_for_each_connector(connector, dev)
739 connector_count++;
740
741 drm_for_each_encoder(encoder, dev)
742 encoder_count++;
743
744 card_res->max_height = dev->mode_config.max_height;
745 card_res->min_height = dev->mode_config.min_height;
746 card_res->max_width = dev->mode_config.max_width;
747 card_res->min_width = dev->mode_config.min_width;
748
749 /* CRTCs */
750 if (card_res->count_crtcs >= crtc_count) {
751 copied = 0;
752 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
753 drm_for_each_crtc(crtc, dev) {
754 if (put_user(crtc->base.id, crtc_id + copied)) {
755 ret = -EFAULT;
756 goto out;
757 }
758 copied++;
759 }
760 }
761 card_res->count_crtcs = crtc_count;
762
763 /* Encoders */
764 if (card_res->count_encoders >= encoder_count) {
765 copied = 0;
766 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
767 drm_for_each_encoder(encoder, dev) {
768 if (put_user(encoder->base.id, encoder_id +
769 copied)) {
770 ret = -EFAULT;
771 goto out;
772 }
773 copied++;
774 }
775 }
776 card_res->count_encoders = encoder_count;
777
778 /* Connectors */
779 if (card_res->count_connectors >= connector_count) {
780 copied = 0;
781 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
782 drm_for_each_connector(connector, dev) {
783 if (put_user(connector->base.id,
784 connector_id + copied)) {
785 ret = -EFAULT;
786 goto out;
787 }
788 copied++;
789 }
790 }
791 card_res->count_connectors = connector_count;
792
793 out:
794 mutex_unlock(&dev->mode_config.mutex);
795 return ret;
796 }
797
798 /**
799 * drm_mode_getcrtc - get CRTC configuration
800 * @dev: drm device for the ioctl
801 * @data: data pointer for the ioctl
802 * @file_priv: drm file for the ioctl call
803 *
804 * Construct a CRTC configuration structure to return to the user.
805 *
806 * Called by the user via ioctl.
807 *
808 * Returns:
809 * Zero on success, negative errno on failure.
810 */
811 int drm_mode_getcrtc(struct drm_device *dev,
812 void *data, struct drm_file *file_priv)
813 {
814 struct drm_mode_crtc *crtc_resp = data;
815 struct drm_crtc *crtc;
816
817 if (!drm_core_check_feature(dev, DRIVER_MODESET))
818 return -EINVAL;
819
820 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
821 if (!crtc)
822 return -ENOENT;
823
824 drm_modeset_lock_crtc(crtc, crtc->primary);
825 crtc_resp->gamma_size = crtc->gamma_size;
826 if (crtc->primary->fb)
827 crtc_resp->fb_id = crtc->primary->fb->base.id;
828 else
829 crtc_resp->fb_id = 0;
830
831 if (crtc->state) {
832 crtc_resp->x = crtc->primary->state->src_x >> 16;
833 crtc_resp->y = crtc->primary->state->src_y >> 16;
834 if (crtc->state->enable) {
835 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
836 crtc_resp->mode_valid = 1;
837
838 } else {
839 crtc_resp->mode_valid = 0;
840 }
841 } else {
842 crtc_resp->x = crtc->x;
843 crtc_resp->y = crtc->y;
844 if (crtc->enabled) {
845 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
846 crtc_resp->mode_valid = 1;
847
848 } else {
849 crtc_resp->mode_valid = 0;
850 }
851 }
852 drm_modeset_unlock_crtc(crtc);
853
854 return 0;
855 }
856
857 /**
858 * drm_mode_getplane_res - enumerate all plane resources
859 * @dev: DRM device
860 * @data: ioctl data
861 * @file_priv: DRM file info
862 *
863 * Construct a list of plane ids to return to the user.
864 *
865 * Called by the user via ioctl.
866 *
867 * Returns:
868 * Zero on success, negative errno on failure.
869 */
870 int drm_mode_getplane_res(struct drm_device *dev, void *data,
871 struct drm_file *file_priv)
872 {
873 struct drm_mode_get_plane_res *plane_resp = data;
874 struct drm_mode_config *config;
875 struct drm_plane *plane;
876 uint32_t __user *plane_ptr;
877 int copied = 0;
878 unsigned num_planes;
879
880 if (!drm_core_check_feature(dev, DRIVER_MODESET))
881 return -EINVAL;
882
883 config = &dev->mode_config;
884
885 if (file_priv->universal_planes)
886 num_planes = config->num_total_plane;
887 else
888 num_planes = config->num_overlay_plane;
889
890 /*
891 * This ioctl is called twice, once to determine how much space is
892 * needed, and the 2nd time to fill it.
893 */
894 if (num_planes &&
895 (plane_resp->count_planes >= num_planes)) {
896 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
897
898 /* Plane lists are invariant, no locking needed. */
899 drm_for_each_plane(plane, dev) {
900 /*
901 * Unless userspace set the 'universal planes'
902 * capability bit, only advertise overlays.
903 */
904 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
905 !file_priv->universal_planes)
906 continue;
907
908 if (put_user(plane->base.id, plane_ptr + copied))
909 return -EFAULT;
910 copied++;
911 }
912 }
913 plane_resp->count_planes = num_planes;
914
915 return 0;
916 }
917
918 /**
919 * drm_mode_getplane - get plane configuration
920 * @dev: DRM device
921 * @data: ioctl data
922 * @file_priv: DRM file info
923 *
924 * Construct a plane configuration structure to return to the user.
925 *
926 * Called by the user via ioctl.
927 *
928 * Returns:
929 * Zero on success, negative errno on failure.
930 */
931 int drm_mode_getplane(struct drm_device *dev, void *data,
932 struct drm_file *file_priv)
933 {
934 struct drm_mode_get_plane *plane_resp = data;
935 struct drm_plane *plane;
936 uint32_t __user *format_ptr;
937
938 if (!drm_core_check_feature(dev, DRIVER_MODESET))
939 return -EINVAL;
940
941 plane = drm_plane_find(dev, plane_resp->plane_id);
942 if (!plane)
943 return -ENOENT;
944
945 drm_modeset_lock(&plane->mutex, NULL);
946 if (plane->crtc)
947 plane_resp->crtc_id = plane->crtc->base.id;
948 else
949 plane_resp->crtc_id = 0;
950
951 if (plane->fb)
952 plane_resp->fb_id = plane->fb->base.id;
953 else
954 plane_resp->fb_id = 0;
955 drm_modeset_unlock(&plane->mutex);
956
957 plane_resp->plane_id = plane->base.id;
958 plane_resp->possible_crtcs = plane->possible_crtcs;
959 plane_resp->gamma_size = 0;
960
961 /*
962 * This ioctl is called twice, once to determine how much space is
963 * needed, and the 2nd time to fill it.
964 */
965 if (plane->format_count &&
966 (plane_resp->count_format_types >= plane->format_count)) {
967 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
968 if (copy_to_user(format_ptr,
969 plane->format_types,
970 sizeof(uint32_t) * plane->format_count)) {
971 return -EFAULT;
972 }
973 }
974 plane_resp->count_format_types = plane->format_count;
975
976 return 0;
977 }
978
979 /**
980 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
981 * @plane: plane to check for format support
982 * @format: the pixel format
983 *
984 * Returns:
985 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
986 * otherwise.
987 */
988 int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
989 {
990 unsigned int i;
991
992 for (i = 0; i < plane->format_count; i++) {
993 if (format == plane->format_types[i])
994 return 0;
995 }
996
997 return -EINVAL;
998 }
999
1000 static int check_src_coords(uint32_t src_x, uint32_t src_y,
1001 uint32_t src_w, uint32_t src_h,
1002 const struct drm_framebuffer *fb)
1003 {
1004 unsigned int fb_width, fb_height;
1005
1006 fb_width = fb->width << 16;
1007 fb_height = fb->height << 16;
1008
1009 /* Make sure source coordinates are inside the fb. */
1010 if (src_w > fb_width ||
1011 src_x > fb_width - src_w ||
1012 src_h > fb_height ||
1013 src_y > fb_height - src_h) {
1014 DRM_DEBUG_KMS("Invalid source coordinates "
1015 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
1016 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
1017 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
1018 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
1019 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
1020 return -ENOSPC;
1021 }
1022
1023 return 0;
1024 }
1025
1026 /*
1027 * setplane_internal - setplane handler for internal callers
1028 *
1029 * Note that we assume an extra reference has already been taken on fb. If the
1030 * update fails, this reference will be dropped before return; if it succeeds,
1031 * the previous framebuffer (if any) will be unreferenced instead.
1032 *
1033 * src_{x,y,w,h} are provided in 16.16 fixed point format
1034 */
1035 static int __setplane_internal(struct drm_plane *plane,
1036 struct drm_crtc *crtc,
1037 struct drm_framebuffer *fb,
1038 int32_t crtc_x, int32_t crtc_y,
1039 uint32_t crtc_w, uint32_t crtc_h,
1040 /* src_{x,y,w,h} values are 16.16 fixed point */
1041 uint32_t src_x, uint32_t src_y,
1042 uint32_t src_w, uint32_t src_h)
1043 {
1044 int ret = 0;
1045
1046 /* No fb means shut it down */
1047 if (!fb) {
1048 plane->old_fb = plane->fb;
1049 ret = plane->funcs->disable_plane(plane);
1050 if (!ret) {
1051 plane->crtc = NULL;
1052 plane->fb = NULL;
1053 } else {
1054 plane->old_fb = NULL;
1055 }
1056 goto out;
1057 }
1058
1059 /* Check whether this plane is usable on this CRTC */
1060 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
1061 DRM_DEBUG_KMS("Invalid crtc for plane\n");
1062 ret = -EINVAL;
1063 goto out;
1064 }
1065
1066 /* Check whether this plane supports the fb pixel format. */
1067 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
1068 if (ret) {
1069 char *format_name = drm_get_format_name(fb->pixel_format);
1070 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1071 kfree(format_name);
1072 goto out;
1073 }
1074
1075 /* Give drivers some help against integer overflows */
1076 if (crtc_w > INT_MAX ||
1077 crtc_x > INT_MAX - (int32_t) crtc_w ||
1078 crtc_h > INT_MAX ||
1079 crtc_y > INT_MAX - (int32_t) crtc_h) {
1080 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
1081 crtc_w, crtc_h, crtc_x, crtc_y);
1082 ret = -ERANGE;
1083 goto out;
1084 }
1085
1086 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
1087 if (ret)
1088 goto out;
1089
1090 plane->old_fb = plane->fb;
1091 ret = plane->funcs->update_plane(plane, crtc, fb,
1092 crtc_x, crtc_y, crtc_w, crtc_h,
1093 src_x, src_y, src_w, src_h);
1094 if (!ret) {
1095 plane->crtc = crtc;
1096 plane->fb = fb;
1097 fb = NULL;
1098 } else {
1099 plane->old_fb = NULL;
1100 }
1101
1102 out:
1103 if (fb)
1104 drm_framebuffer_unreference(fb);
1105 if (plane->old_fb)
1106 drm_framebuffer_unreference(plane->old_fb);
1107 plane->old_fb = NULL;
1108
1109 return ret;
1110 }
1111
1112 static int setplane_internal(struct drm_plane *plane,
1113 struct drm_crtc *crtc,
1114 struct drm_framebuffer *fb,
1115 int32_t crtc_x, int32_t crtc_y,
1116 uint32_t crtc_w, uint32_t crtc_h,
1117 /* src_{x,y,w,h} values are 16.16 fixed point */
1118 uint32_t src_x, uint32_t src_y,
1119 uint32_t src_w, uint32_t src_h)
1120 {
1121 int ret;
1122
1123 drm_modeset_lock_all(plane->dev);
1124 ret = __setplane_internal(plane, crtc, fb,
1125 crtc_x, crtc_y, crtc_w, crtc_h,
1126 src_x, src_y, src_w, src_h);
1127 drm_modeset_unlock_all(plane->dev);
1128
1129 return ret;
1130 }
1131
1132 /**
1133 * drm_mode_setplane - configure a plane's configuration
1134 * @dev: DRM device
1135 * @data: ioctl data*
1136 * @file_priv: DRM file info
1137 *
1138 * Set plane configuration, including placement, fb, scaling, and other factors.
1139 * Or pass a NULL fb to disable (planes may be disabled without providing a
1140 * valid crtc).
1141 *
1142 * Returns:
1143 * Zero on success, negative errno on failure.
1144 */
1145 int drm_mode_setplane(struct drm_device *dev, void *data,
1146 struct drm_file *file_priv)
1147 {
1148 struct drm_mode_set_plane *plane_req = data;
1149 struct drm_plane *plane;
1150 struct drm_crtc *crtc = NULL;
1151 struct drm_framebuffer *fb = NULL;
1152
1153 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1154 return -EINVAL;
1155
1156 /*
1157 * First, find the plane, crtc, and fb objects. If not available,
1158 * we don't bother to call the driver.
1159 */
1160 plane = drm_plane_find(dev, plane_req->plane_id);
1161 if (!plane) {
1162 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1163 plane_req->plane_id);
1164 return -ENOENT;
1165 }
1166
1167 if (plane_req->fb_id) {
1168 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
1169 if (!fb) {
1170 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
1171 plane_req->fb_id);
1172 return -ENOENT;
1173 }
1174
1175 crtc = drm_crtc_find(dev, plane_req->crtc_id);
1176 if (!crtc) {
1177 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1178 plane_req->crtc_id);
1179 return -ENOENT;
1180 }
1181 }
1182
1183 /*
1184 * setplane_internal will take care of deref'ing either the old or new
1185 * framebuffer depending on success.
1186 */
1187 return setplane_internal(plane, crtc, fb,
1188 plane_req->crtc_x, plane_req->crtc_y,
1189 plane_req->crtc_w, plane_req->crtc_h,
1190 plane_req->src_x, plane_req->src_y,
1191 plane_req->src_w, plane_req->src_h);
1192 }
1193
1194 /**
1195 * drm_mode_set_config_internal - helper to call ->set_config
1196 * @set: modeset config to set
1197 *
1198 * This is a little helper to wrap internal calls to the ->set_config driver
1199 * interface. The only thing it adds is correct refcounting dance.
1200 *
1201 * Returns:
1202 * Zero on success, negative errno on failure.
1203 */
1204 int drm_mode_set_config_internal(struct drm_mode_set *set)
1205 {
1206 struct drm_crtc *crtc = set->crtc;
1207 struct drm_framebuffer *fb;
1208 struct drm_crtc *tmp;
1209 int ret;
1210
1211 /*
1212 * NOTE: ->set_config can also disable other crtcs (if we steal all
1213 * connectors from it), hence we need to refcount the fbs across all
1214 * crtcs. Atomic modeset will have saner semantics ...
1215 */
1216 drm_for_each_crtc(tmp, crtc->dev)
1217 tmp->primary->old_fb = tmp->primary->fb;
1218
1219 fb = set->fb;
1220
1221 ret = crtc->funcs->set_config(set);
1222 if (ret == 0) {
1223 crtc->primary->crtc = crtc;
1224 crtc->primary->fb = fb;
1225 }
1226
1227 drm_for_each_crtc(tmp, crtc->dev) {
1228 if (tmp->primary->fb)
1229 drm_framebuffer_reference(tmp->primary->fb);
1230 if (tmp->primary->old_fb)
1231 drm_framebuffer_unreference(tmp->primary->old_fb);
1232 tmp->primary->old_fb = NULL;
1233 }
1234
1235 return ret;
1236 }
1237 EXPORT_SYMBOL(drm_mode_set_config_internal);
1238
1239 /**
1240 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
1241 * @mode: mode to query
1242 * @hdisplay: hdisplay value to fill in
1243 * @vdisplay: vdisplay value to fill in
1244 *
1245 * The vdisplay value will be doubled if the specified mode is a stereo mode of
1246 * the appropriate layout.
1247 */
1248 void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
1249 int *hdisplay, int *vdisplay)
1250 {
1251 struct drm_display_mode adjusted;
1252
1253 drm_mode_copy(&adjusted, mode);
1254 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
1255 *hdisplay = adjusted.crtc_hdisplay;
1256 *vdisplay = adjusted.crtc_vdisplay;
1257 }
1258 EXPORT_SYMBOL(drm_crtc_get_hv_timing);
1259
1260 /**
1261 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
1262 * CRTC viewport
1263 * @crtc: CRTC that framebuffer will be displayed on
1264 * @x: x panning
1265 * @y: y panning
1266 * @mode: mode that framebuffer will be displayed under
1267 * @fb: framebuffer to check size of
1268 */
1269 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
1270 int x, int y,
1271 const struct drm_display_mode *mode,
1272 const struct drm_framebuffer *fb)
1273
1274 {
1275 int hdisplay, vdisplay;
1276
1277 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
1278
1279 if (crtc->state &&
1280 crtc->primary->state->rotation & (DRM_ROTATE_90 |
1281 DRM_ROTATE_270))
1282 swap(hdisplay, vdisplay);
1283
1284 return check_src_coords(x << 16, y << 16,
1285 hdisplay << 16, vdisplay << 16, fb);
1286 }
1287 EXPORT_SYMBOL(drm_crtc_check_viewport);
1288
1289 /**
1290 * drm_mode_setcrtc - set CRTC configuration
1291 * @dev: drm device for the ioctl
1292 * @data: data pointer for the ioctl
1293 * @file_priv: drm file for the ioctl call
1294 *
1295 * Build a new CRTC configuration based on user request.
1296 *
1297 * Called by the user via ioctl.
1298 *
1299 * Returns:
1300 * Zero on success, negative errno on failure.
1301 */
1302 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1303 struct drm_file *file_priv)
1304 {
1305 struct drm_mode_config *config = &dev->mode_config;
1306 struct drm_mode_crtc *crtc_req = data;
1307 struct drm_crtc *crtc;
1308 struct drm_connector **connector_set = NULL, *connector;
1309 struct drm_framebuffer *fb = NULL;
1310 struct drm_display_mode *mode = NULL;
1311 struct drm_mode_set set;
1312 uint32_t __user *set_connectors_ptr;
1313 int ret;
1314 int i;
1315
1316 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1317 return -EINVAL;
1318
1319 /*
1320 * Universal plane src offsets are only 16.16, prevent havoc for
1321 * drivers using universal plane code internally.
1322 */
1323 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
1324 return -ERANGE;
1325
1326 drm_modeset_lock_all(dev);
1327 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
1328 if (!crtc) {
1329 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1330 ret = -ENOENT;
1331 goto out;
1332 }
1333 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
1334
1335 if (crtc_req->mode_valid) {
1336 /* If we have a mode we need a framebuffer. */
1337 /* If we pass -1, set the mode with the currently bound fb */
1338 if (crtc_req->fb_id == -1) {
1339 if (!crtc->primary->fb) {
1340 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1341 ret = -EINVAL;
1342 goto out;
1343 }
1344 fb = crtc->primary->fb;
1345 /* Make refcounting symmetric with the lookup path. */
1346 drm_framebuffer_reference(fb);
1347 } else {
1348 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1349 if (!fb) {
1350 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1351 crtc_req->fb_id);
1352 ret = -ENOENT;
1353 goto out;
1354 }
1355 }
1356
1357 mode = drm_mode_create(dev);
1358 if (!mode) {
1359 ret = -ENOMEM;
1360 goto out;
1361 }
1362
1363 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
1364 if (ret) {
1365 DRM_DEBUG_KMS("Invalid mode\n");
1366 goto out;
1367 }
1368
1369 /*
1370 * Check whether the primary plane supports the fb pixel format.
1371 * Drivers not implementing the universal planes API use a
1372 * default formats list provided by the DRM core which doesn't
1373 * match real hardware capabilities. Skip the check in that
1374 * case.
1375 */
1376 if (!crtc->primary->format_default) {
1377 ret = drm_plane_check_pixel_format(crtc->primary,
1378 fb->pixel_format);
1379 if (ret) {
1380 char *format_name = drm_get_format_name(fb->pixel_format);
1381 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1382 kfree(format_name);
1383 goto out;
1384 }
1385 }
1386
1387 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
1388 mode, fb);
1389 if (ret)
1390 goto out;
1391
1392 }
1393
1394 if (crtc_req->count_connectors == 0 && mode) {
1395 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1396 ret = -EINVAL;
1397 goto out;
1398 }
1399
1400 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1401 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1402 crtc_req->count_connectors);
1403 ret = -EINVAL;
1404 goto out;
1405 }
1406
1407 if (crtc_req->count_connectors > 0) {
1408 u32 out_id;
1409
1410 /* Avoid unbounded kernel memory allocation */
1411 if (crtc_req->count_connectors > config->num_connector) {
1412 ret = -EINVAL;
1413 goto out;
1414 }
1415
1416 connector_set = kmalloc_array(crtc_req->count_connectors,
1417 sizeof(struct drm_connector *),
1418 GFP_KERNEL);
1419 if (!connector_set) {
1420 ret = -ENOMEM;
1421 goto out;
1422 }
1423
1424 for (i = 0; i < crtc_req->count_connectors; i++) {
1425 connector_set[i] = NULL;
1426 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
1427 if (get_user(out_id, &set_connectors_ptr[i])) {
1428 ret = -EFAULT;
1429 goto out;
1430 }
1431
1432 connector = drm_connector_lookup(dev, out_id);
1433 if (!connector) {
1434 DRM_DEBUG_KMS("Connector id %d unknown\n",
1435 out_id);
1436 ret = -ENOENT;
1437 goto out;
1438 }
1439 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1440 connector->base.id,
1441 connector->name);
1442
1443 connector_set[i] = connector;
1444 }
1445 }
1446
1447 set.crtc = crtc;
1448 set.x = crtc_req->x;
1449 set.y = crtc_req->y;
1450 set.mode = mode;
1451 set.connectors = connector_set;
1452 set.num_connectors = crtc_req->count_connectors;
1453 set.fb = fb;
1454 ret = drm_mode_set_config_internal(&set);
1455
1456 out:
1457 if (fb)
1458 drm_framebuffer_unreference(fb);
1459
1460 if (connector_set) {
1461 for (i = 0; i < crtc_req->count_connectors; i++) {
1462 if (connector_set[i])
1463 drm_connector_unreference(connector_set[i]);
1464 }
1465 }
1466 kfree(connector_set);
1467 drm_mode_destroy(dev, mode);
1468 drm_modeset_unlock_all(dev);
1469 return ret;
1470 }
1471
1472 /**
1473 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
1474 * universal plane handler call
1475 * @crtc: crtc to update cursor for
1476 * @req: data pointer for the ioctl
1477 * @file_priv: drm file for the ioctl call
1478 *
1479 * Legacy cursor ioctl's work directly with driver buffer handles. To
1480 * translate legacy ioctl calls into universal plane handler calls, we need to
1481 * wrap the native buffer handle in a drm_framebuffer.
1482 *
1483 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
1484 * buffer with a pitch of 4*width; the universal plane interface should be used
1485 * directly in cases where the hardware can support other buffer settings and
1486 * userspace wants to make use of these capabilities.
1487 *
1488 * Returns:
1489 * Zero on success, negative errno on failure.
1490 */
1491 static int drm_mode_cursor_universal(struct drm_crtc *crtc,
1492 struct drm_mode_cursor2 *req,
1493 struct drm_file *file_priv)
1494 {
1495 struct drm_device *dev = crtc->dev;
1496 struct drm_framebuffer *fb = NULL;
1497 struct drm_mode_fb_cmd2 fbreq = {
1498 .width = req->width,
1499 .height = req->height,
1500 .pixel_format = DRM_FORMAT_ARGB8888,
1501 .pitches = { req->width * 4 },
1502 .handles = { req->handle },
1503 };
1504 int32_t crtc_x, crtc_y;
1505 uint32_t crtc_w = 0, crtc_h = 0;
1506 uint32_t src_w = 0, src_h = 0;
1507 int ret = 0;
1508
1509 BUG_ON(!crtc->cursor);
1510 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
1511
1512 /*
1513 * Obtain fb we'll be using (either new or existing) and take an extra
1514 * reference to it if fb != null. setplane will take care of dropping
1515 * the reference if the plane update fails.
1516 */
1517 if (req->flags & DRM_MODE_CURSOR_BO) {
1518 if (req->handle) {
1519 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
1520 if (IS_ERR(fb)) {
1521 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
1522 return PTR_ERR(fb);
1523 }
1524 fb->hot_x = req->hot_x;
1525 fb->hot_y = req->hot_y;
1526 } else {
1527 fb = NULL;
1528 }
1529 } else {
1530 fb = crtc->cursor->fb;
1531 if (fb)
1532 drm_framebuffer_reference(fb);
1533 }
1534
1535 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1536 crtc_x = req->x;
1537 crtc_y = req->y;
1538 } else {
1539 crtc_x = crtc->cursor_x;
1540 crtc_y = crtc->cursor_y;
1541 }
1542
1543 if (fb) {
1544 crtc_w = fb->width;
1545 crtc_h = fb->height;
1546 src_w = fb->width << 16;
1547 src_h = fb->height << 16;
1548 }
1549
1550 /*
1551 * setplane_internal will take care of deref'ing either the old or new
1552 * framebuffer depending on success.
1553 */
1554 ret = __setplane_internal(crtc->cursor, crtc, fb,
1555 crtc_x, crtc_y, crtc_w, crtc_h,
1556 0, 0, src_w, src_h);
1557
1558 /* Update successful; save new cursor position, if necessary */
1559 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
1560 crtc->cursor_x = req->x;
1561 crtc->cursor_y = req->y;
1562 }
1563
1564 return ret;
1565 }
1566
1567 static int drm_mode_cursor_common(struct drm_device *dev,
1568 struct drm_mode_cursor2 *req,
1569 struct drm_file *file_priv)
1570 {
1571 struct drm_crtc *crtc;
1572 int ret = 0;
1573
1574 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1575 return -EINVAL;
1576
1577 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
1578 return -EINVAL;
1579
1580 crtc = drm_crtc_find(dev, req->crtc_id);
1581 if (!crtc) {
1582 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1583 return -ENOENT;
1584 }
1585
1586 /*
1587 * If this crtc has a universal cursor plane, call that plane's update
1588 * handler rather than using legacy cursor handlers.
1589 */
1590 drm_modeset_lock_crtc(crtc, crtc->cursor);
1591 if (crtc->cursor) {
1592 ret = drm_mode_cursor_universal(crtc, req, file_priv);
1593 goto out;
1594 }
1595
1596 if (req->flags & DRM_MODE_CURSOR_BO) {
1597 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
1598 ret = -ENXIO;
1599 goto out;
1600 }
1601 /* Turns off the cursor if handle is 0 */
1602 if (crtc->funcs->cursor_set2)
1603 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
1604 req->width, req->height, req->hot_x, req->hot_y);
1605 else
1606 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1607 req->width, req->height);
1608 }
1609
1610 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1611 if (crtc->funcs->cursor_move) {
1612 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1613 } else {
1614 ret = -EFAULT;
1615 goto out;
1616 }
1617 }
1618 out:
1619 drm_modeset_unlock_crtc(crtc);
1620
1621 return ret;
1622
1623 }
1624
1625
1626 /**
1627 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
1628 * @dev: drm device for the ioctl
1629 * @data: data pointer for the ioctl
1630 * @file_priv: drm file for the ioctl call
1631 *
1632 * Set the cursor configuration based on user request.
1633 *
1634 * Called by the user via ioctl.
1635 *
1636 * Returns:
1637 * Zero on success, negative errno on failure.
1638 */
1639 int drm_mode_cursor_ioctl(struct drm_device *dev,
1640 void *data, struct drm_file *file_priv)
1641 {
1642 struct drm_mode_cursor *req = data;
1643 struct drm_mode_cursor2 new_req;
1644
1645 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
1646 new_req.hot_x = new_req.hot_y = 0;
1647
1648 return drm_mode_cursor_common(dev, &new_req, file_priv);
1649 }
1650
1651 /**
1652 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
1653 * @dev: drm device for the ioctl
1654 * @data: data pointer for the ioctl
1655 * @file_priv: drm file for the ioctl call
1656 *
1657 * Set the cursor configuration based on user request. This implements the 2nd
1658 * version of the cursor ioctl, which allows userspace to additionally specify
1659 * the hotspot of the pointer.
1660 *
1661 * Called by the user via ioctl.
1662 *
1663 * Returns:
1664 * Zero on success, negative errno on failure.
1665 */
1666 int drm_mode_cursor2_ioctl(struct drm_device *dev,
1667 void *data, struct drm_file *file_priv)
1668 {
1669 struct drm_mode_cursor2 *req = data;
1670
1671 return drm_mode_cursor_common(dev, req, file_priv);
1672 }
1673
1674 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
1675 struct drm_property *property,
1676 uint64_t value)
1677 {
1678 int ret = -EINVAL;
1679 struct drm_crtc *crtc = obj_to_crtc(obj);
1680
1681 if (crtc->funcs->set_property)
1682 ret = crtc->funcs->set_property(crtc, property, value);
1683 if (!ret)
1684 drm_object_property_set_value(obj, property, value);
1685
1686 return ret;
1687 }
1688
1689 /**
1690 * drm_mode_plane_set_obj_prop - set the value of a property
1691 * @plane: drm plane object to set property value for
1692 * @property: property to set
1693 * @value: value the property should be set to
1694 *
1695 * This functions sets a given property on a given plane object. This function
1696 * calls the driver's ->set_property callback and changes the software state of
1697 * the property if the callback succeeds.
1698 *
1699 * Returns:
1700 * Zero on success, error code on failure.
1701 */
1702 int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
1703 struct drm_property *property,
1704 uint64_t value)
1705 {
1706 int ret = -EINVAL;
1707 struct drm_mode_object *obj = &plane->base;
1708
1709 if (plane->funcs->set_property)
1710 ret = plane->funcs->set_property(plane, property, value);
1711 if (!ret)
1712 drm_object_property_set_value(obj, property, value);
1713
1714 return ret;
1715 }
1716 EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
1717
1718 /**
1719 * drm_mode_crtc_set_gamma_size - set the gamma table size
1720 * @crtc: CRTC to set the gamma table size for
1721 * @gamma_size: size of the gamma table
1722 *
1723 * Drivers which support gamma tables should set this to the supported gamma
1724 * table size when initializing the CRTC. Currently the drm core only supports a
1725 * fixed gamma table size.
1726 *
1727 * Returns:
1728 * Zero on success, negative errno on failure.
1729 */
1730 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
1731 int gamma_size)
1732 {
1733 uint16_t *r_base, *g_base, *b_base;
1734 int i;
1735
1736 crtc->gamma_size = gamma_size;
1737
1738 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
1739 GFP_KERNEL);
1740 if (!crtc->gamma_store) {
1741 crtc->gamma_size = 0;
1742 return -ENOMEM;
1743 }
1744
1745 r_base = crtc->gamma_store;
1746 g_base = r_base + gamma_size;
1747 b_base = g_base + gamma_size;
1748 for (i = 0; i < gamma_size; i++) {
1749 r_base[i] = i << 8;
1750 g_base[i] = i << 8;
1751 b_base[i] = i << 8;
1752 }
1753
1754
1755 return 0;
1756 }
1757 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
1758
1759 /**
1760 * drm_mode_gamma_set_ioctl - set the gamma table
1761 * @dev: DRM device
1762 * @data: ioctl data
1763 * @file_priv: DRM file info
1764 *
1765 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
1766 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
1767 *
1768 * Called by the user via ioctl.
1769 *
1770 * Returns:
1771 * Zero on success, negative errno on failure.
1772 */
1773 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
1774 void *data, struct drm_file *file_priv)
1775 {
1776 struct drm_mode_crtc_lut *crtc_lut = data;
1777 struct drm_crtc *crtc;
1778 void *r_base, *g_base, *b_base;
1779 int size;
1780 int ret = 0;
1781
1782 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1783 return -EINVAL;
1784
1785 drm_modeset_lock_all(dev);
1786 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1787 if (!crtc) {
1788 ret = -ENOENT;
1789 goto out;
1790 }
1791
1792 if (crtc->funcs->gamma_set == NULL) {
1793 ret = -ENOSYS;
1794 goto out;
1795 }
1796
1797 /* memcpy into gamma store */
1798 if (crtc_lut->gamma_size != crtc->gamma_size) {
1799 ret = -EINVAL;
1800 goto out;
1801 }
1802
1803 size = crtc_lut->gamma_size * (sizeof(uint16_t));
1804 r_base = crtc->gamma_store;
1805 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
1806 ret = -EFAULT;
1807 goto out;
1808 }
1809
1810 g_base = r_base + size;
1811 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
1812 ret = -EFAULT;
1813 goto out;
1814 }
1815
1816 b_base = g_base + size;
1817 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
1818 ret = -EFAULT;
1819 goto out;
1820 }
1821
1822 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
1823
1824 out:
1825 drm_modeset_unlock_all(dev);
1826 return ret;
1827
1828 }
1829
1830 /**
1831 * drm_mode_gamma_get_ioctl - get the gamma table
1832 * @dev: DRM device
1833 * @data: ioctl data
1834 * @file_priv: DRM file info
1835 *
1836 * Copy the current gamma table into the storage provided. This also provides
1837 * the gamma table size the driver expects, which can be used to size the
1838 * allocated storage.
1839 *
1840 * Called by the user via ioctl.
1841 *
1842 * Returns:
1843 * Zero on success, negative errno on failure.
1844 */
1845 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
1846 void *data, struct drm_file *file_priv)
1847 {
1848 struct drm_mode_crtc_lut *crtc_lut = data;
1849 struct drm_crtc *crtc;
1850 void *r_base, *g_base, *b_base;
1851 int size;
1852 int ret = 0;
1853
1854 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1855 return -EINVAL;
1856
1857 drm_modeset_lock_all(dev);
1858 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1859 if (!crtc) {
1860 ret = -ENOENT;
1861 goto out;
1862 }
1863
1864 /* memcpy into gamma store */
1865 if (crtc_lut->gamma_size != crtc->gamma_size) {
1866 ret = -EINVAL;
1867 goto out;
1868 }
1869
1870 size = crtc_lut->gamma_size * (sizeof(uint16_t));
1871 r_base = crtc->gamma_store;
1872 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
1873 ret = -EFAULT;
1874 goto out;
1875 }
1876
1877 g_base = r_base + size;
1878 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
1879 ret = -EFAULT;
1880 goto out;
1881 }
1882
1883 b_base = g_base + size;
1884 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
1885 ret = -EFAULT;
1886 goto out;
1887 }
1888 out:
1889 drm_modeset_unlock_all(dev);
1890 return ret;
1891 }
1892
1893 /**
1894 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
1895 * @dev: DRM device
1896 * @data: ioctl data
1897 * @file_priv: DRM file info
1898 *
1899 * This schedules an asynchronous update on a given CRTC, called page flip.
1900 * Optionally a drm event is generated to signal the completion of the event.
1901 * Generic drivers cannot assume that a pageflip with changed framebuffer
1902 * properties (including driver specific metadata like tiling layout) will work,
1903 * but some drivers support e.g. pixel format changes through the pageflip
1904 * ioctl.
1905 *
1906 * Called by the user via ioctl.
1907 *
1908 * Returns:
1909 * Zero on success, negative errno on failure.
1910 */
1911 int drm_mode_page_flip_ioctl(struct drm_device *dev,
1912 void *data, struct drm_file *file_priv)
1913 {
1914 struct drm_mode_crtc_page_flip_target *page_flip = data;
1915 struct drm_crtc *crtc;
1916 struct drm_framebuffer *fb = NULL;
1917 struct drm_pending_vblank_event *e = NULL;
1918 u32 target_vblank = page_flip->sequence;
1919 int ret = -EINVAL;
1920
1921 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1922 return -EINVAL;
1923
1924 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS)
1925 return -EINVAL;
1926
1927 if (page_flip->sequence != 0 && !(page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET))
1928 return -EINVAL;
1929
1930 /* Only one of the DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE/RELATIVE flags
1931 * can be specified
1932 */
1933 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) == DRM_MODE_PAGE_FLIP_TARGET)
1934 return -EINVAL;
1935
1936 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1937 return -EINVAL;
1938
1939 crtc = drm_crtc_find(dev, page_flip->crtc_id);
1940 if (!crtc)
1941 return -ENOENT;
1942
1943 if (crtc->funcs->page_flip_target) {
1944 u32 current_vblank;
1945 int r;
1946
1947 r = drm_crtc_vblank_get(crtc);
1948 if (r)
1949 return r;
1950
1951 current_vblank = drm_crtc_vblank_count(crtc);
1952
1953 switch (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET) {
1954 case DRM_MODE_PAGE_FLIP_TARGET_ABSOLUTE:
1955 if ((int)(target_vblank - current_vblank) > 1) {
1956 DRM_DEBUG("Invalid absolute flip target %u, "
1957 "must be <= %u\n", target_vblank,
1958 current_vblank + 1);
1959 drm_crtc_vblank_put(crtc);
1960 return -EINVAL;
1961 }
1962 break;
1963 case DRM_MODE_PAGE_FLIP_TARGET_RELATIVE:
1964 if (target_vblank != 0 && target_vblank != 1) {
1965 DRM_DEBUG("Invalid relative flip target %u, "
1966 "must be 0 or 1\n", target_vblank);
1967 drm_crtc_vblank_put(crtc);
1968 return -EINVAL;
1969 }
1970 target_vblank += current_vblank;
1971 break;
1972 default:
1973 target_vblank = current_vblank +
1974 !(page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC);
1975 break;
1976 }
1977 } else if (crtc->funcs->page_flip == NULL ||
1978 (page_flip->flags & DRM_MODE_PAGE_FLIP_TARGET)) {
1979 return -EINVAL;
1980 }
1981
1982 drm_modeset_lock_crtc(crtc, crtc->primary);
1983 if (crtc->primary->fb == NULL) {
1984 /* The framebuffer is currently unbound, presumably
1985 * due to a hotplug event, that userspace has not
1986 * yet discovered.
1987 */
1988 ret = -EBUSY;
1989 goto out;
1990 }
1991
1992 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
1993 if (!fb) {
1994 ret = -ENOENT;
1995 goto out;
1996 }
1997
1998 if (crtc->state) {
1999 const struct drm_plane_state *state = crtc->primary->state;
2000
2001 ret = check_src_coords(state->src_x, state->src_y,
2002 state->src_w, state->src_h, fb);
2003 } else {
2004 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
2005 }
2006 if (ret)
2007 goto out;
2008
2009 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
2010 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
2011 ret = -EINVAL;
2012 goto out;
2013 }
2014
2015 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2016 e = kzalloc(sizeof *e, GFP_KERNEL);
2017 if (!e) {
2018 ret = -ENOMEM;
2019 goto out;
2020 }
2021 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2022 e->event.base.length = sizeof(e->event);
2023 e->event.user_data = page_flip->user_data;
2024 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
2025 if (ret) {
2026 kfree(e);
2027 goto out;
2028 }
2029 }
2030
2031 crtc->primary->old_fb = crtc->primary->fb;
2032 if (crtc->funcs->page_flip_target)
2033 ret = crtc->funcs->page_flip_target(crtc, fb, e,
2034 page_flip->flags,
2035 target_vblank);
2036 else
2037 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
2038 if (ret) {
2039 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
2040 drm_event_cancel_free(dev, &e->base);
2041 /* Keep the old fb, don't unref it. */
2042 crtc->primary->old_fb = NULL;
2043 } else {
2044 crtc->primary->fb = fb;
2045 /* Unref only the old framebuffer. */
2046 fb = NULL;
2047 }
2048
2049 out:
2050 if (ret && crtc->funcs->page_flip_target)
2051 drm_crtc_vblank_put(crtc);
2052 if (fb)
2053 drm_framebuffer_unreference(fb);
2054 if (crtc->primary->old_fb)
2055 drm_framebuffer_unreference(crtc->primary->old_fb);
2056 crtc->primary->old_fb = NULL;
2057 drm_modeset_unlock_crtc(crtc);
2058
2059 return ret;
2060 }
2061
2062 /**
2063 * drm_mode_config_reset - call ->reset callbacks
2064 * @dev: drm device
2065 *
2066 * This functions calls all the crtc's, encoder's and connector's ->reset
2067 * callback. Drivers can use this in e.g. their driver load or resume code to
2068 * reset hardware and software state.
2069 */
2070 void drm_mode_config_reset(struct drm_device *dev)
2071 {
2072 struct drm_crtc *crtc;
2073 struct drm_plane *plane;
2074 struct drm_encoder *encoder;
2075 struct drm_connector *connector;
2076
2077 drm_for_each_plane(plane, dev)
2078 if (plane->funcs->reset)
2079 plane->funcs->reset(plane);
2080
2081 drm_for_each_crtc(crtc, dev)
2082 if (crtc->funcs->reset)
2083 crtc->funcs->reset(crtc);
2084
2085 drm_for_each_encoder(encoder, dev)
2086 if (encoder->funcs->reset)
2087 encoder->funcs->reset(encoder);
2088
2089 mutex_lock(&dev->mode_config.mutex);
2090 drm_for_each_connector(connector, dev)
2091 if (connector->funcs->reset)
2092 connector->funcs->reset(connector);
2093 mutex_unlock(&dev->mode_config.mutex);
2094 }
2095 EXPORT_SYMBOL(drm_mode_config_reset);
2096
2097 /**
2098 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
2099 * @dev: DRM device
2100 * @data: ioctl data
2101 * @file_priv: DRM file info
2102 *
2103 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
2104 * TTM or something else entirely) and returns the resulting buffer handle. This
2105 * handle can then be wrapped up into a framebuffer modeset object.
2106 *
2107 * Note that userspace is not allowed to use such objects for render
2108 * acceleration - drivers must create their own private ioctls for such a use
2109 * case.
2110 *
2111 * Called by the user via ioctl.
2112 *
2113 * Returns:
2114 * Zero on success, negative errno on failure.
2115 */
2116 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2117 void *data, struct drm_file *file_priv)
2118 {
2119 struct drm_mode_create_dumb *args = data;
2120 u32 cpp, stride, size;
2121
2122 if (!dev->driver->dumb_create)
2123 return -ENOSYS;
2124 if (!args->width || !args->height || !args->bpp)
2125 return -EINVAL;
2126
2127 /* overflow checks for 32bit size calculations */
2128 /* NOTE: DIV_ROUND_UP() can overflow */
2129 cpp = DIV_ROUND_UP(args->bpp, 8);
2130 if (!cpp || cpp > 0xffffffffU / args->width)
2131 return -EINVAL;
2132 stride = cpp * args->width;
2133 if (args->height > 0xffffffffU / stride)
2134 return -EINVAL;
2135
2136 /* test for wrap-around */
2137 size = args->height * stride;
2138 if (PAGE_ALIGN(size) == 0)
2139 return -EINVAL;
2140
2141 /*
2142 * handle, pitch and size are output parameters. Zero them out to
2143 * prevent drivers from accidentally using uninitialized data. Since
2144 * not all existing userspace is clearing these fields properly we
2145 * cannot reject IOCTL with garbage in them.
2146 */
2147 args->handle = 0;
2148 args->pitch = 0;
2149 args->size = 0;
2150
2151 return dev->driver->dumb_create(file_priv, dev, args);
2152 }
2153
2154 /**
2155 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
2156 * @dev: DRM device
2157 * @data: ioctl data
2158 * @file_priv: DRM file info
2159 *
2160 * Allocate an offset in the drm device node's address space to be able to
2161 * memory map a dumb buffer.
2162 *
2163 * Called by the user via ioctl.
2164 *
2165 * Returns:
2166 * Zero on success, negative errno on failure.
2167 */
2168 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2169 void *data, struct drm_file *file_priv)
2170 {
2171 struct drm_mode_map_dumb *args = data;
2172
2173 /* call driver ioctl to get mmap offset */
2174 if (!dev->driver->dumb_map_offset)
2175 return -ENOSYS;
2176
2177 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
2178 }
2179
2180 /**
2181 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
2182 * @dev: DRM device
2183 * @data: ioctl data
2184 * @file_priv: DRM file info
2185 *
2186 * This destroys the userspace handle for the given dumb backing storage buffer.
2187 * Since buffer objects must be reference counted in the kernel a buffer object
2188 * won't be immediately freed if a framebuffer modeset object still uses it.
2189 *
2190 * Called by the user via ioctl.
2191 *
2192 * Returns:
2193 * Zero on success, negative errno on failure.
2194 */
2195 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2196 void *data, struct drm_file *file_priv)
2197 {
2198 struct drm_mode_destroy_dumb *args = data;
2199
2200 if (!dev->driver->dumb_destroy)
2201 return -ENOSYS;
2202
2203 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
2204 }
2205
2206 /**
2207 * drm_rotation_simplify() - Try to simplify the rotation
2208 * @rotation: Rotation to be simplified
2209 * @supported_rotations: Supported rotations
2210 *
2211 * Attempt to simplify the rotation to a form that is supported.
2212 * Eg. if the hardware supports everything except DRM_REFLECT_X
2213 * one could call this function like this:
2214 *
2215 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
2216 * DRM_ROTATE_90 | DRM_ROTATE_180 |
2217 * DRM_ROTATE_270 | DRM_REFLECT_Y);
2218 *
2219 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
2220 * transforms the hardware supports, this function may not
2221 * be able to produce a supported transform, so the caller should
2222 * check the result afterwards.
2223 */
2224 unsigned int drm_rotation_simplify(unsigned int rotation,
2225 unsigned int supported_rotations)
2226 {
2227 if (rotation & ~supported_rotations) {
2228 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
2229 rotation = (rotation & DRM_REFLECT_MASK) |
2230 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
2231 }
2232
2233 return rotation;
2234 }
2235 EXPORT_SYMBOL(drm_rotation_simplify);
2236
2237 /**
2238 * drm_mode_config_init - initialize DRM mode_configuration structure
2239 * @dev: DRM device
2240 *
2241 * Initialize @dev's mode_config structure, used for tracking the graphics
2242 * configuration of @dev.
2243 *
2244 * Since this initializes the modeset locks, no locking is possible. Which is no
2245 * problem, since this should happen single threaded at init time. It is the
2246 * driver's problem to ensure this guarantee.
2247 *
2248 */
2249 void drm_mode_config_init(struct drm_device *dev)
2250 {
2251 mutex_init(&dev->mode_config.mutex);
2252 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
2253 mutex_init(&dev->mode_config.idr_mutex);
2254 mutex_init(&dev->mode_config.fb_lock);
2255 mutex_init(&dev->mode_config.blob_lock);
2256 INIT_LIST_HEAD(&dev->mode_config.fb_list);
2257 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
2258 INIT_LIST_HEAD(&dev->mode_config.connector_list);
2259 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
2260 INIT_LIST_HEAD(&dev->mode_config.property_list);
2261 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
2262 INIT_LIST_HEAD(&dev->mode_config.plane_list);
2263 idr_init(&dev->mode_config.crtc_idr);
2264 idr_init(&dev->mode_config.tile_idr);
2265 ida_init(&dev->mode_config.connector_ida);
2266
2267 drm_modeset_lock_all(dev);
2268 drm_mode_create_standard_properties(dev);
2269 drm_modeset_unlock_all(dev);
2270
2271 /* Just to be sure */
2272 dev->mode_config.num_fb = 0;
2273 dev->mode_config.num_connector = 0;
2274 dev->mode_config.num_crtc = 0;
2275 dev->mode_config.num_encoder = 0;
2276 dev->mode_config.num_overlay_plane = 0;
2277 dev->mode_config.num_total_plane = 0;
2278 }
2279 EXPORT_SYMBOL(drm_mode_config_init);
2280
2281 /**
2282 * drm_mode_config_cleanup - free up DRM mode_config info
2283 * @dev: DRM device
2284 *
2285 * Free up all the connectors and CRTCs associated with this DRM device, then
2286 * free up the framebuffers and associated buffer objects.
2287 *
2288 * Note that since this /should/ happen single-threaded at driver/device
2289 * teardown time, no locking is required. It's the driver's job to ensure that
2290 * this guarantee actually holds true.
2291 *
2292 * FIXME: cleanup any dangling user buffer objects too
2293 */
2294 void drm_mode_config_cleanup(struct drm_device *dev)
2295 {
2296 struct drm_connector *connector, *ot;
2297 struct drm_crtc *crtc, *ct;
2298 struct drm_encoder *encoder, *enct;
2299 struct drm_framebuffer *fb, *fbt;
2300 struct drm_property *property, *pt;
2301 struct drm_property_blob *blob, *bt;
2302 struct drm_plane *plane, *plt;
2303
2304 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
2305 head) {
2306 encoder->funcs->destroy(encoder);
2307 }
2308
2309 list_for_each_entry_safe(connector, ot,
2310 &dev->mode_config.connector_list, head) {
2311 connector->funcs->destroy(connector);
2312 }
2313
2314 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
2315 head) {
2316 drm_property_destroy(dev, property);
2317 }
2318
2319 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
2320 head) {
2321 plane->funcs->destroy(plane);
2322 }
2323
2324 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
2325 crtc->funcs->destroy(crtc);
2326 }
2327
2328 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
2329 head_global) {
2330 drm_property_unreference_blob(blob);
2331 }
2332
2333 /*
2334 * Single-threaded teardown context, so it's not required to grab the
2335 * fb_lock to protect against concurrent fb_list access. Contrary, it
2336 * would actually deadlock with the drm_framebuffer_cleanup function.
2337 *
2338 * Also, if there are any framebuffers left, that's a driver leak now,
2339 * so politely WARN about this.
2340 */
2341 WARN_ON(!list_empty(&dev->mode_config.fb_list));
2342 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
2343 drm_framebuffer_free(&fb->base.refcount);
2344 }
2345
2346 ida_destroy(&dev->mode_config.connector_ida);
2347 idr_destroy(&dev->mode_config.tile_idr);
2348 idr_destroy(&dev->mode_config.crtc_idr);
2349 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
2350 }
2351 EXPORT_SYMBOL(drm_mode_config_cleanup);
2352
2353 struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
2354 unsigned int supported_rotations)
2355 {
2356 static const struct drm_prop_enum_list props[] = {
2357 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
2358 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
2359 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
2360 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
2361 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
2362 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
2363 };
2364
2365 return drm_property_create_bitmask(dev, 0, "rotation",
2366 props, ARRAY_SIZE(props),
2367 supported_rotations);
2368 }
2369 EXPORT_SYMBOL(drm_mode_create_rotation_property);
2370
2371 /**
2372 * DOC: Tile group
2373 *
2374 * Tile groups are used to represent tiled monitors with a unique
2375 * integer identifier. Tiled monitors using DisplayID v1.3 have
2376 * a unique 8-byte handle, we store this in a tile group, so we
2377 * have a common identifier for all tiles in a monitor group.
2378 */
2379 static void drm_tile_group_free(struct kref *kref)
2380 {
2381 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
2382 struct drm_device *dev = tg->dev;
2383 mutex_lock(&dev->mode_config.idr_mutex);
2384 idr_remove(&dev->mode_config.tile_idr, tg->id);
2385 mutex_unlock(&dev->mode_config.idr_mutex);
2386 kfree(tg);
2387 }
2388
2389 /**
2390 * drm_mode_put_tile_group - drop a reference to a tile group.
2391 * @dev: DRM device
2392 * @tg: tile group to drop reference to.
2393 *
2394 * drop reference to tile group and free if 0.
2395 */
2396 void drm_mode_put_tile_group(struct drm_device *dev,
2397 struct drm_tile_group *tg)
2398 {
2399 kref_put(&tg->refcount, drm_tile_group_free);
2400 }
2401
2402 /**
2403 * drm_mode_get_tile_group - get a reference to an existing tile group
2404 * @dev: DRM device
2405 * @topology: 8-bytes unique per monitor.
2406 *
2407 * Use the unique bytes to get a reference to an existing tile group.
2408 *
2409 * RETURNS:
2410 * tile group or NULL if not found.
2411 */
2412 struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
2413 char topology[8])
2414 {
2415 struct drm_tile_group *tg;
2416 int id;
2417 mutex_lock(&dev->mode_config.idr_mutex);
2418 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
2419 if (!memcmp(tg->group_data, topology, 8)) {
2420 if (!kref_get_unless_zero(&tg->refcount))
2421 tg = NULL;
2422 mutex_unlock(&dev->mode_config.idr_mutex);
2423 return tg;
2424 }
2425 }
2426 mutex_unlock(&dev->mode_config.idr_mutex);
2427 return NULL;
2428 }
2429 EXPORT_SYMBOL(drm_mode_get_tile_group);
2430
2431 /**
2432 * drm_mode_create_tile_group - create a tile group from a displayid description
2433 * @dev: DRM device
2434 * @topology: 8-bytes unique per monitor.
2435 *
2436 * Create a tile group for the unique monitor, and get a unique
2437 * identifier for the tile group.
2438 *
2439 * RETURNS:
2440 * new tile group or error.
2441 */
2442 struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
2443 char topology[8])
2444 {
2445 struct drm_tile_group *tg;
2446 int ret;
2447
2448 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
2449 if (!tg)
2450 return ERR_PTR(-ENOMEM);
2451
2452 kref_init(&tg->refcount);
2453 memcpy(tg->group_data, topology, 8);
2454 tg->dev = dev;
2455
2456 mutex_lock(&dev->mode_config.idr_mutex);
2457 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
2458 if (ret >= 0) {
2459 tg->id = ret;
2460 } else {
2461 kfree(tg);
2462 tg = ERR_PTR(ret);
2463 }
2464
2465 mutex_unlock(&dev->mode_config.idr_mutex);
2466 return tg;
2467 }
2468 EXPORT_SYMBOL(drm_mode_create_tile_group);
2469
2470 /**
2471 * drm_crtc_enable_color_mgmt - enable color management properties
2472 * @crtc: DRM CRTC
2473 * @degamma_lut_size: the size of the degamma lut (before CSC)
2474 * @has_ctm: whether to attach ctm_property for CSC matrix
2475 * @gamma_lut_size: the size of the gamma lut (after CSC)
2476 *
2477 * This function lets the driver enable the color correction
2478 * properties on a CRTC. This includes 3 degamma, csc and gamma
2479 * properties that userspace can set and 2 size properties to inform
2480 * the userspace of the lut sizes. Each of the properties are
2481 * optional. The gamma and degamma properties are only attached if
2482 * their size is not 0 and ctm_property is only attached if has_ctm is
2483 * true.
2484 */
2485 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
2486 uint degamma_lut_size,
2487 bool has_ctm,
2488 uint gamma_lut_size)
2489 {
2490 struct drm_device *dev = crtc->dev;
2491 struct drm_mode_config *config = &dev->mode_config;
2492
2493 if (degamma_lut_size) {
2494 drm_object_attach_property(&crtc->base,
2495 config->degamma_lut_property, 0);
2496 drm_object_attach_property(&crtc->base,
2497 config->degamma_lut_size_property,
2498 degamma_lut_size);
2499 }
2500
2501 if (has_ctm)
2502 drm_object_attach_property(&crtc->base,
2503 config->ctm_property, 0);
2504
2505 if (gamma_lut_size) {
2506 drm_object_attach_property(&crtc->base,
2507 config->gamma_lut_property, 0);
2508 drm_object_attach_property(&crtc->base,
2509 config->gamma_lut_size_property,
2510 gamma_lut_size);
2511 }
2512 }
2513 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
This page took 0.091266 seconds and 5 git commands to generate.