/* dma-alloc.c: consistent DMA memory allocation1*2* Derived from arch/ppc/mm/cachemap.c3*4* PowerPC version derived from arch/arm/mm/consistent.c5* Copyright (C) 2001 Dan Malek ([email protected])6*7* linux/arch/arm/mm/consistent.c8*9* Copyright (C) 2000 Russell King10*11* Consistent memory allocators. Used for DMA devices that want to12* share uncached memory with the processor core. The function return13* is the virtual address and 'dma_handle' is the physical address.14* Mostly stolen from the ARM port, with some changes for PowerPC.15* -- Dan16* Modified for 36-bit support. -Matt17*18* This program is free software; you can redistribute it and/or modify19* it under the terms of the GNU General Public License version 2 as20* published by the Free Software Foundation.21*/2223#include <linux/module.h>24#include <linux/signal.h>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/ptrace.h>31#include <linux/mman.h>32#include <linux/mm.h>33#include <linux/swap.h>34#include <linux/stddef.h>35#include <linux/vmalloc.h>36#include <linux/init.h>37#include <linux/pci.h>38#include <linux/hardirq.h>39#include <linux/gfp.h>4041#include <asm/pgalloc.h>42#include <asm/io.h>43#include <asm/mmu_context.h>44#include <asm/pgtable.h>45#include <asm/mmu.h>46#include <asm/uaccess.h>47#include <asm/smp.h>4849static int map_page(unsigned long va, unsigned long pa, pgprot_t prot)50{51pgd_t *pge;52pud_t *pue;53pmd_t *pme;54pte_t *pte;55int err = -ENOMEM;5657/* Use upper 10 bits of VA to index the first level map */58pge = pgd_offset_k(va);59pue = pud_offset(pge, va);60pme = pmd_offset(pue, va);6162/* Use middle 10 bits of VA to index the second-level map */63pte = pte_alloc_kernel(pme, va);64if (pte != 0) {65err = 0;66set_pte(pte, mk_pte_phys(pa & PAGE_MASK, prot));67}6869return err;70}7172/*73* This function will allocate the requested contiguous pages and74* map them into the kernel's vmalloc() space. This is done so we75* get unique mapping for these pages, outside of the kernel's 1:176* virtual:physical mapping. This is necessary so we can cover large77* portions of the kernel with single large page TLB entries, and78* still get unique uncached pages for consistent DMA.79*/80void *consistent_alloc(gfp_t gfp, size_t size, dma_addr_t *dma_handle)81{82struct vm_struct *area;83unsigned long page, va, pa;84void *ret;85int order, err, i;8687if (in_interrupt())88BUG();8990/* only allocate page size areas */91size = PAGE_ALIGN(size);92order = get_order(size);9394page = __get_free_pages(gfp, order);95if (!page) {96BUG();97return NULL;98}99100/* allocate some common virtual space to map the new pages */101area = get_vm_area(size, VM_ALLOC);102if (area == 0) {103free_pages(page, order);104return NULL;105}106va = VMALLOC_VMADDR(area->addr);107ret = (void *) va;108109/* this gives us the real physical address of the first page */110*dma_handle = pa = virt_to_bus((void *) page);111112/* set refcount=1 on all pages in an order>0 allocation so that vfree() will actually free113* all pages that were allocated.114*/115if (order > 0) {116struct page *rpage = virt_to_page(page);117split_page(rpage, order);118}119120err = 0;121for (i = 0; i < size && err == 0; i += PAGE_SIZE)122err = map_page(va + i, pa + i, PAGE_KERNEL_NOCACHE);123124if (err) {125vfree((void *) va);126return NULL;127}128129/* we need to ensure that there are no cachelines in use, or worse dirty in this area130* - can't do until after virtual address mappings are created131*/132frv_cache_invalidate(va, va + size);133134return ret;135}136137/*138* free page(s) as defined by the above mapping.139*/140void consistent_free(void *vaddr)141{142if (in_interrupt())143BUG();144vfree(vaddr);145}146147/*148* make an area consistent.149*/150void consistent_sync(void *vaddr, size_t size, int direction)151{152unsigned long start = (unsigned long) vaddr;153unsigned long end = start + size;154155switch (direction) {156case PCI_DMA_NONE:157BUG();158case PCI_DMA_FROMDEVICE: /* invalidate only */159frv_cache_invalidate(start, end);160break;161case PCI_DMA_TODEVICE: /* writeback only */162frv_dcache_writeback(start, end);163break;164case PCI_DMA_BIDIRECTIONAL: /* writeback and invalidate */165frv_dcache_writeback(start, end);166break;167}168}169170/*171* consistent_sync_page make a page are consistent. identical172* to consistent_sync, but takes a struct page instead of a virtual address173*/174175void consistent_sync_page(struct page *page, unsigned long offset,176size_t size, int direction)177{178void *start;179180start = page_address(page) + offset;181consistent_sync(start, size, direction);182}183184185