smp.c 25 KB

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