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