smp.c 26 KB

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