1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62#include "iwl-drv.h"
63#include "runtime.h"
64#include "fw/api/commands.h"
65
66void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
67{
68 int i;
69
70 if (!fwrt->fw_paging_db[0].fw_paging_block)
71 return;
72
73 for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
74 struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
75
76 if (!paging->fw_paging_block) {
77 IWL_DEBUG_FW(fwrt,
78 "Paging: block %d already freed, continue to next page\n",
79 i);
80
81 continue;
82 }
83 dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
84 paging->fw_paging_size, DMA_BIDIRECTIONAL);
85
86 __free_pages(paging->fw_paging_block,
87 get_order(paging->fw_paging_size));
88 paging->fw_paging_block = NULL;
89 }
90
91 memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
92}
93IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
94
95static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
96 const struct fw_img *image)
97{
98 struct page *block;
99 dma_addr_t phys = 0;
100 int blk_idx, order, num_of_pages, size;
101
102 if (fwrt->fw_paging_db[0].fw_paging_block)
103 return 0;
104
105
106 BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
107
108 num_of_pages = image->paging_mem_size / FW_PAGING_SIZE;
109 fwrt->num_of_paging_blk =
110 DIV_ROUND_UP(num_of_pages, NUM_OF_PAGE_PER_GROUP);
111 fwrt->num_of_pages_in_last_blk =
112 num_of_pages -
113 NUM_OF_PAGE_PER_GROUP * (fwrt->num_of_paging_blk - 1);
114
115 IWL_DEBUG_FW(fwrt,
116 "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
117 fwrt->num_of_paging_blk,
118 fwrt->num_of_pages_in_last_blk);
119
120
121
122
123 for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
124
125 size = blk_idx ? PAGING_BLOCK_SIZE : FW_PAGING_SIZE;
126 order = get_order(size);
127 block = alloc_pages(GFP_KERNEL, order);
128 if (!block) {
129
130 iwl_free_fw_paging(fwrt);
131 return -ENOMEM;
132 }
133
134 fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
135 fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
136
137 phys = dma_map_page(fwrt->trans->dev, block, 0,
138 PAGE_SIZE << order,
139 DMA_BIDIRECTIONAL);
140 if (dma_mapping_error(fwrt->trans->dev, phys)) {
141
142
143
144
145 iwl_free_fw_paging(fwrt);
146 return -ENOMEM;
147 }
148 fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
149
150 if (!blk_idx)
151 IWL_DEBUG_FW(fwrt,
152 "Paging: allocated 4K(CSS) bytes (order %d) for firmware paging.\n",
153 order);
154 else
155 IWL_DEBUG_FW(fwrt,
156 "Paging: allocated 32K bytes (order %d) for firmware paging.\n",
157 order);
158 }
159
160 return 0;
161}
162
163static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
164 const struct fw_img *image)
165{
166 int sec_idx, idx;
167 u32 offset = 0;
168
169
170
171
172
173
174
175
176
177
178
179
180 for (sec_idx = 0; sec_idx < image->num_sec; sec_idx++) {
181 if (image->sec[sec_idx].offset == PAGING_SEPARATOR_SECTION) {
182 sec_idx++;
183 break;
184 }
185 }
186
187
188
189
190
191 if (sec_idx >= image->num_sec - 1) {
192 IWL_ERR(fwrt, "Paging: Missing CSS and/or paging sections\n");
193 iwl_free_fw_paging(fwrt);
194 return -EINVAL;
195 }
196
197
198 IWL_DEBUG_FW(fwrt, "Paging: load paging CSS to FW, sec = %d\n",
199 sec_idx);
200
201 memcpy(page_address(fwrt->fw_paging_db[0].fw_paging_block),
202 image->sec[sec_idx].data,
203 fwrt->fw_paging_db[0].fw_paging_size);
204 dma_sync_single_for_device(fwrt->trans->dev,
205 fwrt->fw_paging_db[0].fw_paging_phys,
206 fwrt->fw_paging_db[0].fw_paging_size,
207 DMA_BIDIRECTIONAL);
208
209 IWL_DEBUG_FW(fwrt,
210 "Paging: copied %d CSS bytes to first block\n",
211 fwrt->fw_paging_db[0].fw_paging_size);
212
213 sec_idx++;
214
215
216
217
218
219
220
221 for (idx = 1; idx < fwrt->num_of_paging_blk; idx++) {
222 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
223
224 memcpy(page_address(block->fw_paging_block),
225 image->sec[sec_idx].data + offset,
226 block->fw_paging_size);
227 dma_sync_single_for_device(fwrt->trans->dev,
228 block->fw_paging_phys,
229 block->fw_paging_size,
230 DMA_BIDIRECTIONAL);
231
232 IWL_DEBUG_FW(fwrt,
233 "Paging: copied %d paging bytes to block %d\n",
234 fwrt->fw_paging_db[idx].fw_paging_size,
235 idx);
236
237 offset += fwrt->fw_paging_db[idx].fw_paging_size;
238 }
239
240
241 if (fwrt->num_of_pages_in_last_blk > 0) {
242 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
243
244 memcpy(page_address(block->fw_paging_block),
245 image->sec[sec_idx].data + offset,
246 FW_PAGING_SIZE * fwrt->num_of_pages_in_last_blk);
247 dma_sync_single_for_device(fwrt->trans->dev,
248 block->fw_paging_phys,
249 block->fw_paging_size,
250 DMA_BIDIRECTIONAL);
251
252 IWL_DEBUG_FW(fwrt,
253 "Paging: copied %d pages in the last block %d\n",
254 fwrt->num_of_pages_in_last_blk, idx);
255 }
256
257 return 0;
258}
259
260static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
261 const struct fw_img *fw)
262{
263 int ret;
264
265 ret = iwl_alloc_fw_paging_mem(fwrt, fw);
266 if (ret)
267 return ret;
268
269 return iwl_fill_paging_mem(fwrt, fw);
270}
271
272
273static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
274 const struct fw_img *fw)
275{
276 struct iwl_fw_paging_cmd paging_cmd = {
277 .flags = cpu_to_le32(PAGING_CMD_IS_SECURED |
278 PAGING_CMD_IS_ENABLED |
279 (fwrt->num_of_pages_in_last_blk <<
280 PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
281 .block_size = cpu_to_le32(BLOCK_2_EXP_SIZE),
282 .block_num = cpu_to_le32(fwrt->num_of_paging_blk),
283 };
284 struct iwl_host_cmd hcmd = {
285 .id = iwl_cmd_id(FW_PAGING_BLOCK_CMD, IWL_ALWAYS_LONG_GROUP, 0),
286 .len = { sizeof(paging_cmd), },
287 .data = { &paging_cmd, },
288 };
289 int blk_idx;
290
291
292 for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
293 dma_addr_t addr = fwrt->fw_paging_db[blk_idx].fw_paging_phys;
294 __le32 phy_addr;
295
296 addr = addr >> PAGE_2_EXP_SIZE;
297 phy_addr = cpu_to_le32(addr);
298 paging_cmd.device_phy_addr[blk_idx] = phy_addr;
299 }
300
301 return iwl_trans_send_cmd(fwrt->trans, &hcmd);
302}
303
304int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
305{
306 const struct fw_img *fw = &fwrt->fw->img[type];
307 int ret;
308
309 if (fwrt->trans->cfg->gen2)
310 return 0;
311
312
313
314
315
316
317 if (!fw->paging_mem_size)
318 return 0;
319
320 ret = iwl_save_fw_paging(fwrt, fw);
321 if (ret) {
322 IWL_ERR(fwrt, "failed to save the FW paging image\n");
323 return ret;
324 }
325
326 ret = iwl_send_paging_cmd(fwrt, fw);
327 if (ret) {
328 IWL_ERR(fwrt, "failed to send the paging cmd\n");
329 iwl_free_fw_paging(fwrt);
330 return ret;
331 }
332
333 return 0;
334}
335IWL_EXPORT_SYMBOL(iwl_init_paging);
336