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