1 // SPDX-License-Identifier: GPL-2.0
3 * ioport.c: Simple io mapping allocator.
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
8 * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
11 * <rth> zait: as long as pci_alloc_consistent produces something addressable,
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()))
18 * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
20 * <zaitcev> Now, driver calls pci_free_consistent(with result of
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.
26 * <zaitcev> Sounds reasonable
29 #include <linux/module.h>
30 #include <linux/sched.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/types.h>
34 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/pci.h> /* struct pci_dev */
38 #include <linux/proc_fs.h>
39 #include <linux/seq_file.h>
40 #include <linux/scatterlist.h>
41 #include <linux/dma-noncoherent.h>
42 #include <linux/of_device.h>
45 #include <asm/vaddrs.h>
46 #include <asm/oplib.h>
49 #include <asm/pgalloc.h>
51 #include <asm/iommu.h>
52 #include <asm/io-unit.h>
55 const struct sparc32_dma_ops *sparc32_dma_ops;
57 /* This function must make sure that caches and memory are coherent after DMA
58 * On LEON systems without cache snooping it flushes the entire D-CACHE.
60 static inline void dma_make_coherent(unsigned long pa, unsigned long len)
62 if (sparc_cpu_model == sparc_leon) {
63 if (!sparc_leon3_snooping_enabled())
64 leon_flush_dcache_all();
68 static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
69 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
70 unsigned long size, char *name);
71 static void _sparc_free_io(struct resource *res);
73 static void register_proc_sparc_ioport(void);
75 /* This points to the next to use virtual memory for DVMA mappings */
76 static struct resource _sparc_dvma = {
77 .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
79 /* This points to the start of I/O mappings, cluable from outside. */
80 /*ext*/ struct resource sparc_iomap = {
81 .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
85 * Our mini-allocator...
86 * Boy this is gross! We need it because we must map I/O for
87 * timers and interrupt controller before the kmalloc is available.
91 #define XNRES 10 /* SS-10 uses 8 */
94 struct resource xres; /* Must be first */
95 int xflag; /* 1 == used */
99 static struct xresource xresv[XNRES];
101 static struct xresource *xres_alloc(void) {
102 struct xresource *xrp;
106 for (n = 0; n < XNRES; n++) {
107 if (xrp->xflag == 0) {
116 static void xres_free(struct xresource *xrp) {
121 * These are typically used in PCI drivers
122 * which are trying to be cross-platform.
124 * Bus type is always zero on IIep.
126 void __iomem *ioremap(phys_addr_t offset, size_t size)
130 sprintf(name, "phys_%08x", (u32)offset);
131 return _sparc_alloc_io(0, (unsigned long)offset, size, name);
133 EXPORT_SYMBOL(ioremap);
136 * Complementary to ioremap().
138 void iounmap(volatile void __iomem *virtual)
140 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
141 struct resource *res;
144 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
145 * This probably warrants some sort of hashing.
147 if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) {
148 printk("free_io/iounmap: cannot free %lx\n", vaddr);
153 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
154 xres_free((struct xresource *)res);
159 EXPORT_SYMBOL(iounmap);
161 void __iomem *of_ioremap(struct resource *res, unsigned long offset,
162 unsigned long size, char *name)
164 return _sparc_alloc_io(res->flags & 0xF,
168 EXPORT_SYMBOL(of_ioremap);
170 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
174 EXPORT_SYMBOL(of_iounmap);
179 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
180 unsigned long size, char *name)
182 static int printed_full;
183 struct xresource *xres;
184 struct resource *res;
187 void __iomem *va; /* P3 diag */
189 if (name == NULL) name = "???";
191 if ((xres = xres_alloc()) != NULL) {
196 printk("ioremap: done with statics, switching to malloc\n");
200 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
201 if (tack == NULL) return NULL;
202 memset(tack, 0, sizeof(struct resource));
203 res = (struct resource *) tack;
204 tack += sizeof (struct resource);
207 strlcpy(tack, name, XNMLN+1);
210 va = _sparc_ioremap(res, busno, phys, size);
211 /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
217 static void __iomem *
218 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
220 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
222 if (allocate_resource(&sparc_iomap, res,
223 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
224 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
225 /* Usually we cannot see printks in this case. */
226 prom_printf("alloc_io_res(%s): cannot occupy\n",
227 (res->name != NULL)? res->name: "???");
232 srmmu_mapiorange(bus, pa, res->start, resource_size(res));
234 return (void __iomem *)(unsigned long)(res->start + offset);
238 * Complementary to _sparc_ioremap().
240 static void _sparc_free_io(struct resource *res)
244 plen = resource_size(res);
245 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
246 srmmu_unmapiorange(res->start, plen);
247 release_resource(res);
252 void sbus_set_sbus64(struct device *dev, int x)
254 printk("sbus_set_sbus64: unsupported\n");
256 EXPORT_SYMBOL(sbus_set_sbus64);
259 * Allocate a chunk of memory suitable for DMA.
260 * Typically devices use them for control blocks.
261 * CPU may access them without any explicit flushing.
263 static void *sbus_alloc_coherent(struct device *dev, size_t len,
264 dma_addr_t *dma_addrp, gfp_t gfp,
267 struct platform_device *op = to_platform_device(dev);
268 unsigned long len_total = PAGE_ALIGN(len);
270 struct resource *res;
273 /* XXX why are some lengths signed, others unsigned? */
277 /* XXX So what is maxphys for us and how do drivers know it? */
278 if (len > 256*1024) { /* __get_free_pages() limit */
282 order = get_order(len_total);
283 va = __get_free_pages(gfp, order);
287 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
290 if (allocate_resource(&_sparc_dvma, res, len_total,
291 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
292 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
296 // XXX The sbus_map_dma_area does this for us below, see comments.
297 // srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total);
299 * XXX That's where sdev would be used. Currently we load
300 * all iommu tables with the same translations.
302 if (sbus_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
305 res->name = op->dev.of_node->name;
307 return (void *)(unsigned long)res->start;
310 release_resource(res);
314 free_pages(va, order);
319 static void sbus_free_coherent(struct device *dev, size_t n, void *p,
320 dma_addr_t ba, unsigned long attrs)
322 struct resource *res;
325 if ((res = lookup_resource(&_sparc_dvma,
326 (unsigned long)p)) == NULL) {
327 printk("sbus_free_consistent: cannot free %p\n", p);
331 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
332 printk("sbus_free_consistent: unaligned va %p\n", p);
337 if (resource_size(res) != n) {
338 printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
339 (long)resource_size(res), n);
343 release_resource(res);
346 pgv = virt_to_page(p);
347 sbus_unmap_dma_area(dev, ba, n);
349 __free_pages(pgv, get_order(n));
353 * Map a chunk of memory so that devices can see it.
354 * CPU view of this memory may be inconsistent with
355 * a device view and explicit flushing is necessary.
357 static dma_addr_t sbus_map_page(struct device *dev, struct page *page,
358 unsigned long offset, size_t len,
359 enum dma_data_direction dir,
362 void *va = page_address(page) + offset;
364 /* XXX why are some lengths signed, others unsigned? */
368 /* XXX So what is maxphys for us and how do drivers know it? */
369 if (len > 256*1024) { /* __get_free_pages() limit */
372 return mmu_get_scsi_one(dev, va, len);
375 static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n,
376 enum dma_data_direction dir, unsigned long attrs)
378 mmu_release_scsi_one(dev, ba, n);
381 static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
382 enum dma_data_direction dir, unsigned long attrs)
384 mmu_get_scsi_sgl(dev, sg, n);
388 static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
389 enum dma_data_direction dir, unsigned long attrs)
391 mmu_release_scsi_sgl(dev, sg, n);
394 static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
395 int n, enum dma_data_direction dir)
400 static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
401 int n, enum dma_data_direction dir)
406 static int sbus_dma_supported(struct device *dev, u64 mask)
411 static const struct dma_map_ops sbus_dma_ops = {
412 .alloc = sbus_alloc_coherent,
413 .free = sbus_free_coherent,
414 .map_page = sbus_map_page,
415 .unmap_page = sbus_unmap_page,
416 .map_sg = sbus_map_sg,
417 .unmap_sg = sbus_unmap_sg,
418 .sync_sg_for_cpu = sbus_sync_sg_for_cpu,
419 .sync_sg_for_device = sbus_sync_sg_for_device,
420 .dma_supported = sbus_dma_supported,
423 static int __init sparc_register_ioport(void)
425 register_proc_sparc_ioport();
430 arch_initcall(sparc_register_ioport);
432 #endif /* CONFIG_SBUS */
435 /* Allocate and map kernel buffer using consistent mode DMA for a device.
436 * hwdev should be valid struct pci_dev pointer for PCI devices.
438 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
439 gfp_t gfp, unsigned long attrs)
441 unsigned long len_total = PAGE_ALIGN(size);
443 struct resource *res;
449 if (size > 256*1024) { /* __get_free_pages() limit */
453 order = get_order(len_total);
454 va = (void *) __get_free_pages(gfp, order);
456 printk("%s: no %ld pages\n", __func__, len_total>>PAGE_SHIFT);
460 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
461 printk("%s: no core\n", __func__);
465 if (allocate_resource(&_sparc_dvma, res, len_total,
466 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
467 printk("%s: cannot occupy 0x%lx", __func__, len_total);
470 srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total);
472 *dma_handle = virt_to_phys(va);
473 return (void *) res->start;
478 free_pages((unsigned long)va, order);
483 /* Free and unmap a consistent DMA buffer.
484 * cpu_addr is what was returned arch_dma_alloc, size must be the same as what
485 * was passed into arch_dma_alloc, and likewise dma_addr must be the same as
486 * what *dma_ndler was set to.
488 * References to the memory and mappings associated with cpu_addr/dma_addr
489 * past this call are illegal.
491 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
492 dma_addr_t dma_addr, unsigned long attrs)
494 struct resource *res;
496 if ((res = lookup_resource(&_sparc_dvma,
497 (unsigned long)cpu_addr)) == NULL) {
498 printk("%s: cannot free %p\n", __func__, cpu_addr);
502 if (((unsigned long)cpu_addr & (PAGE_SIZE-1)) != 0) {
503 printk("%s: unaligned va %p\n", __func__, cpu_addr);
507 size = PAGE_ALIGN(size);
508 if (resource_size(res) != size) {
509 printk("%s: region 0x%lx asked 0x%zx\n", __func__,
510 (long)resource_size(res), size);
514 dma_make_coherent(dma_addr, size);
515 srmmu_unmapiorange((unsigned long)cpu_addr, size);
517 release_resource(res);
519 free_pages((unsigned long)phys_to_virt(dma_addr), get_order(size));
522 /* IIep is write-through, not flushing on cpu to device transfer. */
524 void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
525 size_t size, enum dma_data_direction dir)
527 if (dir != PCI_DMA_TODEVICE)
528 dma_make_coherent(paddr, PAGE_ALIGN(size));
531 const struct dma_map_ops *dma_ops = &sbus_dma_ops;
532 EXPORT_SYMBOL(dma_ops);
534 #ifdef CONFIG_PROC_FS
536 static int sparc_io_proc_show(struct seq_file *m, void *v)
538 struct resource *root = m->private, *r;
541 for (r = root->child; r != NULL; r = r->sibling) {
542 if ((nm = r->name) == NULL) nm = "???";
543 seq_printf(m, "%016llx-%016llx: %s\n",
544 (unsigned long long)r->start,
545 (unsigned long long)r->end, nm);
550 #endif /* CONFIG_PROC_FS */
552 static void register_proc_sparc_ioport(void)
554 #ifdef CONFIG_PROC_FS
555 proc_create_single_data("io_map", 0, NULL, sparc_io_proc_show,
557 proc_create_single_data("dvma_map", 0, NULL, sparc_io_proc_show,