tegra-smmu.c 27 KB

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