smp.c 26 KB

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