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.
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
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.
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.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
25 * Contact Information:
26 * Intel Linux Wireless <linuxwifi@intel.com>
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
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.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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
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.
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.
63 *****************************************************************************/
66 #include "fw/api/commands.h"
68 void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
72 if (!fwrt->fw_paging_db[0].fw_paging_block)
75 for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
76 struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
78 if (!paging->fw_paging_block) {
80 "Paging: block %d already freed, continue to next page\n",
85 dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
86 paging->fw_paging_size, DMA_BIDIRECTIONAL);
88 __free_pages(paging->fw_paging_block,
89 get_order(paging->fw_paging_size));
90 paging->fw_paging_block = NULL;
93 memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
95 IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
97 static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
98 const struct fw_img *image)
102 int blk_idx, order, num_of_pages, size;
104 if (fwrt->fw_paging_db[0].fw_paging_block)
107 /* ensure BLOCK_2_EXP_SIZE is power of 2 of PAGING_BLOCK_SIZE */
108 BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
110 num_of_pages = image->paging_mem_size / FW_PAGING_SIZE;
111 fwrt->num_of_paging_blk =
112 DIV_ROUND_UP(num_of_pages, NUM_OF_PAGE_PER_GROUP);
113 fwrt->num_of_pages_in_last_blk =
115 NUM_OF_PAGE_PER_GROUP * (fwrt->num_of_paging_blk - 1);
118 "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
119 fwrt->num_of_paging_blk,
120 fwrt->num_of_pages_in_last_blk);
123 * Allocate CSS and paging blocks in dram.
125 for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
126 /* For CSS allocate 4KB, for others PAGING_BLOCK_SIZE (32K) */
127 size = blk_idx ? PAGING_BLOCK_SIZE : FW_PAGING_SIZE;
128 order = get_order(size);
129 block = alloc_pages(GFP_KERNEL, order);
131 /* free all the previous pages since we failed */
132 iwl_free_fw_paging(fwrt);
136 fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
137 fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
139 phys = dma_map_page(fwrt->trans->dev, block, 0,
142 if (dma_mapping_error(fwrt->trans->dev, phys)) {
144 * free the previous pages and the current one
145 * since we failed to map_page.
147 iwl_free_fw_paging(fwrt);
150 fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
154 "Paging: allocated 4K(CSS) bytes (order %d) for firmware paging.\n",
158 "Paging: allocated 32K bytes (order %d) for firmware paging.\n",
165 static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
166 const struct fw_img *image)
168 int sec_idx, idx, ret;
172 * find where is the paging image start point:
173 * if CPU2 exist and it's in paging format, then the image looks like:
174 * CPU1 sections (2 or more)
175 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
176 * CPU2 sections (not paged)
177 * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
178 * non paged to CPU2 paging sec
180 * CPU2 paging image (including instruction and data)
182 for (sec_idx = 0; sec_idx < image->num_sec; sec_idx++) {
183 if (image->sec[sec_idx].offset == PAGING_SEPARATOR_SECTION) {
190 * If paging is enabled there should be at least 2 more sections left
191 * (one for CSS and one for Paging data)
193 if (sec_idx >= image->num_sec - 1) {
194 IWL_ERR(fwrt, "Paging: Missing CSS and/or paging sections\n");
199 /* copy the CSS block to the dram */
200 IWL_DEBUG_FW(fwrt, "Paging: load paging CSS to FW, sec = %d\n",
203 if (image->sec[sec_idx].len > fwrt->fw_paging_db[0].fw_paging_size) {
204 IWL_ERR(fwrt, "CSS block is larger than paging size\n");
209 memcpy(page_address(fwrt->fw_paging_db[0].fw_paging_block),
210 image->sec[sec_idx].data,
211 image->sec[sec_idx].len);
212 dma_sync_single_for_device(fwrt->trans->dev,
213 fwrt->fw_paging_db[0].fw_paging_phys,
214 fwrt->fw_paging_db[0].fw_paging_size,
218 "Paging: copied %d CSS bytes to first block\n",
219 fwrt->fw_paging_db[0].fw_paging_size);
224 * copy the paging blocks to the dram
225 * loop index start from 1 since that CSS block already copied to dram
226 * and CSS index is 0.
227 * loop stop at num_of_paging_blk since that last block is not full.
229 for (idx = 1; idx < fwrt->num_of_paging_blk; idx++) {
230 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
232 if (block->fw_paging_size > image->sec[sec_idx].len - offset) {
234 "Paging: paging size is larger than remaining data in block %d\n",
240 memcpy(page_address(block->fw_paging_block),
241 image->sec[sec_idx].data + offset,
242 block->fw_paging_size);
243 dma_sync_single_for_device(fwrt->trans->dev,
244 block->fw_paging_phys,
245 block->fw_paging_size,
249 "Paging: copied %d paging bytes to block %d\n",
250 block->fw_paging_size, idx);
252 offset += block->fw_paging_size;
254 if (offset > image->sec[sec_idx].len) {
256 "Paging: offset goes over section size\n");
262 /* copy the last paging block */
263 if (fwrt->num_of_pages_in_last_blk > 0) {
264 struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
266 if (image->sec[sec_idx].len - offset > block->fw_paging_size) {
268 "Paging: last block is larger than paging size\n");
273 memcpy(page_address(block->fw_paging_block),
274 image->sec[sec_idx].data + offset,
275 image->sec[sec_idx].len - offset);
276 dma_sync_single_for_device(fwrt->trans->dev,
277 block->fw_paging_phys,
278 block->fw_paging_size,
282 "Paging: copied %d pages in the last block %d\n",
283 fwrt->num_of_pages_in_last_blk, idx);
289 iwl_free_fw_paging(fwrt);
293 static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
294 const struct fw_img *fw)
298 ret = iwl_alloc_fw_paging_mem(fwrt, fw);
302 return iwl_fill_paging_mem(fwrt, fw);
305 /* send paging cmd to FW in case CPU2 has paging image */
306 static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
307 const struct fw_img *fw)
309 struct iwl_fw_paging_cmd paging_cmd = {
310 .flags = cpu_to_le32(PAGING_CMD_IS_SECURED |
311 PAGING_CMD_IS_ENABLED |
312 (fwrt->num_of_pages_in_last_blk <<
313 PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
314 .block_size = cpu_to_le32(BLOCK_2_EXP_SIZE),
315 .block_num = cpu_to_le32(fwrt->num_of_paging_blk),
317 struct iwl_host_cmd hcmd = {
318 .id = iwl_cmd_id(FW_PAGING_BLOCK_CMD, IWL_ALWAYS_LONG_GROUP, 0),
319 .len = { sizeof(paging_cmd), },
320 .data = { &paging_cmd, },
324 /* loop for for all paging blocks + CSS block */
325 for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
326 dma_addr_t addr = fwrt->fw_paging_db[blk_idx].fw_paging_phys;
329 addr = addr >> PAGE_2_EXP_SIZE;
330 phy_addr = cpu_to_le32(addr);
331 paging_cmd.device_phy_addr[blk_idx] = phy_addr;
334 return iwl_trans_send_cmd(fwrt->trans, &hcmd);
337 int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
339 const struct fw_img *fw = &fwrt->fw->img[type];
342 if (fwrt->trans->cfg->gen2)
346 * Configure and operate fw paging mechanism.
347 * The driver configures the paging flow only once.
348 * The CPU2 paging image is included in the IWL_UCODE_INIT image.
350 if (!fw->paging_mem_size)
353 ret = iwl_save_fw_paging(fwrt, fw);
355 IWL_ERR(fwrt, "failed to save the FW paging image\n");
359 ret = iwl_send_paging_cmd(fwrt, fw);
361 IWL_ERR(fwrt, "failed to send the paging cmd\n");
362 iwl_free_fw_paging(fwrt);
368 IWL_EXPORT_SYMBOL(iwl_init_paging);