^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2009 Wind River Systems Inc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2004 Microtronix Datacom Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static inline void remap_area_pte(pte_t *pte, unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned long size, unsigned long phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned long pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | _PAGE_READ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) | _PAGE_WRITE | flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) address &= ~PMD_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) end = address + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (end > PMD_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) end = PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (address >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pfn = PFN_DOWN(phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!pte_none(*pte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pr_err("remap_area_pte: page already exists\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) set_pte(pte, pfn_pte(pfn, pgprot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) address += PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) pfn++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) pte++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) } while (address && (address < end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static inline int remap_area_pmd(pmd_t *pmd, unsigned long address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned long size, unsigned long phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned long end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) address &= ~PGDIR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) end = address + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (end > PGDIR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) end = PGDIR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) phys_addr -= address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (address >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) pte_t *pte = pte_alloc_kernel(pmd, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) remap_area_pte(pte, address, end - address, address + phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) address = (address + PMD_SIZE) & PMD_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pmd++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) } while (address && (address < end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static int remap_area_pages(unsigned long address, unsigned long phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned long size, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) pgd_t *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned long end = address + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) phys_addr -= address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) dir = pgd_offset(&init_mm, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) flush_cache_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (address >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) p4d = p4d_alloc(&init_mm, dir, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!p4d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) pud = pud_alloc(&init_mm, p4d, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) pmd = pmd_alloc(&init_mm, pud, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (remap_area_pmd(pmd, address, end - address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) phys_addr + address, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) address = (address + PGDIR_SIZE) & PGDIR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dir++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) } while (address && (address < end));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) flush_tlb_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define IS_MAPPABLE_UNCACHEABLE(addr) (addr < 0x20000000UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Map some physical address range into the kernel address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void __iomem *ioremap(unsigned long phys_addr, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct vm_struct *area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned long last_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* Don't allow wraparound or zero size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) last_addr = phys_addr + size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!size || last_addr < phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Don't allow anybody to remap normal RAM that we're using */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (phys_addr > PHYS_OFFSET && phys_addr < virt_to_phys(high_memory)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) char *t_addr, *t_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) t_addr = __va(phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) t_end = t_addr + (size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) for (page = virt_to_page(t_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) page <= virt_to_page(t_end); page++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (!PageReserved(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Map uncached objects in the low part of address space to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * CONFIG_NIOS2_IO_REGION_BASE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (IS_MAPPABLE_UNCACHEABLE(phys_addr) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) IS_MAPPABLE_UNCACHEABLE(last_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return (void __iomem *)(CONFIG_NIOS2_IO_REGION_BASE + phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Mappings have to be page-aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) offset = phys_addr & ~PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) phys_addr &= PAGE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) size = PAGE_ALIGN(last_addr + 1) - phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /* Ok, go for it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) area = get_vm_area(size, VM_IOREMAP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!area)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) addr = area->addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (remap_area_pages((unsigned long) addr, phys_addr, size, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) vunmap(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return (void __iomem *) (offset + (char *)addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) EXPORT_SYMBOL(ioremap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * iounmap unmaps nearly everything, so be careful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * it doesn't free currently pointer/page tables anymore but it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * wasn't used anyway and might be added later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) void iounmap(void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct vm_struct *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if ((unsigned long) addr > CONFIG_NIOS2_IO_REGION_BASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) pr_err("iounmap: bad address %p\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) EXPORT_SYMBOL(iounmap);