tegra-smmu.c 32 KB

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