pci-ioda.c 108 KB

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