io-pgtable-arm-v7s.c 23 KB

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