Path: blob/master/arch/mips/cavium-octeon/executive/cvmx-pko.c
26481 views
/***********************license start***************1* Author: Cavium Networks2*3* Contact: [email protected]4* This file is part of the OCTEON SDK5*6* Copyright (c) 2003-2008 Cavium Networks7*8* This file is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License, Version 2, as10* published by the Free Software Foundation.11*12* This file is distributed in the hope that it will be useful, but13* AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty14* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or15* NONINFRINGEMENT. See the GNU General Public License for more16* details.17*18* You should have received a copy of the GNU General Public License19* along with this file; if not, write to the Free Software20* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA21* or visit http://www.gnu.org/licenses/.22*23* This file may also be available under a different license from Cavium.24* Contact Cavium Networks for more information25***********************license end**************************************/2627/*28* Support library for the hardware Packet Output unit.29*/3031#include <asm/octeon/octeon.h>3233#include <asm/octeon/cvmx-config.h>34#include <asm/octeon/cvmx-pko.h>35#include <asm/octeon/cvmx-helper.h>3637/*38* Internal state of packet output39*/4041static int __cvmx_pko_int(int interface, int index)42{43switch (interface) {44case 0:45return index;46case 1:47return 4;48case 2:49return index + 0x08;50case 3:51return index + 0x0c;52case 4:53return index + 0x10;54case 5:55return 0x1c;56case 6:57return 0x1d;58case 7:59return 0x1e;60case 8:61return 0x1f;62default:63return -1;64}65}6667static void __cvmx_pko_iport_config(int pko_port)68{69int queue;70const int num_queues = 1;71const int base_queue = pko_port;72const int static_priority_end = 1;73const int static_priority_base = 1;7475for (queue = 0; queue < num_queues; queue++) {76union cvmx_pko_mem_iqueue_ptrs config;77cvmx_cmd_queue_result_t cmd_res;78uint64_t *buf_ptr;7980config.u64 = 0;81config.s.index = queue;82config.s.qid = base_queue + queue;83config.s.ipid = pko_port;84config.s.tail = (queue == (num_queues - 1));85config.s.s_tail = (queue == static_priority_end);86config.s.static_p = (static_priority_base >= 0);87config.s.static_q = (queue <= static_priority_end);88config.s.qos_mask = 0xff;8990cmd_res = cvmx_cmd_queue_initialize(91CVMX_CMD_QUEUE_PKO(base_queue + queue),92CVMX_PKO_MAX_QUEUE_DEPTH,93CVMX_FPA_OUTPUT_BUFFER_POOL,94(CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE -95CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST * 8));9697WARN(cmd_res,98"%s: cmd_res=%d pko_port=%d base_queue=%d num_queues=%d queue=%d\n",99__func__, (int)cmd_res, pko_port, base_queue,100num_queues, queue);101102buf_ptr = (uint64_t *)cvmx_cmd_queue_buffer(103CVMX_CMD_QUEUE_PKO(base_queue + queue));104config.s.buf_ptr = cvmx_ptr_to_phys(buf_ptr) >> 7;105CVMX_SYNCWS;106cvmx_write_csr(CVMX_PKO_MEM_IQUEUE_PTRS, config.u64);107}108}109110static void __cvmx_pko_queue_alloc_o68(void)111{112int port;113114for (port = 0; port < 48; port++)115__cvmx_pko_iport_config(port);116}117118static void __cvmx_pko_port_map_o68(void)119{120int port;121int interface, index;122cvmx_helper_interface_mode_t mode;123union cvmx_pko_mem_iport_ptrs config;124125/*126* Initialize every iport with the invalid eid.127*/128config.u64 = 0;129config.s.eid = 31; /* Invalid */130for (port = 0; port < 128; port++) {131config.s.ipid = port;132cvmx_write_csr(CVMX_PKO_MEM_IPORT_PTRS, config.u64);133}134135/*136* Set up PKO_MEM_IPORT_PTRS137*/138for (port = 0; port < 48; port++) {139interface = cvmx_helper_get_interface_num(port);140index = cvmx_helper_get_interface_index_num(port);141mode = cvmx_helper_interface_get_mode(interface);142if (mode == CVMX_HELPER_INTERFACE_MODE_DISABLED)143continue;144145config.s.ipid = port;146config.s.qos_mask = 0xff;147config.s.crc = 1;148config.s.min_pkt = 1;149config.s.intr = __cvmx_pko_int(interface, index);150config.s.eid = config.s.intr;151config.s.pipe = (mode == CVMX_HELPER_INTERFACE_MODE_LOOP) ?152index : port;153cvmx_write_csr(CVMX_PKO_MEM_IPORT_PTRS, config.u64);154}155}156157static void __cvmx_pko_chip_init(void)158{159int i;160161if (OCTEON_IS_MODEL(OCTEON_CN68XX)) {162__cvmx_pko_port_map_o68();163__cvmx_pko_queue_alloc_o68();164return;165}166167/*168* Initialize queues169*/170for (i = 0; i < CVMX_PKO_MAX_OUTPUT_QUEUES; i++) {171const uint64_t priority = 8;172173cvmx_pko_config_port(CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID, i, 1,174&priority);175}176}177178/*179* Call before any other calls to initialize the packet180* output system. This does chip global config, and should only be181* done by one core.182*/183184void cvmx_pko_initialize_global(void)185{186union cvmx_pko_reg_cmd_buf config;187188/*189* Set the size of the PKO command buffers to an odd number of190* 64bit words. This allows the normal two word send to stay191* aligned and never span a command word buffer.192*/193config.u64 = 0;194config.s.pool = CVMX_FPA_OUTPUT_BUFFER_POOL;195config.s.size = CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE / 8 - 1;196197cvmx_write_csr(CVMX_PKO_REG_CMD_BUF, config.u64);198199/*200* Chip-specific setup.201*/202__cvmx_pko_chip_init();203204/*205* If we aren't using all of the queues optimize PKO's206* internal memory.207*/208if (OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN58XX)209|| OCTEON_IS_MODEL(OCTEON_CN56XX)210|| OCTEON_IS_MODEL(OCTEON_CN52XX)) {211int num_interfaces = cvmx_helper_get_number_of_interfaces();212int last_port =213cvmx_helper_get_last_ipd_port(num_interfaces - 1);214int max_queues =215cvmx_pko_get_base_queue(last_port) +216cvmx_pko_get_num_queues(last_port);217if (OCTEON_IS_MODEL(OCTEON_CN38XX)) {218if (max_queues <= 32)219cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 2);220else if (max_queues <= 64)221cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 1);222} else {223if (max_queues <= 64)224cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 2);225else if (max_queues <= 128)226cvmx_write_csr(CVMX_PKO_REG_QUEUE_MODE, 1);227}228}229}230231/*232* Enables the packet output hardware. It must already be233* configured.234*/235void cvmx_pko_enable(void)236{237union cvmx_pko_reg_flags flags;238239flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);240if (flags.s.ena_pko)241cvmx_dprintf242("Warning: Enabling PKO when PKO already enabled.\n");243244flags.s.ena_dwb = 1;245flags.s.ena_pko = 1;246/*247* always enable big endian for 3-word command. Does nothing248* for 2-word.249*/250flags.s.store_be = 1;251cvmx_write_csr(CVMX_PKO_REG_FLAGS, flags.u64);252}253254/*255* Disables the packet output. Does not affect any configuration.256*/257void cvmx_pko_disable(void)258{259union cvmx_pko_reg_flags pko_reg_flags;260pko_reg_flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);261pko_reg_flags.s.ena_pko = 0;262cvmx_write_csr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);263}264EXPORT_SYMBOL_GPL(cvmx_pko_disable);265266/*267* Reset the packet output.268*/269static void __cvmx_pko_reset(void)270{271union cvmx_pko_reg_flags pko_reg_flags;272pko_reg_flags.u64 = cvmx_read_csr(CVMX_PKO_REG_FLAGS);273pko_reg_flags.s.reset = 1;274cvmx_write_csr(CVMX_PKO_REG_FLAGS, pko_reg_flags.u64);275}276277/*278* Shutdown and free resources required by packet output.279*/280void cvmx_pko_shutdown(void)281{282union cvmx_pko_mem_queue_ptrs config;283int queue;284285cvmx_pko_disable();286287for (queue = 0; queue < CVMX_PKO_MAX_OUTPUT_QUEUES; queue++) {288config.u64 = 0;289config.s.tail = 1;290config.s.index = 0;291config.s.port = CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID;292config.s.queue = queue & 0x7f;293config.s.qos_mask = 0;294config.s.buf_ptr = 0;295if (!OCTEON_IS_MODEL(OCTEON_CN3XXX)) {296union cvmx_pko_reg_queue_ptrs1 config1;297config1.u64 = 0;298config1.s.qid7 = queue >> 7;299cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);300}301cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);302cvmx_cmd_queue_shutdown(CVMX_CMD_QUEUE_PKO(queue));303}304__cvmx_pko_reset();305}306EXPORT_SYMBOL_GPL(cvmx_pko_shutdown);307308/*309* Configure a output port and the associated queues for use.310*311* @port: Port to configure.312* @base_queue: First queue number to associate with this port.313* @num_queues: Number of queues to associate with this port314* @priority: Array of priority levels for each queue. Values are315* allowed to be 0-8. A value of 8 get 8 times the traffic316* of a value of 1. A value of 0 indicates that no rounds317* will be participated in. These priorities can be changed318* on the fly while the pko is enabled. A priority of 9319* indicates that static priority should be used. If static320* priority is used all queues with static priority must be321* contiguous starting at the base_queue, and lower numbered322* queues have higher priority than higher numbered queues.323* There must be num_queues elements in the array.324*/325cvmx_pko_status_t cvmx_pko_config_port(uint64_t port, uint64_t base_queue,326uint64_t num_queues,327const uint64_t priority[])328{329cvmx_pko_status_t result_code;330uint64_t queue;331union cvmx_pko_mem_queue_ptrs config;332union cvmx_pko_reg_queue_ptrs1 config1;333int static_priority_base = -1;334int static_priority_end = -1;335336if (OCTEON_IS_MODEL(OCTEON_CN68XX))337return CVMX_PKO_SUCCESS;338339if ((port >= CVMX_PKO_NUM_OUTPUT_PORTS)340&& (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID)) {341cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid port %llu\n",342(unsigned long long)port);343return CVMX_PKO_INVALID_PORT;344}345346if (base_queue + num_queues > CVMX_PKO_MAX_OUTPUT_QUEUES) {347cvmx_dprintf348("ERROR: cvmx_pko_config_port: Invalid queue range %llu\n",349(unsigned long long)(base_queue + num_queues));350return CVMX_PKO_INVALID_QUEUE;351}352353if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {354/*355* Validate the static queue priority setup and set356* static_priority_base and static_priority_end357* accordingly.358*/359for (queue = 0; queue < num_queues; queue++) {360/* Find first queue of static priority */361if (static_priority_base == -1362&& priority[queue] ==363CVMX_PKO_QUEUE_STATIC_PRIORITY)364static_priority_base = queue;365/* Find last queue of static priority */366if (static_priority_base != -1367&& static_priority_end == -1368&& priority[queue] != CVMX_PKO_QUEUE_STATIC_PRIORITY369&& queue)370static_priority_end = queue - 1;371else if (static_priority_base != -1372&& static_priority_end == -1373&& queue == num_queues - 1)374/* all queues are static priority */375static_priority_end = queue;376/*377* Check to make sure all static priority378* queues are contiguous. Also catches some379* cases of static priorities not starting at380* queue 0.381*/382if (static_priority_end != -1383&& (int)queue > static_priority_end384&& priority[queue] ==385CVMX_PKO_QUEUE_STATIC_PRIORITY) {386cvmx_dprintf("ERROR: cvmx_pko_config_port: "387"Static priority queues aren't "388"contiguous or don't start at "389"base queue. q: %d, eq: %d\n",390(int)queue, static_priority_end);391return CVMX_PKO_INVALID_PRIORITY;392}393}394if (static_priority_base > 0) {395cvmx_dprintf("ERROR: cvmx_pko_config_port: Static "396"priority queues don't start at base "397"queue. sq: %d\n",398static_priority_base);399return CVMX_PKO_INVALID_PRIORITY;400}401#if 0402cvmx_dprintf("Port %d: Static priority queue base: %d, "403"end: %d\n", port,404static_priority_base, static_priority_end);405#endif406}407/*408* At this point, static_priority_base and static_priority_end409* are either both -1, or are valid start/end queue410* numbers.411*/412413result_code = CVMX_PKO_SUCCESS;414415#ifdef PKO_DEBUG416cvmx_dprintf("num queues: %d (%lld,%lld)\n", num_queues,417CVMX_PKO_QUEUES_PER_PORT_INTERFACE0,418CVMX_PKO_QUEUES_PER_PORT_INTERFACE1);419#endif420421for (queue = 0; queue < num_queues; queue++) {422uint64_t *buf_ptr = NULL;423424config1.u64 = 0;425config1.s.idx3 = queue >> 3;426config1.s.qid7 = (base_queue + queue) >> 7;427428config.u64 = 0;429config.s.tail = queue == (num_queues - 1);430config.s.index = queue;431config.s.port = port;432config.s.queue = base_queue + queue;433434if (!cvmx_octeon_is_pass1()) {435config.s.static_p = static_priority_base >= 0;436config.s.static_q = (int)queue <= static_priority_end;437config.s.s_tail = (int)queue == static_priority_end;438}439/*440* Convert the priority into an enable bit field. Try441* to space the bits out evenly so the packet don't442* get grouped up443*/444switch ((int)priority[queue]) {445case 0:446config.s.qos_mask = 0x00;447break;448case 1:449config.s.qos_mask = 0x01;450break;451case 2:452config.s.qos_mask = 0x11;453break;454case 3:455config.s.qos_mask = 0x49;456break;457case 4:458config.s.qos_mask = 0x55;459break;460case 5:461config.s.qos_mask = 0x57;462break;463case 6:464config.s.qos_mask = 0x77;465break;466case 7:467config.s.qos_mask = 0x7f;468break;469case 8:470config.s.qos_mask = 0xff;471break;472case CVMX_PKO_QUEUE_STATIC_PRIORITY:473if (!cvmx_octeon_is_pass1()) {474config.s.qos_mask = 0xff;475break;476}477fallthrough; /* to the error case, when Pass 1 */478default:479cvmx_dprintf("ERROR: cvmx_pko_config_port: Invalid "480"priority %llu\n",481(unsigned long long)priority[queue]);482config.s.qos_mask = 0xff;483result_code = CVMX_PKO_INVALID_PRIORITY;484break;485}486487if (port != CVMX_PKO_MEM_QUEUE_PTRS_ILLEGAL_PID) {488cvmx_cmd_queue_result_t cmd_res =489cvmx_cmd_queue_initialize(CVMX_CMD_QUEUE_PKO490(base_queue + queue),491CVMX_PKO_MAX_QUEUE_DEPTH,492CVMX_FPA_OUTPUT_BUFFER_POOL,493CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE494-495CVMX_PKO_COMMAND_BUFFER_SIZE_ADJUST496* 8);497if (cmd_res != CVMX_CMD_QUEUE_SUCCESS) {498switch (cmd_res) {499case CVMX_CMD_QUEUE_NO_MEMORY:500cvmx_dprintf("ERROR: "501"cvmx_pko_config_port: "502"Unable to allocate "503"output buffer.\n");504return CVMX_PKO_NO_MEMORY;505case CVMX_CMD_QUEUE_ALREADY_SETUP:506cvmx_dprintf507("ERROR: cvmx_pko_config_port: Port already setup.\n");508return CVMX_PKO_PORT_ALREADY_SETUP;509case CVMX_CMD_QUEUE_INVALID_PARAM:510default:511cvmx_dprintf512("ERROR: cvmx_pko_config_port: Command queue initialization failed.\n");513return CVMX_PKO_CMD_QUEUE_INIT_ERROR;514}515}516517buf_ptr =518(uint64_t *)519cvmx_cmd_queue_buffer(CVMX_CMD_QUEUE_PKO520(base_queue + queue));521config.s.buf_ptr = cvmx_ptr_to_phys(buf_ptr);522} else523config.s.buf_ptr = 0;524525CVMX_SYNCWS;526527if (!OCTEON_IS_MODEL(OCTEON_CN3XXX))528cvmx_write_csr(CVMX_PKO_REG_QUEUE_PTRS1, config1.u64);529cvmx_write_csr(CVMX_PKO_MEM_QUEUE_PTRS, config.u64);530}531532return result_code;533}534535#ifdef PKO_DEBUG536/*537* Show map of ports -> queues for different cores.538*/539void cvmx_pko_show_queue_map()540{541int core, port;542int pko_output_ports = 36;543544cvmx_dprintf("port");545for (port = 0; port < pko_output_ports; port++)546cvmx_dprintf("%3d ", port);547cvmx_dprintf("\n");548549for (core = 0; core < CVMX_MAX_CORES; core++) {550cvmx_dprintf("\n%2d: ", core);551for (port = 0; port < pko_output_ports; port++) {552cvmx_dprintf("%3d ",553cvmx_pko_get_base_queue_per_core(port,554core));555}556}557cvmx_dprintf("\n");558}559#endif560561/*562* Rate limit a PKO port to a max packets/sec. This function is only563* supported on CN51XX and higher, excluding CN58XX.564*565* @port: Port to rate limit566* @packets_s: Maximum packet/sec567* @burst: Maximum number of packets to burst in a row before rate568* limiting cuts in.569*570* Returns Zero on success, negative on failure571*/572int cvmx_pko_rate_limit_packets(int port, int packets_s, int burst)573{574union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;575union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;576577pko_mem_port_rate0.u64 = 0;578pko_mem_port_rate0.s.pid = port;579pko_mem_port_rate0.s.rate_pkt =580cvmx_sysinfo_get()->cpu_clock_hz / packets_s / 16;581/* No cost per word since we are limited by packets/sec, not bits/sec */582pko_mem_port_rate0.s.rate_word = 0;583584pko_mem_port_rate1.u64 = 0;585pko_mem_port_rate1.s.pid = port;586pko_mem_port_rate1.s.rate_lim =587((uint64_t) pko_mem_port_rate0.s.rate_pkt * burst) >> 8;588589cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);590cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);591return 0;592}593594/*595* Rate limit a PKO port to a max bits/sec. This function is only596* supported on CN51XX and higher, excluding CN58XX.597*598* @port: Port to rate limit599* @bits_s: PKO rate limit in bits/sec600* @burst: Maximum number of bits to burst before rate601* limiting cuts in.602*603* Returns Zero on success, negative on failure604*/605int cvmx_pko_rate_limit_bits(int port, uint64_t bits_s, int burst)606{607union cvmx_pko_mem_port_rate0 pko_mem_port_rate0;608union cvmx_pko_mem_port_rate1 pko_mem_port_rate1;609uint64_t clock_rate = cvmx_sysinfo_get()->cpu_clock_hz;610uint64_t tokens_per_bit = clock_rate * 16 / bits_s;611612pko_mem_port_rate0.u64 = 0;613pko_mem_port_rate0.s.pid = port;614/*615* Each packet has a 12 bytes of interframe gap, an 8 byte616* preamble, and a 4 byte CRC. These are not included in the617* per word count. Multiply by 8 to convert to bits and divide618* by 256 for limit granularity.619*/620pko_mem_port_rate0.s.rate_pkt = (12 + 8 + 4) * 8 * tokens_per_bit / 256;621/* Each 8 byte word has 64bits */622pko_mem_port_rate0.s.rate_word = 64 * tokens_per_bit;623624pko_mem_port_rate1.u64 = 0;625pko_mem_port_rate1.s.pid = port;626pko_mem_port_rate1.s.rate_lim = tokens_per_bit * burst / 256;627628cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE0, pko_mem_port_rate0.u64);629cvmx_write_csr(CVMX_PKO_MEM_PORT_RATE1, pko_mem_port_rate1.u64);630return 0;631}632633634