tegra-smmu.c 27 KB

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