18c2ecf20Sopenharmony_ci/* Copyright 2008 - 2016 Freescale Semiconductor, Inc.
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
48c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
58c2ecf20Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
68c2ecf20Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer.
78c2ecf20Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copyright
88c2ecf20Sopenharmony_ci *	 notice, this list of conditions and the following disclaimer in the
98c2ecf20Sopenharmony_ci *	 documentation and/or other materials provided with the distribution.
108c2ecf20Sopenharmony_ci *     * Neither the name of Freescale Semiconductor nor the
118c2ecf20Sopenharmony_ci *	 names of its contributors may be used to endorse or promote products
128c2ecf20Sopenharmony_ci *	 derived from this software without specific prior written permission.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * ALTERNATIVELY, this software may be distributed under the terms of the
158c2ecf20Sopenharmony_ci * GNU General Public License ("GPL") as published by the Free Software
168c2ecf20Sopenharmony_ci * Foundation, either version 2 of that License or (at your option) any
178c2ecf20Sopenharmony_ci * later version.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
208c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
218c2ecf20Sopenharmony_ci * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
228c2ecf20Sopenharmony_ci * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
238c2ecf20Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
248c2ecf20Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
258c2ecf20Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
268c2ecf20Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
288c2ecf20Sopenharmony_ci * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "dpaa_sys.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include <soc/fsl/qman.h>
348c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
358c2ecf20Sopenharmony_ci#include <linux/iommu.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#if defined(CONFIG_FSL_PAMU)
388c2ecf20Sopenharmony_ci#include <asm/fsl_pamu_stash.h>
398c2ecf20Sopenharmony_ci#endif
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistruct qm_mcr_querywq {
428c2ecf20Sopenharmony_ci	u8 verb;
438c2ecf20Sopenharmony_ci	u8 result;
448c2ecf20Sopenharmony_ci	u16 channel_wq; /* ignores wq (3 lsbits): _res[0-2] */
458c2ecf20Sopenharmony_ci	u8 __reserved[28];
468c2ecf20Sopenharmony_ci	u32 wq_len[8];
478c2ecf20Sopenharmony_ci} __packed;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic inline u16 qm_mcr_querywq_get_chan(const struct qm_mcr_querywq *wq)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	return wq->channel_wq >> 3;
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistruct __qm_mcr_querycongestion {
558c2ecf20Sopenharmony_ci	u32 state[8];
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* "Query Congestion Group State" */
598c2ecf20Sopenharmony_cistruct qm_mcr_querycongestion {
608c2ecf20Sopenharmony_ci	u8 verb;
618c2ecf20Sopenharmony_ci	u8 result;
628c2ecf20Sopenharmony_ci	u8 __reserved[30];
638c2ecf20Sopenharmony_ci	/* Access this struct using qman_cgrs_get() */
648c2ecf20Sopenharmony_ci	struct __qm_mcr_querycongestion state;
658c2ecf20Sopenharmony_ci} __packed;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* "Query CGR" */
688c2ecf20Sopenharmony_cistruct qm_mcr_querycgr {
698c2ecf20Sopenharmony_ci	u8 verb;
708c2ecf20Sopenharmony_ci	u8 result;
718c2ecf20Sopenharmony_ci	u16 __reserved1;
728c2ecf20Sopenharmony_ci	struct __qm_mc_cgr cgr; /* CGR fields */
738c2ecf20Sopenharmony_ci	u8 __reserved2[6];
748c2ecf20Sopenharmony_ci	u8 i_bcnt_hi;	/* high 8-bits of 40-bit "Instant" */
758c2ecf20Sopenharmony_ci	__be32 i_bcnt_lo;	/* low 32-bits of 40-bit */
768c2ecf20Sopenharmony_ci	u8 __reserved3[3];
778c2ecf20Sopenharmony_ci	u8 a_bcnt_hi;	/* high 8-bits of 40-bit "Average" */
788c2ecf20Sopenharmony_ci	__be32 a_bcnt_lo;	/* low 32-bits of 40-bit */
798c2ecf20Sopenharmony_ci	__be32 cscn_targ_swp[4];
808c2ecf20Sopenharmony_ci} __packed;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic inline u64 qm_mcr_querycgr_i_get64(const struct qm_mcr_querycgr *q)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	return ((u64)q->i_bcnt_hi << 32) | be32_to_cpu(q->i_bcnt_lo);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_cistatic inline u64 qm_mcr_querycgr_a_get64(const struct qm_mcr_querycgr *q)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	return ((u64)q->a_bcnt_hi << 32) | be32_to_cpu(q->a_bcnt_lo);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* Congestion Groups */
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/*
948c2ecf20Sopenharmony_ci * This wrapper represents a bit-array for the state of the 256 QMan congestion
958c2ecf20Sopenharmony_ci * groups. Is also used as a *mask* for congestion groups, eg. so we ignore
968c2ecf20Sopenharmony_ci * those that don't concern us. We harness the structure and accessor details
978c2ecf20Sopenharmony_ci * already used in the management command to query congestion groups.
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_ci#define CGR_BITS_PER_WORD 5
1008c2ecf20Sopenharmony_ci#define CGR_WORD(x)	((x) >> CGR_BITS_PER_WORD)
1018c2ecf20Sopenharmony_ci#define CGR_BIT(x)	(BIT(31) >> ((x) & 0x1f))
1028c2ecf20Sopenharmony_ci#define CGR_NUM	(sizeof(struct __qm_mcr_querycongestion) << 3)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistruct qman_cgrs {
1058c2ecf20Sopenharmony_ci	struct __qm_mcr_querycongestion q;
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline void qman_cgrs_init(struct qman_cgrs *c)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	memset(c, 0, sizeof(*c));
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic inline void qman_cgrs_fill(struct qman_cgrs *c)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	memset(c, 0xff, sizeof(*c));
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic inline int qman_cgrs_get(struct qman_cgrs *c, u8 cgr)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return c->q.state[CGR_WORD(cgr)] & CGR_BIT(cgr);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic inline void qman_cgrs_cp(struct qman_cgrs *dest,
1248c2ecf20Sopenharmony_ci				const struct qman_cgrs *src)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	*dest = *src;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic inline void qman_cgrs_and(struct qman_cgrs *dest,
1308c2ecf20Sopenharmony_ci			const struct qman_cgrs *a, const struct qman_cgrs *b)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	int ret;
1338c2ecf20Sopenharmony_ci	u32 *_d = dest->q.state;
1348c2ecf20Sopenharmony_ci	const u32 *_a = a->q.state;
1358c2ecf20Sopenharmony_ci	const u32 *_b = b->q.state;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	for (ret = 0; ret < 8; ret++)
1388c2ecf20Sopenharmony_ci		*_d++ = *_a++ & *_b++;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic inline void qman_cgrs_xor(struct qman_cgrs *dest,
1428c2ecf20Sopenharmony_ci			const struct qman_cgrs *a, const struct qman_cgrs *b)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	int ret;
1458c2ecf20Sopenharmony_ci	u32 *_d = dest->q.state;
1468c2ecf20Sopenharmony_ci	const u32 *_a = a->q.state;
1478c2ecf20Sopenharmony_ci	const u32 *_b = b->q.state;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	for (ret = 0; ret < 8; ret++)
1508c2ecf20Sopenharmony_ci		*_d++ = *_a++ ^ *_b++;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_civoid qman_init_cgr_all(void);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistruct qm_portal_config {
1568c2ecf20Sopenharmony_ci	/* Portal addresses */
1578c2ecf20Sopenharmony_ci	void *addr_virt_ce;
1588c2ecf20Sopenharmony_ci	void __iomem *addr_virt_ci;
1598c2ecf20Sopenharmony_ci	struct device *dev;
1608c2ecf20Sopenharmony_ci	struct iommu_domain *iommu_domain;
1618c2ecf20Sopenharmony_ci	/* Allow these to be joined in lists */
1628c2ecf20Sopenharmony_ci	struct list_head list;
1638c2ecf20Sopenharmony_ci	/* User-visible portal configuration settings */
1648c2ecf20Sopenharmony_ci	/* portal is affined to this cpu */
1658c2ecf20Sopenharmony_ci	int cpu;
1668c2ecf20Sopenharmony_ci	/* portal interrupt line */
1678c2ecf20Sopenharmony_ci	int irq;
1688c2ecf20Sopenharmony_ci	/*
1698c2ecf20Sopenharmony_ci	 * the portal's dedicated channel id, used initialising
1708c2ecf20Sopenharmony_ci	 * frame queues to target this portal when scheduled
1718c2ecf20Sopenharmony_ci	 */
1728c2ecf20Sopenharmony_ci	u16 channel;
1738c2ecf20Sopenharmony_ci	/*
1748c2ecf20Sopenharmony_ci	 * mask of pool channels this portal has dequeue access to
1758c2ecf20Sopenharmony_ci	 * (using QM_SDQCR_CHANNELS_POOL(n) for the bitmask)
1768c2ecf20Sopenharmony_ci	 */
1778c2ecf20Sopenharmony_ci	u32 pools;
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci/* Revision info (for errata and feature handling) */
1818c2ecf20Sopenharmony_ci#define QMAN_REV11 0x0101
1828c2ecf20Sopenharmony_ci#define QMAN_REV12 0x0102
1838c2ecf20Sopenharmony_ci#define QMAN_REV20 0x0200
1848c2ecf20Sopenharmony_ci#define QMAN_REV30 0x0300
1858c2ecf20Sopenharmony_ci#define QMAN_REV31 0x0301
1868c2ecf20Sopenharmony_ci#define QMAN_REV32 0x0302
1878c2ecf20Sopenharmony_ciextern u16 qman_ip_rev; /* 0 if uninitialised, otherwise QMAN_REVx */
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci#define QM_FQID_RANGE_START 1 /* FQID 0 reserved for internal use */
1908c2ecf20Sopenharmony_ciextern struct gen_pool *qm_fqalloc; /* FQID allocator */
1918c2ecf20Sopenharmony_ciextern struct gen_pool *qm_qpalloc; /* pool-channel allocator */
1928c2ecf20Sopenharmony_ciextern struct gen_pool *qm_cgralloc; /* CGR ID allocator */
1938c2ecf20Sopenharmony_ciu32 qm_get_pools_sdqcr(void);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ciint qman_wq_alloc(void);
1968c2ecf20Sopenharmony_ci#ifdef CONFIG_FSL_PAMU
1978c2ecf20Sopenharmony_ci#define qman_liodn_fixup __qman_liodn_fixup
1988c2ecf20Sopenharmony_ci#else
1998c2ecf20Sopenharmony_cistatic inline void qman_liodn_fixup(u16 channel)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci#endif
2038c2ecf20Sopenharmony_civoid __qman_liodn_fixup(u16 channel);
2048c2ecf20Sopenharmony_civoid qman_set_sdest(u16 channel, unsigned int cpu_idx);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistruct qman_portal *qman_create_affine_portal(
2078c2ecf20Sopenharmony_ci			const struct qm_portal_config *config,
2088c2ecf20Sopenharmony_ci			const struct qman_cgrs *cgrs);
2098c2ecf20Sopenharmony_ciconst struct qm_portal_config *qman_destroy_affine_portal(void);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/*
2128c2ecf20Sopenharmony_ci * qman_query_fq - Queries FQD fields (via h/w query command)
2138c2ecf20Sopenharmony_ci * @fq: the frame queue object to be queried
2148c2ecf20Sopenharmony_ci * @fqd: storage for the queried FQD fields
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_ciint qman_query_fq(struct qman_fq *fq, struct qm_fqd *fqd);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ciint qman_alloc_fq_table(u32 num_fqids);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/*   QMan s/w corenet portal, low-level i/face	 */
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci/*
2238c2ecf20Sopenharmony_ci * For qm_dqrr_sdqcr_set(); Choose one SOURCE. Choose one COUNT. Choose one
2248c2ecf20Sopenharmony_ci * dequeue TYPE. Choose TOKEN (8-bit).
2258c2ecf20Sopenharmony_ci * If SOURCE == CHANNELS,
2268c2ecf20Sopenharmony_ci *   Choose CHANNELS_DEDICATED and/or CHANNELS_POOL(n).
2278c2ecf20Sopenharmony_ci *   You can choose DEDICATED_PRECEDENCE if the portal channel should have
2288c2ecf20Sopenharmony_ci *   priority.
2298c2ecf20Sopenharmony_ci * If SOURCE == SPECIFICWQ,
2308c2ecf20Sopenharmony_ci *     Either select the work-queue ID with SPECIFICWQ_WQ(), or select the
2318c2ecf20Sopenharmony_ci *     channel (SPECIFICWQ_DEDICATED or SPECIFICWQ_POOL()) and specify the
2328c2ecf20Sopenharmony_ci *     work-queue priority (0-7) with SPECIFICWQ_WQ() - either way, you get the
2338c2ecf20Sopenharmony_ci *     same value.
2348c2ecf20Sopenharmony_ci */
2358c2ecf20Sopenharmony_ci#define QM_SDQCR_SOURCE_CHANNELS	0x0
2368c2ecf20Sopenharmony_ci#define QM_SDQCR_SOURCE_SPECIFICWQ	0x40000000
2378c2ecf20Sopenharmony_ci#define QM_SDQCR_COUNT_EXACT1		0x0
2388c2ecf20Sopenharmony_ci#define QM_SDQCR_COUNT_UPTO3		0x20000000
2398c2ecf20Sopenharmony_ci#define QM_SDQCR_DEDICATED_PRECEDENCE	0x10000000
2408c2ecf20Sopenharmony_ci#define QM_SDQCR_TYPE_MASK		0x03000000
2418c2ecf20Sopenharmony_ci#define QM_SDQCR_TYPE_NULL		0x0
2428c2ecf20Sopenharmony_ci#define QM_SDQCR_TYPE_PRIO_QOS		0x01000000
2438c2ecf20Sopenharmony_ci#define QM_SDQCR_TYPE_ACTIVE_QOS	0x02000000
2448c2ecf20Sopenharmony_ci#define QM_SDQCR_TYPE_ACTIVE		0x03000000
2458c2ecf20Sopenharmony_ci#define QM_SDQCR_TOKEN_MASK		0x00ff0000
2468c2ecf20Sopenharmony_ci#define QM_SDQCR_TOKEN_SET(v)		(((v) & 0xff) << 16)
2478c2ecf20Sopenharmony_ci#define QM_SDQCR_TOKEN_GET(v)		(((v) >> 16) & 0xff)
2488c2ecf20Sopenharmony_ci#define QM_SDQCR_CHANNELS_DEDICATED	0x00008000
2498c2ecf20Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_MASK	0x000000f7
2508c2ecf20Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_DEDICATED	0x00000000
2518c2ecf20Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_POOL(n)	((n) << 4)
2528c2ecf20Sopenharmony_ci#define QM_SDQCR_SPECIFICWQ_WQ(n)	(n)
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/* For qm_dqrr_vdqcr_set(): use FQID(n) to fill in the frame queue ID */
2558c2ecf20Sopenharmony_ci#define QM_VDQCR_FQID_MASK		0x00ffffff
2568c2ecf20Sopenharmony_ci#define QM_VDQCR_FQID(n)		((n) & QM_VDQCR_FQID_MASK)
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci/*
2598c2ecf20Sopenharmony_ci * Used by all portal interrupt registers except 'inhibit'
2608c2ecf20Sopenharmony_ci * Channels with frame availability
2618c2ecf20Sopenharmony_ci */
2628c2ecf20Sopenharmony_ci#define QM_PIRQ_DQAVAIL	0x0000ffff
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/* The DQAVAIL interrupt fields break down into these bits; */
2658c2ecf20Sopenharmony_ci#define QM_DQAVAIL_PORTAL	0x8000		/* Portal channel */
2668c2ecf20Sopenharmony_ci#define QM_DQAVAIL_POOL(n)	(0x8000 >> (n))	/* Pool channel, n==[1..15] */
2678c2ecf20Sopenharmony_ci#define QM_DQAVAIL_MASK		0xffff
2688c2ecf20Sopenharmony_ci/* This mask contains all the "irqsource" bits visible to API users */
2698c2ecf20Sopenharmony_ci#define QM_PIRQ_VISIBLE	(QM_PIRQ_SLOW | QM_PIRQ_DQRI)
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ciextern struct qman_portal *affine_portals[NR_CPUS];
2728c2ecf20Sopenharmony_ciextern struct qman_portal *qman_dma_portal;
2738c2ecf20Sopenharmony_ciconst struct qm_portal_config *qman_get_qm_portal_config(
2748c2ecf20Sopenharmony_ci						struct qman_portal *portal);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ciunsigned int qm_get_fqid_maxcnt(void);
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ciint qman_shutdown_fq(u32 fqid);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ciint qman_requires_cleanup(void);
2818c2ecf20Sopenharmony_civoid qman_done_cleanup(void);
2828c2ecf20Sopenharmony_civoid qman_enable_irqs(void);
283