18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3fdb70083SJohannes Berg * Copyright (C) 2012-2014, 2018-2019, 2021 Intel Corporation
48e99ea8dSJohannes Berg * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg */
7235acb18SJohannes Berg #include "iwl-drv.h"
8235acb18SJohannes Berg #include "runtime.h"
9d172a5efSJohannes Berg #include "fw/api/commands.h"
10235acb18SJohannes Berg
iwl_free_fw_paging(struct iwl_fw_runtime * fwrt)11235acb18SJohannes Berg void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
12235acb18SJohannes Berg {
13235acb18SJohannes Berg int i;
14235acb18SJohannes Berg
15235acb18SJohannes Berg if (!fwrt->fw_paging_db[0].fw_paging_block)
16235acb18SJohannes Berg return;
17235acb18SJohannes Berg
18235acb18SJohannes Berg for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
19235acb18SJohannes Berg struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
20235acb18SJohannes Berg
21235acb18SJohannes Berg if (!paging->fw_paging_block) {
22235acb18SJohannes Berg IWL_DEBUG_FW(fwrt,
23235acb18SJohannes Berg "Paging: block %d already freed, continue to next page\n",
24235acb18SJohannes Berg i);
25235acb18SJohannes Berg
26235acb18SJohannes Berg continue;
27235acb18SJohannes Berg }
28235acb18SJohannes Berg dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
29235acb18SJohannes Berg paging->fw_paging_size, DMA_BIDIRECTIONAL);
30235acb18SJohannes Berg
31235acb18SJohannes Berg __free_pages(paging->fw_paging_block,
32235acb18SJohannes Berg get_order(paging->fw_paging_size));
33235acb18SJohannes Berg paging->fw_paging_block = NULL;
34235acb18SJohannes Berg }
35235acb18SJohannes Berg
36235acb18SJohannes Berg memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
37235acb18SJohannes Berg }
38235acb18SJohannes Berg IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
39235acb18SJohannes Berg
iwl_alloc_fw_paging_mem(struct iwl_fw_runtime * fwrt,const struct fw_img * image)40235acb18SJohannes Berg static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
41235acb18SJohannes Berg const struct fw_img *image)
42235acb18SJohannes Berg {
43235acb18SJohannes Berg struct page *block;
44235acb18SJohannes Berg dma_addr_t phys = 0;
454ae827ffSEmmanuel Grumbach int blk_idx, order, num_of_pages, size;
46235acb18SJohannes Berg
47235acb18SJohannes Berg if (fwrt->fw_paging_db[0].fw_paging_block)
48235acb18SJohannes Berg return 0;
49235acb18SJohannes Berg
50235acb18SJohannes Berg /* ensure BLOCK_2_EXP_SIZE is power of 2 of PAGING_BLOCK_SIZE */
51235acb18SJohannes Berg BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
52235acb18SJohannes Berg
53235acb18SJohannes Berg num_of_pages = image->paging_mem_size / FW_PAGING_SIZE;
54235acb18SJohannes Berg fwrt->num_of_paging_blk =
55235acb18SJohannes Berg DIV_ROUND_UP(num_of_pages, NUM_OF_PAGE_PER_GROUP);
56235acb18SJohannes Berg fwrt->num_of_pages_in_last_blk =
57235acb18SJohannes Berg num_of_pages -
58235acb18SJohannes Berg NUM_OF_PAGE_PER_GROUP * (fwrt->num_of_paging_blk - 1);
59235acb18SJohannes Berg
60235acb18SJohannes Berg IWL_DEBUG_FW(fwrt,
61235acb18SJohannes Berg "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
62235acb18SJohannes Berg fwrt->num_of_paging_blk,
63235acb18SJohannes Berg fwrt->num_of_pages_in_last_blk);
64235acb18SJohannes Berg
65235acb18SJohannes Berg /*
66235acb18SJohannes Berg * Allocate CSS and paging blocks in dram.
67235acb18SJohannes Berg */
68235acb18SJohannes Berg for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
69235acb18SJohannes Berg /* For CSS allocate 4KB, for others PAGING_BLOCK_SIZE (32K) */
70235acb18SJohannes Berg size = blk_idx ? PAGING_BLOCK_SIZE : FW_PAGING_SIZE;
71235acb18SJohannes Berg order = get_order(size);
72235acb18SJohannes Berg block = alloc_pages(GFP_KERNEL, order);
73235acb18SJohannes Berg if (!block) {
74235acb18SJohannes Berg /* free all the previous pages since we failed */
75235acb18SJohannes Berg iwl_free_fw_paging(fwrt);
76235acb18SJohannes Berg return -ENOMEM;
77235acb18SJohannes Berg }
78235acb18SJohannes Berg
79235acb18SJohannes Berg fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
80235acb18SJohannes Berg fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
81235acb18SJohannes Berg
82235acb18SJohannes Berg phys = dma_map_page(fwrt->trans->dev, block, 0,
83235acb18SJohannes Berg PAGE_SIZE << order,
84235acb18SJohannes Berg DMA_BIDIRECTIONAL);
85235acb18SJohannes Berg if (dma_mapping_error(fwrt->trans->dev, phys)) {
86235acb18SJohannes Berg /*
87235acb18SJohannes Berg * free the previous pages and the current one
88235acb18SJohannes Berg * since we failed to map_page.
89235acb18SJohannes Berg */
90235acb18SJohannes Berg iwl_free_fw_paging(fwrt);
91235acb18SJohannes Berg return -ENOMEM;
92235acb18SJohannes Berg }
93235acb18SJohannes Berg fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
94235acb18SJohannes Berg
95235acb18SJohannes Berg if (!blk_idx)
96235acb18SJohannes Berg IWL_DEBUG_FW(fwrt,
97235acb18SJohannes Berg "Paging: allocated 4K(CSS) bytes (order %d) for firmware paging.\n",
98235acb18SJohannes Berg order);
99235acb18SJohannes Berg else
100235acb18SJohannes Berg IWL_DEBUG_FW(fwrt,
101235acb18SJohannes Berg "Paging: allocated 32K bytes (order %d) for firmware paging.\n",
102235acb18SJohannes Berg order);
103235acb18SJohannes Berg }
104235acb18SJohannes Berg
105235acb18SJohannes Berg return 0;
106235acb18SJohannes Berg }
107235acb18SJohannes Berg
iwl_fill_paging_mem(struct iwl_fw_runtime * fwrt,const struct fw_img * image)108235acb18SJohannes Berg static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
109235acb18SJohannes Berg const struct fw_img *image)
110235acb18SJohannes Berg {
1119039d985SLuca Coelho int sec_idx, idx, ret;
112235acb18SJohannes Berg u32 offset = 0;
113235acb18SJohannes Berg
114235acb18SJohannes Berg /*
115235acb18SJohannes Berg * find where is the paging image start point:
116235acb18SJohannes Berg * if CPU2 exist and it's in paging format, then the image looks like:
117235acb18SJohannes Berg * CPU1 sections (2 or more)
118235acb18SJohannes Berg * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
119235acb18SJohannes Berg * CPU2 sections (not paged)
120235acb18SJohannes Berg * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
121235acb18SJohannes Berg * non paged to CPU2 paging sec
122235acb18SJohannes Berg * CPU2 paging CSS
123235acb18SJohannes Berg * CPU2 paging image (including instruction and data)
124235acb18SJohannes Berg */
125235acb18SJohannes Berg for (sec_idx = 0; sec_idx < image->num_sec; sec_idx++) {
126235acb18SJohannes Berg if (image->sec[sec_idx].offset == PAGING_SEPARATOR_SECTION) {
127235acb18SJohannes Berg sec_idx++;
128235acb18SJohannes Berg break;
129235acb18SJohannes Berg }
130235acb18SJohannes Berg }
131235acb18SJohannes Berg
132235acb18SJohannes Berg /*
133235acb18SJohannes Berg * If paging is enabled there should be at least 2 more sections left
134235acb18SJohannes Berg * (one for CSS and one for Paging data)
135235acb18SJohannes Berg */
136235acb18SJohannes Berg if (sec_idx >= image->num_sec - 1) {
137235acb18SJohannes Berg IWL_ERR(fwrt, "Paging: Missing CSS and/or paging sections\n");
1389039d985SLuca Coelho ret = -EINVAL;
1399039d985SLuca Coelho goto err;
140235acb18SJohannes Berg }
141235acb18SJohannes Berg
142235acb18SJohannes Berg /* copy the CSS block to the dram */
143235acb18SJohannes Berg IWL_DEBUG_FW(fwrt, "Paging: load paging CSS to FW, sec = %d\n",
144235acb18SJohannes Berg sec_idx);
145235acb18SJohannes Berg
1469039d985SLuca Coelho if (image->sec[sec_idx].len > fwrt->fw_paging_db[0].fw_paging_size) {
1479039d985SLuca Coelho IWL_ERR(fwrt, "CSS block is larger than paging size\n");
1489039d985SLuca Coelho ret = -EINVAL;
1499039d985SLuca Coelho goto err;
1509039d985SLuca Coelho }
1519039d985SLuca Coelho
152235acb18SJohannes Berg memcpy(page_address(fwrt->fw_paging_db[0].fw_paging_block),
153235acb18SJohannes Berg image->sec[sec_idx].data,
1549039d985SLuca Coelho image->sec[sec_idx].len);
155fdb70083SJohannes Berg fwrt->fw_paging_db[0].fw_offs = image->sec[sec_idx].offset;
156235acb18SJohannes Berg dma_sync_single_for_device(fwrt->trans->dev,
157235acb18SJohannes Berg fwrt->fw_paging_db[0].fw_paging_phys,
158235acb18SJohannes Berg fwrt->fw_paging_db[0].fw_paging_size,
159235acb18SJohannes Berg DMA_BIDIRECTIONAL);
160235acb18SJohannes Berg
161235acb18SJohannes Berg IWL_DEBUG_FW(fwrt,
162235acb18SJohannes Berg "Paging: copied %d CSS bytes to first block\n",
163235acb18SJohannes Berg fwrt->fw_paging_db[0].fw_paging_size);
164235acb18SJohannes Berg
165235acb18SJohannes Berg sec_idx++;
166235acb18SJohannes Berg
167235acb18SJohannes Berg /*
168de460dddSLuca Coelho * Copy the paging blocks to the dram. The loop index starts
169de460dddSLuca Coelho * from 1 since the CSS block (index 0) was already copied to
170de460dddSLuca Coelho * dram. We use num_of_paging_blk + 1 to account for that.
171235acb18SJohannes Berg */
172de460dddSLuca Coelho for (idx = 1; idx < fwrt->num_of_paging_blk + 1; idx++) {
173235acb18SJohannes Berg struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
174de460dddSLuca Coelho int remaining = image->sec[sec_idx].len - offset;
175de460dddSLuca Coelho int len = block->fw_paging_size;
176235acb18SJohannes Berg
177de460dddSLuca Coelho /*
178de460dddSLuca Coelho * For the last block, we copy all that is remaining,
179de460dddSLuca Coelho * for all other blocks, we copy fw_paging_size at a
180de460dddSLuca Coelho * time. */
181de460dddSLuca Coelho if (idx == fwrt->num_of_paging_blk) {
182de460dddSLuca Coelho len = remaining;
183de460dddSLuca Coelho if (remaining !=
184de460dddSLuca Coelho fwrt->num_of_pages_in_last_blk * FW_PAGING_SIZE) {
1859039d985SLuca Coelho IWL_ERR(fwrt,
186de460dddSLuca Coelho "Paging: last block contains more data than expected %d\n",
187de460dddSLuca Coelho remaining);
188de460dddSLuca Coelho ret = -EINVAL;
189de460dddSLuca Coelho goto err;
190de460dddSLuca Coelho }
191de460dddSLuca Coelho } else if (block->fw_paging_size > remaining) {
192de460dddSLuca Coelho IWL_ERR(fwrt,
193de460dddSLuca Coelho "Paging: not enough data in other in block %d (%d)\n",
194de460dddSLuca Coelho idx, remaining);
1959039d985SLuca Coelho ret = -EINVAL;
1969039d985SLuca Coelho goto err;
1979039d985SLuca Coelho }
1989039d985SLuca Coelho
199235acb18SJohannes Berg memcpy(page_address(block->fw_paging_block),
200*3827cb59SJohannes Berg (const u8 *)image->sec[sec_idx].data + offset, len);
201fdb70083SJohannes Berg block->fw_offs = image->sec[sec_idx].offset + offset;
202235acb18SJohannes Berg dma_sync_single_for_device(fwrt->trans->dev,
203235acb18SJohannes Berg block->fw_paging_phys,
204235acb18SJohannes Berg block->fw_paging_size,
205235acb18SJohannes Berg DMA_BIDIRECTIONAL);
206235acb18SJohannes Berg
207235acb18SJohannes Berg IWL_DEBUG_FW(fwrt,
208235acb18SJohannes Berg "Paging: copied %d paging bytes to block %d\n",
209de460dddSLuca Coelho len, idx);
210235acb18SJohannes Berg
2119039d985SLuca Coelho offset += block->fw_paging_size;
212235acb18SJohannes Berg }
213235acb18SJohannes Berg
214235acb18SJohannes Berg return 0;
2159039d985SLuca Coelho
2169039d985SLuca Coelho err:
2179039d985SLuca Coelho iwl_free_fw_paging(fwrt);
2189039d985SLuca Coelho return ret;
219235acb18SJohannes Berg }
220235acb18SJohannes Berg
iwl_save_fw_paging(struct iwl_fw_runtime * fwrt,const struct fw_img * fw)221235acb18SJohannes Berg static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
222235acb18SJohannes Berg const struct fw_img *fw)
223235acb18SJohannes Berg {
224235acb18SJohannes Berg int ret;
225235acb18SJohannes Berg
226235acb18SJohannes Berg ret = iwl_alloc_fw_paging_mem(fwrt, fw);
227235acb18SJohannes Berg if (ret)
228235acb18SJohannes Berg return ret;
229235acb18SJohannes Berg
230235acb18SJohannes Berg return iwl_fill_paging_mem(fwrt, fw);
231235acb18SJohannes Berg }
232235acb18SJohannes Berg
233235acb18SJohannes Berg /* send paging cmd to FW in case CPU2 has paging image */
iwl_send_paging_cmd(struct iwl_fw_runtime * fwrt,const struct fw_img * fw)234235acb18SJohannes Berg static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
235235acb18SJohannes Berg const struct fw_img *fw)
236235acb18SJohannes Berg {
237235acb18SJohannes Berg struct iwl_fw_paging_cmd paging_cmd = {
238235acb18SJohannes Berg .flags = cpu_to_le32(PAGING_CMD_IS_SECURED |
239235acb18SJohannes Berg PAGING_CMD_IS_ENABLED |
240235acb18SJohannes Berg (fwrt->num_of_pages_in_last_blk <<
241235acb18SJohannes Berg PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
242235acb18SJohannes Berg .block_size = cpu_to_le32(BLOCK_2_EXP_SIZE),
243235acb18SJohannes Berg .block_num = cpu_to_le32(fwrt->num_of_paging_blk),
244235acb18SJohannes Berg };
245235acb18SJohannes Berg struct iwl_host_cmd hcmd = {
246f0c86427SJohannes Berg .id = WIDE_ID(IWL_ALWAYS_LONG_GROUP, FW_PAGING_BLOCK_CMD),
247235acb18SJohannes Berg .len = { sizeof(paging_cmd), },
248235acb18SJohannes Berg .data = { &paging_cmd, },
249235acb18SJohannes Berg };
250235acb18SJohannes Berg int blk_idx;
251235acb18SJohannes Berg
252235acb18SJohannes Berg /* loop for for all paging blocks + CSS block */
253235acb18SJohannes Berg for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
254235acb18SJohannes Berg dma_addr_t addr = fwrt->fw_paging_db[blk_idx].fw_paging_phys;
255235acb18SJohannes Berg __le32 phy_addr;
256235acb18SJohannes Berg
257235acb18SJohannes Berg addr = addr >> PAGE_2_EXP_SIZE;
258235acb18SJohannes Berg phy_addr = cpu_to_le32(addr);
259235acb18SJohannes Berg paging_cmd.device_phy_addr[blk_idx] = phy_addr;
260235acb18SJohannes Berg }
261235acb18SJohannes Berg
262235acb18SJohannes Berg return iwl_trans_send_cmd(fwrt->trans, &hcmd);
263235acb18SJohannes Berg }
264235acb18SJohannes Berg
iwl_init_paging(struct iwl_fw_runtime * fwrt,enum iwl_ucode_type type)265235acb18SJohannes Berg int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
266235acb18SJohannes Berg {
267235acb18SJohannes Berg const struct fw_img *fw = &fwrt->fw->img[type];
268235acb18SJohannes Berg int ret;
269235acb18SJohannes Berg
270286ca8ebSLuca Coelho if (fwrt->trans->trans_cfg->gen2)
271235acb18SJohannes Berg return 0;
272235acb18SJohannes Berg
273235acb18SJohannes Berg /*
274235acb18SJohannes Berg * Configure and operate fw paging mechanism.
275235acb18SJohannes Berg * The driver configures the paging flow only once.
276235acb18SJohannes Berg * The CPU2 paging image is included in the IWL_UCODE_INIT image.
277235acb18SJohannes Berg */
278235acb18SJohannes Berg if (!fw->paging_mem_size)
279235acb18SJohannes Berg return 0;
280235acb18SJohannes Berg
281235acb18SJohannes Berg ret = iwl_save_fw_paging(fwrt, fw);
282235acb18SJohannes Berg if (ret) {
283235acb18SJohannes Berg IWL_ERR(fwrt, "failed to save the FW paging image\n");
284235acb18SJohannes Berg return ret;
285235acb18SJohannes Berg }
286235acb18SJohannes Berg
287235acb18SJohannes Berg ret = iwl_send_paging_cmd(fwrt, fw);
288235acb18SJohannes Berg if (ret) {
289235acb18SJohannes Berg IWL_ERR(fwrt, "failed to send the paging cmd\n");
290235acb18SJohannes Berg iwl_free_fw_paging(fwrt);
291235acb18SJohannes Berg return ret;
292235acb18SJohannes Berg }
293235acb18SJohannes Berg
294235acb18SJohannes Berg return 0;
295235acb18SJohannes Berg }
296235acb18SJohannes Berg IWL_EXPORT_SYMBOL(iwl_init_paging);
297