cvmx.h 14 KB

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