cvmx.h 14 KB

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