pci-ioda.c 36 KB

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