i40e_hmc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "i40e_osdep.h"
  4. #include "i40e_register.h"
  5. #include "i40e_status.h"
  6. #include "i40e_alloc.h"
  7. #include "i40e_hmc.h"
  8. #include "i40e_type.h"
  9. /**
  10. * i40e_add_sd_table_entry - Adds a segment descriptor to the table
  11. * @hw: pointer to our hw struct
  12. * @hmc_info: pointer to the HMC configuration information struct
  13. * @sd_index: segment descriptor index to manipulate
  14. * @type: what type of segment descriptor we're manipulating
  15. * @direct_mode_sz: size to alloc in direct mode
  16. **/
  17. i40e_status i40e_add_sd_table_entry(struct i40e_hw *hw,
  18. struct i40e_hmc_info *hmc_info,
  19. u32 sd_index,
  20. enum i40e_sd_entry_type type,
  21. u64 direct_mode_sz)
  22. {
  23. enum i40e_memory_type mem_type __attribute__((unused));
  24. struct i40e_hmc_sd_entry *sd_entry;
  25. bool dma_mem_alloc_done = false;
  26. struct i40e_dma_mem mem;
  27. i40e_status ret_code = I40E_SUCCESS;
  28. u64 alloc_len;
  29. if (NULL == hmc_info->sd_table.sd_entry) {
  30. ret_code = I40E_ERR_BAD_PTR;
  31. hw_dbg(hw, "i40e_add_sd_table_entry: bad sd_entry\n");
  32. goto exit;
  33. }
  34. if (sd_index >= hmc_info->sd_table.sd_cnt) {
  35. ret_code = I40E_ERR_INVALID_SD_INDEX;
  36. hw_dbg(hw, "i40e_add_sd_table_entry: bad sd_index\n");
  37. goto exit;
  38. }
  39. sd_entry = &hmc_info->sd_table.sd_entry[sd_index];
  40. if (!sd_entry->valid) {
  41. if (I40E_SD_TYPE_PAGED == type) {
  42. mem_type = i40e_mem_pd;
  43. alloc_len = I40E_HMC_PAGED_BP_SIZE;
  44. } else {
  45. mem_type = i40e_mem_bp_jumbo;
  46. alloc_len = direct_mode_sz;
  47. }
  48. /* allocate a 4K pd page or 2M backing page */
  49. ret_code = i40e_allocate_dma_mem(hw, &mem, mem_type, alloc_len,
  50. I40E_HMC_PD_BP_BUF_ALIGNMENT);
  51. if (ret_code)
  52. goto exit;
  53. dma_mem_alloc_done = true;
  54. if (I40E_SD_TYPE_PAGED == type) {
  55. ret_code = i40e_allocate_virt_mem(hw,
  56. &sd_entry->u.pd_table.pd_entry_virt_mem,
  57. sizeof(struct i40e_hmc_pd_entry) * 512);
  58. if (ret_code)
  59. goto exit;
  60. sd_entry->u.pd_table.pd_entry =
  61. (struct i40e_hmc_pd_entry *)
  62. sd_entry->u.pd_table.pd_entry_virt_mem.va;
  63. sd_entry->u.pd_table.pd_page_addr = mem;
  64. } else {
  65. sd_entry->u.bp.addr = mem;
  66. sd_entry->u.bp.sd_pd_index = sd_index;
  67. }
  68. /* initialize the sd entry */
  69. hmc_info->sd_table.sd_entry[sd_index].entry_type = type;
  70. /* increment the ref count */
  71. I40E_INC_SD_REFCNT(&hmc_info->sd_table);
  72. }
  73. /* Increment backing page reference count */
  74. if (I40E_SD_TYPE_DIRECT == sd_entry->entry_type)
  75. I40E_INC_BP_REFCNT(&sd_entry->u.bp);
  76. exit:
  77. if (ret_code)
  78. if (dma_mem_alloc_done)
  79. i40e_free_dma_mem(hw, &mem);
  80. return ret_code;
  81. }
  82. /**
  83. * i40e_add_pd_table_entry - Adds page descriptor to the specified table
  84. * @hw: pointer to our HW structure
  85. * @hmc_info: pointer to the HMC configuration information structure
  86. * @pd_index: which page descriptor index to manipulate
  87. * @rsrc_pg: if not NULL, use preallocated page instead of allocating new one.
  88. *
  89. * This function:
  90. * 1. Initializes the pd entry
  91. * 2. Adds pd_entry in the pd_table
  92. * 3. Mark the entry valid in i40e_hmc_pd_entry structure
  93. * 4. Initializes the pd_entry's ref count to 1
  94. * assumptions:
  95. * 1. The memory for pd should be pinned down, physically contiguous and
  96. * aligned on 4K boundary and zeroed memory.
  97. * 2. It should be 4K in size.
  98. **/
  99. i40e_status i40e_add_pd_table_entry(struct i40e_hw *hw,
  100. struct i40e_hmc_info *hmc_info,
  101. u32 pd_index,
  102. struct i40e_dma_mem *rsrc_pg)
  103. {
  104. i40e_status ret_code = 0;
  105. struct i40e_hmc_pd_table *pd_table;
  106. struct i40e_hmc_pd_entry *pd_entry;
  107. struct i40e_dma_mem mem;
  108. struct i40e_dma_mem *page = &mem;
  109. u32 sd_idx, rel_pd_idx;
  110. u64 *pd_addr;
  111. u64 page_desc;
  112. if (pd_index / I40E_HMC_PD_CNT_IN_SD >= hmc_info->sd_table.sd_cnt) {
  113. ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
  114. hw_dbg(hw, "i40e_add_pd_table_entry: bad pd_index\n");
  115. goto exit;
  116. }
  117. /* find corresponding sd */
  118. sd_idx = (pd_index / I40E_HMC_PD_CNT_IN_SD);
  119. if (I40E_SD_TYPE_PAGED !=
  120. hmc_info->sd_table.sd_entry[sd_idx].entry_type)
  121. goto exit;
  122. rel_pd_idx = (pd_index % I40E_HMC_PD_CNT_IN_SD);
  123. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  124. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  125. if (!pd_entry->valid) {
  126. if (rsrc_pg) {
  127. pd_entry->rsrc_pg = true;
  128. page = rsrc_pg;
  129. } else {
  130. /* allocate a 4K backing page */
  131. ret_code = i40e_allocate_dma_mem(hw, page, i40e_mem_bp,
  132. I40E_HMC_PAGED_BP_SIZE,
  133. I40E_HMC_PD_BP_BUF_ALIGNMENT);
  134. if (ret_code)
  135. goto exit;
  136. pd_entry->rsrc_pg = false;
  137. }
  138. pd_entry->bp.addr = *page;
  139. pd_entry->bp.sd_pd_index = pd_index;
  140. pd_entry->bp.entry_type = I40E_SD_TYPE_PAGED;
  141. /* Set page address and valid bit */
  142. page_desc = page->pa | 0x1;
  143. pd_addr = (u64 *)pd_table->pd_page_addr.va;
  144. pd_addr += rel_pd_idx;
  145. /* Add the backing page physical address in the pd entry */
  146. memcpy(pd_addr, &page_desc, sizeof(u64));
  147. pd_entry->sd_index = sd_idx;
  148. pd_entry->valid = true;
  149. I40E_INC_PD_REFCNT(pd_table);
  150. }
  151. I40E_INC_BP_REFCNT(&pd_entry->bp);
  152. exit:
  153. return ret_code;
  154. }
  155. /**
  156. * i40e_remove_pd_bp - remove a backing page from a page descriptor
  157. * @hw: pointer to our HW structure
  158. * @hmc_info: pointer to the HMC configuration information structure
  159. * @idx: the page index
  160. *
  161. * This function:
  162. * 1. Marks the entry in pd tabe (for paged address mode) or in sd table
  163. * (for direct address mode) invalid.
  164. * 2. Write to register PMPDINV to invalidate the backing page in FV cache
  165. * 3. Decrement the ref count for the pd _entry
  166. * assumptions:
  167. * 1. Caller can deallocate the memory used by backing storage after this
  168. * function returns.
  169. **/
  170. i40e_status i40e_remove_pd_bp(struct i40e_hw *hw,
  171. struct i40e_hmc_info *hmc_info,
  172. u32 idx)
  173. {
  174. i40e_status ret_code = 0;
  175. struct i40e_hmc_pd_entry *pd_entry;
  176. struct i40e_hmc_pd_table *pd_table;
  177. struct i40e_hmc_sd_entry *sd_entry;
  178. u32 sd_idx, rel_pd_idx;
  179. u64 *pd_addr;
  180. /* calculate index */
  181. sd_idx = idx / I40E_HMC_PD_CNT_IN_SD;
  182. rel_pd_idx = idx % I40E_HMC_PD_CNT_IN_SD;
  183. if (sd_idx >= hmc_info->sd_table.sd_cnt) {
  184. ret_code = I40E_ERR_INVALID_PAGE_DESC_INDEX;
  185. hw_dbg(hw, "i40e_remove_pd_bp: bad idx\n");
  186. goto exit;
  187. }
  188. sd_entry = &hmc_info->sd_table.sd_entry[sd_idx];
  189. if (I40E_SD_TYPE_PAGED != sd_entry->entry_type) {
  190. ret_code = I40E_ERR_INVALID_SD_TYPE;
  191. hw_dbg(hw, "i40e_remove_pd_bp: wrong sd_entry type\n");
  192. goto exit;
  193. }
  194. /* get the entry and decrease its ref counter */
  195. pd_table = &hmc_info->sd_table.sd_entry[sd_idx].u.pd_table;
  196. pd_entry = &pd_table->pd_entry[rel_pd_idx];
  197. I40E_DEC_BP_REFCNT(&pd_entry->bp);
  198. if (pd_entry->bp.ref_cnt)
  199. goto exit;
  200. /* mark the entry invalid */
  201. pd_entry->valid = false;
  202. I40E_DEC_PD_REFCNT(pd_table);
  203. pd_addr = (u64 *)pd_table->pd_page_addr.va;
  204. pd_addr += rel_pd_idx;
  205. memset(pd_addr, 0, sizeof(u64));
  206. I40E_INVALIDATE_PF_HMC_PD(hw, sd_idx, idx);
  207. /* free memory here */
  208. if (!pd_entry->rsrc_pg)
  209. ret_code = i40e_free_dma_mem(hw, &pd_entry->bp.addr);
  210. if (ret_code)
  211. goto exit;
  212. if (!pd_table->ref_cnt)
  213. i40e_free_virt_mem(hw, &pd_table->pd_entry_virt_mem);
  214. exit:
  215. return ret_code;
  216. }
  217. /**
  218. * i40e_prep_remove_sd_bp - Prepares to remove a backing page from a sd entry
  219. * @hmc_info: pointer to the HMC configuration information structure
  220. * @idx: the page index
  221. **/
  222. i40e_status i40e_prep_remove_sd_bp(struct i40e_hmc_info *hmc_info,
  223. u32 idx)
  224. {
  225. i40e_status ret_code = 0;
  226. struct i40e_hmc_sd_entry *sd_entry;
  227. /* get the entry and decrease its ref counter */
  228. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  229. I40E_DEC_BP_REFCNT(&sd_entry->u.bp);
  230. if (sd_entry->u.bp.ref_cnt) {
  231. ret_code = I40E_ERR_NOT_READY;
  232. goto exit;
  233. }
  234. I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
  235. /* mark the entry invalid */
  236. sd_entry->valid = false;
  237. exit:
  238. return ret_code;
  239. }
  240. /**
  241. * i40e_remove_sd_bp_new - Removes a backing page from a segment descriptor
  242. * @hw: pointer to our hw struct
  243. * @hmc_info: pointer to the HMC configuration information structure
  244. * @idx: the page index
  245. * @is_pf: used to distinguish between VF and PF
  246. **/
  247. i40e_status i40e_remove_sd_bp_new(struct i40e_hw *hw,
  248. struct i40e_hmc_info *hmc_info,
  249. u32 idx, bool is_pf)
  250. {
  251. struct i40e_hmc_sd_entry *sd_entry;
  252. if (!is_pf)
  253. return I40E_NOT_SUPPORTED;
  254. /* get the entry and decrease its ref counter */
  255. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  256. I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_DIRECT);
  257. return i40e_free_dma_mem(hw, &sd_entry->u.bp.addr);
  258. }
  259. /**
  260. * i40e_prep_remove_pd_page - Prepares to remove a PD page from sd entry.
  261. * @hmc_info: pointer to the HMC configuration information structure
  262. * @idx: segment descriptor index to find the relevant page descriptor
  263. **/
  264. i40e_status i40e_prep_remove_pd_page(struct i40e_hmc_info *hmc_info,
  265. u32 idx)
  266. {
  267. i40e_status ret_code = 0;
  268. struct i40e_hmc_sd_entry *sd_entry;
  269. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  270. if (sd_entry->u.pd_table.ref_cnt) {
  271. ret_code = I40E_ERR_NOT_READY;
  272. goto exit;
  273. }
  274. /* mark the entry invalid */
  275. sd_entry->valid = false;
  276. I40E_DEC_SD_REFCNT(&hmc_info->sd_table);
  277. exit:
  278. return ret_code;
  279. }
  280. /**
  281. * i40e_remove_pd_page_new - Removes a PD page from sd entry.
  282. * @hw: pointer to our hw struct
  283. * @hmc_info: pointer to the HMC configuration information structure
  284. * @idx: segment descriptor index to find the relevant page descriptor
  285. * @is_pf: used to distinguish between VF and PF
  286. **/
  287. i40e_status i40e_remove_pd_page_new(struct i40e_hw *hw,
  288. struct i40e_hmc_info *hmc_info,
  289. u32 idx, bool is_pf)
  290. {
  291. struct i40e_hmc_sd_entry *sd_entry;
  292. if (!is_pf)
  293. return I40E_NOT_SUPPORTED;
  294. sd_entry = &hmc_info->sd_table.sd_entry[idx];
  295. I40E_CLEAR_PF_SD_ENTRY(hw, idx, I40E_SD_TYPE_PAGED);
  296. return i40e_free_dma_mem(hw, &sd_entry->u.pd_table.pd_page_addr);
  297. }