xref: /openbmc/linux/arch/mips/include/asm/octeon/cvmx.h (revision 81a67e52)
158f07778SDavid Daney /***********************license start***************
258f07778SDavid Daney  * Author: Cavium Networks
358f07778SDavid Daney  *
458f07778SDavid Daney  * Contact: support@caviumnetworks.com
558f07778SDavid Daney  * This file is part of the OCTEON SDK
658f07778SDavid Daney  *
715f68479SSteven J. Hill  * Copyright (c) 2003-2017 Cavium, Inc.
858f07778SDavid Daney  *
958f07778SDavid Daney  * This file is free software; you can redistribute it and/or modify
1058f07778SDavid Daney  * it under the terms of the GNU General Public License, Version 2, as
1158f07778SDavid Daney  * published by the Free Software Foundation.
1258f07778SDavid Daney  *
1358f07778SDavid Daney  * This file is distributed in the hope that it will be useful, but
1458f07778SDavid Daney  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
1558f07778SDavid Daney  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
1658f07778SDavid Daney  * NONINFRINGEMENT.  See the GNU General Public License for more
1758f07778SDavid Daney  * details.
1858f07778SDavid Daney  *
1958f07778SDavid Daney  * You should have received a copy of the GNU General Public License
2058f07778SDavid Daney  * along with this file; if not, write to the Free Software
2158f07778SDavid Daney  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2258f07778SDavid Daney  * or visit http://www.gnu.org/licenses/.
2358f07778SDavid Daney  *
2458f07778SDavid Daney  * This file may also be available under a different license from Cavium.
2558f07778SDavid Daney  * Contact Cavium Networks for more information
2658f07778SDavid Daney  ***********************license end**************************************/
2758f07778SDavid Daney 
2858f07778SDavid Daney #ifndef __CVMX_H__
2958f07778SDavid Daney #define __CVMX_H__
3058f07778SDavid Daney 
3158f07778SDavid Daney #include <linux/kernel.h>
3258f07778SDavid Daney #include <linux/string.h>
3358f07778SDavid Daney 
3426afc5e3SDavid Daney enum cvmx_mips_space {
3526afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XKSEG = 3LL,
3626afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XKPHYS = 2LL,
3726afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XSSEG = 1LL,
3826afc5e3SDavid Daney 	CVMX_MIPS_SPACE_XUSEG = 0LL
3926afc5e3SDavid Daney };
4026afc5e3SDavid Daney 
4126afc5e3SDavid Daney /* These macros for use when using 32 bit pointers. */
4226afc5e3SDavid Daney #define CVMX_MIPS32_SPACE_KSEG0 1l
4326afc5e3SDavid Daney #define CVMX_ADD_SEG32(segment, add) \
4426afc5e3SDavid Daney 	(((int32_t)segment << 31) | (int32_t)(add))
4526afc5e3SDavid Daney 
4626afc5e3SDavid Daney #define CVMX_IO_SEG CVMX_MIPS_SPACE_XKPHYS
4726afc5e3SDavid Daney 
4826afc5e3SDavid Daney /* These macros simplify the process of creating common IO addresses */
4926afc5e3SDavid Daney #define CVMX_ADD_SEG(segment, add) \
5026afc5e3SDavid Daney 	((((uint64_t)segment) << 62) | (add))
5126afc5e3SDavid Daney #ifndef CVMX_ADD_IO_SEG
5226afc5e3SDavid Daney #define CVMX_ADD_IO_SEG(add) CVMX_ADD_SEG(CVMX_IO_SEG, (add))
5326afc5e3SDavid Daney #endif
5426afc5e3SDavid Daney 
55a1ce3928SDavid Howells #include <asm/octeon/cvmx-asm.h>
56a1ce3928SDavid Howells #include <asm/octeon/cvmx-packet.h>
57a1ce3928SDavid Howells #include <asm/octeon/cvmx-sysinfo.h>
5858f07778SDavid Daney 
59a1ce3928SDavid Howells #include <asm/octeon/cvmx-ciu-defs.h>
60182a6d1cSDavid Daney #include <asm/octeon/cvmx-ciu3-defs.h>
61a1ce3928SDavid Howells #include <asm/octeon/cvmx-gpio-defs.h>
62a1ce3928SDavid Howells #include <asm/octeon/cvmx-iob-defs.h>
63a1ce3928SDavid Howells #include <asm/octeon/cvmx-ipd-defs.h>
64a1ce3928SDavid Howells #include <asm/octeon/cvmx-l2c-defs.h>
6581a67e52SSteven J. Hill #include <asm/octeon/cvmx-l2d-defs.h>
66a1ce3928SDavid Howells #include <asm/octeon/cvmx-l2t-defs.h>
67a1ce3928SDavid Howells #include <asm/octeon/cvmx-led-defs.h>
68a1ce3928SDavid Howells #include <asm/octeon/cvmx-mio-defs.h>
69a1ce3928SDavid Howells #include <asm/octeon/cvmx-pow-defs.h>
7058f07778SDavid Daney 
71a1ce3928SDavid Howells #include <asm/octeon/cvmx-bootinfo.h>
72a1ce3928SDavid Howells #include <asm/octeon/cvmx-bootmem.h>
73a1ce3928SDavid Howells #include <asm/octeon/cvmx-l2c.h>
7458f07778SDavid Daney 
7558f07778SDavid Daney #ifndef CVMX_ENABLE_DEBUG_PRINTS
7658f07778SDavid Daney #define CVMX_ENABLE_DEBUG_PRINTS 1
7758f07778SDavid Daney #endif
7858f07778SDavid Daney 
7958f07778SDavid Daney #if CVMX_ENABLE_DEBUG_PRINTS
8058f07778SDavid Daney #define cvmx_dprintf	    printk
8158f07778SDavid Daney #else
8258f07778SDavid Daney #define cvmx_dprintf(...)   {}
8358f07778SDavid Daney #endif
8458f07778SDavid Daney 
8558f07778SDavid Daney #define CVMX_MAX_CORES		(16)
8658f07778SDavid Daney #define CVMX_CACHE_LINE_SIZE	(128)	/* In bytes */
8758f07778SDavid Daney #define CVMX_CACHE_LINE_MASK	(CVMX_CACHE_LINE_SIZE - 1)	/* In bytes */
8858f07778SDavid Daney #define CVMX_CACHE_LINE_ALIGNED __attribute__ ((aligned(CVMX_CACHE_LINE_SIZE)))
8958f07778SDavid Daney #define CAST64(v) ((long long)(long)(v))
9058f07778SDavid Daney #define CASTPTR(type, v) ((type *)(long)(v))
9158f07778SDavid Daney 
9258f07778SDavid Daney /*
9358f07778SDavid Daney  * Returns processor ID, different Linux and simple exec versions
9458f07778SDavid Daney  * provided in the cvmx-app-init*.c files.
9558f07778SDavid Daney  */
9658f07778SDavid Daney static inline uint32_t cvmx_get_proc_id(void) __attribute__ ((pure));
9758f07778SDavid Daney static inline uint32_t cvmx_get_proc_id(void)
9858f07778SDavid Daney {
9958f07778SDavid Daney 	uint32_t id;
10058f07778SDavid Daney 	asm("mfc0 %0, $15,0" : "=r"(id));
10158f07778SDavid Daney 	return id;
10258f07778SDavid Daney }
10358f07778SDavid Daney 
10458f07778SDavid Daney /* turn the variable name into a string */
10558f07778SDavid Daney #define CVMX_TMP_STR(x) CVMX_TMP_STR2(x)
10658f07778SDavid Daney #define CVMX_TMP_STR2(x) #x
10758f07778SDavid Daney 
10858f07778SDavid Daney /**
10958f07778SDavid Daney  * Builds a bit mask given the required size in bits.
11058f07778SDavid Daney  *
11158f07778SDavid Daney  * @bits:   Number of bits in the mask
11258f07778SDavid Daney  * Returns The mask
11358f07778SDavid Daney  */ static inline uint64_t cvmx_build_mask(uint64_t bits)
11458f07778SDavid Daney {
11558f07778SDavid Daney 	return ~((~0x0ull) << bits);
11658f07778SDavid Daney }
11758f07778SDavid Daney 
11858f07778SDavid Daney /**
11958f07778SDavid Daney  * Builds a memory address for I/O based on the Major and Sub DID.
12058f07778SDavid Daney  *
12158f07778SDavid Daney  * @major_did: 5 bit major did
12258f07778SDavid Daney  * @sub_did:   3 bit sub did
12358f07778SDavid Daney  * Returns I/O base address
12458f07778SDavid Daney  */
12558f07778SDavid Daney static inline uint64_t cvmx_build_io_address(uint64_t major_did,
12658f07778SDavid Daney 					     uint64_t sub_did)
12758f07778SDavid Daney {
12858f07778SDavid Daney 	return (0x1ull << 48) | (major_did << 43) | (sub_did << 40);
12958f07778SDavid Daney }
13058f07778SDavid Daney 
13158f07778SDavid Daney /**
13258f07778SDavid Daney  * Perform mask and shift to place the supplied value into
13358f07778SDavid Daney  * the supplied bit rage.
13458f07778SDavid Daney  *
13558f07778SDavid Daney  * Example: cvmx_build_bits(39,24,value)
13658f07778SDavid Daney  * <pre>
13758f07778SDavid Daney  * 6	   5	   4	   3	   3	   2	   1
13858f07778SDavid Daney  * 3	   5	   7	   9	   1	   3	   5	   7	  0
13958f07778SDavid Daney  * +-------+-------+-------+-------+-------+-------+-------+------+
14058f07778SDavid Daney  * 000000000000000000000000___________value000000000000000000000000
14158f07778SDavid Daney  * </pre>
14258f07778SDavid Daney  *
14358f07778SDavid Daney  * @high_bit: Highest bit value can occupy (inclusive) 0-63
14458f07778SDavid Daney  * @low_bit:  Lowest bit value can occupy inclusive 0-high_bit
14558f07778SDavid Daney  * @value:    Value to use
14658f07778SDavid Daney  * Returns Value masked and shifted
14758f07778SDavid Daney  */
14858f07778SDavid Daney static inline uint64_t cvmx_build_bits(uint64_t high_bit,
14958f07778SDavid Daney 				       uint64_t low_bit, uint64_t value)
15058f07778SDavid Daney {
15158f07778SDavid Daney 	return (value & cvmx_build_mask(high_bit - low_bit + 1)) << low_bit;
15258f07778SDavid Daney }
15358f07778SDavid Daney 
15458f07778SDavid Daney /**
15525985edcSLucas De Marchi  * Convert a memory pointer (void*) into a hardware compatible
15658f07778SDavid Daney  * memory address (uint64_t). Octeon hardware widgets don't
15758f07778SDavid Daney  * understand logical addresses.
15858f07778SDavid Daney  *
15958f07778SDavid Daney  * @ptr:    C style memory pointer
16058f07778SDavid Daney  * Returns Hardware physical address
16158f07778SDavid Daney  */
16258f07778SDavid Daney static inline uint64_t cvmx_ptr_to_phys(void *ptr)
16358f07778SDavid Daney {
16458f07778SDavid Daney 	if (sizeof(void *) == 8) {
16558f07778SDavid Daney 		/*
16658f07778SDavid Daney 		 * We're running in 64 bit mode. Normally this means
16758f07778SDavid Daney 		 * that we can use 40 bits of address space (the
16858f07778SDavid Daney 		 * hardware limit). Unfortunately there is one case
16958f07778SDavid Daney 		 * were we need to limit this to 30 bits, sign
17058f07778SDavid Daney 		 * extended 32 bit. Although these are 64 bits wide,
17158f07778SDavid Daney 		 * only 30 bits can be used.
17258f07778SDavid Daney 		 */
17358f07778SDavid Daney 		if ((CAST64(ptr) >> 62) == 3)
17458f07778SDavid Daney 			return CAST64(ptr) & cvmx_build_mask(30);
17558f07778SDavid Daney 		else
17658f07778SDavid Daney 			return CAST64(ptr) & cvmx_build_mask(40);
17758f07778SDavid Daney 	} else {
17858f07778SDavid Daney 		return (long)(ptr) & 0x1fffffff;
17958f07778SDavid Daney 	}
18058f07778SDavid Daney }
18158f07778SDavid Daney 
18258f07778SDavid Daney /**
18358f07778SDavid Daney  * Convert a hardware physical address (uint64_t) into a
18458f07778SDavid Daney  * memory pointer (void *).
18558f07778SDavid Daney  *
18658f07778SDavid Daney  * @physical_address:
18758f07778SDavid Daney  *		 Hardware physical address to memory
18858f07778SDavid Daney  * Returns Pointer to memory
18958f07778SDavid Daney  */
19058f07778SDavid Daney static inline void *cvmx_phys_to_ptr(uint64_t physical_address)
19158f07778SDavid Daney {
19258f07778SDavid Daney 	if (sizeof(void *) == 8) {
19392a76f6dSAdam Buchbinder 		/* Just set the top bit, avoiding any TLB ugliness */
19458f07778SDavid Daney 		return CASTPTR(void,
19558f07778SDavid Daney 			       CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
19658f07778SDavid Daney 					    physical_address));
19758f07778SDavid Daney 	} else {
19858f07778SDavid Daney 		return CASTPTR(void,
19958f07778SDavid Daney 			       CVMX_ADD_SEG32(CVMX_MIPS32_SPACE_KSEG0,
20058f07778SDavid Daney 					      physical_address));
20158f07778SDavid Daney 	}
20258f07778SDavid Daney }
20358f07778SDavid Daney 
20458f07778SDavid Daney /* The following #if controls the definition of the macro
20558f07778SDavid Daney     CVMX_BUILD_WRITE64. This macro is used to build a store operation to
20658f07778SDavid Daney     a full 64bit address. With a 64bit ABI, this can be done with a simple
20758f07778SDavid Daney     pointer access. 32bit ABIs require more complicated assembly */
20858f07778SDavid Daney 
20958f07778SDavid Daney /* We have a full 64bit ABI. Writing to a 64bit address can be done with
21058f07778SDavid Daney     a simple volatile pointer */
21158f07778SDavid Daney #define CVMX_BUILD_WRITE64(TYPE, ST)					\
21258f07778SDavid Daney static inline void cvmx_write64_##TYPE(uint64_t addr, TYPE##_t val)	\
21358f07778SDavid Daney {									\
21458f07778SDavid Daney     *CASTPTR(volatile TYPE##_t, addr) = val;				\
21558f07778SDavid Daney }
21658f07778SDavid Daney 
21758f07778SDavid Daney 
21858f07778SDavid Daney /* The following #if controls the definition of the macro
21958f07778SDavid Daney     CVMX_BUILD_READ64. This macro is used to build a load operation from
22058f07778SDavid Daney     a full 64bit address. With a 64bit ABI, this can be done with a simple
22158f07778SDavid Daney     pointer access. 32bit ABIs require more complicated assembly */
22258f07778SDavid Daney 
22358f07778SDavid Daney /* We have a full 64bit ABI. Writing to a 64bit address can be done with
22458f07778SDavid Daney     a simple volatile pointer */
22558f07778SDavid Daney #define CVMX_BUILD_READ64(TYPE, LT)					\
22658f07778SDavid Daney static inline TYPE##_t cvmx_read64_##TYPE(uint64_t addr)		\
22758f07778SDavid Daney {									\
22858f07778SDavid Daney 	return *CASTPTR(volatile TYPE##_t, addr);			\
22958f07778SDavid Daney }
23058f07778SDavid Daney 
23158f07778SDavid Daney 
23258f07778SDavid Daney /* The following defines 8 functions for writing to a 64bit address. Each
23358f07778SDavid Daney     takes two arguments, the address and the value to write.
23458f07778SDavid Daney     cvmx_write64_int64	    cvmx_write64_uint64
23558f07778SDavid Daney     cvmx_write64_int32	    cvmx_write64_uint32
23658f07778SDavid Daney     cvmx_write64_int16	    cvmx_write64_uint16
23758f07778SDavid Daney     cvmx_write64_int8	    cvmx_write64_uint8 */
23858f07778SDavid Daney CVMX_BUILD_WRITE64(int64, "sd");
23958f07778SDavid Daney CVMX_BUILD_WRITE64(int32, "sw");
24058f07778SDavid Daney CVMX_BUILD_WRITE64(int16, "sh");
24158f07778SDavid Daney CVMX_BUILD_WRITE64(int8, "sb");
24258f07778SDavid Daney CVMX_BUILD_WRITE64(uint64, "sd");
24358f07778SDavid Daney CVMX_BUILD_WRITE64(uint32, "sw");
24458f07778SDavid Daney CVMX_BUILD_WRITE64(uint16, "sh");
24558f07778SDavid Daney CVMX_BUILD_WRITE64(uint8, "sb");
24658f07778SDavid Daney #define cvmx_write64 cvmx_write64_uint64
24758f07778SDavid Daney 
24858f07778SDavid Daney /* The following defines 8 functions for reading from a 64bit address. Each
24958f07778SDavid Daney     takes the address as the only argument
25058f07778SDavid Daney     cvmx_read64_int64	    cvmx_read64_uint64
25158f07778SDavid Daney     cvmx_read64_int32	    cvmx_read64_uint32
25258f07778SDavid Daney     cvmx_read64_int16	    cvmx_read64_uint16
25358f07778SDavid Daney     cvmx_read64_int8	    cvmx_read64_uint8 */
25458f07778SDavid Daney CVMX_BUILD_READ64(int64, "ld");
25558f07778SDavid Daney CVMX_BUILD_READ64(int32, "lw");
25658f07778SDavid Daney CVMX_BUILD_READ64(int16, "lh");
25758f07778SDavid Daney CVMX_BUILD_READ64(int8, "lb");
25858f07778SDavid Daney CVMX_BUILD_READ64(uint64, "ld");
25958f07778SDavid Daney CVMX_BUILD_READ64(uint32, "lw");
26058f07778SDavid Daney CVMX_BUILD_READ64(uint16, "lhu");
26158f07778SDavid Daney CVMX_BUILD_READ64(uint8, "lbu");
26258f07778SDavid Daney #define cvmx_read64 cvmx_read64_uint64
26358f07778SDavid Daney 
26458f07778SDavid Daney 
26558f07778SDavid Daney static inline void cvmx_write_csr(uint64_t csr_addr, uint64_t val)
26658f07778SDavid Daney {
26758f07778SDavid Daney 	cvmx_write64(csr_addr, val);
26858f07778SDavid Daney 
26958f07778SDavid Daney 	/*
27058f07778SDavid Daney 	 * Perform an immediate read after every write to an RSL
27158f07778SDavid Daney 	 * register to force the write to complete. It doesn't matter
27258f07778SDavid Daney 	 * what RSL read we do, so we choose CVMX_MIO_BOOT_BIST_STAT
27358f07778SDavid Daney 	 * because it is fast and harmless.
27458f07778SDavid Daney 	 */
27526084411SDavid Daney 	if (((csr_addr >> 40) & 0x7ffff) == (0x118))
27658f07778SDavid Daney 		cvmx_read64(CVMX_MIO_BOOT_BIST_STAT);
27758f07778SDavid Daney }
27858f07778SDavid Daney 
279a2127e40SAleksey Makarov static inline void cvmx_writeq_csr(void __iomem *csr_addr, uint64_t val)
280a2127e40SAleksey Makarov {
281a2127e40SAleksey Makarov 	cvmx_write_csr((__force uint64_t)csr_addr, val);
282a2127e40SAleksey Makarov }
283a2127e40SAleksey Makarov 
28458f07778SDavid Daney static inline void cvmx_write_io(uint64_t io_addr, uint64_t val)
28558f07778SDavid Daney {
28658f07778SDavid Daney 	cvmx_write64(io_addr, val);
28758f07778SDavid Daney 
28858f07778SDavid Daney }
28958f07778SDavid Daney 
29058f07778SDavid Daney static inline uint64_t cvmx_read_csr(uint64_t csr_addr)
29158f07778SDavid Daney {
29258f07778SDavid Daney 	uint64_t val = cvmx_read64(csr_addr);
29358f07778SDavid Daney 	return val;
29458f07778SDavid Daney }
29558f07778SDavid Daney 
296a2127e40SAleksey Makarov static inline uint64_t cvmx_readq_csr(void __iomem *csr_addr)
297a2127e40SAleksey Makarov {
298a2127e40SAleksey Makarov 	return cvmx_read_csr((__force uint64_t) csr_addr);
299a2127e40SAleksey Makarov }
30058f07778SDavid Daney 
30158f07778SDavid Daney static inline void cvmx_send_single(uint64_t data)
30258f07778SDavid Daney {
30358f07778SDavid Daney 	const uint64_t CVMX_IOBDMA_SENDSINGLE = 0xffffffffffffa200ull;
30458f07778SDavid Daney 	cvmx_write64(CVMX_IOBDMA_SENDSINGLE, data);
30558f07778SDavid Daney }
30658f07778SDavid Daney 
30758f07778SDavid Daney static inline void cvmx_read_csr_async(uint64_t scraddr, uint64_t csr_addr)
30858f07778SDavid Daney {
30958f07778SDavid Daney 	union {
31058f07778SDavid Daney 		uint64_t u64;
31158f07778SDavid Daney 		struct {
31258f07778SDavid Daney 			uint64_t scraddr:8;
31358f07778SDavid Daney 			uint64_t len:8;
31458f07778SDavid Daney 			uint64_t addr:48;
31558f07778SDavid Daney 		} s;
31658f07778SDavid Daney 	} addr;
31758f07778SDavid Daney 	addr.u64 = csr_addr;
31858f07778SDavid Daney 	addr.s.scraddr = scraddr >> 3;
31958f07778SDavid Daney 	addr.s.len = 1;
32058f07778SDavid Daney 	cvmx_send_single(addr.u64);
32158f07778SDavid Daney }
32258f07778SDavid Daney 
32358f07778SDavid Daney /* Return true if Octeon is CN38XX pass 1 */
32458f07778SDavid Daney static inline int cvmx_octeon_is_pass1(void)
32558f07778SDavid Daney {
32658f07778SDavid Daney #if OCTEON_IS_COMMON_BINARY()
32758f07778SDavid Daney 	return 0;	/* Pass 1 isn't supported for common binaries */
32858f07778SDavid Daney #else
32958f07778SDavid Daney /* Now that we know we're built for a specific model, only check CN38XX */
33058f07778SDavid Daney #if OCTEON_IS_MODEL(OCTEON_CN38XX)
33158f07778SDavid Daney 	return cvmx_get_proc_id() == OCTEON_CN38XX_PASS1;
33258f07778SDavid Daney #else
33358f07778SDavid Daney 	return 0;	/* Built for non CN38XX chip, we're not CN38XX pass1 */
33458f07778SDavid Daney #endif
33558f07778SDavid Daney #endif
33658f07778SDavid Daney }
33758f07778SDavid Daney 
33858f07778SDavid Daney static inline unsigned int cvmx_get_core_num(void)
33958f07778SDavid Daney {
34058f07778SDavid Daney 	unsigned int core_num;
34158f07778SDavid Daney 	CVMX_RDHWRNV(core_num, 0);
34258f07778SDavid Daney 	return core_num;
34358f07778SDavid Daney }
34458f07778SDavid Daney 
345182a6d1cSDavid Daney /* Maximum # of bits to define core in node */
346182a6d1cSDavid Daney #define CVMX_NODE_NO_SHIFT	7
347182a6d1cSDavid Daney #define CVMX_NODE_MASK		0x3
348182a6d1cSDavid Daney static inline unsigned int cvmx_get_node_num(void)
349182a6d1cSDavid Daney {
350182a6d1cSDavid Daney 	unsigned int core_num = cvmx_get_core_num();
351182a6d1cSDavid Daney 
352182a6d1cSDavid Daney 	return (core_num >> CVMX_NODE_NO_SHIFT) & CVMX_NODE_MASK;
353182a6d1cSDavid Daney }
354182a6d1cSDavid Daney 
355182a6d1cSDavid Daney static inline unsigned int cvmx_get_local_core_num(void)
356182a6d1cSDavid Daney {
357182a6d1cSDavid Daney 	return cvmx_get_core_num() & ((1 << CVMX_NODE_NO_SHIFT) - 1);
358182a6d1cSDavid Daney }
359182a6d1cSDavid Daney 
36058f07778SDavid Daney /**
36158f07778SDavid Daney  * Returns the number of bits set in the provided value.
36258f07778SDavid Daney  * Simple wrapper for POP instruction.
36358f07778SDavid Daney  *
36458f07778SDavid Daney  * @val:    32 bit value to count set bits in
36558f07778SDavid Daney  *
36658f07778SDavid Daney  * Returns Number of bits set
36758f07778SDavid Daney  */
36858f07778SDavid Daney static inline uint32_t cvmx_pop(uint32_t val)
36958f07778SDavid Daney {
37058f07778SDavid Daney 	uint32_t pop;
37158f07778SDavid Daney 	CVMX_POP(pop, val);
37258f07778SDavid Daney 	return pop;
37358f07778SDavid Daney }
37458f07778SDavid Daney 
37558f07778SDavid Daney /**
37658f07778SDavid Daney  * Returns the number of bits set in the provided value.
37758f07778SDavid Daney  * Simple wrapper for DPOP instruction.
37858f07778SDavid Daney  *
37958f07778SDavid Daney  * @val:    64 bit value to count set bits in
38058f07778SDavid Daney  *
38158f07778SDavid Daney  * Returns Number of bits set
38258f07778SDavid Daney  */
38358f07778SDavid Daney static inline int cvmx_dpop(uint64_t val)
38458f07778SDavid Daney {
38558f07778SDavid Daney 	int pop;
38658f07778SDavid Daney 	CVMX_DPOP(pop, val);
38758f07778SDavid Daney 	return pop;
38858f07778SDavid Daney }
38958f07778SDavid Daney 
39058f07778SDavid Daney /**
39158f07778SDavid Daney  * Provide current cycle counter as a return value
39258f07778SDavid Daney  *
39358f07778SDavid Daney  * Returns current cycle counter
39458f07778SDavid Daney  */
39558f07778SDavid Daney 
39658f07778SDavid Daney static inline uint64_t cvmx_get_cycle(void)
39758f07778SDavid Daney {
39858f07778SDavid Daney 	uint64_t cycle;
39958f07778SDavid Daney 	CVMX_RDHWR(cycle, 31);
40058f07778SDavid Daney 	return cycle;
40158f07778SDavid Daney }
40258f07778SDavid Daney 
40358f07778SDavid Daney /**
404e8635b48SDavid Daney  * Wait for the specified number of cycle
405e8635b48SDavid Daney  *
406e8635b48SDavid Daney  */
407e8635b48SDavid Daney static inline void cvmx_wait(uint64_t cycles)
408e8635b48SDavid Daney {
409e8635b48SDavid Daney 	uint64_t done = cvmx_get_cycle() + cycles;
410e8635b48SDavid Daney 
411e8635b48SDavid Daney 	while (cvmx_get_cycle() < done)
412e8635b48SDavid Daney 		; /* Spin */
413e8635b48SDavid Daney }
414e8635b48SDavid Daney 
415e8635b48SDavid Daney /**
41658f07778SDavid Daney  * Reads a chip global cycle counter.  This counts CPU cycles since
41758f07778SDavid Daney  * chip reset.	The counter is 64 bit.
41858f07778SDavid Daney  * This register does not exist on CN38XX pass 1 silicion
41958f07778SDavid Daney  *
42058f07778SDavid Daney  * Returns Global chip cycle count since chip reset.
42158f07778SDavid Daney  */
42258f07778SDavid Daney static inline uint64_t cvmx_get_cycle_global(void)
42358f07778SDavid Daney {
42458f07778SDavid Daney 	if (cvmx_octeon_is_pass1())
42558f07778SDavid Daney 		return 0;
42658f07778SDavid Daney 	else
42758f07778SDavid Daney 		return cvmx_read64(CVMX_IPD_CLK_COUNT);
42858f07778SDavid Daney }
42958f07778SDavid Daney 
43058f07778SDavid Daney /**
43158f07778SDavid Daney  * This macro spins on a field waiting for it to reach a value. It
43258f07778SDavid Daney  * is common in code to need to wait for a specific field in a CSR
43358f07778SDavid Daney  * to match a specific value. Conceptually this macro expands to:
43458f07778SDavid Daney  *
43558f07778SDavid Daney  * 1) read csr at "address" with a csr typedef of "type"
43658f07778SDavid Daney  * 2) Check if ("type".s."field" "op" "value")
43758f07778SDavid Daney  * 3) If #2 isn't true loop to #1 unless too much time has passed.
43858f07778SDavid Daney  */
43958f07778SDavid Daney #define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\
44058f07778SDavid Daney     (									\
44158f07778SDavid Daney {									\
44258f07778SDavid Daney 	int result;							\
44358f07778SDavid Daney 	do {								\
44458f07778SDavid Daney 		uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
44558f07778SDavid Daney 			cvmx_sysinfo_get()->cpu_clock_hz / 1000000;	\
44658f07778SDavid Daney 		type c;							\
44758f07778SDavid Daney 		while (1) {						\
44858f07778SDavid Daney 			c.u64 = cvmx_read_csr(address);			\
44958f07778SDavid Daney 			if ((c.s.field) op(value)) {			\
45058f07778SDavid Daney 				result = 0;				\
45158f07778SDavid Daney 				break;					\
45258f07778SDavid Daney 			} else if (cvmx_get_cycle() > done) {		\
45358f07778SDavid Daney 				result = -1;				\
45458f07778SDavid Daney 				break;					\
45558f07778SDavid Daney 			} else						\
45658f07778SDavid Daney 				cvmx_wait(100);				\
45758f07778SDavid Daney 		}							\
45858f07778SDavid Daney 	} while (0);							\
45958f07778SDavid Daney 	result;								\
46058f07778SDavid Daney })
46158f07778SDavid Daney 
46258f07778SDavid Daney /***************************************************************************/
46358f07778SDavid Daney 
46458f07778SDavid Daney /* Return the number of cores available in the chip */
46558f07778SDavid Daney static inline uint32_t cvmx_octeon_num_cores(void)
46658f07778SDavid Daney {
467182a6d1cSDavid Daney 	u64 ciu_fuse_reg;
468182a6d1cSDavid Daney 	u64 ciu_fuse;
469182a6d1cSDavid Daney 
470182a6d1cSDavid Daney 	if (OCTEON_IS_OCTEON3() && !OCTEON_IS_MODEL(OCTEON_CN70XX))
471182a6d1cSDavid Daney 		ciu_fuse_reg = CVMX_CIU3_FUSE;
472182a6d1cSDavid Daney 	else
473182a6d1cSDavid Daney 		ciu_fuse_reg = CVMX_CIU_FUSE;
474182a6d1cSDavid Daney 	ciu_fuse = cvmx_read_csr(ciu_fuse_reg);
475182a6d1cSDavid Daney 	return cvmx_dpop(ciu_fuse);
47658f07778SDavid Daney }
47758f07778SDavid Daney 
47858f07778SDavid Daney #endif /*  __CVMX_H__  */
479