smp.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /*
  2. * arch/s390/kernel/smp.c
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
  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. * We work with logical cpu numbering everywhere we can. The only
  14. * functions using the real cpu address (got from STAP) are the sigp
  15. * functions. For all other functions we use the identity mapping.
  16. * That means that cpu_number_map[i] == i for every cpu. cpu_number_map is
  17. * used e.g. to find the idle task belonging to a logical cpu. Every array
  18. * in the kernel is sorted by the logical cpu number and not by the physical
  19. * one which is causing all the confusion with __cpu_logical_map and
  20. * cpu_number_map in other architectures.
  21. */
  22. #define KMSG_COMPONENT "cpu"
  23. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  24. #include <linux/workqueue.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/mm.h>
  28. #include <linux/err.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/delay.h>
  32. #include <linux/cache.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/irqflags.h>
  35. #include <linux/cpu.h>
  36. #include <linux/timex.h>
  37. #include <linux/bootmem.h>
  38. #include <linux/slab.h>
  39. #include <asm/asm-offsets.h>
  40. #include <asm/ipl.h>
  41. #include <asm/setup.h>
  42. #include <asm/sigp.h>
  43. #include <asm/pgalloc.h>
  44. #include <asm/irq.h>
  45. #include <asm/cpcmd.h>
  46. #include <asm/tlbflush.h>
  47. #include <asm/timer.h>
  48. #include <asm/lowcore.h>
  49. #include <asm/sclp.h>
  50. #include <asm/cputime.h>
  51. #include <asm/vdso.h>
  52. #include <asm/cpu.h>
  53. #include "entry.h"
  54. /* logical cpu to cpu address */
  55. unsigned short __cpu_logical_map[NR_CPUS];
  56. static struct task_struct *current_set[NR_CPUS];
  57. static u8 smp_cpu_type;
  58. static int smp_use_sigp_detection;
  59. enum s390_cpu_state {
  60. CPU_STATE_STANDBY,
  61. CPU_STATE_CONFIGURED,
  62. };
  63. DEFINE_MUTEX(smp_cpu_state_mutex);
  64. int smp_cpu_polarization[NR_CPUS];
  65. static int smp_cpu_state[NR_CPUS];
  66. static int cpu_management;
  67. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  68. static void smp_ext_bitcall(int, int);
  69. static int raw_cpu_stopped(int cpu)
  70. {
  71. u32 status;
  72. switch (raw_sigp_ps(&status, 0, cpu, sigp_sense)) {
  73. case sigp_status_stored:
  74. /* Check for stopped and check stop state */
  75. if (status & 0x50)
  76. return 1;
  77. break;
  78. default:
  79. break;
  80. }
  81. return 0;
  82. }
  83. static inline int cpu_stopped(int cpu)
  84. {
  85. return raw_cpu_stopped(cpu_logical_map(cpu));
  86. }
  87. /*
  88. * Ensure that PSW restart is done on an online CPU
  89. */
  90. void smp_restart_with_online_cpu(void)
  91. {
  92. int cpu;
  93. for_each_online_cpu(cpu) {
  94. if (stap() == __cpu_logical_map[cpu]) {
  95. /* We are online: Enable DAT again and return */
  96. __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
  97. return;
  98. }
  99. }
  100. /* We are not online: Do PSW restart on an online CPU */
  101. while (sigp(cpu, sigp_restart) == sigp_busy)
  102. cpu_relax();
  103. /* And stop ourself */
  104. while (raw_sigp(stap(), sigp_stop) == sigp_busy)
  105. cpu_relax();
  106. for (;;);
  107. }
  108. void smp_switch_to_ipl_cpu(void (*func)(void *), void *data)
  109. {
  110. struct _lowcore *lc, *current_lc;
  111. struct stack_frame *sf;
  112. struct pt_regs *regs;
  113. unsigned long sp;
  114. if (smp_processor_id() == 0)
  115. func(data);
  116. __load_psw_mask(PSW_BASE_BITS | PSW_DEFAULT_KEY);
  117. /* Disable lowcore protection */
  118. __ctl_clear_bit(0, 28);
  119. current_lc = lowcore_ptr[smp_processor_id()];
  120. lc = lowcore_ptr[0];
  121. if (!lc)
  122. lc = current_lc;
  123. lc->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
  124. lc->restart_psw.addr = PSW_ADDR_AMODE | (unsigned long) smp_restart_cpu;
  125. if (!cpu_online(0))
  126. smp_switch_to_cpu(func, data, 0, stap(), __cpu_logical_map[0]);
  127. while (sigp(0, sigp_stop_and_store_status) == sigp_busy)
  128. cpu_relax();
  129. sp = lc->panic_stack;
  130. sp -= sizeof(struct pt_regs);
  131. regs = (struct pt_regs *) sp;
  132. memcpy(&regs->gprs, &current_lc->gpregs_save_area, sizeof(regs->gprs));
  133. regs->psw = lc->psw_save_area;
  134. sp -= STACK_FRAME_OVERHEAD;
  135. sf = (struct stack_frame *) sp;
  136. sf->back_chain = regs->gprs[15];
  137. smp_switch_to_cpu(func, data, sp, stap(), __cpu_logical_map[0]);
  138. }
  139. void smp_send_stop(void)
  140. {
  141. int cpu, rc;
  142. /* Disable all interrupts/machine checks */
  143. __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
  144. trace_hardirqs_off();
  145. /* stop all processors */
  146. for_each_online_cpu(cpu) {
  147. if (cpu == smp_processor_id())
  148. continue;
  149. do {
  150. rc = sigp(cpu, sigp_stop);
  151. } while (rc == sigp_busy);
  152. while (!cpu_stopped(cpu))
  153. cpu_relax();
  154. }
  155. }
  156. /*
  157. * This is the main routine where commands issued by other
  158. * cpus are handled.
  159. */
  160. static void do_ext_call_interrupt(unsigned int ext_int_code,
  161. unsigned int param32, unsigned long param64)
  162. {
  163. unsigned long bits;
  164. kstat_cpu(smp_processor_id()).irqs[EXTINT_IPI]++;
  165. /*
  166. * handle bit signal external calls
  167. */
  168. bits = xchg(&S390_lowcore.ext_call_fast, 0);
  169. if (test_bit(ec_schedule, &bits))
  170. scheduler_ipi();
  171. if (test_bit(ec_call_function, &bits))
  172. generic_smp_call_function_interrupt();
  173. if (test_bit(ec_call_function_single, &bits))
  174. generic_smp_call_function_single_interrupt();
  175. }
  176. /*
  177. * Send an external call sigp to another cpu and return without waiting
  178. * for its completion.
  179. */
  180. static void smp_ext_bitcall(int cpu, int sig)
  181. {
  182. /*
  183. * Set signaling bit in lowcore of target cpu and kick it
  184. */
  185. set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
  186. while (sigp(cpu, sigp_emergency_signal) == sigp_busy)
  187. udelay(10);
  188. }
  189. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  190. {
  191. int cpu;
  192. for_each_cpu(cpu, mask)
  193. smp_ext_bitcall(cpu, ec_call_function);
  194. }
  195. void arch_send_call_function_single_ipi(int cpu)
  196. {
  197. smp_ext_bitcall(cpu, ec_call_function_single);
  198. }
  199. #ifndef CONFIG_64BIT
  200. /*
  201. * this function sends a 'purge tlb' signal to another CPU.
  202. */
  203. static void smp_ptlb_callback(void *info)
  204. {
  205. __tlb_flush_local();
  206. }
  207. void smp_ptlb_all(void)
  208. {
  209. on_each_cpu(smp_ptlb_callback, NULL, 1);
  210. }
  211. EXPORT_SYMBOL(smp_ptlb_all);
  212. #endif /* ! CONFIG_64BIT */
  213. /*
  214. * this function sends a 'reschedule' IPI to another CPU.
  215. * it goes straight through and wastes no time serializing
  216. * anything. Worst case is that we lose a reschedule ...
  217. */
  218. void smp_send_reschedule(int cpu)
  219. {
  220. smp_ext_bitcall(cpu, ec_schedule);
  221. }
  222. /*
  223. * parameter area for the set/clear control bit callbacks
  224. */
  225. struct ec_creg_mask_parms {
  226. unsigned long orvals[16];
  227. unsigned long andvals[16];
  228. };
  229. /*
  230. * callback for setting/clearing control bits
  231. */
  232. static void smp_ctl_bit_callback(void *info)
  233. {
  234. struct ec_creg_mask_parms *pp = info;
  235. unsigned long cregs[16];
  236. int i;
  237. __ctl_store(cregs, 0, 15);
  238. for (i = 0; i <= 15; i++)
  239. cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
  240. __ctl_load(cregs, 0, 15);
  241. }
  242. /*
  243. * Set a bit in a control register of all cpus
  244. */
  245. void smp_ctl_set_bit(int cr, int bit)
  246. {
  247. struct ec_creg_mask_parms parms;
  248. memset(&parms.orvals, 0, sizeof(parms.orvals));
  249. memset(&parms.andvals, 0xff, sizeof(parms.andvals));
  250. parms.orvals[cr] = 1UL << bit;
  251. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  252. }
  253. EXPORT_SYMBOL(smp_ctl_set_bit);
  254. /*
  255. * Clear a bit in a control register of all cpus
  256. */
  257. void smp_ctl_clear_bit(int cr, int bit)
  258. {
  259. struct ec_creg_mask_parms parms;
  260. memset(&parms.orvals, 0, sizeof(parms.orvals));
  261. memset(&parms.andvals, 0xff, sizeof(parms.andvals));
  262. parms.andvals[cr] = ~(1UL << bit);
  263. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  264. }
  265. EXPORT_SYMBOL(smp_ctl_clear_bit);
  266. #ifdef CONFIG_ZFCPDUMP
  267. static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
  268. {
  269. if (ipl_info.type != IPL_TYPE_FCP_DUMP)
  270. return;
  271. if (cpu >= NR_CPUS) {
  272. pr_warning("CPU %i exceeds the maximum %i and is excluded from "
  273. "the dump\n", cpu, NR_CPUS - 1);
  274. return;
  275. }
  276. zfcpdump_save_areas[cpu] = kmalloc(sizeof(struct save_area), GFP_KERNEL);
  277. while (raw_sigp(phy_cpu, sigp_stop_and_store_status) == sigp_busy)
  278. cpu_relax();
  279. memcpy_real(zfcpdump_save_areas[cpu],
  280. (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
  281. sizeof(struct save_area));
  282. }
  283. struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
  284. EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
  285. #else
  286. static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
  287. #endif /* CONFIG_ZFCPDUMP */
  288. static int cpu_known(int cpu_id)
  289. {
  290. int cpu;
  291. for_each_present_cpu(cpu) {
  292. if (__cpu_logical_map[cpu] == cpu_id)
  293. return 1;
  294. }
  295. return 0;
  296. }
  297. static int smp_rescan_cpus_sigp(cpumask_t avail)
  298. {
  299. int cpu_id, logical_cpu;
  300. logical_cpu = cpumask_first(&avail);
  301. if (logical_cpu >= nr_cpu_ids)
  302. return 0;
  303. for (cpu_id = 0; cpu_id <= MAX_CPU_ADDRESS; cpu_id++) {
  304. if (cpu_known(cpu_id))
  305. continue;
  306. __cpu_logical_map[logical_cpu] = cpu_id;
  307. smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
  308. if (!cpu_stopped(logical_cpu))
  309. continue;
  310. set_cpu_present(logical_cpu, true);
  311. smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
  312. logical_cpu = cpumask_next(logical_cpu, &avail);
  313. if (logical_cpu >= nr_cpu_ids)
  314. break;
  315. }
  316. return 0;
  317. }
  318. static int smp_rescan_cpus_sclp(cpumask_t avail)
  319. {
  320. struct sclp_cpu_info *info;
  321. int cpu_id, logical_cpu, cpu;
  322. int rc;
  323. logical_cpu = cpumask_first(&avail);
  324. if (logical_cpu >= nr_cpu_ids)
  325. return 0;
  326. info = kmalloc(sizeof(*info), GFP_KERNEL);
  327. if (!info)
  328. return -ENOMEM;
  329. rc = sclp_get_cpu_info(info);
  330. if (rc)
  331. goto out;
  332. for (cpu = 0; cpu < info->combined; cpu++) {
  333. if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
  334. continue;
  335. cpu_id = info->cpu[cpu].address;
  336. if (cpu_known(cpu_id))
  337. continue;
  338. __cpu_logical_map[logical_cpu] = cpu_id;
  339. smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
  340. set_cpu_present(logical_cpu, true);
  341. if (cpu >= info->configured)
  342. smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
  343. else
  344. smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
  345. logical_cpu = cpumask_next(logical_cpu, &avail);
  346. if (logical_cpu >= nr_cpu_ids)
  347. break;
  348. }
  349. out:
  350. kfree(info);
  351. return rc;
  352. }
  353. static int __smp_rescan_cpus(void)
  354. {
  355. cpumask_t avail;
  356. cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
  357. if (smp_use_sigp_detection)
  358. return smp_rescan_cpus_sigp(avail);
  359. else
  360. return smp_rescan_cpus_sclp(avail);
  361. }
  362. static void __init smp_detect_cpus(void)
  363. {
  364. unsigned int cpu, c_cpus, s_cpus;
  365. struct sclp_cpu_info *info;
  366. u16 boot_cpu_addr, cpu_addr;
  367. c_cpus = 1;
  368. s_cpus = 0;
  369. boot_cpu_addr = __cpu_logical_map[0];
  370. info = kmalloc(sizeof(*info), GFP_KERNEL);
  371. if (!info)
  372. panic("smp_detect_cpus failed to allocate memory\n");
  373. /* Use sigp detection algorithm if sclp doesn't work. */
  374. if (sclp_get_cpu_info(info)) {
  375. smp_use_sigp_detection = 1;
  376. for (cpu = 0; cpu <= MAX_CPU_ADDRESS; cpu++) {
  377. if (cpu == boot_cpu_addr)
  378. continue;
  379. if (!raw_cpu_stopped(cpu))
  380. continue;
  381. smp_get_save_area(c_cpus, cpu);
  382. c_cpus++;
  383. }
  384. goto out;
  385. }
  386. if (info->has_cpu_type) {
  387. for (cpu = 0; cpu < info->combined; cpu++) {
  388. if (info->cpu[cpu].address == boot_cpu_addr) {
  389. smp_cpu_type = info->cpu[cpu].type;
  390. break;
  391. }
  392. }
  393. }
  394. for (cpu = 0; cpu < info->combined; cpu++) {
  395. if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
  396. continue;
  397. cpu_addr = info->cpu[cpu].address;
  398. if (cpu_addr == boot_cpu_addr)
  399. continue;
  400. if (!raw_cpu_stopped(cpu_addr)) {
  401. s_cpus++;
  402. continue;
  403. }
  404. smp_get_save_area(c_cpus, cpu_addr);
  405. c_cpus++;
  406. }
  407. out:
  408. kfree(info);
  409. pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
  410. get_online_cpus();
  411. __smp_rescan_cpus();
  412. put_online_cpus();
  413. }
  414. /*
  415. * Activate a secondary processor.
  416. */
  417. int __cpuinit start_secondary(void *cpuvoid)
  418. {
  419. cpu_init();
  420. preempt_disable();
  421. init_cpu_timer();
  422. init_cpu_vtimer();
  423. pfault_init();
  424. notify_cpu_starting(smp_processor_id());
  425. ipi_call_lock();
  426. set_cpu_online(smp_processor_id(), true);
  427. ipi_call_unlock();
  428. __ctl_clear_bit(0, 28); /* Disable lowcore protection */
  429. S390_lowcore.restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
  430. S390_lowcore.restart_psw.addr =
  431. PSW_ADDR_AMODE | (unsigned long) psw_restart_int_handler;
  432. __ctl_set_bit(0, 28); /* Enable lowcore protection */
  433. /*
  434. * Wait until the cpu which brought this one up marked it
  435. * active before enabling interrupts.
  436. */
  437. while (!cpumask_test_cpu(smp_processor_id(), cpu_active_mask))
  438. cpu_relax();
  439. local_irq_enable();
  440. /* cpu_idle will call schedule for us */
  441. cpu_idle();
  442. return 0;
  443. }
  444. struct create_idle {
  445. struct work_struct work;
  446. struct task_struct *idle;
  447. struct completion done;
  448. int cpu;
  449. };
  450. static void __cpuinit smp_fork_idle(struct work_struct *work)
  451. {
  452. struct create_idle *c_idle;
  453. c_idle = container_of(work, struct create_idle, work);
  454. c_idle->idle = fork_idle(c_idle->cpu);
  455. complete(&c_idle->done);
  456. }
  457. static int __cpuinit smp_alloc_lowcore(int cpu)
  458. {
  459. unsigned long async_stack, panic_stack;
  460. struct _lowcore *lowcore;
  461. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  462. if (!lowcore)
  463. return -ENOMEM;
  464. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  465. panic_stack = __get_free_page(GFP_KERNEL);
  466. if (!panic_stack || !async_stack)
  467. goto out;
  468. memcpy(lowcore, &S390_lowcore, 512);
  469. memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
  470. lowcore->async_stack = async_stack + ASYNC_SIZE;
  471. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  472. lowcore->restart_psw.mask = PSW_BASE_BITS | PSW_DEFAULT_KEY;
  473. lowcore->restart_psw.addr =
  474. PSW_ADDR_AMODE | (unsigned long) restart_int_handler;
  475. if (user_mode != HOME_SPACE_MODE)
  476. lowcore->restart_psw.mask |= PSW_ASC_HOME;
  477. #ifndef CONFIG_64BIT
  478. if (MACHINE_HAS_IEEE) {
  479. unsigned long save_area;
  480. save_area = get_zeroed_page(GFP_KERNEL);
  481. if (!save_area)
  482. goto out;
  483. lowcore->extended_save_area_addr = (u32) save_area;
  484. }
  485. #else
  486. if (vdso_alloc_per_cpu(cpu, lowcore))
  487. goto out;
  488. #endif
  489. lowcore_ptr[cpu] = lowcore;
  490. return 0;
  491. out:
  492. free_page(panic_stack);
  493. free_pages(async_stack, ASYNC_ORDER);
  494. free_pages((unsigned long) lowcore, LC_ORDER);
  495. return -ENOMEM;
  496. }
  497. static void smp_free_lowcore(int cpu)
  498. {
  499. struct _lowcore *lowcore;
  500. lowcore = lowcore_ptr[cpu];
  501. #ifndef CONFIG_64BIT
  502. if (MACHINE_HAS_IEEE)
  503. free_page((unsigned long) lowcore->extended_save_area_addr);
  504. #else
  505. vdso_free_per_cpu(cpu, lowcore);
  506. #endif
  507. free_page(lowcore->panic_stack - PAGE_SIZE);
  508. free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
  509. free_pages((unsigned long) lowcore, LC_ORDER);
  510. lowcore_ptr[cpu] = NULL;
  511. }
  512. /* Upping and downing of CPUs */
  513. int __cpuinit __cpu_up(unsigned int cpu)
  514. {
  515. struct _lowcore *cpu_lowcore;
  516. struct create_idle c_idle;
  517. struct task_struct *idle;
  518. struct stack_frame *sf;
  519. u32 lowcore;
  520. int ccode;
  521. if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
  522. return -EIO;
  523. idle = current_set[cpu];
  524. if (!idle) {
  525. c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
  526. INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
  527. c_idle.cpu = cpu;
  528. schedule_work(&c_idle.work);
  529. wait_for_completion(&c_idle.done);
  530. if (IS_ERR(c_idle.idle))
  531. return PTR_ERR(c_idle.idle);
  532. idle = c_idle.idle;
  533. current_set[cpu] = c_idle.idle;
  534. }
  535. init_idle(idle, cpu);
  536. if (smp_alloc_lowcore(cpu))
  537. return -ENOMEM;
  538. do {
  539. ccode = sigp(cpu, sigp_initial_cpu_reset);
  540. if (ccode == sigp_busy)
  541. udelay(10);
  542. if (ccode == sigp_not_operational)
  543. goto err_out;
  544. } while (ccode == sigp_busy);
  545. lowcore = (u32)(unsigned long)lowcore_ptr[cpu];
  546. while (sigp_p(lowcore, cpu, sigp_set_prefix) == sigp_busy)
  547. udelay(10);
  548. cpu_lowcore = lowcore_ptr[cpu];
  549. cpu_lowcore->kernel_stack = (unsigned long)
  550. task_stack_page(idle) + THREAD_SIZE;
  551. cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
  552. sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
  553. - sizeof(struct pt_regs)
  554. - sizeof(struct stack_frame));
  555. memset(sf, 0, sizeof(struct stack_frame));
  556. sf->gprs[9] = (unsigned long) sf;
  557. cpu_lowcore->save_area[15] = (unsigned long) sf;
  558. __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
  559. atomic_inc(&init_mm.context.attach_count);
  560. asm volatile(
  561. " stam 0,15,0(%0)"
  562. : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
  563. cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
  564. cpu_lowcore->current_task = (unsigned long) idle;
  565. cpu_lowcore->cpu_nr = cpu;
  566. cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
  567. cpu_lowcore->machine_flags = S390_lowcore.machine_flags;
  568. cpu_lowcore->ftrace_func = S390_lowcore.ftrace_func;
  569. memcpy(cpu_lowcore->stfle_fac_list, S390_lowcore.stfle_fac_list,
  570. MAX_FACILITY_BIT/8);
  571. eieio();
  572. while (sigp(cpu, sigp_restart) == sigp_busy)
  573. udelay(10);
  574. while (!cpu_online(cpu))
  575. cpu_relax();
  576. return 0;
  577. err_out:
  578. smp_free_lowcore(cpu);
  579. return -EIO;
  580. }
  581. static int __init setup_possible_cpus(char *s)
  582. {
  583. int pcpus, cpu;
  584. pcpus = simple_strtoul(s, NULL, 0);
  585. init_cpu_possible(cpumask_of(0));
  586. for (cpu = 1; cpu < pcpus && cpu < nr_cpu_ids; cpu++)
  587. set_cpu_possible(cpu, true);
  588. return 0;
  589. }
  590. early_param("possible_cpus", setup_possible_cpus);
  591. #ifdef CONFIG_HOTPLUG_CPU
  592. int __cpu_disable(void)
  593. {
  594. struct ec_creg_mask_parms cr_parms;
  595. int cpu = smp_processor_id();
  596. set_cpu_online(cpu, false);
  597. /* Disable pfault pseudo page faults on this cpu. */
  598. pfault_fini();
  599. memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
  600. memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
  601. /* disable all external interrupts */
  602. cr_parms.orvals[0] = 0;
  603. cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 11 |
  604. 1 << 10 | 1 << 9 | 1 << 6 | 1 << 5 |
  605. 1 << 4);
  606. /* disable all I/O interrupts */
  607. cr_parms.orvals[6] = 0;
  608. cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
  609. 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
  610. /* disable most machine checks */
  611. cr_parms.orvals[14] = 0;
  612. cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
  613. 1 << 25 | 1 << 24);
  614. smp_ctl_bit_callback(&cr_parms);
  615. return 0;
  616. }
  617. void __cpu_die(unsigned int cpu)
  618. {
  619. /* Wait until target cpu is down */
  620. while (!cpu_stopped(cpu))
  621. cpu_relax();
  622. while (sigp_p(0, cpu, sigp_set_prefix) == sigp_busy)
  623. udelay(10);
  624. smp_free_lowcore(cpu);
  625. atomic_dec(&init_mm.context.attach_count);
  626. }
  627. void __noreturn cpu_die(void)
  628. {
  629. idle_task_exit();
  630. while (sigp(smp_processor_id(), sigp_stop) == sigp_busy)
  631. cpu_relax();
  632. for (;;);
  633. }
  634. #endif /* CONFIG_HOTPLUG_CPU */
  635. void __init smp_prepare_cpus(unsigned int max_cpus)
  636. {
  637. #ifndef CONFIG_64BIT
  638. unsigned long save_area = 0;
  639. #endif
  640. unsigned long async_stack, panic_stack;
  641. struct _lowcore *lowcore;
  642. smp_detect_cpus();
  643. /* request the 0x1201 emergency signal external interrupt */
  644. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  645. panic("Couldn't request external interrupt 0x1201");
  646. /* Reallocate current lowcore, but keep its contents. */
  647. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  648. panic_stack = __get_free_page(GFP_KERNEL);
  649. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  650. BUG_ON(!lowcore || !panic_stack || !async_stack);
  651. #ifndef CONFIG_64BIT
  652. if (MACHINE_HAS_IEEE)
  653. save_area = get_zeroed_page(GFP_KERNEL);
  654. #endif
  655. local_irq_disable();
  656. local_mcck_disable();
  657. lowcore_ptr[smp_processor_id()] = lowcore;
  658. *lowcore = S390_lowcore;
  659. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  660. lowcore->async_stack = async_stack + ASYNC_SIZE;
  661. #ifndef CONFIG_64BIT
  662. if (MACHINE_HAS_IEEE)
  663. lowcore->extended_save_area_addr = (u32) save_area;
  664. #endif
  665. set_prefix((u32)(unsigned long) lowcore);
  666. local_mcck_enable();
  667. local_irq_enable();
  668. #ifdef CONFIG_64BIT
  669. if (vdso_alloc_per_cpu(smp_processor_id(), &S390_lowcore))
  670. BUG();
  671. #endif
  672. }
  673. void __init smp_prepare_boot_cpu(void)
  674. {
  675. BUG_ON(smp_processor_id() != 0);
  676. current_thread_info()->cpu = 0;
  677. set_cpu_present(0, true);
  678. set_cpu_online(0, true);
  679. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  680. current_set[0] = current;
  681. smp_cpu_state[0] = CPU_STATE_CONFIGURED;
  682. smp_cpu_polarization[0] = POLARIZATION_UNKNWN;
  683. }
  684. void __init smp_cpus_done(unsigned int max_cpus)
  685. {
  686. }
  687. void __init smp_setup_processor_id(void)
  688. {
  689. S390_lowcore.cpu_nr = 0;
  690. __cpu_logical_map[0] = stap();
  691. }
  692. /*
  693. * the frequency of the profiling timer can be changed
  694. * by writing a multiplier value into /proc/profile.
  695. *
  696. * usually you want to run this on all CPUs ;)
  697. */
  698. int setup_profiling_timer(unsigned int multiplier)
  699. {
  700. return 0;
  701. }
  702. #ifdef CONFIG_HOTPLUG_CPU
  703. static ssize_t cpu_configure_show(struct sys_device *dev,
  704. struct sysdev_attribute *attr, char *buf)
  705. {
  706. ssize_t count;
  707. mutex_lock(&smp_cpu_state_mutex);
  708. count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
  709. mutex_unlock(&smp_cpu_state_mutex);
  710. return count;
  711. }
  712. static ssize_t cpu_configure_store(struct sys_device *dev,
  713. struct sysdev_attribute *attr,
  714. const char *buf, size_t count)
  715. {
  716. int cpu = dev->id;
  717. int val, rc;
  718. char delim;
  719. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  720. return -EINVAL;
  721. if (val != 0 && val != 1)
  722. return -EINVAL;
  723. get_online_cpus();
  724. mutex_lock(&smp_cpu_state_mutex);
  725. rc = -EBUSY;
  726. /* disallow configuration changes of online cpus and cpu 0 */
  727. if (cpu_online(cpu) || cpu == 0)
  728. goto out;
  729. rc = 0;
  730. switch (val) {
  731. case 0:
  732. if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
  733. rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
  734. if (!rc) {
  735. smp_cpu_state[cpu] = CPU_STATE_STANDBY;
  736. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  737. }
  738. }
  739. break;
  740. case 1:
  741. if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
  742. rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
  743. if (!rc) {
  744. smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
  745. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  746. }
  747. }
  748. break;
  749. default:
  750. break;
  751. }
  752. out:
  753. mutex_unlock(&smp_cpu_state_mutex);
  754. put_online_cpus();
  755. return rc ? rc : count;
  756. }
  757. static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  758. #endif /* CONFIG_HOTPLUG_CPU */
  759. static ssize_t cpu_polarization_show(struct sys_device *dev,
  760. struct sysdev_attribute *attr, char *buf)
  761. {
  762. int cpu = dev->id;
  763. ssize_t count;
  764. mutex_lock(&smp_cpu_state_mutex);
  765. switch (smp_cpu_polarization[cpu]) {
  766. case POLARIZATION_HRZ:
  767. count = sprintf(buf, "horizontal\n");
  768. break;
  769. case POLARIZATION_VL:
  770. count = sprintf(buf, "vertical:low\n");
  771. break;
  772. case POLARIZATION_VM:
  773. count = sprintf(buf, "vertical:medium\n");
  774. break;
  775. case POLARIZATION_VH:
  776. count = sprintf(buf, "vertical:high\n");
  777. break;
  778. default:
  779. count = sprintf(buf, "unknown\n");
  780. break;
  781. }
  782. mutex_unlock(&smp_cpu_state_mutex);
  783. return count;
  784. }
  785. static SYSDEV_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  786. static ssize_t show_cpu_address(struct sys_device *dev,
  787. struct sysdev_attribute *attr, char *buf)
  788. {
  789. return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
  790. }
  791. static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
  792. static struct attribute *cpu_common_attrs[] = {
  793. #ifdef CONFIG_HOTPLUG_CPU
  794. &attr_configure.attr,
  795. #endif
  796. &attr_address.attr,
  797. &attr_polarization.attr,
  798. NULL,
  799. };
  800. static struct attribute_group cpu_common_attr_group = {
  801. .attrs = cpu_common_attrs,
  802. };
  803. static ssize_t show_capability(struct sys_device *dev,
  804. struct sysdev_attribute *attr, char *buf)
  805. {
  806. unsigned int capability;
  807. int rc;
  808. rc = get_cpu_capability(&capability);
  809. if (rc)
  810. return rc;
  811. return sprintf(buf, "%u\n", capability);
  812. }
  813. static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
  814. static ssize_t show_idle_count(struct sys_device *dev,
  815. struct sysdev_attribute *attr, char *buf)
  816. {
  817. struct s390_idle_data *idle;
  818. unsigned long long idle_count;
  819. unsigned int sequence;
  820. idle = &per_cpu(s390_idle, dev->id);
  821. repeat:
  822. sequence = idle->sequence;
  823. smp_rmb();
  824. if (sequence & 1)
  825. goto repeat;
  826. idle_count = idle->idle_count;
  827. if (idle->idle_enter)
  828. idle_count++;
  829. smp_rmb();
  830. if (idle->sequence != sequence)
  831. goto repeat;
  832. return sprintf(buf, "%llu\n", idle_count);
  833. }
  834. static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
  835. static ssize_t show_idle_time(struct sys_device *dev,
  836. struct sysdev_attribute *attr, char *buf)
  837. {
  838. struct s390_idle_data *idle;
  839. unsigned long long now, idle_time, idle_enter;
  840. unsigned int sequence;
  841. idle = &per_cpu(s390_idle, dev->id);
  842. now = get_clock();
  843. repeat:
  844. sequence = idle->sequence;
  845. smp_rmb();
  846. if (sequence & 1)
  847. goto repeat;
  848. idle_time = idle->idle_time;
  849. idle_enter = idle->idle_enter;
  850. if (idle_enter != 0ULL && idle_enter < now)
  851. idle_time += now - idle_enter;
  852. smp_rmb();
  853. if (idle->sequence != sequence)
  854. goto repeat;
  855. return sprintf(buf, "%llu\n", idle_time >> 12);
  856. }
  857. static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  858. static struct attribute *cpu_online_attrs[] = {
  859. &attr_capability.attr,
  860. &attr_idle_count.attr,
  861. &attr_idle_time_us.attr,
  862. NULL,
  863. };
  864. static struct attribute_group cpu_online_attr_group = {
  865. .attrs = cpu_online_attrs,
  866. };
  867. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  868. unsigned long action, void *hcpu)
  869. {
  870. unsigned int cpu = (unsigned int)(long)hcpu;
  871. struct cpu *c = &per_cpu(cpu_devices, cpu);
  872. struct sys_device *s = &c->sysdev;
  873. struct s390_idle_data *idle;
  874. int err = 0;
  875. switch (action) {
  876. case CPU_ONLINE:
  877. case CPU_ONLINE_FROZEN:
  878. idle = &per_cpu(s390_idle, cpu);
  879. memset(idle, 0, sizeof(struct s390_idle_data));
  880. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  881. break;
  882. case CPU_DEAD:
  883. case CPU_DEAD_FROZEN:
  884. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  885. break;
  886. }
  887. return notifier_from_errno(err);
  888. }
  889. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  890. .notifier_call = smp_cpu_notify,
  891. };
  892. static int __devinit smp_add_present_cpu(int cpu)
  893. {
  894. struct cpu *c = &per_cpu(cpu_devices, cpu);
  895. struct sys_device *s = &c->sysdev;
  896. int rc;
  897. c->hotpluggable = 1;
  898. rc = register_cpu(c, cpu);
  899. if (rc)
  900. goto out;
  901. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  902. if (rc)
  903. goto out_cpu;
  904. if (!cpu_online(cpu))
  905. goto out;
  906. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  907. if (!rc)
  908. return 0;
  909. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  910. out_cpu:
  911. #ifdef CONFIG_HOTPLUG_CPU
  912. unregister_cpu(c);
  913. #endif
  914. out:
  915. return rc;
  916. }
  917. #ifdef CONFIG_HOTPLUG_CPU
  918. int __ref smp_rescan_cpus(void)
  919. {
  920. cpumask_t newcpus;
  921. int cpu;
  922. int rc;
  923. get_online_cpus();
  924. mutex_lock(&smp_cpu_state_mutex);
  925. cpumask_copy(&newcpus, cpu_present_mask);
  926. rc = __smp_rescan_cpus();
  927. if (rc)
  928. goto out;
  929. cpumask_andnot(&newcpus, cpu_present_mask, &newcpus);
  930. for_each_cpu(cpu, &newcpus) {
  931. rc = smp_add_present_cpu(cpu);
  932. if (rc)
  933. set_cpu_present(cpu, false);
  934. }
  935. rc = 0;
  936. out:
  937. mutex_unlock(&smp_cpu_state_mutex);
  938. put_online_cpus();
  939. if (!cpumask_empty(&newcpus))
  940. topology_schedule_update();
  941. return rc;
  942. }
  943. static ssize_t __ref rescan_store(struct sysdev_class *class,
  944. struct sysdev_class_attribute *attr,
  945. const char *buf,
  946. size_t count)
  947. {
  948. int rc;
  949. rc = smp_rescan_cpus();
  950. return rc ? rc : count;
  951. }
  952. static SYSDEV_CLASS_ATTR(rescan, 0200, NULL, rescan_store);
  953. #endif /* CONFIG_HOTPLUG_CPU */
  954. static ssize_t dispatching_show(struct sysdev_class *class,
  955. struct sysdev_class_attribute *attr,
  956. char *buf)
  957. {
  958. ssize_t count;
  959. mutex_lock(&smp_cpu_state_mutex);
  960. count = sprintf(buf, "%d\n", cpu_management);
  961. mutex_unlock(&smp_cpu_state_mutex);
  962. return count;
  963. }
  964. static ssize_t dispatching_store(struct sysdev_class *dev,
  965. struct sysdev_class_attribute *attr,
  966. const char *buf,
  967. size_t count)
  968. {
  969. int val, rc;
  970. char delim;
  971. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  972. return -EINVAL;
  973. if (val != 0 && val != 1)
  974. return -EINVAL;
  975. rc = 0;
  976. get_online_cpus();
  977. mutex_lock(&smp_cpu_state_mutex);
  978. if (cpu_management == val)
  979. goto out;
  980. rc = topology_set_cpu_management(val);
  981. if (!rc)
  982. cpu_management = val;
  983. out:
  984. mutex_unlock(&smp_cpu_state_mutex);
  985. put_online_cpus();
  986. return rc ? rc : count;
  987. }
  988. static SYSDEV_CLASS_ATTR(dispatching, 0644, dispatching_show,
  989. dispatching_store);
  990. static int __init topology_init(void)
  991. {
  992. int cpu;
  993. int rc;
  994. register_cpu_notifier(&smp_cpu_nb);
  995. #ifdef CONFIG_HOTPLUG_CPU
  996. rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_rescan);
  997. if (rc)
  998. return rc;
  999. #endif
  1000. rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_dispatching);
  1001. if (rc)
  1002. return rc;
  1003. for_each_present_cpu(cpu) {
  1004. rc = smp_add_present_cpu(cpu);
  1005. if (rc)
  1006. return rc;
  1007. }
  1008. return 0;
  1009. }
  1010. subsys_initcall(topology_init);