smp.c 26 KB

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