tegra-smmu.c 22 KB

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