2018-08-24 11:31:08 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2014 The Linux Foundation
|
|
|
|
*/
|
2020-10-20 11:41:07 +03:00
|
|
|
#include <linux/dma-map-ops.h>
|
2018-08-24 11:31:08 +03:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
|
2019-06-03 10:14:31 +03: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 11:31:08 +03: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 09:51:01 +03:00
|
|
|
pgprot_t prot, const void *caller)
|
2018-08-24 11:31:08 +03:00
|
|
|
{
|
2020-06-02 07:50:32 +03:00
|
|
|
void *vaddr;
|
2018-08-24 11:31:08 +03:00
|
|
|
|
2020-06-23 15:07:55 +03:00
|
|
|
vaddr = vmap(pages, PAGE_ALIGN(size) >> PAGE_SHIFT,
|
|
|
|
VM_DMA_COHERENT, prot);
|
2020-06-02 07:50:32 +03:00
|
|
|
if (vaddr)
|
|
|
|
find_vm_area(vaddr)->pages = pages;
|
|
|
|
return vaddr;
|
2018-08-24 11:31:08 +03: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-23 15:07:55 +03:00
|
|
|
int count = PAGE_ALIGN(size) >> PAGE_SHIFT;
|
2018-08-24 11:31:08 +03:00
|
|
|
struct page **pages;
|
2020-06-02 07:50:32 +03:00
|
|
|
void *vaddr;
|
|
|
|
int i;
|
2018-08-24 11:31:08 +03:00
|
|
|
|
2020-06-02 07:50:32 +03:00
|
|
|
pages = kmalloc_array(count, sizeof(struct page *), GFP_KERNEL);
|
2018-08-24 11:31:08 +03:00
|
|
|
if (!pages)
|
|
|
|
return NULL;
|
2020-06-02 07:50:32 +03:00
|
|
|
for (i = 0; i < count; i++)
|
2018-08-24 11:31:08 +03:00
|
|
|
pages[i] = nth_page(page, i);
|
2020-06-02 07:50:32 +03:00
|
|
|
vaddr = vmap(pages, count, VM_DMA_COHERENT, prot);
|
2018-08-24 11:31:08 +03:00
|
|
|
kfree(pages);
|
|
|
|
|
2020-06-02 07:50:32 +03:00
|
|
|
return vaddr;
|
2018-08-24 11:31:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmaps a range previously mapped by dma_common_*_remap
|
|
|
|
*/
|
2019-08-30 09:51:01 +03:00
|
|
|
void dma_common_free_remap(void *cpu_addr, size_t size)
|
2018-08-24 11:31:08 +03:00
|
|
|
{
|
2019-10-05 11:23:30 +03:00
|
|
|
struct vm_struct *area = find_vm_area(cpu_addr);
|
2018-08-24 11:31:08 +03:00
|
|
|
|
2019-10-05 11:23:30 +03:00
|
|
|
if (!area || area->flags != VM_DMA_COHERENT) {
|
2018-08-24 11:31:08 +03:00
|
|
|
WARN(1, "trying to free invalid coherent area: %p\n", cpu_addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vunmap(cpu_addr);
|
|
|
|
}
|