2 * skl-sst-utils.c - SKL sst utils functions
4 * Copyright (C) 2016 Intel Corp
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/uuid.h>
19 #include "skl-sst-dsp.h"
20 #include "../common/sst-dsp.h"
21 #include "../common/sst-dsp-priv.h"
22 #include "skl-sst-ipc.h"
25 #define UUID_STR_SIZE 37
26 #define DEFAULT_HASH_SHA256_LEN 32
28 /* FW Extended Manifest Header id = $AE1 */
29 #define SKL_EXT_MANIFEST_HEADER_MAGIC 0x31454124
52 union seg_flags flags;
65 struct adsp_module_entry {
69 struct module_type type;
70 u8 hash1[DEFAULT_HASH_SHA256_LEN];
75 u16 instance_max_count;
76 u16 instance_bss_size;
77 struct segment_desc segments[3];
84 u32 preload_page_count;
97 struct skl_ext_manifest_hdr {
105 static int skl_get_pvtid_map(struct uuid_module *module, int instance_id)
109 for (pvt_id = 0; pvt_id < module->max_instance; pvt_id++) {
110 if (module->instance_id[pvt_id] == instance_id)
116 int skl_get_pvt_instance_id_map(struct skl_sst *ctx,
117 int module_id, int instance_id)
119 struct uuid_module *module;
121 list_for_each_entry(module, &ctx->uuid_list, list) {
122 if (module->id == module_id)
123 return skl_get_pvtid_map(module, instance_id);
128 EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map);
130 static inline int skl_getid_32(struct uuid_module *module, u64 *val,
131 int word1_mask, int word2_mask)
133 int index, max_inst, pvt_id;
136 max_inst = module->max_instance;
137 mask_val = (u32)(*val >> word1_mask);
139 if (mask_val != 0xffffffff) {
140 index = ffz(mask_val);
141 pvt_id = index + word1_mask + word2_mask;
142 if (pvt_id <= (max_inst - 1)) {
143 *val |= 1ULL << (index + word1_mask);
151 static inline int skl_pvtid_128(struct uuid_module *module)
153 int j, i, word1_mask, word2_mask = 0, pvt_id;
155 for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
158 for (i = 0; i < 2; i++) {
159 pvt_id = skl_getid_32(module, &module->pvt_id[j],
160 word1_mask, word2_mask);
165 if ((word1_mask + word2_mask) >= module->max_instance)
170 if (word2_mask >= module->max_instance)
178 * skl_get_pvt_id: generate a private id for use as module id
180 * @ctx: driver context
181 * @uuid_mod: module's uuid
182 * @instance_id: module's instance id
184 * This generates a 128 bit private unique id for a module TYPE so that
185 * module instance is unique
187 int skl_get_pvt_id(struct skl_sst *ctx, uuid_le *uuid_mod, int instance_id)
189 struct uuid_module *module;
192 list_for_each_entry(module, &ctx->uuid_list, list) {
193 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
195 pvt_id = skl_pvtid_128(module);
197 module->instance_id[pvt_id] = instance_id;
206 EXPORT_SYMBOL_GPL(skl_get_pvt_id);
209 * skl_put_pvt_id: free up the private id allocated
211 * @ctx: driver context
212 * @uuid_mod: module's uuid
213 * @pvt_id: module pvt id
215 * This frees a 128 bit private unique id previously generated
217 int skl_put_pvt_id(struct skl_sst *ctx, uuid_le *uuid_mod, int *pvt_id)
220 struct uuid_module *module;
222 list_for_each_entry(module, &ctx->uuid_list, list) {
223 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
230 module->pvt_id[i] &= ~(1 << (*pvt_id));
238 EXPORT_SYMBOL_GPL(skl_put_pvt_id);
241 * Parse the firmware binary to get the UUID, module id
244 int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
245 unsigned int offset, int index)
247 struct adsp_fw_hdr *adsp_hdr;
248 struct adsp_module_entry *mod_entry;
249 int i, num_entry, size;
252 struct skl_sst *skl = ctx->thread_context;
253 struct uuid_module *module;
254 struct firmware stripped_fw;
255 unsigned int safe_file;
258 /* Get the FW pointer to derive ADSP header */
259 stripped_fw.data = fw->data;
260 stripped_fw.size = fw->size;
262 skl_dsp_strip_extended_manifest(&stripped_fw);
264 buf = stripped_fw.data;
266 /* check if we have enough space in file to move to header */
267 safe_file = sizeof(*adsp_hdr) + offset;
268 if (stripped_fw.size <= safe_file) {
269 dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
273 adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
275 /* check 1st module entry is in file */
276 safe_file += adsp_hdr->len + sizeof(*mod_entry);
277 if (stripped_fw.size <= safe_file) {
278 dev_err(ctx->dev, "Small fw file size, No module entry\n");
282 mod_entry = (struct adsp_module_entry *)
283 (buf + offset + adsp_hdr->len);
285 num_entry = adsp_hdr->num_modules;
287 /* check all entries are in file */
288 safe_file += num_entry * sizeof(*mod_entry);
289 if (stripped_fw.size <= safe_file) {
290 dev_err(ctx->dev, "Small fw file size, No modules\n");
296 * Read the UUID(GUID) from FW Manifest.
298 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
299 * Populate the UUID table to store module_id and loadable flags
303 for (i = 0; i < num_entry; i++, mod_entry++) {
304 module = kzalloc(sizeof(*module), GFP_KERNEL);
310 uuid_bin = (uuid_le *)mod_entry->uuid.id;
311 memcpy(&module->uuid, uuid_bin, sizeof(module->uuid));
313 module->id = (i | (index << 12));
314 module->is_loadable = mod_entry->type.load_type;
315 module->max_instance = mod_entry->instance_max_count;
316 size = sizeof(int) * mod_entry->instance_max_count;
317 module->instance_id = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
318 if (!module->instance_id) {
323 list_add_tail(&module->list, &skl->uuid_list);
326 "Adding uuid :%pUL mod id: %d Loadable: %d\n",
327 &module->uuid, module->id, module->is_loadable);
333 skl_freeup_uuid_list(skl);
337 void skl_freeup_uuid_list(struct skl_sst *ctx)
339 struct uuid_module *uuid, *_uuid;
341 list_for_each_entry_safe(uuid, _uuid, &ctx->uuid_list, list) {
342 list_del(&uuid->list);
348 * some firmware binary contains some extended manifest. This needs
349 * to be stripped in that case before we load and use that image.
351 * Get the module id for the module by checking
352 * the table for the UUID for the module
354 int skl_dsp_strip_extended_manifest(struct firmware *fw)
356 struct skl_ext_manifest_hdr *hdr;
358 /* check if fw file is greater than header we are looking */
359 if (fw->size < sizeof(hdr)) {
360 pr_err("%s: Firmware file small, no hdr\n", __func__);
364 hdr = (struct skl_ext_manifest_hdr *)fw->data;
366 if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
367 fw->size -= hdr->len;
368 fw->data += hdr->len;
374 int skl_sst_ctx_init(struct device *dev, int irq, const char *fw_name,
375 struct skl_dsp_loader_ops dsp_ops, struct skl_sst **dsp,
376 struct sst_dsp_device *skl_dev)
381 skl = devm_kzalloc(dev, sizeof(*skl), GFP_KERNEL);
386 skl_dev->thread_context = skl;
387 INIT_LIST_HEAD(&skl->uuid_list);
388 skl->dsp = skl_dsp_ctx_init(dev, skl_dev, irq);
390 dev_err(skl->dev, "%s: no device\n", __func__);
395 sst->fw_name = fw_name;
396 sst->dsp_ops = dsp_ops;
397 init_waitqueue_head(&skl->mod_load_wait);
398 INIT_LIST_HEAD(&sst->module_list);
400 skl->is_first_boot = true;
407 int skl_prepare_lib_load(struct skl_sst *skl, struct skl_lib_info *linfo,
408 struct firmware *stripped_fw,
409 unsigned int hdr_offset, int index)
412 struct sst_dsp *dsp = skl->dsp;
414 if (linfo->fw == NULL) {
415 ret = reject_firmware(&linfo->fw, linfo->name,
418 dev_err(skl->dev, "Request lib %s failed:%d\n",
424 if (skl->is_first_boot) {
425 ret = snd_skl_parse_uuids(dsp, linfo->fw, hdr_offset, index);
430 stripped_fw->data = linfo->fw->data;
431 stripped_fw->size = linfo->fw->size;
432 skl_dsp_strip_extended_manifest(stripped_fw);
437 void skl_release_library(struct skl_lib_info *linfo, int lib_count)
441 /* library indices start from 1 to N. 0 represents base FW */
442 for (i = 1; i < lib_count; i++) {
444 release_firmware(linfo[i].fw);