xref: /openbmc/linux/arch/x86/mm/iomap_32.c (revision cdd38c5f1ce4398ec58fec95904b75824daab7b5)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fd940934SKeith Packard /*
3fd940934SKeith Packard  * Copyright © 2008 Ingo Molnar
4fd940934SKeith Packard  */
5fd940934SKeith Packard 
6fd940934SKeith Packard #include <asm/iomap.h>
7eb243d1dSIngo Molnar #include <asm/memtype.h>
84b599fedSPaul Gortmaker #include <linux/export.h>
97ca43e75SAkinobu Mita #include <linux/highmem.h>
10fd940934SKeith Packard 
is_io_mapping_possible(resource_size_t base,unsigned long size)119e36fda0SVenkatesh Pallipadi static int is_io_mapping_possible(resource_size_t base, unsigned long size)
124ab0d47dSVenkatesh Pallipadi {
136a491e2eSAndrew Morton #if !defined(CONFIG_X86_PAE) && defined(CONFIG_PHYS_ADDR_T_64BIT)
144ab0d47dSVenkatesh Pallipadi 	/* There is no way to map greater than 1 << 32 address without PAE */
154ab0d47dSVenkatesh Pallipadi 	if (base + size > 0x100000000ULL)
164ab0d47dSVenkatesh Pallipadi 		return 0;
1792b9af9eSIngo Molnar #endif
184ab0d47dSVenkatesh Pallipadi 	return 1;
194ab0d47dSVenkatesh Pallipadi }
209e36fda0SVenkatesh Pallipadi 
iomap_create_wc(resource_size_t base,unsigned long size,pgprot_t * prot)219e36fda0SVenkatesh Pallipadi int iomap_create_wc(resource_size_t base, unsigned long size, pgprot_t *prot)
229e36fda0SVenkatesh Pallipadi {
2349a3b3cbSJuergen Gross 	enum page_cache_mode pcm = _PAGE_CACHE_MODE_WC;
249e36fda0SVenkatesh Pallipadi 	int ret;
259e36fda0SVenkatesh Pallipadi 
269e36fda0SVenkatesh Pallipadi 	if (!is_io_mapping_possible(base, size))
279e36fda0SVenkatesh Pallipadi 		return -EINVAL;
289e36fda0SVenkatesh Pallipadi 
29ecdd6ee7SIngo Molnar 	ret = memtype_reserve_io(base, base + size, &pcm);
309e36fda0SVenkatesh Pallipadi 	if (ret)
319e36fda0SVenkatesh Pallipadi 		return ret;
329e36fda0SVenkatesh Pallipadi 
3349a3b3cbSJuergen Gross 	*prot = __pgprot(__PAGE_KERNEL | cachemode2protval(pcm));
34fb43d6cbSDave Hansen 	/* Filter out unsupported __PAGE_KERNEL* bits: */
35fb43d6cbSDave Hansen 	pgprot_val(*prot) &= __default_kernel_pte_mask;
36fb43d6cbSDave Hansen 
379e36fda0SVenkatesh Pallipadi 	return 0;
389e36fda0SVenkatesh Pallipadi }
399e36fda0SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(iomap_create_wc);
409e36fda0SVenkatesh Pallipadi 
iomap_free(resource_size_t base,unsigned long size)413e4d3af5SPeter Zijlstra void iomap_free(resource_size_t base, unsigned long size)
429e36fda0SVenkatesh Pallipadi {
43ecdd6ee7SIngo Molnar 	memtype_free_io(base, base + size);
449e36fda0SVenkatesh Pallipadi }
459e36fda0SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(iomap_free);
464ab0d47dSVenkatesh Pallipadi 
__iomap_local_pfn_prot(unsigned long pfn,pgprot_t prot)47*351191adSThomas Gleixner void __iomem *__iomap_local_pfn_prot(unsigned long pfn, pgprot_t prot)
48fd940934SKeith Packard {
49ef5fa0abSEric Anholt 	/*
507202fdb1SBorislav Petkov 	 * For non-PAT systems, translate non-WB request to UC- just in
517202fdb1SBorislav Petkov 	 * case the caller set the PWT bit to prot directly without using
527202fdb1SBorislav Petkov 	 * pgprot_writecombine(). UC- translates to uncached if the MTRR
537202fdb1SBorislav Petkov 	 * is UC or WC. UC- gets the real intention, of the user, which is
547202fdb1SBorislav Petkov 	 * "WC if the MTRR is WC, UC if you can't do that."
55ef5fa0abSEric Anholt 	 */
567202fdb1SBorislav Petkov 	if (!pat_enabled() && pgprot2cachemode(prot) != _PAGE_CACHE_MODE_WB)
5749a3b3cbSJuergen Gross 		prot = __pgprot(__PAGE_KERNEL |
5849a3b3cbSJuergen Gross 				cachemode2protval(_PAGE_CACHE_MODE_UC_MINUS));
59ef5fa0abSEric Anholt 
60fb43d6cbSDave Hansen 	/* Filter out unsupported __PAGE_KERNEL* bits: */
61fb43d6cbSDave Hansen 	pgprot_val(prot) &= __default_kernel_pte_mask;
62fb43d6cbSDave Hansen 
63157e118bSThomas Gleixner 	return (void __force __iomem *)__kmap_local_pfn_prot(pfn, prot);
64fd940934SKeith Packard }
65*351191adSThomas Gleixner EXPORT_SYMBOL_GPL(__iomap_local_pfn_prot);
66