ioport.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ioport.c: Simple io mapping allocator.
  4. *
  5. * Copyright (C) 1995 David S. Miller ([email protected])
  6. * Copyright (C) 1995 Miguel de Icaza ([email protected])
  7. *
  8. * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
  9. *
  10. * 2000/01/29
  11. * <rth> zait: as long as pci_alloc_consistent produces something addressable,
  12. * things are ok.
  13. * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
  14. * pointer into the big page mapping
  15. * <rth> zait: so what?
  16. * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
  17. * <zaitcev> Hmm
  18. * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
  19. * So far so good.
  20. * <zaitcev> Now, driver calls pci_free_consistent(with result of
  21. * remap_it_my_way()).
  22. * <zaitcev> How do you find the address to pass to free_pages()?
  23. * <rth> zait: walk the page tables? It's only two or three level after all.
  24. * <rth> zait: you have to walk them anyway to remove the mapping.
  25. * <zaitcev> Hmm
  26. * <zaitcev> Sounds reasonable
  27. */
  28. #include <linux/module.h>
  29. #include <linux/sched.h>
  30. #include <linux/kernel.h>
  31. #include <linux/errno.h>
  32. #include <linux/types.h>
  33. #include <linux/ioport.h>
  34. #include <linux/mm.h>
  35. #include <linux/slab.h>
  36. #include <linux/pci.h> /* struct pci_dev */
  37. #include <linux/proc_fs.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/scatterlist.h>
  40. #include <linux/dma-map-ops.h>
  41. #include <linux/of_device.h>
  42. #include <asm/io.h>
  43. #include <asm/vaddrs.h>
  44. #include <asm/oplib.h>
  45. #include <asm/prom.h>
  46. #include <asm/page.h>
  47. #include <asm/pgalloc.h>
  48. #include <asm/dma.h>
  49. #include <asm/iommu.h>
  50. #include <asm/io-unit.h>
  51. #include <asm/leon.h>
  52. static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
  53. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  54. unsigned long size, char *name);
  55. static void _sparc_free_io(struct resource *res);
  56. static void register_proc_sparc_ioport(void);
  57. /* This points to the next to use virtual memory for DVMA mappings */
  58. static struct resource _sparc_dvma = {
  59. .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
  60. };
  61. /* This points to the start of I/O mappings, cluable from outside. */
  62. /*ext*/ struct resource sparc_iomap = {
  63. .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
  64. };
  65. /*
  66. * Our mini-allocator...
  67. * Boy this is gross! We need it because we must map I/O for
  68. * timers and interrupt controller before the kmalloc is available.
  69. */
  70. #define XNMLN 15
  71. #define XNRES 10 /* SS-10 uses 8 */
  72. struct xresource {
  73. struct resource xres; /* Must be first */
  74. int xflag; /* 1 == used */
  75. char xname[XNMLN+1];
  76. };
  77. static struct xresource xresv[XNRES];
  78. static struct xresource *xres_alloc(void) {
  79. struct xresource *xrp;
  80. int n;
  81. xrp = xresv;
  82. for (n = 0; n < XNRES; n++) {
  83. if (xrp->xflag == 0) {
  84. xrp->xflag = 1;
  85. return xrp;
  86. }
  87. xrp++;
  88. }
  89. return NULL;
  90. }
  91. static void xres_free(struct xresource *xrp) {
  92. xrp->xflag = 0;
  93. }
  94. /*
  95. * These are typically used in PCI drivers
  96. * which are trying to be cross-platform.
  97. *
  98. * Bus type is always zero on IIep.
  99. */
  100. void __iomem *ioremap(phys_addr_t offset, size_t size)
  101. {
  102. char name[14];
  103. sprintf(name, "phys_%08x", (u32)offset);
  104. return _sparc_alloc_io(0, (unsigned long)offset, size, name);
  105. }
  106. EXPORT_SYMBOL(ioremap);
  107. /*
  108. * Complementary to ioremap().
  109. */
  110. void iounmap(volatile void __iomem *virtual)
  111. {
  112. unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
  113. struct resource *res;
  114. /*
  115. * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
  116. * This probably warrants some sort of hashing.
  117. */
  118. if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) {
  119. printk("free_io/iounmap: cannot free %lx\n", vaddr);
  120. return;
  121. }
  122. _sparc_free_io(res);
  123. if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
  124. xres_free((struct xresource *)res);
  125. } else {
  126. kfree(res);
  127. }
  128. }
  129. EXPORT_SYMBOL(iounmap);
  130. void __iomem *of_ioremap(struct resource *res, unsigned long offset,
  131. unsigned long size, char *name)
  132. {
  133. return _sparc_alloc_io(res->flags & 0xF,
  134. res->start + offset,
  135. size, name);
  136. }
  137. EXPORT_SYMBOL(of_ioremap);
  138. void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
  139. {
  140. iounmap(base);
  141. }
  142. EXPORT_SYMBOL(of_iounmap);
  143. /*
  144. * Meat of mapping
  145. */
  146. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  147. unsigned long size, char *name)
  148. {
  149. static int printed_full;
  150. struct xresource *xres;
  151. struct resource *res;
  152. char *tack;
  153. int tlen;
  154. void __iomem *va; /* P3 diag */
  155. if (name == NULL) name = "???";
  156. if ((xres = xres_alloc()) != NULL) {
  157. tack = xres->xname;
  158. res = &xres->xres;
  159. } else {
  160. if (!printed_full) {
  161. printk("ioremap: done with statics, switching to malloc\n");
  162. printed_full = 1;
  163. }
  164. tlen = strlen(name);
  165. tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
  166. if (tack == NULL) return NULL;
  167. memset(tack, 0, sizeof(struct resource));
  168. res = (struct resource *) tack;
  169. tack += sizeof (struct resource);
  170. }
  171. strlcpy(tack, name, XNMLN+1);
  172. res->name = tack;
  173. va = _sparc_ioremap(res, busno, phys, size);
  174. /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
  175. return va;
  176. }
  177. /*
  178. */
  179. static void __iomem *
  180. _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
  181. {
  182. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  183. if (allocate_resource(&sparc_iomap, res,
  184. (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
  185. sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
  186. /* Usually we cannot see printks in this case. */
  187. prom_printf("alloc_io_res(%s): cannot occupy\n",
  188. (res->name != NULL)? res->name: "???");
  189. prom_halt();
  190. }
  191. pa &= PAGE_MASK;
  192. srmmu_mapiorange(bus, pa, res->start, resource_size(res));
  193. return (void __iomem *)(unsigned long)(res->start + offset);
  194. }
  195. /*
  196. * Complementary to _sparc_ioremap().
  197. */
  198. static void _sparc_free_io(struct resource *res)
  199. {
  200. unsigned long plen;
  201. plen = resource_size(res);
  202. BUG_ON((plen & (PAGE_SIZE-1)) != 0);
  203. srmmu_unmapiorange(res->start, plen);
  204. release_resource(res);
  205. }
  206. unsigned long sparc_dma_alloc_resource(struct device *dev, size_t len)
  207. {
  208. struct resource *res;
  209. res = kzalloc(sizeof(*res), GFP_KERNEL);
  210. if (!res)
  211. return 0;
  212. res->name = dev->of_node->full_name;
  213. if (allocate_resource(&_sparc_dvma, res, len, _sparc_dvma.start,
  214. _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  215. printk("%s: cannot occupy 0x%zx", __func__, len);
  216. kfree(res);
  217. return 0;
  218. }
  219. return res->start;
  220. }
  221. bool sparc_dma_free_resource(void *cpu_addr, size_t size)
  222. {
  223. unsigned long addr = (unsigned long)cpu_addr;
  224. struct resource *res;
  225. res = lookup_resource(&_sparc_dvma, addr);
  226. if (!res) {
  227. printk("%s: cannot free %p\n", __func__, cpu_addr);
  228. return false;
  229. }
  230. if ((addr & (PAGE_SIZE - 1)) != 0) {
  231. printk("%s: unaligned va %p\n", __func__, cpu_addr);
  232. return false;
  233. }
  234. size = PAGE_ALIGN(size);
  235. if (resource_size(res) != size) {
  236. printk("%s: region 0x%lx asked 0x%zx\n",
  237. __func__, (long)resource_size(res), size);
  238. return false;
  239. }
  240. release_resource(res);
  241. kfree(res);
  242. return true;
  243. }
  244. #ifdef CONFIG_SBUS
  245. void sbus_set_sbus64(struct device *dev, int x)
  246. {
  247. printk("sbus_set_sbus64: unsupported\n");
  248. }
  249. EXPORT_SYMBOL(sbus_set_sbus64);
  250. static int __init sparc_register_ioport(void)
  251. {
  252. register_proc_sparc_ioport();
  253. return 0;
  254. }
  255. arch_initcall(sparc_register_ioport);
  256. #endif /* CONFIG_SBUS */
  257. /*
  258. * IIep is write-through, not flushing on cpu to device transfer.
  259. *
  260. * On LEON systems without cache snooping, the entire D-CACHE must be flushed to
  261. * make DMA to cacheable memory coherent.
  262. */
  263. void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
  264. enum dma_data_direction dir)
  265. {
  266. if (dir != DMA_TO_DEVICE &&
  267. sparc_cpu_model == sparc_leon &&
  268. !sparc_leon3_snooping_enabled())
  269. leon_flush_dcache_all();
  270. }
  271. #ifdef CONFIG_PROC_FS
  272. static int sparc_io_proc_show(struct seq_file *m, void *v)
  273. {
  274. struct resource *root = m->private, *r;
  275. const char *nm;
  276. for (r = root->child; r != NULL; r = r->sibling) {
  277. if ((nm = r->name) == NULL) nm = "???";
  278. seq_printf(m, "%016llx-%016llx: %s\n",
  279. (unsigned long long)r->start,
  280. (unsigned long long)r->end, nm);
  281. }
  282. return 0;
  283. }
  284. #endif /* CONFIG_PROC_FS */
  285. static void register_proc_sparc_ioport(void)
  286. {
  287. #ifdef CONFIG_PROC_FS
  288. proc_create_single_data("io_map", 0, NULL, sparc_io_proc_show,
  289. &sparc_iomap);
  290. proc_create_single_data("dvma_map", 0, NULL, sparc_io_proc_show,
  291. &_sparc_dvma);
  292. #endif
  293. }