1dfc03588SAlex Hung // SPDX-License-Identifier: MIT
2dfc03588SAlex Hung /*
3dfc03588SAlex Hung * Copyright 2022 Advanced Micro Devices, Inc.
4dfc03588SAlex Hung *
5dfc03588SAlex Hung * Permission is hereby granted, free of charge, to any person obtaining a
6dfc03588SAlex Hung * copy of this software and associated documentation files (the "Software"),
7dfc03588SAlex Hung * to deal in the Software without restriction, including without limitation
8dfc03588SAlex Hung * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9dfc03588SAlex Hung * and/or sell copies of the Software, and to permit persons to whom the
10dfc03588SAlex Hung * Software is furnished to do so, subject to the following conditions:
11dfc03588SAlex Hung *
12dfc03588SAlex Hung * The above copyright notice and this permission notice shall be included in
13dfc03588SAlex Hung * all copies or substantial portions of the Software.
14dfc03588SAlex Hung *
15dfc03588SAlex Hung * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16dfc03588SAlex Hung * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17dfc03588SAlex Hung * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18dfc03588SAlex Hung * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19dfc03588SAlex Hung * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20dfc03588SAlex Hung * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21dfc03588SAlex Hung * OTHER DEALINGS IN THE SOFTWARE.
22dfc03588SAlex Hung *
23dfc03588SAlex Hung * Authors: AMD
24dfc03588SAlex Hung *
25dfc03588SAlex Hung */
26dfc03588SAlex Hung
27dfc03588SAlex Hung #include "dm_services_types.h"
28dfc03588SAlex Hung
29dfc03588SAlex Hung #include "amdgpu.h"
30dfc03588SAlex Hung #include "amdgpu_dm.h"
31dfc03588SAlex Hung #include "amdgpu_dm_wb.h"
32dfc03588SAlex Hung #include "amdgpu_display.h"
33ff73d4cdSHarry Wentland #include "dc.h"
34dfc03588SAlex Hung
35d9501844SJani Nikula #include <drm/drm_edid.h>
36dfc03588SAlex Hung #include <drm/drm_atomic_state_helper.h>
37dfc03588SAlex Hung #include <drm/drm_modeset_helper_vtables.h>
38dfc03588SAlex Hung
39dfc03588SAlex Hung static const u32 amdgpu_dm_wb_formats[] = {
40dfc03588SAlex Hung DRM_FORMAT_XRGB2101010,
41dfc03588SAlex Hung };
42dfc03588SAlex Hung
amdgpu_dm_wb_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)43dfc03588SAlex Hung static int amdgpu_dm_wb_encoder_atomic_check(struct drm_encoder *encoder,
44dfc03588SAlex Hung struct drm_crtc_state *crtc_state,
45dfc03588SAlex Hung struct drm_connector_state *conn_state)
46dfc03588SAlex Hung {
47dfc03588SAlex Hung struct drm_framebuffer *fb;
48dfc03588SAlex Hung const struct drm_display_mode *mode = &crtc_state->mode;
49dfc03588SAlex Hung bool found = false;
50dfc03588SAlex Hung uint8_t i;
51dfc03588SAlex Hung
52dfc03588SAlex Hung if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
53dfc03588SAlex Hung return 0;
54dfc03588SAlex Hung
55dfc03588SAlex Hung fb = conn_state->writeback_job->fb;
56dfc03588SAlex Hung if (fb->width != mode->hdisplay || fb->height != mode->vdisplay) {
57dfc03588SAlex Hung DRM_DEBUG_KMS("Invalid framebuffer size %ux%u\n",
58dfc03588SAlex Hung fb->width, fb->height);
59dfc03588SAlex Hung return -EINVAL;
60dfc03588SAlex Hung }
61dfc03588SAlex Hung
62dfc03588SAlex Hung for (i = 0; i < sizeof(amdgpu_dm_wb_formats) / sizeof(u32); i++) {
63dfc03588SAlex Hung if (fb->format->format == amdgpu_dm_wb_formats[i])
64dfc03588SAlex Hung found = true;
65dfc03588SAlex Hung }
66dfc03588SAlex Hung
67dfc03588SAlex Hung if (!found) {
68dfc03588SAlex Hung DRM_DEBUG_KMS("Invalid pixel format %p4cc\n",
69dfc03588SAlex Hung &fb->format->format);
70dfc03588SAlex Hung return -EINVAL;
71dfc03588SAlex Hung }
72dfc03588SAlex Hung
73dfc03588SAlex Hung return 0;
74dfc03588SAlex Hung }
75dfc03588SAlex Hung
76dfc03588SAlex Hung
amdgpu_dm_wb_connector_get_modes(struct drm_connector * connector)77dfc03588SAlex Hung static int amdgpu_dm_wb_connector_get_modes(struct drm_connector *connector)
78dfc03588SAlex Hung {
792cc69a10SAlex Hung /* Maximum resolution supported by DWB */
802cc69a10SAlex Hung return drm_add_modes_noedid(connector, 3840, 2160);
81dfc03588SAlex Hung }
82dfc03588SAlex Hung
amdgpu_dm_wb_prepare_job(struct drm_writeback_connector * wb_connector,struct drm_writeback_job * job)83dfc03588SAlex Hung static int amdgpu_dm_wb_prepare_job(struct drm_writeback_connector *wb_connector,
84dfc03588SAlex Hung struct drm_writeback_job *job)
85dfc03588SAlex Hung {
86dfc03588SAlex Hung struct amdgpu_framebuffer *afb;
87dfc03588SAlex Hung struct drm_gem_object *obj;
88dfc03588SAlex Hung struct amdgpu_device *adev;
89dfc03588SAlex Hung struct amdgpu_bo *rbo;
90dfc03588SAlex Hung uint32_t domain;
91dfc03588SAlex Hung int r;
92dfc03588SAlex Hung
93dfc03588SAlex Hung if (!job->fb) {
94dfc03588SAlex Hung DRM_DEBUG_KMS("No FB bound\n");
95dfc03588SAlex Hung return 0;
96dfc03588SAlex Hung }
97dfc03588SAlex Hung
98dfc03588SAlex Hung afb = to_amdgpu_framebuffer(job->fb);
99dfc03588SAlex Hung obj = job->fb->obj[0];
100dfc03588SAlex Hung rbo = gem_to_amdgpu_bo(obj);
101dfc03588SAlex Hung adev = amdgpu_ttm_adev(rbo->tbo.bdev);
102dfc03588SAlex Hung
103dfc03588SAlex Hung r = amdgpu_bo_reserve(rbo, true);
104dfc03588SAlex Hung if (r) {
105dfc03588SAlex Hung dev_err(adev->dev, "fail to reserve bo (%d)\n", r);
106dfc03588SAlex Hung return r;
107dfc03588SAlex Hung }
108dfc03588SAlex Hung
109dfc03588SAlex Hung r = dma_resv_reserve_fences(rbo->tbo.base.resv, 1);
110dfc03588SAlex Hung if (r) {
111dfc03588SAlex Hung dev_err(adev->dev, "reserving fence slot failed (%d)\n", r);
112dfc03588SAlex Hung goto error_unlock;
113dfc03588SAlex Hung }
114dfc03588SAlex Hung
115dfc03588SAlex Hung domain = amdgpu_display_supported_domains(adev, rbo->flags);
116dfc03588SAlex Hung
117*54b86443SChristian König rbo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
118dfc03588SAlex Hung r = amdgpu_bo_pin(rbo, domain);
119dfc03588SAlex Hung if (unlikely(r != 0)) {
120dfc03588SAlex Hung if (r != -ERESTARTSYS)
121dfc03588SAlex Hung DRM_ERROR("Failed to pin framebuffer with error %d\n", r);
122dfc03588SAlex Hung goto error_unlock;
123dfc03588SAlex Hung }
124dfc03588SAlex Hung
125dfc03588SAlex Hung r = amdgpu_ttm_alloc_gart(&rbo->tbo);
126dfc03588SAlex Hung if (unlikely(r != 0)) {
127dfc03588SAlex Hung DRM_ERROR("%p bind failed\n", rbo);
128dfc03588SAlex Hung goto error_unpin;
129dfc03588SAlex Hung }
130dfc03588SAlex Hung
131dfc03588SAlex Hung amdgpu_bo_unreserve(rbo);
132dfc03588SAlex Hung
133dfc03588SAlex Hung afb->address = amdgpu_bo_gpu_offset(rbo);
134dfc03588SAlex Hung
135dfc03588SAlex Hung amdgpu_bo_ref(rbo);
136dfc03588SAlex Hung
137dfc03588SAlex Hung return 0;
138dfc03588SAlex Hung
139dfc03588SAlex Hung error_unpin:
140dfc03588SAlex Hung amdgpu_bo_unpin(rbo);
141dfc03588SAlex Hung
142dfc03588SAlex Hung error_unlock:
143dfc03588SAlex Hung amdgpu_bo_unreserve(rbo);
144dfc03588SAlex Hung return r;
145dfc03588SAlex Hung }
146dfc03588SAlex Hung
amdgpu_dm_wb_cleanup_job(struct drm_writeback_connector * connector,struct drm_writeback_job * job)147dfc03588SAlex Hung static void amdgpu_dm_wb_cleanup_job(struct drm_writeback_connector *connector,
148dfc03588SAlex Hung struct drm_writeback_job *job)
149dfc03588SAlex Hung {
150dfc03588SAlex Hung struct amdgpu_bo *rbo;
151dfc03588SAlex Hung int r;
152dfc03588SAlex Hung
153dfc03588SAlex Hung if (!job->fb)
154dfc03588SAlex Hung return;
155dfc03588SAlex Hung
156dfc03588SAlex Hung rbo = gem_to_amdgpu_bo(job->fb->obj[0]);
157dfc03588SAlex Hung r = amdgpu_bo_reserve(rbo, false);
158dfc03588SAlex Hung if (unlikely(r)) {
159dfc03588SAlex Hung DRM_ERROR("failed to reserve rbo before unpin\n");
160dfc03588SAlex Hung return;
161dfc03588SAlex Hung }
162dfc03588SAlex Hung
163dfc03588SAlex Hung amdgpu_bo_unpin(rbo);
164dfc03588SAlex Hung amdgpu_bo_unreserve(rbo);
165dfc03588SAlex Hung amdgpu_bo_unref(&rbo);
166dfc03588SAlex Hung }
167dfc03588SAlex Hung
168dfc03588SAlex Hung static const struct drm_encoder_helper_funcs amdgpu_dm_wb_encoder_helper_funcs = {
169dfc03588SAlex Hung .atomic_check = amdgpu_dm_wb_encoder_atomic_check,
170dfc03588SAlex Hung };
171dfc03588SAlex Hung
172dfc03588SAlex Hung static const struct drm_connector_funcs amdgpu_dm_wb_connector_funcs = {
173dfc03588SAlex Hung .fill_modes = drm_helper_probe_single_connector_modes,
174dfc03588SAlex Hung .destroy = drm_connector_cleanup,
175dfc03588SAlex Hung .reset = amdgpu_dm_connector_funcs_reset,
176dfc03588SAlex Hung .atomic_duplicate_state = amdgpu_dm_connector_atomic_duplicate_state,
177dfc03588SAlex Hung .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
178dfc03588SAlex Hung };
179dfc03588SAlex Hung
180dfc03588SAlex Hung static const struct drm_connector_helper_funcs amdgpu_dm_wb_conn_helper_funcs = {
181dfc03588SAlex Hung .get_modes = amdgpu_dm_wb_connector_get_modes,
182dfc03588SAlex Hung .prepare_writeback_job = amdgpu_dm_wb_prepare_job,
183dfc03588SAlex Hung .cleanup_writeback_job = amdgpu_dm_wb_cleanup_job,
184dfc03588SAlex Hung };
185dfc03588SAlex Hung
amdgpu_dm_wb_connector_init(struct amdgpu_display_manager * dm,struct amdgpu_dm_wb_connector * wbcon,uint32_t link_index)186dfc03588SAlex Hung int amdgpu_dm_wb_connector_init(struct amdgpu_display_manager *dm,
187ff73d4cdSHarry Wentland struct amdgpu_dm_wb_connector *wbcon,
188ff73d4cdSHarry Wentland uint32_t link_index)
189dfc03588SAlex Hung {
190ff73d4cdSHarry Wentland struct dc *dc = dm->dc;
191ff73d4cdSHarry Wentland struct dc_link *link = dc_get_link_at_index(dc, link_index);
192dfc03588SAlex Hung int res = 0;
193dfc03588SAlex Hung
194ff73d4cdSHarry Wentland wbcon->link = link;
195dfc03588SAlex Hung
196ff73d4cdSHarry Wentland drm_connector_helper_add(&wbcon->base.base, &amdgpu_dm_wb_conn_helper_funcs);
197ff73d4cdSHarry Wentland
198ff73d4cdSHarry Wentland res = drm_writeback_connector_init(&dm->adev->ddev, &wbcon->base,
199dfc03588SAlex Hung &amdgpu_dm_wb_connector_funcs,
200dfc03588SAlex Hung &amdgpu_dm_wb_encoder_helper_funcs,
201dfc03588SAlex Hung amdgpu_dm_wb_formats,
202dfc03588SAlex Hung ARRAY_SIZE(amdgpu_dm_wb_formats),
203dfc03588SAlex Hung amdgpu_dm_get_encoder_crtc_mask(dm->adev));
204dfc03588SAlex Hung
205dfc03588SAlex Hung if (res)
206dfc03588SAlex Hung return res;
207dfc03588SAlex Hung /*
208dfc03588SAlex Hung * Some of the properties below require access to state, like bpc.
209dfc03588SAlex Hung * Allocate some default initial connector state with our reset helper.
210dfc03588SAlex Hung */
211ff73d4cdSHarry Wentland if (wbcon->base.base.funcs->reset)
212ff73d4cdSHarry Wentland wbcon->base.base.funcs->reset(&wbcon->base.base);
213dfc03588SAlex Hung
214dfc03588SAlex Hung return 0;
215dfc03588SAlex Hung }
216