xref: /OK3568_Linux_fs/kernel/drivers/soc/fsl/qbman/qman_test_stash.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* Copyright 2009 - 2016 Freescale Semiconductor, Inc.
2*4882a593Smuzhiyun  *
3*4882a593Smuzhiyun  * Redistribution and use in source and binary forms, with or without
4*4882a593Smuzhiyun  * modification, are permitted provided that the following conditions are met:
5*4882a593Smuzhiyun  *     * Redistributions of source code must retain the above copyright
6*4882a593Smuzhiyun  *	 notice, this list of conditions and the following disclaimer.
7*4882a593Smuzhiyun  *     * Redistributions in binary form must reproduce the above copyright
8*4882a593Smuzhiyun  *	 notice, this list of conditions and the following disclaimer in the
9*4882a593Smuzhiyun  *	 documentation and/or other materials provided with the distribution.
10*4882a593Smuzhiyun  *     * Neither the name of Freescale Semiconductor nor the
11*4882a593Smuzhiyun  *	 names of its contributors may be used to endorse or promote products
12*4882a593Smuzhiyun  *	 derived from this software without specific prior written permission.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * ALTERNATIVELY, this software may be distributed under the terms of the
15*4882a593Smuzhiyun  * GNU General Public License ("GPL") as published by the Free Software
16*4882a593Smuzhiyun  * Foundation, either version 2 of that License or (at your option) any
17*4882a593Smuzhiyun  * later version.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
20*4882a593Smuzhiyun  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21*4882a593Smuzhiyun  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22*4882a593Smuzhiyun  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
23*4882a593Smuzhiyun  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*4882a593Smuzhiyun  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*4882a593Smuzhiyun  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26*4882a593Smuzhiyun  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*4882a593Smuzhiyun  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*4882a593Smuzhiyun  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include "qman_test.h"
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <linux/dma-mapping.h>
34*4882a593Smuzhiyun #include <linux/delay.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun  * Algorithm:
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * Each cpu will have HP_PER_CPU "handlers" set up, each of which incorporates
40*4882a593Smuzhiyun  * an rx/tx pair of FQ objects (both of which are stashed on dequeue). The
41*4882a593Smuzhiyun  * organisation of FQIDs is such that the HP_PER_CPU*NUM_CPUS handlers will
42*4882a593Smuzhiyun  * shuttle a "hot potato" frame around them such that every forwarding action
43*4882a593Smuzhiyun  * moves it from one cpu to another. (The use of more than one handler per cpu
44*4882a593Smuzhiyun  * is to allow enough handlers/FQs to truly test the significance of caching -
45*4882a593Smuzhiyun  * ie. when cache-expiries are occurring.)
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  * The "hot potato" frame content will be HP_NUM_WORDS*4 bytes in size, and the
48*4882a593Smuzhiyun  * first and last words of the frame data will undergo a transformation step on
49*4882a593Smuzhiyun  * each forwarding action. To achieve this, each handler will be assigned a
50*4882a593Smuzhiyun  * 32-bit "mixer", that is produced using a 32-bit LFSR. When a frame is
51*4882a593Smuzhiyun  * received by a handler, the mixer of the expected sender is XOR'd into all
52*4882a593Smuzhiyun  * words of the entire frame, which is then validated against the original
53*4882a593Smuzhiyun  * values. Then, before forwarding, the entire frame is XOR'd with the mixer of
54*4882a593Smuzhiyun  * the current handler. Apart from validating that the frame is taking the
55*4882a593Smuzhiyun  * expected path, this also provides some quasi-realistic overheads to each
56*4882a593Smuzhiyun  * forwarding action - dereferencing *all* the frame data, computation, and
57*4882a593Smuzhiyun  * conditional branching. There is a "special" handler designated to act as the
58*4882a593Smuzhiyun  * instigator of the test by creating an enqueuing the "hot potato" frame, and
59*4882a593Smuzhiyun  * to determine when the test has completed by counting HP_LOOPS iterations.
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * Init phases:
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * 1. prepare each cpu's 'hp_cpu' struct using on_each_cpu(,,1) and link them
64*4882a593Smuzhiyun  *    into 'hp_cpu_list'. Specifically, set processor_id, allocate HP_PER_CPU
65*4882a593Smuzhiyun  *    handlers and link-list them (but do no other handler setup).
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * 2. scan over 'hp_cpu_list' HP_PER_CPU times, the first time sets each
68*4882a593Smuzhiyun  *    hp_cpu's 'iterator' to point to its first handler. With each loop,
69*4882a593Smuzhiyun  *    allocate rx/tx FQIDs and mixer values to the hp_cpu's iterator handler
70*4882a593Smuzhiyun  *    and advance the iterator for the next loop. This includes a final fixup,
71*4882a593Smuzhiyun  *    which connects the last handler to the first (and which is why phase 2
72*4882a593Smuzhiyun  *    and 3 are separate).
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * 3. scan over 'hp_cpu_list' HP_PER_CPU times, the first time sets each
75*4882a593Smuzhiyun  *    hp_cpu's 'iterator' to point to its first handler. With each loop,
76*4882a593Smuzhiyun  *    initialise FQ objects and advance the iterator for the next loop.
77*4882a593Smuzhiyun  *    Moreover, do this initialisation on the cpu it applies to so that Rx FQ
78*4882a593Smuzhiyun  *    initialisation targets the correct cpu.
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun  * helper to run something on all cpus (can't use on_each_cpu(), as that invokes
83*4882a593Smuzhiyun  * the fn from irq context, which is too restrictive).
84*4882a593Smuzhiyun  */
85*4882a593Smuzhiyun struct bstrap {
86*4882a593Smuzhiyun 	int (*fn)(void);
87*4882a593Smuzhiyun 	atomic_t started;
88*4882a593Smuzhiyun };
bstrap_fn(void * bs)89*4882a593Smuzhiyun static int bstrap_fn(void *bs)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	struct bstrap *bstrap = bs;
92*4882a593Smuzhiyun 	int err;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	atomic_inc(&bstrap->started);
95*4882a593Smuzhiyun 	err = bstrap->fn();
96*4882a593Smuzhiyun 	if (err)
97*4882a593Smuzhiyun 		return err;
98*4882a593Smuzhiyun 	while (!kthread_should_stop())
99*4882a593Smuzhiyun 		msleep(20);
100*4882a593Smuzhiyun 	return 0;
101*4882a593Smuzhiyun }
on_all_cpus(int (* fn)(void))102*4882a593Smuzhiyun static int on_all_cpus(int (*fn)(void))
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	int cpu;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	for_each_cpu(cpu, cpu_online_mask) {
107*4882a593Smuzhiyun 		struct bstrap bstrap = {
108*4882a593Smuzhiyun 			.fn = fn,
109*4882a593Smuzhiyun 			.started = ATOMIC_INIT(0)
110*4882a593Smuzhiyun 		};
111*4882a593Smuzhiyun 		struct task_struct *k = kthread_create(bstrap_fn, &bstrap,
112*4882a593Smuzhiyun 			"hotpotato%d", cpu);
113*4882a593Smuzhiyun 		int ret;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 		if (IS_ERR(k))
116*4882a593Smuzhiyun 			return -ENOMEM;
117*4882a593Smuzhiyun 		kthread_bind(k, cpu);
118*4882a593Smuzhiyun 		wake_up_process(k);
119*4882a593Smuzhiyun 		/*
120*4882a593Smuzhiyun 		 * If we call kthread_stop() before the "wake up" has had an
121*4882a593Smuzhiyun 		 * effect, then the thread may exit with -EINTR without ever
122*4882a593Smuzhiyun 		 * running the function. So poll until it's started before
123*4882a593Smuzhiyun 		 * requesting it to stop.
124*4882a593Smuzhiyun 		 */
125*4882a593Smuzhiyun 		while (!atomic_read(&bstrap.started))
126*4882a593Smuzhiyun 			msleep(20);
127*4882a593Smuzhiyun 		ret = kthread_stop(k);
128*4882a593Smuzhiyun 		if (ret)
129*4882a593Smuzhiyun 			return ret;
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 	return 0;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun struct hp_handler {
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	/* The following data is stashed when 'rx' is dequeued; */
137*4882a593Smuzhiyun 	/* -------------- */
138*4882a593Smuzhiyun 	/* The Rx FQ, dequeues of which will stash the entire hp_handler */
139*4882a593Smuzhiyun 	struct qman_fq rx;
140*4882a593Smuzhiyun 	/* The Tx FQ we should forward to */
141*4882a593Smuzhiyun 	struct qman_fq tx;
142*4882a593Smuzhiyun 	/* The value we XOR post-dequeue, prior to validating */
143*4882a593Smuzhiyun 	u32 rx_mixer;
144*4882a593Smuzhiyun 	/* The value we XOR pre-enqueue, after validating */
145*4882a593Smuzhiyun 	u32 tx_mixer;
146*4882a593Smuzhiyun 	/* what the hotpotato address should be on dequeue */
147*4882a593Smuzhiyun 	dma_addr_t addr;
148*4882a593Smuzhiyun 	u32 *frame_ptr;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/* The following data isn't (necessarily) stashed on dequeue; */
151*4882a593Smuzhiyun 	/* -------------- */
152*4882a593Smuzhiyun 	u32 fqid_rx, fqid_tx;
153*4882a593Smuzhiyun 	/* list node for linking us into 'hp_cpu' */
154*4882a593Smuzhiyun 	struct list_head node;
155*4882a593Smuzhiyun 	/* Just to check ... */
156*4882a593Smuzhiyun 	unsigned int processor_id;
157*4882a593Smuzhiyun } ____cacheline_aligned;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun struct hp_cpu {
160*4882a593Smuzhiyun 	/* identify the cpu we run on; */
161*4882a593Smuzhiyun 	unsigned int processor_id;
162*4882a593Smuzhiyun 	/* root node for the per-cpu list of handlers */
163*4882a593Smuzhiyun 	struct list_head handlers;
164*4882a593Smuzhiyun 	/* list node for linking us into 'hp_cpu_list' */
165*4882a593Smuzhiyun 	struct list_head node;
166*4882a593Smuzhiyun 	/*
167*4882a593Smuzhiyun 	 * when repeatedly scanning 'hp_list', each time linking the n'th
168*4882a593Smuzhiyun 	 * handlers together, this is used as per-cpu iterator state
169*4882a593Smuzhiyun 	 */
170*4882a593Smuzhiyun 	struct hp_handler *iterator;
171*4882a593Smuzhiyun };
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /* Each cpu has one of these */
174*4882a593Smuzhiyun static DEFINE_PER_CPU(struct hp_cpu, hp_cpus);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /* links together the hp_cpu structs, in first-come first-serve order. */
177*4882a593Smuzhiyun static LIST_HEAD(hp_cpu_list);
178*4882a593Smuzhiyun static DEFINE_SPINLOCK(hp_lock);
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun static unsigned int hp_cpu_list_length;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /* the "special" handler, that starts and terminates the test. */
183*4882a593Smuzhiyun static struct hp_handler *special_handler;
184*4882a593Smuzhiyun static int loop_counter;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /* handlers are allocated out of this, so they're properly aligned. */
187*4882a593Smuzhiyun static struct kmem_cache *hp_handler_slab;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /* this is the frame data */
190*4882a593Smuzhiyun static void *__frame_ptr;
191*4882a593Smuzhiyun static u32 *frame_ptr;
192*4882a593Smuzhiyun static dma_addr_t frame_dma;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun /* needed for dma_map*() */
195*4882a593Smuzhiyun static const struct qm_portal_config *pcfg;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun /* the main function waits on this */
198*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(queue);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun #define HP_PER_CPU	2
201*4882a593Smuzhiyun #define HP_LOOPS	8
202*4882a593Smuzhiyun /* 80 bytes, like a small ethernet frame, and bleeds into a second cacheline */
203*4882a593Smuzhiyun #define HP_NUM_WORDS	80
204*4882a593Smuzhiyun /* First word of the LFSR-based frame data */
205*4882a593Smuzhiyun #define HP_FIRST_WORD	0xabbaf00d
206*4882a593Smuzhiyun 
do_lfsr(u32 prev)207*4882a593Smuzhiyun static inline u32 do_lfsr(u32 prev)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	return (prev >> 1) ^ (-(prev & 1u) & 0xd0000001u);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
allocate_frame_data(void)212*4882a593Smuzhiyun static int allocate_frame_data(void)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	u32 lfsr = HP_FIRST_WORD;
215*4882a593Smuzhiyun 	int loop;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (!qman_dma_portal) {
218*4882a593Smuzhiyun 		pr_crit("portal not available\n");
219*4882a593Smuzhiyun 		return -EIO;
220*4882a593Smuzhiyun 	}
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	pcfg = qman_get_qm_portal_config(qman_dma_portal);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	__frame_ptr = kmalloc(4 * HP_NUM_WORDS, GFP_KERNEL);
225*4882a593Smuzhiyun 	if (!__frame_ptr)
226*4882a593Smuzhiyun 		return -ENOMEM;
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	frame_ptr = PTR_ALIGN(__frame_ptr, 64);
229*4882a593Smuzhiyun 	for (loop = 0; loop < HP_NUM_WORDS; loop++) {
230*4882a593Smuzhiyun 		frame_ptr[loop] = lfsr;
231*4882a593Smuzhiyun 		lfsr = do_lfsr(lfsr);
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	frame_dma = dma_map_single(pcfg->dev, frame_ptr, 4 * HP_NUM_WORDS,
235*4882a593Smuzhiyun 				   DMA_BIDIRECTIONAL);
236*4882a593Smuzhiyun 	if (dma_mapping_error(pcfg->dev, frame_dma)) {
237*4882a593Smuzhiyun 		pr_crit("dma mapping failure\n");
238*4882a593Smuzhiyun 		kfree(__frame_ptr);
239*4882a593Smuzhiyun 		return -EIO;
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	return 0;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
deallocate_frame_data(void)245*4882a593Smuzhiyun static void deallocate_frame_data(void)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun 	dma_unmap_single(pcfg->dev, frame_dma, 4 * HP_NUM_WORDS,
248*4882a593Smuzhiyun 			 DMA_BIDIRECTIONAL);
249*4882a593Smuzhiyun 	kfree(__frame_ptr);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
process_frame_data(struct hp_handler * handler,const struct qm_fd * fd)252*4882a593Smuzhiyun static inline int process_frame_data(struct hp_handler *handler,
253*4882a593Smuzhiyun 				     const struct qm_fd *fd)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun 	u32 *p = handler->frame_ptr;
256*4882a593Smuzhiyun 	u32 lfsr = HP_FIRST_WORD;
257*4882a593Smuzhiyun 	int loop;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	if (qm_fd_addr_get64(fd) != handler->addr) {
260*4882a593Smuzhiyun 		pr_crit("bad frame address, [%llX != %llX]\n",
261*4882a593Smuzhiyun 			qm_fd_addr_get64(fd), handler->addr);
262*4882a593Smuzhiyun 		return -EIO;
263*4882a593Smuzhiyun 	}
264*4882a593Smuzhiyun 	for (loop = 0; loop < HP_NUM_WORDS; loop++, p++) {
265*4882a593Smuzhiyun 		*p ^= handler->rx_mixer;
266*4882a593Smuzhiyun 		if (*p != lfsr) {
267*4882a593Smuzhiyun 			pr_crit("corrupt frame data");
268*4882a593Smuzhiyun 			return -EIO;
269*4882a593Smuzhiyun 		}
270*4882a593Smuzhiyun 		*p ^= handler->tx_mixer;
271*4882a593Smuzhiyun 		lfsr = do_lfsr(lfsr);
272*4882a593Smuzhiyun 	}
273*4882a593Smuzhiyun 	return 0;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
normal_dqrr(struct qman_portal * portal,struct qman_fq * fq,const struct qm_dqrr_entry * dqrr)276*4882a593Smuzhiyun static enum qman_cb_dqrr_result normal_dqrr(struct qman_portal *portal,
277*4882a593Smuzhiyun 					    struct qman_fq *fq,
278*4882a593Smuzhiyun 					    const struct qm_dqrr_entry *dqrr)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	struct hp_handler *handler = (struct hp_handler *)fq;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	if (process_frame_data(handler, &dqrr->fd)) {
283*4882a593Smuzhiyun 		WARN_ON(1);
284*4882a593Smuzhiyun 		goto skip;
285*4882a593Smuzhiyun 	}
286*4882a593Smuzhiyun 	if (qman_enqueue(&handler->tx, &dqrr->fd)) {
287*4882a593Smuzhiyun 		pr_crit("qman_enqueue() failed");
288*4882a593Smuzhiyun 		WARN_ON(1);
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun skip:
291*4882a593Smuzhiyun 	return qman_cb_dqrr_consume;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
special_dqrr(struct qman_portal * portal,struct qman_fq * fq,const struct qm_dqrr_entry * dqrr)294*4882a593Smuzhiyun static enum qman_cb_dqrr_result special_dqrr(struct qman_portal *portal,
295*4882a593Smuzhiyun 					     struct qman_fq *fq,
296*4882a593Smuzhiyun 					     const struct qm_dqrr_entry *dqrr)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun 	struct hp_handler *handler = (struct hp_handler *)fq;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	process_frame_data(handler, &dqrr->fd);
301*4882a593Smuzhiyun 	if (++loop_counter < HP_LOOPS) {
302*4882a593Smuzhiyun 		if (qman_enqueue(&handler->tx, &dqrr->fd)) {
303*4882a593Smuzhiyun 			pr_crit("qman_enqueue() failed");
304*4882a593Smuzhiyun 			WARN_ON(1);
305*4882a593Smuzhiyun 			goto skip;
306*4882a593Smuzhiyun 		}
307*4882a593Smuzhiyun 	} else {
308*4882a593Smuzhiyun 		pr_info("Received final (%dth) frame\n", loop_counter);
309*4882a593Smuzhiyun 		wake_up(&queue);
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun skip:
312*4882a593Smuzhiyun 	return qman_cb_dqrr_consume;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
create_per_cpu_handlers(void)315*4882a593Smuzhiyun static int create_per_cpu_handlers(void)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun 	struct hp_handler *handler;
318*4882a593Smuzhiyun 	int loop;
319*4882a593Smuzhiyun 	struct hp_cpu *hp_cpu = this_cpu_ptr(&hp_cpus);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	hp_cpu->processor_id = smp_processor_id();
322*4882a593Smuzhiyun 	spin_lock(&hp_lock);
323*4882a593Smuzhiyun 	list_add_tail(&hp_cpu->node, &hp_cpu_list);
324*4882a593Smuzhiyun 	hp_cpu_list_length++;
325*4882a593Smuzhiyun 	spin_unlock(&hp_lock);
326*4882a593Smuzhiyun 	INIT_LIST_HEAD(&hp_cpu->handlers);
327*4882a593Smuzhiyun 	for (loop = 0; loop < HP_PER_CPU; loop++) {
328*4882a593Smuzhiyun 		handler = kmem_cache_alloc(hp_handler_slab, GFP_KERNEL);
329*4882a593Smuzhiyun 		if (!handler) {
330*4882a593Smuzhiyun 			pr_crit("kmem_cache_alloc() failed");
331*4882a593Smuzhiyun 			WARN_ON(1);
332*4882a593Smuzhiyun 			return -EIO;
333*4882a593Smuzhiyun 		}
334*4882a593Smuzhiyun 		handler->processor_id = hp_cpu->processor_id;
335*4882a593Smuzhiyun 		handler->addr = frame_dma;
336*4882a593Smuzhiyun 		handler->frame_ptr = frame_ptr;
337*4882a593Smuzhiyun 		list_add_tail(&handler->node, &hp_cpu->handlers);
338*4882a593Smuzhiyun 	}
339*4882a593Smuzhiyun 	return 0;
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun 
destroy_per_cpu_handlers(void)342*4882a593Smuzhiyun static int destroy_per_cpu_handlers(void)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	struct list_head *loop, *tmp;
345*4882a593Smuzhiyun 	struct hp_cpu *hp_cpu = this_cpu_ptr(&hp_cpus);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	spin_lock(&hp_lock);
348*4882a593Smuzhiyun 	list_del(&hp_cpu->node);
349*4882a593Smuzhiyun 	spin_unlock(&hp_lock);
350*4882a593Smuzhiyun 	list_for_each_safe(loop, tmp, &hp_cpu->handlers) {
351*4882a593Smuzhiyun 		u32 flags = 0;
352*4882a593Smuzhiyun 		struct hp_handler *handler = list_entry(loop, struct hp_handler,
353*4882a593Smuzhiyun 							node);
354*4882a593Smuzhiyun 		if (qman_retire_fq(&handler->rx, &flags) ||
355*4882a593Smuzhiyun 		    (flags & QMAN_FQ_STATE_BLOCKOOS)) {
356*4882a593Smuzhiyun 			pr_crit("qman_retire_fq(rx) failed, flags: %x", flags);
357*4882a593Smuzhiyun 			WARN_ON(1);
358*4882a593Smuzhiyun 			return -EIO;
359*4882a593Smuzhiyun 		}
360*4882a593Smuzhiyun 		if (qman_oos_fq(&handler->rx)) {
361*4882a593Smuzhiyun 			pr_crit("qman_oos_fq(rx) failed");
362*4882a593Smuzhiyun 			WARN_ON(1);
363*4882a593Smuzhiyun 			return -EIO;
364*4882a593Smuzhiyun 		}
365*4882a593Smuzhiyun 		qman_destroy_fq(&handler->rx);
366*4882a593Smuzhiyun 		qman_destroy_fq(&handler->tx);
367*4882a593Smuzhiyun 		qman_release_fqid(handler->fqid_rx);
368*4882a593Smuzhiyun 		list_del(&handler->node);
369*4882a593Smuzhiyun 		kmem_cache_free(hp_handler_slab, handler);
370*4882a593Smuzhiyun 	}
371*4882a593Smuzhiyun 	return 0;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun 
num_cachelines(u32 offset)374*4882a593Smuzhiyun static inline u8 num_cachelines(u32 offset)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun 	u8 res = (offset + (L1_CACHE_BYTES - 1))
377*4882a593Smuzhiyun 			 / (L1_CACHE_BYTES);
378*4882a593Smuzhiyun 	if (res > 3)
379*4882a593Smuzhiyun 		return 3;
380*4882a593Smuzhiyun 	return res;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun #define STASH_DATA_CL \
383*4882a593Smuzhiyun 	num_cachelines(HP_NUM_WORDS * 4)
384*4882a593Smuzhiyun #define STASH_CTX_CL \
385*4882a593Smuzhiyun 	num_cachelines(offsetof(struct hp_handler, fqid_rx))
386*4882a593Smuzhiyun 
init_handler(void * h)387*4882a593Smuzhiyun static int init_handler(void *h)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	struct qm_mcc_initfq opts;
390*4882a593Smuzhiyun 	struct hp_handler *handler = h;
391*4882a593Smuzhiyun 	int err;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	if (handler->processor_id != smp_processor_id()) {
394*4882a593Smuzhiyun 		err = -EIO;
395*4882a593Smuzhiyun 		goto failed;
396*4882a593Smuzhiyun 	}
397*4882a593Smuzhiyun 	/* Set up rx */
398*4882a593Smuzhiyun 	memset(&handler->rx, 0, sizeof(handler->rx));
399*4882a593Smuzhiyun 	if (handler == special_handler)
400*4882a593Smuzhiyun 		handler->rx.cb.dqrr = special_dqrr;
401*4882a593Smuzhiyun 	else
402*4882a593Smuzhiyun 		handler->rx.cb.dqrr = normal_dqrr;
403*4882a593Smuzhiyun 	err = qman_create_fq(handler->fqid_rx, 0, &handler->rx);
404*4882a593Smuzhiyun 	if (err) {
405*4882a593Smuzhiyun 		pr_crit("qman_create_fq(rx) failed");
406*4882a593Smuzhiyun 		goto failed;
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 	memset(&opts, 0, sizeof(opts));
409*4882a593Smuzhiyun 	opts.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL |
410*4882a593Smuzhiyun 				   QM_INITFQ_WE_CONTEXTA);
411*4882a593Smuzhiyun 	opts.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_CTXASTASHING);
412*4882a593Smuzhiyun 	qm_fqd_set_stashing(&opts.fqd, 0, STASH_DATA_CL, STASH_CTX_CL);
413*4882a593Smuzhiyun 	err = qman_init_fq(&handler->rx, QMAN_INITFQ_FLAG_SCHED |
414*4882a593Smuzhiyun 			   QMAN_INITFQ_FLAG_LOCAL, &opts);
415*4882a593Smuzhiyun 	if (err) {
416*4882a593Smuzhiyun 		pr_crit("qman_init_fq(rx) failed");
417*4882a593Smuzhiyun 		goto failed;
418*4882a593Smuzhiyun 	}
419*4882a593Smuzhiyun 	/* Set up tx */
420*4882a593Smuzhiyun 	memset(&handler->tx, 0, sizeof(handler->tx));
421*4882a593Smuzhiyun 	err = qman_create_fq(handler->fqid_tx, QMAN_FQ_FLAG_NO_MODIFY,
422*4882a593Smuzhiyun 			     &handler->tx);
423*4882a593Smuzhiyun 	if (err) {
424*4882a593Smuzhiyun 		pr_crit("qman_create_fq(tx) failed");
425*4882a593Smuzhiyun 		goto failed;
426*4882a593Smuzhiyun 	}
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	return 0;
429*4882a593Smuzhiyun failed:
430*4882a593Smuzhiyun 	return err;
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
init_handler_cb(void * h)433*4882a593Smuzhiyun static void init_handler_cb(void *h)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun 	if (init_handler(h))
436*4882a593Smuzhiyun 		WARN_ON(1);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
init_phase2(void)439*4882a593Smuzhiyun static int init_phase2(void)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	int loop;
442*4882a593Smuzhiyun 	u32 fqid = 0;
443*4882a593Smuzhiyun 	u32 lfsr = 0xdeadbeef;
444*4882a593Smuzhiyun 	struct hp_cpu *hp_cpu;
445*4882a593Smuzhiyun 	struct hp_handler *handler;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	for (loop = 0; loop < HP_PER_CPU; loop++) {
448*4882a593Smuzhiyun 		list_for_each_entry(hp_cpu, &hp_cpu_list, node) {
449*4882a593Smuzhiyun 			int err;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 			if (!loop)
452*4882a593Smuzhiyun 				hp_cpu->iterator = list_first_entry(
453*4882a593Smuzhiyun 						&hp_cpu->handlers,
454*4882a593Smuzhiyun 						struct hp_handler, node);
455*4882a593Smuzhiyun 			else
456*4882a593Smuzhiyun 				hp_cpu->iterator = list_entry(
457*4882a593Smuzhiyun 						hp_cpu->iterator->node.next,
458*4882a593Smuzhiyun 						struct hp_handler, node);
459*4882a593Smuzhiyun 			/* Rx FQID is the previous handler's Tx FQID */
460*4882a593Smuzhiyun 			hp_cpu->iterator->fqid_rx = fqid;
461*4882a593Smuzhiyun 			/* Allocate new FQID for Tx */
462*4882a593Smuzhiyun 			err = qman_alloc_fqid(&fqid);
463*4882a593Smuzhiyun 			if (err) {
464*4882a593Smuzhiyun 				pr_crit("qman_alloc_fqid() failed");
465*4882a593Smuzhiyun 				return err;
466*4882a593Smuzhiyun 			}
467*4882a593Smuzhiyun 			hp_cpu->iterator->fqid_tx = fqid;
468*4882a593Smuzhiyun 			/* Rx mixer is the previous handler's Tx mixer */
469*4882a593Smuzhiyun 			hp_cpu->iterator->rx_mixer = lfsr;
470*4882a593Smuzhiyun 			/* Get new mixer for Tx */
471*4882a593Smuzhiyun 			lfsr = do_lfsr(lfsr);
472*4882a593Smuzhiyun 			hp_cpu->iterator->tx_mixer = lfsr;
473*4882a593Smuzhiyun 		}
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 	/* Fix up the first handler (fqid_rx==0, rx_mixer=0xdeadbeef) */
476*4882a593Smuzhiyun 	hp_cpu = list_first_entry(&hp_cpu_list, struct hp_cpu, node);
477*4882a593Smuzhiyun 	handler = list_first_entry(&hp_cpu->handlers, struct hp_handler, node);
478*4882a593Smuzhiyun 	if (handler->fqid_rx != 0 || handler->rx_mixer != 0xdeadbeef)
479*4882a593Smuzhiyun 		return 1;
480*4882a593Smuzhiyun 	handler->fqid_rx = fqid;
481*4882a593Smuzhiyun 	handler->rx_mixer = lfsr;
482*4882a593Smuzhiyun 	/* and tag it as our "special" handler */
483*4882a593Smuzhiyun 	special_handler = handler;
484*4882a593Smuzhiyun 	return 0;
485*4882a593Smuzhiyun }
486*4882a593Smuzhiyun 
init_phase3(void)487*4882a593Smuzhiyun static int init_phase3(void)
488*4882a593Smuzhiyun {
489*4882a593Smuzhiyun 	int loop, err;
490*4882a593Smuzhiyun 	struct hp_cpu *hp_cpu;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 	for (loop = 0; loop < HP_PER_CPU; loop++) {
493*4882a593Smuzhiyun 		list_for_each_entry(hp_cpu, &hp_cpu_list, node) {
494*4882a593Smuzhiyun 			if (!loop)
495*4882a593Smuzhiyun 				hp_cpu->iterator = list_first_entry(
496*4882a593Smuzhiyun 						&hp_cpu->handlers,
497*4882a593Smuzhiyun 						struct hp_handler, node);
498*4882a593Smuzhiyun 			else
499*4882a593Smuzhiyun 				hp_cpu->iterator = list_entry(
500*4882a593Smuzhiyun 						hp_cpu->iterator->node.next,
501*4882a593Smuzhiyun 						struct hp_handler, node);
502*4882a593Smuzhiyun 			preempt_disable();
503*4882a593Smuzhiyun 			if (hp_cpu->processor_id == smp_processor_id()) {
504*4882a593Smuzhiyun 				err = init_handler(hp_cpu->iterator);
505*4882a593Smuzhiyun 				if (err)
506*4882a593Smuzhiyun 					return err;
507*4882a593Smuzhiyun 			} else {
508*4882a593Smuzhiyun 				smp_call_function_single(hp_cpu->processor_id,
509*4882a593Smuzhiyun 					init_handler_cb, hp_cpu->iterator, 1);
510*4882a593Smuzhiyun 			}
511*4882a593Smuzhiyun 			preempt_enable();
512*4882a593Smuzhiyun 		}
513*4882a593Smuzhiyun 	}
514*4882a593Smuzhiyun 	return 0;
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun 
send_first_frame(void * ignore)517*4882a593Smuzhiyun static int send_first_frame(void *ignore)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	u32 *p = special_handler->frame_ptr;
520*4882a593Smuzhiyun 	u32 lfsr = HP_FIRST_WORD;
521*4882a593Smuzhiyun 	int loop, err;
522*4882a593Smuzhiyun 	struct qm_fd fd;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	if (special_handler->processor_id != smp_processor_id()) {
525*4882a593Smuzhiyun 		err = -EIO;
526*4882a593Smuzhiyun 		goto failed;
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun 	memset(&fd, 0, sizeof(fd));
529*4882a593Smuzhiyun 	qm_fd_addr_set64(&fd, special_handler->addr);
530*4882a593Smuzhiyun 	qm_fd_set_contig_big(&fd, HP_NUM_WORDS * 4);
531*4882a593Smuzhiyun 	for (loop = 0; loop < HP_NUM_WORDS; loop++, p++) {
532*4882a593Smuzhiyun 		if (*p != lfsr) {
533*4882a593Smuzhiyun 			err = -EIO;
534*4882a593Smuzhiyun 			pr_crit("corrupt frame data");
535*4882a593Smuzhiyun 			goto failed;
536*4882a593Smuzhiyun 		}
537*4882a593Smuzhiyun 		*p ^= special_handler->tx_mixer;
538*4882a593Smuzhiyun 		lfsr = do_lfsr(lfsr);
539*4882a593Smuzhiyun 	}
540*4882a593Smuzhiyun 	pr_info("Sending first frame\n");
541*4882a593Smuzhiyun 	err = qman_enqueue(&special_handler->tx, &fd);
542*4882a593Smuzhiyun 	if (err) {
543*4882a593Smuzhiyun 		pr_crit("qman_enqueue() failed");
544*4882a593Smuzhiyun 		goto failed;
545*4882a593Smuzhiyun 	}
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	return 0;
548*4882a593Smuzhiyun failed:
549*4882a593Smuzhiyun 	return err;
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
send_first_frame_cb(void * ignore)552*4882a593Smuzhiyun static void send_first_frame_cb(void *ignore)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun 	if (send_first_frame(NULL))
555*4882a593Smuzhiyun 		WARN_ON(1);
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun 
qman_test_stash(void)558*4882a593Smuzhiyun int qman_test_stash(void)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	int err;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	if (cpumask_weight(cpu_online_mask) < 2) {
563*4882a593Smuzhiyun 		pr_info("%s(): skip - only 1 CPU\n", __func__);
564*4882a593Smuzhiyun 		return 0;
565*4882a593Smuzhiyun 	}
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	pr_info("%s(): Starting\n", __func__);
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	hp_cpu_list_length = 0;
570*4882a593Smuzhiyun 	loop_counter = 0;
571*4882a593Smuzhiyun 	hp_handler_slab = kmem_cache_create("hp_handler_slab",
572*4882a593Smuzhiyun 			sizeof(struct hp_handler), L1_CACHE_BYTES,
573*4882a593Smuzhiyun 			SLAB_HWCACHE_ALIGN, NULL);
574*4882a593Smuzhiyun 	if (!hp_handler_slab) {
575*4882a593Smuzhiyun 		err = -EIO;
576*4882a593Smuzhiyun 		pr_crit("kmem_cache_create() failed");
577*4882a593Smuzhiyun 		goto failed;
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	err = allocate_frame_data();
581*4882a593Smuzhiyun 	if (err)
582*4882a593Smuzhiyun 		goto failed;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	/* Init phase 1 */
585*4882a593Smuzhiyun 	pr_info("Creating %d handlers per cpu...\n", HP_PER_CPU);
586*4882a593Smuzhiyun 	if (on_all_cpus(create_per_cpu_handlers)) {
587*4882a593Smuzhiyun 		err = -EIO;
588*4882a593Smuzhiyun 		pr_crit("on_each_cpu() failed");
589*4882a593Smuzhiyun 		goto failed;
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 	pr_info("Number of cpus: %d, total of %d handlers\n",
592*4882a593Smuzhiyun 		hp_cpu_list_length, hp_cpu_list_length * HP_PER_CPU);
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	err = init_phase2();
595*4882a593Smuzhiyun 	if (err)
596*4882a593Smuzhiyun 		goto failed;
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	err = init_phase3();
599*4882a593Smuzhiyun 	if (err)
600*4882a593Smuzhiyun 		goto failed;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	preempt_disable();
603*4882a593Smuzhiyun 	if (special_handler->processor_id == smp_processor_id()) {
604*4882a593Smuzhiyun 		err = send_first_frame(NULL);
605*4882a593Smuzhiyun 		if (err)
606*4882a593Smuzhiyun 			goto failed;
607*4882a593Smuzhiyun 	} else {
608*4882a593Smuzhiyun 		smp_call_function_single(special_handler->processor_id,
609*4882a593Smuzhiyun 					 send_first_frame_cb, NULL, 1);
610*4882a593Smuzhiyun 	}
611*4882a593Smuzhiyun 	preempt_enable();
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	wait_event(queue, loop_counter == HP_LOOPS);
614*4882a593Smuzhiyun 	deallocate_frame_data();
615*4882a593Smuzhiyun 	if (on_all_cpus(destroy_per_cpu_handlers)) {
616*4882a593Smuzhiyun 		err = -EIO;
617*4882a593Smuzhiyun 		pr_crit("on_each_cpu() failed");
618*4882a593Smuzhiyun 		goto failed;
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 	kmem_cache_destroy(hp_handler_slab);
621*4882a593Smuzhiyun 	pr_info("%s(): Finished\n", __func__);
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	return 0;
624*4882a593Smuzhiyun failed:
625*4882a593Smuzhiyun 	WARN_ON(1);
626*4882a593Smuzhiyun 	return err;
627*4882a593Smuzhiyun }
628