tegra-smmu.c 31 KB

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