smp.c 28 KB

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