mmu.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016-2022 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/slab.h>
  7. #include "../habanalabs.h"
  8. #include <trace/events/habanalabs.h>
  9. /**
  10. * hl_mmu_get_funcs() - get MMU functions structure
  11. * @hdev: habanalabs device structure.
  12. * @pgt_residency: page table residency.
  13. * @is_dram_addr: true if we need HMMU functions
  14. *
  15. * @return appropriate MMU functions structure
  16. */
  17. static struct hl_mmu_funcs *hl_mmu_get_funcs(struct hl_device *hdev, int pgt_residency,
  18. bool is_dram_addr)
  19. {
  20. return &hdev->mmu_func[pgt_residency];
  21. }
  22. bool hl_is_dram_va(struct hl_device *hdev, u64 virt_addr)
  23. {
  24. struct asic_fixed_properties *prop = &hdev->asic_prop;
  25. return hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
  26. prop->dmmu.start_addr,
  27. prop->dmmu.end_addr);
  28. }
  29. /**
  30. * hl_mmu_init() - initialize the MMU module.
  31. * @hdev: habanalabs device structure.
  32. *
  33. * Return: 0 for success, non-zero for failure.
  34. */
  35. int hl_mmu_init(struct hl_device *hdev)
  36. {
  37. int rc = -EOPNOTSUPP;
  38. if (!hdev->mmu_enable)
  39. return 0;
  40. mutex_init(&hdev->mmu_lock);
  41. if (hdev->mmu_func[MMU_DR_PGT].init != NULL) {
  42. rc = hdev->mmu_func[MMU_DR_PGT].init(hdev);
  43. if (rc)
  44. return rc;
  45. }
  46. if (hdev->mmu_func[MMU_HR_PGT].init != NULL) {
  47. rc = hdev->mmu_func[MMU_HR_PGT].init(hdev);
  48. if (rc)
  49. goto fini_dr_mmu;
  50. }
  51. return 0;
  52. fini_dr_mmu:
  53. if (hdev->mmu_func[MMU_DR_PGT].fini != NULL)
  54. hdev->mmu_func[MMU_DR_PGT].fini(hdev);
  55. return rc;
  56. }
  57. /**
  58. * hl_mmu_fini() - release the MMU module.
  59. * @hdev: habanalabs device structure.
  60. *
  61. * This function does the following:
  62. * - Disable MMU in H/W.
  63. * - Free the pgt_infos pool.
  64. *
  65. * All contexts should be freed before calling this function.
  66. */
  67. void hl_mmu_fini(struct hl_device *hdev)
  68. {
  69. if (!hdev->mmu_enable)
  70. return;
  71. if (hdev->mmu_func[MMU_DR_PGT].fini != NULL)
  72. hdev->mmu_func[MMU_DR_PGT].fini(hdev);
  73. if (hdev->mmu_func[MMU_HR_PGT].fini != NULL)
  74. hdev->mmu_func[MMU_HR_PGT].fini(hdev);
  75. mutex_destroy(&hdev->mmu_lock);
  76. }
  77. /**
  78. * hl_mmu_ctx_init() - initialize a context for using the MMU module.
  79. * @ctx: pointer to the context structure to initialize.
  80. *
  81. * Initialize a mutex to protect the concurrent mapping flow, a hash to hold all
  82. * page tables hops related to this context.
  83. * Return: 0 on success, non-zero otherwise.
  84. */
  85. int hl_mmu_ctx_init(struct hl_ctx *ctx)
  86. {
  87. struct hl_device *hdev = ctx->hdev;
  88. int rc = -EOPNOTSUPP;
  89. if (!hdev->mmu_enable)
  90. return 0;
  91. if (hdev->mmu_func[MMU_DR_PGT].ctx_init != NULL) {
  92. rc = hdev->mmu_func[MMU_DR_PGT].ctx_init(ctx);
  93. if (rc)
  94. return rc;
  95. }
  96. if (hdev->mmu_func[MMU_HR_PGT].ctx_init != NULL) {
  97. rc = hdev->mmu_func[MMU_HR_PGT].ctx_init(ctx);
  98. if (rc)
  99. goto fini_dr_ctx;
  100. }
  101. return 0;
  102. fini_dr_ctx:
  103. if (hdev->mmu_func[MMU_DR_PGT].fini != NULL)
  104. hdev->mmu_func[MMU_DR_PGT].fini(hdev);
  105. return rc;
  106. }
  107. /*
  108. * hl_mmu_ctx_fini - disable a ctx from using the mmu module
  109. *
  110. * @ctx: pointer to the context structure
  111. *
  112. * This function does the following:
  113. * - Free any pgts which were not freed yet
  114. * - Free the mutex
  115. * - Free DRAM default page mapping hops
  116. */
  117. void hl_mmu_ctx_fini(struct hl_ctx *ctx)
  118. {
  119. struct hl_device *hdev = ctx->hdev;
  120. if (!hdev->mmu_enable)
  121. return;
  122. if (hdev->mmu_func[MMU_DR_PGT].ctx_fini != NULL)
  123. hdev->mmu_func[MMU_DR_PGT].ctx_fini(ctx);
  124. if (hdev->mmu_func[MMU_HR_PGT].ctx_fini != NULL)
  125. hdev->mmu_func[MMU_HR_PGT].ctx_fini(ctx);
  126. }
  127. /*
  128. * hl_mmu_get_real_page_size - get real page size to use in map/unmap operation
  129. *
  130. * @hdev: pointer to device data.
  131. * @mmu_prop: MMU properties.
  132. * @page_size: page size
  133. * @real_page_size: set here the actual page size to use for the operation
  134. * @is_dram_addr: true if DRAM address, otherwise false.
  135. *
  136. * @return 0 on success, otherwise non 0 error code
  137. *
  138. * note that this is general implementation that can fit most MMU arch. but as this is used as an
  139. * MMU function:
  140. * 1. it shall not be called directly- only from mmu_func structure instance
  141. * 2. each MMU may modify the implementation internally
  142. */
  143. int hl_mmu_get_real_page_size(struct hl_device *hdev, struct hl_mmu_properties *mmu_prop,
  144. u32 page_size, u32 *real_page_size, bool is_dram_addr)
  145. {
  146. /*
  147. * The H/W handles mapping of specific page sizes. Hence if the page
  148. * size is bigger, we break it to sub-pages and map them separately.
  149. */
  150. if ((page_size % mmu_prop->page_size) == 0) {
  151. *real_page_size = mmu_prop->page_size;
  152. return 0;
  153. }
  154. dev_err(hdev->dev, "page size of %u is not %uKB aligned, can't map\n",
  155. page_size, mmu_prop->page_size >> 10);
  156. return -EFAULT;
  157. }
  158. static struct hl_mmu_properties *hl_mmu_get_prop(struct hl_device *hdev, u32 page_size,
  159. bool is_dram_addr)
  160. {
  161. struct asic_fixed_properties *prop = &hdev->asic_prop;
  162. if (is_dram_addr)
  163. return &prop->dmmu;
  164. else if ((page_size % prop->pmmu_huge.page_size) == 0)
  165. return &prop->pmmu_huge;
  166. return &prop->pmmu;
  167. }
  168. /*
  169. * hl_mmu_unmap_page - unmaps a virtual addr
  170. *
  171. * @ctx: pointer to the context structure
  172. * @virt_addr: virt addr to map from
  173. * @page_size: size of the page to unmap
  174. * @flush_pte: whether to do a PCI flush
  175. *
  176. * This function does the following:
  177. * - Check that the virt addr is mapped
  178. * - Unmap the virt addr and frees pgts if possible
  179. * - Returns 0 on success, -EINVAL if the given addr is not mapped
  180. *
  181. * Because this function changes the page tables in the device and because it
  182. * changes the MMU hash, it must be protected by a lock.
  183. * However, because it maps only a single page, the lock should be implemented
  184. * in a higher level in order to protect the entire mapping of the memory area
  185. *
  186. * For optimization reasons PCI flush may be requested once after unmapping of
  187. * large area.
  188. */
  189. int hl_mmu_unmap_page(struct hl_ctx *ctx, u64 virt_addr, u32 page_size, bool flush_pte)
  190. {
  191. struct hl_device *hdev = ctx->hdev;
  192. struct hl_mmu_properties *mmu_prop;
  193. struct hl_mmu_funcs *mmu_funcs;
  194. int i, pgt_residency, rc = 0;
  195. u32 real_page_size, npages;
  196. u64 real_virt_addr;
  197. bool is_dram_addr;
  198. if (!hdev->mmu_enable)
  199. return 0;
  200. is_dram_addr = hl_is_dram_va(hdev, virt_addr);
  201. mmu_prop = hl_mmu_get_prop(hdev, page_size, is_dram_addr);
  202. pgt_residency = mmu_prop->host_resident ? MMU_HR_PGT : MMU_DR_PGT;
  203. mmu_funcs = hl_mmu_get_funcs(hdev, pgt_residency, is_dram_addr);
  204. rc = hdev->asic_funcs->mmu_get_real_page_size(hdev, mmu_prop, page_size, &real_page_size,
  205. is_dram_addr);
  206. if (rc)
  207. return rc;
  208. npages = page_size / real_page_size;
  209. real_virt_addr = virt_addr;
  210. for (i = 0 ; i < npages ; i++) {
  211. rc = mmu_funcs->unmap(ctx, real_virt_addr, is_dram_addr);
  212. if (rc)
  213. break;
  214. real_virt_addr += real_page_size;
  215. }
  216. if (flush_pte)
  217. mmu_funcs->flush(ctx);
  218. if (trace_habanalabs_mmu_unmap_enabled() && !rc)
  219. trace_habanalabs_mmu_unmap(hdev->dev, virt_addr, 0, page_size, flush_pte);
  220. return rc;
  221. }
  222. /*
  223. * hl_mmu_map_page - maps a virtual addr to physical addr
  224. *
  225. * @ctx: pointer to the context structure
  226. * @virt_addr: virt addr to map from
  227. * @phys_addr: phys addr to map to
  228. * @page_size: physical page size
  229. * @flush_pte: whether to do a PCI flush
  230. *
  231. * This function does the following:
  232. * - Check that the virt addr is not mapped
  233. * - Allocate pgts as necessary in order to map the virt addr to the phys
  234. * - Returns 0 on success, -EINVAL if addr is already mapped, or -ENOMEM.
  235. *
  236. * Because this function changes the page tables in the device and because it
  237. * changes the MMU hash, it must be protected by a lock.
  238. * However, because it maps only a single page, the lock should be implemented
  239. * in a higher level in order to protect the entire mapping of the memory area
  240. *
  241. * For optimization reasons PCI flush may be requested once after mapping of
  242. * large area.
  243. */
  244. int hl_mmu_map_page(struct hl_ctx *ctx, u64 virt_addr, u64 phys_addr, u32 page_size,
  245. bool flush_pte)
  246. {
  247. int i, rc, pgt_residency, mapped_cnt = 0;
  248. struct hl_device *hdev = ctx->hdev;
  249. struct hl_mmu_properties *mmu_prop;
  250. u64 real_virt_addr, real_phys_addr;
  251. struct hl_mmu_funcs *mmu_funcs;
  252. u32 real_page_size, npages;
  253. bool is_dram_addr;
  254. if (!hdev->mmu_enable)
  255. return 0;
  256. is_dram_addr = hl_is_dram_va(hdev, virt_addr);
  257. mmu_prop = hl_mmu_get_prop(hdev, page_size, is_dram_addr);
  258. pgt_residency = mmu_prop->host_resident ? MMU_HR_PGT : MMU_DR_PGT;
  259. mmu_funcs = hl_mmu_get_funcs(hdev, pgt_residency, is_dram_addr);
  260. rc = hdev->asic_funcs->mmu_get_real_page_size(hdev, mmu_prop, page_size, &real_page_size,
  261. is_dram_addr);
  262. if (rc)
  263. return rc;
  264. /*
  265. * Verify that the phys and virt addresses are aligned with the
  266. * MMU page size (in dram this means checking the address and MMU
  267. * after scrambling)
  268. */
  269. if ((is_dram_addr &&
  270. ((hdev->asic_funcs->scramble_addr(hdev, phys_addr) &
  271. (mmu_prop->page_size - 1)) ||
  272. (hdev->asic_funcs->scramble_addr(hdev, virt_addr) &
  273. (mmu_prop->page_size - 1)))) ||
  274. (!is_dram_addr && ((phys_addr & (real_page_size - 1)) ||
  275. (virt_addr & (real_page_size - 1)))))
  276. dev_crit(hdev->dev,
  277. "Mapping address 0x%llx with virtual address 0x%llx and page size of 0x%x is erroneous! Addresses must be divisible by page size",
  278. phys_addr, virt_addr, real_page_size);
  279. npages = page_size / real_page_size;
  280. real_virt_addr = virt_addr;
  281. real_phys_addr = phys_addr;
  282. for (i = 0 ; i < npages ; i++) {
  283. rc = mmu_funcs->map(ctx, real_virt_addr, real_phys_addr, real_page_size,
  284. is_dram_addr);
  285. if (rc)
  286. goto err;
  287. real_virt_addr += real_page_size;
  288. real_phys_addr += real_page_size;
  289. mapped_cnt++;
  290. }
  291. if (flush_pte)
  292. mmu_funcs->flush(ctx);
  293. trace_habanalabs_mmu_map(hdev->dev, virt_addr, phys_addr, page_size, flush_pte);
  294. return 0;
  295. err:
  296. real_virt_addr = virt_addr;
  297. for (i = 0 ; i < mapped_cnt ; i++) {
  298. if (mmu_funcs->unmap(ctx, real_virt_addr, is_dram_addr))
  299. dev_warn_ratelimited(hdev->dev,
  300. "failed to unmap va: 0x%llx\n", real_virt_addr);
  301. real_virt_addr += real_page_size;
  302. }
  303. mmu_funcs->flush(ctx);
  304. return rc;
  305. }
  306. /*
  307. * hl_mmu_map_contiguous - implements a wrapper for hl_mmu_map_page
  308. * for mapping contiguous physical memory
  309. *
  310. * @ctx: pointer to the context structure
  311. * @virt_addr: virt addr to map from
  312. * @phys_addr: phys addr to map to
  313. * @size: size to map
  314. *
  315. */
  316. int hl_mmu_map_contiguous(struct hl_ctx *ctx, u64 virt_addr,
  317. u64 phys_addr, u32 size)
  318. {
  319. struct hl_device *hdev = ctx->hdev;
  320. struct asic_fixed_properties *prop = &hdev->asic_prop;
  321. u64 curr_va, curr_pa;
  322. u32 page_size;
  323. bool flush_pte;
  324. int rc = 0, off;
  325. if (hl_mem_area_inside_range(virt_addr, size,
  326. prop->dmmu.start_addr, prop->dmmu.end_addr))
  327. page_size = prop->dmmu.page_size;
  328. else if (hl_mem_area_inside_range(virt_addr, size,
  329. prop->pmmu.start_addr, prop->pmmu.end_addr))
  330. page_size = prop->pmmu.page_size;
  331. else if (hl_mem_area_inside_range(virt_addr, size,
  332. prop->pmmu_huge.start_addr, prop->pmmu_huge.end_addr))
  333. page_size = prop->pmmu_huge.page_size;
  334. else
  335. return -EINVAL;
  336. for (off = 0 ; off < size ; off += page_size) {
  337. curr_va = virt_addr + off;
  338. curr_pa = phys_addr + off;
  339. flush_pte = (off + page_size) >= size;
  340. rc = hl_mmu_map_page(ctx, curr_va, curr_pa, page_size,
  341. flush_pte);
  342. if (rc) {
  343. dev_err(hdev->dev,
  344. "Map failed for va 0x%llx to pa 0x%llx\n",
  345. curr_va, curr_pa);
  346. /* last mapping failed so don't try to unmap it - reduce off by page_size */
  347. off -= page_size;
  348. goto unmap;
  349. }
  350. }
  351. return rc;
  352. unmap:
  353. for (; off >= 0 ; off -= page_size) {
  354. curr_va = virt_addr + off;
  355. flush_pte = (off - (s32) page_size) < 0;
  356. if (hl_mmu_unmap_page(ctx, curr_va, page_size, flush_pte))
  357. dev_warn_ratelimited(hdev->dev,
  358. "failed to unmap va 0x%llx\n", curr_va);
  359. }
  360. return rc;
  361. }
  362. /*
  363. * hl_mmu_unmap_contiguous - implements a wrapper for hl_mmu_unmap_page
  364. * for unmapping contiguous physical memory
  365. *
  366. * @ctx: pointer to the context structure
  367. * @virt_addr: virt addr to unmap
  368. * @size: size to unmap
  369. *
  370. */
  371. int hl_mmu_unmap_contiguous(struct hl_ctx *ctx, u64 virt_addr, u32 size)
  372. {
  373. struct hl_device *hdev = ctx->hdev;
  374. struct asic_fixed_properties *prop = &hdev->asic_prop;
  375. u64 curr_va;
  376. u32 page_size;
  377. bool flush_pte;
  378. int rc = 0, off;
  379. if (hl_mem_area_inside_range(virt_addr, size,
  380. prop->dmmu.start_addr, prop->dmmu.end_addr))
  381. page_size = prop->dmmu.page_size;
  382. else if (hl_mem_area_inside_range(virt_addr, size,
  383. prop->pmmu.start_addr, prop->pmmu.end_addr))
  384. page_size = prop->pmmu.page_size;
  385. else if (hl_mem_area_inside_range(virt_addr, size,
  386. prop->pmmu_huge.start_addr, prop->pmmu_huge.end_addr))
  387. page_size = prop->pmmu_huge.page_size;
  388. else
  389. return -EINVAL;
  390. for (off = 0 ; off < size ; off += page_size) {
  391. curr_va = virt_addr + off;
  392. flush_pte = (off + page_size) >= size;
  393. rc = hl_mmu_unmap_page(ctx, curr_va, page_size, flush_pte);
  394. if (rc)
  395. dev_warn_ratelimited(hdev->dev,
  396. "Unmap failed for va 0x%llx\n", curr_va);
  397. }
  398. return rc;
  399. }
  400. /*
  401. * hl_mmu_swap_out - marks all mapping of the given ctx as swapped out
  402. *
  403. * @ctx: pointer to the context structure
  404. *
  405. */
  406. void hl_mmu_swap_out(struct hl_ctx *ctx)
  407. {
  408. struct hl_device *hdev = ctx->hdev;
  409. if (!hdev->mmu_enable)
  410. return;
  411. if (hdev->mmu_func[MMU_DR_PGT].swap_out != NULL)
  412. hdev->mmu_func[MMU_DR_PGT].swap_out(ctx);
  413. if (hdev->mmu_func[MMU_HR_PGT].swap_out != NULL)
  414. hdev->mmu_func[MMU_HR_PGT].swap_out(ctx);
  415. }
  416. /*
  417. * hl_mmu_swap_in - marks all mapping of the given ctx as swapped in
  418. *
  419. * @ctx: pointer to the context structure
  420. *
  421. */
  422. void hl_mmu_swap_in(struct hl_ctx *ctx)
  423. {
  424. struct hl_device *hdev = ctx->hdev;
  425. if (!hdev->mmu_enable)
  426. return;
  427. if (hdev->mmu_func[MMU_DR_PGT].swap_in != NULL)
  428. hdev->mmu_func[MMU_DR_PGT].swap_in(ctx);
  429. if (hdev->mmu_func[MMU_HR_PGT].swap_in != NULL)
  430. hdev->mmu_func[MMU_HR_PGT].swap_in(ctx);
  431. }
  432. static void hl_mmu_pa_page_with_offset(struct hl_ctx *ctx, u64 virt_addr,
  433. struct hl_mmu_hop_info *hops,
  434. u64 *phys_addr)
  435. {
  436. struct asic_fixed_properties *prop = &ctx->hdev->asic_prop;
  437. u64 offset_mask, addr_mask, hop_shift, tmp_phys_addr;
  438. struct hl_mmu_properties *mmu_prop;
  439. /* last hop holds the phys address and flags */
  440. if (hops->unscrambled_paddr)
  441. tmp_phys_addr = hops->unscrambled_paddr;
  442. else
  443. tmp_phys_addr = hops->hop_info[hops->used_hops - 1].hop_pte_val;
  444. if (hops->range_type == HL_VA_RANGE_TYPE_HOST_HUGE)
  445. mmu_prop = &prop->pmmu_huge;
  446. else if (hops->range_type == HL_VA_RANGE_TYPE_HOST)
  447. mmu_prop = &prop->pmmu;
  448. else /* HL_VA_RANGE_TYPE_DRAM */
  449. mmu_prop = &prop->dmmu;
  450. if ((hops->range_type == HL_VA_RANGE_TYPE_DRAM) &&
  451. !is_power_of_2(prop->dram_page_size)) {
  452. u64 dram_page_size, dram_base, abs_phys_addr, abs_virt_addr,
  453. page_id, page_start;
  454. u32 page_off;
  455. /*
  456. * Bit arithmetics cannot be used for non power of two page
  457. * sizes. In addition, since bit arithmetics is not used,
  458. * we cannot ignore dram base. All that shall be considered.
  459. */
  460. dram_page_size = prop->dram_page_size;
  461. dram_base = prop->dram_base_address;
  462. abs_phys_addr = tmp_phys_addr - dram_base;
  463. abs_virt_addr = virt_addr - dram_base;
  464. page_id = DIV_ROUND_DOWN_ULL(abs_phys_addr, dram_page_size);
  465. page_start = page_id * dram_page_size;
  466. div_u64_rem(abs_virt_addr, dram_page_size, &page_off);
  467. *phys_addr = page_start + page_off + dram_base;
  468. } else {
  469. /*
  470. * find the correct hop shift field in hl_mmu_properties
  471. * structure in order to determine the right masks
  472. * for the page offset.
  473. */
  474. hop_shift = mmu_prop->hop_shifts[hops->used_hops - 1];
  475. offset_mask = (1ull << hop_shift) - 1;
  476. addr_mask = ~(offset_mask);
  477. *phys_addr = (tmp_phys_addr & addr_mask) |
  478. (virt_addr & offset_mask);
  479. }
  480. }
  481. int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr)
  482. {
  483. struct hl_mmu_hop_info hops;
  484. int rc;
  485. memset(&hops, 0, sizeof(hops));
  486. rc = hl_mmu_get_tlb_info(ctx, virt_addr, &hops);
  487. if (rc)
  488. return rc;
  489. hl_mmu_pa_page_with_offset(ctx, virt_addr, &hops, phys_addr);
  490. return 0;
  491. }
  492. int hl_mmu_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr,
  493. struct hl_mmu_hop_info *hops)
  494. {
  495. struct hl_device *hdev = ctx->hdev;
  496. struct asic_fixed_properties *prop;
  497. struct hl_mmu_properties *mmu_prop;
  498. struct hl_mmu_funcs *mmu_funcs;
  499. int pgt_residency, rc;
  500. bool is_dram_addr;
  501. if (!hdev->mmu_enable)
  502. return -EOPNOTSUPP;
  503. prop = &hdev->asic_prop;
  504. hops->scrambled_vaddr = virt_addr; /* assume no scrambling */
  505. is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
  506. prop->dmmu.start_addr,
  507. prop->dmmu.end_addr);
  508. /* host-residency is the same in PMMU and PMMU huge, no need to distinguish here */
  509. mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
  510. pgt_residency = mmu_prop->host_resident ? MMU_HR_PGT : MMU_DR_PGT;
  511. mmu_funcs = hl_mmu_get_funcs(hdev, pgt_residency, is_dram_addr);
  512. mutex_lock(&hdev->mmu_lock);
  513. rc = mmu_funcs->get_tlb_info(ctx, virt_addr, hops);
  514. mutex_unlock(&hdev->mmu_lock);
  515. if (rc)
  516. return rc;
  517. /* add page offset to physical address */
  518. if (hops->unscrambled_paddr)
  519. hl_mmu_pa_page_with_offset(ctx, virt_addr, hops, &hops->unscrambled_paddr);
  520. return 0;
  521. }
  522. int hl_mmu_if_set_funcs(struct hl_device *hdev)
  523. {
  524. if (!hdev->mmu_enable)
  525. return 0;
  526. switch (hdev->asic_type) {
  527. case ASIC_GOYA:
  528. case ASIC_GAUDI:
  529. case ASIC_GAUDI_SEC:
  530. hl_mmu_v1_set_funcs(hdev, &hdev->mmu_func[MMU_DR_PGT]);
  531. break;
  532. case ASIC_GAUDI2:
  533. case ASIC_GAUDI2_SEC:
  534. /* MMUs in Gaudi2 are always host resident */
  535. hl_mmu_v2_hr_set_funcs(hdev, &hdev->mmu_func[MMU_HR_PGT]);
  536. break;
  537. default:
  538. dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
  539. hdev->asic_type);
  540. return -EOPNOTSUPP;
  541. }
  542. return 0;
  543. }
  544. /**
  545. * hl_mmu_scramble_addr() - The generic mmu address scrambling routine.
  546. * @hdev: pointer to device data.
  547. * @addr: The address to scramble.
  548. *
  549. * Return: The scrambled address.
  550. */
  551. u64 hl_mmu_scramble_addr(struct hl_device *hdev, u64 addr)
  552. {
  553. return addr;
  554. }
  555. /**
  556. * hl_mmu_descramble_addr() - The generic mmu address descrambling
  557. * routine.
  558. * @hdev: pointer to device data.
  559. * @addr: The address to descramble.
  560. *
  561. * Return: The un-scrambled address.
  562. */
  563. u64 hl_mmu_descramble_addr(struct hl_device *hdev, u64 addr)
  564. {
  565. return addr;
  566. }
  567. int hl_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard, u32 flags)
  568. {
  569. int rc;
  570. rc = hdev->asic_funcs->mmu_invalidate_cache(hdev, is_hard, flags);
  571. if (rc)
  572. dev_err_ratelimited(hdev->dev, "MMU cache invalidation failed\n");
  573. return rc;
  574. }
  575. int hl_mmu_invalidate_cache_range(struct hl_device *hdev, bool is_hard,
  576. u32 flags, u32 asid, u64 va, u64 size)
  577. {
  578. int rc;
  579. rc = hdev->asic_funcs->mmu_invalidate_cache_range(hdev, is_hard, flags,
  580. asid, va, size);
  581. if (rc)
  582. dev_err_ratelimited(hdev->dev, "MMU cache range invalidation failed\n");
  583. return rc;
  584. }
  585. static void hl_mmu_prefetch_work_function(struct work_struct *work)
  586. {
  587. struct hl_prefetch_work *pfw = container_of(work, struct hl_prefetch_work, pf_work);
  588. struct hl_ctx *ctx = pfw->ctx;
  589. struct hl_device *hdev = ctx->hdev;
  590. if (!hl_device_operational(hdev, NULL))
  591. goto put_ctx;
  592. mutex_lock(&hdev->mmu_lock);
  593. hdev->asic_funcs->mmu_prefetch_cache_range(ctx, pfw->flags, pfw->asid, pfw->va, pfw->size);
  594. mutex_unlock(&hdev->mmu_lock);
  595. put_ctx:
  596. /*
  597. * context was taken in the common mmu prefetch function- see comment there about
  598. * context handling.
  599. */
  600. hl_ctx_put(ctx);
  601. kfree(pfw);
  602. }
  603. int hl_mmu_prefetch_cache_range(struct hl_ctx *ctx, u32 flags, u32 asid, u64 va, u64 size)
  604. {
  605. struct hl_prefetch_work *handle_pf_work;
  606. handle_pf_work = kmalloc(sizeof(*handle_pf_work), GFP_KERNEL);
  607. if (!handle_pf_work)
  608. return -ENOMEM;
  609. INIT_WORK(&handle_pf_work->pf_work, hl_mmu_prefetch_work_function);
  610. handle_pf_work->ctx = ctx;
  611. handle_pf_work->va = va;
  612. handle_pf_work->size = size;
  613. handle_pf_work->flags = flags;
  614. handle_pf_work->asid = asid;
  615. /*
  616. * as actual prefetch is done in a WQ we must get the context (and put it
  617. * at the end of the work function)
  618. */
  619. hl_ctx_get(ctx);
  620. queue_work(ctx->hdev->pf_wq, &handle_pf_work->pf_work);
  621. return 0;
  622. }
  623. u64 hl_mmu_get_next_hop_addr(struct hl_ctx *ctx, u64 curr_pte)
  624. {
  625. return (curr_pte & PAGE_PRESENT_MASK) ? (curr_pte & HOP_PHYS_ADDR_MASK) : ULLONG_MAX;
  626. }
  627. /**
  628. * hl_mmu_get_hop_pte_phys_addr() - extract PTE address from HOP
  629. * @ctx: pointer to the context structure to initialize.
  630. * @mmu_prop: MMU properties.
  631. * @hop_idx: HOP index.
  632. * @hop_addr: HOP address.
  633. * @virt_addr: virtual address fro the translation.
  634. *
  635. * @return the matching PTE value on success, otherwise U64_MAX.
  636. */
  637. u64 hl_mmu_get_hop_pte_phys_addr(struct hl_ctx *ctx, struct hl_mmu_properties *mmu_prop,
  638. u8 hop_idx, u64 hop_addr, u64 virt_addr)
  639. {
  640. u64 mask, shift;
  641. if (hop_idx >= mmu_prop->num_hops) {
  642. dev_err_ratelimited(ctx->hdev->dev, "Invalid hop index %d\n", hop_idx);
  643. return U64_MAX;
  644. }
  645. shift = mmu_prop->hop_shifts[hop_idx];
  646. mask = mmu_prop->hop_masks[hop_idx];
  647. return hop_addr + ctx->hdev->asic_prop.mmu_pte_size * ((virt_addr & mask) >> shift);
  648. }
  649. static void mmu_dma_mem_free_from_chunk(struct gen_pool *pool,
  650. struct gen_pool_chunk *chunk,
  651. void *data)
  652. {
  653. struct hl_device *hdev = (struct hl_device *)data;
  654. hl_asic_dma_free_coherent(hdev, (chunk->end_addr - chunk->start_addr) + 1,
  655. (void *)chunk->start_addr, chunk->phys_addr);
  656. }
  657. void hl_mmu_hr_flush(struct hl_ctx *ctx)
  658. {
  659. /* a flush operation requires memory barrier */
  660. mb();
  661. }
  662. /**
  663. * hl_mmu_hr_pool_destroy() - destroy genpool
  664. * @hdev: habanalabs device structure.
  665. * @hr_priv: MMU HR private data.
  666. * @hop_table_size: HOP table size.
  667. *
  668. * This function does the following:
  669. * - free entries allocated for shadow HOP0
  670. * - free pool chunks
  671. * - free pool
  672. */
  673. static void hl_mmu_hr_pool_destroy(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv,
  674. u32 hop_table_size)
  675. {
  676. struct asic_fixed_properties *prop = &hdev->asic_prop;
  677. struct gen_pool **pool = &hr_priv->mmu_pgt_pool;
  678. struct pgt_info *hop0_pgt;
  679. int asid;
  680. if (ZERO_OR_NULL_PTR(*pool))
  681. return;
  682. /* Free the Fixed allocation of HOPs0 */
  683. if (hr_priv->mmu_asid_hop0) {
  684. for (asid = 0 ; asid < prop->max_asid ; asid++) {
  685. hop0_pgt = &hr_priv->mmu_asid_hop0[asid];
  686. if (ZERO_OR_NULL_PTR(hop0_pgt->virt_addr))
  687. continue;
  688. gen_pool_free(*pool, (uintptr_t) hop0_pgt->virt_addr, hop_table_size);
  689. }
  690. }
  691. gen_pool_for_each_chunk(*pool, mmu_dma_mem_free_from_chunk, hdev);
  692. gen_pool_destroy(*pool);
  693. /* Make sure that if we arrive here again without init was called we
  694. * won't cause kernel panic. This can happen for example if we fail
  695. * during hard reset code at certain points
  696. */
  697. *pool = NULL;
  698. }
  699. /**
  700. * hl_mmu_hr_init() - initialize the MMU module.
  701. * @hdev: habanalabs device structure.
  702. * @hr_priv: MMU HR private data.
  703. * @hop_table_size: HOP table size.
  704. * @pgt_size: memory size allocated for the page table
  705. *
  706. * @return 0 on success otherwise non-zero error code
  707. *
  708. * This function does the following:
  709. * - Create a pool of pages for pgt_infos.
  710. * - Create a shadow table for pgt
  711. */
  712. int hl_mmu_hr_init(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size,
  713. u64 pgt_size)
  714. {
  715. struct asic_fixed_properties *prop = &hdev->asic_prop;
  716. size_t pool_chunk_size = SZ_4M;
  717. struct pgt_info *hop0_pgt;
  718. dma_addr_t dma_addr;
  719. u64 virt_addr;
  720. int i, rc;
  721. /*
  722. * we set alloc size as PAGE_SIZE (sine dma_alloc_coherent allocation order/size is
  723. * PAGE_SHIFT/PAGE_SIZE) in order to be able to control the allocations alignment.
  724. * This way we can call "DMA alloc align" according to dma_alloc granularity and supply
  725. * allocations with higher-order alignment restrictions
  726. */
  727. hr_priv->mmu_pgt_pool = gen_pool_create(PAGE_SHIFT, -1);
  728. if (ZERO_OR_NULL_PTR(hr_priv->mmu_pgt_pool)) {
  729. dev_err(hdev->dev, "Failed to create hr page pool\n");
  730. return -ENOMEM;
  731. }
  732. hr_priv->mmu_asid_hop0 = kvcalloc(prop->max_asid, sizeof(struct pgt_info), GFP_KERNEL);
  733. if (ZERO_OR_NULL_PTR(hr_priv->mmu_asid_hop0)) {
  734. dev_err(hdev->dev, "Failed to allocate hr-mmu hop0 table\n");
  735. rc = -ENOMEM;
  736. goto destroy_mmu_pgt_pool;
  737. }
  738. for (i = 0 ; i < pgt_size ; i += pool_chunk_size) {
  739. virt_addr = (uintptr_t) hl_asic_dma_alloc_coherent(hdev, pool_chunk_size,
  740. &dma_addr,
  741. GFP_KERNEL | __GFP_ZERO);
  742. if (ZERO_OR_NULL_PTR(virt_addr)) {
  743. dev_err(hdev->dev,
  744. "Failed to allocate memory for host-resident page pool\n");
  745. rc = -ENOMEM;
  746. goto destroy_mmu_pgt_pool;
  747. }
  748. rc = gen_pool_add_virt(hr_priv->mmu_pgt_pool, virt_addr, (phys_addr_t) dma_addr,
  749. pool_chunk_size, -1);
  750. if (rc) {
  751. dev_err(hdev->dev, "Failed to fill host-resident page pool\n");
  752. goto destroy_mmu_pgt_pool;
  753. }
  754. }
  755. for (i = 0 ; i < prop->max_asid ; i++) {
  756. hop0_pgt = &hr_priv->mmu_asid_hop0[i];
  757. hop0_pgt->virt_addr = (uintptr_t)
  758. gen_pool_dma_zalloc_align(hr_priv->mmu_pgt_pool,
  759. hop_table_size,
  760. (dma_addr_t *) &hop0_pgt->phys_addr,
  761. hop_table_size);
  762. if (!hop0_pgt->virt_addr) {
  763. dev_err(hdev->dev, "Failed to allocate HOP from pgt pool\n");
  764. rc = -ENOMEM;
  765. goto destroy_mmu_pgt_pool;
  766. }
  767. }
  768. /* MMU H/W init will be done in device hw_init() */
  769. return 0;
  770. destroy_mmu_pgt_pool:
  771. hl_mmu_hr_pool_destroy(hdev, hr_priv, hop_table_size);
  772. if (!ZERO_OR_NULL_PTR(hr_priv->mmu_asid_hop0))
  773. kvfree(hr_priv->mmu_asid_hop0);
  774. return rc;
  775. }
  776. /**
  777. * hl_mmu_hr_fini() - release the MMU module.
  778. * @hdev: habanalabs device structure.
  779. * @hr_priv: MMU host resident private info.
  780. * @hop_table_size: HOP table size
  781. *
  782. * This function does the following:
  783. * - Disable MMU in H/W.
  784. * - Free the pgt_infos pool.
  785. *
  786. * All contexts should be freed before calling this function.
  787. */
  788. void hl_mmu_hr_fini(struct hl_device *hdev, struct hl_mmu_hr_priv *hr_priv, u32 hop_table_size)
  789. {
  790. /* MMU H/W fini was already done in device hw_fini() */
  791. hl_mmu_hr_pool_destroy(hdev, hr_priv, hop_table_size);
  792. if (!ZERO_OR_NULL_PTR(hr_priv->mmu_asid_hop0)) {
  793. kvfree(hr_priv->mmu_asid_hop0);
  794. /* Make sure that if we arrive here again without init was
  795. * called we won't cause kernel panic. This can happen for
  796. * example if we fail during hard reset code at certain points
  797. */
  798. hr_priv->mmu_asid_hop0 = NULL;
  799. }
  800. }
  801. /**
  802. * hl_mmu_hr_free_hop_remove_pgt() - free HOP and remove PGT from hash
  803. * @pgt_info: page table info structure.
  804. * @hr_priv: MMU HR private data.
  805. * @hop_table_size: HOP table size.
  806. */
  807. void hl_mmu_hr_free_hop_remove_pgt(struct pgt_info *pgt_info, struct hl_mmu_hr_priv *hr_priv,
  808. u32 hop_table_size)
  809. {
  810. gen_pool_free(hr_priv->mmu_pgt_pool, pgt_info->virt_addr, hop_table_size);
  811. hash_del(&pgt_info->node);
  812. kfree(pgt_info);
  813. }
  814. /**
  815. * hl_mmu_hr_pte_phys_to_virt() - translate PTE phys addr to virt addr
  816. * @ctx: pointer to the context structure
  817. * @pgt: pgt_info for the HOP hosting the PTE
  818. * @phys_pte_addr: phys address of the PTE
  819. * @hop_table_size: HOP table size
  820. *
  821. * @return PTE virtual address
  822. *
  823. * The function use the pgt_info to get HOP base virt addr and obtain the PTE's virt addr
  824. * by adding the PTE offset.
  825. */
  826. u64 hl_mmu_hr_pte_phys_to_virt(struct hl_ctx *ctx, struct pgt_info *pgt,
  827. u64 phys_pte_addr, u32 hop_table_size)
  828. {
  829. u64 page_mask = (hop_table_size - 1);
  830. u64 pte_offset = phys_pte_addr & page_mask;
  831. return pgt->virt_addr + pte_offset;
  832. }
  833. /**
  834. * hl_mmu_hr_write_pte() - write HR PTE
  835. * @ctx: pointer to the context structure
  836. * @pgt_info: HOP's page table info structure
  837. * @phys_pte_addr: phys PTE address
  838. * @val: raw PTE data
  839. * @hop_table_size: HOP table size
  840. */
  841. void hl_mmu_hr_write_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
  842. u64 val, u32 hop_table_size)
  843. {
  844. /*
  845. * The value to write is the phys address of the next hop +
  846. * flags at the 12 LSBs.
  847. */
  848. u64 virt_addr = hl_mmu_hr_pte_phys_to_virt(ctx, pgt_info, phys_pte_addr, hop_table_size);
  849. *((u64 *) (uintptr_t) virt_addr) = val;
  850. }
  851. /**
  852. * hl_mmu_hr_clear_pte() - clear HR PTE
  853. * @ctx: pointer to the context structure
  854. * @pgt_info: HOP's page table info structure
  855. * @phys_pte_addr: phys PTE address
  856. * @hop_table_size: HOP table size
  857. */
  858. void hl_mmu_hr_clear_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info, u64 phys_pte_addr,
  859. u32 hop_table_size)
  860. {
  861. /* no need to transform the value to physical address */
  862. hl_mmu_hr_write_pte(ctx, pgt_info, phys_pte_addr, 0, hop_table_size);
  863. }
  864. /**
  865. * hl_mmu_hr_put_pte() - put HR PTE and remove it if necessary (no more PTEs)
  866. * @ctx: pointer to the context structure
  867. * @pgt_info: HOP's page table info structure
  868. * @hr_priv: HR MMU private info
  869. * @hop_table_size: HOP table size
  870. *
  871. * @return number of PTEs still in the HOP
  872. */
  873. int hl_mmu_hr_put_pte(struct hl_ctx *ctx, struct pgt_info *pgt_info,
  874. struct hl_mmu_hr_priv *hr_priv,
  875. u32 hop_table_size)
  876. {
  877. int num_of_ptes_left;
  878. pgt_info->num_of_ptes--;
  879. /*
  880. * Need to save the number of ptes left because free_hop might free
  881. * the pgt_info
  882. */
  883. num_of_ptes_left = pgt_info->num_of_ptes;
  884. if (!num_of_ptes_left)
  885. hl_mmu_hr_free_hop_remove_pgt(pgt_info, hr_priv, hop_table_size);
  886. return num_of_ptes_left;
  887. }
  888. /**
  889. * hl_mmu_hr_get_pte() - increase PGT PTE count
  890. * @ctx: pointer to the context structure
  891. * @hr_func: host resident functions
  892. * @phys_hop_addr: HOP phys address
  893. */
  894. void hl_mmu_hr_get_pte(struct hl_ctx *ctx, struct hl_hr_mmu_funcs *hr_func, u64 phys_hop_addr)
  895. {
  896. hr_func->get_pgt_info(ctx, phys_hop_addr)->num_of_ptes++;
  897. }
  898. /**
  899. * hl_mmu_hr_get_next_hop_pgt_info() - get pgt_info structure for the next HOP
  900. * @ctx: pointer to the context structure.
  901. * @hr_func: host resident functions.
  902. * @curr_pte: current PTE value.
  903. *
  904. * @return pgt_info structure on success, otherwise NULL.
  905. */
  906. struct pgt_info *hl_mmu_hr_get_next_hop_pgt_info(struct hl_ctx *ctx,
  907. struct hl_hr_mmu_funcs *hr_func,
  908. u64 curr_pte)
  909. {
  910. u64 next_hop_phys_addr = hl_mmu_get_next_hop_addr(ctx, curr_pte);
  911. if (next_hop_phys_addr == ULLONG_MAX)
  912. return NULL;
  913. return hr_func->get_pgt_info(ctx, next_hop_phys_addr);
  914. }
  915. /**
  916. * hl_mmu_hr_alloc_hop() - allocate HOP
  917. * @ctx: pointer to the context structure.
  918. * @hr_priv: host resident private info structure.
  919. * @hr_func: host resident functions.
  920. * @mmu_prop: MMU properties.
  921. *
  922. * @return pgt_info structure associated with the allocated HOP on success, otherwise NULL.
  923. */
  924. struct pgt_info *hl_mmu_hr_alloc_hop(struct hl_ctx *ctx, struct hl_mmu_hr_priv *hr_priv,
  925. struct hl_hr_mmu_funcs *hr_func,
  926. struct hl_mmu_properties *mmu_prop)
  927. {
  928. struct hl_device *hdev = ctx->hdev;
  929. struct pgt_info *pgt_info;
  930. dma_addr_t phys_addr;
  931. void *virt_addr;
  932. int i, retry = 1;
  933. pgt_info = kmalloc(sizeof(*pgt_info), GFP_KERNEL);
  934. if (!pgt_info)
  935. return NULL;
  936. for (i = 0; i <= retry; i++) {
  937. virt_addr = gen_pool_dma_zalloc_align(hr_priv->mmu_pgt_pool,
  938. mmu_prop->hop_table_size,
  939. &phys_addr,
  940. mmu_prop->hop_table_size);
  941. if (virt_addr)
  942. break;
  943. /* No memory in pool - get some and try again */
  944. virt_addr = hl_asic_dma_alloc_coherent(hdev, SZ_2M, &phys_addr,
  945. GFP_KERNEL | __GFP_ZERO);
  946. if (ZERO_OR_NULL_PTR(virt_addr))
  947. break;
  948. if (gen_pool_add_virt(hr_priv->mmu_pgt_pool, (unsigned long)virt_addr,
  949. phys_addr, SZ_2M, -1)) {
  950. hl_asic_dma_free_coherent(hdev, SZ_2M, virt_addr, phys_addr);
  951. virt_addr = NULL;
  952. break;
  953. }
  954. }
  955. if (ZERO_OR_NULL_PTR(virt_addr)) {
  956. dev_err(hdev->dev, "failed to allocate page\n");
  957. goto pool_alloc_err;
  958. }
  959. pgt_info->phys_addr = phys_addr;
  960. pgt_info->shadow_addr = (unsigned long) NULL;
  961. pgt_info->virt_addr = (unsigned long)virt_addr;
  962. pgt_info->ctx = ctx;
  963. pgt_info->num_of_ptes = 0;
  964. hr_func->add_pgt_info(ctx, pgt_info, phys_addr);
  965. return pgt_info;
  966. pool_alloc_err:
  967. kfree(pgt_info);
  968. return NULL;
  969. }
  970. /**
  971. * hl_mmu_hr_get_alloc_next_hop() - get the next HOP, allocate it if it does not exist
  972. * @ctx: pointer to the context structure.
  973. * @hr_priv: host resident private info structure.
  974. * @hr_func: host resident functions.
  975. * @mmu_prop: MMU properties.
  976. * @curr_pte: current PTE value.
  977. * @is_new_hop: set to true if HOP is new (caller responsibility to set it to false).
  978. *
  979. * @return pgt_info structure associated with the allocated HOP on success, otherwise NULL.
  980. */
  981. struct pgt_info *hl_mmu_hr_get_alloc_next_hop(struct hl_ctx *ctx,
  982. struct hl_mmu_hr_priv *hr_priv,
  983. struct hl_hr_mmu_funcs *hr_func,
  984. struct hl_mmu_properties *mmu_prop,
  985. u64 curr_pte, bool *is_new_hop)
  986. {
  987. u64 hop_addr = hl_mmu_get_next_hop_addr(ctx, curr_pte);
  988. if (hop_addr != ULLONG_MAX)
  989. return hr_func->get_pgt_info(ctx, hop_addr);
  990. *is_new_hop = true;
  991. return hl_mmu_hr_alloc_hop(ctx, hr_priv, hr_func, mmu_prop);
  992. }
  993. /**
  994. * hl_mmu_hr_get_tlb_info() - get the TLB info (info for a specific mapping)
  995. * @ctx: pointer to the context structure.
  996. * @virt_addr: the virt address for which to get info.
  997. * @hops: HOPs info structure.
  998. * @hr_func: host resident functions.
  999. *
  1000. * @return 0 on success, otherwise non 0 error code..
  1001. */
  1002. int hl_mmu_hr_get_tlb_info(struct hl_ctx *ctx, u64 virt_addr, struct hl_mmu_hop_info *hops,
  1003. struct hl_hr_mmu_funcs *hr_func)
  1004. {
  1005. /* using 6 HOPs as this is the maximum number of HOPs */
  1006. struct pgt_info *hops_pgt_info[MMU_ARCH_6_HOPS] = { NULL };
  1007. struct hl_device *hdev = ctx->hdev;
  1008. struct hl_mmu_properties *mmu_prop;
  1009. int rc, i, used_hops;
  1010. bool is_huge;
  1011. rc = hr_func->get_tlb_mapping_params(hdev, &mmu_prop, hops, virt_addr, &is_huge);
  1012. if (rc)
  1013. return rc;
  1014. used_hops = mmu_prop->num_hops;
  1015. /* huge pages use one less hop */
  1016. if (is_huge)
  1017. used_hops--;
  1018. hops->scrambled_vaddr = hdev->asic_funcs->scramble_addr(hdev, virt_addr);
  1019. for (i = 0 ; i < used_hops ; i++) {
  1020. if (i == 0)
  1021. hops_pgt_info[i] = hr_func->get_hop0_pgt_info(ctx);
  1022. else
  1023. hops_pgt_info[i] = hl_mmu_hr_get_next_hop_pgt_info(ctx, hr_func,
  1024. hops->hop_info[i - 1].hop_pte_val);
  1025. if (!hops_pgt_info[i])
  1026. return -EFAULT;
  1027. hops->hop_info[i].hop_addr = hops_pgt_info[i]->phys_addr;
  1028. hops->hop_info[i].hop_pte_addr =
  1029. hl_mmu_get_hop_pte_phys_addr(ctx, mmu_prop, i,
  1030. hops->hop_info[i].hop_addr,
  1031. hops->scrambled_vaddr);
  1032. hops->hop_info[i].hop_pte_val = *(u64 *) (uintptr_t)
  1033. hl_mmu_hr_pte_phys_to_virt(ctx, hops_pgt_info[i],
  1034. hops->hop_info[i].hop_pte_addr,
  1035. mmu_prop->hop_table_size);
  1036. if (!(hops->hop_info[i].hop_pte_val & PAGE_PRESENT_MASK))
  1037. return -EFAULT;
  1038. if (hops->hop_info[i].hop_pte_val & mmu_prop->last_mask)
  1039. break;
  1040. }
  1041. /* if passed over all hops then no last hop was found */
  1042. if (i == mmu_prop->num_hops)
  1043. return -EFAULT;
  1044. if (hops->scrambled_vaddr != virt_addr)
  1045. hops->unscrambled_paddr = hdev->asic_funcs->descramble_addr
  1046. (hdev, hops->hop_info[i].hop_pte_val);
  1047. else
  1048. hops->unscrambled_paddr = hops->hop_info[i].hop_pte_val;
  1049. hops->used_hops = i + 1;
  1050. return 0;
  1051. }