amdgpu_ucode.c revision 1.1 1 /* $NetBSD: amdgpu_ucode.c,v 1.1 2018/08/27 01:34:44 riastradh Exp $ */
2
3 /*
4 * Copyright 2014 Advanced Micro Devices, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: amdgpu_ucode.c,v 1.1 2018/08/27 01:34:44 riastradh Exp $");
28
29 #include <linux/firmware.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <drm/drmP.h>
33 #include "amdgpu.h"
34 #include "amdgpu_ucode.h"
35
36 static void amdgpu_ucode_print_common_hdr(const struct common_firmware_header *hdr)
37 {
38 DRM_DEBUG("size_bytes: %u\n", le32_to_cpu(hdr->size_bytes));
39 DRM_DEBUG("header_size_bytes: %u\n", le32_to_cpu(hdr->header_size_bytes));
40 DRM_DEBUG("header_version_major: %u\n", le16_to_cpu(hdr->header_version_major));
41 DRM_DEBUG("header_version_minor: %u\n", le16_to_cpu(hdr->header_version_minor));
42 DRM_DEBUG("ip_version_major: %u\n", le16_to_cpu(hdr->ip_version_major));
43 DRM_DEBUG("ip_version_minor: %u\n", le16_to_cpu(hdr->ip_version_minor));
44 DRM_DEBUG("ucode_version: 0x%08x\n", le32_to_cpu(hdr->ucode_version));
45 DRM_DEBUG("ucode_size_bytes: %u\n", le32_to_cpu(hdr->ucode_size_bytes));
46 DRM_DEBUG("ucode_array_offset_bytes: %u\n",
47 le32_to_cpu(hdr->ucode_array_offset_bytes));
48 DRM_DEBUG("crc32: 0x%08x\n", le32_to_cpu(hdr->crc32));
49 }
50
51 void amdgpu_ucode_print_mc_hdr(const struct common_firmware_header *hdr)
52 {
53 uint16_t version_major = le16_to_cpu(hdr->header_version_major);
54 uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
55
56 DRM_DEBUG("MC\n");
57 amdgpu_ucode_print_common_hdr(hdr);
58
59 if (version_major == 1) {
60 const struct mc_firmware_header_v1_0 *mc_hdr =
61 container_of(hdr, struct mc_firmware_header_v1_0, header);
62
63 DRM_DEBUG("io_debug_size_bytes: %u\n",
64 le32_to_cpu(mc_hdr->io_debug_size_bytes));
65 DRM_DEBUG("io_debug_array_offset_bytes: %u\n",
66 le32_to_cpu(mc_hdr->io_debug_array_offset_bytes));
67 } else {
68 DRM_ERROR("Unknown MC ucode version: %u.%u\n", version_major, version_minor);
69 }
70 }
71
72 void amdgpu_ucode_print_smc_hdr(const struct common_firmware_header *hdr)
73 {
74 uint16_t version_major = le16_to_cpu(hdr->header_version_major);
75 uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
76
77 DRM_DEBUG("SMC\n");
78 amdgpu_ucode_print_common_hdr(hdr);
79
80 if (version_major == 1) {
81 const struct smc_firmware_header_v1_0 *smc_hdr =
82 container_of(hdr, struct smc_firmware_header_v1_0, header);
83
84 DRM_DEBUG("ucode_start_addr: %u\n", le32_to_cpu(smc_hdr->ucode_start_addr));
85 } else {
86 DRM_ERROR("Unknown SMC ucode version: %u.%u\n", version_major, version_minor);
87 }
88 }
89
90 void amdgpu_ucode_print_gfx_hdr(const struct common_firmware_header *hdr)
91 {
92 uint16_t version_major = le16_to_cpu(hdr->header_version_major);
93 uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
94
95 DRM_DEBUG("GFX\n");
96 amdgpu_ucode_print_common_hdr(hdr);
97
98 if (version_major == 1) {
99 const struct gfx_firmware_header_v1_0 *gfx_hdr =
100 container_of(hdr, struct gfx_firmware_header_v1_0, header);
101
102 DRM_DEBUG("ucode_feature_version: %u\n",
103 le32_to_cpu(gfx_hdr->ucode_feature_version));
104 DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(gfx_hdr->jt_offset));
105 DRM_DEBUG("jt_size: %u\n", le32_to_cpu(gfx_hdr->jt_size));
106 } else {
107 DRM_ERROR("Unknown GFX ucode version: %u.%u\n", version_major, version_minor);
108 }
109 }
110
111 void amdgpu_ucode_print_rlc_hdr(const struct common_firmware_header *hdr)
112 {
113 uint16_t version_major = le16_to_cpu(hdr->header_version_major);
114 uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
115
116 DRM_DEBUG("RLC\n");
117 amdgpu_ucode_print_common_hdr(hdr);
118
119 if (version_major == 1) {
120 const struct rlc_firmware_header_v1_0 *rlc_hdr =
121 container_of(hdr, struct rlc_firmware_header_v1_0, header);
122
123 DRM_DEBUG("ucode_feature_version: %u\n",
124 le32_to_cpu(rlc_hdr->ucode_feature_version));
125 DRM_DEBUG("save_and_restore_offset: %u\n",
126 le32_to_cpu(rlc_hdr->save_and_restore_offset));
127 DRM_DEBUG("clear_state_descriptor_offset: %u\n",
128 le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
129 DRM_DEBUG("avail_scratch_ram_locations: %u\n",
130 le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
131 DRM_DEBUG("master_pkt_description_offset: %u\n",
132 le32_to_cpu(rlc_hdr->master_pkt_description_offset));
133 } else if (version_major == 2) {
134 const struct rlc_firmware_header_v2_0 *rlc_hdr =
135 container_of(hdr, struct rlc_firmware_header_v2_0, header);
136
137 DRM_DEBUG("ucode_feature_version: %u\n",
138 le32_to_cpu(rlc_hdr->ucode_feature_version));
139 DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(rlc_hdr->jt_offset));
140 DRM_DEBUG("jt_size: %u\n", le32_to_cpu(rlc_hdr->jt_size));
141 DRM_DEBUG("save_and_restore_offset: %u\n",
142 le32_to_cpu(rlc_hdr->save_and_restore_offset));
143 DRM_DEBUG("clear_state_descriptor_offset: %u\n",
144 le32_to_cpu(rlc_hdr->clear_state_descriptor_offset));
145 DRM_DEBUG("avail_scratch_ram_locations: %u\n",
146 le32_to_cpu(rlc_hdr->avail_scratch_ram_locations));
147 DRM_DEBUG("reg_restore_list_size: %u\n",
148 le32_to_cpu(rlc_hdr->reg_restore_list_size));
149 DRM_DEBUG("reg_list_format_start: %u\n",
150 le32_to_cpu(rlc_hdr->reg_list_format_start));
151 DRM_DEBUG("reg_list_format_separate_start: %u\n",
152 le32_to_cpu(rlc_hdr->reg_list_format_separate_start));
153 DRM_DEBUG("starting_offsets_start: %u\n",
154 le32_to_cpu(rlc_hdr->starting_offsets_start));
155 DRM_DEBUG("reg_list_format_size_bytes: %u\n",
156 le32_to_cpu(rlc_hdr->reg_list_format_size_bytes));
157 DRM_DEBUG("reg_list_format_array_offset_bytes: %u\n",
158 le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
159 DRM_DEBUG("reg_list_size_bytes: %u\n",
160 le32_to_cpu(rlc_hdr->reg_list_size_bytes));
161 DRM_DEBUG("reg_list_array_offset_bytes: %u\n",
162 le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
163 DRM_DEBUG("reg_list_format_separate_size_bytes: %u\n",
164 le32_to_cpu(rlc_hdr->reg_list_format_separate_size_bytes));
165 DRM_DEBUG("reg_list_format_separate_array_offset_bytes: %u\n",
166 le32_to_cpu(rlc_hdr->reg_list_format_separate_array_offset_bytes));
167 DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
168 le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
169 DRM_DEBUG("reg_list_separate_size_bytes: %u\n",
170 le32_to_cpu(rlc_hdr->reg_list_separate_size_bytes));
171 } else {
172 DRM_ERROR("Unknown RLC ucode version: %u.%u\n", version_major, version_minor);
173 }
174 }
175
176 void amdgpu_ucode_print_sdma_hdr(const struct common_firmware_header *hdr)
177 {
178 uint16_t version_major = le16_to_cpu(hdr->header_version_major);
179 uint16_t version_minor = le16_to_cpu(hdr->header_version_minor);
180
181 DRM_DEBUG("SDMA\n");
182 amdgpu_ucode_print_common_hdr(hdr);
183
184 if (version_major == 1) {
185 const struct sdma_firmware_header_v1_0 *sdma_hdr =
186 container_of(hdr, struct sdma_firmware_header_v1_0, header);
187
188 DRM_DEBUG("ucode_feature_version: %u\n",
189 le32_to_cpu(sdma_hdr->ucode_feature_version));
190 DRM_DEBUG("ucode_change_version: %u\n",
191 le32_to_cpu(sdma_hdr->ucode_change_version));
192 DRM_DEBUG("jt_offset: %u\n", le32_to_cpu(sdma_hdr->jt_offset));
193 DRM_DEBUG("jt_size: %u\n", le32_to_cpu(sdma_hdr->jt_size));
194 if (version_minor >= 1) {
195 const struct sdma_firmware_header_v1_1 *sdma_v1_1_hdr =
196 container_of(sdma_hdr, struct sdma_firmware_header_v1_1, v1_0);
197 DRM_DEBUG("digest_size: %u\n", le32_to_cpu(sdma_v1_1_hdr->digest_size));
198 }
199 } else {
200 DRM_ERROR("Unknown SDMA ucode version: %u.%u\n",
201 version_major, version_minor);
202 }
203 }
204
205 int amdgpu_ucode_validate(const struct firmware *fw)
206 {
207 const struct common_firmware_header *hdr =
208 (const struct common_firmware_header *)fw->data;
209
210 if (fw->size == le32_to_cpu(hdr->size_bytes))
211 return 0;
212
213 return -EINVAL;
214 }
215
216 bool amdgpu_ucode_hdr_version(union amdgpu_firmware_header *hdr,
217 uint16_t hdr_major, uint16_t hdr_minor)
218 {
219 if ((hdr->common.header_version_major == hdr_major) &&
220 (hdr->common.header_version_minor == hdr_minor))
221 return false;
222 return true;
223 }
224
225 static int amdgpu_ucode_init_single_fw(struct amdgpu_firmware_info *ucode,
226 uint64_t mc_addr, void *kptr)
227 {
228 const struct common_firmware_header *header = NULL;
229
230 if (NULL == ucode->fw)
231 return 0;
232
233 ucode->mc_addr = mc_addr;
234 ucode->kaddr = kptr;
235
236 header = (const struct common_firmware_header *)ucode->fw->data;
237 memcpy(ucode->kaddr, (void *)((uint8_t *)ucode->fw->data +
238 le32_to_cpu(header->ucode_array_offset_bytes)),
239 le32_to_cpu(header->ucode_size_bytes));
240
241 return 0;
242 }
243
244 int amdgpu_ucode_init_bo(struct amdgpu_device *adev)
245 {
246 struct amdgpu_bo **bo = &adev->firmware.fw_buf;
247 uint64_t fw_mc_addr;
248 void *fw_buf_ptr = NULL;
249 uint64_t fw_offset = 0;
250 int i, err;
251 struct amdgpu_firmware_info *ucode = NULL;
252 const struct common_firmware_header *header = NULL;
253
254 err = amdgpu_bo_create(adev, adev->firmware.fw_size, PAGE_SIZE, true,
255 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL, bo);
256 if (err) {
257 dev_err(adev->dev, "(%d) Firmware buffer allocate failed\n", err);
258 err = -ENOMEM;
259 goto failed;
260 }
261
262 err = amdgpu_bo_reserve(*bo, false);
263 if (err) {
264 amdgpu_bo_unref(bo);
265 dev_err(adev->dev, "(%d) Firmware buffer reserve failed\n", err);
266 goto failed;
267 }
268
269 err = amdgpu_bo_pin(*bo, AMDGPU_GEM_DOMAIN_GTT, &fw_mc_addr);
270 if (err) {
271 amdgpu_bo_unreserve(*bo);
272 amdgpu_bo_unref(bo);
273 dev_err(adev->dev, "(%d) Firmware buffer pin failed\n", err);
274 goto failed;
275 }
276
277 err = amdgpu_bo_kmap(*bo, &fw_buf_ptr);
278 if (err) {
279 dev_err(adev->dev, "(%d) Firmware buffer kmap failed\n", err);
280 amdgpu_bo_unpin(*bo);
281 amdgpu_bo_unreserve(*bo);
282 amdgpu_bo_unref(bo);
283 goto failed;
284 }
285
286 amdgpu_bo_unreserve(*bo);
287
288 fw_offset = 0;
289 for (i = 0; i < AMDGPU_UCODE_ID_MAXIMUM; i++) {
290 ucode = &adev->firmware.ucode[i];
291 if (ucode->fw) {
292 header = (const struct common_firmware_header *)ucode->fw->data;
293 amdgpu_ucode_init_single_fw(ucode, fw_mc_addr + fw_offset,
294 fw_buf_ptr + fw_offset);
295 fw_offset += ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
296 }
297 }
298
299 failed:
300 if (err)
301 adev->firmware.smu_load = false;
302
303 return err;
304 }
305
306 int amdgpu_ucode_fini_bo(struct amdgpu_device *adev)
307 {
308 int i;
309 struct amdgpu_firmware_info *ucode = NULL;
310
311 for (i = 0; i < AMDGPU_UCODE_ID_MAXIMUM; i++) {
312 ucode = &adev->firmware.ucode[i];
313 if (ucode->fw) {
314 ucode->mc_addr = 0;
315 ucode->kaddr = NULL;
316 }
317 }
318 amdgpu_bo_unref(&adev->firmware.fw_buf);
319 adev->firmware.fw_buf = NULL;
320
321 return 0;
322 }
323