Skip to content

Commit 0a65579

Browse files
Claire Changkonradwilk
authored andcommitted
swiotlb: Refactor swiotlb init functions
Add a new function, swiotlb_init_io_tlb_mem, for the io_tlb_mem struct initialization to make the code reusable. Signed-off-by: Claire Chang <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Tested-by: Stefano Stabellini <[email protected]> Acked-by: Stefano Stabellini <[email protected]> Tested-by: Will Deacon <[email protected]> Signed-off-by: Konrad Rzeszutek Wilk <[email protected]>
1 parent e73f0f0 commit 0a65579

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

kernel/dma/swiotlb.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,28 @@ void __init swiotlb_update_mem_attributes(void)
168168
memset(vaddr, 0, bytes);
169169
}
170170

171-
int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
171+
static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
172+
unsigned long nslabs, bool late_alloc)
172173
{
174+
void *vaddr = phys_to_virt(start);
173175
unsigned long bytes = nslabs << IO_TLB_SHIFT, i;
176+
177+
mem->nslabs = nslabs;
178+
mem->start = start;
179+
mem->end = mem->start + bytes;
180+
mem->index = 0;
181+
mem->late_alloc = late_alloc;
182+
spin_lock_init(&mem->lock);
183+
for (i = 0; i < mem->nslabs; i++) {
184+
mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i);
185+
mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
186+
mem->slots[i].alloc_size = 0;
187+
}
188+
memset(vaddr, 0, bytes);
189+
}
190+
191+
int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
192+
{
174193
struct io_tlb_mem *mem;
175194
size_t alloc_size;
176195

@@ -186,16 +205,8 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
186205
if (!mem)
187206
panic("%s: Failed to allocate %zu bytes align=0x%lx\n",
188207
__func__, alloc_size, PAGE_SIZE);
189-
mem->nslabs = nslabs;
190-
mem->start = __pa(tlb);
191-
mem->end = mem->start + bytes;
192-
mem->index = 0;
193-
spin_lock_init(&mem->lock);
194-
for (i = 0; i < mem->nslabs; i++) {
195-
mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i);
196-
mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
197-
mem->slots[i].alloc_size = 0;
198-
}
208+
209+
swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false);
199210

200211
io_tlb_default_mem = mem;
201212
if (verbose)
@@ -282,8 +293,8 @@ swiotlb_late_init_with_default_size(size_t default_size)
282293
int
283294
swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
284295
{
285-
unsigned long bytes = nslabs << IO_TLB_SHIFT, i;
286296
struct io_tlb_mem *mem;
297+
unsigned long bytes = nslabs << IO_TLB_SHIFT;
287298

288299
if (swiotlb_force == SWIOTLB_NO_FORCE)
289300
return 0;
@@ -297,20 +308,9 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
297308
if (!mem)
298309
return -ENOMEM;
299310

300-
mem->nslabs = nslabs;
301-
mem->start = virt_to_phys(tlb);
302-
mem->end = mem->start + bytes;
303-
mem->index = 0;
304-
mem->late_alloc = 1;
305-
spin_lock_init(&mem->lock);
306-
for (i = 0; i < mem->nslabs; i++) {
307-
mem->slots[i].list = IO_TLB_SEGSIZE - io_tlb_offset(i);
308-
mem->slots[i].orig_addr = INVALID_PHYS_ADDR;
309-
mem->slots[i].alloc_size = 0;
310-
}
311-
311+
memset(mem, 0, sizeof(*mem));
312312
set_memory_decrypted((unsigned long)tlb, bytes >> PAGE_SHIFT);
313-
memset(tlb, 0, bytes);
313+
swiotlb_init_io_tlb_mem(mem, virt_to_phys(tlb), nslabs, true);
314314

315315
io_tlb_default_mem = mem;
316316
swiotlb_print_info();

0 commit comments

Comments
 (0)