blob: 0d3701bc88f603a1c43f2366d6578010735db873 [file] [log] [blame]
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -04001/*
2 * Port on Texas Instruments TMS320C6x architecture
3 *
4 * Copyright (C) 2004, 2009, 2010, 2011 Texas Instruments Incorporated
5 * Author: Aurelien Jacquiot <aurelien.jacquiot@ti.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * DMA uncached mapping support.
12 *
13 * Using code pulled from ARM
14 * Copyright (C) 2000-2004 Russell King
15 *
16 */
17#include <linux/slab.h>
18#include <linux/bitmap.h>
19#include <linux/bitops.h>
20#include <linux/module.h>
21#include <linux/interrupt.h>
Christoph Hellwig7f5c1ea2018-04-16 17:27:40 +020022#include <linux/dma-noncoherent.h>
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -040023#include <linux/memblock.h>
24
Christoph Hellwig7f5c1ea2018-04-16 17:27:40 +020025#include <asm/cacheflush.h>
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -040026#include <asm/page.h>
Christoph Hellwig7f5c1ea2018-04-16 17:27:40 +020027#include <asm/setup.h>
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -040028
29/*
30 * DMA coherent memory management, can be redefined using the memdma=
31 * kernel command line
32 */
33
34/* none by default */
35static phys_addr_t dma_base;
36static u32 dma_size;
37static u32 dma_pages;
38
39static unsigned long *dma_bitmap;
40
41/* bitmap lock */
42static DEFINE_SPINLOCK(dma_lock);
43
44/*
45 * Return a DMA coherent and contiguous memory chunk from the DMA memory
46 */
47static inline u32 __alloc_dma_pages(int order)
48{
49 unsigned long flags;
50 u32 pos;
51
52 spin_lock_irqsave(&dma_lock, flags);
53 pos = bitmap_find_free_region(dma_bitmap, dma_pages, order);
54 spin_unlock_irqrestore(&dma_lock, flags);
55
56 return dma_base + (pos << PAGE_SHIFT);
57}
58
59static void __free_dma_pages(u32 addr, int order)
60{
61 unsigned long flags;
62 u32 pos = (addr - dma_base) >> PAGE_SHIFT;
63
64 if (addr < dma_base || (pos + (1 << order)) >= dma_pages) {
65 printk(KERN_ERR "%s: freeing outside range.\n", __func__);
66 BUG();
67 }
68
69 spin_lock_irqsave(&dma_lock, flags);
70 bitmap_release_region(dma_bitmap, pos, order);
71 spin_unlock_irqrestore(&dma_lock, flags);
72}
73
74/*
75 * Allocate DMA coherent memory space and return both the kernel
76 * virtual and DMA address for that space.
77 */
Christoph Hellwig7f5c1ea2018-04-16 17:27:40 +020078void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -070079 gfp_t gfp, unsigned long attrs)
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -040080{
Christoph Hellwig518a2f12018-12-14 09:00:40 +010081 void *ret;
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -040082 u32 paddr;
83 int order;
84
85 if (!dma_size || !size)
86 return NULL;
87
88 order = get_count_order(((size - 1) >> PAGE_SHIFT) + 1);
89
90 paddr = __alloc_dma_pages(order);
91
92 if (handle)
93 *handle = paddr;
94
95 if (!paddr)
96 return NULL;
97
Christoph Hellwig518a2f12018-12-14 09:00:40 +010098 ret = phys_to_virt(paddr);
99 memset(ret, 0, 1 << order);
100 return ret;
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -0400101}
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -0400102
103/*
104 * Free DMA coherent memory as defined by the above mapping.
105 */
Christoph Hellwig7f5c1ea2018-04-16 17:27:40 +0200106void arch_dma_free(struct device *dev, size_t size, void *vaddr,
Krzysztof Kozlowski00085f12016-08-03 13:46:00 -0700107 dma_addr_t dma_handle, unsigned long attrs)
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -0400108{
109 int order;
110
111 if (!dma_size || !size)
112 return;
113
114 order = get_count_order(((size - 1) >> PAGE_SHIFT) + 1);
115
116 __free_dma_pages(virt_to_phys(vaddr), order);
117}
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -0400118
119/*
120 * Initialise the coherent DMA memory allocator using the given uncached region.
121 */
122void __init coherent_mem_init(phys_addr_t start, u32 size)
123{
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -0400124 if (!size)
125 return;
126
127 printk(KERN_INFO
128 "Coherent memory (DMA) region start=0x%x size=0x%x\n",
129 start, size);
130
131 dma_base = start;
132 dma_size = size;
133
134 /* allocate bitmap */
135 dma_pages = dma_size >> PAGE_SHIFT;
136 if (dma_size & (PAGE_SIZE - 1))
137 ++dma_pages;
138
Mike Rapoportb63a07d2019-03-07 16:31:06 -0800139 dma_bitmap = memblock_alloc(BITS_TO_LONGS(dma_pages) * sizeof(long),
140 sizeof(long));
Mike Rapoport8a7f97b2019-03-11 23:30:31 -0700141 if (!dma_bitmap)
142 panic("%s: Failed to allocate %zu bytes align=0x%zx\n",
143 __func__, BITS_TO_LONGS(dma_pages) * sizeof(long),
144 sizeof(long));
Aurelien Jacquiot14aa7e82011-10-04 12:17:19 -0400145}
Christoph Hellwig7f5c1ea2018-04-16 17:27:40 +0200146
147static void c6x_dma_sync(struct device *dev, phys_addr_t paddr, size_t size,
148 enum dma_data_direction dir)
149{
150 BUG_ON(!valid_dma_direction(dir));
151
152 switch (dir) {
153 case DMA_FROM_DEVICE:
154 L2_cache_block_invalidate(paddr, paddr + size);
155 break;
156 case DMA_TO_DEVICE:
157 L2_cache_block_writeback(paddr, paddr + size);
158 break;
159 case DMA_BIDIRECTIONAL:
160 L2_cache_block_writeback_invalidate(paddr, paddr + size);
161 break;
162 default:
163 break;
164 }
165}
166
167void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
168 size_t size, enum dma_data_direction dir)
169{
170 return c6x_dma_sync(dev, paddr, size, dir);
171}
172
173void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
174 size_t size, enum dma_data_direction dir)
175{
176 return c6x_dma_sync(dev, paddr, size, dir);
177}