io-pgtable-arm-v7s.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * CPU-agnostic ARM page table allocator.
  3. *
  4. * ARMv7 Short-descriptor format, supporting
  5. * - Basic memory attributes
  6. * - Simplified access permissions (AP[2:1] model)
  7. * - Backwards-compatible TEX remap
  8. * - Large pages/supersections (if indicated by the caller)
  9. *
  10. * Not supporting:
  11. * - Legacy access permissions (AP[2:0] model)
  12. *
  13. * Almost certainly never supporting:
  14. * - PXN
  15. * - Domains
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. * Copyright (C) 2014-2015 ARM Limited
  30. * Copyright (c) 2014-2015 MediaTek Inc.
  31. */
  32. #define pr_fmt(fmt) "arm-v7s io-pgtable: " fmt
  33. #include <linux/dma-mapping.h>
  34. #include <linux/gfp.h>
  35. #include <linux/iommu.h>
  36. #include <linux/kernel.h>
  37. #include <linux/kmemleak.h>
  38. #include <linux/sizes.h>
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <asm/barrier.h>
  42. #include "io-pgtable.h"
  43. /* Struct accessors */
  44. #define io_pgtable_to_data(x) \
  45. container_of((x), struct arm_v7s_io_pgtable, iop)
  46. #define io_pgtable_ops_to_data(x) \
  47. io_pgtable_to_data(io_pgtable_ops_to_pgtable(x))
  48. /*
  49. * We have 32 bits total; 12 bits resolved at level 1, 8 bits at level 2,
  50. * and 12 bits in a page. With some carefully-chosen coefficients we can
  51. * hide the ugly inconsistencies behind these macros and at least let the
  52. * rest of the code pretend to be somewhat sane.
  53. */
  54. #define ARM_V7S_ADDR_BITS 32
  55. #define _ARM_V7S_LVL_BITS(lvl) (16 - (lvl) * 4)
  56. #define ARM_V7S_LVL_SHIFT(lvl) (ARM_V7S_ADDR_BITS - (4 + 8 * (lvl)))
  57. #define ARM_V7S_TABLE_SHIFT 10
  58. #define ARM_V7S_PTES_PER_LVL(lvl) (1 << _ARM_V7S_LVL_BITS(lvl))
  59. #define ARM_V7S_TABLE_SIZE(lvl) \
  60. (ARM_V7S_PTES_PER_LVL(lvl) * sizeof(arm_v7s_iopte))
  61. #define ARM_V7S_BLOCK_SIZE(lvl) (1UL << ARM_V7S_LVL_SHIFT(lvl))
  62. #define ARM_V7S_LVL_MASK(lvl) ((u32)(~0U << ARM_V7S_LVL_SHIFT(lvl)))
  63. #define ARM_V7S_TABLE_MASK ((u32)(~0U << ARM_V7S_TABLE_SHIFT))
  64. #define _ARM_V7S_IDX_MASK(lvl) (ARM_V7S_PTES_PER_LVL(lvl) - 1)
  65. #define ARM_V7S_LVL_IDX(addr, lvl) ({ \
  66. int _l = lvl; \
  67. ((u32)(addr) >> ARM_V7S_LVL_SHIFT(_l)) & _ARM_V7S_IDX_MASK(_l); \
  68. })
  69. /*
  70. * Large page/supersection entries are effectively a block of 16 page/section
  71. * entries, along the lines of the LPAE contiguous hint, but all with the
  72. * same output address. For want of a better common name we'll call them
  73. * "contiguous" versions of their respective page/section entries here, but
  74. * noting the distinction (WRT to TLB maintenance) that they represent *one*
  75. * entry repeated 16 times, not 16 separate entries (as in the LPAE case).
  76. */
  77. #define ARM_V7S_CONT_PAGES 16
  78. /* PTE type bits: these are all mixed up with XN/PXN bits in most cases */
  79. #define ARM_V7S_PTE_TYPE_TABLE 0x1
  80. #define ARM_V7S_PTE_TYPE_PAGE 0x2
  81. #define ARM_V7S_PTE_TYPE_CONT_PAGE 0x1
  82. #define ARM_V7S_PTE_IS_VALID(pte) (((pte) & 0x3) != 0)
  83. #define ARM_V7S_PTE_IS_TABLE(pte, lvl) (lvl == 1 && ((pte) & ARM_V7S_PTE_TYPE_TABLE))
  84. /* Page table bits */
  85. #define ARM_V7S_ATTR_XN(lvl) BIT(4 * (2 - (lvl)))
  86. #define ARM_V7S_ATTR_B BIT(2)
  87. #define ARM_V7S_ATTR_C BIT(3)
  88. #define ARM_V7S_ATTR_NS_TABLE BIT(3)
  89. #define ARM_V7S_ATTR_NS_SECTION BIT(19)
  90. #define ARM_V7S_CONT_SECTION BIT(18)
  91. #define ARM_V7S_CONT_PAGE_XN_SHIFT 15
  92. /*
  93. * The attribute bits are consistently ordered*, but occupy bits [17:10] of
  94. * a level 1 PTE vs. bits [11:4] at level 2. Thus we define the individual
  95. * fields relative to that 8-bit block, plus a total shift relative to the PTE.
  96. */
  97. #define ARM_V7S_ATTR_SHIFT(lvl) (16 - (lvl) * 6)
  98. #define ARM_V7S_ATTR_MASK 0xff
  99. #define ARM_V7S_ATTR_AP0 BIT(0)
  100. #define ARM_V7S_ATTR_AP1 BIT(1)
  101. #define ARM_V7S_ATTR_AP2 BIT(5)
  102. #define ARM_V7S_ATTR_S BIT(6)
  103. #define ARM_V7S_ATTR_NG BIT(7)
  104. #define ARM_V7S_TEX_SHIFT 2
  105. #define ARM_V7S_TEX_MASK 0x7
  106. #define ARM_V7S_ATTR_TEX(val) (((val) & ARM_V7S_TEX_MASK) << ARM_V7S_TEX_SHIFT)
  107. #define ARM_V7S_ATTR_MTK_4GB BIT(9) /* MTK extend it for 4GB mode */
  108. /* *well, except for TEX on level 2 large pages, of course :( */
  109. #define ARM_V7S_CONT_PAGE_TEX_SHIFT 6
  110. #define ARM_V7S_CONT_PAGE_TEX_MASK (ARM_V7S_TEX_MASK << ARM_V7S_CONT_PAGE_TEX_SHIFT)
  111. /* Simplified access permissions */
  112. #define ARM_V7S_PTE_AF ARM_V7S_ATTR_AP0
  113. #define ARM_V7S_PTE_AP_UNPRIV ARM_V7S_ATTR_AP1
  114. #define ARM_V7S_PTE_AP_RDONLY ARM_V7S_ATTR_AP2
  115. /* Register bits */
  116. #define ARM_V7S_RGN_NC 0
  117. #define ARM_V7S_RGN_WBWA 1
  118. #define ARM_V7S_RGN_WT 2
  119. #define ARM_V7S_RGN_WB 3
  120. #define ARM_V7S_PRRR_TYPE_DEVICE 1
  121. #define ARM_V7S_PRRR_TYPE_NORMAL 2
  122. #define ARM_V7S_PRRR_TR(n, type) (((type) & 0x3) << ((n) * 2))
  123. #define ARM_V7S_PRRR_DS0 BIT(16)
  124. #define ARM_V7S_PRRR_DS1 BIT(17)
  125. #define ARM_V7S_PRRR_NS0 BIT(18)
  126. #define ARM_V7S_PRRR_NS1 BIT(19)
  127. #define ARM_V7S_PRRR_NOS(n) BIT((n) + 24)
  128. #define ARM_V7S_NMRR_IR(n, attr) (((attr) & 0x3) << ((n) * 2))
  129. #define ARM_V7S_NMRR_OR(n, attr) (((attr) & 0x3) << ((n) * 2 + 16))
  130. #define ARM_V7S_TTBR_S BIT(1)
  131. #define ARM_V7S_TTBR_NOS BIT(5)
  132. #define ARM_V7S_TTBR_ORGN_ATTR(attr) (((attr) & 0x3) << 3)
  133. #define ARM_V7S_TTBR_IRGN_ATTR(attr) \
  134. ((((attr) & 0x1) << 6) | (((attr) & 0x2) >> 1))
  135. #define ARM_V7S_TCR_PD1 BIT(5)
  136. typedef u32 arm_v7s_iopte;
  137. static bool selftest_running;
  138. struct arm_v7s_io_pgtable {
  139. struct io_pgtable iop;
  140. arm_v7s_iopte *pgd;
  141. struct kmem_cache *l2_tables;
  142. };
  143. static dma_addr_t __arm_v7s_dma_addr(void *pages)
  144. {
  145. return (dma_addr_t)virt_to_phys(pages);
  146. }
  147. static arm_v7s_iopte *iopte_deref(arm_v7s_iopte pte, int lvl)
  148. {
  149. if (ARM_V7S_PTE_IS_TABLE(pte, lvl))
  150. pte &= ARM_V7S_TABLE_MASK;
  151. else
  152. pte &= ARM_V7S_LVL_MASK(lvl);
  153. return phys_to_virt(pte);
  154. }
  155. static void *__arm_v7s_alloc_table(int lvl, gfp_t gfp,
  156. struct arm_v7s_io_pgtable *data)
  157. {
  158. struct device *dev = data->iop.cfg.iommu_dev;
  159. dma_addr_t dma;
  160. size_t size = ARM_V7S_TABLE_SIZE(lvl);
  161. void *table = NULL;
  162. if (lvl == 1)
  163. table = (void *)__get_dma_pages(__GFP_ZERO, get_order(size));
  164. else if (lvl == 2)
  165. table = kmem_cache_zalloc(data->l2_tables, gfp | GFP_DMA);
  166. if (table && !selftest_running) {
  167. dma = dma_map_single(dev, table, size, DMA_TO_DEVICE);
  168. if (dma_mapping_error(dev, dma))
  169. goto out_free;
  170. /*
  171. * We depend on the IOMMU being able to work with any physical
  172. * address directly, so if the DMA layer suggests otherwise by
  173. * translating or truncating them, that bodes very badly...
  174. */
  175. if (dma != virt_to_phys(table))
  176. goto out_unmap;
  177. }
  178. if (lvl == 2)
  179. kmemleak_ignore(table);
  180. return table;
  181. out_unmap:
  182. dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
  183. dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
  184. out_free:
  185. if (lvl == 1)
  186. free_pages((unsigned long)table, get_order(size));
  187. else
  188. kmem_cache_free(data->l2_tables, table);
  189. return NULL;
  190. }
  191. static void __arm_v7s_free_table(void *table, int lvl,
  192. struct arm_v7s_io_pgtable *data)
  193. {
  194. struct device *dev = data->iop.cfg.iommu_dev;
  195. size_t size = ARM_V7S_TABLE_SIZE(lvl);
  196. if (!selftest_running)
  197. dma_unmap_single(dev, __arm_v7s_dma_addr(table), size,
  198. DMA_TO_DEVICE);
  199. if (lvl == 1)
  200. free_pages((unsigned long)table, get_order(size));
  201. else
  202. kmem_cache_free(data->l2_tables, table);
  203. }
  204. static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries,
  205. struct io_pgtable_cfg *cfg)
  206. {
  207. if (selftest_running)
  208. return;
  209. dma_sync_single_for_device(cfg->iommu_dev, __arm_v7s_dma_addr(ptep),
  210. num_entries * sizeof(*ptep), DMA_TO_DEVICE);
  211. }
  212. static void __arm_v7s_set_pte(arm_v7s_iopte *ptep, arm_v7s_iopte pte,
  213. int num_entries, struct io_pgtable_cfg *cfg)
  214. {
  215. int i;
  216. for (i = 0; i < num_entries; i++)
  217. ptep[i] = pte;
  218. __arm_v7s_pte_sync(ptep, num_entries, cfg);
  219. }
  220. static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl,
  221. struct io_pgtable_cfg *cfg)
  222. {
  223. bool ap = !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS);
  224. arm_v7s_iopte pte = ARM_V7S_ATTR_NG | ARM_V7S_ATTR_S;
  225. if (!(prot & IOMMU_MMIO))
  226. pte |= ARM_V7S_ATTR_TEX(1);
  227. if (ap) {
  228. pte |= ARM_V7S_PTE_AF | ARM_V7S_PTE_AP_UNPRIV;
  229. if (!(prot & IOMMU_WRITE))
  230. pte |= ARM_V7S_PTE_AP_RDONLY;
  231. }
  232. pte <<= ARM_V7S_ATTR_SHIFT(lvl);
  233. if ((prot & IOMMU_NOEXEC) && ap)
  234. pte |= ARM_V7S_ATTR_XN(lvl);
  235. if (prot & IOMMU_MMIO)
  236. pte |= ARM_V7S_ATTR_B;
  237. else if (prot & IOMMU_CACHE)
  238. pte |= ARM_V7S_ATTR_B | ARM_V7S_ATTR_C;
  239. return pte;
  240. }
  241. static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl)
  242. {
  243. int prot = IOMMU_READ;
  244. arm_v7s_iopte attr = pte >> ARM_V7S_ATTR_SHIFT(lvl);
  245. if (!(attr & ARM_V7S_PTE_AP_RDONLY))
  246. prot |= IOMMU_WRITE;
  247. if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0)
  248. prot |= IOMMU_MMIO;
  249. else if (pte & ARM_V7S_ATTR_C)
  250. prot |= IOMMU_CACHE;
  251. if (pte & ARM_V7S_ATTR_XN(lvl))
  252. prot |= IOMMU_NOEXEC;
  253. return prot;
  254. }
  255. static arm_v7s_iopte arm_v7s_pte_to_cont(arm_v7s_iopte pte, int lvl)
  256. {
  257. if (lvl == 1) {
  258. pte |= ARM_V7S_CONT_SECTION;
  259. } else if (lvl == 2) {
  260. arm_v7s_iopte xn = pte & ARM_V7S_ATTR_XN(lvl);
  261. arm_v7s_iopte tex = pte & ARM_V7S_CONT_PAGE_TEX_MASK;
  262. pte ^= xn | tex | ARM_V7S_PTE_TYPE_PAGE;
  263. pte |= (xn << ARM_V7S_CONT_PAGE_XN_SHIFT) |
  264. (tex << ARM_V7S_CONT_PAGE_TEX_SHIFT) |
  265. ARM_V7S_PTE_TYPE_CONT_PAGE;
  266. }
  267. return pte;
  268. }
  269. static arm_v7s_iopte arm_v7s_cont_to_pte(arm_v7s_iopte pte, int lvl)
  270. {
  271. if (lvl == 1) {
  272. pte &= ~ARM_V7S_CONT_SECTION;
  273. } else if (lvl == 2) {
  274. arm_v7s_iopte xn = pte & BIT(ARM_V7S_CONT_PAGE_XN_SHIFT);
  275. arm_v7s_iopte tex = pte & (ARM_V7S_CONT_PAGE_TEX_MASK <<
  276. ARM_V7S_CONT_PAGE_TEX_SHIFT);
  277. pte ^= xn | tex | ARM_V7S_PTE_TYPE_CONT_PAGE;
  278. pte |= (xn >> ARM_V7S_CONT_PAGE_XN_SHIFT) |
  279. (tex >> ARM_V7S_CONT_PAGE_TEX_SHIFT) |
  280. ARM_V7S_PTE_TYPE_PAGE;
  281. }
  282. return pte;
  283. }
  284. static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl)
  285. {
  286. if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte, lvl))
  287. return pte & ARM_V7S_CONT_SECTION;
  288. else if (lvl == 2)
  289. return !(pte & ARM_V7S_PTE_TYPE_PAGE);
  290. return false;
  291. }
  292. static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *, unsigned long,
  293. size_t, int, arm_v7s_iopte *);
  294. static int arm_v7s_init_pte(struct arm_v7s_io_pgtable *data,
  295. unsigned long iova, phys_addr_t paddr, int prot,
  296. int lvl, int num_entries, arm_v7s_iopte *ptep)
  297. {
  298. struct io_pgtable_cfg *cfg = &data->iop.cfg;
  299. arm_v7s_iopte pte = arm_v7s_prot_to_pte(prot, lvl, cfg);
  300. int i;
  301. for (i = 0; i < num_entries; i++)
  302. if (ARM_V7S_PTE_IS_TABLE(ptep[i], lvl)) {
  303. /*
  304. * We need to unmap and free the old table before
  305. * overwriting it with a block entry.
  306. */
  307. arm_v7s_iopte *tblp;
  308. size_t sz = ARM_V7S_BLOCK_SIZE(lvl);
  309. tblp = ptep - ARM_V7S_LVL_IDX(iova, lvl);
  310. if (WARN_ON(__arm_v7s_unmap(data, iova + i * sz,
  311. sz, lvl, tblp) != sz))
  312. return -EINVAL;
  313. } else if (ptep[i]) {
  314. /* We require an unmap first */
  315. WARN_ON(!selftest_running);
  316. return -EEXIST;
  317. }
  318. pte |= ARM_V7S_PTE_TYPE_PAGE;
  319. if (lvl == 1 && (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS))
  320. pte |= ARM_V7S_ATTR_NS_SECTION;
  321. if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB)
  322. pte |= ARM_V7S_ATTR_MTK_4GB;
  323. if (num_entries > 1)
  324. pte = arm_v7s_pte_to_cont(pte, lvl);
  325. pte |= paddr & ARM_V7S_LVL_MASK(lvl);
  326. __arm_v7s_set_pte(ptep, pte, num_entries, cfg);
  327. return 0;
  328. }
  329. static int __arm_v7s_map(struct arm_v7s_io_pgtable *data, unsigned long iova,
  330. phys_addr_t paddr, size_t size, int prot,
  331. int lvl, arm_v7s_iopte *ptep)
  332. {
  333. struct io_pgtable_cfg *cfg = &data->iop.cfg;
  334. arm_v7s_iopte pte, *cptep;
  335. int num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
  336. /* Find our entry at the current level */
  337. ptep += ARM_V7S_LVL_IDX(iova, lvl);
  338. /* If we can install a leaf entry at this level, then do so */
  339. if (num_entries)
  340. return arm_v7s_init_pte(data, iova, paddr, prot,
  341. lvl, num_entries, ptep);
  342. /* We can't allocate tables at the final level */
  343. if (WARN_ON(lvl == 2))
  344. return -EINVAL;
  345. /* Grab a pointer to the next level */
  346. pte = *ptep;
  347. if (!pte) {
  348. cptep = __arm_v7s_alloc_table(lvl + 1, GFP_ATOMIC, data);
  349. if (!cptep)
  350. return -ENOMEM;
  351. pte = virt_to_phys(cptep) | ARM_V7S_PTE_TYPE_TABLE;
  352. if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
  353. pte |= ARM_V7S_ATTR_NS_TABLE;
  354. __arm_v7s_set_pte(ptep, pte, 1, cfg);
  355. } else if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) {
  356. cptep = iopte_deref(pte, lvl);
  357. } else {
  358. /* We require an unmap first */
  359. WARN_ON(!selftest_running);
  360. return -EEXIST;
  361. }
  362. /* Rinse, repeat */
  363. return __arm_v7s_map(data, iova, paddr, size, prot, lvl + 1, cptep);
  364. }
  365. static int arm_v7s_map(struct io_pgtable_ops *ops, unsigned long iova,
  366. phys_addr_t paddr, size_t size, int prot)
  367. {
  368. struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
  369. struct io_pgtable *iop = &data->iop;
  370. int ret;
  371. /* If no access, then nothing to do */
  372. if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
  373. return 0;
  374. ret = __arm_v7s_map(data, iova, paddr, size, prot, 1, data->pgd);
  375. /*
  376. * Synchronise all PTE updates for the new mapping before there's
  377. * a chance for anything to kick off a table walk for the new iova.
  378. */
  379. if (iop->cfg.quirks & IO_PGTABLE_QUIRK_TLBI_ON_MAP) {
  380. io_pgtable_tlb_add_flush(iop, iova, size,
  381. ARM_V7S_BLOCK_SIZE(2), false);
  382. io_pgtable_tlb_sync(iop);
  383. } else {
  384. wmb();
  385. }
  386. return ret;
  387. }
  388. static void arm_v7s_free_pgtable(struct io_pgtable *iop)
  389. {
  390. struct arm_v7s_io_pgtable *data = io_pgtable_to_data(iop);
  391. int i;
  392. for (i = 0; i < ARM_V7S_PTES_PER_LVL(1); i++) {
  393. arm_v7s_iopte pte = data->pgd[i];
  394. if (ARM_V7S_PTE_IS_TABLE(pte, 1))
  395. __arm_v7s_free_table(iopte_deref(pte, 1), 2, data);
  396. }
  397. __arm_v7s_free_table(data->pgd, 1, data);
  398. kmem_cache_destroy(data->l2_tables);
  399. kfree(data);
  400. }
  401. static void arm_v7s_split_cont(struct arm_v7s_io_pgtable *data,
  402. unsigned long iova, int idx, int lvl,
  403. arm_v7s_iopte *ptep)
  404. {
  405. struct io_pgtable *iop = &data->iop;
  406. arm_v7s_iopte pte;
  407. size_t size = ARM_V7S_BLOCK_SIZE(lvl);
  408. int i;
  409. ptep -= idx & (ARM_V7S_CONT_PAGES - 1);
  410. pte = arm_v7s_cont_to_pte(*ptep, lvl);
  411. for (i = 0; i < ARM_V7S_CONT_PAGES; i++) {
  412. ptep[i] = pte;
  413. pte += size;
  414. }
  415. __arm_v7s_pte_sync(ptep, ARM_V7S_CONT_PAGES, &iop->cfg);
  416. size *= ARM_V7S_CONT_PAGES;
  417. io_pgtable_tlb_add_flush(iop, iova, size, size, true);
  418. io_pgtable_tlb_sync(iop);
  419. }
  420. static int arm_v7s_split_blk_unmap(struct arm_v7s_io_pgtable *data,
  421. unsigned long iova, size_t size,
  422. arm_v7s_iopte *ptep)
  423. {
  424. unsigned long blk_start, blk_end, blk_size;
  425. phys_addr_t blk_paddr;
  426. arm_v7s_iopte table = 0;
  427. int prot = arm_v7s_pte_to_prot(*ptep, 1);
  428. blk_size = ARM_V7S_BLOCK_SIZE(1);
  429. blk_start = iova & ARM_V7S_LVL_MASK(1);
  430. blk_end = blk_start + ARM_V7S_BLOCK_SIZE(1);
  431. blk_paddr = *ptep & ARM_V7S_LVL_MASK(1);
  432. for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
  433. arm_v7s_iopte *tablep;
  434. /* Unmap! */
  435. if (blk_start == iova)
  436. continue;
  437. /* __arm_v7s_map expects a pointer to the start of the table */
  438. tablep = &table - ARM_V7S_LVL_IDX(blk_start, 1);
  439. if (__arm_v7s_map(data, blk_start, blk_paddr, size, prot, 1,
  440. tablep) < 0) {
  441. if (table) {
  442. /* Free the table we allocated */
  443. tablep = iopte_deref(table, 1);
  444. __arm_v7s_free_table(tablep, 2, data);
  445. }
  446. return 0; /* Bytes unmapped */
  447. }
  448. }
  449. __arm_v7s_set_pte(ptep, table, 1, &data->iop.cfg);
  450. iova &= ~(blk_size - 1);
  451. io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true);
  452. return size;
  453. }
  454. static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *data,
  455. unsigned long iova, size_t size, int lvl,
  456. arm_v7s_iopte *ptep)
  457. {
  458. arm_v7s_iopte pte[ARM_V7S_CONT_PAGES];
  459. struct io_pgtable *iop = &data->iop;
  460. int idx, i = 0, num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
  461. /* Something went horribly wrong and we ran out of page table */
  462. if (WARN_ON(lvl > 2))
  463. return 0;
  464. idx = ARM_V7S_LVL_IDX(iova, lvl);
  465. ptep += idx;
  466. do {
  467. if (WARN_ON(!ARM_V7S_PTE_IS_VALID(ptep[i])))
  468. return 0;
  469. pte[i] = ptep[i];
  470. } while (++i < num_entries);
  471. /*
  472. * If we've hit a contiguous 'large page' entry at this level, it
  473. * needs splitting first, unless we're unmapping the whole lot.
  474. */
  475. if (num_entries <= 1 && arm_v7s_pte_is_cont(pte[0], lvl))
  476. arm_v7s_split_cont(data, iova, idx, lvl, ptep);
  477. /* If the size matches this level, we're in the right place */
  478. if (num_entries) {
  479. size_t blk_size = ARM_V7S_BLOCK_SIZE(lvl);
  480. __arm_v7s_set_pte(ptep, 0, num_entries, &iop->cfg);
  481. for (i = 0; i < num_entries; i++) {
  482. if (ARM_V7S_PTE_IS_TABLE(pte[i], lvl)) {
  483. /* Also flush any partial walks */
  484. io_pgtable_tlb_add_flush(iop, iova, blk_size,
  485. ARM_V7S_BLOCK_SIZE(lvl + 1), false);
  486. io_pgtable_tlb_sync(iop);
  487. ptep = iopte_deref(pte[i], lvl);
  488. __arm_v7s_free_table(ptep, lvl + 1, data);
  489. } else {
  490. io_pgtable_tlb_add_flush(iop, iova, blk_size,
  491. blk_size, true);
  492. }
  493. iova += blk_size;
  494. }
  495. return size;
  496. } else if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte[0], lvl)) {
  497. /*
  498. * Insert a table at the next level to map the old region,
  499. * minus the part we want to unmap
  500. */
  501. return arm_v7s_split_blk_unmap(data, iova, size, ptep);
  502. }
  503. /* Keep on walkin' */
  504. ptep = iopte_deref(pte[0], lvl);
  505. return __arm_v7s_unmap(data, iova, size, lvl + 1, ptep);
  506. }
  507. static int arm_v7s_unmap(struct io_pgtable_ops *ops, unsigned long iova,
  508. size_t size)
  509. {
  510. struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
  511. size_t unmapped;
  512. unmapped = __arm_v7s_unmap(data, iova, size, 1, data->pgd);
  513. if (unmapped)
  514. io_pgtable_tlb_sync(&data->iop);
  515. return unmapped;
  516. }
  517. static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops,
  518. unsigned long iova)
  519. {
  520. struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
  521. arm_v7s_iopte *ptep = data->pgd, pte;
  522. int lvl = 0;
  523. u32 mask;
  524. do {
  525. pte = ptep[ARM_V7S_LVL_IDX(iova, ++lvl)];
  526. ptep = iopte_deref(pte, lvl);
  527. } while (ARM_V7S_PTE_IS_TABLE(pte, lvl));
  528. if (!ARM_V7S_PTE_IS_VALID(pte))
  529. return 0;
  530. mask = ARM_V7S_LVL_MASK(lvl);
  531. if (arm_v7s_pte_is_cont(pte, lvl))
  532. mask *= ARM_V7S_CONT_PAGES;
  533. return (pte & mask) | (iova & ~mask);
  534. }
  535. static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg,
  536. void *cookie)
  537. {
  538. struct arm_v7s_io_pgtable *data;
  539. #ifdef PHYS_OFFSET
  540. if (upper_32_bits(PHYS_OFFSET))
  541. return NULL;
  542. #endif
  543. if (cfg->ias > ARM_V7S_ADDR_BITS || cfg->oas > ARM_V7S_ADDR_BITS)
  544. return NULL;
  545. if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
  546. IO_PGTABLE_QUIRK_NO_PERMS |
  547. IO_PGTABLE_QUIRK_TLBI_ON_MAP |
  548. IO_PGTABLE_QUIRK_ARM_MTK_4GB))
  549. return NULL;
  550. /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */
  551. if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB &&
  552. !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS))
  553. return NULL;
  554. data = kmalloc(sizeof(*data), GFP_KERNEL);
  555. if (!data)
  556. return NULL;
  557. data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2",
  558. ARM_V7S_TABLE_SIZE(2),
  559. ARM_V7S_TABLE_SIZE(2),
  560. SLAB_CACHE_DMA, NULL);
  561. if (!data->l2_tables)
  562. goto out_free_data;
  563. data->iop.ops = (struct io_pgtable_ops) {
  564. .map = arm_v7s_map,
  565. .unmap = arm_v7s_unmap,
  566. .iova_to_phys = arm_v7s_iova_to_phys,
  567. };
  568. /* We have to do this early for __arm_v7s_alloc_table to work... */
  569. data->iop.cfg = *cfg;
  570. /*
  571. * Unless the IOMMU driver indicates supersection support by
  572. * having SZ_16M set in the initial bitmap, they won't be used.
  573. */
  574. cfg->pgsize_bitmap &= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
  575. /* TCR: T0SZ=0, disable TTBR1 */
  576. cfg->arm_v7s_cfg.tcr = ARM_V7S_TCR_PD1;
  577. /*
  578. * TEX remap: the indices used map to the closest equivalent types
  579. * under the non-TEX-remap interpretation of those attribute bits,
  580. * excepting various implementation-defined aspects of shareability.
  581. */
  582. cfg->arm_v7s_cfg.prrr = ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE) |
  583. ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL) |
  584. ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL) |
  585. ARM_V7S_PRRR_DS0 | ARM_V7S_PRRR_DS1 |
  586. ARM_V7S_PRRR_NS1 | ARM_V7S_PRRR_NOS(7);
  587. cfg->arm_v7s_cfg.nmrr = ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA) |
  588. ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA);
  589. /* Looking good; allocate a pgd */
  590. data->pgd = __arm_v7s_alloc_table(1, GFP_KERNEL, data);
  591. if (!data->pgd)
  592. goto out_free_data;
  593. /* Ensure the empty pgd is visible before any actual TTBR write */
  594. wmb();
  595. /* TTBRs */
  596. cfg->arm_v7s_cfg.ttbr[0] = virt_to_phys(data->pgd) |
  597. ARM_V7S_TTBR_S | ARM_V7S_TTBR_NOS |
  598. ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |
  599. ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA);
  600. cfg->arm_v7s_cfg.ttbr[1] = 0;
  601. return &data->iop;
  602. out_free_data:
  603. kmem_cache_destroy(data->l2_tables);
  604. kfree(data);
  605. return NULL;
  606. }
  607. struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
  608. .alloc = arm_v7s_alloc_pgtable,
  609. .free = arm_v7s_free_pgtable,
  610. };
  611. #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
  612. static struct io_pgtable_cfg *cfg_cookie;
  613. static void dummy_tlb_flush_all(void *cookie)
  614. {
  615. WARN_ON(cookie != cfg_cookie);
  616. }
  617. static void dummy_tlb_add_flush(unsigned long iova, size_t size,
  618. size_t granule, bool leaf, void *cookie)
  619. {
  620. WARN_ON(cookie != cfg_cookie);
  621. WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
  622. }
  623. static void dummy_tlb_sync(void *cookie)
  624. {
  625. WARN_ON(cookie != cfg_cookie);
  626. }
  627. static struct iommu_gather_ops dummy_tlb_ops = {
  628. .tlb_flush_all = dummy_tlb_flush_all,
  629. .tlb_add_flush = dummy_tlb_add_flush,
  630. .tlb_sync = dummy_tlb_sync,
  631. };
  632. #define __FAIL(ops) ({ \
  633. WARN(1, "selftest: test failed\n"); \
  634. selftest_running = false; \
  635. -EFAULT; \
  636. })
  637. static int __init arm_v7s_do_selftests(void)
  638. {
  639. struct io_pgtable_ops *ops;
  640. struct io_pgtable_cfg cfg = {
  641. .tlb = &dummy_tlb_ops,
  642. .oas = 32,
  643. .ias = 32,
  644. .quirks = IO_PGTABLE_QUIRK_ARM_NS,
  645. .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
  646. };
  647. unsigned int iova, size, iova_start;
  648. unsigned int i, loopnr = 0;
  649. selftest_running = true;
  650. cfg_cookie = &cfg;
  651. ops = alloc_io_pgtable_ops(ARM_V7S, &cfg, &cfg);
  652. if (!ops) {
  653. pr_err("selftest: failed to allocate io pgtable ops\n");
  654. return -EINVAL;
  655. }
  656. /*
  657. * Initial sanity checks.
  658. * Empty page tables shouldn't provide any translations.
  659. */
  660. if (ops->iova_to_phys(ops, 42))
  661. return __FAIL(ops);
  662. if (ops->iova_to_phys(ops, SZ_1G + 42))
  663. return __FAIL(ops);
  664. if (ops->iova_to_phys(ops, SZ_2G + 42))
  665. return __FAIL(ops);
  666. /*
  667. * Distinct mappings of different granule sizes.
  668. */
  669. iova = 0;
  670. i = find_first_bit(&cfg.pgsize_bitmap, BITS_PER_LONG);
  671. while (i != BITS_PER_LONG) {
  672. size = 1UL << i;
  673. if (ops->map(ops, iova, iova, size, IOMMU_READ |
  674. IOMMU_WRITE |
  675. IOMMU_NOEXEC |
  676. IOMMU_CACHE))
  677. return __FAIL(ops);
  678. /* Overlapping mappings */
  679. if (!ops->map(ops, iova, iova + size, size,
  680. IOMMU_READ | IOMMU_NOEXEC))
  681. return __FAIL(ops);
  682. if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
  683. return __FAIL(ops);
  684. iova += SZ_16M;
  685. i++;
  686. i = find_next_bit(&cfg.pgsize_bitmap, BITS_PER_LONG, i);
  687. loopnr++;
  688. }
  689. /* Partial unmap */
  690. i = 1;
  691. size = 1UL << __ffs(cfg.pgsize_bitmap);
  692. while (i < loopnr) {
  693. iova_start = i * SZ_16M;
  694. if (ops->unmap(ops, iova_start + size, size) != size)
  695. return __FAIL(ops);
  696. /* Remap of partial unmap */
  697. if (ops->map(ops, iova_start + size, size, size, IOMMU_READ))
  698. return __FAIL(ops);
  699. if (ops->iova_to_phys(ops, iova_start + size + 42)
  700. != (size + 42))
  701. return __FAIL(ops);
  702. i++;
  703. }
  704. /* Full unmap */
  705. iova = 0;
  706. i = find_first_bit(&cfg.pgsize_bitmap, BITS_PER_LONG);
  707. while (i != BITS_PER_LONG) {
  708. size = 1UL << i;
  709. if (ops->unmap(ops, iova, size) != size)
  710. return __FAIL(ops);
  711. if (ops->iova_to_phys(ops, iova + 42))
  712. return __FAIL(ops);
  713. /* Remap full block */
  714. if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
  715. return __FAIL(ops);
  716. if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
  717. return __FAIL(ops);
  718. iova += SZ_16M;
  719. i++;
  720. i = find_next_bit(&cfg.pgsize_bitmap, BITS_PER_LONG, i);
  721. }
  722. free_io_pgtable_ops(ops);
  723. selftest_running = false;
  724. pr_info("self test ok\n");
  725. return 0;
  726. }
  727. subsys_initcall(arm_v7s_do_selftests);
  728. #endif