Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 : : *
3 : : * Copyright 2008-2016 Freescale Semiconductor Inc.
4 : : * Copyright 2017, 2024-2026 NXP
5 : : *
6 : : */
7 : : #include <rte_memcpy.h>
8 : : #include <rte_branch_prediction.h>
9 : : #include <eal_export.h>
10 : : #include <stdint.h>
11 : : #include <limits.h>
12 : :
13 : : #include "bman.h"
14 : :
15 : : /* Compilation constants */
16 : : #define RCR_THRESH 2 /* reread h/w CI when running out of space */
17 : : #define IRQNAME "BMan portal %d"
18 : : #define MAX_IRQNAME 16 /* big enough for "BMan portal %d" */
19 : :
20 : : struct bman_portal {
21 : : struct bm_portal p;
22 : : /* 2-element array. pools[0] is mask, pools[1] is snapshot. */
23 : : struct bman_depletion *pools;
24 : : int thresh_set;
25 : : unsigned long irq_sources;
26 : : u32 slowpoll; /* only used when interrupts are off */
27 : : /* When the cpu-affine portal is activated, this is non-NULL */
28 : : const struct bm_portal_config *config;
29 : : char irqname[MAX_IRQNAME];
30 : : };
31 : :
32 : : static cpumask_t affine_mask;
33 : : static DEFINE_SPINLOCK(affine_mask_lock);
34 : : static RTE_DEFINE_PER_LCORE(struct bman_portal, bman_affine_portal);
35 : :
36 : : static inline struct bman_portal *get_affine_portal(void)
37 : : {
38 : : return &RTE_PER_LCORE(bman_affine_portal);
39 : : }
40 : :
41 : : /*
42 : : * This object type refers to a pool, it isn't *the* pool. There may be
43 : : * more than one such object per BMan buffer pool, eg. if different users of
44 : : * the pool are operating via different portals.
45 : : */
46 : : struct bman_pool {
47 : : struct bman_pool_params params;
48 : : /* Used for hash-table admin when using depletion notifications. */
49 : : struct bman_portal *portal;
50 : : struct bman_pool *next;
51 : : };
52 : :
53 : : static inline
54 : 0 : struct bman_portal *bman_create_portal(struct bman_portal *portal,
55 : : const struct bm_portal_config *c)
56 : : {
57 : : struct bm_portal *p;
58 : : const struct bman_depletion *pools = &c->mask;
59 : : int ret;
60 : : u8 bpid = 0;
61 : :
62 : 0 : p = &portal->p;
63 : : /*
64 : : * prep the low-level portal struct with the mapped addresses from the
65 : : * config, everything that follows depends on it and "config" is more
66 : : * for (de)reference...
67 : : */
68 : 0 : p->addr.ce = c->addr_virt[DPAA_PORTAL_CE];
69 : 0 : p->addr.ci = c->addr_virt[DPAA_PORTAL_CI];
70 [ # # ]: 0 : if (bm_rcr_init(p, bm_rcr_pvb, bm_rcr_cce)) {
71 : 0 : pr_err("Bman RCR initialisation failed\n");
72 : 0 : return NULL;
73 : : }
74 : : if (bm_mc_init(p)) {
75 : : pr_err("Bman MC initialisation failed\n");
76 : : goto fail_mc;
77 : : }
78 : 0 : portal->pools = kmalloc(2 * sizeof(*pools), GFP_KERNEL);
79 [ # # ]: 0 : if (!portal->pools)
80 : 0 : goto fail_pools;
81 : 0 : portal->pools[0] = *pools;
82 : : bman_depletion_init(portal->pools + 1);
83 [ # # ]: 0 : while (bpid < bman_pool_max) {
84 : : /*
85 : : * Default to all BPIDs disabled, we enable as required at
86 : : * run-time.
87 : : */
88 : 0 : bm_isr_bscn_mask(p, bpid, 0);
89 : 0 : bpid++;
90 : : }
91 : 0 : portal->slowpoll = 0;
92 : : /* Write-to-clear any stale interrupt status bits */
93 : : bm_isr_disable_write(p, 0xffffffff);
94 : 0 : portal->irq_sources = 0;
95 : : bm_isr_enable_write(p, portal->irq_sources);
96 : : bm_isr_status_clear(p, 0xffffffff);
97 : 0 : snprintf(portal->irqname, MAX_IRQNAME, IRQNAME, c->cpu);
98 [ # # ]: 0 : if (request_irq(c->irq, NULL, 0, portal->irqname,
99 : : portal)) {
100 : 0 : pr_err("request_irq() failed\n");
101 : 0 : goto fail_irq;
102 : : }
103 : :
104 : : /* Need RCR to be empty before continuing */
105 : : ret = bm_rcr_get_fill(p);
106 [ # # ]: 0 : if (ret) {
107 : 0 : pr_err("Bman RCR unclean\n");
108 : 0 : goto fail_rcr_empty;
109 : : }
110 : : /* Success */
111 : 0 : portal->config = c;
112 : :
113 : : bm_isr_disable_write(p, 0);
114 : : bm_isr_uninhibit(p);
115 : 0 : return portal;
116 : : fail_rcr_empty:
117 : 0 : free_irq(c->irq, portal);
118 : 0 : fail_irq:
119 : 0 : kfree(portal->pools);
120 : 0 : fail_pools:
121 : : bm_mc_finish(p);
122 : : fail_mc:
123 : 0 : bm_rcr_finish(p);
124 : 0 : return NULL;
125 : : }
126 : :
127 : : struct bman_portal *
128 : 0 : bman_create_affine_portal(const struct bm_portal_config *c)
129 : : {
130 : : struct bman_portal *portal = get_affine_portal();
131 : :
132 : : /*This function is called from the context which is already affine to
133 : : *CPU or in other words this in non-migratable to other CPUs.
134 : : */
135 : 0 : portal = bman_create_portal(portal, c);
136 [ # # ]: 0 : if (portal) {
137 : : spin_lock(&affine_mask_lock);
138 [ # # ]: 0 : CPU_SET(c->cpu, &affine_mask);
139 : : spin_unlock(&affine_mask_lock);
140 : : }
141 : 0 : return portal;
142 : : }
143 : :
144 : : static inline
145 : 0 : void bman_destroy_portal(struct bman_portal *bm)
146 : : {
147 : : const struct bm_portal_config *pcfg;
148 : :
149 : 0 : pcfg = bm->config;
150 : 0 : bm_rcr_cce_update(&bm->p);
151 : : bm_rcr_cce_update(&bm->p);
152 : :
153 : 0 : free_irq(pcfg->irq, bm);
154 : :
155 : 0 : kfree(bm->pools);
156 : : bm_mc_finish(&bm->p);
157 : 0 : bm_rcr_finish(&bm->p);
158 : 0 : bm->config = NULL;
159 : 0 : }
160 : :
161 : : const struct
162 : 0 : bm_portal_config *bman_destroy_affine_portal(void)
163 : : {
164 : : struct bman_portal *bm = get_affine_portal();
165 : : const struct bm_portal_config *pcfg;
166 : :
167 : 0 : pcfg = bm->config;
168 : 0 : bman_destroy_portal(bm);
169 : : spin_lock(&affine_mask_lock);
170 [ # # ]: 0 : CPU_CLR(pcfg->cpu, &affine_mask);
171 : : spin_unlock(&affine_mask_lock);
172 : 0 : return pcfg;
173 : : }
174 : :
175 : : int
176 : 0 : bman_get_portal_index(void)
177 : : {
178 : : struct bman_portal *p = get_affine_portal();
179 : 0 : return p->config->index;
180 : : }
181 : :
182 : : static const u32 zero_thresholds[4] = {0, 0, 0, 0};
183 : :
184 : 0 : struct bman_pool *bman_new_pool(const struct bman_pool_params *params)
185 : : {
186 : : struct bman_pool *pool = NULL;
187 : : u32 bpid;
188 : :
189 [ # # ]: 0 : if (params->flags & BMAN_POOL_FLAG_DYNAMIC_BPID) {
190 : : int ret = bman_alloc_bpid(&bpid);
191 : :
192 [ # # ]: 0 : if (ret)
193 : : return NULL;
194 : : } else {
195 [ # # ]: 0 : if (params->bpid >= bman_pool_max)
196 : : return NULL;
197 : 0 : bpid = params->bpid;
198 : : }
199 [ # # ]: 0 : if (params->flags & BMAN_POOL_FLAG_THRESH) {
200 : 0 : int ret = bm_pool_set(bpid, params->thresholds);
201 : :
202 [ # # ]: 0 : if (ret)
203 : 0 : goto err;
204 : : }
205 : :
206 : 0 : pool = kmalloc(sizeof(*pool), GFP_KERNEL);
207 [ # # ]: 0 : if (!pool)
208 : 0 : goto err;
209 : 0 : pool->params = *params;
210 : :
211 [ # # ]: 0 : if (params->flags & BMAN_POOL_FLAG_DYNAMIC_BPID)
212 : 0 : pool->params.bpid = bpid;
213 : :
214 : : return pool;
215 : 0 : err:
216 [ # # ]: 0 : if (params->flags & BMAN_POOL_FLAG_THRESH)
217 : 0 : bm_pool_set(bpid, zero_thresholds);
218 : :
219 [ # # ]: 0 : if (params->flags & BMAN_POOL_FLAG_DYNAMIC_BPID)
220 : 0 : bman_release_bpid(bpid);
221 : 0 : kfree(pool);
222 : :
223 : 0 : return NULL;
224 : : }
225 : :
226 : 0 : void bman_free_pool(struct bman_pool *pool)
227 : : {
228 [ # # ]: 0 : if (pool->params.flags & BMAN_POOL_FLAG_THRESH)
229 : 0 : bm_pool_set(pool->params.bpid, zero_thresholds);
230 [ # # ]: 0 : if (pool->params.flags & BMAN_POOL_FLAG_DYNAMIC_BPID)
231 : 0 : bman_release_bpid(pool->params.bpid);
232 : 0 : kfree(pool);
233 : 0 : }
234 : :
235 : 0 : void bman_free_bpid(u8 bpid, u32 flags)
236 : : {
237 [ # # ]: 0 : if (flags & BMAN_POOL_FLAG_THRESH)
238 : 0 : bm_pool_set(bpid, zero_thresholds);
239 [ # # ]: 0 : if (flags & BMAN_POOL_FLAG_DYNAMIC_BPID)
240 : 0 : bman_release_bpid(bpid);
241 : 0 : }
242 : :
243 : 0 : const struct bman_pool_params *bman_get_params(const struct bman_pool *pool)
244 : : {
245 : 0 : return &pool->params;
246 : : }
247 : :
248 : : static void update_rcr_ci(struct bman_portal *p, int avail)
249 : : {
250 [ # # # # ]: 0 : if (avail)
251 : : bm_rcr_cce_prefetch(&p->p);
252 : : else
253 : : bm_rcr_cce_update(&p->p);
254 : : }
255 : :
256 : : RTE_EXPORT_INTERNAL_SYMBOL(bman_release_fast)
257 : : int
258 : 0 : bman_release_fast(struct bman_pool *pool, const uint64_t *bufs,
259 : : uint8_t num)
260 : : {
261 : : struct bman_portal *p;
262 : : struct bm_rcr_entry *r;
263 : : uint8_t i, avail;
264 [ # # ]: 0 : uint64_t bpid = pool->params.bpid;
265 : : struct bm_buffer bm_bufs[FSL_BM_BURST_MAX];
266 : :
267 : : #ifdef RTE_LIBRTE_DPAA_HWDEBUG
268 : : if (!num || (num > FSL_BM_BURST_MAX))
269 : : return -EINVAL;
270 : : if (pool->params.flags & BMAN_POOL_FLAG_NO_RELEASE)
271 : : return -EINVAL;
272 : : #endif
273 : :
274 : : p = get_affine_portal();
275 : : avail = bm_rcr_get_avail(&p->p);
276 [ # # ]: 0 : if (avail < 2)
277 : : update_rcr_ci(p, avail);
278 : : r = bm_rcr_start(&p->p);
279 [ # # ]: 0 : if (unlikely(!r))
280 : : return -EBUSY;
281 : :
282 [ # # ]: 0 : bm_buffer_set64_to_be(&bm_bufs[0], bufs[0]);
283 [ # # ]: 0 : for (i = 1; i < num; i++)
284 [ # # ]: 0 : bm_buffer_set64_to_be(&bm_bufs[i], bufs[i]);
285 : :
286 : : /* bufs[0] overlaps the verb, which bm_rcr_pvb_commit() writes last
287 : : * to publish the entry, so do not store over byte 0 here.
288 : : */
289 : 0 : r->bpid = bpid;
290 : 0 : r->bufs[0].be_desc.hi = bm_bufs[0].be_desc.hi;
291 : 0 : r->bufs[0].be_desc.lo = bm_bufs[0].be_desc.lo;
292 [ # # ]: 0 : if (num > 1)
293 : 0 : memcpy(&r->bufs[1], &bm_bufs[1],
294 : 0 : sizeof(struct bm_buffer) * (num - 1));
295 : :
296 : 0 : bm_rcr_pvb_commit(&p->p, BM_RCR_VERB_CMD_BPID_SINGLE |
297 : 0 : (num & BM_RCR_VERB_BUFCOUNT_MASK));
298 : :
299 : 0 : return 0;
300 : : }
301 : :
302 [ # # ]: 0 : int bman_release(struct bman_pool *pool, const struct bm_buffer *bufs, u8 num,
303 : : u32 flags __maybe_unused)
304 : : {
305 : : struct bman_portal *p;
306 : : struct bm_rcr_entry *r;
307 : : u32 i;
308 : : u8 avail;
309 : :
310 : : #ifdef RTE_LIBRTE_DPAA_HWDEBUG
311 : : if (!num || (num > FSL_BM_BURST_MAX))
312 : : return -EINVAL;
313 : : if (pool->params.flags & BMAN_POOL_FLAG_NO_RELEASE)
314 : : return -EINVAL;
315 : : #endif
316 : :
317 : : p = get_affine_portal();
318 : : avail = bm_rcr_get_avail(&p->p);
319 [ # # ]: 0 : if (avail < 2)
320 : : update_rcr_ci(p, avail);
321 : : r = bm_rcr_start(&p->p);
322 [ # # ]: 0 : if (unlikely(!r))
323 : : return -EBUSY;
324 : :
325 : : /*
326 : : * we can copy all but the first entry, as this can trigger badness
327 : : * with the valid-bit
328 : : */
329 : 0 : r->bufs[0].opaque =
330 [ # # ]: 0 : cpu_to_be64(((u64)pool->params.bpid << 48) |
331 : : (bufs[0].opaque & MAX_U48));
332 [ # # ]: 0 : for (i = 1; i < num; i++)
333 : 0 : r->bufs[i].opaque =
334 [ # # ]: 0 : cpu_to_be64(bufs[i].opaque & MAX_U48);
335 : :
336 : 0 : bm_rcr_pvb_commit(&p->p, BM_RCR_VERB_CMD_BPID_SINGLE |
337 : 0 : (num & BM_RCR_VERB_BUFCOUNT_MASK));
338 : :
339 : 0 : return 0;
340 : : }
341 : :
342 : : static inline uint64_t
343 : : __rte_unused bman_extract_addr(struct bm_buffer *buf)
344 : : {
345 : : buf->opaque = be64_to_cpu(buf->opaque);
346 : :
347 : : return buf->addr;
348 : : }
349 : :
350 : : RTE_EXPORT_INTERNAL_SYMBOL(bman_acquire_fast)
351 : : int
352 : 0 : bman_acquire_fast(struct bman_pool *pool, uint64_t *bufs, uint8_t num)
353 : : {
354 : : struct bman_portal *p = get_affine_portal();
355 : : struct bm_mc_command *mcc;
356 : : struct bm_mc_result *mcr;
357 : : uint8_t i, rst;
358 : : struct bm_buffer bm_bufs[FSL_BM_BURST_MAX];
359 : :
360 : : #ifdef RTE_LIBRTE_DPAA_HWDEBUG
361 : : if (!num || (num > FSL_BM_BURST_MAX))
362 : : return -EINVAL;
363 : : if (pool->params.flags & BMAN_POOL_FLAG_ONLY_RELEASE)
364 : : return -EINVAL;
365 : : #endif
366 : :
367 : : mcc = bm_mc_start(&p->p);
368 : 0 : mcc->acquire.bpid = pool->params.bpid;
369 : 0 : bm_mc_commit(&p->p, BM_MCC_VERB_CMD_ACQUIRE |
370 : 0 : (num & BM_MCC_VERB_ACQUIRE_BUFCOUNT));
371 : : while (!(mcr = bm_mc_result(&p->p)))
372 : : ;
373 : 0 : rst = mcr->verb & BM_MCR_VERB_ACQUIRE_BUFCOUNT;
374 [ # # ]: 0 : if (unlikely(rst < 1 || rst > FSL_BM_BURST_MAX))
375 : : return -EINVAL;
376 : :
377 [ # # ]: 0 : rte_memcpy(bm_bufs, mcr->acquire.bufs, sizeof(struct bm_buffer) * rst);
378 : :
379 [ # # ]: 0 : for (i = 0; i < rst; i++)
380 [ # # # # ]: 0 : bufs[i] = bm_buffer_get64_from_be(&bm_bufs[i]);
381 : :
382 : 0 : return rst;
383 : : }
384 : :
385 : 0 : int bman_acquire(struct bman_pool *pool, struct bm_buffer *bufs, u8 num,
386 : : u32 flags __maybe_unused)
387 : : {
388 : : struct bman_portal *p = get_affine_portal();
389 : : struct bm_mc_command *mcc;
390 : : struct bm_mc_result *mcr;
391 : : uint8_t rst, i;
392 : :
393 : : #ifdef RTE_LIBRTE_DPAA_HWDEBUG
394 : : if (!num || (num > FSL_BM_BURST_MAX))
395 : : return -EINVAL;
396 : : if (pool->params.flags & BMAN_POOL_FLAG_ONLY_RELEASE)
397 : : return -EINVAL;
398 : : #endif
399 : :
400 : : mcc = bm_mc_start(&p->p);
401 : 0 : mcc->acquire.bpid = pool->params.bpid;
402 : 0 : bm_mc_commit(&p->p, BM_MCC_VERB_CMD_ACQUIRE |
403 : 0 : (num & BM_MCC_VERB_ACQUIRE_BUFCOUNT));
404 : : while (!(mcr = bm_mc_result(&p->p)))
405 : 0 : cpu_relax();
406 : 0 : rst = mcr->verb & BM_MCR_VERB_ACQUIRE_BUFCOUNT;
407 [ # # ]: 0 : for (i = 0; i < rst; i++)
408 [ # # ]: 0 : bufs[i].opaque = be64_to_cpu(mcr->acquire.bufs[i].opaque);
409 : :
410 : 0 : return rst;
411 : : }
412 : :
413 : 0 : int bman_query_pools(struct bm_pool_state *state)
414 : : {
415 : : struct bman_portal *p = get_affine_portal();
416 : : struct bm_mc_result *mcr;
417 : :
418 : : bm_mc_start(&p->p);
419 : : bm_mc_commit(&p->p, BM_MCC_VERB_CMD_QUERY);
420 : : while (!(mcr = bm_mc_result(&p->p)))
421 : 0 : cpu_relax();
422 : : DPAA_ASSERT((mcr->verb & BM_MCR_VERB_CMD_MASK) ==
423 : : BM_MCR_VERB_CMD_QUERY);
424 : 0 : *state = mcr->query;
425 [ # # ]: 0 : state->as.state.state[0] = be32_to_cpu(state->as.state.state[0]);
426 [ # # ]: 0 : state->as.state.state[1] = be32_to_cpu(state->as.state.state[1]);
427 [ # # ]: 0 : state->ds.state.state[0] = be32_to_cpu(state->ds.state.state[0]);
428 [ # # ]: 0 : state->ds.state.state[1] = be32_to_cpu(state->ds.state.state[1]);
429 : 0 : return 0;
430 : : }
431 : :
432 : 0 : u32 bman_query_free_buffers(struct bman_pool *pool)
433 : : {
434 : 0 : return bm_pool_free_buffers(pool->params.bpid);
435 : : }
436 : :
437 : 0 : int bman_update_pool_thresholds(struct bman_pool *pool, const u32 *thresholds)
438 : : {
439 : : u32 bpid;
440 : :
441 : 0 : bpid = bman_get_params(pool)->bpid;
442 : :
443 : 0 : return bm_pool_set(bpid, thresholds);
444 : : }
445 : :
446 : 0 : int bman_shutdown_pool(u32 bpid)
447 : : {
448 : : struct bman_portal *p = get_affine_portal();
449 : 0 : return bm_shutdown_pool(&p->p, bpid);
450 : : }
|