cvmx-pko.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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 <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. * This function does per-core initialization required by the PKO routines.
  206. * This must be called on all cores that will do packet output, and must
  207. * be called after the FPA has been initialized and filled with pages.
  208. *
  209. * Returns 0 on success
  210. * !0 on failure
  211. */
  212. int cvmx_pko_initialize_local(void)
  213. {
  214. /* Nothing to do */
  215. return 0;
  216. }
  217. /**
  218. * Enables the packet output hardware. It must already be
  219. * configured.
  220. */
  221. void cvmx_pko_enable(void)
  222. {
  223. union cvmx_pko_reg_flags flags;
  224. flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);
  225. if (flags.s.ena_pko)
  226. cvmx_dprintf
  227. ("Warning: Enabling PKO when PKO already enabled.\n");
  228. flags.s.ena_dwb = 1;
  229. flags.s.ena_pko = 1;
  230. /*
  231. * always enable big endian for 3-word command. Does nothing
  232. * for 2-word.
  233. */
  234. flags.s.store_be = 1;
  235. cvmx_write_csr(CVMX_PKO_REG_FLAGS, flags.u64);
  236. }
  237. /**
  238. * Disables the packet output. Does not affect any configuration.
  239. */
  240. void cvmx_pko_disable(void)
  241. {
  242. union cvmx_pko_reg_flags pko_reg_flags;
  243. pko_reg_flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);
  244. pko_reg_flags.s.ena_pko = 0;
  245. cvmx_write_csr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);
  246. }
  247. EXPORT_SYMBOL_GPL(cvmx_pko_disable);
  248. /**
  249. * Reset the packet output.
  250. */
  251. static void __cvmx_pko_reset(void)
  252. {
  253. union cvmx_pko_reg_flags pko_reg_flags;
  254. pko_reg_flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);
  255. pko_reg_flags.s.reset = 1;
  256. cvmx_write_csr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);
  257. }
  258. /**
  259. * Shutdown and free resources required by packet output.
  260. */
  261. void cvmx_pko_shutdown(void)
  262. {
  263. union cvmx_pko_mem_queue_ptrs config;
  264. int queue;
  265. cvmx_pko_disable();
  266. for (queue = 0; queue < CVMX_PKO_MAX_OUTPUT_QUEUES; queue++) {
  267. config.u64 = 0;
  268. config.s.tail = 1;
  269. config.s.index = 0;
  270. config.s.port = CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID;
  271. config.s.queue = queue & 0x7f;
  272. config.s.qos_mask = 0;
  273. config.s.buf_ptr = 0;
  274. if (!OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
  275. union cvmx_pko_reg_queue_ptrs1 config1;
  276. config1.u64 = 0;
  277. config1.s.qid7 = queue >> 7;
  278. cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
  279. }
  280. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
  281. cvmx_cmd_queue_shutdown(CVMX_CMD_QUEUE_PKO(queue));
  282. }
  283. __cvmx_pko_reset();
  284. }
  285. EXPORT_SYMBOL_GPL(cvmx_pko_shutdown);
  286. /**
  287. * Configure a output port and the associated queues for use.
  288. *
  289. * @port: Port to configure.
  290. * @base_queue: First queue number to associate with this port.
  291. * @num_queues: Number of queues to associate with this port
  292. * @priority: Array of priority levels for each queue. Values are
  293. * allowed to be 0-8. A value of 8 get 8 times the traffic
  294. * of a value of 1. A value of 0 indicates that no rounds
  295. * will be participated in. These priorities can be changed
  296. * on the fly while the pko is enabled. A priority of 9
  297. * indicates that static priority should be used. If static
  298. * priority is used all queues with static priority must be
  299. * contiguous starting at the base_queue, and lower numbered
  300. * queues have higher priority than higher numbered queues.
  301. * There must be num_queues elements in the array.
  302. */
  303. cvmx_pko_status_t cvmx_pko_config_port(uint64_t port, uint64_t base_queue,
  304. uint64_t num_queues,
  305. const uint64_t priority[])
  306. {
  307. cvmx_pko_status_t result_code;
  308. uint64_t queue;
  309. union cvmx_pko_mem_queue_ptrs config;
  310. union cvmx_pko_reg_queue_ptrs1 config1;
  311. int static_priority_base = -1;
  312. int static_priority_end = -1;
  313. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  314. return CVMX_PKO_SUCCESS;
  315. if ((port >= CVMX_PKO_NUM_OUTPUT_PORTS)
  316. && (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID)) {
  317. cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid port %llu\n",
  318. (unsigned long long)port);
  319. return CVMX_PKO_INVALID_PORT;
  320. }
  321. if (base_queue + num_queues > CVMX_PKO_MAX_OUTPUT_QUEUES) {
  322. cvmx_dprintf
  323. ("ERROR: cvmx_pko_config_port: Invalid queue range %llu\n",
  324. (unsigned long long)(base_queue + num_queues));
  325. return CVMX_PKO_INVALID_QUEUE;
  326. }
  327. if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
  328. /*
  329. * Validate the static queue priority setup and set
  330. * static_priority_base and static_priority_end
  331. * accordingly.
  332. */
  333. for (queue = 0; queue < num_queues; queue++) {
  334. /* Find first queue of static priority */
  335. if (static_priority_base == -1
  336. && priority[queue] ==
  337. CVMX_PKO_QUEUE_STATIC_PRIORITY)
  338. static_priority_base = queue;
  339. /* Find last queue of static priority */
  340. if (static_priority_base != -1
  341. && static_priority_end == -1
  342. && priority[queue] != CVMX_PKO_QUEUE_STATIC_PRIORITY
  343. && queue)
  344. static_priority_end = queue - 1;
  345. else if (static_priority_base != -1
  346. && static_priority_end == -1
  347. && queue == num_queues - 1)
  348. /* all queues are static priority */
  349. static_priority_end = queue;
  350. /*
  351. * Check to make sure all static priority
  352. * queues are contiguous. Also catches some
  353. * cases of static priorites not starting at
  354. * queue 0.
  355. */
  356. if (static_priority_end != -1
  357. && (int)queue > static_priority_end
  358. && priority[queue] ==
  359. CVMX_PKO_QUEUE_STATIC_PRIORITY) {
  360. cvmx_dprintf("ERROR: cvmx_pko_config_port: "
  361. "Static priority queues aren't "
  362. "contiguous or don't start at "
  363. "base queue. q: %d, eq: %d\n",
  364. (int)queue, static_priority_end);
  365. return CVMX_PKO_INVALID_PRIORITY;
  366. }
  367. }
  368. if (static_priority_base > 0) {
  369. cvmx_dprintf("ERROR: cvmx_pko_config_port: Static "
  370. "priority queues don't start at base "
  371. "queue. sq: %d\n",
  372. static_priority_base);
  373. return CVMX_PKO_INVALID_PRIORITY;
  374. }
  375. #if 0
  376. cvmx_dprintf("Port %d: Static priority queue base: %d, "
  377. "end: %d\n", port,
  378. static_priority_base, static_priority_end);
  379. #endif
  380. }
  381. /*
  382. * At this point, static_priority_base and static_priority_end
  383. * are either both -1, or are valid start/end queue
  384. * numbers.
  385. */
  386. result_code = CVMX_PKO_SUCCESS;
  387. #ifdef PKO_DEBUG
  388. cvmx_dprintf("num queues: %d (%lld,%lld)\n", num_queues,
  389. CVMX_PKO_QUEUES_PER_PORT_INTERFACE0,
  390. CVMX_PKO_QUEUES_PER_PORT_INTERFACE1);
  391. #endif
  392. for (queue = 0; queue < num_queues; queue++) {
  393. uint64_t *buf_ptr = NULL;
  394. config1.u64 = 0;
  395. config1.s.idx3 = queue >> 3;
  396. config1.s.qid7 = (base_queue + queue) >> 7;
  397. config.u64 = 0;
  398. config.s.tail = queue == (num_queues - 1);
  399. config.s.index = queue;
  400. config.s.port = port;
  401. config.s.queue = base_queue + queue;
  402. if (!cvmx_octeon_is_pass1()) {
  403. config.s.static_p = static_priority_base >= 0;
  404. config.s.static_q = (int)queue <= static_priority_end;
  405. config.s.s_tail = (int)queue == static_priority_end;
  406. }
  407. /*
  408. * Convert the priority into an enable bit field. Try
  409. * to space the bits out evenly so the packet don't
  410. * get grouped up
  411. */
  412. switch ((int)priority[queue]) {
  413. case 0:
  414. config.s.qos_mask = 0x00;
  415. break;
  416. case 1:
  417. config.s.qos_mask = 0x01;
  418. break;
  419. case 2:
  420. config.s.qos_mask = 0x11;
  421. break;
  422. case 3:
  423. config.s.qos_mask = 0x49;
  424. break;
  425. case 4:
  426. config.s.qos_mask = 0x55;
  427. break;
  428. case 5:
  429. config.s.qos_mask = 0x57;
  430. break;
  431. case 6:
  432. config.s.qos_mask = 0x77;
  433. break;
  434. case 7:
  435. config.s.qos_mask = 0x7f;
  436. break;
  437. case 8:
  438. config.s.qos_mask = 0xff;
  439. break;
  440. case CVMX_PKO_QUEUE_STATIC_PRIORITY:
  441. /* Pass 1 will fall through to the error case */
  442. if (!cvmx_octeon_is_pass1()) {
  443. config.s.qos_mask = 0xff;
  444. break;
  445. }
  446. default:
  447. cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid "
  448. "priority %llu\n",
  449. (unsigned long long)priority[queue]);
  450. config.s.qos_mask = 0xff;
  451. result_code = CVMX_PKO_INVALID_PRIORITY;
  452. break;
  453. }
  454. if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
  455. cvmx_cmd_queue_result_t cmd_res =
  456. cvmx_cmd_queue_initialize(CVMX_CMD_QUEUE_PKO
  457. (base_queue + queue),
  458. CVMX_PKO_MAX_QUEUE_DEPTH,
  459. CVMX_FPA_OUTPUT_BUFFER_POOL,
  460. CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE
  461. -
  462. CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST
  463. * 8);
  464. if (cmd_res != CVMX_CMD_QUEUE_SUCCESS) {
  465. switch (cmd_res) {
  466. case CVMX_CMD_QUEUE_NO_MEMORY:
  467. cvmx_dprintf("ERROR: "
  468. "cvmx_pko_config_port: "
  469. "Unable to allocate "
  470. "output buffer.\n");
  471. return CVMX_PKO_NO_MEMORY;
  472. case CVMX_CMD_QUEUE_ALREADY_SETUP:
  473. cvmx_dprintf
  474. ("ERROR: cvmx_pko_config_port: Port already setup.\n");
  475. return CVMX_PKO_PORT_ALREADY_SETUP;
  476. case CVMX_CMD_QUEUE_INVALID_PARAM:
  477. default:
  478. cvmx_dprintf
  479. ("ERROR: cvmx_pko_config_port: Command queue initialization failed.\n");
  480. return CVMX_PKO_CMD_QUEUE_INIT_ERROR;
  481. }
  482. }
  483. buf_ptr =
  484. (uint64_t *)
  485. cvmx_cmd_queue_buffer(CVMX_CMD_QUEUE_PKO
  486. (base_queue + queue));
  487. config.s.buf_ptr = cvmx_ptr_to_phys(buf_ptr);
  488. } else
  489. config.s.buf_ptr = 0;
  490. CVMX_SYNCWS;
  491. if (!OCTEON_IS_MODEL(OCTEON_CN3XXX))
  492. cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
  493. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
  494. }
  495. return result_code;
  496. }
  497. #ifdef PKO_DEBUG
  498. /**
  499. * Show map of ports -> queues for different cores.
  500. */
  501. void cvmx_pko_show_queue_map()
  502. {
  503. int core, port;
  504. int pko_output_ports = 36;
  505. cvmx_dprintf("port");
  506. for (port = 0; port < pko_output_ports; port++)
  507. cvmx_dprintf("%3d ", port);
  508. cvmx_dprintf("\n");
  509. for (core = 0; core < CVMX_MAX_CORES; core++) {
  510. cvmx_dprintf("\n%2d: ", core);
  511. for (port = 0; port < pko_output_ports; port++) {
  512. cvmx_dprintf("%3d ",
  513. cvmx_pko_get_base_queue_per_core(port,
  514. core));
  515. }
  516. }
  517. cvmx_dprintf("\n");
  518. }
  519. #endif
  520. /**
  521. * Rate limit a PKO port to a max packets/sec. This function is only
  522. * supported on CN51XX and higher, excluding CN58XX.
  523. *
  524. * @port: Port to rate limit
  525. * @packets_s: Maximum packet/sec
  526. * @burst: Maximum number of packets to burst in a row before rate
  527. * limiting cuts in.
  528. *
  529. * Returns Zero on success, negative on failure
  530. */
  531. int cvmx_pko_rate_limit_packets(int port, int packets_s, int burst)
  532. {
  533. union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
  534. union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
  535. pko_mem_port_rate0.u64 = 0;
  536. pko_mem_port_rate0.s.pid = port;
  537. pko_mem_port_rate0.s.rate_pkt =
  538. cvmx_sysinfo_get()->cpu_clock_hz / packets_s / 16;
  539. /* No cost per word since we are limited by packets/sec, not bits/sec */
  540. pko_mem_port_rate0.s.rate_word = 0;
  541. pko_mem_port_rate1.u64 = 0;
  542. pko_mem_port_rate1.s.pid = port;
  543. pko_mem_port_rate1.s.rate_lim =
  544. ((uint64_t) pko_mem_port_rate0.s.rate_pkt * burst) >> 8;
  545. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
  546. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
  547. return 0;
  548. }
  549. /**
  550. * Rate limit a PKO port to a max bits/sec. This function is only
  551. * supported on CN51XX and higher, excluding CN58XX.
  552. *
  553. * @port: Port to rate limit
  554. * @bits_s: PKO rate limit in bits/sec
  555. * @burst: Maximum number of bits to burst before rate
  556. * limiting cuts in.
  557. *
  558. * Returns Zero on success, negative on failure
  559. */
  560. int cvmx_pko_rate_limit_bits(int port, uint64_t bits_s, int burst)
  561. {
  562. union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
  563. union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
  564. uint64_t clock_rate = cvmx_sysinfo_get()->cpu_clock_hz;
  565. uint64_t tokens_per_bit = clock_rate * 16 / bits_s;
  566. pko_mem_port_rate0.u64 = 0;
  567. pko_mem_port_rate0.s.pid = port;
  568. /*
  569. * Each packet has a 12 bytes of interframe gap, an 8 byte
  570. * preamble, and a 4 byte CRC. These are not included in the
  571. * per word count. Multiply by 8 to covert to bits and divide
  572. * by 256 for limit granularity.
  573. */
  574. pko_mem_port_rate0.s.rate_pkt = (12 + 8 + 4) * 8 * tokens_per_bit / 256;
  575. /* Each 8 byte word has 64bits */
  576. pko_mem_port_rate0.s.rate_word = 64 * tokens_per_bit;
  577. pko_mem_port_rate1.u64 = 0;
  578. pko_mem_port_rate1.s.pid = port;
  579. pko_mem_port_rate1.s.rate_lim = tokens_per_bit * burst / 256;
  580. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
  581. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
  582. return 0;
  583. }