skl-sst-utils.c 9.7 KB

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