File Index
Files and directories
Sign in
FILE

lpar.c

File preview with syntax highlighting.

linux-6.19.14/arch/powerpc/platforms/pseries/lpar.c C
   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/seq_file.h>
  20#include <linux/stop_machine.h>
  21#include <linux/spinlock.h>
  22#include <linux/cpuhotplug.h>
  23#include <linux/workqueue.h>
  24#include <linux/proc_fs.h>
  25#include <linux/pgtable.h>
  26#include <linux/debugfs.h>
  27
  28#include <asm/processor.h>
  29#include <asm/mmu.h>
  30#include <asm/page.h>
  31#include <asm/setup.h>
  32#include <asm/mmu_context.h>
  33#include <asm/iommu.h>
  34#include <asm/tlb.h>
  35#include <asm/cputable.h>
  36#include <asm/papr-sysparm.h>
  37#include <asm/udbg.h>
  38#include <asm/smp.h>
  39#include <asm/trace.h>
  40#include <asm/firmware.h>
  41#include <asm/plpar_wrappers.h>
  42#include <asm/kexec.h>
  43#include <asm/fadump.h>
  44#include <asm/dtl.h>
  45#include <asm/vphn.h>
  46
  47#include "pseries.h"
  48
  49/* Flag bits for H_BULK_REMOVE */
  50#define HBR_REQUEST	0x4000000000000000UL
  51#define HBR_RESPONSE	0x8000000000000000UL
  52#define HBR_END		0xc000000000000000UL
  53#define HBR_AVPN	0x0200000000000000UL
  54#define HBR_ANDCOND	0x0100000000000000UL
  55
  56
  57/* in hvCall.S */
  58EXPORT_SYMBOL(plpar_hcall);
  59EXPORT_SYMBOL(plpar_hcall9);
  60EXPORT_SYMBOL(plpar_hcall_norets);
  61
  62#ifdef CONFIG_PPC_64S_HASH_MMU
  63/*
  64 * H_BLOCK_REMOVE supported block size for this page size in segment who's base
  65 * page size is that page size.
  66 *
  67 * The first index is the segment base page size, the second one is the actual
  68 * page size.
  69 */
  70static int hblkrm_size[MMU_PAGE_COUNT][MMU_PAGE_COUNT] __ro_after_init;
  71#endif
  72
  73/*
  74 * Due to the involved complexity, and that the current hypervisor is only
  75 * returning this value or 0, we are limiting the support of the H_BLOCK_REMOVE
  76 * buffer size to 8 size block.
  77 */
  78#define HBLKRM_SUPPORTED_BLOCK_SIZE 8
  79
  80#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  81static u8 dtl_mask = DTL_LOG_PREEMPT;
  82#else
  83static u8 dtl_mask;
  84#endif
  85
  86void alloc_dtl_buffers(unsigned long *time_limit)
  87{
  88	int cpu;
  89	struct paca_struct *pp;
  90	struct dtl_entry *dtl;
  91
  92	for_each_possible_cpu(cpu) {
  93		pp = paca_ptrs[cpu];
  94		if (pp->dispatch_log)
  95			continue;
  96		dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
  97		if (!dtl) {
  98			pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
  99				cpu);
 100#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
 101			pr_warn("Stolen time statistics will be unreliable\n");
 102#endif
 103			break;
 104		}
 105
 106		pp->dtl_ridx = 0;
 107		pp->dispatch_log = dtl;
 108		pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
 109		pp->dtl_curr = dtl;
 110
 111		if (time_limit && time_after(jiffies, *time_limit)) {
 112			cond_resched();
 113			*time_limit = jiffies + HZ;
 114		}
 115	}
 116}
 117
 118void register_dtl_buffer(int cpu)
 119{
 120	long ret;
 121	struct paca_struct *pp;
 122	struct dtl_entry *dtl;
 123	int hwcpu = get_hard_smp_processor_id(cpu);
 124
 125	pp = paca_ptrs[cpu];
 126	dtl = pp->dispatch_log;
 127	if (dtl && dtl_mask) {
 128		pp->dtl_ridx = 0;
 129		pp->dtl_curr = dtl;
 130		lppaca_of(cpu).dtl_idx = 0;
 131
 132		/* hypervisor reads buffer length from this field */
 133		dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
 134		ret = register_dtl(hwcpu, __pa(dtl));
 135		if (ret)
 136			pr_err("WARNING: DTL registration of cpu %d (hw %d) failed with %ld\n",
 137			       cpu, hwcpu, ret);
 138
 139		lppaca_of(cpu).dtl_enable_mask = dtl_mask;
 140	}
 141}
 142
 143#ifdef CONFIG_PPC_SPLPAR
 144struct dtl_worker {
 145	struct delayed_work work;
 146	int cpu;
 147};
 148
 149struct vcpu_dispatch_data {
 150	int last_disp_cpu;
 151
 152	int total_disp;
 153
 154	int same_cpu_disp;
 155	int same_chip_disp;
 156	int diff_chip_disp;
 157	int far_chip_disp;
 158
 159	int numa_home_disp;
 160	int numa_remote_disp;
 161	int numa_far_disp;
 162};
 163
 164/*
 165 * This represents the number of cpus in the hypervisor. Since there is no
 166 * architected way to discover the number of processors in the host, we
 167 * provision for dealing with NR_CPUS. This is currently 2048 by default, and
 168 * is sufficient for our purposes. This will need to be tweaked if
 169 * CONFIG_NR_CPUS is changed.
 170 */
 171#define NR_CPUS_H	NR_CPUS
 172
 173DECLARE_RWSEM(dtl_access_lock);
 174static DEFINE_PER_CPU(struct vcpu_dispatch_data, vcpu_disp_data);
 175static DEFINE_PER_CPU(u64, dtl_entry_ridx);
 176static DEFINE_PER_CPU(struct dtl_worker, dtl_workers);
 177static enum cpuhp_state dtl_worker_state;
 178static DEFINE_MUTEX(dtl_enable_mutex);
 179static int vcpudispatch_stats_on __read_mostly;
 180static int vcpudispatch_stats_freq = 50;
 181static __be32 *vcpu_associativity, *pcpu_associativity;
 182
 183
 184static void free_dtl_buffers(unsigned long *time_limit)
 185{
 186#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
 187	int cpu;
 188	struct paca_struct *pp;
 189
 190	for_each_possible_cpu(cpu) {
 191		pp = paca_ptrs[cpu];
 192		if (!pp->dispatch_log)
 193			continue;
 194		kmem_cache_free(dtl_cache, pp->dispatch_log);
 195		pp->dtl_ridx = 0;
 196		pp->dispatch_log = NULL;
 197		pp->dispatch_log_end = NULL;
 198		pp->dtl_curr = NULL;
 199
 200		if (time_limit && time_after(jiffies, *time_limit)) {
 201			cond_resched();
 202			*time_limit = jiffies + HZ;
 203		}
 204	}
 205#endif
 206}
 207
 208static int init_cpu_associativity(void)
 209{
 210	vcpu_associativity = kcalloc(num_possible_cpus() / threads_per_core,
 211			VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
 212	pcpu_associativity = kcalloc(NR_CPUS_H / threads_per_core,
 213			VPHN_ASSOC_BUFSIZE * sizeof(__be32), GFP_KERNEL);
 214
 215	if (!vcpu_associativity || !pcpu_associativity) {
 216		pr_err("error allocating memory for associativity information\n");
 217		return -ENOMEM;
 218	}
 219
 220	return 0;
 221}
 222
 223static void destroy_cpu_associativity(void)
 224{
 225	kfree(vcpu_associativity);
 226	kfree(pcpu_associativity);
 227	vcpu_associativity = pcpu_associativity = NULL;
 228}
 229
 230static __be32 *__get_cpu_associativity(int cpu, __be32 *cpu_assoc, int flag)
 231{
 232	__be32 *assoc;
 233	int rc = 0;
 234
 235	assoc = &cpu_assoc[(int)(cpu / threads_per_core) * VPHN_ASSOC_BUFSIZE];
 236	if (!assoc[0]) {
 237		rc = hcall_vphn(cpu, flag, &assoc[0]);
 238		if (rc)
 239			return NULL;
 240	}
 241
 242	return assoc;
 243}
 244
 245static __be32 *get_pcpu_associativity(int cpu)
 246{
 247	return __get_cpu_associativity(cpu, pcpu_associativity, VPHN_FLAG_PCPU);
 248}
 249
 250static __be32 *get_vcpu_associativity(int cpu)
 251{
 252	return __get_cpu_associativity(cpu, vcpu_associativity, VPHN_FLAG_VCPU);
 253}
 254
 255static int cpu_relative_dispatch_distance(int last_disp_cpu, int cur_disp_cpu)
 256{
 257	__be32 *last_disp_cpu_assoc, *cur_disp_cpu_assoc;
 258
 259	if (last_disp_cpu >= NR_CPUS_H || cur_disp_cpu >= NR_CPUS_H)
 260		return -EINVAL;
 261
 262	last_disp_cpu_assoc = get_pcpu_associativity(last_disp_cpu);
 263	cur_disp_cpu_assoc = get_pcpu_associativity(cur_disp_cpu);
 264
 265	if (!last_disp_cpu_assoc || !cur_disp_cpu_assoc)
 266		return -EIO;
 267
 268	return cpu_relative_distance(last_disp_cpu_assoc, cur_disp_cpu_assoc);
 269}
 270
 271static int cpu_home_node_dispatch_distance(int disp_cpu)
 272{
 273	__be32 *disp_cpu_assoc, *vcpu_assoc;
 274	int vcpu_id = smp_processor_id();
 275
 276	if (disp_cpu >= NR_CPUS_H) {
 277		pr_debug_ratelimited("vcpu dispatch cpu %d > %d\n",
 278						disp_cpu, NR_CPUS_H);
 279		return -EINVAL;
 280	}
 281
 282	disp_cpu_assoc = get_pcpu_associativity(disp_cpu);
 283	vcpu_assoc = get_vcpu_associativity(vcpu_id);
 284
 285	if (!disp_cpu_assoc || !vcpu_assoc)
 286		return -EIO;
 287
 288	return cpu_relative_distance(disp_cpu_assoc, vcpu_assoc);
 289}
 290
 291static void update_vcpu_disp_stat(int disp_cpu)
 292{
 293	struct vcpu_dispatch_data *disp;
 294	int distance;
 295
 296	disp = this_cpu_ptr(&vcpu_disp_data);
 297	if (disp->last_disp_cpu == -1) {
 298		disp->last_disp_cpu = disp_cpu;
 299		return;
 300	}
 301
 302	disp->total_disp++;
 303
 304	if (disp->last_disp_cpu == disp_cpu ||
 305		(cpu_first_thread_sibling(disp->last_disp_cpu) ==
 306					cpu_first_thread_sibling(disp_cpu)))
 307		disp->same_cpu_disp++;
 308	else {
 309		distance = cpu_relative_dispatch_distance(disp->last_disp_cpu,
 310								disp_cpu);
 311		if (distance < 0)
 312			pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
 313					smp_processor_id());
 314		else {
 315			switch (distance) {
 316			case 0:
 317				disp->same_chip_disp++;
 318				break;
 319			case 1:
 320				disp->diff_chip_disp++;
 321				break;
 322			case 2:
 323				disp->far_chip_disp++;
 324				break;
 325			default:
 326				pr_debug_ratelimited("vcpudispatch_stats: cpu %d (%d -> %d): unexpected relative dispatch distance %d\n",
 327						 smp_processor_id(),
 328						 disp->last_disp_cpu,
 329						 disp_cpu,
 330						 distance);
 331			}
 332		}
 333	}
 334
 335	distance = cpu_home_node_dispatch_distance(disp_cpu);
 336	if (distance < 0)
 337		pr_debug_ratelimited("vcpudispatch_stats: cpu %d: error determining associativity\n",
 338				smp_processor_id());
 339	else {
 340		switch (distance) {
 341		case 0:
 342			disp->numa_home_disp++;
 343			break;
 344		case 1:
 345			disp->numa_remote_disp++;
 346			break;
 347		case 2:
 348			disp->numa_far_disp++;
 349			break;
 350		default:
 351			pr_debug_ratelimited("vcpudispatch_stats: cpu %d on %d: unexpected numa dispatch distance %d\n",
 352						 smp_processor_id(),
 353						 disp_cpu,
 354						 distance);
 355		}
 356	}
 357
 358	disp->last_disp_cpu = disp_cpu;
 359}
 360
 361static void process_dtl_buffer(struct work_struct *work)
 362{
 363	struct dtl_entry dtle;
 364	u64 i = __this_cpu_read(dtl_entry_ridx);
 365	struct dtl_entry *dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
 366	struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
 367	struct lppaca *vpa = local_paca->lppaca_ptr;
 368	struct dtl_worker *d = container_of(work, struct dtl_worker, work.work);
 369
 370	if (!local_paca->dispatch_log)
 371		return;
 372
 373	/* if we have been migrated away, we cancel ourself */
 374	if (d->cpu != smp_processor_id()) {
 375		pr_debug("vcpudispatch_stats: cpu %d worker migrated -- canceling worker\n",
 376						smp_processor_id());
 377		return;
 378	}
 379
 380	if (i == be64_to_cpu(vpa->dtl_idx))
 381		goto out;
 382
 383	while (i < be64_to_cpu(vpa->dtl_idx)) {
 384		dtle = *dtl;
 385		barrier();
 386		if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
 387			/* buffer has overflowed */
 388			pr_debug_ratelimited("vcpudispatch_stats: cpu %d lost %lld DTL samples\n",
 389				d->cpu,
 390				be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG - i);
 391			i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
 392			dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
 393			continue;
 394		}
 395		update_vcpu_disp_stat(be16_to_cpu(dtle.processor_id));
 396		++i;
 397		++dtl;
 398		if (dtl == dtl_end)
 399			dtl = local_paca->dispatch_log;
 400	}
 401
 402	__this_cpu_write(dtl_entry_ridx, i);
 403
 404out:
 405	schedule_delayed_work_on(d->cpu, to_delayed_work(work),
 406					HZ / vcpudispatch_stats_freq);
 407}
 408
 409static int dtl_worker_online(unsigned int cpu)
 410{
 411	struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
 412
 413	memset(d, 0, sizeof(*d));
 414	INIT_DELAYED_WORK(&d->work, process_dtl_buffer);
 415	d->cpu = cpu;
 416
 417#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
 418	per_cpu(dtl_entry_ridx, cpu) = 0;
 419	register_dtl_buffer(cpu);
 420#else
 421	per_cpu(dtl_entry_ridx, cpu) = be64_to_cpu(lppaca_of(cpu).dtl_idx);
 422#endif
 423
 424	schedule_delayed_work_on(cpu, &d->work, HZ / vcpudispatch_stats_freq);
 425	return 0;
 426}
 427
 428static int dtl_worker_offline(unsigned int cpu)
 429{
 430	struct dtl_worker *d = &per_cpu(dtl_workers, cpu);
 431
 432	cancel_delayed_work_sync(&d->work);
 433
 434#ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
 435	unregister_dtl(get_hard_smp_processor_id(cpu));
 436#endif
 437
 438	return 0;
 439}
 440
 441static void set_global_dtl_mask(u8 mask)
 442{
 443	int cpu;
 444
 445	dtl_mask = mask;
 446	for_each_present_cpu(cpu)
 447		lppaca_of(cpu).dtl_enable_mask = dtl_mask;
 448}
 449
 450static void reset_global_dtl_mask(void)
 451{
 452	int cpu;
 453
 454#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
 455	dtl_mask = DTL_LOG_PREEMPT;
 456#else
 457	dtl_mask = 0;
 458#endif
 459	for_each_present_cpu(cpu)
 460		lppaca_of(cpu).dtl_enable_mask = dtl_mask;
 461}
 462
 463static int dtl_worker_enable(unsigned long *time_limit)
 464{
 465	int rc = 0, state;
 466
 467	if (!down_write_trylock(&dtl_access_lock)) {
 468		rc = -EBUSY;
 469		goto out;
 470	}
 471
 472	set_global_dtl_mask(DTL_LOG_ALL);
 473
 474	/* Setup dtl buffers and register those */
 475	alloc_dtl_buffers(time_limit);
 476
 477	state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/dtl:online",
 478					dtl_worker_online, dtl_worker_offline);
 479	if (state < 0) {
 480		pr_err("vcpudispatch_stats: unable to setup workqueue for DTL processing\n");
 481		free_dtl_buffers(time_limit);
 482		reset_global_dtl_mask();
 483		up_write(&dtl_access_lock);
 484		rc = -EINVAL;
 485		goto out;
 486	}
 487	dtl_worker_state = state;
 488
 489out:
 490	return rc;
 491}
 492
 493static void dtl_worker_disable(unsigned long *time_limit)
 494{
 495	cpuhp_remove_state(dtl_worker_state);
 496	free_dtl_buffers(time_limit);
 497	reset_global_dtl_mask();
 498	up_write(&dtl_access_lock);
 499}
 500
 501static ssize_t vcpudispatch_stats_write(struct file *file, const char __user *p,
 502		size_t count, loff_t *ppos)
 503{
 504	unsigned long time_limit = jiffies + HZ;
 505	struct vcpu_dispatch_data *disp;
 506	int rc, cmd, cpu;
 507	char buf[16];
 508
 509	if (count > 15)
 510		return -EINVAL;
 511
 512	if (copy_from_user(buf, p, count))
 513		return -EFAULT;
 514
 515	buf[count] = 0;
 516	rc = kstrtoint(buf, 0, &cmd);
 517	if (rc || cmd < 0 || cmd > 1) {
 518		pr_err("vcpudispatch_stats: please use 0 to disable or 1 to enable dispatch statistics\n");
 519		return rc ? rc : -EINVAL;
 520	}
 521
 522	mutex_lock(&dtl_enable_mutex);
 523
 524	if ((cmd == 0 && !vcpudispatch_stats_on) ||
 525			(cmd == 1 && vcpudispatch_stats_on))
 526		goto out;
 527
 528	if (cmd) {
 529		rc = init_cpu_associativity();
 530		if (rc) {
 531			destroy_cpu_associativity();
 532			goto out;
 533		}
 534
 535		for_each_possible_cpu(cpu) {
 536			disp = per_cpu_ptr(&vcpu_disp_data, cpu);
 537			memset(disp, 0, sizeof(*disp));
 538			disp->last_disp_cpu = -1;
 539		}
 540
 541		rc = dtl_worker_enable(&time_limit);
 542		if (rc) {
 543			destroy_cpu_associativity();
 544			goto out;
 545		}
 546	} else {
 547		dtl_worker_disable(&time_limit);
 548		destroy_cpu_associativity();
 549	}
 550
 551	vcpudispatch_stats_on = cmd;
 552
 553out:
 554	mutex_unlock(&dtl_enable_mutex);
 555	if (rc)
 556		return rc;
 557	return count;
 558}
 559
 560static int vcpudispatch_stats_display(struct seq_file *p, void *v)
 561{
 562	int cpu;
 563	struct vcpu_dispatch_data *disp;
 564
 565	if (!vcpudispatch_stats_on) {
 566		seq_puts(p, "off\n");
 567		return 0;
 568	}
 569
 570	for_each_online_cpu(cpu) {
 571		disp = per_cpu_ptr(&vcpu_disp_data, cpu);
 572		seq_printf(p, "cpu%d", cpu);
 573		seq_put_decimal_ull(p, " ", disp->total_disp);
 574		seq_put_decimal_ull(p, " ", disp->same_cpu_disp);
 575		seq_put_decimal_ull(p, " ", disp->same_chip_disp);
 576		seq_put_decimal_ull(p, " ", disp->diff_chip_disp);
 577		seq_put_decimal_ull(p, " ", disp->far_chip_disp);
 578		seq_put_decimal_ull(p, " ", disp->numa_home_disp);
 579		seq_put_decimal_ull(p, " ", disp->numa_remote_disp);
 580		seq_put_decimal_ull(p, " ", disp->numa_far_disp);
 581		seq_puts(p, "\n");
 582	}
 583
 584	return 0;
 585}
 586
 587static int vcpudispatch_stats_open(struct inode *inode, struct file *file)
 588{
 589	return single_open(file, vcpudispatch_stats_display, NULL);
 590}
 591
 592static const struct proc_ops vcpudispatch_stats_proc_ops = {
 593	.proc_open	= vcpudispatch_stats_open,
 594	.proc_read	= seq_read,
 595	.proc_write	= vcpudispatch_stats_write,
 596	.proc_lseek	= seq_lseek,
 597	.proc_release	= single_release,
 598};
 599
 600static ssize_t vcpudispatch_stats_freq_write(struct file *file,
 601		const char __user *p, size_t count, loff_t *ppos)
 602{
 603	int rc, freq;
 604	char buf[16];
 605
 606	if (count > 15)
 607		return -EINVAL;
 608
 609	if (copy_from_user(buf, p, count))
 610		return -EFAULT;
 611
 612	buf[count] = 0;
 613	rc = kstrtoint(buf, 0, &freq);
 614	if (rc || freq < 1 || freq > HZ) {
 615		pr_err("vcpudispatch_stats_freq: please specify a frequency between 1 and %d\n",
 616				HZ);
 617		return rc ? rc : -EINVAL;
 618	}
 619
 620	vcpudispatch_stats_freq = freq;
 621
 622	return count;
 623}
 624
 625static int vcpudispatch_stats_freq_display(struct seq_file *p, void *v)
 626{
 627	seq_printf(p, "%d\n", vcpudispatch_stats_freq);
 628	return 0;
 629}
 630
 631static int vcpudispatch_stats_freq_open(struct inode *inode, struct file *file)
 632{
 633	return single_open(file, vcpudispatch_stats_freq_display, NULL);
 634}
 635
 636static const struct proc_ops vcpudispatch_stats_freq_proc_ops = {
 637	.proc_open	= vcpudispatch_stats_freq_open,
 638	.proc_read	= seq_read,
 639	.proc_write	= vcpudispatch_stats_freq_write,
 640	.proc_lseek	= seq_lseek,
 641	.proc_release	= single_release,
 642};
 643
 644static int __init vcpudispatch_stats_procfs_init(void)
 645{
 646	if (!lppaca_shared_proc())
 647		return 0;
 648
 649	if (!proc_create("powerpc/vcpudispatch_stats", 0600, NULL,
 650					&vcpudispatch_stats_proc_ops))
 651		pr_err("vcpudispatch_stats: error creating procfs file\n");
 652	else if (!proc_create("powerpc/vcpudispatch_stats_freq", 0600, NULL,
 653					&vcpudispatch_stats_freq_proc_ops))
 654		pr_err("vcpudispatch_stats_freq: error creating procfs file\n");
 655
 656	return 0;
 657}
 658
 659machine_device_initcall(pseries, vcpudispatch_stats_procfs_init);
 660
 661#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
 662u64 pseries_paravirt_steal_clock(int cpu)
 663{
 664	struct lppaca *lppaca = &lppaca_of(cpu);
 665
 666	/*
 667	 * VPA steal time counters are reported at TB frequency. Hence do a
 668	 * conversion to ns before returning
 669	 */
 670	return tb_to_ns(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
 677void 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
 727static 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
 757static 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
 813static DEFINE_SPINLOCK(pSeries_lpar_tlbie_lock);
 814
 815static 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 */
 848static 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 */
 882static 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 */
 894static 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 */
 924static 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
 959static 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
 987static 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
1013static 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
1038static 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 */
1075static 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 */
1089static 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	}
1099again:
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
1148static 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
1194static 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
1228static 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
1249static 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
1299static 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
1308static 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
1329static 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 */
1350static 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 */
1432static 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
1449static 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
1481void __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 */
1526static 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
1584out:
1585	if (lock_tlbie)
1586		spin_unlock_irqrestore(&pSeries_lpar_tlbie_lock, flags);
1587}
1588
1589static 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
1603struct hpt_resize_state {
1604	unsigned long shift;
1605	int commit_rc;
1606};
1607
1608static 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 */
1628static 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
1705void __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
1730void __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
1741static int cmo_free_hint_flag = CMO_FREE_HINT_DEFAULT;
1742
1743static int __init cmo_free_hint(char *str)
1744{
1745	char *parm;
1746	parm = strstrip(str);
1747
1748	if (strcasecmp(parm, "no") == 0 || strcasecmp(parm, "off") == 0) {
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
1765static void pSeries_set_page_state(struct page *page, int order,
1766				   unsigned long state)
1767{
1768	int i, j;
1769	unsigned long cmo_page_sz, addr;
1770
1771	cmo_page_sz = cmo_get_page_size();
1772	addr = __pa((unsigned long)page_address(page));
1773
1774	for (i = 0; i < (1 << order); i++, addr += PAGE_SIZE) {
1775		for (j = 0; j < PAGE_SIZE; j += cmo_page_sz)
1776			plpar_hcall_norets(H_PAGE_INIT, state, addr + j, 0);
1777	}
1778}
1779
1780void arch_free_page(struct page *page, int order)
1781{
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}
1789EXPORT_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
1796struct static_key hcall_tracepoint_key = STATIC_KEY_INIT;
1797
1798int hcall_tracepoint_regfunc(void)
1799{
1800	static_key_slow_inc(&hcall_tracepoint_key);
1801	return 0;
1802}
1803
1804void 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 */
1816extern long hcall_tracepoint_refcount;
1817
1818int hcall_tracepoint_regfunc(void)
1819{
1820	hcall_tracepoint_refcount++;
1821	return 0;
1822}
1823
1824void 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 */
1840static DEFINE_PER_CPU(unsigned int, hcall_trace_depth);
1841
1842
1843notrace 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
1860out:
1861	local_irq_restore(flags);
1862}
1863
1864notrace 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
1881out:
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 */
1890long h_get_mpp(struct hvcall_mpp_data *mpp_data)
1891{
1892	unsigned long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
1893	long rc;
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}
1913EXPORT_SYMBOL(h_get_mpp);
1914
1915int 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
1931static 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
1975static 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}
1990machine_device_initcall(pseries, reserve_vrma_context_id);
1991#endif
1992
1993#ifdef CONFIG_DEBUG_FS
1994/* debugfs file interface for vpa data */
1995static 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
2005static const struct file_operations vpa_fops = {
2006	.open		= simple_open,
2007	.read		= vpa_file_read,
2008	.llseek		= default_llseek,
2009};
2010
2011static 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}
2030machine_arch_initcall(pseries, vpa_debugfs_init);
2031#endif /* CONFIG_DEBUG_FS */
~/ Read-only file preview.