irq_domain/powerpc: Eliminate virq_is_host()
[deliverable/linux.git] / kernel / irq / irqdomain.c
CommitLineData
08a543ad
GL
1#include <linux/irq.h>
2#include <linux/irqdomain.h>
3#include <linux/module.h>
4#include <linux/mutex.h>
5#include <linux/of.h>
7e713301
GL
6#include <linux/of_address.h>
7#include <linux/slab.h>
08a543ad
GL
8
9static LIST_HEAD(irq_domain_list);
10static DEFINE_MUTEX(irq_domain_mutex);
11
12/**
13 * irq_domain_add() - Register an irq_domain
14 * @domain: ptr to initialized irq_domain structure
15 *
16 * Registers an irq_domain structure. The irq_domain must at a minimum be
17 * initialized with an ops structure pointer, and either a ->to_irq hook or
18 * a valid irq_base value. Everything else is optional.
19 */
20void irq_domain_add(struct irq_domain *domain)
21{
22 struct irq_data *d;
6d274309 23 int hwirq, irq;
08a543ad
GL
24
25 /*
26 * This assumes that the irq_domain owner has already allocated
27 * the irq_descs. This block will be removed when support for dynamic
28 * allocation of irq_descs is added to irq_domain.
29 */
6d274309
RH
30 irq_domain_for_each_irq(domain, hwirq, irq) {
31 d = irq_get_irq_data(irq);
eef24afb
RH
32 if (!d) {
33 WARN(1, "error: assigning domain to non existant irq_desc");
34 return;
35 }
36 if (d->domain) {
08a543ad
GL
37 /* things are broken; just report, don't clean up */
38 WARN(1, "error: irq_desc already assigned to a domain");
39 return;
40 }
41 d->domain = domain;
42 d->hwirq = hwirq;
43 }
44
45 mutex_lock(&irq_domain_mutex);
7bb69bad 46 list_add(&domain->link, &irq_domain_list);
08a543ad
GL
47 mutex_unlock(&irq_domain_mutex);
48}
49
50/**
51 * irq_domain_del() - Unregister an irq_domain
52 * @domain: ptr to registered irq_domain.
53 */
54void irq_domain_del(struct irq_domain *domain)
55{
56 struct irq_data *d;
6d274309 57 int hwirq, irq;
08a543ad
GL
58
59 mutex_lock(&irq_domain_mutex);
7bb69bad 60 list_del(&domain->link);
08a543ad
GL
61 mutex_unlock(&irq_domain_mutex);
62
63 /* Clear the irq_domain assignments */
6d274309
RH
64 irq_domain_for_each_irq(domain, hwirq, irq) {
65 d = irq_get_irq_data(irq);
08a543ad
GL
66 d->domain = NULL;
67 }
68}
69
70#if defined(CONFIG_OF_IRQ)
71/**
72 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
73 *
74 * Used by the device tree interrupt mapping code to translate a device tree
75 * interrupt specifier to a valid linux irq number. Returns either a valid
76 * linux IRQ number or 0.
77 *
78 * When the caller no longer need the irq number returned by this function it
79 * should arrange to call irq_dispose_mapping().
80 */
81unsigned int irq_create_of_mapping(struct device_node *controller,
82 const u32 *intspec, unsigned int intsize)
83{
84 struct irq_domain *domain;
85 unsigned long hwirq;
86 unsigned int irq, type;
87 int rc = -EINVAL;
88
89 /* Find a domain which can translate the irq spec */
90 mutex_lock(&irq_domain_mutex);
7bb69bad
GL
91 list_for_each_entry(domain, &irq_domain_list, link) {
92 if (!domain->ops->xlate)
08a543ad 93 continue;
7bb69bad 94 rc = domain->ops->xlate(domain, controller,
08a543ad
GL
95 intspec, intsize, &hwirq, &type);
96 if (rc == 0)
97 break;
98 }
99 mutex_unlock(&irq_domain_mutex);
100
101 if (rc != 0)
102 return 0;
103
104 irq = irq_domain_to_irq(domain, hwirq);
105 if (type != IRQ_TYPE_NONE)
106 irq_set_irq_type(irq, type);
107 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
108 controller->full_name, (int)hwirq, irq, type);
109 return irq;
110}
111EXPORT_SYMBOL_GPL(irq_create_of_mapping);
112
113/**
114 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
115 * @irq: linux irq number to be discarded
116 *
117 * Calling this function indicates the caller no longer needs a reference to
118 * the linux irq number returned by a prior call to irq_create_of_mapping().
119 */
120void irq_dispose_mapping(unsigned int irq)
121{
122 /*
123 * nothing yet; will be filled when support for dynamic allocation of
124 * irq_descs is added to irq_domain
125 */
126}
127EXPORT_SYMBOL_GPL(irq_dispose_mapping);
7e713301 128
7bb69bad 129int irq_domain_simple_xlate(struct irq_domain *d,
7e713301
GL
130 struct device_node *controller,
131 const u32 *intspec, unsigned int intsize,
132 unsigned long *out_hwirq, unsigned int *out_type)
133{
134 if (d->of_node != controller)
135 return -EINVAL;
136 if (intsize < 1)
137 return -EINVAL;
93797d87
RH
138 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
139 (intspec[0] >= d->hwirq_base + d->nr_irq)))
140 return -EINVAL;
7e713301
GL
141
142 *out_hwirq = intspec[0];
143 *out_type = IRQ_TYPE_NONE;
144 if (intsize > 1)
145 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
146 return 0;
147}
148
7e713301
GL
149/**
150 * irq_domain_create_simple() - Set up a 'simple' translation range
151 */
152void irq_domain_add_simple(struct device_node *controller, int irq_base)
153{
154 struct irq_domain *domain;
155
156 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
157 if (!domain) {
158 WARN_ON(1);
159 return;
160 }
161
162 domain->irq_base = irq_base;
163 domain->of_node = of_node_get(controller);
164 domain->ops = &irq_domain_simple_ops;
165 irq_domain_add(domain);
166}
167EXPORT_SYMBOL_GPL(irq_domain_add_simple);
168
169void irq_domain_generate_simple(const struct of_device_id *match,
170 u64 phys_base, unsigned int irq_start)
171{
172 struct device_node *node;
e1964c50 173 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
7e713301
GL
174 (unsigned long long) phys_base, (int) irq_start);
175 node = of_find_matching_node_by_address(NULL, match, phys_base);
176 if (node)
177 irq_domain_add_simple(node, irq_start);
7e713301
GL
178}
179EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
08a543ad 180#endif /* CONFIG_OF_IRQ */
c87fb573
JI
181
182struct irq_domain_ops irq_domain_simple_ops = {
183#ifdef CONFIG_OF_IRQ
7bb69bad 184 .xlate = irq_domain_simple_xlate,
c87fb573
JI
185#endif /* CONFIG_OF_IRQ */
186};
187EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
This page took 0.081627 seconds and 5 git commands to generate.