blob: f8355a902ac5f80c5a05efab27531911d81c572d [file] [log] [blame]
Alexander Graf3ae07892010-04-16 00:11:37 +02001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright SUSE Linux Products GmbH 2010
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#ifndef __ASM_KVM_BOOK3S_64_H__
21#define __ASM_KVM_BOOK3S_64_H__
22
Paul Mackerrasde56a942011-06-29 00:21:34 +000023#ifdef CONFIG_KVM_BOOK3S_PR
Alexander Graf468a12c2011-12-09 14:44:13 +010024static inline struct kvmppc_book3s_shadow_vcpu *svcpu_get(struct kvm_vcpu *vcpu)
Alexander Graf3ae07892010-04-16 00:11:37 +020025{
Alexander Graf468a12c2011-12-09 14:44:13 +010026 preempt_disable();
Alexander Graf3ae07892010-04-16 00:11:37 +020027 return &get_paca()->shadow_vcpu;
28}
Alexander Graf468a12c2011-12-09 14:44:13 +010029
30static inline void svcpu_put(struct kvmppc_book3s_shadow_vcpu *svcpu)
31{
32 preempt_enable();
33}
Paul Mackerrasde56a942011-06-29 00:21:34 +000034#endif
Alexander Graf3ae07892010-04-16 00:11:37 +020035
David Gibson54738c02011-06-29 00:22:41 +000036#define SPAPR_TCE_SHIFT 12
37
Paul Mackerras8936dda2011-12-12 12:27:39 +000038#ifdef CONFIG_KVM_BOOK3S_64_HV
Paul Mackerras32fad282012-05-04 02:32:53 +000039#define KVM_DEFAULT_HPT_ORDER 24 /* 16MB HPT by default */
Paul Mackerras8936dda2011-12-12 12:27:39 +000040#endif
41
Paul Mackerras697d3892011-12-12 12:36:37 +000042#define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */
43
Paul Mackerras075295d2011-12-12 12:30:16 +000044/*
45 * We use a lock bit in HPTE dword 0 to synchronize updates and
46 * accesses to each HPTE, and another bit to indicate non-present
47 * HPTEs.
48 */
49#define HPTE_V_HVLOCK 0x40UL
Paul Mackerras697d3892011-12-12 12:36:37 +000050#define HPTE_V_ABSENT 0x20UL
Paul Mackerras075295d2011-12-12 12:30:16 +000051
Paul Mackerras44e5f6b2012-11-19 22:52:49 +000052/*
53 * We use this bit in the guest_rpte field of the revmap entry
54 * to indicate a modified HPTE.
55 */
56#define HPTE_GR_MODIFIED (1ul << 62)
57
58/* These bits are reserved in the guest view of the HPTE */
59#define HPTE_GR_RESERVED HPTE_GR_MODIFIED
60
Paul Mackerras075295d2011-12-12 12:30:16 +000061static inline long try_lock_hpte(unsigned long *hpte, unsigned long bits)
62{
63 unsigned long tmp, old;
64
65 asm volatile(" ldarx %0,0,%2\n"
66 " and. %1,%0,%3\n"
67 " bne 2f\n"
68 " ori %0,%0,%4\n"
69 " stdcx. %0,0,%2\n"
70 " beq+ 2f\n"
Paul Mackerras8b5869a2012-10-15 01:20:50 +000071 " mr %1,%3\n"
Paul Mackerras075295d2011-12-12 12:30:16 +000072 "2: isync"
73 : "=&r" (tmp), "=&r" (old)
74 : "r" (hpte), "r" (bits), "i" (HPTE_V_HVLOCK)
75 : "cc", "memory");
76 return old == 0;
77}
78
Andreas Schwab36cc66d2011-11-08 07:08:52 +000079static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
80 unsigned long pte_index)
81{
82 unsigned long rb, va_low;
83
84 rb = (v & ~0x7fUL) << 16; /* AVA field */
85 va_low = pte_index >> 3;
86 if (v & HPTE_V_SECONDARY)
87 va_low = ~va_low;
88 /* xor vsid from AVA */
89 if (!(v & HPTE_V_1TB_SEG))
90 va_low ^= v >> 12;
91 else
92 va_low ^= v >> 24;
93 va_low &= 0x7ff;
94 if (v & HPTE_V_LARGE) {
95 rb |= 1; /* L field */
96 if (cpu_has_feature(CPU_FTR_ARCH_206) &&
97 (r & 0xff000)) {
98 /* non-16MB large page, must be 64k */
99 /* (masks depend on page size) */
100 rb |= 0x1000; /* page encoding in LP field */
101 rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
102 rb |= (va_low & 0xfe); /* AVAL field (P7 doesn't seem to care) */
103 }
104 } else {
105 /* 4kB page */
106 rb |= (va_low & 0x7ff) << 12; /* remaining 11b of VA */
107 }
108 rb |= (v >> 54) & 0x300; /* B field */
109 return rb;
110}
111
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000112static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
113{
114 /* only handle 4k, 64k and 16M pages for now */
115 if (!(h & HPTE_V_LARGE))
116 return 1ul << 12; /* 4k page */
117 if ((l & 0xf000) == 0x1000 && cpu_has_feature(CPU_FTR_ARCH_206))
118 return 1ul << 16; /* 64k page */
119 if ((l & 0xff000) == 0)
120 return 1ul << 24; /* 16M page */
121 return 0; /* error */
122}
123
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000124static inline unsigned long hpte_rpn(unsigned long ptel, unsigned long psize)
125{
126 return ((ptel & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
127}
128
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000129static inline int hpte_is_writable(unsigned long ptel)
130{
131 unsigned long pp = ptel & (HPTE_R_PP0 | HPTE_R_PP);
132
133 return pp != PP_RXRX && pp != PP_RXXX;
134}
135
136static inline unsigned long hpte_make_readonly(unsigned long ptel)
137{
138 if ((ptel & HPTE_R_PP0) || (ptel & HPTE_R_PP) == PP_RWXX)
139 ptel = (ptel & ~HPTE_R_PP) | PP_RXXX;
140 else
141 ptel |= PP_RXRX;
142 return ptel;
143}
144
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000145static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
146{
147 unsigned int wimg = ptel & HPTE_R_WIMG;
148
149 /* Handle SAO */
150 if (wimg == (HPTE_R_W | HPTE_R_I | HPTE_R_M) &&
151 cpu_has_feature(CPU_FTR_ARCH_206))
152 wimg = HPTE_R_M;
153
154 if (!io_type)
155 return wimg == HPTE_R_M;
156
157 return (wimg & (HPTE_R_W | HPTE_R_I)) == io_type;
158}
159
Paul Mackerras342d3db2011-12-12 12:38:05 +0000160/*
161 * Lock and read a linux PTE. If it's present and writable, atomically
162 * set dirty and referenced bits and return the PTE, otherwise return 0.
163 */
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000164static inline pte_t kvmppc_read_update_linux_pte(pte_t *p, int writing)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000165{
166 pte_t pte, tmp;
167
168 /* wait until _PAGE_BUSY is clear then set it atomically */
169 __asm__ __volatile__ (
170 "1: ldarx %0,0,%3\n"
171 " andi. %1,%0,%4\n"
172 " bne- 1b\n"
173 " ori %1,%0,%4\n"
174 " stdcx. %1,0,%3\n"
175 " bne- 1b"
176 : "=&r" (pte), "=&r" (tmp), "=m" (*p)
177 : "r" (p), "i" (_PAGE_BUSY)
178 : "cc");
179
180 if (pte_present(pte)) {
181 pte = pte_mkyoung(pte);
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000182 if (writing && pte_write(pte))
Paul Mackerras342d3db2011-12-12 12:38:05 +0000183 pte = pte_mkdirty(pte);
184 }
185
186 *p = pte; /* clears _PAGE_BUSY */
187
188 return pte;
189}
190
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000191/* Return HPTE cache control bits corresponding to Linux pte bits */
192static inline unsigned long hpte_cache_bits(unsigned long pte_val)
193{
194#if _PAGE_NO_CACHE == HPTE_R_I && _PAGE_WRITETHRU == HPTE_R_W
195 return pte_val & (HPTE_R_W | HPTE_R_I);
196#else
197 return ((pte_val & _PAGE_NO_CACHE) ? HPTE_R_I : 0) +
198 ((pte_val & _PAGE_WRITETHRU) ? HPTE_R_W : 0);
199#endif
200}
201
Paul Mackerras697d3892011-12-12 12:36:37 +0000202static inline bool hpte_read_permission(unsigned long pp, unsigned long key)
203{
204 if (key)
205 return PP_RWRX <= pp && pp <= PP_RXRX;
206 return 1;
207}
208
209static inline bool hpte_write_permission(unsigned long pp, unsigned long key)
210{
211 if (key)
212 return pp == PP_RWRW;
213 return pp <= PP_RWRW;
214}
215
216static inline int hpte_get_skey_perm(unsigned long hpte_r, unsigned long amr)
217{
218 unsigned long skey;
219
220 skey = ((hpte_r & HPTE_R_KEY_HI) >> 57) |
221 ((hpte_r & HPTE_R_KEY_LO) >> 9);
222 return (amr >> (62 - 2 * skey)) & 3;
223}
224
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000225static inline void lock_rmap(unsigned long *rmap)
226{
227 do {
228 while (test_bit(KVMPPC_RMAP_LOCK_BIT, rmap))
229 cpu_relax();
230 } while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmap));
231}
232
233static inline void unlock_rmap(unsigned long *rmap)
234{
235 __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmap);
236}
237
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000238static inline bool slot_is_aligned(struct kvm_memory_slot *memslot,
239 unsigned long pagesize)
240{
241 unsigned long mask = (pagesize >> PAGE_SHIFT) - 1;
242
243 if (pagesize <= PAGE_SIZE)
244 return 1;
245 return !(memslot->base_gfn & mask) && !(memslot->npages & mask);
246}
247
Paul Mackerrasa2932922012-11-19 22:57:20 +0000248/*
249 * This works for 4k, 64k and 16M pages on POWER7,
250 * and 4k and 16M pages on PPC970.
251 */
252static inline unsigned long slb_pgsize_encoding(unsigned long psize)
253{
254 unsigned long senc = 0;
255
256 if (psize > 0x1000) {
257 senc = SLB_VSID_L;
258 if (psize == 0x10000)
259 senc |= SLB_VSID_LP_01;
260 }
261 return senc;
262}
263
264static inline int is_vrma_hpte(unsigned long hpte_v)
265{
266 return (hpte_v & ~0xffffffUL) ==
267 (HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)));
268}
269
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000270#ifdef CONFIG_KVM_BOOK3S_64_HV
271/*
272 * Note modification of an HPTE; set the HPTE modified bit
273 * if anyone is interested.
274 */
275static inline void note_hpte_modification(struct kvm *kvm,
276 struct revmap_entry *rev)
277{
278 if (atomic_read(&kvm->arch.hpte_mod_interest))
279 rev->guest_rpte |= HPTE_GR_MODIFIED;
280}
281#endif /* CONFIG_KVM_BOOK3S_64_HV */
282
Alexander Graf3ae07892010-04-16 00:11:37 +0200283#endif /* __ASM_KVM_BOOK3S_64_H__ */