Subversion Repositories Kolibri OS

Rev

Rev 2326 | Rev 2351 | 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.  
  354.     INIT_LIST_HEAD(&dev->link);
  355.  
  356.     if(unlikely(dev == NULL))
  357.         return NULL;
  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. static inline int pci_read_config_dword(struct pci_dev *dev, int where,
  681.                     u32 *val)
  682. {
  683.     *val = PciRead32(dev->busnr, dev->devfn, where);
  684.     return 1;
  685. }
  686.  
  687. static inline int pci_write_config_dword(struct pci_dev *dev, int where,
  688.                     u32 val)
  689. {
  690.     PciWrite32(dev->busnr, dev->devfn, where, val);
  691.     return 1;
  692. }
  693.  
  694. int pci_enable_rom(struct pci_dev *pdev)
  695. {
  696.     struct resource *res = pdev->resource + PCI_ROM_RESOURCE;
  697.     struct pci_bus_region region;
  698.     u32 rom_addr;
  699.  
  700.     if (!res->flags)
  701.             return -1;
  702.  
  703.     pcibios_resource_to_bus(pdev, &region, res);
  704.     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  705.     rom_addr &= ~PCI_ROM_ADDRESS_MASK;
  706.     rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
  707.     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  708.     return 0;
  709. }
  710.  
  711. void pci_disable_rom(struct pci_dev *pdev)
  712. {
  713.     u32 rom_addr;
  714.     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  715.     rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  716.     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  717. }
  718.  
  719. /**
  720.  * pci_get_rom_size - obtain the actual size of the ROM image
  721.  * @pdev: target PCI device
  722.  * @rom: kernel virtual pointer to image of ROM
  723.  * @size: size of PCI window
  724.  *  return: size of actual ROM image
  725.  *
  726.  * Determine the actual length of the ROM image.
  727.  * The PCI window size could be much larger than the
  728.  * actual image size.
  729.  */
  730. size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
  731. {
  732.         void __iomem *image;
  733.         int last_image;
  734.  
  735.         image = rom;
  736.         do {
  737.                 void __iomem *pds;
  738.                 /* Standard PCI ROMs start out with these bytes 55 AA */
  739.                 if (readb(image) != 0x55) {
  740.                         dev_err(&pdev->dev, "Invalid ROM contents\n");
  741.                         break;
  742.                 }
  743.                 if (readb(image + 1) != 0xAA)
  744.                         break;
  745.                 /* get the PCI data structure and check its signature */
  746.                 pds = image + readw(image + 24);
  747.                 if (readb(pds) != 'P')
  748.                         break;
  749.                 if (readb(pds + 1) != 'C')
  750.                         break;
  751.                 if (readb(pds + 2) != 'I')
  752.                         break;
  753.                 if (readb(pds + 3) != 'R')
  754.                         break;
  755.                 last_image = readb(pds + 21) & 0x80;
  756.                 /* this length is reliable */
  757.                 image += readw(pds + 16) * 512;
  758.         } while (!last_image);
  759.  
  760.         /* never return a size larger than the PCI resource window */
  761.         /* there are known ROMs that get the size wrong */
  762.         return min((size_t)(image - rom), size);
  763. }
  764.  
  765.  
  766. /**
  767.  * pci_map_rom - map a PCI ROM to kernel space
  768.  * @pdev: pointer to pci device struct
  769.  * @size: pointer to receive size of pci window over ROM
  770.  *
  771.  * Return: kernel virtual pointer to image of ROM
  772.  *
  773.  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  774.  * the shadow BIOS copy will be returned instead of the
  775.  * actual ROM.
  776.  */
  777. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  778. {
  779.         struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  780.         loff_t start;
  781.         void __iomem *rom;
  782.  
  783.         /*
  784.          * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
  785.          * memory map if the VGA enable bit of the Bridge Control register is
  786.          * set for embedded VGA.
  787.          */
  788.         if (res->flags & IORESOURCE_ROM_SHADOW) {
  789.                 /* primary video rom always starts here */
  790.                 start = (loff_t)0xC0000;
  791.                 *size = 0x20000; /* cover C000:0 through E000:0 */
  792.         } else {
  793.                 if (res->flags &
  794.                         (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
  795.                         *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  796.                         return (void __iomem *)(unsigned long)
  797.                                 pci_resource_start(pdev, PCI_ROM_RESOURCE);
  798.                 } else {
  799.                         /* assign the ROM an address if it doesn't have one */
  800. //                        if (res->parent == NULL &&
  801. //                            pci_assign_resource(pdev,PCI_ROM_RESOURCE))
  802.                                 return NULL;
  803. //                        start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  804. //                        *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  805. //                        if (*size == 0)
  806. //                                return NULL;
  807.  
  808.                         /* Enable ROM space decodes */
  809. //                        if (pci_enable_rom(pdev))
  810. //                                return NULL;
  811.                 }
  812.         }
  813.  
  814.         rom = ioremap(start, *size);
  815.         if (!rom) {
  816.                 /* restore enable if ioremap fails */
  817.                 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  818.                                     IORESOURCE_ROM_SHADOW |
  819.                                     IORESOURCE_ROM_COPY)))
  820.                         pci_disable_rom(pdev);
  821.                 return NULL;
  822.         }
  823.  
  824.         /*
  825.          * Try to find the true size of the ROM since sometimes the PCI window
  826.          * size is much larger than the actual size of the ROM.
  827.          * True size is important if the ROM is going to be copied.
  828.          */
  829.         *size = pci_get_rom_size(pdev, rom, *size);
  830.         return rom;
  831. }
  832.  
  833. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  834. {
  835.     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  836.  
  837.     if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
  838.             return;
  839.  
  840.     iounmap(rom);
  841.  
  842.     /* Disable again before continuing, leave enabled if pci=rom */
  843.     if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
  844.             pci_disable_rom(pdev);
  845. }
  846.  
  847.