cvmx-pko.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. * Support library for the hardware Packet Output unit.
  29. */
  30. #include <asm/octeon/octeon.h>
  31. #include "cvmx-config.h"
  32. #include "cvmx-pko.h"
  33. #include "cvmx-helper.h"
  34. /**
  35. * Internal state of packet output
  36. */
  37. /**
  38. * Call before any other calls to initialize the packet
  39. * output system. This does chip global config, and should only be
  40. * done by one core.
  41. */
  42. void cvmx_pko_initialize_global(void)
  43. {
  44. int i;
  45. uint64_t priority = 8;
  46. union cvmx_pko_reg_cmd_buf config;
  47. /*
  48. * Set the size of the PKO command buffers to an odd number of
  49. * 64bit words. This allows the normal two word send to stay
  50. * aligned and never span a command word buffer.
  51. */
  52. config.u64 = 0;
  53. config.s.pool = CVMX_FPA_OUTPUT_BUFFER_POOL;
  54. config.s.size = CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE / 8 - 1;
  55. cvmx_write_csr(CVMX_PKO_REG_CMD_BUF, config.u64);
  56. for (i = 0; i < CVMX_PKO_MAX_OUTPUT_QUEUES; i++)
  57. cvmx_pko_config_port(CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID, i, 1,
  58. &priority);
  59. /*
  60. * If we aren't using all of the queues optimize PKO's
  61. * internal memory.
  62. */
  63. if (OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)
  64. || OCTEON_IS_MODEL(OCTEON_CN56XX)
  65. || OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  66. int num_interfaces = cvmx_helper_get_number_of_interfaces();
  67. int last_port =
  68. cvmx_helper_get_last_ipd_port(num_interfaces - 1);
  69. int max_queues =
  70. cvmx_pko_get_base_queue(last_port) +
  71. cvmx_pko_get_num_queues(last_port);
  72. if (OCTEON_IS_MODEL(OCTEON_CN38XX)) {
  73. if (max_queues <= 32)
  74. cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 2);
  75. else if (max_queues <= 64)
  76. cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 1);
  77. } else {
  78. if (max_queues <= 64)
  79. cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 2);
  80. else if (max_queues <= 128)
  81. cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 1);
  82. }
  83. }
  84. }
  85. /**
  86. * This function does per-core initialization required by the PKO routines.
  87. * This must be called on all cores that will do packet output, and must
  88. * be called after the FPA has been initialized and filled with pages.
  89. *
  90. * Returns 0 on success
  91. * !0 on failure
  92. */
  93. int cvmx_pko_initialize_local(void)
  94. {
  95. /* Nothing to do */
  96. return 0;
  97. }
  98. /**
  99. * Enables the packet output hardware. It must already be
  100. * configured.
  101. */
  102. void cvmx_pko_enable(void)
  103. {
  104. union cvmx_pko_reg_flags flags;
  105. flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);
  106. if (flags.s.ena_pko)
  107. cvmx_dprintf
  108. ("Warning: Enabling PKO when PKO already enabled.\n");
  109. flags.s.ena_dwb = 1;
  110. flags.s.ena_pko = 1;
  111. /*
  112. * always enable big endian for 3-word command. Does nothing
  113. * for 2-word.
  114. */
  115. flags.s.store_be = 1;
  116. cvmx_write_csr(CVMX_PKO_REG_FLAGS, flags.u64);
  117. }
  118. /**
  119. * Disables the packet output. Does not affect any configuration.
  120. */
  121. void cvmx_pko_disable(void)
  122. {
  123. union cvmx_pko_reg_flags pko_reg_flags;
  124. pko_reg_flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);
  125. pko_reg_flags.s.ena_pko = 0;
  126. cvmx_write_csr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);
  127. }
  128. /**
  129. * Reset the packet output.
  130. */
  131. static void __cvmx_pko_reset(void)
  132. {
  133. union cvmx_pko_reg_flags pko_reg_flags;
  134. pko_reg_flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);
  135. pko_reg_flags.s.reset = 1;
  136. cvmx_write_csr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);
  137. }
  138. /**
  139. * Shutdown and free resources required by packet output.
  140. */
  141. void cvmx_pko_shutdown(void)
  142. {
  143. union cvmx_pko_mem_queue_ptrs config;
  144. int queue;
  145. cvmx_pko_disable();
  146. for (queue = 0; queue < CVMX_PKO_MAX_OUTPUT_QUEUES; queue++) {
  147. config.u64 = 0;
  148. config.s.tail = 1;
  149. config.s.index = 0;
  150. config.s.port = CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID;
  151. config.s.queue = queue & 0x7f;
  152. config.s.qos_mask = 0;
  153. config.s.buf_ptr = 0;
  154. if (!OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
  155. union cvmx_pko_reg_queue_ptrs1 config1;
  156. config1.u64 = 0;
  157. config1.s.qid7 = queue >> 7;
  158. cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
  159. }
  160. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
  161. cvmx_cmd_queue_shutdown(CVMX_CMD_QUEUE_PKO(queue));
  162. }
  163. __cvmx_pko_reset();
  164. }
  165. /**
  166. * Configure a output port and the associated queues for use.
  167. *
  168. * @port: Port to configure.
  169. * @base_queue: First queue number to associate with this port.
  170. * @num_queues: Number of queues to associate with this port
  171. * @priority: Array of priority levels for each queue. Values are
  172. * allowed to be 0-8. A value of 8 get 8 times the traffic
  173. * of a value of 1. A value of 0 indicates that no rounds
  174. * will be participated in. These priorities can be changed
  175. * on the fly while the pko is enabled. A priority of 9
  176. * indicates that static priority should be used. If static
  177. * priority is used all queues with static priority must be
  178. * contiguous starting at the base_queue, and lower numbered
  179. * queues have higher priority than higher numbered queues.
  180. * There must be num_queues elements in the array.
  181. */
  182. cvmx_pko_status_t cvmx_pko_config_port(uint64_t port, uint64_t base_queue,
  183. uint64_t num_queues,
  184. const uint64_t priority[])
  185. {
  186. cvmx_pko_status_t result_code;
  187. uint64_t queue;
  188. union cvmx_pko_mem_queue_ptrs config;
  189. union cvmx_pko_reg_queue_ptrs1 config1;
  190. int static_priority_base = -1;
  191. int static_priority_end = -1;
  192. if ((port >= CVMX_PKO_NUM_OUTPUT_PORTS)
  193. && (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID)) {
  194. cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid port %llu\n",
  195. (unsigned long long)port);
  196. return CVMX_PKO_INVALID_PORT;
  197. }
  198. if (base_queue + num_queues > CVMX_PKO_MAX_OUTPUT_QUEUES) {
  199. cvmx_dprintf
  200. ("ERROR: cvmx_pko_config_port: Invalid queue range %llu\n",
  201. (unsigned long long)(base_queue + num_queues));
  202. return CVMX_PKO_INVALID_QUEUE;
  203. }
  204. if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
  205. /*
  206. * Validate the static queue priority setup and set
  207. * static_priority_base and static_priority_end
  208. * accordingly.
  209. */
  210. for (queue = 0; queue < num_queues; queue++) {
  211. /* Find first queue of static priority */
  212. if (static_priority_base == -1
  213. && priority[queue] ==
  214. CVMX_PKO_QUEUE_STATIC_PRIORITY)
  215. static_priority_base = queue;
  216. /* Find last queue of static priority */
  217. if (static_priority_base != -1
  218. && static_priority_end == -1
  219. && priority[queue] != CVMX_PKO_QUEUE_STATIC_PRIORITY
  220. && queue)
  221. static_priority_end = queue - 1;
  222. else if (static_priority_base != -1
  223. && static_priority_end == -1
  224. && queue == num_queues - 1)
  225. /* all queues are static priority */
  226. static_priority_end = queue;
  227. /*
  228. * Check to make sure all static priority
  229. * queues are contiguous. Also catches some
  230. * cases of static priorites not starting at
  231. * queue 0.
  232. */
  233. if (static_priority_end != -1
  234. && (int)queue > static_priority_end
  235. && priority[queue] ==
  236. CVMX_PKO_QUEUE_STATIC_PRIORITY) {
  237. cvmx_dprintf("ERROR: cvmx_pko_config_port: "
  238. "Static priority queues aren't "
  239. "contiguous or don't start at "
  240. "base queue. q: %d, eq: %d\n",
  241. (int)queue, static_priority_end);
  242. return CVMX_PKO_INVALID_PRIORITY;
  243. }
  244. }
  245. if (static_priority_base > 0) {
  246. cvmx_dprintf("ERROR: cvmx_pko_config_port: Static "
  247. "priority queues don't start at base "
  248. "queue. sq: %d\n",
  249. static_priority_base);
  250. return CVMX_PKO_INVALID_PRIORITY;
  251. }
  252. #if 0
  253. cvmx_dprintf("Port %d: Static priority queue base: %d, "
  254. "end: %d\n", port,
  255. static_priority_base, static_priority_end);
  256. #endif
  257. }
  258. /*
  259. * At this point, static_priority_base and static_priority_end
  260. * are either both -1, or are valid start/end queue
  261. * numbers.
  262. */
  263. result_code = CVMX_PKO_SUCCESS;
  264. #ifdef PKO_DEBUG
  265. cvmx_dprintf("num queues: %d (%lld,%lld)\n", num_queues,
  266. CVMX_PKO_QUEUES_PER_PORT_INTERFACE0,
  267. CVMX_PKO_QUEUES_PER_PORT_INTERFACE1);
  268. #endif
  269. for (queue = 0; queue < num_queues; queue++) {
  270. uint64_t *buf_ptr = NULL;
  271. config1.u64 = 0;
  272. config1.s.idx3 = queue >> 3;
  273. config1.s.qid7 = (base_queue + queue) >> 7;
  274. config.u64 = 0;
  275. config.s.tail = queue == (num_queues - 1);
  276. config.s.index = queue;
  277. config.s.port = port;
  278. config.s.queue = base_queue + queue;
  279. if (!cvmx_octeon_is_pass1()) {
  280. config.s.static_p = static_priority_base >= 0;
  281. config.s.static_q = (int)queue <= static_priority_end;
  282. config.s.s_tail = (int)queue == static_priority_end;
  283. }
  284. /*
  285. * Convert the priority into an enable bit field. Try
  286. * to space the bits out evenly so the packet don't
  287. * get grouped up
  288. */
  289. switch ((int)priority[queue]) {
  290. case 0:
  291. config.s.qos_mask = 0x00;
  292. break;
  293. case 1:
  294. config.s.qos_mask = 0x01;
  295. break;
  296. case 2:
  297. config.s.qos_mask = 0x11;
  298. break;
  299. case 3:
  300. config.s.qos_mask = 0x49;
  301. break;
  302. case 4:
  303. config.s.qos_mask = 0x55;
  304. break;
  305. case 5:
  306. config.s.qos_mask = 0x57;
  307. break;
  308. case 6:
  309. config.s.qos_mask = 0x77;
  310. break;
  311. case 7:
  312. config.s.qos_mask = 0x7f;
  313. break;
  314. case 8:
  315. config.s.qos_mask = 0xff;
  316. break;
  317. case CVMX_PKO_QUEUE_STATIC_PRIORITY:
  318. /* Pass 1 will fall through to the error case */
  319. if (!cvmx_octeon_is_pass1()) {
  320. config.s.qos_mask = 0xff;
  321. break;
  322. }
  323. default:
  324. cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid "
  325. "priority %llu\n",
  326. (unsigned long long)priority[queue]);
  327. config.s.qos_mask = 0xff;
  328. result_code = CVMX_PKO_INVALID_PRIORITY;
  329. break;
  330. }
  331. if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
  332. cvmx_cmd_queue_result_t cmd_res =
  333. cvmx_cmd_queue_initialize(CVMX_CMD_QUEUE_PKO
  334. (base_queue + queue),
  335. CVMX_PKO_MAX_QUEUE_DEPTH,
  336. CVMX_FPA_OUTPUT_BUFFER_POOL,
  337. CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE
  338. -
  339. CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST
  340. * 8);
  341. if (cmd_res != CVMX_CMD_QUEUE_SUCCESS) {
  342. switch (cmd_res) {
  343. case CVMX_CMD_QUEUE_NO_MEMORY:
  344. cvmx_dprintf("ERROR: "
  345. "cvmx_pko_config_port: "
  346. "Unable to allocate "
  347. "output buffer.\n");
  348. return CVMX_PKO_NO_MEMORY;
  349. case CVMX_CMD_QUEUE_ALREADY_SETUP:
  350. cvmx_dprintf
  351. ("ERROR: cvmx_pko_config_port: Port already setup.\n");
  352. return CVMX_PKO_PORT_ALREADY_SETUP;
  353. case CVMX_CMD_QUEUE_INVALID_PARAM:
  354. default:
  355. cvmx_dprintf
  356. ("ERROR: cvmx_pko_config_port: Command queue initialization failed.\n");
  357. return CVMX_PKO_CMD_QUEUE_INIT_ERROR;
  358. }
  359. }
  360. buf_ptr =
  361. (uint64_t *)
  362. cvmx_cmd_queue_buffer(CVMX_CMD_QUEUE_PKO
  363. (base_queue + queue));
  364. config.s.buf_ptr = cvmx_ptr_to_phys(buf_ptr);
  365. } else
  366. config.s.buf_ptr = 0;
  367. CVMX_SYNCWS;
  368. if (!OCTEON_IS_MODEL(OCTEON_CN3XXX))
  369. cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
  370. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
  371. }
  372. return result_code;
  373. }
  374. #ifdef PKO_DEBUG
  375. /**
  376. * Show map of ports -> queues for different cores.
  377. */
  378. void cvmx_pko_show_queue_map()
  379. {
  380. int core, port;
  381. int pko_output_ports = 36;
  382. cvmx_dprintf("port");
  383. for (port = 0; port < pko_output_ports; port++)
  384. cvmx_dprintf("%3d ", port);
  385. cvmx_dprintf("\n");
  386. for (core = 0; core < CVMX_MAX_CORES; core++) {
  387. cvmx_dprintf("\n%2d: ", core);
  388. for (port = 0; port < pko_output_ports; port++) {
  389. cvmx_dprintf("%3d ",
  390. cvmx_pko_get_base_queue_per_core(port,
  391. core));
  392. }
  393. }
  394. cvmx_dprintf("\n");
  395. }
  396. #endif
  397. /**
  398. * Rate limit a PKO port to a max packets/sec. This function is only
  399. * supported on CN51XX and higher, excluding CN58XX.
  400. *
  401. * @port: Port to rate limit
  402. * @packets_s: Maximum packet/sec
  403. * @burst: Maximum number of packets to burst in a row before rate
  404. * limiting cuts in.
  405. *
  406. * Returns Zero on success, negative on failure
  407. */
  408. int cvmx_pko_rate_limit_packets(int port, int packets_s, int burst)
  409. {
  410. union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
  411. union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
  412. pko_mem_port_rate0.u64 = 0;
  413. pko_mem_port_rate0.s.pid = port;
  414. pko_mem_port_rate0.s.rate_pkt =
  415. cvmx_sysinfo_get()->cpu_clock_hz / packets_s / 16;
  416. /* No cost per word since we are limited by packets/sec, not bits/sec */
  417. pko_mem_port_rate0.s.rate_word = 0;
  418. pko_mem_port_rate1.u64 = 0;
  419. pko_mem_port_rate1.s.pid = port;
  420. pko_mem_port_rate1.s.rate_lim =
  421. ((uint64_t) pko_mem_port_rate0.s.rate_pkt * burst) >> 8;
  422. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
  423. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
  424. return 0;
  425. }
  426. /**
  427. * Rate limit a PKO port to a max bits/sec. This function is only
  428. * supported on CN51XX and higher, excluding CN58XX.
  429. *
  430. * @port: Port to rate limit
  431. * @bits_s: PKO rate limit in bits/sec
  432. * @burst: Maximum number of bits to burst before rate
  433. * limiting cuts in.
  434. *
  435. * Returns Zero on success, negative on failure
  436. */
  437. int cvmx_pko_rate_limit_bits(int port, uint64_t bits_s, int burst)
  438. {
  439. union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
  440. union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
  441. uint64_t clock_rate = cvmx_sysinfo_get()->cpu_clock_hz;
  442. uint64_t tokens_per_bit = clock_rate * 16 / bits_s;
  443. pko_mem_port_rate0.u64 = 0;
  444. pko_mem_port_rate0.s.pid = port;
  445. /*
  446. * Each packet has a 12 bytes of interframe gap, an 8 byte
  447. * preamble, and a 4 byte CRC. These are not included in the
  448. * per word count. Multiply by 8 to covert to bits and divide
  449. * by 256 for limit granularity.
  450. */
  451. pko_mem_port_rate0.s.rate_pkt = (12 + 8 + 4) * 8 * tokens_per_bit / 256;
  452. /* Each 8 byte word has 64bits */
  453. pko_mem_port_rate0.s.rate_word = 64 * tokens_per_bit;
  454. pko_mem_port_rate1.u64 = 0;
  455. pko_mem_port_rate1.s.pid = port;
  456. pko_mem_port_rate1.s.rate_lim = tokens_per_bit * burst / 256;
  457. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
  458. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
  459. return 0;
  460. }