pci-ioda.c 36 KB

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