tegra-smmu.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011-2014 NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/err.h>
  8. #include <linux/iommu.h>
  9. #include <linux/kernel.h>
  10. #include <linux/of.h>
  11. #include <linux/of_platform.h>
  12. #include <linux/pci.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/dma-mapping.h>
  17. #include <soc/tegra/ahb.h>
  18. #include <soc/tegra/mc.h>
  19. #include "iommu-pages.h"
  20. struct tegra_smmu_group {
  21. struct list_head list;
  22. struct tegra_smmu *smmu;
  23. const struct tegra_smmu_group_soc *soc;
  24. struct iommu_group *group;
  25. unsigned int swgroup;
  26. };
  27. struct tegra_smmu {
  28. void __iomem *regs;
  29. struct device *dev;
  30. struct tegra_mc *mc;
  31. const struct tegra_smmu_soc *soc;
  32. struct list_head groups;
  33. unsigned long pfn_mask;
  34. unsigned long tlb_mask;
  35. unsigned long *asids;
  36. struct mutex lock;
  37. struct list_head list;
  38. struct dentry *debugfs;
  39. struct iommu_device iommu; /* IOMMU Core code handle */
  40. };
  41. struct tegra_pd;
  42. struct tegra_pt;
  43. struct tegra_smmu_as {
  44. struct iommu_domain domain;
  45. struct tegra_smmu *smmu;
  46. unsigned int use_count;
  47. spinlock_t lock;
  48. u32 *count;
  49. struct tegra_pt **pts;
  50. struct tegra_pd *pd;
  51. dma_addr_t pd_dma;
  52. unsigned id;
  53. u32 attr;
  54. };
  55. static struct tegra_smmu_as *to_smmu_as(struct iommu_domain *dom)
  56. {
  57. return container_of(dom, struct tegra_smmu_as, domain);
  58. }
  59. static inline void smmu_writel(struct tegra_smmu *smmu, u32 value,
  60. unsigned long offset)
  61. {
  62. writel(value, smmu->regs + offset);
  63. }
  64. static inline u32 smmu_readl(struct tegra_smmu *smmu, unsigned long offset)
  65. {
  66. return readl(smmu->regs + offset);
  67. }
  68. #define SMMU_CONFIG 0x010
  69. #define SMMU_CONFIG_ENABLE (1 << 0)
  70. #define SMMU_TLB_CONFIG 0x14
  71. #define SMMU_TLB_CONFIG_HIT_UNDER_MISS (1 << 29)
  72. #define SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION (1 << 28)
  73. #define SMMU_TLB_CONFIG_ACTIVE_LINES(smmu) \
  74. ((smmu)->soc->num_tlb_lines & (smmu)->tlb_mask)
  75. #define SMMU_PTC_CONFIG 0x18
  76. #define SMMU_PTC_CONFIG_ENABLE (1 << 29)
  77. #define SMMU_PTC_CONFIG_REQ_LIMIT(x) (((x) & 0x0f) << 24)
  78. #define SMMU_PTC_CONFIG_INDEX_MAP(x) ((x) & 0x3f)
  79. #define SMMU_PTB_ASID 0x01c
  80. #define SMMU_PTB_ASID_VALUE(x) ((x) & 0x7f)
  81. #define SMMU_PTB_DATA 0x020
  82. #define SMMU_PTB_DATA_VALUE(dma, attr) ((dma) >> 12 | (attr))
  83. #define SMMU_MK_PDE(dma, attr) ((dma) >> SMMU_PTE_SHIFT | (attr))
  84. #define SMMU_TLB_FLUSH 0x030
  85. #define SMMU_TLB_FLUSH_VA_MATCH_ALL (0 << 0)
  86. #define SMMU_TLB_FLUSH_VA_MATCH_SECTION (2 << 0)
  87. #define SMMU_TLB_FLUSH_VA_MATCH_GROUP (3 << 0)
  88. #define SMMU_TLB_FLUSH_VA_SECTION(addr) ((((addr) & 0xffc00000) >> 12) | \
  89. SMMU_TLB_FLUSH_VA_MATCH_SECTION)
  90. #define SMMU_TLB_FLUSH_VA_GROUP(addr) ((((addr) & 0xffffc000) >> 12) | \
  91. SMMU_TLB_FLUSH_VA_MATCH_GROUP)
  92. #define SMMU_TLB_FLUSH_ASID_MATCH (1 << 31)
  93. #define SMMU_PTC_FLUSH 0x034
  94. #define SMMU_PTC_FLUSH_TYPE_ALL (0 << 0)
  95. #define SMMU_PTC_FLUSH_TYPE_ADR (1 << 0)
  96. #define SMMU_PTC_FLUSH_HI 0x9b8
  97. #define SMMU_PTC_FLUSH_HI_MASK 0x3
  98. /* per-SWGROUP SMMU_*_ASID register */
  99. #define SMMU_ASID_ENABLE (1 << 31)
  100. #define SMMU_ASID_MASK 0x7f
  101. #define SMMU_ASID_VALUE(x) ((x) & SMMU_ASID_MASK)
  102. /* page table definitions */
  103. #define SMMU_NUM_PDE 1024
  104. #define SMMU_NUM_PTE 1024
  105. #define SMMU_SIZE_PD (SMMU_NUM_PDE * 4)
  106. #define SMMU_SIZE_PT (SMMU_NUM_PTE * 4)
  107. #define SMMU_PDE_SHIFT 22
  108. #define SMMU_PTE_SHIFT 12
  109. #define SMMU_PAGE_MASK (~(SMMU_SIZE_PT-1))
  110. #define SMMU_OFFSET_IN_PAGE(x) ((unsigned long)(x) & ~SMMU_PAGE_MASK)
  111. #define SMMU_PFN_PHYS(x) ((phys_addr_t)(x) << SMMU_PTE_SHIFT)
  112. #define SMMU_PHYS_PFN(x) ((unsigned long)((x) >> SMMU_PTE_SHIFT))
  113. #define SMMU_PD_READABLE (1 << 31)
  114. #define SMMU_PD_WRITABLE (1 << 30)
  115. #define SMMU_PD_NONSECURE (1 << 29)
  116. #define SMMU_PDE_READABLE (1 << 31)
  117. #define SMMU_PDE_WRITABLE (1 << 30)
  118. #define SMMU_PDE_NONSECURE (1 << 29)
  119. #define SMMU_PDE_NEXT (1 << 28)
  120. #define SMMU_PTE_READABLE (1 << 31)
  121. #define SMMU_PTE_WRITABLE (1 << 30)
  122. #define SMMU_PTE_NONSECURE (1 << 29)
  123. #define SMMU_PDE_ATTR (SMMU_PDE_READABLE | SMMU_PDE_WRITABLE | \
  124. SMMU_PDE_NONSECURE)
  125. struct tegra_pd {
  126. u32 val[SMMU_NUM_PDE];
  127. };
  128. struct tegra_pt {
  129. u32 val[SMMU_NUM_PTE];
  130. };
  131. static unsigned int iova_pd_index(unsigned long iova)
  132. {
  133. return (iova >> SMMU_PDE_SHIFT) & (SMMU_NUM_PDE - 1);
  134. }
  135. static unsigned int iova_pt_index(unsigned long iova)
  136. {
  137. return (iova >> SMMU_PTE_SHIFT) & (SMMU_NUM_PTE - 1);
  138. }
  139. static bool smmu_dma_addr_valid(struct tegra_smmu *smmu, dma_addr_t addr)
  140. {
  141. addr >>= 12;
  142. return (addr & smmu->pfn_mask) == addr;
  143. }
  144. static dma_addr_t smmu_pde_to_dma(struct tegra_smmu *smmu, u32 pde)
  145. {
  146. return (dma_addr_t)(pde & smmu->pfn_mask) << 12;
  147. }
  148. static void smmu_flush_ptc_all(struct tegra_smmu *smmu)
  149. {
  150. smmu_writel(smmu, SMMU_PTC_FLUSH_TYPE_ALL, SMMU_PTC_FLUSH);
  151. }
  152. static inline void smmu_flush_ptc(struct tegra_smmu *smmu, dma_addr_t dma,
  153. unsigned long offset)
  154. {
  155. u32 value;
  156. offset &= ~(smmu->mc->soc->atom_size - 1);
  157. if (smmu->mc->soc->num_address_bits > 32) {
  158. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  159. value = (dma >> 32) & SMMU_PTC_FLUSH_HI_MASK;
  160. #else
  161. value = 0;
  162. #endif
  163. smmu_writel(smmu, value, SMMU_PTC_FLUSH_HI);
  164. }
  165. value = (dma + offset) | SMMU_PTC_FLUSH_TYPE_ADR;
  166. smmu_writel(smmu, value, SMMU_PTC_FLUSH);
  167. }
  168. static inline void smmu_flush_tlb(struct tegra_smmu *smmu)
  169. {
  170. smmu_writel(smmu, SMMU_TLB_FLUSH_VA_MATCH_ALL, SMMU_TLB_FLUSH);
  171. }
  172. static inline void smmu_flush_tlb_asid(struct tegra_smmu *smmu,
  173. unsigned long asid)
  174. {
  175. u32 value;
  176. if (smmu->soc->num_asids == 4)
  177. value = (asid & 0x3) << 29;
  178. else
  179. value = (asid & 0x7f) << 24;
  180. value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_MATCH_ALL;
  181. smmu_writel(smmu, value, SMMU_TLB_FLUSH);
  182. }
  183. static inline void smmu_flush_tlb_section(struct tegra_smmu *smmu,
  184. unsigned long asid,
  185. unsigned long iova)
  186. {
  187. u32 value;
  188. if (smmu->soc->num_asids == 4)
  189. value = (asid & 0x3) << 29;
  190. else
  191. value = (asid & 0x7f) << 24;
  192. value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_SECTION(iova);
  193. smmu_writel(smmu, value, SMMU_TLB_FLUSH);
  194. }
  195. static inline void smmu_flush_tlb_group(struct tegra_smmu *smmu,
  196. unsigned long asid,
  197. unsigned long iova)
  198. {
  199. u32 value;
  200. if (smmu->soc->num_asids == 4)
  201. value = (asid & 0x3) << 29;
  202. else
  203. value = (asid & 0x7f) << 24;
  204. value |= SMMU_TLB_FLUSH_ASID_MATCH | SMMU_TLB_FLUSH_VA_GROUP(iova);
  205. smmu_writel(smmu, value, SMMU_TLB_FLUSH);
  206. }
  207. static inline void smmu_flush(struct tegra_smmu *smmu)
  208. {
  209. smmu_readl(smmu, SMMU_PTB_ASID);
  210. }
  211. static int tegra_smmu_alloc_asid(struct tegra_smmu *smmu, unsigned int *idp)
  212. {
  213. unsigned long id;
  214. id = find_first_zero_bit(smmu->asids, smmu->soc->num_asids);
  215. if (id >= smmu->soc->num_asids)
  216. return -ENOSPC;
  217. set_bit(id, smmu->asids);
  218. *idp = id;
  219. return 0;
  220. }
  221. static void tegra_smmu_free_asid(struct tegra_smmu *smmu, unsigned int id)
  222. {
  223. clear_bit(id, smmu->asids);
  224. }
  225. static struct iommu_domain *tegra_smmu_domain_alloc_paging(struct device *dev)
  226. {
  227. struct tegra_smmu_as *as;
  228. as = kzalloc_obj(*as);
  229. if (!as)
  230. return NULL;
  231. as->attr = SMMU_PD_READABLE | SMMU_PD_WRITABLE | SMMU_PD_NONSECURE;
  232. as->pd = iommu_alloc_pages_sz(GFP_KERNEL | __GFP_DMA, SMMU_SIZE_PD);
  233. if (!as->pd) {
  234. kfree(as);
  235. return NULL;
  236. }
  237. as->count = kcalloc(SMMU_NUM_PDE, sizeof(u32), GFP_KERNEL);
  238. if (!as->count) {
  239. iommu_free_pages(as->pd);
  240. kfree(as);
  241. return NULL;
  242. }
  243. as->pts = kzalloc_objs(*as->pts, SMMU_NUM_PDE);
  244. if (!as->pts) {
  245. kfree(as->count);
  246. iommu_free_pages(as->pd);
  247. kfree(as);
  248. return NULL;
  249. }
  250. spin_lock_init(&as->lock);
  251. as->domain.pgsize_bitmap = SZ_4K;
  252. /* setup aperture */
  253. as->domain.geometry.aperture_start = 0;
  254. as->domain.geometry.aperture_end = 0xffffffff;
  255. as->domain.geometry.force_aperture = true;
  256. return &as->domain;
  257. }
  258. static void tegra_smmu_domain_free(struct iommu_domain *domain)
  259. {
  260. struct tegra_smmu_as *as = to_smmu_as(domain);
  261. /* TODO: free page directory and page tables */
  262. WARN_ON_ONCE(as->use_count);
  263. kfree(as->count);
  264. kfree(as->pts);
  265. kfree(as);
  266. }
  267. static const struct tegra_smmu_swgroup *
  268. tegra_smmu_find_swgroup(struct tegra_smmu *smmu, unsigned int swgroup)
  269. {
  270. const struct tegra_smmu_swgroup *group = NULL;
  271. unsigned int i;
  272. for (i = 0; i < smmu->soc->num_swgroups; i++) {
  273. if (smmu->soc->swgroups[i].swgroup == swgroup) {
  274. group = &smmu->soc->swgroups[i];
  275. break;
  276. }
  277. }
  278. return group;
  279. }
  280. static void tegra_smmu_enable(struct tegra_smmu *smmu, unsigned int swgroup,
  281. unsigned int asid)
  282. {
  283. const struct tegra_smmu_swgroup *group;
  284. unsigned int i;
  285. u32 value;
  286. group = tegra_smmu_find_swgroup(smmu, swgroup);
  287. if (group) {
  288. value = smmu_readl(smmu, group->reg);
  289. value &= ~SMMU_ASID_MASK;
  290. value |= SMMU_ASID_VALUE(asid);
  291. value |= SMMU_ASID_ENABLE;
  292. smmu_writel(smmu, value, group->reg);
  293. } else {
  294. pr_warn("%s group from swgroup %u not found\n", __func__,
  295. swgroup);
  296. /* No point moving ahead if group was not found */
  297. return;
  298. }
  299. for (i = 0; i < smmu->soc->num_clients; i++) {
  300. const struct tegra_mc_client *client = &smmu->soc->clients[i];
  301. if (client->swgroup != swgroup)
  302. continue;
  303. value = smmu_readl(smmu, client->regs.smmu.reg);
  304. value |= BIT(client->regs.smmu.bit);
  305. smmu_writel(smmu, value, client->regs.smmu.reg);
  306. }
  307. }
  308. static void tegra_smmu_disable(struct tegra_smmu *smmu, unsigned int swgroup,
  309. unsigned int asid)
  310. {
  311. const struct tegra_smmu_swgroup *group;
  312. unsigned int i;
  313. u32 value;
  314. group = tegra_smmu_find_swgroup(smmu, swgroup);
  315. if (group) {
  316. value = smmu_readl(smmu, group->reg);
  317. value &= ~SMMU_ASID_MASK;
  318. value |= SMMU_ASID_VALUE(asid);
  319. value &= ~SMMU_ASID_ENABLE;
  320. smmu_writel(smmu, value, group->reg);
  321. }
  322. for (i = 0; i < smmu->soc->num_clients; i++) {
  323. const struct tegra_mc_client *client = &smmu->soc->clients[i];
  324. if (client->swgroup != swgroup)
  325. continue;
  326. value = smmu_readl(smmu, client->regs.smmu.reg);
  327. value &= ~BIT(client->regs.smmu.bit);
  328. smmu_writel(smmu, value, client->regs.smmu.reg);
  329. }
  330. }
  331. static int tegra_smmu_as_prepare(struct tegra_smmu *smmu,
  332. struct tegra_smmu_as *as)
  333. {
  334. u32 value;
  335. int err = 0;
  336. mutex_lock(&smmu->lock);
  337. if (as->use_count > 0) {
  338. as->use_count++;
  339. goto unlock;
  340. }
  341. as->pd_dma =
  342. dma_map_single(smmu->dev, as->pd, SMMU_SIZE_PD, DMA_TO_DEVICE);
  343. if (dma_mapping_error(smmu->dev, as->pd_dma)) {
  344. err = -ENOMEM;
  345. goto unlock;
  346. }
  347. /* We can't handle 64-bit DMA addresses */
  348. if (!smmu_dma_addr_valid(smmu, as->pd_dma)) {
  349. err = -ENOMEM;
  350. goto err_unmap;
  351. }
  352. err = tegra_smmu_alloc_asid(smmu, &as->id);
  353. if (err < 0)
  354. goto err_unmap;
  355. smmu_flush_ptc(smmu, as->pd_dma, 0);
  356. smmu_flush_tlb_asid(smmu, as->id);
  357. smmu_writel(smmu, as->id & 0x7f, SMMU_PTB_ASID);
  358. value = SMMU_PTB_DATA_VALUE(as->pd_dma, as->attr);
  359. smmu_writel(smmu, value, SMMU_PTB_DATA);
  360. smmu_flush(smmu);
  361. as->smmu = smmu;
  362. as->use_count++;
  363. mutex_unlock(&smmu->lock);
  364. return 0;
  365. err_unmap:
  366. dma_unmap_single(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
  367. unlock:
  368. mutex_unlock(&smmu->lock);
  369. return err;
  370. }
  371. static void tegra_smmu_as_unprepare(struct tegra_smmu *smmu,
  372. struct tegra_smmu_as *as)
  373. {
  374. mutex_lock(&smmu->lock);
  375. if (--as->use_count > 0) {
  376. mutex_unlock(&smmu->lock);
  377. return;
  378. }
  379. tegra_smmu_free_asid(smmu, as->id);
  380. dma_unmap_single(smmu->dev, as->pd_dma, SMMU_SIZE_PD, DMA_TO_DEVICE);
  381. as->smmu = NULL;
  382. mutex_unlock(&smmu->lock);
  383. }
  384. static int tegra_smmu_attach_dev(struct iommu_domain *domain,
  385. struct device *dev, struct iommu_domain *old)
  386. {
  387. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  388. struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
  389. struct tegra_smmu_as *as = to_smmu_as(domain);
  390. unsigned int index;
  391. int err;
  392. if (!fwspec)
  393. return -ENOENT;
  394. for (index = 0; index < fwspec->num_ids; index++) {
  395. err = tegra_smmu_as_prepare(smmu, as);
  396. if (err)
  397. goto disable;
  398. tegra_smmu_enable(smmu, fwspec->ids[index], as->id);
  399. }
  400. if (index == 0)
  401. return -ENODEV;
  402. return 0;
  403. disable:
  404. while (index--) {
  405. tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
  406. tegra_smmu_as_unprepare(smmu, as);
  407. }
  408. return err;
  409. }
  410. static int tegra_smmu_identity_attach(struct iommu_domain *identity_domain,
  411. struct device *dev,
  412. struct iommu_domain *old)
  413. {
  414. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  415. struct tegra_smmu_as *as;
  416. struct tegra_smmu *smmu;
  417. unsigned int index;
  418. if (!fwspec)
  419. return -ENODEV;
  420. if (old == identity_domain || !old)
  421. return 0;
  422. as = to_smmu_as(old);
  423. smmu = as->smmu;
  424. for (index = 0; index < fwspec->num_ids; index++) {
  425. tegra_smmu_disable(smmu, fwspec->ids[index], as->id);
  426. tegra_smmu_as_unprepare(smmu, as);
  427. }
  428. return 0;
  429. }
  430. static struct iommu_domain_ops tegra_smmu_identity_ops = {
  431. .attach_dev = tegra_smmu_identity_attach,
  432. };
  433. static struct iommu_domain tegra_smmu_identity_domain = {
  434. .type = IOMMU_DOMAIN_IDENTITY,
  435. .ops = &tegra_smmu_identity_ops,
  436. };
  437. static void tegra_smmu_set_pde(struct tegra_smmu_as *as, unsigned long iova,
  438. u32 value)
  439. {
  440. unsigned int pd_index = iova_pd_index(iova);
  441. struct tegra_smmu *smmu = as->smmu;
  442. u32 *pd = &as->pd->val[pd_index];
  443. unsigned long offset = pd_index * sizeof(*pd);
  444. /* Set the page directory entry first */
  445. *pd = value;
  446. /* The flush the page directory entry from caches */
  447. dma_sync_single_range_for_device(smmu->dev, as->pd_dma, offset,
  448. sizeof(*pd), DMA_TO_DEVICE);
  449. /* And flush the iommu */
  450. smmu_flush_ptc(smmu, as->pd_dma, offset);
  451. smmu_flush_tlb_section(smmu, as->id, iova);
  452. smmu_flush(smmu);
  453. }
  454. static u32 *tegra_smmu_pte_offset(struct tegra_pt *pt, unsigned long iova)
  455. {
  456. return &pt->val[iova_pt_index(iova)];
  457. }
  458. static u32 *tegra_smmu_pte_lookup(struct tegra_smmu_as *as, unsigned long iova,
  459. dma_addr_t *dmap)
  460. {
  461. unsigned int pd_index = iova_pd_index(iova);
  462. struct tegra_smmu *smmu = as->smmu;
  463. struct tegra_pt *pt;
  464. pt = as->pts[pd_index];
  465. if (!pt)
  466. return NULL;
  467. *dmap = smmu_pde_to_dma(smmu, as->pd->val[pd_index]);
  468. return tegra_smmu_pte_offset(pt, iova);
  469. }
  470. static u32 *as_get_pte(struct tegra_smmu_as *as, dma_addr_t iova,
  471. dma_addr_t *dmap, struct tegra_pt *pt)
  472. {
  473. unsigned int pde = iova_pd_index(iova);
  474. struct tegra_smmu *smmu = as->smmu;
  475. if (!as->pts[pde]) {
  476. dma_addr_t dma;
  477. dma = dma_map_single(smmu->dev, pt, SMMU_SIZE_PT,
  478. DMA_TO_DEVICE);
  479. if (dma_mapping_error(smmu->dev, dma)) {
  480. iommu_free_pages(pt);
  481. return NULL;
  482. }
  483. if (!smmu_dma_addr_valid(smmu, dma)) {
  484. dma_unmap_single(smmu->dev, dma, SMMU_SIZE_PT,
  485. DMA_TO_DEVICE);
  486. iommu_free_pages(pt);
  487. return NULL;
  488. }
  489. as->pts[pde] = pt;
  490. tegra_smmu_set_pde(as, iova, SMMU_MK_PDE(dma, SMMU_PDE_ATTR |
  491. SMMU_PDE_NEXT));
  492. *dmap = dma;
  493. } else {
  494. *dmap = smmu_pde_to_dma(smmu, as->pd->val[pde]);
  495. }
  496. return tegra_smmu_pte_offset(as->pts[pde], iova);
  497. }
  498. static void tegra_smmu_pte_get_use(struct tegra_smmu_as *as, unsigned long iova)
  499. {
  500. unsigned int pd_index = iova_pd_index(iova);
  501. as->count[pd_index]++;
  502. }
  503. static void tegra_smmu_pte_put_use(struct tegra_smmu_as *as, unsigned long iova)
  504. {
  505. unsigned int pde = iova_pd_index(iova);
  506. struct tegra_pt *pt = as->pts[pde];
  507. /*
  508. * When no entries in this page table are used anymore, return the
  509. * memory page to the system.
  510. */
  511. if (--as->count[pde] == 0) {
  512. struct tegra_smmu *smmu = as->smmu;
  513. dma_addr_t pte_dma = smmu_pde_to_dma(smmu, as->pd->val[pde]);
  514. tegra_smmu_set_pde(as, iova, 0);
  515. dma_unmap_single(smmu->dev, pte_dma, SMMU_SIZE_PT,
  516. DMA_TO_DEVICE);
  517. iommu_free_pages(pt);
  518. as->pts[pde] = NULL;
  519. }
  520. }
  521. static void tegra_smmu_set_pte(struct tegra_smmu_as *as, unsigned long iova,
  522. u32 *pte, dma_addr_t pte_dma, u32 val)
  523. {
  524. struct tegra_smmu *smmu = as->smmu;
  525. unsigned long offset = SMMU_OFFSET_IN_PAGE(pte);
  526. *pte = val;
  527. dma_sync_single_range_for_device(smmu->dev, pte_dma, offset,
  528. 4, DMA_TO_DEVICE);
  529. smmu_flush_ptc(smmu, pte_dma, offset);
  530. smmu_flush_tlb_group(smmu, as->id, iova);
  531. smmu_flush(smmu);
  532. }
  533. static struct tegra_pt *as_get_pde_page(struct tegra_smmu_as *as,
  534. unsigned long iova, gfp_t gfp,
  535. unsigned long *flags)
  536. {
  537. unsigned int pde = iova_pd_index(iova);
  538. struct tegra_pt *pt = as->pts[pde];
  539. /* at first check whether allocation needs to be done at all */
  540. if (pt)
  541. return pt;
  542. /*
  543. * In order to prevent exhaustion of the atomic memory pool, we
  544. * allocate page in a sleeping context if GFP flags permit. Hence
  545. * spinlock needs to be unlocked and re-locked after allocation.
  546. */
  547. if (gfpflags_allow_blocking(gfp))
  548. spin_unlock_irqrestore(&as->lock, *flags);
  549. pt = iommu_alloc_pages_sz(gfp | __GFP_DMA, SMMU_SIZE_PT);
  550. if (gfpflags_allow_blocking(gfp))
  551. spin_lock_irqsave(&as->lock, *flags);
  552. /*
  553. * In a case of blocking allocation, a concurrent mapping may win
  554. * the PDE allocation. In this case the allocated page isn't needed
  555. * if allocation succeeded and the allocation failure isn't fatal.
  556. */
  557. if (as->pts[pde]) {
  558. if (pt)
  559. iommu_free_pages(pt);
  560. pt = as->pts[pde];
  561. }
  562. return pt;
  563. }
  564. static int
  565. __tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
  566. phys_addr_t paddr, size_t size, int prot, gfp_t gfp,
  567. unsigned long *flags)
  568. {
  569. struct tegra_smmu_as *as = to_smmu_as(domain);
  570. dma_addr_t pte_dma;
  571. struct tegra_pt *pt;
  572. u32 pte_attrs;
  573. u32 *pte;
  574. pt = as_get_pde_page(as, iova, gfp, flags);
  575. if (!pt)
  576. return -ENOMEM;
  577. pte = as_get_pte(as, iova, &pte_dma, pt);
  578. if (!pte)
  579. return -ENOMEM;
  580. /* If we aren't overwriting a pre-existing entry, increment use */
  581. if (*pte == 0)
  582. tegra_smmu_pte_get_use(as, iova);
  583. pte_attrs = SMMU_PTE_NONSECURE;
  584. if (prot & IOMMU_READ)
  585. pte_attrs |= SMMU_PTE_READABLE;
  586. if (prot & IOMMU_WRITE)
  587. pte_attrs |= SMMU_PTE_WRITABLE;
  588. tegra_smmu_set_pte(as, iova, pte, pte_dma,
  589. SMMU_PHYS_PFN(paddr) | pte_attrs);
  590. return 0;
  591. }
  592. static size_t
  593. __tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  594. size_t size, struct iommu_iotlb_gather *gather)
  595. {
  596. struct tegra_smmu_as *as = to_smmu_as(domain);
  597. dma_addr_t pte_dma;
  598. u32 *pte;
  599. pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
  600. if (!pte || !*pte)
  601. return 0;
  602. tegra_smmu_set_pte(as, iova, pte, pte_dma, 0);
  603. tegra_smmu_pte_put_use(as, iova);
  604. return size;
  605. }
  606. static int tegra_smmu_map(struct iommu_domain *domain, unsigned long iova,
  607. phys_addr_t paddr, size_t size, size_t count,
  608. int prot, gfp_t gfp, size_t *mapped)
  609. {
  610. struct tegra_smmu_as *as = to_smmu_as(domain);
  611. unsigned long flags;
  612. int ret;
  613. spin_lock_irqsave(&as->lock, flags);
  614. ret = __tegra_smmu_map(domain, iova, paddr, size, prot, gfp, &flags);
  615. spin_unlock_irqrestore(&as->lock, flags);
  616. if (!ret)
  617. *mapped = size;
  618. return ret;
  619. }
  620. static size_t tegra_smmu_unmap(struct iommu_domain *domain, unsigned long iova,
  621. size_t size, size_t count, struct iommu_iotlb_gather *gather)
  622. {
  623. struct tegra_smmu_as *as = to_smmu_as(domain);
  624. unsigned long flags;
  625. spin_lock_irqsave(&as->lock, flags);
  626. size = __tegra_smmu_unmap(domain, iova, size, gather);
  627. spin_unlock_irqrestore(&as->lock, flags);
  628. return size;
  629. }
  630. static phys_addr_t tegra_smmu_iova_to_phys(struct iommu_domain *domain,
  631. dma_addr_t iova)
  632. {
  633. struct tegra_smmu_as *as = to_smmu_as(domain);
  634. unsigned long pfn;
  635. dma_addr_t pte_dma;
  636. u32 *pte;
  637. pte = tegra_smmu_pte_lookup(as, iova, &pte_dma);
  638. if (!pte || !*pte)
  639. return 0;
  640. pfn = *pte & as->smmu->pfn_mask;
  641. return SMMU_PFN_PHYS(pfn) + SMMU_OFFSET_IN_PAGE(iova);
  642. }
  643. static struct tegra_smmu *tegra_smmu_find(struct device_node *np)
  644. {
  645. struct platform_device *pdev;
  646. struct tegra_mc *mc;
  647. pdev = of_find_device_by_node(np);
  648. if (!pdev)
  649. return NULL;
  650. mc = platform_get_drvdata(pdev);
  651. put_device(&pdev->dev);
  652. if (!mc)
  653. return NULL;
  654. return mc->smmu;
  655. }
  656. static int tegra_smmu_configure(struct tegra_smmu *smmu, struct device *dev,
  657. const struct of_phandle_args *args)
  658. {
  659. const struct iommu_ops *ops = smmu->iommu.ops;
  660. int err;
  661. err = iommu_fwspec_init(dev, dev_fwnode(smmu->dev));
  662. if (err < 0) {
  663. dev_err(dev, "failed to initialize fwspec: %d\n", err);
  664. return err;
  665. }
  666. err = ops->of_xlate(dev, args);
  667. if (err < 0) {
  668. dev_err(dev, "failed to parse SW group ID: %d\n", err);
  669. return err;
  670. }
  671. return 0;
  672. }
  673. static struct iommu_device *tegra_smmu_probe_device(struct device *dev)
  674. {
  675. struct device_node *np = dev->of_node;
  676. struct tegra_smmu *smmu = NULL;
  677. struct of_phandle_args args;
  678. unsigned int index = 0;
  679. int err;
  680. while (of_parse_phandle_with_args(np, "iommus", "#iommu-cells", index,
  681. &args) == 0) {
  682. smmu = tegra_smmu_find(args.np);
  683. if (smmu) {
  684. err = tegra_smmu_configure(smmu, dev, &args);
  685. if (err < 0) {
  686. of_node_put(args.np);
  687. return ERR_PTR(err);
  688. }
  689. }
  690. of_node_put(args.np);
  691. index++;
  692. }
  693. smmu = dev_iommu_priv_get(dev);
  694. if (!smmu)
  695. return ERR_PTR(-ENODEV);
  696. return &smmu->iommu;
  697. }
  698. static const struct tegra_smmu_group_soc *
  699. tegra_smmu_find_group(struct tegra_smmu *smmu, unsigned int swgroup)
  700. {
  701. unsigned int i, j;
  702. for (i = 0; i < smmu->soc->num_groups; i++)
  703. for (j = 0; j < smmu->soc->groups[i].num_swgroups; j++)
  704. if (smmu->soc->groups[i].swgroups[j] == swgroup)
  705. return &smmu->soc->groups[i];
  706. return NULL;
  707. }
  708. static void tegra_smmu_group_release(void *iommu_data)
  709. {
  710. struct tegra_smmu_group *group = iommu_data;
  711. struct tegra_smmu *smmu = group->smmu;
  712. mutex_lock(&smmu->lock);
  713. list_del(&group->list);
  714. mutex_unlock(&smmu->lock);
  715. }
  716. static struct iommu_group *tegra_smmu_device_group(struct device *dev)
  717. {
  718. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  719. struct tegra_smmu *smmu = dev_iommu_priv_get(dev);
  720. const struct tegra_smmu_group_soc *soc;
  721. unsigned int swgroup = fwspec->ids[0];
  722. struct tegra_smmu_group *group;
  723. struct iommu_group *grp;
  724. /* Find group_soc associating with swgroup */
  725. soc = tegra_smmu_find_group(smmu, swgroup);
  726. mutex_lock(&smmu->lock);
  727. /* Find existing iommu_group associating with swgroup or group_soc */
  728. list_for_each_entry(group, &smmu->groups, list)
  729. if ((group->swgroup == swgroup) || (soc && group->soc == soc)) {
  730. grp = iommu_group_ref_get(group->group);
  731. mutex_unlock(&smmu->lock);
  732. return grp;
  733. }
  734. group = devm_kzalloc(smmu->dev, sizeof(*group), GFP_KERNEL);
  735. if (!group) {
  736. mutex_unlock(&smmu->lock);
  737. return NULL;
  738. }
  739. INIT_LIST_HEAD(&group->list);
  740. group->swgroup = swgroup;
  741. group->smmu = smmu;
  742. group->soc = soc;
  743. if (dev_is_pci(dev))
  744. group->group = pci_device_group(dev);
  745. else
  746. group->group = generic_device_group(dev);
  747. if (IS_ERR(group->group)) {
  748. devm_kfree(smmu->dev, group);
  749. mutex_unlock(&smmu->lock);
  750. return NULL;
  751. }
  752. iommu_group_set_iommudata(group->group, group, tegra_smmu_group_release);
  753. if (soc)
  754. iommu_group_set_name(group->group, soc->name);
  755. list_add_tail(&group->list, &smmu->groups);
  756. mutex_unlock(&smmu->lock);
  757. return group->group;
  758. }
  759. static int tegra_smmu_of_xlate(struct device *dev,
  760. const struct of_phandle_args *args)
  761. {
  762. struct platform_device *iommu_pdev = of_find_device_by_node(args->np);
  763. struct tegra_mc *mc = platform_get_drvdata(iommu_pdev);
  764. u32 id = args->args[0];
  765. /*
  766. * Note: we are here releasing the reference of &iommu_pdev->dev, which
  767. * is mc->dev. Although some functions in tegra_smmu_ops may keep using
  768. * its private data beyond this point, it's still safe to do so because
  769. * the SMMU parent device is the same as the MC, so the reference count
  770. * isn't strictly necessary.
  771. */
  772. put_device(&iommu_pdev->dev);
  773. dev_iommu_priv_set(dev, mc->smmu);
  774. return iommu_fwspec_add_ids(dev, &id, 1);
  775. }
  776. static int tegra_smmu_def_domain_type(struct device *dev)
  777. {
  778. /*
  779. * FIXME: For now we want to run all translation in IDENTITY mode, due
  780. * to some device quirks. Better would be to just quirk the troubled
  781. * devices.
  782. */
  783. return IOMMU_DOMAIN_IDENTITY;
  784. }
  785. static const struct iommu_ops tegra_smmu_ops = {
  786. .identity_domain = &tegra_smmu_identity_domain,
  787. .def_domain_type = &tegra_smmu_def_domain_type,
  788. .domain_alloc_paging = tegra_smmu_domain_alloc_paging,
  789. .probe_device = tegra_smmu_probe_device,
  790. .device_group = tegra_smmu_device_group,
  791. .of_xlate = tegra_smmu_of_xlate,
  792. .default_domain_ops = &(const struct iommu_domain_ops) {
  793. .attach_dev = tegra_smmu_attach_dev,
  794. .map_pages = tegra_smmu_map,
  795. .unmap_pages = tegra_smmu_unmap,
  796. .iova_to_phys = tegra_smmu_iova_to_phys,
  797. .free = tegra_smmu_domain_free,
  798. }
  799. };
  800. static void tegra_smmu_ahb_enable(void)
  801. {
  802. static const struct of_device_id ahb_match[] = {
  803. { .compatible = "nvidia,tegra30-ahb", },
  804. { }
  805. };
  806. struct device_node *ahb;
  807. ahb = of_find_matching_node(NULL, ahb_match);
  808. if (ahb) {
  809. tegra_ahb_enable_smmu(ahb);
  810. of_node_put(ahb);
  811. }
  812. }
  813. static int tegra_smmu_swgroups_show(struct seq_file *s, void *data)
  814. {
  815. struct tegra_smmu *smmu = s->private;
  816. unsigned int i;
  817. u32 value;
  818. seq_printf(s, "swgroup enabled ASID\n");
  819. seq_printf(s, "------------------------\n");
  820. for (i = 0; i < smmu->soc->num_swgroups; i++) {
  821. const struct tegra_smmu_swgroup *group = &smmu->soc->swgroups[i];
  822. const char *status;
  823. unsigned int asid;
  824. value = smmu_readl(smmu, group->reg);
  825. if (value & SMMU_ASID_ENABLE)
  826. status = "yes";
  827. else
  828. status = "no";
  829. asid = value & SMMU_ASID_MASK;
  830. seq_printf(s, "%-9s %-7s %#04x\n", group->name, status,
  831. asid);
  832. }
  833. return 0;
  834. }
  835. DEFINE_SHOW_ATTRIBUTE(tegra_smmu_swgroups);
  836. static int tegra_smmu_clients_show(struct seq_file *s, void *data)
  837. {
  838. struct tegra_smmu *smmu = s->private;
  839. unsigned int i;
  840. u32 value;
  841. seq_printf(s, "client enabled\n");
  842. seq_printf(s, "--------------------\n");
  843. for (i = 0; i < smmu->soc->num_clients; i++) {
  844. const struct tegra_mc_client *client = &smmu->soc->clients[i];
  845. const char *status;
  846. value = smmu_readl(smmu, client->regs.smmu.reg);
  847. if (value & BIT(client->regs.smmu.bit))
  848. status = "yes";
  849. else
  850. status = "no";
  851. seq_printf(s, "%-12s %s\n", client->name, status);
  852. }
  853. return 0;
  854. }
  855. DEFINE_SHOW_ATTRIBUTE(tegra_smmu_clients);
  856. static void tegra_smmu_debugfs_init(struct tegra_smmu *smmu)
  857. {
  858. smmu->debugfs = debugfs_create_dir("smmu", NULL);
  859. debugfs_create_file("swgroups", S_IRUGO, smmu->debugfs, smmu,
  860. &tegra_smmu_swgroups_fops);
  861. debugfs_create_file("clients", S_IRUGO, smmu->debugfs, smmu,
  862. &tegra_smmu_clients_fops);
  863. }
  864. static void tegra_smmu_debugfs_exit(struct tegra_smmu *smmu)
  865. {
  866. debugfs_remove_recursive(smmu->debugfs);
  867. }
  868. struct tegra_smmu *tegra_smmu_probe(struct device *dev,
  869. const struct tegra_smmu_soc *soc,
  870. struct tegra_mc *mc)
  871. {
  872. struct tegra_smmu *smmu;
  873. u32 value;
  874. int err;
  875. smmu = devm_kzalloc(dev, sizeof(*smmu), GFP_KERNEL);
  876. if (!smmu)
  877. return ERR_PTR(-ENOMEM);
  878. /*
  879. * This is a bit of a hack. Ideally we'd want to simply return this
  880. * value. However iommu_device_register() will attempt to add
  881. * all devices to the IOMMU before we get that far. In order
  882. * not to rely on global variables to track the IOMMU instance, we
  883. * set it here so that it can be looked up from the .probe_device()
  884. * callback via the IOMMU device's .drvdata field.
  885. */
  886. mc->smmu = smmu;
  887. smmu->asids = devm_bitmap_zalloc(dev, soc->num_asids, GFP_KERNEL);
  888. if (!smmu->asids)
  889. return ERR_PTR(-ENOMEM);
  890. INIT_LIST_HEAD(&smmu->groups);
  891. mutex_init(&smmu->lock);
  892. smmu->regs = mc->regs;
  893. smmu->soc = soc;
  894. smmu->dev = dev;
  895. smmu->mc = mc;
  896. smmu->pfn_mask =
  897. BIT_MASK(mc->soc->num_address_bits - SMMU_PTE_SHIFT) - 1;
  898. dev_dbg(dev, "address bits: %u, PFN mask: %#lx\n",
  899. mc->soc->num_address_bits, smmu->pfn_mask);
  900. smmu->tlb_mask = (1 << fls(smmu->soc->num_tlb_lines)) - 1;
  901. dev_dbg(dev, "TLB lines: %u, mask: %#lx\n", smmu->soc->num_tlb_lines,
  902. smmu->tlb_mask);
  903. value = SMMU_PTC_CONFIG_ENABLE | SMMU_PTC_CONFIG_INDEX_MAP(0x3f);
  904. if (soc->supports_request_limit)
  905. value |= SMMU_PTC_CONFIG_REQ_LIMIT(8);
  906. smmu_writel(smmu, value, SMMU_PTC_CONFIG);
  907. value = SMMU_TLB_CONFIG_HIT_UNDER_MISS |
  908. SMMU_TLB_CONFIG_ACTIVE_LINES(smmu);
  909. if (soc->supports_round_robin_arbitration)
  910. value |= SMMU_TLB_CONFIG_ROUND_ROBIN_ARBITRATION;
  911. smmu_writel(smmu, value, SMMU_TLB_CONFIG);
  912. smmu_flush_ptc_all(smmu);
  913. smmu_flush_tlb(smmu);
  914. smmu_writel(smmu, SMMU_CONFIG_ENABLE, SMMU_CONFIG);
  915. smmu_flush(smmu);
  916. tegra_smmu_ahb_enable();
  917. err = iommu_device_sysfs_add(&smmu->iommu, dev, NULL, dev_name(dev));
  918. if (err)
  919. return ERR_PTR(err);
  920. err = iommu_device_register(&smmu->iommu, &tegra_smmu_ops, dev);
  921. if (err) {
  922. iommu_device_sysfs_remove(&smmu->iommu);
  923. return ERR_PTR(err);
  924. }
  925. if (IS_ENABLED(CONFIG_DEBUG_FS))
  926. tegra_smmu_debugfs_init(smmu);
  927. return smmu;
  928. }
  929. void tegra_smmu_remove(struct tegra_smmu *smmu)
  930. {
  931. iommu_device_unregister(&smmu->iommu);
  932. iommu_device_sysfs_remove(&smmu->iommu);
  933. if (IS_ENABLED(CONFIG_DEBUG_FS))
  934. tegra_smmu_debugfs_exit(smmu);
  935. }