blob: 2a8571f72b173b2d06ef14d8576536b8052056b9 [file] [log] [blame]
Thomas Gleixner52a65ff2018-03-14 22:15:19 +01001// SPDX-License-Identifier: GPL-2.0
Jiang Liuf3cf8bb2014-11-12 11:39:03 +01002/*
Jiang Liuf3cf8bb2014-11-12 11:39:03 +01003 * Copyright (C) 2014 Intel Corp.
4 * Author: Jiang Liu <jiang.liu@linux.intel.com>
5 *
6 * This file is licensed under GPLv2.
7 *
8 * This file contains common code to support Message Signalled Interrupt for
9 * PCI compatible and non PCI compatible devices.
10 */
Jiang Liuaeeb5962014-11-15 22:24:05 +080011#include <linux/types.h>
12#include <linux/device.h>
Jiang Liuf3cf8bb2014-11-12 11:39:03 +010013#include <linux/irq.h>
14#include <linux/irqdomain.h>
15#include <linux/msi.h>
Marc Zyngier4e201562016-11-22 09:21:16 +000016#include <linux/slab.h>
Jiang Liud9109692014-11-15 22:24:04 +080017
Thomas Gleixner07557cc2017-09-13 23:29:05 +020018#include "internals.h"
19
Thomas Gleixner28f4b042016-09-14 16:18:47 +020020/**
21 * alloc_msi_entry - Allocate an initialize msi_entry
22 * @dev: Pointer to the device for which this is allocated
23 * @nvec: The number of vectors used in this entry
24 * @affinity: Optional pointer to an affinity mask array size of @nvec
25 *
26 * If @affinity is not NULL then a an affinity array[@nvec] is allocated
27 * and the affinity masks from @affinity are copied.
28 */
29struct msi_desc *
30alloc_msi_entry(struct device *dev, int nvec, const struct cpumask *affinity)
Jiang Liuaa48b6f2015-07-09 16:00:47 +080031{
Thomas Gleixner28f4b042016-09-14 16:18:47 +020032 struct msi_desc *desc;
33
34 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
Jiang Liuaa48b6f2015-07-09 16:00:47 +080035 if (!desc)
36 return NULL;
37
38 INIT_LIST_HEAD(&desc->list);
39 desc->dev = dev;
Thomas Gleixner28f4b042016-09-14 16:18:47 +020040 desc->nvec_used = nvec;
41 if (affinity) {
42 desc->affinity = kmemdup(affinity,
43 nvec * sizeof(*desc->affinity), GFP_KERNEL);
44 if (!desc->affinity) {
45 kfree(desc);
46 return NULL;
47 }
48 }
Jiang Liuaa48b6f2015-07-09 16:00:47 +080049
50 return desc;
51}
52
53void free_msi_entry(struct msi_desc *entry)
54{
Thomas Gleixner28f4b042016-09-14 16:18:47 +020055 kfree(entry->affinity);
Jiang Liuaa48b6f2015-07-09 16:00:47 +080056 kfree(entry);
57}
58
Jiang Liu38b6a1c2014-11-12 12:11:25 +010059void __get_cached_msi_msg(struct msi_desc *entry, struct msi_msg *msg)
60{
61 *msg = entry->msg;
62}
63
64void get_cached_msi_msg(unsigned int irq, struct msi_msg *msg)
65{
66 struct msi_desc *entry = irq_get_msi_desc(irq);
67
68 __get_cached_msi_msg(entry, msg);
69}
70EXPORT_SYMBOL_GPL(get_cached_msi_msg);
71
Jiang Liuf3cf8bb2014-11-12 11:39:03 +010072#ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
Thomas Gleixner74faaf72014-12-06 21:20:20 +010073static inline void irq_chip_write_msi_msg(struct irq_data *data,
74 struct msi_msg *msg)
75{
76 data->chip->irq_write_msi_msg(data, msg);
77}
78
Jiang Liuf3cf8bb2014-11-12 11:39:03 +010079/**
80 * msi_domain_set_affinity - Generic affinity setter function for MSI domains
81 * @irq_data: The irq data associated to the interrupt
82 * @mask: The affinity mask to set
83 * @force: Flag to enforce setting (disable online checks)
84 *
85 * Intended to be used by MSI interrupt controllers which are
86 * implemented with hierarchical domains.
87 */
88int msi_domain_set_affinity(struct irq_data *irq_data,
89 const struct cpumask *mask, bool force)
90{
91 struct irq_data *parent = irq_data->parent_data;
92 struct msi_msg msg;
93 int ret;
94
95 ret = parent->chip->irq_set_affinity(parent, mask, force);
96 if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) {
97 BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg));
98 irq_chip_write_msi_msg(irq_data, &msg);
99 }
100
101 return ret;
102}
103
Thomas Gleixner72491642017-09-13 23:29:10 +0200104static int msi_domain_activate(struct irq_domain *domain,
105 struct irq_data *irq_data, bool early)
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100106{
107 struct msi_msg msg;
108
109 BUG_ON(irq_chip_compose_msi_msg(irq_data, &msg));
110 irq_chip_write_msi_msg(irq_data, &msg);
Thomas Gleixner72491642017-09-13 23:29:10 +0200111 return 0;
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100112}
113
114static void msi_domain_deactivate(struct irq_domain *domain,
115 struct irq_data *irq_data)
116{
117 struct msi_msg msg;
118
119 memset(&msg, 0, sizeof(msg));
120 irq_chip_write_msi_msg(irq_data, &msg);
121}
122
123static int msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
124 unsigned int nr_irqs, void *arg)
125{
126 struct msi_domain_info *info = domain->host_data;
127 struct msi_domain_ops *ops = info->ops;
128 irq_hw_number_t hwirq = ops->get_hwirq(info, arg);
129 int i, ret;
130
131 if (irq_find_mapping(domain, hwirq) > 0)
132 return -EEXIST;
133
Liu Jiangbf6f8692016-01-12 13:18:06 -0700134 if (domain->parent) {
135 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
136 if (ret < 0)
137 return ret;
138 }
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100139
140 for (i = 0; i < nr_irqs; i++) {
141 ret = ops->msi_init(domain, info, virq + i, hwirq + i, arg);
142 if (ret < 0) {
143 if (ops->msi_free) {
144 for (i--; i > 0; i--)
145 ops->msi_free(domain, info, virq + i);
146 }
147 irq_domain_free_irqs_top(domain, virq, nr_irqs);
148 return ret;
149 }
150 }
151
152 return 0;
153}
154
155static void msi_domain_free(struct irq_domain *domain, unsigned int virq,
156 unsigned int nr_irqs)
157{
158 struct msi_domain_info *info = domain->host_data;
159 int i;
160
161 if (info->ops->msi_free) {
162 for (i = 0; i < nr_irqs; i++)
163 info->ops->msi_free(domain, info, virq + i);
164 }
165 irq_domain_free_irqs_top(domain, virq, nr_irqs);
166}
167
Krzysztof Kozlowski01364022015-04-27 21:54:23 +0900168static const struct irq_domain_ops msi_domain_ops = {
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100169 .alloc = msi_domain_alloc,
170 .free = msi_domain_free,
171 .activate = msi_domain_activate,
172 .deactivate = msi_domain_deactivate,
173};
174
Jiang Liuaeeb5962014-11-15 22:24:05 +0800175#ifdef GENERIC_MSI_DOMAIN_OPS
176static irq_hw_number_t msi_domain_ops_get_hwirq(struct msi_domain_info *info,
177 msi_alloc_info_t *arg)
178{
179 return arg->hwirq;
180}
181
182static int msi_domain_ops_prepare(struct irq_domain *domain, struct device *dev,
183 int nvec, msi_alloc_info_t *arg)
184{
185 memset(arg, 0, sizeof(*arg));
186 return 0;
187}
188
189static void msi_domain_ops_set_desc(msi_alloc_info_t *arg,
190 struct msi_desc *desc)
191{
192 arg->desc = desc;
193}
194#else
195#define msi_domain_ops_get_hwirq NULL
196#define msi_domain_ops_prepare NULL
197#define msi_domain_ops_set_desc NULL
198#endif /* !GENERIC_MSI_DOMAIN_OPS */
199
200static int msi_domain_ops_init(struct irq_domain *domain,
201 struct msi_domain_info *info,
202 unsigned int virq, irq_hw_number_t hwirq,
203 msi_alloc_info_t *arg)
204{
205 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, info->chip,
206 info->chip_data);
207 if (info->handler && info->handler_name) {
208 __irq_set_handler(virq, info->handler, 0, info->handler_name);
209 if (info->handler_data)
210 irq_set_handler_data(virq, info->handler_data);
211 }
212 return 0;
213}
214
215static int msi_domain_ops_check(struct irq_domain *domain,
216 struct msi_domain_info *info,
217 struct device *dev)
218{
219 return 0;
220}
221
222static struct msi_domain_ops msi_domain_ops_default = {
223 .get_hwirq = msi_domain_ops_get_hwirq,
224 .msi_init = msi_domain_ops_init,
225 .msi_check = msi_domain_ops_check,
226 .msi_prepare = msi_domain_ops_prepare,
227 .set_desc = msi_domain_ops_set_desc,
228};
229
230static void msi_domain_update_dom_ops(struct msi_domain_info *info)
231{
232 struct msi_domain_ops *ops = info->ops;
233
234 if (ops == NULL) {
235 info->ops = &msi_domain_ops_default;
236 return;
237 }
238
239 if (ops->get_hwirq == NULL)
240 ops->get_hwirq = msi_domain_ops_default.get_hwirq;
241 if (ops->msi_init == NULL)
242 ops->msi_init = msi_domain_ops_default.msi_init;
243 if (ops->msi_check == NULL)
244 ops->msi_check = msi_domain_ops_default.msi_check;
245 if (ops->msi_prepare == NULL)
246 ops->msi_prepare = msi_domain_ops_default.msi_prepare;
247 if (ops->set_desc == NULL)
248 ops->set_desc = msi_domain_ops_default.set_desc;
249}
250
251static void msi_domain_update_chip_ops(struct msi_domain_info *info)
252{
253 struct irq_chip *chip = info->chip;
254
Marc Zyngier0701c532015-10-13 19:14:45 +0100255 BUG_ON(!chip || !chip->irq_mask || !chip->irq_unmask);
Jiang Liuaeeb5962014-11-15 22:24:05 +0800256 if (!chip->irq_set_affinity)
257 chip->irq_set_affinity = msi_domain_set_affinity;
258}
259
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100260/**
261 * msi_create_irq_domain - Create a MSI interrupt domain
Marc Zyngierbe5436c2015-10-13 12:51:44 +0100262 * @fwnode: Optional fwnode of the interrupt controller
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100263 * @info: MSI domain info
264 * @parent: Parent irq domain
265 */
Marc Zyngierbe5436c2015-10-13 12:51:44 +0100266struct irq_domain *msi_create_irq_domain(struct fwnode_handle *fwnode,
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100267 struct msi_domain_info *info,
268 struct irq_domain *parent)
269{
Marc Zyngiera97b8522017-05-12 12:55:37 +0100270 struct irq_domain *domain;
271
Jiang Liuaeeb5962014-11-15 22:24:05 +0800272 if (info->flags & MSI_FLAG_USE_DEF_DOM_OPS)
273 msi_domain_update_dom_ops(info);
274 if (info->flags & MSI_FLAG_USE_DEF_CHIP_OPS)
275 msi_domain_update_chip_ops(info);
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100276
Marc Zyngiera97b8522017-05-12 12:55:37 +0100277 domain = irq_domain_create_hierarchy(parent, IRQ_DOMAIN_FLAG_MSI, 0,
278 fwnode, &msi_domain_ops, info);
Thomas Gleixner01653082017-06-20 01:37:04 +0200279
280 if (domain && !domain->name && info->chip)
Marc Zyngiera97b8522017-05-12 12:55:37 +0100281 domain->name = info->chip->name;
282
283 return domain;
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100284}
285
Marc Zyngierb2eba392015-11-23 08:26:05 +0000286int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev,
287 int nvec, msi_alloc_info_t *arg)
288{
289 struct msi_domain_info *info = domain->host_data;
290 struct msi_domain_ops *ops = info->ops;
291 int ret;
292
293 ret = ops->msi_check(domain, info, dev);
294 if (ret == 0)
295 ret = ops->msi_prepare(domain, dev, nvec, arg);
296
297 return ret;
298}
299
Marc Zyngier2145ac92015-11-23 08:26:06 +0000300int msi_domain_populate_irqs(struct irq_domain *domain, struct device *dev,
301 int virq, int nvec, msi_alloc_info_t *arg)
302{
303 struct msi_domain_info *info = domain->host_data;
304 struct msi_domain_ops *ops = info->ops;
305 struct msi_desc *desc;
306 int ret = 0;
307
308 for_each_msi_entry(desc, dev) {
309 /* Don't even try the multi-MSI brain damage. */
310 if (WARN_ON(!desc->irq || desc->nvec_used != 1)) {
311 ret = -EINVAL;
312 break;
313 }
314
315 if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
316 continue;
317
318 ops->set_desc(arg, desc);
319 /* Assumes the domain mutex is held! */
John Keeping596a7a12017-09-06 10:35:40 +0100320 ret = irq_domain_alloc_irqs_hierarchy(domain, desc->irq, 1,
321 arg);
Marc Zyngier2145ac92015-11-23 08:26:06 +0000322 if (ret)
323 break;
324
John Keeping596a7a12017-09-06 10:35:40 +0100325 irq_set_msi_desc_off(desc->irq, 0, desc);
Marc Zyngier2145ac92015-11-23 08:26:06 +0000326 }
327
328 if (ret) {
329 /* Mop up the damage */
330 for_each_msi_entry(desc, dev) {
331 if (!(desc->irq >= virq && desc->irq < (virq + nvec)))
332 continue;
333
334 irq_domain_free_irqs_common(domain, desc->irq, 1);
335 }
336 }
337
338 return ret;
339}
340
Thomas Gleixnerbc976232017-12-29 10:47:22 +0100341/*
342 * Carefully check whether the device can use reservation mode. If
343 * reservation mode is enabled then the early activation will assign a
344 * dummy vector to the device. If the PCI/MSI device does not support
345 * masking of the entry then this can result in spurious interrupts when
346 * the device driver is not absolutely careful. But even then a malfunction
347 * of the hardware could result in a spurious interrupt on the dummy vector
348 * and render the device unusable. If the entry can be masked then the core
349 * logic will prevent the spurious interrupt and reservation mode can be
350 * used. For now reservation mode is restricted to PCI/MSI.
351 */
352static bool msi_check_reservation_mode(struct irq_domain *domain,
353 struct msi_domain_info *info,
354 struct device *dev)
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100355{
Thomas Gleixnerbc976232017-12-29 10:47:22 +0100356 struct msi_desc *desc;
357
358 if (domain->bus_token != DOMAIN_BUS_PCI_MSI)
359 return false;
360
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100361 if (!(info->flags & MSI_FLAG_MUST_REACTIVATE))
362 return false;
Thomas Gleixnerbc976232017-12-29 10:47:22 +0100363
364 if (IS_ENABLED(CONFIG_PCI_MSI) && pci_msi_ignore_mask)
365 return false;
366
367 /*
368 * Checking the first MSI descriptor is sufficient. MSIX supports
369 * masking and MSI does so when the maskbit is set.
370 */
371 desc = first_msi_entry(dev);
372 return desc->msi_attrib.is_msix || desc->msi_attrib.maskbit;
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100373}
374
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100375/**
Jiang Liud9109692014-11-15 22:24:04 +0800376 * msi_domain_alloc_irqs - Allocate interrupts from a MSI interrupt domain
377 * @domain: The domain to allocate from
378 * @dev: Pointer to device struct of the device for which the interrupts
379 * are allocated
380 * @nvec: The number of interrupts to allocate
381 *
382 * Returns 0 on success or an error code.
383 */
384int msi_domain_alloc_irqs(struct irq_domain *domain, struct device *dev,
385 int nvec)
386{
387 struct msi_domain_info *info = domain->host_data;
388 struct msi_domain_ops *ops = info->ops;
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100389 struct irq_data *irq_data;
Jiang Liud9109692014-11-15 22:24:04 +0800390 struct msi_desc *desc;
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100391 msi_alloc_info_t arg;
Thomas Gleixnerb6140912016-07-04 17:39:22 +0900392 int i, ret, virq;
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100393 bool can_reserve;
Jiang Liud9109692014-11-15 22:24:04 +0800394
Marc Zyngierb2eba392015-11-23 08:26:05 +0000395 ret = msi_domain_prepare_irqs(domain, dev, nvec, &arg);
Jiang Liud9109692014-11-15 22:24:04 +0800396 if (ret)
397 return ret;
398
399 for_each_msi_entry(desc, dev) {
400 ops->set_desc(&arg, desc);
401
Thomas Gleixnerb6140912016-07-04 17:39:22 +0900402 virq = __irq_domain_alloc_irqs(domain, -1, desc->nvec_used,
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900403 dev_to_node(dev), &arg, false,
Thomas Gleixner0972fa52016-07-04 17:39:26 +0900404 desc->affinity);
Jiang Liud9109692014-11-15 22:24:04 +0800405 if (virq < 0) {
406 ret = -ENOSPC;
407 if (ops->handle_error)
408 ret = ops->handle_error(domain, desc, ret);
409 if (ops->msi_finish)
410 ops->msi_finish(&arg, ret);
411 return ret;
412 }
413
Thomas Gleixner07557cc2017-09-13 23:29:05 +0200414 for (i = 0; i < desc->nvec_used; i++) {
Jiang Liud9109692014-11-15 22:24:04 +0800415 irq_set_msi_desc_off(virq, i, desc);
Thomas Gleixner07557cc2017-09-13 23:29:05 +0200416 irq_debugfs_copy_devname(virq + i, dev);
417 }
Jiang Liud9109692014-11-15 22:24:04 +0800418 }
419
420 if (ops->msi_finish)
421 ops->msi_finish(&arg, 0);
422
Thomas Gleixnerbc976232017-12-29 10:47:22 +0100423 can_reserve = msi_check_reservation_mode(domain, info, dev);
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100424
Jiang Liud9109692014-11-15 22:24:04 +0800425 for_each_msi_entry(desc, dev) {
Thomas Gleixner4364e1a2016-07-04 15:32:25 +0200426 virq = desc->irq;
Jiang Liud9109692014-11-15 22:24:04 +0800427 if (desc->nvec_used == 1)
428 dev_dbg(dev, "irq %d for MSI\n", virq);
429 else
430 dev_dbg(dev, "irq [%d-%d] for MSI\n",
431 virq, virq + desc->nvec_used - 1);
Marc Zyngierf3b09462016-07-13 17:18:33 +0100432 /*
433 * This flag is set by the PCI layer as we need to activate
434 * the MSI entries before the PCI layer enables MSI in the
435 * card. Otherwise the card latches a random msi message.
436 */
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100437 if (!(info->flags & MSI_FLAG_ACTIVATE_EARLY))
438 continue;
Marc Zyngierf3b09462016-07-13 17:18:33 +0100439
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100440 irq_data = irq_domain_get_irq_data(domain, desc->irq);
Thomas Gleixnerbc976232017-12-29 10:47:22 +0100441 if (!can_reserve)
442 irqd_clr_can_reserve(irq_data);
443 ret = irq_domain_activate_irq(irq_data, can_reserve);
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100444 if (ret)
445 goto cleanup;
446 }
447
448 /*
449 * If these interrupts use reservation mode, clear the activated bit
450 * so request_irq() will assign the final vector.
451 */
452 if (can_reserve) {
453 for_each_msi_entry(desc, dev) {
Marc Zyngierf3b09462016-07-13 17:18:33 +0100454 irq_data = irq_domain_get_irq_data(domain, desc->irq);
Thomas Gleixnerda5dd9e2017-12-29 10:42:10 +0100455 irqd_clr_activated(irq_data);
Marc Zyngierf3b09462016-07-13 17:18:33 +0100456 }
Jiang Liud9109692014-11-15 22:24:04 +0800457 }
Jiang Liud9109692014-11-15 22:24:04 +0800458 return 0;
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +0200459
460cleanup:
461 for_each_msi_entry(desc, dev) {
462 struct irq_data *irqd;
463
464 if (desc->irq == virq)
465 break;
466
467 irqd = irq_domain_get_irq_data(domain, desc->irq);
468 if (irqd_is_activated(irqd))
469 irq_domain_deactivate_irq(irqd);
470 }
471 msi_domain_free_irqs(domain, dev);
472 return ret;
Jiang Liud9109692014-11-15 22:24:04 +0800473}
474
475/**
476 * msi_domain_free_irqs - Free interrupts from a MSI interrupt @domain associated tp @dev
477 * @domain: The domain to managing the interrupts
478 * @dev: Pointer to device struct of the device for which the interrupts
479 * are free
480 */
481void msi_domain_free_irqs(struct irq_domain *domain, struct device *dev)
482{
483 struct msi_desc *desc;
484
485 for_each_msi_entry(desc, dev) {
Marc Zyngierfe0c52f2015-01-26 19:10:19 +0000486 /*
487 * We might have failed to allocate an MSI early
488 * enough that there is no IRQ associated to this
489 * entry. If that's the case, don't do anything.
490 */
491 if (desc->irq) {
492 irq_domain_free_irqs(desc->irq, desc->nvec_used);
493 desc->irq = 0;
494 }
Jiang Liud9109692014-11-15 22:24:04 +0800495 }
496}
497
498/**
Jiang Liuf3cf8bb2014-11-12 11:39:03 +0100499 * msi_get_domain_info - Get the MSI interrupt domain info for @domain
500 * @domain: The interrupt domain to retrieve data from
501 *
502 * Returns the pointer to the msi_domain_info stored in
503 * @domain->host_data.
504 */
505struct msi_domain_info *msi_get_domain_info(struct irq_domain *domain)
506{
507 return (struct msi_domain_info *)domain->host_data;
508}
509
510#endif /* CONFIG_GENERIC_MSI_IRQ_DOMAIN */