pci-ioda.c 103 KB

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