smp.c 26 KB

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