tegra-smmu.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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 <linux/debugfs.h>
  34. #include <linux/seq_file.h>
  35. #include <asm/page.h>
  36. #include <asm/cacheflush.h>
  37. #include <mach/iomap.h>
  38. #include <mach/tegra-ahb.h>
  39. enum smmu_hwgrp {
  40. HWGRP_AFI,
  41. HWGRP_AVPC,
  42. HWGRP_DC,
  43. HWGRP_DCB,
  44. HWGRP_EPP,
  45. HWGRP_G2,
  46. HWGRP_HC,
  47. HWGRP_HDA,
  48. HWGRP_ISP,
  49. HWGRP_MPE,
  50. HWGRP_NV,
  51. HWGRP_NV2,
  52. HWGRP_PPCS,
  53. HWGRP_SATA,
  54. HWGRP_VDE,
  55. HWGRP_VI,
  56. HWGRP_COUNT,
  57. HWGRP_END = ~0,
  58. };
  59. #define HWG_AFI (1 << HWGRP_AFI)
  60. #define HWG_AVPC (1 << HWGRP_AVPC)
  61. #define HWG_DC (1 << HWGRP_DC)
  62. #define HWG_DCB (1 << HWGRP_DCB)
  63. #define HWG_EPP (1 << HWGRP_EPP)
  64. #define HWG_G2 (1 << HWGRP_G2)
  65. #define HWG_HC (1 << HWGRP_HC)
  66. #define HWG_HDA (1 << HWGRP_HDA)
  67. #define HWG_ISP (1 << HWGRP_ISP)
  68. #define HWG_MPE (1 << HWGRP_MPE)
  69. #define HWG_NV (1 << HWGRP_NV)
  70. #define HWG_NV2 (1 << HWGRP_NV2)
  71. #define HWG_PPCS (1 << HWGRP_PPCS)
  72. #define HWG_SATA (1 << HWGRP_SATA)
  73. #define HWG_VDE (1 << HWGRP_VDE)
  74. #define HWG_VI (1 << HWGRP_VI)
  75. /* bitmap of the page sizes currently supported */
  76. #define SMMU_IOMMU_PGSIZES (SZ_4K)
  77. #define SMMU_CONFIG 0x10
  78. #define SMMU_CONFIG_DISABLE 0
  79. #define SMMU_CONFIG_ENABLE 1
  80. /* REVISIT: To support multiple MCs */
  81. enum {
  82. _MC = 0,
  83. };
  84. enum {
  85. _TLB = 0,
  86. _PTC,
  87. };
  88. #define SMMU_CACHE_CONFIG_BASE 0x14
  89. #define __SMMU_CACHE_CONFIG(mc, cache) (SMMU_CACHE_CONFIG_BASE + 4 * cache)
  90. #define SMMU_CACHE_CONFIG(cache) __SMMU_CACHE_CONFIG(_MC, cache)
  91. #define SMMU_CACHE_CONFIG_STATS_SHIFT 31
  92. #define SMMU_CACHE_CONFIG_STATS_ENABLE (1 << SMMU_CACHE_CONFIG_STATS_SHIFT)
  93. #define SMMU_CACHE_CONFIG_STATS_TEST_SHIFT 30
  94. #define SMMU_CACHE_CONFIG_STATS_TEST (1 << SMMU_CACHE_CONFIG_STATS_TEST_SHIFT)
  95. #define SMMU_TLB_CONFIG_HIT_UNDER_MISS__ENABLE (1 << 29)
  96. #define SMMU_TLB_CONFIG_ACTIVE_LINES__VALUE 0x10
  97. #define SMMU_TLB_CONFIG_RESET_VAL 0x20000010
  98. #define SMMU_PTC_CONFIG_CACHE__ENABLE (1 << 29)
  99. #define SMMU_PTC_CONFIG_INDEX_MAP__PATTERN 0x3f
  100. #define SMMU_PTC_CONFIG_RESET_VAL 0x2000003f
  101. #define SMMU_PTB_ASID 0x1c
  102. #define SMMU_PTB_ASID_CURRENT_SHIFT 0
  103. #define SMMU_PTB_DATA 0x20
  104. #define SMMU_PTB_DATA_RESET_VAL 0
  105. #define SMMU_PTB_DATA_ASID_NONSECURE_SHIFT 29
  106. #define SMMU_PTB_DATA_ASID_WRITABLE_SHIFT 30
  107. #define SMMU_PTB_DATA_ASID_READABLE_SHIFT 31
  108. #define SMMU_TLB_FLUSH 0x30
  109. #define SMMU_TLB_FLUSH_VA_MATCH_ALL 0
  110. #define SMMU_TLB_FLUSH_VA_MATCH_SECTION 2
  111. #define SMMU_TLB_FLUSH_VA_MATCH_GROUP 3
  112. #define SMMU_TLB_FLUSH_ASID_SHIFT 29
  113. #define SMMU_TLB_FLUSH_ASID_MATCH_DISABLE 0
  114. #define SMMU_TLB_FLUSH_ASID_MATCH_ENABLE 1
  115. #define SMMU_TLB_FLUSH_ASID_MATCH_SHIFT 31
  116. #define SMMU_PTC_FLUSH 0x34
  117. #define SMMU_PTC_FLUSH_TYPE_ALL 0
  118. #define SMMU_PTC_FLUSH_TYPE_ADR 1
  119. #define SMMU_PTC_FLUSH_ADR_SHIFT 4
  120. #define SMMU_ASID_SECURITY 0x38
  121. #define SMMU_STATS_CACHE_COUNT_BASE 0x1f0
  122. #define SMMU_STATS_CACHE_COUNT(mc, cache, hitmiss) \
  123. (SMMU_STATS_CACHE_COUNT_BASE + 8 * cache + 4 * hitmiss)
  124. #define SMMU_TRANSLATION_ENABLE_0 0x228
  125. #define SMMU_TRANSLATION_ENABLE_1 0x22c
  126. #define SMMU_TRANSLATION_ENABLE_2 0x230
  127. #define SMMU_AFI_ASID 0x238 /* PCIE */
  128. #define SMMU_AVPC_ASID 0x23c /* AVP */
  129. #define SMMU_DC_ASID 0x240 /* Display controller */
  130. #define SMMU_DCB_ASID 0x244 /* Display controller B */
  131. #define SMMU_EPP_ASID 0x248 /* Encoder pre-processor */
  132. #define SMMU_G2_ASID 0x24c /* 2D engine */
  133. #define SMMU_HC_ASID 0x250 /* Host1x */
  134. #define SMMU_HDA_ASID 0x254 /* High-def audio */
  135. #define SMMU_ISP_ASID 0x258 /* Image signal processor */
  136. #define SMMU_MPE_ASID 0x264 /* MPEG encoder */
  137. #define SMMU_NV_ASID 0x268 /* (3D) */
  138. #define SMMU_NV2_ASID 0x26c /* (3D) */
  139. #define SMMU_PPCS_ASID 0x270 /* AHB */
  140. #define SMMU_SATA_ASID 0x278 /* SATA */
  141. #define SMMU_VDE_ASID 0x27c /* Video decoder */
  142. #define SMMU_VI_ASID 0x280 /* Video input */
  143. #define SMMU_PDE_NEXT_SHIFT 28
  144. #define SMMU_TLB_FLUSH_VA_SECTION__MASK 0xffc00000
  145. #define SMMU_TLB_FLUSH_VA_SECTION__SHIFT 12 /* right shift */
  146. #define SMMU_TLB_FLUSH_VA_GROUP__MASK 0xffffc000
  147. #define SMMU_TLB_FLUSH_VA_GROUP__SHIFT 12 /* right shift */
  148. #define SMMU_TLB_FLUSH_VA(iova, which) \
  149. ((((iova) & SMMU_TLB_FLUSH_VA_##which##__MASK) >> \
  150. SMMU_TLB_FLUSH_VA_##which##__SHIFT) | \
  151. SMMU_TLB_FLUSH_VA_MATCH_##which)
  152. #define SMMU_PTB_ASID_CUR(n) \
  153. ((n) << SMMU_PTB_ASID_CURRENT_SHIFT)
  154. #define SMMU_TLB_FLUSH_ASID_MATCH_disable \
  155. (SMMU_TLB_FLUSH_ASID_MATCH_DISABLE << \
  156. SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
  157. #define SMMU_TLB_FLUSH_ASID_MATCH__ENABLE \
  158. (SMMU_TLB_FLUSH_ASID_MATCH_ENABLE << \
  159. SMMU_TLB_FLUSH_ASID_MATCH_SHIFT)
  160. #define SMMU_PAGE_SHIFT 12
  161. #define SMMU_PAGE_SIZE (1 << SMMU_PAGE_SHIFT)
  162. #define SMMU_PAGE_MASK ((1 << SMMU_PAGE_SHIFT) - 1)
  163. #define SMMU_PDIR_COUNT 1024
  164. #define SMMU_PDIR_SIZE (sizeof(unsigned long) * SMMU_PDIR_COUNT)
  165. #define SMMU_PTBL_COUNT 1024
  166. #define SMMU_PTBL_SIZE (sizeof(unsigned long) * SMMU_PTBL_COUNT)
  167. #define SMMU_PDIR_SHIFT 12
  168. #define SMMU_PDE_SHIFT 12
  169. #define SMMU_PTE_SHIFT 12
  170. #define SMMU_PFN_MASK 0x000fffff
  171. #define SMMU_ADDR_TO_PFN(addr) ((addr) >> 12)
  172. #define SMMU_ADDR_TO_PDN(addr) ((addr) >> 22)
  173. #define SMMU_PDN_TO_ADDR(pdn) ((pdn) << 22)
  174. #define _READABLE (1 << SMMU_PTB_DATA_ASID_READABLE_SHIFT)
  175. #define _WRITABLE (1 << SMMU_PTB_DATA_ASID_WRITABLE_SHIFT)
  176. #define _NONSECURE (1 << SMMU_PTB_DATA_ASID_NONSECURE_SHIFT)
  177. #define _PDE_NEXT (1 << SMMU_PDE_NEXT_SHIFT)
  178. #define _MASK_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  179. #define _PDIR_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  180. #define _PDE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  181. #define _PDE_ATTR_N (_PDE_ATTR | _PDE_NEXT)
  182. #define _PDE_VACANT(pdn) (((pdn) << 10) | _PDE_ATTR)
  183. #define _PTE_ATTR (_READABLE | _WRITABLE | _NONSECURE)
  184. #define _PTE_VACANT(addr) (((addr) >> SMMU_PAGE_SHIFT) | _PTE_ATTR)
  185. #define SMMU_MK_PDIR(page, attr) \
  186. ((page_to_phys(page) >> SMMU_PDIR_SHIFT) | (attr))
  187. #define SMMU_MK_PDE(page, attr) \
  188. (unsigned long)((page_to_phys(page) >> SMMU_PDE_SHIFT) | (attr))
  189. #define SMMU_EX_PTBL_PAGE(pde) \
  190. pfn_to_page((unsigned long)(pde) & SMMU_PFN_MASK)
  191. #define SMMU_PFN_TO_PTE(pfn, attr) (unsigned long)((pfn) | (attr))
  192. #define SMMU_ASID_ENABLE(asid) ((asid) | (1 << 31))
  193. #define SMMU_ASID_DISABLE 0
  194. #define SMMU_ASID_ASID(n) ((n) & ~SMMU_ASID_ENABLE(0))
  195. #define NUM_SMMU_REG_BANKS 3
  196. #define smmu_client_enable_hwgrp(c, m) smmu_client_set_hwgrp(c, m, 1)
  197. #define smmu_client_disable_hwgrp(c) smmu_client_set_hwgrp(c, 0, 0)
  198. #define __smmu_client_enable_hwgrp(c, m) __smmu_client_set_hwgrp(c, m, 1)
  199. #define __smmu_client_disable_hwgrp(c) __smmu_client_set_hwgrp(c, 0, 0)
  200. #define HWGRP_INIT(client) [HWGRP_##client] = SMMU_##client##_ASID
  201. static const u32 smmu_hwgrp_asid_reg[] = {
  202. HWGRP_INIT(AFI),
  203. HWGRP_INIT(AVPC),
  204. HWGRP_INIT(DC),
  205. HWGRP_INIT(DCB),
  206. HWGRP_INIT(EPP),
  207. HWGRP_INIT(G2),
  208. HWGRP_INIT(HC),
  209. HWGRP_INIT(HDA),
  210. HWGRP_INIT(ISP),
  211. HWGRP_INIT(MPE),
  212. HWGRP_INIT(NV),
  213. HWGRP_INIT(NV2),
  214. HWGRP_INIT(PPCS),
  215. HWGRP_INIT(SATA),
  216. HWGRP_INIT(VDE),
  217. HWGRP_INIT(VI),
  218. };
  219. #define HWGRP_ASID_REG(x) (smmu_hwgrp_asid_reg[x])
  220. /*
  221. * Per client for address space
  222. */
  223. struct smmu_client {
  224. struct device *dev;
  225. struct list_head list;
  226. struct smmu_as *as;
  227. u32 hwgrp;
  228. };
  229. /*
  230. * Per address space
  231. */
  232. struct smmu_as {
  233. struct smmu_device *smmu; /* back pointer to container */
  234. unsigned int asid;
  235. spinlock_t lock; /* for pagetable */
  236. struct page *pdir_page;
  237. unsigned long pdir_attr;
  238. unsigned long pde_attr;
  239. unsigned long pte_attr;
  240. unsigned int *pte_count;
  241. struct list_head client;
  242. spinlock_t client_lock; /* for client list */
  243. };
  244. struct smmu_debugfs_info {
  245. struct smmu_device *smmu;
  246. int mc;
  247. int cache;
  248. };
  249. /*
  250. * Per SMMU device - IOMMU device
  251. */
  252. struct smmu_device {
  253. void __iomem *regs[NUM_SMMU_REG_BANKS];
  254. unsigned long iovmm_base; /* remappable base address */
  255. unsigned long page_count; /* total remappable size */
  256. spinlock_t lock;
  257. char *name;
  258. struct device *dev;
  259. struct page *avp_vector_page; /* dummy page shared by all AS's */
  260. /*
  261. * Register image savers for suspend/resume
  262. */
  263. unsigned long translation_enable_0;
  264. unsigned long translation_enable_1;
  265. unsigned long translation_enable_2;
  266. unsigned long asid_security;
  267. struct dentry *debugfs_root;
  268. struct smmu_debugfs_info *debugfs_info;
  269. struct device_node *ahb;
  270. int num_as;
  271. struct smmu_as as[0]; /* Run-time allocated array */
  272. };
  273. static struct smmu_device *smmu_handle; /* unique for a system */
  274. /*
  275. * SMMU register accessors
  276. */
  277. static inline u32 smmu_read(struct smmu_device *smmu, size_t offs)
  278. {
  279. BUG_ON(offs < 0x10);
  280. if (offs < 0x3c)
  281. return readl(smmu->regs[0] + offs - 0x10);
  282. BUG_ON(offs < 0x1f0);
  283. if (offs < 0x200)
  284. return readl(smmu->regs[1] + offs - 0x1f0);
  285. BUG_ON(offs < 0x228);
  286. if (offs < 0x284)
  287. return readl(smmu->regs[2] + offs - 0x228);
  288. BUG();
  289. }
  290. static inline void smmu_write(struct smmu_device *smmu, u32 val, size_t offs)
  291. {
  292. BUG_ON(offs < 0x10);
  293. if (offs < 0x3c) {
  294. writel(val, smmu->regs[0] + offs - 0x10);
  295. return;
  296. }
  297. BUG_ON(offs < 0x1f0);
  298. if (offs < 0x200) {
  299. writel(val, smmu->regs[1] + offs - 0x1f0);
  300. return;
  301. }
  302. BUG_ON(offs < 0x228);
  303. if (offs < 0x284) {
  304. writel(val, smmu->regs[2] + offs - 0x228);
  305. return;
  306. }
  307. BUG();
  308. }
  309. #define VA_PAGE_TO_PA(va, page) \
  310. (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
  311. #define FLUSH_CPU_DCACHE(va, page, size) \
  312. do { \
  313. unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
  314. __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
  315. outer_flush_range(_pa_, _pa_+(size_t)(size)); \
  316. } while (0)
  317. /*
  318. * Any interaction between any block on PPSB and a block on APB or AHB
  319. * must have these read-back barriers to ensure the APB/AHB bus
  320. * transaction is complete before initiating activity on the PPSB
  321. * block.
  322. */
  323. #define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
  324. #define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
  325. static int __smmu_client_set_hwgrp(struct smmu_client *c,
  326. unsigned long map, int on)
  327. {
  328. int i;
  329. struct smmu_as *as = c->as;
  330. u32 val, offs, mask = SMMU_ASID_ENABLE(as->asid);
  331. struct smmu_device *smmu = as->smmu;
  332. WARN_ON(!on && map);
  333. if (on && !map)
  334. return -EINVAL;
  335. if (!on)
  336. map = smmu_client_hwgrp(c);
  337. for_each_set_bit(i, &map, HWGRP_COUNT) {
  338. offs = HWGRP_ASID_REG(i);
  339. val = smmu_read(smmu, offs);
  340. if (on) {
  341. if (WARN_ON(val & mask))
  342. goto err_hw_busy;
  343. val |= mask;
  344. } else {
  345. WARN_ON((val & mask) == mask);
  346. val &= ~mask;
  347. }
  348. smmu_write(smmu, val, offs);
  349. }
  350. FLUSH_SMMU_REGS(smmu);
  351. c->hwgrp = map;
  352. return 0;
  353. err_hw_busy:
  354. for_each_set_bit(i, &map, HWGRP_COUNT) {
  355. offs = HWGRP_ASID_REG(i);
  356. val = smmu_read(smmu, offs);
  357. val &= ~mask;
  358. smmu_write(smmu, val, offs);
  359. }
  360. return -EBUSY;
  361. }
  362. static int smmu_client_set_hwgrp(struct smmu_client *c, u32 map, int on)
  363. {
  364. u32 val;
  365. unsigned long flags;
  366. struct smmu_as *as = c->as;
  367. struct smmu_device *smmu = as->smmu;
  368. spin_lock_irqsave(&smmu->lock, flags);
  369. val = __smmu_client_set_hwgrp(c, map, on);
  370. spin_unlock_irqrestore(&smmu->lock, flags);
  371. return val;
  372. }
  373. /*
  374. * Flush all TLB entries and all PTC entries
  375. * Caller must lock smmu
  376. */
  377. static void smmu_flush_regs(struct smmu_device *smmu, int enable)
  378. {
  379. u32 val;
  380. smmu_write(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
  381. FLUSH_SMMU_REGS(smmu);
  382. val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
  383. SMMU_TLB_FLUSH_ASID_MATCH_disable;
  384. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  385. if (enable)
  386. smmu_write(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
  387. FLUSH_SMMU_REGS(smmu);
  388. }
  389. static int smmu_setup_regs(struct smmu_device *smmu)
  390. {
  391. int i;
  392. u32 val;
  393. for (i = 0; i < smmu->num_as; i++) {
  394. struct smmu_as *as = &smmu->as[i];
  395. struct smmu_client *c;
  396. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  397. val = as->pdir_page ?
  398. SMMU_MK_PDIR(as->pdir_page, as->pdir_attr) :
  399. SMMU_PTB_DATA_RESET_VAL;
  400. smmu_write(smmu, val, SMMU_PTB_DATA);
  401. list_for_each_entry(c, &as->client, list)
  402. __smmu_client_set_hwgrp(c, c->hwgrp, 1);
  403. }
  404. smmu_write(smmu, smmu->translation_enable_0, SMMU_TRANSLATION_ENABLE_0);
  405. smmu_write(smmu, smmu->translation_enable_1, SMMU_TRANSLATION_ENABLE_1);
  406. smmu_write(smmu, smmu->translation_enable_2, SMMU_TRANSLATION_ENABLE_2);
  407. smmu_write(smmu, smmu->asid_security, SMMU_ASID_SECURITY);
  408. smmu_write(smmu, SMMU_TLB_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_TLB));
  409. smmu_write(smmu, SMMU_PTC_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_PTC));
  410. smmu_flush_regs(smmu, 1);
  411. return tegra_ahb_enable_smmu(smmu->ahb);
  412. }
  413. static void flush_ptc_and_tlb(struct smmu_device *smmu,
  414. struct smmu_as *as, dma_addr_t iova,
  415. unsigned long *pte, struct page *page, int is_pde)
  416. {
  417. u32 val;
  418. unsigned long tlb_flush_va = is_pde
  419. ? SMMU_TLB_FLUSH_VA(iova, SECTION)
  420. : SMMU_TLB_FLUSH_VA(iova, GROUP);
  421. val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pte, page);
  422. smmu_write(smmu, val, SMMU_PTC_FLUSH);
  423. FLUSH_SMMU_REGS(smmu);
  424. val = tlb_flush_va |
  425. SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
  426. (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
  427. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  428. FLUSH_SMMU_REGS(smmu);
  429. }
  430. static void free_ptbl(struct smmu_as *as, dma_addr_t iova)
  431. {
  432. unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
  433. unsigned long *pdir = (unsigned long *)page_address(as->pdir_page);
  434. if (pdir[pdn] != _PDE_VACANT(pdn)) {
  435. dev_dbg(as->smmu->dev, "pdn: %lx\n", pdn);
  436. ClearPageReserved(SMMU_EX_PTBL_PAGE(pdir[pdn]));
  437. __free_page(SMMU_EX_PTBL_PAGE(pdir[pdn]));
  438. pdir[pdn] = _PDE_VACANT(pdn);
  439. FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
  440. flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
  441. as->pdir_page, 1);
  442. }
  443. }
  444. static void free_pdir(struct smmu_as *as)
  445. {
  446. unsigned addr;
  447. int count;
  448. struct device *dev = as->smmu->dev;
  449. if (!as->pdir_page)
  450. return;
  451. addr = as->smmu->iovmm_base;
  452. count = as->smmu->page_count;
  453. while (count-- > 0) {
  454. free_ptbl(as, addr);
  455. addr += SMMU_PAGE_SIZE * SMMU_PTBL_COUNT;
  456. }
  457. ClearPageReserved(as->pdir_page);
  458. __free_page(as->pdir_page);
  459. as->pdir_page = NULL;
  460. devm_kfree(dev, as->pte_count);
  461. as->pte_count = NULL;
  462. }
  463. /*
  464. * Maps PTBL for given iova and returns the PTE address
  465. * Caller must unmap the mapped PTBL returned in *ptbl_page_p
  466. */
  467. static unsigned long *locate_pte(struct smmu_as *as,
  468. dma_addr_t iova, bool allocate,
  469. struct page **ptbl_page_p,
  470. unsigned int **count)
  471. {
  472. unsigned long ptn = SMMU_ADDR_TO_PFN(iova);
  473. unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
  474. unsigned long *pdir = page_address(as->pdir_page);
  475. unsigned long *ptbl;
  476. if (pdir[pdn] != _PDE_VACANT(pdn)) {
  477. /* Mapped entry table already exists */
  478. *ptbl_page_p = SMMU_EX_PTBL_PAGE(pdir[pdn]);
  479. ptbl = page_address(*ptbl_page_p);
  480. } else if (!allocate) {
  481. return NULL;
  482. } else {
  483. int pn;
  484. unsigned long addr = SMMU_PDN_TO_ADDR(pdn);
  485. /* Vacant - allocate a new page table */
  486. dev_dbg(as->smmu->dev, "New PTBL pdn: %lx\n", pdn);
  487. *ptbl_page_p = alloc_page(GFP_ATOMIC);
  488. if (!*ptbl_page_p) {
  489. dev_err(as->smmu->dev,
  490. "failed to allocate smmu_device page table\n");
  491. return NULL;
  492. }
  493. SetPageReserved(*ptbl_page_p);
  494. ptbl = (unsigned long *)page_address(*ptbl_page_p);
  495. for (pn = 0; pn < SMMU_PTBL_COUNT;
  496. pn++, addr += SMMU_PAGE_SIZE) {
  497. ptbl[pn] = _PTE_VACANT(addr);
  498. }
  499. FLUSH_CPU_DCACHE(ptbl, *ptbl_page_p, SMMU_PTBL_SIZE);
  500. pdir[pdn] = SMMU_MK_PDE(*ptbl_page_p,
  501. as->pde_attr | _PDE_NEXT);
  502. FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
  503. flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
  504. as->pdir_page, 1);
  505. }
  506. *count = &as->pte_count[pdn];
  507. return &ptbl[ptn % SMMU_PTBL_COUNT];
  508. }
  509. #ifdef CONFIG_SMMU_SIG_DEBUG
  510. static void put_signature(struct smmu_as *as,
  511. dma_addr_t iova, unsigned long pfn)
  512. {
  513. struct page *page;
  514. unsigned long *vaddr;
  515. page = pfn_to_page(pfn);
  516. vaddr = page_address(page);
  517. if (!vaddr)
  518. return;
  519. vaddr[0] = iova;
  520. vaddr[1] = pfn << PAGE_SHIFT;
  521. FLUSH_CPU_DCACHE(vaddr, page, sizeof(vaddr[0]) * 2);
  522. }
  523. #else
  524. static inline void put_signature(struct smmu_as *as,
  525. unsigned long addr, unsigned long pfn)
  526. {
  527. }
  528. #endif
  529. /*
  530. * Caller must not hold as->lock
  531. */
  532. static int alloc_pdir(struct smmu_as *as)
  533. {
  534. unsigned long *pdir, flags;
  535. int pdn, err = 0;
  536. u32 val;
  537. struct smmu_device *smmu = as->smmu;
  538. struct page *page;
  539. unsigned int *cnt;
  540. /*
  541. * do the allocation, then grab as->lock
  542. */
  543. cnt = devm_kzalloc(smmu->dev,
  544. sizeof(cnt[0]) * SMMU_PDIR_COUNT,
  545. GFP_KERNEL);
  546. page = alloc_page(GFP_KERNEL | __GFP_DMA);
  547. spin_lock_irqsave(&as->lock, flags);
  548. if (as->pdir_page) {
  549. /* We raced, free the redundant */
  550. err = -EAGAIN;
  551. goto err_out;
  552. }
  553. if (!page || !cnt) {
  554. dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
  555. err = -ENOMEM;
  556. goto err_out;
  557. }
  558. as->pdir_page = page;
  559. as->pte_count = cnt;
  560. SetPageReserved(as->pdir_page);
  561. pdir = page_address(as->pdir_page);
  562. for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
  563. pdir[pdn] = _PDE_VACANT(pdn);
  564. FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
  565. val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
  566. smmu_write(smmu, val, SMMU_PTC_FLUSH);
  567. FLUSH_SMMU_REGS(as->smmu);
  568. val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
  569. SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
  570. (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
  571. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  572. FLUSH_SMMU_REGS(as->smmu);
  573. spin_unlock_irqrestore(&as->lock, flags);
  574. return 0;
  575. err_out:
  576. spin_unlock_irqrestore(&as->lock, flags);
  577. devm_kfree(smmu->dev, cnt);
  578. if (page)
  579. __free_page(page);
  580. return err;
  581. }
  582. static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
  583. {
  584. unsigned long *pte;
  585. struct page *page;
  586. unsigned int *count;
  587. pte = locate_pte(as, iova, false, &page, &count);
  588. if (WARN_ON(!pte))
  589. return;
  590. if (WARN_ON(*pte == _PTE_VACANT(iova)))
  591. return;
  592. *pte = _PTE_VACANT(iova);
  593. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  594. flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
  595. if (!--(*count))
  596. free_ptbl(as, iova);
  597. }
  598. static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
  599. unsigned long pfn)
  600. {
  601. struct smmu_device *smmu = as->smmu;
  602. unsigned long *pte;
  603. unsigned int *count;
  604. struct page *page;
  605. pte = locate_pte(as, iova, true, &page, &count);
  606. if (WARN_ON(!pte))
  607. return;
  608. if (*pte == _PTE_VACANT(iova))
  609. (*count)++;
  610. *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
  611. if (unlikely((*pte == _PTE_VACANT(iova))))
  612. (*count)--;
  613. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  614. flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
  615. put_signature(as, iova, pfn);
  616. }
  617. static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
  618. phys_addr_t pa, size_t bytes, int prot)
  619. {
  620. struct smmu_as *as = domain->priv;
  621. unsigned long pfn = __phys_to_pfn(pa);
  622. unsigned long flags;
  623. dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
  624. if (!pfn_valid(pfn))
  625. return -ENOMEM;
  626. spin_lock_irqsave(&as->lock, flags);
  627. __smmu_iommu_map_pfn(as, iova, pfn);
  628. spin_unlock_irqrestore(&as->lock, flags);
  629. return 0;
  630. }
  631. static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  632. size_t bytes)
  633. {
  634. struct smmu_as *as = domain->priv;
  635. unsigned long flags;
  636. dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
  637. spin_lock_irqsave(&as->lock, flags);
  638. __smmu_iommu_unmap(as, iova);
  639. spin_unlock_irqrestore(&as->lock, flags);
  640. return SMMU_PAGE_SIZE;
  641. }
  642. static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
  643. unsigned long iova)
  644. {
  645. struct smmu_as *as = domain->priv;
  646. unsigned long *pte;
  647. unsigned int *count;
  648. struct page *page;
  649. unsigned long pfn;
  650. unsigned long flags;
  651. spin_lock_irqsave(&as->lock, flags);
  652. pte = locate_pte(as, iova, true, &page, &count);
  653. pfn = *pte & SMMU_PFN_MASK;
  654. WARN_ON(!pfn_valid(pfn));
  655. dev_dbg(as->smmu->dev,
  656. "iova:%08lx pfn:%08lx asid:%d\n", iova, pfn, as->asid);
  657. spin_unlock_irqrestore(&as->lock, flags);
  658. return PFN_PHYS(pfn);
  659. }
  660. static int smmu_iommu_domain_has_cap(struct iommu_domain *domain,
  661. unsigned long cap)
  662. {
  663. return 0;
  664. }
  665. static int smmu_iommu_attach_dev(struct iommu_domain *domain,
  666. struct device *dev)
  667. {
  668. struct smmu_as *as = domain->priv;
  669. struct smmu_device *smmu = as->smmu;
  670. struct smmu_client *client, *c;
  671. u32 map;
  672. int err;
  673. client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
  674. if (!client)
  675. return -ENOMEM;
  676. client->dev = dev;
  677. client->as = as;
  678. map = (unsigned long)dev->platform_data;
  679. if (!map)
  680. return -EINVAL;
  681. err = smmu_client_enable_hwgrp(client, map);
  682. if (err)
  683. goto err_hwgrp;
  684. spin_lock(&as->client_lock);
  685. list_for_each_entry(c, &as->client, list) {
  686. if (c->dev == dev) {
  687. dev_err(smmu->dev,
  688. "%s is already attached\n", dev_name(c->dev));
  689. err = -EINVAL;
  690. goto err_client;
  691. }
  692. }
  693. list_add(&client->list, &as->client);
  694. spin_unlock(&as->client_lock);
  695. /*
  696. * Reserve "page zero" for AVP vectors using a common dummy
  697. * page.
  698. */
  699. if (map & HWG_AVPC) {
  700. struct page *page;
  701. page = as->smmu->avp_vector_page;
  702. __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
  703. pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
  704. }
  705. dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
  706. return 0;
  707. err_client:
  708. smmu_client_disable_hwgrp(client);
  709. spin_unlock(&as->client_lock);
  710. err_hwgrp:
  711. devm_kfree(smmu->dev, client);
  712. return err;
  713. }
  714. static void smmu_iommu_detach_dev(struct iommu_domain *domain,
  715. struct device *dev)
  716. {
  717. struct smmu_as *as = domain->priv;
  718. struct smmu_device *smmu = as->smmu;
  719. struct smmu_client *c;
  720. spin_lock(&as->client_lock);
  721. list_for_each_entry(c, &as->client, list) {
  722. if (c->dev == dev) {
  723. smmu_client_disable_hwgrp(c);
  724. list_del(&c->list);
  725. devm_kfree(smmu->dev, c);
  726. c->as = NULL;
  727. dev_dbg(smmu->dev,
  728. "%s is detached\n", dev_name(c->dev));
  729. goto out;
  730. }
  731. }
  732. dev_err(smmu->dev, "Couldn't find %s\n", dev_name(dev));
  733. out:
  734. spin_unlock(&as->client_lock);
  735. }
  736. static int smmu_iommu_domain_init(struct iommu_domain *domain)
  737. {
  738. int i, err = -EAGAIN;
  739. unsigned long flags;
  740. struct smmu_as *as;
  741. struct smmu_device *smmu = smmu_handle;
  742. /* Look for a free AS with lock held */
  743. for (i = 0; i < smmu->num_as; i++) {
  744. as = &smmu->as[i];
  745. if (as->pdir_page)
  746. continue;
  747. err = alloc_pdir(as);
  748. if (!err)
  749. goto found;
  750. if (err != -EAGAIN)
  751. break;
  752. }
  753. if (i == smmu->num_as)
  754. dev_err(smmu->dev, "no free AS\n");
  755. return err;
  756. found:
  757. spin_lock_irqsave(&smmu->lock, flags);
  758. /* Update PDIR register */
  759. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  760. smmu_write(smmu,
  761. SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
  762. FLUSH_SMMU_REGS(smmu);
  763. spin_unlock_irqrestore(&smmu->lock, flags);
  764. domain->priv = as;
  765. domain->geometry.aperture_start = smmu->iovmm_base;
  766. domain->geometry.aperture_end = smmu->iovmm_base +
  767. smmu->page_count * SMMU_PAGE_SIZE - 1;
  768. domain->geometry.force_aperture = true;
  769. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  770. return 0;
  771. }
  772. static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
  773. {
  774. struct smmu_as *as = domain->priv;
  775. struct smmu_device *smmu = as->smmu;
  776. unsigned long flags;
  777. spin_lock_irqsave(&as->lock, flags);
  778. if (as->pdir_page) {
  779. spin_lock(&smmu->lock);
  780. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  781. smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
  782. FLUSH_SMMU_REGS(smmu);
  783. spin_unlock(&smmu->lock);
  784. free_pdir(as);
  785. }
  786. if (!list_empty(&as->client)) {
  787. struct smmu_client *c;
  788. list_for_each_entry(c, &as->client, list)
  789. smmu_iommu_detach_dev(domain, c->dev);
  790. }
  791. spin_unlock_irqrestore(&as->lock, flags);
  792. domain->priv = NULL;
  793. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  794. }
  795. static struct iommu_ops smmu_iommu_ops = {
  796. .domain_init = smmu_iommu_domain_init,
  797. .domain_destroy = smmu_iommu_domain_destroy,
  798. .attach_dev = smmu_iommu_attach_dev,
  799. .detach_dev = smmu_iommu_detach_dev,
  800. .map = smmu_iommu_map,
  801. .unmap = smmu_iommu_unmap,
  802. .iova_to_phys = smmu_iommu_iova_to_phys,
  803. .domain_has_cap = smmu_iommu_domain_has_cap,
  804. .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
  805. };
  806. /* Should be in the order of enum */
  807. static const char * const smmu_debugfs_mc[] = { "mc", };
  808. static const char * const smmu_debugfs_cache[] = { "tlb", "ptc", };
  809. static ssize_t smmu_debugfs_stats_write(struct file *file,
  810. const char __user *buffer,
  811. size_t count, loff_t *pos)
  812. {
  813. struct smmu_debugfs_info *info;
  814. struct smmu_device *smmu;
  815. struct dentry *dent;
  816. int i;
  817. enum {
  818. _OFF = 0,
  819. _ON,
  820. _RESET,
  821. };
  822. const char * const command[] = {
  823. [_OFF] = "off",
  824. [_ON] = "on",
  825. [_RESET] = "reset",
  826. };
  827. char str[] = "reset";
  828. u32 val;
  829. size_t offs;
  830. count = min_t(size_t, count, sizeof(str));
  831. if (copy_from_user(str, buffer, count))
  832. return -EINVAL;
  833. for (i = 0; i < ARRAY_SIZE(command); i++)
  834. if (strncmp(str, command[i],
  835. strlen(command[i])) == 0)
  836. break;
  837. if (i == ARRAY_SIZE(command))
  838. return -EINVAL;
  839. dent = file->f_dentry;
  840. info = dent->d_inode->i_private;
  841. smmu = info->smmu;
  842. offs = SMMU_CACHE_CONFIG(info->cache);
  843. val = smmu_read(smmu, offs);
  844. switch (i) {
  845. case _OFF:
  846. val &= ~SMMU_CACHE_CONFIG_STATS_ENABLE;
  847. val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
  848. smmu_write(smmu, val, offs);
  849. break;
  850. case _ON:
  851. val |= SMMU_CACHE_CONFIG_STATS_ENABLE;
  852. val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
  853. smmu_write(smmu, val, offs);
  854. break;
  855. case _RESET:
  856. val |= SMMU_CACHE_CONFIG_STATS_TEST;
  857. smmu_write(smmu, val, offs);
  858. val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
  859. smmu_write(smmu, val, offs);
  860. break;
  861. default:
  862. BUG();
  863. break;
  864. }
  865. dev_dbg(smmu->dev, "%s() %08x, %08x @%08x\n", __func__,
  866. val, smmu_read(smmu, offs), offs);
  867. return count;
  868. }
  869. static int smmu_debugfs_stats_show(struct seq_file *s, void *v)
  870. {
  871. struct smmu_debugfs_info *info;
  872. struct smmu_device *smmu;
  873. struct dentry *dent;
  874. int i;
  875. const char * const stats[] = { "hit", "miss", };
  876. dent = d_find_alias(s->private);
  877. info = dent->d_inode->i_private;
  878. smmu = info->smmu;
  879. for (i = 0; i < ARRAY_SIZE(stats); i++) {
  880. u32 val;
  881. size_t offs;
  882. offs = SMMU_STATS_CACHE_COUNT(info->mc, info->cache, i);
  883. val = smmu_read(smmu, offs);
  884. seq_printf(s, "%s:%08x ", stats[i], val);
  885. dev_dbg(smmu->dev, "%s() %s %08x @%08x\n", __func__,
  886. stats[i], val, offs);
  887. }
  888. seq_printf(s, "\n");
  889. dput(dent);
  890. return 0;
  891. }
  892. static int smmu_debugfs_stats_open(struct inode *inode, struct file *file)
  893. {
  894. return single_open(file, smmu_debugfs_stats_show, inode);
  895. }
  896. static const struct file_operations smmu_debugfs_stats_fops = {
  897. .open = smmu_debugfs_stats_open,
  898. .read = seq_read,
  899. .llseek = seq_lseek,
  900. .release = single_release,
  901. .write = smmu_debugfs_stats_write,
  902. };
  903. static void smmu_debugfs_delete(struct smmu_device *smmu)
  904. {
  905. debugfs_remove_recursive(smmu->debugfs_root);
  906. kfree(smmu->debugfs_info);
  907. }
  908. static void smmu_debugfs_create(struct smmu_device *smmu)
  909. {
  910. int i;
  911. size_t bytes;
  912. struct dentry *root;
  913. bytes = ARRAY_SIZE(smmu_debugfs_mc) * ARRAY_SIZE(smmu_debugfs_cache) *
  914. sizeof(*smmu->debugfs_info);
  915. smmu->debugfs_info = kmalloc(bytes, GFP_KERNEL);
  916. if (!smmu->debugfs_info)
  917. return;
  918. root = debugfs_create_dir(dev_name(smmu->dev), NULL);
  919. if (!root)
  920. goto err_out;
  921. smmu->debugfs_root = root;
  922. for (i = 0; i < ARRAY_SIZE(smmu_debugfs_mc); i++) {
  923. int j;
  924. struct dentry *mc;
  925. mc = debugfs_create_dir(smmu_debugfs_mc[i], root);
  926. if (!mc)
  927. goto err_out;
  928. for (j = 0; j < ARRAY_SIZE(smmu_debugfs_cache); j++) {
  929. struct dentry *cache;
  930. struct smmu_debugfs_info *info;
  931. info = smmu->debugfs_info;
  932. info += i * ARRAY_SIZE(smmu_debugfs_mc) + j;
  933. info->smmu = smmu;
  934. info->mc = i;
  935. info->cache = j;
  936. cache = debugfs_create_file(smmu_debugfs_cache[j],
  937. S_IWUGO | S_IRUGO, mc,
  938. (void *)info,
  939. &smmu_debugfs_stats_fops);
  940. if (!cache)
  941. goto err_out;
  942. }
  943. }
  944. return;
  945. err_out:
  946. smmu_debugfs_delete(smmu);
  947. }
  948. static int tegra_smmu_suspend(struct device *dev)
  949. {
  950. struct smmu_device *smmu = dev_get_drvdata(dev);
  951. smmu->translation_enable_0 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_0);
  952. smmu->translation_enable_1 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_1);
  953. smmu->translation_enable_2 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_2);
  954. smmu->asid_security = smmu_read(smmu, SMMU_ASID_SECURITY);
  955. return 0;
  956. }
  957. static int tegra_smmu_resume(struct device *dev)
  958. {
  959. struct smmu_device *smmu = dev_get_drvdata(dev);
  960. unsigned long flags;
  961. int err;
  962. spin_lock_irqsave(&smmu->lock, flags);
  963. err = smmu_setup_regs(smmu);
  964. spin_unlock_irqrestore(&smmu->lock, flags);
  965. return err;
  966. }
  967. static int tegra_smmu_probe(struct platform_device *pdev)
  968. {
  969. struct smmu_device *smmu;
  970. struct device *dev = &pdev->dev;
  971. int i, asids, err = 0;
  972. dma_addr_t uninitialized_var(base);
  973. size_t bytes, uninitialized_var(size);
  974. if (smmu_handle)
  975. return -EIO;
  976. BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
  977. if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
  978. return -ENODEV;
  979. bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
  980. smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
  981. if (!smmu) {
  982. dev_err(dev, "failed to allocate smmu_device\n");
  983. return -ENOMEM;
  984. }
  985. for (i = 0; i < ARRAY_SIZE(smmu->regs); i++) {
  986. struct resource *res;
  987. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  988. if (!res)
  989. return -ENODEV;
  990. smmu->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
  991. if (!smmu->regs[i])
  992. return -EBUSY;
  993. }
  994. err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
  995. if (err)
  996. return -ENODEV;
  997. if (size & SMMU_PAGE_MASK)
  998. return -EINVAL;
  999. size >>= SMMU_PAGE_SHIFT;
  1000. if (!size)
  1001. return -EINVAL;
  1002. smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
  1003. if (!smmu->ahb)
  1004. return -ENODEV;
  1005. smmu->dev = dev;
  1006. smmu->num_as = asids;
  1007. smmu->iovmm_base = base;
  1008. smmu->page_count = size;
  1009. smmu->translation_enable_0 = ~0;
  1010. smmu->translation_enable_1 = ~0;
  1011. smmu->translation_enable_2 = ~0;
  1012. smmu->asid_security = 0;
  1013. for (i = 0; i < smmu->num_as; i++) {
  1014. struct smmu_as *as = &smmu->as[i];
  1015. as->smmu = smmu;
  1016. as->asid = i;
  1017. as->pdir_attr = _PDIR_ATTR;
  1018. as->pde_attr = _PDE_ATTR;
  1019. as->pte_attr = _PTE_ATTR;
  1020. spin_lock_init(&as->lock);
  1021. INIT_LIST_HEAD(&as->client);
  1022. }
  1023. spin_lock_init(&smmu->lock);
  1024. err = smmu_setup_regs(smmu);
  1025. if (err)
  1026. return err;
  1027. platform_set_drvdata(pdev, smmu);
  1028. smmu->avp_vector_page = alloc_page(GFP_KERNEL);
  1029. if (!smmu->avp_vector_page)
  1030. return -ENOMEM;
  1031. smmu_debugfs_create(smmu);
  1032. smmu_handle = smmu;
  1033. return 0;
  1034. }
  1035. static int tegra_smmu_remove(struct platform_device *pdev)
  1036. {
  1037. struct smmu_device *smmu = platform_get_drvdata(pdev);
  1038. int i;
  1039. smmu_debugfs_delete(smmu);
  1040. smmu_write(smmu, SMMU_CONFIG_DISABLE, SMMU_CONFIG);
  1041. for (i = 0; i < smmu->num_as; i++)
  1042. free_pdir(&smmu->as[i]);
  1043. __free_page(smmu->avp_vector_page);
  1044. smmu_handle = NULL;
  1045. return 0;
  1046. }
  1047. const struct dev_pm_ops tegra_smmu_pm_ops = {
  1048. .suspend = tegra_smmu_suspend,
  1049. .resume = tegra_smmu_resume,
  1050. };
  1051. #ifdef CONFIG_OF
  1052. static struct of_device_id tegra_smmu_of_match[] __devinitdata = {
  1053. { .compatible = "nvidia,tegra30-smmu", },
  1054. { },
  1055. };
  1056. MODULE_DEVICE_TABLE(of, tegra_smmu_of_match);
  1057. #endif
  1058. static struct platform_driver tegra_smmu_driver = {
  1059. .probe = tegra_smmu_probe,
  1060. .remove = tegra_smmu_remove,
  1061. .driver = {
  1062. .owner = THIS_MODULE,
  1063. .name = "tegra-smmu",
  1064. .pm = &tegra_smmu_pm_ops,
  1065. .of_match_table = of_match_ptr(tegra_smmu_of_match),
  1066. },
  1067. };
  1068. static int __devinit tegra_smmu_init(void)
  1069. {
  1070. bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
  1071. return platform_driver_register(&tegra_smmu_driver);
  1072. }
  1073. static void __exit tegra_smmu_exit(void)
  1074. {
  1075. platform_driver_unregister(&tegra_smmu_driver);
  1076. }
  1077. subsys_initcall(tegra_smmu_init);
  1078. module_exit(tegra_smmu_exit);
  1079. MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
  1080. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  1081. MODULE_ALIAS("platform:tegra-smmu");
  1082. MODULE_LICENSE("GPL v2");