Skip to content

Commit 7474043

Browse files
committed
Merge branch 'for-3.15' of git://git.linaro.org/people/mszyprowski/linux-dma-mapping
Pull DMA-mapping updates from Marek Szyprowski: "This contains extension for more efficient handling of io address space for dma-mapping subsystem for ARM architecture" * 'for-3.15' of git://git.linaro.org/people/mszyprowski/linux-dma-mapping: arm: dma-mapping: remove order parameter from arm_iommu_create_mapping() arm: dma-mapping: Add support to extend DMA IOMMU mappings
2 parents b9f2b21 + 68efd7d commit 7474043

6 files changed

Lines changed: 124 additions & 43 deletions

File tree

‎arch/arm/include/asm/dma-iommu.h‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ struct dma_iommu_mapping {
1313
/* iommu specific data */
1414
struct iommu_domain *domain;
1515

16-
void *bitmap;
17-
size_t bits;
18-
unsigned int order;
16+
unsigned long **bitmaps; /* array of bitmaps */
17+
unsigned int nr_bitmaps; /* nr of elements in array */
18+
unsigned int extensions;
19+
size_t bitmap_size; /* size of a single bitmap */
20+
size_t bits; /* per bitmap */
21+
unsigned int size; /* per bitmap */
1922
dma_addr_t base;
2023

2124
spinlock_t lock;
2225
struct kref kref;
2326
};
2427

2528
struct dma_iommu_mapping *
26-
arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
27-
int order);
29+
arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size);
2830

2931
void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping);
3032

‎arch/arm/mm/dma-mapping.c‎

Lines changed: 114 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,48 +1069,96 @@ fs_initcall(dma_debug_do_init);
10691069

10701070
/* IOMMU */
10711071

1072+
static int extend_iommu_mapping(struct dma_iommu_mapping *mapping);
1073+
10721074
static inline dma_addr_t __alloc_iova(struct dma_iommu_mapping *mapping,
10731075
size_t size)
10741076
{
10751077
unsigned int order = get_order(size);
10761078
unsigned int align = 0;
10771079
unsigned int count, start;
10781080
unsigned long flags;
1081+
dma_addr_t iova;
1082+
int i;
10791083

10801084
if (order > CONFIG_ARM_DMA_IOMMU_ALIGNMENT)
10811085
order = CONFIG_ARM_DMA_IOMMU_ALIGNMENT;
10821086

1083-
count = ((PAGE_ALIGN(size) >> PAGE_SHIFT) +
1084-
(1 << mapping->order) - 1) >> mapping->order;
1085-
1086-
if (order > mapping->order)
1087-
align = (1 << (order - mapping->order)) - 1;
1087+
count = PAGE_ALIGN(size) >> PAGE_SHIFT;
1088+
align = (1 << order) - 1;
10881089

10891090
spin_lock_irqsave(&mapping->lock, flags);
1090-
start = bitmap_find_next_zero_area(mapping->bitmap, mapping->bits, 0,
1091-
count, align);
1092-
if (start > mapping->bits) {
1093-
spin_unlock_irqrestore(&mapping->lock, flags);
1094-
return DMA_ERROR_CODE;
1091+
for (i = 0; i < mapping->nr_bitmaps; i++) {
1092+
start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1093+
mapping->bits, 0, count, align);
1094+
1095+
if (start > mapping->bits)
1096+
continue;
1097+
1098+
bitmap_set(mapping->bitmaps[i], start, count);
1099+
break;
10951100
}
10961101

1097-
bitmap_set(mapping->bitmap, start, count);
1102+
/*
1103+
* No unused range found. Try to extend the existing mapping
1104+
* and perform a second attempt to reserve an IO virtual
1105+
* address range of size bytes.
1106+
*/
1107+
if (i == mapping->nr_bitmaps) {
1108+
if (extend_iommu_mapping(mapping)) {
1109+
spin_unlock_irqrestore(&mapping->lock, flags);
1110+
return DMA_ERROR_CODE;
1111+
}
1112+
1113+
start = bitmap_find_next_zero_area(mapping->bitmaps[i],
1114+
mapping->bits, 0, count, align);
1115+
1116+
if (start > mapping->bits) {
1117+
spin_unlock_irqrestore(&mapping->lock, flags);
1118+
return DMA_ERROR_CODE;
1119+
}
1120+
1121+
bitmap_set(mapping->bitmaps[i], start, count);
1122+
}
10981123
spin_unlock_irqrestore(&mapping->lock, flags);
10991124

1100-
return mapping->base + (start << (mapping->order + PAGE_SHIFT));
1125+
iova = mapping->base + (mapping->size * i);
1126+
iova += start << PAGE_SHIFT;
1127+
1128+
return iova;
11011129
}
11021130

11031131
static inline void __free_iova(struct dma_iommu_mapping *mapping,
11041132
dma_addr_t addr, size_t size)
11051133
{
1106-
unsigned int start = (addr - mapping->base) >>
1107-
(mapping->order + PAGE_SHIFT);
1108-
unsigned int count = ((size >> PAGE_SHIFT) +
1109-
(1 << mapping->order) - 1) >> mapping->order;
1134+
unsigned int start, count;
11101135
unsigned long flags;
1136+
dma_addr_t bitmap_base;
1137+
u32 bitmap_index;
1138+
1139+
if (!size)
1140+
return;
1141+
1142+
bitmap_index = (u32) (addr - mapping->base) / (u32) mapping->size;
1143+
BUG_ON(addr < mapping->base || bitmap_index > mapping->extensions);
1144+
1145+
bitmap_base = mapping->base + mapping->size * bitmap_index;
1146+
1147+
start = (addr - bitmap_base) >> PAGE_SHIFT;
1148+
1149+
if (addr + size > bitmap_base + mapping->size) {
1150+
/*
1151+
* The address range to be freed reaches into the iova
1152+
* range of the next bitmap. This should not happen as
1153+
* we don't allow this in __alloc_iova (at the
1154+
* moment).
1155+
*/
1156+
BUG();
1157+
} else
1158+
count = size >> PAGE_SHIFT;
11111159

11121160
spin_lock_irqsave(&mapping->lock, flags);
1113-
bitmap_clear(mapping->bitmap, start, count);
1161+
bitmap_clear(mapping->bitmaps[bitmap_index], start, count);
11141162
spin_unlock_irqrestore(&mapping->lock, flags);
11151163
}
11161164

@@ -1875,8 +1923,7 @@ struct dma_map_ops iommu_coherent_ops = {
18751923
* arm_iommu_create_mapping
18761924
* @bus: pointer to the bus holding the client device (for IOMMU calls)
18771925
* @base: start address of the valid IO address space
1878-
* @size: size of the valid IO address space
1879-
* @order: accuracy of the IO addresses allocations
1926+
* @size: maximum size of the valid IO address space
18801927
*
18811928
* Creates a mapping structure which holds information about used/unused
18821929
* IO address ranges, which is required to perform memory allocation and
@@ -1886,38 +1933,54 @@ struct dma_map_ops iommu_coherent_ops = {
18861933
* arm_iommu_attach_device function.
18871934
*/
18881935
struct dma_iommu_mapping *
1889-
arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size,
1890-
int order)
1936+
arm_iommu_create_mapping(struct bus_type *bus, dma_addr_t base, size_t size)
18911937
{
1892-
unsigned int count = size >> (PAGE_SHIFT + order);
1893-
unsigned int bitmap_size = BITS_TO_LONGS(count) * sizeof(long);
1938+
unsigned int bits = size >> PAGE_SHIFT;
1939+
unsigned int bitmap_size = BITS_TO_LONGS(bits) * sizeof(long);
18941940
struct dma_iommu_mapping *mapping;
1941+
int extensions = 1;
18951942
int err = -ENOMEM;
18961943

1897-
if (!count)
1944+
if (!bitmap_size)
18981945
return ERR_PTR(-EINVAL);
18991946

1947+
if (bitmap_size > PAGE_SIZE) {
1948+
extensions = bitmap_size / PAGE_SIZE;
1949+
bitmap_size = PAGE_SIZE;
1950+
}
1951+
19001952
mapping = kzalloc(sizeof(struct dma_iommu_mapping), GFP_KERNEL);
19011953
if (!mapping)
19021954
goto err;
19031955

1904-
mapping->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
1905-
if (!mapping->bitmap)
1956+
mapping->bitmap_size = bitmap_size;
1957+
mapping->bitmaps = kzalloc(extensions * sizeof(unsigned long *),
1958+
GFP_KERNEL);
1959+
if (!mapping->bitmaps)
19061960
goto err2;
19071961

1962+
mapping->bitmaps[0] = kzalloc(bitmap_size, GFP_KERNEL);
1963+
if (!mapping->bitmaps[0])
1964+
goto err3;
1965+
1966+
mapping->nr_bitmaps = 1;
1967+
mapping->extensions = extensions;
19081968
mapping->base = base;
1969+
mapping->size = bitmap_size << PAGE_SHIFT;
19091970
mapping->bits = BITS_PER_BYTE * bitmap_size;
1910-
mapping->order = order;
1971+
19111972
spin_lock_init(&mapping->lock);
19121973

19131974
mapping->domain = iommu_domain_alloc(bus);
19141975
if (!mapping->domain)
1915-
goto err3;
1976+
goto err4;
19161977

19171978
kref_init(&mapping->kref);
19181979
return mapping;
1980+
err4:
1981+
kfree(mapping->bitmaps[0]);
19191982
err3:
1920-
kfree(mapping->bitmap);
1983+
kfree(mapping->bitmaps);
19211984
err2:
19221985
kfree(mapping);
19231986
err:
@@ -1927,14 +1990,35 @@ EXPORT_SYMBOL_GPL(arm_iommu_create_mapping);
19271990

19281991
static void release_iommu_mapping(struct kref *kref)
19291992
{
1993+
int i;
19301994
struct dma_iommu_mapping *mapping =
19311995
container_of(kref, struct dma_iommu_mapping, kref);
19321996

19331997
iommu_domain_free(mapping->domain);
1934-
kfree(mapping->bitmap);
1998+
for (i = 0; i < mapping->nr_bitmaps; i++)
1999+
kfree(mapping->bitmaps[i]);
2000+
kfree(mapping->bitmaps);
19352001
kfree(mapping);
19362002
}
19372003

2004+
static int extend_iommu_mapping(struct dma_iommu_mapping *mapping)
2005+
{
2006+
int next_bitmap;
2007+
2008+
if (mapping->nr_bitmaps > mapping->extensions)
2009+
return -EINVAL;
2010+
2011+
next_bitmap = mapping->nr_bitmaps;
2012+
mapping->bitmaps[next_bitmap] = kzalloc(mapping->bitmap_size,
2013+
GFP_ATOMIC);
2014+
if (!mapping->bitmaps[next_bitmap])
2015+
return -ENOMEM;
2016+
2017+
mapping->nr_bitmaps++;
2018+
2019+
return 0;
2020+
}
2021+
19382022
void arm_iommu_release_mapping(struct dma_iommu_mapping *mapping)
19392023
{
19402024
if (mapping)

‎drivers/gpu/drm/exynos/exynos_drm_drv.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ struct drm_exynos_file_private {
237237
* otherwise default one.
238238
* @da_space_size: size of device address space.
239239
* if 0 then default value is used for it.
240-
* @da_space_order: order to device address space.
241240
*/
242241
struct exynos_drm_private {
243242
struct drm_fb_helper *fb_helper;
@@ -255,7 +254,6 @@ struct exynos_drm_private {
255254

256255
unsigned long da_start;
257256
unsigned long da_space_size;
258-
unsigned long da_space_order;
259257
};
260258

261259
/*

‎drivers/gpu/drm/exynos/exynos_drm_iommu.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ int drm_create_iommu_mapping(struct drm_device *drm_dev)
3636
priv->da_start = EXYNOS_DEV_ADDR_START;
3737
if (!priv->da_space_size)
3838
priv->da_space_size = EXYNOS_DEV_ADDR_SIZE;
39-
if (!priv->da_space_order)
40-
priv->da_space_order = EXYNOS_DEV_ADDR_ORDER;
4139

4240
mapping = arm_iommu_create_mapping(&platform_bus_type, priv->da_start,
43-
priv->da_space_size,
44-
priv->da_space_order);
41+
priv->da_space_size);
42+
4543
if (IS_ERR(mapping))
4644
return PTR_ERR(mapping);
4745

‎drivers/gpu/drm/exynos/exynos_drm_iommu.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#define EXYNOS_DEV_ADDR_START 0x20000000
1616
#define EXYNOS_DEV_ADDR_SIZE 0x40000000
17-
#define EXYNOS_DEV_ADDR_ORDER 0x0
1817

1918
#ifdef CONFIG_DRM_EXYNOS_IOMMU
2019

‎drivers/iommu/shmobile-iommu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ static int shmobile_iommu_add_device(struct device *dev)
343343
mapping = archdata->iommu_mapping;
344344
if (!mapping) {
345345
mapping = arm_iommu_create_mapping(&platform_bus_type, 0,
346-
L1_LEN << 20, 0);
346+
L1_LEN << 20);
347347
if (IS_ERR(mapping))
348348
return PTR_ERR(mapping);
349349
archdata->iommu_mapping = mapping;

0 commit comments

Comments
 (0)