smp.c 29 KB

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