1 /*
2 * Support PCI/PCIe on PowerNV platforms
3 *
4 * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12 #undef DEBUG
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>
22 #include <linux/msi.h>
23
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 struct resource_wrap {
39 struct list_head link;
40 resource_size_t size;
41 resource_size_t align;
42 struct pci_dev *dev; /* Set if it's a device */
43 struct pci_bus *bus; /* Set if it's a bridge */
44 };
45
__pe_printk(const char * level,const struct pnv_ioda_pe * pe,struct va_format * vaf)46 static int __pe_printk(const char *level, const struct pnv_ioda_pe *pe,
47 struct va_format *vaf)
48 {
49 char pfix[32];
50
51 if (pe->pdev)
52 strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
53 else
54 sprintf(pfix, "%04x:%02x ",
55 pci_domain_nr(pe->pbus), pe->pbus->number);
56 return printk("pci %s%s: [PE# %.3d] %pV", level, pfix, pe->pe_number, vaf);
57 }
58
59 #define define_pe_printk_level(func, kern_level) \
60 static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
61 { \
62 struct va_format vaf; \
63 va_list args; \
64 int r; \
65 \
66 va_start(args, fmt); \
67 \
68 vaf.fmt = fmt; \
69 vaf.va = &args; \
70 \
71 r = __pe_printk(kern_level, pe, &vaf); \
72 va_end(args); \
73 \
74 return r; \
75 } \
76
77 define_pe_printk_level(pe_err, KERN_ERR);
78 define_pe_printk_level(pe_warn, KERN_WARNING);
79 define_pe_printk_level(pe_info, KERN_INFO);
80
81
82 /* Calculate resource usage & alignment requirement of a single
83 * device. This will also assign all resources within the device
84 * for a given type starting at 0 for the biggest one and then
85 * assigning in decreasing order of size.
86 */
pnv_ioda_calc_dev(struct pci_dev * dev,unsigned int flags,resource_size_t * size,resource_size_t * align)87 static void __devinit pnv_ioda_calc_dev(struct pci_dev *dev, unsigned int flags,
88 resource_size_t *size,
89 resource_size_t *align)
90 {
91 resource_size_t start;
92 struct resource *r;
93 int i;
94
95 pr_devel(" -> CDR %s\n", pci_name(dev));
96
97 *size = *align = 0;
98
99 /* Clear the resources out and mark them all unset */
100 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
101 r = &dev->resource[i];
102 if (!(r->flags & flags))
103 continue;
104 if (r->start) {
105 r->end -= r->start;
106 r->start = 0;
107 }
108 r->flags |= IORESOURCE_UNSET;
109 }
110
111 /* We currently keep all memory resources together, we
112 * will handle prefetch & 64-bit separately in the future
113 * but for now we stick everybody in M32
114 */
115 start = 0;
116 for (;;) {
117 resource_size_t max_size = 0;
118 int max_no = -1;
119
120 /* Find next biggest resource */
121 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
122 r = &dev->resource[i];
123 if (!(r->flags & IORESOURCE_UNSET) ||
124 !(r->flags & flags))
125 continue;
126 if (resource_size(r) > max_size) {
127 max_size = resource_size(r);
128 max_no = i;
129 }
130 }
131 if (max_no < 0)
132 break;
133 r = &dev->resource[max_no];
134 if (max_size > *align)
135 *align = max_size;
136 *size += max_size;
137 r->start = start;
138 start += max_size;
139 r->end = r->start + max_size - 1;
140 r->flags &= ~IORESOURCE_UNSET;
141 pr_devel(" -> R%d %016llx..%016llx\n",
142 max_no, r->start, r->end);
143 }
144 pr_devel(" <- CDR %s size=%llx align=%llx\n",
145 pci_name(dev), *size, *align);
146 }
147
148 /* Allocate a resource "wrap" for a given device or bridge and
149 * insert it at the right position in the sorted list
150 */
pnv_ioda_add_wrap(struct list_head * list,struct pci_bus * bus,struct pci_dev * dev,resource_size_t size,resource_size_t align)151 static void __devinit pnv_ioda_add_wrap(struct list_head *list,
152 struct pci_bus *bus,
153 struct pci_dev *dev,
154 resource_size_t size,
155 resource_size_t align)
156 {
157 struct resource_wrap *w1, *w = kzalloc(sizeof(*w), GFP_KERNEL);
158
159 w->size = size;
160 w->align = align;
161 w->dev = dev;
162 w->bus = bus;
163
164 list_for_each_entry(w1, list, link) {
165 if (w1->align < align) {
166 list_add_tail(&w->link, &w1->link);
167 return;
168 }
169 }
170 list_add_tail(&w->link, list);
171 }
172
173 /* Offset device resources of a given type */
pnv_ioda_offset_dev(struct pci_dev * dev,unsigned int flags,resource_size_t offset)174 static void __devinit pnv_ioda_offset_dev(struct pci_dev *dev,
175 unsigned int flags,
176 resource_size_t offset)
177 {
178 struct resource *r;
179 int i;
180
181 pr_devel(" -> ODR %s [%x] +%016llx\n", pci_name(dev), flags, offset);
182
183 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
184 r = &dev->resource[i];
185 if (r->flags & flags) {
186 dev->resource[i].start += offset;
187 dev->resource[i].end += offset;
188 }
189 }
190
191 pr_devel(" <- ODR %s [%x] +%016llx\n", pci_name(dev), flags, offset);
192 }
193
194 /* Offset bus resources (& all children) of a given type */
pnv_ioda_offset_bus(struct pci_bus * bus,unsigned int flags,resource_size_t offset)195 static void __devinit pnv_ioda_offset_bus(struct pci_bus *bus,
196 unsigned int flags,
197 resource_size_t offset)
198 {
199 struct resource *r;
200 struct pci_dev *dev;
201 struct pci_bus *cbus;
202 int i;
203
204 pr_devel(" -> OBR %s [%x] +%016llx\n",
205 bus->self ? pci_name(bus->self) : "root", flags, offset);
206
207 pci_bus_for_each_resource(bus, r, i) {
208 if (r && (r->flags & flags)) {
209 r->start += offset;
210 r->end += offset;
211 }
212 }
213 list_for_each_entry(dev, &bus->devices, bus_list)
214 pnv_ioda_offset_dev(dev, flags, offset);
215 list_for_each_entry(cbus, &bus->children, node)
216 pnv_ioda_offset_bus(cbus, flags, offset);
217
218 pr_devel(" <- OBR %s [%x]\n",
219 bus->self ? pci_name(bus->self) : "root", flags);
220 }
221
222 /* This is the guts of our IODA resource allocation. This is called
223 * recursively for each bus in the system. It calculates all the
224 * necessary size and requirements for children and assign them
225 * resources such that:
226 *
227 * - Each function fits in it's own contiguous set of IO/M32
228 * segment
229 *
230 * - All segments behind a P2P bridge are contiguous and obey
231 * alignment constraints of those bridges
232 */
pnv_ioda_calc_bus(struct pci_bus * bus,unsigned int flags,resource_size_t * size,resource_size_t * align)233 static void __devinit pnv_ioda_calc_bus(struct pci_bus *bus, unsigned int flags,
234 resource_size_t *size,
235 resource_size_t *align)
236 {
237 struct pci_controller *hose = pci_bus_to_host(bus);
238 struct pnv_phb *phb = hose->private_data;
239 resource_size_t dev_size, dev_align, start;
240 resource_size_t min_align, min_balign;
241 struct pci_dev *cdev;
242 struct pci_bus *cbus;
243 struct list_head head;
244 struct resource_wrap *w;
245 unsigned int bres;
246
247 *size = *align = 0;
248
249 pr_devel("-> CBR %s [%x]\n",
250 bus->self ? pci_name(bus->self) : "root", flags);
251
252 /* Calculate alignment requirements based on the type
253 * of resource we are working on
254 */
255 if (flags & IORESOURCE_IO) {
256 bres = 0;
257 min_align = phb->ioda.io_segsize;
258 min_balign = 0x1000;
259 } else {
260 bres = 1;
261 min_align = phb->ioda.m32_segsize;
262 min_balign = 0x100000;
263 }
264
265 /* Gather all our children resources ordered by alignment */
266 INIT_LIST_HEAD(&head);
267
268 /* - Busses */
269 list_for_each_entry(cbus, &bus->children, node) {
270 pnv_ioda_calc_bus(cbus, flags, &dev_size, &dev_align);
271 pnv_ioda_add_wrap(&head, cbus, NULL, dev_size, dev_align);
272 }
273
274 /* - Devices */
275 list_for_each_entry(cdev, &bus->devices, bus_list) {
276 pnv_ioda_calc_dev(cdev, flags, &dev_size, &dev_align);
277 /* Align them to segment size */
278 if (dev_align < min_align)
279 dev_align = min_align;
280 pnv_ioda_add_wrap(&head, NULL, cdev, dev_size, dev_align);
281 }
282 if (list_empty(&head))
283 goto empty;
284
285 /* Now we can do two things: assign offsets to them within that
286 * level and get our total alignment & size requirements. The
287 * assignment algorithm is going to be uber-trivial for now, we
288 * can try to be smarter later at filling out holes.
289 */
290 if (bus->self) {
291 /* No offset for downstream bridges */
292 start = 0;
293 } else {
294 /* Offset from the root */
295 if (flags & IORESOURCE_IO)
296 /* Don't hand out IO 0 */
297 start = hose->io_resource.start + 0x1000;
298 else
299 start = hose->mem_resources[0].start;
300 }
301 while(!list_empty(&head)) {
302 w = list_first_entry(&head, struct resource_wrap, link);
303 list_del(&w->link);
304 if (w->size) {
305 if (start) {
306 start = ALIGN(start, w->align);
307 if (w->dev)
308 pnv_ioda_offset_dev(w->dev,flags,start);
309 else if (w->bus)
310 pnv_ioda_offset_bus(w->bus,flags,start);
311 }
312 if (w->align > *align)
313 *align = w->align;
314 }
315 start += w->size;
316 kfree(w);
317 }
318 *size = start;
319
320 /* Align and setup bridge resources */
321 *align = max_t(resource_size_t, *align,
322 max_t(resource_size_t, min_align, min_balign));
323 *size = ALIGN(*size,
324 max_t(resource_size_t, min_align, min_balign));
325 empty:
326 /* Only setup P2P's, not the PHB itself */
327 if (bus->self) {
328 struct resource *res = bus->resource[bres];
329
330 if (WARN_ON(res == NULL))
331 return;
332
333 /*
334 * FIXME: We should probably export and call
335 * pci_bridge_check_ranges() to properly re-initialize
336 * the PCI portion of the flags here, and to detect
337 * what the bridge actually supports.
338 */
339 res->start = 0;
340 res->flags = (*size) ? flags : 0;
341 res->end = (*size) ? (*size - 1) : 0;
342 }
343
344 pr_devel("<- CBR %s [%x] *size=%016llx *align=%016llx\n",
345 bus->self ? pci_name(bus->self) : "root", flags,*size,*align);
346 }
347
pnv_ioda_get_pdn(struct pci_dev * dev)348 static struct pci_dn *pnv_ioda_get_pdn(struct pci_dev *dev)
349 {
350 struct device_node *np;
351
352 np = pci_device_to_OF_node(dev);
353 if (!np)
354 return NULL;
355 return PCI_DN(np);
356 }
357
pnv_ioda_setup_pe_segments(struct pci_dev * dev)358 static void __devinit pnv_ioda_setup_pe_segments(struct pci_dev *dev)
359 {
360 struct pci_controller *hose = pci_bus_to_host(dev->bus);
361 struct pnv_phb *phb = hose->private_data;
362 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
363 unsigned int pe, i;
364 resource_size_t pos;
365 struct resource io_res;
366 struct resource m32_res;
367 struct pci_bus_region region;
368 int rc;
369
370 /* Anything not referenced in the device-tree gets PE#0 */
371 pe = pdn ? pdn->pe_number : 0;
372
373 /* Calculate the device min/max */
374 io_res.start = m32_res.start = (resource_size_t)-1;
375 io_res.end = m32_res.end = 0;
376 io_res.flags = IORESOURCE_IO;
377 m32_res.flags = IORESOURCE_MEM;
378
379 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
380 struct resource *r = NULL;
381 if (dev->resource[i].flags & IORESOURCE_IO)
382 r = &io_res;
383 if (dev->resource[i].flags & IORESOURCE_MEM)
384 r = &m32_res;
385 if (!r)
386 continue;
387 if (dev->resource[i].start < r->start)
388 r->start = dev->resource[i].start;
389 if (dev->resource[i].end > r->end)
390 r->end = dev->resource[i].end;
391 }
392
393 /* Setup IO segments */
394 if (io_res.start < io_res.end) {
395 pcibios_resource_to_bus(dev, ®ion, &io_res);
396 pos = region.start;
397 i = pos / phb->ioda.io_segsize;
398 while(i < phb->ioda.total_pe && pos <= region.end) {
399 if (phb->ioda.io_segmap[i]) {
400 pr_err("%s: Trying to use IO seg #%d which is"
401 " already used by PE# %d\n",
402 pci_name(dev), i,
403 phb->ioda.io_segmap[i]);
404 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
405 break;
406 }
407 phb->ioda.io_segmap[i] = pe;
408 rc = opal_pci_map_pe_mmio_window(phb->opal_id, pe,
409 OPAL_IO_WINDOW_TYPE,
410 0, i);
411 if (rc != OPAL_SUCCESS) {
412 pr_err("%s: OPAL error %d setting up mapping"
413 " for IO seg# %d\n",
414 pci_name(dev), rc, i);
415 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
416 break;
417 }
418 pos += phb->ioda.io_segsize;
419 i++;
420 };
421 }
422
423 /* Setup M32 segments */
424 if (m32_res.start < m32_res.end) {
425 pcibios_resource_to_bus(dev, ®ion, &m32_res);
426 pos = region.start;
427 i = pos / phb->ioda.m32_segsize;
428 while(i < phb->ioda.total_pe && pos <= region.end) {
429 if (phb->ioda.m32_segmap[i]) {
430 pr_err("%s: Trying to use M32 seg #%d which is"
431 " already used by PE# %d\n",
432 pci_name(dev), i,
433 phb->ioda.m32_segmap[i]);
434 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
435 break;
436 }
437 phb->ioda.m32_segmap[i] = pe;
438 rc = opal_pci_map_pe_mmio_window(phb->opal_id, pe,
439 OPAL_M32_WINDOW_TYPE,
440 0, i);
441 if (rc != OPAL_SUCCESS) {
442 pr_err("%s: OPAL error %d setting up mapping"
443 " for M32 seg# %d\n",
444 pci_name(dev), rc, i);
445 /* XXX DO SOMETHING TO DISABLE DEVICE ? */
446 break;
447 }
448 pos += phb->ioda.m32_segsize;
449 i++;
450 }
451 }
452 }
453
454 /* Check if a resource still fits in the total IO or M32 range
455 * for a given PHB
456 */
pnv_ioda_resource_fit(struct pci_controller * hose,struct resource * r)457 static int __devinit pnv_ioda_resource_fit(struct pci_controller *hose,
458 struct resource *r)
459 {
460 struct resource *bounds;
461
462 if (r->flags & IORESOURCE_IO)
463 bounds = &hose->io_resource;
464 else if (r->flags & IORESOURCE_MEM)
465 bounds = &hose->mem_resources[0];
466 else
467 return 1;
468
469 if (r->start >= bounds->start && r->end <= bounds->end)
470 return 1;
471 r->flags = 0;
472 return 0;
473 }
474
pnv_ioda_update_resources(struct pci_bus * bus)475 static void __devinit pnv_ioda_update_resources(struct pci_bus *bus)
476 {
477 struct pci_controller *hose = pci_bus_to_host(bus);
478 struct pci_bus *cbus;
479 struct pci_dev *cdev;
480 unsigned int i;
481
482 /* We used to clear all device enables here. However it looks like
483 * clearing MEM enable causes Obsidian (IPR SCS) to go bonkers,
484 * and shoot fatal errors to the PHB which in turns fences itself
485 * and we can't recover from that ... yet. So for now, let's leave
486 * the enables as-is and hope for the best.
487 */
488
489 /* Check if bus resources fit in our IO or M32 range */
490 for (i = 0; bus->self && (i < 2); i++) {
491 struct resource *r = bus->resource[i];
492 if (r && !pnv_ioda_resource_fit(hose, r))
493 pr_err("%s: Bus %d resource %d disabled, no room\n",
494 pci_name(bus->self), bus->number, i);
495 }
496
497 /* Update self if it's not a PHB */
498 if (bus->self)
499 pci_setup_bridge(bus);
500
501 /* Update child devices */
502 list_for_each_entry(cdev, &bus->devices, bus_list) {
503 /* Check if resource fits, if not, disabled it */
504 for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
505 struct resource *r = &cdev->resource[i];
506 if (!pnv_ioda_resource_fit(hose, r))
507 pr_err("%s: Resource %d disabled, no room\n",
508 pci_name(cdev), i);
509 }
510
511 /* Assign segments */
512 pnv_ioda_setup_pe_segments(cdev);
513
514 /* Update HW BARs */
515 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
516 pci_update_resource(cdev, i);
517 }
518
519 /* Update child busses */
520 list_for_each_entry(cbus, &bus->children, node)
521 pnv_ioda_update_resources(cbus);
522 }
523
pnv_ioda_alloc_pe(struct pnv_phb * phb)524 static int __devinit pnv_ioda_alloc_pe(struct pnv_phb *phb)
525 {
526 unsigned long pe;
527
528 do {
529 pe = find_next_zero_bit(phb->ioda.pe_alloc,
530 phb->ioda.total_pe, 0);
531 if (pe >= phb->ioda.total_pe)
532 return IODA_INVALID_PE;
533 } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
534
535 phb->ioda.pe_array[pe].pe_number = pe;
536 return pe;
537 }
538
pnv_ioda_free_pe(struct pnv_phb * phb,int pe)539 static void __devinit pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
540 {
541 WARN_ON(phb->ioda.pe_array[pe].pdev);
542
543 memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
544 clear_bit(pe, phb->ioda.pe_alloc);
545 }
546
547 /* Currently those 2 are only used when MSIs are enabled, this will change
548 * but in the meantime, we need to protect them to avoid warnings
549 */
550 #ifdef CONFIG_PCI_MSI
__pnv_ioda_get_one_pe(struct pci_dev * dev)551 static struct pnv_ioda_pe * __devinit __pnv_ioda_get_one_pe(struct pci_dev *dev)
552 {
553 struct pci_controller *hose = pci_bus_to_host(dev->bus);
554 struct pnv_phb *phb = hose->private_data;
555 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
556
557 if (!pdn)
558 return NULL;
559 if (pdn->pe_number == IODA_INVALID_PE)
560 return NULL;
561 return &phb->ioda.pe_array[pdn->pe_number];
562 }
563
pnv_ioda_get_pe(struct pci_dev * dev)564 static struct pnv_ioda_pe * __devinit pnv_ioda_get_pe(struct pci_dev *dev)
565 {
566 struct pnv_ioda_pe *pe = __pnv_ioda_get_one_pe(dev);
567
568 while (!pe && dev->bus->self) {
569 dev = dev->bus->self;
570 pe = __pnv_ioda_get_one_pe(dev);
571 if (pe)
572 pe = pe->bus_pe;
573 }
574 return pe;
575 }
576 #endif /* CONFIG_PCI_MSI */
577
pnv_ioda_configure_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe)578 static int __devinit pnv_ioda_configure_pe(struct pnv_phb *phb,
579 struct pnv_ioda_pe *pe)
580 {
581 struct pci_dev *parent;
582 uint8_t bcomp, dcomp, fcomp;
583 long rc, rid_end, rid;
584
585 /* Bus validation ? */
586 if (pe->pbus) {
587 int count;
588
589 dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
590 fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
591 parent = pe->pbus->self;
592 count = pe->pbus->subordinate - pe->pbus->secondary + 1;
593 switch(count) {
594 case 1: bcomp = OpalPciBusAll; break;
595 case 2: bcomp = OpalPciBus7Bits; break;
596 case 4: bcomp = OpalPciBus6Bits; break;
597 case 8: bcomp = OpalPciBus5Bits; break;
598 case 16: bcomp = OpalPciBus4Bits; break;
599 case 32: bcomp = OpalPciBus3Bits; break;
600 default:
601 pr_err("%s: Number of subordinate busses %d"
602 " unsupported\n",
603 pci_name(pe->pbus->self), count);
604 /* Do an exact match only */
605 bcomp = OpalPciBusAll;
606 }
607 rid_end = pe->rid + (count << 8);
608 } else {
609 parent = pe->pdev->bus->self;
610 bcomp = OpalPciBusAll;
611 dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
612 fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
613 rid_end = pe->rid + 1;
614 }
615
616 /*
617 * Associate PE in PELT. We need add the PE into the
618 * corresponding PELT-V as well. Otherwise, the error
619 * originated from the PE might contribute to other
620 * PEs.
621 */
622 rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
623 bcomp, dcomp, fcomp, OPAL_MAP_PE);
624 if (rc) {
625 pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
626 return -ENXIO;
627 }
628
629 rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
630 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
631 if (rc)
632 pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc);
633 opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
634 OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
635
636 /* Add to all parents PELT-V */
637 while (parent) {
638 struct pci_dn *pdn = pnv_ioda_get_pdn(parent);
639 if (pdn && pdn->pe_number != IODA_INVALID_PE) {
640 rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
641 pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
642 /* XXX What to do in case of error ? */
643 }
644 parent = parent->bus->self;
645 }
646 /* Setup reverse map */
647 for (rid = pe->rid; rid < rid_end; rid++)
648 phb->ioda.pe_rmap[rid] = pe->pe_number;
649
650 /* Setup one MVTs on IODA1 */
651 if (phb->type == PNV_PHB_IODA1) {
652 pe->mve_number = pe->pe_number;
653 rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
654 pe->pe_number);
655 if (rc) {
656 pe_err(pe, "OPAL error %ld setting up MVE %d\n",
657 rc, pe->mve_number);
658 pe->mve_number = -1;
659 } else {
660 rc = opal_pci_set_mve_enable(phb->opal_id,
661 pe->mve_number, OPAL_ENABLE_MVE);
662 if (rc) {
663 pe_err(pe, "OPAL error %ld enabling MVE %d\n",
664 rc, pe->mve_number);
665 pe->mve_number = -1;
666 }
667 }
668 } else if (phb->type == PNV_PHB_IODA2)
669 pe->mve_number = 0;
670
671 return 0;
672 }
673
pnv_ioda_link_pe_by_weight(struct pnv_phb * phb,struct pnv_ioda_pe * pe)674 static void __devinit pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
675 struct pnv_ioda_pe *pe)
676 {
677 struct pnv_ioda_pe *lpe;
678
679 list_for_each_entry(lpe, &phb->ioda.pe_list, link) {
680 if (lpe->dma_weight < pe->dma_weight) {
681 list_add_tail(&pe->link, &lpe->link);
682 return;
683 }
684 }
685 list_add_tail(&pe->link, &phb->ioda.pe_list);
686 }
687
pnv_ioda_dma_weight(struct pci_dev * dev)688 static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
689 {
690 /* This is quite simplistic. The "base" weight of a device
691 * is 10. 0 means no DMA is to be accounted for it.
692 */
693
694 /* If it's a bridge, no DMA */
695 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
696 return 0;
697
698 /* Reduce the weight of slow USB controllers */
699 if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
700 dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
701 dev->class == PCI_CLASS_SERIAL_USB_EHCI)
702 return 3;
703
704 /* Increase the weight of RAID (includes Obsidian) */
705 if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
706 return 15;
707
708 /* Default */
709 return 10;
710 }
711
pnv_ioda_setup_dev_PE(struct pci_dev * dev)712 static struct pnv_ioda_pe * __devinit pnv_ioda_setup_dev_PE(struct pci_dev *dev)
713 {
714 struct pci_controller *hose = pci_bus_to_host(dev->bus);
715 struct pnv_phb *phb = hose->private_data;
716 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
717 struct pnv_ioda_pe *pe;
718 int pe_num;
719
720 if (!pdn) {
721 pr_err("%s: Device tree node not associated properly\n",
722 pci_name(dev));
723 return NULL;
724 }
725 if (pdn->pe_number != IODA_INVALID_PE)
726 return NULL;
727
728 /* PE#0 has been pre-set */
729 if (dev->bus->number == 0)
730 pe_num = 0;
731 else
732 pe_num = pnv_ioda_alloc_pe(phb);
733 if (pe_num == IODA_INVALID_PE) {
734 pr_warning("%s: Not enough PE# available, disabling device\n",
735 pci_name(dev));
736 return NULL;
737 }
738
739 /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
740 * pointer in the PE data structure, both should be destroyed at the
741 * same time. However, this needs to be looked at more closely again
742 * once we actually start removing things (Hotplug, SR-IOV, ...)
743 *
744 * At some point we want to remove the PDN completely anyways
745 */
746 pe = &phb->ioda.pe_array[pe_num];
747 pci_dev_get(dev);
748 pdn->pcidev = dev;
749 pdn->pe_number = pe_num;
750 pe->pdev = dev;
751 pe->pbus = NULL;
752 pe->tce32_seg = -1;
753 pe->mve_number = -1;
754 pe->rid = dev->bus->number << 8 | pdn->devfn;
755
756 pe_info(pe, "Associated device to PE\n");
757
758 if (pnv_ioda_configure_pe(phb, pe)) {
759 /* XXX What do we do here ? */
760 if (pe_num)
761 pnv_ioda_free_pe(phb, pe_num);
762 pdn->pe_number = IODA_INVALID_PE;
763 pe->pdev = NULL;
764 pci_dev_put(dev);
765 return NULL;
766 }
767
768 /* Assign a DMA weight to the device */
769 pe->dma_weight = pnv_ioda_dma_weight(dev);
770 if (pe->dma_weight != 0) {
771 phb->ioda.dma_weight += pe->dma_weight;
772 phb->ioda.dma_pe_count++;
773 }
774
775 /* Link the PE */
776 pnv_ioda_link_pe_by_weight(phb, pe);
777
778 return pe;
779 }
780
pnv_ioda_setup_same_PE(struct pci_bus * bus,struct pnv_ioda_pe * pe)781 static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
782 {
783 struct pci_dev *dev;
784
785 list_for_each_entry(dev, &bus->devices, bus_list) {
786 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
787
788 if (pdn == NULL) {
789 pr_warn("%s: No device node associated with device !\n",
790 pci_name(dev));
791 continue;
792 }
793 pci_dev_get(dev);
794 pdn->pcidev = dev;
795 pdn->pe_number = pe->pe_number;
796 pe->dma_weight += pnv_ioda_dma_weight(dev);
797 if (dev->subordinate)
798 pnv_ioda_setup_same_PE(dev->subordinate, pe);
799 }
800 }
801
pnv_ioda_setup_bus_PE(struct pci_dev * dev,struct pnv_ioda_pe * ppe)802 static void __devinit pnv_ioda_setup_bus_PE(struct pci_dev *dev,
803 struct pnv_ioda_pe *ppe)
804 {
805 struct pci_controller *hose = pci_bus_to_host(dev->bus);
806 struct pnv_phb *phb = hose->private_data;
807 struct pci_bus *bus = dev->subordinate;
808 struct pnv_ioda_pe *pe;
809 int pe_num;
810
811 if (!bus) {
812 pr_warning("%s: Bridge without a subordinate bus !\n",
813 pci_name(dev));
814 return;
815 }
816 pe_num = pnv_ioda_alloc_pe(phb);
817 if (pe_num == IODA_INVALID_PE) {
818 pr_warning("%s: Not enough PE# available, disabling bus\n",
819 pci_name(dev));
820 return;
821 }
822
823 pe = &phb->ioda.pe_array[pe_num];
824 ppe->bus_pe = pe;
825 pe->pbus = bus;
826 pe->pdev = NULL;
827 pe->tce32_seg = -1;
828 pe->mve_number = -1;
829 pe->rid = bus->secondary << 8;
830 pe->dma_weight = 0;
831
832 pe_info(pe, "Secondary busses %d..%d associated with PE\n",
833 bus->secondary, bus->subordinate);
834
835 if (pnv_ioda_configure_pe(phb, pe)) {
836 /* XXX What do we do here ? */
837 if (pe_num)
838 pnv_ioda_free_pe(phb, pe_num);
839 pe->pbus = NULL;
840 return;
841 }
842
843 /* Associate it with all child devices */
844 pnv_ioda_setup_same_PE(bus, pe);
845
846 /* Account for one DMA PE if at least one DMA capable device exist
847 * below the bridge
848 */
849 if (pe->dma_weight != 0) {
850 phb->ioda.dma_weight += pe->dma_weight;
851 phb->ioda.dma_pe_count++;
852 }
853
854 /* Link the PE */
855 pnv_ioda_link_pe_by_weight(phb, pe);
856 }
857
pnv_ioda_setup_PEs(struct pci_bus * bus)858 static void __devinit pnv_ioda_setup_PEs(struct pci_bus *bus)
859 {
860 struct pci_dev *dev;
861 struct pnv_ioda_pe *pe;
862
863 list_for_each_entry(dev, &bus->devices, bus_list) {
864 pe = pnv_ioda_setup_dev_PE(dev);
865 if (pe == NULL)
866 continue;
867 /* Leaving the PCIe domain ... single PE# */
868 if (dev->pcie_type == PCI_EXP_TYPE_PCI_BRIDGE)
869 pnv_ioda_setup_bus_PE(dev, pe);
870 else if (dev->subordinate)
871 pnv_ioda_setup_PEs(dev->subordinate);
872 }
873 }
874
pnv_pci_ioda_dma_dev_setup(struct pnv_phb * phb,struct pci_dev * dev)875 static void __devinit pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb,
876 struct pci_dev *dev)
877 {
878 /* We delay DMA setup after we have assigned all PE# */
879 }
880
pnv_ioda_setup_bus_dma(struct pnv_ioda_pe * pe,struct pci_bus * bus)881 static void __devinit pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
882 struct pci_bus *bus)
883 {
884 struct pci_dev *dev;
885
886 list_for_each_entry(dev, &bus->devices, bus_list) {
887 set_iommu_table_base(&dev->dev, &pe->tce32_table);
888 if (dev->subordinate)
889 pnv_ioda_setup_bus_dma(pe, dev->subordinate);
890 }
891 }
892
pnv_pci_ioda_setup_dma_pe(struct pnv_phb * phb,struct pnv_ioda_pe * pe,unsigned int base,unsigned int segs)893 static void __devinit pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
894 struct pnv_ioda_pe *pe,
895 unsigned int base,
896 unsigned int segs)
897 {
898
899 struct page *tce_mem = NULL;
900 const __be64 *swinvp;
901 struct iommu_table *tbl;
902 unsigned int i;
903 int64_t rc;
904 void *addr;
905
906 /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
907 #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
908
909 /* XXX FIXME: Handle 64-bit only DMA devices */
910 /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
911 /* XXX FIXME: Allocate multi-level tables on PHB3 */
912
913 /* We shouldn't already have a 32-bit DMA associated */
914 if (WARN_ON(pe->tce32_seg >= 0))
915 return;
916
917 /* Grab a 32-bit TCE table */
918 pe->tce32_seg = base;
919 pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
920 (base << 28), ((base + segs) << 28) - 1);
921
922 /* XXX Currently, we allocate one big contiguous table for the
923 * TCEs. We only really need one chunk per 256M of TCE space
924 * (ie per segment) but that's an optimization for later, it
925 * requires some added smarts with our get/put_tce implementation
926 */
927 tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
928 get_order(TCE32_TABLE_SIZE * segs));
929 if (!tce_mem) {
930 pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
931 goto fail;
932 }
933 addr = page_address(tce_mem);
934 memset(addr, 0, TCE32_TABLE_SIZE * segs);
935
936 /* Configure HW */
937 for (i = 0; i < segs; i++) {
938 rc = opal_pci_map_pe_dma_window(phb->opal_id,
939 pe->pe_number,
940 base + i, 1,
941 __pa(addr) + TCE32_TABLE_SIZE * i,
942 TCE32_TABLE_SIZE, 0x1000);
943 if (rc) {
944 pe_err(pe, " Failed to configure 32-bit TCE table,"
945 " err %ld\n", rc);
946 goto fail;
947 }
948 }
949
950 /* Setup linux iommu table */
951 tbl = &pe->tce32_table;
952 pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
953 base << 28);
954
955 /* OPAL variant of P7IOC SW invalidated TCEs */
956 swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
957 if (swinvp) {
958 /* We need a couple more fields -- an address and a data
959 * to or. Since the bus is only printed out on table free
960 * errors, and on the first pass the data will be a relative
961 * bus number, print that out instead.
962 */
963 tbl->it_busno = 0;
964 tbl->it_index = (unsigned long)ioremap(be64_to_cpup(swinvp), 8);
965 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE
966 | TCE_PCI_SWINV_PAIR;
967 }
968 iommu_init_table(tbl, phb->hose->node);
969
970 if (pe->pdev)
971 set_iommu_table_base(&pe->pdev->dev, tbl);
972 else
973 pnv_ioda_setup_bus_dma(pe, pe->pbus);
974
975 return;
976 fail:
977 /* XXX Failure: Try to fallback to 64-bit only ? */
978 if (pe->tce32_seg >= 0)
979 pe->tce32_seg = -1;
980 if (tce_mem)
981 __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
982 }
983
pnv_ioda_setup_dma(struct pnv_phb * phb)984 static void __devinit pnv_ioda_setup_dma(struct pnv_phb *phb)
985 {
986 struct pci_controller *hose = phb->hose;
987 unsigned int residual, remaining, segs, tw, base;
988 struct pnv_ioda_pe *pe;
989
990 /* If we have more PE# than segments available, hand out one
991 * per PE until we run out and let the rest fail. If not,
992 * then we assign at least one segment per PE, plus more based
993 * on the amount of devices under that PE
994 */
995 if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
996 residual = 0;
997 else
998 residual = phb->ioda.tce32_count -
999 phb->ioda.dma_pe_count;
1000
1001 pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
1002 hose->global_number, phb->ioda.tce32_count);
1003 pr_info("PCI: %d PE# for a total weight of %d\n",
1004 phb->ioda.dma_pe_count, phb->ioda.dma_weight);
1005
1006 /* Walk our PE list and configure their DMA segments, hand them
1007 * out one base segment plus any residual segments based on
1008 * weight
1009 */
1010 remaining = phb->ioda.tce32_count;
1011 tw = phb->ioda.dma_weight;
1012 base = 0;
1013 list_for_each_entry(pe, &phb->ioda.pe_list, link) {
1014 if (!pe->dma_weight)
1015 continue;
1016 if (!remaining) {
1017 pe_warn(pe, "No DMA32 resources available\n");
1018 continue;
1019 }
1020 segs = 1;
1021 if (residual) {
1022 segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
1023 if (segs > remaining)
1024 segs = remaining;
1025 }
1026 pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
1027 pe->dma_weight, segs);
1028 pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
1029 remaining -= segs;
1030 base += segs;
1031 }
1032 }
1033
1034 #ifdef CONFIG_PCI_MSI
pnv_pci_ioda_msi_setup(struct pnv_phb * phb,struct pci_dev * dev,unsigned int hwirq,unsigned int is_64,struct msi_msg * msg)1035 static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
1036 unsigned int hwirq, unsigned int is_64,
1037 struct msi_msg *msg)
1038 {
1039 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
1040 unsigned int xive_num = hwirq - phb->msi_base;
1041 uint64_t addr64;
1042 uint32_t addr32, data;
1043 int rc;
1044
1045 /* No PE assigned ? bail out ... no MSI for you ! */
1046 if (pe == NULL)
1047 return -ENXIO;
1048
1049 /* Check if we have an MVE */
1050 if (pe->mve_number < 0)
1051 return -ENXIO;
1052
1053 /* Assign XIVE to PE */
1054 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
1055 if (rc) {
1056 pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
1057 pci_name(dev), rc, xive_num);
1058 return -EIO;
1059 }
1060
1061 if (is_64) {
1062 rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
1063 &addr64, &data);
1064 if (rc) {
1065 pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
1066 pci_name(dev), rc);
1067 return -EIO;
1068 }
1069 msg->address_hi = addr64 >> 32;
1070 msg->address_lo = addr64 & 0xfffffffful;
1071 } else {
1072 rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
1073 &addr32, &data);
1074 if (rc) {
1075 pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
1076 pci_name(dev), rc);
1077 return -EIO;
1078 }
1079 msg->address_hi = 0;
1080 msg->address_lo = addr32;
1081 }
1082 msg->data = data;
1083
1084 pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
1085 " address=%x_%08x data=%x PE# %d\n",
1086 pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
1087 msg->address_hi, msg->address_lo, data, pe->pe_number);
1088
1089 return 0;
1090 }
1091
pnv_pci_init_ioda_msis(struct pnv_phb * phb)1092 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
1093 {
1094 unsigned int bmap_size;
1095 const __be32 *prop = of_get_property(phb->hose->dn,
1096 "ibm,opal-msi-ranges", NULL);
1097 if (!prop) {
1098 /* BML Fallback */
1099 prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
1100 }
1101 if (!prop)
1102 return;
1103
1104 phb->msi_base = be32_to_cpup(prop);
1105 phb->msi_count = be32_to_cpup(prop + 1);
1106 bmap_size = BITS_TO_LONGS(phb->msi_count) * sizeof(unsigned long);
1107 phb->msi_map = zalloc_maybe_bootmem(bmap_size, GFP_KERNEL);
1108 if (!phb->msi_map) {
1109 pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
1110 phb->hose->global_number);
1111 return;
1112 }
1113 phb->msi_setup = pnv_pci_ioda_msi_setup;
1114 phb->msi32_support = 1;
1115 pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
1116 phb->msi_count, phb->msi_base);
1117 }
1118 #else
pnv_pci_init_ioda_msis(struct pnv_phb * phb)1119 static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
1120 #endif /* CONFIG_PCI_MSI */
1121
1122 /* This is the starting point of our IODA specific resource
1123 * allocation process
1124 */
pnv_pci_ioda_fixup_phb(struct pci_controller * hose)1125 static void __devinit pnv_pci_ioda_fixup_phb(struct pci_controller *hose)
1126 {
1127 resource_size_t size, align;
1128 struct pci_bus *child;
1129
1130 /* Associate PEs per functions */
1131 pnv_ioda_setup_PEs(hose->bus);
1132
1133 /* Calculate all resources */
1134 pnv_ioda_calc_bus(hose->bus, IORESOURCE_IO, &size, &align);
1135 pnv_ioda_calc_bus(hose->bus, IORESOURCE_MEM, &size, &align);
1136
1137 /* Apply then to HW */
1138 pnv_ioda_update_resources(hose->bus);
1139
1140 /* Setup DMA */
1141 pnv_ioda_setup_dma(hose->private_data);
1142
1143 /* Configure PCI Express settings */
1144 list_for_each_entry(child, &hose->bus->children, node) {
1145 struct pci_dev *self = child->self;
1146 if (!self)
1147 continue;
1148 pcie_bus_configure_settings(child, self->pcie_mpss);
1149 }
1150 }
1151
1152 /* Prevent enabling devices for which we couldn't properly
1153 * assign a PE
1154 */
pnv_pci_enable_device_hook(struct pci_dev * dev)1155 static int __devinit pnv_pci_enable_device_hook(struct pci_dev *dev)
1156 {
1157 struct pci_dn *pdn = pnv_ioda_get_pdn(dev);
1158
1159 if (!pdn || pdn->pe_number == IODA_INVALID_PE)
1160 return -EINVAL;
1161 return 0;
1162 }
1163
pnv_ioda_bdfn_to_pe(struct pnv_phb * phb,struct pci_bus * bus,u32 devfn)1164 static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
1165 u32 devfn)
1166 {
1167 return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
1168 }
1169
pnv_pci_init_ioda1_phb(struct device_node * np)1170 void __init pnv_pci_init_ioda1_phb(struct device_node *np)
1171 {
1172 struct pci_controller *hose;
1173 static int primary = 1;
1174 struct pnv_phb *phb;
1175 unsigned long size, m32map_off, iomap_off, pemap_off;
1176 const u64 *prop64;
1177 u64 phb_id;
1178 void *aux;
1179 long rc;
1180
1181 pr_info(" Initializing IODA OPAL PHB %s\n", np->full_name);
1182
1183 prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
1184 if (!prop64) {
1185 pr_err(" Missing \"ibm,opal-phbid\" property !\n");
1186 return;
1187 }
1188 phb_id = be64_to_cpup(prop64);
1189 pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
1190
1191 phb = alloc_bootmem(sizeof(struct pnv_phb));
1192 if (phb) {
1193 memset(phb, 0, sizeof(struct pnv_phb));
1194 phb->hose = hose = pcibios_alloc_controller(np);
1195 }
1196 if (!phb || !phb->hose) {
1197 pr_err("PCI: Failed to allocate PCI controller for %s\n",
1198 np->full_name);
1199 return;
1200 }
1201
1202 spin_lock_init(&phb->lock);
1203 /* XXX Use device-tree */
1204 hose->first_busno = 0;
1205 hose->last_busno = 0xff;
1206 hose->private_data = phb;
1207 phb->opal_id = phb_id;
1208 phb->type = PNV_PHB_IODA1;
1209
1210 /* Detect specific models for error handling */
1211 if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
1212 phb->model = PNV_PHB_MODEL_P7IOC;
1213 else
1214 phb->model = PNV_PHB_MODEL_UNKNOWN;
1215
1216 /* We parse "ranges" now since we need to deduce the register base
1217 * from the IO base
1218 */
1219 pci_process_bridge_OF_ranges(phb->hose, np, primary);
1220 primary = 0;
1221
1222 /* Magic formula from Milton */
1223 phb->regs = of_iomap(np, 0);
1224 if (phb->regs == NULL)
1225 pr_err(" Failed to map registers !\n");
1226
1227
1228 /* XXX This is hack-a-thon. This needs to be changed so that:
1229 * - we obtain stuff like PE# etc... from device-tree
1230 * - we properly re-allocate M32 ourselves
1231 * (the OFW one isn't very good)
1232 */
1233
1234 /* Initialize more IODA stuff */
1235 phb->ioda.total_pe = 128;
1236
1237 phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
1238 /* OFW Has already off top 64k of M32 space (MSI space) */
1239 phb->ioda.m32_size += 0x10000;
1240
1241 phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
1242 phb->ioda.m32_pci_base = hose->mem_resources[0].start -
1243 hose->pci_mem_offset;
1244 phb->ioda.io_size = hose->pci_io_size;
1245 phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
1246 phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
1247
1248 /* Allocate aux data & arrays */
1249 size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
1250 m32map_off = size;
1251 size += phb->ioda.total_pe;
1252 iomap_off = size;
1253 size += phb->ioda.total_pe;
1254 pemap_off = size;
1255 size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
1256 aux = alloc_bootmem(size);
1257 memset(aux, 0, size);
1258 phb->ioda.pe_alloc = aux;
1259 phb->ioda.m32_segmap = aux + m32map_off;
1260 phb->ioda.io_segmap = aux + iomap_off;
1261 phb->ioda.pe_array = aux + pemap_off;
1262 set_bit(0, phb->ioda.pe_alloc);
1263
1264 INIT_LIST_HEAD(&phb->ioda.pe_list);
1265
1266 /* Calculate how many 32-bit TCE segments we have */
1267 phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
1268
1269 /* Clear unusable m64 */
1270 hose->mem_resources[1].flags = 0;
1271 hose->mem_resources[1].start = 0;
1272 hose->mem_resources[1].end = 0;
1273 hose->mem_resources[2].flags = 0;
1274 hose->mem_resources[2].start = 0;
1275 hose->mem_resources[2].end = 0;
1276
1277 #if 0
1278 rc = opal_pci_set_phb_mem_window(opal->phb_id,
1279 window_type,
1280 window_num,
1281 starting_real_address,
1282 starting_pci_address,
1283 segment_size);
1284 #endif
1285
1286 pr_info(" %d PE's M32: 0x%x [segment=0x%x] IO: 0x%x [segment=0x%x]\n",
1287 phb->ioda.total_pe,
1288 phb->ioda.m32_size, phb->ioda.m32_segsize,
1289 phb->ioda.io_size, phb->ioda.io_segsize);
1290
1291 if (phb->regs) {
1292 pr_devel(" BUID = 0x%016llx\n", in_be64(phb->regs + 0x100));
1293 pr_devel(" PHB2_CR = 0x%016llx\n", in_be64(phb->regs + 0x160));
1294 pr_devel(" IO_BAR = 0x%016llx\n", in_be64(phb->regs + 0x170));
1295 pr_devel(" IO_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x178));
1296 pr_devel(" IO_SAR = 0x%016llx\n", in_be64(phb->regs + 0x180));
1297 pr_devel(" M32_BAR = 0x%016llx\n", in_be64(phb->regs + 0x190));
1298 pr_devel(" M32_BAMR = 0x%016llx\n", in_be64(phb->regs + 0x198));
1299 pr_devel(" M32_SAR = 0x%016llx\n", in_be64(phb->regs + 0x1a0));
1300 }
1301 phb->hose->ops = &pnv_pci_ops;
1302
1303 /* Setup RID -> PE mapping function */
1304 phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
1305
1306 /* Setup TCEs */
1307 phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
1308
1309 /* Setup MSI support */
1310 pnv_pci_init_ioda_msis(phb);
1311
1312 /* We set both PCI_PROBE_ONLY and PCI_REASSIGN_ALL_RSRC. This is an
1313 * odd combination which essentially means that we skip all resource
1314 * fixups and assignments in the generic code, and do it all
1315 * ourselves here
1316 */
1317 ppc_md.pcibios_fixup_phb = pnv_pci_ioda_fixup_phb;
1318 ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
1319 pci_add_flags(PCI_PROBE_ONLY | PCI_REASSIGN_ALL_RSRC);
1320
1321 /* Reset IODA tables to a clean state */
1322 rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
1323 if (rc)
1324 pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
1325 opal_pci_set_pe(phb_id, 0, 0, 7, 1, 1 , OPAL_MAP_PE);
1326 }
1327
pnv_pci_init_ioda_hub(struct device_node * np)1328 void __init pnv_pci_init_ioda_hub(struct device_node *np)
1329 {
1330 struct device_node *phbn;
1331 const u64 *prop64;
1332 u64 hub_id;
1333
1334 pr_info("Probing IODA IO-Hub %s\n", np->full_name);
1335
1336 prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
1337 if (!prop64) {
1338 pr_err(" Missing \"ibm,opal-hubid\" property !\n");
1339 return;
1340 }
1341 hub_id = be64_to_cpup(prop64);
1342 pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
1343
1344 /* Count child PHBs */
1345 for_each_child_of_node(np, phbn) {
1346 /* Look for IODA1 PHBs */
1347 if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
1348 pnv_pci_init_ioda1_phb(phbn);
1349 }
1350 }
1351