ctxt-info.c 7.7 KB

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