2020-04-19 23:04:05 +02:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2020-03-27 20:36:02 +01:00
|
|
|
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
|
|
|
2020-08-09 15:36:47 +02:00
|
|
|
#include <gpu.h>
|
|
|
|
#include <kernel/types/KProcess.h>
|
2020-09-14 16:13:36 +02:00
|
|
|
#include <services/nvdrv/driver.h>
|
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
|
|
|
#include "nvhost_ctrl.h"
|
|
|
|
|
2020-03-24 21:17:31 +01:00
|
|
|
namespace skyline::service::nvdrv::device {
|
2020-08-09 15:36:47 +02:00
|
|
|
NvHostEvent::NvHostEvent(const DeviceState &state) : event(std::make_shared<type::KEvent>(state)) {}
|
|
|
|
|
|
|
|
void NvHostEvent::Signal() {
|
2020-09-26 07:17:57 +02:00
|
|
|
auto oldState{state};
|
2020-08-09 15:36:47 +02:00
|
|
|
state = State::Signaling;
|
|
|
|
|
|
|
|
// This is to ensure that the HOS event isn't signalled when the nvhost event is cancelled
|
|
|
|
if (oldState == State::Waiting)
|
|
|
|
event->Signal();
|
|
|
|
|
|
|
|
state = State::Signaled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NvHostEvent::Cancel(const std::shared_ptr<gpu::GPU> &gpuState) {
|
|
|
|
gpuState->syncpoints.at(fence.id).DeregisterWaiter(waiterId);
|
|
|
|
Signal();
|
|
|
|
event->ResetSignal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NvHostEvent::Wait(const std::shared_ptr<gpu::GPU> &gpuState, const Fence &fence) {
|
|
|
|
waiterId = gpuState->syncpoints.at(fence.id).RegisterWaiter(fence.value, [this] { Signal(); });
|
|
|
|
|
|
|
|
// If waiter ID is zero then the fence has already been hit and was signalled in the call to RegisterWaiter
|
|
|
|
if (waiterId) {
|
|
|
|
this->fence = fence;
|
|
|
|
state = State::Waiting;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-18 02:45:57 +02:00
|
|
|
NvHostCtrl::NvHostCtrl(const DeviceState &state) : NvDevice(state) {}
|
2020-07-14 16:15:28 +02:00
|
|
|
|
2020-08-09 15:36:47 +02:00
|
|
|
u32 NvHostCtrl::FindFreeEvent(u32 syncpointId) {
|
|
|
|
u32 eventIndex{constant::NvHostEventCount}; //!< Holds the index of the last populated event in the event array
|
|
|
|
u32 freeIndex{constant::NvHostEventCount}; //!< Holds the index of the first unused event id
|
|
|
|
|
|
|
|
for (u32 i{}; i < constant::NvHostEventCount; i++) {
|
|
|
|
if (events[i]) {
|
2020-09-26 07:17:57 +02:00
|
|
|
const auto &event{*events[i]};
|
2020-08-09 15:36:47 +02:00
|
|
|
|
|
|
|
if (event.state == NvHostEvent::State::Cancelled || event.state == NvHostEvent::State::Available || event.state == NvHostEvent::State::Signaled) {
|
|
|
|
eventIndex = i;
|
|
|
|
|
|
|
|
// This event is already attached to the requested syncpoint, so use it
|
|
|
|
if (event.fence.id == syncpointId)
|
|
|
|
return eventIndex;
|
|
|
|
}
|
|
|
|
} else if (freeIndex == constant::NvHostEventCount) {
|
|
|
|
freeIndex = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use an unused event if possible
|
|
|
|
if (freeIndex < constant::NvHostEventCount) {
|
|
|
|
events.at(freeIndex) = static_cast<const std::optional<NvHostEvent>>(NvHostEvent(state));
|
|
|
|
return freeIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recycle an existing event if all else fails
|
|
|
|
if (eventIndex < constant::NvHostEventCount)
|
|
|
|
return eventIndex;
|
|
|
|
|
|
|
|
throw exception("Failed to find a free nvhost event!");
|
|
|
|
}
|
|
|
|
|
2020-09-25 02:05:12 +02:00
|
|
|
NvStatus NvHostCtrl::EventWaitImpl(span<u8> buffer, bool async) {
|
2020-08-09 15:36:47 +02:00
|
|
|
struct Data {
|
2020-09-19 16:45:17 +02:00
|
|
|
Fence fence; // In
|
|
|
|
u32 timeout; // In
|
|
|
|
EventValue value; // InOut
|
2020-09-25 02:05:12 +02:00
|
|
|
} &data = buffer.as<Data>();
|
2020-08-09 15:36:47 +02:00
|
|
|
|
2020-09-19 16:45:17 +02:00
|
|
|
if (data.fence.id >= constant::MaxHwSyncpointCount)
|
|
|
|
return NvStatus::BadValue;
|
|
|
|
|
|
|
|
if (data.timeout == 0)
|
|
|
|
return NvStatus::Timeout;
|
2020-08-09 15:36:47 +02:00
|
|
|
|
2020-09-26 07:17:57 +02:00
|
|
|
auto driver{nvdrv::driver.lock()};
|
|
|
|
auto &hostSyncpoint{driver->hostSyncpoint};
|
2020-08-09 15:36:47 +02:00
|
|
|
|
|
|
|
// Check if the syncpoint has already expired using the last known values
|
2020-09-19 16:45:17 +02:00
|
|
|
if (hostSyncpoint.HasSyncpointExpired(data.fence.id, data.fence.value)) {
|
|
|
|
data.value.val = hostSyncpoint.ReadSyncpointMinValue(data.fence.id);
|
|
|
|
return NvStatus::Success;
|
2020-08-09 15:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sync the syncpoint with the GPU then check again
|
2020-09-26 07:17:57 +02:00
|
|
|
auto minVal{hostSyncpoint.UpdateMin(data.fence.id)};
|
2020-09-19 16:45:17 +02:00
|
|
|
if (hostSyncpoint.HasSyncpointExpired(data.fence.id, data.fence.value)) {
|
|
|
|
data.value.val = minVal;
|
|
|
|
return NvStatus::Success;
|
2020-08-09 15:36:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u32 userEventId{};
|
|
|
|
if (async) {
|
2020-09-19 16:45:17 +02:00
|
|
|
if (data.value.val >= constant::NvHostEventCount || !events.at(data.value.val))
|
|
|
|
return NvStatus::BadValue;
|
2020-08-09 15:36:47 +02:00
|
|
|
|
2020-09-19 16:45:17 +02:00
|
|
|
userEventId = data.value.val;
|
2020-08-09 15:36:47 +02:00
|
|
|
} else {
|
2020-09-19 16:45:17 +02:00
|
|
|
data.fence.value = 0;
|
2020-08-09 15:36:47 +02:00
|
|
|
|
2020-09-19 16:45:17 +02:00
|
|
|
userEventId = FindFreeEvent(data.fence.id);
|
2020-08-09 15:36:47 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 10:40:42 +01:00
|
|
|
auto &event{*events.at(userEventId)};
|
2020-09-19 16:45:17 +02:00
|
|
|
if (event.state == NvHostEvent::State::Cancelled || event.state == NvHostEvent::State::Available || event.state == NvHostEvent::State::Signaled) {
|
2020-11-17 01:48:41 +01:00
|
|
|
state.logger->Debug("Waiting on nvhost event: {} with fence: {}", userEventId, data.fence.id);
|
2020-09-19 16:45:17 +02:00
|
|
|
event.Wait(state.gpu, data.fence);
|
2020-08-09 15:36:47 +02:00
|
|
|
|
2020-09-19 16:45:17 +02:00
|
|
|
data.value.val = 0;
|
2020-08-09 15:36:47 +02:00
|
|
|
|
|
|
|
if (async) {
|
2020-09-19 16:45:17 +02:00
|
|
|
data.value.syncpointIdAsync = data.fence.id;
|
2020-08-09 15:36:47 +02:00
|
|
|
} else {
|
2020-09-19 16:45:17 +02:00
|
|
|
data.value.syncpointIdNonAsync = data.fence.id;
|
|
|
|
data.value.nonAsync = true;
|
2020-08-09 15:36:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-19 16:45:17 +02:00
|
|
|
data.value.val |= userEventId;
|
2020-08-09 15:36:47 +02:00
|
|
|
|
2020-09-19 16:45:17 +02:00
|
|
|
return NvStatus::Timeout;
|
2020-08-09 15:36:47 +02:00
|
|
|
} else {
|
2020-09-19 16:45:17 +02:00
|
|
|
return NvStatus::BadValue;
|
2020-08-09 15:36:47 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-12 21:02:24 +02:00
|
|
|
|
2020-09-25 02:05:12 +02:00
|
|
|
NvStatus NvHostCtrl::GetConfig(IoctlType type, span<u8> buffer, span<u8> inlineBuffer) {
|
2020-09-19 16:45:17 +02:00
|
|
|
return NvStatus::BadValue;
|
2020-08-12 21:02:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 02:05:12 +02:00
|
|
|
NvStatus NvHostCtrl::EventSignal(IoctlType type, span<u8> buffer, span<u8> inlineBuffer) {
|
2020-11-17 01:48:41 +01:00
|
|
|
auto userEventId{buffer.as<u16>()};
|
2020-08-12 21:02:24 +02:00
|
|
|
state.logger->Debug("Signalling nvhost event: {}", userEventId);
|
|
|
|
|
2020-09-19 16:45:17 +02:00
|
|
|
if (userEventId >= constant::NvHostEventCount || !events.at(userEventId))
|
|
|
|
return NvStatus::BadValue;
|
2020-08-12 21:02:24 +02:00
|
|
|
|
2020-09-26 07:17:57 +02:00
|
|
|
auto &event{*events.at(userEventId)};
|
2020-08-12 21:02:24 +02:00
|
|
|
|
|
|
|
if (event.state == NvHostEvent::State::Waiting) {
|
|
|
|
event.state = NvHostEvent::State::Cancelling;
|
|
|
|
state.logger->Debug("Cancelling waiting nvhost event: {}", userEventId);
|
|
|
|
event.Cancel(state.gpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
event.state = NvHostEvent::State::Cancelled;
|
|
|
|
|
2020-09-26 07:17:57 +02:00
|
|
|
auto driver{nvdrv::driver.lock()};
|
|
|
|
auto &hostSyncpoint{driver->hostSyncpoint};
|
2020-08-12 21:02:24 +02:00
|
|
|
hostSyncpoint.UpdateMin(event.fence.id);
|
2020-09-19 16:45:17 +02:00
|
|
|
|
|
|
|
return NvStatus::Success;
|
2020-08-12 21:02:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 02:05:12 +02:00
|
|
|
NvStatus NvHostCtrl::EventWait(IoctlType type, span<u8> buffer, span<u8> inlineBuffer) {
|
2020-09-19 16:45:17 +02:00
|
|
|
return EventWaitImpl(buffer, false);
|
2020-08-12 21:02:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 02:05:12 +02:00
|
|
|
NvStatus NvHostCtrl::EventWaitAsync(IoctlType type, span<u8> buffer, span<u8> inlineBuffer) {
|
2020-09-19 16:45:17 +02:00
|
|
|
return EventWaitImpl(buffer, true);
|
2020-08-12 21:02:24 +02:00
|
|
|
}
|
|
|
|
|
2020-09-25 02:05:12 +02:00
|
|
|
NvStatus NvHostCtrl::EventRegister(IoctlType type, span<u8> buffer, span<u8> inlineBuffer) {
|
|
|
|
auto userEventId{buffer.as<u32>()};
|
2020-08-12 21:02:24 +02:00
|
|
|
state.logger->Debug("Registering nvhost event: {}", userEventId);
|
|
|
|
|
2020-09-26 07:17:57 +02:00
|
|
|
auto &event{events.at(userEventId)};
|
2020-08-12 21:02:24 +02:00
|
|
|
if (event)
|
|
|
|
throw exception("Recreating events is unimplemented");
|
|
|
|
event = NvHostEvent(state);
|
2020-09-19 16:45:17 +02:00
|
|
|
|
|
|
|
return NvStatus::Success;
|
2020-08-12 21:02:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<type::KEvent> NvHostCtrl::QueryEvent(u32 eventId) {
|
2020-09-19 16:45:17 +02:00
|
|
|
EventValue eventValue{.val = eventId};
|
2020-09-26 07:17:57 +02:00
|
|
|
const auto &event{events.at(eventValue.nonAsync ? eventValue.eventSlotNonAsync : eventValue.eventSlotAsync)};
|
2020-08-12 21:02:24 +02:00
|
|
|
|
|
|
|
if (event && event->fence.id == (eventValue.nonAsync ? eventValue.syncpointIdNonAsync : eventValue.syncpointIdAsync))
|
|
|
|
return event->event;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
Framebuffer and NativeActivity
What was added:
* Framebuffer
* NativeActivity
* NV Services
* IOCTL Handler
* NV Devices:
* * /dev/nvmap - 0xC0080101, 0xC0080103, 0xC0200104, 0xC0180105, 0xC00C0109, 0xC008010E
* * /dev/nvhost-as-gpu
* * /dev/nvhost-channel - 0x40044801, 0xC0104809, 0xC010480B, 0xC018480C, 0x4004480D, 0xC020481A, 0x40084714
* * /dev/nvhost-ctrl
* * /dev/nvhost-ctrl-gpu - 0x80044701, 0x80284702, 0xC0184706, 0xC0B04705, 0x80084714
* SVCs:
* * SetMemoryAttribute
* * CreateTransferMemory
* * ResetSignal
* * GetSystemTick
* Addition of Compact Logger
What was fixed:
* SVCs:
* * SetHeapSize
* * SetMemoryAttribute
* * QueryMemory
* A release build would not set CMAKE_BUILD_TYPE to "RELEASE"
* The logger code was simplified
2019-11-13 21:09:31 +01:00
|
|
|
}
|