io-pgtable-fast.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #define pr_fmt(fmt) "io-pgtable-fast: " fmt
  13. #include <linux/iommu.h>
  14. #include <linux/kernel.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/sizes.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <linux/io-pgtable-fast.h>
  20. #include <asm/cacheflush.h>
  21. #include <linux/vmalloc.h>
  22. #include "io-pgtable.h"
  23. #define AV8L_FAST_MAX_ADDR_BITS 48
  24. /* Struct accessors */
  25. #define iof_pgtable_to_data(x) \
  26. container_of((x), struct av8l_fast_io_pgtable, iop)
  27. #define iof_pgtable_ops_to_pgtable(x) \
  28. container_of((x), struct io_pgtable, ops)
  29. #define iof_pgtable_ops_to_data(x) \
  30. iof_pgtable_to_data(iof_pgtable_ops_to_pgtable(x))
  31. struct av8l_fast_io_pgtable {
  32. struct io_pgtable iop;
  33. av8l_fast_iopte *pgd;
  34. av8l_fast_iopte *puds[4];
  35. av8l_fast_iopte *pmds;
  36. struct page **pages; /* page table memory */
  37. };
  38. /* Page table bits */
  39. #define AV8L_FAST_PTE_TYPE_SHIFT 0
  40. #define AV8L_FAST_PTE_TYPE_MASK 0x3
  41. #define AV8L_FAST_PTE_TYPE_BLOCK 1
  42. #define AV8L_FAST_PTE_TYPE_TABLE 3
  43. #define AV8L_FAST_PTE_TYPE_PAGE 3
  44. #define AV8L_FAST_PTE_NSTABLE (((av8l_fast_iopte)1) << 63)
  45. #define AV8L_FAST_PTE_XN (((av8l_fast_iopte)3) << 53)
  46. #define AV8L_FAST_PTE_AF (((av8l_fast_iopte)1) << 10)
  47. #define AV8L_FAST_PTE_SH_NS (((av8l_fast_iopte)0) << 8)
  48. #define AV8L_FAST_PTE_SH_OS (((av8l_fast_iopte)2) << 8)
  49. #define AV8L_FAST_PTE_SH_IS (((av8l_fast_iopte)3) << 8)
  50. #define AV8L_FAST_PTE_NS (((av8l_fast_iopte)1) << 5)
  51. #define AV8L_FAST_PTE_VALID (((av8l_fast_iopte)1) << 0)
  52. #define AV8L_FAST_PTE_ATTR_LO_MASK (((av8l_fast_iopte)0x3ff) << 2)
  53. /* Ignore the contiguous bit for block splitting */
  54. #define AV8L_FAST_PTE_ATTR_HI_MASK (((av8l_fast_iopte)6) << 52)
  55. #define AV8L_FAST_PTE_ATTR_MASK (AV8L_FAST_PTE_ATTR_LO_MASK | \
  56. AV8L_FAST_PTE_ATTR_HI_MASK)
  57. #define AV8L_FAST_PTE_ADDR_MASK ((av8l_fast_iopte)0xfffffffff000)
  58. /* Stage-1 PTE */
  59. #define AV8L_FAST_PTE_AP_PRIV_RW (((av8l_fast_iopte)0) << 6)
  60. #define AV8L_FAST_PTE_AP_RW (((av8l_fast_iopte)1) << 6)
  61. #define AV8L_FAST_PTE_AP_PRIV_RO (((av8l_fast_iopte)2) << 6)
  62. #define AV8L_FAST_PTE_AP_RO (((av8l_fast_iopte)3) << 6)
  63. #define AV8L_FAST_PTE_ATTRINDX_SHIFT 2
  64. #define AV8L_FAST_PTE_nG (((av8l_fast_iopte)1) << 11)
  65. /* Stage-2 PTE */
  66. #define AV8L_FAST_PTE_HAP_FAULT (((av8l_fast_iopte)0) << 6)
  67. #define AV8L_FAST_PTE_HAP_READ (((av8l_fast_iopte)1) << 6)
  68. #define AV8L_FAST_PTE_HAP_WRITE (((av8l_fast_iopte)2) << 6)
  69. #define AV8L_FAST_PTE_MEMATTR_OIWB (((av8l_fast_iopte)0xf) << 2)
  70. #define AV8L_FAST_PTE_MEMATTR_NC (((av8l_fast_iopte)0x5) << 2)
  71. #define AV8L_FAST_PTE_MEMATTR_DEV (((av8l_fast_iopte)0x1) << 2)
  72. /* Register bits */
  73. #define ARM_32_LPAE_TCR_EAE (1 << 31)
  74. #define ARM_64_LPAE_S2_TCR_RES1 (1 << 31)
  75. #define AV8L_FAST_TCR_TG0_4K (0 << 14)
  76. #define AV8L_FAST_TCR_TG0_64K (1 << 14)
  77. #define AV8L_FAST_TCR_TG0_16K (2 << 14)
  78. #define AV8L_FAST_TCR_SH0_SHIFT 12
  79. #define AV8L_FAST_TCR_SH0_MASK 0x3
  80. #define AV8L_FAST_TCR_SH_NS 0
  81. #define AV8L_FAST_TCR_SH_OS 2
  82. #define AV8L_FAST_TCR_SH_IS 3
  83. #define AV8L_FAST_TCR_ORGN0_SHIFT 10
  84. #define AV8L_FAST_TCR_IRGN0_SHIFT 8
  85. #define AV8L_FAST_TCR_RGN_MASK 0x3
  86. #define AV8L_FAST_TCR_RGN_NC 0
  87. #define AV8L_FAST_TCR_RGN_WBWA 1
  88. #define AV8L_FAST_TCR_RGN_WT 2
  89. #define AV8L_FAST_TCR_RGN_WB 3
  90. #define AV8L_FAST_TCR_SL0_SHIFT 6
  91. #define AV8L_FAST_TCR_SL0_MASK 0x3
  92. #define AV8L_FAST_TCR_T0SZ_SHIFT 0
  93. #define AV8L_FAST_TCR_SZ_MASK 0xf
  94. #define AV8L_FAST_TCR_PS_SHIFT 16
  95. #define AV8L_FAST_TCR_PS_MASK 0x7
  96. #define AV8L_FAST_TCR_IPS_SHIFT 32
  97. #define AV8L_FAST_TCR_IPS_MASK 0x7
  98. #define AV8L_FAST_TCR_PS_32_BIT 0x0ULL
  99. #define AV8L_FAST_TCR_PS_36_BIT 0x1ULL
  100. #define AV8L_FAST_TCR_PS_40_BIT 0x2ULL
  101. #define AV8L_FAST_TCR_PS_42_BIT 0x3ULL
  102. #define AV8L_FAST_TCR_PS_44_BIT 0x4ULL
  103. #define AV8L_FAST_TCR_PS_48_BIT 0x5ULL
  104. #define AV8L_FAST_TCR_EPD1_SHIFT 23
  105. #define AV8L_FAST_TCR_EPD1_FAULT 1
  106. #define AV8L_FAST_MAIR_ATTR_SHIFT(n) ((n) << 3)
  107. #define AV8L_FAST_MAIR_ATTR_MASK 0xff
  108. #define AV8L_FAST_MAIR_ATTR_DEVICE 0x04
  109. #define AV8L_FAST_MAIR_ATTR_NC 0x44
  110. #define AV8L_FAST_MAIR_ATTR_WBRWA 0xff
  111. #define AV8L_FAST_MAIR_ATTR_UPSTREAM 0xf4
  112. #define AV8L_FAST_MAIR_ATTR_IDX_NC 0
  113. #define AV8L_FAST_MAIR_ATTR_IDX_CACHE 1
  114. #define AV8L_FAST_MAIR_ATTR_IDX_DEV 2
  115. #define AV8L_FAST_MAIR_ATTR_IDX_UPSTREAM 3
  116. #define AV8L_FAST_PAGE_SHIFT 12
  117. #ifdef CONFIG_IOMMU_IO_PGTABLE_FAST_PROVE_TLB
  118. #include <asm/cacheflush.h>
  119. #include <linux/notifier.h>
  120. static ATOMIC_NOTIFIER_HEAD(av8l_notifier_list);
  121. void av8l_register_notify(struct notifier_block *nb)
  122. {
  123. atomic_notifier_chain_register(&av8l_notifier_list, nb);
  124. }
  125. EXPORT_SYMBOL(av8l_register_notify);
  126. static void __av8l_check_for_stale_tlb(av8l_fast_iopte *ptep)
  127. {
  128. if (unlikely(*ptep)) {
  129. atomic_notifier_call_chain(
  130. &av8l_notifier_list, MAPPED_OVER_STALE_TLB,
  131. (void *) ptep);
  132. pr_err("Tried to map over a non-vacant pte: 0x%llx @ %p\n",
  133. *ptep, ptep);
  134. pr_err("Nearby memory:\n");
  135. print_hex_dump(KERN_ERR, "pgtbl: ", DUMP_PREFIX_ADDRESS,
  136. 32, 8, ptep - 16, 32 * sizeof(*ptep), false);
  137. }
  138. }
  139. void av8l_fast_clear_stale_ptes(av8l_fast_iopte *pmds, bool skip_sync)
  140. {
  141. int i;
  142. av8l_fast_iopte *pmdp = pmds;
  143. for (i = 0; i < ((SZ_1G * 4UL) >> AV8L_FAST_PAGE_SHIFT); ++i) {
  144. if (!(*pmdp & AV8L_FAST_PTE_VALID)) {
  145. *pmdp = 0;
  146. if (!skip_sync)
  147. dmac_clean_range(pmdp, pmdp + 1);
  148. }
  149. pmdp++;
  150. }
  151. }
  152. #else
  153. static void __av8l_check_for_stale_tlb(av8l_fast_iopte *ptep)
  154. {
  155. }
  156. #endif
  157. /* caller must take care of cache maintenance on *ptep */
  158. int av8l_fast_map_public(av8l_fast_iopte *ptep, phys_addr_t paddr, size_t size,
  159. int prot)
  160. {
  161. int i, nptes = size >> AV8L_FAST_PAGE_SHIFT;
  162. av8l_fast_iopte pte = AV8L_FAST_PTE_XN
  163. | AV8L_FAST_PTE_TYPE_PAGE
  164. | AV8L_FAST_PTE_AF
  165. | AV8L_FAST_PTE_nG
  166. | AV8L_FAST_PTE_SH_OS;
  167. if (prot & IOMMU_MMIO)
  168. pte |= (AV8L_FAST_MAIR_ATTR_IDX_DEV
  169. << AV8L_FAST_PTE_ATTRINDX_SHIFT);
  170. else if (prot & IOMMU_CACHE)
  171. pte |= (AV8L_FAST_MAIR_ATTR_IDX_CACHE
  172. << AV8L_FAST_PTE_ATTRINDX_SHIFT);
  173. else if (prot & IOMMU_USE_UPSTREAM_HINT)
  174. pte |= (AV8L_FAST_MAIR_ATTR_IDX_UPSTREAM
  175. << AV8L_FAST_PTE_ATTRINDX_SHIFT);
  176. if (!(prot & IOMMU_WRITE))
  177. pte |= AV8L_FAST_PTE_AP_RO;
  178. else
  179. pte |= AV8L_FAST_PTE_AP_RW;
  180. paddr &= AV8L_FAST_PTE_ADDR_MASK;
  181. for (i = 0; i < nptes; i++, paddr += SZ_4K) {
  182. __av8l_check_for_stale_tlb(ptep + i);
  183. *(ptep + i) = pte | paddr;
  184. }
  185. return 0;
  186. }
  187. static int av8l_fast_map(struct io_pgtable_ops *ops, unsigned long iova,
  188. phys_addr_t paddr, size_t size, int prot)
  189. {
  190. struct av8l_fast_io_pgtable *data = iof_pgtable_ops_to_data(ops);
  191. av8l_fast_iopte *ptep = iopte_pmd_offset(data->pmds, iova);
  192. unsigned long nptes = size >> AV8L_FAST_PAGE_SHIFT;
  193. av8l_fast_map_public(ptep, paddr, size, prot);
  194. dmac_clean_range(ptep, ptep + nptes);
  195. return 0;
  196. }
  197. static void __av8l_fast_unmap(av8l_fast_iopte *ptep, size_t size,
  198. bool need_stale_tlb_tracking)
  199. {
  200. unsigned long nptes = size >> AV8L_FAST_PAGE_SHIFT;
  201. int val = need_stale_tlb_tracking
  202. ? AV8L_FAST_PTE_UNMAPPED_NEED_TLBI
  203. : 0;
  204. memset(ptep, val, sizeof(*ptep) * nptes);
  205. }
  206. /* caller must take care of cache maintenance on *ptep */
  207. void av8l_fast_unmap_public(av8l_fast_iopte *ptep, size_t size)
  208. {
  209. __av8l_fast_unmap(ptep, size, true);
  210. }
  211. static size_t av8l_fast_unmap(struct io_pgtable_ops *ops, unsigned long iova,
  212. size_t size)
  213. {
  214. struct av8l_fast_io_pgtable *data = iof_pgtable_ops_to_data(ops);
  215. struct io_pgtable *iop = &data->iop;
  216. av8l_fast_iopte *ptep = iopte_pmd_offset(data->pmds, iova);
  217. unsigned long nptes = size >> AV8L_FAST_PAGE_SHIFT;
  218. __av8l_fast_unmap(ptep, size, false);
  219. dmac_clean_range(ptep, ptep + nptes);
  220. io_pgtable_tlb_flush_all(iop);
  221. return size;
  222. }
  223. #if defined(CONFIG_ARM64)
  224. #define FAST_PGDNDX(va) (((va) & 0x7fc0000000) >> 27)
  225. #elif defined(CONFIG_ARM)
  226. #define FAST_PGDNDX(va) (((va) & 0xc0000000) >> 27)
  227. #endif
  228. static phys_addr_t av8l_fast_iova_to_phys(struct io_pgtable_ops *ops,
  229. unsigned long iova)
  230. {
  231. struct av8l_fast_io_pgtable *data = iof_pgtable_ops_to_data(ops);
  232. av8l_fast_iopte pte, *pgdp, *pudp, *pmdp;
  233. unsigned long pgd;
  234. phys_addr_t phys;
  235. const unsigned long pts = AV8L_FAST_PTE_TYPE_SHIFT;
  236. const unsigned long ptm = AV8L_FAST_PTE_TYPE_MASK;
  237. const unsigned long ptt = AV8L_FAST_PTE_TYPE_TABLE;
  238. const unsigned long ptp = AV8L_FAST_PTE_TYPE_PAGE;
  239. const av8l_fast_iopte am = AV8L_FAST_PTE_ADDR_MASK;
  240. /* TODO: clean up some of these magic numbers... */
  241. pgd = (unsigned long)data->pgd | FAST_PGDNDX(iova);
  242. pgdp = (av8l_fast_iopte *)pgd;
  243. pte = *pgdp;
  244. if (((pte >> pts) & ptm) != ptt)
  245. return 0;
  246. pudp = phys_to_virt((pte & am) | ((iova & 0x3fe00000) >> 18));
  247. pte = *pudp;
  248. if (((pte >> pts) & ptm) != ptt)
  249. return 0;
  250. pmdp = phys_to_virt((pte & am) | ((iova & 0x1ff000) >> 9));
  251. pte = *pmdp;
  252. if (((pte >> pts) & ptm) != ptp)
  253. return 0;
  254. phys = pte & am;
  255. return phys | (iova & 0xfff);
  256. }
  257. static int av8l_fast_map_sg(struct io_pgtable_ops *ops, unsigned long iova,
  258. struct scatterlist *sg, unsigned int nents,
  259. int prot, size_t *size)
  260. {
  261. return -ENODEV;
  262. }
  263. static struct av8l_fast_io_pgtable *
  264. av8l_fast_alloc_pgtable_data(struct io_pgtable_cfg *cfg)
  265. {
  266. struct av8l_fast_io_pgtable *data;
  267. data = kmalloc(sizeof(*data), GFP_KERNEL);
  268. if (!data)
  269. return NULL;
  270. data->iop.ops = (struct io_pgtable_ops) {
  271. .map = av8l_fast_map,
  272. .map_sg = av8l_fast_map_sg,
  273. .unmap = av8l_fast_unmap,
  274. .iova_to_phys = av8l_fast_iova_to_phys,
  275. };
  276. return data;
  277. }
  278. /*
  279. * We need 1 page for the pgd, 4 pages for puds (1GB VA per pud page) and
  280. * 2048 pages for pmds (each pud page contains 512 table entries, each
  281. * pointing to a pmd).
  282. */
  283. #define NUM_PGD_PAGES 1
  284. #define NUM_PUD_PAGES 4
  285. #define NUM_PMD_PAGES 2048
  286. #define NUM_PGTBL_PAGES (NUM_PGD_PAGES + NUM_PUD_PAGES + NUM_PMD_PAGES)
  287. static int
  288. av8l_fast_prepopulate_pgtables(struct av8l_fast_io_pgtable *data,
  289. struct io_pgtable_cfg *cfg, void *cookie)
  290. {
  291. int i, j, pg = 0;
  292. struct page **pages, *page;
  293. pages = kmalloc(sizeof(*pages) * NUM_PGTBL_PAGES, __GFP_NOWARN |
  294. __GFP_NORETRY);
  295. if (!pages)
  296. pages = vmalloc(sizeof(*pages) * NUM_PGTBL_PAGES);
  297. if (!pages)
  298. return -ENOMEM;
  299. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  300. if (!page)
  301. goto err_free_pages_arr;
  302. pages[pg++] = page;
  303. data->pgd = page_address(page);
  304. /*
  305. * We need 2048 entries at level 2 to map 4GB of VA space. A page
  306. * can hold 512 entries, so we need 4 pages.
  307. */
  308. for (i = 0; i < 4; ++i) {
  309. av8l_fast_iopte pte, *ptep;
  310. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  311. if (!page)
  312. goto err_free_pages;
  313. pages[pg++] = page;
  314. data->puds[i] = page_address(page);
  315. pte = page_to_phys(page) | AV8L_FAST_PTE_TYPE_TABLE;
  316. ptep = ((av8l_fast_iopte *)data->pgd) + i;
  317. *ptep = pte;
  318. }
  319. dmac_clean_range(data->pgd, data->pgd + 4);
  320. /*
  321. * We have 4 puds, each of which can point to 512 pmds, so we'll
  322. * have 2048 pmds, each of which can hold 512 ptes, for a grand
  323. * total of 2048*512=1048576 PTEs.
  324. */
  325. for (i = 0; i < 4; ++i) {
  326. for (j = 0; j < 512; ++j) {
  327. av8l_fast_iopte pte, *pudp;
  328. void *addr;
  329. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  330. if (!page)
  331. goto err_free_pages;
  332. pages[pg++] = page;
  333. addr = page_address(page);
  334. dmac_clean_range(addr, addr + SZ_4K);
  335. pte = page_to_phys(page) | AV8L_FAST_PTE_TYPE_TABLE;
  336. pudp = data->puds[i] + j;
  337. *pudp = pte;
  338. }
  339. dmac_clean_range(data->puds[i], data->puds[i] + 512);
  340. }
  341. if (WARN_ON(pg != NUM_PGTBL_PAGES))
  342. goto err_free_pages;
  343. /*
  344. * We map the pmds into a virtually contiguous space so that we
  345. * don't have to traverse the first two levels of the page tables
  346. * to find the appropriate pud. Instead, it will be a simple
  347. * offset from the virtual base of the pmds.
  348. */
  349. data->pmds = vmap(&pages[NUM_PGD_PAGES + NUM_PUD_PAGES], NUM_PMD_PAGES,
  350. VM_IOREMAP, PAGE_KERNEL);
  351. if (!data->pmds)
  352. goto err_free_pages;
  353. data->pages = pages;
  354. return 0;
  355. err_free_pages:
  356. for (i = 0; i < pg; ++i)
  357. __free_page(pages[i]);
  358. err_free_pages_arr:
  359. kvfree(pages);
  360. return -ENOMEM;
  361. }
  362. static struct io_pgtable *
  363. av8l_fast_alloc_pgtable(struct io_pgtable_cfg *cfg, void *cookie)
  364. {
  365. u64 reg;
  366. struct av8l_fast_io_pgtable *data =
  367. av8l_fast_alloc_pgtable_data(cfg);
  368. if (!data)
  369. return NULL;
  370. /* restrict according to the fast map requirements */
  371. cfg->ias = 32;
  372. cfg->pgsize_bitmap = SZ_4K;
  373. /* TCR */
  374. if (cfg->quirks & IO_PGTABLE_QUIRK_QCOM_USE_UPSTREAM_HINT)
  375. reg = (AV8L_FAST_TCR_SH_OS << AV8L_FAST_TCR_SH0_SHIFT) |
  376. (AV8L_FAST_TCR_RGN_NC << AV8L_FAST_TCR_IRGN0_SHIFT) |
  377. (AV8L_FAST_TCR_RGN_WBWA << AV8L_FAST_TCR_ORGN0_SHIFT);
  378. else if (cfg->quirks & IO_PGTABLE_QUIRK_PAGE_TABLE_COHERENT)
  379. reg = (AV8L_FAST_TCR_SH_OS << AV8L_FAST_TCR_SH0_SHIFT) |
  380. (AV8L_FAST_TCR_RGN_WBWA << AV8L_FAST_TCR_IRGN0_SHIFT) |
  381. (AV8L_FAST_TCR_RGN_WBWA << AV8L_FAST_TCR_ORGN0_SHIFT);
  382. else
  383. reg = (AV8L_FAST_TCR_SH_OS << AV8L_FAST_TCR_SH0_SHIFT) |
  384. (AV8L_FAST_TCR_RGN_NC << AV8L_FAST_TCR_IRGN0_SHIFT) |
  385. (AV8L_FAST_TCR_RGN_NC << AV8L_FAST_TCR_ORGN0_SHIFT);
  386. reg |= AV8L_FAST_TCR_TG0_4K;
  387. switch (cfg->oas) {
  388. case 32:
  389. reg |= (AV8L_FAST_TCR_PS_32_BIT << AV8L_FAST_TCR_IPS_SHIFT);
  390. break;
  391. case 36:
  392. reg |= (AV8L_FAST_TCR_PS_36_BIT << AV8L_FAST_TCR_IPS_SHIFT);
  393. break;
  394. case 40:
  395. reg |= (AV8L_FAST_TCR_PS_40_BIT << AV8L_FAST_TCR_IPS_SHIFT);
  396. break;
  397. case 42:
  398. reg |= (AV8L_FAST_TCR_PS_42_BIT << AV8L_FAST_TCR_IPS_SHIFT);
  399. break;
  400. case 44:
  401. reg |= (AV8L_FAST_TCR_PS_44_BIT << AV8L_FAST_TCR_IPS_SHIFT);
  402. break;
  403. case 48:
  404. reg |= (AV8L_FAST_TCR_PS_48_BIT << AV8L_FAST_TCR_IPS_SHIFT);
  405. break;
  406. default:
  407. goto out_free_data;
  408. }
  409. reg |= (64ULL - cfg->ias) << AV8L_FAST_TCR_T0SZ_SHIFT;
  410. reg |= AV8L_FAST_TCR_EPD1_FAULT << AV8L_FAST_TCR_EPD1_SHIFT;
  411. #if defined(CONFIG_ARM)
  412. reg |= ARM_32_LPAE_TCR_EAE;
  413. #endif
  414. cfg->av8l_fast_cfg.tcr = reg;
  415. /* MAIRs */
  416. reg = (AV8L_FAST_MAIR_ATTR_NC
  417. << AV8L_FAST_MAIR_ATTR_SHIFT(AV8L_FAST_MAIR_ATTR_IDX_NC)) |
  418. (AV8L_FAST_MAIR_ATTR_WBRWA
  419. << AV8L_FAST_MAIR_ATTR_SHIFT(AV8L_FAST_MAIR_ATTR_IDX_CACHE)) |
  420. (AV8L_FAST_MAIR_ATTR_DEVICE
  421. << AV8L_FAST_MAIR_ATTR_SHIFT(AV8L_FAST_MAIR_ATTR_IDX_DEV)) |
  422. (AV8L_FAST_MAIR_ATTR_UPSTREAM
  423. << AV8L_FAST_MAIR_ATTR_SHIFT(AV8L_FAST_MAIR_ATTR_IDX_UPSTREAM));
  424. cfg->av8l_fast_cfg.mair[0] = reg;
  425. cfg->av8l_fast_cfg.mair[1] = 0;
  426. /* Allocate all page table memory! */
  427. if (av8l_fast_prepopulate_pgtables(data, cfg, cookie))
  428. goto out_free_data;
  429. cfg->av8l_fast_cfg.pmds = data->pmds;
  430. /* TTBRs */
  431. cfg->av8l_fast_cfg.ttbr[0] = virt_to_phys(data->pgd);
  432. cfg->av8l_fast_cfg.ttbr[1] = 0;
  433. return &data->iop;
  434. out_free_data:
  435. kfree(data);
  436. return NULL;
  437. }
  438. static void av8l_fast_free_pgtable(struct io_pgtable *iop)
  439. {
  440. int i;
  441. struct av8l_fast_io_pgtable *data = iof_pgtable_to_data(iop);
  442. vunmap(data->pmds);
  443. for (i = 0; i < NUM_PGTBL_PAGES; ++i)
  444. __free_page(data->pages[i]);
  445. kvfree(data->pages);
  446. kfree(data);
  447. }
  448. struct io_pgtable_init_fns io_pgtable_av8l_fast_init_fns = {
  449. .alloc = av8l_fast_alloc_pgtable,
  450. .free = av8l_fast_free_pgtable,
  451. };
  452. #ifdef CONFIG_IOMMU_IO_PGTABLE_FAST_SELFTEST
  453. #include <linux/dma-contiguous.h>
  454. static struct io_pgtable_cfg *cfg_cookie;
  455. static void dummy_tlb_flush_all(void *cookie)
  456. {
  457. WARN_ON(cookie != cfg_cookie);
  458. }
  459. static void dummy_tlb_add_flush(unsigned long iova, size_t size, size_t granule,
  460. bool leaf, void *cookie)
  461. {
  462. WARN_ON(cookie != cfg_cookie);
  463. WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
  464. }
  465. static void dummy_tlb_sync(void *cookie)
  466. {
  467. WARN_ON(cookie != cfg_cookie);
  468. }
  469. static struct iommu_gather_ops dummy_tlb_ops __initdata = {
  470. .tlb_flush_all = dummy_tlb_flush_all,
  471. .tlb_add_flush = dummy_tlb_add_flush,
  472. .tlb_sync = dummy_tlb_sync,
  473. };
  474. /*
  475. * Returns true if the iova range is successfully mapped to the contiguous
  476. * phys range in ops.
  477. */
  478. static bool av8l_fast_range_has_specific_mapping(struct io_pgtable_ops *ops,
  479. const unsigned long iova_start,
  480. const phys_addr_t phys_start,
  481. const size_t size)
  482. {
  483. u64 iova = iova_start;
  484. phys_addr_t phys = phys_start;
  485. while (iova < (iova_start + size)) {
  486. /* + 42 just to make sure offsetting is working */
  487. if (ops->iova_to_phys(ops, iova + 42) != (phys + 42))
  488. return false;
  489. iova += SZ_4K;
  490. phys += SZ_4K;
  491. }
  492. return true;
  493. }
  494. static int __init av8l_fast_positive_testing(void)
  495. {
  496. int failed = 0;
  497. u64 iova;
  498. struct io_pgtable_ops *ops;
  499. struct io_pgtable_cfg cfg;
  500. struct av8l_fast_io_pgtable *data;
  501. av8l_fast_iopte *pmds;
  502. u64 max = SZ_1G * 4ULL - 1;
  503. cfg = (struct io_pgtable_cfg) {
  504. .quirks = 0,
  505. .tlb = &dummy_tlb_ops,
  506. .ias = 32,
  507. .oas = 32,
  508. .pgsize_bitmap = SZ_4K,
  509. };
  510. cfg_cookie = &cfg;
  511. ops = alloc_io_pgtable_ops(ARM_V8L_FAST, &cfg, &cfg);
  512. if (WARN_ON(!ops))
  513. return 1;
  514. data = iof_pgtable_ops_to_data(ops);
  515. pmds = data->pmds;
  516. /* map the entire 4GB VA space with 4K map calls */
  517. for (iova = 0; iova < max; iova += SZ_4K) {
  518. if (WARN_ON(ops->map(ops, iova, iova, SZ_4K, IOMMU_READ))) {
  519. failed++;
  520. continue;
  521. }
  522. }
  523. if (WARN_ON(!av8l_fast_range_has_specific_mapping(ops, 0, 0,
  524. max)))
  525. failed++;
  526. /* unmap it all */
  527. for (iova = 0; iova < max; iova += SZ_4K) {
  528. if (WARN_ON(ops->unmap(ops, iova, SZ_4K) != SZ_4K))
  529. failed++;
  530. }
  531. /* sweep up TLB proving PTEs */
  532. av8l_fast_clear_stale_ptes(pmds, false);
  533. /* map the entire 4GB VA space with 8K map calls */
  534. for (iova = 0; iova < max; iova += SZ_8K) {
  535. if (WARN_ON(ops->map(ops, iova, iova, SZ_8K, IOMMU_READ))) {
  536. failed++;
  537. continue;
  538. }
  539. }
  540. if (WARN_ON(!av8l_fast_range_has_specific_mapping(ops, 0, 0,
  541. max)))
  542. failed++;
  543. /* unmap it all with 8K unmap calls */
  544. for (iova = 0; iova < max; iova += SZ_8K) {
  545. if (WARN_ON(ops->unmap(ops, iova, SZ_8K) != SZ_8K))
  546. failed++;
  547. }
  548. /* sweep up TLB proving PTEs */
  549. av8l_fast_clear_stale_ptes(pmds, false);
  550. /* map the entire 4GB VA space with 16K map calls */
  551. for (iova = 0; iova < max; iova += SZ_16K) {
  552. if (WARN_ON(ops->map(ops, iova, iova, SZ_16K, IOMMU_READ))) {
  553. failed++;
  554. continue;
  555. }
  556. }
  557. if (WARN_ON(!av8l_fast_range_has_specific_mapping(ops, 0, 0,
  558. max)))
  559. failed++;
  560. /* unmap it all */
  561. for (iova = 0; iova < max; iova += SZ_16K) {
  562. if (WARN_ON(ops->unmap(ops, iova, SZ_16K) != SZ_16K))
  563. failed++;
  564. }
  565. /* sweep up TLB proving PTEs */
  566. av8l_fast_clear_stale_ptes(pmds, false);
  567. /* map the entire 4GB VA space with 64K map calls */
  568. for (iova = 0; iova < max; iova += SZ_64K) {
  569. if (WARN_ON(ops->map(ops, iova, iova, SZ_64K, IOMMU_READ))) {
  570. failed++;
  571. continue;
  572. }
  573. }
  574. if (WARN_ON(!av8l_fast_range_has_specific_mapping(ops, 0, 0,
  575. max)))
  576. failed++;
  577. /* unmap it all at once */
  578. if (WARN_ON(ops->unmap(ops, 0, max) != max))
  579. failed++;
  580. free_io_pgtable_ops(ops);
  581. return failed;
  582. }
  583. static int __init av8l_fast_do_selftests(void)
  584. {
  585. int failed = 0;
  586. failed += av8l_fast_positive_testing();
  587. pr_err("selftest: completed with %d failures\n", failed);
  588. return 0;
  589. }
  590. subsys_initcall(av8l_fast_do_selftests);
  591. #endif