tegra-smmu.c 22 KB

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