David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 1 | /* pci.c: UltraSparc PCI controller support. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com) |
| 4 | * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be) |
| 5 | * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz) |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 6 | * |
| 7 | * OF tree based PCI bus probing taken from the PowerPC port |
| 8 | * with minor modifications, see there for credits. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/capability.h> |
| 16 | #include <linux/errno.h> |
David S. Miller | c57c2ff | 2007-05-08 00:43:56 -0700 | [diff] [blame] | 17 | #include <linux/pci.h> |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 18 | #include <linux/msi.h> |
| 19 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
David S. Miller | 356d164 | 2008-08-30 00:36:11 -0700 | [diff] [blame^] | 21 | #include <linux/of.h> |
| 22 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/pgtable.h> |
| 26 | #include <asm/irq.h> |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 27 | #include <asm/prom.h> |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 28 | #include <asm/apb.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
David S. Miller | 1e8a8cc | 2007-02-28 23:38:38 -0800 | [diff] [blame] | 30 | #include "pci_impl.h" |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | /* List of all PCI controllers found in the system. */ |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 33 | struct pci_pbm_info *pci_pbm_root = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
David S. Miller | 6c108f1 | 2007-05-07 23:49:01 -0700 | [diff] [blame] | 35 | /* Each PBM found gets a unique index. */ |
| 36 | int pci_num_pbms = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | volatile int pci_poke_in_progress; |
| 39 | volatile int pci_poke_cpu = -1; |
| 40 | volatile int pci_poke_faulted; |
| 41 | |
| 42 | static DEFINE_SPINLOCK(pci_poke_lock); |
| 43 | |
| 44 | void pci_config_read8(u8 *addr, u8 *ret) |
| 45 | { |
| 46 | unsigned long flags; |
| 47 | u8 byte; |
| 48 | |
| 49 | spin_lock_irqsave(&pci_poke_lock, flags); |
| 50 | pci_poke_cpu = smp_processor_id(); |
| 51 | pci_poke_in_progress = 1; |
| 52 | pci_poke_faulted = 0; |
| 53 | __asm__ __volatile__("membar #Sync\n\t" |
| 54 | "lduba [%1] %2, %0\n\t" |
| 55 | "membar #Sync" |
| 56 | : "=r" (byte) |
| 57 | : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L) |
| 58 | : "memory"); |
| 59 | pci_poke_in_progress = 0; |
| 60 | pci_poke_cpu = -1; |
| 61 | if (!pci_poke_faulted) |
| 62 | *ret = byte; |
| 63 | spin_unlock_irqrestore(&pci_poke_lock, flags); |
| 64 | } |
| 65 | |
| 66 | void pci_config_read16(u16 *addr, u16 *ret) |
| 67 | { |
| 68 | unsigned long flags; |
| 69 | u16 word; |
| 70 | |
| 71 | spin_lock_irqsave(&pci_poke_lock, flags); |
| 72 | pci_poke_cpu = smp_processor_id(); |
| 73 | pci_poke_in_progress = 1; |
| 74 | pci_poke_faulted = 0; |
| 75 | __asm__ __volatile__("membar #Sync\n\t" |
| 76 | "lduha [%1] %2, %0\n\t" |
| 77 | "membar #Sync" |
| 78 | : "=r" (word) |
| 79 | : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L) |
| 80 | : "memory"); |
| 81 | pci_poke_in_progress = 0; |
| 82 | pci_poke_cpu = -1; |
| 83 | if (!pci_poke_faulted) |
| 84 | *ret = word; |
| 85 | spin_unlock_irqrestore(&pci_poke_lock, flags); |
| 86 | } |
| 87 | |
| 88 | void pci_config_read32(u32 *addr, u32 *ret) |
| 89 | { |
| 90 | unsigned long flags; |
| 91 | u32 dword; |
| 92 | |
| 93 | spin_lock_irqsave(&pci_poke_lock, flags); |
| 94 | pci_poke_cpu = smp_processor_id(); |
| 95 | pci_poke_in_progress = 1; |
| 96 | pci_poke_faulted = 0; |
| 97 | __asm__ __volatile__("membar #Sync\n\t" |
| 98 | "lduwa [%1] %2, %0\n\t" |
| 99 | "membar #Sync" |
| 100 | : "=r" (dword) |
| 101 | : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L) |
| 102 | : "memory"); |
| 103 | pci_poke_in_progress = 0; |
| 104 | pci_poke_cpu = -1; |
| 105 | if (!pci_poke_faulted) |
| 106 | *ret = dword; |
| 107 | spin_unlock_irqrestore(&pci_poke_lock, flags); |
| 108 | } |
| 109 | |
| 110 | void pci_config_write8(u8 *addr, u8 val) |
| 111 | { |
| 112 | unsigned long flags; |
| 113 | |
| 114 | spin_lock_irqsave(&pci_poke_lock, flags); |
| 115 | pci_poke_cpu = smp_processor_id(); |
| 116 | pci_poke_in_progress = 1; |
| 117 | pci_poke_faulted = 0; |
| 118 | __asm__ __volatile__("membar #Sync\n\t" |
| 119 | "stba %0, [%1] %2\n\t" |
| 120 | "membar #Sync" |
| 121 | : /* no outputs */ |
| 122 | : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L) |
| 123 | : "memory"); |
| 124 | pci_poke_in_progress = 0; |
| 125 | pci_poke_cpu = -1; |
| 126 | spin_unlock_irqrestore(&pci_poke_lock, flags); |
| 127 | } |
| 128 | |
| 129 | void pci_config_write16(u16 *addr, u16 val) |
| 130 | { |
| 131 | unsigned long flags; |
| 132 | |
| 133 | spin_lock_irqsave(&pci_poke_lock, flags); |
| 134 | pci_poke_cpu = smp_processor_id(); |
| 135 | pci_poke_in_progress = 1; |
| 136 | pci_poke_faulted = 0; |
| 137 | __asm__ __volatile__("membar #Sync\n\t" |
| 138 | "stha %0, [%1] %2\n\t" |
| 139 | "membar #Sync" |
| 140 | : /* no outputs */ |
| 141 | : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L) |
| 142 | : "memory"); |
| 143 | pci_poke_in_progress = 0; |
| 144 | pci_poke_cpu = -1; |
| 145 | spin_unlock_irqrestore(&pci_poke_lock, flags); |
| 146 | } |
| 147 | |
| 148 | void pci_config_write32(u32 *addr, u32 val) |
| 149 | { |
| 150 | unsigned long flags; |
| 151 | |
| 152 | spin_lock_irqsave(&pci_poke_lock, flags); |
| 153 | pci_poke_cpu = smp_processor_id(); |
| 154 | pci_poke_in_progress = 1; |
| 155 | pci_poke_faulted = 0; |
| 156 | __asm__ __volatile__("membar #Sync\n\t" |
| 157 | "stwa %0, [%1] %2\n\t" |
| 158 | "membar #Sync" |
| 159 | : /* no outputs */ |
| 160 | : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L) |
| 161 | : "memory"); |
| 162 | pci_poke_in_progress = 0; |
| 163 | pci_poke_cpu = -1; |
| 164 | spin_unlock_irqrestore(&pci_poke_lock, flags); |
| 165 | } |
| 166 | |
| 167 | /* Probe for all PCI controllers in the system. */ |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 168 | extern void sabre_init(struct device_node *, const char *); |
| 169 | extern void psycho_init(struct device_node *, const char *); |
| 170 | extern void schizo_init(struct device_node *, const char *); |
| 171 | extern void schizo_plus_init(struct device_node *, const char *); |
| 172 | extern void tomatillo_init(struct device_node *, const char *); |
| 173 | extern void sun4v_pci_init(struct device_node *, const char *); |
David S. Miller | 861fe90 | 2007-05-02 17:31:36 -0700 | [diff] [blame] | 174 | extern void fire_pci_init(struct device_node *, const char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | static struct { |
| 177 | char *model_name; |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 178 | void (*init)(struct device_node *, const char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } pci_controller_table[] __initdata = { |
| 180 | { "SUNW,sabre", sabre_init }, |
| 181 | { "pci108e,a000", sabre_init }, |
| 182 | { "pci108e,a001", sabre_init }, |
| 183 | { "SUNW,psycho", psycho_init }, |
| 184 | { "pci108e,8000", psycho_init }, |
| 185 | { "SUNW,schizo", schizo_init }, |
| 186 | { "pci108e,8001", schizo_init }, |
| 187 | { "SUNW,schizo+", schizo_plus_init }, |
| 188 | { "pci108e,8002", schizo_plus_init }, |
| 189 | { "SUNW,tomatillo", tomatillo_init }, |
| 190 | { "pci108e,a801", tomatillo_init }, |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 191 | { "SUNW,sun4v-pci", sun4v_pci_init }, |
David S. Miller | 861fe90 | 2007-05-02 17:31:36 -0700 | [diff] [blame] | 192 | { "pciex108e,80f0", fire_pci_init }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | }; |
Alejandro Martinez Ruiz | 29f139c | 2007-10-22 17:24:19 -0700 | [diff] [blame] | 194 | #define PCI_NUM_CONTROLLER_TYPES ARRAY_SIZE(pci_controller_table) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 196 | static int __init pci_controller_init(const char *model_name, int namelen, struct device_node *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
| 198 | int i; |
| 199 | |
| 200 | for (i = 0; i < PCI_NUM_CONTROLLER_TYPES; i++) { |
| 201 | if (!strncmp(model_name, |
| 202 | pci_controller_table[i].model_name, |
| 203 | namelen)) { |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 204 | pci_controller_table[i].init(dp, model_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | return 1; |
| 206 | } |
| 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 212 | static int __init pci_controller_scan(int (*handler)(const char *, int, struct device_node *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 214 | struct device_node *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | int count = 0; |
| 216 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 217 | for_each_node_by_name(dp, "pci") { |
| 218 | struct property *prop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | int len; |
| 220 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 221 | prop = of_find_property(dp, "model", &len); |
| 222 | if (!prop) |
| 223 | prop = of_find_property(dp, "compatible", &len); |
| 224 | |
| 225 | if (prop) { |
| 226 | const char *model = prop->value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | int item_len = 0; |
| 228 | |
| 229 | /* Our value may be a multi-valued string in the |
| 230 | * case of some compatible properties. For sanity, |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 231 | * only try the first one. |
| 232 | */ |
| 233 | while (model[item_len] && len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | len--; |
| 235 | item_len++; |
| 236 | } |
| 237 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 238 | if (handler(model, item_len, dp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | count++; |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return count; |
| 244 | } |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | /* Find each controller in the system, attach and initialize |
| 247 | * software state structure for each and link into the |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 248 | * pci_pbm_root. Setup the controller enough such |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | * that bus scanning can be done. |
| 250 | */ |
| 251 | static void __init pci_controller_probe(void) |
| 252 | { |
| 253 | printk("PCI: Probing for controllers.\n"); |
| 254 | |
| 255 | pci_controller_scan(pci_controller_init); |
| 256 | } |
| 257 | |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 258 | static int ofpci_verbose; |
| 259 | |
| 260 | static int __init ofpci_debug(char *str) |
| 261 | { |
| 262 | int val = 0; |
| 263 | |
| 264 | get_option(&str, &val); |
| 265 | if (val) |
| 266 | ofpci_verbose = 1; |
| 267 | return 1; |
| 268 | } |
| 269 | |
| 270 | __setup("ofpci_debug=", ofpci_debug); |
| 271 | |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 272 | static unsigned long pci_parse_of_flags(u32 addr0) |
| 273 | { |
| 274 | unsigned long flags = 0; |
| 275 | |
| 276 | if (addr0 & 0x02000000) { |
| 277 | flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY; |
| 278 | flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64; |
| 279 | flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M; |
| 280 | if (addr0 & 0x40000000) |
| 281 | flags |= IORESOURCE_PREFETCH |
| 282 | | PCI_BASE_ADDRESS_MEM_PREFETCH; |
| 283 | } else if (addr0 & 0x01000000) |
| 284 | flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO; |
| 285 | return flags; |
| 286 | } |
| 287 | |
| 288 | /* The of_device layer has translated all of the assigned-address properties |
| 289 | * into physical address resources, we only have to figure out the register |
| 290 | * mapping. |
| 291 | */ |
| 292 | static void pci_parse_of_addrs(struct of_device *op, |
| 293 | struct device_node *node, |
| 294 | struct pci_dev *dev) |
| 295 | { |
| 296 | struct resource *op_res; |
| 297 | const u32 *addrs; |
| 298 | int proplen; |
| 299 | |
| 300 | addrs = of_get_property(node, "assigned-addresses", &proplen); |
| 301 | if (!addrs) |
| 302 | return; |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 303 | if (ofpci_verbose) |
| 304 | printk(" parse addresses (%d bytes) @ %p\n", |
| 305 | proplen, addrs); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 306 | op_res = &op->resource[0]; |
| 307 | for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) { |
| 308 | struct resource *res; |
| 309 | unsigned long flags; |
| 310 | int i; |
| 311 | |
| 312 | flags = pci_parse_of_flags(addrs[0]); |
| 313 | if (!flags) |
| 314 | continue; |
| 315 | i = addrs[0] & 0xff; |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 316 | if (ofpci_verbose) |
| 317 | printk(" start: %lx, end: %lx, i: %x\n", |
| 318 | op_res->start, op_res->end, i); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 319 | |
| 320 | if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) { |
| 321 | res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2]; |
| 322 | } else if (i == dev->rom_base_reg) { |
| 323 | res = &dev->resource[PCI_ROM_RESOURCE]; |
| 324 | flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE; |
| 325 | } else { |
| 326 | printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i); |
| 327 | continue; |
| 328 | } |
| 329 | res->start = op_res->start; |
| 330 | res->end = op_res->end; |
| 331 | res->flags = flags; |
| 332 | res->name = pci_name(dev); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm, |
| 337 | struct device_node *node, |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 338 | struct pci_bus *bus, int devfn) |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 339 | { |
| 340 | struct dev_archdata *sd; |
David S. Miller | ae05f87 | 2008-08-29 22:42:34 -0700 | [diff] [blame] | 341 | struct of_device *op; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 342 | struct pci_dev *dev; |
| 343 | const char *type; |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 344 | u32 class; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 345 | |
David S. Miller | 26e6385 | 2007-05-10 02:16:27 -0700 | [diff] [blame] | 346 | dev = alloc_pci_dev(); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 347 | if (!dev) |
| 348 | return NULL; |
| 349 | |
| 350 | sd = &dev->dev.archdata; |
| 351 | sd->iommu = pbm->iommu; |
| 352 | sd->stc = &pbm->stc; |
| 353 | sd->host_controller = pbm; |
| 354 | sd->prom_node = node; |
David S. Miller | ae05f87 | 2008-08-29 22:42:34 -0700 | [diff] [blame] | 355 | sd->op = op = of_find_device_by_node(node); |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 356 | sd->numa_node = pbm->numa_node; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 357 | |
David S. Miller | ae05f87 | 2008-08-29 22:42:34 -0700 | [diff] [blame] | 358 | sd = &op->dev.archdata; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 359 | sd->iommu = pbm->iommu; |
| 360 | sd->stc = &pbm->stc; |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 361 | sd->numa_node = pbm->numa_node; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 362 | |
David S. Miller | ae05f87 | 2008-08-29 22:42:34 -0700 | [diff] [blame] | 363 | if (!strcmp(node->name, "ebus")) |
| 364 | of_propagate_archdata(op); |
| 365 | |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 366 | type = of_get_property(node, "device_type", NULL); |
| 367 | if (type == NULL) |
| 368 | type = ""; |
| 369 | |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 370 | if (ofpci_verbose) |
| 371 | printk(" create device, devfn: %x, type: %s\n", |
| 372 | devfn, type); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 373 | |
| 374 | dev->bus = bus; |
| 375 | dev->sysdata = node; |
| 376 | dev->dev.parent = bus->bridge; |
| 377 | dev->dev.bus = &pci_bus_type; |
| 378 | dev->devfn = devfn; |
| 379 | dev->multifunction = 0; /* maybe a lie? */ |
| 380 | |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 381 | dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff); |
| 382 | dev->device = of_getintprop_default(node, "device-id", 0xffff); |
| 383 | dev->subsystem_vendor = |
| 384 | of_getintprop_default(node, "subsystem-vendor-id", 0); |
| 385 | dev->subsystem_device = |
| 386 | of_getintprop_default(node, "subsystem-id", 0); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 387 | |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 388 | dev->cfg_size = pci_cfg_space_size(dev); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 389 | |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 390 | /* We can't actually use the firmware value, we have |
| 391 | * to read what is in the register right now. One |
| 392 | * reason is that in the case of IDE interfaces the |
| 393 | * firmware can sample the value before the the IDE |
| 394 | * interface is programmed into native mode. |
| 395 | */ |
| 396 | pci_read_config_dword(dev, PCI_CLASS_REVISION, &class); |
| 397 | dev->class = class >> 8; |
| 398 | dev->revision = class & 0xff; |
David S. Miller | 28f57e7 | 2007-03-12 19:40:26 -0700 | [diff] [blame] | 399 | |
Greg Kroah-Hartman | 2222c31 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 400 | dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus), |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 401 | dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
| 402 | |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 403 | if (ofpci_verbose) |
| 404 | printk(" class: 0x%x device name: %s\n", |
| 405 | dev->class, pci_name(dev)); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 406 | |
David S. Miller | 861fe90 | 2007-05-02 17:31:36 -0700 | [diff] [blame] | 407 | /* I have seen IDE devices which will not respond to |
| 408 | * the bmdma simplex check reads if bus mastering is |
| 409 | * disabled. |
| 410 | */ |
| 411 | if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE) |
| 412 | pci_set_master(dev); |
| 413 | |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 414 | dev->current_state = 4; /* unknown power state */ |
| 415 | dev->error_state = pci_channel_io_normal; |
| 416 | |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 417 | if (!strcmp(type, "pci") || !strcmp(type, "pciex")) { |
| 418 | /* a PCI-PCI bridge */ |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 419 | dev->hdr_type = PCI_HEADER_TYPE_BRIDGE; |
| 420 | dev->rom_base_reg = PCI_ROM_ADDRESS1; |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 421 | } else if (!strcmp(type, "cardbus")) { |
| 422 | dev->hdr_type = PCI_HEADER_TYPE_CARDBUS; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 423 | } else { |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 424 | dev->hdr_type = PCI_HEADER_TYPE_NORMAL; |
| 425 | dev->rom_base_reg = PCI_ROM_ADDRESS; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 426 | |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 427 | dev->irq = sd->op->irqs[0]; |
| 428 | if (dev->irq == 0xffffffff) |
| 429 | dev->irq = PCI_IRQ_NONE; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 430 | } |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 431 | |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 432 | pci_parse_of_addrs(sd->op, node, dev); |
| 433 | |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 434 | if (ofpci_verbose) |
| 435 | printk(" adding to system ...\n"); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 436 | |
| 437 | pci_device_add(dev, bus); |
| 438 | |
| 439 | return dev; |
| 440 | } |
| 441 | |
David S. Miller | a6009dd | 2007-05-07 00:01:38 -0700 | [diff] [blame] | 442 | static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p) |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 443 | { |
| 444 | u32 idx, first, last; |
| 445 | |
| 446 | first = 8; |
| 447 | last = 0; |
| 448 | for (idx = 0; idx < 8; idx++) { |
| 449 | if ((map & (1 << idx)) != 0) { |
| 450 | if (first > idx) |
| 451 | first = idx; |
| 452 | if (last < idx) |
| 453 | last = idx; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | *first_p = first; |
| 458 | *last_p = last; |
| 459 | } |
| 460 | |
David S. Miller | f16537b | 2007-05-11 14:29:43 -0700 | [diff] [blame] | 461 | static void pci_resource_adjust(struct resource *res, |
| 462 | struct resource *root) |
David S. Miller | 0bae5f8 | 2007-03-08 22:42:19 -0800 | [diff] [blame] | 463 | { |
| 464 | res->start += root->start; |
| 465 | res->end += root->start; |
| 466 | } |
| 467 | |
David S. Miller | 8c2786c | 2007-06-07 21:59:44 -0700 | [diff] [blame] | 468 | /* For PCI bus devices which lack a 'ranges' property we interrogate |
| 469 | * the config space values to set the resources, just like the generic |
| 470 | * Linux PCI probing code does. |
| 471 | */ |
| 472 | static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev, |
| 473 | struct pci_bus *bus, |
| 474 | struct pci_pbm_info *pbm) |
| 475 | { |
| 476 | struct resource *res; |
| 477 | u8 io_base_lo, io_limit_lo; |
| 478 | u16 mem_base_lo, mem_limit_lo; |
| 479 | unsigned long base, limit; |
| 480 | |
| 481 | pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo); |
| 482 | pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo); |
| 483 | base = (io_base_lo & PCI_IO_RANGE_MASK) << 8; |
| 484 | limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8; |
| 485 | |
| 486 | if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) { |
| 487 | u16 io_base_hi, io_limit_hi; |
| 488 | |
| 489 | pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi); |
| 490 | pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi); |
| 491 | base |= (io_base_hi << 16); |
| 492 | limit |= (io_limit_hi << 16); |
| 493 | } |
| 494 | |
| 495 | res = bus->resource[0]; |
| 496 | if (base <= limit) { |
| 497 | res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO; |
| 498 | if (!res->start) |
| 499 | res->start = base; |
| 500 | if (!res->end) |
| 501 | res->end = limit + 0xfff; |
| 502 | pci_resource_adjust(res, &pbm->io_space); |
| 503 | } |
| 504 | |
| 505 | pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo); |
| 506 | pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo); |
| 507 | base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16; |
| 508 | limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16; |
| 509 | |
| 510 | res = bus->resource[1]; |
| 511 | if (base <= limit) { |
| 512 | res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | |
| 513 | IORESOURCE_MEM); |
| 514 | res->start = base; |
| 515 | res->end = limit + 0xfffff; |
| 516 | pci_resource_adjust(res, &pbm->mem_space); |
| 517 | } |
| 518 | |
| 519 | pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo); |
| 520 | pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo); |
| 521 | base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16; |
| 522 | limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16; |
| 523 | |
| 524 | if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) { |
| 525 | u32 mem_base_hi, mem_limit_hi; |
| 526 | |
| 527 | pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi); |
| 528 | pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi); |
| 529 | |
| 530 | /* |
| 531 | * Some bridges set the base > limit by default, and some |
| 532 | * (broken) BIOSes do not initialize them. If we find |
| 533 | * this, just assume they are not being used. |
| 534 | */ |
| 535 | if (mem_base_hi <= mem_limit_hi) { |
| 536 | base |= ((long) mem_base_hi) << 32; |
| 537 | limit |= ((long) mem_limit_hi) << 32; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | res = bus->resource[2]; |
| 542 | if (base <= limit) { |
| 543 | res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | |
| 544 | IORESOURCE_MEM | IORESOURCE_PREFETCH); |
| 545 | res->start = base; |
| 546 | res->end = limit + 0xfffff; |
| 547 | pci_resource_adjust(res, &pbm->mem_space); |
| 548 | } |
| 549 | } |
| 550 | |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 551 | /* Cook up fake bus resources for SUNW,simba PCI bridges which lack |
| 552 | * a proper 'ranges' property. |
| 553 | */ |
David S. Miller | a6009dd | 2007-05-07 00:01:38 -0700 | [diff] [blame] | 554 | static void __devinit apb_fake_ranges(struct pci_dev *dev, |
| 555 | struct pci_bus *bus, |
| 556 | struct pci_pbm_info *pbm) |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 557 | { |
| 558 | struct resource *res; |
| 559 | u32 first, last; |
| 560 | u8 map; |
| 561 | |
| 562 | pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map); |
| 563 | apb_calc_first_last(map, &first, &last); |
| 564 | res = bus->resource[0]; |
| 565 | res->start = (first << 21); |
| 566 | res->end = (last << 21) + ((1 << 21) - 1); |
| 567 | res->flags = IORESOURCE_IO; |
David S. Miller | 0bae5f8 | 2007-03-08 22:42:19 -0800 | [diff] [blame] | 568 | pci_resource_adjust(res, &pbm->io_space); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 569 | |
| 570 | pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map); |
| 571 | apb_calc_first_last(map, &first, &last); |
| 572 | res = bus->resource[1]; |
| 573 | res->start = (first << 21); |
| 574 | res->end = (last << 21) + ((1 << 21) - 1); |
| 575 | res->flags = IORESOURCE_MEM; |
David S. Miller | 0bae5f8 | 2007-03-08 22:42:19 -0800 | [diff] [blame] | 576 | pci_resource_adjust(res, &pbm->mem_space); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 577 | } |
| 578 | |
David S. Miller | a6009dd | 2007-05-07 00:01:38 -0700 | [diff] [blame] | 579 | static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm, |
| 580 | struct device_node *node, |
| 581 | struct pci_bus *bus); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 582 | |
| 583 | #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1]) |
| 584 | |
David S. Miller | a6009dd | 2007-05-07 00:01:38 -0700 | [diff] [blame] | 585 | static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm, |
| 586 | struct device_node *node, |
| 587 | struct pci_dev *dev) |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 588 | { |
| 589 | struct pci_bus *bus; |
| 590 | const u32 *busrange, *ranges; |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 591 | int len, i, simba; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 592 | struct resource *res; |
| 593 | unsigned int flags; |
| 594 | u64 size; |
| 595 | |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 596 | if (ofpci_verbose) |
| 597 | printk("of_scan_pci_bridge(%s)\n", node->full_name); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 598 | |
| 599 | /* parse bus-range property */ |
| 600 | busrange = of_get_property(node, "bus-range", &len); |
| 601 | if (busrange == NULL || len != 8) { |
| 602 | printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n", |
| 603 | node->full_name); |
| 604 | return; |
| 605 | } |
| 606 | ranges = of_get_property(node, "ranges", &len); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 607 | simba = 0; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 608 | if (ranges == NULL) { |
David S. Miller | a165b42 | 2007-03-29 01:50:16 -0700 | [diff] [blame] | 609 | const char *model = of_get_property(node, "model", NULL); |
David S. Miller | 8c2786c | 2007-06-07 21:59:44 -0700 | [diff] [blame] | 610 | if (model && !strcmp(model, "SUNW,simba")) |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 611 | simba = 1; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | bus = pci_add_new_bus(dev->bus, dev, busrange[0]); |
| 615 | if (!bus) { |
| 616 | printk(KERN_ERR "Failed to create pci bus for %s\n", |
| 617 | node->full_name); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | bus->primary = dev->bus->number; |
| 622 | bus->subordinate = busrange[1]; |
| 623 | bus->bridge_ctl = 0; |
| 624 | |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 625 | /* parse ranges property, or cook one up by hand for Simba */ |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 626 | /* PCI #address-cells == 3 and #size-cells == 2 always */ |
| 627 | res = &dev->resource[PCI_BRIDGE_RESOURCES]; |
| 628 | for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) { |
| 629 | res->flags = 0; |
| 630 | bus->resource[i] = res; |
| 631 | ++res; |
| 632 | } |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 633 | if (simba) { |
| 634 | apb_fake_ranges(dev, bus, pbm); |
David S. Miller | 8c2786c | 2007-06-07 21:59:44 -0700 | [diff] [blame] | 635 | goto after_ranges; |
| 636 | } else if (ranges == NULL) { |
| 637 | pci_cfg_fake_ranges(dev, bus, pbm); |
| 638 | goto after_ranges; |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 639 | } |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 640 | i = 1; |
| 641 | for (; len >= 32; len -= 32, ranges += 8) { |
| 642 | struct resource *root; |
| 643 | |
| 644 | flags = pci_parse_of_flags(ranges[0]); |
| 645 | size = GET_64BIT(ranges, 6); |
| 646 | if (flags == 0 || size == 0) |
| 647 | continue; |
| 648 | if (flags & IORESOURCE_IO) { |
| 649 | res = bus->resource[0]; |
| 650 | if (res->flags) { |
| 651 | printk(KERN_ERR "PCI: ignoring extra I/O range" |
| 652 | " for bridge %s\n", node->full_name); |
| 653 | continue; |
| 654 | } |
| 655 | root = &pbm->io_space; |
| 656 | } else { |
| 657 | if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) { |
| 658 | printk(KERN_ERR "PCI: too many memory ranges" |
| 659 | " for bridge %s\n", node->full_name); |
| 660 | continue; |
| 661 | } |
| 662 | res = bus->resource[i]; |
| 663 | ++i; |
| 664 | root = &pbm->mem_space; |
| 665 | } |
| 666 | |
| 667 | res->start = GET_64BIT(ranges, 1); |
| 668 | res->end = res->start + size - 1; |
| 669 | res->flags = flags; |
| 670 | |
| 671 | /* Another way to implement this would be to add an of_device |
| 672 | * layer routine that can calculate a resource for a given |
| 673 | * range property value in a PCI device. |
| 674 | */ |
David S. Miller | 0bae5f8 | 2007-03-08 22:42:19 -0800 | [diff] [blame] | 675 | pci_resource_adjust(res, root); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 676 | } |
David S. Miller | 8c2786c | 2007-06-07 21:59:44 -0700 | [diff] [blame] | 677 | after_ranges: |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 678 | sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus), |
| 679 | bus->number); |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 680 | if (ofpci_verbose) |
| 681 | printk(" bus name: %s\n", bus->name); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 682 | |
| 683 | pci_of_scan_bus(pbm, node, bus); |
| 684 | } |
| 685 | |
David S. Miller | a6009dd | 2007-05-07 00:01:38 -0700 | [diff] [blame] | 686 | static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm, |
| 687 | struct device_node *node, |
| 688 | struct pci_bus *bus) |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 689 | { |
| 690 | struct device_node *child; |
| 691 | const u32 *reg; |
David S. Miller | 2cc7345 | 2007-09-12 10:15:59 +0200 | [diff] [blame] | 692 | int reglen, devfn, prev_devfn; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 693 | struct pci_dev *dev; |
| 694 | |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 695 | if (ofpci_verbose) |
| 696 | printk("PCI: scan_bus[%s] bus no %d\n", |
| 697 | node->full_name, bus->number); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 698 | |
| 699 | child = NULL; |
David S. Miller | 2cc7345 | 2007-09-12 10:15:59 +0200 | [diff] [blame] | 700 | prev_devfn = -1; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 701 | while ((child = of_get_next_child(node, child)) != NULL) { |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 702 | if (ofpci_verbose) |
| 703 | printk(" * %s\n", child->full_name); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 704 | reg = of_get_property(child, "reg", ®len); |
| 705 | if (reg == NULL || reglen < 20) |
| 706 | continue; |
David S. Miller | 2cc7345 | 2007-09-12 10:15:59 +0200 | [diff] [blame] | 707 | |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 708 | devfn = (reg[0] >> 8) & 0xff; |
| 709 | |
David S. Miller | 2cc7345 | 2007-09-12 10:15:59 +0200 | [diff] [blame] | 710 | /* This is a workaround for some device trees |
| 711 | * which list PCI devices twice. On the V100 |
| 712 | * for example, device number 3 is listed twice. |
| 713 | * Once as "pm" and once again as "lomp". |
| 714 | */ |
| 715 | if (devfn == prev_devfn) |
| 716 | continue; |
| 717 | prev_devfn = devfn; |
| 718 | |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 719 | /* create a new pci_dev for this device */ |
David S. Miller | c26d3c0 | 2008-05-01 01:12:40 -0700 | [diff] [blame] | 720 | dev = of_create_pci_dev(pbm, child, bus, devfn); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 721 | if (!dev) |
| 722 | continue; |
David S. Miller | 5840fc6 | 2007-05-22 01:24:14 -0700 | [diff] [blame] | 723 | if (ofpci_verbose) |
| 724 | printk("PCI: dev header type: %x\n", |
| 725 | dev->hdr_type); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 726 | |
| 727 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE || |
| 728 | dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) |
| 729 | of_scan_pci_bridge(pbm, child, dev); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | static ssize_t |
| 734 | show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf) |
| 735 | { |
| 736 | struct pci_dev *pdev; |
| 737 | struct device_node *dp; |
| 738 | |
| 739 | pdev = to_pci_dev(dev); |
| 740 | dp = pdev->dev.archdata.prom_node; |
| 741 | |
| 742 | return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name); |
| 743 | } |
| 744 | |
| 745 | static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL); |
| 746 | |
| 747 | static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus) |
| 748 | { |
| 749 | struct pci_dev *dev; |
David S. Miller | a378fd0 | 2007-03-01 11:46:13 -0800 | [diff] [blame] | 750 | struct pci_bus *child_bus; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 751 | int err; |
| 752 | |
| 753 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 754 | /* we don't really care if we can create this file or |
| 755 | * not, but we need to assign the result of the call |
| 756 | * or the world will fall under alien invasion and |
| 757 | * everybody will be frozen on a spaceship ready to be |
| 758 | * eaten on alpha centauri by some green and jelly |
| 759 | * humanoid. |
| 760 | */ |
| 761 | err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr); |
| 762 | } |
David S. Miller | a378fd0 | 2007-03-01 11:46:13 -0800 | [diff] [blame] | 763 | list_for_each_entry(child_bus, &bus->children, node) |
| 764 | pci_bus_register_of_sysfs(child_bus); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 765 | } |
| 766 | |
David S. Miller | a6009dd | 2007-05-07 00:01:38 -0700 | [diff] [blame] | 767 | struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm) |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 768 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 769 | struct device_node *node = pbm->prom_node; |
| 770 | struct pci_bus *bus; |
| 771 | |
| 772 | printk("PCI: Scanning PBM %s\n", node->full_name); |
| 773 | |
| 774 | /* XXX parent device? XXX */ |
David S. Miller | f1cd8de | 2007-05-07 23:24:05 -0700 | [diff] [blame] | 775 | bus = pci_create_bus(NULL, pbm->pci_first_busno, pbm->pci_ops, pbm); |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 776 | if (!bus) { |
| 777 | printk(KERN_ERR "Failed to create bus for %s\n", |
| 778 | node->full_name); |
| 779 | return NULL; |
| 780 | } |
| 781 | bus->secondary = pbm->pci_first_busno; |
| 782 | bus->subordinate = pbm->pci_last_busno; |
| 783 | |
| 784 | bus->resource[0] = &pbm->io_space; |
| 785 | bus->resource[1] = &pbm->mem_space; |
| 786 | |
| 787 | pci_of_scan_bus(pbm, node, bus); |
| 788 | pci_bus_add_devices(bus); |
| 789 | pci_bus_register_of_sysfs(bus); |
| 790 | |
| 791 | return bus; |
| 792 | } |
| 793 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | static void __init pci_scan_each_controller_bus(void) |
| 795 | { |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 796 | struct pci_pbm_info *pbm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 798 | for (pbm = pci_pbm_root; pbm; pbm = pbm->next) |
| 799 | pbm->scan_bus(pbm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | extern void power_init(void); |
| 803 | |
| 804 | static int __init pcibios_init(void) |
| 805 | { |
| 806 | pci_controller_probe(); |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 807 | if (pci_pbm_root == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | return 0; |
| 809 | |
| 810 | pci_scan_each_controller_bus(); |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | power_init(); |
| 813 | |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | subsys_initcall(pcibios_init); |
| 818 | |
Robert Reif | f6b45da | 2007-04-12 13:47:37 -0700 | [diff] [blame] | 819 | void __devinit pcibios_fixup_bus(struct pci_bus *pbus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
| 821 | struct pci_pbm_info *pbm = pbus->sysdata; |
| 822 | |
| 823 | /* Generic PCI bus probing sets these to point at |
| 824 | * &io{port,mem}_resouce which is wrong for us. |
| 825 | */ |
| 826 | pbus->resource[0] = &pbm->io_space; |
| 827 | pbus->resource[1] = &pbm->mem_space; |
| 828 | } |
| 829 | |
David S. Miller | 085ae41 | 2005-08-08 13:19:08 -0700 | [diff] [blame] | 830 | struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
| 832 | struct pci_pbm_info *pbm = pdev->bus->sysdata; |
David S. Miller | 085ae41 | 2005-08-08 13:19:08 -0700 | [diff] [blame] | 833 | struct resource *root = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | |
David S. Miller | 085ae41 | 2005-08-08 13:19:08 -0700 | [diff] [blame] | 835 | if (r->flags & IORESOURCE_IO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | root = &pbm->io_space; |
David S. Miller | 085ae41 | 2005-08-08 13:19:08 -0700 | [diff] [blame] | 837 | if (r->flags & IORESOURCE_MEM) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | root = &pbm->mem_space; |
| 839 | |
David S. Miller | 085ae41 | 2005-08-08 13:19:08 -0700 | [diff] [blame] | 840 | return root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | void pcibios_update_irq(struct pci_dev *pdev, int irq) |
| 844 | { |
| 845 | } |
| 846 | |
| 847 | void pcibios_align_resource(void *data, struct resource *res, |
Greg Kroah-Hartman | e31dd6e | 2006-06-12 17:06:02 -0700 | [diff] [blame] | 848 | resource_size_t size, resource_size_t align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
| 850 | } |
| 851 | |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 852 | int pcibios_enable_device(struct pci_dev *dev, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 854 | u16 cmd, oldcmd; |
| 855 | int i; |
| 856 | |
| 857 | pci_read_config_word(dev, PCI_COMMAND, &cmd); |
| 858 | oldcmd = cmd; |
| 859 | |
| 860 | for (i = 0; i < PCI_NUM_RESOURCES; i++) { |
| 861 | struct resource *res = &dev->resource[i]; |
| 862 | |
| 863 | /* Only set up the requested stuff */ |
| 864 | if (!(mask & (1<<i))) |
| 865 | continue; |
| 866 | |
| 867 | if (res->flags & IORESOURCE_IO) |
| 868 | cmd |= PCI_COMMAND_IO; |
| 869 | if (res->flags & IORESOURCE_MEM) |
| 870 | cmd |= PCI_COMMAND_MEMORY; |
| 871 | } |
| 872 | |
| 873 | if (cmd != oldcmd) { |
| 874 | printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n", |
| 875 | pci_name(dev), cmd); |
| 876 | /* Enable the appropriate bits in the PCI command register. */ |
| 877 | pci_write_config_word(dev, PCI_COMMAND, cmd); |
| 878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region, |
| 883 | struct resource *res) |
| 884 | { |
| 885 | struct pci_pbm_info *pbm = pdev->bus->sysdata; |
| 886 | struct resource zero_res, *root; |
| 887 | |
| 888 | zero_res.start = 0; |
| 889 | zero_res.end = 0; |
| 890 | zero_res.flags = res->flags; |
| 891 | |
| 892 | if (res->flags & IORESOURCE_IO) |
| 893 | root = &pbm->io_space; |
| 894 | else |
| 895 | root = &pbm->mem_space; |
| 896 | |
David S. Miller | 0bae5f8 | 2007-03-08 22:42:19 -0800 | [diff] [blame] | 897 | pci_resource_adjust(&zero_res, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | region->start = res->start - zero_res.start; |
| 900 | region->end = res->end - zero_res.start; |
| 901 | } |
David S. Miller | 5fdfd42 | 2006-04-17 13:34:44 -0700 | [diff] [blame] | 902 | EXPORT_SYMBOL(pcibios_resource_to_bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res, |
| 905 | struct pci_bus_region *region) |
| 906 | { |
| 907 | struct pci_pbm_info *pbm = pdev->bus->sysdata; |
| 908 | struct resource *root; |
| 909 | |
| 910 | res->start = region->start; |
| 911 | res->end = region->end; |
| 912 | |
| 913 | if (res->flags & IORESOURCE_IO) |
| 914 | root = &pbm->io_space; |
| 915 | else |
| 916 | root = &pbm->mem_space; |
| 917 | |
David S. Miller | 0bae5f8 | 2007-03-08 22:42:19 -0800 | [diff] [blame] | 918 | pci_resource_adjust(res, root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } |
Keith Owens | 41290c1 | 2005-08-24 16:06:25 +1000 | [diff] [blame] | 920 | EXPORT_SYMBOL(pcibios_bus_to_resource); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
Robert Reif | f6b45da | 2007-04-12 13:47:37 -0700 | [diff] [blame] | 922 | char * __devinit pcibios_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | return str; |
| 925 | } |
| 926 | |
| 927 | /* Platform support for /proc/bus/pci/X/Y mmap()s. */ |
| 928 | |
| 929 | /* If the user uses a host-bridge as the PCI device, he may use |
| 930 | * this to perform a raw mmap() of the I/O or MEM space behind |
| 931 | * that controller. |
| 932 | * |
| 933 | * This can be useful for execution of x86 PCI bios initialization code |
| 934 | * on a PCI card, like the xfree86 int10 stuff does. |
| 935 | */ |
| 936 | static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma, |
| 937 | enum pci_mmap_state mmap_state) |
| 938 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 939 | struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | unsigned long space_size, user_offset, user_size; |
| 941 | |
David S. Miller | 3875c5c | 2007-03-08 22:52:11 -0800 | [diff] [blame] | 942 | if (mmap_state == pci_mmap_io) { |
| 943 | space_size = (pbm->io_space.end - |
| 944 | pbm->io_space.start) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } else { |
David S. Miller | 3875c5c | 2007-03-08 22:52:11 -0800 | [diff] [blame] | 946 | space_size = (pbm->mem_space.end - |
| 947 | pbm->mem_space.start) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* Make sure the request is in range. */ |
| 951 | user_offset = vma->vm_pgoff << PAGE_SHIFT; |
| 952 | user_size = vma->vm_end - vma->vm_start; |
| 953 | |
| 954 | if (user_offset >= space_size || |
| 955 | (user_offset + user_size) > space_size) |
| 956 | return -EINVAL; |
| 957 | |
David S. Miller | 3875c5c | 2007-03-08 22:52:11 -0800 | [diff] [blame] | 958 | if (mmap_state == pci_mmap_io) { |
| 959 | vma->vm_pgoff = (pbm->io_space.start + |
| 960 | user_offset) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | } else { |
David S. Miller | 3875c5c | 2007-03-08 22:52:11 -0800 | [diff] [blame] | 962 | vma->vm_pgoff = (pbm->mem_space.start + |
| 963 | user_offset) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 969 | /* Adjust vm_pgoff of VMA such that it is the physical page offset |
| 970 | * corresponding to the 32-bit pci bus offset for DEV requested by the user. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | * |
| 972 | * Basically, the user finds the base address for his device which he wishes |
| 973 | * to mmap. They read the 32-bit value from the config space base register, |
| 974 | * add whatever PAGE_SIZE multiple offset they wish, and feed this into the |
| 975 | * offset parameter of mmap on /proc/bus/pci/XXX for that device. |
| 976 | * |
| 977 | * Returns negative error code on failure, zero on success. |
| 978 | */ |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 979 | static int __pci_mmap_make_offset(struct pci_dev *pdev, |
| 980 | struct vm_area_struct *vma, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | enum pci_mmap_state mmap_state) |
| 982 | { |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 983 | unsigned long user_paddr, user_size; |
| 984 | int i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 986 | /* First compute the physical address in vma->vm_pgoff, |
| 987 | * making sure the user offset is within range in the |
| 988 | * appropriate PCI space. |
| 989 | */ |
| 990 | err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state); |
| 991 | if (err) |
| 992 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 994 | /* If this is a mapping on a host bridge, any address |
| 995 | * is OK. |
| 996 | */ |
| 997 | if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST) |
| 998 | return err; |
| 999 | |
| 1000 | /* Otherwise make sure it's in the range for one of the |
| 1001 | * device's resources. |
| 1002 | */ |
| 1003 | user_paddr = vma->vm_pgoff << PAGE_SHIFT; |
| 1004 | user_size = vma->vm_end - vma->vm_start; |
| 1005 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | for (i = 0; i <= PCI_ROM_RESOURCE; i++) { |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 1007 | struct resource *rp = &pdev->resource[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | /* Active? */ |
| 1010 | if (!rp->flags) |
| 1011 | continue; |
| 1012 | |
| 1013 | /* Same type? */ |
| 1014 | if (i == PCI_ROM_RESOURCE) { |
| 1015 | if (mmap_state != pci_mmap_mem) |
| 1016 | continue; |
| 1017 | } else { |
| 1018 | if ((mmap_state == pci_mmap_io && |
| 1019 | (rp->flags & IORESOURCE_IO) == 0) || |
| 1020 | (mmap_state == pci_mmap_mem && |
| 1021 | (rp->flags & IORESOURCE_MEM) == 0)) |
| 1022 | continue; |
| 1023 | } |
| 1024 | |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 1025 | if ((rp->start <= user_paddr) && |
| 1026 | (user_paddr + user_size) <= (rp->end + 1UL)) |
| 1027 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
David S. Miller | bbe0b5e | 2007-10-11 15:41:01 -0700 | [diff] [blame] | 1030 | if (i > PCI_ROM_RESOURCE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | return -EINVAL; |
| 1032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device |
| 1037 | * mapping. |
| 1038 | */ |
| 1039 | static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma, |
| 1040 | enum pci_mmap_state mmap_state) |
| 1041 | { |
| 1042 | vma->vm_flags |= (VM_IO | VM_RESERVED); |
| 1043 | } |
| 1044 | |
| 1045 | /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci |
| 1046 | * device mapping. |
| 1047 | */ |
| 1048 | static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma, |
| 1049 | enum pci_mmap_state mmap_state) |
| 1050 | { |
David S. Miller | a7a6cac | 2005-09-01 21:51:26 -0700 | [diff] [blame] | 1051 | /* Our io_remap_pfn_range takes care of this, do nothing. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* Perform the actual remap of the pages for a PCI device mapping, as appropriate |
| 1055 | * for this architecture. The region in the process to map is described by vm_start |
| 1056 | * and vm_end members of VMA, the base physical address is found in vm_pgoff. |
| 1057 | * The pci device structure is provided so that architectures may make mapping |
| 1058 | * decisions on a per-device or per-bus basis. |
| 1059 | * |
| 1060 | * Returns a negative error code on failure, zero on success. |
| 1061 | */ |
| 1062 | int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma, |
| 1063 | enum pci_mmap_state mmap_state, |
| 1064 | int write_combine) |
| 1065 | { |
| 1066 | int ret; |
| 1067 | |
| 1068 | ret = __pci_mmap_make_offset(dev, vma, mmap_state); |
| 1069 | if (ret < 0) |
| 1070 | return ret; |
| 1071 | |
| 1072 | __pci_mmap_set_flags(dev, vma, mmap_state); |
| 1073 | __pci_mmap_set_pgprot(dev, vma, mmap_state); |
| 1074 | |
David S. Miller | 14778d9 | 2006-03-21 02:29:39 -0800 | [diff] [blame] | 1075 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | ret = io_remap_pfn_range(vma, vma->vm_start, |
| 1077 | vma->vm_pgoff, |
| 1078 | vma->vm_end - vma->vm_start, |
| 1079 | vma->vm_page_prot); |
| 1080 | if (ret) |
| 1081 | return ret; |
| 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | return 0; |
| 1084 | } |
| 1085 | |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 1086 | #ifdef CONFIG_NUMA |
| 1087 | int pcibus_to_node(struct pci_bus *pbus) |
| 1088 | { |
| 1089 | struct pci_pbm_info *pbm = pbus->sysdata; |
| 1090 | |
| 1091 | return pbm->numa_node; |
| 1092 | } |
| 1093 | EXPORT_SYMBOL(pcibus_to_node); |
| 1094 | #endif |
| 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | /* Return the domain nuber for this pci bus */ |
| 1097 | |
| 1098 | int pci_domain_nr(struct pci_bus *pbus) |
| 1099 | { |
| 1100 | struct pci_pbm_info *pbm = pbus->sysdata; |
| 1101 | int ret; |
| 1102 | |
| 1103 | if (pbm == NULL || pbm->parent == NULL) { |
| 1104 | ret = -ENXIO; |
| 1105 | } else { |
David S. Miller | 6c108f1 | 2007-05-07 23:49:01 -0700 | [diff] [blame] | 1106 | ret = pbm->index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | return ret; |
| 1110 | } |
| 1111 | EXPORT_SYMBOL(pci_domain_nr); |
| 1112 | |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1113 | #ifdef CONFIG_PCI_MSI |
| 1114 | int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc) |
| 1115 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 1116 | struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller; |
David S. Miller | e9870c4 | 2007-05-07 23:28:50 -0700 | [diff] [blame] | 1117 | int virt_irq; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1118 | |
David S. Miller | e9870c4 | 2007-05-07 23:28:50 -0700 | [diff] [blame] | 1119 | if (!pbm->setup_msi_irq) |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1120 | return -EINVAL; |
| 1121 | |
David S. Miller | e9870c4 | 2007-05-07 23:28:50 -0700 | [diff] [blame] | 1122 | return pbm->setup_msi_irq(&virt_irq, pdev, desc); |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | void arch_teardown_msi_irq(unsigned int virt_irq) |
| 1126 | { |
David S. Miller | abfd336 | 2007-02-26 09:40:34 -0800 | [diff] [blame] | 1127 | struct msi_desc *entry = get_irq_msi(virt_irq); |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1128 | struct pci_dev *pdev = entry->dev; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 1129 | struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1130 | |
David S. Miller | e9870c4 | 2007-05-07 23:28:50 -0700 | [diff] [blame] | 1131 | if (!pbm->teardown_msi_irq) |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1132 | return; |
| 1133 | |
David S. Miller | e9870c4 | 2007-05-07 23:28:50 -0700 | [diff] [blame] | 1134 | return pbm->teardown_msi_irq(virt_irq, pdev); |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 1135 | } |
| 1136 | #endif /* !(CONFIG_PCI_MSI) */ |
| 1137 | |
David S. Miller | f6d0f9e | 2007-03-01 18:09:18 -0800 | [diff] [blame] | 1138 | struct device_node *pci_device_to_OF_node(struct pci_dev *pdev) |
| 1139 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 1140 | return pdev->dev.archdata.prom_node; |
David S. Miller | f6d0f9e | 2007-03-01 18:09:18 -0800 | [diff] [blame] | 1141 | } |
| 1142 | EXPORT_SYMBOL(pci_device_to_OF_node); |
| 1143 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 1144 | static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit) |
| 1145 | { |
| 1146 | struct pci_dev *ali_isa_bridge; |
| 1147 | u8 val; |
| 1148 | |
| 1149 | /* ALI sound chips generate 31-bits of DMA, a special register |
| 1150 | * determines what bit 31 is emitted as. |
| 1151 | */ |
| 1152 | ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL, |
| 1153 | PCI_DEVICE_ID_AL_M1533, |
| 1154 | NULL); |
| 1155 | |
| 1156 | pci_read_config_byte(ali_isa_bridge, 0x7e, &val); |
| 1157 | if (set_bit) |
| 1158 | val |= 0x01; |
| 1159 | else |
| 1160 | val &= ~0x01; |
| 1161 | pci_write_config_byte(ali_isa_bridge, 0x7e, val); |
| 1162 | pci_dev_put(ali_isa_bridge); |
| 1163 | } |
| 1164 | |
| 1165 | int pci_dma_supported(struct pci_dev *pdev, u64 device_mask) |
| 1166 | { |
| 1167 | u64 dma_addr_mask; |
| 1168 | |
| 1169 | if (pdev == NULL) { |
| 1170 | dma_addr_mask = 0xffffffff; |
| 1171 | } else { |
| 1172 | struct iommu *iommu = pdev->dev.archdata.iommu; |
| 1173 | |
| 1174 | dma_addr_mask = iommu->dma_addr_mask; |
| 1175 | |
| 1176 | if (pdev->vendor == PCI_VENDOR_ID_AL && |
| 1177 | pdev->device == PCI_DEVICE_ID_AL_M5451 && |
| 1178 | device_mask == 0x7fffffff) { |
| 1179 | ali_sound_dma_hack(pdev, |
| 1180 | (dma_addr_mask & 0x80000000) != 0); |
| 1181 | return 1; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | if (device_mask >= (1UL << 32UL)) |
| 1186 | return 0; |
| 1187 | |
| 1188 | return (device_mask & dma_addr_mask) == dma_addr_mask; |
| 1189 | } |
| 1190 | |
David S. Miller | bcea1db | 2007-12-25 02:20:33 -0800 | [diff] [blame] | 1191 | void pci_resource_to_user(const struct pci_dev *pdev, int bar, |
| 1192 | const struct resource *rp, resource_size_t *start, |
| 1193 | resource_size_t *end) |
| 1194 | { |
| 1195 | struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller; |
| 1196 | unsigned long offset; |
| 1197 | |
| 1198 | if (rp->flags & IORESOURCE_IO) |
| 1199 | offset = pbm->io_space.start; |
| 1200 | else |
| 1201 | offset = pbm->mem_space.start; |
| 1202 | |
| 1203 | *start = rp->start - offset; |
| 1204 | *end = rp->end - offset; |
| 1205 | } |