blob: 80adfbf75065d487a572226eafd21450bc84962d [file] [log] [blame]
Jan Glauber305b1522011-03-15 17:08:22 +01001/*
2 * Copyright IBM Corp. 2011
3 * Author(s): Jan Glauber <jang@linux.vnet.ibm.com>
4 */
Heiko Carstens6b70a922012-11-02 12:56:43 +01005#include <linux/hugetlb.h>
Jan Glauber305b1522011-03-15 17:08:22 +01006#include <linux/module.h>
7#include <linux/mm.h>
Martin Schwidefsky638ad342011-10-30 15:17:13 +01008#include <asm/cacheflush.h>
Jan Glauber305b1522011-03-15 17:08:22 +01009#include <asm/pgtable.h>
Heiko Carstens6b70a922012-11-02 12:56:43 +010010#include <asm/page.h>
11
Heiko Carstensf7f8d7e2013-03-14 16:46:05 +010012static inline unsigned long sske_frame(unsigned long addr, unsigned char skey)
13{
14 asm volatile(".insn rrf,0xb22b0000,%[skey],%[addr],9,0"
15 : [addr] "+a" (addr) : [skey] "d" (skey));
16 return addr;
17}
18
Heiko Carstens6b70a922012-11-02 12:56:43 +010019void storage_key_init_range(unsigned long start, unsigned long end)
20{
Heiko Carstensf7f8d7e2013-03-14 16:46:05 +010021 unsigned long boundary, size;
Heiko Carstens6b70a922012-11-02 12:56:43 +010022
23 while (start < end) {
Heiko Carstens6b70a922012-11-02 12:56:43 +010024 if (MACHINE_HAS_EDAT1) {
25 /* set storage keys for a 1MB frame */
Heiko Carstens6b70a922012-11-02 12:56:43 +010026 size = 1UL << 20;
27 boundary = (start + size) & ~(size - 1);
28 if (boundary <= end) {
29 do {
Heiko Carstensf7f8d7e2013-03-14 16:46:05 +010030 start = sske_frame(start, PAGE_DEFAULT_KEY);
Heiko Carstens6b70a922012-11-02 12:56:43 +010031 } while (start < boundary);
32 continue;
33 }
34 }
35 page_set_storage_key(start, PAGE_DEFAULT_KEY, 0);
36 start += PAGE_SIZE;
37 }
38}
Jan Glauber305b1522011-03-15 17:08:22 +010039
Heiko Carstens5b1ba9e2012-10-01 16:18:46 +020040static pte_t *walk_page_table(unsigned long addr)
41{
42 pgd_t *pgdp;
43 pud_t *pudp;
44 pmd_t *pmdp;
45 pte_t *ptep;
46
47 pgdp = pgd_offset_k(addr);
48 if (pgd_none(*pgdp))
49 return NULL;
50 pudp = pud_offset(pgdp, addr);
Heiko Carstens18da2362012-10-08 09:18:26 +020051 if (pud_none(*pudp) || pud_large(*pudp))
Heiko Carstens5b1ba9e2012-10-01 16:18:46 +020052 return NULL;
53 pmdp = pmd_offset(pudp, addr);
54 if (pmd_none(*pmdp) || pmd_large(*pmdp))
55 return NULL;
56 ptep = pte_offset_kernel(pmdp, addr);
57 if (pte_none(*ptep))
58 return NULL;
59 return ptep;
60}
61
Jan Glauber305b1522011-03-15 17:08:22 +010062static void change_page_attr(unsigned long addr, int numpages,
63 pte_t (*set) (pte_t))
64{
65 pte_t *ptep, pte;
Jan Glauber305b1522011-03-15 17:08:22 +010066 int i;
67
68 for (i = 0; i < numpages; i++) {
Heiko Carstens5b1ba9e2012-10-01 16:18:46 +020069 ptep = walk_page_table(addr);
70 if (WARN_ON_ONCE(!ptep))
71 break;
Jan Glauber305b1522011-03-15 17:08:22 +010072 pte = *ptep;
73 pte = set(pte);
Martin Schwidefskyb2fa47e2011-05-23 10:24:40 +020074 __ptep_ipte(addr, ptep);
Jan Glauber305b1522011-03-15 17:08:22 +010075 *ptep = pte;
Jan Glaubere4c031b2011-04-20 10:15:32 +020076 addr += PAGE_SIZE;
Jan Glauber305b1522011-03-15 17:08:22 +010077 }
78}
79
80int set_memory_ro(unsigned long addr, int numpages)
81{
82 change_page_attr(addr, numpages, pte_wrprotect);
83 return 0;
84}
Jan Glauber305b1522011-03-15 17:08:22 +010085
86int set_memory_rw(unsigned long addr, int numpages)
87{
88 change_page_attr(addr, numpages, pte_mkwrite);
89 return 0;
90}
Jan Glauber305b1522011-03-15 17:08:22 +010091
92/* not possible */
93int set_memory_nx(unsigned long addr, int numpages)
94{
95 return 0;
96}
Jan Glauber448694a2011-05-19 16:55:26 -060097
98int set_memory_x(unsigned long addr, int numpages)
99{
100 return 0;
101}
Heiko Carstens0a4ccc92012-11-02 13:28:48 +0100102
103#ifdef CONFIG_DEBUG_PAGEALLOC
104void kernel_map_pages(struct page *page, int numpages, int enable)
105{
106 unsigned long address;
107 pgd_t *pgd;
108 pud_t *pud;
109 pmd_t *pmd;
110 pte_t *pte;
111 int i;
112
113 for (i = 0; i < numpages; i++) {
114 address = page_to_phys(page + i);
115 pgd = pgd_offset_k(address);
116 pud = pud_offset(pgd, address);
117 pmd = pmd_offset(pud, address);
118 pte = pte_offset_kernel(pmd, address);
119 if (!enable) {
120 __ptep_ipte(address, pte);
121 pte_val(*pte) = _PAGE_TYPE_EMPTY;
122 continue;
123 }
Martin Schwidefskyabf09be2012-11-07 13:17:37 +0100124 pte_val(*pte) = __pa(address);
Heiko Carstens0a4ccc92012-11-02 13:28:48 +0100125 }
126}
127
128#ifdef CONFIG_HIBERNATION
129bool kernel_page_present(struct page *page)
130{
131 unsigned long addr;
132 int cc;
133
134 addr = page_to_phys(page);
135 asm volatile(
136 " lra %1,0(%1)\n"
137 " ipm %0\n"
138 " srl %0,28"
139 : "=d" (cc), "+a" (addr) : : "cc");
140 return cc == 0;
141}
142#endif /* CONFIG_HIBERNATION */
143
144#endif /* CONFIG_DEBUG_PAGEALLOC */