smp.c 27 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. * 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. /* cpu_idle will call schedule for us */
  418. cpu_idle();
  419. return 0;
  420. }
  421. struct create_idle {
  422. struct work_struct work;
  423. struct task_struct *idle;
  424. struct completion done;
  425. int cpu;
  426. };
  427. static void __cpuinit smp_fork_idle(struct work_struct *work)
  428. {
  429. struct create_idle *c_idle;
  430. c_idle = container_of(work, struct create_idle, work);
  431. c_idle->idle = fork_idle(c_idle->cpu);
  432. complete(&c_idle->done);
  433. }
  434. static int __cpuinit smp_alloc_lowcore(int cpu)
  435. {
  436. unsigned long async_stack, panic_stack;
  437. struct _lowcore *lowcore;
  438. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  439. if (!lowcore)
  440. return -ENOMEM;
  441. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  442. panic_stack = __get_free_page(GFP_KERNEL);
  443. if (!panic_stack || !async_stack)
  444. goto out;
  445. memcpy(lowcore, &S390_lowcore, 512);
  446. memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
  447. lowcore->async_stack = async_stack + ASYNC_SIZE;
  448. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  449. #ifndef CONFIG_64BIT
  450. if (MACHINE_HAS_IEEE) {
  451. unsigned long save_area;
  452. save_area = get_zeroed_page(GFP_KERNEL);
  453. if (!save_area)
  454. goto out;
  455. lowcore->extended_save_area_addr = (u32) save_area;
  456. }
  457. #else
  458. if (vdso_alloc_per_cpu(cpu, lowcore))
  459. goto out;
  460. #endif
  461. lowcore_ptr[cpu] = lowcore;
  462. return 0;
  463. out:
  464. free_page(panic_stack);
  465. free_pages(async_stack, ASYNC_ORDER);
  466. free_pages((unsigned long) lowcore, LC_ORDER);
  467. return -ENOMEM;
  468. }
  469. static void smp_free_lowcore(int cpu)
  470. {
  471. struct _lowcore *lowcore;
  472. lowcore = lowcore_ptr[cpu];
  473. #ifndef CONFIG_64BIT
  474. if (MACHINE_HAS_IEEE)
  475. free_page((unsigned long) lowcore->extended_save_area_addr);
  476. #else
  477. vdso_free_per_cpu(cpu, lowcore);
  478. #endif
  479. free_page(lowcore->panic_stack - PAGE_SIZE);
  480. free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
  481. free_pages((unsigned long) lowcore, LC_ORDER);
  482. lowcore_ptr[cpu] = NULL;
  483. }
  484. /* Upping and downing of CPUs */
  485. int __cpuinit __cpu_up(unsigned int cpu)
  486. {
  487. struct _lowcore *cpu_lowcore;
  488. struct create_idle c_idle;
  489. struct task_struct *idle;
  490. struct stack_frame *sf;
  491. u32 lowcore;
  492. int ccode;
  493. if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
  494. return -EIO;
  495. idle = current_set[cpu];
  496. if (!idle) {
  497. c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
  498. INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
  499. c_idle.cpu = cpu;
  500. schedule_work(&c_idle.work);
  501. wait_for_completion(&c_idle.done);
  502. if (IS_ERR(c_idle.idle))
  503. return PTR_ERR(c_idle.idle);
  504. idle = c_idle.idle;
  505. current_set[cpu] = c_idle.idle;
  506. }
  507. init_idle(idle, cpu);
  508. if (smp_alloc_lowcore(cpu))
  509. return -ENOMEM;
  510. do {
  511. ccode = sigp(cpu, sigp_initial_cpu_reset);
  512. if (ccode == sigp_busy)
  513. udelay(10);
  514. if (ccode == sigp_not_operational)
  515. goto err_out;
  516. } while (ccode == sigp_busy);
  517. lowcore = (u32)(unsigned long)lowcore_ptr[cpu];
  518. while (sigp_p(lowcore, cpu, sigp_set_prefix) == sigp_busy)
  519. udelay(10);
  520. cpu_lowcore = lowcore_ptr[cpu];
  521. cpu_lowcore->kernel_stack = (unsigned long)
  522. task_stack_page(idle) + THREAD_SIZE;
  523. cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
  524. sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
  525. - sizeof(struct pt_regs)
  526. - sizeof(struct stack_frame));
  527. memset(sf, 0, sizeof(struct stack_frame));
  528. sf->gprs[9] = (unsigned long) sf;
  529. cpu_lowcore->save_area[15] = (unsigned long) sf;
  530. __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
  531. atomic_inc(&init_mm.context.attach_count);
  532. asm volatile(
  533. " stam 0,15,0(%0)"
  534. : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
  535. cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
  536. cpu_lowcore->current_task = (unsigned long) idle;
  537. cpu_lowcore->cpu_nr = cpu;
  538. cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
  539. cpu_lowcore->machine_flags = S390_lowcore.machine_flags;
  540. cpu_lowcore->ftrace_func = S390_lowcore.ftrace_func;
  541. memcpy(cpu_lowcore->stfle_fac_list, S390_lowcore.stfle_fac_list,
  542. MAX_FACILITY_BIT/8);
  543. eieio();
  544. while (sigp(cpu, sigp_restart) == sigp_busy)
  545. udelay(10);
  546. while (!cpu_online(cpu))
  547. cpu_relax();
  548. return 0;
  549. err_out:
  550. smp_free_lowcore(cpu);
  551. return -EIO;
  552. }
  553. static int __init setup_possible_cpus(char *s)
  554. {
  555. int pcpus, cpu;
  556. pcpus = simple_strtoul(s, NULL, 0);
  557. init_cpu_possible(cpumask_of(0));
  558. for (cpu = 1; cpu < pcpus && cpu < nr_cpu_ids; cpu++)
  559. set_cpu_possible(cpu, true);
  560. return 0;
  561. }
  562. early_param("possible_cpus", setup_possible_cpus);
  563. #ifdef CONFIG_HOTPLUG_CPU
  564. int __cpu_disable(void)
  565. {
  566. struct ec_creg_mask_parms cr_parms;
  567. int cpu = smp_processor_id();
  568. cpu_clear(cpu, cpu_online_map);
  569. /* Disable pfault pseudo page faults on this cpu. */
  570. pfault_fini();
  571. memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
  572. memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
  573. /* disable all external interrupts */
  574. cr_parms.orvals[0] = 0;
  575. cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
  576. 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
  577. /* disable all I/O interrupts */
  578. cr_parms.orvals[6] = 0;
  579. cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
  580. 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
  581. /* disable most machine checks */
  582. cr_parms.orvals[14] = 0;
  583. cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
  584. 1 << 25 | 1 << 24);
  585. smp_ctl_bit_callback(&cr_parms);
  586. return 0;
  587. }
  588. void __cpu_die(unsigned int cpu)
  589. {
  590. /* Wait until target cpu is down */
  591. while (!cpu_stopped(cpu))
  592. cpu_relax();
  593. while (sigp_p(0, cpu, sigp_set_prefix) == sigp_busy)
  594. udelay(10);
  595. smp_free_lowcore(cpu);
  596. atomic_dec(&init_mm.context.attach_count);
  597. }
  598. void cpu_die(void)
  599. {
  600. idle_task_exit();
  601. while (sigp(smp_processor_id(), sigp_stop) == sigp_busy)
  602. cpu_relax();
  603. for (;;);
  604. }
  605. #endif /* CONFIG_HOTPLUG_CPU */
  606. void __init smp_prepare_cpus(unsigned int max_cpus)
  607. {
  608. #ifndef CONFIG_64BIT
  609. unsigned long save_area = 0;
  610. #endif
  611. unsigned long async_stack, panic_stack;
  612. struct _lowcore *lowcore;
  613. smp_detect_cpus();
  614. /* request the 0x1201 emergency signal external interrupt */
  615. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  616. panic("Couldn't request external interrupt 0x1201");
  617. /* Reallocate current lowcore, but keep its contents. */
  618. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  619. panic_stack = __get_free_page(GFP_KERNEL);
  620. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  621. BUG_ON(!lowcore || !panic_stack || !async_stack);
  622. #ifndef CONFIG_64BIT
  623. if (MACHINE_HAS_IEEE)
  624. save_area = get_zeroed_page(GFP_KERNEL);
  625. #endif
  626. local_irq_disable();
  627. local_mcck_disable();
  628. lowcore_ptr[smp_processor_id()] = lowcore;
  629. *lowcore = S390_lowcore;
  630. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  631. lowcore->async_stack = async_stack + ASYNC_SIZE;
  632. #ifndef CONFIG_64BIT
  633. if (MACHINE_HAS_IEEE)
  634. lowcore->extended_save_area_addr = (u32) save_area;
  635. #endif
  636. set_prefix((u32)(unsigned long) lowcore);
  637. local_mcck_enable();
  638. local_irq_enable();
  639. #ifdef CONFIG_64BIT
  640. if (vdso_alloc_per_cpu(smp_processor_id(), &S390_lowcore))
  641. BUG();
  642. #endif
  643. }
  644. void __init smp_prepare_boot_cpu(void)
  645. {
  646. BUG_ON(smp_processor_id() != 0);
  647. current_thread_info()->cpu = 0;
  648. cpu_set(0, cpu_present_map);
  649. cpu_set(0, cpu_online_map);
  650. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  651. current_set[0] = current;
  652. smp_cpu_state[0] = CPU_STATE_CONFIGURED;
  653. smp_cpu_polarization[0] = POLARIZATION_UNKNWN;
  654. }
  655. void __init smp_cpus_done(unsigned int max_cpus)
  656. {
  657. }
  658. void __init smp_setup_processor_id(void)
  659. {
  660. S390_lowcore.cpu_nr = 0;
  661. __cpu_logical_map[0] = stap();
  662. }
  663. /*
  664. * the frequency of the profiling timer can be changed
  665. * by writing a multiplier value into /proc/profile.
  666. *
  667. * usually you want to run this on all CPUs ;)
  668. */
  669. int setup_profiling_timer(unsigned int multiplier)
  670. {
  671. return 0;
  672. }
  673. #ifdef CONFIG_HOTPLUG_CPU
  674. static ssize_t cpu_configure_show(struct sys_device *dev,
  675. struct sysdev_attribute *attr, char *buf)
  676. {
  677. ssize_t count;
  678. mutex_lock(&smp_cpu_state_mutex);
  679. count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
  680. mutex_unlock(&smp_cpu_state_mutex);
  681. return count;
  682. }
  683. static ssize_t cpu_configure_store(struct sys_device *dev,
  684. struct sysdev_attribute *attr,
  685. const char *buf, size_t count)
  686. {
  687. int cpu = dev->id;
  688. int val, rc;
  689. char delim;
  690. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  691. return -EINVAL;
  692. if (val != 0 && val != 1)
  693. return -EINVAL;
  694. get_online_cpus();
  695. mutex_lock(&smp_cpu_state_mutex);
  696. rc = -EBUSY;
  697. /* disallow configuration changes of online cpus and cpu 0 */
  698. if (cpu_online(cpu) || cpu == 0)
  699. goto out;
  700. rc = 0;
  701. switch (val) {
  702. case 0:
  703. if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
  704. rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
  705. if (!rc) {
  706. smp_cpu_state[cpu] = CPU_STATE_STANDBY;
  707. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  708. }
  709. }
  710. break;
  711. case 1:
  712. if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
  713. rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
  714. if (!rc) {
  715. smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
  716. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  717. }
  718. }
  719. break;
  720. default:
  721. break;
  722. }
  723. out:
  724. mutex_unlock(&smp_cpu_state_mutex);
  725. put_online_cpus();
  726. return rc ? rc : count;
  727. }
  728. static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  729. #endif /* CONFIG_HOTPLUG_CPU */
  730. static ssize_t cpu_polarization_show(struct sys_device *dev,
  731. struct sysdev_attribute *attr, char *buf)
  732. {
  733. int cpu = dev->id;
  734. ssize_t count;
  735. mutex_lock(&smp_cpu_state_mutex);
  736. switch (smp_cpu_polarization[cpu]) {
  737. case POLARIZATION_HRZ:
  738. count = sprintf(buf, "horizontal\n");
  739. break;
  740. case POLARIZATION_VL:
  741. count = sprintf(buf, "vertical:low\n");
  742. break;
  743. case POLARIZATION_VM:
  744. count = sprintf(buf, "vertical:medium\n");
  745. break;
  746. case POLARIZATION_VH:
  747. count = sprintf(buf, "vertical:high\n");
  748. break;
  749. default:
  750. count = sprintf(buf, "unknown\n");
  751. break;
  752. }
  753. mutex_unlock(&smp_cpu_state_mutex);
  754. return count;
  755. }
  756. static SYSDEV_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  757. static ssize_t show_cpu_address(struct sys_device *dev,
  758. struct sysdev_attribute *attr, char *buf)
  759. {
  760. return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
  761. }
  762. static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
  763. static struct attribute *cpu_common_attrs[] = {
  764. #ifdef CONFIG_HOTPLUG_CPU
  765. &attr_configure.attr,
  766. #endif
  767. &attr_address.attr,
  768. &attr_polarization.attr,
  769. NULL,
  770. };
  771. static struct attribute_group cpu_common_attr_group = {
  772. .attrs = cpu_common_attrs,
  773. };
  774. static ssize_t show_capability(struct sys_device *dev,
  775. struct sysdev_attribute *attr, char *buf)
  776. {
  777. unsigned int capability;
  778. int rc;
  779. rc = get_cpu_capability(&capability);
  780. if (rc)
  781. return rc;
  782. return sprintf(buf, "%u\n", capability);
  783. }
  784. static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
  785. static ssize_t show_idle_count(struct sys_device *dev,
  786. struct sysdev_attribute *attr, char *buf)
  787. {
  788. struct s390_idle_data *idle;
  789. unsigned long long idle_count;
  790. unsigned int sequence;
  791. idle = &per_cpu(s390_idle, dev->id);
  792. repeat:
  793. sequence = idle->sequence;
  794. smp_rmb();
  795. if (sequence & 1)
  796. goto repeat;
  797. idle_count = idle->idle_count;
  798. if (idle->idle_enter)
  799. idle_count++;
  800. smp_rmb();
  801. if (idle->sequence != sequence)
  802. goto repeat;
  803. return sprintf(buf, "%llu\n", idle_count);
  804. }
  805. static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
  806. static ssize_t show_idle_time(struct sys_device *dev,
  807. struct sysdev_attribute *attr, char *buf)
  808. {
  809. struct s390_idle_data *idle;
  810. unsigned long long now, idle_time, idle_enter;
  811. unsigned int sequence;
  812. idle = &per_cpu(s390_idle, dev->id);
  813. now = get_clock();
  814. repeat:
  815. sequence = idle->sequence;
  816. smp_rmb();
  817. if (sequence & 1)
  818. goto repeat;
  819. idle_time = idle->idle_time;
  820. idle_enter = idle->idle_enter;
  821. if (idle_enter != 0ULL && idle_enter < now)
  822. idle_time += now - idle_enter;
  823. smp_rmb();
  824. if (idle->sequence != sequence)
  825. goto repeat;
  826. return sprintf(buf, "%llu\n", idle_time >> 12);
  827. }
  828. static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  829. static struct attribute *cpu_online_attrs[] = {
  830. &attr_capability.attr,
  831. &attr_idle_count.attr,
  832. &attr_idle_time_us.attr,
  833. NULL,
  834. };
  835. static struct attribute_group cpu_online_attr_group = {
  836. .attrs = cpu_online_attrs,
  837. };
  838. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  839. unsigned long action, void *hcpu)
  840. {
  841. unsigned int cpu = (unsigned int)(long)hcpu;
  842. struct cpu *c = &per_cpu(cpu_devices, cpu);
  843. struct sys_device *s = &c->sysdev;
  844. struct s390_idle_data *idle;
  845. int err = 0;
  846. switch (action) {
  847. case CPU_ONLINE:
  848. case CPU_ONLINE_FROZEN:
  849. idle = &per_cpu(s390_idle, cpu);
  850. memset(idle, 0, sizeof(struct s390_idle_data));
  851. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  852. break;
  853. case CPU_DEAD:
  854. case CPU_DEAD_FROZEN:
  855. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  856. break;
  857. }
  858. return notifier_from_errno(err);
  859. }
  860. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  861. .notifier_call = smp_cpu_notify,
  862. };
  863. static int __devinit smp_add_present_cpu(int cpu)
  864. {
  865. struct cpu *c = &per_cpu(cpu_devices, cpu);
  866. struct sys_device *s = &c->sysdev;
  867. int rc;
  868. c->hotpluggable = 1;
  869. rc = register_cpu(c, cpu);
  870. if (rc)
  871. goto out;
  872. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  873. if (rc)
  874. goto out_cpu;
  875. if (!cpu_online(cpu))
  876. goto out;
  877. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  878. if (!rc)
  879. return 0;
  880. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  881. out_cpu:
  882. #ifdef CONFIG_HOTPLUG_CPU
  883. unregister_cpu(c);
  884. #endif
  885. out:
  886. return rc;
  887. }
  888. #ifdef CONFIG_HOTPLUG_CPU
  889. int __ref smp_rescan_cpus(void)
  890. {
  891. cpumask_t newcpus;
  892. int cpu;
  893. int rc;
  894. get_online_cpus();
  895. mutex_lock(&smp_cpu_state_mutex);
  896. newcpus = cpu_present_map;
  897. rc = __smp_rescan_cpus();
  898. if (rc)
  899. goto out;
  900. cpus_andnot(newcpus, cpu_present_map, newcpus);
  901. for_each_cpu_mask(cpu, newcpus) {
  902. rc = smp_add_present_cpu(cpu);
  903. if (rc)
  904. cpu_clear(cpu, cpu_present_map);
  905. }
  906. rc = 0;
  907. out:
  908. mutex_unlock(&smp_cpu_state_mutex);
  909. put_online_cpus();
  910. if (!cpus_empty(newcpus))
  911. topology_schedule_update();
  912. return rc;
  913. }
  914. static ssize_t __ref rescan_store(struct sysdev_class *class,
  915. struct sysdev_class_attribute *attr,
  916. const char *buf,
  917. size_t count)
  918. {
  919. int rc;
  920. rc = smp_rescan_cpus();
  921. return rc ? rc : count;
  922. }
  923. static SYSDEV_CLASS_ATTR(rescan, 0200, NULL, rescan_store);
  924. #endif /* CONFIG_HOTPLUG_CPU */
  925. static ssize_t dispatching_show(struct sysdev_class *class,
  926. struct sysdev_class_attribute *attr,
  927. char *buf)
  928. {
  929. ssize_t count;
  930. mutex_lock(&smp_cpu_state_mutex);
  931. count = sprintf(buf, "%d\n", cpu_management);
  932. mutex_unlock(&smp_cpu_state_mutex);
  933. return count;
  934. }
  935. static ssize_t dispatching_store(struct sysdev_class *dev,
  936. struct sysdev_class_attribute *attr,
  937. const char *buf,
  938. size_t count)
  939. {
  940. int val, rc;
  941. char delim;
  942. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  943. return -EINVAL;
  944. if (val != 0 && val != 1)
  945. return -EINVAL;
  946. rc = 0;
  947. get_online_cpus();
  948. mutex_lock(&smp_cpu_state_mutex);
  949. if (cpu_management == val)
  950. goto out;
  951. rc = topology_set_cpu_management(val);
  952. if (!rc)
  953. cpu_management = val;
  954. out:
  955. mutex_unlock(&smp_cpu_state_mutex);
  956. put_online_cpus();
  957. return rc ? rc : count;
  958. }
  959. static SYSDEV_CLASS_ATTR(dispatching, 0644, dispatching_show,
  960. dispatching_store);
  961. static int __init topology_init(void)
  962. {
  963. int cpu;
  964. int rc;
  965. register_cpu_notifier(&smp_cpu_nb);
  966. #ifdef CONFIG_HOTPLUG_CPU
  967. rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_rescan);
  968. if (rc)
  969. return rc;
  970. #endif
  971. rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_dispatching);
  972. if (rc)
  973. return rc;
  974. for_each_present_cpu(cpu) {
  975. rc = smp_add_present_cpu(cpu);
  976. if (rc)
  977. return rc;
  978. }
  979. return 0;
  980. }
  981. subsys_initcall(topology_init);