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