pci-ioda.c 86 KB

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