tegra-smmu.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 <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 *regs[NUM_SMMU_REG_BANKS];
  253. unsigned long iovmm_base; /* remappable base address */
  254. unsigned long page_count; /* total remappable size */
  255. spinlock_t lock;
  256. char *name;
  257. struct device *dev;
  258. struct page *avp_vector_page; /* dummy page shared by all AS's */
  259. /*
  260. * Register image savers for suspend/resume
  261. */
  262. unsigned long translation_enable_0;
  263. unsigned long translation_enable_1;
  264. unsigned long translation_enable_2;
  265. unsigned long asid_security;
  266. struct dentry *debugfs_root;
  267. struct smmu_debugfs_info *debugfs_info;
  268. struct device_node *ahb;
  269. int num_as;
  270. struct smmu_as as[0]; /* Run-time allocated array */
  271. };
  272. static struct smmu_device *smmu_handle; /* unique for a system */
  273. /*
  274. * SMMU register accessors
  275. */
  276. static inline u32 smmu_read(struct smmu_device *smmu, size_t offs)
  277. {
  278. BUG_ON(offs < 0x10);
  279. if (offs < 0x3c)
  280. return readl(smmu->regs[0] + offs - 0x10);
  281. BUG_ON(offs < 0x1f0);
  282. if (offs < 0x200)
  283. return readl(smmu->regs[1] + offs - 0x1f0);
  284. BUG_ON(offs < 0x228);
  285. if (offs < 0x284)
  286. return readl(smmu->regs[2] + offs - 0x228);
  287. BUG();
  288. }
  289. static inline void smmu_write(struct smmu_device *smmu, u32 val, size_t offs)
  290. {
  291. BUG_ON(offs < 0x10);
  292. if (offs < 0x3c) {
  293. writel(val, smmu->regs[0] + offs - 0x10);
  294. return;
  295. }
  296. BUG_ON(offs < 0x1f0);
  297. if (offs < 0x200) {
  298. writel(val, smmu->regs[1] + offs - 0x1f0);
  299. return;
  300. }
  301. BUG_ON(offs < 0x228);
  302. if (offs < 0x284) {
  303. writel(val, smmu->regs[2] + offs - 0x228);
  304. return;
  305. }
  306. BUG();
  307. }
  308. #define VA_PAGE_TO_PA(va, page) \
  309. (page_to_phys(page) + ((unsigned long)(va) & ~PAGE_MASK))
  310. #define FLUSH_CPU_DCACHE(va, page, size) \
  311. do { \
  312. unsigned long _pa_ = VA_PAGE_TO_PA(va, page); \
  313. __cpuc_flush_dcache_area((void *)(va), (size_t)(size)); \
  314. outer_flush_range(_pa_, _pa_+(size_t)(size)); \
  315. } while (0)
  316. /*
  317. * Any interaction between any block on PPSB and a block on APB or AHB
  318. * must have these read-back barriers to ensure the APB/AHB bus
  319. * transaction is complete before initiating activity on the PPSB
  320. * block.
  321. */
  322. #define FLUSH_SMMU_REGS(smmu) smmu_read(smmu, SMMU_CONFIG)
  323. #define smmu_client_hwgrp(c) (u32)((c)->dev->platform_data)
  324. static int __smmu_client_set_hwgrp(struct smmu_client *c,
  325. unsigned long map, int on)
  326. {
  327. int i;
  328. struct smmu_as *as = c->as;
  329. u32 val, offs, mask = SMMU_ASID_ENABLE(as->asid);
  330. struct smmu_device *smmu = as->smmu;
  331. WARN_ON(!on && map);
  332. if (on && !map)
  333. return -EINVAL;
  334. if (!on)
  335. map = smmu_client_hwgrp(c);
  336. for_each_set_bit(i, &map, HWGRP_COUNT) {
  337. offs = HWGRP_ASID_REG(i);
  338. val = smmu_read(smmu, offs);
  339. if (on) {
  340. if (WARN_ON(val & mask))
  341. goto err_hw_busy;
  342. val |= mask;
  343. } else {
  344. WARN_ON((val & mask) == mask);
  345. val &= ~mask;
  346. }
  347. smmu_write(smmu, val, offs);
  348. }
  349. FLUSH_SMMU_REGS(smmu);
  350. c->hwgrp = map;
  351. return 0;
  352. err_hw_busy:
  353. for_each_set_bit(i, &map, HWGRP_COUNT) {
  354. offs = HWGRP_ASID_REG(i);
  355. val = smmu_read(smmu, offs);
  356. val &= ~mask;
  357. smmu_write(smmu, val, offs);
  358. }
  359. return -EBUSY;
  360. }
  361. static int smmu_client_set_hwgrp(struct smmu_client *c, u32 map, int on)
  362. {
  363. u32 val;
  364. unsigned long flags;
  365. struct smmu_as *as = c->as;
  366. struct smmu_device *smmu = as->smmu;
  367. spin_lock_irqsave(&smmu->lock, flags);
  368. val = __smmu_client_set_hwgrp(c, map, on);
  369. spin_unlock_irqrestore(&smmu->lock, flags);
  370. return val;
  371. }
  372. /*
  373. * Flush all TLB entries and all PTC entries
  374. * Caller must lock smmu
  375. */
  376. static void smmu_flush_regs(struct smmu_device *smmu, int enable)
  377. {
  378. u32 val;
  379. smmu_write(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
  380. FLUSH_SMMU_REGS(smmu);
  381. val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
  382. SMMU_TLB_FLUSH_ASID_MATCH_disable;
  383. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  384. if (enable)
  385. smmu_write(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
  386. FLUSH_SMMU_REGS(smmu);
  387. }
  388. static int smmu_setup_regs(struct smmu_device *smmu)
  389. {
  390. int i;
  391. u32 val;
  392. for (i = 0; i < smmu->num_as; i++) {
  393. struct smmu_as *as = &smmu->as[i];
  394. struct smmu_client *c;
  395. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  396. val = as->pdir_page ?
  397. SMMU_MK_PDIR(as->pdir_page, as->pdir_attr) :
  398. SMMU_PTB_DATA_RESET_VAL;
  399. smmu_write(smmu, val, SMMU_PTB_DATA);
  400. list_for_each_entry(c, &as->client, list)
  401. __smmu_client_set_hwgrp(c, c->hwgrp, 1);
  402. }
  403. smmu_write(smmu, smmu->translation_enable_0, SMMU_TRANSLATION_ENABLE_0);
  404. smmu_write(smmu, smmu->translation_enable_1, SMMU_TRANSLATION_ENABLE_1);
  405. smmu_write(smmu, smmu->translation_enable_2, SMMU_TRANSLATION_ENABLE_2);
  406. smmu_write(smmu, smmu->asid_security, SMMU_ASID_SECURITY);
  407. smmu_write(smmu, SMMU_TLB_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_TLB));
  408. smmu_write(smmu, SMMU_PTC_CONFIG_RESET_VAL, SMMU_CACHE_CONFIG(_PTC));
  409. smmu_flush_regs(smmu, 1);
  410. return tegra_ahb_enable_smmu(smmu->ahb);
  411. }
  412. static void flush_ptc_and_tlb(struct smmu_device *smmu,
  413. struct smmu_as *as, dma_addr_t iova,
  414. unsigned long *pte, struct page *page, int is_pde)
  415. {
  416. u32 val;
  417. unsigned long tlb_flush_va = is_pde
  418. ? SMMU_TLB_FLUSH_VA(iova, SECTION)
  419. : SMMU_TLB_FLUSH_VA(iova, GROUP);
  420. val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pte, page);
  421. smmu_write(smmu, val, SMMU_PTC_FLUSH);
  422. FLUSH_SMMU_REGS(smmu);
  423. val = tlb_flush_va |
  424. SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
  425. (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
  426. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  427. FLUSH_SMMU_REGS(smmu);
  428. }
  429. static void free_ptbl(struct smmu_as *as, dma_addr_t iova)
  430. {
  431. unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
  432. unsigned long *pdir = (unsigned long *)page_address(as->pdir_page);
  433. if (pdir[pdn] != _PDE_VACANT(pdn)) {
  434. dev_dbg(as->smmu->dev, "pdn: %lx\n", pdn);
  435. ClearPageReserved(SMMU_EX_PTBL_PAGE(pdir[pdn]));
  436. __free_page(SMMU_EX_PTBL_PAGE(pdir[pdn]));
  437. pdir[pdn] = _PDE_VACANT(pdn);
  438. FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
  439. flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
  440. as->pdir_page, 1);
  441. }
  442. }
  443. static void free_pdir(struct smmu_as *as)
  444. {
  445. unsigned addr;
  446. int count;
  447. struct device *dev = as->smmu->dev;
  448. if (!as->pdir_page)
  449. return;
  450. addr = as->smmu->iovmm_base;
  451. count = as->smmu->page_count;
  452. while (count-- > 0) {
  453. free_ptbl(as, addr);
  454. addr += SMMU_PAGE_SIZE * SMMU_PTBL_COUNT;
  455. }
  456. ClearPageReserved(as->pdir_page);
  457. __free_page(as->pdir_page);
  458. as->pdir_page = NULL;
  459. devm_kfree(dev, as->pte_count);
  460. as->pte_count = NULL;
  461. }
  462. /*
  463. * Maps PTBL for given iova and returns the PTE address
  464. * Caller must unmap the mapped PTBL returned in *ptbl_page_p
  465. */
  466. static unsigned long *locate_pte(struct smmu_as *as,
  467. dma_addr_t iova, bool allocate,
  468. struct page **ptbl_page_p,
  469. unsigned int **count)
  470. {
  471. unsigned long ptn = SMMU_ADDR_TO_PFN(iova);
  472. unsigned long pdn = SMMU_ADDR_TO_PDN(iova);
  473. unsigned long *pdir = page_address(as->pdir_page);
  474. unsigned long *ptbl;
  475. if (pdir[pdn] != _PDE_VACANT(pdn)) {
  476. /* Mapped entry table already exists */
  477. *ptbl_page_p = SMMU_EX_PTBL_PAGE(pdir[pdn]);
  478. ptbl = page_address(*ptbl_page_p);
  479. } else if (!allocate) {
  480. return NULL;
  481. } else {
  482. int pn;
  483. unsigned long addr = SMMU_PDN_TO_ADDR(pdn);
  484. /* Vacant - allocate a new page table */
  485. dev_dbg(as->smmu->dev, "New PTBL pdn: %lx\n", pdn);
  486. *ptbl_page_p = alloc_page(GFP_ATOMIC);
  487. if (!*ptbl_page_p) {
  488. dev_err(as->smmu->dev,
  489. "failed to allocate smmu_device page table\n");
  490. return NULL;
  491. }
  492. SetPageReserved(*ptbl_page_p);
  493. ptbl = (unsigned long *)page_address(*ptbl_page_p);
  494. for (pn = 0; pn < SMMU_PTBL_COUNT;
  495. pn++, addr += SMMU_PAGE_SIZE) {
  496. ptbl[pn] = _PTE_VACANT(addr);
  497. }
  498. FLUSH_CPU_DCACHE(ptbl, *ptbl_page_p, SMMU_PTBL_SIZE);
  499. pdir[pdn] = SMMU_MK_PDE(*ptbl_page_p,
  500. as->pde_attr | _PDE_NEXT);
  501. FLUSH_CPU_DCACHE(&pdir[pdn], as->pdir_page, sizeof pdir[pdn]);
  502. flush_ptc_and_tlb(as->smmu, as, iova, &pdir[pdn],
  503. as->pdir_page, 1);
  504. }
  505. *count = &as->pte_count[pdn];
  506. return &ptbl[ptn % SMMU_PTBL_COUNT];
  507. }
  508. #ifdef CONFIG_SMMU_SIG_DEBUG
  509. static void put_signature(struct smmu_as *as,
  510. dma_addr_t iova, unsigned long pfn)
  511. {
  512. struct page *page;
  513. unsigned long *vaddr;
  514. page = pfn_to_page(pfn);
  515. vaddr = page_address(page);
  516. if (!vaddr)
  517. return;
  518. vaddr[0] = iova;
  519. vaddr[1] = pfn << PAGE_SHIFT;
  520. FLUSH_CPU_DCACHE(vaddr, page, sizeof(vaddr[0]) * 2);
  521. }
  522. #else
  523. static inline void put_signature(struct smmu_as *as,
  524. unsigned long addr, unsigned long pfn)
  525. {
  526. }
  527. #endif
  528. /*
  529. * Caller must not hold as->lock
  530. */
  531. static int alloc_pdir(struct smmu_as *as)
  532. {
  533. unsigned long *pdir, flags;
  534. int pdn, err = 0;
  535. u32 val;
  536. struct smmu_device *smmu = as->smmu;
  537. struct page *page;
  538. unsigned int *cnt;
  539. /*
  540. * do the allocation, then grab as->lock
  541. */
  542. cnt = devm_kzalloc(smmu->dev,
  543. sizeof(cnt[0]) * SMMU_PDIR_COUNT,
  544. GFP_KERNEL);
  545. page = alloc_page(GFP_KERNEL | __GFP_DMA);
  546. spin_lock_irqsave(&as->lock, flags);
  547. if (as->pdir_page) {
  548. /* We raced, free the redundant */
  549. err = -EAGAIN;
  550. goto err_out;
  551. }
  552. if (!page || !cnt) {
  553. dev_err(smmu->dev, "failed to allocate at %s\n", __func__);
  554. err = -ENOMEM;
  555. goto err_out;
  556. }
  557. as->pdir_page = page;
  558. as->pte_count = cnt;
  559. SetPageReserved(as->pdir_page);
  560. pdir = page_address(as->pdir_page);
  561. for (pdn = 0; pdn < SMMU_PDIR_COUNT; pdn++)
  562. pdir[pdn] = _PDE_VACANT(pdn);
  563. FLUSH_CPU_DCACHE(pdir, as->pdir_page, SMMU_PDIR_SIZE);
  564. val = SMMU_PTC_FLUSH_TYPE_ADR | VA_PAGE_TO_PA(pdir, as->pdir_page);
  565. smmu_write(smmu, val, SMMU_PTC_FLUSH);
  566. FLUSH_SMMU_REGS(as->smmu);
  567. val = SMMU_TLB_FLUSH_VA_MATCH_ALL |
  568. SMMU_TLB_FLUSH_ASID_MATCH__ENABLE |
  569. (as->asid << SMMU_TLB_FLUSH_ASID_SHIFT);
  570. smmu_write(smmu, val, SMMU_TLB_FLUSH);
  571. FLUSH_SMMU_REGS(as->smmu);
  572. spin_unlock_irqrestore(&as->lock, flags);
  573. return 0;
  574. err_out:
  575. spin_unlock_irqrestore(&as->lock, flags);
  576. devm_kfree(smmu->dev, cnt);
  577. if (page)
  578. __free_page(page);
  579. return err;
  580. }
  581. static void __smmu_iommu_unmap(struct smmu_as *as, dma_addr_t iova)
  582. {
  583. unsigned long *pte;
  584. struct page *page;
  585. unsigned int *count;
  586. pte = locate_pte(as, iova, false, &page, &count);
  587. if (WARN_ON(!pte))
  588. return;
  589. if (WARN_ON(*pte == _PTE_VACANT(iova)))
  590. return;
  591. *pte = _PTE_VACANT(iova);
  592. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  593. flush_ptc_and_tlb(as->smmu, as, iova, pte, page, 0);
  594. if (!--(*count))
  595. free_ptbl(as, iova);
  596. }
  597. static void __smmu_iommu_map_pfn(struct smmu_as *as, dma_addr_t iova,
  598. unsigned long pfn)
  599. {
  600. struct smmu_device *smmu = as->smmu;
  601. unsigned long *pte;
  602. unsigned int *count;
  603. struct page *page;
  604. pte = locate_pte(as, iova, true, &page, &count);
  605. if (WARN_ON(!pte))
  606. return;
  607. if (*pte == _PTE_VACANT(iova))
  608. (*count)++;
  609. *pte = SMMU_PFN_TO_PTE(pfn, as->pte_attr);
  610. if (unlikely((*pte == _PTE_VACANT(iova))))
  611. (*count)--;
  612. FLUSH_CPU_DCACHE(pte, page, sizeof(*pte));
  613. flush_ptc_and_tlb(smmu, as, iova, pte, page, 0);
  614. put_signature(as, iova, pfn);
  615. }
  616. static int smmu_iommu_map(struct iommu_domain *domain, unsigned long iova,
  617. phys_addr_t pa, size_t bytes, int prot)
  618. {
  619. struct smmu_as *as = domain->priv;
  620. unsigned long pfn = __phys_to_pfn(pa);
  621. unsigned long flags;
  622. dev_dbg(as->smmu->dev, "[%d] %08lx:%08x\n", as->asid, iova, pa);
  623. if (!pfn_valid(pfn))
  624. return -ENOMEM;
  625. spin_lock_irqsave(&as->lock, flags);
  626. __smmu_iommu_map_pfn(as, iova, pfn);
  627. spin_unlock_irqrestore(&as->lock, flags);
  628. return 0;
  629. }
  630. static size_t smmu_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  631. size_t bytes)
  632. {
  633. struct smmu_as *as = domain->priv;
  634. unsigned long flags;
  635. dev_dbg(as->smmu->dev, "[%d] %08lx\n", as->asid, iova);
  636. spin_lock_irqsave(&as->lock, flags);
  637. __smmu_iommu_unmap(as, iova);
  638. spin_unlock_irqrestore(&as->lock, flags);
  639. return SMMU_PAGE_SIZE;
  640. }
  641. static phys_addr_t smmu_iommu_iova_to_phys(struct iommu_domain *domain,
  642. unsigned long iova)
  643. {
  644. struct smmu_as *as = domain->priv;
  645. unsigned long *pte;
  646. unsigned int *count;
  647. struct page *page;
  648. unsigned long pfn;
  649. unsigned long flags;
  650. spin_lock_irqsave(&as->lock, flags);
  651. pte = locate_pte(as, iova, true, &page, &count);
  652. pfn = *pte & SMMU_PFN_MASK;
  653. WARN_ON(!pfn_valid(pfn));
  654. dev_dbg(as->smmu->dev,
  655. "iova:%08lx pfn:%08lx asid:%d\n", iova, pfn, as->asid);
  656. spin_unlock_irqrestore(&as->lock, flags);
  657. return PFN_PHYS(pfn);
  658. }
  659. static int smmu_iommu_domain_has_cap(struct iommu_domain *domain,
  660. unsigned long cap)
  661. {
  662. return 0;
  663. }
  664. static int smmu_iommu_attach_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 *client, *c;
  670. u32 map;
  671. int err;
  672. client = devm_kzalloc(smmu->dev, sizeof(*c), GFP_KERNEL);
  673. if (!client)
  674. return -ENOMEM;
  675. client->dev = dev;
  676. client->as = as;
  677. map = (unsigned long)dev->platform_data;
  678. if (!map)
  679. return -EINVAL;
  680. err = smmu_client_enable_hwgrp(client, map);
  681. if (err)
  682. goto err_hwgrp;
  683. spin_lock(&as->client_lock);
  684. list_for_each_entry(c, &as->client, list) {
  685. if (c->dev == dev) {
  686. dev_err(smmu->dev,
  687. "%s is already attached\n", dev_name(c->dev));
  688. err = -EINVAL;
  689. goto err_client;
  690. }
  691. }
  692. list_add(&client->list, &as->client);
  693. spin_unlock(&as->client_lock);
  694. /*
  695. * Reserve "page zero" for AVP vectors using a common dummy
  696. * page.
  697. */
  698. if (map & HWG_AVPC) {
  699. struct page *page;
  700. page = as->smmu->avp_vector_page;
  701. __smmu_iommu_map_pfn(as, 0, page_to_pfn(page));
  702. pr_info("Reserve \"page zero\" for AVP vectors using a common dummy\n");
  703. }
  704. dev_dbg(smmu->dev, "%s is attached\n", dev_name(dev));
  705. return 0;
  706. err_client:
  707. smmu_client_disable_hwgrp(client);
  708. spin_unlock(&as->client_lock);
  709. err_hwgrp:
  710. devm_kfree(smmu->dev, client);
  711. return err;
  712. }
  713. static void smmu_iommu_detach_dev(struct iommu_domain *domain,
  714. struct device *dev)
  715. {
  716. struct smmu_as *as = domain->priv;
  717. struct smmu_device *smmu = as->smmu;
  718. struct smmu_client *c;
  719. spin_lock(&as->client_lock);
  720. list_for_each_entry(c, &as->client, list) {
  721. if (c->dev == dev) {
  722. smmu_client_disable_hwgrp(c);
  723. list_del(&c->list);
  724. devm_kfree(smmu->dev, c);
  725. c->as = NULL;
  726. dev_dbg(smmu->dev,
  727. "%s is detached\n", dev_name(c->dev));
  728. goto out;
  729. }
  730. }
  731. dev_err(smmu->dev, "Couldn't find %s\n", dev_name(dev));
  732. out:
  733. spin_unlock(&as->client_lock);
  734. }
  735. static int smmu_iommu_domain_init(struct iommu_domain *domain)
  736. {
  737. int i, err = -EAGAIN;
  738. unsigned long flags;
  739. struct smmu_as *as;
  740. struct smmu_device *smmu = smmu_handle;
  741. /* Look for a free AS with lock held */
  742. for (i = 0; i < smmu->num_as; i++) {
  743. as = &smmu->as[i];
  744. if (as->pdir_page)
  745. continue;
  746. err = alloc_pdir(as);
  747. if (!err)
  748. goto found;
  749. if (err != -EAGAIN)
  750. break;
  751. }
  752. if (i == smmu->num_as)
  753. dev_err(smmu->dev, "no free AS\n");
  754. return err;
  755. found:
  756. spin_lock_irqsave(&smmu->lock, flags);
  757. /* Update PDIR register */
  758. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  759. smmu_write(smmu,
  760. SMMU_MK_PDIR(as->pdir_page, as->pdir_attr), SMMU_PTB_DATA);
  761. FLUSH_SMMU_REGS(smmu);
  762. spin_unlock_irqrestore(&smmu->lock, flags);
  763. domain->priv = as;
  764. domain->geometry.aperture_start = smmu->iovmm_base;
  765. domain->geometry.aperture_end = smmu->iovmm_base +
  766. smmu->page_count * SMMU_PAGE_SIZE - 1;
  767. domain->geometry.force_aperture = true;
  768. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  769. return 0;
  770. }
  771. static void smmu_iommu_domain_destroy(struct iommu_domain *domain)
  772. {
  773. struct smmu_as *as = domain->priv;
  774. struct smmu_device *smmu = as->smmu;
  775. unsigned long flags;
  776. spin_lock_irqsave(&as->lock, flags);
  777. if (as->pdir_page) {
  778. spin_lock(&smmu->lock);
  779. smmu_write(smmu, SMMU_PTB_ASID_CUR(as->asid), SMMU_PTB_ASID);
  780. smmu_write(smmu, SMMU_PTB_DATA_RESET_VAL, SMMU_PTB_DATA);
  781. FLUSH_SMMU_REGS(smmu);
  782. spin_unlock(&smmu->lock);
  783. free_pdir(as);
  784. }
  785. if (!list_empty(&as->client)) {
  786. struct smmu_client *c;
  787. list_for_each_entry(c, &as->client, list)
  788. smmu_iommu_detach_dev(domain, c->dev);
  789. }
  790. spin_unlock_irqrestore(&as->lock, flags);
  791. domain->priv = NULL;
  792. dev_dbg(smmu->dev, "smmu_as@%p\n", as);
  793. }
  794. static struct iommu_ops smmu_iommu_ops = {
  795. .domain_init = smmu_iommu_domain_init,
  796. .domain_destroy = smmu_iommu_domain_destroy,
  797. .attach_dev = smmu_iommu_attach_dev,
  798. .detach_dev = smmu_iommu_detach_dev,
  799. .map = smmu_iommu_map,
  800. .unmap = smmu_iommu_unmap,
  801. .iova_to_phys = smmu_iommu_iova_to_phys,
  802. .domain_has_cap = smmu_iommu_domain_has_cap,
  803. .pgsize_bitmap = SMMU_IOMMU_PGSIZES,
  804. };
  805. /* Should be in the order of enum */
  806. static const char * const smmu_debugfs_mc[] = { "mc", };
  807. static const char * const smmu_debugfs_cache[] = { "tlb", "ptc", };
  808. static ssize_t smmu_debugfs_stats_write(struct file *file,
  809. const char __user *buffer,
  810. size_t count, loff_t *pos)
  811. {
  812. struct smmu_debugfs_info *info;
  813. struct smmu_device *smmu;
  814. struct dentry *dent;
  815. int i;
  816. enum {
  817. _OFF = 0,
  818. _ON,
  819. _RESET,
  820. };
  821. const char * const command[] = {
  822. [_OFF] = "off",
  823. [_ON] = "on",
  824. [_RESET] = "reset",
  825. };
  826. char str[] = "reset";
  827. u32 val;
  828. size_t offs;
  829. count = min_t(size_t, count, sizeof(str));
  830. if (copy_from_user(str, buffer, count))
  831. return -EINVAL;
  832. for (i = 0; i < ARRAY_SIZE(command); i++)
  833. if (strncmp(str, command[i],
  834. strlen(command[i])) == 0)
  835. break;
  836. if (i == ARRAY_SIZE(command))
  837. return -EINVAL;
  838. dent = file->f_dentry;
  839. info = dent->d_inode->i_private;
  840. smmu = info->smmu;
  841. offs = SMMU_CACHE_CONFIG(info->cache);
  842. val = smmu_read(smmu, offs);
  843. switch (i) {
  844. case _OFF:
  845. val &= ~SMMU_CACHE_CONFIG_STATS_ENABLE;
  846. val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
  847. smmu_write(smmu, val, offs);
  848. break;
  849. case _ON:
  850. val |= SMMU_CACHE_CONFIG_STATS_ENABLE;
  851. val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
  852. smmu_write(smmu, val, offs);
  853. break;
  854. case _RESET:
  855. val |= SMMU_CACHE_CONFIG_STATS_TEST;
  856. smmu_write(smmu, val, offs);
  857. val &= ~SMMU_CACHE_CONFIG_STATS_TEST;
  858. smmu_write(smmu, val, offs);
  859. break;
  860. default:
  861. BUG();
  862. break;
  863. }
  864. dev_dbg(smmu->dev, "%s() %08x, %08x @%08x\n", __func__,
  865. val, smmu_read(smmu, offs), offs);
  866. return count;
  867. }
  868. static int smmu_debugfs_stats_show(struct seq_file *s, void *v)
  869. {
  870. struct smmu_debugfs_info *info;
  871. struct smmu_device *smmu;
  872. struct dentry *dent;
  873. int i;
  874. const char * const stats[] = { "hit", "miss", };
  875. dent = d_find_alias(s->private);
  876. info = dent->d_inode->i_private;
  877. smmu = info->smmu;
  878. for (i = 0; i < ARRAY_SIZE(stats); i++) {
  879. u32 val;
  880. size_t offs;
  881. offs = SMMU_STATS_CACHE_COUNT(info->mc, info->cache, i);
  882. val = smmu_read(smmu, offs);
  883. seq_printf(s, "%s:%08x ", stats[i], val);
  884. dev_dbg(smmu->dev, "%s() %s %08x @%08x\n", __func__,
  885. stats[i], val, offs);
  886. }
  887. seq_printf(s, "\n");
  888. dput(dent);
  889. return 0;
  890. }
  891. static int smmu_debugfs_stats_open(struct inode *inode, struct file *file)
  892. {
  893. return single_open(file, smmu_debugfs_stats_show, inode);
  894. }
  895. static const struct file_operations smmu_debugfs_stats_fops = {
  896. .open = smmu_debugfs_stats_open,
  897. .read = seq_read,
  898. .llseek = seq_lseek,
  899. .release = single_release,
  900. .write = smmu_debugfs_stats_write,
  901. };
  902. static void smmu_debugfs_delete(struct smmu_device *smmu)
  903. {
  904. debugfs_remove_recursive(smmu->debugfs_root);
  905. kfree(smmu->debugfs_info);
  906. }
  907. static void smmu_debugfs_create(struct smmu_device *smmu)
  908. {
  909. int i;
  910. size_t bytes;
  911. struct dentry *root;
  912. bytes = ARRAY_SIZE(smmu_debugfs_mc) * ARRAY_SIZE(smmu_debugfs_cache) *
  913. sizeof(*smmu->debugfs_info);
  914. smmu->debugfs_info = kmalloc(bytes, GFP_KERNEL);
  915. if (!smmu->debugfs_info)
  916. return;
  917. root = debugfs_create_dir(dev_name(smmu->dev), NULL);
  918. if (!root)
  919. goto err_out;
  920. smmu->debugfs_root = root;
  921. for (i = 0; i < ARRAY_SIZE(smmu_debugfs_mc); i++) {
  922. int j;
  923. struct dentry *mc;
  924. mc = debugfs_create_dir(smmu_debugfs_mc[i], root);
  925. if (!mc)
  926. goto err_out;
  927. for (j = 0; j < ARRAY_SIZE(smmu_debugfs_cache); j++) {
  928. struct dentry *cache;
  929. struct smmu_debugfs_info *info;
  930. info = smmu->debugfs_info;
  931. info += i * ARRAY_SIZE(smmu_debugfs_mc) + j;
  932. info->smmu = smmu;
  933. info->mc = i;
  934. info->cache = j;
  935. cache = debugfs_create_file(smmu_debugfs_cache[j],
  936. S_IWUGO | S_IRUGO, mc,
  937. (void *)info,
  938. &smmu_debugfs_stats_fops);
  939. if (!cache)
  940. goto err_out;
  941. }
  942. }
  943. return;
  944. err_out:
  945. smmu_debugfs_delete(smmu);
  946. }
  947. static int tegra_smmu_suspend(struct device *dev)
  948. {
  949. struct smmu_device *smmu = dev_get_drvdata(dev);
  950. smmu->translation_enable_0 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_0);
  951. smmu->translation_enable_1 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_1);
  952. smmu->translation_enable_2 = smmu_read(smmu, SMMU_TRANSLATION_ENABLE_2);
  953. smmu->asid_security = smmu_read(smmu, SMMU_ASID_SECURITY);
  954. return 0;
  955. }
  956. static int tegra_smmu_resume(struct device *dev)
  957. {
  958. struct smmu_device *smmu = dev_get_drvdata(dev);
  959. unsigned long flags;
  960. int err;
  961. spin_lock_irqsave(&smmu->lock, flags);
  962. err = smmu_setup_regs(smmu);
  963. spin_unlock_irqrestore(&smmu->lock, flags);
  964. return err;
  965. }
  966. static int tegra_smmu_probe(struct platform_device *pdev)
  967. {
  968. struct smmu_device *smmu;
  969. struct device *dev = &pdev->dev;
  970. int i, asids, err = 0;
  971. dma_addr_t uninitialized_var(base);
  972. size_t bytes, uninitialized_var(size);
  973. if (smmu_handle)
  974. return -EIO;
  975. BUILD_BUG_ON(PAGE_SHIFT != SMMU_PAGE_SHIFT);
  976. if (of_property_read_u32(dev->of_node, "nvidia,#asids", &asids))
  977. return -ENODEV;
  978. bytes = sizeof(*smmu) + asids * sizeof(*smmu->as);
  979. smmu = devm_kzalloc(dev, bytes, GFP_KERNEL);
  980. if (!smmu) {
  981. dev_err(dev, "failed to allocate smmu_device\n");
  982. return -ENOMEM;
  983. }
  984. for (i = 0; i < ARRAY_SIZE(smmu->regs); i++) {
  985. struct resource *res;
  986. res = platform_get_resource(pdev, IORESOURCE_MEM, i);
  987. if (!res)
  988. return -ENODEV;
  989. smmu->regs[i] = devm_request_and_ioremap(&pdev->dev, res);
  990. if (!smmu->regs[i])
  991. return -EBUSY;
  992. }
  993. err = of_get_dma_window(dev->of_node, NULL, 0, NULL, &base, &size);
  994. if (err)
  995. return -ENODEV;
  996. if (size & SMMU_PAGE_MASK)
  997. return -EINVAL;
  998. size >>= SMMU_PAGE_SHIFT;
  999. if (!size)
  1000. return -EINVAL;
  1001. smmu->ahb = of_parse_phandle(dev->of_node, "nvidia,ahb", 0);
  1002. if (!smmu->ahb)
  1003. return -ENODEV;
  1004. smmu->dev = dev;
  1005. smmu->num_as = asids;
  1006. smmu->iovmm_base = base;
  1007. smmu->page_count = size;
  1008. smmu->translation_enable_0 = ~0;
  1009. smmu->translation_enable_1 = ~0;
  1010. smmu->translation_enable_2 = ~0;
  1011. smmu->asid_security = 0;
  1012. for (i = 0; i < smmu->num_as; i++) {
  1013. struct smmu_as *as = &smmu->as[i];
  1014. as->smmu = smmu;
  1015. as->asid = i;
  1016. as->pdir_attr = _PDIR_ATTR;
  1017. as->pde_attr = _PDE_ATTR;
  1018. as->pte_attr = _PTE_ATTR;
  1019. spin_lock_init(&as->lock);
  1020. INIT_LIST_HEAD(&as->client);
  1021. }
  1022. spin_lock_init(&smmu->lock);
  1023. err = smmu_setup_regs(smmu);
  1024. if (err)
  1025. return err;
  1026. platform_set_drvdata(pdev, smmu);
  1027. smmu->avp_vector_page = alloc_page(GFP_KERNEL);
  1028. if (!smmu->avp_vector_page)
  1029. return -ENOMEM;
  1030. smmu_debugfs_create(smmu);
  1031. smmu_handle = smmu;
  1032. bus_set_iommu(&platform_bus_type, &smmu_iommu_ops);
  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[] = {
  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 tegra_smmu_init(void)
  1069. {
  1070. return platform_driver_register(&tegra_smmu_driver);
  1071. }
  1072. static void __exit tegra_smmu_exit(void)
  1073. {
  1074. platform_driver_unregister(&tegra_smmu_driver);
  1075. }
  1076. subsys_initcall(tegra_smmu_init);
  1077. module_exit(tegra_smmu_exit);
  1078. MODULE_DESCRIPTION("IOMMU API for SMMU in Tegra30");
  1079. MODULE_AUTHOR("Hiroshi DOYU <hdoyu@nvidia.com>");
  1080. MODULE_ALIAS("platform:tegra-smmu");
  1081. MODULE_LICENSE("GPL v2");