2018-08-24 15:31:08 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 The Linux Foundation
|
|
|
|
*/
|
2020-04-15 07:04:52 +07:00
|
|
|
#include <linux/dma-mapping.h>
|
2018-08-24 15:31:08 +07:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
|
2019-06-03 14:14:31 +07:00
|
|
|
struct page **dma_common_find_pages(void *cpu_addr)
|
|
|
|
{
|
|
|
|
struct vm_struct *area = find_vm_area(cpu_addr);
|
|
|
|
|
|
|
|
if (!area || area->flags != VM_DMA_COHERENT)
|
|
|
|
return NULL;
|
|
|
|
return area->pages;
|
|
|
|
}
|
|
|
|
|
2018-08-24 15:31:08 +07:00
|
|
|
/*
|
|
|
|
* Remaps an array of PAGE_SIZE pages into another vm_area.
|
|
|
|
* Cannot be used in non-sleeping contexts
|
|
|
|
*/
|
|
|
|
void *dma_common_pages_remap(struct page **pages, size_t size,
|
2019-08-30 13:51:01 +07:00
|
|
|
pgprot_t prot, const void *caller)
|
2018-08-24 15:31:08 +07:00
|
|
|
{
|
2020-06-02 11:50:32 +07:00
|
|
|
void *vaddr;
|
2018-08-24 15:31:08 +07:00
|
|
|
|
2020-06-02 11:50:32 +07:00
|
|
|
vaddr = vmap(pages, size >> PAGE_SHIFT, VM_DMA_COHERENT, prot);
|
|
|
|
if (vaddr)
|
|
|
|
find_vm_area(vaddr)->pages = pages;
|
|
|
|
return vaddr;
|
2018-08-24 15:31:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remaps an allocated contiguous region into another vm_area.
|
|
|
|
* Cannot be used in non-sleeping contexts
|
|
|
|
*/
|
|
|
|
void *dma_common_contiguous_remap(struct page *page, size_t size,
|
|
|
|
pgprot_t prot, const void *caller)
|
|
|
|
{
|
2020-06-02 11:50:32 +07:00
|
|
|
int count = size >> PAGE_SHIFT;
|
2018-08-24 15:31:08 +07:00
|
|
|
struct page **pages;
|
2020-06-02 11:50:32 +07:00
|
|
|
void *vaddr;
|
|
|
|
int i;
|
2018-08-24 15:31:08 +07:00
|
|
|
|
2020-06-02 11:50:32 +07:00
|
|
|
pages = kmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
|
2018-08-24 15:31:08 +07:00
|
|
|
if (!pages)
|
|
|
|
return NULL;
|
2020-06-02 11:50:32 +07:00
|
|
|
for (i = 0; i < count; i++)
|
2018-08-24 15:31:08 +07:00
|
|
|
pages[i] = nth_page(page, i);
|
2020-06-02 11:50:32 +07:00
|
|
|
vaddr = vmap(pages, count, VM_DMA_COHERENT, prot);
|
2018-08-24 15:31:08 +07:00
|
|
|
kfree(pages);
|
|
|
|
|
2020-06-02 11:50:32 +07:00
|
|
|
return vaddr;
|
2018-08-24 15:31:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmaps a range previously mapped by dma_common_*_remap
|
|
|
|
*/
|
2019-08-30 13:51:01 +07:00
|
|
|
void dma_common_free_remap(void *cpu_addr, size_t size)
|
2018-08-24 15:31:08 +07:00
|
|
|
{
|
2019-10-05 15:23:30 +07:00
|
|
|
struct vm_struct *area = find_vm_area(cpu_addr);
|
2018-08-24 15:31:08 +07:00
|
|
|
|
2019-10-05 15:23:30 +07:00
|
|
|
if (!area || area->flags != VM_DMA_COHERENT) {
|
2018-08-24 15:31:08 +07:00
|
|
|
WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unmap_kernel_range((unsigned long)cpu_addr, PAGE_ALIGN(size));
|
|
|
|
vunmap(cpu_addr);
|
|
|
|
}
|