mirror of
https://github.com/genodelabs/genode.git
synced 2025-06-12 12:18:22 +00:00
libdrm/lima: refactor session and context handling
Merge 'Gpu_session' and 'Gpu_context' and create the main session as context. Issue #4760.
This commit is contained in:
committed by
Christian Helmuth
parent
da32849ac2
commit
909b25db57
@ -273,21 +273,54 @@ class Lima::Call
|
|||||||
|
|
||||||
struct Gpu_context
|
struct Gpu_context
|
||||||
{
|
{
|
||||||
int const fd;
|
private:
|
||||||
unsigned long const _gpu_id;
|
|
||||||
|
|
||||||
Gpu::Connection &_gpu { *vfs_gpu_connection(_gpu_id) };
|
/*
|
||||||
|
* Noncopyable
|
||||||
|
*/
|
||||||
|
Gpu_context(Gpu_context const &) = delete;
|
||||||
|
Gpu_context &operator=(Gpu_context const &) = delete;
|
||||||
|
|
||||||
using Id_space = Genode::Id_space<Gpu_context>;
|
static int _open_gpu()
|
||||||
Id_space::Element const _elem;
|
|
||||||
|
|
||||||
Gpu_context(int fd, unsigned long gpu,
|
|
||||||
Genode::Id_space<Gpu_context> &space)
|
|
||||||
: fd { fd }, _gpu_id { gpu }, _elem { *this, space } { }
|
|
||||||
|
|
||||||
virtual ~Gpu_context()
|
|
||||||
{
|
{
|
||||||
|
int const fd = ::open("/dev/gpu", 0);
|
||||||
|
if (fd < 0) {
|
||||||
|
Genode::error("Failed to open '/dev/gpu': ",
|
||||||
|
"try configure '<gpu>' in 'dev' directory of VFS'");
|
||||||
|
throw Gpu::Session::Invalid_state();
|
||||||
|
}
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned long _stat_gpu(int fd)
|
||||||
|
{
|
||||||
|
struct ::stat buf;
|
||||||
|
if (::fstat(fd, &buf) < 0) {
|
||||||
|
Genode::error("Could not stat '/dev/gpu'");
|
||||||
::close(fd);
|
::close(fd);
|
||||||
|
throw Gpu::Session::Invalid_state();
|
||||||
|
}
|
||||||
|
return buf.st_ino;
|
||||||
|
}
|
||||||
|
|
||||||
|
int const _fd;
|
||||||
|
unsigned long const _id;
|
||||||
|
|
||||||
|
Genode::Id_space<Gpu_context>::Element const _elem;
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Gpu_context(Genode::Id_space<Gpu_context> &space)
|
||||||
|
:
|
||||||
|
_fd { _open_gpu() },
|
||||||
|
_id { _stat_gpu(_fd) },
|
||||||
|
_elem { *this, space }
|
||||||
|
{ }
|
||||||
|
|
||||||
|
~Gpu_context()
|
||||||
|
{
|
||||||
|
::close(_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long id() const
|
unsigned long id() const
|
||||||
@ -297,34 +330,18 @@ class Lima::Call
|
|||||||
|
|
||||||
Gpu::Connection& gpu()
|
Gpu::Connection& gpu()
|
||||||
{
|
{
|
||||||
return _gpu;
|
return *vfs_gpu_connection(_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd() const
|
||||||
|
{
|
||||||
|
return _fd;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using Gpu_context_space = Genode::Id_space<Gpu_context>;
|
using Gpu_context_space = Genode::Id_space<Gpu_context>;
|
||||||
Gpu_context_space _gpu_context_space { };
|
Gpu_context_space _gpu_context_space { };
|
||||||
|
|
||||||
Gpu_context &_create_ctx()
|
|
||||||
{
|
|
||||||
int const fd = ::open("/dev/gpu", 0);
|
|
||||||
if (fd < 0) {
|
|
||||||
Genode::error("Failed to open '/dev/gpu': ",
|
|
||||||
"try configure '<gpu>' in 'dev' directory of VFS'");
|
|
||||||
throw Gpu::Session::Invalid_state();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ::stat buf;
|
|
||||||
if (::fstat(fd, &buf) < 0) {
|
|
||||||
Genode::error("Could not stat '/dev/gpu'");
|
|
||||||
::close(fd);
|
|
||||||
throw Gpu::Session::Invalid_state();
|
|
||||||
}
|
|
||||||
Gpu_context * context =
|
|
||||||
new (_heap) Gpu_context(fd, buf.st_ino, _gpu_context_space);
|
|
||||||
|
|
||||||
return *context;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Syncobj
|
struct Syncobj
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@ -371,47 +388,9 @@ class Lima::Call
|
|||||||
};
|
};
|
||||||
Genode::Id_space<Syncobj> _syncobj_space { };
|
Genode::Id_space<Syncobj> _syncobj_space { };
|
||||||
|
|
||||||
struct Gpu_session
|
Gpu_context _main_ctx { _gpu_context_space };
|
||||||
{
|
|
||||||
int const fd;
|
|
||||||
unsigned long const id;
|
|
||||||
|
|
||||||
Gpu_session(int fd, unsigned long id)
|
Gpu::Connection &_gpu { _main_ctx.gpu() };
|
||||||
: fd { fd }, id { id } { }
|
|
||||||
|
|
||||||
virtual ~Gpu_session()
|
|
||||||
{
|
|
||||||
::close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
Gpu::Connection &gpu()
|
|
||||||
{
|
|
||||||
return *vfs_gpu_connection(id);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Gpu_session _open_gpu()
|
|
||||||
{
|
|
||||||
int const fd = ::open("/dev/gpu", 0);
|
|
||||||
if (fd < 0) {
|
|
||||||
Genode::error("Failed to open '/dev/gpu': ",
|
|
||||||
"try configure '<gpu>' in 'dev' directory of VFS'");
|
|
||||||
throw Gpu::Session::Invalid_state();
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ::stat buf;
|
|
||||||
if (::fstat(fd, &buf) < 0) {
|
|
||||||
Genode::error("Could not stat '/dev/gpu'");
|
|
||||||
::close(fd);
|
|
||||||
throw Gpu::Session::Invalid_state();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Gpu_session { fd, buf.st_ino };
|
|
||||||
}
|
|
||||||
|
|
||||||
Gpu_session _gpu_session { _open_gpu() };
|
|
||||||
|
|
||||||
Gpu::Connection &_gpu { _gpu_session.gpu() };
|
|
||||||
Gpu::Info_lima const &_gpu_info {
|
Gpu::Info_lima const &_gpu_info {
|
||||||
*_gpu.attached_info<Gpu::Info_lima>() };
|
*_gpu.attached_info<Gpu::Info_lima>() };
|
||||||
|
|
||||||
@ -428,11 +407,11 @@ class Lima::Call
|
|||||||
{
|
{
|
||||||
Buffer_id const id { .value = handle };
|
Buffer_id const id { .value = handle };
|
||||||
do {
|
do {
|
||||||
if (_gpu.set_tiling_gpu(id, 0, op))
|
if (_main_ctx.gpu().set_tiling(id, 0, op))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
char buf;
|
char buf;
|
||||||
(void)::read(_gpu_session.fd, &buf, sizeof(buf));
|
(void)::read(_main_ctx.fd(), &buf, sizeof(buf));
|
||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -455,7 +434,7 @@ class Lima::Call
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
char buf;
|
char buf;
|
||||||
(void)::read(gc.fd, &buf, sizeof(buf));
|
(void)::read(gc.fd(), &buf, sizeof(buf));
|
||||||
} while (true);
|
} while (true);
|
||||||
};
|
};
|
||||||
_syncobj_space.apply<Syncobj>(syncobj_id, wait);
|
_syncobj_space.apply<Syncobj>(syncobj_id, wait);
|
||||||
@ -570,7 +549,7 @@ class Lima::Call
|
|||||||
|
|
||||||
int _drm_lima_gem_submit(drm_lima_gem_submit &arg)
|
int _drm_lima_gem_submit(drm_lima_gem_submit &arg)
|
||||||
{
|
{
|
||||||
Gpu_context::Id_space::Id ctx_id { .value = arg.ctx };
|
Gpu_context_space::Id ctx_id { .value = arg.ctx };
|
||||||
|
|
||||||
Syncobj::Id_space::Id syncobj_id { .value = arg.out_sync };
|
Syncobj::Id_space::Id syncobj_id { .value = arg.out_sync };
|
||||||
|
|
||||||
@ -634,9 +613,10 @@ class Lima::Call
|
|||||||
int _drm_lima_ctx_create(drm_lima_ctx_create &arg)
|
int _drm_lima_ctx_create(drm_lima_ctx_create &arg)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
Gpu_context &ctx = _create_ctx();
|
Gpu_context * ctx =
|
||||||
|
new (_heap) Gpu_context(_gpu_context_space);
|
||||||
|
|
||||||
arg.id = ctx.id();
|
arg.id = ctx->id();
|
||||||
return 0;
|
return 0;
|
||||||
} catch (... /* intentional catch-all ... */) {
|
} catch (... /* intentional catch-all ... */) {
|
||||||
/* ... as the lima GPU driver will not throw */
|
/* ... as the lima GPU driver will not throw */
|
||||||
@ -646,7 +626,7 @@ class Lima::Call
|
|||||||
|
|
||||||
int _drm_lima_ctx_free(drm_lima_ctx_free &arg)
|
int _drm_lima_ctx_free(drm_lima_ctx_free &arg)
|
||||||
{
|
{
|
||||||
Gpu_context::Id_space::Id id { .value = arg.id };
|
Gpu_context_space::Id id { .value = arg.id };
|
||||||
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
auto free_ctx = [&] (Gpu_context &ctx) {
|
auto free_ctx = [&] (Gpu_context &ctx) {
|
||||||
|
Reference in New Issue
Block a user