smp.c 29 KB

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