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. kmemleak_ignore(table);
  179. return table;
  180. out_unmap:
  181. dev_err(dev, "Cannot accommodate DMA translation for IOMMU page tables\n");
  182. dma_unmap_single(dev, dma, size, DMA_TO_DEVICE);
  183. out_free:
  184. if (lvl == 1)
  185. free_pages((unsigned long)table, get_order(size));
  186. else
  187. kmem_cache_free(data->l2_tables, table);
  188. return NULL;
  189. }
  190. static void __arm_v7s_free_table(void *table, int lvl,
  191. struct arm_v7s_io_pgtable *data)
  192. {
  193. struct device *dev = data->iop.cfg.iommu_dev;
  194. size_t size = ARM_V7S_TABLE_SIZE(lvl);
  195. if (!selftest_running)
  196. dma_unmap_single(dev, __arm_v7s_dma_addr(table), size,
  197. DMA_TO_DEVICE);
  198. if (lvl == 1)
  199. free_pages((unsigned long)table, get_order(size));
  200. else
  201. kmem_cache_free(data->l2_tables, table);
  202. }
  203. static void __arm_v7s_pte_sync(arm_v7s_iopte *ptep, int num_entries,
  204. struct io_pgtable_cfg *cfg)
  205. {
  206. if (selftest_running)
  207. return;
  208. dma_sync_single_for_device(cfg->iommu_dev, __arm_v7s_dma_addr(ptep),
  209. num_entries * sizeof(*ptep), DMA_TO_DEVICE);
  210. }
  211. static void __arm_v7s_set_pte(arm_v7s_iopte *ptep, arm_v7s_iopte pte,
  212. int num_entries, struct io_pgtable_cfg *cfg)
  213. {
  214. int i;
  215. for (i = 0; i < num_entries; i++)
  216. ptep[i] = pte;
  217. __arm_v7s_pte_sync(ptep, num_entries, cfg);
  218. }
  219. static arm_v7s_iopte arm_v7s_prot_to_pte(int prot, int lvl,
  220. struct io_pgtable_cfg *cfg)
  221. {
  222. bool ap = !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS);
  223. arm_v7s_iopte pte = ARM_V7S_ATTR_NG | ARM_V7S_ATTR_S;
  224. if (!(prot & IOMMU_MMIO))
  225. pte |= ARM_V7S_ATTR_TEX(1);
  226. if (ap) {
  227. pte |= ARM_V7S_PTE_AF | ARM_V7S_PTE_AP_UNPRIV;
  228. if (!(prot & IOMMU_WRITE))
  229. pte |= ARM_V7S_PTE_AP_RDONLY;
  230. }
  231. pte <<= ARM_V7S_ATTR_SHIFT(lvl);
  232. if ((prot & IOMMU_NOEXEC) && ap)
  233. pte |= ARM_V7S_ATTR_XN(lvl);
  234. if (prot & IOMMU_MMIO)
  235. pte |= ARM_V7S_ATTR_B;
  236. else if (prot & IOMMU_CACHE)
  237. pte |= ARM_V7S_ATTR_B | ARM_V7S_ATTR_C;
  238. return pte;
  239. }
  240. static int arm_v7s_pte_to_prot(arm_v7s_iopte pte, int lvl)
  241. {
  242. int prot = IOMMU_READ;
  243. arm_v7s_iopte attr = pte >> ARM_V7S_ATTR_SHIFT(lvl);
  244. if (!(attr & ARM_V7S_PTE_AP_RDONLY))
  245. prot |= IOMMU_WRITE;
  246. if ((attr & (ARM_V7S_TEX_MASK << ARM_V7S_TEX_SHIFT)) == 0)
  247. prot |= IOMMU_MMIO;
  248. else if (pte & ARM_V7S_ATTR_C)
  249. prot |= IOMMU_CACHE;
  250. if (pte & ARM_V7S_ATTR_XN(lvl))
  251. prot |= IOMMU_NOEXEC;
  252. return prot;
  253. }
  254. static arm_v7s_iopte arm_v7s_pte_to_cont(arm_v7s_iopte pte, int lvl)
  255. {
  256. if (lvl == 1) {
  257. pte |= ARM_V7S_CONT_SECTION;
  258. } else if (lvl == 2) {
  259. arm_v7s_iopte xn = pte & ARM_V7S_ATTR_XN(lvl);
  260. arm_v7s_iopte tex = pte & ARM_V7S_CONT_PAGE_TEX_MASK;
  261. pte ^= xn | tex | ARM_V7S_PTE_TYPE_PAGE;
  262. pte |= (xn << ARM_V7S_CONT_PAGE_XN_SHIFT) |
  263. (tex << ARM_V7S_CONT_PAGE_TEX_SHIFT) |
  264. ARM_V7S_PTE_TYPE_CONT_PAGE;
  265. }
  266. return pte;
  267. }
  268. static arm_v7s_iopte arm_v7s_cont_to_pte(arm_v7s_iopte pte, int lvl)
  269. {
  270. if (lvl == 1) {
  271. pte &= ~ARM_V7S_CONT_SECTION;
  272. } else if (lvl == 2) {
  273. arm_v7s_iopte xn = pte & BIT(ARM_V7S_CONT_PAGE_XN_SHIFT);
  274. arm_v7s_iopte tex = pte & (ARM_V7S_CONT_PAGE_TEX_MASK <<
  275. ARM_V7S_CONT_PAGE_TEX_SHIFT);
  276. pte ^= xn | tex | ARM_V7S_PTE_TYPE_CONT_PAGE;
  277. pte |= (xn >> ARM_V7S_CONT_PAGE_XN_SHIFT) |
  278. (tex >> ARM_V7S_CONT_PAGE_TEX_SHIFT) |
  279. ARM_V7S_PTE_TYPE_PAGE;
  280. }
  281. return pte;
  282. }
  283. static bool arm_v7s_pte_is_cont(arm_v7s_iopte pte, int lvl)
  284. {
  285. if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte, lvl))
  286. return pte & ARM_V7S_CONT_SECTION;
  287. else if (lvl == 2)
  288. return !(pte & ARM_V7S_PTE_TYPE_PAGE);
  289. return false;
  290. }
  291. static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *, unsigned long,
  292. size_t, int, arm_v7s_iopte *);
  293. static int arm_v7s_init_pte(struct arm_v7s_io_pgtable *data,
  294. unsigned long iova, phys_addr_t paddr, int prot,
  295. int lvl, int num_entries, arm_v7s_iopte *ptep)
  296. {
  297. struct io_pgtable_cfg *cfg = &data->iop.cfg;
  298. arm_v7s_iopte pte = arm_v7s_prot_to_pte(prot, lvl, cfg);
  299. int i;
  300. for (i = 0; i < num_entries; i++)
  301. if (ARM_V7S_PTE_IS_TABLE(ptep[i], lvl)) {
  302. /*
  303. * We need to unmap and free the old table before
  304. * overwriting it with a block entry.
  305. */
  306. arm_v7s_iopte *tblp;
  307. size_t sz = ARM_V7S_BLOCK_SIZE(lvl);
  308. tblp = ptep - ARM_V7S_LVL_IDX(iova, lvl);
  309. if (WARN_ON(__arm_v7s_unmap(data, iova + i * sz,
  310. sz, lvl, tblp) != sz))
  311. return -EINVAL;
  312. } else if (ptep[i]) {
  313. /* We require an unmap first */
  314. WARN_ON(!selftest_running);
  315. return -EEXIST;
  316. }
  317. pte |= ARM_V7S_PTE_TYPE_PAGE;
  318. if (lvl == 1 && (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS))
  319. pte |= ARM_V7S_ATTR_NS_SECTION;
  320. if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB)
  321. pte |= ARM_V7S_ATTR_MTK_4GB;
  322. if (num_entries > 1)
  323. pte = arm_v7s_pte_to_cont(pte, lvl);
  324. pte |= paddr & ARM_V7S_LVL_MASK(lvl);
  325. __arm_v7s_set_pte(ptep, pte, num_entries, cfg);
  326. return 0;
  327. }
  328. static int __arm_v7s_map(struct arm_v7s_io_pgtable *data, unsigned long iova,
  329. phys_addr_t paddr, size_t size, int prot,
  330. int lvl, arm_v7s_iopte *ptep)
  331. {
  332. struct io_pgtable_cfg *cfg = &data->iop.cfg;
  333. arm_v7s_iopte pte, *cptep;
  334. int num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
  335. /* Find our entry at the current level */
  336. ptep += ARM_V7S_LVL_IDX(iova, lvl);
  337. /* If we can install a leaf entry at this level, then do so */
  338. if (num_entries)
  339. return arm_v7s_init_pte(data, iova, paddr, prot,
  340. lvl, num_entries, ptep);
  341. /* We can't allocate tables at the final level */
  342. if (WARN_ON(lvl == 2))
  343. return -EINVAL;
  344. /* Grab a pointer to the next level */
  345. pte = *ptep;
  346. if (!pte) {
  347. cptep = __arm_v7s_alloc_table(lvl + 1, GFP_ATOMIC, data);
  348. if (!cptep)
  349. return -ENOMEM;
  350. pte = virt_to_phys(cptep) | ARM_V7S_PTE_TYPE_TABLE;
  351. if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_NS)
  352. pte |= ARM_V7S_ATTR_NS_TABLE;
  353. __arm_v7s_set_pte(ptep, pte, 1, cfg);
  354. } else if (ARM_V7S_PTE_IS_TABLE(pte, lvl)) {
  355. cptep = iopte_deref(pte, lvl);
  356. } else {
  357. /* We require an unmap first */
  358. WARN_ON(!selftest_running);
  359. return -EEXIST;
  360. }
  361. /* Rinse, repeat */
  362. return __arm_v7s_map(data, iova, paddr, size, prot, lvl + 1, cptep);
  363. }
  364. static int arm_v7s_map(struct io_pgtable_ops *ops, unsigned long iova,
  365. phys_addr_t paddr, size_t size, int prot)
  366. {
  367. struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
  368. struct io_pgtable *iop = &data->iop;
  369. int ret;
  370. /* If no access, then nothing to do */
  371. if (!(prot & (IOMMU_READ | IOMMU_WRITE)))
  372. return 0;
  373. ret = __arm_v7s_map(data, iova, paddr, size, prot, 1, data->pgd);
  374. /*
  375. * Synchronise all PTE updates for the new mapping before there's
  376. * a chance for anything to kick off a table walk for the new iova.
  377. */
  378. if (iop->cfg.quirks & IO_PGTABLE_QUIRK_TLBI_ON_MAP) {
  379. io_pgtable_tlb_add_flush(iop, iova, size,
  380. ARM_V7S_BLOCK_SIZE(2), false);
  381. io_pgtable_tlb_sync(iop);
  382. } else {
  383. wmb();
  384. }
  385. return ret;
  386. }
  387. static void arm_v7s_free_pgtable(struct io_pgtable *iop)
  388. {
  389. struct arm_v7s_io_pgtable *data = io_pgtable_to_data(iop);
  390. int i;
  391. for (i = 0; i < ARM_V7S_PTES_PER_LVL(1); i++) {
  392. arm_v7s_iopte pte = data->pgd[i];
  393. if (ARM_V7S_PTE_IS_TABLE(pte, 1))
  394. __arm_v7s_free_table(iopte_deref(pte, 1), 2, data);
  395. }
  396. __arm_v7s_free_table(data->pgd, 1, data);
  397. kmem_cache_destroy(data->l2_tables);
  398. kfree(data);
  399. }
  400. static void arm_v7s_split_cont(struct arm_v7s_io_pgtable *data,
  401. unsigned long iova, int idx, int lvl,
  402. arm_v7s_iopte *ptep)
  403. {
  404. struct io_pgtable *iop = &data->iop;
  405. arm_v7s_iopte pte;
  406. size_t size = ARM_V7S_BLOCK_SIZE(lvl);
  407. int i;
  408. ptep -= idx & (ARM_V7S_CONT_PAGES - 1);
  409. pte = arm_v7s_cont_to_pte(*ptep, lvl);
  410. for (i = 0; i < ARM_V7S_CONT_PAGES; i++) {
  411. ptep[i] = pte;
  412. pte += size;
  413. }
  414. __arm_v7s_pte_sync(ptep, ARM_V7S_CONT_PAGES, &iop->cfg);
  415. size *= ARM_V7S_CONT_PAGES;
  416. io_pgtable_tlb_add_flush(iop, iova, size, size, true);
  417. io_pgtable_tlb_sync(iop);
  418. }
  419. static int arm_v7s_split_blk_unmap(struct arm_v7s_io_pgtable *data,
  420. unsigned long iova, size_t size,
  421. arm_v7s_iopte *ptep)
  422. {
  423. unsigned long blk_start, blk_end, blk_size;
  424. phys_addr_t blk_paddr;
  425. arm_v7s_iopte table = 0;
  426. int prot = arm_v7s_pte_to_prot(*ptep, 1);
  427. blk_size = ARM_V7S_BLOCK_SIZE(1);
  428. blk_start = iova & ARM_V7S_LVL_MASK(1);
  429. blk_end = blk_start + ARM_V7S_BLOCK_SIZE(1);
  430. blk_paddr = *ptep & ARM_V7S_LVL_MASK(1);
  431. for (; blk_start < blk_end; blk_start += size, blk_paddr += size) {
  432. arm_v7s_iopte *tablep;
  433. /* Unmap! */
  434. if (blk_start == iova)
  435. continue;
  436. /* __arm_v7s_map expects a pointer to the start of the table */
  437. tablep = &table - ARM_V7S_LVL_IDX(blk_start, 1);
  438. if (__arm_v7s_map(data, blk_start, blk_paddr, size, prot, 1,
  439. tablep) < 0) {
  440. if (table) {
  441. /* Free the table we allocated */
  442. tablep = iopte_deref(table, 1);
  443. __arm_v7s_free_table(tablep, 2, data);
  444. }
  445. return 0; /* Bytes unmapped */
  446. }
  447. }
  448. __arm_v7s_set_pte(ptep, table, 1, &data->iop.cfg);
  449. iova &= ~(blk_size - 1);
  450. io_pgtable_tlb_add_flush(&data->iop, iova, blk_size, blk_size, true);
  451. return size;
  452. }
  453. static int __arm_v7s_unmap(struct arm_v7s_io_pgtable *data,
  454. unsigned long iova, size_t size, int lvl,
  455. arm_v7s_iopte *ptep)
  456. {
  457. arm_v7s_iopte pte[ARM_V7S_CONT_PAGES];
  458. struct io_pgtable *iop = &data->iop;
  459. int idx, i = 0, num_entries = size >> ARM_V7S_LVL_SHIFT(lvl);
  460. /* Something went horribly wrong and we ran out of page table */
  461. if (WARN_ON(lvl > 2))
  462. return 0;
  463. idx = ARM_V7S_LVL_IDX(iova, lvl);
  464. ptep += idx;
  465. do {
  466. if (WARN_ON(!ARM_V7S_PTE_IS_VALID(ptep[i])))
  467. return 0;
  468. pte[i] = ptep[i];
  469. } while (++i < num_entries);
  470. /*
  471. * If we've hit a contiguous 'large page' entry at this level, it
  472. * needs splitting first, unless we're unmapping the whole lot.
  473. */
  474. if (num_entries <= 1 && arm_v7s_pte_is_cont(pte[0], lvl))
  475. arm_v7s_split_cont(data, iova, idx, lvl, ptep);
  476. /* If the size matches this level, we're in the right place */
  477. if (num_entries) {
  478. size_t blk_size = ARM_V7S_BLOCK_SIZE(lvl);
  479. __arm_v7s_set_pte(ptep, 0, num_entries, &iop->cfg);
  480. for (i = 0; i < num_entries; i++) {
  481. if (ARM_V7S_PTE_IS_TABLE(pte[i], lvl)) {
  482. /* Also flush any partial walks */
  483. io_pgtable_tlb_add_flush(iop, iova, blk_size,
  484. ARM_V7S_BLOCK_SIZE(lvl + 1), false);
  485. io_pgtable_tlb_sync(iop);
  486. ptep = iopte_deref(pte[i], lvl);
  487. __arm_v7s_free_table(ptep, lvl + 1, data);
  488. } else {
  489. io_pgtable_tlb_add_flush(iop, iova, blk_size,
  490. blk_size, true);
  491. }
  492. iova += blk_size;
  493. }
  494. return size;
  495. } else if (lvl == 1 && !ARM_V7S_PTE_IS_TABLE(pte[0], lvl)) {
  496. /*
  497. * Insert a table at the next level to map the old region,
  498. * minus the part we want to unmap
  499. */
  500. return arm_v7s_split_blk_unmap(data, iova, size, ptep);
  501. }
  502. /* Keep on walkin' */
  503. ptep = iopte_deref(pte[0], lvl);
  504. return __arm_v7s_unmap(data, iova, size, lvl + 1, ptep);
  505. }
  506. static int arm_v7s_unmap(struct io_pgtable_ops *ops, unsigned long iova,
  507. size_t size)
  508. {
  509. struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
  510. size_t unmapped;
  511. unmapped = __arm_v7s_unmap(data, iova, size, 1, data->pgd);
  512. if (unmapped)
  513. io_pgtable_tlb_sync(&data->iop);
  514. return unmapped;
  515. }
  516. static phys_addr_t arm_v7s_iova_to_phys(struct io_pgtable_ops *ops,
  517. unsigned long iova)
  518. {
  519. struct arm_v7s_io_pgtable *data = io_pgtable_ops_to_data(ops);
  520. arm_v7s_iopte *ptep = data->pgd, pte;
  521. int lvl = 0;
  522. u32 mask;
  523. do {
  524. pte = ptep[ARM_V7S_LVL_IDX(iova, ++lvl)];
  525. ptep = iopte_deref(pte, lvl);
  526. } while (ARM_V7S_PTE_IS_TABLE(pte, lvl));
  527. if (!ARM_V7S_PTE_IS_VALID(pte))
  528. return 0;
  529. mask = ARM_V7S_LVL_MASK(lvl);
  530. if (arm_v7s_pte_is_cont(pte, lvl))
  531. mask *= ARM_V7S_CONT_PAGES;
  532. return (pte & mask) | (iova & ~mask);
  533. }
  534. static struct io_pgtable *arm_v7s_alloc_pgtable(struct io_pgtable_cfg *cfg,
  535. void *cookie)
  536. {
  537. struct arm_v7s_io_pgtable *data;
  538. #ifdef PHYS_OFFSET
  539. if (upper_32_bits(PHYS_OFFSET))
  540. return NULL;
  541. #endif
  542. if (cfg->ias > ARM_V7S_ADDR_BITS || cfg->oas > ARM_V7S_ADDR_BITS)
  543. return NULL;
  544. if (cfg->quirks & ~(IO_PGTABLE_QUIRK_ARM_NS |
  545. IO_PGTABLE_QUIRK_NO_PERMS |
  546. IO_PGTABLE_QUIRK_TLBI_ON_MAP |
  547. IO_PGTABLE_QUIRK_ARM_MTK_4GB))
  548. return NULL;
  549. /* If ARM_MTK_4GB is enabled, the NO_PERMS is also expected. */
  550. if (cfg->quirks & IO_PGTABLE_QUIRK_ARM_MTK_4GB &&
  551. !(cfg->quirks & IO_PGTABLE_QUIRK_NO_PERMS))
  552. return NULL;
  553. data = kmalloc(sizeof(*data), GFP_KERNEL);
  554. if (!data)
  555. return NULL;
  556. data->l2_tables = kmem_cache_create("io-pgtable_armv7s_l2",
  557. ARM_V7S_TABLE_SIZE(2),
  558. ARM_V7S_TABLE_SIZE(2),
  559. SLAB_CACHE_DMA, NULL);
  560. if (!data->l2_tables)
  561. goto out_free_data;
  562. data->iop.ops = (struct io_pgtable_ops) {
  563. .map = arm_v7s_map,
  564. .unmap = arm_v7s_unmap,
  565. .iova_to_phys = arm_v7s_iova_to_phys,
  566. };
  567. /* We have to do this early for __arm_v7s_alloc_table to work... */
  568. data->iop.cfg = *cfg;
  569. /*
  570. * Unless the IOMMU driver indicates supersection support by
  571. * having SZ_16M set in the initial bitmap, they won't be used.
  572. */
  573. cfg->pgsize_bitmap &= SZ_4K | SZ_64K | SZ_1M | SZ_16M;
  574. /* TCR: T0SZ=0, disable TTBR1 */
  575. cfg->arm_v7s_cfg.tcr = ARM_V7S_TCR_PD1;
  576. /*
  577. * TEX remap: the indices used map to the closest equivalent types
  578. * under the non-TEX-remap interpretation of those attribute bits,
  579. * excepting various implementation-defined aspects of shareability.
  580. */
  581. cfg->arm_v7s_cfg.prrr = ARM_V7S_PRRR_TR(1, ARM_V7S_PRRR_TYPE_DEVICE) |
  582. ARM_V7S_PRRR_TR(4, ARM_V7S_PRRR_TYPE_NORMAL) |
  583. ARM_V7S_PRRR_TR(7, ARM_V7S_PRRR_TYPE_NORMAL) |
  584. ARM_V7S_PRRR_DS0 | ARM_V7S_PRRR_DS1 |
  585. ARM_V7S_PRRR_NS1 | ARM_V7S_PRRR_NOS(7);
  586. cfg->arm_v7s_cfg.nmrr = ARM_V7S_NMRR_IR(7, ARM_V7S_RGN_WBWA) |
  587. ARM_V7S_NMRR_OR(7, ARM_V7S_RGN_WBWA);
  588. /* Looking good; allocate a pgd */
  589. data->pgd = __arm_v7s_alloc_table(1, GFP_KERNEL, data);
  590. if (!data->pgd)
  591. goto out_free_data;
  592. /* Ensure the empty pgd is visible before any actual TTBR write */
  593. wmb();
  594. /* TTBRs */
  595. cfg->arm_v7s_cfg.ttbr[0] = virt_to_phys(data->pgd) |
  596. ARM_V7S_TTBR_S | ARM_V7S_TTBR_NOS |
  597. ARM_V7S_TTBR_IRGN_ATTR(ARM_V7S_RGN_WBWA) |
  598. ARM_V7S_TTBR_ORGN_ATTR(ARM_V7S_RGN_WBWA);
  599. cfg->arm_v7s_cfg.ttbr[1] = 0;
  600. return &data->iop;
  601. out_free_data:
  602. kmem_cache_destroy(data->l2_tables);
  603. kfree(data);
  604. return NULL;
  605. }
  606. struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns = {
  607. .alloc = arm_v7s_alloc_pgtable,
  608. .free = arm_v7s_free_pgtable,
  609. };
  610. #ifdef CONFIG_IOMMU_IO_PGTABLE_ARMV7S_SELFTEST
  611. static struct io_pgtable_cfg *cfg_cookie;
  612. static void dummy_tlb_flush_all(void *cookie)
  613. {
  614. WARN_ON(cookie != cfg_cookie);
  615. }
  616. static void dummy_tlb_add_flush(unsigned long iova, size_t size,
  617. size_t granule, bool leaf, void *cookie)
  618. {
  619. WARN_ON(cookie != cfg_cookie);
  620. WARN_ON(!(size & cfg_cookie->pgsize_bitmap));
  621. }
  622. static void dummy_tlb_sync(void *cookie)
  623. {
  624. WARN_ON(cookie != cfg_cookie);
  625. }
  626. static struct iommu_gather_ops dummy_tlb_ops = {
  627. .tlb_flush_all = dummy_tlb_flush_all,
  628. .tlb_add_flush = dummy_tlb_add_flush,
  629. .tlb_sync = dummy_tlb_sync,
  630. };
  631. #define __FAIL(ops) ({ \
  632. WARN(1, "selftest: test failed\n"); \
  633. selftest_running = false; \
  634. -EFAULT; \
  635. })
  636. static int __init arm_v7s_do_selftests(void)
  637. {
  638. struct io_pgtable_ops *ops;
  639. struct io_pgtable_cfg cfg = {
  640. .tlb = &dummy_tlb_ops,
  641. .oas = 32,
  642. .ias = 32,
  643. .quirks = IO_PGTABLE_QUIRK_ARM_NS,
  644. .pgsize_bitmap = SZ_4K | SZ_64K | SZ_1M | SZ_16M,
  645. };
  646. unsigned int iova, size, iova_start;
  647. unsigned int i, loopnr = 0;
  648. selftest_running = true;
  649. cfg_cookie = &cfg;
  650. ops = alloc_io_pgtable_ops(ARM_V7S, &cfg, &cfg);
  651. if (!ops) {
  652. pr_err("selftest: failed to allocate io pgtable ops\n");
  653. return -EINVAL;
  654. }
  655. /*
  656. * Initial sanity checks.
  657. * Empty page tables shouldn't provide any translations.
  658. */
  659. if (ops->iova_to_phys(ops, 42))
  660. return __FAIL(ops);
  661. if (ops->iova_to_phys(ops, SZ_1G + 42))
  662. return __FAIL(ops);
  663. if (ops->iova_to_phys(ops, SZ_2G + 42))
  664. return __FAIL(ops);
  665. /*
  666. * Distinct mappings of different granule sizes.
  667. */
  668. iova = 0;
  669. i = find_first_bit(&cfg.pgsize_bitmap, BITS_PER_LONG);
  670. while (i != BITS_PER_LONG) {
  671. size = 1UL << i;
  672. if (ops->map(ops, iova, iova, size, IOMMU_READ |
  673. IOMMU_WRITE |
  674. IOMMU_NOEXEC |
  675. IOMMU_CACHE))
  676. return __FAIL(ops);
  677. /* Overlapping mappings */
  678. if (!ops->map(ops, iova, iova + size, size,
  679. IOMMU_READ | IOMMU_NOEXEC))
  680. return __FAIL(ops);
  681. if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
  682. return __FAIL(ops);
  683. iova += SZ_16M;
  684. i++;
  685. i = find_next_bit(&cfg.pgsize_bitmap, BITS_PER_LONG, i);
  686. loopnr++;
  687. }
  688. /* Partial unmap */
  689. i = 1;
  690. size = 1UL << __ffs(cfg.pgsize_bitmap);
  691. while (i < loopnr) {
  692. iova_start = i * SZ_16M;
  693. if (ops->unmap(ops, iova_start + size, size) != size)
  694. return __FAIL(ops);
  695. /* Remap of partial unmap */
  696. if (ops->map(ops, iova_start + size, size, size, IOMMU_READ))
  697. return __FAIL(ops);
  698. if (ops->iova_to_phys(ops, iova_start + size + 42)
  699. != (size + 42))
  700. return __FAIL(ops);
  701. i++;
  702. }
  703. /* Full unmap */
  704. iova = 0;
  705. i = find_first_bit(&cfg.pgsize_bitmap, BITS_PER_LONG);
  706. while (i != BITS_PER_LONG) {
  707. size = 1UL << i;
  708. if (ops->unmap(ops, iova, size) != size)
  709. return __FAIL(ops);
  710. if (ops->iova_to_phys(ops, iova + 42))
  711. return __FAIL(ops);
  712. /* Remap full block */
  713. if (ops->map(ops, iova, iova, size, IOMMU_WRITE))
  714. return __FAIL(ops);
  715. if (ops->iova_to_phys(ops, iova + 42) != (iova + 42))
  716. return __FAIL(ops);
  717. iova += SZ_16M;
  718. i++;
  719. i = find_next_bit(&cfg.pgsize_bitmap, BITS_PER_LONG, i);
  720. }
  721. free_io_pgtable_ops(ops);
  722. selftest_running = false;
  723. pr_info("self test ok\n");
  724. return 0;
  725. }
  726. subsys_initcall(arm_v7s_do_selftests);
  727. #endif