blob: d8930c3a8a11ed6a482754ecdcfdfee0eada9157 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Paul Mackerras033ef332005-10-26 17:05:24 +10003 * 64-bit pSeries and RS/6000 setup code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/*
12 * bootup setup stuff..
13 */
14
Michael Ellerman62d60e92005-07-07 17:56:30 -070015#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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>
Olaf Heringcebb2b12005-07-10 19:35:15 +000031#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/adb.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040033#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/delay.h>
35#include <linux/irq.h>
36#include <linux/seq_file.h>
37#include <linux/root_dev.h>
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +000038#include <linux/of.h>
Marc Zyngier705a7b42015-09-04 17:50:10 +010039#include <linux/of_pci.h>
Mahesh Salgaonkar94675cc2018-07-04 23:27:21 +053040#include <linux/memblock.h>
Alexey Kardashevskiy1a047cc2019-05-07 16:25:59 +100041#include <linux/swiotlb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/mmu.h>
44#include <asm/processor.h>
45#include <asm/io.h>
46#include <asm/pgtable.h>
47#include <asm/prom.h>
48#include <asm/rtas.h>
49#include <asm/pci-bridge.h>
50#include <asm/iommu.h>
51#include <asm/dma.h>
52#include <asm/machdep.h>
53#include <asm/irq.h>
54#include <asm/time.h>
55#include <asm/nvram.h>
Michael Ellerman180a3362005-08-09 11:13:36 +100056#include <asm/pmc.h>
Benjamin Herrenschmidt0b05ac62011-04-04 13:46:58 +100057#include <asm/xics.h>
Cédric Le Goatereac1e732017-08-30 21:46:11 +020058#include <asm/xive.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100059#include <asm/ppc-pci.h>
Paul Mackerras69a80d32005-10-10 22:03:41 +100060#include <asm/i8259.h>
61#include <asm/udbg.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110062#include <asm/smp.h>
Michael Ellerman577830b2007-02-08 18:33:51 +110063#include <asm/firmware.h>
Stephen Rothwellbed59272007-03-04 17:04:44 +110064#include <asm/eeh.h>
Michael Neulingbf99de32012-12-20 14:06:45 +000065#include <asm/reg.h>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053066#include <asm/plpar_wrappers.h>
Thiago Jung Bauermannd81d8252016-08-23 19:57:39 -030067#include <asm/kexec.h>
Benjamin Herrenschmidt38e9d362017-01-30 18:11:55 +110068#include <asm/isa-bridge.h>
Michael Ellermanf636c142018-03-27 23:01:46 +110069#include <asm/security_features.h>
Christophe Leroyec0c4642018-07-05 16:24:57 +000070#include <asm/asm-const.h>
Alexey Kardashevskiy1a047cc2019-05-07 16:25:59 +100071#include <asm/swiotlb.h>
Anshuman Khanduald5394c02019-08-19 23:13:19 -030072#include <asm/svm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Michael Ellerman577830b2007-02-08 18:33:51 +110074#include "pseries.h"
Hari Vyas44bda4b2018-07-03 14:35:41 +053075#include "../../../../drivers/pci/pci.h"
Michael Ellermana1218722005-11-03 15:33:31 +110076
Robert Jennings81f14992008-08-16 05:07:31 +100077int CMO_PrPSP = -1;
78int CMO_SecPSP = -1;
Alistair Popplee589a4402013-12-09 18:17:01 +110079unsigned long CMO_PageSize = (ASM_CONST(1) << IOMMU_PAGE_SHIFT_4K);
Andrew Mortond617a402008-08-21 11:16:26 +100080EXPORT_SYMBOL(CMO_PageSize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082int fwnmi_active; /* TRUE if an FWNMI handler is present */
83
Arnd Bergmann84461962006-01-11 00:00:02 +000084static void pSeries_show_cpuinfo(struct seq_file *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 struct device_node *root;
87 const char *model = "";
88
89 root = of_find_node_by_path("/");
90 if (root)
Stephen Rothwelle2eb6392007-04-03 22:26:41 +100091 model = of_get_property(root, "model", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 seq_printf(m, "machine\t\t: CHRP %s\n", model);
93 of_node_put(root);
Aneesh Kumar K.V3a4c26012017-03-21 22:59:55 +053094 if (radix_enabled())
95 seq_printf(m, "MMU\t\t: Radix\n");
96 else
97 seq_printf(m, "MMU\t\t: Hash\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100/* Initialize firmware assisted non-maskable interrupts if
101 * the firmware supports this feature.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103static void __init fwnmi_init(void)
104{
Michael Ellerman8c4f1f22005-12-04 18:39:33 +1100105 unsigned long system_reset_addr, machine_check_addr;
Mahesh Salgaonkar94675cc2018-07-04 23:27:21 +0530106 u8 *mce_data_buf;
107 unsigned int i;
108 int nr_cpus = num_possible_cpus();
Mahesh Salgaonkarc6d15252018-09-11 19:57:15 +0530109#ifdef CONFIG_PPC_BOOK3S_64
110 struct slb_entry *slb_ptr;
111 size_t size;
112#endif
Michael Ellerman8c4f1f22005-12-04 18:39:33 +1100113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int ibm_nmi_register = rtas_token("ibm,nmi-register");
115 if (ibm_nmi_register == RTAS_UNKNOWN_SERVICE)
116 return;
Michael Ellerman8c4f1f22005-12-04 18:39:33 +1100117
118 /* If the kernel's not linked at zero we point the firmware at low
119 * addresses anyway, and use a trampoline to get to the real code. */
120 system_reset_addr = __pa(system_reset_fwnmi) - PHYSICAL_START;
121 machine_check_addr = __pa(machine_check_fwnmi) - PHYSICAL_START;
122
123 if (0 == rtas_call(ibm_nmi_register, 2, 1, NULL, system_reset_addr,
124 machine_check_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 fwnmi_active = 1;
Mahesh Salgaonkar94675cc2018-07-04 23:27:21 +0530126
127 /*
128 * Allocate a chunk for per cpu buffer to hold rtas errorlog.
129 * It will be used in real mode mce handler, hence it needs to be
130 * below RMA.
131 */
Mike Rapoportf8067142019-03-07 16:30:48 -0800132 mce_data_buf = memblock_alloc_try_nid_raw(RTAS_ERROR_LOG_MAX * nr_cpus,
133 RTAS_ERROR_LOG_MAX, MEMBLOCK_LOW_LIMIT,
134 ppc64_rma_size, NUMA_NO_NODE);
135 if (!mce_data_buf)
136 panic("Failed to allocate %d bytes below %pa for MCE buffer\n",
137 RTAS_ERROR_LOG_MAX * nr_cpus, &ppc64_rma_size);
138
Mahesh Salgaonkar94675cc2018-07-04 23:27:21 +0530139 for_each_possible_cpu(i) {
140 paca_ptrs[i]->mce_data_buf = mce_data_buf +
141 (RTAS_ERROR_LOG_MAX * i);
142 }
Mahesh Salgaonkarc6d15252018-09-11 19:57:15 +0530143
144#ifdef CONFIG_PPC_BOOK3S_64
145 /* Allocate per cpu slb area to save old slb contents during MCE */
146 size = sizeof(struct slb_entry) * mmu_slb_size * nr_cpus;
Mike Rapoportf8067142019-03-07 16:30:48 -0800147 slb_ptr = memblock_alloc_try_nid_raw(size, sizeof(struct slb_entry),
148 MEMBLOCK_LOW_LIMIT, ppc64_rma_size,
149 NUMA_NO_NODE);
150 if (!slb_ptr)
151 panic("Failed to allocate %zu bytes below %pa for slb area\n",
152 size, &ppc64_rma_size);
153
Mahesh Salgaonkarc6d15252018-09-11 19:57:15 +0530154 for_each_possible_cpu(i)
155 paca_ptrs[i]->mce_faulty_slbs = slb_ptr + (mmu_slb_size * i);
156#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200159static void pseries_8259_cascade(struct irq_desc *desc)
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000160{
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100161 struct irq_chip *chip = irq_desc_get_chip(desc);
Olaf Hering35a84c22006-10-07 22:08:26 +1000162 unsigned int cascade_irq = i8259_irq();
Lennert Buytenhek79f26c22011-03-07 13:59:45 +0000163
Michael Ellermanef24ba72016-09-06 21:53:24 +1000164 if (cascade_irq)
David Howells7d12e782006-10-05 14:55:46 +0100165 generic_handle_irq(cascade_irq);
Lennert Buytenhek79f26c22011-03-07 13:59:45 +0000166
167 chip->irq_eoi(&desc->irq_data);
Benjamin Herrenschmidtb9e5b4e2006-07-03 19:32:51 +1000168}
169
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100170static void __init pseries_setup_i8259_cascade(void)
Michael Ellerman032ace72008-04-01 17:42:25 +1100171{
172 struct device_node *np, *old, *found = NULL;
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100173 unsigned int cascade;
Michael Ellerman032ace72008-04-01 17:42:25 +1100174 const u32 *addrp;
175 unsigned long intack = 0;
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100176 int naddr;
Michael Ellerman032ace72008-04-01 17:42:25 +1100177
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100178 for_each_node_by_type(np, "interrupt-controller") {
Michael Ellerman032ace72008-04-01 17:42:25 +1100179 if (of_device_is_compatible(np, "chrp,iic")) {
180 found = np;
181 break;
182 }
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100183 }
184
Michael Ellerman032ace72008-04-01 17:42:25 +1100185 if (found == NULL) {
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100186 printk(KERN_DEBUG "pic: no ISA interrupt controller\n");
Michael Ellerman032ace72008-04-01 17:42:25 +1100187 return;
188 }
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100189
Michael Ellerman032ace72008-04-01 17:42:25 +1100190 cascade = irq_of_parse_and_map(found, 0);
Michael Ellermanef24ba72016-09-06 21:53:24 +1000191 if (!cascade) {
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100192 printk(KERN_ERR "pic: failed to map cascade interrupt");
Michael Ellerman032ace72008-04-01 17:42:25 +1100193 return;
194 }
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100195 pr_debug("pic: cascade mapped to irq %d\n", cascade);
Michael Ellerman032ace72008-04-01 17:42:25 +1100196
197 for (old = of_node_get(found); old != NULL ; old = np) {
198 np = of_get_parent(old);
199 of_node_put(old);
200 if (np == NULL)
201 break;
Rob Herring2c8e65b2018-12-05 13:50:18 -0600202 if (!of_node_name_eq(np, "pci"))
Michael Ellerman032ace72008-04-01 17:42:25 +1100203 continue;
204 addrp = of_get_property(np, "8259-interrupt-acknowledge", NULL);
205 if (addrp == NULL)
206 continue;
207 naddr = of_n_addr_cells(np);
208 intack = addrp[naddr-1];
209 if (naddr > 1)
210 intack |= ((unsigned long)addrp[naddr-2]) << 32;
211 }
212 if (intack)
Michael Ellerman30d6ad22008-04-01 17:42:25 +1100213 printk(KERN_DEBUG "pic: PCI 8259 intack at 0x%016lx\n", intack);
Michael Ellerman032ace72008-04-01 17:42:25 +1100214 i8259_init(found, intack);
215 of_node_put(found);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100216 irq_set_chained_handler(cascade, pseries_8259_cascade);
Michael Ellerman032ace72008-04-01 17:42:25 +1100217}
218
Rashmica Guptae7da5da2016-05-30 16:18:11 +1000219static void __init pseries_init_irq(void)
Michael Ellerman032ace72008-04-01 17:42:25 +1100220{
Cédric Le Goatereac1e732017-08-30 21:46:11 +0200221 /* Try using a XIVE if available, otherwise use a XICS */
222 if (!xive_spapr_init()) {
223 xics_init();
224 pseries_setup_i8259_cascade();
225 }
Michael Ellerman032ace72008-04-01 17:42:25 +1100226}
227
Michael Ellerman180a3362005-08-09 11:13:36 +1000228static void pseries_lpar_enable_pmcs(void)
229{
230 unsigned long set, reset;
231
Michael Ellerman180a3362005-08-09 11:13:36 +1000232 set = 1UL << 63;
233 reset = 0;
234 plpar_hcall_norets(H_PERFMON, set, reset);
Michael Ellerman180a3362005-08-09 11:13:36 +1000235}
236
Grant Likelyf5242e52014-11-24 17:58:01 +0000237static int pci_dn_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
Kumar Gala2eb4afb2009-04-30 09:26:21 +0000238{
Grant Likelyf5242e52014-11-24 17:58:01 +0000239 struct of_reconfig_data *rd = data;
Gavin Shanea0f8ac2015-08-27 14:12:37 +1000240 struct device_node *parent, *np = rd->dn;
241 struct pci_dn *pdn;
Kumar Gala2eb4afb2009-04-30 09:26:21 +0000242 int err = NOTIFY_OK;
243
244 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000245 case OF_RECONFIG_ATTACH_NODE:
Gavin Shanea0f8ac2015-08-27 14:12:37 +1000246 parent = of_get_parent(np);
247 pdn = parent ? PCI_DN(parent) : NULL;
Gavin Shan8cc75812016-05-20 16:41:37 +1000248 if (pdn)
Gavin Shand8f66f42016-05-03 15:41:40 +1000249 pci_add_device_node_info(pdn->phb, np);
Gavin Shanea0f8ac2015-08-27 14:12:37 +1000250
251 of_node_put(parent);
Kumar Gala2eb4afb2009-04-30 09:26:21 +0000252 break;
Gavin Shan590c7562015-08-27 14:12:36 +1000253 case OF_RECONFIG_DETACH_NODE:
Gavin Shanea0f8ac2015-08-27 14:12:37 +1000254 pdn = PCI_DN(np);
255 if (pdn)
256 list_del(&pdn->list);
Gavin Shan590c7562015-08-27 14:12:36 +1000257 break;
Kumar Gala2eb4afb2009-04-30 09:26:21 +0000258 default:
259 err = NOTIFY_DONE;
260 break;
261 }
262 return err;
263}
264
265static struct notifier_block pci_dn_reconfig_nb = {
266 .notifier_call = pci_dn_reconfig_notifier,
267};
268
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000269struct kmem_cache *dtl_cache;
270
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200271#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000272/*
273 * Allocate space for the dispatch trace log for all possible cpus
274 * and register the buffers with the hypervisor. This is used for
275 * computing time stolen by the hypervisor.
276 */
277static int alloc_dispatch_logs(void)
278{
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000279 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
280 return 0;
281
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000282 if (!dtl_cache)
Nishanth Aravamudan127493d2011-04-13 19:45:59 +0000283 return 0;
Nishanth Aravamudan127493d2011-04-13 19:45:59 +0000284
Naveen N. Rao18a593c2019-07-03 22:34:01 +0530285 alloc_dtl_buffers(0);
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000286
287 /* Register the DTL for the current (boot) cpu */
Naveen N. Rao1c85a2a2019-07-03 22:33:56 +0530288 register_dtl_buffer(smp_processor_id());
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000289
290 return 0;
291}
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200292#else /* !CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000293static inline int alloc_dispatch_logs(void)
294{
295 return 0;
296}
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200297#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000298
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000299static int alloc_dispatch_log_kmem_cache(void)
300{
Anshuman Khanduald5394c02019-08-19 23:13:19 -0300301 void (*ctor)(void *) = get_dtl_cache_ctor();
302
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000303 dtl_cache = kmem_cache_create("dtl", DISPATCH_LOG_BYTES,
Anshuman Khanduald5394c02019-08-19 23:13:19 -0300304 DISPATCH_LOG_BYTES, 0, ctor);
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000305 if (!dtl_cache) {
306 pr_warn("Failed to create dispatch trace log buffer cache\n");
307 pr_warn("Stolen time statistics will be unreliable\n");
308 return 0;
309 }
310
311 return alloc_dispatch_logs();
312}
Michael Ellerman8e83e902014-07-16 12:02:43 +1000313machine_early_initcall(pseries, alloc_dispatch_log_kmem_cache);
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000314
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530315static void pseries_lpar_idle(void)
Deepthi Dharware1798162011-11-30 02:46:55 +0000316{
Nicolas Pitred8c6ad32014-01-29 12:45:10 -0500317 /*
318 * Default handler to go into low thread priority and possibly
Michael Ellerman027dfac2016-06-01 16:34:37 +1000319 * low power mode by ceding processor to hypervisor
Deepthi Dharware1798162011-11-30 02:46:55 +0000320 */
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530321
Nicolas Pitred8c6ad32014-01-29 12:45:10 -0500322 /* Indicate to hypervisor that we are idle. */
323 get_lppaca()->idle = 1;
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530324
Nicolas Pitred8c6ad32014-01-29 12:45:10 -0500325 /*
326 * Yield the processor to the hypervisor. We return if
327 * an external interrupt occurs (which are driven prior
328 * to returning here) or if a prod occurs from another
329 * processor. When returning here, external interrupts
330 * are enabled.
331 */
332 cede_processor();
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530333
Nicolas Pitred8c6ad32014-01-29 12:45:10 -0500334 get_lppaca()->idle = 0;
Deepthi Dharware1798162011-11-30 02:46:55 +0000335}
336
Ian Munsiefc8effa2012-11-08 16:03:14 +1100337/*
338 * Enable relocation on during exceptions. This has partition wide scope and
339 * may take a while to complete, if it takes longer than one second we will
340 * just give up rather than wasting any more time on this - if that turns out
341 * to ever be a problem in practice we can move this into a kernel thread to
342 * finish off the process later in boot.
343 */
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000344void pseries_enable_reloc_on_exc(void)
Ian Munsiefc8effa2012-11-08 16:03:14 +1100345{
346 long rc;
347 unsigned int delay, total_delay = 0;
348
349 while (1) {
350 rc = enable_reloc_on_exceptions();
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000351 if (!H_IS_LONG_BUSY(rc)) {
352 if (rc == H_P2) {
353 pr_info("Relocation on exceptions not"
354 " supported\n");
355 } else if (rc != H_SUCCESS) {
356 pr_warn("Unable to enable relocation"
357 " on exceptions: %ld\n", rc);
358 }
359 break;
360 }
Ian Munsiefc8effa2012-11-08 16:03:14 +1100361
362 delay = get_longbusy_msecs(rc);
363 total_delay += delay;
364 if (total_delay > 1000) {
365 pr_warn("Warning: Giving up waiting to enable "
366 "relocation on exceptions (%u msec)!\n",
367 total_delay);
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000368 return;
Ian Munsiefc8effa2012-11-08 16:03:14 +1100369 }
370
371 mdelay(delay);
372 }
373}
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000374EXPORT_SYMBOL(pseries_enable_reloc_on_exc);
Ian Munsiefc8effa2012-11-08 16:03:14 +1100375
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000376void pseries_disable_reloc_on_exc(void)
Ian Munsiecedddd82012-11-08 16:40:28 +1100377{
378 long rc;
379
380 while (1) {
381 rc = disable_reloc_on_exceptions();
382 if (!H_IS_LONG_BUSY(rc))
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000383 break;
Ian Munsiecedddd82012-11-08 16:40:28 +1100384 mdelay(get_longbusy_msecs(rc));
385 }
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000386 if (rc != H_SUCCESS)
Joe Perchesf2c2cbc2016-10-24 21:00:08 -0700387 pr_warn("Warning: Failed to disable relocation on exceptions: %ld\n",
388 rc);
Ian Munsiecedddd82012-11-08 16:40:28 +1100389}
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000390EXPORT_SYMBOL(pseries_disable_reloc_on_exc);
Ian Munsiecedddd82012-11-08 16:40:28 +1100391
Thiago Jung Bauermannda665882016-11-29 23:45:50 +1100392#ifdef CONFIG_KEXEC_CORE
Ian Munsiecedddd82012-11-08 16:40:28 +1100393static void pSeries_machine_kexec(struct kimage *image)
394{
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000395 if (firmware_has_feature(FW_FEATURE_SET_MODE))
396 pseries_disable_reloc_on_exc();
Ian Munsiecedddd82012-11-08 16:40:28 +1100397
398 default_machine_kexec(image);
399}
400#endif
401
Anton Blancharde844b1e2013-11-20 22:14:59 +1100402#ifdef __LITTLE_ENDIAN__
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000403void pseries_big_endian_exceptions(void)
Anton Blancharde844b1e2013-11-20 22:14:59 +1100404{
405 long rc;
406
407 while (1) {
408 rc = enable_big_endian_exceptions();
409 if (!H_IS_LONG_BUSY(rc))
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000410 break;
Anton Blancharde844b1e2013-11-20 22:14:59 +1100411 mdelay(get_longbusy_msecs(rc));
412 }
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000413
414 /*
415 * At this point it is unlikely panic() will get anything
416 * out to the user, since this is called very late in kexec
417 * but at least this will stop us from continuing on further
418 * and creating an even more difficult to debug situation.
419 *
420 * There is a known problem when kdump'ing, if cpus are offline
421 * the above call will fail. Rather than panicking again, keep
422 * going and hope the kdump kernel is also little endian, which
423 * it usually is.
424 */
425 if (rc && !kdump_in_progress())
426 panic("Could not enable big endian exceptions");
Anton Blancharde844b1e2013-11-20 22:14:59 +1100427}
428
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000429void pseries_little_endian_exceptions(void)
Anton Blancharde844b1e2013-11-20 22:14:59 +1100430{
431 long rc;
432
433 while (1) {
434 rc = enable_little_endian_exceptions();
435 if (!H_IS_LONG_BUSY(rc))
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000436 break;
Anton Blancharde844b1e2013-11-20 22:14:59 +1100437 mdelay(get_longbusy_msecs(rc));
438 }
Benjamin Herrenschmidtd3cbff12016-07-05 15:03:49 +1000439 if (rc) {
440 ppc_md.progress("H_SET_MODE LE exception fail", 0);
441 panic("Could not enable little endian exceptions");
442 }
Anton Blancharde844b1e2013-11-20 22:14:59 +1100443}
444#endif
445
Daniel Axtensbdc728a2015-03-31 16:00:39 +1100446static void __init find_and_init_phbs(void)
447{
448 struct device_node *node;
449 struct pci_controller *phb;
450 struct device_node *root = of_find_node_by_path("/");
451
452 for_each_child_of_node(root, node) {
Rob Herringe5480bd2018-11-16 16:11:00 -0600453 if (!of_node_is_type(node, "pci") &&
454 !of_node_is_type(node, "pciex"))
Daniel Axtensbdc728a2015-03-31 16:00:39 +1100455 continue;
456
457 phb = pcibios_alloc_controller(node);
458 if (!phb)
459 continue;
460 rtas_setup_phb(phb);
461 pci_process_bridge_OF_ranges(phb, node, 0);
462 isa_bridge_find_early(phb);
Daniel Axtens38ae9ec2015-03-31 16:00:50 +1100463 phb->controller_ops = pseries_pci_controller_ops;
Daniel Axtensbdc728a2015-03-31 16:00:39 +1100464 }
465
466 of_node_put(root);
Daniel Axtensbdc728a2015-03-31 16:00:39 +1100467
468 /*
469 * PCI_PROBE_ONLY and PCI_REASSIGN_ALL_BUS can be set via properties
470 * in chosen.
471 */
Marc Zyngier705a7b42015-09-04 17:50:10 +0100472 of_pci_check_probe_only();
Daniel Axtensbdc728a2015-03-31 16:00:39 +1100473}
474
Michael Ellermanf636c142018-03-27 23:01:46 +1100475static void init_cpu_char_feature_flags(struct h_cpu_char_result *result)
476{
Mauricio Faria de Oliveira6232774f2018-03-30 14:28:25 -0300477 /*
478 * The features below are disabled by default, so we instead look to see
479 * if firmware has *enabled* them, and set them if so.
480 */
Michael Ellermanf636c142018-03-27 23:01:46 +1100481 if (result->character & H_CPU_CHAR_SPEC_BAR_ORI31)
482 security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
483
484 if (result->character & H_CPU_CHAR_BCCTRL_SERIALISED)
485 security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
486
487 if (result->character & H_CPU_CHAR_L1D_FLUSH_ORI30)
488 security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
489
490 if (result->character & H_CPU_CHAR_L1D_FLUSH_TRIG2)
491 security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
492
493 if (result->character & H_CPU_CHAR_L1D_THREAD_PRIV)
494 security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
495
496 if (result->character & H_CPU_CHAR_COUNT_CACHE_DISABLED)
497 security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
498
Michael Ellermanba72dc12018-07-24 01:07:55 +1000499 if (result->character & H_CPU_CHAR_BCCTR_FLUSH_ASSIST)
500 security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);
501
502 if (result->behaviour & H_CPU_BEHAV_FLUSH_COUNT_CACHE)
503 security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);
504
Michael Ellermanf636c142018-03-27 23:01:46 +1100505 /*
506 * The features below are enabled by default, so we instead look to see
507 * if firmware has *disabled* them, and clear them if so.
508 */
Mauricio Faria de Oliveira0f9bdfe2018-03-29 15:32:11 -0300509 if (!(result->behaviour & H_CPU_BEHAV_FAVOUR_SECURITY))
Michael Ellermanf636c142018-03-27 23:01:46 +1100510 security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
511
Mauricio Faria de Oliveira0f9bdfe2018-03-29 15:32:11 -0300512 if (!(result->behaviour & H_CPU_BEHAV_L1D_FLUSH_PR))
Michael Ellermanf636c142018-03-27 23:01:46 +1100513 security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
514
Mauricio Faria de Oliveira0f9bdfe2018-03-29 15:32:11 -0300515 if (!(result->behaviour & H_CPU_BEHAV_BNDS_CHK_SPEC_BAR))
Michael Ellermanf636c142018-03-27 23:01:46 +1100516 security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
517}
518
Michael Ellerman921bc6c2018-03-14 19:40:42 -0300519void pseries_setup_rfi_flush(void)
Michael Neuling8989d562018-01-10 03:07:15 +1100520{
521 struct h_cpu_char_result result;
522 enum l1d_flush_type types;
523 bool enable;
524 long rc;
525
Mauricio Faria de Oliveira6232774f2018-03-30 14:28:25 -0300526 /*
527 * Set features to the defaults assumed by init_cpu_char_feature_flags()
528 * so it can set/clear again any features that might have changed after
529 * migration, and in case the hypercall fails and it is not even called.
530 */
531 powerpc_security_features = SEC_FTR_DEFAULT;
532
Michael Neuling8989d562018-01-10 03:07:15 +1100533 rc = plpar_get_cpu_characteristics(&result);
Michael Ellerman2e4a1612018-03-27 23:01:51 +1100534 if (rc == H_SUCCESS)
Michael Ellermanf636c142018-03-27 23:01:46 +1100535 init_cpu_char_feature_flags(&result);
536
Michael Ellermanf636c142018-03-27 23:01:46 +1100537 /*
538 * We're the guest so this doesn't apply to us, clear it to simplify
539 * handling of it elsewhere.
540 */
541 security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
542
Michael Ellerman2e4a1612018-03-27 23:01:51 +1100543 types = L1D_FLUSH_FALLBACK;
544
545 if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
546 types |= L1D_FLUSH_MTTRIG;
547
548 if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
549 types |= L1D_FLUSH_ORI;
550
551 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
552 security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR);
553
Michael Neuling8989d562018-01-10 03:07:15 +1100554 setup_rfi_flush(types, enable);
Michael Ellermanba72dc12018-07-24 01:07:55 +1000555 setup_count_cache_flush();
Michael Neuling8989d562018-01-10 03:07:15 +1100556}
557
Bryant G. Lyfc5f6222018-01-05 10:45:52 -0600558#ifdef CONFIG_PCI_IOV
559enum rtas_iov_fw_value_map {
560 NUM_RES_PROPERTY = 0, /* Number of Resources */
561 LOW_INT = 1, /* Lowest 32 bits of Address */
562 START_OF_ENTRIES = 2, /* Always start of entry */
563 APERTURE_PROPERTY = 2, /* Start of entry+ to Aperture Size */
564 WDW_SIZE_PROPERTY = 4, /* Start of entry+ to Window Size */
565 NEXT_ENTRY = 7 /* Go to next entry on array */
566};
567
568enum get_iov_fw_value_index {
569 BAR_ADDRS = 1, /* Get Bar Address */
570 APERTURE_SIZE = 2, /* Get Aperture Size */
571 WDW_SIZE = 3 /* Get Window Size */
572};
573
574resource_size_t pseries_get_iov_fw_value(struct pci_dev *dev, int resno,
575 enum get_iov_fw_value_index value)
576{
577 const int *indexes;
578 struct device_node *dn = pci_device_to_OF_node(dev);
579 int i, num_res, ret = 0;
580
581 indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
582 if (!indexes)
583 return 0;
584
585 /*
586 * First element in the array is the number of Bars
587 * returned. Search through the list to find the matching
588 * bar
589 */
590 num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
591 if (resno >= num_res)
592 return 0; /* or an errror */
593
594 i = START_OF_ENTRIES + NEXT_ENTRY * resno;
595 switch (value) {
596 case BAR_ADDRS:
597 ret = of_read_number(&indexes[i], 2);
598 break;
599 case APERTURE_SIZE:
600 ret = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
601 break;
602 case WDW_SIZE:
603 ret = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
604 break;
605 }
606
607 return ret;
608}
609
610void of_pci_set_vf_bar_size(struct pci_dev *dev, const int *indexes)
611{
612 struct resource *res;
613 resource_size_t base, size;
614 int i, r, num_res;
615
616 num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
617 num_res = min_t(int, num_res, PCI_SRIOV_NUM_BARS);
618 for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
619 i += NEXT_ENTRY, r++) {
620 res = &dev->resource[r + PCI_IOV_RESOURCES];
621 base = of_read_number(&indexes[i], 2);
622 size = of_read_number(&indexes[i + APERTURE_PROPERTY], 2);
623 res->flags = pci_parse_of_flags(of_read_number
624 (&indexes[i + LOW_INT], 1), 0);
625 res->flags |= (IORESOURCE_MEM_64 | IORESOURCE_PCI_FIXED);
626 res->name = pci_name(dev);
627 res->start = base;
628 res->end = base + size - 1;
629 }
630}
631
632void of_pci_parse_iov_addrs(struct pci_dev *dev, const int *indexes)
633{
634 struct resource *res, *root, *conflict;
635 resource_size_t base, size;
636 int i, r, num_res;
637
638 /*
639 * First element in the array is the number of Bars
640 * returned. Search through the list to find the matching
641 * bars assign them from firmware into resources structure.
642 */
643 num_res = of_read_number(&indexes[NUM_RES_PROPERTY], 1);
644 for (i = START_OF_ENTRIES, r = 0; r < num_res && r < PCI_SRIOV_NUM_BARS;
645 i += NEXT_ENTRY, r++) {
646 res = &dev->resource[r + PCI_IOV_RESOURCES];
647 base = of_read_number(&indexes[i], 2);
648 size = of_read_number(&indexes[i + WDW_SIZE_PROPERTY], 2);
649 res->name = pci_name(dev);
650 res->start = base;
651 res->end = base + size - 1;
652 root = &iomem_resource;
653 dev_dbg(&dev->dev,
654 "pSeries IOV BAR %d: trying firmware assignment %pR\n",
655 r + PCI_IOV_RESOURCES, res);
656 conflict = request_resource_conflict(root, res);
657 if (conflict) {
658 dev_info(&dev->dev,
659 "BAR %d: %pR conflicts with %s %pR\n",
660 r + PCI_IOV_RESOURCES, res,
661 conflict->name, conflict);
662 res->flags |= IORESOURCE_UNSET;
663 }
664 }
665}
666
Sam Bobroffb87b9cf2018-07-30 11:59:14 +1000667static void pseries_disable_sriov_resources(struct pci_dev *pdev)
668{
669 int i;
670
671 pci_warn(pdev, "No hypervisor support for SR-IOV on this device, IOV BARs disabled.\n");
672 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
673 pdev->resource[i + PCI_IOV_RESOURCES].flags = 0;
674}
675
Bryant G. Lyfc5f6222018-01-05 10:45:52 -0600676static void pseries_pci_fixup_resources(struct pci_dev *pdev)
677{
678 const int *indexes;
679 struct device_node *dn = pci_device_to_OF_node(pdev);
680
681 /*Firmware must support open sriov otherwise dont configure*/
682 indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
Sam Bobroffb87b9cf2018-07-30 11:59:14 +1000683 if (indexes)
684 of_pci_set_vf_bar_size(pdev, indexes);
685 else
686 pseries_disable_sriov_resources(pdev);
Bryant G. Lyfc5f6222018-01-05 10:45:52 -0600687}
688
689static void pseries_pci_fixup_iov_resources(struct pci_dev *pdev)
690{
691 const int *indexes;
692 struct device_node *dn = pci_device_to_OF_node(pdev);
693
Hari Vyas44bda4b2018-07-03 14:35:41 +0530694 if (!pdev->is_physfn || pci_dev_is_added(pdev))
Bryant G. Lyfc5f6222018-01-05 10:45:52 -0600695 return;
696 /*Firmware must support open sriov otherwise dont configure*/
697 indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);
Sam Bobroffb87b9cf2018-07-30 11:59:14 +1000698 if (indexes)
699 of_pci_parse_iov_addrs(pdev, indexes);
700 else
701 pseries_disable_sriov_resources(pdev);
Bryant G. Lyfc5f6222018-01-05 10:45:52 -0600702}
703
704static resource_size_t pseries_pci_iov_resource_alignment(struct pci_dev *pdev,
705 int resno)
706{
707 const __be32 *reg;
708 struct device_node *dn = pci_device_to_OF_node(pdev);
709
710 /*Firmware must support open sriov otherwise report regular alignment*/
711 reg = of_get_property(dn, "ibm,is-open-sriov-pf", NULL);
712 if (!reg)
713 return pci_iov_resource_size(pdev, resno);
714
715 if (!pdev->is_physfn)
716 return 0;
717 return pseries_get_iov_fw_value(pdev,
718 resno - PCI_IOV_RESOURCES,
719 APERTURE_SIZE);
720}
721#endif
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723static void __init pSeries_setup_arch(void)
724{
Jason Baronb71d47c2013-11-25 23:23:11 +0000725 set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
Anton Blancharda9349042011-11-30 00:23:14 +0000726
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000727 /* Discover PIC type and setup ppc_md accordingly */
Rashmica Gupta86425be2016-05-30 16:18:12 +1000728 smp_init_pseries();
Rashmica Guptae7da5da2016-05-30 16:18:11 +1000729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* openpic global configuration register (64-bit format). */
732 /* openpic Interrupt Source Unit pointer (64-bit format). */
733 /* python0 facility area (mmio) (64-bit format) REAL address. */
734
735 /* init to some ~sane value until calibrate_delay() runs */
736 loops_per_jiffy = 50000000;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 fwnmi_init();
739
Michael Neuling8989d562018-01-10 03:07:15 +1100740 pseries_setup_rfi_flush();
Nicholas Piggina048a072018-05-22 09:00:00 +1000741 setup_stf_barrier();
Michael Neuling8989d562018-01-10 03:07:15 +1100742
Adam Buchbinder446957b2016-02-24 10:51:11 -0800743 /* By default, only probe PCI (can be overridden by rtas_pci) */
Bjorn Helgaas673c9752012-02-23 20:18:58 -0700744 pci_add_flags(PCI_PROBE_ONLY);
Bjorn Helgaas3c13be02012-02-23 20:18:58 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* Find and initialize PCI host bridges */
747 init_pci_config_tokens();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 find_and_init_phbs();
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000749 of_reconfig_notifier_register(&pci_dn_reconfig_nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 pSeries_nvram_init();
752
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530753 if (firmware_has_feature(FW_FEATURE_LPAR)) {
Stephen Rothwell8d15a3e2005-08-03 14:40:16 +1000754 vpa_init(boot_cpuid);
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530755 ppc_md.power_save = pseries_lpar_idle;
Michael Ellerman180a3362005-08-09 11:13:36 +1000756 ppc_md.enable_pmcs = pseries_lpar_enable_pmcs;
Bryant G. Lyfc5f6222018-01-05 10:45:52 -0600757#ifdef CONFIG_PCI_IOV
758 ppc_md.pcibios_fixup_resources =
759 pseries_pci_fixup_resources;
760 ppc_md.pcibios_fixup_sriov =
761 pseries_pci_fixup_iov_resources;
762 ppc_md.pcibios_iov_resource_alignment =
763 pseries_pci_iov_resource_alignment;
764#endif
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530765 } else {
766 /* No special idle routine */
Michael Ellerman180a3362005-08-09 11:13:36 +1000767 ppc_md.enable_pmcs = power4_enable_pmcs;
Vaidyanathan Srinivasan363edbe2013-09-06 00:25:06 +0530768 }
Ian Munsiefc8effa2012-11-08 16:03:14 +1100769
Kleber Sacilotto de Souzad82fb312013-05-03 12:43:12 +0000770 ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare;
Alexey Kardashevskiy1a047cc2019-05-07 16:25:59 +1000771
772 if (swiotlb_force == SWIOTLB_FORCE)
773 ppc_swiotlb_enable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Nicholas Piggin35adacd2017-12-24 02:49:23 +1000776static void pseries_panic(char *str)
777{
778 panic_flush_kmsg_end();
779 rtas_os_term(str);
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static int __init pSeries_init_panel(void)
783{
784 /* Manually leave the kernel version on the panel. */
Tony Breeds983d8a62014-03-12 19:17:07 +1100785#ifdef __BIG_ENDIAN__
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 ppc_md.progress("Linux ppc64\n", 0);
Tony Breeds983d8a62014-03-12 19:17:07 +1100787#else
788 ppc_md.progress("Linux ppc64le\n", 0);
789#endif
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700790 ppc_md.progress(init_utsname()->version, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 return 0;
793}
Benjamin Herrenschmidtf86d6b92011-03-31 18:49:45 +0000794machine_arch_initcall(pseries, pSeries_init_panel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Michael Neuling4474ef02012-09-06 21:24:56 +0000796static int pseries_set_dabr(unsigned long dabr, unsigned long dabrx)
Michael Ellermancab0af92005-11-03 15:30:49 +1100797{
Michael Ellerman76032de2005-11-07 13:12:03 +1100798 return plpar_hcall_norets(H_SET_DABR, dabr);
Michael Ellermancab0af92005-11-03 15:30:49 +1100799}
800
Michael Neuling4474ef02012-09-06 21:24:56 +0000801static int pseries_set_xdabr(unsigned long dabr, unsigned long dabrx)
Michael Ellerman76032de2005-11-07 13:12:03 +1100802{
Michael Neuling4474ef02012-09-06 21:24:56 +0000803 /* Have to set at least one bit in the DABRX according to PAPR */
804 if (dabrx == 0 && dabr == 0)
805 dabrx = DABRX_USER;
806 /* PAPR says we can only set kernel and user bits */
Michael Neulingcd144572012-09-06 21:24:57 +0000807 dabrx &= DABRX_KERNEL | DABRX_USER;
Michael Neuling4474ef02012-09-06 21:24:56 +0000808
809 return plpar_hcall_norets(H_SET_XDABR, dabr, dabrx);
Michael Ellerman76032de2005-11-07 13:12:03 +1100810}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Michael Neulingbf99de32012-12-20 14:06:45 +0000812static int pseries_set_dawr(unsigned long dawr, unsigned long dawrx)
813{
814 /* PAPR says we can't set HYP */
815 dawrx &= ~DAWRX_HYP;
816
Michael Ellerman7c09c182018-03-08 13:54:41 +1100817 return plpar_set_watchpoint0(dawr, dawrx);
Michael Neulingbf99de32012-12-20 14:06:45 +0000818}
819
Robert Jenningse46de422008-07-24 04:29:03 +1000820#define CMO_CHARACTERISTICS_TOKEN 44
821#define CMO_MAXLENGTH 1026
822
Brian King9ee820f2011-05-04 16:01:20 +1000823void pSeries_coalesce_init(void)
824{
825 struct hvcall_mpp_x_data mpp_x_data;
826
827 if (firmware_has_feature(FW_FEATURE_CMO) && !h_get_mpp_x(&mpp_x_data))
828 powerpc_firmware_features |= FW_FEATURE_XCMO;
829 else
830 powerpc_firmware_features &= ~FW_FEATURE_XCMO;
831}
832
Robert Jenningse46de422008-07-24 04:29:03 +1000833/**
834 * fw_cmo_feature_init - FW_FEATURE_CMO is not stored in ibm,hypertas-functions,
835 * handle that here. (Stolen from parse_system_parameter_string)
836 */
Anton Blancharde51df2c2014-08-20 08:55:18 +1000837static void pSeries_cmo_feature_init(void)
Robert Jenningse46de422008-07-24 04:29:03 +1000838{
839 char *ptr, *key, *value, *end;
840 int call_status;
Alistair Popplee589a4402013-12-09 18:17:01 +1100841 int page_order = IOMMU_PAGE_SHIFT_4K;
Robert Jenningse46de422008-07-24 04:29:03 +1000842
843 pr_debug(" -> fw_cmo_feature_init()\n");
844 spin_lock(&rtas_data_buf_lock);
845 memset(rtas_data_buf, 0, RTAS_DATA_BUF_SIZE);
846 call_status = rtas_call(rtas_token("ibm,get-system-parameter"), 3, 1,
847 NULL,
848 CMO_CHARACTERISTICS_TOKEN,
849 __pa(rtas_data_buf),
850 RTAS_DATA_BUF_SIZE);
851
852 if (call_status != 0) {
853 spin_unlock(&rtas_data_buf_lock);
854 pr_debug("CMO not available\n");
855 pr_debug(" <- fw_cmo_feature_init()\n");
856 return;
857 }
858
859 end = rtas_data_buf + CMO_MAXLENGTH - 2;
860 ptr = rtas_data_buf + 2; /* step over strlen value */
861 key = value = ptr;
862
863 while (*ptr && (ptr <= end)) {
864 /* Separate the key and value by replacing '=' with '\0' and
865 * point the value at the string after the '='
866 */
867 if (ptr[0] == '=') {
868 ptr[0] = '\0';
869 value = ptr + 1;
870 } else if (ptr[0] == '\0' || ptr[0] == ',') {
871 /* Terminate the string containing the key/value pair */
872 ptr[0] = '\0';
873
874 if (key == value) {
875 pr_debug("Malformed key/value pair\n");
876 /* Never found a '=', end processing */
877 break;
878 }
879
Robert Jennings81f14992008-08-16 05:07:31 +1000880 if (0 == strcmp(key, "CMOPageSize"))
881 page_order = simple_strtol(value, NULL, 10);
882 else if (0 == strcmp(key, "PrPSP"))
883 CMO_PrPSP = simple_strtol(value, NULL, 10);
Robert Jenningse46de422008-07-24 04:29:03 +1000884 else if (0 == strcmp(key, "SecPSP"))
Robert Jennings81f14992008-08-16 05:07:31 +1000885 CMO_SecPSP = simple_strtol(value, NULL, 10);
Robert Jenningse46de422008-07-24 04:29:03 +1000886 value = key = ptr + 1;
887 }
888 ptr++;
889 }
890
Robert Jennings81f14992008-08-16 05:07:31 +1000891 /* Page size is returned as the power of 2 of the page size,
892 * convert to the page size in bytes before returning
893 */
894 CMO_PageSize = 1 << page_order;
895 pr_debug("CMO_PageSize = %lu\n", CMO_PageSize);
896
897 if (CMO_PrPSP != -1 || CMO_SecPSP != -1) {
Robert Jenningse46de422008-07-24 04:29:03 +1000898 pr_info("CMO enabled\n");
Robert Jennings81f14992008-08-16 05:07:31 +1000899 pr_debug("CMO enabled, PrPSP=%d, SecPSP=%d\n", CMO_PrPSP,
900 CMO_SecPSP);
Robert Jenningse46de422008-07-24 04:29:03 +1000901 powerpc_firmware_features |= FW_FEATURE_CMO;
Brian King9ee820f2011-05-04 16:01:20 +1000902 pSeries_coalesce_init();
Robert Jenningse46de422008-07-24 04:29:03 +1000903 } else
Robert Jennings81f14992008-08-16 05:07:31 +1000904 pr_debug("CMO not enabled, PrPSP=%d, SecPSP=%d\n", CMO_PrPSP,
905 CMO_SecPSP);
Robert Jenningse46de422008-07-24 04:29:03 +1000906 spin_unlock(&rtas_data_buf_lock);
907 pr_debug(" <- fw_cmo_feature_init()\n");
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
911 * Early initialization. Relocation is on but do not reference unbolted pages
912 */
Benjamin Herrenschmidtf2d57692016-07-05 15:04:06 +1000913static void __init pseries_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Benjamin Herrenschmidtf2d57692016-07-05 15:04:06 +1000915 pr_debug(" -> pseries_init()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000917#ifdef CONFIG_HVC_CONSOLE
Michael Ellerman57cfb812006-03-21 20:45:59 +1100918 if (firmware_has_feature(FW_FEATURE_LPAR))
Benjamin Herrenschmidt4d2bb3f2011-05-12 13:46:38 +1000919 hvc_vio_init_early();
920#endif
Michael Neuling06c88762012-09-05 19:17:49 +0000921 if (firmware_has_feature(FW_FEATURE_XDABR))
Michael Ellerman76032de2005-11-07 13:12:03 +1100922 ppc_md.set_dabr = pseries_set_xdabr;
Michael Neuling06c88762012-09-05 19:17:49 +0000923 else if (firmware_has_feature(FW_FEATURE_DABR))
924 ppc_md.set_dabr = pseries_set_dabr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Michael Neulingbf99de32012-12-20 14:06:45 +0000926 if (firmware_has_feature(FW_FEATURE_SET_MODE))
927 ppc_md.set_dawr = pseries_set_dawr;
928
Robert Jenningse46de422008-07-24 04:29:03 +1000929 pSeries_cmo_feature_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 iommu_init_early_pSeries();
931
Benjamin Herrenschmidtf2d57692016-07-05 15:04:06 +1000932 pr_debug(" <- pseries_init()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Alexander Graf9178ba22014-10-13 16:01:09 +0200935/**
936 * pseries_power_off - tell firmware about how to power off the system.
937 *
938 * This function calls either the power-off rtas token in normal cases
939 * or the ibm,power-off-ups token (if present & requested) in case of
940 * a power failure. If power-off token is used, power on will only be
941 * possible with power button press. If ibm,power-off-ups token is used
942 * it will allow auto poweron after power is restored.
943 */
944static void pseries_power_off(void)
945{
946 int rc;
947 int rtas_poweroff_ups_token = rtas_token("ibm,power-off-ups");
948
949 if (rtas_flash_term_hook)
950 rtas_flash_term_hook(SYS_POWER_OFF);
951
952 if (rtas_poweron_auto == 0 ||
953 rtas_poweroff_ups_token == RTAS_UNKNOWN_SERVICE) {
954 rc = rtas_call(rtas_token("power-off"), 2, 1, NULL, -1, -1);
955 printk(KERN_INFO "RTAS power-off returned %d\n", rc);
956 } else {
957 rc = rtas_call(rtas_poweroff_ups_token, 0, 1, NULL);
958 printk(KERN_INFO "RTAS ibm,power-off-ups returned %d\n", rc);
959 }
960 for (;;);
961}
962
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100963static int __init pSeries_probe(void)
964{
Rob Herringe5480bd2018-11-16 16:11:00 -0600965 if (!of_node_is_type(of_root, "chrp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return 0;
967
Arnd Bergmann133dda12006-06-07 12:04:18 +1000968 /* Cell blades firmware claims to be chrp while it's not. Until this
969 * is fixed, we need to avoid those here.
970 */
Benjamin Herrenschmidt406b0b62016-07-05 15:04:00 +1000971 if (of_machine_is_compatible("IBM,CPBW-1.0") ||
972 of_machine_is_compatible("IBM,CBEA"))
Arnd Bergmann133dda12006-06-07 12:04:18 +1000973 return 0;
974
Alexander Graf9178ba22014-10-13 16:01:09 +0200975 pm_power_off = pseries_power_off;
976
Michael Ellermanf7ebf352008-04-24 15:13:19 +1000977 pr_debug("Machine is%s LPAR !\n",
978 (powerpc_firmware_features & FW_FEATURE_LPAR) ? "" : " not");
Michael Ellerman57cfb812006-03-21 20:45:59 +1100979
Benjamin Herrenschmidtf2d57692016-07-05 15:04:06 +1000980 pseries_init();
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return 1;
983}
984
Paul Mackerras42672922005-09-12 17:17:36 +1000985static int pSeries_pci_probe_mode(struct pci_bus *bus)
986{
Michael Ellerman57cfb812006-03-21 20:45:59 +1100987 if (firmware_has_feature(FW_FEATURE_LPAR))
Paul Mackerras42672922005-09-12 17:17:36 +1000988 return PCI_PROBE_DEVTREE;
989 return PCI_PROBE_NORMAL;
990}
991
Daniel Axtens38ae9ec2015-03-31 16:00:50 +1100992struct pci_controller_ops pseries_pci_controller_ops = {
993 .probe_mode = pSeries_pci_probe_mode,
994};
995
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100996define_machine(pseries) {
997 .name = "pSeries",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 .probe = pSeries_probe,
999 .setup_arch = pSeries_setup_arch,
Rashmica Guptae7da5da2016-05-30 16:18:11 +10001000 .init_IRQ = pseries_init_irq,
Paul Mackerras0dd194d2005-10-20 20:48:19 +10001001 .show_cpuinfo = pSeries_show_cpuinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 .log_error = pSeries_log_error,
1003 .pcibios_fixup = pSeries_final_fixup,
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +11001004 .restart = rtas_restart,
Paul Mackerrasf4fcbbe2005-11-03 14:41:19 +11001005 .halt = rtas_halt,
Nicholas Piggin35adacd2017-12-24 02:49:23 +10001006 .panic = pseries_panic,
Arnd Bergmann773bf9c2005-06-23 09:43:18 +10001007 .get_boot_time = rtas_get_boot_time,
1008 .get_rtc_time = rtas_get_rtc_time,
1009 .set_rtc_time = rtas_set_rtc_time,
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +10001010 .calibrate_decr = generic_calibrate_decr,
Arnd Bergmann6566c6f2005-06-23 09:43:28 +10001011 .progress = rtas_progress,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .system_reset_exception = pSeries_system_reset_exception,
Mahesh Salgaonkara43c1592018-09-11 19:57:00 +05301013 .machine_check_early = pseries_machine_check_realmode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 .machine_check_exception = pSeries_machine_check_exception,
Thiago Jung Bauermannda665882016-11-29 23:45:50 +11001015#ifdef CONFIG_KEXEC_CORE
Ian Munsiecedddd82012-11-08 16:40:28 +11001016 .machine_kexec = pSeries_machine_kexec,
Rashmica Guptad739d2c2016-05-30 16:18:13 +10001017 .kexec_cpu_down = pseries_kexec_cpu_down,
Ian Munsiecedddd82012-11-08 16:40:28 +11001018#endif
Anton Blancharda5d86252014-06-04 17:50:47 +10001019#ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
1020 .memory_block_size = pseries_memory_block_size,
1021#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022};