GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / net / wireless / intel / iwlwifi / fw / paging.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018        Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018        Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  *****************************************************************************/
64 #include "iwl-drv.h"
65 #include "runtime.h"
66 #include "fw/api/commands.h"
67
68 void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
69 {
70         int i;
71
72         if (!fwrt->fw_paging_db[0].fw_paging_block)
73                 return;
74
75         for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
76                 struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
77
78                 if (!paging->fw_paging_block) {
79                         IWL_DEBUG_FW(fwrt,
80                                      "Paging: block %d already freed, continue to next page\n",
81                                      i);
82
83                         continue;
84                 }
85                 dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
86                                paging->fw_paging_size, DMA_BIDIRECTIONAL);
87
88                 __free_pages(paging->fw_paging_block,
89                              get_order(paging->fw_paging_size));
90                 paging->fw_paging_block = NULL;
91         }
92         kfree(fwrt->trans->paging_download_buf);
93         fwrt->trans->paging_download_buf = NULL;
94         fwrt->trans->paging_db = NULL;
95
96         memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
97 }
98 IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
99
100 static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
101                                    const struct fw_img *image)
102 {
103         struct page *block;
104         dma_addr_t phys = 0;
105         int blk_idx, order, num_of_pages, size, dma_enabled;
106
107         if (fwrt->fw_paging_db[0].fw_paging_block)
108                 return 0;
109
110         dma_enabled = is_device_dma_capable(fwrt->trans->dev);
111
112         /* ensure BLOCK_2_EXP_SIZE is power of 2 of PAGING_BLOCK_SIZE */
113         BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
114
115         num_of_pages = image->paging_mem_size / FW_PAGING_SIZE;
116         fwrt->num_of_paging_blk =
117                 DIV_ROUND_UP(num_of_pages, NUM_OF_PAGE_PER_GROUP);
118         fwrt->num_of_pages_in_last_blk =
119                 num_of_pages -
120                 NUM_OF_PAGE_PER_GROUP * (fwrt->num_of_paging_blk - 1);
121
122         IWL_DEBUG_FW(fwrt,
123                      "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
124                      fwrt->num_of_paging_blk,
125                      fwrt->num_of_pages_in_last_blk);
126
127         /*
128          * Allocate CSS and paging blocks in dram.
129          */
130         for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
131                 /* For CSS allocate 4KB, for others PAGING_BLOCK_SIZE (32K) */
132                 size = blk_idx ? PAGING_BLOCK_SIZE : FW_PAGING_SIZE;
133                 order = get_order(size);
134                 block = alloc_pages(GFP_KERNEL, order);
135                 if (!block) {
136                         /* free all the previous pages since we failed */
137                         iwl_free_fw_paging(fwrt);
138                         return -ENOMEM;
139                 }
140
141                 fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
142                 fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
143
144                 if (dma_enabled) {
145                         phys = dma_map_page(fwrt->trans->dev, block, 0,
146                                             PAGE_SIZE << order,
147                                             DMA_BIDIRECTIONAL);
148                         if (dma_mapping_error(fwrt->trans->dev, phys)) {
149                                 /*
150                                  * free the previous pages and the current one
151                                  * since we failed to map_page.
152                                  */
153                                 iwl_free_fw_paging(fwrt);
154                                 return -ENOMEM;
155                         }
156                         fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
157                 } else {
158                         fwrt->fw_paging_db[blk_idx].fw_paging_phys =
159                                 PAGING_ADDR_SIG |
160                                 blk_idx << BLOCK_2_EXP_SIZE;
161                 }
162
163                 if (!blk_idx)
164                         IWL_DEBUG_FW(fwrt,
165                                      "Paging: allocated 4K(CSS) bytes (order %d) for firmware paging.\n",
166                                      order);
167                 else
168                         IWL_DEBUG_FW(fwrt,
169                                      "Paging: allocated 32K bytes (order %d) for firmware paging.\n",
170                                      order);
171         }
172
173         return 0;
174 }
175
176 static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
177                                const struct fw_img *image)
178 {
179         int sec_idx, idx, ret;
180         u32 offset = 0;
181
182         /*
183          * find where is the paging image start point:
184          * if CPU2 exist and it's in paging format, then the image looks like:
185          * CPU1 sections (2 or more)
186          * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
187          * CPU2 sections (not paged)
188          * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
189          * non paged to CPU2 paging sec
190          * CPU2 paging CSS
191          * CPU2 paging image (including instruction and data)
192          */
193         for (sec_idx = 0; sec_idx < image->num_sec; sec_idx++) {
194                 if (image->sec[sec_idx].offset == PAGING_SEPARATOR_SECTION) {
195                         sec_idx++;
196                         break;
197                 }
198         }
199
200         /*
201          * If paging is enabled there should be at least 2 more sections left
202          * (one for CSS and one for Paging data)
203          */
204         if (sec_idx >= image->num_sec - 1) {
205                 IWL_ERR(fwrt, "Paging: Missing CSS and/or paging sections\n");
206                 ret = -EINVAL;
207                 goto err;
208         }
209
210         /* copy the CSS block to the dram */
211         IWL_DEBUG_FW(fwrt, "Paging: load paging CSS to FW, sec = %d\n",
212                      sec_idx);
213
214         if (image->sec[sec_idx].len > fwrt->fw_paging_db[0].fw_paging_size) {
215                 IWL_ERR(fwrt, "CSS block is larger than paging size\n");
216                 ret = -EINVAL;
217                 goto err;
218         }
219
220         memcpy(page_address(fwrt->fw_paging_db[0].fw_paging_block),
221                image->sec[sec_idx].data,
222                image->sec[sec_idx].len);
223         dma_sync_single_for_device(fwrt->trans->dev,
224                                    fwrt->fw_paging_db[0].fw_paging_phys,
225                                    fwrt->fw_paging_db[0].fw_paging_size,
226                                    DMA_BIDIRECTIONAL);
227
228         IWL_DEBUG_FW(fwrt,
229                      "Paging: copied %d CSS bytes to first block\n",
230                      fwrt->fw_paging_db[0].fw_paging_size);
231
232         sec_idx++;
233
234         /*
235          * copy the paging blocks to the dram
236          * loop index start from 1 since that CSS block already copied to dram
237          * and CSS index is 0.
238          * loop stop at num_of_paging_blk since that last block is not full.
239          */
240         for (idx = 1; idx < fwrt->num_of_paging_blk; idx++) {
241                 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
242
243                 if (block->fw_paging_size > image->sec[sec_idx].len - offset) {
244                         IWL_ERR(fwrt,
245                                 "Paging: paging size is larger than remaining data in block %d\n",
246                                 idx);
247                         ret = -EINVAL;
248                         goto err;
249                 }
250
251                 memcpy(page_address(block->fw_paging_block),
252                        image->sec[sec_idx].data + offset,
253                        block->fw_paging_size);
254                 dma_sync_single_for_device(fwrt->trans->dev,
255                                            block->fw_paging_phys,
256                                            block->fw_paging_size,
257                                            DMA_BIDIRECTIONAL);
258
259                 IWL_DEBUG_FW(fwrt,
260                              "Paging: copied %d paging bytes to block %d\n",
261                              block->fw_paging_size, idx);
262
263                 offset += block->fw_paging_size;
264
265                 if (offset > image->sec[sec_idx].len) {
266                         IWL_ERR(fwrt,
267                                 "Paging: offset goes over section size\n");
268                         ret = -EINVAL;
269                         goto err;
270                 }
271         }
272
273         /* copy the last paging block */
274         if (fwrt->num_of_pages_in_last_blk > 0) {
275                 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
276
277                 if (image->sec[sec_idx].len - offset > block->fw_paging_size) {
278                         IWL_ERR(fwrt,
279                                 "Paging: last block is larger than paging size\n");
280                         ret = -EINVAL;
281                         goto err;
282                 }
283
284                 memcpy(page_address(block->fw_paging_block),
285                        image->sec[sec_idx].data + offset,
286                        image->sec[sec_idx].len - offset);
287                 dma_sync_single_for_device(fwrt->trans->dev,
288                                            block->fw_paging_phys,
289                                            block->fw_paging_size,
290                                            DMA_BIDIRECTIONAL);
291
292                 IWL_DEBUG_FW(fwrt,
293                              "Paging: copied %d pages in the last block %d\n",
294                              fwrt->num_of_pages_in_last_blk, idx);
295         }
296
297         return 0;
298
299 err:
300         iwl_free_fw_paging(fwrt);
301         return ret;
302 }
303
304 static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
305                               const struct fw_img *fw)
306 {
307         int ret;
308
309         ret = iwl_alloc_fw_paging_mem(fwrt, fw);
310         if (ret)
311                 return ret;
312
313         return iwl_fill_paging_mem(fwrt, fw);
314 }
315
316 /* send paging cmd to FW in case CPU2 has paging image */
317 static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
318                                const struct fw_img *fw)
319 {
320         struct iwl_fw_paging_cmd paging_cmd = {
321                 .flags = cpu_to_le32(PAGING_CMD_IS_SECURED |
322                                      PAGING_CMD_IS_ENABLED |
323                                      (fwrt->num_of_pages_in_last_blk <<
324                                       PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
325                 .block_size = cpu_to_le32(BLOCK_2_EXP_SIZE),
326                 .block_num = cpu_to_le32(fwrt->num_of_paging_blk),
327         };
328         struct iwl_host_cmd hcmd = {
329                 .id = iwl_cmd_id(FW_PAGING_BLOCK_CMD, IWL_ALWAYS_LONG_GROUP, 0),
330                 .len = { sizeof(paging_cmd), },
331                 .data = { &paging_cmd, },
332         };
333         int blk_idx;
334
335         /* loop for for all paging blocks + CSS block */
336         for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
337                 dma_addr_t addr = fwrt->fw_paging_db[blk_idx].fw_paging_phys;
338                 __le32 phy_addr;
339
340                 addr = addr >> PAGE_2_EXP_SIZE;
341                 phy_addr = cpu_to_le32(addr);
342                 paging_cmd.device_phy_addr[blk_idx] = phy_addr;
343         }
344
345         return iwl_trans_send_cmd(fwrt->trans, &hcmd);
346 }
347
348 /*
349  * Send paging item cmd to FW in case CPU2 has paging image
350  */
351 static int iwl_trans_get_paging_item(struct iwl_fw_runtime *fwrt)
352 {
353         int ret;
354         struct iwl_fw_get_item_cmd fw_get_item_cmd = {
355                 .item_id = cpu_to_le32(IWL_FW_ITEM_ID_PAGING),
356         };
357         struct iwl_fw_get_item_resp *item_resp;
358         struct iwl_host_cmd cmd = {
359                 .id = iwl_cmd_id(FW_GET_ITEM_CMD, IWL_ALWAYS_LONG_GROUP, 0),
360                 .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
361                 .data = { &fw_get_item_cmd, },
362                 .len = { sizeof(fw_get_item_cmd), },
363         };
364
365         ret = iwl_trans_send_cmd(fwrt->trans, &cmd);
366         if (ret) {
367                 IWL_ERR(fwrt,
368                         "Paging: Failed to send FW_GET_ITEM_CMD cmd (err = %d)\n",
369                         ret);
370                 return ret;
371         }
372
373         item_resp = (void *)((struct iwl_rx_packet *)cmd.resp_pkt)->data;
374         if (item_resp->item_id != cpu_to_le32(IWL_FW_ITEM_ID_PAGING)) {
375                 IWL_ERR(fwrt,
376                         "Paging: got wrong item in FW_GET_ITEM_CMD resp (item_id = %u)\n",
377                         le32_to_cpu(item_resp->item_id));
378                 ret = -EIO;
379                 goto exit;
380         }
381
382         /* Add an extra page for headers */
383         fwrt->trans->paging_download_buf = kzalloc(PAGING_BLOCK_SIZE +
384                                                   FW_PAGING_SIZE,
385                                                   GFP_KERNEL);
386         if (!fwrt->trans->paging_download_buf) {
387                 ret = -ENOMEM;
388                 goto exit;
389         }
390         fwrt->trans->paging_req_addr = le32_to_cpu(item_resp->item_val);
391         fwrt->trans->paging_db = fwrt->fw_paging_db;
392         IWL_DEBUG_FW(fwrt,
393                      "Paging: got paging request address (paging_req_addr 0x%08x)\n",
394                      fwrt->trans->paging_req_addr);
395
396 exit:
397         iwl_free_resp(&cmd);
398
399         return ret;
400 }
401
402 int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
403 {
404         const struct fw_img *fw = &fwrt->fw->img[type];
405         int ret;
406
407         if (fwrt->trans->cfg->gen2)
408                 return 0;
409
410         /*
411          * Configure and operate fw paging mechanism.
412          * The driver configures the paging flow only once.
413          * The CPU2 paging image is included in the IWL_UCODE_INIT image.
414          */
415         if (!fw->paging_mem_size)
416                 return 0;
417
418         /*
419          * When dma is not enabled, the driver needs to copy / write
420          * the downloaded / uploaded page to / from the smem.
421          * This gets the location of the place were the pages are
422          * stored.
423          */
424         if (!is_device_dma_capable(fwrt->trans->dev)) {
425                 ret = iwl_trans_get_paging_item(fwrt);
426                 if (ret) {
427                         IWL_ERR(fwrt, "failed to get FW paging item\n");
428                         return ret;
429                 }
430         }
431
432         ret = iwl_save_fw_paging(fwrt, fw);
433         if (ret) {
434                 IWL_ERR(fwrt, "failed to save the FW paging image\n");
435                 return ret;
436         }
437
438         ret = iwl_send_paging_cmd(fwrt, fw);
439         if (ret) {
440                 IWL_ERR(fwrt, "failed to send the paging cmd\n");
441                 iwl_free_fw_paging(fwrt);
442                 return ret;
443         }
444
445         return 0;
446 }
447 IWL_EXPORT_SYMBOL(iwl_init_paging);