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