blob: db35a4073127db69b55cbda676349ed80bc5b07f [file] [log] [blame]
Michael Ellerman05af7bd2007-05-08 12:58:37 +10001/*
2 * Copyright 2006, Segher Boessenkool, IBM Corporation.
3 * Copyright 2006-2007, Michael Ellerman, IBM Corporation.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; version 2 of the
8 * License.
9 *
10 */
11
12#include <linux/irq.h>
13#include <linux/bootmem.h>
14#include <linux/msi.h>
15#include <asm/mpic.h>
16#include <asm/prom.h>
17#include <asm/hw_irq.h>
18#include <asm/ppc-pci.h>
Michael Ellerman25235f72008-08-06 09:10:03 +100019#include <asm/msi_bitmap.h>
Michael Ellerman05af7bd2007-05-08 12:58:37 +100020
21#include "mpic.h"
22
23/* A bit ugly, can we get this from the pci_dev somehow? */
24static struct mpic *msi_mpic;
25
Thomas Gleixner1c9db522010-09-28 16:46:51 +020026static void mpic_u3msi_mask_irq(struct irq_data *data)
Michael Ellerman05af7bd2007-05-08 12:58:37 +100027{
Thomas Gleixner1c9db522010-09-28 16:46:51 +020028 mask_msi_irq(data);
Lennert Buytenhek835c05532011-03-08 22:26:43 +000029 mpic_mask_irq(data);
Michael Ellerman05af7bd2007-05-08 12:58:37 +100030}
31
Thomas Gleixner1c9db522010-09-28 16:46:51 +020032static void mpic_u3msi_unmask_irq(struct irq_data *data)
Michael Ellerman05af7bd2007-05-08 12:58:37 +100033{
Lennert Buytenhek835c05532011-03-08 22:26:43 +000034 mpic_unmask_irq(data);
Thomas Gleixner1c9db522010-09-28 16:46:51 +020035 unmask_msi_irq(data);
Michael Ellerman05af7bd2007-05-08 12:58:37 +100036}
37
38static struct irq_chip mpic_u3msi_chip = {
Lennert Buytenhek835c05532011-03-08 22:26:43 +000039 .irq_shutdown = mpic_u3msi_mask_irq,
40 .irq_mask = mpic_u3msi_mask_irq,
41 .irq_unmask = mpic_u3msi_unmask_irq,
42 .irq_eoi = mpic_end_irq,
43 .irq_set_type = mpic_set_irq_type,
44 .irq_set_affinity = mpic_set_affinity,
45 .name = "MPIC-U3MSI",
Michael Ellerman05af7bd2007-05-08 12:58:37 +100046};
47
48static u64 read_ht_magic_addr(struct pci_dev *pdev, unsigned int pos)
49{
50 u8 flags;
51 u32 tmp;
52 u64 addr;
53
54 pci_read_config_byte(pdev, pos + HT_MSI_FLAGS, &flags);
55
56 if (flags & HT_MSI_FLAGS_FIXED)
57 return HT_MSI_FIXED_ADDR;
58
59 pci_read_config_dword(pdev, pos + HT_MSI_ADDR_LO, &tmp);
60 addr = tmp & HT_MSI_ADDR_LO_MASK;
61 pci_read_config_dword(pdev, pos + HT_MSI_ADDR_HI, &tmp);
62 addr = addr | ((u64)tmp << 32);
63
64 return addr;
65}
66
Benjamin Herrenschmidt7a96c6b2009-12-14 15:31:13 +000067static u64 find_ht_magic_addr(struct pci_dev *pdev, unsigned int hwirq)
Michael Ellerman05af7bd2007-05-08 12:58:37 +100068{
69 struct pci_bus *bus;
70 unsigned int pos;
71
Benjamin Herrenschmidt7a96c6b2009-12-14 15:31:13 +000072 for (bus = pdev->bus; bus && bus->self; bus = bus->parent) {
Michael Ellerman05af7bd2007-05-08 12:58:37 +100073 pos = pci_find_ht_capability(bus->self, HT_CAPTYPE_MSI_MAPPING);
74 if (pos)
75 return read_ht_magic_addr(bus->self, pos);
76 }
77
78 return 0;
79}
80
Benjamin Herrenschmidt7a96c6b2009-12-14 15:31:13 +000081static u64 find_u4_magic_addr(struct pci_dev *pdev, unsigned int hwirq)
82{
83 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
84
85 /* U4 PCIe MSIs need to write to the special register in
86 * the bridge that generates interrupts. There should be
87 * theorically a register at 0xf8005000 where you just write
88 * the MSI number and that triggers the right interrupt, but
89 * unfortunately, this is busted in HW, the bridge endian swaps
90 * the value and hits the wrong nibble in the register.
91 *
92 * So instead we use another register set which is used normally
93 * for converting HT interrupts to MPIC interrupts, which decodes
94 * the interrupt number as part of the low address bits
95 *
96 * This will not work if we ever use more than one legacy MSI in
97 * a block but we never do. For one MSI or multiple MSI-X where
98 * each interrupt address can be specified separately, it works
99 * just fine.
100 */
101 if (of_device_is_compatible(hose->dn, "u4-pcie") ||
102 of_device_is_compatible(hose->dn, "U4-pcie"))
103 return 0xf8004000 | (hwirq << 4);
104
105 return 0;
106}
107
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000108static void u3msi_teardown_msi_irqs(struct pci_dev *pdev)
109{
110 struct msi_desc *entry;
Paul Mackerras5b124052015-09-10 14:36:21 +1000111 irq_hw_number_t hwirq;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000112
113 list_for_each_entry(entry, &pdev->msi_list, list) {
114 if (entry->irq == NO_IRQ)
115 continue;
116
Paul Mackerras5b124052015-09-10 14:36:21 +1000117 hwirq = virq_to_hw(entry->irq);
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100118 irq_set_msi_desc(entry->irq, NULL);
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000119 irq_dispose_mapping(entry->irq);
Paul Mackerras5b124052015-09-10 14:36:21 +1000120 msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000121 }
122
123 return;
124}
125
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000126static int u3msi_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
127{
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000128 unsigned int virq;
129 struct msi_desc *entry;
130 struct msi_msg msg;
Michael Ellerman21ccdd32007-09-20 16:36:51 +1000131 u64 addr;
Michael Ellerman25235f72008-08-06 09:10:03 +1000132 int hwirq;
Michael Ellerman21ccdd32007-09-20 16:36:51 +1000133
Alexander Gordeev6b2fd7ef2014-09-07 20:57:53 +0200134 if (type == PCI_CAP_ID_MSIX)
135 pr_debug("u3msi: MSI-X untested, trying anyway.\n");
136
137 /* If we can't find a magic address then MSI ain't gonna work */
138 if (find_ht_magic_addr(pdev, 0) == 0 &&
139 find_u4_magic_addr(pdev, 0) == 0) {
140 pr_debug("u3msi: no magic address found for %s\n",
141 pci_name(pdev));
142 return -ENXIO;
143 }
144
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000145 list_for_each_entry(entry, &pdev->msi_list, list) {
Michael Ellerman25235f72008-08-06 09:10:03 +1000146 hwirq = msi_bitmap_alloc_hwirqs(&msi_mpic->msi_bitmap, 1);
147 if (hwirq < 0) {
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000148 pr_debug("u3msi: failed allocating hwirq\n");
Michael Ellerman25235f72008-08-06 09:10:03 +1000149 return hwirq;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000150 }
151
Benjamin Herrenschmidt7a96c6b2009-12-14 15:31:13 +0000152 addr = find_ht_magic_addr(pdev, hwirq);
153 if (addr == 0)
154 addr = find_u4_magic_addr(pdev, hwirq);
155 msg.address_lo = addr & 0xFFFFFFFF;
156 msg.address_hi = addr >> 32;
157
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000158 virq = irq_create_mapping(msi_mpic->irqhost, hwirq);
159 if (virq == NO_IRQ) {
Michael Ellerman25235f72008-08-06 09:10:03 +1000160 pr_debug("u3msi: failed mapping hwirq 0x%x\n", hwirq);
161 msi_bitmap_free_hwirqs(&msi_mpic->msi_bitmap, hwirq, 1);
Michael Ellermand9303d62007-09-20 16:36:47 +1000162 return -ENOSPC;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000163 }
164
Thomas Gleixnerec775d02011-03-25 16:45:20 +0100165 irq_set_msi_desc(virq, entry);
166 irq_set_chip(virq, &mpic_u3msi_chip);
167 irq_set_irq_type(virq, IRQ_TYPE_EDGE_RISING);
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000168
Michael Ellerman25235f72008-08-06 09:10:03 +1000169 pr_debug("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",
170 virq, hwirq, (unsigned long)addr);
Michael Ellerman21ccdd32007-09-20 16:36:51 +1000171
Benjamin Herrenschmidt7a96c6b2009-12-14 15:31:13 +0000172 printk("u3msi: allocated virq 0x%x (hw 0x%x) addr 0x%lx\n",
173 virq, hwirq, (unsigned long)addr);
Michael Ellerman21ccdd32007-09-20 16:36:51 +1000174 msg.data = hwirq;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000175 write_msi_msg(virq, &msg);
176
177 hwirq++;
178 }
179
180 return 0;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000181}
182
183int mpic_u3msi_init(struct mpic *mpic)
184{
185 int rc;
186
187 rc = mpic_msi_init_allocator(mpic);
188 if (rc) {
189 pr_debug("u3msi: Error allocating bitmap!\n");
190 return rc;
191 }
192
193 pr_debug("u3msi: Registering MPIC U3 MSI callbacks.\n");
194
195 BUG_ON(msi_mpic);
196 msi_mpic = mpic;
197
198 WARN_ON(ppc_md.setup_msi_irqs);
199 ppc_md.setup_msi_irqs = u3msi_setup_msi_irqs;
200 ppc_md.teardown_msi_irqs = u3msi_teardown_msi_irqs;
Michael Ellerman05af7bd2007-05-08 12:58:37 +1000201
202 return 0;
203}