drm: Extract drm_plane.[hc]
[linux-2.6-block.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
52217195
DV
57/*
58 * Optional properties
ac1bb36c 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
79190ea2
BG
254int drm_modeset_register_all(struct drm_device *dev)
255{
256 int ret;
257
258 ret = drm_plane_register_all(dev);
259 if (ret)
260 goto err_plane;
261
262 ret = drm_crtc_register_all(dev);
263 if (ret)
264 goto err_crtc;
265
266 ret = drm_encoder_register_all(dev);
267 if (ret)
268 goto err_encoder;
269
270 ret = drm_connector_register_all(dev);
271 if (ret)
272 goto err_connector;
273
274 return 0;
275
276err_connector:
277 drm_encoder_unregister_all(dev);
278err_encoder:
279 drm_crtc_unregister_all(dev);
280err_crtc:
281 drm_plane_unregister_all(dev);
282err_plane:
283 return ret;
284}
285
286void drm_modeset_unregister_all(struct drm_device *dev)
287{
288 drm_connector_unregister_all(dev);
289 drm_encoder_unregister_all(dev);
290 drm_crtc_unregister_all(dev);
291 drm_plane_unregister_all(dev);
292}
293
6b4959f4 294static int drm_mode_create_standard_properties(struct drm_device *dev)
f453ba04 295{
356af0e1 296 struct drm_property *prop;
52217195 297 int ret;
f453ba04 298
52217195
DV
299 ret = drm_connector_create_standard_properties(dev);
300 if (ret)
301 return ret;
9922ab5a 302
6b4959f4 303 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
9922ab5a
RC
304 "type", drm_plane_type_enum_list,
305 ARRAY_SIZE(drm_plane_type_enum_list));
6b4959f4
RC
306 if (!prop)
307 return -ENOMEM;
308 dev->mode_config.plane_type_property = prop;
309
310 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
311 "SRC_X", 0, UINT_MAX);
312 if (!prop)
313 return -ENOMEM;
314 dev->mode_config.prop_src_x = prop;
315
316 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
317 "SRC_Y", 0, UINT_MAX);
318 if (!prop)
319 return -ENOMEM;
320 dev->mode_config.prop_src_y = prop;
321
322 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
323 "SRC_W", 0, UINT_MAX);
324 if (!prop)
325 return -ENOMEM;
326 dev->mode_config.prop_src_w = prop;
327
328 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
329 "SRC_H", 0, UINT_MAX);
330 if (!prop)
331 return -ENOMEM;
332 dev->mode_config.prop_src_h = prop;
333
334 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
335 "CRTC_X", INT_MIN, INT_MAX);
336 if (!prop)
337 return -ENOMEM;
338 dev->mode_config.prop_crtc_x = prop;
339
340 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
341 "CRTC_Y", INT_MIN, INT_MAX);
342 if (!prop)
343 return -ENOMEM;
344 dev->mode_config.prop_crtc_y = prop;
345
346 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
347 "CRTC_W", 0, INT_MAX);
348 if (!prop)
349 return -ENOMEM;
350 dev->mode_config.prop_crtc_w = prop;
351
352 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
353 "CRTC_H", 0, INT_MAX);
354 if (!prop)
355 return -ENOMEM;
356 dev->mode_config.prop_crtc_h = prop;
357
358 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
359 "FB_ID", DRM_MODE_OBJECT_FB);
360 if (!prop)
361 return -ENOMEM;
362 dev->mode_config.prop_fb_id = prop;
363
364 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
365 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
366 if (!prop)
367 return -ENOMEM;
368 dev->mode_config.prop_crtc_id = prop;
9922ab5a 369
eab3bbef
DV
370 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
371 "ACTIVE");
372 if (!prop)
373 return -ENOMEM;
374 dev->mode_config.prop_active = prop;
375
955f3c33
DS
376 prop = drm_property_create(dev,
377 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
378 "MODE_ID", 0);
379 if (!prop)
380 return -ENOMEM;
381 dev->mode_config.prop_mode_id = prop;
382
5488dc16
LL
383 prop = drm_property_create(dev,
384 DRM_MODE_PROP_BLOB,
385 "DEGAMMA_LUT", 0);
386 if (!prop)
387 return -ENOMEM;
388 dev->mode_config.degamma_lut_property = prop;
389
390 prop = drm_property_create_range(dev,
391 DRM_MODE_PROP_IMMUTABLE,
392 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
393 if (!prop)
394 return -ENOMEM;
395 dev->mode_config.degamma_lut_size_property = prop;
396
397 prop = drm_property_create(dev,
398 DRM_MODE_PROP_BLOB,
399 "CTM", 0);
400 if (!prop)
401 return -ENOMEM;
402 dev->mode_config.ctm_property = prop;
403
404 prop = drm_property_create(dev,
405 DRM_MODE_PROP_BLOB,
406 "GAMMA_LUT", 0);
407 if (!prop)
408 return -ENOMEM;
409 dev->mode_config.gamma_lut_property = prop;
410
411 prop = drm_property_create_range(dev,
412 DRM_MODE_PROP_IMMUTABLE,
413 "GAMMA_LUT_SIZE", 0, UINT_MAX);
414 if (!prop)
415 return -ENOMEM;
416 dev->mode_config.gamma_lut_size_property = prop;
417
9922ab5a
RC
418 return 0;
419}
420
f453ba04
DA
421/**
422 * drm_mode_getresources - get graphics configuration
065a50ed
DV
423 * @dev: drm device for the ioctl
424 * @data: data pointer for the ioctl
425 * @file_priv: drm file for the ioctl call
f453ba04 426 *
f453ba04
DA
427 * Construct a set of configuration description structures and return
428 * them to the user, including CRTC, connector and framebuffer configuration.
429 *
430 * Called by the user via ioctl.
431 *
c8e32cc1 432 * Returns:
1a498633 433 * Zero on success, negative errno on failure.
f453ba04
DA
434 */
435int drm_mode_getresources(struct drm_device *dev, void *data,
436 struct drm_file *file_priv)
437{
438 struct drm_mode_card_res *card_res = data;
439 struct list_head *lh;
440 struct drm_framebuffer *fb;
441 struct drm_connector *connector;
442 struct drm_crtc *crtc;
443 struct drm_encoder *encoder;
444 int ret = 0;
445 int connector_count = 0;
446 int crtc_count = 0;
447 int fb_count = 0;
448 int encoder_count = 0;
9c7060f7 449 int copied = 0;
f453ba04
DA
450 uint32_t __user *fb_id;
451 uint32_t __user *crtc_id;
452 uint32_t __user *connector_id;
453 uint32_t __user *encoder_id;
f453ba04 454
fb3b06c8
DA
455 if (!drm_core_check_feature(dev, DRIVER_MODESET))
456 return -EINVAL;
457
f453ba04 458
4b096ac1 459 mutex_lock(&file_priv->fbs_lock);
f453ba04
DA
460 /*
461 * For the non-control nodes we need to limit the list of resources
462 * by IDs in the group list for this node
463 */
464 list_for_each(lh, &file_priv->fbs)
465 fb_count++;
466
4b096ac1
DV
467 /* handle this in 4 parts */
468 /* FBs */
469 if (card_res->count_fbs >= fb_count) {
470 copied = 0;
471 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
472 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
473 if (put_user(fb->base.id, fb_id + copied)) {
474 mutex_unlock(&file_priv->fbs_lock);
475 return -EFAULT;
476 }
477 copied++;
478 }
479 }
480 card_res->count_fbs = fb_count;
481 mutex_unlock(&file_priv->fbs_lock);
482
fcf93f69
DV
483 /* mode_config.mutex protects the connector list against e.g. DP MST
484 * connector hot-adding. CRTC/Plane lists are invariant. */
485 mutex_lock(&dev->mode_config.mutex);
9c7060f7
DV
486 drm_for_each_crtc(crtc, dev)
487 crtc_count++;
f453ba04 488
9c7060f7
DV
489 drm_for_each_connector(connector, dev)
490 connector_count++;
f453ba04 491
9c7060f7
DV
492 drm_for_each_encoder(encoder, dev)
493 encoder_count++;
f453ba04
DA
494
495 card_res->max_height = dev->mode_config.max_height;
496 card_res->min_height = dev->mode_config.min_height;
497 card_res->max_width = dev->mode_config.max_width;
498 card_res->min_width = dev->mode_config.min_width;
499
f453ba04
DA
500 /* CRTCs */
501 if (card_res->count_crtcs >= crtc_count) {
502 copied = 0;
503 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
9c7060f7 504 drm_for_each_crtc(crtc, dev) {
9c7060f7
DV
505 if (put_user(crtc->base.id, crtc_id + copied)) {
506 ret = -EFAULT;
507 goto out;
f453ba04 508 }
9c7060f7 509 copied++;
f453ba04
DA
510 }
511 }
512 card_res->count_crtcs = crtc_count;
513
514 /* Encoders */
515 if (card_res->count_encoders >= encoder_count) {
516 copied = 0;
517 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
9c7060f7 518 drm_for_each_encoder(encoder, dev) {
9c7060f7
DV
519 if (put_user(encoder->base.id, encoder_id +
520 copied)) {
521 ret = -EFAULT;
522 goto out;
f453ba04 523 }
9c7060f7 524 copied++;
f453ba04
DA
525 }
526 }
527 card_res->count_encoders = encoder_count;
528
529 /* Connectors */
530 if (card_res->count_connectors >= connector_count) {
531 copied = 0;
532 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
9c7060f7 533 drm_for_each_connector(connector, dev) {
9c7060f7
DV
534 if (put_user(connector->base.id,
535 connector_id + copied)) {
536 ret = -EFAULT;
537 goto out;
f453ba04 538 }
9c7060f7 539 copied++;
f453ba04
DA
540 }
541 }
542 card_res->count_connectors = connector_count;
543
f453ba04 544out:
fcf93f69 545 mutex_unlock(&dev->mode_config.mutex);
f453ba04
DA
546 return ret;
547}
548
549/**
550 * drm_mode_getcrtc - get CRTC configuration
065a50ed
DV
551 * @dev: drm device for the ioctl
552 * @data: data pointer for the ioctl
553 * @file_priv: drm file for the ioctl call
f453ba04 554 *
f453ba04
DA
555 * Construct a CRTC configuration structure to return to the user.
556 *
557 * Called by the user via ioctl.
558 *
c8e32cc1 559 * Returns:
1a498633 560 * Zero on success, negative errno on failure.
f453ba04
DA
561 */
562int drm_mode_getcrtc(struct drm_device *dev,
563 void *data, struct drm_file *file_priv)
564{
565 struct drm_mode_crtc *crtc_resp = data;
566 struct drm_crtc *crtc;
f453ba04 567
fb3b06c8
DA
568 if (!drm_core_check_feature(dev, DRIVER_MODESET))
569 return -EINVAL;
570
a2b34e22 571 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
fcf93f69
DV
572 if (!crtc)
573 return -ENOENT;
f453ba04 574
fcf93f69 575 drm_modeset_lock_crtc(crtc, crtc->primary);
f453ba04 576 crtc_resp->gamma_size = crtc->gamma_size;
f4510a27
MR
577 if (crtc->primary->fb)
578 crtc_resp->fb_id = crtc->primary->fb->base.id;
f453ba04
DA
579 else
580 crtc_resp->fb_id = 0;
581
31c946e8
DV
582 if (crtc->state) {
583 crtc_resp->x = crtc->primary->state->src_x >> 16;
584 crtc_resp->y = crtc->primary->state->src_y >> 16;
585 if (crtc->state->enable) {
934a8a89 586 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
31c946e8 587 crtc_resp->mode_valid = 1;
f453ba04 588
31c946e8
DV
589 } else {
590 crtc_resp->mode_valid = 0;
591 }
f453ba04 592 } else {
31c946e8
DV
593 crtc_resp->x = crtc->x;
594 crtc_resp->y = crtc->y;
595 if (crtc->enabled) {
934a8a89 596 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
31c946e8
DV
597 crtc_resp->mode_valid = 1;
598
599 } else {
600 crtc_resp->mode_valid = 0;
601 }
f453ba04 602 }
fcf93f69 603 drm_modeset_unlock_crtc(crtc);
f453ba04 604
baf698b0 605 return 0;
f453ba04
DA
606}
607
2d13b679
DV
608/**
609 * drm_mode_set_config_internal - helper to call ->set_config
610 * @set: modeset config to set
611 *
612 * This is a little helper to wrap internal calls to the ->set_config driver
613 * interface. The only thing it adds is correct refcounting dance.
4dfd909f 614 *
c8e32cc1 615 * Returns:
1a498633 616 * Zero on success, negative errno on failure.
2d13b679
DV
617 */
618int drm_mode_set_config_internal(struct drm_mode_set *set)
619{
620 struct drm_crtc *crtc = set->crtc;
5cef29aa
DV
621 struct drm_framebuffer *fb;
622 struct drm_crtc *tmp;
b0d12325
DV
623 int ret;
624
5cef29aa
DV
625 /*
626 * NOTE: ->set_config can also disable other crtcs (if we steal all
627 * connectors from it), hence we need to refcount the fbs across all
628 * crtcs. Atomic modeset will have saner semantics ...
629 */
e4f62546 630 drm_for_each_crtc(tmp, crtc->dev)
3d30a59b 631 tmp->primary->old_fb = tmp->primary->fb;
5cef29aa 632
b0d12325 633 fb = set->fb;
2d13b679 634
b0d12325
DV
635 ret = crtc->funcs->set_config(set);
636 if (ret == 0) {
e13161af 637 crtc->primary->crtc = crtc;
0fe27f06 638 crtc->primary->fb = fb;
5cef29aa 639 }
cc85e121 640
e4f62546 641 drm_for_each_crtc(tmp, crtc->dev) {
f4510a27
MR
642 if (tmp->primary->fb)
643 drm_framebuffer_reference(tmp->primary->fb);
3d30a59b
DV
644 if (tmp->primary->old_fb)
645 drm_framebuffer_unreference(tmp->primary->old_fb);
646 tmp->primary->old_fb = NULL;
b0d12325
DV
647 }
648
649 return ret;
2d13b679
DV
650}
651EXPORT_SYMBOL(drm_mode_set_config_internal);
652
ecb7e16b
GP
653/**
654 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
655 * @mode: mode to query
656 * @hdisplay: hdisplay value to fill in
657 * @vdisplay: vdisplay value to fill in
658 *
659 * The vdisplay value will be doubled if the specified mode is a stereo mode of
660 * the appropriate layout.
661 */
662void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
663 int *hdisplay, int *vdisplay)
664{
665 struct drm_display_mode adjusted;
666
667 drm_mode_copy(&adjusted, mode);
668 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
669 *hdisplay = adjusted.crtc_hdisplay;
670 *vdisplay = adjusted.crtc_vdisplay;
671}
672EXPORT_SYMBOL(drm_crtc_get_hv_timing);
673
af93629d
MR
674/**
675 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
676 * CRTC viewport
677 * @crtc: CRTC that framebuffer will be displayed on
678 * @x: x panning
679 * @y: y panning
680 * @mode: mode that framebuffer will be displayed under
681 * @fb: framebuffer to check size of
c11e9283 682 */
af93629d
MR
683int drm_crtc_check_viewport(const struct drm_crtc *crtc,
684 int x, int y,
685 const struct drm_display_mode *mode,
686 const struct drm_framebuffer *fb)
c11e9283
DL
687
688{
689 int hdisplay, vdisplay;
690
ecb7e16b 691 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
a0c1bbb0 692
33e0be63 693 if (crtc->state &&
31ad61e4
JL
694 crtc->primary->state->rotation & (DRM_ROTATE_90 |
695 DRM_ROTATE_270))
c11e9283
DL
696 swap(hdisplay, vdisplay);
697
43968d7b
DV
698 return drm_framebuffer_check_src_coords(x << 16, y << 16,
699 hdisplay << 16, vdisplay << 16,
700 fb);
c11e9283 701}
af93629d 702EXPORT_SYMBOL(drm_crtc_check_viewport);
c11e9283 703
f453ba04
DA
704/**
705 * drm_mode_setcrtc - set CRTC configuration
065a50ed
DV
706 * @dev: drm device for the ioctl
707 * @data: data pointer for the ioctl
708 * @file_priv: drm file for the ioctl call
f453ba04 709 *
f453ba04
DA
710 * Build a new CRTC configuration based on user request.
711 *
712 * Called by the user via ioctl.
713 *
c8e32cc1 714 * Returns:
1a498633 715 * Zero on success, negative errno on failure.
f453ba04
DA
716 */
717int drm_mode_setcrtc(struct drm_device *dev, void *data,
718 struct drm_file *file_priv)
719{
720 struct drm_mode_config *config = &dev->mode_config;
721 struct drm_mode_crtc *crtc_req = data;
6653cc8d 722 struct drm_crtc *crtc;
f453ba04
DA
723 struct drm_connector **connector_set = NULL, *connector;
724 struct drm_framebuffer *fb = NULL;
725 struct drm_display_mode *mode = NULL;
726 struct drm_mode_set set;
727 uint32_t __user *set_connectors_ptr;
4a1b0714 728 int ret;
f453ba04
DA
729 int i;
730
fb3b06c8
DA
731 if (!drm_core_check_feature(dev, DRIVER_MODESET))
732 return -EINVAL;
733
01447e9f
ZJ
734 /*
735 * Universal plane src offsets are only 16.16, prevent havoc for
736 * drivers using universal plane code internally.
737 */
738 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
1d97e915
VS
739 return -ERANGE;
740
84849903 741 drm_modeset_lock_all(dev);
a2b34e22
RC
742 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
743 if (!crtc) {
58367ed6 744 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
f27657f2 745 ret = -ENOENT;
f453ba04
DA
746 goto out;
747 }
fa3ab4c2 748 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
f453ba04
DA
749
750 if (crtc_req->mode_valid) {
751 /* If we have a mode we need a framebuffer. */
752 /* If we pass -1, set the mode with the currently bound fb */
753 if (crtc_req->fb_id == -1) {
f4510a27 754 if (!crtc->primary->fb) {
6653cc8d
VS
755 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
756 ret = -EINVAL;
757 goto out;
f453ba04 758 }
f4510a27 759 fb = crtc->primary->fb;
b0d12325
DV
760 /* Make refcounting symmetric with the lookup path. */
761 drm_framebuffer_reference(fb);
f453ba04 762 } else {
786b99ed
DV
763 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
764 if (!fb) {
58367ed6
ZY
765 DRM_DEBUG_KMS("Unknown FB ID%d\n",
766 crtc_req->fb_id);
37c4e705 767 ret = -ENOENT;
f453ba04
DA
768 goto out;
769 }
f453ba04
DA
770 }
771
772 mode = drm_mode_create(dev);
ee34ab5b
VS
773 if (!mode) {
774 ret = -ENOMEM;
775 goto out;
776 }
777
934a8a89 778 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
90367bf6
VS
779 if (ret) {
780 DRM_DEBUG_KMS("Invalid mode\n");
781 goto out;
782 }
783
7eb5f302
LP
784 /*
785 * Check whether the primary plane supports the fb pixel format.
786 * Drivers not implementing the universal planes API use a
787 * default formats list provided by the DRM core which doesn't
788 * match real hardware capabilities. Skip the check in that
789 * case.
790 */
791 if (!crtc->primary->format_default) {
792 ret = drm_plane_check_pixel_format(crtc->primary,
793 fb->pixel_format);
794 if (ret) {
d3828147 795 char *format_name = drm_get_format_name(fb->pixel_format);
90844f00
EE
796 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
797 kfree(format_name);
7eb5f302
LP
798 goto out;
799 }
800 }
801
c11e9283
DL
802 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
803 mode, fb);
804 if (ret)
5f61bb42 805 goto out;
c11e9283 806
f453ba04
DA
807 }
808
809 if (crtc_req->count_connectors == 0 && mode) {
58367ed6 810 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
f453ba04
DA
811 ret = -EINVAL;
812 goto out;
813 }
814
7781de74 815 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
58367ed6 816 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
f453ba04
DA
817 crtc_req->count_connectors);
818 ret = -EINVAL;
819 goto out;
820 }
821
822 if (crtc_req->count_connectors > 0) {
823 u32 out_id;
824
825 /* Avoid unbounded kernel memory allocation */
826 if (crtc_req->count_connectors > config->num_connector) {
827 ret = -EINVAL;
828 goto out;
829 }
830
2f6c5389
TR
831 connector_set = kmalloc_array(crtc_req->count_connectors,
832 sizeof(struct drm_connector *),
833 GFP_KERNEL);
f453ba04
DA
834 if (!connector_set) {
835 ret = -ENOMEM;
836 goto out;
837 }
838
839 for (i = 0; i < crtc_req->count_connectors; i++) {
b164d31f 840 connector_set[i] = NULL;
81f6c7f8 841 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
f453ba04
DA
842 if (get_user(out_id, &set_connectors_ptr[i])) {
843 ret = -EFAULT;
844 goto out;
845 }
846
b164d31f 847 connector = drm_connector_lookup(dev, out_id);
a2b34e22 848 if (!connector) {
58367ed6
ZY
849 DRM_DEBUG_KMS("Connector id %d unknown\n",
850 out_id);
f27657f2 851 ret = -ENOENT;
f453ba04
DA
852 goto out;
853 }
9440106b
JG
854 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
855 connector->base.id,
25933820 856 connector->name);
f453ba04
DA
857
858 connector_set[i] = connector;
859 }
860 }
861
862 set.crtc = crtc;
863 set.x = crtc_req->x;
864 set.y = crtc_req->y;
865 set.mode = mode;
866 set.connectors = connector_set;
867 set.num_connectors = crtc_req->count_connectors;
5ef5f72f 868 set.fb = fb;
2d13b679 869 ret = drm_mode_set_config_internal(&set);
f453ba04
DA
870
871out:
b0d12325
DV
872 if (fb)
873 drm_framebuffer_unreference(fb);
874
b164d31f
DA
875 if (connector_set) {
876 for (i = 0; i < crtc_req->count_connectors; i++) {
877 if (connector_set[i])
878 drm_connector_unreference(connector_set[i]);
879 }
880 }
f453ba04 881 kfree(connector_set);
ee34ab5b 882 drm_mode_destroy(dev, mode);
84849903 883 drm_modeset_unlock_all(dev);
f453ba04
DA
884 return ret;
885}
886
949619f3
DV
887int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
888 struct drm_property *property,
889 uint64_t value)
4a67d391 890{
bffd9de0
PZ
891 int ret = -EINVAL;
892 struct drm_crtc *crtc = obj_to_crtc(obj);
4a67d391 893
bffd9de0
PZ
894 if (crtc->funcs->set_property)
895 ret = crtc->funcs->set_property(crtc, property, value);
896 if (!ret)
897 drm_object_property_set_value(obj, property, value);
4a67d391 898
bffd9de0 899 return ret;
4a67d391 900}
4a67d391 901
c8e32cc1
DV
902/**
903 * drm_mode_crtc_set_gamma_size - set the gamma table size
904 * @crtc: CRTC to set the gamma table size for
905 * @gamma_size: size of the gamma table
906 *
907 * Drivers which support gamma tables should set this to the supported gamma
908 * table size when initializing the CRTC. Currently the drm core only supports a
909 * fixed gamma table size.
910 *
911 * Returns:
1a498633 912 * Zero on success, negative errno on failure.
c8e32cc1 913 */
4cae5b84 914int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
c8e32cc1 915 int gamma_size)
f453ba04 916{
cf48e292
DV
917 uint16_t *r_base, *g_base, *b_base;
918 int i;
919
f453ba04
DA
920 crtc->gamma_size = gamma_size;
921
bd3f0ff9
TR
922 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
923 GFP_KERNEL);
f453ba04
DA
924 if (!crtc->gamma_store) {
925 crtc->gamma_size = 0;
4cae5b84 926 return -ENOMEM;
f453ba04
DA
927 }
928
cf48e292
DV
929 r_base = crtc->gamma_store;
930 g_base = r_base + gamma_size;
931 b_base = g_base + gamma_size;
932 for (i = 0; i < gamma_size; i++) {
933 r_base[i] = i << 8;
934 g_base[i] = i << 8;
935 b_base[i] = i << 8;
936 }
937
938
4cae5b84 939 return 0;
f453ba04
DA
940}
941EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
942
c8e32cc1
DV
943/**
944 * drm_mode_gamma_set_ioctl - set the gamma table
945 * @dev: DRM device
946 * @data: ioctl data
947 * @file_priv: DRM file info
948 *
949 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
950 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
951 *
952 * Called by the user via ioctl.
953 *
954 * Returns:
1a498633 955 * Zero on success, negative errno on failure.
c8e32cc1 956 */
f453ba04
DA
957int drm_mode_gamma_set_ioctl(struct drm_device *dev,
958 void *data, struct drm_file *file_priv)
959{
960 struct drm_mode_crtc_lut *crtc_lut = data;
f453ba04
DA
961 struct drm_crtc *crtc;
962 void *r_base, *g_base, *b_base;
963 int size;
964 int ret = 0;
965
fb3b06c8
DA
966 if (!drm_core_check_feature(dev, DRIVER_MODESET))
967 return -EINVAL;
968
84849903 969 drm_modeset_lock_all(dev);
a2b34e22
RC
970 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
971 if (!crtc) {
f27657f2 972 ret = -ENOENT;
f453ba04
DA
973 goto out;
974 }
f453ba04 975
ebe0f244
LP
976 if (crtc->funcs->gamma_set == NULL) {
977 ret = -ENOSYS;
978 goto out;
979 }
980
f453ba04
DA
981 /* memcpy into gamma store */
982 if (crtc_lut->gamma_size != crtc->gamma_size) {
983 ret = -EINVAL;
984 goto out;
985 }
986
987 size = crtc_lut->gamma_size * (sizeof(uint16_t));
988 r_base = crtc->gamma_store;
989 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
990 ret = -EFAULT;
991 goto out;
992 }
993
994 g_base = r_base + size;
995 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
996 ret = -EFAULT;
997 goto out;
998 }
999
1000 b_base = g_base + size;
1001 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
1002 ret = -EFAULT;
1003 goto out;
1004 }
1005
7ea77283 1006 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
f453ba04
DA
1007
1008out:
84849903 1009 drm_modeset_unlock_all(dev);
f453ba04
DA
1010 return ret;
1011
1012}
1013
c8e32cc1
DV
1014/**
1015 * drm_mode_gamma_get_ioctl - get the gamma table
1016 * @dev: DRM device
1017 * @data: ioctl data
1018 * @file_priv: DRM file info
1019 *
1020 * Copy the current gamma table into the storage provided. This also provides
1021 * the gamma table size the driver expects, which can be used to size the
1022 * allocated storage.
1023 *
1024 * Called by the user via ioctl.
1025 *
1026 * Returns:
1a498633 1027 * Zero on success, negative errno on failure.
c8e32cc1 1028 */
f453ba04
DA
1029int drm_mode_gamma_get_ioctl(struct drm_device *dev,
1030 void *data, struct drm_file *file_priv)
1031{
1032 struct drm_mode_crtc_lut *crtc_lut = data;
f453ba04
DA
1033 struct drm_crtc *crtc;
1034 void *r_base, *g_base, *b_base;
1035 int size;
1036 int ret = 0;
1037
fb3b06c8
DA
1038 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1039 return -EINVAL;
1040
84849903 1041 drm_modeset_lock_all(dev);
a2b34e22
RC
1042 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
1043 if (!crtc) {
f27657f2 1044 ret = -ENOENT;
f453ba04
DA
1045 goto out;
1046 }
f453ba04
DA
1047
1048 /* memcpy into gamma store */
1049 if (crtc_lut->gamma_size != crtc->gamma_size) {
1050 ret = -EINVAL;
1051 goto out;
1052 }
1053
1054 size = crtc_lut->gamma_size * (sizeof(uint16_t));
1055 r_base = crtc->gamma_store;
1056 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
1057 ret = -EFAULT;
1058 goto out;
1059 }
1060
1061 g_base = r_base + size;
1062 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
1063 ret = -EFAULT;
1064 goto out;
1065 }
1066
1067 b_base = g_base + size;
1068 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
1069 ret = -EFAULT;
1070 goto out;
1071 }
1072out:
84849903 1073 drm_modeset_unlock_all(dev);
f453ba04
DA
1074 return ret;
1075}
d91d8a3f 1076
c8e32cc1
DV
1077/**
1078 * drm_mode_config_reset - call ->reset callbacks
1079 * @dev: drm device
1080 *
1081 * This functions calls all the crtc's, encoder's and connector's ->reset
1082 * callback. Drivers can use this in e.g. their driver load or resume code to
1083 * reset hardware and software state.
1084 */
eb033556
CW
1085void drm_mode_config_reset(struct drm_device *dev)
1086{
1087 struct drm_crtc *crtc;
2a0d7cfd 1088 struct drm_plane *plane;
eb033556
CW
1089 struct drm_encoder *encoder;
1090 struct drm_connector *connector;
1091
e4f62546 1092 drm_for_each_plane(plane, dev)
2a0d7cfd
DV
1093 if (plane->funcs->reset)
1094 plane->funcs->reset(plane);
1095
e4f62546 1096 drm_for_each_crtc(crtc, dev)
eb033556
CW
1097 if (crtc->funcs->reset)
1098 crtc->funcs->reset(crtc);
1099
e4f62546 1100 drm_for_each_encoder(encoder, dev)
eb033556
CW
1101 if (encoder->funcs->reset)
1102 encoder->funcs->reset(encoder);
1103
f8c2ba31 1104 mutex_lock(&dev->mode_config.mutex);
4eebf60b 1105 drm_for_each_connector(connector, dev)
eb033556
CW
1106 if (connector->funcs->reset)
1107 connector->funcs->reset(connector);
f8c2ba31 1108 mutex_unlock(&dev->mode_config.mutex);
eb033556
CW
1109}
1110EXPORT_SYMBOL(drm_mode_config_reset);
ff72145b 1111
c8e32cc1
DV
1112/**
1113 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
1114 * @dev: DRM device
1115 * @data: ioctl data
1116 * @file_priv: DRM file info
1117 *
1118 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
1119 * TTM or something else entirely) and returns the resulting buffer handle. This
1120 * handle can then be wrapped up into a framebuffer modeset object.
1121 *
1122 * Note that userspace is not allowed to use such objects for render
1123 * acceleration - drivers must create their own private ioctls for such a use
1124 * case.
1125 *
1126 * Called by the user via ioctl.
1127 *
1128 * Returns:
1a498633 1129 * Zero on success, negative errno on failure.
c8e32cc1 1130 */
ff72145b
DA
1131int drm_mode_create_dumb_ioctl(struct drm_device *dev,
1132 void *data, struct drm_file *file_priv)
1133{
1134 struct drm_mode_create_dumb *args = data;
b28cd41f 1135 u32 cpp, stride, size;
ff72145b
DA
1136
1137 if (!dev->driver->dumb_create)
1138 return -ENOSYS;
b28cd41f
DH
1139 if (!args->width || !args->height || !args->bpp)
1140 return -EINVAL;
1141
1142 /* overflow checks for 32bit size calculations */
00e72089 1143 /* NOTE: DIV_ROUND_UP() can overflow */
b28cd41f 1144 cpp = DIV_ROUND_UP(args->bpp, 8);
00e72089 1145 if (!cpp || cpp > 0xffffffffU / args->width)
b28cd41f
DH
1146 return -EINVAL;
1147 stride = cpp * args->width;
1148 if (args->height > 0xffffffffU / stride)
1149 return -EINVAL;
1150
1151 /* test for wrap-around */
1152 size = args->height * stride;
1153 if (PAGE_ALIGN(size) == 0)
1154 return -EINVAL;
1155
f6085952
TR
1156 /*
1157 * handle, pitch and size are output parameters. Zero them out to
1158 * prevent drivers from accidentally using uninitialized data. Since
1159 * not all existing userspace is clearing these fields properly we
1160 * cannot reject IOCTL with garbage in them.
1161 */
1162 args->handle = 0;
1163 args->pitch = 0;
1164 args->size = 0;
1165
ff72145b
DA
1166 return dev->driver->dumb_create(file_priv, dev, args);
1167}
1168
c8e32cc1
DV
1169/**
1170 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
1171 * @dev: DRM device
1172 * @data: ioctl data
1173 * @file_priv: DRM file info
1174 *
1175 * Allocate an offset in the drm device node's address space to be able to
1176 * memory map a dumb buffer.
1177 *
1178 * Called by the user via ioctl.
1179 *
1180 * Returns:
1a498633 1181 * Zero on success, negative errno on failure.
c8e32cc1 1182 */
ff72145b
DA
1183int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
1184 void *data, struct drm_file *file_priv)
1185{
1186 struct drm_mode_map_dumb *args = data;
1187
1188 /* call driver ioctl to get mmap offset */
1189 if (!dev->driver->dumb_map_offset)
1190 return -ENOSYS;
1191
1192 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
1193}
1194
c8e32cc1
DV
1195/**
1196 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
1197 * @dev: DRM device
1198 * @data: ioctl data
1199 * @file_priv: DRM file info
1200 *
1201 * This destroys the userspace handle for the given dumb backing storage buffer.
1202 * Since buffer objects must be reference counted in the kernel a buffer object
1203 * won't be immediately freed if a framebuffer modeset object still uses it.
1204 *
1205 * Called by the user via ioctl.
1206 *
1207 * Returns:
1a498633 1208 * Zero on success, negative errno on failure.
c8e32cc1 1209 */
ff72145b
DA
1210int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
1211 void *data, struct drm_file *file_priv)
1212{
1213 struct drm_mode_destroy_dumb *args = data;
1214
1215 if (!dev->driver->dumb_destroy)
1216 return -ENOSYS;
1217
1218 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
1219}
248dbc23 1220
3c9855f6
VS
1221/**
1222 * drm_rotation_simplify() - Try to simplify the rotation
1223 * @rotation: Rotation to be simplified
1224 * @supported_rotations: Supported rotations
1225 *
1226 * Attempt to simplify the rotation to a form that is supported.
1227 * Eg. if the hardware supports everything except DRM_REFLECT_X
1228 * one could call this function like this:
1229 *
31ad61e4
JL
1230 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
1231 * DRM_ROTATE_90 | DRM_ROTATE_180 |
1232 * DRM_ROTATE_270 | DRM_REFLECT_Y);
3c9855f6
VS
1233 *
1234 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
1235 * transforms the hardware supports, this function may not
1236 * be able to produce a supported transform, so the caller should
1237 * check the result afterwards.
1238 */
1239unsigned int drm_rotation_simplify(unsigned int rotation,
1240 unsigned int supported_rotations)
1241{
1242 if (rotation & ~supported_rotations) {
31ad61e4 1243 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
14152c8d
JL
1244 rotation = (rotation & DRM_REFLECT_MASK) |
1245 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
3c9855f6
VS
1246 }
1247
1248 return rotation;
1249}
1250EXPORT_SYMBOL(drm_rotation_simplify);
1251
87d24fc3
LP
1252/**
1253 * drm_mode_config_init - initialize DRM mode_configuration structure
1254 * @dev: DRM device
1255 *
1256 * Initialize @dev's mode_config structure, used for tracking the graphics
1257 * configuration of @dev.
1258 *
1259 * Since this initializes the modeset locks, no locking is possible. Which is no
1260 * problem, since this should happen single threaded at init time. It is the
1261 * driver's problem to ensure this guarantee.
1262 *
1263 */
1264void drm_mode_config_init(struct drm_device *dev)
1265{
1266 mutex_init(&dev->mode_config.mutex);
51fd371b 1267 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
87d24fc3
LP
1268 mutex_init(&dev->mode_config.idr_mutex);
1269 mutex_init(&dev->mode_config.fb_lock);
8fb6e7a5 1270 mutex_init(&dev->mode_config.blob_lock);
87d24fc3
LP
1271 INIT_LIST_HEAD(&dev->mode_config.fb_list);
1272 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
1273 INIT_LIST_HEAD(&dev->mode_config.connector_list);
1274 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
1275 INIT_LIST_HEAD(&dev->mode_config.property_list);
1276 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
1277 INIT_LIST_HEAD(&dev->mode_config.plane_list);
1278 idr_init(&dev->mode_config.crtc_idr);
138f9ebb 1279 idr_init(&dev->mode_config.tile_idr);
5fff80bb 1280 ida_init(&dev->mode_config.connector_ida);
87d24fc3
LP
1281
1282 drm_modeset_lock_all(dev);
6b4959f4 1283 drm_mode_create_standard_properties(dev);
87d24fc3
LP
1284 drm_modeset_unlock_all(dev);
1285
1286 /* Just to be sure */
1287 dev->mode_config.num_fb = 0;
1288 dev->mode_config.num_connector = 0;
1289 dev->mode_config.num_crtc = 0;
1290 dev->mode_config.num_encoder = 0;
e27dde3e
MR
1291 dev->mode_config.num_overlay_plane = 0;
1292 dev->mode_config.num_total_plane = 0;
87d24fc3
LP
1293}
1294EXPORT_SYMBOL(drm_mode_config_init);
1295
1296/**
1297 * drm_mode_config_cleanup - free up DRM mode_config info
1298 * @dev: DRM device
1299 *
1300 * Free up all the connectors and CRTCs associated with this DRM device, then
1301 * free up the framebuffers and associated buffer objects.
1302 *
1303 * Note that since this /should/ happen single-threaded at driver/device
1304 * teardown time, no locking is required. It's the driver's job to ensure that
1305 * this guarantee actually holds true.
1306 *
1307 * FIXME: cleanup any dangling user buffer objects too
1308 */
1309void drm_mode_config_cleanup(struct drm_device *dev)
1310{
1311 struct drm_connector *connector, *ot;
1312 struct drm_crtc *crtc, *ct;
1313 struct drm_encoder *encoder, *enct;
1314 struct drm_framebuffer *fb, *fbt;
1315 struct drm_property *property, *pt;
1316 struct drm_property_blob *blob, *bt;
1317 struct drm_plane *plane, *plt;
1318
1319 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
1320 head) {
1321 encoder->funcs->destroy(encoder);
1322 }
1323
1324 list_for_each_entry_safe(connector, ot,
1325 &dev->mode_config.connector_list, head) {
1326 connector->funcs->destroy(connector);
1327 }
1328
1329 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
1330 head) {
1331 drm_property_destroy(dev, property);
1332 }
1333
f35034f8
ML
1334 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
1335 head) {
1336 plane->funcs->destroy(plane);
1337 }
1338
1339 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
1340 crtc->funcs->destroy(crtc);
1341 }
1342
87d24fc3 1343 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
e2f5d2ea 1344 head_global) {
6bcacf51 1345 drm_property_unreference_blob(blob);
87d24fc3
LP
1346 }
1347
1348 /*
1349 * Single-threaded teardown context, so it's not required to grab the
1350 * fb_lock to protect against concurrent fb_list access. Contrary, it
1351 * would actually deadlock with the drm_framebuffer_cleanup function.
1352 *
1353 * Also, if there are any framebuffers left, that's a driver leak now,
1354 * so politely WARN about this.
1355 */
1356 WARN_ON(!list_empty(&dev->mode_config.fb_list));
1357 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
d0f37cf6 1358 drm_framebuffer_free(&fb->base.refcount);
87d24fc3
LP
1359 }
1360
5fff80bb 1361 ida_destroy(&dev->mode_config.connector_ida);
138f9ebb 1362 idr_destroy(&dev->mode_config.tile_idr);
87d24fc3 1363 idr_destroy(&dev->mode_config.crtc_idr);
51fd371b 1364 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
87d24fc3
LP
1365}
1366EXPORT_SYMBOL(drm_mode_config_cleanup);
c1df5f3c
VS
1367
1368struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
1369 unsigned int supported_rotations)
1370{
1371 static const struct drm_prop_enum_list props[] = {
31ad61e4
JL
1372 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
1373 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
1374 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
1375 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
1376 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
1377 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
c1df5f3c
VS
1378 };
1379
1380 return drm_property_create_bitmask(dev, 0, "rotation",
1381 props, ARRAY_SIZE(props),
1382 supported_rotations);
1383}
1384EXPORT_SYMBOL(drm_mode_create_rotation_property);
138f9ebb
DA
1385
1386/**
1387 * DOC: Tile group
1388 *
1389 * Tile groups are used to represent tiled monitors with a unique
1390 * integer identifier. Tiled monitors using DisplayID v1.3 have
1391 * a unique 8-byte handle, we store this in a tile group, so we
1392 * have a common identifier for all tiles in a monitor group.
1393 */
1394static void drm_tile_group_free(struct kref *kref)
1395{
1396 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
1397 struct drm_device *dev = tg->dev;
1398 mutex_lock(&dev->mode_config.idr_mutex);
1399 idr_remove(&dev->mode_config.tile_idr, tg->id);
1400 mutex_unlock(&dev->mode_config.idr_mutex);
1401 kfree(tg);
1402}
1403
1404/**
1405 * drm_mode_put_tile_group - drop a reference to a tile group.
1406 * @dev: DRM device
1407 * @tg: tile group to drop reference to.
1408 *
1409 * drop reference to tile group and free if 0.
1410 */
1411void drm_mode_put_tile_group(struct drm_device *dev,
1412 struct drm_tile_group *tg)
1413{
1414 kref_put(&tg->refcount, drm_tile_group_free);
1415}
1416
1417/**
1418 * drm_mode_get_tile_group - get a reference to an existing tile group
1419 * @dev: DRM device
1420 * @topology: 8-bytes unique per monitor.
1421 *
1422 * Use the unique bytes to get a reference to an existing tile group.
1423 *
1424 * RETURNS:
1425 * tile group or NULL if not found.
1426 */
1427struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
1428 char topology[8])
1429{
1430 struct drm_tile_group *tg;
1431 int id;
1432 mutex_lock(&dev->mode_config.idr_mutex);
1433 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
1434 if (!memcmp(tg->group_data, topology, 8)) {
1435 if (!kref_get_unless_zero(&tg->refcount))
1436 tg = NULL;
1437 mutex_unlock(&dev->mode_config.idr_mutex);
1438 return tg;
1439 }
1440 }
1441 mutex_unlock(&dev->mode_config.idr_mutex);
1442 return NULL;
1443}
81ddd1bc 1444EXPORT_SYMBOL(drm_mode_get_tile_group);
138f9ebb
DA
1445
1446/**
1447 * drm_mode_create_tile_group - create a tile group from a displayid description
1448 * @dev: DRM device
1449 * @topology: 8-bytes unique per monitor.
1450 *
1451 * Create a tile group for the unique monitor, and get a unique
1452 * identifier for the tile group.
1453 *
1454 * RETURNS:
1455 * new tile group or error.
1456 */
1457struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
1458 char topology[8])
1459{
1460 struct drm_tile_group *tg;
1461 int ret;
1462
1463 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
1464 if (!tg)
1465 return ERR_PTR(-ENOMEM);
1466
1467 kref_init(&tg->refcount);
1468 memcpy(tg->group_data, topology, 8);
1469 tg->dev = dev;
1470
1471 mutex_lock(&dev->mode_config.idr_mutex);
1472 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
1473 if (ret >= 0) {
1474 tg->id = ret;
1475 } else {
1476 kfree(tg);
1477 tg = ERR_PTR(ret);
1478 }
1479
1480 mutex_unlock(&dev->mode_config.idr_mutex);
1481 return tg;
1482}
81ddd1bc 1483EXPORT_SYMBOL(drm_mode_create_tile_group);
f8ed34ac
JS
1484
1485/**
1486 * drm_crtc_enable_color_mgmt - enable color management properties
1487 * @crtc: DRM CRTC
1488 * @degamma_lut_size: the size of the degamma lut (before CSC)
1489 * @has_ctm: whether to attach ctm_property for CSC matrix
1490 * @gamma_lut_size: the size of the gamma lut (after CSC)
1491 *
1492 * This function lets the driver enable the color correction
1493 * properties on a CRTC. This includes 3 degamma, csc and gamma
1494 * properties that userspace can set and 2 size properties to inform
1495 * the userspace of the lut sizes. Each of the properties are
1496 * optional. The gamma and degamma properties are only attached if
1497 * their size is not 0 and ctm_property is only attached if has_ctm is
1498 * true.
1499 */
1500void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
1501 uint degamma_lut_size,
1502 bool has_ctm,
1503 uint gamma_lut_size)
1504{
1505 struct drm_device *dev = crtc->dev;
1506 struct drm_mode_config *config = &dev->mode_config;
1507
1508 if (degamma_lut_size) {
1509 drm_object_attach_property(&crtc->base,
1510 config->degamma_lut_property, 0);
1511 drm_object_attach_property(&crtc->base,
1512 config->degamma_lut_size_property,
1513 degamma_lut_size);
1514 }
1515
1516 if (has_ctm)
1517 drm_object_attach_property(&crtc->base,
1518 config->ctm_property, 0);
1519
1520 if (gamma_lut_size) {
1521 drm_object_attach_property(&crtc->base,
1522 config->gamma_lut_property, 0);
1523 drm_object_attach_property(&crtc->base,
1524 config->gamma_lut_size_property,
1525 gamma_lut_size);
1526 }
1527}
1528EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);