pci-ioda.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871
  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 <asm/pnv-pci.h>
  38. #include <misc/cxl.h>
  39. #include "powernv.h"
  40. #include "pci.h"
  41. /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
  42. #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
  43. static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
  44. const char *fmt, ...)
  45. {
  46. struct va_format vaf;
  47. va_list args;
  48. char pfix[32];
  49. va_start(args, fmt);
  50. vaf.fmt = fmt;
  51. vaf.va = &args;
  52. if (pe->flags & PNV_IODA_PE_DEV)
  53. strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
  54. else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
  55. sprintf(pfix, "%04x:%02x ",
  56. pci_domain_nr(pe->pbus), pe->pbus->number);
  57. #ifdef CONFIG_PCI_IOV
  58. else if (pe->flags & PNV_IODA_PE_VF)
  59. sprintf(pfix, "%04x:%02x:%2x.%d",
  60. pci_domain_nr(pe->parent_dev->bus),
  61. (pe->rid & 0xff00) >> 8,
  62. PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
  63. #endif /* CONFIG_PCI_IOV*/
  64. printk("%spci %s: [PE# %.3d] %pV",
  65. level, pfix, pe->pe_number, &vaf);
  66. va_end(args);
  67. }
  68. #define pe_err(pe, fmt, ...) \
  69. pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
  70. #define pe_warn(pe, fmt, ...) \
  71. pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
  72. #define pe_info(pe, fmt, ...) \
  73. pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
  74. static bool pnv_iommu_bypass_disabled __read_mostly;
  75. static int __init iommu_setup(char *str)
  76. {
  77. if (!str)
  78. return -EINVAL;
  79. while (*str) {
  80. if (!strncmp(str, "nobypass", 8)) {
  81. pnv_iommu_bypass_disabled = true;
  82. pr_info("PowerNV: IOMMU bypass window disabled.\n");
  83. break;
  84. }
  85. str += strcspn(str, ",");
  86. if (*str == ',')
  87. str++;
  88. }
  89. return 0;
  90. }
  91. early_param("iommu", iommu_setup);
  92. /*
  93. * stdcix is only supposed to be used in hypervisor real mode as per
  94. * the architecture spec
  95. */
  96. static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
  97. {
  98. __asm__ __volatile__("stdcix %0,0,%1"
  99. : : "r" (val), "r" (paddr) : "memory");
  100. }
  101. static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
  102. {
  103. return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
  104. (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
  105. }
  106. static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
  107. {
  108. if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe)) {
  109. pr_warn("%s: Invalid PE %d on PHB#%x\n",
  110. __func__, pe_no, phb->hose->global_number);
  111. return;
  112. }
  113. if (test_and_set_bit(pe_no, phb->ioda.pe_alloc)) {
  114. pr_warn("%s: PE %d was assigned on PHB#%x\n",
  115. __func__, pe_no, phb->hose->global_number);
  116. return;
  117. }
  118. phb->ioda.pe_array[pe_no].phb = phb;
  119. phb->ioda.pe_array[pe_no].pe_number = pe_no;
  120. }
  121. static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
  122. {
  123. unsigned long pe;
  124. do {
  125. pe = find_next_zero_bit(phb->ioda.pe_alloc,
  126. phb->ioda.total_pe, 0);
  127. if (pe >= phb->ioda.total_pe)
  128. return IODA_INVALID_PE;
  129. } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
  130. phb->ioda.pe_array[pe].phb = phb;
  131. phb->ioda.pe_array[pe].pe_number = pe;
  132. return pe;
  133. }
  134. static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
  135. {
  136. WARN_ON(phb->ioda.pe_array[pe].pdev);
  137. memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
  138. clear_bit(pe, phb->ioda.pe_alloc);
  139. }
  140. /* The default M64 BAR is shared by all PEs */
  141. static int pnv_ioda2_init_m64(struct pnv_phb *phb)
  142. {
  143. const char *desc;
  144. struct resource *r;
  145. s64 rc;
  146. /* Configure the default M64 BAR */
  147. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  148. OPAL_M64_WINDOW_TYPE,
  149. phb->ioda.m64_bar_idx,
  150. phb->ioda.m64_base,
  151. 0, /* unused */
  152. phb->ioda.m64_size);
  153. if (rc != OPAL_SUCCESS) {
  154. desc = "configuring";
  155. goto fail;
  156. }
  157. /* Enable the default M64 BAR */
  158. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  159. OPAL_M64_WINDOW_TYPE,
  160. phb->ioda.m64_bar_idx,
  161. OPAL_ENABLE_M64_SPLIT);
  162. if (rc != OPAL_SUCCESS) {
  163. desc = "enabling";
  164. goto fail;
  165. }
  166. /* Mark the M64 BAR assigned */
  167. set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
  168. /*
  169. * Strip off the segment used by the reserved PE, which is
  170. * expected to be 0 or last one of PE capabicity.
  171. */
  172. r = &phb->hose->mem_resources[1];
  173. if (phb->ioda.reserved_pe == 0)
  174. r->start += phb->ioda.m64_segsize;
  175. else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
  176. r->end -= phb->ioda.m64_segsize;
  177. else
  178. pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
  179. phb->ioda.reserved_pe);
  180. return 0;
  181. fail:
  182. pr_warn(" Failure %lld %s M64 BAR#%d\n",
  183. rc, desc, phb->ioda.m64_bar_idx);
  184. opal_pci_phb_mmio_enable(phb->opal_id,
  185. OPAL_M64_WINDOW_TYPE,
  186. phb->ioda.m64_bar_idx,
  187. OPAL_DISABLE_M64);
  188. return -EIO;
  189. }
  190. static void pnv_ioda2_reserve_m64_pe(struct pnv_phb *phb)
  191. {
  192. resource_size_t sgsz = phb->ioda.m64_segsize;
  193. struct pci_dev *pdev;
  194. struct resource *r;
  195. int base, step, i;
  196. /*
  197. * Root bus always has full M64 range and root port has
  198. * M64 range used in reality. So we're checking root port
  199. * instead of root bus.
  200. */
  201. list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
  202. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  203. r = &pdev->resource[PCI_BRIDGE_RESOURCES + i];
  204. if (!r->parent ||
  205. !pnv_pci_is_mem_pref_64(r->flags))
  206. continue;
  207. base = (r->start - phb->ioda.m64_base) / sgsz;
  208. for (step = 0; step < resource_size(r) / sgsz; step++)
  209. pnv_ioda_reserve_pe(phb, base + step);
  210. }
  211. }
  212. }
  213. static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
  214. struct pci_bus *bus, int all)
  215. {
  216. resource_size_t segsz = phb->ioda.m64_segsize;
  217. struct pci_dev *pdev;
  218. struct resource *r;
  219. struct pnv_ioda_pe *master_pe, *pe;
  220. unsigned long size, *pe_alloc;
  221. bool found;
  222. int start, i, j;
  223. /* Root bus shouldn't use M64 */
  224. if (pci_is_root_bus(bus))
  225. return IODA_INVALID_PE;
  226. /* We support only one M64 window on each bus */
  227. found = false;
  228. pci_bus_for_each_resource(bus, r, i) {
  229. if (r && r->parent &&
  230. pnv_pci_is_mem_pref_64(r->flags)) {
  231. found = true;
  232. break;
  233. }
  234. }
  235. /* No M64 window found ? */
  236. if (!found)
  237. return IODA_INVALID_PE;
  238. /* Allocate bitmap */
  239. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  240. pe_alloc = kzalloc(size, GFP_KERNEL);
  241. if (!pe_alloc) {
  242. pr_warn("%s: Out of memory !\n",
  243. __func__);
  244. return IODA_INVALID_PE;
  245. }
  246. /*
  247. * Figure out reserved PE numbers by the PE
  248. * the its child PEs.
  249. */
  250. start = (r->start - phb->ioda.m64_base) / segsz;
  251. for (i = 0; i < resource_size(r) / segsz; i++)
  252. set_bit(start + i, pe_alloc);
  253. if (all)
  254. goto done;
  255. /*
  256. * If the PE doesn't cover all subordinate buses,
  257. * we need subtract from reserved PEs for children.
  258. */
  259. list_for_each_entry(pdev, &bus->devices, bus_list) {
  260. if (!pdev->subordinate)
  261. continue;
  262. pci_bus_for_each_resource(pdev->subordinate, r, i) {
  263. if (!r || !r->parent ||
  264. !pnv_pci_is_mem_pref_64(r->flags))
  265. continue;
  266. start = (r->start - phb->ioda.m64_base) / segsz;
  267. for (j = 0; j < resource_size(r) / segsz ; j++)
  268. clear_bit(start + j, pe_alloc);
  269. }
  270. }
  271. /*
  272. * the current bus might not own M64 window and that's all
  273. * contributed by its child buses. For the case, we needn't
  274. * pick M64 dependent PE#.
  275. */
  276. if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
  277. kfree(pe_alloc);
  278. return IODA_INVALID_PE;
  279. }
  280. /*
  281. * Figure out the master PE and put all slave PEs to master
  282. * PE's list to form compound PE.
  283. */
  284. done:
  285. master_pe = NULL;
  286. i = -1;
  287. while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
  288. phb->ioda.total_pe) {
  289. pe = &phb->ioda.pe_array[i];
  290. if (!master_pe) {
  291. pe->flags |= PNV_IODA_PE_MASTER;
  292. INIT_LIST_HEAD(&pe->slaves);
  293. master_pe = pe;
  294. } else {
  295. pe->flags |= PNV_IODA_PE_SLAVE;
  296. pe->master = master_pe;
  297. list_add_tail(&pe->list, &master_pe->slaves);
  298. }
  299. }
  300. kfree(pe_alloc);
  301. return master_pe->pe_number;
  302. }
  303. static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
  304. {
  305. struct pci_controller *hose = phb->hose;
  306. struct device_node *dn = hose->dn;
  307. struct resource *res;
  308. const u32 *r;
  309. u64 pci_addr;
  310. /* FIXME: Support M64 for P7IOC */
  311. if (phb->type != PNV_PHB_IODA2) {
  312. pr_info(" Not support M64 window\n");
  313. return;
  314. }
  315. if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
  316. pr_info(" Firmware too old to support M64 window\n");
  317. return;
  318. }
  319. r = of_get_property(dn, "ibm,opal-m64-window", NULL);
  320. if (!r) {
  321. pr_info(" No <ibm,opal-m64-window> on %s\n",
  322. dn->full_name);
  323. return;
  324. }
  325. res = &hose->mem_resources[1];
  326. res->start = of_translate_address(dn, r + 2);
  327. res->end = res->start + of_read_number(r + 4, 2) - 1;
  328. res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
  329. pci_addr = of_read_number(r, 2);
  330. hose->mem_offset[1] = res->start - pci_addr;
  331. phb->ioda.m64_size = resource_size(res);
  332. phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
  333. phb->ioda.m64_base = pci_addr;
  334. pr_info(" MEM64 0x%016llx..0x%016llx -> 0x%016llx\n",
  335. res->start, res->end, pci_addr);
  336. /* Use last M64 BAR to cover M64 window */
  337. phb->ioda.m64_bar_idx = 15;
  338. phb->init_m64 = pnv_ioda2_init_m64;
  339. phb->reserve_m64_pe = pnv_ioda2_reserve_m64_pe;
  340. phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
  341. }
  342. static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
  343. {
  344. struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
  345. struct pnv_ioda_pe *slave;
  346. s64 rc;
  347. /* Fetch master PE */
  348. if (pe->flags & PNV_IODA_PE_SLAVE) {
  349. pe = pe->master;
  350. if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
  351. return;
  352. pe_no = pe->pe_number;
  353. }
  354. /* Freeze master PE */
  355. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  356. pe_no,
  357. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  358. if (rc != OPAL_SUCCESS) {
  359. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  360. __func__, rc, phb->hose->global_number, pe_no);
  361. return;
  362. }
  363. /* Freeze slave PEs */
  364. if (!(pe->flags & PNV_IODA_PE_MASTER))
  365. return;
  366. list_for_each_entry(slave, &pe->slaves, list) {
  367. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  368. slave->pe_number,
  369. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  370. if (rc != OPAL_SUCCESS)
  371. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  372. __func__, rc, phb->hose->global_number,
  373. slave->pe_number);
  374. }
  375. }
  376. static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
  377. {
  378. struct pnv_ioda_pe *pe, *slave;
  379. s64 rc;
  380. /* Find master PE */
  381. pe = &phb->ioda.pe_array[pe_no];
  382. if (pe->flags & PNV_IODA_PE_SLAVE) {
  383. pe = pe->master;
  384. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  385. pe_no = pe->pe_number;
  386. }
  387. /* Clear frozen state for master PE */
  388. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
  389. if (rc != OPAL_SUCCESS) {
  390. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  391. __func__, rc, opt, phb->hose->global_number, pe_no);
  392. return -EIO;
  393. }
  394. if (!(pe->flags & PNV_IODA_PE_MASTER))
  395. return 0;
  396. /* Clear frozen state for slave PEs */
  397. list_for_each_entry(slave, &pe->slaves, list) {
  398. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  399. slave->pe_number,
  400. opt);
  401. if (rc != OPAL_SUCCESS) {
  402. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  403. __func__, rc, opt, phb->hose->global_number,
  404. slave->pe_number);
  405. return -EIO;
  406. }
  407. }
  408. return 0;
  409. }
  410. static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
  411. {
  412. struct pnv_ioda_pe *slave, *pe;
  413. u8 fstate, state;
  414. __be16 pcierr;
  415. s64 rc;
  416. /* Sanity check on PE number */
  417. if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
  418. return OPAL_EEH_STOPPED_PERM_UNAVAIL;
  419. /*
  420. * Fetch the master PE and the PE instance might be
  421. * not initialized yet.
  422. */
  423. pe = &phb->ioda.pe_array[pe_no];
  424. if (pe->flags & PNV_IODA_PE_SLAVE) {
  425. pe = pe->master;
  426. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  427. pe_no = pe->pe_number;
  428. }
  429. /* Check the master PE */
  430. rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
  431. &state, &pcierr, NULL);
  432. if (rc != OPAL_SUCCESS) {
  433. pr_warn("%s: Failure %lld getting "
  434. "PHB#%x-PE#%x state\n",
  435. __func__, rc,
  436. phb->hose->global_number, pe_no);
  437. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  438. }
  439. /* Check the slave PE */
  440. if (!(pe->flags & PNV_IODA_PE_MASTER))
  441. return state;
  442. list_for_each_entry(slave, &pe->slaves, list) {
  443. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  444. slave->pe_number,
  445. &fstate,
  446. &pcierr,
  447. NULL);
  448. if (rc != OPAL_SUCCESS) {
  449. pr_warn("%s: Failure %lld getting "
  450. "PHB#%x-PE#%x state\n",
  451. __func__, rc,
  452. phb->hose->global_number, slave->pe_number);
  453. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  454. }
  455. /*
  456. * Override the result based on the ascending
  457. * priority.
  458. */
  459. if (fstate > state)
  460. state = fstate;
  461. }
  462. return state;
  463. }
  464. /* Currently those 2 are only used when MSIs are enabled, this will change
  465. * but in the meantime, we need to protect them to avoid warnings
  466. */
  467. #ifdef CONFIG_PCI_MSI
  468. static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
  469. {
  470. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  471. struct pnv_phb *phb = hose->private_data;
  472. struct pci_dn *pdn = pci_get_pdn(dev);
  473. if (!pdn)
  474. return NULL;
  475. if (pdn->pe_number == IODA_INVALID_PE)
  476. return NULL;
  477. return &phb->ioda.pe_array[pdn->pe_number];
  478. }
  479. #endif /* CONFIG_PCI_MSI */
  480. static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
  481. struct pnv_ioda_pe *parent,
  482. struct pnv_ioda_pe *child,
  483. bool is_add)
  484. {
  485. const char *desc = is_add ? "adding" : "removing";
  486. uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
  487. OPAL_REMOVE_PE_FROM_DOMAIN;
  488. struct pnv_ioda_pe *slave;
  489. long rc;
  490. /* Parent PE affects child PE */
  491. rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
  492. child->pe_number, op);
  493. if (rc != OPAL_SUCCESS) {
  494. pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
  495. rc, desc);
  496. return -ENXIO;
  497. }
  498. if (!(child->flags & PNV_IODA_PE_MASTER))
  499. return 0;
  500. /* Compound case: parent PE affects slave PEs */
  501. list_for_each_entry(slave, &child->slaves, list) {
  502. rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
  503. slave->pe_number, op);
  504. if (rc != OPAL_SUCCESS) {
  505. pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
  506. rc, desc);
  507. return -ENXIO;
  508. }
  509. }
  510. return 0;
  511. }
  512. static int pnv_ioda_set_peltv(struct pnv_phb *phb,
  513. struct pnv_ioda_pe *pe,
  514. bool is_add)
  515. {
  516. struct pnv_ioda_pe *slave;
  517. struct pci_dev *pdev = NULL;
  518. int ret;
  519. /*
  520. * Clear PE frozen state. If it's master PE, we need
  521. * clear slave PE frozen state as well.
  522. */
  523. if (is_add) {
  524. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  525. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  526. if (pe->flags & PNV_IODA_PE_MASTER) {
  527. list_for_each_entry(slave, &pe->slaves, list)
  528. opal_pci_eeh_freeze_clear(phb->opal_id,
  529. slave->pe_number,
  530. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  531. }
  532. }
  533. /*
  534. * Associate PE in PELT. We need add the PE into the
  535. * corresponding PELT-V as well. Otherwise, the error
  536. * originated from the PE might contribute to other
  537. * PEs.
  538. */
  539. ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
  540. if (ret)
  541. return ret;
  542. /* For compound PEs, any one affects all of them */
  543. if (pe->flags & PNV_IODA_PE_MASTER) {
  544. list_for_each_entry(slave, &pe->slaves, list) {
  545. ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
  546. if (ret)
  547. return ret;
  548. }
  549. }
  550. if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
  551. pdev = pe->pbus->self;
  552. else if (pe->flags & PNV_IODA_PE_DEV)
  553. pdev = pe->pdev->bus->self;
  554. #ifdef CONFIG_PCI_IOV
  555. else if (pe->flags & PNV_IODA_PE_VF)
  556. pdev = pe->parent_dev->bus->self;
  557. #endif /* CONFIG_PCI_IOV */
  558. while (pdev) {
  559. struct pci_dn *pdn = pci_get_pdn(pdev);
  560. struct pnv_ioda_pe *parent;
  561. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  562. parent = &phb->ioda.pe_array[pdn->pe_number];
  563. ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
  564. if (ret)
  565. return ret;
  566. }
  567. pdev = pdev->bus->self;
  568. }
  569. return 0;
  570. }
  571. #ifdef CONFIG_PCI_IOV
  572. static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  573. {
  574. struct pci_dev *parent;
  575. uint8_t bcomp, dcomp, fcomp;
  576. int64_t rc;
  577. long rid_end, rid;
  578. /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
  579. if (pe->pbus) {
  580. int count;
  581. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  582. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  583. parent = pe->pbus->self;
  584. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  585. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  586. else
  587. count = 1;
  588. switch(count) {
  589. case 1: bcomp = OpalPciBusAll; break;
  590. case 2: bcomp = OpalPciBus7Bits; break;
  591. case 4: bcomp = OpalPciBus6Bits; break;
  592. case 8: bcomp = OpalPciBus5Bits; break;
  593. case 16: bcomp = OpalPciBus4Bits; break;
  594. case 32: bcomp = OpalPciBus3Bits; break;
  595. default:
  596. dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
  597. count);
  598. /* Do an exact match only */
  599. bcomp = OpalPciBusAll;
  600. }
  601. rid_end = pe->rid + (count << 8);
  602. } else {
  603. if (pe->flags & PNV_IODA_PE_VF)
  604. parent = pe->parent_dev;
  605. else
  606. parent = pe->pdev->bus->self;
  607. bcomp = OpalPciBusAll;
  608. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  609. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  610. rid_end = pe->rid + 1;
  611. }
  612. /* Clear the reverse map */
  613. for (rid = pe->rid; rid < rid_end; rid++)
  614. phb->ioda.pe_rmap[rid] = 0;
  615. /* Release from all parents PELT-V */
  616. while (parent) {
  617. struct pci_dn *pdn = pci_get_pdn(parent);
  618. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  619. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  620. pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
  621. /* XXX What to do in case of error ? */
  622. }
  623. parent = parent->bus->self;
  624. }
  625. opal_pci_eeh_freeze_set(phb->opal_id, pe->pe_number,
  626. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  627. /* Disassociate PE in PELT */
  628. rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
  629. pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
  630. if (rc)
  631. pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
  632. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  633. bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
  634. if (rc)
  635. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  636. pe->pbus = NULL;
  637. pe->pdev = NULL;
  638. pe->parent_dev = NULL;
  639. return 0;
  640. }
  641. #endif /* CONFIG_PCI_IOV */
  642. static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  643. {
  644. struct pci_dev *parent;
  645. uint8_t bcomp, dcomp, fcomp;
  646. long rc, rid_end, rid;
  647. /* Bus validation ? */
  648. if (pe->pbus) {
  649. int count;
  650. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  651. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  652. parent = pe->pbus->self;
  653. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  654. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  655. else
  656. count = 1;
  657. switch(count) {
  658. case 1: bcomp = OpalPciBusAll; break;
  659. case 2: bcomp = OpalPciBus7Bits; break;
  660. case 4: bcomp = OpalPciBus6Bits; break;
  661. case 8: bcomp = OpalPciBus5Bits; break;
  662. case 16: bcomp = OpalPciBus4Bits; break;
  663. case 32: bcomp = OpalPciBus3Bits; break;
  664. default:
  665. dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
  666. count);
  667. /* Do an exact match only */
  668. bcomp = OpalPciBusAll;
  669. }
  670. rid_end = pe->rid + (count << 8);
  671. } else {
  672. #ifdef CONFIG_PCI_IOV
  673. if (pe->flags & PNV_IODA_PE_VF)
  674. parent = pe->parent_dev;
  675. else
  676. #endif /* CONFIG_PCI_IOV */
  677. parent = pe->pdev->bus->self;
  678. bcomp = OpalPciBusAll;
  679. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  680. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  681. rid_end = pe->rid + 1;
  682. }
  683. /*
  684. * Associate PE in PELT. We need add the PE into the
  685. * corresponding PELT-V as well. Otherwise, the error
  686. * originated from the PE might contribute to other
  687. * PEs.
  688. */
  689. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  690. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  691. if (rc) {
  692. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  693. return -ENXIO;
  694. }
  695. /* Configure PELTV */
  696. pnv_ioda_set_peltv(phb, pe, true);
  697. /* Setup reverse map */
  698. for (rid = pe->rid; rid < rid_end; rid++)
  699. phb->ioda.pe_rmap[rid] = pe->pe_number;
  700. /* Setup one MVTs on IODA1 */
  701. if (phb->type != PNV_PHB_IODA1) {
  702. pe->mve_number = 0;
  703. goto out;
  704. }
  705. pe->mve_number = pe->pe_number;
  706. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
  707. if (rc != OPAL_SUCCESS) {
  708. pe_err(pe, "OPAL error %ld setting up MVE %d\n",
  709. rc, pe->mve_number);
  710. pe->mve_number = -1;
  711. } else {
  712. rc = opal_pci_set_mve_enable(phb->opal_id,
  713. pe->mve_number, OPAL_ENABLE_MVE);
  714. if (rc) {
  715. pe_err(pe, "OPAL error %ld enabling MVE %d\n",
  716. rc, pe->mve_number);
  717. pe->mve_number = -1;
  718. }
  719. }
  720. out:
  721. return 0;
  722. }
  723. static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
  724. struct pnv_ioda_pe *pe)
  725. {
  726. struct pnv_ioda_pe *lpe;
  727. list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
  728. if (lpe->dma_weight < pe->dma_weight) {
  729. list_add_tail(&pe->dma_link, &lpe->dma_link);
  730. return;
  731. }
  732. }
  733. list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
  734. }
  735. static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
  736. {
  737. /* This is quite simplistic. The "base" weight of a device
  738. * is 10. 0 means no DMA is to be accounted for it.
  739. */
  740. /* If it's a bridge, no DMA */
  741. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  742. return 0;
  743. /* Reduce the weight of slow USB controllers */
  744. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  745. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  746. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  747. return 3;
  748. /* Increase the weight of RAID (includes Obsidian) */
  749. if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  750. return 15;
  751. /* Default */
  752. return 10;
  753. }
  754. #ifdef CONFIG_PCI_IOV
  755. static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
  756. {
  757. struct pci_dn *pdn = pci_get_pdn(dev);
  758. int i;
  759. struct resource *res, res2;
  760. resource_size_t size;
  761. u16 num_vfs;
  762. if (!dev->is_physfn)
  763. return -EINVAL;
  764. /*
  765. * "offset" is in VFs. The M64 windows are sized so that when they
  766. * are segmented, each segment is the same size as the IOV BAR.
  767. * Each segment is in a separate PE, and the high order bits of the
  768. * address are the PE number. Therefore, each VF's BAR is in a
  769. * separate PE, and changing the IOV BAR start address changes the
  770. * range of PEs the VFs are in.
  771. */
  772. num_vfs = pdn->num_vfs;
  773. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  774. res = &dev->resource[i + PCI_IOV_RESOURCES];
  775. if (!res->flags || !res->parent)
  776. continue;
  777. if (!pnv_pci_is_mem_pref_64(res->flags))
  778. continue;
  779. /*
  780. * The actual IOV BAR range is determined by the start address
  781. * and the actual size for num_vfs VFs BAR. This check is to
  782. * make sure that after shifting, the range will not overlap
  783. * with another device.
  784. */
  785. size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
  786. res2.flags = res->flags;
  787. res2.start = res->start + (size * offset);
  788. res2.end = res2.start + (size * num_vfs) - 1;
  789. if (res2.end > res->end) {
  790. dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
  791. i, &res2, res, num_vfs, offset);
  792. return -EBUSY;
  793. }
  794. }
  795. /*
  796. * After doing so, there would be a "hole" in the /proc/iomem when
  797. * offset is a positive value. It looks like the device return some
  798. * mmio back to the system, which actually no one could use it.
  799. */
  800. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  801. res = &dev->resource[i + PCI_IOV_RESOURCES];
  802. if (!res->flags || !res->parent)
  803. continue;
  804. if (!pnv_pci_is_mem_pref_64(res->flags))
  805. continue;
  806. size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
  807. res2 = *res;
  808. res->start += size * offset;
  809. dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (enabling %d VFs shifted by %d)\n",
  810. i, &res2, res, num_vfs, offset);
  811. pci_update_resource(dev, i + PCI_IOV_RESOURCES);
  812. }
  813. return 0;
  814. }
  815. #endif /* CONFIG_PCI_IOV */
  816. #if 0
  817. static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  818. {
  819. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  820. struct pnv_phb *phb = hose->private_data;
  821. struct pci_dn *pdn = pci_get_pdn(dev);
  822. struct pnv_ioda_pe *pe;
  823. int pe_num;
  824. if (!pdn) {
  825. pr_err("%s: Device tree node not associated properly\n",
  826. pci_name(dev));
  827. return NULL;
  828. }
  829. if (pdn->pe_number != IODA_INVALID_PE)
  830. return NULL;
  831. /* PE#0 has been pre-set */
  832. if (dev->bus->number == 0)
  833. pe_num = 0;
  834. else
  835. pe_num = pnv_ioda_alloc_pe(phb);
  836. if (pe_num == IODA_INVALID_PE) {
  837. pr_warning("%s: Not enough PE# available, disabling device\n",
  838. pci_name(dev));
  839. return NULL;
  840. }
  841. /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
  842. * pointer in the PE data structure, both should be destroyed at the
  843. * same time. However, this needs to be looked at more closely again
  844. * once we actually start removing things (Hotplug, SR-IOV, ...)
  845. *
  846. * At some point we want to remove the PDN completely anyways
  847. */
  848. pe = &phb->ioda.pe_array[pe_num];
  849. pci_dev_get(dev);
  850. pdn->pcidev = dev;
  851. pdn->pe_number = pe_num;
  852. pe->pdev = dev;
  853. pe->pbus = NULL;
  854. pe->tce32_seg = -1;
  855. pe->mve_number = -1;
  856. pe->rid = dev->bus->number << 8 | pdn->devfn;
  857. pe_info(pe, "Associated device to PE\n");
  858. if (pnv_ioda_configure_pe(phb, pe)) {
  859. /* XXX What do we do here ? */
  860. if (pe_num)
  861. pnv_ioda_free_pe(phb, pe_num);
  862. pdn->pe_number = IODA_INVALID_PE;
  863. pe->pdev = NULL;
  864. pci_dev_put(dev);
  865. return NULL;
  866. }
  867. /* Assign a DMA weight to the device */
  868. pe->dma_weight = pnv_ioda_dma_weight(dev);
  869. if (pe->dma_weight != 0) {
  870. phb->ioda.dma_weight += pe->dma_weight;
  871. phb->ioda.dma_pe_count++;
  872. }
  873. /* Link the PE */
  874. pnv_ioda_link_pe_by_weight(phb, pe);
  875. return pe;
  876. }
  877. #endif /* Useful for SRIOV case */
  878. static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
  879. {
  880. struct pci_dev *dev;
  881. list_for_each_entry(dev, &bus->devices, bus_list) {
  882. struct pci_dn *pdn = pci_get_pdn(dev);
  883. if (pdn == NULL) {
  884. pr_warn("%s: No device node associated with device !\n",
  885. pci_name(dev));
  886. continue;
  887. }
  888. pdn->pe_number = pe->pe_number;
  889. pe->dma_weight += pnv_ioda_dma_weight(dev);
  890. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  891. pnv_ioda_setup_same_PE(dev->subordinate, pe);
  892. }
  893. }
  894. /*
  895. * There're 2 types of PCI bus sensitive PEs: One that is compromised of
  896. * single PCI bus. Another one that contains the primary PCI bus and its
  897. * subordinate PCI devices and buses. The second type of PE is normally
  898. * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
  899. */
  900. static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
  901. {
  902. struct pci_controller *hose = pci_bus_to_host(bus);
  903. struct pnv_phb *phb = hose->private_data;
  904. struct pnv_ioda_pe *pe;
  905. int pe_num = IODA_INVALID_PE;
  906. /* Check if PE is determined by M64 */
  907. if (phb->pick_m64_pe)
  908. pe_num = phb->pick_m64_pe(phb, bus, all);
  909. /* The PE number isn't pinned by M64 */
  910. if (pe_num == IODA_INVALID_PE)
  911. pe_num = pnv_ioda_alloc_pe(phb);
  912. if (pe_num == IODA_INVALID_PE) {
  913. pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
  914. __func__, pci_domain_nr(bus), bus->number);
  915. return;
  916. }
  917. pe = &phb->ioda.pe_array[pe_num];
  918. pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
  919. pe->pbus = bus;
  920. pe->pdev = NULL;
  921. pe->tce32_seg = -1;
  922. pe->mve_number = -1;
  923. pe->rid = bus->busn_res.start << 8;
  924. pe->dma_weight = 0;
  925. if (all)
  926. pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
  927. bus->busn_res.start, bus->busn_res.end, pe_num);
  928. else
  929. pe_info(pe, "Secondary bus %d associated with PE#%d\n",
  930. bus->busn_res.start, pe_num);
  931. if (pnv_ioda_configure_pe(phb, pe)) {
  932. /* XXX What do we do here ? */
  933. if (pe_num)
  934. pnv_ioda_free_pe(phb, pe_num);
  935. pe->pbus = NULL;
  936. return;
  937. }
  938. pe->tce32_table = kzalloc_node(sizeof(struct iommu_table),
  939. GFP_KERNEL, hose->node);
  940. pe->tce32_table->data = pe;
  941. /* Associate it with all child devices */
  942. pnv_ioda_setup_same_PE(bus, pe);
  943. /* Put PE to the list */
  944. list_add_tail(&pe->list, &phb->ioda.pe_list);
  945. /* Account for one DMA PE if at least one DMA capable device exist
  946. * below the bridge
  947. */
  948. if (pe->dma_weight != 0) {
  949. phb->ioda.dma_weight += pe->dma_weight;
  950. phb->ioda.dma_pe_count++;
  951. }
  952. /* Link the PE */
  953. pnv_ioda_link_pe_by_weight(phb, pe);
  954. }
  955. static void pnv_ioda_setup_PEs(struct pci_bus *bus)
  956. {
  957. struct pci_dev *dev;
  958. pnv_ioda_setup_bus_PE(bus, 0);
  959. list_for_each_entry(dev, &bus->devices, bus_list) {
  960. if (dev->subordinate) {
  961. if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
  962. pnv_ioda_setup_bus_PE(dev->subordinate, 1);
  963. else
  964. pnv_ioda_setup_PEs(dev->subordinate);
  965. }
  966. }
  967. }
  968. /*
  969. * Configure PEs so that the downstream PCI buses and devices
  970. * could have their associated PE#. Unfortunately, we didn't
  971. * figure out the way to identify the PLX bridge yet. So we
  972. * simply put the PCI bus and the subordinate behind the root
  973. * port to PE# here. The game rule here is expected to be changed
  974. * as soon as we can detected PLX bridge correctly.
  975. */
  976. static void pnv_pci_ioda_setup_PEs(void)
  977. {
  978. struct pci_controller *hose, *tmp;
  979. struct pnv_phb *phb;
  980. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  981. phb = hose->private_data;
  982. /* M64 layout might affect PE allocation */
  983. if (phb->reserve_m64_pe)
  984. phb->reserve_m64_pe(phb);
  985. pnv_ioda_setup_PEs(hose->bus);
  986. }
  987. }
  988. #ifdef CONFIG_PCI_IOV
  989. static int pnv_pci_vf_release_m64(struct pci_dev *pdev)
  990. {
  991. struct pci_bus *bus;
  992. struct pci_controller *hose;
  993. struct pnv_phb *phb;
  994. struct pci_dn *pdn;
  995. int i, j;
  996. bus = pdev->bus;
  997. hose = pci_bus_to_host(bus);
  998. phb = hose->private_data;
  999. pdn = pci_get_pdn(pdev);
  1000. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
  1001. for (j = 0; j < M64_PER_IOV; j++) {
  1002. if (pdn->m64_wins[i][j] == IODA_INVALID_M64)
  1003. continue;
  1004. opal_pci_phb_mmio_enable(phb->opal_id,
  1005. OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 0);
  1006. clear_bit(pdn->m64_wins[i][j], &phb->ioda.m64_bar_alloc);
  1007. pdn->m64_wins[i][j] = IODA_INVALID_M64;
  1008. }
  1009. return 0;
  1010. }
  1011. static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
  1012. {
  1013. struct pci_bus *bus;
  1014. struct pci_controller *hose;
  1015. struct pnv_phb *phb;
  1016. struct pci_dn *pdn;
  1017. unsigned int win;
  1018. struct resource *res;
  1019. int i, j;
  1020. int64_t rc;
  1021. int total_vfs;
  1022. resource_size_t size, start;
  1023. int pe_num;
  1024. int vf_groups;
  1025. int vf_per_group;
  1026. bus = pdev->bus;
  1027. hose = pci_bus_to_host(bus);
  1028. phb = hose->private_data;
  1029. pdn = pci_get_pdn(pdev);
  1030. total_vfs = pci_sriov_get_totalvfs(pdev);
  1031. /* Initialize the m64_wins to IODA_INVALID_M64 */
  1032. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
  1033. for (j = 0; j < M64_PER_IOV; j++)
  1034. pdn->m64_wins[i][j] = IODA_INVALID_M64;
  1035. if (pdn->m64_per_iov == M64_PER_IOV) {
  1036. vf_groups = (num_vfs <= M64_PER_IOV) ? num_vfs: M64_PER_IOV;
  1037. vf_per_group = (num_vfs <= M64_PER_IOV)? 1:
  1038. roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
  1039. } else {
  1040. vf_groups = 1;
  1041. vf_per_group = 1;
  1042. }
  1043. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  1044. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  1045. if (!res->flags || !res->parent)
  1046. continue;
  1047. if (!pnv_pci_is_mem_pref_64(res->flags))
  1048. continue;
  1049. for (j = 0; j < vf_groups; j++) {
  1050. do {
  1051. win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
  1052. phb->ioda.m64_bar_idx + 1, 0);
  1053. if (win >= phb->ioda.m64_bar_idx + 1)
  1054. goto m64_failed;
  1055. } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
  1056. pdn->m64_wins[i][j] = win;
  1057. if (pdn->m64_per_iov == M64_PER_IOV) {
  1058. size = pci_iov_resource_size(pdev,
  1059. PCI_IOV_RESOURCES + i);
  1060. size = size * vf_per_group;
  1061. start = res->start + size * j;
  1062. } else {
  1063. size = resource_size(res);
  1064. start = res->start;
  1065. }
  1066. /* Map the M64 here */
  1067. if (pdn->m64_per_iov == M64_PER_IOV) {
  1068. pe_num = pdn->offset + j;
  1069. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  1070. pe_num, OPAL_M64_WINDOW_TYPE,
  1071. pdn->m64_wins[i][j], 0);
  1072. }
  1073. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  1074. OPAL_M64_WINDOW_TYPE,
  1075. pdn->m64_wins[i][j],
  1076. start,
  1077. 0, /* unused */
  1078. size);
  1079. if (rc != OPAL_SUCCESS) {
  1080. dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
  1081. win, rc);
  1082. goto m64_failed;
  1083. }
  1084. if (pdn->m64_per_iov == M64_PER_IOV)
  1085. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  1086. OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 2);
  1087. else
  1088. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  1089. OPAL_M64_WINDOW_TYPE, pdn->m64_wins[i][j], 1);
  1090. if (rc != OPAL_SUCCESS) {
  1091. dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
  1092. win, rc);
  1093. goto m64_failed;
  1094. }
  1095. }
  1096. }
  1097. return 0;
  1098. m64_failed:
  1099. pnv_pci_vf_release_m64(pdev);
  1100. return -EBUSY;
  1101. }
  1102. static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
  1103. {
  1104. struct pci_bus *bus;
  1105. struct pci_controller *hose;
  1106. struct pnv_phb *phb;
  1107. struct iommu_table *tbl;
  1108. unsigned long addr;
  1109. int64_t rc;
  1110. bus = dev->bus;
  1111. hose = pci_bus_to_host(bus);
  1112. phb = hose->private_data;
  1113. tbl = pe->tce32_table;
  1114. addr = tbl->it_base;
  1115. opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  1116. pe->pe_number << 1, 1, __pa(addr),
  1117. 0, 0x1000);
  1118. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  1119. pe->pe_number,
  1120. (pe->pe_number << 1) + 1,
  1121. pe->tce_bypass_base,
  1122. 0);
  1123. if (rc)
  1124. pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
  1125. iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
  1126. free_pages(addr, get_order(TCE32_TABLE_SIZE));
  1127. pe->tce32_table = NULL;
  1128. }
  1129. static void pnv_ioda_release_vf_PE(struct pci_dev *pdev, u16 num_vfs)
  1130. {
  1131. struct pci_bus *bus;
  1132. struct pci_controller *hose;
  1133. struct pnv_phb *phb;
  1134. struct pnv_ioda_pe *pe, *pe_n;
  1135. struct pci_dn *pdn;
  1136. u16 vf_index;
  1137. int64_t rc;
  1138. bus = pdev->bus;
  1139. hose = pci_bus_to_host(bus);
  1140. phb = hose->private_data;
  1141. pdn = pci_get_pdn(pdev);
  1142. if (!pdev->is_physfn)
  1143. return;
  1144. if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
  1145. int vf_group;
  1146. int vf_per_group;
  1147. int vf_index1;
  1148. vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
  1149. for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++)
  1150. for (vf_index = vf_group * vf_per_group;
  1151. vf_index < (vf_group + 1) * vf_per_group &&
  1152. vf_index < num_vfs;
  1153. vf_index++)
  1154. for (vf_index1 = vf_group * vf_per_group;
  1155. vf_index1 < (vf_group + 1) * vf_per_group &&
  1156. vf_index1 < num_vfs;
  1157. vf_index1++){
  1158. rc = opal_pci_set_peltv(phb->opal_id,
  1159. pdn->offset + vf_index,
  1160. pdn->offset + vf_index1,
  1161. OPAL_REMOVE_PE_FROM_DOMAIN);
  1162. if (rc)
  1163. dev_warn(&pdev->dev, "%s: Failed to unlink same group PE#%d(%lld)\n",
  1164. __func__,
  1165. pdn->offset + vf_index1, rc);
  1166. }
  1167. }
  1168. list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
  1169. if (pe->parent_dev != pdev)
  1170. continue;
  1171. pnv_pci_ioda2_release_dma_pe(pdev, pe);
  1172. /* Remove from list */
  1173. mutex_lock(&phb->ioda.pe_list_mutex);
  1174. list_del(&pe->list);
  1175. mutex_unlock(&phb->ioda.pe_list_mutex);
  1176. pnv_ioda_deconfigure_pe(phb, pe);
  1177. pnv_ioda_free_pe(phb, pe->pe_number);
  1178. }
  1179. }
  1180. void pnv_pci_sriov_disable(struct pci_dev *pdev)
  1181. {
  1182. struct pci_bus *bus;
  1183. struct pci_controller *hose;
  1184. struct pnv_phb *phb;
  1185. struct pci_dn *pdn;
  1186. struct pci_sriov *iov;
  1187. u16 num_vfs;
  1188. bus = pdev->bus;
  1189. hose = pci_bus_to_host(bus);
  1190. phb = hose->private_data;
  1191. pdn = pci_get_pdn(pdev);
  1192. iov = pdev->sriov;
  1193. num_vfs = pdn->num_vfs;
  1194. /* Release VF PEs */
  1195. pnv_ioda_release_vf_PE(pdev, num_vfs);
  1196. if (phb->type == PNV_PHB_IODA2) {
  1197. if (pdn->m64_per_iov == 1)
  1198. pnv_pci_vf_resource_shift(pdev, -pdn->offset);
  1199. /* Release M64 windows */
  1200. pnv_pci_vf_release_m64(pdev);
  1201. /* Release PE numbers */
  1202. bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
  1203. pdn->offset = 0;
  1204. }
  1205. }
  1206. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  1207. struct pnv_ioda_pe *pe);
  1208. static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
  1209. {
  1210. struct pci_bus *bus;
  1211. struct pci_controller *hose;
  1212. struct pnv_phb *phb;
  1213. struct pnv_ioda_pe *pe;
  1214. int pe_num;
  1215. u16 vf_index;
  1216. struct pci_dn *pdn;
  1217. int64_t rc;
  1218. bus = pdev->bus;
  1219. hose = pci_bus_to_host(bus);
  1220. phb = hose->private_data;
  1221. pdn = pci_get_pdn(pdev);
  1222. if (!pdev->is_physfn)
  1223. return;
  1224. /* Reserve PE for each VF */
  1225. for (vf_index = 0; vf_index < num_vfs; vf_index++) {
  1226. pe_num = pdn->offset + vf_index;
  1227. pe = &phb->ioda.pe_array[pe_num];
  1228. pe->pe_number = pe_num;
  1229. pe->phb = phb;
  1230. pe->flags = PNV_IODA_PE_VF;
  1231. pe->pbus = NULL;
  1232. pe->parent_dev = pdev;
  1233. pe->tce32_seg = -1;
  1234. pe->mve_number = -1;
  1235. pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
  1236. pci_iov_virtfn_devfn(pdev, vf_index);
  1237. pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%d\n",
  1238. hose->global_number, pdev->bus->number,
  1239. PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
  1240. PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
  1241. if (pnv_ioda_configure_pe(phb, pe)) {
  1242. /* XXX What do we do here ? */
  1243. if (pe_num)
  1244. pnv_ioda_free_pe(phb, pe_num);
  1245. pe->pdev = NULL;
  1246. continue;
  1247. }
  1248. pe->tce32_table = kzalloc_node(sizeof(struct iommu_table),
  1249. GFP_KERNEL, hose->node);
  1250. pe->tce32_table->data = pe;
  1251. /* Put PE to the list */
  1252. mutex_lock(&phb->ioda.pe_list_mutex);
  1253. list_add_tail(&pe->list, &phb->ioda.pe_list);
  1254. mutex_unlock(&phb->ioda.pe_list_mutex);
  1255. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  1256. }
  1257. if (pdn->m64_per_iov == M64_PER_IOV && num_vfs > M64_PER_IOV) {
  1258. int vf_group;
  1259. int vf_per_group;
  1260. int vf_index1;
  1261. vf_per_group = roundup_pow_of_two(num_vfs) / pdn->m64_per_iov;
  1262. for (vf_group = 0; vf_group < M64_PER_IOV; vf_group++) {
  1263. for (vf_index = vf_group * vf_per_group;
  1264. vf_index < (vf_group + 1) * vf_per_group &&
  1265. vf_index < num_vfs;
  1266. vf_index++) {
  1267. for (vf_index1 = vf_group * vf_per_group;
  1268. vf_index1 < (vf_group + 1) * vf_per_group &&
  1269. vf_index1 < num_vfs;
  1270. vf_index1++) {
  1271. rc = opal_pci_set_peltv(phb->opal_id,
  1272. pdn->offset + vf_index,
  1273. pdn->offset + vf_index1,
  1274. OPAL_ADD_PE_TO_DOMAIN);
  1275. if (rc)
  1276. dev_warn(&pdev->dev, "%s: Failed to link same group PE#%d(%lld)\n",
  1277. __func__,
  1278. pdn->offset + vf_index1, rc);
  1279. }
  1280. }
  1281. }
  1282. }
  1283. }
  1284. int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  1285. {
  1286. struct pci_bus *bus;
  1287. struct pci_controller *hose;
  1288. struct pnv_phb *phb;
  1289. struct pci_dn *pdn;
  1290. int ret;
  1291. bus = pdev->bus;
  1292. hose = pci_bus_to_host(bus);
  1293. phb = hose->private_data;
  1294. pdn = pci_get_pdn(pdev);
  1295. if (phb->type == PNV_PHB_IODA2) {
  1296. /* Calculate available PE for required VFs */
  1297. mutex_lock(&phb->ioda.pe_alloc_mutex);
  1298. pdn->offset = bitmap_find_next_zero_area(
  1299. phb->ioda.pe_alloc, phb->ioda.total_pe,
  1300. 0, num_vfs, 0);
  1301. if (pdn->offset >= phb->ioda.total_pe) {
  1302. mutex_unlock(&phb->ioda.pe_alloc_mutex);
  1303. dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
  1304. pdn->offset = 0;
  1305. return -EBUSY;
  1306. }
  1307. bitmap_set(phb->ioda.pe_alloc, pdn->offset, num_vfs);
  1308. pdn->num_vfs = num_vfs;
  1309. mutex_unlock(&phb->ioda.pe_alloc_mutex);
  1310. /* Assign M64 window accordingly */
  1311. ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
  1312. if (ret) {
  1313. dev_info(&pdev->dev, "Not enough M64 window resources\n");
  1314. goto m64_failed;
  1315. }
  1316. /*
  1317. * When using one M64 BAR to map one IOV BAR, we need to shift
  1318. * the IOV BAR according to the PE# allocated to the VFs.
  1319. * Otherwise, the PE# for the VF will conflict with others.
  1320. */
  1321. if (pdn->m64_per_iov == 1) {
  1322. ret = pnv_pci_vf_resource_shift(pdev, pdn->offset);
  1323. if (ret)
  1324. goto m64_failed;
  1325. }
  1326. }
  1327. /* Setup VF PEs */
  1328. pnv_ioda_setup_vf_PE(pdev, num_vfs);
  1329. return 0;
  1330. m64_failed:
  1331. bitmap_clear(phb->ioda.pe_alloc, pdn->offset, num_vfs);
  1332. pdn->offset = 0;
  1333. return ret;
  1334. }
  1335. int pcibios_sriov_disable(struct pci_dev *pdev)
  1336. {
  1337. pnv_pci_sriov_disable(pdev);
  1338. /* Release PCI data */
  1339. remove_dev_pci_data(pdev);
  1340. return 0;
  1341. }
  1342. int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  1343. {
  1344. /* Allocate PCI data */
  1345. add_dev_pci_data(pdev);
  1346. pnv_pci_sriov_enable(pdev, num_vfs);
  1347. return 0;
  1348. }
  1349. #endif /* CONFIG_PCI_IOV */
  1350. static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
  1351. {
  1352. struct pci_dn *pdn = pci_get_pdn(pdev);
  1353. struct pnv_ioda_pe *pe;
  1354. /*
  1355. * The function can be called while the PE#
  1356. * hasn't been assigned. Do nothing for the
  1357. * case.
  1358. */
  1359. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  1360. return;
  1361. pe = &phb->ioda.pe_array[pdn->pe_number];
  1362. WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
  1363. set_iommu_table_base_and_group(&pdev->dev, pe->tce32_table);
  1364. }
  1365. static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
  1366. struct pci_dev *pdev, u64 dma_mask)
  1367. {
  1368. struct pci_dn *pdn = pci_get_pdn(pdev);
  1369. struct pnv_ioda_pe *pe;
  1370. uint64_t top;
  1371. bool bypass = false;
  1372. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  1373. return -ENODEV;;
  1374. pe = &phb->ioda.pe_array[pdn->pe_number];
  1375. if (pe->tce_bypass_enabled) {
  1376. top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
  1377. bypass = (dma_mask >= top);
  1378. }
  1379. if (bypass) {
  1380. dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
  1381. set_dma_ops(&pdev->dev, &dma_direct_ops);
  1382. set_dma_offset(&pdev->dev, pe->tce_bypass_base);
  1383. } else {
  1384. dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
  1385. set_dma_ops(&pdev->dev, &dma_iommu_ops);
  1386. set_iommu_table_base(&pdev->dev, pe->tce32_table);
  1387. }
  1388. *pdev->dev.dma_mask = dma_mask;
  1389. return 0;
  1390. }
  1391. static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
  1392. struct pci_dev *pdev)
  1393. {
  1394. struct pci_dn *pdn = pci_get_pdn(pdev);
  1395. struct pnv_ioda_pe *pe;
  1396. u64 end, mask;
  1397. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  1398. return 0;
  1399. pe = &phb->ioda.pe_array[pdn->pe_number];
  1400. if (!pe->tce_bypass_enabled)
  1401. return __dma_get_required_mask(&pdev->dev);
  1402. end = pe->tce_bypass_base + memblock_end_of_DRAM();
  1403. mask = 1ULL << (fls64(end) - 1);
  1404. mask += mask - 1;
  1405. return mask;
  1406. }
  1407. static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
  1408. struct pci_bus *bus,
  1409. bool add_to_iommu_group)
  1410. {
  1411. struct pci_dev *dev;
  1412. list_for_each_entry(dev, &bus->devices, bus_list) {
  1413. if (add_to_iommu_group)
  1414. set_iommu_table_base_and_group(&dev->dev,
  1415. pe->tce32_table);
  1416. else
  1417. set_iommu_table_base(&dev->dev, pe->tce32_table);
  1418. if (dev->subordinate)
  1419. pnv_ioda_setup_bus_dma(pe, dev->subordinate,
  1420. add_to_iommu_group);
  1421. }
  1422. }
  1423. static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
  1424. struct iommu_table *tbl,
  1425. __be64 *startp, __be64 *endp, bool rm)
  1426. {
  1427. __be64 __iomem *invalidate = rm ?
  1428. (__be64 __iomem *)pe->tce_inval_reg_phys :
  1429. (__be64 __iomem *)tbl->it_index;
  1430. unsigned long start, end, inc;
  1431. const unsigned shift = tbl->it_page_shift;
  1432. start = __pa(startp);
  1433. end = __pa(endp);
  1434. /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
  1435. if (tbl->it_busno) {
  1436. start <<= shift;
  1437. end <<= shift;
  1438. inc = 128ull << shift;
  1439. start |= tbl->it_busno;
  1440. end |= tbl->it_busno;
  1441. } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
  1442. /* p7ioc-style invalidation, 2 TCEs per write */
  1443. start |= (1ull << 63);
  1444. end |= (1ull << 63);
  1445. inc = 16;
  1446. } else {
  1447. /* Default (older HW) */
  1448. inc = 128;
  1449. }
  1450. end |= inc - 1; /* round up end to be different than start */
  1451. mb(); /* Ensure above stores are visible */
  1452. while (start <= end) {
  1453. if (rm)
  1454. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  1455. else
  1456. __raw_writeq(cpu_to_be64(start), invalidate);
  1457. start += inc;
  1458. }
  1459. /*
  1460. * The iommu layer will do another mb() for us on build()
  1461. * and we don't care on free()
  1462. */
  1463. }
  1464. static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
  1465. struct iommu_table *tbl,
  1466. __be64 *startp, __be64 *endp, bool rm)
  1467. {
  1468. unsigned long start, end, inc;
  1469. __be64 __iomem *invalidate = rm ?
  1470. (__be64 __iomem *)pe->tce_inval_reg_phys :
  1471. (__be64 __iomem *)tbl->it_index;
  1472. const unsigned shift = tbl->it_page_shift;
  1473. /* We'll invalidate DMA address in PE scope */
  1474. start = 0x2ull << 60;
  1475. start |= (pe->pe_number & 0xFF);
  1476. end = start;
  1477. /* Figure out the start, end and step */
  1478. inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
  1479. start |= (inc << shift);
  1480. inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
  1481. end |= (inc << shift);
  1482. inc = (0x1ull << shift);
  1483. mb();
  1484. while (start <= end) {
  1485. if (rm)
  1486. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  1487. else
  1488. __raw_writeq(cpu_to_be64(start), invalidate);
  1489. start += inc;
  1490. }
  1491. }
  1492. void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
  1493. __be64 *startp, __be64 *endp, bool rm)
  1494. {
  1495. struct pnv_ioda_pe *pe = tbl->data;
  1496. struct pnv_phb *phb = pe->phb;
  1497. if (phb->type == PNV_PHB_IODA1)
  1498. pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
  1499. else
  1500. pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
  1501. }
  1502. static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
  1503. struct pnv_ioda_pe *pe, unsigned int base,
  1504. unsigned int segs)
  1505. {
  1506. struct page *tce_mem = NULL;
  1507. const __be64 *swinvp;
  1508. struct iommu_table *tbl;
  1509. unsigned int i;
  1510. int64_t rc;
  1511. void *addr;
  1512. /* XXX FIXME: Handle 64-bit only DMA devices */
  1513. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  1514. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  1515. /* We shouldn't already have a 32-bit DMA associated */
  1516. if (WARN_ON(pe->tce32_seg >= 0))
  1517. return;
  1518. /* Grab a 32-bit TCE table */
  1519. pe->tce32_seg = base;
  1520. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  1521. (base << 28), ((base + segs) << 28) - 1);
  1522. /* XXX Currently, we allocate one big contiguous table for the
  1523. * TCEs. We only really need one chunk per 256M of TCE space
  1524. * (ie per segment) but that's an optimization for later, it
  1525. * requires some added smarts with our get/put_tce implementation
  1526. */
  1527. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  1528. get_order(TCE32_TABLE_SIZE * segs));
  1529. if (!tce_mem) {
  1530. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  1531. goto fail;
  1532. }
  1533. addr = page_address(tce_mem);
  1534. memset(addr, 0, TCE32_TABLE_SIZE * segs);
  1535. /* Configure HW */
  1536. for (i = 0; i < segs; i++) {
  1537. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  1538. pe->pe_number,
  1539. base + i, 1,
  1540. __pa(addr) + TCE32_TABLE_SIZE * i,
  1541. TCE32_TABLE_SIZE, 0x1000);
  1542. if (rc) {
  1543. pe_err(pe, " Failed to configure 32-bit TCE table,"
  1544. " err %ld\n", rc);
  1545. goto fail;
  1546. }
  1547. }
  1548. /* Setup linux iommu table */
  1549. tbl = pe->tce32_table;
  1550. pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
  1551. base << 28, IOMMU_PAGE_SHIFT_4K);
  1552. /* OPAL variant of P7IOC SW invalidated TCEs */
  1553. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  1554. if (swinvp) {
  1555. /* We need a couple more fields -- an address and a data
  1556. * to or. Since the bus is only printed out on table free
  1557. * errors, and on the first pass the data will be a relative
  1558. * bus number, print that out instead.
  1559. */
  1560. pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
  1561. tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
  1562. 8);
  1563. tbl->it_type |= (TCE_PCI_SWINV_CREATE |
  1564. TCE_PCI_SWINV_FREE |
  1565. TCE_PCI_SWINV_PAIR);
  1566. }
  1567. iommu_init_table(tbl, phb->hose->node);
  1568. if (pe->flags & PNV_IODA_PE_DEV) {
  1569. iommu_register_group(tbl, phb->hose->global_number,
  1570. pe->pe_number);
  1571. set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
  1572. } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) {
  1573. iommu_register_group(tbl, phb->hose->global_number,
  1574. pe->pe_number);
  1575. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  1576. } else if (pe->flags & PNV_IODA_PE_VF) {
  1577. iommu_register_group(tbl, phb->hose->global_number,
  1578. pe->pe_number);
  1579. }
  1580. return;
  1581. fail:
  1582. /* XXX Failure: Try to fallback to 64-bit only ? */
  1583. if (pe->tce32_seg >= 0)
  1584. pe->tce32_seg = -1;
  1585. if (tce_mem)
  1586. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  1587. }
  1588. static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
  1589. {
  1590. struct pnv_ioda_pe *pe = tbl->data;
  1591. uint16_t window_id = (pe->pe_number << 1 ) + 1;
  1592. int64_t rc;
  1593. pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
  1594. if (enable) {
  1595. phys_addr_t top = memblock_end_of_DRAM();
  1596. top = roundup_pow_of_two(top);
  1597. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  1598. pe->pe_number,
  1599. window_id,
  1600. pe->tce_bypass_base,
  1601. top);
  1602. } else {
  1603. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  1604. pe->pe_number,
  1605. window_id,
  1606. pe->tce_bypass_base,
  1607. 0);
  1608. /*
  1609. * EEH needs the mapping between IOMMU table and group
  1610. * of those VFIO/KVM pass-through devices. We can postpone
  1611. * resetting DMA ops until the DMA mask is configured in
  1612. * host side.
  1613. */
  1614. if (pe->pdev)
  1615. set_iommu_table_base(&pe->pdev->dev, tbl);
  1616. else
  1617. pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
  1618. }
  1619. if (rc)
  1620. pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
  1621. else
  1622. pe->tce_bypass_enabled = enable;
  1623. }
  1624. static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
  1625. struct pnv_ioda_pe *pe)
  1626. {
  1627. /* TVE #1 is selected by PCI address bit 59 */
  1628. pe->tce_bypass_base = 1ull << 59;
  1629. /* Install set_bypass callback for VFIO */
  1630. pe->tce32_table->set_bypass = pnv_pci_ioda2_set_bypass;
  1631. /* Enable bypass by default */
  1632. pnv_pci_ioda2_set_bypass(pe->tce32_table, true);
  1633. }
  1634. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  1635. struct pnv_ioda_pe *pe)
  1636. {
  1637. struct page *tce_mem = NULL;
  1638. void *addr;
  1639. const __be64 *swinvp;
  1640. struct iommu_table *tbl;
  1641. unsigned int tce_table_size, end;
  1642. int64_t rc;
  1643. /* We shouldn't already have a 32-bit DMA associated */
  1644. if (WARN_ON(pe->tce32_seg >= 0))
  1645. return;
  1646. /* The PE will reserve all possible 32-bits space */
  1647. pe->tce32_seg = 0;
  1648. end = (1 << ilog2(phb->ioda.m32_pci_base));
  1649. tce_table_size = (end / 0x1000) * 8;
  1650. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  1651. end);
  1652. /* Allocate TCE table */
  1653. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  1654. get_order(tce_table_size));
  1655. if (!tce_mem) {
  1656. pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
  1657. goto fail;
  1658. }
  1659. addr = page_address(tce_mem);
  1660. memset(addr, 0, tce_table_size);
  1661. /*
  1662. * Map TCE table through TVT. The TVE index is the PE number
  1663. * shifted by 1 bit for 32-bits DMA space.
  1664. */
  1665. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  1666. pe->pe_number << 1, 1, __pa(addr),
  1667. tce_table_size, 0x1000);
  1668. if (rc) {
  1669. pe_err(pe, "Failed to configure 32-bit TCE table,"
  1670. " err %ld\n", rc);
  1671. goto fail;
  1672. }
  1673. /* Setup linux iommu table */
  1674. tbl = pe->tce32_table;
  1675. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
  1676. IOMMU_PAGE_SHIFT_4K);
  1677. /* OPAL variant of PHB3 invalidated TCEs */
  1678. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  1679. if (swinvp) {
  1680. /* We need a couple more fields -- an address and a data
  1681. * to or. Since the bus is only printed out on table free
  1682. * errors, and on the first pass the data will be a relative
  1683. * bus number, print that out instead.
  1684. */
  1685. pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
  1686. tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
  1687. 8);
  1688. tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
  1689. }
  1690. iommu_init_table(tbl, phb->hose->node);
  1691. if (pe->flags & PNV_IODA_PE_DEV) {
  1692. iommu_register_group(tbl, phb->hose->global_number,
  1693. pe->pe_number);
  1694. set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
  1695. } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)) {
  1696. iommu_register_group(tbl, phb->hose->global_number,
  1697. pe->pe_number);
  1698. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  1699. } else if (pe->flags & PNV_IODA_PE_VF) {
  1700. iommu_register_group(tbl, phb->hose->global_number,
  1701. pe->pe_number);
  1702. }
  1703. /* Also create a bypass window */
  1704. if (!pnv_iommu_bypass_disabled)
  1705. pnv_pci_ioda2_setup_bypass_pe(phb, pe);
  1706. return;
  1707. fail:
  1708. if (pe->tce32_seg >= 0)
  1709. pe->tce32_seg = -1;
  1710. if (tce_mem)
  1711. __free_pages(tce_mem, get_order(tce_table_size));
  1712. }
  1713. static void pnv_ioda_setup_dma(struct pnv_phb *phb)
  1714. {
  1715. struct pci_controller *hose = phb->hose;
  1716. unsigned int residual, remaining, segs, tw, base;
  1717. struct pnv_ioda_pe *pe;
  1718. /* If we have more PE# than segments available, hand out one
  1719. * per PE until we run out and let the rest fail. If not,
  1720. * then we assign at least one segment per PE, plus more based
  1721. * on the amount of devices under that PE
  1722. */
  1723. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  1724. residual = 0;
  1725. else
  1726. residual = phb->ioda.tce32_count -
  1727. phb->ioda.dma_pe_count;
  1728. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  1729. hose->global_number, phb->ioda.tce32_count);
  1730. pr_info("PCI: %d PE# for a total weight of %d\n",
  1731. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  1732. /* Walk our PE list and configure their DMA segments, hand them
  1733. * out one base segment plus any residual segments based on
  1734. * weight
  1735. */
  1736. remaining = phb->ioda.tce32_count;
  1737. tw = phb->ioda.dma_weight;
  1738. base = 0;
  1739. list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
  1740. if (!pe->dma_weight)
  1741. continue;
  1742. if (!remaining) {
  1743. pe_warn(pe, "No DMA32 resources available\n");
  1744. continue;
  1745. }
  1746. segs = 1;
  1747. if (residual) {
  1748. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  1749. if (segs > remaining)
  1750. segs = remaining;
  1751. }
  1752. /*
  1753. * For IODA2 compliant PHB3, we needn't care about the weight.
  1754. * The all available 32-bits DMA space will be assigned to
  1755. * the specific PE.
  1756. */
  1757. if (phb->type == PNV_PHB_IODA1) {
  1758. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  1759. pe->dma_weight, segs);
  1760. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  1761. } else {
  1762. pe_info(pe, "Assign DMA32 space\n");
  1763. segs = 0;
  1764. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  1765. }
  1766. remaining -= segs;
  1767. base += segs;
  1768. }
  1769. }
  1770. #ifdef CONFIG_PCI_MSI
  1771. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  1772. {
  1773. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1774. struct irq_chip *chip = irq_data_get_irq_chip(d);
  1775. struct pnv_phb *phb = container_of(chip, struct pnv_phb,
  1776. ioda.irq_chip);
  1777. int64_t rc;
  1778. rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
  1779. WARN_ON_ONCE(rc);
  1780. icp_native_eoi(d);
  1781. }
  1782. static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
  1783. {
  1784. struct irq_data *idata;
  1785. struct irq_chip *ichip;
  1786. if (phb->type != PNV_PHB_IODA2)
  1787. return;
  1788. if (!phb->ioda.irq_chip_init) {
  1789. /*
  1790. * First time we setup an MSI IRQ, we need to setup the
  1791. * corresponding IRQ chip to route correctly.
  1792. */
  1793. idata = irq_get_irq_data(virq);
  1794. ichip = irq_data_get_irq_chip(idata);
  1795. phb->ioda.irq_chip_init = 1;
  1796. phb->ioda.irq_chip = *ichip;
  1797. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  1798. }
  1799. irq_set_chip(virq, &phb->ioda.irq_chip);
  1800. }
  1801. #ifdef CONFIG_CXL_BASE
  1802. struct device_node *pnv_pci_get_phb_node(struct pci_dev *dev)
  1803. {
  1804. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1805. return of_node_get(hose->dn);
  1806. }
  1807. EXPORT_SYMBOL(pnv_pci_get_phb_node);
  1808. int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
  1809. {
  1810. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1811. struct pnv_phb *phb = hose->private_data;
  1812. struct pnv_ioda_pe *pe;
  1813. int rc;
  1814. pe = pnv_ioda_get_pe(dev);
  1815. if (!pe)
  1816. return -ENODEV;
  1817. pe_info(pe, "Switching PHB to CXL\n");
  1818. rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
  1819. if (rc)
  1820. dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
  1821. return rc;
  1822. }
  1823. EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
  1824. /* Find PHB for cxl dev and allocate MSI hwirqs?
  1825. * Returns the absolute hardware IRQ number
  1826. */
  1827. int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
  1828. {
  1829. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1830. struct pnv_phb *phb = hose->private_data;
  1831. int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
  1832. if (hwirq < 0) {
  1833. dev_warn(&dev->dev, "Failed to find a free MSI\n");
  1834. return -ENOSPC;
  1835. }
  1836. return phb->msi_base + hwirq;
  1837. }
  1838. EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
  1839. void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
  1840. {
  1841. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1842. struct pnv_phb *phb = hose->private_data;
  1843. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
  1844. }
  1845. EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
  1846. void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
  1847. struct pci_dev *dev)
  1848. {
  1849. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1850. struct pnv_phb *phb = hose->private_data;
  1851. int i, hwirq;
  1852. for (i = 1; i < CXL_IRQ_RANGES; i++) {
  1853. if (!irqs->range[i])
  1854. continue;
  1855. pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
  1856. i, irqs->offset[i],
  1857. irqs->range[i]);
  1858. hwirq = irqs->offset[i] - phb->msi_base;
  1859. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
  1860. irqs->range[i]);
  1861. }
  1862. }
  1863. EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
  1864. int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
  1865. struct pci_dev *dev, int num)
  1866. {
  1867. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1868. struct pnv_phb *phb = hose->private_data;
  1869. int i, hwirq, try;
  1870. memset(irqs, 0, sizeof(struct cxl_irq_ranges));
  1871. /* 0 is reserved for the multiplexed PSL DSI interrupt */
  1872. for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
  1873. try = num;
  1874. while (try) {
  1875. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
  1876. if (hwirq >= 0)
  1877. break;
  1878. try /= 2;
  1879. }
  1880. if (!try)
  1881. goto fail;
  1882. irqs->offset[i] = phb->msi_base + hwirq;
  1883. irqs->range[i] = try;
  1884. pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
  1885. i, irqs->offset[i], irqs->range[i]);
  1886. num -= try;
  1887. }
  1888. if (num)
  1889. goto fail;
  1890. return 0;
  1891. fail:
  1892. pnv_cxl_release_hwirq_ranges(irqs, dev);
  1893. return -ENOSPC;
  1894. }
  1895. EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
  1896. int pnv_cxl_get_irq_count(struct pci_dev *dev)
  1897. {
  1898. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1899. struct pnv_phb *phb = hose->private_data;
  1900. return phb->msi_bmp.irq_count;
  1901. }
  1902. EXPORT_SYMBOL(pnv_cxl_get_irq_count);
  1903. int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
  1904. unsigned int virq)
  1905. {
  1906. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1907. struct pnv_phb *phb = hose->private_data;
  1908. unsigned int xive_num = hwirq - phb->msi_base;
  1909. struct pnv_ioda_pe *pe;
  1910. int rc;
  1911. if (!(pe = pnv_ioda_get_pe(dev)))
  1912. return -ENODEV;
  1913. /* Assign XIVE to PE */
  1914. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  1915. if (rc) {
  1916. pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
  1917. "hwirq 0x%x XIVE 0x%x PE\n",
  1918. pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
  1919. return -EIO;
  1920. }
  1921. set_msi_irq_chip(phb, virq);
  1922. return 0;
  1923. }
  1924. EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
  1925. #endif
  1926. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  1927. unsigned int hwirq, unsigned int virq,
  1928. unsigned int is_64, struct msi_msg *msg)
  1929. {
  1930. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  1931. unsigned int xive_num = hwirq - phb->msi_base;
  1932. __be32 data;
  1933. int rc;
  1934. /* No PE assigned ? bail out ... no MSI for you ! */
  1935. if (pe == NULL)
  1936. return -ENXIO;
  1937. /* Check if we have an MVE */
  1938. if (pe->mve_number < 0)
  1939. return -ENXIO;
  1940. /* Force 32-bit MSI on some broken devices */
  1941. if (dev->no_64bit_msi)
  1942. is_64 = 0;
  1943. /* Assign XIVE to PE */
  1944. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  1945. if (rc) {
  1946. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  1947. pci_name(dev), rc, xive_num);
  1948. return -EIO;
  1949. }
  1950. if (is_64) {
  1951. __be64 addr64;
  1952. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  1953. &addr64, &data);
  1954. if (rc) {
  1955. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  1956. pci_name(dev), rc);
  1957. return -EIO;
  1958. }
  1959. msg->address_hi = be64_to_cpu(addr64) >> 32;
  1960. msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
  1961. } else {
  1962. __be32 addr32;
  1963. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  1964. &addr32, &data);
  1965. if (rc) {
  1966. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  1967. pci_name(dev), rc);
  1968. return -EIO;
  1969. }
  1970. msg->address_hi = 0;
  1971. msg->address_lo = be32_to_cpu(addr32);
  1972. }
  1973. msg->data = be32_to_cpu(data);
  1974. set_msi_irq_chip(phb, virq);
  1975. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  1976. " address=%x_%08x data=%x PE# %d\n",
  1977. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  1978. msg->address_hi, msg->address_lo, data, pe->pe_number);
  1979. return 0;
  1980. }
  1981. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  1982. {
  1983. unsigned int count;
  1984. const __be32 *prop = of_get_property(phb->hose->dn,
  1985. "ibm,opal-msi-ranges", NULL);
  1986. if (!prop) {
  1987. /* BML Fallback */
  1988. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  1989. }
  1990. if (!prop)
  1991. return;
  1992. phb->msi_base = be32_to_cpup(prop);
  1993. count = be32_to_cpup(prop + 1);
  1994. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  1995. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  1996. phb->hose->global_number);
  1997. return;
  1998. }
  1999. phb->msi_setup = pnv_pci_ioda_msi_setup;
  2000. phb->msi32_support = 1;
  2001. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  2002. count, phb->msi_base);
  2003. }
  2004. #else
  2005. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  2006. #endif /* CONFIG_PCI_MSI */
  2007. #ifdef CONFIG_PCI_IOV
  2008. static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
  2009. {
  2010. struct pci_controller *hose;
  2011. struct pnv_phb *phb;
  2012. struct resource *res;
  2013. int i;
  2014. resource_size_t size;
  2015. struct pci_dn *pdn;
  2016. int mul, total_vfs;
  2017. if (!pdev->is_physfn || pdev->is_added)
  2018. return;
  2019. hose = pci_bus_to_host(pdev->bus);
  2020. phb = hose->private_data;
  2021. pdn = pci_get_pdn(pdev);
  2022. pdn->vfs_expanded = 0;
  2023. total_vfs = pci_sriov_get_totalvfs(pdev);
  2024. pdn->m64_per_iov = 1;
  2025. mul = phb->ioda.total_pe;
  2026. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  2027. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  2028. if (!res->flags || res->parent)
  2029. continue;
  2030. if (!pnv_pci_is_mem_pref_64(res->flags)) {
  2031. dev_warn(&pdev->dev, " non M64 VF BAR%d: %pR\n",
  2032. i, res);
  2033. continue;
  2034. }
  2035. size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
  2036. /* bigger than 64M */
  2037. if (size > (1 << 26)) {
  2038. dev_info(&pdev->dev, "PowerNV: VF BAR%d: %pR IOV size is bigger than 64M, roundup power2\n",
  2039. i, res);
  2040. pdn->m64_per_iov = M64_PER_IOV;
  2041. mul = roundup_pow_of_two(total_vfs);
  2042. break;
  2043. }
  2044. }
  2045. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  2046. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  2047. if (!res->flags || res->parent)
  2048. continue;
  2049. if (!pnv_pci_is_mem_pref_64(res->flags)) {
  2050. dev_warn(&pdev->dev, "Skipping expanding VF BAR%d: %pR\n",
  2051. i, res);
  2052. continue;
  2053. }
  2054. dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
  2055. size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
  2056. res->end = res->start + size * mul - 1;
  2057. dev_dbg(&pdev->dev, " %pR\n", res);
  2058. dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
  2059. i, res, mul);
  2060. }
  2061. pdn->vfs_expanded = mul;
  2062. }
  2063. #endif /* CONFIG_PCI_IOV */
  2064. /*
  2065. * This function is supposed to be called on basis of PE from top
  2066. * to bottom style. So the the I/O or MMIO segment assigned to
  2067. * parent PE could be overrided by its child PEs if necessary.
  2068. */
  2069. static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
  2070. struct pnv_ioda_pe *pe)
  2071. {
  2072. struct pnv_phb *phb = hose->private_data;
  2073. struct pci_bus_region region;
  2074. struct resource *res;
  2075. int i, index;
  2076. int rc;
  2077. /*
  2078. * NOTE: We only care PCI bus based PE for now. For PCI
  2079. * device based PE, for example SRIOV sensitive VF should
  2080. * be figured out later.
  2081. */
  2082. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  2083. pci_bus_for_each_resource(pe->pbus, res, i) {
  2084. if (!res || !res->flags ||
  2085. res->start > res->end)
  2086. continue;
  2087. if (res->flags & IORESOURCE_IO) {
  2088. region.start = res->start - phb->ioda.io_pci_base;
  2089. region.end = res->end - phb->ioda.io_pci_base;
  2090. index = region.start / phb->ioda.io_segsize;
  2091. while (index < phb->ioda.total_pe &&
  2092. region.start <= region.end) {
  2093. phb->ioda.io_segmap[index] = pe->pe_number;
  2094. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2095. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  2096. if (rc != OPAL_SUCCESS) {
  2097. pr_err("%s: OPAL error %d when mapping IO "
  2098. "segment #%d to PE#%d\n",
  2099. __func__, rc, index, pe->pe_number);
  2100. break;
  2101. }
  2102. region.start += phb->ioda.io_segsize;
  2103. index++;
  2104. }
  2105. } else if ((res->flags & IORESOURCE_MEM) &&
  2106. !pnv_pci_is_mem_pref_64(res->flags)) {
  2107. region.start = res->start -
  2108. hose->mem_offset[0] -
  2109. phb->ioda.m32_pci_base;
  2110. region.end = res->end -
  2111. hose->mem_offset[0] -
  2112. phb->ioda.m32_pci_base;
  2113. index = region.start / phb->ioda.m32_segsize;
  2114. while (index < phb->ioda.total_pe &&
  2115. region.start <= region.end) {
  2116. phb->ioda.m32_segmap[index] = pe->pe_number;
  2117. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2118. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  2119. if (rc != OPAL_SUCCESS) {
  2120. pr_err("%s: OPAL error %d when mapping M32 "
  2121. "segment#%d to PE#%d",
  2122. __func__, rc, index, pe->pe_number);
  2123. break;
  2124. }
  2125. region.start += phb->ioda.m32_segsize;
  2126. index++;
  2127. }
  2128. }
  2129. }
  2130. }
  2131. static void pnv_pci_ioda_setup_seg(void)
  2132. {
  2133. struct pci_controller *tmp, *hose;
  2134. struct pnv_phb *phb;
  2135. struct pnv_ioda_pe *pe;
  2136. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  2137. phb = hose->private_data;
  2138. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  2139. pnv_ioda_setup_pe_seg(hose, pe);
  2140. }
  2141. }
  2142. }
  2143. static void pnv_pci_ioda_setup_DMA(void)
  2144. {
  2145. struct pci_controller *hose, *tmp;
  2146. struct pnv_phb *phb;
  2147. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  2148. pnv_ioda_setup_dma(hose->private_data);
  2149. /* Mark the PHB initialization done */
  2150. phb = hose->private_data;
  2151. phb->initialized = 1;
  2152. }
  2153. }
  2154. static void pnv_pci_ioda_create_dbgfs(void)
  2155. {
  2156. #ifdef CONFIG_DEBUG_FS
  2157. struct pci_controller *hose, *tmp;
  2158. struct pnv_phb *phb;
  2159. char name[16];
  2160. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  2161. phb = hose->private_data;
  2162. sprintf(name, "PCI%04x", hose->global_number);
  2163. phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
  2164. if (!phb->dbgfs)
  2165. pr_warning("%s: Error on creating debugfs on PHB#%x\n",
  2166. __func__, hose->global_number);
  2167. }
  2168. #endif /* CONFIG_DEBUG_FS */
  2169. }
  2170. static void pnv_pci_ioda_fixup(void)
  2171. {
  2172. pnv_pci_ioda_setup_PEs();
  2173. pnv_pci_ioda_setup_seg();
  2174. pnv_pci_ioda_setup_DMA();
  2175. pnv_pci_ioda_create_dbgfs();
  2176. #ifdef CONFIG_EEH
  2177. eeh_init();
  2178. eeh_addr_cache_build();
  2179. #endif
  2180. }
  2181. /*
  2182. * Returns the alignment for I/O or memory windows for P2P
  2183. * bridges. That actually depends on how PEs are segmented.
  2184. * For now, we return I/O or M32 segment size for PE sensitive
  2185. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  2186. * 1MiB for memory) will be returned.
  2187. *
  2188. * The current PCI bus might be put into one PE, which was
  2189. * create against the parent PCI bridge. For that case, we
  2190. * needn't enlarge the alignment so that we can save some
  2191. * resources.
  2192. */
  2193. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  2194. unsigned long type)
  2195. {
  2196. struct pci_dev *bridge;
  2197. struct pci_controller *hose = pci_bus_to_host(bus);
  2198. struct pnv_phb *phb = hose->private_data;
  2199. int num_pci_bridges = 0;
  2200. bridge = bus->self;
  2201. while (bridge) {
  2202. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  2203. num_pci_bridges++;
  2204. if (num_pci_bridges >= 2)
  2205. return 1;
  2206. }
  2207. bridge = bridge->bus->self;
  2208. }
  2209. /* We fail back to M32 if M64 isn't supported */
  2210. if (phb->ioda.m64_segsize &&
  2211. pnv_pci_is_mem_pref_64(type))
  2212. return phb->ioda.m64_segsize;
  2213. if (type & IORESOURCE_MEM)
  2214. return phb->ioda.m32_segsize;
  2215. return phb->ioda.io_segsize;
  2216. }
  2217. #ifdef CONFIG_PCI_IOV
  2218. static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
  2219. int resno)
  2220. {
  2221. struct pci_dn *pdn = pci_get_pdn(pdev);
  2222. resource_size_t align, iov_align;
  2223. iov_align = resource_size(&pdev->resource[resno]);
  2224. if (iov_align)
  2225. return iov_align;
  2226. align = pci_iov_resource_size(pdev, resno);
  2227. if (pdn->vfs_expanded)
  2228. return pdn->vfs_expanded * align;
  2229. return align;
  2230. }
  2231. #endif /* CONFIG_PCI_IOV */
  2232. /* Prevent enabling devices for which we couldn't properly
  2233. * assign a PE
  2234. */
  2235. static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
  2236. {
  2237. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  2238. struct pnv_phb *phb = hose->private_data;
  2239. struct pci_dn *pdn;
  2240. /* The function is probably called while the PEs have
  2241. * not be created yet. For example, resource reassignment
  2242. * during PCI probe period. We just skip the check if
  2243. * PEs isn't ready.
  2244. */
  2245. if (!phb->initialized)
  2246. return true;
  2247. pdn = pci_get_pdn(dev);
  2248. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  2249. return false;
  2250. return true;
  2251. }
  2252. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  2253. u32 devfn)
  2254. {
  2255. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  2256. }
  2257. static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
  2258. {
  2259. opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
  2260. OPAL_ASSERT_RESET);
  2261. }
  2262. static void __init pnv_pci_init_ioda_phb(struct device_node *np,
  2263. u64 hub_id, int ioda_type)
  2264. {
  2265. struct pci_controller *hose;
  2266. struct pnv_phb *phb;
  2267. unsigned long size, m32map_off, pemap_off, iomap_off = 0;
  2268. const __be64 *prop64;
  2269. const __be32 *prop32;
  2270. int len;
  2271. u64 phb_id;
  2272. void *aux;
  2273. long rc;
  2274. pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
  2275. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  2276. if (!prop64) {
  2277. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  2278. return;
  2279. }
  2280. phb_id = be64_to_cpup(prop64);
  2281. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  2282. phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
  2283. /* Allocate PCI controller */
  2284. phb->hose = hose = pcibios_alloc_controller(np);
  2285. if (!phb->hose) {
  2286. pr_err(" Can't allocate PCI controller for %s\n",
  2287. np->full_name);
  2288. memblock_free(__pa(phb), sizeof(struct pnv_phb));
  2289. return;
  2290. }
  2291. spin_lock_init(&phb->lock);
  2292. prop32 = of_get_property(np, "bus-range", &len);
  2293. if (prop32 && len == 8) {
  2294. hose->first_busno = be32_to_cpu(prop32[0]);
  2295. hose->last_busno = be32_to_cpu(prop32[1]);
  2296. } else {
  2297. pr_warn(" Broken <bus-range> on %s\n", np->full_name);
  2298. hose->first_busno = 0;
  2299. hose->last_busno = 0xff;
  2300. }
  2301. hose->private_data = phb;
  2302. phb->hub_id = hub_id;
  2303. phb->opal_id = phb_id;
  2304. phb->type = ioda_type;
  2305. mutex_init(&phb->ioda.pe_alloc_mutex);
  2306. /* Detect specific models for error handling */
  2307. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  2308. phb->model = PNV_PHB_MODEL_P7IOC;
  2309. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  2310. phb->model = PNV_PHB_MODEL_PHB3;
  2311. else
  2312. phb->model = PNV_PHB_MODEL_UNKNOWN;
  2313. /* Parse 32-bit and IO ranges (if any) */
  2314. pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
  2315. /* Get registers */
  2316. phb->regs = of_iomap(np, 0);
  2317. if (phb->regs == NULL)
  2318. pr_err(" Failed to map registers !\n");
  2319. /* Initialize more IODA stuff */
  2320. phb->ioda.total_pe = 1;
  2321. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  2322. if (prop32)
  2323. phb->ioda.total_pe = be32_to_cpup(prop32);
  2324. prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
  2325. if (prop32)
  2326. phb->ioda.reserved_pe = be32_to_cpup(prop32);
  2327. /* Parse 64-bit MMIO range */
  2328. pnv_ioda_parse_m64_window(phb);
  2329. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  2330. /* FW Has already off top 64k of M32 space (MSI space) */
  2331. phb->ioda.m32_size += 0x10000;
  2332. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  2333. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  2334. phb->ioda.io_size = hose->pci_io_size;
  2335. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  2336. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  2337. /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
  2338. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  2339. m32map_off = size;
  2340. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  2341. if (phb->type == PNV_PHB_IODA1) {
  2342. iomap_off = size;
  2343. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  2344. }
  2345. pemap_off = size;
  2346. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  2347. aux = memblock_virt_alloc(size, 0);
  2348. phb->ioda.pe_alloc = aux;
  2349. phb->ioda.m32_segmap = aux + m32map_off;
  2350. if (phb->type == PNV_PHB_IODA1)
  2351. phb->ioda.io_segmap = aux + iomap_off;
  2352. phb->ioda.pe_array = aux + pemap_off;
  2353. set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
  2354. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  2355. INIT_LIST_HEAD(&phb->ioda.pe_list);
  2356. mutex_init(&phb->ioda.pe_list_mutex);
  2357. /* Calculate how many 32-bit TCE segments we have */
  2358. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  2359. #if 0 /* We should really do that ... */
  2360. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  2361. window_type,
  2362. window_num,
  2363. starting_real_address,
  2364. starting_pci_address,
  2365. segment_size);
  2366. #endif
  2367. pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
  2368. phb->ioda.total_pe, phb->ioda.reserved_pe,
  2369. phb->ioda.m32_size, phb->ioda.m32_segsize);
  2370. if (phb->ioda.m64_size)
  2371. pr_info(" M64: 0x%lx [segment=0x%lx]\n",
  2372. phb->ioda.m64_size, phb->ioda.m64_segsize);
  2373. if (phb->ioda.io_size)
  2374. pr_info(" IO: 0x%x [segment=0x%x]\n",
  2375. phb->ioda.io_size, phb->ioda.io_segsize);
  2376. phb->hose->ops = &pnv_pci_ops;
  2377. phb->get_pe_state = pnv_ioda_get_pe_state;
  2378. phb->freeze_pe = pnv_ioda_freeze_pe;
  2379. phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
  2380. /* Setup RID -> PE mapping function */
  2381. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  2382. /* Setup TCEs */
  2383. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  2384. phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
  2385. phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
  2386. /* Setup shutdown function for kexec */
  2387. phb->shutdown = pnv_pci_ioda_shutdown;
  2388. /* Setup MSI support */
  2389. pnv_pci_init_ioda_msis(phb);
  2390. /*
  2391. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  2392. * to let the PCI core do resource assignment. It's supposed
  2393. * that the PCI core will do correct I/O and MMIO alignment
  2394. * for the P2P bridge bars so that each PCI bus (excluding
  2395. * the child P2P bridges) can form individual PE.
  2396. */
  2397. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  2398. pnv_pci_controller_ops.enable_device_hook = pnv_pci_enable_device_hook;
  2399. pnv_pci_controller_ops.window_alignment = pnv_pci_window_alignment;
  2400. pnv_pci_controller_ops.reset_secondary_bus = pnv_pci_reset_secondary_bus;
  2401. hose->controller_ops = pnv_pci_controller_ops;
  2402. #ifdef CONFIG_PCI_IOV
  2403. ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
  2404. ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
  2405. #endif
  2406. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  2407. /* Reset IODA tables to a clean state */
  2408. rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
  2409. if (rc)
  2410. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  2411. /* If we're running in kdump kerenl, the previous kerenl never
  2412. * shutdown PCI devices correctly. We already got IODA table
  2413. * cleaned out. So we have to issue PHB reset to stop all PCI
  2414. * transactions from previous kerenl.
  2415. */
  2416. if (is_kdump_kernel()) {
  2417. pr_info(" Issue PHB reset ...\n");
  2418. pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
  2419. pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
  2420. }
  2421. /* Remove M64 resource if we can't configure it successfully */
  2422. if (!phb->init_m64 || phb->init_m64(phb))
  2423. hose->mem_resources[1].flags = 0;
  2424. }
  2425. void __init pnv_pci_init_ioda2_phb(struct device_node *np)
  2426. {
  2427. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
  2428. }
  2429. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  2430. {
  2431. struct device_node *phbn;
  2432. const __be64 *prop64;
  2433. u64 hub_id;
  2434. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  2435. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  2436. if (!prop64) {
  2437. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  2438. return;
  2439. }
  2440. hub_id = be64_to_cpup(prop64);
  2441. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  2442. /* Count child PHBs */
  2443. for_each_child_of_node(np, phbn) {
  2444. /* Look for IODA1 PHBs */
  2445. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  2446. pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
  2447. }
  2448. }