610-v5.13-45-net-ethernet-mtk_eth_soc-implement-dynamic-interrupt.patch 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. From e9229ffd550b2d8c4997c67a501dbc3919fd4e26 Mon Sep 17 00:00:00 2001
  2. From: Felix Fietkau <[email protected]>
  3. Date: Thu, 22 Apr 2021 22:21:02 -0700
  4. Subject: [PATCH] net: ethernet: mtk_eth_soc: implement dynamic interrupt
  5. moderation
  6. Reduces the number of interrupts under load
  7. Signed-off-by: Felix Fietkau <[email protected]>
  8. [Ilya: add documentation for new struct fields]
  9. Signed-off-by: Ilya Lipnitskiy <[email protected]>
  10. Signed-off-by: David S. Miller <[email protected]>
  11. ---
  12. drivers/net/ethernet/mediatek/Kconfig | 1 +
  13. drivers/net/ethernet/mediatek/mtk_eth_soc.c | 96 +++++++++++++++++++--
  14. drivers/net/ethernet/mediatek/mtk_eth_soc.h | 41 +++++++--
  15. 3 files changed, 124 insertions(+), 14 deletions(-)
  16. --- a/drivers/net/ethernet/mediatek/Kconfig
  17. +++ b/drivers/net/ethernet/mediatek/Kconfig
  18. @@ -10,6 +10,7 @@ if NET_VENDOR_MEDIATEK
  19. config NET_MEDIATEK_SOC
  20. tristate "MediaTek SoC Gigabit Ethernet support"
  21. select PHYLINK
  22. + select DIMLIB
  23. help
  24. This driver supports the gigabit ethernet MACs in the
  25. MediaTek SoC family.
  26. --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  27. +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
  28. @@ -1254,12 +1254,13 @@ static void mtk_update_rx_cpu_idx(struct
  29. static int mtk_poll_rx(struct napi_struct *napi, int budget,
  30. struct mtk_eth *eth)
  31. {
  32. + struct dim_sample dim_sample = {};
  33. struct mtk_rx_ring *ring;
  34. int idx;
  35. struct sk_buff *skb;
  36. u8 *data, *new_data;
  37. struct mtk_rx_dma *rxd, trxd;
  38. - int done = 0;
  39. + int done = 0, bytes = 0;
  40. while (done < budget) {
  41. struct net_device *netdev;
  42. @@ -1333,6 +1334,7 @@ static int mtk_poll_rx(struct napi_struc
  43. else
  44. skb_checksum_none_assert(skb);
  45. skb->protocol = eth_type_trans(skb, netdev);
  46. + bytes += pktlen;
  47. if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
  48. (trxd.rxd2 & RX_DMA_VTAG))
  49. @@ -1365,6 +1367,12 @@ rx_done:
  50. mtk_update_rx_cpu_idx(eth);
  51. }
  52. + eth->rx_packets += done;
  53. + eth->rx_bytes += bytes;
  54. + dim_update_sample(eth->rx_events, eth->rx_packets, eth->rx_bytes,
  55. + &dim_sample);
  56. + net_dim(&eth->rx_dim, dim_sample);
  57. +
  58. return done;
  59. }
  60. @@ -1457,6 +1465,7 @@ static int mtk_poll_tx_pdma(struct mtk_e
  61. static int mtk_poll_tx(struct mtk_eth *eth, int budget)
  62. {
  63. struct mtk_tx_ring *ring = &eth->tx_ring;
  64. + struct dim_sample dim_sample = {};
  65. unsigned int done[MTK_MAX_DEVS];
  66. unsigned int bytes[MTK_MAX_DEVS];
  67. int total = 0, i;
  68. @@ -1474,8 +1483,14 @@ static int mtk_poll_tx(struct mtk_eth *e
  69. continue;
  70. netdev_completed_queue(eth->netdev[i], done[i], bytes[i]);
  71. total += done[i];
  72. + eth->tx_packets += done[i];
  73. + eth->tx_bytes += bytes[i];
  74. }
  75. + dim_update_sample(eth->tx_events, eth->tx_packets, eth->tx_bytes,
  76. + &dim_sample);
  77. + net_dim(&eth->tx_dim, dim_sample);
  78. +
  79. if (mtk_queue_stopped(eth) &&
  80. (atomic_read(&ring->free_count) > ring->thresh))
  81. mtk_wake_queue(eth);
  82. @@ -2150,6 +2165,7 @@ static irqreturn_t mtk_handle_irq_rx(int
  83. {
  84. struct mtk_eth *eth = _eth;
  85. + eth->rx_events++;
  86. if (likely(napi_schedule_prep(&eth->rx_napi))) {
  87. __napi_schedule(&eth->rx_napi);
  88. mtk_rx_irq_disable(eth, MTK_RX_DONE_INT);
  89. @@ -2162,6 +2178,7 @@ static irqreturn_t mtk_handle_irq_tx(int
  90. {
  91. struct mtk_eth *eth = _eth;
  92. + eth->tx_events++;
  93. if (likely(napi_schedule_prep(&eth->tx_napi))) {
  94. __napi_schedule(&eth->tx_napi);
  95. mtk_tx_irq_disable(eth, MTK_TX_DONE_INT);
  96. @@ -2346,6 +2363,9 @@ static int mtk_stop(struct net_device *d
  97. napi_disable(&eth->tx_napi);
  98. napi_disable(&eth->rx_napi);
  99. + cancel_work_sync(&eth->rx_dim.work);
  100. + cancel_work_sync(&eth->tx_dim.work);
  101. +
  102. if (MTK_HAS_CAPS(eth->soc->caps, MTK_QDMA))
  103. mtk_stop_dma(eth, MTK_QDMA_GLO_CFG);
  104. mtk_stop_dma(eth, MTK_PDMA_GLO_CFG);
  105. @@ -2398,6 +2418,64 @@ err_disable_clks:
  106. return ret;
  107. }
  108. +static void mtk_dim_rx(struct work_struct *work)
  109. +{
  110. + struct dim *dim = container_of(work, struct dim, work);
  111. + struct mtk_eth *eth = container_of(dim, struct mtk_eth, rx_dim);
  112. + struct dim_cq_moder cur_profile;
  113. + u32 val, cur;
  114. +
  115. + cur_profile = net_dim_get_rx_moderation(eth->rx_dim.mode,
  116. + dim->profile_ix);
  117. + spin_lock_bh(&eth->dim_lock);
  118. +
  119. + val = mtk_r32(eth, MTK_PDMA_DELAY_INT);
  120. + val &= MTK_PDMA_DELAY_TX_MASK;
  121. + val |= MTK_PDMA_DELAY_RX_EN;
  122. +
  123. + cur = min_t(u32, DIV_ROUND_UP(cur_profile.usec, 20), MTK_PDMA_DELAY_PTIME_MASK);
  124. + val |= cur << MTK_PDMA_DELAY_RX_PTIME_SHIFT;
  125. +
  126. + cur = min_t(u32, cur_profile.pkts, MTK_PDMA_DELAY_PINT_MASK);
  127. + val |= cur << MTK_PDMA_DELAY_RX_PINT_SHIFT;
  128. +
  129. + mtk_w32(eth, val, MTK_PDMA_DELAY_INT);
  130. + mtk_w32(eth, val, MTK_QDMA_DELAY_INT);
  131. +
  132. + spin_unlock_bh(&eth->dim_lock);
  133. +
  134. + dim->state = DIM_START_MEASURE;
  135. +}
  136. +
  137. +static void mtk_dim_tx(struct work_struct *work)
  138. +{
  139. + struct dim *dim = container_of(work, struct dim, work);
  140. + struct mtk_eth *eth = container_of(dim, struct mtk_eth, tx_dim);
  141. + struct dim_cq_moder cur_profile;
  142. + u32 val, cur;
  143. +
  144. + cur_profile = net_dim_get_tx_moderation(eth->tx_dim.mode,
  145. + dim->profile_ix);
  146. + spin_lock_bh(&eth->dim_lock);
  147. +
  148. + val = mtk_r32(eth, MTK_PDMA_DELAY_INT);
  149. + val &= MTK_PDMA_DELAY_RX_MASK;
  150. + val |= MTK_PDMA_DELAY_TX_EN;
  151. +
  152. + cur = min_t(u32, DIV_ROUND_UP(cur_profile.usec, 20), MTK_PDMA_DELAY_PTIME_MASK);
  153. + val |= cur << MTK_PDMA_DELAY_TX_PTIME_SHIFT;
  154. +
  155. + cur = min_t(u32, cur_profile.pkts, MTK_PDMA_DELAY_PINT_MASK);
  156. + val |= cur << MTK_PDMA_DELAY_TX_PINT_SHIFT;
  157. +
  158. + mtk_w32(eth, val, MTK_PDMA_DELAY_INT);
  159. + mtk_w32(eth, val, MTK_QDMA_DELAY_INT);
  160. +
  161. + spin_unlock_bh(&eth->dim_lock);
  162. +
  163. + dim->state = DIM_START_MEASURE;
  164. +}
  165. +
  166. static int mtk_hw_init(struct mtk_eth *eth)
  167. {
  168. int i, val, ret;
  169. @@ -2419,9 +2497,6 @@ static int mtk_hw_init(struct mtk_eth *e
  170. goto err_disable_pm;
  171. }
  172. - /* enable interrupt delay for RX */
  173. - mtk_w32(eth, MTK_PDMA_DELAY_RX_DELAY, MTK_PDMA_DELAY_INT);
  174. -
  175. /* disable delay and normal interrupt */
  176. mtk_tx_irq_disable(eth, ~0);
  177. mtk_rx_irq_disable(eth, ~0);
  178. @@ -2460,11 +2535,11 @@ static int mtk_hw_init(struct mtk_eth *e
  179. /* Enable RX VLan Offloading */
  180. mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
  181. - /* enable interrupt delay for RX */
  182. - mtk_w32(eth, MTK_PDMA_DELAY_RX_DELAY, MTK_PDMA_DELAY_INT);
  183. + /* set interrupt delays based on current Net DIM sample */
  184. + mtk_dim_rx(&eth->rx_dim.work);
  185. + mtk_dim_tx(&eth->tx_dim.work);
  186. /* disable delay and normal interrupt */
  187. - mtk_w32(eth, 0, MTK_QDMA_DELAY_INT);
  188. mtk_tx_irq_disable(eth, ~0);
  189. mtk_rx_irq_disable(eth, ~0);
  190. @@ -2969,6 +3044,13 @@ static int mtk_probe(struct platform_dev
  191. spin_lock_init(&eth->page_lock);
  192. spin_lock_init(&eth->tx_irq_lock);
  193. spin_lock_init(&eth->rx_irq_lock);
  194. + spin_lock_init(&eth->dim_lock);
  195. +
  196. + eth->rx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  197. + INIT_WORK(&eth->rx_dim.work, mtk_dim_rx);
  198. +
  199. + eth->tx_dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  200. + INIT_WORK(&eth->tx_dim.work, mtk_dim_tx);
  201. if (!MTK_HAS_CAPS(eth->soc->caps, MTK_SOC_MT7628)) {
  202. eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  203. --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
  204. +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
  205. @@ -16,6 +16,7 @@
  206. #include <linux/refcount.h>
  207. #include <linux/phylink.h>
  208. #include <linux/rhashtable.h>
  209. +#include <linux/dim.h>
  210. #include "mtk_ppe.h"
  211. #define MTK_QDMA_PAGE_SIZE 2048
  212. @@ -136,13 +137,18 @@
  213. /* PDMA Delay Interrupt Register */
  214. #define MTK_PDMA_DELAY_INT 0xa0c
  215. +#define MTK_PDMA_DELAY_RX_MASK GENMASK(15, 0)
  216. #define MTK_PDMA_DELAY_RX_EN BIT(15)
  217. -#define MTK_PDMA_DELAY_RX_PINT 4
  218. #define MTK_PDMA_DELAY_RX_PINT_SHIFT 8
  219. -#define MTK_PDMA_DELAY_RX_PTIME 4
  220. -#define MTK_PDMA_DELAY_RX_DELAY \
  221. - (MTK_PDMA_DELAY_RX_EN | MTK_PDMA_DELAY_RX_PTIME | \
  222. - (MTK_PDMA_DELAY_RX_PINT << MTK_PDMA_DELAY_RX_PINT_SHIFT))
  223. +#define MTK_PDMA_DELAY_RX_PTIME_SHIFT 0
  224. +
  225. +#define MTK_PDMA_DELAY_TX_MASK GENMASK(31, 16)
  226. +#define MTK_PDMA_DELAY_TX_EN BIT(31)
  227. +#define MTK_PDMA_DELAY_TX_PINT_SHIFT 24
  228. +#define MTK_PDMA_DELAY_TX_PTIME_SHIFT 16
  229. +
  230. +#define MTK_PDMA_DELAY_PINT_MASK 0x7f
  231. +#define MTK_PDMA_DELAY_PTIME_MASK 0xff
  232. /* PDMA Interrupt Status Register */
  233. #define MTK_PDMA_INT_STATUS 0xa20
  234. @@ -224,6 +230,7 @@
  235. /* QDMA Interrupt Status Register */
  236. #define MTK_QDMA_INT_STATUS 0x1A18
  237. #define MTK_RX_DONE_DLY BIT(30)
  238. +#define MTK_TX_DONE_DLY BIT(28)
  239. #define MTK_RX_DONE_INT3 BIT(19)
  240. #define MTK_RX_DONE_INT2 BIT(18)
  241. #define MTK_RX_DONE_INT1 BIT(17)
  242. @@ -233,8 +240,7 @@
  243. #define MTK_TX_DONE_INT1 BIT(1)
  244. #define MTK_TX_DONE_INT0 BIT(0)
  245. #define MTK_RX_DONE_INT MTK_RX_DONE_DLY
  246. -#define MTK_TX_DONE_INT (MTK_TX_DONE_INT0 | MTK_TX_DONE_INT1 | \
  247. - MTK_TX_DONE_INT2 | MTK_TX_DONE_INT3)
  248. +#define MTK_TX_DONE_INT MTK_TX_DONE_DLY
  249. /* QDMA Interrupt grouping registers */
  250. #define MTK_QDMA_INT_GRP1 0x1a20
  251. @@ -863,6 +869,7 @@ struct mtk_sgmii {
  252. * @page_lock: Make sure that register operations are atomic
  253. * @tx_irq__lock: Make sure that IRQ register operations are atomic
  254. * @rx_irq__lock: Make sure that IRQ register operations are atomic
  255. + * @dim_lock: Make sure that Net DIM operations are atomic
  256. * @dummy_dev: we run 2 netdevs on 1 physical DMA ring and need a
  257. * dummy for NAPI to work
  258. * @netdev: The netdev instances
  259. @@ -881,6 +888,14 @@ struct mtk_sgmii {
  260. * @rx_ring_qdma: Pointer to the memory holding info about the QDMA RX ring
  261. * @tx_napi: The TX NAPI struct
  262. * @rx_napi: The RX NAPI struct
  263. + * @rx_events: Net DIM RX event counter
  264. + * @rx_packets: Net DIM RX packet counter
  265. + * @rx_bytes: Net DIM RX byte counter
  266. + * @rx_dim: Net DIM RX context
  267. + * @tx_events: Net DIM TX event counter
  268. + * @tx_packets: Net DIM TX packet counter
  269. + * @tx_bytes: Net DIM TX byte counter
  270. + * @tx_dim: Net DIM TX context
  271. * @scratch_ring: Newer SoCs need memory for a second HW managed TX ring
  272. * @phy_scratch_ring: physical address of scratch_ring
  273. * @scratch_head: The scratch memory that scratch_ring points to.
  274. @@ -925,6 +940,18 @@ struct mtk_eth {
  275. const struct mtk_soc_data *soc;
  276. + spinlock_t dim_lock;
  277. +
  278. + u32 rx_events;
  279. + u32 rx_packets;
  280. + u32 rx_bytes;
  281. + struct dim rx_dim;
  282. +
  283. + u32 tx_events;
  284. + u32 tx_packets;
  285. + u32 tx_bytes;
  286. + struct dim tx_dim;
  287. +
  288. u32 tx_int_mask_reg;
  289. u32 tx_int_status_reg;
  290. u32 rx_dma_l4_valid;