1 // SPDX-License-Identifier: GPL-2.0-only
3 * skl-sst-utils.c - SKL sst utils functions
5 * Copyright (C) 2016 Intel Corp
8 #include <linux/device.h>
9 #include <linux/slab.h>
10 #include <linux/uuid.h>
11 #include "../common/sst-dsp.h"
12 #include "../common/sst-dsp-priv.h"
15 #define DEFAULT_HASH_SHA256_LEN 32
17 /* FW Extended Manifest Header id = $AE1 */
18 #define SKL_EXT_MANIFEST_HEADER_MAGIC 0x31454124
37 union seg_flags flags
;
50 struct adsp_module_entry
{
54 struct module_type type
;
55 u8 hash1
[DEFAULT_HASH_SHA256_LEN
];
60 u16 instance_max_count
;
61 u16 instance_bss_size
;
62 struct segment_desc segments
[3];
69 u32 preload_page_count
;
82 struct skl_ext_manifest_hdr
{
90 static int skl_get_pvtid_map(struct uuid_module
*module
, int instance_id
)
94 for (pvt_id
= 0; pvt_id
< module
->max_instance
; pvt_id
++) {
95 if (module
->instance_id
[pvt_id
] == instance_id
)
101 int skl_get_pvt_instance_id_map(struct skl_dev
*skl
,
102 int module_id
, int instance_id
)
104 struct uuid_module
*module
;
106 list_for_each_entry(module
, &skl
->uuid_list
, list
) {
107 if (module
->id
== module_id
)
108 return skl_get_pvtid_map(module
, instance_id
);
113 EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map
);
115 static inline int skl_getid_32(struct uuid_module
*module
, u64
*val
,
116 int word1_mask
, int word2_mask
)
118 int index
, max_inst
, pvt_id
;
121 max_inst
= module
->max_instance
;
122 mask_val
= (u32
)(*val
>> word1_mask
);
124 if (mask_val
!= 0xffffffff) {
125 index
= ffz(mask_val
);
126 pvt_id
= index
+ word1_mask
+ word2_mask
;
127 if (pvt_id
<= (max_inst
- 1)) {
128 *val
|= 1ULL << (index
+ word1_mask
);
136 static inline int skl_pvtid_128(struct uuid_module
*module
)
138 int j
, i
, word1_mask
, word2_mask
= 0, pvt_id
;
140 for (j
= 0; j
< MAX_INSTANCE_BUFF
; j
++) {
143 for (i
= 0; i
< 2; i
++) {
144 pvt_id
= skl_getid_32(module
, &module
->pvt_id
[j
],
145 word1_mask
, word2_mask
);
150 if ((word1_mask
+ word2_mask
) >= module
->max_instance
)
155 if (word2_mask
>= module
->max_instance
)
163 * skl_get_pvt_id: generate a private id for use as module id
165 * @skl: driver context
166 * @uuid_mod: module's uuid
167 * @instance_id: module's instance id
169 * This generates a 128 bit private unique id for a module TYPE so that
170 * module instance is unique
172 int skl_get_pvt_id(struct skl_dev
*skl
, guid_t
*uuid_mod
, int instance_id
)
174 struct uuid_module
*module
;
177 list_for_each_entry(module
, &skl
->uuid_list
, list
) {
178 if (guid_equal(uuid_mod
, &module
->uuid
)) {
180 pvt_id
= skl_pvtid_128(module
);
182 module
->instance_id
[pvt_id
] = instance_id
;
191 EXPORT_SYMBOL_GPL(skl_get_pvt_id
);
194 * skl_put_pvt_id: free up the private id allocated
196 * @skl: driver context
197 * @uuid_mod: module's uuid
198 * @pvt_id: module pvt id
200 * This frees a 128 bit private unique id previously generated
202 int skl_put_pvt_id(struct skl_dev
*skl
, guid_t
*uuid_mod
, int *pvt_id
)
205 struct uuid_module
*module
;
207 list_for_each_entry(module
, &skl
->uuid_list
, list
) {
208 if (guid_equal(uuid_mod
, &module
->uuid
)) {
215 module
->pvt_id
[i
] &= ~(1 << (*pvt_id
));
223 EXPORT_SYMBOL_GPL(skl_put_pvt_id
);
226 * Parse the firmware binary to get the UUID, module id
229 int snd_skl_parse_uuids(struct sst_dsp
*ctx
, const struct firmware
*fw
,
230 unsigned int offset
, int index
)
232 struct adsp_fw_hdr
*adsp_hdr
;
233 struct adsp_module_entry
*mod_entry
;
234 int i
, num_entry
, size
;
236 struct skl_dev
*skl
= ctx
->thread_context
;
237 struct uuid_module
*module
;
238 struct firmware stripped_fw
;
239 unsigned int safe_file
;
242 /* Get the FW pointer to derive ADSP header */
243 stripped_fw
.data
= fw
->data
;
244 stripped_fw
.size
= fw
->size
;
246 skl_dsp_strip_extended_manifest(&stripped_fw
);
248 buf
= stripped_fw
.data
;
250 /* check if we have enough space in file to move to header */
251 safe_file
= sizeof(*adsp_hdr
) + offset
;
252 if (stripped_fw
.size
<= safe_file
) {
253 dev_err(ctx
->dev
, "Small fw file size, No space for hdr\n");
257 adsp_hdr
= (struct adsp_fw_hdr
*)(buf
+ offset
);
259 /* check 1st module entry is in file */
260 safe_file
+= adsp_hdr
->len
+ sizeof(*mod_entry
);
261 if (stripped_fw
.size
<= safe_file
) {
262 dev_err(ctx
->dev
, "Small fw file size, No module entry\n");
266 mod_entry
= (struct adsp_module_entry
*)(buf
+ offset
+ adsp_hdr
->len
);
268 num_entry
= adsp_hdr
->num_modules
;
270 /* check all entries are in file */
271 safe_file
+= num_entry
* sizeof(*mod_entry
);
272 if (stripped_fw
.size
<= safe_file
) {
273 dev_err(ctx
->dev
, "Small fw file size, No modules\n");
279 * Read the UUID(GUID) from FW Manifest.
281 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
282 * Populate the UUID table to store module_id and loadable flags
286 for (i
= 0; i
< num_entry
; i
++, mod_entry
++) {
287 module
= kzalloc(sizeof(*module
), GFP_KERNEL
);
293 guid_copy(&module
->uuid
, (guid_t
*)&mod_entry
->uuid
);
295 module
->id
= (i
| (index
<< 12));
296 module
->is_loadable
= mod_entry
->type
.load_type
;
297 module
->max_instance
= mod_entry
->instance_max_count
;
298 size
= sizeof(int) * mod_entry
->instance_max_count
;
299 module
->instance_id
= devm_kzalloc(ctx
->dev
, size
, GFP_KERNEL
);
300 if (!module
->instance_id
) {
305 list_add_tail(&module
->list
, &skl
->uuid_list
);
308 "Adding uuid :%pUL mod id: %d Loadable: %d\n",
309 &module
->uuid
, module
->id
, module
->is_loadable
);
315 skl_freeup_uuid_list(skl
);
319 void skl_freeup_uuid_list(struct skl_dev
*skl
)
321 struct uuid_module
*uuid
, *_uuid
;
323 list_for_each_entry_safe(uuid
, _uuid
, &skl
->uuid_list
, list
) {
324 list_del(&uuid
->list
);
330 * some firmware binary contains some extended manifest. This needs
331 * to be stripped in that case before we load and use that image.
333 * Get the module id for the module by checking
334 * the table for the UUID for the module
336 int skl_dsp_strip_extended_manifest(struct firmware
*fw
)
338 struct skl_ext_manifest_hdr
*hdr
;
340 /* check if fw file is greater than header we are looking */
341 if (fw
->size
< sizeof(hdr
)) {
342 pr_err("%s: Firmware file small, no hdr\n", __func__
);
346 hdr
= (struct skl_ext_manifest_hdr
*)fw
->data
;
348 if (hdr
->id
== SKL_EXT_MANIFEST_HEADER_MAGIC
) {
349 fw
->size
-= hdr
->len
;
350 fw
->data
+= hdr
->len
;
356 int skl_sst_ctx_init(struct device
*dev
, int irq
, const char *fw_name
,
357 struct skl_dsp_loader_ops dsp_ops
, struct skl_dev
**dsp
,
358 struct sst_dsp_device
*skl_dev
)
360 struct skl_dev
*skl
= *dsp
;
364 skl_dev
->thread_context
= skl
;
365 INIT_LIST_HEAD(&skl
->uuid_list
);
366 skl
->dsp
= skl_dsp_ctx_init(dev
, skl_dev
, irq
);
368 dev_err(skl
->dev
, "%s: no device\n", __func__
);
373 sst
->fw_name
= fw_name
;
374 sst
->dsp_ops
= dsp_ops
;
375 init_waitqueue_head(&skl
->mod_load_wait
);
376 INIT_LIST_HEAD(&sst
->module_list
);
378 skl
->is_first_boot
= true;
383 int skl_prepare_lib_load(struct skl_dev
*skl
, struct skl_lib_info
*linfo
,
384 struct firmware
*stripped_fw
,
385 unsigned int hdr_offset
, int index
)
388 struct sst_dsp
*dsp
= skl
->dsp
;
390 if (linfo
->fw
== NULL
) {
391 ret
= request_firmware(&linfo
->fw
, linfo
->name
,
394 dev_err(skl
->dev
, "Request lib %s failed:%d\n",
400 if (skl
->is_first_boot
) {
401 ret
= snd_skl_parse_uuids(dsp
, linfo
->fw
, hdr_offset
, index
);
406 stripped_fw
->data
= linfo
->fw
->data
;
407 stripped_fw
->size
= linfo
->fw
->size
;
408 skl_dsp_strip_extended_manifest(stripped_fw
);
413 void skl_release_library(struct skl_lib_info
*linfo
, int lib_count
)
417 /* library indices start from 1 to N. 0 represents base FW */
418 for (i
= 1; i
< lib_count
; i
++) {
420 release_firmware(linfo
[i
].fw
);