s390/dump: streamline oldmem copy functions
[deliverable/linux.git] / arch / s390 / kernel / smp.c
1 /*
2 * SMP related functions
3 *
4 * Copyright IBM Corp. 1999, 2012
5 * Author(s): Denis Joseph Barrow,
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Heiko Carstens <heiko.carstens@de.ibm.com>,
8 *
9 * based on other smp stuff by
10 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
11 * (c) 1998 Ingo Molnar
12 *
13 * The code outside of smp.c uses logical cpu numbers, only smp.c does
14 * the translation of logical to physical cpu ids. All new code that
15 * operates on physical cpu numbers needs to go into smp.c.
16 */
17
18 #define KMSG_COMPONENT "cpu"
19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
21 #include <linux/workqueue.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/mm.h>
25 #include <linux/err.h>
26 #include <linux/spinlock.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/delay.h>
29 #include <linux/interrupt.h>
30 #include <linux/irqflags.h>
31 #include <linux/cpu.h>
32 #include <linux/slab.h>
33 #include <linux/crash_dump.h>
34 #include <linux/memblock.h>
35 #include <asm/asm-offsets.h>
36 #include <asm/diag.h>
37 #include <asm/switch_to.h>
38 #include <asm/facility.h>
39 #include <asm/ipl.h>
40 #include <asm/setup.h>
41 #include <asm/irq.h>
42 #include <asm/tlbflush.h>
43 #include <asm/vtimer.h>
44 #include <asm/lowcore.h>
45 #include <asm/sclp.h>
46 #include <asm/vdso.h>
47 #include <asm/debug.h>
48 #include <asm/os_info.h>
49 #include <asm/sigp.h>
50 #include <asm/idle.h>
51 #include "entry.h"
52
53 enum {
54 ec_schedule = 0,
55 ec_call_function_single,
56 ec_stop_cpu,
57 };
58
59 enum {
60 CPU_STATE_STANDBY,
61 CPU_STATE_CONFIGURED,
62 };
63
64 static DEFINE_PER_CPU(struct cpu *, cpu_device);
65
66 struct pcpu {
67 struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
68 unsigned long ec_mask; /* bit mask for ec_xxx functions */
69 signed char state; /* physical cpu state */
70 signed char polarization; /* physical polarization */
71 u16 address; /* physical cpu address */
72 };
73
74 static u8 boot_core_type;
75 static struct pcpu pcpu_devices[NR_CPUS];
76
77 unsigned int smp_cpu_mt_shift;
78 EXPORT_SYMBOL(smp_cpu_mt_shift);
79
80 unsigned int smp_cpu_mtid;
81 EXPORT_SYMBOL(smp_cpu_mtid);
82
83 static unsigned int smp_max_threads __initdata = -1U;
84
85 static int __init early_nosmt(char *s)
86 {
87 smp_max_threads = 1;
88 return 0;
89 }
90 early_param("nosmt", early_nosmt);
91
92 static int __init early_smt(char *s)
93 {
94 get_option(&s, &smp_max_threads);
95 return 0;
96 }
97 early_param("smt", early_smt);
98
99 /*
100 * The smp_cpu_state_mutex must be held when changing the state or polarization
101 * member of a pcpu data structure within the pcpu_devices arreay.
102 */
103 DEFINE_MUTEX(smp_cpu_state_mutex);
104
105 /*
106 * Signal processor helper functions.
107 */
108 static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm,
109 u32 *status)
110 {
111 int cc;
112
113 while (1) {
114 cc = __pcpu_sigp(addr, order, parm, NULL);
115 if (cc != SIGP_CC_BUSY)
116 return cc;
117 cpu_relax();
118 }
119 }
120
121 static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
122 {
123 int cc, retry;
124
125 for (retry = 0; ; retry++) {
126 cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
127 if (cc != SIGP_CC_BUSY)
128 break;
129 if (retry >= 3)
130 udelay(10);
131 }
132 return cc;
133 }
134
135 static inline int pcpu_stopped(struct pcpu *pcpu)
136 {
137 u32 uninitialized_var(status);
138
139 if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
140 0, &status) != SIGP_CC_STATUS_STORED)
141 return 0;
142 return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
143 }
144
145 static inline int pcpu_running(struct pcpu *pcpu)
146 {
147 if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
148 0, NULL) != SIGP_CC_STATUS_STORED)
149 return 1;
150 /* Status stored condition code is equivalent to cpu not running. */
151 return 0;
152 }
153
154 /*
155 * Find struct pcpu by cpu address.
156 */
157 static struct pcpu *pcpu_find_address(const struct cpumask *mask, u16 address)
158 {
159 int cpu;
160
161 for_each_cpu(cpu, mask)
162 if (pcpu_devices[cpu].address == address)
163 return pcpu_devices + cpu;
164 return NULL;
165 }
166
167 static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
168 {
169 int order;
170
171 if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
172 return;
173 order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
174 pcpu_sigp_retry(pcpu, order, 0);
175 }
176
177 #define ASYNC_FRAME_OFFSET (ASYNC_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
178 #define PANIC_FRAME_OFFSET (PAGE_SIZE - STACK_FRAME_OVERHEAD - __PT_SIZE)
179
180 static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
181 {
182 unsigned long async_stack, panic_stack;
183 struct _lowcore *lc;
184
185 if (pcpu != &pcpu_devices[0]) {
186 pcpu->lowcore = (struct _lowcore *)
187 __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
188 async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
189 panic_stack = __get_free_page(GFP_KERNEL);
190 if (!pcpu->lowcore || !panic_stack || !async_stack)
191 goto out;
192 } else {
193 async_stack = pcpu->lowcore->async_stack - ASYNC_FRAME_OFFSET;
194 panic_stack = pcpu->lowcore->panic_stack - PANIC_FRAME_OFFSET;
195 }
196 lc = pcpu->lowcore;
197 memcpy(lc, &S390_lowcore, 512);
198 memset((char *) lc + 512, 0, sizeof(*lc) - 512);
199 lc->async_stack = async_stack + ASYNC_FRAME_OFFSET;
200 lc->panic_stack = panic_stack + PANIC_FRAME_OFFSET;
201 lc->cpu_nr = cpu;
202 lc->spinlock_lockval = arch_spin_lockval(cpu);
203 if (MACHINE_HAS_VX)
204 lc->vector_save_area_addr =
205 (unsigned long) &lc->vector_save_area;
206 if (vdso_alloc_per_cpu(lc))
207 goto out;
208 lowcore_ptr[cpu] = lc;
209 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
210 return 0;
211 out:
212 if (pcpu != &pcpu_devices[0]) {
213 free_page(panic_stack);
214 free_pages(async_stack, ASYNC_ORDER);
215 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
216 }
217 return -ENOMEM;
218 }
219
220 #ifdef CONFIG_HOTPLUG_CPU
221
222 static void pcpu_free_lowcore(struct pcpu *pcpu)
223 {
224 pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
225 lowcore_ptr[pcpu - pcpu_devices] = NULL;
226 vdso_free_per_cpu(pcpu->lowcore);
227 if (pcpu == &pcpu_devices[0])
228 return;
229 free_page(pcpu->lowcore->panic_stack-PANIC_FRAME_OFFSET);
230 free_pages(pcpu->lowcore->async_stack-ASYNC_FRAME_OFFSET, ASYNC_ORDER);
231 free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
232 }
233
234 #endif /* CONFIG_HOTPLUG_CPU */
235
236 static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
237 {
238 struct _lowcore *lc = pcpu->lowcore;
239
240 if (MACHINE_HAS_TLB_LC)
241 cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
242 cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
243 atomic_inc(&init_mm.context.attach_count);
244 lc->cpu_nr = cpu;
245 lc->spinlock_lockval = arch_spin_lockval(cpu);
246 lc->percpu_offset = __per_cpu_offset[cpu];
247 lc->kernel_asce = S390_lowcore.kernel_asce;
248 lc->machine_flags = S390_lowcore.machine_flags;
249 lc->user_timer = lc->system_timer = lc->steal_timer = 0;
250 __ctl_store(lc->cregs_save_area, 0, 15);
251 save_access_regs((unsigned int *) lc->access_regs_save_area);
252 memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
253 MAX_FACILITY_BIT/8);
254 }
255
256 static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
257 {
258 struct _lowcore *lc = pcpu->lowcore;
259 struct thread_info *ti = task_thread_info(tsk);
260
261 lc->kernel_stack = (unsigned long) task_stack_page(tsk)
262 + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
263 lc->thread_info = (unsigned long) task_thread_info(tsk);
264 lc->current_task = (unsigned long) tsk;
265 lc->lpp = LPP_MAGIC;
266 lc->current_pid = tsk->pid;
267 lc->user_timer = ti->user_timer;
268 lc->system_timer = ti->system_timer;
269 lc->steal_timer = 0;
270 }
271
272 static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
273 {
274 struct _lowcore *lc = pcpu->lowcore;
275
276 lc->restart_stack = lc->kernel_stack;
277 lc->restart_fn = (unsigned long) func;
278 lc->restart_data = (unsigned long) data;
279 lc->restart_source = -1UL;
280 pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
281 }
282
283 /*
284 * Call function via PSW restart on pcpu and stop the current cpu.
285 */
286 static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
287 void *data, unsigned long stack)
288 {
289 struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
290 unsigned long source_cpu = stap();
291
292 __load_psw_mask(PSW_KERNEL_BITS);
293 if (pcpu->address == source_cpu)
294 func(data); /* should not return */
295 /* Stop target cpu (if func returns this stops the current cpu). */
296 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
297 /* Restart func on the target cpu and stop the current cpu. */
298 mem_assign_absolute(lc->restart_stack, stack);
299 mem_assign_absolute(lc->restart_fn, (unsigned long) func);
300 mem_assign_absolute(lc->restart_data, (unsigned long) data);
301 mem_assign_absolute(lc->restart_source, source_cpu);
302 asm volatile(
303 "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
304 " brc 2,0b # busy, try again\n"
305 "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
306 " brc 2,1b # busy, try again\n"
307 : : "d" (pcpu->address), "d" (source_cpu),
308 "K" (SIGP_RESTART), "K" (SIGP_STOP)
309 : "0", "1", "cc");
310 for (;;) ;
311 }
312
313 /*
314 * Enable additional logical cpus for multi-threading.
315 */
316 static int pcpu_set_smt(unsigned int mtid)
317 {
318 register unsigned long reg1 asm ("1") = (unsigned long) mtid;
319 int cc;
320
321 if (smp_cpu_mtid == mtid)
322 return 0;
323 asm volatile(
324 " sigp %1,0,%2 # sigp set multi-threading\n"
325 " ipm %0\n"
326 " srl %0,28\n"
327 : "=d" (cc) : "d" (reg1), "K" (SIGP_SET_MULTI_THREADING)
328 : "cc");
329 if (cc == 0) {
330 smp_cpu_mtid = mtid;
331 smp_cpu_mt_shift = 0;
332 while (smp_cpu_mtid >= (1U << smp_cpu_mt_shift))
333 smp_cpu_mt_shift++;
334 pcpu_devices[0].address = stap();
335 }
336 return cc;
337 }
338
339 /*
340 * Call function on an online CPU.
341 */
342 void smp_call_online_cpu(void (*func)(void *), void *data)
343 {
344 struct pcpu *pcpu;
345
346 /* Use the current cpu if it is online. */
347 pcpu = pcpu_find_address(cpu_online_mask, stap());
348 if (!pcpu)
349 /* Use the first online cpu. */
350 pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
351 pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
352 }
353
354 /*
355 * Call function on the ipl CPU.
356 */
357 void smp_call_ipl_cpu(void (*func)(void *), void *data)
358 {
359 pcpu_delegate(&pcpu_devices[0], func, data,
360 pcpu_devices->lowcore->panic_stack -
361 PANIC_FRAME_OFFSET + PAGE_SIZE);
362 }
363
364 int smp_find_processor_id(u16 address)
365 {
366 int cpu;
367
368 for_each_present_cpu(cpu)
369 if (pcpu_devices[cpu].address == address)
370 return cpu;
371 return -1;
372 }
373
374 int smp_vcpu_scheduled(int cpu)
375 {
376 return pcpu_running(pcpu_devices + cpu);
377 }
378
379 void smp_yield_cpu(int cpu)
380 {
381 if (MACHINE_HAS_DIAG9C) {
382 diag_stat_inc_norecursion(DIAG_STAT_X09C);
383 asm volatile("diag %0,0,0x9c"
384 : : "d" (pcpu_devices[cpu].address));
385 } else if (MACHINE_HAS_DIAG44) {
386 diag_stat_inc_norecursion(DIAG_STAT_X044);
387 asm volatile("diag 0,0,0x44");
388 }
389 }
390
391 /*
392 * Send cpus emergency shutdown signal. This gives the cpus the
393 * opportunity to complete outstanding interrupts.
394 */
395 static void smp_emergency_stop(cpumask_t *cpumask)
396 {
397 u64 end;
398 int cpu;
399
400 end = get_tod_clock() + (1000000UL << 12);
401 for_each_cpu(cpu, cpumask) {
402 struct pcpu *pcpu = pcpu_devices + cpu;
403 set_bit(ec_stop_cpu, &pcpu->ec_mask);
404 while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
405 0, NULL) == SIGP_CC_BUSY &&
406 get_tod_clock() < end)
407 cpu_relax();
408 }
409 while (get_tod_clock() < end) {
410 for_each_cpu(cpu, cpumask)
411 if (pcpu_stopped(pcpu_devices + cpu))
412 cpumask_clear_cpu(cpu, cpumask);
413 if (cpumask_empty(cpumask))
414 break;
415 cpu_relax();
416 }
417 }
418
419 /*
420 * Stop all cpus but the current one.
421 */
422 void smp_send_stop(void)
423 {
424 cpumask_t cpumask;
425 int cpu;
426
427 /* Disable all interrupts/machine checks */
428 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
429 trace_hardirqs_off();
430
431 debug_set_critical();
432 cpumask_copy(&cpumask, cpu_online_mask);
433 cpumask_clear_cpu(smp_processor_id(), &cpumask);
434
435 if (oops_in_progress)
436 smp_emergency_stop(&cpumask);
437
438 /* stop all processors */
439 for_each_cpu(cpu, &cpumask) {
440 struct pcpu *pcpu = pcpu_devices + cpu;
441 pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
442 while (!pcpu_stopped(pcpu))
443 cpu_relax();
444 }
445 }
446
447 /*
448 * This is the main routine where commands issued by other
449 * cpus are handled.
450 */
451 static void smp_handle_ext_call(void)
452 {
453 unsigned long bits;
454
455 /* handle bit signal external calls */
456 bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
457 if (test_bit(ec_stop_cpu, &bits))
458 smp_stop_cpu();
459 if (test_bit(ec_schedule, &bits))
460 scheduler_ipi();
461 if (test_bit(ec_call_function_single, &bits))
462 generic_smp_call_function_single_interrupt();
463 }
464
465 static void do_ext_call_interrupt(struct ext_code ext_code,
466 unsigned int param32, unsigned long param64)
467 {
468 inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
469 smp_handle_ext_call();
470 }
471
472 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
473 {
474 int cpu;
475
476 for_each_cpu(cpu, mask)
477 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
478 }
479
480 void arch_send_call_function_single_ipi(int cpu)
481 {
482 pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
483 }
484
485 /*
486 * this function sends a 'reschedule' IPI to another CPU.
487 * it goes straight through and wastes no time serializing
488 * anything. Worst case is that we lose a reschedule ...
489 */
490 void smp_send_reschedule(int cpu)
491 {
492 pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
493 }
494
495 /*
496 * parameter area for the set/clear control bit callbacks
497 */
498 struct ec_creg_mask_parms {
499 unsigned long orval;
500 unsigned long andval;
501 int cr;
502 };
503
504 /*
505 * callback for setting/clearing control bits
506 */
507 static void smp_ctl_bit_callback(void *info)
508 {
509 struct ec_creg_mask_parms *pp = info;
510 unsigned long cregs[16];
511
512 __ctl_store(cregs, 0, 15);
513 cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
514 __ctl_load(cregs, 0, 15);
515 }
516
517 /*
518 * Set a bit in a control register of all cpus
519 */
520 void smp_ctl_set_bit(int cr, int bit)
521 {
522 struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
523
524 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
525 }
526 EXPORT_SYMBOL(smp_ctl_set_bit);
527
528 /*
529 * Clear a bit in a control register of all cpus
530 */
531 void smp_ctl_clear_bit(int cr, int bit)
532 {
533 struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
534
535 on_each_cpu(smp_ctl_bit_callback, &parms, 1);
536 }
537 EXPORT_SYMBOL(smp_ctl_clear_bit);
538
539 #ifdef CONFIG_CRASH_DUMP
540
541 static void __init __smp_store_cpu_state(struct save_area_ext *sa_ext,
542 u16 address, int is_boot_cpu)
543 {
544 void *lc = (void *)(unsigned long) store_prefix();
545 unsigned long vx_sa;
546
547 if (is_boot_cpu) {
548 /* Copy the registers of the boot CPU. */
549 copy_oldmem_kernel(&sa_ext->sa, (void *) SAVE_AREA_BASE,
550 sizeof(sa_ext->sa));
551 if (MACHINE_HAS_VX)
552 save_vx_regs_safe(sa_ext->vx_regs);
553 return;
554 }
555 /* Get the registers of a non-boot cpu. */
556 __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
557 memcpy_real(&sa_ext->sa, lc + SAVE_AREA_BASE, sizeof(sa_ext->sa));
558 if (!MACHINE_HAS_VX)
559 return;
560 /* Get the VX registers */
561 vx_sa = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
562 if (!vx_sa)
563 panic("could not allocate memory for VX save area\n");
564 __pcpu_sigp_relax(address, SIGP_STORE_ADDITIONAL_STATUS, vx_sa, NULL);
565 memcpy(sa_ext->vx_regs, (void *) vx_sa, sizeof(sa_ext->vx_regs));
566 memblock_free(vx_sa, PAGE_SIZE);
567 }
568
569 int smp_store_status(int cpu)
570 {
571 unsigned long vx_sa;
572 struct pcpu *pcpu;
573
574 pcpu = pcpu_devices + cpu;
575 if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
576 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
577 return -EIO;
578 if (!MACHINE_HAS_VX)
579 return 0;
580 vx_sa = __pa(pcpu->lowcore->vector_save_area_addr);
581 __pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
582 vx_sa, NULL);
583 return 0;
584 }
585
586 #endif /* CONFIG_CRASH_DUMP */
587
588 /*
589 * Collect CPU state of the previous, crashed system.
590 * There are four cases:
591 * 1) standard zfcp dump
592 * condition: OLDMEM_BASE == NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
593 * The state for all CPUs except the boot CPU needs to be collected
594 * with sigp stop-and-store-status. The boot CPU state is located in
595 * the absolute lowcore of the memory stored in the HSA. The zcore code
596 * will allocate the save area and copy the boot CPU state from the HSA.
597 * 2) stand-alone kdump for SCSI (zfcp dump with swapped memory)
598 * condition: OLDMEM_BASE != NULL && ipl_info.type == IPL_TYPE_FCP_DUMP
599 * The state for all CPUs except the boot CPU needs to be collected
600 * with sigp stop-and-store-status. The firmware or the boot-loader
601 * stored the registers of the boot CPU in the absolute lowcore in the
602 * memory of the old system.
603 * 3) kdump and the old kernel did not store the CPU state,
604 * or stand-alone kdump for DASD
605 * condition: OLDMEM_BASE != NULL && !is_kdump_kernel()
606 * The state for all CPUs except the boot CPU needs to be collected
607 * with sigp stop-and-store-status. The kexec code or the boot-loader
608 * stored the registers of the boot CPU in the memory of the old system.
609 * 4) kdump and the old kernel stored the CPU state
610 * condition: OLDMEM_BASE != NULL && is_kdump_kernel()
611 * This case does not exist for s390 anymore, setup_arch explicitly
612 * deactivates the elfcorehdr= kernel parameter
613 */
614 void __init smp_save_dump_cpus(void)
615 {
616 #ifdef CONFIG_CRASH_DUMP
617 int addr, cpu, boot_cpu_addr, max_cpu_addr;
618 struct save_area_ext *sa_ext;
619 bool is_boot_cpu;
620
621 if (!(OLDMEM_BASE || ipl_info.type == IPL_TYPE_FCP_DUMP))
622 /* No previous system present, normal boot. */
623 return;
624 /* Set multi-threading state to the previous system. */
625 pcpu_set_smt(sclp.mtid_prev);
626 max_cpu_addr = SCLP_MAX_CORES << sclp.mtid_prev;
627 for (cpu = 0, addr = 0; addr <= max_cpu_addr; addr++) {
628 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0, NULL) ==
629 SIGP_CC_NOT_OPERATIONAL)
630 continue;
631 cpu += 1;
632 }
633 dump_save_areas.areas = (void *)memblock_alloc(sizeof(void *) * cpu, 8);
634 dump_save_areas.count = cpu;
635 boot_cpu_addr = stap();
636 for (cpu = 0, addr = 0; addr <= max_cpu_addr; addr++) {
637 if (__pcpu_sigp_relax(addr, SIGP_SENSE, 0, NULL) ==
638 SIGP_CC_NOT_OPERATIONAL)
639 continue;
640 sa_ext = (void *) memblock_alloc(sizeof(*sa_ext), 8);
641 dump_save_areas.areas[cpu] = sa_ext;
642 if (!sa_ext)
643 panic("could not allocate memory for save area\n");
644 is_boot_cpu = (addr == boot_cpu_addr);
645 cpu += 1;
646 if (is_boot_cpu && !OLDMEM_BASE)
647 /* Skip boot CPU for standard zfcp dump. */
648 continue;
649 /* Get state for this CPU. */
650 __smp_store_cpu_state(sa_ext, addr, is_boot_cpu);
651 }
652 diag308_reset();
653 pcpu_set_smt(0);
654 #endif /* CONFIG_CRASH_DUMP */
655 }
656
657 void smp_cpu_set_polarization(int cpu, int val)
658 {
659 pcpu_devices[cpu].polarization = val;
660 }
661
662 int smp_cpu_get_polarization(int cpu)
663 {
664 return pcpu_devices[cpu].polarization;
665 }
666
667 static struct sclp_core_info *smp_get_core_info(void)
668 {
669 static int use_sigp_detection;
670 struct sclp_core_info *info;
671 int address;
672
673 info = kzalloc(sizeof(*info), GFP_KERNEL);
674 if (info && (use_sigp_detection || sclp_get_core_info(info))) {
675 use_sigp_detection = 1;
676 for (address = 0;
677 address < (SCLP_MAX_CORES << smp_cpu_mt_shift);
678 address += (1U << smp_cpu_mt_shift)) {
679 if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
680 SIGP_CC_NOT_OPERATIONAL)
681 continue;
682 info->core[info->configured].core_id =
683 address >> smp_cpu_mt_shift;
684 info->configured++;
685 }
686 info->combined = info->configured;
687 }
688 return info;
689 }
690
691 static int smp_add_present_cpu(int cpu);
692
693 static int __smp_rescan_cpus(struct sclp_core_info *info, int sysfs_add)
694 {
695 struct pcpu *pcpu;
696 cpumask_t avail;
697 int cpu, nr, i, j;
698 u16 address;
699
700 nr = 0;
701 cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
702 cpu = cpumask_first(&avail);
703 for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
704 if (sclp.has_core_type && info->core[i].type != boot_core_type)
705 continue;
706 address = info->core[i].core_id << smp_cpu_mt_shift;
707 for (j = 0; j <= smp_cpu_mtid; j++) {
708 if (pcpu_find_address(cpu_present_mask, address + j))
709 continue;
710 pcpu = pcpu_devices + cpu;
711 pcpu->address = address + j;
712 pcpu->state =
713 (cpu >= info->configured*(smp_cpu_mtid + 1)) ?
714 CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
715 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
716 set_cpu_present(cpu, true);
717 if (sysfs_add && smp_add_present_cpu(cpu) != 0)
718 set_cpu_present(cpu, false);
719 else
720 nr++;
721 cpu = cpumask_next(cpu, &avail);
722 if (cpu >= nr_cpu_ids)
723 break;
724 }
725 }
726 return nr;
727 }
728
729 static void __init smp_detect_cpus(void)
730 {
731 unsigned int cpu, mtid, c_cpus, s_cpus;
732 struct sclp_core_info *info;
733 u16 address;
734
735 /* Get CPU information */
736 info = smp_get_core_info();
737 if (!info)
738 panic("smp_detect_cpus failed to allocate memory\n");
739
740 /* Find boot CPU type */
741 if (sclp.has_core_type) {
742 address = stap();
743 for (cpu = 0; cpu < info->combined; cpu++)
744 if (info->core[cpu].core_id == address) {
745 /* The boot cpu dictates the cpu type. */
746 boot_core_type = info->core[cpu].type;
747 break;
748 }
749 if (cpu >= info->combined)
750 panic("Could not find boot CPU type");
751 }
752
753 /* Set multi-threading state for the current system */
754 mtid = boot_core_type ? sclp.mtid : sclp.mtid_cp;
755 mtid = (mtid < smp_max_threads) ? mtid : smp_max_threads - 1;
756 pcpu_set_smt(mtid);
757
758 /* Print number of CPUs */
759 c_cpus = s_cpus = 0;
760 for (cpu = 0; cpu < info->combined; cpu++) {
761 if (sclp.has_core_type &&
762 info->core[cpu].type != boot_core_type)
763 continue;
764 if (cpu < info->configured)
765 c_cpus += smp_cpu_mtid + 1;
766 else
767 s_cpus += smp_cpu_mtid + 1;
768 }
769 pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
770
771 /* Add CPUs present at boot */
772 get_online_cpus();
773 __smp_rescan_cpus(info, 0);
774 put_online_cpus();
775 kfree(info);
776 }
777
778 /*
779 * Activate a secondary processor.
780 */
781 static void smp_start_secondary(void *cpuvoid)
782 {
783 S390_lowcore.last_update_clock = get_tod_clock();
784 S390_lowcore.restart_stack = (unsigned long) restart_stack;
785 S390_lowcore.restart_fn = (unsigned long) do_restart;
786 S390_lowcore.restart_data = 0;
787 S390_lowcore.restart_source = -1UL;
788 restore_access_regs(S390_lowcore.access_regs_save_area);
789 __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
790 __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
791 cpu_init();
792 preempt_disable();
793 init_cpu_timer();
794 vtime_init();
795 pfault_init();
796 notify_cpu_starting(smp_processor_id());
797 set_cpu_online(smp_processor_id(), true);
798 inc_irq_stat(CPU_RST);
799 local_irq_enable();
800 cpu_startup_entry(CPUHP_ONLINE);
801 }
802
803 /* Upping and downing of CPUs */
804 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
805 {
806 struct pcpu *pcpu;
807 int base, i, rc;
808
809 pcpu = pcpu_devices + cpu;
810 if (pcpu->state != CPU_STATE_CONFIGURED)
811 return -EIO;
812 base = cpu - (cpu % (smp_cpu_mtid + 1));
813 for (i = 0; i <= smp_cpu_mtid; i++) {
814 if (base + i < nr_cpu_ids)
815 if (cpu_online(base + i))
816 break;
817 }
818 /*
819 * If this is the first CPU of the core to get online
820 * do an initial CPU reset.
821 */
822 if (i > smp_cpu_mtid &&
823 pcpu_sigp_retry(pcpu_devices + base, SIGP_INITIAL_CPU_RESET, 0) !=
824 SIGP_CC_ORDER_CODE_ACCEPTED)
825 return -EIO;
826
827 rc = pcpu_alloc_lowcore(pcpu, cpu);
828 if (rc)
829 return rc;
830 pcpu_prepare_secondary(pcpu, cpu);
831 pcpu_attach_task(pcpu, tidle);
832 pcpu_start_fn(pcpu, smp_start_secondary, NULL);
833 /* Wait until cpu puts itself in the online & active maps */
834 while (!cpu_online(cpu) || !cpu_active(cpu))
835 cpu_relax();
836 return 0;
837 }
838
839 static unsigned int setup_possible_cpus __initdata;
840
841 static int __init _setup_possible_cpus(char *s)
842 {
843 get_option(&s, &setup_possible_cpus);
844 return 0;
845 }
846 early_param("possible_cpus", _setup_possible_cpus);
847
848 #ifdef CONFIG_HOTPLUG_CPU
849
850 int __cpu_disable(void)
851 {
852 unsigned long cregs[16];
853
854 /* Handle possible pending IPIs */
855 smp_handle_ext_call();
856 set_cpu_online(smp_processor_id(), false);
857 /* Disable pseudo page faults on this cpu. */
858 pfault_fini();
859 /* Disable interrupt sources via control register. */
860 __ctl_store(cregs, 0, 15);
861 cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
862 cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
863 cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
864 __ctl_load(cregs, 0, 15);
865 clear_cpu_flag(CIF_NOHZ_DELAY);
866 return 0;
867 }
868
869 void __cpu_die(unsigned int cpu)
870 {
871 struct pcpu *pcpu;
872
873 /* Wait until target cpu is down */
874 pcpu = pcpu_devices + cpu;
875 while (!pcpu_stopped(pcpu))
876 cpu_relax();
877 pcpu_free_lowcore(pcpu);
878 atomic_dec(&init_mm.context.attach_count);
879 cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
880 if (MACHINE_HAS_TLB_LC)
881 cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
882 }
883
884 void __noreturn cpu_die(void)
885 {
886 idle_task_exit();
887 pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
888 for (;;) ;
889 }
890
891 #endif /* CONFIG_HOTPLUG_CPU */
892
893 void __init smp_fill_possible_mask(void)
894 {
895 unsigned int possible, sclp_max, cpu;
896
897 sclp_max = max(sclp.mtid, sclp.mtid_cp) + 1;
898 sclp_max = min(smp_max_threads, sclp_max);
899 sclp_max = sclp.max_cores * sclp_max ?: nr_cpu_ids;
900 possible = setup_possible_cpus ?: nr_cpu_ids;
901 possible = min(possible, sclp_max);
902 for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
903 set_cpu_possible(cpu, true);
904 }
905
906 void __init smp_prepare_cpus(unsigned int max_cpus)
907 {
908 /* request the 0x1201 emergency signal external interrupt */
909 if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
910 panic("Couldn't request external interrupt 0x1201");
911 /* request the 0x1202 external call external interrupt */
912 if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
913 panic("Couldn't request external interrupt 0x1202");
914 smp_detect_cpus();
915 }
916
917 void __init smp_prepare_boot_cpu(void)
918 {
919 struct pcpu *pcpu = pcpu_devices;
920
921 pcpu->state = CPU_STATE_CONFIGURED;
922 pcpu->address = stap();
923 pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
924 S390_lowcore.percpu_offset = __per_cpu_offset[0];
925 smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
926 set_cpu_present(0, true);
927 set_cpu_online(0, true);
928 }
929
930 void __init smp_cpus_done(unsigned int max_cpus)
931 {
932 }
933
934 void __init smp_setup_processor_id(void)
935 {
936 S390_lowcore.cpu_nr = 0;
937 S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
938 }
939
940 /*
941 * the frequency of the profiling timer can be changed
942 * by writing a multiplier value into /proc/profile.
943 *
944 * usually you want to run this on all CPUs ;)
945 */
946 int setup_profiling_timer(unsigned int multiplier)
947 {
948 return 0;
949 }
950
951 #ifdef CONFIG_HOTPLUG_CPU
952 static ssize_t cpu_configure_show(struct device *dev,
953 struct device_attribute *attr, char *buf)
954 {
955 ssize_t count;
956
957 mutex_lock(&smp_cpu_state_mutex);
958 count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
959 mutex_unlock(&smp_cpu_state_mutex);
960 return count;
961 }
962
963 static ssize_t cpu_configure_store(struct device *dev,
964 struct device_attribute *attr,
965 const char *buf, size_t count)
966 {
967 struct pcpu *pcpu;
968 int cpu, val, rc, i;
969 char delim;
970
971 if (sscanf(buf, "%d %c", &val, &delim) != 1)
972 return -EINVAL;
973 if (val != 0 && val != 1)
974 return -EINVAL;
975 get_online_cpus();
976 mutex_lock(&smp_cpu_state_mutex);
977 rc = -EBUSY;
978 /* disallow configuration changes of online cpus and cpu 0 */
979 cpu = dev->id;
980 cpu -= cpu % (smp_cpu_mtid + 1);
981 if (cpu == 0)
982 goto out;
983 for (i = 0; i <= smp_cpu_mtid; i++)
984 if (cpu_online(cpu + i))
985 goto out;
986 pcpu = pcpu_devices + cpu;
987 rc = 0;
988 switch (val) {
989 case 0:
990 if (pcpu->state != CPU_STATE_CONFIGURED)
991 break;
992 rc = sclp_core_deconfigure(pcpu->address >> smp_cpu_mt_shift);
993 if (rc)
994 break;
995 for (i = 0; i <= smp_cpu_mtid; i++) {
996 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
997 continue;
998 pcpu[i].state = CPU_STATE_STANDBY;
999 smp_cpu_set_polarization(cpu + i,
1000 POLARIZATION_UNKNOWN);
1001 }
1002 topology_expect_change();
1003 break;
1004 case 1:
1005 if (pcpu->state != CPU_STATE_STANDBY)
1006 break;
1007 rc = sclp_core_configure(pcpu->address >> smp_cpu_mt_shift);
1008 if (rc)
1009 break;
1010 for (i = 0; i <= smp_cpu_mtid; i++) {
1011 if (cpu + i >= nr_cpu_ids || !cpu_present(cpu + i))
1012 continue;
1013 pcpu[i].state = CPU_STATE_CONFIGURED;
1014 smp_cpu_set_polarization(cpu + i,
1015 POLARIZATION_UNKNOWN);
1016 }
1017 topology_expect_change();
1018 break;
1019 default:
1020 break;
1021 }
1022 out:
1023 mutex_unlock(&smp_cpu_state_mutex);
1024 put_online_cpus();
1025 return rc ? rc : count;
1026 }
1027 static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
1028 #endif /* CONFIG_HOTPLUG_CPU */
1029
1030 static ssize_t show_cpu_address(struct device *dev,
1031 struct device_attribute *attr, char *buf)
1032 {
1033 return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
1034 }
1035 static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
1036
1037 static struct attribute *cpu_common_attrs[] = {
1038 #ifdef CONFIG_HOTPLUG_CPU
1039 &dev_attr_configure.attr,
1040 #endif
1041 &dev_attr_address.attr,
1042 NULL,
1043 };
1044
1045 static struct attribute_group cpu_common_attr_group = {
1046 .attrs = cpu_common_attrs,
1047 };
1048
1049 static struct attribute *cpu_online_attrs[] = {
1050 &dev_attr_idle_count.attr,
1051 &dev_attr_idle_time_us.attr,
1052 NULL,
1053 };
1054
1055 static struct attribute_group cpu_online_attr_group = {
1056 .attrs = cpu_online_attrs,
1057 };
1058
1059 static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
1060 void *hcpu)
1061 {
1062 unsigned int cpu = (unsigned int)(long)hcpu;
1063 struct device *s = &per_cpu(cpu_device, cpu)->dev;
1064 int err = 0;
1065
1066 switch (action & ~CPU_TASKS_FROZEN) {
1067 case CPU_ONLINE:
1068 err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1069 break;
1070 case CPU_DEAD:
1071 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1072 break;
1073 }
1074 return notifier_from_errno(err);
1075 }
1076
1077 static int smp_add_present_cpu(int cpu)
1078 {
1079 struct device *s;
1080 struct cpu *c;
1081 int rc;
1082
1083 c = kzalloc(sizeof(*c), GFP_KERNEL);
1084 if (!c)
1085 return -ENOMEM;
1086 per_cpu(cpu_device, cpu) = c;
1087 s = &c->dev;
1088 c->hotpluggable = 1;
1089 rc = register_cpu(c, cpu);
1090 if (rc)
1091 goto out;
1092 rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
1093 if (rc)
1094 goto out_cpu;
1095 if (cpu_online(cpu)) {
1096 rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
1097 if (rc)
1098 goto out_online;
1099 }
1100 rc = topology_cpu_init(c);
1101 if (rc)
1102 goto out_topology;
1103 return 0;
1104
1105 out_topology:
1106 if (cpu_online(cpu))
1107 sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
1108 out_online:
1109 sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
1110 out_cpu:
1111 #ifdef CONFIG_HOTPLUG_CPU
1112 unregister_cpu(c);
1113 #endif
1114 out:
1115 return rc;
1116 }
1117
1118 #ifdef CONFIG_HOTPLUG_CPU
1119
1120 int __ref smp_rescan_cpus(void)
1121 {
1122 struct sclp_core_info *info;
1123 int nr;
1124
1125 info = smp_get_core_info();
1126 if (!info)
1127 return -ENOMEM;
1128 get_online_cpus();
1129 mutex_lock(&smp_cpu_state_mutex);
1130 nr = __smp_rescan_cpus(info, 1);
1131 mutex_unlock(&smp_cpu_state_mutex);
1132 put_online_cpus();
1133 kfree(info);
1134 if (nr)
1135 topology_schedule_update();
1136 return 0;
1137 }
1138
1139 static ssize_t __ref rescan_store(struct device *dev,
1140 struct device_attribute *attr,
1141 const char *buf,
1142 size_t count)
1143 {
1144 int rc;
1145
1146 rc = smp_rescan_cpus();
1147 return rc ? rc : count;
1148 }
1149 static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
1150 #endif /* CONFIG_HOTPLUG_CPU */
1151
1152 static int __init s390_smp_init(void)
1153 {
1154 int cpu, rc = 0;
1155
1156 #ifdef CONFIG_HOTPLUG_CPU
1157 rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
1158 if (rc)
1159 return rc;
1160 #endif
1161 cpu_notifier_register_begin();
1162 for_each_present_cpu(cpu) {
1163 rc = smp_add_present_cpu(cpu);
1164 if (rc)
1165 goto out;
1166 }
1167
1168 __hotcpu_notifier(smp_cpu_notify, 0);
1169
1170 out:
1171 cpu_notifier_register_done();
1172 return rc;
1173 }
1174 subsys_initcall(s390_smp_init);
This page took 0.056595 seconds and 5 git commands to generate.