From d794ec926cc976e12ee475661d57ff5a0239ccd7 Mon Sep 17 00:00:00 2001 From: ajesh Date: Wed, 29 Jan 2020 14:57:08 +0530 Subject: [PATCH] gpu: nvgpu: add unit tests for log2 Add unit tests for posix log2 unit. Fix the build issue with rounddown_pow_of_two macro. Jira NVGPU-4417 Change-Id: I244aa4ae2f8c5e682e263784b4efbf9cd372379e Signed-off-by: ajesh Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2286955 Tested-by: mobile promotions Reviewed-by: mobile promotions --- Makefile.umbrella.tmk | 1 + drivers/gpu/nvgpu/include/nvgpu/posix/log2.h | 2 +- userspace/Makefile.sources | 1 + userspace/SWUTS.h | 1 + userspace/SWUTS.sources | 1 + userspace/required_tests.json | 24 +++ userspace/units/posix/log2/Makefile | 26 +++ .../units/posix/log2/Makefile.interface.tmk | 35 ++++ userspace/units/posix/log2/Makefile.tmk | 35 ++++ userspace/units/posix/log2/posix-log2.c | 169 ++++++++++++++++++ userspace/units/posix/log2/posix-log2.h | 147 +++++++++++++++ 11 files changed, 441 insertions(+), 1 deletion(-) create mode 100644 userspace/units/posix/log2/Makefile create mode 100644 userspace/units/posix/log2/Makefile.interface.tmk create mode 100644 userspace/units/posix/log2/Makefile.tmk create mode 100644 userspace/units/posix/log2/posix-log2.c create mode 100644 userspace/units/posix/log2/posix-log2.h diff --git a/Makefile.umbrella.tmk b/Makefile.umbrella.tmk index 970748d04..20962e03e 100644 --- a/Makefile.umbrella.tmk +++ b/Makefile.umbrella.tmk @@ -54,6 +54,7 @@ NV_REPOSITORY_COMPONENTS += userspace/units/posix/bug NV_REPOSITORY_COMPONENTS += userspace/units/posix/os_sched NV_REPOSITORY_COMPONENTS += userspace/units/posix/sizes NV_REPOSITORY_COMPONENTS += userspace/units/init +NV_REPOSITORY_COMPONENTS += userspace/units/posix/log2 NV_REPOSITORY_COMPONENTS += userspace/units/posix/thread NV_REPOSITORY_COMPONENTS += userspace/units/posix/cond NV_REPOSITORY_COMPONENTS += userspace/units/posix/timers diff --git a/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h b/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h index e63752137..2c0e6a64d 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h +++ b/drivers/gpu/nvgpu/include/nvgpu/posix/log2.h @@ -82,7 +82,7 @@ BUG(); \ } else { \ ret = 1UL << \ - nvgpu_(fls(x) - 1UL); \ + (nvgpu_fls((x)) - 1UL); \ } \ ret; \ }) diff --git a/userspace/Makefile.sources b/userspace/Makefile.sources index 766870d3f..fde275993 100644 --- a/userspace/Makefile.sources +++ b/userspace/Makefile.sources @@ -49,6 +49,7 @@ UNITS := \ $(UNIT_SRC)/posix/fault-injection \ $(UNIT_SRC)/posix/bug \ $(UNIT_SRC)/posix/os_sched \ + $(UNIT_SRC)/posix/log2 \ $(UNIT_SRC)/posix/sizes \ $(UNIT_SRC)/posix/thread \ $(UNIT_SRC)/posix/cond \ diff --git a/userspace/SWUTS.h b/userspace/SWUTS.h index 2131c235d..f28b22a9f 100644 --- a/userspace/SWUTS.h +++ b/userspace/SWUTS.h @@ -110,6 +110,7 @@ * - @ref SWUTS-fuse * - @ref SWUTS-posix-bitops * - @ref SWUTS-posix-cond + * - @ref SWUTS-posix-log2 * - @ref SWUTS-posix-fault-injection * - @ref SWUTS-posix-sizes * - @ref SWUTS-posix-thread diff --git a/userspace/SWUTS.sources b/userspace/SWUTS.sources index 2b51c3bc5..1ca9ecd1a 100644 --- a/userspace/SWUTS.sources +++ b/userspace/SWUTS.sources @@ -87,6 +87,7 @@ INPUT += ../../../userspace/units/sync/nvgpu-sync.h INPUT += ../../../userspace/units/posix/bitops/posix-bitops.h INPUT += ../../../userspace/units/posix/bug/posix-bug.h INPUT += ../../../userspace/units/posix/cond/posix-cond.h +INPUT += ../../../userspace/units/posix/log2/posix-log2.h INPUT += ../../../userspace/units/posix/queue/posix-queue.h INPUT += ../../../userspace/units/posix/fault-injection/posix-fault-injection.h INPUT += ../../../userspace/units/posix/fault-injection/posix-fault-injection-kmem.h diff --git a/userspace/required_tests.json b/userspace/required_tests.json index 552f3b399..0479e5738 100644 --- a/userspace/required_tests.json +++ b/userspace/required_tests.json @@ -4292,6 +4292,30 @@ "unit": "posix_kmem", "test_level": 0 }, + { + "test": "test_ilog2", + "case": "integer_log2", + "unit": "posix_log2", + "test_level": 0 + }, + { + "test": "test_roundup_powoftwo", + "case": "roundup_pow2", + "unit": "posix_log2", + "test_level": 0 + }, + { + "test": "test_rounddown_powoftwo", + "case": "rounddown_pow2", + "unit": "posix_log2", + "test_level": 0 + }, + { + "test": "test_ispow2", + "case": "is_powof2", + "unit": "posix_log2", + "test_level": 0 + }, { "test": "test_readl", "case": "bar1_readl", diff --git a/userspace/units/posix/log2/Makefile b/userspace/units/posix/log2/Makefile new file mode 100644 index 000000000..711eecc91 --- /dev/null +++ b/userspace/units/posix/log2/Makefile @@ -0,0 +1,26 @@ +# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. + +.SUFFIXES: + +OBJS = posix-log2.o +MODULE = posix-log2 + +include ../../Makefile.units diff --git a/userspace/units/posix/log2/Makefile.interface.tmk b/userspace/units/posix/log2/Makefile.interface.tmk new file mode 100644 index 000000000..a7b64c58d --- /dev/null +++ b/userspace/units/posix/log2/Makefile.interface.tmk @@ -0,0 +1,35 @@ +################################### tell Emacs this is a -*- makefile-gmake -*- +# +# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# +# tmake for SW Mobile component makefile +# +############################################################################### + +NVGPU_UNIT_NAME=posix-log2 + +include $(NV_COMPONENT_DIR)/../../Makefile.units.common.interface.tmk + +# Local Variables: +# indent-tabs-mode: t +# tab-width: 8 +# End: +# vi: set tabstop=8 noexpandtab: diff --git a/userspace/units/posix/log2/Makefile.tmk b/userspace/units/posix/log2/Makefile.tmk new file mode 100644 index 000000000..227993903 --- /dev/null +++ b/userspace/units/posix/log2/Makefile.tmk @@ -0,0 +1,35 @@ +################################### tell Emacs this is a -*- makefile-gmake -*- +# +# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# +# tmake for SW Mobile component makefile +# +############################################################################### + +NVGPU_UNIT_NAME=posix-log2 + +include $(NV_COMPONENT_DIR)/../../Makefile.units.common.tmk + +# Local Variables: +# indent-tabs-mode: t +# tab-width: 8 +# End: +# vi: set tabstop=8 noexpandtab: diff --git a/userspace/units/posix/log2/posix-log2.c b/userspace/units/posix/log2/posix-log2.c new file mode 100644 index 000000000..1d57775cd --- /dev/null +++ b/userspace/units/posix/log2/posix-log2.c @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include + +#include +#include + +#include + +#include "posix-log2.h" + +int test_ilog2(struct unit_module *m, + struct gk20a *g, void *args) +{ + unsigned long i, test, ret; + + for (i = 0; i < BITS_PER_LONG; i++) { + test = 1UL << i; + ret = ilog2(test); + if (ret != i) { + unit_return_fail(m, + "ilog2 failure %ld\n", test); + } + } + + for (i = 1; i < (BITS_PER_LONG - 1); i++) { + test = 1UL << i; + test += 1; + ret = ilog2(test); + if (ret != i) { + unit_return_fail(m, + "ilog2 failure %ld\n", test); + } + } + + return UNIT_SUCCESS; +} + +int test_roundup_powoftwo(struct unit_module *m, + struct gk20a *g, void *args) +{ + unsigned long i, test, ret; + + test = 0UL; + + if (!EXPECT_BUG(roundup_pow_of_two(test))) { + unit_return_fail(m, + "roundup_pow_of_two did not invoke BUG()\n"); + } else { + unit_info(m, "BUG invoked as expected for input value 0\n"); + } + + for (i = 0; i < BITS_PER_LONG; i++) { + test = 1UL << i; + ret = roundup_pow_of_two(test); + if (ret != test) { + unit_return_fail(m, + "roundup_pow_of_two failure.\n"); + } + } + + for (i = 0; i < (BITS_PER_LONG - 1); i++) { + test = 1UL << i; + test += 1; + ret = roundup_pow_of_two(test); + if (ret != (1UL << (i + 1))) { + unit_return_fail(m, + "roundup_pow_of_two failure.\n"); + } + } + + return UNIT_SUCCESS; +} + +int test_rounddown_powoftwo(struct unit_module *m, + struct gk20a *g, void *args) +{ + unsigned long i, test, ret; + + test = 0UL; + + if (!EXPECT_BUG(rounddown_pow_of_two(test))) { + unit_return_fail(m, + "rounddown_pow_of_two did not invoke BUG()\n"); + } else { + unit_info(m, "BUG invoked as expected for input value 0\n"); + } + + for (i = 0; i < BITS_PER_LONG; i++) { + test = 1UL << i; + ret = rounddown_pow_of_two(test); + if (ret != test) { + unit_return_fail(m, + "rounddown_pow_of_two failure.\n"); + } + } + + for (i = 1; i < BITS_PER_LONG; i++) { + test = 1UL << i; + test -= 1; + ret = rounddown_pow_of_two(test); + if (ret != (1UL << (i - 1))) { + unit_return_fail(m, + "rounddown_pow_of_two failure.\n"); + } + } + + return UNIT_SUCCESS; +} + +int test_ispow2(struct unit_module *m, + struct gk20a *g, void *args) +{ + bool ret; + unsigned long i, test; + + test = 0UL; + + for (i = 0; i < BITS_PER_LONG; i++) { + test = 1UL << i; + ret = is_power_of_2(test); + if (!ret) { + unit_return_fail(m, + "is_power_of_2 failure %ld\n", test); + } + } + + for (i = 1; i < (BITS_PER_LONG - 1); i++) { + test = 1UL << i; + test += 1; + ret = is_power_of_2(test); + if (ret) { + unit_return_fail(m, + "is_power_of_2 failure %ld\n", test); + } + } + + return UNIT_SUCCESS; +} + +struct unit_module_test posix_log2_tests[] = { + UNIT_TEST(integer_log2, test_ilog2, NULL, 0), + UNIT_TEST(roundup_pow2, test_roundup_powoftwo, NULL, 0), + UNIT_TEST(rounddown_pow2, test_rounddown_powoftwo, NULL, 0), + UNIT_TEST(is_powof2, test_ispow2, NULL, 0), +}; + +UNIT_MODULE(posix_log2, posix_log2_tests, UNIT_PRIO_POSIX_TEST); diff --git a/userspace/units/posix/log2/posix-log2.h b/userspace/units/posix/log2/posix-log2.h new file mode 100644 index 000000000..47514ff48 --- /dev/null +++ b/userspace/units/posix/log2/posix-log2.h @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +/** + * @addtogroup SWUTS-posix-log2 + * @{ + * + * Software Unit Test Specification for posix-log2 + */ + +#ifndef UNIT_POSIX_LOG2_H +#define UNIT_POSIX_LOG2_H + +/** + * Test specification for test_ilog2 + * + * Description: Test integer logarithm of base 2 implementation. + * + * Test Type: Feature + * + * Targets: ilog2 + * + * Inputs: None + * + * Steps: + * 1) Invoke ilog2 in loop for all the power of two numbers that can be held in + * an unsigned long variable. + * 2) Confirm if the return value from the macro is equal to the loop index. + * 3) Return false otherwise. + * 4) Invoke ilog2 in loop for numbers which are one greater than the power of + * two. + * 5) Confirm if the return value from the macro is equal to the loop index. + * 6) Return false otherwise. + * + * Output: + * The test returns PASS if all the invocations of ilog2 returns the log to + * the base 2 of the input number as expected. Otherwise, test returns FAIL. + */ +int test_ilog2(struct unit_module *m, + struct gk20a *g, void *args); + +/** + * Test specification for test_roundup_powoftwo + * + * Description: Test round up power of two implementation. + * + * Test Type: Feature + * + * Targets: roundup_pow_of_two + * + * Inputs: None + * + * Steps: + * 1) Invoke roundup_pow_of_two for input value 0UL using EXPECT_BUG. + * 2) Check if EXPECT_BUG returns true, otherwise, return fail. + * 3) Invoke roundup_pow_of_two in loop for all the power of two numbers + * that can be held in an unsigned long variable. + * 4) Confirm if the return value from the macro is equal to the input argument + * passed. Return false otherwise. + * 5) Invoke roundup_pow_of_two in loop for numbers which are one greater than + * the power of two. + * 6) Confirm if the return value from the macro is equal to the rounded up + * power of two value of the input number. Return false otherwise. + * + * Output: + * The test returns PASS if all the invocations of roundup_pow_of_two + * returns the expected value as result. Otherwise, test returns FAIL. + */ +int test_roundup_powoftwo(struct unit_module *m, + struct gk20a *g, void *args); + +/** + * Test specification for test_rounddown_powoftwo + * + * Description: Test round down power of two implementation. + * + * Test Type: Feature + * + * Targets: rounddown_pow_of_two + * + * Inputs: None + * + * Steps: + * 1) Invoke rounddown_pow_of_two for input value 0UL using EXPECT_BUG. + * 2) Check if EXPECT_BUG returns true, otherwise, return fail. + * 3) Invoke rounddown_pow_of_two in loop for all the power of two numbers + * that can be held in an unsigned long variable. + * 4) Confirm if the return value from the macro is equal to the input argument + * passed. Return false otherwise. + * 5) Invoke rounddown_pow_of_two in loop for numbers which are one less than + * the power of two. + * 6) Confirm if the return value from the macro is equal to the power of two + * rounded down value of the input number. Return false otherwise. + * + * Output: + * The test returns PASS if all the invocations of rounddown_pow_of_two + * returns the expected value as result. Otherwise, test returns FAIL. + */ +int test_rounddown_powoftwo(struct unit_module *m, + struct gk20a *g, void *args); + +/** + * Test specification for test_ispow2 + * + * Description: Test the power of two implementation. + * + * Test Type: Feature + * + * Targets: is_power_of_2 + * + * Inputs: None + * + * Steps: + * 1) Invoke is_power_of_2 in loop for all the power of two numbers that can + * be held in an unsigned long variable. + * 2) Confirm if the return value from the macro is true, else, return fail. + * 3) Invoke is_power_of_2 in loop for numbers which are one greater than the + * power of two. + * 4) Confirm if the return value from the macro is false. Otherwise, return + * fail. + * + * Output: + * The test returns PASS if all the invocations of is_power_of_2 returns the + * result as expected. Otherwise, test returns FAIL. + */ +int test_ispow2(struct unit_module *m, + struct gk20a *g, void *args); +#endif /* UNIT_POSIX_LOG2_H */