treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / fpga / dfl-afu-dma-region.c
blob62f924489db593ff1aeb32dcd86ed8f18619ee71
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>
15 #include <linux/mm.h>
17 #include "dfl-afu.h"
19 static void put_all_pages(struct page **pages, int npages)
21 int i;
23 for (i = 0; i < npages; i++)
24 if (pages[i])
25 put_page(pages[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;
35 /**
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;
48 int ret, pinned;
50 ret = account_locked_vm(current->mm, npages, true);
51 if (ret)
52 return ret;
54 region->pages = kcalloc(npages, sizeof(struct page *), GFP_KERNEL);
55 if (!region->pages) {
56 ret = -ENOMEM;
57 goto unlock_vm;
60 pinned = get_user_pages_fast(region->user_addr, npages, FOLL_WRITE,
61 region->pages);
62 if (pinned < 0) {
63 ret = pinned;
64 goto put_pages;
65 } else if (pinned != npages) {
66 ret = -EFAULT;
67 goto free_pages;
70 dev_dbg(dev, "%d pages pinned\n", pinned);
72 return 0;
74 put_pages:
75 put_all_pages(region->pages, pinned);
76 free_pages:
77 kfree(region->pages);
78 unlock_vm:
79 account_locked_vm(current->mm, npages, false);
80 return ret;
83 /**
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);
98 kfree(region->pages);
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;
114 int i;
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]))
119 return false;
121 return true;
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,
134 u64 iova, u64 size)
136 if (!size && region->iova != iova)
137 return false;
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;
163 while (*new) {
164 struct dfl_afu_dma_region *this;
166 this = container_of(*new, struct dfl_afu_dma_region, node);
168 parent = *new;
170 if (dma_region_check_iova(this, region->iova, region->length))
171 return -EEXIST;
173 if (region->iova < this->iova)
174 new = &((*new)->rb_left);
175 else if (region->iova > this->iova)
176 new = &((*new)->rb_right);
177 else
178 return -EEXIST;
181 rb_link_node(&region->node, parent, new);
182 rb_insert_color(&region->node, &afu->dma_regions);
184 return 0;
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)
197 struct dfl_afu *afu;
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(&region->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;
218 while (node) {
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);
226 if (region->iova)
227 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
228 region->iova, region->length,
229 DMA_BIDIRECTIONAL);
231 if (region->pages)
232 afu_dma_unpin_pages(pdata, region);
234 node = rb_next(node);
235 kfree(region);
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;
260 while (node) {
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);
268 return region;
271 if (iova < region->iova)
272 node = node->rb_left;
273 else if (iova > region->iova)
274 node = node->rb_right;
275 else
276 /* the iova region is not fully covered. */
277 break;
280 dev_dbg(dev, "region with iova %llx and size %llx is not found\n",
281 (unsigned long long)iova, (unsigned long long)size);
283 return NULL;
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;
314 int ret;
317 * Check Inputs, only accept page-aligned user memory region with
318 * valid length.
320 if (!PAGE_ALIGNED(user_addr) || !PAGE_ALIGNED(length) || !length)
321 return -EINVAL;
323 /* Check overflow */
324 if (user_addr + length < user_addr)
325 return -EINVAL;
327 if (!access_ok((void __user *)(unsigned long)user_addr,
328 length))
329 return -EINVAL;
331 region = kzalloc(sizeof(*region), GFP_KERNEL);
332 if (!region)
333 return -ENOMEM;
335 region->user_addr = user_addr;
336 region->length = length;
338 /* Pin the user memory region */
339 ret = afu_dma_pin_pages(pdata, region);
340 if (ret) {
341 dev_err(&pdata->dev->dev, "failed to pin memory region\n");
342 goto free_region;
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");
348 ret = -EINVAL;
349 goto unpin_pages;
352 /* As pages are continuous then start to do DMA mapping */
353 region->iova = dma_map_page(dfl_fpga_pdata_to_parent(pdata),
354 region->pages[0], 0,
355 region->length,
356 DMA_BIDIRECTIONAL);
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");
359 ret = -EFAULT;
360 goto unpin_pages;
363 *iova = region->iova;
365 mutex_lock(&pdata->lock);
366 ret = afu_dma_region_add(pdata, region);
367 mutex_unlock(&pdata->lock);
368 if (ret) {
369 dev_err(&pdata->dev->dev, "failed to add dma region\n");
370 goto unmap_dma;
373 return 0;
375 unmap_dma:
376 dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
377 region->iova, region->length, DMA_BIDIRECTIONAL);
378 unpin_pages:
379 afu_dma_unpin_pages(pdata, region);
380 free_region:
381 kfree(region);
382 return ret;
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);
399 if (!region) {
400 mutex_unlock(&pdata->lock);
401 return -EINVAL;
404 if (region->in_use) {
405 mutex_unlock(&pdata->lock);
406 return -EBUSY;
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);
415 kfree(region);
417 return 0;