tegra-smmu.c 32 KB

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