pci-ioda.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894
  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 <linux/iommu.h>
  25. #include <linux/rculist.h>
  26. #include <linux/sizes.h>
  27. #include <asm/sections.h>
  28. #include <asm/io.h>
  29. #include <asm/prom.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/machdep.h>
  32. #include <asm/msi_bitmap.h>
  33. #include <asm/ppc-pci.h>
  34. #include <asm/opal.h>
  35. #include <asm/iommu.h>
  36. #include <asm/tce.h>
  37. #include <asm/xics.h>
  38. #include <asm/debug.h>
  39. #include <asm/firmware.h>
  40. #include <asm/pnv-pci.h>
  41. #include <asm/mmzone.h>
  42. #include <misc/cxl-base.h>
  43. #include "powernv.h"
  44. #include "pci.h"
  45. #define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
  46. #define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
  47. #define PNV_IODA1_DMA32_SEGSIZE 0x10000000
  48. #define POWERNV_IOMMU_DEFAULT_LEVELS 1
  49. #define POWERNV_IOMMU_MAX_LEVELS 5
  50. static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU" };
  51. static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl);
  52. void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
  53. const char *fmt, ...)
  54. {
  55. struct va_format vaf;
  56. va_list args;
  57. char pfix[32];
  58. va_start(args, fmt);
  59. vaf.fmt = fmt;
  60. vaf.va = &args;
  61. if (pe->flags & PNV_IODA_PE_DEV)
  62. strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
  63. else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
  64. sprintf(pfix, "%04x:%02x ",
  65. pci_domain_nr(pe->pbus), pe->pbus->number);
  66. #ifdef CONFIG_PCI_IOV
  67. else if (pe->flags & PNV_IODA_PE_VF)
  68. sprintf(pfix, "%04x:%02x:%2x.%d",
  69. pci_domain_nr(pe->parent_dev->bus),
  70. (pe->rid & 0xff00) >> 8,
  71. PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
  72. #endif /* CONFIG_PCI_IOV*/
  73. printk("%spci %s: [PE# %.2x] %pV",
  74. level, pfix, pe->pe_number, &vaf);
  75. va_end(args);
  76. }
  77. static bool pnv_iommu_bypass_disabled __read_mostly;
  78. static int __init iommu_setup(char *str)
  79. {
  80. if (!str)
  81. return -EINVAL;
  82. while (*str) {
  83. if (!strncmp(str, "nobypass", 8)) {
  84. pnv_iommu_bypass_disabled = true;
  85. pr_info("PowerNV: IOMMU bypass window disabled.\n");
  86. break;
  87. }
  88. str += strcspn(str, ",");
  89. if (*str == ',')
  90. str++;
  91. }
  92. return 0;
  93. }
  94. early_param("iommu", iommu_setup);
  95. static inline bool pnv_pci_is_m64(struct pnv_phb *phb, struct resource *r)
  96. {
  97. /*
  98. * WARNING: We cannot rely on the resource flags. The Linux PCI
  99. * allocation code sometimes decides to put a 64-bit prefetchable
  100. * BAR in the 32-bit window, so we have to compare the addresses.
  101. *
  102. * For simplicity we only test resource start.
  103. */
  104. return (r->start >= phb->ioda.m64_base &&
  105. r->start < (phb->ioda.m64_base + phb->ioda.m64_size));
  106. }
  107. static inline bool pnv_pci_is_m64_flags(unsigned long resource_flags)
  108. {
  109. unsigned long flags = (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
  110. return (resource_flags & flags) == flags;
  111. }
  112. static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
  113. {
  114. s64 rc;
  115. phb->ioda.pe_array[pe_no].phb = phb;
  116. phb->ioda.pe_array[pe_no].pe_number = pe_no;
  117. /*
  118. * Clear the PE frozen state as it might be put into frozen state
  119. * in the last PCI remove path. It's not harmful to do so when the
  120. * PE is already in unfrozen state.
  121. */
  122. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
  123. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  124. if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
  125. pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
  126. __func__, rc, phb->hose->global_number, pe_no);
  127. return &phb->ioda.pe_array[pe_no];
  128. }
  129. static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
  130. {
  131. if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
  132. pr_warn("%s: Invalid PE %x on PHB#%x\n",
  133. __func__, pe_no, phb->hose->global_number);
  134. return;
  135. }
  136. if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
  137. pr_debug("%s: PE %x was reserved on PHB#%x\n",
  138. __func__, pe_no, phb->hose->global_number);
  139. pnv_ioda_init_pe(phb, pe_no);
  140. }
  141. static struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb)
  142. {
  143. long pe;
  144. for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
  145. if (!test_and_set_bit(pe, phb->ioda.pe_alloc))
  146. return pnv_ioda_init_pe(phb, pe);
  147. }
  148. return NULL;
  149. }
  150. static void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
  151. {
  152. struct pnv_phb *phb = pe->phb;
  153. unsigned int pe_num = pe->pe_number;
  154. WARN_ON(pe->pdev);
  155. memset(pe, 0, sizeof(struct pnv_ioda_pe));
  156. clear_bit(pe_num, phb->ioda.pe_alloc);
  157. }
  158. /* The default M64 BAR is shared by all PEs */
  159. static int pnv_ioda2_init_m64(struct pnv_phb *phb)
  160. {
  161. const char *desc;
  162. struct resource *r;
  163. s64 rc;
  164. /* Configure the default M64 BAR */
  165. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  166. OPAL_M64_WINDOW_TYPE,
  167. phb->ioda.m64_bar_idx,
  168. phb->ioda.m64_base,
  169. 0, /* unused */
  170. phb->ioda.m64_size);
  171. if (rc != OPAL_SUCCESS) {
  172. desc = "configuring";
  173. goto fail;
  174. }
  175. /* Enable the default M64 BAR */
  176. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  177. OPAL_M64_WINDOW_TYPE,
  178. phb->ioda.m64_bar_idx,
  179. OPAL_ENABLE_M64_SPLIT);
  180. if (rc != OPAL_SUCCESS) {
  181. desc = "enabling";
  182. goto fail;
  183. }
  184. /*
  185. * Exclude the segments for reserved and root bus PE, which
  186. * are first or last two PEs.
  187. */
  188. r = &phb->hose->mem_resources[1];
  189. if (phb->ioda.reserved_pe_idx == 0)
  190. r->start += (2 * phb->ioda.m64_segsize);
  191. else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
  192. r->end -= (2 * phb->ioda.m64_segsize);
  193. else
  194. pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",
  195. phb->ioda.reserved_pe_idx);
  196. return 0;
  197. fail:
  198. pr_warn(" Failure %lld %s M64 BAR#%d\n",
  199. rc, desc, phb->ioda.m64_bar_idx);
  200. opal_pci_phb_mmio_enable(phb->opal_id,
  201. OPAL_M64_WINDOW_TYPE,
  202. phb->ioda.m64_bar_idx,
  203. OPAL_DISABLE_M64);
  204. return -EIO;
  205. }
  206. static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
  207. unsigned long *pe_bitmap)
  208. {
  209. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  210. struct pnv_phb *phb = hose->private_data;
  211. struct resource *r;
  212. resource_size_t base, sgsz, start, end;
  213. int segno, i;
  214. base = phb->ioda.m64_base;
  215. sgsz = phb->ioda.m64_segsize;
  216. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  217. r = &pdev->resource[i];
  218. if (!r->parent || !pnv_pci_is_m64(phb, r))
  219. continue;
  220. start = _ALIGN_DOWN(r->start - base, sgsz);
  221. end = _ALIGN_UP(r->end - base, sgsz);
  222. for (segno = start / sgsz; segno < end / sgsz; segno++) {
  223. if (pe_bitmap)
  224. set_bit(segno, pe_bitmap);
  225. else
  226. pnv_ioda_reserve_pe(phb, segno);
  227. }
  228. }
  229. }
  230. static int pnv_ioda1_init_m64(struct pnv_phb *phb)
  231. {
  232. struct resource *r;
  233. int index;
  234. /*
  235. * There are 16 M64 BARs, each of which has 8 segments. So
  236. * there are as many M64 segments as the maximum number of
  237. * PEs, which is 128.
  238. */
  239. for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
  240. unsigned long base, segsz = phb->ioda.m64_segsize;
  241. int64_t rc;
  242. base = phb->ioda.m64_base +
  243. index * PNV_IODA1_M64_SEGS * segsz;
  244. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  245. OPAL_M64_WINDOW_TYPE, index, base, 0,
  246. PNV_IODA1_M64_SEGS * segsz);
  247. if (rc != OPAL_SUCCESS) {
  248. pr_warn(" Error %lld setting M64 PHB#%x-BAR#%d\n",
  249. rc, phb->hose->global_number, index);
  250. goto fail;
  251. }
  252. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  253. OPAL_M64_WINDOW_TYPE, index,
  254. OPAL_ENABLE_M64_SPLIT);
  255. if (rc != OPAL_SUCCESS) {
  256. pr_warn(" Error %lld enabling M64 PHB#%x-BAR#%d\n",
  257. rc, phb->hose->global_number, index);
  258. goto fail;
  259. }
  260. }
  261. /*
  262. * Exclude the segments for reserved and root bus PE, which
  263. * are first or last two PEs.
  264. */
  265. r = &phb->hose->mem_resources[1];
  266. if (phb->ioda.reserved_pe_idx == 0)
  267. r->start += (2 * phb->ioda.m64_segsize);
  268. else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
  269. r->end -= (2 * phb->ioda.m64_segsize);
  270. else
  271. WARN(1, "Wrong reserved PE#%x on PHB#%x\n",
  272. phb->ioda.reserved_pe_idx, phb->hose->global_number);
  273. return 0;
  274. fail:
  275. for ( ; index >= 0; index--)
  276. opal_pci_phb_mmio_enable(phb->opal_id,
  277. OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
  278. return -EIO;
  279. }
  280. static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
  281. unsigned long *pe_bitmap,
  282. bool all)
  283. {
  284. struct pci_dev *pdev;
  285. list_for_each_entry(pdev, &bus->devices, bus_list) {
  286. pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
  287. if (all && pdev->subordinate)
  288. pnv_ioda_reserve_m64_pe(pdev->subordinate,
  289. pe_bitmap, all);
  290. }
  291. }
  292. static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
  293. {
  294. struct pci_controller *hose = pci_bus_to_host(bus);
  295. struct pnv_phb *phb = hose->private_data;
  296. struct pnv_ioda_pe *master_pe, *pe;
  297. unsigned long size, *pe_alloc;
  298. int i;
  299. /* Root bus shouldn't use M64 */
  300. if (pci_is_root_bus(bus))
  301. return NULL;
  302. /* Allocate bitmap */
  303. size = _ALIGN_UP(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
  304. pe_alloc = kzalloc(size, GFP_KERNEL);
  305. if (!pe_alloc) {
  306. pr_warn("%s: Out of memory !\n",
  307. __func__);
  308. return NULL;
  309. }
  310. /* Figure out reserved PE numbers by the PE */
  311. pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
  312. /*
  313. * the current bus might not own M64 window and that's all
  314. * contributed by its child buses. For the case, we needn't
  315. * pick M64 dependent PE#.
  316. */
  317. if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
  318. kfree(pe_alloc);
  319. return NULL;
  320. }
  321. /*
  322. * Figure out the master PE and put all slave PEs to master
  323. * PE's list to form compound PE.
  324. */
  325. master_pe = NULL;
  326. i = -1;
  327. while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
  328. phb->ioda.total_pe_num) {
  329. pe = &phb->ioda.pe_array[i];
  330. phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
  331. if (!master_pe) {
  332. pe->flags |= PNV_IODA_PE_MASTER;
  333. INIT_LIST_HEAD(&pe->slaves);
  334. master_pe = pe;
  335. } else {
  336. pe->flags |= PNV_IODA_PE_SLAVE;
  337. pe->master = master_pe;
  338. list_add_tail(&pe->list, &master_pe->slaves);
  339. }
  340. /*
  341. * P7IOC supports M64DT, which helps mapping M64 segment
  342. * to one particular PE#. However, PHB3 has fixed mapping
  343. * between M64 segment and PE#. In order to have same logic
  344. * for P7IOC and PHB3, we enforce fixed mapping between M64
  345. * segment and PE# on P7IOC.
  346. */
  347. if (phb->type == PNV_PHB_IODA1) {
  348. int64_t rc;
  349. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  350. pe->pe_number, OPAL_M64_WINDOW_TYPE,
  351. pe->pe_number / PNV_IODA1_M64_SEGS,
  352. pe->pe_number % PNV_IODA1_M64_SEGS);
  353. if (rc != OPAL_SUCCESS)
  354. pr_warn("%s: Error %lld mapping M64 for PHB#%x-PE#%x\n",
  355. __func__, rc, phb->hose->global_number,
  356. pe->pe_number);
  357. }
  358. }
  359. kfree(pe_alloc);
  360. return master_pe;
  361. }
  362. static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
  363. {
  364. struct pci_controller *hose = phb->hose;
  365. struct device_node *dn = hose->dn;
  366. struct resource *res;
  367. u32 m64_range[2], i;
  368. const __be32 *r;
  369. u64 pci_addr;
  370. if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
  371. pr_info(" Not support M64 window\n");
  372. return;
  373. }
  374. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  375. pr_info(" Firmware too old to support M64 window\n");
  376. return;
  377. }
  378. r = of_get_property(dn, "ibm,opal-m64-window", NULL);
  379. if (!r) {
  380. pr_info(" No <ibm,opal-m64-window> on %s\n",
  381. dn->full_name);
  382. return;
  383. }
  384. /*
  385. * Find the available M64 BAR range and pickup the last one for
  386. * covering the whole 64-bits space. We support only one range.
  387. */
  388. if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
  389. m64_range, 2)) {
  390. /* In absence of the property, assume 0..15 */
  391. m64_range[0] = 0;
  392. m64_range[1] = 16;
  393. }
  394. /* We only support 64 bits in our allocator */
  395. if (m64_range[1] > 63) {
  396. pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
  397. __func__, m64_range[1], phb->hose->global_number);
  398. m64_range[1] = 63;
  399. }
  400. /* Empty range, no m64 */
  401. if (m64_range[1] <= m64_range[0]) {
  402. pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
  403. __func__, phb->hose->global_number);
  404. return;
  405. }
  406. /* Configure M64 informations */
  407. res = &hose->mem_resources[1];
  408. res->name = dn->full_name;
  409. res->start = of_translate_address(dn, r + 2);
  410. res->end = res->start + of_read_number(r + 4, 2) - 1;
  411. res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
  412. pci_addr = of_read_number(r, 2);
  413. hose->mem_offset[1] = res->start - pci_addr;
  414. phb->ioda.m64_size = resource_size(res);
  415. phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
  416. phb->ioda.m64_base = pci_addr;
  417. /* This lines up nicely with the display from processing OF ranges */
  418. pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
  419. res->start, res->end, pci_addr, m64_range[0],
  420. m64_range[0] + m64_range[1] - 1);
  421. /* Mark all M64 used up by default */
  422. phb->ioda.m64_bar_alloc = (unsigned long)-1;
  423. /* Use last M64 BAR to cover M64 window */
  424. m64_range[1]--;
  425. phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
  426. pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
  427. /* Mark remaining ones free */
  428. for (i = m64_range[0]; i < m64_range[1]; i++)
  429. clear_bit(i, &phb->ioda.m64_bar_alloc);
  430. /*
  431. * Setup init functions for M64 based on IODA version, IODA3 uses
  432. * the IODA2 code.
  433. */
  434. if (phb->type == PNV_PHB_IODA1)
  435. phb->init_m64 = pnv_ioda1_init_m64;
  436. else
  437. phb->init_m64 = pnv_ioda2_init_m64;
  438. phb->reserve_m64_pe = pnv_ioda_reserve_m64_pe;
  439. phb->pick_m64_pe = pnv_ioda_pick_m64_pe;
  440. }
  441. static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
  442. {
  443. struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
  444. struct pnv_ioda_pe *slave;
  445. s64 rc;
  446. /* Fetch master PE */
  447. if (pe->flags & PNV_IODA_PE_SLAVE) {
  448. pe = pe->master;
  449. if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
  450. return;
  451. pe_no = pe->pe_number;
  452. }
  453. /* Freeze master PE */
  454. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  455. pe_no,
  456. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  457. if (rc != OPAL_SUCCESS) {
  458. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  459. __func__, rc, phb->hose->global_number, pe_no);
  460. return;
  461. }
  462. /* Freeze slave PEs */
  463. if (!(pe->flags & PNV_IODA_PE_MASTER))
  464. return;
  465. list_for_each_entry(slave, &pe->slaves, list) {
  466. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  467. slave->pe_number,
  468. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  469. if (rc != OPAL_SUCCESS)
  470. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  471. __func__, rc, phb->hose->global_number,
  472. slave->pe_number);
  473. }
  474. }
  475. static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
  476. {
  477. struct pnv_ioda_pe *pe, *slave;
  478. s64 rc;
  479. /* Find master PE */
  480. pe = &phb->ioda.pe_array[pe_no];
  481. if (pe->flags & PNV_IODA_PE_SLAVE) {
  482. pe = pe->master;
  483. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  484. pe_no = pe->pe_number;
  485. }
  486. /* Clear frozen state for master PE */
  487. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
  488. if (rc != OPAL_SUCCESS) {
  489. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  490. __func__, rc, opt, phb->hose->global_number, pe_no);
  491. return -EIO;
  492. }
  493. if (!(pe->flags & PNV_IODA_PE_MASTER))
  494. return 0;
  495. /* Clear frozen state for slave PEs */
  496. list_for_each_entry(slave, &pe->slaves, list) {
  497. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  498. slave->pe_number,
  499. opt);
  500. if (rc != OPAL_SUCCESS) {
  501. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  502. __func__, rc, opt, phb->hose->global_number,
  503. slave->pe_number);
  504. return -EIO;
  505. }
  506. }
  507. return 0;
  508. }
  509. static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
  510. {
  511. struct pnv_ioda_pe *slave, *pe;
  512. u8 fstate, state;
  513. __be16 pcierr;
  514. s64 rc;
  515. /* Sanity check on PE number */
  516. if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
  517. return OPAL_EEH_STOPPED_PERM_UNAVAIL;
  518. /*
  519. * Fetch the master PE and the PE instance might be
  520. * not initialized yet.
  521. */
  522. pe = &phb->ioda.pe_array[pe_no];
  523. if (pe->flags & PNV_IODA_PE_SLAVE) {
  524. pe = pe->master;
  525. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  526. pe_no = pe->pe_number;
  527. }
  528. /* Check the master PE */
  529. rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
  530. &state, &pcierr, NULL);
  531. if (rc != OPAL_SUCCESS) {
  532. pr_warn("%s: Failure %lld getting "
  533. "PHB#%x-PE#%x state\n",
  534. __func__, rc,
  535. phb->hose->global_number, pe_no);
  536. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  537. }
  538. /* Check the slave PE */
  539. if (!(pe->flags & PNV_IODA_PE_MASTER))
  540. return state;
  541. list_for_each_entry(slave, &pe->slaves, list) {
  542. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  543. slave->pe_number,
  544. &fstate,
  545. &pcierr,
  546. NULL);
  547. if (rc != OPAL_SUCCESS) {
  548. pr_warn("%s: Failure %lld getting "
  549. "PHB#%x-PE#%x state\n",
  550. __func__, rc,
  551. phb->hose->global_number, slave->pe_number);
  552. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  553. }
  554. /*
  555. * Override the result based on the ascending
  556. * priority.
  557. */
  558. if (fstate > state)
  559. state = fstate;
  560. }
  561. return state;
  562. }
  563. /* Currently those 2 are only used when MSIs are enabled, this will change
  564. * but in the meantime, we need to protect them to avoid warnings
  565. */
  566. #ifdef CONFIG_PCI_MSI
  567. struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
  568. {
  569. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  570. struct pnv_phb *phb = hose->private_data;
  571. struct pci_dn *pdn = pci_get_pdn(dev);
  572. if (!pdn)
  573. return NULL;
  574. if (pdn->pe_number == IODA_INVALID_PE)
  575. return NULL;
  576. return &phb->ioda.pe_array[pdn->pe_number];
  577. }
  578. #endif /* CONFIG_PCI_MSI */
  579. static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
  580. struct pnv_ioda_pe *parent,
  581. struct pnv_ioda_pe *child,
  582. bool is_add)
  583. {
  584. const char *desc = is_add ? "adding" : "removing";
  585. uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
  586. OPAL_REMOVE_PE_FROM_DOMAIN;
  587. struct pnv_ioda_pe *slave;
  588. long rc;
  589. /* Parent PE affects child PE */
  590. rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
  591. child->pe_number, op);
  592. if (rc != OPAL_SUCCESS) {
  593. pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
  594. rc, desc);
  595. return -ENXIO;
  596. }
  597. if (!(child->flags & PNV_IODA_PE_MASTER))
  598. return 0;
  599. /* Compound case: parent PE affects slave PEs */
  600. list_for_each_entry(slave, &child->slaves, list) {
  601. rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
  602. slave->pe_number, op);
  603. if (rc != OPAL_SUCCESS) {
  604. pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
  605. rc, desc);
  606. return -ENXIO;
  607. }
  608. }
  609. return 0;
  610. }
  611. static int pnv_ioda_set_peltv(struct pnv_phb *phb,
  612. struct pnv_ioda_pe *pe,
  613. bool is_add)
  614. {
  615. struct pnv_ioda_pe *slave;
  616. struct pci_dev *pdev = NULL;
  617. int ret;
  618. /*
  619. * Clear PE frozen state. If it's master PE, we need
  620. * clear slave PE frozen state as well.
  621. */
  622. if (is_add) {
  623. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  624. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  625. if (pe->flags & PNV_IODA_PE_MASTER) {
  626. list_for_each_entry(slave, &pe->slaves, list)
  627. opal_pci_eeh_freeze_clear(phb->opal_id,
  628. slave->pe_number,
  629. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  630. }
  631. }
  632. /*
  633. * Associate PE in PELT. We need add the PE into the
  634. * corresponding PELT-V as well. Otherwise, the error
  635. * originated from the PE might contribute to other
  636. * PEs.
  637. */
  638. ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
  639. if (ret)
  640. return ret;
  641. /* For compound PEs, any one affects all of them */
  642. if (pe->flags & PNV_IODA_PE_MASTER) {
  643. list_for_each_entry(slave, &pe->slaves, list) {
  644. ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
  645. if (ret)
  646. return ret;
  647. }
  648. }
  649. if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
  650. pdev = pe->pbus->self;
  651. else if (pe->flags & PNV_IODA_PE_DEV)
  652. pdev = pe->pdev->bus->self;
  653. #ifdef CONFIG_PCI_IOV
  654. else if (pe->flags & PNV_IODA_PE_VF)
  655. pdev = pe->parent_dev;
  656. #endif /* CONFIG_PCI_IOV */
  657. while (pdev) {
  658. struct pci_dn *pdn = pci_get_pdn(pdev);
  659. struct pnv_ioda_pe *parent;
  660. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  661. parent = &phb->ioda.pe_array[pdn->pe_number];
  662. ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
  663. if (ret)
  664. return ret;
  665. }
  666. pdev = pdev->bus->self;
  667. }
  668. return 0;
  669. }
  670. static int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  671. {
  672. struct pci_dev *parent;
  673. uint8_t bcomp, dcomp, fcomp;
  674. int64_t rc;
  675. long rid_end, rid;
  676. /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
  677. if (pe->pbus) {
  678. int count;
  679. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  680. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  681. parent = pe->pbus->self;
  682. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  683. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  684. else
  685. count = 1;
  686. switch(count) {
  687. case 1: bcomp = OpalPciBusAll; break;
  688. case 2: bcomp = OpalPciBus7Bits; break;
  689. case 4: bcomp = OpalPciBus6Bits; break;
  690. case 8: bcomp = OpalPciBus5Bits; break;
  691. case 16: bcomp = OpalPciBus4Bits; break;
  692. case 32: bcomp = OpalPciBus3Bits; break;
  693. default:
  694. dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
  695. count);
  696. /* Do an exact match only */
  697. bcomp = OpalPciBusAll;
  698. }
  699. rid_end = pe->rid + (count << 8);
  700. } else {
  701. #ifdef CONFIG_PCI_IOV
  702. if (pe->flags & PNV_IODA_PE_VF)
  703. parent = pe->parent_dev;
  704. else
  705. #endif
  706. parent = pe->pdev->bus->self;
  707. bcomp = OpalPciBusAll;
  708. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  709. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  710. rid_end = pe->rid + 1;
  711. }
  712. /* Clear the reverse map */
  713. for (rid = pe->rid; rid < rid_end; rid++)
  714. phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
  715. /* Release from all parents PELT-V */
  716. while (parent) {
  717. struct pci_dn *pdn = pci_get_pdn(parent);
  718. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  719. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  720. pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
  721. /* XXX What to do in case of error ? */
  722. }
  723. parent = parent->bus->self;
  724. }
  725. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  726. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  727. /* Disassociate PE in PELT */
  728. rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
  729. pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
  730. if (rc)
  731. pe_warn(pe, "OPAL error %ld remove self from PELTV\n", rc);
  732. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  733. bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
  734. if (rc)
  735. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  736. pe->pbus = NULL;
  737. pe->pdev = NULL;
  738. #ifdef CONFIG_PCI_IOV
  739. pe->parent_dev = NULL;
  740. #endif
  741. return 0;
  742. }
  743. static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  744. {
  745. struct pci_dev *parent;
  746. uint8_t bcomp, dcomp, fcomp;
  747. long rc, rid_end, rid;
  748. /* Bus validation ? */
  749. if (pe->pbus) {
  750. int count;
  751. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  752. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  753. parent = pe->pbus->self;
  754. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  755. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  756. else
  757. count = 1;
  758. switch(count) {
  759. case 1: bcomp = OpalPciBusAll; break;
  760. case 2: bcomp = OpalPciBus7Bits; break;
  761. case 4: bcomp = OpalPciBus6Bits; break;
  762. case 8: bcomp = OpalPciBus5Bits; break;
  763. case 16: bcomp = OpalPciBus4Bits; break;
  764. case 32: bcomp = OpalPciBus3Bits; break;
  765. default:
  766. dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
  767. count);
  768. /* Do an exact match only */
  769. bcomp = OpalPciBusAll;
  770. }
  771. rid_end = pe->rid + (count << 8);
  772. } else {
  773. #ifdef CONFIG_PCI_IOV
  774. if (pe->flags & PNV_IODA_PE_VF)
  775. parent = pe->parent_dev;
  776. else
  777. #endif /* CONFIG_PCI_IOV */
  778. parent = pe->pdev->bus->self;
  779. bcomp = OpalPciBusAll;
  780. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  781. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  782. rid_end = pe->rid + 1;
  783. }
  784. /*
  785. * Associate PE in PELT. We need add the PE into the
  786. * corresponding PELT-V as well. Otherwise, the error
  787. * originated from the PE might contribute to other
  788. * PEs.
  789. */
  790. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  791. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  792. if (rc) {
  793. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  794. return -ENXIO;
  795. }
  796. /*
  797. * Configure PELTV. NPUs don't have a PELTV table so skip
  798. * configuration on them.
  799. */
  800. if (phb->type != PNV_PHB_NPU)
  801. pnv_ioda_set_peltv(phb, pe, true);
  802. /* Setup reverse map */
  803. for (rid = pe->rid; rid < rid_end; rid++)
  804. phb->ioda.pe_rmap[rid] = pe->pe_number;
  805. /* Setup one MVTs on IODA1 */
  806. if (phb->type != PNV_PHB_IODA1) {
  807. pe->mve_number = 0;
  808. goto out;
  809. }
  810. pe->mve_number = pe->pe_number;
  811. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
  812. if (rc != OPAL_SUCCESS) {
  813. pe_err(pe, "OPAL error %ld setting up MVE %x\n",
  814. rc, pe->mve_number);
  815. pe->mve_number = -1;
  816. } else {
  817. rc = opal_pci_set_mve_enable(phb->opal_id,
  818. pe->mve_number, OPAL_ENABLE_MVE);
  819. if (rc) {
  820. pe_err(pe, "OPAL error %ld enabling MVE %x\n",
  821. rc, pe->mve_number);
  822. pe->mve_number = -1;
  823. }
  824. }
  825. out:
  826. return 0;
  827. }
  828. #ifdef CONFIG_PCI_IOV
  829. static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
  830. {
  831. struct pci_dn *pdn = pci_get_pdn(dev);
  832. int i;
  833. struct resource *res, res2;
  834. resource_size_t size;
  835. u16 num_vfs;
  836. if (!dev->is_physfn)
  837. return -EINVAL;
  838. /*
  839. * "offset" is in VFs. The M64 windows are sized so that when they
  840. * are segmented, each segment is the same size as the IOV BAR.
  841. * Each segment is in a separate PE, and the high order bits of the
  842. * address are the PE number. Therefore, each VF's BAR is in a
  843. * separate PE, and changing the IOV BAR start address changes the
  844. * range of PEs the VFs are in.
  845. */
  846. num_vfs = pdn->num_vfs;
  847. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  848. res = &dev->resource[i + PCI_IOV_RESOURCES];
  849. if (!res->flags || !res->parent)
  850. continue;
  851. /*
  852. * The actual IOV BAR range is determined by the start address
  853. * and the actual size for num_vfs VFs BAR. This check is to
  854. * make sure that after shifting, the range will not overlap
  855. * with another device.
  856. */
  857. size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
  858. res2.flags = res->flags;
  859. res2.start = res->start + (size * offset);
  860. res2.end = res2.start + (size * num_vfs) - 1;
  861. if (res2.end > res->end) {
  862. dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
  863. i, &res2, res, num_vfs, offset);
  864. return -EBUSY;
  865. }
  866. }
  867. /*
  868. * After doing so, there would be a "hole" in the /proc/iomem when
  869. * offset is a positive value. It looks like the device return some
  870. * mmio back to the system, which actually no one could use it.
  871. */
  872. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  873. res = &dev->resource[i + PCI_IOV_RESOURCES];
  874. if (!res->flags || !res->parent)
  875. continue;
  876. size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
  877. res2 = *res;
  878. res->start += size * offset;
  879. dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
  880. i, &res2, res, (offset > 0) ? "En" : "Dis",
  881. num_vfs, offset);
  882. pci_update_resource(dev, i + PCI_IOV_RESOURCES);
  883. }
  884. return 0;
  885. }
  886. #endif /* CONFIG_PCI_IOV */
  887. static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  888. {
  889. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  890. struct pnv_phb *phb = hose->private_data;
  891. struct pci_dn *pdn = pci_get_pdn(dev);
  892. struct pnv_ioda_pe *pe;
  893. if (!pdn) {
  894. pr_err("%s: Device tree node not associated properly\n",
  895. pci_name(dev));
  896. return NULL;
  897. }
  898. if (pdn->pe_number != IODA_INVALID_PE)
  899. return NULL;
  900. pe = pnv_ioda_alloc_pe(phb);
  901. if (!pe) {
  902. pr_warning("%s: Not enough PE# available, disabling device\n",
  903. pci_name(dev));
  904. return NULL;
  905. }
  906. /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
  907. * pointer in the PE data structure, both should be destroyed at the
  908. * same time. However, this needs to be looked at more closely again
  909. * once we actually start removing things (Hotplug, SR-IOV, ...)
  910. *
  911. * At some point we want to remove the PDN completely anyways
  912. */
  913. pci_dev_get(dev);
  914. pdn->pcidev = dev;
  915. pdn->pe_number = pe->pe_number;
  916. pe->flags = PNV_IODA_PE_DEV;
  917. pe->pdev = dev;
  918. pe->pbus = NULL;
  919. pe->mve_number = -1;
  920. pe->rid = dev->bus->number << 8 | pdn->devfn;
  921. pe_info(pe, "Associated device to PE\n");
  922. if (pnv_ioda_configure_pe(phb, pe)) {
  923. /* XXX What do we do here ? */
  924. pnv_ioda_free_pe(pe);
  925. pdn->pe_number = IODA_INVALID_PE;
  926. pe->pdev = NULL;
  927. pci_dev_put(dev);
  928. return NULL;
  929. }
  930. /* Put PE to the list */
  931. list_add_tail(&pe->list, &phb->ioda.pe_list);
  932. return pe;
  933. }
  934. static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
  935. {
  936. struct pci_dev *dev;
  937. list_for_each_entry(dev, &bus->devices, bus_list) {
  938. struct pci_dn *pdn = pci_get_pdn(dev);
  939. if (pdn == NULL) {
  940. pr_warn("%s: No device node associated with device !\n",
  941. pci_name(dev));
  942. continue;
  943. }
  944. /*
  945. * In partial hotplug case, the PCI device might be still
  946. * associated with the PE and needn't attach it to the PE
  947. * again.
  948. */
  949. if (pdn->pe_number != IODA_INVALID_PE)
  950. continue;
  951. pe->device_count++;
  952. pdn->pcidev = dev;
  953. pdn->pe_number = pe->pe_number;
  954. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  955. pnv_ioda_setup_same_PE(dev->subordinate, pe);
  956. }
  957. }
  958. /*
  959. * There're 2 types of PCI bus sensitive PEs: One that is compromised of
  960. * single PCI bus. Another one that contains the primary PCI bus and its
  961. * subordinate PCI devices and buses. The second type of PE is normally
  962. * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
  963. */
  964. static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
  965. {
  966. struct pci_controller *hose = pci_bus_to_host(bus);
  967. struct pnv_phb *phb = hose->private_data;
  968. struct pnv_ioda_pe *pe = NULL;
  969. unsigned int pe_num;
  970. /*
  971. * In partial hotplug case, the PE instance might be still alive.
  972. * We should reuse it instead of allocating a new one.
  973. */
  974. pe_num = phb->ioda.pe_rmap[bus->number << 8];
  975. if (pe_num != IODA_INVALID_PE) {
  976. pe = &phb->ioda.pe_array[pe_num];
  977. pnv_ioda_setup_same_PE(bus, pe);
  978. return NULL;
  979. }
  980. /* PE number for root bus should have been reserved */
  981. if (pci_is_root_bus(bus) &&
  982. phb->ioda.root_pe_idx != IODA_INVALID_PE)
  983. pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
  984. /* Check if PE is determined by M64 */
  985. if (!pe && phb->pick_m64_pe)
  986. pe = phb->pick_m64_pe(bus, all);
  987. /* The PE number isn't pinned by M64 */
  988. if (!pe)
  989. pe = pnv_ioda_alloc_pe(phb);
  990. if (!pe) {
  991. pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
  992. __func__, pci_domain_nr(bus), bus->number);
  993. return NULL;
  994. }
  995. pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
  996. pe->pbus = bus;
  997. pe->pdev = NULL;
  998. pe->mve_number = -1;
  999. pe->rid = bus->busn_res.start << 8;
  1000. if (all)
  1001. pe_info(pe, "Secondary bus %d..%d associated with PE#%x\n",
  1002. bus->busn_res.start, bus->busn_res.end, pe->pe_number);
  1003. else
  1004. pe_info(pe, "Secondary bus %d associated with PE#%x\n",
  1005. bus->busn_res.start, pe->pe_number);
  1006. if (pnv_ioda_configure_pe(phb, pe)) {
  1007. /* XXX What do we do here ? */
  1008. pnv_ioda_free_pe(pe);
  1009. pe->pbus = NULL;
  1010. return NULL;
  1011. }
  1012. /* Associate it with all child devices */
  1013. pnv_ioda_setup_same_PE(bus, pe);
  1014. /* Put PE to the list */
  1015. list_add_tail(&pe->list, &phb->ioda.pe_list);
  1016. return pe;
  1017. }
  1018. static struct pnv_ioda_pe *pnv_ioda_setup_npu_PE(struct pci_dev *npu_pdev)
  1019. {
  1020. int pe_num, found_pe = false, rc;
  1021. long rid;
  1022. struct pnv_ioda_pe *pe;
  1023. struct pci_dev *gpu_pdev;
  1024. struct pci_dn *npu_pdn;
  1025. struct pci_controller *hose = pci_bus_to_host(npu_pdev->bus);
  1026. struct pnv_phb *phb = hose->private_data;
  1027. /*
  1028. * Due to a hardware errata PE#0 on the NPU is reserved for
  1029. * error handling. This means we only have three PEs remaining
  1030. * which need to be assigned to four links, implying some
  1031. * links must share PEs.
  1032. *
  1033. * To achieve this we assign PEs such that NPUs linking the
  1034. * same GPU get assigned the same PE.
  1035. */
  1036. gpu_pdev = pnv_pci_get_gpu_dev(npu_pdev);
  1037. for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
  1038. pe = &phb->ioda.pe_array[pe_num];
  1039. if (!pe->pdev)
  1040. continue;
  1041. if (pnv_pci_get_gpu_dev(pe->pdev) == gpu_pdev) {
  1042. /*
  1043. * This device has the same peer GPU so should
  1044. * be assigned the same PE as the existing
  1045. * peer NPU.
  1046. */
  1047. dev_info(&npu_pdev->dev,
  1048. "Associating to existing PE %x\n", pe_num);
  1049. pci_dev_get(npu_pdev);
  1050. npu_pdn = pci_get_pdn(npu_pdev);
  1051. rid = npu_pdev->bus->number << 8 | npu_pdn->devfn;
  1052. npu_pdn->pcidev = npu_pdev;
  1053. npu_pdn->pe_number = pe_num;
  1054. phb->ioda.pe_rmap[rid] = pe->pe_number;
  1055. /* Map the PE to this link */
  1056. rc = opal_pci_set_pe(phb->opal_id, pe_num, rid,
  1057. OpalPciBusAll,
  1058. OPAL_COMPARE_RID_DEVICE_NUMBER,
  1059. OPAL_COMPARE_RID_FUNCTION_NUMBER,
  1060. OPAL_MAP_PE);
  1061. WARN_ON(rc != OPAL_SUCCESS);
  1062. found_pe = true;
  1063. break;
  1064. }
  1065. }
  1066. if (!found_pe)
  1067. /*
  1068. * Could not find an existing PE so allocate a new
  1069. * one.
  1070. */
  1071. return pnv_ioda_setup_dev_PE(npu_pdev);
  1072. else
  1073. return pe;
  1074. }
  1075. static void pnv_ioda_setup_npu_PEs(struct pci_bus *bus)
  1076. {
  1077. struct pci_dev *pdev;
  1078. list_for_each_entry(pdev, &bus->devices, bus_list)
  1079. pnv_ioda_setup_npu_PE(pdev);
  1080. }
  1081. static void pnv_pci_ioda_setup_PEs(void)
  1082. {
  1083. struct pci_controller *hose, *tmp;
  1084. struct pnv_phb *phb;
  1085. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1086. phb = hose->private_data;
  1087. if (phb->type == PNV_PHB_NPU) {
  1088. /* PE#0 is needed for error reporting */
  1089. pnv_ioda_reserve_pe(phb, 0);
  1090. pnv_ioda_setup_npu_PEs(hose->bus);
  1091. if (phb->model == PNV_PHB_MODEL_NPU2)
  1092. pnv_npu2_init(phb);
  1093. }
  1094. }
  1095. }
  1096. #ifdef CONFIG_PCI_IOV
  1097. static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
  1098. {
  1099. struct pci_bus *bus;
  1100. struct pci_controller *hose;
  1101. struct pnv_phb *phb;
  1102. struct pci_dn *pdn;
  1103. int i, j;
  1104. int m64_bars;
  1105. bus = pdev->bus;
  1106. hose = pci_bus_to_host(bus);
  1107. phb = hose->private_data;
  1108. pdn = pci_get_pdn(pdev);
  1109. if (pdn->m64_single_mode)
  1110. m64_bars = num_vfs;
  1111. else
  1112. m64_bars = 1;
  1113. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++)
  1114. for (j = 0; j < m64_bars; j++) {
  1115. if (pdn->m64_map[j][i] == IODA_INVALID_M64)
  1116. continue;
  1117. opal_pci_phb_mmio_enable(phb->opal_id,
  1118. OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 0);
  1119. clear_bit(pdn->m64_map[j][i], &phb->ioda.m64_bar_alloc);
  1120. pdn->m64_map[j][i] = IODA_INVALID_M64;
  1121. }
  1122. kfree(pdn->m64_map);
  1123. return 0;
  1124. }
  1125. static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
  1126. {
  1127. struct pci_bus *bus;
  1128. struct pci_controller *hose;
  1129. struct pnv_phb *phb;
  1130. struct pci_dn *pdn;
  1131. unsigned int win;
  1132. struct resource *res;
  1133. int i, j;
  1134. int64_t rc;
  1135. int total_vfs;
  1136. resource_size_t size, start;
  1137. int pe_num;
  1138. int m64_bars;
  1139. bus = pdev->bus;
  1140. hose = pci_bus_to_host(bus);
  1141. phb = hose->private_data;
  1142. pdn = pci_get_pdn(pdev);
  1143. total_vfs = pci_sriov_get_totalvfs(pdev);
  1144. if (pdn->m64_single_mode)
  1145. m64_bars = num_vfs;
  1146. else
  1147. m64_bars = 1;
  1148. pdn->m64_map = kmalloc_array(m64_bars,
  1149. sizeof(*pdn->m64_map),
  1150. GFP_KERNEL);
  1151. if (!pdn->m64_map)
  1152. return -ENOMEM;
  1153. /* Initialize the m64_map to IODA_INVALID_M64 */
  1154. for (i = 0; i < m64_bars ; i++)
  1155. for (j = 0; j < PCI_SRIOV_NUM_BARS; j++)
  1156. pdn->m64_map[i][j] = IODA_INVALID_M64;
  1157. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  1158. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  1159. if (!res->flags || !res->parent)
  1160. continue;
  1161. for (j = 0; j < m64_bars; j++) {
  1162. do {
  1163. win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
  1164. phb->ioda.m64_bar_idx + 1, 0);
  1165. if (win >= phb->ioda.m64_bar_idx + 1)
  1166. goto m64_failed;
  1167. } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
  1168. pdn->m64_map[j][i] = win;
  1169. if (pdn->m64_single_mode) {
  1170. size = pci_iov_resource_size(pdev,
  1171. PCI_IOV_RESOURCES + i);
  1172. start = res->start + size * j;
  1173. } else {
  1174. size = resource_size(res);
  1175. start = res->start;
  1176. }
  1177. /* Map the M64 here */
  1178. if (pdn->m64_single_mode) {
  1179. pe_num = pdn->pe_num_map[j];
  1180. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  1181. pe_num, OPAL_M64_WINDOW_TYPE,
  1182. pdn->m64_map[j][i], 0);
  1183. }
  1184. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  1185. OPAL_M64_WINDOW_TYPE,
  1186. pdn->m64_map[j][i],
  1187. start,
  1188. 0, /* unused */
  1189. size);
  1190. if (rc != OPAL_SUCCESS) {
  1191. dev_err(&pdev->dev, "Failed to map M64 window #%d: %lld\n",
  1192. win, rc);
  1193. goto m64_failed;
  1194. }
  1195. if (pdn->m64_single_mode)
  1196. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  1197. OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 2);
  1198. else
  1199. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  1200. OPAL_M64_WINDOW_TYPE, pdn->m64_map[j][i], 1);
  1201. if (rc != OPAL_SUCCESS) {
  1202. dev_err(&pdev->dev, "Failed to enable M64 window #%d: %llx\n",
  1203. win, rc);
  1204. goto m64_failed;
  1205. }
  1206. }
  1207. }
  1208. return 0;
  1209. m64_failed:
  1210. pnv_pci_vf_release_m64(pdev, num_vfs);
  1211. return -EBUSY;
  1212. }
  1213. static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
  1214. int num);
  1215. static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
  1216. static void pnv_pci_ioda2_release_dma_pe(struct pci_dev *dev, struct pnv_ioda_pe *pe)
  1217. {
  1218. struct iommu_table *tbl;
  1219. int64_t rc;
  1220. tbl = pe->table_group.tables[0];
  1221. rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
  1222. if (rc)
  1223. pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
  1224. pnv_pci_ioda2_set_bypass(pe, false);
  1225. if (pe->table_group.group) {
  1226. iommu_group_put(pe->table_group.group);
  1227. BUG_ON(pe->table_group.group);
  1228. }
  1229. pnv_pci_ioda2_table_free_pages(tbl);
  1230. iommu_free_table(tbl, of_node_full_name(dev->dev.of_node));
  1231. }
  1232. static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
  1233. {
  1234. struct pci_bus *bus;
  1235. struct pci_controller *hose;
  1236. struct pnv_phb *phb;
  1237. struct pnv_ioda_pe *pe, *pe_n;
  1238. struct pci_dn *pdn;
  1239. bus = pdev->bus;
  1240. hose = pci_bus_to_host(bus);
  1241. phb = hose->private_data;
  1242. pdn = pci_get_pdn(pdev);
  1243. if (!pdev->is_physfn)
  1244. return;
  1245. list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
  1246. if (pe->parent_dev != pdev)
  1247. continue;
  1248. pnv_pci_ioda2_release_dma_pe(pdev, pe);
  1249. /* Remove from list */
  1250. mutex_lock(&phb->ioda.pe_list_mutex);
  1251. list_del(&pe->list);
  1252. mutex_unlock(&phb->ioda.pe_list_mutex);
  1253. pnv_ioda_deconfigure_pe(phb, pe);
  1254. pnv_ioda_free_pe(pe);
  1255. }
  1256. }
  1257. void pnv_pci_sriov_disable(struct pci_dev *pdev)
  1258. {
  1259. struct pci_bus *bus;
  1260. struct pci_controller *hose;
  1261. struct pnv_phb *phb;
  1262. struct pnv_ioda_pe *pe;
  1263. struct pci_dn *pdn;
  1264. u16 num_vfs, i;
  1265. bus = pdev->bus;
  1266. hose = pci_bus_to_host(bus);
  1267. phb = hose->private_data;
  1268. pdn = pci_get_pdn(pdev);
  1269. num_vfs = pdn->num_vfs;
  1270. /* Release VF PEs */
  1271. pnv_ioda_release_vf_PE(pdev);
  1272. if (phb->type == PNV_PHB_IODA2) {
  1273. if (!pdn->m64_single_mode)
  1274. pnv_pci_vf_resource_shift(pdev, -*pdn->pe_num_map);
  1275. /* Release M64 windows */
  1276. pnv_pci_vf_release_m64(pdev, num_vfs);
  1277. /* Release PE numbers */
  1278. if (pdn->m64_single_mode) {
  1279. for (i = 0; i < num_vfs; i++) {
  1280. if (pdn->pe_num_map[i] == IODA_INVALID_PE)
  1281. continue;
  1282. pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
  1283. pnv_ioda_free_pe(pe);
  1284. }
  1285. } else
  1286. bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
  1287. /* Releasing pe_num_map */
  1288. kfree(pdn->pe_num_map);
  1289. }
  1290. }
  1291. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  1292. struct pnv_ioda_pe *pe);
  1293. static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
  1294. {
  1295. struct pci_bus *bus;
  1296. struct pci_controller *hose;
  1297. struct pnv_phb *phb;
  1298. struct pnv_ioda_pe *pe;
  1299. int pe_num;
  1300. u16 vf_index;
  1301. struct pci_dn *pdn;
  1302. bus = pdev->bus;
  1303. hose = pci_bus_to_host(bus);
  1304. phb = hose->private_data;
  1305. pdn = pci_get_pdn(pdev);
  1306. if (!pdev->is_physfn)
  1307. return;
  1308. /* Reserve PE for each VF */
  1309. for (vf_index = 0; vf_index < num_vfs; vf_index++) {
  1310. if (pdn->m64_single_mode)
  1311. pe_num = pdn->pe_num_map[vf_index];
  1312. else
  1313. pe_num = *pdn->pe_num_map + vf_index;
  1314. pe = &phb->ioda.pe_array[pe_num];
  1315. pe->pe_number = pe_num;
  1316. pe->phb = phb;
  1317. pe->flags = PNV_IODA_PE_VF;
  1318. pe->pbus = NULL;
  1319. pe->parent_dev = pdev;
  1320. pe->mve_number = -1;
  1321. pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
  1322. pci_iov_virtfn_devfn(pdev, vf_index);
  1323. pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
  1324. hose->global_number, pdev->bus->number,
  1325. PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
  1326. PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
  1327. if (pnv_ioda_configure_pe(phb, pe)) {
  1328. /* XXX What do we do here ? */
  1329. pnv_ioda_free_pe(pe);
  1330. pe->pdev = NULL;
  1331. continue;
  1332. }
  1333. /* Put PE to the list */
  1334. mutex_lock(&phb->ioda.pe_list_mutex);
  1335. list_add_tail(&pe->list, &phb->ioda.pe_list);
  1336. mutex_unlock(&phb->ioda.pe_list_mutex);
  1337. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  1338. }
  1339. }
  1340. int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  1341. {
  1342. struct pci_bus *bus;
  1343. struct pci_controller *hose;
  1344. struct pnv_phb *phb;
  1345. struct pnv_ioda_pe *pe;
  1346. struct pci_dn *pdn;
  1347. int ret;
  1348. u16 i;
  1349. bus = pdev->bus;
  1350. hose = pci_bus_to_host(bus);
  1351. phb = hose->private_data;
  1352. pdn = pci_get_pdn(pdev);
  1353. if (phb->type == PNV_PHB_IODA2) {
  1354. if (!pdn->vfs_expanded) {
  1355. dev_info(&pdev->dev, "don't support this SRIOV device"
  1356. " with non 64bit-prefetchable IOV BAR\n");
  1357. return -ENOSPC;
  1358. }
  1359. /*
  1360. * When M64 BARs functions in Single PE mode, the number of VFs
  1361. * could be enabled must be less than the number of M64 BARs.
  1362. */
  1363. if (pdn->m64_single_mode && num_vfs > phb->ioda.m64_bar_idx) {
  1364. dev_info(&pdev->dev, "Not enough M64 BAR for VFs\n");
  1365. return -EBUSY;
  1366. }
  1367. /* Allocating pe_num_map */
  1368. if (pdn->m64_single_mode)
  1369. pdn->pe_num_map = kmalloc_array(num_vfs,
  1370. sizeof(*pdn->pe_num_map),
  1371. GFP_KERNEL);
  1372. else
  1373. pdn->pe_num_map = kmalloc(sizeof(*pdn->pe_num_map), GFP_KERNEL);
  1374. if (!pdn->pe_num_map)
  1375. return -ENOMEM;
  1376. if (pdn->m64_single_mode)
  1377. for (i = 0; i < num_vfs; i++)
  1378. pdn->pe_num_map[i] = IODA_INVALID_PE;
  1379. /* Calculate available PE for required VFs */
  1380. if (pdn->m64_single_mode) {
  1381. for (i = 0; i < num_vfs; i++) {
  1382. pe = pnv_ioda_alloc_pe(phb);
  1383. if (!pe) {
  1384. ret = -EBUSY;
  1385. goto m64_failed;
  1386. }
  1387. pdn->pe_num_map[i] = pe->pe_number;
  1388. }
  1389. } else {
  1390. mutex_lock(&phb->ioda.pe_alloc_mutex);
  1391. *pdn->pe_num_map = bitmap_find_next_zero_area(
  1392. phb->ioda.pe_alloc, phb->ioda.total_pe_num,
  1393. 0, num_vfs, 0);
  1394. if (*pdn->pe_num_map >= phb->ioda.total_pe_num) {
  1395. mutex_unlock(&phb->ioda.pe_alloc_mutex);
  1396. dev_info(&pdev->dev, "Failed to enable VF%d\n", num_vfs);
  1397. kfree(pdn->pe_num_map);
  1398. return -EBUSY;
  1399. }
  1400. bitmap_set(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
  1401. mutex_unlock(&phb->ioda.pe_alloc_mutex);
  1402. }
  1403. pdn->num_vfs = num_vfs;
  1404. /* Assign M64 window accordingly */
  1405. ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
  1406. if (ret) {
  1407. dev_info(&pdev->dev, "Not enough M64 window resources\n");
  1408. goto m64_failed;
  1409. }
  1410. /*
  1411. * When using one M64 BAR to map one IOV BAR, we need to shift
  1412. * the IOV BAR according to the PE# allocated to the VFs.
  1413. * Otherwise, the PE# for the VF will conflict with others.
  1414. */
  1415. if (!pdn->m64_single_mode) {
  1416. ret = pnv_pci_vf_resource_shift(pdev, *pdn->pe_num_map);
  1417. if (ret)
  1418. goto m64_failed;
  1419. }
  1420. }
  1421. /* Setup VF PEs */
  1422. pnv_ioda_setup_vf_PE(pdev, num_vfs);
  1423. return 0;
  1424. m64_failed:
  1425. if (pdn->m64_single_mode) {
  1426. for (i = 0; i < num_vfs; i++) {
  1427. if (pdn->pe_num_map[i] == IODA_INVALID_PE)
  1428. continue;
  1429. pe = &phb->ioda.pe_array[pdn->pe_num_map[i]];
  1430. pnv_ioda_free_pe(pe);
  1431. }
  1432. } else
  1433. bitmap_clear(phb->ioda.pe_alloc, *pdn->pe_num_map, num_vfs);
  1434. /* Releasing pe_num_map */
  1435. kfree(pdn->pe_num_map);
  1436. return ret;
  1437. }
  1438. int pcibios_sriov_disable(struct pci_dev *pdev)
  1439. {
  1440. pnv_pci_sriov_disable(pdev);
  1441. /* Release PCI data */
  1442. remove_dev_pci_data(pdev);
  1443. return 0;
  1444. }
  1445. int pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
  1446. {
  1447. /* Allocate PCI data */
  1448. add_dev_pci_data(pdev);
  1449. return pnv_pci_sriov_enable(pdev, num_vfs);
  1450. }
  1451. #endif /* CONFIG_PCI_IOV */
  1452. static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
  1453. {
  1454. struct pci_dn *pdn = pci_get_pdn(pdev);
  1455. struct pnv_ioda_pe *pe;
  1456. /*
  1457. * The function can be called while the PE#
  1458. * hasn't been assigned. Do nothing for the
  1459. * case.
  1460. */
  1461. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  1462. return;
  1463. pe = &phb->ioda.pe_array[pdn->pe_number];
  1464. WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
  1465. set_dma_offset(&pdev->dev, pe->tce_bypass_base);
  1466. set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
  1467. /*
  1468. * Note: iommu_add_device() will fail here as
  1469. * for physical PE: the device is already added by now;
  1470. * for virtual PE: sysfs entries are not ready yet and
  1471. * tce_iommu_bus_notifier will add the device to a group later.
  1472. */
  1473. }
  1474. static int pnv_pci_ioda_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
  1475. {
  1476. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  1477. struct pnv_phb *phb = hose->private_data;
  1478. struct pci_dn *pdn = pci_get_pdn(pdev);
  1479. struct pnv_ioda_pe *pe;
  1480. uint64_t top;
  1481. bool bypass = false;
  1482. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  1483. return -ENODEV;;
  1484. pe = &phb->ioda.pe_array[pdn->pe_number];
  1485. if (pe->tce_bypass_enabled) {
  1486. top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
  1487. bypass = (dma_mask >= top);
  1488. }
  1489. if (bypass) {
  1490. dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
  1491. set_dma_ops(&pdev->dev, &dma_direct_ops);
  1492. } else {
  1493. dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
  1494. set_dma_ops(&pdev->dev, &dma_iommu_ops);
  1495. }
  1496. *pdev->dev.dma_mask = dma_mask;
  1497. /* Update peer npu devices */
  1498. pnv_npu_try_dma_set_bypass(pdev, bypass);
  1499. return 0;
  1500. }
  1501. static u64 pnv_pci_ioda_dma_get_required_mask(struct pci_dev *pdev)
  1502. {
  1503. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  1504. struct pnv_phb *phb = hose->private_data;
  1505. struct pci_dn *pdn = pci_get_pdn(pdev);
  1506. struct pnv_ioda_pe *pe;
  1507. u64 end, mask;
  1508. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  1509. return 0;
  1510. pe = &phb->ioda.pe_array[pdn->pe_number];
  1511. if (!pe->tce_bypass_enabled)
  1512. return __dma_get_required_mask(&pdev->dev);
  1513. end = pe->tce_bypass_base + memblock_end_of_DRAM();
  1514. mask = 1ULL << (fls64(end) - 1);
  1515. mask += mask - 1;
  1516. return mask;
  1517. }
  1518. static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
  1519. struct pci_bus *bus,
  1520. bool add_to_group)
  1521. {
  1522. struct pci_dev *dev;
  1523. list_for_each_entry(dev, &bus->devices, bus_list) {
  1524. set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
  1525. set_dma_offset(&dev->dev, pe->tce_bypass_base);
  1526. if (add_to_group)
  1527. iommu_add_device(&dev->dev);
  1528. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  1529. pnv_ioda_setup_bus_dma(pe, dev->subordinate,
  1530. add_to_group);
  1531. }
  1532. }
  1533. static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb,
  1534. bool real_mode)
  1535. {
  1536. return real_mode ? (__be64 __iomem *)(phb->regs_phys + 0x210) :
  1537. (phb->regs + 0x210);
  1538. }
  1539. static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl,
  1540. unsigned long index, unsigned long npages, bool rm)
  1541. {
  1542. struct iommu_table_group_link *tgl = list_first_entry_or_null(
  1543. &tbl->it_group_list, struct iommu_table_group_link,
  1544. next);
  1545. struct pnv_ioda_pe *pe = container_of(tgl->table_group,
  1546. struct pnv_ioda_pe, table_group);
  1547. __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
  1548. unsigned long start, end, inc;
  1549. start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
  1550. end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
  1551. npages - 1);
  1552. /* p7ioc-style invalidation, 2 TCEs per write */
  1553. start |= (1ull << 63);
  1554. end |= (1ull << 63);
  1555. inc = 16;
  1556. end |= inc - 1; /* round up end to be different than start */
  1557. mb(); /* Ensure above stores are visible */
  1558. while (start <= end) {
  1559. if (rm)
  1560. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  1561. else
  1562. __raw_writeq(cpu_to_be64(start), invalidate);
  1563. start += inc;
  1564. }
  1565. /*
  1566. * The iommu layer will do another mb() for us on build()
  1567. * and we don't care on free()
  1568. */
  1569. }
  1570. static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
  1571. long npages, unsigned long uaddr,
  1572. enum dma_data_direction direction,
  1573. unsigned long attrs)
  1574. {
  1575. int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
  1576. attrs);
  1577. if (!ret)
  1578. pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
  1579. return ret;
  1580. }
  1581. #ifdef CONFIG_IOMMU_API
  1582. static int pnv_ioda1_tce_xchg(struct iommu_table *tbl, long index,
  1583. unsigned long *hpa, enum dma_data_direction *direction)
  1584. {
  1585. long ret = pnv_tce_xchg(tbl, index, hpa, direction);
  1586. if (!ret)
  1587. pnv_pci_p7ioc_tce_invalidate(tbl, index, 1, false);
  1588. return ret;
  1589. }
  1590. #endif
  1591. static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
  1592. long npages)
  1593. {
  1594. pnv_tce_free(tbl, index, npages);
  1595. pnv_pci_p7ioc_tce_invalidate(tbl, index, npages, false);
  1596. }
  1597. static struct iommu_table_ops pnv_ioda1_iommu_ops = {
  1598. .set = pnv_ioda1_tce_build,
  1599. #ifdef CONFIG_IOMMU_API
  1600. .exchange = pnv_ioda1_tce_xchg,
  1601. #endif
  1602. .clear = pnv_ioda1_tce_free,
  1603. .get = pnv_tce_get,
  1604. };
  1605. #define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
  1606. #define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
  1607. #define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
  1608. void pnv_pci_phb3_tce_invalidate_entire(struct pnv_phb *phb, bool rm)
  1609. {
  1610. __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(phb, rm);
  1611. const unsigned long val = PHB3_TCE_KILL_INVAL_ALL;
  1612. mb(); /* Ensure previous TCE table stores are visible */
  1613. if (rm)
  1614. __raw_rm_writeq(cpu_to_be64(val), invalidate);
  1615. else
  1616. __raw_writeq(cpu_to_be64(val), invalidate);
  1617. }
  1618. static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
  1619. {
  1620. /* 01xb - invalidate TCEs that match the specified PE# */
  1621. __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, false);
  1622. unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
  1623. mb(); /* Ensure above stores are visible */
  1624. __raw_writeq(cpu_to_be64(val), invalidate);
  1625. }
  1626. static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe, bool rm,
  1627. unsigned shift, unsigned long index,
  1628. unsigned long npages)
  1629. {
  1630. __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb, rm);
  1631. unsigned long start, end, inc;
  1632. /* We'll invalidate DMA address in PE scope */
  1633. start = PHB3_TCE_KILL_INVAL_ONE;
  1634. start |= (pe->pe_number & 0xFF);
  1635. end = start;
  1636. /* Figure out the start, end and step */
  1637. start |= (index << shift);
  1638. end |= ((index + npages - 1) << shift);
  1639. inc = (0x1ull << shift);
  1640. mb();
  1641. while (start <= end) {
  1642. if (rm)
  1643. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  1644. else
  1645. __raw_writeq(cpu_to_be64(start), invalidate);
  1646. start += inc;
  1647. }
  1648. }
  1649. static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
  1650. {
  1651. struct pnv_phb *phb = pe->phb;
  1652. if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
  1653. pnv_pci_phb3_tce_invalidate_pe(pe);
  1654. else
  1655. opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
  1656. pe->pe_number, 0, 0, 0);
  1657. }
  1658. static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
  1659. unsigned long index, unsigned long npages, bool rm)
  1660. {
  1661. struct iommu_table_group_link *tgl;
  1662. list_for_each_entry_rcu(tgl, &tbl->it_group_list, next) {
  1663. struct pnv_ioda_pe *pe = container_of(tgl->table_group,
  1664. struct pnv_ioda_pe, table_group);
  1665. struct pnv_phb *phb = pe->phb;
  1666. unsigned int shift = tbl->it_page_shift;
  1667. /*
  1668. * NVLink1 can use the TCE kill register directly as
  1669. * it's the same as PHB3. NVLink2 is different and
  1670. * should go via the OPAL call.
  1671. */
  1672. if (phb->model == PNV_PHB_MODEL_NPU) {
  1673. /*
  1674. * The NVLink hardware does not support TCE kill
  1675. * per TCE entry so we have to invalidate
  1676. * the entire cache for it.
  1677. */
  1678. pnv_pci_phb3_tce_invalidate_entire(phb, rm);
  1679. continue;
  1680. }
  1681. if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
  1682. pnv_pci_phb3_tce_invalidate(pe, rm, shift,
  1683. index, npages);
  1684. else
  1685. opal_pci_tce_kill(phb->opal_id,
  1686. OPAL_PCI_TCE_KILL_PAGES,
  1687. pe->pe_number, 1u << shift,
  1688. index << shift, npages);
  1689. }
  1690. }
  1691. static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
  1692. long npages, unsigned long uaddr,
  1693. enum dma_data_direction direction,
  1694. unsigned long attrs)
  1695. {
  1696. int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
  1697. attrs);
  1698. if (!ret)
  1699. pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
  1700. return ret;
  1701. }
  1702. #ifdef CONFIG_IOMMU_API
  1703. static int pnv_ioda2_tce_xchg(struct iommu_table *tbl, long index,
  1704. unsigned long *hpa, enum dma_data_direction *direction)
  1705. {
  1706. long ret = pnv_tce_xchg(tbl, index, hpa, direction);
  1707. if (!ret)
  1708. pnv_pci_ioda2_tce_invalidate(tbl, index, 1, false);
  1709. return ret;
  1710. }
  1711. #endif
  1712. static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
  1713. long npages)
  1714. {
  1715. pnv_tce_free(tbl, index, npages);
  1716. pnv_pci_ioda2_tce_invalidate(tbl, index, npages, false);
  1717. }
  1718. static void pnv_ioda2_table_free(struct iommu_table *tbl)
  1719. {
  1720. pnv_pci_ioda2_table_free_pages(tbl);
  1721. iommu_free_table(tbl, "pnv");
  1722. }
  1723. static struct iommu_table_ops pnv_ioda2_iommu_ops = {
  1724. .set = pnv_ioda2_tce_build,
  1725. #ifdef CONFIG_IOMMU_API
  1726. .exchange = pnv_ioda2_tce_xchg,
  1727. #endif
  1728. .clear = pnv_ioda2_tce_free,
  1729. .get = pnv_tce_get,
  1730. .free = pnv_ioda2_table_free,
  1731. };
  1732. static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
  1733. {
  1734. unsigned int *weight = (unsigned int *)data;
  1735. /* This is quite simplistic. The "base" weight of a device
  1736. * is 10. 0 means no DMA is to be accounted for it.
  1737. */
  1738. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  1739. return 0;
  1740. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  1741. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  1742. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  1743. *weight += 3;
  1744. else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  1745. *weight += 15;
  1746. else
  1747. *weight += 10;
  1748. return 0;
  1749. }
  1750. static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
  1751. {
  1752. unsigned int weight = 0;
  1753. /* SRIOV VF has same DMA32 weight as its PF */
  1754. #ifdef CONFIG_PCI_IOV
  1755. if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
  1756. pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
  1757. return weight;
  1758. }
  1759. #endif
  1760. if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
  1761. pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
  1762. } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
  1763. struct pci_dev *pdev;
  1764. list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
  1765. pnv_pci_ioda_dev_dma_weight(pdev, &weight);
  1766. } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
  1767. pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
  1768. }
  1769. return weight;
  1770. }
  1771. static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
  1772. struct pnv_ioda_pe *pe)
  1773. {
  1774. struct page *tce_mem = NULL;
  1775. struct iommu_table *tbl;
  1776. unsigned int weight, total_weight = 0;
  1777. unsigned int tce32_segsz, base, segs, avail, i;
  1778. int64_t rc;
  1779. void *addr;
  1780. /* XXX FIXME: Handle 64-bit only DMA devices */
  1781. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  1782. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  1783. weight = pnv_pci_ioda_pe_dma_weight(pe);
  1784. if (!weight)
  1785. return;
  1786. pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
  1787. &total_weight);
  1788. segs = (weight * phb->ioda.dma32_count) / total_weight;
  1789. if (!segs)
  1790. segs = 1;
  1791. /*
  1792. * Allocate contiguous DMA32 segments. We begin with the expected
  1793. * number of segments. With one more attempt, the number of DMA32
  1794. * segments to be allocated is decreased by one until one segment
  1795. * is allocated successfully.
  1796. */
  1797. do {
  1798. for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
  1799. for (avail = 0, i = base; i < base + segs; i++) {
  1800. if (phb->ioda.dma32_segmap[i] ==
  1801. IODA_INVALID_PE)
  1802. avail++;
  1803. }
  1804. if (avail == segs)
  1805. goto found;
  1806. }
  1807. } while (--segs);
  1808. if (!segs) {
  1809. pe_warn(pe, "No available DMA32 segments\n");
  1810. return;
  1811. }
  1812. found:
  1813. tbl = pnv_pci_table_alloc(phb->hose->node);
  1814. iommu_register_group(&pe->table_group, phb->hose->global_number,
  1815. pe->pe_number);
  1816. pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
  1817. /* Grab a 32-bit TCE table */
  1818. pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
  1819. weight, total_weight, base, segs);
  1820. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  1821. base * PNV_IODA1_DMA32_SEGSIZE,
  1822. (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
  1823. /* XXX Currently, we allocate one big contiguous table for the
  1824. * TCEs. We only really need one chunk per 256M of TCE space
  1825. * (ie per segment) but that's an optimization for later, it
  1826. * requires some added smarts with our get/put_tce implementation
  1827. *
  1828. * Each TCE page is 4KB in size and each TCE entry occupies 8
  1829. * bytes
  1830. */
  1831. tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
  1832. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  1833. get_order(tce32_segsz * segs));
  1834. if (!tce_mem) {
  1835. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  1836. goto fail;
  1837. }
  1838. addr = page_address(tce_mem);
  1839. memset(addr, 0, tce32_segsz * segs);
  1840. /* Configure HW */
  1841. for (i = 0; i < segs; i++) {
  1842. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  1843. pe->pe_number,
  1844. base + i, 1,
  1845. __pa(addr) + tce32_segsz * i,
  1846. tce32_segsz, IOMMU_PAGE_SIZE_4K);
  1847. if (rc) {
  1848. pe_err(pe, " Failed to configure 32-bit TCE table,"
  1849. " err %ld\n", rc);
  1850. goto fail;
  1851. }
  1852. }
  1853. /* Setup DMA32 segment mapping */
  1854. for (i = base; i < base + segs; i++)
  1855. phb->ioda.dma32_segmap[i] = pe->pe_number;
  1856. /* Setup linux iommu table */
  1857. pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
  1858. base * PNV_IODA1_DMA32_SEGSIZE,
  1859. IOMMU_PAGE_SHIFT_4K);
  1860. tbl->it_ops = &pnv_ioda1_iommu_ops;
  1861. pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
  1862. pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
  1863. iommu_init_table(tbl, phb->hose->node);
  1864. if (pe->flags & PNV_IODA_PE_DEV) {
  1865. /*
  1866. * Setting table base here only for carrying iommu_group
  1867. * further down to let iommu_add_device() do the job.
  1868. * pnv_pci_ioda_dma_dev_setup will override it later anyway.
  1869. */
  1870. set_iommu_table_base(&pe->pdev->dev, tbl);
  1871. iommu_add_device(&pe->pdev->dev);
  1872. } else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
  1873. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  1874. return;
  1875. fail:
  1876. /* XXX Failure: Try to fallback to 64-bit only ? */
  1877. if (tce_mem)
  1878. __free_pages(tce_mem, get_order(tce32_segsz * segs));
  1879. if (tbl) {
  1880. pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
  1881. iommu_free_table(tbl, "pnv");
  1882. }
  1883. }
  1884. static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
  1885. int num, struct iommu_table *tbl)
  1886. {
  1887. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  1888. table_group);
  1889. struct pnv_phb *phb = pe->phb;
  1890. int64_t rc;
  1891. const unsigned long size = tbl->it_indirect_levels ?
  1892. tbl->it_level_size : tbl->it_size;
  1893. const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
  1894. const __u64 win_size = tbl->it_size << tbl->it_page_shift;
  1895. pe_info(pe, "Setting up window#%d %llx..%llx pg=%x\n", num,
  1896. start_addr, start_addr + win_size - 1,
  1897. IOMMU_PAGE_SIZE(tbl));
  1898. /*
  1899. * Map TCE table through TVT. The TVE index is the PE number
  1900. * shifted by 1 bit for 32-bits DMA space.
  1901. */
  1902. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  1903. pe->pe_number,
  1904. (pe->pe_number << 1) + num,
  1905. tbl->it_indirect_levels + 1,
  1906. __pa(tbl->it_base),
  1907. size << 3,
  1908. IOMMU_PAGE_SIZE(tbl));
  1909. if (rc) {
  1910. pe_err(pe, "Failed to configure TCE table, err %ld\n", rc);
  1911. return rc;
  1912. }
  1913. pnv_pci_link_table_and_group(phb->hose->node, num,
  1914. tbl, &pe->table_group);
  1915. pnv_pci_ioda2_tce_invalidate_pe(pe);
  1916. return 0;
  1917. }
  1918. static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
  1919. {
  1920. uint16_t window_id = (pe->pe_number << 1 ) + 1;
  1921. int64_t rc;
  1922. pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
  1923. if (enable) {
  1924. phys_addr_t top = memblock_end_of_DRAM();
  1925. top = roundup_pow_of_two(top);
  1926. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  1927. pe->pe_number,
  1928. window_id,
  1929. pe->tce_bypass_base,
  1930. top);
  1931. } else {
  1932. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  1933. pe->pe_number,
  1934. window_id,
  1935. pe->tce_bypass_base,
  1936. 0);
  1937. }
  1938. if (rc)
  1939. pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
  1940. else
  1941. pe->tce_bypass_enabled = enable;
  1942. }
  1943. static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
  1944. __u32 page_shift, __u64 window_size, __u32 levels,
  1945. struct iommu_table *tbl);
  1946. static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
  1947. int num, __u32 page_shift, __u64 window_size, __u32 levels,
  1948. struct iommu_table **ptbl)
  1949. {
  1950. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  1951. table_group);
  1952. int nid = pe->phb->hose->node;
  1953. __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
  1954. long ret;
  1955. struct iommu_table *tbl;
  1956. tbl = pnv_pci_table_alloc(nid);
  1957. if (!tbl)
  1958. return -ENOMEM;
  1959. ret = pnv_pci_ioda2_table_alloc_pages(nid,
  1960. bus_offset, page_shift, window_size,
  1961. levels, tbl);
  1962. if (ret) {
  1963. iommu_free_table(tbl, "pnv");
  1964. return ret;
  1965. }
  1966. tbl->it_ops = &pnv_ioda2_iommu_ops;
  1967. *ptbl = tbl;
  1968. return 0;
  1969. }
  1970. static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
  1971. {
  1972. struct iommu_table *tbl = NULL;
  1973. long rc;
  1974. /*
  1975. * crashkernel= specifies the kdump kernel's maximum memory at
  1976. * some offset and there is no guaranteed the result is a power
  1977. * of 2, which will cause errors later.
  1978. */
  1979. const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
  1980. /*
  1981. * In memory constrained environments, e.g. kdump kernel, the
  1982. * DMA window can be larger than available memory, which will
  1983. * cause errors later.
  1984. */
  1985. const u64 window_size = min((u64)pe->table_group.tce32_size, max_memory);
  1986. rc = pnv_pci_ioda2_create_table(&pe->table_group, 0,
  1987. IOMMU_PAGE_SHIFT_4K,
  1988. window_size,
  1989. POWERNV_IOMMU_DEFAULT_LEVELS, &tbl);
  1990. if (rc) {
  1991. pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
  1992. rc);
  1993. return rc;
  1994. }
  1995. iommu_init_table(tbl, pe->phb->hose->node);
  1996. rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
  1997. if (rc) {
  1998. pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n",
  1999. rc);
  2000. pnv_ioda2_table_free(tbl);
  2001. return rc;
  2002. }
  2003. if (!pnv_iommu_bypass_disabled)
  2004. pnv_pci_ioda2_set_bypass(pe, true);
  2005. /*
  2006. * Setting table base here only for carrying iommu_group
  2007. * further down to let iommu_add_device() do the job.
  2008. * pnv_pci_ioda_dma_dev_setup will override it later anyway.
  2009. */
  2010. if (pe->flags & PNV_IODA_PE_DEV)
  2011. set_iommu_table_base(&pe->pdev->dev, tbl);
  2012. return 0;
  2013. }
  2014. #if defined(CONFIG_IOMMU_API) || defined(CONFIG_PCI_IOV)
  2015. static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
  2016. int num)
  2017. {
  2018. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  2019. table_group);
  2020. struct pnv_phb *phb = pe->phb;
  2021. long ret;
  2022. pe_info(pe, "Removing DMA window #%d\n", num);
  2023. ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  2024. (pe->pe_number << 1) + num,
  2025. 0/* levels */, 0/* table address */,
  2026. 0/* table size */, 0/* page size */);
  2027. if (ret)
  2028. pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
  2029. else
  2030. pnv_pci_ioda2_tce_invalidate_pe(pe);
  2031. pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
  2032. return ret;
  2033. }
  2034. #endif
  2035. #ifdef CONFIG_IOMMU_API
  2036. static unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
  2037. __u64 window_size, __u32 levels)
  2038. {
  2039. unsigned long bytes = 0;
  2040. const unsigned window_shift = ilog2(window_size);
  2041. unsigned entries_shift = window_shift - page_shift;
  2042. unsigned table_shift = entries_shift + 3;
  2043. unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
  2044. unsigned long direct_table_size;
  2045. if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
  2046. (window_size > memory_hotplug_max()) ||
  2047. !is_power_of_2(window_size))
  2048. return 0;
  2049. /* Calculate a direct table size from window_size and levels */
  2050. entries_shift = (entries_shift + levels - 1) / levels;
  2051. table_shift = entries_shift + 3;
  2052. table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
  2053. direct_table_size = 1UL << table_shift;
  2054. for ( ; levels; --levels) {
  2055. bytes += _ALIGN_UP(tce_table_size, direct_table_size);
  2056. tce_table_size /= direct_table_size;
  2057. tce_table_size <<= 3;
  2058. tce_table_size = _ALIGN_UP(tce_table_size, direct_table_size);
  2059. }
  2060. return bytes;
  2061. }
  2062. static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
  2063. {
  2064. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  2065. table_group);
  2066. /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
  2067. struct iommu_table *tbl = pe->table_group.tables[0];
  2068. pnv_pci_ioda2_set_bypass(pe, false);
  2069. pnv_pci_ioda2_unset_window(&pe->table_group, 0);
  2070. if (pe->pbus)
  2071. pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
  2072. pnv_ioda2_table_free(tbl);
  2073. }
  2074. static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
  2075. {
  2076. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  2077. table_group);
  2078. pnv_pci_ioda2_setup_default_config(pe);
  2079. if (pe->pbus)
  2080. pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
  2081. }
  2082. static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
  2083. .get_table_size = pnv_pci_ioda2_get_table_size,
  2084. .create_table = pnv_pci_ioda2_create_table,
  2085. .set_window = pnv_pci_ioda2_set_window,
  2086. .unset_window = pnv_pci_ioda2_unset_window,
  2087. .take_ownership = pnv_ioda2_take_ownership,
  2088. .release_ownership = pnv_ioda2_release_ownership,
  2089. };
  2090. static int gpe_table_group_to_npe_cb(struct device *dev, void *opaque)
  2091. {
  2092. struct pci_controller *hose;
  2093. struct pnv_phb *phb;
  2094. struct pnv_ioda_pe **ptmppe = opaque;
  2095. struct pci_dev *pdev = container_of(dev, struct pci_dev, dev);
  2096. struct pci_dn *pdn = pci_get_pdn(pdev);
  2097. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  2098. return 0;
  2099. hose = pci_bus_to_host(pdev->bus);
  2100. phb = hose->private_data;
  2101. if (phb->type != PNV_PHB_NPU)
  2102. return 0;
  2103. *ptmppe = &phb->ioda.pe_array[pdn->pe_number];
  2104. return 1;
  2105. }
  2106. /*
  2107. * This returns PE of associated NPU.
  2108. * This assumes that NPU is in the same IOMMU group with GPU and there is
  2109. * no other PEs.
  2110. */
  2111. static struct pnv_ioda_pe *gpe_table_group_to_npe(
  2112. struct iommu_table_group *table_group)
  2113. {
  2114. struct pnv_ioda_pe *npe = NULL;
  2115. int ret = iommu_group_for_each_dev(table_group->group, &npe,
  2116. gpe_table_group_to_npe_cb);
  2117. BUG_ON(!ret || !npe);
  2118. return npe;
  2119. }
  2120. static long pnv_pci_ioda2_npu_set_window(struct iommu_table_group *table_group,
  2121. int num, struct iommu_table *tbl)
  2122. {
  2123. long ret = pnv_pci_ioda2_set_window(table_group, num, tbl);
  2124. if (ret)
  2125. return ret;
  2126. ret = pnv_npu_set_window(gpe_table_group_to_npe(table_group), num, tbl);
  2127. if (ret)
  2128. pnv_pci_ioda2_unset_window(table_group, num);
  2129. return ret;
  2130. }
  2131. static long pnv_pci_ioda2_npu_unset_window(
  2132. struct iommu_table_group *table_group,
  2133. int num)
  2134. {
  2135. long ret = pnv_pci_ioda2_unset_window(table_group, num);
  2136. if (ret)
  2137. return ret;
  2138. return pnv_npu_unset_window(gpe_table_group_to_npe(table_group), num);
  2139. }
  2140. static void pnv_ioda2_npu_take_ownership(struct iommu_table_group *table_group)
  2141. {
  2142. /*
  2143. * Detach NPU first as pnv_ioda2_take_ownership() will destroy
  2144. * the iommu_table if 32bit DMA is enabled.
  2145. */
  2146. pnv_npu_take_ownership(gpe_table_group_to_npe(table_group));
  2147. pnv_ioda2_take_ownership(table_group);
  2148. }
  2149. static struct iommu_table_group_ops pnv_pci_ioda2_npu_ops = {
  2150. .get_table_size = pnv_pci_ioda2_get_table_size,
  2151. .create_table = pnv_pci_ioda2_create_table,
  2152. .set_window = pnv_pci_ioda2_npu_set_window,
  2153. .unset_window = pnv_pci_ioda2_npu_unset_window,
  2154. .take_ownership = pnv_ioda2_npu_take_ownership,
  2155. .release_ownership = pnv_ioda2_release_ownership,
  2156. };
  2157. static void pnv_pci_ioda_setup_iommu_api(void)
  2158. {
  2159. struct pci_controller *hose, *tmp;
  2160. struct pnv_phb *phb;
  2161. struct pnv_ioda_pe *pe, *gpe;
  2162. /*
  2163. * Now we have all PHBs discovered, time to add NPU devices to
  2164. * the corresponding IOMMU groups.
  2165. */
  2166. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  2167. phb = hose->private_data;
  2168. if (phb->type != PNV_PHB_NPU)
  2169. continue;
  2170. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  2171. gpe = pnv_pci_npu_setup_iommu(pe);
  2172. if (gpe)
  2173. gpe->table_group.ops = &pnv_pci_ioda2_npu_ops;
  2174. }
  2175. }
  2176. }
  2177. #else /* !CONFIG_IOMMU_API */
  2178. static void pnv_pci_ioda_setup_iommu_api(void) { };
  2179. #endif
  2180. static __be64 *pnv_pci_ioda2_table_do_alloc_pages(int nid, unsigned shift,
  2181. unsigned levels, unsigned long limit,
  2182. unsigned long *current_offset, unsigned long *total_allocated)
  2183. {
  2184. struct page *tce_mem = NULL;
  2185. __be64 *addr, *tmp;
  2186. unsigned order = max_t(unsigned, shift, PAGE_SHIFT) - PAGE_SHIFT;
  2187. unsigned long allocated = 1UL << (order + PAGE_SHIFT);
  2188. unsigned entries = 1UL << (shift - 3);
  2189. long i;
  2190. tce_mem = alloc_pages_node(nid, GFP_KERNEL, order);
  2191. if (!tce_mem) {
  2192. pr_err("Failed to allocate a TCE memory, order=%d\n", order);
  2193. return NULL;
  2194. }
  2195. addr = page_address(tce_mem);
  2196. memset(addr, 0, allocated);
  2197. *total_allocated += allocated;
  2198. --levels;
  2199. if (!levels) {
  2200. *current_offset += allocated;
  2201. return addr;
  2202. }
  2203. for (i = 0; i < entries; ++i) {
  2204. tmp = pnv_pci_ioda2_table_do_alloc_pages(nid, shift,
  2205. levels, limit, current_offset, total_allocated);
  2206. if (!tmp)
  2207. break;
  2208. addr[i] = cpu_to_be64(__pa(tmp) |
  2209. TCE_PCI_READ | TCE_PCI_WRITE);
  2210. if (*current_offset >= limit)
  2211. break;
  2212. }
  2213. return addr;
  2214. }
  2215. static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
  2216. unsigned long size, unsigned level);
  2217. static long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset,
  2218. __u32 page_shift, __u64 window_size, __u32 levels,
  2219. struct iommu_table *tbl)
  2220. {
  2221. void *addr;
  2222. unsigned long offset = 0, level_shift, total_allocated = 0;
  2223. const unsigned window_shift = ilog2(window_size);
  2224. unsigned entries_shift = window_shift - page_shift;
  2225. unsigned table_shift = max_t(unsigned, entries_shift + 3, PAGE_SHIFT);
  2226. const unsigned long tce_table_size = 1UL << table_shift;
  2227. if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS))
  2228. return -EINVAL;
  2229. if ((window_size > memory_hotplug_max()) || !is_power_of_2(window_size))
  2230. return -EINVAL;
  2231. /* Adjust direct table size from window_size and levels */
  2232. entries_shift = (entries_shift + levels - 1) / levels;
  2233. level_shift = entries_shift + 3;
  2234. level_shift = max_t(unsigned, level_shift, PAGE_SHIFT);
  2235. if ((level_shift - 3) * levels + page_shift >= 60)
  2236. return -EINVAL;
  2237. /* Allocate TCE table */
  2238. addr = pnv_pci_ioda2_table_do_alloc_pages(nid, level_shift,
  2239. levels, tce_table_size, &offset, &total_allocated);
  2240. /* addr==NULL means that the first level allocation failed */
  2241. if (!addr)
  2242. return -ENOMEM;
  2243. /*
  2244. * First level was allocated but some lower level failed as
  2245. * we did not allocate as much as we wanted,
  2246. * release partially allocated table.
  2247. */
  2248. if (offset < tce_table_size) {
  2249. pnv_pci_ioda2_table_do_free_pages(addr,
  2250. 1ULL << (level_shift - 3), levels - 1);
  2251. return -ENOMEM;
  2252. }
  2253. /* Setup linux iommu table */
  2254. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, bus_offset,
  2255. page_shift);
  2256. tbl->it_level_size = 1ULL << (level_shift - 3);
  2257. tbl->it_indirect_levels = levels - 1;
  2258. tbl->it_allocated_size = total_allocated;
  2259. pr_devel("Created TCE table: ws=%08llx ts=%lx @%08llx\n",
  2260. window_size, tce_table_size, bus_offset);
  2261. return 0;
  2262. }
  2263. static void pnv_pci_ioda2_table_do_free_pages(__be64 *addr,
  2264. unsigned long size, unsigned level)
  2265. {
  2266. const unsigned long addr_ul = (unsigned long) addr &
  2267. ~(TCE_PCI_READ | TCE_PCI_WRITE);
  2268. if (level) {
  2269. long i;
  2270. u64 *tmp = (u64 *) addr_ul;
  2271. for (i = 0; i < size; ++i) {
  2272. unsigned long hpa = be64_to_cpu(tmp[i]);
  2273. if (!(hpa & (TCE_PCI_READ | TCE_PCI_WRITE)))
  2274. continue;
  2275. pnv_pci_ioda2_table_do_free_pages(__va(hpa), size,
  2276. level - 1);
  2277. }
  2278. }
  2279. free_pages(addr_ul, get_order(size << 3));
  2280. }
  2281. static void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl)
  2282. {
  2283. const unsigned long size = tbl->it_indirect_levels ?
  2284. tbl->it_level_size : tbl->it_size;
  2285. if (!tbl->it_size)
  2286. return;
  2287. pnv_pci_ioda2_table_do_free_pages((__be64 *)tbl->it_base, size,
  2288. tbl->it_indirect_levels);
  2289. }
  2290. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  2291. struct pnv_ioda_pe *pe)
  2292. {
  2293. int64_t rc;
  2294. if (!pnv_pci_ioda_pe_dma_weight(pe))
  2295. return;
  2296. /* TVE #1 is selected by PCI address bit 59 */
  2297. pe->tce_bypass_base = 1ull << 59;
  2298. iommu_register_group(&pe->table_group, phb->hose->global_number,
  2299. pe->pe_number);
  2300. /* The PE will reserve all possible 32-bits space */
  2301. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  2302. phb->ioda.m32_pci_base);
  2303. /* Setup linux iommu table */
  2304. pe->table_group.tce32_start = 0;
  2305. pe->table_group.tce32_size = phb->ioda.m32_pci_base;
  2306. pe->table_group.max_dynamic_windows_supported =
  2307. IOMMU_TABLE_GROUP_MAX_TABLES;
  2308. pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
  2309. pe->table_group.pgsizes = SZ_4K | SZ_64K | SZ_16M;
  2310. #ifdef CONFIG_IOMMU_API
  2311. pe->table_group.ops = &pnv_pci_ioda2_ops;
  2312. #endif
  2313. rc = pnv_pci_ioda2_setup_default_config(pe);
  2314. if (rc)
  2315. return;
  2316. if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
  2317. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  2318. }
  2319. #ifdef CONFIG_PCI_MSI
  2320. int64_t pnv_opal_pci_msi_eoi(struct irq_chip *chip, unsigned int hw_irq)
  2321. {
  2322. struct pnv_phb *phb = container_of(chip, struct pnv_phb,
  2323. ioda.irq_chip);
  2324. return opal_pci_msi_eoi(phb->opal_id, hw_irq);
  2325. }
  2326. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  2327. {
  2328. int64_t rc;
  2329. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  2330. struct irq_chip *chip = irq_data_get_irq_chip(d);
  2331. rc = pnv_opal_pci_msi_eoi(chip, hw_irq);
  2332. WARN_ON_ONCE(rc);
  2333. icp_native_eoi(d);
  2334. }
  2335. void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
  2336. {
  2337. struct irq_data *idata;
  2338. struct irq_chip *ichip;
  2339. /* The MSI EOI OPAL call is only needed on PHB3 */
  2340. if (phb->model != PNV_PHB_MODEL_PHB3)
  2341. return;
  2342. if (!phb->ioda.irq_chip_init) {
  2343. /*
  2344. * First time we setup an MSI IRQ, we need to setup the
  2345. * corresponding IRQ chip to route correctly.
  2346. */
  2347. idata = irq_get_irq_data(virq);
  2348. ichip = irq_data_get_irq_chip(idata);
  2349. phb->ioda.irq_chip_init = 1;
  2350. phb->ioda.irq_chip = *ichip;
  2351. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  2352. }
  2353. irq_set_chip(virq, &phb->ioda.irq_chip);
  2354. }
  2355. /*
  2356. * Returns true iff chip is something that we could call
  2357. * pnv_opal_pci_msi_eoi for.
  2358. */
  2359. bool is_pnv_opal_msi(struct irq_chip *chip)
  2360. {
  2361. return chip->irq_eoi == pnv_ioda2_msi_eoi;
  2362. }
  2363. EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
  2364. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  2365. unsigned int hwirq, unsigned int virq,
  2366. unsigned int is_64, struct msi_msg *msg)
  2367. {
  2368. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  2369. unsigned int xive_num = hwirq - phb->msi_base;
  2370. __be32 data;
  2371. int rc;
  2372. /* No PE assigned ? bail out ... no MSI for you ! */
  2373. if (pe == NULL)
  2374. return -ENXIO;
  2375. /* Check if we have an MVE */
  2376. if (pe->mve_number < 0)
  2377. return -ENXIO;
  2378. /* Force 32-bit MSI on some broken devices */
  2379. if (dev->no_64bit_msi)
  2380. is_64 = 0;
  2381. /* Assign XIVE to PE */
  2382. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  2383. if (rc) {
  2384. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  2385. pci_name(dev), rc, xive_num);
  2386. return -EIO;
  2387. }
  2388. if (is_64) {
  2389. __be64 addr64;
  2390. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  2391. &addr64, &data);
  2392. if (rc) {
  2393. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  2394. pci_name(dev), rc);
  2395. return -EIO;
  2396. }
  2397. msg->address_hi = be64_to_cpu(addr64) >> 32;
  2398. msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
  2399. } else {
  2400. __be32 addr32;
  2401. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  2402. &addr32, &data);
  2403. if (rc) {
  2404. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  2405. pci_name(dev), rc);
  2406. return -EIO;
  2407. }
  2408. msg->address_hi = 0;
  2409. msg->address_lo = be32_to_cpu(addr32);
  2410. }
  2411. msg->data = be32_to_cpu(data);
  2412. pnv_set_msi_irq_chip(phb, virq);
  2413. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  2414. " address=%x_%08x data=%x PE# %x\n",
  2415. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  2416. msg->address_hi, msg->address_lo, data, pe->pe_number);
  2417. return 0;
  2418. }
  2419. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  2420. {
  2421. unsigned int count;
  2422. const __be32 *prop = of_get_property(phb->hose->dn,
  2423. "ibm,opal-msi-ranges", NULL);
  2424. if (!prop) {
  2425. /* BML Fallback */
  2426. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  2427. }
  2428. if (!prop)
  2429. return;
  2430. phb->msi_base = be32_to_cpup(prop);
  2431. count = be32_to_cpup(prop + 1);
  2432. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  2433. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  2434. phb->hose->global_number);
  2435. return;
  2436. }
  2437. phb->msi_setup = pnv_pci_ioda_msi_setup;
  2438. phb->msi32_support = 1;
  2439. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  2440. count, phb->msi_base);
  2441. }
  2442. #else
  2443. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  2444. #endif /* CONFIG_PCI_MSI */
  2445. #ifdef CONFIG_PCI_IOV
  2446. static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
  2447. {
  2448. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  2449. struct pnv_phb *phb = hose->private_data;
  2450. const resource_size_t gate = phb->ioda.m64_segsize >> 2;
  2451. struct resource *res;
  2452. int i;
  2453. resource_size_t size, total_vf_bar_sz;
  2454. struct pci_dn *pdn;
  2455. int mul, total_vfs;
  2456. if (!pdev->is_physfn || pdev->is_added)
  2457. return;
  2458. pdn = pci_get_pdn(pdev);
  2459. pdn->vfs_expanded = 0;
  2460. pdn->m64_single_mode = false;
  2461. total_vfs = pci_sriov_get_totalvfs(pdev);
  2462. mul = phb->ioda.total_pe_num;
  2463. total_vf_bar_sz = 0;
  2464. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  2465. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  2466. if (!res->flags || res->parent)
  2467. continue;
  2468. if (!pnv_pci_is_m64_flags(res->flags)) {
  2469. dev_warn(&pdev->dev, "Don't support SR-IOV with"
  2470. " non M64 VF BAR%d: %pR. \n",
  2471. i, res);
  2472. goto truncate_iov;
  2473. }
  2474. total_vf_bar_sz += pci_iov_resource_size(pdev,
  2475. i + PCI_IOV_RESOURCES);
  2476. /*
  2477. * If bigger than quarter of M64 segment size, just round up
  2478. * power of two.
  2479. *
  2480. * Generally, one M64 BAR maps one IOV BAR. To avoid conflict
  2481. * with other devices, IOV BAR size is expanded to be
  2482. * (total_pe * VF_BAR_size). When VF_BAR_size is half of M64
  2483. * segment size , the expanded size would equal to half of the
  2484. * whole M64 space size, which will exhaust the M64 Space and
  2485. * limit the system flexibility. This is a design decision to
  2486. * set the boundary to quarter of the M64 segment size.
  2487. */
  2488. if (total_vf_bar_sz > gate) {
  2489. mul = roundup_pow_of_two(total_vfs);
  2490. dev_info(&pdev->dev,
  2491. "VF BAR Total IOV size %llx > %llx, roundup to %d VFs\n",
  2492. total_vf_bar_sz, gate, mul);
  2493. pdn->m64_single_mode = true;
  2494. break;
  2495. }
  2496. }
  2497. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  2498. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  2499. if (!res->flags || res->parent)
  2500. continue;
  2501. size = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
  2502. /*
  2503. * On PHB3, the minimum size alignment of M64 BAR in single
  2504. * mode is 32MB.
  2505. */
  2506. if (pdn->m64_single_mode && (size < SZ_32M))
  2507. goto truncate_iov;
  2508. dev_dbg(&pdev->dev, " Fixing VF BAR%d: %pR to\n", i, res);
  2509. res->end = res->start + size * mul - 1;
  2510. dev_dbg(&pdev->dev, " %pR\n", res);
  2511. dev_info(&pdev->dev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
  2512. i, res, mul);
  2513. }
  2514. pdn->vfs_expanded = mul;
  2515. return;
  2516. truncate_iov:
  2517. /* To save MMIO space, IOV BAR is truncated. */
  2518. for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
  2519. res = &pdev->resource[i + PCI_IOV_RESOURCES];
  2520. res->flags = 0;
  2521. res->end = res->start - 1;
  2522. }
  2523. }
  2524. #endif /* CONFIG_PCI_IOV */
  2525. static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
  2526. struct resource *res)
  2527. {
  2528. struct pnv_phb *phb = pe->phb;
  2529. struct pci_bus_region region;
  2530. int index;
  2531. int64_t rc;
  2532. if (!res || !res->flags || res->start > res->end)
  2533. return;
  2534. if (res->flags & IORESOURCE_IO) {
  2535. region.start = res->start - phb->ioda.io_pci_base;
  2536. region.end = res->end - phb->ioda.io_pci_base;
  2537. index = region.start / phb->ioda.io_segsize;
  2538. while (index < phb->ioda.total_pe_num &&
  2539. region.start <= region.end) {
  2540. phb->ioda.io_segmap[index] = pe->pe_number;
  2541. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2542. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  2543. if (rc != OPAL_SUCCESS) {
  2544. pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
  2545. __func__, rc, index, pe->pe_number);
  2546. break;
  2547. }
  2548. region.start += phb->ioda.io_segsize;
  2549. index++;
  2550. }
  2551. } else if ((res->flags & IORESOURCE_MEM) &&
  2552. !pnv_pci_is_m64(phb, res)) {
  2553. region.start = res->start -
  2554. phb->hose->mem_offset[0] -
  2555. phb->ioda.m32_pci_base;
  2556. region.end = res->end -
  2557. phb->hose->mem_offset[0] -
  2558. phb->ioda.m32_pci_base;
  2559. index = region.start / phb->ioda.m32_segsize;
  2560. while (index < phb->ioda.total_pe_num &&
  2561. region.start <= region.end) {
  2562. phb->ioda.m32_segmap[index] = pe->pe_number;
  2563. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2564. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  2565. if (rc != OPAL_SUCCESS) {
  2566. pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
  2567. __func__, rc, index, pe->pe_number);
  2568. break;
  2569. }
  2570. region.start += phb->ioda.m32_segsize;
  2571. index++;
  2572. }
  2573. }
  2574. }
  2575. /*
  2576. * This function is supposed to be called on basis of PE from top
  2577. * to bottom style. So the the I/O or MMIO segment assigned to
  2578. * parent PE could be overridden by its child PEs if necessary.
  2579. */
  2580. static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
  2581. {
  2582. struct pci_dev *pdev;
  2583. int i;
  2584. /*
  2585. * NOTE: We only care PCI bus based PE for now. For PCI
  2586. * device based PE, for example SRIOV sensitive VF should
  2587. * be figured out later.
  2588. */
  2589. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  2590. list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
  2591. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  2592. pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
  2593. /*
  2594. * If the PE contains all subordinate PCI buses, the
  2595. * windows of the child bridges should be mapped to
  2596. * the PE as well.
  2597. */
  2598. if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
  2599. continue;
  2600. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  2601. pnv_ioda_setup_pe_res(pe,
  2602. &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
  2603. }
  2604. }
  2605. #ifdef CONFIG_DEBUG_FS
  2606. static int pnv_pci_diag_data_set(void *data, u64 val)
  2607. {
  2608. struct pci_controller *hose;
  2609. struct pnv_phb *phb;
  2610. s64 ret;
  2611. if (val != 1ULL)
  2612. return -EINVAL;
  2613. hose = (struct pci_controller *)data;
  2614. if (!hose || !hose->private_data)
  2615. return -ENODEV;
  2616. phb = hose->private_data;
  2617. /* Retrieve the diag data from firmware */
  2618. ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag.blob,
  2619. PNV_PCI_DIAG_BUF_SIZE);
  2620. if (ret != OPAL_SUCCESS)
  2621. return -EIO;
  2622. /* Print the diag data to the kernel log */
  2623. pnv_pci_dump_phb_diag_data(phb->hose, phb->diag.blob);
  2624. return 0;
  2625. }
  2626. DEFINE_SIMPLE_ATTRIBUTE(pnv_pci_diag_data_fops, NULL,
  2627. pnv_pci_diag_data_set, "%llu\n");
  2628. #endif /* CONFIG_DEBUG_FS */
  2629. static void pnv_pci_ioda_create_dbgfs(void)
  2630. {
  2631. #ifdef CONFIG_DEBUG_FS
  2632. struct pci_controller *hose, *tmp;
  2633. struct pnv_phb *phb;
  2634. char name[16];
  2635. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  2636. phb = hose->private_data;
  2637. /* Notify initialization of PHB done */
  2638. phb->initialized = 1;
  2639. sprintf(name, "PCI%04x", hose->global_number);
  2640. phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
  2641. if (!phb->dbgfs) {
  2642. pr_warning("%s: Error on creating debugfs on PHB#%x\n",
  2643. __func__, hose->global_number);
  2644. continue;
  2645. }
  2646. debugfs_create_file("dump_diag_regs", 0200, phb->dbgfs, hose,
  2647. &pnv_pci_diag_data_fops);
  2648. }
  2649. #endif /* CONFIG_DEBUG_FS */
  2650. }
  2651. static void pnv_pci_ioda_fixup(void)
  2652. {
  2653. pnv_pci_ioda_setup_PEs();
  2654. pnv_pci_ioda_setup_iommu_api();
  2655. pnv_pci_ioda_create_dbgfs();
  2656. #ifdef CONFIG_EEH
  2657. eeh_init();
  2658. eeh_addr_cache_build();
  2659. #endif
  2660. }
  2661. /*
  2662. * Returns the alignment for I/O or memory windows for P2P
  2663. * bridges. That actually depends on how PEs are segmented.
  2664. * For now, we return I/O or M32 segment size for PE sensitive
  2665. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  2666. * 1MiB for memory) will be returned.
  2667. *
  2668. * The current PCI bus might be put into one PE, which was
  2669. * create against the parent PCI bridge. For that case, we
  2670. * needn't enlarge the alignment so that we can save some
  2671. * resources.
  2672. */
  2673. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  2674. unsigned long type)
  2675. {
  2676. struct pci_dev *bridge;
  2677. struct pci_controller *hose = pci_bus_to_host(bus);
  2678. struct pnv_phb *phb = hose->private_data;
  2679. int num_pci_bridges = 0;
  2680. bridge = bus->self;
  2681. while (bridge) {
  2682. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  2683. num_pci_bridges++;
  2684. if (num_pci_bridges >= 2)
  2685. return 1;
  2686. }
  2687. bridge = bridge->bus->self;
  2688. }
  2689. /*
  2690. * We fall back to M32 if M64 isn't supported. We enforce the M64
  2691. * alignment for any 64-bit resource, PCIe doesn't care and
  2692. * bridges only do 64-bit prefetchable anyway.
  2693. */
  2694. if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
  2695. return phb->ioda.m64_segsize;
  2696. if (type & IORESOURCE_MEM)
  2697. return phb->ioda.m32_segsize;
  2698. return phb->ioda.io_segsize;
  2699. }
  2700. /*
  2701. * We are updating root port or the upstream port of the
  2702. * bridge behind the root port with PHB's windows in order
  2703. * to accommodate the changes on required resources during
  2704. * PCI (slot) hotplug, which is connected to either root
  2705. * port or the downstream ports of PCIe switch behind the
  2706. * root port.
  2707. */
  2708. static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
  2709. unsigned long type)
  2710. {
  2711. struct pci_controller *hose = pci_bus_to_host(bus);
  2712. struct pnv_phb *phb = hose->private_data;
  2713. struct pci_dev *bridge = bus->self;
  2714. struct resource *r, *w;
  2715. bool msi_region = false;
  2716. int i;
  2717. /* Check if we need apply fixup to the bridge's windows */
  2718. if (!pci_is_root_bus(bridge->bus) &&
  2719. !pci_is_root_bus(bridge->bus->self->bus))
  2720. return;
  2721. /* Fixup the resources */
  2722. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  2723. r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
  2724. if (!r->flags || !r->parent)
  2725. continue;
  2726. w = NULL;
  2727. if (r->flags & type & IORESOURCE_IO)
  2728. w = &hose->io_resource;
  2729. else if (pnv_pci_is_m64(phb, r) &&
  2730. (type & IORESOURCE_PREFETCH) &&
  2731. phb->ioda.m64_segsize)
  2732. w = &hose->mem_resources[1];
  2733. else if (r->flags & type & IORESOURCE_MEM) {
  2734. w = &hose->mem_resources[0];
  2735. msi_region = true;
  2736. }
  2737. r->start = w->start;
  2738. r->end = w->end;
  2739. /* The 64KB 32-bits MSI region shouldn't be included in
  2740. * the 32-bits bridge window. Otherwise, we can see strange
  2741. * issues. One of them is EEH error observed on Garrison.
  2742. *
  2743. * Exclude top 1MB region which is the minimal alignment of
  2744. * 32-bits bridge window.
  2745. */
  2746. if (msi_region) {
  2747. r->end += 0x10000;
  2748. r->end -= 0x100000;
  2749. }
  2750. }
  2751. }
  2752. static void pnv_pci_setup_bridge(struct pci_bus *bus, unsigned long type)
  2753. {
  2754. struct pci_controller *hose = pci_bus_to_host(bus);
  2755. struct pnv_phb *phb = hose->private_data;
  2756. struct pci_dev *bridge = bus->self;
  2757. struct pnv_ioda_pe *pe;
  2758. bool all = (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
  2759. /* Extend bridge's windows if necessary */
  2760. pnv_pci_fixup_bridge_resources(bus, type);
  2761. /* The PE for root bus should be realized before any one else */
  2762. if (!phb->ioda.root_pe_populated) {
  2763. pe = pnv_ioda_setup_bus_PE(phb->hose->bus, false);
  2764. if (pe) {
  2765. phb->ioda.root_pe_idx = pe->pe_number;
  2766. phb->ioda.root_pe_populated = true;
  2767. }
  2768. }
  2769. /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
  2770. if (list_empty(&bus->devices))
  2771. return;
  2772. /* Reserve PEs according to used M64 resources */
  2773. if (phb->reserve_m64_pe)
  2774. phb->reserve_m64_pe(bus, NULL, all);
  2775. /*
  2776. * Assign PE. We might run here because of partial hotplug.
  2777. * For the case, we just pick up the existing PE and should
  2778. * not allocate resources again.
  2779. */
  2780. pe = pnv_ioda_setup_bus_PE(bus, all);
  2781. if (!pe)
  2782. return;
  2783. pnv_ioda_setup_pe_seg(pe);
  2784. switch (phb->type) {
  2785. case PNV_PHB_IODA1:
  2786. pnv_pci_ioda1_setup_dma_pe(phb, pe);
  2787. break;
  2788. case PNV_PHB_IODA2:
  2789. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  2790. break;
  2791. default:
  2792. pr_warn("%s: No DMA for PHB#%x (type %d)\n",
  2793. __func__, phb->hose->global_number, phb->type);
  2794. }
  2795. }
  2796. #ifdef CONFIG_PCI_IOV
  2797. static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
  2798. int resno)
  2799. {
  2800. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  2801. struct pnv_phb *phb = hose->private_data;
  2802. struct pci_dn *pdn = pci_get_pdn(pdev);
  2803. resource_size_t align;
  2804. /*
  2805. * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
  2806. * SR-IOV. While from hardware perspective, the range mapped by M64
  2807. * BAR should be size aligned.
  2808. *
  2809. * When IOV BAR is mapped with M64 BAR in Single PE mode, the extra
  2810. * powernv-specific hardware restriction is gone. But if just use the
  2811. * VF BAR size as the alignment, PF BAR / VF BAR may be allocated with
  2812. * in one segment of M64 #15, which introduces the PE conflict between
  2813. * PF and VF. Based on this, the minimum alignment of an IOV BAR is
  2814. * m64_segsize.
  2815. *
  2816. * This function returns the total IOV BAR size if M64 BAR is in
  2817. * Shared PE mode or just VF BAR size if not.
  2818. * If the M64 BAR is in Single PE mode, return the VF BAR size or
  2819. * M64 segment size if IOV BAR size is less.
  2820. */
  2821. align = pci_iov_resource_size(pdev, resno);
  2822. if (!pdn->vfs_expanded)
  2823. return align;
  2824. if (pdn->m64_single_mode)
  2825. return max(align, (resource_size_t)phb->ioda.m64_segsize);
  2826. return pdn->vfs_expanded * align;
  2827. }
  2828. #endif /* CONFIG_PCI_IOV */
  2829. /* Prevent enabling devices for which we couldn't properly
  2830. * assign a PE
  2831. */
  2832. bool pnv_pci_enable_device_hook(struct pci_dev *dev)
  2833. {
  2834. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  2835. struct pnv_phb *phb = hose->private_data;
  2836. struct pci_dn *pdn;
  2837. /* The function is probably called while the PEs have
  2838. * not be created yet. For example, resource reassignment
  2839. * during PCI probe period. We just skip the check if
  2840. * PEs isn't ready.
  2841. */
  2842. if (!phb->initialized)
  2843. return true;
  2844. pdn = pci_get_pdn(dev);
  2845. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  2846. return false;
  2847. return true;
  2848. }
  2849. static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group,
  2850. int num)
  2851. {
  2852. struct pnv_ioda_pe *pe = container_of(table_group,
  2853. struct pnv_ioda_pe, table_group);
  2854. struct pnv_phb *phb = pe->phb;
  2855. unsigned int idx;
  2856. long rc;
  2857. pe_info(pe, "Removing DMA window #%d\n", num);
  2858. for (idx = 0; idx < phb->ioda.dma32_count; idx++) {
  2859. if (phb->ioda.dma32_segmap[idx] != pe->pe_number)
  2860. continue;
  2861. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  2862. idx, 0, 0ul, 0ul, 0ul);
  2863. if (rc != OPAL_SUCCESS) {
  2864. pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n",
  2865. rc, idx);
  2866. return rc;
  2867. }
  2868. phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE;
  2869. }
  2870. pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
  2871. return OPAL_SUCCESS;
  2872. }
  2873. static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
  2874. {
  2875. unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
  2876. struct iommu_table *tbl = pe->table_group.tables[0];
  2877. int64_t rc;
  2878. if (!weight)
  2879. return;
  2880. rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0);
  2881. if (rc != OPAL_SUCCESS)
  2882. return;
  2883. pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size, false);
  2884. if (pe->table_group.group) {
  2885. iommu_group_put(pe->table_group.group);
  2886. WARN_ON(pe->table_group.group);
  2887. }
  2888. free_pages(tbl->it_base, get_order(tbl->it_size << 3));
  2889. iommu_free_table(tbl, "pnv");
  2890. }
  2891. static void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
  2892. {
  2893. struct iommu_table *tbl = pe->table_group.tables[0];
  2894. unsigned int weight = pnv_pci_ioda_pe_dma_weight(pe);
  2895. #ifdef CONFIG_IOMMU_API
  2896. int64_t rc;
  2897. #endif
  2898. if (!weight)
  2899. return;
  2900. #ifdef CONFIG_IOMMU_API
  2901. rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
  2902. if (rc)
  2903. pe_warn(pe, "OPAL error %ld release DMA window\n", rc);
  2904. #endif
  2905. pnv_pci_ioda2_set_bypass(pe, false);
  2906. if (pe->table_group.group) {
  2907. iommu_group_put(pe->table_group.group);
  2908. WARN_ON(pe->table_group.group);
  2909. }
  2910. pnv_pci_ioda2_table_free_pages(tbl);
  2911. iommu_free_table(tbl, "pnv");
  2912. }
  2913. static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
  2914. unsigned short win,
  2915. unsigned int *map)
  2916. {
  2917. struct pnv_phb *phb = pe->phb;
  2918. int idx;
  2919. int64_t rc;
  2920. for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
  2921. if (map[idx] != pe->pe_number)
  2922. continue;
  2923. if (win == OPAL_M64_WINDOW_TYPE)
  2924. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2925. phb->ioda.reserved_pe_idx, win,
  2926. idx / PNV_IODA1_M64_SEGS,
  2927. idx % PNV_IODA1_M64_SEGS);
  2928. else
  2929. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2930. phb->ioda.reserved_pe_idx, win, 0, idx);
  2931. if (rc != OPAL_SUCCESS)
  2932. pe_warn(pe, "Error %ld unmapping (%d) segment#%d\n",
  2933. rc, win, idx);
  2934. map[idx] = IODA_INVALID_PE;
  2935. }
  2936. }
  2937. static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
  2938. {
  2939. struct pnv_phb *phb = pe->phb;
  2940. if (phb->type == PNV_PHB_IODA1) {
  2941. pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE,
  2942. phb->ioda.io_segmap);
  2943. pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
  2944. phb->ioda.m32_segmap);
  2945. pnv_ioda_free_pe_seg(pe, OPAL_M64_WINDOW_TYPE,
  2946. phb->ioda.m64_segmap);
  2947. } else if (phb->type == PNV_PHB_IODA2) {
  2948. pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
  2949. phb->ioda.m32_segmap);
  2950. }
  2951. }
  2952. static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
  2953. {
  2954. struct pnv_phb *phb = pe->phb;
  2955. struct pnv_ioda_pe *slave, *tmp;
  2956. list_del(&pe->list);
  2957. switch (phb->type) {
  2958. case PNV_PHB_IODA1:
  2959. pnv_pci_ioda1_release_pe_dma(pe);
  2960. break;
  2961. case PNV_PHB_IODA2:
  2962. pnv_pci_ioda2_release_pe_dma(pe);
  2963. break;
  2964. default:
  2965. WARN_ON(1);
  2966. }
  2967. pnv_ioda_release_pe_seg(pe);
  2968. pnv_ioda_deconfigure_pe(pe->phb, pe);
  2969. /* Release slave PEs in the compound PE */
  2970. if (pe->flags & PNV_IODA_PE_MASTER) {
  2971. list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
  2972. list_del(&slave->list);
  2973. pnv_ioda_free_pe(slave);
  2974. }
  2975. }
  2976. /*
  2977. * The PE for root bus can be removed because of hotplug in EEH
  2978. * recovery for fenced PHB error. We need to mark the PE dead so
  2979. * that it can be populated again in PCI hot add path. The PE
  2980. * shouldn't be destroyed as it's the global reserved resource.
  2981. */
  2982. if (phb->ioda.root_pe_populated &&
  2983. phb->ioda.root_pe_idx == pe->pe_number)
  2984. phb->ioda.root_pe_populated = false;
  2985. else
  2986. pnv_ioda_free_pe(pe);
  2987. }
  2988. static void pnv_pci_release_device(struct pci_dev *pdev)
  2989. {
  2990. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  2991. struct pnv_phb *phb = hose->private_data;
  2992. struct pci_dn *pdn = pci_get_pdn(pdev);
  2993. struct pnv_ioda_pe *pe;
  2994. if (pdev->is_virtfn)
  2995. return;
  2996. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  2997. return;
  2998. /*
  2999. * PCI hotplug can happen as part of EEH error recovery. The @pdn
  3000. * isn't removed and added afterwards in this scenario. We should
  3001. * set the PE number in @pdn to an invalid one. Otherwise, the PE's
  3002. * device count is decreased on removing devices while failing to
  3003. * be increased on adding devices. It leads to unbalanced PE's device
  3004. * count and eventually make normal PCI hotplug path broken.
  3005. */
  3006. pe = &phb->ioda.pe_array[pdn->pe_number];
  3007. pdn->pe_number = IODA_INVALID_PE;
  3008. WARN_ON(--pe->device_count < 0);
  3009. if (pe->device_count == 0)
  3010. pnv_ioda_release_pe(pe);
  3011. }
  3012. static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
  3013. {
  3014. struct pnv_phb *phb = hose->private_data;
  3015. opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
  3016. OPAL_ASSERT_RESET);
  3017. }
  3018. static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
  3019. .dma_dev_setup = pnv_pci_dma_dev_setup,
  3020. .dma_bus_setup = pnv_pci_dma_bus_setup,
  3021. #ifdef CONFIG_PCI_MSI
  3022. .setup_msi_irqs = pnv_setup_msi_irqs,
  3023. .teardown_msi_irqs = pnv_teardown_msi_irqs,
  3024. #endif
  3025. .enable_device_hook = pnv_pci_enable_device_hook,
  3026. .release_device = pnv_pci_release_device,
  3027. .window_alignment = pnv_pci_window_alignment,
  3028. .setup_bridge = pnv_pci_setup_bridge,
  3029. .reset_secondary_bus = pnv_pci_reset_secondary_bus,
  3030. .dma_set_mask = pnv_pci_ioda_dma_set_mask,
  3031. .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
  3032. .shutdown = pnv_pci_ioda_shutdown,
  3033. };
  3034. static int pnv_npu_dma_set_mask(struct pci_dev *npdev, u64 dma_mask)
  3035. {
  3036. dev_err_once(&npdev->dev,
  3037. "%s operation unsupported for NVLink devices\n",
  3038. __func__);
  3039. return -EPERM;
  3040. }
  3041. static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
  3042. .dma_dev_setup = pnv_pci_dma_dev_setup,
  3043. #ifdef CONFIG_PCI_MSI
  3044. .setup_msi_irqs = pnv_setup_msi_irqs,
  3045. .teardown_msi_irqs = pnv_teardown_msi_irqs,
  3046. #endif
  3047. .enable_device_hook = pnv_pci_enable_device_hook,
  3048. .window_alignment = pnv_pci_window_alignment,
  3049. .reset_secondary_bus = pnv_pci_reset_secondary_bus,
  3050. .dma_set_mask = pnv_npu_dma_set_mask,
  3051. .shutdown = pnv_pci_ioda_shutdown,
  3052. };
  3053. #ifdef CONFIG_CXL_BASE
  3054. const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops = {
  3055. .dma_dev_setup = pnv_pci_dma_dev_setup,
  3056. .dma_bus_setup = pnv_pci_dma_bus_setup,
  3057. #ifdef CONFIG_PCI_MSI
  3058. .setup_msi_irqs = pnv_cxl_cx4_setup_msi_irqs,
  3059. .teardown_msi_irqs = pnv_cxl_cx4_teardown_msi_irqs,
  3060. #endif
  3061. .enable_device_hook = pnv_cxl_enable_device_hook,
  3062. .disable_device = pnv_cxl_disable_device,
  3063. .release_device = pnv_pci_release_device,
  3064. .window_alignment = pnv_pci_window_alignment,
  3065. .setup_bridge = pnv_pci_setup_bridge,
  3066. .reset_secondary_bus = pnv_pci_reset_secondary_bus,
  3067. .dma_set_mask = pnv_pci_ioda_dma_set_mask,
  3068. .dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
  3069. .shutdown = pnv_pci_ioda_shutdown,
  3070. };
  3071. #endif
  3072. static void __init pnv_pci_init_ioda_phb(struct device_node *np,
  3073. u64 hub_id, int ioda_type)
  3074. {
  3075. struct pci_controller *hose;
  3076. struct pnv_phb *phb;
  3077. unsigned long size, m64map_off, m32map_off, pemap_off;
  3078. unsigned long iomap_off = 0, dma32map_off = 0;
  3079. struct resource r;
  3080. const __be64 *prop64;
  3081. const __be32 *prop32;
  3082. int len;
  3083. unsigned int segno;
  3084. u64 phb_id;
  3085. void *aux;
  3086. long rc;
  3087. if (!of_device_is_available(np))
  3088. return;
  3089. pr_info("Initializing %s PHB (%s)\n",
  3090. pnv_phb_names[ioda_type], of_node_full_name(np));
  3091. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  3092. if (!prop64) {
  3093. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  3094. return;
  3095. }
  3096. phb_id = be64_to_cpup(prop64);
  3097. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  3098. phb = memblock_virt_alloc(sizeof(struct pnv_phb), 0);
  3099. /* Allocate PCI controller */
  3100. phb->hose = hose = pcibios_alloc_controller(np);
  3101. if (!phb->hose) {
  3102. pr_err(" Can't allocate PCI controller for %s\n",
  3103. np->full_name);
  3104. memblock_free(__pa(phb), sizeof(struct pnv_phb));
  3105. return;
  3106. }
  3107. spin_lock_init(&phb->lock);
  3108. prop32 = of_get_property(np, "bus-range", &len);
  3109. if (prop32 && len == 8) {
  3110. hose->first_busno = be32_to_cpu(prop32[0]);
  3111. hose->last_busno = be32_to_cpu(prop32[1]);
  3112. } else {
  3113. pr_warn(" Broken <bus-range> on %s\n", np->full_name);
  3114. hose->first_busno = 0;
  3115. hose->last_busno = 0xff;
  3116. }
  3117. hose->private_data = phb;
  3118. phb->hub_id = hub_id;
  3119. phb->opal_id = phb_id;
  3120. phb->type = ioda_type;
  3121. mutex_init(&phb->ioda.pe_alloc_mutex);
  3122. /* Detect specific models for error handling */
  3123. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  3124. phb->model = PNV_PHB_MODEL_P7IOC;
  3125. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  3126. phb->model = PNV_PHB_MODEL_PHB3;
  3127. else if (of_device_is_compatible(np, "ibm,power8-npu-pciex"))
  3128. phb->model = PNV_PHB_MODEL_NPU;
  3129. else if (of_device_is_compatible(np, "ibm,power9-npu-pciex"))
  3130. phb->model = PNV_PHB_MODEL_NPU2;
  3131. else
  3132. phb->model = PNV_PHB_MODEL_UNKNOWN;
  3133. /* Parse 32-bit and IO ranges (if any) */
  3134. pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
  3135. /* Get registers */
  3136. if (!of_address_to_resource(np, 0, &r)) {
  3137. phb->regs_phys = r.start;
  3138. phb->regs = ioremap(r.start, resource_size(&r));
  3139. if (phb->regs == NULL)
  3140. pr_err(" Failed to map registers !\n");
  3141. }
  3142. /* Initialize more IODA stuff */
  3143. phb->ioda.total_pe_num = 1;
  3144. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  3145. if (prop32)
  3146. phb->ioda.total_pe_num = be32_to_cpup(prop32);
  3147. prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
  3148. if (prop32)
  3149. phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
  3150. /* Invalidate RID to PE# mapping */
  3151. for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
  3152. phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
  3153. /* Parse 64-bit MMIO range */
  3154. pnv_ioda_parse_m64_window(phb);
  3155. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  3156. /* FW Has already off top 64k of M32 space (MSI space) */
  3157. phb->ioda.m32_size += 0x10000;
  3158. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
  3159. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  3160. phb->ioda.io_size = hose->pci_io_size;
  3161. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
  3162. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  3163. /* Calculate how many 32-bit TCE segments we have */
  3164. phb->ioda.dma32_count = phb->ioda.m32_pci_base /
  3165. PNV_IODA1_DMA32_SEGSIZE;
  3166. /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
  3167. size = _ALIGN_UP(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
  3168. sizeof(unsigned long));
  3169. m64map_off = size;
  3170. size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
  3171. m32map_off = size;
  3172. size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
  3173. if (phb->type == PNV_PHB_IODA1) {
  3174. iomap_off = size;
  3175. size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
  3176. dma32map_off = size;
  3177. size += phb->ioda.dma32_count *
  3178. sizeof(phb->ioda.dma32_segmap[0]);
  3179. }
  3180. pemap_off = size;
  3181. size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
  3182. aux = memblock_virt_alloc(size, 0);
  3183. phb->ioda.pe_alloc = aux;
  3184. phb->ioda.m64_segmap = aux + m64map_off;
  3185. phb->ioda.m32_segmap = aux + m32map_off;
  3186. for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
  3187. phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
  3188. phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
  3189. }
  3190. if (phb->type == PNV_PHB_IODA1) {
  3191. phb->ioda.io_segmap = aux + iomap_off;
  3192. for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
  3193. phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
  3194. phb->ioda.dma32_segmap = aux + dma32map_off;
  3195. for (segno = 0; segno < phb->ioda.dma32_count; segno++)
  3196. phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
  3197. }
  3198. phb->ioda.pe_array = aux + pemap_off;
  3199. /*
  3200. * Choose PE number for root bus, which shouldn't have
  3201. * M64 resources consumed by its child devices. To pick
  3202. * the PE number adjacent to the reserved one if possible.
  3203. */
  3204. pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
  3205. if (phb->ioda.reserved_pe_idx == 0) {
  3206. phb->ioda.root_pe_idx = 1;
  3207. pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
  3208. } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
  3209. phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
  3210. pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
  3211. } else {
  3212. phb->ioda.root_pe_idx = IODA_INVALID_PE;
  3213. }
  3214. INIT_LIST_HEAD(&phb->ioda.pe_list);
  3215. mutex_init(&phb->ioda.pe_list_mutex);
  3216. /* Calculate how many 32-bit TCE segments we have */
  3217. phb->ioda.dma32_count = phb->ioda.m32_pci_base /
  3218. PNV_IODA1_DMA32_SEGSIZE;
  3219. #if 0 /* We should really do that ... */
  3220. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  3221. window_type,
  3222. window_num,
  3223. starting_real_address,
  3224. starting_pci_address,
  3225. segment_size);
  3226. #endif
  3227. pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
  3228. phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
  3229. phb->ioda.m32_size, phb->ioda.m32_segsize);
  3230. if (phb->ioda.m64_size)
  3231. pr_info(" M64: 0x%lx [segment=0x%lx]\n",
  3232. phb->ioda.m64_size, phb->ioda.m64_segsize);
  3233. if (phb->ioda.io_size)
  3234. pr_info(" IO: 0x%x [segment=0x%x]\n",
  3235. phb->ioda.io_size, phb->ioda.io_segsize);
  3236. phb->hose->ops = &pnv_pci_ops;
  3237. phb->get_pe_state = pnv_ioda_get_pe_state;
  3238. phb->freeze_pe = pnv_ioda_freeze_pe;
  3239. phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
  3240. /* Setup MSI support */
  3241. pnv_pci_init_ioda_msis(phb);
  3242. /*
  3243. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  3244. * to let the PCI core do resource assignment. It's supposed
  3245. * that the PCI core will do correct I/O and MMIO alignment
  3246. * for the P2P bridge bars so that each PCI bus (excluding
  3247. * the child P2P bridges) can form individual PE.
  3248. */
  3249. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  3250. if (phb->type == PNV_PHB_NPU) {
  3251. hose->controller_ops = pnv_npu_ioda_controller_ops;
  3252. } else {
  3253. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  3254. hose->controller_ops = pnv_pci_ioda_controller_ops;
  3255. }
  3256. #ifdef CONFIG_PCI_IOV
  3257. ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
  3258. ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
  3259. #endif
  3260. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  3261. /* Reset IODA tables to a clean state */
  3262. rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
  3263. if (rc)
  3264. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  3265. /*
  3266. * If we're running in kdump kernel, the previous kernel never
  3267. * shutdown PCI devices correctly. We already got IODA table
  3268. * cleaned out. So we have to issue PHB reset to stop all PCI
  3269. * transactions from previous kernel.
  3270. */
  3271. if (is_kdump_kernel()) {
  3272. pr_info(" Issue PHB reset ...\n");
  3273. pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
  3274. pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
  3275. }
  3276. /* Remove M64 resource if we can't configure it successfully */
  3277. if (!phb->init_m64 || phb->init_m64(phb))
  3278. hose->mem_resources[1].flags = 0;
  3279. }
  3280. void __init pnv_pci_init_ioda2_phb(struct device_node *np)
  3281. {
  3282. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
  3283. }
  3284. void __init pnv_pci_init_npu_phb(struct device_node *np)
  3285. {
  3286. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU);
  3287. }
  3288. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  3289. {
  3290. struct device_node *phbn;
  3291. const __be64 *prop64;
  3292. u64 hub_id;
  3293. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  3294. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  3295. if (!prop64) {
  3296. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  3297. return;
  3298. }
  3299. hub_id = be64_to_cpup(prop64);
  3300. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  3301. /* Count child PHBs */
  3302. for_each_child_of_node(np, phbn) {
  3303. /* Look for IODA1 PHBs */
  3304. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  3305. pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
  3306. }
  3307. }