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