1 /*
2 * Copyright (c) 2026 Justin Hibbits
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 /*-
8 * Copyright (c) 2011-2012 Semihalf.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef _BMAN_H
34 #define _BMAN_H
35
36 #include <sys/vmem.h>
37 #include <machine/vmparam.h>
38
39 /*
40 * BMAN Configuration
41 */
42
43 /*
44 * Portal definitions
45 */
46
47 struct bman_softc {
48 device_t sc_dev; /* device handle */
49 int sc_rrid; /* register rid */
50 struct resource *sc_rres; /* register resource */
51 int sc_irid; /* interrupt rid */
52 struct resource *sc_ires; /* interrupt resource */
53 void *sc_icookie;
54 vmem_t *sc_vmem; /* resource pool */
55 int sc_major;
56 int sc_minor;
57 };
58
59 struct bman_buffer {
60 uint16_t bpid;
61 uint16_t buf_hi;
62 uint32_t buf_lo;
63 } __aligned(8);
64
65 struct bman_pool;
66 struct bman_buffer;
67
68 typedef void (*bm_depletion_handler)(void *, bool);
69
70 /*
71 * External API
72 */
73
74 struct bman_pool *bman_new_pool(void);
75 struct bman_pool *bman_pool_create(uint8_t *bpid, uint16_t buffer_size,
76 uint16_t max_buffers, uint32_t dep_sw_entry, uint32_t dep_sw_exit, uint32_t
77 dep_hw_entry, uint32_t dep_hw_exit, bm_depletion_handler dep_cb, void *arg);
78
79 /*
80 * @brief Destroy pool.
81 *
82 * The bman_pool_destroy() function destroys the BMAN pool.
83 * The buffer pool must be empty.
84 *
85 * @param pool The BMAN pool handle.
86 * @return 0 on success, EBUSY if the pool is not empty.
87 */
88 int bman_pool_destroy(struct bman_pool *pool);
89
90 /*
91 * @brief Count free buffers in given pool.
92 *
93 * @param pool The BMAN pool handle.
94 *
95 * @returns Number of free buffers in pool.
96 */
97 uint32_t bman_count(struct bman_pool *pool);
98
99 int bman_put_buffers(struct bman_pool *, struct bman_buffer *, int);
100 static inline int
bman_put_buffer(struct bman_pool * p,vm_paddr_t buf,int bpid)101 bman_put_buffer(struct bman_pool *p, vm_paddr_t buf, int bpid)
102 {
103 struct bman_buffer b = {
104 .bpid = bpid,
105 .buf_hi = ((uintptr_t)buf) >> 32,
106 .buf_lo = ((uintptr_t)buf) & 0xffffffff
107 };
108 return (bman_put_buffers(p, &b, 1));
109 }
110
111 int bman_acquire(struct bman_pool *, struct bman_buffer *, uint8_t);
112
113 int bman_create_affine_portal(device_t, vm_offset_t, vm_offset_t, int);
114 void bman_destroy_affine_portal(int);
115 uint32_t bman_get_bpid(struct bman_pool *);
116
117 /*
118 * Bus i/f
119 */
120 int bman_attach(device_t dev);
121 int bman_detach(device_t dev);
122 int bman_suspend(device_t dev);
123 int bman_resume(device_t dev);
124 int bman_shutdown(device_t dev);
125
126 #endif /* BMAN_H */
127