pci-ioda.c 103 KB

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