diff --git a/Makefile.umbrella.tmk b/Makefile.umbrella.tmk index 8263da4f1..bd3d843fa 100644 --- a/Makefile.umbrella.tmk +++ b/Makefile.umbrella.tmk @@ -47,6 +47,7 @@ NV_REPOSITORY_COMPONENTS += userspace/units/posix/cond NV_REPOSITORY_COMPONENTS += userspace/units/posix/timers NV_REPOSITORY_COMPONENTS += userspace/units/posix/kmem NV_REPOSITORY_COMPONENTS += userspace/units/posix/rwsem +NV_REPOSITORY_COMPONENTS += userspace/units/posix/queue NV_REPOSITORY_COMPONENTS += userspace/units/interface/bsearch NV_REPOSITORY_COMPONENTS += userspace/units/interface/lock NV_REPOSITORY_COMPONENTS += userspace/units/interface/atomic diff --git a/drivers/gpu/nvgpu/libnvgpu-drv_safe.export b/drivers/gpu/nvgpu/libnvgpu-drv_safe.export index 777bc0017..af58a1975 100644 --- a/drivers/gpu/nvgpu/libnvgpu-drv_safe.export +++ b/drivers/gpu/nvgpu/libnvgpu-drv_safe.export @@ -619,6 +619,12 @@ nvgpu_posix_is_fault_injection_cntr_set nvgpu_posix_is_fault_injection_triggered nvgpu_posix_probe nvgpu_posix_register_io +nvgpu_queue_alloc +nvgpu_queue_free +nvgpu_queue_in +nvgpu_queue_in_locked +nvgpu_queue_out +nvgpu_queue_out_locked nvgpu_preempt_channel nvgpu_preempt_get_timeout nvgpu_preempt_poll_tsg_on_pbdma diff --git a/userspace/Makefile.sources b/userspace/Makefile.sources index 3328e7eab..bf20b7693 100644 --- a/userspace/Makefile.sources +++ b/userspace/Makefile.sources @@ -55,6 +55,7 @@ UNITS := \ $(UNIT_SRC)/posix/timers \ $(UNIT_SRC)/posix/kmem \ $(UNIT_SRC)/posix/rwsem \ + $(UNIT_SRC)/posix/queue \ $(UNIT_SRC)/bus \ $(UNIT_SRC)/pramin \ $(UNIT_SRC)/ptimer \ diff --git a/userspace/SWUTS.h b/userspace/SWUTS.h index a610e539f..576986d46 100644 --- a/userspace/SWUTS.h +++ b/userspace/SWUTS.h @@ -104,6 +104,7 @@ * - @ref SWUTS-posix-sizes * - @ref SWUTS-posix-thread * - @ref SWUTS-posix-timers + * - @ref SWUTS-posix-queue * - @ref SWUTS-priv_ring * - @ref SWUTS-ptimer * - @ref SWUTS-sdl diff --git a/userspace/units/posix/queue/Makefile b/userspace/units/posix/queue/Makefile new file mode 100644 index 000000000..1c9571a28 --- /dev/null +++ b/userspace/units/posix/queue/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-queue.o +MODULE = posix-queue + +include ../../Makefile.units diff --git a/userspace/units/posix/queue/Makefile.interface.tmk b/userspace/units/posix/queue/Makefile.interface.tmk new file mode 100644 index 000000000..beaf84432 --- /dev/null +++ b/userspace/units/posix/queue/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-queue + +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/queue/Makefile.tmk b/userspace/units/posix/queue/Makefile.tmk new file mode 100644 index 000000000..5c6053028 --- /dev/null +++ b/userspace/units/posix/queue/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-queue + +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/queue/posix-queue.c b/userspace/units/posix/queue/posix-queue.c new file mode 100644 index 000000000..d96d96822 --- /dev/null +++ b/userspace/units/posix/queue/posix-queue.c @@ -0,0 +1,258 @@ +/* + * 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 +#include +#include + +#include "posix-queue.h" + +#define QUEUE_LEN 10 +#define QUEUE_LEN_POW_2 16 +#define BUF_LEN 10 + +int test_nvgpu_queue_alloc_and_free(struct unit_module *m, struct gk20a *g, + void *args) +{ + int ret = 0, err = UNIT_SUCCESS; + struct nvgpu_queue q = {0}; + struct nvgpu_posix_fault_inj *kmem_fi = + nvgpu_kmem_get_fault_injection(); + + ret = nvgpu_queue_alloc(NULL, QUEUE_LEN); + if (ret != -EINVAL) { + err = UNIT_FAIL; + unit_err(m, "%d. test_queue_alloc failed err=%d\n", __LINE__, + ret); + goto fail; + } + + ret = nvgpu_queue_alloc(&q, 0); + if (ret != -EINVAL) { + err = UNIT_FAIL; + unit_err(m, "%d. test_queue_alloc failed err=%d\n", __LINE__, + ret); + goto fail; + } + + ret = nvgpu_queue_alloc(&q, (unsigned int)INT64_MAX); + if (ret != -EINVAL) { + err = UNIT_FAIL; + unit_err(m, "%d. test_queue_alloc failed err=%d\n", __LINE__, + ret); + goto fail; + } + + nvgpu_posix_enable_fault_injection(kmem_fi, true, 0); + ret = nvgpu_queue_alloc(&q, QUEUE_LEN_POW_2); + if (ret != -ENOMEM) { + nvgpu_posix_enable_fault_injection(kmem_fi, false, 0); + err = UNIT_FAIL; + unit_err(m, "%d. test_queue_alloc failed err=%d\n", __LINE__, + ret); + goto fail; + } + nvgpu_posix_enable_fault_injection(kmem_fi, false, 0); + + ret = nvgpu_queue_alloc(&q, QUEUE_LEN); + if (ret != 0) { + err = UNIT_FAIL; + unit_err(m, "%d. test_queue_alloc failed err=%d\n", __LINE__, + ret); + goto fail; + } + nvgpu_queue_free(&q); + + ret = nvgpu_queue_alloc(&q, QUEUE_LEN_POW_2); + if (ret != 0) { + err = UNIT_FAIL; + unit_err(m, "%d. test_queue_alloc failed err=%d\n", __LINE__, + ret); + goto fail; + } + nvgpu_queue_free(&q); + +fail: + return err; +} + +int test_nvgpu_queue_in(struct unit_module *m, struct gk20a *g, void *args) +{ + int ret = 0, err = UNIT_SUCCESS; + struct nvgpu_queue q = {0}; + struct nvgpu_mutex lock; + char buf[BUF_LEN]; + + nvgpu_mutex_init(&lock); + + /* Allocate Queue of size QUEUE_LEN_POW_2 */ + ret = nvgpu_queue_alloc(&q, QUEUE_LEN_POW_2); + if (ret != 0) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_alloc failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* Enqueue message of length BUF_LEN */ + ret = nvgpu_queue_in(&q, buf, BUF_LEN); + if (ret != BUF_LEN) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_in failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* + * Update "in" and "out" indexes and enqueue message of length BUF_LEN + * such that we wrap around the Queue while enqueuing the message. + */ + q.in = BUF_LEN; + q.out = BUF_LEN; + ret = nvgpu_queue_in(&q, buf, BUF_LEN); + if (ret != BUF_LEN) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_in failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* + * Reset "in" and "out" indexes and enueue message of length BUF_LEN + * with the lock. + */ + q.in = 0; + q.out = 0; + ret = nvgpu_queue_in_locked(&q, buf, BUF_LEN, &lock); + if (ret != BUF_LEN) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_in failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* Enueue message of length BUF_LEN again and expect memory full */ + ret = nvgpu_queue_in_locked(&q, buf, BUF_LEN, &lock); + if (ret != -ENOMEM) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_in failed err=%d\n", __LINE__, ret); + goto fail; + } + +fail: + if (q.data != NULL) + free(q.data); + nvgpu_mutex_destroy(&lock); + + return err; +} + +int test_nvgpu_queue_out(struct unit_module *m, struct gk20a *g, void *args) +{ + int ret = 0, err = UNIT_SUCCESS; + struct nvgpu_queue q = {0}; + struct nvgpu_mutex lock; + char buf[BUF_LEN]; + + nvgpu_mutex_init(&lock); + + /* Allocate Queue of size QUEUE_LEN_POW_2 */ + ret = nvgpu_queue_alloc(&q, QUEUE_LEN_POW_2); + if (ret != 0) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_alloc failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* Queue is empty. Dequeue message should return "-ENOMEM" */ + ret = nvgpu_queue_out(&q, buf, BUF_LEN); + if (ret != -ENOMEM) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_out failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* Queue is empty. Dequeue message with lock should return "-ENOMEM" */ + ret = nvgpu_queue_out_locked(&q, buf, BUF_LEN, &lock); + if (ret != -ENOMEM) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_out failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* + * Advance "in" index by "BUF_LEN" and deueue message of length BUF_LEN. + */ + q.in = BUF_LEN; + q.out = 0; + ret = nvgpu_queue_out(&q, buf, BUF_LEN); + if (ret != BUF_LEN) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_out failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* + * Advance "in" index by "BUF_LEN" and deueue message of length BUF_LEN + * with the lock. + */ + q.in = BUF_LEN; + q.out = 0; + ret = nvgpu_queue_out_locked(&q, buf, BUF_LEN, &lock); + if (ret != BUF_LEN) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_out failed err=%d\n", __LINE__, ret); + goto fail; + } + + /* + * Update "in" and "out" indexes and dequeue message of length BUF_LEN + * such that we wrap around the Queue while dequeuing the message. + */ + q.in = 1; + q.out = QUEUE_LEN_POW_2 -(BUF_LEN -1); + ret = nvgpu_queue_out(&q, buf, BUF_LEN); + if (ret != BUF_LEN) { + err = UNIT_FAIL; + unit_err(m, "%d. queue_out failed err=%d\n", __LINE__, ret); + goto fail; + } + +fail: + if (q.data != NULL) + free(q.data); + nvgpu_mutex_destroy(&lock); + + return err; +} + +struct unit_module_test posix_queue_tests[] = { + UNIT_TEST(nvgpu_queue_alloc_free, test_nvgpu_queue_alloc_and_free, + NULL, 0), + UNIT_TEST(nvgpu_queue_in, test_nvgpu_queue_in, NULL, 0), + UNIT_TEST(nvgpu_queue_out, test_nvgpu_queue_out, NULL, 0), +}; + +UNIT_MODULE(posix_queue, posix_queue_tests, UNIT_PRIO_POSIX_TEST); diff --git a/userspace/units/posix/queue/posix-queue.h b/userspace/units/posix/queue/posix-queue.h new file mode 100644 index 000000000..0f70bc7c7 --- /dev/null +++ b/userspace/units/posix/queue/posix-queue.h @@ -0,0 +1,148 @@ +/* + * 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-queue + * @{ + * + * Software Unit Test Specification for posix-queue + */ + +#ifndef __UNIT_POSIX_QUEUE_H__ +#define __UNIT_POSIX_QUEUE_H__ + +/** + * Test specification for: test_nvgpu_queue_alloc_and_free + * + * Description: Functionalities of posix queue such as allocating and freeing + * of the message queue are are tested. + * + * Test Type: Feature, Error guessing, Boundary values + * + * Targets: nvgpu_queue_alloc, nvgpu_queue_free + * + * Input: None + * + * Steps: + * - Pass NULL nvgpu_queue pointer as argument to nvgpu_queue_alloc() API and + * check that the API returns "-EINVAL" error. + * - Pass zero size queue length as argument to nvgpu_queue_alloc() API and + * check that the API returns "-EINVAL" error. + * - Pass "INT64_MAX" size queue length as argument to nvgpu_queue_alloc() API + * and check that the API returns "-EINVAL" error. + * - Inject fault so that immediate call to nvgpu_kzalloc() API would fail. + * - Check that when the nvgpu_queue_alloc() API is called with valid arguments, + * it would fail by returning "-ENOMEM" error. + * - Remove the injected fault in nvgpu_kzalloc() API. + * - Pass below valid arguments to nvgpu_queue_alloc() API and check that the + * API returns success. + * - Valid pointer to "struct nvgpu_queue" + * - Queue size which is not power of 2 + * - Free the allocated queue by caling nvgpu_queue_free() API. + * - Pass below valid arguments to nvgpu_queue_alloc() API and check that the + * API returns success. + * - Valid pointer to "struct nvgpu_queue" + * - Queue size which is power of 2 + * - Free the allocated queue by caling nvgpu_queue_free() API. + * + * Output: Returns PASS if the steps above were executed successfully. FAIL + * otherwise. + */ +int test_nvgpu_queue_alloc_and_free(struct unit_module *m, struct gk20a *g, + void *args); + +/** + * Test specification for: test_nvgpu_queue_in + * + * Description: Functionalities of posix queue such as allocating queue and + * enqueueing messages into the queue are tested. + * + * Test Type: Feature, Error guessing, Boundary values + * + * Targets: nvgpu_queue_alloc, nvgpu_queue_in, nvgpu_queue_in_locked, + * nvgpu_queue_unused, nvgpu_queue_available + * + * Input: None + * + * Steps: + * - Pass below valid arguments to nvgpu_queue_alloc() API and check that the + * API returns success. + * - Valid pointer to "struct nvgpu_queue" + * - Queue size which is power of 2 + * - Enqueue message of length "BUF_LEN" calling nvgpu_queue_in() API and check + * that the API returns "BUF_LEN". + * - Update "in" and "out" indexes and enqueue message of length BUF_LEN such + * that we wrap around the Queue while enqueuing the message using + * nvgpu_queue_in() API. Check that the API returns "BUF_LEN". + * - Reset "in" and "out" indexes and enueue message of length "BUF_LEN" with + * the lock using nvgpu_queue_in_locked() API. Check that the API returns + * "BUF_LEN". + * - Enueue message of length "BUF_LEN" again using nvgpu_queue_in_locked() API. + * Check that the API returns error "-ENOMEM". + * - Uninitialize the allocated resources. + * + * Output: Returns PASS if the steps above were executed successfully. FAIL + * otherwise. + */ +int test_nvgpu_queue_in(struct unit_module *m, struct gk20a *g, void *args); + +/** + * Test specification for: test_nvgpu_queue_out + * + * Description: Functionalities of posix queue such as allocating queue and + * dequeueing messages from the queue are tested. + * + * Test Type: Feature, Error guessing, Boundary values + * + * Targets: nvgpu_queue_alloc, nvgpu_queue_out, nvgpu_queue_out_locked + * + * Input: None + * + * Steps: + * - Pass below valid arguments to nvgpu_queue_alloc() API and check that the + * API returns success. + * - Valid pointer to "struct nvgpu_queue" + * - Queue size which is power of 2 + * - Dequeue message of length "BUF_LEN" from the empty queue calling + * nvgpu_queue_out() API and check that the API returns "-ENOMEM" error. + * - Dequeue message of length "BUF_LEN" from the empty queue calling + * nvgpu_queue_out_locked() API and check that the API returns "-ENOMEM" + * error. + * - Advance "in" index by "BUF_LEN" and deueue message of length BUF_LEN by + * calling nvgpu_queue_out() API and check that the API returns "BUF_LEN". + * - Advance "in" index by "BUF_LEN" and deueue message of length BUF_LEN by + * calling nvgpu_queue_out_locked() API and check that the API returns + * "BUF_LEN". + * - Update "in" and "out" indexes and dequeue message of length BUF_LEN such + * that we wrap around the Queue while dequeuing the message using + * nvgpu_queue_out() API. Check that the API returns "BUF_LEN". + * - Uninitialize the allocated resources. + * + * Output: Returns PASS if the steps above were executed successfully. FAIL + * otherwise. + */ +int test_nvgpu_queue_out(struct unit_module *m, struct gk20a *g, void *args); + +#endif /* __UNIT_POSIX_QUEUE_H__ */ +/* + * @} + */