cvmx-utils.h revision 232812
1/***********************license start***************
2 * Copyright (c) 2003-2010  Cavium Inc. (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 Inc. 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 INC. 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_CACHE_LINE_SIZE    (128)   // In bytes
99#define CVMX_CACHE_LINE_MASK    (CVMX_CACHE_LINE_SIZE - 1)   // In bytes
100#define CVMX_CACHE_LINE_ALIGNED __attribute__ ((aligned (CVMX_CACHE_LINE_SIZE)))
101
102/**
103 * This macro spins on a field waiting for it to reach a value. It
104 * is common in code to need to wait for a specific field in a CSR
105 * to match a specific value. Conceptually this macro expands to:
106 *
107 * 1) read csr at "address" with a csr typedef of "type"
108 * 2) Check if ("type".s."field" "op" "value")
109 * 3) If #2 isn't true loop to #1 unless too much time has passed.
110 */
111#define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\
112    ({int result;                                                       \
113    do {                                                                \
114        uint64_t done = cvmx_clock_get_count(CVMX_CLOCK_CORE) + (uint64_t)timeout_usec * \
115                        cvmx_clock_get_rate(CVMX_CLOCK_CORE) / 1000000; \
116        type c;                                                         \
117        while (1)                                                       \
118        {                                                               \
119            c.u64 = cvmx_read_csr(address);                             \
120            if ((c.s.field) op (value)) {                               \
121                result = 0;                                             \
122                break;                                                  \
123            } else if (cvmx_clock_get_count(CVMX_CLOCK_CORE) > done) {  \
124                result = -1;                                            \
125                break;                                                  \
126            } else                                                      \
127                cvmx_wait(100);                                         \
128        }                                                               \
129    } while (0);                                                        \
130    result;})
131
132#define CVMX_BUILD_ASSERT_ZERO(e)    (sizeof(struct {int __static_assert:(e)?1:-1;}))
133#define CVMX_BUILD_ASSERT(condition) ((void)CVMX_BUILD_ASSERT_ZERO(condition))
134
135/**
136 * Builds a bit mask given the required size in bits.
137 *
138 * @param bits   Number of bits in the mask
139 * @return The mask
140 */
141static inline uint64_t cvmx_build_mask(uint64_t bits)
142{
143    return ~((~0x0ull) << bits);
144}
145
146
147/**
148 * Builds a memory address for I/O based on the Major and Sub DID.
149 *
150 * @param major_did 5 bit major did
151 * @param sub_did   3 bit sub did
152 * @return I/O base address
153 */
154static inline uint64_t cvmx_build_io_address(uint64_t major_did, uint64_t sub_did)
155{
156    return ((0x1ull << 48) | (major_did << 43) | (sub_did << 40));
157}
158
159
160/**
161 * Perform mask and shift to place the supplied value into
162 * the supplied bit rage.
163 *
164 * Example: cvmx_build_bits(39,24,value)
165 * <pre>
166 * 6       5       4       3       3       2       1
167 * 3       5       7       9       1       3       5       7      0
168 * +-------+-------+-------+-------+-------+-------+-------+------+
169 * 000000000000000000000000___________value000000000000000000000000
170 * </pre>
171 *
172 * @param high_bit Highest bit value can occupy (inclusive) 0-63
173 * @param low_bit  Lowest bit value can occupy inclusive 0-high_bit
174 * @param value    Value to use
175 * @return Value masked and shifted
176 */
177static inline uint64_t cvmx_build_bits(uint64_t high_bit, uint64_t low_bit, uint64_t value)
178{
179    return ((value & cvmx_build_mask(high_bit - low_bit + 1)) << low_bit);
180}
181
182
183/**
184 * Return the number of cores available in the chip
185 *
186 * @return
187 */
188static inline uint32_t cvmx_octeon_num_cores(void)
189{
190    uint32_t ciu_fuse = (uint32_t)cvmx_read_csr(CVMX_CIU_FUSE) & 0xffffffffull;
191    return cvmx_pop(ciu_fuse);
192}
193
194
195/**
196 * Return true if Octeon is CN36XX
197 *
198 * @return
199 */
200static inline int cvmx_octeon_model_CN36XX(void)
201{
202    return(OCTEON_IS_MODEL(OCTEON_CN38XX)
203           &&cvmx_fuse_read(264));
204}
205
206
207#ifdef	__cplusplus
208}
209#endif
210
211#endif /* __CVMX_UTILS_H__ */
212
213