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. 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_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_clock() < end)
  331. cpu_relax();
  332. }
  333. while (get_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. kstat_cpu(cpu).irqs[EXTINT_EXC]++;
  385. else
  386. kstat_cpu(cpu).irqs[EXTINT_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 __devinit smp_add_present_cpu(int cpu);
  543. static int __devinit __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_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. local_irq_enable();
  624. /* cpu_idle will call schedule for us */
  625. cpu_idle();
  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. set_cpu_online(smp_processor_id(), false);
  664. /* Disable pseudo page faults on this cpu. */
  665. pfault_fini();
  666. /* Disable interrupt sources via control register. */
  667. __ctl_store(cregs, 0, 15);
  668. cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
  669. cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
  670. cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
  671. __ctl_load(cregs, 0, 15);
  672. return 0;
  673. }
  674. void __cpu_die(unsigned int cpu)
  675. {
  676. struct pcpu *pcpu;
  677. /* Wait until target cpu is down */
  678. pcpu = pcpu_devices + cpu;
  679. while (!pcpu_stopped(pcpu))
  680. cpu_relax();
  681. pcpu_free_lowcore(pcpu);
  682. atomic_dec(&init_mm.context.attach_count);
  683. }
  684. void __noreturn cpu_die(void)
  685. {
  686. idle_task_exit();
  687. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  688. for (;;) ;
  689. }
  690. #endif /* CONFIG_HOTPLUG_CPU */
  691. void __init smp_prepare_cpus(unsigned int max_cpus)
  692. {
  693. /* request the 0x1201 emergency signal external interrupt */
  694. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  695. panic("Couldn't request external interrupt 0x1201");
  696. /* request the 0x1202 external call external interrupt */
  697. if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
  698. panic("Couldn't request external interrupt 0x1202");
  699. smp_detect_cpus();
  700. }
  701. void __init smp_prepare_boot_cpu(void)
  702. {
  703. struct pcpu *pcpu = pcpu_devices;
  704. boot_cpu_address = stap();
  705. pcpu->state = CPU_STATE_CONFIGURED;
  706. pcpu->address = boot_cpu_address;
  707. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  708. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE;
  709. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE;
  710. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  711. smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  712. set_cpu_present(0, true);
  713. set_cpu_online(0, true);
  714. }
  715. void __init smp_cpus_done(unsigned int max_cpus)
  716. {
  717. }
  718. void __init smp_setup_processor_id(void)
  719. {
  720. S390_lowcore.cpu_nr = 0;
  721. }
  722. /*
  723. * the frequency of the profiling timer can be changed
  724. * by writing a multiplier value into /proc/profile.
  725. *
  726. * usually you want to run this on all CPUs ;)
  727. */
  728. int setup_profiling_timer(unsigned int multiplier)
  729. {
  730. return 0;
  731. }
  732. #ifdef CONFIG_HOTPLUG_CPU
  733. static ssize_t cpu_configure_show(struct device *dev,
  734. struct device_attribute *attr, char *buf)
  735. {
  736. ssize_t count;
  737. mutex_lock(&smp_cpu_state_mutex);
  738. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  739. mutex_unlock(&smp_cpu_state_mutex);
  740. return count;
  741. }
  742. static ssize_t cpu_configure_store(struct device *dev,
  743. struct device_attribute *attr,
  744. const char *buf, size_t count)
  745. {
  746. struct pcpu *pcpu;
  747. int cpu, val, rc;
  748. char delim;
  749. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  750. return -EINVAL;
  751. if (val != 0 && val != 1)
  752. return -EINVAL;
  753. get_online_cpus();
  754. mutex_lock(&smp_cpu_state_mutex);
  755. rc = -EBUSY;
  756. /* disallow configuration changes of online cpus and cpu 0 */
  757. cpu = dev->id;
  758. if (cpu_online(cpu) || cpu == 0)
  759. goto out;
  760. pcpu = pcpu_devices + cpu;
  761. rc = 0;
  762. switch (val) {
  763. case 0:
  764. if (pcpu->state != CPU_STATE_CONFIGURED)
  765. break;
  766. rc = sclp_cpu_deconfigure(pcpu->address);
  767. if (rc)
  768. break;
  769. pcpu->state = CPU_STATE_STANDBY;
  770. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  771. topology_expect_change();
  772. break;
  773. case 1:
  774. if (pcpu->state != CPU_STATE_STANDBY)
  775. break;
  776. rc = sclp_cpu_configure(pcpu->address);
  777. if (rc)
  778. break;
  779. pcpu->state = CPU_STATE_CONFIGURED;
  780. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  781. topology_expect_change();
  782. break;
  783. default:
  784. break;
  785. }
  786. out:
  787. mutex_unlock(&smp_cpu_state_mutex);
  788. put_online_cpus();
  789. return rc ? rc : count;
  790. }
  791. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  792. #endif /* CONFIG_HOTPLUG_CPU */
  793. static ssize_t show_cpu_address(struct device *dev,
  794. struct device_attribute *attr, char *buf)
  795. {
  796. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  797. }
  798. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  799. static struct attribute *cpu_common_attrs[] = {
  800. #ifdef CONFIG_HOTPLUG_CPU
  801. &dev_attr_configure.attr,
  802. #endif
  803. &dev_attr_address.attr,
  804. NULL,
  805. };
  806. static struct attribute_group cpu_common_attr_group = {
  807. .attrs = cpu_common_attrs,
  808. };
  809. static ssize_t show_idle_count(struct device *dev,
  810. struct device_attribute *attr, char *buf)
  811. {
  812. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  813. unsigned long long idle_count;
  814. unsigned int sequence;
  815. do {
  816. sequence = ACCESS_ONCE(idle->sequence);
  817. idle_count = ACCESS_ONCE(idle->idle_count);
  818. if (ACCESS_ONCE(idle->clock_idle_enter))
  819. idle_count++;
  820. } while ((sequence & 1) || (idle->sequence != sequence));
  821. return sprintf(buf, "%llu\n", idle_count);
  822. }
  823. static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
  824. static ssize_t show_idle_time(struct device *dev,
  825. struct device_attribute *attr, char *buf)
  826. {
  827. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  828. unsigned long long now, idle_time, idle_enter, idle_exit;
  829. unsigned int sequence;
  830. do {
  831. now = get_clock();
  832. sequence = ACCESS_ONCE(idle->sequence);
  833. idle_time = ACCESS_ONCE(idle->idle_time);
  834. idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
  835. idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
  836. } while ((sequence & 1) || (idle->sequence != sequence));
  837. idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  838. return sprintf(buf, "%llu\n", idle_time >> 12);
  839. }
  840. static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  841. static struct attribute *cpu_online_attrs[] = {
  842. &dev_attr_idle_count.attr,
  843. &dev_attr_idle_time_us.attr,
  844. NULL,
  845. };
  846. static struct attribute_group cpu_online_attr_group = {
  847. .attrs = cpu_online_attrs,
  848. };
  849. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  850. unsigned long action, void *hcpu)
  851. {
  852. unsigned int cpu = (unsigned int)(long)hcpu;
  853. struct cpu *c = &pcpu_devices[cpu].cpu;
  854. struct device *s = &c->dev;
  855. int err = 0;
  856. switch (action & ~CPU_TASKS_FROZEN) {
  857. case CPU_ONLINE:
  858. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  859. break;
  860. case CPU_DEAD:
  861. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  862. break;
  863. }
  864. return notifier_from_errno(err);
  865. }
  866. static int __devinit smp_add_present_cpu(int cpu)
  867. {
  868. struct cpu *c = &pcpu_devices[cpu].cpu;
  869. struct device *s = &c->dev;
  870. int rc;
  871. c->hotpluggable = 1;
  872. rc = register_cpu(c, cpu);
  873. if (rc)
  874. goto out;
  875. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  876. if (rc)
  877. goto out_cpu;
  878. if (cpu_online(cpu)) {
  879. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  880. if (rc)
  881. goto out_online;
  882. }
  883. rc = topology_cpu_init(c);
  884. if (rc)
  885. goto out_topology;
  886. return 0;
  887. out_topology:
  888. if (cpu_online(cpu))
  889. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  890. out_online:
  891. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  892. out_cpu:
  893. #ifdef CONFIG_HOTPLUG_CPU
  894. unregister_cpu(c);
  895. #endif
  896. out:
  897. return rc;
  898. }
  899. #ifdef CONFIG_HOTPLUG_CPU
  900. int __ref smp_rescan_cpus(void)
  901. {
  902. struct sclp_cpu_info *info;
  903. int nr;
  904. info = smp_get_cpu_info();
  905. if (!info)
  906. return -ENOMEM;
  907. get_online_cpus();
  908. mutex_lock(&smp_cpu_state_mutex);
  909. nr = __smp_rescan_cpus(info, 1);
  910. mutex_unlock(&smp_cpu_state_mutex);
  911. put_online_cpus();
  912. kfree(info);
  913. if (nr)
  914. topology_schedule_update();
  915. return 0;
  916. }
  917. static ssize_t __ref rescan_store(struct device *dev,
  918. struct device_attribute *attr,
  919. const char *buf,
  920. size_t count)
  921. {
  922. int rc;
  923. rc = smp_rescan_cpus();
  924. return rc ? rc : count;
  925. }
  926. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  927. #endif /* CONFIG_HOTPLUG_CPU */
  928. static int __init s390_smp_init(void)
  929. {
  930. int cpu, rc;
  931. hotcpu_notifier(smp_cpu_notify, 0);
  932. #ifdef CONFIG_HOTPLUG_CPU
  933. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  934. if (rc)
  935. return rc;
  936. #endif
  937. for_each_present_cpu(cpu) {
  938. rc = smp_add_present_cpu(cpu);
  939. if (rc)
  940. return rc;
  941. }
  942. return 0;
  943. }
  944. subsys_initcall(s390_smp_init);