Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Russell King | 0c66898 | 2006-09-27 15:40:28 +0100 | [diff] [blame] | 2 | * linux/arch/arm/mm/pgd.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Russell King | 9007205 | 2005-10-28 14:48:37 +0100 | [diff] [blame] | 4 | * Copyright (C) 1998-2005 Russell King |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | #include <asm/pgalloc.h> |
| 15 | #include <asm/page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/tlbflush.h> |
| 17 | |
Russell King | 1b2e2b7 | 2006-08-21 17:06:38 +0100 | [diff] [blame] | 18 | #include "mm.h" |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | /* |
| 21 | * need to get a 16k page for level 1 |
| 22 | */ |
Russell King | b0d0374 | 2010-11-21 11:00:56 +0000 | [diff] [blame] | 23 | pgd_t *pgd_alloc(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
| 25 | pgd_t *new_pgd, *init_pgd; |
| 26 | pmd_t *new_pmd, *init_pmd; |
| 27 | pte_t *new_pte, *init_pte; |
| 28 | |
| 29 | new_pgd = (pgd_t *)__get_free_pages(GFP_KERNEL, 2); |
| 30 | if (!new_pgd) |
| 31 | goto no_pgd; |
| 32 | |
Russell King | e926f44 | 2010-11-21 11:55:37 +0000 | [diff] [blame] | 33 | memset(new_pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Russell King | a343e60 | 2005-06-27 14:08:56 +0100 | [diff] [blame] | 35 | /* |
| 36 | * Copy over the kernel and IO PGD entries |
| 37 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | init_pgd = pgd_offset_k(0); |
Russell King | e926f44 | 2010-11-21 11:55:37 +0000 | [diff] [blame] | 39 | memcpy(new_pgd + USER_PTRS_PER_PGD, init_pgd + USER_PTRS_PER_PGD, |
| 40 | (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); |
Russell King | a343e60 | 2005-06-27 14:08:56 +0100 | [diff] [blame] | 41 | |
| 42 | clean_dcache_area(new_pgd, PTRS_PER_PGD * sizeof(pgd_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | if (!vectors_high()) { |
| 45 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * On ARM, first page must always be allocated since it |
| 47 | * contains the machine vectors. |
| 48 | */ |
| 49 | new_pmd = pmd_alloc(mm, new_pgd, 0); |
| 50 | if (!new_pmd) |
| 51 | goto no_pmd; |
| 52 | |
Andrea Arcangeli | 8ac1f83 | 2011-01-13 15:46:43 -0800 | [diff] [blame^] | 53 | new_pte = pte_alloc_map(mm, NULL, new_pmd, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (!new_pte) |
| 55 | goto no_pte; |
| 56 | |
| 57 | init_pmd = pmd_offset(init_pgd, 0); |
Peter Zijlstra | ece0e2b | 2010-10-26 14:21:52 -0700 | [diff] [blame] | 58 | init_pte = pte_offset_map(init_pmd, 0); |
Russell King | ad1ae2f | 2006-12-13 14:34:43 +0000 | [diff] [blame] | 59 | set_pte_ext(new_pte, *init_pte, 0); |
Peter Zijlstra | ece0e2b | 2010-10-26 14:21:52 -0700 | [diff] [blame] | 60 | pte_unmap(init_pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | pte_unmap(new_pte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return new_pgd; |
| 65 | |
| 66 | no_pte: |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 67 | pmd_free(mm, new_pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | no_pmd: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | free_pages((unsigned long)new_pgd, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | no_pgd: |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 74 | void pgd_free(struct mm_struct *mm, pgd_t *pgd_base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 76 | pgd_t *pgd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | pmd_t *pmd; |
Uwe Kleine-König | 0c82d83 | 2008-02-27 13:44:59 +0100 | [diff] [blame] | 78 | pgtable_t pte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 80 | if (!pgd_base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return; |
| 82 | |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 83 | pgd = pgd_base + pgd_index(0); |
| 84 | if (pgd_none_or_clear_bad(pgd)) |
| 85 | goto no_pgd; |
| 86 | |
| 87 | pmd = pmd_offset(pgd, 0); |
| 88 | if (pmd_none_or_clear_bad(pmd)) |
| 89 | goto no_pmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Uwe Kleine-König | 0c82d83 | 2008-02-27 13:44:59 +0100 | [diff] [blame] | 91 | pte = pmd_pgtable(*pmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | pmd_clear(pmd); |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 93 | pte_free(mm, pte); |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 94 | no_pmd: |
| 95 | pgd_clear(pgd); |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 96 | pmd_free(mm, pmd); |
Russell King | 6e4beb5 | 2010-11-21 23:48:55 +0000 | [diff] [blame] | 97 | no_pgd: |
| 98 | free_pages((unsigned long) pgd_base, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |