dfl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* 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. #ifndef __FPGA_DFL_H
  14. #define __FPGA_DFL_H
  15. #include <linux/bitfield.h>
  16. #include <linux/cdev.h>
  17. #include <linux/delay.h>
  18. #include <linux/fs.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/io-64-nonatomic-lo-hi.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/uuid.h>
  24. #include <linux/fpga/fpga-region.h>
  25. /* maximum supported number of ports */
  26. #define MAX_DFL_FPGA_PORT_NUM 4
  27. /* plus one for fme device */
  28. #define MAX_DFL_FEATURE_DEV_NUM (MAX_DFL_FPGA_PORT_NUM + 1)
  29. /* Reserved 0x0 for Header Group Register and 0xff for AFU */
  30. #define FEATURE_ID_FIU_HEADER 0x0
  31. #define FEATURE_ID_AFU 0xff
  32. #define FME_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
  33. #define FME_FEATURE_ID_THERMAL_MGMT 0x1
  34. #define FME_FEATURE_ID_POWER_MGMT 0x2
  35. #define FME_FEATURE_ID_GLOBAL_IPERF 0x3
  36. #define FME_FEATURE_ID_GLOBAL_ERR 0x4
  37. #define FME_FEATURE_ID_PR_MGMT 0x5
  38. #define FME_FEATURE_ID_HSSI 0x6
  39. #define FME_FEATURE_ID_GLOBAL_DPERF 0x7
  40. #define PORT_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
  41. #define PORT_FEATURE_ID_AFU FEATURE_ID_AFU
  42. #define PORT_FEATURE_ID_ERROR 0x10
  43. #define PORT_FEATURE_ID_UMSG 0x11
  44. #define PORT_FEATURE_ID_UINT 0x12
  45. #define PORT_FEATURE_ID_STP 0x13
  46. /*
  47. * Device Feature Header Register Set
  48. *
  49. * For FIUs, they all have DFH + GUID + NEXT_AFU as common header registers.
  50. * For AFUs, they have DFH + GUID as common header registers.
  51. * For private features, they only have DFH register as common header.
  52. */
  53. #define DFH 0x0
  54. #define GUID_L 0x8
  55. #define GUID_H 0x10
  56. #define NEXT_AFU 0x18
  57. #define DFH_SIZE 0x8
  58. /* Device Feature Header Register Bitfield */
  59. #define DFH_ID GENMASK_ULL(11, 0) /* Feature ID */
  60. #define DFH_ID_FIU_FME 0
  61. #define DFH_ID_FIU_PORT 1
  62. #define DFH_REVISION GENMASK_ULL(15, 12) /* Feature revision */
  63. #define DFH_NEXT_HDR_OFST GENMASK_ULL(39, 16) /* Offset to next DFH */
  64. #define DFH_EOL BIT_ULL(40) /* End of list */
  65. #define DFH_TYPE GENMASK_ULL(63, 60) /* Feature type */
  66. #define DFH_TYPE_AFU 1
  67. #define DFH_TYPE_PRIVATE 3
  68. #define DFH_TYPE_FIU 4
  69. /* Next AFU Register Bitfield */
  70. #define NEXT_AFU_NEXT_DFH_OFST GENMASK_ULL(23, 0) /* Offset to next AFU */
  71. /* FME Header Register Set */
  72. #define FME_HDR_DFH DFH
  73. #define FME_HDR_GUID_L GUID_L
  74. #define FME_HDR_GUID_H GUID_H
  75. #define FME_HDR_NEXT_AFU NEXT_AFU
  76. #define FME_HDR_CAP 0x30
  77. #define FME_HDR_PORT_OFST(n) (0x38 + ((n) * 0x8))
  78. #define FME_HDR_BITSTREAM_ID 0x60
  79. #define FME_HDR_BITSTREAM_MD 0x68
  80. /* FME Fab Capability Register Bitfield */
  81. #define FME_CAP_FABRIC_VERID GENMASK_ULL(7, 0) /* Fabric version ID */
  82. #define FME_CAP_SOCKET_ID BIT_ULL(8) /* Socket ID */
  83. #define FME_CAP_PCIE0_LINK_AVL BIT_ULL(12) /* PCIE0 Link */
  84. #define FME_CAP_PCIE1_LINK_AVL BIT_ULL(13) /* PCIE1 Link */
  85. #define FME_CAP_COHR_LINK_AVL BIT_ULL(14) /* Coherent Link */
  86. #define FME_CAP_IOMMU_AVL BIT_ULL(16) /* IOMMU available */
  87. #define FME_CAP_NUM_PORTS GENMASK_ULL(19, 17) /* Number of ports */
  88. #define FME_CAP_ADDR_WIDTH GENMASK_ULL(29, 24) /* Address bus width */
  89. #define FME_CAP_CACHE_SIZE GENMASK_ULL(43, 32) /* cache size in KB */
  90. #define FME_CAP_CACHE_ASSOC GENMASK_ULL(47, 44) /* Associativity */
  91. /* FME Port Offset Register Bitfield */
  92. /* Offset to port device feature header */
  93. #define FME_PORT_OFST_DFH_OFST GENMASK_ULL(23, 0)
  94. /* PCI Bar ID for this port */
  95. #define FME_PORT_OFST_BAR_ID GENMASK_ULL(34, 32)
  96. /* AFU MMIO access permission. 1 - VF, 0 - PF. */
  97. #define FME_PORT_OFST_ACC_CTRL BIT_ULL(55)
  98. #define FME_PORT_OFST_ACC_PF 0
  99. #define FME_PORT_OFST_ACC_VF 1
  100. #define FME_PORT_OFST_IMP BIT_ULL(60)
  101. /* PORT Header Register Set */
  102. #define PORT_HDR_DFH DFH
  103. #define PORT_HDR_GUID_L GUID_L
  104. #define PORT_HDR_GUID_H GUID_H
  105. #define PORT_HDR_NEXT_AFU NEXT_AFU
  106. #define PORT_HDR_CAP 0x30
  107. #define PORT_HDR_CTRL 0x38
  108. /* Port Capability Register Bitfield */
  109. #define PORT_CAP_PORT_NUM GENMASK_ULL(1, 0) /* ID of this port */
  110. #define PORT_CAP_MMIO_SIZE GENMASK_ULL(23, 8) /* MMIO size in KB */
  111. #define PORT_CAP_SUPP_INT_NUM GENMASK_ULL(35, 32) /* Interrupts num */
  112. /* Port Control Register Bitfield */
  113. #define PORT_CTRL_SFTRST BIT_ULL(0) /* Port soft reset */
  114. /* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
  115. #define PORT_CTRL_LATENCY BIT_ULL(2)
  116. #define PORT_CTRL_SFTRST_ACK BIT_ULL(4) /* HW ack for reset */
  117. /**
  118. * struct dfl_fpga_port_ops - port ops
  119. *
  120. * @name: name of this port ops, to match with port platform device.
  121. * @owner: pointer to the module which owns this port ops.
  122. * @node: node to link port ops to global list.
  123. * @get_id: get port id from hardware.
  124. * @enable_set: enable/disable the port.
  125. */
  126. struct dfl_fpga_port_ops {
  127. const char *name;
  128. struct module *owner;
  129. struct list_head node;
  130. int (*get_id)(struct platform_device *pdev);
  131. int (*enable_set)(struct platform_device *pdev, bool enable);
  132. };
  133. void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
  134. void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
  135. struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
  136. void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
  137. int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
  138. /**
  139. * struct dfl_feature_driver - sub feature's driver
  140. *
  141. * @id: sub feature id.
  142. * @ops: ops of this sub feature.
  143. */
  144. struct dfl_feature_driver {
  145. u64 id;
  146. const struct dfl_feature_ops *ops;
  147. };
  148. /**
  149. * struct dfl_feature - sub feature of the feature devices
  150. *
  151. * @id: sub feature id.
  152. * @resource_index: each sub feature has one mmio resource for its registers.
  153. * this index is used to find its mmio resource from the
  154. * feature dev (platform device)'s reources.
  155. * @ioaddr: mapped mmio resource address.
  156. * @ops: ops of this sub feature.
  157. */
  158. struct dfl_feature {
  159. u64 id;
  160. int resource_index;
  161. void __iomem *ioaddr;
  162. const struct dfl_feature_ops *ops;
  163. };
  164. #define DEV_STATUS_IN_USE 0
  165. /**
  166. * struct dfl_feature_platform_data - platform data for feature devices
  167. *
  168. * @node: node to link feature devs to container device's port_dev_list.
  169. * @lock: mutex to protect platform data.
  170. * @cdev: cdev of feature dev.
  171. * @dev: ptr to platform device linked with this platform data.
  172. * @dfl_cdev: ptr to container device.
  173. * @disable_count: count for port disable.
  174. * @num: number for sub features.
  175. * @dev_status: dev status (e.g. DEV_STATUS_IN_USE).
  176. * @private: ptr to feature dev private data.
  177. * @features: sub features of this feature dev.
  178. */
  179. struct dfl_feature_platform_data {
  180. struct list_head node;
  181. struct mutex lock;
  182. struct cdev cdev;
  183. struct platform_device *dev;
  184. struct dfl_fpga_cdev *dfl_cdev;
  185. unsigned int disable_count;
  186. unsigned long dev_status;
  187. void *private;
  188. int num;
  189. struct dfl_feature features[0];
  190. };
  191. static inline
  192. int dfl_feature_dev_use_begin(struct dfl_feature_platform_data *pdata)
  193. {
  194. /* Test and set IN_USE flags to ensure file is exclusively used */
  195. if (test_and_set_bit_lock(DEV_STATUS_IN_USE, &pdata->dev_status))
  196. return -EBUSY;
  197. return 0;
  198. }
  199. static inline
  200. void dfl_feature_dev_use_end(struct dfl_feature_platform_data *pdata)
  201. {
  202. clear_bit_unlock(DEV_STATUS_IN_USE, &pdata->dev_status);
  203. }
  204. static inline
  205. void dfl_fpga_pdata_set_private(struct dfl_feature_platform_data *pdata,
  206. void *private)
  207. {
  208. pdata->private = private;
  209. }
  210. static inline
  211. void *dfl_fpga_pdata_get_private(struct dfl_feature_platform_data *pdata)
  212. {
  213. return pdata->private;
  214. }
  215. struct dfl_feature_ops {
  216. int (*init)(struct platform_device *pdev, struct dfl_feature *feature);
  217. void (*uinit)(struct platform_device *pdev,
  218. struct dfl_feature *feature);
  219. long (*ioctl)(struct platform_device *pdev, struct dfl_feature *feature,
  220. unsigned int cmd, unsigned long arg);
  221. };
  222. #define DFL_FPGA_FEATURE_DEV_FME "dfl-fme"
  223. #define DFL_FPGA_FEATURE_DEV_PORT "dfl-port"
  224. static inline int dfl_feature_platform_data_size(const int num)
  225. {
  226. return sizeof(struct dfl_feature_platform_data) +
  227. num * sizeof(struct dfl_feature);
  228. }
  229. void dfl_fpga_dev_feature_uinit(struct platform_device *pdev);
  230. int dfl_fpga_dev_feature_init(struct platform_device *pdev,
  231. struct dfl_feature_driver *feature_drvs);
  232. int dfl_fpga_dev_ops_register(struct platform_device *pdev,
  233. const struct file_operations *fops,
  234. struct module *owner);
  235. void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
  236. static inline
  237. struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
  238. {
  239. struct dfl_feature_platform_data *pdata;
  240. pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
  241. cdev);
  242. return pdata->dev;
  243. }
  244. #define dfl_fpga_dev_for_each_feature(pdata, feature) \
  245. for ((feature) = (pdata)->features; \
  246. (feature) < (pdata)->features + (pdata)->num; (feature)++)
  247. static inline
  248. struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u64 id)
  249. {
  250. struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
  251. struct dfl_feature *feature;
  252. dfl_fpga_dev_for_each_feature(pdata, feature)
  253. if (feature->id == id)
  254. return feature;
  255. return NULL;
  256. }
  257. static inline
  258. void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u64 id)
  259. {
  260. struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
  261. if (feature && feature->ioaddr)
  262. return feature->ioaddr;
  263. WARN_ON(1);
  264. return NULL;
  265. }
  266. static inline bool is_dfl_feature_present(struct device *dev, u64 id)
  267. {
  268. return !!dfl_get_feature_ioaddr_by_id(dev, id);
  269. }
  270. static inline
  271. struct device *dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data *pdata)
  272. {
  273. return pdata->dev->dev.parent->parent;
  274. }
  275. static inline bool dfl_feature_is_fme(void __iomem *base)
  276. {
  277. u64 v = readq(base + DFH);
  278. return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
  279. (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
  280. }
  281. static inline bool dfl_feature_is_port(void __iomem *base)
  282. {
  283. u64 v = readq(base + DFH);
  284. return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
  285. (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
  286. }
  287. /**
  288. * struct dfl_fpga_enum_info - DFL FPGA enumeration information
  289. *
  290. * @dev: parent device.
  291. * @dfls: list of device feature lists.
  292. */
  293. struct dfl_fpga_enum_info {
  294. struct device *dev;
  295. struct list_head dfls;
  296. };
  297. /**
  298. * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
  299. *
  300. * @start: base address of this device feature list.
  301. * @len: size of this device feature list.
  302. * @ioaddr: mapped base address of this device feature list.
  303. * @node: node in list of device feature lists.
  304. */
  305. struct dfl_fpga_enum_dfl {
  306. resource_size_t start;
  307. resource_size_t len;
  308. void __iomem *ioaddr;
  309. struct list_head node;
  310. };
  311. struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
  312. int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
  313. resource_size_t start, resource_size_t len,
  314. void __iomem *ioaddr);
  315. void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
  316. /**
  317. * struct dfl_fpga_cdev - container device of DFL based FPGA
  318. *
  319. * @parent: parent device of this container device.
  320. * @region: base fpga region.
  321. * @fme_dev: FME feature device under this container device.
  322. * @lock: mutex lock to protect the port device list.
  323. * @port_dev_list: list of all port feature devices under this container device.
  324. */
  325. struct dfl_fpga_cdev {
  326. struct device *parent;
  327. struct fpga_region *region;
  328. struct device *fme_dev;
  329. struct mutex lock;
  330. struct list_head port_dev_list;
  331. };
  332. struct dfl_fpga_cdev *
  333. dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
  334. void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
  335. /*
  336. * need to drop the device reference with put_device() after use port platform
  337. * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
  338. * functions.
  339. */
  340. struct platform_device *
  341. __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
  342. int (*match)(struct platform_device *, void *));
  343. static inline struct platform_device *
  344. dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
  345. int (*match)(struct platform_device *, void *))
  346. {
  347. struct platform_device *pdev;
  348. mutex_lock(&cdev->lock);
  349. pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
  350. mutex_unlock(&cdev->lock);
  351. return pdev;
  352. }
  353. #endif /* __FPGA_DFL_H */