Skip to content

Commit a693357

Browse files
author
Christoph Hellwig
committed
dma-mapping: simplify dma_init_coherent_memory
Return the allocated dma_coherent_mem structure, set the use_dma_pfn_offset and print the failure warning inside of dma_init_coherent_memory instead of leaving that to the callers. Signed-off-by: Christoph Hellwig <[email protected]> Tested-by: Dillon Min <[email protected]>
1 parent 70d6aa0 commit a693357

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed

kernel/dma/coherent.c

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,44 @@ static inline dma_addr_t dma_get_device_base(struct device *dev,
3737
return mem->device_base;
3838
}
3939

40-
static int dma_init_coherent_memory(phys_addr_t phys_addr,
41-
dma_addr_t device_addr, size_t size,
42-
struct dma_coherent_mem **mem)
40+
static struct dma_coherent_mem *dma_init_coherent_memory(phys_addr_t phys_addr,
41+
dma_addr_t device_addr, size_t size, bool use_dma_pfn_offset)
4342
{
44-
struct dma_coherent_mem *dma_mem = NULL;
45-
void *mem_base = NULL;
43+
struct dma_coherent_mem *dma_mem;
4644
int pages = size >> PAGE_SHIFT;
4745
int bitmap_size = BITS_TO_LONGS(pages) * sizeof(long);
48-
int ret;
46+
void *mem_base;
4947

50-
if (!size) {
51-
ret = -EINVAL;
52-
goto out;
53-
}
48+
if (!size)
49+
return ERR_PTR(-EINVAL);
5450

5551
mem_base = memremap(phys_addr, size, MEMREMAP_WC);
56-
if (!mem_base) {
57-
ret = -EINVAL;
58-
goto out;
59-
}
52+
if (!mem_base)
53+
return ERR_PTR(-EINVAL);
54+
6055
dma_mem = kzalloc(sizeof(struct dma_coherent_mem), GFP_KERNEL);
61-
if (!dma_mem) {
62-
ret = -ENOMEM;
63-
goto out;
64-
}
56+
if (!dma_mem)
57+
goto out_unmap_membase;
6558
dma_mem->bitmap = kzalloc(bitmap_size, GFP_KERNEL);
66-
if (!dma_mem->bitmap) {
67-
ret = -ENOMEM;
68-
goto out;
69-
}
59+
if (!dma_mem->bitmap)
60+
goto out_free_dma_mem;
7061

7162
dma_mem->virt_base = mem_base;
7263
dma_mem->device_base = device_addr;
7364
dma_mem->pfn_base = PFN_DOWN(phys_addr);
7465
dma_mem->size = pages;
66+
dma_mem->use_dev_dma_pfn_offset = use_dma_pfn_offset;
7567
spin_lock_init(&dma_mem->spinlock);
7668

77-
*mem = dma_mem;
78-
return 0;
69+
return dma_mem;
7970

80-
out:
71+
out_free_dma_mem:
8172
kfree(dma_mem);
82-
if (mem_base)
83-
memunmap(mem_base);
84-
return ret;
73+
out_unmap_membase:
74+
memunmap(mem_base);
75+
pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %zd MiB\n",
76+
&phys_addr, size / SZ_1M);
77+
return ERR_PTR(-ENOMEM);
8578
}
8679

8780
static void dma_release_coherent_memory(struct dma_coherent_mem *mem)
@@ -130,9 +123,9 @@ int dma_declare_coherent_memory(struct device *dev, phys_addr_t phys_addr,
130123
struct dma_coherent_mem *mem;
131124
int ret;
132125

133-
ret = dma_init_coherent_memory(phys_addr, device_addr, size, &mem);
134-
if (ret)
135-
return ret;
126+
mem = dma_init_coherent_memory(phys_addr, device_addr, size, false);
127+
if (IS_ERR(mem))
128+
return PTR_ERR(mem);
136129

137130
ret = dma_assign_coherent_memory(dev, mem);
138131
if (ret)
@@ -319,21 +312,16 @@ static struct reserved_mem *dma_reserved_default_memory __initdata;
319312

320313
static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev)
321314
{
322-
struct dma_coherent_mem *mem = rmem->priv;
323-
int ret;
324-
325-
if (!mem) {
326-
ret = dma_init_coherent_memory(rmem->base, rmem->base,
327-
rmem->size, &mem);
328-
if (ret) {
329-
pr_err("Reserved memory: failed to init DMA memory pool at %pa, size %ld MiB\n",
330-
&rmem->base, (unsigned long)rmem->size / SZ_1M);
331-
return ret;
332-
}
315+
if (!rmem->priv) {
316+
struct dma_coherent_mem *mem;
317+
318+
mem = dma_init_coherent_memory(rmem->base, rmem->base,
319+
rmem->size, true);
320+
if (IS_ERR(mem))
321+
return PTR_ERR(mem);
322+
rmem->priv = mem;
333323
}
334-
mem->use_dev_dma_pfn_offset = true;
335-
rmem->priv = mem;
336-
dma_assign_coherent_memory(dev, mem);
324+
dma_assign_coherent_memory(dev, rmem->priv);
337325
return 0;
338326
}
339327

0 commit comments

Comments
 (0)