smp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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
  202. smp_call_function_mask(cpumask_t mask,
  203. void (*func)(void *), void *info,
  204. int wait)
  205. {
  206. preempt_disable();
  207. __smp_call_function_map(func, info, 0, wait, mask);
  208. preempt_enable();
  209. return 0;
  210. }
  211. EXPORT_SYMBOL(smp_call_function_mask);
  212. void smp_send_stop(void)
  213. {
  214. int cpu, rc;
  215. /* Disable all interrupts/machine checks */
  216. __load_psw_mask(psw_kernel_bits & ~PSW_MASK_MCHECK);
  217. /* write magic number to zero page (absolute 0) */
  218. lowcore_ptr[smp_processor_id()]->panic_magic = __PANIC_MAGIC;
  219. /* stop all processors */
  220. for_each_online_cpu(cpu) {
  221. if (cpu == smp_processor_id())
  222. continue;
  223. do {
  224. rc = signal_processor(cpu, sigp_stop);
  225. } while (rc == sigp_busy);
  226. while (!smp_cpu_not_running(cpu))
  227. cpu_relax();
  228. }
  229. }
  230. /*
  231. * This is the main routine where commands issued by other
  232. * cpus are handled.
  233. */
  234. static void do_ext_call_interrupt(__u16 code)
  235. {
  236. unsigned long bits;
  237. /*
  238. * handle bit signal external calls
  239. *
  240. * For the ec_schedule signal we have to do nothing. All the work
  241. * is done automatically when we return from the interrupt.
  242. */
  243. bits = xchg(&S390_lowcore.ext_call_fast, 0);
  244. if (test_bit(ec_call_function, &bits))
  245. do_call_function();
  246. }
  247. /*
  248. * Send an external call sigp to another cpu and return without waiting
  249. * for its completion.
  250. */
  251. static void smp_ext_bitcall(int cpu, ec_bit_sig sig)
  252. {
  253. /*
  254. * Set signaling bit in lowcore of target cpu and kick it
  255. */
  256. set_bit(sig, (unsigned long *) &lowcore_ptr[cpu]->ext_call_fast);
  257. while (signal_processor(cpu, sigp_emergency_signal) == sigp_busy)
  258. udelay(10);
  259. }
  260. #ifndef CONFIG_64BIT
  261. /*
  262. * this function sends a 'purge tlb' signal to another CPU.
  263. */
  264. void smp_ptlb_callback(void *info)
  265. {
  266. __tlb_flush_local();
  267. }
  268. void smp_ptlb_all(void)
  269. {
  270. on_each_cpu(smp_ptlb_callback, NULL, 0, 1);
  271. }
  272. EXPORT_SYMBOL(smp_ptlb_all);
  273. #endif /* ! CONFIG_64BIT */
  274. /*
  275. * this function sends a 'reschedule' IPI to another CPU.
  276. * it goes straight through and wastes no time serializing
  277. * anything. Worst case is that we lose a reschedule ...
  278. */
  279. void smp_send_reschedule(int cpu)
  280. {
  281. smp_ext_bitcall(cpu, ec_schedule);
  282. }
  283. /*
  284. * parameter area for the set/clear control bit callbacks
  285. */
  286. struct ec_creg_mask_parms {
  287. unsigned long orvals[16];
  288. unsigned long andvals[16];
  289. };
  290. /*
  291. * callback for setting/clearing control bits
  292. */
  293. static void smp_ctl_bit_callback(void *info)
  294. {
  295. struct ec_creg_mask_parms *pp = info;
  296. unsigned long cregs[16];
  297. int i;
  298. __ctl_store(cregs, 0, 15);
  299. for (i = 0; i <= 15; i++)
  300. cregs[i] = (cregs[i] & pp->andvals[i]) | pp->orvals[i];
  301. __ctl_load(cregs, 0, 15);
  302. }
  303. /*
  304. * Set a bit in a control register of all cpus
  305. */
  306. void smp_ctl_set_bit(int cr, int bit)
  307. {
  308. struct ec_creg_mask_parms parms;
  309. memset(&parms.orvals, 0, sizeof(parms.orvals));
  310. memset(&parms.andvals, 0xff, sizeof(parms.andvals));
  311. parms.orvals[cr] = 1 << bit;
  312. on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
  313. }
  314. EXPORT_SYMBOL(smp_ctl_set_bit);
  315. /*
  316. * Clear a bit in a control register of all cpus
  317. */
  318. void smp_ctl_clear_bit(int cr, int bit)
  319. {
  320. struct ec_creg_mask_parms parms;
  321. memset(&parms.orvals, 0, sizeof(parms.orvals));
  322. memset(&parms.andvals, 0xff, sizeof(parms.andvals));
  323. parms.andvals[cr] = ~(1L << bit);
  324. on_each_cpu(smp_ctl_bit_callback, &parms, 0, 1);
  325. }
  326. EXPORT_SYMBOL(smp_ctl_clear_bit);
  327. /*
  328. * In early ipl state a temp. logically cpu number is needed, so the sigp
  329. * functions can be used to sense other cpus. Since NR_CPUS is >= 2 on
  330. * CONFIG_SMP and the ipl cpu is logical cpu 0, it must be 1.
  331. */
  332. #define CPU_INIT_NO 1
  333. #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_ZFCPDUMP_MODULE)
  334. /*
  335. * zfcpdump_prefix_array holds prefix registers for the following scenario:
  336. * 64 bit zfcpdump kernel and 31 bit kernel which is to be dumped. We have to
  337. * save its prefix registers, since they get lost, when switching from 31 bit
  338. * to 64 bit.
  339. */
  340. unsigned int zfcpdump_prefix_array[NR_CPUS + 1] \
  341. __attribute__((__section__(".data")));
  342. static void __init smp_get_save_area(unsigned int cpu, unsigned int phy_cpu)
  343. {
  344. if (ipl_info.type != IPL_TYPE_FCP_DUMP)
  345. return;
  346. if (cpu >= NR_CPUS) {
  347. printk(KERN_WARNING "Registers for cpu %i not saved since dump "
  348. "kernel was compiled with NR_CPUS=%i\n", cpu, NR_CPUS);
  349. return;
  350. }
  351. zfcpdump_save_areas[cpu] = kmalloc(sizeof(union save_area), GFP_KERNEL);
  352. __cpu_logical_map[CPU_INIT_NO] = (__u16) phy_cpu;
  353. while (signal_processor(CPU_INIT_NO, sigp_stop_and_store_status) ==
  354. sigp_busy)
  355. cpu_relax();
  356. memcpy(zfcpdump_save_areas[cpu],
  357. (void *)(unsigned long) store_prefix() + SAVE_AREA_BASE,
  358. SAVE_AREA_SIZE);
  359. #ifdef CONFIG_64BIT
  360. /* copy original prefix register */
  361. zfcpdump_save_areas[cpu]->s390x.pref_reg = zfcpdump_prefix_array[cpu];
  362. #endif
  363. }
  364. union save_area *zfcpdump_save_areas[NR_CPUS + 1];
  365. EXPORT_SYMBOL_GPL(zfcpdump_save_areas);
  366. #else
  367. static inline void smp_get_save_area(unsigned int cpu, unsigned int phy_cpu) { }
  368. #endif /* CONFIG_ZFCPDUMP || CONFIG_ZFCPDUMP_MODULE */
  369. static int cpu_stopped(int cpu)
  370. {
  371. __u32 status;
  372. /* Check for stopped state */
  373. if (signal_processor_ps(&status, 0, cpu, sigp_sense) ==
  374. sigp_status_stored) {
  375. if (status & 0x40)
  376. return 1;
  377. }
  378. return 0;
  379. }
  380. static int cpu_known(int cpu_id)
  381. {
  382. int cpu;
  383. for_each_present_cpu(cpu) {
  384. if (__cpu_logical_map[cpu] == cpu_id)
  385. return 1;
  386. }
  387. return 0;
  388. }
  389. static int smp_rescan_cpus_sigp(cpumask_t avail)
  390. {
  391. int cpu_id, logical_cpu;
  392. logical_cpu = first_cpu(avail);
  393. if (logical_cpu == NR_CPUS)
  394. return 0;
  395. for (cpu_id = 0; cpu_id <= 65535; cpu_id++) {
  396. if (cpu_known(cpu_id))
  397. continue;
  398. __cpu_logical_map[logical_cpu] = cpu_id;
  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. cpu_set(logical_cpu, cpu_present_map);
  431. if (cpu >= info->configured)
  432. smp_cpu_state[logical_cpu] = CPU_STATE_STANDBY;
  433. else
  434. smp_cpu_state[logical_cpu] = CPU_STATE_CONFIGURED;
  435. logical_cpu = next_cpu(logical_cpu, avail);
  436. if (logical_cpu == NR_CPUS)
  437. break;
  438. }
  439. out:
  440. kfree(info);
  441. return rc;
  442. }
  443. static int smp_rescan_cpus(void)
  444. {
  445. cpumask_t avail;
  446. cpus_xor(avail, cpu_possible_map, cpu_present_map);
  447. if (smp_use_sigp_detection)
  448. return smp_rescan_cpus_sigp(avail);
  449. else
  450. return smp_rescan_cpus_sclp(avail);
  451. }
  452. static void __init smp_detect_cpus(void)
  453. {
  454. unsigned int cpu, c_cpus, s_cpus;
  455. struct sclp_cpu_info *info;
  456. u16 boot_cpu_addr, cpu_addr;
  457. c_cpus = 1;
  458. s_cpus = 0;
  459. boot_cpu_addr = S390_lowcore.cpu_data.cpu_addr;
  460. info = kmalloc(sizeof(*info), GFP_KERNEL);
  461. if (!info)
  462. panic("smp_detect_cpus failed to allocate memory\n");
  463. /* Use sigp detection algorithm if sclp doesn't work. */
  464. if (sclp_get_cpu_info(info)) {
  465. smp_use_sigp_detection = 1;
  466. for (cpu = 0; cpu <= 65535; cpu++) {
  467. if (cpu == boot_cpu_addr)
  468. continue;
  469. __cpu_logical_map[CPU_INIT_NO] = cpu;
  470. if (!cpu_stopped(CPU_INIT_NO))
  471. continue;
  472. smp_get_save_area(c_cpus, cpu);
  473. c_cpus++;
  474. }
  475. goto out;
  476. }
  477. if (info->has_cpu_type) {
  478. for (cpu = 0; cpu < info->combined; cpu++) {
  479. if (info->cpu[cpu].address == boot_cpu_addr) {
  480. smp_cpu_type = info->cpu[cpu].type;
  481. break;
  482. }
  483. }
  484. }
  485. for (cpu = 0; cpu < info->combined; cpu++) {
  486. if (info->has_cpu_type && info->cpu[cpu].type != smp_cpu_type)
  487. continue;
  488. cpu_addr = info->cpu[cpu].address;
  489. if (cpu_addr == boot_cpu_addr)
  490. continue;
  491. __cpu_logical_map[CPU_INIT_NO] = cpu_addr;
  492. if (!cpu_stopped(CPU_INIT_NO)) {
  493. s_cpus++;
  494. continue;
  495. }
  496. smp_get_save_area(c_cpus, cpu_addr);
  497. c_cpus++;
  498. }
  499. out:
  500. kfree(info);
  501. printk(KERN_INFO "CPUs: %d configured, %d standby\n", c_cpus, s_cpus);
  502. get_online_cpus();
  503. smp_rescan_cpus();
  504. put_online_cpus();
  505. }
  506. /*
  507. * Activate a secondary processor.
  508. */
  509. int __cpuinit start_secondary(void *cpuvoid)
  510. {
  511. /* Setup the cpu */
  512. cpu_init();
  513. preempt_disable();
  514. /* Enable TOD clock interrupts on the secondary cpu. */
  515. init_cpu_timer();
  516. #ifdef CONFIG_VIRT_TIMER
  517. /* Enable cpu timer interrupts on the secondary cpu. */
  518. init_cpu_vtimer();
  519. #endif
  520. /* Enable pfault pseudo page faults on this cpu. */
  521. pfault_init();
  522. /* Mark this cpu as online */
  523. cpu_set(smp_processor_id(), cpu_online_map);
  524. /* Switch on interrupts */
  525. local_irq_enable();
  526. /* Print info about this processor */
  527. print_cpu_info(&S390_lowcore.cpu_data);
  528. /* cpu_idle will call schedule for us */
  529. cpu_idle();
  530. return 0;
  531. }
  532. static void __init smp_create_idle(unsigned int cpu)
  533. {
  534. struct task_struct *p;
  535. /*
  536. * don't care about the psw and regs settings since we'll never
  537. * reschedule the forked task.
  538. */
  539. p = fork_idle(cpu);
  540. if (IS_ERR(p))
  541. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  542. current_set[cpu] = p;
  543. spin_lock_init(&(&per_cpu(s390_idle, cpu))->lock);
  544. }
  545. static int __cpuinit smp_alloc_lowcore(int cpu)
  546. {
  547. unsigned long async_stack, panic_stack;
  548. struct _lowcore *lowcore;
  549. int lc_order;
  550. lc_order = sizeof(long) == 8 ? 1 : 0;
  551. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, lc_order);
  552. if (!lowcore)
  553. return -ENOMEM;
  554. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  555. if (!async_stack)
  556. goto out_async_stack;
  557. panic_stack = __get_free_page(GFP_KERNEL);
  558. if (!panic_stack)
  559. goto out_panic_stack;
  560. *lowcore = S390_lowcore;
  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_panic_stack:
  579. free_pages(async_stack, ASYNC_ORDER);
  580. out_async_stack:
  581. free_pages((unsigned long) lowcore, lc_order);
  582. return -ENOMEM;
  583. }
  584. #ifdef CONFIG_HOTPLUG_CPU
  585. static void smp_free_lowcore(int cpu)
  586. {
  587. struct _lowcore *lowcore;
  588. int lc_order;
  589. lc_order = sizeof(long) == 8 ? 1 : 0;
  590. lowcore = lowcore_ptr[cpu];
  591. #ifndef CONFIG_64BIT
  592. if (MACHINE_HAS_IEEE)
  593. free_page((unsigned long) lowcore->extended_save_area_addr);
  594. #endif
  595. free_page(lowcore->panic_stack - PAGE_SIZE);
  596. free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
  597. free_pages((unsigned long) lowcore, lc_order);
  598. lowcore_ptr[cpu] = NULL;
  599. }
  600. #endif /* CONFIG_HOTPLUG_CPU */
  601. /* Upping and downing of CPUs */
  602. int __cpuinit __cpu_up(unsigned int cpu)
  603. {
  604. struct task_struct *idle;
  605. struct _lowcore *cpu_lowcore;
  606. struct stack_frame *sf;
  607. sigp_ccode ccode;
  608. if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
  609. return -EIO;
  610. if (smp_alloc_lowcore(cpu))
  611. return -ENOMEM;
  612. ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
  613. cpu, sigp_set_prefix);
  614. if (ccode) {
  615. printk("sigp_set_prefix failed for cpu %d "
  616. "with condition code %d\n",
  617. (int) cpu, (int) ccode);
  618. return -EIO;
  619. }
  620. idle = current_set[cpu];
  621. cpu_lowcore = lowcore_ptr[cpu];
  622. cpu_lowcore->kernel_stack = (unsigned long)
  623. task_stack_page(idle) + THREAD_SIZE;
  624. cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
  625. sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
  626. - sizeof(struct pt_regs)
  627. - sizeof(struct stack_frame));
  628. memset(sf, 0, sizeof(struct stack_frame));
  629. sf->gprs[9] = (unsigned long) sf;
  630. cpu_lowcore->save_area[15] = (unsigned long) sf;
  631. __ctl_store(cpu_lowcore->cregs_save_area[0], 0, 15);
  632. asm volatile(
  633. " stam 0,15,0(%0)"
  634. : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
  635. cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
  636. cpu_lowcore->current_task = (unsigned long) idle;
  637. cpu_lowcore->cpu_data.cpu_nr = cpu;
  638. cpu_lowcore->softirq_pending = 0;
  639. cpu_lowcore->ext_call_fast = 0;
  640. eieio();
  641. while (signal_processor(cpu, sigp_restart) == sigp_busy)
  642. udelay(10);
  643. while (!cpu_online(cpu))
  644. cpu_relax();
  645. return 0;
  646. }
  647. static int __init setup_possible_cpus(char *s)
  648. {
  649. int pcpus, cpu;
  650. pcpus = simple_strtoul(s, NULL, 0);
  651. cpu_possible_map = cpumask_of_cpu(0);
  652. for (cpu = 1; cpu < pcpus && cpu < NR_CPUS; cpu++)
  653. cpu_set(cpu, cpu_possible_map);
  654. return 0;
  655. }
  656. early_param("possible_cpus", setup_possible_cpus);
  657. #ifdef CONFIG_HOTPLUG_CPU
  658. int __cpu_disable(void)
  659. {
  660. struct ec_creg_mask_parms cr_parms;
  661. int cpu = smp_processor_id();
  662. cpu_clear(cpu, cpu_online_map);
  663. /* Disable pfault pseudo page faults on this cpu. */
  664. pfault_fini();
  665. memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
  666. memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
  667. /* disable all external interrupts */
  668. cr_parms.orvals[0] = 0;
  669. cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
  670. 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
  671. /* disable all I/O interrupts */
  672. cr_parms.orvals[6] = 0;
  673. cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
  674. 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
  675. /* disable most machine checks */
  676. cr_parms.orvals[14] = 0;
  677. cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
  678. 1 << 25 | 1 << 24);
  679. smp_ctl_bit_callback(&cr_parms);
  680. return 0;
  681. }
  682. void __cpu_die(unsigned int cpu)
  683. {
  684. /* Wait until target cpu is down */
  685. while (!smp_cpu_not_running(cpu))
  686. cpu_relax();
  687. smp_free_lowcore(cpu);
  688. printk(KERN_INFO "Processor %d spun down\n", cpu);
  689. }
  690. void cpu_die(void)
  691. {
  692. idle_task_exit();
  693. signal_processor(smp_processor_id(), sigp_stop);
  694. BUG();
  695. for (;;);
  696. }
  697. #endif /* CONFIG_HOTPLUG_CPU */
  698. void __init smp_prepare_cpus(unsigned int max_cpus)
  699. {
  700. unsigned int cpu;
  701. smp_detect_cpus();
  702. /* request the 0x1201 emergency signal external interrupt */
  703. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  704. panic("Couldn't request external interrupt 0x1201");
  705. memset(lowcore_ptr, 0, sizeof(lowcore_ptr));
  706. print_cpu_info(&S390_lowcore.cpu_data);
  707. smp_alloc_lowcore(smp_processor_id());
  708. #ifndef CONFIG_64BIT
  709. if (MACHINE_HAS_IEEE)
  710. ctl_set_bit(14, 29); /* enable extended save area */
  711. #endif
  712. set_prefix((u32)(unsigned long) lowcore_ptr[smp_processor_id()]);
  713. for_each_possible_cpu(cpu)
  714. if (cpu != smp_processor_id())
  715. smp_create_idle(cpu);
  716. }
  717. void __init smp_prepare_boot_cpu(void)
  718. {
  719. BUG_ON(smp_processor_id() != 0);
  720. current_thread_info()->cpu = 0;
  721. cpu_set(0, cpu_present_map);
  722. cpu_set(0, cpu_online_map);
  723. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  724. current_set[0] = current;
  725. smp_cpu_state[0] = CPU_STATE_CONFIGURED;
  726. spin_lock_init(&(&__get_cpu_var(s390_idle))->lock);
  727. }
  728. void __init smp_cpus_done(unsigned int max_cpus)
  729. {
  730. }
  731. /*
  732. * the frequency of the profiling timer can be changed
  733. * by writing a multiplier value into /proc/profile.
  734. *
  735. * usually you want to run this on all CPUs ;)
  736. */
  737. int setup_profiling_timer(unsigned int multiplier)
  738. {
  739. return 0;
  740. }
  741. #ifdef CONFIG_HOTPLUG_CPU
  742. static ssize_t cpu_configure_show(struct sys_device *dev, char *buf)
  743. {
  744. ssize_t count;
  745. mutex_lock(&smp_cpu_state_mutex);
  746. count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
  747. mutex_unlock(&smp_cpu_state_mutex);
  748. return count;
  749. }
  750. static ssize_t cpu_configure_store(struct sys_device *dev, const char *buf,
  751. size_t count)
  752. {
  753. int cpu = dev->id;
  754. int val, rc;
  755. char delim;
  756. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  757. return -EINVAL;
  758. if (val != 0 && val != 1)
  759. return -EINVAL;
  760. mutex_lock(&smp_cpu_state_mutex);
  761. get_online_cpus();
  762. rc = -EBUSY;
  763. if (cpu_online(cpu))
  764. goto out;
  765. rc = 0;
  766. switch (val) {
  767. case 0:
  768. if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
  769. rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
  770. if (!rc)
  771. smp_cpu_state[cpu] = CPU_STATE_STANDBY;
  772. }
  773. break;
  774. case 1:
  775. if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
  776. rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
  777. if (!rc)
  778. smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
  779. }
  780. break;
  781. default:
  782. break;
  783. }
  784. out:
  785. put_online_cpus();
  786. mutex_unlock(&smp_cpu_state_mutex);
  787. return rc ? rc : count;
  788. }
  789. static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  790. #endif /* CONFIG_HOTPLUG_CPU */
  791. static ssize_t show_cpu_address(struct sys_device *dev, char *buf)
  792. {
  793. return sprintf(buf, "%d\n", __cpu_logical_map[dev->id]);
  794. }
  795. static SYSDEV_ATTR(address, 0444, show_cpu_address, NULL);
  796. static struct attribute *cpu_common_attrs[] = {
  797. #ifdef CONFIG_HOTPLUG_CPU
  798. &attr_configure.attr,
  799. #endif
  800. &attr_address.attr,
  801. NULL,
  802. };
  803. static struct attribute_group cpu_common_attr_group = {
  804. .attrs = cpu_common_attrs,
  805. };
  806. static ssize_t show_capability(struct sys_device *dev, char *buf)
  807. {
  808. unsigned int capability;
  809. int rc;
  810. rc = get_cpu_capability(&capability);
  811. if (rc)
  812. return rc;
  813. return sprintf(buf, "%u\n", capability);
  814. }
  815. static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
  816. static ssize_t show_idle_count(struct sys_device *dev, char *buf)
  817. {
  818. struct s390_idle_data *idle;
  819. unsigned long long idle_count;
  820. idle = &per_cpu(s390_idle, dev->id);
  821. spin_lock_irq(&idle->lock);
  822. idle_count = idle->idle_count;
  823. spin_unlock_irq(&idle->lock);
  824. return sprintf(buf, "%llu\n", idle_count);
  825. }
  826. static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
  827. static ssize_t show_idle_time(struct sys_device *dev, char *buf)
  828. {
  829. struct s390_idle_data *idle;
  830. unsigned long long new_time;
  831. idle = &per_cpu(s390_idle, dev->id);
  832. spin_lock_irq(&idle->lock);
  833. if (idle->in_idle) {
  834. new_time = get_clock();
  835. idle->idle_time += new_time - idle->idle_enter;
  836. idle->idle_enter = new_time;
  837. }
  838. new_time = idle->idle_time;
  839. spin_unlock_irq(&idle->lock);
  840. return sprintf(buf, "%llu\n", new_time >> 12);
  841. }
  842. static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  843. static struct attribute *cpu_online_attrs[] = {
  844. &attr_capability.attr,
  845. &attr_idle_count.attr,
  846. &attr_idle_time_us.attr,
  847. NULL,
  848. };
  849. static struct attribute_group cpu_online_attr_group = {
  850. .attrs = cpu_online_attrs,
  851. };
  852. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  853. unsigned long action, void *hcpu)
  854. {
  855. unsigned int cpu = (unsigned int)(long)hcpu;
  856. struct cpu *c = &per_cpu(cpu_devices, cpu);
  857. struct sys_device *s = &c->sysdev;
  858. struct s390_idle_data *idle;
  859. switch (action) {
  860. case CPU_ONLINE:
  861. case CPU_ONLINE_FROZEN:
  862. idle = &per_cpu(s390_idle, cpu);
  863. spin_lock_irq(&idle->lock);
  864. idle->idle_enter = 0;
  865. idle->idle_time = 0;
  866. idle->idle_count = 0;
  867. spin_unlock_irq(&idle->lock);
  868. if (sysfs_create_group(&s->kobj, &cpu_online_attr_group))
  869. return NOTIFY_BAD;
  870. break;
  871. case CPU_DEAD:
  872. case CPU_DEAD_FROZEN:
  873. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  874. break;
  875. }
  876. return NOTIFY_OK;
  877. }
  878. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  879. .notifier_call = smp_cpu_notify,
  880. };
  881. static int smp_add_present_cpu(int cpu)
  882. {
  883. struct cpu *c = &per_cpu(cpu_devices, cpu);
  884. struct sys_device *s = &c->sysdev;
  885. int rc;
  886. c->hotpluggable = 1;
  887. rc = register_cpu(c, cpu);
  888. if (rc)
  889. goto out;
  890. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  891. if (rc)
  892. goto out_cpu;
  893. if (!cpu_online(cpu))
  894. goto out;
  895. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  896. if (!rc)
  897. return 0;
  898. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  899. out_cpu:
  900. #ifdef CONFIG_HOTPLUG_CPU
  901. unregister_cpu(c);
  902. #endif
  903. out:
  904. return rc;
  905. }
  906. #ifdef CONFIG_HOTPLUG_CPU
  907. static ssize_t rescan_store(struct sys_device *dev, const char *buf,
  908. size_t count)
  909. {
  910. cpumask_t newcpus;
  911. int cpu;
  912. int rc;
  913. mutex_lock(&smp_cpu_state_mutex);
  914. get_online_cpus();
  915. newcpus = cpu_present_map;
  916. rc = smp_rescan_cpus();
  917. if (rc)
  918. goto out;
  919. cpus_andnot(newcpus, cpu_present_map, newcpus);
  920. for_each_cpu_mask(cpu, newcpus) {
  921. rc = smp_add_present_cpu(cpu);
  922. if (rc)
  923. cpu_clear(cpu, cpu_present_map);
  924. }
  925. rc = 0;
  926. out:
  927. put_online_cpus();
  928. mutex_unlock(&smp_cpu_state_mutex);
  929. return rc ? rc : count;
  930. }
  931. static SYSDEV_ATTR(rescan, 0200, NULL, rescan_store);
  932. #endif /* CONFIG_HOTPLUG_CPU */
  933. static int __init topology_init(void)
  934. {
  935. int cpu;
  936. int rc;
  937. register_cpu_notifier(&smp_cpu_nb);
  938. #ifdef CONFIG_HOTPLUG_CPU
  939. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  940. &attr_rescan.attr);
  941. if (rc)
  942. return rc;
  943. #endif
  944. for_each_present_cpu(cpu) {
  945. rc = smp_add_present_cpu(cpu);
  946. if (rc)
  947. return rc;
  948. }
  949. return 0;
  950. }
  951. subsys_initcall(topology_init);