smp.c 26 KB

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