// SPDX-License-Identifier: GPL-2.0+
/*
* PCI Hotplug Driver for PowerPC PowerNV platform.
*
* Copyright Gavin Shan, IBM Corporation 2016.
* Copyright (C) 2025 Raptor Engineering, LLC
* Copyright (C) 2025 Raptor Computing Systems, LLC
*/
#include <linux/bitfield.h>
#include <linux/libfdt.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/pci_hotplug.h>
#include <linux/of_fdt.h>
#include <asm/opal.h>
#include <asm/pnv-pci.h>
#include <asm/ppc-pci.h>
#define DRIVER_VERSION "0.1"
#define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
#define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
#define SLOT_WARN(sl, x...) \
((sl)->pdev ? pci_warn((sl)->pdev, x) : dev_warn(&(sl)->bus->dev, x))
struct pnv_php_event {
bool added;
struct pnv_php_slot *php_slot;
struct work_struct work;
};
static LIST_HEAD(pnv_php_slot_list);
static DEFINE_SPINLOCK(pnv_php_lock);
static void pnv_php_register(struct device_node *dn);
static void pnv_php_unregister_one(struct device_node *dn);
static void pnv_php_unregister(struct device_node *dn);
static void pnv_php_enable_irq(struct pnv_php_slot *php_slot);
static void pnv_php_disable_irq(struct pnv_php_slot *php_slot,
bool disable_device, bool disable_msi)
{
struct pci_dev *pdev = php_slot->pdev;
u16 ctrl;
if (php_slot->irq > 0) {
pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
ctrl &= ~(PCI_EXP_SLTCTL_HPIE |
PCI_EXP_SLTCTL_PDCE |
PCI_EXP_SLTCTL_DLLSCE);
pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
free_irq(php_slot->irq, php_slot);
php_slot->irq = 0;
}
if (disable_device || disable_msi) {
if (pdev->msix_enabled)
pci_disable_msix(pdev);
else if (pdev->msi_enabled)
pci_disable_msi(pdev);
}
if (disable_device)
pci_disable_device(pdev);
}
static void pnv_php_free_slot(struct kref *kref)
{
struct pnv_php_slot *php_slot = container_of(kref,
struct pnv_php_slot, kref);
WARN_ON(!list_empty(&php_slot->children));
pnv_php_disable_irq(php_slot, false, false);
destroy_workqueue(php_slot->wq);
kfree(php_slot->name);
kfree(php_slot);
}
static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
{
if (!php_slot)
return;
kref_put(&php_slot->kref, pnv_php_free_slot);
}
static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
struct pnv_php_slot *php_slot)
{
struct pnv_php_slot *target