skl-sst-utils.c 9.1 KB

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