skl-sst-utils.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. #define MAX_INSTANCE_BUFF 2
  87. struct uuid_module {
  88. uuid_le uuid;
  89. int id;
  90. int is_loadable;
  91. int max_instance;
  92. u64 pvt_id[MAX_INSTANCE_BUFF];
  93. int *instance_id;
  94. struct list_head list;
  95. };
  96. struct skl_ext_manifest_hdr {
  97. u32 id;
  98. u32 len;
  99. u16 version_major;
  100. u16 version_minor;
  101. u32 entries;
  102. };
  103. int snd_skl_get_module_info(struct skl_sst *ctx,
  104. struct skl_module_cfg *mconfig)
  105. {
  106. struct uuid_module *module;
  107. uuid_le *uuid_mod;
  108. uuid_mod = (uuid_le *)mconfig->guid;
  109. if (list_empty(&ctx->uuid_list)) {
  110. dev_err(ctx->dev, "Module list is empty\n");
  111. return -EINVAL;
  112. }
  113. list_for_each_entry(module, &ctx->uuid_list, list) {
  114. if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
  115. mconfig->id.module_id = module->id;
  116. mconfig->is_loadable = module->is_loadable;
  117. return 0;
  118. }
  119. }
  120. return -EINVAL;
  121. }
  122. EXPORT_SYMBOL_GPL(snd_skl_get_module_info);
  123. static int skl_get_pvtid_map(struct uuid_module *module, int instance_id)
  124. {
  125. int pvt_id;
  126. for (pvt_id = 0; pvt_id < module->max_instance; pvt_id++) {
  127. if (module->instance_id[pvt_id] == instance_id)
  128. return pvt_id;
  129. }
  130. return -EINVAL;
  131. }
  132. int skl_get_pvt_instance_id_map(struct skl_sst *ctx,
  133. int module_id, int instance_id)
  134. {
  135. struct uuid_module *module;
  136. list_for_each_entry(module, &ctx->uuid_list, list) {
  137. if (module->id == module_id)
  138. return skl_get_pvtid_map(module, instance_id);
  139. }
  140. return -EINVAL;
  141. }
  142. EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map);
  143. static inline int skl_getid_32(struct uuid_module *module, u64 *val,
  144. int word1_mask, int word2_mask)
  145. {
  146. int index, max_inst, pvt_id;
  147. u32 mask_val;
  148. max_inst = module->max_instance;
  149. mask_val = (u32)(*val >> word1_mask);
  150. if (mask_val != 0xffffffff) {
  151. index = ffz(mask_val);
  152. pvt_id = index + word1_mask + word2_mask;
  153. if (pvt_id <= (max_inst - 1)) {
  154. *val |= 1ULL << (index + word1_mask);
  155. return pvt_id;
  156. }
  157. }
  158. return -EINVAL;
  159. }
  160. static inline int skl_pvtid_128(struct uuid_module *module)
  161. {
  162. int j, i, word1_mask, word2_mask = 0, pvt_id;
  163. for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
  164. word1_mask = 0;
  165. for (i = 0; i < 2; i++) {
  166. pvt_id = skl_getid_32(module, &module->pvt_id[j],
  167. word1_mask, word2_mask);
  168. if (pvt_id >= 0)
  169. return pvt_id;
  170. word1_mask += 32;
  171. if ((word1_mask + word2_mask) >= module->max_instance)
  172. return -EINVAL;
  173. }
  174. word2_mask += 64;
  175. if (word2_mask >= module->max_instance)
  176. return -EINVAL;
  177. }
  178. return -EINVAL;
  179. }
  180. /**
  181. * skl_get_pvt_id: generate a private id for use as module id
  182. *
  183. * @ctx: driver context
  184. * @mconfig: module configuration data
  185. *
  186. * This generates a 128 bit private unique id for a module TYPE so that
  187. * module instance is unique
  188. */
  189. int skl_get_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
  190. {
  191. struct uuid_module *module;
  192. uuid_le *uuid_mod;
  193. int pvt_id;
  194. uuid_mod = (uuid_le *)mconfig->guid;
  195. list_for_each_entry(module, &ctx->uuid_list, list) {
  196. if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
  197. pvt_id = skl_pvtid_128(module);
  198. if (pvt_id >= 0) {
  199. module->instance_id[pvt_id] =
  200. mconfig->id.instance_id;
  201. return pvt_id;
  202. }
  203. }
  204. }
  205. return -EINVAL;
  206. }
  207. EXPORT_SYMBOL_GPL(skl_get_pvt_id);
  208. /**
  209. * skl_put_pvt_id: free up the private id allocated
  210. *
  211. * @ctx: driver context
  212. * @mconfig: module configuration data
  213. *
  214. * This frees a 128 bit private unique id previously generated
  215. */
  216. int skl_put_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
  217. {
  218. int i;
  219. uuid_le *uuid_mod;
  220. struct uuid_module *module;
  221. uuid_mod = (uuid_le *)mconfig->guid;
  222. list_for_each_entry(module, &ctx->uuid_list, list) {
  223. if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
  224. if (mconfig->id.pvt_id != 0)
  225. i = (mconfig->id.pvt_id) / 64;
  226. else
  227. i = 0;
  228. module->pvt_id[i] &= ~(1 << (mconfig->id.pvt_id));
  229. mconfig->id.pvt_id = -1;
  230. return 0;
  231. }
  232. }
  233. return -EINVAL;
  234. }
  235. EXPORT_SYMBOL_GPL(skl_put_pvt_id);
  236. /*
  237. * Parse the firmware binary to get the UUID, module id
  238. * and loadable flags
  239. */
  240. int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
  241. unsigned int offset, int index)
  242. {
  243. struct adsp_fw_hdr *adsp_hdr;
  244. struct adsp_module_entry *mod_entry;
  245. int i, num_entry, size;
  246. uuid_le *uuid_bin;
  247. const char *buf;
  248. struct skl_sst *skl = ctx->thread_context;
  249. struct uuid_module *module;
  250. struct firmware stripped_fw;
  251. unsigned int safe_file;
  252. int ret = 0;
  253. /* Get the FW pointer to derive ADSP header */
  254. stripped_fw.data = fw->data;
  255. stripped_fw.size = fw->size;
  256. skl_dsp_strip_extended_manifest(&stripped_fw);
  257. buf = stripped_fw.data;
  258. /* check if we have enough space in file to move to header */
  259. safe_file = sizeof(*adsp_hdr) + offset;
  260. if (stripped_fw.size <= safe_file) {
  261. dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
  262. return -EINVAL;
  263. }
  264. adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
  265. /* check 1st module entry is in file */
  266. safe_file += adsp_hdr->len + sizeof(*mod_entry);
  267. if (stripped_fw.size <= safe_file) {
  268. dev_err(ctx->dev, "Small fw file size, No module entry\n");
  269. return -EINVAL;
  270. }
  271. mod_entry = (struct adsp_module_entry *)
  272. (buf + offset + adsp_hdr->len);
  273. num_entry = adsp_hdr->num_modules;
  274. /* check all entries are in file */
  275. safe_file += num_entry * sizeof(*mod_entry);
  276. if (stripped_fw.size <= safe_file) {
  277. dev_err(ctx->dev, "Small fw file size, No modules\n");
  278. return -EINVAL;
  279. }
  280. /*
  281. * Read the UUID(GUID) from FW Manifest.
  282. *
  283. * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
  284. * Populate the UUID table to store module_id and loadable flags
  285. * for the module.
  286. */
  287. for (i = 0; i < num_entry; i++, mod_entry++) {
  288. module = kzalloc(sizeof(*module), GFP_KERNEL);
  289. if (!module) {
  290. ret = -ENOMEM;
  291. goto free_uuid_list;
  292. }
  293. uuid_bin = (uuid_le *)mod_entry->uuid.id;
  294. memcpy(&module->uuid, uuid_bin, sizeof(module->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) {
  301. ret = -ENOMEM;
  302. goto free_uuid_list;
  303. }
  304. list_add_tail(&module->list, &skl->uuid_list);
  305. dev_dbg(ctx->dev,
  306. "Adding uuid :%pUL mod id: %d Loadable: %d\n",
  307. &module->uuid, module->id, module->is_loadable);
  308. }
  309. return 0;
  310. free_uuid_list:
  311. skl_freeup_uuid_list(skl);
  312. return ret;
  313. }
  314. void skl_freeup_uuid_list(struct skl_sst *ctx)
  315. {
  316. struct uuid_module *uuid, *_uuid;
  317. list_for_each_entry_safe(uuid, _uuid, &ctx->uuid_list, list) {
  318. list_del(&uuid->list);
  319. kfree(uuid);
  320. }
  321. }
  322. /*
  323. * some firmware binary contains some extended manifest. This needs
  324. * to be stripped in that case before we load and use that image.
  325. *
  326. * Get the module id for the module by checking
  327. * the table for the UUID for the module
  328. */
  329. int skl_dsp_strip_extended_manifest(struct firmware *fw)
  330. {
  331. struct skl_ext_manifest_hdr *hdr;
  332. /* check if fw file is greater than header we are looking */
  333. if (fw->size < sizeof(hdr)) {
  334. pr_err("%s: Firmware file small, no hdr\n", __func__);
  335. return -EINVAL;
  336. }
  337. hdr = (struct skl_ext_manifest_hdr *)fw->data;
  338. if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
  339. fw->size -= hdr->len;
  340. fw->data += hdr->len;
  341. }
  342. return 0;
  343. }