]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - arch/powerpc/platforms/pseries/setup.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[mirror_ubuntu-focal-kernel.git] / arch / powerpc / platforms / pseries / setup.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
033ef338 3 * 64-bit pSeries and RS/6000 setup code.
1da177e4
LT
4 *
5 * Copyright (C) 1995 Linus Torvalds
6 * Adapted from 'alpha' version by Gary Thomas
7 * Modified by Cort Dougan (cort@cs.nmt.edu)
8 * Modified by PPC64 Team, IBM Corp
1da177e4
LT
9 */
10
11/*
12 * bootup setup stuff..
13 */
14
62d60e9f 15#include <linux/cpu.h>
1da177e4
LT
16#include <linux/errno.h>
17#include <linux/sched.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/stddef.h>
21#include <linux/unistd.h>
1da177e4 22#include <linux/user.h>
1da177e4
LT
23#include <linux/tty.h>
24#include <linux/major.h>
25#include <linux/interrupt.h>
26#include <linux/reboot.h>
27#include <linux/init.h>
28#include <linux/ioport.h>
29#include <linux/console.h>
30#include <linux/pci.h>
cebb2b15 31#include <linux/utsname.h>
1da177e4 32#include <linux/adb.h>
4b16f8e2 33#include <linux/export.h>
1da177e4
LT
34#include <linux/delay.h>
35#include <linux/irq.h>
36#include <linux/seq_file.h>
37#include <linux/root_dev.h>
1cf3d8b3 38#include <linux/of.h>
705a7b47 39#include <linux/of_pci.h>
94675cce 40#include <linux/memblock.h>
1da177e4
LT
41
42#include <asm/mmu.h>
43#include <asm/processor.h>
44#include <asm/io.h>
45#include <asm/pgtable.h>
46#include <asm/prom.h>
47#include <asm/rtas.h>
48#include <asm/pci-bridge.h>
49#include <asm/iommu.h>
50#include <asm/dma.h>
51#include <asm/machdep.h>
52#include <asm/irq.h>
53#include <asm/time.h>
54#include <asm/nvram.h>
180a3362 55#include <asm/pmc.h>
0b05ac6e 56#include <asm/xics.h>
eac1e731 57#include <asm/xive.h>
d387899f 58#include <asm/ppc-pci.h>
69a80d3f
PM
59#include <asm/i8259.h>
60#include <asm/udbg.h>
2249ca9d 61#include <asm/smp.h>
577830b0 62#include <asm/firmware.h>
bed59275 63#include <asm/eeh.h>
bf99de36 64#include <asm/reg.h>
212bebb4 65#include <asm/plpar_wrappers.h>
d81d8258 66#include <asm/kexec.h>
38e9d36b 67#include <asm/isa-bridge.h>
f636c147 68#include <asm/security_features.h>
ec0c464c 69#include <asm/asm-const.h>
1da177e4 70
577830b0 71#include "pseries.h"
44bda4b7 72#include "../../../../drivers/pci/pci.h"
a1218720 73
81f14997
RJ
74int CMO_PrPSP = -1;
75int CMO_SecPSP = -1;
e589a440 76unsigned long CMO_PageSize = (ASM_CONST(1) << IOMMU_PAGE_SHIFT_4K);
d617a402 77EXPORT_SYMBOL(CMO_PageSize);
1da177e4 78
1da177e4
LT
79int fwnmi_active; /* TRUE if an FWNMI handler is present */
80
8446196a 81static void pSeries_show_cpuinfo(struct seq_file *m)
1da177e4
LT
82{
83 struct device_node *root;
84 const char *model = "";
85
86 root = of_find_node_by_path("/");
87 if (root)
e2eb6392 88 model = of_get_property(root, "model", NULL);
1da177e4
LT
89 seq_printf(m, "machine\t\t: CHRP %s\n", model);
90 of_node_put(root);
3a4c2601
AK
91 if (radix_enabled())
92 seq_printf(m, "MMU\t\t: Radix\n");
93 else
94 seq_printf(m, "MMU\t\t: Hash\n");
1da177e4
LT
95}
96
97/* Initialize firmware assisted non-maskable interrupts if
98 * the firmware supports this feature.
1da177e4
LT
99 */
100static void __init fwnmi_init(void)
101{
8c4f1f29 102 unsigned long system_reset_addr, machine_check_addr;
94675cce
MS
103 u8 *mce_data_buf;
104 unsigned int i;
105 int nr_cpus = num_possible_cpus();
c6d15258
MS
106#ifdef CONFIG_PPC_BOOK3S_64
107 struct slb_entry *slb_ptr;
108 size_t size;
109#endif
8c4f1f29 110
1da177e4
LT
111 int ibm_nmi_register = rtas_token("ibm,nmi-register");
112 if (ibm_nmi_register == RTAS_UNKNOWN_SERVICE)
113 return;
8c4f1f29
ME
114
115 /* If the kernel's not linked at zero we point the firmware at low
116 * addresses anyway, and use a trampoline to get to the real code. */
117 system_reset_addr = __pa(system_reset_fwnmi) - PHYSICAL_START;
118 machine_check_addr = __pa(machine_check_fwnmi) - PHYSICAL_START;
119
120 if (0 == rtas_call(ibm_nmi_register, 2, 1, NULL, system_reset_addr,
121 machine_check_addr))
1da177e4 122 fwnmi_active = 1;
94675cce
MS
123
124 /*
125 * Allocate a chunk for per cpu buffer to hold rtas errorlog.
126 * It will be used in real mode mce handler, hence it needs to be
127 * below RMA.
128 */
f806714f
MR
129 mce_data_buf = memblock_alloc_try_nid_raw(RTAS_ERROR_LOG_MAX * nr_cpus,
130 RTAS_ERROR_LOG_MAX, MEMBLOCK_LOW_LIMIT,
131 ppc64_rma_size, NUMA_NO_NODE);
132 if (!mce_data_buf)
133 panic("Failed to allocate %d bytes below %pa for MCE buffer\n",
134 RTAS_ERROR_LOG_MAX * nr_cpus, &ppc64_rma_size);
135
94675cce
MS
136 for_each_possible_cpu(i) {
137 paca_ptrs[i]->mce_data_buf = mce_data_buf +
138 (RTAS_ERROR_LOG_MAX * i);
139 }
c6d15258
MS
140
141#ifdef CONFIG_PPC_BOOK3S_64
142 /* Allocate per cpu slb area to save old slb contents during MCE */
143 size = sizeof(struct slb_entry) * mmu_slb_size * nr_cpus;
f806714f
MR
144 slb_ptr = memblock_alloc_try_nid_raw(size, sizeof(struct slb_entry),
145 MEMBLOCK_LOW_LIMIT, ppc64_rma_size,
146 NUMA_NO_NODE);
147 if (!slb_ptr)
148 panic("Failed to allocate %zu bytes below %pa for slb area\n",
149 size, &ppc64_rma_size);
150
c6d15258
MS
151 for_each_possible_cpu(i)
152 paca_ptrs[i]->mce_faulty_slbs = slb_ptr + (mmu_slb_size * i);
153#endif
1da177e4
LT
154}
155
bd0b9ac4 156static void pseries_8259_cascade(struct irq_desc *desc)
b9e5b4e6 157{
ec775d0e 158 struct irq_chip *chip = irq_desc_get_chip(desc);
35a84c2f 159 unsigned int cascade_irq = i8259_irq();
79f26c26 160
ef24ba70 161 if (cascade_irq)
7d12e780 162 generic_handle_irq(cascade_irq);
79f26c26
LB
163
164 chip->irq_eoi(&desc->irq_data);
b9e5b4e6
BH
165}
166
30d6ad25 167static void __init pseries_setup_i8259_cascade(void)
032ace7e
ME
168{
169 struct device_node *np, *old, *found = NULL;
30d6ad25 170 unsigned int cascade;
032ace7e
ME
171 const u32 *addrp;
172 unsigned long intack = 0;
30d6ad25 173 int naddr;
032ace7e 174
30d6ad25 175 for_each_node_by_type(np, "interrupt-controller") {
032ace7e
ME
176 if (of_device_is_compatible(np, "chrp,iic")) {
177 found = np;
178 break;
179 }
30d6ad25
ME
180 }
181
032ace7e 182 if (found == NULL) {
30d6ad25 183 printk(KERN_DEBUG "pic: no ISA interrupt controller\n");
032ace7e
ME
184 return;
185 }
30d6ad25 186
032ace7e 187 cascade = irq_of_parse_and_map(found, 0);
ef24ba70 188 if (!cascade) {
30d6ad25 189 printk(KERN_ERR "pic: failed to map cascade interrupt");
032ace7e
ME
190 return;
191 }
30d6ad25 192 pr_debug("pic: cascade mapped to irq %d\n", cascade);
032ace7e
ME
193
194 for (old = of_node_get(found); old != NULL ; old = np) {
195 np = of_get_parent(old);
196 of_node_put(old);
197 if (np == NULL)
198 break;
2c8e65b5 199 if (!of_node_name_eq(np, "pci"))
032ace7e
ME
200 continue;
201 addrp = of_get_property(np, "8259-interrupt-acknowledge", NULL);
202 if (addrp == NULL)
203 continue;
204 naddr = of_n_addr_cells(np);
205 intack = addrp[naddr-1];
206 if (naddr > 1)
207 intack |= ((unsigned long)addrp[naddr-2]) << 32;
208 }
209 if (intack)
30d6ad25 210 printk(KERN_DEBUG "pic: PCI 8259 intack at 0x%016lx\n", intack);
032ace7e
ME
211 i8259_init(found, intack);
212 of_node_put(found);
ec775d0e 213 irq_set_chained_handler(cascade, pseries_8259_cascade);
032ace7e
ME
214}
215
e7da5dac 216static void __init pseries_init_irq(void)
032ace7e 217{
eac1e731
CLG
218 /* Try using a XIVE if available, otherwise use a XICS */
219 if (!xive_spapr_init()) {
220 xics_init();
221 pseries_setup_i8259_cascade();
222 }
032ace7e
ME
223}
224
180a3362
ME
225static void pseries_lpar_enable_pmcs(void)
226{
227 unsigned long set, reset;
228
180a3362
ME
229 set = 1UL << 63;
230 reset = 0;
231 plpar_hcall_norets(H_PERFMON, set, reset);
180a3362
ME
232}
233
f5242e5a 234static int pci_dn_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
2eb4afb6 235{
f5242e5a 236 struct of_reconfig_data *rd = data;
ea0f8acf
GS
237 struct device_node *parent, *np = rd->dn;
238 struct pci_dn *pdn;
2eb4afb6
KG
239 int err = NOTIFY_OK;
240
241 switch (action) {
1cf3d8b3 242 case OF_RECONFIG_ATTACH_NODE:
ea0f8acf
GS
243 parent = of_get_parent(np);
244 pdn = parent ? PCI_DN(parent) : NULL;
8cc7581c 245 if (pdn)
d8f66f41 246 pci_add_device_node_info(pdn->phb, np);
ea0f8acf
GS
247
248 of_node_put(parent);
2eb4afb6 249 break;
590c7567 250 case OF_RECONFIG_DETACH_NODE:
ea0f8acf
GS
251 pdn = PCI_DN(np);
252 if (pdn)
253 list_del(&pdn->list);
590c7567 254 break;
2eb4afb6
KG
255 default:
256 err = NOTIFY_DONE;
257 break;
258 }
259 return err;
260}
261
262static struct notifier_block pci_dn_reconfig_nb = {
263 .notifier_call = pci_dn_reconfig_notifier,
264};
265
af442a1b
NA
266struct kmem_cache *dtl_cache;
267
abf917cd 268#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
cf9efce0
PM
269/*
270 * Allocate space for the dispatch trace log for all possible cpus
271 * and register the buffers with the hypervisor. This is used for
272 * computing time stolen by the hypervisor.
273 */
274static int alloc_dispatch_logs(void)
275{
276 int cpu, ret;
277 struct paca_struct *pp;
278 struct dtl_entry *dtl;
279
280 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
281 return 0;
282
af442a1b 283 if (!dtl_cache)
127493d5 284 return 0;
127493d5 285
cf9efce0 286 for_each_possible_cpu(cpu) {
d2e60075 287 pp = paca_ptrs[cpu];
127493d5 288 dtl = kmem_cache_alloc(dtl_cache, GFP_KERNEL);
cf9efce0
PM
289 if (!dtl) {
290 pr_warn("Failed to allocate dispatch trace log for cpu %d\n",
291 cpu);
292 pr_warn("Stolen time statistics will be unreliable\n");
293 break;
294 }
295
296 pp->dtl_ridx = 0;
297 pp->dispatch_log = dtl;
298 pp->dispatch_log_end = dtl + N_DISPATCH_LOG;
299 pp->dtl_curr = dtl;
300 }
301
302 /* Register the DTL for the current (boot) cpu */
303 dtl = get_paca()->dispatch_log;
304 get_paca()->dtl_ridx = 0;
305 get_paca()->dtl_curr = dtl;
306 get_paca()->lppaca_ptr->dtl_idx = 0;
307
308 /* hypervisor reads buffer length from this field */
7ffcf8ec 309 dtl->enqueue_to_dispatch_time = cpu_to_be32(DISPATCH_LOG_BYTES);
cf9efce0
PM
310 ret = register_dtl(hard_smp_processor_id(), __pa(dtl));
311 if (ret)
711ef84e
AB
312 pr_err("WARNING: DTL registration of cpu %d (hw %d) failed "
313 "with %d\n", smp_processor_id(),
314 hard_smp_processor_id(), ret);
cf9efce0
PM
315 get_paca()->lppaca_ptr->dtl_enable_mask = 2;
316
317 return 0;
318}
abf917cd 319#else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
af442a1b
NA
320static inline int alloc_dispatch_logs(void)
321{
322 return 0;
323}
abf917cd 324#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
cf9efce0 325
af442a1b
NA
326static int alloc_dispatch_log_kmem_cache(void)
327{
328 dtl_cache = kmem_cache_create("dtl", DISPATCH_LOG_BYTES,
329 DISPATCH_LOG_BYTES, 0, NULL);
330 if (!dtl_cache) {
331 pr_warn("Failed to create dispatch trace log buffer cache\n");
332 pr_warn("Stolen time statistics will be unreliable\n");
333 return 0;
334 }
335
336 return alloc_dispatch_logs();
337}
8e83e905 338machine_early_initcall(pseries, alloc_dispatch_log_kmem_cache);
af442a1b 339
363edbe2 340static void pseries_lpar_idle(void)
e179816c 341{
d8c6ad31
NP
342 /*
343 * Default handler to go into low thread priority and possibly
027dfac6 344 * low power mode by ceding processor to hypervisor
e179816c 345 */
d8c6ad31
NP
346
347 /* Indicate to hypervisor that we are idle. */
348 get_lppaca()->idle = 1;
349
350 /*
351 * Yield the processor to the hypervisor. We return if
352 * an external interrupt occurs (which are driven prior
353 * to returning here) or if a prod occurs from another
354 * processor. When returning here, external interrupts
355 * are enabled.
356 */
357 cede_processor();
358
359 get_lppaca()->idle = 0;
e179816c
DD
360}
361
fc8effa4
IM
362/*
363 * Enable relocation on during exceptions. This has partition wide scope and
364 * may take a while to complete, if it takes longer than one second we will
365 * just give up rather than wasting any more time on this - if that turns out
366 * to ever be a problem in practice we can move this into a kernel thread to
367 * finish off the process later in boot.
368 */
d3cbff1b 369void pseries_enable_reloc_on_exc(void)
fc8effa4
IM
370{
371 long rc;
372 unsigned int delay, total_delay = 0;
373
374 while (1) {
375 rc = enable_reloc_on_exceptions();
d3cbff1b
BH
376 if (!H_IS_LONG_BUSY(rc)) {
377 if (rc == H_P2) {
378 pr_info("Relocation on exceptions not"
379 " supported\n");
380 } else if (rc != H_SUCCESS) {
381 pr_warn("Unable to enable relocation"
382 " on exceptions: %ld\n", rc);
383 }
384 break;
385 }
fc8effa4
IM
386
387 delay = get_longbusy_msecs(rc);
388 total_delay += delay;
389 if (total_delay > 1000) {
390 pr_warn("Warning: Giving up waiting to enable "
391 "relocation on exceptions (%u msec)!\n",
392 total_delay);
d3cbff1b 393 return;
fc8effa4
IM
394 }
395
396 mdelay(delay);
397 }
398}
d3cbff1b 399EXPORT_SYMBOL(pseries_enable_reloc_on_exc);
fc8effa4 400
d3cbff1b 401void pseries_disable_reloc_on_exc(void)
cedddd81
IM
402{
403 long rc;
404
405 while (1) {
406 rc = disable_reloc_on_exceptions();
407 if (!H_IS_LONG_BUSY(rc))
d3cbff1b 408 break;
cedddd81
IM
409 mdelay(get_longbusy_msecs(rc));
410 }
d3cbff1b 411 if (rc != H_SUCCESS)
f2c2cbcc
JP
412 pr_warn("Warning: Failed to disable relocation on exceptions: %ld\n",
413 rc);
cedddd81 414}
d3cbff1b 415EXPORT_SYMBOL(pseries_disable_reloc_on_exc);
cedddd81 416
da665885 417#ifdef CONFIG_KEXEC_CORE
cedddd81
IM
418static void pSeries_machine_kexec(struct kimage *image)
419{
d3cbff1b
BH
420 if (firmware_has_feature(FW_FEATURE_SET_MODE))
421 pseries_disable_reloc_on_exc();
cedddd81
IM
422
423 default_machine_kexec(image);
424}
425#endif
426
e844b1ee 427#ifdef __LITTLE_ENDIAN__
d3cbff1b 428void pseries_big_endian_exceptions(void)
e844b1ee
AB
429{
430 long rc;
431
432 while (1) {
433 rc = enable_big_endian_exceptions();
434 if (!H_IS_LONG_BUSY(rc))
d3cbff1b 435 break;
e844b1ee
AB
436 mdelay(get_longbusy_msecs(rc));
437 }
d3cbff1b
BH
438
439 /*
440 * At this point it is unlikely panic() will get anything
441 * out to the user, since this is called very late in kexec
442 * but at least this will stop us from continuing on further
443 * and creating an even more difficult to debug situation.
444 *
445 * There is a known problem when kdump'ing, if cpus are offline
446 * the above call will fail. Rather than panicking again, keep
447 * going and hope the kdump kernel is also little endian, which
448 * it usually is.
449 */
450 if (rc && !kdump_in_progress())
451 panic("Could not enable big endian exceptions");
e844b1ee
AB
452}
453
d3cbff1b 454void pseries_little_endian_exceptions(void)
e844b1ee
AB
455{
456 long rc;
457
458 while (1) {
459 rc = enable_little_endian_exceptions();
460 if (!H_IS_LONG_BUSY(rc))
d3cbff1b 461 break;
e844b1ee
AB
462 mdelay(get_longbusy_msecs(rc));
463 }
d3cbff1b
BH
464 if (rc) {
465 ppc_md.progress("H_SET_MODE LE exception fail", 0);
466 panic("Could not enable little endian exceptions");
467 }
e844b1ee
AB
468}
469#endif
470
bdc728a8
DA
471static void __init find_and_init_phbs(void)
472{
473 struct device_node *node;
474 struct pci_controller *phb;
475 struct device_node *root = of_find_node_by_path("/");
476
477 for_each_child_of_node(root, node) {
e5480bdc
RH
478 if (!of_node_is_type(node, "pci") &&
479 !of_node_is_type(node, "pciex"))
bdc728a8
DA
480 continue;
481
482 phb = pcibios_alloc_controller(node);
483 if (!phb)
484 continue;
485 rtas_setup_phb(phb);
486 pci_process_bridge_OF_ranges(phb, node, 0);
487 isa_bridge_find_early(phb);
38ae9ec4 488 phb->controller_ops = pseries_pci_controller_ops;
bdc728a8
DA
489 }
490
491 of_node_put(root);
bdc728a8
DA
492
493 /*
494 * PCI_PROBE_ONLY and PCI_REASSIGN_ALL_BUS can be set via properties
495 * in chosen.
496 */
705a7b47 497 of_pci_check_probe_only();
bdc728a8
DA
498}
499
f636c147
ME
500static void init_cpu_char_feature_flags(struct h_cpu_char_result *result)
501{
6232774f
MFO
502 /*
503 * The features below are disabled by default, so we instead look to see
504 * if firmware has *enabled* them, and set them if so.
505 */
f636c147
ME
506 if (result->character & H_CPU_CHAR_SPEC_BAR_ORI31)
507 security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
508
509 if (result->character & H_CPU_CHAR_BCCTRL_SERIALISED)
510 security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
511
512 if (result->character & H_CPU_CHAR_L1D_FLUSH_ORI30)
513 security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
514
515 if (result->character & H_CPU_CHAR_L1D_FLUSH_TRIG2)
516 security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
517
518 if (result->character & H_CPU_CHAR_L1D_THREAD_PRIV)
519 security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
520
521 if (result->character & H_CPU_CHAR_COUNT_CACHE_DISABLED)
522 security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
523
ba72dc17
ME
524 if (result->character & H_CPU_CHAR_BCCTR_FLUSH_ASSIST)
525 security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);
526
527 if (result->behaviour & H_CPU_BEHAV_FLUSH_COUNT_CACHE)
528 security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);
529
f636c147
ME
530 /*
531 * The features below are enabled by default, so we instead look to see
532 * if firmware has *disabled* them, and clear them if so.
533 */
0f9bdfe3 534 if (!(result->behaviour & H_CPU_BEHAV_FAVOUR_SECURITY))
f636c147
ME
535 security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
536
0f9bdfe3 537 if (!(result->behaviour & H_CPU_BEHAV_L1D_FLUSH_PR))
f636c147
ME
538 security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
539
0f9bdfe3 540 if (!(result->behaviour & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR))
f636c147
ME
541 security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
542}
543
921bc6cf 544void pseries_setup_rfi_flush(void)
8989d568
MN
545{
546 struct h_cpu_char_result result;
547 enum l1d_flush_type types;
548 bool enable;
549 long rc;
550
6232774f
MFO
551 /*
552 * Set features to the defaults assumed by init_cpu_char_feature_flags()
553 * so it can set/clear again any features that might have changed after
554 * migration, and in case the hypercall fails and it is not even called.
555 */
556 powerpc_security_features = SEC_FTR_DEFAULT;
557
8989d568 558 rc = plpar_get_cpu_characteristics(&result);
2e4a1616 559 if (rc == H_SUCCESS)
f636c147
ME
560 init_cpu_char_feature_flags(&result);
561
f636c147
ME
562 /*
563 * We're the guest so this doesn't apply to us, clear it to simplify
564 * handling of it elsewhere.
565 */
566 security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
567
2e4a1616
ME
568 types = L1D_FLUSH_FALLBACK;
569
570 if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
571 types |= L1D_FLUSH_MTTRIG;
572
573 if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
574 types |= L1D_FLUSH_ORI;
575
576 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
577 security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR);
578
8989d568 579 setup_rfi_flush(types, enable);
ba72dc17 580 setup_count_cache_flush();
8989d568
MN
581}
582
fc5f6221
BL
583#ifdef CONFIG_PCI_IOV
584enum rtas_iov_fw_value_map {
585 NUM_RES_PROPERTY = 0, /* Number of Resources */
586 LOW_INT = 1, /* Lowest 32 bits of Address */
587 START_OF_ENTRIES = 2, /* Always start of entry */
588 APERTURE_PROPERTY = 2, /* Start of entry+ to Aperture Size */
589 WDW_SIZE_PROPERTY = 4, /* Start of entry+ to Window Size */
590 NEXT_ENTRY = 7 /* Go to next entry on array */
591};
592
593enum get_iov_fw_value_index {
594 BAR_ADDRS = 1, /* Get Bar Address */
595 APERTURE_SIZE = 2, /* Get Aperture Size */
596 WDW_SIZE = 3 /* Get Window Size */
597};
598
599resource_size_t pseries_get_iov_fw_value(struct pci_dev *dev, int resno,
600 enum get_iov_fw_value_index value)
601{
602 const int *indexes;
603 struct device_node *dn = pci_device_to_OF_node(dev);
604 int i, num_res, ret = 0;
605
606 indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
607 if (!indexes)
608 return 0;
609
610 /*
611 * First element in the array is the number of Bars
612 * returned. Search through the list to find the matching
613 * bar
614 */
615 num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
616 if (resno >= num_res)
617 return 0; /* or an errror */
618
619 i = START_OF_ENTRIES + NEXT_ENTRY * resno;
620 switch (value) {
621 case BAR_ADDRS:
622 ret = of_read_number(&indexes[i], 2);
623 break;
624 case APERTURE_SIZE:
625 ret = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
626 break;
627 case WDW_SIZE:
628 ret = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
629 break;
630 }
631
632 return ret;
633}
634
635void of_pci_set_vf_bar_size(struct pci_dev *dev, const int *indexes)
636{
637 struct resource *res;
638 resource_size_t base, size;
639 int i, r, num_res;
640
641 num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
642 num_res = min_t(int, num_res, PCI_SRIOV_NUM_BARS);
643 for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
644 i += NEXT_ENTRY, r++) {
645 res = &dev->resource[r + PCI_IOV_RESOURCES];
646 base = of_read_number(&indexes[i], 2);
647 size = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
648 res->flags = pci_parse_of_flags(of_read_number
649 (&indexes[i + LOW_INT], 1), 0);
650 res->flags |= (IORESOURCE_MEM_64 | IORESOURCE_PCI_FIXED);
651 res->name = pci_name(dev);
652 res->start = base;
653 res->end = base + size - 1;
654 }
655}
656
657void of_pci_parse_iov_addrs(struct pci_dev *dev, const int *indexes)
658{
659 struct resource *res, *root, *conflict;
660 resource_size_t base, size;
661 int i, r, num_res;
662
663 /*
664 * First element in the array is the number of Bars
665 * returned. Search through the list to find the matching
666 * bars assign them from firmware into resources structure.
667 */
668 num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
669 for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
670 i += NEXT_ENTRY, r++) {
671 res = &dev->resource[r + PCI_IOV_RESOURCES];
672 base = of_read_number(&indexes[i], 2);
673 size = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
674 res->name = pci_name(dev);
675 res->start = base;
676 res->end = base + size - 1;
677 root = &iomem_resource;
678 dev_dbg(&dev->dev,
679 "pSeries IOV BAR %d: trying firmware assignment %pR\n",
680 r + PCI_IOV_RESOURCES, res);
681 conflict = request_resource_conflict(root, res);
682 if (conflict) {
683 dev_info(&dev->dev,
684 "BAR %d: %pR conflicts with %s %pR\n",
685 r + PCI_IOV_RESOURCES, res,
686 conflict->name, conflict);
687 res->flags |= IORESOURCE_UNSET;
688 }
689 }
690}
691
b87b9cf4
SB
692static void pseries_disable_sriov_resources(struct pci_dev *pdev)
693{
694 int i;
695
696 pci_warn(pdev, "No hypervisor support for SR-IOV on this device, IOV BARs disabled.\n");
697 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
698 pdev->resource[i + PCI_IOV_RESOURCES].flags = 0;
699}
700
fc5f6221
BL
701static void pseries_pci_fixup_resources(struct pci_dev *pdev)
702{
703 const int *indexes;
704 struct device_node *dn = pci_device_to_OF_node(pdev);
705
706 /*Firmware must support open sriov otherwise dont configure*/
707 indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
b87b9cf4
SB
708 if (indexes)
709 of_pci_set_vf_bar_size(pdev, indexes);
710 else
711 pseries_disable_sriov_resources(pdev);
fc5f6221
BL
712}
713
714static void pseries_pci_fixup_iov_resources(struct pci_dev *pdev)
715{
716 const int *indexes;
717 struct device_node *dn = pci_device_to_OF_node(pdev);
718
44bda4b7 719 if (!pdev->is_physfn || pci_dev_is_added(pdev))
fc5f6221
BL
720 return;
721 /*Firmware must support open sriov otherwise dont configure*/
722 indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
b87b9cf4
SB
723 if (indexes)
724 of_pci_parse_iov_addrs(pdev, indexes);
725 else
726 pseries_disable_sriov_resources(pdev);
fc5f6221
BL
727}
728
729static resource_size_t pseries_pci_iov_resource_alignment(struct pci_dev *pdev,
730 int resno)
731{
732 const __be32 *reg;
733 struct device_node *dn = pci_device_to_OF_node(pdev);
734
735 /*Firmware must support open sriov otherwise report regular alignment*/
736 reg = of_get_property(dn, "ibm,is-open-sriov-pf", NULL);
737 if (!reg)
738 return pci_iov_resource_size(pdev, resno);
739
740 if (!pdev->is_physfn)
741 return 0;
742 return pseries_get_iov_fw_value(pdev,
743 resno - PCI_IOV_RESOURCES,
744 APERTURE_SIZE);
745}
746#endif
747
0ebfff14
BH
748static void __init pSeries_setup_arch(void)
749{
b71d47c1 750 set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
a934904d 751
0ebfff14 752 /* Discover PIC type and setup ppc_md accordingly */
86425bed 753 smp_init_pseries();
e7da5dac 754
0ebfff14 755
1da177e4
LT
756 /* openpic global configuration register (64-bit format). */
757 /* openpic Interrupt Source Unit pointer (64-bit format). */
758 /* python0 facility area (mmio) (64-bit format) REAL address. */
759
760 /* init to some ~sane value until calibrate_delay() runs */
761 loops_per_jiffy = 50000000;
762
1da177e4
LT
763 fwnmi_init();
764
8989d568 765 pseries_setup_rfi_flush();
a048a07d 766 setup_stf_barrier();
8989d568 767
446957ba 768 /* By default, only probe PCI (can be overridden by rtas_pci) */
673c9756 769 pci_add_flags(PCI_PROBE_ONLY);
3c13be01 770
1da177e4
LT
771 /* Find and initialize PCI host bridges */
772 init_pci_config_tokens();
1da177e4 773 find_and_init_phbs();
1cf3d8b3 774 of_reconfig_notifier_register(&pci_dn_reconfig_nb);
1da177e4 775
1da177e4
LT
776 pSeries_nvram_init();
777
363edbe2 778 if (firmware_has_feature(FW_FEATURE_LPAR)) {
8d15a3e5 779 vpa_init(boot_cpuid);
363edbe2 780 ppc_md.power_save = pseries_lpar_idle;
180a3362 781 ppc_md.enable_pmcs = pseries_lpar_enable_pmcs;
fc5f6221
BL
782#ifdef CONFIG_PCI_IOV
783 ppc_md.pcibios_fixup_resources =
784 pseries_pci_fixup_resources;
785 ppc_md.pcibios_fixup_sriov =
786 pseries_pci_fixup_iov_resources;
787 ppc_md.pcibios_iov_resource_alignment =
788 pseries_pci_iov_resource_alignment;
789#endif
363edbe2
VS
790 } else {
791 /* No special idle routine */
180a3362 792 ppc_md.enable_pmcs = power4_enable_pmcs;
363edbe2 793 }
fc8effa4 794
d82fb31a 795 ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare;
1da177e4
LT
796}
797
35adacd6
NP
798static void pseries_panic(char *str)
799{
800 panic_flush_kmsg_end();
801 rtas_os_term(str);
802}
803
1da177e4
LT
804static int __init pSeries_init_panel(void)
805{
806 /* Manually leave the kernel version on the panel. */
983d8a6d 807#ifdef __BIG_ENDIAN__
1da177e4 808 ppc_md.progress("Linux ppc64\n", 0);
983d8a6d
TB
809#else
810 ppc_md.progress("Linux ppc64le\n", 0);
811#endif
96b644bd 812 ppc_md.progress(init_utsname()->version, 0);
1da177e4
LT
813
814 return 0;
815}
f86d6b9b 816machine_arch_initcall(pseries, pSeries_init_panel);
1da177e4 817
4474ef05 818static int pseries_set_dabr(unsigned long dabr, unsigned long dabrx)
cab0af98 819{
76032de8 820 return plpar_hcall_norets(H_SET_DABR, dabr);
cab0af98
ME
821}
822
4474ef05 823static int pseries_set_xdabr(unsigned long dabr, unsigned long dabrx)
76032de8 824{
4474ef05
MN
825 /* Have to set at least one bit in the DABRX according to PAPR */
826 if (dabrx == 0 && dabr == 0)
827 dabrx = DABRX_USER;
828 /* PAPR says we can only set kernel and user bits */
cd144573 829 dabrx &= DABRX_KERNEL | DABRX_USER;
4474ef05
MN
830
831 return plpar_hcall_norets(H_SET_XDABR, dabr, dabrx);
76032de8 832}
1da177e4 833
bf99de36
MN
834static int pseries_set_dawr(unsigned long dawr, unsigned long dawrx)
835{
836 /* PAPR says we can't set HYP */
837 dawrx &= ~DAWRX_HYP;
838
7c09c186 839 return plpar_set_watchpoint0(dawr, dawrx);
bf99de36
MN
840}
841
e46de429
RJ
842#define CMO_CHARACTERISTICS_TOKEN 44
843#define CMO_MAXLENGTH 1026
844
9ee820fa
BK
845void pSeries_coalesce_init(void)
846{
847 struct hvcall_mpp_x_data mpp_x_data;
848
849 if (firmware_has_feature(FW_FEATURE_CMO) && !h_get_mpp_x(&mpp_x_data))
850 powerpc_firmware_features |= FW_FEATURE_XCMO;
851 else
852 powerpc_firmware_features &= ~FW_FEATURE_XCMO;
853}
854
e46de429
RJ
855/**
856 * fw_cmo_feature_init - FW_FEATURE_CMO is not stored in ibm,hypertas-functions,
857 * handle that here. (Stolen from parse_system_parameter_string)
858 */
e51df2c1 859static void pSeries_cmo_feature_init(void)
e46de429
RJ
860{
861 char *ptr, *key, *value, *end;
862 int call_status;
e589a440 863 int page_order = IOMMU_PAGE_SHIFT_4K;
e46de429
RJ
864
865 pr_debug(" -> fw_cmo_feature_init()\n");
866 spin_lock(&rtas_data_buf_lock);
867 memset(rtas_data_buf, 0, RTAS_DATA_BUF_SIZE);
868 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
869 NULL,
870 CMO_CHARACTERISTICS_TOKEN,
871 __pa(rtas_data_buf),
872 RTAS_DATA_BUF_SIZE);
873
874 if (call_status != 0) {
875 spin_unlock(&rtas_data_buf_lock);
876 pr_debug("CMO not available\n");
877 pr_debug(" <- fw_cmo_feature_init()\n");
878 return;
879 }
880
881 end = rtas_data_buf + CMO_MAXLENGTH - 2;
882 ptr = rtas_data_buf + 2; /* step over strlen value */
883 key = value = ptr;
884
885 while (*ptr && (ptr <= end)) {
886 /* Separate the key and value by replacing '=' with '\0' and
887 * point the value at the string after the '='
888 */
889 if (ptr[0] == '=') {
890 ptr[0] = '\0';
891 value = ptr + 1;
892 } else if (ptr[0] == '\0' || ptr[0] == ',') {
893 /* Terminate the string containing the key/value pair */
894 ptr[0] = '\0';
895
896 if (key == value) {
897 pr_debug("Malformed key/value pair\n");
898 /* Never found a '=', end processing */
899 break;
900 }
901
81f14997
RJ
902 if (0 == strcmp(key, "CMOPageSize"))
903 page_order = simple_strtol(value, NULL, 10);
904 else if (0 == strcmp(key, "PrPSP"))
905 CMO_PrPSP = simple_strtol(value, NULL, 10);
e46de429 906 else if (0 == strcmp(key, "SecPSP"))
81f14997 907 CMO_SecPSP = simple_strtol(value, NULL, 10);
e46de429
RJ
908 value = key = ptr + 1;
909 }
910 ptr++;
911 }
912
81f14997
RJ
913 /* Page size is returned as the power of 2 of the page size,
914 * convert to the page size in bytes before returning
915 */
916 CMO_PageSize = 1 << page_order;
917 pr_debug("CMO_PageSize = %lu\n", CMO_PageSize);
918
919 if (CMO_PrPSP != -1 || CMO_SecPSP != -1) {
e46de429 920 pr_info("CMO enabled\n");
81f14997
RJ
921 pr_debug("CMO enabled, PrPSP=%d, SecPSP=%d\n", CMO_PrPSP,
922 CMO_SecPSP);
e46de429 923 powerpc_firmware_features |= FW_FEATURE_CMO;
9ee820fa 924 pSeries_coalesce_init();
e46de429 925 } else
81f14997
RJ
926 pr_debug("CMO not enabled, PrPSP=%d, SecPSP=%d\n", CMO_PrPSP,
927 CMO_SecPSP);
e46de429
RJ
928 spin_unlock(&rtas_data_buf_lock);
929 pr_debug(" <- fw_cmo_feature_init()\n");
930}
931
1da177e4
LT
932/*
933 * Early initialization. Relocation is on but do not reference unbolted pages
934 */
f2d57694 935static void __init pseries_init(void)
1da177e4 936{
f2d57694 937 pr_debug(" -> pseries_init()\n");
1da177e4 938
4d2bb3f5 939#ifdef CONFIG_HVC_CONSOLE
57cfb814 940 if (firmware_has_feature(FW_FEATURE_LPAR))
4d2bb3f5
BH
941 hvc_vio_init_early();
942#endif
06c88766 943 if (firmware_has_feature(FW_FEATURE_XDABR))
76032de8 944 ppc_md.set_dabr = pseries_set_xdabr;
06c88766
MN
945 else if (firmware_has_feature(FW_FEATURE_DABR))
946 ppc_md.set_dabr = pseries_set_dabr;
1da177e4 947
bf99de36
MN
948 if (firmware_has_feature(FW_FEATURE_SET_MODE))
949 ppc_md.set_dawr = pseries_set_dawr;
950
e46de429 951 pSeries_cmo_feature_init();
1da177e4
LT
952 iommu_init_early_pSeries();
953
f2d57694 954 pr_debug(" <- pseries_init()\n");
1da177e4
LT
955}
956
9178ba29
AG
957/**
958 * pseries_power_off - tell firmware about how to power off the system.
959 *
960 * This function calls either the power-off rtas token in normal cases
961 * or the ibm,power-off-ups token (if present & requested) in case of
962 * a power failure. If power-off token is used, power on will only be
963 * possible with power button press. If ibm,power-off-ups token is used
964 * it will allow auto poweron after power is restored.
965 */
966static void pseries_power_off(void)
967{
968 int rc;
969 int rtas_poweroff_ups_token = rtas_token("ibm,power-off-ups");
970
971 if (rtas_flash_term_hook)
972 rtas_flash_term_hook(SYS_POWER_OFF);
973
974 if (rtas_poweron_auto == 0 ||
975 rtas_poweroff_ups_token == RTAS_UNKNOWN_SERVICE) {
976 rc = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
977 printk(KERN_INFO "RTAS power-off returned %d\n", rc);
978 } else {
979 rc = rtas_call(rtas_poweroff_ups_token, 0, 1, NULL);
980 printk(KERN_INFO "RTAS ibm,power-off-ups returned %d\n", rc);
981 }
982 for (;;);
983}
984
e8222502
BH
985static int __init pSeries_probe(void)
986{
e5480bdc 987 if (!of_node_is_type(of_root, "chrp"))
1da177e4
LT
988 return 0;
989
133dda1e
AB
990 /* Cell blades firmware claims to be chrp while it's not. Until this
991 * is fixed, we need to avoid those here.
992 */
406b0b6a
BH
993 if (of_machine_is_compatible("IBM,CPBW-1.0") ||
994 of_machine_is_compatible("IBM,CBEA"))
133dda1e
AB
995 return 0;
996
9178ba29
AG
997 pm_power_off = pseries_power_off;
998
f7ebf352
ME
999 pr_debug("Machine is%s LPAR !\n",
1000 (powerpc_firmware_features & FW_FEATURE_LPAR) ? "" : " not");
57cfb814 1001
f2d57694
BH
1002 pseries_init();
1003
1da177e4
LT
1004 return 1;
1005}
1006
4267292b
PM
1007static int pSeries_pci_probe_mode(struct pci_bus *bus)
1008{
57cfb814 1009 if (firmware_has_feature(FW_FEATURE_LPAR))
4267292b
PM
1010 return PCI_PROBE_DEVTREE;
1011 return PCI_PROBE_NORMAL;
1012}
1013
38ae9ec4
DA
1014struct pci_controller_ops pseries_pci_controller_ops = {
1015 .probe_mode = pSeries_pci_probe_mode,
1016};
1017
e8222502
BH
1018define_machine(pseries) {
1019 .name = "pSeries",
1da177e4
LT
1020 .probe = pSeries_probe,
1021 .setup_arch = pSeries_setup_arch,
e7da5dac 1022 .init_IRQ = pseries_init_irq,
0dd194d0 1023 .show_cpuinfo = pSeries_show_cpuinfo,
1da177e4
LT
1024 .log_error = pSeries_log_error,
1025 .pcibios_fixup = pSeries_final_fixup,
f4fcbbe9 1026 .restart = rtas_restart,
f4fcbbe9 1027 .halt = rtas_halt,
35adacd6 1028 .panic = pseries_panic,
773bf9c4
AB
1029 .get_boot_time = rtas_get_boot_time,
1030 .get_rtc_time = rtas_get_rtc_time,
1031 .set_rtc_time = rtas_set_rtc_time,
10f7e7c1 1032 .calibrate_decr = generic_calibrate_decr,
6566c6f1 1033 .progress = rtas_progress,
1da177e4 1034 .system_reset_exception = pSeries_system_reset_exception,
a43c1590 1035 .machine_check_early = pseries_machine_check_realmode,
1da177e4 1036 .machine_check_exception = pSeries_machine_check_exception,
da665885 1037#ifdef CONFIG_KEXEC_CORE
cedddd81 1038 .machine_kexec = pSeries_machine_kexec,
d739d2ca 1039 .kexec_cpu_down = pseries_kexec_cpu_down,
cedddd81 1040#endif
a5d86257
AB
1041#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
1042 .memory_block_size = pseries_memory_block_size,
1043#endif
1da177e4 1044};