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 void afu_dma_region_init(struct dfl_feature_platform_data
*pdata
)
21 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
23 afu
->dma_regions
= RB_ROOT
;
27 * afu_dma_pin_pages - pin pages of given dma memory region
28 * @pdata: feature device platform data
29 * @region: dma memory region to be pinned
31 * Pin all the pages of given dfl_afu_dma_region.
32 * Return 0 for success or negative error code.
34 static int afu_dma_pin_pages(struct dfl_feature_platform_data
*pdata
,
35 struct dfl_afu_dma_region
*region
)
37 int npages
= region
->length
>> PAGE_SHIFT
;
38 struct device
*dev
= &pdata
->dev
->dev
;
41 ret
= account_locked_vm(current
->mm
, npages
, true);
45 region
->pages
= kcalloc(npages
, sizeof(struct page
*), GFP_KERNEL
);
51 pinned
= pin_user_pages_fast(region
->user_addr
, npages
, FOLL_WRITE
,
56 } else if (pinned
!= npages
) {
61 dev_dbg(dev
, "%d pages pinned\n", pinned
);
66 unpin_user_pages(region
->pages
, pinned
);
70 account_locked_vm(current
->mm
, npages
, false);
75 * afu_dma_unpin_pages - unpin pages of given dma memory region
76 * @pdata: feature device platform data
77 * @region: dma memory region to be unpinned
79 * Unpin all the pages of given dfl_afu_dma_region.
80 * Return 0 for success or negative error code.
82 static void afu_dma_unpin_pages(struct dfl_feature_platform_data
*pdata
,
83 struct dfl_afu_dma_region
*region
)
85 long npages
= region
->length
>> PAGE_SHIFT
;
86 struct device
*dev
= &pdata
->dev
->dev
;
88 unpin_user_pages(region
->pages
, npages
);
90 account_locked_vm(current
->mm
, npages
, false);
92 dev_dbg(dev
, "%ld pages unpinned\n", npages
);
96 * afu_dma_check_continuous_pages - check if pages are continuous
97 * @region: dma memory region
99 * Return true if pages of given dma memory region have continuous physical
100 * address, otherwise return false.
102 static bool afu_dma_check_continuous_pages(struct dfl_afu_dma_region
*region
)
104 int npages
= region
->length
>> PAGE_SHIFT
;
107 for (i
= 0; i
< npages
- 1; i
++)
108 if (page_to_pfn(region
->pages
[i
]) + 1 !=
109 page_to_pfn(region
->pages
[i
+ 1]))
116 * dma_region_check_iova - check if memory area is fully contained in the region
117 * @region: dma memory region
118 * @iova: address of the dma memory area
119 * @size: size of the dma memory area
121 * Compare the dma memory area defined by @iova and @size with given dma region.
122 * Return true if memory area is fully contained in the region, otherwise false.
124 static bool dma_region_check_iova(struct dfl_afu_dma_region
*region
,
127 if (!size
&& region
->iova
!= iova
)
130 return (region
->iova
<= iova
) &&
131 (region
->length
+ region
->iova
>= iova
+ size
);
135 * afu_dma_region_add - add given dma region to rbtree
136 * @pdata: feature device platform data
137 * @region: dma region to be added
139 * Return 0 for success, -EEXIST if dma region has already been added.
141 * Needs to be called with pdata->lock heold.
143 static int afu_dma_region_add(struct dfl_feature_platform_data
*pdata
,
144 struct dfl_afu_dma_region
*region
)
146 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
147 struct rb_node
**new, *parent
= NULL
;
149 dev_dbg(&pdata
->dev
->dev
, "add region (iova = %llx)\n",
150 (unsigned long long)region
->iova
);
152 new = &afu
->dma_regions
.rb_node
;
155 struct dfl_afu_dma_region
*this;
157 this = container_of(*new, struct dfl_afu_dma_region
, node
);
161 if (dma_region_check_iova(this, region
->iova
, region
->length
))
164 if (region
->iova
< this->iova
)
165 new = &((*new)->rb_left
);
166 else if (region
->iova
> this->iova
)
167 new = &((*new)->rb_right
);
172 rb_link_node(®ion
->node
, parent
, new);
173 rb_insert_color(®ion
->node
, &afu
->dma_regions
);
179 * afu_dma_region_remove - remove given dma region from rbtree
180 * @pdata: feature device platform data
181 * @region: dma region to be removed
183 * Needs to be called with pdata->lock heold.
185 static void afu_dma_region_remove(struct dfl_feature_platform_data
*pdata
,
186 struct dfl_afu_dma_region
*region
)
190 dev_dbg(&pdata
->dev
->dev
, "del region (iova = %llx)\n",
191 (unsigned long long)region
->iova
);
193 afu
= dfl_fpga_pdata_get_private(pdata
);
194 rb_erase(®ion
->node
, &afu
->dma_regions
);
198 * afu_dma_region_destroy - destroy all regions in rbtree
199 * @pdata: feature device platform data
201 * Needs to be called with pdata->lock heold.
203 void afu_dma_region_destroy(struct dfl_feature_platform_data
*pdata
)
205 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
206 struct rb_node
*node
= rb_first(&afu
->dma_regions
);
207 struct dfl_afu_dma_region
*region
;
210 region
= container_of(node
, struct dfl_afu_dma_region
, node
);
212 dev_dbg(&pdata
->dev
->dev
, "del region (iova = %llx)\n",
213 (unsigned long long)region
->iova
);
215 rb_erase(node
, &afu
->dma_regions
);
218 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata
),
219 region
->iova
, region
->length
,
223 afu_dma_unpin_pages(pdata
, region
);
225 node
= rb_next(node
);
231 * afu_dma_region_find - find the dma region from rbtree based on iova and size
232 * @pdata: feature device platform data
233 * @iova: address of the dma memory area
234 * @size: size of the dma memory area
236 * It finds the dma region from the rbtree based on @iova and @size:
237 * - if @size == 0, it finds the dma region which starts from @iova
238 * - otherwise, it finds the dma region which fully contains
239 * [@iova, @iova+size)
240 * If nothing is matched returns NULL.
242 * Needs to be called with pdata->lock held.
244 struct dfl_afu_dma_region
*
245 afu_dma_region_find(struct dfl_feature_platform_data
*pdata
, u64 iova
, u64 size
)
247 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
248 struct rb_node
*node
= afu
->dma_regions
.rb_node
;
249 struct device
*dev
= &pdata
->dev
->dev
;
252 struct dfl_afu_dma_region
*region
;
254 region
= container_of(node
, struct dfl_afu_dma_region
, node
);
256 if (dma_region_check_iova(region
, iova
, size
)) {
257 dev_dbg(dev
, "find region (iova = %llx)\n",
258 (unsigned long long)region
->iova
);
262 if (iova
< region
->iova
)
263 node
= node
->rb_left
;
264 else if (iova
> region
->iova
)
265 node
= node
->rb_right
;
267 /* the iova region is not fully covered. */
271 dev_dbg(dev
, "region with iova %llx and size %llx is not found\n",
272 (unsigned long long)iova
, (unsigned long long)size
);
278 * afu_dma_region_find_iova - find the dma region from rbtree by iova
279 * @pdata: feature device platform data
280 * @iova: address of the dma region
282 * Needs to be called with pdata->lock held.
284 static struct dfl_afu_dma_region
*
285 afu_dma_region_find_iova(struct dfl_feature_platform_data
*pdata
, u64 iova
)
287 return afu_dma_region_find(pdata
, iova
, 0);
291 * afu_dma_map_region - map memory region for dma
292 * @pdata: feature device platform data
293 * @user_addr: address of the memory region
294 * @length: size of the memory region
295 * @iova: pointer of iova address
297 * Map memory region defined by @user_addr and @length, and return dma address
298 * of the memory region via @iova.
299 * Return 0 for success, otherwise error code.
301 int afu_dma_map_region(struct dfl_feature_platform_data
*pdata
,
302 u64 user_addr
, u64 length
, u64
*iova
)
304 struct dfl_afu_dma_region
*region
;
308 * Check Inputs, only accept page-aligned user memory region with
311 if (!PAGE_ALIGNED(user_addr
) || !PAGE_ALIGNED(length
) || !length
)
315 if (user_addr
+ length
< user_addr
)
318 region
= kzalloc(sizeof(*region
), GFP_KERNEL
);
322 region
->user_addr
= user_addr
;
323 region
->length
= length
;
325 /* Pin the user memory region */
326 ret
= afu_dma_pin_pages(pdata
, region
);
328 dev_err(&pdata
->dev
->dev
, "failed to pin memory region\n");
332 /* Only accept continuous pages, return error else */
333 if (!afu_dma_check_continuous_pages(region
)) {
334 dev_err(&pdata
->dev
->dev
, "pages are not continuous\n");
339 /* As pages are continuous then start to do DMA mapping */
340 region
->iova
= dma_map_page(dfl_fpga_pdata_to_parent(pdata
),
344 if (dma_mapping_error(dfl_fpga_pdata_to_parent(pdata
), region
->iova
)) {
345 dev_err(&pdata
->dev
->dev
, "failed to map for dma\n");
350 *iova
= region
->iova
;
352 mutex_lock(&pdata
->lock
);
353 ret
= afu_dma_region_add(pdata
, region
);
354 mutex_unlock(&pdata
->lock
);
356 dev_err(&pdata
->dev
->dev
, "failed to add dma region\n");
363 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata
),
364 region
->iova
, region
->length
, DMA_BIDIRECTIONAL
);
366 afu_dma_unpin_pages(pdata
, region
);
373 * afu_dma_unmap_region - unmap dma memory region
374 * @pdata: feature device platform data
375 * @iova: dma address of the region
377 * Unmap dma memory region based on @iova.
378 * Return 0 for success, otherwise error code.
380 int afu_dma_unmap_region(struct dfl_feature_platform_data
*pdata
, u64 iova
)
382 struct dfl_afu_dma_region
*region
;
384 mutex_lock(&pdata
->lock
);
385 region
= afu_dma_region_find_iova(pdata
, iova
);
387 mutex_unlock(&pdata
->lock
);
391 if (region
->in_use
) {
392 mutex_unlock(&pdata
->lock
);
396 afu_dma_region_remove(pdata
, region
);
397 mutex_unlock(&pdata
->lock
);
399 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata
),
400 region
->iova
, region
->length
, DMA_BIDIRECTIONAL
);
401 afu_dma_unpin_pages(pdata
, region
);