Full support for Ginger Console
[linux-ginger.git] / arch / sparc / kernel / ioport.c
blob9f61fd8cbb7b80d8ade28e73623567306bc171ee
1 /*
2 * ioport.c: Simple io mapping allocator.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
7 * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
9 * 2000/01/29
10 * <rth> zait: as long as pci_alloc_consistent produces something addressable,
11 * things are ok.
12 * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
13 * pointer into the big page mapping
14 * <rth> zait: so what?
15 * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
16 * <zaitcev> Hmm
17 * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
18 * So far so good.
19 * <zaitcev> Now, driver calls pci_free_consistent(with result of
20 * remap_it_my_way()).
21 * <zaitcev> How do you find the address to pass to free_pages()?
22 * <rth> zait: walk the page tables? It's only two or three level after all.
23 * <rth> zait: you have to walk them anyway to remove the mapping.
24 * <zaitcev> Hmm
25 * <zaitcev> Sounds reasonable
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/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>
52 #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
54 static struct resource *_sparc_find_resource(struct resource *r,
55 unsigned long);
57 static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
58 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
59 unsigned long size, char *name);
60 static void _sparc_free_io(struct resource *res);
62 static void register_proc_sparc_ioport(void);
64 /* This points to the next to use virtual memory for DVMA mappings */
65 static struct resource _sparc_dvma = {
66 .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
68 /* This points to the start of I/O mappings, cluable from outside. */
69 /*ext*/ struct resource sparc_iomap = {
70 .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
74 * Our mini-allocator...
75 * Boy this is gross! We need it because we must map I/O for
76 * timers and interrupt controller before the kmalloc is available.
79 #define XNMLN 15
80 #define XNRES 10 /* SS-10 uses 8 */
82 struct xresource {
83 struct resource xres; /* Must be first */
84 int xflag; /* 1 == used */
85 char xname[XNMLN+1];
88 static struct xresource xresv[XNRES];
90 static struct xresource *xres_alloc(void) {
91 struct xresource *xrp;
92 int n;
94 xrp = xresv;
95 for (n = 0; n < XNRES; n++) {
96 if (xrp->xflag == 0) {
97 xrp->xflag = 1;
98 return xrp;
100 xrp++;
102 return NULL;
105 static void xres_free(struct xresource *xrp) {
106 xrp->xflag = 0;
110 * These are typically used in PCI drivers
111 * which are trying to be cross-platform.
113 * Bus type is always zero on IIep.
115 void __iomem *ioremap(unsigned long offset, unsigned long size)
117 char name[14];
119 sprintf(name, "phys_%08x", (u32)offset);
120 return _sparc_alloc_io(0, offset, size, name);
122 EXPORT_SYMBOL(ioremap);
125 * Comlimentary to ioremap().
127 void iounmap(volatile void __iomem *virtual)
129 unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
130 struct resource *res;
132 if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
133 printk("free_io/iounmap: cannot free %lx\n", vaddr);
134 return;
136 _sparc_free_io(res);
138 if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
139 xres_free((struct xresource *)res);
140 } else {
141 kfree(res);
144 EXPORT_SYMBOL(iounmap);
146 void __iomem *of_ioremap(struct resource *res, unsigned long offset,
147 unsigned long size, char *name)
149 return _sparc_alloc_io(res->flags & 0xF,
150 res->start + offset,
151 size, name);
153 EXPORT_SYMBOL(of_ioremap);
155 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
157 iounmap(base);
159 EXPORT_SYMBOL(of_iounmap);
162 * Meat of mapping
164 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
165 unsigned long size, char *name)
167 static int printed_full;
168 struct xresource *xres;
169 struct resource *res;
170 char *tack;
171 int tlen;
172 void __iomem *va; /* P3 diag */
174 if (name == NULL) name = "???";
176 if ((xres = xres_alloc()) != 0) {
177 tack = xres->xname;
178 res = &xres->xres;
179 } else {
180 if (!printed_full) {
181 printk("ioremap: done with statics, switching to malloc\n");
182 printed_full = 1;
184 tlen = strlen(name);
185 tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
186 if (tack == NULL) return NULL;
187 memset(tack, 0, sizeof(struct resource));
188 res = (struct resource *) tack;
189 tack += sizeof (struct resource);
192 strlcpy(tack, name, XNMLN+1);
193 res->name = tack;
195 va = _sparc_ioremap(res, busno, phys, size);
196 /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
197 return va;
202 static void __iomem *
203 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
205 unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
207 if (allocate_resource(&sparc_iomap, res,
208 (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
209 sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
210 /* Usually we cannot see printks in this case. */
211 prom_printf("alloc_io_res(%s): cannot occupy\n",
212 (res->name != NULL)? res->name: "???");
213 prom_halt();
216 pa &= PAGE_MASK;
217 sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
219 return (void __iomem *)(unsigned long)(res->start + offset);
223 * Comlimentary to _sparc_ioremap().
225 static void _sparc_free_io(struct resource *res)
227 unsigned long plen;
229 plen = res->end - res->start + 1;
230 BUG_ON((plen & (PAGE_SIZE-1)) != 0);
231 sparc_unmapiorange(res->start, plen);
232 release_resource(res);
235 #ifdef CONFIG_SBUS
237 void sbus_set_sbus64(struct device *dev, int x)
239 printk("sbus_set_sbus64: unsupported\n");
241 EXPORT_SYMBOL(sbus_set_sbus64);
244 * Allocate a chunk of memory suitable for DMA.
245 * Typically devices use them for control blocks.
246 * CPU may access them without any explicit flushing.
248 static void *sbus_alloc_coherent(struct device *dev, size_t len,
249 dma_addr_t *dma_addrp, gfp_t gfp)
251 struct of_device *op = to_of_device(dev);
252 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
253 unsigned long va;
254 struct resource *res;
255 int order;
257 /* XXX why are some lengths signed, others unsigned? */
258 if (len <= 0) {
259 return NULL;
261 /* XXX So what is maxphys for us and how do drivers know it? */
262 if (len > 256*1024) { /* __get_free_pages() limit */
263 return NULL;
266 order = get_order(len_total);
267 if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
268 goto err_nopages;
270 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
271 goto err_nomem;
273 if (allocate_resource(&_sparc_dvma, res, len_total,
274 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
275 printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
276 goto err_nova;
278 mmu_inval_dma_area(va, len_total);
279 // XXX The mmu_map_dma_area does this for us below, see comments.
280 // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
282 * XXX That's where sdev would be used. Currently we load
283 * all iommu tables with the same translations.
285 if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
286 goto err_noiommu;
288 res->name = op->node->name;
290 return (void *)(unsigned long)res->start;
292 err_noiommu:
293 release_resource(res);
294 err_nova:
295 free_pages(va, order);
296 err_nomem:
297 kfree(res);
298 err_nopages:
299 return NULL;
302 static void sbus_free_coherent(struct device *dev, size_t n, void *p,
303 dma_addr_t ba)
305 struct resource *res;
306 struct page *pgv;
308 if ((res = _sparc_find_resource(&_sparc_dvma,
309 (unsigned long)p)) == NULL) {
310 printk("sbus_free_consistent: cannot free %p\n", p);
311 return;
314 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
315 printk("sbus_free_consistent: unaligned va %p\n", p);
316 return;
319 n = (n + PAGE_SIZE-1) & PAGE_MASK;
320 if ((res->end-res->start)+1 != n) {
321 printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
322 (long)((res->end-res->start)+1), n);
323 return;
326 release_resource(res);
327 kfree(res);
329 /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
330 pgv = virt_to_page(p);
331 mmu_unmap_dma_area(dev, ba, n);
333 __free_pages(pgv, get_order(n));
337 * Map a chunk of memory so that devices can see it.
338 * CPU view of this memory may be inconsistent with
339 * a device view and explicit flushing is necessary.
341 static dma_addr_t sbus_map_page(struct device *dev, struct page *page,
342 unsigned long offset, size_t len,
343 enum dma_data_direction dir,
344 struct dma_attrs *attrs)
346 void *va = page_address(page) + offset;
348 /* XXX why are some lengths signed, others unsigned? */
349 if (len <= 0) {
350 return 0;
352 /* XXX So what is maxphys for us and how do drivers know it? */
353 if (len > 256*1024) { /* __get_free_pages() limit */
354 return 0;
356 return mmu_get_scsi_one(dev, va, len);
359 static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n,
360 enum dma_data_direction dir, struct dma_attrs *attrs)
362 mmu_release_scsi_one(dev, ba, n);
365 static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
366 enum dma_data_direction dir, struct dma_attrs *attrs)
368 mmu_get_scsi_sgl(dev, sg, n);
371 * XXX sparc64 can return a partial length here. sun4c should do this
372 * but it currently panics if it can't fulfill the request - Anton
374 return n;
377 static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
378 enum dma_data_direction dir, struct dma_attrs *attrs)
380 mmu_release_scsi_sgl(dev, sg, n);
383 static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
384 int n, enum dma_data_direction dir)
386 BUG();
389 static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
390 int n, enum dma_data_direction dir)
392 BUG();
395 struct dma_map_ops sbus_dma_ops = {
396 .alloc_coherent = sbus_alloc_coherent,
397 .free_coherent = sbus_free_coherent,
398 .map_page = sbus_map_page,
399 .unmap_page = sbus_unmap_page,
400 .map_sg = sbus_map_sg,
401 .unmap_sg = sbus_unmap_sg,
402 .sync_sg_for_cpu = sbus_sync_sg_for_cpu,
403 .sync_sg_for_device = sbus_sync_sg_for_device,
406 struct dma_map_ops *dma_ops = &sbus_dma_ops;
407 EXPORT_SYMBOL(dma_ops);
409 static int __init sparc_register_ioport(void)
411 register_proc_sparc_ioport();
413 return 0;
416 arch_initcall(sparc_register_ioport);
418 #endif /* CONFIG_SBUS */
420 #ifdef CONFIG_PCI
422 /* Allocate and map kernel buffer using consistent mode DMA for a device.
423 * hwdev should be valid struct pci_dev pointer for PCI devices.
425 static void *pci32_alloc_coherent(struct device *dev, size_t len,
426 dma_addr_t *pba, gfp_t gfp)
428 unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
429 unsigned long va;
430 struct resource *res;
431 int order;
433 if (len == 0) {
434 return NULL;
436 if (len > 256*1024) { /* __get_free_pages() limit */
437 return NULL;
440 order = get_order(len_total);
441 va = __get_free_pages(GFP_KERNEL, order);
442 if (va == 0) {
443 printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
444 return NULL;
447 if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
448 free_pages(va, order);
449 printk("pci_alloc_consistent: no core\n");
450 return NULL;
453 if (allocate_resource(&_sparc_dvma, res, len_total,
454 _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
455 printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
456 free_pages(va, order);
457 kfree(res);
458 return NULL;
460 mmu_inval_dma_area(va, len_total);
461 #if 0
462 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
463 (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
464 #endif
465 sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
467 *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
468 return (void *) res->start;
471 /* Free and unmap a consistent DMA buffer.
472 * cpu_addr is what was returned from pci_alloc_consistent,
473 * size must be the same as what as passed into pci_alloc_consistent,
474 * and likewise dma_addr must be the same as what *dma_addrp was set to.
476 * References to the memory and mappings associated with cpu_addr/dma_addr
477 * past this call are illegal.
479 static void pci32_free_coherent(struct device *dev, size_t n, void *p,
480 dma_addr_t ba)
482 struct resource *res;
483 unsigned long pgp;
485 if ((res = _sparc_find_resource(&_sparc_dvma,
486 (unsigned long)p)) == NULL) {
487 printk("pci_free_consistent: cannot free %p\n", p);
488 return;
491 if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
492 printk("pci_free_consistent: unaligned va %p\n", p);
493 return;
496 n = (n + PAGE_SIZE-1) & PAGE_MASK;
497 if ((res->end-res->start)+1 != n) {
498 printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
499 (long)((res->end-res->start)+1), (long)n);
500 return;
503 pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
504 mmu_inval_dma_area(pgp, n);
505 sparc_unmapiorange((unsigned long)p, n);
507 release_resource(res);
508 kfree(res);
510 free_pages(pgp, get_order(n));
514 * Same as pci_map_single, but with pages.
516 static dma_addr_t pci32_map_page(struct device *dev, struct page *page,
517 unsigned long offset, size_t size,
518 enum dma_data_direction dir,
519 struct dma_attrs *attrs)
521 /* IIep is write-through, not flushing. */
522 return page_to_phys(page) + offset;
525 /* Map a set of buffers described by scatterlist in streaming
526 * mode for DMA. This is the scather-gather version of the
527 * above pci_map_single interface. Here the scatter gather list
528 * elements are each tagged with the appropriate dma address
529 * and length. They are obtained via sg_dma_{address,length}(SG).
531 * NOTE: An implementation may be able to use a smaller number of
532 * DMA address/length pairs than there are SG table elements.
533 * (for example via virtual mapping capabilities)
534 * The routine returns the number of addr/length pairs actually
535 * used, at most nents.
537 * Device ownership issues as mentioned above for pci_map_single are
538 * the same here.
540 static int pci32_map_sg(struct device *device, struct scatterlist *sgl,
541 int nents, enum dma_data_direction dir,
542 struct dma_attrs *attrs)
544 struct scatterlist *sg;
545 int n;
547 /* IIep is write-through, not flushing. */
548 for_each_sg(sgl, sg, nents, n) {
549 BUG_ON(page_address(sg_page(sg)) == NULL);
550 sg->dma_address = virt_to_phys(sg_virt(sg));
551 sg->dma_length = sg->length;
553 return nents;
556 /* Unmap a set of streaming mode DMA translations.
557 * Again, cpu read rules concerning calls here are the same as for
558 * pci_unmap_single() above.
560 static void pci32_unmap_sg(struct device *dev, struct scatterlist *sgl,
561 int nents, enum dma_data_direction dir,
562 struct dma_attrs *attrs)
564 struct scatterlist *sg;
565 int n;
567 if (dir != PCI_DMA_TODEVICE) {
568 for_each_sg(sgl, sg, nents, n) {
569 BUG_ON(page_address(sg_page(sg)) == NULL);
570 mmu_inval_dma_area(
571 (unsigned long) page_address(sg_page(sg)),
572 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
577 /* Make physical memory consistent for a single
578 * streaming mode DMA translation before or after a transfer.
580 * If you perform a pci_map_single() but wish to interrogate the
581 * buffer using the cpu, yet do not wish to teardown the PCI dma
582 * mapping, you must call this function before doing so. At the
583 * next point you give the PCI dma address back to the card, you
584 * must first perform a pci_dma_sync_for_device, and then the
585 * device again owns the buffer.
587 static void pci32_sync_single_for_cpu(struct device *dev, dma_addr_t ba,
588 size_t size, enum dma_data_direction dir)
590 if (dir != PCI_DMA_TODEVICE) {
591 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
592 (size + PAGE_SIZE-1) & PAGE_MASK);
596 static void pci32_sync_single_for_device(struct device *dev, dma_addr_t ba,
597 size_t size, enum dma_data_direction dir)
599 if (dir != PCI_DMA_TODEVICE) {
600 mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
601 (size + PAGE_SIZE-1) & PAGE_MASK);
605 /* Make physical memory consistent for a set of streaming
606 * mode DMA translations after a transfer.
608 * The same as pci_dma_sync_single_* but for a scatter-gather list,
609 * same rules and usage.
611 static void pci32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
612 int nents, enum dma_data_direction dir)
614 struct scatterlist *sg;
615 int n;
617 if (dir != PCI_DMA_TODEVICE) {
618 for_each_sg(sgl, sg, nents, n) {
619 BUG_ON(page_address(sg_page(sg)) == NULL);
620 mmu_inval_dma_area(
621 (unsigned long) page_address(sg_page(sg)),
622 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
627 static void pci32_sync_sg_for_device(struct device *device, struct scatterlist *sgl,
628 int nents, enum dma_data_direction dir)
630 struct scatterlist *sg;
631 int n;
633 if (dir != PCI_DMA_TODEVICE) {
634 for_each_sg(sgl, sg, nents, n) {
635 BUG_ON(page_address(sg_page(sg)) == NULL);
636 mmu_inval_dma_area(
637 (unsigned long) page_address(sg_page(sg)),
638 (sg->length + PAGE_SIZE-1) & PAGE_MASK);
643 struct dma_map_ops pci32_dma_ops = {
644 .alloc_coherent = pci32_alloc_coherent,
645 .free_coherent = pci32_free_coherent,
646 .map_page = pci32_map_page,
647 .map_sg = pci32_map_sg,
648 .unmap_sg = pci32_unmap_sg,
649 .sync_single_for_cpu = pci32_sync_single_for_cpu,
650 .sync_single_for_device = pci32_sync_single_for_device,
651 .sync_sg_for_cpu = pci32_sync_sg_for_cpu,
652 .sync_sg_for_device = pci32_sync_sg_for_device,
654 EXPORT_SYMBOL(pci32_dma_ops);
656 #endif /* CONFIG_PCI */
659 * Return whether the given PCI device DMA address mask can be
660 * supported properly. For example, if your device can only drive the
661 * low 24-bits during PCI bus mastering, then you would pass
662 * 0x00ffffff as the mask to this function.
664 int dma_supported(struct device *dev, u64 mask)
666 #ifdef CONFIG_PCI
667 if (dev->bus == &pci_bus_type)
668 return 1;
669 #endif
670 return 0;
672 EXPORT_SYMBOL(dma_supported);
674 int dma_set_mask(struct device *dev, u64 dma_mask)
676 #ifdef CONFIG_PCI
677 if (dev->bus == &pci_bus_type)
678 return pci_set_dma_mask(to_pci_dev(dev), dma_mask);
679 #endif
680 return -EOPNOTSUPP;
682 EXPORT_SYMBOL(dma_set_mask);
685 #ifdef CONFIG_PROC_FS
687 static int sparc_io_proc_show(struct seq_file *m, void *v)
689 struct resource *root = m->private, *r;
690 const char *nm;
692 for (r = root->child; r != NULL; r = r->sibling) {
693 if ((nm = r->name) == 0) nm = "???";
694 seq_printf(m, "%016llx-%016llx: %s\n",
695 (unsigned long long)r->start,
696 (unsigned long long)r->end, nm);
699 return 0;
702 static int sparc_io_proc_open(struct inode *inode, struct file *file)
704 return single_open(file, sparc_io_proc_show, PDE(inode)->data);
707 static const struct file_operations sparc_io_proc_fops = {
708 .owner = THIS_MODULE,
709 .open = sparc_io_proc_open,
710 .read = seq_read,
711 .llseek = seq_lseek,
712 .release = single_release,
714 #endif /* CONFIG_PROC_FS */
717 * This is a version of find_resource and it belongs to kernel/resource.c.
718 * Until we have agreement with Linus and Martin, it lingers here.
720 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
721 * This probably warrants some sort of hashing.
723 static struct resource *_sparc_find_resource(struct resource *root,
724 unsigned long hit)
726 struct resource *tmp;
728 for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
729 if (tmp->start <= hit && tmp->end >= hit)
730 return tmp;
732 return NULL;
735 static void register_proc_sparc_ioport(void)
737 #ifdef CONFIG_PROC_FS
738 proc_create_data("io_map", 0, NULL, &sparc_io_proc_fops, &sparc_iomap);
739 proc_create_data("dvma_map", 0, NULL, &sparc_io_proc_fops, &_sparc_dvma);
740 #endif