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