1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * pSeries_lpar.c
4  * Copyright (C) 2001 Todd Inglett, IBM Corporation
5  *
6  * pSeries LPAR support.
7  */
8 
9 /* Enables debugging of low-level hash table routines - careful! */
10 #undef DEBUG
11 #define pr_fmt(fmt) "lpar: " fmt
12 
13 #include <linux/kernel.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/console.h>
16 #include <linux/export.h>
17 #include <linux/jump_label.h>
18 #include <linux/delay.h>
19 #include <linux/stop_machine.h>
20 #include <linux/spinlock.h>
21 #include <linux/cpuhotplug.h>
22 #include <linux/workqueue.h>
23 #include <linux/proc_fs.h>
24 #include <linux/pgtable.h>
25 #include <linux/debugfs.h>
26 
27 #include <asm/processor.h>
28 #include <asm/mmu.h>
29 #include <asm/page.h>
30 #include <asm/setup.h>
31 #include <asm/mmu_context.h>
32 #include <asm/iommu.h>
33 #include <asm/tlb.h>
34 #include <asm/cputable.h>
35 #include <asm/papr-sysparm.h>
36 #include <asm/udbg.h>
37 #include <asm/smp.h>
38 #include <asm/trace.h>
39 #include <asm/firmware.h>
40 #include <asm/plpar_wrappers.h>
41 #include <asm/kexec.h>
42 #include <asm/fadump.h>
43 #include <asm/dtl.h>
44 
45 #include "pseries.h"
46 
47 /* 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 
54 
55 /* in hvCall.S */
56 EXPORT_SYMBOL(plpar_hcall);
57 EXPORT_SYMBOL(plpar_hcall9);
58 EXPORT_SYMBOL(plpar_hcall_norets);
59 
60 #ifdef CONFIG_PPC_64S_HASH_MMU
61 /*
62  * H_BLOCK_REMOVE supported block size for this page size in segment who's base
63  * page size is that page size.
64  *
65  * The first index is the segment base page size, the second one is the actual
66  * page size.
67  */
68 static int hblkrm_size[MMU_PAGE_COUNT][MMU_PAGE_COUNT] __ro_after_init;
69 #endif
70 
71 /*
72  * Due to the involved complexity, and that the current hypervisor is only
73  * returning this value or 0, we are limiting the support of the H_BLOCK_REMOVE
74  * buffer size to 8 size block.
75  */
76 #define HBLKRM_SUPPORTED_BLOCK_SIZE 8
77 
78 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
79 static u8 dtl_mask = DTL_LOG_PREEMPT;
80 #else
81 static u8 dtl_mask;
82 #endif
83 
84 void alloc_dtl_buffers(unsigned long *time_limit)
85 {
86 	int cpu;
87 	struct paca_struct *pp;
88 	struct dtl_entry *dtl;
89 
90 	for_each_possible_cpu(cpu) {
91 		pp = paca_ptrs[cpu];
92 		if (pp->dispatch_log)
93 			continue;
94 		dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
95 		if (!dtl) {
96 			pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
97 				cpu);
98 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
99 			pr_warn("Stolen time statistics will be unreliable\n");
100 #endif
101 			break;
102 		}
103 
104 		pp->dtl_ridx = 0;
105 		pp->dispatch_log = dtl;
106 		pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
107 		pp->dtl_curr = dtl;
108 
109 		if (time_limit && time_after(jiffies, *time_limit)) {
110 			cond_resched();
111 			*time_limit = jiffies + HZ;
112 		}
113 	}
114 }
115 
116 void register_dtl_buffer(int cpu)
117 {
118 	long ret;
119 	struct paca_struct *pp;
120 	struct dtl_entry *dtl;
121 	int hwcpu = get_hard_smp_processor_id(cpu);
122 
123 	pp = paca_ptrs[cpu];
124 	dtl = pp->dispatch_log;
125 	if (dtl && dtl_mask) {
126 		pp->dtl_ridx = 0;
127 		pp->dtl_curr = dtl;
128 		lppaca_of(cpu).dtl_idx = 0;
129 
130 		/* hypervisor reads buffer length from this field */
131 		dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
132 		ret = register_dtl(hwcpu, __pa(dtl));
133 		if (ret)
134 			pr_err("WARNING: DTL registration of cpu %d (hw %d) failed with %ld\n",
135 			       cpu, hwcpu, ret);
136 
137 		lppaca_of(cpu).dtl_enable_mask = dtl_mask;
138 	}
139 }
140 
141 #ifdef CONFIG_PPC_SPLPAR
142 struct dtl_worker {
143 	struct delayed_work work;
144 	int cpu;
145 };
146 
147 struct vcpu_dispatch_data {
148 	int last_disp_cpu;
149 
150 	int total_disp;
151 
152 	int same_cpu_disp;
153 	int same_chip_disp;
154 	int diff_chip_disp;
155 	int far_chip_disp;
156 
157 	int numa_home_disp;
158 	int numa_remote_disp;
159 	int numa_far_disp;
160 };
161 
162 /*
163  * This represents the number of cpus in the hypervisor. Since there is no
164  * architected way to discover the number of processors in the host, we
165  * provision for dealing with NR_CPUS. This is currently 2048 by default, and
166  * is sufficient for our purposes. This will need to be tweaked if
167  * CONFIG_NR_CPUS is changed.
168  */
169 #define NR_CPUS_H	NR_CPUS
170 
171 DEFINE_RWLOCK(dtl_access_lock);
172 static DEFINE_PER_CPU(struct vcpu_dispatch_data, vcpu_disp_data);
173 static DEFINE_PER_CPU(u64, dtl_entry_ridx);
174 static DEFINE_PER_CPU(struct dtl_worker, dtl_workers);
175 static enum cpuhp_state dtl_worker_state;
176 static DEFINE_MUTEX(dtl_enable_mutex);
177 static int vcpudispatch_stats_on __read_mostly;
178 static int vcpudispatch_stats_freq = 50;
179 static __be32 *vcpu_associativity, *pcpu_associativity;
180 
181 
182 static void free_dtl_buffers(unsigned long *time_limit)
183 {
184 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
185 	int cpu;
186 	struct paca_struct *pp;
187 
188 	for_each_possible_cpu(cpu) {
189 		pp = paca_ptrs[cpu];
190 		if (!pp->dispatch_log)
191 			continue;
192 		kmem_cache_free(dtl_cache, pp->dispatch_log);
193 		pp->dtl_ridx = 0;
194 		pp->dispatch_log = 0;
195 		pp->dispatch_log_end = 0;
196 		pp->dtl_curr = 0;
197 
198 		if (time_limit && time_after(jiffies, *time_limit)) {
199 			cond_resched();
200 			*time_limit = jiffies + HZ;
201 		}
202 	}
203 #endif
204 }
205 
206 static int init_cpu_associativity(void)
207 {
208 	vcpu_associativity = kcalloc(num_possible_cpus() / threads_per_core,
209 			VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
210 	pcpu_associativity = kcalloc(NR_CPUS_H / threads_per_core,
211 			VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
212 
213 	if (!vcpu_associativity || !pcpu_associativity) {
214 		pr_err("error allocating memory for associativity information\n");
215 		return -ENOMEM;
216 	}
217 
218 	return 0;
219 }
220 
221 static void destroy_cpu_associativity(void)
222 {
223 	kfree(vcpu_associativity);
224 	kfree(pcpu_associativity);
225 	vcpu_associativity = pcpu_associativity = 0;
226 }
227 
228 static __be32 *__get_cpu_associativity(int cpu, __be32 *cpu_assoc, int flag)
229 {
230 	__be32 *assoc;
231 	int rc = 0;
232 
233 	assoc = &cpu_assoc[(int)(cpu / threads_per_core) * VPHN_ASSOC_BUFSIZE];
234 	if (!assoc[0]) {
235 		rc = hcall_vphn(cpu, flag, &assoc[0]);
236 		if (rc)
237 			return NULL;
238 	}
239 
240 	return assoc;
241 }
242 
243 static __be32 *get_pcpu_associativity(int cpu)
244 {
245 	return __get_cpu_associativity(cpu, pcpu_associativity, VPHN_FLAG_PCPU);
246 }
247 
248 static __be32 *get_vcpu_associativity(int cpu)
249 {
250 	return __get_cpu_associativity(cpu, vcpu_associativity, VPHN_FLAG_VCPU);
251 }
252 
253 static int cpu_relative_dispatch_distance(int last_disp_cpu, int cur_disp_cpu)
254 {
255 	__be32 *last_disp_cpu_assoc, *cur_disp_cpu_assoc;
256 
257 	if (last_disp_cpu >= NR_CPUS_H || cur_disp_cpu >= NR_CPUS_H)
258 		return -EINVAL;
259 
260 	last_disp_cpu_assoc = get_pcpu_associativity(last_disp_cpu);
261 	cur_disp_cpu_assoc = get_pcpu_associativity(cur_disp_cpu);
262 
263 	if (!last_disp_cpu_assoc || !cur_disp_cpu_assoc)
264 		return -EIO;
265 
266 	return cpu_relative_distance(last_disp_cpu_assoc, cur_disp_cpu_assoc);
267 }
268 
269 static int cpu_home_node_dispatch_distance(int disp_cpu)
270 {
271 	__be32 *disp_cpu_assoc, *vcpu_assoc;
272 	int vcpu_id = smp_processor_id();
273 
274 	if (disp_cpu >= NR_CPUS_H) {
275 		pr_debug_ratelimited("vcpu dispatch cpu %d > %d\n",
276 						disp_cpu, NR_CPUS_H);
277 		return -EINVAL;
278 	}
279 
280 	disp_cpu_assoc = get_pcpu_associativity(disp_cpu);
281 	vcpu_assoc = get_vcpu_associativity(vcpu_id);
282 
283 	if (!disp_cpu_assoc || !vcpu_assoc)
284 		return -EIO;
285 
286 	return cpu_relative_distance(disp_cpu_assoc, vcpu_assoc);
287 }
288 
289 static void update_vcpu_disp_stat(int disp_cpu)
290 {
291 	struct vcpu_dispatch_data *disp;
292 	int distance;
293 
294 	disp = this_cpu_ptr(&vcpu_disp_data);
295 	if (disp->last_disp_cpu == -1) {
296 		disp->last_disp_cpu = disp_cpu;
297 		return;
298 	}
299 
300 	disp->total_disp++;
301 
302 	if (disp->last_disp_cpu == disp_cpu ||
303 		(cpu_first_thread_sibling(disp->last_disp_cpu) ==
304 					cpu_first_thread_sibling(disp_cpu)))
305 		disp->same_cpu_disp++;
306 	else {
307 		distance = cpu_relative_dispatch_distance(disp->last_disp_cpu,
308 								disp_cpu);
309 		if (distance < 0)
310 			pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
311 					smp_processor_id());
312 		else {
313 			switch (distance) {
314 			case 0:
315 				disp->same_chip_disp++;
316 				break;
317 			case 1:
318 				disp->diff_chip_disp++;
319 				break;
320 			case 2:
321 				disp->far_chip_disp++;
322 				break;
323 			default:
324 				pr_debug_ratelimited("vcpudispatch_stats: cpu %d (%d -> %d): unexpected relative dispatch distance %d\n",
325 						 smp_processor_id(),
326 						 disp->last_disp_cpu,
327 						 disp_cpu,
328 						 distance);
329 			}
330 		}
331 	}
332 
333 	distance = cpu_home_node_dispatch_distance(disp_cpu);
334 	if (distance < 0)
335 		pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
336 				smp_processor_id());
337 	else {
338 		switch (distance) {
339 		case 0:
340 			disp->numa_home_disp++;
341 			break;
342 		case 1:
343 			disp->numa_remote_disp++;
344 			break;
345 		case 2:
346 			disp->numa_far_disp++;
347 			break;
348 		default:
349 			pr_debug_ratelimited("vcpudispatch_stats: cpu %d on %d: unexpected numa dispatch distance %d\n",
350 						 smp_processor_id(),
351 						 disp_cpu,
352 						 distance);
353 		}
354 	}
355 
356 	disp->last_disp_cpu = disp_cpu;
357 }
358 
359 static void process_dtl_buffer(struct work_struct *work)
360 {
361 	struct dtl_entry dtle;
362 	u64 i = __this_cpu_read(dtl_entry_ridx);
363 	struct dtl_entry *dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
364 	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
365 	struct lppaca *vpa = local_paca->lppaca_ptr;
366 	struct dtl_worker *d = container_of(work, struct dtl_worker, work.work);
367 
368 	if (!local_paca->dispatch_log)
369 		return;
370 
371 	/* if we have been migrated away, we cancel ourself */
372 	if (d->cpu != smp_processor_id()) {
373 		pr_debug("vcpudispatch_stats: cpu %d worker migrated -- canceling worker\n",
374 						smp_processor_id());
375 		return;
376 	}
377 
378 	if (i == be64_to_cpu(vpa->dtl_idx))
379 		goto out;
380 
381 	while (i < be64_to_cpu(vpa->dtl_idx)) {
382 		dtle = *dtl;
383 		barrier();
384 		if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
385 			/* buffer has overflowed */
386 			pr_debug_ratelimited("vcpudispatch_stats: cpu %d lost %lld DTL samples\n",
387 				d->cpu,
388 				be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG - i);
389 			i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
390 			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
391 			continue;
392 		}
393 		update_vcpu_disp_stat(be16_to_cpu(dtle.processor_id));
394 		++i;
395 		++dtl;
396 		if (dtl == dtl_end)
397 			dtl = local_paca->dispatch_log;
398 	}
399 
400 	__this_cpu_write(dtl_entry_ridx, i);
401 
402 out:
403 	schedule_delayed_work_on(d->cpu, to_delayed_work(work),
404 					HZ / vcpudispatch_stats_freq);
405 }
406 
407 static int dtl_worker_online(unsigned int cpu)
408 {
409 	struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
410 
411 	memset(d, 0, sizeof(*d));
412 	INIT_DELAYED_WORK(&d->work, process_dtl_buffer);
413 	d->cpu = cpu;
414 
415 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
416 	per_cpu(dtl_entry_ridx, cpu) = 0;
417 	register_dtl_buffer(cpu);
418 #else
419 	per_cpu(dtl_entry_ridx, cpu) = be64_to_cpu(lppaca_of(cpu).dtl_idx);
420 #endif
421 
422 	schedule_delayed_work_on(cpu, &d->work, HZ / vcpudispatch_stats_freq);
423 	return 0;
424 }
425 
426 static int dtl_worker_offline(unsigned int cpu)
427 {
428 	struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
429 
430 	cancel_delayed_work_sync(&d->work);
431 
432 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
433 	unregister_dtl(get_hard_smp_processor_id(cpu));
434 #endif
435 
436 	return 0;
437 }
438 
439 static void set_global_dtl_mask(u8 mask)
440 {
441 	int cpu;
442 
443 	dtl_mask = mask;
444 	for_each_present_cpu(cpu)
445 		lppaca_of(cpu).dtl_enable_mask = dtl_mask;
446 }
447 
448 static void reset_global_dtl_mask(void)
449 {
450 	int cpu;
451 
452 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
453 	dtl_mask = DTL_LOG_PREEMPT;
454 #else
455 	dtl_mask = 0;
456 #endif
457 	for_each_present_cpu(cpu)
458 		lppaca_of(cpu).dtl_enable_mask = dtl_mask;
459 }
460 
461 static int dtl_worker_enable(unsigned long *time_limit)
462 {
463 	int rc = 0, state;
464 
465 	if (!write_trylock(&dtl_access_lock)) {
466 		rc = -EBUSY;
467 		goto out;
468 	}
469 
470 	set_global_dtl_mask(DTL_LOG_ALL);
471 
472 	/* Setup dtl buffers and register those */
473 	alloc_dtl_buffers(time_limit);
474 
475 	state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/dtl:online",
476 					dtl_worker_online, dtl_worker_offline);
477 	if (state < 0) {
478 		pr_err("vcpudispatch_stats: unable to setup workqueue for DTL processing\n");
479 		free_dtl_buffers(time_limit);
480 		reset_global_dtl_mask();
481 		write_unlock(&dtl_access_lock);
482 		rc = -EINVAL;
483 		goto out;
484 	}
485 	dtl_worker_state = state;
486 
487 out:
488 	return rc;
489 }
490 
491 static void dtl_worker_disable(unsigned long *time_limit)
492 {
493 	cpuhp_remove_state(dtl_worker_state);
494 	free_dtl_buffers(time_limit);
495 	reset_global_dtl_mask();
496 	write_unlock(&dtl_access_lock);
497 }
498 
499 static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
500 		size_t count, loff_t *ppos)
501 {
502 	unsigned long time_limit = jiffies + HZ;
503 	struct vcpu_dispatch_data *disp;
504 	int rc, cmd, cpu;
505 	char buf[16];
506 
507 	if (count > 15)
508 		return -EINVAL;
509 
510 	if (copy_from_user(buf, p, count))
511 		return -EFAULT;
512 
513 	buf[count] = 0;
514 	rc = kstrtoint(buf, 0, &cmd);
515 	if (rc || cmd < 0 || cmd > 1) {
516 		pr_err("vcpudispatch_stats: please use 0 to disable or 1 to enable dispatch statistics\n");
517 		return rc ? rc : -EINVAL;
518 	}
519 
520 	mutex_lock(&dtl_enable_mutex);
521 
522 	if ((cmd == 0 && !vcpudispatch_stats_on) ||
523 			(cmd == 1 && vcpudispatch_stats_on))
524 		goto out;
525 
526 	if (cmd) {
527 		rc = init_cpu_associativity();
528 		if (rc)
529 			goto out;
530 
531 		for_each_possible_cpu(cpu) {
532 			disp = per_cpu_ptr(&vcpu_disp_data, cpu);
533 			memset(disp, 0, sizeof(*disp));
534 			disp->last_disp_cpu = -1;
535 		}
536 
537 		rc = dtl_worker_enable(&time_limit);
538 		if (rc) {
539 			destroy_cpu_associativity();
540 			goto out;
541 		}
542 	} else {
543 		dtl_worker_disable(&time_limit);
544 		destroy_cpu_associativity();
545 	}
546 
547 	vcpudispatch_stats_on = cmd;
548 
549 out:
550 	mutex_unlock(&dtl_enable_mutex);
551 	if (rc)
552 		return rc;
553 	return count;
554 }
555 
556 static int vcpudispatch_stats_display(struct seq_file *p, void *v)
557 {
558 	int cpu;
559 	struct vcpu_dispatch_data *disp;
560 
561 	if (!vcpudispatch_stats_on) {
562 		seq_puts(p, "off\n");
563 		return 0;
564 	}
565 
566 	for_each_online_cpu(cpu) {
567 		disp = per_cpu_ptr(&vcpu_disp_data, cpu);
568 		seq_printf(p, "cpu%d", cpu);
569 		seq_put_decimal_ull(p, " ", disp->total_disp);
570 		seq_put_decimal_ull(p, " ", disp->same_cpu_disp);
571 		seq_put_decimal_ull(p, " ", disp->same_chip_disp);
572 		seq_put_decimal_ull(p, " ", disp->diff_chip_disp);
573 		seq_put_decimal_ull(p, " ", disp->far_chip_disp);
574 		seq_put_decimal_ull(p, " ", disp->numa_home_disp);
575 		seq_put_decimal_ull(p, " ", disp->numa_remote_disp);
576 		seq_put_decimal_ull(p, " ", disp->numa_far_disp);
577 		seq_puts(p, "\n");
578 	}
579 
580 	return 0;
581 }
582 
583 static int vcpudispatch_stats_open(struct inode *inode, struct file *file)
584 {
585 	return single_open(file, vcpudispatch_stats_display, NULL);
586 }
587 
588 static const struct proc_ops vcpudispatch_stats_proc_ops = {
589 	.proc_open	= vcpudispatch_stats_open,
590 	.proc_read	= seq_read,
591 	.proc_write	= vcpudispatch_stats_write,
592 	.proc_lseek	= seq_lseek,
593 	.proc_release	= single_release,
594 };
595 
596 static ssize_t vcpudispatch_stats_freq_write(struct file *file,
597 		const char __user *p, size_t count, loff_t *ppos)
598 {
599 	int rc, freq;
600 	char buf[16];
601 
602 	if (count > 15)
603 		return -EINVAL;
604 
605 	if (copy_from_user(buf, p, count))
606 		return -EFAULT;
607 
608 	buf[count] = 0;
609 	rc = kstrtoint(buf, 0, &freq);
610 	if (rc || freq < 1 || freq > HZ) {
611 		pr_err("vcpudispatch_stats_freq: please specify a frequency between 1 and %d\n",
612 				HZ);
613 		return rc ? rc : -EINVAL;
614 	}
615 
616 	vcpudispatch_stats_freq = freq;
617 
618 	return count;
619 }
620 
621 static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
622 {
623 	seq_printf(p, "%d\n", vcpudispatch_stats_freq);
624 	return 0;
625 }
626 
627 static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)
628 {
629 	return single_open(file, vcpudispatch_stats_freq_display, NULL);
630 }
631 
632 static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {
633 	.proc_open	= vcpudispatch_stats_freq_open,
634 	.proc_read	= seq_read,
635 	.proc_write	= vcpudispatch_stats_freq_write,
636 	.proc_lseek	= seq_lseek,
637 	.proc_release	= single_release,
638 };
639 
640 static int __init vcpudispatch_stats_procfs_init(void)
641 {
642 	/*
643 	 * Avoid smp_processor_id while preemptible. All CPUs should have
644 	 * the same value for lppaca_shared_proc.
645 	 */
646 	preempt_disable();
647 	if (!lppaca_shared_proc(get_lppaca())) {
648 		preempt_enable();
649 		return 0;
650 	}
651 	preempt_enable();
652 
653 	if (!proc_create("powerpc/vcpudispatch_stats", 0600, NULL,
654 					&vcpudispatch_stats_proc_ops))
655 		pr_err("vcpudispatch_stats: error creating procfs file\n");
656 	else if (!proc_create("powerpc/vcpudispatch_stats_freq", 0600, NULL,
657 					&vcpudispatch_stats_freq_proc_ops))
658 		pr_err("vcpudispatch_stats_freq: error creating procfs file\n");
659 
660 	return 0;
661 }
662 
663 machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);
664 
665 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
666 u64 pseries_paravirt_steal_clock(int cpu)
667 {
668 	struct lppaca *lppaca = &lppaca_of(cpu);
669 
670 	return be64_to_cpu(READ_ONCE(lppaca->enqueue_dispatch_tb)) +
671 		be64_to_cpu(READ_ONCE(lppaca->ready_enqueue_tb));
672 }
673 #endif
674 
675 #endif /* CONFIG_PPC_SPLPAR */
676 
677 void vpa_init(int cpu)
678 {
679 	int hwcpu = get_hard_smp_processor_id(cpu);
680 	unsigned long addr;
681 	long ret;
682 
683 	/*
684 	 * The spec says it "may be problematic" if CPU x registers the VPA of
685 	 * CPU y. We should never do that, but wail if we ever do.
686 	 */
687 	WARN_ON(cpu != smp_processor_id());
688 
689 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
690 		lppaca_of(cpu).vmxregs_in_use = 1;
691 
692 	if (cpu_has_feature(CPU_FTR_ARCH_207S))
693 		lppaca_of(cpu).ebb_regs_in_use = 1;
694 
695 	addr = __pa(&lppaca_of(cpu));
696 	ret = register_vpa(hwcpu, addr);
697 
698 	if (ret) {
699 		pr_err("WARNING: VPA registration for cpu %d (hw %d) of area "
700 		       "%lx failed with %ld\n", cpu, hwcpu, addr, ret);
701 		return;
702 	}
703 
704 #ifdef CONFIG_PPC_64S_HASH_MMU
705 	/*
706 	 * PAPR says this feature is SLB-Buffer but firmware never
707 	 * reports that.  All SPLPAR support SLB shadow buffer.
708 	 */
709 	if (!radix_enabled() && firmware_has_feature(FW_FEATURE_SPLPAR)) {
710 		addr = __pa(paca_ptrs[cpu]->slb_shadow_ptr);
711 		ret = register_slb_shadow(hwcpu, addr);
712 		if (ret)
713 			pr_err("WARNING: SLB shadow buffer registration for "
714 			       "cpu %d (hw %d) of area %lx failed with %ld\n",
715 			       cpu, hwcpu, addr, ret);
716 	}
717 #endif /* CONFIG_PPC_64S_HASH_MMU */
718 
719 	/*
720 	 * Register dispatch trace log, if one has been allocated.
721 	 */
722 	register_dtl_buffer(cpu);
723 }
724 
725 #ifdef CONFIG_PPC_BOOK3S_64
726 
727 static int __init pseries_lpar_register_process_table(unsigned long base,
728 			unsigned long page_size, unsigned long table_size)
729 {
730 	long rc;
731 	unsigned long flags = 0;
732 
733 	if (table_size)
734 		flags |= PROC_TABLE_NEW;
735 	if (radix_enabled()) {
736 		flags |= PROC_TABLE_RADIX;
737 		if (mmu_has_feature(MMU_FTR_GTSE))
738 			flags |= PROC_TABLE_GTSE;
739 	} else
740 		flags |= PROC_TABLE_HPT_SLB;
741 	for (;;) {
742 		rc = plpar_hcall_norets(H_REGISTER_PROC_TBL, flags, base,
743 					page_size, table_size);
744 		if (!H_IS_LONG_BUSY(rc))
745 			break;
746 		mdelay(get_longbusy_msecs(rc));
747 	}
748 	if (rc != H_SUCCESS) {
749 		pr_err("Failed to register process table (rc=%ld)\n", rc);
750 		BUG();
751 	}
752 	return rc;
753 }
754 
755 #ifdef CONFIG_PPC_64S_HASH_MMU
756 
757 static long pSeries_lpar_hpte_insert(unsigned long hpte_group,
758 				     unsigned long vpn, unsigned long pa,
759 				     unsigned long rflags, unsigned long vflags,
760 				     int psize, int apsize, int ssize)
761 {
762 	unsigned long lpar_rc;
763 	unsigned long flags;
764 	unsigned long slot;
765 	unsigned long hpte_v, hpte_r;
766 
767 	if (!(vflags & HPTE_V_BOLTED))
768 		pr_devel("hpte_insert(group=%lx, vpn=%016lx, "
769 			 "pa=%016lx, rflags=%lx, vflags=%lx, psize=%d)\n",
770 			 hpte_group, vpn,  pa, rflags, vflags, psize);
771 
772 	hpte_v = hpte_encode_v(vpn, psize, apsize, ssize) | vflags | HPTE_V_VALID;
773 	hpte_r = hpte_encode_r(pa, psize, apsize) | rflags;
774 
775 	if (!(vflags & HPTE_V_BOLTED))
776 		pr_devel(" hpte_v=%016lx, hpte_r=%016lx\n", hpte_v, hpte_r);
777 
778 	/* Now fill in the actual HPTE */
779 	/* Set CEC cookie to 0         */
780 	/* Zero page = 0               */
781 	/* I-cache Invalidate = 0      */
782 	/* I-cache synchronize = 0     */
783 	/* Exact = 0                   */
784 	flags = 0;
785 
786 	if (firmware_has_feature(FW_FEATURE_XCMO) && !(hpte_r & HPTE_R_N))
787 		flags |= H_COALESCE_CAND;
788 
789 	lpar_rc = plpar_pte_enter(flags, hpte_group, hpte_v, hpte_r, &slot);
790 	if (unlikely(lpar_rc == H_PTEG_FULL)) {
791 		pr_devel("Hash table group is full\n");
792 		return -1;
793 	}
794 
795 	/*
796 	 * Since we try and ioremap PHBs we don't own, the pte insert
797 	 * will fail. However we must catch the failure in hash_page
798 	 * or we will loop forever, so return -2 in this case.
799 	 */
800 	if (unlikely(lpar_rc != H_SUCCESS)) {
801 		pr_err("Failed hash pte insert with error %ld\n", lpar_rc);
802 		return -2;
803 	}
804 	if (!(vflags & HPTE_V_BOLTED))
805 		pr_devel(" -> slot: %lu\n", slot & 7);
806 
807 	/* Because of iSeries, we have to pass down the secondary
808 	 * bucket bit here as well
809 	 */
810 	return (slot & 7) | (!!(vflags & HPTE_V_SECONDARY) << 3);
811 }
812 
813 static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
814 
815 static long pSeries_lpar_hpte_remove(unsigned long hpte_group)
816 {
817 	unsigned long slot_offset;
818 	unsigned long lpar_rc;
819 	int i;
820 	unsigned long dummy1, dummy2;
821 
822 	/* pick a random slot to start at */
823 	slot_offset = mftb() & 0x7;
824 
825 	for (i = 0; i < HPTES_PER_GROUP; i++) {
826 
827 		/* don't remove a bolted entry */
828 		lpar_rc = plpar_pte_remove(H_ANDCOND, hpte_group + slot_offset,
829 					   HPTE_V_BOLTED, &dummy1, &dummy2);
830 		if (lpar_rc == H_SUCCESS)
831 			return i;
832 
833 		/*
834 		 * The test for adjunct partition is performed before the
835 		 * ANDCOND test.  H_RESOURCE may be returned, so we need to
836 		 * check for that as well.
837 		 */
838 		BUG_ON(lpar_rc != H_NOT_FOUND && lpar_rc != H_RESOURCE);
839 
840 		slot_offset++;
841 		slot_offset &= 0x7;
842 	}
843 
844 	return -1;
845 }
846 
847 /* Called during kexec sequence with MMU off */
848 static notrace void manual_hpte_clear_all(void)
849 {
850 	unsigned long size_bytes = 1UL << ppc64_pft_size;
851 	unsigned long hpte_count = size_bytes >> 4;
852 	struct {
853 		unsigned long pteh;
854 		unsigned long ptel;
855 	} ptes[4];
856 	long lpar_rc;
857 	unsigned long i, j;
858 
859 	/* Read in batches of 4,
860 	 * invalidate only valid entries not in the VRMA
861 	 * hpte_count will be a multiple of 4
862          */
863 	for (i = 0; i < hpte_count; i += 4) {
864 		lpar_rc = plpar_pte_read_4_raw(0, i, (void *)ptes);
865 		if (lpar_rc != H_SUCCESS) {
866 			pr_info("Failed to read hash page table at %ld err %ld\n",
867 				i, lpar_rc);
868 			continue;
869 		}
870 		for (j = 0; j < 4; j++){
871 			if ((ptes[j].pteh & HPTE_V_VRMA_MASK) ==
872 				HPTE_V_VRMA_MASK)
873 				continue;
874 			if (ptes[j].pteh & HPTE_V_VALID)
875 				plpar_pte_remove_raw(0, i + j, 0,
876 					&(ptes[j].pteh), &(ptes[j].ptel));
877 		}
878 	}
879 }
880 
881 /* Called during kexec sequence with MMU off */
882 static notrace int hcall_hpte_clear_all(void)
883 {
884 	int rc;
885 
886 	do {
887 		rc = plpar_hcall_norets(H_CLEAR_HPT);
888 	} while (rc == H_CONTINUE);
889 
890 	return rc;
891 }
892 
893 /* Called during kexec sequence with MMU off */
894 static notrace void pseries_hpte_clear_all(void)
895 {
896 	int rc;
897 
898 	rc = hcall_hpte_clear_all();
899 	if (rc != H_SUCCESS)
900 		manual_hpte_clear_all();
901 
902 #ifdef __LITTLE_ENDIAN__
903 	/*
904 	 * Reset exceptions to big endian.
905 	 *
906 	 * FIXME this is a hack for kexec, we need to reset the exception
907 	 * endian before starting the new kernel and this is a convenient place
908 	 * to do it.
909 	 *
910 	 * This is also called on boot when a fadump happens. In that case we
911 	 * must not change the exception endian mode.
912 	 */
913 	if (firmware_has_feature(FW_FEATURE_SET_MODE) && !is_fadump_active())
914 		pseries_big_endian_exceptions();
915 #endif
916 }
917 
918 /*
919  * NOTE: for updatepp ops we are fortunate that the linux "newpp" bits and
920  * the low 3 bits of flags happen to line up.  So no transform is needed.
921  * We can probably optimize here and assume the high bits of newpp are
922  * already zero.  For now I am paranoid.
923  */
924 static long pSeries_lpar_hpte_updatepp(unsigned long slot,
925 				       unsigned long newpp,
926 				       unsigned long vpn,
927 				       int psize, int apsize,
928 				       int ssize, unsigned long inv_flags)
929 {
930 	unsigned long lpar_rc;
931 	unsigned long flags;
932 	unsigned long want_v;
933 
934 	want_v = hpte_encode_avpn(vpn, psize, ssize);
935 
936 	flags = (newpp & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO)) | H_AVPN;
937 	flags |= (newpp & HPTE_R_KEY_HI) >> 48;
938 	if (mmu_has_feature(MMU_FTR_KERNEL_RO))
939 		/* Move pp0 into bit 8 (IBM 55) */
940 		flags |= (newpp & HPTE_R_PP0) >> 55;
941 
942 	pr_devel("    update: avpnv=%016lx, hash=%016lx, f=%lx, psize: %d ...",
943 		 want_v, slot, flags, psize);
944 
945 	lpar_rc = plpar_pte_protect(flags, slot, want_v);
946 
947 	if (lpar_rc == H_NOT_FOUND) {
948 		pr_devel("not found !\n");
949 		return -1;
950 	}
951 
952 	pr_devel("ok\n");
953 
954 	BUG_ON(lpar_rc != H_SUCCESS);
955 
956 	return 0;
957 }
958 
959 static long __pSeries_lpar_hpte_find(unsigned long want_v, unsigned long hpte_group)
960 {
961 	long lpar_rc;
962 	unsigned long i, j;
963 	struct {
964 		unsigned long pteh;
965 		unsigned long ptel;
966 	} ptes[4];
967 
968 	for (i = 0; i < HPTES_PER_GROUP; i += 4, hpte_group += 4) {
969 
970 		lpar_rc = plpar_pte_read_4(0, hpte_group, (void *)ptes);
971 		if (lpar_rc != H_SUCCESS) {
972 			pr_info("Failed to read hash page table at %ld err %ld\n",
973 				hpte_group, lpar_rc);
974 			continue;
975 		}
976 
977 		for (j = 0; j < 4; j++) {
978 			if (HPTE_V_COMPARE(ptes[j].pteh, want_v) &&
979 			    (ptes[j].pteh & HPTE_V_VALID))
980 				return i + j;
981 		}
982 	}
983 
984 	return -1;
985 }
986 
987 static long pSeries_lpar_hpte_find(unsigned long vpn, int psize, int ssize)
988 {
989 	long slot;
990 	unsigned long hash;
991 	unsigned long want_v;
992 	unsigned long hpte_group;
993 
994 	hash = hpt_hash(vpn, mmu_psize_defs[psize].shift, ssize);
995 	want_v = hpte_encode_avpn(vpn, psize, ssize);
996 
997 	/*
998 	 * We try to keep bolted entries always in primary hash
999 	 * But in some case we can find them in secondary too.
1000 	 */
1001 	hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1002 	slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
1003 	if (slot < 0) {
1004 		/* Try in secondary */
1005 		hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
1006 		slot = __pSeries_lpar_hpte_find(want_v, hpte_group);
1007 		if (slot < 0)
1008 			return -1;
1009 	}
1010 	return hpte_group + slot;
1011 }
1012 
1013 static void pSeries_lpar_hpte_updateboltedpp(unsigned long newpp,
1014 					     unsigned long ea,
1015 					     int psize, int ssize)
1016 {
1017 	unsigned long vpn;
1018 	unsigned long lpar_rc, slot, vsid, flags;
1019 
1020 	vsid = get_kernel_vsid(ea, ssize);
1021 	vpn = hpt_vpn(ea, vsid, ssize);
1022 
1023 	slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
1024 	BUG_ON(slot == -1);
1025 
1026 	flags = newpp & (HPTE_R_PP | HPTE_R_N);
1027 	if (mmu_has_feature(MMU_FTR_KERNEL_RO))
1028 		/* Move pp0 into bit 8 (IBM 55) */
1029 		flags |= (newpp & HPTE_R_PP0) >> 55;
1030 
1031 	flags |= ((newpp & HPTE_R_KEY_HI) >> 48) | (newpp & HPTE_R_KEY_LO);
1032 
1033 	lpar_rc = plpar_pte_protect(flags, slot, 0);
1034 
1035 	BUG_ON(lpar_rc != H_SUCCESS);
1036 }
1037 
1038 static void pSeries_lpar_hpte_invalidate(unsigned long slot, unsigned long vpn,
1039 					 int psize, int apsize,
1040 					 int ssize, int local)
1041 {
1042 	unsigned long want_v;
1043 	unsigned long lpar_rc;
1044 	unsigned long dummy1, dummy2;
1045 
1046 	pr_devel("    inval : slot=%lx, vpn=%016lx, psize: %d, local: %d\n",
1047 		 slot, vpn, psize, local);
1048 
1049 	want_v = hpte_encode_avpn(vpn, psize, ssize);
1050 	lpar_rc = plpar_pte_remove(H_AVPN, slot, want_v, &dummy1, &dummy2);
1051 	if (lpar_rc == H_NOT_FOUND)
1052 		return;
1053 
1054 	BUG_ON(lpar_rc != H_SUCCESS);
1055 }
1056 
1057 
1058 /*
1059  * As defined in the PAPR's section 14.5.4.1.8
1060  * The control mask doesn't include the returned reference and change bit from
1061  * the processed PTE.
1062  */
1063 #define HBLKR_AVPN		0x0100000000000000UL
1064 #define HBLKR_CTRL_MASK		0xf800000000000000UL
1065 #define HBLKR_CTRL_SUCCESS	0x8000000000000000UL
1066 #define HBLKR_CTRL_ERRNOTFOUND	0x8800000000000000UL
1067 #define HBLKR_CTRL_ERRBUSY	0xa000000000000000UL
1068 
1069 /*
1070  * Returned true if we are supporting this block size for the specified segment
1071  * base page size and actual page size.
1072  *
1073  * Currently, we only support 8 size block.
1074  */
1075 static inline bool is_supported_hlbkrm(int bpsize, int psize)
1076 {
1077 	return (hblkrm_size[bpsize][psize] == HBLKRM_SUPPORTED_BLOCK_SIZE);
1078 }
1079 
1080 /**
1081  * H_BLOCK_REMOVE caller.
1082  * @idx should point to the latest @param entry set with a PTEX.
1083  * If PTE cannot be processed because another CPUs has already locked that
1084  * group, those entries are put back in @param starting at index 1.
1085  * If entries has to be retried and @retry_busy is set to true, these entries
1086  * are retried until success. If @retry_busy is set to false, the returned
1087  * is the number of entries yet to process.
1088  */
1089 static unsigned long call_block_remove(unsigned long idx, unsigned long *param,
1090 				       bool retry_busy)
1091 {
1092 	unsigned long i, rc, new_idx;
1093 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1094 
1095 	if (idx < 2) {
1096 		pr_warn("Unexpected empty call to H_BLOCK_REMOVE");
1097 		return 0;
1098 	}
1099 again:
1100 	new_idx = 0;
1101 	if (idx > PLPAR_HCALL9_BUFSIZE) {
1102 		pr_err("Too many PTEs (%lu) for H_BLOCK_REMOVE", idx);
1103 		idx = PLPAR_HCALL9_BUFSIZE;
1104 	} else if (idx < PLPAR_HCALL9_BUFSIZE)
1105 		param[idx] = HBR_END;
1106 
1107 	rc = plpar_hcall9(H_BLOCK_REMOVE, retbuf,
1108 			  param[0], /* AVA */
1109 			  param[1],  param[2],  param[3],  param[4], /* TS0-7 */
1110 			  param[5],  param[6],  param[7],  param[8]);
1111 	if (rc == H_SUCCESS)
1112 		return 0;
1113 
1114 	BUG_ON(rc != H_PARTIAL);
1115 
1116 	/* Check that the unprocessed entries were 'not found' or 'busy' */
1117 	for (i = 0; i < idx-1; i++) {
1118 		unsigned long ctrl = retbuf[i] & HBLKR_CTRL_MASK;
1119 
1120 		if (ctrl == HBLKR_CTRL_ERRBUSY) {
1121 			param[++new_idx] = param[i+1];
1122 			continue;
1123 		}
1124 
1125 		BUG_ON(ctrl != HBLKR_CTRL_SUCCESS
1126 		       && ctrl != HBLKR_CTRL_ERRNOTFOUND);
1127 	}
1128 
1129 	/*
1130 	 * If there were entries found busy, retry these entries if requested,
1131 	 * of if all the entries have to be retried.
1132 	 */
1133 	if (new_idx && (retry_busy || new_idx == (PLPAR_HCALL9_BUFSIZE-1))) {
1134 		idx = new_idx + 1;
1135 		goto again;
1136 	}
1137 
1138 	return new_idx;
1139 }
1140 
1141 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1142 /*
1143  * Limit iterations holding pSeries_lpar_tlbie_lock to 3. We also need
1144  * to make sure that we avoid bouncing the hypervisor tlbie lock.
1145  */
1146 #define PPC64_HUGE_HPTE_BATCH 12
1147 
1148 static void hugepage_block_invalidate(unsigned long *slot, unsigned long *vpn,
1149 				      int count, int psize, int ssize)
1150 {
1151 	unsigned long param[PLPAR_HCALL9_BUFSIZE];
1152 	unsigned long shift, current_vpgb, vpgb;
1153 	int i, pix = 0;
1154 
1155 	shift = mmu_psize_defs[psize].shift;
1156 
1157 	for (i = 0; i < count; i++) {
1158 		/*
1159 		 * Shifting 3 bits more on the right to get a
1160 		 * 8 pages aligned virtual addresse.
1161 		 */
1162 		vpgb = (vpn[i] >> (shift - VPN_SHIFT + 3));
1163 		if (!pix || vpgb != current_vpgb) {
1164 			/*
1165 			 * Need to start a new 8 pages block, flush
1166 			 * the current one if needed.
1167 			 */
1168 			if (pix)
1169 				(void)call_block_remove(pix, param, true);
1170 			current_vpgb = vpgb;
1171 			param[0] = hpte_encode_avpn(vpn[i], psize, ssize);
1172 			pix = 1;
1173 		}
1174 
1175 		param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot[i];
1176 		if (pix == PLPAR_HCALL9_BUFSIZE) {
1177 			pix = call_block_remove(pix, param, false);
1178 			/*
1179 			 * pix = 0 means that all the entries were
1180 			 * removed, we can start a new block.
1181 			 * Otherwise, this means that there are entries
1182 			 * to retry, and pix points to latest one, so
1183 			 * we should increment it and try to continue
1184 			 * the same block.
1185 			 */
1186 			if (pix)
1187 				pix++;
1188 		}
1189 	}
1190 	if (pix)
1191 		(void)call_block_remove(pix, param, true);
1192 }
1193 
1194 static void hugepage_bulk_invalidate(unsigned long *slot, unsigned long *vpn,
1195 				     int count, int psize, int ssize)
1196 {
1197 	unsigned long param[PLPAR_HCALL9_BUFSIZE];
1198 	int i = 0, pix = 0, rc;
1199 
1200 	for (i = 0; i < count; i++) {
1201 
1202 		if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1203 			pSeries_lpar_hpte_invalidate(slot[i], vpn[i], psize, 0,
1204 						     ssize, 0);
1205 		} else {
1206 			param[pix] = HBR_REQUEST | HBR_AVPN | slot[i];
1207 			param[pix+1] = hpte_encode_avpn(vpn[i], psize, ssize);
1208 			pix += 2;
1209 			if (pix == 8) {
1210 				rc = plpar_hcall9(H_BULK_REMOVE, param,
1211 						  param[0], param[1], param[2],
1212 						  param[3], param[4], param[5],
1213 						  param[6], param[7]);
1214 				BUG_ON(rc != H_SUCCESS);
1215 				pix = 0;
1216 			}
1217 		}
1218 	}
1219 	if (pix) {
1220 		param[pix] = HBR_END;
1221 		rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1222 				  param[2], param[3], param[4], param[5],
1223 				  param[6], param[7]);
1224 		BUG_ON(rc != H_SUCCESS);
1225 	}
1226 }
1227 
1228 static inline void __pSeries_lpar_hugepage_invalidate(unsigned long *slot,
1229 						      unsigned long *vpn,
1230 						      int count, int psize,
1231 						      int ssize)
1232 {
1233 	unsigned long flags = 0;
1234 	int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1235 
1236 	if (lock_tlbie)
1237 		spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1238 
1239 	/* Assuming THP size is 16M */
1240 	if (is_supported_hlbkrm(psize, MMU_PAGE_16M))
1241 		hugepage_block_invalidate(slot, vpn, count, psize, ssize);
1242 	else
1243 		hugepage_bulk_invalidate(slot, vpn, count, psize, ssize);
1244 
1245 	if (lock_tlbie)
1246 		spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1247 }
1248 
1249 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1250 					     unsigned long addr,
1251 					     unsigned char *hpte_slot_array,
1252 					     int psize, int ssize, int local)
1253 {
1254 	int i, index = 0;
1255 	unsigned long s_addr = addr;
1256 	unsigned int max_hpte_count, valid;
1257 	unsigned long vpn_array[PPC64_HUGE_HPTE_BATCH];
1258 	unsigned long slot_array[PPC64_HUGE_HPTE_BATCH];
1259 	unsigned long shift, hidx, vpn = 0, hash, slot;
1260 
1261 	shift = mmu_psize_defs[psize].shift;
1262 	max_hpte_count = 1U << (PMD_SHIFT - shift);
1263 
1264 	for (i = 0; i < max_hpte_count; i++) {
1265 		valid = hpte_valid(hpte_slot_array, i);
1266 		if (!valid)
1267 			continue;
1268 		hidx =  hpte_hash_index(hpte_slot_array, i);
1269 
1270 		/* get the vpn */
1271 		addr = s_addr + (i * (1ul << shift));
1272 		vpn = hpt_vpn(addr, vsid, ssize);
1273 		hash = hpt_hash(vpn, shift, ssize);
1274 		if (hidx & _PTEIDX_SECONDARY)
1275 			hash = ~hash;
1276 
1277 		slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1278 		slot += hidx & _PTEIDX_GROUP_IX;
1279 
1280 		slot_array[index] = slot;
1281 		vpn_array[index] = vpn;
1282 		if (index == PPC64_HUGE_HPTE_BATCH - 1) {
1283 			/*
1284 			 * Now do a bluk invalidate
1285 			 */
1286 			__pSeries_lpar_hugepage_invalidate(slot_array,
1287 							   vpn_array,
1288 							   PPC64_HUGE_HPTE_BATCH,
1289 							   psize, ssize);
1290 			index = 0;
1291 		} else
1292 			index++;
1293 	}
1294 	if (index)
1295 		__pSeries_lpar_hugepage_invalidate(slot_array, vpn_array,
1296 						   index, psize, ssize);
1297 }
1298 #else
1299 static void pSeries_lpar_hugepage_invalidate(unsigned long vsid,
1300 					     unsigned long addr,
1301 					     unsigned char *hpte_slot_array,
1302 					     int psize, int ssize, int local)
1303 {
1304 	WARN(1, "%s called without THP support\n", __func__);
1305 }
1306 #endif
1307 
1308 static int pSeries_lpar_hpte_removebolted(unsigned long ea,
1309 					  int psize, int ssize)
1310 {
1311 	unsigned long vpn;
1312 	unsigned long slot, vsid;
1313 
1314 	vsid = get_kernel_vsid(ea, ssize);
1315 	vpn = hpt_vpn(ea, vsid, ssize);
1316 
1317 	slot = pSeries_lpar_hpte_find(vpn, psize, ssize);
1318 	if (slot == -1)
1319 		return -ENOENT;
1320 
1321 	/*
1322 	 * lpar doesn't use the passed actual page size
1323 	 */
1324 	pSeries_lpar_hpte_invalidate(slot, vpn, psize, 0, ssize, 0);
1325 	return 0;
1326 }
1327 
1328 
1329 static inline unsigned long compute_slot(real_pte_t pte,
1330 					 unsigned long vpn,
1331 					 unsigned long index,
1332 					 unsigned long shift,
1333 					 int ssize)
1334 {
1335 	unsigned long slot, hash, hidx;
1336 
1337 	hash = hpt_hash(vpn, shift, ssize);
1338 	hidx = __rpte_to_hidx(pte, index);
1339 	if (hidx & _PTEIDX_SECONDARY)
1340 		hash = ~hash;
1341 	slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1342 	slot += hidx & _PTEIDX_GROUP_IX;
1343 	return slot;
1344 }
1345 
1346 /**
1347  * The hcall H_BLOCK_REMOVE implies that the virtual pages to processed are
1348  * "all within the same naturally aligned 8 page virtual address block".
1349  */
1350 static void do_block_remove(unsigned long number, struct ppc64_tlb_batch *batch,
1351 			    unsigned long *param)
1352 {
1353 	unsigned long vpn;
1354 	unsigned long i, pix = 0;
1355 	unsigned long index, shift, slot, current_vpgb, vpgb;
1356 	real_pte_t pte;
1357 	int psize, ssize;
1358 
1359 	psize = batch->psize;
1360 	ssize = batch->ssize;
1361 
1362 	for (i = 0; i < number; i++) {
1363 		vpn = batch->vpn[i];
1364 		pte = batch->pte[i];
1365 		pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1366 			/*
1367 			 * Shifting 3 bits more on the right to get a
1368 			 * 8 pages aligned virtual addresse.
1369 			 */
1370 			vpgb = (vpn >> (shift - VPN_SHIFT + 3));
1371 			if (!pix || vpgb != current_vpgb) {
1372 				/*
1373 				 * Need to start a new 8 pages block, flush
1374 				 * the current one if needed.
1375 				 */
1376 				if (pix)
1377 					(void)call_block_remove(pix, param,
1378 								true);
1379 				current_vpgb = vpgb;
1380 				param[0] = hpte_encode_avpn(vpn, psize,
1381 							    ssize);
1382 				pix = 1;
1383 			}
1384 
1385 			slot = compute_slot(pte, vpn, index, shift, ssize);
1386 			param[pix++] = HBR_REQUEST | HBLKR_AVPN | slot;
1387 
1388 			if (pix == PLPAR_HCALL9_BUFSIZE) {
1389 				pix = call_block_remove(pix, param, false);
1390 				/*
1391 				 * pix = 0 means that all the entries were
1392 				 * removed, we can start a new block.
1393 				 * Otherwise, this means that there are entries
1394 				 * to retry, and pix points to latest one, so
1395 				 * we should increment it and try to continue
1396 				 * the same block.
1397 				 */
1398 				if (pix)
1399 					pix++;
1400 			}
1401 		} pte_iterate_hashed_end();
1402 	}
1403 
1404 	if (pix)
1405 		(void)call_block_remove(pix, param, true);
1406 }
1407 
1408 /*
1409  * TLB Block Invalidate Characteristics
1410  *
1411  * These characteristics define the size of the block the hcall H_BLOCK_REMOVE
1412  * is able to process for each couple segment base page size, actual page size.
1413  *
1414  * The ibm,get-system-parameter properties is returning a buffer with the
1415  * following layout:
1416  *
1417  * [ 2 bytes size of the RTAS buffer (excluding these 2 bytes) ]
1418  * -----------------
1419  * TLB Block Invalidate Specifiers:
1420  * [ 1 byte LOG base 2 of the TLB invalidate block size being specified ]
1421  * [ 1 byte Number of page sizes (N) that are supported for the specified
1422  *          TLB invalidate block size ]
1423  * [ 1 byte Encoded segment base page size and actual page size
1424  *          MSB=0 means 4k segment base page size and actual page size
1425  *          MSB=1 the penc value in mmu_psize_def ]
1426  * ...
1427  * -----------------
1428  * Next TLB Block Invalidate Specifiers...
1429  * -----------------
1430  * [ 0 ]
1431  */
1432 static inline void set_hblkrm_bloc_size(int bpsize, int psize,
1433 					unsigned int block_size)
1434 {
1435 	if (block_size > hblkrm_size[bpsize][psize])
1436 		hblkrm_size[bpsize][psize] = block_size;
1437 }
1438 
1439 /*
1440  * Decode the Encoded segment base page size and actual page size.
1441  * PAPR specifies:
1442  *   - bit 7 is the L bit
1443  *   - bits 0-5 are the penc value
1444  * If the L bit is 0, this means 4K segment base page size and actual page size
1445  * otherwise the penc value should be read.
1446  */
1447 #define HBLKRM_L_MASK		0x80
1448 #define HBLKRM_PENC_MASK	0x3f
1449 static inline void __init check_lp_set_hblkrm(unsigned int lp,
1450 					      unsigned int block_size)
1451 {
1452 	unsigned int bpsize, psize;
1453 
1454 	/* First, check the L bit, if not set, this means 4K */
1455 	if ((lp & HBLKRM_L_MASK) == 0) {
1456 		set_hblkrm_bloc_size(MMU_PAGE_4K, MMU_PAGE_4K, block_size);
1457 		return;
1458 	}
1459 
1460 	lp &= HBLKRM_PENC_MASK;
1461 	for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++) {
1462 		struct mmu_psize_def *def = &mmu_psize_defs[bpsize];
1463 
1464 		for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
1465 			if (def->penc[psize] == lp) {
1466 				set_hblkrm_bloc_size(bpsize, psize, block_size);
1467 				return;
1468 			}
1469 		}
1470 	}
1471 }
1472 
1473 /*
1474  * The size of the TLB Block Invalidate Characteristics is variable. But at the
1475  * maximum it will be the number of possible page sizes *2 + 10 bytes.
1476  * Currently MMU_PAGE_COUNT is 16, which means 42 bytes. Use a cache line size
1477  * (128 bytes) for the buffer to get plenty of space.
1478  */
1479 #define SPLPAR_TLB_BIC_MAXLENGTH	128
1480 
1481 void __init pseries_lpar_read_hblkrm_characteristics(void)
1482 {
1483 	static struct papr_sysparm_buf buf __initdata;
1484 	int len, idx, bpsize;
1485 
1486 	if (!firmware_has_feature(FW_FEATURE_BLOCK_REMOVE))
1487 		return;
1488 
1489 	if (papr_sysparm_get(PAPR_SYSPARM_TLB_BLOCK_INVALIDATE_ATTRS, &buf))
1490 		return;
1491 
1492 	len = be16_to_cpu(buf.len);
1493 	if (len > SPLPAR_TLB_BIC_MAXLENGTH) {
1494 		pr_warn("%s too large returned buffer %d", __func__, len);
1495 		return;
1496 	}
1497 
1498 	idx = 0;
1499 	while (idx < len) {
1500 		u8 block_shift = buf.val[idx++];
1501 		u32 block_size;
1502 		unsigned int npsize;
1503 
1504 		if (!block_shift)
1505 			break;
1506 
1507 		block_size = 1 << block_shift;
1508 
1509 		for (npsize = buf.val[idx++];
1510 		     npsize > 0 && idx < len; npsize--)
1511 			check_lp_set_hblkrm((unsigned int)buf.val[idx++],
1512 					    block_size);
1513 	}
1514 
1515 	for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
1516 		for (idx = 0; idx < MMU_PAGE_COUNT; idx++)
1517 			if (hblkrm_size[bpsize][idx])
1518 				pr_info("H_BLOCK_REMOVE supports base psize:%d psize:%d block size:%d",
1519 					bpsize, idx, hblkrm_size[bpsize][idx]);
1520 }
1521 
1522 /*
1523  * Take a spinlock around flushes to avoid bouncing the hypervisor tlbie
1524  * lock.
1525  */
1526 static void pSeries_lpar_flush_hash_range(unsigned long number, int local)
1527 {
1528 	unsigned long vpn;
1529 	unsigned long i, pix, rc;
1530 	unsigned long flags = 0;
1531 	struct ppc64_tlb_batch *batch = this_cpu_ptr(&ppc64_tlb_batch);
1532 	int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);
1533 	unsigned long param[PLPAR_HCALL9_BUFSIZE];
1534 	unsigned long index, shift, slot;
1535 	real_pte_t pte;
1536 	int psize, ssize;
1537 
1538 	if (lock_tlbie)
1539 		spin_lock_irqsave(&pSeries_lpar_tlbie_lock, flags);
1540 
1541 	if (is_supported_hlbkrm(batch->psize, batch->psize)) {
1542 		do_block_remove(number, batch, param);
1543 		goto out;
1544 	}
1545 
1546 	psize = batch->psize;
1547 	ssize = batch->ssize;
1548 	pix = 0;
1549 	for (i = 0; i < number; i++) {
1550 		vpn = batch->vpn[i];
1551 		pte = batch->pte[i];
1552 		pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
1553 			slot = compute_slot(pte, vpn, index, shift, ssize);
1554 			if (!firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1555 				/*
1556 				 * lpar doesn't use the passed actual page size
1557 				 */
1558 				pSeries_lpar_hpte_invalidate(slot, vpn, psize,
1559 							     0, ssize, local);
1560 			} else {
1561 				param[pix] = HBR_REQUEST | HBR_AVPN | slot;
1562 				param[pix+1] = hpte_encode_avpn(vpn, psize,
1563 								ssize);
1564 				pix += 2;
1565 				if (pix == 8) {
1566 					rc = plpar_hcall9(H_BULK_REMOVE, param,
1567 						param[0], param[1], param[2],
1568 						param[3], param[4], param[5],
1569 						param[6], param[7]);
1570 					BUG_ON(rc != H_SUCCESS);
1571 					pix = 0;
1572 				}
1573 			}
1574 		} pte_iterate_hashed_end();
1575 	}
1576 	if (pix) {
1577 		param[pix] = HBR_END;
1578 		rc = plpar_hcall9(H_BULK_REMOVE, param, param[0], param[1],
1579 				  param[2], param[3], param[4], param[5],
1580 				  param[6], param[7]);
1581 		BUG_ON(rc != H_SUCCESS);
1582 	}
1583 
1584 out:
1585 	if (lock_tlbie)
1586 		spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1587 }
1588 
1589 static int __init disable_bulk_remove(char *str)
1590 {
1591 	if (strcmp(str, "off") == 0 &&
1592 	    firmware_has_feature(FW_FEATURE_BULK_REMOVE)) {
1593 		pr_info("Disabling BULK_REMOVE firmware feature");
1594 		powerpc_firmware_features &= ~FW_FEATURE_BULK_REMOVE;
1595 	}
1596 	return 1;
1597 }
1598 
1599 __setup("bulk_remove=", disable_bulk_remove);
1600 
1601 #define HPT_RESIZE_TIMEOUT	10000 /* ms */
1602 
1603 struct hpt_resize_state {
1604 	unsigned long shift;
1605 	int commit_rc;
1606 };
1607 
1608 static int pseries_lpar_resize_hpt_commit(void *data)
1609 {
1610 	struct hpt_resize_state *state = data;
1611 
1612 	state->commit_rc = plpar_resize_hpt_commit(0, state->shift);
1613 	if (state->commit_rc != H_SUCCESS)
1614 		return -EIO;
1615 
1616 	/* Hypervisor has transitioned the HTAB, update our globals */
1617 	ppc64_pft_size = state->shift;
1618 	htab_size_bytes = 1UL << ppc64_pft_size;
1619 	htab_hash_mask = (htab_size_bytes >> 7) - 1;
1620 
1621 	return 0;
1622 }
1623 
1624 /*
1625  * Must be called in process context. The caller must hold the
1626  * cpus_lock.
1627  */
1628 static int pseries_lpar_resize_hpt(unsigned long shift)
1629 {
1630 	struct hpt_resize_state state = {
1631 		.shift = shift,
1632 		.commit_rc = H_FUNCTION,
1633 	};
1634 	unsigned int delay, total_delay = 0;
1635 	int rc;
1636 	ktime_t t0, t1, t2;
1637 
1638 	might_sleep();
1639 
1640 	if (!firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1641 		return -ENODEV;
1642 
1643 	pr_info("Attempting to resize HPT to shift %lu\n", shift);
1644 
1645 	t0 = ktime_get();
1646 
1647 	rc = plpar_resize_hpt_prepare(0, shift);
1648 	while (H_IS_LONG_BUSY(rc)) {
1649 		delay = get_longbusy_msecs(rc);
1650 		total_delay += delay;
1651 		if (total_delay > HPT_RESIZE_TIMEOUT) {
1652 			/* prepare with shift==0 cancels an in-progress resize */
1653 			rc = plpar_resize_hpt_prepare(0, 0);
1654 			if (rc != H_SUCCESS)
1655 				pr_warn("Unexpected error %d cancelling timed out HPT resize\n",
1656 				       rc);
1657 			return -ETIMEDOUT;
1658 		}
1659 		msleep(delay);
1660 		rc = plpar_resize_hpt_prepare(0, shift);
1661 	}
1662 
1663 	switch (rc) {
1664 	case H_SUCCESS:
1665 		/* Continue on */
1666 		break;
1667 
1668 	case H_PARAMETER:
1669 		pr_warn("Invalid argument from H_RESIZE_HPT_PREPARE\n");
1670 		return -EINVAL;
1671 	case H_RESOURCE:
1672 		pr_warn("Operation not permitted from H_RESIZE_HPT_PREPARE\n");
1673 		return -EPERM;
1674 	default:
1675 		pr_warn("Unexpected error %d from H_RESIZE_HPT_PREPARE\n", rc);
1676 		return -EIO;
1677 	}
1678 
1679 	t1 = ktime_get();
1680 
1681 	rc = stop_machine_cpuslocked(pseries_lpar_resize_hpt_commit,
1682 				     &state, NULL);
1683 
1684 	t2 = ktime_get();
1685 
1686 	if (rc != 0) {
1687 		switch (state.commit_rc) {
1688 		case H_PTEG_FULL:
1689 			return -ENOSPC;
1690 
1691 		default:
1692 			pr_warn("Unexpected error %d from H_RESIZE_HPT_COMMIT\n",
1693 				state.commit_rc);
1694 			return -EIO;
1695 		};
1696 	}
1697 
1698 	pr_info("HPT resize to shift %lu complete (%lld ms / %lld ms)\n",
1699 		shift, (long long) ktime_ms_delta(t1, t0),
1700 		(long long) ktime_ms_delta(t2, t1));
1701 
1702 	return 0;
1703 }
1704 
1705 void __init hpte_init_pseries(void)
1706 {
1707 	mmu_hash_ops.hpte_invalidate	 = pSeries_lpar_hpte_invalidate;
1708 	mmu_hash_ops.hpte_updatepp	 = pSeries_lpar_hpte_updatepp;
1709 	mmu_hash_ops.hpte_updateboltedpp = pSeries_lpar_hpte_updateboltedpp;
1710 	mmu_hash_ops.hpte_insert	 = pSeries_lpar_hpte_insert;
1711 	mmu_hash_ops.hpte_remove	 = pSeries_lpar_hpte_remove;
1712 	mmu_hash_ops.hpte_removebolted   = pSeries_lpar_hpte_removebolted;
1713 	mmu_hash_ops.flush_hash_range	 = pSeries_lpar_flush_hash_range;
1714 	mmu_hash_ops.hpte_clear_all      = pseries_hpte_clear_all;
1715 	mmu_hash_ops.hugepage_invalidate = pSeries_lpar_hugepage_invalidate;
1716 
1717 	if (firmware_has_feature(FW_FEATURE_HPT_RESIZE))
1718 		mmu_hash_ops.resize_hpt = pseries_lpar_resize_hpt;
1719 
1720 	/*
1721 	 * On POWER9, we need to do a H_REGISTER_PROC_TBL hcall
1722 	 * to inform the hypervisor that we wish to use the HPT.
1723 	 */
1724 	if (cpu_has_feature(CPU_FTR_ARCH_300))
1725 		pseries_lpar_register_process_table(0, 0, 0);
1726 }
1727 #endif /* CONFIG_PPC_64S_HASH_MMU */
1728 
1729 #ifdef CONFIG_PPC_RADIX_MMU
1730 void __init radix_init_pseries(void)
1731 {
1732 	pr_info("Using radix MMU under hypervisor\n");
1733 
1734 	pseries_lpar_register_process_table(__pa(process_tb),
1735 						0, PRTB_SIZE_SHIFT - 12);
1736 }
1737 #endif
1738 
1739 #ifdef CONFIG_PPC_SMLPAR
1740 #define CMO_FREE_HINT_DEFAULT 1
1741 static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
1742 
1743 static 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) {
1749 		pr_info("%s: CMO free page hinting is not active.\n", __func__);
1750 		cmo_free_hint_flag = 0;
1751 		return 1;
1752 	}
1753 
1754 	cmo_free_hint_flag = 1;
1755 	pr_info("%s: CMO free page hinting is active.\n", __func__);
1756 
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 
1765 static 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 
1780 void arch_free_page(struct page *page, int order)
1781 {
1782 	if (radix_enabled())
1783 		return;
1784 	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 }
1789 EXPORT_SYMBOL(arch_free_page);
1790 
1791 #endif /* CONFIG_PPC_SMLPAR */
1792 #endif /* CONFIG_PPC_BOOK3S_64 */
1793 
1794 #ifdef CONFIG_TRACEPOINTS
1795 #ifdef CONFIG_JUMP_LABEL
1796 struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
1797 
1798 int hcall_tracepoint_regfunc(void)
1799 {
1800 	static_key_slow_inc(&hcall_tracepoint_key);
1801 	return 0;
1802 }
1803 
1804 void hcall_tracepoint_unregfunc(void)
1805 {
1806 	static_key_slow_dec(&hcall_tracepoint_key);
1807 }
1808 #else
1809 /*
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 */
1816 extern long hcall_tracepoint_refcount;
1817 
1818 int hcall_tracepoint_regfunc(void)
1819 {
1820 	hcall_tracepoint_refcount++;
1821 	return 0;
1822 }
1823 
1824 void hcall_tracepoint_unregfunc(void)
1825 {
1826 	hcall_tracepoint_refcount--;
1827 }
1828 #endif
1829 
1830 /*
1831  * Keep track of hcall tracing depth and prevent recursion. Warn if any is
1832  * detected because it may indicate a problem. This will not catch all
1833  * problems with tracing code making hcalls, because the tracing might have
1834  * been invoked from a non-hcall, so the first hcall could recurse into it
1835  * without warning here, but this better than nothing.
1836  *
1837  * Hcalls with specific problems being traced should use the _notrace
1838  * plpar_hcall variants.
1839  */
1840 static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
1841 
1842 
1843 notrace void __trace_hcall_entry(unsigned long opcode, unsigned long *args)
1844 {
1845 	unsigned long flags;
1846 	unsigned int *depth;
1847 
1848 	local_irq_save(flags);
1849 
1850 	depth = this_cpu_ptr(&hcall_trace_depth);
1851 
1852 	if (WARN_ON_ONCE(*depth))
1853 		goto out;
1854 
1855 	(*depth)++;
1856 	preempt_disable();
1857 	trace_hcall_entry(opcode, args);
1858 	(*depth)--;
1859 
1860 out:
1861 	local_irq_restore(flags);
1862 }
1863 
1864 notrace void __trace_hcall_exit(long opcode, long retval, unsigned long *retbuf)
1865 {
1866 	unsigned long flags;
1867 	unsigned int *depth;
1868 
1869 	local_irq_save(flags);
1870 
1871 	depth = this_cpu_ptr(&hcall_trace_depth);
1872 
1873 	if (*depth) /* Don't warn again on the way out */
1874 		goto out;
1875 
1876 	(*depth)++;
1877 	trace_hcall_exit(opcode, retval, retbuf);
1878 	preempt_enable();
1879 	(*depth)--;
1880 
1881 out:
1882 	local_irq_restore(flags);
1883 }
1884 #endif
1885 
1886 /**
1887  * h_get_mpp
1888  * H_GET_MPP hcall returns info in 7 parms
1889  */
1890 int h_get_mpp(struct hvcall_mpp_data *mpp_data)
1891 {
1892 	int rc;
1893 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE];
1894 
1895 	rc = plpar_hcall9(H_GET_MPP, retbuf);
1896 
1897 	mpp_data->entitled_mem = retbuf[0];
1898 	mpp_data->mapped_mem = retbuf[1];
1899 
1900 	mpp_data->group_num = (retbuf[2] >> 2 * 8) & 0xffff;
1901 	mpp_data->pool_num = retbuf[2] & 0xffff;
1902 
1903 	mpp_data->mem_weight = (retbuf[3] >> 7 * 8) & 0xff;
1904 	mpp_data->unallocated_mem_weight = (retbuf[3] >> 6 * 8) & 0xff;
1905 	mpp_data->unallocated_entitlement = retbuf[3] & 0xffffffffffffUL;
1906 
1907 	mpp_data->pool_size = retbuf[4];
1908 	mpp_data->loan_request = retbuf[5];
1909 	mpp_data->backing_mem = retbuf[6];
1910 
1911 	return rc;
1912 }
1913 EXPORT_SYMBOL(h_get_mpp);
1914 
1915 int h_get_mpp_x(struct hvcall_mpp_x_data *mpp_x_data)
1916 {
1917 	int rc;
1918 	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = { 0 };
1919 
1920 	rc = plpar_hcall9(H_GET_MPP_X, retbuf);
1921 
1922 	mpp_x_data->coalesced_bytes = retbuf[0];
1923 	mpp_x_data->pool_coalesced_bytes = retbuf[1];
1924 	mpp_x_data->pool_purr_cycles = retbuf[2];
1925 	mpp_x_data->pool_spurr_cycles = retbuf[3];
1926 
1927 	return rc;
1928 }
1929 
1930 #ifdef CONFIG_PPC_64S_HASH_MMU
1931 static unsigned long __init vsid_unscramble(unsigned long vsid, int ssize)
1932 {
1933 	unsigned long protovsid;
1934 	unsigned long va_bits = VA_BITS;
1935 	unsigned long modinv, vsid_modulus;
1936 	unsigned long max_mod_inv, tmp_modinv;
1937 
1938 	if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
1939 		va_bits = 65;
1940 
1941 	if (ssize == MMU_SEGSIZE_256M) {
1942 		modinv = VSID_MULINV_256M;
1943 		vsid_modulus = ((1UL << (va_bits - SID_SHIFT)) - 1);
1944 	} else {
1945 		modinv = VSID_MULINV_1T;
1946 		vsid_modulus = ((1UL << (va_bits - SID_SHIFT_1T)) - 1);
1947 	}
1948 
1949 	/*
1950 	 * vsid outside our range.
1951 	 */
1952 	if (vsid >= vsid_modulus)
1953 		return 0;
1954 
1955 	/*
1956 	 * If modinv is the modular multiplicate inverse of (x % vsid_modulus)
1957 	 * and vsid = (protovsid * x) % vsid_modulus, then we say:
1958 	 *   protovsid = (vsid * modinv) % vsid_modulus
1959 	 */
1960 
1961 	/* Check if (vsid * modinv) overflow (63 bits) */
1962 	max_mod_inv = 0x7fffffffffffffffull / vsid;
1963 	if (modinv < max_mod_inv)
1964 		return (vsid * modinv) % vsid_modulus;
1965 
1966 	tmp_modinv = modinv/max_mod_inv;
1967 	modinv %= max_mod_inv;
1968 
1969 	protovsid = (((vsid * max_mod_inv) % vsid_modulus) * tmp_modinv) % vsid_modulus;
1970 	protovsid = (protovsid + vsid * modinv) % vsid_modulus;
1971 
1972 	return protovsid;
1973 }
1974 
1975 static int __init reserve_vrma_context_id(void)
1976 {
1977 	unsigned long protovsid;
1978 
1979 	/*
1980 	 * Reserve context ids which map to reserved virtual addresses. For now
1981 	 * we only reserve the context id which maps to the VRMA VSID. We ignore
1982 	 * the addresses in "ibm,adjunct-virtual-addresses" because we don't
1983 	 * enable adjunct support via the "ibm,client-architecture-support"
1984 	 * interface.
1985 	 */
1986 	protovsid = vsid_unscramble(VRMA_VSID, MMU_SEGSIZE_1T);
1987 	hash__reserve_context_id(protovsid >> ESID_BITS_1T);
1988 	return 0;
1989 }
1990 machine_device_initcall(pseries, reserve_vrma_context_id);
1991 #endif
1992 
1993 #ifdef CONFIG_DEBUG_FS
1994 /* debugfs file interface for vpa data */
1995 static ssize_t vpa_file_read(struct file *filp, char __user *buf, size_t len,
1996 			      loff_t *pos)
1997 {
1998 	int cpu = (long)filp->private_data;
1999 	struct lppaca *lppaca = &lppaca_of(cpu);
2000 
2001 	return simple_read_from_buffer(buf, len, pos, lppaca,
2002 				sizeof(struct lppaca));
2003 }
2004 
2005 static const struct file_operations vpa_fops = {
2006 	.open		= simple_open,
2007 	.read		= vpa_file_read,
2008 	.llseek		= default_llseek,
2009 };
2010 
2011 static int __init vpa_debugfs_init(void)
2012 {
2013 	char name[16];
2014 	long i;
2015 	struct dentry *vpa_dir;
2016 
2017 	if (!firmware_has_feature(FW_FEATURE_SPLPAR))
2018 		return 0;
2019 
2020 	vpa_dir = debugfs_create_dir("vpa", arch_debugfs_dir);
2021 
2022 	/* set up the per-cpu vpa file*/
2023 	for_each_possible_cpu(i) {
2024 		sprintf(name, "cpu-%ld", i);
2025 		debugfs_create_file(name, 0400, vpa_dir, (void *)i, &vpa_fops);
2026 	}
2027 
2028 	return 0;
2029 }
2030 machine_arch_initcall(pseries, vpa_debugfs_init);
2031 #endif /* CONFIG_DEBUG_FS */
2032