Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2007, Olof Johansson, PA Semi |
| 4 | * |
| 5 | * Based on arch/powerpc/sysdev/mpic_u3msi.c: |
| 6 | * |
| 7 | * Copyright 2006, Segher Boessenkool, IBM Corporation. |
| 8 | * Copyright 2006-2007, Michael Ellerman, IBM Corporation. |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 9 | */ |
| 10 | |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 11 | #include <linux/irq.h> |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 12 | #include <linux/msi.h> |
| 13 | #include <asm/mpic.h> |
| 14 | #include <asm/prom.h> |
| 15 | #include <asm/hw_irq.h> |
| 16 | #include <asm/ppc-pci.h> |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 17 | #include <asm/msi_bitmap.h> |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 18 | |
Michael Ellerman | 5af7a6f | 2015-04-10 11:52:06 +1000 | [diff] [blame] | 19 | #include <sysdev/mpic.h> |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 20 | |
| 21 | /* Allocate 16 interrupts per device, to give an alignment of 16, |
| 22 | * since that's the size of the grouping w.r.t. affinity. If someone |
| 23 | * needs more than 32 MSI's down the road we'll have to rethink this, |
| 24 | * but it should be OK for now. |
| 25 | */ |
| 26 | #define ALLOC_CHUNK 16 |
| 27 | |
| 28 | #define PASEMI_MSI_ADDR 0xfc080000 |
| 29 | |
| 30 | /* A bit ugly, can we get this from the pci_dev somehow? */ |
| 31 | static struct mpic *msi_mpic; |
| 32 | |
| 33 | |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 34 | static void mpic_pasemi_msi_mask_irq(struct irq_data *data) |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 35 | { |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 36 | pr_debug("mpic_pasemi_msi_mask_irq %d\n", data->irq); |
Thomas Gleixner | 280510f | 2014-11-23 12:23:20 +0100 | [diff] [blame] | 37 | pci_msi_mask_irq(data); |
Lennert Buytenhek | 835c0553 | 2011-03-08 22:26:43 +0000 | [diff] [blame] | 38 | mpic_mask_irq(data); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 39 | } |
| 40 | |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 41 | static void mpic_pasemi_msi_unmask_irq(struct irq_data *data) |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 42 | { |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 43 | pr_debug("mpic_pasemi_msi_unmask_irq %d\n", data->irq); |
Lennert Buytenhek | 835c0553 | 2011-03-08 22:26:43 +0000 | [diff] [blame] | 44 | mpic_unmask_irq(data); |
Thomas Gleixner | 280510f | 2014-11-23 12:23:20 +0100 | [diff] [blame] | 45 | pci_msi_unmask_irq(data); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static struct irq_chip mpic_pasemi_msi_chip = { |
Lennert Buytenhek | 835c0553 | 2011-03-08 22:26:43 +0000 | [diff] [blame] | 49 | .irq_shutdown = mpic_pasemi_msi_mask_irq, |
| 50 | .irq_mask = mpic_pasemi_msi_mask_irq, |
| 51 | .irq_unmask = mpic_pasemi_msi_unmask_irq, |
| 52 | .irq_eoi = mpic_end_irq, |
| 53 | .irq_set_type = mpic_set_irq_type, |
| 54 | .irq_set_affinity = mpic_set_affinity, |
| 55 | .name = "PASEMI-MSI", |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 56 | }; |
| 57 | |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 58 | static void pasemi_msi_teardown_msi_irqs(struct pci_dev *pdev) |
| 59 | { |
| 60 | struct msi_desc *entry; |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 61 | irq_hw_number_t hwirq; |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 62 | |
| 63 | pr_debug("pasemi_msi_teardown_msi_irqs, pdev %p\n", pdev); |
| 64 | |
Thomas Gleixner | e22b0d1 | 2021-12-06 23:51:28 +0100 | [diff] [blame] | 65 | msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 66 | hwirq = virq_to_hw(entry->irq); |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 67 | irq_set_msi_desc(entry->irq, NULL); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 68 | irq_dispose_mapping(entry->irq); |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 69 | msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, ALLOC_CHUNK); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 70 | } |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static int pasemi_msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) |
| 74 | { |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 75 | unsigned int virq; |
| 76 | struct msi_desc *entry; |
| 77 | struct msi_msg msg; |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 78 | int hwirq; |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 79 | |
Alexander Gordeev | 6b2fd7ef | 2014-09-07 20:57:53 +0200 | [diff] [blame] | 80 | if (type == PCI_CAP_ID_MSIX) |
| 81 | pr_debug("pasemi_msi: MSI-X untested, trying anyway\n"); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 82 | pr_debug("pasemi_msi_setup_msi_irqs, pdev %p nvec %d type %d\n", |
| 83 | pdev, nvec, type); |
| 84 | |
| 85 | msg.address_hi = 0; |
| 86 | msg.address_lo = PASEMI_MSI_ADDR; |
| 87 | |
Thomas Gleixner | e22b0d1 | 2021-12-06 23:51:28 +0100 | [diff] [blame] | 88 | msi_for_each_desc(entry, &pdev->dev, MSI_DESC_NOTASSOCIATED) { |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 89 | /* Allocate 16 interrupts for now, since that's the grouping for |
| 90 | * affinity. This can be changed later if it turns out 32 is too |
| 91 | * few MSIs for someone, but restrictions will apply to how the |
| 92 | * sources can be changed independently. |
| 93 | */ |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 94 | hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, |
| 95 | ALLOC_CHUNK); |
| 96 | if (hwirq < 0) { |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 97 | pr_debug("pasemi_msi: failed allocating hwirq\n"); |
| 98 | return hwirq; |
| 99 | } |
| 100 | |
| 101 | virq = irq_create_mapping(msi_mpic->irqhost, hwirq); |
Michael Ellerman | ef24ba7 | 2016-09-06 21:53:24 +1000 | [diff] [blame] | 102 | if (!virq) { |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 103 | pr_debug("pasemi_msi: failed mapping hwirq 0x%x\n", |
| 104 | hwirq); |
| 105 | msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, |
| 106 | ALLOC_CHUNK); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 107 | return -ENOSPC; |
| 108 | } |
| 109 | |
| 110 | /* Vector on MSI is really an offset, the hardware adds |
| 111 | * it to the value written at the magic address. So set |
| 112 | * it to 0 to remain sane. |
| 113 | */ |
| 114 | mpic_set_vector(virq, 0); |
| 115 | |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 116 | irq_set_msi_desc(virq, entry); |
| 117 | irq_set_chip(virq, &mpic_pasemi_msi_chip); |
| 118 | irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 119 | |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 120 | pr_debug("pasemi_msi: allocated virq 0x%x (hw 0x%x) " \ |
| 121 | "addr 0x%x\n", virq, hwirq, msg.address_lo); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 122 | |
| 123 | /* Likewise, the device writes [0...511] into the target |
| 124 | * register to generate MSI [512...1023] |
| 125 | */ |
| 126 | msg.data = hwirq-0x200; |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 127 | pci_write_msi_msg(virq, &msg); |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Nick Child | e37e06a | 2021-12-16 17:00:24 -0500 | [diff] [blame] | 133 | int __init mpic_pasemi_msi_init(struct mpic *mpic) |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 134 | { |
| 135 | int rc; |
Daniel Axtens | 8392296 | 2015-04-14 14:28:01 +1000 | [diff] [blame] | 136 | struct pci_controller *phb; |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 137 | struct device_node *of_node; |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 138 | |
Marc Zyngier | 5d4c9bc | 2015-10-13 12:51:29 +0100 | [diff] [blame] | 139 | of_node = irq_domain_get_of_node(mpic->irqhost); |
| 140 | if (!of_node || |
| 141 | !of_device_is_compatible(of_node, |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 142 | "pasemi,pwrficient-openpic")) |
| 143 | return -ENODEV; |
| 144 | |
| 145 | rc = mpic_msi_init_allocator(mpic); |
| 146 | if (rc) { |
| 147 | pr_debug("pasemi_msi: Error allocating bitmap!\n"); |
| 148 | return rc; |
| 149 | } |
| 150 | |
| 151 | pr_debug("pasemi_msi: Registering PA Semi MPIC MSI callbacks\n"); |
| 152 | |
| 153 | msi_mpic = mpic; |
Daniel Axtens | 8392296 | 2015-04-14 14:28:01 +1000 | [diff] [blame] | 154 | list_for_each_entry(phb, &hose_list, list_node) { |
| 155 | WARN_ON(phb->controller_ops.setup_msi_irqs); |
| 156 | phb->controller_ops.setup_msi_irqs = pasemi_msi_setup_msi_irqs; |
| 157 | phb->controller_ops.teardown_msi_irqs = pasemi_msi_teardown_msi_irqs; |
| 158 | } |
Olof Johansson | 38958dd | 2007-12-12 17:44:46 +1100 | [diff] [blame] | 159 | |
| 160 | return 0; |
| 161 | } |