smp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. * SMP related functions
  3. *
  4. * Copyright IBM Corp. 1999, 2012
  5. * Author(s): Denis Joseph Barrow,
  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. * The code outside of smp.c uses logical cpu numbers, only smp.c does
  14. * the translation of logical to physical cpu ids. All new code that
  15. * operates on physical cpu numbers needs to go into smp.c.
  16. */
  17. #define KMSG_COMPONENT "cpu"
  18. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  19. #include <linux/workqueue.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/mm.h>
  23. #include <linux/err.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/delay.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/irqflags.h>
  29. #include <linux/cpu.h>
  30. #include <linux/slab.h>
  31. #include <linux/crash_dump.h>
  32. #include <asm/asm-offsets.h>
  33. #include <asm/switch_to.h>
  34. #include <asm/facility.h>
  35. #include <asm/ipl.h>
  36. #include <asm/setup.h>
  37. #include <asm/irq.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/vtimer.h>
  40. #include <asm/lowcore.h>
  41. #include <asm/sclp.h>
  42. #include <asm/vdso.h>
  43. #include <asm/debug.h>
  44. #include <asm/os_info.h>
  45. #include <asm/sigp.h>
  46. #include "entry.h"
  47. enum {
  48. ec_schedule = 0,
  49. ec_call_function_single,
  50. ec_stop_cpu,
  51. };
  52. enum {
  53. CPU_STATE_STANDBY,
  54. CPU_STATE_CONFIGURED,
  55. };
  56. struct pcpu {
  57. struct cpu *cpu;
  58. struct _lowcore *lowcore; /* lowcore page(s) for the cpu */
  59. unsigned long async_stack; /* async stack for the cpu */
  60. unsigned long panic_stack; /* panic stack for the cpu */
  61. unsigned long ec_mask; /* bit mask for ec_xxx functions */
  62. int state; /* physical cpu state */
  63. int polarization; /* physical polarization */
  64. u16 address; /* physical cpu address */
  65. };
  66. static u8 boot_cpu_type;
  67. static u16 boot_cpu_address;
  68. static struct pcpu pcpu_devices[NR_CPUS];
  69. /*
  70. * The smp_cpu_state_mutex must be held when changing the state or polarization
  71. * member of a pcpu data structure within the pcpu_devices arreay.
  72. */
  73. DEFINE_MUTEX(smp_cpu_state_mutex);
  74. /*
  75. * Signal processor helper functions.
  76. */
  77. static inline int __pcpu_sigp_relax(u16 addr, u8 order, u32 parm, u32 *status)
  78. {
  79. int cc;
  80. while (1) {
  81. cc = __pcpu_sigp(addr, order, parm, NULL);
  82. if (cc != SIGP_CC_BUSY)
  83. return cc;
  84. cpu_relax();
  85. }
  86. }
  87. static int pcpu_sigp_retry(struct pcpu *pcpu, u8 order, u32 parm)
  88. {
  89. int cc, retry;
  90. for (retry = 0; ; retry++) {
  91. cc = __pcpu_sigp(pcpu->address, order, parm, NULL);
  92. if (cc != SIGP_CC_BUSY)
  93. break;
  94. if (retry >= 3)
  95. udelay(10);
  96. }
  97. return cc;
  98. }
  99. static inline int pcpu_stopped(struct pcpu *pcpu)
  100. {
  101. u32 uninitialized_var(status);
  102. if (__pcpu_sigp(pcpu->address, SIGP_SENSE,
  103. 0, &status) != SIGP_CC_STATUS_STORED)
  104. return 0;
  105. return !!(status & (SIGP_STATUS_CHECK_STOP|SIGP_STATUS_STOPPED));
  106. }
  107. static inline int pcpu_running(struct pcpu *pcpu)
  108. {
  109. if (__pcpu_sigp(pcpu->address, SIGP_SENSE_RUNNING,
  110. 0, NULL) != SIGP_CC_STATUS_STORED)
  111. return 1;
  112. /* Status stored condition code is equivalent to cpu not running. */
  113. return 0;
  114. }
  115. /*
  116. * Find struct pcpu by cpu address.
  117. */
  118. static struct pcpu *pcpu_find_address(const struct cpumask *mask, int address)
  119. {
  120. int cpu;
  121. for_each_cpu(cpu, mask)
  122. if (pcpu_devices[cpu].address == address)
  123. return pcpu_devices + cpu;
  124. return NULL;
  125. }
  126. static void pcpu_ec_call(struct pcpu *pcpu, int ec_bit)
  127. {
  128. int order;
  129. if (test_and_set_bit(ec_bit, &pcpu->ec_mask))
  130. return;
  131. order = pcpu_running(pcpu) ? SIGP_EXTERNAL_CALL : SIGP_EMERGENCY_SIGNAL;
  132. pcpu_sigp_retry(pcpu, order, 0);
  133. }
  134. static int pcpu_alloc_lowcore(struct pcpu *pcpu, int cpu)
  135. {
  136. struct _lowcore *lc;
  137. if (pcpu != &pcpu_devices[0]) {
  138. pcpu->lowcore = (struct _lowcore *)
  139. __get_free_pages(GFP_KERNEL | GFP_DMA, LC_ORDER);
  140. pcpu->async_stack = __get_free_pages(GFP_KERNEL, ASYNC_ORDER);
  141. pcpu->panic_stack = __get_free_page(GFP_KERNEL);
  142. if (!pcpu->lowcore || !pcpu->panic_stack || !pcpu->async_stack)
  143. goto out;
  144. }
  145. lc = pcpu->lowcore;
  146. memcpy(lc, &S390_lowcore, 512);
  147. memset((char *) lc + 512, 0, sizeof(*lc) - 512);
  148. lc->async_stack = pcpu->async_stack + ASYNC_SIZE
  149. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  150. lc->panic_stack = pcpu->panic_stack + PAGE_SIZE
  151. - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  152. lc->cpu_nr = cpu;
  153. #ifndef CONFIG_64BIT
  154. if (MACHINE_HAS_IEEE) {
  155. lc->extended_save_area_addr = get_zeroed_page(GFP_KERNEL);
  156. if (!lc->extended_save_area_addr)
  157. goto out;
  158. }
  159. #else
  160. if (vdso_alloc_per_cpu(lc))
  161. goto out;
  162. #endif
  163. lowcore_ptr[cpu] = lc;
  164. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, (u32)(unsigned long) lc);
  165. return 0;
  166. out:
  167. if (pcpu != &pcpu_devices[0]) {
  168. free_page(pcpu->panic_stack);
  169. free_pages(pcpu->async_stack, ASYNC_ORDER);
  170. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  171. }
  172. return -ENOMEM;
  173. }
  174. #ifdef CONFIG_HOTPLUG_CPU
  175. static void pcpu_free_lowcore(struct pcpu *pcpu)
  176. {
  177. pcpu_sigp_retry(pcpu, SIGP_SET_PREFIX, 0);
  178. lowcore_ptr[pcpu - pcpu_devices] = NULL;
  179. #ifndef CONFIG_64BIT
  180. if (MACHINE_HAS_IEEE) {
  181. struct _lowcore *lc = pcpu->lowcore;
  182. free_page((unsigned long) lc->extended_save_area_addr);
  183. lc->extended_save_area_addr = 0;
  184. }
  185. #else
  186. vdso_free_per_cpu(pcpu->lowcore);
  187. #endif
  188. if (pcpu != &pcpu_devices[0]) {
  189. free_page(pcpu->panic_stack);
  190. free_pages(pcpu->async_stack, ASYNC_ORDER);
  191. free_pages((unsigned long) pcpu->lowcore, LC_ORDER);
  192. }
  193. }
  194. #endif /* CONFIG_HOTPLUG_CPU */
  195. static void pcpu_prepare_secondary(struct pcpu *pcpu, int cpu)
  196. {
  197. struct _lowcore *lc = pcpu->lowcore;
  198. if (MACHINE_HAS_TLB_LC)
  199. cpumask_set_cpu(cpu, &init_mm.context.cpu_attach_mask);
  200. cpumask_set_cpu(cpu, mm_cpumask(&init_mm));
  201. atomic_inc(&init_mm.context.attach_count);
  202. lc->cpu_nr = cpu;
  203. lc->percpu_offset = __per_cpu_offset[cpu];
  204. lc->kernel_asce = S390_lowcore.kernel_asce;
  205. lc->machine_flags = S390_lowcore.machine_flags;
  206. lc->ftrace_func = S390_lowcore.ftrace_func;
  207. lc->user_timer = lc->system_timer = lc->steal_timer = 0;
  208. __ctl_store(lc->cregs_save_area, 0, 15);
  209. save_access_regs((unsigned int *) lc->access_regs_save_area);
  210. memcpy(lc->stfle_fac_list, S390_lowcore.stfle_fac_list,
  211. MAX_FACILITY_BIT/8);
  212. }
  213. static void pcpu_attach_task(struct pcpu *pcpu, struct task_struct *tsk)
  214. {
  215. struct _lowcore *lc = pcpu->lowcore;
  216. struct thread_info *ti = task_thread_info(tsk);
  217. lc->kernel_stack = (unsigned long) task_stack_page(tsk)
  218. + THREAD_SIZE - STACK_FRAME_OVERHEAD - sizeof(struct pt_regs);
  219. lc->thread_info = (unsigned long) task_thread_info(tsk);
  220. lc->current_task = (unsigned long) tsk;
  221. lc->user_timer = ti->user_timer;
  222. lc->system_timer = ti->system_timer;
  223. lc->steal_timer = 0;
  224. }
  225. static void pcpu_start_fn(struct pcpu *pcpu, void (*func)(void *), void *data)
  226. {
  227. struct _lowcore *lc = pcpu->lowcore;
  228. lc->restart_stack = lc->kernel_stack;
  229. lc->restart_fn = (unsigned long) func;
  230. lc->restart_data = (unsigned long) data;
  231. lc->restart_source = -1UL;
  232. pcpu_sigp_retry(pcpu, SIGP_RESTART, 0);
  233. }
  234. /*
  235. * Call function via PSW restart on pcpu and stop the current cpu.
  236. */
  237. static void pcpu_delegate(struct pcpu *pcpu, void (*func)(void *),
  238. void *data, unsigned long stack)
  239. {
  240. struct _lowcore *lc = lowcore_ptr[pcpu - pcpu_devices];
  241. unsigned long source_cpu = stap();
  242. __load_psw_mask(PSW_KERNEL_BITS);
  243. if (pcpu->address == source_cpu)
  244. func(data); /* should not return */
  245. /* Stop target cpu (if func returns this stops the current cpu). */
  246. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  247. /* Restart func on the target cpu and stop the current cpu. */
  248. mem_assign_absolute(lc->restart_stack, stack);
  249. mem_assign_absolute(lc->restart_fn, (unsigned long) func);
  250. mem_assign_absolute(lc->restart_data, (unsigned long) data);
  251. mem_assign_absolute(lc->restart_source, source_cpu);
  252. asm volatile(
  253. "0: sigp 0,%0,%2 # sigp restart to target cpu\n"
  254. " brc 2,0b # busy, try again\n"
  255. "1: sigp 0,%1,%3 # sigp stop to current cpu\n"
  256. " brc 2,1b # busy, try again\n"
  257. : : "d" (pcpu->address), "d" (source_cpu),
  258. "K" (SIGP_RESTART), "K" (SIGP_STOP)
  259. : "0", "1", "cc");
  260. for (;;) ;
  261. }
  262. /*
  263. * Call function on an online CPU.
  264. */
  265. void smp_call_online_cpu(void (*func)(void *), void *data)
  266. {
  267. struct pcpu *pcpu;
  268. /* Use the current cpu if it is online. */
  269. pcpu = pcpu_find_address(cpu_online_mask, stap());
  270. if (!pcpu)
  271. /* Use the first online cpu. */
  272. pcpu = pcpu_devices + cpumask_first(cpu_online_mask);
  273. pcpu_delegate(pcpu, func, data, (unsigned long) restart_stack);
  274. }
  275. /*
  276. * Call function on the ipl CPU.
  277. */
  278. void smp_call_ipl_cpu(void (*func)(void *), void *data)
  279. {
  280. pcpu_delegate(&pcpu_devices[0], func, data,
  281. pcpu_devices->panic_stack + PAGE_SIZE);
  282. }
  283. int smp_find_processor_id(u16 address)
  284. {
  285. int cpu;
  286. for_each_present_cpu(cpu)
  287. if (pcpu_devices[cpu].address == address)
  288. return cpu;
  289. return -1;
  290. }
  291. int smp_vcpu_scheduled(int cpu)
  292. {
  293. return pcpu_running(pcpu_devices + cpu);
  294. }
  295. void smp_yield(void)
  296. {
  297. if (MACHINE_HAS_DIAG44)
  298. asm volatile("diag 0,0,0x44");
  299. }
  300. void smp_yield_cpu(int cpu)
  301. {
  302. if (MACHINE_HAS_DIAG9C)
  303. asm volatile("diag %0,0,0x9c"
  304. : : "d" (pcpu_devices[cpu].address));
  305. else if (MACHINE_HAS_DIAG44)
  306. asm volatile("diag 0,0,0x44");
  307. }
  308. /*
  309. * Send cpus emergency shutdown signal. This gives the cpus the
  310. * opportunity to complete outstanding interrupts.
  311. */
  312. static void smp_emergency_stop(cpumask_t *cpumask)
  313. {
  314. u64 end;
  315. int cpu;
  316. end = get_tod_clock() + (1000000UL << 12);
  317. for_each_cpu(cpu, cpumask) {
  318. struct pcpu *pcpu = pcpu_devices + cpu;
  319. set_bit(ec_stop_cpu, &pcpu->ec_mask);
  320. while (__pcpu_sigp(pcpu->address, SIGP_EMERGENCY_SIGNAL,
  321. 0, NULL) == SIGP_CC_BUSY &&
  322. get_tod_clock() < end)
  323. cpu_relax();
  324. }
  325. while (get_tod_clock() < end) {
  326. for_each_cpu(cpu, cpumask)
  327. if (pcpu_stopped(pcpu_devices + cpu))
  328. cpumask_clear_cpu(cpu, cpumask);
  329. if (cpumask_empty(cpumask))
  330. break;
  331. cpu_relax();
  332. }
  333. }
  334. /*
  335. * Stop all cpus but the current one.
  336. */
  337. void smp_send_stop(void)
  338. {
  339. cpumask_t cpumask;
  340. int cpu;
  341. /* Disable all interrupts/machine checks */
  342. __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
  343. trace_hardirqs_off();
  344. debug_set_critical();
  345. cpumask_copy(&cpumask, cpu_online_mask);
  346. cpumask_clear_cpu(smp_processor_id(), &cpumask);
  347. if (oops_in_progress)
  348. smp_emergency_stop(&cpumask);
  349. /* stop all processors */
  350. for_each_cpu(cpu, &cpumask) {
  351. struct pcpu *pcpu = pcpu_devices + cpu;
  352. pcpu_sigp_retry(pcpu, SIGP_STOP, 0);
  353. while (!pcpu_stopped(pcpu))
  354. cpu_relax();
  355. }
  356. }
  357. /*
  358. * Stop the current cpu.
  359. */
  360. void smp_stop_cpu(void)
  361. {
  362. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  363. for (;;) ;
  364. }
  365. /*
  366. * This is the main routine where commands issued by other
  367. * cpus are handled.
  368. */
  369. static void smp_handle_ext_call(void)
  370. {
  371. unsigned long bits;
  372. /* handle bit signal external calls */
  373. bits = xchg(&pcpu_devices[smp_processor_id()].ec_mask, 0);
  374. if (test_bit(ec_stop_cpu, &bits))
  375. smp_stop_cpu();
  376. if (test_bit(ec_schedule, &bits))
  377. scheduler_ipi();
  378. if (test_bit(ec_call_function_single, &bits))
  379. generic_smp_call_function_single_interrupt();
  380. }
  381. static void do_ext_call_interrupt(struct ext_code ext_code,
  382. unsigned int param32, unsigned long param64)
  383. {
  384. inc_irq_stat(ext_code.code == 0x1202 ? IRQEXT_EXC : IRQEXT_EMS);
  385. smp_handle_ext_call();
  386. }
  387. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  388. {
  389. int cpu;
  390. for_each_cpu(cpu, mask)
  391. pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
  392. }
  393. void arch_send_call_function_single_ipi(int cpu)
  394. {
  395. pcpu_ec_call(pcpu_devices + cpu, ec_call_function_single);
  396. }
  397. #ifndef CONFIG_64BIT
  398. /*
  399. * this function sends a 'purge tlb' signal to another CPU.
  400. */
  401. static void smp_ptlb_callback(void *info)
  402. {
  403. __tlb_flush_local();
  404. }
  405. void smp_ptlb_all(void)
  406. {
  407. on_each_cpu(smp_ptlb_callback, NULL, 1);
  408. }
  409. EXPORT_SYMBOL(smp_ptlb_all);
  410. #endif /* ! CONFIG_64BIT */
  411. /*
  412. * this function sends a 'reschedule' IPI to another CPU.
  413. * it goes straight through and wastes no time serializing
  414. * anything. Worst case is that we lose a reschedule ...
  415. */
  416. void smp_send_reschedule(int cpu)
  417. {
  418. pcpu_ec_call(pcpu_devices + cpu, ec_schedule);
  419. }
  420. /*
  421. * parameter area for the set/clear control bit callbacks
  422. */
  423. struct ec_creg_mask_parms {
  424. unsigned long orval;
  425. unsigned long andval;
  426. int cr;
  427. };
  428. /*
  429. * callback for setting/clearing control bits
  430. */
  431. static void smp_ctl_bit_callback(void *info)
  432. {
  433. struct ec_creg_mask_parms *pp = info;
  434. unsigned long cregs[16];
  435. __ctl_store(cregs, 0, 15);
  436. cregs[pp->cr] = (cregs[pp->cr] & pp->andval) | pp->orval;
  437. __ctl_load(cregs, 0, 15);
  438. }
  439. /*
  440. * Set a bit in a control register of all cpus
  441. */
  442. void smp_ctl_set_bit(int cr, int bit)
  443. {
  444. struct ec_creg_mask_parms parms = { 1UL << bit, -1UL, cr };
  445. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  446. }
  447. EXPORT_SYMBOL(smp_ctl_set_bit);
  448. /*
  449. * Clear a bit in a control register of all cpus
  450. */
  451. void smp_ctl_clear_bit(int cr, int bit)
  452. {
  453. struct ec_creg_mask_parms parms = { 0, ~(1UL << bit), cr };
  454. on_each_cpu(smp_ctl_bit_callback, &parms, 1);
  455. }
  456. EXPORT_SYMBOL(smp_ctl_clear_bit);
  457. #if defined(CONFIG_ZFCPDUMP) || defined(CONFIG_CRASH_DUMP)
  458. static void __init smp_get_save_area(int cpu, u16 address)
  459. {
  460. void *lc = pcpu_devices[0].lowcore;
  461. struct save_area *save_area;
  462. if (is_kdump_kernel())
  463. return;
  464. if (!OLDMEM_BASE && (address == boot_cpu_address ||
  465. ipl_info.type != IPL_TYPE_FCP_DUMP))
  466. return;
  467. save_area = dump_save_area_create(cpu);
  468. if (!save_area)
  469. panic("could not allocate memory for save area\n");
  470. #ifdef CONFIG_CRASH_DUMP
  471. if (address == boot_cpu_address) {
  472. /* Copy the registers of the boot cpu. */
  473. copy_oldmem_page(1, (void *) save_area, sizeof(*save_area),
  474. SAVE_AREA_BASE - PAGE_SIZE, 0);
  475. return;
  476. }
  477. #endif
  478. /* Get the registers of a non-boot cpu. */
  479. __pcpu_sigp_relax(address, SIGP_STOP_AND_STORE_STATUS, 0, NULL);
  480. memcpy_real(save_area, lc + SAVE_AREA_BASE, sizeof(*save_area));
  481. }
  482. int smp_store_status(int cpu)
  483. {
  484. struct pcpu *pcpu;
  485. pcpu = pcpu_devices + cpu;
  486. if (__pcpu_sigp_relax(pcpu->address, SIGP_STOP_AND_STORE_STATUS,
  487. 0, NULL) != SIGP_CC_ORDER_CODE_ACCEPTED)
  488. return -EIO;
  489. return 0;
  490. }
  491. #else /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
  492. static inline void smp_get_save_area(int cpu, u16 address) { }
  493. #endif /* CONFIG_ZFCPDUMP || CONFIG_CRASH_DUMP */
  494. void smp_cpu_set_polarization(int cpu, int val)
  495. {
  496. pcpu_devices[cpu].polarization = val;
  497. }
  498. int smp_cpu_get_polarization(int cpu)
  499. {
  500. return pcpu_devices[cpu].polarization;
  501. }
  502. static struct sclp_cpu_info *smp_get_cpu_info(void)
  503. {
  504. static int use_sigp_detection;
  505. struct sclp_cpu_info *info;
  506. int address;
  507. info = kzalloc(sizeof(*info), GFP_KERNEL);
  508. if (info && (use_sigp_detection || sclp_get_cpu_info(info))) {
  509. use_sigp_detection = 1;
  510. for (address = 0; address <= MAX_CPU_ADDRESS; address++) {
  511. if (__pcpu_sigp_relax(address, SIGP_SENSE, 0, NULL) ==
  512. SIGP_CC_NOT_OPERATIONAL)
  513. continue;
  514. info->cpu[info->configured].address = address;
  515. info->configured++;
  516. }
  517. info->combined = info->configured;
  518. }
  519. return info;
  520. }
  521. static int smp_add_present_cpu(int cpu);
  522. static int __smp_rescan_cpus(struct sclp_cpu_info *info, int sysfs_add)
  523. {
  524. struct pcpu *pcpu;
  525. cpumask_t avail;
  526. int cpu, nr, i;
  527. nr = 0;
  528. cpumask_xor(&avail, cpu_possible_mask, cpu_present_mask);
  529. cpu = cpumask_first(&avail);
  530. for (i = 0; (i < info->combined) && (cpu < nr_cpu_ids); i++) {
  531. if (info->has_cpu_type && info->cpu[i].type != boot_cpu_type)
  532. continue;
  533. if (pcpu_find_address(cpu_present_mask, info->cpu[i].address))
  534. continue;
  535. pcpu = pcpu_devices + cpu;
  536. pcpu->address = info->cpu[i].address;
  537. pcpu->state = (i >= info->configured) ?
  538. CPU_STATE_STANDBY : CPU_STATE_CONFIGURED;
  539. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  540. set_cpu_present(cpu, true);
  541. if (sysfs_add && smp_add_present_cpu(cpu) != 0)
  542. set_cpu_present(cpu, false);
  543. else
  544. nr++;
  545. cpu = cpumask_next(cpu, &avail);
  546. }
  547. return nr;
  548. }
  549. static void __init smp_detect_cpus(void)
  550. {
  551. unsigned int cpu, c_cpus, s_cpus;
  552. struct sclp_cpu_info *info;
  553. info = smp_get_cpu_info();
  554. if (!info)
  555. panic("smp_detect_cpus failed to allocate memory\n");
  556. if (info->has_cpu_type) {
  557. for (cpu = 0; cpu < info->combined; cpu++) {
  558. if (info->cpu[cpu].address != boot_cpu_address)
  559. continue;
  560. /* The boot cpu dictates the cpu type. */
  561. boot_cpu_type = info->cpu[cpu].type;
  562. break;
  563. }
  564. }
  565. c_cpus = s_cpus = 0;
  566. for (cpu = 0; cpu < info->combined; cpu++) {
  567. if (info->has_cpu_type && info->cpu[cpu].type != boot_cpu_type)
  568. continue;
  569. if (cpu < info->configured) {
  570. smp_get_save_area(c_cpus, info->cpu[cpu].address);
  571. c_cpus++;
  572. } else
  573. s_cpus++;
  574. }
  575. pr_info("%d configured CPUs, %d standby CPUs\n", c_cpus, s_cpus);
  576. get_online_cpus();
  577. __smp_rescan_cpus(info, 0);
  578. put_online_cpus();
  579. kfree(info);
  580. }
  581. /*
  582. * Activate a secondary processor.
  583. */
  584. static void smp_start_secondary(void *cpuvoid)
  585. {
  586. S390_lowcore.last_update_clock = get_tod_clock();
  587. S390_lowcore.restart_stack = (unsigned long) restart_stack;
  588. S390_lowcore.restart_fn = (unsigned long) do_restart;
  589. S390_lowcore.restart_data = 0;
  590. S390_lowcore.restart_source = -1UL;
  591. restore_access_regs(S390_lowcore.access_regs_save_area);
  592. __ctl_load(S390_lowcore.cregs_save_area, 0, 15);
  593. __load_psw_mask(PSW_KERNEL_BITS | PSW_MASK_DAT);
  594. cpu_init();
  595. preempt_disable();
  596. init_cpu_timer();
  597. init_cpu_vtimer();
  598. pfault_init();
  599. notify_cpu_starting(smp_processor_id());
  600. set_cpu_online(smp_processor_id(), true);
  601. inc_irq_stat(CPU_RST);
  602. local_irq_enable();
  603. cpu_startup_entry(CPUHP_ONLINE);
  604. }
  605. /* Upping and downing of CPUs */
  606. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  607. {
  608. struct pcpu *pcpu;
  609. int rc;
  610. pcpu = pcpu_devices + cpu;
  611. if (pcpu->state != CPU_STATE_CONFIGURED)
  612. return -EIO;
  613. if (pcpu_sigp_retry(pcpu, SIGP_INITIAL_CPU_RESET, 0) !=
  614. SIGP_CC_ORDER_CODE_ACCEPTED)
  615. return -EIO;
  616. rc = pcpu_alloc_lowcore(pcpu, cpu);
  617. if (rc)
  618. return rc;
  619. pcpu_prepare_secondary(pcpu, cpu);
  620. pcpu_attach_task(pcpu, tidle);
  621. pcpu_start_fn(pcpu, smp_start_secondary, NULL);
  622. while (!cpu_online(cpu))
  623. cpu_relax();
  624. return 0;
  625. }
  626. static unsigned int setup_possible_cpus __initdata;
  627. static int __init _setup_possible_cpus(char *s)
  628. {
  629. get_option(&s, &setup_possible_cpus);
  630. return 0;
  631. }
  632. early_param("possible_cpus", _setup_possible_cpus);
  633. #ifdef CONFIG_HOTPLUG_CPU
  634. int __cpu_disable(void)
  635. {
  636. unsigned long cregs[16];
  637. /* Handle possible pending IPIs */
  638. smp_handle_ext_call();
  639. set_cpu_online(smp_processor_id(), false);
  640. /* Disable pseudo page faults on this cpu. */
  641. pfault_fini();
  642. /* Disable interrupt sources via control register. */
  643. __ctl_store(cregs, 0, 15);
  644. cregs[0] &= ~0x0000ee70UL; /* disable all external interrupts */
  645. cregs[6] &= ~0xff000000UL; /* disable all I/O interrupts */
  646. cregs[14] &= ~0x1f000000UL; /* disable most machine checks */
  647. __ctl_load(cregs, 0, 15);
  648. return 0;
  649. }
  650. void __cpu_die(unsigned int cpu)
  651. {
  652. struct pcpu *pcpu;
  653. /* Wait until target cpu is down */
  654. pcpu = pcpu_devices + cpu;
  655. while (!pcpu_stopped(pcpu))
  656. cpu_relax();
  657. pcpu_free_lowcore(pcpu);
  658. atomic_dec(&init_mm.context.attach_count);
  659. cpumask_clear_cpu(cpu, mm_cpumask(&init_mm));
  660. if (MACHINE_HAS_TLB_LC)
  661. cpumask_clear_cpu(cpu, &init_mm.context.cpu_attach_mask);
  662. }
  663. void __noreturn cpu_die(void)
  664. {
  665. idle_task_exit();
  666. pcpu_sigp_retry(pcpu_devices + smp_processor_id(), SIGP_STOP, 0);
  667. for (;;) ;
  668. }
  669. #endif /* CONFIG_HOTPLUG_CPU */
  670. void __init smp_fill_possible_mask(void)
  671. {
  672. unsigned int possible, sclp, cpu;
  673. sclp = sclp_get_max_cpu() ?: nr_cpu_ids;
  674. possible = setup_possible_cpus ?: nr_cpu_ids;
  675. possible = min(possible, sclp);
  676. for (cpu = 0; cpu < possible && cpu < nr_cpu_ids; cpu++)
  677. set_cpu_possible(cpu, true);
  678. }
  679. void __init smp_prepare_cpus(unsigned int max_cpus)
  680. {
  681. /* request the 0x1201 emergency signal external interrupt */
  682. if (register_external_irq(EXT_IRQ_EMERGENCY_SIG, do_ext_call_interrupt))
  683. panic("Couldn't request external interrupt 0x1201");
  684. /* request the 0x1202 external call external interrupt */
  685. if (register_external_irq(EXT_IRQ_EXTERNAL_CALL, do_ext_call_interrupt))
  686. panic("Couldn't request external interrupt 0x1202");
  687. smp_detect_cpus();
  688. }
  689. void __init smp_prepare_boot_cpu(void)
  690. {
  691. struct pcpu *pcpu = pcpu_devices;
  692. boot_cpu_address = stap();
  693. pcpu->state = CPU_STATE_CONFIGURED;
  694. pcpu->address = boot_cpu_address;
  695. pcpu->lowcore = (struct _lowcore *)(unsigned long) store_prefix();
  696. pcpu->async_stack = S390_lowcore.async_stack - ASYNC_SIZE
  697. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  698. pcpu->panic_stack = S390_lowcore.panic_stack - PAGE_SIZE
  699. + STACK_FRAME_OVERHEAD + sizeof(struct pt_regs);
  700. S390_lowcore.percpu_offset = __per_cpu_offset[0];
  701. smp_cpu_set_polarization(0, POLARIZATION_UNKNOWN);
  702. set_cpu_present(0, true);
  703. set_cpu_online(0, true);
  704. }
  705. void __init smp_cpus_done(unsigned int max_cpus)
  706. {
  707. }
  708. void __init smp_setup_processor_id(void)
  709. {
  710. S390_lowcore.cpu_nr = 0;
  711. }
  712. /*
  713. * the frequency of the profiling timer can be changed
  714. * by writing a multiplier value into /proc/profile.
  715. *
  716. * usually you want to run this on all CPUs ;)
  717. */
  718. int setup_profiling_timer(unsigned int multiplier)
  719. {
  720. return 0;
  721. }
  722. #ifdef CONFIG_HOTPLUG_CPU
  723. static ssize_t cpu_configure_show(struct device *dev,
  724. struct device_attribute *attr, char *buf)
  725. {
  726. ssize_t count;
  727. mutex_lock(&smp_cpu_state_mutex);
  728. count = sprintf(buf, "%d\n", pcpu_devices[dev->id].state);
  729. mutex_unlock(&smp_cpu_state_mutex);
  730. return count;
  731. }
  732. static ssize_t cpu_configure_store(struct device *dev,
  733. struct device_attribute *attr,
  734. const char *buf, size_t count)
  735. {
  736. struct pcpu *pcpu;
  737. int cpu, val, rc;
  738. char delim;
  739. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  740. return -EINVAL;
  741. if (val != 0 && val != 1)
  742. return -EINVAL;
  743. get_online_cpus();
  744. mutex_lock(&smp_cpu_state_mutex);
  745. rc = -EBUSY;
  746. /* disallow configuration changes of online cpus and cpu 0 */
  747. cpu = dev->id;
  748. if (cpu_online(cpu) || cpu == 0)
  749. goto out;
  750. pcpu = pcpu_devices + cpu;
  751. rc = 0;
  752. switch (val) {
  753. case 0:
  754. if (pcpu->state != CPU_STATE_CONFIGURED)
  755. break;
  756. rc = sclp_cpu_deconfigure(pcpu->address);
  757. if (rc)
  758. break;
  759. pcpu->state = CPU_STATE_STANDBY;
  760. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  761. topology_expect_change();
  762. break;
  763. case 1:
  764. if (pcpu->state != CPU_STATE_STANDBY)
  765. break;
  766. rc = sclp_cpu_configure(pcpu->address);
  767. if (rc)
  768. break;
  769. pcpu->state = CPU_STATE_CONFIGURED;
  770. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  771. topology_expect_change();
  772. break;
  773. default:
  774. break;
  775. }
  776. out:
  777. mutex_unlock(&smp_cpu_state_mutex);
  778. put_online_cpus();
  779. return rc ? rc : count;
  780. }
  781. static DEVICE_ATTR(configure, 0644, cpu_configure_show, cpu_configure_store);
  782. #endif /* CONFIG_HOTPLUG_CPU */
  783. static ssize_t show_cpu_address(struct device *dev,
  784. struct device_attribute *attr, char *buf)
  785. {
  786. return sprintf(buf, "%d\n", pcpu_devices[dev->id].address);
  787. }
  788. static DEVICE_ATTR(address, 0444, show_cpu_address, NULL);
  789. static struct attribute *cpu_common_attrs[] = {
  790. #ifdef CONFIG_HOTPLUG_CPU
  791. &dev_attr_configure.attr,
  792. #endif
  793. &dev_attr_address.attr,
  794. NULL,
  795. };
  796. static struct attribute_group cpu_common_attr_group = {
  797. .attrs = cpu_common_attrs,
  798. };
  799. static ssize_t show_idle_count(struct device *dev,
  800. struct device_attribute *attr, char *buf)
  801. {
  802. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  803. unsigned long long idle_count;
  804. unsigned int sequence;
  805. do {
  806. sequence = ACCESS_ONCE(idle->sequence);
  807. idle_count = ACCESS_ONCE(idle->idle_count);
  808. if (ACCESS_ONCE(idle->clock_idle_enter))
  809. idle_count++;
  810. } while ((sequence & 1) || (ACCESS_ONCE(idle->sequence) != sequence));
  811. return sprintf(buf, "%llu\n", idle_count);
  812. }
  813. static DEVICE_ATTR(idle_count, 0444, show_idle_count, NULL);
  814. static ssize_t show_idle_time(struct device *dev,
  815. struct device_attribute *attr, char *buf)
  816. {
  817. struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id);
  818. unsigned long long now, idle_time, idle_enter, idle_exit;
  819. unsigned int sequence;
  820. do {
  821. now = get_tod_clock();
  822. sequence = ACCESS_ONCE(idle->sequence);
  823. idle_time = ACCESS_ONCE(idle->idle_time);
  824. idle_enter = ACCESS_ONCE(idle->clock_idle_enter);
  825. idle_exit = ACCESS_ONCE(idle->clock_idle_exit);
  826. } while ((sequence & 1) || (ACCESS_ONCE(idle->sequence) != sequence));
  827. idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  828. return sprintf(buf, "%llu\n", idle_time >> 12);
  829. }
  830. static DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL);
  831. static struct attribute *cpu_online_attrs[] = {
  832. &dev_attr_idle_count.attr,
  833. &dev_attr_idle_time_us.attr,
  834. NULL,
  835. };
  836. static struct attribute_group cpu_online_attr_group = {
  837. .attrs = cpu_online_attrs,
  838. };
  839. static int smp_cpu_notify(struct notifier_block *self, unsigned long action,
  840. void *hcpu)
  841. {
  842. unsigned int cpu = (unsigned int)(long)hcpu;
  843. struct cpu *c = pcpu_devices[cpu].cpu;
  844. struct device *s = &c->dev;
  845. int err = 0;
  846. switch (action & ~CPU_TASKS_FROZEN) {
  847. case CPU_ONLINE:
  848. err = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  849. break;
  850. case CPU_DEAD:
  851. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  852. break;
  853. }
  854. return notifier_from_errno(err);
  855. }
  856. static int smp_add_present_cpu(int cpu)
  857. {
  858. struct device *s;
  859. struct cpu *c;
  860. int rc;
  861. c = kzalloc(sizeof(*c), GFP_KERNEL);
  862. if (!c)
  863. return -ENOMEM;
  864. pcpu_devices[cpu].cpu = c;
  865. s = &c->dev;
  866. c->hotpluggable = 1;
  867. rc = register_cpu(c, cpu);
  868. if (rc)
  869. goto out;
  870. rc = sysfs_create_group(&s->kobj, &cpu_common_attr_group);
  871. if (rc)
  872. goto out_cpu;
  873. if (cpu_online(cpu)) {
  874. rc = sysfs_create_group(&s->kobj, &cpu_online_attr_group);
  875. if (rc)
  876. goto out_online;
  877. }
  878. rc = topology_cpu_init(c);
  879. if (rc)
  880. goto out_topology;
  881. return 0;
  882. out_topology:
  883. if (cpu_online(cpu))
  884. sysfs_remove_group(&s->kobj, &cpu_online_attr_group);
  885. out_online:
  886. sysfs_remove_group(&s->kobj, &cpu_common_attr_group);
  887. out_cpu:
  888. #ifdef CONFIG_HOTPLUG_CPU
  889. unregister_cpu(c);
  890. #endif
  891. out:
  892. return rc;
  893. }
  894. #ifdef CONFIG_HOTPLUG_CPU
  895. int __ref smp_rescan_cpus(void)
  896. {
  897. struct sclp_cpu_info *info;
  898. int nr;
  899. info = smp_get_cpu_info();
  900. if (!info)
  901. return -ENOMEM;
  902. get_online_cpus();
  903. mutex_lock(&smp_cpu_state_mutex);
  904. nr = __smp_rescan_cpus(info, 1);
  905. mutex_unlock(&smp_cpu_state_mutex);
  906. put_online_cpus();
  907. kfree(info);
  908. if (nr)
  909. topology_schedule_update();
  910. return 0;
  911. }
  912. static ssize_t __ref rescan_store(struct device *dev,
  913. struct device_attribute *attr,
  914. const char *buf,
  915. size_t count)
  916. {
  917. int rc;
  918. rc = smp_rescan_cpus();
  919. return rc ? rc : count;
  920. }
  921. static DEVICE_ATTR(rescan, 0200, NULL, rescan_store);
  922. #endif /* CONFIG_HOTPLUG_CPU */
  923. static int __init s390_smp_init(void)
  924. {
  925. int cpu, rc = 0;
  926. #ifdef CONFIG_HOTPLUG_CPU
  927. rc = device_create_file(cpu_subsys.dev_root, &dev_attr_rescan);
  928. if (rc)
  929. return rc;
  930. #endif
  931. cpu_notifier_register_begin();
  932. for_each_present_cpu(cpu) {
  933. rc = smp_add_present_cpu(cpu);
  934. if (rc)
  935. goto out;
  936. }
  937. __hotcpu_notifier(smp_cpu_notify, 0);
  938. out:
  939. cpu_notifier_register_done();
  940. return rc;
  941. }
  942. subsys_initcall(s390_smp_init);