Linux Audio

Check our new training course

Loading...
v3.5.6
  1/***********************license start***************
  2 * Author: Cavium Networks
  3 *
  4 * Contact: support@caviumnetworks.com
  5 * This file is part of the OCTEON SDK
  6 *
  7 * Copyright (c) 2003-2008 Cavium Networks
  8 *
  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.
 12 *
 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
 17 * details.
 18 *
 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/.
 23 *
 24 * This file may also be available under a different license from Cavium.
 25 * Contact Cavium Networks for more information
 26 ***********************license end**************************************/
 27
 28#ifndef __CVMX_H__
 29#define __CVMX_H__
 30
 31#include <linux/kernel.h>
 32#include <linux/string.h>
 33
 34enum cvmx_mips_space {
 35	CVMX_MIPS_SPACE_XKSEG = 3LL,
 36	CVMX_MIPS_SPACE_XKPHYS = 2LL,
 37	CVMX_MIPS_SPACE_XSSEG = 1LL,
 38	CVMX_MIPS_SPACE_XUSEG = 0LL
 39};
 40
 41/* These macros for use when using 32 bit pointers. */
 42#define CVMX_MIPS32_SPACE_KSEG0 1l
 43#define CVMX_ADD_SEG32(segment, add) \
 44	(((int32_t)segment << 31) | (int32_t)(add))
 45
 46#define CVMX_IO_SEG CVMX_MIPS_SPACE_XKPHYS
 47
 48/* These macros simplify the process of creating common IO addresses */
 49#define CVMX_ADD_SEG(segment, add) \
 50	((((uint64_t)segment) << 62) | (add))
 51#ifndef CVMX_ADD_IO_SEG
 52#define CVMX_ADD_IO_SEG(add) CVMX_ADD_SEG(CVMX_IO_SEG, (add))
 53#endif
 54
 55#include "cvmx-asm.h"
 56#include "cvmx-packet.h"
 57#include "cvmx-sysinfo.h"
 58
 59#include "cvmx-ciu-defs.h"
 60#include "cvmx-gpio-defs.h"
 61#include "cvmx-iob-defs.h"
 62#include "cvmx-ipd-defs.h"
 63#include "cvmx-l2c-defs.h"
 64#include "cvmx-l2d-defs.h"
 65#include "cvmx-l2t-defs.h"
 66#include "cvmx-led-defs.h"
 67#include "cvmx-mio-defs.h"
 68#include "cvmx-pow-defs.h"
 69
 70#include "cvmx-bootinfo.h"
 71#include "cvmx-bootmem.h"
 72#include "cvmx-l2c.h"
 
 73
 74#ifndef CVMX_ENABLE_DEBUG_PRINTS
 75#define CVMX_ENABLE_DEBUG_PRINTS 1
 76#endif
 77
 78#if CVMX_ENABLE_DEBUG_PRINTS
 79#define cvmx_dprintf        printk
 80#else
 81#define cvmx_dprintf(...)   {}
 82#endif
 83
 84#define CVMX_MAX_CORES          (16)
 85#define CVMX_CACHE_LINE_SIZE    (128)	/* In bytes */
 86#define CVMX_CACHE_LINE_MASK    (CVMX_CACHE_LINE_SIZE - 1)	/* In bytes */
 87#define CVMX_CACHE_LINE_ALIGNED __attribute__ ((aligned(CVMX_CACHE_LINE_SIZE)))
 88#define CAST64(v) ((long long)(long)(v))
 89#define CASTPTR(type, v) ((type *)(long)(v))
 90
 91/*
 92 * Returns processor ID, different Linux and simple exec versions
 93 * provided in the cvmx-app-init*.c files.
 94 */
 95static inline uint32_t cvmx_get_proc_id(void) __attribute__ ((pure));
 96static inline uint32_t cvmx_get_proc_id(void)
 97{
 98	uint32_t id;
 99	asm("mfc0 %0, $15,0" : "=r"(id));
100	return id;
101}
102
103/* turn the variable name into a string */
104#define CVMX_TMP_STR(x) CVMX_TMP_STR2(x)
105#define CVMX_TMP_STR2(x) #x
106
107/**
108 * Builds a bit mask given the required size in bits.
109 *
110 * @bits:   Number of bits in the mask
111 * Returns The mask
112 */ static inline uint64_t cvmx_build_mask(uint64_t bits)
113{
114	return ~((~0x0ull) << bits);
115}
116
117/**
118 * Builds a memory address for I/O based on the Major and Sub DID.
119 *
120 * @major_did: 5 bit major did
121 * @sub_did:   3 bit sub did
122 * Returns I/O base address
123 */
124static inline uint64_t cvmx_build_io_address(uint64_t major_did,
125					     uint64_t sub_did)
126{
127	return (0x1ull << 48) | (major_did << 43) | (sub_did << 40);
128}
129
130/**
131 * Perform mask and shift to place the supplied value into
132 * the supplied bit rage.
133 *
134 * Example: cvmx_build_bits(39,24,value)
135 * <pre>
136 * 6       5       4       3       3       2       1
137 * 3       5       7       9       1       3       5       7      0
138 * +-------+-------+-------+-------+-------+-------+-------+------+
139 * 000000000000000000000000___________value000000000000000000000000
140 * </pre>
141 *
142 * @high_bit: Highest bit value can occupy (inclusive) 0-63
143 * @low_bit:  Lowest bit value can occupy inclusive 0-high_bit
144 * @value:    Value to use
145 * Returns Value masked and shifted
146 */
147static inline uint64_t cvmx_build_bits(uint64_t high_bit,
148				       uint64_t low_bit, uint64_t value)
149{
150	return (value & cvmx_build_mask(high_bit - low_bit + 1)) << low_bit;
151}
152
153/**
154 * Convert a memory pointer (void*) into a hardware compatible
155 * memory address (uint64_t). Octeon hardware widgets don't
156 * understand logical addresses.
157 *
158 * @ptr:    C style memory pointer
159 * Returns Hardware physical address
160 */
161static inline uint64_t cvmx_ptr_to_phys(void *ptr)
162{
163	if (sizeof(void *) == 8) {
164		/*
165		 * We're running in 64 bit mode. Normally this means
166		 * that we can use 40 bits of address space (the
167		 * hardware limit). Unfortunately there is one case
168		 * were we need to limit this to 30 bits, sign
169		 * extended 32 bit. Although these are 64 bits wide,
170		 * only 30 bits can be used.
171		 */
172		if ((CAST64(ptr) >> 62) == 3)
173			return CAST64(ptr) & cvmx_build_mask(30);
174		else
175			return CAST64(ptr) & cvmx_build_mask(40);
176	} else {
177		return (long)(ptr) & 0x1fffffff;
178	}
179}
180
181/**
182 * Convert a hardware physical address (uint64_t) into a
183 * memory pointer (void *).
184 *
185 * @physical_address:
186 *               Hardware physical address to memory
187 * Returns Pointer to memory
188 */
189static inline void *cvmx_phys_to_ptr(uint64_t physical_address)
190{
191	if (sizeof(void *) == 8) {
192		/* Just set the top bit, avoiding any TLB uglyness */
193		return CASTPTR(void,
194			       CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
195					    physical_address));
196	} else {
197		return CASTPTR(void,
198			       CVMX_ADD_SEG32(CVMX_MIPS32_SPACE_KSEG0,
199					      physical_address));
200	}
201}
202
203/* The following #if controls the definition of the macro
204    CVMX_BUILD_WRITE64. This macro is used to build a store operation to
205    a full 64bit address. With a 64bit ABI, this can be done with a simple
206    pointer access. 32bit ABIs require more complicated assembly */
207
208/* We have a full 64bit ABI. Writing to a 64bit address can be done with
209    a simple volatile pointer */
210#define CVMX_BUILD_WRITE64(TYPE, ST)                                    \
211static inline void cvmx_write64_##TYPE(uint64_t addr, TYPE##_t val)     \
212{                                                                       \
213    *CASTPTR(volatile TYPE##_t, addr) = val;                            \
214}
215
216
217/* The following #if controls the definition of the macro
218    CVMX_BUILD_READ64. This macro is used to build a load operation from
219    a full 64bit address. With a 64bit ABI, this can be done with a simple
220    pointer access. 32bit ABIs require more complicated assembly */
221
222/* We have a full 64bit ABI. Writing to a 64bit address can be done with
223    a simple volatile pointer */
224#define CVMX_BUILD_READ64(TYPE, LT)                                     \
225static inline TYPE##_t cvmx_read64_##TYPE(uint64_t addr)                \
226{                                                                       \
227	return *CASTPTR(volatile TYPE##_t, addr);			\
228}
229
230
231/* The following defines 8 functions for writing to a 64bit address. Each
232    takes two arguments, the address and the value to write.
233    cvmx_write64_int64      cvmx_write64_uint64
234    cvmx_write64_int32      cvmx_write64_uint32
235    cvmx_write64_int16      cvmx_write64_uint16
236    cvmx_write64_int8       cvmx_write64_uint8 */
237CVMX_BUILD_WRITE64(int64, "sd");
238CVMX_BUILD_WRITE64(int32, "sw");
239CVMX_BUILD_WRITE64(int16, "sh");
240CVMX_BUILD_WRITE64(int8, "sb");
241CVMX_BUILD_WRITE64(uint64, "sd");
242CVMX_BUILD_WRITE64(uint32, "sw");
243CVMX_BUILD_WRITE64(uint16, "sh");
244CVMX_BUILD_WRITE64(uint8, "sb");
245#define cvmx_write64 cvmx_write64_uint64
246
247/* The following defines 8 functions for reading from a 64bit address. Each
248    takes the address as the only argument
249    cvmx_read64_int64       cvmx_read64_uint64
250    cvmx_read64_int32       cvmx_read64_uint32
251    cvmx_read64_int16       cvmx_read64_uint16
252    cvmx_read64_int8        cvmx_read64_uint8 */
253CVMX_BUILD_READ64(int64, "ld");
254CVMX_BUILD_READ64(int32, "lw");
255CVMX_BUILD_READ64(int16, "lh");
256CVMX_BUILD_READ64(int8, "lb");
257CVMX_BUILD_READ64(uint64, "ld");
258CVMX_BUILD_READ64(uint32, "lw");
259CVMX_BUILD_READ64(uint16, "lhu");
260CVMX_BUILD_READ64(uint8, "lbu");
261#define cvmx_read64 cvmx_read64_uint64
262
263
264static inline void cvmx_write_csr(uint64_t csr_addr, uint64_t val)
265{
266	cvmx_write64(csr_addr, val);
267
268	/*
269	 * Perform an immediate read after every write to an RSL
270	 * register to force the write to complete. It doesn't matter
271	 * what RSL read we do, so we choose CVMX_MIO_BOOT_BIST_STAT
272	 * because it is fast and harmless.
273	 */
274	if (((csr_addr >> 40) & 0x7ffff) == (0x118))
275		cvmx_read64(CVMX_MIO_BOOT_BIST_STAT);
276}
277
 
 
 
 
 
278static inline void cvmx_write_io(uint64_t io_addr, uint64_t val)
279{
280	cvmx_write64(io_addr, val);
281
282}
283
284static inline uint64_t cvmx_read_csr(uint64_t csr_addr)
285{
286	uint64_t val = cvmx_read64(csr_addr);
287	return val;
288}
289
 
 
 
 
290
291static inline void cvmx_send_single(uint64_t data)
292{
293	const uint64_t CVMX_IOBDMA_SENDSINGLE = 0xffffffffffffa200ull;
294	cvmx_write64(CVMX_IOBDMA_SENDSINGLE, data);
295}
296
297static inline void cvmx_read_csr_async(uint64_t scraddr, uint64_t csr_addr)
298{
299	union {
300		uint64_t u64;
301		struct {
302			uint64_t scraddr:8;
303			uint64_t len:8;
304			uint64_t addr:48;
305		} s;
306	} addr;
307	addr.u64 = csr_addr;
308	addr.s.scraddr = scraddr >> 3;
309	addr.s.len = 1;
310	cvmx_send_single(addr.u64);
311}
312
313/* Return true if Octeon is CN38XX pass 1 */
314static inline int cvmx_octeon_is_pass1(void)
315{
316#if OCTEON_IS_COMMON_BINARY()
317	return 0;	/* Pass 1 isn't supported for common binaries */
318#else
319/* Now that we know we're built for a specific model, only check CN38XX */
320#if OCTEON_IS_MODEL(OCTEON_CN38XX)
321	return cvmx_get_proc_id() == OCTEON_CN38XX_PASS1;
322#else
323	return 0;	/* Built for non CN38XX chip, we're not CN38XX pass1 */
324#endif
325#endif
326}
327
328static inline unsigned int cvmx_get_core_num(void)
329{
330	unsigned int core_num;
331	CVMX_RDHWRNV(core_num, 0);
332	return core_num;
333}
334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335/**
336 * Returns the number of bits set in the provided value.
337 * Simple wrapper for POP instruction.
338 *
339 * @val:    32 bit value to count set bits in
340 *
341 * Returns Number of bits set
342 */
343static inline uint32_t cvmx_pop(uint32_t val)
344{
345	uint32_t pop;
346	CVMX_POP(pop, val);
347	return pop;
348}
349
350/**
351 * Returns the number of bits set in the provided value.
352 * Simple wrapper for DPOP instruction.
353 *
354 * @val:    64 bit value to count set bits in
355 *
356 * Returns Number of bits set
357 */
358static inline int cvmx_dpop(uint64_t val)
359{
360	int pop;
361	CVMX_DPOP(pop, val);
362	return pop;
363}
364
365/**
366 * Provide current cycle counter as a return value
367 *
368 * Returns current cycle counter
369 */
370
371static inline uint64_t cvmx_get_cycle(void)
372{
373	uint64_t cycle;
374	CVMX_RDHWR(cycle, 31);
375	return cycle;
376}
377
378/**
379 * Wait for the specified number of cycle
380 *
381 */
382static inline void cvmx_wait(uint64_t cycles)
383{
384	uint64_t done = cvmx_get_cycle() + cycles;
385
386	while (cvmx_get_cycle() < done)
387		; /* Spin */
388}
389
390/**
391 * Reads a chip global cycle counter.  This counts CPU cycles since
392 * chip reset.  The counter is 64 bit.
393 * This register does not exist on CN38XX pass 1 silicion
394 *
395 * Returns Global chip cycle count since chip reset.
396 */
397static inline uint64_t cvmx_get_cycle_global(void)
398{
399	if (cvmx_octeon_is_pass1())
400		return 0;
401	else
402		return cvmx_read64(CVMX_IPD_CLK_COUNT);
403}
404
405/**
406 * This macro spins on a field waiting for it to reach a value. It
407 * is common in code to need to wait for a specific field in a CSR
408 * to match a specific value. Conceptually this macro expands to:
409 *
410 * 1) read csr at "address" with a csr typedef of "type"
411 * 2) Check if ("type".s."field" "op" "value")
412 * 3) If #2 isn't true loop to #1 unless too much time has passed.
413 */
414#define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\
415    (									\
416{									\
417	int result;							\
418	do {								\
419		uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
420			cvmx_sysinfo_get()->cpu_clock_hz / 1000000;	\
421		type c;							\
422		while (1) {						\
423			c.u64 = cvmx_read_csr(address);			\
424			if ((c.s.field) op(value)) {			\
425				result = 0;				\
426				break;					\
427			} else if (cvmx_get_cycle() > done) {		\
428				result = -1;				\
429				break;					\
430			} else						\
431				cvmx_wait(100);				\
432		}							\
433	} while (0);							\
434	result;								\
435})
436
437/***************************************************************************/
438
439static inline void cvmx_reset_octeon(void)
440{
441	union cvmx_ciu_soft_rst ciu_soft_rst;
442	ciu_soft_rst.u64 = 0;
443	ciu_soft_rst.s.soft_rst = 1;
444	cvmx_write_csr(CVMX_CIU_SOFT_RST, ciu_soft_rst.u64);
445}
446
447/* Return the number of cores available in the chip */
448static inline uint32_t cvmx_octeon_num_cores(void)
449{
450	uint32_t ciu_fuse = (uint32_t) cvmx_read_csr(CVMX_CIU_FUSE) & 0xffff;
451	return cvmx_pop(ciu_fuse);
452}
453
454/**
455 * Read a byte of fuse data
456 * @byte_addr:   address to read
457 *
458 * Returns fuse value: 0 or 1
459 */
460static uint8_t cvmx_fuse_read_byte(int byte_addr)
461{
462	union cvmx_mio_fus_rcmd read_cmd;
463
464	read_cmd.u64 = 0;
465	read_cmd.s.addr = byte_addr;
466	read_cmd.s.pend = 1;
467	cvmx_write_csr(CVMX_MIO_FUS_RCMD, read_cmd.u64);
468	while ((read_cmd.u64 = cvmx_read_csr(CVMX_MIO_FUS_RCMD))
469	       && read_cmd.s.pend)
470		;
471	return read_cmd.s.dat;
472}
473
474/**
475 * Read a single fuse bit
476 *
477 * @fuse:   Fuse number (0-1024)
478 *
479 * Returns fuse value: 0 or 1
480 */
481static inline int cvmx_fuse_read(int fuse)
482{
483	return (cvmx_fuse_read_byte(fuse >> 3) >> (fuse & 0x7)) & 1;
484}
485
486static inline int cvmx_octeon_model_CN36XX(void)
487{
488	return OCTEON_IS_MODEL(OCTEON_CN38XX)
489		&& !cvmx_octeon_is_pass1()
490		&& cvmx_fuse_read(264);
491}
492
493static inline int cvmx_octeon_zip_present(void)
494{
495	return octeon_has_feature(OCTEON_FEATURE_ZIP);
496}
497
498static inline int cvmx_octeon_dfa_present(void)
499{
500	if (!OCTEON_IS_MODEL(OCTEON_CN38XX)
501	    && !OCTEON_IS_MODEL(OCTEON_CN31XX)
502	    && !OCTEON_IS_MODEL(OCTEON_CN58XX))
503		return 0;
504	else if (OCTEON_IS_MODEL(OCTEON_CN3020))
505		return 0;
506	else if (cvmx_octeon_is_pass1())
507		return 1;
508	else
509		return !cvmx_fuse_read(120);
510}
511
512static inline int cvmx_octeon_crypto_present(void)
513{
514	return octeon_has_feature(OCTEON_FEATURE_CRYPTO);
515}
516
517#endif /*  __CVMX_H__  */
v4.10.11
  1/***********************license start***************
  2 * Author: Cavium Networks
  3 *
  4 * Contact: support@caviumnetworks.com
  5 * This file is part of the OCTEON SDK
  6 *
  7 * Copyright (c) 2003-2008 Cavium Networks
  8 *
  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.
 12 *
 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
 17 * details.
 18 *
 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/.
 23 *
 24 * This file may also be available under a different license from Cavium.
 25 * Contact Cavium Networks for more information
 26 ***********************license end**************************************/
 27
 28#ifndef __CVMX_H__
 29#define __CVMX_H__
 30
 31#include <linux/kernel.h>
 32#include <linux/string.h>
 33
 34enum cvmx_mips_space {
 35	CVMX_MIPS_SPACE_XKSEG = 3LL,
 36	CVMX_MIPS_SPACE_XKPHYS = 2LL,
 37	CVMX_MIPS_SPACE_XSSEG = 1LL,
 38	CVMX_MIPS_SPACE_XUSEG = 0LL
 39};
 40
 41/* These macros for use when using 32 bit pointers. */
 42#define CVMX_MIPS32_SPACE_KSEG0 1l
 43#define CVMX_ADD_SEG32(segment, add) \
 44	(((int32_t)segment << 31) | (int32_t)(add))
 45
 46#define CVMX_IO_SEG CVMX_MIPS_SPACE_XKPHYS
 47
 48/* These macros simplify the process of creating common IO addresses */
 49#define CVMX_ADD_SEG(segment, add) \
 50	((((uint64_t)segment) << 62) | (add))
 51#ifndef CVMX_ADD_IO_SEG
 52#define CVMX_ADD_IO_SEG(add) CVMX_ADD_SEG(CVMX_IO_SEG, (add))
 53#endif
 54
 55#include <asm/octeon/cvmx-asm.h>
 56#include <asm/octeon/cvmx-packet.h>
 57#include <asm/octeon/cvmx-sysinfo.h>
 58
 59#include <asm/octeon/cvmx-ciu-defs.h>
 60#include <asm/octeon/cvmx-ciu3-defs.h>
 61#include <asm/octeon/cvmx-gpio-defs.h>
 62#include <asm/octeon/cvmx-iob-defs.h>
 63#include <asm/octeon/cvmx-ipd-defs.h>
 64#include <asm/octeon/cvmx-l2c-defs.h>
 65#include <asm/octeon/cvmx-l2d-defs.h>
 66#include <asm/octeon/cvmx-l2t-defs.h>
 67#include <asm/octeon/cvmx-led-defs.h>
 68#include <asm/octeon/cvmx-mio-defs.h>
 69#include <asm/octeon/cvmx-pow-defs.h>
 70
 71#include <asm/octeon/cvmx-bootinfo.h>
 72#include <asm/octeon/cvmx-bootmem.h>
 73#include <asm/octeon/cvmx-l2c.h>
 74
 75#ifndef CVMX_ENABLE_DEBUG_PRINTS
 76#define CVMX_ENABLE_DEBUG_PRINTS 1
 77#endif
 78
 79#if CVMX_ENABLE_DEBUG_PRINTS
 80#define cvmx_dprintf	    printk
 81#else
 82#define cvmx_dprintf(...)   {}
 83#endif
 84
 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)))
 89#define CAST64(v) ((long long)(long)(v))
 90#define CASTPTR(type, v) ((type *)(long)(v))
 91
 92/*
 93 * Returns processor ID, different Linux and simple exec versions
 94 * provided in the cvmx-app-init*.c files.
 95 */
 96static inline uint32_t cvmx_get_proc_id(void) __attribute__ ((pure));
 97static inline uint32_t cvmx_get_proc_id(void)
 98{
 99	uint32_t id;
100	asm("mfc0 %0, $15,0" : "=r"(id));
101	return id;
102}
103
104/* turn the variable name into a string */
105#define CVMX_TMP_STR(x) CVMX_TMP_STR2(x)
106#define CVMX_TMP_STR2(x) #x
107
108/**
109 * Builds a bit mask given the required size in bits.
110 *
111 * @bits:   Number of bits in the mask
112 * Returns The mask
113 */ static inline uint64_t cvmx_build_mask(uint64_t bits)
114{
115	return ~((~0x0ull) << bits);
116}
117
118/**
119 * Builds a memory address for I/O based on the Major and Sub DID.
120 *
121 * @major_did: 5 bit major did
122 * @sub_did:   3 bit sub did
123 * Returns I/O base address
124 */
125static inline uint64_t cvmx_build_io_address(uint64_t major_did,
126					     uint64_t sub_did)
127{
128	return (0x1ull << 48) | (major_did << 43) | (sub_did << 40);
129}
130
131/**
132 * Perform mask and shift to place the supplied value into
133 * the supplied bit rage.
134 *
135 * Example: cvmx_build_bits(39,24,value)
136 * <pre>
137 * 6	   5	   4	   3	   3	   2	   1
138 * 3	   5	   7	   9	   1	   3	   5	   7	  0
139 * +-------+-------+-------+-------+-------+-------+-------+------+
140 * 000000000000000000000000___________value000000000000000000000000
141 * </pre>
142 *
143 * @high_bit: Highest bit value can occupy (inclusive) 0-63
144 * @low_bit:  Lowest bit value can occupy inclusive 0-high_bit
145 * @value:    Value to use
146 * Returns Value masked and shifted
147 */
148static inline uint64_t cvmx_build_bits(uint64_t high_bit,
149				       uint64_t low_bit, uint64_t value)
150{
151	return (value & cvmx_build_mask(high_bit - low_bit + 1)) << low_bit;
152}
153
154/**
155 * Convert a memory pointer (void*) into a hardware compatible
156 * memory address (uint64_t). Octeon hardware widgets don't
157 * understand logical addresses.
158 *
159 * @ptr:    C style memory pointer
160 * Returns Hardware physical address
161 */
162static inline uint64_t cvmx_ptr_to_phys(void *ptr)
163{
164	if (sizeof(void *) == 8) {
165		/*
166		 * We're running in 64 bit mode. Normally this means
167		 * that we can use 40 bits of address space (the
168		 * hardware limit). Unfortunately there is one case
169		 * were we need to limit this to 30 bits, sign
170		 * extended 32 bit. Although these are 64 bits wide,
171		 * only 30 bits can be used.
172		 */
173		if ((CAST64(ptr) >> 62) == 3)
174			return CAST64(ptr) & cvmx_build_mask(30);
175		else
176			return CAST64(ptr) & cvmx_build_mask(40);
177	} else {
178		return (long)(ptr) & 0x1fffffff;
179	}
180}
181
182/**
183 * Convert a hardware physical address (uint64_t) into a
184 * memory pointer (void *).
185 *
186 * @physical_address:
187 *		 Hardware physical address to memory
188 * Returns Pointer to memory
189 */
190static inline void *cvmx_phys_to_ptr(uint64_t physical_address)
191{
192	if (sizeof(void *) == 8) {
193		/* Just set the top bit, avoiding any TLB ugliness */
194		return CASTPTR(void,
195			       CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
196					    physical_address));
197	} else {
198		return CASTPTR(void,
199			       CVMX_ADD_SEG32(CVMX_MIPS32_SPACE_KSEG0,
200					      physical_address));
201	}
202}
203
204/* The following #if controls the definition of the macro
205    CVMX_BUILD_WRITE64. This macro is used to build a store operation to
206    a full 64bit address. With a 64bit ABI, this can be done with a simple
207    pointer access. 32bit ABIs require more complicated assembly */
208
209/* We have a full 64bit ABI. Writing to a 64bit address can be done with
210    a simple volatile pointer */
211#define CVMX_BUILD_WRITE64(TYPE, ST)					\
212static inline void cvmx_write64_##TYPE(uint64_t addr, TYPE##_t val)	\
213{									\
214    *CASTPTR(volatile TYPE##_t, addr) = val;				\
215}
216
217
218/* The following #if controls the definition of the macro
219    CVMX_BUILD_READ64. This macro is used to build a load operation from
220    a full 64bit address. With a 64bit ABI, this can be done with a simple
221    pointer access. 32bit ABIs require more complicated assembly */
222
223/* We have a full 64bit ABI. Writing to a 64bit address can be done with
224    a simple volatile pointer */
225#define CVMX_BUILD_READ64(TYPE, LT)					\
226static inline TYPE##_t cvmx_read64_##TYPE(uint64_t addr)		\
227{									\
228	return *CASTPTR(volatile TYPE##_t, addr);			\
229}
230
231
232/* The following defines 8 functions for writing to a 64bit address. Each
233    takes two arguments, the address and the value to write.
234    cvmx_write64_int64	    cvmx_write64_uint64
235    cvmx_write64_int32	    cvmx_write64_uint32
236    cvmx_write64_int16	    cvmx_write64_uint16
237    cvmx_write64_int8	    cvmx_write64_uint8 */
238CVMX_BUILD_WRITE64(int64, "sd");
239CVMX_BUILD_WRITE64(int32, "sw");
240CVMX_BUILD_WRITE64(int16, "sh");
241CVMX_BUILD_WRITE64(int8, "sb");
242CVMX_BUILD_WRITE64(uint64, "sd");
243CVMX_BUILD_WRITE64(uint32, "sw");
244CVMX_BUILD_WRITE64(uint16, "sh");
245CVMX_BUILD_WRITE64(uint8, "sb");
246#define cvmx_write64 cvmx_write64_uint64
247
248/* The following defines 8 functions for reading from a 64bit address. Each
249    takes the address as the only argument
250    cvmx_read64_int64	    cvmx_read64_uint64
251    cvmx_read64_int32	    cvmx_read64_uint32
252    cvmx_read64_int16	    cvmx_read64_uint16
253    cvmx_read64_int8	    cvmx_read64_uint8 */
254CVMX_BUILD_READ64(int64, "ld");
255CVMX_BUILD_READ64(int32, "lw");
256CVMX_BUILD_READ64(int16, "lh");
257CVMX_BUILD_READ64(int8, "lb");
258CVMX_BUILD_READ64(uint64, "ld");
259CVMX_BUILD_READ64(uint32, "lw");
260CVMX_BUILD_READ64(uint16, "lhu");
261CVMX_BUILD_READ64(uint8, "lbu");
262#define cvmx_read64 cvmx_read64_uint64
263
264
265static inline void cvmx_write_csr(uint64_t csr_addr, uint64_t val)
266{
267	cvmx_write64(csr_addr, val);
268
269	/*
270	 * Perform an immediate read after every write to an RSL
271	 * register to force the write to complete. It doesn't matter
272	 * what RSL read we do, so we choose CVMX_MIO_BOOT_BIST_STAT
273	 * because it is fast and harmless.
274	 */
275	if (((csr_addr >> 40) & 0x7ffff) == (0x118))
276		cvmx_read64(CVMX_MIO_BOOT_BIST_STAT);
277}
278
279static inline void cvmx_writeq_csr(void __iomem *csr_addr, uint64_t val)
280{
281	cvmx_write_csr((__force uint64_t)csr_addr, val);
282}
283
284static inline void cvmx_write_io(uint64_t io_addr, uint64_t val)
285{
286	cvmx_write64(io_addr, val);
287
288}
289
290static inline uint64_t cvmx_read_csr(uint64_t csr_addr)
291{
292	uint64_t val = cvmx_read64(csr_addr);
293	return val;
294}
295
296static inline uint64_t cvmx_readq_csr(void __iomem *csr_addr)
297{
298	return cvmx_read_csr((__force uint64_t) csr_addr);
299}
300
301static inline void cvmx_send_single(uint64_t data)
302{
303	const uint64_t CVMX_IOBDMA_SENDSINGLE = 0xffffffffffffa200ull;
304	cvmx_write64(CVMX_IOBDMA_SENDSINGLE, data);
305}
306
307static inline void cvmx_read_csr_async(uint64_t scraddr, uint64_t csr_addr)
308{
309	union {
310		uint64_t u64;
311		struct {
312			uint64_t scraddr:8;
313			uint64_t len:8;
314			uint64_t addr:48;
315		} s;
316	} addr;
317	addr.u64 = csr_addr;
318	addr.s.scraddr = scraddr >> 3;
319	addr.s.len = 1;
320	cvmx_send_single(addr.u64);
321}
322
323/* Return true if Octeon is CN38XX pass 1 */
324static inline int cvmx_octeon_is_pass1(void)
325{
326#if OCTEON_IS_COMMON_BINARY()
327	return 0;	/* Pass 1 isn't supported for common binaries */
328#else
329/* Now that we know we're built for a specific model, only check CN38XX */
330#if OCTEON_IS_MODEL(OCTEON_CN38XX)
331	return cvmx_get_proc_id() == OCTEON_CN38XX_PASS1;
332#else
333	return 0;	/* Built for non CN38XX chip, we're not CN38XX pass1 */
334#endif
335#endif
336}
337
338static inline unsigned int cvmx_get_core_num(void)
339{
340	unsigned int core_num;
341	CVMX_RDHWRNV(core_num, 0);
342	return core_num;
343}
344
345/* Maximum # of bits to define core in node */
346#define CVMX_NODE_NO_SHIFT	7
347#define CVMX_NODE_MASK		0x3
348static inline unsigned int cvmx_get_node_num(void)
349{
350	unsigned int core_num = cvmx_get_core_num();
351
352	return (core_num >> CVMX_NODE_NO_SHIFT) & CVMX_NODE_MASK;
353}
354
355static inline unsigned int cvmx_get_local_core_num(void)
356{
357	return cvmx_get_core_num() & ((1 << CVMX_NODE_NO_SHIFT) - 1);
358}
359
360/**
361 * Returns the number of bits set in the provided value.
362 * Simple wrapper for POP instruction.
363 *
364 * @val:    32 bit value to count set bits in
365 *
366 * Returns Number of bits set
367 */
368static inline uint32_t cvmx_pop(uint32_t val)
369{
370	uint32_t pop;
371	CVMX_POP(pop, val);
372	return pop;
373}
374
375/**
376 * Returns the number of bits set in the provided value.
377 * Simple wrapper for DPOP instruction.
378 *
379 * @val:    64 bit value to count set bits in
380 *
381 * Returns Number of bits set
382 */
383static inline int cvmx_dpop(uint64_t val)
384{
385	int pop;
386	CVMX_DPOP(pop, val);
387	return pop;
388}
389
390/**
391 * Provide current cycle counter as a return value
392 *
393 * Returns current cycle counter
394 */
395
396static inline uint64_t cvmx_get_cycle(void)
397{
398	uint64_t cycle;
399	CVMX_RDHWR(cycle, 31);
400	return cycle;
401}
402
403/**
404 * Wait for the specified number of cycle
405 *
406 */
407static inline void cvmx_wait(uint64_t cycles)
408{
409	uint64_t done = cvmx_get_cycle() + cycles;
410
411	while (cvmx_get_cycle() < done)
412		; /* Spin */
413}
414
415/**
416 * Reads a chip global cycle counter.  This counts CPU cycles since
417 * chip reset.	The counter is 64 bit.
418 * This register does not exist on CN38XX pass 1 silicion
419 *
420 * Returns Global chip cycle count since chip reset.
421 */
422static inline uint64_t cvmx_get_cycle_global(void)
423{
424	if (cvmx_octeon_is_pass1())
425		return 0;
426	else
427		return cvmx_read64(CVMX_IPD_CLK_COUNT);
428}
429
430/**
431 * This macro spins on a field waiting for it to reach a value. It
432 * is common in code to need to wait for a specific field in a CSR
433 * to match a specific value. Conceptually this macro expands to:
434 *
435 * 1) read csr at "address" with a csr typedef of "type"
436 * 2) Check if ("type".s."field" "op" "value")
437 * 3) If #2 isn't true loop to #1 unless too much time has passed.
438 */
439#define CVMX_WAIT_FOR_FIELD64(address, type, field, op, value, timeout_usec)\
440    (									\
441{									\
442	int result;							\
443	do {								\
444		uint64_t done = cvmx_get_cycle() + (uint64_t)timeout_usec * \
445			cvmx_sysinfo_get()->cpu_clock_hz / 1000000;	\
446		type c;							\
447		while (1) {						\
448			c.u64 = cvmx_read_csr(address);			\
449			if ((c.s.field) op(value)) {			\
450				result = 0;				\
451				break;					\
452			} else if (cvmx_get_cycle() > done) {		\
453				result = -1;				\
454				break;					\
455			} else						\
456				cvmx_wait(100);				\
457		}							\
458	} while (0);							\
459	result;								\
460})
461
462/***************************************************************************/
463
 
 
 
 
 
 
 
 
464/* Return the number of cores available in the chip */
465static inline uint32_t cvmx_octeon_num_cores(void)
466{
467	u64 ciu_fuse_reg;
468	u64 ciu_fuse;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
469
470	if (OCTEON_IS_OCTEON3() && !OCTEON_IS_MODEL(OCTEON_CN70XX))
471		ciu_fuse_reg = CVMX_CIU3_FUSE;
 
 
 
 
 
 
 
 
472	else
473		ciu_fuse_reg = CVMX_CIU_FUSE;
474	ciu_fuse = cvmx_read_csr(ciu_fuse_reg);
475	return cvmx_dpop(ciu_fuse);
 
 
 
476}
477
478#endif /*  __CVMX_H__  */