tegra-smmu.c 27 KB

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