GNU Linux-libre 4.19.264-gnu1
[releases.git] / arch / sparc / kernel / ioport.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ioport.c:  Simple io mapping allocator.
4  *
5  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6  * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
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
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>
35 #include <linux/mm.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>
43
44 #include <asm/io.h>
45 #include <asm/vaddrs.h>
46 #include <asm/oplib.h>
47 #include <asm/prom.h>
48 #include <asm/page.h>
49 #include <asm/pgalloc.h>
50 #include <asm/dma.h>
51 #include <asm/iommu.h>
52 #include <asm/io-unit.h>
53 #include <asm/leon.h>
54
55 const struct sparc32_dma_ops *sparc32_dma_ops;
56
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.
59  */
60 static inline void dma_make_coherent(unsigned long pa, unsigned long len)
61 {
62         if (sparc_cpu_model == sparc_leon) {
63                 if (!sparc_leon3_snooping_enabled())
64                         leon_flush_dcache_all();
65         }
66 }
67
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);
72
73 static void register_proc_sparc_ioport(void);
74
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
78 };
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
82 };
83
84 /*
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.
88  */
89
90 #define XNMLN  15
91 #define XNRES  10       /* SS-10 uses 8 */
92
93 struct xresource {
94         struct resource xres;   /* Must be first */
95         int xflag;              /* 1 == used */
96         char xname[XNMLN+1];
97 };
98
99 static struct xresource xresv[XNRES];
100
101 static struct xresource *xres_alloc(void) {
102         struct xresource *xrp;
103         int n;
104
105         xrp = xresv;
106         for (n = 0; n < XNRES; n++) {
107                 if (xrp->xflag == 0) {
108                         xrp->xflag = 1;
109                         return xrp;
110                 }
111                 xrp++;
112         }
113         return NULL;
114 }
115
116 static void xres_free(struct xresource *xrp) {
117         xrp->xflag = 0;
118 }
119
120 /*
121  * These are typically used in PCI drivers
122  * which are trying to be cross-platform.
123  *
124  * Bus type is always zero on IIep.
125  */
126 void __iomem *ioremap(phys_addr_t offset, size_t size)
127 {
128         char name[14];
129
130         sprintf(name, "phys_%08x", (u32)offset);
131         return _sparc_alloc_io(0, (unsigned long)offset, size, name);
132 }
133 EXPORT_SYMBOL(ioremap);
134
135 /*
136  * Complementary to ioremap().
137  */
138 void iounmap(volatile void __iomem *virtual)
139 {
140         unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
141         struct resource *res;
142
143         /*
144          * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
145          * This probably warrants some sort of hashing.
146         */
147         if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) {
148                 printk("free_io/iounmap: cannot free %lx\n", vaddr);
149                 return;
150         }
151         _sparc_free_io(res);
152
153         if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
154                 xres_free((struct xresource *)res);
155         } else {
156                 kfree(res);
157         }
158 }
159 EXPORT_SYMBOL(iounmap);
160
161 void __iomem *of_ioremap(struct resource *res, unsigned long offset,
162                          unsigned long size, char *name)
163 {
164         return _sparc_alloc_io(res->flags & 0xF,
165                                res->start + offset,
166                                size, name);
167 }
168 EXPORT_SYMBOL(of_ioremap);
169
170 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
171 {
172         iounmap(base);
173 }
174 EXPORT_SYMBOL(of_iounmap);
175
176 /*
177  * Meat of mapping
178  */
179 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
180     unsigned long size, char *name)
181 {
182         static int printed_full;
183         struct xresource *xres;
184         struct resource *res;
185         char *tack;
186         int tlen;
187         void __iomem *va;       /* P3 diag */
188
189         if (name == NULL) name = "???";
190
191         if ((xres = xres_alloc()) != NULL) {
192                 tack = xres->xname;
193                 res = &xres->xres;
194         } else {
195                 if (!printed_full) {
196                         printk("ioremap: done with statics, switching to malloc\n");
197                         printed_full = 1;
198                 }
199                 tlen = strlen(name);
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);
205         }
206
207         strlcpy(tack, name, XNMLN+1);
208         res->name = tack;
209
210         va = _sparc_ioremap(res, busno, phys, size);
211         /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
212         return va;
213 }
214
215 /*
216  */
217 static void __iomem *
218 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
219 {
220         unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
221
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: "???");
228                 prom_halt();
229         }
230
231         pa &= PAGE_MASK;
232         srmmu_mapiorange(bus, pa, res->start, resource_size(res));
233
234         return (void __iomem *)(unsigned long)(res->start + offset);
235 }
236
237 /*
238  * Complementary to _sparc_ioremap().
239  */
240 static void _sparc_free_io(struct resource *res)
241 {
242         unsigned long plen;
243
244         plen = resource_size(res);
245         BUG_ON((plen & (PAGE_SIZE-1)) != 0);
246         srmmu_unmapiorange(res->start, plen);
247         release_resource(res);
248 }
249
250 #ifdef CONFIG_SBUS
251
252 void sbus_set_sbus64(struct device *dev, int x)
253 {
254         printk("sbus_set_sbus64: unsupported\n");
255 }
256 EXPORT_SYMBOL(sbus_set_sbus64);
257
258 /*
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.
262  */
263 static void *sbus_alloc_coherent(struct device *dev, size_t len,
264                                  dma_addr_t *dma_addrp, gfp_t gfp,
265                                  unsigned long attrs)
266 {
267         struct platform_device *op = to_platform_device(dev);
268         unsigned long len_total = PAGE_ALIGN(len);
269         unsigned long va;
270         struct resource *res;
271         int order;
272
273         /* XXX why are some lengths signed, others unsigned? */
274         if (len <= 0) {
275                 return NULL;
276         }
277         /* XXX So what is maxphys for us and how do drivers know it? */
278         if (len > 256*1024) {                   /* __get_free_pages() limit */
279                 return NULL;
280         }
281
282         order = get_order(len_total);
283         va = __get_free_pages(gfp, order);
284         if (va == 0)
285                 goto err_nopages;
286
287         if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
288                 goto err_nomem;
289
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);
293                 goto err_nova;
294         }
295
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);
298         /*
299          * XXX That's where sdev would be used. Currently we load
300          * all iommu tables with the same translations.
301          */
302         if (sbus_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
303                 goto err_noiommu;
304
305         res->name = op->dev.of_node->name;
306
307         return (void *)(unsigned long)res->start;
308
309 err_noiommu:
310         release_resource(res);
311 err_nova:
312         kfree(res);
313 err_nomem:
314         free_pages(va, order);
315 err_nopages:
316         return NULL;
317 }
318
319 static void sbus_free_coherent(struct device *dev, size_t n, void *p,
320                                dma_addr_t ba, unsigned long attrs)
321 {
322         struct resource *res;
323         struct page *pgv;
324
325         if ((res = lookup_resource(&_sparc_dvma,
326             (unsigned long)p)) == NULL) {
327                 printk("sbus_free_consistent: cannot free %p\n", p);
328                 return;
329         }
330
331         if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
332                 printk("sbus_free_consistent: unaligned va %p\n", p);
333                 return;
334         }
335
336         n = PAGE_ALIGN(n);
337         if (resource_size(res) != n) {
338                 printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
339                     (long)resource_size(res), n);
340                 return;
341         }
342
343         release_resource(res);
344         kfree(res);
345
346         pgv = virt_to_page(p);
347         sbus_unmap_dma_area(dev, ba, n);
348
349         __free_pages(pgv, get_order(n));
350 }
351
352 /*
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.
356  */
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,
360                                 unsigned long attrs)
361 {
362         void *va = page_address(page) + offset;
363
364         /* XXX why are some lengths signed, others unsigned? */
365         if (len <= 0) {
366                 return 0;
367         }
368         /* XXX So what is maxphys for us and how do drivers know it? */
369         if (len > 256*1024) {                   /* __get_free_pages() limit */
370                 return 0;
371         }
372         return mmu_get_scsi_one(dev, va, len);
373 }
374
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)
377 {
378         mmu_release_scsi_one(dev, ba, n);
379 }
380
381 static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
382                        enum dma_data_direction dir, unsigned long attrs)
383 {
384         mmu_get_scsi_sgl(dev, sg, n);
385         return n;
386 }
387
388 static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
389                           enum dma_data_direction dir, unsigned long attrs)
390 {
391         mmu_release_scsi_sgl(dev, sg, n);
392 }
393
394 static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
395                                  int n, enum dma_data_direction dir)
396 {
397         BUG();
398 }
399
400 static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
401                                     int n, enum dma_data_direction dir)
402 {
403         BUG();
404 }
405
406 static int sbus_dma_supported(struct device *dev, u64 mask)
407 {
408         return 0;
409 }
410
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,
421 };
422
423 static int __init sparc_register_ioport(void)
424 {
425         register_proc_sparc_ioport();
426
427         return 0;
428 }
429
430 arch_initcall(sparc_register_ioport);
431
432 #endif /* CONFIG_SBUS */
433
434
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.
437  */
438 void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
439                 gfp_t gfp, unsigned long attrs)
440 {
441         unsigned long len_total = PAGE_ALIGN(size);
442         void *va;
443         struct resource *res;
444         int order;
445
446         if (size == 0) {
447                 return NULL;
448         }
449         if (size > 256*1024) {                  /* __get_free_pages() limit */
450                 return NULL;
451         }
452
453         order = get_order(len_total);
454         va = (void *) __get_free_pages(gfp, order);
455         if (va == NULL) {
456                 printk("%s: no %ld pages\n", __func__, len_total>>PAGE_SHIFT);
457                 goto err_nopages;
458         }
459
460         if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
461                 printk("%s: no core\n", __func__);
462                 goto err_nomem;
463         }
464
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);
468                 goto err_nova;
469         }
470         srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total);
471
472         *dma_handle = virt_to_phys(va);
473         return (void *) res->start;
474
475 err_nova:
476         kfree(res);
477 err_nomem:
478         free_pages((unsigned long)va, order);
479 err_nopages:
480         return NULL;
481 }
482
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.
487  *
488  * References to the memory and mappings associated with cpu_addr/dma_addr
489  * past this call are illegal.
490  */
491 void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
492                 dma_addr_t dma_addr, unsigned long attrs)
493 {
494         struct resource *res;
495
496         if ((res = lookup_resource(&_sparc_dvma,
497             (unsigned long)cpu_addr)) == NULL) {
498                 printk("%s: cannot free %p\n", __func__, cpu_addr);
499                 return;
500         }
501
502         if (((unsigned long)cpu_addr & (PAGE_SIZE-1)) != 0) {
503                 printk("%s: unaligned va %p\n", __func__, cpu_addr);
504                 return;
505         }
506
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);
511                 return;
512         }
513
514         dma_make_coherent(dma_addr, size);
515         srmmu_unmapiorange((unsigned long)cpu_addr, size);
516
517         release_resource(res);
518         kfree(res);
519         free_pages((unsigned long)phys_to_virt(dma_addr), get_order(size));
520 }
521
522 /* IIep is write-through, not flushing on cpu to device transfer. */
523
524 void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
525                 size_t size, enum dma_data_direction dir)
526 {
527         if (dir != PCI_DMA_TODEVICE)
528                 dma_make_coherent(paddr, PAGE_ALIGN(size));
529 }
530
531 const struct dma_map_ops *dma_ops = &sbus_dma_ops;
532 EXPORT_SYMBOL(dma_ops);
533
534 #ifdef CONFIG_PROC_FS
535
536 static int sparc_io_proc_show(struct seq_file *m, void *v)
537 {
538         struct resource *root = m->private, *r;
539         const char *nm;
540
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);
546         }
547
548         return 0;
549 }
550 #endif /* CONFIG_PROC_FS */
551
552 static void register_proc_sparc_ioport(void)
553 {
554 #ifdef CONFIG_PROC_FS
555         proc_create_single_data("io_map", 0, NULL, sparc_io_proc_show,
556                         &sparc_iomap);
557         proc_create_single_data("dvma_map", 0, NULL, sparc_io_proc_show,
558                         &_sparc_dvma);
559 #endif
560 }