tegra-smmu.c 32 KB

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