dfl.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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_feature - sub feature of the feature devices
  119. *
  120. * @id: sub feature id.
  121. * @resource_index: each sub feature has one mmio resource for its registers.
  122. * this index is used to find its mmio resource from the
  123. * feature dev (platform device)'s reources.
  124. * @ioaddr: mapped mmio resource address.
  125. */
  126. struct dfl_feature {
  127. u64 id;
  128. int resource_index;
  129. void __iomem *ioaddr;
  130. };
  131. /**
  132. * struct dfl_feature_platform_data - platform data for feature devices
  133. *
  134. * @node: node to link feature devs to container device's port_dev_list.
  135. * @lock: mutex to protect platform data.
  136. * @cdev: cdev of feature dev.
  137. * @dev: ptr to platform device linked with this platform data.
  138. * @dfl_cdev: ptr to container device.
  139. * @disable_count: count for port disable.
  140. * @num: number for sub features.
  141. * @features: sub features of this feature dev.
  142. */
  143. struct dfl_feature_platform_data {
  144. struct list_head node;
  145. struct mutex lock;
  146. struct cdev cdev;
  147. struct platform_device *dev;
  148. struct dfl_fpga_cdev *dfl_cdev;
  149. unsigned int disable_count;
  150. int num;
  151. struct dfl_feature features[0];
  152. };
  153. #define DFL_FPGA_FEATURE_DEV_FME "dfl-fme"
  154. #define DFL_FPGA_FEATURE_DEV_PORT "dfl-port"
  155. static inline int dfl_feature_platform_data_size(const int num)
  156. {
  157. return sizeof(struct dfl_feature_platform_data) +
  158. num * sizeof(struct dfl_feature);
  159. }
  160. int dfl_fpga_dev_ops_register(struct platform_device *pdev,
  161. const struct file_operations *fops,
  162. struct module *owner);
  163. void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
  164. #define dfl_fpga_dev_for_each_feature(pdata, feature) \
  165. for ((feature) = (pdata)->features; \
  166. (feature) < (pdata)->features + (pdata)->num; (feature)++)
  167. static inline
  168. struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u64 id)
  169. {
  170. struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
  171. struct dfl_feature *feature;
  172. dfl_fpga_dev_for_each_feature(pdata, feature)
  173. if (feature->id == id)
  174. return feature;
  175. return NULL;
  176. }
  177. static inline
  178. void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u64 id)
  179. {
  180. struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
  181. if (feature && feature->ioaddr)
  182. return feature->ioaddr;
  183. WARN_ON(1);
  184. return NULL;
  185. }
  186. static inline bool dfl_feature_is_fme(void __iomem *base)
  187. {
  188. u64 v = readq(base + DFH);
  189. return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
  190. (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
  191. }
  192. static inline bool dfl_feature_is_port(void __iomem *base)
  193. {
  194. u64 v = readq(base + DFH);
  195. return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
  196. (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
  197. }
  198. /**
  199. * struct dfl_fpga_enum_info - DFL FPGA enumeration information
  200. *
  201. * @dev: parent device.
  202. * @dfls: list of device feature lists.
  203. */
  204. struct dfl_fpga_enum_info {
  205. struct device *dev;
  206. struct list_head dfls;
  207. };
  208. /**
  209. * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
  210. *
  211. * @start: base address of this device feature list.
  212. * @len: size of this device feature list.
  213. * @ioaddr: mapped base address of this device feature list.
  214. * @node: node in list of device feature lists.
  215. */
  216. struct dfl_fpga_enum_dfl {
  217. resource_size_t start;
  218. resource_size_t len;
  219. void __iomem *ioaddr;
  220. struct list_head node;
  221. };
  222. struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
  223. int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
  224. resource_size_t start, resource_size_t len,
  225. void __iomem *ioaddr);
  226. void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
  227. /**
  228. * struct dfl_fpga_cdev - container device of DFL based FPGA
  229. *
  230. * @parent: parent device of this container device.
  231. * @region: base fpga region.
  232. * @fme_dev: FME feature device under this container device.
  233. * @lock: mutex lock to protect the port device list.
  234. * @port_dev_list: list of all port feature devices under this container device.
  235. */
  236. struct dfl_fpga_cdev {
  237. struct device *parent;
  238. struct fpga_region *region;
  239. struct device *fme_dev;
  240. struct mutex lock;
  241. struct list_head port_dev_list;
  242. };
  243. struct dfl_fpga_cdev *
  244. dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
  245. void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
  246. #endif /* __FPGA_DFL_H */