1c6674154SChen Wang // SPDX-License-Identifier: GPL-2.0
2c6674154SChen Wang /*
3c6674154SChen Wang * SG2042 MSI Controller
4c6674154SChen Wang *
5c6674154SChen Wang * Copyright (C) 2024 Sophgo Technology Inc.
6c6674154SChen Wang * Copyright (C) 2024 Chen Wang <unicorn_wang@outlook.com>
7c6674154SChen Wang */
8c6674154SChen Wang
9c6674154SChen Wang #include <linux/cleanup.h>
10c6674154SChen Wang #include <linux/io.h>
11c6674154SChen Wang #include <linux/irq.h>
12c6674154SChen Wang #include <linux/irqdomain.h>
13c6674154SChen Wang #include <linux/kernel.h>
14c6674154SChen Wang #include <linux/module.h>
15c6674154SChen Wang #include <linux/msi.h>
16c6674154SChen Wang #include <linux/platform_device.h>
17c6674154SChen Wang #include <linux/property.h>
18c6674154SChen Wang #include <linux/slab.h>
19c6674154SChen Wang
20*e51b2743SMarc Zyngier #include <linux/irqchip/irq-msi-lib.h>
21c6674154SChen Wang
22bad2094eSInochi Amaoto struct sg204x_msi_chip_info {
23bad2094eSInochi Amaoto const struct irq_chip *irqchip;
24bad2094eSInochi Amaoto const struct msi_parent_ops *parent_ops;
25c6674154SChen Wang };
26c6674154SChen Wang
27bad2094eSInochi Amaoto /**
28bad2094eSInochi Amaoto * struct sg204x_msi_chipdata - chip data for the SG204x MSI IRQ controller
29bad2094eSInochi Amaoto * @reg_clr: clear reg, see TRM, 10.1.33, GP_INTR0_CLR
30bad2094eSInochi Amaoto * @doorbell_addr: see TRM, 10.1.32, GP_INTR0_SET
31bad2094eSInochi Amaoto * @irq_first: First vectors number that MSIs starts
32bad2094eSInochi Amaoto * @num_irqs: Number of vectors for MSIs
33bad2094eSInochi Amaoto * @msi_map: mapping for allocated MSI vectors.
34bad2094eSInochi Amaoto * @msi_map_lock: Lock for msi_map
35bad2094eSInochi Amaoto * @chip_info: chip specific infomations
36bad2094eSInochi Amaoto */
37bced5549SInochi Amaoto struct sg204x_msi_chipdata {
38bad2094eSInochi Amaoto void __iomem *reg_clr;
39c6674154SChen Wang
40bad2094eSInochi Amaoto phys_addr_t doorbell_addr;
41c6674154SChen Wang
42bad2094eSInochi Amaoto u32 irq_first;
43bad2094eSInochi Amaoto u32 num_irqs;
44c6674154SChen Wang
45bad2094eSInochi Amaoto unsigned long *msi_map;
46bad2094eSInochi Amaoto struct mutex msi_map_lock;
47bad2094eSInochi Amaoto
48bad2094eSInochi Amaoto const struct sg204x_msi_chip_info *chip_info;
49c6674154SChen Wang };
50c6674154SChen Wang
sg204x_msi_allocate_hwirq(struct sg204x_msi_chipdata * data,int num_req)51bced5549SInochi Amaoto static int sg204x_msi_allocate_hwirq(struct sg204x_msi_chipdata *data, int num_req)
52c6674154SChen Wang {
53c6674154SChen Wang int first;
54c6674154SChen Wang
55c6674154SChen Wang guard(mutex)(&data->msi_map_lock);
56c6674154SChen Wang first = bitmap_find_free_region(data->msi_map, data->num_irqs,
57c6674154SChen Wang get_count_order(num_req));
58c6674154SChen Wang return first >= 0 ? first : -ENOSPC;
59c6674154SChen Wang }
60c6674154SChen Wang
sg204x_msi_free_hwirq(struct sg204x_msi_chipdata * data,int hwirq,int num_req)61bced5549SInochi Amaoto static void sg204x_msi_free_hwirq(struct sg204x_msi_chipdata *data, int hwirq, int num_req)
62c6674154SChen Wang {
63c6674154SChen Wang guard(mutex)(&data->msi_map_lock);
64c6674154SChen Wang bitmap_release_region(data->msi_map, hwirq, get_count_order(num_req));
65c6674154SChen Wang }
66c6674154SChen Wang
sg2042_msi_irq_ack(struct irq_data * d)67c6674154SChen Wang static void sg2042_msi_irq_ack(struct irq_data *d)
68c6674154SChen Wang {
69bced5549SInochi Amaoto struct sg204x_msi_chipdata *data = irq_data_get_irq_chip_data(d);
70c6674154SChen Wang int bit_off = d->hwirq;
71c6674154SChen Wang
72c6674154SChen Wang writel(1 << bit_off, data->reg_clr);
73c6674154SChen Wang
74c6674154SChen Wang irq_chip_ack_parent(d);
75c6674154SChen Wang }
76c6674154SChen Wang
sg2042_msi_irq_compose_msi_msg(struct irq_data * d,struct msi_msg * msg)77c6674154SChen Wang static void sg2042_msi_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
78c6674154SChen Wang {
79bced5549SInochi Amaoto struct sg204x_msi_chipdata *data = irq_data_get_irq_chip_data(d);
80c6674154SChen Wang
81c6674154SChen Wang msg->address_hi = upper_32_bits(data->doorbell_addr);
82c6674154SChen Wang msg->address_lo = lower_32_bits(data->doorbell_addr);
83c6674154SChen Wang msg->data = 1 << d->hwirq;
84c6674154SChen Wang }
85c6674154SChen Wang
86c6674154SChen Wang static const struct irq_chip sg2042_msi_middle_irq_chip = {
87c6674154SChen Wang .name = "SG2042 MSI",
88c6674154SChen Wang .irq_ack = sg2042_msi_irq_ack,
89c6674154SChen Wang .irq_mask = irq_chip_mask_parent,
90c6674154SChen Wang .irq_unmask = irq_chip_unmask_parent,
91c6674154SChen Wang #ifdef CONFIG_SMP
92c6674154SChen Wang .irq_set_affinity = irq_chip_set_affinity_parent,
93c6674154SChen Wang #endif
94c6674154SChen Wang .irq_compose_msi_msg = sg2042_msi_irq_compose_msi_msg,
95c6674154SChen Wang };
96c6674154SChen Wang
sg2044_msi_irq_ack(struct irq_data * d)97e96b93a9SInochi Amaoto static void sg2044_msi_irq_ack(struct irq_data *d)
98c6674154SChen Wang {
99e96b93a9SInochi Amaoto struct sg204x_msi_chipdata *data = irq_data_get_irq_chip_data(d);
100e96b93a9SInochi Amaoto
10176b66e8cSInochi Amaoto writel(0, (u32 __iomem *)data->reg_clr + d->hwirq);
102e96b93a9SInochi Amaoto irq_chip_ack_parent(d);
103e96b93a9SInochi Amaoto }
104e96b93a9SInochi Amaoto
sg2044_msi_irq_compose_msi_msg(struct irq_data * d,struct msi_msg * msg)105e96b93a9SInochi Amaoto static void sg2044_msi_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
106e96b93a9SInochi Amaoto {
107e96b93a9SInochi Amaoto struct sg204x_msi_chipdata *data = irq_data_get_irq_chip_data(d);
108e96b93a9SInochi Amaoto phys_addr_t doorbell = data->doorbell_addr + 4 * (d->hwirq / 32);
109e96b93a9SInochi Amaoto
110e96b93a9SInochi Amaoto msg->address_lo = lower_32_bits(doorbell);
111e96b93a9SInochi Amaoto msg->address_hi = upper_32_bits(doorbell);
112e96b93a9SInochi Amaoto msg->data = d->hwirq % 32;
113e96b93a9SInochi Amaoto }
114e96b93a9SInochi Amaoto
115e96b93a9SInochi Amaoto static struct irq_chip sg2044_msi_middle_irq_chip = {
116e96b93a9SInochi Amaoto .name = "SG2044 MSI",
117e96b93a9SInochi Amaoto .irq_ack = sg2044_msi_irq_ack,
118e96b93a9SInochi Amaoto .irq_mask = irq_chip_mask_parent,
119e96b93a9SInochi Amaoto .irq_unmask = irq_chip_unmask_parent,
120e96b93a9SInochi Amaoto #ifdef CONFIG_SMP
121e96b93a9SInochi Amaoto .irq_set_affinity = irq_chip_set_affinity_parent,
122e96b93a9SInochi Amaoto #endif
123e96b93a9SInochi Amaoto .irq_compose_msi_msg = sg2044_msi_irq_compose_msi_msg,
124e96b93a9SInochi Amaoto };
125e96b93a9SInochi Amaoto
sg204x_msi_parent_domain_alloc(struct irq_domain * domain,unsigned int virq,int hwirq)126bced5549SInochi Amaoto static int sg204x_msi_parent_domain_alloc(struct irq_domain *domain, unsigned int virq, int hwirq)
127c6674154SChen Wang {
128bced5549SInochi Amaoto struct sg204x_msi_chipdata *data = domain->host_data;
129c6674154SChen Wang struct irq_fwspec fwspec;
130c6674154SChen Wang struct irq_data *d;
131c6674154SChen Wang int ret;
132c6674154SChen Wang
133c6674154SChen Wang fwspec.fwnode = domain->parent->fwnode;
134c6674154SChen Wang fwspec.param_count = 2;
135c6674154SChen Wang fwspec.param[0] = data->irq_first + hwirq;
136c6674154SChen Wang fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
137c6674154SChen Wang
138c6674154SChen Wang ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
139c6674154SChen Wang if (ret)
140c6674154SChen Wang return ret;
141c6674154SChen Wang
142c6674154SChen Wang d = irq_domain_get_irq_data(domain->parent, virq);
143c6674154SChen Wang return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
144c6674154SChen Wang }
145c6674154SChen Wang
sg204x_msi_middle_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)146bced5549SInochi Amaoto static int sg204x_msi_middle_domain_alloc(struct irq_domain *domain, unsigned int virq,
147c6674154SChen Wang unsigned int nr_irqs, void *args)
148c6674154SChen Wang {
149bced5549SInochi Amaoto struct sg204x_msi_chipdata *data = domain->host_data;
150c6674154SChen Wang int hwirq, err, i;
151c6674154SChen Wang
152bced5549SInochi Amaoto hwirq = sg204x_msi_allocate_hwirq(data, nr_irqs);
153c6674154SChen Wang if (hwirq < 0)
154c6674154SChen Wang return hwirq;
155c6674154SChen Wang
156c6674154SChen Wang for (i = 0; i < nr_irqs; i++) {
157bced5549SInochi Amaoto err = sg204x_msi_parent_domain_alloc(domain, virq + i, hwirq + i);
158c6674154SChen Wang if (err)
159c6674154SChen Wang goto err_hwirq;
160c6674154SChen Wang
161c6674154SChen Wang irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
162bad2094eSInochi Amaoto data->chip_info->irqchip, data);
163c6674154SChen Wang }
164c6674154SChen Wang return 0;
165c6674154SChen Wang
166c6674154SChen Wang err_hwirq:
167bced5549SInochi Amaoto sg204x_msi_free_hwirq(data, hwirq, nr_irqs);
168c6674154SChen Wang irq_domain_free_irqs_parent(domain, virq, i);
169c6674154SChen Wang return err;
170c6674154SChen Wang }
171c6674154SChen Wang
sg204x_msi_middle_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)172bced5549SInochi Amaoto static void sg204x_msi_middle_domain_free(struct irq_domain *domain, unsigned int virq,
173c6674154SChen Wang unsigned int nr_irqs)
174c6674154SChen Wang {
175c6674154SChen Wang struct irq_data *d = irq_domain_get_irq_data(domain, virq);
176bced5549SInochi Amaoto struct sg204x_msi_chipdata *data = irq_data_get_irq_chip_data(d);
177c6674154SChen Wang
178c6674154SChen Wang irq_domain_free_irqs_parent(domain, virq, nr_irqs);
179bced5549SInochi Amaoto sg204x_msi_free_hwirq(data, d->hwirq, nr_irqs);
180c6674154SChen Wang }
181c6674154SChen Wang
182bced5549SInochi Amaoto static const struct irq_domain_ops sg204x_msi_middle_domain_ops = {
183bced5549SInochi Amaoto .alloc = sg204x_msi_middle_domain_alloc,
184bced5549SInochi Amaoto .free = sg204x_msi_middle_domain_free,
185c6674154SChen Wang .select = msi_lib_irq_domain_select,
186c6674154SChen Wang };
187c6674154SChen Wang
188c6674154SChen Wang #define SG2042_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
189c6674154SChen Wang MSI_FLAG_USE_DEF_CHIP_OPS)
190c6674154SChen Wang
191c6674154SChen Wang #define SG2042_MSI_FLAGS_SUPPORTED MSI_GENERIC_FLAGS_MASK
192c6674154SChen Wang
193c6674154SChen Wang static const struct msi_parent_ops sg2042_msi_parent_ops = {
194c6674154SChen Wang .required_flags = SG2042_MSI_FLAGS_REQUIRED,
195c6674154SChen Wang .supported_flags = SG2042_MSI_FLAGS_SUPPORTED,
196305825d0SInochi Amaoto .chip_flags = MSI_CHIP_FLAG_SET_ACK,
197c6674154SChen Wang .bus_select_mask = MATCH_PCI_MSI,
198c6674154SChen Wang .bus_select_token = DOMAIN_BUS_NEXUS,
199c6674154SChen Wang .prefix = "SG2042-",
200c6674154SChen Wang .init_dev_msi_info = msi_lib_init_dev_msi_info,
201c6674154SChen Wang };
202c6674154SChen Wang
203e96b93a9SInochi Amaoto #define SG2044_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
204e96b93a9SInochi Amaoto MSI_FLAG_USE_DEF_CHIP_OPS)
205e96b93a9SInochi Amaoto
206e96b93a9SInochi Amaoto #define SG2044_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
207e96b93a9SInochi Amaoto MSI_FLAG_PCI_MSIX)
208e96b93a9SInochi Amaoto
209e96b93a9SInochi Amaoto static const struct msi_parent_ops sg2044_msi_parent_ops = {
210e96b93a9SInochi Amaoto .required_flags = SG2044_MSI_FLAGS_REQUIRED,
211e96b93a9SInochi Amaoto .supported_flags = SG2044_MSI_FLAGS_SUPPORTED,
212e96b93a9SInochi Amaoto .chip_flags = MSI_CHIP_FLAG_SET_EOI | MSI_CHIP_FLAG_SET_ACK,
213e96b93a9SInochi Amaoto .bus_select_mask = MATCH_PCI_MSI,
214e96b93a9SInochi Amaoto .bus_select_token = DOMAIN_BUS_NEXUS,
215e96b93a9SInochi Amaoto .prefix = "SG2044-",
216e96b93a9SInochi Amaoto .init_dev_msi_info = msi_lib_init_dev_msi_info,
217e96b93a9SInochi Amaoto };
218e96b93a9SInochi Amaoto
sg204x_msi_init_domains(struct sg204x_msi_chipdata * data,struct irq_domain * plic_domain,struct device * dev)219bced5549SInochi Amaoto static int sg204x_msi_init_domains(struct sg204x_msi_chipdata *data,
220c6674154SChen Wang struct irq_domain *plic_domain, struct device *dev)
221c6674154SChen Wang {
222c6674154SChen Wang struct fwnode_handle *fwnode = dev_fwnode(dev);
223c6674154SChen Wang struct irq_domain *middle_domain;
224c6674154SChen Wang
225c6674154SChen Wang middle_domain = irq_domain_create_hierarchy(plic_domain, 0, data->num_irqs, fwnode,
226bced5549SInochi Amaoto &sg204x_msi_middle_domain_ops, data);
227c6674154SChen Wang if (!middle_domain) {
228c6674154SChen Wang pr_err("Failed to create the MSI middle domain\n");
229c6674154SChen Wang return -ENOMEM;
230c6674154SChen Wang }
231c6674154SChen Wang
232c6674154SChen Wang irq_domain_update_bus_token(middle_domain, DOMAIN_BUS_NEXUS);
233c6674154SChen Wang
234c6674154SChen Wang middle_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT;
235bad2094eSInochi Amaoto middle_domain->msi_parent_ops = data->chip_info->parent_ops;
236c6674154SChen Wang return 0;
237c6674154SChen Wang }
238c6674154SChen Wang
sg2042_msi_probe(struct platform_device * pdev)239c6674154SChen Wang static int sg2042_msi_probe(struct platform_device *pdev)
240c6674154SChen Wang {
241c6674154SChen Wang struct fwnode_reference_args args = { };
242bced5549SInochi Amaoto struct sg204x_msi_chipdata *data;
243c6674154SChen Wang struct device *dev = &pdev->dev;
244c6674154SChen Wang struct irq_domain *plic_domain;
245c6674154SChen Wang struct resource *res;
246c6674154SChen Wang int ret;
247c6674154SChen Wang
248bced5549SInochi Amaoto data = devm_kzalloc(dev, sizeof(struct sg204x_msi_chipdata), GFP_KERNEL);
249c6674154SChen Wang if (!data)
250c6674154SChen Wang return -ENOMEM;
251c6674154SChen Wang
252bad2094eSInochi Amaoto data->chip_info = device_get_match_data(&pdev->dev);
253bad2094eSInochi Amaoto if (!data->chip_info) {
254bad2094eSInochi Amaoto dev_err(&pdev->dev, "Failed to get irqchip\n");
255bad2094eSInochi Amaoto return -EINVAL;
256bad2094eSInochi Amaoto }
257bad2094eSInochi Amaoto
258c6674154SChen Wang data->reg_clr = devm_platform_ioremap_resource_byname(pdev, "clr");
259c6674154SChen Wang if (IS_ERR(data->reg_clr)) {
260c6674154SChen Wang dev_err(dev, "Failed to map clear register\n");
261c6674154SChen Wang return PTR_ERR(data->reg_clr);
262c6674154SChen Wang }
263c6674154SChen Wang
264c6674154SChen Wang res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "doorbell");
265c6674154SChen Wang if (!res) {
266c6674154SChen Wang dev_err(dev, "Failed get resource from set\n");
267c6674154SChen Wang return -EINVAL;
268c6674154SChen Wang }
269c6674154SChen Wang data->doorbell_addr = res->start;
270c6674154SChen Wang
271c6674154SChen Wang ret = fwnode_property_get_reference_args(dev_fwnode(dev), "msi-ranges",
272c6674154SChen Wang "#interrupt-cells", 0, 0, &args);
273c6674154SChen Wang if (ret) {
274c6674154SChen Wang dev_err(dev, "Unable to parse MSI vec base\n");
275c6674154SChen Wang return ret;
276c6674154SChen Wang }
277c6674154SChen Wang fwnode_handle_put(args.fwnode);
278c6674154SChen Wang
279c6674154SChen Wang ret = fwnode_property_get_reference_args(dev_fwnode(dev), "msi-ranges", NULL,
280c6674154SChen Wang args.nargs + 1, 0, &args);
281c6674154SChen Wang if (ret) {
282c6674154SChen Wang dev_err(dev, "Unable to parse MSI vec number\n");
283c6674154SChen Wang return ret;
284c6674154SChen Wang }
285c6674154SChen Wang
286c6674154SChen Wang plic_domain = irq_find_matching_fwnode(args.fwnode, DOMAIN_BUS_ANY);
287c6674154SChen Wang fwnode_handle_put(args.fwnode);
288c6674154SChen Wang if (!plic_domain) {
289c6674154SChen Wang pr_err("Failed to find the PLIC domain\n");
290c6674154SChen Wang return -ENXIO;
291c6674154SChen Wang }
292c6674154SChen Wang
293c6674154SChen Wang data->irq_first = (u32)args.args[0];
294c6674154SChen Wang data->num_irqs = (u32)args.args[args.nargs - 1];
295c6674154SChen Wang
296c6674154SChen Wang mutex_init(&data->msi_map_lock);
297c6674154SChen Wang
298bad2094eSInochi Amaoto data->msi_map = devm_bitmap_zalloc(&pdev->dev, data->num_irqs, GFP_KERNEL);
299bad2094eSInochi Amaoto if (!data->msi_map) {
300bad2094eSInochi Amaoto dev_err(&pdev->dev, "Unable to allocate msi mapping\n");
301bad2094eSInochi Amaoto return -ENOMEM;
302c6674154SChen Wang }
303c6674154SChen Wang
304bced5549SInochi Amaoto return sg204x_msi_init_domains(data, plic_domain, dev);
305c6674154SChen Wang }
306c6674154SChen Wang
307bad2094eSInochi Amaoto static const struct sg204x_msi_chip_info sg2042_chip_info = {
308bad2094eSInochi Amaoto .irqchip = &sg2042_msi_middle_irq_chip,
309bad2094eSInochi Amaoto .parent_ops = &sg2042_msi_parent_ops,
310bad2094eSInochi Amaoto };
311bad2094eSInochi Amaoto
312e96b93a9SInochi Amaoto static const struct sg204x_msi_chip_info sg2044_chip_info = {
313e96b93a9SInochi Amaoto .irqchip = &sg2044_msi_middle_irq_chip,
314e96b93a9SInochi Amaoto .parent_ops = &sg2044_msi_parent_ops,
315e96b93a9SInochi Amaoto };
316e96b93a9SInochi Amaoto
317c6674154SChen Wang static const struct of_device_id sg2042_msi_of_match[] = {
318bad2094eSInochi Amaoto { .compatible = "sophgo,sg2042-msi", .data = &sg2042_chip_info },
319e96b93a9SInochi Amaoto { .compatible = "sophgo,sg2044-msi", .data = &sg2044_chip_info },
320c6674154SChen Wang { }
321c6674154SChen Wang };
322c6674154SChen Wang
323c6674154SChen Wang static struct platform_driver sg2042_msi_driver = {
324c6674154SChen Wang .driver = {
325c6674154SChen Wang .name = "sg2042-msi",
326c6674154SChen Wang .of_match_table = sg2042_msi_of_match,
327c6674154SChen Wang },
328c6674154SChen Wang .probe = sg2042_msi_probe,
329c6674154SChen Wang };
330c6674154SChen Wang builtin_platform_driver(sg2042_msi_driver);
331