blob: 0660c31b4f59a69a821de2dc981c80c087727378 [file] [log] [blame]
Aaron Williams4fd1e552021-04-23 19:56:32 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2020 Marvell International Ltd.
4 *
5 * Interface to the hardware Free Pool Allocator.
6 */
7
8#ifndef __CVMX_FPA_H__
9#define __CVMX_FPA_H__
10
11#include "cvmx-scratch.h"
12#include "cvmx-fpa-defs.h"
13#include "cvmx-fpa1.h"
14#include "cvmx-fpa3.h"
15
16#define CVMX_FPA_MIN_BLOCK_SIZE 128
17#define CVMX_FPA_ALIGNMENT 128
18#define CVMX_FPA_POOL_NAME_LEN 16
19
20/* On CN78XX in backward-compatible mode, pool is mapped to AURA */
21#define CVMX_FPA_NUM_POOLS \
22 (octeon_has_feature(OCTEON_FEATURE_FPA3) ? cvmx_fpa3_num_auras() : CVMX_FPA1_NUM_POOLS)
23
24/**
25 * Structure to store FPA pool configuration parameters.
26 */
27struct cvmx_fpa_pool_config {
28 s64 pool_num;
29 u64 buffer_size;
30 u64 buffer_count;
31};
32
33typedef struct cvmx_fpa_pool_config cvmx_fpa_pool_config_t;
34
35/**
36 * Return the name of the pool
37 *
38 * @param pool_num Pool to get the name of
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010039 * Return: The name
Aaron Williams4fd1e552021-04-23 19:56:32 +020040 */
41const char *cvmx_fpa_get_name(int pool_num);
42
43/**
44 * Initialize FPA per node
45 */
46int cvmx_fpa_global_init_node(int node);
47
48/**
49 * Enable the FPA
50 */
51static inline void cvmx_fpa_enable(void)
52{
53 if (!octeon_has_feature(OCTEON_FEATURE_FPA3))
54 cvmx_fpa1_enable();
55 else
56 cvmx_fpa_global_init_node(cvmx_get_node_num());
57}
58
59/**
60 * Disable the FPA
61 */
62static inline void cvmx_fpa_disable(void)
63{
64 if (!octeon_has_feature(OCTEON_FEATURE_FPA3))
65 cvmx_fpa1_disable();
66 /* FPA3 does not have a disable function */
67}
68
69/**
70 * @INTERNAL
71 * @deprecated OBSOLETE
72 *
73 * Kept for transition assistance only
74 */
75static inline void cvmx_fpa_global_initialize(void)
76{
77 cvmx_fpa_global_init_node(cvmx_get_node_num());
78}
79
80/**
81 * @INTERNAL
82 *
83 * Convert FPA1 style POOL into FPA3 AURA in
84 * backward compatibility mode.
85 */
86static inline cvmx_fpa3_gaura_t cvmx_fpa1_pool_to_fpa3_aura(cvmx_fpa1_pool_t pool)
87{
88 if ((octeon_has_feature(OCTEON_FEATURE_FPA3))) {
89 unsigned int node = cvmx_get_node_num();
90 cvmx_fpa3_gaura_t aura = __cvmx_fpa3_gaura(node, pool);
91 return aura;
92 }
93 return CVMX_FPA3_INVALID_GAURA;
94}
95
96/**
97 * Get a new block from the FPA
98 *
99 * @param pool Pool to get the block from
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100100 * Return: Pointer to the block or NULL on failure
Aaron Williams4fd1e552021-04-23 19:56:32 +0200101 */
102static inline void *cvmx_fpa_alloc(u64 pool)
103{
104 /* FPA3 is handled differently */
105 if ((octeon_has_feature(OCTEON_FEATURE_FPA3))) {
106 return cvmx_fpa3_alloc(cvmx_fpa1_pool_to_fpa3_aura(pool));
Stefan Roesecbe62432022-04-07 09:11:10 +0200107 } else {
Aaron Williams4fd1e552021-04-23 19:56:32 +0200108 return cvmx_fpa1_alloc(pool);
Stefan Roesecbe62432022-04-07 09:11:10 +0200109 }
Aaron Williams4fd1e552021-04-23 19:56:32 +0200110}
111
112/**
113 * Asynchronously get a new block from the FPA
114 *
115 * The result of cvmx_fpa_async_alloc() may be retrieved using
116 * cvmx_fpa_async_alloc_finish().
117 *
118 * @param scr_addr Local scratch address to put response in. This is a byte
119 * address but must be 8 byte aligned.
120 * @param pool Pool to get the block from
121 */
122static inline void cvmx_fpa_async_alloc(u64 scr_addr, u64 pool)
123{
124 if ((octeon_has_feature(OCTEON_FEATURE_FPA3))) {
125 return cvmx_fpa3_async_alloc(scr_addr, cvmx_fpa1_pool_to_fpa3_aura(pool));
126 } else
127 return cvmx_fpa1_async_alloc(scr_addr, pool);
128}
129
130/**
131 * Retrieve the result of cvmx_fpa_async_alloc
132 *
133 * @param scr_addr The Local scratch address. Must be the same value
134 * passed to cvmx_fpa_async_alloc().
135 *
136 * @param pool Pool the block came from. Must be the same value
137 * passed to cvmx_fpa_async_alloc.
138 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100139 * Return: Pointer to the block or NULL on failure
Aaron Williams4fd1e552021-04-23 19:56:32 +0200140 */
141static inline void *cvmx_fpa_async_alloc_finish(u64 scr_addr, u64 pool)
142{
143 if ((octeon_has_feature(OCTEON_FEATURE_FPA3)))
144 return cvmx_fpa3_async_alloc_finish(scr_addr, cvmx_fpa1_pool_to_fpa3_aura(pool));
145 else
146 return cvmx_fpa1_async_alloc_finish(scr_addr, pool);
147}
148
149/**
150 * Free a block allocated with a FPA pool.
151 * Does NOT provide memory ordering in cases where the memory block was
152 * modified by the core.
153 *
154 * @param ptr Block to free
155 * @param pool Pool to put it in
156 * @param num_cache_lines
157 * Cache lines to invalidate
158 */
159static inline void cvmx_fpa_free_nosync(void *ptr, u64 pool, u64 num_cache_lines)
160{
161 /* FPA3 is handled differently */
162 if ((octeon_has_feature(OCTEON_FEATURE_FPA3)))
163 cvmx_fpa3_free_nosync(ptr, cvmx_fpa1_pool_to_fpa3_aura(pool), num_cache_lines);
164 else
165 cvmx_fpa1_free_nosync(ptr, pool, num_cache_lines);
166}
167
168/**
169 * Free a block allocated with a FPA pool. Provides required memory
170 * ordering in cases where memory block was modified by core.
171 *
172 * @param ptr Block to free
173 * @param pool Pool to put it in
174 * @param num_cache_lines
175 * Cache lines to invalidate
176 */
177static inline void cvmx_fpa_free(void *ptr, u64 pool, u64 num_cache_lines)
178{
179 if ((octeon_has_feature(OCTEON_FEATURE_FPA3)))
180 cvmx_fpa3_free(ptr, cvmx_fpa1_pool_to_fpa3_aura(pool), num_cache_lines);
181 else
182 cvmx_fpa1_free(ptr, pool, num_cache_lines);
183}
184
185/**
186 * Setup a FPA pool to control a new block of memory.
187 * This can only be called once per pool. Make sure proper
188 * locking enforces this.
189 *
190 * @param pool Pool to initialize
191 * @param name Constant character string to name this pool.
192 * String is not copied.
193 * @param buffer Pointer to the block of memory to use. This must be
194 * accessible by all processors and external hardware.
195 * @param block_size Size for each block controlled by the FPA
196 * @param num_blocks Number of blocks
197 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100198 * Return: the pool number on Success,
Aaron Williams4fd1e552021-04-23 19:56:32 +0200199 * -1 on failure
200 */
201int cvmx_fpa_setup_pool(int pool, const char *name, void *buffer, u64 block_size, u64 num_blocks);
202
203int cvmx_fpa_shutdown_pool(int pool);
204
205/**
206 * Gets the block size of buffer in specified pool
207 * @param pool Pool to get the block size from
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100208 * Return: Size of buffer in specified pool
Aaron Williams4fd1e552021-04-23 19:56:32 +0200209 */
210unsigned int cvmx_fpa_get_block_size(int pool);
211
212int cvmx_fpa_is_pool_available(int pool_num);
213u64 cvmx_fpa_get_pool_owner(int pool_num);
214int cvmx_fpa_get_max_pools(void);
215int cvmx_fpa_get_current_count(int pool_num);
216int cvmx_fpa_validate_pool(int pool);
217
218#endif /* __CVM_FPA_H__ */