home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Linux / PCI / pci-pc.c < prev    next >
C/C++ Source or Header  |  2001-12-07  |  32KB  |  1,286 lines

  1. /*
  2.  *    Low-Level PCI Support for PC
  3.  *
  4.  *    (c) 1999--2000 Martin Mares <mj@ucw.cz>
  5.  */
  6.  
  7. #include <linux/config.h>
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/pci.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14.  
  15. #include <asm/segment.h>
  16. #include <asm/io.h>
  17.  
  18. #include "pci-i386.h"
  19.  
  20. unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2;
  21.  
  22. int pcibios_last_bus = -1;
  23. struct pci_bus *pci_root_bus = NULL;
  24. struct pci_ops *pci_root_ops = NULL;
  25.  
  26. int (*pci_config_read)(int seg, int bus, int dev, int fn, int reg, int len, u32 *value) = NULL;
  27. int (*pci_config_write)(int seg, int bus, int dev, int fn, int reg, int len, u32 value) = NULL;
  28.  
  29. /*
  30.  * This interrupt-safe spinlock protects all accesses to PCI
  31.  * configuration space.
  32.  */
  33. spinlock_t pci_config_lock = SPIN_LOCK_UNLOCKED;
  34.  
  35.  
  36. /*
  37.  * Functions for accessing PCI configuration space with type 1 accesses
  38.  */
  39.  
  40. #ifdef CONFIG_PCI_DIRECT
  41.  
  42. #define PCI_CONF1_ADDRESS(bus, dev, fn, reg) \
  43.     (0x80000000 | (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3))
  44.  
  45. static int pci_conf1_read (int seg, int bus, int dev, int fn, int reg, int len, u32 *value)
  46. {
  47.     unsigned long flags;
  48.  
  49.     if (!value || (bus > 255) || (dev > 31) || (fn > 7) || (reg > 255))
  50.         return -EINVAL;
  51.  
  52.     spin_lock_irqsave(&pci_config_lock, flags);
  53.  
  54.     outl(PCI_CONF1_ADDRESS(bus, dev, fn, reg), 0xCF8);
  55.  
  56.     switch (len) {
  57.     case 1:
  58.         *value = inb(0xCFC + (reg & 3));
  59.         break;
  60.     case 2:
  61.         *value = inw(0xCFC + (reg & 2));
  62.         break;
  63.     case 4:
  64.         *value = inl(0xCFC);
  65.         break;
  66.     }
  67.  
  68.     spin_unlock_irqrestore(&pci_config_lock, flags);
  69.  
  70.     return 0;
  71. }
  72.  
  73. static int pci_conf1_write (int seg, int bus, int dev, int fn, int reg, int len, u32 value)
  74. {
  75.     unsigned long flags;
  76.  
  77.     if ((bus > 255) || (dev > 31) || (fn > 7) || (reg > 255)) 
  78.         return -EINVAL;
  79.  
  80.     spin_lock_irqsave(&pci_config_lock, flags);
  81.  
  82.     outl(PCI_CONF1_ADDRESS(bus, dev, fn, reg), 0xCF8);
  83.  
  84.     switch (len) {
  85.     case 1:
  86.         outb((u8)value, 0xCFC + (reg & 3));
  87.         break;
  88.     case 2:
  89.         outw((u16)value, 0xCFC + (reg & 2));
  90.         break;
  91.     case 4:
  92.         outl((u32)value, 0xCFC);
  93.         break;
  94.     }
  95.  
  96.     spin_unlock_irqrestore(&pci_config_lock, flags);
  97.  
  98.     return 0;
  99. }
  100.  
  101. #undef PCI_CONF1_ADDRESS
  102.  
  103. static int pci_conf1_read_config_byte(struct pci_dev *dev, int where, u8 *value)
  104. {
  105.     int result; 
  106.     u32 data;
  107.  
  108.     if (!value) 
  109.         return -EINVAL;
  110.  
  111.     result = pci_conf1_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  112.         PCI_FUNC(dev->devfn), where, 1, &data);
  113.  
  114.     *value = (u8)data;
  115.  
  116.     return result;
  117. }
  118.  
  119. static int pci_conf1_read_config_word(struct pci_dev *dev, int where, u16 *value)
  120. {
  121.     int result; 
  122.     u32 data;
  123.  
  124.     if (!value) 
  125.         return -EINVAL;
  126.  
  127.     result = pci_conf1_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  128.         PCI_FUNC(dev->devfn), where, 2, &data);
  129.  
  130.     *value = (u16)data;
  131.  
  132.     return result;
  133. }
  134.  
  135. static int pci_conf1_read_config_dword(struct pci_dev *dev, int where, u32 *value)
  136. {
  137.     if (!value) 
  138.         return -EINVAL;
  139.  
  140.     return pci_conf1_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  141.         PCI_FUNC(dev->devfn), where, 4, value);
  142. }
  143.  
  144. static int pci_conf1_write_config_byte(struct pci_dev *dev, int where, u8 value)
  145. {
  146.     return pci_conf1_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  147.         PCI_FUNC(dev->devfn), where, 1, value);
  148. }
  149.  
  150. static int pci_conf1_write_config_word(struct pci_dev *dev, int where, u16 value)
  151. {
  152.     return pci_conf1_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  153.         PCI_FUNC(dev->devfn), where, 2, value);
  154. }
  155.  
  156. static int pci_conf1_write_config_dword(struct pci_dev *dev, int where, u32 value)
  157. {
  158.     return pci_conf1_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  159.         PCI_FUNC(dev->devfn), where, 4, value);
  160. }
  161.  
  162. static struct pci_ops pci_direct_conf1 = {
  163.     pci_conf1_read_config_byte,
  164.     pci_conf1_read_config_word,
  165.     pci_conf1_read_config_dword,
  166.     pci_conf1_write_config_byte,
  167.     pci_conf1_write_config_word,
  168.     pci_conf1_write_config_dword
  169. };
  170.  
  171.  
  172. /*
  173.  * Functions for accessing PCI configuration space with type 2 accesses
  174.  */
  175.  
  176. #define PCI_CONF2_ADDRESS(dev, reg)    (u16)(0xC000 | (dev << 8) | reg)
  177.  
  178. static int pci_conf2_read (int seg, int bus, int dev, int fn, int reg, int len, u32 *value)
  179. {
  180.     unsigned long flags;
  181.  
  182.     if (!value || (bus > 255) || (dev > 31) || (fn > 7) || (reg > 255))
  183.         return -EINVAL;
  184.  
  185.     if (dev & 0x10) 
  186.         return PCIBIOS_DEVICE_NOT_FOUND;
  187.  
  188.     spin_lock_irqsave(&pci_config_lock, flags);
  189.  
  190.     outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  191.     outb((u8)bus, 0xCFA);
  192.  
  193.     switch (len) {
  194.     case 1:
  195.         *value = inb(PCI_CONF2_ADDRESS(dev, reg));
  196.         break;
  197.     case 2:
  198.         *value = inw(PCI_CONF2_ADDRESS(dev, reg));
  199.         break;
  200.     case 4:
  201.         *value = inl(PCI_CONF2_ADDRESS(dev, reg));
  202.         break;
  203.     }
  204.  
  205.     outb (0, 0xCF8);
  206.  
  207.     spin_unlock_irqrestore(&pci_config_lock, flags);
  208.  
  209.     return 0;
  210. }
  211.  
  212. static int pci_conf2_write (int seg, int bus, int dev, int fn, int reg, int len, u32 value)
  213. {
  214.     unsigned long flags;
  215.  
  216.     if ((bus > 255) || (dev > 31) || (fn > 7) || (reg > 255)) 
  217.         return -EINVAL;
  218.  
  219.     if (dev & 0x10) 
  220.         return PCIBIOS_DEVICE_NOT_FOUND;
  221.  
  222.     spin_lock_irqsave(&pci_config_lock, flags);
  223.  
  224.     outb((u8)(0xF0 | (fn << 1)), 0xCF8);
  225.     outb((u8)bus, 0xCFA);
  226.  
  227.     switch (len) {
  228.     case 1:
  229.         outb ((u8)value, PCI_CONF2_ADDRESS(dev, reg));
  230.         break;
  231.     case 2:
  232.         outw ((u16)value, PCI_CONF2_ADDRESS(dev, reg));
  233.         break;
  234.     case 4:
  235.         outl ((u32)value, PCI_CONF2_ADDRESS(dev, reg));
  236.         break;
  237.     }
  238.  
  239.     outb (0, 0xCF8);    
  240.  
  241.     spin_unlock_irqrestore(&pci_config_lock, flags);
  242.  
  243.     return 0;
  244. }
  245.  
  246. #undef PCI_CONF2_ADDRESS
  247.  
  248. static int pci_conf2_read_config_byte(struct pci_dev *dev, int where, u8 *value)
  249. {
  250.     int result; 
  251.     u32 data;
  252.     result = pci_conf2_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  253.         PCI_FUNC(dev->devfn), where, 1, &data);
  254.     *value = (u8)data;
  255.     return result;
  256. }
  257.  
  258. static int pci_conf2_read_config_word(struct pci_dev *dev, int where, u16 *value)
  259. {
  260.     int result; 
  261.     u32 data;
  262.     result = pci_conf2_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  263.         PCI_FUNC(dev->devfn), where, 2, &data);
  264.     *value = (u16)data;
  265.     return result;
  266. }
  267.  
  268. static int pci_conf2_read_config_dword(struct pci_dev *dev, int where, u32 *value)
  269. {
  270.     return pci_conf2_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  271.         PCI_FUNC(dev->devfn), where, 4, value);
  272. }
  273.  
  274. static int pci_conf2_write_config_byte(struct pci_dev *dev, int where, u8 value)
  275. {
  276.     return pci_conf2_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  277.         PCI_FUNC(dev->devfn), where, 1, value);
  278. }
  279.  
  280. static int pci_conf2_write_config_word(struct pci_dev *dev, int where, u16 value)
  281. {
  282.     return pci_conf2_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  283.         PCI_FUNC(dev->devfn), where, 2, value);
  284. }
  285.  
  286. static int pci_conf2_write_config_dword(struct pci_dev *dev, int where, u32 value)
  287. {
  288.     return pci_conf2_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  289.         PCI_FUNC(dev->devfn), where, 4, value);
  290. }
  291.  
  292. static struct pci_ops pci_direct_conf2 = {
  293.     pci_conf2_read_config_byte,
  294.     pci_conf2_read_config_word,
  295.     pci_conf2_read_config_dword,
  296.     pci_conf2_write_config_byte,
  297.     pci_conf2_write_config_word,
  298.     pci_conf2_write_config_dword
  299. };
  300.  
  301.  
  302. /*
  303.  * Before we decide to use direct hardware access mechanisms, we try to do some
  304.  * trivial checks to ensure it at least _seems_ to be working -- we just test
  305.  * whether bus 00 contains a host bridge (this is similar to checking
  306.  * techniques used in XFree86, but ours should be more reliable since we
  307.  * attempt to make use of direct access hints provided by the PCI BIOS).
  308.  *
  309.  * This should be close to trivial, but it isn't, because there are buggy
  310.  * chipsets (yes, you guessed it, by Intel and Compaq) that have no class ID.
  311.  */
  312. static int __devinit pci_sanity_check(struct pci_ops *o)
  313. {
  314.     u16 x;
  315.     struct pci_bus bus;        /* Fake bus and device */
  316.     struct pci_dev dev;
  317.  
  318.     if (pci_probe & PCI_NO_CHECKS)
  319.         return 1;
  320.     bus.number = 0;
  321.     dev.bus = &bus;
  322.     for(dev.devfn=0; dev.devfn < 0x100; dev.devfn++)
  323.         if ((!o->read_word(&dev, PCI_CLASS_DEVICE, &x) &&
  324.              (x == PCI_CLASS_BRIDGE_HOST || x == PCI_CLASS_DISPLAY_VGA)) ||
  325.             (!o->read_word(&dev, PCI_VENDOR_ID, &x) &&
  326.              (x == PCI_VENDOR_ID_INTEL || x == PCI_VENDOR_ID_COMPAQ)))
  327.             return 1;
  328.     DBG("PCI: Sanity check failed\n");
  329.     return 0;
  330. }
  331.  
  332. static struct pci_ops * __devinit pci_check_direct(void)
  333. {
  334.     unsigned int tmp;
  335.     unsigned long flags;
  336.  
  337.     __save_flags(flags); __cli();
  338.  
  339.     /*
  340.      * Check if configuration type 1 works.
  341.      */
  342.     if (pci_probe & PCI_PROBE_CONF1) {
  343.         outb (0x01, 0xCFB);
  344.         tmp = inl (0xCF8);
  345.         outl (0x80000000, 0xCF8);
  346.         if (inl (0xCF8) == 0x80000000 &&
  347.             pci_sanity_check(&pci_direct_conf1)) {
  348.             outl (tmp, 0xCF8);
  349.             __restore_flags(flags);
  350.             printk("PCI: Using configuration type 1\n");
  351.             request_region(0xCF8, 8, "PCI conf1");
  352.             return &pci_direct_conf1;
  353.         }
  354.         outl (tmp, 0xCF8);
  355.     }
  356.  
  357.     /*
  358.      * Check if configuration type 2 works.
  359.      */
  360.     if (pci_probe & PCI_PROBE_CONF2) {
  361.         outb (0x00, 0xCFB);
  362.         outb (0x00, 0xCF8);
  363.         outb (0x00, 0xCFA);
  364.         if (inb (0xCF8) == 0x00 && inb (0xCFA) == 0x00 &&
  365.             pci_sanity_check(&pci_direct_conf2)) {
  366.             __restore_flags(flags);
  367.             printk("PCI: Using configuration type 2\n");
  368.             request_region(0xCF8, 4, "PCI conf2");
  369.             return &pci_direct_conf2;
  370.         }
  371.     }
  372.  
  373.     __restore_flags(flags);
  374.     return NULL;
  375. }
  376.  
  377. #endif
  378.  
  379. /*
  380.  * BIOS32 and PCI BIOS handling.
  381.  */
  382.  
  383. #ifdef CONFIG_PCI_BIOS
  384.  
  385. #define PCIBIOS_PCI_FUNCTION_ID     0xb1XX
  386. #define PCIBIOS_PCI_BIOS_PRESENT     0xb101
  387. #define PCIBIOS_FIND_PCI_DEVICE        0xb102
  388. #define PCIBIOS_FIND_PCI_CLASS_CODE    0xb103
  389. #define PCIBIOS_GENERATE_SPECIAL_CYCLE    0xb106
  390. #define PCIBIOS_READ_CONFIG_BYTE    0xb108
  391. #define PCIBIOS_READ_CONFIG_WORD    0xb109
  392. #define PCIBIOS_READ_CONFIG_DWORD    0xb10a
  393. #define PCIBIOS_WRITE_CONFIG_BYTE    0xb10b
  394. #define PCIBIOS_WRITE_CONFIG_WORD    0xb10c
  395. #define PCIBIOS_WRITE_CONFIG_DWORD    0xb10d
  396. #define PCIBIOS_GET_ROUTING_OPTIONS    0xb10e
  397. #define PCIBIOS_SET_PCI_HW_INT        0xb10f
  398.  
  399. /* BIOS32 signature: "_32_" */
  400. #define BIOS32_SIGNATURE    (('_' << 0) + ('3' << 8) + ('2' << 16) + ('_' << 24))
  401.  
  402. /* PCI signature: "PCI " */
  403. #define PCI_SIGNATURE        (('P' << 0) + ('C' << 8) + ('I' << 16) + (' ' << 24))
  404.  
  405. /* PCI service signature: "$PCI" */
  406. #define PCI_SERVICE        (('$' << 0) + ('P' << 8) + ('C' << 16) + ('I' << 24))
  407.  
  408. /* PCI BIOS hardware mechanism flags */
  409. #define PCIBIOS_HW_TYPE1        0x01
  410. #define PCIBIOS_HW_TYPE2        0x02
  411. #define PCIBIOS_HW_TYPE1_SPEC        0x10
  412. #define PCIBIOS_HW_TYPE2_SPEC        0x20
  413.  
  414. /*
  415.  * This is the standard structure used to identify the entry point
  416.  * to the BIOS32 Service Directory, as documented in
  417.  *     Standard BIOS 32-bit Service Directory Proposal
  418.  *     Revision 0.4 May 24, 1993
  419.  *     Phoenix Technologies Ltd.
  420.  *    Norwood, MA
  421.  * and the PCI BIOS specification.
  422.  */
  423.  
  424. union bios32 {
  425.     struct {
  426.         unsigned long signature;    /* _32_ */
  427.         unsigned long entry;        /* 32 bit physical address */
  428.         unsigned char revision;        /* Revision level, 0 */
  429.         unsigned char length;        /* Length in paragraphs should be 01 */
  430.         unsigned char checksum;        /* All bytes must add up to zero */
  431.         unsigned char reserved[5];     /* Must be zero */
  432.     } fields;
  433.     char chars[16];
  434. };
  435.  
  436. /*
  437.  * Physical address of the service directory.  I don't know if we're
  438.  * allowed to have more than one of these or not, so just in case
  439.  * we'll make pcibios_present() take a memory start parameter and store
  440.  * the array there.
  441.  */
  442.  
  443. static struct {
  444.     unsigned long address;
  445.     unsigned short segment;
  446. } bios32_indirect = { 0, __KERNEL_CS };
  447.  
  448. /*
  449.  * Returns the entry point for the given service, NULL on error
  450.  */
  451.  
  452. static unsigned long bios32_service(unsigned long service)
  453. {
  454.     unsigned char return_code;    /* %al */
  455.     unsigned long address;        /* %ebx */
  456.     unsigned long length;        /* %ecx */
  457.     unsigned long entry;        /* %edx */
  458.     unsigned long flags;
  459.  
  460.     __save_flags(flags); __cli();
  461.     __asm__("lcall *(%%edi); cld"
  462.         : "=a" (return_code),
  463.           "=b" (address),
  464.           "=c" (length),
  465.           "=d" (entry)
  466.         : "0" (service),
  467.           "1" (0),
  468.           "D" (&bios32_indirect));
  469.     __restore_flags(flags);
  470.  
  471.     switch (return_code) {
  472.         case 0:
  473.             return address + entry;
  474.         case 0x80:    /* Not present */
  475.             printk("bios32_service(0x%lx): not present\n", service);
  476.             return 0;
  477.         default: /* Shouldn't happen */
  478.             printk("bios32_service(0x%lx): returned 0x%x -- BIOS bug!\n",
  479.                 service, return_code);
  480.             return 0;
  481.     }
  482. }
  483.  
  484. static struct {
  485.     unsigned long address;
  486.     unsigned short segment;
  487. } pci_indirect = { 0, __KERNEL_CS };
  488.  
  489. static int pci_bios_present;
  490.  
  491. static int __devinit check_pcibios(void)
  492. {
  493.     u32 signature, eax, ebx, ecx;
  494.     u8 status, major_ver, minor_ver, hw_mech;
  495.     unsigned long flags, pcibios_entry;
  496.  
  497.     if ((pcibios_entry = bios32_service(PCI_SERVICE))) {
  498.         pci_indirect.address = pcibios_entry + PAGE_OFFSET;
  499.  
  500.         __save_flags(flags); __cli();
  501.         __asm__(
  502.             "lcall *(%%edi); cld\n\t"
  503.             "jc 1f\n\t"
  504.             "xor %%ah, %%ah\n"
  505.             "1:"
  506.             : "=d" (signature),
  507.               "=a" (eax),
  508.               "=b" (ebx),
  509.               "=c" (ecx)
  510.             : "1" (PCIBIOS_PCI_BIOS_PRESENT),
  511.               "D" (&pci_indirect)
  512.             : "memory");
  513.         __restore_flags(flags);
  514.  
  515.         status = (eax >> 8) & 0xff;
  516.         hw_mech = eax & 0xff;
  517.         major_ver = (ebx >> 8) & 0xff;
  518.         minor_ver = ebx & 0xff;
  519.         if (pcibios_last_bus < 0)
  520.             pcibios_last_bus = ecx & 0xff;
  521.         DBG("PCI: BIOS probe returned s=%02x hw=%02x ver=%02x.%02x l=%02x\n",
  522.             status, hw_mech, major_ver, minor_ver, pcibios_last_bus);
  523.         if (status || signature != PCI_SIGNATURE) {
  524.             printk (KERN_ERR "PCI: BIOS BUG #%x[%08x] found\n",
  525.                 status, signature);
  526.             return 0;
  527.         }
  528.         printk("PCI: PCI BIOS revision %x.%02x entry at 0x%lx, last bus=%d\n",
  529.             major_ver, minor_ver, pcibios_entry, pcibios_last_bus);
  530. #ifdef CONFIG_PCI_DIRECT
  531.         if (!(hw_mech & PCIBIOS_HW_TYPE1))
  532.             pci_probe &= ~PCI_PROBE_CONF1;
  533.         if (!(hw_mech & PCIBIOS_HW_TYPE2))
  534.             pci_probe &= ~PCI_PROBE_CONF2;
  535. #endif
  536.         return 1;
  537.     }
  538.     return 0;
  539. }
  540.  
  541. static int __devinit pci_bios_find_device (unsigned short vendor, unsigned short device_id,
  542.                     unsigned short index, unsigned char *bus, unsigned char *device_fn)
  543. {
  544.     unsigned short bx;
  545.     unsigned short ret;
  546.  
  547.     __asm__("lcall *(%%edi); cld\n\t"
  548.         "jc 1f\n\t"
  549.         "xor %%ah, %%ah\n"
  550.         "1:"
  551.         : "=b" (bx),
  552.           "=a" (ret)
  553.         : "1" (PCIBIOS_FIND_PCI_DEVICE),
  554.           "c" (device_id),
  555.           "d" (vendor),
  556.           "S" ((int) index),
  557.           "D" (&pci_indirect));
  558.     *bus = (bx >> 8) & 0xff;
  559.     *device_fn = bx & 0xff;
  560.     return (int) (ret & 0xff00) >> 8;
  561. }
  562.  
  563. static int pci_bios_read (int seg, int bus, int dev, int fn, int reg, int len, u32 *value)
  564. {
  565.     unsigned long result = 0;
  566.     unsigned long flags;
  567.     unsigned long bx = ((bus << 8) | (dev << 3) | fn);
  568.  
  569.     if (!value || (bus > 255) || (dev > 31) || (fn > 7) || (reg > 255))
  570.         return -EINVAL;
  571.  
  572.     spin_lock_irqsave(&pci_config_lock, flags);
  573.  
  574.     switch (len) {
  575.     case 1:
  576.         __asm__("lcall *(%%esi); cld\n\t"
  577.             "jc 1f\n\t"
  578.             "xor %%ah, %%ah\n"
  579.             "1:"
  580.             : "=c" (*value),
  581.               "=a" (result)
  582.             : "1" (PCIBIOS_READ_CONFIG_BYTE),
  583.               "b" (bx),
  584.               "D" ((long)reg),
  585.               "S" (&pci_indirect));
  586.         break;
  587.     case 2:
  588.         __asm__("lcall *(%%esi); cld\n\t"
  589.             "jc 1f\n\t"
  590.             "xor %%ah, %%ah\n"
  591.             "1:"
  592.             : "=c" (*value),
  593.               "=a" (result)
  594.             : "1" (PCIBIOS_READ_CONFIG_WORD),
  595.               "b" (bx),
  596.               "D" ((long)reg),
  597.               "S" (&pci_indirect));
  598.         break;
  599.     case 4:
  600.         __asm__("lcall *(%%esi); cld\n\t"
  601.             "jc 1f\n\t"
  602.             "xor %%ah, %%ah\n"
  603.             "1:"
  604.             : "=c" (*value),
  605.               "=a" (result)
  606.             : "1" (PCIBIOS_READ_CONFIG_DWORD),
  607.               "b" (bx),
  608.               "D" ((long)reg),
  609.               "S" (&pci_indirect));
  610.         break;
  611.     }
  612.  
  613.     spin_unlock_irqrestore(&pci_config_lock, flags);
  614.  
  615.     return (int)((result & 0xff00) >> 8);
  616. }
  617.  
  618. static int pci_bios_write (int seg, int bus, int dev, int fn, int reg, int len, u32 value)
  619. {
  620.     unsigned long result = 0;
  621.     unsigned long flags;
  622.     unsigned long bx = ((bus << 8) | (dev << 3) | fn);
  623.  
  624.     if ((bus > 255) || (dev > 31) || (fn > 7) || (reg > 255)) 
  625.         return -EINVAL;
  626.  
  627.     spin_lock_irqsave(&pci_config_lock, flags);
  628.  
  629.     switch (len) {
  630.     case 1:
  631.         __asm__("lcall *(%%esi); cld\n\t"
  632.             "jc 1f\n\t"
  633.             "xor %%ah, %%ah\n"
  634.             "1:"
  635.             : "=a" (result)
  636.             : "0" (PCIBIOS_WRITE_CONFIG_BYTE),
  637.               "c" (value),
  638.               "b" (bx),
  639.               "D" ((long)reg),
  640.               "S" (&pci_indirect));
  641.         break;
  642.     case 2:
  643.         __asm__("lcall *(%%esi); cld\n\t"
  644.             "jc 1f\n\t"
  645.             "xor %%ah, %%ah\n"
  646.             "1:"
  647.             : "=a" (result)
  648.             : "0" (PCIBIOS_WRITE_CONFIG_WORD),
  649.               "c" (value),
  650.               "b" (bx),
  651.               "D" ((long)reg),
  652.               "S" (&pci_indirect));
  653.         break;
  654.     case 4:
  655.         __asm__("lcall *(%%esi); cld\n\t"
  656.             "jc 1f\n\t"
  657.             "xor %%ah, %%ah\n"
  658.             "1:"
  659.             : "=a" (result)
  660.             : "0" (PCIBIOS_WRITE_CONFIG_DWORD),
  661.               "c" (value),
  662.               "b" (bx),
  663.               "D" ((long)reg),
  664.               "S" (&pci_indirect));
  665.         break;
  666.     }
  667.  
  668.     spin_unlock_irqrestore(&pci_config_lock, flags);
  669.  
  670.     return (int)((result & 0xff00) >> 8);
  671. }
  672.  
  673. static int pci_bios_read_config_byte(struct pci_dev *dev, int where, u8 *value)
  674. {
  675.     int result; 
  676.     u32 data;
  677.  
  678.     if (!value) 
  679.         return -EINVAL;
  680.  
  681.     result = pci_bios_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  682.         PCI_FUNC(dev->devfn), where, 1, &data);
  683.  
  684.     *value = (u8)data;
  685.  
  686.     return result;
  687. }
  688.  
  689. static int pci_bios_read_config_word(struct pci_dev *dev, int where, u16 *value)
  690. {
  691.     int result; 
  692.     u32 data;
  693.  
  694.     if (!value) 
  695.         return -EINVAL;
  696.  
  697.     result = pci_bios_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  698.         PCI_FUNC(dev->devfn), where, 2, &data);
  699.  
  700.     *value = (u16)data;
  701.  
  702.     return result;
  703. }
  704.  
  705. static int pci_bios_read_config_dword(struct pci_dev *dev, int where, u32 *value)
  706. {
  707.     if (!value) 
  708.         return -EINVAL;
  709.     
  710.     return pci_bios_read(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  711.         PCI_FUNC(dev->devfn), where, 4, value);
  712. }
  713.  
  714. static int pci_bios_write_config_byte(struct pci_dev *dev, int where, u8 value)
  715. {
  716.     return pci_bios_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  717.         PCI_FUNC(dev->devfn), where, 1, value);
  718. }
  719.  
  720. static int pci_bios_write_config_word(struct pci_dev *dev, int where, u16 value)
  721. {
  722.     return pci_bios_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  723.         PCI_FUNC(dev->devfn), where, 2, value);
  724. }
  725.  
  726. static int pci_bios_write_config_dword(struct pci_dev *dev, int where, u32 value)
  727. {
  728.     return pci_bios_write(0, dev->bus->number, PCI_SLOT(dev->devfn), 
  729.         PCI_FUNC(dev->devfn), where, 4, value);
  730. }
  731.  
  732.  
  733. /*
  734.  * Function table for BIOS32 access
  735.  */
  736.  
  737. static struct pci_ops pci_bios_access = {
  738.       pci_bios_read_config_byte,
  739.       pci_bios_read_config_word,
  740.       pci_bios_read_config_dword,
  741.       pci_bios_write_config_byte,
  742.       pci_bios_write_config_word,
  743.       pci_bios_write_config_dword
  744. };
  745.  
  746. /*
  747.  * Try to find PCI BIOS.
  748.  */
  749.  
  750. static struct pci_ops * __devinit pci_find_bios(void)
  751. {
  752.     union bios32 *check;
  753.     unsigned char sum;
  754.     int i, length;
  755.  
  756.     /*
  757.      * Follow the standard procedure for locating the BIOS32 Service
  758.      * directory by scanning the permissible address range from
  759.      * 0xe0000 through 0xfffff for a valid BIOS32 structure.
  760.      */
  761.  
  762.     for (check = (union bios32 *) __va(0xe0000);
  763.          check <= (union bios32 *) __va(0xffff0);
  764.          ++check) {
  765.         if (check->fields.signature != BIOS32_SIGNATURE)
  766.             continue;
  767.         length = check->fields.length * 16;
  768.         if (!length)
  769.             continue;
  770.         sum = 0;
  771.         for (i = 0; i < length ; ++i)
  772.             sum += check->chars[i];
  773.         if (sum != 0)
  774.             continue;
  775.         if (check->fields.revision != 0) {
  776.             printk("PCI: unsupported BIOS32 revision %d at 0x%p\n",
  777.                 check->fields.revision, check);
  778.             continue;
  779.         }
  780.         DBG("PCI: BIOS32 Service Directory structure at 0x%p\n", check);
  781.         if (check->fields.entry >= 0x100000) {
  782.             printk("PCI: BIOS32 entry (0x%p) in high memory, cannot use.\n", check);
  783.             return NULL;
  784.         } else {
  785.             unsigned long bios32_entry = check->fields.entry;
  786.             DBG("PCI: BIOS32 Service Directory entry at 0x%lx\n", bios32_entry);
  787.             bios32_indirect.address = bios32_entry + PAGE_OFFSET;
  788.             if (check_pcibios())
  789.                 return &pci_bios_access;
  790.         }
  791.         break;    /* Hopefully more than one BIOS32 cannot happen... */
  792.     }
  793.  
  794.     return NULL;
  795. }
  796.  
  797. /*
  798.  * Sort the device list according to PCI BIOS. Nasty hack, but since some
  799.  * fool forgot to define the `correct' device order in the PCI BIOS specs
  800.  * and we want to be (possibly bug-to-bug ;-]) compatible with older kernels
  801.  * which used BIOS ordering, we are bound to do this...
  802.  */
  803.  
  804. static void __devinit pcibios_sort(void)
  805. {
  806.     LIST_HEAD(sorted_devices);
  807.     struct list_head *ln;
  808.     struct pci_dev *dev, *d;
  809.     int idx, found;
  810.     unsigned char bus, devfn;
  811.  
  812.     DBG("PCI: Sorting device list...\n");
  813.     while (!list_empty(&pci_devices)) {
  814.         ln = pci_devices.next;
  815.         dev = pci_dev_g(ln);
  816.         idx = found = 0;
  817.         while (pci_bios_find_device(dev->vendor, dev->device, idx, &bus, &devfn) == PCIBIOS_SUCCESSFUL) {
  818.             idx++;
  819.             for (ln=pci_devices.next; ln != &pci_devices; ln=ln->next) {
  820.                 d = pci_dev_g(ln);
  821.                 if (d->bus->number == bus && d->devfn == devfn) {
  822.                     list_del(&d->global_list);
  823.                     list_add_tail(&d->global_list, &sorted_devices);
  824.                     if (d == dev)
  825.                         found = 1;
  826.                     break;
  827.                 }
  828.             }
  829.             if (ln == &pci_devices) {
  830.                 printk("PCI: BIOS reporting unknown device %02x:%02x\n", bus, devfn);
  831.                 /*
  832.                  * We must not continue scanning as several buggy BIOSes
  833.                  * return garbage after the last device. Grr.
  834.                  */
  835.                 break;
  836.             }
  837.         }
  838.         if (!found) {
  839.             printk("PCI: Device %02x:%02x not found by BIOS\n",
  840.                 dev->bus->number, dev->devfn);
  841.             list_del(&dev->global_list);
  842.             list_add_tail(&dev->global_list, &sorted_devices);
  843.         }
  844.     }
  845.     list_splice(&sorted_devices, &pci_devices);
  846. }
  847.  
  848. /*
  849.  *  BIOS Functions for IRQ Routing
  850.  */
  851.  
  852. struct irq_routing_options {
  853.     u16 size;
  854.     struct irq_info *table;
  855.     u16 segment;
  856. } __attribute__((packed));
  857.  
  858. struct irq_routing_table * __devinit pcibios_get_irq_routing_table(void)
  859. {
  860.     struct irq_routing_options opt;
  861.     struct irq_routing_table *rt = NULL;
  862.     int ret, map;
  863.     unsigned long page;
  864.  
  865.     if (!pci_bios_present)
  866.         return NULL;
  867.     page = __get_free_page(GFP_KERNEL);
  868.     if (!page)
  869.         return NULL;
  870.     opt.table = (struct irq_info *) page;
  871.     opt.size = PAGE_SIZE;
  872.     opt.segment = __KERNEL_DS;
  873.  
  874.     DBG("PCI: Fetching IRQ routing table... ");
  875.     __asm__("push %%es\n\t"
  876.         "push %%ds\n\t"
  877.         "pop  %%es\n\t"
  878.         "lcall *(%%esi); cld\n\t"
  879.         "pop %%es\n\t"
  880.         "jc 1f\n\t"
  881.         "xor %%ah, %%ah\n"
  882.         "1:"
  883.         : "=a" (ret),
  884.           "=b" (map)
  885.         : "0" (PCIBIOS_GET_ROUTING_OPTIONS),
  886.           "1" (0),
  887.           "D" ((long) &opt),
  888.           "S" (&pci_indirect));
  889.     DBG("OK  ret=%d, size=%d, map=%x\n", ret, opt.size, map);
  890.     if (ret & 0xff00)
  891.         printk(KERN_ERR "PCI: Error %02x when fetching IRQ routing table.\n", (ret >> 8) & 0xff);
  892.     else if (opt.size) {
  893.         rt = kmalloc(sizeof(struct irq_routing_table) + opt.size, GFP_KERNEL);
  894.         if (rt) {
  895.             memset(rt, 0, sizeof(struct irq_routing_table));
  896.             rt->size = opt.size + sizeof(struct irq_routing_table);
  897.             rt->exclusive_irqs = map;
  898.             memcpy(rt->slots, (void *) page, opt.size);
  899.             printk("PCI: Using BIOS Interrupt Routing Table\n");
  900.         }
  901.     }
  902.     free_page(page);
  903.     return rt;
  904. }
  905.  
  906.  
  907. int pcibios_set_irq_routing(struct pci_dev *dev, int pin, int irq)
  908. {
  909.     int ret;
  910.  
  911.     __asm__("lcall *(%%esi); cld\n\t"
  912.         "jc 1f\n\t"
  913.         "xor %%ah, %%ah\n"
  914.         "1:"
  915.         : "=a" (ret)
  916.         : "0" (PCIBIOS_SET_PCI_HW_INT),
  917.           "b" ((dev->bus->number << 8) | dev->devfn),
  918.           "c" ((irq << 8) | (pin + 10)),
  919.           "S" (&pci_indirect));
  920.     return !(ret & 0xff00);
  921. }
  922.  
  923. #endif
  924.  
  925. /*
  926.  * Several buggy motherboards address only 16 devices and mirror
  927.  * them to next 16 IDs. We try to detect this `feature' on all
  928.  * primary buses (those containing host bridges as they are
  929.  * expected to be unique) and remove the ghost devices.
  930.  */
  931.  
  932. static void __devinit pcibios_fixup_ghosts(struct pci_bus *b)
  933. {
  934.     struct list_head *ln, *mn;
  935.     struct pci_dev *d, *e;
  936.     int mirror = PCI_DEVFN(16,0);
  937.     int seen_host_bridge = 0;
  938.     int i;
  939.  
  940.     DBG("PCI: Scanning for ghost devices on bus %d\n", b->number);
  941.     for (ln=b->devices.next; ln != &b->devices; ln=ln->next) {
  942.         d = pci_dev_b(ln);
  943.         if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  944.             seen_host_bridge++;
  945.         for (mn=ln->next; mn != &b->devices; mn=mn->next) {
  946.             e = pci_dev_b(mn);
  947.             if (e->devfn != d->devfn + mirror ||
  948.                 e->vendor != d->vendor ||
  949.                 e->device != d->device ||
  950.                 e->class != d->class)
  951.                 continue;
  952.             for(i=0; i<PCI_NUM_RESOURCES; i++)
  953.                 if (e->resource[i].start != d->resource[i].start ||
  954.                     e->resource[i].end != d->resource[i].end ||
  955.                     e->resource[i].flags != d->resource[i].flags)
  956.                     continue;
  957.             break;
  958.         }
  959.         if (mn == &b->devices)
  960.             return;
  961.     }
  962.     if (!seen_host_bridge)
  963.         return;
  964.     printk("PCI: Ignoring ghost devices on bus %02x\n", b->number);
  965.  
  966.     ln = &b->devices;
  967.     while (ln->next != &b->devices) {
  968.         d = pci_dev_b(ln->next);
  969.         if (d->devfn >= mirror) {
  970.             list_del(&d->global_list);
  971.             list_del(&d->bus_list);
  972.             kfree(d);
  973.         } else
  974.             ln = ln->next;
  975.     }
  976. }
  977.  
  978. /*
  979.  * Discover remaining PCI buses in case there are peer host bridges.
  980.  * We use the number of last PCI bus provided by the PCI BIOS.
  981.  */
  982. static void __devinit pcibios_fixup_peer_bridges(void)
  983. {
  984.     int n;
  985.     struct pci_bus bus;
  986.     struct pci_dev dev;
  987.     u16 l;
  988.  
  989.     if (pcibios_last_bus <= 0 || pcibios_last_bus >= 0xff)
  990.         return;
  991.     DBG("PCI: Peer bridge fixup\n");
  992.     for (n=0; n <= pcibios_last_bus; n++) {
  993.         if (pci_bus_exists(&pci_root_buses, n))
  994.             continue;
  995.         bus.number = n;
  996.         bus.ops = pci_root_ops;
  997.         dev.bus = &bus;
  998.         for(dev.devfn=0; dev.devfn<256; dev.devfn += 8)
  999.             if (!pci_read_config_word(&dev, PCI_VENDOR_ID, &l) &&
  1000.                 l != 0x0000 && l != 0xffff) {
  1001.                 DBG("Found device at %02x:%02x [%04x]\n", n, dev.devfn, l);
  1002.                 printk("PCI: Discovered peer bus %02x\n", n);
  1003.                 pci_scan_bus(n, pci_root_ops, NULL);
  1004.                 break;
  1005.             }
  1006.     }
  1007. }
  1008.  
  1009. /*
  1010.  * Exceptions for specific devices. Usually work-arounds for fatal design flaws.
  1011.  */
  1012.  
  1013. static void __devinit pci_fixup_i450nx(struct pci_dev *d)
  1014. {
  1015.     /*
  1016.      * i450NX -- Find and scan all secondary buses on all PXB's.
  1017.      */
  1018.     int pxb, reg;
  1019.     u8 busno, suba, subb;
  1020.     printk("PCI: Searching for i450NX host bridges on %s\n", d->slot_name);
  1021.     reg = 0xd0;
  1022.     for(pxb=0; pxb<2; pxb++) {
  1023.         pci_read_config_byte(d, reg++, &busno);
  1024.         pci_read_config_byte(d, reg++, &suba);
  1025.         pci_read_config_byte(d, reg++, &subb);
  1026.         DBG("i450NX PXB %d: %02x/%02x/%02x\n", pxb, busno, suba, subb);
  1027.         if (busno)
  1028.             pci_scan_bus(busno, pci_root_ops, NULL);    /* Bus A */
  1029.         if (suba < subb)
  1030.             pci_scan_bus(suba+1, pci_root_ops, NULL);    /* Bus B */
  1031.     }
  1032.     pcibios_last_bus = -1;
  1033. }
  1034.  
  1035. static void __devinit pci_fixup_i450gx(struct pci_dev *d)
  1036. {
  1037.     /*
  1038.      * i450GX and i450KX -- Find and scan all secondary buses.
  1039.      * (called separately for each PCI bridge found)
  1040.      */
  1041.     u8 busno;
  1042.     pci_read_config_byte(d, 0x4a, &busno);
  1043.     printk("PCI: i440KX/GX host bridge %s: secondary bus %02x\n", d->slot_name, busno);
  1044.     pci_scan_bus(busno, pci_root_ops, NULL);
  1045.     pcibios_last_bus = -1;
  1046. }
  1047.  
  1048. static void __devinit  pci_fixup_umc_ide(struct pci_dev *d)
  1049. {
  1050.     /*
  1051.      * UM8886BF IDE controller sets region type bits incorrectly,
  1052.      * therefore they look like memory despite of them being I/O.
  1053.      */
  1054.     int i;
  1055.  
  1056.     printk("PCI: Fixing base address flags for device %s\n", d->slot_name);
  1057.     for(i=0; i<4; i++)
  1058.         d->resource[i].flags |= PCI_BASE_ADDRESS_SPACE_IO;
  1059. }
  1060.  
  1061. static void __devinit pci_fixup_ide_bases(struct pci_dev *d)
  1062. {
  1063.     int i;
  1064.  
  1065.     /*
  1066.      * PCI IDE controllers use non-standard I/O port decoding, respect it.
  1067.      */
  1068.     if ((d->class >> 8) != PCI_CLASS_STORAGE_IDE)
  1069.         return;
  1070.     DBG("PCI: IDE base address fixup for %s\n", d->slot_name);
  1071.     for(i=0; i<4; i++) {
  1072.         struct resource *r = &d->resource[i];
  1073.         if ((r->start & ~0x80) == 0x374) {
  1074.             r->start |= 2;
  1075.             r->end = r->start;
  1076.         }
  1077.     }
  1078. }
  1079.  
  1080. static void __devinit  pci_fixup_ide_trash(struct pci_dev *d)
  1081. {
  1082.     int i;
  1083.  
  1084.     /*
  1085.      * There exist PCI IDE controllers which have utter garbage
  1086.      * in first four base registers. Ignore that.
  1087.      */
  1088.     DBG("PCI: IDE base address trash cleared for %s\n", d->slot_name);
  1089.     for(i=0; i<4; i++)
  1090.         d->resource[i].start = d->resource[i].end = d->resource[i].flags = 0;
  1091. }
  1092.  
  1093. static void __devinit  pci_fixup_latency(struct pci_dev *d)
  1094. {
  1095.     /*
  1096.      *  SiS 5597 and 5598 chipsets require latency timer set to
  1097.      *  at most 32 to avoid lockups.
  1098.      */
  1099.     DBG("PCI: Setting max latency to 32\n");
  1100.     pcibios_max_latency = 32;
  1101. }
  1102.  
  1103. static void __devinit pci_fixup_piix4_acpi(struct pci_dev *d)
  1104. {
  1105.     /*
  1106.      * PIIX4 ACPI device: hardwired IRQ9
  1107.      */
  1108.     d->irq = 9;
  1109. }
  1110.  
  1111. /*
  1112.  * Nobody seems to know what this does. Damn.
  1113.  *
  1114.  * But it does seem to fix some unspecified problem
  1115.  * with 'movntq' copies on Athlons.
  1116.  *
  1117.  * VIA 8363,8622,8361 Northbridges:
  1118.  *  - bits  5, 6, 7 at offset 0x55 need to be turned off
  1119.  * VIA 8367 (KT266x) Northbridges:
  1120.  *  - bits  5, 6, 7 at offset 0x95 need to be turned off
  1121.  */
  1122. static void __init pci_fixup_via_athlon_bug(struct pci_dev *d)
  1123. {
  1124.     u8 v;
  1125.     int where = 0x55;
  1126.  
  1127.     if (d->device == PCI_DEVICE_ID_VIA_8367_0) {
  1128.               where = 0x95; /* the memory write queue timer register is 
  1129.                                  different for the kt266x's: 0x95 not 0x55 */
  1130.     }
  1131.  
  1132.         pci_read_config_byte(d, where, &v);
  1133.     if (v & 0xe0) {
  1134.         printk("Trying to stomp on Athlon bug...\n");
  1135.         v &= 0x1f; /* clear bits 5, 6, 7 */
  1136.         pci_write_config_byte(d, where, v);
  1137.     }
  1138. }
  1139.  
  1140. struct pci_fixup pcibios_fixups[] = {
  1141.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82451NX,    pci_fixup_i450nx },
  1142.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82454GX,    pci_fixup_i450gx },
  1143.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_UMC,    PCI_DEVICE_ID_UMC_UM8886BF,    pci_fixup_umc_ide },
  1144.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_SI,    PCI_DEVICE_ID_SI_5513,        pci_fixup_ide_trash },
  1145.     { PCI_FIXUP_HEADER,    PCI_ANY_ID,        PCI_ANY_ID,            pci_fixup_ide_bases },
  1146.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_SI,    PCI_DEVICE_ID_SI_5597,        pci_fixup_latency },
  1147.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_SI,    PCI_DEVICE_ID_SI_5598,        pci_fixup_latency },
  1148.      { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_INTEL,    PCI_DEVICE_ID_INTEL_82371AB_3,    pci_fixup_piix4_acpi },
  1149.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_VIA,    PCI_DEVICE_ID_VIA_8363_0,    pci_fixup_via_athlon_bug },
  1150.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_VIA,    PCI_DEVICE_ID_VIA_8622,            pci_fixup_via_athlon_bug },
  1151.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_VIA,    PCI_DEVICE_ID_VIA_8361,            pci_fixup_via_athlon_bug },
  1152.     { PCI_FIXUP_HEADER,    PCI_VENDOR_ID_VIA,    PCI_DEVICE_ID_VIA_8367_0,    pci_fixup_via_athlon_bug },
  1153.     { 0 }
  1154. };
  1155.  
  1156. /*
  1157.  *  Called after each bus is probed, but before its children
  1158.  *  are examined.
  1159.  */
  1160.  
  1161. void __devinit  pcibios_fixup_bus(struct pci_bus *b)
  1162. {
  1163.     pcibios_fixup_ghosts(b);
  1164.     pci_read_bridge_bases(b);
  1165. }
  1166.  
  1167.  
  1168. void __devinit pcibios_config_init(void)
  1169. {
  1170.     /*
  1171.      * Try all known PCI access methods. Note that we support using 
  1172.      * both PCI BIOS and direct access, with a preference for direct.
  1173.      */
  1174.  
  1175. #ifdef CONFIG_PCI_BIOS
  1176.     if ((pci_probe & PCI_PROBE_BIOS) 
  1177.         && ((pci_root_ops = pci_find_bios()))) {
  1178.         pci_probe |= PCI_BIOS_SORT;
  1179.         pci_bios_present = 1;
  1180.         pci_config_read = pci_bios_read;
  1181.         pci_config_write = pci_bios_write;
  1182.     }
  1183. #endif
  1184.  
  1185. #ifdef CONFIG_PCI_DIRECT
  1186.     if ((pci_probe & (PCI_PROBE_CONF1 | PCI_PROBE_CONF2)) 
  1187.         && (pci_root_ops = pci_check_direct())) {
  1188.         if (pci_root_ops == &pci_direct_conf1) {
  1189.             pci_config_read = pci_conf1_read;
  1190.             pci_config_write = pci_conf1_write;
  1191.         }
  1192.         else {
  1193.             pci_config_read = pci_conf2_read;
  1194.             pci_config_write = pci_conf2_write;
  1195.         }
  1196.     }
  1197. #endif
  1198.  
  1199.     return;
  1200. }
  1201.  
  1202. void __init pcibios_init(void)
  1203. {
  1204.     if (!pci_root_ops)
  1205.         pcibios_config_init();
  1206.     if (!pci_root_ops) {
  1207.         printk("PCI: System does not support PCI\n");
  1208.         return;
  1209.     }
  1210.  
  1211.     printk("PCI: Probing PCI hardware\n");
  1212.     pci_root_bus = pci_scan_bus(0, pci_root_ops, NULL);
  1213.  
  1214.     pcibios_irq_init();
  1215.     pcibios_fixup_peer_bridges();
  1216.     pcibios_fixup_irqs();
  1217.     pcibios_resource_survey();
  1218.  
  1219. #ifdef CONFIG_PCI_BIOS
  1220.     if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
  1221.         pcibios_sort();
  1222. #endif
  1223. }
  1224.  
  1225. char * __devinit  pcibios_setup(char *str)
  1226. {
  1227.     if (!strcmp(str, "off")) {
  1228.         pci_probe = 0;
  1229.         return NULL;
  1230.     }
  1231. #ifdef CONFIG_PCI_BIOS
  1232.     else if (!strcmp(str, "bios")) {
  1233.         pci_probe = PCI_PROBE_BIOS;
  1234.         return NULL;
  1235.     } else if (!strcmp(str, "nobios")) {
  1236.         pci_probe &= ~PCI_PROBE_BIOS;
  1237.         return NULL;
  1238.     } else if (!strcmp(str, "nosort")) {
  1239.         pci_probe |= PCI_NO_SORT;
  1240.         return NULL;
  1241.     } else if (!strcmp(str, "biosirq")) {
  1242.         pci_probe |= PCI_BIOS_IRQ_SCAN;
  1243.         return NULL;
  1244.     }
  1245. #endif
  1246. #ifdef CONFIG_PCI_DIRECT
  1247.     else if (!strcmp(str, "conf1")) {
  1248.         pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
  1249.         return NULL;
  1250.     }
  1251.     else if (!strcmp(str, "conf2")) {
  1252.         pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
  1253.         return NULL;
  1254.     }
  1255. #endif
  1256.     else if (!strcmp(str, "rom")) {
  1257.         pci_probe |= PCI_ASSIGN_ROMS;
  1258.         return NULL;
  1259.     } else if (!strcmp(str, "assign-busses")) {
  1260.         pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  1261.         return NULL;
  1262.     } else if (!strncmp(str, "irqmask=", 8)) {
  1263.         pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
  1264.         return NULL;
  1265.     } else if (!strncmp(str, "lastbus=", 8)) {
  1266.         pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  1267.         return NULL;
  1268.     }
  1269.     return str;
  1270. }
  1271.  
  1272. unsigned int pcibios_assign_all_busses(void)
  1273. {
  1274.     return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  1275. }
  1276.  
  1277. int pcibios_enable_device(struct pci_dev *dev)
  1278. {
  1279.     int err;
  1280.  
  1281.     if ((err = pcibios_enable_resources(dev)) < 0)
  1282.         return err;
  1283.     pcibios_enable_irq(dev);
  1284.     return 0;
  1285. }
  1286.