pci-ioda.c 36 KB

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