smp.c 27 KB

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