blob: ec5a7893f71b54d2935063718445c5710cbab76c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Ellermanf7ebf352008-04-24 15:13:19 +100022/* Enables debugging of low-level hash table routines - careful! */
23#undef DEBUG
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +053024#define pr_fmt(fmt) "lpar: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/dma-mapping.h>
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110028#include <linux/console.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040029#include <linux/export.h>
Anton Blanchard58995a92015-04-09 13:51:32 +100030#include <linux/jump_label.h>
David Gibsondbcf9292016-12-09 11:07:36 +110031#include <linux/delay.h>
32#include <linux/stop_machine.h>
Naveen N. Raod62c8de2019-07-03 22:34:00 +053033#include <linux/spinlock.h>
34#include <linux/cpuhotplug.h>
35#include <linux/workqueue.h>
36#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/processor.h>
38#include <asm/mmu.h>
39#include <asm/page.h>
40#include <asm/pgtable.h>
41#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/tlb.h>
45#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/cputable.h>
David Gibsondcad47f2005-11-07 09:49:43 +110047#include <asm/udbg.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110048#include <asm/smp.h>
Anton Blanchardc8cd0932009-10-26 18:50:29 +000049#include <asm/trace.h>
Stephen Rothwellf5339272012-03-15 18:18:00 +000050#include <asm/firmware.h>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053051#include <asm/plpar_wrappers.h>
Hari Bathinic1caae32014-12-18 23:36:55 +053052#include <asm/kexec.h>
Hari Bathini408cddd2014-10-01 12:32:30 +053053#include <asm/fadump.h>
Daniel Axtens42f5b4c2016-05-18 11:16:50 +100054#include <asm/asm-prototypes.h>
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +053055#include <asm/debugfs.h>
Michael Ellermana1218722005-11-03 15:33:31 +110056
Michael Ellerman21cf9132008-04-16 13:51:48 +100057#include "pseries.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +053059/* Flag bits for H_BULK_REMOVE */
60#define HBR_REQUEST 0x4000000000000000UL
61#define HBR_RESPONSE 0x8000000000000000UL
62#define HBR_END 0xc000000000000000UL
63#define HBR_AVPN 0x0200000000000000UL
64#define HBR_ANDCOND 0x0100000000000000UL
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Anton Blanchardb9377ff2006-07-19 08:01:28 +100067/* in hvCall.S */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068EXPORT_SYMBOL(plpar_hcall);
Anton Blanchardb9377ff2006-07-19 08:01:28 +100069EXPORT_SYMBOL(plpar_hcall9);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070EXPORT_SYMBOL(plpar_hcall_norets);
Anton Blanchardb9377ff2006-07-19 08:01:28 +100071
Naveen N. Raod62c8de2019-07-03 22:34:00 +053072#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
73static u8 dtl_mask = DTL_LOG_PREEMPT;
74#else
75static u8 dtl_mask;
76#endif
77
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053078void alloc_dtl_buffers(void)
79{
80 int cpu;
81 struct paca_struct *pp;
82 struct dtl_entry *dtl;
83
84 for_each_possible_cpu(cpu) {
85 pp = paca_ptrs[cpu];
Naveen N. Raod62c8de2019-07-03 22:34:00 +053086 if (pp->dispatch_log)
87 continue;
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053088 dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
89 if (!dtl) {
90 pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
91 cpu);
Naveen N. Raod62c8de2019-07-03 22:34:00 +053092#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053093 pr_warn("Stolen time statistics will be unreliable\n");
Naveen N. Raod62c8de2019-07-03 22:34:00 +053094#endif
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +053095 break;
96 }
97
98 pp->dtl_ridx = 0;
99 pp->dispatch_log = dtl;
100 pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
101 pp->dtl_curr = dtl;
102 }
103}
104
105void register_dtl_buffer(int cpu)
106{
107 long ret;
108 struct paca_struct *pp;
109 struct dtl_entry *dtl;
110 int hwcpu = get_hard_smp_processor_id(cpu);
111
112 pp = paca_ptrs[cpu];
113 dtl = pp->dispatch_log;
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530114 if (dtl && dtl_mask) {
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +0530115 pp->dtl_ridx = 0;
116 pp->dtl_curr = dtl;
117 lppaca_of(cpu).dtl_idx = 0;
118
119 /* hypervisor reads buffer length from this field */
120 dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
121 ret = register_dtl(hwcpu, __pa(dtl));
122 if (ret)
123 pr_err("WARNING: DTL registration of cpu %d (hw %d) failed with %ld\n",
124 cpu, hwcpu, ret);
125
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530126 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +0530127 }
128}
129
Naveen N. Rao06220d72019-07-03 22:33:57 +0530130#ifdef CONFIG_PPC_SPLPAR
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530131struct dtl_worker {
132 struct delayed_work work;
133 int cpu;
134};
135
136struct vcpu_dispatch_data {
137 int last_disp_cpu;
138
139 int total_disp;
140
141 int same_cpu_disp;
142 int same_chip_disp;
143 int diff_chip_disp;
144 int far_chip_disp;
145
146 int numa_home_disp;
147 int numa_remote_disp;
148 int numa_far_disp;
149};
150
151/*
152 * This represents the number of cpus in the hypervisor. Since there is no
153 * architected way to discover the number of processors in the host, we
154 * provision for dealing with NR_CPUS. This is currently 2048 by default, and
155 * is sufficient for our purposes. This will need to be tweaked if
156 * CONFIG_NR_CPUS is changed.
157 */
158#define NR_CPUS_H NR_CPUS
159
Naveen N. Rao06220d72019-07-03 22:33:57 +0530160DEFINE_RWLOCK(dtl_access_lock);
Naveen N. Raod62c8de2019-07-03 22:34:00 +0530161static DEFINE_PER_CPU(struct vcpu_dispatch_data, vcpu_disp_data);
162static DEFINE_PER_CPU(u64, dtl_entry_ridx);
163static DEFINE_PER_CPU(struct dtl_worker, dtl_workers);
164static enum cpuhp_state dtl_worker_state;
165static DEFINE_MUTEX(dtl_enable_mutex);
166static int vcpudispatch_stats_on __read_mostly;
167static int vcpudispatch_stats_freq = 50;
168static __be32 *vcpu_associativity, *pcpu_associativity;
169
170
171static void free_dtl_buffers(void)
172{
173#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
174 int cpu;
175 struct paca_struct *pp;
176
177 for_each_possible_cpu(cpu) {
178 pp = paca_ptrs[cpu];
179 if (!pp->dispatch_log)
180 continue;
181 kmem_cache_free(dtl_cache, pp->dispatch_log);
182 pp->dtl_ridx = 0;
183 pp->dispatch_log = 0;
184 pp->dispatch_log_end = 0;
185 pp->dtl_curr = 0;
186 }
187#endif
188}
189
190static int init_cpu_associativity(void)
191{
192 vcpu_associativity = kcalloc(num_possible_cpus() / threads_per_core,
193 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
194 pcpu_associativity = kcalloc(NR_CPUS_H / threads_per_core,
195 VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
196
197 if (!vcpu_associativity || !pcpu_associativity) {
198 pr_err("error allocating memory for associativity information\n");
199 return -ENOMEM;
200 }
201
202 return 0;
203}
204
205static void destroy_cpu_associativity(void)
206{
207 kfree(vcpu_associativity);
208 kfree(pcpu_associativity);
209 vcpu_associativity = pcpu_associativity = 0;
210}
211
212static __be32 *__get_cpu_associativity(int cpu, __be32 *cpu_assoc, int flag)
213{
214 __be32 *assoc;
215 int rc = 0;
216
217 assoc = &cpu_assoc[(int)(cpu / threads_per_core) * VPHN_ASSOC_BUFSIZE];
218 if (!assoc[0]) {
219 rc = hcall_vphn(cpu, flag, &assoc[0]);
220 if (rc)
221 return NULL;
222 }
223
224 return assoc;
225}
226
227static __be32 *get_pcpu_associativity(int cpu)
228{
229 return __get_cpu_associativity(cpu, pcpu_associativity, VPHN_FLAG_PCPU);
230}
231
232static __be32 *get_vcpu_associativity(int cpu)
233{
234 return __get_cpu_associativity(cpu, vcpu_associativity, VPHN_FLAG_VCPU);
235}
236
237static int cpu_relative_dispatch_distance(int last_disp_cpu, int cur_disp_cpu)
238{
239 __be32 *last_disp_cpu_assoc, *cur_disp_cpu_assoc;
240
241 if (last_disp_cpu >= NR_CPUS_H || cur_disp_cpu >= NR_CPUS_H)
242 return -EINVAL;
243
244 last_disp_cpu_assoc = get_pcpu_associativity(last_disp_cpu);
245 cur_disp_cpu_assoc = get_pcpu_associativity(cur_disp_cpu);
246
247 if (!last_disp_cpu_assoc || !cur_disp_cpu_assoc)
248 return -EIO;
249
250 return cpu_distance(last_disp_cpu_assoc, cur_disp_cpu_assoc);
251}
252
253static int cpu_home_node_dispatch_distance(int disp_cpu)
254{
255 __be32 *disp_cpu_assoc, *vcpu_assoc;
256 int vcpu_id = smp_processor_id();
257
258 if (disp_cpu >= NR_CPUS_H) {
259 pr_debug_ratelimited("vcpu dispatch cpu %d > %d\n",
260 disp_cpu, NR_CPUS_H);
261 return -EINVAL;
262 }
263
264 disp_cpu_assoc = get_pcpu_associativity(disp_cpu);
265 vcpu_assoc = get_vcpu_associativity(vcpu_id);
266
267 if (!disp_cpu_assoc || !vcpu_assoc)
268 return -EIO;
269
270 return cpu_distance(disp_cpu_assoc, vcpu_assoc);
271}
272
273static void update_vcpu_disp_stat(int disp_cpu)
274{
275 struct vcpu_dispatch_data *disp;
276 int distance;
277
278 disp = this_cpu_ptr(&vcpu_disp_data);
279 if (disp->last_disp_cpu == -1) {
280 disp->last_disp_cpu = disp_cpu;
281 return;
282 }
283
284 disp->total_disp++;
285
286 if (disp->last_disp_cpu == disp_cpu ||
287 (cpu_first_thread_sibling(disp->last_disp_cpu) ==
288 cpu_first_thread_sibling(disp_cpu)))
289 disp->same_cpu_disp++;
290 else {
291 distance = cpu_relative_dispatch_distance(disp->last_disp_cpu,
292 disp_cpu);
293 if (distance < 0)
294 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
295 smp_processor_id());
296 else {
297 switch (distance) {
298 case 0:
299 disp->same_chip_disp++;
300 break;
301 case 1:
302 disp->diff_chip_disp++;
303 break;
304 case 2:
305 disp->far_chip_disp++;
306 break;
307 default:
308 pr_debug_ratelimited("vcpudispatch_stats: cpu %d (%d -> %d): unexpected relative dispatch distance %d\n",
309 smp_processor_id(),
310 disp->last_disp_cpu,
311 disp_cpu,
312 distance);
313 }
314 }
315 }
316
317 distance = cpu_home_node_dispatch_distance(disp_cpu);
318 if (distance < 0)
319 pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
320 smp_processor_id());
321 else {
322 switch (distance) {
323 case 0:
324 disp->numa_home_disp++;
325 break;
326 case 1:
327 disp->numa_remote_disp++;
328 break;
329 case 2:
330 disp->numa_far_disp++;
331 break;
332 default:
333 pr_debug_ratelimited("vcpudispatch_stats: cpu %d on %d: unexpected numa dispatch distance %d\n",
334 smp_processor_id(),
335 disp_cpu,
336 distance);
337 }
338 }
339
340 disp->last_disp_cpu = disp_cpu;
341}
342
343static void process_dtl_buffer(struct work_struct *work)
344{
345 struct dtl_entry dtle;
346 u64 i = __this_cpu_read(dtl_entry_ridx);
347 struct dtl_entry *dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
348 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
349 struct lppaca *vpa = local_paca->lppaca_ptr;
350 struct dtl_worker *d = container_of(work, struct dtl_worker, work.work);
351
352 if (!local_paca->dispatch_log)
353 return;
354
355 /* if we have been migrated away, we cancel ourself */
356 if (d->cpu != smp_processor_id()) {
357 pr_debug("vcpudispatch_stats: cpu %d worker migrated -- canceling worker\n",
358 smp_processor_id());
359 return;
360 }
361
362 if (i == be64_to_cpu(vpa->dtl_idx))
363 goto out;
364
365 while (i < be64_to_cpu(vpa->dtl_idx)) {
366 dtle = *dtl;
367 barrier();
368 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
369 /* buffer has overflowed */
370 pr_debug_ratelimited("vcpudispatch_stats: cpu %d lost %lld DTL samples\n",
371 d->cpu,
372 be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG - i);
373 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
374 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
375 continue;
376 }
377 update_vcpu_disp_stat(be16_to_cpu(dtle.processor_id));
378 ++i;
379 ++dtl;
380 if (dtl == dtl_end)
381 dtl = local_paca->dispatch_log;
382 }
383
384 __this_cpu_write(dtl_entry_ridx, i);
385
386out:
387 schedule_delayed_work_on(d->cpu, to_delayed_work(work),
388 HZ / vcpudispatch_stats_freq);
389}
390
391static int dtl_worker_online(unsigned int cpu)
392{
393 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
394
395 memset(d, 0, sizeof(*d));
396 INIT_DELAYED_WORK(&d->work, process_dtl_buffer);
397 d->cpu = cpu;
398
399#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
400 per_cpu(dtl_entry_ridx, cpu) = 0;
401 register_dtl_buffer(cpu);
402#else
403 per_cpu(dtl_entry_ridx, cpu) = be64_to_cpu(lppaca_of(cpu).dtl_idx);
404#endif
405
406 schedule_delayed_work_on(cpu, &d->work, HZ / vcpudispatch_stats_freq);
407 return 0;
408}
409
410static int dtl_worker_offline(unsigned int cpu)
411{
412 struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
413
414 cancel_delayed_work_sync(&d->work);
415
416#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
417 unregister_dtl(get_hard_smp_processor_id(cpu));
418#endif
419
420 return 0;
421}
422
423static void set_global_dtl_mask(u8 mask)
424{
425 int cpu;
426
427 dtl_mask = mask;
428 for_each_present_cpu(cpu)
429 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
430}
431
432static void reset_global_dtl_mask(void)
433{
434 int cpu;
435
436#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
437 dtl_mask = DTL_LOG_PREEMPT;
438#else
439 dtl_mask = 0;
440#endif
441 for_each_present_cpu(cpu)
442 lppaca_of(cpu).dtl_enable_mask = dtl_mask;
443}
444
445static int dtl_worker_enable(void)
446{
447 int rc = 0, state;
448
449 if (!write_trylock(&dtl_access_lock)) {
450 rc = -EBUSY;
451 goto out;
452 }
453
454 set_global_dtl_mask(DTL_LOG_ALL);
455
456 /* Setup dtl buffers and register those */
457 alloc_dtl_buffers();
458
459 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/dtl:online",
460 dtl_worker_online, dtl_worker_offline);
461 if (state < 0) {
462 pr_err("vcpudispatch_stats: unable to setup workqueue for DTL processing\n");
463 free_dtl_buffers();
464 reset_global_dtl_mask();
465 write_unlock(&dtl_access_lock);
466 rc = -EINVAL;
467 goto out;
468 }
469 dtl_worker_state = state;
470
471out:
472 return rc;
473}
474
475static void dtl_worker_disable(void)
476{
477 cpuhp_remove_state(dtl_worker_state);
478 free_dtl_buffers();
479 reset_global_dtl_mask();
480 write_unlock(&dtl_access_lock);
481}
482
483static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
484 size_t count, loff_t *ppos)
485{
486 struct vcpu_dispatch_data *disp;
487 int rc, cmd, cpu;
488 char buf[16];
489
490 if (count > 15)
491 return -EINVAL;
492
493 if (copy_from_user(buf, p, count))
494 return -EFAULT;
495
496 buf[count] = 0;
497 rc = kstrtoint(buf, 0, &cmd);
498 if (rc || cmd < 0 || cmd > 1) {
499 pr_err("vcpudispatch_stats: please use 0 to disable or 1 to enable dispatch statistics\n");
500 return rc ? rc : -EINVAL;
501 }
502
503 mutex_lock(&dtl_enable_mutex);
504
505 if ((cmd == 0 && !vcpudispatch_stats_on) ||
506 (cmd == 1 && vcpudispatch_stats_on))
507 goto out;
508
509 if (cmd) {
510 rc = init_cpu_associativity();
511 if (rc)
512 goto out;
513
514 for_each_possible_cpu(cpu) {
515 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
516 memset(disp, 0, sizeof(*disp));
517 disp->last_disp_cpu = -1;
518 }
519
520 rc = dtl_worker_enable();
521 if (rc) {
522 destroy_cpu_associativity();
523 goto out;
524 }
525 } else {
526 dtl_worker_disable();
527 destroy_cpu_associativity();
528 }
529
530 vcpudispatch_stats_on = cmd;
531
532out:
533 mutex_unlock(&dtl_enable_mutex);
534 if (rc)
535 return rc;
536 return count;
537}
538
539static int vcpudispatch_stats_display(struct seq_file *p, void *v)
540{
541 int cpu;
542 struct vcpu_dispatch_data *disp;
543
544 if (!vcpudispatch_stats_on) {
545 seq_puts(p, "off\n");
546 return 0;
547 }
548
549 for_each_online_cpu(cpu) {
550 disp = per_cpu_ptr(&vcpu_disp_data, cpu);
551 seq_printf(p, "cpu%d", cpu);
552 seq_put_decimal_ull(p, " ", disp->total_disp);
553 seq_put_decimal_ull(p, " ", disp->same_cpu_disp);
554 seq_put_decimal_ull(p, " ", disp->same_chip_disp);
555 seq_put_decimal_ull(p, " ", disp->diff_chip_disp);
556 seq_put_decimal_ull(p, " ", disp->far_chip_disp);
557 seq_put_decimal_ull(p, " ", disp->numa_home_disp);
558 seq_put_decimal_ull(p, " ", disp->numa_remote_disp);
559 seq_put_decimal_ull(p, " ", disp->numa_far_disp);
560 seq_puts(p, "\n");
561 }
562
563 return 0;
564}
565
566static int vcpudispatch_stats_open(struct inode *inode, struct file *file)
567{
568 return single_open(file, vcpudispatch_stats_display, NULL);
569}
570
571static const struct file_operations vcpudispatch_stats_proc_ops = {
572 .open = vcpudispatch_stats_open,
573 .read = seq_read,
574 .write = vcpudispatch_stats_write,
575 .llseek = seq_lseek,
576 .release = single_release,
577};
578
579static ssize_t vcpudispatch_stats_freq_write(struct file *file,
580 const char __user *p, size_t count, loff_t *ppos)
581{
582 int rc, freq;
583 char buf[16];
584
585 if (count > 15)
586 return -EINVAL;
587
588 if (copy_from_user(buf, p, count))
589 return -EFAULT;
590
591 buf[count] = 0;
592 rc = kstrtoint(buf, 0, &freq);
593 if (rc || freq < 1 || freq > HZ) {
594 pr_err("vcpudispatch_stats_freq: please specify a frequency between 1 and %d\n",
595 HZ);
596 return rc ? rc : -EINVAL;
597 }
598
599 vcpudispatch_stats_freq = freq;
600
601 return count;
602}
603
604static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
605{
606 seq_printf(p, "%d\n", vcpudispatch_stats_freq);
607 return 0;
608}
609
610static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)
611{
612 return single_open(file, vcpudispatch_stats_freq_display, NULL);
613}
614
615static const struct file_operations vcpudispatch_stats_freq_proc_ops = {
616 .open = vcpudispatch_stats_freq_open,
617 .read = seq_read,
618 .write = vcpudispatch_stats_freq_write,
619 .llseek = seq_lseek,
620 .release = single_release,
621};
622
623static int __init vcpudispatch_stats_procfs_init(void)
624{
625 if (!lppaca_shared_proc(get_lppaca()))
626 return 0;
627
628 if (!proc_create("powerpc/vcpudispatch_stats", 0600, NULL,
629 &vcpudispatch_stats_proc_ops))
630 pr_err("vcpudispatch_stats: error creating procfs file\n");
631 else if (!proc_create("powerpc/vcpudispatch_stats_freq", 0600, NULL,
632 &vcpudispatch_stats_freq_proc_ops))
633 pr_err("vcpudispatch_stats_freq: error creating procfs file\n");
634
635 return 0;
636}
637
638machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);
Naveen N. Rao06220d72019-07-03 22:33:57 +0530639#endif /* CONFIG_PPC_SPLPAR */
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641void vpa_init(int cpu)
642{
643 int hwcpu = get_hard_smp_processor_id(cpu);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000644 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 long ret;
Olof Johansson233ccd02005-09-03 15:55:59 -0700646
Michael Ellermanb89bdfb2013-08-15 15:22:15 +1000647 /*
648 * The spec says it "may be problematic" if CPU x registers the VPA of
649 * CPU y. We should never do that, but wail if we ever do.
650 */
651 WARN_ON(cpu != smp_processor_id());
652
Olof Johansson233ccd02005-09-03 15:55:59 -0700653 if (cpu_has_feature(CPU_FTR_ALTIVEC))
Paul Mackerras8154c5d2010-08-12 20:18:15 +0000654 lppaca_of(cpu).vmxregs_in_use = 1;
Olof Johansson233ccd02005-09-03 15:55:59 -0700655
Michael Ellerman6e0b8bc92013-06-28 18:15:18 +1000656 if (cpu_has_feature(CPU_FTR_ARCH_207S))
657 lppaca_of(cpu).ebb_regs_in_use = 1;
658
Paul Mackerras8154c5d2010-08-12 20:18:15 +0000659 addr = __pa(&lppaca_of(cpu));
Michael Neuling2f6093c2006-08-07 16:19:19 +1000660 ret = register_vpa(hwcpu, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Michael Neuling2f6093c2006-08-07 16:19:19 +1000662 if (ret) {
Anton Blanchard711ef842011-07-25 01:46:33 +0000663 pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "
664 "%lx failed with %ld\n", cpu, hwcpu, addr, ret);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000665 return;
666 }
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +1000667
Michael Ellerman4e003742017-10-19 15:08:43 +1100668#ifdef CONFIG_PPC_BOOK3S_64
Michael Neuling2f6093c2006-08-07 16:19:19 +1000669 /*
670 * PAPR says this feature is SLB-Buffer but firmware never
671 * reports that. All SPLPAR support SLB shadow buffer.
672 */
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +1000673 if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {
Nicholas Piggind2e60072018-02-14 01:08:12 +1000674 addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000675 ret = register_slb_shadow(hwcpu, addr);
676 if (ret)
Anton Blanchard711ef842011-07-25 01:46:33 +0000677 pr_err("WARNING: SLB shadow buffer registration for "
678 "cpu %d (hw %d) of area %lx failed with %ld\n",
679 cpu, hwcpu, addr, ret);
Michael Neuling2f6093c2006-08-07 16:19:19 +1000680 }
Michael Ellerman4e003742017-10-19 15:08:43 +1100681#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000682
683 /*
684 * Register dispatch trace log, if one has been allocated.
685 */
Naveen N. Rao1c85a2a12019-07-03 22:33:56 +0530686 register_dtl_buffer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Michael Ellerman4e003742017-10-19 15:08:43 +1100689#ifdef CONFIG_PPC_BOOK3S_64
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +1000690
Geoff Levand035223f2006-10-05 11:35:10 -0700691static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000692 unsigned long vpn, unsigned long pa,
693 unsigned long rflags, unsigned long vflags,
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000694 int psize, int apsize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 unsigned long lpar_rc;
697 unsigned long flags;
698 unsigned long slot;
David Gibson96e28442005-07-13 01:11:42 -0700699 unsigned long hpte_v, hpte_r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100701 if (!(vflags & HPTE_V_BOLTED))
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000702 pr_devel("hpte_insert(group=%lx, vpn=%016lx, "
703 "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",
704 hpte_group, vpn, pa, rflags, vflags, psize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +0000706 hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
Paul Mackerras6b243fc2016-11-11 16:55:03 +1100707 hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100708
709 if (!(vflags & HPTE_V_BOLTED))
Michael Ellerman551a2322009-06-17 18:13:50 +0000710 pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* Now fill in the actual HPTE */
713 /* Set CEC cookie to 0 */
714 /* Zero page = 0 */
715 /* I-cache Invalidate = 0 */
716 /* I-cache synchronize = 0 */
717 /* Exact = 0 */
718 flags = 0;
719
Brian King9ee820f2011-05-04 16:01:20 +1000720 if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))
721 flags |= H_COALESCE_CAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Anton Blanchardb9377ff2006-07-19 08:01:28 +1000723 lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200724 if (unlikely(lpar_rc == H_PTEG_FULL)) {
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530725 pr_devel("Hash table group is full\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return -1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /*
730 * Since we try and ioremap PHBs we don't own, the pte insert
731 * will fail. However we must catch the failure in hash_page
732 * or we will loop forever, so return -2 in this case.
733 */
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200734 if (unlikely(lpar_rc != H_SUCCESS)) {
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530735 pr_err("Failed hash pte insert with error %ld\n", lpar_rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return -2;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100737 }
738 if (!(vflags & HPTE_V_BOLTED))
Michael Ellerman551a2322009-06-17 18:13:50 +0000739 pr_devel(" -> slot: %lu\n", slot & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* Because of iSeries, we have to pass down the secondary
742 * bucket bit here as well
743 */
David Gibson96e28442005-07-13 01:11:42 -0700744 return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
748
749static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
750{
751 unsigned long slot_offset;
752 unsigned long lpar_rc;
753 int i;
754 unsigned long dummy1, dummy2;
755
756 /* pick a random slot to start at */
757 slot_offset = mftb() & 0x7;
758
759 for (i = 0; i < HPTES_PER_GROUP; i++) {
760
761 /* don't remove a bolted entry */
762 lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
763 (0x1UL << 4), &dummy1, &dummy2);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200764 if (lpar_rc == H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return i;
Michael Wolf9fb26402013-04-05 10:41:40 +0000766
767 /*
768 * The test for adjunct partition is performed before the
769 * ANDCOND test. H_RESOURCE may be returned, so we need to
770 * check for that as well.
771 */
772 BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 slot_offset++;
775 slot_offset &= 0x7;
776 }
777
778 return -1;
779}
780
Anton Blanchard5246ade2016-10-01 20:41:56 +1000781static void manual_hpte_clear_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 unsigned long size_bytes = 1UL << ppc64_pft_size;
784 unsigned long hpte_count = size_bytes >> 4;
Michael Neulingd504bed2010-05-10 20:28:26 +0000785 struct {
786 unsigned long pteh;
787 unsigned long ptel;
788 } ptes[4];
Sachin P. Santb7abc5c2007-06-14 15:31:34 +1000789 long lpar_rc;
Anton Blanchardbed9a312011-07-26 18:15:03 +0000790 unsigned long i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Michael Neulingd504bed2010-05-10 20:28:26 +0000792 /* Read in batches of 4,
793 * invalidate only valid entries not in the VRMA
794 * hpte_count will be a multiple of 4
795 */
796 for (i = 0; i < hpte_count; i += 4) {
797 lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530798 if (lpar_rc != H_SUCCESS) {
799 pr_info("Failed to read hash page table at %ld err %ld\n",
800 i, lpar_rc);
Michael Neulingd504bed2010-05-10 20:28:26 +0000801 continue;
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530802 }
Michael Neulingd504bed2010-05-10 20:28:26 +0000803 for (j = 0; j < 4; j++){
804 if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==
805 HPTE_V_VRMA_MASK)
806 continue;
807 if (ptes[j].pteh & HPTE_V_VALID)
808 plpar_pte_remove_raw(0, i + j, 0,
809 &(ptes[j].pteh), &(ptes[j].ptel));
Sachin P. Santb7abc5c2007-06-14 15:31:34 +1000810 }
811 }
Anton Blanchard5246ade2016-10-01 20:41:56 +1000812}
813
814static int hcall_hpte_clear_all(void)
815{
816 int rc;
817
818 do {
819 rc = plpar_hcall_norets(H_CLEAR_HPT);
820 } while (rc == H_CONTINUE);
821
822 return rc;
823}
824
825static void pseries_hpte_clear_all(void)
826{
827 int rc;
828
829 rc = hcall_hpte_clear_all();
830 if (rc != H_SUCCESS)
831 manual_hpte_clear_all();
Anton Blancharde844b1e2013-11-20 22:14:59 +1100832
833#ifdef __LITTLE_ENDIAN__
Hari Bathini408cddd2014-10-01 12:32:30 +0530834 /*
835 * Reset exceptions to big endian.
836 *
837 * FIXME this is a hack for kexec, we need to reset the exception
838 * endian before starting the new kernel and this is a convenient place
839 * to do it.
840 *
841 * This is also called on boot when a fadump happens. In that case we
842 * must not change the exception endian mode.
843 */
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000844 if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())
845 pseries_big_endian_exceptions();
Anton Blancharde844b1e2013-11-20 22:14:59 +1100846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
849/*
850 * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
851 * the low 3 bits of flags happen to line up. So no transform is needed.
852 * We can probably optimize here and assume the high bits of newpp are
853 * already zero. For now I am paranoid.
854 */
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100855static long pSeries_lpar_hpte_updatepp(unsigned long slot,
856 unsigned long newpp,
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000857 unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530858 int psize, int apsize,
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530859 int ssize, unsigned long inv_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 unsigned long lpar_rc;
Balbir Singhe71ff982017-06-29 03:04:07 +1000862 unsigned long flags;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100863 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000865 want_v = hpte_encode_avpn(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Balbir Singhe71ff982017-06-29 03:04:07 +1000867 flags = (newpp & 7) | H_AVPN;
868 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
869 /* Move pp0 into bit 8 (IBM 55) */
870 flags |= (newpp & HPTE_R_PP0) >> 55;
871
Alexey Kardashevskiya8c0bf32018-01-09 16:52:14 +1100872 pr_devel(" update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
873 want_v, slot, flags, psize);
874
Paul Mackerras1189be62007-10-11 20:37:10 +1000875 lpar_rc = plpar_pte_protect(flags, slot, want_v);
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100876
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200877 if (lpar_rc == H_NOT_FOUND) {
Michael Ellerman551a2322009-06-17 18:13:50 +0000878 pr_devel("not found !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -1;
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100880 }
881
Michael Ellerman551a2322009-06-17 18:13:50 +0000882 pr_devel("ok\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200884 BUG_ON(lpar_rc != H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 return 0;
887}
888
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530889static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530891 long lpar_rc;
892 unsigned long i, j;
893 struct {
894 unsigned long pteh;
895 unsigned long ptel;
896 } ptes[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530898 for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530900 lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530901 if (lpar_rc != H_SUCCESS) {
902 pr_info("Failed to read hash page table at %ld err %ld\n",
903 hpte_group, lpar_rc);
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530904 continue;
Aneesh Kumar K.Vca42d8d2d2018-06-29 14:09:04 +0530905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530907 for (j = 0; j < 4; j++) {
908 if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
909 (ptes[j].pteh & HPTE_V_VALID))
910 return i + j;
911 }
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530914 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000917static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 long slot;
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530920 unsigned long hash;
921 unsigned long want_v;
922 unsigned long hpte_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000924 hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
925 want_v = hpte_encode_avpn(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Paul Mackerras1189be62007-10-11 20:37:10 +1000927 /* Bolted entries are always in the primary group */
Aneesh Kumar K.V4ad90c82015-12-01 09:06:59 +0530928 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
929 slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
930 if (slot < 0)
931 return -1;
932 return hpte_group + slot;
933}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100936 unsigned long ea,
Paul Mackerras1189be62007-10-11 20:37:10 +1000937 int psize, int ssize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000939 unsigned long vpn;
940 unsigned long lpar_rc, slot, vsid, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Paul Mackerras1189be62007-10-11 20:37:10 +1000942 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000943 vpn = hpt_vpn(ea, vsid, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000945 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 BUG_ON(slot == -1);
947
948 flags = newpp & 7;
Balbir Singhe71ff982017-06-29 03:04:07 +1000949 if (mmu_has_feature(MMU_FTR_KERNEL_RO))
950 /* Move pp0 into bit 8 (IBM 55) */
951 flags |= (newpp & HPTE_R_PP0) >> 55;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 lpar_rc = plpar_pte_protect(flags, slot, 0);
954
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200955 BUG_ON(lpar_rc != H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000958static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +0530959 int psize, int apsize,
960 int ssize, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +1100962 unsigned long want_v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned long lpar_rc;
964 unsigned long dummy1, dummy2;
965
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000966 pr_devel(" inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
967 slot, vpn, psize, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +0000969 want_v = hpte_encode_avpn(vpn, psize, ssize);
Paul Mackerras1189be62007-10-11 20:37:10 +1000970 lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200971 if (lpar_rc == H_NOT_FOUND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 return;
973
Segher Boessenkool706c8c92006-03-30 14:49:40 +0200974 BUG_ON(lpar_rc != H_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Laurent Dufourba2dd8a2018-08-20 16:29:36 +0200977
978/*
979 * As defined in the PAPR's section 14.5.4.1.8
980 * The control mask doesn't include the returned reference and change bit from
981 * the processed PTE.
982 */
983#define HBLKR_AVPN 0x0100000000000000UL
984#define HBLKR_CTRL_MASK 0xf800000000000000UL
985#define HBLKR_CTRL_SUCCESS 0x8000000000000000UL
986#define HBLKR_CTRL_ERRNOTFOUND 0x8800000000000000UL
987#define HBLKR_CTRL_ERRBUSY 0xa000000000000000UL
988
989/**
990 * H_BLOCK_REMOVE caller.
991 * @idx should point to the latest @param entry set with a PTEX.
992 * If PTE cannot be processed because another CPUs has already locked that
993 * group, those entries are put back in @param starting at index 1.
994 * If entries has to be retried and @retry_busy is set to true, these entries
995 * are retried until success. If @retry_busy is set to false, the returned
996 * is the number of entries yet to process.
997 */
998static unsigned long call_block_remove(unsigned long idx, unsigned long *param,
999 bool retry_busy)
1000{
1001 unsigned long i, rc, new_idx;
1002 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1003
1004 if (idx < 2) {
1005 pr_warn("Unexpected empty call to H_BLOCK_REMOVE");
1006 return 0;
1007 }
1008again:
1009 new_idx = 0;
1010 if (idx > PLPAR_HCALL9_BUFSIZE) {
1011 pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx);
1012 idx = PLPAR_HCALL9_BUFSIZE;
1013 } else if (idx < PLPAR_HCALL9_BUFSIZE)
1014 param[idx] = HBR_END;
1015
1016 rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf,
1017 param[0], /* AVA */
1018 param[1], param[2], param[3], param[4], /* TS0-7 */
1019 param[5], param[6], param[7], param[8]);
1020 if (rc == H_SUCCESS)
1021 return 0;
1022
1023 BUG_ON(rc != H_PARTIAL);
1024
1025 /* Check that the unprocessed entries were 'not found' or 'busy' */
1026 for (i = 0; i < idx-1; i++) {
1027 unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK;
1028
1029 if (ctrl == HBLKR_CTRL_ERRBUSY) {
1030 param[++new_idx] = param[i+1];
1031 continue;
1032 }
1033
1034 BUG_ON(ctrl != HBLKR_CTRL_SUCCESS
1035 && ctrl != HBLKR_CTRL_ERRNOTFOUND);
1036 }
1037
1038 /*
1039 * If there were entries found busy, retry these entries if requested,
1040 * of if all the entries have to be retried.
1041 */
1042 if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) {
1043 idx = new_idx + 1;
1044 goto again;
1045 }
1046
1047 return new_idx;
1048}
1049
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +05301050#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301051/*
1052 * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
1053 * to make sure that we avoid bouncing the hypervisor tlbie lock.
1054 */
1055#define PPC64_HUGE_HPTE_BATCH 12
1056
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001057static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn,
1058 int count, int psize, int ssize)
1059{
1060 unsigned long param[PLPAR_HCALL9_BUFSIZE];
1061 unsigned long shift, current_vpgb, vpgb;
1062 int i, pix = 0;
1063
1064 shift = mmu_psize_defs[psize].shift;
1065
1066 for (i = 0; i < count; i++) {
1067 /*
1068 * Shifting 3 bits more on the right to get a
1069 * 8 pages aligned virtual addresse.
1070 */
1071 vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3));
1072 if (!pix || vpgb != current_vpgb) {
1073 /*
1074 * Need to start a new 8 pages block, flush
1075 * the current one if needed.
1076 */
1077 if (pix)
1078 (void)call_block_remove(pix, param, true);
1079 current_vpgb = vpgb;
1080 param[0] = hpte_encode_avpn(vpn[i], psize, ssize);
1081 pix = 1;
1082 }
1083
1084 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i];
1085 if (pix == PLPAR_HCALL9_BUFSIZE) {
1086 pix = call_block_remove(pix, param, false);
1087 /*
1088 * pix = 0 means that all the entries were
1089 * removed, we can start a new block.
1090 * Otherwise, this means that there are entries
1091 * to retry, and pix points to latest one, so
1092 * we should increment it and try to continue
1093 * the same block.
1094 */
1095 if (pix)
1096 pix++;
1097 }
1098 }
1099 if (pix)
1100 (void)call_block_remove(pix, param, true);
1101}
1102
1103static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
1104 int count, int psize, int ssize)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301105{
Laurent Dufour05af40e2016-10-06 15:33:21 +02001106 unsigned long param[PLPAR_HCALL9_BUFSIZE];
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301107 int i = 0, pix = 0, rc;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301108
1109 for (i = 0; i < count; i++) {
1110
1111 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1112 pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
1113 ssize, 0);
1114 } else {
1115 param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
1116 param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
1117 pix += 2;
1118 if (pix == 8) {
1119 rc = plpar_hcall9(H_BULK_REMOVE, param,
1120 param[0], param[1], param[2],
1121 param[3], param[4], param[5],
1122 param[6], param[7]);
1123 BUG_ON(rc != H_SUCCESS);
1124 pix = 0;
1125 }
1126 }
1127 }
1128 if (pix) {
1129 param[pix] = HBR_END;
1130 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1131 param[2], param[3], param[4], param[5],
1132 param[6], param[7]);
1133 BUG_ON(rc != H_SUCCESS);
1134 }
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001135}
1136
1137static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
1138 unsigned long *vpn,
1139 int count, int psize,
1140 int ssize)
1141{
1142 unsigned long flags = 0;
1143 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1144
1145 if (lock_tlbie)
1146 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1147
1148 if (firmware_has_feature(FW_FEATURE_BLOCK_REMOVE))
1149 hugepage_block_invalidate(slot, vpn, count, psize, ssize);
1150 else
1151 hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301152
1153 if (lock_tlbie)
1154 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1155}
1156
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +05301157static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1158 unsigned long addr,
1159 unsigned char *hpte_slot_array,
Aneesh Kumar K.Vd557b092014-11-02 21:15:28 +05301160 int psize, int ssize, int local)
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301161{
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +05301162 int i, index = 0;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301163 unsigned long s_addr = addr;
1164 unsigned int max_hpte_count, valid;
1165 unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
1166 unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
Aneesh Kumar K.Vfa1f8ae2014-08-13 12:31:58 +05301167 unsigned long shift, hidx, vpn = 0, hash, slot;
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301168
1169 shift = mmu_psize_defs[psize].shift;
1170 max_hpte_count = 1U << (PMD_SHIFT - shift);
1171
1172 for (i = 0; i < max_hpte_count; i++) {
1173 valid = hpte_valid(hpte_slot_array, i);
1174 if (!valid)
1175 continue;
1176 hidx = hpte_hash_index(hpte_slot_array, i);
1177
1178 /* get the vpn */
1179 addr = s_addr + (i * (1ul << shift));
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301180 vpn = hpt_vpn(addr, vsid, ssize);
1181 hash = hpt_hash(vpn, shift, ssize);
1182 if (hidx & _PTEIDX_SECONDARY)
1183 hash = ~hash;
1184
1185 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1186 slot += hidx & _PTEIDX_GROUP_IX;
1187
1188 slot_array[index] = slot;
1189 vpn_array[index] = vpn;
1190 if (index == PPC64_HUGE_HPTE_BATCH - 1) {
1191 /*
1192 * Now do a bluk invalidate
1193 */
1194 __pSeries_lpar_hugepage_invalidate(slot_array,
1195 vpn_array,
1196 PPC64_HUGE_HPTE_BATCH,
1197 psize, ssize);
1198 index = 0;
1199 } else
1200 index++;
1201 }
1202 if (index)
1203 __pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
1204 index, psize, ssize);
1205}
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +05301206#else
1207static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1208 unsigned long addr,
1209 unsigned char *hpte_slot_array,
1210 int psize, int ssize, int local)
1211{
1212 WARN(1, "%s called without THP support\n", __func__);
1213}
1214#endif
Aneesh Kumar K.V1a527282013-06-20 14:30:27 +05301215
David Gibson27828f92016-02-09 13:32:41 +10001216static int pSeries_lpar_hpte_removebolted(unsigned long ea,
1217 int psize, int ssize)
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001218{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001219 unsigned long vpn;
1220 unsigned long slot, vsid;
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001221
1222 vsid = get_kernel_vsid(ea, ssize);
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001223 vpn = hpt_vpn(ea, vsid, ssize);
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001224
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001225 slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
David Gibson27828f92016-02-09 13:32:41 +10001226 if (slot == -1)
1227 return -ENOENT;
1228
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301229 /*
1230 * lpar doesn't use the passed actual page size
1231 */
1232 pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
David Gibson27828f92016-02-09 13:32:41 +10001233 return 0;
Badari Pulavartyf8c88032008-01-29 09:19:24 +11001234}
1235
Laurent Dufour0effa482018-08-20 16:29:35 +02001236
1237static inline unsigned long compute_slot(real_pte_t pte,
1238 unsigned long vpn,
1239 unsigned long index,
1240 unsigned long shift,
1241 int ssize)
1242{
1243 unsigned long slot, hash, hidx;
1244
1245 hash = hpt_hash(vpn, shift, ssize);
1246 hidx = __rpte_to_hidx(pte, index);
1247 if (hidx & _PTEIDX_SECONDARY)
1248 hash = ~hash;
1249 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1250 slot += hidx & _PTEIDX_GROUP_IX;
1251 return slot;
1252}
1253
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001254/**
1255 * The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are
1256 * "all within the same naturally aligned 8 page virtual address block".
1257 */
1258static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch,
1259 unsigned long *param)
1260{
1261 unsigned long vpn;
1262 unsigned long i, pix = 0;
1263 unsigned long index, shift, slot, current_vpgb, vpgb;
1264 real_pte_t pte;
1265 int psize, ssize;
1266
1267 psize = batch->psize;
1268 ssize = batch->ssize;
1269
1270 for (i = 0; i < number; i++) {
1271 vpn = batch->vpn[i];
1272 pte = batch->pte[i];
1273 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1274 /*
1275 * Shifting 3 bits more on the right to get a
1276 * 8 pages aligned virtual addresse.
1277 */
1278 vpgb = (vpn >> (shift - VPN_SHIFT + 3));
1279 if (!pix || vpgb != current_vpgb) {
1280 /*
1281 * Need to start a new 8 pages block, flush
1282 * the current one if needed.
1283 */
1284 if (pix)
1285 (void)call_block_remove(pix, param,
1286 true);
1287 current_vpgb = vpgb;
1288 param[0] = hpte_encode_avpn(vpn, psize,
1289 ssize);
1290 pix = 1;
1291 }
1292
1293 slot = compute_slot(pte, vpn, index, shift, ssize);
1294 param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot;
1295
1296 if (pix == PLPAR_HCALL9_BUFSIZE) {
1297 pix = call_block_remove(pix, param, false);
1298 /*
1299 * pix = 0 means that all the entries were
1300 * removed, we can start a new block.
1301 * Otherwise, this means that there are entries
1302 * to retry, and pix points to latest one, so
1303 * we should increment it and try to continue
1304 * the same block.
1305 */
1306 if (pix)
1307 pix++;
1308 }
1309 } pte_iterate_hashed_end();
1310 }
1311
1312 if (pix)
1313 (void)call_block_remove(pix, param, true);
1314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/*
1317 * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
1318 * lock.
1319 */
Geoff Levand035223f2006-10-05 11:35:10 -07001320static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001322 unsigned long vpn;
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001323 unsigned long i, pix, rc;
Paul Mackerras12e86f92007-02-08 15:02:35 +11001324 unsigned long flags = 0;
Christoph Lameter69111ba2014-10-21 15:23:25 -05001325 struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
Matt Evans44ae3ab2011-04-06 19:48:50 +00001326 int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
Laurent Dufour05af40e2016-10-06 15:33:21 +02001327 unsigned long param[PLPAR_HCALL9_BUFSIZE];
Laurent Dufour0effa482018-08-20 16:29:35 +02001328 unsigned long index, shift, slot;
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001329 real_pte_t pte;
Paul Mackerras1189be62007-10-11 20:37:10 +10001330 int psize, ssize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 if (lock_tlbie)
1333 spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1334
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001335 if (firmware_has_feature(FW_FEATURE_BLOCK_REMOVE)) {
1336 do_block_remove(number, batch, param);
1337 goto out;
1338 }
1339
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001340 psize = batch->psize;
Paul Mackerras1189be62007-10-11 20:37:10 +10001341 ssize = batch->ssize;
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001342 pix = 0;
1343 for (i = 0; i < number; i++) {
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001344 vpn = batch->vpn[i];
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001345 pte = batch->pte[i];
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001346 pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
Laurent Dufour0effa482018-08-20 16:29:35 +02001347 slot = compute_slot(pte, vpn, index, shift, ssize);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001348 if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301349 /*
1350 * lpar doesn't use the passed actual page size
1351 */
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001352 pSeries_lpar_hpte_invalidate(slot, vpn, psize,
Aneesh Kumar K.Vdb3d8532013-06-20 14:30:13 +05301353 0, ssize, local);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001354 } else {
1355 param[pix] = HBR_REQUEST | HBR_AVPN | slot;
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +00001356 param[pix+1] = hpte_encode_avpn(vpn, psize,
Paul Mackerras1189be62007-10-11 20:37:10 +10001357 ssize);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001358 pix += 2;
1359 if (pix == 8) {
1360 rc = plpar_hcall9(H_BULK_REMOVE, param,
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001361 param[0], param[1], param[2],
1362 param[3], param[4], param[5],
1363 param[6], param[7]);
Paul Mackerras12e86f92007-02-08 15:02:35 +11001364 BUG_ON(rc != H_SUCCESS);
1365 pix = 0;
1366 }
Paul Mackerrasf03e64f2007-02-06 21:10:31 +11001367 }
1368 } pte_iterate_hashed_end();
1369 }
1370 if (pix) {
1371 param[pix] = HBR_END;
1372 rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1373 param[2], param[3], param[4], param[5],
1374 param[6], param[7]);
1375 BUG_ON(rc != H_SUCCESS);
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Laurent Dufourba2dd8a2018-08-20 16:29:36 +02001378out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (lock_tlbie)
1380 spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1381}
1382
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001383static int __init disable_bulk_remove(char *str)
1384{
1385 if (strcmp(str, "off") == 0 &&
1386 firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301387 pr_info("Disabling BULK_REMOVE firmware feature");
1388 powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
Will Schmidt4e89a2d2010-09-28 15:33:12 +00001389 }
1390 return 1;
1391}
1392
1393__setup("bulk_remove=", disable_bulk_remove);
1394
David Gibsondbcf9292016-12-09 11:07:36 +11001395#define HPT_RESIZE_TIMEOUT 10000 /* ms */
1396
1397struct hpt_resize_state {
1398 unsigned long shift;
1399 int commit_rc;
1400};
1401
1402static int pseries_lpar_resize_hpt_commit(void *data)
1403{
1404 struct hpt_resize_state *state = data;
1405
1406 state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
1407 if (state->commit_rc != H_SUCCESS)
1408 return -EIO;
1409
1410 /* Hypervisor has transitioned the HTAB, update our globals */
1411 ppc64_pft_size = state->shift;
1412 htab_size_bytes = 1UL << ppc64_pft_size;
1413 htab_hash_mask = (htab_size_bytes >> 7) - 1;
1414
1415 return 0;
1416}
1417
1418/* Must be called in user context */
1419static int pseries_lpar_resize_hpt(unsigned long shift)
1420{
1421 struct hpt_resize_state state = {
1422 .shift = shift,
1423 .commit_rc = H_FUNCTION,
1424 };
1425 unsigned int delay, total_delay = 0;
1426 int rc;
1427 ktime_t t0, t1, t2;
1428
1429 might_sleep();
1430
1431 if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1432 return -ENODEV;
1433
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301434 pr_info("Attempting to resize HPT to shift %lu\n", shift);
David Gibsondbcf9292016-12-09 11:07:36 +11001435
1436 t0 = ktime_get();
1437
1438 rc = plpar_resize_hpt_prepare(0, shift);
1439 while (H_IS_LONG_BUSY(rc)) {
1440 delay = get_longbusy_msecs(rc);
1441 total_delay += delay;
1442 if (total_delay > HPT_RESIZE_TIMEOUT) {
1443 /* prepare with shift==0 cancels an in-progress resize */
1444 rc = plpar_resize_hpt_prepare(0, 0);
1445 if (rc != H_SUCCESS)
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301446 pr_warn("Unexpected error %d cancelling timed out HPT resize\n",
David Gibsondbcf9292016-12-09 11:07:36 +11001447 rc);
1448 return -ETIMEDOUT;
1449 }
1450 msleep(delay);
1451 rc = plpar_resize_hpt_prepare(0, shift);
1452 };
1453
1454 switch (rc) {
1455 case H_SUCCESS:
1456 /* Continue on */
1457 break;
1458
1459 case H_PARAMETER:
Laurent Vivierf172acb2019-03-13 11:25:28 +01001460 pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n");
David Gibsondbcf9292016-12-09 11:07:36 +11001461 return -EINVAL;
1462 case H_RESOURCE:
Laurent Vivierf172acb2019-03-13 11:25:28 +01001463 pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n");
David Gibsondbcf9292016-12-09 11:07:36 +11001464 return -EPERM;
1465 default:
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301466 pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc);
David Gibsondbcf9292016-12-09 11:07:36 +11001467 return -EIO;
1468 }
1469
1470 t1 = ktime_get();
1471
1472 rc = stop_machine(pseries_lpar_resize_hpt_commit, &state, NULL);
1473
1474 t2 = ktime_get();
1475
1476 if (rc != 0) {
1477 switch (state.commit_rc) {
1478 case H_PTEG_FULL:
David Gibsondbcf9292016-12-09 11:07:36 +11001479 return -ENOSPC;
1480
1481 default:
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301482 pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
1483 state.commit_rc);
David Gibsondbcf9292016-12-09 11:07:36 +11001484 return -EIO;
1485 };
1486 }
1487
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301488 pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
1489 shift, (long long) ktime_ms_delta(t1, t0),
1490 (long long) ktime_ms_delta(t2, t1));
David Gibsondbcf9292016-12-09 11:07:36 +11001491
1492 return 0;
1493}
1494
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001495static int pseries_lpar_register_process_table(unsigned long base,
1496 unsigned long page_size, unsigned long table_size)
1497{
1498 long rc;
Paul Mackerrasdbfcf3c2017-02-16 16:03:39 +11001499 unsigned long flags = 0;
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001500
Paul Mackerrasdbfcf3c2017-02-16 16:03:39 +11001501 if (table_size)
1502 flags |= PROC_TABLE_NEW;
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001503 if (radix_enabled())
1504 flags |= PROC_TABLE_RADIX | PROC_TABLE_GTSE;
Paul Mackerrasdbfcf3c2017-02-16 16:03:39 +11001505 else
1506 flags |= PROC_TABLE_HPT_SLB;
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001507 for (;;) {
1508 rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
1509 page_size, table_size);
1510 if (!H_IS_LONG_BUSY(rc))
1511 break;
1512 mdelay(get_longbusy_msecs(rc));
1513 }
1514 if (rc != H_SUCCESS) {
1515 pr_err("Failed to register process table (rc=%ld)\n", rc);
1516 BUG();
1517 }
1518 return rc;
1519}
1520
Michael Ellerman6364e842016-07-26 10:33:03 +10001521void __init hpte_init_pseries(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001523 mmu_hash_ops.hpte_invalidate = pSeries_lpar_hpte_invalidate;
1524 mmu_hash_ops.hpte_updatepp = pSeries_lpar_hpte_updatepp;
1525 mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
1526 mmu_hash_ops.hpte_insert = pSeries_lpar_hpte_insert;
1527 mmu_hash_ops.hpte_remove = pSeries_lpar_hpte_remove;
1528 mmu_hash_ops.hpte_removebolted = pSeries_lpar_hpte_removebolted;
1529 mmu_hash_ops.flush_hash_range = pSeries_lpar_flush_hash_range;
Anton Blanchard5246ade2016-10-01 20:41:56 +10001530 mmu_hash_ops.hpte_clear_all = pseries_hpte_clear_all;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +10001531 mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
Paul Mackerrasdbfcf3c2017-02-16 16:03:39 +11001532 register_process_table = pseries_lpar_register_process_table;
Michael Ellerman8971e1c2017-03-17 16:02:35 +11001533
1534 if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1535 mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
Robert Jennings14f966e2009-04-15 05:55:32 +00001537
Paul Mackerrascc3d2942017-01-30 21:21:36 +11001538void radix_init_pseries(void)
1539{
1540 pr_info("Using radix MMU under hypervisor\n");
1541 register_process_table = pseries_lpar_register_process_table;
1542}
1543
Robert Jennings14f966e2009-04-15 05:55:32 +00001544#ifdef CONFIG_PPC_SMLPAR
1545#define CMO_FREE_HINT_DEFAULT 1
1546static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
1547
1548static int __init cmo_free_hint(char *str)
1549{
1550 char *parm;
1551 parm = strstrip(str);
1552
1553 if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301554 pr_info("%s: CMO free page hinting is not active.\n", __func__);
Robert Jennings14f966e2009-04-15 05:55:32 +00001555 cmo_free_hint_flag = 0;
1556 return 1;
1557 }
1558
1559 cmo_free_hint_flag = 1;
Aneesh Kumar K.V65471d72018-06-29 14:09:04 +05301560 pr_info("%s: CMO free page hinting is active.\n", __func__);
Robert Jennings14f966e2009-04-15 05:55:32 +00001561
1562 if (strcasecmp(parm, "yes") == 0 || strcasecmp(parm, "on") == 0)
1563 return 1;
1564
1565 return 0;
1566}
1567
1568__setup("cmo_free_hint=", cmo_free_hint);
1569
1570static void pSeries_set_page_state(struct page *page, int order,
1571 unsigned long state)
1572{
1573 int i, j;
1574 unsigned long cmo_page_sz, addr;
1575
1576 cmo_page_sz = cmo_get_page_size();
1577 addr = __pa((unsigned long)page_address(page));
1578
1579 for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {
1580 for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)
1581 plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);
1582 }
1583}
1584
1585void arch_free_page(struct page *page, int order)
1586{
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +10001587 if (radix_enabled())
1588 return;
Robert Jennings14f966e2009-04-15 05:55:32 +00001589 if (!cmo_free_hint_flag || !firmware_has_feature(FW_FEATURE_CMO))
1590 return;
1591
1592 pSeries_set_page_state(page, order, H_PAGE_SET_UNUSED);
1593}
1594EXPORT_SYMBOL(arch_free_page);
1595
Aneesh Kumar K.Vd8c476e2016-04-29 23:26:08 +10001596#endif /* CONFIG_PPC_SMLPAR */
Michael Ellerman4e003742017-10-19 15:08:43 +11001597#endif /* CONFIG_PPC_BOOK3S_64 */
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001598
1599#ifdef CONFIG_TRACEPOINTS
Masahiro Yamadae9666d12018-12-31 00:14:15 +09001600#ifdef CONFIG_JUMP_LABEL
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001601struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
1602
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001603int hcall_tracepoint_regfunc(void)
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001604{
1605 static_key_slow_inc(&hcall_tracepoint_key);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001606 return 0;
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001607}
1608
1609void hcall_tracepoint_unregfunc(void)
1610{
1611 static_key_slow_dec(&hcall_tracepoint_key);
1612}
1613#else
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001614/*
1615 * We optimise our hcall path by placing hcall_tracepoint_refcount
1616 * directly in the TOC so we can check if the hcall tracepoints are
1617 * enabled via a single load.
1618 */
1619
1620/* NB: reg/unreg are called while guarded with the tracepoints_mutex */
1621extern long hcall_tracepoint_refcount;
1622
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001623int hcall_tracepoint_regfunc(void)
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001624{
1625 hcall_tracepoint_refcount++;
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -05001626 return 0;
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001627}
1628
1629void hcall_tracepoint_unregfunc(void)
1630{
1631 hcall_tracepoint_refcount--;
1632}
Anton Blanchardcc1adb52014-07-03 15:52:03 +10001633#endif
1634
1635/*
1636 * Since the tracing code might execute hcalls we need to guard against
1637 * recursion. One example of this are spinlocks calling H_YIELD on
1638 * shared processor partitions.
1639 */
1640static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
1641
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001642
Anton Blanchard6f263532009-10-26 18:51:09 +00001643void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001644{
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001645 unsigned long flags;
1646 unsigned int *depth;
1647
Anton Blancharda5ccfee2012-01-09 14:29:15 +00001648 /*
1649 * We cannot call tracepoints inside RCU idle regions which
1650 * means we must not trace H_CEDE.
1651 */
1652 if (opcode == H_CEDE)
1653 return;
1654
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001655 local_irq_save(flags);
1656
Christoph Lameter69111ba2014-10-21 15:23:25 -05001657 depth = this_cpu_ptr(&hcall_trace_depth);
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001658
1659 if (*depth)
1660 goto out;
1661
1662 (*depth)++;
Li Zhonge4f387d2011-12-18 16:03:04 +00001663 preempt_disable();
Anton Blanchard6f263532009-10-26 18:51:09 +00001664 trace_hcall_entry(opcode, args);
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001665 (*depth)--;
1666
1667out:
1668 local_irq_restore(flags);
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001669}
1670
Michael Ellerman8f2133c2018-05-07 23:03:55 +10001671void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf)
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001672{
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001673 unsigned long flags;
1674 unsigned int *depth;
1675
Anton Blancharda5ccfee2012-01-09 14:29:15 +00001676 if (opcode == H_CEDE)
1677 return;
1678
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001679 local_irq_save(flags);
1680
Christoph Lameter69111ba2014-10-21 15:23:25 -05001681 depth = this_cpu_ptr(&hcall_trace_depth);
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001682
1683 if (*depth)
1684 goto out;
1685
1686 (*depth)++;
Anton Blanchard6f263532009-10-26 18:51:09 +00001687 trace_hcall_exit(opcode, retval, retbuf);
Li Zhonge4f387d2011-12-18 16:03:04 +00001688 preempt_enable();
Anton Blanchard57cdfdf2010-10-21 00:52:12 +00001689 (*depth)--;
1690
1691out:
1692 local_irq_restore(flags);
Anton Blanchardc8cd0932009-10-26 18:50:29 +00001693}
1694#endif
Brian King9ee820f2011-05-04 16:01:20 +10001695
1696/**
1697 * h_get_mpp
1698 * H_GET_MPP hcall returns info in 7 parms
1699 */
1700int h_get_mpp(struct hvcall_mpp_data *mpp_data)
1701{
1702 int rc;
1703 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1704
1705 rc = plpar_hcall9(H_GET_MPP, retbuf);
1706
1707 mpp_data->entitled_mem = retbuf[0];
1708 mpp_data->mapped_mem = retbuf[1];
1709
1710 mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
1711 mpp_data->pool_num = retbuf[2] & 0xffff;
1712
1713 mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
1714 mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
Anton Blanchardb0d436c2013-08-07 02:01:24 +10001715 mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
Brian King9ee820f2011-05-04 16:01:20 +10001716
1717 mpp_data->pool_size = retbuf[4];
1718 mpp_data->loan_request = retbuf[5];
1719 mpp_data->backing_mem = retbuf[6];
1720
1721 return rc;
1722}
1723EXPORT_SYMBOL(h_get_mpp);
1724
1725int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
1726{
1727 int rc;
1728 unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
1729
1730 rc = plpar_hcall9(H_GET_MPP_X, retbuf);
1731
1732 mpp_x_data->coalesced_bytes = retbuf[0];
1733 mpp_x_data->pool_coalesced_bytes = retbuf[1];
1734 mpp_x_data->pool_purr_cycles = retbuf[2];
1735 mpp_x_data->pool_spurr_cycles = retbuf[3];
1736
1737 return rc;
1738}
Aneesh Kumar K.V82228e32017-03-22 09:07:00 +05301739
1740static unsigned long vsid_unscramble(unsigned long vsid, int ssize)
1741{
1742 unsigned long protovsid;
1743 unsigned long va_bits = VA_BITS;
1744 unsigned long modinv, vsid_modulus;
1745 unsigned long max_mod_inv, tmp_modinv;
1746
1747 if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
1748 va_bits = 65;
1749
1750 if (ssize == MMU_SEGSIZE_256M) {
1751 modinv = VSID_MULINV_256M;
1752 vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);
1753 } else {
1754 modinv = VSID_MULINV_1T;
1755 vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);
1756 }
1757
1758 /*
1759 * vsid outside our range.
1760 */
1761 if (vsid >= vsid_modulus)
1762 return 0;
1763
1764 /*
1765 * If modinv is the modular multiplicate inverse of (x % vsid_modulus)
1766 * and vsid = (protovsid * x) % vsid_modulus, then we say:
1767 * protovsid = (vsid * modinv) % vsid_modulus
1768 */
1769
1770 /* Check if (vsid * modinv) overflow (63 bits) */
1771 max_mod_inv = 0x7fffffffffffffffull / vsid;
1772 if (modinv < max_mod_inv)
1773 return (vsid * modinv) % vsid_modulus;
1774
1775 tmp_modinv = modinv/max_mod_inv;
1776 modinv %= max_mod_inv;
1777
1778 protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;
1779 protovsid = (protovsid + vsid * modinv) % vsid_modulus;
1780
1781 return protovsid;
1782}
1783
1784static int __init reserve_vrma_context_id(void)
1785{
1786 unsigned long protovsid;
1787
1788 /*
1789 * Reserve context ids which map to reserved virtual addresses. For now
1790 * we only reserve the context id which maps to the VRMA VSID. We ignore
1791 * the addresses in "ibm,adjunct-virtual-addresses" because we don't
1792 * enable adjunct support via the "ibm,client-architecture-support"
1793 * interface.
1794 */
1795 protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);
1796 hash__reserve_context_id(protovsid >> ESID_BITS_1T);
1797 return 0;
1798}
1799machine_device_initcall(pseries, reserve_vrma_context_id);
Aravinda Prasadc6c26fb2018-10-16 17:20:05 +05301800
1801#ifdef CONFIG_DEBUG_FS
1802/* debugfs file interface for vpa data */
1803static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,
1804 loff_t *pos)
1805{
1806 int cpu = (long)filp->private_data;
1807 struct lppaca *lppaca = &lppaca_of(cpu);
1808
1809 return simple_read_from_buffer(buf, len, pos, lppaca,
1810 sizeof(struct lppaca));
1811}
1812
1813static const struct file_operations vpa_fops = {
1814 .open = simple_open,
1815 .read = vpa_file_read,
1816 .llseek = default_llseek,
1817};
1818
1819static int __init vpa_debugfs_init(void)
1820{
1821 char name[16];
1822 long i;
1823 static struct dentry *vpa_dir;
1824
1825 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
1826 return 0;
1827
1828 vpa_dir = debugfs_create_dir("vpa", powerpc_debugfs_root);
1829 if (!vpa_dir) {
1830 pr_warn("%s: can't create vpa root dir\n", __func__);
1831 return -ENOMEM;
1832 }
1833
1834 /* set up the per-cpu vpa file*/
1835 for_each_possible_cpu(i) {
1836 struct dentry *d;
1837
1838 sprintf(name, "cpu-%ld", i);
1839
1840 d = debugfs_create_file(name, 0400, vpa_dir, (void *)i,
1841 &vpa_fops);
1842 if (!d) {
1843 pr_warn("%s: can't create per-cpu vpa file\n",
1844 __func__);
1845 return -ENOMEM;
1846 }
1847 }
1848
1849 return 0;
1850}
1851machine_arch_initcall(pseries, vpa_debugfs_init);
1852#endif /* CONFIG_DEBUG_FS */