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