smp.c 26 KB

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