smp.c 25 KB

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