cvmx-utils.h revision 215990
1/***********************license start*************** 2 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights 3 * reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 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. 17 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 21 * permission. 22 23 * This Software, including technical data, may be subject to U.S. export control 24 * laws, including the U.S. Export Administration Act and its associated 25 * regulations, and may be subject to export or import regulations in other 26 * countries. 27 28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR 30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38 ***********************license end**************************************/ 39 40 41/** 42 * @file 43 * Small utility functions and macros to ease programming of Octeon. 44 * 45 * <hr>$Revision: 38306 $<hr> 46*/ 47#ifndef __CVMX_UTILS_H__ 48#define __CVMX_UTILS_H__ 49 50#if !defined(__FreeBSD__) || !defined(_KERNEL) 51#include <stdarg.h> 52#endif 53 54#ifdef __cplusplus 55extern "C" { 56#endif 57 58#ifndef TRUE 59#define FALSE 0 60#define TRUE (!(FALSE)) 61#endif 62 63/* 64 * The macros cvmx_likely and cvmx_unlikely use the 65 * __builtin_expect GCC operation to control branch 66 * probabilities for a conditional. For example, an "if" 67 * statement in the code that will almost always be 68 * executed should be written as "if (cvmx_likely(...))". 69 * If the "else" section of an if statement is more 70 * probable, use "if (cvmx_unlikey(...))". 71 */ 72#define cvmx_likely(x) __builtin_expect(!!(x), 1) 73#define cvmx_unlikely(x) __builtin_expect(!!(x), 0) 74 75#if CVMX_ENABLE_DEBUG_PRINTS 76 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL 77 #define cvmx_dprintf printk 78 #define cvmx_dvprintf vprintk 79 #else 80 #define cvmx_dprintf printf 81 #define cvmx_dvprintf vprintf 82 #endif 83#else 84 static inline void cvmx_dvprintf(const char *format, va_list ap) 85 { 86 /* Prints are disbled, do nothing */ 87 } 88 89 static inline void cvmx_dprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2))); 90 static inline void cvmx_dprintf(const char *format, ...) 91 { 92 /* Prints are disbled, do nothing */ 93 } 94#endif 95 96#define CAST64(v) ((long long)(long)(v)) // use only when 'v' is a pointer 97#define CASTPTR(type, v) ((type *)(long)(v)) 98#define CVMX_MAX_CORES (16) 99#define CVMX_CACHE_LINE_SIZE (128) // In bytes 100#define CVMX_CACHE_LINE_MASK (CVMX_CACHE_LINE_SIZE - 1) // In bytes 101#define CVMX_CACHE_LINE_ALIGNED __attribute__ ((aligned (CVMX_CACHE_LINE_SIZE))) 102 103/** 104 * This macro spins on a field waiting for it to reach a value. It 105 * is common in code to need to wait for a specific field in a CSR 106 * to match a specific value. Conceptually this macro expands to: 107 * 108 * 1) read csr at "address" with a csr typedef of "type" 109 * 2) Check if ("type".s."field" "op" "value") 110 * 3) If #2 isn't true loop to #1 unless too much time has passed. 111 */ 112#define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\ 113 ({int result; \ 114 do { \ 115 uint64_t done = cvmx_clock_get_count(CVMX_CLOCK_CORE) + (uint64_t)timeout_usec * \ 116 cvmx_clock_get_rate(CVMX_CLOCK_CORE) / 1000000; \ 117 type c; \ 118 while (1) \ 119 { \ 120 c.u64 = cvmx_read_csr(address); \ 121 if ((c.s.field) op (value)) { \ 122 result = 0; \ 123 break; \ 124 } else if (cvmx_clock_get_count(CVMX_CLOCK_CORE) > done) { \ 125 result = -1; \ 126 break; \ 127 } else \ 128 cvmx_wait(100); \ 129 } \ 130 } while (0); \ 131 result;}) 132 133/** 134 * Builds a bit mask given the required size in bits. 135 * 136 * @param bits Number of bits in the mask 137 * @return The mask 138 */ 139static inline uint64_t cvmx_build_mask(uint64_t bits) 140{ 141 return ~((~0x0ull) << bits); 142} 143 144 145/** 146 * Builds a memory address for I/O based on the Major and Sub DID. 147 * 148 * @param major_did 5 bit major did 149 * @param sub_did 3 bit sub did 150 * @return I/O base address 151 */ 152static inline uint64_t cvmx_build_io_address(uint64_t major_did, uint64_t sub_did) 153{ 154 return ((0x1ull << 48) | (major_did << 43) | (sub_did << 40)); 155} 156 157 158/** 159 * Perform mask and shift to place the supplied value into 160 * the supplied bit rage. 161 * 162 * Example: cvmx_build_bits(39,24,value) 163 * <pre> 164 * 6 5 4 3 3 2 1 165 * 3 5 7 9 1 3 5 7 0 166 * +-------+-------+-------+-------+-------+-------+-------+------+ 167 * 000000000000000000000000___________value000000000000000000000000 168 * </pre> 169 * 170 * @param high_bit Highest bit value can occupy (inclusive) 0-63 171 * @param low_bit Lowest bit value can occupy inclusive 0-high_bit 172 * @param value Value to use 173 * @return Value masked and shifted 174 */ 175static inline uint64_t cvmx_build_bits(uint64_t high_bit, uint64_t low_bit, uint64_t value) 176{ 177 return ((value & cvmx_build_mask(high_bit - low_bit + 1)) << low_bit); 178} 179 180 181/** 182 * Return the number of cores available in the chip 183 * 184 * @return 185 */ 186static inline uint32_t cvmx_octeon_num_cores(void) 187{ 188 uint32_t ciu_fuse = (uint32_t)cvmx_read_csr(CVMX_CIU_FUSE) & 0xffff; 189 return cvmx_pop(ciu_fuse); 190} 191 192 193/** 194 * Return true if Octeon is CN36XX 195 * 196 * @return 197 */ 198static inline int cvmx_octeon_model_CN36XX(void) 199{ 200 return(OCTEON_IS_MODEL(OCTEON_CN38XX) 201 &&cvmx_fuse_read(264)); 202} 203 204 205/** 206 * @deprecated 207 * Determine if Octeon supports the DFA state machines. This function is 208 * deprecated, use octeon_has_feature(OCTEON_FEATURE_DFA) instead. 209 * 210 * @return Non zero if DFA is supported 211 */ 212static inline int cvmx_octeon_dfa_present(void) __attribute__((deprecated)); 213static inline int cvmx_octeon_dfa_present(void) 214{ 215 return octeon_has_feature(OCTEON_FEATURE_DFA); 216} 217 218 219/** 220 * @deprecated 221 * Determine if Octeon supports ZIP. This function is deprecated, use 222 * octeon_has_feature(OCTEON_FEATURE_ZIP) instead. 223 * 224 * @return Non zero if DFA is supported 225 */ 226static inline int cvmx_octeon_zip_present(void) __attribute__((deprecated)); 227static inline int cvmx_octeon_zip_present(void) 228{ 229 return octeon_has_feature(OCTEON_FEATURE_ZIP); 230} 231 232 233/** 234 * @deprecated 235 * Determine if Octeon supports Crypto acceleration. This function is 236 * deprecated, use octeon_has_feature(OCTEON_FEATURE_CRYPTO) instead. 237 * 238 * @return Non zero if DFA is supported 239 */ 240static inline int cvmx_octeon_crypto_present(void) __attribute__((deprecated)); 241static inline int cvmx_octeon_crypto_present(void) 242{ 243 return octeon_has_feature(OCTEON_FEATURE_CRYPTO); 244} 245 246 247/** 248 * @deprecated 249 * This function is a trival wrapper around cvmx_read64_uint64(). Use 250 * cvmx_read64_uint64() instead as this function is deprecated. 251 * 252 * @param address 253 * 254 * @return 255 */ 256static inline uint64_t cvmx_read64(uint64_t address) __attribute__((deprecated)); 257static inline uint64_t cvmx_read64(uint64_t address) 258{ 259 return cvmx_read64_uint64(address); 260} 261 262 263/** 264 * @deprecated 265 * This function is a trival wrapper around cvmx_write64_uint64(). Use 266 * cvmx_write64_uint64() instead as this function is deprecated. 267 * 268 * @param address Location to write ro 269 * @param value Value to write 270 * 271 * @return 272 */ 273static inline void cvmx_write64(uint64_t address, uint64_t value) __attribute__((deprecated)); 274static inline void cvmx_write64(uint64_t address, uint64_t value) 275{ 276 cvmx_write64_uint64(address, value); 277} 278 279#ifdef __cplusplus 280} 281#endif 282 283#endif /* __CVMX_UTILS_H__ */ 284 285