ctxt-info.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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) 2017 Intel Deutschland GmbH
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * BSD LICENSE
  20. *
  21. * Copyright(c) 2017 Intel Deutschland GmbH
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. *
  28. * * Redistributions of source code must retain the above copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * * Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in
  32. * the documentation and/or other materials provided with the
  33. * distribution.
  34. * * Neither the name Intel Corporation nor the names of its
  35. * contributors may be used to endorse or promote products derived
  36. * from this software without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. *****************************************************************************/
  51. #include "iwl-trans.h"
  52. #include "iwl-fh.h"
  53. #include "iwl-context-info.h"
  54. #include "internal.h"
  55. #include "iwl-prph.h"
  56. static int iwl_pcie_get_num_sections(const struct fw_img *fw,
  57. int start)
  58. {
  59. int i = 0;
  60. while (start < fw->num_sec &&
  61. fw->sec[start].offset != CPU1_CPU2_SEPARATOR_SECTION &&
  62. fw->sec[start].offset != PAGING_SEPARATOR_SECTION) {
  63. start++;
  64. i++;
  65. }
  66. return i;
  67. }
  68. static int iwl_pcie_ctxt_info_alloc_dma(struct iwl_trans *trans,
  69. const struct fw_desc *sec,
  70. struct iwl_dram_data *dram)
  71. {
  72. dram->block = dma_alloc_coherent(trans->dev, sec->len,
  73. &dram->physical,
  74. GFP_KERNEL);
  75. if (!dram->block)
  76. return -ENOMEM;
  77. dram->size = sec->len;
  78. memcpy(dram->block, sec->data, sec->len);
  79. return 0;
  80. }
  81. static void iwl_pcie_ctxt_info_free_fw_img(struct iwl_trans *trans)
  82. {
  83. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  84. struct iwl_self_init_dram *dram = &trans_pcie->init_dram;
  85. int i;
  86. if (!dram->fw) {
  87. WARN_ON(dram->fw_cnt);
  88. return;
  89. }
  90. for (i = 0; i < dram->fw_cnt; i++)
  91. dma_free_coherent(trans->dev, dram->fw[i].size,
  92. dram->fw[i].block, dram->fw[i].physical);
  93. kfree(dram->fw);
  94. dram->fw_cnt = 0;
  95. }
  96. void iwl_pcie_ctxt_info_free_paging(struct iwl_trans *trans)
  97. {
  98. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  99. struct iwl_self_init_dram *dram = &trans_pcie->init_dram;
  100. int i;
  101. if (!dram->paging) {
  102. WARN_ON(dram->paging_cnt);
  103. return;
  104. }
  105. /* free paging*/
  106. for (i = 0; i < dram->paging_cnt; i++)
  107. dma_free_coherent(trans->dev, dram->paging[i].size,
  108. dram->paging[i].block,
  109. dram->paging[i].physical);
  110. kfree(dram->paging);
  111. dram->paging_cnt = 0;
  112. }
  113. static int iwl_pcie_ctxt_info_init_fw_sec(struct iwl_trans *trans,
  114. const struct fw_img *fw,
  115. struct iwl_context_info *ctxt_info)
  116. {
  117. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  118. struct iwl_self_init_dram *dram = &trans_pcie->init_dram;
  119. struct iwl_context_info_dram *ctxt_dram = &ctxt_info->dram;
  120. int i, ret, lmac_cnt, umac_cnt, paging_cnt;
  121. lmac_cnt = iwl_pcie_get_num_sections(fw, 0);
  122. /* add 1 due to separator */
  123. umac_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + 1);
  124. /* add 2 due to separators */
  125. paging_cnt = iwl_pcie_get_num_sections(fw, lmac_cnt + umac_cnt + 2);
  126. dram->fw = kcalloc(umac_cnt + lmac_cnt, sizeof(*dram->fw), GFP_KERNEL);
  127. if (!dram->fw)
  128. return -ENOMEM;
  129. dram->paging = kcalloc(paging_cnt, sizeof(*dram->paging), GFP_KERNEL);
  130. if (!dram->paging)
  131. return -ENOMEM;
  132. /* initialize lmac sections */
  133. for (i = 0; i < lmac_cnt; i++) {
  134. ret = iwl_pcie_ctxt_info_alloc_dma(trans, &fw->sec[i],
  135. &dram->fw[dram->fw_cnt]);
  136. if (ret)
  137. return ret;
  138. ctxt_dram->lmac_img[i] =
  139. cpu_to_le64(dram->fw[dram->fw_cnt].physical);
  140. dram->fw_cnt++;
  141. }
  142. /* initialize umac sections */
  143. for (i = 0; i < umac_cnt; i++) {
  144. /* access FW with +1 to make up for lmac separator */
  145. ret = iwl_pcie_ctxt_info_alloc_dma(trans,
  146. &fw->sec[dram->fw_cnt + 1],
  147. &dram->fw[dram->fw_cnt]);
  148. if (ret)
  149. return ret;
  150. ctxt_dram->umac_img[i] =
  151. cpu_to_le64(dram->fw[dram->fw_cnt].physical);
  152. dram->fw_cnt++;
  153. }
  154. /*
  155. * Initialize paging.
  156. * Paging memory isn't stored in dram->fw as the umac and lmac - it is
  157. * stored separately.
  158. * This is since the timing of its release is different -
  159. * while fw memory can be released on alive, the paging memory can be
  160. * freed only when the device goes down.
  161. * Given that, the logic here in accessing the fw image is a bit
  162. * different - fw_cnt isn't changing so loop counter is added to it.
  163. */
  164. for (i = 0; i < paging_cnt; i++) {
  165. /* access FW with +2 to make up for lmac & umac separators */
  166. int fw_idx = dram->fw_cnt + i + 2;
  167. ret = iwl_pcie_ctxt_info_alloc_dma(trans, &fw->sec[fw_idx],
  168. &dram->paging[i]);
  169. if (ret)
  170. return ret;
  171. ctxt_dram->virtual_img[i] =
  172. cpu_to_le64(dram->paging[i].physical);
  173. dram->paging_cnt++;
  174. }
  175. return 0;
  176. }
  177. int iwl_pcie_ctxt_info_init(struct iwl_trans *trans,
  178. const struct fw_img *fw)
  179. {
  180. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  181. struct iwl_context_info *ctxt_info;
  182. struct iwl_context_info_rbd_cfg *rx_cfg;
  183. u32 control_flags = 0;
  184. int ret;
  185. ctxt_info = dma_alloc_coherent(trans->dev, sizeof(*ctxt_info),
  186. &trans_pcie->ctxt_info_dma_addr,
  187. GFP_KERNEL);
  188. if (!ctxt_info)
  189. return -ENOMEM;
  190. ctxt_info->version.version = 0;
  191. ctxt_info->version.mac_id =
  192. cpu_to_le16((u16)iwl_read32(trans, CSR_HW_REV));
  193. /* size is in DWs */
  194. ctxt_info->version.size = cpu_to_le16(sizeof(*ctxt_info) / 4);
  195. BUILD_BUG_ON(RX_QUEUE_CB_SIZE(MQ_RX_TABLE_SIZE) > 0xF);
  196. control_flags = IWL_CTXT_INFO_RB_SIZE_4K |
  197. IWL_CTXT_INFO_TFD_FORMAT_LONG |
  198. RX_QUEUE_CB_SIZE(MQ_RX_TABLE_SIZE) <<
  199. IWL_CTXT_INFO_RB_CB_SIZE_POS;
  200. ctxt_info->control.control_flags = cpu_to_le32(control_flags);
  201. /* initialize RX default queue */
  202. rx_cfg = &ctxt_info->rbd_cfg;
  203. rx_cfg->free_rbd_addr = cpu_to_le64(trans_pcie->rxq->bd_dma);
  204. rx_cfg->used_rbd_addr = cpu_to_le64(trans_pcie->rxq->used_bd_dma);
  205. rx_cfg->status_wr_ptr = cpu_to_le64(trans_pcie->rxq->rb_stts_dma);
  206. /* initialize TX command queue */
  207. ctxt_info->hcmd_cfg.cmd_queue_addr =
  208. cpu_to_le64(trans_pcie->txq[trans_pcie->cmd_queue]->dma_addr);
  209. ctxt_info->hcmd_cfg.cmd_queue_size =
  210. TFD_QUEUE_CB_SIZE(TFD_QUEUE_SIZE_MAX);
  211. /* allocate ucode sections in dram and set addresses */
  212. ret = iwl_pcie_ctxt_info_init_fw_sec(trans, fw, ctxt_info);
  213. if (ret) {
  214. dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),
  215. ctxt_info, trans_pcie->ctxt_info_dma_addr);
  216. return ret;
  217. }
  218. trans_pcie->ctxt_info = ctxt_info;
  219. iwl_enable_interrupts(trans);
  220. /* Configure debug, if exists */
  221. if (trans->dbg_dest_tlv)
  222. iwl_pcie_apply_destination(trans);
  223. /* kick FW self load */
  224. iwl_write64(trans, CSR_CTXT_INFO_BA, trans_pcie->ctxt_info_dma_addr);
  225. iwl_write_prph(trans, UREG_CPU_INIT_RUN, 1);
  226. /* Context info will be released upon alive or failure to get one */
  227. return 0;
  228. }
  229. void iwl_pcie_ctxt_info_free(struct iwl_trans *trans)
  230. {
  231. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  232. if (!trans_pcie->ctxt_info)
  233. return;
  234. dma_free_coherent(trans->dev, sizeof(*trans_pcie->ctxt_info),
  235. trans_pcie->ctxt_info,
  236. trans_pcie->ctxt_info_dma_addr);
  237. trans_pcie->ctxt_info_dma_addr = 0;
  238. trans_pcie->ctxt_info = NULL;
  239. iwl_pcie_ctxt_info_free_fw_img(trans);
  240. }