From a6bb97da342d4f43f3d39bd6849dc097c45cd016 Mon Sep 17 00:00:00 2001 From: Dinesh Date: Wed, 20 Mar 2019 11:26:56 +0530 Subject: [PATCH] gpu: nvgpu: Unifying qnx kmem with posix This patch will add changes for unifying the kmem functions in posix with qnx kmem functions. JIRA NVGPU-2148 Change-Id: I7ed08796d8baa9a0f5e194a664ac5710d5cb79b7 Signed-off-by: Dinesh Reviewed-on: https://git-master.nvidia.com/r/2077081 Reviewed-by: mobile promotions Tested-by: mobile promotions --- drivers/gpu/nvgpu/Makefile.sources | 10 ++- drivers/gpu/nvgpu/Makefile.tmk | 3 + drivers/gpu/nvgpu/include/nvgpu/kmem.h | 6 +- drivers/gpu/nvgpu/os/posix/kmem.c | 101 ++++++++++++++----------- drivers/gpu/nvgpu/os/posix/posix-dma.c | 8 +- userspace/Makefile.tmk | 3 +- 6 files changed, 75 insertions(+), 56 deletions(-) diff --git a/drivers/gpu/nvgpu/Makefile.sources b/drivers/gpu/nvgpu/Makefile.sources index 2f1b37b11..78e40d049 100644 --- a/drivers/gpu/nvgpu/Makefile.sources +++ b/drivers/gpu/nvgpu/Makefile.sources @@ -26,7 +26,6 @@ ifdef NVGPU_POSIX srcs += os/posix/nvgpu.c \ os/posix/bitmap.c \ os/posix/log.c \ - os/posix/kmem.c \ os/posix/posix-io.c \ os/posix/posix-nvgpu_mem.c \ os/posix/posix-dma.c \ @@ -41,7 +40,6 @@ srcs += os/posix/nvgpu.c \ os/posix/posix-tsg.c \ os/posix/posix-nvlink.c \ os/posix/stubs.c \ - os/posix/posix-fault-injection.c \ os/posix/posix-sim.c \ os/posix/posix-nvhost.c \ os/posix/posix-vgpu.c \ @@ -57,7 +55,8 @@ srcs += os/posix/bug.c \ os/posix/lock.c \ os/posix/thread.c \ os/posix/bsearch.c \ - os/posix/os_sched.c + os/posix/os_sched.c \ + os/posix/kmem.c srcs += common/sim.c \ common/sim_pci.c \ @@ -394,6 +393,11 @@ ifeq ($(NVGPU_DEBUGGER),1) srcs += common/debugger.c endif +# POSIX file used for unit testing for both qnx and linux +ifdef NVGPU_FAULT_INJECTION_ENABLEMENT +srcs += os/posix/posix-fault-injection.c +endif + srcs += hal/gr/config/gr_config_gm20b.c \ hal/gr/config/gr_config_gv100.c diff --git a/drivers/gpu/nvgpu/Makefile.tmk b/drivers/gpu/nvgpu/Makefile.tmk index 0fc96f1b4..9000e0599 100644 --- a/drivers/gpu/nvgpu/Makefile.tmk +++ b/drivers/gpu/nvgpu/Makefile.tmk @@ -77,6 +77,9 @@ else NV_COMPONENT_CFLAGS += -DNVGPU_USERD endif +NVGPU_FAULT_INJECTION_ENABLEMENT := 1 +NV_COMPONENT_CFLAGS += -DNVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT + _NV_TOOLCHAIN_CFLAGS += -rdynamic -g -include $(NV_COMPONENT_DIR)/Makefile.sources diff --git a/drivers/gpu/nvgpu/include/nvgpu/kmem.h b/drivers/gpu/nvgpu/include/nvgpu/kmem.h index 61f90bf54..da9e33907 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/kmem.h +++ b/drivers/gpu/nvgpu/include/nvgpu/kmem.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2018, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2017-2019, 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"), @@ -34,10 +34,8 @@ struct gk20a; */ #ifdef __KERNEL__ #include -#elif defined(__NVGPU_POSIX__) -#include #else -#include +#include #endif /** diff --git a/drivers/gpu/nvgpu/os/posix/kmem.c b/drivers/gpu/nvgpu/os/posix/kmem.c index e522c7215..12c35980f 100644 --- a/drivers/gpu/nvgpu/os/posix/kmem.c +++ b/drivers/gpu/nvgpu/os/posix/kmem.c @@ -25,20 +25,30 @@ #include #include #include - +#include #include +#include +#include +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT #include +#endif struct nvgpu_kmem_cache { - size_t alloc_size; + struct gk20a *g; + size_t size; + char name[128]; }; +static nvgpu_atomic_t kmem_cache_id; + +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT _Thread_local struct nvgpu_posix_fault_inj kmem_fi; struct nvgpu_posix_fault_inj *nvgpu_kmem_get_fault_injection(void) { return &kmem_fi; } +#endif /* * kmem cache emulation: basically just do a regular malloc(). This is slower @@ -47,17 +57,23 @@ struct nvgpu_posix_fault_inj *nvgpu_kmem_get_fault_injection(void) struct nvgpu_kmem_cache *nvgpu_kmem_cache_create(struct gk20a *g, size_t size) { struct nvgpu_kmem_cache *cache; - +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&kmem_fi)) { return NULL; } - +#endif cache = malloc(sizeof(struct nvgpu_kmem_cache)); + if (cache == NULL) { return NULL; } - cache->alloc_size = size; + cache->g = g; + cache->size = size; + + (void)snprintf(cache->name, sizeof(cache->name), + "nvgpu-cache-0x%p-%d-%d", g, (int)size, + nvgpu_atomic_inc_return(&kmem_cache_id)); return cache; } @@ -69,11 +85,12 @@ void nvgpu_kmem_cache_destroy(struct nvgpu_kmem_cache *cache) void *nvgpu_kmem_cache_alloc(struct nvgpu_kmem_cache *cache) { +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&kmem_fi)) { return NULL; - } else { - return malloc(cache->alloc_size); } +#endif + return malloc(cache->size); } void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr) @@ -83,33 +100,49 @@ void nvgpu_kmem_cache_free(struct nvgpu_kmem_cache *cache, void *ptr) void *__nvgpu_kmalloc(struct gk20a *g, size_t size, void *ip) { +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&kmem_fi)) { return NULL; - } else { - return malloc(size); } +#endif + /* + * Since, the callers don't really need the memory region to be + * contiguous, use malloc here. If the need arises for this + * interface to return contiguous memory, we can explore using + * nvmap_page_alloc in qnx (i.e. using shm_open/shm_ctl_special/mmap + * calls). + */ + return malloc(size); } void *__nvgpu_kzalloc(struct gk20a *g, size_t size, void *ip) { +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&kmem_fi)) { return NULL; - } else { - return calloc(1, size); } +#endif + return calloc(1, size); } void *__nvgpu_kcalloc(struct gk20a *g, size_t n, size_t size, void *ip) { - /* - * calloc() implicitly zeros mem. So calloc a single member size bytes - * long. - */ +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&kmem_fi)) { return NULL; - } else { - return calloc(n, size); } +#endif + return calloc(1, n * size); +} + +void *__nvgpu_vmalloc(struct gk20a *g, unsigned long size, void *ip) +{ + return __nvgpu_kmalloc(g, size, ip); +} + +void *__nvgpu_vzalloc(struct gk20a *g, unsigned long size, void *ip) +{ + return __nvgpu_kzalloc(g, size, ip); } void __nvgpu_kfree(struct gk20a *g, void *addr) @@ -117,38 +150,18 @@ void __nvgpu_kfree(struct gk20a *g, void *addr) free(addr); } -/* - * The concept of vmalloc() does not exist in userspace. - */ -void *__nvgpu_vmalloc(struct gk20a *g, unsigned long size, void *ip) -{ - /* note: fault injection handled in common function */ - return __nvgpu_kmalloc(g, size, ip); -} - -void *__nvgpu_vzalloc(struct gk20a *g, unsigned long size, void *ip) -{ - /* note: fault injection handled in common function */ - return __nvgpu_kzalloc(g, size, ip); -} - void __nvgpu_vfree(struct gk20a *g, void *addr) { - /* note: fault injection handled in common function */ __nvgpu_kfree(g, addr); } void *__nvgpu_big_alloc(struct gk20a *g, size_t size, bool clear) { - /* - * Since in userspace vmalloc() == kmalloc() == malloc() we can just - * reuse k[zm]alloc() for this. - * - * note: fault injection handled in common function - */ - return clear ? - __nvgpu_kzalloc(g, size, _NVGPU_GET_IP_) : - __nvgpu_kmalloc(g, size, _NVGPU_GET_IP_); + if (clear) { + return nvgpu_kzalloc(g, size); + } else { + return nvgpu_kmalloc(g, size); + } } void nvgpu_big_free(struct gk20a *g, void *p) @@ -158,15 +171,15 @@ void nvgpu_big_free(struct gk20a *g, void *p) int nvgpu_kmem_init(struct gk20a *g) { +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&kmem_fi)) { return -ENOMEM; } - +#endif /* Nothing to init at the moment. */ return 0; } void nvgpu_kmem_fini(struct gk20a *g, int flags) { - } diff --git a/drivers/gpu/nvgpu/os/posix/posix-dma.c b/drivers/gpu/nvgpu/os/posix/posix-dma.c index 1fa25f4c1..d65eaaeac 100644 --- a/drivers/gpu/nvgpu/os/posix/posix-dma.c +++ b/drivers/gpu/nvgpu/os/posix/posix-dma.c @@ -69,11 +69,11 @@ static int __nvgpu_do_dma_alloc(struct gk20a *g, unsigned long flags, enum nvgpu_aperture ap) { void *memory; - +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&dma_fi)) { return -ENOMEM; } - +#endif memory = malloc(PAGE_ALIGN(size)); if (memory == NULL) { @@ -119,11 +119,11 @@ int nvgpu_dma_alloc_flags_vid_at(struct gk20a *g, unsigned long flags, { u64 addr; int err; - +#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT if (nvgpu_posix_fault_injection_handle_call(&dma_fi)) { return -ENOMEM; } - +#endif g->ops.fb.get_vidmem_size = mock_fb_get_vidmem_size; nvgpu_set_enabled(g, NVGPU_MM_UNIFIED_MEMORY, false); diff --git a/userspace/Makefile.tmk b/userspace/Makefile.tmk index fdee10841..2475e11ac 100644 --- a/userspace/Makefile.tmk +++ b/userspace/Makefile.tmk @@ -125,7 +125,8 @@ NV_COMPONENT_CFLAGS += \ -DCONFIG_GK20A_VIDMEM=1 \ -DCONFIG_PCI_MSI \ -DCONFIG_SUPPORT_PMU_PSTATE \ - -DCONFIG_TEGRA_NVLINK + -DCONFIG_TEGRA_NVLINK \ + -DNVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT NV_COMPONENT_SYSTEMIMAGE_DIR := $(NV_SYSTEMIMAGE_TEST_EXECUTABLE_DIR)/nvgpu_unit/ systemimage:: $(NV_COMPONENT_SYSTEMIMAGE_DIR)