smp.c 28 KB

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