1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for FPGA Accelerated Function Unit (AFU) DMA Region Management
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
8 * Wu Hao <hao.wu@intel.com>
9 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
12 #include <linux/dma-mapping.h>
13 #include <linux/sched/signal.h>
14 #include <linux/uaccess.h>
19 static void put_all_pages(struct page
**pages
, int npages
)
23 for (i
= 0; i
< npages
; i
++)
28 void afu_dma_region_init(struct dfl_feature_platform_data
*pdata
)
30 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
32 afu
->dma_regions
= RB_ROOT
;
36 * afu_dma_pin_pages - pin pages of given dma memory region
37 * @pdata: feature device platform data
38 * @region: dma memory region to be pinned
40 * Pin all the pages of given dfl_afu_dma_region.
41 * Return 0 for success or negative error code.
43 static int afu_dma_pin_pages(struct dfl_feature_platform_data
*pdata
,
44 struct dfl_afu_dma_region
*region
)
46 int npages
= region
->length
>> PAGE_SHIFT
;
47 struct device
*dev
= &pdata
->dev
->dev
;
50 ret
= account_locked_vm(current
->mm
, npages
, true);
54 region
->pages
= kcalloc(npages
, sizeof(struct page
*), GFP_KERNEL
);
60 pinned
= get_user_pages_fast(region
->user_addr
, npages
, FOLL_WRITE
,
65 } else if (pinned
!= npages
) {
70 dev_dbg(dev
, "%d pages pinned\n", pinned
);
75 put_all_pages(region
->pages
, pinned
);
79 account_locked_vm(current
->mm
, npages
, false);
84 * afu_dma_unpin_pages - unpin pages of given dma memory region
85 * @pdata: feature device platform data
86 * @region: dma memory region to be unpinned
88 * Unpin all the pages of given dfl_afu_dma_region.
89 * Return 0 for success or negative error code.
91 static void afu_dma_unpin_pages(struct dfl_feature_platform_data
*pdata
,
92 struct dfl_afu_dma_region
*region
)
94 long npages
= region
->length
>> PAGE_SHIFT
;
95 struct device
*dev
= &pdata
->dev
->dev
;
97 put_all_pages(region
->pages
, npages
);
99 account_locked_vm(current
->mm
, npages
, false);
101 dev_dbg(dev
, "%ld pages unpinned\n", npages
);
105 * afu_dma_check_continuous_pages - check if pages are continuous
106 * @region: dma memory region
108 * Return true if pages of given dma memory region have continuous physical
109 * address, otherwise return false.
111 static bool afu_dma_check_continuous_pages(struct dfl_afu_dma_region
*region
)
113 int npages
= region
->length
>> PAGE_SHIFT
;
116 for (i
= 0; i
< npages
- 1; i
++)
117 if (page_to_pfn(region
->pages
[i
]) + 1 !=
118 page_to_pfn(region
->pages
[i
+ 1]))
125 * dma_region_check_iova - check if memory area is fully contained in the region
126 * @region: dma memory region
127 * @iova: address of the dma memory area
128 * @size: size of the dma memory area
130 * Compare the dma memory area defined by @iova and @size with given dma region.
131 * Return true if memory area is fully contained in the region, otherwise false.
133 static bool dma_region_check_iova(struct dfl_afu_dma_region
*region
,
136 if (!size
&& region
->iova
!= iova
)
139 return (region
->iova
<= iova
) &&
140 (region
->length
+ region
->iova
>= iova
+ size
);
144 * afu_dma_region_add - add given dma region to rbtree
145 * @pdata: feature device platform data
146 * @region: dma region to be added
148 * Return 0 for success, -EEXIST if dma region has already been added.
150 * Needs to be called with pdata->lock heold.
152 static int afu_dma_region_add(struct dfl_feature_platform_data
*pdata
,
153 struct dfl_afu_dma_region
*region
)
155 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
156 struct rb_node
**new, *parent
= NULL
;
158 dev_dbg(&pdata
->dev
->dev
, "add region (iova = %llx)\n",
159 (unsigned long long)region
->iova
);
161 new = &afu
->dma_regions
.rb_node
;
164 struct dfl_afu_dma_region
*this;
166 this = container_of(*new, struct dfl_afu_dma_region
, node
);
170 if (dma_region_check_iova(this, region
->iova
, region
->length
))
173 if (region
->iova
< this->iova
)
174 new = &((*new)->rb_left
);
175 else if (region
->iova
> this->iova
)
176 new = &((*new)->rb_right
);
181 rb_link_node(®ion
->node
, parent
, new);
182 rb_insert_color(®ion
->node
, &afu
->dma_regions
);
188 * afu_dma_region_remove - remove given dma region from rbtree
189 * @pdata: feature device platform data
190 * @region: dma region to be removed
192 * Needs to be called with pdata->lock heold.
194 static void afu_dma_region_remove(struct dfl_feature_platform_data
*pdata
,
195 struct dfl_afu_dma_region
*region
)
199 dev_dbg(&pdata
->dev
->dev
, "del region (iova = %llx)\n",
200 (unsigned long long)region
->iova
);
202 afu
= dfl_fpga_pdata_get_private(pdata
);
203 rb_erase(®ion
->node
, &afu
->dma_regions
);
207 * afu_dma_region_destroy - destroy all regions in rbtree
208 * @pdata: feature device platform data
210 * Needs to be called with pdata->lock heold.
212 void afu_dma_region_destroy(struct dfl_feature_platform_data
*pdata
)
214 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
215 struct rb_node
*node
= rb_first(&afu
->dma_regions
);
216 struct dfl_afu_dma_region
*region
;
219 region
= container_of(node
, struct dfl_afu_dma_region
, node
);
221 dev_dbg(&pdata
->dev
->dev
, "del region (iova = %llx)\n",
222 (unsigned long long)region
->iova
);
224 rb_erase(node
, &afu
->dma_regions
);
227 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata
),
228 region
->iova
, region
->length
,
232 afu_dma_unpin_pages(pdata
, region
);
234 node
= rb_next(node
);
240 * afu_dma_region_find - find the dma region from rbtree based on iova and size
241 * @pdata: feature device platform data
242 * @iova: address of the dma memory area
243 * @size: size of the dma memory area
245 * It finds the dma region from the rbtree based on @iova and @size:
246 * - if @size == 0, it finds the dma region which starts from @iova
247 * - otherwise, it finds the dma region which fully contains
248 * [@iova, @iova+size)
249 * If nothing is matched returns NULL.
251 * Needs to be called with pdata->lock held.
253 struct dfl_afu_dma_region
*
254 afu_dma_region_find(struct dfl_feature_platform_data
*pdata
, u64 iova
, u64 size
)
256 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
257 struct rb_node
*node
= afu
->dma_regions
.rb_node
;
258 struct device
*dev
= &pdata
->dev
->dev
;
261 struct dfl_afu_dma_region
*region
;
263 region
= container_of(node
, struct dfl_afu_dma_region
, node
);
265 if (dma_region_check_iova(region
, iova
, size
)) {
266 dev_dbg(dev
, "find region (iova = %llx)\n",
267 (unsigned long long)region
->iova
);
271 if (iova
< region
->iova
)
272 node
= node
->rb_left
;
273 else if (iova
> region
->iova
)
274 node
= node
->rb_right
;
276 /* the iova region is not fully covered. */
280 dev_dbg(dev
, "region with iova %llx and size %llx is not found\n",
281 (unsigned long long)iova
, (unsigned long long)size
);
287 * afu_dma_region_find_iova - find the dma region from rbtree by iova
288 * @pdata: feature device platform data
289 * @iova: address of the dma region
291 * Needs to be called with pdata->lock held.
293 static struct dfl_afu_dma_region
*
294 afu_dma_region_find_iova(struct dfl_feature_platform_data
*pdata
, u64 iova
)
296 return afu_dma_region_find(pdata
, iova
, 0);
300 * afu_dma_map_region - map memory region for dma
301 * @pdata: feature device platform data
302 * @user_addr: address of the memory region
303 * @length: size of the memory region
304 * @iova: pointer of iova address
306 * Map memory region defined by @user_addr and @length, and return dma address
307 * of the memory region via @iova.
308 * Return 0 for success, otherwise error code.
310 int afu_dma_map_region(struct dfl_feature_platform_data
*pdata
,
311 u64 user_addr
, u64 length
, u64
*iova
)
313 struct dfl_afu_dma_region
*region
;
317 * Check Inputs, only accept page-aligned user memory region with
320 if (!PAGE_ALIGNED(user_addr
) || !PAGE_ALIGNED(length
) || !length
)
324 if (user_addr
+ length
< user_addr
)
327 if (!access_ok((void __user
*)(unsigned long)user_addr
,
331 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
335 region
->user_addr
= user_addr
;
336 region
->length
= length
;
338 /* Pin the user memory region */
339 ret
= afu_dma_pin_pages(pdata
, region
);
341 dev_err(&pdata
->dev
->dev
, "failed to pin memory region\n");
345 /* Only accept continuous pages, return error else */
346 if (!afu_dma_check_continuous_pages(region
)) {
347 dev_err(&pdata
->dev
->dev
, "pages are not continuous\n");
352 /* As pages are continuous then start to do DMA mapping */
353 region
->iova
= dma_map_page(dfl_fpga_pdata_to_parent(pdata
),
357 if (dma_mapping_error(dfl_fpga_pdata_to_parent(pdata
), region
->iova
)) {
358 dev_err(&pdata
->dev
->dev
, "failed to map for dma\n");
363 *iova
= region
->iova
;
365 mutex_lock(&pdata
->lock
);
366 ret
= afu_dma_region_add(pdata
, region
);
367 mutex_unlock(&pdata
->lock
);
369 dev_err(&pdata
->dev
->dev
, "failed to add dma region\n");
376 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata
),
377 region
->iova
, region
->length
, DMA_BIDIRECTIONAL
);
379 afu_dma_unpin_pages(pdata
, region
);
386 * afu_dma_unmap_region - unmap dma memory region
387 * @pdata: feature device platform data
388 * @iova: dma address of the region
390 * Unmap dma memory region based on @iova.
391 * Return 0 for success, otherwise error code.
393 int afu_dma_unmap_region(struct dfl_feature_platform_data
*pdata
, u64 iova
)
395 struct dfl_afu_dma_region
*region
;
397 mutex_lock(&pdata
->lock
);
398 region
= afu_dma_region_find_iova(pdata
, iova
);
400 mutex_unlock(&pdata
->lock
);
404 if (region
->in_use
) {
405 mutex_unlock(&pdata
->lock
);
409 afu_dma_region_remove(pdata
, region
);
410 mutex_unlock(&pdata
->lock
);
412 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata
),
413 region
->iova
, region
->length
, DMA_BIDIRECTIONAL
);
414 afu_dma_unpin_pages(pdata
, region
);