smp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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 "entry.h"
  47. enum {
  48. ec_schedule = 0,
  49. ec_call_function_single,
  50. ec_stop_cpu,
  51. };
  52. enum {
  53. CPU_STATE_STANDBY,
  54. CPU_STATE_CONFIGURED,
  55. };
  56. struct pcpu {
  57. struct cpu *cpu;
  58. struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
  59. unsigned long async_stack; /* async stack for the cpu */
  60. unsigned long panic_stack; /* panic stack for the cpu */
  61. unsigned long ec_mask; /* bit mask for ec_xxx functions */
  62. int state; /* physical cpu state */
  63. int polarization; /* physical polarization */
  64. u16 address; /* physical cpu address */
  65. };
  66. static u8 boot_cpu_type;
  67. static u16 boot_cpu_address;
  68. static struct pcpu pcpu_devices[NR_CPUS];
  69. /*
  70. * The smp_cpu_state_mutex must be held when changing the state or polarization
  71. * member of a pcpu data structure within the pcpu_devices arreay.
  72. */
  73. DEFINE_MUTEX(smp_cpu_state_mutex);
  74. /*
  75. * Signal processor helper functions.
  76. */
  77. static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
  78. {
  79. register unsigned int reg1 asm ("1") = parm;
  80. int cc;
  81. asm volatile(
  82. " sigp %1,%2,0(%3)\n"
  83. " ipm %0\n"
  84. " srl %0,28\n"
  85. : "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
  86. if (status && cc == 1)
  87. *status = reg1;
  88. return cc;
  89. }
  90. static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
  91. {
  92. int cc;
  93. while (1) {
  94. cc = __pcpu_sigp(addr, order, parm, NULL);
  95. if (cc != SIGP_CC_BUSY)
  96. return cc;
  97. cpu_relax();
  98. }
  99. }
  100. static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
  101. {
  102. int cc, retry;
  103. for (retry = 0; ; retry++) {
  104. cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
  105. if (cc != SIGP_CC_BUSY)
  106. break;
  107. if (retry >= 3)
  108. udelay(10);
  109. }
  110. return cc;
  111. }
  112. static inline int pcpu_stopped(struct pcpu *pcpu)
  113. {
  114. u32 uninitialized_var(status);
  115. if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
  116. 0, &status) != SIGP_CC_STATUS_STORED)
  117. return 0;
  118. return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
  119. }
  120. static inline int pcpu_running(struct pcpu *pcpu)
  121. {
  122. if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
  123. 0, NULL) != SIGP_CC_STATUS_STORED)
  124. return 1;
  125. /* Status stored condition code is equivalent to cpu not running. */
  126. return 0;
  127. }
  128. /*
  129. * Find struct pcpu by cpu address.
  130. */
  131. static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
  132. {
  133. int cpu;
  134. for_each_cpu(cpu, mask)
  135. if (pcpu_devices[cpu].address == address)
  136. return pcpu_devices + cpu;
  137. return NULL;
  138. }
  139. static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
  140. {
  141. int order;
  142. if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
  143. return;
  144. order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
  145. pcpu_sigp_retry(pcpu, order, 0);
  146. }
  147. static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
  148. {
  149. struct _lowcore *lc;
  150. if (pcpu != &pcpu_devices[0]) {
  151. pcpu->lowcore = (struct _lowcore *)
  152. __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  153. pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  154. pcpu->panic_stack = __get_free_page(GFP_KERNEL);
  155. if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
  156. goto out;
  157. }
  158. lc = pcpu->lowcore;
  159. memcpy(lc, &S390_lowcore, 512);
  160. memset((char *) lc + 512, 0, sizeof(*lc) - 512);
  161. lc->async_stack = pcpu->async_stack + ASYNC_SIZE
  162. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  163. lc->panic_stack = pcpu->panic_stack + PAGE_SIZE
  164. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  165. lc->cpu_nr = cpu;
  166. #ifndef CONFIG_64BIT
  167. if (MACHINE_HAS_IEEE) {
  168. lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
  169. if (!lc->extended_save_area_addr)
  170. goto out;
  171. }
  172. #else
  173. if (vdso_alloc_per_cpu(lc))
  174. goto out;
  175. #endif
  176. lowcore_ptr[cpu] = lc;
  177. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
  178. return 0;
  179. out:
  180. if (pcpu != &pcpu_devices[0]) {
  181. free_page(pcpu->panic_stack);
  182. free_pages(pcpu->async_stack, ASYNC_ORDER);
  183. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  184. }
  185. return -ENOMEM;
  186. }
  187. #ifdef CONFIG_HOTPLUG_CPU
  188. static void pcpu_free_lowcore(struct pcpu *pcpu)
  189. {
  190. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
  191. lowcore_ptr[pcpu - pcpu_devices] = NULL;
  192. #ifndef CONFIG_64BIT
  193. if (MACHINE_HAS_IEEE) {
  194. struct _lowcore *lc = pcpu->lowcore;
  195. free_page((unsigned long) lc->extended_save_area_addr);
  196. lc->extended_save_area_addr = 0;
  197. }
  198. #else
  199. vdso_free_per_cpu(pcpu->lowcore);
  200. #endif
  201. if (pcpu != &pcpu_devices[0]) {
  202. free_page(pcpu->panic_stack);
  203. free_pages(pcpu->async_stack, ASYNC_ORDER);
  204. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  205. }
  206. }
  207. #endif /* CONFIG_HOTPLUG_CPU */
  208. static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
  209. {
  210. struct _lowcore *lc = pcpu->lowcore;
  211. atomic_inc(&init_mm.context.attach_count);
  212. lc->cpu_nr = cpu;
  213. lc->percpu_offset = __per_cpu_offset[cpu];
  214. lc->kernel_asce = S390_lowcore.kernel_asce;
  215. lc->machine_flags = S390_lowcore.machine_flags;
  216. lc->ftrace_func = S390_lowcore.ftrace_func;
  217. lc->user_timer = lc->system_timer = lc->steal_timer = 0;
  218. __ctl_store(lc->cregs_save_area, 0, 15);
  219. save_access_regs((unsigned int *) lc->access_regs_save_area);
  220. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  221. MAX_FACILITY_BIT/8);
  222. }
  223. static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
  224. {
  225. struct _lowcore *lc = pcpu->lowcore;
  226. struct thread_info *ti = task_thread_info(tsk);
  227. lc->kernel_stack = (unsigned long) task_stack_page(tsk)
  228. + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  229. lc->thread_info = (unsigned long) task_thread_info(tsk);
  230. lc->current_task = (unsigned long) tsk;
  231. lc->user_timer = ti->user_timer;
  232. lc->system_timer = ti->system_timer;
  233. lc->steal_timer = 0;
  234. }
  235. static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
  236. {
  237. struct _lowcore *lc = pcpu->lowcore;
  238. lc->restart_stack = lc->kernel_stack;
  239. lc->restart_fn = (unsigned long) func;
  240. lc->restart_data = (unsigned long) data;
  241. lc->restart_source = -1UL;
  242. pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
  243. }
  244. /*
  245. * Call function via PSW restart on pcpu and stop the current cpu.
  246. */
  247. static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
  248. void *data, unsigned long stack)
  249. {
  250. struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
  251. unsigned long source_cpu = stap();
  252. __load_psw_mask(PSW_KERNEL_BITS);
  253. if (pcpu->address == source_cpu)
  254. func(data); /* should not return */
  255. /* Stop target cpu (if func returns this stops the current cpu). */
  256. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  257. /* Restart func on the target cpu and stop the current cpu. */
  258. mem_assign_absolute(lc->restart_stack, stack);
  259. mem_assign_absolute(lc->restart_fn, (unsigned long) func);
  260. mem_assign_absolute(lc->restart_data, (unsigned long) data);
  261. mem_assign_absolute(lc->restart_source, source_cpu);
  262. asm volatile(
  263. "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
  264. " brc 2,0b # busy, try again\n"
  265. "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
  266. " brc 2,1b # busy, try again\n"
  267. : : "d" (pcpu->address), "d" (source_cpu),
  268. "K" (SIGP_RESTART), "K" (SIGP_STOP)
  269. : "0", "1", "cc");
  270. for (;;) ;
  271. }
  272. /*
  273. * Call function on an online CPU.
  274. */
  275. void smp_call_online_cpu(void (*func)(void *), void *data)
  276. {
  277. struct pcpu *pcpu;
  278. /* Use the current cpu if it is online. */
  279. pcpu = pcpu_find_address(cpu_online_mask, stap());
  280. if (!pcpu)
  281. /* Use the first online cpu. */
  282. pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
  283. pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
  284. }
  285. /*
  286. * Call function on the ipl CPU.
  287. */
  288. void smp_call_ipl_cpu(void (*func)(void *), void *data)
  289. {
  290. pcpu_delegate(&pcpu_devices[0], func, data,
  291. pcpu_devices->panic_stack + PAGE_SIZE);
  292. }
  293. int smp_find_processor_id(u16 address)
  294. {
  295. int cpu;
  296. for_each_present_cpu(cpu)
  297. if (pcpu_devices[cpu].address == address)
  298. return cpu;
  299. return -1;
  300. }
  301. int smp_vcpu_scheduled(int cpu)
  302. {
  303. return pcpu_running(pcpu_devices + cpu);
  304. }
  305. void smp_yield(void)
  306. {
  307. if (MACHINE_HAS_DIAG44)
  308. asm volatile("diag 0,0,0x44");
  309. }
  310. void smp_yield_cpu(int cpu)
  311. {
  312. if (MACHINE_HAS_DIAG9C)
  313. asm volatile("diag %0,0,0x9c"
  314. : : "d" (pcpu_devices[cpu].address));
  315. else if (MACHINE_HAS_DIAG44)
  316. asm volatile("diag 0,0,0x44");
  317. }
  318. /*
  319. * Send cpus emergency shutdown signal. This gives the cpus the
  320. * opportunity to complete outstanding interrupts.
  321. */
  322. static void smp_emergency_stop(cpumask_t *cpumask)
  323. {
  324. u64 end;
  325. int cpu;
  326. end = get_tod_clock() + (1000000UL << 12);
  327. for_each_cpu(cpu, cpumask) {
  328. struct pcpu *pcpu = pcpu_devices + cpu;
  329. set_bit(ec_stop_cpu, &pcpu->ec_mask);
  330. while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
  331. 0, NULL) == SIGP_CC_BUSY &&
  332. get_tod_clock() < end)
  333. cpu_relax();
  334. }
  335. while (get_tod_clock() < end) {
  336. for_each_cpu(cpu, cpumask)
  337. if (pcpu_stopped(pcpu_devices + cpu))
  338. cpumask_clear_cpu(cpu, cpumask);
  339. if (cpumask_empty(cpumask))
  340. break;
  341. cpu_relax();
  342. }
  343. }
  344. /*
  345. * Stop all cpus but the current one.
  346. */
  347. void smp_send_stop(void)
  348. {
  349. cpumask_t cpumask;
  350. int cpu;
  351. /* Disable all interrupts/machine checks */
  352. __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
  353. trace_hardirqs_off();
  354. debug_set_critical();
  355. cpumask_copy(&cpumask, cpu_online_mask);
  356. cpumask_clear_cpu(smp_processor_id(), &cpumask);
  357. if (oops_in_progress)
  358. smp_emergency_stop(&cpumask);
  359. /* stop all processors */
  360. for_each_cpu(cpu, &cpumask) {
  361. struct pcpu *pcpu = pcpu_devices + cpu;
  362. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  363. while (!pcpu_stopped(pcpu))
  364. cpu_relax();
  365. }
  366. }
  367. /*
  368. * Stop the current cpu.
  369. */
  370. void smp_stop_cpu(void)
  371. {
  372. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  373. for (;;) ;
  374. }
  375. /*
  376. * This is the main routine where commands issued by other
  377. * cpus are handled.
  378. */
  379. static void smp_handle_ext_call(void)
  380. {
  381. unsigned long bits;
  382. /* handle bit signal external calls */
  383. bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
  384. if (test_bit(ec_stop_cpu, &bits))
  385. smp_stop_cpu();
  386. if (test_bit(ec_schedule, &bits))
  387. scheduler_ipi();
  388. if (test_bit(ec_call_function_single, &bits))
  389. generic_smp_call_function_single_interrupt();
  390. }
  391. static void do_ext_call_interrupt(struct ext_code ext_code,
  392. unsigned int param32, unsigned long param64)
  393. {
  394. inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
  395. smp_handle_ext_call();
  396. }
  397. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  398. {
  399. int cpu;
  400. for_each_cpu(cpu, mask)
  401. pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
  402. }
  403. void arch_send_call_function_single_ipi(int cpu)
  404. {
  405. pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
  406. }
  407. #ifndef CONFIG_64BIT
  408. /*
  409. * this function sends a 'purge tlb' signal to another CPU.
  410. */
  411. static void smp_ptlb_callback(void *info)
  412. {
  413. __tlb_flush_local();
  414. }
  415. void smp_ptlb_all(void)
  416. {
  417. on_each_cpu(smp_ptlb_callback, NULL, 1);
  418. }
  419. EXPORT_SYMBOL(smp_ptlb_all);
  420. #endif /* ! CONFIG_64BIT */
  421. /*
  422. * this function sends a 'reschedule' IPI to another CPU.
  423. * it goes straight through and wastes no time serializing
  424. * anything. Worst case is that we lose a reschedule ...
  425. */
  426. void smp_send_reschedule(int cpu)
  427. {
  428. pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
  429. }
  430. /*
  431. * parameter area for the set/clear control bit callbacks
  432. */
  433. struct ec_creg_mask_parms {
  434. unsigned long orval;
  435. unsigned long andval;
  436. int cr;
  437. };
  438. /*
  439. * callback for setting/clearing control bits
  440. */
  441. static void smp_ctl_bit_callback(void *info)
  442. {
  443. struct ec_creg_mask_parms *pp = info;
  444. unsigned long cregs[16];
  445. __ctl_store(cregs, 0, 15);
  446. cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
  447. __ctl_load(cregs, 0, 15);
  448. }
  449. /*
  450. * Set a bit in a control register of all cpus
  451. */
  452. void smp_ctl_set_bit(int cr, int bit)
  453. {
  454. struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
  455. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  456. }
  457. EXPORT_SYMBOL(smp_ctl_set_bit);
  458. /*
  459. * Clear a bit in a control register of all cpus
  460. */
  461. void smp_ctl_clear_bit(int cr, int bit)
  462. {
  463. struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
  464. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  465. }
  466. EXPORT_SYMBOL(smp_ctl_clear_bit);
  467. #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
  468. static void __init smp_get_save_area(int cpu, u16 address)
  469. {
  470. void *lc = pcpu_devices[0].lowcore;
  471. struct save_area *save_area;
  472. if (is_kdump_kernel())
  473. return;
  474. if (!OLDMEM_BASE && (address == boot_cpu_address ||
  475. ipl_info.type != IPL_TYPE_FCP_DUMP))
  476. return;
  477. save_area = dump_save_area_create(cpu);
  478. if (!save_area)
  479. panic("could not allocate memory for save area\n");
  480. #ifdef CONFIG_CRASH_DUMP
  481. if (address == boot_cpu_address) {
  482. /* Copy the registers of the boot cpu. */
  483. copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
  484. SAVE_AREA_BASE - PAGE_SIZE, 0);
  485. return;
  486. }
  487. #endif
  488. /* Get the registers of a non-boot cpu. */
  489. __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
  490. memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
  491. }
  492. int smp_store_status(int cpu)
  493. {
  494. struct pcpu *pcpu;
  495. pcpu = pcpu_devices + cpu;
  496. if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
  497. 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
  498. return -EIO;
  499. return 0;
  500. }
  501. #else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
  502. static inline void smp_get_save_area(int cpu, u16 address) { }
  503. #endif /* CONFIG_ZFCPDUMP || 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. init_cpu_vtimer();
  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. return 0;
  659. }
  660. void __cpu_die(unsigned int cpu)
  661. {
  662. struct pcpu *pcpu;
  663. /* Wait until target cpu is down */
  664. pcpu = pcpu_devices + cpu;
  665. while (!pcpu_stopped(pcpu))
  666. cpu_relax();
  667. pcpu_free_lowcore(pcpu);
  668. atomic_dec(&init_mm.context.attach_count);
  669. }
  670. void __noreturn cpu_die(void)
  671. {
  672. idle_task_exit();
  673. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  674. for (;;) ;
  675. }
  676. #endif /* CONFIG_HOTPLUG_CPU */
  677. void __init smp_fill_possible_mask(void)
  678. {
  679. unsigned int possible, cpu;
  680. possible = setup_possible_cpus;
  681. if (!possible)
  682. possible = MACHINE_IS_VM ? 64 : nr_cpu_ids;
  683. for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
  684. set_cpu_possible(cpu, true);
  685. }
  686. void __init smp_prepare_cpus(unsigned int max_cpus)
  687. {
  688. /* request the 0x1201 emergency signal external interrupt */
  689. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  690. panic("Couldn't request external interrupt 0x1201");
  691. /* request the 0x1202 external call external interrupt */
  692. if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
  693. panic("Couldn't request external interrupt 0x1202");
  694. smp_detect_cpus();
  695. }
  696. void __init smp_prepare_boot_cpu(void)
  697. {
  698. struct pcpu *pcpu = pcpu_devices;
  699. boot_cpu_address = stap();
  700. pcpu->state = CPU_STATE_CONFIGURED;
  701. pcpu->address = boot_cpu_address;
  702. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  703. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE
  704. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  705. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE
  706. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  707. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  708. smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  709. set_cpu_present(0, true);
  710. set_cpu_online(0, true);
  711. }
  712. void __init smp_cpus_done(unsigned int max_cpus)
  713. {
  714. }
  715. void __init smp_setup_processor_id(void)
  716. {
  717. S390_lowcore.cpu_nr = 0;
  718. }
  719. /*
  720. * the frequency of the profiling timer can be changed
  721. * by writing a multiplier value into /proc/profile.
  722. *
  723. * usually you want to run this on all CPUs ;)
  724. */
  725. int setup_profiling_timer(unsigned int multiplier)
  726. {
  727. return 0;
  728. }
  729. #ifdef CONFIG_HOTPLUG_CPU
  730. static ssize_t cpu_configure_show(struct device *dev,
  731. struct device_attribute *attr, char *buf)
  732. {
  733. ssize_t count;
  734. mutex_lock(&smp_cpu_state_mutex);
  735. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  736. mutex_unlock(&smp_cpu_state_mutex);
  737. return count;
  738. }
  739. static ssize_t cpu_configure_store(struct device *dev,
  740. struct device_attribute *attr,
  741. const char *buf, size_t count)
  742. {
  743. struct pcpu *pcpu;
  744. int cpu, val, rc;
  745. char delim;
  746. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  747. return -EINVAL;
  748. if (val != 0 && val != 1)
  749. return -EINVAL;
  750. get_online_cpus();
  751. mutex_lock(&smp_cpu_state_mutex);
  752. rc = -EBUSY;
  753. /* disallow configuration changes of online cpus and cpu 0 */
  754. cpu = dev->id;
  755. if (cpu_online(cpu) || cpu == 0)
  756. goto out;
  757. pcpu = pcpu_devices + cpu;
  758. rc = 0;
  759. switch (val) {
  760. case 0:
  761. if (pcpu->state != CPU_STATE_CONFIGURED)
  762. break;
  763. rc = sclp_cpu_deconfigure(pcpu->address);
  764. if (rc)
  765. break;
  766. pcpu->state = CPU_STATE_STANDBY;
  767. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  768. topology_expect_change();
  769. break;
  770. case 1:
  771. if (pcpu->state != CPU_STATE_STANDBY)
  772. break;
  773. rc = sclp_cpu_configure(pcpu->address);
  774. if (rc)
  775. break;
  776. pcpu->state = CPU_STATE_CONFIGURED;
  777. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  778. topology_expect_change();
  779. break;
  780. default:
  781. break;
  782. }
  783. out:
  784. mutex_unlock(&smp_cpu_state_mutex);
  785. put_online_cpus();
  786. return rc ? rc : count;
  787. }
  788. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  789. #endif /* CONFIG_HOTPLUG_CPU */
  790. static ssize_t show_cpu_address(struct device *dev,
  791. struct device_attribute *attr, char *buf)
  792. {
  793. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  794. }
  795. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  796. static struct attribute *cpu_common_attrs[] = {
  797. #ifdef CONFIG_HOTPLUG_CPU
  798. &dev_attr_configure.attr,
  799. #endif
  800. &dev_attr_address.attr,
  801. NULL,
  802. };
  803. static struct attribute_group cpu_common_attr_group = {
  804. .attrs = cpu_common_attrs,
  805. };
  806. static ssize_t show_idle_count(struct device *dev,
  807. struct device_attribute *attr, char *buf)
  808. {
  809. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  810. unsigned long long idle_count;
  811. unsigned int sequence;
  812. do {
  813. sequence = ACCESS_ONCE(idle->sequence);
  814. idle_count = ACCESS_ONCE(idle->idle_count);
  815. if (ACCESS_ONCE(idle->clock_idle_enter))
  816. idle_count++;
  817. } while ((sequence & 1) || (ACCESS_ONCE(idle->sequence) != sequence));
  818. return sprintf(buf, "%llu\n", idle_count);
  819. }
  820. static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
  821. static ssize_t show_idle_time(struct device *dev,
  822. struct device_attribute *attr, char *buf)
  823. {
  824. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  825. unsigned long long now, idle_time, idle_enter, idle_exit;
  826. unsigned int sequence;
  827. do {
  828. now = get_tod_clock();
  829. sequence = ACCESS_ONCE(idle->sequence);
  830. idle_time = ACCESS_ONCE(idle->idle_time);
  831. idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
  832. idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
  833. } while ((sequence & 1) || (ACCESS_ONCE(idle->sequence) != sequence));
  834. idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  835. return sprintf(buf, "%llu\n", idle_time >> 12);
  836. }
  837. static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  838. static struct attribute *cpu_online_attrs[] = {
  839. &dev_attr_idle_count.attr,
  840. &dev_attr_idle_time_us.attr,
  841. NULL,
  842. };
  843. static struct attribute_group cpu_online_attr_group = {
  844. .attrs = cpu_online_attrs,
  845. };
  846. static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
  847. void *hcpu)
  848. {
  849. unsigned int cpu = (unsigned int)(long)hcpu;
  850. struct cpu *c = pcpu_devices[cpu].cpu;
  851. struct device *s = &c->dev;
  852. int err = 0;
  853. switch (action & ~CPU_TASKS_FROZEN) {
  854. case CPU_ONLINE:
  855. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  856. break;
  857. case CPU_DEAD:
  858. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  859. break;
  860. }
  861. return notifier_from_errno(err);
  862. }
  863. static int smp_add_present_cpu(int cpu)
  864. {
  865. struct device *s;
  866. struct cpu *c;
  867. int rc;
  868. c = kzalloc(sizeof(*c), GFP_KERNEL);
  869. if (!c)
  870. return -ENOMEM;
  871. pcpu_devices[cpu].cpu = c;
  872. s = &c->dev;
  873. c->hotpluggable = 1;
  874. rc = register_cpu(c, cpu);
  875. if (rc)
  876. goto out;
  877. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  878. if (rc)
  879. goto out_cpu;
  880. if (cpu_online(cpu)) {
  881. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  882. if (rc)
  883. goto out_online;
  884. }
  885. rc = topology_cpu_init(c);
  886. if (rc)
  887. goto out_topology;
  888. return 0;
  889. out_topology:
  890. if (cpu_online(cpu))
  891. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  892. out_online:
  893. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  894. out_cpu:
  895. #ifdef CONFIG_HOTPLUG_CPU
  896. unregister_cpu(c);
  897. #endif
  898. out:
  899. return rc;
  900. }
  901. #ifdef CONFIG_HOTPLUG_CPU
  902. int __ref smp_rescan_cpus(void)
  903. {
  904. struct sclp_cpu_info *info;
  905. int nr;
  906. info = smp_get_cpu_info();
  907. if (!info)
  908. return -ENOMEM;
  909. get_online_cpus();
  910. mutex_lock(&smp_cpu_state_mutex);
  911. nr = __smp_rescan_cpus(info, 1);
  912. mutex_unlock(&smp_cpu_state_mutex);
  913. put_online_cpus();
  914. kfree(info);
  915. if (nr)
  916. topology_schedule_update();
  917. return 0;
  918. }
  919. static ssize_t __ref rescan_store(struct device *dev,
  920. struct device_attribute *attr,
  921. const char *buf,
  922. size_t count)
  923. {
  924. int rc;
  925. rc = smp_rescan_cpus();
  926. return rc ? rc : count;
  927. }
  928. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  929. #endif /* CONFIG_HOTPLUG_CPU */
  930. static int __init s390_smp_init(void)
  931. {
  932. int cpu, rc;
  933. hotcpu_notifier(smp_cpu_notify, 0);
  934. #ifdef CONFIG_HOTPLUG_CPU
  935. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  936. if (rc)
  937. return rc;
  938. #endif
  939. for_each_present_cpu(cpu) {
  940. rc = smp_add_present_cpu(cpu);
  941. if (rc)
  942. return rc;
  943. }
  944. return 0;
  945. }
  946. subsys_initcall(s390_smp_init);