blob: 4986340326e29c3072efc4e066f7947777baf823 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include <drm/drmP.h>
27#include <drm/amdgpu_drm.h>
28#include "amdgpu.h"
29#include "amdgpu_i2c.h"
30#include "atom.h"
31#include "amdgpu_connectors.h"
32#include <asm/div64.h>
33
34#include <linux/pm_runtime.h>
35#include <drm/drm_crtc_helper.h>
36#include <drm/drm_edid.h>
37
Chris Wilsonf54d1862016-10-25 13:00:45 +010038static void amdgpu_flip_callback(struct dma_fence *f, struct dma_fence_cb *cb)
Christian König1ffd2652015-08-11 17:29:52 +020039{
Christian Königc3874b72016-02-11 15:48:30 +010040 struct amdgpu_flip_work *work =
41 container_of(cb, struct amdgpu_flip_work, cb);
Christian König1ffd2652015-08-11 17:29:52 +020042
Chris Wilsonf54d1862016-10-25 13:00:45 +010043 dma_fence_put(f);
Michel Dänzer325cbba2016-08-04 12:39:37 +090044 schedule_work(&work->flip_work.work);
Christian Königc3874b72016-02-11 15:48:30 +010045}
Christian König1ffd2652015-08-11 17:29:52 +020046
Christian Königc3874b72016-02-11 15:48:30 +010047static bool amdgpu_flip_handle_fence(struct amdgpu_flip_work *work,
Chris Wilsonf54d1862016-10-25 13:00:45 +010048 struct dma_fence **f)
Christian Königc3874b72016-02-11 15:48:30 +010049{
Chris Wilsonf54d1862016-10-25 13:00:45 +010050 struct dma_fence *fence= *f;
Christian König1ffd2652015-08-11 17:29:52 +020051
Christian Königc3874b72016-02-11 15:48:30 +010052 if (fence == NULL)
53 return false;
54
Christian König1ffd2652015-08-11 17:29:52 +020055 *f = NULL;
Christian Königc3874b72016-02-11 15:48:30 +010056
Chris Wilsonf54d1862016-10-25 13:00:45 +010057 if (!dma_fence_add_callback(fence, &work->cb, amdgpu_flip_callback))
Christian Königc3874b72016-02-11 15:48:30 +010058 return true;
59
Chris Wilsonf54d1862016-10-25 13:00:45 +010060 dma_fence_put(fence);
Christian Königc3874b72016-02-11 15:48:30 +010061 return false;
Christian König1ffd2652015-08-11 17:29:52 +020062}
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063
64static void amdgpu_flip_work_func(struct work_struct *__work)
65{
Michel Dänzer325cbba2016-08-04 12:39:37 +090066 struct delayed_work *delayed_work =
67 container_of(__work, struct delayed_work, work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068 struct amdgpu_flip_work *work =
Michel Dänzer325cbba2016-08-04 12:39:37 +090069 container_of(delayed_work, struct amdgpu_flip_work, flip_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040070 struct amdgpu_device *adev = work->adev;
Alex Deucherf93932b2016-10-21 16:36:12 -040071 struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[work->crtc_id];
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072
Alex Deucherf93932b2016-10-21 16:36:12 -040073 struct drm_crtc *crtc = &amdgpu_crtc->base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 unsigned long flags;
Michel Dänzer325cbba2016-08-04 12:39:37 +090075 unsigned i;
76 int vpos, hpos;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077
Christian Königc3874b72016-02-11 15:48:30 +010078 if (amdgpu_flip_handle_fence(work, &work->excl))
79 return;
80
Christian König1ffd2652015-08-11 17:29:52 +020081 for (i = 0; i < work->shared_count; ++i)
Christian Königc3874b72016-02-11 15:48:30 +010082 if (amdgpu_flip_handle_fence(work, &work->shared[i]))
83 return;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084
Michel Dänzer325cbba2016-08-04 12:39:37 +090085 /* Wait until we're out of the vertical blank period before the one
86 * targeted by the flip
Alex Deucher8e36f9d2015-12-03 12:31:56 -050087 */
Alex Deucherf93932b2016-10-21 16:36:12 -040088 if (amdgpu_crtc->enabled &&
Michel Dänzer325cbba2016-08-04 12:39:37 +090089 (amdgpu_get_crtc_scanoutpos(adev->ddev, work->crtc_id, 0,
90 &vpos, &hpos, NULL, NULL,
91 &crtc->hwmode)
92 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
93 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
94 (int)(work->target_vblank -
Alex Deucherf93932b2016-10-21 16:36:12 -040095 amdgpu_get_vblank_counter_kms(adev->ddev, amdgpu_crtc->crtc_id)) > 0) {
Michel Dänzer325cbba2016-08-04 12:39:37 +090096 schedule_delayed_work(&work->flip_work, usecs_to_jiffies(1000));
97 return;
Edward O'Callaghan9c3578a2016-07-12 10:17:51 +100098 }
Alex Deucher8e36f9d2015-12-03 12:31:56 -050099
Michel Dänzer325cbba2016-08-04 12:39:37 +0900100 /* We borrow the event spin lock for protecting flip_status */
101 spin_lock_irqsave(&crtc->dev->event_lock, flags);
Mario Kleinere1d09dc2016-02-19 02:06:39 +0100102
Andrey Grodzovskybd4c72d2016-03-30 17:34:27 -0400103 /* Do the flip (mmio) */
Alex Deuchercb9e59d2016-05-05 16:03:57 -0400104 adev->mode_info.funcs->page_flip(adev, work->crtc_id, work->base, work->async);
Andrey Grodzovskybd4c72d2016-03-30 17:34:27 -0400105
106 /* Set the flip status */
Alex Deucherf93932b2016-10-21 16:36:12 -0400107 amdgpu_crtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Vitaly Prosyak6bd9e872015-10-20 15:02:03 -0400109
Andrey Grodzovskybd4c72d2016-03-30 17:34:27 -0400110
111 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_SUBMITTED, work: %p,\n",
Alex Deucherf93932b2016-10-21 16:36:12 -0400112 amdgpu_crtc->crtc_id, amdgpu_crtc, work);
Andrey Grodzovskybd4c72d2016-03-30 17:34:27 -0400113
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114}
115
116/*
117 * Handle unpin events outside the interrupt handler proper.
118 */
119static void amdgpu_unpin_work_func(struct work_struct *__work)
120{
121 struct amdgpu_flip_work *work =
122 container_of(__work, struct amdgpu_flip_work, unpin_work);
123 int r;
124
125 /* unpin of the old buffer */
Christian König765e7fb2016-09-15 15:06:50 +0200126 r = amdgpu_bo_reserve(work->old_abo, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 if (likely(r == 0)) {
Christian König765e7fb2016-09-15 15:06:50 +0200128 r = amdgpu_bo_unpin(work->old_abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129 if (unlikely(r != 0)) {
130 DRM_ERROR("failed to unpin buffer after flip\n");
131 }
Christian König765e7fb2016-09-15 15:06:50 +0200132 amdgpu_bo_unreserve(work->old_abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 } else
134 DRM_ERROR("failed to reserve buffer after flip\n");
135
Christian König765e7fb2016-09-15 15:06:50 +0200136 amdgpu_bo_unref(&work->old_abo);
Christian König1ffd2652015-08-11 17:29:52 +0200137 kfree(work->shared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 kfree(work);
139}
140
Michel Dänzer325cbba2016-08-04 12:39:37 +0900141int amdgpu_crtc_page_flip_target(struct drm_crtc *crtc,
142 struct drm_framebuffer *fb,
143 struct drm_pending_vblank_event *event,
144 uint32_t page_flip_flags, uint32_t target)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145{
146 struct drm_device *dev = crtc->dev;
147 struct amdgpu_device *adev = dev->dev_private;
148 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
149 struct amdgpu_framebuffer *old_amdgpu_fb;
150 struct amdgpu_framebuffer *new_amdgpu_fb;
151 struct drm_gem_object *obj;
152 struct amdgpu_flip_work *work;
Christian König765e7fb2016-09-15 15:06:50 +0200153 struct amdgpu_bo *new_abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154 unsigned long flags;
155 u64 tiling_flags;
156 u64 base;
Christian König1ffd2652015-08-11 17:29:52 +0200157 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158
159 work = kzalloc(sizeof *work, GFP_KERNEL);
160 if (work == NULL)
161 return -ENOMEM;
162
Michel Dänzer325cbba2016-08-04 12:39:37 +0900163 INIT_DELAYED_WORK(&work->flip_work, amdgpu_flip_work_func);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400164 INIT_WORK(&work->unpin_work, amdgpu_unpin_work_func);
165
166 work->event = event;
167 work->adev = adev;
168 work->crtc_id = amdgpu_crtc->crtc_id;
Alex Deuchercb9e59d2016-05-05 16:03:57 -0400169 work->async = (page_flip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170
171 /* schedule unpin of the old buffer */
172 old_amdgpu_fb = to_amdgpu_framebuffer(crtc->primary->fb);
173 obj = old_amdgpu_fb->obj;
174
175 /* take a reference to the old object */
Christian König765e7fb2016-09-15 15:06:50 +0200176 work->old_abo = gem_to_amdgpu_bo(obj);
177 amdgpu_bo_ref(work->old_abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178
179 new_amdgpu_fb = to_amdgpu_framebuffer(fb);
180 obj = new_amdgpu_fb->obj;
Christian König765e7fb2016-09-15 15:06:50 +0200181 new_abo = gem_to_amdgpu_bo(obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182
183 /* pin the new buffer */
Christian König765e7fb2016-09-15 15:06:50 +0200184 r = amdgpu_bo_reserve(new_abo, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 if (unlikely(r != 0)) {
Christian König765e7fb2016-09-15 15:06:50 +0200186 DRM_ERROR("failed to reserve new abo buffer before flip\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400187 goto cleanup;
188 }
189
Alex Deucher7fe28572016-12-07 16:14:38 -0500190 r = amdgpu_bo_pin(new_abo, AMDGPU_GEM_DOMAIN_VRAM, &base);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191 if (unlikely(r != 0)) {
Christian König765e7fb2016-09-15 15:06:50 +0200192 DRM_ERROR("failed to pin new abo buffer before flip\n");
Michel Dänzeree7fd952016-06-24 17:30:08 +0900193 goto unreserve;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400194 }
195
Christian König765e7fb2016-09-15 15:06:50 +0200196 r = reservation_object_get_fences_rcu(new_abo->tbo.resv, &work->excl,
Christian König1ffd2652015-08-11 17:29:52 +0200197 &work->shared_count,
198 &work->shared);
199 if (unlikely(r != 0)) {
Christian König1ffd2652015-08-11 17:29:52 +0200200 DRM_ERROR("failed to get fences for buffer\n");
Michel Dänzeree7fd952016-06-24 17:30:08 +0900201 goto unpin;
Christian König1ffd2652015-08-11 17:29:52 +0200202 }
203
Christian König765e7fb2016-09-15 15:06:50 +0200204 amdgpu_bo_get_tiling_flags(new_abo, &tiling_flags);
205 amdgpu_bo_unreserve(new_abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206
207 work->base = base;
Michel Dänzer325cbba2016-08-04 12:39:37 +0900208 work->target_vblank = target - drm_crtc_vblank_count(crtc) +
209 amdgpu_get_vblank_counter_kms(dev, work->crtc_id);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210
211 /* we borrow the event spin lock for protecting flip_wrok */
212 spin_lock_irqsave(&crtc->dev->event_lock, flags);
213 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_NONE) {
214 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
215 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
216 r = -EBUSY;
Michel Dänzer325cbba2016-08-04 12:39:37 +0900217 goto pflip_cleanup;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400218 }
219
220 amdgpu_crtc->pflip_status = AMDGPU_FLIP_PENDING;
221 amdgpu_crtc->pflip_works = work;
222
Andrey Grodzovskybd4c72d2016-03-30 17:34:27 -0400223
224 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_PENDING, work: %p,\n",
225 amdgpu_crtc->crtc_id, amdgpu_crtc, work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226 /* update crtc fb */
227 crtc->primary->fb = fb;
228 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
Michel Dänzer325cbba2016-08-04 12:39:37 +0900229 amdgpu_flip_work_func(&work->flip_work.work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400230 return 0;
231
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232pflip_cleanup:
Christian König765e7fb2016-09-15 15:06:50 +0200233 if (unlikely(amdgpu_bo_reserve(new_abo, false) != 0)) {
234 DRM_ERROR("failed to reserve new abo in error path\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235 goto cleanup;
236 }
Michel Dänzeree7fd952016-06-24 17:30:08 +0900237unpin:
Christian König765e7fb2016-09-15 15:06:50 +0200238 if (unlikely(amdgpu_bo_unpin(new_abo) != 0)) {
239 DRM_ERROR("failed to unpin new abo in error path\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 }
Michel Dänzeree7fd952016-06-24 17:30:08 +0900241unreserve:
Christian König765e7fb2016-09-15 15:06:50 +0200242 amdgpu_bo_unreserve(new_abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400243
244cleanup:
Christian König765e7fb2016-09-15 15:06:50 +0200245 amdgpu_bo_unref(&work->old_abo);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100246 dma_fence_put(work->excl);
Christian König1ffd2652015-08-11 17:29:52 +0200247 for (i = 0; i < work->shared_count; ++i)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100248 dma_fence_put(work->shared[i]);
Christian König1ffd2652015-08-11 17:29:52 +0200249 kfree(work->shared);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250 kfree(work);
251
252 return r;
253}
254
255int amdgpu_crtc_set_config(struct drm_mode_set *set)
256{
257 struct drm_device *dev;
258 struct amdgpu_device *adev;
259 struct drm_crtc *crtc;
260 bool active = false;
261 int ret;
262
263 if (!set || !set->crtc)
264 return -EINVAL;
265
266 dev = set->crtc->dev;
267
268 ret = pm_runtime_get_sync(dev->dev);
269 if (ret < 0)
270 return ret;
271
272 ret = drm_crtc_helper_set_config(set);
273
274 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
275 if (crtc->enabled)
276 active = true;
277
278 pm_runtime_mark_last_busy(dev->dev);
279
280 adev = dev->dev_private;
281 /* if we have active crtcs and we don't have a power ref,
282 take the current one */
283 if (active && !adev->have_disp_power_ref) {
284 adev->have_disp_power_ref = true;
285 return ret;
286 }
287 /* if we have no active crtcs, then drop the power ref
288 we got before */
289 if (!active && adev->have_disp_power_ref) {
290 pm_runtime_put_autosuspend(dev->dev);
291 adev->have_disp_power_ref = false;
292 }
293
294 /* drop the power reference we got coming in here */
295 pm_runtime_put_autosuspend(dev->dev);
296 return ret;
297}
298
Emily Dengc6e14f42016-08-08 11:30:50 +0800299static const char *encoder_names[41] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400300 "NONE",
301 "INTERNAL_LVDS",
302 "INTERNAL_TMDS1",
303 "INTERNAL_TMDS2",
304 "INTERNAL_DAC1",
305 "INTERNAL_DAC2",
306 "INTERNAL_SDVOA",
307 "INTERNAL_SDVOB",
308 "SI170B",
309 "CH7303",
310 "CH7301",
311 "INTERNAL_DVO1",
312 "EXTERNAL_SDVOA",
313 "EXTERNAL_SDVOB",
314 "TITFP513",
315 "INTERNAL_LVTM1",
316 "VT1623",
317 "HDMI_SI1930",
318 "HDMI_INTERNAL",
319 "INTERNAL_KLDSCP_TMDS1",
320 "INTERNAL_KLDSCP_DVO1",
321 "INTERNAL_KLDSCP_DAC1",
322 "INTERNAL_KLDSCP_DAC2",
323 "SI178",
324 "MVPU_FPGA",
325 "INTERNAL_DDI",
326 "VT1625",
327 "HDMI_SI1932",
328 "DP_AN9801",
329 "DP_DP501",
330 "INTERNAL_UNIPHY",
331 "INTERNAL_KLDSCP_LVTMA",
332 "INTERNAL_UNIPHY1",
333 "INTERNAL_UNIPHY2",
334 "NUTMEG",
335 "TRAVIS",
336 "INTERNAL_VCE",
337 "INTERNAL_UNIPHY3",
Emily Dengc6e14f42016-08-08 11:30:50 +0800338 "HDMI_ANX9805",
339 "INTERNAL_AMCLK",
340 "VIRTUAL",
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400341};
342
343static const char *hpd_names[6] = {
344 "HPD1",
345 "HPD2",
346 "HPD3",
347 "HPD4",
348 "HPD5",
349 "HPD6",
350};
351
352void amdgpu_print_display_setup(struct drm_device *dev)
353{
354 struct drm_connector *connector;
355 struct amdgpu_connector *amdgpu_connector;
356 struct drm_encoder *encoder;
357 struct amdgpu_encoder *amdgpu_encoder;
358 uint32_t devices;
359 int i = 0;
360
361 DRM_INFO("AMDGPU Display Connectors\n");
362 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
363 amdgpu_connector = to_amdgpu_connector(connector);
364 DRM_INFO("Connector %d:\n", i);
365 DRM_INFO(" %s\n", connector->name);
366 if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE)
367 DRM_INFO(" %s\n", hpd_names[amdgpu_connector->hpd.hpd]);
368 if (amdgpu_connector->ddc_bus) {
369 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
370 amdgpu_connector->ddc_bus->rec.mask_clk_reg,
371 amdgpu_connector->ddc_bus->rec.mask_data_reg,
372 amdgpu_connector->ddc_bus->rec.a_clk_reg,
373 amdgpu_connector->ddc_bus->rec.a_data_reg,
374 amdgpu_connector->ddc_bus->rec.en_clk_reg,
375 amdgpu_connector->ddc_bus->rec.en_data_reg,
376 amdgpu_connector->ddc_bus->rec.y_clk_reg,
377 amdgpu_connector->ddc_bus->rec.y_data_reg);
378 if (amdgpu_connector->router.ddc_valid)
379 DRM_INFO(" DDC Router 0x%x/0x%x\n",
380 amdgpu_connector->router.ddc_mux_control_pin,
381 amdgpu_connector->router.ddc_mux_state);
382 if (amdgpu_connector->router.cd_valid)
383 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
384 amdgpu_connector->router.cd_mux_control_pin,
385 amdgpu_connector->router.cd_mux_state);
386 } else {
387 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
388 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
389 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
390 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
391 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
392 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
393 DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
394 }
395 DRM_INFO(" Encoders:\n");
396 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
397 amdgpu_encoder = to_amdgpu_encoder(encoder);
398 devices = amdgpu_encoder->devices & amdgpu_connector->devices;
399 if (devices) {
400 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
401 DRM_INFO(" CRT1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
402 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
403 DRM_INFO(" CRT2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
404 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
405 DRM_INFO(" LCD1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
406 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
407 DRM_INFO(" DFP1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
408 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
409 DRM_INFO(" DFP2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
410 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
411 DRM_INFO(" DFP3: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
412 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
413 DRM_INFO(" DFP4: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
414 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
415 DRM_INFO(" DFP5: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
416 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
417 DRM_INFO(" DFP6: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
418 if (devices & ATOM_DEVICE_TV1_SUPPORT)
419 DRM_INFO(" TV1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
420 if (devices & ATOM_DEVICE_CV_SUPPORT)
421 DRM_INFO(" CV: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
422 }
423 }
424 i++;
425 }
426}
427
428/**
429 * amdgpu_ddc_probe
430 *
431 */
432bool amdgpu_ddc_probe(struct amdgpu_connector *amdgpu_connector,
433 bool use_aux)
434{
435 u8 out = 0x0;
436 u8 buf[8];
437 int ret;
438 struct i2c_msg msgs[] = {
439 {
440 .addr = DDC_ADDR,
441 .flags = 0,
442 .len = 1,
443 .buf = &out,
444 },
445 {
446 .addr = DDC_ADDR,
447 .flags = I2C_M_RD,
448 .len = 8,
449 .buf = buf,
450 }
451 };
452
453 /* on hw with routers, select right port */
454 if (amdgpu_connector->router.ddc_valid)
455 amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
456
457 if (use_aux) {
458 ret = i2c_transfer(&amdgpu_connector->ddc_bus->aux.ddc, msgs, 2);
459 } else {
460 ret = i2c_transfer(&amdgpu_connector->ddc_bus->adapter, msgs, 2);
461 }
462
463 if (ret != 2)
464 /* Couldn't find an accessible DDC on this connector */
465 return false;
466 /* Probe also for valid EDID header
467 * EDID header starts with:
468 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
469 * Only the first 6 bytes must be valid as
470 * drm_edid_block_valid() can fix the last 2 bytes */
471 if (drm_edid_header_is_valid(buf) < 6) {
472 /* Couldn't find an accessible EDID on this
473 * connector */
474 return false;
475 }
476 return true;
477}
478
479static void amdgpu_user_framebuffer_destroy(struct drm_framebuffer *fb)
480{
481 struct amdgpu_framebuffer *amdgpu_fb = to_amdgpu_framebuffer(fb);
482
Markus Elfring1721c692016-07-16 11:28:36 +0200483 drm_gem_object_unreference_unlocked(amdgpu_fb->obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400484 drm_framebuffer_cleanup(fb);
485 kfree(amdgpu_fb);
486}
487
488static int amdgpu_user_framebuffer_create_handle(struct drm_framebuffer *fb,
489 struct drm_file *file_priv,
490 unsigned int *handle)
491{
492 struct amdgpu_framebuffer *amdgpu_fb = to_amdgpu_framebuffer(fb);
493
494 return drm_gem_handle_create(file_priv, amdgpu_fb->obj, handle);
495}
496
497static const struct drm_framebuffer_funcs amdgpu_fb_funcs = {
498 .destroy = amdgpu_user_framebuffer_destroy,
499 .create_handle = amdgpu_user_framebuffer_create_handle,
500};
501
502int
503amdgpu_framebuffer_init(struct drm_device *dev,
504 struct amdgpu_framebuffer *rfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200505 const struct drm_mode_fb_cmd2 *mode_cmd,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506 struct drm_gem_object *obj)
507{
508 int ret;
509 rfb->obj = obj;
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200510 drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
512 if (ret) {
513 rfb->obj = NULL;
514 return ret;
515 }
516 return 0;
517}
518
519static struct drm_framebuffer *
520amdgpu_user_framebuffer_create(struct drm_device *dev,
521 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200522 const struct drm_mode_fb_cmd2 *mode_cmd)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523{
524 struct drm_gem_object *obj;
525 struct amdgpu_framebuffer *amdgpu_fb;
526 int ret;
527
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100528 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 if (obj == NULL) {
530 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
531 "can't create framebuffer\n", mode_cmd->handles[0]);
532 return ERR_PTR(-ENOENT);
533 }
534
535 amdgpu_fb = kzalloc(sizeof(*amdgpu_fb), GFP_KERNEL);
536 if (amdgpu_fb == NULL) {
537 drm_gem_object_unreference_unlocked(obj);
538 return ERR_PTR(-ENOMEM);
539 }
540
541 ret = amdgpu_framebuffer_init(dev, amdgpu_fb, mode_cmd, obj);
542 if (ret) {
543 kfree(amdgpu_fb);
544 drm_gem_object_unreference_unlocked(obj);
545 return ERR_PTR(ret);
546 }
547
548 return &amdgpu_fb->base;
549}
550
551static void amdgpu_output_poll_changed(struct drm_device *dev)
552{
553 struct amdgpu_device *adev = dev->dev_private;
554 amdgpu_fb_output_poll_changed(adev);
555}
556
557const struct drm_mode_config_funcs amdgpu_mode_funcs = {
558 .fb_create = amdgpu_user_framebuffer_create,
559 .output_poll_changed = amdgpu_output_poll_changed
560};
561
Nils Wallméniusf498d9e2016-04-10 16:29:59 +0200562static const struct drm_prop_enum_list amdgpu_underscan_enum_list[] =
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563{ { UNDERSCAN_OFF, "off" },
564 { UNDERSCAN_ON, "on" },
565 { UNDERSCAN_AUTO, "auto" },
566};
567
Nils Wallméniusf498d9e2016-04-10 16:29:59 +0200568static const struct drm_prop_enum_list amdgpu_audio_enum_list[] =
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569{ { AMDGPU_AUDIO_DISABLE, "off" },
570 { AMDGPU_AUDIO_ENABLE, "on" },
571 { AMDGPU_AUDIO_AUTO, "auto" },
572};
573
574/* XXX support different dither options? spatial, temporal, both, etc. */
Nils Wallméniusf498d9e2016-04-10 16:29:59 +0200575static const struct drm_prop_enum_list amdgpu_dither_enum_list[] =
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576{ { AMDGPU_FMT_DITHER_DISABLE, "off" },
577 { AMDGPU_FMT_DITHER_ENABLE, "on" },
578};
579
580int amdgpu_modeset_create_props(struct amdgpu_device *adev)
581{
582 int sz;
583
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +0100584 adev->mode_info.coherent_mode_property =
585 drm_property_create_range(adev->ddev, 0 , "coherent", 0, 1);
586 if (!adev->mode_info.coherent_mode_property)
587 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588
589 adev->mode_info.load_detect_property =
590 drm_property_create_range(adev->ddev, 0, "load detection", 0, 1);
591 if (!adev->mode_info.load_detect_property)
592 return -ENOMEM;
593
594 drm_mode_create_scaling_mode_property(adev->ddev);
595
596 sz = ARRAY_SIZE(amdgpu_underscan_enum_list);
597 adev->mode_info.underscan_property =
598 drm_property_create_enum(adev->ddev, 0,
599 "underscan",
600 amdgpu_underscan_enum_list, sz);
601
602 adev->mode_info.underscan_hborder_property =
603 drm_property_create_range(adev->ddev, 0,
604 "underscan hborder", 0, 128);
605 if (!adev->mode_info.underscan_hborder_property)
606 return -ENOMEM;
607
608 adev->mode_info.underscan_vborder_property =
609 drm_property_create_range(adev->ddev, 0,
610 "underscan vborder", 0, 128);
611 if (!adev->mode_info.underscan_vborder_property)
612 return -ENOMEM;
613
614 sz = ARRAY_SIZE(amdgpu_audio_enum_list);
615 adev->mode_info.audio_property =
616 drm_property_create_enum(adev->ddev, 0,
617 "audio",
618 amdgpu_audio_enum_list, sz);
619
620 sz = ARRAY_SIZE(amdgpu_dither_enum_list);
621 adev->mode_info.dither_property =
622 drm_property_create_enum(adev->ddev, 0,
623 "dither",
624 amdgpu_dither_enum_list, sz);
625
626 return 0;
627}
628
629void amdgpu_update_display_priority(struct amdgpu_device *adev)
630{
631 /* adjustment options for the display watermarks */
632 if ((amdgpu_disp_priority == 0) || (amdgpu_disp_priority > 2))
633 adev->mode_info.disp_priority = 0;
634 else
635 adev->mode_info.disp_priority = amdgpu_disp_priority;
636
637}
638
639static bool is_hdtv_mode(const struct drm_display_mode *mode)
640{
641 /* try and guess if this is a tv or a monitor */
642 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
643 (mode->vdisplay == 576) || /* 576p */
644 (mode->vdisplay == 720) || /* 720p */
645 (mode->vdisplay == 1080)) /* 1080p */
646 return true;
647 else
648 return false;
649}
650
651bool amdgpu_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
652 const struct drm_display_mode *mode,
653 struct drm_display_mode *adjusted_mode)
654{
655 struct drm_device *dev = crtc->dev;
656 struct drm_encoder *encoder;
657 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
658 struct amdgpu_encoder *amdgpu_encoder;
659 struct drm_connector *connector;
660 struct amdgpu_connector *amdgpu_connector;
661 u32 src_v = 1, dst_v = 1;
662 u32 src_h = 1, dst_h = 1;
663
664 amdgpu_crtc->h_border = 0;
665 amdgpu_crtc->v_border = 0;
666
667 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
668 if (encoder->crtc != crtc)
669 continue;
670 amdgpu_encoder = to_amdgpu_encoder(encoder);
671 connector = amdgpu_get_connector_for_encoder(encoder);
672 amdgpu_connector = to_amdgpu_connector(connector);
673
674 /* set scaling */
675 if (amdgpu_encoder->rmx_type == RMX_OFF)
676 amdgpu_crtc->rmx_type = RMX_OFF;
677 else if (mode->hdisplay < amdgpu_encoder->native_mode.hdisplay ||
678 mode->vdisplay < amdgpu_encoder->native_mode.vdisplay)
679 amdgpu_crtc->rmx_type = amdgpu_encoder->rmx_type;
680 else
681 amdgpu_crtc->rmx_type = RMX_OFF;
682 /* copy native mode */
683 memcpy(&amdgpu_crtc->native_mode,
684 &amdgpu_encoder->native_mode,
685 sizeof(struct drm_display_mode));
686 src_v = crtc->mode.vdisplay;
687 dst_v = amdgpu_crtc->native_mode.vdisplay;
688 src_h = crtc->mode.hdisplay;
689 dst_h = amdgpu_crtc->native_mode.hdisplay;
690
691 /* fix up for overscan on hdmi */
692 if ((!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
693 ((amdgpu_encoder->underscan_type == UNDERSCAN_ON) ||
694 ((amdgpu_encoder->underscan_type == UNDERSCAN_AUTO) &&
695 drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
696 is_hdtv_mode(mode)))) {
697 if (amdgpu_encoder->underscan_hborder != 0)
698 amdgpu_crtc->h_border = amdgpu_encoder->underscan_hborder;
699 else
700 amdgpu_crtc->h_border = (mode->hdisplay >> 5) + 16;
701 if (amdgpu_encoder->underscan_vborder != 0)
702 amdgpu_crtc->v_border = amdgpu_encoder->underscan_vborder;
703 else
704 amdgpu_crtc->v_border = (mode->vdisplay >> 5) + 16;
705 amdgpu_crtc->rmx_type = RMX_FULL;
706 src_v = crtc->mode.vdisplay;
707 dst_v = crtc->mode.vdisplay - (amdgpu_crtc->v_border * 2);
708 src_h = crtc->mode.hdisplay;
709 dst_h = crtc->mode.hdisplay - (amdgpu_crtc->h_border * 2);
710 }
711 }
712 if (amdgpu_crtc->rmx_type != RMX_OFF) {
713 fixed20_12 a, b;
714 a.full = dfixed_const(src_v);
715 b.full = dfixed_const(dst_v);
716 amdgpu_crtc->vsc.full = dfixed_div(a, b);
717 a.full = dfixed_const(src_h);
718 b.full = dfixed_const(dst_h);
719 amdgpu_crtc->hsc.full = dfixed_div(a, b);
720 } else {
721 amdgpu_crtc->vsc.full = dfixed_const(1);
722 amdgpu_crtc->hsc.full = dfixed_const(1);
723 }
724 return true;
725}
726
727/*
728 * Retrieve current video scanout position of crtc on a given gpu, and
729 * an optional accurate timestamp of when query happened.
730 *
731 * \param dev Device to query.
Thierry Reding88e72712015-09-24 18:35:31 +0200732 * \param pipe Crtc to query.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400733 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
Alex Deucher8e36f9d2015-12-03 12:31:56 -0500734 * For driver internal use only also supports these flags:
735 *
736 * USE_REAL_VBLANKSTART to use the real start of vblank instead
737 * of a fudged earlier start of vblank.
738 *
739 * GET_DISTANCE_TO_VBLANKSTART to return distance to the
740 * fudged earlier start of vblank in *vpos and the distance
741 * to true start of vblank in *hpos.
742 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400743 * \param *vpos Location where vertical scanout position should be stored.
744 * \param *hpos Location where horizontal scanout position should go.
745 * \param *stime Target location for timestamp taken immediately before
746 * scanout position query. Can be NULL to skip timestamp.
747 * \param *etime Target location for timestamp taken immediately after
748 * scanout position query. Can be NULL to skip timestamp.
749 *
750 * Returns vpos as a positive number while in active scanout area.
751 * Returns vpos as a negative number inside vblank, counting the number
752 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
753 * until start of active scanout / end of vblank."
754 *
755 * \return Flags, or'ed together as follows:
756 *
757 * DRM_SCANOUTPOS_VALID = Query successful.
758 * DRM_SCANOUTPOS_INVBL = Inside vblank.
759 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
760 * this flag means that returned position may be offset by a constant but
761 * unknown small number of scanlines wrt. real scanout position.
762 *
763 */
Thierry Reding88e72712015-09-24 18:35:31 +0200764int amdgpu_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
765 unsigned int flags, int *vpos, int *hpos,
766 ktime_t *stime, ktime_t *etime,
Ville Syrjälä3bb403b2015-09-14 22:43:44 +0300767 const struct drm_display_mode *mode)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400768{
769 u32 vbl = 0, position = 0;
770 int vbl_start, vbl_end, vtotal, ret = 0;
771 bool in_vbl = true;
772
773 struct amdgpu_device *adev = dev->dev_private;
774
775 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
776
777 /* Get optional system timestamp before query. */
778 if (stime)
779 *stime = ktime_get();
780
Thierry Reding88e72712015-09-24 18:35:31 +0200781 if (amdgpu_display_page_flip_get_scanoutpos(adev, pipe, &vbl, &position) == 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400782 ret |= DRM_SCANOUTPOS_VALID;
783
784 /* Get optional system timestamp after query. */
785 if (etime)
786 *etime = ktime_get();
787
788 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
789
790 /* Decode into vertical and horizontal scanout position. */
791 *vpos = position & 0x1fff;
792 *hpos = (position >> 16) & 0x1fff;
793
794 /* Valid vblank area boundaries from gpu retrieved? */
795 if (vbl > 0) {
796 /* Yes: Decode. */
797 ret |= DRM_SCANOUTPOS_ACCURATE;
798 vbl_start = vbl & 0x1fff;
799 vbl_end = (vbl >> 16) & 0x1fff;
800 }
801 else {
802 /* No: Fake something reasonable which gives at least ok results. */
Ville Syrjälä3bb403b2015-09-14 22:43:44 +0300803 vbl_start = mode->crtc_vdisplay;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 vbl_end = 0;
805 }
806
Alex Deucher8e36f9d2015-12-03 12:31:56 -0500807 /* Called from driver internal vblank counter query code? */
808 if (flags & GET_DISTANCE_TO_VBLANKSTART) {
809 /* Caller wants distance from real vbl_start in *hpos */
810 *hpos = *vpos - vbl_start;
811 }
812
813 /* Fudge vblank to start a few scanlines earlier to handle the
814 * problem that vblank irqs fire a few scanlines before start
815 * of vblank. Some driver internal callers need the true vblank
816 * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
817 *
818 * The cause of the "early" vblank irq is that the irq is triggered
819 * by the line buffer logic when the line buffer read position enters
820 * the vblank, whereas our crtc scanout position naturally lags the
821 * line buffer read position.
822 */
823 if (!(flags & USE_REAL_VBLANKSTART))
824 vbl_start -= adev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
825
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400826 /* Test scanout position against vblank region. */
827 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
828 in_vbl = false;
829
Alex Deucher8e36f9d2015-12-03 12:31:56 -0500830 /* In vblank? */
831 if (in_vbl)
832 ret |= DRM_SCANOUTPOS_IN_VBLANK;
833
834 /* Called from driver internal vblank counter query code? */
835 if (flags & GET_DISTANCE_TO_VBLANKSTART) {
836 /* Caller wants distance from fudged earlier vbl_start */
837 *vpos -= vbl_start;
838 return ret;
839 }
840
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841 /* Check if inside vblank area and apply corrective offsets:
842 * vpos will then be >=0 in video scanout area, but negative
843 * within vblank area, counting down the number of lines until
844 * start of scanout.
845 */
846
847 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
848 if (in_vbl && (*vpos >= vbl_start)) {
Ville Syrjälä3bb403b2015-09-14 22:43:44 +0300849 vtotal = mode->crtc_vtotal;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400850 *vpos = *vpos - vtotal;
851 }
852
853 /* Correct for shifted end of vbl at vbl_end. */
854 *vpos = *vpos - vbl_end;
855
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400856 return ret;
857}
858
859int amdgpu_crtc_idx_to_irq_type(struct amdgpu_device *adev, int crtc)
860{
861 if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
862 return AMDGPU_CRTC_IRQ_NONE;
863
864 switch (crtc) {
865 case 0:
866 return AMDGPU_CRTC_IRQ_VBLANK1;
867 case 1:
868 return AMDGPU_CRTC_IRQ_VBLANK2;
869 case 2:
870 return AMDGPU_CRTC_IRQ_VBLANK3;
871 case 3:
872 return AMDGPU_CRTC_IRQ_VBLANK4;
873 case 4:
874 return AMDGPU_CRTC_IRQ_VBLANK5;
875 case 5:
876 return AMDGPU_CRTC_IRQ_VBLANK6;
877 default:
878 return AMDGPU_CRTC_IRQ_NONE;
879 }
880}