1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020, Jiaxun Yang <jiaxun.yang@flygoat.com>
4 * Loongson HyperTransport Interrupt Vector support
7 #define pr_fmt(fmt) "htvec: " fmt
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/irqchip.h>
12 #include <linux/irqdomain.h>
13 #include <linux/irqchip/chained_irq.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/syscore_ops.h>
20 #include "irq-loongson.h"
23 #define HTVEC_EN_OFF 0x20
24 #define HTVEC_MAX_PARENT_IRQ 8
25 #define VEC_COUNT_PER_REG 32
26 #define VEC_REG_IDX(irq_id) ((irq_id) / VEC_COUNT_PER_REG)
27 #define VEC_REG_BIT(irq_id) ((irq_id) % VEC_COUNT_PER_REG)
32 struct irq_domain
*htvec_domain
;
33 raw_spinlock_t htvec_lock
;
34 u32 saved_vec_en
[HTVEC_MAX_PARENT_IRQ
];
37 static struct htvec
*htvec_priv
;
39 static void htvec_irq_dispatch(struct irq_desc
*desc
)
44 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
45 struct htvec
*priv
= irq_desc_get_handler_data(desc
);
47 chained_irq_enter(chip
, desc
);
49 for (i
= 0; i
< priv
->num_parents
; i
++) {
50 pending
= readl(priv
->base
+ 4 * i
);
52 int bit
= __ffs(pending
);
54 generic_handle_domain_irq(priv
->htvec_domain
,
55 bit
+ VEC_COUNT_PER_REG
* i
);
64 chained_irq_exit(chip
, desc
);
67 static void htvec_ack_irq(struct irq_data
*d
)
69 struct htvec
*priv
= irq_data_get_irq_chip_data(d
);
71 writel(BIT(VEC_REG_BIT(d
->hwirq
)),
72 priv
->base
+ VEC_REG_IDX(d
->hwirq
) * 4);
75 static void htvec_mask_irq(struct irq_data
*d
)
79 struct htvec
*priv
= irq_data_get_irq_chip_data(d
);
81 raw_spin_lock(&priv
->htvec_lock
);
82 addr
= priv
->base
+ HTVEC_EN_OFF
;
83 addr
+= VEC_REG_IDX(d
->hwirq
) * 4;
85 reg
&= ~BIT(VEC_REG_BIT(d
->hwirq
));
87 raw_spin_unlock(&priv
->htvec_lock
);
90 static void htvec_unmask_irq(struct irq_data
*d
)
94 struct htvec
*priv
= irq_data_get_irq_chip_data(d
);
96 raw_spin_lock(&priv
->htvec_lock
);
97 addr
= priv
->base
+ HTVEC_EN_OFF
;
98 addr
+= VEC_REG_IDX(d
->hwirq
) * 4;
100 reg
|= BIT(VEC_REG_BIT(d
->hwirq
));
102 raw_spin_unlock(&priv
->htvec_lock
);
105 static struct irq_chip htvec_irq_chip
= {
106 .name
= "LOONGSON_HTVEC",
107 .irq_mask
= htvec_mask_irq
,
108 .irq_unmask
= htvec_unmask_irq
,
109 .irq_ack
= htvec_ack_irq
,
112 static int htvec_domain_alloc(struct irq_domain
*domain
, unsigned int virq
,
113 unsigned int nr_irqs
, void *arg
)
117 unsigned int type
, i
;
118 struct htvec
*priv
= domain
->host_data
;
120 ret
= irq_domain_translate_onecell(domain
, arg
, &hwirq
, &type
);
124 for (i
= 0; i
< nr_irqs
; i
++) {
125 irq_domain_set_info(domain
, virq
+ i
, hwirq
+ i
, &htvec_irq_chip
,
126 priv
, handle_edge_irq
, NULL
, NULL
);
132 static void htvec_domain_free(struct irq_domain
*domain
, unsigned int virq
,
133 unsigned int nr_irqs
)
137 for (i
= 0; i
< nr_irqs
; i
++) {
138 struct irq_data
*d
= irq_domain_get_irq_data(domain
, virq
+ i
);
140 irq_set_handler(virq
+ i
, NULL
);
141 irq_domain_reset_irq_data(d
);
145 static const struct irq_domain_ops htvec_domain_ops
= {
146 .translate
= irq_domain_translate_onecell
,
147 .alloc
= htvec_domain_alloc
,
148 .free
= htvec_domain_free
,
151 static void htvec_reset(struct htvec
*priv
)
155 /* Clear IRQ cause registers, mask all interrupts */
156 for (idx
= 0; idx
< priv
->num_parents
; idx
++) {
157 writel_relaxed(0x0, priv
->base
+ HTVEC_EN_OFF
+ 4 * idx
);
158 writel_relaxed(0xFFFFFFFF, priv
->base
+ 4 * idx
);
162 static int htvec_suspend(void)
166 for (i
= 0; i
< htvec_priv
->num_parents
; i
++)
167 htvec_priv
->saved_vec_en
[i
] = readl(htvec_priv
->base
+ HTVEC_EN_OFF
+ 4 * i
);
172 static void htvec_resume(void)
176 for (i
= 0; i
< htvec_priv
->num_parents
; i
++)
177 writel(htvec_priv
->saved_vec_en
[i
], htvec_priv
->base
+ HTVEC_EN_OFF
+ 4 * i
);
180 static struct syscore_ops htvec_syscore_ops
= {
181 .suspend
= htvec_suspend
,
182 .resume
= htvec_resume
,
185 static int htvec_init(phys_addr_t addr
, unsigned long size
,
186 int num_parents
, int parent_irq
[], struct fwnode_handle
*domain_handle
)
191 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
195 priv
->num_parents
= num_parents
;
196 priv
->base
= ioremap(addr
, size
);
197 raw_spin_lock_init(&priv
->htvec_lock
);
199 /* Setup IRQ domain */
200 priv
->htvec_domain
= irq_domain_create_linear(domain_handle
,
201 (VEC_COUNT_PER_REG
* priv
->num_parents
),
202 &htvec_domain_ops
, priv
);
203 if (!priv
->htvec_domain
) {
204 pr_err("loongson-htvec: cannot add IRQ domain\n");
210 for (i
= 0; i
< priv
->num_parents
; i
++) {
211 irq_set_chained_handler_and_data(parent_irq
[i
],
212 htvec_irq_dispatch
, priv
);
217 register_syscore_ops(&htvec_syscore_ops
);
230 static int htvec_of_init(struct device_node
*node
,
231 struct device_node
*parent
)
238 if (of_address_to_resource(node
, 0, &res
))
241 /* Interrupt may come from any of the 8 interrupt lines */
242 for (i
= 0; i
< HTVEC_MAX_PARENT_IRQ
; i
++) {
243 parent_irq
[i
] = irq_of_parse_and_map(node
, i
);
244 if (parent_irq
[i
] <= 0)
250 err
= htvec_init(res
.start
, resource_size(&res
),
251 num_parents
, parent_irq
, of_node_to_fwnode(node
));
258 IRQCHIP_DECLARE(htvec
, "loongson,htvec-1.0", htvec_of_init
);
263 static int __init
pch_pic_parse_madt(union acpi_subtable_headers
*header
,
264 const unsigned long end
)
266 struct acpi_madt_bio_pic
*pchpic_entry
= (struct acpi_madt_bio_pic
*)header
;
268 return pch_pic_acpi_init(htvec_priv
->htvec_domain
, pchpic_entry
);
271 static int __init
pch_msi_parse_madt(union acpi_subtable_headers
*header
,
272 const unsigned long end
)
274 struct acpi_madt_msi_pic
*pchmsi_entry
= (struct acpi_madt_msi_pic
*)header
;
276 return pch_msi_acpi_init(htvec_priv
->htvec_domain
, pchmsi_entry
);
279 static int __init
acpi_cascade_irqdomain_init(void)
283 r
= acpi_table_parse_madt(ACPI_MADT_TYPE_BIO_PIC
, pch_pic_parse_madt
, 0);
287 r
= acpi_table_parse_madt(ACPI_MADT_TYPE_MSI_PIC
, pch_msi_parse_madt
, 0);
294 int __init
htvec_acpi_init(struct irq_domain
*parent
,
295 struct acpi_madt_ht_pic
*acpi_htvec
)
298 int num_parents
, parent_irq
[8];
299 struct fwnode_handle
*domain_handle
;
304 num_parents
= HTVEC_MAX_PARENT_IRQ
;
306 domain_handle
= irq_domain_alloc_fwnode(&acpi_htvec
->address
);
307 if (!domain_handle
) {
308 pr_err("Unable to allocate domain handle\n");
312 /* Interrupt may come from any of the 8 interrupt lines */
313 for (i
= 0; i
< HTVEC_MAX_PARENT_IRQ
; i
++)
314 parent_irq
[i
] = irq_create_mapping(parent
, acpi_htvec
->cascade
[i
]);
316 ret
= htvec_init(acpi_htvec
->address
, acpi_htvec
->size
,
317 num_parents
, parent_irq
, domain_handle
);
320 ret
= acpi_cascade_irqdomain_init();
322 irq_domain_free_fwnode(domain_handle
);