Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

irqchip/riscv-imsic: Add device MSI domain support for platform devices

The Linux platform MSI support allows per-device MSI domains so add
a platform irqchip driver for RISC-V IMSIC which provides a base IRQ
domain with MSI parent support for platform device domains.

The IMSIC platform driver assumes that the IMSIC state is already
initialized by the IMSIC early driver.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Björn Töpel <bjorn@rivosinc.com>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
Link: https://lore.kernel.org/r/20240307140307.646078-4-apatel@ventanamicro.com

authored by

Anup Patel and committed by
Thomas Gleixner
027e125a 21a8f8a0

+345 -1
+1 -1
drivers/irqchip/Makefile
··· 95 95 obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o 96 96 obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o 97 97 obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o 98 - obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic-state.o irq-riscv-imsic-early.o 98 + obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic-state.o irq-riscv-imsic-early.o irq-riscv-imsic-platform.o 99 99 obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o 100 100 obj-$(CONFIG_STARFIVE_JH8100_INTC) += irq-starfive-jh8100-intc.o 101 101 obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.o
+343
drivers/irqchip/irq-riscv-imsic-platform.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2021 Western Digital Corporation or its affiliates. 4 + * Copyright (C) 2022 Ventana Micro Systems Inc. 5 + */ 6 + 7 + #define pr_fmt(fmt) "riscv-imsic: " fmt 8 + #include <linux/bitmap.h> 9 + #include <linux/cpu.h> 10 + #include <linux/interrupt.h> 11 + #include <linux/io.h> 12 + #include <linux/irq.h> 13 + #include <linux/irqchip.h> 14 + #include <linux/irqdomain.h> 15 + #include <linux/module.h> 16 + #include <linux/msi.h> 17 + #include <linux/platform_device.h> 18 + #include <linux/spinlock.h> 19 + #include <linux/smp.h> 20 + 21 + #include "irq-riscv-imsic-state.h" 22 + 23 + static bool imsic_cpu_page_phys(unsigned int cpu, unsigned int guest_index, 24 + phys_addr_t *out_msi_pa) 25 + { 26 + struct imsic_global_config *global; 27 + struct imsic_local_config *local; 28 + 29 + global = &imsic->global; 30 + local = per_cpu_ptr(global->local, cpu); 31 + 32 + if (BIT(global->guest_index_bits) <= guest_index) 33 + return false; 34 + 35 + if (out_msi_pa) 36 + *out_msi_pa = local->msi_pa + (guest_index * IMSIC_MMIO_PAGE_SZ); 37 + 38 + return true; 39 + } 40 + 41 + static void imsic_irq_mask(struct irq_data *d) 42 + { 43 + imsic_vector_mask(irq_data_get_irq_chip_data(d)); 44 + } 45 + 46 + static void imsic_irq_unmask(struct irq_data *d) 47 + { 48 + imsic_vector_unmask(irq_data_get_irq_chip_data(d)); 49 + } 50 + 51 + static int imsic_irq_retrigger(struct irq_data *d) 52 + { 53 + struct imsic_vector *vec = irq_data_get_irq_chip_data(d); 54 + struct imsic_local_config *local; 55 + 56 + if (WARN_ON(!vec)) 57 + return -ENOENT; 58 + 59 + local = per_cpu_ptr(imsic->global.local, vec->cpu); 60 + writel_relaxed(vec->local_id, local->msi_va); 61 + return 0; 62 + } 63 + 64 + static void imsic_irq_compose_vector_msg(struct imsic_vector *vec, struct msi_msg *msg) 65 + { 66 + phys_addr_t msi_addr; 67 + 68 + if (WARN_ON(!vec)) 69 + return; 70 + 71 + if (WARN_ON(!imsic_cpu_page_phys(vec->cpu, 0, &msi_addr))) 72 + return; 73 + 74 + msg->address_hi = upper_32_bits(msi_addr); 75 + msg->address_lo = lower_32_bits(msi_addr); 76 + msg->data = vec->local_id; 77 + } 78 + 79 + static void imsic_irq_compose_msg(struct irq_data *d, struct msi_msg *msg) 80 + { 81 + imsic_irq_compose_vector_msg(irq_data_get_irq_chip_data(d), msg); 82 + } 83 + 84 + #ifdef CONFIG_SMP 85 + static void imsic_msi_update_msg(struct irq_data *d, struct imsic_vector *vec) 86 + { 87 + struct msi_msg msg = { }; 88 + 89 + imsic_irq_compose_vector_msg(vec, &msg); 90 + irq_data_get_irq_chip(d)->irq_write_msi_msg(d, &msg); 91 + } 92 + 93 + static int imsic_irq_set_affinity(struct irq_data *d, const struct cpumask *mask_val, 94 + bool force) 95 + { 96 + struct imsic_vector *old_vec, *new_vec; 97 + struct irq_data *pd = d->parent_data; 98 + 99 + old_vec = irq_data_get_irq_chip_data(pd); 100 + if (WARN_ON(!old_vec)) 101 + return -ENOENT; 102 + 103 + /* If old vector cpu belongs to the target cpumask then do nothing */ 104 + if (cpumask_test_cpu(old_vec->cpu, mask_val)) 105 + return IRQ_SET_MASK_OK_DONE; 106 + 107 + /* If move is already in-flight then return failure */ 108 + if (imsic_vector_get_move(old_vec)) 109 + return -EBUSY; 110 + 111 + /* Get a new vector on the desired set of CPUs */ 112 + new_vec = imsic_vector_alloc(old_vec->hwirq, mask_val); 113 + if (!new_vec) 114 + return -ENOSPC; 115 + 116 + /* Point device to the new vector */ 117 + imsic_msi_update_msg(d, new_vec); 118 + 119 + /* Update irq descriptors with the new vector */ 120 + pd->chip_data = new_vec; 121 + 122 + /* Update effective affinity of parent irq data */ 123 + irq_data_update_effective_affinity(pd, cpumask_of(new_vec->cpu)); 124 + 125 + /* Move state of the old vector to the new vector */ 126 + imsic_vector_move(old_vec, new_vec); 127 + 128 + return IRQ_SET_MASK_OK_DONE; 129 + } 130 + #endif 131 + 132 + static struct irq_chip imsic_irq_base_chip = { 133 + .name = "IMSIC", 134 + .irq_mask = imsic_irq_mask, 135 + .irq_unmask = imsic_irq_unmask, 136 + .irq_retrigger = imsic_irq_retrigger, 137 + .irq_compose_msi_msg = imsic_irq_compose_msg, 138 + .flags = IRQCHIP_SKIP_SET_WAKE | 139 + IRQCHIP_MASK_ON_SUSPEND, 140 + }; 141 + 142 + static int imsic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, 143 + unsigned int nr_irqs, void *args) 144 + { 145 + struct imsic_vector *vec; 146 + 147 + /* Multi-MSI is not supported yet. */ 148 + if (nr_irqs > 1) 149 + return -EOPNOTSUPP; 150 + 151 + vec = imsic_vector_alloc(virq, cpu_online_mask); 152 + if (!vec) 153 + return -ENOSPC; 154 + 155 + irq_domain_set_info(domain, virq, virq, &imsic_irq_base_chip, vec, 156 + handle_simple_irq, NULL, NULL); 157 + irq_set_noprobe(virq); 158 + irq_set_affinity(virq, cpu_online_mask); 159 + 160 + return 0; 161 + } 162 + 163 + static void imsic_irq_domain_free(struct irq_domain *domain, unsigned int virq, 164 + unsigned int nr_irqs) 165 + { 166 + struct irq_data *d = irq_domain_get_irq_data(domain, virq); 167 + 168 + imsic_vector_free(irq_data_get_irq_chip_data(d)); 169 + irq_domain_free_irqs_parent(domain, virq, nr_irqs); 170 + } 171 + 172 + static int imsic_irq_domain_select(struct irq_domain *domain, struct irq_fwspec *fwspec, 173 + enum irq_domain_bus_token bus_token) 174 + { 175 + const struct msi_parent_ops *ops = domain->msi_parent_ops; 176 + u32 busmask = BIT(bus_token); 177 + 178 + if (fwspec->fwnode != domain->fwnode || fwspec->param_count != 0) 179 + return 0; 180 + 181 + /* Handle pure domain searches */ 182 + if (bus_token == ops->bus_select_token) 183 + return 1; 184 + 185 + return !!(ops->bus_select_mask & busmask); 186 + } 187 + 188 + #ifdef CONFIG_GENERIC_IRQ_DEBUGFS 189 + static void imsic_irq_debug_show(struct seq_file *m, struct irq_domain *d, 190 + struct irq_data *irqd, int ind) 191 + { 192 + if (!irqd) { 193 + imsic_vector_debug_show_summary(m, ind); 194 + return; 195 + } 196 + 197 + imsic_vector_debug_show(m, irq_data_get_irq_chip_data(irqd), ind); 198 + } 199 + #endif 200 + 201 + static const struct irq_domain_ops imsic_base_domain_ops = { 202 + .alloc = imsic_irq_domain_alloc, 203 + .free = imsic_irq_domain_free, 204 + .select = imsic_irq_domain_select, 205 + #ifdef CONFIG_GENERIC_IRQ_DEBUGFS 206 + .debug_show = imsic_irq_debug_show, 207 + #endif 208 + }; 209 + 210 + static bool imsic_init_dev_msi_info(struct device *dev, 211 + struct irq_domain *domain, 212 + struct irq_domain *real_parent, 213 + struct msi_domain_info *info) 214 + { 215 + const struct msi_parent_ops *pops = real_parent->msi_parent_ops; 216 + 217 + /* MSI parent domain specific settings */ 218 + switch (real_parent->bus_token) { 219 + case DOMAIN_BUS_NEXUS: 220 + if (WARN_ON_ONCE(domain != real_parent)) 221 + return false; 222 + #ifdef CONFIG_SMP 223 + info->chip->irq_set_affinity = imsic_irq_set_affinity; 224 + #endif 225 + break; 226 + default: 227 + WARN_ON_ONCE(1); 228 + return false; 229 + } 230 + 231 + /* Is the target supported? */ 232 + switch (info->bus_token) { 233 + case DOMAIN_BUS_DEVICE_MSI: 234 + /* 235 + * Per-device MSI should never have any MSI feature bits 236 + * set. It's sole purpose is to create a dumb interrupt 237 + * chip which has a device specific irq_write_msi_msg() 238 + * callback. 239 + */ 240 + if (WARN_ON_ONCE(info->flags)) 241 + return false; 242 + 243 + /* Core managed MSI descriptors */ 244 + info->flags |= MSI_FLAG_ALLOC_SIMPLE_MSI_DESCS | 245 + MSI_FLAG_FREE_MSI_DESCS; 246 + break; 247 + case DOMAIN_BUS_WIRED_TO_MSI: 248 + break; 249 + default: 250 + WARN_ON_ONCE(1); 251 + return false; 252 + } 253 + 254 + /* Use hierarchial chip operations re-trigger */ 255 + info->chip->irq_retrigger = irq_chip_retrigger_hierarchy; 256 + 257 + /* 258 + * Mask out the domain specific MSI feature flags which are not 259 + * supported by the real parent. 260 + */ 261 + info->flags &= pops->supported_flags; 262 + 263 + /* Enforce the required flags */ 264 + info->flags |= pops->required_flags; 265 + 266 + return true; 267 + } 268 + 269 + #define MATCH_PLATFORM_MSI BIT(DOMAIN_BUS_PLATFORM_MSI) 270 + 271 + static const struct msi_parent_ops imsic_msi_parent_ops = { 272 + .supported_flags = MSI_GENERIC_FLAGS_MASK, 273 + .required_flags = MSI_FLAG_USE_DEF_DOM_OPS | 274 + MSI_FLAG_USE_DEF_CHIP_OPS, 275 + .bus_select_token = DOMAIN_BUS_NEXUS, 276 + .bus_select_mask = MATCH_PLATFORM_MSI, 277 + .init_dev_msi_info = imsic_init_dev_msi_info, 278 + }; 279 + 280 + int imsic_irqdomain_init(void) 281 + { 282 + struct imsic_global_config *global; 283 + 284 + if (!imsic || !imsic->fwnode) { 285 + pr_err("early driver not probed\n"); 286 + return -ENODEV; 287 + } 288 + 289 + if (imsic->base_domain) { 290 + pr_err("%pfwP: irq domain already created\n", imsic->fwnode); 291 + return -ENODEV; 292 + } 293 + 294 + /* Create Base IRQ domain */ 295 + imsic->base_domain = irq_domain_create_tree(imsic->fwnode, 296 + &imsic_base_domain_ops, imsic); 297 + if (!imsic->base_domain) { 298 + pr_err("%pfwP: failed to create IMSIC base domain\n", imsic->fwnode); 299 + return -ENOMEM; 300 + } 301 + imsic->base_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT; 302 + imsic->base_domain->msi_parent_ops = &imsic_msi_parent_ops; 303 + 304 + irq_domain_update_bus_token(imsic->base_domain, DOMAIN_BUS_NEXUS); 305 + 306 + global = &imsic->global; 307 + pr_info("%pfwP: hart-index-bits: %d, guest-index-bits: %d\n", 308 + imsic->fwnode, global->hart_index_bits, global->guest_index_bits); 309 + pr_info("%pfwP: group-index-bits: %d, group-index-shift: %d\n", 310 + imsic->fwnode, global->group_index_bits, global->group_index_shift); 311 + pr_info("%pfwP: per-CPU IDs %d at base PPN %pa\n", 312 + imsic->fwnode, global->nr_ids, &global->base_addr); 313 + pr_info("%pfwP: total %d interrupts available\n", 314 + imsic->fwnode, num_possible_cpus() * (global->nr_ids - 1)); 315 + 316 + return 0; 317 + } 318 + 319 + static int imsic_platform_probe(struct platform_device *pdev) 320 + { 321 + struct device *dev = &pdev->dev; 322 + 323 + if (imsic && imsic->fwnode != dev->fwnode) { 324 + dev_err(dev, "fwnode mismatch\n"); 325 + return -ENODEV; 326 + } 327 + 328 + return imsic_irqdomain_init(); 329 + } 330 + 331 + static const struct of_device_id imsic_platform_match[] = { 332 + { .compatible = "riscv,imsics" }, 333 + {} 334 + }; 335 + 336 + static struct platform_driver imsic_platform_driver = { 337 + .driver = { 338 + .name = "riscv-imsic", 339 + .of_match_table = imsic_platform_match, 340 + }, 341 + .probe = imsic_platform_probe, 342 + }; 343 + builtin_platform_driver(imsic_platform_driver);
+1
drivers/irqchip/irq-riscv-imsic-state.h
··· 103 103 void imsic_state_online(void); 104 104 void imsic_state_offline(void); 105 105 int imsic_setup_state(struct fwnode_handle *fwnode); 106 + int imsic_irqdomain_init(void); 106 107 107 108 #endif