pci-ioda.c 101 KB

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