16a3cece5SMichal Simek /*
21f84e1eaSMichal Simek  * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
31f84e1eaSMichal Simek  * Copyright (C) 2008-2009 PetaLogix
46a3cece5SMichal Simek  * Copyright (C) 2006 Atmark Techno, Inc.
56a3cece5SMichal Simek  *
66a3cece5SMichal Simek  * This file is subject to the terms and conditions of the GNU General Public
76a3cece5SMichal Simek  * License. See the file "COPYING" in the main directory of this archive
86a3cece5SMichal Simek  * for more details.
96a3cece5SMichal Simek  */
106a3cece5SMichal Simek 
116a3cece5SMichal Simek #ifndef _ASM_MICROBLAZE_PGALLOC_H
126a3cece5SMichal Simek #define _ASM_MICROBLAZE_PGALLOC_H
136a3cece5SMichal Simek 
141f84e1eaSMichal Simek #ifdef CONFIG_MMU
151f84e1eaSMichal Simek 
161f84e1eaSMichal Simek #include <linux/kernel.h>	/* For min/max macros */
171f84e1eaSMichal Simek #include <linux/highmem.h>
181f84e1eaSMichal Simek #include <asm/setup.h>
191f84e1eaSMichal Simek #include <asm/io.h>
201f84e1eaSMichal Simek #include <asm/page.h>
211f84e1eaSMichal Simek #include <asm/cache.h>
2279bf3a13SMichal Simek #include <asm/pgtable.h>
231f84e1eaSMichal Simek 
241f84e1eaSMichal Simek #define PGDIR_ORDER	0
251f84e1eaSMichal Simek 
261f84e1eaSMichal Simek /*
271f84e1eaSMichal Simek  * This is handled very differently on MicroBlaze since out page tables
281f84e1eaSMichal Simek  * are all 0's and I want to be able to use these zero'd pages elsewhere
291f84e1eaSMichal Simek  * as well - it gives us quite a speedup.
301f84e1eaSMichal Simek  * -- Cort
311f84e1eaSMichal Simek  */
321f84e1eaSMichal Simek extern struct pgtable_cache_struct {
331f84e1eaSMichal Simek 	unsigned long *pgd_cache;
341f84e1eaSMichal Simek 	unsigned long *pte_cache;
351f84e1eaSMichal Simek 	unsigned long pgtable_cache_sz;
361f84e1eaSMichal Simek } quicklists;
371f84e1eaSMichal Simek 
381f84e1eaSMichal Simek #define pgd_quicklist		(quicklists.pgd_cache)
391f84e1eaSMichal Simek #define pmd_quicklist		((unsigned long *)0)
401f84e1eaSMichal Simek #define pte_quicklist		(quicklists.pte_cache)
411f84e1eaSMichal Simek #define pgtable_cache_size	(quicklists.pgtable_cache_sz)
421f84e1eaSMichal Simek 
431f84e1eaSMichal Simek extern unsigned long *zero_cache; /* head linked list of pre-zero'd pages */
441f84e1eaSMichal Simek extern atomic_t zero_sz; /* # currently pre-zero'd pages */
451f84e1eaSMichal Simek extern atomic_t zeropage_hits; /* # zero'd pages request that we've done */
461f84e1eaSMichal Simek extern atomic_t zeropage_calls; /* # zero'd pages request that've been made */
471f84e1eaSMichal Simek extern atomic_t zerototal; /* # pages zero'd over time */
481f84e1eaSMichal Simek 
491f84e1eaSMichal Simek #define zero_quicklist		(zero_cache)
501f84e1eaSMichal Simek #define zero_cache_sz	 	(zero_sz)
511f84e1eaSMichal Simek #define zero_cache_calls	(zeropage_calls)
521f84e1eaSMichal Simek #define zero_cache_hits		(zeropage_hits)
531f84e1eaSMichal Simek #define zero_cache_total	(zerototal)
541f84e1eaSMichal Simek 
551f84e1eaSMichal Simek /*
561f84e1eaSMichal Simek  * return a pre-zero'd page from the list,
571f84e1eaSMichal Simek  * return NULL if none available -- Cort
581f84e1eaSMichal Simek  */
591f84e1eaSMichal Simek extern unsigned long get_zero_page_fast(void);
601f84e1eaSMichal Simek 
611f84e1eaSMichal Simek extern void __bad_pte(pmd_t *pmd);
621f84e1eaSMichal Simek 
63b6db0a56SMichal Simek static inline pgd_t *get_pgd_slow(void)
641f84e1eaSMichal Simek {
651f84e1eaSMichal Simek 	pgd_t *ret;
661f84e1eaSMichal Simek 
671f84e1eaSMichal Simek 	ret = (pgd_t *)__get_free_pages(GFP_KERNEL, PGDIR_ORDER);
681f84e1eaSMichal Simek 	if (ret != NULL)
691f84e1eaSMichal Simek 		clear_page(ret);
701f84e1eaSMichal Simek 	return ret;
711f84e1eaSMichal Simek }
721f84e1eaSMichal Simek 
73b6db0a56SMichal Simek static inline pgd_t *get_pgd_fast(void)
741f84e1eaSMichal Simek {
751f84e1eaSMichal Simek 	unsigned long *ret;
761f84e1eaSMichal Simek 
771f84e1eaSMichal Simek 	ret = pgd_quicklist;
781f84e1eaSMichal Simek 	if (ret != NULL) {
791f84e1eaSMichal Simek 		pgd_quicklist = (unsigned long *)(*ret);
801f84e1eaSMichal Simek 		ret[0] = 0;
811f84e1eaSMichal Simek 		pgtable_cache_size--;
821f84e1eaSMichal Simek 	} else
831f84e1eaSMichal Simek 		ret = (unsigned long *)get_pgd_slow();
841f84e1eaSMichal Simek 	return (pgd_t *)ret;
851f84e1eaSMichal Simek }
861f84e1eaSMichal Simek 
87b6db0a56SMichal Simek static inline void free_pgd_fast(pgd_t *pgd)
881f84e1eaSMichal Simek {
891f84e1eaSMichal Simek 	*(unsigned long **)pgd = pgd_quicklist;
901f84e1eaSMichal Simek 	pgd_quicklist = (unsigned long *) pgd;
911f84e1eaSMichal Simek 	pgtable_cache_size++;
921f84e1eaSMichal Simek }
931f84e1eaSMichal Simek 
94b6db0a56SMichal Simek static inline void free_pgd_slow(pgd_t *pgd)
951f84e1eaSMichal Simek {
961f84e1eaSMichal Simek 	free_page((unsigned long)pgd);
971f84e1eaSMichal Simek }
981f84e1eaSMichal Simek 
991f84e1eaSMichal Simek #define pgd_free(mm, pgd)        free_pgd_fast(pgd)
1001f84e1eaSMichal Simek #define pgd_alloc(mm)		get_pgd_fast()
1011f84e1eaSMichal Simek 
1021f84e1eaSMichal Simek #define pmd_pgtable(pmd)	pmd_page(pmd)
1031f84e1eaSMichal Simek 
1041f84e1eaSMichal Simek /*
1051f84e1eaSMichal Simek  * We don't have any real pmd's, and this code never triggers because
1061f84e1eaSMichal Simek  * the pgd will always be present..
1071f84e1eaSMichal Simek  */
1081f84e1eaSMichal Simek #define pmd_alloc_one_fast(mm, address)	({ BUG(); ((pmd_t *)1); })
1091f84e1eaSMichal Simek #define pmd_alloc_one(mm, address)	({ BUG(); ((pmd_t *)2); })
1101f84e1eaSMichal Simek 
11163f1032bSMichal Simek extern pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long addr);
1121f84e1eaSMichal Simek 
1131f84e1eaSMichal Simek static inline struct page *pte_alloc_one(struct mm_struct *mm,
1141f84e1eaSMichal Simek 		unsigned long address)
1151f84e1eaSMichal Simek {
1161f84e1eaSMichal Simek 	struct page *ptepage;
1171f84e1eaSMichal Simek 
1181f84e1eaSMichal Simek #ifdef CONFIG_HIGHPTE
1191f84e1eaSMichal Simek 	int flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT;
1201f84e1eaSMichal Simek #else
1211f84e1eaSMichal Simek 	int flags = GFP_KERNEL | __GFP_REPEAT;
1221f84e1eaSMichal Simek #endif
1231f84e1eaSMichal Simek 
1241f84e1eaSMichal Simek 	ptepage = alloc_pages(flags, 0);
1258abe7346SKirill A. Shutemov 	if (!ptepage)
1268abe7346SKirill A. Shutemov 		return NULL;
1271f84e1eaSMichal Simek 	clear_highpage(ptepage);
1288abe7346SKirill A. Shutemov 	if (!pgtable_page_ctor(ptepage)) {
1298abe7346SKirill A. Shutemov 		__free_page(ptepage);
1308abe7346SKirill A. Shutemov 		return NULL;
1318abe7346SKirill A. Shutemov 	}
1321f84e1eaSMichal Simek 	return ptepage;
1331f84e1eaSMichal Simek }
1341f84e1eaSMichal Simek 
1351f84e1eaSMichal Simek static inline pte_t *pte_alloc_one_fast(struct mm_struct *mm,
1361f84e1eaSMichal Simek 		unsigned long address)
1371f84e1eaSMichal Simek {
1381f84e1eaSMichal Simek 	unsigned long *ret;
1391f84e1eaSMichal Simek 
1401f84e1eaSMichal Simek 	ret = pte_quicklist;
1411f84e1eaSMichal Simek 	if (ret != NULL) {
1421f84e1eaSMichal Simek 		pte_quicklist = (unsigned long *)(*ret);
1431f84e1eaSMichal Simek 		ret[0] = 0;
1441f84e1eaSMichal Simek 		pgtable_cache_size--;
1451f84e1eaSMichal Simek 	}
1461f84e1eaSMichal Simek 	return (pte_t *)ret;
1471f84e1eaSMichal Simek }
1481f84e1eaSMichal Simek 
149b6db0a56SMichal Simek static inline void pte_free_fast(pte_t *pte)
1501f84e1eaSMichal Simek {
1511f84e1eaSMichal Simek 	*(unsigned long **)pte = pte_quicklist;
1521f84e1eaSMichal Simek 	pte_quicklist = (unsigned long *) pte;
1531f84e1eaSMichal Simek 	pgtable_cache_size++;
1541f84e1eaSMichal Simek }
1551f84e1eaSMichal Simek 
156b6db0a56SMichal Simek static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
1571f84e1eaSMichal Simek {
1581f84e1eaSMichal Simek 	free_page((unsigned long)pte);
1591f84e1eaSMichal Simek }
1601f84e1eaSMichal Simek 
161b6db0a56SMichal Simek static inline void pte_free_slow(struct page *ptepage)
1621f84e1eaSMichal Simek {
1631f84e1eaSMichal Simek 	__free_page(ptepage);
1641f84e1eaSMichal Simek }
1651f84e1eaSMichal Simek 
1668abe7346SKirill A. Shutemov static inline void pte_free(struct mm_struct *mm, struct page *ptepage)
1671f84e1eaSMichal Simek {
1688abe7346SKirill A. Shutemov 	pgtable_page_dtor(ptepage);
1691f84e1eaSMichal Simek 	__free_page(ptepage);
1701f84e1eaSMichal Simek }
1711f84e1eaSMichal Simek 
1729e1b32caSBenjamin Herrenschmidt #define __pte_free_tlb(tlb, pte, addr)	pte_free((tlb)->mm, (pte))
1731f84e1eaSMichal Simek 
1745f0cb3e0SMichal Simek #define pmd_populate(mm, pmd, pte) \
1755f0cb3e0SMichal Simek 			(pmd_val(*(pmd)) = (unsigned long)page_address(pte))
1761f84e1eaSMichal Simek 
1771f84e1eaSMichal Simek #define pmd_populate_kernel(mm, pmd, pte) \
1781f84e1eaSMichal Simek 		(pmd_val(*(pmd)) = (unsigned long) (pte))
1791f84e1eaSMichal Simek 
1801f84e1eaSMichal Simek /*
1811f84e1eaSMichal Simek  * We don't have any real pmd's, and this code never triggers because
1821f84e1eaSMichal Simek  * the pgd will always be present..
1831f84e1eaSMichal Simek  */
1841f84e1eaSMichal Simek #define pmd_alloc_one(mm, address)	({ BUG(); ((pmd_t *)2); })
185d4f18270SMichal Simek #define pmd_free(mm, x)			do { } while (0)
186d4f18270SMichal Simek #define __pmd_free_tlb(tlb, x, addr)	pmd_free((tlb)->mm, x)
1871f84e1eaSMichal Simek #define pgd_populate(mm, pmd, pte)	BUG()
1881f84e1eaSMichal Simek 
1891f84e1eaSMichal Simek extern int do_check_pgt_cache(int, int);
1901f84e1eaSMichal Simek 
1911f84e1eaSMichal Simek #endif /* CONFIG_MMU */
1921f84e1eaSMichal Simek 
1936a3cece5SMichal Simek #define check_pgt_cache()		do { } while (0)
1946a3cece5SMichal Simek 
1956a3cece5SMichal Simek #endif /* _ASM_MICROBLAZE_PGALLOC_H */
196