Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.8  riastrad /*	$NetBSD: amdgpu_display.c,v 1.8 2021/12/18 23:44:58 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2007-8 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      8  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      9  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     10  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     12  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     13  1.1  riastrad  *
     14  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     15  1.1  riastrad  * all copies or substantial portions of the Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     21  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     22  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     23  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors: Dave Airlie
     26  1.1  riastrad  *          Alex Deucher
     27  1.1  riastrad  */
     28  1.8  riastrad 
     29  1.1  riastrad #include <sys/cdefs.h>
     30  1.8  riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_display.c,v 1.8 2021/12/18 23:44:58 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include <drm/amdgpu_drm.h>
     33  1.1  riastrad #include "amdgpu.h"
     34  1.1  riastrad #include "amdgpu_i2c.h"
     35  1.1  riastrad #include "atom.h"
     36  1.1  riastrad #include "amdgpu_connectors.h"
     37  1.8  riastrad #include "amdgpu_display.h"
     38  1.1  riastrad #include <asm/div64.h>
     39  1.1  riastrad 
     40  1.8  riastrad #include <linux/pci.h>
     41  1.1  riastrad #include <linux/pm_runtime.h>
     42  1.1  riastrad #include <drm/drm_crtc_helper.h>
     43  1.1  riastrad #include <drm/drm_edid.h>
     44  1.8  riastrad #include <drm/drm_gem_framebuffer_helper.h>
     45  1.8  riastrad #include <drm/drm_fb_helper.h>
     46  1.8  riastrad #include <drm/drm_vblank.h>
     47  1.1  riastrad 
     48  1.8  riastrad static void amdgpu_display_flip_callback(struct dma_fence *f,
     49  1.8  riastrad 					 struct dma_fence_cb *cb)
     50  1.1  riastrad {
     51  1.8  riastrad 	struct amdgpu_flip_work *work =
     52  1.8  riastrad 		container_of(cb, struct amdgpu_flip_work, cb);
     53  1.1  riastrad 
     54  1.8  riastrad 	dma_fence_put(f);
     55  1.8  riastrad 	schedule_work(&work->flip_work.work);
     56  1.8  riastrad }
     57  1.1  riastrad 
     58  1.8  riastrad static bool amdgpu_display_flip_handle_fence(struct amdgpu_flip_work *work,
     59  1.8  riastrad 					     struct dma_fence **f)
     60  1.8  riastrad {
     61  1.8  riastrad 	struct dma_fence *fence= *f;
     62  1.1  riastrad 
     63  1.8  riastrad 	if (fence == NULL)
     64  1.8  riastrad 		return false;
     65  1.1  riastrad 
     66  1.1  riastrad 	*f = NULL;
     67  1.8  riastrad 
     68  1.8  riastrad 	if (!dma_fence_add_callback(fence, &work->cb,
     69  1.8  riastrad 				    amdgpu_display_flip_callback))
     70  1.8  riastrad 		return true;
     71  1.8  riastrad 
     72  1.8  riastrad 	dma_fence_put(fence);
     73  1.8  riastrad 	return false;
     74  1.1  riastrad }
     75  1.1  riastrad 
     76  1.8  riastrad static void amdgpu_display_flip_work_func(struct work_struct *__work)
     77  1.1  riastrad {
     78  1.8  riastrad 	struct delayed_work *delayed_work =
     79  1.8  riastrad 		container_of(__work, struct delayed_work, work);
     80  1.1  riastrad 	struct amdgpu_flip_work *work =
     81  1.8  riastrad 		container_of(delayed_work, struct amdgpu_flip_work, flip_work);
     82  1.1  riastrad 	struct amdgpu_device *adev = work->adev;
     83  1.8  riastrad 	struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[work->crtc_id];
     84  1.1  riastrad 
     85  1.8  riastrad 	struct drm_crtc *crtc = &amdgpu_crtc->base;
     86  1.1  riastrad 	unsigned long flags;
     87  1.8  riastrad 	unsigned i;
     88  1.8  riastrad 	int vpos, hpos;
     89  1.8  riastrad 
     90  1.8  riastrad 	if (amdgpu_display_flip_handle_fence(work, &work->excl))
     91  1.8  riastrad 		return;
     92  1.1  riastrad 
     93  1.1  riastrad 	for (i = 0; i < work->shared_count; ++i)
     94  1.8  riastrad 		if (amdgpu_display_flip_handle_fence(work, &work->shared[i]))
     95  1.8  riastrad 			return;
     96  1.8  riastrad 
     97  1.8  riastrad 	/* Wait until we're out of the vertical blank period before the one
     98  1.8  riastrad 	 * targeted by the flip
     99  1.8  riastrad 	 */
    100  1.8  riastrad 	if (amdgpu_crtc->enabled &&
    101  1.8  riastrad 	    (amdgpu_display_get_crtc_scanoutpos(adev->ddev, work->crtc_id, 0,
    102  1.8  riastrad 						&vpos, &hpos, NULL, NULL,
    103  1.8  riastrad 						&crtc->hwmode)
    104  1.8  riastrad 	     & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
    105  1.8  riastrad 	    (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
    106  1.8  riastrad 	    (int)(work->target_vblank -
    107  1.8  riastrad 		  amdgpu_get_vblank_counter_kms(adev->ddev, amdgpu_crtc->crtc_id)) > 0) {
    108  1.8  riastrad 		schedule_delayed_work(&work->flip_work, usecs_to_jiffies(1000));
    109  1.8  riastrad 		return;
    110  1.8  riastrad 	}
    111  1.1  riastrad 
    112  1.1  riastrad 	/* We borrow the event spin lock for protecting flip_status */
    113  1.1  riastrad 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
    114  1.1  riastrad 
    115  1.8  riastrad 	/* Do the flip (mmio) */
    116  1.8  riastrad 	adev->mode_info.funcs->page_flip(adev, work->crtc_id, work->base, work->async);
    117  1.8  riastrad 
    118  1.8  riastrad 	/* Set the flip status */
    119  1.8  riastrad 	amdgpu_crtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
    120  1.8  riastrad 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
    121  1.1  riastrad 
    122  1.1  riastrad 
    123  1.8  riastrad 	DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_SUBMITTED, work: %p,\n",
    124  1.8  riastrad 					 amdgpu_crtc->crtc_id, amdgpu_crtc, work);
    125  1.1  riastrad 
    126  1.1  riastrad }
    127  1.1  riastrad 
    128  1.1  riastrad /*
    129  1.1  riastrad  * Handle unpin events outside the interrupt handler proper.
    130  1.1  riastrad  */
    131  1.8  riastrad static void amdgpu_display_unpin_work_func(struct work_struct *__work)
    132  1.1  riastrad {
    133  1.1  riastrad 	struct amdgpu_flip_work *work =
    134  1.1  riastrad 		container_of(__work, struct amdgpu_flip_work, unpin_work);
    135  1.1  riastrad 	int r;
    136  1.1  riastrad 
    137  1.1  riastrad 	/* unpin of the old buffer */
    138  1.8  riastrad 	r = amdgpu_bo_reserve(work->old_abo, true);
    139  1.1  riastrad 	if (likely(r == 0)) {
    140  1.8  riastrad 		r = amdgpu_bo_unpin(work->old_abo);
    141  1.1  riastrad 		if (unlikely(r != 0)) {
    142  1.1  riastrad 			DRM_ERROR("failed to unpin buffer after flip\n");
    143  1.1  riastrad 		}
    144  1.8  riastrad 		amdgpu_bo_unreserve(work->old_abo);
    145  1.1  riastrad 	} else
    146  1.1  riastrad 		DRM_ERROR("failed to reserve buffer after flip\n");
    147  1.1  riastrad 
    148  1.8  riastrad 	amdgpu_bo_unref(&work->old_abo);
    149  1.1  riastrad 	kfree(work->shared);
    150  1.1  riastrad 	kfree(work);
    151  1.1  riastrad }
    152  1.1  riastrad 
    153  1.8  riastrad int amdgpu_display_crtc_page_flip_target(struct drm_crtc *crtc,
    154  1.8  riastrad 				struct drm_framebuffer *fb,
    155  1.8  riastrad 				struct drm_pending_vblank_event *event,
    156  1.8  riastrad 				uint32_t page_flip_flags, uint32_t target,
    157  1.8  riastrad 				struct drm_modeset_acquire_ctx *ctx)
    158  1.1  riastrad {
    159  1.1  riastrad 	struct drm_device *dev = crtc->dev;
    160  1.1  riastrad 	struct amdgpu_device *adev = dev->dev_private;
    161  1.1  riastrad 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
    162  1.1  riastrad 	struct drm_gem_object *obj;
    163  1.1  riastrad 	struct amdgpu_flip_work *work;
    164  1.8  riastrad 	struct amdgpu_bo *new_abo;
    165  1.1  riastrad 	unsigned long flags;
    166  1.1  riastrad 	u64 tiling_flags;
    167  1.1  riastrad 	int i, r;
    168  1.1  riastrad 
    169  1.1  riastrad 	work = kzalloc(sizeof *work, GFP_KERNEL);
    170  1.1  riastrad 	if (work == NULL)
    171  1.1  riastrad 		return -ENOMEM;
    172  1.1  riastrad 
    173  1.8  riastrad 	INIT_DELAYED_WORK(&work->flip_work, amdgpu_display_flip_work_func);
    174  1.8  riastrad 	INIT_WORK(&work->unpin_work, amdgpu_display_unpin_work_func);
    175  1.1  riastrad 
    176  1.1  riastrad 	work->event = event;
    177  1.1  riastrad 	work->adev = adev;
    178  1.1  riastrad 	work->crtc_id = amdgpu_crtc->crtc_id;
    179  1.8  riastrad 	work->async = (page_flip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
    180  1.1  riastrad 
    181  1.1  riastrad 	/* schedule unpin of the old buffer */
    182  1.8  riastrad 	obj = crtc->primary->fb->obj[0];
    183  1.1  riastrad 
    184  1.1  riastrad 	/* take a reference to the old object */
    185  1.8  riastrad 	work->old_abo = gem_to_amdgpu_bo(obj);
    186  1.8  riastrad 	amdgpu_bo_ref(work->old_abo);
    187  1.1  riastrad 
    188  1.8  riastrad 	obj = fb->obj[0];
    189  1.8  riastrad 	new_abo = gem_to_amdgpu_bo(obj);
    190  1.1  riastrad 
    191  1.1  riastrad 	/* pin the new buffer */
    192  1.8  riastrad 	r = amdgpu_bo_reserve(new_abo, false);
    193  1.1  riastrad 	if (unlikely(r != 0)) {
    194  1.8  riastrad 		DRM_ERROR("failed to reserve new abo buffer before flip\n");
    195  1.1  riastrad 		goto cleanup;
    196  1.1  riastrad 	}
    197  1.1  riastrad 
    198  1.8  riastrad 	if (!adev->enable_virtual_display) {
    199  1.8  riastrad 		r = amdgpu_bo_pin(new_abo,
    200  1.8  riastrad 				  amdgpu_display_supported_domains(adev, new_abo->flags));
    201  1.8  riastrad 		if (unlikely(r != 0)) {
    202  1.8  riastrad 			DRM_ERROR("failed to pin new abo buffer before flip\n");
    203  1.8  riastrad 			goto unreserve;
    204  1.8  riastrad 		}
    205  1.8  riastrad 	}
    206  1.8  riastrad 
    207  1.8  riastrad 	r = amdgpu_ttm_alloc_gart(&new_abo->tbo);
    208  1.1  riastrad 	if (unlikely(r != 0)) {
    209  1.8  riastrad 		DRM_ERROR("%p bind failed\n", new_abo);
    210  1.8  riastrad 		goto unpin;
    211  1.1  riastrad 	}
    212  1.1  riastrad 
    213  1.8  riastrad 	r = dma_resv_get_fences_rcu(new_abo->tbo.base.resv, &work->excl,
    214  1.1  riastrad 					      &work->shared_count,
    215  1.1  riastrad 					      &work->shared);
    216  1.1  riastrad 	if (unlikely(r != 0)) {
    217  1.1  riastrad 		DRM_ERROR("failed to get fences for buffer\n");
    218  1.8  riastrad 		goto unpin;
    219  1.1  riastrad 	}
    220  1.1  riastrad 
    221  1.8  riastrad 	amdgpu_bo_get_tiling_flags(new_abo, &tiling_flags);
    222  1.8  riastrad 	amdgpu_bo_unreserve(new_abo);
    223  1.1  riastrad 
    224  1.8  riastrad 	if (!adev->enable_virtual_display)
    225  1.8  riastrad 		work->base = amdgpu_bo_gpu_offset(new_abo);
    226  1.8  riastrad 	work->target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
    227  1.8  riastrad 		amdgpu_get_vblank_counter_kms(dev, work->crtc_id);
    228  1.1  riastrad 
    229  1.1  riastrad 	/* we borrow the event spin lock for protecting flip_wrok */
    230  1.1  riastrad 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
    231  1.1  riastrad 	if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_NONE) {
    232  1.1  riastrad 		DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
    233  1.1  riastrad 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
    234  1.1  riastrad 		r = -EBUSY;
    235  1.8  riastrad 		goto pflip_cleanup;
    236  1.1  riastrad 	}
    237  1.1  riastrad 
    238  1.1  riastrad 	amdgpu_crtc->pflip_status = AMDGPU_FLIP_PENDING;
    239  1.1  riastrad 	amdgpu_crtc->pflip_works = work;
    240  1.1  riastrad 
    241  1.8  riastrad 
    242  1.8  riastrad 	DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_PENDING, work: %p,\n",
    243  1.8  riastrad 					 amdgpu_crtc->crtc_id, amdgpu_crtc, work);
    244  1.1  riastrad 	/* update crtc fb */
    245  1.1  riastrad 	crtc->primary->fb = fb;
    246  1.1  riastrad 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
    247  1.8  riastrad 	amdgpu_display_flip_work_func(&work->flip_work.work);
    248  1.1  riastrad 	return 0;
    249  1.1  riastrad 
    250  1.1  riastrad pflip_cleanup:
    251  1.8  riastrad 	if (unlikely(amdgpu_bo_reserve(new_abo, false) != 0)) {
    252  1.8  riastrad 		DRM_ERROR("failed to reserve new abo in error path\n");
    253  1.1  riastrad 		goto cleanup;
    254  1.1  riastrad 	}
    255  1.8  riastrad unpin:
    256  1.8  riastrad 	if (!adev->enable_virtual_display)
    257  1.8  riastrad 		if (unlikely(amdgpu_bo_unpin(new_abo) != 0))
    258  1.8  riastrad 			DRM_ERROR("failed to unpin new abo in error path\n");
    259  1.8  riastrad 
    260  1.8  riastrad unreserve:
    261  1.8  riastrad 	amdgpu_bo_unreserve(new_abo);
    262  1.1  riastrad 
    263  1.1  riastrad cleanup:
    264  1.8  riastrad 	amdgpu_bo_unref(&work->old_abo);
    265  1.8  riastrad 	dma_fence_put(work->excl);
    266  1.1  riastrad 	for (i = 0; i < work->shared_count; ++i)
    267  1.8  riastrad 		dma_fence_put(work->shared[i]);
    268  1.1  riastrad 	kfree(work->shared);
    269  1.1  riastrad 	kfree(work);
    270  1.1  riastrad 
    271  1.1  riastrad 	return r;
    272  1.1  riastrad }
    273  1.1  riastrad 
    274  1.8  riastrad int amdgpu_display_crtc_set_config(struct drm_mode_set *set,
    275  1.8  riastrad 				   struct drm_modeset_acquire_ctx *ctx)
    276  1.1  riastrad {
    277  1.1  riastrad 	struct drm_device *dev;
    278  1.1  riastrad 	struct amdgpu_device *adev;
    279  1.1  riastrad 	struct drm_crtc *crtc;
    280  1.1  riastrad 	bool active = false;
    281  1.1  riastrad 	int ret;
    282  1.1  riastrad 
    283  1.1  riastrad 	if (!set || !set->crtc)
    284  1.1  riastrad 		return -EINVAL;
    285  1.1  riastrad 
    286  1.1  riastrad 	dev = set->crtc->dev;
    287  1.1  riastrad 
    288  1.1  riastrad 	ret = pm_runtime_get_sync(dev->dev);
    289  1.1  riastrad 	if (ret < 0)
    290  1.1  riastrad 		return ret;
    291  1.1  riastrad 
    292  1.8  riastrad 	ret = drm_crtc_helper_set_config(set, ctx);
    293  1.1  riastrad 
    294  1.1  riastrad 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
    295  1.1  riastrad 		if (crtc->enabled)
    296  1.1  riastrad 			active = true;
    297  1.1  riastrad 
    298  1.1  riastrad 	pm_runtime_mark_last_busy(dev->dev);
    299  1.1  riastrad 
    300  1.1  riastrad 	adev = dev->dev_private;
    301  1.1  riastrad 	/* if we have active crtcs and we don't have a power ref,
    302  1.1  riastrad 	   take the current one */
    303  1.1  riastrad 	if (active && !adev->have_disp_power_ref) {
    304  1.1  riastrad 		adev->have_disp_power_ref = true;
    305  1.1  riastrad 		return ret;
    306  1.1  riastrad 	}
    307  1.1  riastrad 	/* if we have no active crtcs, then drop the power ref
    308  1.1  riastrad 	   we got before */
    309  1.1  riastrad 	if (!active && adev->have_disp_power_ref) {
    310  1.1  riastrad 		pm_runtime_put_autosuspend(dev->dev);
    311  1.1  riastrad 		adev->have_disp_power_ref = false;
    312  1.1  riastrad 	}
    313  1.1  riastrad 
    314  1.1  riastrad 	/* drop the power reference we got coming in here */
    315  1.1  riastrad 	pm_runtime_put_autosuspend(dev->dev);
    316  1.1  riastrad 	return ret;
    317  1.1  riastrad }
    318  1.1  riastrad 
    319  1.8  riastrad static const char *encoder_names[41] = {
    320  1.1  riastrad 	"NONE",
    321  1.1  riastrad 	"INTERNAL_LVDS",
    322  1.1  riastrad 	"INTERNAL_TMDS1",
    323  1.1  riastrad 	"INTERNAL_TMDS2",
    324  1.1  riastrad 	"INTERNAL_DAC1",
    325  1.1  riastrad 	"INTERNAL_DAC2",
    326  1.1  riastrad 	"INTERNAL_SDVOA",
    327  1.1  riastrad 	"INTERNAL_SDVOB",
    328  1.1  riastrad 	"SI170B",
    329  1.1  riastrad 	"CH7303",
    330  1.1  riastrad 	"CH7301",
    331  1.1  riastrad 	"INTERNAL_DVO1",
    332  1.1  riastrad 	"EXTERNAL_SDVOA",
    333  1.1  riastrad 	"EXTERNAL_SDVOB",
    334  1.1  riastrad 	"TITFP513",
    335  1.1  riastrad 	"INTERNAL_LVTM1",
    336  1.1  riastrad 	"VT1623",
    337  1.1  riastrad 	"HDMI_SI1930",
    338  1.1  riastrad 	"HDMI_INTERNAL",
    339  1.1  riastrad 	"INTERNAL_KLDSCP_TMDS1",
    340  1.1  riastrad 	"INTERNAL_KLDSCP_DVO1",
    341  1.1  riastrad 	"INTERNAL_KLDSCP_DAC1",
    342  1.1  riastrad 	"INTERNAL_KLDSCP_DAC2",
    343  1.1  riastrad 	"SI178",
    344  1.1  riastrad 	"MVPU_FPGA",
    345  1.1  riastrad 	"INTERNAL_DDI",
    346  1.1  riastrad 	"VT1625",
    347  1.1  riastrad 	"HDMI_SI1932",
    348  1.1  riastrad 	"DP_AN9801",
    349  1.1  riastrad 	"DP_DP501",
    350  1.1  riastrad 	"INTERNAL_UNIPHY",
    351  1.1  riastrad 	"INTERNAL_KLDSCP_LVTMA",
    352  1.1  riastrad 	"INTERNAL_UNIPHY1",
    353  1.1  riastrad 	"INTERNAL_UNIPHY2",
    354  1.1  riastrad 	"NUTMEG",
    355  1.1  riastrad 	"TRAVIS",
    356  1.1  riastrad 	"INTERNAL_VCE",
    357  1.1  riastrad 	"INTERNAL_UNIPHY3",
    358  1.8  riastrad 	"HDMI_ANX9805",
    359  1.8  riastrad 	"INTERNAL_AMCLK",
    360  1.8  riastrad 	"VIRTUAL",
    361  1.1  riastrad };
    362  1.1  riastrad 
    363  1.1  riastrad static const char *hpd_names[6] = {
    364  1.1  riastrad 	"HPD1",
    365  1.1  riastrad 	"HPD2",
    366  1.1  riastrad 	"HPD3",
    367  1.1  riastrad 	"HPD4",
    368  1.1  riastrad 	"HPD5",
    369  1.1  riastrad 	"HPD6",
    370  1.1  riastrad };
    371  1.1  riastrad 
    372  1.8  riastrad void amdgpu_display_print_display_setup(struct drm_device *dev)
    373  1.1  riastrad {
    374  1.1  riastrad 	struct drm_connector *connector;
    375  1.1  riastrad 	struct amdgpu_connector *amdgpu_connector;
    376  1.1  riastrad 	struct drm_encoder *encoder;
    377  1.1  riastrad 	struct amdgpu_encoder *amdgpu_encoder;
    378  1.8  riastrad 	struct drm_connector_list_iter iter;
    379  1.1  riastrad 	uint32_t devices;
    380  1.1  riastrad 	int i = 0;
    381  1.1  riastrad 
    382  1.8  riastrad 	drm_connector_list_iter_begin(dev, &iter);
    383  1.1  riastrad 	DRM_INFO("AMDGPU Display Connectors\n");
    384  1.8  riastrad 	drm_for_each_connector_iter(connector, &iter) {
    385  1.1  riastrad 		amdgpu_connector = to_amdgpu_connector(connector);
    386  1.1  riastrad 		DRM_INFO("Connector %d:\n", i);
    387  1.1  riastrad 		DRM_INFO("  %s\n", connector->name);
    388  1.1  riastrad 		if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE)
    389  1.1  riastrad 			DRM_INFO("  %s\n", hpd_names[amdgpu_connector->hpd.hpd]);
    390  1.1  riastrad 		if (amdgpu_connector->ddc_bus) {
    391  1.1  riastrad 			DRM_INFO("  DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
    392  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.mask_clk_reg,
    393  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.mask_data_reg,
    394  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.a_clk_reg,
    395  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.a_data_reg,
    396  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.en_clk_reg,
    397  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.en_data_reg,
    398  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.y_clk_reg,
    399  1.1  riastrad 				 amdgpu_connector->ddc_bus->rec.y_data_reg);
    400  1.1  riastrad 			if (amdgpu_connector->router.ddc_valid)
    401  1.1  riastrad 				DRM_INFO("  DDC Router 0x%x/0x%x\n",
    402  1.1  riastrad 					 amdgpu_connector->router.ddc_mux_control_pin,
    403  1.1  riastrad 					 amdgpu_connector->router.ddc_mux_state);
    404  1.1  riastrad 			if (amdgpu_connector->router.cd_valid)
    405  1.1  riastrad 				DRM_INFO("  Clock/Data Router 0x%x/0x%x\n",
    406  1.1  riastrad 					 amdgpu_connector->router.cd_mux_control_pin,
    407  1.1  riastrad 					 amdgpu_connector->router.cd_mux_state);
    408  1.1  riastrad 		} else {
    409  1.1  riastrad 			if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
    410  1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
    411  1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
    412  1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
    413  1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
    414  1.1  riastrad 			    connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
    415  1.1  riastrad 				DRM_INFO("  DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati (at) lists.x.org\n");
    416  1.1  riastrad 		}
    417  1.1  riastrad 		DRM_INFO("  Encoders:\n");
    418  1.1  riastrad 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
    419  1.1  riastrad 			amdgpu_encoder = to_amdgpu_encoder(encoder);
    420  1.1  riastrad 			devices = amdgpu_encoder->devices & amdgpu_connector->devices;
    421  1.1  riastrad 			if (devices) {
    422  1.1  riastrad 				if (devices & ATOM_DEVICE_CRT1_SUPPORT)
    423  1.1  riastrad 					DRM_INFO("    CRT1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    424  1.1  riastrad 				if (devices & ATOM_DEVICE_CRT2_SUPPORT)
    425  1.1  riastrad 					DRM_INFO("    CRT2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    426  1.1  riastrad 				if (devices & ATOM_DEVICE_LCD1_SUPPORT)
    427  1.1  riastrad 					DRM_INFO("    LCD1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    428  1.1  riastrad 				if (devices & ATOM_DEVICE_DFP1_SUPPORT)
    429  1.1  riastrad 					DRM_INFO("    DFP1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    430  1.1  riastrad 				if (devices & ATOM_DEVICE_DFP2_SUPPORT)
    431  1.1  riastrad 					DRM_INFO("    DFP2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    432  1.1  riastrad 				if (devices & ATOM_DEVICE_DFP3_SUPPORT)
    433  1.1  riastrad 					DRM_INFO("    DFP3: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    434  1.1  riastrad 				if (devices & ATOM_DEVICE_DFP4_SUPPORT)
    435  1.1  riastrad 					DRM_INFO("    DFP4: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    436  1.1  riastrad 				if (devices & ATOM_DEVICE_DFP5_SUPPORT)
    437  1.1  riastrad 					DRM_INFO("    DFP5: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    438  1.1  riastrad 				if (devices & ATOM_DEVICE_DFP6_SUPPORT)
    439  1.1  riastrad 					DRM_INFO("    DFP6: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    440  1.1  riastrad 				if (devices & ATOM_DEVICE_TV1_SUPPORT)
    441  1.1  riastrad 					DRM_INFO("    TV1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    442  1.1  riastrad 				if (devices & ATOM_DEVICE_CV_SUPPORT)
    443  1.1  riastrad 					DRM_INFO("    CV: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
    444  1.1  riastrad 			}
    445  1.1  riastrad 		}
    446  1.1  riastrad 		i++;
    447  1.1  riastrad 	}
    448  1.8  riastrad 	drm_connector_list_iter_end(&iter);
    449  1.1  riastrad }
    450  1.1  riastrad 
    451  1.1  riastrad /**
    452  1.8  riastrad  * amdgpu_display_ddc_probe
    453  1.1  riastrad  *
    454  1.1  riastrad  */
    455  1.8  riastrad bool amdgpu_display_ddc_probe(struct amdgpu_connector *amdgpu_connector,
    456  1.8  riastrad 			      bool use_aux)
    457  1.1  riastrad {
    458  1.1  riastrad 	u8 out = 0x0;
    459  1.1  riastrad 	u8 buf[8];
    460  1.1  riastrad 	int ret;
    461  1.1  riastrad 	struct i2c_msg msgs[] = {
    462  1.1  riastrad 		{
    463  1.1  riastrad 			.addr = DDC_ADDR,
    464  1.1  riastrad 			.flags = 0,
    465  1.1  riastrad 			.len = 1,
    466  1.1  riastrad 			.buf = &out,
    467  1.1  riastrad 		},
    468  1.1  riastrad 		{
    469  1.1  riastrad 			.addr = DDC_ADDR,
    470  1.1  riastrad 			.flags = I2C_M_RD,
    471  1.1  riastrad 			.len = 8,
    472  1.1  riastrad 			.buf = buf,
    473  1.1  riastrad 		}
    474  1.1  riastrad 	};
    475  1.1  riastrad 
    476  1.1  riastrad 	/* on hw with routers, select right port */
    477  1.1  riastrad 	if (amdgpu_connector->router.ddc_valid)
    478  1.1  riastrad 		amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
    479  1.1  riastrad 
    480  1.1  riastrad 	if (use_aux) {
    481  1.1  riastrad 		ret = i2c_transfer(&amdgpu_connector->ddc_bus->aux.ddc, msgs, 2);
    482  1.1  riastrad 	} else {
    483  1.1  riastrad 		ret = i2c_transfer(&amdgpu_connector->ddc_bus->adapter, msgs, 2);
    484  1.1  riastrad 	}
    485  1.1  riastrad 
    486  1.1  riastrad 	if (ret != 2)
    487  1.1  riastrad 		/* Couldn't find an accessible DDC on this connector */
    488  1.1  riastrad 		return false;
    489  1.1  riastrad 	/* Probe also for valid EDID header
    490  1.1  riastrad 	 * EDID header starts with:
    491  1.1  riastrad 	 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
    492  1.1  riastrad 	 * Only the first 6 bytes must be valid as
    493  1.1  riastrad 	 * drm_edid_block_valid() can fix the last 2 bytes */
    494  1.1  riastrad 	if (drm_edid_header_is_valid(buf) < 6) {
    495  1.1  riastrad 		/* Couldn't find an accessible EDID on this
    496  1.1  riastrad 		 * connector */
    497  1.1  riastrad 		return false;
    498  1.1  riastrad 	}
    499  1.1  riastrad 	return true;
    500  1.1  riastrad }
    501  1.1  riastrad 
    502  1.8  riastrad static const struct drm_framebuffer_funcs amdgpu_fb_funcs = {
    503  1.8  riastrad 	.destroy = drm_gem_fb_destroy,
    504  1.8  riastrad 	.create_handle = drm_gem_fb_create_handle,
    505  1.8  riastrad };
    506  1.8  riastrad 
    507  1.8  riastrad uint32_t amdgpu_display_supported_domains(struct amdgpu_device *adev,
    508  1.8  riastrad 					  uint64_t bo_flags)
    509  1.1  riastrad {
    510  1.8  riastrad 	uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
    511  1.1  riastrad 
    512  1.8  riastrad #if defined(CONFIG_DRM_AMD_DC)
    513  1.8  riastrad 	/*
    514  1.8  riastrad 	 * if amdgpu_bo_support_uswc returns false it means that USWC mappings
    515  1.8  riastrad 	 * is not supported for this board. But this mapping is required
    516  1.8  riastrad 	 * to avoid hang caused by placement of scanout BO in GTT on certain
    517  1.8  riastrad 	 * APUs. So force the BO placement to VRAM in case this architecture
    518  1.8  riastrad 	 * will not allow USWC mappings.
    519  1.8  riastrad 	 * Also, don't allow GTT domain if the BO doens't have USWC falg set.
    520  1.8  riastrad 	 */
    521  1.8  riastrad 	if ((bo_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) &&
    522  1.8  riastrad 	    amdgpu_bo_support_uswc(bo_flags) &&
    523  1.8  riastrad 	    amdgpu_device_asic_has_dc_support(adev->asic_type)) {
    524  1.8  riastrad 		switch (adev->asic_type) {
    525  1.8  riastrad 		case CHIP_CARRIZO:
    526  1.8  riastrad 		case CHIP_STONEY:
    527  1.8  riastrad 			domain |= AMDGPU_GEM_DOMAIN_GTT;
    528  1.8  riastrad 			break;
    529  1.8  riastrad 		case CHIP_RAVEN:
    530  1.8  riastrad 			/* enable S/G on PCO and RV2 */
    531  1.8  riastrad 			if (adev->rev_id >= 0x8 || adev->pdev->device == 0x15d8)
    532  1.8  riastrad 				domain |= AMDGPU_GEM_DOMAIN_GTT;
    533  1.8  riastrad 			break;
    534  1.8  riastrad 		default:
    535  1.8  riastrad 			break;
    536  1.8  riastrad 		}
    537  1.1  riastrad 	}
    538  1.8  riastrad #endif
    539  1.1  riastrad 
    540  1.8  riastrad 	return domain;
    541  1.1  riastrad }
    542  1.1  riastrad 
    543  1.8  riastrad int amdgpu_display_framebuffer_init(struct drm_device *dev,
    544  1.8  riastrad 				    struct amdgpu_framebuffer *rfb,
    545  1.8  riastrad 				    const struct drm_mode_fb_cmd2 *mode_cmd,
    546  1.8  riastrad 				    struct drm_gem_object *obj)
    547  1.1  riastrad {
    548  1.1  riastrad 	int ret;
    549  1.8  riastrad 	rfb->base.obj[0] = obj;
    550  1.8  riastrad 	drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
    551  1.1  riastrad 	ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
    552  1.1  riastrad 	if (ret) {
    553  1.8  riastrad 		rfb->base.obj[0] = NULL;
    554  1.1  riastrad 		return ret;
    555  1.1  riastrad 	}
    556  1.1  riastrad 	return 0;
    557  1.1  riastrad }
    558  1.1  riastrad 
    559  1.8  riastrad struct drm_framebuffer *
    560  1.8  riastrad amdgpu_display_user_framebuffer_create(struct drm_device *dev,
    561  1.8  riastrad 				       struct drm_file *file_priv,
    562  1.8  riastrad 				       const struct drm_mode_fb_cmd2 *mode_cmd)
    563  1.1  riastrad {
    564  1.1  riastrad 	struct drm_gem_object *obj;
    565  1.1  riastrad 	struct amdgpu_framebuffer *amdgpu_fb;
    566  1.1  riastrad 	int ret;
    567  1.1  riastrad 
    568  1.8  riastrad 	obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
    569  1.1  riastrad 	if (obj ==  NULL) {
    570  1.3  riastrad 		dev_err(pci_dev_dev(dev->pdev), "No GEM object associated to handle 0x%08X, "
    571  1.1  riastrad 			"can't create framebuffer\n", mode_cmd->handles[0]);
    572  1.1  riastrad 		return ERR_PTR(-ENOENT);
    573  1.1  riastrad 	}
    574  1.1  riastrad 
    575  1.1  riastrad 	/* Handle is imported dma-buf, so cannot be migrated to VRAM for scanout */
    576  1.1  riastrad 	if (obj->import_attach) {
    577  1.1  riastrad 		DRM_DEBUG_KMS("Cannot create framebuffer from imported dma_buf\n");
    578  1.1  riastrad 		return ERR_PTR(-EINVAL);
    579  1.1  riastrad 	}
    580  1.1  riastrad 
    581  1.1  riastrad 	amdgpu_fb = kzalloc(sizeof(*amdgpu_fb), GFP_KERNEL);
    582  1.1  riastrad 	if (amdgpu_fb == NULL) {
    583  1.8  riastrad 		drm_gem_object_put_unlocked(obj);
    584  1.1  riastrad 		return ERR_PTR(-ENOMEM);
    585  1.1  riastrad 	}
    586  1.1  riastrad 
    587  1.8  riastrad 	ret = amdgpu_display_framebuffer_init(dev, amdgpu_fb, mode_cmd, obj);
    588  1.1  riastrad 	if (ret) {
    589  1.1  riastrad 		kfree(amdgpu_fb);
    590  1.8  riastrad 		drm_gem_object_put_unlocked(obj);
    591  1.1  riastrad 		return ERR_PTR(ret);
    592  1.1  riastrad 	}
    593  1.1  riastrad 
    594  1.1  riastrad 	return &amdgpu_fb->base;
    595  1.1  riastrad }
    596  1.1  riastrad 
    597  1.1  riastrad const struct drm_mode_config_funcs amdgpu_mode_funcs = {
    598  1.8  riastrad 	.fb_create = amdgpu_display_user_framebuffer_create,
    599  1.8  riastrad 	.output_poll_changed = drm_fb_helper_output_poll_changed,
    600  1.1  riastrad };
    601  1.1  riastrad 
    602  1.8  riastrad static const struct drm_prop_enum_list amdgpu_underscan_enum_list[] =
    603  1.1  riastrad {	{ UNDERSCAN_OFF, "off" },
    604  1.1  riastrad 	{ UNDERSCAN_ON, "on" },
    605  1.1  riastrad 	{ UNDERSCAN_AUTO, "auto" },
    606  1.1  riastrad };
    607  1.1  riastrad 
    608  1.8  riastrad static const struct drm_prop_enum_list amdgpu_audio_enum_list[] =
    609  1.1  riastrad {	{ AMDGPU_AUDIO_DISABLE, "off" },
    610  1.1  riastrad 	{ AMDGPU_AUDIO_ENABLE, "on" },
    611  1.1  riastrad 	{ AMDGPU_AUDIO_AUTO, "auto" },
    612  1.1  riastrad };
    613  1.1  riastrad 
    614  1.1  riastrad /* XXX support different dither options? spatial, temporal, both, etc. */
    615  1.8  riastrad static const struct drm_prop_enum_list amdgpu_dither_enum_list[] =
    616  1.1  riastrad {	{ AMDGPU_FMT_DITHER_DISABLE, "off" },
    617  1.1  riastrad 	{ AMDGPU_FMT_DITHER_ENABLE, "on" },
    618  1.1  riastrad };
    619  1.1  riastrad 
    620  1.8  riastrad int amdgpu_display_modeset_create_props(struct amdgpu_device *adev)
    621  1.1  riastrad {
    622  1.1  riastrad 	int sz;
    623  1.1  riastrad 
    624  1.8  riastrad 	adev->mode_info.coherent_mode_property =
    625  1.8  riastrad 		drm_property_create_range(adev->ddev, 0 , "coherent", 0, 1);
    626  1.8  riastrad 	if (!adev->mode_info.coherent_mode_property)
    627  1.8  riastrad 		return -ENOMEM;
    628  1.1  riastrad 
    629  1.1  riastrad 	adev->mode_info.load_detect_property =
    630  1.1  riastrad 		drm_property_create_range(adev->ddev, 0, "load detection", 0, 1);
    631  1.1  riastrad 	if (!adev->mode_info.load_detect_property)
    632  1.1  riastrad 		return -ENOMEM;
    633  1.1  riastrad 
    634  1.1  riastrad 	drm_mode_create_scaling_mode_property(adev->ddev);
    635  1.1  riastrad 
    636  1.1  riastrad 	sz = ARRAY_SIZE(amdgpu_underscan_enum_list);
    637  1.1  riastrad 	adev->mode_info.underscan_property =
    638  1.1  riastrad 		drm_property_create_enum(adev->ddev, 0,
    639  1.1  riastrad 				    "underscan",
    640  1.1  riastrad 				    amdgpu_underscan_enum_list, sz);
    641  1.1  riastrad 
    642  1.1  riastrad 	adev->mode_info.underscan_hborder_property =
    643  1.1  riastrad 		drm_property_create_range(adev->ddev, 0,
    644  1.1  riastrad 					"underscan hborder", 0, 128);
    645  1.1  riastrad 	if (!adev->mode_info.underscan_hborder_property)
    646  1.1  riastrad 		return -ENOMEM;
    647  1.1  riastrad 
    648  1.1  riastrad 	adev->mode_info.underscan_vborder_property =
    649  1.1  riastrad 		drm_property_create_range(adev->ddev, 0,
    650  1.1  riastrad 					"underscan vborder", 0, 128);
    651  1.1  riastrad 	if (!adev->mode_info.underscan_vborder_property)
    652  1.1  riastrad 		return -ENOMEM;
    653  1.1  riastrad 
    654  1.1  riastrad 	sz = ARRAY_SIZE(amdgpu_audio_enum_list);
    655  1.1  riastrad 	adev->mode_info.audio_property =
    656  1.1  riastrad 		drm_property_create_enum(adev->ddev, 0,
    657  1.1  riastrad 					 "audio",
    658  1.1  riastrad 					 amdgpu_audio_enum_list, sz);
    659  1.1  riastrad 
    660  1.1  riastrad 	sz = ARRAY_SIZE(amdgpu_dither_enum_list);
    661  1.1  riastrad 	adev->mode_info.dither_property =
    662  1.1  riastrad 		drm_property_create_enum(adev->ddev, 0,
    663  1.1  riastrad 					 "dither",
    664  1.1  riastrad 					 amdgpu_dither_enum_list, sz);
    665  1.1  riastrad 
    666  1.8  riastrad 	if (amdgpu_device_has_dc_support(adev)) {
    667  1.8  riastrad 		adev->mode_info.abm_level_property =
    668  1.8  riastrad 			drm_property_create_range(adev->ddev, 0,
    669  1.8  riastrad 						"abm level", 0, 4);
    670  1.8  riastrad 		if (!adev->mode_info.abm_level_property)
    671  1.8  riastrad 			return -ENOMEM;
    672  1.8  riastrad 	}
    673  1.8  riastrad 
    674  1.1  riastrad 	return 0;
    675  1.1  riastrad }
    676  1.1  riastrad 
    677  1.8  riastrad void amdgpu_display_update_priority(struct amdgpu_device *adev)
    678  1.1  riastrad {
    679  1.1  riastrad 	/* adjustment options for the display watermarks */
    680  1.1  riastrad 	if ((amdgpu_disp_priority == 0) || (amdgpu_disp_priority > 2))
    681  1.1  riastrad 		adev->mode_info.disp_priority = 0;
    682  1.1  riastrad 	else
    683  1.1  riastrad 		adev->mode_info.disp_priority = amdgpu_disp_priority;
    684  1.1  riastrad 
    685  1.1  riastrad }
    686  1.1  riastrad 
    687  1.8  riastrad static bool amdgpu_display_is_hdtv_mode(const struct drm_display_mode *mode)
    688  1.1  riastrad {
    689  1.1  riastrad 	/* try and guess if this is a tv or a monitor */
    690  1.1  riastrad 	if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
    691  1.1  riastrad 	    (mode->vdisplay == 576) || /* 576p */
    692  1.1  riastrad 	    (mode->vdisplay == 720) || /* 720p */
    693  1.1  riastrad 	    (mode->vdisplay == 1080)) /* 1080p */
    694  1.1  riastrad 		return true;
    695  1.1  riastrad 	else
    696  1.1  riastrad 		return false;
    697  1.1  riastrad }
    698  1.1  riastrad 
    699  1.8  riastrad bool amdgpu_display_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
    700  1.8  riastrad 					const struct drm_display_mode *mode,
    701  1.8  riastrad 					struct drm_display_mode *adjusted_mode)
    702  1.1  riastrad {
    703  1.1  riastrad 	struct drm_device *dev = crtc->dev;
    704  1.1  riastrad 	struct drm_encoder *encoder;
    705  1.1  riastrad 	struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
    706  1.1  riastrad 	struct amdgpu_encoder *amdgpu_encoder;
    707  1.1  riastrad 	struct drm_connector *connector;
    708  1.1  riastrad 	u32 src_v = 1, dst_v = 1;
    709  1.1  riastrad 	u32 src_h = 1, dst_h = 1;
    710  1.1  riastrad 
    711  1.1  riastrad 	amdgpu_crtc->h_border = 0;
    712  1.1  riastrad 	amdgpu_crtc->v_border = 0;
    713  1.1  riastrad 
    714  1.1  riastrad 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
    715  1.1  riastrad 		if (encoder->crtc != crtc)
    716  1.1  riastrad 			continue;
    717  1.1  riastrad 		amdgpu_encoder = to_amdgpu_encoder(encoder);
    718  1.1  riastrad 		connector = amdgpu_get_connector_for_encoder(encoder);
    719  1.1  riastrad 
    720  1.1  riastrad 		/* set scaling */
    721  1.1  riastrad 		if (amdgpu_encoder->rmx_type == RMX_OFF)
    722  1.1  riastrad 			amdgpu_crtc->rmx_type = RMX_OFF;
    723  1.1  riastrad 		else if (mode->hdisplay < amdgpu_encoder->native_mode.hdisplay ||
    724  1.1  riastrad 			 mode->vdisplay < amdgpu_encoder->native_mode.vdisplay)
    725  1.1  riastrad 			amdgpu_crtc->rmx_type = amdgpu_encoder->rmx_type;
    726  1.1  riastrad 		else
    727  1.1  riastrad 			amdgpu_crtc->rmx_type = RMX_OFF;
    728  1.1  riastrad 		/* copy native mode */
    729  1.1  riastrad 		memcpy(&amdgpu_crtc->native_mode,
    730  1.1  riastrad 		       &amdgpu_encoder->native_mode,
    731  1.1  riastrad 		       sizeof(struct drm_display_mode));
    732  1.1  riastrad 		src_v = crtc->mode.vdisplay;
    733  1.1  riastrad 		dst_v = amdgpu_crtc->native_mode.vdisplay;
    734  1.1  riastrad 		src_h = crtc->mode.hdisplay;
    735  1.1  riastrad 		dst_h = amdgpu_crtc->native_mode.hdisplay;
    736  1.1  riastrad 
    737  1.1  riastrad 		/* fix up for overscan on hdmi */
    738  1.1  riastrad 		if ((!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
    739  1.1  riastrad 		    ((amdgpu_encoder->underscan_type == UNDERSCAN_ON) ||
    740  1.1  riastrad 		     ((amdgpu_encoder->underscan_type == UNDERSCAN_AUTO) &&
    741  1.1  riastrad 		      drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
    742  1.8  riastrad 		      amdgpu_display_is_hdtv_mode(mode)))) {
    743  1.1  riastrad 			if (amdgpu_encoder->underscan_hborder != 0)
    744  1.1  riastrad 				amdgpu_crtc->h_border = amdgpu_encoder->underscan_hborder;
    745  1.1  riastrad 			else
    746  1.1  riastrad 				amdgpu_crtc->h_border = (mode->hdisplay >> 5) + 16;
    747  1.1  riastrad 			if (amdgpu_encoder->underscan_vborder != 0)
    748  1.1  riastrad 				amdgpu_crtc->v_border = amdgpu_encoder->underscan_vborder;
    749  1.1  riastrad 			else
    750  1.1  riastrad 				amdgpu_crtc->v_border = (mode->vdisplay >> 5) + 16;
    751  1.1  riastrad 			amdgpu_crtc->rmx_type = RMX_FULL;
    752  1.1  riastrad 			src_v = crtc->mode.vdisplay;
    753  1.1  riastrad 			dst_v = crtc->mode.vdisplay - (amdgpu_crtc->v_border * 2);
    754  1.1  riastrad 			src_h = crtc->mode.hdisplay;
    755  1.1  riastrad 			dst_h = crtc->mode.hdisplay - (amdgpu_crtc->h_border * 2);
    756  1.1  riastrad 		}
    757  1.1  riastrad 	}
    758  1.1  riastrad 	if (amdgpu_crtc->rmx_type != RMX_OFF) {
    759  1.1  riastrad 		fixed20_12 a, b;
    760  1.1  riastrad 		a.full = dfixed_const(src_v);
    761  1.1  riastrad 		b.full = dfixed_const(dst_v);
    762  1.1  riastrad 		amdgpu_crtc->vsc.full = dfixed_div(a, b);
    763  1.1  riastrad 		a.full = dfixed_const(src_h);
    764  1.1  riastrad 		b.full = dfixed_const(dst_h);
    765  1.1  riastrad 		amdgpu_crtc->hsc.full = dfixed_div(a, b);
    766  1.1  riastrad 	} else {
    767  1.1  riastrad 		amdgpu_crtc->vsc.full = dfixed_const(1);
    768  1.1  riastrad 		amdgpu_crtc->hsc.full = dfixed_const(1);
    769  1.1  riastrad 	}
    770  1.1  riastrad 	return true;
    771  1.1  riastrad }
    772  1.1  riastrad 
    773  1.1  riastrad /*
    774  1.1  riastrad  * Retrieve current video scanout position of crtc on a given gpu, and
    775  1.1  riastrad  * an optional accurate timestamp of when query happened.
    776  1.1  riastrad  *
    777  1.1  riastrad  * \param dev Device to query.
    778  1.1  riastrad  * \param pipe Crtc to query.
    779  1.1  riastrad  * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
    780  1.1  riastrad  *              For driver internal use only also supports these flags:
    781  1.1  riastrad  *
    782  1.1  riastrad  *              USE_REAL_VBLANKSTART to use the real start of vblank instead
    783  1.1  riastrad  *              of a fudged earlier start of vblank.
    784  1.1  riastrad  *
    785  1.1  riastrad  *              GET_DISTANCE_TO_VBLANKSTART to return distance to the
    786  1.1  riastrad  *              fudged earlier start of vblank in *vpos and the distance
    787  1.1  riastrad  *              to true start of vblank in *hpos.
    788  1.1  riastrad  *
    789  1.1  riastrad  * \param *vpos Location where vertical scanout position should be stored.
    790  1.1  riastrad  * \param *hpos Location where horizontal scanout position should go.
    791  1.1  riastrad  * \param *stime Target location for timestamp taken immediately before
    792  1.1  riastrad  *               scanout position query. Can be NULL to skip timestamp.
    793  1.1  riastrad  * \param *etime Target location for timestamp taken immediately after
    794  1.1  riastrad  *               scanout position query. Can be NULL to skip timestamp.
    795  1.1  riastrad  *
    796  1.1  riastrad  * Returns vpos as a positive number while in active scanout area.
    797  1.1  riastrad  * Returns vpos as a negative number inside vblank, counting the number
    798  1.1  riastrad  * of scanlines to go until end of vblank, e.g., -1 means "one scanline
    799  1.1  riastrad  * until start of active scanout / end of vblank."
    800  1.1  riastrad  *
    801  1.1  riastrad  * \return Flags, or'ed together as follows:
    802  1.1  riastrad  *
    803  1.1  riastrad  * DRM_SCANOUTPOS_VALID = Query successful.
    804  1.1  riastrad  * DRM_SCANOUTPOS_INVBL = Inside vblank.
    805  1.1  riastrad  * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
    806  1.1  riastrad  * this flag means that returned position may be offset by a constant but
    807  1.1  riastrad  * unknown small number of scanlines wrt. real scanout position.
    808  1.1  riastrad  *
    809  1.1  riastrad  */
    810  1.8  riastrad int amdgpu_display_get_crtc_scanoutpos(struct drm_device *dev,
    811  1.8  riastrad 			unsigned int pipe, unsigned int flags, int *vpos,
    812  1.8  riastrad 			int *hpos, ktime_t *stime, ktime_t *etime,
    813  1.8  riastrad 			const struct drm_display_mode *mode)
    814  1.1  riastrad {
    815  1.1  riastrad 	u32 vbl = 0, position = 0;
    816  1.1  riastrad 	int vbl_start, vbl_end, vtotal, ret = 0;
    817  1.1  riastrad 	bool in_vbl = true;
    818  1.1  riastrad 
    819  1.1  riastrad 	struct amdgpu_device *adev = dev->dev_private;
    820  1.1  riastrad 
    821  1.1  riastrad 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
    822  1.1  riastrad 
    823  1.1  riastrad 	/* Get optional system timestamp before query. */
    824  1.1  riastrad 	if (stime)
    825  1.1  riastrad 		*stime = ktime_get();
    826  1.1  riastrad 
    827  1.1  riastrad 	if (amdgpu_display_page_flip_get_scanoutpos(adev, pipe, &vbl, &position) == 0)
    828  1.1  riastrad 		ret |= DRM_SCANOUTPOS_VALID;
    829  1.1  riastrad 
    830  1.1  riastrad 	/* Get optional system timestamp after query. */
    831  1.1  riastrad 	if (etime)
    832  1.1  riastrad 		*etime = ktime_get();
    833  1.1  riastrad 
    834  1.1  riastrad 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
    835  1.1  riastrad 
    836  1.1  riastrad 	/* Decode into vertical and horizontal scanout position. */
    837  1.1  riastrad 	*vpos = position & 0x1fff;
    838  1.1  riastrad 	*hpos = (position >> 16) & 0x1fff;
    839  1.1  riastrad 
    840  1.1  riastrad 	/* Valid vblank area boundaries from gpu retrieved? */
    841  1.1  riastrad 	if (vbl > 0) {
    842  1.1  riastrad 		/* Yes: Decode. */
    843  1.1  riastrad 		ret |= DRM_SCANOUTPOS_ACCURATE;
    844  1.1  riastrad 		vbl_start = vbl & 0x1fff;
    845  1.1  riastrad 		vbl_end = (vbl >> 16) & 0x1fff;
    846  1.1  riastrad 	}
    847  1.1  riastrad 	else {
    848  1.1  riastrad 		/* No: Fake something reasonable which gives at least ok results. */
    849  1.1  riastrad 		vbl_start = mode->crtc_vdisplay;
    850  1.1  riastrad 		vbl_end = 0;
    851  1.1  riastrad 	}
    852  1.1  riastrad 
    853  1.1  riastrad 	/* Called from driver internal vblank counter query code? */
    854  1.1  riastrad 	if (flags & GET_DISTANCE_TO_VBLANKSTART) {
    855  1.1  riastrad 	    /* Caller wants distance from real vbl_start in *hpos */
    856  1.1  riastrad 	    *hpos = *vpos - vbl_start;
    857  1.1  riastrad 	}
    858  1.1  riastrad 
    859  1.1  riastrad 	/* Fudge vblank to start a few scanlines earlier to handle the
    860  1.1  riastrad 	 * problem that vblank irqs fire a few scanlines before start
    861  1.1  riastrad 	 * of vblank. Some driver internal callers need the true vblank
    862  1.1  riastrad 	 * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
    863  1.1  riastrad 	 *
    864  1.1  riastrad 	 * The cause of the "early" vblank irq is that the irq is triggered
    865  1.1  riastrad 	 * by the line buffer logic when the line buffer read position enters
    866  1.1  riastrad 	 * the vblank, whereas our crtc scanout position naturally lags the
    867  1.1  riastrad 	 * line buffer read position.
    868  1.1  riastrad 	 */
    869  1.1  riastrad 	if (!(flags & USE_REAL_VBLANKSTART))
    870  1.1  riastrad 		vbl_start -= adev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
    871  1.1  riastrad 
    872  1.1  riastrad 	/* Test scanout position against vblank region. */
    873  1.1  riastrad 	if ((*vpos < vbl_start) && (*vpos >= vbl_end))
    874  1.1  riastrad 		in_vbl = false;
    875  1.1  riastrad 
    876  1.1  riastrad 	/* In vblank? */
    877  1.1  riastrad 	if (in_vbl)
    878  1.1  riastrad 	    ret |= DRM_SCANOUTPOS_IN_VBLANK;
    879  1.1  riastrad 
    880  1.1  riastrad 	/* Called from driver internal vblank counter query code? */
    881  1.1  riastrad 	if (flags & GET_DISTANCE_TO_VBLANKSTART) {
    882  1.1  riastrad 		/* Caller wants distance from fudged earlier vbl_start */
    883  1.1  riastrad 		*vpos -= vbl_start;
    884  1.1  riastrad 		return ret;
    885  1.1  riastrad 	}
    886  1.1  riastrad 
    887  1.1  riastrad 	/* Check if inside vblank area and apply corrective offsets:
    888  1.1  riastrad 	 * vpos will then be >=0 in video scanout area, but negative
    889  1.1  riastrad 	 * within vblank area, counting down the number of lines until
    890  1.1  riastrad 	 * start of scanout.
    891  1.1  riastrad 	 */
    892  1.1  riastrad 
    893  1.1  riastrad 	/* Inside "upper part" of vblank area? Apply corrective offset if so: */
    894  1.1  riastrad 	if (in_vbl && (*vpos >= vbl_start)) {
    895  1.1  riastrad 		vtotal = mode->crtc_vtotal;
    896  1.8  riastrad 
    897  1.8  riastrad 		/* With variable refresh rate displays the vpos can exceed
    898  1.8  riastrad 		 * the vtotal value. Clamp to 0 to return -vbl_end instead
    899  1.8  riastrad 		 * of guessing the remaining number of lines until scanout.
    900  1.8  riastrad 		 */
    901  1.8  riastrad 		*vpos = (*vpos < vtotal) ? (*vpos - vtotal) : 0;
    902  1.1  riastrad 	}
    903  1.1  riastrad 
    904  1.1  riastrad 	/* Correct for shifted end of vbl at vbl_end. */
    905  1.1  riastrad 	*vpos = *vpos - vbl_end;
    906  1.1  riastrad 
    907  1.1  riastrad 	return ret;
    908  1.1  riastrad }
    909  1.1  riastrad 
    910  1.8  riastrad int amdgpu_display_crtc_idx_to_irq_type(struct amdgpu_device *adev, int crtc)
    911  1.1  riastrad {
    912  1.1  riastrad 	if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
    913  1.1  riastrad 		return AMDGPU_CRTC_IRQ_NONE;
    914  1.1  riastrad 
    915  1.1  riastrad 	switch (crtc) {
    916  1.1  riastrad 	case 0:
    917  1.1  riastrad 		return AMDGPU_CRTC_IRQ_VBLANK1;
    918  1.1  riastrad 	case 1:
    919  1.1  riastrad 		return AMDGPU_CRTC_IRQ_VBLANK2;
    920  1.1  riastrad 	case 2:
    921  1.1  riastrad 		return AMDGPU_CRTC_IRQ_VBLANK3;
    922  1.1  riastrad 	case 3:
    923  1.1  riastrad 		return AMDGPU_CRTC_IRQ_VBLANK4;
    924  1.1  riastrad 	case 4:
    925  1.1  riastrad 		return AMDGPU_CRTC_IRQ_VBLANK5;
    926  1.1  riastrad 	case 5:
    927  1.1  riastrad 		return AMDGPU_CRTC_IRQ_VBLANK6;
    928  1.1  riastrad 	default:
    929  1.1  riastrad 		return AMDGPU_CRTC_IRQ_NONE;
    930  1.1  riastrad 	}
    931  1.1  riastrad }
    932