smp.c 28 KB

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