tegra-smmu.c 28 KB

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