tegra-smmu.c 22 KB

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