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