1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * OpenRISC ioremap.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Linux architectural port borrowing liberally from similar works of
6*4882a593Smuzhiyun * others. All original copyrights apply as per the original source
7*4882a593Smuzhiyun * declaration.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Modifications for the OpenRISC architecture:
10*4882a593Smuzhiyun * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11*4882a593Smuzhiyun * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/vmalloc.h>
15*4882a593Smuzhiyun #include <linux/io.h>
16*4882a593Smuzhiyun #include <linux/pgtable.h>
17*4882a593Smuzhiyun #include <asm/pgalloc.h>
18*4882a593Smuzhiyun #include <asm/kmap_types.h>
19*4882a593Smuzhiyun #include <asm/fixmap.h>
20*4882a593Smuzhiyun #include <asm/bug.h>
21*4882a593Smuzhiyun #include <linux/sched.h>
22*4882a593Smuzhiyun #include <asm/tlbflush.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun extern int mem_init_done;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun static unsigned int fixmaps_used __initdata;
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * Remap an arbitrary physical address space into the kernel virtual
30*4882a593Smuzhiyun * address space. Needed when the kernel wants to access high addresses
31*4882a593Smuzhiyun * directly.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * NOTE! We need to allow non-page-aligned mappings too: we will obviously
34*4882a593Smuzhiyun * have to convert them into an offset in a page-aligned mapping, but the
35*4882a593Smuzhiyun * caller shouldn't need to know that small detail.
36*4882a593Smuzhiyun */
ioremap(phys_addr_t addr,unsigned long size)37*4882a593Smuzhiyun void __iomem *__ref ioremap(phys_addr_t addr, unsigned long size)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun phys_addr_t p;
40*4882a593Smuzhiyun unsigned long v;
41*4882a593Smuzhiyun unsigned long offset, last_addr;
42*4882a593Smuzhiyun struct vm_struct *area = NULL;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* Don't allow wraparound or zero size */
45*4882a593Smuzhiyun last_addr = addr + size - 1;
46*4882a593Smuzhiyun if (!size || last_addr < addr)
47*4882a593Smuzhiyun return NULL;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun * Mappings have to be page-aligned
51*4882a593Smuzhiyun */
52*4882a593Smuzhiyun offset = addr & ~PAGE_MASK;
53*4882a593Smuzhiyun p = addr & PAGE_MASK;
54*4882a593Smuzhiyun size = PAGE_ALIGN(last_addr + 1) - p;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun if (likely(mem_init_done)) {
57*4882a593Smuzhiyun area = get_vm_area(size, VM_IOREMAP);
58*4882a593Smuzhiyun if (!area)
59*4882a593Smuzhiyun return NULL;
60*4882a593Smuzhiyun v = (unsigned long)area->addr;
61*4882a593Smuzhiyun } else {
62*4882a593Smuzhiyun if ((fixmaps_used + (size >> PAGE_SHIFT)) > FIX_N_IOREMAPS)
63*4882a593Smuzhiyun return NULL;
64*4882a593Smuzhiyun v = fix_to_virt(FIX_IOREMAP_BEGIN + fixmaps_used);
65*4882a593Smuzhiyun fixmaps_used += (size >> PAGE_SHIFT);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun if (ioremap_page_range(v, v + size, p,
69*4882a593Smuzhiyun __pgprot(pgprot_val(PAGE_KERNEL) | _PAGE_CI))) {
70*4882a593Smuzhiyun if (likely(mem_init_done))
71*4882a593Smuzhiyun vfree(area->addr);
72*4882a593Smuzhiyun else
73*4882a593Smuzhiyun fixmaps_used -= (size >> PAGE_SHIFT);
74*4882a593Smuzhiyun return NULL;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun return (void __iomem *)(offset + (char *)v);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun EXPORT_SYMBOL(ioremap);
80*4882a593Smuzhiyun
iounmap(void * addr)81*4882a593Smuzhiyun void iounmap(void *addr)
82*4882a593Smuzhiyun {
83*4882a593Smuzhiyun /* If the page is from the fixmap pool then we just clear out
84*4882a593Smuzhiyun * the fixmap mapping.
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun if (unlikely((unsigned long)addr > FIXADDR_START)) {
87*4882a593Smuzhiyun /* This is a bit broken... we don't really know
88*4882a593Smuzhiyun * how big the area is so it's difficult to know
89*4882a593Smuzhiyun * how many fixed pages to invalidate...
90*4882a593Smuzhiyun * just flush tlb and hope for the best...
91*4882a593Smuzhiyun * consider this a FIXME
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * Really we should be clearing out one or more page
94*4882a593Smuzhiyun * table entries for these virtual addresses so that
95*4882a593Smuzhiyun * future references cause a page fault... for now, we
96*4882a593Smuzhiyun * rely on two things:
97*4882a593Smuzhiyun * i) this code never gets called on known boards
98*4882a593Smuzhiyun * ii) invalid accesses to the freed areas aren't made
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun flush_tlb_all();
101*4882a593Smuzhiyun return;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun return vfree((void *)(PAGE_MASK & (unsigned long)addr));
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun EXPORT_SYMBOL(iounmap);
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun * OK, this one's a bit tricky... ioremap can get called before memory is
110*4882a593Smuzhiyun * initialized (early serial console does this) and will want to alloc a page
111*4882a593Smuzhiyun * for its mapping. No userspace pages will ever get allocated before memory
112*4882a593Smuzhiyun * is initialized so this applies only to kernel pages. In the event that
113*4882a593Smuzhiyun * this is called before memory is initialized we allocate the page using
114*4882a593Smuzhiyun * the memblock infrastructure.
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun
pte_alloc_one_kernel(struct mm_struct * mm)117*4882a593Smuzhiyun pte_t __ref *pte_alloc_one_kernel(struct mm_struct *mm)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun pte_t *pte;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (likely(mem_init_done)) {
122*4882a593Smuzhiyun pte = (pte_t *)get_zeroed_page(GFP_KERNEL);
123*4882a593Smuzhiyun } else {
124*4882a593Smuzhiyun pte = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
125*4882a593Smuzhiyun if (!pte)
126*4882a593Smuzhiyun panic("%s: Failed to allocate %lu bytes align=0x%lx\n",
127*4882a593Smuzhiyun __func__, PAGE_SIZE, PAGE_SIZE);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun return pte;
131*4882a593Smuzhiyun }
132