pci-ioda.c 90 KB

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