cvmx.h 14 KB

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