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