]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_display.c
drm/amd/display: Set new format info for converted metadata.
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_display.c
1 /*
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
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 "amdgpu_display.h"
33 #include <asm/div64.h>
34
35 #include <linux/pci.h>
36 #include <linux/pm_runtime.h>
37 #include <drm/drm_crtc_helper.h>
38 #include <drm/drm_edid.h>
39 #include <drm/drm_gem_framebuffer_helper.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_fourcc.h>
42 #include <drm/drm_vblank.h>
43
44 static void amdgpu_display_flip_callback(struct dma_fence *f,
45                                          struct dma_fence_cb *cb)
46 {
47         struct amdgpu_flip_work *work =
48                 container_of(cb, struct amdgpu_flip_work, cb);
49
50         dma_fence_put(f);
51         schedule_work(&work->flip_work.work);
52 }
53
54 static bool amdgpu_display_flip_handle_fence(struct amdgpu_flip_work *work,
55                                              struct dma_fence **f)
56 {
57         struct dma_fence *fence= *f;
58
59         if (fence == NULL)
60                 return false;
61
62         *f = NULL;
63
64         if (!dma_fence_add_callback(fence, &work->cb,
65                                     amdgpu_display_flip_callback))
66                 return true;
67
68         dma_fence_put(fence);
69         return false;
70 }
71
72 static void amdgpu_display_flip_work_func(struct work_struct *__work)
73 {
74         struct delayed_work *delayed_work =
75                 container_of(__work, struct delayed_work, work);
76         struct amdgpu_flip_work *work =
77                 container_of(delayed_work, struct amdgpu_flip_work, flip_work);
78         struct amdgpu_device *adev = work->adev;
79         struct amdgpu_crtc *amdgpu_crtc = adev->mode_info.crtcs[work->crtc_id];
80
81         struct drm_crtc *crtc = &amdgpu_crtc->base;
82         unsigned long flags;
83         unsigned i;
84         int vpos, hpos;
85
86         if (amdgpu_display_flip_handle_fence(work, &work->excl))
87                 return;
88
89         for (i = 0; i < work->shared_count; ++i)
90                 if (amdgpu_display_flip_handle_fence(work, &work->shared[i]))
91                         return;
92
93         /* Wait until we're out of the vertical blank period before the one
94          * targeted by the flip
95          */
96         if (amdgpu_crtc->enabled &&
97             (amdgpu_display_get_crtc_scanoutpos(adev_to_drm(adev), work->crtc_id, 0,
98                                                 &vpos, &hpos, NULL, NULL,
99                                                 &crtc->hwmode)
100              & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
101             (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
102             (int)(work->target_vblank -
103                   amdgpu_get_vblank_counter_kms(crtc)) > 0) {
104                 schedule_delayed_work(&work->flip_work, usecs_to_jiffies(1000));
105                 return;
106         }
107
108         /* We borrow the event spin lock for protecting flip_status */
109         spin_lock_irqsave(&crtc->dev->event_lock, flags);
110
111         /* Do the flip (mmio) */
112         adev->mode_info.funcs->page_flip(adev, work->crtc_id, work->base, work->async);
113
114         /* Set the flip status */
115         amdgpu_crtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
116         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
117
118
119         DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_SUBMITTED, work: %p,\n",
120                                          amdgpu_crtc->crtc_id, amdgpu_crtc, work);
121
122 }
123
124 /*
125  * Handle unpin events outside the interrupt handler proper.
126  */
127 static void amdgpu_display_unpin_work_func(struct work_struct *__work)
128 {
129         struct amdgpu_flip_work *work =
130                 container_of(__work, struct amdgpu_flip_work, unpin_work);
131         int r;
132
133         /* unpin of the old buffer */
134         r = amdgpu_bo_reserve(work->old_abo, true);
135         if (likely(r == 0)) {
136                 amdgpu_bo_unpin(work->old_abo);
137                 amdgpu_bo_unreserve(work->old_abo);
138         } else
139                 DRM_ERROR("failed to reserve buffer after flip\n");
140
141         amdgpu_bo_unref(&work->old_abo);
142         kfree(work->shared);
143         kfree(work);
144 }
145
146 int amdgpu_display_crtc_page_flip_target(struct drm_crtc *crtc,
147                                 struct drm_framebuffer *fb,
148                                 struct drm_pending_vblank_event *event,
149                                 uint32_t page_flip_flags, uint32_t target,
150                                 struct drm_modeset_acquire_ctx *ctx)
151 {
152         struct drm_device *dev = crtc->dev;
153         struct amdgpu_device *adev = drm_to_adev(dev);
154         struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
155         struct drm_gem_object *obj;
156         struct amdgpu_flip_work *work;
157         struct amdgpu_bo *new_abo;
158         unsigned long flags;
159         u64 tiling_flags;
160         int i, r;
161
162         work = kzalloc(sizeof *work, GFP_KERNEL);
163         if (work == NULL)
164                 return -ENOMEM;
165
166         INIT_DELAYED_WORK(&work->flip_work, amdgpu_display_flip_work_func);
167         INIT_WORK(&work->unpin_work, amdgpu_display_unpin_work_func);
168
169         work->event = event;
170         work->adev = adev;
171         work->crtc_id = amdgpu_crtc->crtc_id;
172         work->async = (page_flip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
173
174         /* schedule unpin of the old buffer */
175         obj = crtc->primary->fb->obj[0];
176
177         /* take a reference to the old object */
178         work->old_abo = gem_to_amdgpu_bo(obj);
179         amdgpu_bo_ref(work->old_abo);
180
181         obj = fb->obj[0];
182         new_abo = gem_to_amdgpu_bo(obj);
183
184         /* pin the new buffer */
185         r = amdgpu_bo_reserve(new_abo, false);
186         if (unlikely(r != 0)) {
187                 DRM_ERROR("failed to reserve new abo buffer before flip\n");
188                 goto cleanup;
189         }
190
191         if (!adev->enable_virtual_display) {
192                 r = amdgpu_bo_pin(new_abo,
193                                   amdgpu_display_supported_domains(adev, new_abo->flags));
194                 if (unlikely(r != 0)) {
195                         DRM_ERROR("failed to pin new abo buffer before flip\n");
196                         goto unreserve;
197                 }
198         }
199
200         r = amdgpu_ttm_alloc_gart(&new_abo->tbo);
201         if (unlikely(r != 0)) {
202                 DRM_ERROR("%p bind failed\n", new_abo);
203                 goto unpin;
204         }
205
206         r = dma_resv_get_fences_rcu(new_abo->tbo.base.resv, &work->excl,
207                                               &work->shared_count,
208                                               &work->shared);
209         if (unlikely(r != 0)) {
210                 DRM_ERROR("failed to get fences for buffer\n");
211                 goto unpin;
212         }
213
214         amdgpu_bo_get_tiling_flags(new_abo, &tiling_flags);
215         amdgpu_bo_unreserve(new_abo);
216
217         if (!adev->enable_virtual_display)
218                 work->base = amdgpu_bo_gpu_offset(new_abo);
219         work->target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
220                 amdgpu_get_vblank_counter_kms(crtc);
221
222         /* we borrow the event spin lock for protecting flip_wrok */
223         spin_lock_irqsave(&crtc->dev->event_lock, flags);
224         if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_NONE) {
225                 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
226                 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
227                 r = -EBUSY;
228                 goto pflip_cleanup;
229         }
230
231         amdgpu_crtc->pflip_status = AMDGPU_FLIP_PENDING;
232         amdgpu_crtc->pflip_works = work;
233
234
235         DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_PENDING, work: %p,\n",
236                                          amdgpu_crtc->crtc_id, amdgpu_crtc, work);
237         /* update crtc fb */
238         crtc->primary->fb = fb;
239         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
240         amdgpu_display_flip_work_func(&work->flip_work.work);
241         return 0;
242
243 pflip_cleanup:
244         if (unlikely(amdgpu_bo_reserve(new_abo, false) != 0)) {
245                 DRM_ERROR("failed to reserve new abo in error path\n");
246                 goto cleanup;
247         }
248 unpin:
249         if (!adev->enable_virtual_display)
250                 amdgpu_bo_unpin(new_abo);
251
252 unreserve:
253         amdgpu_bo_unreserve(new_abo);
254
255 cleanup:
256         amdgpu_bo_unref(&work->old_abo);
257         dma_fence_put(work->excl);
258         for (i = 0; i < work->shared_count; ++i)
259                 dma_fence_put(work->shared[i]);
260         kfree(work->shared);
261         kfree(work);
262
263         return r;
264 }
265
266 int amdgpu_display_crtc_set_config(struct drm_mode_set *set,
267                                    struct drm_modeset_acquire_ctx *ctx)
268 {
269         struct drm_device *dev;
270         struct amdgpu_device *adev;
271         struct drm_crtc *crtc;
272         bool active = false;
273         int ret;
274
275         if (!set || !set->crtc)
276                 return -EINVAL;
277
278         dev = set->crtc->dev;
279
280         ret = pm_runtime_get_sync(dev->dev);
281         if (ret < 0)
282                 goto out;
283
284         ret = drm_crtc_helper_set_config(set, ctx);
285
286         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
287                 if (crtc->enabled)
288                         active = true;
289
290         pm_runtime_mark_last_busy(dev->dev);
291
292         adev = drm_to_adev(dev);
293         /* if we have active crtcs and we don't have a power ref,
294            take the current one */
295         if (active && !adev->have_disp_power_ref) {
296                 adev->have_disp_power_ref = true;
297                 return ret;
298         }
299         /* if we have no active crtcs, then drop the power ref
300            we got before */
301         if (!active && adev->have_disp_power_ref) {
302                 pm_runtime_put_autosuspend(dev->dev);
303                 adev->have_disp_power_ref = false;
304         }
305
306 out:
307         /* drop the power reference we got coming in here */
308         pm_runtime_put_autosuspend(dev->dev);
309         return ret;
310 }
311
312 static const char *encoder_names[41] = {
313         "NONE",
314         "INTERNAL_LVDS",
315         "INTERNAL_TMDS1",
316         "INTERNAL_TMDS2",
317         "INTERNAL_DAC1",
318         "INTERNAL_DAC2",
319         "INTERNAL_SDVOA",
320         "INTERNAL_SDVOB",
321         "SI170B",
322         "CH7303",
323         "CH7301",
324         "INTERNAL_DVO1",
325         "EXTERNAL_SDVOA",
326         "EXTERNAL_SDVOB",
327         "TITFP513",
328         "INTERNAL_LVTM1",
329         "VT1623",
330         "HDMI_SI1930",
331         "HDMI_INTERNAL",
332         "INTERNAL_KLDSCP_TMDS1",
333         "INTERNAL_KLDSCP_DVO1",
334         "INTERNAL_KLDSCP_DAC1",
335         "INTERNAL_KLDSCP_DAC2",
336         "SI178",
337         "MVPU_FPGA",
338         "INTERNAL_DDI",
339         "VT1625",
340         "HDMI_SI1932",
341         "DP_AN9801",
342         "DP_DP501",
343         "INTERNAL_UNIPHY",
344         "INTERNAL_KLDSCP_LVTMA",
345         "INTERNAL_UNIPHY1",
346         "INTERNAL_UNIPHY2",
347         "NUTMEG",
348         "TRAVIS",
349         "INTERNAL_VCE",
350         "INTERNAL_UNIPHY3",
351         "HDMI_ANX9805",
352         "INTERNAL_AMCLK",
353         "VIRTUAL",
354 };
355
356 static const char *hpd_names[6] = {
357         "HPD1",
358         "HPD2",
359         "HPD3",
360         "HPD4",
361         "HPD5",
362         "HPD6",
363 };
364
365 void amdgpu_display_print_display_setup(struct drm_device *dev)
366 {
367         struct drm_connector *connector;
368         struct amdgpu_connector *amdgpu_connector;
369         struct drm_encoder *encoder;
370         struct amdgpu_encoder *amdgpu_encoder;
371         struct drm_connector_list_iter iter;
372         uint32_t devices;
373         int i = 0;
374
375         drm_connector_list_iter_begin(dev, &iter);
376         DRM_INFO("AMDGPU Display Connectors\n");
377         drm_for_each_connector_iter(connector, &iter) {
378                 amdgpu_connector = to_amdgpu_connector(connector);
379                 DRM_INFO("Connector %d:\n", i);
380                 DRM_INFO("  %s\n", connector->name);
381                 if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE)
382                         DRM_INFO("  %s\n", hpd_names[amdgpu_connector->hpd.hpd]);
383                 if (amdgpu_connector->ddc_bus) {
384                         DRM_INFO("  DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
385                                  amdgpu_connector->ddc_bus->rec.mask_clk_reg,
386                                  amdgpu_connector->ddc_bus->rec.mask_data_reg,
387                                  amdgpu_connector->ddc_bus->rec.a_clk_reg,
388                                  amdgpu_connector->ddc_bus->rec.a_data_reg,
389                                  amdgpu_connector->ddc_bus->rec.en_clk_reg,
390                                  amdgpu_connector->ddc_bus->rec.en_data_reg,
391                                  amdgpu_connector->ddc_bus->rec.y_clk_reg,
392                                  amdgpu_connector->ddc_bus->rec.y_data_reg);
393                         if (amdgpu_connector->router.ddc_valid)
394                                 DRM_INFO("  DDC Router 0x%x/0x%x\n",
395                                          amdgpu_connector->router.ddc_mux_control_pin,
396                                          amdgpu_connector->router.ddc_mux_state);
397                         if (amdgpu_connector->router.cd_valid)
398                                 DRM_INFO("  Clock/Data Router 0x%x/0x%x\n",
399                                          amdgpu_connector->router.cd_mux_control_pin,
400                                          amdgpu_connector->router.cd_mux_state);
401                 } else {
402                         if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
403                             connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
404                             connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
405                             connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
406                             connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
407                             connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
408                                 DRM_INFO("  DDC: no ddc bus - possible BIOS bug - please report to [email protected]\n");
409                 }
410                 DRM_INFO("  Encoders:\n");
411                 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
412                         amdgpu_encoder = to_amdgpu_encoder(encoder);
413                         devices = amdgpu_encoder->devices & amdgpu_connector->devices;
414                         if (devices) {
415                                 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
416                                         DRM_INFO("    CRT1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
417                                 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
418                                         DRM_INFO("    CRT2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
419                                 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
420                                         DRM_INFO("    LCD1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
421                                 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
422                                         DRM_INFO("    DFP1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
423                                 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
424                                         DRM_INFO("    DFP2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
425                                 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
426                                         DRM_INFO("    DFP3: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
427                                 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
428                                         DRM_INFO("    DFP4: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
429                                 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
430                                         DRM_INFO("    DFP5: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
431                                 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
432                                         DRM_INFO("    DFP6: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
433                                 if (devices & ATOM_DEVICE_TV1_SUPPORT)
434                                         DRM_INFO("    TV1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
435                                 if (devices & ATOM_DEVICE_CV_SUPPORT)
436                                         DRM_INFO("    CV: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
437                         }
438                 }
439                 i++;
440         }
441         drm_connector_list_iter_end(&iter);
442 }
443
444 bool amdgpu_display_ddc_probe(struct amdgpu_connector *amdgpu_connector,
445                               bool use_aux)
446 {
447         u8 out = 0x0;
448         u8 buf[8];
449         int ret;
450         struct i2c_msg msgs[] = {
451                 {
452                         .addr = DDC_ADDR,
453                         .flags = 0,
454                         .len = 1,
455                         .buf = &out,
456                 },
457                 {
458                         .addr = DDC_ADDR,
459                         .flags = I2C_M_RD,
460                         .len = 8,
461                         .buf = buf,
462                 }
463         };
464
465         /* on hw with routers, select right port */
466         if (amdgpu_connector->router.ddc_valid)
467                 amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
468
469         if (use_aux) {
470                 ret = i2c_transfer(&amdgpu_connector->ddc_bus->aux.ddc, msgs, 2);
471         } else {
472                 ret = i2c_transfer(&amdgpu_connector->ddc_bus->adapter, msgs, 2);
473         }
474
475         if (ret != 2)
476                 /* Couldn't find an accessible DDC on this connector */
477                 return false;
478         /* Probe also for valid EDID header
479          * EDID header starts with:
480          * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
481          * Only the first 6 bytes must be valid as
482          * drm_edid_block_valid() can fix the last 2 bytes */
483         if (drm_edid_header_is_valid(buf) < 6) {
484                 /* Couldn't find an accessible EDID on this
485                  * connector */
486                 return false;
487         }
488         return true;
489 }
490
491 static const struct drm_framebuffer_funcs amdgpu_fb_funcs = {
492         .destroy = drm_gem_fb_destroy,
493         .create_handle = drm_gem_fb_create_handle,
494 };
495
496 uint32_t amdgpu_display_supported_domains(struct amdgpu_device *adev,
497                                           uint64_t bo_flags)
498 {
499         uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
500
501 #if defined(CONFIG_DRM_AMD_DC)
502         /*
503          * if amdgpu_bo_support_uswc returns false it means that USWC mappings
504          * is not supported for this board. But this mapping is required
505          * to avoid hang caused by placement of scanout BO in GTT on certain
506          * APUs. So force the BO placement to VRAM in case this architecture
507          * will not allow USWC mappings.
508          * Also, don't allow GTT domain if the BO doesn't have USWC flag set.
509          */
510         if ((bo_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) &&
511             amdgpu_bo_support_uswc(bo_flags) &&
512             amdgpu_device_asic_has_dc_support(adev->asic_type)) {
513                 switch (adev->asic_type) {
514                 case CHIP_CARRIZO:
515                 case CHIP_STONEY:
516                         domain |= AMDGPU_GEM_DOMAIN_GTT;
517                         break;
518                 case CHIP_RAVEN:
519                         /* enable S/G on PCO and RV2 */
520                         if ((adev->apu_flags & AMD_APU_IS_RAVEN2) ||
521                             (adev->apu_flags & AMD_APU_IS_PICASSO))
522                                 domain |= AMDGPU_GEM_DOMAIN_GTT;
523                         break;
524                 case CHIP_RENOIR:
525                         domain |= AMDGPU_GEM_DOMAIN_GTT;
526                         break;
527
528                 default:
529                         break;
530                 }
531         }
532 #endif
533
534         return domain;
535 }
536
537 static const struct drm_format_info dcc_formats[] = {
538         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
539           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
540          { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
541           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
542         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
543           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
544            .has_alpha = true, },
545         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
546           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
547           .has_alpha = true, },
548         { .format = DRM_FORMAT_BGRA8888, .depth = 32, .num_planes = 2,
549           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
550           .has_alpha = true, },
551         { .format = DRM_FORMAT_XRGB2101010, .depth = 30, .num_planes = 2,
552           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
553         { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 2,
554           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
555         { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 2,
556           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
557           .has_alpha = true, },
558         { .format = DRM_FORMAT_ABGR2101010, .depth = 30, .num_planes = 2,
559           .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
560           .has_alpha = true, },
561         { .format = DRM_FORMAT_RGB565, .depth = 16, .num_planes = 2,
562           .cpp = { 2, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
563 };
564
565 static const struct drm_format_info dcc_retile_formats[] = {
566         { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
567           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
568          { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
569           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
570         { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
571           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
572            .has_alpha = true, },
573         { .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 3,
574           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
575           .has_alpha = true, },
576         { .format = DRM_FORMAT_BGRA8888, .depth = 32, .num_planes = 3,
577           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
578           .has_alpha = true, },
579         { .format = DRM_FORMAT_XRGB2101010, .depth = 30, .num_planes = 3,
580           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
581         { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 3,
582           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
583         { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 3,
584           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
585           .has_alpha = true, },
586         { .format = DRM_FORMAT_ABGR2101010, .depth = 30, .num_planes = 3,
587           .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
588           .has_alpha = true, },
589         { .format = DRM_FORMAT_RGB565, .depth = 16, .num_planes = 3,
590           .cpp = { 2, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
591 };
592
593 static const struct drm_format_info *
594 lookup_format_info(const struct drm_format_info formats[],
595                   int num_formats, u32 format)
596 {
597         int i;
598
599         for (i = 0; i < num_formats; i++) {
600                 if (formats[i].format == format)
601                         return &formats[i];
602         }
603
604         return NULL;
605 }
606
607 const struct drm_format_info *
608 amdgpu_lookup_format_info(u32 format, uint64_t modifier)
609 {
610         if (!IS_AMD_FMT_MOD(modifier))
611                 return NULL;
612
613         if (AMD_FMT_MOD_GET(DCC_RETILE, modifier))
614                 return lookup_format_info(dcc_retile_formats,
615                                           ARRAY_SIZE(dcc_retile_formats),
616                                           format);
617
618         if (AMD_FMT_MOD_GET(DCC, modifier))
619                 return lookup_format_info(dcc_formats, ARRAY_SIZE(dcc_formats),
620                                           format);
621
622         /* returning NULL will cause the default format structs to be used. */
623         return NULL;
624 }
625
626 static int convert_tiling_flags_to_modifier(struct amdgpu_framebuffer *afb)
627 {
628         struct amdgpu_device *adev = drm_to_adev(afb->base.dev);
629         uint64_t modifier = 0;
630
631         if (!afb->tiling_flags || !AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE)) {
632                 modifier = DRM_FORMAT_MOD_LINEAR;
633         } else {
634                 int swizzle = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE);
635                 bool has_xor = swizzle >= 16;
636                 int block_size_bits;
637                 int version;
638                 int pipe_xor_bits = 0;
639                 int bank_xor_bits = 0;
640                 int packers = 0;
641                 uint32_t dcc_offset = AMDGPU_TILING_GET(afb->tiling_flags, DCC_OFFSET_256B);
642
643                 switch (swizzle >> 2) {
644                 case 0: /* 256B */
645                         block_size_bits = 8;
646                         break;
647                 case 1: /* 4KiB */
648                 case 5: /* 4KiB _X */
649                         block_size_bits = 12;
650                         break;
651                 case 2: /* 64KiB */
652                 case 4: /* 64 KiB _T */
653                 case 6: /* 64 KiB _X */
654                         block_size_bits = 16;
655                         break;
656                 default:
657                         /* RESERVED or VAR */
658                         return -EINVAL;
659                 }
660
661                 if (adev->asic_type >= CHIP_SIENNA_CICHLID)
662                         version = AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS;
663                 else if (adev->family == AMDGPU_FAMILY_NV)
664                         version = AMD_FMT_MOD_TILE_VER_GFX10;
665                 else
666                         version = AMD_FMT_MOD_TILE_VER_GFX9;
667
668                 switch (swizzle & 3) {
669                 case 0: /* Z microtiling */
670                         return -EINVAL;
671                 case 1: /* S microtiling */
672                         if (!has_xor)
673                                 version = AMD_FMT_MOD_TILE_VER_GFX9;
674                         break;
675                 case 2:
676                         if (!has_xor && afb->base.format->cpp[0] != 4)
677                                 version = AMD_FMT_MOD_TILE_VER_GFX9;
678                         break;
679                 case 3:
680                         break;
681                 }
682
683                 if (has_xor) {
684                         switch (version) {
685                         case AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS:
686                                 pipe_xor_bits = min(block_size_bits - 8,
687                                                     ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes));
688                                 packers = min(block_size_bits - 8 - pipe_xor_bits,
689                                               ilog2(adev->gfx.config.gb_addr_config_fields.num_pkrs));
690                                 break;
691                         case AMD_FMT_MOD_TILE_VER_GFX10:
692                                 pipe_xor_bits = min(block_size_bits - 8,
693                                                     ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes));
694                                 break;
695                         case AMD_FMT_MOD_TILE_VER_GFX9:
696                                 pipe_xor_bits = min(block_size_bits - 8,
697                                                     ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes) +
698                                                     ilog2(adev->gfx.config.gb_addr_config_fields.num_se));
699                                 bank_xor_bits = min(block_size_bits - 8 - pipe_xor_bits,
700                                                     ilog2(adev->gfx.config.gb_addr_config_fields.num_banks));
701                                 break;
702                         }
703                 }
704
705                 modifier = AMD_FMT_MOD |
706                            AMD_FMT_MOD_SET(TILE, AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE)) |
707                            AMD_FMT_MOD_SET(TILE_VERSION, version) |
708                            AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
709                            AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
710                            AMD_FMT_MOD_SET(PACKERS, packers);
711
712                 if (dcc_offset != 0) {
713                         bool dcc_i64b = AMDGPU_TILING_GET(afb->tiling_flags, DCC_INDEPENDENT_64B) != 0;
714                         bool dcc_i128b = version >= AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS;
715                         const struct drm_format_info *format_info;
716
717                         /* Enable constant encode on RAVEN2 and later. */
718                         bool dcc_constant_encode = adev->asic_type > CHIP_RAVEN ||
719                                                    (adev->asic_type == CHIP_RAVEN &&
720                                                     adev->external_rev_id >= 0x81);
721
722                         int max_cblock_size = dcc_i64b ? AMD_FMT_MOD_DCC_BLOCK_64B :
723                                               dcc_i128b ? AMD_FMT_MOD_DCC_BLOCK_128B :
724                                               AMD_FMT_MOD_DCC_BLOCK_256B;
725
726                         modifier |= AMD_FMT_MOD_SET(DCC, 1) |
727                                     AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, dcc_constant_encode) |
728                                     AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, dcc_i64b) |
729                                     AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, dcc_i128b) |
730                                     AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, max_cblock_size);
731
732                         afb->base.offsets[1] = dcc_offset * 256 + afb->base.offsets[0];
733                         afb->base.pitches[1] = AMDGPU_TILING_GET(afb->tiling_flags, DCC_PITCH_MAX) + 1;
734
735                         format_info = amdgpu_lookup_format_info(afb->base.format->format,
736                                                                 modifier);
737                         if (!format_info)
738                                 return -EINVAL;
739
740                         afb->base.format = format_info;
741                 }
742         }
743
744         afb->base.modifier = modifier;
745         afb->base.flags |= DRM_MODE_FB_MODIFIERS;
746         return 0;
747 }
748
749 static int amdgpu_display_get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
750                                       uint64_t *tiling_flags, bool *tmz_surface)
751 {
752         struct amdgpu_bo *rbo;
753         int r;
754
755         if (!amdgpu_fb) {
756                 *tiling_flags = 0;
757                 *tmz_surface = false;
758                 return 0;
759         }
760
761         rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
762         r = amdgpu_bo_reserve(rbo, false);
763
764         if (unlikely(r)) {
765                 /* Don't show error message when returning -ERESTARTSYS */
766                 if (r != -ERESTARTSYS)
767                         DRM_ERROR("Unable to reserve buffer: %d\n", r);
768                 return r;
769         }
770
771         if (tiling_flags)
772                 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
773
774         if (tmz_surface)
775                 *tmz_surface = amdgpu_bo_encrypted(rbo);
776
777         amdgpu_bo_unreserve(rbo);
778
779         return r;
780 }
781
782 int amdgpu_display_framebuffer_init(struct drm_device *dev,
783                                     struct amdgpu_framebuffer *rfb,
784                                     const struct drm_mode_fb_cmd2 *mode_cmd,
785                                     struct drm_gem_object *obj)
786 {
787         int ret, i;
788         rfb->base.obj[0] = obj;
789         drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
790         ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
791         if (ret)
792                 goto fail;
793
794         /*
795          * This needs to happen before modifier conversion as that might change
796          * the number of planes.
797          */
798         for (i = 1; i < rfb->base.format->num_planes; ++i) {
799                 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
800                         dev_err(&dev->pdev->dev, "Plane 0 and %d have different BOs: %u vs. %u\n",
801                                 i, mode_cmd->handles[0], mode_cmd->handles[i]);
802                         ret = -EINVAL;
803                         goto fail;
804                 }
805         }
806
807         ret = amdgpu_display_get_fb_info(rfb, &rfb->tiling_flags, &rfb->tmz_surface);
808         if (ret)
809                 goto fail;
810
811         if (dev->mode_config.allow_fb_modifiers &&
812             !(rfb->base.flags & DRM_MODE_FB_MODIFIERS)) {
813                 ret = convert_tiling_flags_to_modifier(rfb);
814                 if (ret)
815                         goto fail;
816         }
817
818         for (i = 1; i < rfb->base.format->num_planes; ++i) {
819                 rfb->base.obj[i] = rfb->base.obj[0];
820                 drm_gem_object_get(rfb->base.obj[i]);
821         }
822
823         return 0;
824
825 fail:
826         rfb->base.obj[0] = NULL;
827         return ret;
828 }
829
830 struct drm_framebuffer *
831 amdgpu_display_user_framebuffer_create(struct drm_device *dev,
832                                        struct drm_file *file_priv,
833                                        const struct drm_mode_fb_cmd2 *mode_cmd)
834 {
835         struct drm_gem_object *obj;
836         struct amdgpu_framebuffer *amdgpu_fb;
837         int ret;
838
839         obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
840         if (obj ==  NULL) {
841                 dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
842                         "can't create framebuffer\n", mode_cmd->handles[0]);
843                 return ERR_PTR(-ENOENT);
844         }
845
846         /* Handle is imported dma-buf, so cannot be migrated to VRAM for scanout */
847         if (obj->import_attach) {
848                 DRM_DEBUG_KMS("Cannot create framebuffer from imported dma_buf\n");
849                 return ERR_PTR(-EINVAL);
850         }
851
852         amdgpu_fb = kzalloc(sizeof(*amdgpu_fb), GFP_KERNEL);
853         if (amdgpu_fb == NULL) {
854                 drm_gem_object_put(obj);
855                 return ERR_PTR(-ENOMEM);
856         }
857
858         ret = amdgpu_display_framebuffer_init(dev, amdgpu_fb, mode_cmd, obj);
859         if (ret) {
860                 kfree(amdgpu_fb);
861                 drm_gem_object_put(obj);
862                 return ERR_PTR(ret);
863         }
864
865         return &amdgpu_fb->base;
866 }
867
868 const struct drm_mode_config_funcs amdgpu_mode_funcs = {
869         .fb_create = amdgpu_display_user_framebuffer_create,
870         .output_poll_changed = drm_fb_helper_output_poll_changed,
871 };
872
873 static const struct drm_prop_enum_list amdgpu_underscan_enum_list[] =
874 {       { UNDERSCAN_OFF, "off" },
875         { UNDERSCAN_ON, "on" },
876         { UNDERSCAN_AUTO, "auto" },
877 };
878
879 static const struct drm_prop_enum_list amdgpu_audio_enum_list[] =
880 {       { AMDGPU_AUDIO_DISABLE, "off" },
881         { AMDGPU_AUDIO_ENABLE, "on" },
882         { AMDGPU_AUDIO_AUTO, "auto" },
883 };
884
885 /* XXX support different dither options? spatial, temporal, both, etc. */
886 static const struct drm_prop_enum_list amdgpu_dither_enum_list[] =
887 {       { AMDGPU_FMT_DITHER_DISABLE, "off" },
888         { AMDGPU_FMT_DITHER_ENABLE, "on" },
889 };
890
891 int amdgpu_display_modeset_create_props(struct amdgpu_device *adev)
892 {
893         int sz;
894
895         adev->mode_info.coherent_mode_property =
896                 drm_property_create_range(adev_to_drm(adev), 0, "coherent", 0, 1);
897         if (!adev->mode_info.coherent_mode_property)
898                 return -ENOMEM;
899
900         adev->mode_info.load_detect_property =
901                 drm_property_create_range(adev_to_drm(adev), 0, "load detection", 0, 1);
902         if (!adev->mode_info.load_detect_property)
903                 return -ENOMEM;
904
905         drm_mode_create_scaling_mode_property(adev_to_drm(adev));
906
907         sz = ARRAY_SIZE(amdgpu_underscan_enum_list);
908         adev->mode_info.underscan_property =
909                 drm_property_create_enum(adev_to_drm(adev), 0,
910                                          "underscan",
911                                          amdgpu_underscan_enum_list, sz);
912
913         adev->mode_info.underscan_hborder_property =
914                 drm_property_create_range(adev_to_drm(adev), 0,
915                                           "underscan hborder", 0, 128);
916         if (!adev->mode_info.underscan_hborder_property)
917                 return -ENOMEM;
918
919         adev->mode_info.underscan_vborder_property =
920                 drm_property_create_range(adev_to_drm(adev), 0,
921                                           "underscan vborder", 0, 128);
922         if (!adev->mode_info.underscan_vborder_property)
923                 return -ENOMEM;
924
925         sz = ARRAY_SIZE(amdgpu_audio_enum_list);
926         adev->mode_info.audio_property =
927                 drm_property_create_enum(adev_to_drm(adev), 0,
928                                          "audio",
929                                          amdgpu_audio_enum_list, sz);
930
931         sz = ARRAY_SIZE(amdgpu_dither_enum_list);
932         adev->mode_info.dither_property =
933                 drm_property_create_enum(adev_to_drm(adev), 0,
934                                          "dither",
935                                          amdgpu_dither_enum_list, sz);
936
937         if (amdgpu_device_has_dc_support(adev)) {
938                 adev->mode_info.abm_level_property =
939                         drm_property_create_range(adev_to_drm(adev), 0,
940                                                   "abm level", 0, 4);
941                 if (!adev->mode_info.abm_level_property)
942                         return -ENOMEM;
943         }
944
945         return 0;
946 }
947
948 void amdgpu_display_update_priority(struct amdgpu_device *adev)
949 {
950         /* adjustment options for the display watermarks */
951         if ((amdgpu_disp_priority == 0) || (amdgpu_disp_priority > 2))
952                 adev->mode_info.disp_priority = 0;
953         else
954                 adev->mode_info.disp_priority = amdgpu_disp_priority;
955
956 }
957
958 static bool amdgpu_display_is_hdtv_mode(const struct drm_display_mode *mode)
959 {
960         /* try and guess if this is a tv or a monitor */
961         if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
962             (mode->vdisplay == 576) || /* 576p */
963             (mode->vdisplay == 720) || /* 720p */
964             (mode->vdisplay == 1080)) /* 1080p */
965                 return true;
966         else
967                 return false;
968 }
969
970 bool amdgpu_display_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
971                                         const struct drm_display_mode *mode,
972                                         struct drm_display_mode *adjusted_mode)
973 {
974         struct drm_device *dev = crtc->dev;
975         struct drm_encoder *encoder;
976         struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
977         struct amdgpu_encoder *amdgpu_encoder;
978         struct drm_connector *connector;
979         u32 src_v = 1, dst_v = 1;
980         u32 src_h = 1, dst_h = 1;
981
982         amdgpu_crtc->h_border = 0;
983         amdgpu_crtc->v_border = 0;
984
985         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
986                 if (encoder->crtc != crtc)
987                         continue;
988                 amdgpu_encoder = to_amdgpu_encoder(encoder);
989                 connector = amdgpu_get_connector_for_encoder(encoder);
990
991                 /* set scaling */
992                 if (amdgpu_encoder->rmx_type == RMX_OFF)
993                         amdgpu_crtc->rmx_type = RMX_OFF;
994                 else if (mode->hdisplay < amdgpu_encoder->native_mode.hdisplay ||
995                          mode->vdisplay < amdgpu_encoder->native_mode.vdisplay)
996                         amdgpu_crtc->rmx_type = amdgpu_encoder->rmx_type;
997                 else
998                         amdgpu_crtc->rmx_type = RMX_OFF;
999                 /* copy native mode */
1000                 memcpy(&amdgpu_crtc->native_mode,
1001                        &amdgpu_encoder->native_mode,
1002                        sizeof(struct drm_display_mode));
1003                 src_v = crtc->mode.vdisplay;
1004                 dst_v = amdgpu_crtc->native_mode.vdisplay;
1005                 src_h = crtc->mode.hdisplay;
1006                 dst_h = amdgpu_crtc->native_mode.hdisplay;
1007
1008                 /* fix up for overscan on hdmi */
1009                 if ((!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
1010                     ((amdgpu_encoder->underscan_type == UNDERSCAN_ON) ||
1011                      ((amdgpu_encoder->underscan_type == UNDERSCAN_AUTO) &&
1012                       drm_detect_hdmi_monitor(amdgpu_connector_edid(connector)) &&
1013                       amdgpu_display_is_hdtv_mode(mode)))) {
1014                         if (amdgpu_encoder->underscan_hborder != 0)
1015                                 amdgpu_crtc->h_border = amdgpu_encoder->underscan_hborder;
1016                         else
1017                                 amdgpu_crtc->h_border = (mode->hdisplay >> 5) + 16;
1018                         if (amdgpu_encoder->underscan_vborder != 0)
1019                                 amdgpu_crtc->v_border = amdgpu_encoder->underscan_vborder;
1020                         else
1021                                 amdgpu_crtc->v_border = (mode->vdisplay >> 5) + 16;
1022                         amdgpu_crtc->rmx_type = RMX_FULL;
1023                         src_v = crtc->mode.vdisplay;
1024                         dst_v = crtc->mode.vdisplay - (amdgpu_crtc->v_border * 2);
1025                         src_h = crtc->mode.hdisplay;
1026                         dst_h = crtc->mode.hdisplay - (amdgpu_crtc->h_border * 2);
1027                 }
1028         }
1029         if (amdgpu_crtc->rmx_type != RMX_OFF) {
1030                 fixed20_12 a, b;
1031                 a.full = dfixed_const(src_v);
1032                 b.full = dfixed_const(dst_v);
1033                 amdgpu_crtc->vsc.full = dfixed_div(a, b);
1034                 a.full = dfixed_const(src_h);
1035                 b.full = dfixed_const(dst_h);
1036                 amdgpu_crtc->hsc.full = dfixed_div(a, b);
1037         } else {
1038                 amdgpu_crtc->vsc.full = dfixed_const(1);
1039                 amdgpu_crtc->hsc.full = dfixed_const(1);
1040         }
1041         return true;
1042 }
1043
1044 /*
1045  * Retrieve current video scanout position of crtc on a given gpu, and
1046  * an optional accurate timestamp of when query happened.
1047  *
1048  * \param dev Device to query.
1049  * \param pipe Crtc to query.
1050  * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
1051  *              For driver internal use only also supports these flags:
1052  *
1053  *              USE_REAL_VBLANKSTART to use the real start of vblank instead
1054  *              of a fudged earlier start of vblank.
1055  *
1056  *              GET_DISTANCE_TO_VBLANKSTART to return distance to the
1057  *              fudged earlier start of vblank in *vpos and the distance
1058  *              to true start of vblank in *hpos.
1059  *
1060  * \param *vpos Location where vertical scanout position should be stored.
1061  * \param *hpos Location where horizontal scanout position should go.
1062  * \param *stime Target location for timestamp taken immediately before
1063  *               scanout position query. Can be NULL to skip timestamp.
1064  * \param *etime Target location for timestamp taken immediately after
1065  *               scanout position query. Can be NULL to skip timestamp.
1066  *
1067  * Returns vpos as a positive number while in active scanout area.
1068  * Returns vpos as a negative number inside vblank, counting the number
1069  * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1070  * until start of active scanout / end of vblank."
1071  *
1072  * \return Flags, or'ed together as follows:
1073  *
1074  * DRM_SCANOUTPOS_VALID = Query successful.
1075  * DRM_SCANOUTPOS_INVBL = Inside vblank.
1076  * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1077  * this flag means that returned position may be offset by a constant but
1078  * unknown small number of scanlines wrt. real scanout position.
1079  *
1080  */
1081 int amdgpu_display_get_crtc_scanoutpos(struct drm_device *dev,
1082                         unsigned int pipe, unsigned int flags, int *vpos,
1083                         int *hpos, ktime_t *stime, ktime_t *etime,
1084                         const struct drm_display_mode *mode)
1085 {
1086         u32 vbl = 0, position = 0;
1087         int vbl_start, vbl_end, vtotal, ret = 0;
1088         bool in_vbl = true;
1089
1090         struct amdgpu_device *adev = drm_to_adev(dev);
1091
1092         /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1093
1094         /* Get optional system timestamp before query. */
1095         if (stime)
1096                 *stime = ktime_get();
1097
1098         if (amdgpu_display_page_flip_get_scanoutpos(adev, pipe, &vbl, &position) == 0)
1099                 ret |= DRM_SCANOUTPOS_VALID;
1100
1101         /* Get optional system timestamp after query. */
1102         if (etime)
1103                 *etime = ktime_get();
1104
1105         /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1106
1107         /* Decode into vertical and horizontal scanout position. */
1108         *vpos = position & 0x1fff;
1109         *hpos = (position >> 16) & 0x1fff;
1110
1111         /* Valid vblank area boundaries from gpu retrieved? */
1112         if (vbl > 0) {
1113                 /* Yes: Decode. */
1114                 ret |= DRM_SCANOUTPOS_ACCURATE;
1115                 vbl_start = vbl & 0x1fff;
1116                 vbl_end = (vbl >> 16) & 0x1fff;
1117         }
1118         else {
1119                 /* No: Fake something reasonable which gives at least ok results. */
1120                 vbl_start = mode->crtc_vdisplay;
1121                 vbl_end = 0;
1122         }
1123
1124         /* Called from driver internal vblank counter query code? */
1125         if (flags & GET_DISTANCE_TO_VBLANKSTART) {
1126             /* Caller wants distance from real vbl_start in *hpos */
1127             *hpos = *vpos - vbl_start;
1128         }
1129
1130         /* Fudge vblank to start a few scanlines earlier to handle the
1131          * problem that vblank irqs fire a few scanlines before start
1132          * of vblank. Some driver internal callers need the true vblank
1133          * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
1134          *
1135          * The cause of the "early" vblank irq is that the irq is triggered
1136          * by the line buffer logic when the line buffer read position enters
1137          * the vblank, whereas our crtc scanout position naturally lags the
1138          * line buffer read position.
1139          */
1140         if (!(flags & USE_REAL_VBLANKSTART))
1141                 vbl_start -= adev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
1142
1143         /* Test scanout position against vblank region. */
1144         if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1145                 in_vbl = false;
1146
1147         /* In vblank? */
1148         if (in_vbl)
1149             ret |= DRM_SCANOUTPOS_IN_VBLANK;
1150
1151         /* Called from driver internal vblank counter query code? */
1152         if (flags & GET_DISTANCE_TO_VBLANKSTART) {
1153                 /* Caller wants distance from fudged earlier vbl_start */
1154                 *vpos -= vbl_start;
1155                 return ret;
1156         }
1157
1158         /* Check if inside vblank area and apply corrective offsets:
1159          * vpos will then be >=0 in video scanout area, but negative
1160          * within vblank area, counting down the number of lines until
1161          * start of scanout.
1162          */
1163
1164         /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1165         if (in_vbl && (*vpos >= vbl_start)) {
1166                 vtotal = mode->crtc_vtotal;
1167
1168                 /* With variable refresh rate displays the vpos can exceed
1169                  * the vtotal value. Clamp to 0 to return -vbl_end instead
1170                  * of guessing the remaining number of lines until scanout.
1171                  */
1172                 *vpos = (*vpos < vtotal) ? (*vpos - vtotal) : 0;
1173         }
1174
1175         /* Correct for shifted end of vbl at vbl_end. */
1176         *vpos = *vpos - vbl_end;
1177
1178         return ret;
1179 }
1180
1181 int amdgpu_display_crtc_idx_to_irq_type(struct amdgpu_device *adev, int crtc)
1182 {
1183         if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
1184                 return AMDGPU_CRTC_IRQ_NONE;
1185
1186         switch (crtc) {
1187         case 0:
1188                 return AMDGPU_CRTC_IRQ_VBLANK1;
1189         case 1:
1190                 return AMDGPU_CRTC_IRQ_VBLANK2;
1191         case 2:
1192                 return AMDGPU_CRTC_IRQ_VBLANK3;
1193         case 3:
1194                 return AMDGPU_CRTC_IRQ_VBLANK4;
1195         case 4:
1196                 return AMDGPU_CRTC_IRQ_VBLANK5;
1197         case 5:
1198                 return AMDGPU_CRTC_IRQ_VBLANK6;
1199         default:
1200                 return AMDGPU_CRTC_IRQ_NONE;
1201         }
1202 }
1203
1204 bool amdgpu_crtc_get_scanout_position(struct drm_crtc *crtc,
1205                         bool in_vblank_irq, int *vpos,
1206                         int *hpos, ktime_t *stime, ktime_t *etime,
1207                         const struct drm_display_mode *mode)
1208 {
1209         struct drm_device *dev = crtc->dev;
1210         unsigned int pipe = crtc->index;
1211
1212         return amdgpu_display_get_crtc_scanoutpos(dev, pipe, 0, vpos, hpos,
1213                                                   stime, etime, mode);
1214 }
This page took 0.113889 seconds and 4 git commands to generate.