162306a36Sopenharmony_ci/* Copyright 2008 - 2016 Freescale Semiconductor, Inc.
262306a36Sopenharmony_ci *
362306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
462306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
562306a36Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
662306a36Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer.
762306a36Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copyright
862306a36Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer in the
962306a36Sopenharmony_ci *	 documentation and/or other materials provided with the distribution.
1062306a36Sopenharmony_ci *     * Neither the name of Freescale Semiconductor nor the
1162306a36Sopenharmony_ci *	 names of its contributors may be used to endorse or promote products
1262306a36Sopenharmony_ci *	 derived from this software without specific prior written permission.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * ALTERNATIVELY, this software may be distributed under the terms of the
1562306a36Sopenharmony_ci * GNU General Public License ("GPL") as published by the Free Software
1662306a36Sopenharmony_ci * Foundation, either version 2 of that License or (at your option) any
1762306a36Sopenharmony_ci * later version.
1862306a36Sopenharmony_ci *
1962306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
2062306a36Sopenharmony_ci * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2162306a36Sopenharmony_ci * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2262306a36Sopenharmony_ci * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
2362306a36Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2462306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2562306a36Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2662306a36Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2762306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2862306a36Sopenharmony_ci * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#include "dpaa_sys.h"
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#include <soc/fsl/qman.h>
3462306a36Sopenharmony_ci#include <linux/dma-mapping.h>
3562306a36Sopenharmony_ci#include <linux/iommu.h>
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#if defined(CONFIG_FSL_PAMU)
3862306a36Sopenharmony_ci#include <asm/fsl_pamu_stash.h>
3962306a36Sopenharmony_ci#endif
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_cistruct qm_mcr_querywq {
4262306a36Sopenharmony_ci	u8 verb;
4362306a36Sopenharmony_ci	u8 result;
4462306a36Sopenharmony_ci	u16 channel_wq; /* ignores wq (3 lsbits): _res[0-2] */
4562306a36Sopenharmony_ci	u8 __reserved[28];
4662306a36Sopenharmony_ci	u32 wq_len[8];
4762306a36Sopenharmony_ci} __packed;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistatic inline u16 qm_mcr_querywq_get_chan(const struct qm_mcr_querywq *wq)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	return wq->channel_wq >> 3;
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistruct __qm_mcr_querycongestion {
5562306a36Sopenharmony_ci	u32 state[8];
5662306a36Sopenharmony_ci};
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/* "Query Congestion Group State" */
5962306a36Sopenharmony_cistruct qm_mcr_querycongestion {
6062306a36Sopenharmony_ci	u8 verb;
6162306a36Sopenharmony_ci	u8 result;
6262306a36Sopenharmony_ci	u8 __reserved[30];
6362306a36Sopenharmony_ci	/* Access this struct using qman_cgrs_get() */
6462306a36Sopenharmony_ci	struct __qm_mcr_querycongestion state;
6562306a36Sopenharmony_ci} __packed;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci/* "Query CGR" */
6862306a36Sopenharmony_cistruct qm_mcr_querycgr {
6962306a36Sopenharmony_ci	u8 verb;
7062306a36Sopenharmony_ci	u8 result;
7162306a36Sopenharmony_ci	u16 __reserved1;
7262306a36Sopenharmony_ci	struct __qm_mc_cgr cgr; /* CGR fields */
7362306a36Sopenharmony_ci	u8 __reserved2[6];
7462306a36Sopenharmony_ci	u8 i_bcnt_hi;	/* high 8-bits of 40-bit "Instant" */
7562306a36Sopenharmony_ci	__be32 i_bcnt_lo;	/* low 32-bits of 40-bit */
7662306a36Sopenharmony_ci	u8 __reserved3[3];
7762306a36Sopenharmony_ci	u8 a_bcnt_hi;	/* high 8-bits of 40-bit "Average" */
7862306a36Sopenharmony_ci	__be32 a_bcnt_lo;	/* low 32-bits of 40-bit */
7962306a36Sopenharmony_ci	__be32 cscn_targ_swp[4];
8062306a36Sopenharmony_ci} __packed;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic inline u64 qm_mcr_querycgr_i_get64(const struct qm_mcr_querycgr *q)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	return ((u64)q->i_bcnt_hi << 32) | be32_to_cpu(q->i_bcnt_lo);
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_cistatic inline u64 qm_mcr_querycgr_a_get64(const struct qm_mcr_querycgr *q)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci	return ((u64)q->a_bcnt_hi << 32) | be32_to_cpu(q->a_bcnt_lo);
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/* Congestion Groups */
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/*
9462306a36Sopenharmony_ci * This wrapper represents a bit-array for the state of the 256 QMan congestion
9562306a36Sopenharmony_ci * groups. Is also used as a *mask* for congestion groups, eg. so we ignore
9662306a36Sopenharmony_ci * those that don't concern us. We harness the structure and accessor details
9762306a36Sopenharmony_ci * already used in the management command to query congestion groups.
9862306a36Sopenharmony_ci */
9962306a36Sopenharmony_ci#define CGR_BITS_PER_WORD 5
10062306a36Sopenharmony_ci#define CGR_WORD(x)	((x) >> CGR_BITS_PER_WORD)
10162306a36Sopenharmony_ci#define CGR_BIT(x)	(BIT(31) >> ((x) & 0x1f))
10262306a36Sopenharmony_ci#define CGR_NUM	(sizeof(struct __qm_mcr_querycongestion) << 3)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistruct qman_cgrs {
10562306a36Sopenharmony_ci	struct __qm_mcr_querycongestion q;
10662306a36Sopenharmony_ci};
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic inline void qman_cgrs_init(struct qman_cgrs *c)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	memset(c, 0, sizeof(*c));
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic inline void qman_cgrs_fill(struct qman_cgrs *c)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	memset(c, 0xff, sizeof(*c));
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic inline int qman_cgrs_get(struct qman_cgrs *c, u8 cgr)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	return c->q.state[CGR_WORD(cgr)] & CGR_BIT(cgr);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic inline void qman_cgrs_cp(struct qman_cgrs *dest,
12462306a36Sopenharmony_ci				const struct qman_cgrs *src)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	*dest = *src;
12762306a36Sopenharmony_ci}
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistatic inline void qman_cgrs_and(struct qman_cgrs *dest,
13062306a36Sopenharmony_ci			const struct qman_cgrs *a, const struct qman_cgrs *b)
13162306a36Sopenharmony_ci{
13262306a36Sopenharmony_ci	int ret;
13362306a36Sopenharmony_ci	u32 *_d = dest->q.state;
13462306a36Sopenharmony_ci	const u32 *_a = a->q.state;
13562306a36Sopenharmony_ci	const u32 *_b = b->q.state;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	for (ret = 0; ret < 8; ret++)
13862306a36Sopenharmony_ci		*_d++ = *_a++ & *_b++;
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_cistatic inline void qman_cgrs_xor(struct qman_cgrs *dest,
14262306a36Sopenharmony_ci			const struct qman_cgrs *a, const struct qman_cgrs *b)
14362306a36Sopenharmony_ci{
14462306a36Sopenharmony_ci	int ret;
14562306a36Sopenharmony_ci	u32 *_d = dest->q.state;
14662306a36Sopenharmony_ci	const u32 *_a = a->q.state;
14762306a36Sopenharmony_ci	const u32 *_b = b->q.state;
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	for (ret = 0; ret < 8; ret++)
15062306a36Sopenharmony_ci		*_d++ = *_a++ ^ *_b++;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_civoid qman_init_cgr_all(void);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistruct qm_portal_config {
15662306a36Sopenharmony_ci	/* Portal addresses */
15762306a36Sopenharmony_ci	void *addr_virt_ce;
15862306a36Sopenharmony_ci	void __iomem *addr_virt_ci;
15962306a36Sopenharmony_ci	struct device *dev;
16062306a36Sopenharmony_ci	struct iommu_domain *iommu_domain;
16162306a36Sopenharmony_ci	/* Allow these to be joined in lists */
16262306a36Sopenharmony_ci	struct list_head list;
16362306a36Sopenharmony_ci	/* User-visible portal configuration settings */
16462306a36Sopenharmony_ci	/* portal is affined to this cpu */
16562306a36Sopenharmony_ci	int cpu;
16662306a36Sopenharmony_ci	/* portal interrupt line */
16762306a36Sopenharmony_ci	int irq;
16862306a36Sopenharmony_ci	/*
16962306a36Sopenharmony_ci	 * the portal's dedicated channel id, used initialising
17062306a36Sopenharmony_ci	 * frame queues to target this portal when scheduled
17162306a36Sopenharmony_ci	 */
17262306a36Sopenharmony_ci	u16 channel;
17362306a36Sopenharmony_ci	/*
17462306a36Sopenharmony_ci	 * mask of pool channels this portal has dequeue access to
17562306a36Sopenharmony_ci	 * (using QM_SDQCR_CHANNELS_POOL(n) for the bitmask)
17662306a36Sopenharmony_ci	 */
17762306a36Sopenharmony_ci	u32 pools;
17862306a36Sopenharmony_ci};
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/* Revision info (for errata and feature handling) */
18162306a36Sopenharmony_ci#define QMAN_REV11 0x0101
18262306a36Sopenharmony_ci#define QMAN_REV12 0x0102
18362306a36Sopenharmony_ci#define QMAN_REV20 0x0200
18462306a36Sopenharmony_ci#define QMAN_REV30 0x0300
18562306a36Sopenharmony_ci#define QMAN_REV31 0x0301
18662306a36Sopenharmony_ci#define QMAN_REV32 0x0302
18762306a36Sopenharmony_ciextern u16 qman_ip_rev; /* 0 if uninitialised, otherwise QMAN_REVx */
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci#define QM_FQID_RANGE_START 1 /* FQID 0 reserved for internal use */
19062306a36Sopenharmony_ciextern struct gen_pool *qm_fqalloc; /* FQID allocator */
19162306a36Sopenharmony_ciextern struct gen_pool *qm_qpalloc; /* pool-channel allocator */
19262306a36Sopenharmony_ciextern struct gen_pool *qm_cgralloc; /* CGR ID allocator */
19362306a36Sopenharmony_ciu32 qm_get_pools_sdqcr(void);
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ciint qman_wq_alloc(void);
19662306a36Sopenharmony_ci#ifdef CONFIG_FSL_PAMU
19762306a36Sopenharmony_ci#define qman_liodn_fixup __qman_liodn_fixup
19862306a36Sopenharmony_ci#else
19962306a36Sopenharmony_cistatic inline void qman_liodn_fixup(u16 channel)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci#endif
20362306a36Sopenharmony_civoid __qman_liodn_fixup(u16 channel);
20462306a36Sopenharmony_civoid qman_set_sdest(u16 channel, unsigned int cpu_idx);
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_cistruct qman_portal *qman_create_affine_portal(
20762306a36Sopenharmony_ci			const struct qm_portal_config *config,
20862306a36Sopenharmony_ci			const struct qman_cgrs *cgrs);
20962306a36Sopenharmony_ciconst struct qm_portal_config *qman_destroy_affine_portal(void);
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci/*
21262306a36Sopenharmony_ci * qman_query_fq - Queries FQD fields (via h/w query command)
21362306a36Sopenharmony_ci * @fq: the frame queue object to be queried
21462306a36Sopenharmony_ci * @fqd: storage for the queried FQD fields
21562306a36Sopenharmony_ci */
21662306a36Sopenharmony_ciint qman_query_fq(struct qman_fq *fq, struct qm_fqd *fqd);
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciint qman_alloc_fq_table(u32 num_fqids);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci/*   QMan s/w corenet portal, low-level i/face	 */
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci/*
22362306a36Sopenharmony_ci * For qm_dqrr_sdqcr_set(); Choose one SOURCE. Choose one COUNT. Choose one
22462306a36Sopenharmony_ci * dequeue TYPE. Choose TOKEN (8-bit).
22562306a36Sopenharmony_ci * If SOURCE == CHANNELS,
22662306a36Sopenharmony_ci *   Choose CHANNELS_DEDICATED and/or CHANNELS_POOL(n).
22762306a36Sopenharmony_ci *   You can choose DEDICATED_PRECEDENCE if the portal channel should have
22862306a36Sopenharmony_ci *   priority.
22962306a36Sopenharmony_ci * If SOURCE == SPECIFICWQ,
23062306a36Sopenharmony_ci *     Either select the work-queue ID with SPECIFICWQ_WQ(), or select the
23162306a36Sopenharmony_ci *     channel (SPECIFICWQ_DEDICATED or SPECIFICWQ_POOL()) and specify the
23262306a36Sopenharmony_ci *     work-queue priority (0-7) with SPECIFICWQ_WQ() - either way, you get the
23362306a36Sopenharmony_ci *     same value.
23462306a36Sopenharmony_ci */
23562306a36Sopenharmony_ci#define QM_SDQCR_SOURCE_CHANNELS	0x0
23662306a36Sopenharmony_ci#define QM_SDQCR_SOURCE_SPECIFICWQ	0x40000000
23762306a36Sopenharmony_ci#define QM_SDQCR_COUNT_EXACT1		0x0
23862306a36Sopenharmony_ci#define QM_SDQCR_COUNT_UPTO3		0x20000000
23962306a36Sopenharmony_ci#define QM_SDQCR_DEDICATED_PRECEDENCE	0x10000000
24062306a36Sopenharmony_ci#define QM_SDQCR_TYPE_MASK		0x03000000
24162306a36Sopenharmony_ci#define QM_SDQCR_TYPE_NULL		0x0
24262306a36Sopenharmony_ci#define QM_SDQCR_TYPE_PRIO_QOS		0x01000000
24362306a36Sopenharmony_ci#define QM_SDQCR_TYPE_ACTIVE_QOS	0x02000000
24462306a36Sopenharmony_ci#define QM_SDQCR_TYPE_ACTIVE		0x03000000
24562306a36Sopenharmony_ci#define QM_SDQCR_TOKEN_MASK		0x00ff0000
24662306a36Sopenharmony_ci#define QM_SDQCR_TOKEN_SET(v)		(((v) & 0xff) << 16)
24762306a36Sopenharmony_ci#define QM_SDQCR_TOKEN_GET(v)		(((v) >> 16) & 0xff)
24862306a36Sopenharmony_ci#define QM_SDQCR_CHANNELS_DEDICATED	0x00008000
24962306a36Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_MASK	0x000000f7
25062306a36Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_DEDICATED	0x00000000
25162306a36Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_POOL(n)	((n) << 4)
25262306a36Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_WQ(n)	(n)
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci/* For qm_dqrr_vdqcr_set(): use FQID(n) to fill in the frame queue ID */
25562306a36Sopenharmony_ci#define QM_VDQCR_FQID_MASK		0x00ffffff
25662306a36Sopenharmony_ci#define QM_VDQCR_FQID(n)		((n) & QM_VDQCR_FQID_MASK)
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci/*
25962306a36Sopenharmony_ci * Used by all portal interrupt registers except 'inhibit'
26062306a36Sopenharmony_ci * Channels with frame availability
26162306a36Sopenharmony_ci */
26262306a36Sopenharmony_ci#define QM_PIRQ_DQAVAIL	0x0000ffff
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci/* The DQAVAIL interrupt fields break down into these bits; */
26562306a36Sopenharmony_ci#define QM_DQAVAIL_PORTAL	0x8000		/* Portal channel */
26662306a36Sopenharmony_ci#define QM_DQAVAIL_POOL(n)	(0x8000 >> (n))	/* Pool channel, n==[1..15] */
26762306a36Sopenharmony_ci#define QM_DQAVAIL_MASK		0xffff
26862306a36Sopenharmony_ci/* This mask contains all the "irqsource" bits visible to API users */
26962306a36Sopenharmony_ci#define QM_PIRQ_VISIBLE	(QM_PIRQ_SLOW | QM_PIRQ_DQRI)
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ciextern struct qman_portal *affine_portals[NR_CPUS];
27262306a36Sopenharmony_ciextern struct qman_portal *qman_dma_portal;
27362306a36Sopenharmony_ciconst struct qm_portal_config *qman_get_qm_portal_config(
27462306a36Sopenharmony_ci						struct qman_portal *portal);
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ciunsigned int qm_get_fqid_maxcnt(void);
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ciint qman_shutdown_fq(u32 fqid);
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ciint qman_requires_cleanup(void);
28162306a36Sopenharmony_civoid qman_done_cleanup(void);
28262306a36Sopenharmony_civoid qman_enable_irqs(void);
283