1
2
3
4
5
6
7
8
9
10
11#include "core.h"
12#include "bmi.h"
13#include "debug.h"
14
15static int ath10k_swap_code_seg_fill(struct ath10k *ar,
16 struct ath10k_swap_code_seg_info *seg_info,
17 const void *data, size_t data_len)
18{
19 u8 *virt_addr = seg_info->virt_address[0];
20 u8 swap_magic[ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ] = {};
21 const u8 *fw_data = data;
22 union ath10k_swap_code_seg_item *swap_item;
23 u32 length = 0;
24 u32 payload_len;
25 u32 total_payload_len = 0;
26 u32 size_left = data_len;
27
28
29
30
31
32 seg_info->target_addr = -1;
33 while (size_left >= sizeof(*swap_item)) {
34 swap_item = (union ath10k_swap_code_seg_item *)fw_data;
35 payload_len = __le32_to_cpu(swap_item->tlv.length);
36 if ((payload_len > size_left) ||
37 (payload_len == 0 &&
38 size_left != sizeof(struct ath10k_swap_code_seg_tail))) {
39 ath10k_err(ar, "refusing to parse invalid tlv length %d\n",
40 payload_len);
41 return -EINVAL;
42 }
43
44 if (payload_len == 0) {
45 if (memcmp(swap_item->tail.magic_signature, swap_magic,
46 ATH10K_SWAP_CODE_SEG_MAGIC_BYTES_SZ)) {
47 ath10k_err(ar, "refusing an invalid swap file\n");
48 return -EINVAL;
49 }
50 seg_info->target_addr =
51 __le32_to_cpu(swap_item->tail.bmi_write_addr);
52 break;
53 }
54
55 memcpy(virt_addr, swap_item->tlv.data, payload_len);
56 virt_addr += payload_len;
57 length = payload_len + sizeof(struct ath10k_swap_code_seg_tlv);
58 size_left -= length;
59 fw_data += length;
60 total_payload_len += payload_len;
61 }
62
63 if (seg_info->target_addr == -1) {
64 ath10k_err(ar, "failed to parse invalid swap file\n");
65 return -EINVAL;
66 }
67 seg_info->seg_hw_info.swap_size = __cpu_to_le32(total_payload_len);
68
69 return 0;
70}
71
72static void
73ath10k_swap_code_seg_free(struct ath10k *ar,
74 struct ath10k_swap_code_seg_info *seg_info)
75{
76 u32 seg_size;
77
78 if (!seg_info)
79 return;
80
81 if (!seg_info->virt_address[0])
82 return;
83
84 seg_size = __le32_to_cpu(seg_info->seg_hw_info.size);
85 dma_free_coherent(ar->dev, seg_size, seg_info->virt_address[0],
86 seg_info->paddr[0]);
87}
88
89static struct ath10k_swap_code_seg_info *
90ath10k_swap_code_seg_alloc(struct ath10k *ar, size_t swap_bin_len)
91{
92 struct ath10k_swap_code_seg_info *seg_info;
93 void *virt_addr;
94 dma_addr_t paddr;
95
96 swap_bin_len = roundup(swap_bin_len, 2);
97 if (swap_bin_len > ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX) {
98 ath10k_err(ar, "refusing code swap bin because it is too big %zu > %d\n",
99 swap_bin_len, ATH10K_SWAP_CODE_SEG_BIN_LEN_MAX);
100 return NULL;
101 }
102
103 seg_info = devm_kzalloc(ar->dev, sizeof(*seg_info), GFP_KERNEL);
104 if (!seg_info)
105 return NULL;
106
107 virt_addr = dma_alloc_coherent(ar->dev, swap_bin_len, &paddr,
108 GFP_KERNEL);
109 if (!virt_addr) {
110 ath10k_err(ar, "failed to allocate dma coherent memory\n");
111 return NULL;
112 }
113
114 seg_info->seg_hw_info.bus_addr[0] = __cpu_to_le32(paddr);
115 seg_info->seg_hw_info.size = __cpu_to_le32(swap_bin_len);
116 seg_info->seg_hw_info.swap_size = __cpu_to_le32(swap_bin_len);
117 seg_info->seg_hw_info.num_segs =
118 __cpu_to_le32(ATH10K_SWAP_CODE_SEG_NUM_SUPPORTED);
119 seg_info->seg_hw_info.size_log2 = __cpu_to_le32(ilog2(swap_bin_len));
120 seg_info->virt_address[0] = virt_addr;
121 seg_info->paddr[0] = paddr;
122
123 return seg_info;
124}
125
126int ath10k_swap_code_seg_configure(struct ath10k *ar,
127 const struct ath10k_fw_file *fw_file)
128{
129 int ret;
130 struct ath10k_swap_code_seg_info *seg_info = NULL;
131
132 if (!fw_file->firmware_swap_code_seg_info)
133 return 0;
134
135 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot found firmware code swap binary\n");
136
137 seg_info = fw_file->firmware_swap_code_seg_info;
138
139 ret = ath10k_bmi_write_memory(ar, seg_info->target_addr,
140 &seg_info->seg_hw_info,
141 sizeof(seg_info->seg_hw_info));
142 if (ret) {
143 ath10k_err(ar, "failed to write Code swap segment information (%d)\n",
144 ret);
145 return ret;
146 }
147
148 return 0;
149}
150
151void ath10k_swap_code_seg_release(struct ath10k *ar,
152 struct ath10k_fw_file *fw_file)
153{
154 ath10k_swap_code_seg_free(ar, fw_file->firmware_swap_code_seg_info);
155
156
157
158
159 fw_file->codeswap_data = NULL;
160 fw_file->codeswap_len = 0;
161
162 fw_file->firmware_swap_code_seg_info = NULL;
163}
164
165int ath10k_swap_code_seg_init(struct ath10k *ar, struct ath10k_fw_file *fw_file)
166{
167 int ret;
168 struct ath10k_swap_code_seg_info *seg_info;
169 const void *codeswap_data;
170 size_t codeswap_len;
171
172 codeswap_data = fw_file->codeswap_data;
173 codeswap_len = fw_file->codeswap_len;
174
175 if (!codeswap_len || !codeswap_data)
176 return 0;
177
178 seg_info = ath10k_swap_code_seg_alloc(ar, codeswap_len);
179 if (!seg_info) {
180 ath10k_err(ar, "failed to allocate fw code swap segment\n");
181 return -ENOMEM;
182 }
183
184 ret = ath10k_swap_code_seg_fill(ar, seg_info,
185 codeswap_data, codeswap_len);
186
187 if (ret) {
188 ath10k_warn(ar, "failed to initialize fw code swap segment: %d\n",
189 ret);
190 ath10k_swap_code_seg_free(ar, seg_info);
191 return ret;
192 }
193
194 fw_file->firmware_swap_code_seg_info = seg_info;
195
196 return 0;
197}
198