PCI: hotplug: Embed hotplug_slot
[linux/fpc-iii.git] / drivers / fpga / dfl-afu-dma-region.c
blob0e81d33af856a6f51751762bd84fd61baebdf162
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Driver for FPGA Accelerated Function Unit (AFU) DMA Region Management
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
7 * Authors:
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>
16 #include "dfl-afu.h"
18 static void put_all_pages(struct page **pages, int npages)
20 int i;
22 for (i = 0; i < npages; i++)
23 if (pages[i])
24 put_page(pages[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;
34 /**
35 * afu_dma_adjust_locked_vm - adjust locked memory
36 * @dev: port device
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;
48 int ret = 0;
50 /* the task is exiting. */
51 if (!current->mm)
52 return 0;
54 down_write(&current->mm->mmap_sem);
56 if (incr) {
57 locked = current->mm->locked_vm + npages;
58 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
60 if (locked > lock_limit && !capable(CAP_IPC_LOCK))
61 ret = -ENOMEM;
62 else
63 current->mm->locked_vm += npages;
64 } else {
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 ? "- execeeded" : "");
75 up_write(&current->mm->mmap_sem);
77 return ret;
80 /**
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;
93 int ret, pinned;
95 ret = afu_dma_adjust_locked_vm(dev, npages, true);
96 if (ret)
97 return ret;
99 region->pages = kcalloc(npages, sizeof(struct page *), GFP_KERNEL);
100 if (!region->pages) {
101 ret = -ENOMEM;
102 goto unlock_vm;
105 pinned = get_user_pages_fast(region->user_addr, npages, 1,
106 region->pages);
107 if (pinned < 0) {
108 ret = pinned;
109 goto put_pages;
110 } else if (pinned != npages) {
111 ret = -EFAULT;
112 goto free_pages;
115 dev_dbg(dev, "%d pages pinned\n", pinned);
117 return 0;
119 put_pages:
120 put_all_pages(region->pages, pinned);
121 free_pages:
122 kfree(region->pages);
123 unlock_vm:
124 afu_dma_adjust_locked_vm(dev, npages, false);
125 return ret;
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;
159 int i;
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]))
164 return false;
166 return true;
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,
179 u64 iova, u64 size)
181 if (!size && region->iova != iova)
182 return false;
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;
208 while (*new) {
209 struct dfl_afu_dma_region *this;
211 this = container_of(*new, struct dfl_afu_dma_region, node);
213 parent = *new;
215 if (dma_region_check_iova(this, region->iova, region->length))
216 return -EEXIST;
218 if (region->iova < this->iova)
219 new = &((*new)->rb_left);
220 else if (region->iova > this->iova)
221 new = &((*new)->rb_right);
222 else
223 return -EEXIST;
226 rb_link_node(&region->node, parent, new);
227 rb_insert_color(&region->node, &afu->dma_regions);
229 return 0;
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)
242 struct dfl_afu *afu;
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(&region->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;
263 while (node) {
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);
271 if (region->iova)
272 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
273 region->iova, region->length,
274 DMA_BIDIRECTIONAL);
276 if (region->pages)
277 afu_dma_unpin_pages(pdata, region);
279 node = rb_next(node);
280 kfree(region);
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;
305 while (node) {
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);
313 return region;
316 if (iova < region->iova)
317 node = node->rb_left;
318 else if (iova > region->iova)
319 node = node->rb_right;
320 else
321 /* the iova region is not fully covered. */
322 break;
325 dev_dbg(dev, "region with iova %llx and size %llx is not found\n",
326 (unsigned long long)iova, (unsigned long long)size);
328 return NULL;
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;
359 int ret;
362 * Check Inputs, only accept page-aligned user memory region with
363 * valid length.
365 if (!PAGE_ALIGNED(user_addr) || !PAGE_ALIGNED(length) || !length)
366 return -EINVAL;
368 /* Check overflow */
369 if (user_addr + length < user_addr)
370 return -EINVAL;
372 if (!access_ok(VERIFY_WRITE, (void __user *)(unsigned long)user_addr,
373 length))
374 return -EINVAL;
376 region = kzalloc(sizeof(*region), GFP_KERNEL);
377 if (!region)
378 return -ENOMEM;
380 region->user_addr = user_addr;
381 region->length = length;
383 /* Pin the user memory region */
384 ret = afu_dma_pin_pages(pdata, region);
385 if (ret) {
386 dev_err(&pdata->dev->dev, "failed to pin memory region\n");
387 goto free_region;
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");
393 ret = -EINVAL;
394 goto unpin_pages;
397 /* As pages are continuous then start to do DMA mapping */
398 region->iova = dma_map_page(dfl_fpga_pdata_to_parent(pdata),
399 region->pages[0], 0,
400 region->length,
401 DMA_BIDIRECTIONAL);
402 if (dma_mapping_error(&pdata->dev->dev, region->iova)) {
403 dev_err(&pdata->dev->dev, "failed to map for dma\n");
404 ret = -EFAULT;
405 goto unpin_pages;
408 *iova = region->iova;
410 mutex_lock(&pdata->lock);
411 ret = afu_dma_region_add(pdata, region);
412 mutex_unlock(&pdata->lock);
413 if (ret) {
414 dev_err(&pdata->dev->dev, "failed to add dma region\n");
415 goto unmap_dma;
418 return 0;
420 unmap_dma:
421 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
422 region->iova, region->length, DMA_BIDIRECTIONAL);
423 unpin_pages:
424 afu_dma_unpin_pages(pdata, region);
425 free_region:
426 kfree(region);
427 return ret;
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);
444 if (!region) {
445 mutex_unlock(&pdata->lock);
446 return -EINVAL;
449 if (region->in_use) {
450 mutex_unlock(&pdata->lock);
451 return -EBUSY;
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);
460 kfree(region);
462 return 0;