xref: /linux/drivers/soc/ti/ti_sci_inta_msi.c (revision 1e0731c05c985deb68a97fa44c1adcd3305dda90)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Texas Instruments' K3 Interrupt Aggregator MSI bus
4  *
5  * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
6  *	Lokesh Vutla <lokeshvutla@ti.com>
7  */
8 
9 #include <linux/irq.h>
10 #include <linux/irqdomain.h>
11 #include <linux/msi.h>
12 #include <linux/of.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/soc/ti/ti_sci_inta_msi.h>
17 #include <linux/soc/ti/ti_sci_protocol.h>
18 
19 static void ti_sci_inta_msi_write_msg(struct irq_data *data,
20 				      struct msi_msg *msg)
21 {
22 	/* Nothing to do */
23 }
24 
25 static void ti_sci_inta_msi_compose_msi_msg(struct irq_data *data,
26 					    struct msi_msg *msg)
27 {
28 	/* Nothing to do */
29 }
30 
31 static void ti_sci_inta_msi_update_chip_ops(struct msi_domain_info *info)
32 {
33 	struct irq_chip *chip = info->chip;
34 
35 	if (WARN_ON(!chip))
36 		return;
37 
38 	chip->irq_request_resources = irq_chip_request_resources_parent;
39 	chip->irq_release_resources = irq_chip_release_resources_parent;
40 	chip->irq_compose_msi_msg = ti_sci_inta_msi_compose_msi_msg;
41 	chip->irq_write_msi_msg = ti_sci_inta_msi_write_msg;
42 	chip->irq_set_type = irq_chip_set_type_parent;
43 	chip->irq_unmask = irq_chip_unmask_parent;
44 	chip->irq_mask = irq_chip_mask_parent;
45 	chip->irq_ack = irq_chip_ack_parent;
46 }
47 
48 struct irq_domain *ti_sci_inta_msi_create_irq_domain(struct fwnode_handle *fwnode,
49 						     struct msi_domain_info *info,
50 						     struct irq_domain *parent)
51 {
52 	struct irq_domain *domain;
53 
54 	ti_sci_inta_msi_update_chip_ops(info);
55 	info->flags |= MSI_FLAG_FREE_MSI_DESCS;
56 
57 	domain = msi_create_irq_domain(fwnode, info, parent);
58 	if (domain)
59 		irq_domain_update_bus_token(domain, DOMAIN_BUS_TI_SCI_INTA_MSI);
60 
61 	return domain;
62 }
63 EXPORT_SYMBOL_GPL(ti_sci_inta_msi_create_irq_domain);
64 
65 static int ti_sci_inta_msi_alloc_descs(struct device *dev,
66 				       struct ti_sci_resource *res)
67 {
68 	struct msi_desc msi_desc;
69 	int set, i, count = 0;
70 
71 	memset(&msi_desc, 0, sizeof(msi_desc));
72 	msi_desc.nvec_used = 1;
73 
74 	for (set = 0; set < res->sets; set++) {
75 		for (i = 0; i < res->desc[set].num; i++, count++) {
76 			msi_desc.msi_index = res->desc[set].start + i;
77 			if (msi_insert_msi_desc(dev, &msi_desc))
78 				goto fail;
79 		}
80 
81 		for (i = 0; i < res->desc[set].num_sec; i++, count++) {
82 			msi_desc.msi_index = res->desc[set].start_sec + i;
83 			if (msi_insert_msi_desc(dev, &msi_desc))
84 				goto fail;
85 		}
86 	}
87 	return count;
88 fail:
89 	msi_free_msi_descs(dev);
90 	return -ENOMEM;
91 }
92 
93 int ti_sci_inta_msi_domain_alloc_irqs(struct device *dev,
94 				      struct ti_sci_resource *res)
95 {
96 	struct platform_device *pdev = to_platform_device(dev);
97 	int ret, nvec;
98 
99 	if (pdev->id < 0)
100 		return -ENODEV;
101 
102 	ret = msi_setup_device_data(dev);
103 	if (ret)
104 		return ret;
105 
106 	msi_lock_descs(dev);
107 	nvec = ti_sci_inta_msi_alloc_descs(dev, res);
108 	if (nvec <= 0) {
109 		ret = nvec;
110 		goto unlock;
111 	}
112 
113 	/* Use alloc ALL as it's unclear whether there are gaps in the indices */
114 	ret = msi_domain_alloc_irqs_all_locked(dev, MSI_DEFAULT_DOMAIN, nvec);
115 	if (ret)
116 		dev_err(dev, "Failed to allocate IRQs %d\n", ret);
117 unlock:
118 	msi_unlock_descs(dev);
119 	return ret;
120 }
121 EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_alloc_irqs);
122