1 /***********************license start***************
2 * Copyright (c) 2003-2009 Cavium Networks (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * * Neither the name of Cavium Networks nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31 * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT
32 * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
41 * Small utility functions and macros to ease programming of Octeon.
43 * <hr>$Revision: 38306 $<hr>
45 #ifndef __CVMX_UTILS_H__
46 #define __CVMX_UTILS_H__
54 #define TRUE (!(FALSE))
58 * The macros cvmx_likely and cvmx_unlikely use the
59 * __builtin_expect GCC operation to control branch
60 * probabilities for a conditional. For example, an "if"
61 * statement in the code that will almost always be
62 * executed should be written as "if (cvmx_likely(...))".
63 * If the "else" section of an if statement is more
64 * probable, use "if (cvmx_unlikey(...))".
66 #define cvmx_likely(x) __builtin_expect(!!(x), 1)
67 #define cvmx_unlikely(x) __builtin_expect(!!(x), 0)
69 #if CVMX_ENABLE_DEBUG_PRINTS
70 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
71 #define cvmx_dprintf printk
73 #define cvmx_dprintf printf
76 static inline void cvmx_dprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2)));
77 static inline void cvmx_dprintf(const char *format, ...)
79 /* Prints are disbled, do nothing */
83 #define CAST64(v) ((long long)(long)(v))
84 #define CASTPTR(type, v) ((type *)(long)(v))
85 #define CVMX_MAX_CORES (16)
86 #define CVMX_CACHE_LINE_SIZE (128) // In bytes
87 #define CVMX_CACHE_LINE_MASK (CVMX_CACHE_LINE_SIZE - 1) // In bytes
88 #define CVMX_CACHE_LINE_ALIGNED __attribute__ ((aligned (CVMX_CACHE_LINE_SIZE)))
91 * This macro spins on a field waiting for it to reach a value. It
92 * is common in code to need to wait for a specific field in a CSR
93 * to match a specific value. Conceptually this macro expands to:
95 * 1) read csr at "address" with a csr typedef of "type"
96 * 2) Check if ("type".s."field" "op" "value")
97 * 3) If #2 isn't true loop to #1 unless too much time has passed.
99 #define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\
102 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
103 cvmx_sysinfo_get()->cpu_clock_hz / 1000000; \
107 c.u64 = cvmx_read_csr(address); \
108 if ((c.s.field) op (value)) { \
111 } else if (cvmx_get_cycle() > done) { \
121 * Builds a bit mask given the required size in bits.
123 * @param bits Number of bits in the mask
126 static inline uint64_t cvmx_build_mask(uint64_t bits)
128 return ~((~0x0ull) << bits);
133 * Builds a memory address for I/O based on the Major and Sub DID.
135 * @param major_did 5 bit major did
136 * @param sub_did 3 bit sub did
137 * @return I/O base address
139 static inline uint64_t cvmx_build_io_address(uint64_t major_did, uint64_t sub_did)
141 return ((0x1ull << 48) | (major_did << 43) | (sub_did << 40));
146 * Perform mask and shift to place the supplied value into
147 * the supplied bit rage.
149 * Example: cvmx_build_bits(39,24,value)
153 * +-------+-------+-------+-------+-------+-------+-------+------+
154 * 000000000000000000000000___________value000000000000000000000000
157 * @param high_bit Highest bit value can occupy (inclusive) 0-63
158 * @param low_bit Lowest bit value can occupy inclusive 0-high_bit
159 * @param value Value to use
160 * @return Value masked and shifted
162 static inline uint64_t cvmx_build_bits(uint64_t high_bit, uint64_t low_bit, uint64_t value)
164 return ((value & cvmx_build_mask(high_bit - low_bit + 1)) << low_bit);
169 * Return the number of cores available in the chip
173 static inline uint32_t cvmx_octeon_num_cores(void)
175 uint32_t ciu_fuse = (uint32_t)cvmx_read_csr(CVMX_CIU_FUSE) & 0xffff;
176 return cvmx_pop(ciu_fuse);
181 * Return true if Octeon is CN38XX pass 1
185 static inline int cvmx_octeon_is_pass1(void)
187 return OCTEON_IS_MODEL(OCTEON_CN38XX_PASS1);
192 * Return true if Octeon is CN36XX
196 static inline int cvmx_octeon_model_CN36XX(void)
198 return(OCTEON_IS_MODEL(OCTEON_CN38XX)
199 && !OCTEON_IS_MODEL(OCTEON_CN38XX_PASS1)
200 &&cvmx_fuse_read(264));
206 * Determine if Octeon supports the DFA state machines. This function is
207 * deprecated, use octeon_has_feature(OCTEON_FEATURE_DFA) instead.
209 * @return Non zero if DFA is supported
211 static inline int cvmx_octeon_dfa_present(void) __attribute__((deprecated));
212 static inline int cvmx_octeon_dfa_present(void)
214 return octeon_has_feature(OCTEON_FEATURE_DFA);
220 * Determine if Octeon supports ZIP. This function is deprecated, use
221 * octeon_has_feature(OCTEON_FEATURE_ZIP) instead.
223 * @return Non zero if DFA is supported
225 static inline int cvmx_octeon_zip_present(void) __attribute__((deprecated));
226 static inline int cvmx_octeon_zip_present(void)
228 return octeon_has_feature(OCTEON_FEATURE_ZIP);
234 * Determine if Octeon supports Crypto acceleration. This function is
235 * deprecated, use octeon_has_feature(OCTEON_FEATURE_CRYPTO) instead.
237 * @return Non zero if DFA is supported
239 static inline int cvmx_octeon_crypto_present(void) __attribute__((deprecated));
240 static inline int cvmx_octeon_crypto_present(void)
242 return octeon_has_feature(OCTEON_FEATURE_CRYPTO);
248 * This function is a trival wrapper around cvmx_read64_uint64(). Use
249 * cvmx_read64_uint64() instead as this function is deprecated.
255 static inline uint64_t cvmx_read64(uint64_t address) __attribute__((deprecated));
256 static inline uint64_t cvmx_read64(uint64_t address)
258 return cvmx_read64_uint64(address);
264 * This function is a trival wrapper around cvmx_write64_uint64(). Use
265 * cvmx_write64_uint64() instead as this function is deprecated.
267 * @param address Location to write ro
268 * @param value Value to write
272 static inline void cvmx_write64(uint64_t address, uint64_t value) __attribute__((deprecated));
273 static inline void cvmx_write64(uint64_t address, uint64_t value)
275 cvmx_write64_uint64(address, value);
282 #endif /* __CVMX_UTILS_H__ */