dfl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 <[email protected]>
  9. * Zhang Yi <[email protected]>
  10. * Wu Hao <[email protected]>
  11. * Xiao Guangrong <[email protected]>
  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/eventfd.h>
  19. #include <linux/fs.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/io-64-nonatomic-lo-hi.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include <linux/uuid.h>
  27. #include <linux/fpga/fpga-region.h>
  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. /* Reserved 0xfe for Header Group Register and 0xff for AFU */
  33. #define FEATURE_ID_FIU_HEADER 0xfe
  34. #define FEATURE_ID_AFU 0xff
  35. #define FME_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
  36. #define FME_FEATURE_ID_THERMAL_MGMT 0x1
  37. #define FME_FEATURE_ID_POWER_MGMT 0x2
  38. #define FME_FEATURE_ID_GLOBAL_IPERF 0x3
  39. #define FME_FEATURE_ID_GLOBAL_ERR 0x4
  40. #define FME_FEATURE_ID_PR_MGMT 0x5
  41. #define FME_FEATURE_ID_HSSI 0x6
  42. #define FME_FEATURE_ID_GLOBAL_DPERF 0x7
  43. #define PORT_FEATURE_ID_HEADER FEATURE_ID_FIU_HEADER
  44. #define PORT_FEATURE_ID_AFU FEATURE_ID_AFU
  45. #define PORT_FEATURE_ID_ERROR 0x10
  46. #define PORT_FEATURE_ID_UMSG 0x11
  47. #define PORT_FEATURE_ID_UINT 0x12
  48. #define PORT_FEATURE_ID_STP 0x13
  49. /*
  50. * Device Feature Header Register Set
  51. *
  52. * For FIUs, they all have DFH + GUID + NEXT_AFU as common header registers.
  53. * For AFUs, they have DFH + GUID as common header registers.
  54. * For private features, they only have DFH register as common header.
  55. */
  56. #define DFH 0x0
  57. #define GUID_L 0x8
  58. #define GUID_H 0x10
  59. #define NEXT_AFU 0x18
  60. #define DFH_SIZE 0x8
  61. /* Device Feature Header Register Bitfield */
  62. #define DFH_ID GENMASK_ULL(11, 0) /* Feature ID */
  63. #define DFH_ID_FIU_FME 0
  64. #define DFH_ID_FIU_PORT 1
  65. #define DFH_REVISION GENMASK_ULL(15, 12) /* Feature revision */
  66. #define DFH_NEXT_HDR_OFST GENMASK_ULL(39, 16) /* Offset to next DFH */
  67. #define DFH_EOL BIT_ULL(40) /* End of list */
  68. #define DFH_TYPE GENMASK_ULL(63, 60) /* Feature type */
  69. #define DFH_TYPE_AFU 1
  70. #define DFH_TYPE_PRIVATE 3
  71. #define DFH_TYPE_FIU 4
  72. /* Next AFU Register Bitfield */
  73. #define NEXT_AFU_NEXT_DFH_OFST GENMASK_ULL(23, 0) /* Offset to next AFU */
  74. /* FME Header Register Set */
  75. #define FME_HDR_DFH DFH
  76. #define FME_HDR_GUID_L GUID_L
  77. #define FME_HDR_GUID_H GUID_H
  78. #define FME_HDR_NEXT_AFU NEXT_AFU
  79. #define FME_HDR_CAP 0x30
  80. #define FME_HDR_PORT_OFST(n) (0x38 + ((n) * 0x8))
  81. #define FME_PORT_OFST_BAR_SKIP 7
  82. #define FME_HDR_BITSTREAM_ID 0x60
  83. #define FME_HDR_BITSTREAM_MD 0x68
  84. /* FME Fab Capability Register Bitfield */
  85. #define FME_CAP_FABRIC_VERID GENMASK_ULL(7, 0) /* Fabric version ID */
  86. #define FME_CAP_SOCKET_ID BIT_ULL(8) /* Socket ID */
  87. #define FME_CAP_PCIE0_LINK_AVL BIT_ULL(12) /* PCIE0 Link */
  88. #define FME_CAP_PCIE1_LINK_AVL BIT_ULL(13) /* PCIE1 Link */
  89. #define FME_CAP_COHR_LINK_AVL BIT_ULL(14) /* Coherent Link */
  90. #define FME_CAP_IOMMU_AVL BIT_ULL(16) /* IOMMU available */
  91. #define FME_CAP_NUM_PORTS GENMASK_ULL(19, 17) /* Number of ports */
  92. #define FME_CAP_ADDR_WIDTH GENMASK_ULL(29, 24) /* Address bus width */
  93. #define FME_CAP_CACHE_SIZE GENMASK_ULL(43, 32) /* cache size in KB */
  94. #define FME_CAP_CACHE_ASSOC GENMASK_ULL(47, 44) /* Associativity */
  95. /* FME Port Offset Register Bitfield */
  96. /* Offset to port device feature header */
  97. #define FME_PORT_OFST_DFH_OFST GENMASK_ULL(23, 0)
  98. /* PCI Bar ID for this port */
  99. #define FME_PORT_OFST_BAR_ID GENMASK_ULL(34, 32)
  100. /* AFU MMIO access permission. 1 - VF, 0 - PF. */
  101. #define FME_PORT_OFST_ACC_CTRL BIT_ULL(55)
  102. #define FME_PORT_OFST_ACC_PF 0
  103. #define FME_PORT_OFST_ACC_VF 1
  104. #define FME_PORT_OFST_IMP BIT_ULL(60)
  105. /* FME Error Capability Register */
  106. #define FME_ERROR_CAP 0x70
  107. /* FME Error Capability Register Bitfield */
  108. #define FME_ERROR_CAP_SUPP_INT BIT_ULL(0) /* Interrupt Support */
  109. #define FME_ERROR_CAP_INT_VECT GENMASK_ULL(12, 1) /* Interrupt vector */
  110. /* PORT Header Register Set */
  111. #define PORT_HDR_DFH DFH
  112. #define PORT_HDR_GUID_L GUID_L
  113. #define PORT_HDR_GUID_H GUID_H
  114. #define PORT_HDR_NEXT_AFU NEXT_AFU
  115. #define PORT_HDR_CAP 0x30
  116. #define PORT_HDR_CTRL 0x38
  117. #define PORT_HDR_STS 0x40
  118. #define PORT_HDR_USRCLK_CMD0 0x50
  119. #define PORT_HDR_USRCLK_CMD1 0x58
  120. #define PORT_HDR_USRCLK_STS0 0x60
  121. #define PORT_HDR_USRCLK_STS1 0x68
  122. /* Port Capability Register Bitfield */
  123. #define PORT_CAP_PORT_NUM GENMASK_ULL(1, 0) /* ID of this port */
  124. #define PORT_CAP_MMIO_SIZE GENMASK_ULL(23, 8) /* MMIO size in KB */
  125. #define PORT_CAP_SUPP_INT_NUM GENMASK_ULL(35, 32) /* Interrupts num */
  126. /* Port Control Register Bitfield */
  127. #define PORT_CTRL_SFTRST BIT_ULL(0) /* Port soft reset */
  128. /* Latency tolerance reporting. '1' >= 40us, '0' < 40us.*/
  129. #define PORT_CTRL_LATENCY BIT_ULL(2)
  130. #define PORT_CTRL_SFTRST_ACK BIT_ULL(4) /* HW ack for reset */
  131. /* Port Status Register Bitfield */
  132. #define PORT_STS_AP2_EVT BIT_ULL(13) /* AP2 event detected */
  133. #define PORT_STS_AP1_EVT BIT_ULL(12) /* AP1 event detected */
  134. #define PORT_STS_PWR_STATE GENMASK_ULL(11, 8) /* AFU power states */
  135. #define PORT_STS_PWR_STATE_NORM 0
  136. #define PORT_STS_PWR_STATE_AP1 1 /* 50% throttling */
  137. #define PORT_STS_PWR_STATE_AP2 2 /* 90% throttling */
  138. #define PORT_STS_PWR_STATE_AP6 6 /* 100% throttling */
  139. /* Port Error Capability Register */
  140. #define PORT_ERROR_CAP 0x38
  141. /* Port Error Capability Register Bitfield */
  142. #define PORT_ERROR_CAP_SUPP_INT BIT_ULL(0) /* Interrupt Support */
  143. #define PORT_ERROR_CAP_INT_VECT GENMASK_ULL(12, 1) /* Interrupt vector */
  144. /* Port Uint Capability Register */
  145. #define PORT_UINT_CAP 0x8
  146. /* Port Uint Capability Register Bitfield */
  147. #define PORT_UINT_CAP_INT_NUM GENMASK_ULL(11, 0) /* Interrupts num */
  148. #define PORT_UINT_CAP_FST_VECT GENMASK_ULL(23, 12) /* First Vector */
  149. /**
  150. * struct dfl_fpga_port_ops - port ops
  151. *
  152. * @name: name of this port ops, to match with port platform device.
  153. * @owner: pointer to the module which owns this port ops.
  154. * @node: node to link port ops to global list.
  155. * @get_id: get port id from hardware.
  156. * @enable_set: enable/disable the port.
  157. */
  158. struct dfl_fpga_port_ops {
  159. const char *name;
  160. struct module *owner;
  161. struct list_head node;
  162. int (*get_id)(struct platform_device *pdev);
  163. int (*enable_set)(struct platform_device *pdev, bool enable);
  164. };
  165. void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
  166. void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
  167. struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
  168. void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
  169. int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
  170. /**
  171. * struct dfl_feature_id - dfl private feature id
  172. *
  173. * @id: unique dfl private feature id.
  174. */
  175. struct dfl_feature_id {
  176. u16 id;
  177. };
  178. /**
  179. * struct dfl_feature_driver - dfl private feature driver
  180. *
  181. * @id_table: id_table for dfl private features supported by this driver.
  182. * @ops: ops of this dfl private feature driver.
  183. */
  184. struct dfl_feature_driver {
  185. const struct dfl_feature_id *id_table;
  186. const struct dfl_feature_ops *ops;
  187. };
  188. /**
  189. * struct dfl_feature_irq_ctx - dfl private feature interrupt context
  190. *
  191. * @irq: Linux IRQ number of this interrupt.
  192. * @trigger: eventfd context to signal when interrupt happens.
  193. * @name: irq name needed when requesting irq.
  194. */
  195. struct dfl_feature_irq_ctx {
  196. int irq;
  197. struct eventfd_ctx *trigger;
  198. char *name;
  199. };
  200. /**
  201. * struct dfl_feature - sub feature of the feature devices
  202. *
  203. * @dev: ptr to pdev of the feature device which has the sub feature.
  204. * @id: sub feature id.
  205. * @resource_index: each sub feature has one mmio resource for its registers.
  206. * this index is used to find its mmio resource from the
  207. * feature dev (platform device)'s resources.
  208. * @ioaddr: mapped mmio resource address.
  209. * @irq_ctx: interrupt context list.
  210. * @nr_irqs: number of interrupt contexts.
  211. * @ops: ops of this sub feature.
  212. * @ddev: ptr to the dfl device of this sub feature.
  213. * @priv: priv data of this feature.
  214. */
  215. struct dfl_feature {
  216. struct platform_device *dev;
  217. u16 id;
  218. u8 revision;
  219. int resource_index;
  220. void __iomem *ioaddr;
  221. struct dfl_feature_irq_ctx *irq_ctx;
  222. unsigned int nr_irqs;
  223. const struct dfl_feature_ops *ops;
  224. struct dfl_device *ddev;
  225. void *priv;
  226. };
  227. #define FEATURE_DEV_ID_UNUSED (-1)
  228. /**
  229. * struct dfl_feature_platform_data - platform data for feature devices
  230. *
  231. * @node: node to link feature devs to container device's port_dev_list.
  232. * @lock: mutex to protect platform data.
  233. * @cdev: cdev of feature dev.
  234. * @dev: ptr to platform device linked with this platform data.
  235. * @dfl_cdev: ptr to container device.
  236. * @id: id used for this feature device.
  237. * @disable_count: count for port disable.
  238. * @excl_open: set on feature device exclusive open.
  239. * @open_count: count for feature device open.
  240. * @num: number for sub features.
  241. * @private: ptr to feature dev private data.
  242. * @features: sub features of this feature dev.
  243. */
  244. struct dfl_feature_platform_data {
  245. struct list_head node;
  246. struct mutex lock;
  247. struct cdev cdev;
  248. struct platform_device *dev;
  249. struct dfl_fpga_cdev *dfl_cdev;
  250. int id;
  251. unsigned int disable_count;
  252. bool excl_open;
  253. int open_count;
  254. void *private;
  255. int num;
  256. struct dfl_feature features[];
  257. };
  258. static inline
  259. int dfl_feature_dev_use_begin(struct dfl_feature_platform_data *pdata,
  260. bool excl)
  261. {
  262. if (pdata->excl_open)
  263. return -EBUSY;
  264. if (excl) {
  265. if (pdata->open_count)
  266. return -EBUSY;
  267. pdata->excl_open = true;
  268. }
  269. pdata->open_count++;
  270. return 0;
  271. }
  272. static inline
  273. void dfl_feature_dev_use_end(struct dfl_feature_platform_data *pdata)
  274. {
  275. pdata->excl_open = false;
  276. if (WARN_ON(pdata->open_count <= 0))
  277. return;
  278. pdata->open_count--;
  279. }
  280. static inline
  281. int dfl_feature_dev_use_count(struct dfl_feature_platform_data *pdata)
  282. {
  283. return pdata->open_count;
  284. }
  285. static inline
  286. void dfl_fpga_pdata_set_private(struct dfl_feature_platform_data *pdata,
  287. void *private)
  288. {
  289. pdata->private = private;
  290. }
  291. static inline
  292. void *dfl_fpga_pdata_get_private(struct dfl_feature_platform_data *pdata)
  293. {
  294. return pdata->private;
  295. }
  296. struct dfl_feature_ops {
  297. int (*init)(struct platform_device *pdev, struct dfl_feature *feature);
  298. void (*uinit)(struct platform_device *pdev,
  299. struct dfl_feature *feature);
  300. long (*ioctl)(struct platform_device *pdev, struct dfl_feature *feature,
  301. unsigned int cmd, unsigned long arg);
  302. };
  303. #define DFL_FPGA_FEATURE_DEV_FME "dfl-fme"
  304. #define DFL_FPGA_FEATURE_DEV_PORT "dfl-port"
  305. void dfl_fpga_dev_feature_uinit(struct platform_device *pdev);
  306. int dfl_fpga_dev_feature_init(struct platform_device *pdev,
  307. struct dfl_feature_driver *feature_drvs);
  308. int dfl_fpga_dev_ops_register(struct platform_device *pdev,
  309. const struct file_operations *fops,
  310. struct module *owner);
  311. void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
  312. static inline
  313. struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
  314. {
  315. struct dfl_feature_platform_data *pdata;
  316. pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
  317. cdev);
  318. return pdata->dev;
  319. }
  320. #define dfl_fpga_dev_for_each_feature(pdata, feature) \
  321. for ((feature) = (pdata)->features; \
  322. (feature) < (pdata)->features + (pdata)->num; (feature)++)
  323. static inline
  324. struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u16 id)
  325. {
  326. struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
  327. struct dfl_feature *feature;
  328. dfl_fpga_dev_for_each_feature(pdata, feature)
  329. if (feature->id == id)
  330. return feature;
  331. return NULL;
  332. }
  333. static inline
  334. void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u16 id)
  335. {
  336. struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
  337. if (feature && feature->ioaddr)
  338. return feature->ioaddr;
  339. WARN_ON(1);
  340. return NULL;
  341. }
  342. static inline bool is_dfl_feature_present(struct device *dev, u16 id)
  343. {
  344. return !!dfl_get_feature_ioaddr_by_id(dev, id);
  345. }
  346. static inline
  347. struct device *dfl_fpga_pdata_to_parent(struct dfl_feature_platform_data *pdata)
  348. {
  349. return pdata->dev->dev.parent->parent;
  350. }
  351. static inline bool dfl_feature_is_fme(void __iomem *base)
  352. {
  353. u64 v = readq(base + DFH);
  354. return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
  355. (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_FME);
  356. }
  357. static inline bool dfl_feature_is_port(void __iomem *base)
  358. {
  359. u64 v = readq(base + DFH);
  360. return (FIELD_GET(DFH_TYPE, v) == DFH_TYPE_FIU) &&
  361. (FIELD_GET(DFH_ID, v) == DFH_ID_FIU_PORT);
  362. }
  363. static inline u8 dfl_feature_revision(void __iomem *base)
  364. {
  365. return (u8)FIELD_GET(DFH_REVISION, readq(base + DFH));
  366. }
  367. /**
  368. * struct dfl_fpga_enum_info - DFL FPGA enumeration information
  369. *
  370. * @dev: parent device.
  371. * @dfls: list of device feature lists.
  372. * @nr_irqs: number of irqs for all feature devices.
  373. * @irq_table: Linux IRQ numbers for all irqs, indexed by hw irq numbers.
  374. */
  375. struct dfl_fpga_enum_info {
  376. struct device *dev;
  377. struct list_head dfls;
  378. unsigned int nr_irqs;
  379. int *irq_table;
  380. };
  381. /**
  382. * struct dfl_fpga_enum_dfl - DFL FPGA enumeration device feature list info
  383. *
  384. * @start: base address of this device feature list.
  385. * @len: size of this device feature list.
  386. * @node: node in list of device feature lists.
  387. */
  388. struct dfl_fpga_enum_dfl {
  389. resource_size_t start;
  390. resource_size_t len;
  391. struct list_head node;
  392. };
  393. struct dfl_fpga_enum_info *dfl_fpga_enum_info_alloc(struct device *dev);
  394. int dfl_fpga_enum_info_add_dfl(struct dfl_fpga_enum_info *info,
  395. resource_size_t start, resource_size_t len);
  396. int dfl_fpga_enum_info_add_irq(struct dfl_fpga_enum_info *info,
  397. unsigned int nr_irqs, int *irq_table);
  398. void dfl_fpga_enum_info_free(struct dfl_fpga_enum_info *info);
  399. /**
  400. * struct dfl_fpga_cdev - container device of DFL based FPGA
  401. *
  402. * @parent: parent device of this container device.
  403. * @region: base fpga region.
  404. * @fme_dev: FME feature device under this container device.
  405. * @lock: mutex lock to protect the port device list.
  406. * @port_dev_list: list of all port feature devices under this container device.
  407. * @released_port_num: released port number under this container device.
  408. */
  409. struct dfl_fpga_cdev {
  410. struct device *parent;
  411. struct fpga_region *region;
  412. struct device *fme_dev;
  413. struct mutex lock;
  414. struct list_head port_dev_list;
  415. int released_port_num;
  416. };
  417. struct dfl_fpga_cdev *
  418. dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info);
  419. void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
  420. /*
  421. * need to drop the device reference with put_device() after use port platform
  422. * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
  423. * functions.
  424. */
  425. struct platform_device *
  426. __dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
  427. int (*match)(struct platform_device *, void *));
  428. static inline struct platform_device *
  429. dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
  430. int (*match)(struct platform_device *, void *))
  431. {
  432. struct platform_device *pdev;
  433. mutex_lock(&cdev->lock);
  434. pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
  435. mutex_unlock(&cdev->lock);
  436. return pdev;
  437. }
  438. int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id);
  439. int dfl_fpga_cdev_assign_port(struct dfl_fpga_cdev *cdev, int port_id);
  440. void dfl_fpga_cdev_config_ports_pf(struct dfl_fpga_cdev *cdev);
  441. int dfl_fpga_cdev_config_ports_vf(struct dfl_fpga_cdev *cdev, int num_vf);
  442. int dfl_fpga_set_irq_triggers(struct dfl_feature *feature, unsigned int start,
  443. unsigned int count, int32_t *fds);
  444. long dfl_feature_ioctl_get_num_irqs(struct platform_device *pdev,
  445. struct dfl_feature *feature,
  446. unsigned long arg);
  447. long dfl_feature_ioctl_set_irq(struct platform_device *pdev,
  448. struct dfl_feature *feature,
  449. unsigned long arg);
  450. #endif /* __FPGA_DFL_H */