blob: 6f7855e578697e33d1ec07ea29793330b5e93ba1 [file] [log] [blame]
Wu Hao543be3d2018-06-30 08:53:13 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Driver Header File for FPGA Device Feature List (DFL) Support
4 *
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
6 *
7 * Authors:
8 * Kang Luwei <luwei.kang@intel.com>
9 * Zhang Yi <yi.z.zhang@intel.com>
10 * Wu Hao <hao.wu@intel.com>
11 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
12 */
13
14#ifndef __FPGA_DFL_H
15#define __FPGA_DFL_H
16
17#include <linux/bitfield.h>
Wu Haob16c5142018-06-30 08:53:14 +080018#include <linux/cdev.h>
Wu Hao543be3d2018-06-30 08:53:13 +080019#include <linux/delay.h>
20#include <linux/fs.h>
21#include <linux/iopoll.h>
22#include <linux/io-64-nonatomic-lo-hi.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/uuid.h>
26#include <linux/fpga/fpga-region.h>
27
28/* maximum supported number of ports */
29#define MAX_DFL_FPGA_PORT_NUM 4
30/* plus one for fme device */
31#define MAX_DFL_FEATURE_DEV_NUM (MAX_DFL_FPGA_PORT_NUM + 1)
32
33/* Reserved 0x0 for Header Group Register and 0xff for AFU */
34#define FEATURE_ID_FIU_HEADER 0x0
35#define FEATURE_ID_AFU 0xff
36
37#define FME_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
38#define FME_FEATURE_ID_THERMAL_MGMT 0x1
39#define FME_FEATURE_ID_POWER_MGMT 0x2
40#define FME_FEATURE_ID_GLOBAL_IPERF 0x3
41#define FME_FEATURE_ID_GLOBAL_ERR 0x4
42#define FME_FEATURE_ID_PR_MGMT 0x5
43#define FME_FEATURE_ID_HSSI 0x6
44#define FME_FEATURE_ID_GLOBAL_DPERF 0x7
45
46#define PORT_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
47#define PORT_FEATURE_ID_AFU FEATURE_ID_AFU
48#define PORT_FEATURE_ID_ERROR 0x10
49#define PORT_FEATURE_ID_UMSG 0x11
50#define PORT_FEATURE_ID_UINT 0x12
51#define PORT_FEATURE_ID_STP 0x13
52
53/*
54 * Device Feature Header Register Set
55 *
56 * For FIUs, they all have DFH + GUID + NEXT_AFU as common header registers.
57 * For AFUs, they have DFH + GUID as common header registers.
58 * For private features, they only have DFH register as common header.
59 */
60#define DFH 0x0
61#define GUID_L 0x8
62#define GUID_H 0x10
63#define NEXT_AFU 0x18
64
65#define DFH_SIZE 0x8
66
67/* Device Feature Header Register Bitfield */
68#define DFH_ID GENMASK_ULL(11, 0) /* Feature ID */
69#define DFH_ID_FIU_FME 0
70#define DFH_ID_FIU_PORT 1
71#define DFH_REVISION GENMASK_ULL(15, 12) /* Feature revision */
72#define DFH_NEXT_HDR_OFST GENMASK_ULL(39, 16) /* Offset to next DFH */
73#define DFH_EOL BIT_ULL(40) /* End of list */
74#define DFH_TYPE GENMASK_ULL(63, 60) /* Feature type */
75#define DFH_TYPE_AFU 1
76#define DFH_TYPE_PRIVATE 3
77#define DFH_TYPE_FIU 4
78
79/* Next AFU Register Bitfield */
80#define NEXT_AFU_NEXT_DFH_OFST GENMASK_ULL(23, 0) /* Offset to next AFU */
81
82/* FME Header Register Set */
83#define FME_HDR_DFH DFH
84#define FME_HDR_GUID_L GUID_L
85#define FME_HDR_GUID_H GUID_H
86#define FME_HDR_NEXT_AFU NEXT_AFU
87#define FME_HDR_CAP 0x30
88#define FME_HDR_PORT_OFST(n) (0x38 + ((n) * 0x8))
89#define FME_HDR_BITSTREAM_ID 0x60
90#define FME_HDR_BITSTREAM_MD 0x68
91
92/* FME Fab Capability Register Bitfield */
93#define FME_CAP_FABRIC_VERID GENMASK_ULL(7, 0) /* Fabric version ID */
94#define FME_CAP_SOCKET_ID BIT_ULL(8) /* Socket ID */
95#define FME_CAP_PCIE0_LINK_AVL BIT_ULL(12) /* PCIE0 Link */
96#define FME_CAP_PCIE1_LINK_AVL BIT_ULL(13) /* PCIE1 Link */
97#define FME_CAP_COHR_LINK_AVL BIT_ULL(14) /* Coherent Link */
98#define FME_CAP_IOMMU_AVL BIT_ULL(16) /* IOMMU available */
99#define FME_CAP_NUM_PORTS GENMASK_ULL(19, 17) /* Number of ports */
100#define FME_CAP_ADDR_WIDTH GENMASK_ULL(29, 24) /* Address bus width */
101#define FME_CAP_CACHE_SIZE GENMASK_ULL(43, 32) /* cache size in KB */
102#define FME_CAP_CACHE_ASSOC GENMASK_ULL(47, 44) /* Associativity */
103
104/* FME Port Offset Register Bitfield */
105/* Offset to port device feature header */
106#define FME_PORT_OFST_DFH_OFST GENMASK_ULL(23, 0)
107/* PCI Bar ID for this port */
108#define FME_PORT_OFST_BAR_ID GENMASK_ULL(34, 32)
109/* AFU MMIO access permission. 1 - VF, 0 - PF. */
110#define FME_PORT_OFST_ACC_CTRL BIT_ULL(55)
111#define FME_PORT_OFST_ACC_PF 0
112#define FME_PORT_OFST_ACC_VF 1
113#define FME_PORT_OFST_IMP BIT_ULL(60)
114
115/* PORT Header Register Set */
116#define PORT_HDR_DFH DFH
117#define PORT_HDR_GUID_L GUID_L
118#define PORT_HDR_GUID_H GUID_H
119#define PORT_HDR_NEXT_AFU NEXT_AFU
120#define PORT_HDR_CAP 0x30
121#define PORT_HDR_CTRL 0x38
122
123/* Port Capability Register Bitfield */
124#define PORT_CAP_PORT_NUM GENMASK_ULL(1, 0) /* ID of this port */
125#define PORT_CAP_MMIO_SIZE GENMASK_ULL(23, 8) /* MMIO size in KB */
126#define PORT_CAP_SUPP_INT_NUM GENMASK_ULL(35, 32) /* Interrupts num */
127
128/* Port Control Register Bitfield */
129#define PORT_CTRL_SFTRST BIT_ULL(0) /* Port soft reset */
130/* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
131#define PORT_CTRL_LATENCY BIT_ULL(2)
132#define PORT_CTRL_SFTRST_ACK BIT_ULL(4) /* HW ack for reset */
Wu Hao6e8fd6e2018-06-30 08:53:17 +0800133/**
134 * struct dfl_fpga_port_ops - port ops
135 *
136 * @name: name of this port ops, to match with port platform device.
137 * @owner: pointer to the module which owns this port ops.
138 * @node: node to link port ops to global list.
139 * @get_id: get port id from hardware.
140 * @enable_set: enable/disable the port.
141 */
142struct dfl_fpga_port_ops {
143 const char *name;
144 struct module *owner;
145 struct list_head node;
146 int (*get_id)(struct platform_device *pdev);
147 int (*enable_set)(struct platform_device *pdev, bool enable);
148};
149
150void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
151void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
152struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
153void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
Wu Haod06b0042018-06-30 08:53:18 +0800154int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
Wu Hao543be3d2018-06-30 08:53:13 +0800155
156/**
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800157 * struct dfl_feature_driver - sub feature's driver
158 *
159 * @id: sub feature id.
160 * @ops: ops of this sub feature.
161 */
162struct dfl_feature_driver {
163 u64 id;
164 const struct dfl_feature_ops *ops;
165};
166
167/**
Wu Hao543be3d2018-06-30 08:53:13 +0800168 * struct dfl_feature - sub feature of the feature devices
169 *
170 * @id: sub feature id.
171 * @resource_index: each sub feature has one mmio resource for its registers.
172 * this index is used to find its mmio resource from the
173 * feature dev (platform device)'s reources.
174 * @ioaddr: mapped mmio resource address.
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800175 * @ops: ops of this sub feature.
Wu Hao543be3d2018-06-30 08:53:13 +0800176 */
177struct dfl_feature {
178 u64 id;
179 int resource_index;
180 void __iomem *ioaddr;
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800181 const struct dfl_feature_ops *ops;
Wu Hao543be3d2018-06-30 08:53:13 +0800182};
183
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800184#define DEV_STATUS_IN_USE 0
185
Wu Hao69bb18d2019-08-04 18:20:11 +0800186#define FEATURE_DEV_ID_UNUSED (-1)
187
Wu Hao543be3d2018-06-30 08:53:13 +0800188/**
189 * struct dfl_feature_platform_data - platform data for feature devices
190 *
191 * @node: node to link feature devs to container device's port_dev_list.
192 * @lock: mutex to protect platform data.
Wu Haob16c5142018-06-30 08:53:14 +0800193 * @cdev: cdev of feature dev.
Wu Hao543be3d2018-06-30 08:53:13 +0800194 * @dev: ptr to platform device linked with this platform data.
195 * @dfl_cdev: ptr to container device.
Wu Hao69bb18d2019-08-04 18:20:11 +0800196 * @id: id used for this feature device.
Wu Hao543be3d2018-06-30 08:53:13 +0800197 * @disable_count: count for port disable.
198 * @num: number for sub features.
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800199 * @dev_status: dev status (e.g. DEV_STATUS_IN_USE).
200 * @private: ptr to feature dev private data.
Wu Hao543be3d2018-06-30 08:53:13 +0800201 * @features: sub features of this feature dev.
202 */
203struct dfl_feature_platform_data {
204 struct list_head node;
205 struct mutex lock;
Wu Haob16c5142018-06-30 08:53:14 +0800206 struct cdev cdev;
Wu Hao543be3d2018-06-30 08:53:13 +0800207 struct platform_device *dev;
208 struct dfl_fpga_cdev *dfl_cdev;
Wu Hao69bb18d2019-08-04 18:20:11 +0800209 int id;
Wu Hao543be3d2018-06-30 08:53:13 +0800210 unsigned int disable_count;
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800211 unsigned long dev_status;
212 void *private;
Wu Hao543be3d2018-06-30 08:53:13 +0800213 int num;
214 struct dfl_feature features[0];
215};
216
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800217static inline
218int dfl_feature_dev_use_begin(struct dfl_feature_platform_data *pdata)
219{
220 /* Test and set IN_USE flags to ensure file is exclusively used */
221 if (test_and_set_bit_lock(DEV_STATUS_IN_USE, &pdata->dev_status))
222 return -EBUSY;
223
224 return 0;
225}
226
227static inline
228void dfl_feature_dev_use_end(struct dfl_feature_platform_data *pdata)
229{
230 clear_bit_unlock(DEV_STATUS_IN_USE, &pdata->dev_status);
231}
232
233static inline
234void dfl_fpga_pdata_set_private(struct dfl_feature_platform_data *pdata,
235 void *private)
236{
237 pdata->private = private;
238}
239
240static inline
241void *dfl_fpga_pdata_get_private(struct dfl_feature_platform_data *pdata)
242{
243 return pdata->private;
244}
245
246struct dfl_feature_ops {
247 int (*init)(struct platform_device *pdev, struct dfl_feature *feature);
248 void (*uinit)(struct platform_device *pdev,
249 struct dfl_feature *feature);
250 long (*ioctl)(struct platform_device *pdev, struct dfl_feature *feature,
251 unsigned int cmd, unsigned long arg);
252};
253
Wu Hao543be3d2018-06-30 08:53:13 +0800254#define DFL_FPGA_FEATURE_DEV_FME "dfl-fme"
255#define DFL_FPGA_FEATURE_DEV_PORT "dfl-port"
256
257static inline int dfl_feature_platform_data_size(const int num)
258{
259 return sizeof(struct dfl_feature_platform_data) +
260 num * sizeof(struct dfl_feature);
261}
262
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800263void dfl_fpga_dev_feature_uinit(struct platform_device *pdev);
264int dfl_fpga_dev_feature_init(struct platform_device *pdev,
265 struct dfl_feature_driver *feature_drvs);
266
Wu Haob16c5142018-06-30 08:53:14 +0800267int dfl_fpga_dev_ops_register(struct platform_device *pdev,
268 const struct file_operations *fops,
269 struct module *owner);
270void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
271
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800272static inline
273struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
274{
275 struct dfl_feature_platform_data *pdata;
276
277 pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
278 cdev);
279 return pdata->dev;
280}
281
Wu Hao543be3d2018-06-30 08:53:13 +0800282#define dfl_fpga_dev_for_each_feature(pdata, feature) \
283 for ((feature) = (pdata)->features; \
284 (feature) < (pdata)->features + (pdata)->num; (feature)++)
285
286static inline
287struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u64 id)
288{
289 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
290 struct dfl_feature *feature;
291
292 dfl_fpga_dev_for_each_feature(pdata, feature)
293 if (feature->id == id)
294 return feature;
295
296 return NULL;
297}
298
299static inline
300void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u64 id)
301{
302 struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
303
304 if (feature && feature->ioaddr)
305 return feature->ioaddr;
306
307 WARN_ON(1);
308 return NULL;
309}
310
Xiao Guangrong5b57d022018-06-30 08:53:16 +0800311static inline bool is_dfl_feature_present(struct device *dev, u64 id)
312{
313 return !!dfl_get_feature_ioaddr_by_id(dev, id);
314}
315
316static inline
317struct device *dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data *pdata)
318{
319 return pdata->dev->dev.parent->parent;
320}
321
Wu Hao543be3d2018-06-30 08:53:13 +0800322static inline bool dfl_feature_is_fme(void __iomem *base)
323{
324 u64 v = readq(base + DFH);
325
326 return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
327 (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
328}
329
330static inline bool dfl_feature_is_port(void __iomem *base)
331{
332 u64 v = readq(base + DFH);
333
334 return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
335 (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
336}
337
338/**
339 * struct dfl_fpga_enum_info - DFL FPGA enumeration information
340 *
341 * @dev: parent device.
342 * @dfls: list of device feature lists.
343 */
344struct dfl_fpga_enum_info {
345 struct device *dev;
346 struct list_head dfls;
347};
348
349/**
350 * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
351 *
352 * @start: base address of this device feature list.
353 * @len: size of this device feature list.
354 * @ioaddr: mapped base address of this device feature list.
355 * @node: node in list of device feature lists.
356 */
357struct dfl_fpga_enum_dfl {
358 resource_size_t start;
359 resource_size_t len;
360
361 void __iomem *ioaddr;
362
363 struct list_head node;
364};
365
366struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
367int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
368 resource_size_t start, resource_size_t len,
369 void __iomem *ioaddr);
370void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
371
372/**
373 * struct dfl_fpga_cdev - container device of DFL based FPGA
374 *
375 * @parent: parent device of this container device.
376 * @region: base fpga region.
377 * @fme_dev: FME feature device under this container device.
378 * @lock: mutex lock to protect the port device list.
379 * @port_dev_list: list of all port feature devices under this container device.
Wu Hao69bb18d2019-08-04 18:20:11 +0800380 * @released_port_num: released port number under this container device.
Wu Hao543be3d2018-06-30 08:53:13 +0800381 */
382struct dfl_fpga_cdev {
383 struct device *parent;
384 struct fpga_region *region;
385 struct device *fme_dev;
386 struct mutex lock;
387 struct list_head port_dev_list;
Wu Hao69bb18d2019-08-04 18:20:11 +0800388 int released_port_num;
Wu Hao543be3d2018-06-30 08:53:13 +0800389};
390
391struct dfl_fpga_cdev *
392dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
393void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
394
Wu Hao5d56e112018-06-30 08:53:15 +0800395/*
396 * need to drop the device reference with put_device() after use port platform
397 * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
398 * functions.
399 */
400struct platform_device *
401__dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
402 int (*match)(struct platform_device *, void *));
403
404static inline struct platform_device *
405dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
406 int (*match)(struct platform_device *, void *))
407{
408 struct platform_device *pdev;
409
410 mutex_lock(&cdev->lock);
411 pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
412 mutex_unlock(&cdev->lock);
413
414 return pdev;
415}
Wu Hao69bb18d2019-08-04 18:20:11 +0800416
417int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id);
418int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id);
419
Wu Hao543be3d2018-06-30 08:53:13 +0800420#endif /* __FPGA_DFL_H */