smp.c 26 KB

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