2 * The file intends to implement PE based on the information from
3 * platforms. Basically, there have 3 types of PEs: PHB/Bus/Device.
4 * All the PEs should be organized as hierarchy tree. The first level
5 * of the tree will be associated to existing PHBs since the particular
6 * PE is only meaningful in one PHB domain.
8 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/export.h>
26 #include <linux/gfp.h>
27 #include <linux/init.h>
28 #include <linux/kernel.h>
29 #include <linux/pci.h>
30 #include <linux/string.h>
32 #include <asm/pci-bridge.h>
33 #include <asm/ppc-pci.h>
35 static LIST_HEAD(eeh_phb_pe);
38 * eeh_pe_alloc - Allocate PE
39 * @phb: PCI controller
42 * Allocate PE instance dynamically.
44 static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type)
49 pe = kzalloc(sizeof(struct eeh_pe), GFP_KERNEL);
52 /* Initialize PHB PE */
55 INIT_LIST_HEAD(&pe->child_list);
56 INIT_LIST_HEAD(&pe->child);
57 INIT_LIST_HEAD(&pe->edevs);
63 * eeh_phb_pe_create - Create PHB PE
64 * @phb: PCI controller
66 * The function should be called while the PHB is detected during
67 * system boot or PCI hotplug in order to create PHB PE.
69 int __devinit eeh_phb_pe_create(struct pci_controller *phb)
74 pe = eeh_pe_alloc(phb, EEH_PE_PHB);
76 pr_err("%s: out of memory!\n", __func__);
80 /* Put it into the list */
82 list_add_tail(&pe->child, &eeh_phb_pe);
85 pr_debug("EEH: Add PE for PHB#%d\n", phb->global_number);
91 * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB
92 * @phb: PCI controller
94 * The overall PEs form hierarchy tree. The first layer of the
95 * hierarchy tree is composed of PHB PEs. The function is used
96 * to retrieve the corresponding PHB PE according to the given PHB.
98 static struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb)
104 list_for_each_entry(pe, &eeh_phb_pe, child) {
106 * Actually, we needn't check the type since
107 * the PE for PHB has been determined when that
110 if (pe->type == EEH_PE_PHB &&
123 * eeh_pe_next - Retrieve the next PE in the tree
127 * The function is used to retrieve the next PE in the
130 static struct eeh_pe *eeh_pe_next(struct eeh_pe *pe,
133 struct list_head *next = pe->child_list.next;
135 if (next == &pe->child_list) {
139 next = pe->child.next;
140 if (next != &pe->parent->child_list)
146 return list_entry(next, struct eeh_pe, child);
150 * eeh_pe_traverse - Traverse PEs in the specified PHB
153 * @flag: extra parameter to callback
155 * The function is used to traverse the specified PE and its
156 * child PEs. The traversing is to be terminated once the
157 * callback returns something other than NULL, or no more PEs
160 static void *eeh_pe_traverse(struct eeh_pe *root,
161 eeh_traverse_func fn, void *flag)
166 for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
175 * eeh_pe_dev_traverse - Traverse the devices from the PE
177 * @fn: function callback
178 * @flag: extra parameter to callback
180 * The function is used to traverse the devices of the specified
181 * PE and its child PEs.
183 void *eeh_pe_dev_traverse(struct eeh_pe *root,
184 eeh_traverse_func fn, void *flag)
187 struct eeh_dev *edev;
191 pr_warning("%s: Invalid PE %p\n", __func__, root);
195 /* Traverse root PE */
196 for (pe = root; pe; pe = eeh_pe_next(pe, root)) {
197 eeh_pe_for_each_dev(pe, edev) {
198 ret = fn(edev, flag);
207 * __eeh_pe_get - Check the PE address
211 * For one particular PE, it can be identified by PE address
212 * or tranditional BDF address. BDF address is composed of
213 * Bus/Device/Function number. The extra data referred by flag
214 * indicates which type of address should be used.
216 static void *__eeh_pe_get(void *data, void *flag)
218 struct eeh_pe *pe = (struct eeh_pe *)data;
219 struct eeh_dev *edev = (struct eeh_dev *)flag;
221 /* Unexpected PHB PE */
222 if (pe->type == EEH_PE_PHB)
225 /* We prefer PE address */
226 if (edev->pe_config_addr &&
227 (edev->pe_config_addr == pe->addr))
230 /* Try BDF address */
231 if (edev->pe_config_addr &&
232 (edev->config_addr == pe->config_addr))
239 * eeh_pe_get - Search PE based on the given address
242 * Search the corresponding PE based on the specified address which
243 * is included in the eeh device. The function is used to check if
244 * the associated PE has been created against the PE address. It's
245 * notable that the PE address has 2 format: traditional PE address
246 * which is composed of PCI bus/device/function number, or unified
249 static struct eeh_pe *eeh_pe_get(struct eeh_dev *edev)
251 struct eeh_pe *root = eeh_phb_pe_get(edev->phb);
255 pe = eeh_pe_traverse(root, __eeh_pe_get, edev);
262 * eeh_pe_get_parent - Retrieve the parent PE
265 * The whole PEs existing in the system are organized as hierarchy
266 * tree. The function is used to retrieve the parent PE according
267 * to the parent EEH device.
269 static struct eeh_pe *eeh_pe_get_parent(struct eeh_dev *edev)
271 struct device_node *dn;
272 struct eeh_dev *parent;
275 * It might have the case for the indirect parent
276 * EEH device already having associated PE, but
277 * the direct parent EEH device doesn't have yet.
279 dn = edev->dn->parent;
281 /* We're poking out of PCI territory */
282 if (!PCI_DN(dn)) return NULL;
284 parent = of_node_to_eeh_dev(dn);
285 /* We're poking out of PCI territory */
286 if (!parent) return NULL;
298 * eeh_add_to_parent_pe - Add EEH device to parent PE
301 * Add EEH device to the parent PE. If the parent PE already
302 * exists, the PE type will be changed to EEH_PE_BUS. Otherwise,
303 * we have to create new PE to hold the EEH device and the new
304 * PE will be linked to its parent PE as well.
306 int eeh_add_to_parent_pe(struct eeh_dev *edev)
308 struct eeh_pe *pe, *parent;
311 * Search the PE has been existing or not according
312 * to the PE address. If that has been existing, the
313 * PE should be composed of PCI bus and its subordinate
316 pe = eeh_pe_get(edev);
318 if (!edev->pe_config_addr) {
319 pr_err("%s: PE with addr 0x%x already exists\n",
320 __func__, edev->config_addr);
324 /* Mark the PE as type of PCI bus */
325 pe->type = EEH_PE_BUS;
328 /* Put the edev to PE */
329 list_add_tail(&edev->list, &pe->edevs);
330 pr_debug("EEH: Add %s to Bus PE#%x\n",
331 edev->dn->full_name, pe->addr);
336 /* Create a new EEH PE */
337 pe = eeh_pe_alloc(edev->phb, EEH_PE_DEVICE);
339 pr_err("%s: out of memory!\n", __func__);
342 pe->addr = edev->pe_config_addr;
343 pe->config_addr = edev->config_addr;
346 * Put the new EEH PE into hierarchy tree. If the parent
347 * can't be found, the newly created PE will be attached
348 * to PHB directly. Otherwise, we have to associate the
349 * PE with its parent.
351 parent = eeh_pe_get_parent(edev);
353 parent = eeh_phb_pe_get(edev->phb);
355 pr_err("%s: No PHB PE is found (PHB Domain=%d)\n",
356 __func__, edev->phb->global_number);
365 * Put the newly created PE into the child list and
366 * link the EEH device accordingly.
368 list_add_tail(&pe->child, &parent->child_list);
369 list_add_tail(&edev->list, &pe->edevs);
371 pr_debug("EEH: Add %s to Device PE#%x, Parent PE#%x\n",
372 edev->dn->full_name, pe->addr, pe->parent->addr);
378 * eeh_rmv_from_parent_pe - Remove one EEH device from the associated PE
381 * The PE hierarchy tree might be changed when doing PCI hotplug.
382 * Also, the PCI devices or buses could be removed from the system
383 * during EEH recovery. So we have to call the function remove the
384 * corresponding PE accordingly if necessary.
386 int eeh_rmv_from_parent_pe(struct eeh_dev *edev)
388 struct eeh_pe *pe, *parent;
391 pr_warning("%s: No PE found for EEH device %s\n",
392 __func__, edev->dn->full_name);
396 /* Remove the EEH device */
399 list_del(&edev->list);
402 * Check if the parent PE includes any EEH devices.
403 * If not, we should delete that. Also, we should
404 * delete the parent PE if it doesn't have associated
405 * child PEs and EEH devices.
409 if (pe->type == EEH_PE_PHB)
412 if (list_empty(&pe->edevs) &&
413 list_empty(&pe->child_list)) {
414 list_del(&pe->child);
425 * __eeh_pe_state_mark - Mark the state for the PE
429 * The function is used to mark the indicated state for the given
430 * PE. Also, the associated PCI devices will be put into IO frozen
433 static void *__eeh_pe_state_mark(void *data, void *flag)
435 struct eeh_pe *pe = (struct eeh_pe *)data;
436 int state = *((int *)flag);
438 struct pci_dev *pdev;
441 * Mark the PE with the indicated state. Also,
442 * the associated PCI device will be put into
443 * I/O frozen state to avoid I/O accesses from
444 * the PCI device driver.
447 eeh_pe_for_each_dev(pe, tmp) {
448 pdev = eeh_dev_to_pci_dev(tmp);
450 pdev->error_state = pci_channel_io_frozen;
457 * eeh_pe_state_mark - Mark specified state for PE and its associated device
460 * EEH error affects the current PE and its child PEs. The function
461 * is used to mark appropriate state for the affected PEs and the
462 * associated devices.
464 void eeh_pe_state_mark(struct eeh_pe *pe, int state)
466 eeh_pe_traverse(pe, __eeh_pe_state_mark, &state);
470 * __eeh_pe_state_clear - Clear state for the PE
474 * The function is used to clear the indicated state from the
475 * given PE. Besides, we also clear the check count of the PE
478 static void *__eeh_pe_state_clear(void *data, void *flag)
480 struct eeh_pe *pe = (struct eeh_pe *)data;
481 int state = *((int *)flag);
490 * eeh_pe_state_clear - Clear state for the PE and its children
492 * @state: state to be cleared
494 * When the PE and its children has been recovered from error,
495 * we need clear the error state for that. The function is used
498 void eeh_pe_state_clear(struct eeh_pe *pe, int state)
500 eeh_pe_traverse(pe, __eeh_pe_state_clear, &state);
504 * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
508 * Loads the PCI configuration space base address registers,
509 * the expansion ROM base address, the latency timer, and etc.
510 * from the saved values in the device node.
512 static void *eeh_restore_one_device_bars(void *data, void *flag)
516 struct eeh_dev *edev = (struct eeh_dev *)data;
517 struct device_node *dn = eeh_dev_to_of_node(edev);
519 for (i = 4; i < 10; i++)
520 eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
521 /* 12 == Expansion ROM Address */
522 eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
524 #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
525 #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
527 eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
528 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
529 eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
530 SAVED_BYTE(PCI_LATENCY_TIMER));
532 /* max latency, min grant, interrupt pin and line */
533 eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
536 * Restore PERR & SERR bits, some devices require it,
537 * don't touch the other command bits
539 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
540 if (edev->config_space[1] & PCI_COMMAND_PARITY)
541 cmd |= PCI_COMMAND_PARITY;
543 cmd &= ~PCI_COMMAND_PARITY;
544 if (edev->config_space[1] & PCI_COMMAND_SERR)
545 cmd |= PCI_COMMAND_SERR;
547 cmd &= ~PCI_COMMAND_SERR;
548 eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
554 * eeh_pe_restore_bars - Restore the PCI config space info
557 * This routine performs a recursive walk to the children
558 * of this device as well.
560 void eeh_pe_restore_bars(struct eeh_pe *pe)
562 eeh_pe_dev_traverse(pe, eeh_restore_one_device_bars, NULL);
566 * eeh_pe_bus_get - Retrieve PCI bus according to the given PE
569 * Retrieve the PCI bus according to the given PE. Basically,
570 * there're 3 types of PEs: PHB/Bus/Device. For PHB PE, the
571 * primary PCI bus will be retrieved. The parent bus will be
572 * returned for BUS PE. However, we don't have associated PCI
575 struct pci_bus *eeh_pe_bus_get(struct eeh_pe *pe)
577 struct pci_bus *bus = NULL;
578 struct eeh_dev *edev;
579 struct pci_dev *pdev;
581 if (pe->type == EEH_PE_PHB) {
583 } else if (pe->type == EEH_PE_BUS) {
584 edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
585 pdev = eeh_dev_to_pci_dev(edev);