developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2023 Mediatek Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Author: Ren-Ting Wang <ren-ting.wang@mediatek.com> |
| 6 | */ |
| 7 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 8 | #include <linux/bitmap.h> |
| 9 | #include <linux/bitops.h> |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 10 | #include <linux/err.h> |
| 11 | #include <linux/lockdep.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | |
| 14 | #include "pce/cls.h" |
| 15 | #include "pce/internal.h" |
| 16 | #include "pce/netsys.h" |
| 17 | |
developer | 732266e | 2024-07-26 13:39:37 +0800 | [diff] [blame^] | 18 | static RAW_NOTIFIER_HEAD(cls_notif_chain); |
| 19 | |
| 20 | int mtk_pce_register_cls_notifier(struct notifier_block *nb) |
| 21 | { |
| 22 | int ret; |
| 23 | |
| 24 | ret = raw_notifier_chain_register(&cls_notif_chain, nb); |
| 25 | |
| 26 | return ret; |
| 27 | } |
| 28 | EXPORT_SYMBOL(mtk_pce_register_cls_notifier); |
| 29 | |
| 30 | int mtk_pce_unregister_cls_notifier(struct notifier_block *nb) |
| 31 | { |
| 32 | int ret; |
| 33 | |
| 34 | ret = raw_notifier_chain_unregister(&cls_notif_chain, nb); |
| 35 | |
| 36 | return ret; |
| 37 | } |
| 38 | EXPORT_SYMBOL(mtk_pce_unregister_cls_notifier); |
| 39 | |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 40 | struct cls_hw { |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 41 | struct cls_entry cls_tbl[FE_MEM_CLS_MAX_INDEX]; |
| 42 | DECLARE_BITMAP(cls_used, FE_MEM_CLS_MAX_INDEX); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 43 | spinlock_t lock; |
| 44 | }; |
| 45 | |
| 46 | struct cls_hw cls_hw; |
| 47 | |
| 48 | int mtk_pce_cls_enable(void) |
| 49 | { |
| 50 | mtk_pce_netsys_setbits(GLO_MEM_CFG, GDM_CLS_EN); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | void mtk_pce_cls_disable(void) |
| 56 | { |
| 57 | mtk_pce_netsys_clrbits(GLO_MEM_CFG, GDM_CLS_EN); |
| 58 | } |
| 59 | |
| 60 | static void mtk_pce_cls_clean_up(void) |
| 61 | { |
| 62 | struct fe_mem_msg msg = { |
| 63 | .cmd = FE_MEM_CMD_WRITE, |
| 64 | .type = FE_MEM_TYPE_CLS, |
| 65 | }; |
| 66 | unsigned long flag; |
| 67 | int ret = 0; |
| 68 | u32 i = 0; |
| 69 | |
| 70 | memset(&msg.raw, 0, sizeof(msg.raw)); |
| 71 | |
| 72 | spin_lock_irqsave(&cls_hw.lock, flag); |
| 73 | |
| 74 | /* clean up cls table */ |
| 75 | for (i = 0; i < FE_MEM_CLS_MAX_INDEX; i++) { |
| 76 | msg.index = i; |
| 77 | ret = mtk_pce_fe_mem_msg_send(&msg); |
| 78 | if (ret) |
| 79 | goto unlock; |
| 80 | } |
| 81 | |
| 82 | unlock: |
| 83 | spin_unlock_irqrestore(&cls_hw.lock, flag); |
| 84 | } |
| 85 | |
| 86 | int mtk_pce_cls_init(struct platform_device *pdev) |
| 87 | { |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 88 | u32 i; |
| 89 | |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 90 | spin_lock_init(&cls_hw.lock); |
| 91 | |
| 92 | mtk_pce_cls_clean_up(); |
| 93 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 94 | for (i = 0; i < FE_MEM_CLS_MAX_INDEX; i++) |
| 95 | cls_hw.cls_tbl[i].idx = i + 1; |
| 96 | |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | void mtk_pce_cls_deinit(struct platform_device *pdev) |
| 101 | { |
| 102 | mtk_pce_cls_clean_up(); |
| 103 | } |
| 104 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 105 | int mtk_pce_cls_desc_read(struct cls_desc *cdesc, u32 idx) |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 106 | { |
| 107 | struct fe_mem_msg msg; |
| 108 | int ret; |
| 109 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 110 | if (unlikely(!cdesc || !idx || idx >= FE_MEM_CLS_MAX_INDEX)) |
| 111 | return -EINVAL; |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 112 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 113 | mtk_pce_fe_mem_msg_config(&msg, FE_MEM_CMD_READ, FE_MEM_TYPE_CLS, idx); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 114 | |
| 115 | memset(&msg.raw, 0, sizeof(msg.raw)); |
| 116 | |
| 117 | ret = mtk_pce_fe_mem_msg_send(&msg); |
| 118 | if (ret) |
| 119 | return ret; |
| 120 | |
| 121 | memcpy(cdesc, &msg.cdesc, sizeof(struct cls_desc)); |
| 122 | |
| 123 | return ret; |
| 124 | } |
| 125 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 126 | int mtk_pce_cls_desc_write(struct cls_desc *cdesc, u32 idx) |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 127 | { |
| 128 | struct fe_mem_msg msg; |
| 129 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 130 | if (unlikely(!cdesc || !idx || idx >= FE_MEM_CLS_MAX_INDEX)) |
| 131 | return -EINVAL; |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 132 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 133 | mtk_pce_fe_mem_msg_config(&msg, FE_MEM_CMD_WRITE, FE_MEM_TYPE_CLS, idx); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 134 | |
| 135 | memset(&msg.raw, 0, sizeof(msg.raw)); |
| 136 | memcpy(&msg.cdesc, cdesc, sizeof(struct cls_desc)); |
| 137 | |
| 138 | return mtk_pce_fe_mem_msg_send(&msg); |
| 139 | } |
| 140 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 141 | int mtk_pce_cls_entry_write(struct cls_entry *cls) |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 142 | { |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 143 | if (unlikely(!cls)) |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 146 | return mtk_pce_cls_desc_write(&cls->cdesc, cls->idx); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 147 | } |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 148 | EXPORT_SYMBOL(mtk_pce_cls_entry_write); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 149 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 150 | struct cls_entry *mtk_pce_cls_entry_alloc(void) |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 151 | { |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 152 | struct cls_entry *cls; |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 153 | unsigned long flag; |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 154 | u32 idx; |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 155 | |
| 156 | spin_lock_irqsave(&cls_hw.lock, flag); |
| 157 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 158 | idx = find_first_zero_bit(cls_hw.cls_used, FE_MEM_CLS_MAX_INDEX); |
| 159 | if (idx == FE_MEM_CLS_MAX_INDEX) { |
| 160 | cls = ERR_PTR(-ENOMEM); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 161 | goto unlock; |
| 162 | } |
| 163 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 164 | set_bit(idx, cls_hw.cls_used); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 165 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 166 | cls = &cls_hw.cls_tbl[idx]; |
| 167 | |
| 168 | memset(&cls->cdesc, 0, sizeof(cls->cdesc)); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 169 | |
| 170 | unlock: |
| 171 | spin_unlock_irqrestore(&cls_hw.lock, flag); |
| 172 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 173 | return cls; |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 174 | } |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 175 | EXPORT_SYMBOL(mtk_pce_cls_entry_alloc); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 176 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 177 | void mtk_pce_cls_entry_free(struct cls_entry *cls) |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 178 | { |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 179 | unsigned long flag; |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 180 | |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 181 | if (!cls) |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 182 | return; |
| 183 | |
developer | 732266e | 2024-07-26 13:39:37 +0800 | [diff] [blame^] | 184 | raw_notifier_call_chain(&cls_notif_chain, CLS_NOTIFY_DELETE_ENTRY, cls); |
| 185 | |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 186 | spin_lock_irqsave(&cls_hw.lock, flag); |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 187 | clear_bit(cls->idx - 1, cls_hw.cls_used); |
developer | f2f1b88 | 2023-08-08 16:02:18 +0800 | [diff] [blame] | 188 | spin_unlock_irqrestore(&cls_hw.lock, flag); |
| 189 | } |
developer | a4177cd | 2023-08-24 17:39:15 +0800 | [diff] [blame] | 190 | EXPORT_SYMBOL(mtk_pce_cls_entry_free); |