Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* pci-dma.c: Dynamic DMA mapping support for the FRV CPUs that have MMUs |
| 2 | * |
| 3 | * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/dma-mapping.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/pci.h> |
| 16 | #include <linux/highmem.h> |
Al Viro | 9e6a76b | 2007-10-27 19:23:30 +0100 | [diff] [blame] | 17 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | |
Al Viro | a5da7d3 | 2005-10-21 03:21:18 -0400 | [diff] [blame] | 20 | void *dma_alloc_coherent(struct device *hwdev, size_t size, dma_addr_t *dma_handle, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | void *ret; |
| 23 | |
| 24 | ret = consistent_alloc(gfp, size, dma_handle); |
| 25 | if (ret) |
| 26 | memset(ret, 0, size); |
| 27 | |
| 28 | return ret; |
| 29 | } |
| 30 | |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 31 | EXPORT_SYMBOL(dma_alloc_coherent); |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | void dma_free_coherent(struct device *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle) |
| 34 | { |
| 35 | consistent_free(vaddr); |
| 36 | } |
| 37 | |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 38 | EXPORT_SYMBOL(dma_free_coherent); |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | dma_addr_t dma_map_single(struct device *dev, void *ptr, size_t size, |
| 41 | enum dma_data_direction direction) |
| 42 | { |
Stoyan Gaydarov | db5c444 | 2009-06-11 13:05:04 +0100 | [diff] [blame] | 43 | BUG_ON(direction == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | frv_cache_wback_inv((unsigned long) ptr, (unsigned long) ptr + size); |
| 46 | |
| 47 | return virt_to_bus(ptr); |
| 48 | } |
| 49 | |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 50 | EXPORT_SYMBOL(dma_map_single); |
| 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int dma_map_sg(struct device *dev, struct scatterlist *sg, int nents, |
| 53 | enum dma_data_direction direction) |
| 54 | { |
| 55 | unsigned long dampr2; |
| 56 | void *vaddr; |
| 57 | int i; |
| 58 | |
Stoyan Gaydarov | db5c444 | 2009-06-11 13:05:04 +0100 | [diff] [blame] | 59 | BUG_ON(direction == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | dampr2 = __get_DAMPR(2); |
| 62 | |
| 63 | for (i = 0; i < nents; i++) { |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 64 | vaddr = kmap_atomic_primary(sg_page(&sg[i]), __KM_CACHE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | frv_dcache_writeback((unsigned long) vaddr, |
| 67 | (unsigned long) vaddr + PAGE_SIZE); |
| 68 | |
| 69 | } |
| 70 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 71 | kunmap_atomic_primary(vaddr, __KM_CACHE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | if (dampr2) { |
| 73 | __set_DAMPR(2, dampr2); |
| 74 | __set_IAMPR(2, dampr2); |
| 75 | } |
| 76 | |
| 77 | return nents; |
| 78 | } |
| 79 | |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 80 | EXPORT_SYMBOL(dma_map_sg); |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | dma_addr_t dma_map_page(struct device *dev, struct page *page, unsigned long offset, |
| 83 | size_t size, enum dma_data_direction direction) |
| 84 | { |
| 85 | BUG_ON(direction == DMA_NONE); |
| 86 | flush_dcache_page(page); |
| 87 | return (dma_addr_t) page_to_phys(page) + offset; |
| 88 | } |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 89 | |
| 90 | EXPORT_SYMBOL(dma_map_page); |