NVDRV: Implement sessions and initial implementation of SMMU

This commit is contained in:
Fernando Sahmkow 2023-12-24 18:20:02 +01:00 • committed by Liam
commit eb4f7f35ea
37 changed files with 260 additions and 100 deletions

View file

@ -19,6 +19,8 @@
#include "core/hle/ipc.h"
#include "core/hle/kernel/k_handle_table.h"
#include "core/hle/kernel/svc_common.h"
#include "core/hle/kernel/k_auto_object.h"
#include "core/hle/kernel/k_handle_table.h"
union Result;
@ -41,6 +43,8 @@ class KernelCore;
class KHandleTable;
class KProcess;
class KServerSession;
template <typename T>
class KScopedAutoObject;
class KThread;
} // namespace Kernel
@ -373,6 +377,10 @@ public:
return nullptr;
}
Kernel::KScopedAutoObject<Kernel::KAutoObject> GetObjectFromHandle(u32 handle) {
return GetClientHandleTable().GetObjectForIpc(handle, thread);
}
[[nodiscard]] std::shared_ptr<SessionRequestManager> GetManager() const {
return manager.lock();
}

View file

@ -2,19 +2,30 @@
// SPDX-FileCopyrightText: 2022 Skyline Team and Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
#include <atomic>
#include <deque>
#include <mutex>
#include "core/hle/kernel/k_process.h"
#include "core/hle/service/nvdrv/core/container.h"
#include "core/hle/service/nvdrv/core/nvmap.h"
#include "core/hle/service/nvdrv/core/syncpoint_manager.h"
#include "core/memory.h"
#include "video_core/host1x/host1x.h"
namespace Service::Nvidia::NvCore {
struct ContainerImpl {
explicit ContainerImpl(Tegra::Host1x::Host1x& host1x_)
: file{host1x_}, manager{host1x_}, device_file_data{} {}
: host1x{host1x_}, file{host1x_}, manager{host1x_}, device_file_data{} {}
Tegra::Host1x::Host1x& host1x;
NvMap file;
SyncpointManager manager;
Container::Host1xDeviceFileData device_file_data;
std::deque<Session> sessions;
size_t new_ids{};
std::deque<size_t> id_pool;
std::mutex session_guard;
};
Container::Container(Tegra::Host1x::Host1x& host1x_) {
@ -23,6 +34,37 @@ Container::Container(Tegra::Host1x::Host1x& host1x_) {
Container::~Container() = default;
size_t Container::OpenSession(Kernel::KProcess* process) {
std::scoped_lock lk(impl->session_guard);
size_t new_id{};
auto* memory_interface = &process->GetMemory();
auto& smmu = impl->host1x.MemoryManager();
auto smmu_id = smmu.RegisterProcess(memory_interface);
if (!impl->id_pool.empty()) {
new_id = impl->id_pool.front();
impl->id_pool.pop_front();
impl->sessions[new_id] = Session{new_id, process, smmu_id};
} else {
impl->sessions.emplace_back(new_id, process, smmu_id);
new_id = impl->new_ids++;
}
LOG_CRITICAL(Debug, "Created Session {}", new_id);
return new_id;
}
void Container::CloseSession(size_t id) {
std::scoped_lock lk(impl->session_guard);
auto& smmu = impl->host1x.MemoryManager();
smmu.UnregisterProcess(impl->sessions[id].smmu_id);
impl->id_pool.emplace_front(id);
LOG_CRITICAL(Debug, "Closed Session {}", id);
}
Session* Container::GetSession(size_t id) {
std::atomic_thread_fence(std::memory_order_acquire);
return &impl->sessions[id];
}
NvMap& Container::GetNvMapFile() {
return impl->file;
}

View file

@ -10,6 +10,10 @@
#include "core/hle/service/nvdrv/nvdata.h"
namespace Kernel {
class KProcess;
}
namespace Tegra::Host1x {
class Host1x;
} // namespace Tegra::Host1x
@ -21,11 +25,22 @@ class SyncpointManager;
struct ContainerImpl;
struct Session {
size_t id;
Kernel::KProcess* process;
size_t smmu_id;
};
class Container {
public:
explicit Container(Tegra::Host1x::Host1x& host1x);
~Container();
size_t OpenSession(Kernel::KProcess* process);
void CloseSession(size_t id);
Session* GetSession(size_t id);
NvMap& GetNvMapFile();
const NvMap& GetNvMapFile() const;

View file

@ -18,8 +18,6 @@ NvMap::Handle::Handle(u64 size_, Id id_)
}
NvResult NvMap::Handle::Alloc(Flags pFlags, u32 pAlign, u8 pKind, u64 pAddress) {
std::scoped_lock lock(mutex);
// Handles cannot be allocated twice
if (allocated) {
return NvResult::AccessDenied;
@ -79,10 +77,11 @@ void NvMap::UnmapHandle(Handle& handle_description) {
}
// Free and unmap the handle from the SMMU
host1x.MemoryManager().Unmap(static_cast<GPUVAddr>(handle_description.pin_virt_address),
handle_description.aligned_size);
host1x.Allocator().Free(handle_description.pin_virt_address,
static_cast<u32>(handle_description.aligned_size));
auto& smmu = host1x.MemoryManager();
smmu.Unmap(static_cast<DAddr>(handle_description.pin_virt_address),
handle_description.aligned_size);
smmu.Free(handle_description.pin_virt_address,
static_cast<size_t>(handle_description.aligned_size));
handle_description.pin_virt_address = 0;
}
@ -133,7 +132,32 @@ VAddr NvMap::GetHandleAddress(Handle::Id handle) {
}
}
u32 NvMap::PinHandle(NvMap::Handle::Id handle) {
NvResult NvMap::AllocateHandle(Handle::Id handle, Handle::Flags pFlags, u32 pAlign, u8 pKind, u64 pAddress, size_t session_id) {
auto handle_description{GetHandle(handle)};
if (!handle_description) [[unlikely]] {
return NvResult::BadParameter;
}
if (handle_description->allocated) [[unlikely]] {
return NvResult::InsufficientMemory;
}
std::scoped_lock lock(handle_description->mutex);
NvResult result = handle_description->Alloc(pFlags, pAlign, pKind, pAddress);
if (result != NvResult::Success) {
return result;
}
auto& smmu = host1x.MemoryManager();
size_t total_size = static_cast<size_t>(handle_description->aligned_size);
handle_description->d_address = smmu.Allocate(total_size);
if (handle_description->d_address == 0) {
return NvResult::InsufficientMemory;
}
smmu.Map(handle_description->d_address, handle_description->address, total_size, session_id);
return NvResult::Success;
}
u32 NvMap::PinHandle(NvMap::Handle::Id handle, size_t session_id) {
auto handle_description{GetHandle(handle)};
if (!handle_description) [[unlikely]] {
return 0;
@ -157,11 +181,10 @@ u32 NvMap::PinHandle(NvMap::Handle::Id handle) {
}
// If not then allocate some space and map it
u32 address{};
auto& smmu_allocator = host1x.Allocator();
auto& smmu_memory_manager = host1x.MemoryManager();
while ((address = smmu_allocator.Allocate(
static_cast<u32>(handle_description->aligned_size))) == 0) {
DAddr address{};
auto& smmu = host1x.MemoryManager();
while ((address = smmu.AllocatePinned(
static_cast<size_t>(handle_description->aligned_size))) == 0) {
// Free handles until the allocation succeeds
std::scoped_lock queueLock(unmap_queue_lock);
if (auto freeHandleDesc{unmap_queue.front()}) {
@ -175,9 +198,9 @@ u32 NvMap::PinHandle(NvMap::Handle::Id handle) {
}
}
smmu_memory_manager.Map(static_cast<GPUVAddr>(address), handle_description->address,
handle_description->aligned_size);
handle_description->pin_virt_address = address;
smmu.Map(address, handle_description->address, handle_description->aligned_size,
session_id);
handle_description->pin_virt_address = static_cast<u32>(address);
}
handle_description->pins++;
@ -236,6 +259,11 @@ std::optional<NvMap::FreeInfo> NvMap::FreeHandle(Handle::Id handle, bool interna
std::scoped_lock queueLock(unmap_queue_lock);
UnmapHandle(*handle_description);
}
if (handle_description->allocated) {
auto& smmu = host1x.MemoryManager();
smmu.Free(handle_description->d_address, handle_description->aligned_size);
smmu.Unmap(handle_description->d_address, handle_description->aligned_size);
}
handle_description->pins = 0;
}

View file

@ -61,8 +61,10 @@ public:
} flags{};
static_assert(sizeof(Flags) == sizeof(u32));
u64 address{}; //!< The memory location in the guest's AS that this handle corresponds to,
//!< this can also be in the nvdrv tmem
VAddr address{}; //!< The memory location in the guest's AS that this handle corresponds to,
//!< this can also be in the nvdrv tmem
DAddr d_address{}; //!< The memory location in the device's AS that this handle corresponds to,
//!< this can also be in the nvdrv tmem
bool is_shared_mem_mapped{}; //!< If this nvmap has been mapped with the MapSharedMem IPC
//!< call
@ -125,7 +127,15 @@ public:
* number of calls to `UnpinHandle`
* @return The SMMU virtual address that the handle has been mapped to
*/
u32 PinHandle(Handle::Id handle);
u32 PinHandle(Handle::Id handle, size_t session_id);
/**
* @brief Maps a handle into the SMMU address space
* @note This operation is refcounted, the number of calls to this must eventually match the
* number of calls to `UnpinHandle`
* @return The SMMU virtual address that the handle has been mapped to
*/
NvResult AllocateHandle(Handle::Id handle, Handle::Flags pFlags, u32 pAlign, u8 pKind, u64 pAddress, size_t session_id);
/**
* @brief When this has been called an equal number of times to `PinHandle` for the supplied

View file

@ -62,7 +62,7 @@ public:
* Called once a device is opened
* @param fd The device fd
*/
virtual void OnOpen(DeviceFD fd) = 0;
virtual void OnOpen(size_t session_id, DeviceFD fd) = 0;
/**
* Called once a device is closed

View file

@ -35,7 +35,7 @@ NvResult nvdisp_disp0::Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> in
return NvResult::NotImplemented;
}
void nvdisp_disp0::OnOpen(DeviceFD fd) {}
void nvdisp_disp0::OnOpen(size_t session_id, DeviceFD fd) {}
void nvdisp_disp0::OnClose(DeviceFD fd) {}
void nvdisp_disp0::flip(u32 buffer_handle, u32 offset, android::PixelFormat format, u32 width,

View file

@ -32,7 +32,7 @@ public:
NvResult Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> input, std::span<u8> output,
std::span<u8> inline_output) override;
void OnOpen(DeviceFD fd) override;
void OnOpen(size_t session_id, DeviceFD fd) override;
void OnClose(DeviceFD fd) override;
/// Performs a screen flip, drawing the buffer pointed to by the handle.

View file

@ -86,7 +86,7 @@ NvResult nvhost_as_gpu::Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> i
return NvResult::NotImplemented;
}
void nvhost_as_gpu::OnOpen(DeviceFD fd) {}
void nvhost_as_gpu::OnOpen(size_t session_id, DeviceFD fd) {}
void nvhost_as_gpu::OnClose(DeviceFD fd) {}
NvResult nvhost_as_gpu::AllocAsEx(IoctlAllocAsEx& params) {

View file

@ -55,7 +55,7 @@ public:
NvResult Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> input, std::span<u8> output,
std::span<u8> inline_output) override;
void OnOpen(DeviceFD fd) override;
void OnOpen(size_t session_id, DeviceFD fd) override;
void OnClose(DeviceFD fd) override;
Kernel::KEvent* QueryEvent(u32 event_id) override;

View file

@ -76,7 +76,7 @@ NvResult nvhost_ctrl::Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> inp
return NvResult::NotImplemented;
}
void nvhost_ctrl::OnOpen(DeviceFD fd) {}
void nvhost_ctrl::OnOpen(size_t session_id, DeviceFD fd) {}
void nvhost_ctrl::OnClose(DeviceFD fd) {}

View file

@ -32,7 +32,7 @@ public:
NvResult Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> input, std::span<u8> output,
std::span<u8> inline_output) override;
void OnOpen(DeviceFD fd) override;
void OnOpen(size_t session_id, DeviceFD fd) override;
void OnClose(DeviceFD fd) override;
Kernel::KEvent* QueryEvent(u32 event_id) override;

View file

@ -82,7 +82,7 @@ NvResult nvhost_ctrl_gpu::Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8>
return NvResult::NotImplemented;
}
void nvhost_ctrl_gpu::OnOpen(DeviceFD fd) {}
void nvhost_ctrl_gpu::OnOpen(size_t session_id, DeviceFD fd) {}
void nvhost_ctrl_gpu::OnClose(DeviceFD fd) {}
NvResult nvhost_ctrl_gpu::GetCharacteristics1(IoctlCharacteristics& params) {

View file

@ -28,7 +28,7 @@ public:
NvResult Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> input, std::span<u8> output,
std::span<u8> inline_output) override;
void OnOpen(DeviceFD fd) override;
void OnOpen(size_t session_id, DeviceFD fd) override;
void OnClose(DeviceFD fd) override;
Kernel::KEvent* QueryEvent(u32 event_id) override;

View file

@ -120,7 +120,7 @@ NvResult nvhost_gpu::Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> inpu
return NvResult::NotImplemented;
}
void nvhost_gpu::OnOpen(DeviceFD fd) {}
void nvhost_gpu::OnOpen(size_t session_id, DeviceFD fd) {}
void nvhost_gpu::OnClose(DeviceFD fd) {}
NvResult nvhost_gpu::SetNVMAPfd(IoctlSetNvmapFD& params) {

View file

@ -47,7 +47,7 @@ public:
NvResult Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> input, std::span<u8> output,
std::span<u8> inline_output) override;
void OnOpen(DeviceFD fd) override;
void OnOpen(size_t session_id, DeviceFD fd) override;
void OnClose(DeviceFD fd) override;
Kernel::KEvent* QueryEvent(u32 event_id) override;

View file

@ -35,7 +35,7 @@ NvResult nvhost_nvdec::Ioctl1(DeviceFD fd, Ioctl command, std::span<const u8> in
case 0x7:
return WrapFixed(this, &nvhost_nvdec::SetSubmitTimeout, input, output);
case 0x9:
return WrapFixedVariable(this, &nvhost_nvdec::MapBuffer, input, output);
return WrapFixedVariable(this, &nvhost_nvdec::MapBuffer, input, output, fd);
case 0xa:
return WrapFixedVariable(this, &nvhost_nvdec::UnmapBuffer, input, output);
default:
@ -68,9 +68,10 @@ NvResult nvhost_nvdec::Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> in
return NvResult::NotImplemented;
}
void nvhost_nvdec::OnOpen(DeviceFD fd) {
void nvhost_nvdec::OnOpen(size_t session_id, DeviceFD fd) {
LOG_INFO(Service_NVDRV, "NVDEC video stream started");
system.SetNVDECActive(true);
sessions[fd] = session_id;
}
void nvhost_nvdec::OnClose(DeviceFD fd) {
@ -81,6 +82,10 @@ void nvhost_nvdec::OnClose(DeviceFD fd) {
system.GPU().ClearCdmaInstance(iter->second);
}
system.SetNVDECActive(false);
auto it = sessions.find(fd);
if (it != sessions.end()) {
sessions.erase(it);
}
}
} // namespace Service::Nvidia::Devices

View file

@ -20,7 +20,7 @@ public:
NvResult Ioctl3(DeviceFD fd, Ioctl command, std::span<const u8> input, std::span<u8> output,
std::span<u8> inline_output) override;
void OnOpen(DeviceFD fd) override;
void OnOpen(size_t session_id, DeviceFD fd) override;
void OnClose(DeviceFD fd) override;
};

View file

@ -133,10 +133,10 @@ NvResult nvhost_nvdec_common::GetWaitbase(IoctlGetWaitbase& params) {
return NvResult::Success;
}
NvResult nvhost_nvdec_common::MapBuffer(IoctlMapBuffer& params, std::span<MapBufferEntry> entries) {
NvResult nvhost_nvdec_common::MapBuffer(IoctlMapBuffer& params, std::span<MapBufferEntry> entries, DeviceFD fd) {
const size_t num_entries = std::min(params.num_entries, static_cast<u32>(entries.size()));
for (size_t i = 0; i < num_entries; i++) {
entries[i].map_address = nvmap.PinHandle(entries[i].map_handle);
entries[i].map_address = nvmap.PinHandle(entries[i].map_handle, sessions[fd]);
}
return NvResult::Success;

View file

@ -5,6 +5,8 @@
#include <deque>
#include <vector>
#include <unordered_map>
#include "common/common_types.h"
#include "common/swap.h"
#include "core/hle/service/nvdrv/core/syncpoint_manager.h"
@ -111,7 +113,7 @@ protected:
NvResult Submit(IoctlSubmit& params, std::span<u8> input, DeviceFD fd);
NvResult GetSyncpoint(IoctlGetSyncpoint& params);
NvResult GetWaitbase(IoctlGetWaitbase& params);
NvResult MapBuffer(IoctlMapBuffer& params, std::span<MapBufferEntry> entries);
NvResult MapBuffer(IoctlMapBuffer& params, std::span<MapBufferEntry> entries, DeviceFD fd);
NvResult UnmapBuffer(IoctlMapBuffer& params, std::span<MapBufferEntry> entries);
NvResult SetSubmitTimeout(u32 timeout);
@ -125,6 +127,7 @@ protected:
NvCore::NvMap& nvmap;
NvCore::ChannelType channel_type;
std::array<u32, MaxSyncPoints> device_syncpoints{};
std::unordered_map<DeviceFD, size_t> sessions;
};
}; // namespace Devices
} // namespace Service::Nvidia

Some files were not shown because too many files have changed in this diff Show more