tegra-smmu.c 25 KB

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