1 /* $Id: pci_common.c,v 1.3 1999/09/04 22:26:32 ecd Exp $ 2 * pci_common.c: PCI controller common support. 4 * Copyright (C) 1999 David S. Miller (davem@redhat.com) 7 #include <linux/string.h> 8 #include <linux/malloc.h> 9 #include <linux/init.h> 13 /* Find the OBP PROM device tree node for a PCI device. 14 * Return zero if not found. 16 static int __init
find_device_prom_node(struct pci_pbm_info
*pbm
, 19 struct linux_prom_pci_registers
*pregs
, 25 * Return the PBM's PROM node in case we are it's PCI device, 26 * as the PBM's reg property is different to standard PCI reg 27 * properties. We would delete this device entry otherwise, 28 * which confuses XFree86's device probing... 30 if((pdev
->bus
->number
== pbm
->pci_bus
->number
) && (pdev
->devfn
==0) && 31 (pdev
->vendor
== PCI_VENDOR_ID_SUN
) && 32 (pdev
->device
== PCI_DEVICE_ID_SUN_PBM
)) { 37 node
=prom_getchild(bus_prom_node
); 39 int err
=prom_getproperty(node
,"reg", 41 sizeof(*pregs
) * PROMREG_MAX
); 42 if(err
==0|| err
== -1) 44 if(((pregs
[0].phys_hi
>>8) &0xff) == pdev
->devfn
) { 45 *nregs
= err
/sizeof(*pregs
); 50 node
=prom_getsibling(node
); 55 /* Remove a PCI device from the device trees, then 56 * free it up. Note that this must run before 57 * the device's resources are registered because we 58 * do not handle unregistering them here. 60 static voidpci_device_delete(struct pci_dev
*pdev
) 64 /* First, unlink from list of all devices. */ 75 /* Next, unlink from bus sibling chain. */ 76 dpp
= &pdev
->bus
->devices
; 83 dpp
= &(*dpp
)->sibling
; 86 /* Ok, all references are gone, free it up. */ 90 /* Fill in the PCI device cookie sysdata for the given 91 * PCI device. This cookie is the means by which one 92 * can get to OBP and PCI controller specific information 95 static void __init
pdev_cookie_fillin(struct pci_pbm_info
*pbm
, 99 struct linux_prom_pci_registers pregs
[PROMREG_MAX
]; 100 struct pcidev_cookie
*pcp
; 101 int device_prom_node
, nregs
, err
; 103 device_prom_node
=find_device_prom_node(pbm
, pdev
, bus_prom_node
, 105 if(device_prom_node
==0) { 106 /* If it is not in the OBP device tree then 107 * there must be a damn good reason for it. 109 * So what we do is delete the device from the 110 * PCI device tree completely. This scenerio 111 * is seen, for example, on CP1500 for the 112 * second EBUS/HappyMeal pair if the external 113 * connector for it is not present. 115 pci_device_delete(pdev
); 119 pcp
=kmalloc(sizeof(*pcp
), GFP_ATOMIC
); 121 prom_printf("PCI_COOKIE: Fatal malloc error, aborting...\n"); 125 pcp
->prom_node
= device_prom_node
; 126 memcpy(pcp
->prom_regs
, pregs
,sizeof(pcp
->prom_regs
)); 127 pcp
->num_prom_regs
= nregs
; 128 err
=prom_getproperty(device_prom_node
,"name", 129 pcp
->prom_name
,sizeof(pcp
->prom_name
)); 131 pcp
->prom_name
[err
] =0; 133 pcp
->prom_name
[0] =0; 134 if(strcmp(pcp
->prom_name
,"ebus") ==0) { 135 struct linux_prom_ebus_ranges erng
[PROM_PCIRNG_MAX
]; 138 /* EBUS is special... */ 139 err
=prom_getproperty(device_prom_node
,"ranges", 140 (char*)&erng
[0],sizeof(erng
)); 141 if(err
==0|| err
== -1) { 142 prom_printf("EBUS: Fatal error, no range property\n"); 145 err
= (err
/sizeof(erng
[0])); 146 for(iter
=0; iter
< err
; iter
++) { 147 struct linux_prom_ebus_ranges
*ep
= &erng
[iter
]; 148 struct linux_prom_pci_registers
*ap
; 150 ap
= &pcp
->prom_assignments
[iter
]; 152 ap
->phys_hi
= ep
->parent_phys_hi
; 153 ap
->phys_mid
= ep
->parent_phys_mid
; 154 ap
->phys_lo
= ep
->parent_phys_lo
; 156 ap
->size_lo
= ep
->size
; 158 pcp
->num_prom_assignments
= err
; 160 err
=prom_getproperty(device_prom_node
, 161 "assigned-addresses", 162 (char*)pcp
->prom_assignments
, 163 sizeof(pcp
->prom_assignments
)); 164 if(err
==0|| err
== -1) 165 pcp
->num_prom_assignments
=0; 167 pcp
->num_prom_assignments
= 168 (err
/sizeof(pcp
->prom_assignments
[0])); 174 void __init
pci_fill_in_pbm_cookies(struct pci_bus
*pbus
, 175 struct pci_pbm_info
*pbm
, 178 struct pci_dev
*pdev
; 180 /* This loop is coded like this because the cookie 181 * fillin routine can delete devices from the tree. 183 pdev
= pbus
->devices
; 184 while(pdev
!= NULL
) { 185 struct pci_dev
*next
= pdev
->sibling
; 187 pdev_cookie_fillin(pbm
, pdev
, prom_node
); 192 for(pbus
= pbus
->children
; pbus
; pbus
= pbus
->next
) { 193 struct pcidev_cookie
*pcp
= pbus
->self
->sysdata
; 194 pci_fill_in_pbm_cookies(pbus
, pbm
, pcp
->prom_node
); 198 static void __init
bad_assignment(struct linux_prom_pci_registers
*ap
, 199 struct resource
*res
, 202 prom_printf("PCI: Bogus PROM assignment.\n"); 204 prom_printf("PCI: phys[%08x:%08x:%08x] size[%08x:%08x]\n", 205 ap
->phys_hi
, ap
->phys_mid
, ap
->phys_lo
, 206 ap
->size_hi
, ap
->size_lo
); 208 prom_printf("PCI: RES[%016lx-->%016lx:(%lx)]\n", 209 res
->start
, res
->end
, res
->flags
); 210 prom_printf("Please email this information to davem@redhat.com\n"); 215 static struct resource
* 216 __init
get_root_resource(struct linux_prom_pci_registers
*ap
, 217 struct pci_pbm_info
*pbm
) 219 int space
= (ap
->phys_hi
>>24) &3; 223 /* Configuration space, silently ignore it. */ 227 /* 16-bit IO space */ 228 return&pbm
->io_space
; 231 /* 32-bit MEM space */ 232 return&pbm
->mem_space
; 236 /* 64-bit MEM space, unsupported. */ 237 printk("PCI: 64-bit MEM assignment??? " 238 "Tell davem@redhat.com about it!\n"); 243 static struct resource
* 244 __init
get_device_resource(struct linux_prom_pci_registers
*ap
, 245 struct pci_dev
*pdev
) 247 int breg
= (ap
->phys_hi
&0xff); 248 int space
= (ap
->phys_hi
>>24) &3; 251 case PCI_ROM_ADDRESS
: 252 /* It had better be MEM space. */ 254 bad_assignment(ap
, NULL
,0); 256 return&pdev
->resource
[PCI_ROM_RESOURCE
]; 258 case PCI_BASE_ADDRESS_0
: 259 case PCI_BASE_ADDRESS_1
: 260 case PCI_BASE_ADDRESS_2
: 261 case PCI_BASE_ADDRESS_3
: 262 case PCI_BASE_ADDRESS_4
: 263 case PCI_BASE_ADDRESS_5
: 264 return&pdev
->resource
[(breg
- PCI_BASE_ADDRESS_0
) /4]; 267 bad_assignment(ap
, NULL
,0); 272 static void __init
pdev_record_assignments(struct pci_pbm_info
*pbm
, 273 struct pci_dev
*pdev
) 275 struct pcidev_cookie
*pcp
= pdev
->sysdata
; 278 for(i
=0; i
< pcp
->num_prom_assignments
; i
++) { 279 struct linux_prom_pci_registers
*ap
; 280 struct resource
*root
, *res
; 282 /* The format of this property is specified in 283 * the PCI Bus Binding to IEEE1275-1994. 285 ap
= &pcp
->prom_assignments
[i
]; 286 root
=get_root_resource(ap
, pbm
); 287 res
=get_device_resource(ap
, pdev
); 288 if(root
== NULL
|| res
== NULL
) 291 /* Ok we know which resource this PROM assignment is 292 * for, sanity check it. 294 if((res
->start
&0xffffffffUL
) != ap
->phys_lo
) 295 bad_assignment(ap
, res
,1); 297 /* Adjust the resource into the physical address space 300 pbm
->parent
->resource_adjust(pdev
, res
, root
); 302 if(request_resource(root
, res
) <0) { 303 /* OK, there is some conflict. But this is fine 304 * since we'll reassign it in the fixup pass. 305 * Nevertheless notify the user that OBP made 308 printk(KERN_ERR
"PCI: Address space collision on region %ld " 310 (res
- &pdev
->resource
[0]), pdev
->name
); 315 void __init
pci_record_assignments(struct pci_pbm_info
*pbm
, 316 struct pci_bus
*pbus
) 318 struct pci_dev
*pdev
; 320 for(pdev
= pbus
->devices
; pdev
; pdev
= pdev
->sibling
) 321 pdev_record_assignments(pbm
, pdev
); 323 for(pbus
= pbus
->children
; pbus
; pbus
= pbus
->next
) 324 pci_record_assignments(pbm
, pbus
); 327 static void __init
pdev_assign_unassigned(struct pci_pbm_info
*pbm
, 328 struct pci_dev
*pdev
) 332 int i
, io_seen
, mem_seen
; 334 io_seen
= mem_seen
=0; 335 for(i
=0; i
< PCI_NUM_RESOURCES
; i
++) { 336 struct resource
*root
, *res
; 337 unsigned long size
, min
, max
, align
; 339 res
= &pdev
->resource
[i
]; 341 if(res
->flags
& IORESOURCE_IO
) 343 else if(res
->flags
& IORESOURCE_MEM
) 346 /* If it is already assigned or the resource does 347 * not exist, there is nothing to do. 349 if(res
->parent
!= NULL
|| res
->flags
==0UL) 352 /* Determine the root we allocate from. */ 353 if(res
->flags
& IORESOURCE_IO
) { 354 root
= &pbm
->io_space
; 355 min
= root
->start
+0x400UL
; 358 root
= &pbm
->mem_space
; 360 max
= min
+0x80000000UL
; 363 size
= res
->end
- res
->start
; 365 if(allocate_resource(root
, res
, size
+1, min
, max
, align
) <0) { 367 prom_printf("PCI: Failed to allocate resource %d for %s\n", 372 /* Update PCI config space. */ 373 pbm
->parent
->base_address_update(pdev
, i
); 376 /* Special case, disable the ROM. Several devices 377 * act funny (ie. do not respond to memory space writes) 378 * when it is left enabled. A good example are Qlogic,ISP 381 pci_read_config_dword(pdev
, PCI_ROM_ADDRESS
, ®
); 382 reg
&= ~PCI_ROM_ADDRESS_ENABLE
; 383 pci_write_config_dword(pdev
, PCI_ROM_ADDRESS
, reg
); 385 /* If we saw I/O or MEM resources, enable appropriate 386 * bits in PCI command register. 388 if(io_seen
|| mem_seen
) { 389 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
); 391 cmd
|= PCI_COMMAND_IO
; 393 cmd
|= PCI_COMMAND_MEMORY
; 394 pci_write_config_word(pdev
, PCI_COMMAND
, cmd
); 397 /* If this is a PCI bridge or an IDE controller, 398 * enable bus mastering. In the former case also 399 * set the cache line size correctly. 401 if(((pdev
->class>>8) == PCI_CLASS_BRIDGE_PCI
) || 402 (((pdev
->class>>8) == PCI_CLASS_STORAGE_IDE
) && 403 ((pdev
->class&0x80) !=0))) { 404 pci_read_config_word(pdev
, PCI_COMMAND
, &cmd
); 405 cmd
|= PCI_COMMAND_MASTER
; 406 pci_write_config_word(pdev
, PCI_COMMAND
, cmd
); 408 if((pdev
->class>>8) == PCI_CLASS_BRIDGE_PCI
) 409 pci_write_config_byte(pdev
, 415 void __init
pci_assign_unassigned(struct pci_pbm_info
*pbm
, 416 struct pci_bus
*pbus
) 418 struct pci_dev
*pdev
; 420 for(pdev
= pbus
->devices
; pdev
; pdev
= pdev
->sibling
) 421 pdev_assign_unassigned(pbm
, pdev
); 423 for(pbus
= pbus
->children
; pbus
; pbus
= pbus
->next
) 424 pci_assign_unassigned(pbm
, pbus
); 427 static int __init
pci_intmap_match(struct pci_dev
*pdev
,unsigned int*interrupt
) 429 struct pcidev_cookie
*dev_pcp
= pdev
->sysdata
; 430 struct pci_pbm_info
*pbm
= dev_pcp
->pbm
; 431 struct linux_prom_pci_registers
*pregs
= dev_pcp
->prom_regs
; 432 unsigned int hi
, mid
, lo
, irq
; 435 if(pbm
->num_pbm_intmap
==0) 438 /* If we are underneath a PCI bridge, use PROM register 439 * property of parent bridge. 441 if(pdev
->bus
->number
!= pbm
->pci_first_busno
) { 442 struct pcidev_cookie
*bus_pcp
; 445 bus_pcp
= pdev
->bus
->self
->sysdata
; 446 pregs
= bus_pcp
->prom_regs
; 447 offset
=prom_getint(bus_pcp
->prom_node
, 450 /* Did PROM know better and assign an interrupt other 451 * than #INTA to the device? - We test here for presence of 452 * FCODE on the card, in this case we assume PROM has set 453 * correct 'interrupts' property, unless it is quadhme. 456 !strcmp(bus_pcp
->prom_name
,"SUNW,qfe") || 457 !strcmp(bus_pcp
->prom_name
,"qfe")) { 459 * No, use low slot number bits of child as IRQ line. 461 *interrupt
= ((*interrupt
-1+PCI_SLOT(pdev
->devfn
)) &3) +1; 465 hi
= pregs
->phys_hi
& pbm
->pbm_intmask
.phys_hi
; 466 mid
= pregs
->phys_mid
& pbm
->pbm_intmask
.phys_mid
; 467 lo
= pregs
->phys_lo
& pbm
->pbm_intmask
.phys_lo
; 468 irq
= *interrupt
& pbm
->pbm_intmask
.interrupt
; 470 for(i
=0; i
< pbm
->num_pbm_intmap
; i
++) { 471 if(pbm
->pbm_intmap
[i
].phys_hi
== hi
&& 472 pbm
->pbm_intmap
[i
].phys_mid
== mid
&& 473 pbm
->pbm_intmap
[i
].phys_lo
== lo
&& 474 pbm
->pbm_intmap
[i
].interrupt
== irq
) { 475 *interrupt
= pbm
->pbm_intmap
[i
].cinterrupt
; 480 prom_printf("pbm_intmap_match: bus %02x, devfn %02x: ", 481 pdev
->bus
->number
, pdev
->devfn
); 482 prom_printf("IRQ [%08x.%08x.%08x.%08x] not found in interrupt-map\n", 483 pregs
->phys_hi
, pregs
->phys_mid
, pregs
->phys_lo
, *interrupt
); 484 prom_printf("Please email this information to davem@redhat.com\n"); 488 static void __init
pdev_fixup_irq(struct pci_dev
*pdev
) 490 struct pcidev_cookie
*pcp
= pdev
->sysdata
; 491 struct pci_pbm_info
*pbm
= pcp
->pbm
; 492 struct pci_controller_info
*p
= pbm
->parent
; 493 unsigned int portid
= p
->portid
; 494 unsigned int prom_irq
; 495 int prom_node
= pcp
->prom_node
; 498 err
=prom_getproperty(prom_node
,"interrupts", 499 (char*)&prom_irq
,sizeof(prom_irq
)); 500 if(err
==0|| err
== -1) { 505 /* Fully specified already? */ 506 if(((prom_irq
& PCI_IRQ_IGN
) >>6) == portid
) { 507 pdev
->irq
= p
->irq_build(p
, pdev
, prom_irq
); 511 /* An onboard device? (bit 5 set) */ 512 if((prom_irq
& PCI_IRQ_INO
) &0x20) { 513 pdev
->irq
= p
->irq_build(p
, pdev
, (portid
<<6| prom_irq
)); 517 /* Can we find a matching entry in the interrupt-map? */ 518 if(pci_intmap_match(pdev
, &prom_irq
)) { 519 pdev
->irq
= p
->irq_build(p
, pdev
, (portid
<<6) | prom_irq
); 523 /* Ok, we have to do it the hard way. */ 525 unsigned int bus
, slot
, line
; 527 bus
= (pbm
== &pbm
->parent
->pbm_B
) ? (1<<4) :0; 529 /* If we have a legal interrupt property, use it as 532 if(prom_irq
>0&& prom_irq
<5) { 533 line
= ((prom_irq
-1) &3); 537 /* Else just directly consult PCI config space. */ 538 pci_read_config_byte(pdev
, PCI_INTERRUPT_PIN
, &pci_irq_line
); 539 line
= ((pci_irq_line
-1) &3); 542 /* Now figure out the slot. */ 543 if(pdev
->bus
->number
== pbm
->pci_first_busno
) { 544 if(pbm
== &pbm
->parent
->pbm_A
) 545 slot
= (pdev
->devfn
>>3) -1; 547 slot
= (pdev
->devfn
>>3) -2; 549 if(pbm
== &pbm
->parent
->pbm_A
) 550 slot
= (pdev
->bus
->self
->devfn
>>3) -1; 552 slot
= (pdev
->bus
->self
->devfn
>>3) -2; 556 pdev
->irq
= p
->irq_build(p
, pdev
, 557 ((portid
<<6) & PCI_IRQ_IGN
) | 558 (bus
| slot
| line
)); 562 pci_write_config_byte(pdev
, PCI_INTERRUPT_LINE
, 563 pdev
->irq
& PCI_IRQ_INO
); 566 void __init
pci_fixup_irq(struct pci_pbm_info
*pbm
, 567 struct pci_bus
*pbus
) 569 struct pci_dev
*pdev
; 571 for(pdev
= pbus
->devices
; pdev
; pdev
= pdev
->sibling
) 572 pdev_fixup_irq(pdev
); 574 for(pbus
= pbus
->children
; pbus
; pbus
= pbus
->next
) 575 pci_fixup_irq(pbm
, pbus
); 578 /* Generic helper routines for PCI error reporting. */ 579 voidpci_scan_for_target_abort(struct pci_controller_info
*p
, 580 struct pci_pbm_info
*pbm
, 581 struct pci_bus
*pbus
) 583 struct pci_dev
*pdev
; 585 for(pdev
= pbus
->devices
; pdev
; pdev
= pdev
->sibling
) { 586 u16 status
, error_bits
; 588 pci_read_config_word(pdev
, PCI_STATUS
, &status
); 590 (status
& (PCI_STATUS_SIG_TARGET_ABORT
| 591 PCI_STATUS_REC_TARGET_ABORT
)); 593 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
); 594 printk("PCI%d(PBM%c): Device [%s] saw Target Abort [%016x]\n", 595 p
->index
, ((pbm
== &p
->pbm_A
) ?'A':'B'), 600 for(pbus
= pbus
->children
; pbus
; pbus
= pbus
->next
) 601 pci_scan_for_target_abort(p
, pbm
, pbus
); 604 voidpci_scan_for_master_abort(struct pci_controller_info
*p
, 605 struct pci_pbm_info
*pbm
, 606 struct pci_bus
*pbus
) 608 struct pci_dev
*pdev
; 610 for(pdev
= pbus
->devices
; pdev
; pdev
= pdev
->sibling
) { 611 u16 status
, error_bits
; 613 pci_read_config_word(pdev
, PCI_STATUS
, &status
); 615 (status
& (PCI_STATUS_REC_MASTER_ABORT
)); 617 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
); 618 printk("PCI%d(PBM%c): Device [%s] received Master Abort [%016x]\n", 619 p
->index
, ((pbm
== &p
->pbm_A
) ?'A':'B'), 624 for(pbus
= pbus
->children
; pbus
; pbus
= pbus
->next
) 625 pci_scan_for_master_abort(p
, pbm
, pbus
); 628 voidpci_scan_for_parity_error(struct pci_controller_info
*p
, 629 struct pci_pbm_info
*pbm
, 630 struct pci_bus
*pbus
) 632 struct pci_dev
*pdev
; 634 for(pdev
= pbus
->devices
; pdev
; pdev
= pdev
->sibling
) { 635 u16 status
, error_bits
; 637 pci_read_config_word(pdev
, PCI_STATUS
, &status
); 639 (status
& (PCI_STATUS_PARITY
| 640 PCI_STATUS_DETECTED_PARITY
)); 642 pci_write_config_word(pdev
, PCI_STATUS
, error_bits
); 643 printk("PCI%d(PBM%c): Device [%s] saw Parity Error [%016x]\n", 644 p
->index
, ((pbm
== &p
->pbm_A
) ?'A':'B'), 649 for(pbus
= pbus
->children
; pbus
; pbus
= pbus
->next
) 650 pci_scan_for_parity_error(p
, pbm
, pbus
);