xref: /openbmc/linux/arch/xtensa/include/asm/page.h (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1367b8112SChris Zankel /*
2367b8112SChris Zankel  * include/asm-xtensa/page.h
3367b8112SChris Zankel  *
4367b8112SChris Zankel  * This program is free software; you can redistribute it and/or modify
5367b8112SChris Zankel  * it under the terms of the GNU General Public License version2 as
6367b8112SChris Zankel  * published by the Free Software Foundation.
7367b8112SChris Zankel  *
8367b8112SChris Zankel  * Copyright (C) 2001 - 2007 Tensilica Inc.
9367b8112SChris Zankel  */
10367b8112SChris Zankel 
11367b8112SChris Zankel #ifndef _XTENSA_PAGE_H
12367b8112SChris Zankel #define _XTENSA_PAGE_H
13367b8112SChris Zankel 
14*e5080a96SMike Rapoport (IBM) #include <linux/const.h>
15*e5080a96SMike Rapoport (IBM) 
16367b8112SChris Zankel #include <asm/processor.h>
17367b8112SChris Zankel #include <asm/types.h>
18367b8112SChris Zankel #include <asm/cache.h>
19f1883aa7SMax Filippov #include <asm/kmem_layout.h>
20367b8112SChris Zankel 
21367b8112SChris Zankel /*
22367b8112SChris Zankel  * PAGE_SHIFT determines the page size
23367b8112SChris Zankel  */
24367b8112SChris Zankel 
25367b8112SChris Zankel #define PAGE_SHIFT	12
26367b8112SChris Zankel #define PAGE_SIZE	(__XTENSA_UL_CONST(1) << PAGE_SHIFT)
27367b8112SChris Zankel #define PAGE_MASK	(~(PAGE_SIZE-1))
28367b8112SChris Zankel 
29e5083a63SJohannes Weiner #ifdef CONFIG_MMU
30367b8112SChris Zankel #define PAGE_OFFSET	XCHAL_KSEG_CACHED_VADDR
31a9f2fc62SMax Filippov #define PHYS_OFFSET	XCHAL_KSEG_PADDR
32d39af902SMax Filippov #define MAX_LOW_PFN	(PHYS_PFN(XCHAL_KSEG_PADDR) + \
33d39af902SMax Filippov 			 PHYS_PFN(XCHAL_KSEG_SIZE))
34e5083a63SJohannes Weiner #else
35aea731c8SMax Filippov #define PAGE_OFFSET	_AC(CONFIG_DEFAULT_MEM_START, UL)
36aea731c8SMax Filippov #define PHYS_OFFSET	_AC(CONFIG_DEFAULT_MEM_START, UL)
373de00482SMax Filippov #define MAX_LOW_PFN	PHYS_PFN(0xfffffffful)
38e5083a63SJohannes Weiner #endif
39e5083a63SJohannes Weiner 
40367b8112SChris Zankel /*
41367b8112SChris Zankel  * Cache aliasing:
42367b8112SChris Zankel  *
43367b8112SChris Zankel  * If the cache size for one way is greater than the page size, we have to
44367b8112SChris Zankel  * deal with cache aliasing. The cache index is wider than the page size:
45367b8112SChris Zankel  *
46367b8112SChris Zankel  * |    |cache| cache index
47367b8112SChris Zankel  * | pfn  |off|	virtual address
48367b8112SChris Zankel  * |xxxx:X|zzz|
49367b8112SChris Zankel  * |    : |   |
50367b8112SChris Zankel  * | \  / |   |
51367b8112SChris Zankel  * |trans.|   |
52367b8112SChris Zankel  * | /  \ |   |
53367b8112SChris Zankel  * |yyyy:Y|zzz|	physical address
54367b8112SChris Zankel  *
55367b8112SChris Zankel  * When the page number is translated to the physical page address, the lowest
56367b8112SChris Zankel  * bit(s) (X) that are part of the cache index are also translated (Y).
57367b8112SChris Zankel  * If this translation changes bit(s) (X), the cache index is also afected,
58367b8112SChris Zankel  * thus resulting in a different cache line than before.
59367b8112SChris Zankel  * The kernel does not provide a mechanism to ensure that the page color
60367b8112SChris Zankel  * (represented by this bit) remains the same when allocated or when pages
61367b8112SChris Zankel  * are remapped. When user pages are mapped into kernel space, the color of
62367b8112SChris Zankel  * the page might also change.
63367b8112SChris Zankel  *
64367b8112SChris Zankel  * We use the address space VMALLOC_END ... VMALLOC_END + DCACHE_WAY_SIZE * 2
65367b8112SChris Zankel  * to temporarily map a patch so we can match the color.
66367b8112SChris Zankel  */
67367b8112SChris Zankel 
68367b8112SChris Zankel #if DCACHE_WAY_SIZE > PAGE_SIZE
69367b8112SChris Zankel # define DCACHE_ALIAS_ORDER	(DCACHE_WAY_SHIFT - PAGE_SHIFT)
70367b8112SChris Zankel # define DCACHE_ALIAS_MASK	(PAGE_MASK & (DCACHE_WAY_SIZE - 1))
71367b8112SChris Zankel # define DCACHE_ALIAS(a)	(((a) & DCACHE_ALIAS_MASK) >> PAGE_SHIFT)
72367b8112SChris Zankel # define DCACHE_ALIAS_EQ(a,b)	((((a) ^ (b)) & DCACHE_ALIAS_MASK) == 0)
73367b8112SChris Zankel #else
74367b8112SChris Zankel # define DCACHE_ALIAS_ORDER	0
7532544d9cSMax Filippov # define DCACHE_ALIAS(a)	((void)(a), 0)
76367b8112SChris Zankel #endif
7732544d9cSMax Filippov #define DCACHE_N_COLORS		(1 << DCACHE_ALIAS_ORDER)
78367b8112SChris Zankel 
79367b8112SChris Zankel #if ICACHE_WAY_SIZE > PAGE_SIZE
80367b8112SChris Zankel # define ICACHE_ALIAS_ORDER	(ICACHE_WAY_SHIFT - PAGE_SHIFT)
81367b8112SChris Zankel # define ICACHE_ALIAS_MASK	(PAGE_MASK & (ICACHE_WAY_SIZE - 1))
82367b8112SChris Zankel # define ICACHE_ALIAS(a)	(((a) & ICACHE_ALIAS_MASK) >> PAGE_SHIFT)
83367b8112SChris Zankel # define ICACHE_ALIAS_EQ(a,b)	((((a) ^ (b)) & ICACHE_ALIAS_MASK) == 0)
84367b8112SChris Zankel #else
85367b8112SChris Zankel # define ICACHE_ALIAS_ORDER	0
86367b8112SChris Zankel #endif
87367b8112SChris Zankel 
88367b8112SChris Zankel 
89367b8112SChris Zankel #ifdef __ASSEMBLY__
90367b8112SChris Zankel 
91367b8112SChris Zankel #define __pgprot(x)	(x)
92367b8112SChris Zankel 
93367b8112SChris Zankel #else
94367b8112SChris Zankel 
95367b8112SChris Zankel /*
96367b8112SChris Zankel  * These are used to make use of C type-checking..
97367b8112SChris Zankel  */
98367b8112SChris Zankel 
99367b8112SChris Zankel typedef struct { unsigned long pte; } pte_t;		/* page table entry */
100367b8112SChris Zankel typedef struct { unsigned long pgd; } pgd_t;		/* PGD table entry */
101367b8112SChris Zankel typedef struct { unsigned long pgprot; } pgprot_t;
102367b8112SChris Zankel typedef struct page *pgtable_t;
103367b8112SChris Zankel 
104367b8112SChris Zankel #define pte_val(x)	((x).pte)
105367b8112SChris Zankel #define pgd_val(x)	((x).pgd)
106367b8112SChris Zankel #define pgprot_val(x)	((x).pgprot)
107367b8112SChris Zankel 
108367b8112SChris Zankel #define __pte(x)	((pte_t) { (x) } )
109367b8112SChris Zankel #define __pgd(x)	((pgd_t) { (x) } )
110367b8112SChris Zankel #define __pgprot(x)	((pgprot_t) { (x) } )
111367b8112SChris Zankel 
112367b8112SChris Zankel /*
113367b8112SChris Zankel  * Pure 2^n version of get_order
114367b8112SChris Zankel  * Use 'nsau' instructions if supported by the processor or the generic version.
115367b8112SChris Zankel  */
116367b8112SChris Zankel 
117367b8112SChris Zankel #if XCHAL_HAVE_NSA
118367b8112SChris Zankel 
get_order(unsigned long size)119367b8112SChris Zankel static inline __attribute_const__ int get_order(unsigned long size)
120367b8112SChris Zankel {
121367b8112SChris Zankel 	int lz;
122367b8112SChris Zankel 	asm ("nsau %0, %1" : "=r" (lz) : "r" ((size - 1) >> PAGE_SHIFT));
123367b8112SChris Zankel 	return 32 - lz;
124367b8112SChris Zankel }
125367b8112SChris Zankel 
126367b8112SChris Zankel #else
127367b8112SChris Zankel 
1285b17e1cdSArnd Bergmann # include <asm-generic/getorder.h>
129367b8112SChris Zankel 
130367b8112SChris Zankel #endif
131367b8112SChris Zankel 
132367b8112SChris Zankel struct page;
133a91902dbSMax Filippov struct vm_area_struct;
134367b8112SChris Zankel extern void clear_page(void *page);
135367b8112SChris Zankel extern void copy_page(void *to, void *from);
136367b8112SChris Zankel 
137367b8112SChris Zankel /*
138367b8112SChris Zankel  * If we have cache aliasing and writeback caches, we might have to do
139367b8112SChris Zankel  * some extra work
140367b8112SChris Zankel  */
141367b8112SChris Zankel 
142b6cee17bSMax Filippov #if defined(CONFIG_MMU) && DCACHE_WAY_SIZE > PAGE_SIZE
143a91902dbSMax Filippov extern void clear_page_alias(void *vaddr, unsigned long paddr);
144a91902dbSMax Filippov extern void copy_page_alias(void *to, void *from,
145a91902dbSMax Filippov 			    unsigned long to_paddr, unsigned long from_paddr);
146a91902dbSMax Filippov 
147a91902dbSMax Filippov #define clear_user_highpage clear_user_highpage
148a91902dbSMax Filippov void clear_user_highpage(struct page *page, unsigned long vaddr);
149a91902dbSMax Filippov #define __HAVE_ARCH_COPY_USER_HIGHPAGE
150a91902dbSMax Filippov void copy_user_highpage(struct page *to, struct page *from,
151a91902dbSMax Filippov 			unsigned long vaddr, struct vm_area_struct *vma);
152367b8112SChris Zankel #else
153367b8112SChris Zankel # define clear_user_page(page, vaddr, pg)	clear_page(page)
154367b8112SChris Zankel # define copy_user_page(to, from, vaddr, pg)	copy_page(to, from)
155367b8112SChris Zankel #endif
156367b8112SChris Zankel 
157367b8112SChris Zankel /*
158367b8112SChris Zankel  * This handles the memory map.  We handle pages at
159367b8112SChris Zankel  * XCHAL_KSEG_CACHED_VADDR for kernels with 32 bit address space.
160367b8112SChris Zankel  * These macros are for conversion of kernel address, not user
161367b8112SChris Zankel  * addresses.
162367b8112SChris Zankel  */
163367b8112SChris Zankel 
1643de00482SMax Filippov #define ARCH_PFN_OFFSET		(PHYS_OFFSET >> PAGE_SHIFT)
165c947a585SJohannes Weiner 
1662b83878dSMax Filippov #ifdef CONFIG_MMU
___pa(unsigned long va)1672b83878dSMax Filippov static inline unsigned long ___pa(unsigned long va)
1682b83878dSMax Filippov {
1692b83878dSMax Filippov 	unsigned long off = va - PAGE_OFFSET;
1702b83878dSMax Filippov 
1712b83878dSMax Filippov 	if (off >= XCHAL_KSEG_SIZE)
1722b83878dSMax Filippov 		off -= XCHAL_KSEG_SIZE;
1732b83878dSMax Filippov 
1747af710d9SMax Filippov #ifndef CONFIG_XIP_KERNEL
1752b83878dSMax Filippov 	return off + PHYS_OFFSET;
1767af710d9SMax Filippov #else
1777af710d9SMax Filippov 	if (off < XCHAL_KSEG_SIZE)
1787af710d9SMax Filippov 		return off + PHYS_OFFSET;
1797af710d9SMax Filippov 
1807af710d9SMax Filippov 	off -= XCHAL_KSEG_SIZE;
1817af710d9SMax Filippov 	if (off >= XCHAL_KIO_SIZE)
1827af710d9SMax Filippov 		off -= XCHAL_KIO_SIZE;
1837af710d9SMax Filippov 
1847af710d9SMax Filippov 	return off + XCHAL_KIO_PADDR;
1857af710d9SMax Filippov #endif
1862b83878dSMax Filippov }
1872b83878dSMax Filippov #define __pa(x)	___pa((unsigned long)(x))
1882b83878dSMax Filippov #else
189a9f2fc62SMax Filippov #define __pa(x)	\
190a9f2fc62SMax Filippov 	((unsigned long) (x) - PAGE_OFFSET + PHYS_OFFSET)
1912b83878dSMax Filippov #endif
192a9f2fc62SMax Filippov #define __va(x)	\
193a9f2fc62SMax Filippov 	((void *)((unsigned long) (x) - PHYS_OFFSET + PAGE_OFFSET))
194c4c4594bSChris Zankel 
195367b8112SChris Zankel #define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
196367b8112SChris Zankel #define page_to_virt(page)	__va(page_to_pfn(page) << PAGE_SHIFT)
197367b8112SChris Zankel #define virt_addr_valid(kaddr)	pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
198367b8112SChris Zankel #define page_to_phys(page)	(page_to_pfn(page) << PAGE_SHIFT)
199367b8112SChris Zankel 
200367b8112SChris Zankel #endif /* __ASSEMBLY__ */
201367b8112SChris Zankel 
202367b8112SChris Zankel #include <asm-generic/memory_model.h>
203367b8112SChris Zankel #endif /* _XTENSA_PAGE_H */
204