smp.c 26 KB

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