smp.c 27 KB

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