2 * PowerPC version derived from arch/arm/mm/consistent.c
3 * Copyright (C) 2001 Dan Malek (dmalek@jlc.net)
5 * linux/arch/arm/mm/consistent.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.
14 * Modified for 36-bit support. -Matt
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/kernel.h>
26 #include <linux/errno.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/ptrace.h>
30 #include <linux/mman.h>
32 #include <linux/swap.h>
33 #include <linux/stddef.h>
34 #include <linux/vmalloc.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/bootmem.h>
38 #include <linux/highmem.h>
39 #include <linux/pci.h>
41 #include <asm/pgalloc.h>
44 #include <asm/hardirq.h>
45 #include <asm/mmu_context.h>
46 #include <asm/pgtable.h>
48 #include <asm/uaccess.h>
50 #include <asm/machdep.h>
54 extern int map_page(unsigned long va, phys_addr_t pa, int flags);
56 /* This function will allocate the requested contiguous pages and
57 * map them into the kernel's vmalloc() space. This is done so we
58 * get unique mapping for these pages, outside of the kernel's 1:1
59 * virtual:physical mapping. This is necessary so we can cover large
60 * portions of the kernel with single large page TLB entries, and
61 * still get unique uncached pages for consistent DMA.
63 void *consistent_alloc(int gfp, size_t size, dma_addr_t *dma_handle)
66 unsigned long page, va, flags;
68 struct vm_struct *area;
74 /* Only allocate page size areas.
76 size = PAGE_ALIGN(size);
77 order = get_order(size);
79 page = __get_free_pages(gfp, order);
86 * we need to ensure that there are no cachelines in use,
87 * or worse dirty in this area.
89 invalidate_dcache_range(page, page + size);
91 /* Allocate some common virtual space to map the new pages.
93 area = get_vm_area(size, VM_ALLOC);
95 free_pages(page, order);
98 va = VMALLOC_VMADDR(area->addr);
101 /* This gives us the real physical address of the first page.
103 *dma_handle = pa = virt_to_bus((void *)page);
105 flags = _PAGE_KERNEL | _PAGE_NO_CACHE;
108 * Set refcount=1 on all pages in an order>0
109 * allocation so that vfree() will actually
110 * free all pages that were allocated.
114 struct page *rpage = virt_to_page(page);
115 for (i = 1; i < (1 << order); i++)
116 set_page_count(rpage+i, 1);
120 for (i = 0; i < size && err == 0; i += PAGE_SIZE)
121 err = map_page(va+i, pa+i, flags);
132 * free page(s) as defined by the above mapping.
134 void consistent_free(void *vaddr)
142 * make an area consistent.
144 void consistent_sync(void *vaddr, size_t size, int direction)
146 unsigned long start = (unsigned long)vaddr;
147 unsigned long end = start + size;
152 case PCI_DMA_FROMDEVICE: /* invalidate only */
153 invalidate_dcache_range(start, end);
155 case PCI_DMA_TODEVICE: /* writeback only */
156 clean_dcache_range(start, end);
158 case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */
159 flush_dcache_range(start, end);
165 * consistent_sync_page make a page are consistent. identical
166 * to consistent_sync, but takes a struct page instead of a virtual address
169 void consistent_sync_page(struct page *page, unsigned long offset,
170 size_t size, int direction)
174 start = page_address(page) + offset;
175 consistent_sync(start, size, direction);