1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for FPGA Accelerated Function Unit (AFU) MMIO 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>
14 * afu_mmio_region_init - init function for afu mmio region support
15 * @pdata: afu platform device's pdata.
17 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata)
19 struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
21 INIT_LIST_HEAD(&afu->regions);
24 #define for_each_region(region, afu) \
25 list_for_each_entry((region), &(afu)->regions, node)
27 static struct dfl_afu_mmio_region *get_region_by_index(struct dfl_afu *afu,
30 struct dfl_afu_mmio_region *region;
32 for_each_region(region, afu)
33 if (region->index == region_index)
40 * afu_mmio_region_add - add a mmio region to given feature dev.
42 * @region_index: region index.
43 * @region_size: region size.
44 * @phys: region's physical address of this region.
45 * @flags: region flags (access permission).
47 * Return: 0 on success, negative error code otherwise.
49 int afu_mmio_region_add(struct dfl_feature_platform_data *pdata,
50 u32 region_index, u64 region_size, u64 phys, u32 flags)
52 struct dfl_afu_mmio_region *region;
56 region = devm_kzalloc(&pdata->dev->dev, sizeof(*region), GFP_KERNEL);
60 region->index = region_index;
61 region->size = region_size;
63 region->flags = flags;
65 mutex_lock(&pdata->lock);
67 afu = dfl_fpga_pdata_get_private(pdata);
69 /* check if @index already exists */
70 if (get_region_by_index(afu, region_index)) {
71 mutex_unlock(&pdata->lock);
76 region_size = PAGE_ALIGN(region_size);
77 region->offset = afu->region_cur_offset;
78 list_add(®ion->node, &afu->regions);
80 afu->region_cur_offset += region_size;
82 mutex_unlock(&pdata->lock);
87 devm_kfree(&pdata->dev->dev, region);
92 * afu_mmio_region_destroy - destroy all mmio regions under given feature dev.
93 * @pdata: afu platform device's pdata.
95 void afu_mmio_region_destroy(struct dfl_feature_platform_data *pdata)
97 struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
98 struct dfl_afu_mmio_region *tmp, *region;
100 list_for_each_entry_safe(region, tmp, &afu->regions, node)
101 devm_kfree(&pdata->dev->dev, region);
105 * afu_mmio_region_get_by_index - find an afu region by index.
106 * @pdata: afu platform device's pdata.
107 * @region_index: region index.
108 * @pregion: ptr to region for result.
110 * Return: 0 on success, negative error code otherwise.
112 int afu_mmio_region_get_by_index(struct dfl_feature_platform_data *pdata,
114 struct dfl_afu_mmio_region *pregion)
116 struct dfl_afu_mmio_region *region;
120 mutex_lock(&pdata->lock);
121 afu = dfl_fpga_pdata_get_private(pdata);
122 region = get_region_by_index(afu, region_index);
129 mutex_unlock(&pdata->lock);
134 * afu_mmio_region_get_by_offset - find an afu mmio region by offset and size
136 * @pdata: afu platform device's pdata.
137 * @offset: region offset from start of the device fd.
138 * @size: region size.
139 * @pregion: ptr to region for result.
141 * Find the region which fully contains the region described by input
142 * parameters (offset and size) from the feature dev's region linked list.
144 * Return: 0 on success, negative error code otherwise.
146 int afu_mmio_region_get_by_offset(struct dfl_feature_platform_data *pdata,
147 u64 offset, u64 size,
148 struct dfl_afu_mmio_region *pregion)
150 struct dfl_afu_mmio_region *region;
154 mutex_lock(&pdata->lock);
155 afu = dfl_fpga_pdata_get_private(pdata);
156 for_each_region(region, afu)
157 if (region->offset <= offset &&
158 region->offset + region->size >= offset + size) {
164 mutex_unlock(&pdata->lock);