pci-ioda.c 41 KB

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