smp.c 27 KB

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