tegra-smmu.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/err.h>
  11. #include <linux/iommu.h>
  12. #include <linux/kernel.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <soc/tegra/ahb.h>
  18. #include <soc/tegra/mc.h>
  19. struct tegra_smmu {
  20. void __iomem *regs;
  21. struct device *dev;
  22. struct tegra_mc *mc;
  23. const struct tegra_smmu_soc *soc;
  24. unsigned long pfn_mask;
  25. unsigned long *asids;
  26. struct mutex lock;
  27. struct list_head list;
  28. struct dentry *debugfs;
  29. };
  30. struct tegra_smmu_as {
  31. struct iommu_domain domain;
  32. struct tegra_smmu *smmu;
  33. unsigned int use_count;
  34. struct page *count;
  35. struct page *pd;
  36. unsigned id;
  37. u32 attr;
  38. };
  39. static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
  40. {
  41. return container_of(dom, struct tegra_smmu_as, domain);
  42. }
  43. static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
  44. unsigned long offset)
  45. {
  46. writel(value, smmu->regs + offset);
  47. }
  48. static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
  49. {
  50. return readl(smmu->regs + offset);
  51. }
  52. #define SMMU_CONFIG 0x010
  53. #define SMMU_CONFIG_ENABLE (1 << 0)
  54. #define SMMU_TLB_CONFIG 0x14
  55. #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
  56. #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
  57. #define SMMU_TLB_CONFIG_ACTIVE_LINES(x) ((x) & 0x3f)
  58. #define SMMU_PTC_CONFIG 0x18
  59. #define SMMU_PTC_CONFIG_ENABLE (1 << 29)
  60. #define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
  61. #define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
  62. #define SMMU_PTB_ASID 0x01c
  63. #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
  64. #define SMMU_PTB_DATA 0x020
  65. #define SMMU_PTB_DATA_VALUE(page, attr) (page_to_phys(page) >> 12 | (attr))
  66. #define SMMU_MK_PDE(page, attr) (page_to_phys(page) >> SMMU_PTE_SHIFT | (attr))
  67. #define SMMU_TLB_FLUSH 0x030
  68. #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
  69. #define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
  70. #define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
  71. #define SMMU_TLB_FLUSH_ASID(x) (((x) & 0x7f) << 24)
  72. #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
  73. SMMU_TLB_FLUSH_VA_MATCH_SECTION)
  74. #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
  75. SMMU_TLB_FLUSH_VA_MATCH_GROUP)
  76. #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
  77. #define SMMU_PTC_FLUSH 0x034
  78. #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
  79. #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
  80. #define SMMU_PTC_FLUSH_HI 0x9b8
  81. #define SMMU_PTC_FLUSH_HI_MASK 0x3
  82. /* per-SWGROUP SMMU_*_ASID register */
  83. #define SMMU_ASID_ENABLE (1 << 31)
  84. #define SMMU_ASID_MASK 0x7f
  85. #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
  86. /* page table definitions */
  87. #define SMMU_NUM_PDE 1024
  88. #define SMMU_NUM_PTE 1024
  89. #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
  90. #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
  91. #define SMMU_PDE_SHIFT 22
  92. #define SMMU_PTE_SHIFT 12
  93. #define SMMU_PD_READABLE (1 << 31)
  94. #define SMMU_PD_WRITABLE (1 << 30)
  95. #define SMMU_PD_NONSECURE (1 << 29)
  96. #define SMMU_PDE_READABLE (1 << 31)
  97. #define SMMU_PDE_WRITABLE (1 << 30)
  98. #define SMMU_PDE_NONSECURE (1 << 29)
  99. #define SMMU_PDE_NEXT (1 << 28)
  100. #define SMMU_PTE_READABLE (1 << 31)
  101. #define SMMU_PTE_WRITABLE (1 << 30)
  102. #define SMMU_PTE_NONSECURE (1 << 29)
  103. #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
  104. SMMU_PDE_NONSECURE)
  105. #define SMMU_PTE_ATTR (SMMU_PTE_READABLE | SMMU_PTE_WRITABLE | \
  106. SMMU_PTE_NONSECURE)
  107. static inline void smmu_flush_ptc(struct tegra_smmu *smmu, struct page *page,
  108. unsigned long offset)
  109. {
  110. phys_addr_t phys = page ? page_to_phys(page) : 0;
  111. u32 value;
  112. if (page) {
  113. offset &= ~(smmu->mc->soc->atom_size - 1);
  114. if (smmu->mc->soc->num_address_bits > 32) {
  115. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  116. value = (phys >> 32) & SMMU_PTC_FLUSH_HI_MASK;
  117. #else
  118. value = 0;
  119. #endif
  120. smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
  121. }
  122. value = (phys + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
  123. } else {
  124. value = SMMU_PTC_FLUSH_TYPE_ALL;
  125. }
  126. smmu_writel(smmu, value, SMMU_PTC_FLUSH);
  127. }
  128. static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
  129. {
  130. smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
  131. }
  132. static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
  133. unsigned long asid)
  134. {
  135. u32 value;
  136. value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
  137. SMMU_TLB_FLUSH_VA_MATCH_ALL;
  138. smmu_writel(smmu, value, SMMU_TLB_FLUSH);
  139. }
  140. static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
  141. unsigned long asid,
  142. unsigned long iova)
  143. {
  144. u32 value;
  145. value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
  146. SMMU_TLB_FLUSH_VA_SECTION(iova);
  147. smmu_writel(smmu, value, SMMU_TLB_FLUSH);
  148. }
  149. static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
  150. unsigned long asid,
  151. unsigned long iova)
  152. {
  153. u32 value;
  154. value = SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_ASID(asid) |
  155. SMMU_TLB_FLUSH_VA_GROUP(iova);
  156. smmu_writel(smmu, value, SMMU_TLB_FLUSH);
  157. }
  158. static inline void smmu_flush(struct tegra_smmu *smmu)
  159. {
  160. smmu_readl(smmu, SMMU_CONFIG);
  161. }
  162. static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
  163. {
  164. unsigned long id;
  165. mutex_lock(&smmu->lock);
  166. id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
  167. if (id >= smmu->soc->num_asids) {
  168. mutex_unlock(&smmu->lock);
  169. return -ENOSPC;
  170. }
  171. set_bit(id, smmu->asids);
  172. *idp = id;
  173. mutex_unlock(&smmu->lock);
  174. return 0;
  175. }
  176. static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
  177. {
  178. mutex_lock(&smmu->lock);
  179. clear_bit(id, smmu->asids);
  180. mutex_unlock(&smmu->lock);
  181. }
  182. static bool tegra_smmu_capable(enum iommu_cap cap)
  183. {
  184. return false;
  185. }
  186. static struct iommu_domain *tegra_smmu_domain_alloc(unsigned type)
  187. {
  188. struct tegra_smmu_as *as;
  189. unsigned int i;
  190. uint32_t *pd;
  191. if (type != IOMMU_DOMAIN_UNMANAGED)
  192. return NULL;
  193. as = kzalloc(sizeof(*as), GFP_KERNEL);
  194. if (!as)
  195. return NULL;
  196. as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
  197. as->pd = alloc_page(GFP_KERNEL | __GFP_DMA);
  198. if (!as->pd) {
  199. kfree(as);
  200. return NULL;
  201. }
  202. as->count = alloc_page(GFP_KERNEL);
  203. if (!as->count) {
  204. __free_page(as->pd);
  205. kfree(as);
  206. return NULL;
  207. }
  208. /* clear PDEs */
  209. pd = page_address(as->pd);
  210. SetPageReserved(as->pd);
  211. for (i = 0; i < SMMU_NUM_PDE; i++)
  212. pd[i] = 0;
  213. /* clear PDE usage counters */
  214. pd = page_address(as->count);
  215. SetPageReserved(as->count);
  216. for (i = 0; i < SMMU_NUM_PDE; i++)
  217. pd[i] = 0;
  218. /* setup aperture */
  219. as->domain.geometry.aperture_start = 0;
  220. as->domain.geometry.aperture_end = 0xffffffff;
  221. as->domain.geometry.force_aperture = true;
  222. return &as->domain;
  223. }
  224. static void tegra_smmu_domain_free(struct iommu_domain *domain)
  225. {
  226. struct tegra_smmu_as *as = to_smmu_as(domain);
  227. /* TODO: free page directory and page tables */
  228. ClearPageReserved(as->pd);
  229. kfree(as);
  230. }
  231. static const struct tegra_smmu_swgroup *
  232. tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
  233. {
  234. const struct tegra_smmu_swgroup *group = NULL;
  235. unsigned int i;
  236. for (i = 0; i < smmu->soc->num_swgroups; i++) {
  237. if (smmu->soc->swgroups[i].swgroup == swgroup) {
  238. group = &smmu->soc->swgroups[i];
  239. break;
  240. }
  241. }
  242. return group;
  243. }
  244. static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
  245. unsigned int asid)
  246. {
  247. const struct tegra_smmu_swgroup *group;
  248. unsigned int i;
  249. u32 value;
  250. for (i = 0; i < smmu->soc->num_clients; i++) {
  251. const struct tegra_mc_client *client = &smmu->soc->clients[i];
  252. if (client->swgroup != swgroup)
  253. continue;
  254. value = smmu_readl(smmu, client->smmu.reg);
  255. value |= BIT(client->smmu.bit);
  256. smmu_writel(smmu, value, client->smmu.reg);
  257. }
  258. group = tegra_smmu_find_swgroup(smmu, swgroup);
  259. if (group) {
  260. value = smmu_readl(smmu, group->reg);
  261. value &= ~SMMU_ASID_MASK;
  262. value |= SMMU_ASID_VALUE(asid);
  263. value |= SMMU_ASID_ENABLE;
  264. smmu_writel(smmu, value, group->reg);
  265. }
  266. }
  267. static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
  268. unsigned int asid)
  269. {
  270. const struct tegra_smmu_swgroup *group;
  271. unsigned int i;
  272. u32 value;
  273. group = tegra_smmu_find_swgroup(smmu, swgroup);
  274. if (group) {
  275. value = smmu_readl(smmu, group->reg);
  276. value &= ~SMMU_ASID_MASK;
  277. value |= SMMU_ASID_VALUE(asid);
  278. value &= ~SMMU_ASID_ENABLE;
  279. smmu_writel(smmu, value, group->reg);
  280. }
  281. for (i = 0; i < smmu->soc->num_clients; i++) {
  282. const struct tegra_mc_client *client = &smmu->soc->clients[i];
  283. if (client->swgroup != swgroup)
  284. continue;
  285. value = smmu_readl(smmu, client->smmu.reg);
  286. value &= ~BIT(client->smmu.bit);
  287. smmu_writel(smmu, value, client->smmu.reg);
  288. }
  289. }
  290. static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
  291. struct tegra_smmu_as *as)
  292. {
  293. u32 value;
  294. int err;
  295. if (as->use_count > 0) {
  296. as->use_count++;
  297. return 0;
  298. }
  299. err = tegra_smmu_alloc_asid(smmu, &as->id);
  300. if (err < 0)
  301. return err;
  302. smmu->soc->ops->flush_dcache(as->pd, 0, SMMU_SIZE_PD);
  303. smmu_flush_ptc(smmu, as->pd, 0);
  304. smmu_flush_tlb_asid(smmu, as->id);
  305. smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
  306. value = SMMU_PTB_DATA_VALUE(as->pd, as->attr);
  307. smmu_writel(smmu, value, SMMU_PTB_DATA);
  308. smmu_flush(smmu);
  309. as->smmu = smmu;
  310. as->use_count++;
  311. return 0;
  312. }
  313. static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
  314. struct tegra_smmu_as *as)
  315. {
  316. if (--as->use_count > 0)
  317. return;
  318. tegra_smmu_free_asid(smmu, as->id);
  319. as->smmu = NULL;
  320. }
  321. static int tegra_smmu_attach_dev(struct iommu_domain *domain,
  322. struct device *dev)
  323. {
  324. struct tegra_smmu *smmu = dev->archdata.iommu;
  325. struct tegra_smmu_as *as = to_smmu_as(domain);
  326. struct device_node *np = dev->of_node;
  327. struct of_phandle_args args;
  328. unsigned int index = 0;
  329. int err = 0;
  330. while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
  331. &args)) {
  332. unsigned int swgroup = args.args[0];
  333. if (args.np != smmu->dev->of_node) {
  334. of_node_put(args.np);
  335. continue;
  336. }
  337. of_node_put(args.np);
  338. err = tegra_smmu_as_prepare(smmu, as);
  339. if (err < 0)
  340. return err;
  341. tegra_smmu_enable(smmu, swgroup, as->id);
  342. index++;
  343. }
  344. if (index == 0)
  345. return -ENODEV;
  346. return 0;
  347. }
  348. static void tegra_smmu_detach_dev(struct iommu_domain *domain, struct device *dev)
  349. {
  350. struct tegra_smmu_as *as = to_smmu_as(domain);
  351. struct device_node *np = dev->of_node;
  352. struct tegra_smmu *smmu = as->smmu;
  353. struct of_phandle_args args;
  354. unsigned int index = 0;
  355. while (!of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
  356. &args)) {
  357. unsigned int swgroup = args.args[0];
  358. if (args.np != smmu->dev->of_node) {
  359. of_node_put(args.np);
  360. continue;
  361. }
  362. of_node_put(args.np);
  363. tegra_smmu_disable(smmu, swgroup, as->id);
  364. tegra_smmu_as_unprepare(smmu, as);
  365. index++;
  366. }
  367. }
  368. static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
  369. struct page **pagep)
  370. {
  371. u32 *pd = page_address(as->pd), *pt, *count;
  372. u32 pde = (iova >> SMMU_PDE_SHIFT) & 0x3ff;
  373. u32 pte = (iova >> SMMU_PTE_SHIFT) & 0x3ff;
  374. struct tegra_smmu *smmu = as->smmu;
  375. struct page *page;
  376. unsigned int i;
  377. if (pd[pde] == 0) {
  378. page = alloc_page(GFP_KERNEL | __GFP_DMA);
  379. if (!page)
  380. return NULL;
  381. pt = page_address(page);
  382. SetPageReserved(page);
  383. for (i = 0; i < SMMU_NUM_PTE; i++)
  384. pt[i] = 0;
  385. smmu->soc->ops->flush_dcache(page, 0, SMMU_SIZE_PT);
  386. pd[pde] = SMMU_MK_PDE(page, SMMU_PDE_ATTR | SMMU_PDE_NEXT);
  387. smmu->soc->ops->flush_dcache(as->pd, pde << 2, 4);
  388. smmu_flush_ptc(smmu, as->pd, pde << 2);
  389. smmu_flush_tlb_section(smmu, as->id, iova);
  390. smmu_flush(smmu);
  391. } else {
  392. page = pfn_to_page(pd[pde] & smmu->pfn_mask);
  393. pt = page_address(page);
  394. }
  395. *pagep = page;
  396. /* Keep track of entries in this page table. */
  397. count = page_address(as->count);
  398. if (pt[pte] == 0)
  399. count[pde]++;
  400. return &pt[pte];
  401. }
  402. static void as_put_pte(struct tegra_smmu_as *as, dma_addr_t iova)
  403. {
  404. u32 pde = (iova >> SMMU_PDE_SHIFT) & 0x3ff;
  405. u32 pte = (iova >> SMMU_PTE_SHIFT) & 0x3ff;
  406. u32 *count = page_address(as->count);
  407. u32 *pd = page_address(as->pd), *pt;
  408. struct page *page;
  409. page = pfn_to_page(pd[pde] & as->smmu->pfn_mask);
  410. pt = page_address(page);
  411. /*
  412. * When no entries in this page table are used anymore, return the
  413. * memory page to the system.
  414. */
  415. if (pt[pte] != 0) {
  416. if (--count[pde] == 0) {
  417. ClearPageReserved(page);
  418. __free_page(page);
  419. pd[pde] = 0;
  420. }
  421. pt[pte] = 0;
  422. }
  423. }
  424. static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
  425. phys_addr_t paddr, size_t size, int prot)
  426. {
  427. struct tegra_smmu_as *as = to_smmu_as(domain);
  428. struct tegra_smmu *smmu = as->smmu;
  429. unsigned long offset;
  430. struct page *page;
  431. u32 *pte;
  432. pte = as_get_pte(as, iova, &page);
  433. if (!pte)
  434. return -ENOMEM;
  435. *pte = __phys_to_pfn(paddr) | SMMU_PTE_ATTR;
  436. offset = offset_in_page(pte);
  437. smmu->soc->ops->flush_dcache(page, offset, 4);
  438. smmu_flush_ptc(smmu, page, offset);
  439. smmu_flush_tlb_group(smmu, as->id, iova);
  440. smmu_flush(smmu);
  441. return 0;
  442. }
  443. static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  444. size_t size)
  445. {
  446. struct tegra_smmu_as *as = to_smmu_as(domain);
  447. struct tegra_smmu *smmu = as->smmu;
  448. unsigned long offset;
  449. struct page *page;
  450. u32 *pte;
  451. pte = as_get_pte(as, iova, &page);
  452. if (!pte)
  453. return 0;
  454. offset = offset_in_page(pte);
  455. as_put_pte(as, iova);
  456. smmu->soc->ops->flush_dcache(page, offset, 4);
  457. smmu_flush_ptc(smmu, page, offset);
  458. smmu_flush_tlb_group(smmu, as->id, iova);
  459. smmu_flush(smmu);
  460. return size;
  461. }
  462. static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
  463. dma_addr_t iova)
  464. {
  465. struct tegra_smmu_as *as = to_smmu_as(domain);
  466. struct page *page;
  467. unsigned long pfn;
  468. u32 *pte;
  469. pte = as_get_pte(as, iova, &page);
  470. pfn = *pte & as->smmu->pfn_mask;
  471. return PFN_PHYS(pfn);
  472. }
  473. static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
  474. {
  475. struct platform_device *pdev;
  476. struct tegra_mc *mc;
  477. pdev = of_find_device_by_node(np);
  478. if (!pdev)
  479. return NULL;
  480. mc = platform_get_drvdata(pdev);
  481. if (!mc)
  482. return NULL;
  483. return mc->smmu;
  484. }
  485. static int tegra_smmu_add_device(struct device *dev)
  486. {
  487. struct device_node *np = dev->of_node;
  488. struct of_phandle_args args;
  489. unsigned int index = 0;
  490. while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
  491. &args) == 0) {
  492. struct tegra_smmu *smmu;
  493. smmu = tegra_smmu_find(args.np);
  494. if (smmu) {
  495. /*
  496. * Only a single IOMMU master interface is currently
  497. * supported by the Linux kernel, so abort after the
  498. * first match.
  499. */
  500. dev->archdata.iommu = smmu;
  501. break;
  502. }
  503. index++;
  504. }
  505. return 0;
  506. }
  507. static void tegra_smmu_remove_device(struct device *dev)
  508. {
  509. dev->archdata.iommu = NULL;
  510. }
  511. static const struct iommu_ops tegra_smmu_ops = {
  512. .capable = tegra_smmu_capable,
  513. .domain_alloc = tegra_smmu_domain_alloc,
  514. .domain_free = tegra_smmu_domain_free,
  515. .attach_dev = tegra_smmu_attach_dev,
  516. .detach_dev = tegra_smmu_detach_dev,
  517. .add_device = tegra_smmu_add_device,
  518. .remove_device = tegra_smmu_remove_device,
  519. .map = tegra_smmu_map,
  520. .unmap = tegra_smmu_unmap,
  521. .map_sg = default_iommu_map_sg,
  522. .iova_to_phys = tegra_smmu_iova_to_phys,
  523. .pgsize_bitmap = SZ_4K,
  524. };
  525. static void tegra_smmu_ahb_enable(void)
  526. {
  527. static const struct of_device_id ahb_match[] = {
  528. { .compatible = "nvidia,tegra30-ahb", },
  529. { }
  530. };
  531. struct device_node *ahb;
  532. ahb = of_find_matching_node(NULL, ahb_match);
  533. if (ahb) {
  534. tegra_ahb_enable_smmu(ahb);
  535. of_node_put(ahb);
  536. }
  537. }
  538. static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
  539. {
  540. struct tegra_smmu *smmu = s->private;
  541. unsigned int i;
  542. u32 value;
  543. seq_printf(s, "swgroup enabled ASID\n");
  544. seq_printf(s, "------------------------\n");
  545. for (i = 0; i < smmu->soc->num_swgroups; i++) {
  546. const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
  547. const char *status;
  548. unsigned int asid;
  549. value = smmu_readl(smmu, group->reg);
  550. if (value & SMMU_ASID_ENABLE)
  551. status = "yes";
  552. else
  553. status = "no";
  554. asid = value & SMMU_ASID_MASK;
  555. seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
  556. asid);
  557. }
  558. return 0;
  559. }
  560. static int tegra_smmu_swgroups_open(struct inode *inode, struct file *file)
  561. {
  562. return single_open(file, tegra_smmu_swgroups_show, inode->i_private);
  563. }
  564. static const struct file_operations tegra_smmu_swgroups_fops = {
  565. .open = tegra_smmu_swgroups_open,
  566. .read = seq_read,
  567. .llseek = seq_lseek,
  568. .release = single_release,
  569. };
  570. static int tegra_smmu_clients_show(struct seq_file *s, void *data)
  571. {
  572. struct tegra_smmu *smmu = s->private;
  573. unsigned int i;
  574. u32 value;
  575. seq_printf(s, "client enabled\n");
  576. seq_printf(s, "--------------------\n");
  577. for (i = 0; i < smmu->soc->num_clients; i++) {
  578. const struct tegra_mc_client *client = &smmu->soc->clients[i];
  579. const char *status;
  580. value = smmu_readl(smmu, client->smmu.reg);
  581. if (value & BIT(client->smmu.bit))
  582. status = "yes";
  583. else
  584. status = "no";
  585. seq_printf(s, "%-12s %s\n", client->name, status);
  586. }
  587. return 0;
  588. }
  589. static int tegra_smmu_clients_open(struct inode *inode, struct file *file)
  590. {
  591. return single_open(file, tegra_smmu_clients_show, inode->i_private);
  592. }
  593. static const struct file_operations tegra_smmu_clients_fops = {
  594. .open = tegra_smmu_clients_open,
  595. .read = seq_read,
  596. .llseek = seq_lseek,
  597. .release = single_release,
  598. };
  599. static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
  600. {
  601. smmu->debugfs = debugfs_create_dir("smmu", NULL);
  602. if (!smmu->debugfs)
  603. return;
  604. debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
  605. &tegra_smmu_swgroups_fops);
  606. debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
  607. &tegra_smmu_clients_fops);
  608. }
  609. static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
  610. {
  611. debugfs_remove_recursive(smmu->debugfs);
  612. }
  613. struct tegra_smmu *tegra_smmu_probe(struct device *dev,
  614. const struct tegra_smmu_soc *soc,
  615. struct tegra_mc *mc)
  616. {
  617. struct tegra_smmu *smmu;
  618. size_t size;
  619. u32 value;
  620. int err;
  621. /* This can happen on Tegra20 which doesn't have an SMMU */
  622. if (!soc)
  623. return NULL;
  624. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  625. if (!smmu)
  626. return ERR_PTR(-ENOMEM);
  627. /*
  628. * This is a bit of a hack. Ideally we'd want to simply return this
  629. * value. However the IOMMU registration process will attempt to add
  630. * all devices to the IOMMU when bus_set_iommu() is called. In order
  631. * not to rely on global variables to track the IOMMU instance, we
  632. * set it here so that it can be looked up from the .add_device()
  633. * callback via the IOMMU device's .drvdata field.
  634. */
  635. mc->smmu = smmu;
  636. size = BITS_TO_LONGS(soc->num_asids) * sizeof(long);
  637. smmu->asids = devm_kzalloc(dev, size, GFP_KERNEL);
  638. if (!smmu->asids)
  639. return ERR_PTR(-ENOMEM);
  640. mutex_init(&smmu->lock);
  641. smmu->regs = mc->regs;
  642. smmu->soc = soc;
  643. smmu->dev = dev;
  644. smmu->mc = mc;
  645. smmu->pfn_mask = BIT_MASK(mc->soc->num_address_bits - PAGE_SHIFT) - 1;
  646. dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
  647. mc->soc->num_address_bits, smmu->pfn_mask);
  648. value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
  649. if (soc->supports_request_limit)
  650. value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
  651. smmu_writel(smmu, value, SMMU_PTC_CONFIG);
  652. value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
  653. SMMU_TLB_CONFIG_ACTIVE_LINES(0x20);
  654. if (soc->supports_round_robin_arbitration)
  655. value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
  656. smmu_writel(smmu, value, SMMU_TLB_CONFIG);
  657. smmu_flush_ptc(smmu, NULL, 0);
  658. smmu_flush_tlb(smmu);
  659. smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
  660. smmu_flush(smmu);
  661. tegra_smmu_ahb_enable();
  662. err = bus_set_iommu(&platform_bus_type, &tegra_smmu_ops);
  663. if (err < 0)
  664. return ERR_PTR(err);
  665. if (IS_ENABLED(CONFIG_DEBUG_FS))
  666. tegra_smmu_debugfs_init(smmu);
  667. return smmu;
  668. }
  669. void tegra_smmu_remove(struct tegra_smmu *smmu)
  670. {
  671. if (IS_ENABLED(CONFIG_DEBUG_FS))
  672. tegra_smmu_debugfs_exit(smmu);
  673. }