swap.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (c) 2015-2016 Qualcomm Atheros, Inc.
  4. */
  5. /* This file has implementation for code swap logic. With code swap feature,
  6. * target can run the fw binary with even smaller IRAM size by using host
  7. * memory to store some of the code segments.
  8. */
  9. #include "core.h"
  10. #include "bmi.h"
  11. #include "debug.h"
  12. static int ath10k_swap_code_seg_fill(struct ath10k *ar,
  13. struct ath10k_swap_code_seg_info *seg_info,
  14. const void *data, size_t data_len)
  15. {
  16. u8 *virt_addr = seg_info->virt_address[0];
  17. u8 swap_magic[ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ] = {};
  18. const u8 *fw_data = data;
  19. union ath10k_swap_code_seg_item *swap_item;
  20. u32 length = 0;
  21. u32 payload_len;
  22. u32 total_payload_len = 0;
  23. u32 size_left = data_len;
  24. /* Parse swap bin and copy the content to host allocated memory.
  25. * The format is Address, length and value. The last 4-bytes is
  26. * target write address. Currently address field is not used.
  27. */
  28. seg_info->target_addr = -1;
  29. while (size_left >= sizeof(*swap_item)) {
  30. swap_item = (union ath10k_swap_code_seg_item *)fw_data;
  31. payload_len = __le32_to_cpu(swap_item->tlv.length);
  32. if ((payload_len > size_left) ||
  33. (payload_len == 0 &&
  34. size_left != sizeof(struct ath10k_swap_code_seg_tail))) {
  35. ath10k_err(ar, "refusing to parse invalid tlv length %d\n",
  36. payload_len);
  37. return -EINVAL;
  38. }
  39. if (payload_len == 0) {
  40. if (memcmp(swap_item->tail.magic_signature, swap_magic,
  41. ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ)) {
  42. ath10k_err(ar, "refusing an invalid swap file\n");
  43. return -EINVAL;
  44. }
  45. seg_info->target_addr =
  46. __le32_to_cpu(swap_item->tail.bmi_write_addr);
  47. break;
  48. }
  49. memcpy(virt_addr, swap_item->tlv.data, payload_len);
  50. virt_addr += payload_len;
  51. length = payload_len + sizeof(struct ath10k_swap_code_seg_tlv);
  52. size_left -= length;
  53. fw_data += length;
  54. total_payload_len += payload_len;
  55. }
  56. if (seg_info->target_addr == -1) {
  57. ath10k_err(ar, "failed to parse invalid swap file\n");
  58. return -EINVAL;
  59. }
  60. seg_info->seg_hw_info.swap_size = __cpu_to_le32(total_payload_len);
  61. return 0;
  62. }
  63. static void
  64. ath10k_swap_code_seg_free(struct ath10k *ar,
  65. struct ath10k_swap_code_seg_info *seg_info)
  66. {
  67. u32 seg_size;
  68. if (!seg_info)
  69. return;
  70. if (!seg_info->virt_address[0])
  71. return;
  72. seg_size = __le32_to_cpu(seg_info->seg_hw_info.size);
  73. dma_free_coherent(ar->dev, seg_size, seg_info->virt_address[0],
  74. seg_info->paddr[0]);
  75. }
  76. static struct ath10k_swap_code_seg_info *
  77. ath10k_swap_code_seg_alloc(struct ath10k *ar, size_t swap_bin_len)
  78. {
  79. struct ath10k_swap_code_seg_info *seg_info;
  80. void *virt_addr;
  81. dma_addr_t paddr;
  82. swap_bin_len = roundup(swap_bin_len, 2);
  83. if (swap_bin_len > ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX) {
  84. ath10k_err(ar, "refusing code swap bin because it is too big %zu > %d\n",
  85. swap_bin_len, ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX);
  86. return NULL;
  87. }
  88. seg_info = devm_kzalloc(ar->dev, sizeof(*seg_info), GFP_KERNEL);
  89. if (!seg_info)
  90. return NULL;
  91. virt_addr = dma_alloc_coherent(ar->dev, swap_bin_len, &paddr,
  92. GFP_KERNEL);
  93. if (!virt_addr)
  94. return NULL;
  95. seg_info->seg_hw_info.bus_addr[0] = __cpu_to_le32(paddr);
  96. seg_info->seg_hw_info.size = __cpu_to_le32(swap_bin_len);
  97. seg_info->seg_hw_info.swap_size = __cpu_to_le32(swap_bin_len);
  98. seg_info->seg_hw_info.num_segs =
  99. __cpu_to_le32(ATH10K_SWAP_CODE_SEG_NUM_SUPPORTED);
  100. seg_info->seg_hw_info.size_log2 = __cpu_to_le32(ilog2(swap_bin_len));
  101. seg_info->virt_address[0] = virt_addr;
  102. seg_info->paddr[0] = paddr;
  103. return seg_info;
  104. }
  105. int ath10k_swap_code_seg_configure(struct ath10k *ar,
  106. const struct ath10k_fw_file *fw_file)
  107. {
  108. int ret;
  109. struct ath10k_swap_code_seg_info *seg_info = NULL;
  110. if (!fw_file->firmware_swap_code_seg_info)
  111. return 0;
  112. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot found firmware code swap binary\n");
  113. seg_info = fw_file->firmware_swap_code_seg_info;
  114. ret = ath10k_bmi_write_memory(ar, seg_info->target_addr,
  115. &seg_info->seg_hw_info,
  116. sizeof(seg_info->seg_hw_info));
  117. if (ret) {
  118. ath10k_err(ar, "failed to write Code swap segment information (%d)\n",
  119. ret);
  120. return ret;
  121. }
  122. return 0;
  123. }
  124. void ath10k_swap_code_seg_release(struct ath10k *ar,
  125. struct ath10k_fw_file *fw_file)
  126. {
  127. ath10k_swap_code_seg_free(ar, fw_file->firmware_swap_code_seg_info);
  128. /* FIXME: these two assignments look to bein wrong place! Shouldn't
  129. * they be in ath10k_core_free_firmware_files() like the rest?
  130. */
  131. fw_file->codeswap_data = NULL;
  132. fw_file->codeswap_len = 0;
  133. fw_file->firmware_swap_code_seg_info = NULL;
  134. }
  135. int ath10k_swap_code_seg_init(struct ath10k *ar, struct ath10k_fw_file *fw_file)
  136. {
  137. int ret;
  138. struct ath10k_swap_code_seg_info *seg_info;
  139. const void *codeswap_data;
  140. size_t codeswap_len;
  141. codeswap_data = fw_file->codeswap_data;
  142. codeswap_len = fw_file->codeswap_len;
  143. if (!codeswap_len || !codeswap_data)
  144. return 0;
  145. seg_info = ath10k_swap_code_seg_alloc(ar, codeswap_len);
  146. if (!seg_info) {
  147. ath10k_err(ar, "failed to allocate fw code swap segment\n");
  148. return -ENOMEM;
  149. }
  150. ret = ath10k_swap_code_seg_fill(ar, seg_info,
  151. codeswap_data, codeswap_len);
  152. if (ret) {
  153. ath10k_warn(ar, "failed to initialize fw code swap segment: %d\n",
  154. ret);
  155. ath10k_swap_code_seg_free(ar, seg_info);
  156. return ret;
  157. }
  158. fw_file->firmware_swap_code_seg_info = seg_info;
  159. return 0;
  160. }