tegra-smmu.c 17 KB

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