smp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /*
  2. * SMP related functions
  3. *
  4. * Copyright IBM Corp. 1999,2012
  5. * Author(s): Denis Joseph Barrow,
  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. * The code outside of smp.c uses logical cpu numbers, only smp.c does
  14. * the translation of logical to physical cpu ids. All new code that
  15. * operates on physical cpu numbers needs to go into smp.c.
  16. */
  17. #define KMSG_COMPONENT "cpu"
  18. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  19. #include <linux/workqueue.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/mm.h>
  23. #include <linux/err.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/delay.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/irqflags.h>
  29. #include <linux/cpu.h>
  30. #include <linux/slab.h>
  31. #include <linux/crash_dump.h>
  32. #include <asm/asm-offsets.h>
  33. #include <asm/ipl.h>
  34. #include <asm/setup.h>
  35. #include <asm/irq.h>
  36. #include <asm/tlbflush.h>
  37. #include <asm/timer.h>
  38. #include <asm/lowcore.h>
  39. #include <asm/sclp.h>
  40. #include <asm/vdso.h>
  41. #include <asm/debug.h>
  42. #include <asm/os_info.h>
  43. #include "entry.h"
  44. enum {
  45. sigp_sense = 1,
  46. sigp_external_call = 2,
  47. sigp_emergency_signal = 3,
  48. sigp_start = 4,
  49. sigp_stop = 5,
  50. sigp_restart = 6,
  51. sigp_stop_and_store_status = 9,
  52. sigp_initial_cpu_reset = 11,
  53. sigp_cpu_reset = 12,
  54. sigp_set_prefix = 13,
  55. sigp_store_status_at_address = 14,
  56. sigp_store_extended_status_at_address = 15,
  57. sigp_set_architecture = 18,
  58. sigp_conditional_emergency_signal = 19,
  59. sigp_sense_running = 21,
  60. };
  61. enum {
  62. sigp_order_code_accepted = 0,
  63. sigp_status_stored = 1,
  64. sigp_busy = 2,
  65. sigp_not_operational = 3,
  66. };
  67. enum {
  68. ec_schedule = 0,
  69. ec_call_function,
  70. ec_call_function_single,
  71. ec_stop_cpu,
  72. };
  73. enum {
  74. CPU_STATE_STANDBY,
  75. CPU_STATE_CONFIGURED,
  76. };
  77. struct pcpu {
  78. struct cpu cpu;
  79. struct task_struct *idle; /* idle process for the cpu */
  80. struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
  81. unsigned long async_stack; /* async stack for the cpu */
  82. unsigned long panic_stack; /* panic stack for the cpu */
  83. unsigned long ec_mask; /* bit mask for ec_xxx functions */
  84. int state; /* physical cpu state */
  85. u32 status; /* last status received via sigp */
  86. u16 address; /* physical cpu address */
  87. };
  88. static u8 boot_cpu_type;
  89. static u16 boot_cpu_address;
  90. static struct pcpu pcpu_devices[NR_CPUS];
  91. DEFINE_MUTEX(smp_cpu_state_mutex);
  92. /*
  93. * Signal processor helper functions.
  94. */
  95. static inline int __pcpu_sigp(u16 addr, u8 order, u32 parm, u32 *status)
  96. {
  97. register unsigned int reg1 asm ("1") = parm;
  98. int cc;
  99. asm volatile(
  100. " sigp %1,%2,0(%3)\n"
  101. " ipm %0\n"
  102. " srl %0,28\n"
  103. : "=d" (cc), "+d" (reg1) : "d" (addr), "a" (order) : "cc");
  104. if (status && cc == 1)
  105. *status = reg1;
  106. return cc;
  107. }
  108. static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
  109. {
  110. int cc;
  111. while (1) {
  112. cc = __pcpu_sigp(addr, order, parm, status);
  113. if (cc != sigp_busy)
  114. return cc;
  115. cpu_relax();
  116. }
  117. }
  118. static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
  119. {
  120. int cc, retry;
  121. for (retry = 0; ; retry++) {
  122. cc = __pcpu_sigp(pcpu->address, order, parm, &pcpu->status);
  123. if (cc != sigp_busy)
  124. break;
  125. if (retry >= 3)
  126. udelay(10);
  127. }
  128. return cc;
  129. }
  130. static inline int pcpu_stopped(struct pcpu *pcpu)
  131. {
  132. if (__pcpu_sigp(pcpu->address, sigp_sense,
  133. 0, &pcpu->status) != sigp_status_stored)
  134. return 0;
  135. /* Check for stopped and check stop state */
  136. return !!(pcpu->status & 0x50);
  137. }
  138. static inline int pcpu_running(struct pcpu *pcpu)
  139. {
  140. if (__pcpu_sigp(pcpu->address, sigp_sense_running,
  141. 0, &pcpu->status) != sigp_status_stored)
  142. return 1;
  143. /* Check for running status */
  144. return !(pcpu->status & 0x400);
  145. }
  146. /*
  147. * Find struct pcpu by cpu address.
  148. */
  149. static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
  150. {
  151. int cpu;
  152. for_each_cpu(cpu, mask)
  153. if (pcpu_devices[cpu].address == address)
  154. return pcpu_devices + cpu;
  155. return NULL;
  156. }
  157. static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
  158. {
  159. int order;
  160. set_bit(ec_bit, &pcpu->ec_mask);
  161. order = pcpu_running(pcpu) ?
  162. sigp_external_call : sigp_emergency_signal;
  163. pcpu_sigp_retry(pcpu, order, 0);
  164. }
  165. static int __cpuinit pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
  166. {
  167. struct _lowcore *lc;
  168. if (pcpu != &pcpu_devices[0]) {
  169. pcpu->lowcore = (struct _lowcore *)
  170. __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  171. pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  172. pcpu->panic_stack = __get_free_page(GFP_KERNEL);
  173. if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
  174. goto out;
  175. }
  176. lc = pcpu->lowcore;
  177. memcpy(lc, &S390_lowcore, 512);
  178. memset((char *) lc + 512, 0, sizeof(*lc) - 512);
  179. lc->async_stack = pcpu->async_stack + ASYNC_SIZE;
  180. lc->panic_stack = pcpu->panic_stack + PAGE_SIZE;
  181. lc->cpu_nr = cpu;
  182. #ifndef CONFIG_64BIT
  183. if (MACHINE_HAS_IEEE) {
  184. lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
  185. if (!lc->extended_save_area_addr)
  186. goto out;
  187. }
  188. #else
  189. if (vdso_alloc_per_cpu(lc))
  190. goto out;
  191. #endif
  192. lowcore_ptr[cpu] = lc;
  193. pcpu_sigp_retry(pcpu, sigp_set_prefix, (u32)(unsigned long) lc);
  194. return 0;
  195. out:
  196. if (pcpu != &pcpu_devices[0]) {
  197. free_page(pcpu->panic_stack);
  198. free_pages(pcpu->async_stack, ASYNC_ORDER);
  199. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  200. }
  201. return -ENOMEM;
  202. }
  203. static void pcpu_free_lowcore(struct pcpu *pcpu)
  204. {
  205. pcpu_sigp_retry(pcpu, sigp_set_prefix, 0);
  206. lowcore_ptr[pcpu - pcpu_devices] = NULL;
  207. #ifndef CONFIG_64BIT
  208. if (MACHINE_HAS_IEEE) {
  209. struct _lowcore *lc = pcpu->lowcore;
  210. free_page((unsigned long) lc->extended_save_area_addr);
  211. lc->extended_save_area_addr = 0;
  212. }
  213. #else
  214. vdso_free_per_cpu(pcpu->lowcore);
  215. #endif
  216. if (pcpu != &pcpu_devices[0]) {
  217. free_page(pcpu->panic_stack);
  218. free_pages(pcpu->async_stack, ASYNC_ORDER);
  219. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  220. }
  221. }
  222. static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
  223. {
  224. struct _lowcore *lc = pcpu->lowcore;
  225. atomic_inc(&init_mm.context.attach_count);
  226. lc->cpu_nr = cpu;
  227. lc->percpu_offset = __per_cpu_offset[cpu];
  228. lc->kernel_asce = S390_lowcore.kernel_asce;
  229. lc->machine_flags = S390_lowcore.machine_flags;
  230. lc->ftrace_func = S390_lowcore.ftrace_func;
  231. lc->user_timer = lc->system_timer = lc->steal_timer = 0;
  232. __ctl_store(lc->cregs_save_area, 0, 15);
  233. save_access_regs((unsigned int *) lc->access_regs_save_area);
  234. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  235. MAX_FACILITY_BIT/8);
  236. }
  237. static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
  238. {
  239. struct _lowcore *lc = pcpu->lowcore;
  240. struct thread_info *ti = task_thread_info(tsk);
  241. lc->kernel_stack = (unsigned long) task_stack_page(tsk) + THREAD_SIZE;
  242. lc->thread_info = (unsigned long) task_thread_info(tsk);
  243. lc->current_task = (unsigned long) tsk;
  244. lc->user_timer = ti->user_timer;
  245. lc->system_timer = ti->system_timer;
  246. lc->steal_timer = 0;
  247. }
  248. static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
  249. {
  250. struct _lowcore *lc = pcpu->lowcore;
  251. lc->restart_stack = lc->kernel_stack;
  252. lc->restart_fn = (unsigned long) func;
  253. lc->restart_data = (unsigned long) data;
  254. lc->restart_source = -1UL;
  255. pcpu_sigp_retry(pcpu, sigp_restart, 0);
  256. }
  257. /*
  258. * Call function via PSW restart on pcpu and stop the current cpu.
  259. */
  260. static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
  261. void *data, unsigned long stack)
  262. {
  263. struct _lowcore *lc = pcpu->lowcore;
  264. unsigned short this_cpu;
  265. __load_psw_mask(psw_kernel_bits);
  266. this_cpu = stap();
  267. if (pcpu->address == this_cpu)
  268. func(data); /* should not return */
  269. /* Stop target cpu (if func returns this stops the current cpu). */
  270. pcpu_sigp_retry(pcpu, sigp_stop, 0);
  271. /* Restart func on the target cpu and stop the current cpu. */
  272. lc->restart_stack = stack;
  273. lc->restart_fn = (unsigned long) func;
  274. lc->restart_data = (unsigned long) data;
  275. lc->restart_source = (unsigned long) this_cpu;
  276. asm volatile(
  277. "0: sigp 0,%0,6 # sigp restart to target cpu\n"
  278. " brc 2,0b # busy, try again\n"
  279. "1: sigp 0,%1,5 # sigp stop to current cpu\n"
  280. " brc 2,1b # busy, try again\n"
  281. : : "d" (pcpu->address), "d" (this_cpu) : "0", "1", "cc");
  282. for (;;) ;
  283. }
  284. /*
  285. * Call function on an online CPU.
  286. */
  287. void smp_call_online_cpu(void (*func)(void *), void *data)
  288. {
  289. struct pcpu *pcpu;
  290. /* Use the current cpu if it is online. */
  291. pcpu = pcpu_find_address(cpu_online_mask, stap());
  292. if (!pcpu)
  293. /* Use the first online cpu. */
  294. pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
  295. pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
  296. }
  297. /*
  298. * Call function on the ipl CPU.
  299. */
  300. void smp_call_ipl_cpu(void (*func)(void *), void *data)
  301. {
  302. pcpu_delegate(&pcpu_devices[0], func, data, pcpu_devices->panic_stack);
  303. }
  304. int smp_find_processor_id(u16 address)
  305. {
  306. int cpu;
  307. for_each_present_cpu(cpu)
  308. if (pcpu_devices[cpu].address == address)
  309. return cpu;
  310. return -1;
  311. }
  312. int smp_vcpu_scheduled(int cpu)
  313. {
  314. return pcpu_running(pcpu_devices + cpu);
  315. }
  316. void smp_yield(void)
  317. {
  318. if (MACHINE_HAS_DIAG44)
  319. asm volatile("diag 0,0,0x44");
  320. }
  321. void smp_yield_cpu(int cpu)
  322. {
  323. if (MACHINE_HAS_DIAG9C)
  324. asm volatile("diag %0,0,0x9c"
  325. : : "d" (pcpu_devices[cpu].address));
  326. else if (MACHINE_HAS_DIAG44)
  327. asm volatile("diag 0,0,0x44");
  328. }
  329. /*
  330. * Send cpus emergency shutdown signal. This gives the cpus the
  331. * opportunity to complete outstanding interrupts.
  332. */
  333. void smp_emergency_stop(cpumask_t *cpumask)
  334. {
  335. u64 end;
  336. int cpu;
  337. end = get_clock() + (1000000UL << 12);
  338. for_each_cpu(cpu, cpumask) {
  339. struct pcpu *pcpu = pcpu_devices + cpu;
  340. set_bit(ec_stop_cpu, &pcpu->ec_mask);
  341. while (__pcpu_sigp(pcpu->address, sigp_emergency_signal,
  342. 0, NULL) == sigp_busy &&
  343. get_clock() < end)
  344. cpu_relax();
  345. }
  346. while (get_clock() < end) {
  347. for_each_cpu(cpu, cpumask)
  348. if (pcpu_stopped(pcpu_devices + cpu))
  349. cpumask_clear_cpu(cpu, cpumask);
  350. if (cpumask_empty(cpumask))
  351. break;
  352. cpu_relax();
  353. }
  354. }
  355. /*
  356. * Stop all cpus but the current one.
  357. */
  358. void smp_send_stop(void)
  359. {
  360. cpumask_t cpumask;
  361. int cpu;
  362. /* Disable all interrupts/machine checks */
  363. __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
  364. trace_hardirqs_off();
  365. debug_set_critical();
  366. cpumask_copy(&cpumask, cpu_online_mask);
  367. cpumask_clear_cpu(smp_processor_id(), &cpumask);
  368. if (oops_in_progress)
  369. smp_emergency_stop(&cpumask);
  370. /* stop all processors */
  371. for_each_cpu(cpu, &cpumask) {
  372. struct pcpu *pcpu = pcpu_devices + cpu;
  373. pcpu_sigp_retry(pcpu, sigp_stop, 0);
  374. while (!pcpu_stopped(pcpu))
  375. cpu_relax();
  376. }
  377. }
  378. /*
  379. * Stop the current cpu.
  380. */
  381. void smp_stop_cpu(void)
  382. {
  383. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), sigp_stop, 0);
  384. for (;;) ;
  385. }
  386. /*
  387. * This is the main routine where commands issued by other
  388. * cpus are handled.
  389. */
  390. static void do_ext_call_interrupt(struct ext_code ext_code,
  391. unsigned int param32, unsigned long param64)
  392. {
  393. unsigned long bits;
  394. int cpu;
  395. cpu = smp_processor_id();
  396. if (ext_code.code == 0x1202)
  397. kstat_cpu(cpu).irqs[EXTINT_EXC]++;
  398. else
  399. kstat_cpu(cpu).irqs[EXTINT_EMS]++;
  400. /*
  401. * handle bit signal external calls
  402. */
  403. bits = xchg(&pcpu_devices[cpu].ec_mask, 0);
  404. if (test_bit(ec_stop_cpu, &bits))
  405. smp_stop_cpu();
  406. if (test_bit(ec_schedule, &bits))
  407. scheduler_ipi();
  408. if (test_bit(ec_call_function, &bits))
  409. generic_smp_call_function_interrupt();
  410. if (test_bit(ec_call_function_single, &bits))
  411. generic_smp_call_function_single_interrupt();
  412. }
  413. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  414. {
  415. int cpu;
  416. for_each_cpu(cpu, mask)
  417. pcpu_ec_call(pcpu_devices + cpu, ec_call_function);
  418. }
  419. void arch_send_call_function_single_ipi(int cpu)
  420. {
  421. pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
  422. }
  423. #ifndef CONFIG_64BIT
  424. /*
  425. * this function sends a 'purge tlb' signal to another CPU.
  426. */
  427. static void smp_ptlb_callback(void *info)
  428. {
  429. __tlb_flush_local();
  430. }
  431. void smp_ptlb_all(void)
  432. {
  433. on_each_cpu(smp_ptlb_callback, NULL, 1);
  434. }
  435. EXPORT_SYMBOL(smp_ptlb_all);
  436. #endif /* ! CONFIG_64BIT */
  437. /*
  438. * this function sends a 'reschedule' IPI to another CPU.
  439. * it goes straight through and wastes no time serializing
  440. * anything. Worst case is that we lose a reschedule ...
  441. */
  442. void smp_send_reschedule(int cpu)
  443. {
  444. pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
  445. }
  446. /*
  447. * parameter area for the set/clear control bit callbacks
  448. */
  449. struct ec_creg_mask_parms {
  450. unsigned long orval;
  451. unsigned long andval;
  452. int cr;
  453. };
  454. /*
  455. * callback for setting/clearing control bits
  456. */
  457. static void smp_ctl_bit_callback(void *info)
  458. {
  459. struct ec_creg_mask_parms *pp = info;
  460. unsigned long cregs[16];
  461. __ctl_store(cregs, 0, 15);
  462. cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
  463. __ctl_load(cregs, 0, 15);
  464. }
  465. /*
  466. * Set a bit in a control register of all cpus
  467. */
  468. void smp_ctl_set_bit(int cr, int bit)
  469. {
  470. struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
  471. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  472. }
  473. EXPORT_SYMBOL(smp_ctl_set_bit);
  474. /*
  475. * Clear a bit in a control register of all cpus
  476. */
  477. void smp_ctl_clear_bit(int cr, int bit)
  478. {
  479. struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
  480. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  481. }
  482. EXPORT_SYMBOL(smp_ctl_clear_bit);
  483. #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
  484. struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
  485. EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
  486. static void __init smp_get_save_area(int cpu, u16 address)
  487. {
  488. void *lc = pcpu_devices[0].lowcore;
  489. struct save_area *save_area;
  490. if (is_kdump_kernel())
  491. return;
  492. if (!OLDMEM_BASE && (address == boot_cpu_address ||
  493. ipl_info.type != IPL_TYPE_FCP_DUMP))
  494. return;
  495. if (cpu >= NR_CPUS) {
  496. pr_warning("CPU %i exceeds the maximum %i and is excluded "
  497. "from the dump\n", cpu, NR_CPUS - 1);
  498. return;
  499. }
  500. save_area = kmalloc(sizeof(struct save_area), GFP_KERNEL);
  501. if (!save_area)
  502. panic("could not allocate memory for save area\n");
  503. zfcpdump_save_areas[cpu] = save_area;
  504. #ifdef CONFIG_CRASH_DUMP
  505. if (address == boot_cpu_address) {
  506. /* Copy the registers of the boot cpu. */
  507. copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
  508. SAVE_AREA_BASE - PAGE_SIZE, 0);
  509. return;
  510. }
  511. #endif
  512. /* Get the registers of a non-boot cpu. */
  513. __pcpu_sigp_relax(address, sigp_stop_and_store_status, 0, NULL);
  514. memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
  515. }
  516. int smp_store_status(int cpu)
  517. {
  518. struct pcpu *pcpu;
  519. pcpu = pcpu_devices + cpu;
  520. if (__pcpu_sigp_relax(pcpu->address, sigp_stop_and_store_status,
  521. 0, NULL) != sigp_order_code_accepted)
  522. return -EIO;
  523. return 0;
  524. }
  525. #else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
  526. static inline void smp_get_save_area(int cpu, u16 address) { }
  527. #endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
  528. static struct sclp_cpu_info *smp_get_cpu_info(void)
  529. {
  530. static int use_sigp_detection;
  531. struct sclp_cpu_info *info;
  532. int address;
  533. info = kzalloc(sizeof(*info), GFP_KERNEL);
  534. if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
  535. use_sigp_detection = 1;
  536. for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
  537. if (__pcpu_sigp_relax(address, sigp_sense, 0, NULL) ==
  538. sigp_not_operational)
  539. continue;
  540. info->cpu[info->configured].address = address;
  541. info->configured++;
  542. }
  543. info->combined = info->configured;
  544. }
  545. return info;
  546. }
  547. static int __devinit smp_add_present_cpu(int cpu);
  548. static int __devinit __smp_rescan_cpus(struct sclp_cpu_info *info,
  549. int sysfs_add)
  550. {
  551. struct pcpu *pcpu;
  552. cpumask_t avail;
  553. int cpu, nr, i;
  554. nr = 0;
  555. cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
  556. cpu = cpumask_first(&avail);
  557. for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
  558. if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
  559. continue;
  560. if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
  561. continue;
  562. pcpu = pcpu_devices + cpu;
  563. pcpu->address = info->cpu[i].address;
  564. pcpu->state = (cpu >= info->configured) ?
  565. CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
  566. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  567. set_cpu_present(cpu, true);
  568. if (sysfs_add && smp_add_present_cpu(cpu) != 0)
  569. set_cpu_present(cpu, false);
  570. else
  571. nr++;
  572. cpu = cpumask_next(cpu, &avail);
  573. }
  574. return nr;
  575. }
  576. static void __init smp_detect_cpus(void)
  577. {
  578. unsigned int cpu, c_cpus, s_cpus;
  579. struct sclp_cpu_info *info;
  580. info = smp_get_cpu_info();
  581. if (!info)
  582. panic("smp_detect_cpus failed to allocate memory\n");
  583. if (info->has_cpu_type) {
  584. for (cpu = 0; cpu < info->combined; cpu++) {
  585. if (info->cpu[cpu].address != boot_cpu_address)
  586. continue;
  587. /* The boot cpu dictates the cpu type. */
  588. boot_cpu_type = info->cpu[cpu].type;
  589. break;
  590. }
  591. }
  592. c_cpus = s_cpus = 0;
  593. for (cpu = 0; cpu < info->combined; cpu++) {
  594. if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
  595. continue;
  596. if (cpu < info->configured) {
  597. smp_get_save_area(c_cpus, info->cpu[cpu].address);
  598. c_cpus++;
  599. } else
  600. s_cpus++;
  601. }
  602. pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
  603. get_online_cpus();
  604. __smp_rescan_cpus(info, 0);
  605. put_online_cpus();
  606. kfree(info);
  607. }
  608. /*
  609. * Activate a secondary processor.
  610. */
  611. static void __cpuinit smp_start_secondary(void *cpuvoid)
  612. {
  613. S390_lowcore.last_update_clock = get_clock();
  614. S390_lowcore.restart_stack = (unsigned long) restart_stack;
  615. S390_lowcore.restart_fn = (unsigned long) do_restart;
  616. S390_lowcore.restart_data = 0;
  617. S390_lowcore.restart_source = -1UL;
  618. restore_access_regs(S390_lowcore.access_regs_save_area);
  619. __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
  620. __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
  621. cpu_init();
  622. preempt_disable();
  623. init_cpu_timer();
  624. init_cpu_vtimer();
  625. pfault_init();
  626. notify_cpu_starting(smp_processor_id());
  627. ipi_call_lock();
  628. set_cpu_online(smp_processor_id(), true);
  629. ipi_call_unlock();
  630. /*
  631. * Wait until the cpu which brought this one up marked it
  632. * active before enabling interrupts.
  633. */
  634. while (!cpumask_test_cpu(smp_processor_id(), cpu_active_mask))
  635. cpu_relax();
  636. local_irq_enable();
  637. /* cpu_idle will call schedule for us */
  638. cpu_idle();
  639. }
  640. struct create_idle {
  641. struct work_struct work;
  642. struct task_struct *idle;
  643. struct completion done;
  644. int cpu;
  645. };
  646. static void __cpuinit smp_fork_idle(struct work_struct *work)
  647. {
  648. struct create_idle *c_idle;
  649. c_idle = container_of(work, struct create_idle, work);
  650. c_idle->idle = fork_idle(c_idle->cpu);
  651. complete(&c_idle->done);
  652. }
  653. /* Upping and downing of CPUs */
  654. int __cpuinit __cpu_up(unsigned int cpu)
  655. {
  656. struct create_idle c_idle;
  657. struct pcpu *pcpu;
  658. int rc;
  659. pcpu = pcpu_devices + cpu;
  660. if (pcpu->state != CPU_STATE_CONFIGURED)
  661. return -EIO;
  662. if (pcpu_sigp_retry(pcpu, sigp_initial_cpu_reset, 0) !=
  663. sigp_order_code_accepted)
  664. return -EIO;
  665. if (!pcpu->idle) {
  666. c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
  667. INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
  668. c_idle.cpu = cpu;
  669. schedule_work(&c_idle.work);
  670. wait_for_completion(&c_idle.done);
  671. if (IS_ERR(c_idle.idle))
  672. return PTR_ERR(c_idle.idle);
  673. pcpu->idle = c_idle.idle;
  674. }
  675. init_idle(pcpu->idle, cpu);
  676. rc = pcpu_alloc_lowcore(pcpu, cpu);
  677. if (rc)
  678. return rc;
  679. pcpu_prepare_secondary(pcpu, cpu);
  680. pcpu_attach_task(pcpu, pcpu->idle);
  681. pcpu_start_fn(pcpu, smp_start_secondary, NULL);
  682. while (!cpu_online(cpu))
  683. cpu_relax();
  684. return 0;
  685. }
  686. static int __init setup_possible_cpus(char *s)
  687. {
  688. int max, cpu;
  689. if (kstrtoint(s, 0, &max) < 0)
  690. return 0;
  691. init_cpu_possible(cpumask_of(0));
  692. for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
  693. set_cpu_possible(cpu, true);
  694. return 0;
  695. }
  696. early_param("possible_cpus", setup_possible_cpus);
  697. #ifdef CONFIG_HOTPLUG_CPU
  698. int __cpu_disable(void)
  699. {
  700. unsigned long cregs[16];
  701. set_cpu_online(smp_processor_id(), false);
  702. /* Disable pseudo page faults on this cpu. */
  703. pfault_fini();
  704. /* Disable interrupt sources via control register. */
  705. __ctl_store(cregs, 0, 15);
  706. cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
  707. cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
  708. cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
  709. __ctl_load(cregs, 0, 15);
  710. return 0;
  711. }
  712. void __cpu_die(unsigned int cpu)
  713. {
  714. struct pcpu *pcpu;
  715. /* Wait until target cpu is down */
  716. pcpu = pcpu_devices + cpu;
  717. while (!pcpu_stopped(pcpu))
  718. cpu_relax();
  719. pcpu_free_lowcore(pcpu);
  720. atomic_dec(&init_mm.context.attach_count);
  721. }
  722. void __noreturn cpu_die(void)
  723. {
  724. idle_task_exit();
  725. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), sigp_stop, 0);
  726. for (;;) ;
  727. }
  728. #endif /* CONFIG_HOTPLUG_CPU */
  729. static void smp_call_os_info_init_fn(void)
  730. {
  731. int (*init_fn)(void);
  732. unsigned long size;
  733. init_fn = os_info_old_entry(OS_INFO_INIT_FN, &size);
  734. if (!init_fn)
  735. return;
  736. init_fn();
  737. }
  738. void __init smp_prepare_cpus(unsigned int max_cpus)
  739. {
  740. /* request the 0x1201 emergency signal external interrupt */
  741. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  742. panic("Couldn't request external interrupt 0x1201");
  743. /* request the 0x1202 external call external interrupt */
  744. if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
  745. panic("Couldn't request external interrupt 0x1202");
  746. smp_call_os_info_init_fn();
  747. smp_detect_cpus();
  748. }
  749. void __init smp_prepare_boot_cpu(void)
  750. {
  751. struct pcpu *pcpu = pcpu_devices;
  752. boot_cpu_address = stap();
  753. pcpu->idle = current;
  754. pcpu->state = CPU_STATE_CONFIGURED;
  755. pcpu->address = boot_cpu_address;
  756. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  757. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
  758. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
  759. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  760. cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  761. set_cpu_present(0, true);
  762. set_cpu_online(0, true);
  763. }
  764. void __init smp_cpus_done(unsigned int max_cpus)
  765. {
  766. }
  767. void __init smp_setup_processor_id(void)
  768. {
  769. S390_lowcore.cpu_nr = 0;
  770. }
  771. /*
  772. * the frequency of the profiling timer can be changed
  773. * by writing a multiplier value into /proc/profile.
  774. *
  775. * usually you want to run this on all CPUs ;)
  776. */
  777. int setup_profiling_timer(unsigned int multiplier)
  778. {
  779. return 0;
  780. }
  781. #ifdef CONFIG_HOTPLUG_CPU
  782. static ssize_t cpu_configure_show(struct device *dev,
  783. struct device_attribute *attr, char *buf)
  784. {
  785. ssize_t count;
  786. mutex_lock(&smp_cpu_state_mutex);
  787. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  788. mutex_unlock(&smp_cpu_state_mutex);
  789. return count;
  790. }
  791. static ssize_t cpu_configure_store(struct device *dev,
  792. struct device_attribute *attr,
  793. const char *buf, size_t count)
  794. {
  795. struct pcpu *pcpu;
  796. int cpu, val, rc;
  797. char delim;
  798. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  799. return -EINVAL;
  800. if (val != 0 && val != 1)
  801. return -EINVAL;
  802. get_online_cpus();
  803. mutex_lock(&smp_cpu_state_mutex);
  804. rc = -EBUSY;
  805. /* disallow configuration changes of online cpus and cpu 0 */
  806. cpu = dev->id;
  807. if (cpu_online(cpu) || cpu == 0)
  808. goto out;
  809. pcpu = pcpu_devices + cpu;
  810. rc = 0;
  811. switch (val) {
  812. case 0:
  813. if (pcpu->state != CPU_STATE_CONFIGURED)
  814. break;
  815. rc = sclp_cpu_deconfigure(pcpu->address);
  816. if (rc)
  817. break;
  818. pcpu->state = CPU_STATE_STANDBY;
  819. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  820. topology_expect_change();
  821. break;
  822. case 1:
  823. if (pcpu->state != CPU_STATE_STANDBY)
  824. break;
  825. rc = sclp_cpu_configure(pcpu->address);
  826. if (rc)
  827. break;
  828. pcpu->state = CPU_STATE_CONFIGURED;
  829. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  830. topology_expect_change();
  831. break;
  832. default:
  833. break;
  834. }
  835. out:
  836. mutex_unlock(&smp_cpu_state_mutex);
  837. put_online_cpus();
  838. return rc ? rc : count;
  839. }
  840. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  841. #endif /* CONFIG_HOTPLUG_CPU */
  842. static ssize_t show_cpu_address(struct device *dev,
  843. struct device_attribute *attr, char *buf)
  844. {
  845. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  846. }
  847. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  848. static struct attribute *cpu_common_attrs[] = {
  849. #ifdef CONFIG_HOTPLUG_CPU
  850. &dev_attr_configure.attr,
  851. #endif
  852. &dev_attr_address.attr,
  853. NULL,
  854. };
  855. static struct attribute_group cpu_common_attr_group = {
  856. .attrs = cpu_common_attrs,
  857. };
  858. static ssize_t show_capability(struct device *dev,
  859. struct device_attribute *attr, char *buf)
  860. {
  861. unsigned int capability;
  862. int rc;
  863. rc = get_cpu_capability(&capability);
  864. if (rc)
  865. return rc;
  866. return sprintf(buf, "%u\n", capability);
  867. }
  868. static DEVICE_ATTR(capability, 0444, show_capability, NULL);
  869. static ssize_t show_idle_count(struct device *dev,
  870. struct device_attribute *attr, char *buf)
  871. {
  872. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  873. unsigned long long idle_count;
  874. unsigned int sequence;
  875. do {
  876. sequence = ACCESS_ONCE(idle->sequence);
  877. idle_count = ACCESS_ONCE(idle->idle_count);
  878. if (ACCESS_ONCE(idle->idle_enter))
  879. idle_count++;
  880. } while ((sequence & 1) || (idle->sequence != sequence));
  881. return sprintf(buf, "%llu\n", idle_count);
  882. }
  883. static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
  884. static ssize_t show_idle_time(struct device *dev,
  885. struct device_attribute *attr, char *buf)
  886. {
  887. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  888. unsigned long long now, idle_time, idle_enter, idle_exit;
  889. unsigned int sequence;
  890. do {
  891. now = get_clock();
  892. sequence = ACCESS_ONCE(idle->sequence);
  893. idle_time = ACCESS_ONCE(idle->idle_time);
  894. idle_enter = ACCESS_ONCE(idle->idle_enter);
  895. idle_exit = ACCESS_ONCE(idle->idle_exit);
  896. } while ((sequence & 1) || (idle->sequence != sequence));
  897. idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  898. return sprintf(buf, "%llu\n", idle_time >> 12);
  899. }
  900. static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  901. static struct attribute *cpu_online_attrs[] = {
  902. &dev_attr_capability.attr,
  903. &dev_attr_idle_count.attr,
  904. &dev_attr_idle_time_us.attr,
  905. NULL,
  906. };
  907. static struct attribute_group cpu_online_attr_group = {
  908. .attrs = cpu_online_attrs,
  909. };
  910. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  911. unsigned long action, void *hcpu)
  912. {
  913. unsigned int cpu = (unsigned int)(long)hcpu;
  914. struct cpu *c = &pcpu_devices[cpu].cpu;
  915. struct device *s = &c->dev;
  916. struct s390_idle_data *idle;
  917. int err = 0;
  918. switch (action) {
  919. case CPU_ONLINE:
  920. case CPU_ONLINE_FROZEN:
  921. idle = &per_cpu(s390_idle, cpu);
  922. memset(idle, 0, sizeof(struct s390_idle_data));
  923. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  924. break;
  925. case CPU_DEAD:
  926. case CPU_DEAD_FROZEN:
  927. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  928. break;
  929. }
  930. return notifier_from_errno(err);
  931. }
  932. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  933. .notifier_call = smp_cpu_notify,
  934. };
  935. static int __devinit smp_add_present_cpu(int cpu)
  936. {
  937. struct cpu *c = &pcpu_devices[cpu].cpu;
  938. struct device *s = &c->dev;
  939. int rc;
  940. c->hotpluggable = 1;
  941. rc = register_cpu(c, cpu);
  942. if (rc)
  943. goto out;
  944. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  945. if (rc)
  946. goto out_cpu;
  947. if (cpu_online(cpu)) {
  948. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  949. if (rc)
  950. goto out_online;
  951. }
  952. rc = topology_cpu_init(c);
  953. if (rc)
  954. goto out_topology;
  955. return 0;
  956. out_topology:
  957. if (cpu_online(cpu))
  958. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  959. out_online:
  960. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  961. out_cpu:
  962. #ifdef CONFIG_HOTPLUG_CPU
  963. unregister_cpu(c);
  964. #endif
  965. out:
  966. return rc;
  967. }
  968. #ifdef CONFIG_HOTPLUG_CPU
  969. int __ref smp_rescan_cpus(void)
  970. {
  971. struct sclp_cpu_info *info;
  972. int nr;
  973. info = smp_get_cpu_info();
  974. if (!info)
  975. return -ENOMEM;
  976. get_online_cpus();
  977. mutex_lock(&smp_cpu_state_mutex);
  978. nr = __smp_rescan_cpus(info, 1);
  979. mutex_unlock(&smp_cpu_state_mutex);
  980. put_online_cpus();
  981. kfree(info);
  982. if (nr)
  983. topology_schedule_update();
  984. return 0;
  985. }
  986. static ssize_t __ref rescan_store(struct device *dev,
  987. struct device_attribute *attr,
  988. const char *buf,
  989. size_t count)
  990. {
  991. int rc;
  992. rc = smp_rescan_cpus();
  993. return rc ? rc : count;
  994. }
  995. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  996. #endif /* CONFIG_HOTPLUG_CPU */
  997. static int __init s390_smp_init(void)
  998. {
  999. int cpu, rc;
  1000. register_cpu_notifier(&smp_cpu_nb);
  1001. #ifdef CONFIG_HOTPLUG_CPU
  1002. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  1003. if (rc)
  1004. return rc;
  1005. #endif
  1006. for_each_present_cpu(cpu) {
  1007. rc = smp_add_present_cpu(cpu);
  1008. if (rc)
  1009. return rc;
  1010. }
  1011. return 0;
  1012. }
  1013. subsys_initcall(s390_smp_init);