x86/xen: resume timer irqs early
[linux/fpc-iii.git] / arch / powerpc / platforms / powernv / eeh-powernv.c
blob79663d26e6eaa1de8749c6994316737f991106ab
1 /*
2 * The file intends to implement the platform dependent EEH operations on
3 * powernv platform. Actually, the powernv was created in order to fully
4 * hypervisor support.
6 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2013.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/atomic.h>
15 #include <linux/delay.h>
16 #include <linux/export.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/msi.h>
20 #include <linux/of.h>
21 #include <linux/pci.h>
22 #include <linux/proc_fs.h>
23 #include <linux/rbtree.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/spinlock.h>
28 #include <asm/eeh.h>
29 #include <asm/eeh_event.h>
30 #include <asm/firmware.h>
31 #include <asm/io.h>
32 #include <asm/iommu.h>
33 #include <asm/machdep.h>
34 #include <asm/msi_bitmap.h>
35 #include <asm/opal.h>
36 #include <asm/ppc-pci.h>
38 #include "powernv.h"
39 #include "pci.h"
41 /**
42 * powernv_eeh_init - EEH platform dependent initialization
44 * EEH platform dependent initialization on powernv
46 static int powernv_eeh_init(void)
48 /* We require OPALv3 */
49 if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
50 pr_warning("%s: OPALv3 is required !\n", __func__);
51 return -EINVAL;
54 /* Set EEH probe mode */
55 eeh_probe_mode_set(EEH_PROBE_MODE_DEV);
57 return 0;
60 /**
61 * powernv_eeh_post_init - EEH platform dependent post initialization
63 * EEH platform dependent post initialization on powernv. When
64 * the function is called, the EEH PEs and devices should have
65 * been built. If the I/O cache staff has been built, EEH is
66 * ready to supply service.
68 static int powernv_eeh_post_init(void)
70 struct pci_controller *hose;
71 struct pnv_phb *phb;
72 int ret = 0;
74 list_for_each_entry(hose, &hose_list, list_node) {
75 phb = hose->private_data;
77 if (phb->eeh_ops && phb->eeh_ops->post_init) {
78 ret = phb->eeh_ops->post_init(hose);
79 if (ret)
80 break;
84 return ret;
87 /**
88 * powernv_eeh_dev_probe - Do probe on PCI device
89 * @dev: PCI device
90 * @flag: unused
92 * When EEH module is installed during system boot, all PCI devices
93 * are checked one by one to see if it supports EEH. The function
94 * is introduced for the purpose. By default, EEH has been enabled
95 * on all PCI devices. That's to say, we only need do necessary
96 * initialization on the corresponding eeh device and create PE
97 * accordingly.
99 * It's notable that's unsafe to retrieve the EEH device through
100 * the corresponding PCI device. During the PCI device hotplug, which
101 * was possiblly triggered by EEH core, the binding between EEH device
102 * and the PCI device isn't built yet.
104 static int powernv_eeh_dev_probe(struct pci_dev *dev, void *flag)
106 struct pci_controller *hose = pci_bus_to_host(dev->bus);
107 struct pnv_phb *phb = hose->private_data;
108 struct device_node *dn = pci_device_to_OF_node(dev);
109 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
112 * When probing the root bridge, which doesn't have any
113 * subordinate PCI devices. We don't have OF node for
114 * the root bridge. So it's not reasonable to continue
115 * the probing.
117 if (!dn || !edev || edev->pe)
118 return 0;
120 /* Skip for PCI-ISA bridge */
121 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
122 return 0;
124 /* Initialize eeh device */
125 edev->class_code = dev->class;
126 edev->mode &= 0xFFFFFF00;
127 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
128 edev->mode |= EEH_DEV_BRIDGE;
129 if (pci_is_pcie(dev)) {
130 edev->pcie_cap = pci_pcie_cap(dev);
132 if (pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT)
133 edev->mode |= EEH_DEV_ROOT_PORT;
134 else if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM)
135 edev->mode |= EEH_DEV_DS_PORT;
138 edev->config_addr = ((dev->bus->number << 8) | dev->devfn);
139 edev->pe_config_addr = phb->bdfn_to_pe(phb, dev->bus, dev->devfn & 0xff);
141 /* Create PE */
142 eeh_add_to_parent_pe(edev);
145 * Enable EEH explicitly so that we will do EEH check
146 * while accessing I/O stuff
148 * FIXME: Enable that for PHB3 later
150 if (phb->type == PNV_PHB_IODA1)
151 eeh_subsystem_enabled = 1;
153 /* Save memory bars */
154 eeh_save_bars(edev);
156 return 0;
160 * powernv_eeh_set_option - Initialize EEH or MMIO/DMA reenable
161 * @pe: EEH PE
162 * @option: operation to be issued
164 * The function is used to control the EEH functionality globally.
165 * Currently, following options are support according to PAPR:
166 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
168 static int powernv_eeh_set_option(struct eeh_pe *pe, int option)
170 struct pci_controller *hose = pe->phb;
171 struct pnv_phb *phb = hose->private_data;
172 int ret = -EEXIST;
175 * What we need do is pass it down for hardware
176 * implementation to handle it.
178 if (phb->eeh_ops && phb->eeh_ops->set_option)
179 ret = phb->eeh_ops->set_option(pe, option);
181 return ret;
185 * powernv_eeh_get_pe_addr - Retrieve PE address
186 * @pe: EEH PE
188 * Retrieve the PE address according to the given tranditional
189 * PCI BDF (Bus/Device/Function) address.
191 static int powernv_eeh_get_pe_addr(struct eeh_pe *pe)
193 return pe->addr;
197 * powernv_eeh_get_state - Retrieve PE state
198 * @pe: EEH PE
199 * @delay: delay while PE state is temporarily unavailable
201 * Retrieve the state of the specified PE. For IODA-compitable
202 * platform, it should be retrieved from IODA table. Therefore,
203 * we prefer passing down to hardware implementation to handle
204 * it.
206 static int powernv_eeh_get_state(struct eeh_pe *pe, int *delay)
208 struct pci_controller *hose = pe->phb;
209 struct pnv_phb *phb = hose->private_data;
210 int ret = EEH_STATE_NOT_SUPPORT;
212 if (phb->eeh_ops && phb->eeh_ops->get_state) {
213 ret = phb->eeh_ops->get_state(pe);
216 * If the PE state is temporarily unavailable,
217 * to inform the EEH core delay for default
218 * period (1 second)
220 if (delay) {
221 *delay = 0;
222 if (ret & EEH_STATE_UNAVAILABLE)
223 *delay = 1000;
227 return ret;
231 * powernv_eeh_reset - Reset the specified PE
232 * @pe: EEH PE
233 * @option: reset option
235 * Reset the specified PE
237 static int powernv_eeh_reset(struct eeh_pe *pe, int option)
239 struct pci_controller *hose = pe->phb;
240 struct pnv_phb *phb = hose->private_data;
241 int ret = -EEXIST;
243 if (phb->eeh_ops && phb->eeh_ops->reset)
244 ret = phb->eeh_ops->reset(pe, option);
246 return ret;
250 * powernv_eeh_wait_state - Wait for PE state
251 * @pe: EEH PE
252 * @max_wait: maximal period in microsecond
254 * Wait for the state of associated PE. It might take some time
255 * to retrieve the PE's state.
257 static int powernv_eeh_wait_state(struct eeh_pe *pe, int max_wait)
259 int ret;
260 int mwait;
262 while (1) {
263 ret = powernv_eeh_get_state(pe, &mwait);
266 * If the PE's state is temporarily unavailable,
267 * we have to wait for the specified time. Otherwise,
268 * the PE's state will be returned immediately.
270 if (ret != EEH_STATE_UNAVAILABLE)
271 return ret;
273 max_wait -= mwait;
274 if (max_wait <= 0) {
275 pr_warning("%s: Timeout getting PE#%x's state (%d)\n",
276 __func__, pe->addr, max_wait);
277 return EEH_STATE_NOT_SUPPORT;
280 msleep(mwait);
283 return EEH_STATE_NOT_SUPPORT;
287 * powernv_eeh_get_log - Retrieve error log
288 * @pe: EEH PE
289 * @severity: temporary or permanent error log
290 * @drv_log: driver log to be combined with retrieved error log
291 * @len: length of driver log
293 * Retrieve the temporary or permanent error from the PE.
295 static int powernv_eeh_get_log(struct eeh_pe *pe, int severity,
296 char *drv_log, unsigned long len)
298 struct pci_controller *hose = pe->phb;
299 struct pnv_phb *phb = hose->private_data;
300 int ret = -EEXIST;
302 if (phb->eeh_ops && phb->eeh_ops->get_log)
303 ret = phb->eeh_ops->get_log(pe, severity, drv_log, len);
305 return ret;
309 * powernv_eeh_configure_bridge - Configure PCI bridges in the indicated PE
310 * @pe: EEH PE
312 * The function will be called to reconfigure the bridges included
313 * in the specified PE so that the mulfunctional PE would be recovered
314 * again.
316 static int powernv_eeh_configure_bridge(struct eeh_pe *pe)
318 struct pci_controller *hose = pe->phb;
319 struct pnv_phb *phb = hose->private_data;
320 int ret = 0;
322 if (phb->eeh_ops && phb->eeh_ops->configure_bridge)
323 ret = phb->eeh_ops->configure_bridge(pe);
325 return ret;
329 * powernv_eeh_next_error - Retrieve next EEH error to handle
330 * @pe: Affected PE
332 * Using OPAL API, to retrieve next EEH error for EEH core to handle
334 static int powernv_eeh_next_error(struct eeh_pe **pe)
336 struct pci_controller *hose;
337 struct pnv_phb *phb = NULL;
339 list_for_each_entry(hose, &hose_list, list_node) {
340 phb = hose->private_data;
341 break;
344 if (phb && phb->eeh_ops->next_error)
345 return phb->eeh_ops->next_error(pe);
347 return -EEXIST;
350 static struct eeh_ops powernv_eeh_ops = {
351 .name = "powernv",
352 .init = powernv_eeh_init,
353 .post_init = powernv_eeh_post_init,
354 .of_probe = NULL,
355 .dev_probe = powernv_eeh_dev_probe,
356 .set_option = powernv_eeh_set_option,
357 .get_pe_addr = powernv_eeh_get_pe_addr,
358 .get_state = powernv_eeh_get_state,
359 .reset = powernv_eeh_reset,
360 .wait_state = powernv_eeh_wait_state,
361 .get_log = powernv_eeh_get_log,
362 .configure_bridge = powernv_eeh_configure_bridge,
363 .read_config = pnv_pci_cfg_read,
364 .write_config = pnv_pci_cfg_write,
365 .next_error = powernv_eeh_next_error
369 * eeh_powernv_init - Register platform dependent EEH operations
371 * EEH initialization on powernv platform. This function should be
372 * called before any EEH related functions.
374 static int __init eeh_powernv_init(void)
376 int ret = -EINVAL;
378 if (!machine_is(powernv))
379 return ret;
381 ret = eeh_ops_register(&powernv_eeh_ops);
382 if (!ret)
383 pr_info("EEH: PowerNV platform initialized\n");
384 else
385 pr_info("EEH: Failed to initialize PowerNV platform (%d)\n", ret);
387 return ret;
390 early_initcall(eeh_powernv_init);