Merge remote-tracking branch 'iommu/next'
[deliverable/linux.git] / drivers / gpu / drm / drm_crtc.c
CommitLineData
f453ba04
DA
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 */
6ba6d03e 32#include <linux/ctype.h>
f453ba04 33#include <linux/list.h>
5a0e3ad6 34#include <linux/slab.h>
2d1a8a48 35#include <linux/export.h>
760285e7
DH
36#include <drm/drmP.h>
37#include <drm/drm_crtc.h>
38#include <drm/drm_edid.h>
39#include <drm/drm_fourcc.h>
51fd371b 40#include <drm/drm_modeset_lock.h>
88a48e29 41#include <drm/drm_atomic.h>
3b96a0b1 42#include <drm/drm_auth.h>
7520a277 43#include <drm/drm_framebuffer.h>
f453ba04 44
8bd441b2 45#include "drm_crtc_internal.h"
67d0ec4e 46#include "drm_internal.h"
8bd441b2 47
f453ba04
DA
48/*
49 * Global properties
50 */
4dfd909f 51static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
9922ab5a
RC
52 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
53 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
54 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
55};
56
f453ba04
DA
57/*
58 * Optional properties
59 */
6a0d9528
LW
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 */
67int 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}
75EXPORT_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 */
87int 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 }
99out:
100 drm_modeset_unlock_all(dev);
101 return ret;
102}
103EXPORT_SYMBOL(drm_crtc_force_disable_all);
104
51fd371b
RC
105DEFINE_WW_CLASS(crtc_ww_class);
106
fa3ab4c2
VS
107static 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
79190ea2
BG
119static 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
134static 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
f453ba04 144/**
e13161af
MR
145 * drm_crtc_init_with_planes - Initialise a new CRTC object with
146 * specified primary and cursor planes.
f453ba04
DA
147 * @dev: DRM device
148 * @crtc: CRTC object to init
e13161af
MR
149 * @primary: Primary plane for CRTC
150 * @cursor: Cursor plane for CRTC
f453ba04 151 * @funcs: callbacks for the new CRTC
f9882876 152 * @name: printf style format string for the CRTC name, or NULL for default name
f453ba04 153 *
ad6f5c34 154 * Inits a new object created as base part of a driver crtc object.
6bfc56aa 155 *
c8e32cc1 156 * Returns:
6bfc56aa 157 * Zero on success, error code on failure.
f453ba04 158 */
e13161af
MR
159int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
160 struct drm_plane *primary,
fc1d3e44 161 struct drm_plane *cursor,
f9882876
VS
162 const struct drm_crtc_funcs *funcs,
163 const char *name, ...)
f453ba04 164{
51fd371b 165 struct drm_mode_config *config = &dev->mode_config;
6bfc56aa
VS
166 int ret;
167
522cf91f
BG
168 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
169 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
170
f453ba04
DA
171 crtc->dev = dev;
172 crtc->funcs = funcs;
173
3b24f7d6
DV
174 INIT_LIST_HEAD(&crtc->commit_list);
175 spin_lock_init(&crtc->commit_lock);
176
51fd371b 177 drm_modeset_lock_init(&crtc->mutex);
6bfc56aa
VS
178 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
179 if (ret)
baf698b0 180 return ret;
f453ba04 181
fa3ab4c2
VS
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) {
7c8f6d25 193 drm_mode_object_unregister(dev, &crtc->base);
fa3ab4c2
VS
194 return -ENOMEM;
195 }
196
bffd9de0
PZ
197 crtc->base.properties = &crtc->properties;
198
51fd371b 199 list_add_tail(&crtc->head, &config->crtc_list);
490d3d1b 200 crtc->index = config->num_crtc++;
6bfc56aa 201
e13161af 202 crtc->primary = primary;
fc1d3e44 203 crtc->cursor = cursor;
e13161af
MR
204 if (primary)
205 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
fc1d3e44
MR
206 if (cursor)
207 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
e13161af 208
eab3bbef
DV
209 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
210 drm_object_attach_property(&crtc->base, config->prop_active, 0);
955f3c33 211 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
eab3bbef
DV
212 }
213
baf698b0 214 return 0;
f453ba04 215}
e13161af 216EXPORT_SYMBOL(drm_crtc_init_with_planes);
f453ba04
DA
217
218/**
ad6f5c34 219 * drm_crtc_cleanup - Clean up the core crtc usage
f453ba04
DA
220 * @crtc: CRTC to cleanup
221 *
ad6f5c34
VS
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.
f453ba04
DA
225 */
226void drm_crtc_cleanup(struct drm_crtc *crtc)
227{
228 struct drm_device *dev = crtc->dev;
229
490d3d1b
CW
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
9e1c156f
SK
235 kfree(crtc->gamma_store);
236 crtc->gamma_store = NULL;
f453ba04 237
51fd371b
RC
238 drm_modeset_lock_fini(&crtc->mutex);
239
7c8f6d25 240 drm_mode_object_unregister(dev, &crtc->base);
f453ba04
DA
241 list_del(&crtc->head);
242 dev->mode_config.num_crtc--;
3009c037
TR
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);
a18c0af1 247
fa3ab4c2
VS
248 kfree(crtc->name);
249
a18c0af1 250 memset(crtc, 0, sizeof(*crtc));
f453ba04
DA
251}
252EXPORT_SYMBOL(drm_crtc_cleanup);
253
9f4c97a2 254static unsigned int drm_num_planes(struct drm_device *dev)
eaf99c74 255{
9f4c97a2
VS
256 unsigned int num = 0;
257 struct drm_plane *tmp;
eaf99c74 258
9f4c97a2
VS
259 drm_for_each_plane(tmp, dev) {
260 num++;
eaf99c74
CW
261 }
262
9f4c97a2 263 return num;
b164d31f
DA
264}
265
f453ba04 266/**
dc415ff9 267 * drm_universal_plane_init - Initialize a new universal plane object
f453ba04 268 * @dev: DRM device
35f2c3ae
VS
269 * @plane: plane object to init
270 * @possible_crtcs: bitmask of possible CRTCs
271 * @funcs: callbacks for the new plane
62cacc79 272 * @formats: array of supported formats (DRM_FORMAT\_\*)
35f2c3ae 273 * @format_count: number of elements in @formats
dc415ff9 274 * @type: type of plane (overlay, primary, cursor)
b0b3b795 275 * @name: printf style format string for the plane name, or NULL for default name
f453ba04 276 *
dc415ff9 277 * Initializes a plane object of type @type.
6bfc56aa 278 *
c8e32cc1 279 * Returns:
6bfc56aa 280 * Zero on success, error code on failure.
f453ba04 281 */
dc415ff9
MR
282int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
283 unsigned long possible_crtcs,
284 const struct drm_plane_funcs *funcs,
45e3743a 285 const uint32_t *formats, unsigned int format_count,
b0b3b795
VS
286 enum drm_plane_type type,
287 const char *name, ...)
8cf5c917 288{
6b4959f4 289 struct drm_mode_config *config = &dev->mode_config;
6bfc56aa
VS
290 int ret;
291
6bfc56aa
VS
292 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
293 if (ret)
baf698b0 294 return ret;
6bfc56aa 295
4d02e2de
DV
296 drm_modeset_lock_init(&plane->mutex);
297
4d93914a 298 plane->base.properties = &plane->properties;
8cf5c917 299 plane->dev = dev;
8cf5c917 300 plane->funcs = funcs;
2f6c5389
TR
301 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
302 GFP_KERNEL);
8cf5c917
JB
303 if (!plane->format_types) {
304 DRM_DEBUG_KMS("out of memory when allocating plane\n");
7c8f6d25 305 drm_mode_object_unregister(dev, &plane->base);
baf698b0 306 return -ENOMEM;
8cf5c917
JB
307 }
308
9f4c97a2
VS
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);
7c8f6d25 321 drm_mode_object_unregister(dev, &plane->base);
9f4c97a2
VS
322 return -ENOMEM;
323 }
324
308e5bcb 325 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
8cf5c917
JB
326 plane->format_count = format_count;
327 plane->possible_crtcs = possible_crtcs;
dc415ff9 328 plane->type = type;
8cf5c917 329
6b4959f4 330 list_add_tail(&plane->head, &config->plane_list);
490d3d1b 331 plane->index = config->num_total_plane++;
dc415ff9 332 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
6b4959f4 333 config->num_overlay_plane++;
8cf5c917 334
9922ab5a 335 drm_object_attach_property(&plane->base,
6b4959f4 336 config->plane_type_property,
9922ab5a
RC
337 plane->type);
338
6b4959f4
RC
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
baf698b0 352 return 0;
8cf5c917 353}
dc415ff9
MR
354EXPORT_SYMBOL(drm_universal_plane_init);
355
79190ea2
BG
356static 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
371static 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
dc415ff9
MR
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
62cacc79 387 * @formats: array of supported formats (DRM_FORMAT\_\*)
dc415ff9
MR
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 */
398int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
399 unsigned long possible_crtcs,
400 const struct drm_plane_funcs *funcs,
45e3743a 401 const uint32_t *formats, unsigned int format_count,
dc415ff9
MR
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,
b0b3b795 408 formats, format_count, type, NULL);
dc415ff9 409}
8cf5c917
JB
410EXPORT_SYMBOL(drm_plane_init);
411
35f2c3ae
VS
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 */
8cf5c917
JB
420void drm_plane_cleanup(struct drm_plane *plane)
421{
422 struct drm_device *dev = plane->dev;
423
84849903 424 drm_modeset_lock_all(dev);
8cf5c917 425 kfree(plane->format_types);
7c8f6d25 426 drm_mode_object_unregister(dev, &plane->base);
dc415ff9
MR
427
428 BUG_ON(list_empty(&plane->head));
429
490d3d1b
CW
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
dc415ff9
MR
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--;
84849903 439 drm_modeset_unlock_all(dev);
3009c037
TR
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);
a18c0af1 444
9f4c97a2
VS
445 kfree(plane->name);
446
a18c0af1 447 memset(plane, 0, sizeof(*plane));
8cf5c917
JB
448}
449EXPORT_SYMBOL(drm_plane_cleanup);
450
f81338a5
CK
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 */
459struct drm_plane *
460drm_plane_from_index(struct drm_device *dev, int idx)
461{
462 struct drm_plane *plane;
f81338a5 463
490d3d1b
CW
464 drm_for_each_plane(plane, dev)
465 if (idx == plane->index)
f81338a5 466 return plane;
490d3d1b 467
f81338a5
CK
468 return NULL;
469}
470EXPORT_SYMBOL(drm_plane_from_index);
471
35f2c3ae
VS
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 */
9125e618
VS
481void drm_plane_force_disable(struct drm_plane *plane)
482{
483 int ret;
484
3d30a59b 485 if (!plane->fb)
9125e618
VS
486 return;
487
3d30a59b 488 plane->old_fb = plane->fb;
9125e618 489 ret = plane->funcs->disable_plane(plane);
731cce48 490 if (ret) {
9125e618 491 DRM_ERROR("failed to disable plane with busy fb\n");
3d30a59b 492 plane->old_fb = NULL;
731cce48
DV
493 return;
494 }
9125e618 495 /* disconnect the plane from the fb and crtc: */
220dd2bc 496 drm_framebuffer_unreference(plane->old_fb);
3d30a59b 497 plane->old_fb = NULL;
9125e618
VS
498 plane->fb = NULL;
499 plane->crtc = NULL;
500}
501EXPORT_SYMBOL(drm_plane_force_disable);
502
79190ea2
BG
503int 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
525err_connector:
526 drm_encoder_unregister_all(dev);
527err_encoder:
528 drm_crtc_unregister_all(dev);
529err_crtc:
530 drm_plane_unregister_all(dev);
531err_plane:
532 return ret;
533}
534
535void 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
6b4959f4 543static int drm_mode_create_standard_properties(struct drm_device *dev)
f453ba04 544{
356af0e1 545 struct drm_property *prop;
52217195 546 int ret;
f453ba04 547
52217195
DV
548 ret = drm_connector_create_standard_properties(dev);
549 if (ret)
550 return ret;
9922ab5a 551
6b4959f4 552 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
9922ab5a
RC
553 "type", drm_plane_type_enum_list,
554 ARRAY_SIZE(drm_plane_type_enum_list));
6b4959f4
RC
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;
9922ab5a 618
eab3bbef
DV
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
955f3c33
DS
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
5488dc16
LL
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
9922ab5a
RC
667 return 0;
668}
669
f453ba04 670/**
f453ba04 671 * drm_mode_getresources - get graphics configuration
065a50ed
DV
672 * @dev: drm device for the ioctl
673 * @data: data pointer for the ioctl
674 * @file_priv: drm file for the ioctl call
f453ba04 675 *
f453ba04
DA
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.
f453ba04 680 *
c8e32cc1 681 * Returns:
1a498633 682 * Zero on success, negative errno on failure.
f453ba04 683 */
f453ba04
DA
684int drm_mode_getresources(struct drm_device *dev, void *data,
685 struct drm_file *file_priv)
f453ba04 686{
f453ba04
DA
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;
9c7060f7 698 int copied = 0;
f453ba04
DA
699 uint32_t __user *fb_id;
700 uint32_t __user *crtc_id;
701 uint32_t __user *connector_id;
702 uint32_t __user *encoder_id;
f453ba04 703
fb3b06c8
DA
704 if (!drm_core_check_feature(dev, DRIVER_MODESET))
705 return -EINVAL;
f453ba04 706
f453ba04 707
4b096ac1 708 mutex_lock(&file_priv->fbs_lock);
f453ba04
DA
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
4b096ac1
DV
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
fcf93f69
DV
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);
9c7060f7
DV
735 drm_for_each_crtc(crtc, dev)
736 crtc_count++;
f453ba04 737
9c7060f7
DV
738 drm_for_each_connector(connector, dev)
739 connector_count++;
f453ba04 740
9c7060f7
DV
741 drm_for_each_encoder(encoder, dev)
742 encoder_count++;
f453ba04
DA
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
f453ba04
DA
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;
9c7060f7 753 drm_for_each_crtc(crtc, dev) {
9c7060f7
DV
754 if (put_user(crtc->base.id, crtc_id + copied)) {
755 ret = -EFAULT;
756 goto out;
f453ba04 757 }
9c7060f7 758 copied++;
f453ba04
DA
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;
9c7060f7 767 drm_for_each_encoder(encoder, dev) {
9c7060f7
DV
768 if (put_user(encoder->base.id, encoder_id +
769 copied)) {
770 ret = -EFAULT;
771 goto out;
f453ba04 772 }
9c7060f7 773 copied++;
f453ba04
DA
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;
9c7060f7 782 drm_for_each_connector(connector, dev) {
9c7060f7
DV
783 if (put_user(connector->base.id,
784 connector_id + copied)) {
785 ret = -EFAULT;
786 goto out;
f453ba04 787 }
9c7060f7 788 copied++;
f453ba04
DA
789 }
790 }
791 card_res->count_connectors = connector_count;
792
f453ba04 793out:
fcf93f69 794 mutex_unlock(&dev->mode_config.mutex);
f453ba04
DA
795 return ret;
796}
797
798/**
799 * drm_mode_getcrtc - get CRTC configuration
065a50ed
DV
800 * @dev: drm device for the ioctl
801 * @data: data pointer for the ioctl
802 * @file_priv: drm file for the ioctl call
f453ba04 803 *
f453ba04
DA
804 * Construct a CRTC configuration structure to return to the user.
805 *
806 * Called by the user via ioctl.
807 *
c8e32cc1 808 * Returns:
1a498633 809 * Zero on success, negative errno on failure.
f453ba04
DA
810 */
811int 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;
f453ba04 816
fb3b06c8
DA
817 if (!drm_core_check_feature(dev, DRIVER_MODESET))
818 return -EINVAL;
819
a2b34e22 820 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
fcf93f69
DV
821 if (!crtc)
822 return -ENOENT;
f453ba04 823
fcf93f69 824 drm_modeset_lock_crtc(crtc, crtc->primary);
f453ba04 825 crtc_resp->gamma_size = crtc->gamma_size;
f4510a27
MR
826 if (crtc->primary->fb)
827 crtc_resp->fb_id = crtc->primary->fb->base.id;
f453ba04
DA
828 else
829 crtc_resp->fb_id = 0;
830
31c946e8
DV
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) {
934a8a89 835 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
31c946e8 836 crtc_resp->mode_valid = 1;
f453ba04 837
31c946e8
DV
838 } else {
839 crtc_resp->mode_valid = 0;
840 }
f453ba04 841 } else {
31c946e8
DV
842 crtc_resp->x = crtc->x;
843 crtc_resp->y = crtc->y;
844 if (crtc->enabled) {
934a8a89 845 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
31c946e8
DV
846 crtc_resp->mode_valid = 1;
847
848 } else {
849 crtc_resp->mode_valid = 0;
850 }
f453ba04 851 }
fcf93f69 852 drm_modeset_unlock_crtc(crtc);
f453ba04 853
baf698b0 854 return 0;
f453ba04
DA
855}
856
8cf5c917 857/**
c8e32cc1 858 * drm_mode_getplane_res - enumerate all plane resources
8cf5c917
JB
859 * @dev: DRM device
860 * @data: ioctl data
861 * @file_priv: DRM file info
862 *
c8e32cc1
DV
863 * Construct a list of plane ids to return to the user.
864 *
865 * Called by the user via ioctl.
866 *
867 * Returns:
1a498633 868 * Zero on success, negative errno on failure.
8cf5c917
JB
869 */
870int drm_mode_getplane_res(struct drm_device *dev, void *data,
c8e32cc1 871 struct drm_file *file_priv)
8cf5c917
JB
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;
fcf93f69 877 int copied = 0;
681e7ec7 878 unsigned num_planes;
8cf5c917
JB
879
880 if (!drm_core_check_feature(dev, DRIVER_MODESET))
881 return -EINVAL;
882
8cf5c917
JB
883 config = &dev->mode_config;
884
681e7ec7
MR
885 if (file_priv->universal_planes)
886 num_planes = config->num_total_plane;
887 else
888 num_planes = config->num_overlay_plane;
889
8cf5c917
JB
890 /*
891 * This ioctl is called twice, once to determine how much space is
892 * needed, and the 2nd time to fill it.
893 */
681e7ec7
MR
894 if (num_planes &&
895 (plane_resp->count_planes >= num_planes)) {
81f6c7f8 896 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
8cf5c917 897
fcf93f69 898 /* Plane lists are invariant, no locking needed. */
e4f62546 899 drm_for_each_plane(plane, dev) {
681e7ec7
MR
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)
e27dde3e
MR
906 continue;
907
fcf93f69
DV
908 if (put_user(plane->base.id, plane_ptr + copied))
909 return -EFAULT;
8cf5c917
JB
910 copied++;
911 }
912 }
681e7ec7 913 plane_resp->count_planes = num_planes;
8cf5c917 914
fcf93f69 915 return 0;
8cf5c917
JB
916}
917
918/**
c8e32cc1 919 * drm_mode_getplane - get plane configuration
8cf5c917
JB
920 * @dev: DRM device
921 * @data: ioctl data
922 * @file_priv: DRM file info
923 *
c8e32cc1
DV
924 * Construct a plane configuration structure to return to the user.
925 *
926 * Called by the user via ioctl.
927 *
928 * Returns:
1a498633 929 * Zero on success, negative errno on failure.
8cf5c917
JB
930 */
931int drm_mode_getplane(struct drm_device *dev, void *data,
c8e32cc1 932 struct drm_file *file_priv)
8cf5c917
JB
933{
934 struct drm_mode_get_plane *plane_resp = data;
8cf5c917
JB
935 struct drm_plane *plane;
936 uint32_t __user *format_ptr;
8cf5c917
JB
937
938 if (!drm_core_check_feature(dev, DRIVER_MODESET))
939 return -EINVAL;
940
a2b34e22 941 plane = drm_plane_find(dev, plane_resp->plane_id);
fcf93f69
DV
942 if (!plane)
943 return -ENOENT;
8cf5c917 944
fcf93f69 945 drm_modeset_lock(&plane->mutex, NULL);
8cf5c917
JB
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;
fcf93f69 955 drm_modeset_unlock(&plane->mutex);
8cf5c917
JB
956
957 plane_resp->plane_id = plane->base.id;
958 plane_resp->possible_crtcs = plane->possible_crtcs;
778ad903 959 plane_resp->gamma_size = 0;
8cf5c917
JB
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)) {
81f6c7f8 967 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
8cf5c917
JB
968 if (copy_to_user(format_ptr,
969 plane->format_types,
970 sizeof(uint32_t) * plane->format_count)) {
fcf93f69 971 return -EFAULT;
8cf5c917
JB
972 }
973 }
974 plane_resp->count_format_types = plane->format_count;
975
baf698b0 976 return 0;
8cf5c917
JB
977}
978
ead8610d
LP
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 */
988int 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
ce8d9ecc
VS
1000static 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
b36552b3
MR
1026/*
1027 * setplane_internal - setplane handler for internal callers
8cf5c917 1028 *
b36552b3
MR
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.
c8e32cc1 1032 *
b36552b3 1033 * src_{x,y,w,h} are provided in 16.16 fixed point format
8cf5c917 1034 */
f2b50c11
DV
1035static 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)
8cf5c917 1043{
8cf5c917
JB
1044 int ret = 0;
1045
8cf5c917 1046 /* No fb means shut it down */
b36552b3 1047 if (!fb) {
3d30a59b 1048 plane->old_fb = plane->fb;
731cce48
DV
1049 ret = plane->funcs->disable_plane(plane);
1050 if (!ret) {
1051 plane->crtc = NULL;
1052 plane->fb = NULL;
1053 } else {
3d30a59b 1054 plane->old_fb = NULL;
731cce48 1055 }
8cf5c917
JB
1056 goto out;
1057 }
1058
7f994f3f
MR
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
62443be6 1066 /* Check whether this plane supports the fb pixel format. */
ead8610d
LP
1067 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
1068 if (ret) {
d3828147 1069 char *format_name = drm_get_format_name(fb->pixel_format);
90844f00
EE
1070 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1071 kfree(format_name);
62443be6
VS
1072 goto out;
1073 }
1074
3968be94
MR
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);
c390eed0
VS
1082 ret = -ERANGE;
1083 goto out;
3968be94
MR
1084 }
1085
ce8d9ecc
VS
1086 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
1087 if (ret)
42ef8789 1088 goto out;
42ef8789 1089
3d30a59b 1090 plane->old_fb = plane->fb;
8cf5c917 1091 ret = plane->funcs->update_plane(plane, crtc, fb,
b36552b3
MR
1092 crtc_x, crtc_y, crtc_w, crtc_h,
1093 src_x, src_y, src_w, src_h);
8cf5c917
JB
1094 if (!ret) {
1095 plane->crtc = crtc;
1096 plane->fb = fb;
35f8badc 1097 fb = NULL;
0fe27f06 1098 } else {
3d30a59b 1099 plane->old_fb = NULL;
8cf5c917
JB
1100 }
1101
1102out:
6c2a7532
DV
1103 if (fb)
1104 drm_framebuffer_unreference(fb);
3d30a59b
DV
1105 if (plane->old_fb)
1106 drm_framebuffer_unreference(plane->old_fb);
1107 plane->old_fb = NULL;
8cf5c917
JB
1108
1109 return ret;
f2b50c11 1110}
b36552b3 1111
f2b50c11
DV
1112static 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;
b36552b3
MR
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:
1a498633 1143 * Zero on success, negative errno on failure.
b36552b3
MR
1144 */
1145int 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;
b36552b3
MR
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
b36552b3
MR
1156 /*
1157 * First, find the plane, crtc, and fb objects. If not available,
1158 * we don't bother to call the driver.
1159 */
933f622f
RC
1160 plane = drm_plane_find(dev, plane_req->plane_id);
1161 if (!plane) {
b36552b3
MR
1162 DRM_DEBUG_KMS("Unknown plane ID %d\n",
1163 plane_req->plane_id);
1164 return -ENOENT;
1165 }
b36552b3
MR
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
933f622f
RC
1175 crtc = drm_crtc_find(dev, plane_req->crtc_id);
1176 if (!crtc) {
b36552b3
MR
1177 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
1178 plane_req->crtc_id);
1179 return -ENOENT;
1180 }
b36552b3
MR
1181 }
1182
161d0dc1
MR
1183 /*
1184 * setplane_internal will take care of deref'ing either the old or new
1185 * framebuffer depending on success.
1186 */
17cfd91f 1187 return setplane_internal(plane, crtc, fb,
b36552b3
MR
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);
8cf5c917
JB
1192}
1193
2d13b679
DV
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.
4dfd909f 1200 *
c8e32cc1 1201 * Returns:
1a498633 1202 * Zero on success, negative errno on failure.
2d13b679
DV
1203 */
1204int drm_mode_set_config_internal(struct drm_mode_set *set)
1205{
1206 struct drm_crtc *crtc = set->crtc;
5cef29aa
DV
1207 struct drm_framebuffer *fb;
1208 struct drm_crtc *tmp;
b0d12325
DV
1209 int ret;
1210
5cef29aa
DV
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 */
e4f62546 1216 drm_for_each_crtc(tmp, crtc->dev)
3d30a59b 1217 tmp->primary->old_fb = tmp->primary->fb;
5cef29aa 1218
b0d12325 1219 fb = set->fb;
2d13b679 1220
b0d12325
DV
1221 ret = crtc->funcs->set_config(set);
1222 if (ret == 0) {
e13161af 1223 crtc->primary->crtc = crtc;
0fe27f06 1224 crtc->primary->fb = fb;
5cef29aa 1225 }
cc85e121 1226
e4f62546 1227 drm_for_each_crtc(tmp, crtc->dev) {
f4510a27
MR
1228 if (tmp->primary->fb)
1229 drm_framebuffer_reference(tmp->primary->fb);
3d30a59b
DV
1230 if (tmp->primary->old_fb)
1231 drm_framebuffer_unreference(tmp->primary->old_fb);
1232 tmp->primary->old_fb = NULL;
b0d12325
DV
1233 }
1234
1235 return ret;
2d13b679
DV
1236}
1237EXPORT_SYMBOL(drm_mode_set_config_internal);
1238
ecb7e16b
GP
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 */
1248void 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}
1258EXPORT_SYMBOL(drm_crtc_get_hv_timing);
1259
af93629d
MR
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
c11e9283 1268 */
af93629d
MR
1269int 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)
c11e9283
DL
1273
1274{
1275 int hdisplay, vdisplay;
1276
ecb7e16b 1277 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
a0c1bbb0 1278
33e0be63 1279 if (crtc->state &&
31ad61e4
JL
1280 crtc->primary->state->rotation & (DRM_ROTATE_90 |
1281 DRM_ROTATE_270))
c11e9283
DL
1282 swap(hdisplay, vdisplay);
1283
ce8d9ecc
VS
1284 return check_src_coords(x << 16, y << 16,
1285 hdisplay << 16, vdisplay << 16, fb);
c11e9283 1286}
af93629d 1287EXPORT_SYMBOL(drm_crtc_check_viewport);
c11e9283 1288
f453ba04
DA
1289/**
1290 * drm_mode_setcrtc - set CRTC configuration
065a50ed
DV
1291 * @dev: drm device for the ioctl
1292 * @data: data pointer for the ioctl
1293 * @file_priv: drm file for the ioctl call
f453ba04 1294 *
f453ba04
DA
1295 * Build a new CRTC configuration based on user request.
1296 *
1297 * Called by the user via ioctl.
1298 *
c8e32cc1 1299 * Returns:
1a498633 1300 * Zero on success, negative errno on failure.
f453ba04
DA
1301 */
1302int 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;
6653cc8d 1307 struct drm_crtc *crtc;
f453ba04
DA
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;
4a1b0714 1313 int ret;
f453ba04
DA
1314 int i;
1315
fb3b06c8
DA
1316 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1317 return -EINVAL;
1318
01447e9f
ZJ
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)
1d97e915
VS
1324 return -ERANGE;
1325
84849903 1326 drm_modeset_lock_all(dev);
a2b34e22
RC
1327 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
1328 if (!crtc) {
58367ed6 1329 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
f27657f2 1330 ret = -ENOENT;
f453ba04
DA
1331 goto out;
1332 }
fa3ab4c2 1333 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
f453ba04
DA
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) {
f4510a27 1339 if (!crtc->primary->fb) {
6653cc8d
VS
1340 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
1341 ret = -EINVAL;
1342 goto out;
f453ba04 1343 }
f4510a27 1344 fb = crtc->primary->fb;
b0d12325
DV
1345 /* Make refcounting symmetric with the lookup path. */
1346 drm_framebuffer_reference(fb);
f453ba04 1347 } else {
786b99ed
DV
1348 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
1349 if (!fb) {
58367ed6
ZY
1350 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1351 crtc_req->fb_id);
37c4e705 1352 ret = -ENOENT;
f453ba04
DA
1353 goto out;
1354 }
f453ba04
DA
1355 }
1356
1357 mode = drm_mode_create(dev);
ee34ab5b
VS
1358 if (!mode) {
1359 ret = -ENOMEM;
1360 goto out;
1361 }
1362
934a8a89 1363 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
90367bf6
VS
1364 if (ret) {
1365 DRM_DEBUG_KMS("Invalid mode\n");
1366 goto out;
1367 }
1368
7eb5f302
LP
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) {
d3828147 1380 char *format_name = drm_get_format_name(fb->pixel_format);
90844f00
EE
1381 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
1382 kfree(format_name);
7eb5f302
LP
1383 goto out;
1384 }
1385 }
1386
c11e9283
DL
1387 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
1388 mode, fb);
1389 if (ret)
5f61bb42 1390 goto out;
c11e9283 1391
f453ba04
DA
1392 }
1393
1394 if (crtc_req->count_connectors == 0 && mode) {
58367ed6 1395 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
f453ba04
DA
1396 ret = -EINVAL;
1397 goto out;
1398 }
1399
7781de74 1400 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
58367ed6 1401 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
f453ba04
DA
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
2f6c5389
TR
1416 connector_set = kmalloc_array(crtc_req->count_connectors,
1417 sizeof(struct drm_connector *),
1418 GFP_KERNEL);
f453ba04
DA
1419 if (!connector_set) {
1420 ret = -ENOMEM;
1421 goto out;
1422 }
1423
1424 for (i = 0; i < crtc_req->count_connectors; i++) {
b164d31f 1425 connector_set[i] = NULL;
81f6c7f8 1426 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
f453ba04
DA
1427 if (get_user(out_id, &set_connectors_ptr[i])) {
1428 ret = -EFAULT;
1429 goto out;
1430 }
1431
b164d31f 1432 connector = drm_connector_lookup(dev, out_id);
a2b34e22 1433 if (!connector) {
58367ed6
ZY
1434 DRM_DEBUG_KMS("Connector id %d unknown\n",
1435 out_id);
f27657f2 1436 ret = -ENOENT;
f453ba04
DA
1437 goto out;
1438 }
9440106b
JG
1439 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1440 connector->base.id,
25933820 1441 connector->name);
f453ba04
DA
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;
5ef5f72f 1453 set.fb = fb;
2d13b679 1454 ret = drm_mode_set_config_internal(&set);
f453ba04
DA
1455
1456out:
b0d12325
DV
1457 if (fb)
1458 drm_framebuffer_unreference(fb);
1459
b164d31f
DA
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 }
f453ba04 1466 kfree(connector_set);
ee34ab5b 1467 drm_mode_destroy(dev, mode);
84849903 1468 drm_modeset_unlock_all(dev);
f453ba04
DA
1469 return ret;
1470}
1471
161d0dc1
MR
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:
1a498633 1489 * Zero on success, negative errno on failure.
161d0dc1
MR
1490 */
1491static 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);
f2b50c11 1510 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
161d0dc1
MR
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) {
7520a277 1519 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
161d0dc1
MR
1520 if (IS_ERR(fb)) {
1521 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
1522 return PTR_ERR(fb);
1523 }
dd546591
GH
1524 fb->hot_x = req->hot_x;
1525 fb->hot_y = req->hot_y;
161d0dc1
MR
1526 } else {
1527 fb = NULL;
1528 }
1529 } else {
161d0dc1
MR
1530 fb = crtc->cursor->fb;
1531 if (fb)
1532 drm_framebuffer_reference(fb);
161d0dc1
MR
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 */
f2b50c11 1554 ret = __setplane_internal(crtc->cursor, crtc, fb,
161d0dc1
MR
1555 crtc_x, crtc_y, crtc_w, crtc_h,
1556 0, 0, src_w, src_h);
e2f5d2ea 1557
161d0dc1
MR
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;
e2f5d2ea
DS
1562 }
1563
e2f5d2ea
DS
1564 return ret;
1565}
1566
4c813d4d
DA
1567static int drm_mode_cursor_common(struct drm_device *dev,
1568 struct drm_mode_cursor2 *req,
1569 struct drm_file *file_priv)
e2f5d2ea 1570{
f453ba04 1571 struct drm_crtc *crtc;
e2f5d2ea
DS
1572 int ret = 0;
1573
1574 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1575 return -EINVAL;
1576
7c4eaca4 1577 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
f453ba04 1578 return -EINVAL;
e2f5d2ea 1579
a2b34e22
RC
1580 crtc = drm_crtc_find(dev, req->crtc_id);
1581 if (!crtc) {
58367ed6 1582 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
f27657f2 1583 return -ENOENT;
e2f5d2ea
DS
1584 }
1585
161d0dc1
MR
1586 /*
1587 * If this crtc has a universal cursor plane, call that plane's update
1588 * handler rather than using legacy cursor handlers.
1589 */
4d02e2de 1590 drm_modeset_lock_crtc(crtc, crtc->cursor);
f2b50c11
DV
1591 if (crtc->cursor) {
1592 ret = drm_mode_cursor_universal(crtc, req, file_priv);
1593 goto out;
e2f5d2ea
DS
1594 }
1595
f453ba04 1596 if (req->flags & DRM_MODE_CURSOR_BO) {
4c813d4d 1597 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
f453ba04
DA
1598 ret = -ENXIO;
1599 goto out;
1600 }
1601 /* Turns off the cursor if handle is 0 */
4c813d4d
DA
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);
f453ba04 1608 }
e2f5d2ea 1609
f453ba04
DA
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 {
f453ba04
DA
1614 ret = -EFAULT;
1615 goto out;
1616 }
1617 }
1618out:
d059f652 1619 drm_modeset_unlock_crtc(crtc);
152ef5fa 1620
e2f5d2ea 1621 return ret;
43aba7eb 1622
43aba7eb 1623}
43aba7eb 1624
6f134d7b 1625
c8e32cc1 1626/**
c8e32cc1
DV
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.
c8e32cc1 1633 *
c8e32cc1 1634 * Called by the user via ioctl.
c8e32cc1
DV
1635 *
1636 * Returns:
1a498633 1637 * Zero on success, negative errno on failure.
c8e32cc1 1638 */
4c813d4d 1639int drm_mode_cursor_ioctl(struct drm_device *dev,
c8e32cc1 1640 void *data, struct drm_file *file_priv)
26a34815 1641{
4c813d4d
DA
1642 struct drm_mode_cursor *req = data;
1643 struct drm_mode_cursor2 new_req;
26a34815 1644
4c813d4d
DA
1645 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
1646 new_req.hot_x = new_req.hot_y = 0;
3843e71f 1647
4c813d4d 1648 return drm_mode_cursor_common(dev, &new_req, file_priv);
3843e71f
RC
1649}
1650
c8e32cc1 1651/**
c8e32cc1
DV
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
c8e32cc1 1656 *
c8e32cc1
DV
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.
c8e32cc1
DV
1660 *
1661 * Called by the user via ioctl.
1662 *
1663 * Returns:
1a498633 1664 * Zero on success, negative errno on failure.
c8e32cc1 1665 */
4c813d4d
DA
1666int drm_mode_cursor2_ioctl(struct drm_device *dev,
1667 void *data, struct drm_file *file_priv)
c543188a 1668{
4c813d4d 1669 struct drm_mode_cursor2 *req = data;
c543188a 1670
4c813d4d 1671 return drm_mode_cursor_common(dev, req, file_priv);
c543188a
PZ
1672}
1673
949619f3
DV
1674int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
1675 struct drm_property *property,
1676 uint64_t value)
bffd9de0
PZ
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
3a5f87c2
TW
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 */
1702int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
1703 struct drm_property *property,
1704 uint64_t value)
4d93914a
RC
1705{
1706 int ret = -EINVAL;
3a5f87c2 1707 struct drm_mode_object *obj = &plane->base;
4d93914a
RC
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}
3a5f87c2 1716EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
4d93914a 1717
c8e32cc1
DV
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:
1a498633 1728 * Zero on success, negative errno on failure.
c8e32cc1 1729 */
4cae5b84 1730int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
c8e32cc1 1731 int gamma_size)
f453ba04 1732{
cf48e292
DV
1733 uint16_t *r_base, *g_base, *b_base;
1734 int i;
1735
f453ba04
DA
1736 crtc->gamma_size = gamma_size;
1737
bd3f0ff9
TR
1738 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
1739 GFP_KERNEL);
f453ba04
DA
1740 if (!crtc->gamma_store) {
1741 crtc->gamma_size = 0;
4cae5b84 1742 return -ENOMEM;
f453ba04
DA
1743 }
1744
cf48e292
DV
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
4cae5b84 1755 return 0;
f453ba04
DA
1756}
1757EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
1758
c8e32cc1
DV
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:
1a498633 1771 * Zero on success, negative errno on failure.
c8e32cc1 1772 */
f453ba04
DA
1773int 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;
f453ba04
DA
1777 struct drm_crtc *crtc;
1778 void *r_base, *g_base, *b_base;
1779 int size;
1780 int ret = 0;
1781
fb3b06c8
DA
1782 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1783 return -EINVAL;
1784
84849903 1785 drm_modeset_lock_all(dev);
a2b34e22
RC
1786 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1787 if (!crtc) {
f27657f2 1788 ret = -ENOENT;
f453ba04
DA
1789 goto out;
1790 }
f453ba04 1791
ebe0f244
LP
1792 if (crtc->funcs->gamma_set == NULL) {
1793 ret = -ENOSYS;
1794 goto out;
1795 }
1796
f453ba04
DA
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
7ea77283 1822 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
f453ba04
DA
1823
1824out:
84849903 1825 drm_modeset_unlock_all(dev);
f453ba04
DA
1826 return ret;
1827
1828}
1829
c8e32cc1
DV
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:
1a498633 1843 * Zero on success, negative errno on failure.
c8e32cc1 1844 */
f453ba04
DA
1845int 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;
f453ba04
DA
1849 struct drm_crtc *crtc;
1850 void *r_base, *g_base, *b_base;
1851 int size;
1852 int ret = 0;
1853
fb3b06c8
DA
1854 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1855 return -EINVAL;
1856
84849903 1857 drm_modeset_lock_all(dev);
a2b34e22
RC
1858 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1859 if (!crtc) {
f27657f2 1860 ret = -ENOENT;
f453ba04
DA
1861 goto out;
1862 }
f453ba04
DA
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 }
1888out:
84849903 1889 drm_modeset_unlock_all(dev);
f453ba04
DA
1890 return ret;
1891}
d91d8a3f 1892
c8e32cc1
DV
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:
1a498633 1909 * Zero on success, negative errno on failure.
c8e32cc1 1910 */
d91d8a3f
KH
1911int drm_mode_page_flip_ioctl(struct drm_device *dev,
1912 void *data, struct drm_file *file_priv)
1913{
f837297a 1914 struct drm_mode_crtc_page_flip_target *page_flip = data;
d91d8a3f 1915 struct drm_crtc *crtc;
3d30a59b 1916 struct drm_framebuffer *fb = NULL;
d91d8a3f 1917 struct drm_pending_vblank_event *e = NULL;
f837297a 1918 u32 target_vblank = page_flip->sequence;
d91d8a3f
KH
1919 int ret = -EINVAL;
1920
6f00975c
DV
1921 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1922 return -EINVAL;
1923
f837297a
MD
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)
d91d8a3f
KH
1934 return -EINVAL;
1935
62f2104f
KP
1936 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
1937 return -EINVAL;
1938
a2b34e22
RC
1939 crtc = drm_crtc_find(dev, page_flip->crtc_id);
1940 if (!crtc)
f27657f2 1941 return -ENOENT;
d91d8a3f 1942
c229bfbb 1943 if (crtc->funcs->page_flip_target) {
f837297a 1944 u32 current_vblank;
c229bfbb
MD
1945 int r;
1946
1947 r = drm_crtc_vblank_get(crtc);
1948 if (r)
1949 return r;
1950
f837297a
MD
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)) {
c229bfbb
MD
1979 return -EINVAL;
1980 }
1981
4d02e2de 1982 drm_modeset_lock_crtc(crtc, crtc->primary);
f4510a27 1983 if (crtc->primary->fb == NULL) {
90c1efdd
CW
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
786b99ed 1992 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
37c4e705
VS
1993 if (!fb) {
1994 ret = -ENOENT;
d91d8a3f 1995 goto out;
37c4e705 1996 }
d91d8a3f 1997
2afa701d
VS
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 }
c11e9283 2006 if (ret)
5f61bb42 2007 goto out;
5f61bb42 2008
f4510a27 2009 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
909d9cda
LP
2010 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
2011 ret = -EINVAL;
2012 goto out;
2013 }
2014
d91d8a3f 2015 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2dd500f1
DV
2016 e = kzalloc(sizeof *e, GFP_KERNEL);
2017 if (!e) {
2018 ret = -ENOMEM;
d91d8a3f
KH
2019 goto out;
2020 }
7bd4d7be 2021 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
f76511b9 2022 e->event.base.length = sizeof(e->event);
d91d8a3f 2023 e->event.user_data = page_flip->user_data;
2dd500f1
DV
2024 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
2025 if (ret) {
2026 kfree(e);
2027 goto out;
2028 }
d91d8a3f
KH
2029 }
2030
3d30a59b 2031 crtc->primary->old_fb = crtc->primary->fb;
c229bfbb
MD
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);
d91d8a3f 2038 if (ret) {
2dd500f1
DV
2039 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
2040 drm_event_cancel_free(dev, &e->base);
b0d12325 2041 /* Keep the old fb, don't unref it. */
3d30a59b 2042 crtc->primary->old_fb = NULL;
b0d12325 2043 } else {
3cb43cc0 2044 crtc->primary->fb = fb;
b0d12325
DV
2045 /* Unref only the old framebuffer. */
2046 fb = NULL;
d91d8a3f
KH
2047 }
2048
2049out:
dec90ea1 2050 if (ret && crtc->funcs->page_flip_target)
c229bfbb 2051 drm_crtc_vblank_put(crtc);
b0d12325
DV
2052 if (fb)
2053 drm_framebuffer_unreference(fb);
3d30a59b
DV
2054 if (crtc->primary->old_fb)
2055 drm_framebuffer_unreference(crtc->primary->old_fb);
2056 crtc->primary->old_fb = NULL;
d059f652 2057 drm_modeset_unlock_crtc(crtc);
b4d5e7d1 2058
d91d8a3f
KH
2059 return ret;
2060}
eb033556 2061
c8e32cc1
DV
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 */
eb033556
CW
2070void drm_mode_config_reset(struct drm_device *dev)
2071{
2072 struct drm_crtc *crtc;
2a0d7cfd 2073 struct drm_plane *plane;
eb033556
CW
2074 struct drm_encoder *encoder;
2075 struct drm_connector *connector;
2076
e4f62546 2077 drm_for_each_plane(plane, dev)
2a0d7cfd
DV
2078 if (plane->funcs->reset)
2079 plane->funcs->reset(plane);
2080
e4f62546 2081 drm_for_each_crtc(crtc, dev)
eb033556
CW
2082 if (crtc->funcs->reset)
2083 crtc->funcs->reset(crtc);
2084
e4f62546 2085 drm_for_each_encoder(encoder, dev)
eb033556
CW
2086 if (encoder->funcs->reset)
2087 encoder->funcs->reset(encoder);
2088
f8c2ba31 2089 mutex_lock(&dev->mode_config.mutex);
4eebf60b 2090 drm_for_each_connector(connector, dev)
eb033556
CW
2091 if (connector->funcs->reset)
2092 connector->funcs->reset(connector);
f8c2ba31 2093 mutex_unlock(&dev->mode_config.mutex);
eb033556
CW
2094}
2095EXPORT_SYMBOL(drm_mode_config_reset);
ff72145b 2096
c8e32cc1
DV
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:
1a498633 2114 * Zero on success, negative errno on failure.
c8e32cc1 2115 */
ff72145b
DA
2116int 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;
b28cd41f 2120 u32 cpp, stride, size;
ff72145b
DA
2121
2122 if (!dev->driver->dumb_create)
2123 return -ENOSYS;
b28cd41f
DH
2124 if (!args->width || !args->height || !args->bpp)
2125 return -EINVAL;
2126
2127 /* overflow checks for 32bit size calculations */
00e72089 2128 /* NOTE: DIV_ROUND_UP() can overflow */
b28cd41f 2129 cpp = DIV_ROUND_UP(args->bpp, 8);
00e72089 2130 if (!cpp || cpp > 0xffffffffU / args->width)
b28cd41f
DH
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
f6085952
TR
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
ff72145b
DA
2151 return dev->driver->dumb_create(file_priv, dev, args);
2152}
2153
c8e32cc1
DV
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:
1a498633 2166 * Zero on success, negative errno on failure.
c8e32cc1 2167 */
ff72145b
DA
2168int 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
c8e32cc1
DV
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:
1a498633 2193 * Zero on success, negative errno on failure.
c8e32cc1 2194 */
ff72145b
DA
2195int 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}
248dbc23 2205
3c9855f6
VS
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 *
31ad61e4
JL
2215 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
2216 * DRM_ROTATE_90 | DRM_ROTATE_180 |
2217 * DRM_ROTATE_270 | DRM_REFLECT_Y);
3c9855f6
VS
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 */
2224unsigned int drm_rotation_simplify(unsigned int rotation,
2225 unsigned int supported_rotations)
2226{
2227 if (rotation & ~supported_rotations) {
31ad61e4 2228 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
14152c8d
JL
2229 rotation = (rotation & DRM_REFLECT_MASK) |
2230 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
3c9855f6
VS
2231 }
2232
2233 return rotation;
2234}
2235EXPORT_SYMBOL(drm_rotation_simplify);
2236
87d24fc3
LP
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 */
2249void drm_mode_config_init(struct drm_device *dev)
2250{
2251 mutex_init(&dev->mode_config.mutex);
51fd371b 2252 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
87d24fc3
LP
2253 mutex_init(&dev->mode_config.idr_mutex);
2254 mutex_init(&dev->mode_config.fb_lock);
8fb6e7a5 2255 mutex_init(&dev->mode_config.blob_lock);
87d24fc3
LP
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);
138f9ebb 2264 idr_init(&dev->mode_config.tile_idr);
5fff80bb 2265 ida_init(&dev->mode_config.connector_ida);
87d24fc3
LP
2266
2267 drm_modeset_lock_all(dev);
6b4959f4 2268 drm_mode_create_standard_properties(dev);
87d24fc3
LP
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;
e27dde3e
MR
2276 dev->mode_config.num_overlay_plane = 0;
2277 dev->mode_config.num_total_plane = 0;
87d24fc3
LP
2278}
2279EXPORT_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 */
2294void 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
f35034f8
ML
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
87d24fc3 2328 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
e2f5d2ea 2329 head_global) {
6bcacf51 2330 drm_property_unreference_blob(blob);
87d24fc3
LP
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) {
d0f37cf6 2343 drm_framebuffer_free(&fb->base.refcount);
87d24fc3
LP
2344 }
2345
5fff80bb 2346 ida_destroy(&dev->mode_config.connector_ida);
138f9ebb 2347 idr_destroy(&dev->mode_config.tile_idr);
87d24fc3 2348 idr_destroy(&dev->mode_config.crtc_idr);
51fd371b 2349 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
87d24fc3
LP
2350}
2351EXPORT_SYMBOL(drm_mode_config_cleanup);
c1df5f3c
VS
2352
2353struct 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[] = {
31ad61e4
JL
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" },
c1df5f3c
VS
2363 };
2364
2365 return drm_property_create_bitmask(dev, 0, "rotation",
2366 props, ARRAY_SIZE(props),
2367 supported_rotations);
2368}
2369EXPORT_SYMBOL(drm_mode_create_rotation_property);
138f9ebb
DA
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 */
2379static 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 */
2396void 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 */
2412struct 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}
81ddd1bc 2429EXPORT_SYMBOL(drm_mode_get_tile_group);
138f9ebb
DA
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 */
2442struct 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}
81ddd1bc 2468EXPORT_SYMBOL(drm_mode_create_tile_group);
f8ed34ac
JS
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 */
2485void 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}
2513EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
This page took 0.729615 seconds and 5 git commands to generate.