1 /* Copyright 2009 - 2016 Freescale Semiconductor, Inc.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * * Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * * Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
10 * * Neither the name of Freescale Semiconductor nor the
11 * names of its contributors may be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * ALTERNATIVELY, this software may be distributed under the terms of the
15 * GNU General Public License ("GPL") as published by the Free Software
16 * Foundation, either version 2 of that License or (at your option) any
19 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "qman_test.h"
33 #include <linux/dma-mapping.h>
34 #include <linux/delay.h>
39 * Each cpu will have HP_PER_CPU "handlers" set up, each of which incorporates
40 * an rx/tx pair of FQ objects (both of which are stashed on dequeue). The
41 * organisation of FQIDs is such that the HP_PER_CPU*NUM_CPUS handlers will
42 * shuttle a "hot potato" frame around them such that every forwarding action
43 * moves it from one cpu to another. (The use of more than one handler per cpu
44 * is to allow enough handlers/FQs to truly test the significance of caching -
45 * ie. when cache-expiries are occurring.)
47 * The "hot potato" frame content will be HP_NUM_WORDS*4 bytes in size, and the
48 * first and last words of the frame data will undergo a transformation step on
49 * each forwarding action. To achieve this, each handler will be assigned a
50 * 32-bit "mixer", that is produced using a 32-bit LFSR. When a frame is
51 * received by a handler, the mixer of the expected sender is XOR'd into all
52 * words of the entire frame, which is then validated against the original
53 * values. Then, before forwarding, the entire frame is XOR'd with the mixer of
54 * the current handler. Apart from validating that the frame is taking the
55 * expected path, this also provides some quasi-realistic overheads to each
56 * forwarding action - dereferencing *all* the frame data, computation, and
57 * conditional branching. There is a "special" handler designated to act as the
58 * instigator of the test by creating an enqueuing the "hot potato" frame, and
59 * to determine when the test has completed by counting HP_LOOPS iterations.
63 * 1. prepare each cpu's 'hp_cpu' struct using on_each_cpu(,,1) and link them
64 * into 'hp_cpu_list'. Specifically, set processor_id, allocate HP_PER_CPU
65 * handlers and link-list them (but do no other handler setup).
67 * 2. scan over 'hp_cpu_list' HP_PER_CPU times, the first time sets each
68 * hp_cpu's 'iterator' to point to its first handler. With each loop,
69 * allocate rx/tx FQIDs and mixer values to the hp_cpu's iterator handler
70 * and advance the iterator for the next loop. This includes a final fixup,
71 * which connects the last handler to the first (and which is why phase 2
72 * and 3 are separate).
74 * 3. scan over 'hp_cpu_list' HP_PER_CPU times, the first time sets each
75 * hp_cpu's 'iterator' to point to its first handler. With each loop,
76 * initialise FQ objects and advance the iterator for the next loop.
77 * Moreover, do this initialisation on the cpu it applies to so that Rx FQ
78 * initialisation targets the correct cpu.
82 * helper to run something on all cpus (can't use on_each_cpu(), as that invokes
83 * the fn from irq context, which is too restrictive).
89 static int bstrap_fn(void *bs)
91 struct bstrap *bstrap = bs;
94 atomic_inc(&bstrap->started);
98 while (!kthread_should_stop())
102 static int on_all_cpus(int (*fn)(void))
106 for_each_cpu(cpu, cpu_online_mask) {
107 struct bstrap bstrap = {
109 .started = ATOMIC_INIT(0)
111 struct task_struct *k = kthread_create(bstrap_fn, &bstrap,
117 kthread_bind(k, cpu);
120 * If we call kthread_stop() before the "wake up" has had an
121 * effect, then the thread may exit with -EINTR without ever
122 * running the function. So poll until it's started before
123 * requesting it to stop.
125 while (!atomic_read(&bstrap.started))
127 ret = kthread_stop(k);
136 /* The following data is stashed when 'rx' is dequeued; */
138 /* The Rx FQ, dequeues of which will stash the entire hp_handler */
140 /* The Tx FQ we should forward to */
142 /* The value we XOR post-dequeue, prior to validating */
144 /* The value we XOR pre-enqueue, after validating */
146 /* what the hotpotato address should be on dequeue */
150 /* The following data isn't (necessarily) stashed on dequeue; */
152 u32 fqid_rx, fqid_tx;
153 /* list node for linking us into 'hp_cpu' */
154 struct list_head node;
155 /* Just to check ... */
156 unsigned int processor_id;
157 } ____cacheline_aligned;
160 /* identify the cpu we run on; */
161 unsigned int processor_id;
162 /* root node for the per-cpu list of handlers */
163 struct list_head handlers;
164 /* list node for linking us into 'hp_cpu_list' */
165 struct list_head node;
167 * when repeatedly scanning 'hp_list', each time linking the n'th
168 * handlers together, this is used as per-cpu iterator state
170 struct hp_handler *iterator;
173 /* Each cpu has one of these */
174 static DEFINE_PER_CPU(struct hp_cpu, hp_cpus);
176 /* links together the hp_cpu structs, in first-come first-serve order. */
177 static LIST_HEAD(hp_cpu_list);
178 static spinlock_t hp_lock = __SPIN_LOCK_UNLOCKED(hp_lock);
180 static unsigned int hp_cpu_list_length;
182 /* the "special" handler, that starts and terminates the test. */
183 static struct hp_handler *special_handler;
184 static int loop_counter;
186 /* handlers are allocated out of this, so they're properly aligned. */
187 static struct kmem_cache *hp_handler_slab;
189 /* this is the frame data */
190 static void *__frame_ptr;
191 static u32 *frame_ptr;
192 static dma_addr_t frame_dma;
194 /* the main function waits on this */
195 static DECLARE_WAIT_QUEUE_HEAD(queue);
199 /* 80 bytes, like a small ethernet frame, and bleeds into a second cacheline */
200 #define HP_NUM_WORDS 80
201 /* First word of the LFSR-based frame data */
202 #define HP_FIRST_WORD 0xabbaf00d
204 static inline u32 do_lfsr(u32 prev)
206 return (prev >> 1) ^ (-(prev & 1u) & 0xd0000001u);
209 static int allocate_frame_data(void)
211 u32 lfsr = HP_FIRST_WORD;
213 struct platform_device *pdev = platform_device_alloc("foobar", -1);
216 pr_crit("platform_device_alloc() failed");
219 if (platform_device_add(pdev)) {
220 pr_crit("platform_device_add() failed");
223 __frame_ptr = kmalloc(4 * HP_NUM_WORDS, GFP_KERNEL);
227 frame_ptr = PTR_ALIGN(__frame_ptr, 64);
228 for (loop = 0; loop < HP_NUM_WORDS; loop++) {
229 frame_ptr[loop] = lfsr;
230 lfsr = do_lfsr(lfsr);
232 frame_dma = dma_map_single(&pdev->dev, frame_ptr, 4 * HP_NUM_WORDS,
234 platform_device_del(pdev);
235 platform_device_put(pdev);
239 static void deallocate_frame_data(void)
244 static inline int process_frame_data(struct hp_handler *handler,
245 const struct qm_fd *fd)
247 u32 *p = handler->frame_ptr;
248 u32 lfsr = HP_FIRST_WORD;
251 if (qm_fd_addr_get64(fd) != handler->addr) {
252 pr_crit("bad frame address");
255 for (loop = 0; loop < HP_NUM_WORDS; loop++, p++) {
256 *p ^= handler->rx_mixer;
258 pr_crit("corrupt frame data");
261 *p ^= handler->tx_mixer;
262 lfsr = do_lfsr(lfsr);
267 static enum qman_cb_dqrr_result normal_dqrr(struct qman_portal *portal,
269 const struct qm_dqrr_entry *dqrr)
271 struct hp_handler *handler = (struct hp_handler *)fq;
273 if (process_frame_data(handler, &dqrr->fd)) {
277 if (qman_enqueue(&handler->tx, &dqrr->fd)) {
278 pr_crit("qman_enqueue() failed");
282 return qman_cb_dqrr_consume;
285 static enum qman_cb_dqrr_result special_dqrr(struct qman_portal *portal,
287 const struct qm_dqrr_entry *dqrr)
289 struct hp_handler *handler = (struct hp_handler *)fq;
291 process_frame_data(handler, &dqrr->fd);
292 if (++loop_counter < HP_LOOPS) {
293 if (qman_enqueue(&handler->tx, &dqrr->fd)) {
294 pr_crit("qman_enqueue() failed");
299 pr_info("Received final (%dth) frame\n", loop_counter);
303 return qman_cb_dqrr_consume;
306 static int create_per_cpu_handlers(void)
308 struct hp_handler *handler;
310 struct hp_cpu *hp_cpu = this_cpu_ptr(&hp_cpus);
312 hp_cpu->processor_id = smp_processor_id();
314 list_add_tail(&hp_cpu->node, &hp_cpu_list);
315 hp_cpu_list_length++;
316 spin_unlock(&hp_lock);
317 INIT_LIST_HEAD(&hp_cpu->handlers);
318 for (loop = 0; loop < HP_PER_CPU; loop++) {
319 handler = kmem_cache_alloc(hp_handler_slab, GFP_KERNEL);
321 pr_crit("kmem_cache_alloc() failed");
325 handler->processor_id = hp_cpu->processor_id;
326 handler->addr = frame_dma;
327 handler->frame_ptr = frame_ptr;
328 list_add_tail(&handler->node, &hp_cpu->handlers);
333 static int destroy_per_cpu_handlers(void)
335 struct list_head *loop, *tmp;
336 struct hp_cpu *hp_cpu = this_cpu_ptr(&hp_cpus);
339 list_del(&hp_cpu->node);
340 spin_unlock(&hp_lock);
341 list_for_each_safe(loop, tmp, &hp_cpu->handlers) {
343 struct hp_handler *handler = list_entry(loop, struct hp_handler,
345 if (qman_retire_fq(&handler->rx, &flags) ||
346 (flags & QMAN_FQ_STATE_BLOCKOOS)) {
347 pr_crit("qman_retire_fq(rx) failed, flags: %x", flags);
351 if (qman_oos_fq(&handler->rx)) {
352 pr_crit("qman_oos_fq(rx) failed");
356 qman_destroy_fq(&handler->rx);
357 qman_destroy_fq(&handler->tx);
358 qman_release_fqid(handler->fqid_rx);
359 list_del(&handler->node);
360 kmem_cache_free(hp_handler_slab, handler);
365 static inline u8 num_cachelines(u32 offset)
367 u8 res = (offset + (L1_CACHE_BYTES - 1))
373 #define STASH_DATA_CL \
374 num_cachelines(HP_NUM_WORDS * 4)
375 #define STASH_CTX_CL \
376 num_cachelines(offsetof(struct hp_handler, fqid_rx))
378 static int init_handler(void *h)
380 struct qm_mcc_initfq opts;
381 struct hp_handler *handler = h;
384 if (handler->processor_id != smp_processor_id()) {
389 memset(&handler->rx, 0, sizeof(handler->rx));
390 if (handler == special_handler)
391 handler->rx.cb.dqrr = special_dqrr;
393 handler->rx.cb.dqrr = normal_dqrr;
394 err = qman_create_fq(handler->fqid_rx, 0, &handler->rx);
396 pr_crit("qman_create_fq(rx) failed");
399 memset(&opts, 0, sizeof(opts));
400 opts.we_mask = QM_INITFQ_WE_FQCTRL | QM_INITFQ_WE_CONTEXTA;
401 opts.fqd.fq_ctrl = QM_FQCTRL_CTXASTASHING;
402 qm_fqd_set_stashing(&opts.fqd, 0, STASH_DATA_CL, STASH_CTX_CL);
403 err = qman_init_fq(&handler->rx, QMAN_INITFQ_FLAG_SCHED |
404 QMAN_INITFQ_FLAG_LOCAL, &opts);
406 pr_crit("qman_init_fq(rx) failed");
410 memset(&handler->tx, 0, sizeof(handler->tx));
411 err = qman_create_fq(handler->fqid_tx, QMAN_FQ_FLAG_NO_MODIFY,
414 pr_crit("qman_create_fq(tx) failed");
423 static void init_handler_cb(void *h)
429 static int init_phase2(void)
433 u32 lfsr = 0xdeadbeef;
434 struct hp_cpu *hp_cpu;
435 struct hp_handler *handler;
437 for (loop = 0; loop < HP_PER_CPU; loop++) {
438 list_for_each_entry(hp_cpu, &hp_cpu_list, node) {
442 hp_cpu->iterator = list_first_entry(
444 struct hp_handler, node);
446 hp_cpu->iterator = list_entry(
447 hp_cpu->iterator->node.next,
448 struct hp_handler, node);
449 /* Rx FQID is the previous handler's Tx FQID */
450 hp_cpu->iterator->fqid_rx = fqid;
451 /* Allocate new FQID for Tx */
452 err = qman_alloc_fqid(&fqid);
454 pr_crit("qman_alloc_fqid() failed");
457 hp_cpu->iterator->fqid_tx = fqid;
458 /* Rx mixer is the previous handler's Tx mixer */
459 hp_cpu->iterator->rx_mixer = lfsr;
460 /* Get new mixer for Tx */
461 lfsr = do_lfsr(lfsr);
462 hp_cpu->iterator->tx_mixer = lfsr;
465 /* Fix up the first handler (fqid_rx==0, rx_mixer=0xdeadbeef) */
466 hp_cpu = list_first_entry(&hp_cpu_list, struct hp_cpu, node);
467 handler = list_first_entry(&hp_cpu->handlers, struct hp_handler, node);
468 if (handler->fqid_rx != 0 || handler->rx_mixer != 0xdeadbeef)
470 handler->fqid_rx = fqid;
471 handler->rx_mixer = lfsr;
472 /* and tag it as our "special" handler */
473 special_handler = handler;
477 static int init_phase3(void)
480 struct hp_cpu *hp_cpu;
482 for (loop = 0; loop < HP_PER_CPU; loop++) {
483 list_for_each_entry(hp_cpu, &hp_cpu_list, node) {
485 hp_cpu->iterator = list_first_entry(
487 struct hp_handler, node);
489 hp_cpu->iterator = list_entry(
490 hp_cpu->iterator->node.next,
491 struct hp_handler, node);
493 if (hp_cpu->processor_id == smp_processor_id()) {
494 err = init_handler(hp_cpu->iterator);
498 smp_call_function_single(hp_cpu->processor_id,
499 init_handler_cb, hp_cpu->iterator, 1);
507 static int send_first_frame(void *ignore)
509 u32 *p = special_handler->frame_ptr;
510 u32 lfsr = HP_FIRST_WORD;
514 if (special_handler->processor_id != smp_processor_id()) {
518 memset(&fd, 0, sizeof(fd));
519 qm_fd_addr_set64(&fd, special_handler->addr);
520 qm_fd_set_contig_big(&fd, HP_NUM_WORDS * 4);
521 for (loop = 0; loop < HP_NUM_WORDS; loop++, p++) {
524 pr_crit("corrupt frame data");
527 *p ^= special_handler->tx_mixer;
528 lfsr = do_lfsr(lfsr);
530 pr_info("Sending first frame\n");
531 err = qman_enqueue(&special_handler->tx, &fd);
533 pr_crit("qman_enqueue() failed");
542 static void send_first_frame_cb(void *ignore)
544 if (send_first_frame(NULL))
548 int qman_test_stash(void)
552 if (cpumask_weight(cpu_online_mask) < 2) {
553 pr_info("%s(): skip - only 1 CPU\n", __func__);
557 pr_info("%s(): Starting\n", __func__);
559 hp_cpu_list_length = 0;
561 hp_handler_slab = kmem_cache_create("hp_handler_slab",
562 sizeof(struct hp_handler), L1_CACHE_BYTES,
563 SLAB_HWCACHE_ALIGN, NULL);
564 if (!hp_handler_slab) {
566 pr_crit("kmem_cache_create() failed");
570 err = allocate_frame_data();
575 pr_info("Creating %d handlers per cpu...\n", HP_PER_CPU);
576 if (on_all_cpus(create_per_cpu_handlers)) {
578 pr_crit("on_each_cpu() failed");
581 pr_info("Number of cpus: %d, total of %d handlers\n",
582 hp_cpu_list_length, hp_cpu_list_length * HP_PER_CPU);
593 if (special_handler->processor_id == smp_processor_id()) {
594 err = send_first_frame(NULL);
598 smp_call_function_single(special_handler->processor_id,
599 send_first_frame_cb, NULL, 1);
603 wait_event(queue, loop_counter == HP_LOOPS);
604 deallocate_frame_data();
605 if (on_all_cpus(destroy_per_cpu_handlers)) {
607 pr_crit("on_each_cpu() failed");
610 kmem_cache_destroy(hp_handler_slab);
611 pr_info("%s(): Finished\n", __func__);