Gavin Shan | 55037d1 | 2012-09-07 22:44:07 +0000 | [diff] [blame^] | 1 | /* |
| 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. |
| 7 | * |
| 8 | * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2012. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 19 | * |
| 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 |
| 23 | */ |
| 24 | |
| 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> |
| 31 | |
| 32 | #include <asm/pci-bridge.h> |
| 33 | #include <asm/ppc-pci.h> |
| 34 | |
| 35 | static LIST_HEAD(eeh_phb_pe); |
| 36 | |
| 37 | /** |
| 38 | * eeh_pe_alloc - Allocate PE |
| 39 | * @phb: PCI controller |
| 40 | * @type: PE type |
| 41 | * |
| 42 | * Allocate PE instance dynamically. |
| 43 | */ |
| 44 | static struct eeh_pe *eeh_pe_alloc(struct pci_controller *phb, int type) |
| 45 | { |
| 46 | struct eeh_pe *pe; |
| 47 | |
| 48 | /* Allocate PHB PE */ |
| 49 | pe = kzalloc(sizeof(struct eeh_pe), GFP_KERNEL); |
| 50 | if (!pe) return NULL; |
| 51 | |
| 52 | /* Initialize PHB PE */ |
| 53 | pe->type = type; |
| 54 | pe->phb = phb; |
| 55 | INIT_LIST_HEAD(&pe->child_list); |
| 56 | INIT_LIST_HEAD(&pe->child); |
| 57 | INIT_LIST_HEAD(&pe->edevs); |
| 58 | |
| 59 | return pe; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * eeh_phb_pe_create - Create PHB PE |
| 64 | * @phb: PCI controller |
| 65 | * |
| 66 | * The function should be called while the PHB is detected during |
| 67 | * system boot or PCI hotplug in order to create PHB PE. |
| 68 | */ |
| 69 | int __devinit eeh_phb_pe_create(struct pci_controller *phb) |
| 70 | { |
| 71 | struct eeh_pe *pe; |
| 72 | |
| 73 | /* Allocate PHB PE */ |
| 74 | pe = eeh_pe_alloc(phb, EEH_PE_PHB); |
| 75 | if (!pe) { |
| 76 | pr_err("%s: out of memory!\n", __func__); |
| 77 | return -ENOMEM; |
| 78 | } |
| 79 | |
| 80 | /* Put it into the list */ |
| 81 | eeh_lock(); |
| 82 | list_add_tail(&pe->child, &eeh_phb_pe); |
| 83 | eeh_unlock(); |
| 84 | |
| 85 | pr_debug("EEH: Add PE for PHB#%d\n", phb->global_number); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * eeh_phb_pe_get - Retrieve PHB PE based on the given PHB |
| 92 | * @phb: PCI controller |
| 93 | * |
| 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. |
| 97 | */ |
| 98 | static struct eeh_pe *eeh_phb_pe_get(struct pci_controller *phb) |
| 99 | { |
| 100 | struct eeh_pe *pe; |
| 101 | |
| 102 | eeh_lock(); |
| 103 | |
| 104 | list_for_each_entry(pe, &eeh_phb_pe, child) { |
| 105 | /* |
| 106 | * Actually, we needn't check the type since |
| 107 | * the PE for PHB has been determined when that |
| 108 | * was created. |
| 109 | */ |
| 110 | if (pe->type == EEH_PE_PHB && |
| 111 | pe->phb == phb) { |
| 112 | eeh_unlock(); |
| 113 | return pe; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | eeh_unlock(); |
| 118 | |
| 119 | return NULL; |
| 120 | } |