smp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. #define KMSG_COMPONENT "cpu"
  18. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  19. #include <linux/workqueue.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/mm.h>
  23. #include <linux/err.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/delay.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/irqflags.h>
  29. #include <linux/cpu.h>
  30. #include <linux/slab.h>
  31. #include <linux/crash_dump.h>
  32. #include <asm/asm-offsets.h>
  33. #include <asm/switch_to.h>
  34. #include <asm/facility.h>
  35. #include <asm/ipl.h>
  36. #include <asm/setup.h>
  37. #include <asm/irq.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/vtimer.h>
  40. #include <asm/lowcore.h>
  41. #include <asm/sclp.h>
  42. #include <asm/vdso.h>
  43. #include <asm/debug.h>
  44. #include <asm/os_info.h>
  45. #include <asm/sigp.h>
  46. #include <asm/idle.h>
  47. #include "entry.h"
  48. enum {
  49. ec_schedule = 0,
  50. ec_call_function_single,
  51. ec_stop_cpu,
  52. };
  53. enum {
  54. CPU_STATE_STANDBY,
  55. CPU_STATE_CONFIGURED,
  56. };
  57. struct pcpu {
  58. struct cpu *cpu;
  59. struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
  60. unsigned long async_stack; /* async stack for the cpu */
  61. unsigned long panic_stack; /* panic stack for the cpu */
  62. unsigned long ec_mask; /* bit mask for ec_xxx functions */
  63. int state; /* physical cpu state */
  64. int polarization; /* physical polarization */
  65. u16 address; /* physical cpu address */
  66. };
  67. static u8 boot_cpu_type;
  68. static u16 boot_cpu_address;
  69. static struct pcpu pcpu_devices[NR_CPUS];
  70. /*
  71. * The smp_cpu_state_mutex must be held when changing the state or polarization
  72. * member of a pcpu data structure within the pcpu_devices arreay.
  73. */
  74. DEFINE_MUTEX(smp_cpu_state_mutex);
  75. /*
  76. * Signal processor helper functions.
  77. */
  78. static inline int __pcpu_sigp_relax(u16 addr, u8 order, unsigned long parm,
  79. u32 *status)
  80. {
  81. int cc;
  82. while (1) {
  83. cc = __pcpu_sigp(addr, order, parm, NULL);
  84. if (cc != SIGP_CC_BUSY)
  85. return cc;
  86. cpu_relax();
  87. }
  88. }
  89. static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
  90. {
  91. int cc, retry;
  92. for (retry = 0; ; retry++) {
  93. cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
  94. if (cc != SIGP_CC_BUSY)
  95. break;
  96. if (retry >= 3)
  97. udelay(10);
  98. }
  99. return cc;
  100. }
  101. static inline int pcpu_stopped(struct pcpu *pcpu)
  102. {
  103. u32 uninitialized_var(status);
  104. if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
  105. 0, &status) != SIGP_CC_STATUS_STORED)
  106. return 0;
  107. return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
  108. }
  109. static inline int pcpu_running(struct pcpu *pcpu)
  110. {
  111. if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
  112. 0, NULL) != SIGP_CC_STATUS_STORED)
  113. return 1;
  114. /* Status stored condition code is equivalent to cpu not running. */
  115. return 0;
  116. }
  117. /*
  118. * Find struct pcpu by cpu address.
  119. */
  120. static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
  121. {
  122. int cpu;
  123. for_each_cpu(cpu, mask)
  124. if (pcpu_devices[cpu].address == address)
  125. return pcpu_devices + cpu;
  126. return NULL;
  127. }
  128. static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
  129. {
  130. int order;
  131. if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
  132. return;
  133. order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
  134. pcpu_sigp_retry(pcpu, order, 0);
  135. }
  136. static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
  137. {
  138. struct _lowcore *lc;
  139. if (pcpu != &pcpu_devices[0]) {
  140. pcpu->lowcore = (struct _lowcore *)
  141. __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  142. pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  143. pcpu->panic_stack = __get_free_page(GFP_KERNEL);
  144. if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
  145. goto out;
  146. }
  147. lc = pcpu->lowcore;
  148. memcpy(lc, &S390_lowcore, 512);
  149. memset((char *) lc + 512, 0, sizeof(*lc) - 512);
  150. lc->async_stack = pcpu->async_stack + ASYNC_SIZE
  151. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  152. lc->panic_stack = pcpu->panic_stack + PAGE_SIZE
  153. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  154. lc->cpu_nr = cpu;
  155. lc->spinlock_lockval = arch_spin_lockval(cpu);
  156. #ifndef CONFIG_64BIT
  157. if (MACHINE_HAS_IEEE) {
  158. lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
  159. if (!lc->extended_save_area_addr)
  160. goto out;
  161. }
  162. #else
  163. if (MACHINE_HAS_VX)
  164. lc->vector_save_area_addr =
  165. (unsigned long) &lc->vector_save_area;
  166. if (vdso_alloc_per_cpu(lc))
  167. goto out;
  168. #endif
  169. lowcore_ptr[cpu] = lc;
  170. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
  171. return 0;
  172. out:
  173. if (pcpu != &pcpu_devices[0]) {
  174. free_page(pcpu->panic_stack);
  175. free_pages(pcpu->async_stack, ASYNC_ORDER);
  176. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  177. }
  178. return -ENOMEM;
  179. }
  180. #ifdef CONFIG_HOTPLUG_CPU
  181. static void pcpu_free_lowcore(struct pcpu *pcpu)
  182. {
  183. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
  184. lowcore_ptr[pcpu - pcpu_devices] = NULL;
  185. #ifndef CONFIG_64BIT
  186. if (MACHINE_HAS_IEEE) {
  187. struct _lowcore *lc = pcpu->lowcore;
  188. free_page((unsigned long) lc->extended_save_area_addr);
  189. lc->extended_save_area_addr = 0;
  190. }
  191. #else
  192. vdso_free_per_cpu(pcpu->lowcore);
  193. #endif
  194. if (pcpu != &pcpu_devices[0]) {
  195. free_page(pcpu->panic_stack);
  196. free_pages(pcpu->async_stack, ASYNC_ORDER);
  197. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  198. }
  199. }
  200. #endif /* CONFIG_HOTPLUG_CPU */
  201. static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
  202. {
  203. struct _lowcore *lc = pcpu->lowcore;
  204. if (MACHINE_HAS_TLB_LC)
  205. cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
  206. cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
  207. atomic_inc(&init_mm.context.attach_count);
  208. lc->cpu_nr = cpu;
  209. lc->spinlock_lockval = arch_spin_lockval(cpu);
  210. lc->percpu_offset = __per_cpu_offset[cpu];
  211. lc->kernel_asce = S390_lowcore.kernel_asce;
  212. lc->machine_flags = S390_lowcore.machine_flags;
  213. lc->ftrace_func = S390_lowcore.ftrace_func;
  214. lc->user_timer = lc->system_timer = lc->steal_timer = 0;
  215. __ctl_store(lc->cregs_save_area, 0, 15);
  216. save_access_regs((unsigned int *) lc->access_regs_save_area);
  217. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  218. MAX_FACILITY_BIT/8);
  219. }
  220. static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
  221. {
  222. struct _lowcore *lc = pcpu->lowcore;
  223. struct thread_info *ti = task_thread_info(tsk);
  224. lc->kernel_stack = (unsigned long) task_stack_page(tsk)
  225. + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  226. lc->thread_info = (unsigned long) task_thread_info(tsk);
  227. lc->current_task = (unsigned long) tsk;
  228. lc->user_timer = ti->user_timer;
  229. lc->system_timer = ti->system_timer;
  230. lc->steal_timer = 0;
  231. }
  232. static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
  233. {
  234. struct _lowcore *lc = pcpu->lowcore;
  235. lc->restart_stack = lc->kernel_stack;
  236. lc->restart_fn = (unsigned long) func;
  237. lc->restart_data = (unsigned long) data;
  238. lc->restart_source = -1UL;
  239. pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
  240. }
  241. /*
  242. * Call function via PSW restart on pcpu and stop the current cpu.
  243. */
  244. static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
  245. void *data, unsigned long stack)
  246. {
  247. struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
  248. unsigned long source_cpu = stap();
  249. __load_psw_mask(PSW_KERNEL_BITS);
  250. if (pcpu->address == source_cpu)
  251. func(data); /* should not return */
  252. /* Stop target cpu (if func returns this stops the current cpu). */
  253. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  254. /* Restart func on the target cpu and stop the current cpu. */
  255. mem_assign_absolute(lc->restart_stack, stack);
  256. mem_assign_absolute(lc->restart_fn, (unsigned long) func);
  257. mem_assign_absolute(lc->restart_data, (unsigned long) data);
  258. mem_assign_absolute(lc->restart_source, source_cpu);
  259. asm volatile(
  260. "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
  261. " brc 2,0b # busy, try again\n"
  262. "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
  263. " brc 2,1b # busy, try again\n"
  264. : : "d" (pcpu->address), "d" (source_cpu),
  265. "K" (SIGP_RESTART), "K" (SIGP_STOP)
  266. : "0", "1", "cc");
  267. for (;;) ;
  268. }
  269. /*
  270. * Call function on an online CPU.
  271. */
  272. void smp_call_online_cpu(void (*func)(void *), void *data)
  273. {
  274. struct pcpu *pcpu;
  275. /* Use the current cpu if it is online. */
  276. pcpu = pcpu_find_address(cpu_online_mask, stap());
  277. if (!pcpu)
  278. /* Use the first online cpu. */
  279. pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
  280. pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
  281. }
  282. /*
  283. * Call function on the ipl CPU.
  284. */
  285. void smp_call_ipl_cpu(void (*func)(void *), void *data)
  286. {
  287. pcpu_delegate(&pcpu_devices[0], func, data,
  288. pcpu_devices->panic_stack + PAGE_SIZE);
  289. }
  290. int smp_find_processor_id(u16 address)
  291. {
  292. int cpu;
  293. for_each_present_cpu(cpu)
  294. if (pcpu_devices[cpu].address == address)
  295. return cpu;
  296. return -1;
  297. }
  298. int smp_vcpu_scheduled(int cpu)
  299. {
  300. return pcpu_running(pcpu_devices + cpu);
  301. }
  302. void smp_yield_cpu(int cpu)
  303. {
  304. if (MACHINE_HAS_DIAG9C)
  305. asm volatile("diag %0,0,0x9c"
  306. : : "d" (pcpu_devices[cpu].address));
  307. else if (MACHINE_HAS_DIAG44)
  308. asm volatile("diag 0,0,0x44");
  309. }
  310. /*
  311. * Send cpus emergency shutdown signal. This gives the cpus the
  312. * opportunity to complete outstanding interrupts.
  313. */
  314. static void smp_emergency_stop(cpumask_t *cpumask)
  315. {
  316. u64 end;
  317. int cpu;
  318. end = get_tod_clock() + (1000000UL << 12);
  319. for_each_cpu(cpu, cpumask) {
  320. struct pcpu *pcpu = pcpu_devices + cpu;
  321. set_bit(ec_stop_cpu, &pcpu->ec_mask);
  322. while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
  323. 0, NULL) == SIGP_CC_BUSY &&
  324. get_tod_clock() < end)
  325. cpu_relax();
  326. }
  327. while (get_tod_clock() < end) {
  328. for_each_cpu(cpu, cpumask)
  329. if (pcpu_stopped(pcpu_devices + cpu))
  330. cpumask_clear_cpu(cpu, cpumask);
  331. if (cpumask_empty(cpumask))
  332. break;
  333. cpu_relax();
  334. }
  335. }
  336. /*
  337. * Stop all cpus but the current one.
  338. */
  339. void smp_send_stop(void)
  340. {
  341. cpumask_t cpumask;
  342. int cpu;
  343. /* Disable all interrupts/machine checks */
  344. __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
  345. trace_hardirqs_off();
  346. debug_set_critical();
  347. cpumask_copy(&cpumask, cpu_online_mask);
  348. cpumask_clear_cpu(smp_processor_id(), &cpumask);
  349. if (oops_in_progress)
  350. smp_emergency_stop(&cpumask);
  351. /* stop all processors */
  352. for_each_cpu(cpu, &cpumask) {
  353. struct pcpu *pcpu = pcpu_devices + cpu;
  354. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  355. while (!pcpu_stopped(pcpu))
  356. cpu_relax();
  357. }
  358. }
  359. /*
  360. * This is the main routine where commands issued by other
  361. * cpus are handled.
  362. */
  363. static void smp_handle_ext_call(void)
  364. {
  365. unsigned long bits;
  366. /* handle bit signal external calls */
  367. bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
  368. if (test_bit(ec_stop_cpu, &bits))
  369. smp_stop_cpu();
  370. if (test_bit(ec_schedule, &bits))
  371. scheduler_ipi();
  372. if (test_bit(ec_call_function_single, &bits))
  373. generic_smp_call_function_single_interrupt();
  374. }
  375. static void do_ext_call_interrupt(struct ext_code ext_code,
  376. unsigned int param32, unsigned long param64)
  377. {
  378. inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
  379. smp_handle_ext_call();
  380. }
  381. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  382. {
  383. int cpu;
  384. for_each_cpu(cpu, mask)
  385. pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
  386. }
  387. void arch_send_call_function_single_ipi(int cpu)
  388. {
  389. pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
  390. }
  391. #ifndef CONFIG_64BIT
  392. /*
  393. * this function sends a 'purge tlb' signal to another CPU.
  394. */
  395. static void smp_ptlb_callback(void *info)
  396. {
  397. __tlb_flush_local();
  398. }
  399. void smp_ptlb_all(void)
  400. {
  401. on_each_cpu(smp_ptlb_callback, NULL, 1);
  402. }
  403. EXPORT_SYMBOL(smp_ptlb_all);
  404. #endif /* ! CONFIG_64BIT */
  405. /*
  406. * this function sends a 'reschedule' IPI to another CPU.
  407. * it goes straight through and wastes no time serializing
  408. * anything. Worst case is that we lose a reschedule ...
  409. */
  410. void smp_send_reschedule(int cpu)
  411. {
  412. pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
  413. }
  414. /*
  415. * parameter area for the set/clear control bit callbacks
  416. */
  417. struct ec_creg_mask_parms {
  418. unsigned long orval;
  419. unsigned long andval;
  420. int cr;
  421. };
  422. /*
  423. * callback for setting/clearing control bits
  424. */
  425. static void smp_ctl_bit_callback(void *info)
  426. {
  427. struct ec_creg_mask_parms *pp = info;
  428. unsigned long cregs[16];
  429. __ctl_store(cregs, 0, 15);
  430. cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
  431. __ctl_load(cregs, 0, 15);
  432. }
  433. /*
  434. * Set a bit in a control register of all cpus
  435. */
  436. void smp_ctl_set_bit(int cr, int bit)
  437. {
  438. struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
  439. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  440. }
  441. EXPORT_SYMBOL(smp_ctl_set_bit);
  442. /*
  443. * Clear a bit in a control register of all cpus
  444. */
  445. void smp_ctl_clear_bit(int cr, int bit)
  446. {
  447. struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
  448. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  449. }
  450. EXPORT_SYMBOL(smp_ctl_clear_bit);
  451. #ifdef CONFIG_CRASH_DUMP
  452. static void __init smp_get_save_area(int cpu, u16 address)
  453. {
  454. void *lc = pcpu_devices[0].lowcore;
  455. struct save_area_ext *sa_ext;
  456. unsigned long vx_sa;
  457. if (is_kdump_kernel())
  458. return;
  459. if (!OLDMEM_BASE && (address == boot_cpu_address ||
  460. ipl_info.type != IPL_TYPE_FCP_DUMP))
  461. return;
  462. sa_ext = dump_save_area_create(cpu);
  463. if (!sa_ext)
  464. panic("could not allocate memory for save area\n");
  465. if (address == boot_cpu_address) {
  466. /* Copy the registers of the boot cpu. */
  467. copy_oldmem_page(1, (void *) &sa_ext->sa, sizeof(sa_ext->sa),
  468. SAVE_AREA_BASE - PAGE_SIZE, 0);
  469. if (MACHINE_HAS_VX)
  470. save_vx_regs_safe(sa_ext->vx_regs);
  471. return;
  472. }
  473. /* Get the registers of a non-boot cpu. */
  474. __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
  475. memcpy_real(&sa_ext->sa, lc + SAVE_AREA_BASE, sizeof(sa_ext->sa));
  476. if (!MACHINE_HAS_VX)
  477. return;
  478. /* Get the VX registers */
  479. vx_sa = __get_free_page(GFP_KERNEL);
  480. if (!vx_sa)
  481. panic("could not allocate memory for VX save area\n");
  482. __pcpu_sigp_relax(address, SIGP_STORE_ADDITIONAL_STATUS, vx_sa, NULL);
  483. memcpy(sa_ext->vx_regs, (void *) vx_sa, sizeof(sa_ext->vx_regs));
  484. free_page(vx_sa);
  485. }
  486. int smp_store_status(int cpu)
  487. {
  488. unsigned long vx_sa;
  489. struct pcpu *pcpu;
  490. pcpu = pcpu_devices + cpu;
  491. if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
  492. 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
  493. return -EIO;
  494. if (!MACHINE_HAS_VX)
  495. return 0;
  496. vx_sa = __pa(pcpu->lowcore->vector_save_area_addr);
  497. __pcpu_sigp_relax(pcpu->address, SIGP_STORE_ADDITIONAL_STATUS,
  498. vx_sa, NULL);
  499. return 0;
  500. }
  501. #else /* CONFIG_CRASH_DUMP */
  502. static inline void smp_get_save_area(int cpu, u16 address) { }
  503. #endif /* CONFIG_CRASH_DUMP */
  504. void smp_cpu_set_polarization(int cpu, int val)
  505. {
  506. pcpu_devices[cpu].polarization = val;
  507. }
  508. int smp_cpu_get_polarization(int cpu)
  509. {
  510. return pcpu_devices[cpu].polarization;
  511. }
  512. static struct sclp_cpu_info *smp_get_cpu_info(void)
  513. {
  514. static int use_sigp_detection;
  515. struct sclp_cpu_info *info;
  516. int address;
  517. info = kzalloc(sizeof(*info), GFP_KERNEL);
  518. if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
  519. use_sigp_detection = 1;
  520. for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
  521. if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
  522. SIGP_CC_NOT_OPERATIONAL)
  523. continue;
  524. info->cpu[info->configured].address = address;
  525. info->configured++;
  526. }
  527. info->combined = info->configured;
  528. }
  529. return info;
  530. }
  531. static int smp_add_present_cpu(int cpu);
  532. static int __smp_rescan_cpus(struct sclp_cpu_info *info, int sysfs_add)
  533. {
  534. struct pcpu *pcpu;
  535. cpumask_t avail;
  536. int cpu, nr, i;
  537. nr = 0;
  538. cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
  539. cpu = cpumask_first(&avail);
  540. for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
  541. if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
  542. continue;
  543. if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
  544. continue;
  545. pcpu = pcpu_devices + cpu;
  546. pcpu->address = info->cpu[i].address;
  547. pcpu->state = (i >= info->configured) ?
  548. CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
  549. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  550. set_cpu_present(cpu, true);
  551. if (sysfs_add && smp_add_present_cpu(cpu) != 0)
  552. set_cpu_present(cpu, false);
  553. else
  554. nr++;
  555. cpu = cpumask_next(cpu, &avail);
  556. }
  557. return nr;
  558. }
  559. static void __init smp_detect_cpus(void)
  560. {
  561. unsigned int cpu, c_cpus, s_cpus;
  562. struct sclp_cpu_info *info;
  563. info = smp_get_cpu_info();
  564. if (!info)
  565. panic("smp_detect_cpus failed to allocate memory\n");
  566. if (info->has_cpu_type) {
  567. for (cpu = 0; cpu < info->combined; cpu++) {
  568. if (info->cpu[cpu].address != boot_cpu_address)
  569. continue;
  570. /* The boot cpu dictates the cpu type. */
  571. boot_cpu_type = info->cpu[cpu].type;
  572. break;
  573. }
  574. }
  575. c_cpus = s_cpus = 0;
  576. for (cpu = 0; cpu < info->combined; cpu++) {
  577. if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
  578. continue;
  579. if (cpu < info->configured) {
  580. smp_get_save_area(c_cpus, info->cpu[cpu].address);
  581. c_cpus++;
  582. } else
  583. s_cpus++;
  584. }
  585. pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
  586. get_online_cpus();
  587. __smp_rescan_cpus(info, 0);
  588. put_online_cpus();
  589. kfree(info);
  590. }
  591. /*
  592. * Activate a secondary processor.
  593. */
  594. static void smp_start_secondary(void *cpuvoid)
  595. {
  596. S390_lowcore.last_update_clock = get_tod_clock();
  597. S390_lowcore.restart_stack = (unsigned long) restart_stack;
  598. S390_lowcore.restart_fn = (unsigned long) do_restart;
  599. S390_lowcore.restart_data = 0;
  600. S390_lowcore.restart_source = -1UL;
  601. restore_access_regs(S390_lowcore.access_regs_save_area);
  602. __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
  603. __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
  604. cpu_init();
  605. preempt_disable();
  606. init_cpu_timer();
  607. vtime_init();
  608. pfault_init();
  609. notify_cpu_starting(smp_processor_id());
  610. set_cpu_online(smp_processor_id(), true);
  611. inc_irq_stat(CPU_RST);
  612. local_irq_enable();
  613. cpu_startup_entry(CPUHP_ONLINE);
  614. }
  615. /* Upping and downing of CPUs */
  616. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  617. {
  618. struct pcpu *pcpu;
  619. int rc;
  620. pcpu = pcpu_devices + cpu;
  621. if (pcpu->state != CPU_STATE_CONFIGURED)
  622. return -EIO;
  623. if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
  624. SIGP_CC_ORDER_CODE_ACCEPTED)
  625. return -EIO;
  626. rc = pcpu_alloc_lowcore(pcpu, cpu);
  627. if (rc)
  628. return rc;
  629. pcpu_prepare_secondary(pcpu, cpu);
  630. pcpu_attach_task(pcpu, tidle);
  631. pcpu_start_fn(pcpu, smp_start_secondary, NULL);
  632. while (!cpu_online(cpu))
  633. cpu_relax();
  634. return 0;
  635. }
  636. static unsigned int setup_possible_cpus __initdata;
  637. static int __init _setup_possible_cpus(char *s)
  638. {
  639. get_option(&s, &setup_possible_cpus);
  640. return 0;
  641. }
  642. early_param("possible_cpus", _setup_possible_cpus);
  643. #ifdef CONFIG_HOTPLUG_CPU
  644. int __cpu_disable(void)
  645. {
  646. unsigned long cregs[16];
  647. /* Handle possible pending IPIs */
  648. smp_handle_ext_call();
  649. set_cpu_online(smp_processor_id(), false);
  650. /* Disable pseudo page faults on this cpu. */
  651. pfault_fini();
  652. /* Disable interrupt sources via control register. */
  653. __ctl_store(cregs, 0, 15);
  654. cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
  655. cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
  656. cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
  657. __ctl_load(cregs, 0, 15);
  658. clear_cpu_flag(CIF_NOHZ_DELAY);
  659. return 0;
  660. }
  661. void __cpu_die(unsigned int cpu)
  662. {
  663. struct pcpu *pcpu;
  664. /* Wait until target cpu is down */
  665. pcpu = pcpu_devices + cpu;
  666. while (!pcpu_stopped(pcpu))
  667. cpu_relax();
  668. pcpu_free_lowcore(pcpu);
  669. atomic_dec(&init_mm.context.attach_count);
  670. cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
  671. if (MACHINE_HAS_TLB_LC)
  672. cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
  673. }
  674. void __noreturn cpu_die(void)
  675. {
  676. idle_task_exit();
  677. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  678. for (;;) ;
  679. }
  680. #endif /* CONFIG_HOTPLUG_CPU */
  681. void __init smp_fill_possible_mask(void)
  682. {
  683. unsigned int possible, sclp, cpu;
  684. sclp = sclp_get_max_cpu() ?: nr_cpu_ids;
  685. possible = setup_possible_cpus ?: nr_cpu_ids;
  686. possible = min(possible, sclp);
  687. for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
  688. set_cpu_possible(cpu, true);
  689. }
  690. void __init smp_prepare_cpus(unsigned int max_cpus)
  691. {
  692. /* request the 0x1201 emergency signal external interrupt */
  693. if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
  694. panic("Couldn't request external interrupt 0x1201");
  695. /* request the 0x1202 external call external interrupt */
  696. if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
  697. panic("Couldn't request external interrupt 0x1202");
  698. smp_detect_cpus();
  699. }
  700. void __init smp_prepare_boot_cpu(void)
  701. {
  702. struct pcpu *pcpu = pcpu_devices;
  703. boot_cpu_address = stap();
  704. pcpu->state = CPU_STATE_CONFIGURED;
  705. pcpu->address = boot_cpu_address;
  706. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  707. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE
  708. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  709. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE
  710. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  711. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  712. smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  713. set_cpu_present(0, true);
  714. set_cpu_online(0, true);
  715. }
  716. void __init smp_cpus_done(unsigned int max_cpus)
  717. {
  718. }
  719. void __init smp_setup_processor_id(void)
  720. {
  721. S390_lowcore.cpu_nr = 0;
  722. S390_lowcore.spinlock_lockval = arch_spin_lockval(0);
  723. }
  724. /*
  725. * the frequency of the profiling timer can be changed
  726. * by writing a multiplier value into /proc/profile.
  727. *
  728. * usually you want to run this on all CPUs ;)
  729. */
  730. int setup_profiling_timer(unsigned int multiplier)
  731. {
  732. return 0;
  733. }
  734. #ifdef CONFIG_HOTPLUG_CPU
  735. static ssize_t cpu_configure_show(struct device *dev,
  736. struct device_attribute *attr, char *buf)
  737. {
  738. ssize_t count;
  739. mutex_lock(&smp_cpu_state_mutex);
  740. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  741. mutex_unlock(&smp_cpu_state_mutex);
  742. return count;
  743. }
  744. static ssize_t cpu_configure_store(struct device *dev,
  745. struct device_attribute *attr,
  746. const char *buf, size_t count)
  747. {
  748. struct pcpu *pcpu;
  749. int cpu, val, rc;
  750. char delim;
  751. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  752. return -EINVAL;
  753. if (val != 0 && val != 1)
  754. return -EINVAL;
  755. get_online_cpus();
  756. mutex_lock(&smp_cpu_state_mutex);
  757. rc = -EBUSY;
  758. /* disallow configuration changes of online cpus and cpu 0 */
  759. cpu = dev->id;
  760. if (cpu_online(cpu) || cpu == 0)
  761. goto out;
  762. pcpu = pcpu_devices + cpu;
  763. rc = 0;
  764. switch (val) {
  765. case 0:
  766. if (pcpu->state != CPU_STATE_CONFIGURED)
  767. break;
  768. rc = sclp_cpu_deconfigure(pcpu->address);
  769. if (rc)
  770. break;
  771. pcpu->state = CPU_STATE_STANDBY;
  772. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  773. topology_expect_change();
  774. break;
  775. case 1:
  776. if (pcpu->state != CPU_STATE_STANDBY)
  777. break;
  778. rc = sclp_cpu_configure(pcpu->address);
  779. if (rc)
  780. break;
  781. pcpu->state = CPU_STATE_CONFIGURED;
  782. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  783. topology_expect_change();
  784. break;
  785. default:
  786. break;
  787. }
  788. out:
  789. mutex_unlock(&smp_cpu_state_mutex);
  790. put_online_cpus();
  791. return rc ? rc : count;
  792. }
  793. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  794. #endif /* CONFIG_HOTPLUG_CPU */
  795. static ssize_t show_cpu_address(struct device *dev,
  796. struct device_attribute *attr, char *buf)
  797. {
  798. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  799. }
  800. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  801. static struct attribute *cpu_common_attrs[] = {
  802. #ifdef CONFIG_HOTPLUG_CPU
  803. &dev_attr_configure.attr,
  804. #endif
  805. &dev_attr_address.attr,
  806. NULL,
  807. };
  808. static struct attribute_group cpu_common_attr_group = {
  809. .attrs = cpu_common_attrs,
  810. };
  811. static struct attribute *cpu_online_attrs[] = {
  812. &dev_attr_idle_count.attr,
  813. &dev_attr_idle_time_us.attr,
  814. NULL,
  815. };
  816. static struct attribute_group cpu_online_attr_group = {
  817. .attrs = cpu_online_attrs,
  818. };
  819. static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
  820. void *hcpu)
  821. {
  822. unsigned int cpu = (unsigned int)(long)hcpu;
  823. struct cpu *c = pcpu_devices[cpu].cpu;
  824. struct device *s = &c->dev;
  825. int err = 0;
  826. switch (action & ~CPU_TASKS_FROZEN) {
  827. case CPU_ONLINE:
  828. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  829. break;
  830. case CPU_DEAD:
  831. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  832. break;
  833. }
  834. return notifier_from_errno(err);
  835. }
  836. static int smp_add_present_cpu(int cpu)
  837. {
  838. struct device *s;
  839. struct cpu *c;
  840. int rc;
  841. c = kzalloc(sizeof(*c), GFP_KERNEL);
  842. if (!c)
  843. return -ENOMEM;
  844. pcpu_devices[cpu].cpu = c;
  845. s = &c->dev;
  846. c->hotpluggable = 1;
  847. rc = register_cpu(c, cpu);
  848. if (rc)
  849. goto out;
  850. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  851. if (rc)
  852. goto out_cpu;
  853. if (cpu_online(cpu)) {
  854. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  855. if (rc)
  856. goto out_online;
  857. }
  858. rc = topology_cpu_init(c);
  859. if (rc)
  860. goto out_topology;
  861. return 0;
  862. out_topology:
  863. if (cpu_online(cpu))
  864. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  865. out_online:
  866. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  867. out_cpu:
  868. #ifdef CONFIG_HOTPLUG_CPU
  869. unregister_cpu(c);
  870. #endif
  871. out:
  872. return rc;
  873. }
  874. #ifdef CONFIG_HOTPLUG_CPU
  875. int __ref smp_rescan_cpus(void)
  876. {
  877. struct sclp_cpu_info *info;
  878. int nr;
  879. info = smp_get_cpu_info();
  880. if (!info)
  881. return -ENOMEM;
  882. get_online_cpus();
  883. mutex_lock(&smp_cpu_state_mutex);
  884. nr = __smp_rescan_cpus(info, 1);
  885. mutex_unlock(&smp_cpu_state_mutex);
  886. put_online_cpus();
  887. kfree(info);
  888. if (nr)
  889. topology_schedule_update();
  890. return 0;
  891. }
  892. static ssize_t __ref rescan_store(struct device *dev,
  893. struct device_attribute *attr,
  894. const char *buf,
  895. size_t count)
  896. {
  897. int rc;
  898. rc = smp_rescan_cpus();
  899. return rc ? rc : count;
  900. }
  901. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  902. #endif /* CONFIG_HOTPLUG_CPU */
  903. static int __init s390_smp_init(void)
  904. {
  905. int cpu, rc = 0;
  906. #ifdef CONFIG_HOTPLUG_CPU
  907. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  908. if (rc)
  909. return rc;
  910. #endif
  911. cpu_notifier_register_begin();
  912. for_each_present_cpu(cpu) {
  913. rc = smp_add_present_cpu(cpu);
  914. if (rc)
  915. goto out;
  916. }
  917. __hotcpu_notifier(smp_cpu_notify, 0);
  918. out:
  919. cpu_notifier_register_done();
  920. return rc;
  921. }
  922. subsys_initcall(s390_smp_init);