Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / wireless / intel / iwlwifi / fw / paging.c
blob1fec8e3a6b35cea23eeccf7f1c1c9b9b1f4bcdba
1 /******************************************************************************
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.
6 * GPL LICENSE SUMMARY
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
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * The full GNU General Public License is included in this distribution
22 * in the file called COPYING.
24 * Contact Information:
25 * Intel Linux Wireless <linuxwifi@intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 * BSD LICENSE
30 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
31 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
32 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
39 * * Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * * Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
44 * distribution.
45 * * Neither the name Intel Corporation nor the names of its
46 * contributors may be used to endorse or promote products derived
47 * from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *****************************************************************************/
62 #include "iwl-drv.h"
63 #include "runtime.h"
64 #include "fw/api/commands.h"
66 void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
68 int i;
70 if (!fwrt->fw_paging_db[0].fw_paging_block)
71 return;
73 for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
74 struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
76 if (!paging->fw_paging_block) {
77 IWL_DEBUG_FW(fwrt,
78 "Paging: block %d already freed, continue to next page\n",
79 i);
81 continue;
83 dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
84 paging->fw_paging_size, DMA_BIDIRECTIONAL);
86 __free_pages(paging->fw_paging_block,
87 get_order(paging->fw_paging_size));
88 paging->fw_paging_block = NULL;
91 memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
93 IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
95 static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
96 const struct fw_img *image)
98 struct page *block;
99 dma_addr_t phys = 0;
100 int blk_idx, order, num_of_pages, size;
102 if (fwrt->fw_paging_db[0].fw_paging_block)
103 return 0;
105 /* ensure BLOCK_2_EXP_SIZE is power of 2 of PAGING_BLOCK_SIZE */
106 BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
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);
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);
121 * Allocate CSS and paging blocks in dram.
123 for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
124 /* For CSS allocate 4KB, for others PAGING_BLOCK_SIZE (32K) */
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 /* free all the previous pages since we failed */
130 iwl_free_fw_paging(fwrt);
131 return -ENOMEM;
134 fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
135 fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
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)) {
142 * free the previous pages and the current one
143 * since we failed to map_page.
145 iwl_free_fw_paging(fwrt);
146 return -ENOMEM;
148 fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
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);
160 return 0;
163 static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
164 const struct fw_img *image)
166 int sec_idx, idx;
167 u32 offset = 0;
170 * find where is the paging image start point:
171 * if CPU2 exist and it's in paging format, then the image looks like:
172 * CPU1 sections (2 or more)
173 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
174 * CPU2 sections (not paged)
175 * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
176 * non paged to CPU2 paging sec
177 * CPU2 paging CSS
178 * CPU2 paging image (including instruction and data)
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;
188 * If paging is enabled there should be at least 2 more sections left
189 * (one for CSS and one for Paging data)
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;
197 /* copy the CSS block to the dram */
198 IWL_DEBUG_FW(fwrt, "Paging: load paging CSS to FW, sec = %d\n",
199 sec_idx);
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);
209 IWL_DEBUG_FW(fwrt,
210 "Paging: copied %d CSS bytes to first block\n",
211 fwrt->fw_paging_db[0].fw_paging_size);
213 sec_idx++;
216 * copy the paging blocks to the dram
217 * loop index start from 1 since that CSS block already copied to dram
218 * and CSS index is 0.
219 * loop stop at num_of_paging_blk since that last block is not full.
221 for (idx = 1; idx < fwrt->num_of_paging_blk; idx++) {
222 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
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);
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);
237 offset += fwrt->fw_paging_db[idx].fw_paging_size;
240 /* copy the last paging block */
241 if (fwrt->num_of_pages_in_last_blk > 0) {
242 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
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);
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);
257 return 0;
260 static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
261 const struct fw_img *fw)
263 int ret;
265 ret = iwl_alloc_fw_paging_mem(fwrt, fw);
266 if (ret)
267 return ret;
269 return iwl_fill_paging_mem(fwrt, fw);
272 /* send paging cmd to FW in case CPU2 has paging image */
273 static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
274 const struct fw_img *fw)
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),
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, },
289 int blk_idx;
291 /* loop for for all paging blocks + CSS block */
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;
296 addr = addr >> PAGE_2_EXP_SIZE;
297 phy_addr = cpu_to_le32(addr);
298 paging_cmd.device_phy_addr[blk_idx] = phy_addr;
301 return iwl_trans_send_cmd(fwrt->trans, &hcmd);
304 int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
306 const struct fw_img *fw = &fwrt->fw->img[type];
307 int ret;
309 if (fwrt->trans->cfg->gen2)
310 return 0;
313 * Configure and operate fw paging mechanism.
314 * The driver configures the paging flow only once.
315 * The CPU2 paging image is included in the IWL_UCODE_INIT image.
317 if (!fw->paging_mem_size)
318 return 0;
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;
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;
333 return 0;
335 IWL_EXPORT_SYMBOL(iwl_init_paging);