smp.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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. /* call cpu notifiers */
  518. notify_cpu_starting(smp_processor_id());
  519. /* Mark this cpu as online */
  520. spin_lock(&call_lock);
  521. cpu_set(smp_processor_id(), cpu_online_map);
  522. spin_unlock(&call_lock);
  523. /* Switch on interrupts */
  524. local_irq_enable();
  525. /* Print info about this processor */
  526. print_cpu_info(&S390_lowcore.cpu_data);
  527. /* cpu_idle will call schedule for us */
  528. cpu_idle();
  529. return 0;
  530. }
  531. static void __init smp_create_idle(unsigned int cpu)
  532. {
  533. struct task_struct *p;
  534. /*
  535. * don't care about the psw and regs settings since we'll never
  536. * reschedule the forked task.
  537. */
  538. p = fork_idle(cpu);
  539. if (IS_ERR(p))
  540. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  541. current_set[cpu] = p;
  542. }
  543. static int __cpuinit smp_alloc_lowcore(int cpu)
  544. {
  545. unsigned long async_stack, panic_stack;
  546. struct _lowcore *lowcore;
  547. int lc_order;
  548. lc_order = sizeof(long) == 8 ? 1 : 0;
  549. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, lc_order);
  550. if (!lowcore)
  551. return -ENOMEM;
  552. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  553. panic_stack = __get_free_page(GFP_KERNEL);
  554. if (!panic_stack || !async_stack)
  555. goto out;
  556. memcpy(lowcore, &S390_lowcore, 512);
  557. memset((char *)lowcore + 512, 0, sizeof(*lowcore) - 512);
  558. lowcore->async_stack = async_stack + ASYNC_SIZE;
  559. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  560. #ifndef CONFIG_64BIT
  561. if (MACHINE_HAS_IEEE) {
  562. unsigned long save_area;
  563. save_area = get_zeroed_page(GFP_KERNEL);
  564. if (!save_area)
  565. goto out_save_area;
  566. lowcore->extended_save_area_addr = (u32) save_area;
  567. }
  568. #endif
  569. lowcore_ptr[cpu] = lowcore;
  570. return 0;
  571. #ifndef CONFIG_64BIT
  572. out_save_area:
  573. free_page(panic_stack);
  574. #endif
  575. out:
  576. free_pages(async_stack, ASYNC_ORDER);
  577. free_pages((unsigned long) lowcore, lc_order);
  578. return -ENOMEM;
  579. }
  580. #ifdef CONFIG_HOTPLUG_CPU
  581. static void smp_free_lowcore(int cpu)
  582. {
  583. struct _lowcore *lowcore;
  584. int lc_order;
  585. lc_order = sizeof(long) == 8 ? 1 : 0;
  586. lowcore = lowcore_ptr[cpu];
  587. #ifndef CONFIG_64BIT
  588. if (MACHINE_HAS_IEEE)
  589. free_page((unsigned long) lowcore->extended_save_area_addr);
  590. #endif
  591. free_page(lowcore->panic_stack - PAGE_SIZE);
  592. free_pages(lowcore->async_stack - ASYNC_SIZE, ASYNC_ORDER);
  593. free_pages((unsigned long) lowcore, lc_order);
  594. lowcore_ptr[cpu] = NULL;
  595. }
  596. #endif /* CONFIG_HOTPLUG_CPU */
  597. /* Upping and downing of CPUs */
  598. int __cpuinit __cpu_up(unsigned int cpu)
  599. {
  600. struct task_struct *idle;
  601. struct _lowcore *cpu_lowcore;
  602. struct stack_frame *sf;
  603. sigp_ccode ccode;
  604. if (smp_cpu_state[cpu] != CPU_STATE_CONFIGURED)
  605. return -EIO;
  606. if (smp_alloc_lowcore(cpu))
  607. return -ENOMEM;
  608. ccode = signal_processor_p((__u32)(unsigned long)(lowcore_ptr[cpu]),
  609. cpu, sigp_set_prefix);
  610. if (ccode) {
  611. printk("sigp_set_prefix failed for cpu %d "
  612. "with condition code %d\n",
  613. (int) cpu, (int) ccode);
  614. return -EIO;
  615. }
  616. idle = current_set[cpu];
  617. cpu_lowcore = lowcore_ptr[cpu];
  618. cpu_lowcore->kernel_stack = (unsigned long)
  619. task_stack_page(idle) + THREAD_SIZE;
  620. cpu_lowcore->thread_info = (unsigned long) task_thread_info(idle);
  621. sf = (struct stack_frame *) (cpu_lowcore->kernel_stack
  622. - sizeof(struct pt_regs)
  623. - sizeof(struct stack_frame));
  624. memset(sf, 0, sizeof(struct stack_frame));
  625. sf->gprs[9] = (unsigned long) sf;
  626. cpu_lowcore->save_area[15] = (unsigned long) sf;
  627. __ctl_store(cpu_lowcore->cregs_save_area, 0, 15);
  628. asm volatile(
  629. " stam 0,15,0(%0)"
  630. : : "a" (&cpu_lowcore->access_regs_save_area) : "memory");
  631. cpu_lowcore->percpu_offset = __per_cpu_offset[cpu];
  632. cpu_lowcore->current_task = (unsigned long) idle;
  633. cpu_lowcore->cpu_data.cpu_nr = cpu;
  634. cpu_lowcore->kernel_asce = S390_lowcore.kernel_asce;
  635. cpu_lowcore->ipl_device = S390_lowcore.ipl_device;
  636. eieio();
  637. while (signal_processor(cpu, sigp_restart) == sigp_busy)
  638. udelay(10);
  639. while (!cpu_online(cpu))
  640. cpu_relax();
  641. return 0;
  642. }
  643. static int __init setup_possible_cpus(char *s)
  644. {
  645. int pcpus, cpu;
  646. pcpus = simple_strtoul(s, NULL, 0);
  647. cpu_possible_map = cpumask_of_cpu(0);
  648. for (cpu = 1; cpu < pcpus && cpu < NR_CPUS; cpu++)
  649. cpu_set(cpu, cpu_possible_map);
  650. return 0;
  651. }
  652. early_param("possible_cpus", setup_possible_cpus);
  653. #ifdef CONFIG_HOTPLUG_CPU
  654. int __cpu_disable(void)
  655. {
  656. struct ec_creg_mask_parms cr_parms;
  657. int cpu = smp_processor_id();
  658. cpu_clear(cpu, cpu_online_map);
  659. /* Disable pfault pseudo page faults on this cpu. */
  660. pfault_fini();
  661. memset(&cr_parms.orvals, 0, sizeof(cr_parms.orvals));
  662. memset(&cr_parms.andvals, 0xff, sizeof(cr_parms.andvals));
  663. /* disable all external interrupts */
  664. cr_parms.orvals[0] = 0;
  665. cr_parms.andvals[0] = ~(1 << 15 | 1 << 14 | 1 << 13 | 1 << 12 |
  666. 1 << 11 | 1 << 10 | 1 << 6 | 1 << 4);
  667. /* disable all I/O interrupts */
  668. cr_parms.orvals[6] = 0;
  669. cr_parms.andvals[6] = ~(1 << 31 | 1 << 30 | 1 << 29 | 1 << 28 |
  670. 1 << 27 | 1 << 26 | 1 << 25 | 1 << 24);
  671. /* disable most machine checks */
  672. cr_parms.orvals[14] = 0;
  673. cr_parms.andvals[14] = ~(1 << 28 | 1 << 27 | 1 << 26 |
  674. 1 << 25 | 1 << 24);
  675. smp_ctl_bit_callback(&cr_parms);
  676. return 0;
  677. }
  678. void __cpu_die(unsigned int cpu)
  679. {
  680. /* Wait until target cpu is down */
  681. while (!smp_cpu_not_running(cpu))
  682. cpu_relax();
  683. smp_free_lowcore(cpu);
  684. printk(KERN_INFO "Processor %d spun down\n", cpu);
  685. }
  686. void cpu_die(void)
  687. {
  688. idle_task_exit();
  689. signal_processor(smp_processor_id(), sigp_stop);
  690. BUG();
  691. for (;;);
  692. }
  693. #endif /* CONFIG_HOTPLUG_CPU */
  694. void __init smp_prepare_cpus(unsigned int max_cpus)
  695. {
  696. #ifndef CONFIG_64BIT
  697. unsigned long save_area = 0;
  698. #endif
  699. unsigned long async_stack, panic_stack;
  700. struct _lowcore *lowcore;
  701. unsigned int cpu;
  702. int lc_order;
  703. smp_detect_cpus();
  704. /* request the 0x1201 emergency signal external interrupt */
  705. if (register_external_interrupt(0x1201, do_ext_call_interrupt) != 0)
  706. panic("Couldn't request external interrupt 0x1201");
  707. print_cpu_info(&S390_lowcore.cpu_data);
  708. /* Reallocate current lowcore, but keep its contents. */
  709. lc_order = sizeof(long) == 8 ? 1 : 0;
  710. lowcore = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, lc_order);
  711. panic_stack = __get_free_page(GFP_KERNEL);
  712. async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  713. #ifndef CONFIG_64BIT
  714. if (MACHINE_HAS_IEEE)
  715. save_area = get_zeroed_page(GFP_KERNEL);
  716. #endif
  717. local_irq_disable();
  718. local_mcck_disable();
  719. lowcore_ptr[smp_processor_id()] = lowcore;
  720. *lowcore = S390_lowcore;
  721. lowcore->panic_stack = panic_stack + PAGE_SIZE;
  722. lowcore->async_stack = async_stack + ASYNC_SIZE;
  723. #ifndef CONFIG_64BIT
  724. if (MACHINE_HAS_IEEE)
  725. lowcore->extended_save_area_addr = (u32) save_area;
  726. #endif
  727. set_prefix((u32)(unsigned long) lowcore);
  728. local_mcck_enable();
  729. local_irq_enable();
  730. for_each_possible_cpu(cpu)
  731. if (cpu != smp_processor_id())
  732. smp_create_idle(cpu);
  733. }
  734. void __init smp_prepare_boot_cpu(void)
  735. {
  736. BUG_ON(smp_processor_id() != 0);
  737. current_thread_info()->cpu = 0;
  738. cpu_set(0, cpu_present_map);
  739. cpu_set(0, cpu_online_map);
  740. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  741. current_set[0] = current;
  742. smp_cpu_state[0] = CPU_STATE_CONFIGURED;
  743. smp_cpu_polarization[0] = POLARIZATION_UNKNWN;
  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,
  760. struct sysdev_attribute *attr, char *buf)
  761. {
  762. ssize_t count;
  763. mutex_lock(&smp_cpu_state_mutex);
  764. count = sprintf(buf, "%d\n", smp_cpu_state[dev->id]);
  765. mutex_unlock(&smp_cpu_state_mutex);
  766. return count;
  767. }
  768. static ssize_t cpu_configure_store(struct sys_device *dev,
  769. struct sysdev_attribute *attr,
  770. const char *buf, size_t count)
  771. {
  772. int cpu = dev->id;
  773. int val, rc;
  774. char delim;
  775. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  776. return -EINVAL;
  777. if (val != 0 && val != 1)
  778. return -EINVAL;
  779. get_online_cpus();
  780. mutex_lock(&smp_cpu_state_mutex);
  781. rc = -EBUSY;
  782. if (cpu_online(cpu))
  783. goto out;
  784. rc = 0;
  785. switch (val) {
  786. case 0:
  787. if (smp_cpu_state[cpu] == CPU_STATE_CONFIGURED) {
  788. rc = sclp_cpu_deconfigure(__cpu_logical_map[cpu]);
  789. if (!rc) {
  790. smp_cpu_state[cpu] = CPU_STATE_STANDBY;
  791. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  792. }
  793. }
  794. break;
  795. case 1:
  796. if (smp_cpu_state[cpu] == CPU_STATE_STANDBY) {
  797. rc = sclp_cpu_configure(__cpu_logical_map[cpu]);
  798. if (!rc) {
  799. smp_cpu_state[cpu] = CPU_STATE_CONFIGURED;
  800. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  801. }
  802. }
  803. break;
  804. default:
  805. break;
  806. }
  807. out:
  808. mutex_unlock(&smp_cpu_state_mutex);
  809. put_online_cpus();
  810. return rc ? rc : count;
  811. }
  812. static SYSDEV_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  813. #endif /* CONFIG_HOTPLUG_CPU */
  814. static ssize_t cpu_polarization_show(struct sys_device *dev,
  815. struct sysdev_attribute *attr, char *buf)
  816. {
  817. int cpu = dev->id;
  818. ssize_t count;
  819. mutex_lock(&smp_cpu_state_mutex);
  820. switch (smp_cpu_polarization[cpu]) {
  821. case POLARIZATION_HRZ:
  822. count = sprintf(buf, "horizontal\n");
  823. break;
  824. case POLARIZATION_VL:
  825. count = sprintf(buf, "vertical:low\n");
  826. break;
  827. case POLARIZATION_VM:
  828. count = sprintf(buf, "vertical:medium\n");
  829. break;
  830. case POLARIZATION_VH:
  831. count = sprintf(buf, "vertical:high\n");
  832. break;
  833. default:
  834. count = sprintf(buf, "unknown\n");
  835. break;
  836. }
  837. mutex_unlock(&smp_cpu_state_mutex);
  838. return count;
  839. }
  840. static SYSDEV_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  841. static ssize_t show_cpu_address(struct sys_device *dev,
  842. struct sysdev_attribute *attr, 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,
  859. struct sysdev_attribute *attr, char *buf)
  860. {
  861. unsigned int capability;
  862. int rc;
  863. rc = get_cpu_capability(&capability);
  864. if (rc)
  865. return rc;
  866. return sprintf(buf, "%u\n", capability);
  867. }
  868. static SYSDEV_ATTR(capability, 0444, show_capability, NULL);
  869. static ssize_t show_idle_count(struct sys_device *dev,
  870. struct sysdev_attribute *attr, char *buf)
  871. {
  872. struct s390_idle_data *idle;
  873. unsigned long long idle_count;
  874. idle = &per_cpu(s390_idle, dev->id);
  875. spin_lock_irq(&idle->lock);
  876. idle_count = idle->idle_count;
  877. spin_unlock_irq(&idle->lock);
  878. return sprintf(buf, "%llu\n", idle_count);
  879. }
  880. static SYSDEV_ATTR(idle_count, 0444, show_idle_count, NULL);
  881. static ssize_t show_idle_time(struct sys_device *dev,
  882. struct sysdev_attribute *attr, char *buf)
  883. {
  884. struct s390_idle_data *idle;
  885. unsigned long long new_time;
  886. idle = &per_cpu(s390_idle, dev->id);
  887. spin_lock_irq(&idle->lock);
  888. if (idle->in_idle) {
  889. new_time = get_clock();
  890. idle->idle_time += new_time - idle->idle_enter;
  891. idle->idle_enter = new_time;
  892. }
  893. new_time = idle->idle_time;
  894. spin_unlock_irq(&idle->lock);
  895. return sprintf(buf, "%llu\n", new_time >> 12);
  896. }
  897. static SYSDEV_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  898. static struct attribute *cpu_online_attrs[] = {
  899. &attr_capability.attr,
  900. &attr_idle_count.attr,
  901. &attr_idle_time_us.attr,
  902. NULL,
  903. };
  904. static struct attribute_group cpu_online_attr_group = {
  905. .attrs = cpu_online_attrs,
  906. };
  907. static int __cpuinit smp_cpu_notify(struct notifier_block *self,
  908. unsigned long action, void *hcpu)
  909. {
  910. unsigned int cpu = (unsigned int)(long)hcpu;
  911. struct cpu *c = &per_cpu(cpu_devices, cpu);
  912. struct sys_device *s = &c->sysdev;
  913. struct s390_idle_data *idle;
  914. switch (action) {
  915. case CPU_ONLINE:
  916. case CPU_ONLINE_FROZEN:
  917. idle = &per_cpu(s390_idle, cpu);
  918. spin_lock_irq(&idle->lock);
  919. idle->idle_enter = 0;
  920. idle->idle_time = 0;
  921. idle->idle_count = 0;
  922. spin_unlock_irq(&idle->lock);
  923. if (sysfs_create_group(&s->kobj, &cpu_online_attr_group))
  924. return NOTIFY_BAD;
  925. break;
  926. case CPU_DEAD:
  927. case CPU_DEAD_FROZEN:
  928. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  929. break;
  930. }
  931. return NOTIFY_OK;
  932. }
  933. static struct notifier_block __cpuinitdata smp_cpu_nb = {
  934. .notifier_call = smp_cpu_notify,
  935. };
  936. static int __devinit smp_add_present_cpu(int cpu)
  937. {
  938. struct cpu *c = &per_cpu(cpu_devices, cpu);
  939. struct sys_device *s = &c->sysdev;
  940. int rc;
  941. c->hotpluggable = 1;
  942. rc = register_cpu(c, cpu);
  943. if (rc)
  944. goto out;
  945. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  946. if (rc)
  947. goto out_cpu;
  948. if (!cpu_online(cpu))
  949. goto out;
  950. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  951. if (!rc)
  952. return 0;
  953. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  954. out_cpu:
  955. #ifdef CONFIG_HOTPLUG_CPU
  956. unregister_cpu(c);
  957. #endif
  958. out:
  959. return rc;
  960. }
  961. #ifdef CONFIG_HOTPLUG_CPU
  962. int __ref smp_rescan_cpus(void)
  963. {
  964. cpumask_t newcpus;
  965. int cpu;
  966. int rc;
  967. get_online_cpus();
  968. mutex_lock(&smp_cpu_state_mutex);
  969. newcpus = cpu_present_map;
  970. rc = __smp_rescan_cpus();
  971. if (rc)
  972. goto out;
  973. cpus_andnot(newcpus, cpu_present_map, newcpus);
  974. for_each_cpu_mask(cpu, newcpus) {
  975. rc = smp_add_present_cpu(cpu);
  976. if (rc)
  977. cpu_clear(cpu, cpu_present_map);
  978. }
  979. rc = 0;
  980. out:
  981. mutex_unlock(&smp_cpu_state_mutex);
  982. put_online_cpus();
  983. if (!cpus_empty(newcpus))
  984. topology_schedule_update();
  985. return rc;
  986. }
  987. static ssize_t __ref rescan_store(struct sys_device *dev,
  988. struct sysdev_attribute *attr,
  989. const char *buf,
  990. size_t count)
  991. {
  992. int rc;
  993. rc = smp_rescan_cpus();
  994. return rc ? rc : count;
  995. }
  996. static SYSDEV_ATTR(rescan, 0200, NULL, rescan_store);
  997. #endif /* CONFIG_HOTPLUG_CPU */
  998. static ssize_t dispatching_show(struct sys_device *dev,
  999. struct sysdev_attribute *attr,
  1000. char *buf)
  1001. {
  1002. ssize_t count;
  1003. mutex_lock(&smp_cpu_state_mutex);
  1004. count = sprintf(buf, "%d\n", cpu_management);
  1005. mutex_unlock(&smp_cpu_state_mutex);
  1006. return count;
  1007. }
  1008. static ssize_t dispatching_store(struct sys_device *dev,
  1009. struct sysdev_attribute *attr,
  1010. const char *buf, size_t count)
  1011. {
  1012. int val, rc;
  1013. char delim;
  1014. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  1015. return -EINVAL;
  1016. if (val != 0 && val != 1)
  1017. return -EINVAL;
  1018. rc = 0;
  1019. get_online_cpus();
  1020. mutex_lock(&smp_cpu_state_mutex);
  1021. if (cpu_management == val)
  1022. goto out;
  1023. rc = topology_set_cpu_management(val);
  1024. if (!rc)
  1025. cpu_management = val;
  1026. out:
  1027. mutex_unlock(&smp_cpu_state_mutex);
  1028. put_online_cpus();
  1029. return rc ? rc : count;
  1030. }
  1031. static SYSDEV_ATTR(dispatching, 0644, dispatching_show, dispatching_store);
  1032. static int __init topology_init(void)
  1033. {
  1034. int cpu;
  1035. int rc;
  1036. register_cpu_notifier(&smp_cpu_nb);
  1037. #ifdef CONFIG_HOTPLUG_CPU
  1038. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  1039. &attr_rescan.attr);
  1040. if (rc)
  1041. return rc;
  1042. #endif
  1043. rc = sysfs_create_file(&cpu_sysdev_class.kset.kobj,
  1044. &attr_dispatching.attr);
  1045. if (rc)
  1046. return rc;
  1047. for_each_present_cpu(cpu) {
  1048. rc = smp_add_present_cpu(cpu);
  1049. if (rc)
  1050. return rc;
  1051. }
  1052. return 0;
  1053. }
  1054. subsys_initcall(topology_init);