blob: e47c0c1976fc72bc431e6609fc3d5f4217dde33d [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "msd_img_device.h"
#include "fuchsia_sys_info.h"
extern "C"
{
#include "process_stats.h"
#include "pvrsrv.h"
#include "pvrsrv_error.h"
#include "rgxdevice.h"
#include "srvcore.h"
}
namespace
{
static uint64_t registers_physical_address_s;
static void* registers_cpu_address_s;
} // namespace
MsdImgDevice::MsdImgDevice(std::unique_ptr<magma::PlatformDevice> platform_device)
: platform_device_(std::move(platform_device))
{
magic_ = kMagic;
sys_info_ = std::make_unique<FuchsiaSysInfo>();
}
MsdImgDevice::~MsdImgDevice()
{
registers_physical_address_s = 0u;
registers_cpu_address_s = nullptr;
if (device_node_)
{
PVRSRVDeviceDestroy(device_node_);
}
}
bool
MsdImgDevice::Init()
{
bti_ = platform_device_->GetBusTransactionInitiator();
if (!bti_)
{
return DRETF(false, "Failed to create bus transaction initiator");
}
mmio_ = platform_device_->CpuMapMmio(0u, magma::PlatformMmio::CACHE_POLICY_UNCACHED_DEVICE);
if (!mmio_)
{
return DRETF(false, "Failed to get mmio");
}
registers_physical_address_s = mmio_->physical_address();
registers_cpu_address_s = mmio_->addr();
PVRSRV_ERROR srv_err;
srv_err = PVRSRVDeviceCreate(this, 1u, &device_node_);
if (srv_err != PVRSRV_OK)
{
return DRETF(false, "Failed to create device: %d", srv_err);
}
srv_err = PVRSRVDeviceInitialise(device_node_);
if (srv_err != PVRSRV_OK)
{
return DRETF(false, "Failed to initialize device: %d", srv_err);
}
return true;
}
// static
std::unique_ptr<MsdImgDevice>
MsdImgDevice::Create(msd_driver_t* drv, void* device)
{
auto platform_device = magma::PlatformDevice::Create(device);
if (!platform_device)
{
return DRETP(nullptr, "Failed to create platform device");
}
auto msd_device = std::unique_ptr<MsdImgDevice>(new MsdImgDevice(std::move(platform_device)));
if (!msd_device->Init())
{
return DRETP(nullptr, "Failed to create msd device");
}
return msd_device;
}
// static
void
MsdImgDevice::Destroy(MsdImgDevice* device)
{
delete device;
}
// static
uint64_t
MsdImgDevice::GetRegistersPhysicalAddress()
{
DASSERT(registers_physical_address_s);
return registers_physical_address_s;
}
// static
void*
MsdImgDevice::GetRegistersCpuAddress()
{
DASSERT(registers_cpu_address_s);
return registers_cpu_address_s;
}