Subversion Repositories Kolibri OS

Rev

Rev 6938 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <syscall.h>
  2.  
  3. #include <linux/kernel.h>
  4. #include <linux/mutex.h>
  5. #include <linux/mod_devicetable.h>
  6. #include <linux/slab.h>
  7. #include <linux/pm.h>
  8.  
  9. #include <linux/pci.h>
  10.  
  11. extern int pci_scan_filter(u32 id, u32 busnr, u32 devfn);
  12.  
  13. static LIST_HEAD(devices);
  14.  
  15. /* PCI control bits.  Shares IORESOURCE_BITS with above PCI ROM.  */
  16. #define IORESOURCE_PCI_FIXED            (1<<4)  /* Do not move resource */
  17.  
  18. #define LEGACY_IO_RESOURCE      (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  19.  
  20. #define IORESOURCE_ROM_COPY             (1<<2)  /* ROM is alloc'd copy, resource field overlaid */
  21. #define IORESOURCE_ROM_BIOS_COPY        (1<<3)  /* ROM is BIOS copy, resource field overlaid */
  22.  
  23. /*
  24.  * Translate the low bits of the PCI base
  25.  * to the resource type
  26.  */
  27. static inline unsigned int pci_calc_resource_flags(unsigned int flags)
  28. {
  29.     if (flags & PCI_BASE_ADDRESS_SPACE_IO)
  30.         return IORESOURCE_IO;
  31.  
  32.     if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  33.         return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  34.  
  35.     return IORESOURCE_MEM;
  36. }
  37.  
  38.  
  39. static u32 pci_size(u32 base, u32 maxbase, u32 mask)
  40. {
  41.     u32 size = mask & maxbase;      /* Find the significant bits */
  42.  
  43.     if (!size)
  44.         return 0;
  45.  
  46.     /* Get the lowest of them to find the decode size, and
  47.        from that the extent.  */
  48.     size = (size & ~(size-1)) - 1;
  49.  
  50.     /* base == maxbase can be valid only if the BAR has
  51.        already been programmed with all 1s.  */
  52.     if (base == maxbase && ((base | size) & mask) != mask)
  53.         return 0;
  54.  
  55.     return size;
  56. }
  57.  
  58. static u64 pci_size64(u64 base, u64 maxbase, u64 mask)
  59. {
  60.     u64 size = mask & maxbase;      /* Find the significant bits */
  61.  
  62.     if (!size)
  63.         return 0;
  64.  
  65.     /* Get the lowest of them to find the decode size, and
  66.        from that the extent.  */
  67.     size = (size & ~(size-1)) - 1;
  68.  
  69.     /* base == maxbase can be valid only if the BAR has
  70.        already been programmed with all 1s.  */
  71.     if (base == maxbase && ((base | size) & mask) != mask)
  72.         return 0;
  73.  
  74.     return size;
  75. }
  76.  
  77. static inline int is_64bit_memory(u32 mask)
  78. {
  79.     if ((mask & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  80.         (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64))
  81.         return 1;
  82.     return 0;
  83. }
  84.  
  85. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  86. {
  87.     u32  pos, reg, next;
  88.     u32  l, sz;
  89.     struct resource *res;
  90.  
  91.     for(pos=0; pos < howmany; pos = next)
  92.     {
  93.         u64  l64;
  94.         u64  sz64;
  95.         u32  raw_sz;
  96.  
  97.         next = pos + 1;
  98.  
  99.         res  = &dev->resource[pos];
  100.  
  101.         reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  102.         l = PciRead32(dev->busnr, dev->devfn, reg);
  103.         PciWrite32(dev->busnr, dev->devfn, reg, ~0);
  104.         sz = PciRead32(dev->busnr, dev->devfn, reg);
  105.         PciWrite32(dev->busnr, dev->devfn, reg, l);
  106.  
  107.         if (!sz || sz == 0xffffffff)
  108.             continue;
  109.  
  110.         if (l == 0xffffffff)
  111.             l = 0;
  112.  
  113.         raw_sz = sz;
  114.         if ((l & PCI_BASE_ADDRESS_SPACE) ==
  115.                         PCI_BASE_ADDRESS_SPACE_MEMORY)
  116.         {
  117.             sz = pci_size(l, sz, (u32)PCI_BASE_ADDRESS_MEM_MASK);
  118.             /*
  119.              * For 64bit prefetchable memory sz could be 0, if the
  120.              * real size is bigger than 4G, so we need to check
  121.              * szhi for that.
  122.              */
  123.             if (!is_64bit_memory(l) && !sz)
  124.                     continue;
  125.             res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
  126.             res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;
  127.         }
  128.         else {
  129.             sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);
  130.             if (!sz)
  131.                 continue;
  132.             res->start = l & PCI_BASE_ADDRESS_IO_MASK;
  133.             res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
  134.         }
  135.         res->end = res->start + (unsigned long) sz;
  136.         res->flags |= pci_calc_resource_flags(l);
  137.         if (is_64bit_memory(l))
  138.         {
  139.             u32 szhi, lhi;
  140.  
  141.             lhi = PciRead32(dev->busnr, dev->devfn, reg+4);
  142.             PciWrite32(dev->busnr, dev->devfn, reg+4, ~0);
  143.             szhi = PciRead32(dev->busnr, dev->devfn, reg+4);
  144.             PciWrite32(dev->busnr, dev->devfn, reg+4, lhi);
  145.             sz64 = ((u64)szhi << 32) | raw_sz;
  146.             l64 = ((u64)lhi << 32) | l;
  147.             sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK);
  148.             next++;
  149.  
  150. #if BITS_PER_LONG == 64
  151.             if (!sz64) {
  152.                 res->start = 0;
  153.                 res->end = 0;
  154.                 res->flags = 0;
  155.                 continue;
  156.             }
  157.             res->start = l64 & PCI_BASE_ADDRESS_MEM_MASK;
  158.             res->end = res->start + sz64;
  159. #else
  160.             if (sz64 > 0x100000000ULL) {
  161.                 printk(KERN_ERR "PCI: Unable to handle 64-bit "
  162.                                 "BAR for device %s\n", pci_name(dev));
  163.                 res->start = 0;
  164.                 res->flags = 0;
  165.             }
  166.             else if (lhi)
  167.             {
  168.                 /* 64-bit wide address, treat as disabled */
  169.                 PciWrite32(dev->busnr, dev->devfn, reg,
  170.                         l & ~(u32)PCI_BASE_ADDRESS_MEM_MASK);
  171.                 PciWrite32(dev->busnr, dev->devfn, reg+4, 0);
  172.                 res->start = 0;
  173.                 res->end = sz;
  174.             }
  175. #endif
  176.         }
  177.     }
  178.  
  179.     if ( rom )
  180.     {
  181.         dev->rom_base_reg = rom;
  182.         res = &dev->resource[PCI_ROM_RESOURCE];
  183.  
  184.         l = PciRead32(dev->busnr, dev->devfn, rom);
  185.         PciWrite32(dev->busnr, dev->devfn, rom, ~PCI_ROM_ADDRESS_ENABLE);
  186.         sz = PciRead32(dev->busnr, dev->devfn, rom);
  187.         PciWrite32(dev->busnr, dev->devfn, rom, l);
  188.  
  189.         if (l == 0xffffffff)
  190.             l = 0;
  191.  
  192.         if (sz && sz != 0xffffffff)
  193.         {
  194.             sz = pci_size(l, sz, (u32)PCI_ROM_ADDRESS_MASK);
  195.  
  196.             if (sz)
  197.             {
  198.                 res->flags = (l & IORESOURCE_ROM_ENABLE) |
  199.                                   IORESOURCE_MEM | IORESOURCE_PREFETCH |
  200.                                   IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  201.                 res->start = l & PCI_ROM_ADDRESS_MASK;
  202.                 res->end = res->start + (unsigned long) sz;
  203.             }
  204.         }
  205.     }
  206. }
  207.  
  208. static void pci_read_irq(struct pci_dev *dev)
  209. {
  210.     u8 irq;
  211.  
  212.     irq = PciRead8(dev->busnr, dev->devfn, PCI_INTERRUPT_PIN);
  213.     dev->pin = irq;
  214.     if (irq)
  215.         irq = PciRead8(dev->busnr, dev->devfn, PCI_INTERRUPT_LINE);
  216.     dev->irq = irq;
  217. };
  218.  
  219.  
  220. int pci_setup_device(struct pci_dev *dev)
  221. {
  222.     u32  class;
  223.  
  224.     class = PciRead32(dev->busnr, dev->devfn, PCI_CLASS_REVISION);
  225.     dev->revision = class & 0xff;
  226.     class >>= 8;                                /* upper 3 bytes */
  227.     dev->class = class;
  228.  
  229.     /* "Unknown power state" */
  230. //    dev->current_state = PCI_UNKNOWN;
  231.  
  232.     /* Early fixups, before probing the BARs */
  233.  //   pci_fixup_device(pci_fixup_early, dev);
  234.     class = dev->class >> 8;
  235.  
  236.     switch (dev->hdr_type)
  237.     {
  238.         case PCI_HEADER_TYPE_NORMAL:                /* standard header */
  239.             if (class == PCI_CLASS_BRIDGE_PCI)
  240.                 goto bad;
  241.             pci_read_irq(dev);
  242.             pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  243.             dev->subsystem_vendor = PciRead16(dev->busnr, dev->devfn,PCI_SUBSYSTEM_VENDOR_ID);
  244.             dev->subsystem_device = PciRead16(dev->busnr, dev->devfn, PCI_SUBSYSTEM_ID);
  245.  
  246.             /*
  247.              *      Do the ugly legacy mode stuff here rather than broken chip
  248.              *      quirk code. Legacy mode ATA controllers have fixed
  249.              *      addresses. These are not always echoed in BAR0-3, and
  250.              *      BAR0-3 in a few cases contain junk!
  251.              */
  252.             if (class == PCI_CLASS_STORAGE_IDE)
  253.             {
  254.                 u8 progif;
  255.  
  256.                 progif = PciRead8(dev->busnr, dev->devfn,PCI_CLASS_PROG);
  257.                 if ((progif & 1) == 0)
  258.                 {
  259.                     dev->resource[0].start = 0x1F0;
  260.                     dev->resource[0].end = 0x1F7;
  261.                     dev->resource[0].flags = LEGACY_IO_RESOURCE;
  262.                     dev->resource[1].start = 0x3F6;
  263.                     dev->resource[1].end = 0x3F6;
  264.                     dev->resource[1].flags = LEGACY_IO_RESOURCE;
  265.                 }
  266.                 if ((progif & 4) == 0)
  267.                 {
  268.                     dev->resource[2].start = 0x170;
  269.                     dev->resource[2].end = 0x177;
  270.                     dev->resource[2].flags = LEGACY_IO_RESOURCE;
  271.                     dev->resource[3].start = 0x376;
  272.                     dev->resource[3].end = 0x376;
  273.                     dev->resource[3].flags = LEGACY_IO_RESOURCE;
  274.                 };
  275.             }
  276.             break;
  277.  
  278.         case PCI_HEADER_TYPE_BRIDGE:                /* bridge header */
  279.                 if (class != PCI_CLASS_BRIDGE_PCI)
  280.                         goto bad;
  281.                 /* The PCI-to-PCI bridge spec requires that subtractive
  282.                    decoding (i.e. transparent) bridge must have programming
  283.                    interface code of 0x01. */
  284.                 pci_read_irq(dev);
  285.                 dev->transparent = ((dev->class & 0xff) == 1);
  286.                 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  287.                 break;
  288.  
  289.         case PCI_HEADER_TYPE_CARDBUS:               /* CardBus bridge header */
  290.                 if (class != PCI_CLASS_BRIDGE_CARDBUS)
  291.                         goto bad;
  292.                 pci_read_irq(dev);
  293.                 pci_read_bases(dev, 1, 0);
  294.                 dev->subsystem_vendor = PciRead16(dev->busnr,
  295.                                                   dev->devfn,
  296.                                                   PCI_CB_SUBSYSTEM_VENDOR_ID);
  297.  
  298.                 dev->subsystem_device = PciRead16(dev->busnr,
  299.                                                   dev->devfn,
  300.                                                   PCI_CB_SUBSYSTEM_ID);
  301.                 break;
  302.  
  303.         default:                                    /* unknown header */
  304.                 printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",
  305.                         pci_name(dev), dev->hdr_type);
  306.                 return -1;
  307.  
  308.         bad:
  309.                 printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",
  310.                        pci_name(dev), class, dev->hdr_type);
  311.                 dev->class = PCI_CLASS_NOT_DEFINED;
  312.     }
  313.  
  314.     /* We found a fine healthy device, go go go... */
  315.  
  316.     return 0;
  317. };
  318.  
  319. static pci_dev_t* pci_scan_device(u32 busnr, int devfn)
  320. {
  321.     pci_dev_t  *dev;
  322.  
  323.     u32   id;
  324.     u8    hdr;
  325.  
  326.     int     timeout = 10;
  327.  
  328.     id = PciRead32(busnr, devfn, PCI_VENDOR_ID);
  329.  
  330.     /* some broken boards return 0 or ~0 if a slot is empty: */
  331.     if (id == 0xffffffff || id == 0x00000000 ||
  332.         id == 0x0000ffff || id == 0xffff0000)
  333.         return NULL;
  334.  
  335.     while (id == 0xffff0001)
  336.     {
  337.  
  338.         delay(timeout/10);
  339.         timeout *= 2;
  340.  
  341.         id = PciRead32(busnr, devfn, PCI_VENDOR_ID);
  342.  
  343.         /* Card hasn't responded in 60 seconds?  Must be stuck. */
  344.         if (timeout > 60 * 100)
  345.         {
  346.             printk(KERN_WARNING "Device %04x:%02x:%02x.%d not "
  347.                    "responding\n", busnr,PCI_SLOT(devfn),PCI_FUNC(devfn));
  348.             return NULL;
  349.         }
  350.     };
  351.  
  352.     if( pci_scan_filter(id, busnr, devfn) == 0)
  353.         return NULL;
  354.  
  355.     hdr = PciRead8(busnr, devfn, PCI_HEADER_TYPE);
  356.  
  357.     dev = (pci_dev_t*)kzalloc(sizeof(pci_dev_t), 0);
  358.     if(unlikely(dev == NULL))
  359.         return NULL;
  360.  
  361.     INIT_LIST_HEAD(&dev->link);
  362.  
  363.  
  364.     dev->pci_dev.busnr    = busnr;
  365.     dev->pci_dev.devfn    = devfn;
  366.     dev->pci_dev.hdr_type = hdr & 0x7f;
  367.     dev->pci_dev.multifunction    = !!(hdr & 0x80);
  368.     dev->pci_dev.vendor   = id & 0xffff;
  369.     dev->pci_dev.device   = (id >> 16) & 0xffff;
  370.  
  371.     pci_setup_device(&dev->pci_dev);
  372.  
  373.     return dev;
  374.  
  375. };
  376.  
  377.  
  378.  
  379.  
  380. int _pci_scan_slot(u32 bus, int devfn)
  381. {
  382.     int  func, nr = 0;
  383.  
  384.     for (func = 0; func < 8; func++, devfn++)
  385.     {
  386.         pci_dev_t  *dev;
  387.  
  388.         dev = pci_scan_device(bus, devfn);
  389.         if( dev )
  390.         {
  391.             list_add(&dev->link, &devices);
  392.  
  393.             nr++;
  394.  
  395.             /*
  396.              * If this is a single function device,
  397.              * don't scan past the first function.
  398.              */
  399.             if (!dev->pci_dev.multifunction)
  400.             {
  401.                 if (func > 0) {
  402.                     dev->pci_dev.multifunction = 1;
  403.                 }
  404.                 else {
  405.                     break;
  406.                 }
  407.              }
  408.         }
  409.         else {
  410.             if (func == 0)
  411.                 break;
  412.         }
  413.     };
  414.  
  415.     return nr;
  416. };
  417.  
  418. #define PCI_FIND_CAP_TTL    48
  419.  
  420. static int __pci_find_next_cap_ttl(unsigned int bus, unsigned int devfn,
  421.                    u8 pos, int cap, int *ttl)
  422. {
  423.     u8 id;
  424.  
  425.     while ((*ttl)--) {
  426.         pos = PciRead8(bus, devfn, pos);
  427.         if (pos < 0x40)
  428.             break;
  429.         pos &= ~3;
  430.         id = PciRead8(bus, devfn, pos + PCI_CAP_LIST_ID);
  431.         if (id == 0xff)
  432.             break;
  433.         if (id == cap)
  434.             return pos;
  435.         pos += PCI_CAP_LIST_NEXT;
  436.     }
  437.     return 0;
  438. }
  439.  
  440. static int __pci_find_next_cap(unsigned int bus, unsigned int devfn,
  441.                    u8 pos, int cap)
  442. {
  443.     int ttl = PCI_FIND_CAP_TTL;
  444.  
  445.     return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  446. }
  447.  
  448. static int __pci_bus_find_cap_start(unsigned int bus,
  449.                     unsigned int devfn, u8 hdr_type)
  450. {
  451.     u16 status;
  452.  
  453.     status = PciRead16(bus, devfn, PCI_STATUS);
  454.     if (!(status & PCI_STATUS_CAP_LIST))
  455.         return 0;
  456.  
  457.     switch (hdr_type) {
  458.     case PCI_HEADER_TYPE_NORMAL:
  459.     case PCI_HEADER_TYPE_BRIDGE:
  460.         return PCI_CAPABILITY_LIST;
  461.     case PCI_HEADER_TYPE_CARDBUS:
  462.         return PCI_CB_CAPABILITY_LIST;
  463.     default:
  464.         return 0;
  465.     }
  466.  
  467.     return 0;
  468. }
  469.  
  470.  
  471. int pci_find_capability(struct pci_dev *dev, int cap)
  472. {
  473.     int pos;
  474.  
  475.     pos = __pci_bus_find_cap_start(dev->busnr, dev->devfn, dev->hdr_type);
  476.     if (pos)
  477.         pos = __pci_find_next_cap(dev->busnr, dev->devfn, pos, cap);
  478.  
  479.     return pos;
  480. }
  481.  
  482.  
  483.  
  484.  
  485. int enum_pci_devices()
  486. {
  487.     pci_dev_t  *dev;
  488.     u32       last_bus;
  489.     u32       bus = 0 , devfn = 0;
  490.  
  491.  
  492.     last_bus = PciApi(1);
  493.  
  494.  
  495.     if( unlikely(last_bus == -1))
  496.         return -1;
  497.  
  498.     for(;bus <= last_bus; bus++)
  499.     {
  500.         for (devfn = 0; devfn < 0x100; devfn += 8)
  501.             _pci_scan_slot(bus, devfn);
  502.  
  503.  
  504.     }
  505.     for(dev = (pci_dev_t*)devices.next;
  506.         &dev->link != &devices;
  507.         dev = (pci_dev_t*)dev->link.next)
  508.     {
  509.         dbgprintf("PCI device %x:%x bus:%x devfn:%x\n",
  510.                 dev->pci_dev.vendor,
  511.                 dev->pci_dev.device,
  512.                 dev->pci_dev.busnr,
  513.                 dev->pci_dev.devfn);
  514.  
  515.     }
  516.     return 0;
  517. }
  518.  
  519. const struct pci_device_id* find_pci_device(pci_dev_t* pdev, const struct pci_device_id *idlist)
  520. {
  521.     pci_dev_t *dev;
  522.     const struct pci_device_id *ent;
  523.  
  524.     for(dev = (pci_dev_t*)devices.next;
  525.         &dev->link != &devices;
  526.         dev = (pci_dev_t*)dev->link.next)
  527.     {
  528.         if( dev->pci_dev.vendor != idlist->vendor )
  529.             continue;
  530.  
  531.         for(ent = idlist; ent->vendor != 0; ent++)
  532.         {
  533.             if(unlikely(ent->device == dev->pci_dev.device))
  534.             {
  535.                 pdev->pci_dev = dev->pci_dev;
  536.                 return  ent;
  537.             }
  538.         };
  539.     }
  540.  
  541.     return NULL;
  542. };
  543.  
  544. struct pci_dev *
  545. pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  546. {
  547.     pci_dev_t *dev;
  548.  
  549.     dev = (pci_dev_t*)devices.next;
  550.  
  551.     if(from != NULL)
  552.     {
  553.         for(; &dev->link != &devices;
  554.             dev = (pci_dev_t*)dev->link.next)
  555.         {
  556.             if( &dev->pci_dev == from)
  557.             {
  558.                 dev = (pci_dev_t*)dev->link.next;
  559.                 break;
  560.             };
  561.         }
  562.     };
  563.  
  564.     for(; &dev->link != &devices;
  565.         dev = (pci_dev_t*)dev->link.next)
  566.     {
  567.         if( dev->pci_dev.vendor != vendor )
  568.                 continue;
  569.  
  570.         if(dev->pci_dev.device == device)
  571.         {
  572.             return &dev->pci_dev;
  573.         }
  574.     }
  575.     return NULL;
  576. };
  577.  
  578.  
  579. struct pci_dev * _pci_get_bus_and_slot(unsigned int bus, unsigned int devfn)
  580. {
  581.     pci_dev_t *dev;
  582.  
  583.     for(dev = (pci_dev_t*)devices.next;
  584.         &dev->link != &devices;
  585.         dev = (pci_dev_t*)dev->link.next)
  586.     {
  587.         if ( dev->pci_dev.busnr == bus && dev->pci_dev.devfn == devfn)
  588.             return &dev->pci_dev;
  589.     }
  590.     return NULL;
  591. }
  592.  
  593. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  594. {
  595.     pci_dev_t *dev;
  596.  
  597.     dev = (pci_dev_t*)devices.next;
  598.  
  599.     if(from != NULL)
  600.     {
  601.         for(; &dev->link != &devices;
  602.             dev = (pci_dev_t*)dev->link.next)
  603.         {
  604.             if( &dev->pci_dev == from)
  605.             {
  606.                 dev = (pci_dev_t*)dev->link.next;
  607.                 break;
  608.             };
  609.         }
  610.     };
  611.  
  612.     for(; &dev->link != &devices;
  613.         dev = (pci_dev_t*)dev->link.next)
  614.     {
  615.         if( dev->pci_dev.class == class)
  616.         {
  617.             return &dev->pci_dev;
  618.         }
  619.     }
  620.  
  621.    return NULL;
  622. }
  623.  
  624.  
  625. #define PIO_OFFSET      0x10000UL
  626. #define PIO_MASK        0x0ffffUL
  627. #define PIO_RESERVED    0x40000UL
  628.  
  629. #define IO_COND(addr, is_pio, is_mmio) do {            \
  630.     unsigned long port = (unsigned long __force)addr;  \
  631.     if (port >= PIO_RESERVED) {                        \
  632.         is_mmio;                                       \
  633.     } else if (port > PIO_OFFSET) {                    \
  634.         port &= PIO_MASK;                              \
  635.         is_pio;                                        \
  636.     };                                                 \
  637. } while (0)
  638.  
  639. /* Create a virtual mapping cookie for an IO port range */
  640. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  641. {
  642.     if (port > PIO_MASK)
  643.         return NULL;
  644.     return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  645. }
  646.  
  647. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  648. {
  649.     resource_size_t start = pci_resource_start(dev, bar);
  650.     resource_size_t len = pci_resource_len(dev, bar);
  651.     unsigned long flags = pci_resource_flags(dev, bar);
  652.  
  653.     if (!len || !start)
  654.         return NULL;
  655.     if (maxlen && len > maxlen)
  656.         len = maxlen;
  657.     if (flags & IORESOURCE_IO)
  658.         return ioport_map(start, len);
  659.     if (flags & IORESOURCE_MEM) {
  660.         return ioremap(start, len);
  661.     }
  662.     /* What? */
  663.     return NULL;
  664. }
  665.  
  666. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  667. {
  668.     IO_COND(addr, /* nothing */, iounmap(addr));
  669. }
  670.  
  671.  
  672.  
  673.  
  674. int pci_enable_rom(struct pci_dev *pdev)
  675. {
  676.     struct resource *res = pdev->resource + PCI_ROM_RESOURCE;
  677.     struct pci_bus_region region;
  678.     u32 rom_addr;
  679.  
  680.     if (!res->flags)
  681.             return -1;
  682.  
  683.     _pcibios_resource_to_bus(pdev, &region, res);
  684.     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  685.     rom_addr &= ~PCI_ROM_ADDRESS_MASK;
  686.     rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
  687.     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  688.     return 0;
  689. }
  690.  
  691. void pci_disable_rom(struct pci_dev *pdev)
  692. {
  693.     u32 rom_addr;
  694.     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  695.     rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  696.     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  697. }
  698.  
  699. /**
  700.  * pci_get_rom_size - obtain the actual size of the ROM image
  701.  * @pdev: target PCI device
  702.  * @rom: kernel virtual pointer to image of ROM
  703.  * @size: size of PCI window
  704.  *  return: size of actual ROM image
  705.  *
  706.  * Determine the actual length of the ROM image.
  707.  * The PCI window size could be much larger than the
  708.  * actual image size.
  709.  */
  710. size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
  711. {
  712.         void __iomem *image;
  713.         int last_image;
  714.  
  715.         image = rom;
  716.         do {
  717.                 void __iomem *pds;
  718.                 /* Standard PCI ROMs start out with these bytes 55 AA */
  719.                 if (readb(image) != 0x55) {
  720.                         dev_err(&pdev->dev, "Invalid ROM contents\n");
  721.                         break;
  722.                 }
  723.                 if (readb(image + 1) != 0xAA)
  724.                         break;
  725.                 /* get the PCI data structure and check its signature */
  726.                 pds = image + readw(image + 24);
  727.                 if (readb(pds) != 'P')
  728.                         break;
  729.                 if (readb(pds + 1) != 'C')
  730.                         break;
  731.                 if (readb(pds + 2) != 'I')
  732.                         break;
  733.                 if (readb(pds + 3) != 'R')
  734.                         break;
  735.                 last_image = readb(pds + 21) & 0x80;
  736.                 /* this length is reliable */
  737.                 image += readw(pds + 16) * 512;
  738.         } while (!last_image);
  739.  
  740.         /* never return a size larger than the PCI resource window */
  741.         /* there are known ROMs that get the size wrong */
  742.         return min((size_t)(image - rom), size);
  743. }
  744.  
  745.  
  746. /**
  747.  * pci_map_rom - map a PCI ROM to kernel space
  748.  * @pdev: pointer to pci device struct
  749.  * @size: pointer to receive size of pci window over ROM
  750.  *
  751.  * Return: kernel virtual pointer to image of ROM
  752.  *
  753.  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  754.  * the shadow BIOS copy will be returned instead of the
  755.  * actual ROM.
  756.  */
  757. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  758. {
  759.     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  760.     loff_t start;
  761.     void __iomem *rom;
  762.  
  763.     /*
  764.      * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
  765.      * memory map if the VGA enable bit of the Bridge Control register is
  766.      * set for embedded VGA.
  767.      */
  768.     if (res->flags & IORESOURCE_ROM_SHADOW) {
  769.         /* primary video rom always starts here */
  770.         start = (loff_t)0xC0000;
  771.         *size = 0x20000; /* cover C000:0 through E000:0 */
  772.     } else {
  773.                 if (res->flags &
  774.                         (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
  775.             *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  776.              return (void __iomem *)(unsigned long)
  777.              pci_resource_start(pdev, PCI_ROM_RESOURCE);
  778.         } else {
  779.                                 start = (loff_t)0xC0000;
  780.                                 *size = 0x20000; /* cover C000:0 through E000:0 */
  781.  
  782.         }
  783.     }
  784.  
  785.     rom = ioremap(start, *size);
  786.     if (!rom) {
  787.             /* restore enable if ioremap fails */
  788.             if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  789.                                 IORESOURCE_ROM_SHADOW |
  790.                                 IORESOURCE_ROM_COPY)))
  791.                     pci_disable_rom(pdev);
  792.             return NULL;
  793.     }
  794.  
  795.     /*
  796.      * Try to find the true size of the ROM since sometimes the PCI window
  797.      * size is much larger than the actual size of the ROM.
  798.      * True size is important if the ROM is going to be copied.
  799.      */
  800.     *size = pci_get_rom_size(pdev, rom, *size);
  801.     return rom;
  802. }
  803.  
  804. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  805. {
  806.     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  807.  
  808.     if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
  809.             return;
  810.  
  811.     iounmap(rom);
  812.  
  813.     /* Disable again before continuing, leave enabled if pci=rom */
  814.     if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
  815.             pci_disable_rom(pdev);
  816. }
  817.  
  818. static void __pci_set_master(struct pci_dev *dev, bool enable)
  819. {
  820.     u16 old_cmd, cmd;
  821.  
  822.     pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  823.     if (enable)
  824.         cmd = old_cmd | PCI_COMMAND_MASTER;
  825.     else
  826.         cmd = old_cmd & ~PCI_COMMAND_MASTER;
  827.     if (cmd != old_cmd) {
  828.             dbgprintf("%s bus mastering\n",
  829.                     enable ? "enabling" : "disabling");
  830.         pci_write_config_word(dev, PCI_COMMAND, cmd);
  831.         }
  832.     dev->is_busmaster = enable;
  833. }
  834.  
  835.  
  836. /* pci_set_master - enables bus-mastering for device dev
  837.  * @dev: the PCI device to enable
  838.  *
  839.  * Enables bus-mastering on the device and calls pcibios_set_master()
  840.  * to do the needed arch specific settings.
  841.  */
  842. void pci_set_master(struct pci_dev *dev)
  843. {
  844.         __pci_set_master(dev, true);
  845. //        pcibios_set_master(dev);
  846. }
  847.  
  848. /**
  849.  * pci_clear_master - disables bus-mastering for device dev
  850.  * @dev: the PCI device to disable
  851.  */
  852. void pci_clear_master(struct pci_dev *dev)
  853. {
  854.         __pci_set_master(dev, false);
  855. }
  856.  
  857.  
  858. static inline int pcie_cap_version(const struct pci_dev *dev)
  859. {
  860.     return dev->pcie_flags_reg & PCI_EXP_FLAGS_VERS;
  861. }
  862.  
  863. static inline bool pcie_cap_has_devctl(const struct pci_dev *dev)
  864. {
  865.     return true;
  866. }
  867.  
  868. static inline bool pcie_cap_has_lnkctl(const struct pci_dev *dev)
  869. {
  870.     int type = pci_pcie_type(dev);
  871.  
  872.     return pcie_cap_version(dev) > 1 ||
  873.            type == PCI_EXP_TYPE_ROOT_PORT ||
  874.            type == PCI_EXP_TYPE_ENDPOINT ||
  875.            type == PCI_EXP_TYPE_LEG_END;
  876. }
  877.  
  878. static inline bool pcie_cap_has_sltctl(const struct pci_dev *dev)
  879. {
  880.     int type = pci_pcie_type(dev);
  881.  
  882.     return pcie_cap_version(dev) > 1 ||
  883.            type == PCI_EXP_TYPE_ROOT_PORT ||
  884.            (type == PCI_EXP_TYPE_DOWNSTREAM &&
  885.         dev->pcie_flags_reg & PCI_EXP_FLAGS_SLOT);
  886. }
  887.  
  888. static inline bool pcie_cap_has_rtctl(const struct pci_dev *dev)
  889. {
  890.     int type = pci_pcie_type(dev);
  891.  
  892.     return pcie_cap_version(dev) > 1 ||
  893.            type == PCI_EXP_TYPE_ROOT_PORT ||
  894.            type == PCI_EXP_TYPE_RC_EC;
  895. }
  896.  
  897. static bool pcie_capability_reg_implemented(struct pci_dev *dev, int pos)
  898. {
  899.     if (!pci_is_pcie(dev))
  900.         return false;
  901.  
  902.     switch (pos) {
  903.     case PCI_EXP_FLAGS_TYPE:
  904.         return true;
  905.     case PCI_EXP_DEVCAP:
  906.     case PCI_EXP_DEVCTL:
  907.     case PCI_EXP_DEVSTA:
  908.         return pcie_cap_has_devctl(dev);
  909.     case PCI_EXP_LNKCAP:
  910.     case PCI_EXP_LNKCTL:
  911.     case PCI_EXP_LNKSTA:
  912.         return pcie_cap_has_lnkctl(dev);
  913.     case PCI_EXP_SLTCAP:
  914.     case PCI_EXP_SLTCTL:
  915.     case PCI_EXP_SLTSTA:
  916.         return pcie_cap_has_sltctl(dev);
  917.     case PCI_EXP_RTCTL:
  918.     case PCI_EXP_RTCAP:
  919.     case PCI_EXP_RTSTA:
  920.         return pcie_cap_has_rtctl(dev);
  921.     case PCI_EXP_DEVCAP2:
  922.     case PCI_EXP_DEVCTL2:
  923.     case PCI_EXP_LNKCAP2:
  924.     case PCI_EXP_LNKCTL2:
  925.     case PCI_EXP_LNKSTA2:
  926.         return pcie_cap_version(dev) > 1;
  927.     default:
  928.         return false;
  929.     }
  930. }
  931.  
  932. /*
  933.  * Note that these accessor functions are only for the "PCI Express
  934.  * Capability" (see PCIe spec r3.0, sec 7.8).  They do not apply to the
  935.  * other "PCI Express Extended Capabilities" (AER, VC, ACS, MFVC, etc.)
  936.  */
  937. int pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val)
  938. {
  939.     int ret;
  940.  
  941.     *val = 0;
  942.     if (pos & 1)
  943.         return -EINVAL;
  944.  
  945.     if (pcie_capability_reg_implemented(dev, pos)) {
  946.         ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val);
  947.         /*
  948.          * Reset *val to 0 if pci_read_config_word() fails, it may
  949.          * have been written as 0xFFFF if hardware error happens
  950.          * during pci_read_config_word().
  951.          */
  952.         if (ret)
  953.             *val = 0;
  954.         return ret;
  955.     }
  956.  
  957.     /*
  958.      * For Functions that do not implement the Slot Capabilities,
  959.      * Slot Status, and Slot Control registers, these spaces must
  960.      * be hardwired to 0b, with the exception of the Presence Detect
  961.      * State bit in the Slot Status register of Downstream Ports,
  962.      * which must be hardwired to 1b.  (PCIe Base Spec 3.0, sec 7.8)
  963.      */
  964.     if (pci_is_pcie(dev) && pos == PCI_EXP_SLTSTA &&
  965.          pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) {
  966.         *val = PCI_EXP_SLTSTA_PDS;
  967.     }
  968.  
  969.     return 0;
  970. }
  971. EXPORT_SYMBOL(pcie_capability_read_word);
  972.  
  973. int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val)
  974. {
  975.     int ret;
  976.  
  977.     *val = 0;
  978.     if (pos & 3)
  979.         return -EINVAL;
  980.  
  981.     if (pcie_capability_reg_implemented(dev, pos)) {
  982.         ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  983.         /*
  984.          * Reset *val to 0 if pci_read_config_dword() fails, it may
  985.          * have been written as 0xFFFFFFFF if hardware error happens
  986.          * during pci_read_config_dword().
  987.          */
  988.         if (ret)
  989.             *val = 0;
  990.         return ret;
  991.     }
  992.  
  993.     if (pci_is_pcie(dev) && pos == PCI_EXP_SLTCTL &&
  994.          pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) {
  995.         *val = PCI_EXP_SLTSTA_PDS;
  996.     }
  997.  
  998.     return 0;
  999. }
  1000. EXPORT_SYMBOL(pcie_capability_read_dword);
  1001.  
  1002. int pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val)
  1003. {
  1004.     if (pos & 1)
  1005.         return -EINVAL;
  1006.  
  1007.     if (!pcie_capability_reg_implemented(dev, pos))
  1008.         return 0;
  1009.  
  1010.     return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val);
  1011. }
  1012. EXPORT_SYMBOL(pcie_capability_write_word);
  1013.  
  1014. int pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val)
  1015. {
  1016.     if (pos & 3)
  1017.         return -EINVAL;
  1018.  
  1019.     if (!pcie_capability_reg_implemented(dev, pos))
  1020.         return 0;
  1021.  
  1022.     return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val);
  1023. }
  1024. EXPORT_SYMBOL(pcie_capability_write_dword);
  1025.  
  1026. int pcie_capability_clear_and_set_word(struct pci_dev *dev, int pos,
  1027.                                        u16 clear, u16 set)
  1028. {
  1029.         int ret;
  1030.         u16 val;
  1031.  
  1032.         ret = pcie_capability_read_word(dev, pos, &val);
  1033.         if (!ret) {
  1034.                 val &= ~clear;
  1035.                 val |= set;
  1036.                 ret = pcie_capability_write_word(dev, pos, val);
  1037.         }
  1038.  
  1039.         return ret;
  1040. }
  1041.  
  1042.  
  1043.  
  1044. int pcie_get_readrq(struct pci_dev *dev)
  1045. {
  1046.         u16 ctl;
  1047.  
  1048.         pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &ctl);
  1049.  
  1050.         return 128 << ((ctl & PCI_EXP_DEVCTL_READRQ) >> 12);
  1051. }
  1052. EXPORT_SYMBOL(pcie_get_readrq);
  1053.  
  1054. /**
  1055.  * pcie_set_readrq - set PCI Express maximum memory read request
  1056.  * @dev: PCI device to query
  1057.  * @rq: maximum memory read count in bytes
  1058.  *    valid values are 128, 256, 512, 1024, 2048, 4096
  1059.  *
  1060.  * If possible sets maximum memory read request in bytes
  1061.  */
  1062. int pcie_set_readrq(struct pci_dev *dev, int rq)
  1063. {
  1064.         u16 v;
  1065.  
  1066.         if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
  1067.                 return -EINVAL;
  1068.  
  1069.         v = (ffs(rq) - 8) << 12;
  1070.  
  1071.         return pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
  1072.                                                   PCI_EXP_DEVCTL_READRQ, v);
  1073. }
  1074.  
  1075.