smp.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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 task_struct *idle; /* idle process for the cpu */
  82. struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
  83. unsigned long async_stack; /* async stack for the cpu */
  84. unsigned long panic_stack; /* panic stack for the cpu */
  85. unsigned long ec_mask; /* bit mask for ec_xxx functions */
  86. int state; /* physical cpu state */
  87. u32 status; /* last status received via sigp */
  88. u16 address; /* physical cpu address */
  89. };
  90. static u8 boot_cpu_type;
  91. static u16 boot_cpu_address;
  92. static struct pcpu pcpu_devices[NR_CPUS];
  93. DEFINE_MUTEX(smp_cpu_state_mutex);
  94. /*
  95. * Signal processor helper functions.
  96. */
  97. static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
  98. {
  99. register unsigned int reg1 asm ("1") = parm;
  100. int cc;
  101. asm volatile(
  102. " sigp %1,%2,0(%3)\n"
  103. " ipm %0\n"
  104. " srl %0,28\n"
  105. : "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
  106. if (status && cc == 1)
  107. *status = reg1;
  108. return cc;
  109. }
  110. static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
  111. {
  112. int cc;
  113. while (1) {
  114. cc = __pcpu_sigp(addr, order, parm, status);
  115. if (cc != sigp_busy)
  116. return cc;
  117. cpu_relax();
  118. }
  119. }
  120. static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
  121. {
  122. int cc, retry;
  123. for (retry = 0; ; retry++) {
  124. cc = __pcpu_sigp(pcpu->address, order, parm, &pcpu->status);
  125. if (cc != sigp_busy)
  126. break;
  127. if (retry >= 3)
  128. udelay(10);
  129. }
  130. return cc;
  131. }
  132. static inline int pcpu_stopped(struct pcpu *pcpu)
  133. {
  134. if (__pcpu_sigp(pcpu->address, sigp_sense,
  135. 0, &pcpu->status) != sigp_status_stored)
  136. return 0;
  137. /* Check for stopped and check stop state */
  138. return !!(pcpu->status & 0x50);
  139. }
  140. static inline int pcpu_running(struct pcpu *pcpu)
  141. {
  142. if (__pcpu_sigp(pcpu->address, sigp_sense_running,
  143. 0, &pcpu->status) != sigp_status_stored)
  144. return 1;
  145. /* Check for running status */
  146. return !(pcpu->status & 0x400);
  147. }
  148. /*
  149. * Find struct pcpu by cpu address.
  150. */
  151. static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
  152. {
  153. int cpu;
  154. for_each_cpu(cpu, mask)
  155. if (pcpu_devices[cpu].address == address)
  156. return pcpu_devices + cpu;
  157. return NULL;
  158. }
  159. static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
  160. {
  161. int order;
  162. set_bit(ec_bit, &pcpu->ec_mask);
  163. order = pcpu_running(pcpu) ?
  164. sigp_external_call : sigp_emergency_signal;
  165. pcpu_sigp_retry(pcpu, order, 0);
  166. }
  167. static int __cpuinit pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
  168. {
  169. struct _lowcore *lc;
  170. if (pcpu != &pcpu_devices[0]) {
  171. pcpu->lowcore = (struct _lowcore *)
  172. __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  173. pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  174. pcpu->panic_stack = __get_free_page(GFP_KERNEL);
  175. if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
  176. goto out;
  177. }
  178. lc = pcpu->lowcore;
  179. memcpy(lc, &S390_lowcore, 512);
  180. memset((char *) lc + 512, 0, sizeof(*lc) - 512);
  181. lc->async_stack = pcpu->async_stack + ASYNC_SIZE;
  182. lc->panic_stack = pcpu->panic_stack + PAGE_SIZE;
  183. lc->cpu_nr = cpu;
  184. #ifndef CONFIG_64BIT
  185. if (MACHINE_HAS_IEEE) {
  186. lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
  187. if (!lc->extended_save_area_addr)
  188. goto out;
  189. }
  190. #else
  191. if (vdso_alloc_per_cpu(lc))
  192. goto out;
  193. #endif
  194. lowcore_ptr[cpu] = lc;
  195. pcpu_sigp_retry(pcpu, sigp_set_prefix, (u32)(unsigned long) lc);
  196. return 0;
  197. out:
  198. if (pcpu != &pcpu_devices[0]) {
  199. free_page(pcpu->panic_stack);
  200. free_pages(pcpu->async_stack, ASYNC_ORDER);
  201. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  202. }
  203. return -ENOMEM;
  204. }
  205. #ifdef CONFIG_HOTPLUG_CPU
  206. static void pcpu_free_lowcore(struct pcpu *pcpu)
  207. {
  208. pcpu_sigp_retry(pcpu, sigp_set_prefix, 0);
  209. lowcore_ptr[pcpu - pcpu_devices] = NULL;
  210. #ifndef CONFIG_64BIT
  211. if (MACHINE_HAS_IEEE) {
  212. struct _lowcore *lc = pcpu->lowcore;
  213. free_page((unsigned long) lc->extended_save_area_addr);
  214. lc->extended_save_area_addr = 0;
  215. }
  216. #else
  217. vdso_free_per_cpu(pcpu->lowcore);
  218. #endif
  219. if (pcpu != &pcpu_devices[0]) {
  220. free_page(pcpu->panic_stack);
  221. free_pages(pcpu->async_stack, ASYNC_ORDER);
  222. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  223. }
  224. }
  225. #endif /* CONFIG_HOTPLUG_CPU */
  226. static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
  227. {
  228. struct _lowcore *lc = pcpu->lowcore;
  229. atomic_inc(&init_mm.context.attach_count);
  230. lc->cpu_nr = cpu;
  231. lc->percpu_offset = __per_cpu_offset[cpu];
  232. lc->kernel_asce = S390_lowcore.kernel_asce;
  233. lc->machine_flags = S390_lowcore.machine_flags;
  234. lc->ftrace_func = S390_lowcore.ftrace_func;
  235. lc->user_timer = lc->system_timer = lc->steal_timer = 0;
  236. __ctl_store(lc->cregs_save_area, 0, 15);
  237. save_access_regs((unsigned int *) lc->access_regs_save_area);
  238. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  239. MAX_FACILITY_BIT/8);
  240. }
  241. static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
  242. {
  243. struct _lowcore *lc = pcpu->lowcore;
  244. struct thread_info *ti = task_thread_info(tsk);
  245. lc->kernel_stack = (unsigned long) task_stack_page(tsk) + THREAD_SIZE;
  246. lc->thread_info = (unsigned long) task_thread_info(tsk);
  247. lc->current_task = (unsigned long) tsk;
  248. lc->user_timer = ti->user_timer;
  249. lc->system_timer = ti->system_timer;
  250. lc->steal_timer = 0;
  251. }
  252. static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
  253. {
  254. struct _lowcore *lc = pcpu->lowcore;
  255. lc->restart_stack = lc->kernel_stack;
  256. lc->restart_fn = (unsigned long) func;
  257. lc->restart_data = (unsigned long) data;
  258. lc->restart_source = -1UL;
  259. pcpu_sigp_retry(pcpu, sigp_restart, 0);
  260. }
  261. /*
  262. * Call function via PSW restart on pcpu and stop the current cpu.
  263. */
  264. static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
  265. void *data, unsigned long stack)
  266. {
  267. struct _lowcore *lc = pcpu->lowcore;
  268. unsigned short this_cpu;
  269. __load_psw_mask(psw_kernel_bits);
  270. this_cpu = stap();
  271. if (pcpu->address == this_cpu)
  272. func(data); /* should not return */
  273. /* Stop target cpu (if func returns this stops the current cpu). */
  274. pcpu_sigp_retry(pcpu, sigp_stop, 0);
  275. /* Restart func on the target cpu and stop the current cpu. */
  276. lc->restart_stack = stack;
  277. lc->restart_fn = (unsigned long) func;
  278. lc->restart_data = (unsigned long) data;
  279. lc->restart_source = (unsigned long) this_cpu;
  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" (this_cpu) : "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. struct create_idle {
  640. struct work_struct work;
  641. struct task_struct *idle;
  642. struct completion done;
  643. int cpu;
  644. };
  645. static void __cpuinit smp_fork_idle(struct work_struct *work)
  646. {
  647. struct create_idle *c_idle;
  648. c_idle = container_of(work, struct create_idle, work);
  649. c_idle->idle = fork_idle(c_idle->cpu);
  650. complete(&c_idle->done);
  651. }
  652. /* Upping and downing of CPUs */
  653. int __cpuinit __cpu_up(unsigned int cpu)
  654. {
  655. struct create_idle c_idle;
  656. struct pcpu *pcpu;
  657. int rc;
  658. pcpu = pcpu_devices + cpu;
  659. if (pcpu->state != CPU_STATE_CONFIGURED)
  660. return -EIO;
  661. if (pcpu_sigp_retry(pcpu, sigp_initial_cpu_reset, 0) !=
  662. sigp_order_code_accepted)
  663. return -EIO;
  664. if (!pcpu->idle) {
  665. c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
  666. INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
  667. c_idle.cpu = cpu;
  668. schedule_work(&c_idle.work);
  669. wait_for_completion(&c_idle.done);
  670. if (IS_ERR(c_idle.idle))
  671. return PTR_ERR(c_idle.idle);
  672. pcpu->idle = c_idle.idle;
  673. }
  674. init_idle(pcpu->idle, cpu);
  675. rc = pcpu_alloc_lowcore(pcpu, cpu);
  676. if (rc)
  677. return rc;
  678. pcpu_prepare_secondary(pcpu, cpu);
  679. pcpu_attach_task(pcpu, pcpu->idle);
  680. pcpu_start_fn(pcpu, smp_start_secondary, NULL);
  681. while (!cpu_online(cpu))
  682. cpu_relax();
  683. return 0;
  684. }
  685. static int __init setup_possible_cpus(char *s)
  686. {
  687. int max, cpu;
  688. if (kstrtoint(s, 0, &max) < 0)
  689. return 0;
  690. init_cpu_possible(cpumask_of(0));
  691. for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
  692. set_cpu_possible(cpu, true);
  693. return 0;
  694. }
  695. early_param("possible_cpus", setup_possible_cpus);
  696. #ifdef CONFIG_HOTPLUG_CPU
  697. int __cpu_disable(void)
  698. {
  699. unsigned long cregs[16];
  700. set_cpu_online(smp_processor_id(), false);
  701. /* Disable pseudo page faults on this cpu. */
  702. pfault_fini();
  703. /* Disable interrupt sources via control register. */
  704. __ctl_store(cregs, 0, 15);
  705. cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
  706. cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
  707. cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
  708. __ctl_load(cregs, 0, 15);
  709. return 0;
  710. }
  711. void __cpu_die(unsigned int cpu)
  712. {
  713. struct pcpu *pcpu;
  714. /* Wait until target cpu is down */
  715. pcpu = pcpu_devices + cpu;
  716. while (!pcpu_stopped(pcpu))
  717. cpu_relax();
  718. pcpu_free_lowcore(pcpu);
  719. atomic_dec(&init_mm.context.attach_count);
  720. }
  721. void __noreturn cpu_die(void)
  722. {
  723. idle_task_exit();
  724. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), sigp_stop, 0);
  725. for (;;) ;
  726. }
  727. #endif /* CONFIG_HOTPLUG_CPU */
  728. static void smp_call_os_info_init_fn(void)
  729. {
  730. int (*init_fn)(void);
  731. unsigned long size;
  732. init_fn = os_info_old_entry(OS_INFO_INIT_FN, &size);
  733. if (!init_fn)
  734. return;
  735. init_fn();
  736. }
  737. void __init smp_prepare_cpus(unsigned int max_cpus)
  738. {
  739. /* request the 0x1201 emergency signal external interrupt */
  740. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  741. panic("Couldn't request external interrupt 0x1201");
  742. /* request the 0x1202 external call external interrupt */
  743. if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
  744. panic("Couldn't request external interrupt 0x1202");
  745. smp_call_os_info_init_fn();
  746. smp_detect_cpus();
  747. }
  748. void __init smp_prepare_boot_cpu(void)
  749. {
  750. struct pcpu *pcpu = pcpu_devices;
  751. boot_cpu_address = stap();
  752. pcpu->idle = current;
  753. pcpu->state = CPU_STATE_CONFIGURED;
  754. pcpu->address = boot_cpu_address;
  755. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  756. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
  757. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
  758. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  759. cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  760. set_cpu_present(0, true);
  761. set_cpu_online(0, true);
  762. }
  763. void __init smp_cpus_done(unsigned int max_cpus)
  764. {
  765. }
  766. void __init smp_setup_processor_id(void)
  767. {
  768. S390_lowcore.cpu_nr = 0;
  769. }
  770. /*
  771. * the frequency of the profiling timer can be changed
  772. * by writing a multiplier value into /proc/profile.
  773. *
  774. * usually you want to run this on all CPUs ;)
  775. */
  776. int setup_profiling_timer(unsigned int multiplier)
  777. {
  778. return 0;
  779. }
  780. #ifdef CONFIG_HOTPLUG_CPU
  781. static ssize_t cpu_configure_show(struct device *dev,
  782. struct device_attribute *attr, char *buf)
  783. {
  784. ssize_t count;
  785. mutex_lock(&smp_cpu_state_mutex);
  786. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  787. mutex_unlock(&smp_cpu_state_mutex);
  788. return count;
  789. }
  790. static ssize_t cpu_configure_store(struct device *dev,
  791. struct device_attribute *attr,
  792. const char *buf, size_t count)
  793. {
  794. struct pcpu *pcpu;
  795. int cpu, val, rc;
  796. char delim;
  797. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  798. return -EINVAL;
  799. if (val != 0 && val != 1)
  800. return -EINVAL;
  801. get_online_cpus();
  802. mutex_lock(&smp_cpu_state_mutex);
  803. rc = -EBUSY;
  804. /* disallow configuration changes of online cpus and cpu 0 */
  805. cpu = dev->id;
  806. if (cpu_online(cpu) || cpu == 0)
  807. goto out;
  808. pcpu = pcpu_devices + cpu;
  809. rc = 0;
  810. switch (val) {
  811. case 0:
  812. if (pcpu->state != CPU_STATE_CONFIGURED)
  813. break;
  814. rc = sclp_cpu_deconfigure(pcpu->address);
  815. if (rc)
  816. break;
  817. pcpu->state = CPU_STATE_STANDBY;
  818. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  819. topology_expect_change();
  820. break;
  821. case 1:
  822. if (pcpu->state != CPU_STATE_STANDBY)
  823. break;
  824. rc = sclp_cpu_configure(pcpu->address);
  825. if (rc)
  826. break;
  827. pcpu->state = CPU_STATE_CONFIGURED;
  828. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  829. topology_expect_change();
  830. break;
  831. default:
  832. break;
  833. }
  834. out:
  835. mutex_unlock(&smp_cpu_state_mutex);
  836. put_online_cpus();
  837. return rc ? rc : count;
  838. }
  839. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  840. #endif /* CONFIG_HOTPLUG_CPU */
  841. static ssize_t show_cpu_address(struct device *dev,
  842. struct device_attribute *attr, char *buf)
  843. {
  844. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  845. }
  846. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  847. static struct attribute *cpu_common_attrs[] = {
  848. #ifdef CONFIG_HOTPLUG_CPU
  849. &dev_attr_configure.attr,
  850. #endif
  851. &dev_attr_address.attr,
  852. NULL,
  853. };
  854. static struct attribute_group cpu_common_attr_group = {
  855. .attrs = cpu_common_attrs,
  856. };
  857. static ssize_t show_capability(struct device *dev,
  858. struct device_attribute *attr, char *buf)
  859. {
  860. unsigned int capability;
  861. int rc;
  862. rc = get_cpu_capability(&capability);
  863. if (rc)
  864. return rc;
  865. return sprintf(buf, "%u\n", capability);
  866. }
  867. static DEVICE_ATTR(capability, 0444, show_capability, NULL);
  868. static ssize_t show_idle_count(struct device *dev,
  869. struct device_attribute *attr, char *buf)
  870. {
  871. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  872. unsigned long long idle_count;
  873. unsigned int sequence;
  874. do {
  875. sequence = ACCESS_ONCE(idle->sequence);
  876. idle_count = ACCESS_ONCE(idle->idle_count);
  877. if (ACCESS_ONCE(idle->idle_enter))
  878. idle_count++;
  879. } while ((sequence & 1) || (idle->sequence != sequence));
  880. return sprintf(buf, "%llu\n", idle_count);
  881. }
  882. static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
  883. static ssize_t show_idle_time(struct device *dev,
  884. struct device_attribute *attr, char *buf)
  885. {
  886. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  887. unsigned long long now, idle_time, idle_enter, idle_exit;
  888. unsigned int sequence;
  889. do {
  890. now = get_clock();
  891. sequence = ACCESS_ONCE(idle->sequence);
  892. idle_time = ACCESS_ONCE(idle->idle_time);
  893. idle_enter = ACCESS_ONCE(idle->idle_enter);
  894. idle_exit = ACCESS_ONCE(idle->idle_exit);
  895. } while ((sequence & 1) || (idle->sequence != sequence));
  896. idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  897. return sprintf(buf, "%llu\n", idle_time >> 12);
  898. }
  899. static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  900. static struct attribute *cpu_online_attrs[] = {
  901. &dev_attr_capability.attr,
  902. &dev_attr_idle_count.attr,
  903. &dev_attr_idle_time_us.attr,
  904. NULL,
  905. };
  906. static struct attribute_group cpu_online_attr_group = {
  907. .attrs = cpu_online_attrs,
  908. };
  909. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  910. unsigned long action, void *hcpu)
  911. {
  912. unsigned int cpu = (unsigned int)(long)hcpu;
  913. struct cpu *c = &pcpu_devices[cpu].cpu;
  914. struct device *s = &c->dev;
  915. struct s390_idle_data *idle;
  916. int err = 0;
  917. switch (action) {
  918. case CPU_ONLINE:
  919. case CPU_ONLINE_FROZEN:
  920. idle = &per_cpu(s390_idle, cpu);
  921. memset(idle, 0, sizeof(struct s390_idle_data));
  922. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  923. break;
  924. case CPU_DEAD:
  925. case CPU_DEAD_FROZEN:
  926. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  927. break;
  928. }
  929. return notifier_from_errno(err);
  930. }
  931. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  932. .notifier_call = smp_cpu_notify,
  933. };
  934. static int __devinit smp_add_present_cpu(int cpu)
  935. {
  936. struct cpu *c = &pcpu_devices[cpu].cpu;
  937. struct device *s = &c->dev;
  938. int rc;
  939. c->hotpluggable = 1;
  940. rc = register_cpu(c, cpu);
  941. if (rc)
  942. goto out;
  943. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  944. if (rc)
  945. goto out_cpu;
  946. if (cpu_online(cpu)) {
  947. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  948. if (rc)
  949. goto out_online;
  950. }
  951. rc = topology_cpu_init(c);
  952. if (rc)
  953. goto out_topology;
  954. return 0;
  955. out_topology:
  956. if (cpu_online(cpu))
  957. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  958. out_online:
  959. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  960. out_cpu:
  961. #ifdef CONFIG_HOTPLUG_CPU
  962. unregister_cpu(c);
  963. #endif
  964. out:
  965. return rc;
  966. }
  967. #ifdef CONFIG_HOTPLUG_CPU
  968. int __ref smp_rescan_cpus(void)
  969. {
  970. struct sclp_cpu_info *info;
  971. int nr;
  972. info = smp_get_cpu_info();
  973. if (!info)
  974. return -ENOMEM;
  975. get_online_cpus();
  976. mutex_lock(&smp_cpu_state_mutex);
  977. nr = __smp_rescan_cpus(info, 1);
  978. mutex_unlock(&smp_cpu_state_mutex);
  979. put_online_cpus();
  980. kfree(info);
  981. if (nr)
  982. topology_schedule_update();
  983. return 0;
  984. }
  985. static ssize_t __ref rescan_store(struct device *dev,
  986. struct device_attribute *attr,
  987. const char *buf,
  988. size_t count)
  989. {
  990. int rc;
  991. rc = smp_rescan_cpus();
  992. return rc ? rc : count;
  993. }
  994. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  995. #endif /* CONFIG_HOTPLUG_CPU */
  996. static int __init s390_smp_init(void)
  997. {
  998. int cpu, rc;
  999. register_cpu_notifier(&smp_cpu_nb);
  1000. #ifdef CONFIG_HOTPLUG_CPU
  1001. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  1002. if (rc)
  1003. return rc;
  1004. #endif
  1005. for_each_present_cpu(cpu) {
  1006. rc = smp_add_present_cpu(cpu);
  1007. if (rc)
  1008. return rc;
  1009. }
  1010. return 0;
  1011. }
  1012. subsys_initcall(s390_smp_init);