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 * @pdata: afu platform device's pdata.
43 * @region_index: region index.
44 * @region_size: region size.
45 * @phys: region's physical address of this region.
46 * @flags: region flags (access permission).
48 * Return: 0 on success, negative error code otherwise.
50 int afu_mmio_region_add(struct dfl_feature_platform_data
*pdata
,
51 u32 region_index
, u64 region_size
, u64 phys
, u32 flags
)
53 struct dfl_afu_mmio_region
*region
;
57 region
= devm_kzalloc(&pdata
->dev
->dev
, sizeof(*region
), GFP_KERNEL
);
61 region
->index
= region_index
;
62 region
->size
= region_size
;
64 region
->flags
= flags
;
66 mutex_lock(&pdata
->lock
);
68 afu
= dfl_fpga_pdata_get_private(pdata
);
70 /* check if @index already exists */
71 if (get_region_by_index(afu
, region_index
)) {
72 mutex_unlock(&pdata
->lock
);
77 region_size
= PAGE_ALIGN(region_size
);
78 region
->offset
= afu
->region_cur_offset
;
79 list_add(®ion
->node
, &afu
->regions
);
81 afu
->region_cur_offset
+= region_size
;
83 mutex_unlock(&pdata
->lock
);
88 devm_kfree(&pdata
->dev
->dev
, region
);
93 * afu_mmio_region_destroy - destroy all mmio regions under given feature dev.
94 * @pdata: afu platform device's pdata.
96 void afu_mmio_region_destroy(struct dfl_feature_platform_data
*pdata
)
98 struct dfl_afu
*afu
= dfl_fpga_pdata_get_private(pdata
);
99 struct dfl_afu_mmio_region
*tmp
, *region
;
101 list_for_each_entry_safe(region
, tmp
, &afu
->regions
, node
)
102 devm_kfree(&pdata
->dev
->dev
, region
);
106 * afu_mmio_region_get_by_index - find an afu region by index.
107 * @pdata: afu platform device's pdata.
108 * @region_index: region index.
109 * @pregion: ptr to region for result.
111 * Return: 0 on success, negative error code otherwise.
113 int afu_mmio_region_get_by_index(struct dfl_feature_platform_data
*pdata
,
115 struct dfl_afu_mmio_region
*pregion
)
117 struct dfl_afu_mmio_region
*region
;
121 mutex_lock(&pdata
->lock
);
122 afu
= dfl_fpga_pdata_get_private(pdata
);
123 region
= get_region_by_index(afu
, region_index
);
130 mutex_unlock(&pdata
->lock
);
135 * afu_mmio_region_get_by_offset - find an afu mmio region by offset and size
137 * @pdata: afu platform device's pdata.
138 * @offset: region offset from start of the device fd.
139 * @size: region size.
140 * @pregion: ptr to region for result.
142 * Find the region which fully contains the region described by input
143 * parameters (offset and size) from the feature dev's region linked list.
145 * Return: 0 on success, negative error code otherwise.
147 int afu_mmio_region_get_by_offset(struct dfl_feature_platform_data
*pdata
,
148 u64 offset
, u64 size
,
149 struct dfl_afu_mmio_region
*pregion
)
151 struct dfl_afu_mmio_region
*region
;
155 mutex_lock(&pdata
->lock
);
156 afu
= dfl_fpga_pdata_get_private(pdata
);
157 for_each_region(region
, afu
)
158 if (region
->offset
<= offset
&&
159 region
->offset
+ region
->size
>= offset
+ size
) {
165 mutex_unlock(&pdata
->lock
);