utils.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
  4. //
  5. // Authors: Cezary Rojewski <[email protected]>
  6. // Amadeusz Slawinski <[email protected]>
  7. //
  8. #include <linux/firmware.h>
  9. #include <linux/kfifo.h>
  10. #include <linux/slab.h>
  11. #include "avs.h"
  12. #include "messages.h"
  13. /* Caller responsible for holding adev->modres_mutex. */
  14. static int avs_module_entry_index(struct avs_dev *adev, const guid_t *uuid)
  15. {
  16. int i;
  17. for (i = 0; i < adev->mods_info->count; i++) {
  18. struct avs_module_entry *module;
  19. module = &adev->mods_info->entries[i];
  20. if (guid_equal(&module->uuid, uuid))
  21. return i;
  22. }
  23. return -ENOENT;
  24. }
  25. /* Caller responsible for holding adev->modres_mutex. */
  26. static int avs_module_id_entry_index(struct avs_dev *adev, u32 module_id)
  27. {
  28. int i;
  29. for (i = 0; i < adev->mods_info->count; i++) {
  30. struct avs_module_entry *module;
  31. module = &adev->mods_info->entries[i];
  32. if (module->module_id == module_id)
  33. return i;
  34. }
  35. return -ENOENT;
  36. }
  37. int avs_get_module_entry(struct avs_dev *adev, const guid_t *uuid, struct avs_module_entry *entry)
  38. {
  39. int idx;
  40. mutex_lock(&adev->modres_mutex);
  41. idx = avs_module_entry_index(adev, uuid);
  42. if (idx >= 0)
  43. memcpy(entry, &adev->mods_info->entries[idx], sizeof(*entry));
  44. mutex_unlock(&adev->modres_mutex);
  45. return (idx < 0) ? idx : 0;
  46. }
  47. int avs_get_module_id_entry(struct avs_dev *adev, u32 module_id, struct avs_module_entry *entry)
  48. {
  49. int idx;
  50. mutex_lock(&adev->modres_mutex);
  51. idx = avs_module_id_entry_index(adev, module_id);
  52. if (idx >= 0)
  53. memcpy(entry, &adev->mods_info->entries[idx], sizeof(*entry));
  54. mutex_unlock(&adev->modres_mutex);
  55. return (idx < 0) ? idx : 0;
  56. }
  57. int avs_get_module_id(struct avs_dev *adev, const guid_t *uuid)
  58. {
  59. struct avs_module_entry module;
  60. int ret;
  61. ret = avs_get_module_entry(adev, uuid, &module);
  62. return !ret ? module.module_id : -ENOENT;
  63. }
  64. bool avs_is_module_ida_empty(struct avs_dev *adev, u32 module_id)
  65. {
  66. bool ret = false;
  67. int idx;
  68. mutex_lock(&adev->modres_mutex);
  69. idx = avs_module_id_entry_index(adev, module_id);
  70. if (idx >= 0)
  71. ret = ida_is_empty(adev->mod_idas[idx]);
  72. mutex_unlock(&adev->modres_mutex);
  73. return ret;
  74. }
  75. /* Caller responsible for holding adev->modres_mutex. */
  76. static void avs_module_ida_destroy(struct avs_dev *adev)
  77. {
  78. int i = adev->mods_info ? adev->mods_info->count : 0;
  79. while (i--) {
  80. ida_destroy(adev->mod_idas[i]);
  81. kfree(adev->mod_idas[i]);
  82. }
  83. kfree(adev->mod_idas);
  84. }
  85. /* Caller responsible for holding adev->modres_mutex. */
  86. static int
  87. avs_module_ida_alloc(struct avs_dev *adev, struct avs_mods_info *newinfo, bool purge)
  88. {
  89. struct avs_mods_info *oldinfo = adev->mods_info;
  90. struct ida **ida_ptrs;
  91. u32 tocopy_count = 0;
  92. int i;
  93. if (!purge && oldinfo) {
  94. if (oldinfo->count >= newinfo->count)
  95. dev_warn(adev->dev, "refreshing %d modules info with %d\n",
  96. oldinfo->count, newinfo->count);
  97. tocopy_count = oldinfo->count;
  98. }
  99. ida_ptrs = kcalloc(newinfo->count, sizeof(*ida_ptrs), GFP_KERNEL);
  100. if (!ida_ptrs)
  101. return -ENOMEM;
  102. if (tocopy_count)
  103. memcpy(ida_ptrs, adev->mod_idas, tocopy_count * sizeof(*ida_ptrs));
  104. for (i = tocopy_count; i < newinfo->count; i++) {
  105. ida_ptrs[i] = kzalloc(sizeof(**ida_ptrs), GFP_KERNEL);
  106. if (!ida_ptrs[i]) {
  107. while (i--)
  108. kfree(ida_ptrs[i]);
  109. kfree(ida_ptrs);
  110. return -ENOMEM;
  111. }
  112. ida_init(ida_ptrs[i]);
  113. }
  114. /* If old elements have been reused, don't wipe them. */
  115. if (tocopy_count)
  116. kfree(adev->mod_idas);
  117. else
  118. avs_module_ida_destroy(adev);
  119. adev->mod_idas = ida_ptrs;
  120. return 0;
  121. }
  122. int avs_module_info_init(struct avs_dev *adev, bool purge)
  123. {
  124. struct avs_mods_info *info;
  125. int ret;
  126. ret = avs_ipc_get_modules_info(adev, &info);
  127. if (ret)
  128. return AVS_IPC_RET(ret);
  129. mutex_lock(&adev->modres_mutex);
  130. ret = avs_module_ida_alloc(adev, info, purge);
  131. if (ret < 0) {
  132. dev_err(adev->dev, "initialize module idas failed: %d\n", ret);
  133. goto exit;
  134. }
  135. /* Refresh current information with newly received table. */
  136. kfree(adev->mods_info);
  137. adev->mods_info = info;
  138. exit:
  139. mutex_unlock(&adev->modres_mutex);
  140. return ret;
  141. }
  142. void avs_module_info_free(struct avs_dev *adev)
  143. {
  144. mutex_lock(&adev->modres_mutex);
  145. avs_module_ida_destroy(adev);
  146. kfree(adev->mods_info);
  147. adev->mods_info = NULL;
  148. mutex_unlock(&adev->modres_mutex);
  149. }
  150. int avs_module_id_alloc(struct avs_dev *adev, u16 module_id)
  151. {
  152. int ret, idx, max_id;
  153. mutex_lock(&adev->modres_mutex);
  154. idx = avs_module_id_entry_index(adev, module_id);
  155. if (idx == -ENOENT) {
  156. dev_err(adev->dev, "invalid module id: %d", module_id);
  157. ret = -EINVAL;
  158. goto exit;
  159. }
  160. max_id = adev->mods_info->entries[idx].instance_max_count - 1;
  161. ret = ida_alloc_max(adev->mod_idas[idx], max_id, GFP_KERNEL);
  162. exit:
  163. mutex_unlock(&adev->modres_mutex);
  164. return ret;
  165. }
  166. void avs_module_id_free(struct avs_dev *adev, u16 module_id, u8 instance_id)
  167. {
  168. int idx;
  169. mutex_lock(&adev->modres_mutex);
  170. idx = avs_module_id_entry_index(adev, module_id);
  171. if (idx == -ENOENT) {
  172. dev_err(adev->dev, "invalid module id: %d", module_id);
  173. goto exit;
  174. }
  175. ida_free(adev->mod_idas[idx], instance_id);
  176. exit:
  177. mutex_unlock(&adev->modres_mutex);
  178. }
  179. /*
  180. * Once driver loads FW it should keep it in memory, so we are not affected
  181. * by FW removal from filesystem or even worse by loading different FW at
  182. * runtime suspend/resume.
  183. */
  184. int avs_request_firmware(struct avs_dev *adev, const struct firmware **fw_p, const char *name)
  185. {
  186. struct avs_fw_entry *entry;
  187. int ret;
  188. /* first check in list if it is not already loaded */
  189. list_for_each_entry(entry, &adev->fw_list, node) {
  190. if (!strcmp(name, entry->name)) {
  191. *fw_p = entry->fw;
  192. return 0;
  193. }
  194. }
  195. /* FW is not loaded, let's load it now and add to the list */
  196. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  197. if (!entry)
  198. return -ENOMEM;
  199. entry->name = kstrdup(name, GFP_KERNEL);
  200. if (!entry->name) {
  201. kfree(entry);
  202. return -ENOMEM;
  203. }
  204. ret = request_firmware(&entry->fw, name, adev->dev);
  205. if (ret < 0) {
  206. kfree(entry->name);
  207. kfree(entry);
  208. return ret;
  209. }
  210. *fw_p = entry->fw;
  211. list_add_tail(&entry->node, &adev->fw_list);
  212. return 0;
  213. }
  214. /*
  215. * Release single FW entry, used to handle errors in functions calling
  216. * avs_request_firmware()
  217. */
  218. void avs_release_last_firmware(struct avs_dev *adev)
  219. {
  220. struct avs_fw_entry *entry;
  221. entry = list_last_entry(&adev->fw_list, typeof(*entry), node);
  222. list_del(&entry->node);
  223. release_firmware(entry->fw);
  224. kfree(entry->name);
  225. kfree(entry);
  226. }
  227. /*
  228. * Release all FW entries, used on driver removal
  229. */
  230. void avs_release_firmwares(struct avs_dev *adev)
  231. {
  232. struct avs_fw_entry *entry, *tmp;
  233. list_for_each_entry_safe(entry, tmp, &adev->fw_list, node) {
  234. list_del(&entry->node);
  235. release_firmware(entry->fw);
  236. kfree(entry->name);
  237. kfree(entry);
  238. }
  239. }
  240. unsigned int __kfifo_fromio_locked(struct kfifo *fifo, const void __iomem *src, unsigned int len,
  241. spinlock_t *lock)
  242. {
  243. struct __kfifo *__fifo = &fifo->kfifo;
  244. unsigned long flags;
  245. unsigned int l, off;
  246. spin_lock_irqsave(lock, flags);
  247. len = min(len, kfifo_avail(fifo));
  248. off = __fifo->in & __fifo->mask;
  249. l = min(len, kfifo_size(fifo) - off);
  250. memcpy_fromio(__fifo->data + off, src, l);
  251. memcpy_fromio(__fifo->data, src + l, len - l);
  252. /* Make sure data copied from SRAM is visible to all CPUs. */
  253. smp_mb();
  254. __fifo->in += len;
  255. spin_unlock_irqrestore(lock, flags);
  256. return len;
  257. }