fpga-mgr.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * FPGA Framework
  4. *
  5. * Copyright (C) 2013-2016 Altera Corporation
  6. * Copyright (C) 2017 Intel Corporation
  7. */
  8. #ifndef _LINUX_FPGA_MGR_H
  9. #define _LINUX_FPGA_MGR_H
  10. #include <linux/mutex.h>
  11. #include <linux/platform_device.h>
  12. struct fpga_manager;
  13. struct sg_table;
  14. /**
  15. * enum fpga_mgr_states - fpga framework states
  16. * @FPGA_MGR_STATE_UNKNOWN: can't determine state
  17. * @FPGA_MGR_STATE_POWER_OFF: FPGA power is off
  18. * @FPGA_MGR_STATE_POWER_UP: FPGA reports power is up
  19. * @FPGA_MGR_STATE_RESET: FPGA in reset state
  20. * @FPGA_MGR_STATE_FIRMWARE_REQ: firmware request in progress
  21. * @FPGA_MGR_STATE_FIRMWARE_REQ_ERR: firmware request failed
  22. * @FPGA_MGR_STATE_PARSE_HEADER: parse FPGA image header
  23. * @FPGA_MGR_STATE_PARSE_HEADER_ERR: Error during PARSE_HEADER stage
  24. * @FPGA_MGR_STATE_WRITE_INIT: preparing FPGA for programming
  25. * @FPGA_MGR_STATE_WRITE_INIT_ERR: Error during WRITE_INIT stage
  26. * @FPGA_MGR_STATE_WRITE: writing image to FPGA
  27. * @FPGA_MGR_STATE_WRITE_ERR: Error while writing FPGA
  28. * @FPGA_MGR_STATE_WRITE_COMPLETE: Doing post programming steps
  29. * @FPGA_MGR_STATE_WRITE_COMPLETE_ERR: Error during WRITE_COMPLETE
  30. * @FPGA_MGR_STATE_OPERATING: FPGA is programmed and operating
  31. */
  32. enum fpga_mgr_states {
  33. /* default FPGA states */
  34. FPGA_MGR_STATE_UNKNOWN,
  35. FPGA_MGR_STATE_POWER_OFF,
  36. FPGA_MGR_STATE_POWER_UP,
  37. FPGA_MGR_STATE_RESET,
  38. /* getting an image for loading */
  39. FPGA_MGR_STATE_FIRMWARE_REQ,
  40. FPGA_MGR_STATE_FIRMWARE_REQ_ERR,
  41. /* write sequence: parse header, init, write, complete */
  42. FPGA_MGR_STATE_PARSE_HEADER,
  43. FPGA_MGR_STATE_PARSE_HEADER_ERR,
  44. FPGA_MGR_STATE_WRITE_INIT,
  45. FPGA_MGR_STATE_WRITE_INIT_ERR,
  46. FPGA_MGR_STATE_WRITE,
  47. FPGA_MGR_STATE_WRITE_ERR,
  48. FPGA_MGR_STATE_WRITE_COMPLETE,
  49. FPGA_MGR_STATE_WRITE_COMPLETE_ERR,
  50. /* fpga is programmed and operating */
  51. FPGA_MGR_STATE_OPERATING,
  52. };
  53. /**
  54. * DOC: FPGA Manager flags
  55. *
  56. * Flags used in the &fpga_image_info->flags field
  57. *
  58. * %FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
  59. *
  60. * %FPGA_MGR_EXTERNAL_CONFIG: FPGA has been configured prior to Linux booting
  61. *
  62. * %FPGA_MGR_ENCRYPTED_BITSTREAM: indicates bitstream is encrypted
  63. *
  64. * %FPGA_MGR_BITSTREAM_LSB_FIRST: SPI bitstream bit order is LSB first
  65. *
  66. * %FPGA_MGR_COMPRESSED_BITSTREAM: FPGA bitstream is compressed
  67. */
  68. #define FPGA_MGR_PARTIAL_RECONFIG BIT(0)
  69. #define FPGA_MGR_EXTERNAL_CONFIG BIT(1)
  70. #define FPGA_MGR_ENCRYPTED_BITSTREAM BIT(2)
  71. #define FPGA_MGR_BITSTREAM_LSB_FIRST BIT(3)
  72. #define FPGA_MGR_COMPRESSED_BITSTREAM BIT(4)
  73. /**
  74. * struct fpga_image_info - information specific to an FPGA image
  75. * @flags: boolean flags as defined above
  76. * @enable_timeout_us: maximum time to enable traffic through bridge (uSec)
  77. * @disable_timeout_us: maximum time to disable traffic through bridge (uSec)
  78. * @config_complete_timeout_us: maximum time for FPGA to switch to operating
  79. * status in the write_complete op.
  80. * @firmware_name: name of FPGA image firmware file
  81. * @sgt: scatter/gather table containing FPGA image
  82. * @buf: contiguous buffer containing FPGA image
  83. * @count: size of buf
  84. * @header_size: size of image header.
  85. * @data_size: size of image data to be sent to the device. If not specified,
  86. * whole image will be used. Header may be skipped in either case.
  87. * @region_id: id of target region
  88. * @dev: device that owns this
  89. * @overlay: Device Tree overlay
  90. */
  91. struct fpga_image_info {
  92. u32 flags;
  93. u32 enable_timeout_us;
  94. u32 disable_timeout_us;
  95. u32 config_complete_timeout_us;
  96. char *firmware_name;
  97. struct sg_table *sgt;
  98. const char *buf;
  99. size_t count;
  100. size_t header_size;
  101. size_t data_size;
  102. int region_id;
  103. struct device *dev;
  104. #ifdef CONFIG_OF
  105. struct device_node *overlay;
  106. #endif
  107. };
  108. /**
  109. * struct fpga_compat_id - id for compatibility check
  110. *
  111. * @id_h: high 64bit of the compat_id
  112. * @id_l: low 64bit of the compat_id
  113. */
  114. struct fpga_compat_id {
  115. u64 id_h;
  116. u64 id_l;
  117. };
  118. /**
  119. * struct fpga_manager_info - collection of parameters for an FPGA Manager
  120. * @name: fpga manager name
  121. * @compat_id: FPGA manager id for compatibility check.
  122. * @mops: pointer to structure of fpga manager ops
  123. * @priv: fpga manager private data
  124. *
  125. * fpga_manager_info contains parameters for the register_full function.
  126. * These are separated into an info structure because they some are optional
  127. * others could be added to in the future. The info structure facilitates
  128. * maintaining a stable API.
  129. */
  130. struct fpga_manager_info {
  131. const char *name;
  132. struct fpga_compat_id *compat_id;
  133. const struct fpga_manager_ops *mops;
  134. void *priv;
  135. };
  136. /**
  137. * struct fpga_manager_ops - ops for low level fpga manager drivers
  138. * @initial_header_size: minimum number of bytes that should be passed into
  139. * parse_header and write_init.
  140. * @skip_header: bool flag to tell fpga-mgr core whether it should skip
  141. * info->header_size part at the beginning of the image when invoking
  142. * write callback.
  143. * @state: returns an enum value of the FPGA's state
  144. * @status: returns status of the FPGA, including reconfiguration error code
  145. * @parse_header: parse FPGA image header to set info->header_size and
  146. * info->data_size. In case the input buffer is not large enough, set
  147. * required size to info->header_size and return -EAGAIN.
  148. * @write_init: prepare the FPGA to receive configuration data
  149. * @write: write count bytes of configuration data to the FPGA
  150. * @write_sg: write the scatter list of configuration data to the FPGA
  151. * @write_complete: set FPGA to operating state after writing is done
  152. * @fpga_remove: optional: Set FPGA into a specific state during driver remove
  153. * @groups: optional attribute groups.
  154. *
  155. * fpga_manager_ops are the low level functions implemented by a specific
  156. * fpga manager driver. The optional ones are tested for NULL before being
  157. * called, so leaving them out is fine.
  158. */
  159. struct fpga_manager_ops {
  160. size_t initial_header_size;
  161. bool skip_header;
  162. enum fpga_mgr_states (*state)(struct fpga_manager *mgr);
  163. u64 (*status)(struct fpga_manager *mgr);
  164. int (*parse_header)(struct fpga_manager *mgr,
  165. struct fpga_image_info *info,
  166. const char *buf, size_t count);
  167. int (*write_init)(struct fpga_manager *mgr,
  168. struct fpga_image_info *info,
  169. const char *buf, size_t count);
  170. int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
  171. int (*write_sg)(struct fpga_manager *mgr, struct sg_table *sgt);
  172. int (*write_complete)(struct fpga_manager *mgr,
  173. struct fpga_image_info *info);
  174. void (*fpga_remove)(struct fpga_manager *mgr);
  175. const struct attribute_group **groups;
  176. };
  177. /* FPGA manager status: Partial/Full Reconfiguration errors */
  178. #define FPGA_MGR_STATUS_OPERATION_ERR BIT(0)
  179. #define FPGA_MGR_STATUS_CRC_ERR BIT(1)
  180. #define FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR BIT(2)
  181. #define FPGA_MGR_STATUS_IP_PROTOCOL_ERR BIT(3)
  182. #define FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR BIT(4)
  183. /**
  184. * struct fpga_manager - fpga manager structure
  185. * @name: name of low level fpga manager
  186. * @dev: fpga manager device
  187. * @ref_mutex: only allows one reference to fpga manager
  188. * @state: state of fpga manager
  189. * @compat_id: FPGA manager id for compatibility check.
  190. * @mops: pointer to struct of fpga manager ops
  191. * @priv: low level driver private date
  192. */
  193. struct fpga_manager {
  194. const char *name;
  195. struct device dev;
  196. struct mutex ref_mutex;
  197. enum fpga_mgr_states state;
  198. struct fpga_compat_id *compat_id;
  199. const struct fpga_manager_ops *mops;
  200. void *priv;
  201. };
  202. #define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
  203. struct fpga_image_info *fpga_image_info_alloc(struct device *dev);
  204. void fpga_image_info_free(struct fpga_image_info *info);
  205. int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info);
  206. int fpga_mgr_lock(struct fpga_manager *mgr);
  207. void fpga_mgr_unlock(struct fpga_manager *mgr);
  208. struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
  209. struct fpga_manager *fpga_mgr_get(struct device *dev);
  210. void fpga_mgr_put(struct fpga_manager *mgr);
  211. struct fpga_manager *
  212. fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info);
  213. struct fpga_manager *
  214. fpga_mgr_register(struct device *parent, const char *name,
  215. const struct fpga_manager_ops *mops, void *priv);
  216. void fpga_mgr_unregister(struct fpga_manager *mgr);
  217. struct fpga_manager *
  218. devm_fpga_mgr_register_full(struct device *parent, const struct fpga_manager_info *info);
  219. struct fpga_manager *
  220. devm_fpga_mgr_register(struct device *parent, const char *name,
  221. const struct fpga_manager_ops *mops, void *priv);
  222. #endif /*_LINUX_FPGA_MGR_H */