smp.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  164. lc->panic_stack = pcpu->panic_stack + PAGE_SIZE
  165. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  166. lc->cpu_nr = cpu;
  167. #ifndef CONFIG_64BIT
  168. if (MACHINE_HAS_IEEE) {
  169. lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
  170. if (!lc->extended_save_area_addr)
  171. goto out;
  172. }
  173. #else
  174. if (vdso_alloc_per_cpu(lc))
  175. goto out;
  176. #endif
  177. lowcore_ptr[cpu] = lc;
  178. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
  179. return 0;
  180. out:
  181. if (pcpu != &pcpu_devices[0]) {
  182. free_page(pcpu->panic_stack);
  183. free_pages(pcpu->async_stack, ASYNC_ORDER);
  184. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  185. }
  186. return -ENOMEM;
  187. }
  188. #ifdef CONFIG_HOTPLUG_CPU
  189. static void pcpu_free_lowcore(struct pcpu *pcpu)
  190. {
  191. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
  192. lowcore_ptr[pcpu - pcpu_devices] = NULL;
  193. #ifndef CONFIG_64BIT
  194. if (MACHINE_HAS_IEEE) {
  195. struct _lowcore *lc = pcpu->lowcore;
  196. free_page((unsigned long) lc->extended_save_area_addr);
  197. lc->extended_save_area_addr = 0;
  198. }
  199. #else
  200. vdso_free_per_cpu(pcpu->lowcore);
  201. #endif
  202. if (pcpu != &pcpu_devices[0]) {
  203. free_page(pcpu->panic_stack);
  204. free_pages(pcpu->async_stack, ASYNC_ORDER);
  205. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  206. }
  207. }
  208. #endif /* CONFIG_HOTPLUG_CPU */
  209. static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
  210. {
  211. struct _lowcore *lc = pcpu->lowcore;
  212. atomic_inc(&init_mm.context.attach_count);
  213. lc->cpu_nr = cpu;
  214. lc->percpu_offset = __per_cpu_offset[cpu];
  215. lc->kernel_asce = S390_lowcore.kernel_asce;
  216. lc->machine_flags = S390_lowcore.machine_flags;
  217. lc->ftrace_func = S390_lowcore.ftrace_func;
  218. lc->user_timer = lc->system_timer = lc->steal_timer = 0;
  219. __ctl_store(lc->cregs_save_area, 0, 15);
  220. save_access_regs((unsigned int *) lc->access_regs_save_area);
  221. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  222. MAX_FACILITY_BIT/8);
  223. }
  224. static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
  225. {
  226. struct _lowcore *lc = pcpu->lowcore;
  227. struct thread_info *ti = task_thread_info(tsk);
  228. lc->kernel_stack = (unsigned long) task_stack_page(tsk)
  229. + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  230. lc->thread_info = (unsigned long) task_thread_info(tsk);
  231. lc->current_task = (unsigned long) tsk;
  232. lc->user_timer = ti->user_timer;
  233. lc->system_timer = ti->system_timer;
  234. lc->steal_timer = 0;
  235. }
  236. static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
  237. {
  238. struct _lowcore *lc = pcpu->lowcore;
  239. lc->restart_stack = lc->kernel_stack;
  240. lc->restart_fn = (unsigned long) func;
  241. lc->restart_data = (unsigned long) data;
  242. lc->restart_source = -1UL;
  243. pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
  244. }
  245. /*
  246. * Call function via PSW restart on pcpu and stop the current cpu.
  247. */
  248. static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
  249. void *data, unsigned long stack)
  250. {
  251. struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
  252. unsigned long source_cpu = stap();
  253. __load_psw_mask(psw_kernel_bits);
  254. if (pcpu->address == source_cpu)
  255. func(data); /* should not return */
  256. /* Stop target cpu (if func returns this stops the current cpu). */
  257. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  258. /* Restart func on the target cpu and stop the current cpu. */
  259. mem_assign_absolute(lc->restart_stack, stack);
  260. mem_assign_absolute(lc->restart_fn, (unsigned long) func);
  261. mem_assign_absolute(lc->restart_data, (unsigned long) data);
  262. mem_assign_absolute(lc->restart_source, source_cpu);
  263. asm volatile(
  264. "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
  265. " brc 2,0b # busy, try again\n"
  266. "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
  267. " brc 2,1b # busy, try again\n"
  268. : : "d" (pcpu->address), "d" (source_cpu),
  269. "K" (SIGP_RESTART), "K" (SIGP_STOP)
  270. : "0", "1", "cc");
  271. for (;;) ;
  272. }
  273. /*
  274. * Call function on an online CPU.
  275. */
  276. void smp_call_online_cpu(void (*func)(void *), void *data)
  277. {
  278. struct pcpu *pcpu;
  279. /* Use the current cpu if it is online. */
  280. pcpu = pcpu_find_address(cpu_online_mask, stap());
  281. if (!pcpu)
  282. /* Use the first online cpu. */
  283. pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
  284. pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
  285. }
  286. /*
  287. * Call function on the ipl CPU.
  288. */
  289. void smp_call_ipl_cpu(void (*func)(void *), void *data)
  290. {
  291. pcpu_delegate(&pcpu_devices[0], func, data,
  292. pcpu_devices->panic_stack + PAGE_SIZE);
  293. }
  294. int smp_find_processor_id(u16 address)
  295. {
  296. int cpu;
  297. for_each_present_cpu(cpu)
  298. if (pcpu_devices[cpu].address == address)
  299. return cpu;
  300. return -1;
  301. }
  302. int smp_vcpu_scheduled(int cpu)
  303. {
  304. return pcpu_running(pcpu_devices + cpu);
  305. }
  306. void smp_yield(void)
  307. {
  308. if (MACHINE_HAS_DIAG44)
  309. asm volatile("diag 0,0,0x44");
  310. }
  311. void smp_yield_cpu(int cpu)
  312. {
  313. if (MACHINE_HAS_DIAG9C)
  314. asm volatile("diag %0,0,0x9c"
  315. : : "d" (pcpu_devices[cpu].address));
  316. else if (MACHINE_HAS_DIAG44)
  317. asm volatile("diag 0,0,0x44");
  318. }
  319. /*
  320. * Send cpus emergency shutdown signal. This gives the cpus the
  321. * opportunity to complete outstanding interrupts.
  322. */
  323. void smp_emergency_stop(cpumask_t *cpumask)
  324. {
  325. u64 end;
  326. int cpu;
  327. end = get_tod_clock() + (1000000UL << 12);
  328. for_each_cpu(cpu, cpumask) {
  329. struct pcpu *pcpu = pcpu_devices + cpu;
  330. set_bit(ec_stop_cpu, &pcpu->ec_mask);
  331. while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
  332. 0, NULL) == SIGP_CC_BUSY &&
  333. get_tod_clock() < end)
  334. cpu_relax();
  335. }
  336. while (get_tod_clock() < end) {
  337. for_each_cpu(cpu, cpumask)
  338. if (pcpu_stopped(pcpu_devices + cpu))
  339. cpumask_clear_cpu(cpu, cpumask);
  340. if (cpumask_empty(cpumask))
  341. break;
  342. cpu_relax();
  343. }
  344. }
  345. /*
  346. * Stop all cpus but the current one.
  347. */
  348. void smp_send_stop(void)
  349. {
  350. cpumask_t cpumask;
  351. int cpu;
  352. /* Disable all interrupts/machine checks */
  353. __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
  354. trace_hardirqs_off();
  355. debug_set_critical();
  356. cpumask_copy(&cpumask, cpu_online_mask);
  357. cpumask_clear_cpu(smp_processor_id(), &cpumask);
  358. if (oops_in_progress)
  359. smp_emergency_stop(&cpumask);
  360. /* stop all processors */
  361. for_each_cpu(cpu, &cpumask) {
  362. struct pcpu *pcpu = pcpu_devices + cpu;
  363. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  364. while (!pcpu_stopped(pcpu))
  365. cpu_relax();
  366. }
  367. }
  368. /*
  369. * Stop the current cpu.
  370. */
  371. void smp_stop_cpu(void)
  372. {
  373. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  374. for (;;) ;
  375. }
  376. /*
  377. * This is the main routine where commands issued by other
  378. * cpus are handled.
  379. */
  380. static void smp_handle_ext_call(void)
  381. {
  382. unsigned long bits;
  383. /* handle bit signal external calls */
  384. bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
  385. if (test_bit(ec_stop_cpu, &bits))
  386. smp_stop_cpu();
  387. if (test_bit(ec_schedule, &bits))
  388. scheduler_ipi();
  389. if (test_bit(ec_call_function, &bits))
  390. generic_smp_call_function_interrupt();
  391. if (test_bit(ec_call_function_single, &bits))
  392. generic_smp_call_function_single_interrupt();
  393. }
  394. static void do_ext_call_interrupt(struct ext_code ext_code,
  395. unsigned int param32, unsigned long param64)
  396. {
  397. inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
  398. smp_handle_ext_call();
  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 = (i >= 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_startup_entry(CPUHP_ONLINE);
  626. }
  627. /* Upping and downing of CPUs */
  628. int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
  629. {
  630. struct pcpu *pcpu;
  631. int rc;
  632. pcpu = pcpu_devices + cpu;
  633. if (pcpu->state != CPU_STATE_CONFIGURED)
  634. return -EIO;
  635. if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
  636. SIGP_CC_ORDER_CODE_ACCEPTED)
  637. return -EIO;
  638. rc = pcpu_alloc_lowcore(pcpu, cpu);
  639. if (rc)
  640. return rc;
  641. pcpu_prepare_secondary(pcpu, cpu);
  642. pcpu_attach_task(pcpu, tidle);
  643. pcpu_start_fn(pcpu, smp_start_secondary, NULL);
  644. while (!cpu_online(cpu))
  645. cpu_relax();
  646. return 0;
  647. }
  648. static int __init setup_possible_cpus(char *s)
  649. {
  650. int max, cpu;
  651. if (kstrtoint(s, 0, &max) < 0)
  652. return 0;
  653. init_cpu_possible(cpumask_of(0));
  654. for (cpu = 1; cpu < max && cpu < nr_cpu_ids; cpu++)
  655. set_cpu_possible(cpu, true);
  656. return 0;
  657. }
  658. early_param("possible_cpus", setup_possible_cpus);
  659. #ifdef CONFIG_HOTPLUG_CPU
  660. int __cpu_disable(void)
  661. {
  662. unsigned long cregs[16];
  663. /* Handle possible pending IPIs */
  664. smp_handle_ext_call();
  665. set_cpu_online(smp_processor_id(), false);
  666. /* Disable pseudo page faults on this cpu. */
  667. pfault_fini();
  668. /* Disable interrupt sources via control register. */
  669. __ctl_store(cregs, 0, 15);
  670. cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
  671. cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
  672. cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
  673. __ctl_load(cregs, 0, 15);
  674. return 0;
  675. }
  676. void __cpu_die(unsigned int cpu)
  677. {
  678. struct pcpu *pcpu;
  679. /* Wait until target cpu is down */
  680. pcpu = pcpu_devices + cpu;
  681. while (!pcpu_stopped(pcpu))
  682. cpu_relax();
  683. pcpu_free_lowcore(pcpu);
  684. atomic_dec(&init_mm.context.attach_count);
  685. }
  686. void __noreturn cpu_die(void)
  687. {
  688. idle_task_exit();
  689. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  690. for (;;) ;
  691. }
  692. #endif /* CONFIG_HOTPLUG_CPU */
  693. void __init smp_prepare_cpus(unsigned int max_cpus)
  694. {
  695. /* request the 0x1201 emergency signal external interrupt */
  696. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  697. panic("Couldn't request external interrupt 0x1201");
  698. /* request the 0x1202 external call external interrupt */
  699. if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
  700. panic("Couldn't request external interrupt 0x1202");
  701. smp_detect_cpus();
  702. }
  703. void __init smp_prepare_boot_cpu(void)
  704. {
  705. struct pcpu *pcpu = pcpu_devices;
  706. boot_cpu_address = stap();
  707. pcpu->state = CPU_STATE_CONFIGURED;
  708. pcpu->address = boot_cpu_address;
  709. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  710. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE
  711. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  712. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE
  713. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  714. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  715. smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  716. set_cpu_present(0, true);
  717. set_cpu_online(0, true);
  718. }
  719. void __init smp_cpus_done(unsigned int max_cpus)
  720. {
  721. }
  722. void __init smp_setup_processor_id(void)
  723. {
  724. S390_lowcore.cpu_nr = 0;
  725. }
  726. /*
  727. * the frequency of the profiling timer can be changed
  728. * by writing a multiplier value into /proc/profile.
  729. *
  730. * usually you want to run this on all CPUs ;)
  731. */
  732. int setup_profiling_timer(unsigned int multiplier)
  733. {
  734. return 0;
  735. }
  736. #ifdef CONFIG_HOTPLUG_CPU
  737. static ssize_t cpu_configure_show(struct device *dev,
  738. struct device_attribute *attr, char *buf)
  739. {
  740. ssize_t count;
  741. mutex_lock(&smp_cpu_state_mutex);
  742. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  743. mutex_unlock(&smp_cpu_state_mutex);
  744. return count;
  745. }
  746. static ssize_t cpu_configure_store(struct device *dev,
  747. struct device_attribute *attr,
  748. const char *buf, size_t count)
  749. {
  750. struct pcpu *pcpu;
  751. int cpu, val, rc;
  752. char delim;
  753. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  754. return -EINVAL;
  755. if (val != 0 && val != 1)
  756. return -EINVAL;
  757. get_online_cpus();
  758. mutex_lock(&smp_cpu_state_mutex);
  759. rc = -EBUSY;
  760. /* disallow configuration changes of online cpus and cpu 0 */
  761. cpu = dev->id;
  762. if (cpu_online(cpu) || cpu == 0)
  763. goto out;
  764. pcpu = pcpu_devices + cpu;
  765. rc = 0;
  766. switch (val) {
  767. case 0:
  768. if (pcpu->state != CPU_STATE_CONFIGURED)
  769. break;
  770. rc = sclp_cpu_deconfigure(pcpu->address);
  771. if (rc)
  772. break;
  773. pcpu->state = CPU_STATE_STANDBY;
  774. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  775. topology_expect_change();
  776. break;
  777. case 1:
  778. if (pcpu->state != CPU_STATE_STANDBY)
  779. break;
  780. rc = sclp_cpu_configure(pcpu->address);
  781. if (rc)
  782. break;
  783. pcpu->state = CPU_STATE_CONFIGURED;
  784. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  785. topology_expect_change();
  786. break;
  787. default:
  788. break;
  789. }
  790. out:
  791. mutex_unlock(&smp_cpu_state_mutex);
  792. put_online_cpus();
  793. return rc ? rc : count;
  794. }
  795. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  796. #endif /* CONFIG_HOTPLUG_CPU */
  797. static ssize_t show_cpu_address(struct device *dev,
  798. struct device_attribute *attr, char *buf)
  799. {
  800. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  801. }
  802. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  803. static struct attribute *cpu_common_attrs[] = {
  804. #ifdef CONFIG_HOTPLUG_CPU
  805. &dev_attr_configure.attr,
  806. #endif
  807. &dev_attr_address.attr,
  808. NULL,
  809. };
  810. static struct attribute_group cpu_common_attr_group = {
  811. .attrs = cpu_common_attrs,
  812. };
  813. static ssize_t show_idle_count(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 idle_count;
  818. unsigned int sequence;
  819. do {
  820. sequence = ACCESS_ONCE(idle->sequence);
  821. idle_count = ACCESS_ONCE(idle->idle_count);
  822. if (ACCESS_ONCE(idle->clock_idle_enter))
  823. idle_count++;
  824. } while ((sequence & 1) || (idle->sequence != sequence));
  825. return sprintf(buf, "%llu\n", idle_count);
  826. }
  827. static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
  828. static ssize_t show_idle_time(struct device *dev,
  829. struct device_attribute *attr, char *buf)
  830. {
  831. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  832. unsigned long long now, idle_time, idle_enter, idle_exit;
  833. unsigned int sequence;
  834. do {
  835. now = get_tod_clock();
  836. sequence = ACCESS_ONCE(idle->sequence);
  837. idle_time = ACCESS_ONCE(idle->idle_time);
  838. idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
  839. idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
  840. } while ((sequence & 1) || (idle->sequence != sequence));
  841. idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  842. return sprintf(buf, "%llu\n", idle_time >> 12);
  843. }
  844. static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  845. static struct attribute *cpu_online_attrs[] = {
  846. &dev_attr_idle_count.attr,
  847. &dev_attr_idle_time_us.attr,
  848. NULL,
  849. };
  850. static struct attribute_group cpu_online_attr_group = {
  851. .attrs = cpu_online_attrs,
  852. };
  853. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  854. unsigned long action, void *hcpu)
  855. {
  856. unsigned int cpu = (unsigned int)(long)hcpu;
  857. struct cpu *c = &pcpu_devices[cpu].cpu;
  858. struct device *s = &c->dev;
  859. int err = 0;
  860. switch (action & ~CPU_TASKS_FROZEN) {
  861. case CPU_ONLINE:
  862. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  863. break;
  864. case CPU_DEAD:
  865. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  866. break;
  867. }
  868. return notifier_from_errno(err);
  869. }
  870. static int __cpuinit smp_add_present_cpu(int cpu)
  871. {
  872. struct cpu *c = &pcpu_devices[cpu].cpu;
  873. struct device *s = &c->dev;
  874. int rc;
  875. c->hotpluggable = 1;
  876. rc = register_cpu(c, cpu);
  877. if (rc)
  878. goto out;
  879. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  880. if (rc)
  881. goto out_cpu;
  882. if (cpu_online(cpu)) {
  883. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  884. if (rc)
  885. goto out_online;
  886. }
  887. rc = topology_cpu_init(c);
  888. if (rc)
  889. goto out_topology;
  890. return 0;
  891. out_topology:
  892. if (cpu_online(cpu))
  893. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  894. out_online:
  895. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  896. out_cpu:
  897. #ifdef CONFIG_HOTPLUG_CPU
  898. unregister_cpu(c);
  899. #endif
  900. out:
  901. return rc;
  902. }
  903. #ifdef CONFIG_HOTPLUG_CPU
  904. int __ref smp_rescan_cpus(void)
  905. {
  906. struct sclp_cpu_info *info;
  907. int nr;
  908. info = smp_get_cpu_info();
  909. if (!info)
  910. return -ENOMEM;
  911. get_online_cpus();
  912. mutex_lock(&smp_cpu_state_mutex);
  913. nr = __smp_rescan_cpus(info, 1);
  914. mutex_unlock(&smp_cpu_state_mutex);
  915. put_online_cpus();
  916. kfree(info);
  917. if (nr)
  918. topology_schedule_update();
  919. return 0;
  920. }
  921. static ssize_t __ref rescan_store(struct device *dev,
  922. struct device_attribute *attr,
  923. const char *buf,
  924. size_t count)
  925. {
  926. int rc;
  927. rc = smp_rescan_cpus();
  928. return rc ? rc : count;
  929. }
  930. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  931. #endif /* CONFIG_HOTPLUG_CPU */
  932. static int __init s390_smp_init(void)
  933. {
  934. int cpu, rc;
  935. hotcpu_notifier(smp_cpu_notify, 0);
  936. #ifdef CONFIG_HOTPLUG_CPU
  937. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  938. if (rc)
  939. return rc;
  940. #endif
  941. for_each_present_cpu(cpu) {
  942. rc = smp_add_present_cpu(cpu);
  943. if (rc)
  944. return rc;
  945. }
  946. return 0;
  947. }
  948. subsys_initcall(s390_smp_init);