sprd-iommu.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Unisoc IOMMU driver
  4. *
  5. * Copyright (C) 2020 Unisoc, Inc.
  6. * Author: Chunyan Zhang <[email protected]>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/device.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/errno.h>
  12. #include <linux/iommu.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/regmap.h>
  17. #include <linux/slab.h>
  18. #define SPRD_IOMMU_PAGE_SHIFT 12
  19. #define SPRD_IOMMU_PAGE_SIZE SZ_4K
  20. #define SPRD_EX_CFG 0x0
  21. #define SPRD_IOMMU_VAOR_BYPASS BIT(4)
  22. #define SPRD_IOMMU_GATE_EN BIT(1)
  23. #define SPRD_IOMMU_EN BIT(0)
  24. #define SPRD_EX_UPDATE 0x4
  25. #define SPRD_EX_FIRST_VPN 0x8
  26. #define SPRD_EX_VPN_RANGE 0xc
  27. #define SPRD_EX_FIRST_PPN 0x10
  28. #define SPRD_EX_DEFAULT_PPN 0x14
  29. #define SPRD_IOMMU_VERSION 0x0
  30. #define SPRD_VERSION_MASK GENMASK(15, 8)
  31. #define SPRD_VERSION_SHIFT 0x8
  32. #define SPRD_VAU_CFG 0x4
  33. #define SPRD_VAU_UPDATE 0x8
  34. #define SPRD_VAU_AUTH_CFG 0xc
  35. #define SPRD_VAU_FIRST_PPN 0x10
  36. #define SPRD_VAU_DEFAULT_PPN_RD 0x14
  37. #define SPRD_VAU_DEFAULT_PPN_WR 0x18
  38. #define SPRD_VAU_FIRST_VPN 0x1c
  39. #define SPRD_VAU_VPN_RANGE 0x20
  40. enum sprd_iommu_version {
  41. SPRD_IOMMU_EX,
  42. SPRD_IOMMU_VAU,
  43. };
  44. /*
  45. * struct sprd_iommu_device - high-level sprd IOMMU device representation,
  46. * including hardware information and configuration, also driver data, etc
  47. *
  48. * @ver: sprd IOMMU IP version
  49. * @prot_page_va: protect page base virtual address
  50. * @prot_page_pa: protect page base physical address, data would be
  51. * written to here while translation fault
  52. * @base: mapped base address for accessing registers
  53. * @dev: pointer to basic device structure
  54. * @iommu: IOMMU core representation
  55. * @group: IOMMU group
  56. * @eb: gate clock which controls IOMMU access
  57. */
  58. struct sprd_iommu_device {
  59. enum sprd_iommu_version ver;
  60. u32 *prot_page_va;
  61. dma_addr_t prot_page_pa;
  62. void __iomem *base;
  63. struct device *dev;
  64. struct iommu_device iommu;
  65. struct iommu_group *group;
  66. struct clk *eb;
  67. };
  68. struct sprd_iommu_domain {
  69. spinlock_t pgtlock; /* lock for page table */
  70. struct iommu_domain domain;
  71. u32 *pgt_va; /* page table virtual address base */
  72. dma_addr_t pgt_pa; /* page table physical address base */
  73. struct sprd_iommu_device *sdev;
  74. };
  75. static const struct iommu_ops sprd_iommu_ops;
  76. static struct sprd_iommu_domain *to_sprd_domain(struct iommu_domain *dom)
  77. {
  78. return container_of(dom, struct sprd_iommu_domain, domain);
  79. }
  80. static inline void
  81. sprd_iommu_write(struct sprd_iommu_device *sdev, unsigned int reg, u32 val)
  82. {
  83. writel_relaxed(val, sdev->base + reg);
  84. }
  85. static inline u32
  86. sprd_iommu_read(struct sprd_iommu_device *sdev, unsigned int reg)
  87. {
  88. return readl_relaxed(sdev->base + reg);
  89. }
  90. static inline void
  91. sprd_iommu_update_bits(struct sprd_iommu_device *sdev, unsigned int reg,
  92. u32 mask, u32 shift, u32 val)
  93. {
  94. u32 t = sprd_iommu_read(sdev, reg);
  95. t = (t & (~(mask << shift))) | ((val & mask) << shift);
  96. sprd_iommu_write(sdev, reg, t);
  97. }
  98. static inline int
  99. sprd_iommu_get_version(struct sprd_iommu_device *sdev)
  100. {
  101. int ver = (sprd_iommu_read(sdev, SPRD_IOMMU_VERSION) &
  102. SPRD_VERSION_MASK) >> SPRD_VERSION_SHIFT;
  103. switch (ver) {
  104. case SPRD_IOMMU_EX:
  105. case SPRD_IOMMU_VAU:
  106. return ver;
  107. default:
  108. return -EINVAL;
  109. }
  110. }
  111. static size_t
  112. sprd_iommu_pgt_size(struct iommu_domain *domain)
  113. {
  114. return ((domain->geometry.aperture_end -
  115. domain->geometry.aperture_start + 1) >>
  116. SPRD_IOMMU_PAGE_SHIFT) * sizeof(u32);
  117. }
  118. static struct iommu_domain *sprd_iommu_domain_alloc(unsigned int domain_type)
  119. {
  120. struct sprd_iommu_domain *dom;
  121. if (domain_type != IOMMU_DOMAIN_DMA && domain_type != IOMMU_DOMAIN_UNMANAGED)
  122. return NULL;
  123. dom = kzalloc(sizeof(*dom), GFP_KERNEL);
  124. if (!dom)
  125. return NULL;
  126. spin_lock_init(&dom->pgtlock);
  127. dom->domain.geometry.aperture_start = 0;
  128. dom->domain.geometry.aperture_end = SZ_256M - 1;
  129. dom->domain.geometry.force_aperture = true;
  130. return &dom->domain;
  131. }
  132. static void sprd_iommu_domain_free(struct iommu_domain *domain)
  133. {
  134. struct sprd_iommu_domain *dom = to_sprd_domain(domain);
  135. kfree(dom);
  136. }
  137. static void sprd_iommu_first_vpn(struct sprd_iommu_domain *dom)
  138. {
  139. struct sprd_iommu_device *sdev = dom->sdev;
  140. u32 val;
  141. unsigned int reg;
  142. if (sdev->ver == SPRD_IOMMU_EX)
  143. reg = SPRD_EX_FIRST_VPN;
  144. else
  145. reg = SPRD_VAU_FIRST_VPN;
  146. val = dom->domain.geometry.aperture_start >> SPRD_IOMMU_PAGE_SHIFT;
  147. sprd_iommu_write(sdev, reg, val);
  148. }
  149. static void sprd_iommu_vpn_range(struct sprd_iommu_domain *dom)
  150. {
  151. struct sprd_iommu_device *sdev = dom->sdev;
  152. u32 val;
  153. unsigned int reg;
  154. if (sdev->ver == SPRD_IOMMU_EX)
  155. reg = SPRD_EX_VPN_RANGE;
  156. else
  157. reg = SPRD_VAU_VPN_RANGE;
  158. val = (dom->domain.geometry.aperture_end -
  159. dom->domain.geometry.aperture_start) >> SPRD_IOMMU_PAGE_SHIFT;
  160. sprd_iommu_write(sdev, reg, val);
  161. }
  162. static void sprd_iommu_first_ppn(struct sprd_iommu_domain *dom)
  163. {
  164. u32 val = dom->pgt_pa >> SPRD_IOMMU_PAGE_SHIFT;
  165. struct sprd_iommu_device *sdev = dom->sdev;
  166. unsigned int reg;
  167. if (sdev->ver == SPRD_IOMMU_EX)
  168. reg = SPRD_EX_FIRST_PPN;
  169. else
  170. reg = SPRD_VAU_FIRST_PPN;
  171. sprd_iommu_write(sdev, reg, val);
  172. }
  173. static void sprd_iommu_default_ppn(struct sprd_iommu_device *sdev)
  174. {
  175. u32 val = sdev->prot_page_pa >> SPRD_IOMMU_PAGE_SHIFT;
  176. if (sdev->ver == SPRD_IOMMU_EX) {
  177. sprd_iommu_write(sdev, SPRD_EX_DEFAULT_PPN, val);
  178. } else if (sdev->ver == SPRD_IOMMU_VAU) {
  179. sprd_iommu_write(sdev, SPRD_VAU_DEFAULT_PPN_RD, val);
  180. sprd_iommu_write(sdev, SPRD_VAU_DEFAULT_PPN_WR, val);
  181. }
  182. }
  183. static void sprd_iommu_hw_en(struct sprd_iommu_device *sdev, bool en)
  184. {
  185. unsigned int reg_cfg;
  186. u32 mask, val;
  187. if (sdev->ver == SPRD_IOMMU_EX)
  188. reg_cfg = SPRD_EX_CFG;
  189. else
  190. reg_cfg = SPRD_VAU_CFG;
  191. mask = SPRD_IOMMU_EN | SPRD_IOMMU_GATE_EN;
  192. val = en ? mask : 0;
  193. sprd_iommu_update_bits(sdev, reg_cfg, mask, 0, val);
  194. }
  195. static int sprd_iommu_attach_device(struct iommu_domain *domain,
  196. struct device *dev)
  197. {
  198. struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev);
  199. struct sprd_iommu_domain *dom = to_sprd_domain(domain);
  200. size_t pgt_size = sprd_iommu_pgt_size(domain);
  201. if (dom->sdev) {
  202. pr_err("There's already a device attached to this domain.\n");
  203. return -EINVAL;
  204. }
  205. dom->pgt_va = dma_alloc_coherent(sdev->dev, pgt_size, &dom->pgt_pa, GFP_KERNEL);
  206. if (!dom->pgt_va)
  207. return -ENOMEM;
  208. dom->sdev = sdev;
  209. sprd_iommu_first_ppn(dom);
  210. sprd_iommu_first_vpn(dom);
  211. sprd_iommu_vpn_range(dom);
  212. sprd_iommu_default_ppn(sdev);
  213. sprd_iommu_hw_en(sdev, true);
  214. return 0;
  215. }
  216. static void sprd_iommu_detach_device(struct iommu_domain *domain,
  217. struct device *dev)
  218. {
  219. struct sprd_iommu_domain *dom = to_sprd_domain(domain);
  220. struct sprd_iommu_device *sdev = dom->sdev;
  221. size_t pgt_size = sprd_iommu_pgt_size(domain);
  222. if (!sdev)
  223. return;
  224. dma_free_coherent(sdev->dev, pgt_size, dom->pgt_va, dom->pgt_pa);
  225. sprd_iommu_hw_en(sdev, false);
  226. dom->sdev = NULL;
  227. }
  228. static int sprd_iommu_map(struct iommu_domain *domain, unsigned long iova,
  229. phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
  230. {
  231. struct sprd_iommu_domain *dom = to_sprd_domain(domain);
  232. unsigned int page_num = size >> SPRD_IOMMU_PAGE_SHIFT;
  233. unsigned long flags;
  234. unsigned int i;
  235. u32 *pgt_base_iova;
  236. u32 pabase = (u32)paddr;
  237. unsigned long start = domain->geometry.aperture_start;
  238. unsigned long end = domain->geometry.aperture_end;
  239. if (!dom->sdev) {
  240. pr_err("No sprd_iommu_device attached to the domain\n");
  241. return -EINVAL;
  242. }
  243. if (iova < start || (iova + size) > (end + 1)) {
  244. dev_err(dom->sdev->dev, "(iova(0x%lx) + size(%zx)) are not in the range!\n",
  245. iova, size);
  246. return -EINVAL;
  247. }
  248. pgt_base_iova = dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT);
  249. spin_lock_irqsave(&dom->pgtlock, flags);
  250. for (i = 0; i < page_num; i++) {
  251. pgt_base_iova[i] = pabase >> SPRD_IOMMU_PAGE_SHIFT;
  252. pabase += SPRD_IOMMU_PAGE_SIZE;
  253. }
  254. spin_unlock_irqrestore(&dom->pgtlock, flags);
  255. return 0;
  256. }
  257. static size_t sprd_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  258. size_t size, struct iommu_iotlb_gather *iotlb_gather)
  259. {
  260. struct sprd_iommu_domain *dom = to_sprd_domain(domain);
  261. unsigned long flags;
  262. u32 *pgt_base_iova;
  263. unsigned int page_num = size >> SPRD_IOMMU_PAGE_SHIFT;
  264. unsigned long start = domain->geometry.aperture_start;
  265. unsigned long end = domain->geometry.aperture_end;
  266. if (iova < start || (iova + size) > (end + 1))
  267. return -EINVAL;
  268. pgt_base_iova = dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT);
  269. spin_lock_irqsave(&dom->pgtlock, flags);
  270. memset(pgt_base_iova, 0, page_num * sizeof(u32));
  271. spin_unlock_irqrestore(&dom->pgtlock, flags);
  272. return 0;
  273. }
  274. static void sprd_iommu_sync_map(struct iommu_domain *domain,
  275. unsigned long iova, size_t size)
  276. {
  277. struct sprd_iommu_domain *dom = to_sprd_domain(domain);
  278. unsigned int reg;
  279. if (dom->sdev->ver == SPRD_IOMMU_EX)
  280. reg = SPRD_EX_UPDATE;
  281. else
  282. reg = SPRD_VAU_UPDATE;
  283. /* clear IOMMU TLB buffer after page table updated */
  284. sprd_iommu_write(dom->sdev, reg, 0xffffffff);
  285. }
  286. static void sprd_iommu_sync(struct iommu_domain *domain,
  287. struct iommu_iotlb_gather *iotlb_gather)
  288. {
  289. sprd_iommu_sync_map(domain, 0, 0);
  290. }
  291. static phys_addr_t sprd_iommu_iova_to_phys(struct iommu_domain *domain,
  292. dma_addr_t iova)
  293. {
  294. struct sprd_iommu_domain *dom = to_sprd_domain(domain);
  295. unsigned long flags;
  296. phys_addr_t pa;
  297. unsigned long start = domain->geometry.aperture_start;
  298. unsigned long end = domain->geometry.aperture_end;
  299. if (WARN_ON(iova < start || iova > end))
  300. return 0;
  301. spin_lock_irqsave(&dom->pgtlock, flags);
  302. pa = *(dom->pgt_va + ((iova - start) >> SPRD_IOMMU_PAGE_SHIFT));
  303. pa = (pa << SPRD_IOMMU_PAGE_SHIFT) + ((iova - start) & (SPRD_IOMMU_PAGE_SIZE - 1));
  304. spin_unlock_irqrestore(&dom->pgtlock, flags);
  305. return pa;
  306. }
  307. static struct iommu_device *sprd_iommu_probe_device(struct device *dev)
  308. {
  309. struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
  310. struct sprd_iommu_device *sdev;
  311. if (!fwspec || fwspec->ops != &sprd_iommu_ops)
  312. return ERR_PTR(-ENODEV);
  313. sdev = dev_iommu_priv_get(dev);
  314. return &sdev->iommu;
  315. }
  316. static struct iommu_group *sprd_iommu_device_group(struct device *dev)
  317. {
  318. struct sprd_iommu_device *sdev = dev_iommu_priv_get(dev);
  319. return iommu_group_ref_get(sdev->group);
  320. }
  321. static int sprd_iommu_of_xlate(struct device *dev, struct of_phandle_args *args)
  322. {
  323. struct platform_device *pdev;
  324. if (!dev_iommu_priv_get(dev)) {
  325. pdev = of_find_device_by_node(args->np);
  326. dev_iommu_priv_set(dev, platform_get_drvdata(pdev));
  327. platform_device_put(pdev);
  328. }
  329. return 0;
  330. }
  331. static const struct iommu_ops sprd_iommu_ops = {
  332. .domain_alloc = sprd_iommu_domain_alloc,
  333. .probe_device = sprd_iommu_probe_device,
  334. .device_group = sprd_iommu_device_group,
  335. .of_xlate = sprd_iommu_of_xlate,
  336. .pgsize_bitmap = ~0UL << SPRD_IOMMU_PAGE_SHIFT,
  337. .owner = THIS_MODULE,
  338. .default_domain_ops = &(const struct iommu_domain_ops) {
  339. .attach_dev = sprd_iommu_attach_device,
  340. .detach_dev = sprd_iommu_detach_device,
  341. .map = sprd_iommu_map,
  342. .unmap = sprd_iommu_unmap,
  343. .iotlb_sync_map = sprd_iommu_sync_map,
  344. .iotlb_sync = sprd_iommu_sync,
  345. .iova_to_phys = sprd_iommu_iova_to_phys,
  346. .free = sprd_iommu_domain_free,
  347. }
  348. };
  349. static const struct of_device_id sprd_iommu_of_match[] = {
  350. { .compatible = "sprd,iommu-v1" },
  351. { },
  352. };
  353. MODULE_DEVICE_TABLE(of, sprd_iommu_of_match);
  354. /*
  355. * Clock is not required, access to some of IOMMUs is controlled by gate
  356. * clk, enabled clocks for that kind of IOMMUs before accessing.
  357. * Return 0 for success or no clocks found.
  358. */
  359. static int sprd_iommu_clk_enable(struct sprd_iommu_device *sdev)
  360. {
  361. struct clk *eb;
  362. eb = devm_clk_get_optional(sdev->dev, NULL);
  363. if (!eb)
  364. return 0;
  365. if (IS_ERR(eb))
  366. return PTR_ERR(eb);
  367. sdev->eb = eb;
  368. return clk_prepare_enable(eb);
  369. }
  370. static void sprd_iommu_clk_disable(struct sprd_iommu_device *sdev)
  371. {
  372. if (sdev->eb)
  373. clk_disable_unprepare(sdev->eb);
  374. }
  375. static int sprd_iommu_probe(struct platform_device *pdev)
  376. {
  377. struct sprd_iommu_device *sdev;
  378. struct device *dev = &pdev->dev;
  379. void __iomem *base;
  380. int ret;
  381. sdev = devm_kzalloc(dev, sizeof(*sdev), GFP_KERNEL);
  382. if (!sdev)
  383. return -ENOMEM;
  384. base = devm_platform_ioremap_resource(pdev, 0);
  385. if (IS_ERR(base)) {
  386. dev_err(dev, "Failed to get ioremap resource.\n");
  387. return PTR_ERR(base);
  388. }
  389. sdev->base = base;
  390. sdev->prot_page_va = dma_alloc_coherent(dev, SPRD_IOMMU_PAGE_SIZE,
  391. &sdev->prot_page_pa, GFP_KERNEL);
  392. if (!sdev->prot_page_va)
  393. return -ENOMEM;
  394. platform_set_drvdata(pdev, sdev);
  395. sdev->dev = dev;
  396. /* All the client devices are in the same iommu-group */
  397. sdev->group = iommu_group_alloc();
  398. if (IS_ERR(sdev->group)) {
  399. ret = PTR_ERR(sdev->group);
  400. goto free_page;
  401. }
  402. ret = iommu_device_sysfs_add(&sdev->iommu, dev, NULL, dev_name(dev));
  403. if (ret)
  404. goto put_group;
  405. ret = iommu_device_register(&sdev->iommu, &sprd_iommu_ops, dev);
  406. if (ret)
  407. goto remove_sysfs;
  408. ret = sprd_iommu_clk_enable(sdev);
  409. if (ret)
  410. goto unregister_iommu;
  411. ret = sprd_iommu_get_version(sdev);
  412. if (ret < 0) {
  413. dev_err(dev, "IOMMU version(%d) is invalid.\n", ret);
  414. goto disable_clk;
  415. }
  416. sdev->ver = ret;
  417. return 0;
  418. disable_clk:
  419. sprd_iommu_clk_disable(sdev);
  420. unregister_iommu:
  421. iommu_device_unregister(&sdev->iommu);
  422. remove_sysfs:
  423. iommu_device_sysfs_remove(&sdev->iommu);
  424. put_group:
  425. iommu_group_put(sdev->group);
  426. free_page:
  427. dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa);
  428. return ret;
  429. }
  430. static int sprd_iommu_remove(struct platform_device *pdev)
  431. {
  432. struct sprd_iommu_device *sdev = platform_get_drvdata(pdev);
  433. dma_free_coherent(sdev->dev, SPRD_IOMMU_PAGE_SIZE, sdev->prot_page_va, sdev->prot_page_pa);
  434. iommu_group_put(sdev->group);
  435. sdev->group = NULL;
  436. platform_set_drvdata(pdev, NULL);
  437. iommu_device_sysfs_remove(&sdev->iommu);
  438. iommu_device_unregister(&sdev->iommu);
  439. return 0;
  440. }
  441. static struct platform_driver sprd_iommu_driver = {
  442. .driver = {
  443. .name = "sprd-iommu",
  444. .of_match_table = sprd_iommu_of_match,
  445. .suppress_bind_attrs = true,
  446. },
  447. .probe = sprd_iommu_probe,
  448. .remove = sprd_iommu_remove,
  449. };
  450. module_platform_driver(sprd_iommu_driver);
  451. MODULE_DESCRIPTION("IOMMU driver for Unisoc SoCs");
  452. MODULE_ALIAS("platform:sprd-iommu");
  453. MODULE_LICENSE("GPL");