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 <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. /**
  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 comamnd 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. EXPORT_SYMBOL_GPL(cvmx_pko_disable);
  129. /**
  130. * Reset the packet output.
  131. */
  132. static void __cvmx_pko_reset(void)
  133. {
  134. union cvmx_pko_reg_flags pko_reg_flags;
  135. pko_reg_flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);
  136. pko_reg_flags.s.reset = 1;
  137. cvmx_write_csr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);
  138. }
  139. /**
  140. * Shutdown and free resources required by packet output.
  141. */
  142. void cvmx_pko_shutdown(void)
  143. {
  144. union cvmx_pko_mem_queue_ptrs config;
  145. int queue;
  146. cvmx_pko_disable();
  147. for (queue = 0; queue < CVMX_PKO_MAX_OUTPUT_QUEUES; queue++) {
  148. config.u64 = 0;
  149. config.s.tail = 1;
  150. config.s.index = 0;
  151. config.s.port = CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID;
  152. config.s.queue = queue & 0x7f;
  153. config.s.qos_mask = 0;
  154. config.s.buf_ptr = 0;
  155. if (!OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
  156. union cvmx_pko_reg_queue_ptrs1 config1;
  157. config1.u64 = 0;
  158. config1.s.qid7 = queue >> 7;
  159. cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
  160. }
  161. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
  162. cvmx_cmd_queue_shutdown(CVMX_CMD_QUEUE_PKO(queue));
  163. }
  164. __cvmx_pko_reset();
  165. }
  166. EXPORT_SYMBOL_GPL(cvmx_pko_shutdown);
  167. /**
  168. * Configure a output port and the associated queues for use.
  169. *
  170. * @port: Port to configure.
  171. * @base_queue: First queue number to associate with this port.
  172. * @num_queues: Number of queues to associate with this port
  173. * @priority: Array of priority levels for each queue. Values are
  174. * allowed to be 0-8. A value of 8 get 8 times the traffic
  175. * of a value of 1. A value of 0 indicates that no rounds
  176. * will be participated in. These priorities can be changed
  177. * on the fly while the pko is enabled. A priority of 9
  178. * indicates that static priority should be used. If static
  179. * priority is used all queues with static priority must be
  180. * contiguous starting at the base_queue, and lower numbered
  181. * queues have higher priority than higher numbered queues.
  182. * There must be num_queues elements in the array.
  183. */
  184. cvmx_pko_status_t cvmx_pko_config_port(uint64_t port, uint64_t base_queue,
  185. uint64_t num_queues,
  186. const uint64_t priority[])
  187. {
  188. cvmx_pko_status_t result_code;
  189. uint64_t queue;
  190. union cvmx_pko_mem_queue_ptrs config;
  191. union cvmx_pko_reg_queue_ptrs1 config1;
  192. int static_priority_base = -1;
  193. int static_priority_end = -1;
  194. if ((port >= CVMX_PKO_NUM_OUTPUT_PORTS)
  195. && (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID)) {
  196. cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid port %llu\n",
  197. (unsigned long long)port);
  198. return CVMX_PKO_INVALID_PORT;
  199. }
  200. if (base_queue + num_queues > CVMX_PKO_MAX_OUTPUT_QUEUES) {
  201. cvmx_dprintf
  202. ("ERROR: cvmx_pko_config_port: Invalid queue range %llu\n",
  203. (unsigned long long)(base_queue + num_queues));
  204. return CVMX_PKO_INVALID_QUEUE;
  205. }
  206. if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
  207. /*
  208. * Validate the static queue priority setup and set
  209. * static_priority_base and static_priority_end
  210. * accordingly.
  211. */
  212. for (queue = 0; queue < num_queues; queue++) {
  213. /* Find first queue of static priority */
  214. if (static_priority_base == -1
  215. && priority[queue] ==
  216. CVMX_PKO_QUEUE_STATIC_PRIORITY)
  217. static_priority_base = queue;
  218. /* Find last queue of static priority */
  219. if (static_priority_base != -1
  220. && static_priority_end == -1
  221. && priority[queue] != CVMX_PKO_QUEUE_STATIC_PRIORITY
  222. && queue)
  223. static_priority_end = queue - 1;
  224. else if (static_priority_base != -1
  225. && static_priority_end == -1
  226. && queue == num_queues - 1)
  227. /* all queues are static priority */
  228. static_priority_end = queue;
  229. /*
  230. * Check to make sure all static priority
  231. * queues are contiguous. Also catches some
  232. * cases of static priorites not starting at
  233. * queue 0.
  234. */
  235. if (static_priority_end != -1
  236. && (int)queue > static_priority_end
  237. && priority[queue] ==
  238. CVMX_PKO_QUEUE_STATIC_PRIORITY) {
  239. cvmx_dprintf("ERROR: cvmx_pko_config_port: "
  240. "Static priority queues aren't "
  241. "contiguous or don't start at "
  242. "base queue. q: %d, eq: %d\n",
  243. (int)queue, static_priority_end);
  244. return CVMX_PKO_INVALID_PRIORITY;
  245. }
  246. }
  247. if (static_priority_base > 0) {
  248. cvmx_dprintf("ERROR: cvmx_pko_config_port: Static "
  249. "priority queues don't start at base "
  250. "queue. sq: %d\n",
  251. static_priority_base);
  252. return CVMX_PKO_INVALID_PRIORITY;
  253. }
  254. #if 0
  255. cvmx_dprintf("Port %d: Static priority queue base: %d, "
  256. "end: %d\n", port,
  257. static_priority_base, static_priority_end);
  258. #endif
  259. }
  260. /*
  261. * At this point, static_priority_base and static_priority_end
  262. * are either both -1, or are valid start/end queue
  263. * numbers.
  264. */
  265. result_code = CVMX_PKO_SUCCESS;
  266. #ifdef PKO_DEBUG
  267. cvmx_dprintf("num queues: %d (%lld,%lld)\n", num_queues,
  268. CVMX_PKO_QUEUES_PER_PORT_INTERFACE0,
  269. CVMX_PKO_QUEUES_PER_PORT_INTERFACE1);
  270. #endif
  271. for (queue = 0; queue < num_queues; queue++) {
  272. uint64_t *buf_ptr = NULL;
  273. config1.u64 = 0;
  274. config1.s.idx3 = queue >> 3;
  275. config1.s.qid7 = (base_queue + queue) >> 7;
  276. config.u64 = 0;
  277. config.s.tail = queue == (num_queues - 1);
  278. config.s.index = queue;
  279. config.s.port = port;
  280. config.s.queue = base_queue + queue;
  281. if (!cvmx_octeon_is_pass1()) {
  282. config.s.static_p = static_priority_base >= 0;
  283. config.s.static_q = (int)queue <= static_priority_end;
  284. config.s.s_tail = (int)queue == static_priority_end;
  285. }
  286. /*
  287. * Convert the priority into an enable bit field. Try
  288. * to space the bits out evenly so the packet don't
  289. * get grouped up
  290. */
  291. switch ((int)priority[queue]) {
  292. case 0:
  293. config.s.qos_mask = 0x00;
  294. break;
  295. case 1:
  296. config.s.qos_mask = 0x01;
  297. break;
  298. case 2:
  299. config.s.qos_mask = 0x11;
  300. break;
  301. case 3:
  302. config.s.qos_mask = 0x49;
  303. break;
  304. case 4:
  305. config.s.qos_mask = 0x55;
  306. break;
  307. case 5:
  308. config.s.qos_mask = 0x57;
  309. break;
  310. case 6:
  311. config.s.qos_mask = 0x77;
  312. break;
  313. case 7:
  314. config.s.qos_mask = 0x7f;
  315. break;
  316. case 8:
  317. config.s.qos_mask = 0xff;
  318. break;
  319. case CVMX_PKO_QUEUE_STATIC_PRIORITY:
  320. /* Pass 1 will fall through to the error case */
  321. if (!cvmx_octeon_is_pass1()) {
  322. config.s.qos_mask = 0xff;
  323. break;
  324. }
  325. default:
  326. cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid "
  327. "priority %llu\n",
  328. (unsigned long long)priority[queue]);
  329. config.s.qos_mask = 0xff;
  330. result_code = CVMX_PKO_INVALID_PRIORITY;
  331. break;
  332. }
  333. if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {
  334. cvmx_cmd_queue_result_t cmd_res =
  335. cvmx_cmd_queue_initialize(CVMX_CMD_QUEUE_PKO
  336. (base_queue + queue),
  337. CVMX_PKO_MAX_QUEUE_DEPTH,
  338. CVMX_FPA_OUTPUT_BUFFER_POOL,
  339. CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE
  340. -
  341. CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST
  342. * 8);
  343. if (cmd_res != CVMX_CMD_QUEUE_SUCCESS) {
  344. switch (cmd_res) {
  345. case CVMX_CMD_QUEUE_NO_MEMORY:
  346. cvmx_dprintf("ERROR: "
  347. "cvmx_pko_config_port: "
  348. "Unable to allocate "
  349. "output buffer.\n");
  350. return CVMX_PKO_NO_MEMORY;
  351. case CVMX_CMD_QUEUE_ALREADY_SETUP:
  352. cvmx_dprintf
  353. ("ERROR: cvmx_pko_config_port: Port already setup.\n");
  354. return CVMX_PKO_PORT_ALREADY_SETUP;
  355. case CVMX_CMD_QUEUE_INVALID_PARAM:
  356. default:
  357. cvmx_dprintf
  358. ("ERROR: cvmx_pko_config_port: Command queue initialization failed.\n");
  359. return CVMX_PKO_CMD_QUEUE_INIT_ERROR;
  360. }
  361. }
  362. buf_ptr =
  363. (uint64_t *)
  364. cvmx_cmd_queue_buffer(CVMX_CMD_QUEUE_PKO
  365. (base_queue + queue));
  366. config.s.buf_ptr = cvmx_ptr_to_phys(buf_ptr);
  367. } else
  368. config.s.buf_ptr = 0;
  369. CVMX_SYNCWS;
  370. if (!OCTEON_IS_MODEL(OCTEON_CN3XXX))
  371. cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);
  372. cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);
  373. }
  374. return result_code;
  375. }
  376. #ifdef PKO_DEBUG
  377. /**
  378. * Show map of ports -> queues for different cores.
  379. */
  380. void cvmx_pko_show_queue_map()
  381. {
  382. int core, port;
  383. int pko_output_ports = 36;
  384. cvmx_dprintf("port");
  385. for (port = 0; port < pko_output_ports; port++)
  386. cvmx_dprintf("%3d ", port);
  387. cvmx_dprintf("\n");
  388. for (core = 0; core < CVMX_MAX_CORES; core++) {
  389. cvmx_dprintf("\n%2d: ", core);
  390. for (port = 0; port < pko_output_ports; port++) {
  391. cvmx_dprintf("%3d ",
  392. cvmx_pko_get_base_queue_per_core(port,
  393. core));
  394. }
  395. }
  396. cvmx_dprintf("\n");
  397. }
  398. #endif
  399. /**
  400. * Rate limit a PKO port to a max packets/sec. This function is only
  401. * supported on CN51XX and higher, excluding CN58XX.
  402. *
  403. * @port: Port to rate limit
  404. * @packets_s: Maximum packet/sec
  405. * @burst: Maximum number of packets to burst in a row before rate
  406. * limiting cuts in.
  407. *
  408. * Returns Zero on success, negative on failure
  409. */
  410. int cvmx_pko_rate_limit_packets(int port, int packets_s, int burst)
  411. {
  412. union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
  413. union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
  414. pko_mem_port_rate0.u64 = 0;
  415. pko_mem_port_rate0.s.pid = port;
  416. pko_mem_port_rate0.s.rate_pkt =
  417. cvmx_sysinfo_get()->cpu_clock_hz / packets_s / 16;
  418. /* No cost per word since we are limited by packets/sec, not bits/sec */
  419. pko_mem_port_rate0.s.rate_word = 0;
  420. pko_mem_port_rate1.u64 = 0;
  421. pko_mem_port_rate1.s.pid = port;
  422. pko_mem_port_rate1.s.rate_lim =
  423. ((uint64_t) pko_mem_port_rate0.s.rate_pkt * burst) >> 8;
  424. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
  425. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
  426. return 0;
  427. }
  428. /**
  429. * Rate limit a PKO port to a max bits/sec. This function is only
  430. * supported on CN51XX and higher, excluding CN58XX.
  431. *
  432. * @port: Port to rate limit
  433. * @bits_s: PKO rate limit in bits/sec
  434. * @burst: Maximum number of bits to burst before rate
  435. * limiting cuts in.
  436. *
  437. * Returns Zero on success, negative on failure
  438. */
  439. int cvmx_pko_rate_limit_bits(int port, uint64_t bits_s, int burst)
  440. {
  441. union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;
  442. union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;
  443. uint64_t clock_rate = cvmx_sysinfo_get()->cpu_clock_hz;
  444. uint64_t tokens_per_bit = clock_rate * 16 / bits_s;
  445. pko_mem_port_rate0.u64 = 0;
  446. pko_mem_port_rate0.s.pid = port;
  447. /*
  448. * Each packet has a 12 bytes of interframe gap, an 8 byte
  449. * preamble, and a 4 byte CRC. These are not included in the
  450. * per word count. Multiply by 8 to covert to bits and divide
  451. * by 256 for limit granularity.
  452. */
  453. pko_mem_port_rate0.s.rate_pkt = (12 + 8 + 4) * 8 * tokens_per_bit / 256;
  454. /* Each 8 byte word has 64bits */
  455. pko_mem_port_rate0.s.rate_word = 64 * tokens_per_bit;
  456. pko_mem_port_rate1.u64 = 0;
  457. pko_mem_port_rate1.s.pid = port;
  458. pko_mem_port_rate1.s.rate_lim = tokens_per_bit * burst / 256;
  459. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);
  460. cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);
  461. return 0;
  462. }