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>
18 static void put_all_pages(struct page **pages, int npages)
22 for (i = 0; i < npages; i++)
27 void afu_dma_region_init(struct dfl_feature_platform_data *pdata)
29 struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
31 afu->dma_regions = RB_ROOT;
35 * afu_dma_adjust_locked_vm - adjust locked memory
37 * @npages: number of pages
38 * @incr: increase or decrease locked memory
40 * Increase or decrease the locked memory size with npages input.
42 * Return 0 on success.
43 * Return -ENOMEM if locked memory size is over the limit and no CAP_IPC_LOCK.
45 static int afu_dma_adjust_locked_vm(struct device *dev, long npages, bool incr)
47 unsigned long locked, lock_limit;
50 /* the task is exiting. */
54 down_write(¤t->mm->mmap_sem);
57 locked = current->mm->locked_vm + npages;
58 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
60 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
63 current->mm->locked_vm += npages;
65 if (WARN_ON_ONCE(npages > current->mm->locked_vm))
66 npages = current->mm->locked_vm;
67 current->mm->locked_vm -= npages;
70 dev_dbg(dev, "[%d] RLIMIT_MEMLOCK %c%ld %ld/%ld%s\n", current->pid,
71 incr ? '+' : '-', npages << PAGE_SHIFT,
72 current->mm->locked_vm << PAGE_SHIFT, rlimit(RLIMIT_MEMLOCK),
73 ret ? "- exceeded" : "");
75 up_write(¤t->mm->mmap_sem);
81 * afu_dma_pin_pages - pin pages of given dma memory region
82 * @pdata: feature device platform data
83 * @region: dma memory region to be pinned
85 * Pin all the pages of given dfl_afu_dma_region.
86 * Return 0 for success or negative error code.
88 static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
89 struct dfl_afu_dma_region *region)
91 int npages = region->length >> PAGE_SHIFT;
92 struct device *dev = &pdata->dev->dev;
95 ret = afu_dma_adjust_locked_vm(dev, npages, true);
99 region->pages = kcalloc(npages, sizeof(struct page *), GFP_KERNEL);
100 if (!region->pages) {
105 pinned = get_user_pages_fast(region->user_addr, npages, 1,
110 } else if (pinned != npages) {
115 dev_dbg(dev, "%d pages pinned\n", pinned);
120 put_all_pages(region->pages, pinned);
122 kfree(region->pages);
124 afu_dma_adjust_locked_vm(dev, npages, false);
129 * afu_dma_unpin_pages - unpin pages of given dma memory region
130 * @pdata: feature device platform data
131 * @region: dma memory region to be unpinned
133 * Unpin all the pages of given dfl_afu_dma_region.
134 * Return 0 for success or negative error code.
136 static void afu_dma_unpin_pages(struct dfl_feature_platform_data *pdata,
137 struct dfl_afu_dma_region *region)
139 long npages = region->length >> PAGE_SHIFT;
140 struct device *dev = &pdata->dev->dev;
142 put_all_pages(region->pages, npages);
143 kfree(region->pages);
144 afu_dma_adjust_locked_vm(dev, npages, false);
146 dev_dbg(dev, "%ld pages unpinned\n", npages);
150 * afu_dma_check_continuous_pages - check if pages are continuous
151 * @region: dma memory region
153 * Return true if pages of given dma memory region have continuous physical
154 * address, otherwise return false.
156 static bool afu_dma_check_continuous_pages(struct dfl_afu_dma_region *region)
158 int npages = region->length >> PAGE_SHIFT;
161 for (i = 0; i < npages - 1; i++)
162 if (page_to_pfn(region->pages[i]) + 1 !=
163 page_to_pfn(region->pages[i + 1]))
170 * dma_region_check_iova - check if memory area is fully contained in the region
171 * @region: dma memory region
172 * @iova: address of the dma memory area
173 * @size: size of the dma memory area
175 * Compare the dma memory area defined by @iova and @size with given dma region.
176 * Return true if memory area is fully contained in the region, otherwise false.
178 static bool dma_region_check_iova(struct dfl_afu_dma_region *region,
181 if (!size && region->iova != iova)
184 return (region->iova <= iova) &&
185 (region->length + region->iova >= iova + size);
189 * afu_dma_region_add - add given dma region to rbtree
190 * @pdata: feature device platform data
191 * @region: dma region to be added
193 * Return 0 for success, -EEXIST if dma region has already been added.
195 * Needs to be called with pdata->lock heold.
197 static int afu_dma_region_add(struct dfl_feature_platform_data *pdata,
198 struct dfl_afu_dma_region *region)
200 struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
201 struct rb_node **new, *parent = NULL;
203 dev_dbg(&pdata->dev->dev, "add region (iova = %llx)\n",
204 (unsigned long long)region->iova);
206 new = &afu->dma_regions.rb_node;
209 struct dfl_afu_dma_region *this;
211 this = container_of(*new, struct dfl_afu_dma_region, node);
215 if (dma_region_check_iova(this, region->iova, region->length))
218 if (region->iova < this->iova)
219 new = &((*new)->rb_left);
220 else if (region->iova > this->iova)
221 new = &((*new)->rb_right);
226 rb_link_node(®ion->node, parent, new);
227 rb_insert_color(®ion->node, &afu->dma_regions);
233 * afu_dma_region_remove - remove given dma region from rbtree
234 * @pdata: feature device platform data
235 * @region: dma region to be removed
237 * Needs to be called with pdata->lock heold.
239 static void afu_dma_region_remove(struct dfl_feature_platform_data *pdata,
240 struct dfl_afu_dma_region *region)
244 dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
245 (unsigned long long)region->iova);
247 afu = dfl_fpga_pdata_get_private(pdata);
248 rb_erase(®ion->node, &afu->dma_regions);
252 * afu_dma_region_destroy - destroy all regions in rbtree
253 * @pdata: feature device platform data
255 * Needs to be called with pdata->lock heold.
257 void afu_dma_region_destroy(struct dfl_feature_platform_data *pdata)
259 struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
260 struct rb_node *node = rb_first(&afu->dma_regions);
261 struct dfl_afu_dma_region *region;
264 region = container_of(node, struct dfl_afu_dma_region, node);
266 dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
267 (unsigned long long)region->iova);
269 rb_erase(node, &afu->dma_regions);
272 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
273 region->iova, region->length,
277 afu_dma_unpin_pages(pdata, region);
279 node = rb_next(node);
285 * afu_dma_region_find - find the dma region from rbtree based on iova and size
286 * @pdata: feature device platform data
287 * @iova: address of the dma memory area
288 * @size: size of the dma memory area
290 * It finds the dma region from the rbtree based on @iova and @size:
291 * - if @size == 0, it finds the dma region which starts from @iova
292 * - otherwise, it finds the dma region which fully contains
293 * [@iova, @iova+size)
294 * If nothing is matched returns NULL.
296 * Needs to be called with pdata->lock held.
298 struct dfl_afu_dma_region *
299 afu_dma_region_find(struct dfl_feature_platform_data *pdata, u64 iova, u64 size)
301 struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
302 struct rb_node *node = afu->dma_regions.rb_node;
303 struct device *dev = &pdata->dev->dev;
306 struct dfl_afu_dma_region *region;
308 region = container_of(node, struct dfl_afu_dma_region, node);
310 if (dma_region_check_iova(region, iova, size)) {
311 dev_dbg(dev, "find region (iova = %llx)\n",
312 (unsigned long long)region->iova);
316 if (iova < region->iova)
317 node = node->rb_left;
318 else if (iova > region->iova)
319 node = node->rb_right;
321 /* the iova region is not fully covered. */
325 dev_dbg(dev, "region with iova %llx and size %llx is not found\n",
326 (unsigned long long)iova, (unsigned long long)size);
332 * afu_dma_region_find_iova - find the dma region from rbtree by iova
333 * @pdata: feature device platform data
334 * @iova: address of the dma region
336 * Needs to be called with pdata->lock held.
338 static struct dfl_afu_dma_region *
339 afu_dma_region_find_iova(struct dfl_feature_platform_data *pdata, u64 iova)
341 return afu_dma_region_find(pdata, iova, 0);
345 * afu_dma_map_region - map memory region for dma
346 * @pdata: feature device platform data
347 * @user_addr: address of the memory region
348 * @length: size of the memory region
349 * @iova: pointer of iova address
351 * Map memory region defined by @user_addr and @length, and return dma address
352 * of the memory region via @iova.
353 * Return 0 for success, otherwise error code.
355 int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
356 u64 user_addr, u64 length, u64 *iova)
358 struct dfl_afu_dma_region *region;
362 * Check Inputs, only accept page-aligned user memory region with
365 if (!PAGE_ALIGNED(user_addr) || !PAGE_ALIGNED(length) || !length)
369 if (user_addr + length < user_addr)
372 if (!access_ok((void __user *)(unsigned long)user_addr,
376 region = kzalloc(sizeof(*region), GFP_KERNEL);
380 region->user_addr = user_addr;
381 region->length = length;
383 /* Pin the user memory region */
384 ret = afu_dma_pin_pages(pdata, region);
386 dev_err(&pdata->dev->dev, "failed to pin memory region\n");
390 /* Only accept continuous pages, return error else */
391 if (!afu_dma_check_continuous_pages(region)) {
392 dev_err(&pdata->dev->dev, "pages are not continuous\n");
397 /* As pages are continuous then start to do DMA mapping */
398 region->iova = dma_map_page(dfl_fpga_pdata_to_parent(pdata),
402 if (dma_mapping_error(&pdata->dev->dev, region->iova)) {
403 dev_err(&pdata->dev->dev, "failed to map for dma\n");
408 *iova = region->iova;
410 mutex_lock(&pdata->lock);
411 ret = afu_dma_region_add(pdata, region);
412 mutex_unlock(&pdata->lock);
414 dev_err(&pdata->dev->dev, "failed to add dma region\n");
421 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
422 region->iova, region->length, DMA_BIDIRECTIONAL);
424 afu_dma_unpin_pages(pdata, region);
431 * afu_dma_unmap_region - unmap dma memory region
432 * @pdata: feature device platform data
433 * @iova: dma address of the region
435 * Unmap dma memory region based on @iova.
436 * Return 0 for success, otherwise error code.
438 int afu_dma_unmap_region(struct dfl_feature_platform_data *pdata, u64 iova)
440 struct dfl_afu_dma_region *region;
442 mutex_lock(&pdata->lock);
443 region = afu_dma_region_find_iova(pdata, iova);
445 mutex_unlock(&pdata->lock);
449 if (region->in_use) {
450 mutex_unlock(&pdata->lock);
454 afu_dma_region_remove(pdata, region);
455 mutex_unlock(&pdata->lock);
457 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
458 region->iova, region->length, DMA_BIDIRECTIONAL);
459 afu_dma_unpin_pages(pdata, region);