drm/vc4: add extcon hdmi connection uevent
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / drm_crtc.c
1 /*
2  * Copyright (c) 2006-2008 Intel Corporation
3  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4  * Copyright (c) 2008 Red Hat Inc.
5  *
6  * DRM core CRTC related functions
7  *
8  * Permission to use, copy, modify, distribute, and sell this software and its
9  * documentation for any purpose is hereby granted without fee, provided that
10  * the above copyright notice appear in all copies and that both that copyright
11  * notice and this permission notice appear in supporting documentation, and
12  * that the name of the copyright holders not be used in advertising or
13  * publicity pertaining to distribution of the software without specific,
14  * written prior permission.  The copyright holders make no representations
15  * about the suitability of this software for any purpose.  It is provided "as
16  * is" without express or implied warranty.
17  *
18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24  * OF THIS SOFTWARE.
25  *
26  * Authors:
27  *      Keith Packard
28  *      Eric Anholt <eric@anholt.net>
29  *      Dave Airlie <airlied@linux.ie>
30  *      Jesse Barnes <jesse.barnes@intel.com>
31  */
32 #include <linux/ctype.h>
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #include <linux/export.h>
36 #include <linux/dma-fence.h>
37 #include <linux/uaccess.h>
38 #include <drm/drm_crtc.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_fourcc.h>
41 #include <drm/drm_managed.h>
42 #include <drm/drm_modeset_lock.h>
43 #include <drm/drm_atomic.h>
44 #include <drm/drm_auth.h>
45 #include <drm/drm_debugfs_crc.h>
46 #include <drm/drm_drv.h>
47 #include <drm/drm_print.h>
48 #include <drm/drm_file.h>
49
50 #include "drm_crtc_internal.h"
51 #include "drm_internal.h"
52
53 /**
54  * DOC: overview
55  *
56  * A CRTC represents the overall display pipeline. It receives pixel data from
57  * &drm_plane and blends them together. The &drm_display_mode is also attached
58  * to the CRTC, specifying display timings. On the output side the data is fed
59  * to one or more &drm_encoder, which are then each connected to one
60  * &drm_connector.
61  *
62  * To create a CRTC, a KMS drivers allocates and zeroes an instances of
63  * &struct drm_crtc (possibly as part of a larger structure) and registers it
64  * with a call to drm_crtc_init_with_planes().
65  *
66  * The CRTC is also the entry point for legacy modeset operations, see
67  * &drm_crtc_funcs.set_config, legacy plane operations, see
68  * &drm_crtc_funcs.page_flip and &drm_crtc_funcs.cursor_set2, and other legacy
69  * operations like &drm_crtc_funcs.gamma_set. For atomic drivers all these
70  * features are controlled through &drm_property and
71  * &drm_mode_config_funcs.atomic_check.
72  */
73
74 /**
75  * drm_crtc_from_index - find the registered CRTC at an index
76  * @dev: DRM device
77  * @idx: index of registered CRTC to find for
78  *
79  * Given a CRTC index, return the registered CRTC from DRM device's
80  * list of CRTCs with matching index. This is the inverse of drm_crtc_index().
81  * It's useful in the vblank callbacks (like &drm_driver.enable_vblank or
82  * &drm_driver.disable_vblank), since that still deals with indices instead
83  * of pointers to &struct drm_crtc."
84  */
85 struct drm_crtc *drm_crtc_from_index(struct drm_device *dev, int idx)
86 {
87         struct drm_crtc *crtc;
88
89         drm_for_each_crtc(crtc, dev)
90                 if (idx == crtc->index)
91                         return crtc;
92
93         return NULL;
94 }
95 EXPORT_SYMBOL(drm_crtc_from_index);
96
97 int drm_crtc_force_disable(struct drm_crtc *crtc)
98 {
99         struct drm_mode_set set = {
100                 .crtc = crtc,
101         };
102
103         WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
104
105         return drm_mode_set_config_internal(&set);
106 }
107
108 static unsigned int drm_num_crtcs(struct drm_device *dev)
109 {
110         unsigned int num = 0;
111         struct drm_crtc *tmp;
112
113         drm_for_each_crtc(tmp, dev) {
114                 num++;
115         }
116
117         return num;
118 }
119
120 int drm_crtc_register_all(struct drm_device *dev)
121 {
122         struct drm_crtc *crtc;
123         int ret = 0;
124
125         drm_for_each_crtc(crtc, dev) {
126                 drm_debugfs_crtc_add(crtc);
127
128                 if (crtc->funcs->late_register)
129                         ret = crtc->funcs->late_register(crtc);
130                 if (ret)
131                         return ret;
132         }
133
134         return 0;
135 }
136
137 void drm_crtc_unregister_all(struct drm_device *dev)
138 {
139         struct drm_crtc *crtc;
140
141         drm_for_each_crtc(crtc, dev) {
142                 if (crtc->funcs->early_unregister)
143                         crtc->funcs->early_unregister(crtc);
144                 drm_debugfs_crtc_remove(crtc);
145         }
146 }
147
148 static int drm_crtc_crc_init(struct drm_crtc *crtc)
149 {
150 #ifdef CONFIG_DEBUG_FS
151         spin_lock_init(&crtc->crc.lock);
152         init_waitqueue_head(&crtc->crc.wq);
153         crtc->crc.source = kstrdup("auto", GFP_KERNEL);
154         if (!crtc->crc.source)
155                 return -ENOMEM;
156 #endif
157         return 0;
158 }
159
160 static void drm_crtc_crc_fini(struct drm_crtc *crtc)
161 {
162 #ifdef CONFIG_DEBUG_FS
163         kfree(crtc->crc.source);
164 #endif
165 }
166
167 static const struct dma_fence_ops drm_crtc_fence_ops;
168
169 static struct drm_crtc *fence_to_crtc(struct dma_fence *fence)
170 {
171         BUG_ON(fence->ops != &drm_crtc_fence_ops);
172         return container_of(fence->lock, struct drm_crtc, fence_lock);
173 }
174
175 static const char *drm_crtc_fence_get_driver_name(struct dma_fence *fence)
176 {
177         struct drm_crtc *crtc = fence_to_crtc(fence);
178
179         return crtc->dev->driver->name;
180 }
181
182 static const char *drm_crtc_fence_get_timeline_name(struct dma_fence *fence)
183 {
184         struct drm_crtc *crtc = fence_to_crtc(fence);
185
186         return crtc->timeline_name;
187 }
188
189 static const struct dma_fence_ops drm_crtc_fence_ops = {
190         .get_driver_name = drm_crtc_fence_get_driver_name,
191         .get_timeline_name = drm_crtc_fence_get_timeline_name,
192 };
193
194 struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc)
195 {
196         struct dma_fence *fence;
197
198         fence = kzalloc(sizeof(*fence), GFP_KERNEL);
199         if (!fence)
200                 return NULL;
201
202         dma_fence_init(fence, &drm_crtc_fence_ops, &crtc->fence_lock,
203                        crtc->fence_context, ++crtc->fence_seqno);
204
205         return fence;
206 }
207
208 /**
209  * DOC: standard CRTC properties
210  *
211  * DRM CRTCs have a few standardized properties:
212  *
213  * ACTIVE:
214  *      Atomic property for setting the power state of the CRTC. When set to 1
215  *      the CRTC will actively display content. When set to 0 the CRTC will be
216  *      powered off. There is no expectation that user-space will reset CRTC
217  *      resources like the mode and planes when setting ACTIVE to 0.
218  *
219  *      User-space can rely on an ACTIVE change to 1 to never fail an atomic
220  *      test as long as no other property has changed. If a change to ACTIVE
221  *      fails an atomic test, this is a driver bug. For this reason setting
222  *      ACTIVE to 0 must not release internal resources (like reserved memory
223  *      bandwidth or clock generators).
224  *
225  *      Note that the legacy DPMS property on connectors is internally routed
226  *      to control this property for atomic drivers.
227  * MODE_ID:
228  *      Atomic property for setting the CRTC display timings. The value is the
229  *      ID of a blob containing the DRM mode info. To disable the CRTC,
230  *      user-space must set this property to 0.
231  *
232  *      Setting MODE_ID to 0 will release reserved resources for the CRTC.
233  * SCALING_FILTER:
234  *      Atomic property for setting the scaling filter for CRTC scaler
235  *
236  *      The value of this property can be one of the following:
237  *
238  *      Default:
239  *              Driver's default scaling filter
240  *      Nearest Neighbor:
241  *              Nearest Neighbor scaling filter
242  */
243
244 __printf(6, 0)
245 static int __drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
246                                        struct drm_plane *primary,
247                                        struct drm_plane *cursor,
248                                        const struct drm_crtc_funcs *funcs,
249                                        const char *name, va_list ap)
250 {
251         struct drm_mode_config *config = &dev->mode_config;
252         int ret;
253
254         WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
255         WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
256
257         /* crtc index is used with 32bit bitmasks */
258         if (WARN_ON(config->num_crtc >= 32))
259                 return -EINVAL;
260
261         WARN_ON(drm_drv_uses_atomic_modeset(dev) &&
262                 (!funcs->atomic_destroy_state ||
263                  !funcs->atomic_duplicate_state));
264
265         crtc->dev = dev;
266         crtc->funcs = funcs;
267
268         INIT_LIST_HEAD(&crtc->commit_list);
269         spin_lock_init(&crtc->commit_lock);
270
271         drm_modeset_lock_init(&crtc->mutex);
272         ret = drm_mode_object_add(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
273         if (ret)
274                 return ret;
275
276         if (name) {
277                 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
278         } else {
279                 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
280                                        drm_num_crtcs(dev));
281         }
282         if (!crtc->name) {
283                 drm_mode_object_unregister(dev, &crtc->base);
284                 return -ENOMEM;
285         }
286
287         crtc->fence_context = dma_fence_context_alloc(1);
288         spin_lock_init(&crtc->fence_lock);
289         snprintf(crtc->timeline_name, sizeof(crtc->timeline_name),
290                  "CRTC:%d-%s", crtc->base.id, crtc->name);
291
292         crtc->base.properties = &crtc->properties;
293
294         list_add_tail(&crtc->head, &config->crtc_list);
295         crtc->index = config->num_crtc++;
296
297         crtc->primary = primary;
298         crtc->cursor = cursor;
299         if (primary && !primary->possible_crtcs)
300                 primary->possible_crtcs = drm_crtc_mask(crtc);
301         if (cursor && !cursor->possible_crtcs)
302                 cursor->possible_crtcs = drm_crtc_mask(crtc);
303
304         ret = drm_crtc_crc_init(crtc);
305         if (ret) {
306                 drm_mode_object_unregister(dev, &crtc->base);
307                 return ret;
308         }
309
310         if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
311                 drm_object_attach_property(&crtc->base, config->prop_active, 0);
312                 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
313                 drm_object_attach_property(&crtc->base,
314                                            config->prop_out_fence_ptr, 0);
315                 drm_object_attach_property(&crtc->base,
316                                            config->prop_vrr_enabled, 0);
317         }
318
319         return 0;
320 }
321
322 /**
323  * drm_crtc_init_with_planes - Initialise a new CRTC object with
324  *    specified primary and cursor planes.
325  * @dev: DRM device
326  * @crtc: CRTC object to init
327  * @primary: Primary plane for CRTC
328  * @cursor: Cursor plane for CRTC
329  * @funcs: callbacks for the new CRTC
330  * @name: printf style format string for the CRTC name, or NULL for default name
331  *
332  * Inits a new object created as base part of a driver crtc object. Drivers
333  * should use this function instead of drm_crtc_init(), which is only provided
334  * for backwards compatibility with drivers which do not yet support universal
335  * planes). For really simple hardware which has only 1 plane look at
336  * drm_simple_display_pipe_init() instead.
337  * The &drm_crtc_funcs.destroy hook should call drm_crtc_cleanup() and kfree()
338  * the crtc structure. The crtc structure should not be allocated with
339  * devm_kzalloc().
340  *
341  * The @primary and @cursor planes are only relevant for legacy uAPI, see
342  * &drm_crtc.primary and &drm_crtc.cursor.
343  *
344  * Note: consider using drmm_crtc_alloc_with_planes() or
345  * drmm_crtc_init_with_planes() instead of drm_crtc_init_with_planes()
346  * to let the DRM managed resource infrastructure take care of cleanup
347  * and deallocation.
348  *
349  * Returns:
350  * Zero on success, error code on failure.
351  */
352 int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
353                               struct drm_plane *primary,
354                               struct drm_plane *cursor,
355                               const struct drm_crtc_funcs *funcs,
356                               const char *name, ...)
357 {
358         va_list ap;
359         int ret;
360
361         WARN_ON(!funcs->destroy);
362
363         va_start(ap, name);
364         ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
365                                           name, ap);
366         va_end(ap);
367
368         return ret;
369 }
370 EXPORT_SYMBOL(drm_crtc_init_with_planes);
371
372 static void drmm_crtc_init_with_planes_cleanup(struct drm_device *dev,
373                                                void *ptr)
374 {
375         struct drm_crtc *crtc = ptr;
376
377         drm_crtc_cleanup(crtc);
378 }
379
380 static int __drmm_crtc_init_with_planes(struct drm_device *dev,
381                                         struct drm_crtc *crtc,
382                                         struct drm_plane *primary,
383                                         struct drm_plane *cursor,
384                                         const struct drm_crtc_funcs *funcs,
385                                         const char *name,
386                                         va_list args)
387 {
388         int ret;
389
390         drm_WARN_ON(dev, funcs && funcs->destroy);
391
392         ret = __drm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
393                                           name, args);
394         if (ret)
395                 return ret;
396
397         ret = drmm_add_action_or_reset(dev, drmm_crtc_init_with_planes_cleanup,
398                                        crtc);
399         if (ret)
400                 return ret;
401
402         return 0;
403 }
404
405 /**
406  * drmm_crtc_init_with_planes - Initialise a new CRTC object with
407  *    specified primary and cursor planes.
408  * @dev: DRM device
409  * @crtc: CRTC object to init
410  * @primary: Primary plane for CRTC
411  * @cursor: Cursor plane for CRTC
412  * @funcs: callbacks for the new CRTC
413  * @name: printf style format string for the CRTC name, or NULL for default name
414  *
415  * Inits a new object created as base part of a driver crtc object. Drivers
416  * should use this function instead of drm_crtc_init(), which is only provided
417  * for backwards compatibility with drivers which do not yet support universal
418  * planes). For really simple hardware which has only 1 plane look at
419  * drm_simple_display_pipe_init() instead.
420  *
421  * Cleanup is automatically handled through registering
422  * drmm_crtc_cleanup() with drmm_add_action(). The crtc structure should
423  * be allocated with drmm_kzalloc().
424  *
425  * The @drm_crtc_funcs.destroy hook must be NULL.
426  *
427  * The @primary and @cursor planes are only relevant for legacy uAPI, see
428  * &drm_crtc.primary and &drm_crtc.cursor.
429  *
430  * Returns:
431  * Zero on success, error code on failure.
432  */
433 int drmm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
434                                struct drm_plane *primary,
435                                struct drm_plane *cursor,
436                                const struct drm_crtc_funcs *funcs,
437                                const char *name, ...)
438 {
439         va_list ap;
440         int ret;
441
442         va_start(ap, name);
443         ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
444                                            name, ap);
445         va_end(ap);
446         if (ret)
447                 return ret;
448
449         return 0;
450 }
451 EXPORT_SYMBOL(drmm_crtc_init_with_planes);
452
453 void *__drmm_crtc_alloc_with_planes(struct drm_device *dev,
454                                     size_t size, size_t offset,
455                                     struct drm_plane *primary,
456                                     struct drm_plane *cursor,
457                                     const struct drm_crtc_funcs *funcs,
458                                     const char *name, ...)
459 {
460         void *container;
461         struct drm_crtc *crtc;
462         va_list ap;
463         int ret;
464
465         if (WARN_ON(!funcs || funcs->destroy))
466                 return ERR_PTR(-EINVAL);
467
468         container = drmm_kzalloc(dev, size, GFP_KERNEL);
469         if (!container)
470                 return ERR_PTR(-ENOMEM);
471
472         crtc = container + offset;
473
474         va_start(ap, name);
475         ret = __drmm_crtc_init_with_planes(dev, crtc, primary, cursor, funcs,
476                                            name, ap);
477         va_end(ap);
478         if (ret)
479                 return ERR_PTR(ret);
480
481         return container;
482 }
483 EXPORT_SYMBOL(__drmm_crtc_alloc_with_planes);
484
485 /**
486  * drm_crtc_cleanup - Clean up the core crtc usage
487  * @crtc: CRTC to cleanup
488  *
489  * This function cleans up @crtc and removes it from the DRM mode setting
490  * core. Note that the function does *not* free the crtc structure itself,
491  * this is the responsibility of the caller.
492  */
493 void drm_crtc_cleanup(struct drm_crtc *crtc)
494 {
495         struct drm_device *dev = crtc->dev;
496
497         /* Note that the crtc_list is considered to be static; should we
498          * remove the drm_crtc at runtime we would have to decrement all
499          * the indices on the drm_crtc after us in the crtc_list.
500          */
501
502         drm_crtc_crc_fini(crtc);
503
504         kfree(crtc->gamma_store);
505         crtc->gamma_store = NULL;
506
507         drm_modeset_lock_fini(&crtc->mutex);
508
509         drm_mode_object_unregister(dev, &crtc->base);
510         list_del(&crtc->head);
511         dev->mode_config.num_crtc--;
512
513         WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
514         if (crtc->state && crtc->funcs->atomic_destroy_state)
515                 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
516
517         kfree(crtc->name);
518
519         memset(crtc, 0, sizeof(*crtc));
520 }
521 EXPORT_SYMBOL(drm_crtc_cleanup);
522
523 /**
524  * drm_mode_getcrtc - get CRTC configuration
525  * @dev: drm device for the ioctl
526  * @data: data pointer for the ioctl
527  * @file_priv: drm file for the ioctl call
528  *
529  * Construct a CRTC configuration structure to return to the user.
530  *
531  * Called by the user via ioctl.
532  *
533  * Returns:
534  * Zero on success, negative errno on failure.
535  */
536 int drm_mode_getcrtc(struct drm_device *dev,
537                      void *data, struct drm_file *file_priv)
538 {
539         struct drm_mode_crtc *crtc_resp = data;
540         struct drm_crtc *crtc;
541         struct drm_plane *plane;
542
543         if (!drm_core_check_feature(dev, DRIVER_MODESET))
544                 return -EOPNOTSUPP;
545
546         crtc = drm_crtc_find(dev, file_priv, crtc_resp->crtc_id);
547         if (!crtc)
548                 return -ENOENT;
549
550         plane = crtc->primary;
551
552         crtc_resp->gamma_size = crtc->gamma_size;
553
554         drm_modeset_lock(&plane->mutex, NULL);
555         if (plane->state && plane->state->fb)
556                 crtc_resp->fb_id = plane->state->fb->base.id;
557         else if (!plane->state && plane->fb)
558                 crtc_resp->fb_id = plane->fb->base.id;
559         else
560                 crtc_resp->fb_id = 0;
561
562         if (plane->state) {
563                 crtc_resp->x = plane->state->src_x >> 16;
564                 crtc_resp->y = plane->state->src_y >> 16;
565         }
566         drm_modeset_unlock(&plane->mutex);
567
568         drm_modeset_lock(&crtc->mutex, NULL);
569         if (crtc->state) {
570                 if (crtc->state->enable) {
571                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
572                         crtc_resp->mode_valid = 1;
573                 } else {
574                         crtc_resp->mode_valid = 0;
575                 }
576         } else {
577                 crtc_resp->x = crtc->x;
578                 crtc_resp->y = crtc->y;
579
580                 if (crtc->enabled) {
581                         drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
582                         crtc_resp->mode_valid = 1;
583
584                 } else {
585                         crtc_resp->mode_valid = 0;
586                 }
587         }
588         if (!file_priv->aspect_ratio_allowed)
589                 crtc_resp->mode.flags &= ~DRM_MODE_FLAG_PIC_AR_MASK;
590         drm_modeset_unlock(&crtc->mutex);
591
592         return 0;
593 }
594
595 static int __drm_mode_set_config_internal(struct drm_mode_set *set,
596                                           struct drm_modeset_acquire_ctx *ctx)
597 {
598         struct drm_crtc *crtc = set->crtc;
599         struct drm_framebuffer *fb;
600         struct drm_crtc *tmp;
601         int ret;
602
603         WARN_ON(drm_drv_uses_atomic_modeset(crtc->dev));
604
605         /*
606          * NOTE: ->set_config can also disable other crtcs (if we steal all
607          * connectors from it), hence we need to refcount the fbs across all
608          * crtcs. Atomic modeset will have saner semantics ...
609          */
610         drm_for_each_crtc(tmp, crtc->dev) {
611                 struct drm_plane *plane = tmp->primary;
612
613                 plane->old_fb = plane->fb;
614         }
615
616         fb = set->fb;
617
618         ret = crtc->funcs->set_config(set, ctx);
619         if (ret == 0) {
620                 struct drm_plane *plane = crtc->primary;
621
622                 plane->crtc = fb ? crtc : NULL;
623                 plane->fb = fb;
624         }
625
626         drm_for_each_crtc(tmp, crtc->dev) {
627                 struct drm_plane *plane = tmp->primary;
628
629                 if (plane->fb)
630                         drm_framebuffer_get(plane->fb);
631                 if (plane->old_fb)
632                         drm_framebuffer_put(plane->old_fb);
633                 plane->old_fb = NULL;
634         }
635
636         return ret;
637 }
638
639 /**
640  * drm_mode_set_config_internal - helper to call &drm_mode_config_funcs.set_config
641  * @set: modeset config to set
642  *
643  * This is a little helper to wrap internal calls to the
644  * &drm_mode_config_funcs.set_config driver interface. The only thing it adds is
645  * correct refcounting dance.
646  *
647  * This should only be used by non-atomic legacy drivers.
648  *
649  * Returns:
650  * Zero on success, negative errno on failure.
651  */
652 int drm_mode_set_config_internal(struct drm_mode_set *set)
653 {
654         WARN_ON(drm_drv_uses_atomic_modeset(set->crtc->dev));
655
656         return __drm_mode_set_config_internal(set, NULL);
657 }
658 EXPORT_SYMBOL(drm_mode_set_config_internal);
659
660 /**
661  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
662  *     CRTC viewport
663  * @crtc: CRTC that framebuffer will be displayed on
664  * @x: x panning
665  * @y: y panning
666  * @mode: mode that framebuffer will be displayed under
667  * @fb: framebuffer to check size of
668  */
669 int drm_crtc_check_viewport(const struct drm_crtc *crtc,
670                             int x, int y,
671                             const struct drm_display_mode *mode,
672                             const struct drm_framebuffer *fb)
673
674 {
675         int hdisplay, vdisplay;
676
677         drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
678
679         if (crtc->state &&
680             drm_rotation_90_or_270(crtc->primary->state->rotation))
681                 swap(hdisplay, vdisplay);
682
683         return drm_framebuffer_check_src_coords(x << 16, y << 16,
684                                                 hdisplay << 16, vdisplay << 16,
685                                                 fb);
686 }
687 EXPORT_SYMBOL(drm_crtc_check_viewport);
688
689 /**
690  * drm_mode_setcrtc - set CRTC configuration
691  * @dev: drm device for the ioctl
692  * @data: data pointer for the ioctl
693  * @file_priv: drm file for the ioctl call
694  *
695  * Build a new CRTC configuration based on user request.
696  *
697  * Called by the user via ioctl.
698  *
699  * Returns:
700  * Zero on success, negative errno on failure.
701  */
702 int drm_mode_setcrtc(struct drm_device *dev, void *data,
703                      struct drm_file *file_priv)
704 {
705         struct drm_mode_config *config = &dev->mode_config;
706         struct drm_mode_crtc *crtc_req = data;
707         struct drm_crtc *crtc;
708         struct drm_plane *plane;
709         struct drm_connector **connector_set = NULL, *connector;
710         struct drm_framebuffer *fb = NULL;
711         struct drm_display_mode *mode = NULL;
712         struct drm_mode_set set;
713         uint32_t __user *set_connectors_ptr;
714         struct drm_modeset_acquire_ctx ctx;
715         int ret;
716         int i;
717
718         if (!drm_core_check_feature(dev, DRIVER_MODESET))
719                 return -EOPNOTSUPP;
720
721         /*
722          * Universal plane src offsets are only 16.16, prevent havoc for
723          * drivers using universal plane code internally.
724          */
725         if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
726                 return -ERANGE;
727
728         crtc = drm_crtc_find(dev, file_priv, crtc_req->crtc_id);
729         if (!crtc) {
730                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
731                 return -ENOENT;
732         }
733         DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
734
735         plane = crtc->primary;
736
737         /* allow disabling with the primary plane leased */
738         if (crtc_req->mode_valid && !drm_lease_held(file_priv, plane->base.id))
739                 return -EACCES;
740
741         DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx,
742                                    DRM_MODESET_ACQUIRE_INTERRUPTIBLE, ret);
743
744         if (crtc_req->mode_valid) {
745                 /* If we have a mode we need a framebuffer. */
746                 /* If we pass -1, set the mode with the currently bound fb */
747                 if (crtc_req->fb_id == -1) {
748                         struct drm_framebuffer *old_fb;
749
750                         if (plane->state)
751                                 old_fb = plane->state->fb;
752                         else
753                                 old_fb = plane->fb;
754
755                         if (!old_fb) {
756                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
757                                 ret = -EINVAL;
758                                 goto out;
759                         }
760
761                         fb = old_fb;
762                         /* Make refcounting symmetric with the lookup path. */
763                         drm_framebuffer_get(fb);
764                 } else {
765                         fb = drm_framebuffer_lookup(dev, file_priv, crtc_req->fb_id);
766                         if (!fb) {
767                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
768                                                 crtc_req->fb_id);
769                                 ret = -ENOENT;
770                                 goto out;
771                         }
772                 }
773
774                 mode = drm_mode_create(dev);
775                 if (!mode) {
776                         ret = -ENOMEM;
777                         goto out;
778                 }
779                 if (!file_priv->aspect_ratio_allowed &&
780                     (crtc_req->mode.flags & DRM_MODE_FLAG_PIC_AR_MASK) != DRM_MODE_FLAG_PIC_AR_NONE) {
781                         DRM_DEBUG_KMS("Unexpected aspect-ratio flag bits\n");
782                         ret = -EINVAL;
783                         goto out;
784                 }
785
786
787                 ret = drm_mode_convert_umode(dev, mode, &crtc_req->mode);
788                 if (ret) {
789                         DRM_DEBUG_KMS("Invalid mode (ret=%d, status=%s)\n",
790                                       ret, drm_get_mode_status_name(mode->status));
791                         drm_mode_debug_printmodeline(mode);
792                         goto out;
793                 }
794
795                 /*
796                  * Check whether the primary plane supports the fb pixel format.
797                  * Drivers not implementing the universal planes API use a
798                  * default formats list provided by the DRM core which doesn't
799                  * match real hardware capabilities. Skip the check in that
800                  * case.
801                  */
802                 if (!plane->format_default) {
803                         ret = drm_plane_check_pixel_format(plane,
804                                                            fb->format->format,
805                                                            fb->modifier);
806                         if (ret) {
807                                 DRM_DEBUG_KMS("Invalid pixel format %p4cc, modifier 0x%llx\n",
808                                               &fb->format->format,
809                                               fb->modifier);
810                                 goto out;
811                         }
812                 }
813
814                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
815                                               mode, fb);
816                 if (ret)
817                         goto out;
818
819         }
820
821         if (crtc_req->count_connectors == 0 && mode) {
822                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
823                 ret = -EINVAL;
824                 goto out;
825         }
826
827         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
828                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
829                           crtc_req->count_connectors);
830                 ret = -EINVAL;
831                 goto out;
832         }
833
834         if (crtc_req->count_connectors > 0) {
835                 u32 out_id;
836
837                 /* Avoid unbounded kernel memory allocation */
838                 if (crtc_req->count_connectors > config->num_connector) {
839                         ret = -EINVAL;
840                         goto out;
841                 }
842
843                 connector_set = kmalloc_array(crtc_req->count_connectors,
844                                               sizeof(struct drm_connector *),
845                                               GFP_KERNEL);
846                 if (!connector_set) {
847                         ret = -ENOMEM;
848                         goto out;
849                 }
850
851                 for (i = 0; i < crtc_req->count_connectors; i++) {
852                         connector_set[i] = NULL;
853                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
854                         if (get_user(out_id, &set_connectors_ptr[i])) {
855                                 ret = -EFAULT;
856                                 goto out;
857                         }
858
859                         connector = drm_connector_lookup(dev, file_priv, out_id);
860                         if (!connector) {
861                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
862                                                 out_id);
863                                 ret = -ENOENT;
864                                 goto out;
865                         }
866                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
867                                         connector->base.id,
868                                         connector->name);
869
870                         connector_set[i] = connector;
871                 }
872         }
873
874         set.crtc = crtc;
875         set.x = crtc_req->x;
876         set.y = crtc_req->y;
877         set.mode = mode;
878         set.connectors = connector_set;
879         set.num_connectors = crtc_req->count_connectors;
880         set.fb = fb;
881
882         if (drm_drv_uses_atomic_modeset(dev))
883                 ret = crtc->funcs->set_config(&set, &ctx);
884         else
885                 ret = __drm_mode_set_config_internal(&set, &ctx);
886
887 out:
888         if (fb)
889                 drm_framebuffer_put(fb);
890
891         if (connector_set) {
892                 for (i = 0; i < crtc_req->count_connectors; i++) {
893                         if (connector_set[i])
894                                 drm_connector_put(connector_set[i]);
895                 }
896         }
897         kfree(connector_set);
898         drm_mode_destroy(dev, mode);
899
900         /* In case we need to retry... */
901         connector_set = NULL;
902         fb = NULL;
903         mode = NULL;
904
905         DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
906
907         return ret;
908 }
909
910 int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
911                                struct drm_property *property,
912                                uint64_t value)
913 {
914         int ret = -EINVAL;
915         struct drm_crtc *crtc = obj_to_crtc(obj);
916
917         if (crtc->funcs->set_property)
918                 ret = crtc->funcs->set_property(crtc, property, value);
919         if (!ret)
920                 drm_object_property_set_value(obj, property, value);
921
922         return ret;
923 }
924
925 /**
926  * drm_crtc_create_scaling_filter_property - create a new scaling filter
927  * property
928  *
929  * @crtc: drm CRTC
930  * @supported_filters: bitmask of supported scaling filters, must include
931  *                     BIT(DRM_SCALING_FILTER_DEFAULT).
932  *
933  * This function lets driver to enable the scaling filter property on a given
934  * CRTC.
935  *
936  * RETURNS:
937  * Zero for success or -errno
938  */
939 int drm_crtc_create_scaling_filter_property(struct drm_crtc *crtc,
940                                             unsigned int supported_filters)
941 {
942         struct drm_property *prop =
943                 drm_create_scaling_filter_prop(crtc->dev, supported_filters);
944
945         if (IS_ERR(prop))
946                 return PTR_ERR(prop);
947
948         drm_object_attach_property(&crtc->base, prop,
949                                    DRM_SCALING_FILTER_DEFAULT);
950         crtc->scaling_filter_property = prop;
951
952         return 0;
953 }
954 EXPORT_SYMBOL(drm_crtc_create_scaling_filter_property);