2 * PowerPC version derived from arch/arm/mm/consistent.c
3 * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
5 * Copyright (C) 2000 Russell King
7 * Consistent memory allocators. Used for DMA devices that want to
8 * share uncached memory with the processor core. The function return
9 * is the virtual address and 'dma_handle' is the physical address.
10 * Mostly stolen from the ARM port, with some changes for PowerPC.
13 * Reorganized to get rid of the arch-specific consistent_* functions
14 * and provide non-coherent implementations for the DMA API. -Matt
16 * Added in_interrupt() safe dma_alloc_coherent()/dma_free_coherent()
17 * implementation. This is pulled straight from ARM and barely
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License version 2 as
22 * published by the Free Software Foundation.
25 #include <linux/sched.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/string.h>
29 #include <linux/types.h>
30 #include <linux/highmem.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/vmalloc.h>
34 #include <asm/tlbflush.h>
37 * Allocate DMA-coherent memory space and return both the kernel remapped
38 * virtual and bus address for that space.
41 __dma_alloc_coherent(size_t size, dma_addr_t *handle, gfp_t gfp)
46 unsigned int nr_pages = PAGE_ALIGN(size)>>PAGE_SHIFT;
47 unsigned int array_size = nr_pages * sizeof(struct page *);
50 u64 mask = 0x00ffffff, limit; /* ISA default */
51 struct vm_struct *area;
53 BUG_ON(!mem_init_done);
54 size = PAGE_ALIGN(size);
55 limit = (mask + 1) & ~mask;
56 if (limit && size >= limit) {
57 printk(KERN_WARNING "coherent allocation too big (requested "
58 "%#x mask %#Lx)\n", size, mask);
62 order = get_order(size);
64 if (mask != 0xffffffff)
67 page = alloc_pages(gfp, order);
71 end = page + (1 << order);
74 * Invalidate any data that might be lurking in the
75 * kernel direct-mapped region for device DMA.
78 unsigned long kaddr = (unsigned long)page_address(page);
79 memset(page_address(page), 0, size);
80 flush_dcache_range(kaddr, kaddr + size);
83 split_page(page, order);
86 * Set the "dma handle"
88 *handle = page_to_phys(page);
90 area = get_vm_area_caller(size, VM_IOREMAP,
91 __builtin_return_address(1));
95 if (array_size > PAGE_SIZE) {
96 pages = vmalloc(array_size);
97 area->flags |= VM_VPAGES;
99 pages = kmalloc(array_size, GFP_KERNEL);
105 area->nr_pages = nr_pages;
107 for (i = 0; i < nr_pages; i++)
110 if (map_vm_area(area, pgprot_noncached(PAGE_KERNEL), &pages))
114 * Free the otherwise unused pages.
125 if (array_size > PAGE_SIZE)
134 __free_pages(page, order);
138 EXPORT_SYMBOL(__dma_alloc_coherent);
141 * free a page as defined by the above mapping.
143 void __dma_free_coherent(size_t size, void *vaddr)
148 EXPORT_SYMBOL(__dma_free_coherent);
151 * make an area consistent.
153 void __dma_sync(void *vaddr, size_t size, int direction)
155 unsigned long start = (unsigned long)vaddr;
156 unsigned long end = start + size;
161 case DMA_FROM_DEVICE:
163 * invalidate only when cache-line aligned otherwise there is
164 * the potential for discarding uncommitted data from the cache
166 if ((start & (L1_CACHE_BYTES - 1)) || (size & (L1_CACHE_BYTES - 1)))
167 flush_dcache_range(start, end);
169 invalidate_dcache_range(start, end);
171 case DMA_TO_DEVICE: /* writeback only */
172 clean_dcache_range(start, end);
174 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
175 flush_dcache_range(start, end);
179 EXPORT_SYMBOL(__dma_sync);
181 #ifdef CONFIG_HIGHMEM
183 * __dma_sync_page() implementation for systems using highmem.
184 * In this case, each page of a buffer must be kmapped/kunmapped
185 * in order to have a virtual address for __dma_sync(). This must
186 * not sleep so kmap_atomic()/kunmap_atomic() are used.
188 * Note: yes, it is possible and correct to have a buffer extend
189 * beyond the first page.
191 static inline void __dma_sync_page_highmem(struct page *page,
192 unsigned long offset, size_t size, int direction)
194 size_t seg_size = min((size_t)(PAGE_SIZE - offset), size);
195 size_t cur_size = seg_size;
196 unsigned long flags, start, seg_offset = offset;
197 int nr_segs = 1 + ((size - seg_size) + PAGE_SIZE - 1)/PAGE_SIZE;
200 local_irq_save(flags);
203 start = (unsigned long)kmap_atomic(page + seg_nr,
204 KM_PPC_SYNC_PAGE) + seg_offset;
206 /* Sync this buffer segment */
207 __dma_sync((void *)start, seg_size, direction);
208 kunmap_atomic((void *)start, KM_PPC_SYNC_PAGE);
211 /* Calculate next buffer segment size */
212 seg_size = min((size_t)PAGE_SIZE, size - cur_size);
214 /* Add the segment size to our running total */
215 cur_size += seg_size;
217 } while (seg_nr < nr_segs);
219 local_irq_restore(flags);
221 #endif /* CONFIG_HIGHMEM */
224 * __dma_sync_page makes memory consistent. identical to __dma_sync, but
225 * takes a struct page instead of a virtual address
227 void __dma_sync_page(struct page *page, unsigned long offset,
228 size_t size, int direction)
230 #ifdef CONFIG_HIGHMEM
231 __dma_sync_page_highmem(page, offset, size, direction);
233 unsigned long start = (unsigned long)page_address(page) + offset;
234 __dma_sync((void *)start, size, direction);
237 EXPORT_SYMBOL(__dma_sync_page);