Back to home page

LXR

 
 

    


0001 #include <linux/err.h>
0002 #include <linux/pci.h>
0003 #include <linux/io.h>
0004 #include <linux/gfp.h>
0005 #include <linux/export.h>
0006 
0007 void devm_ioremap_release(struct device *dev, void *res)
0008 {
0009     iounmap(*(void __iomem **)res);
0010 }
0011 
0012 static int devm_ioremap_match(struct device *dev, void *res, void *match_data)
0013 {
0014     return *(void **)res == match_data;
0015 }
0016 
0017 /**
0018  * devm_ioremap - Managed ioremap()
0019  * @dev: Generic device to remap IO address for
0020  * @offset: BUS offset to map
0021  * @size: Size of map
0022  *
0023  * Managed ioremap().  Map is automatically unmapped on driver detach.
0024  */
0025 void __iomem *devm_ioremap(struct device *dev, resource_size_t offset,
0026                resource_size_t size)
0027 {
0028     void __iomem **ptr, *addr;
0029 
0030     ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
0031     if (!ptr)
0032         return NULL;
0033 
0034     addr = ioremap(offset, size);
0035     if (addr) {
0036         *ptr = addr;
0037         devres_add(dev, ptr);
0038     } else
0039         devres_free(ptr);
0040 
0041     return addr;
0042 }
0043 EXPORT_SYMBOL(devm_ioremap);
0044 
0045 /**
0046  * devm_ioremap_nocache - Managed ioremap_nocache()
0047  * @dev: Generic device to remap IO address for
0048  * @offset: BUS offset to map
0049  * @size: Size of map
0050  *
0051  * Managed ioremap_nocache().  Map is automatically unmapped on driver
0052  * detach.
0053  */
0054 void __iomem *devm_ioremap_nocache(struct device *dev, resource_size_t offset,
0055                    resource_size_t size)
0056 {
0057     void __iomem **ptr, *addr;
0058 
0059     ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
0060     if (!ptr)
0061         return NULL;
0062 
0063     addr = ioremap_nocache(offset, size);
0064     if (addr) {
0065         *ptr = addr;
0066         devres_add(dev, ptr);
0067     } else
0068         devres_free(ptr);
0069 
0070     return addr;
0071 }
0072 EXPORT_SYMBOL(devm_ioremap_nocache);
0073 
0074 /**
0075  * devm_ioremap_wc - Managed ioremap_wc()
0076  * @dev: Generic device to remap IO address for
0077  * @offset: BUS offset to map
0078  * @size: Size of map
0079  *
0080  * Managed ioremap_wc().  Map is automatically unmapped on driver detach.
0081  */
0082 void __iomem *devm_ioremap_wc(struct device *dev, resource_size_t offset,
0083                   resource_size_t size)
0084 {
0085     void __iomem **ptr, *addr;
0086 
0087     ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL);
0088     if (!ptr)
0089         return NULL;
0090 
0091     addr = ioremap_wc(offset, size);
0092     if (addr) {
0093         *ptr = addr;
0094         devres_add(dev, ptr);
0095     } else
0096         devres_free(ptr);
0097 
0098     return addr;
0099 }
0100 EXPORT_SYMBOL(devm_ioremap_wc);
0101 
0102 /**
0103  * devm_iounmap - Managed iounmap()
0104  * @dev: Generic device to unmap for
0105  * @addr: Address to unmap
0106  *
0107  * Managed iounmap().  @addr must have been mapped using devm_ioremap*().
0108  */
0109 void devm_iounmap(struct device *dev, void __iomem *addr)
0110 {
0111     WARN_ON(devres_destroy(dev, devm_ioremap_release, devm_ioremap_match,
0112                    (__force void *)addr));
0113     iounmap(addr);
0114 }
0115 EXPORT_SYMBOL(devm_iounmap);
0116 
0117 /**
0118  * devm_ioremap_resource() - check, request region, and ioremap resource
0119  * @dev: generic device to handle the resource for
0120  * @res: resource to be handled
0121  *
0122  * Checks that a resource is a valid memory region, requests the memory
0123  * region and ioremaps it. All operations are managed and will be undone
0124  * on driver detach.
0125  *
0126  * Returns a pointer to the remapped memory or an ERR_PTR() encoded error code
0127  * on failure. Usage example:
0128  *
0129  *  res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0130  *  base = devm_ioremap_resource(&pdev->dev, res);
0131  *  if (IS_ERR(base))
0132  *      return PTR_ERR(base);
0133  */
0134 void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res)
0135 {
0136     resource_size_t size;
0137     const char *name;
0138     void __iomem *dest_ptr;
0139 
0140     BUG_ON(!dev);
0141 
0142     if (!res || resource_type(res) != IORESOURCE_MEM) {
0143         dev_err(dev, "invalid resource\n");
0144         return IOMEM_ERR_PTR(-EINVAL);
0145     }
0146 
0147     size = resource_size(res);
0148     name = res->name ?: dev_name(dev);
0149 
0150     if (!devm_request_mem_region(dev, res->start, size, name)) {
0151         dev_err(dev, "can't request region for resource %pR\n", res);
0152         return IOMEM_ERR_PTR(-EBUSY);
0153     }
0154 
0155     dest_ptr = devm_ioremap(dev, res->start, size);
0156     if (!dest_ptr) {
0157         dev_err(dev, "ioremap failed for resource %pR\n", res);
0158         devm_release_mem_region(dev, res->start, size);
0159         dest_ptr = IOMEM_ERR_PTR(-ENOMEM);
0160     }
0161 
0162     return dest_ptr;
0163 }
0164 EXPORT_SYMBOL(devm_ioremap_resource);
0165 
0166 #ifdef CONFIG_HAS_IOPORT_MAP
0167 /*
0168  * Generic iomap devres
0169  */
0170 static void devm_ioport_map_release(struct device *dev, void *res)
0171 {
0172     ioport_unmap(*(void __iomem **)res);
0173 }
0174 
0175 static int devm_ioport_map_match(struct device *dev, void *res,
0176                  void *match_data)
0177 {
0178     return *(void **)res == match_data;
0179 }
0180 
0181 /**
0182  * devm_ioport_map - Managed ioport_map()
0183  * @dev: Generic device to map ioport for
0184  * @port: Port to map
0185  * @nr: Number of ports to map
0186  *
0187  * Managed ioport_map().  Map is automatically unmapped on driver
0188  * detach.
0189  */
0190 void __iomem *devm_ioport_map(struct device *dev, unsigned long port,
0191                    unsigned int nr)
0192 {
0193     void __iomem **ptr, *addr;
0194 
0195     ptr = devres_alloc(devm_ioport_map_release, sizeof(*ptr), GFP_KERNEL);
0196     if (!ptr)
0197         return NULL;
0198 
0199     addr = ioport_map(port, nr);
0200     if (addr) {
0201         *ptr = addr;
0202         devres_add(dev, ptr);
0203     } else
0204         devres_free(ptr);
0205 
0206     return addr;
0207 }
0208 EXPORT_SYMBOL(devm_ioport_map);
0209 
0210 /**
0211  * devm_ioport_unmap - Managed ioport_unmap()
0212  * @dev: Generic device to unmap for
0213  * @addr: Address to unmap
0214  *
0215  * Managed ioport_unmap().  @addr must have been mapped using
0216  * devm_ioport_map().
0217  */
0218 void devm_ioport_unmap(struct device *dev, void __iomem *addr)
0219 {
0220     ioport_unmap(addr);
0221     WARN_ON(devres_destroy(dev, devm_ioport_map_release,
0222                    devm_ioport_map_match, (__force void *)addr));
0223 }
0224 EXPORT_SYMBOL(devm_ioport_unmap);
0225 #endif /* CONFIG_HAS_IOPORT_MAP */
0226 
0227 #ifdef CONFIG_PCI
0228 /*
0229  * PCI iomap devres
0230  */
0231 #define PCIM_IOMAP_MAX  PCI_ROM_RESOURCE
0232 
0233 struct pcim_iomap_devres {
0234     void __iomem *table[PCIM_IOMAP_MAX];
0235 };
0236 
0237 static void pcim_iomap_release(struct device *gendev, void *res)
0238 {
0239     struct pci_dev *dev = to_pci_dev(gendev);
0240     struct pcim_iomap_devres *this = res;
0241     int i;
0242 
0243     for (i = 0; i < PCIM_IOMAP_MAX; i++)
0244         if (this->table[i])
0245             pci_iounmap(dev, this->table[i]);
0246 }
0247 
0248 /**
0249  * pcim_iomap_table - access iomap allocation table
0250  * @pdev: PCI device to access iomap table for
0251  *
0252  * Access iomap allocation table for @dev.  If iomap table doesn't
0253  * exist and @pdev is managed, it will be allocated.  All iomaps
0254  * recorded in the iomap table are automatically unmapped on driver
0255  * detach.
0256  *
0257  * This function might sleep when the table is first allocated but can
0258  * be safely called without context and guaranteed to succed once
0259  * allocated.
0260  */
0261 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev)
0262 {
0263     struct pcim_iomap_devres *dr, *new_dr;
0264 
0265     dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL);
0266     if (dr)
0267         return dr->table;
0268 
0269     new_dr = devres_alloc(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL);
0270     if (!new_dr)
0271         return NULL;
0272     dr = devres_get(&pdev->dev, new_dr, NULL, NULL);
0273     return dr->table;
0274 }
0275 EXPORT_SYMBOL(pcim_iomap_table);
0276 
0277 /**
0278  * pcim_iomap - Managed pcim_iomap()
0279  * @pdev: PCI device to iomap for
0280  * @bar: BAR to iomap
0281  * @maxlen: Maximum length of iomap
0282  *
0283  * Managed pci_iomap().  Map is automatically unmapped on driver
0284  * detach.
0285  */
0286 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen)
0287 {
0288     void __iomem **tbl;
0289 
0290     BUG_ON(bar >= PCIM_IOMAP_MAX);
0291 
0292     tbl = (void __iomem **)pcim_iomap_table(pdev);
0293     if (!tbl || tbl[bar])   /* duplicate mappings not allowed */
0294         return NULL;
0295 
0296     tbl[bar] = pci_iomap(pdev, bar, maxlen);
0297     return tbl[bar];
0298 }
0299 EXPORT_SYMBOL(pcim_iomap);
0300 
0301 /**
0302  * pcim_iounmap - Managed pci_iounmap()
0303  * @pdev: PCI device to iounmap for
0304  * @addr: Address to unmap
0305  *
0306  * Managed pci_iounmap().  @addr must have been mapped using pcim_iomap().
0307  */
0308 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr)
0309 {
0310     void __iomem **tbl;
0311     int i;
0312 
0313     pci_iounmap(pdev, addr);
0314 
0315     tbl = (void __iomem **)pcim_iomap_table(pdev);
0316     BUG_ON(!tbl);
0317 
0318     for (i = 0; i < PCIM_IOMAP_MAX; i++)
0319         if (tbl[i] == addr) {
0320             tbl[i] = NULL;
0321             return;
0322         }
0323     WARN_ON(1);
0324 }
0325 EXPORT_SYMBOL(pcim_iounmap);
0326 
0327 /**
0328  * pcim_iomap_regions - Request and iomap PCI BARs
0329  * @pdev: PCI device to map IO resources for
0330  * @mask: Mask of BARs to request and iomap
0331  * @name: Name used when requesting regions
0332  *
0333  * Request and iomap regions specified by @mask.
0334  */
0335 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name)
0336 {
0337     void __iomem * const *iomap;
0338     int i, rc;
0339 
0340     iomap = pcim_iomap_table(pdev);
0341     if (!iomap)
0342         return -ENOMEM;
0343 
0344     for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
0345         unsigned long len;
0346 
0347         if (!(mask & (1 << i)))
0348             continue;
0349 
0350         rc = -EINVAL;
0351         len = pci_resource_len(pdev, i);
0352         if (!len)
0353             goto err_inval;
0354 
0355         rc = pci_request_region(pdev, i, name);
0356         if (rc)
0357             goto err_inval;
0358 
0359         rc = -ENOMEM;
0360         if (!pcim_iomap(pdev, i, 0))
0361             goto err_region;
0362     }
0363 
0364     return 0;
0365 
0366  err_region:
0367     pci_release_region(pdev, i);
0368  err_inval:
0369     while (--i >= 0) {
0370         if (!(mask & (1 << i)))
0371             continue;
0372         pcim_iounmap(pdev, iomap[i]);
0373         pci_release_region(pdev, i);
0374     }
0375 
0376     return rc;
0377 }
0378 EXPORT_SYMBOL(pcim_iomap_regions);
0379 
0380 /**
0381  * pcim_iomap_regions_request_all - Request all BARs and iomap specified ones
0382  * @pdev: PCI device to map IO resources for
0383  * @mask: Mask of BARs to iomap
0384  * @name: Name used when requesting regions
0385  *
0386  * Request all PCI BARs and iomap regions specified by @mask.
0387  */
0388 int pcim_iomap_regions_request_all(struct pci_dev *pdev, int mask,
0389                    const char *name)
0390 {
0391     int request_mask = ((1 << 6) - 1) & ~mask;
0392     int rc;
0393 
0394     rc = pci_request_selected_regions(pdev, request_mask, name);
0395     if (rc)
0396         return rc;
0397 
0398     rc = pcim_iomap_regions(pdev, mask, name);
0399     if (rc)
0400         pci_release_selected_regions(pdev, request_mask);
0401     return rc;
0402 }
0403 EXPORT_SYMBOL(pcim_iomap_regions_request_all);
0404 
0405 /**
0406  * pcim_iounmap_regions - Unmap and release PCI BARs
0407  * @pdev: PCI device to map IO resources for
0408  * @mask: Mask of BARs to unmap and release
0409  *
0410  * Unmap and release regions specified by @mask.
0411  */
0412 void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
0413 {
0414     void __iomem * const *iomap;
0415     int i;
0416 
0417     iomap = pcim_iomap_table(pdev);
0418     if (!iomap)
0419         return;
0420 
0421     for (i = 0; i < PCIM_IOMAP_MAX; i++) {
0422         if (!(mask & (1 << i)))
0423             continue;
0424 
0425         pcim_iounmap(pdev, iomap[i]);
0426         pci_release_region(pdev, i);
0427     }
0428 }
0429 EXPORT_SYMBOL(pcim_iounmap_regions);
0430 #endif /* CONFIG_PCI */