Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
0002 /*
0003  * Copyright (C) 2012-2014, 2018-2019, 2021 Intel Corporation
0004  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
0005  * Copyright (C) 2016-2017 Intel Deutschland GmbH
0006  */
0007 #include "iwl-drv.h"
0008 #include "runtime.h"
0009 #include "fw/api/commands.h"
0010 
0011 void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
0012 {
0013     int i;
0014 
0015     if (!fwrt->fw_paging_db[0].fw_paging_block)
0016         return;
0017 
0018     for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
0019         struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
0020 
0021         if (!paging->fw_paging_block) {
0022             IWL_DEBUG_FW(fwrt,
0023                      "Paging: block %d already freed, continue to next page\n",
0024                      i);
0025 
0026             continue;
0027         }
0028         dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
0029                    paging->fw_paging_size, DMA_BIDIRECTIONAL);
0030 
0031         __free_pages(paging->fw_paging_block,
0032                  get_order(paging->fw_paging_size));
0033         paging->fw_paging_block = NULL;
0034     }
0035 
0036     memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
0037 }
0038 IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
0039 
0040 static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
0041                    const struct fw_img *image)
0042 {
0043     struct page *block;
0044     dma_addr_t phys = 0;
0045     int blk_idx, order, num_of_pages, size;
0046 
0047     if (fwrt->fw_paging_db[0].fw_paging_block)
0048         return 0;
0049 
0050     /* ensure BLOCK_2_EXP_SIZE is power of 2 of PAGING_BLOCK_SIZE */
0051     BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
0052 
0053     num_of_pages = image->paging_mem_size / FW_PAGING_SIZE;
0054     fwrt->num_of_paging_blk =
0055         DIV_ROUND_UP(num_of_pages, NUM_OF_PAGE_PER_GROUP);
0056     fwrt->num_of_pages_in_last_blk =
0057         num_of_pages -
0058         NUM_OF_PAGE_PER_GROUP * (fwrt->num_of_paging_blk - 1);
0059 
0060     IWL_DEBUG_FW(fwrt,
0061              "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
0062              fwrt->num_of_paging_blk,
0063              fwrt->num_of_pages_in_last_blk);
0064 
0065     /*
0066      * Allocate CSS and paging blocks in dram.
0067      */
0068     for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
0069         /* For CSS allocate 4KB, for others PAGING_BLOCK_SIZE (32K) */
0070         size = blk_idx ? PAGING_BLOCK_SIZE : FW_PAGING_SIZE;
0071         order = get_order(size);
0072         block = alloc_pages(GFP_KERNEL, order);
0073         if (!block) {
0074             /* free all the previous pages since we failed */
0075             iwl_free_fw_paging(fwrt);
0076             return -ENOMEM;
0077         }
0078 
0079         fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
0080         fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
0081 
0082         phys = dma_map_page(fwrt->trans->dev, block, 0,
0083                     PAGE_SIZE << order,
0084                     DMA_BIDIRECTIONAL);
0085         if (dma_mapping_error(fwrt->trans->dev, phys)) {
0086             /*
0087              * free the previous pages and the current one
0088              * since we failed to map_page.
0089              */
0090             iwl_free_fw_paging(fwrt);
0091             return -ENOMEM;
0092         }
0093         fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
0094 
0095         if (!blk_idx)
0096             IWL_DEBUG_FW(fwrt,
0097                      "Paging: allocated 4K(CSS) bytes (order %d) for firmware paging.\n",
0098                      order);
0099         else
0100             IWL_DEBUG_FW(fwrt,
0101                      "Paging: allocated 32K bytes (order %d) for firmware paging.\n",
0102                      order);
0103     }
0104 
0105     return 0;
0106 }
0107 
0108 static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
0109                    const struct fw_img *image)
0110 {
0111     int sec_idx, idx, ret;
0112     u32 offset = 0;
0113 
0114     /*
0115      * find where is the paging image start point:
0116      * if CPU2 exist and it's in paging format, then the image looks like:
0117      * CPU1 sections (2 or more)
0118      * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
0119      * CPU2 sections (not paged)
0120      * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
0121      * non paged to CPU2 paging sec
0122      * CPU2 paging CSS
0123      * CPU2 paging image (including instruction and data)
0124      */
0125     for (sec_idx = 0; sec_idx < image->num_sec; sec_idx++) {
0126         if (image->sec[sec_idx].offset == PAGING_SEPARATOR_SECTION) {
0127             sec_idx++;
0128             break;
0129         }
0130     }
0131 
0132     /*
0133      * If paging is enabled there should be at least 2 more sections left
0134      * (one for CSS and one for Paging data)
0135      */
0136     if (sec_idx >= image->num_sec - 1) {
0137         IWL_ERR(fwrt, "Paging: Missing CSS and/or paging sections\n");
0138         ret = -EINVAL;
0139         goto err;
0140     }
0141 
0142     /* copy the CSS block to the dram */
0143     IWL_DEBUG_FW(fwrt, "Paging: load paging CSS to FW, sec = %d\n",
0144              sec_idx);
0145 
0146     if (image->sec[sec_idx].len > fwrt->fw_paging_db[0].fw_paging_size) {
0147         IWL_ERR(fwrt, "CSS block is larger than paging size\n");
0148         ret = -EINVAL;
0149         goto err;
0150     }
0151 
0152     memcpy(page_address(fwrt->fw_paging_db[0].fw_paging_block),
0153            image->sec[sec_idx].data,
0154            image->sec[sec_idx].len);
0155     fwrt->fw_paging_db[0].fw_offs = image->sec[sec_idx].offset;
0156     dma_sync_single_for_device(fwrt->trans->dev,
0157                    fwrt->fw_paging_db[0].fw_paging_phys,
0158                    fwrt->fw_paging_db[0].fw_paging_size,
0159                    DMA_BIDIRECTIONAL);
0160 
0161     IWL_DEBUG_FW(fwrt,
0162              "Paging: copied %d CSS bytes to first block\n",
0163              fwrt->fw_paging_db[0].fw_paging_size);
0164 
0165     sec_idx++;
0166 
0167     /*
0168      * Copy the paging blocks to the dram.  The loop index starts
0169      * from 1 since the CSS block (index 0) was already copied to
0170      * dram.  We use num_of_paging_blk + 1 to account for that.
0171      */
0172     for (idx = 1; idx < fwrt->num_of_paging_blk + 1; idx++) {
0173         struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
0174         int remaining = image->sec[sec_idx].len - offset;
0175         int len = block->fw_paging_size;
0176 
0177         /*
0178          * For the last block, we copy all that is remaining,
0179          * for all other blocks, we copy fw_paging_size at a
0180          * time. */
0181         if (idx == fwrt->num_of_paging_blk) {
0182             len = remaining;
0183             if (remaining !=
0184                 fwrt->num_of_pages_in_last_blk * FW_PAGING_SIZE) {
0185                 IWL_ERR(fwrt,
0186                     "Paging: last block contains more data than expected %d\n",
0187                     remaining);
0188                 ret = -EINVAL;
0189                 goto err;
0190             }
0191         } else if (block->fw_paging_size > remaining) {
0192             IWL_ERR(fwrt,
0193                 "Paging: not enough data in other in block %d (%d)\n",
0194                 idx, remaining);
0195             ret = -EINVAL;
0196             goto err;
0197         }
0198 
0199         memcpy(page_address(block->fw_paging_block),
0200                (const u8 *)image->sec[sec_idx].data + offset, len);
0201         block->fw_offs = image->sec[sec_idx].offset + offset;
0202         dma_sync_single_for_device(fwrt->trans->dev,
0203                        block->fw_paging_phys,
0204                        block->fw_paging_size,
0205                        DMA_BIDIRECTIONAL);
0206 
0207         IWL_DEBUG_FW(fwrt,
0208                  "Paging: copied %d paging bytes to block %d\n",
0209                  len, idx);
0210 
0211         offset += block->fw_paging_size;
0212     }
0213 
0214     return 0;
0215 
0216 err:
0217     iwl_free_fw_paging(fwrt);
0218     return ret;
0219 }
0220 
0221 static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
0222                   const struct fw_img *fw)
0223 {
0224     int ret;
0225 
0226     ret = iwl_alloc_fw_paging_mem(fwrt, fw);
0227     if (ret)
0228         return ret;
0229 
0230     return iwl_fill_paging_mem(fwrt, fw);
0231 }
0232 
0233 /* send paging cmd to FW in case CPU2 has paging image */
0234 static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
0235                    const struct fw_img *fw)
0236 {
0237     struct iwl_fw_paging_cmd paging_cmd = {
0238         .flags = cpu_to_le32(PAGING_CMD_IS_SECURED |
0239                      PAGING_CMD_IS_ENABLED |
0240                      (fwrt->num_of_pages_in_last_blk <<
0241                       PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
0242         .block_size = cpu_to_le32(BLOCK_2_EXP_SIZE),
0243         .block_num = cpu_to_le32(fwrt->num_of_paging_blk),
0244     };
0245     struct iwl_host_cmd hcmd = {
0246         .id = WIDE_ID(IWL_ALWAYS_LONG_GROUP, FW_PAGING_BLOCK_CMD),
0247         .len = { sizeof(paging_cmd), },
0248         .data = { &paging_cmd, },
0249     };
0250     int blk_idx;
0251 
0252     /* loop for for all paging blocks + CSS block */
0253     for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
0254         dma_addr_t addr = fwrt->fw_paging_db[blk_idx].fw_paging_phys;
0255         __le32 phy_addr;
0256 
0257         addr = addr >> PAGE_2_EXP_SIZE;
0258         phy_addr = cpu_to_le32(addr);
0259         paging_cmd.device_phy_addr[blk_idx] = phy_addr;
0260     }
0261 
0262     return iwl_trans_send_cmd(fwrt->trans, &hcmd);
0263 }
0264 
0265 int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
0266 {
0267     const struct fw_img *fw = &fwrt->fw->img[type];
0268     int ret;
0269 
0270     if (fwrt->trans->trans_cfg->gen2)
0271         return 0;
0272 
0273     /*
0274      * Configure and operate fw paging mechanism.
0275      * The driver configures the paging flow only once.
0276      * The CPU2 paging image is included in the IWL_UCODE_INIT image.
0277      */
0278     if (!fw->paging_mem_size)
0279         return 0;
0280 
0281     ret = iwl_save_fw_paging(fwrt, fw);
0282     if (ret) {
0283         IWL_ERR(fwrt, "failed to save the FW paging image\n");
0284         return ret;
0285     }
0286 
0287     ret = iwl_send_paging_cmd(fwrt, fw);
0288     if (ret) {
0289         IWL_ERR(fwrt, "failed to send the paging cmd\n");
0290         iwl_free_fw_paging(fwrt);
0291         return ret;
0292     }
0293 
0294     return 0;
0295 }
0296 IWL_EXPORT_SYMBOL(iwl_init_paging);