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