Started implementing channels

This commit is contained in:
plutooo 2018-02-26 08:43:06 +01:00 committed by fincs
parent 9a759c26f5
commit 61ebd9ddfd
8 changed files with 112 additions and 24 deletions

View File

@ -74,6 +74,7 @@ extern "C" {
#include "switch/nvidia/ioctl.h"
#include "switch/nvidia/buffer.h"
#include "switch/nvidia/address_space.h"
#include "switch/nvidia/channel.h"
#include "switch/audio/driver.h"

View File

@ -1,7 +1,8 @@
#pragma once
typedef struct {
u32 fd;
u32 fd;
bool has_init;
} NvAddressSpace;
typedef enum {
@ -13,10 +14,13 @@ typedef u64 iova_t;
Result nvasCreate(NvAddressSpace* a);
Result nvasClose(NvAddressSpace* a);
void nvasClose(NvAddressSpace* a);
Result nvasReserveAlign(NvAddressSpace* a, NvPageSize align, u32 pages, NvPageSize page_sz, iova_t* iova_out);
Result nvasReserveAtFixedAddr(NvAddressSpace* a, iova_t addr, u32 pages, NvPageSize page_sz);
Result nvasReserveFull(NvAddressSpace* a);
Result nvasMapBuffer(NvAddressSpace* a, NvBuffer* buffer, NvBufferKind kind, iova_t* iova_out);
struct NvChannel;
Result nvasBindToChannel(NvAddressSpace* a, struct NvChannel* channel);

View File

@ -6,6 +6,7 @@ typedef struct {
u32 fd;
u32 size;
void* ptr;
bool has_init;
} NvBuffer;
typedef enum {

View File

@ -0,0 +1,9 @@
#pragma once
typedef struct NvChannel {
u32 fd;
bool has_init;
} NvChannel;
Result nvchannelCreate(NvChannel* c, const char* dev);
void nvchannelClose(NvChannel* c);

View File

@ -2,27 +2,33 @@
Result nvasCreate(NvAddressSpace* a)
{
Result rc = nvOpen(&a->fd, "/dev/nvhost-as-gpu");
Result rc;
a->has_init = true;
rc = nvOpen(&a->fd, "/dev/nvhost-as-gpu");
if (R_FAILED(rc))
a->fd = -1;
if (R_SUCCEEDED(rc))
{
rc = nvioctlNvhostAsGpu_InitializeEx(a->fd, 1, 0x100);
if (R_FAILED(rc))
nvasClose(a);
}
if (R_FAILED(rc))
nvasClose(a);
return rc;
}
Result nvasClose(NvAddressSpace* a)
void nvasClose(NvAddressSpace* a)
{
Result rc;
if (!a->has_init)
return;
if (a->fd != -1)
nvClose(a->fd);
rc = nvClose(a->fd);
a->fd = -1;
return rc;
}
Result nvasReserveAlign(NvAddressSpace* a, NvPageSize align, u32 pages, NvPageSize page_sz, iova_t* iova_out) {
@ -40,3 +46,7 @@ Result nvasReserveFull(NvAddressSpace* a) {
Result nvasMapBuffer(NvAddressSpace* a, NvBuffer* buffer, NvBufferKind kind, iova_t* iova_out) {
return nvioctlNvhostAsGpu_MapBufferEx(a->fd, 0, kind, buffer->fd, 0, 0, buffer->size, 0, iova_out);
}
Result nvasBindToChannel(NvAddressSpace* a, NvChannel* channel) {
return nvioctlNvhostAsGpu_BindChannel(a->fd, channel->fd);
}

View File

@ -19,6 +19,7 @@ static Result _nvbufCreate(NvBuffer* m, size_t size, u32 flags, u32 align, NvBuf
{
Result rc;
m->has_init = true;
m->size = size;
m->fd = -1;
m->ptr = memalign(size, align);
@ -28,19 +29,14 @@ static Result _nvbufCreate(NvBuffer* m, size_t size, u32 flags, u32 align, NvBuf
rc = nvioctlNvmap_Create(g_nvmap_fd, size, &m->fd);
if (R_SUCCEEDED(rc)) {
if (R_FAILED(rc))
m->fd = -1;
if (R_SUCCEEDED(rc))
rc = nvioctlNvmap_Alloc(g_nvmap_fd, m->fd, 0, flags, align, kind, m->ptr);
if (R_FAILED(rc)) {
nvClose(m->fd);
m->fd = -1;
}
}
if (R_FAILED(rc)) {
free(m->ptr);
m->ptr = NULL;
}
if (R_FAILED(rc))
nvbufFree(m);
return rc;
}
@ -55,10 +51,15 @@ Result nvbufCreateRw(NvBuffer* m, size_t size, u32 align, NvBufferKind kind) {
void nvbufFree(NvBuffer* m)
{
if (!m->has_init)
return;
free(m->ptr);
m->ptr = NULL;
nvClose(m->fd);
if (m->fd != -1)
nvClose(m->fd);
m->fd = -1;
}

View File

@ -0,0 +1,29 @@
#include <switch.h>
Result nvchannelCreate(NvChannel* c, const char* dev)
{
Result rc;
c->has_init = true;
rc = nvOpen(&c->fd, dev);
if (R_FAILED(rc))
c->fd = -1;
if (R_FAILED(rc))
nvchannelClose(c);
return rc;
}
void nvchannelClose(NvChannel* c)
{
if (!c->has_init)
return;
if (c->fd != -1)
nvClose(c->fd);
c->fd = -1;
}

View File

@ -0,0 +1,33 @@
#include <switch.h>
typedef struct {
NvAddressSpace addr_space;
NvChannel gpu_channel;
} NvGpu;
Result nvgpuCreate(NvGpu* g)
{
Result rc;
rc = nvchannelCreate(&g->gpu_channel, "/dev/nvhost-gpu");
if (R_SUCCEEDED(rc))
rc = nvasCreate(&g->addr_space);
if (R_SUCCEEDED(rc))
rc = nvasReserveFull(&g->addr_space);
if (R_SUCCEEDED(rc))
rc = nvasBindToChannel(&g->addr_space, &g->gpu_channel);
if (R_FAILED(rc))
nvgpuClose(g);
return rc;
}
void nvgpuClose(NvGpu* g)
{
nvasClose(&g->addr_space);
nvchannelClose(&g->gpu_channel);
}