Merge tag 'drm-vc4-fixes-2016-09-14' of https://github.com/anholt/linux into drm...
[linux-2.6-block.git] / drivers / infiniband / hw / hfi1 / dma.c
CommitLineData
77241056 1/*
05d6ac1d 2 * Copyright(c) 2015, 2016 Intel Corporation.
77241056
MM
3 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
77241056
MM
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
77241056
MM
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47#include <linux/types.h>
48#include <linux/scatterlist.h>
49
50#include "verbs.h"
51
50e5dcbe 52#define BAD_DMA_ADDRESS ((u64)0)
77241056
MM
53
54/*
55 * The following functions implement driver specific replacements
56 * for the ib_dma_*() functions.
57 *
58 * These functions return kernel virtual addresses instead of
59 * device bus addresses since the driver uses the CPU to copy
60 * data instead of using hardware DMA.
61 */
62
63static int hfi1_mapping_error(struct ib_device *dev, u64 dma_addr)
64{
65 return dma_addr == BAD_DMA_ADDRESS;
66}
67
68static u64 hfi1_dma_map_single(struct ib_device *dev, void *cpu_addr,
69 size_t size, enum dma_data_direction direction)
70{
71 if (WARN_ON(!valid_dma_direction(direction)))
72 return BAD_DMA_ADDRESS;
73
50e5dcbe 74 return (u64)cpu_addr;
77241056
MM
75}
76
77static void hfi1_dma_unmap_single(struct ib_device *dev, u64 addr, size_t size,
78 enum dma_data_direction direction)
79{
80 /* This is a stub, nothing to be done here */
81}
82
83static u64 hfi1_dma_map_page(struct ib_device *dev, struct page *page,
84 unsigned long offset, size_t size,
85 enum dma_data_direction direction)
86{
87 u64 addr;
88
89 if (WARN_ON(!valid_dma_direction(direction)))
90 return BAD_DMA_ADDRESS;
91
92 if (offset + size > PAGE_SIZE)
93 return BAD_DMA_ADDRESS;
94
50e5dcbe 95 addr = (u64)page_address(page);
77241056
MM
96 if (addr)
97 addr += offset;
98
99 return addr;
100}
101
102static void hfi1_dma_unmap_page(struct ib_device *dev, u64 addr, size_t size,
103 enum dma_data_direction direction)
104{
105 /* This is a stub, nothing to be done here */
106}
107
108static int hfi1_map_sg(struct ib_device *dev, struct scatterlist *sgl,
109 int nents, enum dma_data_direction direction)
110{
111 struct scatterlist *sg;
112 u64 addr;
113 int i;
114 int ret = nents;
115
116 if (WARN_ON(!valid_dma_direction(direction)))
117 return BAD_DMA_ADDRESS;
118
119 for_each_sg(sgl, sg, nents, i) {
50e5dcbe 120 addr = (u64)page_address(sg_page(sg));
77241056
MM
121 if (!addr) {
122 ret = 0;
123 break;
124 }
125 sg->dma_address = addr + sg->offset;
126#ifdef CONFIG_NEED_SG_DMA_LENGTH
127 sg->dma_length = sg->length;
128#endif
129 }
130 return ret;
131}
132
133static void hfi1_unmap_sg(struct ib_device *dev,
134 struct scatterlist *sg, int nents,
135 enum dma_data_direction direction)
136{
137 /* This is a stub, nothing to be done here */
138}
139
140static void hfi1_sync_single_for_cpu(struct ib_device *dev, u64 addr,
141 size_t size, enum dma_data_direction dir)
142{
143}
144
145static void hfi1_sync_single_for_device(struct ib_device *dev, u64 addr,
146 size_t size,
147 enum dma_data_direction dir)
148{
149}
150
151static void *hfi1_dma_alloc_coherent(struct ib_device *dev, size_t size,
152 u64 *dma_handle, gfp_t flag)
153{
154 struct page *p;
155 void *addr = NULL;
156
157 p = alloc_pages(flag, get_order(size));
158 if (p)
159 addr = page_address(p);
160 if (dma_handle)
50e5dcbe 161 *dma_handle = (u64)addr;
77241056
MM
162 return addr;
163}
164
165static void hfi1_dma_free_coherent(struct ib_device *dev, size_t size,
166 void *cpu_addr, u64 dma_handle)
167{
50e5dcbe 168 free_pages((unsigned long)cpu_addr, get_order(size));
77241056
MM
169}
170
171struct ib_dma_mapping_ops hfi1_dma_mapping_ops = {
172 .mapping_error = hfi1_mapping_error,
173 .map_single = hfi1_dma_map_single,
174 .unmap_single = hfi1_dma_unmap_single,
175 .map_page = hfi1_dma_map_page,
176 .unmap_page = hfi1_dma_unmap_page,
177 .map_sg = hfi1_map_sg,
178 .unmap_sg = hfi1_unmap_sg,
179 .sync_single_for_cpu = hfi1_sync_single_for_cpu,
180 .sync_single_for_device = hfi1_sync_single_for_device,
181 .alloc_coherent = hfi1_dma_alloc_coherent,
182 .free_coherent = hfi1_dma_free_coherent
183};