Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * pSeries_lpar.c |
| 3 | * Copyright (C) 2001 Todd Inglett, IBM Corporation |
| 4 | * |
| 5 | * pSeries LPAR support. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Michael Ellerman | f7ebf35 | 2008-04-24 15:13:19 +1000 | [diff] [blame] | 22 | /* Enables debugging of low-level hash table routines - careful! */ |
| 23 | #undef DEBUG |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 24 | #define pr_fmt(fmt) "lpar: " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/dma-mapping.h> |
Benjamin Herrenschmidt | 463ce0e | 2005-11-23 17:56:06 +1100 | [diff] [blame] | 28 | #include <linux/console.h> |
Paul Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 29 | #include <linux/export.h> |
Anton Blanchard | 58995a9 | 2015-04-09 13:51:32 +1000 | [diff] [blame] | 30 | #include <linux/jump_label.h> |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 31 | #include <linux/delay.h> |
| 32 | #include <linux/stop_machine.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <asm/processor.h> |
| 34 | #include <asm/mmu.h> |
| 35 | #include <asm/page.h> |
| 36 | #include <asm/pgtable.h> |
| 37 | #include <asm/machdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/mmu_context.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/iommu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/tlb.h> |
| 41 | #include <asm/prom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/cputable.h> |
David Gibson | dcad47f | 2005-11-07 09:49:43 +1100 | [diff] [blame] | 43 | #include <asm/udbg.h> |
Paul Mackerras | 2249ca9 | 2005-11-07 13:18:13 +1100 | [diff] [blame] | 44 | #include <asm/smp.h> |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 45 | #include <asm/trace.h> |
Stephen Rothwell | f533927 | 2012-03-15 18:18:00 +0000 | [diff] [blame] | 46 | #include <asm/firmware.h> |
Deepthi Dharwar | 212bebb | 2013-08-22 15:23:52 +0530 | [diff] [blame] | 47 | #include <asm/plpar_wrappers.h> |
Hari Bathini | c1caae3 | 2014-12-18 23:36:55 +0530 | [diff] [blame] | 48 | #include <asm/kexec.h> |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 49 | #include <asm/fadump.h> |
Daniel Axtens | 42f5b4c | 2016-05-18 11:16:50 +1000 | [diff] [blame] | 50 | #include <asm/asm-prototypes.h> |
Aravinda Prasad | c6c26fb | 2018-10-16 17:20:05 +0530 | [diff] [blame] | 51 | #include <asm/debugfs.h> |
Michael Ellerman | a121872 | 2005-11-03 15:33:31 +1100 | [diff] [blame] | 52 | |
Michael Ellerman | 21cf913 | 2008-04-16 13:51:48 +1000 | [diff] [blame] | 53 | #include "pseries.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 55 | /* Flag bits for H_BULK_REMOVE */ |
| 56 | #define HBR_REQUEST 0x4000000000000000UL |
| 57 | #define HBR_RESPONSE 0x8000000000000000UL |
| 58 | #define HBR_END 0xc000000000000000UL |
| 59 | #define HBR_AVPN 0x0200000000000000UL |
| 60 | #define HBR_ANDCOND 0x0100000000000000UL |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 63 | /* in hvCall.S */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | EXPORT_SYMBOL(plpar_hcall); |
Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 65 | EXPORT_SYMBOL(plpar_hcall9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | EXPORT_SYMBOL(plpar_hcall_norets); |
Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | void vpa_init(int cpu) |
| 69 | { |
| 70 | int hwcpu = get_hard_smp_processor_id(cpu); |
Michael Neuling | 2f6093c | 2006-08-07 16:19:19 +1000 | [diff] [blame] | 71 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | long ret; |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 73 | struct paca_struct *pp; |
| 74 | struct dtl_entry *dtl; |
Olof Johansson | 233ccd0 | 2005-09-03 15:55:59 -0700 | [diff] [blame] | 75 | |
Michael Ellerman | b89bdfb | 2013-08-15 15:22:15 +1000 | [diff] [blame] | 76 | /* |
| 77 | * The spec says it "may be problematic" if CPU x registers the VPA of |
| 78 | * CPU y. We should never do that, but wail if we ever do. |
| 79 | */ |
| 80 | WARN_ON(cpu != smp_processor_id()); |
| 81 | |
Olof Johansson | 233ccd0 | 2005-09-03 15:55:59 -0700 | [diff] [blame] | 82 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
Paul Mackerras | 8154c5d | 2010-08-12 20:18:15 +0000 | [diff] [blame] | 83 | lppaca_of(cpu).vmxregs_in_use = 1; |
Olof Johansson | 233ccd0 | 2005-09-03 15:55:59 -0700 | [diff] [blame] | 84 | |
Michael Ellerman | 6e0b8bc9 | 2013-06-28 18:15:18 +1000 | [diff] [blame] | 85 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 86 | lppaca_of(cpu).ebb_regs_in_use = 1; |
| 87 | |
Paul Mackerras | 8154c5d | 2010-08-12 20:18:15 +0000 | [diff] [blame] | 88 | addr = __pa(&lppaca_of(cpu)); |
Michael Neuling | 2f6093c | 2006-08-07 16:19:19 +1000 | [diff] [blame] | 89 | ret = register_vpa(hwcpu, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Michael Neuling | 2f6093c | 2006-08-07 16:19:19 +1000 | [diff] [blame] | 91 | if (ret) { |
Anton Blanchard | 711ef84 | 2011-07-25 01:46:33 +0000 | [diff] [blame] | 92 | pr_err("WARNING: VPA registration for cpu %d (hw %d) of area " |
| 93 | "%lx failed with %ld\n", cpu, hwcpu, addr, ret); |
Michael Neuling | 2f6093c | 2006-08-07 16:19:19 +1000 | [diff] [blame] | 94 | return; |
| 95 | } |
Aneesh Kumar K.V | d8c476e | 2016-04-29 23:26:08 +1000 | [diff] [blame] | 96 | |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 97 | #ifdef CONFIG_PPC_BOOK3S_64 |
Michael Neuling | 2f6093c | 2006-08-07 16:19:19 +1000 | [diff] [blame] | 98 | /* |
| 99 | * PAPR says this feature is SLB-Buffer but firmware never |
| 100 | * reports that. All SPLPAR support SLB shadow buffer. |
| 101 | */ |
Aneesh Kumar K.V | d8c476e | 2016-04-29 23:26:08 +1000 | [diff] [blame] | 102 | if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) { |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 103 | addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr); |
Michael Neuling | 2f6093c | 2006-08-07 16:19:19 +1000 | [diff] [blame] | 104 | ret = register_slb_shadow(hwcpu, addr); |
| 105 | if (ret) |
Anton Blanchard | 711ef84 | 2011-07-25 01:46:33 +0000 | [diff] [blame] | 106 | pr_err("WARNING: SLB shadow buffer registration for " |
| 107 | "cpu %d (hw %d) of area %lx failed with %ld\n", |
| 108 | cpu, hwcpu, addr, ret); |
Michael Neuling | 2f6093c | 2006-08-07 16:19:19 +1000 | [diff] [blame] | 109 | } |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 110 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * Register dispatch trace log, if one has been allocated. |
| 114 | */ |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 115 | pp = paca_ptrs[cpu]; |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 116 | dtl = pp->dispatch_log; |
| 117 | if (dtl) { |
| 118 | pp->dtl_ridx = 0; |
| 119 | pp->dtl_curr = dtl; |
| 120 | lppaca_of(cpu).dtl_idx = 0; |
| 121 | |
| 122 | /* hypervisor reads buffer length from this field */ |
Anton Blanchard | 7ffcf8e | 2013-08-07 02:01:46 +1000 | [diff] [blame] | 123 | dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES); |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 124 | ret = register_dtl(hwcpu, __pa(dtl)); |
| 125 | if (ret) |
Anton Blanchard | 711ef84 | 2011-07-25 01:46:33 +0000 | [diff] [blame] | 126 | pr_err("WARNING: DTL registration of cpu %d (hw %d) " |
| 127 | "failed with %ld\n", smp_processor_id(), |
| 128 | hwcpu, ret); |
Paul Mackerras | cf9efce | 2010-08-26 19:56:43 +0000 | [diff] [blame] | 129 | lppaca_of(cpu).dtl_enable_mask = 2; |
| 130 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 133 | #ifdef CONFIG_PPC_BOOK3S_64 |
Aneesh Kumar K.V | d8c476e | 2016-04-29 23:26:08 +1000 | [diff] [blame] | 134 | |
Geoff Levand | 035223f | 2006-10-05 11:35:10 -0700 | [diff] [blame] | 135 | static long pSeries_lpar_hpte_insert(unsigned long hpte_group, |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 136 | unsigned long vpn, unsigned long pa, |
| 137 | unsigned long rflags, unsigned long vflags, |
Aneesh Kumar K.V | b1022fb | 2013-04-28 09:37:35 +0000 | [diff] [blame] | 138 | int psize, int apsize, int ssize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | unsigned long lpar_rc; |
| 141 | unsigned long flags; |
| 142 | unsigned long slot; |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 143 | unsigned long hpte_v, hpte_r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 145 | if (!(vflags & HPTE_V_BOLTED)) |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 146 | pr_devel("hpte_insert(group=%lx, vpn=%016lx, " |
| 147 | "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n", |
| 148 | hpte_group, vpn, pa, rflags, vflags, psize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Aneesh Kumar K.V | b1022fb | 2013-04-28 09:37:35 +0000 | [diff] [blame] | 150 | hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID; |
Paul Mackerras | 6b243fc | 2016-11-11 16:55:03 +1100 | [diff] [blame] | 151 | hpte_r = hpte_encode_r(pa, psize, apsize) | rflags; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 152 | |
| 153 | if (!(vflags & HPTE_V_BOLTED)) |
Michael Ellerman | 551a232 | 2009-06-17 18:13:50 +0000 | [diff] [blame] | 154 | pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | /* Now fill in the actual HPTE */ |
| 157 | /* Set CEC cookie to 0 */ |
| 158 | /* Zero page = 0 */ |
| 159 | /* I-cache Invalidate = 0 */ |
| 160 | /* I-cache synchronize = 0 */ |
| 161 | /* Exact = 0 */ |
| 162 | flags = 0; |
| 163 | |
Brian King | 9ee820f | 2011-05-04 16:01:20 +1000 | [diff] [blame] | 164 | if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N)) |
| 165 | flags |= H_COALESCE_CAND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Anton Blanchard | b9377ff | 2006-07-19 08:01:28 +1000 | [diff] [blame] | 167 | lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot); |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 168 | if (unlikely(lpar_rc == H_PTEG_FULL)) { |
Aneesh Kumar K.V | ca42d8d2d | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 169 | pr_devel("Hash table group is full\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return -1; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Since we try and ioremap PHBs we don't own, the pte insert |
| 175 | * will fail. However we must catch the failure in hash_page |
| 176 | * or we will loop forever, so return -2 in this case. |
| 177 | */ |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 178 | if (unlikely(lpar_rc != H_SUCCESS)) { |
Aneesh Kumar K.V | ca42d8d2d | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 179 | pr_err("Failed hash pte insert with error %ld\n", lpar_rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return -2; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 181 | } |
| 182 | if (!(vflags & HPTE_V_BOLTED)) |
Michael Ellerman | 551a232 | 2009-06-17 18:13:50 +0000 | [diff] [blame] | 183 | pr_devel(" -> slot: %lu\n", slot & 7); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | /* Because of iSeries, we have to pass down the secondary |
| 186 | * bucket bit here as well |
| 187 | */ |
David Gibson | 96e2844 | 2005-07-13 01:11:42 -0700 | [diff] [blame] | 188 | return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock); |
| 192 | |
| 193 | static long pSeries_lpar_hpte_remove(unsigned long hpte_group) |
| 194 | { |
| 195 | unsigned long slot_offset; |
| 196 | unsigned long lpar_rc; |
| 197 | int i; |
| 198 | unsigned long dummy1, dummy2; |
| 199 | |
| 200 | /* pick a random slot to start at */ |
| 201 | slot_offset = mftb() & 0x7; |
| 202 | |
| 203 | for (i = 0; i < HPTES_PER_GROUP; i++) { |
| 204 | |
| 205 | /* don't remove a bolted entry */ |
| 206 | lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset, |
| 207 | (0x1UL << 4), &dummy1, &dummy2); |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 208 | if (lpar_rc == H_SUCCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return i; |
Michael Wolf | 9fb2640 | 2013-04-05 10:41:40 +0000 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * The test for adjunct partition is performed before the |
| 213 | * ANDCOND test. H_RESOURCE may be returned, so we need to |
| 214 | * check for that as well. |
| 215 | */ |
| 216 | BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | slot_offset++; |
| 219 | slot_offset &= 0x7; |
| 220 | } |
| 221 | |
| 222 | return -1; |
| 223 | } |
| 224 | |
Anton Blanchard | 5246ade | 2016-10-01 20:41:56 +1000 | [diff] [blame] | 225 | static void manual_hpte_clear_all(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
| 227 | unsigned long size_bytes = 1UL << ppc64_pft_size; |
| 228 | unsigned long hpte_count = size_bytes >> 4; |
Michael Neuling | d504bed | 2010-05-10 20:28:26 +0000 | [diff] [blame] | 229 | struct { |
| 230 | unsigned long pteh; |
| 231 | unsigned long ptel; |
| 232 | } ptes[4]; |
Sachin P. Sant | b7abc5c | 2007-06-14 15:31:34 +1000 | [diff] [blame] | 233 | long lpar_rc; |
Anton Blanchard | bed9a31 | 2011-07-26 18:15:03 +0000 | [diff] [blame] | 234 | unsigned long i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Michael Neuling | d504bed | 2010-05-10 20:28:26 +0000 | [diff] [blame] | 236 | /* Read in batches of 4, |
| 237 | * invalidate only valid entries not in the VRMA |
| 238 | * hpte_count will be a multiple of 4 |
| 239 | */ |
| 240 | for (i = 0; i < hpte_count; i += 4) { |
| 241 | lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes); |
Aneesh Kumar K.V | ca42d8d2d | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 242 | if (lpar_rc != H_SUCCESS) { |
| 243 | pr_info("Failed to read hash page table at %ld err %ld\n", |
| 244 | i, lpar_rc); |
Michael Neuling | d504bed | 2010-05-10 20:28:26 +0000 | [diff] [blame] | 245 | continue; |
Aneesh Kumar K.V | ca42d8d2d | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 246 | } |
Michael Neuling | d504bed | 2010-05-10 20:28:26 +0000 | [diff] [blame] | 247 | for (j = 0; j < 4; j++){ |
| 248 | if ((ptes[j].pteh & HPTE_V_VRMA_MASK) == |
| 249 | HPTE_V_VRMA_MASK) |
| 250 | continue; |
| 251 | if (ptes[j].pteh & HPTE_V_VALID) |
| 252 | plpar_pte_remove_raw(0, i + j, 0, |
| 253 | &(ptes[j].pteh), &(ptes[j].ptel)); |
Sachin P. Sant | b7abc5c | 2007-06-14 15:31:34 +1000 | [diff] [blame] | 254 | } |
| 255 | } |
Anton Blanchard | 5246ade | 2016-10-01 20:41:56 +1000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static int hcall_hpte_clear_all(void) |
| 259 | { |
| 260 | int rc; |
| 261 | |
| 262 | do { |
| 263 | rc = plpar_hcall_norets(H_CLEAR_HPT); |
| 264 | } while (rc == H_CONTINUE); |
| 265 | |
| 266 | return rc; |
| 267 | } |
| 268 | |
| 269 | static void pseries_hpte_clear_all(void) |
| 270 | { |
| 271 | int rc; |
| 272 | |
| 273 | rc = hcall_hpte_clear_all(); |
| 274 | if (rc != H_SUCCESS) |
| 275 | manual_hpte_clear_all(); |
Anton Blanchard | e844b1e | 2013-11-20 22:14:59 +1100 | [diff] [blame] | 276 | |
| 277 | #ifdef __LITTLE_ENDIAN__ |
Hari Bathini | 408cddd | 2014-10-01 12:32:30 +0530 | [diff] [blame] | 278 | /* |
| 279 | * Reset exceptions to big endian. |
| 280 | * |
| 281 | * FIXME this is a hack for kexec, we need to reset the exception |
| 282 | * endian before starting the new kernel and this is a convenient place |
| 283 | * to do it. |
| 284 | * |
| 285 | * This is also called on boot when a fadump happens. In that case we |
| 286 | * must not change the exception endian mode. |
| 287 | */ |
Benjamin Herrenschmidt | d3cbff1 | 2016-07-05 15:03:49 +1000 | [diff] [blame] | 288 | if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active()) |
| 289 | pseries_big_endian_exceptions(); |
Anton Blanchard | e844b1e | 2013-11-20 22:14:59 +1100 | [diff] [blame] | 290 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* |
| 294 | * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and |
| 295 | * the low 3 bits of flags happen to line up. So no transform is needed. |
| 296 | * We can probably optimize here and assume the high bits of newpp are |
| 297 | * already zero. For now I am paranoid. |
| 298 | */ |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 299 | static long pSeries_lpar_hpte_updatepp(unsigned long slot, |
| 300 | unsigned long newpp, |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 301 | unsigned long vpn, |
Aneesh Kumar K.V | db3d853 | 2013-06-20 14:30:13 +0530 | [diff] [blame] | 302 | int psize, int apsize, |
Aneesh Kumar K.V | aefa568 | 2014-12-04 11:00:14 +0530 | [diff] [blame] | 303 | int ssize, unsigned long inv_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
| 305 | unsigned long lpar_rc; |
Balbir Singh | e71ff98 | 2017-06-29 03:04:07 +1000 | [diff] [blame] | 306 | unsigned long flags; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 307 | unsigned long want_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 309 | want_v = hpte_encode_avpn(vpn, psize, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Balbir Singh | e71ff98 | 2017-06-29 03:04:07 +1000 | [diff] [blame] | 311 | flags = (newpp & 7) | H_AVPN; |
| 312 | if (mmu_has_feature(MMU_FTR_KERNEL_RO)) |
| 313 | /* Move pp0 into bit 8 (IBM 55) */ |
| 314 | flags |= (newpp & HPTE_R_PP0) >> 55; |
| 315 | |
Alexey Kardashevskiy | a8c0bf3 | 2018-01-09 16:52:14 +1100 | [diff] [blame] | 316 | pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...", |
| 317 | want_v, slot, flags, psize); |
| 318 | |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 319 | lpar_rc = plpar_pte_protect(flags, slot, want_v); |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 320 | |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 321 | if (lpar_rc == H_NOT_FOUND) { |
Michael Ellerman | 551a232 | 2009-06-17 18:13:50 +0000 | [diff] [blame] | 322 | pr_devel("not found !\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return -1; |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 324 | } |
| 325 | |
Michael Ellerman | 551a232 | 2009-06-17 18:13:50 +0000 | [diff] [blame] | 326 | pr_devel("ok\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 328 | BUG_ON(lpar_rc != H_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 333 | static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 335 | long lpar_rc; |
| 336 | unsigned long i, j; |
| 337 | struct { |
| 338 | unsigned long pteh; |
| 339 | unsigned long ptel; |
| 340 | } ptes[4]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 342 | for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 344 | lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes); |
Aneesh Kumar K.V | ca42d8d2d | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 345 | if (lpar_rc != H_SUCCESS) { |
| 346 | pr_info("Failed to read hash page table at %ld err %ld\n", |
| 347 | hpte_group, lpar_rc); |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 348 | continue; |
Aneesh Kumar K.V | ca42d8d2d | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 349 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 351 | for (j = 0; j < 4; j++) { |
| 352 | if (HPTE_V_COMPARE(ptes[j].pteh, want_v) && |
| 353 | (ptes[j].pteh & HPTE_V_VALID)) |
| 354 | return i + j; |
| 355 | } |
| 356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 358 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 361 | static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | long slot; |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 364 | unsigned long hash; |
| 365 | unsigned long want_v; |
| 366 | unsigned long hpte_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 368 | hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize); |
| 369 | want_v = hpte_encode_avpn(vpn, psize, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 371 | /* Bolted entries are always in the primary group */ |
Aneesh Kumar K.V | 4ad90c8 | 2015-12-01 09:06:59 +0530 | [diff] [blame] | 372 | hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 373 | slot = __pSeries_lpar_hpte_find(want_v, hpte_group); |
| 374 | if (slot < 0) |
| 375 | return -1; |
| 376 | return hpte_group + slot; |
| 377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp, |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 380 | unsigned long ea, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 381 | int psize, int ssize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 383 | unsigned long vpn; |
| 384 | unsigned long lpar_rc, slot, vsid, flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 386 | vsid = get_kernel_vsid(ea, ssize); |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 387 | vpn = hpt_vpn(ea, vsid, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 389 | slot = pSeries_lpar_hpte_find(vpn, psize, ssize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | BUG_ON(slot == -1); |
| 391 | |
| 392 | flags = newpp & 7; |
Balbir Singh | e71ff98 | 2017-06-29 03:04:07 +1000 | [diff] [blame] | 393 | if (mmu_has_feature(MMU_FTR_KERNEL_RO)) |
| 394 | /* Move pp0 into bit 8 (IBM 55) */ |
| 395 | flags |= (newpp & HPTE_R_PP0) >> 55; |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | lpar_rc = plpar_pte_protect(flags, slot, 0); |
| 398 | |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 399 | BUG_ON(lpar_rc != H_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 402 | static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn, |
Aneesh Kumar K.V | db3d853 | 2013-06-20 14:30:13 +0530 | [diff] [blame] | 403 | int psize, int apsize, |
| 404 | int ssize, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Benjamin Herrenschmidt | 3c726f8 | 2005-11-07 11:06:55 +1100 | [diff] [blame] | 406 | unsigned long want_v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | unsigned long lpar_rc; |
| 408 | unsigned long dummy1, dummy2; |
| 409 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 410 | pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n", |
| 411 | slot, vpn, psize, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 413 | want_v = hpte_encode_avpn(vpn, psize, ssize); |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 414 | lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2); |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 415 | if (lpar_rc == H_NOT_FOUND) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | return; |
| 417 | |
Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 418 | BUG_ON(lpar_rc != H_SUCCESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Laurent Dufour | ba2dd8a | 2018-08-20 16:29:36 +0200 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * As defined in the PAPR's section 14.5.4.1.8 |
| 424 | * The control mask doesn't include the returned reference and change bit from |
| 425 | * the processed PTE. |
| 426 | */ |
| 427 | #define HBLKR_AVPN 0x0100000000000000UL |
| 428 | #define HBLKR_CTRL_MASK 0xf800000000000000UL |
| 429 | #define HBLKR_CTRL_SUCCESS 0x8000000000000000UL |
| 430 | #define HBLKR_CTRL_ERRNOTFOUND 0x8800000000000000UL |
| 431 | #define HBLKR_CTRL_ERRBUSY 0xa000000000000000UL |
| 432 | |
| 433 | /** |
| 434 | * H_BLOCK_REMOVE caller. |
| 435 | * @idx should point to the latest @param entry set with a PTEX. |
| 436 | * If PTE cannot be processed because another CPUs has already locked that |
| 437 | * group, those entries are put back in @param starting at index 1. |
| 438 | * If entries has to be retried and @retry_busy is set to true, these entries |
| 439 | * are retried until success. If @retry_busy is set to false, the returned |
| 440 | * is the number of entries yet to process. |
| 441 | */ |
| 442 | static unsigned long call_block_remove(unsigned long idx, unsigned long *param, |
| 443 | bool retry_busy) |
| 444 | { |
| 445 | unsigned long i, rc, new_idx; |
| 446 | unsigned long retbuf[PLPAR_HCALL9_BUFSIZE]; |
| 447 | |
| 448 | if (idx < 2) { |
| 449 | pr_warn("Unexpected empty call to H_BLOCK_REMOVE"); |
| 450 | return 0; |
| 451 | } |
| 452 | again: |
| 453 | new_idx = 0; |
| 454 | if (idx > PLPAR_HCALL9_BUFSIZE) { |
| 455 | pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx); |
| 456 | idx = PLPAR_HCALL9_BUFSIZE; |
| 457 | } else if (idx < PLPAR_HCALL9_BUFSIZE) |
| 458 | param[idx] = HBR_END; |
| 459 | |
| 460 | rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf, |
| 461 | param[0], /* AVA */ |
| 462 | param[1], param[2], param[3], param[4], /* TS0-7 */ |
| 463 | param[5], param[6], param[7], param[8]); |
| 464 | if (rc == H_SUCCESS) |
| 465 | return 0; |
| 466 | |
| 467 | BUG_ON(rc != H_PARTIAL); |
| 468 | |
| 469 | /* Check that the unprocessed entries were 'not found' or 'busy' */ |
| 470 | for (i = 0; i < idx-1; i++) { |
| 471 | unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK; |
| 472 | |
| 473 | if (ctrl == HBLKR_CTRL_ERRBUSY) { |
| 474 | param[++new_idx] = param[i+1]; |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | BUG_ON(ctrl != HBLKR_CTRL_SUCCESS |
| 479 | && ctrl != HBLKR_CTRL_ERRNOTFOUND); |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * If there were entries found busy, retry these entries if requested, |
| 484 | * of if all the entries have to be retried. |
| 485 | */ |
| 486 | if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) { |
| 487 | idx = new_idx + 1; |
| 488 | goto again; |
| 489 | } |
| 490 | |
| 491 | return new_idx; |
| 492 | } |
| 493 | |
Aneesh Kumar K.V | e34aa03 | 2015-12-01 09:06:53 +0530 | [diff] [blame] | 494 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 495 | /* |
| 496 | * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need |
| 497 | * to make sure that we avoid bouncing the hypervisor tlbie lock. |
| 498 | */ |
| 499 | #define PPC64_HUGE_HPTE_BATCH 12 |
| 500 | |
Laurent Dufour | ba2dd8a | 2018-08-20 16:29:36 +0200 | [diff] [blame] | 501 | static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn, |
| 502 | int count, int psize, int ssize) |
| 503 | { |
| 504 | unsigned long param[PLPAR_HCALL9_BUFSIZE]; |
| 505 | unsigned long shift, current_vpgb, vpgb; |
| 506 | int i, pix = 0; |
| 507 | |
| 508 | shift = mmu_psize_defs[psize].shift; |
| 509 | |
| 510 | for (i = 0; i < count; i++) { |
| 511 | /* |
| 512 | * Shifting 3 bits more on the right to get a |
| 513 | * 8 pages aligned virtual addresse. |
| 514 | */ |
| 515 | vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3)); |
| 516 | if (!pix || vpgb != current_vpgb) { |
| 517 | /* |
| 518 | * Need to start a new 8 pages block, flush |
| 519 | * the current one if needed. |
| 520 | */ |
| 521 | if (pix) |
| 522 | (void)call_block_remove(pix, param, true); |
| 523 | current_vpgb = vpgb; |
| 524 | param[0] = hpte_encode_avpn(vpn[i], psize, ssize); |
| 525 | pix = 1; |
| 526 | } |
| 527 | |
| 528 | param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i]; |
| 529 | if (pix == PLPAR_HCALL9_BUFSIZE) { |
| 530 | pix = call_block_remove(pix, param, false); |
| 531 | /* |
| 532 | * pix = 0 means that all the entries were |
| 533 | * removed, we can start a new block. |
| 534 | * Otherwise, this means that there are entries |
| 535 | * to retry, and pix points to latest one, so |
| 536 | * we should increment it and try to continue |
| 537 | * the same block. |
| 538 | */ |
| 539 | if (pix) |
| 540 | pix++; |
| 541 | } |
| 542 | } |
| 543 | if (pix) |
| 544 | (void)call_block_remove(pix, param, true); |
| 545 | } |
| 546 | |
| 547 | static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn, |
| 548 | int count, int psize, int ssize) |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 549 | { |
Laurent Dufour | 05af40e | 2016-10-06 15:33:21 +0200 | [diff] [blame] | 550 | unsigned long param[PLPAR_HCALL9_BUFSIZE]; |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 551 | int i = 0, pix = 0, rc; |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 552 | |
| 553 | for (i = 0; i < count; i++) { |
| 554 | |
| 555 | if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) { |
| 556 | pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0, |
| 557 | ssize, 0); |
| 558 | } else { |
| 559 | param[pix] = HBR_REQUEST | HBR_AVPN | slot[i]; |
| 560 | param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize); |
| 561 | pix += 2; |
| 562 | if (pix == 8) { |
| 563 | rc = plpar_hcall9(H_BULK_REMOVE, param, |
| 564 | param[0], param[1], param[2], |
| 565 | param[3], param[4], param[5], |
| 566 | param[6], param[7]); |
| 567 | BUG_ON(rc != H_SUCCESS); |
| 568 | pix = 0; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | if (pix) { |
| 573 | param[pix] = HBR_END; |
| 574 | rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1], |
| 575 | param[2], param[3], param[4], param[5], |
| 576 | param[6], param[7]); |
| 577 | BUG_ON(rc != H_SUCCESS); |
| 578 | } |
Laurent Dufour | ba2dd8a | 2018-08-20 16:29:36 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot, |
| 582 | unsigned long *vpn, |
| 583 | int count, int psize, |
| 584 | int ssize) |
| 585 | { |
| 586 | unsigned long flags = 0; |
| 587 | int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE); |
| 588 | |
| 589 | if (lock_tlbie) |
| 590 | spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags); |
| 591 | |
| 592 | if (firmware_has_feature(FW_FEATURE_BLOCK_REMOVE)) |
| 593 | hugepage_block_invalidate(slot, vpn, count, psize, ssize); |
| 594 | else |
| 595 | hugepage_bulk_invalidate(slot, vpn, count, psize, ssize); |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 596 | |
| 597 | if (lock_tlbie) |
| 598 | spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags); |
| 599 | } |
| 600 | |
Aneesh Kumar K.V | fa1f8ae | 2014-08-13 12:31:58 +0530 | [diff] [blame] | 601 | static void pSeries_lpar_hugepage_invalidate(unsigned long vsid, |
| 602 | unsigned long addr, |
| 603 | unsigned char *hpte_slot_array, |
Aneesh Kumar K.V | d557b09 | 2014-11-02 21:15:28 +0530 | [diff] [blame] | 604 | int psize, int ssize, int local) |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 605 | { |
Aneesh Kumar K.V | fa1f8ae | 2014-08-13 12:31:58 +0530 | [diff] [blame] | 606 | int i, index = 0; |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 607 | unsigned long s_addr = addr; |
| 608 | unsigned int max_hpte_count, valid; |
| 609 | unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH]; |
| 610 | unsigned long slot_array[PPC64_HUGE_HPTE_BATCH]; |
Aneesh Kumar K.V | fa1f8ae | 2014-08-13 12:31:58 +0530 | [diff] [blame] | 611 | unsigned long shift, hidx, vpn = 0, hash, slot; |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 612 | |
| 613 | shift = mmu_psize_defs[psize].shift; |
| 614 | max_hpte_count = 1U << (PMD_SHIFT - shift); |
| 615 | |
| 616 | for (i = 0; i < max_hpte_count; i++) { |
| 617 | valid = hpte_valid(hpte_slot_array, i); |
| 618 | if (!valid) |
| 619 | continue; |
| 620 | hidx = hpte_hash_index(hpte_slot_array, i); |
| 621 | |
| 622 | /* get the vpn */ |
| 623 | addr = s_addr + (i * (1ul << shift)); |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 624 | vpn = hpt_vpn(addr, vsid, ssize); |
| 625 | hash = hpt_hash(vpn, shift, ssize); |
| 626 | if (hidx & _PTEIDX_SECONDARY) |
| 627 | hash = ~hash; |
| 628 | |
| 629 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 630 | slot += hidx & _PTEIDX_GROUP_IX; |
| 631 | |
| 632 | slot_array[index] = slot; |
| 633 | vpn_array[index] = vpn; |
| 634 | if (index == PPC64_HUGE_HPTE_BATCH - 1) { |
| 635 | /* |
| 636 | * Now do a bluk invalidate |
| 637 | */ |
| 638 | __pSeries_lpar_hugepage_invalidate(slot_array, |
| 639 | vpn_array, |
| 640 | PPC64_HUGE_HPTE_BATCH, |
| 641 | psize, ssize); |
| 642 | index = 0; |
| 643 | } else |
| 644 | index++; |
| 645 | } |
| 646 | if (index) |
| 647 | __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array, |
| 648 | index, psize, ssize); |
| 649 | } |
Aneesh Kumar K.V | e34aa03 | 2015-12-01 09:06:53 +0530 | [diff] [blame] | 650 | #else |
| 651 | static void pSeries_lpar_hugepage_invalidate(unsigned long vsid, |
| 652 | unsigned long addr, |
| 653 | unsigned char *hpte_slot_array, |
| 654 | int psize, int ssize, int local) |
| 655 | { |
| 656 | WARN(1, "%s called without THP support\n", __func__); |
| 657 | } |
| 658 | #endif |
Aneesh Kumar K.V | 1a52728 | 2013-06-20 14:30:27 +0530 | [diff] [blame] | 659 | |
David Gibson | 27828f9 | 2016-02-09 13:32:41 +1000 | [diff] [blame] | 660 | static int pSeries_lpar_hpte_removebolted(unsigned long ea, |
| 661 | int psize, int ssize) |
Badari Pulavarty | f8c8803 | 2008-01-29 09:19:24 +1100 | [diff] [blame] | 662 | { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 663 | unsigned long vpn; |
| 664 | unsigned long slot, vsid; |
Badari Pulavarty | f8c8803 | 2008-01-29 09:19:24 +1100 | [diff] [blame] | 665 | |
| 666 | vsid = get_kernel_vsid(ea, ssize); |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 667 | vpn = hpt_vpn(ea, vsid, ssize); |
Badari Pulavarty | f8c8803 | 2008-01-29 09:19:24 +1100 | [diff] [blame] | 668 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 669 | slot = pSeries_lpar_hpte_find(vpn, psize, ssize); |
David Gibson | 27828f9 | 2016-02-09 13:32:41 +1000 | [diff] [blame] | 670 | if (slot == -1) |
| 671 | return -ENOENT; |
| 672 | |
Aneesh Kumar K.V | db3d853 | 2013-06-20 14:30:13 +0530 | [diff] [blame] | 673 | /* |
| 674 | * lpar doesn't use the passed actual page size |
| 675 | */ |
| 676 | pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0); |
David Gibson | 27828f9 | 2016-02-09 13:32:41 +1000 | [diff] [blame] | 677 | return 0; |
Badari Pulavarty | f8c8803 | 2008-01-29 09:19:24 +1100 | [diff] [blame] | 678 | } |
| 679 | |
Laurent Dufour | 0effa48 | 2018-08-20 16:29:35 +0200 | [diff] [blame] | 680 | |
| 681 | static inline unsigned long compute_slot(real_pte_t pte, |
| 682 | unsigned long vpn, |
| 683 | unsigned long index, |
| 684 | unsigned long shift, |
| 685 | int ssize) |
| 686 | { |
| 687 | unsigned long slot, hash, hidx; |
| 688 | |
| 689 | hash = hpt_hash(vpn, shift, ssize); |
| 690 | hidx = __rpte_to_hidx(pte, index); |
| 691 | if (hidx & _PTEIDX_SECONDARY) |
| 692 | hash = ~hash; |
| 693 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 694 | slot += hidx & _PTEIDX_GROUP_IX; |
| 695 | return slot; |
| 696 | } |
| 697 | |
Laurent Dufour | ba2dd8a | 2018-08-20 16:29:36 +0200 | [diff] [blame] | 698 | /** |
| 699 | * The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are |
| 700 | * "all within the same naturally aligned 8 page virtual address block". |
| 701 | */ |
| 702 | static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch, |
| 703 | unsigned long *param) |
| 704 | { |
| 705 | unsigned long vpn; |
| 706 | unsigned long i, pix = 0; |
| 707 | unsigned long index, shift, slot, current_vpgb, vpgb; |
| 708 | real_pte_t pte; |
| 709 | int psize, ssize; |
| 710 | |
| 711 | psize = batch->psize; |
| 712 | ssize = batch->ssize; |
| 713 | |
| 714 | for (i = 0; i < number; i++) { |
| 715 | vpn = batch->vpn[i]; |
| 716 | pte = batch->pte[i]; |
| 717 | pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) { |
| 718 | /* |
| 719 | * Shifting 3 bits more on the right to get a |
| 720 | * 8 pages aligned virtual addresse. |
| 721 | */ |
| 722 | vpgb = (vpn >> (shift - VPN_SHIFT + 3)); |
| 723 | if (!pix || vpgb != current_vpgb) { |
| 724 | /* |
| 725 | * Need to start a new 8 pages block, flush |
| 726 | * the current one if needed. |
| 727 | */ |
| 728 | if (pix) |
| 729 | (void)call_block_remove(pix, param, |
| 730 | true); |
| 731 | current_vpgb = vpgb; |
| 732 | param[0] = hpte_encode_avpn(vpn, psize, |
| 733 | ssize); |
| 734 | pix = 1; |
| 735 | } |
| 736 | |
| 737 | slot = compute_slot(pte, vpn, index, shift, ssize); |
| 738 | param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot; |
| 739 | |
| 740 | if (pix == PLPAR_HCALL9_BUFSIZE) { |
| 741 | pix = call_block_remove(pix, param, false); |
| 742 | /* |
| 743 | * pix = 0 means that all the entries were |
| 744 | * removed, we can start a new block. |
| 745 | * Otherwise, this means that there are entries |
| 746 | * to retry, and pix points to latest one, so |
| 747 | * we should increment it and try to continue |
| 748 | * the same block. |
| 749 | */ |
| 750 | if (pix) |
| 751 | pix++; |
| 752 | } |
| 753 | } pte_iterate_hashed_end(); |
| 754 | } |
| 755 | |
| 756 | if (pix) |
| 757 | (void)call_block_remove(pix, param, true); |
| 758 | } |
| 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | /* |
| 761 | * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie |
| 762 | * lock. |
| 763 | */ |
Geoff Levand | 035223f | 2006-10-05 11:35:10 -0700 | [diff] [blame] | 764 | static void pSeries_lpar_flush_hash_range(unsigned long number, int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 766 | unsigned long vpn; |
Paul Mackerras | f03e64f | 2007-02-06 21:10:31 +1100 | [diff] [blame] | 767 | unsigned long i, pix, rc; |
Paul Mackerras | 12e86f9 | 2007-02-08 15:02:35 +1100 | [diff] [blame] | 768 | unsigned long flags = 0; |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 769 | struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch); |
Matt Evans | 44ae3ab | 2011-04-06 19:48:50 +0000 | [diff] [blame] | 770 | int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE); |
Laurent Dufour | 05af40e | 2016-10-06 15:33:21 +0200 | [diff] [blame] | 771 | unsigned long param[PLPAR_HCALL9_BUFSIZE]; |
Laurent Dufour | 0effa48 | 2018-08-20 16:29:35 +0200 | [diff] [blame] | 772 | unsigned long index, shift, slot; |
Paul Mackerras | f03e64f | 2007-02-06 21:10:31 +1100 | [diff] [blame] | 773 | real_pte_t pte; |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 774 | int psize, ssize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
| 776 | if (lock_tlbie) |
| 777 | spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags); |
| 778 | |
Laurent Dufour | ba2dd8a | 2018-08-20 16:29:36 +0200 | [diff] [blame] | 779 | if (firmware_has_feature(FW_FEATURE_BLOCK_REMOVE)) { |
| 780 | do_block_remove(number, batch, param); |
| 781 | goto out; |
| 782 | } |
| 783 | |
Paul Mackerras | f03e64f | 2007-02-06 21:10:31 +1100 | [diff] [blame] | 784 | psize = batch->psize; |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 785 | ssize = batch->ssize; |
Paul Mackerras | f03e64f | 2007-02-06 21:10:31 +1100 | [diff] [blame] | 786 | pix = 0; |
| 787 | for (i = 0; i < number; i++) { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 788 | vpn = batch->vpn[i]; |
Paul Mackerras | f03e64f | 2007-02-06 21:10:31 +1100 | [diff] [blame] | 789 | pte = batch->pte[i]; |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 790 | pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) { |
Laurent Dufour | 0effa48 | 2018-08-20 16:29:35 +0200 | [diff] [blame] | 791 | slot = compute_slot(pte, vpn, index, shift, ssize); |
Paul Mackerras | 12e86f9 | 2007-02-08 15:02:35 +1100 | [diff] [blame] | 792 | if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) { |
Aneesh Kumar K.V | db3d853 | 2013-06-20 14:30:13 +0530 | [diff] [blame] | 793 | /* |
| 794 | * lpar doesn't use the passed actual page size |
| 795 | */ |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 796 | pSeries_lpar_hpte_invalidate(slot, vpn, psize, |
Aneesh Kumar K.V | db3d853 | 2013-06-20 14:30:13 +0530 | [diff] [blame] | 797 | 0, ssize, local); |
Paul Mackerras | 12e86f9 | 2007-02-08 15:02:35 +1100 | [diff] [blame] | 798 | } else { |
| 799 | param[pix] = HBR_REQUEST | HBR_AVPN | slot; |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 800 | param[pix+1] = hpte_encode_avpn(vpn, psize, |
Paul Mackerras | 1189be6 | 2007-10-11 20:37:10 +1000 | [diff] [blame] | 801 | ssize); |
Paul Mackerras | 12e86f9 | 2007-02-08 15:02:35 +1100 | [diff] [blame] | 802 | pix += 2; |
| 803 | if (pix == 8) { |
| 804 | rc = plpar_hcall9(H_BULK_REMOVE, param, |
Paul Mackerras | f03e64f | 2007-02-06 21:10:31 +1100 | [diff] [blame] | 805 | param[0], param[1], param[2], |
| 806 | param[3], param[4], param[5], |
| 807 | param[6], param[7]); |
Paul Mackerras | 12e86f9 | 2007-02-08 15:02:35 +1100 | [diff] [blame] | 808 | BUG_ON(rc != H_SUCCESS); |
| 809 | pix = 0; |
| 810 | } |
Paul Mackerras | f03e64f | 2007-02-06 21:10:31 +1100 | [diff] [blame] | 811 | } |
| 812 | } pte_iterate_hashed_end(); |
| 813 | } |
| 814 | if (pix) { |
| 815 | param[pix] = HBR_END; |
| 816 | rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1], |
| 817 | param[2], param[3], param[4], param[5], |
| 818 | param[6], param[7]); |
| 819 | BUG_ON(rc != H_SUCCESS); |
| 820 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | |
Laurent Dufour | ba2dd8a | 2018-08-20 16:29:36 +0200 | [diff] [blame] | 822 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | if (lock_tlbie) |
| 824 | spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags); |
| 825 | } |
| 826 | |
Will Schmidt | 4e89a2d | 2010-09-28 15:33:12 +0000 | [diff] [blame] | 827 | static int __init disable_bulk_remove(char *str) |
| 828 | { |
| 829 | if (strcmp(str, "off") == 0 && |
| 830 | firmware_has_feature(FW_FEATURE_BULK_REMOVE)) { |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 831 | pr_info("Disabling BULK_REMOVE firmware feature"); |
| 832 | powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE; |
Will Schmidt | 4e89a2d | 2010-09-28 15:33:12 +0000 | [diff] [blame] | 833 | } |
| 834 | return 1; |
| 835 | } |
| 836 | |
| 837 | __setup("bulk_remove=", disable_bulk_remove); |
| 838 | |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 839 | #define HPT_RESIZE_TIMEOUT 10000 /* ms */ |
| 840 | |
| 841 | struct hpt_resize_state { |
| 842 | unsigned long shift; |
| 843 | int commit_rc; |
| 844 | }; |
| 845 | |
| 846 | static int pseries_lpar_resize_hpt_commit(void *data) |
| 847 | { |
| 848 | struct hpt_resize_state *state = data; |
| 849 | |
| 850 | state->commit_rc = plpar_resize_hpt_commit(0, state->shift); |
| 851 | if (state->commit_rc != H_SUCCESS) |
| 852 | return -EIO; |
| 853 | |
| 854 | /* Hypervisor has transitioned the HTAB, update our globals */ |
| 855 | ppc64_pft_size = state->shift; |
| 856 | htab_size_bytes = 1UL << ppc64_pft_size; |
| 857 | htab_hash_mask = (htab_size_bytes >> 7) - 1; |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | /* Must be called in user context */ |
| 863 | static int pseries_lpar_resize_hpt(unsigned long shift) |
| 864 | { |
| 865 | struct hpt_resize_state state = { |
| 866 | .shift = shift, |
| 867 | .commit_rc = H_FUNCTION, |
| 868 | }; |
| 869 | unsigned int delay, total_delay = 0; |
| 870 | int rc; |
| 871 | ktime_t t0, t1, t2; |
| 872 | |
| 873 | might_sleep(); |
| 874 | |
| 875 | if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE)) |
| 876 | return -ENODEV; |
| 877 | |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 878 | pr_info("Attempting to resize HPT to shift %lu\n", shift); |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 879 | |
| 880 | t0 = ktime_get(); |
| 881 | |
| 882 | rc = plpar_resize_hpt_prepare(0, shift); |
| 883 | while (H_IS_LONG_BUSY(rc)) { |
| 884 | delay = get_longbusy_msecs(rc); |
| 885 | total_delay += delay; |
| 886 | if (total_delay > HPT_RESIZE_TIMEOUT) { |
| 887 | /* prepare with shift==0 cancels an in-progress resize */ |
| 888 | rc = plpar_resize_hpt_prepare(0, 0); |
| 889 | if (rc != H_SUCCESS) |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 890 | pr_warn("Unexpected error %d cancelling timed out HPT resize\n", |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 891 | rc); |
| 892 | return -ETIMEDOUT; |
| 893 | } |
| 894 | msleep(delay); |
| 895 | rc = plpar_resize_hpt_prepare(0, shift); |
| 896 | }; |
| 897 | |
| 898 | switch (rc) { |
| 899 | case H_SUCCESS: |
| 900 | /* Continue on */ |
| 901 | break; |
| 902 | |
| 903 | case H_PARAMETER: |
Laurent Vivier | f172acb | 2019-03-13 11:25:28 +0100 | [diff] [blame^] | 904 | pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n"); |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 905 | return -EINVAL; |
| 906 | case H_RESOURCE: |
Laurent Vivier | f172acb | 2019-03-13 11:25:28 +0100 | [diff] [blame^] | 907 | pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n"); |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 908 | return -EPERM; |
| 909 | default: |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 910 | pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc); |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 911 | return -EIO; |
| 912 | } |
| 913 | |
| 914 | t1 = ktime_get(); |
| 915 | |
| 916 | rc = stop_machine(pseries_lpar_resize_hpt_commit, &state, NULL); |
| 917 | |
| 918 | t2 = ktime_get(); |
| 919 | |
| 920 | if (rc != 0) { |
| 921 | switch (state.commit_rc) { |
| 922 | case H_PTEG_FULL: |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 923 | return -ENOSPC; |
| 924 | |
| 925 | default: |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 926 | pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n", |
| 927 | state.commit_rc); |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 928 | return -EIO; |
| 929 | }; |
| 930 | } |
| 931 | |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 932 | pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n", |
| 933 | shift, (long long) ktime_ms_delta(t1, t0), |
| 934 | (long long) ktime_ms_delta(t2, t1)); |
David Gibson | dbcf929 | 2016-12-09 11:07:36 +1100 | [diff] [blame] | 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
Paul Mackerras | cc3d294 | 2017-01-30 21:21:36 +1100 | [diff] [blame] | 939 | static int pseries_lpar_register_process_table(unsigned long base, |
| 940 | unsigned long page_size, unsigned long table_size) |
| 941 | { |
| 942 | long rc; |
Paul Mackerras | dbfcf3c | 2017-02-16 16:03:39 +1100 | [diff] [blame] | 943 | unsigned long flags = 0; |
Paul Mackerras | cc3d294 | 2017-01-30 21:21:36 +1100 | [diff] [blame] | 944 | |
Paul Mackerras | dbfcf3c | 2017-02-16 16:03:39 +1100 | [diff] [blame] | 945 | if (table_size) |
| 946 | flags |= PROC_TABLE_NEW; |
Paul Mackerras | cc3d294 | 2017-01-30 21:21:36 +1100 | [diff] [blame] | 947 | if (radix_enabled()) |
| 948 | flags |= PROC_TABLE_RADIX | PROC_TABLE_GTSE; |
Paul Mackerras | dbfcf3c | 2017-02-16 16:03:39 +1100 | [diff] [blame] | 949 | else |
| 950 | flags |= PROC_TABLE_HPT_SLB; |
Paul Mackerras | cc3d294 | 2017-01-30 21:21:36 +1100 | [diff] [blame] | 951 | for (;;) { |
| 952 | rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base, |
| 953 | page_size, table_size); |
| 954 | if (!H_IS_LONG_BUSY(rc)) |
| 955 | break; |
| 956 | mdelay(get_longbusy_msecs(rc)); |
| 957 | } |
| 958 | if (rc != H_SUCCESS) { |
| 959 | pr_err("Failed to register process table (rc=%ld)\n", rc); |
| 960 | BUG(); |
| 961 | } |
| 962 | return rc; |
| 963 | } |
| 964 | |
Michael Ellerman | 6364e84 | 2016-07-26 10:33:03 +1000 | [diff] [blame] | 965 | void __init hpte_init_pseries(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | { |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 967 | mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate; |
| 968 | mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp; |
| 969 | mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp; |
| 970 | mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert; |
| 971 | mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove; |
| 972 | mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted; |
| 973 | mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range; |
Anton Blanchard | 5246ade | 2016-10-01 20:41:56 +1000 | [diff] [blame] | 974 | mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all; |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 975 | mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate; |
Paul Mackerras | dbfcf3c | 2017-02-16 16:03:39 +1100 | [diff] [blame] | 976 | register_process_table = pseries_lpar_register_process_table; |
Michael Ellerman | 8971e1c | 2017-03-17 16:02:35 +1100 | [diff] [blame] | 977 | |
| 978 | if (firmware_has_feature(FW_FEATURE_HPT_RESIZE)) |
| 979 | mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | } |
Robert Jennings | 14f966e | 2009-04-15 05:55:32 +0000 | [diff] [blame] | 981 | |
Paul Mackerras | cc3d294 | 2017-01-30 21:21:36 +1100 | [diff] [blame] | 982 | void radix_init_pseries(void) |
| 983 | { |
| 984 | pr_info("Using radix MMU under hypervisor\n"); |
| 985 | register_process_table = pseries_lpar_register_process_table; |
| 986 | } |
| 987 | |
Robert Jennings | 14f966e | 2009-04-15 05:55:32 +0000 | [diff] [blame] | 988 | #ifdef CONFIG_PPC_SMLPAR |
| 989 | #define CMO_FREE_HINT_DEFAULT 1 |
| 990 | static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT; |
| 991 | |
| 992 | static int __init cmo_free_hint(char *str) |
| 993 | { |
| 994 | char *parm; |
| 995 | parm = strstrip(str); |
| 996 | |
| 997 | if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) { |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 998 | pr_info("%s: CMO free page hinting is not active.\n", __func__); |
Robert Jennings | 14f966e | 2009-04-15 05:55:32 +0000 | [diff] [blame] | 999 | cmo_free_hint_flag = 0; |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
| 1003 | cmo_free_hint_flag = 1; |
Aneesh Kumar K.V | 65471d7 | 2018-06-29 14:09:04 +0530 | [diff] [blame] | 1004 | pr_info("%s: CMO free page hinting is active.\n", __func__); |
Robert Jennings | 14f966e | 2009-04-15 05:55:32 +0000 | [diff] [blame] | 1005 | |
| 1006 | if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0) |
| 1007 | return 1; |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | __setup("cmo_free_hint=", cmo_free_hint); |
| 1013 | |
| 1014 | static void pSeries_set_page_state(struct page *page, int order, |
| 1015 | unsigned long state) |
| 1016 | { |
| 1017 | int i, j; |
| 1018 | unsigned long cmo_page_sz, addr; |
| 1019 | |
| 1020 | cmo_page_sz = cmo_get_page_size(); |
| 1021 | addr = __pa((unsigned long)page_address(page)); |
| 1022 | |
| 1023 | for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) { |
| 1024 | for (j = 0; j < PAGE_SIZE; j += cmo_page_sz) |
| 1025 | plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | void arch_free_page(struct page *page, int order) |
| 1030 | { |
Aneesh Kumar K.V | d8c476e | 2016-04-29 23:26:08 +1000 | [diff] [blame] | 1031 | if (radix_enabled()) |
| 1032 | return; |
Robert Jennings | 14f966e | 2009-04-15 05:55:32 +0000 | [diff] [blame] | 1033 | if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO)) |
| 1034 | return; |
| 1035 | |
| 1036 | pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED); |
| 1037 | } |
| 1038 | EXPORT_SYMBOL(arch_free_page); |
| 1039 | |
Aneesh Kumar K.V | d8c476e | 2016-04-29 23:26:08 +1000 | [diff] [blame] | 1040 | #endif /* CONFIG_PPC_SMLPAR */ |
Michael Ellerman | 4e00374 | 2017-10-19 15:08:43 +1100 | [diff] [blame] | 1041 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1042 | |
| 1043 | #ifdef CONFIG_TRACEPOINTS |
Masahiro Yamada | e9666d1 | 2018-12-31 00:14:15 +0900 | [diff] [blame] | 1044 | #ifdef CONFIG_JUMP_LABEL |
Anton Blanchard | cc1adb5 | 2014-07-03 15:52:03 +1000 | [diff] [blame] | 1045 | struct static_key hcall_tracepoint_key = STATIC_KEY_INIT; |
| 1046 | |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 1047 | int hcall_tracepoint_regfunc(void) |
Anton Blanchard | cc1adb5 | 2014-07-03 15:52:03 +1000 | [diff] [blame] | 1048 | { |
| 1049 | static_key_slow_inc(&hcall_tracepoint_key); |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 1050 | return 0; |
Anton Blanchard | cc1adb5 | 2014-07-03 15:52:03 +1000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | void hcall_tracepoint_unregfunc(void) |
| 1054 | { |
| 1055 | static_key_slow_dec(&hcall_tracepoint_key); |
| 1056 | } |
| 1057 | #else |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1058 | /* |
| 1059 | * We optimise our hcall path by placing hcall_tracepoint_refcount |
| 1060 | * directly in the TOC so we can check if the hcall tracepoints are |
| 1061 | * enabled via a single load. |
| 1062 | */ |
| 1063 | |
| 1064 | /* NB: reg/unreg are called while guarded with the tracepoints_mutex */ |
| 1065 | extern long hcall_tracepoint_refcount; |
| 1066 | |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 1067 | int hcall_tracepoint_regfunc(void) |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1068 | { |
| 1069 | hcall_tracepoint_refcount++; |
Steven Rostedt (Red Hat) | 8cf868a | 2016-11-28 13:03:21 -0500 | [diff] [blame] | 1070 | return 0; |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | void hcall_tracepoint_unregfunc(void) |
| 1074 | { |
| 1075 | hcall_tracepoint_refcount--; |
| 1076 | } |
Anton Blanchard | cc1adb5 | 2014-07-03 15:52:03 +1000 | [diff] [blame] | 1077 | #endif |
| 1078 | |
| 1079 | /* |
| 1080 | * Since the tracing code might execute hcalls we need to guard against |
| 1081 | * recursion. One example of this are spinlocks calling H_YIELD on |
| 1082 | * shared processor partitions. |
| 1083 | */ |
| 1084 | static DEFINE_PER_CPU(unsigned int, hcall_trace_depth); |
| 1085 | |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1086 | |
Anton Blanchard | 6f26353 | 2009-10-26 18:51:09 +0000 | [diff] [blame] | 1087 | void __trace_hcall_entry(unsigned long opcode, unsigned long *args) |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1088 | { |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1089 | unsigned long flags; |
| 1090 | unsigned int *depth; |
| 1091 | |
Anton Blanchard | a5ccfee | 2012-01-09 14:29:15 +0000 | [diff] [blame] | 1092 | /* |
| 1093 | * We cannot call tracepoints inside RCU idle regions which |
| 1094 | * means we must not trace H_CEDE. |
| 1095 | */ |
| 1096 | if (opcode == H_CEDE) |
| 1097 | return; |
| 1098 | |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1099 | local_irq_save(flags); |
| 1100 | |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 1101 | depth = this_cpu_ptr(&hcall_trace_depth); |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1102 | |
| 1103 | if (*depth) |
| 1104 | goto out; |
| 1105 | |
| 1106 | (*depth)++; |
Li Zhong | e4f387d | 2011-12-18 16:03:04 +0000 | [diff] [blame] | 1107 | preempt_disable(); |
Anton Blanchard | 6f26353 | 2009-10-26 18:51:09 +0000 | [diff] [blame] | 1108 | trace_hcall_entry(opcode, args); |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1109 | (*depth)--; |
| 1110 | |
| 1111 | out: |
| 1112 | local_irq_restore(flags); |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Michael Ellerman | 8f2133c | 2018-05-07 23:03:55 +1000 | [diff] [blame] | 1115 | void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf) |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1116 | { |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1117 | unsigned long flags; |
| 1118 | unsigned int *depth; |
| 1119 | |
Anton Blanchard | a5ccfee | 2012-01-09 14:29:15 +0000 | [diff] [blame] | 1120 | if (opcode == H_CEDE) |
| 1121 | return; |
| 1122 | |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1123 | local_irq_save(flags); |
| 1124 | |
Christoph Lameter | 69111ba | 2014-10-21 15:23:25 -0500 | [diff] [blame] | 1125 | depth = this_cpu_ptr(&hcall_trace_depth); |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1126 | |
| 1127 | if (*depth) |
| 1128 | goto out; |
| 1129 | |
| 1130 | (*depth)++; |
Anton Blanchard | 6f26353 | 2009-10-26 18:51:09 +0000 | [diff] [blame] | 1131 | trace_hcall_exit(opcode, retval, retbuf); |
Li Zhong | e4f387d | 2011-12-18 16:03:04 +0000 | [diff] [blame] | 1132 | preempt_enable(); |
Anton Blanchard | 57cdfdf | 2010-10-21 00:52:12 +0000 | [diff] [blame] | 1133 | (*depth)--; |
| 1134 | |
| 1135 | out: |
| 1136 | local_irq_restore(flags); |
Anton Blanchard | c8cd093 | 2009-10-26 18:50:29 +0000 | [diff] [blame] | 1137 | } |
| 1138 | #endif |
Brian King | 9ee820f | 2011-05-04 16:01:20 +1000 | [diff] [blame] | 1139 | |
| 1140 | /** |
| 1141 | * h_get_mpp |
| 1142 | * H_GET_MPP hcall returns info in 7 parms |
| 1143 | */ |
| 1144 | int h_get_mpp(struct hvcall_mpp_data *mpp_data) |
| 1145 | { |
| 1146 | int rc; |
| 1147 | unsigned long retbuf[PLPAR_HCALL9_BUFSIZE]; |
| 1148 | |
| 1149 | rc = plpar_hcall9(H_GET_MPP, retbuf); |
| 1150 | |
| 1151 | mpp_data->entitled_mem = retbuf[0]; |
| 1152 | mpp_data->mapped_mem = retbuf[1]; |
| 1153 | |
| 1154 | mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff; |
| 1155 | mpp_data->pool_num = retbuf[2] & 0xffff; |
| 1156 | |
| 1157 | mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff; |
| 1158 | mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff; |
Anton Blanchard | b0d436c | 2013-08-07 02:01:24 +1000 | [diff] [blame] | 1159 | mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL; |
Brian King | 9ee820f | 2011-05-04 16:01:20 +1000 | [diff] [blame] | 1160 | |
| 1161 | mpp_data->pool_size = retbuf[4]; |
| 1162 | mpp_data->loan_request = retbuf[5]; |
| 1163 | mpp_data->backing_mem = retbuf[6]; |
| 1164 | |
| 1165 | return rc; |
| 1166 | } |
| 1167 | EXPORT_SYMBOL(h_get_mpp); |
| 1168 | |
| 1169 | int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data) |
| 1170 | { |
| 1171 | int rc; |
| 1172 | unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 }; |
| 1173 | |
| 1174 | rc = plpar_hcall9(H_GET_MPP_X, retbuf); |
| 1175 | |
| 1176 | mpp_x_data->coalesced_bytes = retbuf[0]; |
| 1177 | mpp_x_data->pool_coalesced_bytes = retbuf[1]; |
| 1178 | mpp_x_data->pool_purr_cycles = retbuf[2]; |
| 1179 | mpp_x_data->pool_spurr_cycles = retbuf[3]; |
| 1180 | |
| 1181 | return rc; |
| 1182 | } |
Aneesh Kumar K.V | 82228e3 | 2017-03-22 09:07:00 +0530 | [diff] [blame] | 1183 | |
| 1184 | static unsigned long vsid_unscramble(unsigned long vsid, int ssize) |
| 1185 | { |
| 1186 | unsigned long protovsid; |
| 1187 | unsigned long va_bits = VA_BITS; |
| 1188 | unsigned long modinv, vsid_modulus; |
| 1189 | unsigned long max_mod_inv, tmp_modinv; |
| 1190 | |
| 1191 | if (!mmu_has_feature(MMU_FTR_68_BIT_VA)) |
| 1192 | va_bits = 65; |
| 1193 | |
| 1194 | if (ssize == MMU_SEGSIZE_256M) { |
| 1195 | modinv = VSID_MULINV_256M; |
| 1196 | vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1); |
| 1197 | } else { |
| 1198 | modinv = VSID_MULINV_1T; |
| 1199 | vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1); |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * vsid outside our range. |
| 1204 | */ |
| 1205 | if (vsid >= vsid_modulus) |
| 1206 | return 0; |
| 1207 | |
| 1208 | /* |
| 1209 | * If modinv is the modular multiplicate inverse of (x % vsid_modulus) |
| 1210 | * and vsid = (protovsid * x) % vsid_modulus, then we say: |
| 1211 | * protovsid = (vsid * modinv) % vsid_modulus |
| 1212 | */ |
| 1213 | |
| 1214 | /* Check if (vsid * modinv) overflow (63 bits) */ |
| 1215 | max_mod_inv = 0x7fffffffffffffffull / vsid; |
| 1216 | if (modinv < max_mod_inv) |
| 1217 | return (vsid * modinv) % vsid_modulus; |
| 1218 | |
| 1219 | tmp_modinv = modinv/max_mod_inv; |
| 1220 | modinv %= max_mod_inv; |
| 1221 | |
| 1222 | protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus; |
| 1223 | protovsid = (protovsid + vsid * modinv) % vsid_modulus; |
| 1224 | |
| 1225 | return protovsid; |
| 1226 | } |
| 1227 | |
| 1228 | static int __init reserve_vrma_context_id(void) |
| 1229 | { |
| 1230 | unsigned long protovsid; |
| 1231 | |
| 1232 | /* |
| 1233 | * Reserve context ids which map to reserved virtual addresses. For now |
| 1234 | * we only reserve the context id which maps to the VRMA VSID. We ignore |
| 1235 | * the addresses in "ibm,adjunct-virtual-addresses" because we don't |
| 1236 | * enable adjunct support via the "ibm,client-architecture-support" |
| 1237 | * interface. |
| 1238 | */ |
| 1239 | protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T); |
| 1240 | hash__reserve_context_id(protovsid >> ESID_BITS_1T); |
| 1241 | return 0; |
| 1242 | } |
| 1243 | machine_device_initcall(pseries, reserve_vrma_context_id); |
Aravinda Prasad | c6c26fb | 2018-10-16 17:20:05 +0530 | [diff] [blame] | 1244 | |
| 1245 | #ifdef CONFIG_DEBUG_FS |
| 1246 | /* debugfs file interface for vpa data */ |
| 1247 | static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len, |
| 1248 | loff_t *pos) |
| 1249 | { |
| 1250 | int cpu = (long)filp->private_data; |
| 1251 | struct lppaca *lppaca = &lppaca_of(cpu); |
| 1252 | |
| 1253 | return simple_read_from_buffer(buf, len, pos, lppaca, |
| 1254 | sizeof(struct lppaca)); |
| 1255 | } |
| 1256 | |
| 1257 | static const struct file_operations vpa_fops = { |
| 1258 | .open = simple_open, |
| 1259 | .read = vpa_file_read, |
| 1260 | .llseek = default_llseek, |
| 1261 | }; |
| 1262 | |
| 1263 | static int __init vpa_debugfs_init(void) |
| 1264 | { |
| 1265 | char name[16]; |
| 1266 | long i; |
| 1267 | static struct dentry *vpa_dir; |
| 1268 | |
| 1269 | if (!firmware_has_feature(FW_FEATURE_SPLPAR)) |
| 1270 | return 0; |
| 1271 | |
| 1272 | vpa_dir = debugfs_create_dir("vpa", powerpc_debugfs_root); |
| 1273 | if (!vpa_dir) { |
| 1274 | pr_warn("%s: can't create vpa root dir\n", __func__); |
| 1275 | return -ENOMEM; |
| 1276 | } |
| 1277 | |
| 1278 | /* set up the per-cpu vpa file*/ |
| 1279 | for_each_possible_cpu(i) { |
| 1280 | struct dentry *d; |
| 1281 | |
| 1282 | sprintf(name, "cpu-%ld", i); |
| 1283 | |
| 1284 | d = debugfs_create_file(name, 0400, vpa_dir, (void *)i, |
| 1285 | &vpa_fops); |
| 1286 | if (!d) { |
| 1287 | pr_warn("%s: can't create per-cpu vpa file\n", |
| 1288 | __func__); |
| 1289 | return -ENOMEM; |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | machine_arch_initcall(pseries, vpa_debugfs_init); |
| 1296 | #endif /* CONFIG_DEBUG_FS */ |