xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_isp.c (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  */
27 
28 #include <linux/firmware.h>
29 #include <linux/mfd/core.h>
30 
31 #include "amdgpu.h"
32 #include "amdgpu_isp.h"
33 #include "isp_v4_1_0.h"
34 #include "isp_v4_1_1.h"
35 
36 #define ISP_MC_ADDR_ALIGN (1024 * 32)
37 
38 /**
39  * isp_hw_init - start and test isp block
40  *
41  * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
42  *
43  */
44 static int isp_hw_init(struct amdgpu_ip_block *ip_block)
45 {
46 	struct amdgpu_device *adev = ip_block->adev;
47 	struct amdgpu_isp *isp = &adev->isp;
48 
49 	if (isp->funcs->hw_init != NULL)
50 		return isp->funcs->hw_init(isp);
51 
52 	return -ENODEV;
53 }
54 
55 /**
56  * isp_hw_fini - stop the hardware block
57  *
58  * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
59  *
60  */
61 static int isp_hw_fini(struct amdgpu_ip_block *ip_block)
62 {
63 	struct amdgpu_isp *isp = &ip_block->adev->isp;
64 
65 	if (isp->funcs->hw_fini != NULL)
66 		return isp->funcs->hw_fini(isp);
67 
68 	return -ENODEV;
69 }
70 
71 static int isp_load_fw_by_psp(struct amdgpu_device *adev)
72 {
73 	const struct common_firmware_header *hdr;
74 	char ucode_prefix[10];
75 	int r = 0;
76 
77 	/* get isp fw binary name and path */
78 	amdgpu_ucode_ip_version_decode(adev, ISP_HWIP, ucode_prefix,
79 				       sizeof(ucode_prefix));
80 
81 	/* read isp fw */
82 	r = amdgpu_ucode_request(adev, &adev->isp.fw, AMDGPU_UCODE_OPTIONAL,
83 				"amdgpu/%s.bin", ucode_prefix);
84 	if (r) {
85 		amdgpu_ucode_release(&adev->isp.fw);
86 		return r;
87 	}
88 
89 	hdr = (const struct common_firmware_header *)adev->isp.fw->data;
90 
91 	adev->firmware.ucode[AMDGPU_UCODE_ID_ISP].ucode_id =
92 		AMDGPU_UCODE_ID_ISP;
93 	adev->firmware.ucode[AMDGPU_UCODE_ID_ISP].fw = adev->isp.fw;
94 
95 	adev->firmware.fw_size +=
96 		ALIGN(le32_to_cpu(hdr->ucode_size_bytes), PAGE_SIZE);
97 
98 	return r;
99 }
100 
101 static int isp_early_init(struct amdgpu_ip_block *ip_block)
102 {
103 
104 	struct amdgpu_device *adev = ip_block->adev;
105 	struct amdgpu_isp *isp = &adev->isp;
106 
107 	switch (amdgpu_ip_version(adev, ISP_HWIP, 0)) {
108 	case IP_VERSION(4, 1, 0):
109 		isp_v4_1_0_set_isp_funcs(isp);
110 		break;
111 	case IP_VERSION(4, 1, 1):
112 		isp_v4_1_1_set_isp_funcs(isp);
113 		break;
114 	default:
115 		return -EINVAL;
116 	}
117 
118 	isp->adev = adev;
119 	isp->parent = adev->dev;
120 
121 	if (isp_load_fw_by_psp(adev)) {
122 		DRM_DEBUG_DRIVER("%s: isp fw load failed\n", __func__);
123 		return -ENOENT;
124 	}
125 
126 	return 0;
127 }
128 
129 static int isp_set_clockgating_state(struct amdgpu_ip_block *ip_block,
130 				     enum amd_clockgating_state state)
131 {
132 	return 0;
133 }
134 
135 static int isp_set_powergating_state(struct amdgpu_ip_block *ip_block,
136 				     enum amd_powergating_state state)
137 {
138 	return 0;
139 }
140 
141 static int is_valid_isp_device(struct device *isp_parent, struct device *amdgpu_dev)
142 {
143 	if (isp_parent != amdgpu_dev)
144 		return -EINVAL;
145 
146 	return 0;
147 }
148 
149 /**
150  * isp_user_buffer_alloc - create user buffer object (BO) for isp
151  *
152  * @dev: isp device handle
153  * @dmabuf: DMABUF handle for isp buffer allocated in system memory
154  * @buf_obj: GPU buffer object handle to initialize
155  * @buf_addr: GPU addr of the pinned BO to initialize
156  *
157  * Imports isp DMABUF to allocate and pin a user BO for isp internal use. It does
158  * GART alloc to generate GPU addr for BO to make it accessible through the
159  * GART aperture for ISP HW.
160  *
161  * This function is exported to allow the V4L2 isp device external to drm device
162  * to create and access the isp user BO.
163  *
164  * Returns:
165  * 0 on success, negative error code otherwise.
166  */
167 int isp_user_buffer_alloc(struct device *dev, void *dmabuf,
168 			  void **buf_obj, u64 *buf_addr)
169 {
170 	struct platform_device *ispdev = to_platform_device(dev);
171 	const struct isp_platform_data *isp_pdata;
172 	struct amdgpu_device *adev;
173 	struct mfd_cell *mfd_cell;
174 	struct amdgpu_bo *bo;
175 	u64 gpu_addr;
176 	int ret;
177 
178 	if (WARN_ON(!ispdev))
179 		return -ENODEV;
180 
181 	if (WARN_ON(!buf_obj))
182 		return -EINVAL;
183 
184 	if (WARN_ON(!buf_addr))
185 		return -EINVAL;
186 
187 	mfd_cell = &ispdev->mfd_cell[0];
188 	if (!mfd_cell)
189 		return -ENODEV;
190 
191 	isp_pdata = mfd_cell->platform_data;
192 	adev = isp_pdata->adev;
193 
194 	ret = is_valid_isp_device(ispdev->dev.parent, adev->dev);
195 	if (ret)
196 		return ret;
197 
198 	ret = amdgpu_bo_create_isp_user(adev, dmabuf,
199 					AMDGPU_GEM_DOMAIN_GTT, &bo, &gpu_addr);
200 	if (ret) {
201 		drm_err(&adev->ddev, "failed to alloc gart user buffer (%d)", ret);
202 		return ret;
203 	}
204 
205 	*buf_obj = (void *)bo;
206 	*buf_addr = gpu_addr;
207 
208 	return 0;
209 }
210 EXPORT_SYMBOL(isp_user_buffer_alloc);
211 
212 /**
213  * isp_user_buffer_free - free isp user buffer object (BO)
214  *
215  * @buf_obj: amdgpu isp user BO to free
216  *
217  * unpin and unref BO for isp internal use.
218  *
219  * This function is exported to allow the V4L2 isp device
220  * external to drm device to free the isp user BO.
221  */
222 void isp_user_buffer_free(void *buf_obj)
223 {
224 	amdgpu_bo_free_isp_user(buf_obj);
225 }
226 EXPORT_SYMBOL(isp_user_buffer_free);
227 
228 /**
229  * isp_kernel_buffer_alloc - create kernel buffer object (BO) for isp
230  *
231  * @dev: isp device handle
232  * @size: size for the new BO
233  * @buf_obj: GPU BO handle to initialize
234  * @gpu_addr: GPU addr of the pinned BO
235  * @cpu_addr: CPU address mapping of BO
236  *
237  * Allocates and pins a kernel BO for internal isp firmware use.
238  *
239  * This function is exported to allow the V4L2 isp device
240  * external to drm device to create and access the kernel BO.
241  *
242  * Returns:
243  * 0 on success, negative error code otherwise.
244  */
245 int isp_kernel_buffer_alloc(struct device *dev, u64 size,
246 			    void **buf_obj, u64 *gpu_addr, void **cpu_addr)
247 {
248 	struct platform_device *ispdev = to_platform_device(dev);
249 	struct amdgpu_bo **bo = (struct amdgpu_bo **)buf_obj;
250 	const struct isp_platform_data *isp_pdata;
251 	struct amdgpu_device *adev;
252 	struct mfd_cell *mfd_cell;
253 	int ret;
254 
255 	if (WARN_ON(!ispdev))
256 		return -ENODEV;
257 
258 	if (WARN_ON(!buf_obj))
259 		return -EINVAL;
260 
261 	if (WARN_ON(!gpu_addr))
262 		return -EINVAL;
263 
264 	if (WARN_ON(!cpu_addr))
265 		return -EINVAL;
266 
267 	mfd_cell = &ispdev->mfd_cell[0];
268 	if (!mfd_cell)
269 		return -ENODEV;
270 
271 	isp_pdata = mfd_cell->platform_data;
272 	adev = isp_pdata->adev;
273 
274 	ret = is_valid_isp_device(ispdev->dev.parent, adev->dev);
275 	if (ret)
276 		return ret;
277 
278 	/* Ensure *bo is NULL so a new BO will be created */
279 	*bo = NULL;
280 	ret = amdgpu_bo_create_kernel(adev,
281 				      size,
282 				      ISP_MC_ADDR_ALIGN,
283 				      AMDGPU_GEM_DOMAIN_GTT,
284 				      bo,
285 				      gpu_addr,
286 				      cpu_addr);
287 	if (!cpu_addr || ret) {
288 		drm_err(&adev->ddev, "failed to alloc gart kernel buffer (%d)", ret);
289 		return ret;
290 	}
291 
292 	return 0;
293 }
294 EXPORT_SYMBOL(isp_kernel_buffer_alloc);
295 
296 /**
297  * isp_kernel_buffer_free - free isp kernel buffer object (BO)
298  *
299  * @buf_obj: amdgpu isp user BO to free
300  * @gpu_addr: GPU addr of isp kernel BO
301  * @cpu_addr: CPU addr of isp kernel BO
302  *
303  * unmaps and unpin a isp kernel BO.
304  *
305  * This function is exported to allow the V4L2 isp device
306  * external to drm device to free the kernel BO.
307  */
308 void isp_kernel_buffer_free(void **buf_obj, u64 *gpu_addr, void **cpu_addr)
309 {
310 	struct amdgpu_bo **bo = (struct amdgpu_bo **)buf_obj;
311 
312 	amdgpu_bo_free_kernel(bo, gpu_addr, cpu_addr);
313 }
314 EXPORT_SYMBOL(isp_kernel_buffer_free);
315 
316 static int isp_resume(struct amdgpu_ip_block *ip_block)
317 {
318 	struct amdgpu_device *adev = ip_block->adev;
319 	struct amdgpu_isp *isp = &adev->isp;
320 
321 	if (isp->funcs->hw_resume)
322 		return isp->funcs->hw_resume(isp);
323 
324 	return -ENODEV;
325 }
326 
327 static int isp_suspend(struct amdgpu_ip_block *ip_block)
328 {
329 	struct amdgpu_device *adev = ip_block->adev;
330 	struct amdgpu_isp *isp = &adev->isp;
331 
332 	if (isp->funcs->hw_suspend)
333 		return isp->funcs->hw_suspend(isp);
334 
335 	return -ENODEV;
336 }
337 
338 static const struct amd_ip_funcs isp_ip_funcs = {
339 	.name = "isp_ip",
340 	.early_init = isp_early_init,
341 	.hw_init = isp_hw_init,
342 	.hw_fini = isp_hw_fini,
343 	.suspend = isp_suspend,
344 	.resume = isp_resume,
345 	.set_clockgating_state = isp_set_clockgating_state,
346 	.set_powergating_state = isp_set_powergating_state,
347 };
348 
349 const struct amdgpu_ip_block_version isp_v4_1_0_ip_block = {
350 	.type = AMD_IP_BLOCK_TYPE_ISP,
351 	.major = 4,
352 	.minor = 1,
353 	.rev = 0,
354 	.funcs = &isp_ip_funcs,
355 };
356 
357 const struct amdgpu_ip_block_version isp_v4_1_1_ip_block = {
358 	.type = AMD_IP_BLOCK_TYPE_ISP,
359 	.major = 4,
360 	.minor = 1,
361 	.rev = 1,
362 	.funcs = &isp_ip_funcs,
363 };
364