smp.c 27 KB

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