1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
2a2a55e51SPrabhakar Kushwaha /*
3a2a55e51SPrabhakar Kushwaha * Copyright (C) 2014 Freescale Semiconductor
4a2a55e51SPrabhakar Kushwaha */
5a2a55e51SPrabhakar Kushwaha
6a2a55e51SPrabhakar Kushwaha #include "qbman_private.h"
7a2a55e51SPrabhakar Kushwaha #include <fsl-mc/fsl_qbman_portal.h>
8a2a55e51SPrabhakar Kushwaha #include <fsl-mc/fsl_dpaa_fd.h>
9a2a55e51SPrabhakar Kushwaha
10a2a55e51SPrabhakar Kushwaha /* All QBMan command and result structures use this "valid bit" encoding */
11a2a55e51SPrabhakar Kushwaha #define QB_VALID_BIT ((uint32_t)0x80)
12a2a55e51SPrabhakar Kushwaha
13a2a55e51SPrabhakar Kushwaha /* Management command result codes */
14a2a55e51SPrabhakar Kushwaha #define QBMAN_MC_RSLT_OK 0xf0
15a2a55e51SPrabhakar Kushwaha
168e62f1eeSPriyanka Jain #define QBMAN_VER_4_0_DQRR_SIZE 4
178e62f1eeSPriyanka Jain #define QBMAN_VER_4_1_DQRR_SIZE 8
18cd8aefc0SPrabhakar Kushwaha
19cd8aefc0SPrabhakar Kushwaha
20a2a55e51SPrabhakar Kushwaha /* --------------------- */
21a2a55e51SPrabhakar Kushwaha /* portal data structure */
22a2a55e51SPrabhakar Kushwaha /* --------------------- */
23a2a55e51SPrabhakar Kushwaha
24a2a55e51SPrabhakar Kushwaha struct qbman_swp {
25a2a55e51SPrabhakar Kushwaha const struct qbman_swp_desc *desc;
26a2a55e51SPrabhakar Kushwaha /* The qbman_sys (ie. arch/OS-specific) support code can put anything it
27a2a55e51SPrabhakar Kushwaha * needs in here. */
28a2a55e51SPrabhakar Kushwaha struct qbman_swp_sys sys;
29a2a55e51SPrabhakar Kushwaha /* Management commands */
30a2a55e51SPrabhakar Kushwaha struct {
31a2a55e51SPrabhakar Kushwaha #ifdef QBMAN_CHECKING
32a2a55e51SPrabhakar Kushwaha enum swp_mc_check {
33a2a55e51SPrabhakar Kushwaha swp_mc_can_start, /* call __qbman_swp_mc_start() */
34a2a55e51SPrabhakar Kushwaha swp_mc_can_submit, /* call __qbman_swp_mc_submit() */
35a2a55e51SPrabhakar Kushwaha swp_mc_can_poll, /* call __qbman_swp_mc_result() */
36a2a55e51SPrabhakar Kushwaha } check;
37a2a55e51SPrabhakar Kushwaha #endif
38a2a55e51SPrabhakar Kushwaha uint32_t valid_bit; /* 0x00 or 0x80 */
39a2a55e51SPrabhakar Kushwaha } mc;
40a2a55e51SPrabhakar Kushwaha /* Push dequeues */
41a2a55e51SPrabhakar Kushwaha uint32_t sdq;
42a2a55e51SPrabhakar Kushwaha /* Volatile dequeues */
43a2a55e51SPrabhakar Kushwaha struct {
44a2a55e51SPrabhakar Kushwaha /* VDQCR supports a "1 deep pipeline", meaning that if you know
45a2a55e51SPrabhakar Kushwaha * the last-submitted command is already executing in the
46a2a55e51SPrabhakar Kushwaha * hardware (as evidenced by at least 1 valid dequeue result),
47a2a55e51SPrabhakar Kushwaha * you can write another dequeue command to the register, the
48a2a55e51SPrabhakar Kushwaha * hardware will start executing it as soon as the
49a2a55e51SPrabhakar Kushwaha * already-executing command terminates. (This minimises latency
50a2a55e51SPrabhakar Kushwaha * and stalls.) With that in mind, this "busy" variable refers
51a2a55e51SPrabhakar Kushwaha * to whether or not a command can be submitted, not whether or
52a2a55e51SPrabhakar Kushwaha * not a previously-submitted command is still executing. In
53a2a55e51SPrabhakar Kushwaha * other words, once proof is seen that the previously-submitted
54cd8aefc0SPrabhakar Kushwaha * command is executing, "vdq" is no longer "busy".
55cd8aefc0SPrabhakar Kushwaha */
56cd8aefc0SPrabhakar Kushwaha atomic_t busy;
57a2a55e51SPrabhakar Kushwaha uint32_t valid_bit; /* 0x00 or 0x80 */
58a2a55e51SPrabhakar Kushwaha /* We need to determine when vdq is no longer busy. This depends
59a2a55e51SPrabhakar Kushwaha * on whether the "busy" (last-submitted) dequeue command is
60cd8aefc0SPrabhakar Kushwaha * targeting DQRR or main-memory, and detected is based on the
61a2a55e51SPrabhakar Kushwaha * presence of the dequeue command's "token" showing up in
62a2a55e51SPrabhakar Kushwaha * dequeue entries in DQRR or main-memory (respectively). Debug
63a2a55e51SPrabhakar Kushwaha * builds will, when submitting vdq commands, verify that the
64a2a55e51SPrabhakar Kushwaha * dequeue result location is not already equal to the command's
65a2a55e51SPrabhakar Kushwaha * token value. */
66a2a55e51SPrabhakar Kushwaha struct ldpaa_dq *storage; /* NULL if DQRR */
67a2a55e51SPrabhakar Kushwaha uint32_t token;
68a2a55e51SPrabhakar Kushwaha } vdq;
69a2a55e51SPrabhakar Kushwaha /* DQRR */
70a2a55e51SPrabhakar Kushwaha struct {
71a2a55e51SPrabhakar Kushwaha uint32_t next_idx;
72a2a55e51SPrabhakar Kushwaha uint32_t valid_bit;
738e62f1eeSPriyanka Jain uint8_t dqrr_size;
74a2a55e51SPrabhakar Kushwaha } dqrr;
75a2a55e51SPrabhakar Kushwaha };
76a2a55e51SPrabhakar Kushwaha
77a2a55e51SPrabhakar Kushwaha /* -------------------------- */
78a2a55e51SPrabhakar Kushwaha /* portal management commands */
79a2a55e51SPrabhakar Kushwaha /* -------------------------- */
80a2a55e51SPrabhakar Kushwaha
81a2a55e51SPrabhakar Kushwaha /* Different management commands all use this common base layer of code to issue
82a2a55e51SPrabhakar Kushwaha * commands and poll for results. The first function returns a pointer to where
83a2a55e51SPrabhakar Kushwaha * the caller should fill in their MC command (though they should ignore the
84a2a55e51SPrabhakar Kushwaha * verb byte), the second function commits merges in the caller-supplied command
85a2a55e51SPrabhakar Kushwaha * verb (which should not include the valid-bit) and submits the command to
86a2a55e51SPrabhakar Kushwaha * hardware, and the third function checks for a completed response (returns
87a2a55e51SPrabhakar Kushwaha * non-NULL if only if the response is complete). */
88a2a55e51SPrabhakar Kushwaha void *qbman_swp_mc_start(struct qbman_swp *p);
89a2a55e51SPrabhakar Kushwaha void qbman_swp_mc_submit(struct qbman_swp *p, void *cmd, uint32_t cmd_verb);
90a2a55e51SPrabhakar Kushwaha void *qbman_swp_mc_result(struct qbman_swp *p);
91a2a55e51SPrabhakar Kushwaha
92a2a55e51SPrabhakar Kushwaha /* Wraps up submit + poll-for-result */
qbman_swp_mc_complete(struct qbman_swp * swp,void * cmd,uint32_t cmd_verb)93a2a55e51SPrabhakar Kushwaha static inline void *qbman_swp_mc_complete(struct qbman_swp *swp, void *cmd,
94a2a55e51SPrabhakar Kushwaha uint32_t cmd_verb)
95a2a55e51SPrabhakar Kushwaha {
96a2a55e51SPrabhakar Kushwaha int loopvar;
97a2a55e51SPrabhakar Kushwaha
98a2a55e51SPrabhakar Kushwaha qbman_swp_mc_submit(swp, cmd, cmd_verb);
99a2a55e51SPrabhakar Kushwaha DBG_POLL_START(loopvar);
100a2a55e51SPrabhakar Kushwaha do {
101a2a55e51SPrabhakar Kushwaha DBG_POLL_CHECK(loopvar);
102a2a55e51SPrabhakar Kushwaha cmd = qbman_swp_mc_result(swp);
103a2a55e51SPrabhakar Kushwaha } while (!cmd);
104a2a55e51SPrabhakar Kushwaha return cmd;
105a2a55e51SPrabhakar Kushwaha }
106a2a55e51SPrabhakar Kushwaha
107a2a55e51SPrabhakar Kushwaha /* ------------ */
108a2a55e51SPrabhakar Kushwaha /* qb_attr_code */
109a2a55e51SPrabhakar Kushwaha /* ------------ */
110a2a55e51SPrabhakar Kushwaha
111a2a55e51SPrabhakar Kushwaha /* This struct locates a sub-field within a QBMan portal (CENA) cacheline which
112a2a55e51SPrabhakar Kushwaha * is either serving as a configuration command or a query result. The
113a2a55e51SPrabhakar Kushwaha * representation is inherently little-endian, as the indexing of the words is
114a2a55e51SPrabhakar Kushwaha * itself little-endian in nature and layerscape is little endian for anything
115a2a55e51SPrabhakar Kushwaha * that crosses a word boundary too (64-bit fields are the obvious examples).
116a2a55e51SPrabhakar Kushwaha */
117a2a55e51SPrabhakar Kushwaha struct qb_attr_code {
118a2a55e51SPrabhakar Kushwaha unsigned int word; /* which uint32_t[] array member encodes the field */
119a2a55e51SPrabhakar Kushwaha unsigned int lsoffset; /* encoding offset from ls-bit */
120a2a55e51SPrabhakar Kushwaha unsigned int width; /* encoding width. (bool must be 1.) */
121a2a55e51SPrabhakar Kushwaha };
122a2a55e51SPrabhakar Kushwaha
123a2a55e51SPrabhakar Kushwaha /* Macros to define codes */
124a2a55e51SPrabhakar Kushwaha #define QB_CODE(a, b, c) { a, b, c}
125a2a55e51SPrabhakar Kushwaha
126a2a55e51SPrabhakar Kushwaha /* decode a field from a cacheline */
qb_attr_code_decode(const struct qb_attr_code * code,const uint32_t * cacheline)127a2a55e51SPrabhakar Kushwaha static inline uint32_t qb_attr_code_decode(const struct qb_attr_code *code,
128a2a55e51SPrabhakar Kushwaha const uint32_t *cacheline)
129a2a55e51SPrabhakar Kushwaha {
130a2a55e51SPrabhakar Kushwaha return d32_uint32_t(code->lsoffset, code->width, cacheline[code->word]);
131a2a55e51SPrabhakar Kushwaha }
132a2a55e51SPrabhakar Kushwaha
133cd8aefc0SPrabhakar Kushwaha
134a2a55e51SPrabhakar Kushwaha /* encode a field to a cacheline */
qb_attr_code_encode(const struct qb_attr_code * code,uint32_t * cacheline,uint32_t val)135a2a55e51SPrabhakar Kushwaha static inline void qb_attr_code_encode(const struct qb_attr_code *code,
136a2a55e51SPrabhakar Kushwaha uint32_t *cacheline, uint32_t val)
137a2a55e51SPrabhakar Kushwaha {
138a2a55e51SPrabhakar Kushwaha cacheline[code->word] =
139a2a55e51SPrabhakar Kushwaha r32_uint32_t(code->lsoffset, code->width, cacheline[code->word])
140a2a55e51SPrabhakar Kushwaha | e32_uint32_t(code->lsoffset, code->width, val);
141a2a55e51SPrabhakar Kushwaha }
142a2a55e51SPrabhakar Kushwaha
qb_attr_code_encode_64(const struct qb_attr_code * code,uint64_t * cacheline,uint64_t val)143cd8aefc0SPrabhakar Kushwaha static inline void qb_attr_code_encode_64(const struct qb_attr_code *code,
144cd8aefc0SPrabhakar Kushwaha uint64_t *cacheline, uint64_t val)
145cd8aefc0SPrabhakar Kushwaha {
146cd8aefc0SPrabhakar Kushwaha cacheline[code->word / 2] = val;
147cd8aefc0SPrabhakar Kushwaha }
148cd8aefc0SPrabhakar Kushwaha
149a2a55e51SPrabhakar Kushwaha /* ---------------------- */
150a2a55e51SPrabhakar Kushwaha /* Descriptors/cachelines */
151a2a55e51SPrabhakar Kushwaha /* ---------------------- */
152a2a55e51SPrabhakar Kushwaha
153a2a55e51SPrabhakar Kushwaha /* To avoid needless dynamic allocation, the driver API often gives the caller
154a2a55e51SPrabhakar Kushwaha * a "descriptor" type that the caller can instantiate however they like.
155a2a55e51SPrabhakar Kushwaha * Ultimately though, it is just a cacheline of binary storage (or something
156a2a55e51SPrabhakar Kushwaha * smaller when it is known that the descriptor doesn't need all 64 bytes) for
157cd8aefc0SPrabhakar Kushwaha * holding pre-formatted pieces of hardware commands. The performance-critical
158a2a55e51SPrabhakar Kushwaha * code can then copy these descriptors directly into hardware command
159a2a55e51SPrabhakar Kushwaha * registers more efficiently than trying to construct/format commands
160a2a55e51SPrabhakar Kushwaha * on-the-fly. The API user sees the descriptor as an array of 32-bit words in
161a2a55e51SPrabhakar Kushwaha * order for the compiler to know its size, but the internal details are not
162a2a55e51SPrabhakar Kushwaha * exposed. The following macro is used within the driver for converting *any*
163a2a55e51SPrabhakar Kushwaha * descriptor pointer to a usable array pointer. The use of a macro (instead of
164a2a55e51SPrabhakar Kushwaha * an inline) is necessary to work with different descriptor types and to work
165a2a55e51SPrabhakar Kushwaha * correctly with const and non-const inputs (and similarly-qualified outputs).
166a2a55e51SPrabhakar Kushwaha */
167a2a55e51SPrabhakar Kushwaha #define qb_cl(d) (&(d)->dont_manipulate_directly[0])
168