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];
106 struct list_head list;
109 struct skl_ext_manifest_hdr {
117 int snd_skl_get_module_info(struct skl_sst *ctx,
118 struct skl_module_cfg *mconfig)
120 struct uuid_module *module;
123 uuid_mod = (uuid_le *)mconfig->guid;
125 list_for_each_entry(module, &ctx->uuid_list, list) {
126 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
127 mconfig->id.module_id = module->id;
128 mconfig->is_loadable = module->is_loadable;
136 EXPORT_SYMBOL_GPL(snd_skl_get_module_info);
138 static inline int skl_getid_32(struct uuid_module *module, u64 *val,
139 int word1_mask, int word2_mask)
141 int index, max_inst, pvt_id;
144 max_inst = module->max_instance;
145 mask_val = (u32)(*val >> word1_mask);
147 if (mask_val != 0xffffffff) {
148 index = ffz(mask_val);
149 pvt_id = index + word1_mask + word2_mask;
150 if (pvt_id <= (max_inst - 1)) {
151 *val |= 1 << (index + word1_mask);
159 static inline int skl_pvtid_128(struct uuid_module *module)
161 int j, i, word1_mask, word2_mask = 0, pvt_id;
163 for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
166 for (i = 0; i < 2; i++) {
167 pvt_id = skl_getid_32(module, &module->pvt_id[j],
168 word1_mask, word2_mask);
173 if ((word1_mask + word2_mask) >= module->max_instance)
178 if (word2_mask >= module->max_instance)
186 * skl_get_pvt_id: generate a private id for use as module id
188 * @ctx: driver context
189 * @mconfig: module configuration data
191 * This generates a 128 bit private unique id for a module TYPE so that
192 * module instance is unique
194 int skl_get_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
196 struct uuid_module *module;
200 uuid_mod = (uuid_le *)mconfig->guid;
202 list_for_each_entry(module, &ctx->uuid_list, list) {
203 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
205 pvt_id = skl_pvtid_128(module);
213 EXPORT_SYMBOL_GPL(skl_get_pvt_id);
216 * skl_put_pvt_id: free up the private id allocated
218 * @ctx: driver context
219 * @mconfig: module configuration data
221 * This frees a 128 bit private unique id previously generated
223 int skl_put_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
227 struct uuid_module *module;
229 uuid_mod = (uuid_le *)mconfig->guid;
230 list_for_each_entry(module, &ctx->uuid_list, list) {
231 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
233 if (mconfig->id.pvt_id != 0)
234 i = (mconfig->id.pvt_id) / 64;
238 module->pvt_id[i] &= ~(1 << (mconfig->id.pvt_id));
239 mconfig->id.pvt_id = -1;
246 EXPORT_SYMBOL_GPL(skl_put_pvt_id);
249 * Parse the firmware binary to get the UUID, module id
252 int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
253 unsigned int offset, int index)
255 struct adsp_fw_hdr *adsp_hdr;
256 struct adsp_module_entry *mod_entry;
260 struct skl_sst *skl = ctx->thread_context;
261 struct uuid_module *module;
262 struct firmware stripped_fw;
263 unsigned int safe_file;
265 /* Get the FW pointer to derive ADSP header */
266 stripped_fw.data = fw->data;
267 stripped_fw.size = fw->size;
269 skl_dsp_strip_extended_manifest(&stripped_fw);
271 buf = stripped_fw.data;
273 /* check if we have enough space in file to move to header */
274 safe_file = sizeof(*adsp_hdr) + offset;
275 if (stripped_fw.size <= safe_file) {
276 dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
280 adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
282 /* check 1st module entry is in file */
283 safe_file += adsp_hdr->len + sizeof(*mod_entry);
284 if (stripped_fw.size <= safe_file) {
285 dev_err(ctx->dev, "Small fw file size, No module entry\n");
289 mod_entry = (struct adsp_module_entry *)
290 (buf + offset + adsp_hdr->len);
292 num_entry = adsp_hdr->num_modules;
294 /* check all entries are in file */
295 safe_file += num_entry * sizeof(*mod_entry);
296 if (stripped_fw.size <= safe_file) {
297 dev_err(ctx->dev, "Small fw file size, No modules\n");
303 * Read the UUID(GUID) from FW Manifest.
305 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
306 * Populate the UUID table to store module_id and loadable flags
310 for (i = 0; i < num_entry; i++, mod_entry++) {
311 module = kzalloc(sizeof(*module), GFP_KERNEL);
315 uuid_bin = (uuid_le *)mod_entry->uuid.id;
316 memcpy(&module->uuid, uuid_bin, sizeof(module->uuid));
318 module->id = (i | (index << 12));
319 module->is_loadable = mod_entry->type.load_type;
320 module->max_instance = mod_entry->instance_max_count;
322 list_add_tail(&module->list, &skl->uuid_list);
325 "Adding uuid :%pUL mod id: %d Loadable: %d\n",
326 &module->uuid, module->id, module->is_loadable);
332 void skl_freeup_uuid_list(struct skl_sst *ctx)
334 struct uuid_module *uuid, *_uuid;
336 list_for_each_entry_safe(uuid, _uuid, &ctx->uuid_list, list) {
337 list_del(&uuid->list);
343 * some firmware binary contains some extended manifest. This needs
344 * to be stripped in that case before we load and use that image.
346 * Get the module id for the module by checking
347 * the table for the UUID for the module
349 int skl_dsp_strip_extended_manifest(struct firmware *fw)
351 struct skl_ext_manifest_hdr *hdr;
353 /* check if fw file is greater than header we are looking */
354 if (fw->size < sizeof(hdr)) {
355 pr_err("%s: Firmware file small, no hdr\n", __func__);
359 hdr = (struct skl_ext_manifest_hdr *)fw->data;
361 if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
362 fw->size -= hdr->len;
363 fw->data += hdr->len;