linux/drivers/net/wireless/intel/iwlwifi/fw/paging.c
<<
>>
Prefs
   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 *
  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.
  15 *
  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.
  20 *
  21 * The full GNU General Public License is included in this distribution
  22 * in the file called COPYING.
  23 *
  24 * Contact Information:
  25 *  Intel Linux Wireless <linuxwifi@intel.com>
  26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27 *
  28 * BSD LICENSE
  29 *
  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.
  34 *
  35 * Redistribution and use in source and binary forms, with or without
  36 * modification, are permitted provided that the following conditions
  37 * are met:
  38 *
  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.
  48 *
  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.
  60 *
  61 *****************************************************************************/
  62#include "iwl-drv.h"
  63#include "runtime.h"
  64#include "fw/api/commands.h"
  65
  66void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
  67{
  68        int i;
  69
  70        if (!fwrt->fw_paging_db[0].fw_paging_block)
  71                return;
  72
  73        for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
  74                struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
  75
  76                if (!paging->fw_paging_block) {
  77                        IWL_DEBUG_FW(fwrt,
  78                                     "Paging: block %d already freed, continue to next page\n",
  79                                     i);
  80
  81                        continue;
  82                }
  83                dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
  84                               paging->fw_paging_size, DMA_BIDIRECTIONAL);
  85
  86                __free_pages(paging->fw_paging_block,
  87                             get_order(paging->fw_paging_size));
  88                paging->fw_paging_block = NULL;
  89        }
  90
  91        memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
  92}
  93IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
  94
  95static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
  96                                   const struct fw_img *image)
  97{
  98        struct page *block;
  99        dma_addr_t phys = 0;
 100        int blk_idx, order, num_of_pages, size;
 101
 102        if (fwrt->fw_paging_db[0].fw_paging_block)
 103                return 0;
 104
 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);
 107
 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);
 114
 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);
 119
 120        /*
 121         * Allocate CSS and paging blocks in dram.
 122         */
 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;
 132                }
 133
 134                fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
 135                fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
 136
 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)) {
 141                        /*
 142                         * free the previous pages and the current one
 143                         * since we failed to map_page.
 144                         */
 145                        iwl_free_fw_paging(fwrt);
 146                        return -ENOMEM;
 147                }
 148                fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
 149
 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);
 158        }
 159
 160        return 0;
 161}
 162
 163static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
 164                               const struct fw_img *image)
 165{
 166        int sec_idx, idx;
 167        u32 offset = 0;
 168
 169        /*
 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)
 179         */
 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;
 184                }
 185        }
 186
 187        /*
 188         * If paging is enabled there should be at least 2 more sections left
 189         * (one for CSS and one for Paging data)
 190         */
 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;
 195        }
 196
 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);
 200
 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);
 208
 209        IWL_DEBUG_FW(fwrt,
 210                     "Paging: copied %d CSS bytes to first block\n",
 211                     fwrt->fw_paging_db[0].fw_paging_size);
 212
 213        sec_idx++;
 214
 215        /*
 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.
 220         */
 221        for (idx = 1; idx < fwrt->num_of_paging_blk; idx++) {
 222                struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
 223
 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);
 231
 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);
 236
 237                offset += fwrt->fw_paging_db[idx].fw_paging_size;
 238        }
 239
 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];
 243
 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);
 251
 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);
 255        }
 256
 257        return 0;
 258}
 259
 260static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
 261                              const struct fw_img *fw)
 262{
 263        int ret;
 264
 265        ret = iwl_alloc_fw_paging_mem(fwrt, fw);
 266        if (ret)
 267                return ret;
 268
 269        return iwl_fill_paging_mem(fwrt, fw);
 270}
 271
 272/* send paging cmd to FW in case CPU2 has paging image */
 273static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
 274                               const struct fw_img *fw)
 275{
 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),
 283        };
 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, },
 288        };
 289        int blk_idx;
 290
 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;
 295
 296                addr = addr >> PAGE_2_EXP_SIZE;
 297                phy_addr = cpu_to_le32(addr);
 298                paging_cmd.device_phy_addr[blk_idx] = phy_addr;
 299        }
 300
 301        return iwl_trans_send_cmd(fwrt->trans, &hcmd);
 302}
 303
 304int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
 305{
 306        const struct fw_img *fw = &fwrt->fw->img[type];
 307        int ret;
 308
 309        if (fwrt->trans->cfg->gen2)
 310                return 0;
 311
 312        /*
 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.
 316         */
 317        if (!fw->paging_mem_size)
 318                return 0;
 319
 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;
 324        }
 325
 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;
 331        }
 332
 333        return 0;
 334}
 335IWL_EXPORT_SYMBOL(iwl_init_paging);
 336