blob: 20b6d60dd880b08e9376fe2cc9bd4e8d62672a85 [file] [log] [blame]
Masahiro Yamada9d86b892020-02-14 16:40:19 +09001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _LINUX_DMA_MAPPING_H
3#define _LINUX_DMA_MAPPING_H
4
5#include <linux/dma-direction.h>
6#include <linux/types.h>
7#include <asm/dma-mapping.h>
8#include <cpu_func.h>
9
10#define dma_mapping_error(x, y) 0
11
12/**
13 * Map a buffer to make it available to the DMA device
14 *
15 * Linux-like DMA API that is intended to be used from drivers. This hides the
16 * underlying cache operation from drivers. Call this before starting the DMA
17 * transfer. In most of architectures in U-Boot, the virtual address matches to
18 * the physical address (but we have exceptions like sandbox). U-Boot does not
19 * support iommu at the driver level, so it also matches to the DMA address.
20 * Hence, this helper currently just performs the cache operation, then returns
21 * straight-mapped dma_address, which is intended to be set to the register of
22 * the DMA device.
23 *
24 * @vaddr: address of the buffer
25 * @len: length of the buffer
26 * @dir: the direction of DMA
27 */
28static inline dma_addr_t dma_map_single(void *vaddr, size_t len,
29 enum dma_data_direction dir)
30{
31 unsigned long addr = (unsigned long)vaddr;
32
33 len = ALIGN(len, ARCH_DMA_MINALIGN);
34
35 if (dir == DMA_FROM_DEVICE)
36 invalidate_dcache_range(addr, addr + len);
37 else
38 flush_dcache_range(addr, addr + len);
39
40 return addr;
41}
42
43/**
44 * Unmap a buffer to make it available to CPU
45 *
46 * Linux-like DMA API that is intended to be used from drivers. This hides the
47 * underlying cache operation from drivers. Call this after finishin the DMA
48 * transfer.
49 *
50 * @addr: DMA address
51 * @len: length of the buffer
52 * @dir: the direction of DMA
53 */
54static inline void dma_unmap_single(dma_addr_t addr, size_t len,
55 enum dma_data_direction dir)
56{
57 len = ALIGN(len, ARCH_DMA_MINALIGN);
58
59 if (dir != DMA_TO_DEVICE)
60 invalidate_dcache_range(addr, addr + len);
61}
62
63#endif