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 #define MAX_INSTANCE_BUFF 2
104 u64 pvt_id[MAX_INSTANCE_BUFF];
107 struct list_head list;
110 struct skl_ext_manifest_hdr {
118 int snd_skl_get_module_info(struct skl_sst *ctx,
119 struct skl_module_cfg *mconfig)
121 struct uuid_module *module;
124 uuid_mod = (uuid_le *)mconfig->guid;
126 if (list_empty(&ctx->uuid_list)) {
127 dev_err(ctx->dev, "Module list is empty\n");
131 list_for_each_entry(module, &ctx->uuid_list, list) {
132 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
133 mconfig->id.module_id = module->id;
134 mconfig->is_loadable = module->is_loadable;
142 EXPORT_SYMBOL_GPL(snd_skl_get_module_info);
144 static int skl_get_pvtid_map(struct uuid_module *module, int instance_id)
148 for (pvt_id = 0; pvt_id < module->max_instance; pvt_id++) {
149 if (module->instance_id[pvt_id] == instance_id)
155 int skl_get_pvt_instance_id_map(struct skl_sst *ctx,
156 int module_id, int instance_id)
158 struct uuid_module *module;
160 list_for_each_entry(module, &ctx->uuid_list, list) {
161 if (module->id == module_id)
162 return skl_get_pvtid_map(module, instance_id);
167 EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map);
169 static inline int skl_getid_32(struct uuid_module *module, u64 *val,
170 int word1_mask, int word2_mask)
172 int index, max_inst, pvt_id;
175 max_inst = module->max_instance;
176 mask_val = (u32)(*val >> word1_mask);
178 if (mask_val != 0xffffffff) {
179 index = ffz(mask_val);
180 pvt_id = index + word1_mask + word2_mask;
181 if (pvt_id <= (max_inst - 1)) {
182 *val |= 1ULL << (index + word1_mask);
190 static inline int skl_pvtid_128(struct uuid_module *module)
192 int j, i, word1_mask, word2_mask = 0, pvt_id;
194 for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
197 for (i = 0; i < 2; i++) {
198 pvt_id = skl_getid_32(module, &module->pvt_id[j],
199 word1_mask, word2_mask);
204 if ((word1_mask + word2_mask) >= module->max_instance)
209 if (word2_mask >= module->max_instance)
217 * skl_get_pvt_id: generate a private id for use as module id
219 * @ctx: driver context
220 * @mconfig: module configuration data
222 * This generates a 128 bit private unique id for a module TYPE so that
223 * module instance is unique
225 int skl_get_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
227 struct uuid_module *module;
231 uuid_mod = (uuid_le *)mconfig->guid;
233 list_for_each_entry(module, &ctx->uuid_list, list) {
234 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
236 pvt_id = skl_pvtid_128(module);
238 module->instance_id[pvt_id] =
239 mconfig->id.instance_id;
247 EXPORT_SYMBOL_GPL(skl_get_pvt_id);
250 * skl_put_pvt_id: free up the private id allocated
252 * @ctx: driver context
253 * @mconfig: module configuration data
255 * This frees a 128 bit private unique id previously generated
257 int skl_put_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
261 struct uuid_module *module;
263 uuid_mod = (uuid_le *)mconfig->guid;
264 list_for_each_entry(module, &ctx->uuid_list, list) {
265 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
267 if (mconfig->id.pvt_id != 0)
268 i = (mconfig->id.pvt_id) / 64;
272 module->pvt_id[i] &= ~(1 << (mconfig->id.pvt_id));
273 mconfig->id.pvt_id = -1;
280 EXPORT_SYMBOL_GPL(skl_put_pvt_id);
283 * Parse the firmware binary to get the UUID, module id
286 int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
287 unsigned int offset, int index)
289 struct adsp_fw_hdr *adsp_hdr;
290 struct adsp_module_entry *mod_entry;
291 int i, num_entry, size;
294 struct skl_sst *skl = ctx->thread_context;
295 struct uuid_module *module;
296 struct firmware stripped_fw;
297 unsigned int safe_file;
300 /* Get the FW pointer to derive ADSP header */
301 stripped_fw.data = fw->data;
302 stripped_fw.size = fw->size;
304 skl_dsp_strip_extended_manifest(&stripped_fw);
306 buf = stripped_fw.data;
308 /* check if we have enough space in file to move to header */
309 safe_file = sizeof(*adsp_hdr) + offset;
310 if (stripped_fw.size <= safe_file) {
311 dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
315 adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
317 /* check 1st module entry is in file */
318 safe_file += adsp_hdr->len + sizeof(*mod_entry);
319 if (stripped_fw.size <= safe_file) {
320 dev_err(ctx->dev, "Small fw file size, No module entry\n");
324 mod_entry = (struct adsp_module_entry *)
325 (buf + offset + adsp_hdr->len);
327 num_entry = adsp_hdr->num_modules;
329 /* check all entries are in file */
330 safe_file += num_entry * sizeof(*mod_entry);
331 if (stripped_fw.size <= safe_file) {
332 dev_err(ctx->dev, "Small fw file size, No modules\n");
338 * Read the UUID(GUID) from FW Manifest.
340 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
341 * Populate the UUID table to store module_id and loadable flags
345 for (i = 0; i < num_entry; i++, mod_entry++) {
346 module = kzalloc(sizeof(*module), GFP_KERNEL);
352 uuid_bin = (uuid_le *)mod_entry->uuid.id;
353 memcpy(&module->uuid, uuid_bin, sizeof(module->uuid));
355 module->id = (i | (index << 12));
356 module->is_loadable = mod_entry->type.load_type;
357 module->max_instance = mod_entry->instance_max_count;
358 size = sizeof(int) * mod_entry->instance_max_count;
359 module->instance_id = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
360 if (!module->instance_id) {
365 list_add_tail(&module->list, &skl->uuid_list);
368 "Adding uuid :%pUL mod id: %d Loadable: %d\n",
369 &module->uuid, module->id, module->is_loadable);
375 skl_freeup_uuid_list(skl);
379 void skl_freeup_uuid_list(struct skl_sst *ctx)
381 struct uuid_module *uuid, *_uuid;
383 list_for_each_entry_safe(uuid, _uuid, &ctx->uuid_list, list) {
384 list_del(&uuid->list);
390 * some firmware binary contains some extended manifest. This needs
391 * to be stripped in that case before we load and use that image.
393 * Get the module id for the module by checking
394 * the table for the UUID for the module
396 int skl_dsp_strip_extended_manifest(struct firmware *fw)
398 struct skl_ext_manifest_hdr *hdr;
400 /* check if fw file is greater than header we are looking */
401 if (fw->size < sizeof(hdr)) {
402 pr_err("%s: Firmware file small, no hdr\n", __func__);
406 hdr = (struct skl_ext_manifest_hdr *)fw->data;
408 if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
409 fw->size -= hdr->len;
410 fw->data += hdr->len;