fxmac_bdring.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fxmac_bdring.h
  15. * Date: 2022-04-06 14:46:52
  16. * LastEditTime: 2022-04-06 14:46:58
  17. * Description:  This file is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #ifndef DRIVERS_ETH_F_XMAC_BDRING_H
  24. #define DRIVERS_ETH_F_XMAC_BDRING_H
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. #include "fxmac_bd.h"
  30. #include "ftypes.h"
  31. /**************************** Type Definitions *******************************/
  32. /** This is an internal structure used to maintain the DMA list */
  33. typedef struct
  34. {
  35. uintptr phys_base_addr; /* Physical address of 1st BD in list */
  36. uintptr base_bd_addr; /* Virtual address of 1st BD in list */
  37. uintptr high_bd_addr; /* Virtual address of last BD in the list */
  38. u32 length; /* Total size of ring in bytes */
  39. u32 run_state; /* Flag to indicate DMA is started */
  40. u32 separation; /* Number of bytes between the starting address
  41. of adjacent BDs */
  42. FXmacBd *free_head;
  43. /* First BD in the free group */
  44. FXmacBd *pre_head; /* First BD in the pre-work group */
  45. FXmacBd *hw_head; /* First BD in the work group */
  46. FXmacBd *hw_tail; /* Last BD in the work group */
  47. FXmacBd *post_head;
  48. /* First BD in the post-work group */
  49. FXmacBd *bda_restart;
  50. /* BDA to load when channel is started */
  51. volatile u32 hw_cnt; /* Number of BDs in work group */
  52. u32 pre_cnt; /* Number of BDs in pre-work group */
  53. u32 free_cnt; /* Number of allocatable BDs in the free group */
  54. u32 post_cnt; /* Number of BDs in post-work group */
  55. u32 all_cnt; /* Total Number of BDs for channel */
  56. } FXmacBdRing;
  57. /**
  58. * @name: FXMAC_BD_RING_NEXT
  59. * @msg: Return the next BD from bd_ptr in a list.
  60. *
  61. * @param ring_ptr is the DMA channel to operate on.
  62. * @param bd_ptr is the BD to operate on.
  63. * @return The next BD in the list relative to the bd_ptr parameter.
  64. */
  65. #define FXMAC_BD_RING_NEXT(ring_ptr, bd_ptr) \
  66. (((uintptr)((void *)(bd_ptr)) >= (ring_ptr)->high_bd_addr) ? (FXmacBd *)((void *)(ring_ptr)->base_bd_addr) : (FXmacBd *)((uintptr)((void *)(bd_ptr)) + (ring_ptr)->separation))
  67. /**
  68. * @name: FXMAC_BD_RING_CNT_CALC
  69. * @msg: Use this macro at initialization time to determine how many BDs will fit
  70. * in a BD list within the given memory constraints.
  71. *
  72. * @param alignment specifies what byte alignment the BDs must fall on and
  73. * must be a power of 2 to get an accurate calculation (32, 64, 128,...)
  74. * @param Bytes is the number of bytes to be used to store BDs.
  75. * @return Number of BDs that can fit in the given memory area
  76. */
  77. #define FXMAC_BD_RING_CNT_CALC(alignment, Bytes) \
  78. (u32)((Bytes) / (sizeof(FXmacBd)))
  79. /**
  80. * @name: FXMAC_BD_RING_MEM_CALC
  81. * @msg: Use this macro at initialization time to determine how many bytes of memory
  82. * is required to contain a given number of BDs at a given alignment.
  83. * @param alignment specifies what byte alignment the BDs must fall on. This
  84. * parameter must be a power of 2 to get an accurate calculation (32, 64,
  85. * 128,...)
  86. * @param num_bd is the number of BDs to calculate memory size requirements for
  87. * @return The number of bytes of memory required to create a BD list with the
  88. * given memory constraints.
  89. */
  90. #define FXMAC_BD_RING_MEM_CALC(alignment, num_bd) \
  91. (u32)(sizeof(FXmacBd) * (num_bd))
  92. /**
  93. * @name: FXMAC_BD_RING_GET_CNT
  94. * @msg: Return the total number of BDs allocated by this channel with
  95. * FXmacBdRingCreate().
  96. * @param ring_ptr is the DMA channel to operate on.
  97. * @return The total number of BDs allocated for this channel.
  98. */
  99. #define FXMAC_BD_RING_GET_CNT(ring_ptr) ((ring_ptr)->all_cnt)
  100. /**
  101. * @name: FXMAC_BD_RING_GET_FREE_CNT
  102. * @msg: Return the number of BDs allocatable with FXmacBdRingAlloc() for pre-
  103. * processing.
  104. *
  105. * @param ring_ptr is the DMA channel to operate on.
  106. * @return The number of BDs currently allocatable.
  107. */
  108. #define FXMAC_BD_RING_GET_FREE_CNT(ring_ptr) ((ring_ptr)->free_cnt)
  109. /**
  110. * @name: FXMAC_BD_RING_PREV
  111. * @msg: Return the previous BD from bd_ptr in the list.
  112. * @param ring_ptr is the DMA channel to operate on.
  113. * @param bd_ptr is the BD to operate on
  114. * @return The previous BD in the list relative to the bd_ptr parameter.
  115. */
  116. #define FXMAC_BD_RING_PREV(ring_ptr, bd_ptr) \
  117. (((uintptr)(bd_ptr) <= (ring_ptr)->base_bd_addr) ? (FXmacBd *)(ring_ptr)->high_bd_addr : (FXmacBd *)((uintptr)(bd_ptr) - (ring_ptr)->separation))
  118. /************************** Function Prototypes ******************************/
  119. /*
  120. * Scatter gather DMA related functions in FXmacbdring.c
  121. */
  122. FError FXmacBdRingCreate(FXmacBdRing *ring_ptr, uintptr phys_addr,
  123. uintptr virt_addr, u32 alignment, u32 bd_count);
  124. FError FXmacBdRingClone(FXmacBdRing *ring_ptr, FXmacBd *src_bd_ptr,
  125. u8 direction);
  126. FError FXmacBdRingAlloc(FXmacBdRing *ring_ptr, u32 num_bd,
  127. FXmacBd **bd_set_ptr);
  128. FError FXmacBdRingUnAlloc(FXmacBdRing *ring_ptr, u32 num_bd,
  129. FXmacBd *bd_set_ptr);
  130. FError FXmacBdRingToHw(FXmacBdRing *ring_ptr, u32 num_bd,
  131. FXmacBd *bd_set_ptr);
  132. FError FXmacBdRingFree(FXmacBdRing *ring_ptr, u32 num_bd,
  133. FXmacBd *bd_set_ptr);
  134. u32 FXmacBdRingFromHwTx(FXmacBdRing *ring_ptr, u32 bd_limit,
  135. FXmacBd **bd_set_ptr);
  136. u32 FXmacBdRingFromHwRx(FXmacBdRing *ring_ptr, u32 bd_limit,
  137. FXmacBd **bd_set_ptr);
  138. FError FXmacBdRingCheck(FXmacBdRing *ring_ptr, u8 direction);
  139. void FXmacBdringPtrReset(FXmacBdRing *ring_ptr, void *virt_addrloc);
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif