Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2006, Segher Boessenkool, IBM Corporation. |
| 4 | * Copyright 2006-2007, Michael Ellerman, IBM Corporation. |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/irq.h> |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 8 | #include <linux/msi.h> |
| 9 | #include <asm/mpic.h> |
| 10 | #include <asm/prom.h> |
| 11 | #include <asm/hw_irq.h> |
| 12 | #include <asm/ppc-pci.h> |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 13 | #include <asm/msi_bitmap.h> |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 14 | |
| 15 | #include "mpic.h" |
| 16 | |
| 17 | /* A bit ugly, can we get this from the pci_dev somehow? */ |
| 18 | static struct mpic *msi_mpic; |
| 19 | |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 20 | static void mpic_u3msi_mask_irq(struct irq_data *data) |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 21 | { |
Thomas Gleixner | 280510f | 2014-11-23 12:23:20 +0100 | [diff] [blame] | 22 | pci_msi_mask_irq(data); |
Lennert Buytenhek | 835c0553 | 2011-03-08 22:26:43 +0000 | [diff] [blame] | 23 | mpic_mask_irq(data); |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 24 | } |
| 25 | |
Thomas Gleixner | 1c9db52 | 2010-09-28 16:46:51 +0200 | [diff] [blame] | 26 | static void mpic_u3msi_unmask_irq(struct irq_data *data) |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 27 | { |
Lennert Buytenhek | 835c0553 | 2011-03-08 22:26:43 +0000 | [diff] [blame] | 28 | mpic_unmask_irq(data); |
Thomas Gleixner | 280510f | 2014-11-23 12:23:20 +0100 | [diff] [blame] | 29 | pci_msi_unmask_irq(data); |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static struct irq_chip mpic_u3msi_chip = { |
Lennert Buytenhek | 835c0553 | 2011-03-08 22:26:43 +0000 | [diff] [blame] | 33 | .irq_shutdown = mpic_u3msi_mask_irq, |
| 34 | .irq_mask = mpic_u3msi_mask_irq, |
| 35 | .irq_unmask = mpic_u3msi_unmask_irq, |
| 36 | .irq_eoi = mpic_end_irq, |
| 37 | .irq_set_type = mpic_set_irq_type, |
| 38 | .irq_set_affinity = mpic_set_affinity, |
| 39 | .name = "MPIC-U3MSI", |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static u64 read_ht_magic_addr(struct pci_dev *pdev, unsigned int pos) |
| 43 | { |
| 44 | u8 flags; |
| 45 | u32 tmp; |
| 46 | u64 addr; |
| 47 | |
| 48 | pci_read_config_byte(pdev, pos + HT_MSI_FLAGS, &flags); |
| 49 | |
| 50 | if (flags & HT_MSI_FLAGS_FIXED) |
| 51 | return HT_MSI_FIXED_ADDR; |
| 52 | |
| 53 | pci_read_config_dword(pdev, pos + HT_MSI_ADDR_LO, &tmp); |
| 54 | addr = tmp & HT_MSI_ADDR_LO_MASK; |
| 55 | pci_read_config_dword(pdev, pos + HT_MSI_ADDR_HI, &tmp); |
| 56 | addr = addr | ((u64)tmp << 32); |
| 57 | |
| 58 | return addr; |
| 59 | } |
| 60 | |
Benjamin Herrenschmidt | 7a96c6b | 2009-12-14 15:31:13 +0000 | [diff] [blame] | 61 | static u64 find_ht_magic_addr(struct pci_dev *pdev, unsigned int hwirq) |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 62 | { |
| 63 | struct pci_bus *bus; |
| 64 | unsigned int pos; |
| 65 | |
Benjamin Herrenschmidt | 7a96c6b | 2009-12-14 15:31:13 +0000 | [diff] [blame] | 66 | for (bus = pdev->bus; bus && bus->self; bus = bus->parent) { |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 67 | pos = pci_find_ht_capability(bus->self, HT_CAPTYPE_MSI_MAPPING); |
| 68 | if (pos) |
| 69 | return read_ht_magic_addr(bus->self, pos); |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Benjamin Herrenschmidt | 7a96c6b | 2009-12-14 15:31:13 +0000 | [diff] [blame] | 75 | static u64 find_u4_magic_addr(struct pci_dev *pdev, unsigned int hwirq) |
| 76 | { |
| 77 | struct pci_controller *hose = pci_bus_to_host(pdev->bus); |
| 78 | |
| 79 | /* U4 PCIe MSIs need to write to the special register in |
| 80 | * the bridge that generates interrupts. There should be |
| 81 | * theorically a register at 0xf8005000 where you just write |
| 82 | * the MSI number and that triggers the right interrupt, but |
| 83 | * unfortunately, this is busted in HW, the bridge endian swaps |
| 84 | * the value and hits the wrong nibble in the register. |
| 85 | * |
| 86 | * So instead we use another register set which is used normally |
| 87 | * for converting HT interrupts to MPIC interrupts, which decodes |
| 88 | * the interrupt number as part of the low address bits |
| 89 | * |
| 90 | * This will not work if we ever use more than one legacy MSI in |
| 91 | * a block but we never do. For one MSI or multiple MSI-X where |
| 92 | * each interrupt address can be specified separately, it works |
| 93 | * just fine. |
| 94 | */ |
| 95 | if (of_device_is_compatible(hose->dn, "u4-pcie") || |
| 96 | of_device_is_compatible(hose->dn, "U4-pcie")) |
| 97 | return 0xf8004000 | (hwirq << 4); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 102 | static void u3msi_teardown_msi_irqs(struct pci_dev *pdev) |
| 103 | { |
| 104 | struct msi_desc *entry; |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 105 | irq_hw_number_t hwirq; |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 106 | |
Thomas Gleixner | 706b585 | 2021-12-06 23:51:31 +0100 | [diff] [blame] | 107 | msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 108 | hwirq = virq_to_hw(entry->irq); |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 109 | irq_set_msi_desc(entry->irq, NULL); |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 110 | irq_dispose_mapping(entry->irq); |
Paul Mackerras | e297c93 | 2015-09-10 14:36:21 +1000 | [diff] [blame] | 111 | msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1); |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 112 | } |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 113 | } |
| 114 | |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 115 | static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) |
| 116 | { |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 117 | unsigned int virq; |
| 118 | struct msi_desc *entry; |
| 119 | struct msi_msg msg; |
Michael Ellerman | 21ccdd3 | 2007-09-20 16:36:51 +1000 | [diff] [blame] | 120 | u64 addr; |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 121 | int hwirq; |
Michael Ellerman | 21ccdd3 | 2007-09-20 16:36:51 +1000 | [diff] [blame] | 122 | |
Alexander Gordeev | 6b2fd7ef | 2014-09-07 20:57:53 +0200 | [diff] [blame] | 123 | if (type == PCI_CAP_ID_MSIX) |
| 124 | pr_debug("u3msi: MSI-X untested, trying anyway.\n"); |
| 125 | |
| 126 | /* If we can't find a magic address then MSI ain't gonna work */ |
| 127 | if (find_ht_magic_addr(pdev, 0) == 0 && |
| 128 | find_u4_magic_addr(pdev, 0) == 0) { |
| 129 | pr_debug("u3msi: no magic address found for %s\n", |
| 130 | pci_name(pdev)); |
| 131 | return -ENXIO; |
| 132 | } |
| 133 | |
Thomas Gleixner | 706b585 | 2021-12-06 23:51:31 +0100 | [diff] [blame] | 134 | msi_for_each_desc(entry, &pdev->dev, MSI_DESC_NOTASSOCIATED) { |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 135 | hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, 1); |
| 136 | if (hwirq < 0) { |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 137 | pr_debug("u3msi: failed allocating hwirq\n"); |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 138 | return hwirq; |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 139 | } |
| 140 | |
Benjamin Herrenschmidt | 7a96c6b | 2009-12-14 15:31:13 +0000 | [diff] [blame] | 141 | addr = find_ht_magic_addr(pdev, hwirq); |
| 142 | if (addr == 0) |
| 143 | addr = find_u4_magic_addr(pdev, hwirq); |
| 144 | msg.address_lo = addr & 0xFFFFFFFF; |
| 145 | msg.address_hi = addr >> 32; |
| 146 | |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 147 | virq = irq_create_mapping(msi_mpic->irqhost, hwirq); |
Michael Ellerman | ef24ba7 | 2016-09-06 21:53:24 +1000 | [diff] [blame] | 148 | if (!virq) { |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 149 | pr_debug("u3msi: failed mapping hwirq 0x%x\n", hwirq); |
| 150 | msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1); |
Michael Ellerman | d9303d6 | 2007-09-20 16:36:47 +1000 | [diff] [blame] | 151 | return -ENOSPC; |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 152 | } |
| 153 | |
Thomas Gleixner | ec775d0 | 2011-03-25 16:45:20 +0100 | [diff] [blame] | 154 | irq_set_msi_desc(virq, entry); |
| 155 | irq_set_chip(virq, &mpic_u3msi_chip); |
| 156 | irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING); |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 157 | |
Michael Ellerman | 25235f7 | 2008-08-06 09:10:03 +1000 | [diff] [blame] | 158 | pr_debug("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n", |
| 159 | virq, hwirq, (unsigned long)addr); |
Michael Ellerman | 21ccdd3 | 2007-09-20 16:36:51 +1000 | [diff] [blame] | 160 | |
Benjamin Herrenschmidt | 7a96c6b | 2009-12-14 15:31:13 +0000 | [diff] [blame] | 161 | printk("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n", |
| 162 | virq, hwirq, (unsigned long)addr); |
Michael Ellerman | 21ccdd3 | 2007-09-20 16:36:51 +1000 | [diff] [blame] | 163 | msg.data = hwirq; |
Jiang Liu | 83a1891 | 2014-11-09 23:10:34 +0800 | [diff] [blame] | 164 | pci_write_msi_msg(virq, &msg); |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 165 | |
| 166 | hwirq++; |
| 167 | } |
| 168 | |
| 169 | return 0; |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 170 | } |
| 171 | |
Nick Child | 6c55298 | 2021-12-16 17:00:20 -0500 | [diff] [blame] | 172 | int __init mpic_u3msi_init(struct mpic *mpic) |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 173 | { |
| 174 | int rc; |
Daniel Axtens | 14f95ac | 2015-04-14 14:28:02 +1000 | [diff] [blame] | 175 | struct pci_controller *phb; |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 176 | |
| 177 | rc = mpic_msi_init_allocator(mpic); |
| 178 | if (rc) { |
| 179 | pr_debug("u3msi: Error allocating bitmap!\n"); |
| 180 | return rc; |
| 181 | } |
| 182 | |
| 183 | pr_debug("u3msi: Registering MPIC U3 MSI callbacks.\n"); |
| 184 | |
| 185 | BUG_ON(msi_mpic); |
| 186 | msi_mpic = mpic; |
| 187 | |
Daniel Axtens | 14f95ac | 2015-04-14 14:28:02 +1000 | [diff] [blame] | 188 | list_for_each_entry(phb, &hose_list, list_node) { |
| 189 | WARN_ON(phb->controller_ops.setup_msi_irqs); |
| 190 | phb->controller_ops.setup_msi_irqs = u3msi_setup_msi_irqs; |
| 191 | phb->controller_ops.teardown_msi_irqs = u3msi_teardown_msi_irqs; |
| 192 | } |
Michael Ellerman | 05af7bd | 2007-05-08 12:58:37 +1000 | [diff] [blame] | 193 | |
| 194 | return 0; |
| 195 | } |