smp.c 26 KB

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