paging.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. void iwl_free_fw_paging(struct iwl_fw_runtime *fwrt)
  66. {
  67. int i;
  68. if (!fwrt->fw_paging_db[0].fw_paging_block)
  69. return;
  70. for (i = 0; i < NUM_OF_FW_PAGING_BLOCKS; i++) {
  71. struct iwl_fw_paging *paging = &fwrt->fw_paging_db[i];
  72. if (!paging->fw_paging_block) {
  73. IWL_DEBUG_FW(fwrt,
  74. "Paging: block %d already freed, continue to next page\n",
  75. i);
  76. continue;
  77. }
  78. dma_unmap_page(fwrt->trans->dev, paging->fw_paging_phys,
  79. paging->fw_paging_size, DMA_BIDIRECTIONAL);
  80. __free_pages(paging->fw_paging_block,
  81. get_order(paging->fw_paging_size));
  82. paging->fw_paging_block = NULL;
  83. }
  84. kfree(fwrt->trans->paging_download_buf);
  85. fwrt->trans->paging_download_buf = NULL;
  86. fwrt->trans->paging_db = NULL;
  87. memset(fwrt->fw_paging_db, 0, sizeof(fwrt->fw_paging_db));
  88. }
  89. IWL_EXPORT_SYMBOL(iwl_free_fw_paging);
  90. static int iwl_alloc_fw_paging_mem(struct iwl_fw_runtime *fwrt,
  91. const struct fw_img *image)
  92. {
  93. struct page *block;
  94. dma_addr_t phys = 0;
  95. int blk_idx, order, num_of_pages, size, dma_enabled;
  96. if (fwrt->fw_paging_db[0].fw_paging_block)
  97. return 0;
  98. dma_enabled = is_device_dma_capable(fwrt->trans->dev);
  99. /* ensure BLOCK_2_EXP_SIZE is power of 2 of PAGING_BLOCK_SIZE */
  100. BUILD_BUG_ON(BIT(BLOCK_2_EXP_SIZE) != PAGING_BLOCK_SIZE);
  101. num_of_pages = image->paging_mem_size / FW_PAGING_SIZE;
  102. fwrt->num_of_paging_blk =
  103. DIV_ROUND_UP(num_of_pages, NUM_OF_PAGE_PER_GROUP);
  104. fwrt->num_of_pages_in_last_blk =
  105. num_of_pages -
  106. NUM_OF_PAGE_PER_GROUP * (fwrt->num_of_paging_blk - 1);
  107. IWL_DEBUG_FW(fwrt,
  108. "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
  109. fwrt->num_of_paging_blk,
  110. fwrt->num_of_pages_in_last_blk);
  111. /*
  112. * Allocate CSS and paging blocks in dram.
  113. */
  114. for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
  115. /* For CSS allocate 4KB, for others PAGING_BLOCK_SIZE (32K) */
  116. size = blk_idx ? PAGING_BLOCK_SIZE : FW_PAGING_SIZE;
  117. order = get_order(size);
  118. block = alloc_pages(GFP_KERNEL, order);
  119. if (!block) {
  120. /* free all the previous pages since we failed */
  121. iwl_free_fw_paging(fwrt);
  122. return -ENOMEM;
  123. }
  124. fwrt->fw_paging_db[blk_idx].fw_paging_block = block;
  125. fwrt->fw_paging_db[blk_idx].fw_paging_size = size;
  126. if (dma_enabled) {
  127. phys = dma_map_page(fwrt->trans->dev, block, 0,
  128. PAGE_SIZE << order,
  129. DMA_BIDIRECTIONAL);
  130. if (dma_mapping_error(fwrt->trans->dev, phys)) {
  131. /*
  132. * free the previous pages and the current one
  133. * since we failed to map_page.
  134. */
  135. iwl_free_fw_paging(fwrt);
  136. return -ENOMEM;
  137. }
  138. fwrt->fw_paging_db[blk_idx].fw_paging_phys = phys;
  139. } else {
  140. fwrt->fw_paging_db[blk_idx].fw_paging_phys =
  141. PAGING_ADDR_SIG |
  142. blk_idx << BLOCK_2_EXP_SIZE;
  143. }
  144. if (!blk_idx)
  145. IWL_DEBUG_FW(fwrt,
  146. "Paging: allocated 4K(CSS) bytes (order %d) for firmware paging.\n",
  147. order);
  148. else
  149. IWL_DEBUG_FW(fwrt,
  150. "Paging: allocated 32K bytes (order %d) for firmware paging.\n",
  151. order);
  152. }
  153. return 0;
  154. }
  155. static int iwl_fill_paging_mem(struct iwl_fw_runtime *fwrt,
  156. const struct fw_img *image)
  157. {
  158. int sec_idx, idx;
  159. u32 offset = 0;
  160. /*
  161. * find where is the paging image start point:
  162. * if CPU2 exist and it's in paging format, then the image looks like:
  163. * CPU1 sections (2 or more)
  164. * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
  165. * CPU2 sections (not paged)
  166. * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
  167. * non paged to CPU2 paging sec
  168. * CPU2 paging CSS
  169. * CPU2 paging image (including instruction and data)
  170. */
  171. for (sec_idx = 0; sec_idx < image->num_sec; sec_idx++) {
  172. if (image->sec[sec_idx].offset == PAGING_SEPARATOR_SECTION) {
  173. sec_idx++;
  174. break;
  175. }
  176. }
  177. /*
  178. * If paging is enabled there should be at least 2 more sections left
  179. * (one for CSS and one for Paging data)
  180. */
  181. if (sec_idx >= image->num_sec - 1) {
  182. IWL_ERR(fwrt, "Paging: Missing CSS and/or paging sections\n");
  183. iwl_free_fw_paging(fwrt);
  184. return -EINVAL;
  185. }
  186. /* copy the CSS block to the dram */
  187. IWL_DEBUG_FW(fwrt, "Paging: load paging CSS to FW, sec = %d\n",
  188. sec_idx);
  189. memcpy(page_address(fwrt->fw_paging_db[0].fw_paging_block),
  190. image->sec[sec_idx].data,
  191. fwrt->fw_paging_db[0].fw_paging_size);
  192. dma_sync_single_for_device(fwrt->trans->dev,
  193. fwrt->fw_paging_db[0].fw_paging_phys,
  194. fwrt->fw_paging_db[0].fw_paging_size,
  195. DMA_BIDIRECTIONAL);
  196. IWL_DEBUG_FW(fwrt,
  197. "Paging: copied %d CSS bytes to first block\n",
  198. fwrt->fw_paging_db[0].fw_paging_size);
  199. sec_idx++;
  200. /*
  201. * copy the paging blocks to the dram
  202. * loop index start from 1 since that CSS block already copied to dram
  203. * and CSS index is 0.
  204. * loop stop at num_of_paging_blk since that last block is not full.
  205. */
  206. for (idx = 1; idx < fwrt->num_of_paging_blk; idx++) {
  207. struct iwl_fw_paging *block = &fwrt->fw_paging_db[idx];
  208. memcpy(page_address(block->fw_paging_block),
  209. image->sec[sec_idx].data + offset,
  210. block->fw_paging_size);
  211. dma_sync_single_for_device(fwrt->trans->dev,
  212. block->fw_paging_phys,
  213. block->fw_paging_size,
  214. DMA_BIDIRECTIONAL);
  215. IWL_DEBUG_FW(fwrt,
  216. "Paging: copied %d paging bytes to block %d\n",
  217. fwrt->fw_paging_db[idx].fw_paging_size,
  218. idx);
  219. offset += fwrt->fw_paging_db[idx].fw_paging_size;
  220. }
  221. /* copy the last paging block */
  222. if (fwrt->num_of_pages_in_last_blk > 0) {
  223. 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. FW_PAGING_SIZE * fwrt->num_of_pages_in_last_blk);
  227. dma_sync_single_for_device(fwrt->trans->dev,
  228. block->fw_paging_phys,
  229. block->fw_paging_size,
  230. DMA_BIDIRECTIONAL);
  231. IWL_DEBUG_FW(fwrt,
  232. "Paging: copied %d pages in the last block %d\n",
  233. fwrt->num_of_pages_in_last_blk, idx);
  234. }
  235. return 0;
  236. }
  237. static int iwl_save_fw_paging(struct iwl_fw_runtime *fwrt,
  238. const struct fw_img *fw)
  239. {
  240. int ret;
  241. ret = iwl_alloc_fw_paging_mem(fwrt, fw);
  242. if (ret)
  243. return ret;
  244. return iwl_fill_paging_mem(fwrt, fw);
  245. }
  246. /* send paging cmd to FW in case CPU2 has paging image */
  247. static int iwl_send_paging_cmd(struct iwl_fw_runtime *fwrt,
  248. const struct fw_img *fw)
  249. {
  250. struct iwl_fw_paging_cmd paging_cmd = {
  251. .flags = cpu_to_le32(PAGING_CMD_IS_SECURED |
  252. PAGING_CMD_IS_ENABLED |
  253. (fwrt->num_of_pages_in_last_blk <<
  254. PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
  255. .block_size = cpu_to_le32(BLOCK_2_EXP_SIZE),
  256. .block_num = cpu_to_le32(fwrt->num_of_paging_blk),
  257. };
  258. struct iwl_host_cmd hcmd = {
  259. .id = iwl_cmd_id(FW_PAGING_BLOCK_CMD, IWL_ALWAYS_LONG_GROUP, 0),
  260. .len = { sizeof(paging_cmd), },
  261. .data = { &paging_cmd, },
  262. };
  263. int blk_idx;
  264. /* loop for for all paging blocks + CSS block */
  265. for (blk_idx = 0; blk_idx < fwrt->num_of_paging_blk + 1; blk_idx++) {
  266. dma_addr_t addr = fwrt->fw_paging_db[blk_idx].fw_paging_phys;
  267. __le32 phy_addr;
  268. addr = addr >> PAGE_2_EXP_SIZE;
  269. phy_addr = cpu_to_le32(addr);
  270. paging_cmd.device_phy_addr[blk_idx] = phy_addr;
  271. }
  272. return iwl_trans_send_cmd(fwrt->trans, &hcmd);
  273. }
  274. /*
  275. * Send paging item cmd to FW in case CPU2 has paging image
  276. */
  277. static int iwl_trans_get_paging_item(struct iwl_fw_runtime *fwrt)
  278. {
  279. int ret;
  280. struct iwl_fw_get_item_cmd fw_get_item_cmd = {
  281. .item_id = cpu_to_le32(IWL_FW_ITEM_ID_PAGING),
  282. };
  283. struct iwl_fw_get_item_resp *item_resp;
  284. struct iwl_host_cmd cmd = {
  285. .id = iwl_cmd_id(FW_GET_ITEM_CMD, IWL_ALWAYS_LONG_GROUP, 0),
  286. .flags = CMD_WANT_SKB | CMD_SEND_IN_RFKILL,
  287. .data = { &fw_get_item_cmd, },
  288. .len = { sizeof(fw_get_item_cmd), },
  289. };
  290. ret = iwl_trans_send_cmd(fwrt->trans, &cmd);
  291. if (ret) {
  292. IWL_ERR(fwrt,
  293. "Paging: Failed to send FW_GET_ITEM_CMD cmd (err = %d)\n",
  294. ret);
  295. return ret;
  296. }
  297. item_resp = (void *)((struct iwl_rx_packet *)cmd.resp_pkt)->data;
  298. if (item_resp->item_id != cpu_to_le32(IWL_FW_ITEM_ID_PAGING)) {
  299. IWL_ERR(fwrt,
  300. "Paging: got wrong item in FW_GET_ITEM_CMD resp (item_id = %u)\n",
  301. le32_to_cpu(item_resp->item_id));
  302. ret = -EIO;
  303. goto exit;
  304. }
  305. /* Add an extra page for headers */
  306. fwrt->trans->paging_download_buf = kzalloc(PAGING_BLOCK_SIZE +
  307. FW_PAGING_SIZE,
  308. GFP_KERNEL);
  309. if (!fwrt->trans->paging_download_buf) {
  310. ret = -ENOMEM;
  311. goto exit;
  312. }
  313. fwrt->trans->paging_req_addr = le32_to_cpu(item_resp->item_val);
  314. fwrt->trans->paging_db = fwrt->fw_paging_db;
  315. IWL_DEBUG_FW(fwrt,
  316. "Paging: got paging request address (paging_req_addr 0x%08x)\n",
  317. fwrt->trans->paging_req_addr);
  318. exit:
  319. iwl_free_resp(&cmd);
  320. return ret;
  321. }
  322. int iwl_init_paging(struct iwl_fw_runtime *fwrt, enum iwl_ucode_type type)
  323. {
  324. const struct fw_img *fw = &fwrt->fw->img[type];
  325. int ret;
  326. if (fwrt->trans->cfg->gen2)
  327. return 0;
  328. /*
  329. * Configure and operate fw paging mechanism.
  330. * The driver configures the paging flow only once.
  331. * The CPU2 paging image is included in the IWL_UCODE_INIT image.
  332. */
  333. if (!fw->paging_mem_size)
  334. return 0;
  335. /*
  336. * When dma is not enabled, the driver needs to copy / write
  337. * the downloaded / uploaded page to / from the smem.
  338. * This gets the location of the place were the pages are
  339. * stored.
  340. */
  341. if (!is_device_dma_capable(fwrt->trans->dev)) {
  342. ret = iwl_trans_get_paging_item(fwrt);
  343. if (ret) {
  344. IWL_ERR(fwrt, "failed to get FW paging item\n");
  345. return ret;
  346. }
  347. }
  348. ret = iwl_save_fw_paging(fwrt, fw);
  349. if (ret) {
  350. IWL_ERR(fwrt, "failed to save the FW paging image\n");
  351. return ret;
  352. }
  353. ret = iwl_send_paging_cmd(fwrt, fw);
  354. if (ret) {
  355. IWL_ERR(fwrt, "failed to send the paging cmd\n");
  356. iwl_free_fw_paging(fwrt);
  357. return ret;
  358. }
  359. return 0;
  360. }
  361. IWL_EXPORT_SYMBOL(iwl_init_paging);