Commit f99c0f1c authored by Joerg Roedel's avatar Joerg Roedel

x86/amd-iommu: Use check_device in get_device_resources

Every call-place of get_device_resources calls check_device
before it. So call it from get_device_resources directly and
simplify the code.
Signed-off-by: default avatarJoerg Roedel <joerg.roedel@amd.com>
parent 420aef8a
...@@ -1432,35 +1432,24 @@ static struct dma_ops_domain *find_protection_domain(u16 devid) ...@@ -1432,35 +1432,24 @@ static struct dma_ops_domain *find_protection_domain(u16 devid)
* If the device is not yet associated with a domain this is also done * If the device is not yet associated with a domain this is also done
* in this function. * in this function.
*/ */
static int get_device_resources(struct device *dev, static bool get_device_resources(struct device *dev,
struct amd_iommu **iommu, struct amd_iommu **iommu,
struct protection_domain **domain, struct protection_domain **domain,
u16 *bdf) u16 *bdf)
{ {
struct dma_ops_domain *dma_dom; struct dma_ops_domain *dma_dom;
struct pci_dev *pcidev; struct pci_dev *pcidev;
u16 _bdf; u16 _bdf;
*iommu = NULL; if (!check_device(dev))
*domain = NULL; return false;
*bdf = 0xffff;
if (dev->bus != &pci_bus_type)
return 0;
pcidev = to_pci_dev(dev);
_bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
/* device not translated by any IOMMU in the system? */
if (_bdf > amd_iommu_last_bdf)
return 0;
*bdf = amd_iommu_alias_table[_bdf];
*iommu = amd_iommu_rlookup_table[*bdf]; pcidev = to_pci_dev(dev);
if (*iommu == NULL) _bdf = calc_devid(pcidev->bus->number, pcidev->devfn);
return 0; *bdf = amd_iommu_alias_table[_bdf];
*iommu = amd_iommu_rlookup_table[*bdf];
*domain = domain_for_device(*bdf); *domain = domain_for_device(*bdf);
if (*domain == NULL) { if (*domain == NULL) {
dma_dom = find_protection_domain(*bdf); dma_dom = find_protection_domain(*bdf);
if (!dma_dom) if (!dma_dom)
...@@ -1474,7 +1463,7 @@ static int get_device_resources(struct device *dev, ...@@ -1474,7 +1463,7 @@ static int get_device_resources(struct device *dev,
if (domain_for_device(_bdf) == NULL) if (domain_for_device(_bdf) == NULL)
attach_device(*iommu, *domain, _bdf); attach_device(*iommu, *domain, _bdf);
return 1; return true;
} }
static void update_device_table(struct protection_domain *domain) static void update_device_table(struct protection_domain *domain)
...@@ -1797,17 +1786,12 @@ static dma_addr_t map_page(struct device *dev, struct page *page, ...@@ -1797,17 +1786,12 @@ static dma_addr_t map_page(struct device *dev, struct page *page,
INC_STATS_COUNTER(cnt_map_single); INC_STATS_COUNTER(cnt_map_single);
if (!check_device(dev)) if (!get_device_resources(dev, &iommu, &domain, &devid))
return DMA_ERROR_CODE;
dma_mask = *dev->dma_mask;
get_device_resources(dev, &iommu, &domain, &devid);
if (iommu == NULL || domain == NULL)
/* device not handled by any AMD IOMMU */ /* device not handled by any AMD IOMMU */
return (dma_addr_t)paddr; return (dma_addr_t)paddr;
dma_mask = *dev->dma_mask;
if (!dma_ops_domain(domain)) if (!dma_ops_domain(domain))
return DMA_ERROR_CODE; return DMA_ERROR_CODE;
...@@ -1838,8 +1822,7 @@ static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size, ...@@ -1838,8 +1822,7 @@ static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size,
INC_STATS_COUNTER(cnt_unmap_single); INC_STATS_COUNTER(cnt_unmap_single);
if (!check_device(dev) || if (!get_device_resources(dev, &iommu, &domain, &devid))
!get_device_resources(dev, &iommu, &domain, &devid))
/* device not handled by any AMD IOMMU */ /* device not handled by any AMD IOMMU */
return; return;
...@@ -1893,16 +1876,11 @@ static int map_sg(struct device *dev, struct scatterlist *sglist, ...@@ -1893,16 +1876,11 @@ static int map_sg(struct device *dev, struct scatterlist *sglist,
INC_STATS_COUNTER(cnt_map_sg); INC_STATS_COUNTER(cnt_map_sg);
if (!check_device(dev)) if (!get_device_resources(dev, &iommu, &domain, &devid))
return 0; return map_sg_no_iommu(dev, sglist, nelems, dir);
dma_mask = *dev->dma_mask; dma_mask = *dev->dma_mask;
get_device_resources(dev, &iommu, &domain, &devid);
if (!iommu || !domain)
return map_sg_no_iommu(dev, sglist, nelems, dir);
if (!dma_ops_domain(domain)) if (!dma_ops_domain(domain))
return 0; return 0;
...@@ -1958,8 +1936,7 @@ static void unmap_sg(struct device *dev, struct scatterlist *sglist, ...@@ -1958,8 +1936,7 @@ static void unmap_sg(struct device *dev, struct scatterlist *sglist,
INC_STATS_COUNTER(cnt_unmap_sg); INC_STATS_COUNTER(cnt_unmap_sg);
if (!check_device(dev) || if (!get_device_resources(dev, &iommu, &domain, &devid))
!get_device_resources(dev, &iommu, &domain, &devid))
return; return;
if (!dma_ops_domain(domain)) if (!dma_ops_domain(domain))
...@@ -1994,24 +1971,22 @@ static void *alloc_coherent(struct device *dev, size_t size, ...@@ -1994,24 +1971,22 @@ static void *alloc_coherent(struct device *dev, size_t size,
INC_STATS_COUNTER(cnt_alloc_coherent); INC_STATS_COUNTER(cnt_alloc_coherent);
if (!check_device(dev)) if (!get_device_resources(dev, &iommu, &domain, &devid)) {
return NULL; virt_addr = (void *)__get_free_pages(flag, get_order(size));
*dma_addr = __pa(virt_addr);
return virt_addr;
}
if (!get_device_resources(dev, &iommu, &domain, &devid)) dma_mask = dev->coherent_dma_mask;
flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32); flag &= ~(__GFP_DMA | __GFP_HIGHMEM | __GFP_DMA32);
flag |= __GFP_ZERO;
flag |= __GFP_ZERO;
virt_addr = (void *)__get_free_pages(flag, get_order(size)); virt_addr = (void *)__get_free_pages(flag, get_order(size));
if (!virt_addr) if (!virt_addr)
return NULL; return NULL;
paddr = virt_to_phys(virt_addr); paddr = virt_to_phys(virt_addr);
if (!iommu || !domain) {
*dma_addr = (dma_addr_t)paddr;
return virt_addr;
}
if (!dma_ops_domain(domain)) if (!dma_ops_domain(domain))
goto out_free; goto out_free;
...@@ -2054,12 +2029,7 @@ static void free_coherent(struct device *dev, size_t size, ...@@ -2054,12 +2029,7 @@ static void free_coherent(struct device *dev, size_t size,
INC_STATS_COUNTER(cnt_free_coherent); INC_STATS_COUNTER(cnt_free_coherent);
if (!check_device(dev)) if (!get_device_resources(dev, &iommu, &domain, &devid))
return;
get_device_resources(dev, &iommu, &domain, &devid);
if (!iommu || !domain)
goto free_mem; goto free_mem;
if (!dma_ops_domain(domain)) if (!dma_ops_domain(domain))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment