smp.c 26 KB

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