smp.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. /*
  558. * Only need to copy the first 512 bytes from address 0. But since
  559. * the compiler emits a warning if src == NULL for memcpy use copy_page
  560. * instead. Copies more than needed but this code is not performance
  561. * critical.
  562. */
  563. copy_page(lowcore, &S390_lowcore);
  564. memset((void *)lowcore + 512, 0, sizeof(*lowcore) - 512);
  565. lowcore->async_stack = async_stack + ASYNC_SIZE;
  566. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  567. #ifndef CONFIG_64BIT
  568. if (MACHINE_HAS_IEEE) {
  569. unsigned long save_area;
  570. save_area = get_zeroed_page(GFP_KERNEL);
  571. if (!save_area)
  572. goto out_save_area;
  573. lowcore->extended_save_area_addr = (u32) save_area;
  574. }
  575. #endif
  576. lowcore_ptr[cpu] = lowcore;
  577. return 0;
  578. #ifndef CONFIG_64BIT
  579. out_save_area:
  580. free_page(panic_stack);
  581. #endif
  582. out:
  583. free_pages(async_stack, ASYNC_ORDER);
  584. free_pages((unsigned long) lowcore, lc_order);
  585. return -ENOMEM;
  586. }
  587. #ifdef CONFIG_HOTPLUG_CPU
  588. static void smp_free_lowcore(int cpu)
  589. {
  590. struct _lowcore *lowcore;
  591. int lc_order;
  592. lc_order = sizeof(long) == 8 ? 1 : 0;
  593. lowcore = lowcore_ptr[cpu];
  594. #ifndef CONFIG_64BIT
  595. if (MACHINE_HAS_IEEE)
  596. free_page((unsigned long) lowcore->extended_save_area_addr);
  597. #endif
  598. free_page(lowcore->panic_stack - PAGE_SIZE);
  599. free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
  600. free_pages((unsigned long) lowcore, lc_order);
  601. lowcore_ptr[cpu] = NULL;
  602. }
  603. #endif /* CONFIG_HOTPLUG_CPU */
  604. /* Upping and downing of CPUs */
  605. int __cpuinit __cpu_up(unsigned int cpu)
  606. {
  607. struct task_struct *idle;
  608. struct _lowcore *cpu_lowcore;
  609. struct stack_frame *sf;
  610. sigp_ccode ccode;
  611. if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
  612. return -EIO;
  613. if (smp_alloc_lowcore(cpu))
  614. return -ENOMEM;
  615. ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
  616. cpu, sigp_set_prefix);
  617. if (ccode) {
  618. printk("sigp_set_prefix failed for cpu %d "
  619. "with condition code %d\n",
  620. (int) cpu, (int) ccode);
  621. return -EIO;
  622. }
  623. idle = current_set[cpu];
  624. cpu_lowcore = lowcore_ptr[cpu];
  625. cpu_lowcore->kernel_stack = (unsigned long)
  626. task_stack_page(idle) + THREAD_SIZE;
  627. cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
  628. sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
  629. - sizeof(struct pt_regs)
  630. - sizeof(struct stack_frame));
  631. memset(sf, 0, sizeof(struct stack_frame));
  632. sf->gprs[9] = (unsigned long) sf;
  633. cpu_lowcore->save_area[15] = (unsigned long) sf;
  634. __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
  635. asm volatile(
  636. " stam 0,15,0(%0)"
  637. : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
  638. cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
  639. cpu_lowcore->current_task = (unsigned long) idle;
  640. cpu_lowcore->cpu_data.cpu_nr = cpu;
  641. cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
  642. cpu_lowcore->ipl_device = S390_lowcore.ipl_device;
  643. eieio();
  644. while (signal_processor(cpu, sigp_restart) == sigp_busy)
  645. udelay(10);
  646. while (!cpu_online(cpu))
  647. cpu_relax();
  648. return 0;
  649. }
  650. static int __init setup_possible_cpus(char *s)
  651. {
  652. int pcpus, cpu;
  653. pcpus = simple_strtoul(s, NULL, 0);
  654. cpu_possible_map = cpumask_of_cpu(0);
  655. for (cpu = 1; cpu < pcpus && cpu < NR_CPUS; cpu++)
  656. cpu_set(cpu, cpu_possible_map);
  657. return 0;
  658. }
  659. early_param("possible_cpus", setup_possible_cpus);
  660. #ifdef CONFIG_HOTPLUG_CPU
  661. int __cpu_disable(void)
  662. {
  663. struct ec_creg_mask_parms cr_parms;
  664. int cpu = smp_processor_id();
  665. cpu_clear(cpu, cpu_online_map);
  666. /* Disable pfault pseudo page faults on this cpu. */
  667. pfault_fini();
  668. memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
  669. memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
  670. /* disable all external interrupts */
  671. cr_parms.orvals[0] = 0;
  672. cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
  673. 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
  674. /* disable all I/O interrupts */
  675. cr_parms.orvals[6] = 0;
  676. cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
  677. 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
  678. /* disable most machine checks */
  679. cr_parms.orvals[14] = 0;
  680. cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
  681. 1 << 25 | 1 << 24);
  682. smp_ctl_bit_callback(&cr_parms);
  683. return 0;
  684. }
  685. void __cpu_die(unsigned int cpu)
  686. {
  687. /* Wait until target cpu is down */
  688. while (!smp_cpu_not_running(cpu))
  689. cpu_relax();
  690. smp_free_lowcore(cpu);
  691. printk(KERN_INFO "Processor %d spun down\n", cpu);
  692. }
  693. void cpu_die(void)
  694. {
  695. idle_task_exit();
  696. signal_processor(smp_processor_id(), sigp_stop);
  697. BUG();
  698. for (;;);
  699. }
  700. #endif /* CONFIG_HOTPLUG_CPU */
  701. void __init smp_prepare_cpus(unsigned int max_cpus)
  702. {
  703. #ifndef CONFIG_64BIT
  704. unsigned long save_area = 0;
  705. #endif
  706. unsigned long async_stack, panic_stack;
  707. struct _lowcore *lowcore;
  708. unsigned int cpu;
  709. int lc_order;
  710. smp_detect_cpus();
  711. /* request the 0x1201 emergency signal external interrupt */
  712. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  713. panic("Couldn't request external interrupt 0x1201");
  714. print_cpu_info(&S390_lowcore.cpu_data);
  715. /* Reallocate current lowcore, but keep its contents. */
  716. lc_order = sizeof(long) == 8 ? 1 : 0;
  717. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, lc_order);
  718. panic_stack = __get_free_page(GFP_KERNEL);
  719. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  720. #ifndef CONFIG_64BIT
  721. if (MACHINE_HAS_IEEE)
  722. save_area = get_zeroed_page(GFP_KERNEL);
  723. #endif
  724. local_irq_disable();
  725. local_mcck_disable();
  726. lowcore_ptr[smp_processor_id()] = lowcore;
  727. *lowcore = S390_lowcore;
  728. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  729. lowcore->async_stack = async_stack + ASYNC_SIZE;
  730. #ifndef CONFIG_64BIT
  731. if (MACHINE_HAS_IEEE)
  732. lowcore->extended_save_area_addr = (u32) save_area;
  733. #endif
  734. set_prefix((u32)(unsigned long) lowcore);
  735. local_mcck_enable();
  736. local_irq_enable();
  737. for_each_possible_cpu(cpu)
  738. if (cpu != smp_processor_id())
  739. smp_create_idle(cpu);
  740. }
  741. void __init smp_prepare_boot_cpu(void)
  742. {
  743. BUG_ON(smp_processor_id() != 0);
  744. current_thread_info()->cpu = 0;
  745. cpu_set(0, cpu_present_map);
  746. cpu_set(0, cpu_online_map);
  747. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  748. current_set[0] = current;
  749. smp_cpu_state[0] = CPU_STATE_CONFIGURED;
  750. spin_lock_init(&(&__get_cpu_var(s390_idle))->lock);
  751. }
  752. void __init smp_cpus_done(unsigned int max_cpus)
  753. {
  754. }
  755. /*
  756. * the frequency of the profiling timer can be changed
  757. * by writing a multiplier value into /proc/profile.
  758. *
  759. * usually you want to run this on all CPUs ;)
  760. */
  761. int setup_profiling_timer(unsigned int multiplier)
  762. {
  763. return 0;
  764. }
  765. #ifdef CONFIG_HOTPLUG_CPU
  766. static ssize_t cpu_configure_show(struct sys_device *dev, char *buf)
  767. {
  768. ssize_t count;
  769. mutex_lock(&smp_cpu_state_mutex);
  770. count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
  771. mutex_unlock(&smp_cpu_state_mutex);
  772. return count;
  773. }
  774. static ssize_t cpu_configure_store(struct sys_device *dev, const char *buf,
  775. size_t count)
  776. {
  777. int cpu = dev->id;
  778. int val, rc;
  779. char delim;
  780. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  781. return -EINVAL;
  782. if (val != 0 && val != 1)
  783. return -EINVAL;
  784. mutex_lock(&smp_cpu_state_mutex);
  785. get_online_cpus();
  786. rc = -EBUSY;
  787. if (cpu_online(cpu))
  788. goto out;
  789. rc = 0;
  790. switch (val) {
  791. case 0:
  792. if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
  793. rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
  794. if (!rc)
  795. smp_cpu_state[cpu] = CPU_STATE_STANDBY;
  796. }
  797. break;
  798. case 1:
  799. if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
  800. rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
  801. if (!rc)
  802. smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
  803. }
  804. break;
  805. default:
  806. break;
  807. }
  808. out:
  809. put_online_cpus();
  810. mutex_unlock(&smp_cpu_state_mutex);
  811. return rc ? rc : count;
  812. }
  813. static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  814. #endif /* CONFIG_HOTPLUG_CPU */
  815. static ssize_t show_cpu_address(struct sys_device *dev, char *buf)
  816. {
  817. return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
  818. }
  819. static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
  820. static struct attribute *cpu_common_attrs[] = {
  821. #ifdef CONFIG_HOTPLUG_CPU
  822. &attr_configure.attr,
  823. #endif
  824. &attr_address.attr,
  825. NULL,
  826. };
  827. static struct attribute_group cpu_common_attr_group = {
  828. .attrs = cpu_common_attrs,
  829. };
  830. static ssize_t show_capability(struct sys_device *dev, char *buf)
  831. {
  832. unsigned int capability;
  833. int rc;
  834. rc = get_cpu_capability(&capability);
  835. if (rc)
  836. return rc;
  837. return sprintf(buf, "%u\n", capability);
  838. }
  839. static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
  840. static ssize_t show_idle_count(struct sys_device *dev, char *buf)
  841. {
  842. struct s390_idle_data *idle;
  843. unsigned long long idle_count;
  844. idle = &per_cpu(s390_idle, dev->id);
  845. spin_lock_irq(&idle->lock);
  846. idle_count = idle->idle_count;
  847. spin_unlock_irq(&idle->lock);
  848. return sprintf(buf, "%llu\n", idle_count);
  849. }
  850. static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
  851. static ssize_t show_idle_time(struct sys_device *dev, char *buf)
  852. {
  853. struct s390_idle_data *idle;
  854. unsigned long long new_time;
  855. idle = &per_cpu(s390_idle, dev->id);
  856. spin_lock_irq(&idle->lock);
  857. if (idle->in_idle) {
  858. new_time = get_clock();
  859. idle->idle_time += new_time - idle->idle_enter;
  860. idle->idle_enter = new_time;
  861. }
  862. new_time = idle->idle_time;
  863. spin_unlock_irq(&idle->lock);
  864. return sprintf(buf, "%llu\n", new_time >> 12);
  865. }
  866. static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  867. static struct attribute *cpu_online_attrs[] = {
  868. &attr_capability.attr,
  869. &attr_idle_count.attr,
  870. &attr_idle_time_us.attr,
  871. NULL,
  872. };
  873. static struct attribute_group cpu_online_attr_group = {
  874. .attrs = cpu_online_attrs,
  875. };
  876. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  877. unsigned long action, void *hcpu)
  878. {
  879. unsigned int cpu = (unsigned int)(long)hcpu;
  880. struct cpu *c = &per_cpu(cpu_devices, cpu);
  881. struct sys_device *s = &c->sysdev;
  882. struct s390_idle_data *idle;
  883. switch (action) {
  884. case CPU_ONLINE:
  885. case CPU_ONLINE_FROZEN:
  886. idle = &per_cpu(s390_idle, cpu);
  887. spin_lock_irq(&idle->lock);
  888. idle->idle_enter = 0;
  889. idle->idle_time = 0;
  890. idle->idle_count = 0;
  891. spin_unlock_irq(&idle->lock);
  892. if (sysfs_create_group(&s->kobj, &cpu_online_attr_group))
  893. return NOTIFY_BAD;
  894. break;
  895. case CPU_DEAD:
  896. case CPU_DEAD_FROZEN:
  897. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  898. break;
  899. }
  900. return NOTIFY_OK;
  901. }
  902. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  903. .notifier_call = smp_cpu_notify,
  904. };
  905. static int __devinit smp_add_present_cpu(int cpu)
  906. {
  907. struct cpu *c = &per_cpu(cpu_devices, cpu);
  908. struct sys_device *s = &c->sysdev;
  909. int rc;
  910. c->hotpluggable = 1;
  911. rc = register_cpu(c, cpu);
  912. if (rc)
  913. goto out;
  914. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  915. if (rc)
  916. goto out_cpu;
  917. if (!cpu_online(cpu))
  918. goto out;
  919. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  920. if (!rc)
  921. return 0;
  922. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  923. out_cpu:
  924. #ifdef CONFIG_HOTPLUG_CPU
  925. unregister_cpu(c);
  926. #endif
  927. out:
  928. return rc;
  929. }
  930. #ifdef CONFIG_HOTPLUG_CPU
  931. static ssize_t __ref rescan_store(struct sys_device *dev,
  932. const char *buf, size_t count)
  933. {
  934. cpumask_t newcpus;
  935. int cpu;
  936. int rc;
  937. mutex_lock(&smp_cpu_state_mutex);
  938. get_online_cpus();
  939. newcpus = cpu_present_map;
  940. rc = smp_rescan_cpus();
  941. if (rc)
  942. goto out;
  943. cpus_andnot(newcpus, cpu_present_map, newcpus);
  944. for_each_cpu_mask(cpu, newcpus) {
  945. rc = smp_add_present_cpu(cpu);
  946. if (rc)
  947. cpu_clear(cpu, cpu_present_map);
  948. }
  949. rc = 0;
  950. out:
  951. put_online_cpus();
  952. mutex_unlock(&smp_cpu_state_mutex);
  953. return rc ? rc : count;
  954. }
  955. static SYSDEV_ATTR(rescan, 0200, NULL, rescan_store);
  956. #endif /* CONFIG_HOTPLUG_CPU */
  957. static int __init topology_init(void)
  958. {
  959. int cpu;
  960. int rc;
  961. register_cpu_notifier(&smp_cpu_nb);
  962. #ifdef CONFIG_HOTPLUG_CPU
  963. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  964. &attr_rescan.attr);
  965. if (rc)
  966. return rc;
  967. #endif
  968. for_each_present_cpu(cpu) {
  969. rc = smp_add_present_cpu(cpu);
  970. if (rc)
  971. return rc;
  972. }
  973. return 0;
  974. }
  975. subsys_initcall(topology_init);