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 "bman_test.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define NUM_BUFS	93
348c2ecf20Sopenharmony_ci#define LOOPS		3
358c2ecf20Sopenharmony_ci#define BMAN_TOKEN_MASK 0x00FFFFFFFFFFLLU
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic struct bman_pool *pool;
388c2ecf20Sopenharmony_cistatic struct bm_buffer bufs_in[NUM_BUFS] ____cacheline_aligned;
398c2ecf20Sopenharmony_cistatic struct bm_buffer bufs_out[NUM_BUFS] ____cacheline_aligned;
408c2ecf20Sopenharmony_cistatic int bufs_received;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic void bufs_init(void)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	int i;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_BUFS; i++)
478c2ecf20Sopenharmony_ci		bm_buffer_set64(&bufs_in[i], 0xfedc01234567LLU * i);
488c2ecf20Sopenharmony_ci	bufs_received = 0;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic inline int bufs_cmp(const struct bm_buffer *a, const struct bm_buffer *b)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	if (bman_ip_rev == BMAN_REV20 || bman_ip_rev == BMAN_REV21) {
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci		/*
568c2ecf20Sopenharmony_ci		 * On SoCs with BMan revison 2.0, BMan only respects the 40
578c2ecf20Sopenharmony_ci		 * LS-bits of buffer addresses, masking off the upper 8-bits on
588c2ecf20Sopenharmony_ci		 * release commands. The API provides for 48-bit addresses
598c2ecf20Sopenharmony_ci		 * because some SoCs support all 48-bits. When generating
608c2ecf20Sopenharmony_ci		 * garbage addresses for testing, we either need to zero the
618c2ecf20Sopenharmony_ci		 * upper 8-bits when releasing to BMan (otherwise we'll be
628c2ecf20Sopenharmony_ci		 * disappointed when the buffers we acquire back from BMan
638c2ecf20Sopenharmony_ci		 * don't match), or we need to mask the upper 8-bits off when
648c2ecf20Sopenharmony_ci		 * comparing. We do the latter.
658c2ecf20Sopenharmony_ci		 */
668c2ecf20Sopenharmony_ci		if ((bm_buffer_get64(a) & BMAN_TOKEN_MASK) <
678c2ecf20Sopenharmony_ci		    (bm_buffer_get64(b) & BMAN_TOKEN_MASK))
688c2ecf20Sopenharmony_ci			return -1;
698c2ecf20Sopenharmony_ci		if ((bm_buffer_get64(a) & BMAN_TOKEN_MASK) >
708c2ecf20Sopenharmony_ci		    (bm_buffer_get64(b) & BMAN_TOKEN_MASK))
718c2ecf20Sopenharmony_ci			return 1;
728c2ecf20Sopenharmony_ci	} else {
738c2ecf20Sopenharmony_ci		if (bm_buffer_get64(a) < bm_buffer_get64(b))
748c2ecf20Sopenharmony_ci			return -1;
758c2ecf20Sopenharmony_ci		if (bm_buffer_get64(a) > bm_buffer_get64(b))
768c2ecf20Sopenharmony_ci			return 1;
778c2ecf20Sopenharmony_ci	}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return 0;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void bufs_confirm(void)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	int i, j;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_BUFS; i++) {
878c2ecf20Sopenharmony_ci		int matches = 0;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci		for (j = 0; j < NUM_BUFS; j++)
908c2ecf20Sopenharmony_ci			if (!bufs_cmp(&bufs_in[i], &bufs_out[j]))
918c2ecf20Sopenharmony_ci				matches++;
928c2ecf20Sopenharmony_ci		WARN_ON(matches != 1);
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci/* test */
978c2ecf20Sopenharmony_civoid bman_test_api(void)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	int i, loops = LOOPS;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	bufs_init();
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	pr_info("%s(): Starting\n", __func__);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	pool = bman_new_pool();
1068c2ecf20Sopenharmony_ci	if (!pool) {
1078c2ecf20Sopenharmony_ci		pr_crit("bman_new_pool() failed\n");
1088c2ecf20Sopenharmony_ci		goto failed;
1098c2ecf20Sopenharmony_ci	}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* Release buffers */
1128c2ecf20Sopenharmony_cido_loop:
1138c2ecf20Sopenharmony_ci	i = 0;
1148c2ecf20Sopenharmony_ci	while (i < NUM_BUFS) {
1158c2ecf20Sopenharmony_ci		int num = 8;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci		if (i + num > NUM_BUFS)
1188c2ecf20Sopenharmony_ci			num = NUM_BUFS - i;
1198c2ecf20Sopenharmony_ci		if (bman_release(pool, bufs_in + i, num)) {
1208c2ecf20Sopenharmony_ci			pr_crit("bman_release() failed\n");
1218c2ecf20Sopenharmony_ci			goto failed;
1228c2ecf20Sopenharmony_ci		}
1238c2ecf20Sopenharmony_ci		i += num;
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* Acquire buffers */
1278c2ecf20Sopenharmony_ci	while (i > 0) {
1288c2ecf20Sopenharmony_ci		int tmp, num = 8;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		if (num > i)
1318c2ecf20Sopenharmony_ci			num = i;
1328c2ecf20Sopenharmony_ci		tmp = bman_acquire(pool, bufs_out + i - num, num);
1338c2ecf20Sopenharmony_ci		WARN_ON(tmp != num);
1348c2ecf20Sopenharmony_ci		i -= num;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci	i = bman_acquire(pool, NULL, 1);
1378c2ecf20Sopenharmony_ci	WARN_ON(i > 0);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	bufs_confirm();
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (--loops)
1428c2ecf20Sopenharmony_ci		goto do_loop;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Clean up */
1458c2ecf20Sopenharmony_ci	bman_free_pool(pool);
1468c2ecf20Sopenharmony_ci	pr_info("%s(): Finished\n", __func__);
1478c2ecf20Sopenharmony_ci	return;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cifailed:
1508c2ecf20Sopenharmony_ci	WARN_ON(1);
1518c2ecf20Sopenharmony_ci}
152