tegra-smmu.c 32 KB

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