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