2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
23 * Authors: Dave Airlie
27 #include <drm/amdgpu_drm.h>
29 #include "amdgpu_i2c.h"
31 #include "amdgpu_connectors.h"
32 #include "amdgpu_display.h"
33 #include <asm/div64.h>
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>
44 static void amdgpu_display_flip_callback(struct dma_fence *f,
45 struct dma_fence_cb *cb)
47 struct amdgpu_flip_work *work =
48 container_of(cb, struct amdgpu_flip_work, cb);
51 schedule_work(&work->flip_work.work);
54 static bool amdgpu_display_flip_handle_fence(struct amdgpu_flip_work *work,
57 struct dma_fence *fence= *f;
64 if (!dma_fence_add_callback(fence, &work->cb,
65 amdgpu_display_flip_callback))
72 static void amdgpu_display_flip_work_func(struct work_struct *__work)
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];
81 struct drm_crtc *crtc = &amdgpu_crtc->base;
86 for (i = 0; i < work->shared_count; ++i)
87 if (amdgpu_display_flip_handle_fence(work, &work->shared[i]))
90 /* Wait until we're out of the vertical blank period before the one
91 * targeted by the flip
93 if (amdgpu_crtc->enabled &&
94 (amdgpu_display_get_crtc_scanoutpos(adev_to_drm(adev), work->crtc_id, 0,
95 &vpos, &hpos, NULL, NULL,
97 & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK)) ==
98 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_IN_VBLANK) &&
99 (int)(work->target_vblank -
100 amdgpu_get_vblank_counter_kms(crtc)) > 0) {
101 schedule_delayed_work(&work->flip_work, usecs_to_jiffies(1000));
105 /* We borrow the event spin lock for protecting flip_status */
106 spin_lock_irqsave(&crtc->dev->event_lock, flags);
108 /* Do the flip (mmio) */
109 adev->mode_info.funcs->page_flip(adev, work->crtc_id, work->base, work->async);
111 /* Set the flip status */
112 amdgpu_crtc->pflip_status = AMDGPU_FLIP_SUBMITTED;
113 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
116 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_SUBMITTED, work: %p,\n",
117 amdgpu_crtc->crtc_id, amdgpu_crtc, work);
122 * Handle unpin events outside the interrupt handler proper.
124 static void amdgpu_display_unpin_work_func(struct work_struct *__work)
126 struct amdgpu_flip_work *work =
127 container_of(__work, struct amdgpu_flip_work, unpin_work);
130 /* unpin of the old buffer */
131 r = amdgpu_bo_reserve(work->old_abo, true);
132 if (likely(r == 0)) {
133 amdgpu_bo_unpin(work->old_abo);
134 amdgpu_bo_unreserve(work->old_abo);
136 DRM_ERROR("failed to reserve buffer after flip\n");
138 amdgpu_bo_unref(&work->old_abo);
143 int amdgpu_display_crtc_page_flip_target(struct drm_crtc *crtc,
144 struct drm_framebuffer *fb,
145 struct drm_pending_vblank_event *event,
146 uint32_t page_flip_flags, uint32_t target,
147 struct drm_modeset_acquire_ctx *ctx)
149 struct drm_device *dev = crtc->dev;
150 struct amdgpu_device *adev = drm_to_adev(dev);
151 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
152 struct drm_gem_object *obj;
153 struct amdgpu_flip_work *work;
154 struct amdgpu_bo *new_abo;
159 work = kzalloc(sizeof *work, GFP_KERNEL);
163 INIT_DELAYED_WORK(&work->flip_work, amdgpu_display_flip_work_func);
164 INIT_WORK(&work->unpin_work, amdgpu_display_unpin_work_func);
168 work->crtc_id = amdgpu_crtc->crtc_id;
169 work->async = (page_flip_flags & DRM_MODE_PAGE_FLIP_ASYNC) != 0;
171 /* schedule unpin of the old buffer */
172 obj = crtc->primary->fb->obj[0];
174 /* take a reference to the old object */
175 work->old_abo = gem_to_amdgpu_bo(obj);
176 amdgpu_bo_ref(work->old_abo);
179 new_abo = gem_to_amdgpu_bo(obj);
181 /* pin the new buffer */
182 r = amdgpu_bo_reserve(new_abo, false);
183 if (unlikely(r != 0)) {
184 DRM_ERROR("failed to reserve new abo buffer before flip\n");
188 if (!adev->enable_virtual_display) {
189 r = amdgpu_bo_pin(new_abo,
190 amdgpu_display_supported_domains(adev, new_abo->flags));
191 if (unlikely(r != 0)) {
192 DRM_ERROR("failed to pin new abo buffer before flip\n");
197 r = amdgpu_ttm_alloc_gart(&new_abo->tbo);
198 if (unlikely(r != 0)) {
199 DRM_ERROR("%p bind failed\n", new_abo);
203 r = dma_resv_get_fences(new_abo->tbo.base.resv, NULL,
204 &work->shared_count, &work->shared);
205 if (unlikely(r != 0)) {
206 DRM_ERROR("failed to get fences for buffer\n");
210 amdgpu_bo_get_tiling_flags(new_abo, &tiling_flags);
211 amdgpu_bo_unreserve(new_abo);
213 if (!adev->enable_virtual_display)
214 work->base = amdgpu_bo_gpu_offset(new_abo);
215 work->target_vblank = target - (uint32_t)drm_crtc_vblank_count(crtc) +
216 amdgpu_get_vblank_counter_kms(crtc);
218 /* we borrow the event spin lock for protecting flip_wrok */
219 spin_lock_irqsave(&crtc->dev->event_lock, flags);
220 if (amdgpu_crtc->pflip_status != AMDGPU_FLIP_NONE) {
221 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
222 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
227 amdgpu_crtc->pflip_status = AMDGPU_FLIP_PENDING;
228 amdgpu_crtc->pflip_works = work;
231 DRM_DEBUG_DRIVER("crtc:%d[%p], pflip_stat:AMDGPU_FLIP_PENDING, work: %p,\n",
232 amdgpu_crtc->crtc_id, amdgpu_crtc, work);
234 crtc->primary->fb = fb;
235 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
236 amdgpu_display_flip_work_func(&work->flip_work.work);
240 if (unlikely(amdgpu_bo_reserve(new_abo, false) != 0)) {
241 DRM_ERROR("failed to reserve new abo in error path\n");
245 if (!adev->enable_virtual_display)
246 amdgpu_bo_unpin(new_abo);
249 amdgpu_bo_unreserve(new_abo);
252 amdgpu_bo_unref(&work->old_abo);
253 for (i = 0; i < work->shared_count; ++i)
254 dma_fence_put(work->shared[i]);
261 int amdgpu_display_crtc_set_config(struct drm_mode_set *set,
262 struct drm_modeset_acquire_ctx *ctx)
264 struct drm_device *dev;
265 struct amdgpu_device *adev;
266 struct drm_crtc *crtc;
270 if (!set || !set->crtc)
273 dev = set->crtc->dev;
275 ret = pm_runtime_get_sync(dev->dev);
279 ret = drm_crtc_helper_set_config(set, ctx);
281 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
285 pm_runtime_mark_last_busy(dev->dev);
287 adev = drm_to_adev(dev);
288 /* if we have active crtcs and we don't have a power ref,
289 take the current one */
290 if (active && !adev->have_disp_power_ref) {
291 adev->have_disp_power_ref = true;
294 /* if we have no active crtcs, then drop the power ref
296 if (!active && adev->have_disp_power_ref) {
297 pm_runtime_put_autosuspend(dev->dev);
298 adev->have_disp_power_ref = false;
302 /* drop the power reference we got coming in here */
303 pm_runtime_put_autosuspend(dev->dev);
307 static const char *encoder_names[41] = {
327 "INTERNAL_KLDSCP_TMDS1",
328 "INTERNAL_KLDSCP_DVO1",
329 "INTERNAL_KLDSCP_DAC1",
330 "INTERNAL_KLDSCP_DAC2",
339 "INTERNAL_KLDSCP_LVTMA",
351 static const char *hpd_names[6] = {
360 void amdgpu_display_print_display_setup(struct drm_device *dev)
362 struct drm_connector *connector;
363 struct amdgpu_connector *amdgpu_connector;
364 struct drm_encoder *encoder;
365 struct amdgpu_encoder *amdgpu_encoder;
366 struct drm_connector_list_iter iter;
370 drm_connector_list_iter_begin(dev, &iter);
371 DRM_INFO("AMDGPU Display Connectors\n");
372 drm_for_each_connector_iter(connector, &iter) {
373 amdgpu_connector = to_amdgpu_connector(connector);
374 DRM_INFO("Connector %d:\n", i);
375 DRM_INFO(" %s\n", connector->name);
376 if (amdgpu_connector->hpd.hpd != AMDGPU_HPD_NONE)
377 DRM_INFO(" %s\n", hpd_names[amdgpu_connector->hpd.hpd]);
378 if (amdgpu_connector->ddc_bus) {
379 DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
380 amdgpu_connector->ddc_bus->rec.mask_clk_reg,
381 amdgpu_connector->ddc_bus->rec.mask_data_reg,
382 amdgpu_connector->ddc_bus->rec.a_clk_reg,
383 amdgpu_connector->ddc_bus->rec.a_data_reg,
384 amdgpu_connector->ddc_bus->rec.en_clk_reg,
385 amdgpu_connector->ddc_bus->rec.en_data_reg,
386 amdgpu_connector->ddc_bus->rec.y_clk_reg,
387 amdgpu_connector->ddc_bus->rec.y_data_reg);
388 if (amdgpu_connector->router.ddc_valid)
389 DRM_INFO(" DDC Router 0x%x/0x%x\n",
390 amdgpu_connector->router.ddc_mux_control_pin,
391 amdgpu_connector->router.ddc_mux_state);
392 if (amdgpu_connector->router.cd_valid)
393 DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
394 amdgpu_connector->router.cd_mux_control_pin,
395 amdgpu_connector->router.cd_mux_state);
397 if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
398 connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
399 connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
400 connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
401 connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
402 connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
405 DRM_INFO(" Encoders:\n");
406 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
407 amdgpu_encoder = to_amdgpu_encoder(encoder);
408 devices = amdgpu_encoder->devices & amdgpu_connector->devices;
410 if (devices & ATOM_DEVICE_CRT1_SUPPORT)
411 DRM_INFO(" CRT1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
412 if (devices & ATOM_DEVICE_CRT2_SUPPORT)
413 DRM_INFO(" CRT2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
414 if (devices & ATOM_DEVICE_LCD1_SUPPORT)
415 DRM_INFO(" LCD1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
416 if (devices & ATOM_DEVICE_DFP1_SUPPORT)
417 DRM_INFO(" DFP1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
418 if (devices & ATOM_DEVICE_DFP2_SUPPORT)
419 DRM_INFO(" DFP2: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
420 if (devices & ATOM_DEVICE_DFP3_SUPPORT)
421 DRM_INFO(" DFP3: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
422 if (devices & ATOM_DEVICE_DFP4_SUPPORT)
423 DRM_INFO(" DFP4: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
424 if (devices & ATOM_DEVICE_DFP5_SUPPORT)
425 DRM_INFO(" DFP5: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
426 if (devices & ATOM_DEVICE_DFP6_SUPPORT)
427 DRM_INFO(" DFP6: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
428 if (devices & ATOM_DEVICE_TV1_SUPPORT)
429 DRM_INFO(" TV1: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
430 if (devices & ATOM_DEVICE_CV_SUPPORT)
431 DRM_INFO(" CV: %s\n", encoder_names[amdgpu_encoder->encoder_id]);
436 drm_connector_list_iter_end(&iter);
439 bool amdgpu_display_ddc_probe(struct amdgpu_connector *amdgpu_connector,
445 struct i2c_msg msgs[] = {
460 /* on hw with routers, select right port */
461 if (amdgpu_connector->router.ddc_valid)
462 amdgpu_i2c_router_select_ddc_port(amdgpu_connector);
465 ret = i2c_transfer(&amdgpu_connector->ddc_bus->aux.ddc, msgs, 2);
467 ret = i2c_transfer(&amdgpu_connector->ddc_bus->adapter, msgs, 2);
471 /* Couldn't find an accessible DDC on this connector */
473 /* Probe also for valid EDID header
474 * EDID header starts with:
475 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
476 * Only the first 6 bytes must be valid as
477 * drm_edid_block_valid() can fix the last 2 bytes */
478 if (drm_edid_header_is_valid(buf) < 6) {
479 /* Couldn't find an accessible EDID on this
486 static const struct drm_framebuffer_funcs amdgpu_fb_funcs = {
487 .destroy = drm_gem_fb_destroy,
488 .create_handle = drm_gem_fb_create_handle,
491 uint32_t amdgpu_display_supported_domains(struct amdgpu_device *adev,
494 uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
496 #if defined(CONFIG_DRM_AMD_DC)
498 * if amdgpu_bo_support_uswc returns false it means that USWC mappings
499 * is not supported for this board. But this mapping is required
500 * to avoid hang caused by placement of scanout BO in GTT on certain
501 * APUs. So force the BO placement to VRAM in case this architecture
502 * will not allow USWC mappings.
503 * Also, don't allow GTT domain if the BO doesn't have USWC flag set.
505 if ((bo_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) &&
506 amdgpu_bo_support_uswc(bo_flags) &&
507 amdgpu_device_asic_has_dc_support(adev->asic_type)) {
508 switch (adev->asic_type) {
511 domain |= AMDGPU_GEM_DOMAIN_GTT;
514 /* enable S/G on PCO and RV2 */
515 if ((adev->apu_flags & AMD_APU_IS_RAVEN2) ||
516 (adev->apu_flags & AMD_APU_IS_PICASSO))
517 domain |= AMDGPU_GEM_DOMAIN_GTT;
521 case CHIP_YELLOW_CARP:
522 domain |= AMDGPU_GEM_DOMAIN_GTT;
534 static const struct drm_format_info dcc_formats[] = {
535 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
536 .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
537 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
538 .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
539 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
540 .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
541 .has_alpha = true, },
542 { .format = DRM_FORMAT_ABGR8888, .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_BGRA8888, .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_XRGB2101010, .depth = 30, .num_planes = 2,
549 .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
550 { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 2,
551 .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
552 { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 2,
553 .cpp = { 4, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
554 .has_alpha = true, },
555 { .format = DRM_FORMAT_ABGR2101010, .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_RGB565, .depth = 16, .num_planes = 2,
559 .cpp = { 2, 0, }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
562 static const struct drm_format_info dcc_retile_formats[] = {
563 { .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
564 .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
565 { .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
566 .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
567 { .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
568 .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
569 .has_alpha = true, },
570 { .format = DRM_FORMAT_ABGR8888, .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_BGRA8888, .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_XRGB2101010, .depth = 30, .num_planes = 3,
577 .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
578 { .format = DRM_FORMAT_XBGR2101010, .depth = 30, .num_planes = 3,
579 .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
580 { .format = DRM_FORMAT_ARGB2101010, .depth = 30, .num_planes = 3,
581 .cpp = { 4, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1,
582 .has_alpha = true, },
583 { .format = DRM_FORMAT_ABGR2101010, .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_RGB565, .depth = 16, .num_planes = 3,
587 .cpp = { 2, 0, 0 }, .block_w = {1, 1, 1}, .block_h = {1, 1, 1}, .hsub = 1, .vsub = 1, },
590 static const struct drm_format_info *
591 lookup_format_info(const struct drm_format_info formats[],
592 int num_formats, u32 format)
596 for (i = 0; i < num_formats; i++) {
597 if (formats[i].format == format)
604 const struct drm_format_info *
605 amdgpu_lookup_format_info(u32 format, uint64_t modifier)
607 if (!IS_AMD_FMT_MOD(modifier))
610 if (AMD_FMT_MOD_GET(DCC_RETILE, modifier))
611 return lookup_format_info(dcc_retile_formats,
612 ARRAY_SIZE(dcc_retile_formats),
615 if (AMD_FMT_MOD_GET(DCC, modifier))
616 return lookup_format_info(dcc_formats, ARRAY_SIZE(dcc_formats),
619 /* returning NULL will cause the default format structs to be used. */
625 * Tries to extract the renderable DCC offset from the opaque metadata attached
629 extract_render_dcc_offset(struct amdgpu_device *adev,
630 struct drm_gem_object *obj,
633 struct amdgpu_bo *rbo;
635 uint32_t metadata[10]; /* Something that fits a descriptor + header. */
638 rbo = gem_to_amdgpu_bo(obj);
639 r = amdgpu_bo_reserve(rbo, false);
642 /* Don't show error message when returning -ERESTARTSYS */
643 if (r != -ERESTARTSYS)
644 DRM_ERROR("Unable to reserve buffer: %d\n", r);
648 r = amdgpu_bo_get_metadata(rbo, metadata, sizeof(metadata), &size, NULL);
649 amdgpu_bo_unreserve(rbo);
655 * The first word is the metadata version, and we need space for at least
656 * the version + pci vendor+device id + 8 words for a descriptor.
658 if (size < 40 || metadata[0] != 1)
661 if (adev->family >= AMDGPU_FAMILY_NV) {
662 /* resource word 6/7 META_DATA_ADDRESS{_LO} */
663 *offset = ((u64)metadata[9] << 16u) |
664 ((metadata[8] & 0xFF000000u) >> 16);
666 /* resource word 5/7 META_DATA_ADDRESS */
667 *offset = ((u64)metadata[9] << 8u) |
668 ((u64)(metadata[7] & 0x1FE0000u) << 23);
674 static int convert_tiling_flags_to_modifier(struct amdgpu_framebuffer *afb)
676 struct amdgpu_device *adev = drm_to_adev(afb->base.dev);
677 uint64_t modifier = 0;
679 if (!afb->tiling_flags || !AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE)) {
680 modifier = DRM_FORMAT_MOD_LINEAR;
682 int swizzle = AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE);
683 bool has_xor = swizzle >= 16;
686 int pipe_xor_bits = 0;
687 int bank_xor_bits = 0;
690 int pipes = ilog2(adev->gfx.config.gb_addr_config_fields.num_pipes);
691 uint32_t dcc_offset = AMDGPU_TILING_GET(afb->tiling_flags, DCC_OFFSET_256B);
693 switch (swizzle >> 2) {
698 case 5: /* 4KiB _X */
699 block_size_bits = 12;
702 case 4: /* 64 KiB _T */
703 case 6: /* 64 KiB _X */
704 block_size_bits = 16;
707 /* RESERVED or VAR */
711 if (adev->asic_type >= CHIP_SIENNA_CICHLID)
712 version = AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS;
713 else if (adev->family == AMDGPU_FAMILY_NV)
714 version = AMD_FMT_MOD_TILE_VER_GFX10;
716 version = AMD_FMT_MOD_TILE_VER_GFX9;
718 switch (swizzle & 3) {
719 case 0: /* Z microtiling */
721 case 1: /* S microtiling */
723 version = AMD_FMT_MOD_TILE_VER_GFX9;
726 if (!has_xor && afb->base.format->cpp[0] != 4)
727 version = AMD_FMT_MOD_TILE_VER_GFX9;
735 case AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS:
736 pipe_xor_bits = min(block_size_bits - 8, pipes);
737 packers = min(block_size_bits - 8 - pipe_xor_bits,
738 ilog2(adev->gfx.config.gb_addr_config_fields.num_pkrs));
740 case AMD_FMT_MOD_TILE_VER_GFX10:
741 pipe_xor_bits = min(block_size_bits - 8, pipes);
743 case AMD_FMT_MOD_TILE_VER_GFX9:
744 rb = ilog2(adev->gfx.config.gb_addr_config_fields.num_se) +
745 ilog2(adev->gfx.config.gb_addr_config_fields.num_rb_per_se);
746 pipe_xor_bits = min(block_size_bits - 8, pipes +
747 ilog2(adev->gfx.config.gb_addr_config_fields.num_se));
748 bank_xor_bits = min(block_size_bits - 8 - pipe_xor_bits,
749 ilog2(adev->gfx.config.gb_addr_config_fields.num_banks));
754 modifier = AMD_FMT_MOD |
755 AMD_FMT_MOD_SET(TILE, AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE)) |
756 AMD_FMT_MOD_SET(TILE_VERSION, version) |
757 AMD_FMT_MOD_SET(PIPE_XOR_BITS, pipe_xor_bits) |
758 AMD_FMT_MOD_SET(BANK_XOR_BITS, bank_xor_bits) |
759 AMD_FMT_MOD_SET(PACKERS, packers);
761 if (dcc_offset != 0) {
762 bool dcc_i64b = AMDGPU_TILING_GET(afb->tiling_flags, DCC_INDEPENDENT_64B) != 0;
763 bool dcc_i128b = version >= AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS;
764 const struct drm_format_info *format_info;
765 u64 render_dcc_offset;
767 /* Enable constant encode on RAVEN2 and later. */
768 bool dcc_constant_encode = adev->asic_type > CHIP_RAVEN ||
769 (adev->asic_type == CHIP_RAVEN &&
770 adev->external_rev_id >= 0x81);
772 int max_cblock_size = dcc_i64b ? AMD_FMT_MOD_DCC_BLOCK_64B :
773 dcc_i128b ? AMD_FMT_MOD_DCC_BLOCK_128B :
774 AMD_FMT_MOD_DCC_BLOCK_256B;
776 modifier |= AMD_FMT_MOD_SET(DCC, 1) |
777 AMD_FMT_MOD_SET(DCC_CONSTANT_ENCODE, dcc_constant_encode) |
778 AMD_FMT_MOD_SET(DCC_INDEPENDENT_64B, dcc_i64b) |
779 AMD_FMT_MOD_SET(DCC_INDEPENDENT_128B, dcc_i128b) |
780 AMD_FMT_MOD_SET(DCC_MAX_COMPRESSED_BLOCK, max_cblock_size);
782 afb->base.offsets[1] = dcc_offset * 256 + afb->base.offsets[0];
783 afb->base.pitches[1] =
784 AMDGPU_TILING_GET(afb->tiling_flags, DCC_PITCH_MAX) + 1;
787 * If the userspace driver uses retiling the tiling flags do not contain
788 * info on the renderable DCC buffer. Luckily the opaque metadata contains
789 * the info so we can try to extract it. The kernel does not use this info
790 * but we should convert it to a modifier plane for getfb2, so the
791 * userspace driver that gets it doesn't have to juggle around another DCC
794 if (extract_render_dcc_offset(adev, afb->base.obj[0],
795 &render_dcc_offset) == 0 &&
796 render_dcc_offset != 0 &&
797 render_dcc_offset != afb->base.offsets[1] &&
798 render_dcc_offset < UINT_MAX) {
799 uint32_t dcc_block_bits; /* of base surface data */
801 modifier |= AMD_FMT_MOD_SET(DCC_RETILE, 1);
802 afb->base.offsets[2] = render_dcc_offset;
804 if (adev->family >= AMDGPU_FAMILY_NV) {
807 if (adev->asic_type >= CHIP_SIENNA_CICHLID &&
808 pipes == packers && pipes > 1)
811 dcc_block_bits = max(20, 16 + pipes + extra_pipe);
813 modifier |= AMD_FMT_MOD_SET(RB, rb) |
814 AMD_FMT_MOD_SET(PIPE, pipes);
815 dcc_block_bits = max(20, 18 + rb);
818 dcc_block_bits -= ilog2(afb->base.format->cpp[0]);
819 afb->base.pitches[2] = ALIGN(afb->base.width,
820 1u << ((dcc_block_bits + 1) / 2));
822 format_info = amdgpu_lookup_format_info(afb->base.format->format,
827 afb->base.format = format_info;
831 afb->base.modifier = modifier;
832 afb->base.flags |= DRM_MODE_FB_MODIFIERS;
836 /* Mirrors the is_displayable check in radeonsi's gfx6_compute_surface */
837 static int check_tiling_flags_gfx6(struct amdgpu_framebuffer *afb)
841 /* Zero swizzle mode means linear */
842 if (AMDGPU_TILING_GET(afb->tiling_flags, SWIZZLE_MODE) == 0)
845 micro_tile_mode = AMDGPU_TILING_GET(afb->tiling_flags, MICRO_TILE_MODE);
846 switch (micro_tile_mode) {
847 case 0: /* DISPLAY */
851 drm_dbg_kms(afb->base.dev,
852 "Micro tile mode %llu not supported for scanout\n",
858 static void get_block_dimensions(unsigned int block_log2, unsigned int cpp,
859 unsigned int *width, unsigned int *height)
861 unsigned int cpp_log2 = ilog2(cpp);
862 unsigned int pixel_log2 = block_log2 - cpp_log2;
863 unsigned int width_log2 = (pixel_log2 + 1) / 2;
864 unsigned int height_log2 = pixel_log2 - width_log2;
866 *width = 1 << width_log2;
867 *height = 1 << height_log2;
870 static unsigned int get_dcc_block_size(uint64_t modifier, bool rb_aligned,
873 unsigned int ver = AMD_FMT_MOD_GET(TILE_VERSION, modifier);
876 case AMD_FMT_MOD_TILE_VER_GFX9: {
878 * TODO: for pipe aligned we may need to check the alignment of the
879 * total size of the surface, which may need to be bigger than the
880 * natural alignment due to some HW workarounds
882 return max(10 + (rb_aligned ? (int)AMD_FMT_MOD_GET(RB, modifier) : 0), 12);
884 case AMD_FMT_MOD_TILE_VER_GFX10:
885 case AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS: {
886 int pipes_log2 = AMD_FMT_MOD_GET(PIPE_XOR_BITS, modifier);
888 if (ver == AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS && pipes_log2 > 1 &&
889 AMD_FMT_MOD_GET(PACKERS, modifier) == pipes_log2)
892 return max(8 + (pipe_aligned ? pipes_log2 : 0), 12);
899 static int amdgpu_display_verify_plane(struct amdgpu_framebuffer *rfb, int plane,
900 const struct drm_format_info *format,
901 unsigned int block_width, unsigned int block_height,
902 unsigned int block_size_log2)
904 unsigned int width = rfb->base.width /
905 ((plane && plane < format->num_planes) ? format->hsub : 1);
906 unsigned int height = rfb->base.height /
907 ((plane && plane < format->num_planes) ? format->vsub : 1);
908 unsigned int cpp = plane < format->num_planes ? format->cpp[plane] : 1;
909 unsigned int block_pitch = block_width * cpp;
910 unsigned int min_pitch = ALIGN(width * cpp, block_pitch);
911 unsigned int block_size = 1 << block_size_log2;
914 if (rfb->base.pitches[plane] % block_pitch) {
915 drm_dbg_kms(rfb->base.dev,
916 "pitch %d for plane %d is not a multiple of block pitch %d\n",
917 rfb->base.pitches[plane], plane, block_pitch);
920 if (rfb->base.pitches[plane] < min_pitch) {
921 drm_dbg_kms(rfb->base.dev,
922 "pitch %d for plane %d is less than minimum pitch %d\n",
923 rfb->base.pitches[plane], plane, min_pitch);
927 /* Force at least natural alignment. */
928 if (rfb->base.offsets[plane] % block_size) {
929 drm_dbg_kms(rfb->base.dev,
930 "offset 0x%x for plane %d is not a multiple of block pitch 0x%x\n",
931 rfb->base.offsets[plane], plane, block_size);
935 size = rfb->base.offsets[plane] +
936 (uint64_t)rfb->base.pitches[plane] / block_pitch *
937 block_size * DIV_ROUND_UP(height, block_height);
939 if (rfb->base.obj[0]->size < size) {
940 drm_dbg_kms(rfb->base.dev,
941 "BO size 0x%zx is less than 0x%llx required for plane %d\n",
942 rfb->base.obj[0]->size, size, plane);
950 static int amdgpu_display_verify_sizes(struct amdgpu_framebuffer *rfb)
952 const struct drm_format_info *format_info = drm_format_info(rfb->base.format->format);
953 uint64_t modifier = rfb->base.modifier;
955 unsigned int i, block_width, block_height, block_size_log2;
957 if (!rfb->base.dev->mode_config.allow_fb_modifiers)
960 for (i = 0; i < format_info->num_planes; ++i) {
961 if (modifier == DRM_FORMAT_MOD_LINEAR) {
962 block_width = 256 / format_info->cpp[i];
966 int swizzle = AMD_FMT_MOD_GET(TILE, modifier);
968 switch ((swizzle & ~3) + 1) {
974 block_size_log2 = 12;
979 block_size_log2 = 16;
982 drm_dbg_kms(rfb->base.dev,
983 "Swizzle mode with unknown block size: %d\n", swizzle);
987 get_block_dimensions(block_size_log2, format_info->cpp[i],
988 &block_width, &block_height);
991 ret = amdgpu_display_verify_plane(rfb, i, format_info,
992 block_width, block_height, block_size_log2);
997 if (AMD_FMT_MOD_GET(DCC, modifier)) {
998 if (AMD_FMT_MOD_GET(DCC_RETILE, modifier)) {
999 block_size_log2 = get_dcc_block_size(modifier, false, false);
1000 get_block_dimensions(block_size_log2 + 8, format_info->cpp[0],
1001 &block_width, &block_height);
1002 ret = amdgpu_display_verify_plane(rfb, i, format_info,
1003 block_width, block_height,
1009 block_size_log2 = get_dcc_block_size(modifier, true, true);
1011 bool pipe_aligned = AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier);
1013 block_size_log2 = get_dcc_block_size(modifier, true, pipe_aligned);
1015 get_block_dimensions(block_size_log2 + 8, format_info->cpp[0],
1016 &block_width, &block_height);
1017 ret = amdgpu_display_verify_plane(rfb, i, format_info,
1018 block_width, block_height, block_size_log2);
1026 static int amdgpu_display_get_fb_info(const struct amdgpu_framebuffer *amdgpu_fb,
1027 uint64_t *tiling_flags, bool *tmz_surface)
1029 struct amdgpu_bo *rbo;
1034 *tmz_surface = false;
1038 rbo = gem_to_amdgpu_bo(amdgpu_fb->base.obj[0]);
1039 r = amdgpu_bo_reserve(rbo, false);
1042 /* Don't show error message when returning -ERESTARTSYS */
1043 if (r != -ERESTARTSYS)
1044 DRM_ERROR("Unable to reserve buffer: %d\n", r);
1049 amdgpu_bo_get_tiling_flags(rbo, tiling_flags);
1052 *tmz_surface = amdgpu_bo_encrypted(rbo);
1054 amdgpu_bo_unreserve(rbo);
1059 int amdgpu_display_gem_fb_init(struct drm_device *dev,
1060 struct amdgpu_framebuffer *rfb,
1061 const struct drm_mode_fb_cmd2 *mode_cmd,
1062 struct drm_gem_object *obj)
1066 rfb->base.obj[0] = obj;
1067 drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
1069 ret = amdgpu_display_framebuffer_init(dev, rfb, mode_cmd, obj);
1073 ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
1079 drm_dbg_kms(dev, "Failed to init gem fb: %d\n", ret);
1080 rfb->base.obj[0] = NULL;
1084 int amdgpu_display_gem_fb_verify_and_init(
1085 struct drm_device *dev, struct amdgpu_framebuffer *rfb,
1086 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd,
1087 struct drm_gem_object *obj)
1091 rfb->base.obj[0] = obj;
1092 drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
1093 /* Verify that the modifier is supported. */
1094 if (!drm_any_plane_has_format(dev, mode_cmd->pixel_format,
1095 mode_cmd->modifier[0])) {
1097 "unsupported pixel format %p4cc / modifier 0x%llx\n",
1098 &mode_cmd->pixel_format, mode_cmd->modifier[0]);
1104 ret = amdgpu_display_framebuffer_init(dev, rfb, mode_cmd, obj);
1108 ret = drm_framebuffer_init(dev, &rfb->base, &amdgpu_fb_funcs);
1114 drm_dbg_kms(dev, "Failed to verify and init gem fb: %d\n", ret);
1115 rfb->base.obj[0] = NULL;
1119 int amdgpu_display_framebuffer_init(struct drm_device *dev,
1120 struct amdgpu_framebuffer *rfb,
1121 const struct drm_mode_fb_cmd2 *mode_cmd,
1122 struct drm_gem_object *obj)
1124 struct amdgpu_device *adev = drm_to_adev(dev);
1128 * This needs to happen before modifier conversion as that might change
1129 * the number of planes.
1131 for (i = 1; i < rfb->base.format->num_planes; ++i) {
1132 if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
1133 drm_dbg_kms(dev, "Plane 0 and %d have different BOs: %u vs. %u\n",
1134 i, mode_cmd->handles[0], mode_cmd->handles[i]);
1140 ret = amdgpu_display_get_fb_info(rfb, &rfb->tiling_flags, &rfb->tmz_surface);
1144 if (!dev->mode_config.allow_fb_modifiers && !adev->enable_virtual_display) {
1145 drm_WARN_ONCE(dev, adev->family >= AMDGPU_FAMILY_AI,
1146 "GFX9+ requires FB check based on format modifier\n");
1147 ret = check_tiling_flags_gfx6(rfb);
1152 if (dev->mode_config.allow_fb_modifiers &&
1153 !(rfb->base.flags & DRM_MODE_FB_MODIFIERS)) {
1154 ret = convert_tiling_flags_to_modifier(rfb);
1156 drm_dbg_kms(dev, "Failed to convert tiling flags 0x%llX to a modifier",
1162 ret = amdgpu_display_verify_sizes(rfb);
1166 for (i = 0; i < rfb->base.format->num_planes; ++i) {
1167 drm_gem_object_get(rfb->base.obj[0]);
1168 rfb->base.obj[i] = rfb->base.obj[0];
1174 struct drm_framebuffer *
1175 amdgpu_display_user_framebuffer_create(struct drm_device *dev,
1176 struct drm_file *file_priv,
1177 const struct drm_mode_fb_cmd2 *mode_cmd)
1179 struct amdgpu_framebuffer *amdgpu_fb;
1180 struct drm_gem_object *obj;
1181 struct amdgpu_bo *bo;
1185 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1187 drm_dbg_kms(dev, "No GEM object associated to handle 0x%08X, "
1188 "can't create framebuffer\n", mode_cmd->handles[0]);
1189 return ERR_PTR(-ENOENT);
1192 /* Handle is imported dma-buf, so cannot be migrated to VRAM for scanout */
1193 bo = gem_to_amdgpu_bo(obj);
1194 domains = amdgpu_display_supported_domains(drm_to_adev(dev), bo->flags);
1195 if (obj->import_attach && !(domains & AMDGPU_GEM_DOMAIN_GTT)) {
1196 drm_dbg_kms(dev, "Cannot create framebuffer from imported dma_buf\n");
1197 drm_gem_object_put(obj);
1198 return ERR_PTR(-EINVAL);
1201 amdgpu_fb = kzalloc(sizeof(*amdgpu_fb), GFP_KERNEL);
1202 if (amdgpu_fb == NULL) {
1203 drm_gem_object_put(obj);
1204 return ERR_PTR(-ENOMEM);
1207 ret = amdgpu_display_gem_fb_verify_and_init(dev, amdgpu_fb, file_priv,
1211 drm_gem_object_put(obj);
1212 return ERR_PTR(ret);
1215 drm_gem_object_put(obj);
1216 return &amdgpu_fb->base;
1219 const struct drm_mode_config_funcs amdgpu_mode_funcs = {
1220 .fb_create = amdgpu_display_user_framebuffer_create,
1221 .output_poll_changed = drm_fb_helper_output_poll_changed,
1224 static const struct drm_prop_enum_list amdgpu_underscan_enum_list[] =
1225 { { UNDERSCAN_OFF, "off" },
1226 { UNDERSCAN_ON, "on" },
1227 { UNDERSCAN_AUTO, "auto" },
1230 static const struct drm_prop_enum_list amdgpu_audio_enum_list[] =
1231 { { AMDGPU_AUDIO_DISABLE, "off" },
1232 { AMDGPU_AUDIO_ENABLE, "on" },
1233 { AMDGPU_AUDIO_AUTO, "auto" },
1236 /* XXX support different dither options? spatial, temporal, both, etc. */
1237 static const struct drm_prop_enum_list amdgpu_dither_enum_list[] =
1238 { { AMDGPU_FMT_DITHER_DISABLE, "off" },
1239 { AMDGPU_FMT_DITHER_ENABLE, "on" },
1242 int amdgpu_display_modeset_create_props(struct amdgpu_device *adev)
1246 adev->mode_info.coherent_mode_property =
1247 drm_property_create_range(adev_to_drm(adev), 0, "coherent", 0, 1);
1248 if (!adev->mode_info.coherent_mode_property)
1251 adev->mode_info.load_detect_property =
1252 drm_property_create_range(adev_to_drm(adev), 0, "load detection", 0, 1);
1253 if (!adev->mode_info.load_detect_property)
1256 drm_mode_create_scaling_mode_property(adev_to_drm(adev));
1258 sz = ARRAY_SIZE(amdgpu_underscan_enum_list);
1259 adev->mode_info.underscan_property =
1260 drm_property_create_enum(adev_to_drm(adev), 0,
1262 amdgpu_underscan_enum_list, sz);
1264 adev->mode_info.underscan_hborder_property =
1265 drm_property_create_range(adev_to_drm(adev), 0,
1266 "underscan hborder", 0, 128);
1267 if (!adev->mode_info.underscan_hborder_property)
1270 adev->mode_info.underscan_vborder_property =
1271 drm_property_create_range(adev_to_drm(adev), 0,
1272 "underscan vborder", 0, 128);
1273 if (!adev->mode_info.underscan_vborder_property)
1276 sz = ARRAY_SIZE(amdgpu_audio_enum_list);
1277 adev->mode_info.audio_property =
1278 drm_property_create_enum(adev_to_drm(adev), 0,
1280 amdgpu_audio_enum_list, sz);
1282 sz = ARRAY_SIZE(amdgpu_dither_enum_list);
1283 adev->mode_info.dither_property =
1284 drm_property_create_enum(adev_to_drm(adev), 0,
1286 amdgpu_dither_enum_list, sz);
1288 if (amdgpu_device_has_dc_support(adev)) {
1289 adev->mode_info.abm_level_property =
1290 drm_property_create_range(adev_to_drm(adev), 0,
1292 if (!adev->mode_info.abm_level_property)
1299 void amdgpu_display_update_priority(struct amdgpu_device *adev)
1301 /* adjustment options for the display watermarks */
1302 if ((amdgpu_disp_priority == 0) || (amdgpu_disp_priority > 2))
1303 adev->mode_info.disp_priority = 0;
1305 adev->mode_info.disp_priority = amdgpu_disp_priority;
1309 static bool amdgpu_display_is_hdtv_mode(const struct drm_display_mode *mode)
1311 /* try and guess if this is a tv or a monitor */
1312 if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
1313 (mode->vdisplay == 576) || /* 576p */
1314 (mode->vdisplay == 720) || /* 720p */
1315 (mode->vdisplay == 1080)) /* 1080p */
1321 bool amdgpu_display_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
1322 const struct drm_display_mode *mode,
1323 struct drm_display_mode *adjusted_mode)
1325 struct drm_device *dev = crtc->dev;
1326 struct drm_encoder *encoder;
1327 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1328 struct amdgpu_encoder *amdgpu_encoder;
1329 struct drm_connector *connector;
1330 u32 src_v = 1, dst_v = 1;
1331 u32 src_h = 1, dst_h = 1;
1333 amdgpu_crtc->h_border = 0;
1334 amdgpu_crtc->v_border = 0;
1336 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1337 if (encoder->crtc != crtc)
1339 amdgpu_encoder = to_amdgpu_encoder(encoder);
1340 connector = amdgpu_get_connector_for_encoder(encoder);
1343 if (amdgpu_encoder->rmx_type == RMX_OFF)
1344 amdgpu_crtc->rmx_type = RMX_OFF;
1345 else if (mode->hdisplay < amdgpu_encoder->native_mode.hdisplay ||
1346 mode->vdisplay < amdgpu_encoder->native_mode.vdisplay)
1347 amdgpu_crtc->rmx_type = amdgpu_encoder->rmx_type;
1349 amdgpu_crtc->rmx_type = RMX_OFF;
1350 /* copy native mode */
1351 memcpy(&amdgpu_crtc->native_mode,
1352 &amdgpu_encoder->native_mode,
1353 sizeof(struct drm_display_mode));
1354 src_v = crtc->mode.vdisplay;
1355 dst_v = amdgpu_crtc->native_mode.vdisplay;
1356 src_h = crtc->mode.hdisplay;
1357 dst_h = amdgpu_crtc->native_mode.hdisplay;
1359 /* fix up for overscan on hdmi */
1360 if ((!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
1361 ((amdgpu_encoder->underscan_type == UNDERSCAN_ON) ||
1362 ((amdgpu_encoder->underscan_type == UNDERSCAN_AUTO) &&
1363 connector->display_info.is_hdmi &&
1364 amdgpu_display_is_hdtv_mode(mode)))) {
1365 if (amdgpu_encoder->underscan_hborder != 0)
1366 amdgpu_crtc->h_border = amdgpu_encoder->underscan_hborder;
1368 amdgpu_crtc->h_border = (mode->hdisplay >> 5) + 16;
1369 if (amdgpu_encoder->underscan_vborder != 0)
1370 amdgpu_crtc->v_border = amdgpu_encoder->underscan_vborder;
1372 amdgpu_crtc->v_border = (mode->vdisplay >> 5) + 16;
1373 amdgpu_crtc->rmx_type = RMX_FULL;
1374 src_v = crtc->mode.vdisplay;
1375 dst_v = crtc->mode.vdisplay - (amdgpu_crtc->v_border * 2);
1376 src_h = crtc->mode.hdisplay;
1377 dst_h = crtc->mode.hdisplay - (amdgpu_crtc->h_border * 2);
1380 if (amdgpu_crtc->rmx_type != RMX_OFF) {
1382 a.full = dfixed_const(src_v);
1383 b.full = dfixed_const(dst_v);
1384 amdgpu_crtc->vsc.full = dfixed_div(a, b);
1385 a.full = dfixed_const(src_h);
1386 b.full = dfixed_const(dst_h);
1387 amdgpu_crtc->hsc.full = dfixed_div(a, b);
1389 amdgpu_crtc->vsc.full = dfixed_const(1);
1390 amdgpu_crtc->hsc.full = dfixed_const(1);
1396 * Retrieve current video scanout position of crtc on a given gpu, and
1397 * an optional accurate timestamp of when query happened.
1399 * \param dev Device to query.
1400 * \param pipe Crtc to query.
1401 * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
1402 * For driver internal use only also supports these flags:
1404 * USE_REAL_VBLANKSTART to use the real start of vblank instead
1405 * of a fudged earlier start of vblank.
1407 * GET_DISTANCE_TO_VBLANKSTART to return distance to the
1408 * fudged earlier start of vblank in *vpos and the distance
1409 * to true start of vblank in *hpos.
1411 * \param *vpos Location where vertical scanout position should be stored.
1412 * \param *hpos Location where horizontal scanout position should go.
1413 * \param *stime Target location for timestamp taken immediately before
1414 * scanout position query. Can be NULL to skip timestamp.
1415 * \param *etime Target location for timestamp taken immediately after
1416 * scanout position query. Can be NULL to skip timestamp.
1418 * Returns vpos as a positive number while in active scanout area.
1419 * Returns vpos as a negative number inside vblank, counting the number
1420 * of scanlines to go until end of vblank, e.g., -1 means "one scanline
1421 * until start of active scanout / end of vblank."
1423 * \return Flags, or'ed together as follows:
1425 * DRM_SCANOUTPOS_VALID = Query successful.
1426 * DRM_SCANOUTPOS_INVBL = Inside vblank.
1427 * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
1428 * this flag means that returned position may be offset by a constant but
1429 * unknown small number of scanlines wrt. real scanout position.
1432 int amdgpu_display_get_crtc_scanoutpos(struct drm_device *dev,
1433 unsigned int pipe, unsigned int flags, int *vpos,
1434 int *hpos, ktime_t *stime, ktime_t *etime,
1435 const struct drm_display_mode *mode)
1437 u32 vbl = 0, position = 0;
1438 int vbl_start, vbl_end, vtotal, ret = 0;
1441 struct amdgpu_device *adev = drm_to_adev(dev);
1443 /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
1445 /* Get optional system timestamp before query. */
1447 *stime = ktime_get();
1449 if (amdgpu_display_page_flip_get_scanoutpos(adev, pipe, &vbl, &position) == 0)
1450 ret |= DRM_SCANOUTPOS_VALID;
1452 /* Get optional system timestamp after query. */
1454 *etime = ktime_get();
1456 /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1458 /* Decode into vertical and horizontal scanout position. */
1459 *vpos = position & 0x1fff;
1460 *hpos = (position >> 16) & 0x1fff;
1462 /* Valid vblank area boundaries from gpu retrieved? */
1465 ret |= DRM_SCANOUTPOS_ACCURATE;
1466 vbl_start = vbl & 0x1fff;
1467 vbl_end = (vbl >> 16) & 0x1fff;
1470 /* No: Fake something reasonable which gives at least ok results. */
1471 vbl_start = mode->crtc_vdisplay;
1475 /* Called from driver internal vblank counter query code? */
1476 if (flags & GET_DISTANCE_TO_VBLANKSTART) {
1477 /* Caller wants distance from real vbl_start in *hpos */
1478 *hpos = *vpos - vbl_start;
1481 /* Fudge vblank to start a few scanlines earlier to handle the
1482 * problem that vblank irqs fire a few scanlines before start
1483 * of vblank. Some driver internal callers need the true vblank
1484 * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
1486 * The cause of the "early" vblank irq is that the irq is triggered
1487 * by the line buffer logic when the line buffer read position enters
1488 * the vblank, whereas our crtc scanout position naturally lags the
1489 * line buffer read position.
1491 if (!(flags & USE_REAL_VBLANKSTART))
1492 vbl_start -= adev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
1494 /* Test scanout position against vblank region. */
1495 if ((*vpos < vbl_start) && (*vpos >= vbl_end))
1500 ret |= DRM_SCANOUTPOS_IN_VBLANK;
1502 /* Called from driver internal vblank counter query code? */
1503 if (flags & GET_DISTANCE_TO_VBLANKSTART) {
1504 /* Caller wants distance from fudged earlier vbl_start */
1509 /* Check if inside vblank area and apply corrective offsets:
1510 * vpos will then be >=0 in video scanout area, but negative
1511 * within vblank area, counting down the number of lines until
1515 /* Inside "upper part" of vblank area? Apply corrective offset if so: */
1516 if (in_vbl && (*vpos >= vbl_start)) {
1517 vtotal = mode->crtc_vtotal;
1519 /* With variable refresh rate displays the vpos can exceed
1520 * the vtotal value. Clamp to 0 to return -vbl_end instead
1521 * of guessing the remaining number of lines until scanout.
1523 *vpos = (*vpos < vtotal) ? (*vpos - vtotal) : 0;
1526 /* Correct for shifted end of vbl at vbl_end. */
1527 *vpos = *vpos - vbl_end;
1532 int amdgpu_display_crtc_idx_to_irq_type(struct amdgpu_device *adev, int crtc)
1534 if (crtc < 0 || crtc >= adev->mode_info.num_crtc)
1535 return AMDGPU_CRTC_IRQ_NONE;
1539 return AMDGPU_CRTC_IRQ_VBLANK1;
1541 return AMDGPU_CRTC_IRQ_VBLANK2;
1543 return AMDGPU_CRTC_IRQ_VBLANK3;
1545 return AMDGPU_CRTC_IRQ_VBLANK4;
1547 return AMDGPU_CRTC_IRQ_VBLANK5;
1549 return AMDGPU_CRTC_IRQ_VBLANK6;
1551 return AMDGPU_CRTC_IRQ_NONE;
1555 bool amdgpu_crtc_get_scanout_position(struct drm_crtc *crtc,
1556 bool in_vblank_irq, int *vpos,
1557 int *hpos, ktime_t *stime, ktime_t *etime,
1558 const struct drm_display_mode *mode)
1560 struct drm_device *dev = crtc->dev;
1561 unsigned int pipe = crtc->index;
1563 return amdgpu_display_get_crtc_scanoutpos(dev, pipe, 0, vpos, hpos,
1564 stime, etime, mode);
1567 int amdgpu_display_suspend_helper(struct amdgpu_device *adev)
1569 struct drm_device *dev = adev_to_drm(adev);
1570 struct drm_crtc *crtc;
1571 struct drm_connector *connector;
1572 struct drm_connector_list_iter iter;
1575 /* turn off display hw */
1576 drm_modeset_lock_all(dev);
1577 drm_connector_list_iter_begin(dev, &iter);
1578 drm_for_each_connector_iter(connector, &iter)
1579 drm_helper_connector_dpms(connector,
1581 drm_connector_list_iter_end(&iter);
1582 drm_modeset_unlock_all(dev);
1583 /* unpin the front buffers and cursors */
1584 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1585 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1586 struct drm_framebuffer *fb = crtc->primary->fb;
1587 struct amdgpu_bo *robj;
1589 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
1590 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1591 r = amdgpu_bo_reserve(aobj, true);
1593 amdgpu_bo_unpin(aobj);
1594 amdgpu_bo_unreserve(aobj);
1598 if (fb == NULL || fb->obj[0] == NULL) {
1601 robj = gem_to_amdgpu_bo(fb->obj[0]);
1602 r = amdgpu_bo_reserve(robj, true);
1604 amdgpu_bo_unpin(robj);
1605 amdgpu_bo_unreserve(robj);
1611 int amdgpu_display_resume_helper(struct amdgpu_device *adev)
1613 struct drm_device *dev = adev_to_drm(adev);
1614 struct drm_connector *connector;
1615 struct drm_connector_list_iter iter;
1616 struct drm_crtc *crtc;
1620 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1621 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1623 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
1624 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1625 r = amdgpu_bo_reserve(aobj, true);
1627 r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
1629 dev_err(adev->dev, "Failed to pin cursor BO (%d)\n", r);
1630 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
1631 amdgpu_bo_unreserve(aobj);
1636 drm_helper_resume_force_mode(dev);
1638 /* turn on display hw */
1639 drm_modeset_lock_all(dev);
1641 drm_connector_list_iter_begin(dev, &iter);
1642 drm_for_each_connector_iter(connector, &iter)
1643 drm_helper_connector_dpms(connector,
1645 drm_connector_list_iter_end(&iter);
1647 drm_modeset_unlock_all(dev);