1 /***********************license start***************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2017 Cavium, Inc.
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 ***********************license end**************************************/
31 #include <linux/kernel.h>
32 #include <linux/string.h>
33 #include <linux/delay.h>
35 enum cvmx_mips_space
{
36 CVMX_MIPS_SPACE_XKSEG
= 3LL,
37 CVMX_MIPS_SPACE_XKPHYS
= 2LL,
38 CVMX_MIPS_SPACE_XSSEG
= 1LL,
39 CVMX_MIPS_SPACE_XUSEG
= 0LL
42 /* These macros for use when using 32 bit pointers. */
43 #define CVMX_MIPS32_SPACE_KSEG0 1l
44 #define CVMX_ADD_SEG32(segment, add) \
45 (((int32_t)segment << 31) | (int32_t)(add))
47 #define CVMX_IO_SEG CVMX_MIPS_SPACE_XKPHYS
49 /* These macros simplify the process of creating common IO addresses */
50 #define CVMX_ADD_SEG(segment, add) \
51 ((((uint64_t)segment) << 62) | (add))
52 #ifndef CVMX_ADD_IO_SEG
53 #define CVMX_ADD_IO_SEG(add) CVMX_ADD_SEG(CVMX_IO_SEG, (add))
56 #include <asm/octeon/cvmx-asm.h>
57 #include <asm/octeon/cvmx-packet.h>
58 #include <asm/octeon/cvmx-sysinfo.h>
60 #include <asm/octeon/cvmx-ciu-defs.h>
61 #include <asm/octeon/cvmx-ciu3-defs.h>
62 #include <asm/octeon/cvmx-gpio-defs.h>
63 #include <asm/octeon/cvmx-iob-defs.h>
64 #include <asm/octeon/cvmx-ipd-defs.h>
65 #include <asm/octeon/cvmx-l2c-defs.h>
66 #include <asm/octeon/cvmx-l2d-defs.h>
67 #include <asm/octeon/cvmx-l2t-defs.h>
68 #include <asm/octeon/cvmx-led-defs.h>
69 #include <asm/octeon/cvmx-mio-defs.h>
70 #include <asm/octeon/cvmx-pow-defs.h>
72 #include <asm/octeon/cvmx-bootinfo.h>
73 #include <asm/octeon/cvmx-bootmem.h>
74 #include <asm/octeon/cvmx-l2c.h>
76 #ifndef CVMX_ENABLE_DEBUG_PRINTS
77 #define CVMX_ENABLE_DEBUG_PRINTS 1
80 #if CVMX_ENABLE_DEBUG_PRINTS
81 #define cvmx_dprintf printk
83 #define cvmx_dprintf(...) {}
86 #define CVMX_MAX_CORES (16)
87 #define CVMX_CACHE_LINE_SIZE (128) /* In bytes */
88 #define CVMX_CACHE_LINE_MASK (CVMX_CACHE_LINE_SIZE - 1) /* In bytes */
89 #define CVMX_CACHE_LINE_ALIGNED __attribute__ ((aligned(CVMX_CACHE_LINE_SIZE)))
90 #define CAST64(v) ((long long)(long)(v))
91 #define CASTPTR(type, v) ((type *)(long)(v))
94 * Returns processor ID, different Linux and simple exec versions
95 * provided in the cvmx-app-init*.c files.
97 static inline uint32_t cvmx_get_proc_id(void) __attribute__ ((pure
));
98 static inline uint32_t cvmx_get_proc_id(void)
101 asm("mfc0 %0, $15,0" : "=r"(id
));
105 /* turn the variable name into a string */
106 #define CVMX_TMP_STR(x) CVMX_TMP_STR2(x)
107 #define CVMX_TMP_STR2(x) #x
110 * Builds a bit mask given the required size in bits.
112 * @bits: Number of bits in the mask
114 */ static inline uint64_t cvmx_build_mask(uint64_t bits
)
116 return ~((~0x0ull
) << bits
);
120 * Builds a memory address for I/O based on the Major and Sub DID.
122 * @major_did: 5 bit major did
123 * @sub_did: 3 bit sub did
124 * Returns I/O base address
126 static inline uint64_t cvmx_build_io_address(uint64_t major_did
,
129 return (0x1ull
<< 48) | (major_did
<< 43) | (sub_did
<< 40);
133 * Perform mask and shift to place the supplied value into
134 * the supplied bit rage.
136 * Example: cvmx_build_bits(39,24,value)
140 * +-------+-------+-------+-------+-------+-------+-------+------+
141 * 000000000000000000000000___________value000000000000000000000000
144 * @high_bit: Highest bit value can occupy (inclusive) 0-63
145 * @low_bit: Lowest bit value can occupy inclusive 0-high_bit
146 * @value: Value to use
147 * Returns Value masked and shifted
149 static inline uint64_t cvmx_build_bits(uint64_t high_bit
,
150 uint64_t low_bit
, uint64_t value
)
152 return (value
& cvmx_build_mask(high_bit
- low_bit
+ 1)) << low_bit
;
156 * Convert a memory pointer (void*) into a hardware compatible
157 * memory address (phys_addr_t). Octeon hardware widgets don't
158 * understand logical addresses.
160 * @ptr: C style memory pointer
161 * Returns Hardware physical address
163 static inline phys_addr_t
cvmx_ptr_to_phys(void *ptr
)
165 if (sizeof(void *) == 8) {
167 * We're running in 64 bit mode. Normally this means
168 * that we can use 40 bits of address space (the
169 * hardware limit). Unfortunately there is one case
170 * were we need to limit this to 30 bits, sign
171 * extended 32 bit. Although these are 64 bits wide,
172 * only 30 bits can be used.
174 if ((CAST64(ptr
) >> 62) == 3)
175 return CAST64(ptr
) & cvmx_build_mask(30);
177 return CAST64(ptr
) & cvmx_build_mask(40);
179 return (long)(ptr
) & 0x1fffffff;
184 * Convert a hardware physical address (uint64_t) into a
185 * memory pointer (void *).
188 * Hardware physical address to memory
189 * Returns Pointer to memory
191 static inline void *cvmx_phys_to_ptr(uint64_t physical_address
)
193 if (sizeof(void *) == 8) {
194 /* Just set the top bit, avoiding any TLB ugliness */
196 CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
200 CVMX_ADD_SEG32(CVMX_MIPS32_SPACE_KSEG0
,
205 /* The following #if controls the definition of the macro
206 CVMX_BUILD_WRITE64. This macro is used to build a store operation to
207 a full 64bit address. With a 64bit ABI, this can be done with a simple
208 pointer access. 32bit ABIs require more complicated assembly */
210 /* We have a full 64bit ABI. Writing to a 64bit address can be done with
211 a simple volatile pointer */
212 #define CVMX_BUILD_WRITE64(TYPE, ST) \
213 static inline void cvmx_write64_##TYPE(uint64_t addr, TYPE##_t val) \
215 *CASTPTR(volatile TYPE##_t, addr) = val; \
219 /* The following #if controls the definition of the macro
220 CVMX_BUILD_READ64. This macro is used to build a load operation from
221 a full 64bit address. With a 64bit ABI, this can be done with a simple
222 pointer access. 32bit ABIs require more complicated assembly */
224 /* We have a full 64bit ABI. Writing to a 64bit address can be done with
225 a simple volatile pointer */
226 #define CVMX_BUILD_READ64(TYPE, LT) \
227 static inline TYPE##_t cvmx_read64_##TYPE(uint64_t addr) \
229 return *CASTPTR(volatile TYPE##_t, addr); \
233 /* The following defines 8 functions for writing to a 64bit address. Each
234 takes two arguments, the address and the value to write.
235 cvmx_write64_int64 cvmx_write64_uint64
236 cvmx_write64_int32 cvmx_write64_uint32
237 cvmx_write64_int16 cvmx_write64_uint16
238 cvmx_write64_int8 cvmx_write64_uint8 */
239 CVMX_BUILD_WRITE64(int64
, "sd");
240 CVMX_BUILD_WRITE64(int32
, "sw");
241 CVMX_BUILD_WRITE64(int16
, "sh");
242 CVMX_BUILD_WRITE64(int8
, "sb");
243 CVMX_BUILD_WRITE64(uint64
, "sd");
244 CVMX_BUILD_WRITE64(uint32
, "sw");
245 CVMX_BUILD_WRITE64(uint16
, "sh");
246 CVMX_BUILD_WRITE64(uint8
, "sb");
247 #define cvmx_write64 cvmx_write64_uint64
249 /* The following defines 8 functions for reading from a 64bit address. Each
250 takes the address as the only argument
251 cvmx_read64_int64 cvmx_read64_uint64
252 cvmx_read64_int32 cvmx_read64_uint32
253 cvmx_read64_int16 cvmx_read64_uint16
254 cvmx_read64_int8 cvmx_read64_uint8 */
255 CVMX_BUILD_READ64(int64
, "ld");
256 CVMX_BUILD_READ64(int32
, "lw");
257 CVMX_BUILD_READ64(int16
, "lh");
258 CVMX_BUILD_READ64(int8
, "lb");
259 CVMX_BUILD_READ64(uint64
, "ld");
260 CVMX_BUILD_READ64(uint32
, "lw");
261 CVMX_BUILD_READ64(uint16
, "lhu");
262 CVMX_BUILD_READ64(uint8
, "lbu");
263 #define cvmx_read64 cvmx_read64_uint64
266 static inline void cvmx_write_csr(uint64_t csr_addr
, uint64_t val
)
268 cvmx_write64(csr_addr
, val
);
271 * Perform an immediate read after every write to an RSL
272 * register to force the write to complete. It doesn't matter
273 * what RSL read we do, so we choose CVMX_MIO_BOOT_BIST_STAT
274 * because it is fast and harmless.
276 if (((csr_addr
>> 40) & 0x7ffff) == (0x118))
277 cvmx_read64(CVMX_MIO_BOOT_BIST_STAT
);
280 static inline void cvmx_writeq_csr(void __iomem
*csr_addr
, uint64_t val
)
282 cvmx_write_csr((__force
uint64_t)csr_addr
, val
);
285 static inline void cvmx_write_io(uint64_t io_addr
, uint64_t val
)
287 cvmx_write64(io_addr
, val
);
291 static inline uint64_t cvmx_read_csr(uint64_t csr_addr
)
293 uint64_t val
= cvmx_read64(csr_addr
);
297 static inline uint64_t cvmx_readq_csr(void __iomem
*csr_addr
)
299 return cvmx_read_csr((__force
uint64_t) csr_addr
);
302 static inline void cvmx_send_single(uint64_t data
)
304 const uint64_t CVMX_IOBDMA_SENDSINGLE
= 0xffffffffffffa200ull
;
305 cvmx_write64(CVMX_IOBDMA_SENDSINGLE
, data
);
308 static inline void cvmx_read_csr_async(uint64_t scraddr
, uint64_t csr_addr
)
319 addr
.s
.scraddr
= scraddr
>> 3;
321 cvmx_send_single(addr
.u64
);
324 /* Return true if Octeon is CN38XX pass 1 */
325 static inline int cvmx_octeon_is_pass1(void)
327 #if OCTEON_IS_COMMON_BINARY()
328 return 0; /* Pass 1 isn't supported for common binaries */
330 /* Now that we know we're built for a specific model, only check CN38XX */
331 #if OCTEON_IS_MODEL(OCTEON_CN38XX)
332 return cvmx_get_proc_id() == OCTEON_CN38XX_PASS1
;
334 return 0; /* Built for non CN38XX chip, we're not CN38XX pass1 */
339 static inline unsigned int cvmx_get_core_num(void)
341 unsigned int core_num
;
342 CVMX_RDHWRNV(core_num
, 0);
346 /* Maximum # of bits to define core in node */
347 #define CVMX_NODE_NO_SHIFT 7
348 #define CVMX_NODE_MASK 0x3
349 static inline unsigned int cvmx_get_node_num(void)
351 unsigned int core_num
= cvmx_get_core_num();
353 return (core_num
>> CVMX_NODE_NO_SHIFT
) & CVMX_NODE_MASK
;
356 static inline unsigned int cvmx_get_local_core_num(void)
358 return cvmx_get_core_num() & ((1 << CVMX_NODE_NO_SHIFT
) - 1);
361 #define CVMX_NODE_BITS (2) /* Number of bits to define a node */
362 #define CVMX_MAX_NODES (1 << CVMX_NODE_BITS)
363 #define CVMX_NODE_IO_SHIFT (36)
364 #define CVMX_NODE_MEM_SHIFT (40)
365 #define CVMX_NODE_IO_MASK ((uint64_t)CVMX_NODE_MASK << CVMX_NODE_IO_SHIFT)
367 static inline void cvmx_write_csr_node(uint64_t node
, uint64_t csr_addr
,
370 uint64_t composite_csr_addr
, node_addr
;
372 node_addr
= (node
& CVMX_NODE_MASK
) << CVMX_NODE_IO_SHIFT
;
373 composite_csr_addr
= (csr_addr
& ~CVMX_NODE_IO_MASK
) | node_addr
;
375 cvmx_write64_uint64(composite_csr_addr
, val
);
376 if (((csr_addr
>> 40) & 0x7ffff) == (0x118))
377 cvmx_read64_uint64(CVMX_MIO_BOOT_BIST_STAT
| node_addr
);
380 static inline uint64_t cvmx_read_csr_node(uint64_t node
, uint64_t csr_addr
)
384 node_addr
= (csr_addr
& ~CVMX_NODE_IO_MASK
) |
385 (node
& CVMX_NODE_MASK
) << CVMX_NODE_IO_SHIFT
;
386 return cvmx_read_csr(node_addr
);
390 * Returns the number of bits set in the provided value.
391 * Simple wrapper for POP instruction.
393 * @val: 32 bit value to count set bits in
395 * Returns Number of bits set
397 static inline uint32_t cvmx_pop(uint32_t val
)
405 * Returns the number of bits set in the provided value.
406 * Simple wrapper for DPOP instruction.
408 * @val: 64 bit value to count set bits in
410 * Returns Number of bits set
412 static inline int cvmx_dpop(uint64_t val
)
420 * Provide current cycle counter as a return value
422 * Returns current cycle counter
425 static inline uint64_t cvmx_get_cycle(void)
428 CVMX_RDHWR(cycle
, 31);
433 * Reads a chip global cycle counter. This counts CPU cycles since
434 * chip reset. The counter is 64 bit.
435 * This register does not exist on CN38XX pass 1 silicion
437 * Returns Global chip cycle count since chip reset.
439 static inline uint64_t cvmx_get_cycle_global(void)
441 if (cvmx_octeon_is_pass1())
444 return cvmx_read64(CVMX_IPD_CLK_COUNT
);
448 * This macro spins on a field waiting for it to reach a value. It
449 * is common in code to need to wait for a specific field in a CSR
450 * to match a specific value. Conceptually this macro expands to:
452 * 1) read csr at "address" with a csr typedef of "type"
453 * 2) Check if ("type".s."field" "op" "value")
454 * 3) If #2 isn't true loop to #1 unless too much time has passed.
456 #define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\
461 uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
462 cvmx_sysinfo_get()->cpu_clock_hz / 1000000; \
465 c.u64 = cvmx_read_csr(address); \
466 if ((c.s.field) op(value)) { \
469 } else if (cvmx_get_cycle() > done) { \
479 /***************************************************************************/
481 /* Return the number of cores available in the chip */
482 static inline uint32_t cvmx_octeon_num_cores(void)
487 if (OCTEON_IS_OCTEON3() && !OCTEON_IS_MODEL(OCTEON_CN70XX
))
488 ciu_fuse_reg
= CVMX_CIU3_FUSE
;
490 ciu_fuse_reg
= CVMX_CIU_FUSE
;
491 ciu_fuse
= cvmx_read_csr(ciu_fuse_reg
);
492 return cvmx_dpop(ciu_fuse
);
495 #endif /* __CVMX_H__ */