tegra-gart.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IOMMU API for Graphics Address Relocation Table on Tegra20
  4. *
  5. * Copyright (c) 2010-2012, NVIDIA CORPORATION. All rights reserved.
  6. *
  7. * Author: Hiroshi DOYU <[email protected]>
  8. */
  9. #define dev_fmt(fmt) "gart: " fmt
  10. #include <linux/io.h>
  11. #include <linux/iommu.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/vmalloc.h>
  17. #include <soc/tegra/mc.h>
  18. #define GART_REG_BASE 0x24
  19. #define GART_CONFIG (0x24 - GART_REG_BASE)
  20. #define GART_ENTRY_ADDR (0x28 - GART_REG_BASE)
  21. #define GART_ENTRY_DATA (0x2c - GART_REG_BASE)
  22. #define GART_ENTRY_PHYS_ADDR_VALID BIT(31)
  23. #define GART_PAGE_SHIFT 12
  24. #define GART_PAGE_SIZE (1 << GART_PAGE_SHIFT)
  25. #define GART_PAGE_MASK GENMASK(30, GART_PAGE_SHIFT)
  26. /* bitmap of the page sizes currently supported */
  27. #define GART_IOMMU_PGSIZES (GART_PAGE_SIZE)
  28. struct gart_device {
  29. void __iomem *regs;
  30. u32 *savedata;
  31. unsigned long iovmm_base; /* offset to vmm_area start */
  32. unsigned long iovmm_end; /* offset to vmm_area end */
  33. spinlock_t pte_lock; /* for pagetable */
  34. spinlock_t dom_lock; /* for active domain */
  35. unsigned int active_devices; /* number of active devices */
  36. struct iommu_domain *active_domain; /* current active domain */
  37. struct iommu_device iommu; /* IOMMU Core handle */
  38. struct device *dev;
  39. };
  40. static struct gart_device *gart_handle; /* unique for a system */
  41. static bool gart_debug;
  42. /*
  43. * Any interaction between any block on PPSB and a block on APB or AHB
  44. * must have these read-back to ensure the APB/AHB bus transaction is
  45. * complete before initiating activity on the PPSB block.
  46. */
  47. #define FLUSH_GART_REGS(gart) readl_relaxed((gart)->regs + GART_CONFIG)
  48. #define for_each_gart_pte(gart, iova) \
  49. for (iova = gart->iovmm_base; \
  50. iova < gart->iovmm_end; \
  51. iova += GART_PAGE_SIZE)
  52. static inline void gart_set_pte(struct gart_device *gart,
  53. unsigned long iova, unsigned long pte)
  54. {
  55. writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR);
  56. writel_relaxed(pte, gart->regs + GART_ENTRY_DATA);
  57. }
  58. static inline unsigned long gart_read_pte(struct gart_device *gart,
  59. unsigned long iova)
  60. {
  61. unsigned long pte;
  62. writel_relaxed(iova, gart->regs + GART_ENTRY_ADDR);
  63. pte = readl_relaxed(gart->regs + GART_ENTRY_DATA);
  64. return pte;
  65. }
  66. static void do_gart_setup(struct gart_device *gart, const u32 *data)
  67. {
  68. unsigned long iova;
  69. for_each_gart_pte(gart, iova)
  70. gart_set_pte(gart, iova, data ? *(data++) : 0);
  71. writel_relaxed(1, gart->regs + GART_CONFIG);
  72. FLUSH_GART_REGS(gart);
  73. }
  74. static inline bool gart_iova_range_invalid(struct gart_device *gart,
  75. unsigned long iova, size_t bytes)
  76. {
  77. return unlikely(iova < gart->iovmm_base || bytes != GART_PAGE_SIZE ||
  78. iova + bytes > gart->iovmm_end);
  79. }
  80. static inline bool gart_pte_valid(struct gart_device *gart, unsigned long iova)
  81. {
  82. return !!(gart_read_pte(gart, iova) & GART_ENTRY_PHYS_ADDR_VALID);
  83. }
  84. static int gart_iommu_attach_dev(struct iommu_domain *domain,
  85. struct device *dev)
  86. {
  87. struct gart_device *gart = gart_handle;
  88. int ret = 0;
  89. spin_lock(&gart->dom_lock);
  90. if (gart->active_domain && gart->active_domain != domain) {
  91. ret = -EBUSY;
  92. } else if (dev_iommu_priv_get(dev) != domain) {
  93. dev_iommu_priv_set(dev, domain);
  94. gart->active_domain = domain;
  95. gart->active_devices++;
  96. }
  97. spin_unlock(&gart->dom_lock);
  98. return ret;
  99. }
  100. static void gart_iommu_detach_dev(struct iommu_domain *domain,
  101. struct device *dev)
  102. {
  103. struct gart_device *gart = gart_handle;
  104. spin_lock(&gart->dom_lock);
  105. if (dev_iommu_priv_get(dev) == domain) {
  106. dev_iommu_priv_set(dev, NULL);
  107. if (--gart->active_devices == 0)
  108. gart->active_domain = NULL;
  109. }
  110. spin_unlock(&gart->dom_lock);
  111. }
  112. static struct iommu_domain *gart_iommu_domain_alloc(unsigned type)
  113. {
  114. struct iommu_domain *domain;
  115. if (type != IOMMU_DOMAIN_UNMANAGED)
  116. return NULL;
  117. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  118. if (domain) {
  119. domain->geometry.aperture_start = gart_handle->iovmm_base;
  120. domain->geometry.aperture_end = gart_handle->iovmm_end - 1;
  121. domain->geometry.force_aperture = true;
  122. }
  123. return domain;
  124. }
  125. static void gart_iommu_domain_free(struct iommu_domain *domain)
  126. {
  127. WARN_ON(gart_handle->active_domain == domain);
  128. kfree(domain);
  129. }
  130. static inline int __gart_iommu_map(struct gart_device *gart, unsigned long iova,
  131. unsigned long pa)
  132. {
  133. if (unlikely(gart_debug && gart_pte_valid(gart, iova))) {
  134. dev_err(gart->dev, "Page entry is in-use\n");
  135. return -EINVAL;
  136. }
  137. gart_set_pte(gart, iova, GART_ENTRY_PHYS_ADDR_VALID | pa);
  138. return 0;
  139. }
  140. static int gart_iommu_map(struct iommu_domain *domain, unsigned long iova,
  141. phys_addr_t pa, size_t bytes, int prot, gfp_t gfp)
  142. {
  143. struct gart_device *gart = gart_handle;
  144. int ret;
  145. if (gart_iova_range_invalid(gart, iova, bytes))
  146. return -EINVAL;
  147. spin_lock(&gart->pte_lock);
  148. ret = __gart_iommu_map(gart, iova, (unsigned long)pa);
  149. spin_unlock(&gart->pte_lock);
  150. return ret;
  151. }
  152. static inline int __gart_iommu_unmap(struct gart_device *gart,
  153. unsigned long iova)
  154. {
  155. if (unlikely(gart_debug && !gart_pte_valid(gart, iova))) {
  156. dev_err(gart->dev, "Page entry is invalid\n");
  157. return -EINVAL;
  158. }
  159. gart_set_pte(gart, iova, 0);
  160. return 0;
  161. }
  162. static size_t gart_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
  163. size_t bytes, struct iommu_iotlb_gather *gather)
  164. {
  165. struct gart_device *gart = gart_handle;
  166. int err;
  167. if (gart_iova_range_invalid(gart, iova, bytes))
  168. return 0;
  169. spin_lock(&gart->pte_lock);
  170. err = __gart_iommu_unmap(gart, iova);
  171. spin_unlock(&gart->pte_lock);
  172. return err ? 0 : bytes;
  173. }
  174. static phys_addr_t gart_iommu_iova_to_phys(struct iommu_domain *domain,
  175. dma_addr_t iova)
  176. {
  177. struct gart_device *gart = gart_handle;
  178. unsigned long pte;
  179. if (gart_iova_range_invalid(gart, iova, GART_PAGE_SIZE))
  180. return -EINVAL;
  181. spin_lock(&gart->pte_lock);
  182. pte = gart_read_pte(gart, iova);
  183. spin_unlock(&gart->pte_lock);
  184. return pte & GART_PAGE_MASK;
  185. }
  186. static struct iommu_device *gart_iommu_probe_device(struct device *dev)
  187. {
  188. if (!dev_iommu_fwspec_get(dev))
  189. return ERR_PTR(-ENODEV);
  190. return &gart_handle->iommu;
  191. }
  192. static int gart_iommu_of_xlate(struct device *dev,
  193. struct of_phandle_args *args)
  194. {
  195. return 0;
  196. }
  197. static void gart_iommu_sync_map(struct iommu_domain *domain, unsigned long iova,
  198. size_t size)
  199. {
  200. FLUSH_GART_REGS(gart_handle);
  201. }
  202. static void gart_iommu_sync(struct iommu_domain *domain,
  203. struct iommu_iotlb_gather *gather)
  204. {
  205. size_t length = gather->end - gather->start + 1;
  206. gart_iommu_sync_map(domain, gather->start, length);
  207. }
  208. static const struct iommu_ops gart_iommu_ops = {
  209. .domain_alloc = gart_iommu_domain_alloc,
  210. .probe_device = gart_iommu_probe_device,
  211. .device_group = generic_device_group,
  212. .pgsize_bitmap = GART_IOMMU_PGSIZES,
  213. .of_xlate = gart_iommu_of_xlate,
  214. .default_domain_ops = &(const struct iommu_domain_ops) {
  215. .attach_dev = gart_iommu_attach_dev,
  216. .detach_dev = gart_iommu_detach_dev,
  217. .map = gart_iommu_map,
  218. .unmap = gart_iommu_unmap,
  219. .iova_to_phys = gart_iommu_iova_to_phys,
  220. .iotlb_sync_map = gart_iommu_sync_map,
  221. .iotlb_sync = gart_iommu_sync,
  222. .free = gart_iommu_domain_free,
  223. }
  224. };
  225. int tegra_gart_suspend(struct gart_device *gart)
  226. {
  227. u32 *data = gart->savedata;
  228. unsigned long iova;
  229. /*
  230. * All GART users shall be suspended at this point. Disable
  231. * address translation to trap all GART accesses as invalid
  232. * memory accesses.
  233. */
  234. writel_relaxed(0, gart->regs + GART_CONFIG);
  235. FLUSH_GART_REGS(gart);
  236. for_each_gart_pte(gart, iova)
  237. *(data++) = gart_read_pte(gart, iova);
  238. return 0;
  239. }
  240. int tegra_gart_resume(struct gart_device *gart)
  241. {
  242. do_gart_setup(gart, gart->savedata);
  243. return 0;
  244. }
  245. struct gart_device *tegra_gart_probe(struct device *dev, struct tegra_mc *mc)
  246. {
  247. struct gart_device *gart;
  248. struct resource *res;
  249. int err;
  250. BUILD_BUG_ON(PAGE_SHIFT != GART_PAGE_SHIFT);
  251. /* the GART memory aperture is required */
  252. res = platform_get_resource(to_platform_device(dev), IORESOURCE_MEM, 1);
  253. if (!res) {
  254. dev_err(dev, "Memory aperture resource unavailable\n");
  255. return ERR_PTR(-ENXIO);
  256. }
  257. gart = kzalloc(sizeof(*gart), GFP_KERNEL);
  258. if (!gart)
  259. return ERR_PTR(-ENOMEM);
  260. gart_handle = gart;
  261. gart->dev = dev;
  262. gart->regs = mc->regs + GART_REG_BASE;
  263. gart->iovmm_base = res->start;
  264. gart->iovmm_end = res->end + 1;
  265. spin_lock_init(&gart->pte_lock);
  266. spin_lock_init(&gart->dom_lock);
  267. do_gart_setup(gart, NULL);
  268. err = iommu_device_sysfs_add(&gart->iommu, dev, NULL, "gart");
  269. if (err)
  270. goto free_gart;
  271. err = iommu_device_register(&gart->iommu, &gart_iommu_ops, dev);
  272. if (err)
  273. goto remove_sysfs;
  274. gart->savedata = vmalloc(resource_size(res) / GART_PAGE_SIZE *
  275. sizeof(u32));
  276. if (!gart->savedata) {
  277. err = -ENOMEM;
  278. goto unregister_iommu;
  279. }
  280. return gart;
  281. unregister_iommu:
  282. iommu_device_unregister(&gart->iommu);
  283. remove_sysfs:
  284. iommu_device_sysfs_remove(&gart->iommu);
  285. free_gart:
  286. kfree(gart);
  287. return ERR_PTR(err);
  288. }
  289. module_param(gart_debug, bool, 0644);
  290. MODULE_PARM_DESC(gart_debug, "Enable GART debugging");