Merge tag 'input-for-v6.3-rc0' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor...
[platform/kernel/linux-rpi.git] / sound / soc / sof / ipc4-loader.c
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // This file is provided under a dual BSD/GPLv2 license.  When using or
4 // redistributing this file, you may do so under either license.
5 //
6 // Copyright(c) 2022 Intel Corporation. All rights reserved.
7
8 #include <linux/firmware.h>
9 #include <sound/sof/ext_manifest4.h>
10 #include <sound/sof/ipc4/header.h>
11 #include <trace/events/sof.h>
12 #include "ipc4-priv.h"
13 #include "sof-audio.h"
14 #include "sof-priv.h"
15 #include "ops.h"
16
17 /* The module ID includes the id of the library it is part of at offset 12 */
18 #define SOF_IPC4_MOD_LIB_ID_SHIFT       12
19
20 static ssize_t sof_ipc4_fw_parse_ext_man(struct snd_sof_dev *sdev,
21                                          struct sof_ipc4_fw_library *fw_lib)
22 {
23         struct sof_ipc4_fw_data *ipc4_data = sdev->private;
24         const struct firmware *fw = fw_lib->sof_fw.fw;
25         struct sof_man4_fw_binary_header *fw_header;
26         struct sof_ext_manifest4_hdr *ext_man_hdr;
27         struct sof_man4_module_config *fm_config;
28         struct sof_ipc4_fw_module *fw_module;
29         struct sof_man4_module *fm_entry;
30         ssize_t remaining;
31         u32 fw_hdr_offset;
32         int i;
33
34         if (!ipc4_data) {
35                 dev_err(sdev->dev, "%s: ipc4_data is not available\n", __func__);
36                 return -EINVAL;
37         }
38
39         remaining = fw->size;
40         if (remaining <= sizeof(*ext_man_hdr)) {
41                 dev_err(sdev->dev, "Firmware size is too small: %zu\n", remaining);
42                 return -EINVAL;
43         }
44
45         ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
46
47         /*
48          * At the start of the firmware image we must have an extended manifest.
49          * Verify that the magic number is correct.
50          */
51         if (ext_man_hdr->id != SOF_EXT_MAN4_MAGIC_NUMBER) {
52                 dev_err(sdev->dev,
53                         "Unexpected extended manifest magic number: %#x\n",
54                         ext_man_hdr->id);
55                 return -EINVAL;
56         }
57
58         fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
59         if (!fw_hdr_offset)
60                 return -EINVAL;
61
62         if (remaining <= ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)) {
63                 dev_err(sdev->dev, "Invalid firmware size %zu, should be at least %zu\n",
64                         remaining, ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header));
65                 return -EINVAL;
66         }
67
68         fw_header = (struct sof_man4_fw_binary_header *)
69                                 (fw->data + ext_man_hdr->len + fw_hdr_offset);
70         remaining -= (ext_man_hdr->len + fw_hdr_offset);
71
72         if (remaining <= fw_header->len) {
73                 dev_err(sdev->dev, "Invalid fw_header->len %u\n", fw_header->len);
74                 return -EINVAL;
75         }
76
77         dev_info(sdev->dev, "Loaded firmware library: %s, version: %u.%u.%u.%u\n",
78                  fw_header->name, fw_header->major_version, fw_header->minor_version,
79                  fw_header->hotfix_version, fw_header->build_version);
80         dev_dbg(sdev->dev, "Header length: %u, module count: %u\n",
81                 fw_header->len, fw_header->num_module_entries);
82
83         fw_lib->modules = devm_kmalloc_array(sdev->dev, fw_header->num_module_entries,
84                                              sizeof(*fw_module), GFP_KERNEL);
85         if (!fw_lib->modules)
86                 return -ENOMEM;
87
88         fw_lib->name = fw_header->name;
89         fw_lib->num_modules = fw_header->num_module_entries;
90         fw_module = fw_lib->modules;
91
92         fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len);
93         remaining -= fw_header->len;
94
95         if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) {
96                 dev_err(sdev->dev, "Invalid num_module_entries %u\n",
97                         fw_header->num_module_entries);
98                 return -EINVAL;
99         }
100
101         fm_config = (struct sof_man4_module_config *)
102                                 (fm_entry + fw_header->num_module_entries);
103         remaining -= (fw_header->num_module_entries * sizeof(*fm_entry));
104         for (i = 0; i < fw_header->num_module_entries; i++) {
105                 memcpy(&fw_module->man4_module_entry, fm_entry, sizeof(*fm_entry));
106
107                 if (fm_entry->cfg_count) {
108                         if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) *
109                             sizeof(*fm_config)) {
110                                 dev_err(sdev->dev, "Invalid module cfg_offset %u\n",
111                                         fm_entry->cfg_offset);
112                                 return -EINVAL;
113                         }
114
115                         /* a module's config is always the same size */
116                         fw_module->bss_size = fm_config[fm_entry->cfg_offset].is_bytes;
117
118                         dev_dbg(sdev->dev,
119                                 "module %s: UUID %pUL cfg_count: %u, bss_size: %#x\n",
120                                 fm_entry->name, &fm_entry->uuid, fm_entry->cfg_count,
121                                 fw_module->bss_size);
122                 } else {
123                         fw_module->bss_size = 0;
124
125                         dev_dbg(sdev->dev, "module %s: UUID %pUL\n", fm_entry->name,
126                                 &fm_entry->uuid);
127                 }
128
129                 fw_module->man4_module_entry.id = i;
130                 ida_init(&fw_module->m_ida);
131                 fw_module->private = NULL;
132
133                 fw_module++;
134                 fm_entry++;
135         }
136
137         return ext_man_hdr->len;
138 }
139
140 static size_t sof_ipc4_fw_parse_basefw_ext_man(struct snd_sof_dev *sdev)
141 {
142         struct sof_ipc4_fw_data *ipc4_data = sdev->private;
143         struct sof_ipc4_fw_library *fw_lib;
144         ssize_t payload_offset;
145         int ret;
146
147         fw_lib = devm_kzalloc(sdev->dev, sizeof(*fw_lib), GFP_KERNEL);
148         if (!fw_lib)
149                 return -ENOMEM;
150
151         fw_lib->sof_fw.fw = sdev->basefw.fw;
152
153         payload_offset = sof_ipc4_fw_parse_ext_man(sdev, fw_lib);
154         if (payload_offset > 0) {
155                 fw_lib->sof_fw.payload_offset = payload_offset;
156
157                 /* basefw ID is 0 */
158                 fw_lib->id = 0;
159                 ret = xa_insert(&ipc4_data->fw_lib_xa, 0, fw_lib, GFP_KERNEL);
160                 if (ret)
161                         return ret;
162         }
163
164         return payload_offset;
165 }
166
167 static int sof_ipc4_load_library_by_uuid(struct snd_sof_dev *sdev,
168                                          unsigned long lib_id, const guid_t *uuid)
169 {
170         struct sof_ipc4_fw_data *ipc4_data = sdev->private;
171         struct sof_ipc4_fw_library *fw_lib;
172         const char *fw_filename;
173         ssize_t payload_offset;
174         int ret, i, err;
175
176         if (!sdev->pdata->fw_lib_prefix) {
177                 dev_err(sdev->dev,
178                         "Library loading is not supported due to not set library path\n");
179                 return -EINVAL;
180         }
181
182         if (!ipc4_data->load_library) {
183                 dev_err(sdev->dev, "Library loading is not supported on this platform\n");
184                 return -EOPNOTSUPP;
185         }
186
187         fw_lib = devm_kzalloc(sdev->dev, sizeof(*fw_lib), GFP_KERNEL);
188         if (!fw_lib)
189                 return -ENOMEM;
190
191         fw_filename = kasprintf(GFP_KERNEL, "%s/%pUL.bin",
192                                 sdev->pdata->fw_lib_prefix, uuid);
193         if (!fw_filename) {
194                 ret = -ENOMEM;
195                 goto free_fw_lib;
196         }
197
198         ret = request_firmware(&fw_lib->sof_fw.fw, fw_filename, sdev->dev);
199         if (ret < 0) {
200                 dev_err(sdev->dev, "Library file '%s' is missing\n", fw_filename);
201                 goto free_filename;
202         } else {
203                 dev_dbg(sdev->dev, "Library file '%s' loaded\n", fw_filename);
204         }
205
206         payload_offset = sof_ipc4_fw_parse_ext_man(sdev, fw_lib);
207         if (payload_offset <= 0) {
208                 if (!payload_offset)
209                         ret = -EINVAL;
210                 else
211                         ret = payload_offset;
212
213                 goto release;
214         }
215
216         fw_lib->sof_fw.payload_offset = payload_offset;
217         fw_lib->id = lib_id;
218
219         /* Fix up the module ID numbers within the library */
220         for (i = 0; i < fw_lib->num_modules; i++)
221                 fw_lib->modules[i].man4_module_entry.id |= (lib_id << SOF_IPC4_MOD_LIB_ID_SHIFT);
222
223         /*
224          * Make sure that the DSP is booted and stays up while attempting the
225          * loading the library for the first time
226          */
227         ret = pm_runtime_resume_and_get(sdev->dev);
228         if (ret < 0 && ret != -EACCES) {
229                 dev_err_ratelimited(sdev->dev, "%s: pm_runtime resume failed: %d\n",
230                                     __func__, ret);
231                 goto release;
232         }
233
234         ret = ipc4_data->load_library(sdev, fw_lib, false);
235
236         pm_runtime_mark_last_busy(sdev->dev);
237         err = pm_runtime_put_autosuspend(sdev->dev);
238         if (err < 0)
239                 dev_err_ratelimited(sdev->dev, "%s: pm_runtime idle failed: %d\n",
240                                     __func__, err);
241
242         if (ret)
243                 goto release;
244
245         ret = xa_insert(&ipc4_data->fw_lib_xa, lib_id, fw_lib, GFP_KERNEL);
246         if (unlikely(ret))
247                 goto release;
248
249         kfree(fw_filename);
250
251         return 0;
252
253 release:
254         release_firmware(fw_lib->sof_fw.fw);
255         /* Allocated within sof_ipc4_fw_parse_ext_man() */
256         devm_kfree(sdev->dev, fw_lib->modules);
257 free_filename:
258         kfree(fw_filename);
259 free_fw_lib:
260         devm_kfree(sdev->dev, fw_lib);
261
262         return ret;
263 }
264
265 struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev,
266                                                         const guid_t *uuid)
267 {
268         struct sof_ipc4_fw_data *ipc4_data = sdev->private;
269         struct sof_ipc4_fw_library *fw_lib;
270         unsigned long lib_id;
271         int i, ret;
272
273         if (guid_is_null(uuid))
274                 return NULL;
275
276         xa_for_each(&ipc4_data->fw_lib_xa, lib_id, fw_lib) {
277                 for (i = 0; i < fw_lib->num_modules; i++) {
278                         if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid))
279                                 return &fw_lib->modules[i];
280                 }
281         }
282
283         /*
284          * Do not attempt to load external library in case the maximum number of
285          * firmware libraries have been already loaded
286          */
287         if ((lib_id + 1) == ipc4_data->max_libs_count) {
288                 dev_err(sdev->dev,
289                         "%s: Maximum allowed number of libraries reached (%u)\n",
290                         __func__, ipc4_data->max_libs_count);
291                 return NULL;
292         }
293
294         /* The module cannot be found, try to load it as a library */
295         ret = sof_ipc4_load_library_by_uuid(sdev, lib_id + 1, uuid);
296         if (ret)
297                 return NULL;
298
299         /* Look for the module in the newly loaded library, it should be available now */
300         xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, lib_id) {
301                 for (i = 0; i < fw_lib->num_modules; i++) {
302                         if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid))
303                                 return &fw_lib->modules[i];
304                 }
305         }
306
307         return NULL;
308 }
309
310 static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev)
311 {
312         struct sof_ipc4_fw_data *ipc4_data = sdev->private;
313         u32 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
314         struct sof_man4_fw_binary_header *fw_header;
315         const struct firmware *fw = sdev->basefw.fw;
316         struct sof_ext_manifest4_hdr *ext_man_hdr;
317
318         ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
319         fw_header = (struct sof_man4_fw_binary_header *)
320                                 (fw->data + ext_man_hdr->len + fw_hdr_offset);
321
322         /* TODO: Add firmware verification code here */
323
324         dev_dbg(sdev->dev, "Validated firmware version: %u.%u.%u.%u\n",
325                 fw_header->major_version, fw_header->minor_version,
326                 fw_header->hotfix_version, fw_header->build_version);
327
328         return 0;
329 }
330
331 int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev)
332 {
333         struct sof_ipc4_fw_data *ipc4_data = sdev->private;
334         const struct sof_ipc_ops *iops = sdev->ipc->ops;
335         struct sof_ipc4_fw_version *fw_ver;
336         struct sof_ipc4_tuple *tuple;
337         struct sof_ipc4_msg msg;
338         size_t offset = 0;
339         int ret;
340
341         /* Get the firmware configuration */
342         msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
343         msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
344         msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
345         msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
346         msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_FW_CONFIG);
347
348         msg.data_size = sdev->ipc->max_payload_size;
349         msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL);
350         if (!msg.data_ptr)
351                 return -ENOMEM;
352
353         ret = iops->set_get_data(sdev, &msg, msg.data_size, false);
354         if (ret)
355                 goto out;
356
357         while (offset < msg.data_size) {
358                 tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset);
359
360                 switch (tuple->type) {
361                 case SOF_IPC4_FW_CFG_FW_VERSION:
362                         fw_ver = (struct sof_ipc4_fw_version *)tuple->value;
363
364                         dev_info(sdev->dev,
365                                  "Booted firmware version: %u.%u.%u.%u\n",
366                                  fw_ver->major, fw_ver->minor, fw_ver->hotfix,
367                                  fw_ver->build);
368                         break;
369                 case SOF_IPC4_FW_CFG_DL_MAILBOX_BYTES:
370                         trace_sof_ipc4_fw_config(sdev, "DL mailbox size", *tuple->value);
371                         break;
372                 case SOF_IPC4_FW_CFG_UL_MAILBOX_BYTES:
373                         trace_sof_ipc4_fw_config(sdev, "UL mailbox size", *tuple->value);
374                         break;
375                 case SOF_IPC4_FW_CFG_TRACE_LOG_BYTES:
376                         trace_sof_ipc4_fw_config(sdev, "Trace log size", *tuple->value);
377                         ipc4_data->mtrace_log_bytes = *tuple->value;
378                         break;
379                 case SOF_IPC4_FW_CFG_MAX_LIBS_COUNT:
380                         trace_sof_ipc4_fw_config(sdev, "maximum number of libraries",
381                                                  *tuple->value);
382                         ipc4_data->max_libs_count = *tuple->value;
383                         if (!ipc4_data->max_libs_count)
384                                 ipc4_data->max_libs_count = 1;
385                         break;
386                 case SOF_IPC4_FW_CFG_MAX_PPL_COUNT:
387                         ipc4_data->max_num_pipelines = *tuple->value;
388                         trace_sof_ipc4_fw_config(sdev, "Max PPL count %d",
389                                                  ipc4_data->max_num_pipelines);
390                         if (ipc4_data->max_num_pipelines <= 0) {
391                                 dev_err(sdev->dev, "Invalid max_num_pipelines %d",
392                                         ipc4_data->max_num_pipelines);
393                                 ret = -EINVAL;
394                                 goto out;
395                         }
396                         break;
397                 default:
398                         break;
399                 }
400
401                 offset += sizeof(*tuple) + tuple->size;
402         }
403
404 out:
405         kfree(msg.data_ptr);
406
407         return ret;
408 }
409
410 int sof_ipc4_reload_fw_libraries(struct snd_sof_dev *sdev)
411 {
412         struct sof_ipc4_fw_data *ipc4_data = sdev->private;
413         struct sof_ipc4_fw_library *fw_lib;
414         unsigned long lib_id;
415         int ret = 0;
416
417         xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, 1) {
418                 ret = ipc4_data->load_library(sdev, fw_lib, true);
419                 if (ret) {
420                         dev_err(sdev->dev, "%s: Failed to reload library: %s, %d\n",
421                                 __func__, fw_lib->name, ret);
422                         break;
423                 }
424         }
425
426         return ret;
427 }
428
429 const struct sof_ipc_fw_loader_ops ipc4_loader_ops = {
430         .validate = sof_ipc4_validate_firmware,
431         .parse_ext_manifest = sof_ipc4_fw_parse_basefw_ext_man,
432 };