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