/*1* arch/sh/mm/ioremap.c2*3* (C) Copyright 1995 1996 Linus Torvalds4* (C) Copyright 2005 - 2010 Paul Mundt5*6* Re-map IO memory to kernel address space so that we can access it.7* This is needed for high PCI addresses that aren't mapped in the8* 640k-1MB IO memory area on PC's9*10* This file is subject to the terms and conditions of the GNU General11* Public License. See the file "COPYING" in the main directory of this12* archive for more details.13*/14#include <linux/vmalloc.h>15#include <linux/module.h>16#include <linux/slab.h>17#include <linux/mm.h>18#include <linux/pci.h>19#include <linux/io.h>20#include <asm/io_trapped.h>21#include <asm/page.h>22#include <asm/pgalloc.h>23#include <asm/addrspace.h>24#include <asm/cacheflush.h>25#include <asm/tlbflush.h>26#include <asm/mmu.h>27#include "ioremap.h"2829/*30* On 32-bit SH, we traditionally have the whole physical address space mapped31* at all times (as MIPS does), so "ioremap()" and "iounmap()" do not need to do32* anything but place the address in the proper segment. This is true for P133* and P2 addresses, as well as some P3 ones. However, most of the P3 addresses34* and newer cores using extended addressing need to map through page tables, so35* the ioremap() implementation becomes a bit more complicated.36*/37#ifdef CONFIG_29BIT38static void __iomem *39__ioremap_29bit(phys_addr_t offset, unsigned long size, pgprot_t prot)40{41phys_addr_t last_addr = offset + size - 1;4243/*44* For P1 and P2 space this is trivial, as everything is already45* mapped. Uncached access for P1 addresses are done through P2.46* In the P3 case or for addresses outside of the 29-bit space,47* mapping must be done by the PMB or by using page tables.48*/49if (likely(PXSEG(offset) < P3SEG && PXSEG(last_addr) < P3SEG)) {50u64 flags = pgprot_val(prot);5152/*53* Anything using the legacy PTEA space attributes needs54* to be kicked down to page table mappings.55*/56if (unlikely(flags & _PAGE_PCC_MASK))57return NULL;58if (unlikely(flags & _PAGE_CACHABLE))59return (void __iomem *)P1SEGADDR(offset);6061return (void __iomem *)P2SEGADDR(offset);62}6364/* P4 above the store queues are always mapped. */65if (unlikely(offset >= P3_ADDR_MAX))66return (void __iomem *)P4SEGADDR(offset);6768return NULL;69}70#else71#define __ioremap_29bit(offset, size, prot) NULL72#endif /* CONFIG_29BIT */7374void __iomem __ref *ioremap_prot(phys_addr_t phys_addr, size_t size,75pgprot_t pgprot)76{77void __iomem *mapped;7879mapped = __ioremap_trapped(phys_addr, size);80if (mapped)81return mapped;8283mapped = __ioremap_29bit(phys_addr, size, pgprot);84if (mapped)85return mapped;8687/*88* If we can't yet use the regular approach, go the fixmap route.89*/90if (!mem_init_done)91return ioremap_fixed(phys_addr, size, pgprot);9293/*94* First try to remap through the PMB.95* PMB entries are all pre-faulted.96*/97mapped = pmb_remap_caller(phys_addr, size, pgprot,98__builtin_return_address(0));99if (mapped && !IS_ERR(mapped))100return mapped;101102return generic_ioremap_prot(phys_addr, size, pgprot);103}104EXPORT_SYMBOL(ioremap_prot);105106/*107* Simple checks for non-translatable mappings.108*/109static inline int iomapping_nontranslatable(unsigned long offset)110{111#ifdef CONFIG_29BIT112/*113* In 29-bit mode this includes the fixed P1/P2 areas, as well as114* parts of P3.115*/116if (PXSEG(offset) < P3SEG || offset >= P3_ADDR_MAX)117return 1;118#endif119120return 0;121}122123void iounmap(volatile void __iomem *addr)124{125unsigned long vaddr = (unsigned long __force)addr;126127/*128* Nothing to do if there is no translatable mapping.129*/130if (iomapping_nontranslatable(vaddr))131return;132133/*134* There's no VMA if it's from an early fixed mapping.135*/136if (iounmap_fixed((void __iomem *)addr) == 0)137return;138139/*140* If the PMB handled it, there's nothing else to do.141*/142if (pmb_unmap((void __iomem *)addr) == 0)143return;144145generic_iounmap(addr);146}147EXPORT_SYMBOL(iounmap);148149150