smp.c 26 KB

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