blob: 8bbbddff7226dfdf7de76b9fc5df03e95f46cbdd [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * pSeries_lpar.c
4 * Copyright (C) 2001 Todd Inglett, IBM Corporation
5 *
6 * pSeries LPAR support.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Michael Ellermanf7ebf352008-04-24 15:13:19 +10009/* Enables debugging of low-level hash table routines - careful! */
10#undef DEBUG
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +053011#define pr_fmt(fmt) "lpar: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/dma-mapping.h>
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110015#include <linux/console.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040016#include <linux/export.h>
Anton Blanchard58995a92015-04-09 13:51:32 +100017#include <linux/jump_label.h>
David Gibsondbcf9292016-12-09 11:07:36 +110018#include <linux/delay.h>
19#include <linux/stop_machine.h>
Naveen N. Raod62c8de2019-07-03 22:34:00 +053020#include <linux/spinlock.h>
21#include <linux/cpuhotplug.h>
22#include <linux/workqueue.h>
23#include <linux/proc_fs.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -070024#include <linux/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/processor.h>
26#include <asm/mmu.h>
27#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/tlb.h>
32#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/cputable.h>
David Gibsondcad47f2005-11-07 09:49:43 +110034#include <asm/udbg.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110035#include <asm/smp.h>
Anton Blanchardc8cd0932009-10-26 18:50:29 +000036#include <asm/trace.h>
Stephen Rothwellf5339272012-03-15 18:18:00 +000037#include <asm/firmware.h>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053038#include <asm/plpar_wrappers.h>
Hari Bathinic1caae32014-12-18 23:36:55 +053039#include <asm/kexec.h>
Hari Bathini408cddd2014-10-01 12:32:30 +053040#include <asm/fadump.h>
Daniel Axtens42f5b4c2016-05-18 11:16:50 +100041#include <asm/asm-prototypes.h>
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +053042#include <asm/debugfs.h>
Peter Zijlstrad6bdceb2020-05-29 22:41:01 +020043#include <asm/dtl.h>
Michael Ellermana1218722005-11-03 15:33:31 +110044
Michael Ellerman21cf9132008-04-16 13:51:48 +100045#include "pseries.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +053047/* Flag bits for H_BULK_REMOVE */
48#define HBR_REQUEST 0x4000000000000000UL
49#define HBR_RESPONSE 0x8000000000000000UL
50#define HBR_END 0xc000000000000000UL
51#define HBR_AVPN 0x0200000000000000UL
52#define HBR_ANDCOND 0x0100000000000000UL
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Anton Blanchardb9377ff2006-07-19 08:01:28 +100055/* in hvCall.S */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056EXPORT_SYMBOL(plpar_hcall);
Anton Blanchardb9377ff2006-07-19 08:01:28 +100057EXPORT_SYMBOL(plpar_hcall9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058EXPORT_SYMBOL(plpar_hcall_norets);
Anton Blanchardb9377ff2006-07-19 08:01:28 +100059
Laurent Dufour1211ee62019-09-20 15:05:22 +020060/*
61 * H_BLOCK_REMOVE supported block size for this page size in segment who's base
62 * page size is that page size.
63 *
64 * The first index is the segment base page size, the second one is the actual
65 * page size.
66 */
67static int hblkrm_size[MMU_PAGE_COUNT][MMU_PAGE_COUNT] __ro_after_init;
68
Laurent Dufour59545eb2019-09-20 15:05:23 +020069/*
70 * Due to the involved complexity, and that the current hypervisor is only
71 * returning this value or 0, we are limiting the support of the H_BLOCK_REMOVE
72 * buffer size to 8 size block.
73 */
74#define HBLKRM_SUPPORTED_BLOCK_SIZE 8
75
Naveen N. Raod62c8de2019-07-03 22:34:00 +053076#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
77static u8 dtl_mask = DTL_LOG_PREEMPT;
78#else
79static u8 dtl_mask;
80#endif
81
Naveen N. Rao18a593c2019-07-03 22:34:01 +053082void alloc_dtl_buffers(unsigned long *time_limit)
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053083{
84 int cpu;
85 struct paca_struct *pp;
86 struct dtl_entry *dtl;
87
88 for_each_possible_cpu(cpu) {
89 pp = paca_ptrs[cpu];
Naveen N. Raod62c8de2019-07-03 22:34:00 +053090 if (pp->dispatch_log)
91 continue;
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053092 dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
93 if (!dtl) {
94 pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
95 cpu);
Naveen N. Raod62c8de2019-07-03 22:34:00 +053096#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053097 pr_warn("Stolen time statistics will be unreliable\n");
Naveen N. Raod62c8de2019-07-03 22:34:00 +053098#endif
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053099 break;
100 }
101
102 pp->dtl_ridx = 0;
103 pp->dispatch_log = dtl;
104 pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
105 pp->dtl_curr = dtl;
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530106
107 if (time_limit && time_after(jiffies, *time_limit)) {
108 cond_resched();
109 *time_limit = jiffies + HZ;
110 }
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +0530111 }
112}
113
114void register_dtl_buffer(int cpu)
115{
116 long ret;
117 struct paca_struct *pp;
118 struct dtl_entry *dtl;
119 int hwcpu = get_hard_smp_processor_id(cpu);
120
121 pp = paca_ptrs[cpu];
122 dtl = pp->dispatch_log;
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530123 if (dtl && dtl_mask) {
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +0530124 pp->dtl_ridx = 0;
125 pp->dtl_curr = dtl;
126 lppaca_of(cpu).dtl_idx = 0;
127
128 /* hypervisor reads buffer length from this field */
129 dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
130 ret = register_dtl(hwcpu, __pa(dtl));
131 if (ret)
132 pr_err("WARNING: DTL registration of cpu %d (hw %d) failed with %ld\n",
133 cpu, hwcpu, ret);
134
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530135 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +0530136 }
137}
138
Naveen N. Rao06220d72019-07-03 22:33:57 +0530139#ifdef CONFIG_PPC_SPLPAR
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530140struct dtl_worker {
141 struct delayed_work work;
142 int cpu;
143};
144
145struct vcpu_dispatch_data {
146 int last_disp_cpu;
147
148 int total_disp;
149
150 int same_cpu_disp;
151 int same_chip_disp;
152 int diff_chip_disp;
153 int far_chip_disp;
154
155 int numa_home_disp;
156 int numa_remote_disp;
157 int numa_far_disp;
158};
159
160/*
161 * This represents the number of cpus in the hypervisor. Since there is no
162 * architected way to discover the number of processors in the host, we
163 * provision for dealing with NR_CPUS. This is currently 2048 by default, and
164 * is sufficient for our purposes. This will need to be tweaked if
165 * CONFIG_NR_CPUS is changed.
166 */
167#define NR_CPUS_H NR_CPUS
168
Naveen N. Rao06220d72019-07-03 22:33:57 +0530169DEFINE_RWLOCK(dtl_access_lock);
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530170static DEFINE_PER_CPU(struct vcpu_dispatch_data, vcpu_disp_data);
171static DEFINE_PER_CPU(u64, dtl_entry_ridx);
172static DEFINE_PER_CPU(struct dtl_worker, dtl_workers);
173static enum cpuhp_state dtl_worker_state;
174static DEFINE_MUTEX(dtl_enable_mutex);
175static int vcpudispatch_stats_on __read_mostly;
176static int vcpudispatch_stats_freq = 50;
177static __be32 *vcpu_associativity, *pcpu_associativity;
178
179
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530180static void free_dtl_buffers(unsigned long *time_limit)
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530181{
182#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
183 int cpu;
184 struct paca_struct *pp;
185
186 for_each_possible_cpu(cpu) {
187 pp = paca_ptrs[cpu];
188 if (!pp->dispatch_log)
189 continue;
190 kmem_cache_free(dtl_cache, pp->dispatch_log);
191 pp->dtl_ridx = 0;
192 pp->dispatch_log = 0;
193 pp->dispatch_log_end = 0;
194 pp->dtl_curr = 0;
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530195
196 if (time_limit && time_after(jiffies, *time_limit)) {
197 cond_resched();
198 *time_limit = jiffies + HZ;
199 }
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530200 }
201#endif
202}
203
204static int init_cpu_associativity(void)
205{
206 vcpu_associativity = kcalloc(num_possible_cpus() / threads_per_core,
207 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
208 pcpu_associativity = kcalloc(NR_CPUS_H / threads_per_core,
209 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
210
211 if (!vcpu_associativity || !pcpu_associativity) {
212 pr_err("error allocating memory for associativity information\n");
213 return -ENOMEM;
214 }
215
216 return 0;
217}
218
219static void destroy_cpu_associativity(void)
220{
221 kfree(vcpu_associativity);
222 kfree(pcpu_associativity);
223 vcpu_associativity = pcpu_associativity = 0;
224}
225
226static __be32 *__get_cpu_associativity(int cpu, __be32 *cpu_assoc, int flag)
227{
228 __be32 *assoc;
229 int rc = 0;
230
231 assoc = &cpu_assoc[(int)(cpu / threads_per_core) * VPHN_ASSOC_BUFSIZE];
232 if (!assoc[0]) {
233 rc = hcall_vphn(cpu, flag, &assoc[0]);
234 if (rc)
235 return NULL;
236 }
237
238 return assoc;
239}
240
241static __be32 *get_pcpu_associativity(int cpu)
242{
243 return __get_cpu_associativity(cpu, pcpu_associativity, VPHN_FLAG_PCPU);
244}
245
246static __be32 *get_vcpu_associativity(int cpu)
247{
248 return __get_cpu_associativity(cpu, vcpu_associativity, VPHN_FLAG_VCPU);
249}
250
251static int cpu_relative_dispatch_distance(int last_disp_cpu, int cur_disp_cpu)
252{
253 __be32 *last_disp_cpu_assoc, *cur_disp_cpu_assoc;
254
255 if (last_disp_cpu >= NR_CPUS_H || cur_disp_cpu >= NR_CPUS_H)
256 return -EINVAL;
257
258 last_disp_cpu_assoc = get_pcpu_associativity(last_disp_cpu);
259 cur_disp_cpu_assoc = get_pcpu_associativity(cur_disp_cpu);
260
261 if (!last_disp_cpu_assoc || !cur_disp_cpu_assoc)
262 return -EIO;
263
264 return cpu_distance(last_disp_cpu_assoc, cur_disp_cpu_assoc);
265}
266
267static int cpu_home_node_dispatch_distance(int disp_cpu)
268{
269 __be32 *disp_cpu_assoc, *vcpu_assoc;
270 int vcpu_id = smp_processor_id();
271
272 if (disp_cpu >= NR_CPUS_H) {
273 pr_debug_ratelimited("vcpu dispatch cpu %d > %d\n",
274 disp_cpu, NR_CPUS_H);
275 return -EINVAL;
276 }
277
278 disp_cpu_assoc = get_pcpu_associativity(disp_cpu);
279 vcpu_assoc = get_vcpu_associativity(vcpu_id);
280
281 if (!disp_cpu_assoc || !vcpu_assoc)
282 return -EIO;
283
284 return cpu_distance(disp_cpu_assoc, vcpu_assoc);
285}
286
287static void update_vcpu_disp_stat(int disp_cpu)
288{
289 struct vcpu_dispatch_data *disp;
290 int distance;
291
292 disp = this_cpu_ptr(&vcpu_disp_data);
293 if (disp->last_disp_cpu == -1) {
294 disp->last_disp_cpu = disp_cpu;
295 return;
296 }
297
298 disp->total_disp++;
299
300 if (disp->last_disp_cpu == disp_cpu ||
301 (cpu_first_thread_sibling(disp->last_disp_cpu) ==
302 cpu_first_thread_sibling(disp_cpu)))
303 disp->same_cpu_disp++;
304 else {
305 distance = cpu_relative_dispatch_distance(disp->last_disp_cpu,
306 disp_cpu);
307 if (distance < 0)
308 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
309 smp_processor_id());
310 else {
311 switch (distance) {
312 case 0:
313 disp->same_chip_disp++;
314 break;
315 case 1:
316 disp->diff_chip_disp++;
317 break;
318 case 2:
319 disp->far_chip_disp++;
320 break;
321 default:
322 pr_debug_ratelimited("vcpudispatch_stats: cpu %d (%d -> %d): unexpected relative dispatch distance %d\n",
323 smp_processor_id(),
324 disp->last_disp_cpu,
325 disp_cpu,
326 distance);
327 }
328 }
329 }
330
331 distance = cpu_home_node_dispatch_distance(disp_cpu);
332 if (distance < 0)
333 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
334 smp_processor_id());
335 else {
336 switch (distance) {
337 case 0:
338 disp->numa_home_disp++;
339 break;
340 case 1:
341 disp->numa_remote_disp++;
342 break;
343 case 2:
344 disp->numa_far_disp++;
345 break;
346 default:
347 pr_debug_ratelimited("vcpudispatch_stats: cpu %d on %d: unexpected numa dispatch distance %d\n",
348 smp_processor_id(),
349 disp_cpu,
350 distance);
351 }
352 }
353
354 disp->last_disp_cpu = disp_cpu;
355}
356
357static void process_dtl_buffer(struct work_struct *work)
358{
359 struct dtl_entry dtle;
360 u64 i = __this_cpu_read(dtl_entry_ridx);
361 struct dtl_entry *dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
362 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
363 struct lppaca *vpa = local_paca->lppaca_ptr;
364 struct dtl_worker *d = container_of(work, struct dtl_worker, work.work);
365
366 if (!local_paca->dispatch_log)
367 return;
368
369 /* if we have been migrated away, we cancel ourself */
370 if (d->cpu != smp_processor_id()) {
371 pr_debug("vcpudispatch_stats: cpu %d worker migrated -- canceling worker\n",
372 smp_processor_id());
373 return;
374 }
375
376 if (i == be64_to_cpu(vpa->dtl_idx))
377 goto out;
378
379 while (i < be64_to_cpu(vpa->dtl_idx)) {
380 dtle = *dtl;
381 barrier();
382 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
383 /* buffer has overflowed */
384 pr_debug_ratelimited("vcpudispatch_stats: cpu %d lost %lld DTL samples\n",
385 d->cpu,
386 be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG - i);
387 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
388 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
389 continue;
390 }
391 update_vcpu_disp_stat(be16_to_cpu(dtle.processor_id));
392 ++i;
393 ++dtl;
394 if (dtl == dtl_end)
395 dtl = local_paca->dispatch_log;
396 }
397
398 __this_cpu_write(dtl_entry_ridx, i);
399
400out:
401 schedule_delayed_work_on(d->cpu, to_delayed_work(work),
402 HZ / vcpudispatch_stats_freq);
403}
404
405static int dtl_worker_online(unsigned int cpu)
406{
407 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
408
409 memset(d, 0, sizeof(*d));
410 INIT_DELAYED_WORK(&d->work, process_dtl_buffer);
411 d->cpu = cpu;
412
413#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
414 per_cpu(dtl_entry_ridx, cpu) = 0;
415 register_dtl_buffer(cpu);
416#else
417 per_cpu(dtl_entry_ridx, cpu) = be64_to_cpu(lppaca_of(cpu).dtl_idx);
418#endif
419
420 schedule_delayed_work_on(cpu, &d->work, HZ / vcpudispatch_stats_freq);
421 return 0;
422}
423
424static int dtl_worker_offline(unsigned int cpu)
425{
426 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
427
428 cancel_delayed_work_sync(&d->work);
429
430#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
431 unregister_dtl(get_hard_smp_processor_id(cpu));
432#endif
433
434 return 0;
435}
436
437static void set_global_dtl_mask(u8 mask)
438{
439 int cpu;
440
441 dtl_mask = mask;
442 for_each_present_cpu(cpu)
443 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
444}
445
446static void reset_global_dtl_mask(void)
447{
448 int cpu;
449
450#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
451 dtl_mask = DTL_LOG_PREEMPT;
452#else
453 dtl_mask = 0;
454#endif
455 for_each_present_cpu(cpu)
456 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
457}
458
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530459static int dtl_worker_enable(unsigned long *time_limit)
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530460{
461 int rc = 0, state;
462
463 if (!write_trylock(&dtl_access_lock)) {
464 rc = -EBUSY;
465 goto out;
466 }
467
468 set_global_dtl_mask(DTL_LOG_ALL);
469
470 /* Setup dtl buffers and register those */
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530471 alloc_dtl_buffers(time_limit);
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530472
473 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/dtl:online",
474 dtl_worker_online, dtl_worker_offline);
475 if (state < 0) {
476 pr_err("vcpudispatch_stats: unable to setup workqueue for DTL processing\n");
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530477 free_dtl_buffers(time_limit);
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530478 reset_global_dtl_mask();
479 write_unlock(&dtl_access_lock);
480 rc = -EINVAL;
481 goto out;
482 }
483 dtl_worker_state = state;
484
485out:
486 return rc;
487}
488
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530489static void dtl_worker_disable(unsigned long *time_limit)
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530490{
491 cpuhp_remove_state(dtl_worker_state);
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530492 free_dtl_buffers(time_limit);
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530493 reset_global_dtl_mask();
494 write_unlock(&dtl_access_lock);
495}
496
497static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
498 size_t count, loff_t *ppos)
499{
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530500 unsigned long time_limit = jiffies + HZ;
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530501 struct vcpu_dispatch_data *disp;
502 int rc, cmd, cpu;
503 char buf[16];
504
505 if (count > 15)
506 return -EINVAL;
507
508 if (copy_from_user(buf, p, count))
509 return -EFAULT;
510
511 buf[count] = 0;
512 rc = kstrtoint(buf, 0, &cmd);
513 if (rc || cmd < 0 || cmd > 1) {
514 pr_err("vcpudispatch_stats: please use 0 to disable or 1 to enable dispatch statistics\n");
515 return rc ? rc : -EINVAL;
516 }
517
518 mutex_lock(&dtl_enable_mutex);
519
520 if ((cmd == 0 && !vcpudispatch_stats_on) ||
521 (cmd == 1 && vcpudispatch_stats_on))
522 goto out;
523
524 if (cmd) {
525 rc = init_cpu_associativity();
526 if (rc)
527 goto out;
528
529 for_each_possible_cpu(cpu) {
530 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
531 memset(disp, 0, sizeof(*disp));
532 disp->last_disp_cpu = -1;
533 }
534
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530535 rc = dtl_worker_enable(&time_limit);
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530536 if (rc) {
537 destroy_cpu_associativity();
538 goto out;
539 }
540 } else {
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530541 dtl_worker_disable(&time_limit);
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530542 destroy_cpu_associativity();
543 }
544
545 vcpudispatch_stats_on = cmd;
546
547out:
548 mutex_unlock(&dtl_enable_mutex);
549 if (rc)
550 return rc;
551 return count;
552}
553
554static int vcpudispatch_stats_display(struct seq_file *p, void *v)
555{
556 int cpu;
557 struct vcpu_dispatch_data *disp;
558
559 if (!vcpudispatch_stats_on) {
560 seq_puts(p, "off\n");
561 return 0;
562 }
563
564 for_each_online_cpu(cpu) {
565 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
566 seq_printf(p, "cpu%d", cpu);
567 seq_put_decimal_ull(p, " ", disp->total_disp);
568 seq_put_decimal_ull(p, " ", disp->same_cpu_disp);
569 seq_put_decimal_ull(p, " ", disp->same_chip_disp);
570 seq_put_decimal_ull(p, " ", disp->diff_chip_disp);
571 seq_put_decimal_ull(p, " ", disp->far_chip_disp);
572 seq_put_decimal_ull(p, " ", disp->numa_home_disp);
573 seq_put_decimal_ull(p, " ", disp->numa_remote_disp);
574 seq_put_decimal_ull(p, " ", disp->numa_far_disp);
575 seq_puts(p, "\n");
576 }
577
578 return 0;
579}
580
581static int vcpudispatch_stats_open(struct inode *inode, struct file *file)
582{
583 return single_open(file, vcpudispatch_stats_display, NULL);
584}
585
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800586static const struct proc_ops vcpudispatch_stats_proc_ops = {
587 .proc_open = vcpudispatch_stats_open,
588 .proc_read = seq_read,
589 .proc_write = vcpudispatch_stats_write,
590 .proc_lseek = seq_lseek,
591 .proc_release = single_release,
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530592};
593
594static ssize_t vcpudispatch_stats_freq_write(struct file *file,
595 const char __user *p, size_t count, loff_t *ppos)
596{
597 int rc, freq;
598 char buf[16];
599
600 if (count > 15)
601 return -EINVAL;
602
603 if (copy_from_user(buf, p, count))
604 return -EFAULT;
605
606 buf[count] = 0;
607 rc = kstrtoint(buf, 0, &freq);
608 if (rc || freq < 1 || freq > HZ) {
609 pr_err("vcpudispatch_stats_freq: please specify a frequency between 1 and %d\n",
610 HZ);
611 return rc ? rc : -EINVAL;
612 }
613
614 vcpudispatch_stats_freq = freq;
615
616 return count;
617}
618
619static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
620{
621 seq_printf(p, "%d\n", vcpudispatch_stats_freq);
622 return 0;
623}
624
625static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)
626{
627 return single_open(file, vcpudispatch_stats_freq_display, NULL);
628}
629
Alexey Dobriyan97a32532020-02-03 17:37:17 -0800630static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {
631 .proc_open = vcpudispatch_stats_freq_open,
632 .proc_read = seq_read,
633 .proc_write = vcpudispatch_stats_freq_write,
634 .proc_lseek = seq_lseek,
635 .proc_release = single_release,
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530636};
637
638static int __init vcpudispatch_stats_procfs_init(void)
639{
Nicholas Pigginadde8712020-03-21 01:24:36 +1000640 /*
641 * Avoid smp_processor_id while preemptible. All CPUs should have
642 * the same value for lppaca_shared_proc.
643 */
644 preempt_disable();
645 if (!lppaca_shared_proc(get_lppaca())) {
646 preempt_enable();
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530647 return 0;
Nicholas Pigginadde8712020-03-21 01:24:36 +1000648 }
649 preempt_enable();
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530650
651 if (!proc_create("powerpc/vcpudispatch_stats", 0600, NULL,
652 &vcpudispatch_stats_proc_ops))
653 pr_err("vcpudispatch_stats: error creating procfs file\n");
654 else if (!proc_create("powerpc/vcpudispatch_stats_freq", 0600, NULL,
655 &vcpudispatch_stats_freq_proc_ops))
656 pr_err("vcpudispatch_stats_freq: error creating procfs file\n");
657
658 return 0;
659}
660
661machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);
Naveen N. Rao06220d72019-07-03 22:33:57 +0530662#endif /* CONFIG_PPC_SPLPAR */
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664void vpa_init(int cpu)
665{
666 int hwcpu = get_hard_smp_processor_id(cpu);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000667 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 long ret;
Olof Johansson233ccd02005-09-03 15:55:59 -0700669
Michael Ellermanb89bdfb2013-08-15 15:22:15 +1000670 /*
671 * The spec says it "may be problematic" if CPU x registers the VPA of
672 * CPU y. We should never do that, but wail if we ever do.
673 */
674 WARN_ON(cpu != smp_processor_id());
675
Olof Johansson233ccd02005-09-03 15:55:59 -0700676 if (cpu_has_feature(CPU_FTR_ALTIVEC))
Paul Mackerras8154c5d2010-08-12 20:18:15 +0000677 lppaca_of(cpu).vmxregs_in_use = 1;
Olof Johansson233ccd02005-09-03 15:55:59 -0700678
Michael Ellerman6e0b8bc92013-06-28 18:15:18 +1000679 if (cpu_has_feature(CPU_FTR_ARCH_207S))
680 lppaca_of(cpu).ebb_regs_in_use = 1;
681
Paul Mackerras8154c5d2010-08-12 20:18:15 +0000682 addr = __pa(&lppaca_of(cpu));
Michael Neuling2f6093c2006-08-07 16:19:19 +1000683 ret = register_vpa(hwcpu, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Michael Neuling2f6093c2006-08-07 16:19:19 +1000685 if (ret) {
Anton Blanchard711ef842011-07-25 01:46:33 +0000686 pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "
687 "%lx failed with %ld\n", cpu, hwcpu, addr, ret);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000688 return;
689 }
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +1000690
Michael Ellerman4e003742017-10-19 15:08:43 +1100691#ifdef CONFIG_PPC_BOOK3S_64
Michael Neuling2f6093c2006-08-07 16:19:19 +1000692 /*
693 * PAPR says this feature is SLB-Buffer but firmware never
694 * reports that. All SPLPAR support SLB shadow buffer.
695 */
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +1000696 if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {
Nicholas Piggind2e60072018-02-14 01:08:12 +1000697 addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000698 ret = register_slb_shadow(hwcpu, addr);
699 if (ret)
Anton Blanchard711ef842011-07-25 01:46:33 +0000700 pr_err("WARNING: SLB shadow buffer registration for "
701 "cpu %d (hw %d) of area %lx failed with %ld\n",
702 cpu, hwcpu, addr, ret);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000703 }
Michael Ellerman4e003742017-10-19 15:08:43 +1100704#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000705
706 /*
707 * Register dispatch trace log, if one has been allocated.
708 */
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +0530709 register_dtl_buffer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Michael Ellerman4e003742017-10-19 15:08:43 +1100712#ifdef CONFIG_PPC_BOOK3S_64
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +1000713
Geoff Levand035223f2006-10-05 11:35:10 -0700714static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000715 unsigned long vpn, unsigned long pa,
716 unsigned long rflags, unsigned long vflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000717 int psize, int apsize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 unsigned long lpar_rc;
720 unsigned long flags;
721 unsigned long slot;
David Gibson96e28442005-07-13 01:11:42 -0700722 unsigned long hpte_v, hpte_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100724 if (!(vflags & HPTE_V_BOLTED))
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000725 pr_devel("hpte_insert(group=%lx, vpn=%016lx, "
726 "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",
727 hpte_group, vpn, pa, rflags, vflags, psize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000729 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100730 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100731
732 if (!(vflags & HPTE_V_BOLTED))
Michael Ellerman551a2322009-06-17 18:13:50 +0000733 pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /* Now fill in the actual HPTE */
736 /* Set CEC cookie to 0 */
737 /* Zero page = 0 */
738 /* I-cache Invalidate = 0 */
739 /* I-cache synchronize = 0 */
740 /* Exact = 0 */
741 flags = 0;
742
Brian King9ee820f2011-05-04 16:01:20 +1000743 if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))
744 flags |= H_COALESCE_CAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000746 lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200747 if (unlikely(lpar_rc == H_PTEG_FULL)) {
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530748 pr_devel("Hash table group is full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return -1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /*
753 * Since we try and ioremap PHBs we don't own, the pte insert
754 * will fail. However we must catch the failure in hash_page
755 * or we will loop forever, so return -2 in this case.
756 */
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200757 if (unlikely(lpar_rc != H_SUCCESS)) {
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530758 pr_err("Failed hash pte insert with error %ld\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return -2;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100760 }
761 if (!(vflags & HPTE_V_BOLTED))
Michael Ellerman551a2322009-06-17 18:13:50 +0000762 pr_devel(" -> slot: %lu\n", slot & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* Because of iSeries, we have to pass down the secondary
765 * bucket bit here as well
766 */
David Gibson96e28442005-07-13 01:11:42 -0700767 return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
771
772static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
773{
774 unsigned long slot_offset;
775 unsigned long lpar_rc;
776 int i;
777 unsigned long dummy1, dummy2;
778
779 /* pick a random slot to start at */
780 slot_offset = mftb() & 0x7;
781
782 for (i = 0; i < HPTES_PER_GROUP; i++) {
783
784 /* don't remove a bolted entry */
785 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
Aneesh Kumar K.V82ce0282019-10-24 15:05:40 +0530786 HPTE_V_BOLTED, &dummy1, &dummy2);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200787 if (lpar_rc == H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return i;
Michael Wolf9fb26402013-04-05 10:41:40 +0000789
790 /*
791 * The test for adjunct partition is performed before the
792 * ANDCOND test. H_RESOURCE may be returned, so we need to
793 * check for that as well.
794 */
795 BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 slot_offset++;
798 slot_offset &= 0x7;
799 }
800
801 return -1;
802}
803
Anton Blanchard5246ade2016-10-01 20:41:56 +1000804static void manual_hpte_clear_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 unsigned long size_bytes = 1UL << ppc64_pft_size;
807 unsigned long hpte_count = size_bytes >> 4;
Michael Neulingd504bed2010-05-10 20:28:26 +0000808 struct {
809 unsigned long pteh;
810 unsigned long ptel;
811 } ptes[4];
Sachin P. Santb7abc5c2007-06-14 15:31:34 +1000812 long lpar_rc;
Anton Blanchardbed9a312011-07-26 18:15:03 +0000813 unsigned long i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Michael Neulingd504bed2010-05-10 20:28:26 +0000815 /* Read in batches of 4,
816 * invalidate only valid entries not in the VRMA
817 * hpte_count will be a multiple of 4
818 */
819 for (i = 0; i < hpte_count; i += 4) {
820 lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530821 if (lpar_rc != H_SUCCESS) {
822 pr_info("Failed to read hash page table at %ld err %ld\n",
823 i, lpar_rc);
Michael Neulingd504bed2010-05-10 20:28:26 +0000824 continue;
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530825 }
Michael Neulingd504bed2010-05-10 20:28:26 +0000826 for (j = 0; j < 4; j++){
827 if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==
828 HPTE_V_VRMA_MASK)
829 continue;
830 if (ptes[j].pteh & HPTE_V_VALID)
831 plpar_pte_remove_raw(0, i + j, 0,
832 &(ptes[j].pteh), &(ptes[j].ptel));
Sachin P. Santb7abc5c2007-06-14 15:31:34 +1000833 }
834 }
Anton Blanchard5246ade2016-10-01 20:41:56 +1000835}
836
837static int hcall_hpte_clear_all(void)
838{
839 int rc;
840
841 do {
842 rc = plpar_hcall_norets(H_CLEAR_HPT);
843 } while (rc == H_CONTINUE);
844
845 return rc;
846}
847
848static void pseries_hpte_clear_all(void)
849{
850 int rc;
851
852 rc = hcall_hpte_clear_all();
853 if (rc != H_SUCCESS)
854 manual_hpte_clear_all();
Anton Blancharde844b1e2013-11-20 22:14:59 +1100855
856#ifdef __LITTLE_ENDIAN__
Hari Bathini408cddd2014-10-01 12:32:30 +0530857 /*
858 * Reset exceptions to big endian.
859 *
860 * FIXME this is a hack for kexec, we need to reset the exception
861 * endian before starting the new kernel and this is a convenient place
862 * to do it.
863 *
864 * This is also called on boot when a fadump happens. In that case we
865 * must not change the exception endian mode.
866 */
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000867 if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())
868 pseries_big_endian_exceptions();
Anton Blancharde844b1e2013-11-20 22:14:59 +1100869#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872/*
873 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
874 * the low 3 bits of flags happen to line up. So no transform is needed.
875 * We can probably optimize here and assume the high bits of newpp are
876 * already zero. For now I am paranoid.
877 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100878static long pSeries_lpar_hpte_updatepp(unsigned long slot,
879 unsigned long newpp,
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000880 unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530881 int psize, int apsize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530882 int ssize, unsigned long inv_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 unsigned long lpar_rc;
Balbir Singhe71ff982017-06-29 03:04:07 +1000885 unsigned long flags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100886 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000888 want_v = hpte_encode_avpn(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Balbir Singhe71ff982017-06-29 03:04:07 +1000890 flags = (newpp & 7) | H_AVPN;
891 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
892 /* Move pp0 into bit 8 (IBM 55) */
893 flags |= (newpp & HPTE_R_PP0) >> 55;
894
Alexey Kardashevskiya8c0bf32018-01-09 16:52:14 +1100895 pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
896 want_v, slot, flags, psize);
897
Paul Mackerras1189be62007-10-11 20:37:10 +1000898 lpar_rc = plpar_pte_protect(flags, slot, want_v);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100899
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200900 if (lpar_rc == H_NOT_FOUND) {
Michael Ellerman551a2322009-06-17 18:13:50 +0000901 pr_devel("not found !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100903 }
904
Michael Ellerman551a2322009-06-17 18:13:50 +0000905 pr_devel("ok\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200907 BUG_ON(lpar_rc != H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 return 0;
910}
911
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530912static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530914 long lpar_rc;
915 unsigned long i, j;
916 struct {
917 unsigned long pteh;
918 unsigned long ptel;
919 } ptes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530921 for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530923 lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530924 if (lpar_rc != H_SUCCESS) {
925 pr_info("Failed to read hash page table at %ld err %ld\n",
926 hpte_group, lpar_rc);
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530927 continue;
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530930 for (j = 0; j < 4; j++) {
931 if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
932 (ptes[j].pteh & HPTE_V_VALID))
933 return i + j;
934 }
935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530937 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000940static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 long slot;
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530943 unsigned long hash;
944 unsigned long want_v;
945 unsigned long hpte_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000947 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
948 want_v = hpte_encode_avpn(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530950 /*
951 * We try to keep bolted entries always in primary hash
952 * But in some case we can find them in secondary too.
953 */
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530954 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
955 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
Aneesh Kumar K.Vd78d5da2019-10-24 15:05:42 +0530956 if (slot < 0) {
957 /* Try in secondary */
958 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
959 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
960 if (slot < 0)
961 return -1;
962 }
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530963 return hpte_group + slot;
964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100967 unsigned long ea,
Paul Mackerras1189be62007-10-11 20:37:10 +1000968 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000970 unsigned long vpn;
971 unsigned long lpar_rc, slot, vsid, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Paul Mackerras1189be62007-10-11 20:37:10 +1000973 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000974 vpn = hpt_vpn(ea, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000976 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 BUG_ON(slot == -1);
978
Michael Ellermanb56d55a2021-03-31 11:38:41 +1100979 flags = newpp & (HPTE_R_PP | HPTE_R_N);
Balbir Singhe71ff982017-06-29 03:04:07 +1000980 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
981 /* Move pp0 into bit 8 (IBM 55) */
982 flags |= (newpp & HPTE_R_PP0) >> 55;
983
Michael Ellermanb56d55a2021-03-31 11:38:41 +1100984 flags |= ((newpp & HPTE_R_KEY_HI) >> 48) | (newpp & HPTE_R_KEY_LO);
985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 lpar_rc = plpar_pte_protect(flags, slot, 0);
987
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200988 BUG_ON(lpar_rc != H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000991static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530992 int psize, int apsize,
993 int ssize, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100995 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned long lpar_rc;
997 unsigned long dummy1, dummy2;
998
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000999 pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
1000 slot, vpn, psize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001002 want_v = hpte_encode_avpn(vpn, psize, ssize);
Paul Mackerras1189be62007-10-11 20:37:10 +10001003 lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001004 if (lpar_rc == H_NOT_FOUND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return;
1006
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001007 BUG_ON(lpar_rc != H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001010
1011/*
1012 * As defined in the PAPR's section 14.5.4.1.8
1013 * The control mask doesn't include the returned reference and change bit from
1014 * the processed PTE.
1015 */
1016#define HBLKR_AVPN 0x0100000000000000UL
1017#define HBLKR_CTRL_MASK 0xf800000000000000UL
1018#define HBLKR_CTRL_SUCCESS 0x8000000000000000UL
1019#define HBLKR_CTRL_ERRNOTFOUND 0x8800000000000000UL
1020#define HBLKR_CTRL_ERRBUSY 0xa000000000000000UL
1021
Laurent Dufour59545eb2019-09-20 15:05:23 +02001022/*
1023 * Returned true if we are supporting this block size for the specified segment
1024 * base page size and actual page size.
1025 *
1026 * Currently, we only support 8 size block.
1027 */
1028static inline bool is_supported_hlbkrm(int bpsize, int psize)
1029{
1030 return (hblkrm_size[bpsize][psize] == HBLKRM_SUPPORTED_BLOCK_SIZE);
1031}
1032
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001033/**
1034 * H_BLOCK_REMOVE caller.
1035 * @idx should point to the latest @param entry set with a PTEX.
1036 * If PTE cannot be processed because another CPUs has already locked that
1037 * group, those entries are put back in @param starting at index 1.
1038 * If entries has to be retried and @retry_busy is set to true, these entries
1039 * are retried until success. If @retry_busy is set to false, the returned
1040 * is the number of entries yet to process.
1041 */
1042static unsigned long call_block_remove(unsigned long idx, unsigned long *param,
1043 bool retry_busy)
1044{
1045 unsigned long i, rc, new_idx;
1046 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1047
1048 if (idx < 2) {
1049 pr_warn("Unexpected empty call to H_BLOCK_REMOVE");
1050 return 0;
1051 }
1052again:
1053 new_idx = 0;
1054 if (idx > PLPAR_HCALL9_BUFSIZE) {
1055 pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx);
1056 idx = PLPAR_HCALL9_BUFSIZE;
1057 } else if (idx < PLPAR_HCALL9_BUFSIZE)
1058 param[idx] = HBR_END;
1059
1060 rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf,
1061 param[0], /* AVA */
1062 param[1], param[2], param[3], param[4], /* TS0-7 */
1063 param[5], param[6], param[7], param[8]);
1064 if (rc == H_SUCCESS)
1065 return 0;
1066
1067 BUG_ON(rc != H_PARTIAL);
1068
1069 /* Check that the unprocessed entries were 'not found' or 'busy' */
1070 for (i = 0; i < idx-1; i++) {
1071 unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK;
1072
1073 if (ctrl == HBLKR_CTRL_ERRBUSY) {
1074 param[++new_idx] = param[i+1];
1075 continue;
1076 }
1077
1078 BUG_ON(ctrl != HBLKR_CTRL_SUCCESS
1079 && ctrl != HBLKR_CTRL_ERRNOTFOUND);
1080 }
1081
1082 /*
1083 * If there were entries found busy, retry these entries if requested,
1084 * of if all the entries have to be retried.
1085 */
1086 if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) {
1087 idx = new_idx + 1;
1088 goto again;
1089 }
1090
1091 return new_idx;
1092}
1093
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +05301094#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301095/*
1096 * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
1097 * to make sure that we avoid bouncing the hypervisor tlbie lock.
1098 */
1099#define PPC64_HUGE_HPTE_BATCH 12
1100
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001101static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn,
1102 int count, int psize, int ssize)
1103{
1104 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1105 unsigned long shift, current_vpgb, vpgb;
1106 int i, pix = 0;
1107
1108 shift = mmu_psize_defs[psize].shift;
1109
1110 for (i = 0; i < count; i++) {
1111 /*
1112 * Shifting 3 bits more on the right to get a
1113 * 8 pages aligned virtual addresse.
1114 */
1115 vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3));
1116 if (!pix || vpgb != current_vpgb) {
1117 /*
1118 * Need to start a new 8 pages block, flush
1119 * the current one if needed.
1120 */
1121 if (pix)
1122 (void)call_block_remove(pix, param, true);
1123 current_vpgb = vpgb;
1124 param[0] = hpte_encode_avpn(vpn[i], psize, ssize);
1125 pix = 1;
1126 }
1127
1128 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i];
1129 if (pix == PLPAR_HCALL9_BUFSIZE) {
1130 pix = call_block_remove(pix, param, false);
1131 /*
1132 * pix = 0 means that all the entries were
1133 * removed, we can start a new block.
1134 * Otherwise, this means that there are entries
1135 * to retry, and pix points to latest one, so
1136 * we should increment it and try to continue
1137 * the same block.
1138 */
1139 if (pix)
1140 pix++;
1141 }
1142 }
1143 if (pix)
1144 (void)call_block_remove(pix, param, true);
1145}
1146
1147static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
1148 int count, int psize, int ssize)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301149{
Laurent Dufour05af40e2016-10-06 15:33:21 +02001150 unsigned long param[PLPAR_HCALL9_BUFSIZE];
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301151 int i = 0, pix = 0, rc;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301152
1153 for (i = 0; i < count; i++) {
1154
1155 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1156 pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
1157 ssize, 0);
1158 } else {
1159 param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
1160 param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
1161 pix += 2;
1162 if (pix == 8) {
1163 rc = plpar_hcall9(H_BULK_REMOVE, param,
1164 param[0], param[1], param[2],
1165 param[3], param[4], param[5],
1166 param[6], param[7]);
1167 BUG_ON(rc != H_SUCCESS);
1168 pix = 0;
1169 }
1170 }
1171 }
1172 if (pix) {
1173 param[pix] = HBR_END;
1174 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1175 param[2], param[3], param[4], param[5],
1176 param[6], param[7]);
1177 BUG_ON(rc != H_SUCCESS);
1178 }
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001179}
1180
1181static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
1182 unsigned long *vpn,
1183 int count, int psize,
1184 int ssize)
1185{
1186 unsigned long flags = 0;
1187 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1188
1189 if (lock_tlbie)
1190 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1191
Laurent Dufour59545eb2019-09-20 15:05:23 +02001192 /* Assuming THP size is 16M */
1193 if (is_supported_hlbkrm(psize, MMU_PAGE_16M))
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001194 hugepage_block_invalidate(slot, vpn, count, psize, ssize);
1195 else
1196 hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301197
1198 if (lock_tlbie)
1199 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1200}
1201
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +05301202static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1203 unsigned long addr,
1204 unsigned char *hpte_slot_array,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +05301205 int psize, int ssize, int local)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301206{
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +05301207 int i, index = 0;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301208 unsigned long s_addr = addr;
1209 unsigned int max_hpte_count, valid;
1210 unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
1211 unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +05301212 unsigned long shift, hidx, vpn = 0, hash, slot;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301213
1214 shift = mmu_psize_defs[psize].shift;
1215 max_hpte_count = 1U << (PMD_SHIFT - shift);
1216
1217 for (i = 0; i < max_hpte_count; i++) {
1218 valid = hpte_valid(hpte_slot_array, i);
1219 if (!valid)
1220 continue;
1221 hidx = hpte_hash_index(hpte_slot_array, i);
1222
1223 /* get the vpn */
1224 addr = s_addr + (i * (1ul << shift));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301225 vpn = hpt_vpn(addr, vsid, ssize);
1226 hash = hpt_hash(vpn, shift, ssize);
1227 if (hidx & _PTEIDX_SECONDARY)
1228 hash = ~hash;
1229
1230 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1231 slot += hidx & _PTEIDX_GROUP_IX;
1232
1233 slot_array[index] = slot;
1234 vpn_array[index] = vpn;
1235 if (index == PPC64_HUGE_HPTE_BATCH - 1) {
1236 /*
1237 * Now do a bluk invalidate
1238 */
1239 __pSeries_lpar_hugepage_invalidate(slot_array,
1240 vpn_array,
1241 PPC64_HUGE_HPTE_BATCH,
1242 psize, ssize);
1243 index = 0;
1244 } else
1245 index++;
1246 }
1247 if (index)
1248 __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
1249 index, psize, ssize);
1250}
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +05301251#else
1252static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1253 unsigned long addr,
1254 unsigned char *hpte_slot_array,
1255 int psize, int ssize, int local)
1256{
1257 WARN(1, "%s called without THP support\n", __func__);
1258}
1259#endif
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301260
David Gibson27828f92016-02-09 13:32:41 +10001261static int pSeries_lpar_hpte_removebolted(unsigned long ea,
1262 int psize, int ssize)
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001263{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001264 unsigned long vpn;
1265 unsigned long slot, vsid;
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001266
1267 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001268 vpn = hpt_vpn(ea, vsid, ssize);
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001269
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001270 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
David Gibson27828f92016-02-09 13:32:41 +10001271 if (slot == -1)
1272 return -ENOENT;
1273
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301274 /*
1275 * lpar doesn't use the passed actual page size
1276 */
1277 pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
David Gibson27828f92016-02-09 13:32:41 +10001278 return 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001279}
1280
Laurent Dufour0effa482018-08-20 16:29:35 +02001281
1282static inline unsigned long compute_slot(real_pte_t pte,
1283 unsigned long vpn,
1284 unsigned long index,
1285 unsigned long shift,
1286 int ssize)
1287{
1288 unsigned long slot, hash, hidx;
1289
1290 hash = hpt_hash(vpn, shift, ssize);
1291 hidx = __rpte_to_hidx(pte, index);
1292 if (hidx & _PTEIDX_SECONDARY)
1293 hash = ~hash;
1294 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1295 slot += hidx & _PTEIDX_GROUP_IX;
1296 return slot;
1297}
1298
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001299/**
1300 * The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are
1301 * "all within the same naturally aligned 8 page virtual address block".
1302 */
1303static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch,
1304 unsigned long *param)
1305{
1306 unsigned long vpn;
1307 unsigned long i, pix = 0;
1308 unsigned long index, shift, slot, current_vpgb, vpgb;
1309 real_pte_t pte;
1310 int psize, ssize;
1311
1312 psize = batch->psize;
1313 ssize = batch->ssize;
1314
1315 for (i = 0; i < number; i++) {
1316 vpn = batch->vpn[i];
1317 pte = batch->pte[i];
1318 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1319 /*
1320 * Shifting 3 bits more on the right to get a
1321 * 8 pages aligned virtual addresse.
1322 */
1323 vpgb = (vpn >> (shift - VPN_SHIFT + 3));
1324 if (!pix || vpgb != current_vpgb) {
1325 /*
1326 * Need to start a new 8 pages block, flush
1327 * the current one if needed.
1328 */
1329 if (pix)
1330 (void)call_block_remove(pix, param,
1331 true);
1332 current_vpgb = vpgb;
1333 param[0] = hpte_encode_avpn(vpn, psize,
1334 ssize);
1335 pix = 1;
1336 }
1337
1338 slot = compute_slot(pte, vpn, index, shift, ssize);
1339 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot;
1340
1341 if (pix == PLPAR_HCALL9_BUFSIZE) {
1342 pix = call_block_remove(pix, param, false);
1343 /*
1344 * pix = 0 means that all the entries were
1345 * removed, we can start a new block.
1346 * Otherwise, this means that there are entries
1347 * to retry, and pix points to latest one, so
1348 * we should increment it and try to continue
1349 * the same block.
1350 */
1351 if (pix)
1352 pix++;
1353 }
1354 } pte_iterate_hashed_end();
1355 }
1356
1357 if (pix)
1358 (void)call_block_remove(pix, param, true);
1359}
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/*
Laurent Dufour1211ee62019-09-20 15:05:22 +02001362 * TLB Block Invalidate Characteristics
1363 *
1364 * These characteristics define the size of the block the hcall H_BLOCK_REMOVE
1365 * is able to process for each couple segment base page size, actual page size.
1366 *
1367 * The ibm,get-system-parameter properties is returning a buffer with the
1368 * following layout:
1369 *
1370 * [ 2 bytes size of the RTAS buffer (excluding these 2 bytes) ]
1371 * -----------------
1372 * TLB Block Invalidate Specifiers:
1373 * [ 1 byte LOG base 2 of the TLB invalidate block size being specified ]
1374 * [ 1 byte Number of page sizes (N) that are supported for the specified
1375 * TLB invalidate block size ]
1376 * [ 1 byte Encoded segment base page size and actual page size
1377 * MSB=0 means 4k segment base page size and actual page size
1378 * MSB=1 the penc value in mmu_psize_def ]
1379 * ...
1380 * -----------------
1381 * Next TLB Block Invalidate Specifiers...
1382 * -----------------
1383 * [ 0 ]
1384 */
1385static inline void set_hblkrm_bloc_size(int bpsize, int psize,
1386 unsigned int block_size)
1387{
1388 if (block_size > hblkrm_size[bpsize][psize])
1389 hblkrm_size[bpsize][psize] = block_size;
1390}
1391
1392/*
1393 * Decode the Encoded segment base page size and actual page size.
1394 * PAPR specifies:
1395 * - bit 7 is the L bit
1396 * - bits 0-5 are the penc value
1397 * If the L bit is 0, this means 4K segment base page size and actual page size
1398 * otherwise the penc value should be read.
1399 */
1400#define HBLKRM_L_MASK 0x80
1401#define HBLKRM_PENC_MASK 0x3f
1402static inline void __init check_lp_set_hblkrm(unsigned int lp,
1403 unsigned int block_size)
1404{
1405 unsigned int bpsize, psize;
1406
1407 /* First, check the L bit, if not set, this means 4K */
1408 if ((lp & HBLKRM_L_MASK) == 0) {
1409 set_hblkrm_bloc_size(MMU_PAGE_4K, MMU_PAGE_4K, block_size);
1410 return;
1411 }
1412
1413 lp &= HBLKRM_PENC_MASK;
1414 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++) {
1415 struct mmu_psize_def *def = &mmu_psize_defs[bpsize];
1416
1417 for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1418 if (def->penc[psize] == lp) {
1419 set_hblkrm_bloc_size(bpsize, psize, block_size);
1420 return;
1421 }
1422 }
1423 }
1424}
1425
1426#define SPLPAR_TLB_BIC_TOKEN 50
1427
1428/*
1429 * The size of the TLB Block Invalidate Characteristics is variable. But at the
1430 * maximum it will be the number of possible page sizes *2 + 10 bytes.
1431 * Currently MMU_PAGE_COUNT is 16, which means 42 bytes. Use a cache line size
1432 * (128 bytes) for the buffer to get plenty of space.
1433 */
1434#define SPLPAR_TLB_BIC_MAXLENGTH 128
1435
1436void __init pseries_lpar_read_hblkrm_characteristics(void)
1437{
1438 unsigned char local_buffer[SPLPAR_TLB_BIC_MAXLENGTH];
1439 int call_status, len, idx, bpsize;
1440
Laurent Dufour4ab8a482019-10-01 15:29:28 +02001441 if (!firmware_has_feature(FW_FEATURE_BLOCK_REMOVE))
1442 return;
1443
Laurent Dufour1211ee62019-09-20 15:05:22 +02001444 spin_lock(&rtas_data_buf_lock);
1445 memset(rtas_data_buf, 0, RTAS_DATA_BUF_SIZE);
1446 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
1447 NULL,
1448 SPLPAR_TLB_BIC_TOKEN,
1449 __pa(rtas_data_buf),
1450 RTAS_DATA_BUF_SIZE);
1451 memcpy(local_buffer, rtas_data_buf, SPLPAR_TLB_BIC_MAXLENGTH);
1452 local_buffer[SPLPAR_TLB_BIC_MAXLENGTH - 1] = '\0';
1453 spin_unlock(&rtas_data_buf_lock);
1454
1455 if (call_status != 0) {
1456 pr_warn("%s %s Error calling get-system-parameter (0x%x)\n",
1457 __FILE__, __func__, call_status);
1458 return;
1459 }
1460
1461 /*
1462 * The first two (2) bytes of the data in the buffer are the length of
1463 * the returned data, not counting these first two (2) bytes.
1464 */
1465 len = be16_to_cpu(*((u16 *)local_buffer)) + 2;
1466 if (len > SPLPAR_TLB_BIC_MAXLENGTH) {
1467 pr_warn("%s too large returned buffer %d", __func__, len);
1468 return;
1469 }
1470
1471 idx = 2;
1472 while (idx < len) {
1473 u8 block_shift = local_buffer[idx++];
1474 u32 block_size;
1475 unsigned int npsize;
1476
1477 if (!block_shift)
1478 break;
1479
1480 block_size = 1 << block_shift;
1481
1482 for (npsize = local_buffer[idx++];
1483 npsize > 0 && idx < len; npsize--)
1484 check_lp_set_hblkrm((unsigned int) local_buffer[idx++],
1485 block_size);
1486 }
1487
1488 for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
1489 for (idx = 0; idx < MMU_PAGE_COUNT; idx++)
1490 if (hblkrm_size[bpsize][idx])
1491 pr_info("H_BLOCK_REMOVE supports base psize:%d psize:%d block size:%d",
1492 bpsize, idx, hblkrm_size[bpsize][idx]);
1493}
1494
1495/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
1497 * lock.
1498 */
Geoff Levand035223f2006-10-05 11:35:10 -07001499static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001501 unsigned long vpn;
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001502 unsigned long i, pix, rc;
Paul Mackerras12e86f92007-02-08 15:02:35 +11001503 unsigned long flags = 0;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001504 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
Matt Evans44ae3ab2011-04-06 19:48:50 +00001505 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Laurent Dufour05af40e2016-10-06 15:33:21 +02001506 unsigned long param[PLPAR_HCALL9_BUFSIZE];
Laurent Dufour0effa482018-08-20 16:29:35 +02001507 unsigned long index, shift, slot;
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001508 real_pte_t pte;
Paul Mackerras1189be62007-10-11 20:37:10 +10001509 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (lock_tlbie)
1512 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1513
Laurent Dufour59545eb2019-09-20 15:05:23 +02001514 if (is_supported_hlbkrm(batch->psize, batch->psize)) {
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001515 do_block_remove(number, batch, param);
1516 goto out;
1517 }
1518
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001519 psize = batch->psize;
Paul Mackerras1189be62007-10-11 20:37:10 +10001520 ssize = batch->ssize;
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001521 pix = 0;
1522 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001523 vpn = batch->vpn[i];
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001524 pte = batch->pte[i];
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001525 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
Laurent Dufour0effa482018-08-20 16:29:35 +02001526 slot = compute_slot(pte, vpn, index, shift, ssize);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001527 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301528 /*
1529 * lpar doesn't use the passed actual page size
1530 */
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001531 pSeries_lpar_hpte_invalidate(slot, vpn, psize,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301532 0, ssize, local);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001533 } else {
1534 param[pix] = HBR_REQUEST | HBR_AVPN | slot;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001535 param[pix+1] = hpte_encode_avpn(vpn, psize,
Paul Mackerras1189be62007-10-11 20:37:10 +10001536 ssize);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001537 pix += 2;
1538 if (pix == 8) {
1539 rc = plpar_hcall9(H_BULK_REMOVE, param,
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001540 param[0], param[1], param[2],
1541 param[3], param[4], param[5],
1542 param[6], param[7]);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001543 BUG_ON(rc != H_SUCCESS);
1544 pix = 0;
1545 }
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001546 }
1547 } pte_iterate_hashed_end();
1548 }
1549 if (pix) {
1550 param[pix] = HBR_END;
1551 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1552 param[2], param[3], param[4], param[5],
1553 param[6], param[7]);
1554 BUG_ON(rc != H_SUCCESS);
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001557out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (lock_tlbie)
1559 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1560}
1561
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001562static int __init disable_bulk_remove(char *str)
1563{
1564 if (strcmp(str, "off") == 0 &&
1565 firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301566 pr_info("Disabling BULK_REMOVE firmware feature");
1567 powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001568 }
1569 return 1;
1570}
1571
1572__setup("bulk_remove=", disable_bulk_remove);
1573
David Gibsondbcf9292016-12-09 11:07:36 +11001574#define HPT_RESIZE_TIMEOUT 10000 /* ms */
1575
1576struct hpt_resize_state {
1577 unsigned long shift;
1578 int commit_rc;
1579};
1580
1581static int pseries_lpar_resize_hpt_commit(void *data)
1582{
1583 struct hpt_resize_state *state = data;
1584
1585 state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
1586 if (state->commit_rc != H_SUCCESS)
1587 return -EIO;
1588
1589 /* Hypervisor has transitioned the HTAB, update our globals */
1590 ppc64_pft_size = state->shift;
1591 htab_size_bytes = 1UL << ppc64_pft_size;
1592 htab_hash_mask = (htab_size_bytes >> 7) - 1;
1593
1594 return 0;
1595}
1596
Gautham R. Shenoyc784be42019-05-15 13:15:52 +05301597/*
1598 * Must be called in process context. The caller must hold the
1599 * cpus_lock.
1600 */
David Gibsondbcf9292016-12-09 11:07:36 +11001601static int pseries_lpar_resize_hpt(unsigned long shift)
1602{
1603 struct hpt_resize_state state = {
1604 .shift = shift,
1605 .commit_rc = H_FUNCTION,
1606 };
1607 unsigned int delay, total_delay = 0;
1608 int rc;
1609 ktime_t t0, t1, t2;
1610
1611 might_sleep();
1612
1613 if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1614 return -ENODEV;
1615
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301616 pr_info("Attempting to resize HPT to shift %lu\n", shift);
David Gibsondbcf9292016-12-09 11:07:36 +11001617
1618 t0 = ktime_get();
1619
1620 rc = plpar_resize_hpt_prepare(0, shift);
1621 while (H_IS_LONG_BUSY(rc)) {
1622 delay = get_longbusy_msecs(rc);
1623 total_delay += delay;
1624 if (total_delay > HPT_RESIZE_TIMEOUT) {
1625 /* prepare with shift==0 cancels an in-progress resize */
1626 rc = plpar_resize_hpt_prepare(0, 0);
1627 if (rc != H_SUCCESS)
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301628 pr_warn("Unexpected error %d cancelling timed out HPT resize\n",
David Gibsondbcf9292016-12-09 11:07:36 +11001629 rc);
1630 return -ETIMEDOUT;
1631 }
1632 msleep(delay);
1633 rc = plpar_resize_hpt_prepare(0, shift);
1634 };
1635
1636 switch (rc) {
1637 case H_SUCCESS:
1638 /* Continue on */
1639 break;
1640
1641 case H_PARAMETER:
Laurent Vivierf172acb2019-03-13 11:25:28 +01001642 pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n");
David Gibsondbcf9292016-12-09 11:07:36 +11001643 return -EINVAL;
1644 case H_RESOURCE:
Laurent Vivierf172acb2019-03-13 11:25:28 +01001645 pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n");
David Gibsondbcf9292016-12-09 11:07:36 +11001646 return -EPERM;
1647 default:
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301648 pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc);
David Gibsondbcf9292016-12-09 11:07:36 +11001649 return -EIO;
1650 }
1651
1652 t1 = ktime_get();
1653
Gautham R. Shenoyc784be42019-05-15 13:15:52 +05301654 rc = stop_machine_cpuslocked(pseries_lpar_resize_hpt_commit,
1655 &state, NULL);
David Gibsondbcf9292016-12-09 11:07:36 +11001656
1657 t2 = ktime_get();
1658
1659 if (rc != 0) {
1660 switch (state.commit_rc) {
1661 case H_PTEG_FULL:
David Gibsondbcf9292016-12-09 11:07:36 +11001662 return -ENOSPC;
1663
1664 default:
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301665 pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
1666 state.commit_rc);
David Gibsondbcf9292016-12-09 11:07:36 +11001667 return -EIO;
1668 };
1669 }
1670
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301671 pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
1672 shift, (long long) ktime_ms_delta(t1, t0),
1673 (long long) ktime_ms_delta(t2, t1));
David Gibsondbcf9292016-12-09 11:07:36 +11001674
1675 return 0;
1676}
1677
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001678static int pseries_lpar_register_process_table(unsigned long base,
1679 unsigned long page_size, unsigned long table_size)
1680{
1681 long rc;
Paul Mackerrasdbfcf3c2017-02-16 16:03:39 +11001682 unsigned long flags = 0;
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001683
Paul Mackerrasdbfcf3c2017-02-16 16:03:39 +11001684 if (table_size)
1685 flags |= PROC_TABLE_NEW;
Bharata B Raob6c84172020-07-03 11:06:07 +05301686 if (radix_enabled()) {
1687 flags |= PROC_TABLE_RADIX;
1688 if (mmu_has_feature(MMU_FTR_GTSE))
1689 flags |= PROC_TABLE_GTSE;
1690 } else
Paul Mackerrasdbfcf3c2017-02-16 16:03:39 +11001691 flags |= PROC_TABLE_HPT_SLB;
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001692 for (;;) {
1693 rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
1694 page_size, table_size);
1695 if (!H_IS_LONG_BUSY(rc))
1696 break;
1697 mdelay(get_longbusy_msecs(rc));
1698 }
1699 if (rc != H_SUCCESS) {
1700 pr_err("Failed to register process table (rc=%ld)\n", rc);
1701 BUG();
1702 }
1703 return rc;
1704}
1705
Michael Ellerman6364e842016-07-26 10:33:03 +10001706void __init hpte_init_pseries(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001708 mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate;
1709 mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp;
1710 mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
1711 mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert;
1712 mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove;
1713 mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted;
1714 mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range;
Anton Blanchard5246ade2016-10-01 20:41:56 +10001715 mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001716 mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
Michael Ellerman8971e1c2017-03-17 16:02:35 +11001717
1718 if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1719 mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
Nicholas Piggined6546b2019-09-03 01:29:26 +10001720
1721 /*
1722 * On POWER9, we need to do a H_REGISTER_PROC_TBL hcall
1723 * to inform the hypervisor that we wish to use the HPT.
1724 */
1725 if (cpu_has_feature(CPU_FTR_ARCH_300))
1726 pseries_lpar_register_process_table(0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
Robert Jennings14f966e2009-04-15 05:55:32 +00001728
Yang Yingliangbda76732020-09-17 10:06:43 +08001729#ifdef CONFIG_PPC_RADIX_MMU
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001730void radix_init_pseries(void)
1731{
1732 pr_info("Using radix MMU under hypervisor\n");
Nicholas Piggined6546b2019-09-03 01:29:26 +10001733
1734 pseries_lpar_register_process_table(__pa(process_tb),
1735 0, PRTB_SIZE_SHIFT - 12);
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001736}
Yang Yingliangbda76732020-09-17 10:06:43 +08001737#endif
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001738
Robert Jennings14f966e2009-04-15 05:55:32 +00001739#ifdef CONFIG_PPC_SMLPAR
1740#define CMO_FREE_HINT_DEFAULT 1
1741static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
1742
1743static int __init cmo_free_hint(char *str)
1744{
1745 char *parm;
1746 parm = strstrip(str);
1747
1748 if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301749 pr_info("%s: CMO free page hinting is not active.\n", __func__);
Robert Jennings14f966e2009-04-15 05:55:32 +00001750 cmo_free_hint_flag = 0;
1751 return 1;
1752 }
1753
1754 cmo_free_hint_flag = 1;
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301755 pr_info("%s: CMO free page hinting is active.\n", __func__);
Robert Jennings14f966e2009-04-15 05:55:32 +00001756
1757 if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0)
1758 return 1;
1759
1760 return 0;
1761}
1762
1763__setup("cmo_free_hint=", cmo_free_hint);
1764
1765static void pSeries_set_page_state(struct page *page, int order,
1766 unsigned long state)
1767{
1768 int i, j;
1769 unsigned long cmo_page_sz, addr;
1770
1771 cmo_page_sz = cmo_get_page_size();
1772 addr = __pa((unsigned long)page_address(page));
1773
1774 for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {
1775 for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)
1776 plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);
1777 }
1778}
1779
1780void arch_free_page(struct page *page, int order)
1781{
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +10001782 if (radix_enabled())
1783 return;
Robert Jennings14f966e2009-04-15 05:55:32 +00001784 if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO))
1785 return;
1786
1787 pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED);
1788}
1789EXPORT_SYMBOL(arch_free_page);
1790
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +10001791#endif /* CONFIG_PPC_SMLPAR */
Michael Ellerman4e003742017-10-19 15:08:43 +11001792#endif /* CONFIG_PPC_BOOK3S_64 */
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001793
1794#ifdef CONFIG_TRACEPOINTS
Masahiro Yamadae9666d12018-12-31 00:14:15 +09001795#ifdef CONFIG_JUMP_LABEL
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001796struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
1797
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001798int hcall_tracepoint_regfunc(void)
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001799{
1800 static_key_slow_inc(&hcall_tracepoint_key);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001801 return 0;
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001802}
1803
1804void hcall_tracepoint_unregfunc(void)
1805{
1806 static_key_slow_dec(&hcall_tracepoint_key);
1807}
1808#else
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001809/*
1810 * We optimise our hcall path by placing hcall_tracepoint_refcount
1811 * directly in the TOC so we can check if the hcall tracepoints are
1812 * enabled via a single load.
1813 */
1814
1815/* NB: reg/unreg are called while guarded with the tracepoints_mutex */
1816extern long hcall_tracepoint_refcount;
1817
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001818int hcall_tracepoint_regfunc(void)
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001819{
1820 hcall_tracepoint_refcount++;
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001821 return 0;
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001822}
1823
1824void hcall_tracepoint_unregfunc(void)
1825{
1826 hcall_tracepoint_refcount--;
1827}
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001828#endif
1829
1830/*
1831 * Since the tracing code might execute hcalls we need to guard against
1832 * recursion. One example of this are spinlocks calling H_YIELD on
1833 * shared processor partitions.
1834 */
1835static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
1836
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001837
Anton Blanchard6f263532009-10-26 18:51:09 +00001838void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001839{
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001840 unsigned long flags;
1841 unsigned int *depth;
1842
Anton Blancharda5ccfee2012-01-09 14:29:15 +00001843 /*
1844 * We cannot call tracepoints inside RCU idle regions which
1845 * means we must not trace H_CEDE.
1846 */
1847 if (opcode == H_CEDE)
1848 return;
1849
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001850 local_irq_save(flags);
1851
Christoph Lameter69111ba2014-10-21 15:23:25 -05001852 depth = this_cpu_ptr(&hcall_trace_depth);
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001853
1854 if (*depth)
1855 goto out;
1856
1857 (*depth)++;
Li Zhonge4f387d2011-12-18 16:03:04 +00001858 preempt_disable();
Anton Blanchard6f263532009-10-26 18:51:09 +00001859 trace_hcall_entry(opcode, args);
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001860 (*depth)--;
1861
1862out:
1863 local_irq_restore(flags);
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001864}
1865
Michael Ellerman8f2133c2018-05-07 23:03:55 +10001866void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf)
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001867{
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001868 unsigned long flags;
1869 unsigned int *depth;
1870
Anton Blancharda5ccfee2012-01-09 14:29:15 +00001871 if (opcode == H_CEDE)
1872 return;
1873
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001874 local_irq_save(flags);
1875
Christoph Lameter69111ba2014-10-21 15:23:25 -05001876 depth = this_cpu_ptr(&hcall_trace_depth);
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001877
1878 if (*depth)
1879 goto out;
1880
1881 (*depth)++;
Anton Blanchard6f263532009-10-26 18:51:09 +00001882 trace_hcall_exit(opcode, retval, retbuf);
Li Zhonge4f387d2011-12-18 16:03:04 +00001883 preempt_enable();
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001884 (*depth)--;
1885
1886out:
1887 local_irq_restore(flags);
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001888}
1889#endif
Brian King9ee820f2011-05-04 16:01:20 +10001890
1891/**
1892 * h_get_mpp
1893 * H_GET_MPP hcall returns info in 7 parms
1894 */
1895int h_get_mpp(struct hvcall_mpp_data *mpp_data)
1896{
1897 int rc;
1898 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1899
1900 rc = plpar_hcall9(H_GET_MPP, retbuf);
1901
1902 mpp_data->entitled_mem = retbuf[0];
1903 mpp_data->mapped_mem = retbuf[1];
1904
1905 mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
1906 mpp_data->pool_num = retbuf[2] & 0xffff;
1907
1908 mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
1909 mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
Anton Blanchardb0d436c2013-08-07 02:01:24 +10001910 mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
Brian King9ee820f2011-05-04 16:01:20 +10001911
1912 mpp_data->pool_size = retbuf[4];
1913 mpp_data->loan_request = retbuf[5];
1914 mpp_data->backing_mem = retbuf[6];
1915
1916 return rc;
1917}
1918EXPORT_SYMBOL(h_get_mpp);
1919
1920int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
1921{
1922 int rc;
1923 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
1924
1925 rc = plpar_hcall9(H_GET_MPP_X, retbuf);
1926
1927 mpp_x_data->coalesced_bytes = retbuf[0];
1928 mpp_x_data->pool_coalesced_bytes = retbuf[1];
1929 mpp_x_data->pool_purr_cycles = retbuf[2];
1930 mpp_x_data->pool_spurr_cycles = retbuf[3];
1931
1932 return rc;
1933}
Aneesh Kumar K.V82228e32017-03-22 09:07:00 +05301934
1935static unsigned long vsid_unscramble(unsigned long vsid, int ssize)
1936{
1937 unsigned long protovsid;
1938 unsigned long va_bits = VA_BITS;
1939 unsigned long modinv, vsid_modulus;
1940 unsigned long max_mod_inv, tmp_modinv;
1941
1942 if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
1943 va_bits = 65;
1944
1945 if (ssize == MMU_SEGSIZE_256M) {
1946 modinv = VSID_MULINV_256M;
1947 vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);
1948 } else {
1949 modinv = VSID_MULINV_1T;
1950 vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);
1951 }
1952
1953 /*
1954 * vsid outside our range.
1955 */
1956 if (vsid >= vsid_modulus)
1957 return 0;
1958
1959 /*
1960 * If modinv is the modular multiplicate inverse of (x % vsid_modulus)
1961 * and vsid = (protovsid * x) % vsid_modulus, then we say:
1962 * protovsid = (vsid * modinv) % vsid_modulus
1963 */
1964
1965 /* Check if (vsid * modinv) overflow (63 bits) */
1966 max_mod_inv = 0x7fffffffffffffffull / vsid;
1967 if (modinv < max_mod_inv)
1968 return (vsid * modinv) % vsid_modulus;
1969
1970 tmp_modinv = modinv/max_mod_inv;
1971 modinv %= max_mod_inv;
1972
1973 protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;
1974 protovsid = (protovsid + vsid * modinv) % vsid_modulus;
1975
1976 return protovsid;
1977}
1978
1979static int __init reserve_vrma_context_id(void)
1980{
1981 unsigned long protovsid;
1982
1983 /*
1984 * Reserve context ids which map to reserved virtual addresses. For now
1985 * we only reserve the context id which maps to the VRMA VSID. We ignore
1986 * the addresses in "ibm,adjunct-virtual-addresses" because we don't
1987 * enable adjunct support via the "ibm,client-architecture-support"
1988 * interface.
1989 */
1990 protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);
1991 hash__reserve_context_id(protovsid >> ESID_BITS_1T);
1992 return 0;
1993}
1994machine_device_initcall(pseries, reserve_vrma_context_id);
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +05301995
1996#ifdef CONFIG_DEBUG_FS
1997/* debugfs file interface for vpa data */
1998static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,
1999 loff_t *pos)
2000{
2001 int cpu = (long)filp->private_data;
2002 struct lppaca *lppaca = &lppaca_of(cpu);
2003
2004 return simple_read_from_buffer(buf, len, pos, lppaca,
2005 sizeof(struct lppaca));
2006}
2007
2008static const struct file_operations vpa_fops = {
2009 .open = simple_open,
2010 .read = vpa_file_read,
2011 .llseek = default_llseek,
2012};
2013
2014static int __init vpa_debugfs_init(void)
2015{
2016 char name[16];
2017 long i;
YueHaibing11dd34f2019-02-18 12:56:44 +00002018 struct dentry *vpa_dir;
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +05302019
2020 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
2021 return 0;
2022
2023 vpa_dir = debugfs_create_dir("vpa", powerpc_debugfs_root);
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +05302024
2025 /* set up the per-cpu vpa file*/
2026 for_each_possible_cpu(i) {
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +05302027 sprintf(name, "cpu-%ld", i);
Greg Kroah-Hartmanff229312019-10-14 12:16:42 +02002028 debugfs_create_file(name, 0400, vpa_dir, (void *)i, &vpa_fops);
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +05302029 }
2030
2031 return 0;
2032}
2033machine_arch_initcall(pseries, vpa_debugfs_init);
2034#endif /* CONFIG_DEBUG_FS */