Subversion Repositories Kolibri OS

Rev

Rev 1631 | 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.  
  9. #define LEGACY_IO_RESOURCE  (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  10.  
  11. /**
  12.  * pci_setup_device - fill in class and map information of a device
  13.  * @dev: the device structure to fill
  14.  *
  15.  * Initialize the device structure with information about the device's
  16.  * vendor,class,memory and IO-space addresses,IRQ lines etc.
  17.  * Called at initialisation of the PCI subsystem and by CardBus services.
  18.  * Returns 0 on success and negative if unknown type of device (not normal,
  19.  * bridge or CardBus).
  20.  */
  21. int pci_setup_device(struct pci_dev *dev)
  22. {
  23.     u32 class;
  24.     u8 hdr_type;
  25.     struct pci_slot *slot;
  26.     int pos = 0;
  27.  
  28.     if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
  29.         return -EIO;
  30.  
  31.     dev->sysdata = dev->bus->sysdata;
  32. //    dev->dev.parent = dev->bus->bridge;
  33. //    dev->dev.bus = &pci_bus_type;
  34.     dev->hdr_type = hdr_type & 0x7f;
  35.     dev->multifunction = !!(hdr_type & 0x80);
  36.     dev->error_state = pci_channel_io_normal;
  37.     set_pcie_port_type(dev);
  38.  
  39.     list_for_each_entry(slot, &dev->bus->slots, list)
  40.         if (PCI_SLOT(dev->devfn) == slot->number)
  41.             dev->slot = slot;
  42.  
  43.     /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
  44.        set this higher, assuming the system even supports it.  */
  45.     dev->dma_mask = 0xffffffff;
  46.  
  47. //    dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(dev->bus),
  48. //             dev->bus->number, PCI_SLOT(dev->devfn),
  49. //             PCI_FUNC(dev->devfn));
  50.  
  51.     pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  52.     dev->revision = class & 0xff;
  53.     class >>= 8;                    /* upper 3 bytes */
  54.     dev->class = class;
  55.     class >>= 8;
  56.  
  57.     dbgprintf("found [%04x:%04x] class %06x header type %02x\n",
  58.          dev->vendor, dev->device, class, dev->hdr_type);
  59.  
  60.     /* need to have dev->class ready */
  61.     dev->cfg_size = pci_cfg_space_size(dev);
  62.  
  63.     /* "Unknown power state" */
  64.     dev->current_state = PCI_UNKNOWN;
  65.  
  66.     /* Early fixups, before probing the BARs */
  67. //    pci_fixup_device(pci_fixup_early, dev);
  68.     /* device class may be changed after fixup */
  69.     class = dev->class >> 8;
  70.  
  71.     switch (dev->hdr_type) {            /* header type */
  72.     case PCI_HEADER_TYPE_NORMAL:            /* standard header */
  73.         if (class == PCI_CLASS_BRIDGE_PCI)
  74.             goto bad;
  75.         pci_read_irq(dev);
  76.         pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  77.         pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  78.         pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
  79.  
  80.         /*
  81.          *  Do the ugly legacy mode stuff here rather than broken chip
  82.          *  quirk code. Legacy mode ATA controllers have fixed
  83.          *  addresses. These are not always echoed in BAR0-3, and
  84.          *  BAR0-3 in a few cases contain junk!
  85.          */
  86.         if (class == PCI_CLASS_STORAGE_IDE) {
  87.             u8 progif;
  88.             pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
  89.             if ((progif & 1) == 0) {
  90.                 dev->resource[0].start = 0x1F0;
  91.                 dev->resource[0].end = 0x1F7;
  92.                 dev->resource[0].flags = LEGACY_IO_RESOURCE;
  93.                 dev->resource[1].start = 0x3F6;
  94.                 dev->resource[1].end = 0x3F6;
  95.                 dev->resource[1].flags = LEGACY_IO_RESOURCE;
  96.             }
  97.             if ((progif & 4) == 0) {
  98.                 dev->resource[2].start = 0x170;
  99.                 dev->resource[2].end = 0x177;
  100.                 dev->resource[2].flags = LEGACY_IO_RESOURCE;
  101.                 dev->resource[3].start = 0x376;
  102.                 dev->resource[3].end = 0x376;
  103.                 dev->resource[3].flags = LEGACY_IO_RESOURCE;
  104.             }
  105.         }
  106.         break;
  107.  
  108.     case PCI_HEADER_TYPE_BRIDGE:            /* bridge header */
  109.         if (class != PCI_CLASS_BRIDGE_PCI)
  110.             goto bad;
  111.         /* The PCI-to-PCI bridge spec requires that subtractive
  112.            decoding (i.e. transparent) bridge must have programming
  113.            interface code of 0x01. */
  114.         pci_read_irq(dev);
  115.         dev->transparent = ((dev->class & 0xff) == 1);
  116.         pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  117.         set_pcie_hotplug_bridge(dev);
  118.         pos = pci_find_capability(dev, PCI_CAP_ID_SSVID);
  119.         if (pos) {
  120.             pci_read_config_word(dev, pos + PCI_SSVID_VENDOR_ID, &dev->subsystem_vendor);
  121.             pci_read_config_word(dev, pos + PCI_SSVID_DEVICE_ID, &dev->subsystem_device);
  122.         }
  123.         break;
  124.  
  125.     case PCI_HEADER_TYPE_CARDBUS:           /* CardBus bridge header */
  126.         if (class != PCI_CLASS_BRIDGE_CARDBUS)
  127.             goto bad;
  128.         pci_read_irq(dev);
  129.         pci_read_bases(dev, 1, 0);
  130.         pci_read_config_word(dev, PCI_CB_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
  131.         pci_read_config_word(dev, PCI_CB_SUBSYSTEM_ID, &dev->subsystem_device);
  132.         break;
  133.  
  134.     default:                    /* unknown header */
  135.         dbgprintf("unknown header type %02x, "
  136.             "ignoring device\n", dev->hdr_type);
  137.         return -EIO;
  138.  
  139.     bad:
  140.         dbgprintf("ignoring class %02x (doesn't match header "
  141.             "type %02x)\n", class, dev->hdr_type);
  142.         dev->class = PCI_CLASS_NOT_DEFINED;
  143.     }
  144.  
  145.     /* We found a fine healthy device, go go go... */
  146.     return 0;
  147. }
  148.  
  149.  
  150.  
  151. struct pci_dev *alloc_pci_dev(void)
  152. {
  153.     struct pci_dev *dev;
  154.  
  155.     dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  156.     if (!dev)
  157.         return NULL;
  158.  
  159.     INIT_LIST_HEAD(&dev->bus_list);
  160.  
  161.     return dev;
  162. }
  163.  
  164. /*
  165.  * Read the config data for a PCI device, sanity-check it
  166.  * and fill in the dev structure...
  167.  */
  168. static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
  169. {
  170.     struct pci_dev *dev;
  171.     u32 l;
  172.     int timeout = 10;
  173.  
  174.     if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  175.         return NULL;
  176.  
  177.     /* some broken boards return 0 or ~0 if a slot is empty: */
  178.     if (l == 0xffffffff || l == 0x00000000 ||
  179.         l == 0x0000ffff || l == 0xffff0000)
  180.         return NULL;
  181.  
  182.     /* Configuration request Retry Status */
  183.     while (l == 0xffff0001) {
  184.         delay(timeout/10);
  185.         timeout *= 2;
  186.         if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l))
  187.             return NULL;
  188.         /* Card hasn't responded in 60 seconds?  Must be stuck. */
  189.         if (delay > 60 * 1000) {
  190.             printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
  191.                     "responding\n", pci_domain_nr(bus),
  192.                     bus->number, PCI_SLOT(devfn),
  193.                     PCI_FUNC(devfn));
  194.             return NULL;
  195.         }
  196.     }
  197.  
  198.     dev = alloc_pci_dev();
  199.     if (!dev)
  200.         return NULL;
  201.  
  202.     dev->bus = bus;
  203.     dev->devfn = devfn;
  204.     dev->vendor = l & 0xffff;
  205.     dev->device = (l >> 16) & 0xffff;
  206.  
  207.     if (pci_setup_device(dev)) {
  208.         kfree(dev);
  209.         return NULL;
  210.     }
  211.  
  212.     return dev;
  213. }
  214.  
  215.  
  216. struct pci_dev * pci_scan_single_device(struct pci_bus *bus, int devfn)
  217. {
  218.     struct pci_dev *dev;
  219.  
  220.     dev = pci_get_slot(bus, devfn);
  221.     if (dev) {
  222. //        pci_dev_put(dev);
  223.         return dev;
  224.     }
  225.  
  226.     dev = pci_scan_device(bus, devfn);
  227.     if (!dev)
  228.         return NULL;
  229.  
  230.     pci_device_add(dev, bus);
  231.  
  232.     return dev;
  233. }
  234.  
  235. static unsigned next_ari_fn(struct pci_dev *dev, unsigned fn)
  236. {
  237.     u16 cap;
  238.     unsigned pos, next_fn;
  239.  
  240.     if (!dev)
  241.         return 0;
  242.  
  243.     pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
  244.     if (!pos)
  245.         return 0;
  246.     pci_read_config_word(dev, pos + 4, &cap);
  247.     next_fn = cap >> 8;
  248.     if (next_fn <= fn)
  249.         return 0;
  250.     return next_fn;
  251. }
  252.  
  253. static unsigned next_trad_fn(struct pci_dev *dev, unsigned fn)
  254. {
  255.     return (fn + 1) % 8;
  256. }
  257.  
  258. static unsigned no_next_fn(struct pci_dev *dev, unsigned fn)
  259. {
  260.     return 0;
  261. }
  262.  
  263. static int only_one_child(struct pci_bus *bus)
  264. {
  265.     struct pci_dev *parent = bus->self;
  266.     if (!parent || !pci_is_pcie(parent))
  267.         return 0;
  268.     if (parent->pcie_type == PCI_EXP_TYPE_ROOT_PORT ||
  269.         parent->pcie_type == PCI_EXP_TYPE_DOWNSTREAM)
  270.         return 1;
  271.     return 0;
  272. }
  273.  
  274. /**
  275.  * pci_scan_slot - scan a PCI slot on a bus for devices.
  276.  * @bus: PCI bus to scan
  277.  * @devfn: slot number to scan (must have zero function.)
  278.  *
  279.  * Scan a PCI slot on the specified PCI bus for devices, adding
  280.  * discovered devices to the @bus->devices list.  New devices
  281.  * will not have is_added set.
  282.  *
  283.  * Returns the number of new devices found.
  284.  */
  285. int pci_scan_slot(struct pci_bus *bus, int devfn)
  286. {
  287.     unsigned fn, nr = 0;
  288.     struct pci_dev *dev;
  289.     unsigned (*next_fn)(struct pci_dev *, unsigned) = no_next_fn;
  290.  
  291.     if (only_one_child(bus) && (devfn > 0))
  292.         return 0; /* Already scanned the entire slot */
  293.  
  294.     dev = pci_scan_single_device(bus, devfn);
  295.     if (!dev)
  296.         return 0;
  297.     if (!dev->is_added)
  298.         nr++;
  299.  
  300.     if (pci_ari_enabled(bus))
  301.         next_fn = next_ari_fn;
  302.     else if (dev->multifunction)
  303.         next_fn = next_trad_fn;
  304.  
  305.     for (fn = next_fn(dev, 0); fn > 0; fn = next_fn(dev, fn)) {
  306.         dev = pci_scan_single_device(bus, devfn + fn);
  307.         if (dev) {
  308.             if (!dev->is_added)
  309.                 nr++;
  310.             dev->multifunction = 1;
  311.         }
  312.     }
  313.  
  314.     /* only one slot has pcie device */
  315.     if (bus->self && nr)
  316.         pcie_aspm_init_link_state(bus->self);
  317.  
  318.     return nr;
  319. }
  320.  
  321.  
  322. unsigned int pci_scan_child_bus(struct pci_bus *bus)
  323. {
  324.     unsigned int devfn, pass, max = bus->secondary;
  325.     struct pci_dev *dev;
  326.  
  327.     dbgprintf("scanning bus\n");
  328.  
  329.     /* Go find them, Rover! */
  330.     for (devfn = 0; devfn < 0x100; devfn += 8)
  331.         pci_scan_slot(bus, devfn);
  332.  
  333.     /* Reserve buses for SR-IOV capability. */
  334.     max += pci_iov_bus_range(bus);
  335.  
  336.     /*
  337.      * After performing arch-dependent fixup of the bus, look behind
  338.      * all PCI-to-PCI bridges on this bus.
  339.      */
  340.     if (!bus->is_added) {
  341.         dbgprintf("fixups for bus\n");
  342.         pcibios_fixup_bus(bus);
  343.         if (pci_is_root_bus(bus))
  344.             bus->is_added = 1;
  345.     }
  346.  
  347.     for (pass=0; pass < 2; pass++)
  348.         list_for_each_entry(dev, &bus->devices, bus_list) {
  349.             if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  350.                 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  351.                 max = pci_scan_bridge(bus, dev, max, pass);
  352.         }
  353.  
  354.     /*
  355.      * We've scanned the bus and so we know all about what's on
  356.      * the other side of any bridges that may be on this bus plus
  357.      * any devices.
  358.      *
  359.      * Return how far we've got finding sub-buses.
  360.      */
  361.     dbgprintf("bus scan returning with max=%02x\n", max);
  362.     return max;
  363. }
  364.  
  365. /**
  366.  * pci_cfg_space_size - get the configuration space size of the PCI device.
  367.  * @dev: PCI device
  368.  *
  369.  * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  370.  * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
  371.  * access it.  Maybe we don't have a way to generate extended config space
  372.  * accesses, or the device is behind a reverse Express bridge.  So we try
  373.  * reading the dword at 0x100 which must either be 0 or a valid extended
  374.  * capability header.
  375.  */
  376. int pci_cfg_space_size_ext(struct pci_dev *dev)
  377. {
  378.     u32 status;
  379.     int pos = PCI_CFG_SPACE_SIZE;
  380.  
  381.     if (pci_read_config_dword(dev, pos, &status) != PCIBIOS_SUCCESSFUL)
  382.         goto fail;
  383.     if (status == 0xffffffff)
  384.         goto fail;
  385.  
  386.     return PCI_CFG_SPACE_EXP_SIZE;
  387.  
  388.  fail:
  389.     return PCI_CFG_SPACE_SIZE;
  390. }
  391.  
  392. int pci_cfg_space_size(struct pci_dev *dev)
  393. {
  394.     int pos;
  395.     u32 status;
  396.     u16 class;
  397.  
  398.     class = dev->class >> 8;
  399.     if (class == PCI_CLASS_BRIDGE_HOST)
  400.         return pci_cfg_space_size_ext(dev);
  401.  
  402.     pos = pci_pcie_cap(dev);
  403.     if (!pos) {
  404.         pos = pci_find_capability(dev, PCI_CAP_ID_PCIX);
  405.         if (!pos)
  406.             goto fail;
  407.  
  408.         pci_read_config_dword(dev, pos + PCI_X_STATUS, &status);
  409.         if (!(status & (PCI_X_STATUS_266MHZ | PCI_X_STATUS_533MHZ)))
  410.             goto fail;
  411.     }
  412.  
  413.     return pci_cfg_space_size_ext(dev);
  414.  
  415.  fail:
  416.     return PCI_CFG_SPACE_SIZE;
  417. }
  418.  
  419.  
  420.