blob: 5c175519cf9e25f191541bb87b3e139e179410ba [file] [log] [blame]
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +00001/*
2 * Support PCI/PCIe on PowerNV platforms
3 *
4 * Currently supports only P5IOC2
5 *
6 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/kernel.h>
15#include <linux/pci.h>
16#include <linux/delay.h>
17#include <linux/string.h>
18#include <linux/init.h>
19#include <linux/bootmem.h>
20#include <linux/irq.h>
21#include <linux/io.h>
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000022#include <linux/msi.h>
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +000023
24#include <asm/sections.h>
25#include <asm/io.h>
26#include <asm/prom.h>
27#include <asm/pci-bridge.h>
28#include <asm/machdep.h>
29#include <asm/ppc-pci.h>
30#include <asm/opal.h>
31#include <asm/iommu.h>
32#include <asm/tce.h>
33#include <asm/abs_addr.h>
34
35#include "powernv.h"
36#include "pci.h"
37
38
39#define cfg_dbg(fmt...) do { } while(0)
40//#define cfg_dbg(fmt...) printk(fmt)
41
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +000042#ifdef CONFIG_PCI_MSI
43static int pnv_msi_check_device(struct pci_dev* pdev, int nvec, int type)
44{
45 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
46 struct pnv_phb *phb = hose->private_data;
47
48 return (phb && phb->msi_map) ? 0 : -ENODEV;
49}
50
51static unsigned int pnv_get_one_msi(struct pnv_phb *phb)
52{
53 unsigned int id;
54
55 spin_lock(&phb->lock);
56 id = find_next_zero_bit(phb->msi_map, phb->msi_count, phb->msi_next);
57 if (id >= phb->msi_count && phb->msi_next)
58 id = find_next_zero_bit(phb->msi_map, phb->msi_count, 0);
59 if (id >= phb->msi_count) {
60 spin_unlock(&phb->lock);
61 return 0;
62 }
63 __set_bit(id, phb->msi_map);
64 spin_unlock(&phb->lock);
65 return id + phb->msi_base;
66}
67
68static void pnv_put_msi(struct pnv_phb *phb, unsigned int hwirq)
69{
70 unsigned int id;
71
72 if (WARN_ON(hwirq < phb->msi_base ||
73 hwirq >= (phb->msi_base + phb->msi_count)))
74 return;
75 id = hwirq - phb->msi_base;
76 spin_lock(&phb->lock);
77 __clear_bit(id, phb->msi_map);
78 spin_unlock(&phb->lock);
79}
80
81static int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
82{
83 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
84 struct pnv_phb *phb = hose->private_data;
85 struct msi_desc *entry;
86 struct msi_msg msg;
87 unsigned int hwirq, virq;
88 int rc;
89
90 if (WARN_ON(!phb))
91 return -ENODEV;
92
93 list_for_each_entry(entry, &pdev->msi_list, list) {
94 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
95 pr_warn("%s: Supports only 64-bit MSIs\n",
96 pci_name(pdev));
97 return -ENXIO;
98 }
99 hwirq = pnv_get_one_msi(phb);
100 if (!hwirq) {
101 pr_warn("%s: Failed to find a free MSI\n",
102 pci_name(pdev));
103 return -ENOSPC;
104 }
105 virq = irq_create_mapping(NULL, hwirq);
106 if (virq == NO_IRQ) {
107 pr_warn("%s: Failed to map MSI to linux irq\n",
108 pci_name(pdev));
109 pnv_put_msi(phb, hwirq);
110 return -ENOMEM;
111 }
112 rc = phb->msi_setup(phb, pdev, hwirq, entry->msi_attrib.is_64,
113 &msg);
114 if (rc) {
115 pr_warn("%s: Failed to setup MSI\n", pci_name(pdev));
116 irq_dispose_mapping(virq);
117 pnv_put_msi(phb, hwirq);
118 return rc;
119 }
120 irq_set_msi_desc(virq, entry);
121 write_msi_msg(virq, &msg);
122 }
123 return 0;
124}
125
126static void pnv_teardown_msi_irqs(struct pci_dev *pdev)
127{
128 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
129 struct pnv_phb *phb = hose->private_data;
130 struct msi_desc *entry;
131
132 if (WARN_ON(!phb))
133 return;
134
135 list_for_each_entry(entry, &pdev->msi_list, list) {
136 if (entry->irq == NO_IRQ)
137 continue;
138 irq_set_msi_desc(entry->irq, NULL);
139 pnv_put_msi(phb, virq_to_hw(entry->irq));
140 irq_dispose_mapping(entry->irq);
141 }
142}
143#endif /* CONFIG_PCI_MSI */
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000144
145static void pnv_pci_config_check_eeh(struct pnv_phb *phb, struct pci_bus *bus,
146 u32 bdfn)
147{
148 s64 rc;
149 u8 fstate;
150 u16 pcierr;
151 u32 pe_no;
152
153 /* Get PE# if we support IODA */
154 pe_no = phb->bdfn_to_pe ? phb->bdfn_to_pe(phb, bus, bdfn & 0xff) : 0;
155
156 /* Read freeze status */
157 rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no, &fstate, &pcierr,
158 NULL);
159 if (rc) {
160 pr_warning("PCI %d: Failed to read EEH status for PE#%d,"
161 " err %lld\n", phb->hose->global_number, pe_no, rc);
162 return;
163 }
164 cfg_dbg(" -> EEH check, bdfn=%04x PE%d fstate=%x\n",
165 bdfn, pe_no, fstate);
166 if (fstate != 0) {
167 rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
168 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
169 if (rc) {
170 pr_warning("PCI %d: Failed to clear EEH freeze state"
171 " for PE#%d, err %lld\n",
172 phb->hose->global_number, pe_no, rc);
173 }
174 }
175}
176
177static int pnv_pci_read_config(struct pci_bus *bus,
178 unsigned int devfn,
179 int where, int size, u32 *val)
180{
181 struct pci_controller *hose = pci_bus_to_host(bus);
182 struct pnv_phb *phb = hose->private_data;
183 u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
184 s64 rc;
185
186 if (hose == NULL)
187 return PCIBIOS_DEVICE_NOT_FOUND;
188
189 switch (size) {
190 case 1: {
191 u8 v8;
192 rc = opal_pci_config_read_byte(phb->opal_id, bdfn, where, &v8);
193 *val = (rc == OPAL_SUCCESS) ? v8 : 0xff;
194 break;
195 }
196 case 2: {
197 u16 v16;
198 rc = opal_pci_config_read_half_word(phb->opal_id, bdfn, where,
199 &v16);
200 *val = (rc == OPAL_SUCCESS) ? v16 : 0xffff;
201 break;
202 }
203 case 4: {
204 u32 v32;
205 rc = opal_pci_config_read_word(phb->opal_id, bdfn, where, &v32);
206 *val = (rc == OPAL_SUCCESS) ? v32 : 0xffffffff;
207 break;
208 }
209 default:
210 return PCIBIOS_FUNC_NOT_SUPPORTED;
211 }
212 cfg_dbg("pnv_pci_read_config bus: %x devfn: %x +%x/%x -> %08x\n",
213 bus->number, devfn, where, size, *val);
214
215 /* Check if the PHB got frozen due to an error (no response) */
216 pnv_pci_config_check_eeh(phb, bus, bdfn);
217
218 return PCIBIOS_SUCCESSFUL;
219}
220
221static int pnv_pci_write_config(struct pci_bus *bus,
222 unsigned int devfn,
223 int where, int size, u32 val)
224{
225 struct pci_controller *hose = pci_bus_to_host(bus);
226 struct pnv_phb *phb = hose->private_data;
227 u32 bdfn = (((uint64_t)bus->number) << 8) | devfn;
228
229 if (hose == NULL)
230 return PCIBIOS_DEVICE_NOT_FOUND;
231
232 cfg_dbg("pnv_pci_write_config bus: %x devfn: %x +%x/%x -> %08x\n",
233 bus->number, devfn, where, size, val);
234 switch (size) {
235 case 1:
236 opal_pci_config_write_byte(phb->opal_id, bdfn, where, val);
237 break;
238 case 2:
239 opal_pci_config_write_half_word(phb->opal_id, bdfn, where, val);
240 break;
241 case 4:
242 opal_pci_config_write_word(phb->opal_id, bdfn, where, val);
243 break;
244 default:
245 return PCIBIOS_FUNC_NOT_SUPPORTED;
246 }
247 /* Check if the PHB got frozen due to an error (no response) */
248 pnv_pci_config_check_eeh(phb, bus, bdfn);
249
250 return PCIBIOS_SUCCESSFUL;
251}
252
253struct pci_ops pnv_pci_ops = {
254 .read = pnv_pci_read_config,
255 .write = pnv_pci_write_config,
256};
257
258static int pnv_tce_build(struct iommu_table *tbl, long index, long npages,
259 unsigned long uaddr, enum dma_data_direction direction,
260 struct dma_attrs *attrs)
261{
262 u64 proto_tce;
263 u64 *tcep;
264 u64 rpn;
265
266 proto_tce = TCE_PCI_READ; // Read allowed
267
268 if (direction != DMA_TO_DEVICE)
269 proto_tce |= TCE_PCI_WRITE;
270
271 tcep = ((u64 *)tbl->it_base) + index;
272
273 while (npages--) {
274 /* can't move this out since we might cross LMB boundary */
275 rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
276 *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
277
278 uaddr += TCE_PAGE_SIZE;
279 tcep++;
280 }
281 return 0;
282}
283
284static void pnv_tce_free(struct iommu_table *tbl, long index, long npages)
285{
286 u64 *tcep = ((u64 *)tbl->it_base) + index;
287
288 while (npages--)
289 *(tcep++) = 0;
290}
291
292void pnv_pci_setup_iommu_table(struct iommu_table *tbl,
293 void *tce_mem, u64 tce_size,
294 u64 dma_offset)
295{
296 tbl->it_blocksize = 16;
297 tbl->it_base = (unsigned long)tce_mem;
298 tbl->it_offset = dma_offset >> IOMMU_PAGE_SHIFT;
299 tbl->it_index = 0;
300 tbl->it_size = tce_size >> 3;
301 tbl->it_busno = 0;
302 tbl->it_type = TCE_PCI;
303}
304
305static struct iommu_table * __devinit
306pnv_pci_setup_bml_iommu(struct pci_controller *hose)
307{
308 struct iommu_table *tbl;
309 const __be64 *basep;
310 const __be32 *sizep;
311
312 basep = of_get_property(hose->dn, "linux,tce-base", NULL);
313 sizep = of_get_property(hose->dn, "linux,tce-size", NULL);
314 if (basep == NULL || sizep == NULL) {
315 pr_err("PCI: %s has missing tce entries !\n", hose->dn->full_name);
316 return NULL;
317 }
318 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, hose->node);
319 if (WARN_ON(!tbl))
320 return NULL;
321 pnv_pci_setup_iommu_table(tbl, __va(be64_to_cpup(basep)),
322 be32_to_cpup(sizep), 0);
323 iommu_init_table(tbl, hose->node);
324 return tbl;
325}
326
327static void __devinit pnv_pci_dma_fallback_setup(struct pci_controller *hose,
328 struct pci_dev *pdev)
329{
330 struct device_node *np = pci_bus_to_OF_node(hose->bus);
331 struct pci_dn *pdn;
332
333 if (np == NULL)
334 return;
335 pdn = PCI_DN(np);
336 if (!pdn->iommu_table)
337 pdn->iommu_table = pnv_pci_setup_bml_iommu(hose);
338 if (!pdn->iommu_table)
339 return;
340 set_iommu_table_base(&pdev->dev, pdn->iommu_table);
341}
342
343static void __devinit pnv_pci_dma_dev_setup(struct pci_dev *pdev)
344{
345 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
346 struct pnv_phb *phb = hose->private_data;
347
348 /* If we have no phb structure, try to setup a fallback based on
349 * the device-tree (RTAS PCI for example)
350 */
351 if (phb && phb->dma_dev_setup)
352 phb->dma_dev_setup(phb, pdev);
353 else
354 pnv_pci_dma_fallback_setup(hose, pdev);
355}
356
357void __init pnv_pci_init(void)
358{
359 struct device_node *np;
360
361 pci_set_flags(PCI_CAN_SKIP_ISA_ALIGN);
362
363 /* We do not want to just probe */
364 pci_probe_only = 0;
365
366 /* OPAL absent, try POPAL first then RTAS detection of PHBs */
367 if (!firmware_has_feature(FW_FEATURE_OPAL)) {
368#ifdef CONFIG_PPC_POWERNV_RTAS
369 init_pci_config_tokens();
370 find_and_init_phbs();
371#endif /* CONFIG_PPC_POWERNV_RTAS */
372 } else {
373 /* OPAL is here, do our normal stuff */
374
375 /* Look for p5ioc2 IO-Hubs */
376 for_each_compatible_node(np, NULL, "ibm,p5ioc2")
377 pnv_pci_init_p5ioc2_hub(np);
378 }
379
380 /* Setup the linkage between OF nodes and PHBs */
381 pci_devs_phb_init();
382
383 /* Configure IOMMU DMA hooks */
384 ppc_md.pci_dma_dev_setup = pnv_pci_dma_dev_setup;
385 ppc_md.tce_build = pnv_tce_build;
386 ppc_md.tce_free = pnv_tce_free;
387 set_pci_dma_ops(&dma_iommu_ops);
388
Benjamin Herrenschmidtc1a25622011-09-19 17:45:06 +0000389 /* Configure MSIs */
390#ifdef CONFIG_PCI_MSI
391 ppc_md.msi_check_device = pnv_msi_check_device;
392 ppc_md.setup_msi_irqs = pnv_setup_msi_irqs;
393 ppc_md.teardown_msi_irqs = pnv_teardown_msi_irqs;
394#endif
Benjamin Herrenschmidt61305a92011-09-19 17:45:05 +0000395}