smp.c 26 KB

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