GNU Linux-libre 4.14.290-gnu1
[releases.git] / arch / x86 / kernel / apic / htirq.c
1 /*
2  * Support Hypertransport IRQ
3  *
4  * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
5  *      Moved from arch/x86/kernel/apic/io_apic.c.
6  * Jiang Liu <jiang.liu@linux.intel.com>
7  *      Add support of hierarchical irqdomain
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/mm.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/device.h>
17 #include <linux/pci.h>
18 #include <linux/htirq.h>
19 #include <linux/irq.h>
20
21 #include <asm/irqdomain.h>
22 #include <asm/hw_irq.h>
23 #include <asm/apic.h>
24 #include <asm/hypertransport.h>
25
26 static struct irq_domain *htirq_domain;
27
28 /*
29  * Hypertransport interrupt support
30  */
31 static int
32 ht_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
33 {
34         struct irq_data *parent = data->parent_data;
35         int ret;
36
37         ret = parent->chip->irq_set_affinity(parent, mask, force);
38         if (ret >= 0) {
39                 struct ht_irq_msg msg;
40                 struct irq_cfg *cfg = irqd_cfg(data);
41
42                 fetch_ht_irq_msg(data->irq, &msg);
43                 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK |
44                                     HT_IRQ_LOW_DEST_ID_MASK);
45                 msg.address_lo |= HT_IRQ_LOW_VECTOR(cfg->vector) |
46                                   HT_IRQ_LOW_DEST_ID(cfg->dest_apicid);
47                 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
48                 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(cfg->dest_apicid);
49                 write_ht_irq_msg(data->irq, &msg);
50         }
51
52         return ret;
53 }
54
55 static struct irq_chip ht_irq_chip = {
56         .name                   = "PCI-HT",
57         .irq_mask               = mask_ht_irq,
58         .irq_unmask             = unmask_ht_irq,
59         .irq_ack                = irq_chip_ack_parent,
60         .irq_set_affinity       = ht_set_affinity,
61         .irq_retrigger          = irq_chip_retrigger_hierarchy,
62         .flags                  = IRQCHIP_SKIP_SET_WAKE,
63 };
64
65 static int htirq_domain_alloc(struct irq_domain *domain, unsigned int virq,
66                               unsigned int nr_irqs, void *arg)
67 {
68         struct ht_irq_cfg *ht_cfg;
69         struct irq_alloc_info *info = arg;
70         struct pci_dev *dev;
71         irq_hw_number_t hwirq;
72         int ret;
73
74         if (nr_irqs > 1 || !info)
75                 return -EINVAL;
76
77         dev = info->ht_dev;
78         hwirq = (info->ht_idx & 0xFF) |
79                 PCI_DEVID(dev->bus->number, dev->devfn) << 8 |
80                 (pci_domain_nr(dev->bus) & 0xFFFFFFFF) << 24;
81         if (irq_find_mapping(domain, hwirq) > 0)
82                 return -EEXIST;
83
84         ht_cfg = kmalloc(sizeof(*ht_cfg), GFP_KERNEL);
85         if (!ht_cfg)
86                 return -ENOMEM;
87
88         ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, info);
89         if (ret < 0) {
90                 kfree(ht_cfg);
91                 return ret;
92         }
93
94         /* Initialize msg to a value that will never match the first write. */
95         ht_cfg->msg.address_lo = 0xffffffff;
96         ht_cfg->msg.address_hi = 0xffffffff;
97         ht_cfg->dev = info->ht_dev;
98         ht_cfg->update = info->ht_update;
99         ht_cfg->pos = info->ht_pos;
100         ht_cfg->idx = 0x10 + (info->ht_idx * 2);
101         irq_domain_set_info(domain, virq, hwirq, &ht_irq_chip, ht_cfg,
102                             handle_edge_irq, ht_cfg, "edge");
103
104         return 0;
105 }
106
107 static void htirq_domain_free(struct irq_domain *domain, unsigned int virq,
108                               unsigned int nr_irqs)
109 {
110         struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
111
112         BUG_ON(nr_irqs != 1);
113         kfree(irq_data->chip_data);
114         irq_domain_free_irqs_top(domain, virq, nr_irqs);
115 }
116
117 static void htirq_domain_activate(struct irq_domain *domain,
118                                   struct irq_data *irq_data)
119 {
120         struct ht_irq_msg msg;
121         struct irq_cfg *cfg = irqd_cfg(irq_data);
122
123         msg.address_hi = HT_IRQ_HIGH_DEST_ID(cfg->dest_apicid);
124         msg.address_lo =
125                 HT_IRQ_LOW_BASE |
126                 HT_IRQ_LOW_DEST_ID(cfg->dest_apicid) |
127                 HT_IRQ_LOW_VECTOR(cfg->vector) |
128                 ((apic->irq_dest_mode == 0) ?
129                         HT_IRQ_LOW_DM_PHYSICAL :
130                         HT_IRQ_LOW_DM_LOGICAL) |
131                 HT_IRQ_LOW_RQEOI_EDGE |
132                 ((apic->irq_delivery_mode != dest_LowestPrio) ?
133                         HT_IRQ_LOW_MT_FIXED :
134                         HT_IRQ_LOW_MT_ARBITRATED) |
135                 HT_IRQ_LOW_IRQ_MASKED;
136         write_ht_irq_msg(irq_data->irq, &msg);
137 }
138
139 static void htirq_domain_deactivate(struct irq_domain *domain,
140                                     struct irq_data *irq_data)
141 {
142         struct ht_irq_msg msg;
143
144         memset(&msg, 0, sizeof(msg));
145         write_ht_irq_msg(irq_data->irq, &msg);
146 }
147
148 static const struct irq_domain_ops htirq_domain_ops = {
149         .alloc          = htirq_domain_alloc,
150         .free           = htirq_domain_free,
151         .activate       = htirq_domain_activate,
152         .deactivate     = htirq_domain_deactivate,
153 };
154
155 void __init arch_init_htirq_domain(struct irq_domain *parent)
156 {
157         struct fwnode_handle *fn;
158
159         if (disable_apic)
160                 return;
161
162         fn = irq_domain_alloc_named_fwnode("PCI-HT");
163         if (!fn)
164                 goto warn;
165
166         htirq_domain = irq_domain_create_tree(fn, &htirq_domain_ops, NULL);
167         irq_domain_free_fwnode(fn);
168         if (!htirq_domain)
169                 goto warn;
170
171         htirq_domain->parent = parent;
172         return;
173
174 warn:
175         pr_warn("Failed to initialize irqdomain for HTIRQ.\n");
176 }
177
178 int arch_setup_ht_irq(int idx, int pos, struct pci_dev *dev,
179                       ht_irq_update_t *update)
180 {
181         struct irq_alloc_info info;
182
183         if (!htirq_domain)
184                 return -ENOSYS;
185
186         init_irq_alloc_info(&info, NULL);
187         info.ht_idx = idx;
188         info.ht_pos = pos;
189         info.ht_dev = dev;
190         info.ht_update = update;
191
192         return irq_domain_alloc_irqs(htirq_domain, 1, dev_to_node(&dev->dev),
193                                      &info);
194 }
195
196 void arch_teardown_ht_irq(unsigned int irq)
197 {
198         irq_domain_free_irqs(irq, 1);
199 }