tegra-smmu.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * IOMMU API for SMMU in Tegra30
  3. *
  4. * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #define pr_fmt(fmt) "%s(): " fmt, __func__
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/mm.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/device.h>
  28. #include <linux/sched.h>
  29. #include <linux/iommu.h>
  30. #include <linux/io.h>
  31. #include <linux/of.h>
  32. #include <linux/of_iommu.h>
  33. #include <asm/page.h>
  34. #include <asm/cacheflush.h>
  35. #include <mach/iomap.h>
  36. #include <mach/smmu.h>
  37. #include <mach/tegra-ahb.h>
  38. /* bitmap of the page sizes currently supported */
  39. #define SMMU_IOMMU_PGSIZES (SZ_4K)
  40. #define SMMU_CONFIG 0x10
  41. #define SMMU_CONFIG_DISABLE 0
  42. #define SMMU_CONFIG_ENABLE 1
  43. #define SMMU_TLB_CONFIG 0x14
  44. #define SMMU_TLB_CONFIG_STATS__MASK (1 << 31)
  45. #define SMMU_TLB_CONFIG_STATS__ENABLE (1 << 31)
  46. #define SMMU_TLB_CONFIG_HIT_UNDER_MISS__ENABLE (1 << 29)
  47. #define SMMU_TLB_CONFIG_ACTIVE_LINES__VALUE 0x10
  48. #define SMMU_TLB_CONFIG_RESET_VAL 0x20000010
  49. #define SMMU_PTC_CONFIG 0x18
  50. #define SMMU_PTC_CONFIG_STATS__MASK (1 << 31)
  51. #define SMMU_PTC_CONFIG_STATS__ENABLE (1 << 31)
  52. #define SMMU_PTC_CONFIG_CACHE__ENABLE (1 << 29)
  53. #define SMMU_PTC_CONFIG_INDEX_MAP__PATTERN 0x3f
  54. #define SMMU_PTC_CONFIG_RESET_VAL 0x2000003f
  55. #define SMMU_PTB_ASID 0x1c
  56. #define SMMU_PTB_ASID_CURRENT_SHIFT 0
  57. #define SMMU_PTB_DATA 0x20
  58. #define SMMU_PTB_DATA_RESET_VAL 0
  59. #define SMMU_PTB_DATA_ASID_NONSECURE_SHIFT 29
  60. #define SMMU_PTB_DATA_ASID_WRITABLE_SHIFT 30
  61. #define SMMU_PTB_DATA_ASID_READABLE_SHIFT 31
  62. #define SMMU_TLB_FLUSH 0x30
  63. #define SMMU_TLB_FLUSH_VA_MATCH_ALL 0
  64. #define SMMU_TLB_FLUSH_VA_MATCH_SECTION 2
  65. #define SMMU_TLB_FLUSH_VA_MATCH_GROUP 3
  66. #define SMMU_TLB_FLUSH_ASID_SHIFT 29
  67. #define SMMU_TLB_FLUSH_ASID_MATCH_DISABLE 0
  68. #define SMMU_TLB_FLUSH_ASID_MATCH_ENABLE 1
  69. #define SMMU_TLB_FLUSH_ASID_MATCH_SHIFT 31
  70. #define SMMU_PTC_FLUSH 0x34
  71. #define SMMU_PTC_FLUSH_TYPE_ALL 0
  72. #define SMMU_PTC_FLUSH_TYPE_ADR 1
  73. #define SMMU_PTC_FLUSH_ADR_SHIFT 4
  74. #define SMMU_ASID_SECURITY 0x38
  75. #define SMMU_STATS_TLB_HIT_COUNT 0x1f0
  76. #define SMMU_STATS_TLB_MISS_COUNT 0x1f4
  77. #define SMMU_STATS_PTC_HIT_COUNT 0x1f8
  78. #define SMMU_STATS_PTC_MISS_COUNT 0x1fc
  79. #define SMMU_TRANSLATION_ENABLE_0 0x228
  80. #define SMMU_TRANSLATION_ENABLE_1 0x22c
  81. #define SMMU_TRANSLATION_ENABLE_2 0x230
  82. #define SMMU_AFI_ASID 0x238 /* PCIE */
  83. #define SMMU_AVPC_ASID 0x23c /* AVP */
  84. #define SMMU_DC_ASID 0x240 /* Display controller */
  85. #define SMMU_DCB_ASID 0x244 /* Display controller B */
  86. #define SMMU_EPP_ASID 0x248 /* Encoder pre-processor */
  87. #define SMMU_G2_ASID 0x24c /* 2D engine */
  88. #define SMMU_HC_ASID 0x250 /* Host1x */
  89. #define SMMU_HDA_ASID 0x254 /* High-def audio */
  90. #define SMMU_ISP_ASID 0x258 /* Image signal processor */
  91. #define SMMU_MPE_ASID 0x264 /* MPEG encoder */
  92. #define SMMU_NV_ASID 0x268 /* (3D) */
  93. #define SMMU_NV2_ASID 0x26c /* (3D) */
  94. #define SMMU_PPCS_ASID 0x270 /* AHB */
  95. #define SMMU_SATA_ASID 0x278 /* SATA */
  96. #define SMMU_VDE_ASID 0x27c /* Video decoder */
  97. #define SMMU_VI_ASID 0x280 /* Video input */
  98. #define SMMU_PDE_NEXT_SHIFT 28
  99. #define SMMU_TLB_FLUSH_VA_SECTION__MASK 0xffc00000
  100. #define SMMU_TLB_FLUSH_VA_SECTION__SHIFT 12 /* right shift */
  101. #define SMMU_TLB_FLUSH_VA_GROUP__MASK 0xffffc000
  102. #define SMMU_TLB_FLUSH_VA_GROUP__SHIFT 12 /* right shift */
  103. #define SMMU_TLB_FLUSH_VA(iova, which) \
  104. ((((iova) & SMMU_TLB_FLUSH_VA_##which##__MASK) >> \
  105. SMMU_TLB_FLUSH_VA_##which##__SHIFT) | \
  106. SMMU_TLB_FLUSH_VA_MATCH_##which)
  107. #define SMMU_PTB_ASID_CUR(n) \
  108. ((n) << SMMU_PTB_ASID_CURRENT_SHIFT)
  109. #define SMMU_TLB_FLUSH_ASID_MATCH_disable \
  110. (SMMU_TLB_FLUSH_ASID_MATCH_DISABLE << \
  111. SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
  112. #define SMMU_TLB_FLUSH_ASID_MATCH__ENABLE \
  113. (SMMU_TLB_FLUSH_ASID_MATCH_ENABLE << \
  114. SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
  115. #define SMMU_PAGE_SHIFT 12
  116. #define SMMU_PAGE_SIZE (1 << SMMU_PAGE_SHIFT)
  117. #define SMMU_PAGE_MASK ((1 << SMMU_PAGE_SHIFT) - 1)
  118. #define SMMU_PDIR_COUNT 1024
  119. #define SMMU_PDIR_SIZE (sizeof(unsigned long) * SMMU_PDIR_COUNT)
  120. #define SMMU_PTBL_COUNT 1024
  121. #define SMMU_PTBL_SIZE (sizeof(unsigned long) * SMMU_PTBL_COUNT)
  122. #define SMMU_PDIR_SHIFT 12
  123. #define SMMU_PDE_SHIFT 12
  124. #define SMMU_PTE_SHIFT 12
  125. #define SMMU_PFN_MASK 0x000fffff
  126. #define SMMU_ADDR_TO_PFN(addr) ((addr) >> 12)
  127. #define SMMU_ADDR_TO_PDN(addr) ((addr) >> 22)
  128. #define SMMU_PDN_TO_ADDR(addr) ((pdn) << 22)
  129. #define _READABLE (1 << SMMU_PTB_DATA_ASID_READABLE_SHIFT)
  130. #define _WRITABLE (1 << SMMU_PTB_DATA_ASID_WRITABLE_SHIFT)
  131. #define _NONSECURE (1 << SMMU_PTB_DATA_ASID_NONSECURE_SHIFT)
  132. #define _PDE_NEXT (1 << SMMU_PDE_NEXT_SHIFT)
  133. #define _MASK_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  134. #define _PDIR_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  135. #define _PDE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  136. #define _PDE_ATTR_N (_PDE_ATTR | _PDE_NEXT)
  137. #define _PDE_VACANT(pdn) (((pdn) << 10) | _PDE_ATTR)
  138. #define _PTE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  139. #define _PTE_VACANT(addr) (((addr) >> SMMU_PAGE_SHIFT) | _PTE_ATTR)
  140. #define SMMU_MK_PDIR(page, attr) \
  141. ((page_to_phys(page) >> SMMU_PDIR_SHIFT) | (attr))
  142. #define SMMU_MK_PDE(page, attr) \
  143. (unsigned long)((page_to_phys(page) >> SMMU_PDE_SHIFT) | (attr))
  144. #define SMMU_EX_PTBL_PAGE(pde) \
  145. pfn_to_page((unsigned long)(pde) & SMMU_PFN_MASK)
  146. #define SMMU_PFN_TO_PTE(pfn, attr) (unsigned long)((pfn) | (attr))
  147. #define SMMU_ASID_ENABLE(asid) ((asid) | (1 << 31))
  148. #define SMMU_ASID_DISABLE 0
  149. #define SMMU_ASID_ASID(n) ((n) & ~SMMU_ASID_ENABLE(0))
  150. #define NUM_SMMU_REG_BANKS 3
  151. #define smmu_client_enable_hwgrp(c, m) smmu_client_set_hwgrp(c, m, 1)
  152. #define smmu_client_disable_hwgrp(c) smmu_client_set_hwgrp(c, 0, 0)
  153. #define __smmu_client_enable_hwgrp(c, m) __smmu_client_set_hwgrp(c, m, 1)
  154. #define __smmu_client_disable_hwgrp(c) __smmu_client_set_hwgrp(c, 0, 0)
  155. #define HWGRP_INIT(client) [HWGRP_##client] = SMMU_##client##_ASID
  156. static const u32 smmu_hwgrp_asid_reg[] = {
  157. HWGRP_INIT(AFI),
  158. HWGRP_INIT(AVPC),
  159. HWGRP_INIT(DC),
  160. HWGRP_INIT(DCB),
  161. HWGRP_INIT(EPP),
  162. HWGRP_INIT(G2),
  163. HWGRP_INIT(HC),
  164. HWGRP_INIT(HDA),
  165. HWGRP_INIT(ISP),
  166. HWGRP_INIT(MPE),
  167. HWGRP_INIT(NV),
  168. HWGRP_INIT(NV2),
  169. HWGRP_INIT(PPCS),
  170. HWGRP_INIT(SATA),
  171. HWGRP_INIT(VDE),
  172. HWGRP_INIT(VI),
  173. };
  174. #define HWGRP_ASID_REG(x) (smmu_hwgrp_asid_reg[x])
  175. /*
  176. * Per client for address space
  177. */
  178. struct smmu_client {
  179. struct device *dev;
  180. struct list_head list;
  181. struct smmu_as *as;
  182. u32 hwgrp;
  183. };
  184. /*
  185. * Per address space
  186. */
  187. struct smmu_as {
  188. struct smmu_device *smmu; /* back pointer to container */
  189. unsigned int asid;
  190. spinlock_t lock; /* for pagetable */
  191. struct page *pdir_page;
  192. unsigned long pdir_attr;
  193. unsigned long pde_attr;
  194. unsigned long pte_attr;
  195. unsigned int *pte_count;
  196. struct list_head client;
  197. spinlock_t client_lock; /* for client list */
  198. };
  199. /*
  200. * Per SMMU device - IOMMU device
  201. */
  202. struct smmu_device {
  203. void __iomem *regs[NUM_SMMU_REG_BANKS];
  204. unsigned long iovmm_base; /* remappable base address */
  205. unsigned long page_count; /* total remappable size */
  206. spinlock_t lock;
  207. char *name;
  208. struct device *dev;
  209. struct page *avp_vector_page; /* dummy page shared by all AS's */
  210. /*
  211. * Register image savers for suspend/resume
  212. */
  213. unsigned long translation_enable_0;
  214. unsigned long translation_enable_1;
  215. unsigned long translation_enable_2;
  216. unsigned long asid_security;
  217. struct device_node *ahb;
  218. int num_as;
  219. struct smmu_as as[0]; /* Run-time allocated array */
  220. };
  221. static struct smmu_device *smmu_handle; /* unique for a system */
  222. /*
  223. * SMMU register accessors
  224. */
  225. static inline u32 smmu_read(struct smmu_device *smmu, size_t offs)
  226. {
  227. BUG_ON(offs < 0x10);
  228. if (offs < 0x3c)
  229. return readl(smmu->regs[0] + offs - 0x10);
  230. BUG_ON(offs < 0x1f0);
  231. if (offs < 0x200)
  232. return readl(smmu->regs[1] + offs - 0x1f0);
  233. BUG_ON(offs < 0x228);
  234. if (offs < 0x284)
  235. return readl(smmu->regs[2] + offs - 0x228);
  236. BUG();
  237. }
  238. static inline void smmu_write(struct smmu_device *smmu, u32 val, size_t offs)
  239. {
  240. BUG_ON(offs < 0x10);
  241. if (offs < 0x3c) {
  242. writel(val, smmu->regs[0] + offs - 0x10);
  243. return;
  244. }
  245. BUG_ON(offs < 0x1f0);
  246. if (offs < 0x200) {
  247. writel(val, smmu->regs[1] + offs - 0x1f0);
  248. return;
  249. }
  250. BUG_ON(offs < 0x228);
  251. if (offs < 0x284) {
  252. writel(val, smmu->regs[2] + offs - 0x228);
  253. return;
  254. }
  255. BUG();
  256. }
  257. #define VA_PAGE_TO_PA(va, page) \
  258. (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
  259. #define FLUSH_CPU_DCACHE(va, page, size) \
  260. do { \
  261. unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
  262. __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
  263. outer_flush_range(_pa_, _pa_+(size_t)(size)); \
  264. } while (0)
  265. /*
  266. * Any interaction between any block on PPSB and a block on APB or AHB
  267. * must have these read-back barriers to ensure the APB/AHB bus
  268. * transaction is complete before initiating activity on the PPSB
  269. * block.
  270. */
  271. #define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
  272. #define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
  273. static int __smmu_client_set_hwgrp(struct smmu_client *c,
  274. unsigned long map, int on)
  275. {
  276. int i;
  277. struct smmu_as *as = c->as;
  278. u32 val, offs, mask = SMMU_ASID_ENABLE(as->asid);
  279. struct smmu_device *smmu = as->smmu;
  280. WARN_ON(!on && map);
  281. if (on && !map)
  282. return -EINVAL;
  283. if (!on)
  284. map = smmu_client_hwgrp(c);
  285. for_each_set_bit(i, &map, HWGRP_COUNT) {
  286. offs = HWGRP_ASID_REG(i);
  287. val = smmu_read(smmu, offs);
  288. if (on) {
  289. if (WARN_ON(val & mask))
  290. goto err_hw_busy;
  291. val |= mask;
  292. } else {
  293. WARN_ON((val & mask) == mask);
  294. val &= ~mask;
  295. }
  296. smmu_write(smmu, val, offs);
  297. }
  298. FLUSH_SMMU_REGS(smmu);
  299. c->hwgrp = map;
  300. return 0;
  301. err_hw_busy:
  302. for_each_set_bit(i, &map, HWGRP_COUNT) {
  303. offs = HWGRP_ASID_REG(i);
  304. val = smmu_read(smmu, offs);
  305. val &= ~mask;
  306. smmu_write(smmu, val, offs);
  307. }
  308. return -EBUSY;
  309. }
  310. static int smmu_client_set_hwgrp(struct smmu_client *c, u32 map, int on)
  311. {
  312. u32 val;
  313. unsigned long flags;
  314. struct smmu_as *as = c->as;
  315. struct smmu_device *smmu = as->smmu;
  316. spin_lock_irqsave(&smmu->lock, flags);
  317. val = __smmu_client_set_hwgrp(c, map, on);
  318. spin_unlock_irqrestore(&smmu->lock, flags);
  319. return val;
  320. }
  321. /*
  322. * Flush all TLB entries and all PTC entries
  323. * Caller must lock smmu
  324. */
  325. static void smmu_flush_regs(struct smmu_device *smmu, int enable)
  326. {
  327. u32 val;
  328. smmu_write(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
  329. FLUSH_SMMU_REGS(smmu);
  330. val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
  331. SMMU_TLB_FLUSH_ASID_MATCH_disable;
  332. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  333. if (enable)
  334. smmu_write(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
  335. FLUSH_SMMU_REGS(smmu);
  336. }
  337. static int smmu_setup_regs(struct smmu_device *smmu)
  338. {
  339. int i;
  340. u32 val;
  341. for (i = 0; i < smmu->num_as; i++) {
  342. struct smmu_as *as = &smmu->as[i];
  343. struct smmu_client *c;
  344. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  345. val = as->pdir_page ?
  346. SMMU_MK_PDIR(as->pdir_page, as->pdir_attr) :
  347. SMMU_PTB_DATA_RESET_VAL;
  348. smmu_write(smmu, val, SMMU_PTB_DATA);
  349. list_for_each_entry(c, &as->client, list)
  350. __smmu_client_set_hwgrp(c, c->hwgrp, 1);
  351. }
  352. smmu_write(smmu, smmu->translation_enable_0, SMMU_TRANSLATION_ENABLE_0);
  353. smmu_write(smmu, smmu->translation_enable_1, SMMU_TRANSLATION_ENABLE_1);
  354. smmu_write(smmu, smmu->translation_enable_2, SMMU_TRANSLATION_ENABLE_2);
  355. smmu_write(smmu, smmu->asid_security, SMMU_ASID_SECURITY);
  356. smmu_write(smmu, SMMU_TLB_CONFIG_RESET_VAL, SMMU_TLB_CONFIG);
  357. smmu_write(smmu, SMMU_PTC_CONFIG_RESET_VAL, SMMU_PTC_CONFIG);
  358. smmu_flush_regs(smmu, 1);
  359. return tegra_ahb_enable_smmu(smmu->ahb);
  360. }
  361. static void flush_ptc_and_tlb(struct smmu_device *smmu,
  362. struct smmu_as *as, dma_addr_t iova,
  363. unsigned long *pte, struct page *page, int is_pde)
  364. {
  365. u32 val;
  366. unsigned long tlb_flush_va = is_pde
  367. ? SMMU_TLB_FLUSH_VA(iova, SECTION)
  368. : SMMU_TLB_FLUSH_VA(iova, GROUP);
  369. val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pte, page);
  370. smmu_write(smmu, val, SMMU_PTC_FLUSH);
  371. FLUSH_SMMU_REGS(smmu);
  372. val = tlb_flush_va |
  373. SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
  374. (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
  375. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  376. FLUSH_SMMU_REGS(smmu);
  377. }
  378. static void free_ptbl(struct smmu_as *as, dma_addr_t iova)
  379. {
  380. unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
  381. unsigned long *pdir = (unsigned long *)page_address(as->pdir_page);
  382. if (pdir[pdn] != _PDE_VACANT(pdn)) {
  383. dev_dbg(as->smmu->dev, "pdn: %lx\n", pdn);
  384. ClearPageReserved(SMMU_EX_PTBL_PAGE(pdir[pdn]));
  385. __free_page(SMMU_EX_PTBL_PAGE(pdir[pdn]));
  386. pdir[pdn] = _PDE_VACANT(pdn);
  387. FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
  388. flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
  389. as->pdir_page, 1);
  390. }
  391. }
  392. static void free_pdir(struct smmu_as *as)
  393. {
  394. unsigned addr;
  395. int count;
  396. struct device *dev = as->smmu->dev;
  397. if (!as->pdir_page)
  398. return;
  399. addr = as->smmu->iovmm_base;
  400. count = as->smmu->page_count;
  401. while (count-- > 0) {
  402. free_ptbl(as, addr);
  403. addr += SMMU_PAGE_SIZE * SMMU_PTBL_COUNT;
  404. }
  405. ClearPageReserved(as->pdir_page);
  406. __free_page(as->pdir_page);
  407. as->pdir_page = NULL;
  408. devm_kfree(dev, as->pte_count);
  409. as->pte_count = NULL;
  410. }
  411. /*
  412. * Maps PTBL for given iova and returns the PTE address
  413. * Caller must unmap the mapped PTBL returned in *ptbl_page_p
  414. */
  415. static unsigned long *locate_pte(struct smmu_as *as,
  416. dma_addr_t iova, bool allocate,
  417. struct page **ptbl_page_p,
  418. unsigned int **count)
  419. {
  420. unsigned long ptn = SMMU_ADDR_TO_PFN(iova);
  421. unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
  422. unsigned long *pdir = page_address(as->pdir_page);
  423. unsigned long *ptbl;
  424. if (pdir[pdn] != _PDE_VACANT(pdn)) {
  425. /* Mapped entry table already exists */
  426. *ptbl_page_p = SMMU_EX_PTBL_PAGE(pdir[pdn]);
  427. ptbl = page_address(*ptbl_page_p);
  428. } else if (!allocate) {
  429. return NULL;
  430. } else {
  431. int pn;
  432. unsigned long addr = SMMU_PDN_TO_ADDR(pdn);
  433. /* Vacant - allocate a new page table */
  434. dev_dbg(as->smmu->dev, "New PTBL pdn: %lx\n", pdn);
  435. *ptbl_page_p = alloc_page(GFP_ATOMIC);
  436. if (!*ptbl_page_p) {
  437. dev_err(as->smmu->dev,
  438. "failed to allocate smmu_device page table\n");
  439. return NULL;
  440. }
  441. SetPageReserved(*ptbl_page_p);
  442. ptbl = (unsigned long *)page_address(*ptbl_page_p);
  443. for (pn = 0; pn < SMMU_PTBL_COUNT;
  444. pn++, addr += SMMU_PAGE_SIZE) {
  445. ptbl[pn] = _PTE_VACANT(addr);
  446. }
  447. FLUSH_CPU_DCACHE(ptbl, *ptbl_page_p, SMMU_PTBL_SIZE);
  448. pdir[pdn] = SMMU_MK_PDE(*ptbl_page_p,
  449. as->pde_attr | _PDE_NEXT);
  450. FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
  451. flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
  452. as->pdir_page, 1);
  453. }
  454. *count = &as->pte_count[pdn];
  455. return &ptbl[ptn % SMMU_PTBL_COUNT];
  456. }
  457. #ifdef CONFIG_SMMU_SIG_DEBUG
  458. static void put_signature(struct smmu_as *as,
  459. dma_addr_t iova, unsigned long pfn)
  460. {
  461. struct page *page;
  462. unsigned long *vaddr;
  463. page = pfn_to_page(pfn);
  464. vaddr = page_address(page);
  465. if (!vaddr)
  466. return;
  467. vaddr[0] = iova;
  468. vaddr[1] = pfn << PAGE_SHIFT;
  469. FLUSH_CPU_DCACHE(vaddr, page, sizeof(vaddr[0]) * 2);
  470. }
  471. #else
  472. static inline void put_signature(struct smmu_as *as,
  473. unsigned long addr, unsigned long pfn)
  474. {
  475. }
  476. #endif
  477. /*
  478. * Caller must lock/unlock as
  479. */
  480. static int alloc_pdir(struct smmu_as *as)
  481. {
  482. unsigned long *pdir;
  483. int pdn;
  484. u32 val;
  485. struct smmu_device *smmu = as->smmu;
  486. as->pte_count = devm_kzalloc(smmu->dev,
  487. sizeof(as->pte_count[0]) * SMMU_PDIR_COUNT, GFP_KERNEL);
  488. if (!as->pte_count) {
  489. dev_err(smmu->dev,
  490. "failed to allocate smmu_device PTE cunters\n");
  491. return -ENOMEM;
  492. }
  493. as->pdir_page = alloc_page(GFP_KERNEL | __GFP_DMA);
  494. if (!as->pdir_page) {
  495. dev_err(smmu->dev,
  496. "failed to allocate smmu_device page directory\n");
  497. devm_kfree(smmu->dev, as->pte_count);
  498. as->pte_count = NULL;
  499. return -ENOMEM;
  500. }
  501. SetPageReserved(as->pdir_page);
  502. pdir = page_address(as->pdir_page);
  503. for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
  504. pdir[pdn] = _PDE_VACANT(pdn);
  505. FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
  506. val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
  507. smmu_write(smmu, val, SMMU_PTC_FLUSH);
  508. FLUSH_SMMU_REGS(as->smmu);
  509. val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
  510. SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
  511. (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
  512. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  513. FLUSH_SMMU_REGS(as->smmu);
  514. return 0;
  515. }
  516. static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
  517. {
  518. unsigned long *pte;
  519. struct page *page;
  520. unsigned int *count;
  521. pte = locate_pte(as, iova, false, &page, &count);
  522. if (WARN_ON(!pte))
  523. return;
  524. if (WARN_ON(*pte == _PTE_VACANT(iova)))
  525. return;
  526. *pte = _PTE_VACANT(iova);
  527. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  528. flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
  529. if (!--(*count)) {
  530. free_ptbl(as, iova);
  531. smmu_flush_regs(as->smmu, 0);
  532. }
  533. }
  534. static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
  535. unsigned long pfn)
  536. {
  537. struct smmu_device *smmu = as->smmu;
  538. unsigned long *pte;
  539. unsigned int *count;
  540. struct page *page;
  541. pte = locate_pte(as, iova, true, &page, &count);
  542. if (WARN_ON(!pte))
  543. return;
  544. if (*pte == _PTE_VACANT(iova))
  545. (*count)++;
  546. *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
  547. if (unlikely((*pte == _PTE_VACANT(iova))))
  548. (*count)--;
  549. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  550. flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
  551. put_signature(as, iova, pfn);
  552. }
  553. static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
  554. phys_addr_t pa, size_t bytes, int prot)
  555. {
  556. struct smmu_as *as = domain->priv;
  557. unsigned long pfn = __phys_to_pfn(pa);
  558. unsigned long flags;
  559. dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
  560. if (!pfn_valid(pfn))
  561. return -ENOMEM;
  562. spin_lock_irqsave(&as->lock, flags);
  563. __smmu_iommu_map_pfn(as, iova, pfn);
  564. spin_unlock_irqrestore(&as->lock, flags);
  565. return 0;
  566. }
  567. static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  568. size_t bytes)
  569. {
  570. struct smmu_as *as = domain->priv;
  571. unsigned long flags;
  572. dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
  573. spin_lock_irqsave(&as->lock, flags);
  574. __smmu_iommu_unmap(as, iova);
  575. spin_unlock_irqrestore(&as->lock, flags);
  576. return SMMU_PAGE_SIZE;
  577. }
  578. static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
  579. unsigned long iova)
  580. {
  581. struct smmu_as *as = domain->priv;
  582. unsigned long *pte;
  583. unsigned int *count;
  584. struct page *page;
  585. unsigned long pfn;
  586. unsigned long flags;
  587. spin_lock_irqsave(&as->lock, flags);
  588. pte = locate_pte(as, iova, true, &page, &count);
  589. pfn = *pte & SMMU_PFN_MASK;
  590. WARN_ON(!pfn_valid(pfn));
  591. dev_dbg(as->smmu->dev,
  592. "iova:%08lx pfn:%08lx asid:%d\n", iova, pfn, as->asid);
  593. spin_unlock_irqrestore(&as->lock, flags);
  594. return PFN_PHYS(pfn);
  595. }
  596. static int smmu_iommu_domain_has_cap(struct iommu_domain *domain,
  597. unsigned long cap)
  598. {
  599. return 0;
  600. }
  601. static int smmu_iommu_attach_dev(struct iommu_domain *domain,
  602. struct device *dev)
  603. {
  604. struct smmu_as *as = domain->priv;
  605. struct smmu_device *smmu = as->smmu;
  606. struct smmu_client *client, *c;
  607. u32 map;
  608. int err;
  609. client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
  610. if (!client)
  611. return -ENOMEM;
  612. client->dev = dev;
  613. client->as = as;
  614. map = (unsigned long)dev->platform_data;
  615. if (!map)
  616. return -EINVAL;
  617. err = smmu_client_enable_hwgrp(client, map);
  618. if (err)
  619. goto err_hwgrp;
  620. spin_lock(&as->client_lock);
  621. list_for_each_entry(c, &as->client, list) {
  622. if (c->dev == dev) {
  623. dev_err(smmu->dev,
  624. "%s is already attached\n", dev_name(c->dev));
  625. err = -EINVAL;
  626. goto err_client;
  627. }
  628. }
  629. list_add(&client->list, &as->client);
  630. spin_unlock(&as->client_lock);
  631. /*
  632. * Reserve "page zero" for AVP vectors using a common dummy
  633. * page.
  634. */
  635. if (map & HWG_AVPC) {
  636. struct page *page;
  637. page = as->smmu->avp_vector_page;
  638. __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
  639. pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
  640. }
  641. dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
  642. return 0;
  643. err_client:
  644. smmu_client_disable_hwgrp(client);
  645. spin_unlock(&as->client_lock);
  646. err_hwgrp:
  647. devm_kfree(smmu->dev, client);
  648. return err;
  649. }
  650. static void smmu_iommu_detach_dev(struct iommu_domain *domain,
  651. struct device *dev)
  652. {
  653. struct smmu_as *as = domain->priv;
  654. struct smmu_device *smmu = as->smmu;
  655. struct smmu_client *c;
  656. spin_lock(&as->client_lock);
  657. list_for_each_entry(c, &as->client, list) {
  658. if (c->dev == dev) {
  659. smmu_client_disable_hwgrp(c);
  660. list_del(&c->list);
  661. devm_kfree(smmu->dev, c);
  662. c->as = NULL;
  663. dev_dbg(smmu->dev,
  664. "%s is detached\n", dev_name(c->dev));
  665. goto out;
  666. }
  667. }
  668. dev_err(smmu->dev, "Couldn't find %s\n", dev_name(c->dev));
  669. out:
  670. spin_unlock(&as->client_lock);
  671. }
  672. static int smmu_iommu_domain_init(struct iommu_domain *domain)
  673. {
  674. int i;
  675. unsigned long flags;
  676. struct smmu_as *as;
  677. struct smmu_device *smmu = smmu_handle;
  678. /* Look for a free AS with lock held */
  679. for (i = 0; i < smmu->num_as; i++) {
  680. struct smmu_as *tmp = &smmu->as[i];
  681. spin_lock_irqsave(&tmp->lock, flags);
  682. if (!tmp->pdir_page) {
  683. as = tmp;
  684. goto found;
  685. }
  686. spin_unlock_irqrestore(&tmp->lock, flags);
  687. }
  688. dev_err(smmu->dev, "no free AS\n");
  689. return -ENODEV;
  690. found:
  691. if (alloc_pdir(as) < 0)
  692. goto err_alloc_pdir;
  693. spin_lock(&smmu->lock);
  694. /* Update PDIR register */
  695. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  696. smmu_write(smmu,
  697. SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
  698. FLUSH_SMMU_REGS(smmu);
  699. spin_unlock(&smmu->lock);
  700. spin_unlock_irqrestore(&as->lock, flags);
  701. domain->priv = as;
  702. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  703. return 0;
  704. err_alloc_pdir:
  705. spin_unlock_irqrestore(&as->lock, flags);
  706. return -ENODEV;
  707. }
  708. static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
  709. {
  710. struct smmu_as *as = domain->priv;
  711. struct smmu_device *smmu = as->smmu;
  712. unsigned long flags;
  713. spin_lock_irqsave(&as->lock, flags);
  714. if (as->pdir_page) {
  715. spin_lock(&smmu->lock);
  716. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  717. smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
  718. FLUSH_SMMU_REGS(smmu);
  719. spin_unlock(&smmu->lock);
  720. free_pdir(as);
  721. }
  722. if (!list_empty(&as->client)) {
  723. struct smmu_client *c;
  724. list_for_each_entry(c, &as->client, list)
  725. smmu_iommu_detach_dev(domain, c->dev);
  726. }
  727. spin_unlock_irqrestore(&as->lock, flags);
  728. domain->priv = NULL;
  729. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  730. }
  731. static struct iommu_ops smmu_iommu_ops = {
  732. .domain_init = smmu_iommu_domain_init,
  733. .domain_destroy = smmu_iommu_domain_destroy,
  734. .attach_dev = smmu_iommu_attach_dev,
  735. .detach_dev = smmu_iommu_detach_dev,
  736. .map = smmu_iommu_map,
  737. .unmap = smmu_iommu_unmap,
  738. .iova_to_phys = smmu_iommu_iova_to_phys,
  739. .domain_has_cap = smmu_iommu_domain_has_cap,
  740. .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
  741. };
  742. static int tegra_smmu_suspend(struct device *dev)
  743. {
  744. struct smmu_device *smmu = dev_get_drvdata(dev);
  745. smmu->translation_enable_0 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_0);
  746. smmu->translation_enable_1 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_1);
  747. smmu->translation_enable_2 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_2);
  748. smmu->asid_security = smmu_read(smmu, SMMU_ASID_SECURITY);
  749. return 0;
  750. }
  751. static int tegra_smmu_resume(struct device *dev)
  752. {
  753. struct smmu_device *smmu = dev_get_drvdata(dev);
  754. unsigned long flags;
  755. int err;
  756. spin_lock_irqsave(&smmu->lock, flags);
  757. err = smmu_setup_regs(smmu);
  758. spin_unlock_irqrestore(&smmu->lock, flags);
  759. return err;
  760. }
  761. static int tegra_smmu_probe(struct platform_device *pdev)
  762. {
  763. struct smmu_device *smmu;
  764. struct device *dev = &pdev->dev;
  765. int i, asids, err = 0;
  766. dma_addr_t uninitialized_var(base);
  767. size_t bytes, uninitialized_var(size);
  768. if (smmu_handle)
  769. return -EIO;
  770. BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
  771. if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
  772. return -ENODEV;
  773. bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
  774. smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
  775. if (!smmu) {
  776. dev_err(dev, "failed to allocate smmu_device\n");
  777. return -ENOMEM;
  778. }
  779. for (i = 0; i < ARRAY_SIZE(smmu->regs); i++) {
  780. struct resource *res;
  781. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  782. if (!res)
  783. return -ENODEV;
  784. smmu->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
  785. if (!smmu->regs[i])
  786. return -EBUSY;
  787. }
  788. err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
  789. if (err)
  790. return -ENODEV;
  791. if (size & SMMU_PAGE_MASK)
  792. return -EINVAL;
  793. size >>= SMMU_PAGE_SHIFT;
  794. if (!size)
  795. return -EINVAL;
  796. smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
  797. if (!smmu->ahb)
  798. return -ENODEV;
  799. smmu->dev = dev;
  800. smmu->num_as = asids;
  801. smmu->iovmm_base = base;
  802. smmu->page_count = size;
  803. smmu->translation_enable_0 = ~0;
  804. smmu->translation_enable_1 = ~0;
  805. smmu->translation_enable_2 = ~0;
  806. smmu->asid_security = 0;
  807. for (i = 0; i < smmu->num_as; i++) {
  808. struct smmu_as *as = &smmu->as[i];
  809. as->smmu = smmu;
  810. as->asid = i;
  811. as->pdir_attr = _PDIR_ATTR;
  812. as->pde_attr = _PDE_ATTR;
  813. as->pte_attr = _PTE_ATTR;
  814. spin_lock_init(&as->lock);
  815. INIT_LIST_HEAD(&as->client);
  816. }
  817. spin_lock_init(&smmu->lock);
  818. err = smmu_setup_regs(smmu);
  819. if (err)
  820. return err;
  821. platform_set_drvdata(pdev, smmu);
  822. smmu->avp_vector_page = alloc_page(GFP_KERNEL);
  823. if (!smmu->avp_vector_page)
  824. return -ENOMEM;
  825. smmu_handle = smmu;
  826. return 0;
  827. }
  828. static int tegra_smmu_remove(struct platform_device *pdev)
  829. {
  830. struct smmu_device *smmu = platform_get_drvdata(pdev);
  831. int i;
  832. smmu_write(smmu, SMMU_CONFIG_DISABLE, SMMU_CONFIG);
  833. for (i = 0; i < smmu->num_as; i++)
  834. free_pdir(&smmu->as[i]);
  835. __free_page(smmu->avp_vector_page);
  836. smmu_handle = NULL;
  837. return 0;
  838. }
  839. const struct dev_pm_ops tegra_smmu_pm_ops = {
  840. .suspend = tegra_smmu_suspend,
  841. .resume = tegra_smmu_resume,
  842. };
  843. #ifdef CONFIG_OF
  844. static struct of_device_id tegra_smmu_of_match[] __devinitdata = {
  845. { .compatible = "nvidia,tegra30-smmu", },
  846. { },
  847. };
  848. MODULE_DEVICE_TABLE(of, tegra_smmu_of_match);
  849. #endif
  850. static struct platform_driver tegra_smmu_driver = {
  851. .probe = tegra_smmu_probe,
  852. .remove = tegra_smmu_remove,
  853. .driver = {
  854. .owner = THIS_MODULE,
  855. .name = "tegra-smmu",
  856. .pm = &tegra_smmu_pm_ops,
  857. .of_match_table = of_match_ptr(tegra_smmu_of_match),
  858. },
  859. };
  860. static int __devinit tegra_smmu_init(void)
  861. {
  862. bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
  863. return platform_driver_register(&tegra_smmu_driver);
  864. }
  865. static void __exit tegra_smmu_exit(void)
  866. {
  867. platform_driver_unregister(&tegra_smmu_driver);
  868. }
  869. subsys_initcall(tegra_smmu_init);
  870. module_exit(tegra_smmu_exit);
  871. MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
  872. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  873. MODULE_ALIAS("platform:tegra-smmu");
  874. MODULE_LICENSE("GPL v2");