blob: 43783722a1ae7d4101a03a364f6730450af75d95 [file]
// Copyright 2016 The Fuchsia Authors
// Copyright (c) 2012-2015 Travis Geiselbrecht
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT
#include "arm_gicv2m_pcie.h"
#include <inttypes.h>
#include <lib/lazy_init/lazy_init.h>
#include <lib/pci/kpci.h>
#include <lib/zbi-format/driver-config.h>
#include <trace.h>
#include <zircon/types.h>
#include <dev/interrupt/arm_gicv2m_msi.h>
#include <dev/pcie_bus_driver.h>
#include <dev/pcie_platform.h>
#include <dev/pcie_root.h>
#include <fbl/alloc_checker.h>
#include <fbl/ref_ptr.h>
#include <pdev/interrupt.h>
#include <ktl/enforce.h>
namespace {
class ArmGicV2PciePlatformSupport : public PciePlatformInterface {
public:
explicit ArmGicV2PciePlatformSupport(bool has_msi_gic)
: PciePlatformInterface(has_msi_gic ? MsiSupportLevel::MSI_WITH_MASKING
: MsiSupportLevel::NONE) {}
zx_status_t AllocMsiBlock(uint requested_irqs, bool can_target_64bit, bool is_msix,
msi_block_t* out_block) override {
return arm_gicv2m_msi_alloc_block(requested_irqs, can_target_64bit, is_msix, out_block);
}
void FreeMsiBlock(msi_block_t* block) override { arm_gicv2m_msi_free_block(block); }
void RegisterMsiHandler(const msi_block_t* block, uint msi_id,
interrupt_handler_t handler) override {
arm_gicv2m_msi_register_handler(block, msi_id, ktl::move(handler));
}
void MaskUnmaskMsi(const msi_block_t* block, uint msi_id, bool mask) override {
arm_gicv2m_msi_mask_unmask(block, msi_id, mask);
}
};
lazy_init::LazyInit<ArmGicV2PciePlatformSupport, lazy_init::CheckType::None,
lazy_init::Destructor::Disabled>
g_platform_pcie_support;
} // anonymous namespace
void arm_gicv2_pcie_init(bool use_msi) {
if (!Pci::KernelPciEnabled()) {
return;
}
// based on whether or not ZBI says we support MSI, initialize the v2m allocator
if (use_msi) {
dprintf(SPEW, "GICv2 MSI init\n");
// Initialize the MSI allocator
zx_status_t res = arm_gicv2m_msi_init();
if (res != ZX_OK) {
TRACEF(
"Failed to initialize MSI allocator (res = %d). PCI will be "
"restricted to legacy IRQ mode.\n",
res);
}
use_msi = (res == ZX_OK);
} else {
use_msi = false;
}
// Initialize the PCI platform supported based on whether or not we support MSI
g_platform_pcie_support.Initialize(use_msi);
zx_status_t res = PcieBusDriver::InitializeDriver(g_platform_pcie_support.Get());
if (res != ZX_OK) {
TRACEF(
"Failed to initialize PCI bus driver (res %d). "
"PCI will be non-functional.\n",
res);
}
}