tegra-smmu.c 17 KB

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