pci-ioda.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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/crash_dump.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <linux/msi.h>
  23. #include <linux/memblock.h>
  24. #include <asm/sections.h>
  25. #include <asm/io.h>
  26. #include <asm/prom.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/machdep.h>
  29. #include <asm/msi_bitmap.h>
  30. #include <asm/ppc-pci.h>
  31. #include <asm/opal.h>
  32. #include <asm/iommu.h>
  33. #include <asm/tce.h>
  34. #include <asm/xics.h>
  35. #include <asm/debug.h>
  36. #include <asm/firmware.h>
  37. #include "powernv.h"
  38. #include "pci.h"
  39. #define define_pe_printk_level(func, kern_level) \
  40. static int func(const struct pnv_ioda_pe *pe, const char *fmt, ...) \
  41. { \
  42. struct va_format vaf; \
  43. va_list args; \
  44. char pfix[32]; \
  45. int r; \
  46. \
  47. va_start(args, fmt); \
  48. \
  49. vaf.fmt = fmt; \
  50. vaf.va = &args; \
  51. \
  52. if (pe->pdev) \
  53. strlcpy(pfix, dev_name(&pe->pdev->dev), \
  54. sizeof(pfix)); \
  55. else \
  56. sprintf(pfix, "%04x:%02x ", \
  57. pci_domain_nr(pe->pbus), \
  58. pe->pbus->number); \
  59. r = printk(kern_level "pci %s: [PE# %.3d] %pV", \
  60. pfix, pe->pe_number, &vaf); \
  61. \
  62. va_end(args); \
  63. \
  64. return r; \
  65. } \
  66. define_pe_printk_level(pe_err, KERN_ERR);
  67. define_pe_printk_level(pe_warn, KERN_WARNING);
  68. define_pe_printk_level(pe_info, KERN_INFO);
  69. /*
  70. * stdcix is only supposed to be used in hypervisor real mode as per
  71. * the architecture spec
  72. */
  73. static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
  74. {
  75. __asm__ __volatile__("stdcix %0,0,%1"
  76. : : "r" (val), "r" (paddr) : "memory");
  77. }
  78. static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
  79. {
  80. return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
  81. (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
  82. }
  83. static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
  84. {
  85. unsigned long pe;
  86. do {
  87. pe = find_next_zero_bit(phb->ioda.pe_alloc,
  88. phb->ioda.total_pe, 0);
  89. if (pe >= phb->ioda.total_pe)
  90. return IODA_INVALID_PE;
  91. } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
  92. phb->ioda.pe_array[pe].phb = phb;
  93. phb->ioda.pe_array[pe].pe_number = pe;
  94. return pe;
  95. }
  96. static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
  97. {
  98. WARN_ON(phb->ioda.pe_array[pe].pdev);
  99. memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
  100. clear_bit(pe, phb->ioda.pe_alloc);
  101. }
  102. /* The default M64 BAR is shared by all PEs */
  103. static int pnv_ioda2_init_m64(struct pnv_phb *phb)
  104. {
  105. const char *desc;
  106. struct resource *r;
  107. s64 rc;
  108. /* Configure the default M64 BAR */
  109. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  110. OPAL_M64_WINDOW_TYPE,
  111. phb->ioda.m64_bar_idx,
  112. phb->ioda.m64_base,
  113. 0, /* unused */
  114. phb->ioda.m64_size);
  115. if (rc != OPAL_SUCCESS) {
  116. desc = "configuring";
  117. goto fail;
  118. }
  119. /* Enable the default M64 BAR */
  120. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  121. OPAL_M64_WINDOW_TYPE,
  122. phb->ioda.m64_bar_idx,
  123. OPAL_ENABLE_M64_SPLIT);
  124. if (rc != OPAL_SUCCESS) {
  125. desc = "enabling";
  126. goto fail;
  127. }
  128. /* Mark the M64 BAR assigned */
  129. set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
  130. /*
  131. * Strip off the segment used by the reserved PE, which is
  132. * expected to be 0 or last one of PE capabicity.
  133. */
  134. r = &phb->hose->mem_resources[1];
  135. if (phb->ioda.reserved_pe == 0)
  136. r->start += phb->ioda.m64_segsize;
  137. else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
  138. r->end -= phb->ioda.m64_segsize;
  139. else
  140. pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
  141. phb->ioda.reserved_pe);
  142. return 0;
  143. fail:
  144. pr_warn(" Failure %lld %s M64 BAR#%d\n",
  145. rc, desc, phb->ioda.m64_bar_idx);
  146. opal_pci_phb_mmio_enable(phb->opal_id,
  147. OPAL_M64_WINDOW_TYPE,
  148. phb->ioda.m64_bar_idx,
  149. OPAL_DISABLE_M64);
  150. return -EIO;
  151. }
  152. static void pnv_ioda2_alloc_m64_pe(struct pnv_phb *phb)
  153. {
  154. resource_size_t sgsz = phb->ioda.m64_segsize;
  155. struct pci_dev *pdev;
  156. struct resource *r;
  157. int base, step, i;
  158. /*
  159. * Root bus always has full M64 range and root port has
  160. * M64 range used in reality. So we're checking root port
  161. * instead of root bus.
  162. */
  163. list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
  164. for (i = PCI_BRIDGE_RESOURCES;
  165. i <= PCI_BRIDGE_RESOURCE_END; i++) {
  166. r = &pdev->resource[i];
  167. if (!r->parent ||
  168. !pnv_pci_is_mem_pref_64(r->flags))
  169. continue;
  170. base = (r->start - phb->ioda.m64_base) / sgsz;
  171. for (step = 0; step < resource_size(r) / sgsz; step++)
  172. set_bit(base + step, phb->ioda.pe_alloc);
  173. }
  174. }
  175. }
  176. static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
  177. struct pci_bus *bus, int all)
  178. {
  179. resource_size_t segsz = phb->ioda.m64_segsize;
  180. struct pci_dev *pdev;
  181. struct resource *r;
  182. struct pnv_ioda_pe *master_pe, *pe;
  183. unsigned long size, *pe_alloc;
  184. bool found;
  185. int start, i, j;
  186. /* Root bus shouldn't use M64 */
  187. if (pci_is_root_bus(bus))
  188. return IODA_INVALID_PE;
  189. /* We support only one M64 window on each bus */
  190. found = false;
  191. pci_bus_for_each_resource(bus, r, i) {
  192. if (r && r->parent &&
  193. pnv_pci_is_mem_pref_64(r->flags)) {
  194. found = true;
  195. break;
  196. }
  197. }
  198. /* No M64 window found ? */
  199. if (!found)
  200. return IODA_INVALID_PE;
  201. /* Allocate bitmap */
  202. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  203. pe_alloc = kzalloc(size, GFP_KERNEL);
  204. if (!pe_alloc) {
  205. pr_warn("%s: Out of memory !\n",
  206. __func__);
  207. return IODA_INVALID_PE;
  208. }
  209. /*
  210. * Figure out reserved PE numbers by the PE
  211. * the its child PEs.
  212. */
  213. start = (r->start - phb->ioda.m64_base) / segsz;
  214. for (i = 0; i < resource_size(r) / segsz; i++)
  215. set_bit(start + i, pe_alloc);
  216. if (all)
  217. goto done;
  218. /*
  219. * If the PE doesn't cover all subordinate buses,
  220. * we need subtract from reserved PEs for children.
  221. */
  222. list_for_each_entry(pdev, &bus->devices, bus_list) {
  223. if (!pdev->subordinate)
  224. continue;
  225. pci_bus_for_each_resource(pdev->subordinate, r, i) {
  226. if (!r || !r->parent ||
  227. !pnv_pci_is_mem_pref_64(r->flags))
  228. continue;
  229. start = (r->start - phb->ioda.m64_base) / segsz;
  230. for (j = 0; j < resource_size(r) / segsz ; j++)
  231. clear_bit(start + j, pe_alloc);
  232. }
  233. }
  234. /*
  235. * the current bus might not own M64 window and that's all
  236. * contributed by its child buses. For the case, we needn't
  237. * pick M64 dependent PE#.
  238. */
  239. if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
  240. kfree(pe_alloc);
  241. return IODA_INVALID_PE;
  242. }
  243. /*
  244. * Figure out the master PE and put all slave PEs to master
  245. * PE's list to form compound PE.
  246. */
  247. done:
  248. master_pe = NULL;
  249. i = -1;
  250. while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
  251. phb->ioda.total_pe) {
  252. pe = &phb->ioda.pe_array[i];
  253. pe->phb = phb;
  254. pe->pe_number = i;
  255. if (!master_pe) {
  256. pe->flags |= PNV_IODA_PE_MASTER;
  257. INIT_LIST_HEAD(&pe->slaves);
  258. master_pe = pe;
  259. } else {
  260. pe->flags |= PNV_IODA_PE_SLAVE;
  261. pe->master = master_pe;
  262. list_add_tail(&pe->list, &master_pe->slaves);
  263. }
  264. }
  265. kfree(pe_alloc);
  266. return master_pe->pe_number;
  267. }
  268. static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
  269. {
  270. struct pci_controller *hose = phb->hose;
  271. struct device_node *dn = hose->dn;
  272. struct resource *res;
  273. const u32 *r;
  274. u64 pci_addr;
  275. if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
  276. pr_info(" Firmware too old to support M64 window\n");
  277. return;
  278. }
  279. r = of_get_property(dn, "ibm,opal-m64-window", NULL);
  280. if (!r) {
  281. pr_info(" No <ibm,opal-m64-window> on %s\n",
  282. dn->full_name);
  283. return;
  284. }
  285. /* FIXME: Support M64 for P7IOC */
  286. if (phb->type != PNV_PHB_IODA2) {
  287. pr_info(" Not support M64 window\n");
  288. return;
  289. }
  290. res = &hose->mem_resources[1];
  291. res->start = of_translate_address(dn, r + 2);
  292. res->end = res->start + of_read_number(r + 4, 2) - 1;
  293. res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
  294. pci_addr = of_read_number(r, 2);
  295. hose->mem_offset[1] = res->start - pci_addr;
  296. phb->ioda.m64_size = resource_size(res);
  297. phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
  298. phb->ioda.m64_base = pci_addr;
  299. /* Use last M64 BAR to cover M64 window */
  300. phb->ioda.m64_bar_idx = 15;
  301. phb->init_m64 = pnv_ioda2_init_m64;
  302. phb->alloc_m64_pe = pnv_ioda2_alloc_m64_pe;
  303. phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
  304. }
  305. static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
  306. {
  307. struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
  308. struct pnv_ioda_pe *slave;
  309. s64 rc;
  310. /* Fetch master PE */
  311. if (pe->flags & PNV_IODA_PE_SLAVE) {
  312. pe = pe->master;
  313. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  314. pe_no = pe->pe_number;
  315. }
  316. /* Freeze master PE */
  317. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  318. pe_no,
  319. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  320. if (rc != OPAL_SUCCESS) {
  321. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  322. __func__, rc, phb->hose->global_number, pe_no);
  323. return;
  324. }
  325. /* Freeze slave PEs */
  326. if (!(pe->flags & PNV_IODA_PE_MASTER))
  327. return;
  328. list_for_each_entry(slave, &pe->slaves, list) {
  329. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  330. slave->pe_number,
  331. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  332. if (rc != OPAL_SUCCESS)
  333. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  334. __func__, rc, phb->hose->global_number,
  335. slave->pe_number);
  336. }
  337. }
  338. int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
  339. {
  340. struct pnv_ioda_pe *pe, *slave;
  341. s64 rc;
  342. /* Find master PE */
  343. pe = &phb->ioda.pe_array[pe_no];
  344. if (pe->flags & PNV_IODA_PE_SLAVE) {
  345. pe = pe->master;
  346. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  347. pe_no = pe->pe_number;
  348. }
  349. /* Clear frozen state for master PE */
  350. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
  351. if (rc != OPAL_SUCCESS) {
  352. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  353. __func__, rc, opt, phb->hose->global_number, pe_no);
  354. return -EIO;
  355. }
  356. if (!(pe->flags & PNV_IODA_PE_MASTER))
  357. return 0;
  358. /* Clear frozen state for slave PEs */
  359. list_for_each_entry(slave, &pe->slaves, list) {
  360. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  361. slave->pe_number,
  362. opt);
  363. if (rc != OPAL_SUCCESS) {
  364. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  365. __func__, rc, opt, phb->hose->global_number,
  366. slave->pe_number);
  367. return -EIO;
  368. }
  369. }
  370. return 0;
  371. }
  372. static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
  373. {
  374. struct pnv_ioda_pe *slave, *pe;
  375. u8 fstate, state;
  376. __be16 pcierr;
  377. s64 rc;
  378. /* Sanity check on PE number */
  379. if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
  380. return OPAL_EEH_STOPPED_PERM_UNAVAIL;
  381. /*
  382. * Fetch the master PE and the PE instance might be
  383. * not initialized yet.
  384. */
  385. pe = &phb->ioda.pe_array[pe_no];
  386. if (pe->flags & PNV_IODA_PE_SLAVE) {
  387. pe = pe->master;
  388. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  389. pe_no = pe->pe_number;
  390. }
  391. /* Check the master PE */
  392. rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
  393. &state, &pcierr, NULL);
  394. if (rc != OPAL_SUCCESS) {
  395. pr_warn("%s: Failure %lld getting "
  396. "PHB#%x-PE#%x state\n",
  397. __func__, rc,
  398. phb->hose->global_number, pe_no);
  399. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  400. }
  401. /* Check the slave PE */
  402. if (!(pe->flags & PNV_IODA_PE_MASTER))
  403. return state;
  404. list_for_each_entry(slave, &pe->slaves, list) {
  405. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  406. slave->pe_number,
  407. &fstate,
  408. &pcierr,
  409. NULL);
  410. if (rc != OPAL_SUCCESS) {
  411. pr_warn("%s: Failure %lld getting "
  412. "PHB#%x-PE#%x state\n",
  413. __func__, rc,
  414. phb->hose->global_number, slave->pe_number);
  415. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  416. }
  417. /*
  418. * Override the result based on the ascending
  419. * priority.
  420. */
  421. if (fstate > state)
  422. state = fstate;
  423. }
  424. return state;
  425. }
  426. /* Currently those 2 are only used when MSIs are enabled, this will change
  427. * but in the meantime, we need to protect them to avoid warnings
  428. */
  429. #ifdef CONFIG_PCI_MSI
  430. static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
  431. {
  432. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  433. struct pnv_phb *phb = hose->private_data;
  434. struct pci_dn *pdn = pci_get_pdn(dev);
  435. if (!pdn)
  436. return NULL;
  437. if (pdn->pe_number == IODA_INVALID_PE)
  438. return NULL;
  439. return &phb->ioda.pe_array[pdn->pe_number];
  440. }
  441. #endif /* CONFIG_PCI_MSI */
  442. static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  443. {
  444. struct pci_dev *parent;
  445. uint8_t bcomp, dcomp, fcomp;
  446. long rc, rid_end, rid;
  447. /* Bus validation ? */
  448. if (pe->pbus) {
  449. int count;
  450. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  451. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  452. parent = pe->pbus->self;
  453. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  454. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  455. else
  456. count = 1;
  457. switch(count) {
  458. case 1: bcomp = OpalPciBusAll; break;
  459. case 2: bcomp = OpalPciBus7Bits; break;
  460. case 4: bcomp = OpalPciBus6Bits; break;
  461. case 8: bcomp = OpalPciBus5Bits; break;
  462. case 16: bcomp = OpalPciBus4Bits; break;
  463. case 32: bcomp = OpalPciBus3Bits; break;
  464. default:
  465. pr_err("%s: Number of subordinate busses %d"
  466. " unsupported\n",
  467. pci_name(pe->pbus->self), count);
  468. /* Do an exact match only */
  469. bcomp = OpalPciBusAll;
  470. }
  471. rid_end = pe->rid + (count << 8);
  472. } else {
  473. parent = pe->pdev->bus->self;
  474. bcomp = OpalPciBusAll;
  475. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  476. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  477. rid_end = pe->rid + 1;
  478. }
  479. /*
  480. * Associate PE in PELT. We need add the PE into the
  481. * corresponding PELT-V as well. Otherwise, the error
  482. * originated from the PE might contribute to other
  483. * PEs.
  484. */
  485. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  486. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  487. if (rc) {
  488. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  489. return -ENXIO;
  490. }
  491. rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
  492. pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
  493. if (rc)
  494. pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc);
  495. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  496. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  497. /* Add to all parents PELT-V */
  498. while (parent) {
  499. struct pci_dn *pdn = pci_get_pdn(parent);
  500. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  501. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  502. pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
  503. /* XXX What to do in case of error ? */
  504. }
  505. parent = parent->bus->self;
  506. }
  507. /* Setup reverse map */
  508. for (rid = pe->rid; rid < rid_end; rid++)
  509. phb->ioda.pe_rmap[rid] = pe->pe_number;
  510. /* Setup one MVTs on IODA1 */
  511. if (phb->type == PNV_PHB_IODA1) {
  512. pe->mve_number = pe->pe_number;
  513. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
  514. pe->pe_number);
  515. if (rc) {
  516. pe_err(pe, "OPAL error %ld setting up MVE %d\n",
  517. rc, pe->mve_number);
  518. pe->mve_number = -1;
  519. } else {
  520. rc = opal_pci_set_mve_enable(phb->opal_id,
  521. pe->mve_number, OPAL_ENABLE_MVE);
  522. if (rc) {
  523. pe_err(pe, "OPAL error %ld enabling MVE %d\n",
  524. rc, pe->mve_number);
  525. pe->mve_number = -1;
  526. }
  527. }
  528. } else if (phb->type == PNV_PHB_IODA2)
  529. pe->mve_number = 0;
  530. return 0;
  531. }
  532. static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
  533. struct pnv_ioda_pe *pe)
  534. {
  535. struct pnv_ioda_pe *lpe;
  536. list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
  537. if (lpe->dma_weight < pe->dma_weight) {
  538. list_add_tail(&pe->dma_link, &lpe->dma_link);
  539. return;
  540. }
  541. }
  542. list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
  543. }
  544. static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
  545. {
  546. /* This is quite simplistic. The "base" weight of a device
  547. * is 10. 0 means no DMA is to be accounted for it.
  548. */
  549. /* If it's a bridge, no DMA */
  550. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  551. return 0;
  552. /* Reduce the weight of slow USB controllers */
  553. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  554. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  555. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  556. return 3;
  557. /* Increase the weight of RAID (includes Obsidian) */
  558. if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  559. return 15;
  560. /* Default */
  561. return 10;
  562. }
  563. #if 0
  564. static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  565. {
  566. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  567. struct pnv_phb *phb = hose->private_data;
  568. struct pci_dn *pdn = pci_get_pdn(dev);
  569. struct pnv_ioda_pe *pe;
  570. int pe_num;
  571. if (!pdn) {
  572. pr_err("%s: Device tree node not associated properly\n",
  573. pci_name(dev));
  574. return NULL;
  575. }
  576. if (pdn->pe_number != IODA_INVALID_PE)
  577. return NULL;
  578. /* PE#0 has been pre-set */
  579. if (dev->bus->number == 0)
  580. pe_num = 0;
  581. else
  582. pe_num = pnv_ioda_alloc_pe(phb);
  583. if (pe_num == IODA_INVALID_PE) {
  584. pr_warning("%s: Not enough PE# available, disabling device\n",
  585. pci_name(dev));
  586. return NULL;
  587. }
  588. /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
  589. * pointer in the PE data structure, both should be destroyed at the
  590. * same time. However, this needs to be looked at more closely again
  591. * once we actually start removing things (Hotplug, SR-IOV, ...)
  592. *
  593. * At some point we want to remove the PDN completely anyways
  594. */
  595. pe = &phb->ioda.pe_array[pe_num];
  596. pci_dev_get(dev);
  597. pdn->pcidev = dev;
  598. pdn->pe_number = pe_num;
  599. pe->pdev = dev;
  600. pe->pbus = NULL;
  601. pe->tce32_seg = -1;
  602. pe->mve_number = -1;
  603. pe->rid = dev->bus->number << 8 | pdn->devfn;
  604. pe_info(pe, "Associated device to PE\n");
  605. if (pnv_ioda_configure_pe(phb, pe)) {
  606. /* XXX What do we do here ? */
  607. if (pe_num)
  608. pnv_ioda_free_pe(phb, pe_num);
  609. pdn->pe_number = IODA_INVALID_PE;
  610. pe->pdev = NULL;
  611. pci_dev_put(dev);
  612. return NULL;
  613. }
  614. /* Assign a DMA weight to the device */
  615. pe->dma_weight = pnv_ioda_dma_weight(dev);
  616. if (pe->dma_weight != 0) {
  617. phb->ioda.dma_weight += pe->dma_weight;
  618. phb->ioda.dma_pe_count++;
  619. }
  620. /* Link the PE */
  621. pnv_ioda_link_pe_by_weight(phb, pe);
  622. return pe;
  623. }
  624. #endif /* Useful for SRIOV case */
  625. static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
  626. {
  627. struct pci_dev *dev;
  628. list_for_each_entry(dev, &bus->devices, bus_list) {
  629. struct pci_dn *pdn = pci_get_pdn(dev);
  630. if (pdn == NULL) {
  631. pr_warn("%s: No device node associated with device !\n",
  632. pci_name(dev));
  633. continue;
  634. }
  635. pdn->pcidev = dev;
  636. pdn->pe_number = pe->pe_number;
  637. pe->dma_weight += pnv_ioda_dma_weight(dev);
  638. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  639. pnv_ioda_setup_same_PE(dev->subordinate, pe);
  640. }
  641. }
  642. /*
  643. * There're 2 types of PCI bus sensitive PEs: One that is compromised of
  644. * single PCI bus. Another one that contains the primary PCI bus and its
  645. * subordinate PCI devices and buses. The second type of PE is normally
  646. * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
  647. */
  648. static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
  649. {
  650. struct pci_controller *hose = pci_bus_to_host(bus);
  651. struct pnv_phb *phb = hose->private_data;
  652. struct pnv_ioda_pe *pe;
  653. int pe_num = IODA_INVALID_PE;
  654. /* Check if PE is determined by M64 */
  655. if (phb->pick_m64_pe)
  656. pe_num = phb->pick_m64_pe(phb, bus, all);
  657. /* The PE number isn't pinned by M64 */
  658. if (pe_num == IODA_INVALID_PE)
  659. pe_num = pnv_ioda_alloc_pe(phb);
  660. if (pe_num == IODA_INVALID_PE) {
  661. pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
  662. __func__, pci_domain_nr(bus), bus->number);
  663. return;
  664. }
  665. pe = &phb->ioda.pe_array[pe_num];
  666. pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
  667. pe->pbus = bus;
  668. pe->pdev = NULL;
  669. pe->tce32_seg = -1;
  670. pe->mve_number = -1;
  671. pe->rid = bus->busn_res.start << 8;
  672. pe->dma_weight = 0;
  673. if (all)
  674. pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
  675. bus->busn_res.start, bus->busn_res.end, pe_num);
  676. else
  677. pe_info(pe, "Secondary bus %d associated with PE#%d\n",
  678. bus->busn_res.start, pe_num);
  679. if (pnv_ioda_configure_pe(phb, pe)) {
  680. /* XXX What do we do here ? */
  681. if (pe_num)
  682. pnv_ioda_free_pe(phb, pe_num);
  683. pe->pbus = NULL;
  684. return;
  685. }
  686. /* Associate it with all child devices */
  687. pnv_ioda_setup_same_PE(bus, pe);
  688. /* Put PE to the list */
  689. list_add_tail(&pe->list, &phb->ioda.pe_list);
  690. /* Account for one DMA PE if at least one DMA capable device exist
  691. * below the bridge
  692. */
  693. if (pe->dma_weight != 0) {
  694. phb->ioda.dma_weight += pe->dma_weight;
  695. phb->ioda.dma_pe_count++;
  696. }
  697. /* Link the PE */
  698. pnv_ioda_link_pe_by_weight(phb, pe);
  699. }
  700. static void pnv_ioda_setup_PEs(struct pci_bus *bus)
  701. {
  702. struct pci_dev *dev;
  703. pnv_ioda_setup_bus_PE(bus, 0);
  704. list_for_each_entry(dev, &bus->devices, bus_list) {
  705. if (dev->subordinate) {
  706. if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
  707. pnv_ioda_setup_bus_PE(dev->subordinate, 1);
  708. else
  709. pnv_ioda_setup_PEs(dev->subordinate);
  710. }
  711. }
  712. }
  713. /*
  714. * Configure PEs so that the downstream PCI buses and devices
  715. * could have their associated PE#. Unfortunately, we didn't
  716. * figure out the way to identify the PLX bridge yet. So we
  717. * simply put the PCI bus and the subordinate behind the root
  718. * port to PE# here. The game rule here is expected to be changed
  719. * as soon as we can detected PLX bridge correctly.
  720. */
  721. static void pnv_pci_ioda_setup_PEs(void)
  722. {
  723. struct pci_controller *hose, *tmp;
  724. struct pnv_phb *phb;
  725. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  726. phb = hose->private_data;
  727. /* M64 layout might affect PE allocation */
  728. if (phb->alloc_m64_pe)
  729. phb->alloc_m64_pe(phb);
  730. pnv_ioda_setup_PEs(hose->bus);
  731. }
  732. }
  733. static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
  734. {
  735. struct pci_dn *pdn = pci_get_pdn(pdev);
  736. struct pnv_ioda_pe *pe;
  737. /*
  738. * The function can be called while the PE#
  739. * hasn't been assigned. Do nothing for the
  740. * case.
  741. */
  742. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  743. return;
  744. pe = &phb->ioda.pe_array[pdn->pe_number];
  745. WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
  746. set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table);
  747. }
  748. static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
  749. struct pci_dev *pdev, u64 dma_mask)
  750. {
  751. struct pci_dn *pdn = pci_get_pdn(pdev);
  752. struct pnv_ioda_pe *pe;
  753. uint64_t top;
  754. bool bypass = false;
  755. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  756. return -ENODEV;;
  757. pe = &phb->ioda.pe_array[pdn->pe_number];
  758. if (pe->tce_bypass_enabled) {
  759. top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
  760. bypass = (dma_mask >= top);
  761. }
  762. if (bypass) {
  763. dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
  764. set_dma_ops(&pdev->dev, &dma_direct_ops);
  765. set_dma_offset(&pdev->dev, pe->tce_bypass_base);
  766. } else {
  767. dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
  768. set_dma_ops(&pdev->dev, &dma_iommu_ops);
  769. set_iommu_table_base(&pdev->dev, &pe->tce32_table);
  770. }
  771. *pdev->dev.dma_mask = dma_mask;
  772. return 0;
  773. }
  774. static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
  775. struct pci_bus *bus,
  776. bool add_to_iommu_group)
  777. {
  778. struct pci_dev *dev;
  779. list_for_each_entry(dev, &bus->devices, bus_list) {
  780. if (add_to_iommu_group)
  781. set_iommu_table_base_and_group(&dev->dev,
  782. &pe->tce32_table);
  783. else
  784. set_iommu_table_base(&dev->dev, &pe->tce32_table);
  785. if (dev->subordinate)
  786. pnv_ioda_setup_bus_dma(pe, dev->subordinate,
  787. add_to_iommu_group);
  788. }
  789. }
  790. static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
  791. struct iommu_table *tbl,
  792. __be64 *startp, __be64 *endp, bool rm)
  793. {
  794. __be64 __iomem *invalidate = rm ?
  795. (__be64 __iomem *)pe->tce_inval_reg_phys :
  796. (__be64 __iomem *)tbl->it_index;
  797. unsigned long start, end, inc;
  798. const unsigned shift = tbl->it_page_shift;
  799. start = __pa(startp);
  800. end = __pa(endp);
  801. /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
  802. if (tbl->it_busno) {
  803. start <<= shift;
  804. end <<= shift;
  805. inc = 128ull << shift;
  806. start |= tbl->it_busno;
  807. end |= tbl->it_busno;
  808. } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
  809. /* p7ioc-style invalidation, 2 TCEs per write */
  810. start |= (1ull << 63);
  811. end |= (1ull << 63);
  812. inc = 16;
  813. } else {
  814. /* Default (older HW) */
  815. inc = 128;
  816. }
  817. end |= inc - 1; /* round up end to be different than start */
  818. mb(); /* Ensure above stores are visible */
  819. while (start <= end) {
  820. if (rm)
  821. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  822. else
  823. __raw_writeq(cpu_to_be64(start), invalidate);
  824. start += inc;
  825. }
  826. /*
  827. * The iommu layer will do another mb() for us on build()
  828. * and we don't care on free()
  829. */
  830. }
  831. static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
  832. struct iommu_table *tbl,
  833. __be64 *startp, __be64 *endp, bool rm)
  834. {
  835. unsigned long start, end, inc;
  836. __be64 __iomem *invalidate = rm ?
  837. (__be64 __iomem *)pe->tce_inval_reg_phys :
  838. (__be64 __iomem *)tbl->it_index;
  839. const unsigned shift = tbl->it_page_shift;
  840. /* We'll invalidate DMA address in PE scope */
  841. start = 0x2ull << 60;
  842. start |= (pe->pe_number & 0xFF);
  843. end = start;
  844. /* Figure out the start, end and step */
  845. inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
  846. start |= (inc << shift);
  847. inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
  848. end |= (inc << shift);
  849. inc = (0x1ull << shift);
  850. mb();
  851. while (start <= end) {
  852. if (rm)
  853. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  854. else
  855. __raw_writeq(cpu_to_be64(start), invalidate);
  856. start += inc;
  857. }
  858. }
  859. void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
  860. __be64 *startp, __be64 *endp, bool rm)
  861. {
  862. struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
  863. tce32_table);
  864. struct pnv_phb *phb = pe->phb;
  865. if (phb->type == PNV_PHB_IODA1)
  866. pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
  867. else
  868. pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
  869. }
  870. static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
  871. struct pnv_ioda_pe *pe, unsigned int base,
  872. unsigned int segs)
  873. {
  874. struct page *tce_mem = NULL;
  875. const __be64 *swinvp;
  876. struct iommu_table *tbl;
  877. unsigned int i;
  878. int64_t rc;
  879. void *addr;
  880. /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
  881. #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
  882. /* XXX FIXME: Handle 64-bit only DMA devices */
  883. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  884. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  885. /* We shouldn't already have a 32-bit DMA associated */
  886. if (WARN_ON(pe->tce32_seg >= 0))
  887. return;
  888. /* Grab a 32-bit TCE table */
  889. pe->tce32_seg = base;
  890. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  891. (base << 28), ((base + segs) << 28) - 1);
  892. /* XXX Currently, we allocate one big contiguous table for the
  893. * TCEs. We only really need one chunk per 256M of TCE space
  894. * (ie per segment) but that's an optimization for later, it
  895. * requires some added smarts with our get/put_tce implementation
  896. */
  897. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  898. get_order(TCE32_TABLE_SIZE * segs));
  899. if (!tce_mem) {
  900. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  901. goto fail;
  902. }
  903. addr = page_address(tce_mem);
  904. memset(addr, 0, TCE32_TABLE_SIZE * segs);
  905. /* Configure HW */
  906. for (i = 0; i < segs; i++) {
  907. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  908. pe->pe_number,
  909. base + i, 1,
  910. __pa(addr) + TCE32_TABLE_SIZE * i,
  911. TCE32_TABLE_SIZE, 0x1000);
  912. if (rc) {
  913. pe_err(pe, " Failed to configure 32-bit TCE table,"
  914. " err %ld\n", rc);
  915. goto fail;
  916. }
  917. }
  918. /* Setup linux iommu table */
  919. tbl = &pe->tce32_table;
  920. pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
  921. base << 28, IOMMU_PAGE_SHIFT_4K);
  922. /* OPAL variant of P7IOC SW invalidated TCEs */
  923. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  924. if (swinvp) {
  925. /* We need a couple more fields -- an address and a data
  926. * to or. Since the bus is only printed out on table free
  927. * errors, and on the first pass the data will be a relative
  928. * bus number, print that out instead.
  929. */
  930. pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
  931. tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
  932. 8);
  933. tbl->it_type |= (TCE_PCI_SWINV_CREATE |
  934. TCE_PCI_SWINV_FREE |
  935. TCE_PCI_SWINV_PAIR);
  936. }
  937. iommu_init_table(tbl, phb->hose->node);
  938. iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
  939. if (pe->pdev)
  940. set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
  941. else
  942. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  943. return;
  944. fail:
  945. /* XXX Failure: Try to fallback to 64-bit only ? */
  946. if (pe->tce32_seg >= 0)
  947. pe->tce32_seg = -1;
  948. if (tce_mem)
  949. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  950. }
  951. static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
  952. {
  953. struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
  954. tce32_table);
  955. uint16_t window_id = (pe->pe_number << 1 ) + 1;
  956. int64_t rc;
  957. pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
  958. if (enable) {
  959. phys_addr_t top = memblock_end_of_DRAM();
  960. top = roundup_pow_of_two(top);
  961. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  962. pe->pe_number,
  963. window_id,
  964. pe->tce_bypass_base,
  965. top);
  966. } else {
  967. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  968. pe->pe_number,
  969. window_id,
  970. pe->tce_bypass_base,
  971. 0);
  972. /*
  973. * EEH needs the mapping between IOMMU table and group
  974. * of those VFIO/KVM pass-through devices. We can postpone
  975. * resetting DMA ops until the DMA mask is configured in
  976. * host side.
  977. */
  978. if (pe->pdev)
  979. set_iommu_table_base(&pe->pdev->dev, tbl);
  980. else
  981. pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
  982. }
  983. if (rc)
  984. pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
  985. else
  986. pe->tce_bypass_enabled = enable;
  987. }
  988. static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
  989. struct pnv_ioda_pe *pe)
  990. {
  991. /* TVE #1 is selected by PCI address bit 59 */
  992. pe->tce_bypass_base = 1ull << 59;
  993. /* Install set_bypass callback for VFIO */
  994. pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
  995. /* Enable bypass by default */
  996. pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
  997. }
  998. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  999. struct pnv_ioda_pe *pe)
  1000. {
  1001. struct page *tce_mem = NULL;
  1002. void *addr;
  1003. const __be64 *swinvp;
  1004. struct iommu_table *tbl;
  1005. unsigned int tce_table_size, end;
  1006. int64_t rc;
  1007. /* We shouldn't already have a 32-bit DMA associated */
  1008. if (WARN_ON(pe->tce32_seg >= 0))
  1009. return;
  1010. /* The PE will reserve all possible 32-bits space */
  1011. pe->tce32_seg = 0;
  1012. end = (1 << ilog2(phb->ioda.m32_pci_base));
  1013. tce_table_size = (end / 0x1000) * 8;
  1014. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  1015. end);
  1016. /* Allocate TCE table */
  1017. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  1018. get_order(tce_table_size));
  1019. if (!tce_mem) {
  1020. pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
  1021. goto fail;
  1022. }
  1023. addr = page_address(tce_mem);
  1024. memset(addr, 0, tce_table_size);
  1025. /*
  1026. * Map TCE table through TVT. The TVE index is the PE number
  1027. * shifted by 1 bit for 32-bits DMA space.
  1028. */
  1029. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  1030. pe->pe_number << 1, 1, __pa(addr),
  1031. tce_table_size, 0x1000);
  1032. if (rc) {
  1033. pe_err(pe, "Failed to configure 32-bit TCE table,"
  1034. " err %ld\n", rc);
  1035. goto fail;
  1036. }
  1037. /* Setup linux iommu table */
  1038. tbl = &pe->tce32_table;
  1039. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
  1040. IOMMU_PAGE_SHIFT_4K);
  1041. /* OPAL variant of PHB3 invalidated TCEs */
  1042. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  1043. if (swinvp) {
  1044. /* We need a couple more fields -- an address and a data
  1045. * to or. Since the bus is only printed out on table free
  1046. * errors, and on the first pass the data will be a relative
  1047. * bus number, print that out instead.
  1048. */
  1049. pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
  1050. tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
  1051. 8);
  1052. tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
  1053. }
  1054. iommu_init_table(tbl, phb->hose->node);
  1055. iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
  1056. if (pe->pdev)
  1057. set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
  1058. else
  1059. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  1060. /* Also create a bypass window */
  1061. pnv_pci_ioda2_setup_bypass_pe(phb, pe);
  1062. return;
  1063. fail:
  1064. if (pe->tce32_seg >= 0)
  1065. pe->tce32_seg = -1;
  1066. if (tce_mem)
  1067. __free_pages(tce_mem, get_order(tce_table_size));
  1068. }
  1069. static void pnv_ioda_setup_dma(struct pnv_phb *phb)
  1070. {
  1071. struct pci_controller *hose = phb->hose;
  1072. unsigned int residual, remaining, segs, tw, base;
  1073. struct pnv_ioda_pe *pe;
  1074. /* If we have more PE# than segments available, hand out one
  1075. * per PE until we run out and let the rest fail. If not,
  1076. * then we assign at least one segment per PE, plus more based
  1077. * on the amount of devices under that PE
  1078. */
  1079. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  1080. residual = 0;
  1081. else
  1082. residual = phb->ioda.tce32_count -
  1083. phb->ioda.dma_pe_count;
  1084. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  1085. hose->global_number, phb->ioda.tce32_count);
  1086. pr_info("PCI: %d PE# for a total weight of %d\n",
  1087. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  1088. /* Walk our PE list and configure their DMA segments, hand them
  1089. * out one base segment plus any residual segments based on
  1090. * weight
  1091. */
  1092. remaining = phb->ioda.tce32_count;
  1093. tw = phb->ioda.dma_weight;
  1094. base = 0;
  1095. list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
  1096. if (!pe->dma_weight)
  1097. continue;
  1098. if (!remaining) {
  1099. pe_warn(pe, "No DMA32 resources available\n");
  1100. continue;
  1101. }
  1102. segs = 1;
  1103. if (residual) {
  1104. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  1105. if (segs > remaining)
  1106. segs = remaining;
  1107. }
  1108. /*
  1109. * For IODA2 compliant PHB3, we needn't care about the weight.
  1110. * The all available 32-bits DMA space will be assigned to
  1111. * the specific PE.
  1112. */
  1113. if (phb->type == PNV_PHB_IODA1) {
  1114. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  1115. pe->dma_weight, segs);
  1116. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  1117. } else {
  1118. pe_info(pe, "Assign DMA32 space\n");
  1119. segs = 0;
  1120. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  1121. }
  1122. remaining -= segs;
  1123. base += segs;
  1124. }
  1125. }
  1126. #ifdef CONFIG_PCI_MSI
  1127. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  1128. {
  1129. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1130. struct irq_chip *chip = irq_data_get_irq_chip(d);
  1131. struct pnv_phb *phb = container_of(chip, struct pnv_phb,
  1132. ioda.irq_chip);
  1133. int64_t rc;
  1134. rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
  1135. WARN_ON_ONCE(rc);
  1136. icp_native_eoi(d);
  1137. }
  1138. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  1139. unsigned int hwirq, unsigned int virq,
  1140. unsigned int is_64, struct msi_msg *msg)
  1141. {
  1142. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  1143. struct pci_dn *pdn = pci_get_pdn(dev);
  1144. struct irq_data *idata;
  1145. struct irq_chip *ichip;
  1146. unsigned int xive_num = hwirq - phb->msi_base;
  1147. __be32 data;
  1148. int rc;
  1149. /* No PE assigned ? bail out ... no MSI for you ! */
  1150. if (pe == NULL)
  1151. return -ENXIO;
  1152. /* Check if we have an MVE */
  1153. if (pe->mve_number < 0)
  1154. return -ENXIO;
  1155. /* Force 32-bit MSI on some broken devices */
  1156. if (pdn && pdn->force_32bit_msi)
  1157. is_64 = 0;
  1158. /* Assign XIVE to PE */
  1159. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  1160. if (rc) {
  1161. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  1162. pci_name(dev), rc, xive_num);
  1163. return -EIO;
  1164. }
  1165. if (is_64) {
  1166. __be64 addr64;
  1167. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  1168. &addr64, &data);
  1169. if (rc) {
  1170. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  1171. pci_name(dev), rc);
  1172. return -EIO;
  1173. }
  1174. msg->address_hi = be64_to_cpu(addr64) >> 32;
  1175. msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
  1176. } else {
  1177. __be32 addr32;
  1178. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  1179. &addr32, &data);
  1180. if (rc) {
  1181. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  1182. pci_name(dev), rc);
  1183. return -EIO;
  1184. }
  1185. msg->address_hi = 0;
  1186. msg->address_lo = be32_to_cpu(addr32);
  1187. }
  1188. msg->data = be32_to_cpu(data);
  1189. /*
  1190. * Change the IRQ chip for the MSI interrupts on PHB3.
  1191. * The corresponding IRQ chip should be populated for
  1192. * the first time.
  1193. */
  1194. if (phb->type == PNV_PHB_IODA2) {
  1195. if (!phb->ioda.irq_chip_init) {
  1196. idata = irq_get_irq_data(virq);
  1197. ichip = irq_data_get_irq_chip(idata);
  1198. phb->ioda.irq_chip_init = 1;
  1199. phb->ioda.irq_chip = *ichip;
  1200. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  1201. }
  1202. irq_set_chip(virq, &phb->ioda.irq_chip);
  1203. }
  1204. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  1205. " address=%x_%08x data=%x PE# %d\n",
  1206. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  1207. msg->address_hi, msg->address_lo, data, pe->pe_number);
  1208. return 0;
  1209. }
  1210. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  1211. {
  1212. unsigned int count;
  1213. const __be32 *prop = of_get_property(phb->hose->dn,
  1214. "ibm,opal-msi-ranges", NULL);
  1215. if (!prop) {
  1216. /* BML Fallback */
  1217. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  1218. }
  1219. if (!prop)
  1220. return;
  1221. phb->msi_base = be32_to_cpup(prop);
  1222. count = be32_to_cpup(prop + 1);
  1223. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  1224. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  1225. phb->hose->global_number);
  1226. return;
  1227. }
  1228. phb->msi_setup = pnv_pci_ioda_msi_setup;
  1229. phb->msi32_support = 1;
  1230. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  1231. count, phb->msi_base);
  1232. }
  1233. #else
  1234. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  1235. #endif /* CONFIG_PCI_MSI */
  1236. /*
  1237. * This function is supposed to be called on basis of PE from top
  1238. * to bottom style. So the the I/O or MMIO segment assigned to
  1239. * parent PE could be overrided by its child PEs if necessary.
  1240. */
  1241. static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
  1242. struct pnv_ioda_pe *pe)
  1243. {
  1244. struct pnv_phb *phb = hose->private_data;
  1245. struct pci_bus_region region;
  1246. struct resource *res;
  1247. int i, index;
  1248. int rc;
  1249. /*
  1250. * NOTE: We only care PCI bus based PE for now. For PCI
  1251. * device based PE, for example SRIOV sensitive VF should
  1252. * be figured out later.
  1253. */
  1254. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  1255. pci_bus_for_each_resource(pe->pbus, res, i) {
  1256. if (!res || !res->flags ||
  1257. res->start > res->end)
  1258. continue;
  1259. if (res->flags & IORESOURCE_IO) {
  1260. region.start = res->start - phb->ioda.io_pci_base;
  1261. region.end = res->end - phb->ioda.io_pci_base;
  1262. index = region.start / phb->ioda.io_segsize;
  1263. while (index < phb->ioda.total_pe &&
  1264. region.start <= region.end) {
  1265. phb->ioda.io_segmap[index] = pe->pe_number;
  1266. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  1267. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  1268. if (rc != OPAL_SUCCESS) {
  1269. pr_err("%s: OPAL error %d when mapping IO "
  1270. "segment #%d to PE#%d\n",
  1271. __func__, rc, index, pe->pe_number);
  1272. break;
  1273. }
  1274. region.start += phb->ioda.io_segsize;
  1275. index++;
  1276. }
  1277. } else if (res->flags & IORESOURCE_MEM) {
  1278. region.start = res->start -
  1279. hose->mem_offset[0] -
  1280. phb->ioda.m32_pci_base;
  1281. region.end = res->end -
  1282. hose->mem_offset[0] -
  1283. phb->ioda.m32_pci_base;
  1284. index = region.start / phb->ioda.m32_segsize;
  1285. while (index < phb->ioda.total_pe &&
  1286. region.start <= region.end) {
  1287. phb->ioda.m32_segmap[index] = pe->pe_number;
  1288. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  1289. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  1290. if (rc != OPAL_SUCCESS) {
  1291. pr_err("%s: OPAL error %d when mapping M32 "
  1292. "segment#%d to PE#%d",
  1293. __func__, rc, index, pe->pe_number);
  1294. break;
  1295. }
  1296. region.start += phb->ioda.m32_segsize;
  1297. index++;
  1298. }
  1299. }
  1300. }
  1301. }
  1302. static void pnv_pci_ioda_setup_seg(void)
  1303. {
  1304. struct pci_controller *tmp, *hose;
  1305. struct pnv_phb *phb;
  1306. struct pnv_ioda_pe *pe;
  1307. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1308. phb = hose->private_data;
  1309. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  1310. pnv_ioda_setup_pe_seg(hose, pe);
  1311. }
  1312. }
  1313. }
  1314. static void pnv_pci_ioda_setup_DMA(void)
  1315. {
  1316. struct pci_controller *hose, *tmp;
  1317. struct pnv_phb *phb;
  1318. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1319. pnv_ioda_setup_dma(hose->private_data);
  1320. /* Mark the PHB initialization done */
  1321. phb = hose->private_data;
  1322. phb->initialized = 1;
  1323. }
  1324. }
  1325. static void pnv_pci_ioda_create_dbgfs(void)
  1326. {
  1327. #ifdef CONFIG_DEBUG_FS
  1328. struct pci_controller *hose, *tmp;
  1329. struct pnv_phb *phb;
  1330. char name[16];
  1331. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1332. phb = hose->private_data;
  1333. sprintf(name, "PCI%04x", hose->global_number);
  1334. phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
  1335. if (!phb->dbgfs)
  1336. pr_warning("%s: Error on creating debugfs on PHB#%x\n",
  1337. __func__, hose->global_number);
  1338. }
  1339. #endif /* CONFIG_DEBUG_FS */
  1340. }
  1341. static void pnv_pci_ioda_fixup(void)
  1342. {
  1343. pnv_pci_ioda_setup_PEs();
  1344. pnv_pci_ioda_setup_seg();
  1345. pnv_pci_ioda_setup_DMA();
  1346. pnv_pci_ioda_create_dbgfs();
  1347. #ifdef CONFIG_EEH
  1348. eeh_init();
  1349. eeh_addr_cache_build();
  1350. #endif
  1351. }
  1352. /*
  1353. * Returns the alignment for I/O or memory windows for P2P
  1354. * bridges. That actually depends on how PEs are segmented.
  1355. * For now, we return I/O or M32 segment size for PE sensitive
  1356. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  1357. * 1MiB for memory) will be returned.
  1358. *
  1359. * The current PCI bus might be put into one PE, which was
  1360. * create against the parent PCI bridge. For that case, we
  1361. * needn't enlarge the alignment so that we can save some
  1362. * resources.
  1363. */
  1364. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  1365. unsigned long type)
  1366. {
  1367. struct pci_dev *bridge;
  1368. struct pci_controller *hose = pci_bus_to_host(bus);
  1369. struct pnv_phb *phb = hose->private_data;
  1370. int num_pci_bridges = 0;
  1371. bridge = bus->self;
  1372. while (bridge) {
  1373. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  1374. num_pci_bridges++;
  1375. if (num_pci_bridges >= 2)
  1376. return 1;
  1377. }
  1378. bridge = bridge->bus->self;
  1379. }
  1380. /* We fail back to M32 if M64 isn't supported */
  1381. if (phb->ioda.m64_segsize &&
  1382. pnv_pci_is_mem_pref_64(type))
  1383. return phb->ioda.m64_segsize;
  1384. if (type & IORESOURCE_MEM)
  1385. return phb->ioda.m32_segsize;
  1386. return phb->ioda.io_segsize;
  1387. }
  1388. /* Prevent enabling devices for which we couldn't properly
  1389. * assign a PE
  1390. */
  1391. static int pnv_pci_enable_device_hook(struct pci_dev *dev)
  1392. {
  1393. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1394. struct pnv_phb *phb = hose->private_data;
  1395. struct pci_dn *pdn;
  1396. /* The function is probably called while the PEs have
  1397. * not be created yet. For example, resource reassignment
  1398. * during PCI probe period. We just skip the check if
  1399. * PEs isn't ready.
  1400. */
  1401. if (!phb->initialized)
  1402. return 0;
  1403. pdn = pci_get_pdn(dev);
  1404. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  1405. return -EINVAL;
  1406. return 0;
  1407. }
  1408. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  1409. u32 devfn)
  1410. {
  1411. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  1412. }
  1413. static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
  1414. {
  1415. opal_pci_reset(phb->opal_id, OPAL_PCI_IODA_TABLE_RESET,
  1416. OPAL_ASSERT_RESET);
  1417. }
  1418. void __init pnv_pci_init_ioda_phb(struct device_node *np,
  1419. u64 hub_id, int ioda_type)
  1420. {
  1421. struct pci_controller *hose;
  1422. struct pnv_phb *phb;
  1423. unsigned long size, m32map_off, pemap_off, iomap_off = 0;
  1424. const __be64 *prop64;
  1425. const __be32 *prop32;
  1426. int len;
  1427. u64 phb_id;
  1428. void *aux;
  1429. long rc;
  1430. pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
  1431. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  1432. if (!prop64) {
  1433. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  1434. return;
  1435. }
  1436. phb_id = be64_to_cpup(prop64);
  1437. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  1438. phb = alloc_bootmem(sizeof(struct pnv_phb));
  1439. if (!phb) {
  1440. pr_err(" Out of memory !\n");
  1441. return;
  1442. }
  1443. /* Allocate PCI controller */
  1444. memset(phb, 0, sizeof(struct pnv_phb));
  1445. phb->hose = hose = pcibios_alloc_controller(np);
  1446. if (!phb->hose) {
  1447. pr_err(" Can't allocate PCI controller for %s\n",
  1448. np->full_name);
  1449. free_bootmem((unsigned long)phb, sizeof(struct pnv_phb));
  1450. return;
  1451. }
  1452. spin_lock_init(&phb->lock);
  1453. prop32 = of_get_property(np, "bus-range", &len);
  1454. if (prop32 && len == 8) {
  1455. hose->first_busno = be32_to_cpu(prop32[0]);
  1456. hose->last_busno = be32_to_cpu(prop32[1]);
  1457. } else {
  1458. pr_warn(" Broken <bus-range> on %s\n", np->full_name);
  1459. hose->first_busno = 0;
  1460. hose->last_busno = 0xff;
  1461. }
  1462. hose->private_data = phb;
  1463. phb->hub_id = hub_id;
  1464. phb->opal_id = phb_id;
  1465. phb->type = ioda_type;
  1466. /* Detect specific models for error handling */
  1467. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  1468. phb->model = PNV_PHB_MODEL_P7IOC;
  1469. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  1470. phb->model = PNV_PHB_MODEL_PHB3;
  1471. else
  1472. phb->model = PNV_PHB_MODEL_UNKNOWN;
  1473. /* Parse 32-bit and IO ranges (if any) */
  1474. pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
  1475. /* Get registers */
  1476. phb->regs = of_iomap(np, 0);
  1477. if (phb->regs == NULL)
  1478. pr_err(" Failed to map registers !\n");
  1479. /* Initialize more IODA stuff */
  1480. phb->ioda.total_pe = 1;
  1481. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  1482. if (prop32)
  1483. phb->ioda.total_pe = be32_to_cpup(prop32);
  1484. prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
  1485. if (prop32)
  1486. phb->ioda.reserved_pe = be32_to_cpup(prop32);
  1487. /* Parse 64-bit MMIO range */
  1488. pnv_ioda_parse_m64_window(phb);
  1489. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  1490. /* FW Has already off top 64k of M32 space (MSI space) */
  1491. phb->ioda.m32_size += 0x10000;
  1492. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  1493. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  1494. phb->ioda.io_size = hose->pci_io_size;
  1495. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  1496. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  1497. /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
  1498. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  1499. m32map_off = size;
  1500. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  1501. if (phb->type == PNV_PHB_IODA1) {
  1502. iomap_off = size;
  1503. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  1504. }
  1505. pemap_off = size;
  1506. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  1507. aux = alloc_bootmem(size);
  1508. memset(aux, 0, size);
  1509. phb->ioda.pe_alloc = aux;
  1510. phb->ioda.m32_segmap = aux + m32map_off;
  1511. if (phb->type == PNV_PHB_IODA1)
  1512. phb->ioda.io_segmap = aux + iomap_off;
  1513. phb->ioda.pe_array = aux + pemap_off;
  1514. set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
  1515. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  1516. INIT_LIST_HEAD(&phb->ioda.pe_list);
  1517. /* Calculate how many 32-bit TCE segments we have */
  1518. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  1519. #if 0 /* We should really do that ... */
  1520. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  1521. window_type,
  1522. window_num,
  1523. starting_real_address,
  1524. starting_pci_address,
  1525. segment_size);
  1526. #endif
  1527. pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
  1528. phb->ioda.total_pe, phb->ioda.reserved_pe,
  1529. phb->ioda.m32_size, phb->ioda.m32_segsize);
  1530. if (phb->ioda.m64_size)
  1531. pr_info(" M64: 0x%lx [segment=0x%lx]\n",
  1532. phb->ioda.m64_size, phb->ioda.m64_segsize);
  1533. if (phb->ioda.io_size)
  1534. pr_info(" IO: 0x%x [segment=0x%x]\n",
  1535. phb->ioda.io_size, phb->ioda.io_segsize);
  1536. phb->hose->ops = &pnv_pci_ops;
  1537. phb->get_pe_state = pnv_ioda_get_pe_state;
  1538. phb->freeze_pe = pnv_ioda_freeze_pe;
  1539. phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
  1540. #ifdef CONFIG_EEH
  1541. phb->eeh_ops = &ioda_eeh_ops;
  1542. #endif
  1543. /* Setup RID -> PE mapping function */
  1544. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  1545. /* Setup TCEs */
  1546. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  1547. phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
  1548. /* Setup shutdown function for kexec */
  1549. phb->shutdown = pnv_pci_ioda_shutdown;
  1550. /* Setup MSI support */
  1551. pnv_pci_init_ioda_msis(phb);
  1552. /*
  1553. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  1554. * to let the PCI core do resource assignment. It's supposed
  1555. * that the PCI core will do correct I/O and MMIO alignment
  1556. * for the P2P bridge bars so that each PCI bus (excluding
  1557. * the child P2P bridges) can form individual PE.
  1558. */
  1559. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  1560. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  1561. ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
  1562. ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
  1563. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  1564. /* Reset IODA tables to a clean state */
  1565. rc = opal_pci_reset(phb_id, OPAL_PCI_IODA_TABLE_RESET, OPAL_ASSERT_RESET);
  1566. if (rc)
  1567. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  1568. /* If we're running in kdump kerenl, the previous kerenl never
  1569. * shutdown PCI devices correctly. We already got IODA table
  1570. * cleaned out. So we have to issue PHB reset to stop all PCI
  1571. * transactions from previous kerenl.
  1572. */
  1573. if (is_kdump_kernel()) {
  1574. pr_info(" Issue PHB reset ...\n");
  1575. ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
  1576. ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET);
  1577. }
  1578. /* Configure M64 window */
  1579. if (phb->init_m64 && phb->init_m64(phb))
  1580. hose->mem_resources[1].flags = 0;
  1581. }
  1582. void __init pnv_pci_init_ioda2_phb(struct device_node *np)
  1583. {
  1584. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
  1585. }
  1586. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  1587. {
  1588. struct device_node *phbn;
  1589. const __be64 *prop64;
  1590. u64 hub_id;
  1591. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  1592. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  1593. if (!prop64) {
  1594. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  1595. return;
  1596. }
  1597. hub_id = be64_to_cpup(prop64);
  1598. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  1599. /* Count child PHBs */
  1600. for_each_child_of_node(np, phbn) {
  1601. /* Look for IODA1 PHBs */
  1602. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  1603. pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
  1604. }
  1605. }