Commit 42fe2f91 authored by Honghui Zhang's avatar Honghui Zhang Committed by Lorenzo Pieralisi

PCI: mediatek: Implement chained IRQ handling setup

Implement irq_chip based solution for IRQs management in order to
comply with IRQ framework.
Signed-off-by: default avatarHonghui Zhang <honghui.zhang@mediatek.com>
[lorenzo.pieralisi@arm.com: updated commit log]
Signed-off-by: default avatarLorenzo Pieralisi <lorenzo.pieralisi@arm.com>
Acked-by: default avatarRyder Lee <ryder.lee@mediatek.com>
Reviewed-by: default avatarMarc Zyngier <marc.zyngier@arm.com>
parent 101c92dc
...@@ -194,6 +194,7 @@ config PCIE_MEDIATEK ...@@ -194,6 +194,7 @@ config PCIE_MEDIATEK
depends on (ARM || ARM64) && (ARCH_MEDIATEK || COMPILE_TEST) depends on (ARM || ARM64) && (ARCH_MEDIATEK || COMPILE_TEST)
depends on OF depends on OF
depends on PCI depends on PCI
depends on PCI_MSI_IRQ_DOMAIN
select PCIEPORTBUS select PCIEPORTBUS
help help
Say Y here if you want to enable PCIe controller support on Say Y here if you want to enable PCIe controller support on
......
...@@ -11,8 +11,10 @@ ...@@ -11,8 +11,10 @@
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/iopoll.h> #include <linux/iopoll.h>
#include <linux/irq.h> #include <linux/irq.h>
#include <linux/irqchip/chained_irq.h>
#include <linux/irqdomain.h> #include <linux/irqdomain.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/msi.h>
#include <linux/of_address.h> #include <linux/of_address.h>
#include <linux/of_pci.h> #include <linux/of_pci.h>
#include <linux/of_platform.h> #include <linux/of_platform.h>
...@@ -130,14 +132,12 @@ struct mtk_pcie_port; ...@@ -130,14 +132,12 @@ struct mtk_pcie_port;
/** /**
* struct mtk_pcie_soc - differentiate between host generations * struct mtk_pcie_soc - differentiate between host generations
* @need_fix_class_id: whether this host's class ID needed to be fixed or not * @need_fix_class_id: whether this host's class ID needed to be fixed or not
* @has_msi: whether this host supports MSI interrupts or not
* @ops: pointer to configuration access functions * @ops: pointer to configuration access functions
* @startup: pointer to controller setting functions * @startup: pointer to controller setting functions
* @setup_irq: pointer to initialize IRQ functions * @setup_irq: pointer to initialize IRQ functions
*/ */
struct mtk_pcie_soc { struct mtk_pcie_soc {
bool need_fix_class_id; bool need_fix_class_id;
bool has_msi;
struct pci_ops *ops; struct pci_ops *ops;
int (*startup)(struct mtk_pcie_port *port); int (*startup)(struct mtk_pcie_port *port);
int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node); int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node);
...@@ -161,7 +161,9 @@ struct mtk_pcie_soc { ...@@ -161,7 +161,9 @@ struct mtk_pcie_soc {
* @lane: lane count * @lane: lane count
* @slot: port slot * @slot: port slot
* @irq_domain: legacy INTx IRQ domain * @irq_domain: legacy INTx IRQ domain
* @inner_domain: inner IRQ domain
* @msi_domain: MSI IRQ domain * @msi_domain: MSI IRQ domain
* @lock: protect the msi_irq_in_use bitmap
* @msi_irq_in_use: bit map for assigned MSI IRQ * @msi_irq_in_use: bit map for assigned MSI IRQ
*/ */
struct mtk_pcie_port { struct mtk_pcie_port {
...@@ -179,7 +181,9 @@ struct mtk_pcie_port { ...@@ -179,7 +181,9 @@ struct mtk_pcie_port {
u32 lane; u32 lane;
u32 slot; u32 slot;
struct irq_domain *irq_domain; struct irq_domain *irq_domain;
struct irq_domain *inner_domain;
struct irq_domain *msi_domain; struct irq_domain *msi_domain;
struct mutex lock;
DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM); DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM);
}; };
...@@ -446,103 +450,130 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port) ...@@ -446,103 +450,130 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
return 0; return 0;
} }
static int mtk_pcie_msi_alloc(struct mtk_pcie_port *port) static void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
{ {
int msi; struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
phys_addr_t addr;
msi = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM); /* MT2712/MT7622 only support 32-bit MSI addresses */
if (msi < MTK_MSI_IRQS_NUM) addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
set_bit(msi, port->msi_irq_in_use); msg->address_hi = 0;
else msg->address_lo = lower_32_bits(addr);
return -ENOSPC;
msg->data = data->hwirq;
return msi; dev_dbg(port->pcie->dev, "msi#%d address_hi %#x address_lo %#x\n",
(int)data->hwirq, msg->address_hi, msg->address_lo);
} }
static void mtk_pcie_msi_free(struct mtk_pcie_port *port, unsigned long hwirq) static int mtk_msi_set_affinity(struct irq_data *irq_data,
const struct cpumask *mask, bool force)
{ {
clear_bit(hwirq, port->msi_irq_in_use); return -EINVAL;
} }
static int mtk_pcie_msi_setup_irq(struct msi_controller *chip, static void mtk_msi_ack_irq(struct irq_data *data)
struct pci_dev *pdev, struct msi_desc *desc)
{ {
struct mtk_pcie_port *port; struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
struct msi_msg msg; u32 hwirq = data->hwirq;
unsigned int irq;
int hwirq;
phys_addr_t msg_addr;
port = mtk_pcie_find_port(pdev->bus, pdev->devfn); writel(1 << hwirq, port->base + PCIE_IMSI_STATUS);
if (!port) }
return -EINVAL;
hwirq = mtk_pcie_msi_alloc(port); static struct irq_chip mtk_msi_bottom_irq_chip = {
if (hwirq < 0) .name = "MTK MSI",
return hwirq; .irq_compose_msi_msg = mtk_compose_msi_msg,
.irq_set_affinity = mtk_msi_set_affinity,
.irq_ack = mtk_msi_ack_irq,
};
irq = irq_create_mapping(port->msi_domain, hwirq); static int mtk_pcie_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
if (!irq) { unsigned int nr_irqs, void *args)
mtk_pcie_msi_free(port, hwirq); {
return -EINVAL; struct mtk_pcie_port *port = domain->host_data;
} unsigned long bit;
chip->dev = &pdev->dev; WARN_ON(nr_irqs != 1);
mutex_lock(&port->lock);
irq_set_msi_desc(irq, desc); bit = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM);
if (bit >= MTK_MSI_IRQS_NUM) {
mutex_unlock(&port->lock);
return -ENOSPC;
}
/* MT2712/MT7622 only support 32-bit MSI addresses */ __set_bit(bit, port->msi_irq_in_use);
msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
msg.address_hi = 0; mutex_unlock(&port->lock);
msg.address_lo = lower_32_bits(msg_addr);
msg.data = hwirq;
pci_write_msi_msg(irq, &msg); irq_domain_set_info(domain, virq, bit, &mtk_msi_bottom_irq_chip,
domain->host_data, handle_edge_irq,
NULL, NULL);
return 0; return 0;
} }
static void mtk_msi_teardown_irq(struct msi_controller *chip, unsigned int irq) static void mtk_pcie_irq_domain_free(struct irq_domain *domain,
unsigned int virq, unsigned int nr_irqs)
{ {
struct pci_dev *pdev = to_pci_dev(chip->dev); struct irq_data *d = irq_domain_get_irq_data(domain, virq);
struct irq_data *d = irq_get_irq_data(irq); struct mtk_pcie_port *port = irq_data_get_irq_chip_data(d);
irq_hw_number_t hwirq = irqd_to_hwirq(d);
struct mtk_pcie_port *port;
port = mtk_pcie_find_port(pdev->bus, pdev->devfn); mutex_lock(&port->lock);
if (!port)
return; if (!test_bit(d->hwirq, port->msi_irq_in_use))
dev_err(port->pcie->dev, "trying to free unused MSI#%lu\n",
d->hwirq);
else
__clear_bit(d->hwirq, port->msi_irq_in_use);
mutex_unlock(&port->lock);
irq_dispose_mapping(irq); irq_domain_free_irqs_parent(domain, virq, nr_irqs);
mtk_pcie_msi_free(port, hwirq);
} }
static struct msi_controller mtk_pcie_msi_chip = { static const struct irq_domain_ops msi_domain_ops = {
.setup_irq = mtk_pcie_msi_setup_irq, .alloc = mtk_pcie_irq_domain_alloc,
.teardown_irq = mtk_msi_teardown_irq, .free = mtk_pcie_irq_domain_free,
}; };
static struct irq_chip mtk_msi_irq_chip = { static struct irq_chip mtk_msi_irq_chip = {
.name = "MTK PCIe MSI", .name = "MTK PCIe MSI",
.irq_enable = pci_msi_unmask_irq, .irq_ack = irq_chip_ack_parent,
.irq_disable = pci_msi_mask_irq, .irq_mask = pci_msi_mask_irq,
.irq_mask = pci_msi_mask_irq, .irq_unmask = pci_msi_unmask_irq,
.irq_unmask = pci_msi_unmask_irq,
}; };
static int mtk_pcie_msi_map(struct irq_domain *domain, unsigned int irq, static struct msi_domain_info mtk_msi_domain_info = {
irq_hw_number_t hwirq) .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
MSI_FLAG_PCI_MSIX),
.chip = &mtk_msi_irq_chip,
};
static int mtk_pcie_allocate_msi_domains(struct mtk_pcie_port *port)
{ {
irq_set_chip_and_handler(irq, &mtk_msi_irq_chip, handle_simple_irq); struct fwnode_handle *fwnode = of_node_to_fwnode(port->pcie->dev->of_node);
irq_set_chip_data(irq, domain->host_data);
mutex_init(&port->lock);
port->inner_domain = irq_domain_create_linear(fwnode, MTK_MSI_IRQS_NUM,
&msi_domain_ops, port);
if (!port->inner_domain) {
dev_err(port->pcie->dev, "failed to create IRQ domain\n");
return -ENOMEM;
}
port->msi_domain = pci_msi_create_irq_domain(fwnode, &mtk_msi_domain_info,
port->inner_domain);
if (!port->msi_domain) {
dev_err(port->pcie->dev, "failed to create MSI domain\n");
irq_domain_remove(port->inner_domain);
return -ENOMEM;
}
return 0; return 0;
} }
static const struct irq_domain_ops msi_domain_ops = {
.map = mtk_pcie_msi_map,
};
static void mtk_pcie_enable_msi(struct mtk_pcie_port *port) static void mtk_pcie_enable_msi(struct mtk_pcie_port *port)
{ {
u32 val; u32 val;
...@@ -575,6 +606,7 @@ static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port, ...@@ -575,6 +606,7 @@ static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port,
{ {
struct device *dev = port->pcie->dev; struct device *dev = port->pcie->dev;
struct device_node *pcie_intc_node; struct device_node *pcie_intc_node;
int ret;
/* Setup INTx */ /* Setup INTx */
pcie_intc_node = of_get_next_child(node, NULL); pcie_intc_node = of_get_next_child(node, NULL);
...@@ -591,27 +623,28 @@ static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port, ...@@ -591,27 +623,28 @@ static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port,
} }
if (IS_ENABLED(CONFIG_PCI_MSI)) { if (IS_ENABLED(CONFIG_PCI_MSI)) {
port->msi_domain = irq_domain_add_linear(node, MTK_MSI_IRQS_NUM, ret = mtk_pcie_allocate_msi_domains(port);
&msi_domain_ops, if (ret)
&mtk_pcie_msi_chip); return ret;
if (!port->msi_domain) {
dev_err(dev, "failed to create MSI IRQ domain\n");
return -ENODEV;
}
mtk_pcie_enable_msi(port); mtk_pcie_enable_msi(port);
} }
return 0; return 0;
} }
static irqreturn_t mtk_pcie_intr_handler(int irq, void *data) static void mtk_pcie_intr_handler(struct irq_desc *desc)
{ {
struct mtk_pcie_port *port = (struct mtk_pcie_port *)data; struct mtk_pcie_port *port = irq_desc_get_handler_data(desc);
struct irq_chip *irqchip = irq_desc_get_chip(desc);
unsigned long status; unsigned long status;
u32 virq; u32 virq;
u32 bit = INTX_SHIFT; u32 bit = INTX_SHIFT;
while ((status = readl(port->base + PCIE_INT_STATUS)) & INTX_MASK) { chained_irq_enter(irqchip, desc);
status = readl(port->base + PCIE_INT_STATUS);
if (status & INTX_MASK) {
for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) { for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) {
/* Clear the INTx */ /* Clear the INTx */
writel(1 << bit, port->base + PCIE_INT_STATUS); writel(1 << bit, port->base + PCIE_INT_STATUS);
...@@ -622,14 +655,12 @@ static irqreturn_t mtk_pcie_intr_handler(int irq, void *data) ...@@ -622,14 +655,12 @@ static irqreturn_t mtk_pcie_intr_handler(int irq, void *data)
} }
if (IS_ENABLED(CONFIG_PCI_MSI)) { if (IS_ENABLED(CONFIG_PCI_MSI)) {
while ((status = readl(port->base + PCIE_INT_STATUS)) & MSI_STATUS) { if (status & MSI_STATUS){
unsigned long imsi_status; unsigned long imsi_status;
while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) { while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) {
for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM) { for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM) {
/* Clear the MSI */ virq = irq_find_mapping(port->inner_domain, bit);
writel(1 << bit, port->base + PCIE_IMSI_STATUS);
virq = irq_find_mapping(port->msi_domain, bit);
generic_handle_irq(virq); generic_handle_irq(virq);
} }
} }
...@@ -638,7 +669,9 @@ static irqreturn_t mtk_pcie_intr_handler(int irq, void *data) ...@@ -638,7 +669,9 @@ static irqreturn_t mtk_pcie_intr_handler(int irq, void *data)
} }
} }
return IRQ_HANDLED; chained_irq_exit(irqchip, desc);
return;
} }
static int mtk_pcie_setup_irq(struct mtk_pcie_port *port, static int mtk_pcie_setup_irq(struct mtk_pcie_port *port,
...@@ -649,20 +682,15 @@ static int mtk_pcie_setup_irq(struct mtk_pcie_port *port, ...@@ -649,20 +682,15 @@ static int mtk_pcie_setup_irq(struct mtk_pcie_port *port,
struct platform_device *pdev = to_platform_device(dev); struct platform_device *pdev = to_platform_device(dev);
int err, irq; int err, irq;
irq = platform_get_irq(pdev, port->slot);
err = devm_request_irq(dev, irq, mtk_pcie_intr_handler,
IRQF_SHARED, "mtk-pcie", port);
if (err) {
dev_err(dev, "unable to request IRQ %d\n", irq);
return err;
}
err = mtk_pcie_init_irq_domain(port, node); err = mtk_pcie_init_irq_domain(port, node);
if (err) { if (err) {
dev_err(dev, "failed to init PCIe IRQ domain\n"); dev_err(dev, "failed to init PCIe IRQ domain\n");
return err; return err;
} }
irq = platform_get_irq(pdev, port->slot);
irq_set_chained_handler_and_data(irq, mtk_pcie_intr_handler, port);
return 0; return 0;
} }
...@@ -1096,8 +1124,6 @@ static int mtk_pcie_register_host(struct pci_host_bridge *host) ...@@ -1096,8 +1124,6 @@ static int mtk_pcie_register_host(struct pci_host_bridge *host)
host->map_irq = of_irq_parse_and_map_pci; host->map_irq = of_irq_parse_and_map_pci;
host->swizzle_irq = pci_common_swizzle; host->swizzle_irq = pci_common_swizzle;
host->sysdata = pcie; host->sysdata = pcie;
if (IS_ENABLED(CONFIG_PCI_MSI) && pcie->soc->has_msi)
host->msi = &mtk_pcie_msi_chip;
err = pci_scan_root_bus_bridge(host); err = pci_scan_root_bus_bridge(host);
if (err < 0) if (err < 0)
...@@ -1159,7 +1185,6 @@ static const struct mtk_pcie_soc mtk_pcie_soc_v1 = { ...@@ -1159,7 +1185,6 @@ static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
}; };
static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = { static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = {
.has_msi = true,
.ops = &mtk_pcie_ops_v2, .ops = &mtk_pcie_ops_v2,
.startup = mtk_pcie_startup_port_v2, .startup = mtk_pcie_startup_port_v2,
.setup_irq = mtk_pcie_setup_irq, .setup_irq = mtk_pcie_setup_irq,
...@@ -1167,7 +1192,6 @@ static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = { ...@@ -1167,7 +1192,6 @@ static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = {
static const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = { static const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = {
.need_fix_class_id = true, .need_fix_class_id = true,
.has_msi = true,
.ops = &mtk_pcie_ops_v2, .ops = &mtk_pcie_ops_v2,
.startup = mtk_pcie_startup_port_v2, .startup = mtk_pcie_startup_port_v2,
.setup_irq = mtk_pcie_setup_irq, .setup_irq = mtk_pcie_setup_irq,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment