Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  skl-sst-utils.c - SKL sst utils functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright (C) 2016 Intel Corp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "../common/sst-dsp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "../common/sst-dsp-priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "skl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define DEFAULT_HASH_SHA256_LEN 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) /* FW Extended Manifest Header id = $AE1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define SKL_EXT_MANIFEST_HEADER_MAGIC   0x31454124
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) union seg_flags {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	u32 ul;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		u32 contents : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		u32 alloc    : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		u32 load     : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		u32 read_only : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		u32 code     : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		u32 data     : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		u32 _rsvd0   : 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		u32 type     : 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		u32 _rsvd1   : 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		u32 length   : 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	} r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) struct segment_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	union seg_flags flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	u32 v_base_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	u32 file_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) struct module_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u32 load_type  : 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	u32 auto_start : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	u32 domain_ll  : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	u32 domain_dp  : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	u32 rsvd       : 25;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) struct adsp_module_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	u32 struct_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u8  name[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	u8  uuid[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct module_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	u8  hash1[DEFAULT_HASH_SHA256_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	u32 entry_point;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u16 cfg_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u16 cfg_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u32 affinity_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u16 instance_max_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	u16 instance_bss_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct segment_desc segments[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) struct adsp_fw_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	u32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	u8  name[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	u32 preload_page_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	u32 fw_image_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	u32 feature_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	u16 major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	u16 minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u16 hotfix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	u16 build;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	u32 num_modules;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	u32 hw_buf_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	u32 hw_buf_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	u32 load_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) struct skl_ext_manifest_hdr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	u32 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	u16 version_major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	u16 version_minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	u32 entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int skl_get_pvtid_map(struct uuid_module *module, int instance_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	for (pvt_id = 0; pvt_id < module->max_instance; pvt_id++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (module->instance_id[pvt_id] == instance_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			return pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int skl_get_pvt_instance_id_map(struct skl_dev *skl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 				int module_id, int instance_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	struct uuid_module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	list_for_each_entry(module, &skl->uuid_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		if (module->id == module_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			return skl_get_pvtid_map(module, instance_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static inline int skl_getid_32(struct uuid_module *module, u64 *val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 				int word1_mask, int word2_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	int index, max_inst, pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	u32 mask_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	max_inst =  module->max_instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	mask_val = (u32)(*val >> word1_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (mask_val != 0xffffffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		index = ffz(mask_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		pvt_id = index + word1_mask + word2_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		if (pvt_id <= (max_inst - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			*val |= 1ULL << (index + word1_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			return pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static inline int skl_pvtid_128(struct uuid_module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	int j, i, word1_mask, word2_mask = 0, pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		word1_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			pvt_id = skl_getid_32(module, &module->pvt_id[j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 						word1_mask, word2_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			if (pvt_id >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				return pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			word1_mask += 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			if ((word1_mask + word2_mask) >= module->max_instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		word2_mask += 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (word2_mask >= module->max_instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * skl_get_pvt_id: generate a private id for use as module id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * @skl: driver context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * @uuid_mod: module's uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * @instance_id: module's instance id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * This generates a 128 bit private unique id for a module TYPE so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * module instance is unique
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int skl_get_pvt_id(struct skl_dev *skl, guid_t *uuid_mod, int instance_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct uuid_module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	list_for_each_entry(module, &skl->uuid_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		if (guid_equal(uuid_mod, &module->uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			pvt_id = skl_pvtid_128(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			if (pvt_id >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 				module->instance_id[pvt_id] = instance_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				return pvt_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) EXPORT_SYMBOL_GPL(skl_get_pvt_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * skl_put_pvt_id: free up the private id allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * @skl: driver context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  * @uuid_mod: module's uuid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * @pvt_id: module pvt id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  * This frees a 128 bit private unique id previously generated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int skl_put_pvt_id(struct skl_dev *skl, guid_t *uuid_mod, int *pvt_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct uuid_module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	list_for_each_entry(module, &skl->uuid_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		if (guid_equal(uuid_mod, &module->uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			if (*pvt_id != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 				i = (*pvt_id) / 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			module->pvt_id[i] &= ~(1 << (*pvt_id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			*pvt_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) EXPORT_SYMBOL_GPL(skl_put_pvt_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * Parse the firmware binary to get the UUID, module id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  * and loadable flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			unsigned int offset, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct adsp_fw_hdr *adsp_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct adsp_module_entry *mod_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	int i, num_entry, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	const char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct skl_dev *skl = ctx->thread_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	struct uuid_module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct firmware stripped_fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	unsigned int safe_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	/* Get the FW pointer to derive ADSP header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	stripped_fw.data = fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	stripped_fw.size = fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	skl_dsp_strip_extended_manifest(&stripped_fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	buf = stripped_fw.data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	/* check if we have enough space in file to move to header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	safe_file = sizeof(*adsp_hdr) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	if (stripped_fw.size <= safe_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	/* check 1st module entry is in file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	safe_file += adsp_hdr->len + sizeof(*mod_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (stripped_fw.size <= safe_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		dev_err(ctx->dev, "Small fw file size, No module entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	mod_entry = (struct adsp_module_entry *)(buf + offset + adsp_hdr->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	num_entry = adsp_hdr->num_modules;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	/* check all entries are in file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	safe_file += num_entry * sizeof(*mod_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	if (stripped_fw.size <= safe_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		dev_err(ctx->dev, "Small fw file size, No modules\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	 * Read the UUID(GUID) from FW Manifest.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	 * Populate the UUID table to store module_id and loadable flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * for the module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	for (i = 0; i < num_entry; i++, mod_entry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		module = kzalloc(sizeof(*module), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (!module) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			goto free_uuid_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		import_guid(&module->uuid, mod_entry->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		module->id = (i | (index << 12));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		module->is_loadable = mod_entry->type.load_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		module->max_instance = mod_entry->instance_max_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		size = sizeof(int) * mod_entry->instance_max_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		module->instance_id = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if (!module->instance_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			goto free_uuid_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		list_add_tail(&module->list, &skl->uuid_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		dev_dbg(ctx->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			"Adding uuid :%pUL   mod id: %d  Loadable: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			&module->uuid, module->id, module->is_loadable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) free_uuid_list:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	skl_freeup_uuid_list(skl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) void skl_freeup_uuid_list(struct skl_dev *skl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct uuid_module *uuid, *_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	list_for_each_entry_safe(uuid, _uuid, &skl->uuid_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		list_del(&uuid->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		kfree(uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)  * some firmware binary contains some extended manifest. This needs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  * to be stripped in that case before we load and use that image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)  * Get the module id for the module by checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)  * the table for the UUID for the module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int skl_dsp_strip_extended_manifest(struct firmware *fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct skl_ext_manifest_hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	/* check if fw file is greater than header we are looking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (fw->size < sizeof(hdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		pr_err("%s: Firmware file small, no hdr\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	hdr = (struct skl_ext_manifest_hdr *)fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		fw->size -= hdr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		fw->data += hdr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int skl_sst_ctx_init(struct device *dev, int irq, const char *fw_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct skl_dsp_loader_ops dsp_ops, struct skl_dev **dsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	struct sst_dsp_device *skl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct skl_dev *skl = *dsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct sst_dsp *sst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	skl->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	skl_dev->thread_context = skl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	INIT_LIST_HEAD(&skl->uuid_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	skl->dsp = skl_dsp_ctx_init(dev, skl_dev, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	if (!skl->dsp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		dev_err(skl->dev, "%s: no device\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	sst = skl->dsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	sst->fw_name = fw_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	sst->dsp_ops = dsp_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	init_waitqueue_head(&skl->mod_load_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	INIT_LIST_HEAD(&sst->module_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	skl->is_first_boot = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) int skl_prepare_lib_load(struct skl_dev *skl, struct skl_lib_info *linfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		struct firmware *stripped_fw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		unsigned int hdr_offset, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct sst_dsp *dsp = skl->dsp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (linfo->fw == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		ret = request_firmware(&linfo->fw, linfo->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 					skl->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			dev_err(skl->dev, "Request lib %s failed:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 				linfo->name, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	if (skl->is_first_boot) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		ret = snd_skl_parse_uuids(dsp, linfo->fw, hdr_offset, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	stripped_fw->data = linfo->fw->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	stripped_fw->size = linfo->fw->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	skl_dsp_strip_extended_manifest(stripped_fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) void skl_release_library(struct skl_lib_info *linfo, int lib_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	/* library indices start from 1 to N. 0 represents base FW */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	for (i = 1; i < lib_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		if (linfo[i].fw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			release_firmware(linfo[i].fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			linfo[i].fw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }