1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Texas Instruments' K3 Interrupt Router irqchip driver 4 * 5 * Copyright (C) 2018-2019 Texas Instruments Incorporated - https://www.ti.com/ 6 * Lokesh Vutla <lokeshvutla@ti.com> 7 */ 8 9 #include <linux/err.h> 10 #include <linux/module.h> 11 #include <linux/moduleparam.h> 12 #include <linux/io.h> 13 #include <linux/irqchip.h> 14 #include <linux/irqdomain.h> 15 #include <linux/of.h> 16 #include <linux/of_irq.h> 17 #include <linux/platform_device.h> 18 #include <linux/soc/ti/ti_sci_protocol.h> 19 20 /** 21 * struct ti_sci_intr_irq_domain - Structure representing a TISCI based 22 * Interrupt Router IRQ domain. 23 * @sci: Pointer to TISCI handle 24 * @out_irqs: TISCI resource pointer representing INTR irqs. 25 * @dev: Struct device pointer. 26 * @ti_sci_id: TI-SCI device identifier 27 * @type: Specifies the trigger type supported by this Interrupt Router 28 */ 29 struct ti_sci_intr_irq_domain { 30 const struct ti_sci_handle *sci; 31 struct ti_sci_resource *out_irqs; 32 struct device *dev; 33 u32 ti_sci_id; 34 u32 type; 35 }; 36 37 static struct irq_chip ti_sci_intr_irq_chip = { 38 .name = "INTR", 39 .irq_eoi = irq_chip_eoi_parent, 40 .irq_mask = irq_chip_mask_parent, 41 .irq_unmask = irq_chip_unmask_parent, 42 .irq_set_type = irq_chip_set_type_parent, 43 .irq_retrigger = irq_chip_retrigger_hierarchy, 44 .irq_set_affinity = irq_chip_set_affinity_parent, 45 }; 46 47 /** 48 * ti_sci_intr_irq_domain_translate() - Retrieve hwirq and type from 49 * IRQ firmware specific handler. 50 * @domain: Pointer to IRQ domain 51 * @fwspec: Pointer to IRQ specific firmware structure 52 * @hwirq: IRQ number identified by hardware 53 * @type: IRQ type 54 * 55 * Return 0 if all went ok else appropriate error. 56 */ 57 static int ti_sci_intr_irq_domain_translate(struct irq_domain *domain, 58 struct irq_fwspec *fwspec, 59 unsigned long *hwirq, 60 unsigned int *type) 61 { 62 struct ti_sci_intr_irq_domain *intr = domain->host_data; 63 64 if (intr->type) { 65 /* Global interrupt-type */ 66 if (fwspec->param_count != 1) 67 return -EINVAL; 68 69 *hwirq = fwspec->param[0]; 70 *type = intr->type; 71 } else { 72 /* Per-Line interrupt-type */ 73 if (fwspec->param_count != 2) 74 return -EINVAL; 75 76 *hwirq = fwspec->param[0]; 77 *type = fwspec->param[1]; 78 } 79 return 0; 80 } 81 82 /** 83 * ti_sci_intr_xlate_irq() - Translate hwirq to parent's hwirq. 84 * @intr: IRQ domain corresponding to Interrupt Router 85 * @irq: Hardware irq corresponding to the above irq domain 86 * 87 * Return parent irq number if translation is available else -ENOENT. 88 */ 89 static int ti_sci_intr_xlate_irq(struct ti_sci_intr_irq_domain *intr, u32 irq) 90 { 91 struct device_node *np = dev_of_node(intr->dev); 92 u32 base, pbase, size, len; 93 const __be32 *range; 94 95 range = of_get_property(np, "ti,interrupt-ranges", &len); 96 if (!range) 97 return irq; 98 99 for (len /= sizeof(*range); len >= 3; len -= 3) { 100 base = be32_to_cpu(*range++); 101 pbase = be32_to_cpu(*range++); 102 size = be32_to_cpu(*range++); 103 104 if (base <= irq && irq < base + size) 105 return irq - base + pbase; 106 } 107 108 return -ENOENT; 109 } 110 111 /** 112 * ti_sci_intr_irq_domain_free() - Free the specified IRQs from the domain. 113 * @domain: Domain to which the irqs belong 114 * @virq: Linux virtual IRQ to be freed. 115 * @nr_irqs: Number of continuous irqs to be freed 116 */ 117 static void ti_sci_intr_irq_domain_free(struct irq_domain *domain, 118 unsigned int virq, unsigned int nr_irqs) 119 { 120 struct ti_sci_intr_irq_domain *intr = domain->host_data; 121 struct irq_data *data; 122 int out_irq; 123 124 data = irq_domain_get_irq_data(domain, virq); 125 out_irq = (uintptr_t)data->chip_data; 126 127 intr->sci->ops.rm_irq_ops.free_irq(intr->sci, 128 intr->ti_sci_id, data->hwirq, 129 intr->ti_sci_id, out_irq); 130 ti_sci_release_resource(intr->out_irqs, out_irq); 131 irq_domain_free_irqs_parent(domain, virq, 1); 132 irq_domain_reset_irq_data(data); 133 } 134 135 /** 136 * ti_sci_intr_alloc_parent_irq() - Allocate parent IRQ 137 * @domain: Pointer to the interrupt router IRQ domain 138 * @virq: Corresponding Linux virtual IRQ number 139 * @hwirq: Corresponding hwirq for the IRQ within this IRQ domain 140 * @hwirq_type: Corresponding hwirq trigger type for the IRQ within this IRQ domain 141 * 142 * Returns intr output irq if all went well else appropriate error pointer. 143 */ 144 static int ti_sci_intr_alloc_parent_irq(struct irq_domain *domain, unsigned int virq, 145 u32 hwirq, u32 hwirq_type) 146 { 147 struct ti_sci_intr_irq_domain *intr = domain->host_data; 148 struct device_node *parent_node; 149 struct irq_fwspec fwspec; 150 int p_hwirq, err = 0; 151 u16 out_irq; 152 153 out_irq = ti_sci_get_free_resource(intr->out_irqs); 154 if (out_irq == TI_SCI_RESOURCE_NULL) 155 return -EINVAL; 156 157 p_hwirq = ti_sci_intr_xlate_irq(intr, out_irq); 158 if (p_hwirq < 0) 159 goto err_irqs; 160 161 parent_node = of_irq_find_parent(dev_of_node(intr->dev)); 162 fwspec.fwnode = of_fwnode_handle(parent_node); 163 164 if (of_device_is_compatible(parent_node, "arm,gic-v3")) { 165 /* Parent is GIC */ 166 fwspec.param_count = 3; 167 fwspec.param[0] = 0; /* SPI */ 168 fwspec.param[1] = p_hwirq - 32; /* SPI offset */ 169 fwspec.param[2] = hwirq_type; 170 } else { 171 /* Parent is Interrupt Router */ 172 u32 parent_trigger_type; 173 174 if (!of_property_read_u32(parent_node, "ti,intr-trigger-type", 175 &parent_trigger_type)) { 176 /* Parent has global trigger type */ 177 fwspec.param_count = 1; 178 fwspec.param[0] = p_hwirq; 179 } else { 180 /* Parent supports per-line trigger types */ 181 fwspec.param_count = 2; 182 fwspec.param[0] = p_hwirq; 183 fwspec.param[1] = hwirq_type; 184 } 185 } 186 187 err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); 188 if (err) 189 goto err_irqs; 190 191 err = intr->sci->ops.rm_irq_ops.set_irq(intr->sci, 192 intr->ti_sci_id, hwirq, 193 intr->ti_sci_id, out_irq); 194 if (err) 195 goto err_msg; 196 197 return out_irq; 198 199 err_msg: 200 irq_domain_free_irqs_parent(domain, virq, 1); 201 err_irqs: 202 ti_sci_release_resource(intr->out_irqs, out_irq); 203 return err; 204 } 205 206 /** 207 * ti_sci_intr_irq_domain_alloc() - Allocate Interrupt router IRQs 208 * @domain: Point to the interrupt router IRQ domain 209 * @virq: Corresponding Linux virtual IRQ number 210 * @nr_irqs: Continuous irqs to be allocated 211 * @data: Pointer to firmware specifier 212 * 213 * Return 0 if all went well else appropriate error value. 214 */ 215 static int ti_sci_intr_irq_domain_alloc(struct irq_domain *domain, 216 unsigned int virq, unsigned int nr_irqs, 217 void *data) 218 { 219 struct irq_fwspec *fwspec = data; 220 unsigned int hwirq_type; 221 unsigned long hwirq; 222 int err, out_irq; 223 224 err = ti_sci_intr_irq_domain_translate(domain, fwspec, &hwirq, &hwirq_type); 225 if (err) 226 return err; 227 228 out_irq = ti_sci_intr_alloc_parent_irq(domain, virq, hwirq, hwirq_type); 229 if (out_irq < 0) 230 return out_irq; 231 232 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, 233 &ti_sci_intr_irq_chip, 234 (void *)(uintptr_t)out_irq); 235 236 return 0; 237 } 238 239 static const struct irq_domain_ops ti_sci_intr_irq_domain_ops = { 240 .free = ti_sci_intr_irq_domain_free, 241 .alloc = ti_sci_intr_irq_domain_alloc, 242 .translate = ti_sci_intr_irq_domain_translate, 243 }; 244 245 static int ti_sci_intr_irq_domain_probe(struct platform_device *pdev) 246 { 247 struct irq_domain *parent_domain, *domain; 248 struct ti_sci_intr_irq_domain *intr; 249 struct device_node *parent_node; 250 struct device *dev = &pdev->dev; 251 int ret; 252 253 parent_node = of_irq_find_parent(dev_of_node(dev)); 254 if (!parent_node) { 255 dev_err(dev, "Failed to get IRQ parent node\n"); 256 return -ENODEV; 257 } 258 259 parent_domain = irq_find_host(parent_node); 260 of_node_put(parent_node); 261 if (!parent_domain) { 262 dev_err(dev, "Failed to find IRQ parent domain\n"); 263 return -ENODEV; 264 } 265 266 intr = devm_kzalloc(dev, sizeof(*intr), GFP_KERNEL); 267 if (!intr) 268 return -ENOMEM; 269 270 intr->dev = dev; 271 272 if (of_property_read_u32(dev_of_node(dev), "ti,intr-trigger-type", &intr->type)) 273 intr->type = IRQ_TYPE_NONE; 274 275 intr->sci = devm_ti_sci_get_by_phandle(dev, "ti,sci"); 276 if (IS_ERR(intr->sci)) 277 return dev_err_probe(dev, PTR_ERR(intr->sci), 278 "ti,sci read fail\n"); 279 280 ret = of_property_read_u32(dev_of_node(dev), "ti,sci-dev-id", 281 &intr->ti_sci_id); 282 if (ret) { 283 dev_err(dev, "missing 'ti,sci-dev-id' property\n"); 284 return -EINVAL; 285 } 286 287 intr->out_irqs = devm_ti_sci_get_resource(intr->sci, dev, 288 intr->ti_sci_id, 289 TI_SCI_RESASG_SUBTYPE_IR_OUTPUT); 290 if (IS_ERR(intr->out_irqs)) { 291 dev_err(dev, "Destination irq resource allocation failed\n"); 292 return PTR_ERR(intr->out_irqs); 293 } 294 295 domain = irq_domain_create_hierarchy(parent_domain, 0, 0, dev_fwnode(dev), 296 &ti_sci_intr_irq_domain_ops, intr); 297 if (!domain) { 298 dev_err(dev, "Failed to allocate IRQ domain\n"); 299 return -ENOMEM; 300 } 301 302 dev_info(dev, "Interrupt Router %d domain created\n", intr->ti_sci_id); 303 304 return 0; 305 } 306 307 static const struct of_device_id ti_sci_intr_irq_domain_of_match[] = { 308 { .compatible = "ti,sci-intr", }, 309 { /* sentinel */ }, 310 }; 311 MODULE_DEVICE_TABLE(of, ti_sci_intr_irq_domain_of_match); 312 313 static struct platform_driver ti_sci_intr_irq_domain_driver = { 314 .probe = ti_sci_intr_irq_domain_probe, 315 .driver = { 316 .name = "ti-sci-intr", 317 .of_match_table = ti_sci_intr_irq_domain_of_match, 318 }, 319 }; 320 module_platform_driver(ti_sci_intr_irq_domain_driver); 321 322 MODULE_AUTHOR("Lokesh Vutla <lokeshvutla@ticom>"); 323 MODULE_DESCRIPTION("K3 Interrupt Router driver over TI SCI protocol"); 324 MODULE_LICENSE("GPL"); 325