xref: /aosp_15_r20/external/coreboot/src/vendorcode/cavium/include/bdk/libbdk-hal/bdk-utils.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 #ifndef __CB_BDK_UTILS_H__
2 #define __CB_BDK_UTILS_H__
3 /***********************license start***********************************
4 * Copyright (c) 2003-2017  Cavium Inc. ([email protected]). All rights
5 * reserved.
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 *   * Redistributions of source code must retain the above copyright
13 *     notice, this list of conditions and the following disclaimer.
14 *
15 *   * Redistributions in binary form must reproduce the above
16 *     copyright notice, this list of conditions and the following
17 *     disclaimer in the documentation and/or other materials provided
18 *     with the distribution.
19 *
20 *   * Neither the name of Cavium Inc. nor the names of
21 *     its contributors may be used to endorse or promote products
22 *     derived from this software without specific prior written
23 *     permission.
24 *
25 * This Software, including technical data, may be subject to U.S. export
26 * control laws, including the U.S. Export Administration Act and its
27 * associated regulations, and may be subject to export or import
28 * regulations in other countries.
29 *
30 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
31 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
32 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
33 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
34 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
35 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
36 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
37 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
38 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK
39 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
40 ***********************license end**************************************/
41 #include "libbdk-arch/bdk-csrs-rst.h"
42 #include <bdk-coreboot.h>
43 #include <string.h>
44 
45 /**
46  * @file
47  * Small utility functions and macros to ease programming.
48  *
49  * <hr>$Revision: 38306 $<hr>
50  *
51  * @addtogroup hal
52  * @{
53  */
54 
55 /*
56  * The macros bdk_likely and bdk_unlikely use the
57  * __builtin_expect GCC operation to control branch
58  * probabilities for a conditional. For example, an "if"
59  * statement in the code that will almost always be
60  * executed should be written as "if (bdk_likely(...))".
61  * If the "else" section of an if statement is more
62  * probable, use "if (bdk_unlikey(...))".
63  */
64 #define bdk_likely(x)      __builtin_expect(!!(x), 1)
65 #define bdk_unlikely(x)    __builtin_expect(!!(x), 0)
66 
67 #define BDK_DISPLAY_PASS       1    /* Control the display of the detail chip pass info */
68 #define BDK_CACHE_LINE_SIZE    (128)   // In bytes
69 #define BDK_CACHE_LINE_MASK    (BDK_CACHE_LINE_SIZE - 1)   // In bytes
70 #define BDK_CACHE_LINE_ALIGNED __attribute__ ((aligned (BDK_CACHE_LINE_SIZE)))
71 
72 /**
73  * Builds a bit mask given the required size in bits.
74  *
75  * @param bits   Number of bits in the mask
76  * @return The mask
77  */
bdk_build_mask(uint64_t bits)78 static inline uint64_t bdk_build_mask(uint64_t bits)
79 {
80     if (bits == 64)
81         return -1;
82     else
83         return ~((~0x0ull) << bits);
84 }
85 
86 /**
87  * Extract bits out of a number
88  *
89  * @param input  Number to extract from
90  * @param lsb    Starting bit, least significant (0-63)
91  * @param width  Width in bits (1-64)
92  *
93  * @return Extracted number
94  */
bdk_extract(uint64_t input,int lsb,int width)95 static inline uint64_t bdk_extract(uint64_t input, int lsb, int width)
96 {
97     uint64_t result = input >> lsb;
98     result &= bdk_build_mask(width);
99     return result;
100 }
101 
102 /**
103  * Extract signed bits out of a number
104  *
105  * @param input  Number to extract from
106  * @param lsb    Starting bit, least significant (0-63)
107  * @param width  Width in bits (1-64)
108  *
109  * @return Extracted number
110  */
bdk_extracts(uint64_t input,int lsb,int width)111 static inline int64_t bdk_extracts(uint64_t input, int lsb, int width)
112 {
113     int64_t result = input >> lsb;
114     result <<= 64 - width;
115     result >>= 64 - width;
116     return result;
117 }
118 
119 /**
120  * Extract a signed magnatude value. Signed magnatude is a value where the MSB
121  * is treated as a sign bit, not like the normal twos compliment
122  *
123  * @param v      Value to extract from
124  * @param lsb    LSB of number
125  * @param msb    MSB, which is the signed bit
126  *
127  * @return Extracted number
128  */
129 static inline int64_t bdk_extract_smag(uint64_t v, int lsb, int msb) __attribute__((always_inline));
bdk_extract_smag(uint64_t v,int lsb,int msb)130 static inline int64_t bdk_extract_smag(uint64_t v, int lsb, int msb)
131 {
132     int64_t r = bdk_extract(v, lsb, msb - lsb);
133     if (v & (1ull << msb))
134         r = -r;
135     return r;
136 }
137 
138 /**
139  * Insert bits into a number
140  *
141  * @param original Original data, before insert
142  * @param input    Data to insert
143  * @param lsb    Starting bit, least significant (0-63)
144  * @param width  Width in bits (1-64)
145  *
146  * @return Number with inserted bits
147  */
148 static inline uint64_t bdk_insert(uint64_t original, uint64_t input, int lsb, int width) __attribute__((always_inline));
bdk_insert(uint64_t original,uint64_t input,int lsb,int width)149 static inline uint64_t bdk_insert(uint64_t original, uint64_t input, int lsb, int width)
150 {
151     uint64_t mask = bdk_build_mask(width);
152     uint64_t result = original & ~(mask << lsb);
153     result |= (input & mask) << lsb;
154     return result;
155 }
156 
157 /**
158  * Return the number of cores available in the chip
159  *
160  * @return
161  */
bdk_get_num_cores(bdk_node_t node)162 static inline int bdk_get_num_cores(bdk_node_t node)
163 {
164     uint64_t available = BDK_CSR_READ(node, BDK_RST_PP_AVAILABLE);
165     return bdk_dpop(available);
166 }
167 
168 
169 /**
170  * Return true if DRAM has been configured
171  *
172  * @return Boolean
173  */
174 static inline int __bdk_is_dram_enabled(bdk_node_t node) __attribute__((always_inline));
__bdk_is_dram_enabled(bdk_node_t node)175 static inline int __bdk_is_dram_enabled(bdk_node_t node)
176 {
177     BDK_CSR_INIT(lmcx_ddr_pll_ctl, node, BDK_LMCX_DDR_PLL_CTL(0));
178     if (CAVIUM_IS_MODEL(CAVIUM_CN8XXX))
179         return lmcx_ddr_pll_ctl.cn83xx.reset_n;
180     else
181         return !lmcx_ddr_pll_ctl.cn9.pll_reset;
182 }
183 
184 /**
185  * Zero a block of memory
186  *
187  * @param start
188  * @param length
189  */
190 static inline void bdk_zero_memory(void *start, uint64_t length) __attribute__((always_inline));
bdk_zero_memory(void * start,uint64_t length)191 static inline void bdk_zero_memory(void *start, uint64_t length)
192 {
193     if (((long)start & BDK_CACHE_LINE_MASK) || (length & BDK_CACHE_LINE_MASK))
194     {
195         /* Use slwo memset for unaligned memory */
196         memset(start, 0, length);
197     }
198     else
199     {
200         void *end = start + length;
201         while (start<end)
202         {
203             asm volatile ("dc zva,%0" : : "r"(start));
204             start += BDK_CACHE_LINE_SIZE;
205         }
206     }
207 }
208 
209 /** @} */
210 #endif	/* !__CB_BDK_UTILS_H__ */
211