cvmx-pko.c 17 KB

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