Home | History | Annotate | Line # | Download | only in dev
octeon_pko.c revision 1.2
      1 /*	$NetBSD: octeon_pko.c,v 1.2 2020/05/31 06:27:06 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Internet Initiative Japan, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: octeon_pko.c,v 1.2 2020/05/31 06:27:06 simonb Exp $");
     31 
     32 #include "opt_octeon.h"
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/malloc.h>
     37 #include <mips/locore.h>
     38 #include <mips/cavium/octeonvar.h>
     39 #include <mips/cavium/dev/octeon_faureg.h>
     40 #include <mips/cavium/dev/octeon_fpavar.h>
     41 #include <mips/cavium/dev/octeon_pkoreg.h>
     42 #include <mips/cavium/dev/octeon_pkovar.h>
     43 
     44 static inline void	octpko_op_store(uint64_t, uint64_t);
     45 
     46 #ifdef CNMAC_DEBUG
     47 void			octpko_intr_evcnt_attach(struct octpko_softc *);
     48 void			octpko_intr_rml(void *);
     49 #endif
     50 
     51 #define	_PKO_RD8(sc, off) \
     52 	bus_space_read_8((sc)->sc_regt, (sc)->sc_regh, (off))
     53 #define	_PKO_WR8(sc, off, v) \
     54 	bus_space_write_8((sc)->sc_regt, (sc)->sc_regh, (off), (v))
     55 
     56 #ifdef CNMAC_DEBUG
     57 struct octpko_softc	*__octpko_softc;
     58 #endif
     59 
     60 /* ----- gloal functions */
     61 
     62 /* XXX */
     63 void
     64 octpko_init(struct octpko_attach_args *aa, struct octpko_softc **rsc)
     65 {
     66 	struct octpko_softc *sc;
     67 	int status;
     68 
     69 	sc = malloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
     70 	if (sc == NULL)
     71 		panic("can't allocate memory: %s", __func__);
     72 
     73 	sc->sc_port = aa->aa_port;
     74 	sc->sc_regt = aa->aa_regt;
     75 	sc->sc_cmdptr = aa->aa_cmdptr;
     76 	sc->sc_cmd_buf_pool = aa->aa_cmd_buf_pool;
     77 	sc->sc_cmd_buf_size = aa->aa_cmd_buf_size;
     78 
     79 	status = bus_space_map(sc->sc_regt, PKO_BASE, PKO_SIZE, 0,
     80 	    &sc->sc_regh);
     81 	if (status != 0)
     82 		panic("can't map %s space", "pko register");
     83 
     84 	*rsc = sc;
     85 
     86 #ifdef CNMAC_DEBUG
     87 	octpko_intr_evcnt_attach(sc);
     88 	__octpko_softc = sc;
     89 #endif
     90 }
     91 
     92 int
     93 octpko_enable(struct octpko_softc *sc)
     94 {
     95 	uint64_t reg_flags;
     96 
     97 	reg_flags = _PKO_RD8(sc, PKO_REG_FLAGS_OFFSET);
     98 	/* PKO_REG_FLAGS_RESET=0 */
     99 	/* PKO_REG_FLAGS_STORE_BE=0 */
    100 	SET(reg_flags, PKO_REG_FLAGS_ENA_DWB);
    101 	SET(reg_flags, PKO_REG_FLAGS_ENA_PKO);
    102 	/* XXX */
    103 	OCTEON_SYNCW;
    104 	_PKO_WR8(sc, PKO_REG_FLAGS_OFFSET, reg_flags);
    105 
    106 	return 0;
    107 }
    108 
    109 #if 0
    110 void
    111 octpko_reset(octpko_softc *sc)
    112 {
    113 	uint64_t reg_flags;
    114 
    115 	reg_flags = _PKO_RD8(sc, PKO_REG_FLAGS_OFFSET);
    116 	SET(reg_flags, PKO_REG_FLAGS_RESET);
    117 	_PKO_WR8(sc, PKO_REG_FLAGS_OFFSET, reg_flags);
    118 }
    119 #endif
    120 
    121 void
    122 octpko_config(struct octpko_softc *sc)
    123 {
    124 	uint64_t reg_cmd_buf = 0;
    125 
    126 	SET(reg_cmd_buf, (sc->sc_cmd_buf_pool << 20) & PKO_REG_CMD_BUF_POOL);
    127 	SET(reg_cmd_buf, sc->sc_cmd_buf_size & PKO_REG_CMD_BUF_SIZE);
    128 	_PKO_WR8(sc, PKO_REG_CMD_BUF_OFFSET, reg_cmd_buf);
    129 
    130 #ifdef CNMAC_DEBUG
    131 	octpko_int_enable(sc, 1);
    132 #endif
    133 }
    134 
    135 int
    136 octpko_port_enable(struct octpko_softc *sc, int enable)
    137 {
    138 	uint64_t reg_read_idx;
    139 	uint64_t mem_queue_qos;
    140 
    141 	reg_read_idx = 0;
    142 	SET(reg_read_idx, sc->sc_port & PKO_REG_READ_IDX_IDX);
    143 
    144 	/* XXX assume one queue maped one port */
    145 	/* Enable packet output by enabling all queues for this port */
    146 	mem_queue_qos = 0;
    147 	SET(mem_queue_qos, ((uint64_t)sc->sc_port << 7) & PKO_MEM_QUEUE_QOS_PID);
    148 	SET(mem_queue_qos, sc->sc_port & PKO_MEM_QUEUE_QOS_QID);
    149 	SET(mem_queue_qos, ((enable ? 0xffULL : 0x00ULL) << 53) &
    150 	    PKO_MEM_QUEUE_QOS_QOS_MASK);
    151 
    152 	_PKO_WR8(sc, PKO_REG_READ_IDX_OFFSET, reg_read_idx);
    153 	_PKO_WR8(sc, PKO_MEM_QUEUE_QOS_OFFSET, mem_queue_qos);
    154 
    155 	return 0;
    156 }
    157 
    158 static int pko_queue_map_init[32];
    159 
    160 int
    161 octpko_port_config(struct octpko_softc *sc)
    162 {
    163 	paddr_t buf_ptr = 0;
    164 	uint64_t mem_queue_ptrs;
    165 
    166 	KASSERT(sc->sc_port < 32);
    167 
    168 	buf_ptr = octfpa_load(FPA_COMMAND_BUFFER_POOL);
    169 	if (buf_ptr == 0)
    170 		return 1;
    171 
    172 	KASSERT(buf_ptr != 0);
    173 
    174 	/* assume one queue maped one port */
    175 	mem_queue_ptrs = 0;
    176 	SET(mem_queue_ptrs, PKO_MEM_QUEUE_PTRS_TAIL);
    177 	SET(mem_queue_ptrs, ((uint64_t)0 << 13) & PKO_MEM_QUEUE_PTRS_IDX);
    178 	SET(mem_queue_ptrs, ((uint64_t)sc->sc_port << 7) & PKO_MEM_QUEUE_PTRS_PID);
    179 	SET(mem_queue_ptrs, sc->sc_port & PKO_MEM_QUEUE_PTRS_QID);
    180 	SET(mem_queue_ptrs, ((uint64_t)0xff << 53) & PKO_MEM_QUEUE_PTRS_QOS_MASK);
    181 	SET(mem_queue_ptrs, ((uint64_t)buf_ptr << 17) & PKO_MEM_QUEUE_PTRS_BUF_PTR);
    182 	OCTEON_SYNCW;
    183 	_PKO_WR8(sc, PKO_MEM_QUEUE_PTRS_OFFSET, mem_queue_ptrs);
    184 
    185 	/*
    186 	 * Set initial command buffer address and index
    187 	 * for queue.
    188 	 */
    189 	sc->sc_cmdptr->cmdptr = (uint64_t)buf_ptr;
    190 	sc->sc_cmdptr->cmdptr_idx = 0;
    191 
    192 	pko_queue_map_init[sc->sc_port] = 1;
    193 
    194 	return 0;
    195 }
    196 
    197 #ifdef CNMAC_DEBUG
    198 int			octpko_intr_rml_verbose;
    199 struct evcnt		octpko_intr_evcnt;
    200 
    201 static const struct octeon_evcnt_entry octpko_intr_evcnt_entries[] = {
    202 #define	_ENTRY(name, type, parent, descr) \
    203 	OCTEON_EVCNT_ENTRY(struct octpko_softc, name, type, parent, descr)
    204 	_ENTRY(pkoerrdbell,		MISC, NULL, "pko doorbell overflow"),
    205 	_ENTRY(pkoerrparity,		MISC, NULL, "pko parity error")
    206 #undef	_ENTRY
    207 };
    208 
    209 void
    210 octpko_intr_evcnt_attach(struct octpko_softc *sc)
    211 {
    212 	OCTEON_EVCNT_ATTACH_EVCNTS(sc, octpko_intr_evcnt_entries, "pko0");
    213 }
    214 
    215 void
    216 octpko_intr_rml(void *arg)
    217 {
    218 	struct octpko_softc *sc;
    219 	uint64_t reg;
    220 
    221 	octpko_intr_evcnt.ev_count++;
    222 	sc = __octpko_softc;
    223 	KASSERT(sc != NULL);
    224 	reg = octpko_int_summary(sc);
    225 	if (octpko_intr_rml_verbose)
    226 		printf("%s: PKO_REG_ERROR=0x%016" PRIx64 "\n", __func__, reg);
    227 	if (reg & PKO_REG_ERROR_DOORBELL)
    228 		OCTEON_EVCNT_INC(sc, pkoerrdbell);
    229 	if (reg & PKO_REG_ERROR_PARITY)
    230 		OCTEON_EVCNT_INC(sc, pkoerrparity);
    231 }
    232 
    233 void
    234 octpko_int_enable(struct octpko_softc *sc, int enable)
    235 {
    236 	uint64_t pko_int_xxx = 0;
    237 
    238 	pko_int_xxx = PKO_REG_ERROR_DOORBELL | PKO_REG_ERROR_PARITY;
    239 	_PKO_WR8(sc, PKO_REG_ERROR_OFFSET, pko_int_xxx);
    240 	_PKO_WR8(sc, PKO_REG_INT_MASK_OFFSET, enable ? pko_int_xxx : 0);
    241 }
    242 
    243 uint64_t
    244 octpko_int_summary(struct octpko_softc *sc)
    245 {
    246 	uint64_t summary;
    247 
    248 	summary = _PKO_RD8(sc, PKO_REG_ERROR_OFFSET);
    249 	_PKO_WR8(sc, PKO_REG_ERROR_OFFSET, summary);
    250 	return summary;
    251 }
    252 #endif
    253