smp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. set_bit(ec_bit, &pcpu->ec_mask);
  143. order = pcpu_running(pcpu) ?
  144. 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. 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. struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
  469. EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
  470. static void __init smp_get_save_area(int cpu, u16 address)
  471. {
  472. void *lc = pcpu_devices[0].lowcore;
  473. struct save_area *save_area;
  474. if (is_kdump_kernel())
  475. return;
  476. if (!OLDMEM_BASE && (address == boot_cpu_address ||
  477. ipl_info.type != IPL_TYPE_FCP_DUMP))
  478. return;
  479. if (cpu >= NR_CPUS) {
  480. pr_warning("CPU %i exceeds the maximum %i and is excluded "
  481. "from the dump\n", cpu, NR_CPUS - 1);
  482. return;
  483. }
  484. save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
  485. if (!save_area)
  486. panic("could not allocate memory for save area\n");
  487. zfcpdump_save_areas[cpu] = save_area;
  488. #ifdef CONFIG_CRASH_DUMP
  489. if (address == boot_cpu_address) {
  490. /* Copy the registers of the boot cpu. */
  491. copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
  492. SAVE_AREA_BASE - PAGE_SIZE, 0);
  493. return;
  494. }
  495. #endif
  496. /* Get the registers of a non-boot cpu. */
  497. __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
  498. memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
  499. }
  500. int smp_store_status(int cpu)
  501. {
  502. struct pcpu *pcpu;
  503. pcpu = pcpu_devices + cpu;
  504. if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
  505. 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
  506. return -EIO;
  507. return 0;
  508. }
  509. #else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
  510. static inline void smp_get_save_area(int cpu, u16 address) { }
  511. #endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
  512. void smp_cpu_set_polarization(int cpu, int val)
  513. {
  514. pcpu_devices[cpu].polarization = val;
  515. }
  516. int smp_cpu_get_polarization(int cpu)
  517. {
  518. return pcpu_devices[cpu].polarization;
  519. }
  520. static struct sclp_cpu_info *smp_get_cpu_info(void)
  521. {
  522. static int use_sigp_detection;
  523. struct sclp_cpu_info *info;
  524. int address;
  525. info = kzalloc(sizeof(*info), GFP_KERNEL);
  526. if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
  527. use_sigp_detection = 1;
  528. for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
  529. if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
  530. SIGP_CC_NOT_OPERATIONAL)
  531. continue;
  532. info->cpu[info->configured].address = address;
  533. info->configured++;
  534. }
  535. info->combined = info->configured;
  536. }
  537. return info;
  538. }
  539. static int smp_add_present_cpu(int cpu);
  540. static int __smp_rescan_cpus(struct sclp_cpu_info *info, int sysfs_add)
  541. {
  542. struct pcpu *pcpu;
  543. cpumask_t avail;
  544. int cpu, nr, i;
  545. nr = 0;
  546. cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
  547. cpu = cpumask_first(&avail);
  548. for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
  549. if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
  550. continue;
  551. if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
  552. continue;
  553. pcpu = pcpu_devices + cpu;
  554. pcpu->address = info->cpu[i].address;
  555. pcpu->state = (i >= info->configured) ?
  556. CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
  557. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  558. set_cpu_present(cpu, true);
  559. if (sysfs_add && smp_add_present_cpu(cpu) != 0)
  560. set_cpu_present(cpu, false);
  561. else
  562. nr++;
  563. cpu = cpumask_next(cpu, &avail);
  564. }
  565. return nr;
  566. }
  567. static void __init smp_detect_cpus(void)
  568. {
  569. unsigned int cpu, c_cpus, s_cpus;
  570. struct sclp_cpu_info *info;
  571. info = smp_get_cpu_info();
  572. if (!info)
  573. panic("smp_detect_cpus failed to allocate memory\n");
  574. if (info->has_cpu_type) {
  575. for (cpu = 0; cpu < info->combined; cpu++) {
  576. if (info->cpu[cpu].address != boot_cpu_address)
  577. continue;
  578. /* The boot cpu dictates the cpu type. */
  579. boot_cpu_type = info->cpu[cpu].type;
  580. break;
  581. }
  582. }
  583. c_cpus = s_cpus = 0;
  584. for (cpu = 0; cpu < info->combined; cpu++) {
  585. if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
  586. continue;
  587. if (cpu < info->configured) {
  588. smp_get_save_area(c_cpus, info->cpu[cpu].address);
  589. c_cpus++;
  590. } else
  591. s_cpus++;
  592. }
  593. pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
  594. get_online_cpus();
  595. __smp_rescan_cpus(info, 0);
  596. put_online_cpus();
  597. kfree(info);
  598. }
  599. /*
  600. * Activate a secondary processor.
  601. */
  602. static void smp_start_secondary(void *cpuvoid)
  603. {
  604. S390_lowcore.last_update_clock = get_tod_clock();
  605. S390_lowcore.restart_stack = (unsigned long) restart_stack;
  606. S390_lowcore.restart_fn = (unsigned long) do_restart;
  607. S390_lowcore.restart_data = 0;
  608. S390_lowcore.restart_source = -1UL;
  609. restore_access_regs(S390_lowcore.access_regs_save_area);
  610. __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
  611. __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
  612. cpu_init();
  613. preempt_disable();
  614. init_cpu_timer();
  615. init_cpu_vtimer();
  616. pfault_init();
  617. notify_cpu_starting(smp_processor_id());
  618. set_cpu_online(smp_processor_id(), true);
  619. inc_irq_stat(CPU_RST);
  620. local_irq_enable();
  621. cpu_startup_entry(CPUHP_ONLINE);
  622. }
  623. /* Upping and downing of CPUs */
  624. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  625. {
  626. struct pcpu *pcpu;
  627. int rc;
  628. pcpu = pcpu_devices + cpu;
  629. if (pcpu->state != CPU_STATE_CONFIGURED)
  630. return -EIO;
  631. if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
  632. SIGP_CC_ORDER_CODE_ACCEPTED)
  633. return -EIO;
  634. rc = pcpu_alloc_lowcore(pcpu, cpu);
  635. if (rc)
  636. return rc;
  637. pcpu_prepare_secondary(pcpu, cpu);
  638. pcpu_attach_task(pcpu, tidle);
  639. pcpu_start_fn(pcpu, smp_start_secondary, NULL);
  640. while (!cpu_online(cpu))
  641. cpu_relax();
  642. return 0;
  643. }
  644. static int __init setup_possible_cpus(char *s)
  645. {
  646. int max, cpu;
  647. if (kstrtoint(s, 0, &max) < 0)
  648. return 0;
  649. init_cpu_possible(cpumask_of(0));
  650. for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
  651. set_cpu_possible(cpu, true);
  652. return 0;
  653. }
  654. early_param("possible_cpus", setup_possible_cpus);
  655. #ifdef CONFIG_HOTPLUG_CPU
  656. int __cpu_disable(void)
  657. {
  658. unsigned long cregs[16];
  659. /* Handle possible pending IPIs */
  660. smp_handle_ext_call();
  661. set_cpu_online(smp_processor_id(), false);
  662. /* Disable pseudo page faults on this cpu. */
  663. pfault_fini();
  664. /* Disable interrupt sources via control register. */
  665. __ctl_store(cregs, 0, 15);
  666. cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
  667. cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
  668. cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
  669. __ctl_load(cregs, 0, 15);
  670. return 0;
  671. }
  672. void __cpu_die(unsigned int cpu)
  673. {
  674. struct pcpu *pcpu;
  675. /* Wait until target cpu is down */
  676. pcpu = pcpu_devices + cpu;
  677. while (!pcpu_stopped(pcpu))
  678. cpu_relax();
  679. pcpu_free_lowcore(pcpu);
  680. atomic_dec(&init_mm.context.attach_count);
  681. }
  682. void __noreturn cpu_die(void)
  683. {
  684. idle_task_exit();
  685. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  686. for (;;) ;
  687. }
  688. #endif /* CONFIG_HOTPLUG_CPU */
  689. void __init smp_prepare_cpus(unsigned int max_cpus)
  690. {
  691. /* request the 0x1201 emergency signal external interrupt */
  692. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  693. panic("Couldn't request external interrupt 0x1201");
  694. /* request the 0x1202 external call external interrupt */
  695. if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
  696. panic("Couldn't request external interrupt 0x1202");
  697. smp_detect_cpus();
  698. }
  699. void __init smp_prepare_boot_cpu(void)
  700. {
  701. struct pcpu *pcpu = pcpu_devices;
  702. boot_cpu_address = stap();
  703. pcpu->state = CPU_STATE_CONFIGURED;
  704. pcpu->address = boot_cpu_address;
  705. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  706. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE
  707. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  708. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE
  709. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  710. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  711. smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  712. set_cpu_present(0, true);
  713. set_cpu_online(0, true);
  714. }
  715. void __init smp_cpus_done(unsigned int max_cpus)
  716. {
  717. }
  718. void __init smp_setup_processor_id(void)
  719. {
  720. S390_lowcore.cpu_nr = 0;
  721. }
  722. /*
  723. * the frequency of the profiling timer can be changed
  724. * by writing a multiplier value into /proc/profile.
  725. *
  726. * usually you want to run this on all CPUs ;)
  727. */
  728. int setup_profiling_timer(unsigned int multiplier)
  729. {
  730. return 0;
  731. }
  732. #ifdef CONFIG_HOTPLUG_CPU
  733. static ssize_t cpu_configure_show(struct device *dev,
  734. struct device_attribute *attr, char *buf)
  735. {
  736. ssize_t count;
  737. mutex_lock(&smp_cpu_state_mutex);
  738. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  739. mutex_unlock(&smp_cpu_state_mutex);
  740. return count;
  741. }
  742. static ssize_t cpu_configure_store(struct device *dev,
  743. struct device_attribute *attr,
  744. const char *buf, size_t count)
  745. {
  746. struct pcpu *pcpu;
  747. int cpu, val, rc;
  748. char delim;
  749. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  750. return -EINVAL;
  751. if (val != 0 && val != 1)
  752. return -EINVAL;
  753. get_online_cpus();
  754. mutex_lock(&smp_cpu_state_mutex);
  755. rc = -EBUSY;
  756. /* disallow configuration changes of online cpus and cpu 0 */
  757. cpu = dev->id;
  758. if (cpu_online(cpu) || cpu == 0)
  759. goto out;
  760. pcpu = pcpu_devices + cpu;
  761. rc = 0;
  762. switch (val) {
  763. case 0:
  764. if (pcpu->state != CPU_STATE_CONFIGURED)
  765. break;
  766. rc = sclp_cpu_deconfigure(pcpu->address);
  767. if (rc)
  768. break;
  769. pcpu->state = CPU_STATE_STANDBY;
  770. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  771. topology_expect_change();
  772. break;
  773. case 1:
  774. if (pcpu->state != CPU_STATE_STANDBY)
  775. break;
  776. rc = sclp_cpu_configure(pcpu->address);
  777. if (rc)
  778. break;
  779. pcpu->state = CPU_STATE_CONFIGURED;
  780. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  781. topology_expect_change();
  782. break;
  783. default:
  784. break;
  785. }
  786. out:
  787. mutex_unlock(&smp_cpu_state_mutex);
  788. put_online_cpus();
  789. return rc ? rc : count;
  790. }
  791. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  792. #endif /* CONFIG_HOTPLUG_CPU */
  793. static ssize_t show_cpu_address(struct device *dev,
  794. struct device_attribute *attr, char *buf)
  795. {
  796. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  797. }
  798. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  799. static struct attribute *cpu_common_attrs[] = {
  800. #ifdef CONFIG_HOTPLUG_CPU
  801. &dev_attr_configure.attr,
  802. #endif
  803. &dev_attr_address.attr,
  804. NULL,
  805. };
  806. static struct attribute_group cpu_common_attr_group = {
  807. .attrs = cpu_common_attrs,
  808. };
  809. static ssize_t show_idle_count(struct device *dev,
  810. struct device_attribute *attr, char *buf)
  811. {
  812. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  813. unsigned long long idle_count;
  814. unsigned int sequence;
  815. do {
  816. sequence = ACCESS_ONCE(idle->sequence);
  817. idle_count = ACCESS_ONCE(idle->idle_count);
  818. if (ACCESS_ONCE(idle->clock_idle_enter))
  819. idle_count++;
  820. } while ((sequence & 1) || (idle->sequence != sequence));
  821. return sprintf(buf, "%llu\n", idle_count);
  822. }
  823. static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
  824. static ssize_t show_idle_time(struct device *dev,
  825. struct device_attribute *attr, char *buf)
  826. {
  827. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  828. unsigned long long now, idle_time, idle_enter, idle_exit;
  829. unsigned int sequence;
  830. do {
  831. now = get_tod_clock();
  832. sequence = ACCESS_ONCE(idle->sequence);
  833. idle_time = ACCESS_ONCE(idle->idle_time);
  834. idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
  835. idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
  836. } while ((sequence & 1) || (idle->sequence != sequence));
  837. idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  838. return sprintf(buf, "%llu\n", idle_time >> 12);
  839. }
  840. static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  841. static struct attribute *cpu_online_attrs[] = {
  842. &dev_attr_idle_count.attr,
  843. &dev_attr_idle_time_us.attr,
  844. NULL,
  845. };
  846. static struct attribute_group cpu_online_attr_group = {
  847. .attrs = cpu_online_attrs,
  848. };
  849. static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
  850. void *hcpu)
  851. {
  852. unsigned int cpu = (unsigned int)(long)hcpu;
  853. struct cpu *c = &pcpu_devices[cpu].cpu;
  854. struct device *s = &c->dev;
  855. int err = 0;
  856. switch (action & ~CPU_TASKS_FROZEN) {
  857. case CPU_ONLINE:
  858. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  859. break;
  860. case CPU_DEAD:
  861. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  862. break;
  863. }
  864. return notifier_from_errno(err);
  865. }
  866. static int smp_add_present_cpu(int cpu)
  867. {
  868. struct cpu *c = &pcpu_devices[cpu].cpu;
  869. struct device *s = &c->dev;
  870. int rc;
  871. c->hotpluggable = 1;
  872. rc = register_cpu(c, cpu);
  873. if (rc)
  874. goto out;
  875. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  876. if (rc)
  877. goto out_cpu;
  878. if (cpu_online(cpu)) {
  879. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  880. if (rc)
  881. goto out_online;
  882. }
  883. rc = topology_cpu_init(c);
  884. if (rc)
  885. goto out_topology;
  886. return 0;
  887. out_topology:
  888. if (cpu_online(cpu))
  889. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  890. out_online:
  891. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  892. out_cpu:
  893. #ifdef CONFIG_HOTPLUG_CPU
  894. unregister_cpu(c);
  895. #endif
  896. out:
  897. return rc;
  898. }
  899. #ifdef CONFIG_HOTPLUG_CPU
  900. int __ref smp_rescan_cpus(void)
  901. {
  902. struct sclp_cpu_info *info;
  903. int nr;
  904. info = smp_get_cpu_info();
  905. if (!info)
  906. return -ENOMEM;
  907. get_online_cpus();
  908. mutex_lock(&smp_cpu_state_mutex);
  909. nr = __smp_rescan_cpus(info, 1);
  910. mutex_unlock(&smp_cpu_state_mutex);
  911. put_online_cpus();
  912. kfree(info);
  913. if (nr)
  914. topology_schedule_update();
  915. return 0;
  916. }
  917. static ssize_t __ref rescan_store(struct device *dev,
  918. struct device_attribute *attr,
  919. const char *buf,
  920. size_t count)
  921. {
  922. int rc;
  923. rc = smp_rescan_cpus();
  924. return rc ? rc : count;
  925. }
  926. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  927. #endif /* CONFIG_HOTPLUG_CPU */
  928. static int __init s390_smp_init(void)
  929. {
  930. int cpu, rc;
  931. hotcpu_notifier(smp_cpu_notify, 0);
  932. #ifdef CONFIG_HOTPLUG_CPU
  933. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  934. if (rc)
  935. return rc;
  936. #endif
  937. for_each_present_cpu(cpu) {
  938. rc = smp_add_present_cpu(cpu);
  939. if (rc)
  940. return rc;
  941. }
  942. return 0;
  943. }
  944. subsys_initcall(s390_smp_init);