blob: c75d5c3470e3595ce7af09f00346d5f82fc92a4c [file] [log] [blame]
Vineet Gupta1162b072013-01-18 15:12:20 +05301/*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
Christoph Hellwig6c3e71d2018-05-18 15:41:32 +02009#include <linux/dma-noncoherent.h>
Alexey Brodkinf2b0b252015-05-25 19:54:28 +030010#include <asm/cache.h>
Vineet Gupta1162b072013-01-18 15:12:20 +053011#include <asm/cacheflush.h>
12
Eugeniy Paltsev2820a702018-07-30 19:26:34 +030013/*
14 * ARCH specific callbacks for generic noncoherent DMA ops (dma/noncoherent.c)
15 * - hardware IOC not available (or "dma-coherent" not set for device in DT)
16 * - But still handle both coherent and non-coherent requests from caller
17 *
18 * For DMA coherent hardware (IOC) generic code suffices
19 */
Christoph Hellwig6c3e71d2018-05-18 15:41:32 +020020void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
21 gfp_t gfp, unsigned long attrs)
Vineet Gupta1162b072013-01-18 15:12:20 +053022{
Vineet Guptad98a15a2016-03-14 15:03:59 +053023 unsigned long order = get_order(size);
24 struct page *page;
25 phys_addr_t paddr;
26 void *kvaddr;
Eugeniy Paltsevdd452102018-07-30 19:26:36 +030027 bool need_coh = !(attrs & DMA_ATTR_NON_CONSISTENT);
28
29 /*
30 * __GFP_HIGHMEM flag is cleared by upper layer functions
31 * (in include/linux/dma-mapping.h) so we should never get a
32 * __GFP_HIGHMEM here.
33 */
34 BUG_ON(gfp & __GFP_HIGHMEM);
Vineet Gupta1162b072013-01-18 15:12:20 +053035
Vineet Guptad98a15a2016-03-14 15:03:59 +053036 page = alloc_pages(gfp, order);
37 if (!page)
Vineet Gupta1162b072013-01-18 15:12:20 +053038 return NULL;
39
Vineet Gupta6b700392016-03-14 15:34:36 +053040 /* This is linear addr (0x8000_0000 based) */
41 paddr = page_to_phys(page);
42
Christoph Hellwig57723cb2017-12-20 11:57:40 +010043 *dma_handle = paddr;
Vineet Gupta1162b072013-01-18 15:12:20 +053044
Eugeniy Paltsevdd452102018-07-30 19:26:36 +030045 /*
46 * A coherent buffer needs MMU mapping to enforce non-cachability.
47 * kvaddr is kernel Virtual address (0x7000_0000 based).
48 */
49 if (need_coh) {
Vineet Guptaf5db19e2016-03-16 15:04:39 +053050 kvaddr = ioremap_nocache(paddr, size);
Vineet Gupta6b700392016-03-14 15:34:36 +053051 if (kvaddr == NULL) {
52 __free_pages(page, order);
53 return NULL;
54 }
55 } else {
Vineet Guptaf5db19e2016-03-16 15:04:39 +053056 kvaddr = (void *)(u32)paddr;
Vineet Guptad98a15a2016-03-14 15:03:59 +053057 }
Vineet Gupta1162b072013-01-18 15:12:20 +053058
Vineet Gupta795f4552015-04-03 12:37:07 +030059 /*
60 * Evict any existing L1 and/or L2 lines for the backing page
61 * in case it was used earlier as a normal "cached" page.
62 * Yeah this bit us - STAR 9000898266
63 *
64 * Although core does call flush_cache_vmap(), it gets kvaddr hence
65 * can't be used to efficiently flush L1 and/or L2 which need paddr
66 * Currently flush_cache_vmap nukes the L1 cache completely which
67 * will be optimized as a separate commit
68 */
Vineet Gupta6b700392016-03-14 15:34:36 +053069 if (need_coh)
Vineet Guptaf5db19e2016-03-16 15:04:39 +053070 dma_cache_wback_inv(paddr, size);
Vineet Gupta795f4552015-04-03 12:37:07 +030071
Vineet Gupta1162b072013-01-18 15:12:20 +053072 return kvaddr;
73}
Vineet Gupta1162b072013-01-18 15:12:20 +053074
Christoph Hellwig6c3e71d2018-05-18 15:41:32 +020075void arch_dma_free(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070076 dma_addr_t dma_handle, unsigned long attrs)
Vineet Gupta1162b072013-01-18 15:12:20 +053077{
Christoph Hellwig57723cb2017-12-20 11:57:40 +010078 phys_addr_t paddr = dma_handle;
Vladimir Kondratievb4dff282016-07-03 10:07:48 +030079 struct page *page = virt_to_page(paddr);
Vineet Guptad98a15a2016-03-14 15:03:59 +053080
Eugeniy Paltsevdd452102018-07-30 19:26:36 +030081 if (!(attrs & DMA_ATTR_NON_CONSISTENT))
Christoph Hellwig052c96d2016-01-20 15:01:26 -080082 iounmap((void __force __iomem *)vaddr);
Vineet Gupta1162b072013-01-18 15:12:20 +053083
Vineet Guptad98a15a2016-03-14 15:03:59 +053084 __free_pages(page, get_order(size));
Vineet Gupta1162b072013-01-18 15:12:20 +053085}
Vineet Gupta1162b072013-01-18 15:12:20 +053086
Christoph Hellwig6c3e71d2018-05-18 15:41:32 +020087int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
88 void *cpu_addr, dma_addr_t dma_addr, size_t size,
89 unsigned long attrs)
Alexey Brodkina79a8122016-11-03 18:06:13 +030090{
91 unsigned long user_count = vma_pages(vma);
92 unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
Christoph Hellwig57723cb2017-12-20 11:57:40 +010093 unsigned long pfn = __phys_to_pfn(dma_addr);
Alexey Brodkina79a8122016-11-03 18:06:13 +030094 unsigned long off = vma->vm_pgoff;
95 int ret = -ENXIO;
96
97 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
98
Vladimir Murzin43fc5092017-07-20 11:19:58 +010099 if (dma_mmap_from_dev_coherent(dev, vma, cpu_addr, size, &ret))
Alexey Brodkina79a8122016-11-03 18:06:13 +0300100 return ret;
101
102 if (off < count && user_count <= (count - off)) {
103 ret = remap_pfn_range(vma, vma->vm_start,
104 pfn + off,
105 user_count << PAGE_SHIFT,
106 vma->vm_page_prot);
107 }
108
109 return ret;
110}
111
Eugeniy Paltsev4c612ad2018-07-24 17:13:02 +0300112/*
113 * Cache operations depending on function and direction argument, inspired by
114 * https://lkml.org/lkml/2018/5/18/979
115 * "dma_sync_*_for_cpu and direction=TO_DEVICE (was Re: [PATCH 02/20]
116 * dma-mapping: provide a generic dma-noncoherent implementation)"
117 *
118 * | map == for_device | unmap == for_cpu
119 * |----------------------------------------------------------------
120 * TO_DEV | writeback writeback | none none
121 * FROM_DEV | invalidate invalidate | invalidate* invalidate*
122 * BIDIR | writeback+inv writeback+inv | invalidate invalidate
123 *
124 * [*] needed for CPU speculative prefetches
125 *
126 * NOTE: we don't check the validity of direction argument as it is done in
127 * upper layer functions (in include/linux/dma-mapping.h)
128 */
129
Christoph Hellwig6c3e71d2018-05-18 15:41:32 +0200130void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
131 size_t size, enum dma_data_direction dir)
Christoph Hellwig713a7462018-05-18 15:14:28 +0200132{
Eugeniy Paltsev4c612ad2018-07-24 17:13:02 +0300133 switch (dir) {
134 case DMA_TO_DEVICE:
135 dma_cache_wback(paddr, size);
136 break;
137
138 case DMA_FROM_DEVICE:
139 dma_cache_inv(paddr, size);
140 break;
141
142 case DMA_BIDIRECTIONAL:
143 dma_cache_wback_inv(paddr, size);
144 break;
145
146 default:
147 break;
148 }
Christoph Hellwig713a7462018-05-18 15:14:28 +0200149}
150
Christoph Hellwig6c3e71d2018-05-18 15:41:32 +0200151void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
152 size_t size, enum dma_data_direction dir)
Christoph Hellwig713a7462018-05-18 15:14:28 +0200153{
Eugeniy Paltsev4c612ad2018-07-24 17:13:02 +0300154 switch (dir) {
155 case DMA_TO_DEVICE:
156 break;
157
158 /* FROM_DEVICE invalidate needed if speculative CPU prefetch only */
159 case DMA_FROM_DEVICE:
160 case DMA_BIDIRECTIONAL:
161 dma_cache_inv(paddr, size);
162 break;
163
164 default:
165 break;
166 }
Christoph Hellwig713a7462018-05-18 15:14:28 +0200167}
Eugeniy Paltsev2820a702018-07-30 19:26:34 +0300168
169/*
170 * Plug in coherent or noncoherent dma ops
171 */
172void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
173 const struct iommu_ops *iommu, bool coherent)
174{
175 /*
176 * IOC hardware snoops all DMA traffic keeping the caches consistent
177 * with memory - eliding need for any explicit cache maintenance of
178 * DMA buffers - so we can use dma_direct cache ops.
179 */
180 if (is_isa_arcv2() && ioc_enable && coherent) {
181 set_dma_ops(dev, &dma_direct_ops);
182 dev_info(dev, "use dma_direct_ops cache ops\n");
183 } else {
184 set_dma_ops(dev, &dma_noncoherent_ops);
185 dev_info(dev, "use dma_noncoherent_ops cache ops\n");
186 }
187}