smp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  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. 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. static 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. smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
  399. if (!cpu_stopped(logical_cpu))
  400. continue;
  401. cpu_set(logical_cpu, cpu_present_map);
  402. smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
  403. logical_cpu = next_cpu(logical_cpu, avail);
  404. if (logical_cpu == NR_CPUS)
  405. break;
  406. }
  407. return 0;
  408. }
  409. static int smp_rescan_cpus_sclp(cpumask_t avail)
  410. {
  411. struct sclp_cpu_info *info;
  412. int cpu_id, logical_cpu, cpu;
  413. int rc;
  414. logical_cpu = first_cpu(avail);
  415. if (logical_cpu == NR_CPUS)
  416. return 0;
  417. info = kmalloc(sizeof(*info), GFP_KERNEL);
  418. if (!info)
  419. return -ENOMEM;
  420. rc = sclp_get_cpu_info(info);
  421. if (rc)
  422. goto out;
  423. for (cpu = 0; cpu < info->combined; cpu++) {
  424. if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
  425. continue;
  426. cpu_id = info->cpu[cpu].address;
  427. if (cpu_known(cpu_id))
  428. continue;
  429. __cpu_logical_map[logical_cpu] = cpu_id;
  430. smp_cpu_polarization[logical_cpu] = POLARIZATION_UNKNWN;
  431. cpu_set(logical_cpu, cpu_present_map);
  432. if (cpu >= info->configured)
  433. smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
  434. else
  435. smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
  436. logical_cpu = next_cpu(logical_cpu, avail);
  437. if (logical_cpu == NR_CPUS)
  438. break;
  439. }
  440. out:
  441. kfree(info);
  442. return rc;
  443. }
  444. static int smp_rescan_cpus(void)
  445. {
  446. cpumask_t avail;
  447. cpus_xor(avail, cpu_possible_map, cpu_present_map);
  448. if (smp_use_sigp_detection)
  449. return smp_rescan_cpus_sigp(avail);
  450. else
  451. return smp_rescan_cpus_sclp(avail);
  452. }
  453. static void __init smp_detect_cpus(void)
  454. {
  455. unsigned int cpu, c_cpus, s_cpus;
  456. struct sclp_cpu_info *info;
  457. u16 boot_cpu_addr, cpu_addr;
  458. c_cpus = 1;
  459. s_cpus = 0;
  460. boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
  461. info = kmalloc(sizeof(*info), GFP_KERNEL);
  462. if (!info)
  463. panic("smp_detect_cpus failed to allocate memory\n");
  464. /* Use sigp detection algorithm if sclp doesn't work. */
  465. if (sclp_get_cpu_info(info)) {
  466. smp_use_sigp_detection = 1;
  467. for (cpu = 0; cpu <= 65535; cpu++) {
  468. if (cpu == boot_cpu_addr)
  469. continue;
  470. __cpu_logical_map[CPU_INIT_NO] = cpu;
  471. if (!cpu_stopped(CPU_INIT_NO))
  472. continue;
  473. smp_get_save_area(c_cpus, cpu);
  474. c_cpus++;
  475. }
  476. goto out;
  477. }
  478. if (info->has_cpu_type) {
  479. for (cpu = 0; cpu < info->combined; cpu++) {
  480. if (info->cpu[cpu].address == boot_cpu_addr) {
  481. smp_cpu_type = info->cpu[cpu].type;
  482. break;
  483. }
  484. }
  485. }
  486. for (cpu = 0; cpu < info->combined; cpu++) {
  487. if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
  488. continue;
  489. cpu_addr = info->cpu[cpu].address;
  490. if (cpu_addr == boot_cpu_addr)
  491. continue;
  492. __cpu_logical_map[CPU_INIT_NO] = cpu_addr;
  493. if (!cpu_stopped(CPU_INIT_NO)) {
  494. s_cpus++;
  495. continue;
  496. }
  497. smp_get_save_area(c_cpus, cpu_addr);
  498. c_cpus++;
  499. }
  500. out:
  501. kfree(info);
  502. printk(KERN_INFO "CPUs: %d configured, %d standby\n", c_cpus, s_cpus);
  503. get_online_cpus();
  504. smp_rescan_cpus();
  505. put_online_cpus();
  506. }
  507. /*
  508. * Activate a secondary processor.
  509. */
  510. int __cpuinit start_secondary(void *cpuvoid)
  511. {
  512. /* Setup the cpu */
  513. cpu_init();
  514. preempt_disable();
  515. /* Enable TOD clock interrupts on the secondary cpu. */
  516. init_cpu_timer();
  517. #ifdef CONFIG_VIRT_TIMER
  518. /* Enable cpu timer interrupts on the secondary cpu. */
  519. init_cpu_vtimer();
  520. #endif
  521. /* Enable pfault pseudo page faults on this cpu. */
  522. pfault_init();
  523. /* Mark this cpu as online */
  524. cpu_set(smp_processor_id(), cpu_online_map);
  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], 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. mutex_lock(&smp_cpu_state_mutex);
  782. get_online_cpus();
  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. put_online_cpus();
  811. mutex_unlock(&smp_cpu_state_mutex);
  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. static ssize_t __ref rescan_store(struct sys_device *dev,
  960. const char *buf, size_t count)
  961. {
  962. cpumask_t newcpus;
  963. int cpu;
  964. int rc;
  965. mutex_lock(&smp_cpu_state_mutex);
  966. get_online_cpus();
  967. newcpus = cpu_present_map;
  968. rc = smp_rescan_cpus();
  969. if (rc)
  970. goto out;
  971. cpus_andnot(newcpus, cpu_present_map, newcpus);
  972. for_each_cpu_mask(cpu, newcpus) {
  973. rc = smp_add_present_cpu(cpu);
  974. if (rc)
  975. cpu_clear(cpu, cpu_present_map);
  976. }
  977. rc = 0;
  978. out:
  979. put_online_cpus();
  980. mutex_unlock(&smp_cpu_state_mutex);
  981. if (!cpus_empty(newcpus))
  982. topology_schedule_update();
  983. return rc ? rc : count;
  984. }
  985. static SYSDEV_ATTR(rescan, 0200, NULL, rescan_store);
  986. #endif /* CONFIG_HOTPLUG_CPU */
  987. static ssize_t dispatching_show(struct sys_device *dev, char *buf)
  988. {
  989. ssize_t count;
  990. mutex_lock(&smp_cpu_state_mutex);
  991. count = sprintf(buf, "%d\n", cpu_management);
  992. mutex_unlock(&smp_cpu_state_mutex);
  993. return count;
  994. }
  995. static ssize_t dispatching_store(struct sys_device *dev, const char *buf,
  996. size_t count)
  997. {
  998. int val, rc;
  999. char delim;
  1000. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  1001. return -EINVAL;
  1002. if (val != 0 && val != 1)
  1003. return -EINVAL;
  1004. rc = 0;
  1005. mutex_lock(&smp_cpu_state_mutex);
  1006. get_online_cpus();
  1007. if (cpu_management == val)
  1008. goto out;
  1009. rc = topology_set_cpu_management(val);
  1010. if (!rc)
  1011. cpu_management = val;
  1012. out:
  1013. put_online_cpus();
  1014. mutex_unlock(&smp_cpu_state_mutex);
  1015. return rc ? rc : count;
  1016. }
  1017. static SYSDEV_ATTR(dispatching, 0644, dispatching_show, dispatching_store);
  1018. static int __init topology_init(void)
  1019. {
  1020. int cpu;
  1021. int rc;
  1022. register_cpu_notifier(&smp_cpu_nb);
  1023. #ifdef CONFIG_HOTPLUG_CPU
  1024. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  1025. &attr_rescan.attr);
  1026. if (rc)
  1027. return rc;
  1028. #endif
  1029. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  1030. &attr_dispatching.attr);
  1031. if (rc)
  1032. return rc;
  1033. for_each_present_cpu(cpu) {
  1034. rc = smp_add_present_cpu(cpu);
  1035. if (rc)
  1036. return rc;
  1037. }
  1038. return 0;
  1039. }
  1040. subsys_initcall(topology_init);