Subversion Repositories Kolibri OS

Rev

Rev 1628 | Rev 1633 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. #include <ddk.h>
  3. #include <linux/errno.h>
  4. #include <mutex.h>
  5. #include <pci.h>
  6. #include <syscall.h>
  7.  
  8. LIST_HEAD(pci_root_buses);
  9.  
  10. #define IO_SPACE_LIMIT 0xffff
  11.  
  12.  
  13.  
  14.  
  15. #define LEGACY_IO_RESOURCE  (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  16.  
  17. #define CARDBUS_LATENCY_TIMER   176 /* secondary latency timer */
  18. #define CARDBUS_RESERVE_BUSNR   3
  19.  
  20. static int pcibios_assign_all_busses(void)
  21. {
  22.     return 0;
  23. };
  24.  
  25. /**
  26.  * pci_ari_enabled - query ARI forwarding status
  27.  * @bus: the PCI bus
  28.  *
  29.  * Returns 1 if ARI forwarding is enabled, or 0 if not enabled;
  30.  */
  31. static inline int pci_ari_enabled(struct pci_bus *bus)
  32. {
  33.     return bus->self && bus->self->ari_enabled;
  34. }
  35.  
  36. /*
  37.  * Translate the low bits of the PCI base
  38.  * to the resource type
  39.  */
  40. static inline unsigned int pci_calc_resource_flags(unsigned int flags)
  41. {
  42.     if (flags & PCI_BASE_ADDRESS_SPACE_IO)
  43.         return IORESOURCE_IO;
  44.  
  45.     if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  46.         return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  47.  
  48.     return IORESOURCE_MEM;
  49. }
  50.  
  51. static u64 pci_size(u64 base, u64 maxbase, u64 mask)
  52. {
  53.     u64 size = mask & maxbase;  /* Find the significant bits */
  54.     if (!size)
  55.         return 0;
  56.  
  57.     /* Get the lowest of them to find the decode size, and
  58.        from that the extent.  */
  59.     size = (size & ~(size-1)) - 1;
  60.  
  61.     /* base == maxbase can be valid only if the BAR has
  62.        already been programmed with all 1s.  */
  63.     if (base == maxbase && ((base | size) & mask) != mask)
  64.         return 0;
  65.  
  66.     return size;
  67. }
  68.  
  69. static inline enum pci_bar_type decode_bar(struct resource *res, u32 bar)
  70. {
  71.     if ((bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO) {
  72.         res->flags = bar & ~PCI_BASE_ADDRESS_IO_MASK;
  73.         return pci_bar_io;
  74.     }
  75.  
  76.     res->flags = bar & ~PCI_BASE_ADDRESS_MEM_MASK;
  77.  
  78.     if (res->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
  79.         return pci_bar_mem64;
  80.     return pci_bar_mem32;
  81. }
  82.  
  83.  
  84.  
  85. /**
  86.  * pci_read_base - read a PCI BAR
  87.  * @dev: the PCI device
  88.  * @type: type of the BAR
  89.  * @res: resource buffer to be filled in
  90.  * @pos: BAR position in the config space
  91.  *
  92.  * Returns 1 if the BAR is 64-bit, or 0 if 32-bit.
  93.  */
  94. int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
  95.             struct resource *res, unsigned int pos)
  96. {
  97.     u32 l, sz, mask;
  98.     u16 orig_cmd;
  99.  
  100.     mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
  101.  
  102.     if (!dev->mmio_always_on) {
  103.         pci_read_config_word(dev, PCI_COMMAND, &orig_cmd);
  104.         pci_write_config_word(dev, PCI_COMMAND,
  105.             orig_cmd & ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
  106.     }
  107.  
  108.     res->name = pci_name(dev);
  109.  
  110.     pci_read_config_dword(dev, pos, &l);
  111.     pci_write_config_dword(dev, pos, l | mask);
  112.     pci_read_config_dword(dev, pos, &sz);
  113.     pci_write_config_dword(dev, pos, l);
  114.  
  115.     if (!dev->mmio_always_on)
  116.         pci_write_config_word(dev, PCI_COMMAND, orig_cmd);
  117.  
  118.     /*
  119.      * All bits set in sz means the device isn't working properly.
  120.      * If the BAR isn't implemented, all bits must be 0.  If it's a
  121.      * memory BAR or a ROM, bit 0 must be clear; if it's an io BAR, bit
  122.      * 1 must be clear.
  123.      */
  124.     if (!sz || sz == 0xffffffff)
  125.         goto fail;
  126.  
  127.     /*
  128.      * I don't know how l can have all bits set.  Copied from old code.
  129.      * Maybe it fixes a bug on some ancient platform.
  130.      */
  131.     if (l == 0xffffffff)
  132.         l = 0;
  133.  
  134.     if (type == pci_bar_unknown) {
  135.         type = decode_bar(res, l);
  136.         res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
  137.         if (type == pci_bar_io) {
  138.             l &= PCI_BASE_ADDRESS_IO_MASK;
  139.             mask = PCI_BASE_ADDRESS_IO_MASK & IO_SPACE_LIMIT;
  140.         } else {
  141.             l &= PCI_BASE_ADDRESS_MEM_MASK;
  142.             mask = (u32)PCI_BASE_ADDRESS_MEM_MASK;
  143.         }
  144.     } else {
  145.         res->flags |= (l & IORESOURCE_ROM_ENABLE);
  146.         l &= PCI_ROM_ADDRESS_MASK;
  147.         mask = (u32)PCI_ROM_ADDRESS_MASK;
  148.     }
  149.  
  150.     if (type == pci_bar_mem64) {
  151.         u64 l64 = l;
  152.         u64 sz64 = sz;
  153.         u64 mask64 = mask | (u64)~0 << 32;
  154.  
  155.         pci_read_config_dword(dev, pos + 4, &l);
  156.         pci_write_config_dword(dev, pos + 4, ~0);
  157.         pci_read_config_dword(dev, pos + 4, &sz);
  158.         pci_write_config_dword(dev, pos + 4, l);
  159.  
  160.         l64 |= ((u64)l << 32);
  161.         sz64 |= ((u64)sz << 32);
  162.  
  163.         sz64 = pci_size(l64, sz64, mask64);
  164.  
  165.         if (!sz64)
  166.             goto fail;
  167.  
  168.         if ((sizeof(resource_size_t) < 8) && (sz64 > 0x100000000ULL)) {
  169.             dbgprintf("%s reg %x: can't handle 64-bit BAR\n",
  170.                 __FUNCTION__, pos);
  171.             goto fail;
  172.         }
  173.  
  174.         res->flags |= IORESOURCE_MEM_64;
  175.         if ((sizeof(resource_size_t) < 8) && l) {
  176.             /* Address above 32-bit boundary; disable the BAR */
  177.             pci_write_config_dword(dev, pos, 0);
  178.             pci_write_config_dword(dev, pos + 4, 0);
  179.             res->start = 0;
  180.             res->end = sz64;
  181.         } else {
  182.             res->start = l64;
  183.             res->end = l64 + sz64;
  184.         dbgprintf("%s reg %x: %pR\n", __FUNCTION__, pos, res);
  185.         }
  186.     } else {
  187.         sz = pci_size(l, sz, mask);
  188.  
  189.         if (!sz)
  190.             goto fail;
  191.  
  192.         res->start = l;
  193.         res->end = l + sz;
  194.  
  195.         dbgprintf("%s reg %x: %pR\n", __FUNCTION__, pos, res);
  196.     }
  197.  
  198.  out:
  199.     return (type == pci_bar_mem64) ? 1 : 0;
  200.  fail:
  201.     res->flags = 0;
  202.     goto out;
  203. }
  204.  
  205. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  206. {
  207.     unsigned int pos, reg;
  208.  
  209.     for (pos = 0; pos < howmany; pos++) {
  210.         struct resource *res = &dev->resource[pos];
  211.         reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  212.         pos += __pci_read_base(dev, pci_bar_unknown, res, reg);
  213.     }
  214.  
  215.     if (rom) {
  216.         struct resource *res = &dev->resource[PCI_ROM_RESOURCE];
  217.         dev->rom_base_reg = rom;
  218.         res->flags = IORESOURCE_MEM | IORESOURCE_PREFETCH |
  219.                 IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
  220.                 IORESOURCE_SIZEALIGN;
  221.         __pci_read_base(dev, pci_bar_mem32, res, rom);
  222.     }
  223. }
  224.  
  225. #if 0
  226.  
  227. void pci_read_bridge_bases(struct pci_bus *child)
  228. {
  229.     struct pci_dev *dev = child->self;
  230.     struct resource *res;
  231.     int i;
  232.  
  233.     if (pci_is_root_bus(child)) /* It's a host bus, nothing to read */
  234.         return;
  235.  
  236.     dbgprintf("PCI bridge to [bus %02x-%02x]%s\n",
  237.          child->secondary, child->subordinate,
  238.          dev->transparent ? " (subtractive decode)" : "");
  239.  
  240.     pci_bus_remove_resources(child);
  241.     for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  242.         child->resource[i] = &dev->resource[PCI_BRIDGE_RESOURCES+i];
  243.  
  244.     pci_read_bridge_io(child);
  245.     pci_read_bridge_mmio(child);
  246.     pci_read_bridge_mmio_pref(child);
  247.  
  248.     if (dev->transparent) {
  249.         pci_bus_for_each_resource(child->parent, res, i) {
  250.             if (res) {
  251.                 pci_bus_add_resource(child, res,
  252.                              PCI_SUBTRACTIVE_DECODE);
  253.                 dbgprintf("  bridge window %pR (subtractive decode)\n", res);
  254.             }
  255.         }
  256.     }
  257. }
  258.  
  259. #endif
  260.  
  261. static struct pci_bus * pci_alloc_bus(void)
  262. {
  263.     struct pci_bus *b;
  264.  
  265.     b = kzalloc(sizeof(*b), GFP_KERNEL);
  266.     if (b) {
  267.         INIT_LIST_HEAD(&b->node);
  268.         INIT_LIST_HEAD(&b->children);
  269.         INIT_LIST_HEAD(&b->devices);
  270.         INIT_LIST_HEAD(&b->slots);
  271.         INIT_LIST_HEAD(&b->resources);
  272. //        b->max_bus_speed = PCI_SPEED_UNKNOWN;
  273. //        b->cur_bus_speed = PCI_SPEED_UNKNOWN;
  274.     }
  275.     return b;
  276. }
  277.  
  278.  
  279. #if 0
  280.  
  281. static unsigned char pcix_bus_speed[] = {
  282.     PCI_SPEED_UNKNOWN,      /* 0 */
  283.     PCI_SPEED_66MHz_PCIX,       /* 1 */
  284.     PCI_SPEED_100MHz_PCIX,      /* 2 */
  285.     PCI_SPEED_133MHz_PCIX,      /* 3 */
  286.     PCI_SPEED_UNKNOWN,      /* 4 */
  287.     PCI_SPEED_66MHz_PCIX_ECC,   /* 5 */
  288.     PCI_SPEED_100MHz_PCIX_ECC,  /* 6 */
  289.     PCI_SPEED_133MHz_PCIX_ECC,  /* 7 */
  290.     PCI_SPEED_UNKNOWN,      /* 8 */
  291.     PCI_SPEED_66MHz_PCIX_266,   /* 9 */
  292.     PCI_SPEED_100MHz_PCIX_266,  /* A */
  293.     PCI_SPEED_133MHz_PCIX_266,  /* B */
  294.     PCI_SPEED_UNKNOWN,      /* C */
  295.     PCI_SPEED_66MHz_PCIX_533,   /* D */
  296.     PCI_SPEED_100MHz_PCIX_533,  /* E */
  297.     PCI_SPEED_133MHz_PCIX_533   /* F */
  298. };
  299.  
  300. static unsigned char pcie_link_speed[] = {
  301.     PCI_SPEED_UNKNOWN,      /* 0 */
  302.     PCIE_SPEED_2_5GT,       /* 1 */
  303.     PCIE_SPEED_5_0GT,       /* 2 */
  304.     PCIE_SPEED_8_0GT,       /* 3 */
  305.     PCI_SPEED_UNKNOWN,      /* 4 */
  306.     PCI_SPEED_UNKNOWN,      /* 5 */
  307.     PCI_SPEED_UNKNOWN,      /* 6 */
  308.     PCI_SPEED_UNKNOWN,      /* 7 */
  309.     PCI_SPEED_UNKNOWN,      /* 8 */
  310.     PCI_SPEED_UNKNOWN,      /* 9 */
  311.     PCI_SPEED_UNKNOWN,      /* A */
  312.     PCI_SPEED_UNKNOWN,      /* B */
  313.     PCI_SPEED_UNKNOWN,      /* C */
  314.     PCI_SPEED_UNKNOWN,      /* D */
  315.     PCI_SPEED_UNKNOWN,      /* E */
  316.     PCI_SPEED_UNKNOWN       /* F */
  317. };
  318.  
  319.  
  320. static void pci_set_bus_speed(struct pci_bus *bus)
  321. {
  322.     struct pci_dev *bridge = bus->self;
  323.     int pos;
  324.  
  325.     pos = pci_find_capability(bridge, PCI_CAP_ID_AGP);
  326.     if (!pos)
  327.         pos = pci_find_capability(bridge, PCI_CAP_ID_AGP3);
  328.     if (pos) {
  329.         u32 agpstat, agpcmd;
  330.  
  331.         pci_read_config_dword(bridge, pos + PCI_AGP_STATUS, &agpstat);
  332.         bus->max_bus_speed = agp_speed(agpstat & 8, agpstat & 7);
  333.  
  334.         pci_read_config_dword(bridge, pos + PCI_AGP_COMMAND, &agpcmd);
  335.         bus->cur_bus_speed = agp_speed(agpstat & 8, agpcmd & 7);
  336.     }
  337.  
  338.     pos = pci_find_capability(bridge, PCI_CAP_ID_PCIX);
  339.     if (pos) {
  340.         u16 status;
  341.         enum pci_bus_speed max;
  342.         pci_read_config_word(bridge, pos + 2, &status);
  343.  
  344.         if (status & 0x8000) {
  345.             max = PCI_SPEED_133MHz_PCIX_533;
  346.         } else if (status & 0x4000) {
  347.             max = PCI_SPEED_133MHz_PCIX_266;
  348.         } else if (status & 0x0002) {
  349.             if (((status >> 12) & 0x3) == 2) {
  350.                 max = PCI_SPEED_133MHz_PCIX_ECC;
  351.             } else {
  352.                 max = PCI_SPEED_133MHz_PCIX;
  353.             }
  354.         } else {
  355.             max = PCI_SPEED_66MHz_PCIX;
  356.         }
  357.  
  358.         bus->max_bus_speed = max;
  359.         bus->cur_bus_speed = pcix_bus_speed[(status >> 6) & 0xf];
  360.  
  361.         return;
  362.     }
  363.  
  364.     pos = pci_find_capability(bridge, PCI_CAP_ID_EXP);
  365.     if (pos) {
  366.         u32 linkcap;
  367.         u16 linksta;
  368.  
  369.         pci_read_config_dword(bridge, pos + PCI_EXP_LNKCAP, &linkcap);
  370.         bus->max_bus_speed = pcie_link_speed[linkcap & 0xf];
  371.  
  372.         pci_read_config_word(bridge, pos + PCI_EXP_LNKSTA, &linksta);
  373.         pcie_update_link_speed(bus, linksta);
  374.     }
  375. }
  376.  
  377. #endif
  378.  
  379.  
  380. static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
  381.                        struct pci_dev *bridge, int busnr)
  382. {
  383.     struct pci_bus *child;
  384.     int i;
  385.  
  386.     /*
  387.      * Allocate a new bus, and inherit stuff from the parent..
  388.      */
  389.     child = pci_alloc_bus();
  390.     if (!child)
  391.         return NULL;
  392.  
  393.     child->parent = parent;
  394.     child->ops = parent->ops;
  395.     child->sysdata = parent->sysdata;
  396.     child->bus_flags = parent->bus_flags;
  397.  
  398.     /* initialize some portions of the bus device, but don't register it
  399.      * now as the parent is not properly set up yet.  This device will get
  400.      * registered later in pci_bus_add_devices()
  401.      */
  402. //    child->dev.class = &pcibus_class;
  403. //    dev_set_name(&child->dev, "%04x:%02x", pci_domain_nr(child), busnr);
  404.  
  405.     /*
  406.      * Set up the primary, secondary and subordinate
  407.      * bus numbers.
  408.      */
  409.     child->number = child->secondary = busnr;
  410.     child->primary = parent->secondary;
  411.     child->subordinate = 0xff;
  412.  
  413.     if (!bridge)
  414.         return child;
  415.  
  416.     child->self = bridge;
  417. //    child->bridge = get_device(&bridge->dev);
  418.  
  419. //    pci_set_bus_speed(child);
  420.  
  421.     /* Set up default resource pointers and names.. */
  422.     for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  423.         child->resource[i] = &bridge->resource[PCI_BRIDGE_RESOURCES+i];
  424.         child->resource[i]->name = child->name;
  425.     }
  426.     bridge->subordinate = child;
  427.  
  428.     return child;
  429. }
  430.  
  431. struct pci_bus* pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
  432. {
  433.     struct pci_bus *child;
  434.  
  435.     child = pci_alloc_child_bus(parent, dev, busnr);
  436.     if (child) {
  437. //        down_write(&pci_bus_sem);
  438.         list_add_tail(&child->node, &parent->children);
  439. //        up_write(&pci_bus_sem);
  440.     }
  441.     return child;
  442. }
  443.  
  444.  
  445. static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
  446. {
  447.     struct pci_bus *parent = child->parent;
  448.  
  449.     /* Attempts to fix that up are really dangerous unless
  450.        we're going to re-assign all bus numbers. */
  451.     if (!pcibios_assign_all_busses())
  452.         return;
  453.  
  454.     while (parent->parent && parent->subordinate < max) {
  455.         parent->subordinate = max;
  456.         pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
  457.         parent = parent->parent;
  458.     }
  459. }
  460.  
  461.  
  462. /*
  463.  * If it's a bridge, configure it and scan the bus behind it.
  464.  * For CardBus bridges, we don't scan behind as the devices will
  465.  * be handled by the bridge driver itself.
  466.  *
  467.  * We need to process bridges in two passes -- first we scan those
  468.  * already configured by the BIOS and after we are done with all of
  469.  * them, we proceed to assigning numbers to the remaining buses in
  470.  * order to avoid overlaps between old and new bus numbers.
  471.  */
  472. int pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
  473. {
  474.     struct pci_bus *child;
  475.     int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
  476.     u32 buses, i, j = 0;
  477.     u16 bctl;
  478.     u8 primary, secondary, subordinate;
  479.     int broken = 0;
  480.  
  481.     pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
  482.     primary = buses & 0xFF;
  483.     secondary = (buses >> 8) & 0xFF;
  484.     subordinate = (buses >> 16) & 0xFF;
  485.  
  486.     dbgprintf("scanning [bus %02x-%02x] behind bridge, pass %d\n",
  487.         secondary, subordinate, pass);
  488.  
  489.     /* Check if setup is sensible at all */
  490.     if (!pass &&
  491.         (primary != bus->number || secondary <= bus->number)) {
  492.         dbgprintf("bus configuration invalid, reconfiguring\n");
  493.         broken = 1;
  494.     }
  495.  
  496.     /* Disable MasterAbortMode during probing to avoid reporting
  497.        of bus errors (in some architectures) */
  498.     pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &bctl);
  499.     pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
  500.                   bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
  501.  
  502.     if ((secondary || subordinate) && !pcibios_assign_all_busses() &&
  503.         !is_cardbus && !broken) {
  504.         unsigned int cmax;
  505.         /*
  506.          * Bus already configured by firmware, process it in the first
  507.          * pass and just note the configuration.
  508.          */
  509.         if (pass)
  510.             goto out;
  511.  
  512.         /*
  513.          * If we already got to this bus through a different bridge,
  514.          * don't re-add it. This can happen with the i450NX chipset.
  515.          *
  516.          * However, we continue to descend down the hierarchy and
  517.          * scan remaining child buses.
  518.          */
  519.         child = pci_find_bus(pci_domain_nr(bus), secondary);
  520.         if (!child) {
  521.             child = pci_add_new_bus(bus, dev, secondary);
  522.             if (!child)
  523.                 goto out;
  524.             child->primary = primary;
  525.             child->subordinate = subordinate;
  526.             child->bridge_ctl = bctl;
  527.         }
  528.  
  529.         cmax = pci_scan_child_bus(child);
  530.         if (cmax > max)
  531.             max = cmax;
  532.         if (child->subordinate > max)
  533.             max = child->subordinate;
  534.     } else {
  535.         /*
  536.          * We need to assign a number to this bus which we always
  537.          * do in the second pass.
  538.          */
  539.         if (!pass) {
  540.             if (pcibios_assign_all_busses() || broken)
  541.                 /* Temporarily disable forwarding of the
  542.                    configuration cycles on all bridges in
  543.                    this bus segment to avoid possible
  544.                    conflicts in the second pass between two
  545.                    bridges programmed with overlapping
  546.                    bus ranges. */
  547.                 pci_write_config_dword(dev, PCI_PRIMARY_BUS,
  548.                                buses & ~0xffffff);
  549.             goto out;
  550.         }
  551.  
  552.         /* Clear errors */
  553.         pci_write_config_word(dev, PCI_STATUS, 0xffff);
  554.  
  555.         /* Prevent assigning a bus number that already exists.
  556.          * This can happen when a bridge is hot-plugged */
  557.         if (pci_find_bus(pci_domain_nr(bus), max+1))
  558.             goto out;
  559.         child = pci_add_new_bus(bus, dev, ++max);
  560.         buses = (buses & 0xff000000)
  561.               | ((unsigned int)(child->primary)     <<  0)
  562.               | ((unsigned int)(child->secondary)   <<  8)
  563.               | ((unsigned int)(child->subordinate) << 16);
  564.  
  565.         /*
  566.          * yenta.c forces a secondary latency timer of 176.
  567.          * Copy that behaviour here.
  568.          */
  569.         if (is_cardbus) {
  570.             buses &= ~0xff000000;
  571.             buses |= CARDBUS_LATENCY_TIMER << 24;
  572.         }
  573.  
  574.         /*
  575.          * We need to blast all three values with a single write.
  576.          */
  577.         pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
  578.  
  579.         if (!is_cardbus) {
  580.             child->bridge_ctl = bctl;
  581.             /*
  582.              * Adjust subordinate busnr in parent buses.
  583.              * We do this before scanning for children because
  584.              * some devices may not be detected if the bios
  585.              * was lazy.
  586.              */
  587.             pci_fixup_parent_subordinate_busnr(child, max);
  588.             /* Now we can scan all subordinate buses... */
  589.             max = pci_scan_child_bus(child);
  590.             /*
  591.              * now fix it up again since we have found
  592.              * the real value of max.
  593.              */
  594.             pci_fixup_parent_subordinate_busnr(child, max);
  595.         } else {
  596.             /*
  597.              * For CardBus bridges, we leave 4 bus numbers
  598.              * as cards with a PCI-to-PCI bridge can be
  599.              * inserted later.
  600.              */
  601.             for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) {
  602.                 struct pci_bus *parent = bus;
  603.                 if (pci_find_bus(pci_domain_nr(bus),
  604.                             max+i+1))
  605.                     break;
  606.                 while (parent->parent) {
  607.                     if ((!pcibios_assign_all_busses()) &&
  608.                         (parent->subordinate > max) &&
  609.                         (parent->subordinate <= max+i)) {
  610.                         j = 1;
  611.                     }
  612.                     parent = parent->parent;
  613.                 }
  614.                 if (j) {
  615.                     /*
  616.                      * Often, there are two cardbus bridges
  617.                      * -- try to leave one valid bus number
  618.                      * for each one.
  619.                      */
  620.                     i /= 2;
  621.                     break;
  622.                 }
  623.             }
  624.             max += i;
  625.             pci_fixup_parent_subordinate_busnr(child, max);
  626.         }
  627.         /*
  628.          * Set the subordinate bus number to its real value.
  629.          */
  630.         child->subordinate = max;
  631.         pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
  632.     }
  633.  
  634.     vsprintf(child->name,
  635.         (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
  636.         pci_domain_nr(bus), child->number);
  637.  
  638.     /* Has only triggered on CardBus, fixup is in yenta_socket */
  639.     while (bus->parent) {
  640.         if ((child->subordinate > bus->subordinate) ||
  641.             (child->number > bus->subordinate) ||
  642.             (child->number < bus->number) ||
  643.             (child->subordinate < bus->number)) {
  644.             dbgprintf("[bus %02x-%02x] %s "
  645.                 "hidden behind%s bridge %s [bus %02x-%02x]\n",
  646.                 child->number, child->subordinate,
  647.                 (bus->number > child->subordinate &&
  648.                  bus->subordinate < child->number) ?
  649.                     "wholly" : "partially",
  650.                 bus->self->transparent ? " transparent" : "",
  651.                 "FIX BRIDGE NAME",
  652.                 bus->number, bus->subordinate);
  653.         }
  654.         bus = bus->parent;
  655.     }
  656.  
  657. out:
  658.     pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
  659.  
  660.     return max;
  661. }
  662.  
  663. void set_pcie_port_type(struct pci_dev *pdev)
  664. {
  665.     int pos;
  666.     u16 reg16;
  667.  
  668.     pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  669.     if (!pos)
  670.         return;
  671.     pdev->is_pcie = 1;
  672.     pdev->pcie_cap = pos;
  673.     pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
  674.     pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
  675. }
  676.  
  677. void set_pcie_hotplug_bridge(struct pci_dev *pdev)
  678. {
  679.     int pos;
  680.     u16 reg16;
  681.     u32 reg32;
  682.  
  683.     pos = pci_pcie_cap(pdev);
  684.     if (!pos)
  685.         return;
  686.     pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
  687.     if (!(reg16 & PCI_EXP_FLAGS_SLOT))
  688.         return;
  689.     pci_read_config_dword(pdev, pos + PCI_EXP_SLTCAP, &reg32);
  690.     if (reg32 & PCI_EXP_SLTCAP_HPC)
  691.         pdev->is_hotplug_bridge = 1;
  692. }
  693.  
  694. /*
  695.  * Read interrupt line and base address registers.
  696.  * The architecture-dependent code can tweak these, of course.
  697.  */
  698. static void pci_read_irq(struct pci_dev *dev)
  699. {
  700.     unsigned char irq;
  701.  
  702.     pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  703.     dev->pin = irq;
  704.     if (irq)
  705.         pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
  706.     dev->irq = irq;
  707. }
  708.  
  709.  
  710. /**
  711.  * pci_setup_device - fill in class and map information of a device
  712.  * @dev: the device structure to fill
  713.  *
  714.  * Initialize the device structure with information about the device's
  715.  * vendor,class,memory and IO-space addresses,IRQ lines etc.
  716.  * Called at initialisation of the PCI subsystem and by CardBus services.
  717.  * Returns 0 on success and negative if unknown type of device (not normal,
  718.  * bridge or CardBus).
  719.  */
  720. int pci_setup_device(struct pci_dev *dev)
  721. {
  722.     u32 class;
  723.     u8 hdr_type;
  724.     struct pci_slot *slot;
  725.     int pos = 0;
  726.  
  727.     if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
  728.         return -EIO;
  729.  
  730.     dev->sysdata = dev->bus->sysdata;
  731. //    dev->dev.parent = dev->bus->bridge;
  732. //    dev->dev.bus = &pci_bus_type;
  733.     dev->hdr_type = hdr_type & 0x7f;
  734.     dev->multifunction = !!(hdr_type & 0x80);
  735.     dev->error_state = pci_channel_io_normal;
  736.     set_pcie_port_type(dev);
  737.  
  738.     list_for_each_entry(slot, &dev->bus->slots, list)
  739.         if (PCI_SLOT(dev->devfn) == slot->number)
  740.             dev->slot = slot;
  741.  
  742.     /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  743.        set this higher, assuming the system even supports it.  */
  744.     dev->dma_mask = 0xffffffff;
  745.  
  746. //    dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  747. //             dev->bus->number, PCI_SLOT(dev->devfn),
  748. //             PCI_FUNC(dev->devfn));
  749.  
  750.     pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  751.     dev->revision = class & 0xff;
  752.     class >>= 8;                    /* upper 3 bytes */
  753.     dev->class = class;
  754.     class >>= 8;
  755.  
  756.     dbgprintf("found [%04x:%04x] class %06x header type %02x\n",
  757.          dev->vendor, dev->device, class, dev->hdr_type);
  758.  
  759.     /* need to have dev->class ready */
  760.     dev->cfg_size = pci_cfg_space_size(dev);
  761.  
  762.     /* "Unknown power state" */
  763.     dev->current_state = PCI_UNKNOWN;
  764.  
  765.     /* Early fixups, before probing the BARs */
  766. //    pci_fixup_device(pci_fixup_early, dev);
  767.     /* device class may be changed after fixup */
  768.     class = dev->class >> 8;
  769.  
  770.     switch (dev->hdr_type) {            /* header type */
  771.     case PCI_HEADER_TYPE_NORMAL:            /* standard header */
  772.         if (class == PCI_CLASS_BRIDGE_PCI)
  773.             goto bad;
  774.         pci_read_irq(dev);
  775.         pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  776.         pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  777.         pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  778.  
  779.         /*
  780.          *  Do the ugly legacy mode stuff here rather than broken chip
  781.          *  quirk code. Legacy mode ATA controllers have fixed
  782.          *  addresses. These are not always echoed in BAR0-3, and
  783.          *  BAR0-3 in a few cases contain junk!
  784.          */
  785.         if (class == PCI_CLASS_STORAGE_IDE) {
  786.             u8 progif;
  787.             pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  788.             if ((progif & 1) == 0) {
  789.                 dev->resource[0].start = 0x1F0;
  790.                 dev->resource[0].end = 0x1F7;
  791.                 dev->resource[0].flags = LEGACY_IO_RESOURCE;
  792.                 dev->resource[1].start = 0x3F6;
  793.                 dev->resource[1].end = 0x3F6;
  794.                 dev->resource[1].flags = LEGACY_IO_RESOURCE;
  795.             }
  796.             if ((progif & 4) == 0) {
  797.                 dev->resource[2].start = 0x170;
  798.                 dev->resource[2].end = 0x177;
  799.                 dev->resource[2].flags = LEGACY_IO_RESOURCE;
  800.                 dev->resource[3].start = 0x376;
  801.                 dev->resource[3].end = 0x376;
  802.                 dev->resource[3].flags = LEGACY_IO_RESOURCE;
  803.             }
  804.         }
  805.         break;
  806.  
  807.     case PCI_HEADER_TYPE_BRIDGE:            /* bridge header */
  808.         if (class != PCI_CLASS_BRIDGE_PCI)
  809.             goto bad;
  810.         /* The PCI-to-PCI bridge spec requires that subtractive
  811.            decoding (i.e. transparent) bridge must have programming
  812.            interface code of 0x01. */
  813.         pci_read_irq(dev);
  814.         dev->transparent = ((dev->class & 0xff) == 1);
  815.         pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  816.         set_pcie_hotplug_bridge(dev);
  817.         pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
  818.         if (pos) {
  819.             pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
  820.             pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
  821.         }
  822.         break;
  823.  
  824.     case PCI_HEADER_TYPE_CARDBUS:           /* CardBus bridge header */
  825.         if (class != PCI_CLASS_BRIDGE_CARDBUS)
  826.             goto bad;
  827.         pci_read_irq(dev);
  828.         pci_read_bases(dev, 1, 0);
  829.         pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  830.         pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  831.         break;
  832.  
  833.     default:                    /* unknown header */
  834.         dbgprintf("unknown header type %02x, "
  835.             "ignoring device\n", dev->hdr_type);
  836.         return -EIO;
  837.  
  838.     bad:
  839.         dbgprintf("ignoring class %02x (doesn't match header "
  840.             "type %02x)\n", class, dev->hdr_type);
  841.         dev->class = PCI_CLASS_NOT_DEFINED;
  842.     }
  843.  
  844.     /* We found a fine healthy device, go go go... */
  845.     return 0;
  846. }
  847.  
  848.  
  849.  
  850. struct pci_dev *alloc_pci_dev(void)
  851. {
  852.     struct pci_dev *dev;
  853.  
  854.     dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  855.     if (!dev)
  856.         return NULL;
  857.  
  858.     INIT_LIST_HEAD(&dev->bus_list);
  859.  
  860.     return dev;
  861. }
  862.  
  863. /*
  864.  * Read the config data for a PCI device, sanity-check it
  865.  * and fill in the dev structure...
  866.  */
  867. static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
  868. {
  869.     struct pci_dev *dev;
  870.     u32 l;
  871.     int timeout = 10;
  872.  
  873.     if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  874.         return NULL;
  875.  
  876.     /* some broken boards return 0 or ~0 if a slot is empty: */
  877.     if (l == 0xffffffff || l == 0x00000000 ||
  878.         l == 0x0000ffff || l == 0xffff0000 ||
  879.         (l & 0xffff0000) == 0xffff0000     ||
  880.         (l & 0x0000ffff) == 0x0000ffff )
  881.         return NULL;
  882.  
  883.     /* Configuration request Retry Status */
  884.     while (l == 0xffff0001) {
  885.         delay(timeout/10);
  886.         timeout *= 2;
  887.         if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  888.             return NULL;
  889.         /* Card hasn't responded in 60 seconds?  Must be stuck. */
  890.         if (timeout > 60 * 1000) {
  891.             printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
  892.                     "responding\n", pci_domain_nr(bus),
  893.                     bus->number, PCI_SLOT(devfn),
  894.                     PCI_FUNC(devfn));
  895.             return NULL;
  896.         }
  897.     }
  898.  
  899.     dev = alloc_pci_dev();
  900.     if (!dev)
  901.         return NULL;
  902.  
  903.     dev->bus = bus;
  904.     dev->devfn = devfn;
  905.     dev->vendor = l & 0xffff;
  906.     dev->device = (l >> 16) & 0xffff;
  907.  
  908.     if (pci_setup_device(dev)) {
  909.         kfree(dev);
  910.         return NULL;
  911.     }
  912.  
  913.     return dev;
  914. }
  915.  
  916. void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
  917. {
  918. //    device_initialize(&dev->dev);
  919. //    dev->dev.release = pci_release_dev;
  920. //    pci_dev_get(dev);
  921.  
  922. //    dev->dev.dma_mask = &dev->dma_mask;
  923. //    dev->dev.dma_parms = &dev->dma_parms;
  924. //    dev->dev.coherent_dma_mask = 0xffffffffull;
  925.  
  926. //    pci_set_dma_max_seg_size(dev, 65536);
  927. //    pci_set_dma_seg_boundary(dev, 0xffffffff);
  928.  
  929.     /* Fix up broken headers */
  930. //    pci_fixup_device(pci_fixup_header, dev);
  931.  
  932.     /* Clear the state_saved flag. */
  933.     dev->state_saved = false;
  934.  
  935.     /* Initialize various capabilities */
  936. //    pci_init_capabilities(dev);
  937.  
  938.     /*
  939.      * Add the device to our list of discovered devices
  940.      * and the bus list for fixup functions, etc.
  941.      */
  942. //    down_write(&pci_bus_sem);
  943.     list_add_tail(&dev->bus_list, &bus->devices);
  944. //    up_write(&pci_bus_sem);
  945. }
  946.  
  947. struct pci_dev * pci_scan_single_device(struct pci_bus *bus, int devfn)
  948. {
  949.     struct pci_dev *dev;
  950.  
  951.     dev = pci_get_slot(bus, devfn);
  952.     if (dev) {
  953. //        pci_dev_put(dev);
  954.         return dev;
  955.     }
  956.  
  957.     dev = pci_scan_device(bus, devfn);
  958.     if (!dev)
  959.         return NULL;
  960.  
  961.     pci_device_add(dev, bus);
  962.  
  963.     return dev;
  964. }
  965.  
  966. static unsigned next_ari_fn(struct pci_dev *dev, unsigned fn)
  967. {
  968.     u16 cap;
  969.     unsigned pos, next_fn;
  970.  
  971.     if (!dev)
  972.         return 0;
  973.  
  974.     pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  975.     if (!pos)
  976.         return 0;
  977.     pci_read_config_word(dev, pos + 4, &cap);
  978.     next_fn = cap >> 8;
  979.     if (next_fn <= fn)
  980.         return 0;
  981.     return next_fn;
  982. }
  983.  
  984. static unsigned next_trad_fn(struct pci_dev *dev, unsigned fn)
  985. {
  986.     return (fn + 1) % 8;
  987. }
  988.  
  989. static unsigned no_next_fn(struct pci_dev *dev, unsigned fn)
  990. {
  991.     return 0;
  992. }
  993.  
  994. static int only_one_child(struct pci_bus *bus)
  995. {
  996.     struct pci_dev *parent = bus->self;
  997.     if (!parent || !pci_is_pcie(parent))
  998.         return 0;
  999.     if (parent->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  1000.         parent->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  1001.         return 1;
  1002.     return 0;
  1003. }
  1004.  
  1005. /**
  1006.  * pci_scan_slot - scan a PCI slot on a bus for devices.
  1007.  * @bus: PCI bus to scan
  1008.  * @devfn: slot number to scan (must have zero function.)
  1009.  *
  1010.  * Scan a PCI slot on the specified PCI bus for devices, adding
  1011.  * discovered devices to the @bus->devices list.  New devices
  1012.  * will not have is_added set.
  1013.  *
  1014.  * Returns the number of new devices found.
  1015.  */
  1016. int pci_scan_slot(struct pci_bus *bus, int devfn)
  1017. {
  1018.     unsigned fn, nr = 0;
  1019.     struct pci_dev *dev;
  1020.     unsigned (*next_fn)(struct pci_dev *, unsigned) = no_next_fn;
  1021.  
  1022.     if (only_one_child(bus) && (devfn > 0))
  1023.         return 0; /* Already scanned the entire slot */
  1024.  
  1025.     dev = pci_scan_single_device(bus, devfn);
  1026.     if (!dev)
  1027.         return 0;
  1028.     if (!dev->is_added)
  1029.         nr++;
  1030.  
  1031.     if (pci_ari_enabled(bus))
  1032.         next_fn = next_ari_fn;
  1033.     else if (dev->multifunction)
  1034.         next_fn = next_trad_fn;
  1035.  
  1036.     for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) {
  1037.         dev = pci_scan_single_device(bus, devfn + fn);
  1038.         if (dev) {
  1039.             if (!dev->is_added)
  1040.                 nr++;
  1041.             dev->multifunction = 1;
  1042.         }
  1043.     }
  1044.  
  1045.     /* only one slot has pcie device */
  1046. //    if (bus->self && nr)
  1047. //        pcie_aspm_init_link_state(bus->self);
  1048.  
  1049.     return nr;
  1050. }
  1051.  
  1052.  
  1053. unsigned int pci_scan_child_bus(struct pci_bus *bus)
  1054. {
  1055.     unsigned int devfn, pass, max = bus->secondary;
  1056.     struct pci_dev *dev;
  1057.  
  1058.     dbgprintf("scanning bus\n");
  1059.  
  1060.     /* Go find them, Rover! */
  1061.     for (devfn = 0; devfn < 0x100; devfn += 8)
  1062.         pci_scan_slot(bus, devfn);
  1063.  
  1064.     /* Reserve buses for SR-IOV capability. */
  1065.     max += pci_iov_bus_range(bus);
  1066.  
  1067.     /*
  1068.      * After performing arch-dependent fixup of the bus, look behind
  1069.      * all PCI-to-PCI bridges on this bus.
  1070.      */
  1071.     if (!bus->is_added) {
  1072.         dbgprintf("fixups for bus\n");
  1073. //        pcibios_fixup_bus(bus);
  1074.         if (pci_is_root_bus(bus))
  1075.             bus->is_added = 1;
  1076.     }
  1077.  
  1078.     for (pass=0; pass < 2; pass++)
  1079.         list_for_each_entry(dev, &bus->devices, bus_list) {
  1080.             if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  1081.                 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  1082.                 max = pci_scan_bridge(bus, dev, max, pass);
  1083.         }
  1084.  
  1085.     /*
  1086.      * We've scanned the bus and so we know all about what's on
  1087.      * the other side of any bridges that may be on this bus plus
  1088.      * any devices.
  1089.      *
  1090.      * Return how far we've got finding sub-buses.
  1091.      */
  1092.     dbgprintf("bus scan returning with max=%02x\n", max);
  1093.     return max;
  1094. }
  1095.  
  1096. /**
  1097.  * pci_cfg_space_size - get the configuration space size of the PCI device.
  1098.  * @dev: PCI device
  1099.  *
  1100.  * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  1101.  * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
  1102.  * access it.  Maybe we don't have a way to generate extended config space
  1103.  * accesses, or the device is behind a reverse Express bridge.  So we try
  1104.  * reading the dword at 0x100 which must either be 0 or a valid extended
  1105.  * capability header.
  1106.  */
  1107. int pci_cfg_space_size_ext(struct pci_dev *dev)
  1108. {
  1109.     u32 status;
  1110.     int pos = PCI_CFG_SPACE_SIZE;
  1111.  
  1112.     if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
  1113.         goto fail;
  1114.     if (status == 0xffffffff)
  1115.         goto fail;
  1116.  
  1117.     return PCI_CFG_SPACE_EXP_SIZE;
  1118.  
  1119.  fail:
  1120.     return PCI_CFG_SPACE_SIZE;
  1121. }
  1122.  
  1123. int pci_cfg_space_size(struct pci_dev *dev)
  1124. {
  1125.     int pos;
  1126.     u32 status;
  1127.     u16 class;
  1128.  
  1129.     class = dev->class >> 8;
  1130.     if (class == PCI_CLASS_BRIDGE_HOST)
  1131.         return pci_cfg_space_size_ext(dev);
  1132.  
  1133.     pos = pci_pcie_cap(dev);
  1134.     if (!pos) {
  1135.         pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  1136.         if (!pos)
  1137.             goto fail;
  1138.  
  1139.         pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  1140.         if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
  1141.             goto fail;
  1142.     }
  1143.  
  1144.     return pci_cfg_space_size_ext(dev);
  1145.  
  1146.  fail:
  1147.     return PCI_CFG_SPACE_SIZE;
  1148. }
  1149.  
  1150.  
  1151.  
  1152. struct pci_bus * pci_create_bus(int bus, struct pci_ops *ops, void *sysdata)
  1153. {
  1154.     int error;
  1155.     struct pci_bus *b, *b2;
  1156.  
  1157.     b = pci_alloc_bus();
  1158.     if (!b)
  1159.         return NULL;
  1160.  
  1161.     b->sysdata = sysdata;
  1162.     b->ops = ops;
  1163.  
  1164.     b2 = pci_find_bus(pci_domain_nr(b), bus);
  1165.     if (b2) {
  1166.         /* If we already got to this bus through a different bridge, ignore it */
  1167.         dbgprintf("bus already known\n");
  1168.         goto err_out;
  1169.     }
  1170.  
  1171. //    down_write(&pci_bus_sem);
  1172.     list_add_tail(&b->node, &pci_root_buses);
  1173. //    up_write(&pci_bus_sem);
  1174.  
  1175.     b->number = b->secondary = bus;
  1176.     b->resource[0] = &ioport_resource;
  1177.     b->resource[1] = &iomem_resource;
  1178.  
  1179.     return b;
  1180.  
  1181. err_out:
  1182.     kfree(b);
  1183.     return NULL;
  1184. }
  1185.  
  1186.