fpga-mgr.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 = devm_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. return fpga_mgr_register(mgr);
  47. }
  48. static int socfpga_fpga_remove(struct platform_device *pdev)
  49. {
  50. struct fpga_manager *mgr = platform_get_drvdata(pdev);
  51. fpga_mgr_unregister(mgr);
  52. return 0;
  53. }
  54. The ops will implement whatever device specific register writes are needed to
  55. do the programming sequence for this particular FPGA. These ops return 0 for
  56. success or negative error codes otherwise.
  57. The programming sequence is::
  58. 1. .write_init
  59. 2. .write or .write_sg (may be called once or multiple times)
  60. 3. .write_complete
  61. The .write_init function will prepare the FPGA to receive the image data. The
  62. buffer passed into .write_init will be at most .initial_header_size bytes long;
  63. if the whole bitstream is not immediately available then the core code will
  64. buffer up at least this much before starting.
  65. The .write function writes a buffer to the FPGA. The buffer may be contain the
  66. whole FPGA image or may be a smaller chunk of an FPGA image. In the latter
  67. case, this function is called multiple times for successive chunks. This interface
  68. is suitable for drivers which use PIO.
  69. The .write_sg version behaves the same as .write except the input is a sg_table
  70. scatter list. This interface is suitable for drivers which use DMA.
  71. The .write_complete function is called after all the image has been written
  72. to put the FPGA into operating mode.
  73. The ops include a .state function which will determine the state the FPGA is in
  74. and return a code of type enum fpga_mgr_states. It doesn't result in a change
  75. in state.
  76. How to write an image buffer to a supported FPGA
  77. ------------------------------------------------
  78. Some sample code::
  79. #include <linux/fpga/fpga-mgr.h>
  80. struct fpga_manager *mgr;
  81. struct fpga_image_info *info;
  82. int ret;
  83. /*
  84. * Get a reference to FPGA manager. The manager is not locked, so you can
  85. * hold onto this reference without it preventing programming.
  86. *
  87. * This example uses the device node of the manager. Alternatively, use
  88. * fpga_mgr_get(dev) instead if you have the device.
  89. */
  90. mgr = of_fpga_mgr_get(mgr_node);
  91. /* struct with information about the FPGA image to program. */
  92. info = fpga_image_info_alloc(dev);
  93. /* flags indicates whether to do full or partial reconfiguration */
  94. info->flags = FPGA_MGR_PARTIAL_RECONFIG;
  95. /*
  96. * At this point, indicate where the image is. This is pseudo-code; you're
  97. * going to use one of these three.
  98. */
  99. if (image is in a scatter gather table) {
  100. info->sgt = [your scatter gather table]
  101. } else if (image is in a buffer) {
  102. info->buf = [your image buffer]
  103. info->count = [image buffer size]
  104. } else if (image is in a firmware file) {
  105. info->firmware_name = devm_kstrdup(dev, firmware_name, GFP_KERNEL);
  106. }
  107. /* Get exclusive control of FPGA manager */
  108. ret = fpga_mgr_lock(mgr);
  109. /* Load the buffer to the FPGA */
  110. ret = fpga_mgr_buf_load(mgr, &info, buf, count);
  111. /* Release the FPGA manager */
  112. fpga_mgr_unlock(mgr);
  113. fpga_mgr_put(mgr);
  114. /* Deallocate the image info if you're done with it */
  115. fpga_image_info_free(info);
  116. API for implementing a new FPGA Manager driver
  117. ----------------------------------------------
  118. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  119. :functions: fpga_manager
  120. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  121. :functions: fpga_manager_ops
  122. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  123. :functions: devm_fpga_mgr_create
  124. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  125. :functions: fpga_mgr_create
  126. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  127. :functions: fpga_mgr_free
  128. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  129. :functions: fpga_mgr_register
  130. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  131. :functions: fpga_mgr_unregister
  132. API for programming an FPGA
  133. ---------------------------
  134. FPGA Manager flags
  135. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  136. :doc: FPGA Manager flags
  137. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  138. :functions: fpga_image_info
  139. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  140. :functions: fpga_mgr_states
  141. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  142. :functions: fpga_image_info_alloc
  143. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  144. :functions: fpga_image_info_free
  145. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  146. :functions: of_fpga_mgr_get
  147. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  148. :functions: fpga_mgr_get
  149. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  150. :functions: fpga_mgr_put
  151. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  152. :functions: fpga_mgr_lock
  153. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  154. :functions: fpga_mgr_unlock
  155. .. kernel-doc:: include/linux/fpga/fpga-mgr.h
  156. :functions: fpga_mgr_states
  157. Note - use :c:func:`fpga_region_program_fpga()` instead of :c:func:`fpga_mgr_load()`
  158. .. kernel-doc:: drivers/fpga/fpga-mgr.c
  159. :functions: fpga_mgr_load