18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * include/asm-xtensa/page.h
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
58c2ecf20Sopenharmony_ci * it under the terms of the GNU General Public License version2 as
68c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright (C) 2001 - 2007 Tensilica Inc.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifndef _XTENSA_PAGE_H
128c2ecf20Sopenharmony_ci#define _XTENSA_PAGE_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <asm/processor.h>
158c2ecf20Sopenharmony_ci#include <asm/types.h>
168c2ecf20Sopenharmony_ci#include <asm/cache.h>
178c2ecf20Sopenharmony_ci#include <asm/kmem_layout.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * PAGE_SHIFT determines the page size
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define PAGE_SHIFT	12
248c2ecf20Sopenharmony_ci#define PAGE_SIZE	(__XTENSA_UL_CONST(1) << PAGE_SHIFT)
258c2ecf20Sopenharmony_ci#define PAGE_MASK	(~(PAGE_SIZE-1))
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
288c2ecf20Sopenharmony_ci#define PAGE_OFFSET	XCHAL_KSEG_CACHED_VADDR
298c2ecf20Sopenharmony_ci#define PHYS_OFFSET	XCHAL_KSEG_PADDR
308c2ecf20Sopenharmony_ci#define MAX_LOW_PFN	(PHYS_PFN(XCHAL_KSEG_PADDR) + \
318c2ecf20Sopenharmony_ci			 PHYS_PFN(XCHAL_KSEG_SIZE))
328c2ecf20Sopenharmony_ci#else
338c2ecf20Sopenharmony_ci#define PAGE_OFFSET	_AC(CONFIG_DEFAULT_MEM_START, UL)
348c2ecf20Sopenharmony_ci#define PHYS_OFFSET	_AC(CONFIG_DEFAULT_MEM_START, UL)
358c2ecf20Sopenharmony_ci#define MAX_LOW_PFN	PHYS_PFN(0xfffffffful)
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * Cache aliasing:
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * If the cache size for one way is greater than the page size, we have to
428c2ecf20Sopenharmony_ci * deal with cache aliasing. The cache index is wider than the page size:
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * |    |cache| cache index
458c2ecf20Sopenharmony_ci * | pfn  |off|	virtual address
468c2ecf20Sopenharmony_ci * |xxxx:X|zzz|
478c2ecf20Sopenharmony_ci * |    : |   |
488c2ecf20Sopenharmony_ci * | \  / |   |
498c2ecf20Sopenharmony_ci * |trans.|   |
508c2ecf20Sopenharmony_ci * | /  \ |   |
518c2ecf20Sopenharmony_ci * |yyyy:Y|zzz|	physical address
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * When the page number is translated to the physical page address, the lowest
548c2ecf20Sopenharmony_ci * bit(s) (X) that are part of the cache index are also translated (Y).
558c2ecf20Sopenharmony_ci * If this translation changes bit(s) (X), the cache index is also afected,
568c2ecf20Sopenharmony_ci * thus resulting in a different cache line than before.
578c2ecf20Sopenharmony_ci * The kernel does not provide a mechanism to ensure that the page color
588c2ecf20Sopenharmony_ci * (represented by this bit) remains the same when allocated or when pages
598c2ecf20Sopenharmony_ci * are remapped. When user pages are mapped into kernel space, the color of
608c2ecf20Sopenharmony_ci * the page might also change.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * We use the address space VMALLOC_END ... VMALLOC_END + DCACHE_WAY_SIZE * 2
638c2ecf20Sopenharmony_ci * to temporarily map a patch so we can match the color.
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#if DCACHE_WAY_SIZE > PAGE_SIZE
678c2ecf20Sopenharmony_ci# define DCACHE_ALIAS_ORDER	(DCACHE_WAY_SHIFT - PAGE_SHIFT)
688c2ecf20Sopenharmony_ci# define DCACHE_ALIAS_MASK	(PAGE_MASK & (DCACHE_WAY_SIZE - 1))
698c2ecf20Sopenharmony_ci# define DCACHE_ALIAS(a)	(((a) & DCACHE_ALIAS_MASK) >> PAGE_SHIFT)
708c2ecf20Sopenharmony_ci# define DCACHE_ALIAS_EQ(a,b)	((((a) ^ (b)) & DCACHE_ALIAS_MASK) == 0)
718c2ecf20Sopenharmony_ci#else
728c2ecf20Sopenharmony_ci# define DCACHE_ALIAS_ORDER	0
738c2ecf20Sopenharmony_ci# define DCACHE_ALIAS(a)	((void)(a), 0)
748c2ecf20Sopenharmony_ci#endif
758c2ecf20Sopenharmony_ci#define DCACHE_N_COLORS		(1 << DCACHE_ALIAS_ORDER)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#if ICACHE_WAY_SIZE > PAGE_SIZE
788c2ecf20Sopenharmony_ci# define ICACHE_ALIAS_ORDER	(ICACHE_WAY_SHIFT - PAGE_SHIFT)
798c2ecf20Sopenharmony_ci# define ICACHE_ALIAS_MASK	(PAGE_MASK & (ICACHE_WAY_SIZE - 1))
808c2ecf20Sopenharmony_ci# define ICACHE_ALIAS(a)	(((a) & ICACHE_ALIAS_MASK) >> PAGE_SHIFT)
818c2ecf20Sopenharmony_ci# define ICACHE_ALIAS_EQ(a,b)	((((a) ^ (b)) & ICACHE_ALIAS_MASK) == 0)
828c2ecf20Sopenharmony_ci#else
838c2ecf20Sopenharmony_ci# define ICACHE_ALIAS_ORDER	0
848c2ecf20Sopenharmony_ci#endif
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#ifdef __ASSEMBLY__
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define __pgprot(x)	(x)
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#else
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/*
948c2ecf20Sopenharmony_ci * These are used to make use of C type-checking..
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_citypedef struct { unsigned long pte; } pte_t;		/* page table entry */
988c2ecf20Sopenharmony_citypedef struct { unsigned long pgd; } pgd_t;		/* PGD table entry */
998c2ecf20Sopenharmony_citypedef struct { unsigned long pgprot; } pgprot_t;
1008c2ecf20Sopenharmony_citypedef struct page *pgtable_t;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define pte_val(x)	((x).pte)
1038c2ecf20Sopenharmony_ci#define pgd_val(x)	((x).pgd)
1048c2ecf20Sopenharmony_ci#define pgprot_val(x)	((x).pgprot)
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define __pte(x)	((pte_t) { (x) } )
1078c2ecf20Sopenharmony_ci#define __pgd(x)	((pgd_t) { (x) } )
1088c2ecf20Sopenharmony_ci#define __pgprot(x)	((pgprot_t) { (x) } )
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/*
1118c2ecf20Sopenharmony_ci * Pure 2^n version of get_order
1128c2ecf20Sopenharmony_ci * Use 'nsau' instructions if supported by the processor or the generic version.
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#if XCHAL_HAVE_NSA
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic inline __attribute_const__ int get_order(unsigned long size)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	int lz;
1208c2ecf20Sopenharmony_ci	asm ("nsau %0, %1" : "=r" (lz) : "r" ((size - 1) >> PAGE_SHIFT));
1218c2ecf20Sopenharmony_ci	return 32 - lz;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci#else
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci# include <asm-generic/getorder.h>
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#endif
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct page;
1318c2ecf20Sopenharmony_cistruct vm_area_struct;
1328c2ecf20Sopenharmony_ciextern void clear_page(void *page);
1338c2ecf20Sopenharmony_ciextern void copy_page(void *to, void *from);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/*
1368c2ecf20Sopenharmony_ci * If we have cache aliasing and writeback caches, we might have to do
1378c2ecf20Sopenharmony_ci * some extra work
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#if defined(CONFIG_MMU) && DCACHE_WAY_SIZE > PAGE_SIZE
1418c2ecf20Sopenharmony_ciextern void clear_page_alias(void *vaddr, unsigned long paddr);
1428c2ecf20Sopenharmony_ciextern void copy_page_alias(void *to, void *from,
1438c2ecf20Sopenharmony_ci			    unsigned long to_paddr, unsigned long from_paddr);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define clear_user_highpage clear_user_highpage
1468c2ecf20Sopenharmony_civoid clear_user_highpage(struct page *page, unsigned long vaddr);
1478c2ecf20Sopenharmony_ci#define __HAVE_ARCH_COPY_USER_HIGHPAGE
1488c2ecf20Sopenharmony_civoid copy_user_highpage(struct page *to, struct page *from,
1498c2ecf20Sopenharmony_ci			unsigned long vaddr, struct vm_area_struct *vma);
1508c2ecf20Sopenharmony_ci#else
1518c2ecf20Sopenharmony_ci# define clear_user_page(page, vaddr, pg)	clear_page(page)
1528c2ecf20Sopenharmony_ci# define copy_user_page(to, from, vaddr, pg)	copy_page(to, from)
1538c2ecf20Sopenharmony_ci#endif
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/*
1568c2ecf20Sopenharmony_ci * This handles the memory map.  We handle pages at
1578c2ecf20Sopenharmony_ci * XCHAL_KSEG_CACHED_VADDR for kernels with 32 bit address space.
1588c2ecf20Sopenharmony_ci * These macros are for conversion of kernel address, not user
1598c2ecf20Sopenharmony_ci * addresses.
1608c2ecf20Sopenharmony_ci */
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci#define ARCH_PFN_OFFSET		(PHYS_OFFSET >> PAGE_SHIFT)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
1658c2ecf20Sopenharmony_cistatic inline unsigned long ___pa(unsigned long va)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	unsigned long off = va - PAGE_OFFSET;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (off >= XCHAL_KSEG_SIZE)
1708c2ecf20Sopenharmony_ci		off -= XCHAL_KSEG_SIZE;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#ifndef CONFIG_XIP_KERNEL
1738c2ecf20Sopenharmony_ci	return off + PHYS_OFFSET;
1748c2ecf20Sopenharmony_ci#else
1758c2ecf20Sopenharmony_ci	if (off < XCHAL_KSEG_SIZE)
1768c2ecf20Sopenharmony_ci		return off + PHYS_OFFSET;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	off -= XCHAL_KSEG_SIZE;
1798c2ecf20Sopenharmony_ci	if (off >= XCHAL_KIO_SIZE)
1808c2ecf20Sopenharmony_ci		off -= XCHAL_KIO_SIZE;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return off + XCHAL_KIO_PADDR;
1838c2ecf20Sopenharmony_ci#endif
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci#define __pa(x)	___pa((unsigned long)(x))
1868c2ecf20Sopenharmony_ci#else
1878c2ecf20Sopenharmony_ci#define __pa(x)	\
1888c2ecf20Sopenharmony_ci	((unsigned long) (x) - PAGE_OFFSET + PHYS_OFFSET)
1898c2ecf20Sopenharmony_ci#endif
1908c2ecf20Sopenharmony_ci#define __va(x)	\
1918c2ecf20Sopenharmony_ci	((void *)((unsigned long) (x) - PHYS_OFFSET + PAGE_OFFSET))
1928c2ecf20Sopenharmony_ci#define pfn_valid(pfn) \
1938c2ecf20Sopenharmony_ci	((pfn) >= ARCH_PFN_OFFSET && ((pfn) - ARCH_PFN_OFFSET) < max_mapnr)
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci#ifdef CONFIG_DISCONTIGMEM
1968c2ecf20Sopenharmony_ci# error CONFIG_DISCONTIGMEM not supported
1978c2ecf20Sopenharmony_ci#endif
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define virt_to_page(kaddr)	pfn_to_page(__pa(kaddr) >> PAGE_SHIFT)
2008c2ecf20Sopenharmony_ci#define page_to_virt(page)	__va(page_to_pfn(page) << PAGE_SHIFT)
2018c2ecf20Sopenharmony_ci#define virt_addr_valid(kaddr)	pfn_valid(__pa(kaddr) >> PAGE_SHIFT)
2028c2ecf20Sopenharmony_ci#define page_to_phys(page)	(page_to_pfn(page) << PAGE_SHIFT)
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#include <asm-generic/memory_model.h>
2078c2ecf20Sopenharmony_ci#endif /* _XTENSA_PAGE_H */
208