xref: /openbmc/linux/arch/powerpc/mm/ioremap_64.c (revision 4a45b7460cf458012a6930f675e141256b81dcf4)
1f381d571SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-or-later
2f381d571SChristophe Leroy 
3f381d571SChristophe Leroy #include <linux/io.h>
4f381d571SChristophe Leroy #include <linux/slab.h>
5f381d571SChristophe Leroy #include <linux/vmalloc.h>
6f381d571SChristophe Leroy 
7f381d571SChristophe Leroy /**
8f381d571SChristophe Leroy  * Low level function to establish the page tables for an IO mapping
9f381d571SChristophe Leroy  */
10f381d571SChristophe Leroy void __iomem *__ioremap_at(phys_addr_t pa, void *ea, unsigned long size, pgprot_t prot)
11f381d571SChristophe Leroy {
12f381d571SChristophe Leroy 	/* We don't support the 4K PFN hack with ioremap */
13f381d571SChristophe Leroy 	if (pgprot_val(prot) & H_PAGE_4K_PFN)
14f381d571SChristophe Leroy 		return NULL;
15f381d571SChristophe Leroy 
16f381d571SChristophe Leroy 	if ((ea + size) >= (void *)IOREMAP_END) {
17f381d571SChristophe Leroy 		pr_warn("Outside the supported range\n");
18f381d571SChristophe Leroy 		return NULL;
19f381d571SChristophe Leroy 	}
20f381d571SChristophe Leroy 
21f381d571SChristophe Leroy 	WARN_ON(pa & ~PAGE_MASK);
22f381d571SChristophe Leroy 	WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
23f381d571SChristophe Leroy 	WARN_ON(size & ~PAGE_MASK);
24f381d571SChristophe Leroy 
25191e4206SChristophe Leroy 	if (ioremap_range((unsigned long)ea, pa, size, prot))
26f381d571SChristophe Leroy 		return NULL;
27f381d571SChristophe Leroy 
28f381d571SChristophe Leroy 	return (void __iomem *)ea;
29f381d571SChristophe Leroy }
30f381d571SChristophe Leroy EXPORT_SYMBOL(__ioremap_at);
31f381d571SChristophe Leroy 
32f381d571SChristophe Leroy /**
33f381d571SChristophe Leroy  * Low level function to tear down the page tables for an IO mapping. This is
34f381d571SChristophe Leroy  * used for mappings that are manipulated manually, like partial unmapping of
35f381d571SChristophe Leroy  * PCI IOs or ISA space.
36f381d571SChristophe Leroy  */
37f381d571SChristophe Leroy void __iounmap_at(void *ea, unsigned long size)
38f381d571SChristophe Leroy {
39f381d571SChristophe Leroy 	WARN_ON(((unsigned long)ea) & ~PAGE_MASK);
40f381d571SChristophe Leroy 	WARN_ON(size & ~PAGE_MASK);
41f381d571SChristophe Leroy 
42f381d571SChristophe Leroy 	unmap_kernel_range((unsigned long)ea, size);
43f381d571SChristophe Leroy }
44f381d571SChristophe Leroy EXPORT_SYMBOL(__iounmap_at);
45f381d571SChristophe Leroy 
46f381d571SChristophe Leroy void __iomem *__ioremap_caller(phys_addr_t addr, unsigned long size,
47f381d571SChristophe Leroy 			       pgprot_t prot, void *caller)
48f381d571SChristophe Leroy {
49*4a45b746SChristophe Leroy 	phys_addr_t paligned, offset;
50f381d571SChristophe Leroy 	void __iomem *ret;
51f381d571SChristophe Leroy 
52*4a45b746SChristophe Leroy 	/* We don't support the 4K PFN hack with ioremap */
53*4a45b746SChristophe Leroy 	if (pgprot_val(prot) & H_PAGE_4K_PFN)
54*4a45b746SChristophe Leroy 		return NULL;
55*4a45b746SChristophe Leroy 
56f381d571SChristophe Leroy 	/*
57f381d571SChristophe Leroy 	 * Choose an address to map it to. Once the vmalloc system is running,
58f381d571SChristophe Leroy 	 * we use it. Before that, we map using addresses going up from
59f381d571SChristophe Leroy 	 * ioremap_bot.  vmalloc will use the addresses from IOREMAP_BASE
60f381d571SChristophe Leroy 	 * through ioremap_bot.
61f381d571SChristophe Leroy 	 */
62f381d571SChristophe Leroy 	paligned = addr & PAGE_MASK;
63*4a45b746SChristophe Leroy 	offset = addr & ~PAGE_MASK;
64f381d571SChristophe Leroy 	size = PAGE_ALIGN(addr + size) - paligned;
65f381d571SChristophe Leroy 
66f381d571SChristophe Leroy 	if (size == 0 || paligned == 0)
67f381d571SChristophe Leroy 		return NULL;
68f381d571SChristophe Leroy 
69f381d571SChristophe Leroy 	if (slab_is_available()) {
70*4a45b746SChristophe Leroy 		return do_ioremap(paligned, offset, size, prot, caller);
71f381d571SChristophe Leroy 	} else {
72f381d571SChristophe Leroy 		ret = __ioremap_at(paligned, (void *)ioremap_bot, size, prot);
73f381d571SChristophe Leroy 		if (ret)
74f381d571SChristophe Leroy 			ioremap_bot += size;
75f381d571SChristophe Leroy 	}
76f381d571SChristophe Leroy 
77f381d571SChristophe Leroy 	if (ret)
78f381d571SChristophe Leroy 		ret += addr & ~PAGE_MASK;
79f381d571SChristophe Leroy 	return ret;
80f381d571SChristophe Leroy }
81f381d571SChristophe Leroy 
82f381d571SChristophe Leroy /*
83f381d571SChristophe Leroy  * Unmap an IO region and remove it from vmalloc'd list.
84f381d571SChristophe Leroy  * Access to IO memory should be serialized by driver.
85f381d571SChristophe Leroy  */
86f381d571SChristophe Leroy void iounmap(volatile void __iomem *token)
87f381d571SChristophe Leroy {
88f381d571SChristophe Leroy 	void *addr;
89f381d571SChristophe Leroy 
90f381d571SChristophe Leroy 	if (!slab_is_available())
91f381d571SChristophe Leroy 		return;
92f381d571SChristophe Leroy 
93f381d571SChristophe Leroy 	addr = (void *)((unsigned long __force)PCI_FIX_ADDR(token) & PAGE_MASK);
94f381d571SChristophe Leroy 
95f381d571SChristophe Leroy 	if ((unsigned long)addr < ioremap_bot) {
96f381d571SChristophe Leroy 		pr_warn("Attempt to iounmap early bolted mapping at 0x%p\n", addr);
97f381d571SChristophe Leroy 		return;
98f381d571SChristophe Leroy 	}
99f381d571SChristophe Leroy 	vunmap(addr);
100f381d571SChristophe Leroy }
101f381d571SChristophe Leroy EXPORT_SYMBOL(iounmap);
102