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> |
Paul Gortmaker | 9a78da1 | 2012-04-01 16:38:44 -0400 | [diff] [blame] | 16 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/highmem.h> |
Al Viro | 9e6a76b | 2007-10-27 19:23:30 +0100 | [diff] [blame] | 18 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/io.h> |
| 20 | |
Christoph Hellwig | eae0751 | 2016-01-20 15:01:44 -0800 | [diff] [blame] | 21 | static void *frv_dma_alloc(struct device *hwdev, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 22 | dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
| 24 | void *ret; |
| 25 | |
| 26 | ret = consistent_alloc(gfp, size, dma_handle); |
| 27 | if (ret) |
| 28 | memset(ret, 0, size); |
| 29 | |
| 30 | return ret; |
| 31 | } |
| 32 | |
Christoph Hellwig | eae0751 | 2016-01-20 15:01:44 -0800 | [diff] [blame] | 33 | static void frv_dma_free(struct device *hwdev, size_t size, void *vaddr, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 34 | dma_addr_t dma_handle, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | consistent_free(vaddr); |
| 37 | } |
| 38 | |
Christoph Hellwig | eae0751 | 2016-01-20 15:01:44 -0800 | [diff] [blame] | 39 | static int frv_dma_map_sg(struct device *dev, struct scatterlist *sglist, |
| 40 | int nents, enum dma_data_direction direction, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 41 | unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Alexander Duyck | 34f8be7 | 2016-12-14 15:04:43 -0800 | [diff] [blame] | 43 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | unsigned long dampr2; |
| 45 | void *vaddr; |
| 46 | int i; |
| 47 | |
Stoyan Gaydarov | db5c444 | 2009-06-11 13:05:04 +0100 | [diff] [blame] | 48 | BUG_ON(direction == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Alexander Duyck | 34f8be7 | 2016-12-14 15:04:43 -0800 | [diff] [blame] | 50 | if (attrs & DMA_ATTR_SKIP_CPU_SYNC) |
| 51 | return nents; |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | dampr2 = __get_DAMPR(2); |
| 54 | |
Akinobu Mita | 0989e1f | 2015-06-25 15:00:46 -0700 | [diff] [blame] | 55 | for_each_sg(sglist, sg, nents, i) { |
| 56 | vaddr = kmap_atomic_primary(sg_page(sg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | frv_dcache_writeback((unsigned long) vaddr, |
| 59 | (unsigned long) vaddr + PAGE_SIZE); |
| 60 | |
| 61 | } |
| 62 | |
Cong Wang | 144cf86 | 2012-06-27 13:02:49 +0800 | [diff] [blame] | 63 | kunmap_atomic_primary(vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (dampr2) { |
| 65 | __set_DAMPR(2, dampr2); |
| 66 | __set_IAMPR(2, dampr2); |
| 67 | } |
| 68 | |
| 69 | return nents; |
| 70 | } |
| 71 | |
Christoph Hellwig | eae0751 | 2016-01-20 15:01:44 -0800 | [diff] [blame] | 72 | static dma_addr_t frv_dma_map_page(struct device *dev, struct page *page, |
| 73 | unsigned long offset, size_t size, |
Krzysztof Kozlowski | 00085f1 | 2016-08-03 13:46:00 -0700 | [diff] [blame] | 74 | enum dma_data_direction direction, unsigned long attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Alexander Duyck | 34f8be7 | 2016-12-14 15:04:43 -0800 | [diff] [blame] | 76 | if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC)) |
| 77 | flush_dcache_page(page); |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return (dma_addr_t) page_to_phys(page) + offset; |
| 80 | } |
David Howells | 4023440 | 2006-01-08 01:01:19 -0800 | [diff] [blame] | 81 | |
Christoph Hellwig | eae0751 | 2016-01-20 15:01:44 -0800 | [diff] [blame] | 82 | static void frv_dma_sync_single_for_device(struct device *dev, |
| 83 | dma_addr_t dma_handle, size_t size, |
| 84 | enum dma_data_direction direction) |
| 85 | { |
| 86 | flush_write_buffers(); |
| 87 | } |
| 88 | |
| 89 | static void frv_dma_sync_sg_for_device(struct device *dev, |
| 90 | struct scatterlist *sg, int nelems, |
| 91 | enum dma_data_direction direction) |
| 92 | { |
| 93 | flush_write_buffers(); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | static int frv_dma_supported(struct device *dev, u64 mask) |
| 98 | { |
| 99 | /* |
| 100 | * we fall back to GFP_DMA when the mask isn't all 1s, |
| 101 | * so we can't guarantee allocations that must be |
| 102 | * within a tighter range than GFP_DMA.. |
| 103 | */ |
| 104 | if (mask < 0x00ffffff) |
| 105 | return 0; |
| 106 | return 1; |
| 107 | } |
| 108 | |
Bart Van Assche | 5299709 | 2017-01-20 13:04:01 -0800 | [diff] [blame] | 109 | const struct dma_map_ops frv_dma_ops = { |
Christoph Hellwig | eae0751 | 2016-01-20 15:01:44 -0800 | [diff] [blame] | 110 | .alloc = frv_dma_alloc, |
| 111 | .free = frv_dma_free, |
| 112 | .map_page = frv_dma_map_page, |
| 113 | .map_sg = frv_dma_map_sg, |
| 114 | .sync_single_for_device = frv_dma_sync_single_for_device, |
| 115 | .sync_sg_for_device = frv_dma_sync_sg_for_device, |
| 116 | .dma_supported = frv_dma_supported, |
| 117 | }; |
| 118 | EXPORT_SYMBOL(frv_dma_ops); |