smp.c 26 KB

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