smp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. /*
  2. * arch/s390/kernel/smp.c
  3. *
  4. * Copyright IBM Corp. 1999, 2009
  5. * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
  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. * We work with logical cpu numbering everywhere we can. The only
  14. * functions using the real cpu address (got from STAP) are the sigp
  15. * functions. For all other functions we use the identity mapping.
  16. * That means that cpu_number_map[i] == i for every cpu. cpu_number_map is
  17. * used e.g. to find the idle task belonging to a logical cpu. Every array
  18. * in the kernel is sorted by the logical cpu number and not by the physical
  19. * one which is causing all the confusion with __cpu_logical_map and
  20. * cpu_number_map in other architectures.
  21. */
  22. #define KMSG_COMPONENT "cpu"
  23. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  24. #include <linux/workqueue.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/mm.h>
  28. #include <linux/err.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/delay.h>
  32. #include <linux/cache.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/irqflags.h>
  35. #include <linux/cpu.h>
  36. #include <linux/timex.h>
  37. #include <linux/bootmem.h>
  38. #include <linux/slab.h>
  39. #include <linux/crash_dump.h>
  40. #include <asm/asm-offsets.h>
  41. #include <asm/ipl.h>
  42. #include <asm/setup.h>
  43. #include <asm/sigp.h>
  44. #include <asm/pgalloc.h>
  45. #include <asm/irq.h>
  46. #include <asm/cpcmd.h>
  47. #include <asm/tlbflush.h>
  48. #include <asm/timer.h>
  49. #include <asm/lowcore.h>
  50. #include <asm/sclp.h>
  51. #include <asm/cputime.h>
  52. #include <asm/vdso.h>
  53. #include <asm/cpu.h>
  54. #include "entry.h"
  55. /* logical cpu to cpu address */
  56. unsigned short __cpu_logical_map[NR_CPUS];
  57. static struct task_struct *current_set[NR_CPUS];
  58. static u8 smp_cpu_type;
  59. static int smp_use_sigp_detection;
  60. enum s390_cpu_state {
  61. CPU_STATE_STANDBY,
  62. CPU_STATE_CONFIGURED,
  63. };
  64. DEFINE_MUTEX(smp_cpu_state_mutex);
  65. static int smp_cpu_state[NR_CPUS];
  66. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  67. static void smp_ext_bitcall(int, int);
  68. static int raw_cpu_stopped(int cpu)
  69. {
  70. u32 status;
  71. switch (raw_sigp_ps(&status, 0, cpu, sigp_sense)) {
  72. case sigp_status_stored:
  73. /* Check for stopped and check stop state */
  74. if (status & 0x50)
  75. return 1;
  76. break;
  77. default:
  78. break;
  79. }
  80. return 0;
  81. }
  82. static inline int cpu_stopped(int cpu)
  83. {
  84. return raw_cpu_stopped(cpu_logical_map(cpu));
  85. }
  86. /*
  87. * Ensure that PSW restart is done on an online CPU
  88. */
  89. void smp_restart_with_online_cpu(void)
  90. {
  91. int cpu;
  92. for_each_online_cpu(cpu) {
  93. if (stap() == __cpu_logical_map[cpu]) {
  94. /* We are online: Enable DAT again and return */
  95. __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
  96. return;
  97. }
  98. }
  99. /* We are not online: Do PSW restart on an online CPU */
  100. while (sigp(cpu, sigp_restart) == sigp_busy)
  101. cpu_relax();
  102. /* And stop ourself */
  103. while (raw_sigp(stap(), sigp_stop) == sigp_busy)
  104. cpu_relax();
  105. for (;;);
  106. }
  107. void smp_switch_to_ipl_cpu(void (*func)(void *), void *data)
  108. {
  109. struct _lowcore *lc, *current_lc;
  110. struct stack_frame *sf;
  111. struct pt_regs *regs;
  112. unsigned long sp;
  113. if (smp_processor_id() == 0)
  114. func(data);
  115. __load_psw_mask(PSW_DEFAULT_KEY | PSW_MASK_BASE |
  116. PSW_MASK_EA | PSW_MASK_BA);
  117. /* Disable lowcore protection */
  118. __ctl_clear_bit(0, 28);
  119. current_lc = lowcore_ptr[smp_processor_id()];
  120. lc = lowcore_ptr[0];
  121. if (!lc)
  122. lc = current_lc;
  123. lc->restart_psw.mask =
  124. PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA | PSW_MASK_BA;
  125. lc->restart_psw.addr = PSW_ADDR_AMODE | (unsigned long) smp_restart_cpu;
  126. if (!cpu_online(0))
  127. smp_switch_to_cpu(func, data, 0, stap(), __cpu_logical_map[0]);
  128. while (sigp(0, sigp_stop_and_store_status) == sigp_busy)
  129. cpu_relax();
  130. sp = lc->panic_stack;
  131. sp -= sizeof(struct pt_regs);
  132. regs = (struct pt_regs *) sp;
  133. memcpy(&regs->gprs, &current_lc->gpregs_save_area, sizeof(regs->gprs));
  134. regs->psw = current_lc->psw_save_area;
  135. sp -= STACK_FRAME_OVERHEAD;
  136. sf = (struct stack_frame *) sp;
  137. sf->back_chain = 0;
  138. smp_switch_to_cpu(func, data, sp, stap(), __cpu_logical_map[0]);
  139. }
  140. void smp_send_stop(void)
  141. {
  142. int cpu, rc;
  143. /* Disable all interrupts/machine checks */
  144. __load_psw_mask(psw_kernel_bits | PSW_MASK_DAT);
  145. trace_hardirqs_off();
  146. /* stop all processors */
  147. for_each_online_cpu(cpu) {
  148. if (cpu == smp_processor_id())
  149. continue;
  150. do {
  151. rc = sigp(cpu, sigp_stop);
  152. } while (rc == sigp_busy);
  153. while (!cpu_stopped(cpu))
  154. cpu_relax();
  155. }
  156. }
  157. /*
  158. * This is the main routine where commands issued by other
  159. * cpus are handled.
  160. */
  161. static void do_ext_call_interrupt(unsigned int ext_int_code,
  162. unsigned int param32, unsigned long param64)
  163. {
  164. unsigned long bits;
  165. if ((ext_int_code & 0xffff) == 0x1202)
  166. kstat_cpu(smp_processor_id()).irqs[EXTINT_EXC]++;
  167. else
  168. kstat_cpu(smp_processor_id()).irqs[EXTINT_EMS]++;
  169. /*
  170. * handle bit signal external calls
  171. */
  172. bits = xchg(&S390_lowcore.ext_call_fast, 0);
  173. if (test_bit(ec_schedule, &bits))
  174. scheduler_ipi();
  175. if (test_bit(ec_call_function, &bits))
  176. generic_smp_call_function_interrupt();
  177. if (test_bit(ec_call_function_single, &bits))
  178. generic_smp_call_function_single_interrupt();
  179. }
  180. /*
  181. * Send an external call sigp to another cpu and return without waiting
  182. * for its completion.
  183. */
  184. static void smp_ext_bitcall(int cpu, int sig)
  185. {
  186. int order;
  187. /*
  188. * Set signaling bit in lowcore of target cpu and kick it
  189. */
  190. set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
  191. while (1) {
  192. order = smp_vcpu_scheduled(cpu) ?
  193. sigp_external_call : sigp_emergency_signal;
  194. if (sigp(cpu, order) != sigp_busy)
  195. break;
  196. udelay(10);
  197. }
  198. }
  199. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  200. {
  201. int cpu;
  202. for_each_cpu(cpu, mask)
  203. smp_ext_bitcall(cpu, ec_call_function);
  204. }
  205. void arch_send_call_function_single_ipi(int cpu)
  206. {
  207. smp_ext_bitcall(cpu, ec_call_function_single);
  208. }
  209. #ifndef CONFIG_64BIT
  210. /*
  211. * this function sends a 'purge tlb' signal to another CPU.
  212. */
  213. static void smp_ptlb_callback(void *info)
  214. {
  215. __tlb_flush_local();
  216. }
  217. void smp_ptlb_all(void)
  218. {
  219. on_each_cpu(smp_ptlb_callback, NULL, 1);
  220. }
  221. EXPORT_SYMBOL(smp_ptlb_all);
  222. #endif /* ! CONFIG_64BIT */
  223. /*
  224. * this function sends a 'reschedule' IPI to another CPU.
  225. * it goes straight through and wastes no time serializing
  226. * anything. Worst case is that we lose a reschedule ...
  227. */
  228. void smp_send_reschedule(int cpu)
  229. {
  230. smp_ext_bitcall(cpu, ec_schedule);
  231. }
  232. /*
  233. * parameter area for the set/clear control bit callbacks
  234. */
  235. struct ec_creg_mask_parms {
  236. unsigned long orvals[16];
  237. unsigned long andvals[16];
  238. };
  239. /*
  240. * callback for setting/clearing control bits
  241. */
  242. static void smp_ctl_bit_callback(void *info)
  243. {
  244. struct ec_creg_mask_parms *pp = info;
  245. unsigned long cregs[16];
  246. int i;
  247. __ctl_store(cregs, 0, 15);
  248. for (i = 0; i <= 15; i++)
  249. cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
  250. __ctl_load(cregs, 0, 15);
  251. }
  252. /*
  253. * Set a bit in a control register of all cpus
  254. */
  255. void smp_ctl_set_bit(int cr, int bit)
  256. {
  257. struct ec_creg_mask_parms parms;
  258. memset(&parms.orvals, 0, sizeof(parms.orvals));
  259. memset(&parms.andvals, 0xff, sizeof(parms.andvals));
  260. parms.orvals[cr] = 1UL << bit;
  261. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  262. }
  263. EXPORT_SYMBOL(smp_ctl_set_bit);
  264. /*
  265. * Clear a bit in a control register of all cpus
  266. */
  267. void smp_ctl_clear_bit(int cr, int bit)
  268. {
  269. struct ec_creg_mask_parms parms;
  270. memset(&parms.orvals, 0, sizeof(parms.orvals));
  271. memset(&parms.andvals, 0xff, sizeof(parms.andvals));
  272. parms.andvals[cr] = ~(1UL << bit);
  273. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  274. }
  275. EXPORT_SYMBOL(smp_ctl_clear_bit);
  276. #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
  277. static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
  278. {
  279. if (ipl_info.type != IPL_TYPE_FCP_DUMP && !OLDMEM_BASE)
  280. return;
  281. if (is_kdump_kernel())
  282. return;
  283. if (cpu >= NR_CPUS) {
  284. pr_warning("CPU %i exceeds the maximum %i and is excluded from "
  285. "the dump\n", cpu, NR_CPUS - 1);
  286. return;
  287. }
  288. zfcpdump_save_areas[cpu] = kmalloc(sizeof(struct save_area), GFP_KERNEL);
  289. while (raw_sigp(phy_cpu, sigp_stop_and_store_status) == sigp_busy)
  290. cpu_relax();
  291. memcpy_real(zfcpdump_save_areas[cpu],
  292. (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
  293. sizeof(struct save_area));
  294. }
  295. struct save_area *zfcpdump_save_areas[NR_CPUS + 1];
  296. EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
  297. #else
  298. static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
  299. #endif /* CONFIG_ZFCPDUMP */
  300. static int cpu_known(int cpu_id)
  301. {
  302. int cpu;
  303. for_each_present_cpu(cpu) {
  304. if (__cpu_logical_map[cpu] == cpu_id)
  305. return 1;
  306. }
  307. return 0;
  308. }
  309. static int smp_rescan_cpus_sigp(cpumask_t avail)
  310. {
  311. int cpu_id, logical_cpu;
  312. logical_cpu = cpumask_first(&avail);
  313. if (logical_cpu >= nr_cpu_ids)
  314. return 0;
  315. for (cpu_id = 0; cpu_id <= MAX_CPU_ADDRESS; cpu_id++) {
  316. if (cpu_known(cpu_id))
  317. continue;
  318. __cpu_logical_map[logical_cpu] = cpu_id;
  319. cpu_set_polarization(logical_cpu, POLARIZATION_UNKNOWN);
  320. if (!cpu_stopped(logical_cpu))
  321. continue;
  322. set_cpu_present(logical_cpu, true);
  323. smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
  324. logical_cpu = cpumask_next(logical_cpu, &avail);
  325. if (logical_cpu >= nr_cpu_ids)
  326. break;
  327. }
  328. return 0;
  329. }
  330. static int smp_rescan_cpus_sclp(cpumask_t avail)
  331. {
  332. struct sclp_cpu_info *info;
  333. int cpu_id, logical_cpu, cpu;
  334. int rc;
  335. logical_cpu = cpumask_first(&avail);
  336. if (logical_cpu >= nr_cpu_ids)
  337. return 0;
  338. info = kmalloc(sizeof(*info), GFP_KERNEL);
  339. if (!info)
  340. return -ENOMEM;
  341. rc = sclp_get_cpu_info(info);
  342. if (rc)
  343. goto out;
  344. for (cpu = 0; cpu < info->combined; cpu++) {
  345. if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
  346. continue;
  347. cpu_id = info->cpu[cpu].address;
  348. if (cpu_known(cpu_id))
  349. continue;
  350. __cpu_logical_map[logical_cpu] = cpu_id;
  351. cpu_set_polarization(logical_cpu, POLARIZATION_UNKNOWN);
  352. set_cpu_present(logical_cpu, true);
  353. if (cpu >= info->configured)
  354. smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
  355. else
  356. smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
  357. logical_cpu = cpumask_next(logical_cpu, &avail);
  358. if (logical_cpu >= nr_cpu_ids)
  359. break;
  360. }
  361. out:
  362. kfree(info);
  363. return rc;
  364. }
  365. static int __smp_rescan_cpus(void)
  366. {
  367. cpumask_t avail;
  368. cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
  369. if (smp_use_sigp_detection)
  370. return smp_rescan_cpus_sigp(avail);
  371. else
  372. return smp_rescan_cpus_sclp(avail);
  373. }
  374. static void __init smp_detect_cpus(void)
  375. {
  376. unsigned int cpu, c_cpus, s_cpus;
  377. struct sclp_cpu_info *info;
  378. u16 boot_cpu_addr, cpu_addr;
  379. c_cpus = 1;
  380. s_cpus = 0;
  381. boot_cpu_addr = __cpu_logical_map[0];
  382. info = kmalloc(sizeof(*info), GFP_KERNEL);
  383. if (!info)
  384. panic("smp_detect_cpus failed to allocate memory\n");
  385. #ifdef CONFIG_CRASH_DUMP
  386. if (OLDMEM_BASE && !is_kdump_kernel()) {
  387. struct save_area *save_area;
  388. save_area = kmalloc(sizeof(*save_area), GFP_KERNEL);
  389. if (!save_area)
  390. panic("could not allocate memory for save area\n");
  391. copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
  392. 0x200, 0);
  393. zfcpdump_save_areas[0] = save_area;
  394. }
  395. #endif
  396. /* Use sigp detection algorithm if sclp doesn't work. */
  397. if (sclp_get_cpu_info(info)) {
  398. smp_use_sigp_detection = 1;
  399. for (cpu = 0; cpu <= MAX_CPU_ADDRESS; cpu++) {
  400. if (cpu == boot_cpu_addr)
  401. continue;
  402. if (!raw_cpu_stopped(cpu))
  403. continue;
  404. smp_get_save_area(c_cpus, cpu);
  405. c_cpus++;
  406. }
  407. goto out;
  408. }
  409. if (info->has_cpu_type) {
  410. for (cpu = 0; cpu < info->combined; cpu++) {
  411. if (info->cpu[cpu].address == boot_cpu_addr) {
  412. smp_cpu_type = info->cpu[cpu].type;
  413. break;
  414. }
  415. }
  416. }
  417. for (cpu = 0; cpu < info->combined; cpu++) {
  418. if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
  419. continue;
  420. cpu_addr = info->cpu[cpu].address;
  421. if (cpu_addr == boot_cpu_addr)
  422. continue;
  423. if (!raw_cpu_stopped(cpu_addr)) {
  424. s_cpus++;
  425. continue;
  426. }
  427. smp_get_save_area(c_cpus, cpu_addr);
  428. c_cpus++;
  429. }
  430. out:
  431. kfree(info);
  432. pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
  433. get_online_cpus();
  434. __smp_rescan_cpus();
  435. put_online_cpus();
  436. }
  437. /*
  438. * Activate a secondary processor.
  439. */
  440. int __cpuinit start_secondary(void *cpuvoid)
  441. {
  442. cpu_init();
  443. preempt_disable();
  444. init_cpu_timer();
  445. init_cpu_vtimer();
  446. pfault_init();
  447. notify_cpu_starting(smp_processor_id());
  448. ipi_call_lock();
  449. set_cpu_online(smp_processor_id(), true);
  450. ipi_call_unlock();
  451. __ctl_clear_bit(0, 28); /* Disable lowcore protection */
  452. S390_lowcore.restart_psw.mask =
  453. PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA | PSW_MASK_BA;
  454. S390_lowcore.restart_psw.addr =
  455. PSW_ADDR_AMODE | (unsigned long) psw_restart_int_handler;
  456. __ctl_set_bit(0, 28); /* Enable lowcore protection */
  457. /*
  458. * Wait until the cpu which brought this one up marked it
  459. * active before enabling interrupts.
  460. */
  461. while (!cpumask_test_cpu(smp_processor_id(), cpu_active_mask))
  462. cpu_relax();
  463. local_irq_enable();
  464. /* cpu_idle will call schedule for us */
  465. cpu_idle();
  466. return 0;
  467. }
  468. struct create_idle {
  469. struct work_struct work;
  470. struct task_struct *idle;
  471. struct completion done;
  472. int cpu;
  473. };
  474. static void __cpuinit smp_fork_idle(struct work_struct *work)
  475. {
  476. struct create_idle *c_idle;
  477. c_idle = container_of(work, struct create_idle, work);
  478. c_idle->idle = fork_idle(c_idle->cpu);
  479. complete(&c_idle->done);
  480. }
  481. static int __cpuinit smp_alloc_lowcore(int cpu)
  482. {
  483. unsigned long async_stack, panic_stack;
  484. struct _lowcore *lowcore;
  485. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  486. if (!lowcore)
  487. return -ENOMEM;
  488. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  489. panic_stack = __get_free_page(GFP_KERNEL);
  490. if (!panic_stack || !async_stack)
  491. goto out;
  492. memcpy(lowcore, &S390_lowcore, 512);
  493. memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
  494. lowcore->async_stack = async_stack + ASYNC_SIZE;
  495. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  496. lowcore->restart_psw.mask =
  497. PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA | PSW_MASK_BA;
  498. lowcore->restart_psw.addr =
  499. PSW_ADDR_AMODE | (unsigned long) restart_int_handler;
  500. if (user_mode != HOME_SPACE_MODE)
  501. lowcore->restart_psw.mask |= PSW_ASC_HOME;
  502. #ifndef CONFIG_64BIT
  503. if (MACHINE_HAS_IEEE) {
  504. unsigned long save_area;
  505. save_area = get_zeroed_page(GFP_KERNEL);
  506. if (!save_area)
  507. goto out;
  508. lowcore->extended_save_area_addr = (u32) save_area;
  509. }
  510. #else
  511. if (vdso_alloc_per_cpu(cpu, lowcore))
  512. goto out;
  513. #endif
  514. lowcore_ptr[cpu] = lowcore;
  515. return 0;
  516. out:
  517. free_page(panic_stack);
  518. free_pages(async_stack, ASYNC_ORDER);
  519. free_pages((unsigned long) lowcore, LC_ORDER);
  520. return -ENOMEM;
  521. }
  522. static void smp_free_lowcore(int cpu)
  523. {
  524. struct _lowcore *lowcore;
  525. lowcore = lowcore_ptr[cpu];
  526. #ifndef CONFIG_64BIT
  527. if (MACHINE_HAS_IEEE)
  528. free_page((unsigned long) lowcore->extended_save_area_addr);
  529. #else
  530. vdso_free_per_cpu(cpu, lowcore);
  531. #endif
  532. free_page(lowcore->panic_stack - PAGE_SIZE);
  533. free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
  534. free_pages((unsigned long) lowcore, LC_ORDER);
  535. lowcore_ptr[cpu] = NULL;
  536. }
  537. /* Upping and downing of CPUs */
  538. int __cpuinit __cpu_up(unsigned int cpu)
  539. {
  540. struct _lowcore *cpu_lowcore;
  541. struct create_idle c_idle;
  542. struct task_struct *idle;
  543. struct stack_frame *sf;
  544. u32 lowcore;
  545. int ccode;
  546. if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
  547. return -EIO;
  548. idle = current_set[cpu];
  549. if (!idle) {
  550. c_idle.done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done);
  551. INIT_WORK_ONSTACK(&c_idle.work, smp_fork_idle);
  552. c_idle.cpu = cpu;
  553. schedule_work(&c_idle.work);
  554. wait_for_completion(&c_idle.done);
  555. if (IS_ERR(c_idle.idle))
  556. return PTR_ERR(c_idle.idle);
  557. idle = c_idle.idle;
  558. current_set[cpu] = c_idle.idle;
  559. }
  560. init_idle(idle, cpu);
  561. if (smp_alloc_lowcore(cpu))
  562. return -ENOMEM;
  563. do {
  564. ccode = sigp(cpu, sigp_initial_cpu_reset);
  565. if (ccode == sigp_busy)
  566. udelay(10);
  567. if (ccode == sigp_not_operational)
  568. goto err_out;
  569. } while (ccode == sigp_busy);
  570. lowcore = (u32)(unsigned long)lowcore_ptr[cpu];
  571. while (sigp_p(lowcore, cpu, sigp_set_prefix) == sigp_busy)
  572. udelay(10);
  573. cpu_lowcore = lowcore_ptr[cpu];
  574. cpu_lowcore->kernel_stack = (unsigned long)
  575. task_stack_page(idle) + THREAD_SIZE;
  576. cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
  577. sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
  578. - sizeof(struct pt_regs)
  579. - sizeof(struct stack_frame));
  580. memset(sf, 0, sizeof(struct stack_frame));
  581. sf->gprs[9] = (unsigned long) sf;
  582. cpu_lowcore->gpregs_save_area[15] = (unsigned long) sf;
  583. __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
  584. atomic_inc(&init_mm.context.attach_count);
  585. asm volatile(
  586. " stam 0,15,0(%0)"
  587. : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
  588. cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
  589. cpu_lowcore->current_task = (unsigned long) idle;
  590. cpu_lowcore->cpu_nr = cpu;
  591. cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
  592. cpu_lowcore->machine_flags = S390_lowcore.machine_flags;
  593. cpu_lowcore->ftrace_func = S390_lowcore.ftrace_func;
  594. memcpy(cpu_lowcore->stfle_fac_list, S390_lowcore.stfle_fac_list,
  595. MAX_FACILITY_BIT/8);
  596. eieio();
  597. while (sigp(cpu, sigp_restart) == sigp_busy)
  598. udelay(10);
  599. while (!cpu_online(cpu))
  600. cpu_relax();
  601. return 0;
  602. err_out:
  603. smp_free_lowcore(cpu);
  604. return -EIO;
  605. }
  606. static int __init setup_possible_cpus(char *s)
  607. {
  608. int pcpus, cpu;
  609. pcpus = simple_strtoul(s, NULL, 0);
  610. init_cpu_possible(cpumask_of(0));
  611. for (cpu = 1; cpu < pcpus && cpu < nr_cpu_ids; cpu++)
  612. set_cpu_possible(cpu, true);
  613. return 0;
  614. }
  615. early_param("possible_cpus", setup_possible_cpus);
  616. #ifdef CONFIG_HOTPLUG_CPU
  617. int __cpu_disable(void)
  618. {
  619. struct ec_creg_mask_parms cr_parms;
  620. int cpu = smp_processor_id();
  621. set_cpu_online(cpu, false);
  622. /* Disable pfault pseudo page faults on this cpu. */
  623. pfault_fini();
  624. memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
  625. memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
  626. /* disable all external interrupts */
  627. cr_parms.orvals[0] = 0;
  628. cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 11 |
  629. 1 << 10 | 1 << 9 | 1 << 6 | 1 << 5 |
  630. 1 << 4);
  631. /* disable all I/O interrupts */
  632. cr_parms.orvals[6] = 0;
  633. cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
  634. 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
  635. /* disable most machine checks */
  636. cr_parms.orvals[14] = 0;
  637. cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
  638. 1 << 25 | 1 << 24);
  639. smp_ctl_bit_callback(&cr_parms);
  640. return 0;
  641. }
  642. void __cpu_die(unsigned int cpu)
  643. {
  644. /* Wait until target cpu is down */
  645. while (!cpu_stopped(cpu))
  646. cpu_relax();
  647. while (sigp_p(0, cpu, sigp_set_prefix) == sigp_busy)
  648. udelay(10);
  649. smp_free_lowcore(cpu);
  650. atomic_dec(&init_mm.context.attach_count);
  651. }
  652. void __noreturn cpu_die(void)
  653. {
  654. idle_task_exit();
  655. while (sigp(smp_processor_id(), sigp_stop) == sigp_busy)
  656. cpu_relax();
  657. for (;;);
  658. }
  659. #endif /* CONFIG_HOTPLUG_CPU */
  660. void __init smp_prepare_cpus(unsigned int max_cpus)
  661. {
  662. #ifndef CONFIG_64BIT
  663. unsigned long save_area = 0;
  664. #endif
  665. unsigned long async_stack, panic_stack;
  666. struct _lowcore *lowcore;
  667. smp_detect_cpus();
  668. /* request the 0x1201 emergency signal external interrupt */
  669. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  670. panic("Couldn't request external interrupt 0x1201");
  671. /* request the 0x1202 external call external interrupt */
  672. if (register_external_interrupt(0x1202, do_ext_call_interrupt) != 0)
  673. panic("Couldn't request external interrupt 0x1202");
  674. /* Reallocate current lowcore, but keep its contents. */
  675. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  676. panic_stack = __get_free_page(GFP_KERNEL);
  677. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  678. BUG_ON(!lowcore || !panic_stack || !async_stack);
  679. #ifndef CONFIG_64BIT
  680. if (MACHINE_HAS_IEEE)
  681. save_area = get_zeroed_page(GFP_KERNEL);
  682. #endif
  683. local_irq_disable();
  684. local_mcck_disable();
  685. lowcore_ptr[smp_processor_id()] = lowcore;
  686. *lowcore = S390_lowcore;
  687. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  688. lowcore->async_stack = async_stack + ASYNC_SIZE;
  689. #ifndef CONFIG_64BIT
  690. if (MACHINE_HAS_IEEE)
  691. lowcore->extended_save_area_addr = (u32) save_area;
  692. #endif
  693. set_prefix((u32)(unsigned long) lowcore);
  694. local_mcck_enable();
  695. local_irq_enable();
  696. #ifdef CONFIG_64BIT
  697. if (vdso_alloc_per_cpu(smp_processor_id(), &S390_lowcore))
  698. BUG();
  699. #endif
  700. }
  701. void __init smp_prepare_boot_cpu(void)
  702. {
  703. BUG_ON(smp_processor_id() != 0);
  704. current_thread_info()->cpu = 0;
  705. set_cpu_present(0, true);
  706. set_cpu_online(0, true);
  707. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  708. current_set[0] = current;
  709. smp_cpu_state[0] = CPU_STATE_CONFIGURED;
  710. cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  711. }
  712. void __init smp_cpus_done(unsigned int max_cpus)
  713. {
  714. }
  715. void __init smp_setup_processor_id(void)
  716. {
  717. S390_lowcore.cpu_nr = 0;
  718. __cpu_logical_map[0] = stap();
  719. }
  720. /*
  721. * the frequency of the profiling timer can be changed
  722. * by writing a multiplier value into /proc/profile.
  723. *
  724. * usually you want to run this on all CPUs ;)
  725. */
  726. int setup_profiling_timer(unsigned int multiplier)
  727. {
  728. return 0;
  729. }
  730. #ifdef CONFIG_HOTPLUG_CPU
  731. static ssize_t cpu_configure_show(struct sys_device *dev,
  732. struct sysdev_attribute *attr, char *buf)
  733. {
  734. ssize_t count;
  735. mutex_lock(&smp_cpu_state_mutex);
  736. count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
  737. mutex_unlock(&smp_cpu_state_mutex);
  738. return count;
  739. }
  740. static ssize_t cpu_configure_store(struct sys_device *dev,
  741. struct sysdev_attribute *attr,
  742. const char *buf, size_t count)
  743. {
  744. int cpu = dev->id;
  745. int val, rc;
  746. char delim;
  747. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  748. return -EINVAL;
  749. if (val != 0 && val != 1)
  750. return -EINVAL;
  751. get_online_cpus();
  752. mutex_lock(&smp_cpu_state_mutex);
  753. rc = -EBUSY;
  754. /* disallow configuration changes of online cpus and cpu 0 */
  755. if (cpu_online(cpu) || cpu == 0)
  756. goto out;
  757. rc = 0;
  758. switch (val) {
  759. case 0:
  760. if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
  761. rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
  762. if (!rc) {
  763. smp_cpu_state[cpu] = CPU_STATE_STANDBY;
  764. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  765. topology_expect_change();
  766. }
  767. }
  768. break;
  769. case 1:
  770. if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
  771. rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
  772. if (!rc) {
  773. smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
  774. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  775. topology_expect_change();
  776. }
  777. }
  778. break;
  779. default:
  780. break;
  781. }
  782. out:
  783. mutex_unlock(&smp_cpu_state_mutex);
  784. put_online_cpus();
  785. return rc ? rc : count;
  786. }
  787. static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  788. #endif /* CONFIG_HOTPLUG_CPU */
  789. static ssize_t show_cpu_address(struct sys_device *dev,
  790. struct sysdev_attribute *attr, char *buf)
  791. {
  792. return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
  793. }
  794. static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
  795. static struct attribute *cpu_common_attrs[] = {
  796. #ifdef CONFIG_HOTPLUG_CPU
  797. &attr_configure.attr,
  798. #endif
  799. &attr_address.attr,
  800. NULL,
  801. };
  802. static struct attribute_group cpu_common_attr_group = {
  803. .attrs = cpu_common_attrs,
  804. };
  805. static ssize_t show_capability(struct sys_device *dev,
  806. struct sysdev_attribute *attr, char *buf)
  807. {
  808. unsigned int capability;
  809. int rc;
  810. rc = get_cpu_capability(&capability);
  811. if (rc)
  812. return rc;
  813. return sprintf(buf, "%u\n", capability);
  814. }
  815. static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
  816. static ssize_t show_idle_count(struct sys_device *dev,
  817. struct sysdev_attribute *attr, char *buf)
  818. {
  819. struct s390_idle_data *idle;
  820. unsigned long long idle_count;
  821. unsigned int sequence;
  822. idle = &per_cpu(s390_idle, dev->id);
  823. repeat:
  824. sequence = idle->sequence;
  825. smp_rmb();
  826. if (sequence & 1)
  827. goto repeat;
  828. idle_count = idle->idle_count;
  829. if (idle->idle_enter)
  830. idle_count++;
  831. smp_rmb();
  832. if (idle->sequence != sequence)
  833. goto repeat;
  834. return sprintf(buf, "%llu\n", idle_count);
  835. }
  836. static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
  837. static ssize_t show_idle_time(struct sys_device *dev,
  838. struct sysdev_attribute *attr, char *buf)
  839. {
  840. struct s390_idle_data *idle;
  841. unsigned long long now, idle_time, idle_enter;
  842. unsigned int sequence;
  843. idle = &per_cpu(s390_idle, dev->id);
  844. now = get_clock();
  845. repeat:
  846. sequence = idle->sequence;
  847. smp_rmb();
  848. if (sequence & 1)
  849. goto repeat;
  850. idle_time = idle->idle_time;
  851. idle_enter = idle->idle_enter;
  852. if (idle_enter != 0ULL && idle_enter < now)
  853. idle_time += now - idle_enter;
  854. smp_rmb();
  855. if (idle->sequence != sequence)
  856. goto repeat;
  857. return sprintf(buf, "%llu\n", idle_time >> 12);
  858. }
  859. static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  860. static struct attribute *cpu_online_attrs[] = {
  861. &attr_capability.attr,
  862. &attr_idle_count.attr,
  863. &attr_idle_time_us.attr,
  864. NULL,
  865. };
  866. static struct attribute_group cpu_online_attr_group = {
  867. .attrs = cpu_online_attrs,
  868. };
  869. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  870. unsigned long action, void *hcpu)
  871. {
  872. unsigned int cpu = (unsigned int)(long)hcpu;
  873. struct cpu *c = &per_cpu(cpu_devices, cpu);
  874. struct sys_device *s = &c->sysdev;
  875. struct s390_idle_data *idle;
  876. int err = 0;
  877. switch (action) {
  878. case CPU_ONLINE:
  879. case CPU_ONLINE_FROZEN:
  880. idle = &per_cpu(s390_idle, cpu);
  881. memset(idle, 0, sizeof(struct s390_idle_data));
  882. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  883. break;
  884. case CPU_DEAD:
  885. case CPU_DEAD_FROZEN:
  886. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  887. break;
  888. }
  889. return notifier_from_errno(err);
  890. }
  891. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  892. .notifier_call = smp_cpu_notify,
  893. };
  894. static int __devinit smp_add_present_cpu(int cpu)
  895. {
  896. struct cpu *c = &per_cpu(cpu_devices, cpu);
  897. struct sys_device *s = &c->sysdev;
  898. int rc;
  899. c->hotpluggable = 1;
  900. rc = register_cpu(c, cpu);
  901. if (rc)
  902. goto out;
  903. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  904. if (rc)
  905. goto out_cpu;
  906. if (cpu_online(cpu)) {
  907. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  908. if (rc)
  909. goto out_online;
  910. }
  911. rc = topology_cpu_init(c);
  912. if (rc)
  913. goto out_topology;
  914. return 0;
  915. out_topology:
  916. if (cpu_online(cpu))
  917. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  918. out_online:
  919. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  920. out_cpu:
  921. #ifdef CONFIG_HOTPLUG_CPU
  922. unregister_cpu(c);
  923. #endif
  924. out:
  925. return rc;
  926. }
  927. #ifdef CONFIG_HOTPLUG_CPU
  928. int __ref smp_rescan_cpus(void)
  929. {
  930. cpumask_t newcpus;
  931. int cpu;
  932. int rc;
  933. get_online_cpus();
  934. mutex_lock(&smp_cpu_state_mutex);
  935. cpumask_copy(&newcpus, cpu_present_mask);
  936. rc = __smp_rescan_cpus();
  937. if (rc)
  938. goto out;
  939. cpumask_andnot(&newcpus, cpu_present_mask, &newcpus);
  940. for_each_cpu(cpu, &newcpus) {
  941. rc = smp_add_present_cpu(cpu);
  942. if (rc)
  943. set_cpu_present(cpu, false);
  944. }
  945. rc = 0;
  946. out:
  947. mutex_unlock(&smp_cpu_state_mutex);
  948. put_online_cpus();
  949. if (!cpumask_empty(&newcpus))
  950. topology_schedule_update();
  951. return rc;
  952. }
  953. static ssize_t __ref rescan_store(struct sysdev_class *class,
  954. struct sysdev_class_attribute *attr,
  955. const char *buf,
  956. size_t count)
  957. {
  958. int rc;
  959. rc = smp_rescan_cpus();
  960. return rc ? rc : count;
  961. }
  962. static SYSDEV_CLASS_ATTR(rescan, 0200, NULL, rescan_store);
  963. #endif /* CONFIG_HOTPLUG_CPU */
  964. static int __init s390_smp_init(void)
  965. {
  966. int cpu, rc;
  967. register_cpu_notifier(&smp_cpu_nb);
  968. #ifdef CONFIG_HOTPLUG_CPU
  969. rc = sysdev_class_create_file(&cpu_sysdev_class, &attr_rescan);
  970. if (rc)
  971. return rc;
  972. #endif
  973. for_each_present_cpu(cpu) {
  974. rc = smp_add_present_cpu(cpu);
  975. if (rc)
  976. return rc;
  977. }
  978. return 0;
  979. }
  980. subsys_initcall(s390_smp_init);