Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * PCI ROM access routines
0004  *
0005  * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
0006  * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
0007  */
0008 #include <linux/kernel.h>
0009 #include <linux/export.h>
0010 #include <linux/pci.h>
0011 #include <linux/slab.h>
0012 
0013 #include "pci.h"
0014 
0015 /**
0016  * pci_enable_rom - enable ROM decoding for a PCI device
0017  * @pdev: PCI device to enable
0018  *
0019  * Enable ROM decoding on @dev.  This involves simply turning on the last
0020  * bit of the PCI ROM BAR.  Note that some cards may share address decoders
0021  * between the ROM and other resources, so enabling it may disable access
0022  * to MMIO registers or other card memory.
0023  */
0024 int pci_enable_rom(struct pci_dev *pdev)
0025 {
0026     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
0027     struct pci_bus_region region;
0028     u32 rom_addr;
0029 
0030     if (!res->flags)
0031         return -1;
0032 
0033     /* Nothing to enable if we're using a shadow copy in RAM */
0034     if (res->flags & IORESOURCE_ROM_SHADOW)
0035         return 0;
0036 
0037     /*
0038      * Ideally pci_update_resource() would update the ROM BAR address,
0039      * and we would only set the enable bit here.  But apparently some
0040      * devices have buggy ROM BARs that read as zero when disabled.
0041      */
0042     pcibios_resource_to_bus(pdev->bus, &region, res);
0043     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
0044     rom_addr &= ~PCI_ROM_ADDRESS_MASK;
0045     rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
0046     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
0047     return 0;
0048 }
0049 EXPORT_SYMBOL_GPL(pci_enable_rom);
0050 
0051 /**
0052  * pci_disable_rom - disable ROM decoding for a PCI device
0053  * @pdev: PCI device to disable
0054  *
0055  * Disable ROM decoding on a PCI device by turning off the last bit in the
0056  * ROM BAR.
0057  */
0058 void pci_disable_rom(struct pci_dev *pdev)
0059 {
0060     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
0061     u32 rom_addr;
0062 
0063     if (res->flags & IORESOURCE_ROM_SHADOW)
0064         return;
0065 
0066     pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
0067     rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
0068     pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
0069 }
0070 EXPORT_SYMBOL_GPL(pci_disable_rom);
0071 
0072 /**
0073  * pci_get_rom_size - obtain the actual size of the ROM image
0074  * @pdev: target PCI device
0075  * @rom: kernel virtual pointer to image of ROM
0076  * @size: size of PCI window
0077  *  return: size of actual ROM image
0078  *
0079  * Determine the actual length of the ROM image.
0080  * The PCI window size could be much larger than the
0081  * actual image size.
0082  */
0083 static size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom,
0084                    size_t size)
0085 {
0086     void __iomem *image;
0087     int last_image;
0088     unsigned int length;
0089 
0090     image = rom;
0091     do {
0092         void __iomem *pds;
0093         /* Standard PCI ROMs start out with these bytes 55 AA */
0094         if (readw(image) != 0xAA55) {
0095             pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
0096                  readw(image));
0097             break;
0098         }
0099         /* get the PCI data structure and check its "PCIR" signature */
0100         pds = image + readw(image + 24);
0101         if (readl(pds) != 0x52494350) {
0102             pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
0103                  readl(pds));
0104             break;
0105         }
0106         last_image = readb(pds + 21) & 0x80;
0107         length = readw(pds + 16);
0108         image += length * 512;
0109         /* Avoid iterating through memory outside the resource window */
0110         if (image >= rom + size)
0111             break;
0112         if (!last_image) {
0113             if (readw(image) != 0xAA55) {
0114                 pci_info(pdev, "No more image in the PCI ROM\n");
0115                 break;
0116             }
0117         }
0118     } while (length && !last_image);
0119 
0120     /* never return a size larger than the PCI resource window */
0121     /* there are known ROMs that get the size wrong */
0122     return min((size_t)(image - rom), size);
0123 }
0124 
0125 /**
0126  * pci_map_rom - map a PCI ROM to kernel space
0127  * @pdev: pointer to pci device struct
0128  * @size: pointer to receive size of pci window over ROM
0129  *
0130  * Return: kernel virtual pointer to image of ROM
0131  *
0132  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
0133  * the shadow BIOS copy will be returned instead of the
0134  * actual ROM.
0135  */
0136 void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
0137 {
0138     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
0139     loff_t start;
0140     void __iomem *rom;
0141 
0142     /* assign the ROM an address if it doesn't have one */
0143     if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
0144         return NULL;
0145 
0146     start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
0147     *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
0148     if (*size == 0)
0149         return NULL;
0150 
0151     /* Enable ROM space decodes */
0152     if (pci_enable_rom(pdev))
0153         return NULL;
0154 
0155     rom = ioremap(start, *size);
0156     if (!rom)
0157         goto err_ioremap;
0158 
0159     /*
0160      * Try to find the true size of the ROM since sometimes the PCI window
0161      * size is much larger than the actual size of the ROM.
0162      * True size is important if the ROM is going to be copied.
0163      */
0164     *size = pci_get_rom_size(pdev, rom, *size);
0165     if (!*size)
0166         goto invalid_rom;
0167 
0168     return rom;
0169 
0170 invalid_rom:
0171     iounmap(rom);
0172 err_ioremap:
0173     /* restore enable if ioremap fails */
0174     if (!(res->flags & IORESOURCE_ROM_ENABLE))
0175         pci_disable_rom(pdev);
0176     return NULL;
0177 }
0178 EXPORT_SYMBOL(pci_map_rom);
0179 
0180 /**
0181  * pci_unmap_rom - unmap the ROM from kernel space
0182  * @pdev: pointer to pci device struct
0183  * @rom: virtual address of the previous mapping
0184  *
0185  * Remove a mapping of a previously mapped ROM
0186  */
0187 void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
0188 {
0189     struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
0190 
0191     iounmap(rom);
0192 
0193     /* Disable again before continuing */
0194     if (!(res->flags & IORESOURCE_ROM_ENABLE))
0195         pci_disable_rom(pdev);
0196 }
0197 EXPORT_SYMBOL(pci_unmap_rom);