swap.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2015 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* This file has implementation for code swap logic. With code swap feature,
  17. * target can run the fw binary with even smaller IRAM size by using host
  18. * memory to store some of the code segments.
  19. */
  20. #include "core.h"
  21. #include "bmi.h"
  22. #include "debug.h"
  23. static int ath10k_swap_code_seg_fill(struct ath10k *ar,
  24. struct ath10k_swap_code_seg_info *seg_info,
  25. const void *data, size_t data_len)
  26. {
  27. u8 *virt_addr = seg_info->virt_address[0];
  28. u8 swap_magic[ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ] = {};
  29. const u8 *fw_data = data;
  30. union ath10k_swap_code_seg_item *swap_item;
  31. u32 length = 0;
  32. u32 payload_len;
  33. u32 total_payload_len = 0;
  34. u32 size_left = data_len;
  35. /* Parse swap bin and copy the content to host allocated memory.
  36. * The format is Address, length and value. The last 4-bytes is
  37. * target write address. Currently address field is not used.
  38. */
  39. seg_info->target_addr = -1;
  40. while (size_left >= sizeof(*swap_item)) {
  41. swap_item = (union ath10k_swap_code_seg_item *)fw_data;
  42. payload_len = __le32_to_cpu(swap_item->tlv.length);
  43. if ((payload_len > size_left) ||
  44. (payload_len == 0 &&
  45. size_left != sizeof(struct ath10k_swap_code_seg_tail))) {
  46. ath10k_err(ar, "refusing to parse invalid tlv length %d\n",
  47. payload_len);
  48. return -EINVAL;
  49. }
  50. if (payload_len == 0) {
  51. if (memcmp(swap_item->tail.magic_signature, swap_magic,
  52. ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ)) {
  53. ath10k_err(ar, "refusing an invalid swap file\n");
  54. return -EINVAL;
  55. }
  56. seg_info->target_addr =
  57. __le32_to_cpu(swap_item->tail.bmi_write_addr);
  58. break;
  59. }
  60. memcpy(virt_addr, swap_item->tlv.data, payload_len);
  61. virt_addr += payload_len;
  62. length = payload_len + sizeof(struct ath10k_swap_code_seg_tlv);
  63. size_left -= length;
  64. fw_data += length;
  65. total_payload_len += payload_len;
  66. }
  67. if (seg_info->target_addr == -1) {
  68. ath10k_err(ar, "failed to parse invalid swap file\n");
  69. return -EINVAL;
  70. }
  71. seg_info->seg_hw_info.swap_size = __cpu_to_le32(total_payload_len);
  72. return 0;
  73. }
  74. static void
  75. ath10k_swap_code_seg_free(struct ath10k *ar,
  76. struct ath10k_swap_code_seg_info *seg_info)
  77. {
  78. u32 seg_size;
  79. if (!seg_info)
  80. return;
  81. if (!seg_info->virt_address[0])
  82. return;
  83. seg_size = __le32_to_cpu(seg_info->seg_hw_info.size);
  84. dma_free_coherent(ar->dev, seg_size, seg_info->virt_address[0],
  85. seg_info->paddr[0]);
  86. }
  87. static struct ath10k_swap_code_seg_info *
  88. ath10k_swap_code_seg_alloc(struct ath10k *ar, size_t swap_bin_len)
  89. {
  90. struct ath10k_swap_code_seg_info *seg_info;
  91. void *virt_addr;
  92. dma_addr_t paddr;
  93. swap_bin_len = roundup(swap_bin_len, 2);
  94. if (swap_bin_len > ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX) {
  95. ath10k_err(ar, "refusing code swap bin because it is too big %zu > %d\n",
  96. swap_bin_len, ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX);
  97. return NULL;
  98. }
  99. seg_info = devm_kzalloc(ar->dev, sizeof(*seg_info), GFP_KERNEL);
  100. if (!seg_info)
  101. return NULL;
  102. virt_addr = dma_alloc_coherent(ar->dev, swap_bin_len, &paddr,
  103. GFP_KERNEL);
  104. if (!virt_addr) {
  105. ath10k_err(ar, "failed to allocate dma coherent memory\n");
  106. return NULL;
  107. }
  108. seg_info->seg_hw_info.bus_addr[0] = __cpu_to_le32(paddr);
  109. seg_info->seg_hw_info.size = __cpu_to_le32(swap_bin_len);
  110. seg_info->seg_hw_info.swap_size = __cpu_to_le32(swap_bin_len);
  111. seg_info->seg_hw_info.num_segs =
  112. __cpu_to_le32(ATH10K_SWAP_CODE_SEG_NUM_SUPPORTED);
  113. seg_info->seg_hw_info.size_log2 = __cpu_to_le32(ilog2(swap_bin_len));
  114. seg_info->virt_address[0] = virt_addr;
  115. seg_info->paddr[0] = paddr;
  116. return seg_info;
  117. }
  118. int ath10k_swap_code_seg_configure(struct ath10k *ar,
  119. enum ath10k_swap_code_seg_bin_type type)
  120. {
  121. int ret;
  122. struct ath10k_swap_code_seg_info *seg_info = NULL;
  123. switch (type) {
  124. case ATH10K_SWAP_CODE_SEG_BIN_TYPE_FW:
  125. if (!ar->swap.firmware_swap_code_seg_info)
  126. return 0;
  127. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot found firmware code swap binary\n");
  128. seg_info = ar->swap.firmware_swap_code_seg_info;
  129. break;
  130. default:
  131. case ATH10K_SWAP_CODE_SEG_BIN_TYPE_OTP:
  132. case ATH10K_SWAP_CODE_SEG_BIN_TYPE_UTF:
  133. ath10k_warn(ar, "ignoring unknown code swap binary type %d\n",
  134. type);
  135. return 0;
  136. }
  137. ret = ath10k_bmi_write_memory(ar, seg_info->target_addr,
  138. &seg_info->seg_hw_info,
  139. sizeof(seg_info->seg_hw_info));
  140. if (ret) {
  141. ath10k_err(ar, "failed to write Code swap segment information (%d)\n",
  142. ret);
  143. return ret;
  144. }
  145. return 0;
  146. }
  147. void ath10k_swap_code_seg_release(struct ath10k *ar)
  148. {
  149. ath10k_swap_code_seg_free(ar, ar->swap.firmware_swap_code_seg_info);
  150. ar->swap.firmware_codeswap_data = NULL;
  151. ar->swap.firmware_codeswap_len = 0;
  152. ar->swap.firmware_swap_code_seg_info = NULL;
  153. }
  154. int ath10k_swap_code_seg_init(struct ath10k *ar)
  155. {
  156. int ret;
  157. struct ath10k_swap_code_seg_info *seg_info;
  158. if (!ar->swap.firmware_codeswap_len || !ar->swap.firmware_codeswap_data)
  159. return 0;
  160. seg_info = ath10k_swap_code_seg_alloc(ar,
  161. ar->swap.firmware_codeswap_len);
  162. if (!seg_info) {
  163. ath10k_err(ar, "failed to allocate fw code swap segment\n");
  164. return -ENOMEM;
  165. }
  166. ret = ath10k_swap_code_seg_fill(ar, seg_info,
  167. ar->swap.firmware_codeswap_data,
  168. ar->swap.firmware_codeswap_len);
  169. if (ret) {
  170. ath10k_warn(ar, "failed to initialize fw code swap segment: %d\n",
  171. ret);
  172. ath10k_swap_code_seg_free(ar, seg_info);
  173. return ret;
  174. }
  175. ar->swap.firmware_swap_code_seg_info = seg_info;
  176. return 0;
  177. }