swap.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. const struct ath10k_fw_file *fw_file)
  120. {
  121. int ret;
  122. struct ath10k_swap_code_seg_info *seg_info = NULL;
  123. if (!fw_file->firmware_swap_code_seg_info)
  124. return 0;
  125. ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot found firmware code swap binary\n");
  126. seg_info = fw_file->firmware_swap_code_seg_info;
  127. ret = ath10k_bmi_write_memory(ar, seg_info->target_addr,
  128. &seg_info->seg_hw_info,
  129. sizeof(seg_info->seg_hw_info));
  130. if (ret) {
  131. ath10k_err(ar, "failed to write Code swap segment information (%d)\n",
  132. ret);
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. void ath10k_swap_code_seg_release(struct ath10k *ar,
  138. struct ath10k_fw_file *fw_file)
  139. {
  140. ath10k_swap_code_seg_free(ar, fw_file->firmware_swap_code_seg_info);
  141. /* FIXME: these two assignments look to bein wrong place! Shouldn't
  142. * they be in ath10k_core_free_firmware_files() like the rest?
  143. */
  144. fw_file->codeswap_data = NULL;
  145. fw_file->codeswap_len = 0;
  146. fw_file->firmware_swap_code_seg_info = NULL;
  147. }
  148. int ath10k_swap_code_seg_init(struct ath10k *ar, struct ath10k_fw_file *fw_file)
  149. {
  150. int ret;
  151. struct ath10k_swap_code_seg_info *seg_info;
  152. const void *codeswap_data;
  153. size_t codeswap_len;
  154. codeswap_data = fw_file->codeswap_data;
  155. codeswap_len = fw_file->codeswap_len;
  156. if (!codeswap_len || !codeswap_data)
  157. return 0;
  158. seg_info = ath10k_swap_code_seg_alloc(ar, codeswap_len);
  159. if (!seg_info) {
  160. ath10k_err(ar, "failed to allocate fw code swap segment\n");
  161. return -ENOMEM;
  162. }
  163. ret = ath10k_swap_code_seg_fill(ar, seg_info,
  164. codeswap_data, codeswap_len);
  165. if (ret) {
  166. ath10k_warn(ar, "failed to initialize fw code swap segment: %d\n",
  167. ret);
  168. ath10k_swap_code_seg_free(ar, seg_info);
  169. return ret;
  170. }
  171. fw_file->firmware_swap_code_seg_info = seg_info;
  172. return 0;
  173. }