fpga-mgr.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. FPGA Manager
  2. ============
  3. Overview
  4. --------
  5. The FPGA manager core exports a set of functions for programming an FPGA with
  6. an image. The API is manufacturer agnostic. All manufacturer specifics are
  7. hidden away in a low level driver which registers a set of ops with the core.
  8. The FPGA image data itself is very manufacturer specific, but for our purposes
  9. it's just binary data. The FPGA manager core won't parse it.
  10. The FPGA image to be programmed can be in a scatter gather list, a single
  11. contiguous buffer, or a firmware file. Because allocating contiguous kernel
  12. memory for the buffer should be avoided, users are encouraged to use a scatter
  13. gather list instead if possible.
  14. The particulars for programming the image are presented in a structure (struct
  15. fpga_image_info). This struct contains parameters such as pointers to the
  16. FPGA image as well as image-specific particulars such as whether the image was
  17. built for full or partial reconfiguration.
  18. How to support a new FPGA device
  19. --------------------------------
  20. To add another FPGA manager, write a driver that implements a set of ops. The
  21. probe function calls fpga_mgr_register(), such as::
  22. static const struct fpga_manager_ops socfpga_fpga_ops = {
  23. .write_init = socfpga_fpga_ops_configure_init,
  24. .write = socfpga_fpga_ops_configure_write,
  25. .write_complete = socfpga_fpga_ops_configure_complete,
  26. .state = socfpga_fpga_ops_state,
  27. };
  28. static int socfpga_fpga_probe(struct platform_device *pdev)
  29. {
  30. struct device *dev = &pdev->dev;
  31. struct socfpga_fpga_priv *priv;
  32. struct fpga_manager *mgr;
  33. int ret;
  34. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  35. if (!priv)
  36. return -ENOMEM;
  37. /*
  38. * do ioremaps, get interrupts, etc. and save
  39. * them in priv
  40. */
  41. mgr = fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
  42. &socfpga_fpga_ops, priv);
  43. if (!mgr)
  44. return -ENOMEM;
  45. platform_set_drvdata(pdev, mgr);
  46. ret = fpga_mgr_register(mgr);
  47. if (ret)
  48. fpga_mgr_free(mgr);
  49. return ret;
  50. }
  51. static int socfpga_fpga_remove(struct platform_device *pdev)
  52. {
  53. struct fpga_manager *mgr = platform_get_drvdata(pdev);
  54. fpga_mgr_unregister(mgr);
  55. return 0;
  56. }
  57. The ops will implement whatever device specific register writes are needed to
  58. do the programming sequence for this particular FPGA. These ops return 0 for
  59. success or negative error codes otherwise.
  60. The programming sequence is::
  61. 1. .write_init
  62. 2. .write or .write_sg (may be called once or multiple times)
  63. 3. .write_complete
  64. The .write_init function will prepare the FPGA to receive the image data. The
  65. buffer passed into .write_init will be at most .initial_header_size bytes long;
  66. if the whole bitstream is not immediately available then the core code will
  67. buffer up at least this much before starting.
  68. The .write function writes a buffer to the FPGA. The buffer may be contain the
  69. whole FPGA image or may be a smaller chunk of an FPGA image. In the latter
  70. case, this function is called multiple times for successive chunks. This interface
  71. is suitable for drivers which use PIO.
  72. The .write_sg version behaves the same as .write except the input is a sg_table
  73. scatter list. This interface is suitable for drivers which use DMA.
  74. The .write_complete function is called after all the image has been written
  75. to put the FPGA into operating mode.
  76. The ops include a .state function which will determine the state the FPGA is in
  77. and return a code of type enum fpga_mgr_states. It doesn't result in a change
  78. in state.
  79. How to write an image buffer to a supported FPGA
  80. ------------------------------------------------
  81. Some sample code::
  82. #include <linux/fpga/fpga-mgr.h>
  83. struct fpga_manager *mgr;
  84. struct fpga_image_info *info;
  85. int ret;
  86. /*
  87. * Get a reference to FPGA manager. The manager is not locked, so you can
  88. * hold onto this reference without it preventing programming.
  89. *
  90. * This example uses the device node of the manager. Alternatively, use
  91. * fpga_mgr_get(dev) instead if you have the device.
  92. */
  93. mgr = of_fpga_mgr_get(mgr_node);
  94. /* struct with information about the FPGA image to program. */
  95. info = fpga_image_info_alloc(dev);
  96. /* flags indicates whether to do full or partial reconfiguration */
  97. info->flags = FPGA_MGR_PARTIAL_RECONFIG;
  98. /*
  99. * At this point, indicate where the image is. This is pseudo-code; you're
  100. * going to use one of these three.
  101. */
  102. if (image is in a scatter gather table) {
  103. info->sgt = [your scatter gather table]
  104. } else if (image is in a buffer) {
  105. info->buf = [your image buffer]
  106. info->count = [image buffer size]
  107. } else if (image is in a firmware file) {
  108. info->firmware_name = devm_kstrdup(dev, firmware_name, GFP_KERNEL);
  109. }
  110. /* Get exclusive control of FPGA manager */
  111. ret = fpga_mgr_lock(mgr);
  112. /* Load the buffer to the FPGA */
  113. ret = fpga_mgr_buf_load(mgr, &info, buf, count);
  114. /* Release the FPGA manager */
  115. fpga_mgr_unlock(mgr);
  116. fpga_mgr_put(mgr);
  117. /* Deallocate the image info if you're done with it */
  118. fpga_image_info_free(info);
  119. API for implementing a new FPGA Manager driver
  120. ----------------------------------------------
  121. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  122. :functions: fpga_manager
  123. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  124. :functions: fpga_manager_ops
  125. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  126. :functions: fpga_mgr_create
  127. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  128. :functions: fpga_mgr_free
  129. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  130. :functions: fpga_mgr_register
  131. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  132. :functions: fpga_mgr_unregister
  133. API for programming an FPGA
  134. ---------------------------
  135. FPGA Manager flags
  136. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  137. :doc: FPGA Manager flags
  138. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  139. :functions: fpga_image_info
  140. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  141. :functions: fpga_mgr_states
  142. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  143. :functions: fpga_image_info_alloc
  144. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  145. :functions: fpga_image_info_free
  146. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  147. :functions: of_fpga_mgr_get
  148. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  149. :functions: fpga_mgr_get
  150. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  151. :functions: fpga_mgr_put
  152. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  153. :functions: fpga_mgr_lock
  154. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  155. :functions: fpga_mgr_unlock
  156. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  157. :functions: fpga_mgr_states
  158. Note - use :c:func:`fpga_region_program_fpga()` instead of :c:func:`fpga_mgr_load()`
  159. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  160. :functions: fpga_mgr_load