| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2024-2025 ARM Limited, All Rights Reserved. |
| 4 |
*/ |
| 5 |
#define pr_fmt(fmt) "GICv5 IWB: " fmt |
| 6 |
|
| 7 |
#include <linux/acpi.h> |
| 8 |
#include <linux/init.h> |
| 9 |
#include <linux/kernel.h> |
| 10 |
#include <linux/msi.h> |
| 11 |
#include <linux/of.h> |
| 12 |
#include <linux/of_address.h> |
| 13 |
#include <linux/of_platform.h> |
| 14 |
|
| 15 |
#include <linux/irqchip.h> |
| 16 |
#include <linux/irqchip/arm-gic-v5.h> |
| 17 |
|
| 18 |
struct gicv5_iwb_chip_data { |
| 19 |
void __iomem *iwb_base; |
| 20 |
u16 nr_regs; |
| 21 |
}; |
| 22 |
|
| 23 |
static u32 iwb_readl_relaxed(struct gicv5_iwb_chip_data *iwb_node, const u32 reg_offset) |
| 24 |
{ |
| 25 |
return readl_relaxed(iwb_node->iwb_base + reg_offset); |
| 26 |
} |
| 27 |
|
| 28 |
static void iwb_writel_relaxed(struct gicv5_iwb_chip_data *iwb_node, const u32 val, |
| 29 |
const u32 reg_offset) |
| 30 |
{ |
| 31 |
writel_relaxed(val, iwb_node->iwb_base + reg_offset); |
| 32 |
} |
| 33 |
|
| 34 |
static int gicv5_iwb_wait_for_wenabler(struct gicv5_iwb_chip_data *iwb_node) |
| 35 |
{ |
| 36 |
return gicv5_wait_for_op_atomic(iwb_node->iwb_base, GICV5_IWB_WENABLE_STATUSR, |
| 37 |
GICV5_IWB_WENABLE_STATUSR_IDLE, NULL); |
| 38 |
} |
| 39 |
|
| 40 |
static int __gicv5_iwb_set_wire_enable(struct gicv5_iwb_chip_data *iwb_node, |
| 41 |
u32 iwb_wire, bool enable) |
| 42 |
{ |
| 43 |
u32 n = iwb_wire / 32; |
| 44 |
u8 i = iwb_wire % 32; |
| 45 |
u32 val; |
| 46 |
|
| 47 |
if (n >= iwb_node->nr_regs) { |
| 48 |
pr_err("IWB_WENABLER<n> is invalid for n=%u\n", n); |
| 49 |
return -EINVAL; |
| 50 |
} |
| 51 |
|
| 52 |
/* |
| 53 |
* Enable IWB wire/pin at this point |
| 54 |
* Note: This is not the same as enabling the interrupt |
| 55 |
*/ |
| 56 |
val = iwb_readl_relaxed(iwb_node, GICV5_IWB_WENABLER + (4 * n)); |
| 57 |
if (enable) |
| 58 |
val |= BIT(i); |
| 59 |
else |
| 60 |
val &= ~BIT(i); |
| 61 |
iwb_writel_relaxed(iwb_node, val, GICV5_IWB_WENABLER + (4 * n)); |
| 62 |
|
| 63 |
return gicv5_iwb_wait_for_wenabler(iwb_node); |
| 64 |
} |
| 65 |
|
| 66 |
static int gicv5_iwb_enable_wire(struct gicv5_iwb_chip_data *iwb_node, |
| 67 |
u32 iwb_wire) |
| 68 |
{ |
| 69 |
return __gicv5_iwb_set_wire_enable(iwb_node, iwb_wire, true); |
| 70 |
} |
| 71 |
|
| 72 |
static int gicv5_iwb_disable_wire(struct gicv5_iwb_chip_data *iwb_node, |
| 73 |
u32 iwb_wire) |
| 74 |
{ |
| 75 |
return __gicv5_iwb_set_wire_enable(iwb_node, iwb_wire, false); |
| 76 |
} |
| 77 |
|
| 78 |
static void gicv5_iwb_irq_disable(struct irq_data *d) |
| 79 |
{ |
| 80 |
struct gicv5_iwb_chip_data *iwb_node = irq_data_get_irq_chip_data(d); |
| 81 |
|
| 82 |
gicv5_iwb_disable_wire(iwb_node, d->hwirq); |
| 83 |
irq_chip_disable_parent(d); |
| 84 |
} |
| 85 |
|
| 86 |
static void gicv5_iwb_irq_enable(struct irq_data *d) |
| 87 |
{ |
| 88 |
struct gicv5_iwb_chip_data *iwb_node = irq_data_get_irq_chip_data(d); |
| 89 |
|
| 90 |
gicv5_iwb_enable_wire(iwb_node, d->hwirq); |
| 91 |
irq_chip_enable_parent(d); |
| 92 |
} |
| 93 |
|
| 94 |
static int gicv5_iwb_set_type(struct irq_data *d, unsigned int type) |
| 95 |
{ |
| 96 |
struct gicv5_iwb_chip_data *iwb_node = irq_data_get_irq_chip_data(d); |
| 97 |
u32 iwb_wire, n, wtmr; |
| 98 |
u8 i; |
| 99 |
|
| 100 |
iwb_wire = d->hwirq; |
| 101 |
i = iwb_wire % 32; |
| 102 |
n = iwb_wire / 32; |
| 103 |
|
| 104 |
if (n >= iwb_node->nr_regs) { |
| 105 |
pr_err_once("reg %u out of range\n", n); |
| 106 |
return -EINVAL; |
| 107 |
} |
| 108 |
|
| 109 |
wtmr = iwb_readl_relaxed(iwb_node, GICV5_IWB_WTMR + (4 * n)); |
| 110 |
|
| 111 |
switch (type) { |
| 112 |
case IRQ_TYPE_LEVEL_HIGH: |
| 113 |
case IRQ_TYPE_LEVEL_LOW: |
| 114 |
wtmr |= BIT(i); |
| 115 |
break; |
| 116 |
case IRQ_TYPE_EDGE_RISING: |
| 117 |
case IRQ_TYPE_EDGE_FALLING: |
| 118 |
wtmr &= ~BIT(i); |
| 119 |
break; |
| 120 |
default: |
| 121 |
pr_debug("unexpected wire trigger mode"); |
| 122 |
return -EINVAL; |
| 123 |
} |
| 124 |
|
| 125 |
iwb_writel_relaxed(iwb_node, wtmr, GICV5_IWB_WTMR + (4 * n)); |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static void gicv5_iwb_domain_set_desc(msi_alloc_info_t *alloc_info, struct msi_desc *desc) |
| 131 |
{ |
| 132 |
alloc_info->desc = desc; |
| 133 |
alloc_info->hwirq = (u32)desc->data.icookie.value; |
| 134 |
} |
| 135 |
|
| 136 |
static int gicv5_iwb_irq_domain_translate(struct irq_domain *d, struct irq_fwspec *fwspec, |
| 137 |
irq_hw_number_t *hwirq, |
| 138 |
unsigned int *type) |
| 139 |
{ |
| 140 |
if (is_of_node(fwspec->fwnode)) { |
| 141 |
|
| 142 |
if (fwspec->param_count < 2) |
| 143 |
return -EINVAL; |
| 144 |
|
| 145 |
/* |
| 146 |
* param[0] is be the wire |
| 147 |
* param[1] is the interrupt type |
| 148 |
*/ |
| 149 |
*hwirq = fwspec->param[0]; |
| 150 |
*type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK; |
| 151 |
} |
| 152 |
|
| 153 |
if (is_acpi_device_node(fwspec->fwnode)) { |
| 154 |
|
| 155 |
if (fwspec->param_count < 2) |
| 156 |
return -EINVAL; |
| 157 |
|
| 158 |
/* |
| 159 |
* Extract the wire from param[0] |
| 160 |
* param[1] is the interrupt type |
| 161 |
*/ |
| 162 |
*hwirq = FIELD_GET(GICV5_GSI_IWB_WIRE, fwspec->param[0]); |
| 163 |
*type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK; |
| 164 |
} |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static void gicv5_iwb_write_msi_msg(struct irq_data *d, struct msi_msg *msg) {} |
| 170 |
|
| 171 |
static const struct msi_domain_template iwb_msi_template = { |
| 172 |
.chip = { |
| 173 |
.name = "GICv5-IWB", |
| 174 |
.irq_mask = irq_chip_mask_parent, |
| 175 |
.irq_unmask = irq_chip_unmask_parent, |
| 176 |
.irq_enable = gicv5_iwb_irq_enable, |
| 177 |
.irq_disable = gicv5_iwb_irq_disable, |
| 178 |
.irq_eoi = irq_chip_eoi_parent, |
| 179 |
.irq_set_type = gicv5_iwb_set_type, |
| 180 |
.irq_write_msi_msg = gicv5_iwb_write_msi_msg, |
| 181 |
.irq_set_affinity = irq_chip_set_affinity_parent, |
| 182 |
.irq_get_irqchip_state = irq_chip_get_parent_state, |
| 183 |
.irq_set_irqchip_state = irq_chip_set_parent_state, |
| 184 |
.flags = IRQCHIP_SET_TYPE_MASKED | |
| 185 |
IRQCHIP_SKIP_SET_WAKE | |
| 186 |
IRQCHIP_MASK_ON_SUSPEND, |
| 187 |
}, |
| 188 |
|
| 189 |
.ops = { |
| 190 |
.set_desc = gicv5_iwb_domain_set_desc, |
| 191 |
.msi_translate = gicv5_iwb_irq_domain_translate, |
| 192 |
}, |
| 193 |
|
| 194 |
.info = { |
| 195 |
.bus_token = DOMAIN_BUS_WIRED_TO_MSI, |
| 196 |
.flags = MSI_FLAG_USE_DEV_FWNODE, |
| 197 |
}, |
| 198 |
|
| 199 |
.alloc_info = { |
| 200 |
.flags = MSI_ALLOC_FLAGS_FIXED_MSG_DATA, |
| 201 |
}, |
| 202 |
}; |
| 203 |
|
| 204 |
static bool gicv5_iwb_create_device_domain(struct device *dev, unsigned int size, |
| 205 |
struct gicv5_iwb_chip_data *iwb_node) |
| 206 |
{ |
| 207 |
if (WARN_ON_ONCE(!dev->msi.domain)) |
| 208 |
return false; |
| 209 |
|
| 210 |
return msi_create_device_irq_domain(dev, MSI_DEFAULT_DOMAIN, |
| 211 |
&iwb_msi_template, size, |
| 212 |
NULL, iwb_node); |
| 213 |
} |
| 214 |
|
| 215 |
static struct gicv5_iwb_chip_data * |
| 216 |
gicv5_iwb_init_bases(void __iomem *iwb_base, struct platform_device *pdev) |
| 217 |
{ |
| 218 |
u32 nr_wires, idr0, cr0; |
| 219 |
unsigned int n; |
| 220 |
int ret; |
| 221 |
|
| 222 |
struct gicv5_iwb_chip_data *iwb_node __free(kfree) = kzalloc_obj(*iwb_node); |
| 223 |
if (!iwb_node) |
| 224 |
return ERR_PTR(-ENOMEM); |
| 225 |
|
| 226 |
iwb_node->iwb_base = iwb_base; |
| 227 |
|
| 228 |
idr0 = iwb_readl_relaxed(iwb_node, GICV5_IWB_IDR0); |
| 229 |
nr_wires = (FIELD_GET(GICV5_IWB_IDR0_IW_RANGE, idr0) + 1) * 32; |
| 230 |
|
| 231 |
cr0 = iwb_readl_relaxed(iwb_node, GICV5_IWB_CR0); |
| 232 |
if (!FIELD_GET(GICV5_IWB_CR0_IWBEN, cr0)) { |
| 233 |
dev_err(&pdev->dev, "IWB must be enabled in firmware\n"); |
| 234 |
return ERR_PTR(-EINVAL); |
| 235 |
} |
| 236 |
|
| 237 |
iwb_node->nr_regs = FIELD_GET(GICV5_IWB_IDR0_IW_RANGE, idr0) + 1; |
| 238 |
|
| 239 |
for (n = 0; n < iwb_node->nr_regs; n++) |
| 240 |
iwb_writel_relaxed(iwb_node, 0, GICV5_IWB_WENABLER + (sizeof(u32) * n)); |
| 241 |
|
| 242 |
ret = gicv5_iwb_wait_for_wenabler(iwb_node); |
| 243 |
if (ret) |
| 244 |
return ERR_PTR(ret); |
| 245 |
|
| 246 |
if (!gicv5_iwb_create_device_domain(&pdev->dev, nr_wires, iwb_node)) |
| 247 |
return ERR_PTR(-ENOMEM); |
| 248 |
|
| 249 |
return_ptr(iwb_node); |
| 250 |
} |
| 251 |
|
| 252 |
static int gicv5_iwb_device_probe(struct platform_device *pdev) |
| 253 |
{ |
| 254 |
struct gicv5_iwb_chip_data *iwb_node; |
| 255 |
void __iomem *iwb_base; |
| 256 |
struct resource *res; |
| 257 |
|
| 258 |
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 259 |
if (!res) |
| 260 |
return -EINVAL; |
| 261 |
|
| 262 |
iwb_base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 263 |
if (!iwb_base) { |
| 264 |
dev_err(&pdev->dev, "failed to ioremap %pR\n", res); |
| 265 |
return -ENOMEM; |
| 266 |
} |
| 267 |
|
| 268 |
iwb_node = gicv5_iwb_init_bases(iwb_base, pdev); |
| 269 |
if (IS_ERR(iwb_node)) |
| 270 |
return PTR_ERR(iwb_node); |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
static const struct of_device_id gicv5_iwb_of_match[] = { |
| 276 |
{ .compatible = "arm,gic-v5-iwb" }, |
| 277 |
{ /* END */ } |
| 278 |
}; |
| 279 |
MODULE_DEVICE_TABLE(of, gicv5_iwb_of_match); |
| 280 |
|
| 281 |
#ifdef CONFIG_ACPI |
| 282 |
static const struct acpi_device_id iwb_acpi_match[] = { |
| 283 |
{ "ARMH0003", 0 }, |
| 284 |
{} |
| 285 |
}; |
| 286 |
#endif |
| 287 |
|
| 288 |
static struct platform_driver gicv5_iwb_platform_driver = { |
| 289 |
.driver = { |
| 290 |
.name = "GICv5 IWB", |
| 291 |
.of_match_table = gicv5_iwb_of_match, |
| 292 |
.acpi_match_table = ACPI_PTR(iwb_acpi_match), |
| 293 |
.suppress_bind_attrs = true, |
| 294 |
}, |
| 295 |
.probe = gicv5_iwb_device_probe, |
| 296 |
}; |
| 297 |
|
| 298 |
module_platform_driver(gicv5_iwb_platform_driver); |