pci-ioda.c 111 KB

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