10b59e38fSPaul Mundt /*
20b59e38fSPaul Mundt * arch/sh/mm/ioremap.c
30b59e38fSPaul Mundt *
40b59e38fSPaul Mundt * (C) Copyright 1995 1996 Linus Torvalds
50b59e38fSPaul Mundt * (C) Copyright 2005 - 2010 Paul Mundt
60b59e38fSPaul Mundt *
70b59e38fSPaul Mundt * Re-map IO memory to kernel address space so that we can access it.
80b59e38fSPaul Mundt * This is needed for high PCI addresses that aren't mapped in the
90b59e38fSPaul Mundt * 640k-1MB IO memory area on PC's
100b59e38fSPaul Mundt *
110b59e38fSPaul Mundt * This file is subject to the terms and conditions of the GNU General
120b59e38fSPaul Mundt * Public License. See the file "COPYING" in the main directory of this
130b59e38fSPaul Mundt * archive for more details.
140b59e38fSPaul Mundt */
150b59e38fSPaul Mundt #include <linux/vmalloc.h>
160b59e38fSPaul Mundt #include <linux/module.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
180b59e38fSPaul Mundt #include <linux/mm.h>
190b59e38fSPaul Mundt #include <linux/pci.h>
200b59e38fSPaul Mundt #include <linux/io.h>
2108732d12SChristoph Hellwig #include <asm/io_trapped.h>
220b59e38fSPaul Mundt #include <asm/page.h>
230b59e38fSPaul Mundt #include <asm/pgalloc.h>
240b59e38fSPaul Mundt #include <asm/addrspace.h>
250b59e38fSPaul Mundt #include <asm/cacheflush.h>
260b59e38fSPaul Mundt #include <asm/tlbflush.h>
270b59e38fSPaul Mundt #include <asm/mmu.h>
283eef6b74SChristoph Hellwig #include "ioremap.h"
290b59e38fSPaul Mundt
300b59e38fSPaul Mundt /*
3113f1fc87SChristoph Hellwig * On 32-bit SH, we traditionally have the whole physical address space mapped
3213f1fc87SChristoph Hellwig * at all times (as MIPS does), so "ioremap()" and "iounmap()" do not need to do
3313f1fc87SChristoph Hellwig * anything but place the address in the proper segment. This is true for P1
3413f1fc87SChristoph Hellwig * and P2 addresses, as well as some P3 ones. However, most of the P3 addresses
3513f1fc87SChristoph Hellwig * and newer cores using extended addressing need to map through page tables, so
3613f1fc87SChristoph Hellwig * the ioremap() implementation becomes a bit more complicated.
3713f1fc87SChristoph Hellwig */
3813f1fc87SChristoph Hellwig #ifdef CONFIG_29BIT
3913f1fc87SChristoph Hellwig static void __iomem *
__ioremap_29bit(phys_addr_t offset,unsigned long size,pgprot_t prot)4013f1fc87SChristoph Hellwig __ioremap_29bit(phys_addr_t offset, unsigned long size, pgprot_t prot)
4113f1fc87SChristoph Hellwig {
4213f1fc87SChristoph Hellwig phys_addr_t last_addr = offset + size - 1;
4313f1fc87SChristoph Hellwig
4413f1fc87SChristoph Hellwig /*
4513f1fc87SChristoph Hellwig * For P1 and P2 space this is trivial, as everything is already
4613f1fc87SChristoph Hellwig * mapped. Uncached access for P1 addresses are done through P2.
4713f1fc87SChristoph Hellwig * In the P3 case or for addresses outside of the 29-bit space,
4813f1fc87SChristoph Hellwig * mapping must be done by the PMB or by using page tables.
4913f1fc87SChristoph Hellwig */
5013f1fc87SChristoph Hellwig if (likely(PXSEG(offset) < P3SEG && PXSEG(last_addr) < P3SEG)) {
5113f1fc87SChristoph Hellwig u64 flags = pgprot_val(prot);
5213f1fc87SChristoph Hellwig
5313f1fc87SChristoph Hellwig /*
5413f1fc87SChristoph Hellwig * Anything using the legacy PTEA space attributes needs
5513f1fc87SChristoph Hellwig * to be kicked down to page table mappings.
5613f1fc87SChristoph Hellwig */
5713f1fc87SChristoph Hellwig if (unlikely(flags & _PAGE_PCC_MASK))
5813f1fc87SChristoph Hellwig return NULL;
5913f1fc87SChristoph Hellwig if (unlikely(flags & _PAGE_CACHABLE))
6013f1fc87SChristoph Hellwig return (void __iomem *)P1SEGADDR(offset);
6113f1fc87SChristoph Hellwig
6213f1fc87SChristoph Hellwig return (void __iomem *)P2SEGADDR(offset);
6313f1fc87SChristoph Hellwig }
6413f1fc87SChristoph Hellwig
6513f1fc87SChristoph Hellwig /* P4 above the store queues are always mapped. */
6613f1fc87SChristoph Hellwig if (unlikely(offset >= P3_ADDR_MAX))
6713f1fc87SChristoph Hellwig return (void __iomem *)P4SEGADDR(offset);
6813f1fc87SChristoph Hellwig
6913f1fc87SChristoph Hellwig return NULL;
7013f1fc87SChristoph Hellwig }
7113f1fc87SChristoph Hellwig #else
7213f1fc87SChristoph Hellwig #define __ioremap_29bit(offset, size, prot) NULL
7313f1fc87SChristoph Hellwig #endif /* CONFIG_29BIT */
7413f1fc87SChristoph Hellwig
ioremap_prot(phys_addr_t phys_addr,size_t size,unsigned long prot)75*e72590faSGeert Uytterhoeven void __iomem __ref *ioremap_prot(phys_addr_t phys_addr, size_t size,
760453c9a7SBaoquan He unsigned long prot)
770b59e38fSPaul Mundt {
7890e7d649SPaul Mundt void __iomem *mapped;
790453c9a7SBaoquan He pgprot_t pgprot = __pgprot(prot);
800b59e38fSPaul Mundt
8113f1fc87SChristoph Hellwig mapped = __ioremap_trapped(phys_addr, size);
8213f1fc87SChristoph Hellwig if (mapped)
8313f1fc87SChristoph Hellwig return mapped;
8413f1fc87SChristoph Hellwig
8513f1fc87SChristoph Hellwig mapped = __ioremap_29bit(phys_addr, size, pgprot);
8613f1fc87SChristoph Hellwig if (mapped)
8713f1fc87SChristoph Hellwig return mapped;
8813f1fc87SChristoph Hellwig
890b59e38fSPaul Mundt /*
9090e7d649SPaul Mundt * If we can't yet use the regular approach, go the fixmap route.
9190e7d649SPaul Mundt */
9290e7d649SPaul Mundt if (!mem_init_done)
9390e7d649SPaul Mundt return ioremap_fixed(phys_addr, size, pgprot);
9490e7d649SPaul Mundt
9590e7d649SPaul Mundt /*
9690e7d649SPaul Mundt * First try to remap through the PMB.
9790e7d649SPaul Mundt * PMB entries are all pre-faulted.
9890e7d649SPaul Mundt */
990453c9a7SBaoquan He mapped = pmb_remap_caller(phys_addr, size, pgprot,
1000453c9a7SBaoquan He __builtin_return_address(0));
10190e7d649SPaul Mundt if (mapped && !IS_ERR(mapped))
10290e7d649SPaul Mundt return mapped;
10390e7d649SPaul Mundt
1040453c9a7SBaoquan He return generic_ioremap_prot(phys_addr, size, pgprot);
1050b59e38fSPaul Mundt }
1060453c9a7SBaoquan He EXPORT_SYMBOL(ioremap_prot);
1070b59e38fSPaul Mundt
1080b59e38fSPaul Mundt /*
1090b59e38fSPaul Mundt * Simple checks for non-translatable mappings.
1100b59e38fSPaul Mundt */
iomapping_nontranslatable(unsigned long offset)1110b59e38fSPaul Mundt static inline int iomapping_nontranslatable(unsigned long offset)
1120b59e38fSPaul Mundt {
1130b59e38fSPaul Mundt #ifdef CONFIG_29BIT
1140b59e38fSPaul Mundt /*
1150b59e38fSPaul Mundt * In 29-bit mode this includes the fixed P1/P2 areas, as well as
1160b59e38fSPaul Mundt * parts of P3.
1170b59e38fSPaul Mundt */
1180b59e38fSPaul Mundt if (PXSEG(offset) < P3SEG || offset >= P3_ADDR_MAX)
1190b59e38fSPaul Mundt return 1;
1200b59e38fSPaul Mundt #endif
1210b59e38fSPaul Mundt
1220b59e38fSPaul Mundt return 0;
1230b59e38fSPaul Mundt }
1240b59e38fSPaul Mundt
iounmap(volatile void __iomem * addr)1250453c9a7SBaoquan He void iounmap(volatile void __iomem *addr)
1260b59e38fSPaul Mundt {
1270b59e38fSPaul Mundt unsigned long vaddr = (unsigned long __force)addr;
1280b59e38fSPaul Mundt
1290b59e38fSPaul Mundt /*
1300b59e38fSPaul Mundt * Nothing to do if there is no translatable mapping.
1310b59e38fSPaul Mundt */
1320b59e38fSPaul Mundt if (iomapping_nontranslatable(vaddr))
1330b59e38fSPaul Mundt return;
1340b59e38fSPaul Mundt
13512b6b01cSPaul Mundt /*
13612b6b01cSPaul Mundt * There's no VMA if it's from an early fixed mapping.
13712b6b01cSPaul Mundt */
1380453c9a7SBaoquan He if (iounmap_fixed((void __iomem *)addr) == 0)
13912b6b01cSPaul Mundt return;
14012b6b01cSPaul Mundt
1410b59e38fSPaul Mundt /*
14290e7d649SPaul Mundt * If the PMB handled it, there's nothing else to do.
1430b59e38fSPaul Mundt */
1440453c9a7SBaoquan He if (pmb_unmap((void __iomem *)addr) == 0)
14590e7d649SPaul Mundt return;
1460b59e38fSPaul Mundt
1470453c9a7SBaoquan He generic_iounmap(addr);
1480b59e38fSPaul Mundt }
14998c90e5eSChristoph Hellwig EXPORT_SYMBOL(iounmap);
150