smp.c 25 KB

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