mirror of
git://nv-tegra.nvidia.com/linux-nvgpu.git
synced 2025-12-23 18:16:01 +03:00
Not sure if there's an actual bug or JIRA filed for this, but the change here fixes a long standing bug in the MM code for unit tests. Te GMMU programming code verifies that the CPU _physical_ address programmed into the GMMU PDE0 is a valid Tegra SoC CPU physical address. That means that it's not too large a value. The POSIX imlementation of the nvgpu_mem related code used the CPU virtual address as the "phys" address. Obviously, in userspace, there's no access to physical addresses, so in some sense it's a meaningless function. But the GMMU code does care, as described above, about the format of the address. The fix is simple enough: since the nvgpu_mem_get_addr() and nvgpu_mem_get_phys_addr() values shouldn't actually be accessed by the driver anyway (they could be vidmem addresses or IOVA addresses in real life) ANDing them with 0xffffffff (e.g 32 bits) truncates the potentially problematic CPU virtual address bits returned by malloc() in the POSIX environment. With this, a run of the unit test framework passes for me locally on my Ubuntu 18 machine. Also, clean up a few whitespace issues I noticed while I debugged this and fix another long standing bug where the NVGPU_DEFAULT_DBG_MASK was not being copied to g->log_mask during gk20a struct init. Change-Id: Ie92d3bd26240d194183b4376973d4d32cb6f9b8f Signed-off-by: Alex Waterman <alexw@nvidia.com> Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2395953 Tested-by: mobile promotions <svcmobile_promotions@nvidia.com> Reviewed-by: automaticguardword <automaticguardword@nvidia.com> Reviewed-by: svc-mobile-coverity <svc-mobile-coverity@nvidia.com> Reviewed-by: svc-mobile-cert <svc-mobile-cert@nvidia.com> Reviewed-by: Vijayakumar Subbu <vsubbu@nvidia.com> Reviewed-by: Konsta Holtta <kholtta@nvidia.com> Reviewed-by: mobile promotions <svcmobile_promotions@nvidia.com> GVS: Gerrit_Virtual_Submit
305 lines
7.2 KiB
C
305 lines
7.2 KiB
C
/*
|
|
* Copyright (c) 2018-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 <unistd.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
|
|
#include <nvgpu/bug.h>
|
|
#include <nvgpu/types.h>
|
|
#include <nvgpu/atomic.h>
|
|
#include <nvgpu/nvgpu_common.h>
|
|
#include <nvgpu/nvgpu_init.h>
|
|
#include <nvgpu/hal_init.h>
|
|
#include <nvgpu/os_sched.h>
|
|
#include <nvgpu/gk20a.h>
|
|
#include <nvgpu/enabled.h>
|
|
|
|
#include <nvgpu/posix/probe.h>
|
|
#include <nvgpu/posix/mock-regs.h>
|
|
#include <nvgpu/posix/io.h>
|
|
|
|
#include "os_posix.h"
|
|
|
|
#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT
|
|
#include <nvgpu/posix/posix-fault-injection.h>
|
|
#endif
|
|
|
|
#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT
|
|
struct nvgpu_posix_fault_inj *nvgpu_nvgpu_get_fault_injection(void)
|
|
{
|
|
struct nvgpu_posix_fault_inj_container *c =
|
|
nvgpu_posix_fault_injection_get_container();
|
|
|
|
return &c->nvgpu_fi;
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* Write callback. Forward the write access to the mock IO framework.
|
|
*/
|
|
static void writel_access_reg_fn(struct gk20a *g,
|
|
struct nvgpu_reg_access *access)
|
|
{
|
|
nvgpu_posix_io_writel_reg_space(g, access->addr, access->value);
|
|
}
|
|
|
|
/*
|
|
* Read callback. Get the register value from the mock IO framework.
|
|
*/
|
|
static void readl_access_reg_fn(struct gk20a *g,
|
|
struct nvgpu_reg_access *access)
|
|
{
|
|
access->value = nvgpu_posix_io_readl_reg_space(g, access->addr);
|
|
}
|
|
|
|
static struct nvgpu_posix_io_callbacks default_posix_reg_callbacks = {
|
|
/* Write APIs all can use the same accessor. */
|
|
.writel = writel_access_reg_fn,
|
|
.writel_check = writel_access_reg_fn,
|
|
.bar1_writel = writel_access_reg_fn,
|
|
.usermode_writel = writel_access_reg_fn,
|
|
|
|
/* Likewise for the read APIs. */
|
|
.__readl = readl_access_reg_fn,
|
|
.readl = readl_access_reg_fn,
|
|
.bar1_readl = readl_access_reg_fn,
|
|
};
|
|
|
|
/*
|
|
* Somewhat meaningless in userspace...
|
|
*/
|
|
void nvgpu_kernel_restart(void *cmd)
|
|
{
|
|
BUG();
|
|
}
|
|
|
|
void nvgpu_start_gpu_idle(struct gk20a *g)
|
|
{
|
|
nvgpu_set_enabled(g, NVGPU_DRIVER_IS_DYING, true);
|
|
}
|
|
|
|
int nvgpu_enable_irqs(struct gk20a *g)
|
|
{
|
|
g->mc.irqs_enabled = true;
|
|
return 0;
|
|
}
|
|
|
|
void nvgpu_disable_irqs(struct gk20a *g)
|
|
{
|
|
g->mc.irqs_enabled = false;
|
|
}
|
|
|
|
void nvgpu_set_power_state(struct gk20a *g, u32 state)
|
|
{
|
|
nvgpu_spinlock_acquire(&g->power_spinlock);
|
|
g->power_on_state = state;
|
|
nvgpu_spinlock_release(&g->power_spinlock);
|
|
}
|
|
|
|
const char *nvgpu_get_power_state(struct gk20a *g)
|
|
{
|
|
const char *str = NULL;
|
|
u32 state;
|
|
|
|
nvgpu_spinlock_acquire(&g->power_spinlock);
|
|
state = g->power_on_state;
|
|
nvgpu_spinlock_release(&g->power_spinlock);
|
|
|
|
switch (state) {
|
|
case NVGPU_STATE_POWERED_OFF:
|
|
str = "off";
|
|
break;
|
|
case NVGPU_STATE_POWERING_ON:
|
|
str = "powering on";
|
|
break;
|
|
case NVGPU_STATE_POWERED_ON:
|
|
str = "on";
|
|
break;
|
|
default:
|
|
nvgpu_err(g, "Invalid nvgpu power state.");
|
|
break;
|
|
}
|
|
|
|
return str;
|
|
}
|
|
|
|
bool nvgpu_is_powered_on(struct gk20a *g)
|
|
{
|
|
u32 power_on;
|
|
|
|
nvgpu_spinlock_acquire(&g->power_spinlock);
|
|
power_on = g->power_on_state;
|
|
nvgpu_spinlock_release(&g->power_spinlock);
|
|
|
|
return power_on == NVGPU_STATE_POWERED_ON;
|
|
}
|
|
|
|
bool nvgpu_is_powered_off(struct gk20a *g)
|
|
{
|
|
u32 power_on;
|
|
|
|
nvgpu_spinlock_acquire(&g->power_spinlock);
|
|
power_on = g->power_on_state;
|
|
nvgpu_spinlock_release(&g->power_spinlock);
|
|
|
|
return power_on == NVGPU_STATE_POWERED_OFF;
|
|
}
|
|
|
|
/*
|
|
* We have no runtime PM stuff in userspace so these are really just noops.
|
|
*/
|
|
void gk20a_busy_noresume(struct gk20a *g)
|
|
{
|
|
}
|
|
|
|
void gk20a_idle_nosuspend(struct gk20a *g)
|
|
{
|
|
}
|
|
|
|
int gk20a_busy(struct gk20a *g)
|
|
{
|
|
#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT
|
|
if (nvgpu_posix_fault_injection_handle_call(
|
|
nvgpu_nvgpu_get_fault_injection())) {
|
|
return -ENODEV;
|
|
}
|
|
#endif
|
|
nvgpu_atomic_inc(&g->usage_count);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void gk20a_idle(struct gk20a *g)
|
|
{
|
|
nvgpu_atomic_dec(&g->usage_count);
|
|
}
|
|
|
|
static void nvgpu_posix_load_regs(struct gk20a *g)
|
|
{
|
|
u32 i;
|
|
int err;
|
|
struct nvgpu_mock_iospace space;
|
|
struct nvgpu_posix_io_reg_space *regs;
|
|
|
|
for (i = 0; i < MOCK_REGS_LAST; i++) {
|
|
err = nvgpu_get_mock_reglist(g, i, &space);
|
|
if (err) {
|
|
nvgpu_err(g, "Unknown IO regspace: %d; ignoring.", i);
|
|
continue;
|
|
}
|
|
|
|
err = nvgpu_posix_io_add_reg_space(g, space.base, space.size);
|
|
nvgpu_assert(err == 0);
|
|
|
|
regs = nvgpu_posix_io_get_reg_space(g, space.base);
|
|
nvgpu_assert(regs != NULL);
|
|
|
|
if (space.data != NULL) {
|
|
memcpy(regs->data, space.data, space.size);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* This function aims to initialize enough stuff to make unit testing worth
|
|
* while. There are several interfaces and APIs that rely on the struct gk20a's
|
|
* state in order to function: logging, for example, but there are many other
|
|
* things, too.
|
|
*
|
|
* Initialize as much of that as possible here. This is meant to be equivalent
|
|
* to the kernel space driver's probe function.
|
|
*/
|
|
struct gk20a *nvgpu_posix_probe(void)
|
|
{
|
|
struct gk20a *g;
|
|
struct nvgpu_os_posix *p;
|
|
|
|
#ifdef NVGPU_UNITTEST_FAULT_INJECTION_ENABLEMENT
|
|
if (nvgpu_posix_fault_injection_handle_call(
|
|
nvgpu_nvgpu_get_fault_injection())) {
|
|
return NULL;
|
|
}
|
|
#endif
|
|
|
|
p = malloc(sizeof(*p));
|
|
|
|
if (p == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
(void) memset(p, 0, sizeof(*p));
|
|
|
|
g = &p->g;
|
|
g->log_mask = NVGPU_DEFAULT_DBG_MASK;
|
|
g->mm.g = g;
|
|
|
|
if (nvgpu_kmem_init(g) != 0) {
|
|
goto fail_kmem;
|
|
}
|
|
|
|
if (nvgpu_init_enabled_flags(g) != 0) {
|
|
goto fail_enabled_flags;
|
|
}
|
|
|
|
/*
|
|
* Initialize a bunch of gv11b register values.
|
|
*/
|
|
nvgpu_posix_io_init_reg_space(g);
|
|
nvgpu_posix_load_regs(g);
|
|
|
|
/*
|
|
* Set up some default register IO callbacks that basically all
|
|
* unit tests will be OK with. Unit tests that wish to override this
|
|
* may do so.
|
|
*
|
|
* This needs to happen before the nvgpu_detect_chip() call below
|
|
* otherise we bug out when trying to do a register read.
|
|
*/
|
|
(void)nvgpu_posix_register_io(g, &default_posix_reg_callbacks);
|
|
|
|
/*
|
|
* Detect chip based on the regs we filled above. Most unit tests
|
|
* will be fine with this; a few may have to undo a little bit of it
|
|
* in roder to fully test the nvgpu_detect_chip() function.
|
|
*/
|
|
nvgpu_assert(nvgpu_detect_chip(g) == 0);
|
|
|
|
return g;
|
|
|
|
fail_enabled_flags:
|
|
nvgpu_kmem_fini(g, 0);
|
|
fail_kmem:
|
|
free(p);
|
|
|
|
return NULL;
|
|
}
|
|
|
|
void nvgpu_posix_cleanup(struct gk20a *g)
|
|
{
|
|
struct nvgpu_os_posix *p = nvgpu_os_posix_from_gk20a(g);
|
|
|
|
nvgpu_kmem_fini(g, 0);
|
|
nvgpu_free_enabled_flags(g);
|
|
free(p);
|
|
}
|