18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * OpenRISC ioremap.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Linux architectural port borrowing liberally from similar works of 68c2ecf20Sopenharmony_ci * others. All original copyrights apply as per the original source 78c2ecf20Sopenharmony_ci * declaration. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Modifications for the OpenRISC architecture: 108c2ecf20Sopenharmony_ci * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com> 118c2ecf20Sopenharmony_ci * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se> 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 158c2ecf20Sopenharmony_ci#include <linux/io.h> 168c2ecf20Sopenharmony_ci#include <linux/pgtable.h> 178c2ecf20Sopenharmony_ci#include <asm/pgalloc.h> 188c2ecf20Sopenharmony_ci#include <asm/kmap_types.h> 198c2ecf20Sopenharmony_ci#include <asm/fixmap.h> 208c2ecf20Sopenharmony_ci#include <asm/bug.h> 218c2ecf20Sopenharmony_ci#include <linux/sched.h> 228c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ciextern int mem_init_done; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistatic unsigned int fixmaps_used __initdata; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* 298c2ecf20Sopenharmony_ci * Remap an arbitrary physical address space into the kernel virtual 308c2ecf20Sopenharmony_ci * address space. Needed when the kernel wants to access high addresses 318c2ecf20Sopenharmony_ci * directly. 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * NOTE! We need to allow non-page-aligned mappings too: we will obviously 348c2ecf20Sopenharmony_ci * have to convert them into an offset in a page-aligned mapping, but the 358c2ecf20Sopenharmony_ci * caller shouldn't need to know that small detail. 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_civoid __iomem *__ref ioremap(phys_addr_t addr, unsigned long size) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci phys_addr_t p; 408c2ecf20Sopenharmony_ci unsigned long v; 418c2ecf20Sopenharmony_ci unsigned long offset, last_addr; 428c2ecf20Sopenharmony_ci struct vm_struct *area = NULL; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci /* Don't allow wraparound or zero size */ 458c2ecf20Sopenharmony_ci last_addr = addr + size - 1; 468c2ecf20Sopenharmony_ci if (!size || last_addr < addr) 478c2ecf20Sopenharmony_ci return NULL; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci /* 508c2ecf20Sopenharmony_ci * Mappings have to be page-aligned 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_ci offset = addr & ~PAGE_MASK; 538c2ecf20Sopenharmony_ci p = addr & PAGE_MASK; 548c2ecf20Sopenharmony_ci size = PAGE_ALIGN(last_addr + 1) - p; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci if (likely(mem_init_done)) { 578c2ecf20Sopenharmony_ci area = get_vm_area(size, VM_IOREMAP); 588c2ecf20Sopenharmony_ci if (!area) 598c2ecf20Sopenharmony_ci return NULL; 608c2ecf20Sopenharmony_ci v = (unsigned long)area->addr; 618c2ecf20Sopenharmony_ci } else { 628c2ecf20Sopenharmony_ci if ((fixmaps_used + (size >> PAGE_SHIFT)) > FIX_N_IOREMAPS) 638c2ecf20Sopenharmony_ci return NULL; 648c2ecf20Sopenharmony_ci v = fix_to_virt(FIX_IOREMAP_BEGIN + fixmaps_used); 658c2ecf20Sopenharmony_ci fixmaps_used += (size >> PAGE_SHIFT); 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci if (ioremap_page_range(v, v + size, p, 698c2ecf20Sopenharmony_ci __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_CI))) { 708c2ecf20Sopenharmony_ci if (likely(mem_init_done)) 718c2ecf20Sopenharmony_ci vfree(area->addr); 728c2ecf20Sopenharmony_ci else 738c2ecf20Sopenharmony_ci fixmaps_used -= (size >> PAGE_SHIFT); 748c2ecf20Sopenharmony_ci return NULL; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci return (void __iomem *)(offset + (char *)v); 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ioremap); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_civoid iounmap(void *addr) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci /* If the page is from the fixmap pool then we just clear out 848c2ecf20Sopenharmony_ci * the fixmap mapping. 858c2ecf20Sopenharmony_ci */ 868c2ecf20Sopenharmony_ci if (unlikely((unsigned long)addr > FIXADDR_START)) { 878c2ecf20Sopenharmony_ci /* This is a bit broken... we don't really know 888c2ecf20Sopenharmony_ci * how big the area is so it's difficult to know 898c2ecf20Sopenharmony_ci * how many fixed pages to invalidate... 908c2ecf20Sopenharmony_ci * just flush tlb and hope for the best... 918c2ecf20Sopenharmony_ci * consider this a FIXME 928c2ecf20Sopenharmony_ci * 938c2ecf20Sopenharmony_ci * Really we should be clearing out one or more page 948c2ecf20Sopenharmony_ci * table entries for these virtual addresses so that 958c2ecf20Sopenharmony_ci * future references cause a page fault... for now, we 968c2ecf20Sopenharmony_ci * rely on two things: 978c2ecf20Sopenharmony_ci * i) this code never gets called on known boards 988c2ecf20Sopenharmony_ci * ii) invalid accesses to the freed areas aren't made 998c2ecf20Sopenharmony_ci */ 1008c2ecf20Sopenharmony_ci flush_tlb_all(); 1018c2ecf20Sopenharmony_ci return; 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci return vfree((void *)(PAGE_MASK & (unsigned long)addr)); 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(iounmap); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci/** 1098c2ecf20Sopenharmony_ci * OK, this one's a bit tricky... ioremap can get called before memory is 1108c2ecf20Sopenharmony_ci * initialized (early serial console does this) and will want to alloc a page 1118c2ecf20Sopenharmony_ci * for its mapping. No userspace pages will ever get allocated before memory 1128c2ecf20Sopenharmony_ci * is initialized so this applies only to kernel pages. In the event that 1138c2ecf20Sopenharmony_ci * this is called before memory is initialized we allocate the page using 1148c2ecf20Sopenharmony_ci * the memblock infrastructure. 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cipte_t __ref *pte_alloc_one_kernel(struct mm_struct *mm) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci pte_t *pte; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (likely(mem_init_done)) { 1228c2ecf20Sopenharmony_ci pte = (pte_t *)get_zeroed_page(GFP_KERNEL); 1238c2ecf20Sopenharmony_ci } else { 1248c2ecf20Sopenharmony_ci pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE); 1258c2ecf20Sopenharmony_ci if (!pte) 1268c2ecf20Sopenharmony_ci panic("%s: Failed to allocate %lu bytes align=0x%lx\n", 1278c2ecf20Sopenharmony_ci __func__, PAGE_SIZE, PAGE_SIZE); 1288c2ecf20Sopenharmony_ci } 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci return pte; 1318c2ecf20Sopenharmony_ci} 132