2 * PowerPC version derived from arch/arm/mm/consistent.c
3 * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
5 * arch/ppc/mm/cachemap.c
7 * Copyright (C) 2000 Russell King
9 * Consistent memory allocators. Used for DMA devices that want to
10 * share uncached memory with the processor core. The function return
11 * is the virtual address and 'dma_handle' is the physical address.
12 * Mostly stolen from the ARM port, with some changes for PowerPC.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
20 #include <linux/config.h>
21 #include <linux/module.h>
22 #include <linux/signal.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 #include <linux/ptrace.h>
29 #include <linux/mman.h>
31 #include <linux/swap.h>
32 #include <linux/stddef.h>
33 #include <linux/vmalloc.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36 #include <linux/bootmem.h>
37 #include <linux/highmem.h>
38 #include <linux/dma-mapping.h>
40 #include <asm/pgalloc.h>
43 #include <asm/hardirq.h>
44 #include <asm/mmu_context.h>
45 #include <asm/pgtable.h>
47 #include <asm/uaccess.h>
49 #include <asm/machdep.h>
51 int map_page(unsigned long va, phys_addr_t pa, int flags);
53 /* This function will allocate the requested contiguous pages and
54 * map them into the kernel's vmalloc() space. This is done so we
55 * get unique mapping for these pages, outside of the kernel's 1:1
56 * virtual:physical mapping. This is necessary so we can cover large
57 * portions of the kernel with single large page TLB entries, and
58 * still get unique uncached pages for consistent DMA.
60 void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
63 struct page *page, *free, *end;
65 unsigned long flags, offset;
66 struct vm_struct *area = NULL;
69 BUG_ON(in_interrupt());
71 /* Only allocate page size areas */
72 size = PAGE_ALIGN(size);
73 order = get_order(size);
75 free = page = alloc_pages(gfp, order);
79 pa = page_to_phys(page);
80 *dma_handle = page_to_bus(page);
81 end = page + (1 << order);
84 * we need to ensure that there are no cachelines in use,
85 * or worse dirty in this area.
87 invalidate_dcache_range((unsigned long)page_address(page),
88 (unsigned long)page_address(page) + size);
91 * alloc_pages() expects the block to be handled as a unit, so
92 * it only sets the page count on the first page. We set the
93 * counts on each page so they can be freed individually
95 for (; page < end; page++)
96 set_page_count(page, 1);
99 /* Allocate some common virtual space to map the new pages*/
100 area = get_vm_area(size, VM_ALLOC);
104 va = (unsigned long) area->addr;
106 flags = _PAGE_KERNEL | _PAGE_NO_CACHE;
108 for (offset = 0; offset < size; offset += PAGE_SIZE) {
109 err = map_page(va+offset, pa+offset, flags);
120 /* Free pages which weren't mapped */
121 for (; free < end; free++) {
129 * free page(s) as defined by the above mapping.
131 void consistent_free(void *vaddr)
133 BUG_ON(in_interrupt());
138 * make an area consistent.
140 void consistent_sync(void *vaddr, size_t size, int direction)
142 unsigned long start = (unsigned long)vaddr;
143 unsigned long end = start + size;
148 case DMA_FROM_DEVICE: /* invalidate only */
149 invalidate_dcache_range(start, end);
151 case DMA_TO_DEVICE: /* writeback only */
152 clean_dcache_range(start, end);
154 case DMA_BIDIRECTIONAL: /* writeback and invalidate */
155 flush_dcache_range(start, end);
161 * consistent_sync_page make a page are consistent. identical
162 * to consistent_sync, but takes a struct page instead of a virtual address
165 void consistent_sync_page(struct page *page, unsigned long offset,
166 size_t size, int direction)
170 start = (unsigned long)page_address(page) + offset;
171 consistent_sync((void *)start, size, direction);
174 EXPORT_SYMBOL(consistent_sync_page);