Subversion Repositories Kolibri OS

Rev

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

  1.  
  2. #include <linux/kernel.h>
  3. #include <linux/mutex.h>
  4. #include <linux/mod_devicetable.h>
  5. #include <errno-base.h>
  6. #include <pci.h>
  7. #include <syscall.h>
  8.  
  9. extern int pci_scan_filter(u32_t id, u32_t busnr, u32_t devfn);
  10.  
  11. static LIST_HEAD(devices);
  12.  
  13. /* PCI control bits.  Shares IORESOURCE_BITS with above PCI ROM.  */
  14. #define IORESOURCE_PCI_FIXED            (1<<4)  /* Do not move resource */
  15.  
  16. #define LEGACY_IO_RESOURCE      (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
  17.  
  18. /*
  19.  * Translate the low bits of the PCI base
  20.  * to the resource type
  21.  */
  22. static inline unsigned int pci_calc_resource_flags(unsigned int flags)
  23. {
  24.     if (flags & PCI_BASE_ADDRESS_SPACE_IO)
  25.         return IORESOURCE_IO;
  26.  
  27.     if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  28.         return IORESOURCE_MEM | IORESOURCE_PREFETCH;
  29.  
  30.     return IORESOURCE_MEM;
  31. }
  32.  
  33.  
  34. static u32_t pci_size(u32_t base, u32_t maxbase, u32_t mask)
  35. {
  36.     u32_t size = mask & maxbase;      /* Find the significant bits */
  37.  
  38.     if (!size)
  39.         return 0;
  40.  
  41.     /* Get the lowest of them to find the decode size, and
  42.        from that the extent.  */
  43.     size = (size & ~(size-1)) - 1;
  44.  
  45.     /* base == maxbase can be valid only if the BAR has
  46.        already been programmed with all 1s.  */
  47.     if (base == maxbase && ((base | size) & mask) != mask)
  48.         return 0;
  49.  
  50.     return size;
  51. }
  52.  
  53. static u64_t pci_size64(u64_t base, u64_t maxbase, u64_t mask)
  54. {
  55.     u64_t size = mask & maxbase;      /* Find the significant bits */
  56.  
  57.     if (!size)
  58.         return 0;
  59.  
  60.     /* Get the lowest of them to find the decode size, and
  61.        from that the extent.  */
  62.     size = (size & ~(size-1)) - 1;
  63.  
  64.     /* base == maxbase can be valid only if the BAR has
  65.        already been programmed with all 1s.  */
  66.     if (base == maxbase && ((base | size) & mask) != mask)
  67.         return 0;
  68.  
  69.     return size;
  70. }
  71.  
  72. static inline int is_64bit_memory(u32_t mask)
  73. {
  74.     if ((mask & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
  75.         (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64))
  76.         return 1;
  77.     return 0;
  78. }
  79.  
  80. static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
  81. {
  82.     u32_t  pos, reg, next;
  83.     u32_t  l, sz;
  84.     struct resource *res;
  85.  
  86.     for(pos=0; pos < howmany; pos = next)
  87.     {
  88.         u64_t  l64;
  89.         u64_t  sz64;
  90.         u32_t  raw_sz;
  91.  
  92.         next = pos + 1;
  93.  
  94.         res  = &dev->resource[pos];
  95.  
  96.         reg = PCI_BASE_ADDRESS_0 + (pos << 2);
  97.         l = PciRead32(dev->busnr, dev->devfn, reg);
  98.         PciWrite32(dev->busnr, dev->devfn, reg, ~0);
  99.         sz = PciRead32(dev->busnr, dev->devfn, reg);
  100.         PciWrite32(dev->busnr, dev->devfn, reg, l);
  101.  
  102.         if (!sz || sz == 0xffffffff)
  103.             continue;
  104.  
  105.         if (l == 0xffffffff)
  106.             l = 0;
  107.  
  108.         raw_sz = sz;
  109.         if ((l & PCI_BASE_ADDRESS_SPACE) ==
  110.                         PCI_BASE_ADDRESS_SPACE_MEMORY)
  111.         {
  112.             sz = pci_size(l, sz, (u32_t)PCI_BASE_ADDRESS_MEM_MASK);
  113.             /*
  114.              * For 64bit prefetchable memory sz could be 0, if the
  115.              * real size is bigger than 4G, so we need to check
  116.              * szhi for that.
  117.              */
  118.             if (!is_64bit_memory(l) && !sz)
  119.                     continue;
  120.             res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
  121.             res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;
  122.         }
  123.         else {
  124.             sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);
  125.             if (!sz)
  126.                 continue;
  127.             res->start = l & PCI_BASE_ADDRESS_IO_MASK;
  128.             res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
  129.         }
  130.         res->end = res->start + (unsigned long) sz;
  131.         res->flags |= pci_calc_resource_flags(l);
  132.         if (is_64bit_memory(l))
  133.         {
  134.             u32_t szhi, lhi;
  135.  
  136.             lhi = PciRead32(dev->busnr, dev->devfn, reg+4);
  137.             PciWrite32(dev->busnr, dev->devfn, reg+4, ~0);
  138.             szhi = PciRead32(dev->busnr, dev->devfn, reg+4);
  139.             PciWrite32(dev->busnr, dev->devfn, reg+4, lhi);
  140.             sz64 = ((u64_t)szhi << 32) | raw_sz;
  141.             l64 = ((u64_t)lhi << 32) | l;
  142.             sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK);
  143.             next++;
  144.  
  145. #if BITS_PER_LONG == 64
  146.             if (!sz64) {
  147.                 res->start = 0;
  148.                 res->end = 0;
  149.                 res->flags = 0;
  150.                 continue;
  151.             }
  152.             res->start = l64 & PCI_BASE_ADDRESS_MEM_MASK;
  153.             res->end = res->start + sz64;
  154. #else
  155.             if (sz64 > 0x100000000ULL) {
  156.                 printk(KERN_ERR "PCI: Unable to handle 64-bit "
  157.                                 "BAR for device %s\n", pci_name(dev));
  158.                 res->start = 0;
  159.                 res->flags = 0;
  160.             }
  161.             else if (lhi)
  162.             {
  163.                 /* 64-bit wide address, treat as disabled */
  164.                 PciWrite32(dev->busnr, dev->devfn, reg,
  165.                         l & ~(u32_t)PCI_BASE_ADDRESS_MEM_MASK);
  166.                 PciWrite32(dev->busnr, dev->devfn, reg+4, 0);
  167.                 res->start = 0;
  168.                 res->end = sz;
  169.             }
  170. #endif
  171.         }
  172.     }
  173.  
  174.     if ( rom )
  175.     {
  176.         dev->rom_base_reg = rom;
  177.         res = &dev->resource[PCI_ROM_RESOURCE];
  178.  
  179.         l = PciRead32(dev->busnr, dev->devfn, rom);
  180.         PciWrite32(dev->busnr, dev->devfn, rom, ~PCI_ROM_ADDRESS_ENABLE);
  181.         sz = PciRead32(dev->busnr, dev->devfn, rom);
  182.         PciWrite32(dev->busnr, dev->devfn, rom, l);
  183.  
  184.         if (l == 0xffffffff)
  185.             l = 0;
  186.  
  187.         if (sz && sz != 0xffffffff)
  188.         {
  189.             sz = pci_size(l, sz, (u32_t)PCI_ROM_ADDRESS_MASK);
  190.  
  191.             if (sz)
  192.             {
  193.                 res->flags = (l & IORESOURCE_ROM_ENABLE) |
  194.                                   IORESOURCE_MEM | IORESOURCE_PREFETCH |
  195.                                   IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  196.                 res->start = l & PCI_ROM_ADDRESS_MASK;
  197.                 res->end = res->start + (unsigned long) sz;
  198.             }
  199.         }
  200.     }
  201. }
  202.  
  203. static void pci_read_irq(struct pci_dev *dev)
  204. {
  205.     u8_t irq;
  206.  
  207.     irq = PciRead8(dev->busnr, dev->devfn, PCI_INTERRUPT_PIN);
  208.     dev->pin = irq;
  209.     if (irq)
  210.         irq = PciRead8(dev->busnr, dev->devfn, PCI_INTERRUPT_LINE);
  211.     dev->irq = irq;
  212. };
  213.  
  214.  
  215. int pci_setup_device(struct pci_dev *dev)
  216. {
  217.     u32_t  class;
  218.  
  219.     class = PciRead32(dev->busnr, dev->devfn, PCI_CLASS_REVISION);
  220.     dev->revision = class & 0xff;
  221.     class >>= 8;                                /* upper 3 bytes */
  222.     dev->class = class;
  223.  
  224.     /* "Unknown power state" */
  225. //    dev->current_state = PCI_UNKNOWN;
  226.  
  227.     /* Early fixups, before probing the BARs */
  228.  //   pci_fixup_device(pci_fixup_early, dev);
  229.     class = dev->class >> 8;
  230.  
  231.     switch (dev->hdr_type)
  232.     {
  233.         case PCI_HEADER_TYPE_NORMAL:                /* standard header */
  234.             if (class == PCI_CLASS_BRIDGE_PCI)
  235.                 goto bad;
  236.             pci_read_irq(dev);
  237.             pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
  238.             dev->subsystem_vendor = PciRead16(dev->busnr, dev->devfn,PCI_SUBSYSTEM_VENDOR_ID);
  239.             dev->subsystem_device = PciRead16(dev->busnr, dev->devfn, PCI_SUBSYSTEM_ID);
  240.  
  241.             /*
  242.              *      Do the ugly legacy mode stuff here rather than broken chip
  243.              *      quirk code. Legacy mode ATA controllers have fixed
  244.              *      addresses. These are not always echoed in BAR0-3, and
  245.              *      BAR0-3 in a few cases contain junk!
  246.              */
  247.             if (class == PCI_CLASS_STORAGE_IDE)
  248.             {
  249.                 u8_t progif;
  250.  
  251.                 progif = PciRead8(dev->busnr, dev->devfn,PCI_CLASS_PROG);
  252.                 if ((progif & 1) == 0)
  253.                 {
  254.                     dev->resource[0].start = 0x1F0;
  255.                     dev->resource[0].end = 0x1F7;
  256.                     dev->resource[0].flags = LEGACY_IO_RESOURCE;
  257.                     dev->resource[1].start = 0x3F6;
  258.                     dev->resource[1].end = 0x3F6;
  259.                     dev->resource[1].flags = LEGACY_IO_RESOURCE;
  260.                 }
  261.                 if ((progif & 4) == 0)
  262.                 {
  263.                     dev->resource[2].start = 0x170;
  264.                     dev->resource[2].end = 0x177;
  265.                     dev->resource[2].flags = LEGACY_IO_RESOURCE;
  266.                     dev->resource[3].start = 0x376;
  267.                     dev->resource[3].end = 0x376;
  268.                     dev->resource[3].flags = LEGACY_IO_RESOURCE;
  269.                 };
  270.             }
  271.             break;
  272.  
  273.         case PCI_HEADER_TYPE_BRIDGE:                /* bridge header */
  274.                 if (class != PCI_CLASS_BRIDGE_PCI)
  275.                         goto bad;
  276.                 /* The PCI-to-PCI bridge spec requires that subtractive
  277.                    decoding (i.e. transparent) bridge must have programming
  278.                    interface code of 0x01. */
  279.                 pci_read_irq(dev);
  280.                 dev->transparent = ((dev->class & 0xff) == 1);
  281.                 pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
  282.                 break;
  283.  
  284.         case PCI_HEADER_TYPE_CARDBUS:               /* CardBus bridge header */
  285.                 if (class != PCI_CLASS_BRIDGE_CARDBUS)
  286.                         goto bad;
  287.                 pci_read_irq(dev);
  288.                 pci_read_bases(dev, 1, 0);
  289.                 dev->subsystem_vendor = PciRead16(dev->busnr,
  290.                                                   dev->devfn,
  291.                                                   PCI_CB_SUBSYSTEM_VENDOR_ID);
  292.  
  293.                 dev->subsystem_device = PciRead16(dev->busnr,
  294.                                                   dev->devfn,
  295.                                                   PCI_CB_SUBSYSTEM_ID);
  296.                 break;
  297.  
  298.         default:                                    /* unknown header */
  299.                 printk(KERN_ERR "PCI: device %s has unknown header type %02x, ignoring.\n",
  300.                         pci_name(dev), dev->hdr_type);
  301.                 return -1;
  302.  
  303.         bad:
  304.                 printk(KERN_ERR "PCI: %s: class %x doesn't match header type %02x. Ignoring class.\n",
  305.                        pci_name(dev), class, dev->hdr_type);
  306.                 dev->class = PCI_CLASS_NOT_DEFINED;
  307.     }
  308.  
  309.     /* We found a fine healthy device, go go go... */
  310.  
  311.     return 0;
  312. };
  313.  
  314. static pci_dev_t* pci_scan_device(u32_t busnr, int devfn)
  315. {
  316.     pci_dev_t  *dev;
  317.  
  318.     u32_t   id;
  319.     u8_t    hdr;
  320.  
  321.     int     timeout = 10;
  322.  
  323.     id = PciRead32(busnr, devfn, PCI_VENDOR_ID);
  324.  
  325.     /* some broken boards return 0 or ~0 if a slot is empty: */
  326.     if (id == 0xffffffff || id == 0x00000000 ||
  327.         id == 0x0000ffff || id == 0xffff0000)
  328.         return NULL;
  329.  
  330.     while (id == 0xffff0001)
  331.     {
  332.  
  333.         delay(timeout/10);
  334.         timeout *= 2;
  335.  
  336.         id = PciRead32(busnr, devfn, PCI_VENDOR_ID);
  337.  
  338.         /* Card hasn't responded in 60 seconds?  Must be stuck. */
  339.         if (timeout > 60 * 100)
  340.         {
  341.             printk(KERN_WARNING "Device %04x:%02x:%02x.%d not "
  342.                    "responding\n", busnr,PCI_SLOT(devfn),PCI_FUNC(devfn));
  343.             return NULL;
  344.         }
  345.     };
  346.  
  347.     if( pci_scan_filter(id, busnr, devfn) == 0)
  348.         return NULL;
  349.  
  350.     hdr = PciRead8(busnr, devfn, PCI_HEADER_TYPE);
  351.  
  352.     dev = (pci_dev_t*)kzalloc(sizeof(pci_dev_t), 0);
  353.     if(unlikely(dev == NULL))
  354.         return NULL;
  355.  
  356.     INIT_LIST_HEAD(&dev->link);
  357.  
  358.  
  359.     dev->pci_dev.busnr    = busnr;
  360.     dev->pci_dev.devfn    = devfn;
  361.     dev->pci_dev.hdr_type = hdr & 0x7f;
  362.     dev->pci_dev.multifunction    = !!(hdr & 0x80);
  363.     dev->pci_dev.vendor   = id & 0xffff;
  364.     dev->pci_dev.device   = (id >> 16) & 0xffff;
  365.  
  366.     pci_setup_device(&dev->pci_dev);
  367.  
  368.     return dev;
  369.  
  370. };
  371.  
  372.  
  373.  
  374.  
  375. int pci_scan_slot(u32_t bus, int devfn)
  376. {
  377.     int  func, nr = 0;
  378.  
  379.     for (func = 0; func < 8; func++, devfn++)
  380.     {
  381.         pci_dev_t  *dev;
  382.  
  383.         dev = pci_scan_device(bus, devfn);
  384.         if( dev )
  385.         {
  386.             list_add(&dev->link, &devices);
  387.  
  388.             nr++;
  389.  
  390.             /*
  391.              * If this is a single function device,
  392.              * don't scan past the first function.
  393.              */
  394.             if (!dev->pci_dev.multifunction)
  395.             {
  396.                 if (func > 0) {
  397.                     dev->pci_dev.multifunction = 1;
  398.                 }
  399.                 else {
  400.                     break;
  401.                 }
  402.              }
  403.         }
  404.         else {
  405.             if (func == 0)
  406.                 break;
  407.         }
  408.     };
  409.  
  410.     return nr;
  411. };
  412.  
  413. #define PCI_FIND_CAP_TTL    48
  414.  
  415. static int __pci_find_next_cap_ttl(unsigned int bus, unsigned int devfn,
  416.                    u8 pos, int cap, int *ttl)
  417. {
  418.     u8 id;
  419.  
  420.     while ((*ttl)--) {
  421.         pos = PciRead8(bus, devfn, pos);
  422.         if (pos < 0x40)
  423.             break;
  424.         pos &= ~3;
  425.         id = PciRead8(bus, devfn, pos + PCI_CAP_LIST_ID);
  426.         if (id == 0xff)
  427.             break;
  428.         if (id == cap)
  429.             return pos;
  430.         pos += PCI_CAP_LIST_NEXT;
  431.     }
  432.     return 0;
  433. }
  434.  
  435. static int __pci_find_next_cap(unsigned int bus, unsigned int devfn,
  436.                    u8 pos, int cap)
  437. {
  438.     int ttl = PCI_FIND_CAP_TTL;
  439.  
  440.     return __pci_find_next_cap_ttl(bus, devfn, pos, cap, &ttl);
  441. }
  442.  
  443. static int __pci_bus_find_cap_start(unsigned int bus,
  444.                     unsigned int devfn, u8 hdr_type)
  445. {
  446.     u16 status;
  447.  
  448.     status = PciRead16(bus, devfn, PCI_STATUS);
  449.     if (!(status & PCI_STATUS_CAP_LIST))
  450.         return 0;
  451.  
  452.     switch (hdr_type) {
  453.     case PCI_HEADER_TYPE_NORMAL:
  454.     case PCI_HEADER_TYPE_BRIDGE:
  455.         return PCI_CAPABILITY_LIST;
  456.     case PCI_HEADER_TYPE_CARDBUS:
  457.         return PCI_CB_CAPABILITY_LIST;
  458.     default:
  459.         return 0;
  460.     }
  461.  
  462.     return 0;
  463. }
  464.  
  465.  
  466. int pci_find_capability(struct pci_dev *dev, int cap)
  467. {
  468.     int pos;
  469.  
  470.     pos = __pci_bus_find_cap_start(dev->busnr, dev->devfn, dev->hdr_type);
  471.     if (pos)
  472.         pos = __pci_find_next_cap(dev->busnr, dev->devfn, pos, cap);
  473.  
  474.     return pos;
  475. }
  476.  
  477.  
  478.  
  479.  
  480. int enum_pci_devices()
  481. {
  482.     pci_dev_t  *dev;
  483.     u32_t       last_bus;
  484.     u32_t       bus = 0 , devfn = 0;
  485.  
  486.  
  487.     last_bus = PciApi(1);
  488.  
  489.  
  490.     if( unlikely(last_bus == -1))
  491.         return -1;
  492.  
  493.     for(;bus <= last_bus; bus++)
  494.     {
  495.         for (devfn = 0; devfn < 0x100; devfn += 8)
  496.             pci_scan_slot(bus, devfn);
  497.  
  498.  
  499.     }
  500.     for(dev = (pci_dev_t*)devices.next;
  501.         &dev->link != &devices;
  502.         dev = (pci_dev_t*)dev->link.next)
  503.     {
  504.         dbgprintf("PCI device %x:%x bus:%x devfn:%x\n",
  505.                 dev->pci_dev.vendor,
  506.                 dev->pci_dev.device,
  507.                 dev->pci_dev.busnr,
  508.                 dev->pci_dev.devfn);
  509.  
  510.     }
  511.     return 0;
  512. }
  513.  
  514. const struct pci_device_id* find_pci_device(pci_dev_t* pdev, const struct pci_device_id *idlist)
  515. {
  516.     pci_dev_t *dev;
  517.     const struct pci_device_id *ent;
  518.  
  519.     for(dev = (pci_dev_t*)devices.next;
  520.         &dev->link != &devices;
  521.         dev = (pci_dev_t*)dev->link.next)
  522.     {
  523.         if( dev->pci_dev.vendor != idlist->vendor )
  524.             continue;
  525.  
  526.         for(ent = idlist; ent->vendor != 0; ent++)
  527.         {
  528.             if(unlikely(ent->device == dev->pci_dev.device))
  529.             {
  530.                 pdev->pci_dev = dev->pci_dev;
  531.                 return  ent;
  532.             }
  533.         };
  534.     }
  535.  
  536.     return NULL;
  537. };
  538.  
  539. struct pci_dev *
  540. pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  541. {
  542.     pci_dev_t *dev;
  543.  
  544.     dev = (pci_dev_t*)devices.next;
  545.  
  546.     if(from != NULL)
  547.     {
  548.         for(; &dev->link != &devices;
  549.             dev = (pci_dev_t*)dev->link.next)
  550.         {
  551.             if( &dev->pci_dev == from)
  552.             {
  553.                 dev = (pci_dev_t*)dev->link.next;
  554.                 break;
  555.             };
  556.         }
  557.     };
  558.  
  559.     for(; &dev->link != &devices;
  560.         dev = (pci_dev_t*)dev->link.next)
  561.     {
  562.         if( dev->pci_dev.vendor != vendor )
  563.             continue;
  564.  
  565.         if(dev->pci_dev.device == device)
  566.         {
  567.             return &dev->pci_dev;
  568.         }
  569.     }
  570.     return NULL;
  571. };
  572.  
  573.  
  574. struct pci_dev * pci_get_bus_and_slot(unsigned int bus, unsigned int devfn)
  575. {
  576.     pci_dev_t *dev;
  577.  
  578.     for(dev = (pci_dev_t*)devices.next;
  579.         &dev->link != &devices;
  580.         dev = (pci_dev_t*)dev->link.next)
  581.     {
  582.         if ( dev->pci_dev.busnr == bus && dev->pci_dev.devfn == devfn)
  583.             return &dev->pci_dev;
  584.     }
  585.     return NULL;
  586. }
  587.  
  588. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  589. {
  590.     pci_dev_t *dev;
  591.  
  592.     dev = (pci_dev_t*)devices.next;
  593.  
  594.     if(from != NULL)
  595.     {
  596.         for(; &dev->link != &devices;
  597.             dev = (pci_dev_t*)dev->link.next)
  598.         {
  599.             if( &dev->pci_dev == from)
  600.             {
  601.                 dev = (pci_dev_t*)dev->link.next;
  602.                 break;
  603.             };
  604.         }
  605.     };
  606.  
  607.     for(; &dev->link != &devices;
  608.         dev = (pci_dev_t*)dev->link.next)
  609.     {
  610.         if( dev->pci_dev.class == class)
  611.         {
  612.             return &dev->pci_dev;
  613.         }
  614.     }
  615.  
  616.    return NULL;
  617. }
  618.  
  619.  
  620. #define PIO_OFFSET      0x10000UL
  621. #define PIO_MASK        0x0ffffUL
  622. #define PIO_RESERVED    0x40000UL
  623.  
  624. #define IO_COND(addr, is_pio, is_mmio) do {            \
  625.     unsigned long port = (unsigned long __force)addr;  \
  626.     if (port >= PIO_RESERVED) {                        \
  627.         is_mmio;                                       \
  628.     } else if (port > PIO_OFFSET) {                    \
  629.         port &= PIO_MASK;                              \
  630.         is_pio;                                        \
  631.     };                                                 \
  632. } while (0)
  633.  
  634. /* Create a virtual mapping cookie for an IO port range */
  635. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  636. {
  637.     if (port > PIO_MASK)
  638.         return NULL;
  639.     return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
  640. }
  641.  
  642. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  643. {
  644.     resource_size_t start = pci_resource_start(dev, bar);
  645.     resource_size_t len = pci_resource_len(dev, bar);
  646.     unsigned long flags = pci_resource_flags(dev, bar);
  647.  
  648.     if (!len || !start)
  649.         return NULL;
  650.     if (maxlen && len > maxlen)
  651.         len = maxlen;
  652.     if (flags & IORESOURCE_IO)
  653.         return ioport_map(start, len);
  654.     if (flags & IORESOURCE_MEM) {
  655.         return ioremap(start, len);
  656.     }
  657.     /* What? */
  658.     return NULL;
  659. }
  660.  
  661. void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
  662. {
  663.     IO_COND(addr, /* nothing */, iounmap(addr));
  664. }
  665.  
  666.  
  667. struct pci_bus_region {
  668.     resource_size_t start;
  669.     resource_size_t end;
  670. };
  671.  
  672. static inline void
  673. pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  674.                          struct resource *res)
  675. {
  676.     region->start = res->start;
  677.     region->end = res->end;
  678. }
  679.  
  680.  
  681. int pci_enable_rom(struct pci_dev *pdev)
  682. {
  683.     struct resource *res = pdev->resource + PCI_ROM_RESOURCE;
  684.     struct pci_bus_region region;
  685.     u32 rom_addr;
  686.  
  687.     if (!res->flags)
  688.             return -1;
  689.  
  690.     pcibios_resource_to_bus(pdev, &region, res);
  691.     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  692.     rom_addr &= ~PCI_ROM_ADDRESS_MASK;
  693.     rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
  694.     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  695.     return 0;
  696. }
  697.  
  698. void pci_disable_rom(struct pci_dev *pdev)
  699. {
  700.     u32 rom_addr;
  701.     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  702.     rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  703.     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  704. }
  705.  
  706. /**
  707.  * pci_get_rom_size - obtain the actual size of the ROM image
  708.  * @pdev: target PCI device
  709.  * @rom: kernel virtual pointer to image of ROM
  710.  * @size: size of PCI window
  711.  *  return: size of actual ROM image
  712.  *
  713.  * Determine the actual length of the ROM image.
  714.  * The PCI window size could be much larger than the
  715.  * actual image size.
  716.  */
  717. size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
  718. {
  719.         void __iomem *image;
  720.         int last_image;
  721.  
  722.         image = rom;
  723.         do {
  724.                 void __iomem *pds;
  725.                 /* Standard PCI ROMs start out with these bytes 55 AA */
  726.                 if (readb(image) != 0x55) {
  727.                         dev_err(&pdev->dev, "Invalid ROM contents\n");
  728.                         break;
  729.                 }
  730.                 if (readb(image + 1) != 0xAA)
  731.                         break;
  732.                 /* get the PCI data structure and check its signature */
  733.                 pds = image + readw(image + 24);
  734.                 if (readb(pds) != 'P')
  735.                         break;
  736.                 if (readb(pds + 1) != 'C')
  737.                         break;
  738.                 if (readb(pds + 2) != 'I')
  739.                         break;
  740.                 if (readb(pds + 3) != 'R')
  741.                         break;
  742.                 last_image = readb(pds + 21) & 0x80;
  743.                 /* this length is reliable */
  744.                 image += readw(pds + 16) * 512;
  745.         } while (!last_image);
  746.  
  747.         /* never return a size larger than the PCI resource window */
  748.         /* there are known ROMs that get the size wrong */
  749.         return min((size_t)(image - rom), size);
  750. }
  751.  
  752.  
  753. /**
  754.  * pci_map_rom - map a PCI ROM to kernel space
  755.  * @pdev: pointer to pci device struct
  756.  * @size: pointer to receive size of pci window over ROM
  757.  *
  758.  * Return: kernel virtual pointer to image of ROM
  759.  *
  760.  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  761.  * the shadow BIOS copy will be returned instead of the
  762.  * actual ROM.
  763.  */
  764. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  765. {
  766.         struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  767.         loff_t start;
  768.         void __iomem *rom;
  769.  
  770.         /*
  771.          * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
  772.          * memory map if the VGA enable bit of the Bridge Control register is
  773.          * set for embedded VGA.
  774.          */
  775.         if (res->flags & IORESOURCE_ROM_SHADOW) {
  776.                 /* primary video rom always starts here */
  777.                 start = (loff_t)0xC0000;
  778.                 *size = 0x20000; /* cover C000:0 through E000:0 */
  779.         } else {
  780.                 if (res->flags &
  781.                         (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
  782.                         *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  783.                         return (void __iomem *)(unsigned long)
  784.                                 pci_resource_start(pdev, PCI_ROM_RESOURCE);
  785.                 } else {
  786.                                 start = (loff_t)0xC0000;
  787.                                 *size = 0x20000; /* cover C000:0 through E000:0 */
  788.  
  789.                 }
  790.         }
  791.  
  792.         rom = ioremap(start, *size);
  793.         if (!rom) {
  794.                 /* restore enable if ioremap fails */
  795.                 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  796.                                     IORESOURCE_ROM_SHADOW |
  797.                                     IORESOURCE_ROM_COPY)))
  798.                         pci_disable_rom(pdev);
  799.                 return NULL;
  800.         }
  801.  
  802.         /*
  803.          * Try to find the true size of the ROM since sometimes the PCI window
  804.          * size is much larger than the actual size of the ROM.
  805.          * True size is important if the ROM is going to be copied.
  806.          */
  807.         *size = pci_get_rom_size(pdev, rom, *size);
  808.         return rom;
  809. }
  810.  
  811. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  812. {
  813.     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  814.  
  815.     if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
  816.             return;
  817.  
  818.     iounmap(rom);
  819.  
  820.     /* Disable again before continuing, leave enabled if pci=rom */
  821.     if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
  822.             pci_disable_rom(pdev);
  823. }
  824.  
  825. #if 0
  826. void pcibios_set_master(struct pci_dev *dev)
  827. {
  828.     u8 lat;
  829.  
  830.     /* The latency timer doesn't apply to PCIe (either Type 0 or Type 1) */
  831.     if (pci_is_pcie(dev))
  832.             return;
  833.  
  834.     pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  835.     if (lat < 16)
  836.             lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  837.     else if (lat > pcibios_max_latency)
  838.             lat = pcibios_max_latency;
  839.     else
  840.             return;
  841.     dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
  842.     pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  843. }
  844. #endif
  845.  
  846.  
  847. static void __pci_set_master(struct pci_dev *dev, bool enable)
  848. {
  849.     u16 old_cmd, cmd;
  850.  
  851.     pci_read_config_word(dev, PCI_COMMAND, &old_cmd);
  852.     if (enable)
  853.             cmd = old_cmd | PCI_COMMAND_MASTER;
  854.     else
  855.             cmd = old_cmd & ~PCI_COMMAND_MASTER;
  856.     if (cmd != old_cmd) {
  857.             dbgprintf("%s bus mastering\n",
  858.                     enable ? "enabling" : "disabling");
  859.             pci_write_config_word(dev, PCI_COMMAND, cmd);
  860.     }
  861.     dev->is_busmaster = enable;
  862. }
  863.  
  864. void pci_set_master(struct pci_dev *dev)
  865. {
  866.     __pci_set_master(dev, true);
  867. //    pcibios_set_master(dev);
  868. }
  869.  
  870.  
  871.  
  872.