tegra-smmu.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 not hold as->lock
  479. */
  480. static int alloc_pdir(struct smmu_as *as)
  481. {
  482. unsigned long *pdir, flags;
  483. int pdn, err = 0;
  484. u32 val;
  485. struct smmu_device *smmu = as->smmu;
  486. struct page *page;
  487. unsigned int *cnt;
  488. /*
  489. * do the allocation, then grab as->lock
  490. */
  491. cnt = devm_kzalloc(smmu->dev,
  492. sizeof(cnt[0]) * SMMU_PDIR_COUNT,
  493. GFP_KERNEL);
  494. page = alloc_page(GFP_KERNEL | __GFP_DMA);
  495. spin_lock_irqsave(&as->lock, flags);
  496. if (as->pdir_page) {
  497. /* We raced, free the redundant */
  498. err = -EAGAIN;
  499. goto err_out;
  500. }
  501. if (!page || !cnt) {
  502. dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
  503. err = -ENOMEM;
  504. goto err_out;
  505. }
  506. as->pdir_page = page;
  507. as->pte_count = cnt;
  508. SetPageReserved(as->pdir_page);
  509. pdir = page_address(as->pdir_page);
  510. for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
  511. pdir[pdn] = _PDE_VACANT(pdn);
  512. FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
  513. val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
  514. smmu_write(smmu, val, SMMU_PTC_FLUSH);
  515. FLUSH_SMMU_REGS(as->smmu);
  516. val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
  517. SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
  518. (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
  519. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  520. FLUSH_SMMU_REGS(as->smmu);
  521. spin_unlock_irqrestore(&as->lock, flags);
  522. return 0;
  523. err_out:
  524. spin_unlock_irqrestore(&as->lock, flags);
  525. devm_kfree(smmu->dev, cnt);
  526. if (page)
  527. __free_page(page);
  528. return err;
  529. }
  530. static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
  531. {
  532. unsigned long *pte;
  533. struct page *page;
  534. unsigned int *count;
  535. pte = locate_pte(as, iova, false, &page, &count);
  536. if (WARN_ON(!pte))
  537. return;
  538. if (WARN_ON(*pte == _PTE_VACANT(iova)))
  539. return;
  540. *pte = _PTE_VACANT(iova);
  541. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  542. flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
  543. if (!--(*count)) {
  544. free_ptbl(as, iova);
  545. smmu_flush_regs(as->smmu, 0);
  546. }
  547. }
  548. static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
  549. unsigned long pfn)
  550. {
  551. struct smmu_device *smmu = as->smmu;
  552. unsigned long *pte;
  553. unsigned int *count;
  554. struct page *page;
  555. pte = locate_pte(as, iova, true, &page, &count);
  556. if (WARN_ON(!pte))
  557. return;
  558. if (*pte == _PTE_VACANT(iova))
  559. (*count)++;
  560. *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
  561. if (unlikely((*pte == _PTE_VACANT(iova))))
  562. (*count)--;
  563. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  564. flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
  565. put_signature(as, iova, pfn);
  566. }
  567. static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
  568. phys_addr_t pa, size_t bytes, int prot)
  569. {
  570. struct smmu_as *as = domain->priv;
  571. unsigned long pfn = __phys_to_pfn(pa);
  572. unsigned long flags;
  573. dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
  574. if (!pfn_valid(pfn))
  575. return -ENOMEM;
  576. spin_lock_irqsave(&as->lock, flags);
  577. __smmu_iommu_map_pfn(as, iova, pfn);
  578. spin_unlock_irqrestore(&as->lock, flags);
  579. return 0;
  580. }
  581. static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  582. size_t bytes)
  583. {
  584. struct smmu_as *as = domain->priv;
  585. unsigned long flags;
  586. dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
  587. spin_lock_irqsave(&as->lock, flags);
  588. __smmu_iommu_unmap(as, iova);
  589. spin_unlock_irqrestore(&as->lock, flags);
  590. return SMMU_PAGE_SIZE;
  591. }
  592. static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
  593. unsigned long iova)
  594. {
  595. struct smmu_as *as = domain->priv;
  596. unsigned long *pte;
  597. unsigned int *count;
  598. struct page *page;
  599. unsigned long pfn;
  600. unsigned long flags;
  601. spin_lock_irqsave(&as->lock, flags);
  602. pte = locate_pte(as, iova, true, &page, &count);
  603. pfn = *pte & SMMU_PFN_MASK;
  604. WARN_ON(!pfn_valid(pfn));
  605. dev_dbg(as->smmu->dev,
  606. "iova:%08lx pfn:%08lx asid:%d\n", iova, pfn, as->asid);
  607. spin_unlock_irqrestore(&as->lock, flags);
  608. return PFN_PHYS(pfn);
  609. }
  610. static int smmu_iommu_domain_has_cap(struct iommu_domain *domain,
  611. unsigned long cap)
  612. {
  613. return 0;
  614. }
  615. static int smmu_iommu_attach_dev(struct iommu_domain *domain,
  616. struct device *dev)
  617. {
  618. struct smmu_as *as = domain->priv;
  619. struct smmu_device *smmu = as->smmu;
  620. struct smmu_client *client, *c;
  621. u32 map;
  622. int err;
  623. client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
  624. if (!client)
  625. return -ENOMEM;
  626. client->dev = dev;
  627. client->as = as;
  628. map = (unsigned long)dev->platform_data;
  629. if (!map)
  630. return -EINVAL;
  631. err = smmu_client_enable_hwgrp(client, map);
  632. if (err)
  633. goto err_hwgrp;
  634. spin_lock(&as->client_lock);
  635. list_for_each_entry(c, &as->client, list) {
  636. if (c->dev == dev) {
  637. dev_err(smmu->dev,
  638. "%s is already attached\n", dev_name(c->dev));
  639. err = -EINVAL;
  640. goto err_client;
  641. }
  642. }
  643. list_add(&client->list, &as->client);
  644. spin_unlock(&as->client_lock);
  645. /*
  646. * Reserve "page zero" for AVP vectors using a common dummy
  647. * page.
  648. */
  649. if (map & HWG_AVPC) {
  650. struct page *page;
  651. page = as->smmu->avp_vector_page;
  652. __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
  653. pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
  654. }
  655. dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
  656. return 0;
  657. err_client:
  658. smmu_client_disable_hwgrp(client);
  659. spin_unlock(&as->client_lock);
  660. err_hwgrp:
  661. devm_kfree(smmu->dev, client);
  662. return err;
  663. }
  664. static void smmu_iommu_detach_dev(struct iommu_domain *domain,
  665. struct device *dev)
  666. {
  667. struct smmu_as *as = domain->priv;
  668. struct smmu_device *smmu = as->smmu;
  669. struct smmu_client *c;
  670. spin_lock(&as->client_lock);
  671. list_for_each_entry(c, &as->client, list) {
  672. if (c->dev == dev) {
  673. smmu_client_disable_hwgrp(c);
  674. list_del(&c->list);
  675. devm_kfree(smmu->dev, c);
  676. c->as = NULL;
  677. dev_dbg(smmu->dev,
  678. "%s is detached\n", dev_name(c->dev));
  679. goto out;
  680. }
  681. }
  682. dev_err(smmu->dev, "Couldn't find %s\n", dev_name(dev));
  683. out:
  684. spin_unlock(&as->client_lock);
  685. }
  686. static int smmu_iommu_domain_init(struct iommu_domain *domain)
  687. {
  688. int i, err = -EAGAIN;
  689. unsigned long flags;
  690. struct smmu_as *as;
  691. struct smmu_device *smmu = smmu_handle;
  692. /* Look for a free AS with lock held */
  693. for (i = 0; i < smmu->num_as; i++) {
  694. as = &smmu->as[i];
  695. if (as->pdir_page)
  696. continue;
  697. err = alloc_pdir(as);
  698. if (!err)
  699. goto found;
  700. if (err != -EAGAIN)
  701. break;
  702. }
  703. if (i == smmu->num_as)
  704. dev_err(smmu->dev, "no free AS\n");
  705. return err;
  706. found:
  707. spin_lock_irqsave(&smmu->lock, flags);
  708. /* Update PDIR register */
  709. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  710. smmu_write(smmu,
  711. SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
  712. FLUSH_SMMU_REGS(smmu);
  713. spin_unlock_irqrestore(&smmu->lock, flags);
  714. domain->priv = as;
  715. domain->geometry.aperture_start = smmu->iovmm_base;
  716. domain->geometry.aperture_end = smmu->iovmm_base +
  717. smmu->page_count * SMMU_PAGE_SIZE - 1;
  718. domain->geometry.force_aperture = true;
  719. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  720. return 0;
  721. }
  722. static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
  723. {
  724. struct smmu_as *as = domain->priv;
  725. struct smmu_device *smmu = as->smmu;
  726. unsigned long flags;
  727. spin_lock_irqsave(&as->lock, flags);
  728. if (as->pdir_page) {
  729. spin_lock(&smmu->lock);
  730. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  731. smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
  732. FLUSH_SMMU_REGS(smmu);
  733. spin_unlock(&smmu->lock);
  734. free_pdir(as);
  735. }
  736. if (!list_empty(&as->client)) {
  737. struct smmu_client *c;
  738. list_for_each_entry(c, &as->client, list)
  739. smmu_iommu_detach_dev(domain, c->dev);
  740. }
  741. spin_unlock_irqrestore(&as->lock, flags);
  742. domain->priv = NULL;
  743. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  744. }
  745. static struct iommu_ops smmu_iommu_ops = {
  746. .domain_init = smmu_iommu_domain_init,
  747. .domain_destroy = smmu_iommu_domain_destroy,
  748. .attach_dev = smmu_iommu_attach_dev,
  749. .detach_dev = smmu_iommu_detach_dev,
  750. .map = smmu_iommu_map,
  751. .unmap = smmu_iommu_unmap,
  752. .iova_to_phys = smmu_iommu_iova_to_phys,
  753. .domain_has_cap = smmu_iommu_domain_has_cap,
  754. .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
  755. };
  756. static int tegra_smmu_suspend(struct device *dev)
  757. {
  758. struct smmu_device *smmu = dev_get_drvdata(dev);
  759. smmu->translation_enable_0 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_0);
  760. smmu->translation_enable_1 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_1);
  761. smmu->translation_enable_2 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_2);
  762. smmu->asid_security = smmu_read(smmu, SMMU_ASID_SECURITY);
  763. return 0;
  764. }
  765. static int tegra_smmu_resume(struct device *dev)
  766. {
  767. struct smmu_device *smmu = dev_get_drvdata(dev);
  768. unsigned long flags;
  769. int err;
  770. spin_lock_irqsave(&smmu->lock, flags);
  771. err = smmu_setup_regs(smmu);
  772. spin_unlock_irqrestore(&smmu->lock, flags);
  773. return err;
  774. }
  775. static int tegra_smmu_probe(struct platform_device *pdev)
  776. {
  777. struct smmu_device *smmu;
  778. struct device *dev = &pdev->dev;
  779. int i, asids, err = 0;
  780. dma_addr_t uninitialized_var(base);
  781. size_t bytes, uninitialized_var(size);
  782. if (smmu_handle)
  783. return -EIO;
  784. BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
  785. if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
  786. return -ENODEV;
  787. bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
  788. smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
  789. if (!smmu) {
  790. dev_err(dev, "failed to allocate smmu_device\n");
  791. return -ENOMEM;
  792. }
  793. for (i = 0; i < ARRAY_SIZE(smmu->regs); i++) {
  794. struct resource *res;
  795. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  796. if (!res)
  797. return -ENODEV;
  798. smmu->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
  799. if (!smmu->regs[i])
  800. return -EBUSY;
  801. }
  802. err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
  803. if (err)
  804. return -ENODEV;
  805. if (size & SMMU_PAGE_MASK)
  806. return -EINVAL;
  807. size >>= SMMU_PAGE_SHIFT;
  808. if (!size)
  809. return -EINVAL;
  810. smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
  811. if (!smmu->ahb)
  812. return -ENODEV;
  813. smmu->dev = dev;
  814. smmu->num_as = asids;
  815. smmu->iovmm_base = base;
  816. smmu->page_count = size;
  817. smmu->translation_enable_0 = ~0;
  818. smmu->translation_enable_1 = ~0;
  819. smmu->translation_enable_2 = ~0;
  820. smmu->asid_security = 0;
  821. for (i = 0; i < smmu->num_as; i++) {
  822. struct smmu_as *as = &smmu->as[i];
  823. as->smmu = smmu;
  824. as->asid = i;
  825. as->pdir_attr = _PDIR_ATTR;
  826. as->pde_attr = _PDE_ATTR;
  827. as->pte_attr = _PTE_ATTR;
  828. spin_lock_init(&as->lock);
  829. INIT_LIST_HEAD(&as->client);
  830. }
  831. spin_lock_init(&smmu->lock);
  832. err = smmu_setup_regs(smmu);
  833. if (err)
  834. return err;
  835. platform_set_drvdata(pdev, smmu);
  836. smmu->avp_vector_page = alloc_page(GFP_KERNEL);
  837. if (!smmu->avp_vector_page)
  838. return -ENOMEM;
  839. smmu_handle = smmu;
  840. return 0;
  841. }
  842. static int tegra_smmu_remove(struct platform_device *pdev)
  843. {
  844. struct smmu_device *smmu = platform_get_drvdata(pdev);
  845. int i;
  846. smmu_write(smmu, SMMU_CONFIG_DISABLE, SMMU_CONFIG);
  847. for (i = 0; i < smmu->num_as; i++)
  848. free_pdir(&smmu->as[i]);
  849. __free_page(smmu->avp_vector_page);
  850. smmu_handle = NULL;
  851. return 0;
  852. }
  853. const struct dev_pm_ops tegra_smmu_pm_ops = {
  854. .suspend = tegra_smmu_suspend,
  855. .resume = tegra_smmu_resume,
  856. };
  857. #ifdef CONFIG_OF
  858. static struct of_device_id tegra_smmu_of_match[] __devinitdata = {
  859. { .compatible = "nvidia,tegra30-smmu", },
  860. { },
  861. };
  862. MODULE_DEVICE_TABLE(of, tegra_smmu_of_match);
  863. #endif
  864. static struct platform_driver tegra_smmu_driver = {
  865. .probe = tegra_smmu_probe,
  866. .remove = tegra_smmu_remove,
  867. .driver = {
  868. .owner = THIS_MODULE,
  869. .name = "tegra-smmu",
  870. .pm = &tegra_smmu_pm_ops,
  871. .of_match_table = of_match_ptr(tegra_smmu_of_match),
  872. },
  873. };
  874. static int __devinit tegra_smmu_init(void)
  875. {
  876. bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
  877. return platform_driver_register(&tegra_smmu_driver);
  878. }
  879. static void __exit tegra_smmu_exit(void)
  880. {
  881. platform_driver_unregister(&tegra_smmu_driver);
  882. }
  883. subsys_initcall(tegra_smmu_init);
  884. module_exit(tegra_smmu_exit);
  885. MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
  886. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  887. MODULE_ALIAS("platform:tegra-smmu");
  888. MODULE_LICENSE("GPL v2");