pci-ioda.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Support PCI/PCIe on PowerNV platforms
  4. *
  5. * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
  6. */
  7. #undef DEBUG
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. #include <linux/crash_dump.h>
  11. #include <linux/delay.h>
  12. #include <linux/string.h>
  13. #include <linux/init.h>
  14. #include <linux/memblock.h>
  15. #include <linux/irq.h>
  16. #include <linux/io.h>
  17. #include <linux/msi.h>
  18. #include <linux/iommu.h>
  19. #include <linux/rculist.h>
  20. #include <linux/sizes.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <asm/sections.h>
  25. #include <asm/io.h>
  26. #include <asm/pci-bridge.h>
  27. #include <asm/machdep.h>
  28. #include <asm/msi_bitmap.h>
  29. #include <asm/ppc-pci.h>
  30. #include <asm/opal.h>
  31. #include <asm/iommu.h>
  32. #include <asm/tce.h>
  33. #include <asm/xics.h>
  34. #include <asm/firmware.h>
  35. #include <asm/pnv-pci.h>
  36. #include <asm/mmzone.h>
  37. #include <asm/xive.h>
  38. #include <misc/cxl-base.h>
  39. #include "powernv.h"
  40. #include "pci.h"
  41. #include "../../../../drivers/pci/pci.h"
  42. #define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
  43. #define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
  44. #define PNV_IODA1_DMA32_SEGSIZE 0x10000000
  45. static const char * const pnv_phb_names[] = { "IODA1", "IODA2", "NPU_OCAPI" };
  46. static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable);
  47. static void pnv_pci_configure_bus(struct pci_bus *bus);
  48. void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
  49. const char *fmt, ...)
  50. {
  51. struct va_format vaf;
  52. va_list args;
  53. char pfix[32];
  54. va_start(args, fmt);
  55. vaf.fmt = fmt;
  56. vaf.va = &args;
  57. if (pe->flags & PNV_IODA_PE_DEV)
  58. strscpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
  59. else if (pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL))
  60. sprintf(pfix, "%04x:%02x ",
  61. pci_domain_nr(pe->pbus), pe->pbus->number);
  62. #ifdef CONFIG_PCI_IOV
  63. else if (pe->flags & PNV_IODA_PE_VF)
  64. sprintf(pfix, "%04x:%02x:%2x.%d",
  65. pci_domain_nr(pe->parent_dev->bus),
  66. (pe->rid & 0xff00) >> 8,
  67. PCI_SLOT(pe->rid), PCI_FUNC(pe->rid));
  68. #endif /* CONFIG_PCI_IOV*/
  69. printk("%spci %s: [PE# %.2x] %pV",
  70. level, pfix, pe->pe_number, &vaf);
  71. va_end(args);
  72. }
  73. static bool pnv_iommu_bypass_disabled __read_mostly;
  74. static bool pci_reset_phbs __read_mostly;
  75. static int __init iommu_setup(char *str)
  76. {
  77. if (!str)
  78. return -EINVAL;
  79. while (*str) {
  80. if (!strncmp(str, "nobypass", 8)) {
  81. pnv_iommu_bypass_disabled = true;
  82. pr_info("PowerNV: IOMMU bypass window disabled.\n");
  83. break;
  84. }
  85. str += strcspn(str, ",");
  86. if (*str == ',')
  87. str++;
  88. }
  89. return 0;
  90. }
  91. early_param("iommu", iommu_setup);
  92. static int __init pci_reset_phbs_setup(char *str)
  93. {
  94. pci_reset_phbs = true;
  95. return 0;
  96. }
  97. early_param("ppc_pci_reset_phbs", pci_reset_phbs_setup);
  98. static struct pnv_ioda_pe *pnv_ioda_init_pe(struct pnv_phb *phb, int pe_no)
  99. {
  100. s64 rc;
  101. phb->ioda.pe_array[pe_no].phb = phb;
  102. phb->ioda.pe_array[pe_no].pe_number = pe_no;
  103. phb->ioda.pe_array[pe_no].dma_setup_done = false;
  104. /*
  105. * Clear the PE frozen state as it might be put into frozen state
  106. * in the last PCI remove path. It's not harmful to do so when the
  107. * PE is already in unfrozen state.
  108. */
  109. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no,
  110. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  111. if (rc != OPAL_SUCCESS && rc != OPAL_UNSUPPORTED)
  112. pr_warn("%s: Error %lld unfreezing PHB#%x-PE#%x\n",
  113. __func__, rc, phb->hose->global_number, pe_no);
  114. return &phb->ioda.pe_array[pe_no];
  115. }
  116. static void pnv_ioda_reserve_pe(struct pnv_phb *phb, int pe_no)
  117. {
  118. if (!(pe_no >= 0 && pe_no < phb->ioda.total_pe_num)) {
  119. pr_warn("%s: Invalid PE %x on PHB#%x\n",
  120. __func__, pe_no, phb->hose->global_number);
  121. return;
  122. }
  123. mutex_lock(&phb->ioda.pe_alloc_mutex);
  124. if (test_and_set_bit(pe_no, phb->ioda.pe_alloc))
  125. pr_debug("%s: PE %x was reserved on PHB#%x\n",
  126. __func__, pe_no, phb->hose->global_number);
  127. mutex_unlock(&phb->ioda.pe_alloc_mutex);
  128. pnv_ioda_init_pe(phb, pe_no);
  129. }
  130. struct pnv_ioda_pe *pnv_ioda_alloc_pe(struct pnv_phb *phb, int count)
  131. {
  132. struct pnv_ioda_pe *ret = NULL;
  133. int run = 0, pe, i;
  134. mutex_lock(&phb->ioda.pe_alloc_mutex);
  135. /* scan backwards for a run of @count cleared bits */
  136. for (pe = phb->ioda.total_pe_num - 1; pe >= 0; pe--) {
  137. if (test_bit(pe, phb->ioda.pe_alloc)) {
  138. run = 0;
  139. continue;
  140. }
  141. run++;
  142. if (run == count)
  143. break;
  144. }
  145. if (run != count)
  146. goto out;
  147. for (i = pe; i < pe + count; i++) {
  148. set_bit(i, phb->ioda.pe_alloc);
  149. pnv_ioda_init_pe(phb, i);
  150. }
  151. ret = &phb->ioda.pe_array[pe];
  152. out:
  153. mutex_unlock(&phb->ioda.pe_alloc_mutex);
  154. return ret;
  155. }
  156. void pnv_ioda_free_pe(struct pnv_ioda_pe *pe)
  157. {
  158. struct pnv_phb *phb = pe->phb;
  159. unsigned int pe_num = pe->pe_number;
  160. WARN_ON(pe->pdev);
  161. memset(pe, 0, sizeof(struct pnv_ioda_pe));
  162. mutex_lock(&phb->ioda.pe_alloc_mutex);
  163. clear_bit(pe_num, phb->ioda.pe_alloc);
  164. mutex_unlock(&phb->ioda.pe_alloc_mutex);
  165. }
  166. /* The default M64 BAR is shared by all PEs */
  167. static int pnv_ioda2_init_m64(struct pnv_phb *phb)
  168. {
  169. const char *desc;
  170. struct resource *r;
  171. s64 rc;
  172. /* Configure the default M64 BAR */
  173. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  174. OPAL_M64_WINDOW_TYPE,
  175. phb->ioda.m64_bar_idx,
  176. phb->ioda.m64_base,
  177. 0, /* unused */
  178. phb->ioda.m64_size);
  179. if (rc != OPAL_SUCCESS) {
  180. desc = "configuring";
  181. goto fail;
  182. }
  183. /* Enable the default M64 BAR */
  184. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  185. OPAL_M64_WINDOW_TYPE,
  186. phb->ioda.m64_bar_idx,
  187. OPAL_ENABLE_M64_SPLIT);
  188. if (rc != OPAL_SUCCESS) {
  189. desc = "enabling";
  190. goto fail;
  191. }
  192. /*
  193. * Exclude the segments for reserved and root bus PE, which
  194. * are first or last two PEs.
  195. */
  196. r = &phb->hose->mem_resources[1];
  197. if (phb->ioda.reserved_pe_idx == 0)
  198. r->start += (2 * phb->ioda.m64_segsize);
  199. else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
  200. r->end -= (2 * phb->ioda.m64_segsize);
  201. else
  202. pr_warn(" Cannot strip M64 segment for reserved PE#%x\n",
  203. phb->ioda.reserved_pe_idx);
  204. return 0;
  205. fail:
  206. pr_warn(" Failure %lld %s M64 BAR#%d\n",
  207. rc, desc, phb->ioda.m64_bar_idx);
  208. opal_pci_phb_mmio_enable(phb->opal_id,
  209. OPAL_M64_WINDOW_TYPE,
  210. phb->ioda.m64_bar_idx,
  211. OPAL_DISABLE_M64);
  212. return -EIO;
  213. }
  214. static void pnv_ioda_reserve_dev_m64_pe(struct pci_dev *pdev,
  215. unsigned long *pe_bitmap)
  216. {
  217. struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
  218. struct resource *r;
  219. resource_size_t base, sgsz, start, end;
  220. int segno, i;
  221. base = phb->ioda.m64_base;
  222. sgsz = phb->ioda.m64_segsize;
  223. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  224. r = &pdev->resource[i];
  225. if (!r->parent || !pnv_pci_is_m64(phb, r))
  226. continue;
  227. start = ALIGN_DOWN(r->start - base, sgsz);
  228. end = ALIGN(r->end - base, sgsz);
  229. for (segno = start / sgsz; segno < end / sgsz; segno++) {
  230. if (pe_bitmap)
  231. set_bit(segno, pe_bitmap);
  232. else
  233. pnv_ioda_reserve_pe(phb, segno);
  234. }
  235. }
  236. }
  237. static int pnv_ioda1_init_m64(struct pnv_phb *phb)
  238. {
  239. struct resource *r;
  240. int index;
  241. /*
  242. * There are 16 M64 BARs, each of which has 8 segments. So
  243. * there are as many M64 segments as the maximum number of
  244. * PEs, which is 128.
  245. */
  246. for (index = 0; index < PNV_IODA1_M64_NUM; index++) {
  247. unsigned long base, segsz = phb->ioda.m64_segsize;
  248. int64_t rc;
  249. base = phb->ioda.m64_base +
  250. index * PNV_IODA1_M64_SEGS * segsz;
  251. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  252. OPAL_M64_WINDOW_TYPE, index, base, 0,
  253. PNV_IODA1_M64_SEGS * segsz);
  254. if (rc != OPAL_SUCCESS) {
  255. pr_warn(" Error %lld setting M64 PHB#%x-BAR#%d\n",
  256. rc, phb->hose->global_number, index);
  257. goto fail;
  258. }
  259. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  260. OPAL_M64_WINDOW_TYPE, index,
  261. OPAL_ENABLE_M64_SPLIT);
  262. if (rc != OPAL_SUCCESS) {
  263. pr_warn(" Error %lld enabling M64 PHB#%x-BAR#%d\n",
  264. rc, phb->hose->global_number, index);
  265. goto fail;
  266. }
  267. }
  268. for (index = 0; index < phb->ioda.total_pe_num; index++) {
  269. int64_t rc;
  270. /*
  271. * P7IOC supports M64DT, which helps mapping M64 segment
  272. * to one particular PE#. However, PHB3 has fixed mapping
  273. * between M64 segment and PE#. In order to have same logic
  274. * for P7IOC and PHB3, we enforce fixed mapping between M64
  275. * segment and PE# on P7IOC.
  276. */
  277. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  278. index, OPAL_M64_WINDOW_TYPE,
  279. index / PNV_IODA1_M64_SEGS,
  280. index % PNV_IODA1_M64_SEGS);
  281. if (rc != OPAL_SUCCESS) {
  282. pr_warn("%s: Error %lld mapping M64 for PHB#%x-PE#%x\n",
  283. __func__, rc, phb->hose->global_number,
  284. index);
  285. goto fail;
  286. }
  287. }
  288. /*
  289. * Exclude the segments for reserved and root bus PE, which
  290. * are first or last two PEs.
  291. */
  292. r = &phb->hose->mem_resources[1];
  293. if (phb->ioda.reserved_pe_idx == 0)
  294. r->start += (2 * phb->ioda.m64_segsize);
  295. else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1))
  296. r->end -= (2 * phb->ioda.m64_segsize);
  297. else
  298. WARN(1, "Wrong reserved PE#%x on PHB#%x\n",
  299. phb->ioda.reserved_pe_idx, phb->hose->global_number);
  300. return 0;
  301. fail:
  302. for ( ; index >= 0; index--)
  303. opal_pci_phb_mmio_enable(phb->opal_id,
  304. OPAL_M64_WINDOW_TYPE, index, OPAL_DISABLE_M64);
  305. return -EIO;
  306. }
  307. static void pnv_ioda_reserve_m64_pe(struct pci_bus *bus,
  308. unsigned long *pe_bitmap,
  309. bool all)
  310. {
  311. struct pci_dev *pdev;
  312. list_for_each_entry(pdev, &bus->devices, bus_list) {
  313. pnv_ioda_reserve_dev_m64_pe(pdev, pe_bitmap);
  314. if (all && pdev->subordinate)
  315. pnv_ioda_reserve_m64_pe(pdev->subordinate,
  316. pe_bitmap, all);
  317. }
  318. }
  319. static struct pnv_ioda_pe *pnv_ioda_pick_m64_pe(struct pci_bus *bus, bool all)
  320. {
  321. struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
  322. struct pnv_ioda_pe *master_pe, *pe;
  323. unsigned long size, *pe_alloc;
  324. int i;
  325. /* Root bus shouldn't use M64 */
  326. if (pci_is_root_bus(bus))
  327. return NULL;
  328. /* Allocate bitmap */
  329. size = ALIGN(phb->ioda.total_pe_num / 8, sizeof(unsigned long));
  330. pe_alloc = kzalloc(size, GFP_KERNEL);
  331. if (!pe_alloc) {
  332. pr_warn("%s: Out of memory !\n",
  333. __func__);
  334. return NULL;
  335. }
  336. /* Figure out reserved PE numbers by the PE */
  337. pnv_ioda_reserve_m64_pe(bus, pe_alloc, all);
  338. /*
  339. * the current bus might not own M64 window and that's all
  340. * contributed by its child buses. For the case, we needn't
  341. * pick M64 dependent PE#.
  342. */
  343. if (bitmap_empty(pe_alloc, phb->ioda.total_pe_num)) {
  344. kfree(pe_alloc);
  345. return NULL;
  346. }
  347. /*
  348. * Figure out the master PE and put all slave PEs to master
  349. * PE's list to form compound PE.
  350. */
  351. master_pe = NULL;
  352. i = -1;
  353. while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe_num, i + 1)) <
  354. phb->ioda.total_pe_num) {
  355. pe = &phb->ioda.pe_array[i];
  356. phb->ioda.m64_segmap[pe->pe_number] = pe->pe_number;
  357. if (!master_pe) {
  358. pe->flags |= PNV_IODA_PE_MASTER;
  359. INIT_LIST_HEAD(&pe->slaves);
  360. master_pe = pe;
  361. } else {
  362. pe->flags |= PNV_IODA_PE_SLAVE;
  363. pe->master = master_pe;
  364. list_add_tail(&pe->list, &master_pe->slaves);
  365. }
  366. }
  367. kfree(pe_alloc);
  368. return master_pe;
  369. }
  370. static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
  371. {
  372. struct pci_controller *hose = phb->hose;
  373. struct device_node *dn = hose->dn;
  374. struct resource *res;
  375. u32 m64_range[2], i;
  376. const __be32 *r;
  377. u64 pci_addr;
  378. if (phb->type != PNV_PHB_IODA1 && phb->type != PNV_PHB_IODA2) {
  379. pr_info(" Not support M64 window\n");
  380. return;
  381. }
  382. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  383. pr_info(" Firmware too old to support M64 window\n");
  384. return;
  385. }
  386. r = of_get_property(dn, "ibm,opal-m64-window", NULL);
  387. if (!r) {
  388. pr_info(" No <ibm,opal-m64-window> on %pOF\n",
  389. dn);
  390. return;
  391. }
  392. /*
  393. * Find the available M64 BAR range and pickup the last one for
  394. * covering the whole 64-bits space. We support only one range.
  395. */
  396. if (of_property_read_u32_array(dn, "ibm,opal-available-m64-ranges",
  397. m64_range, 2)) {
  398. /* In absence of the property, assume 0..15 */
  399. m64_range[0] = 0;
  400. m64_range[1] = 16;
  401. }
  402. /* We only support 64 bits in our allocator */
  403. if (m64_range[1] > 63) {
  404. pr_warn("%s: Limiting M64 range to 63 (from %d) on PHB#%x\n",
  405. __func__, m64_range[1], phb->hose->global_number);
  406. m64_range[1] = 63;
  407. }
  408. /* Empty range, no m64 */
  409. if (m64_range[1] <= m64_range[0]) {
  410. pr_warn("%s: M64 empty, disabling M64 usage on PHB#%x\n",
  411. __func__, phb->hose->global_number);
  412. return;
  413. }
  414. /* Configure M64 informations */
  415. res = &hose->mem_resources[1];
  416. res->name = dn->full_name;
  417. res->start = of_translate_address(dn, r + 2);
  418. res->end = res->start + of_read_number(r + 4, 2) - 1;
  419. res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
  420. pci_addr = of_read_number(r, 2);
  421. hose->mem_offset[1] = res->start - pci_addr;
  422. phb->ioda.m64_size = resource_size(res);
  423. phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe_num;
  424. phb->ioda.m64_base = pci_addr;
  425. /* This lines up nicely with the display from processing OF ranges */
  426. pr_info(" MEM 0x%016llx..0x%016llx -> 0x%016llx (M64 #%d..%d)\n",
  427. res->start, res->end, pci_addr, m64_range[0],
  428. m64_range[0] + m64_range[1] - 1);
  429. /* Mark all M64 used up by default */
  430. phb->ioda.m64_bar_alloc = (unsigned long)-1;
  431. /* Use last M64 BAR to cover M64 window */
  432. m64_range[1]--;
  433. phb->ioda.m64_bar_idx = m64_range[0] + m64_range[1];
  434. pr_info(" Using M64 #%d as default window\n", phb->ioda.m64_bar_idx);
  435. /* Mark remaining ones free */
  436. for (i = m64_range[0]; i < m64_range[1]; i++)
  437. clear_bit(i, &phb->ioda.m64_bar_alloc);
  438. /*
  439. * Setup init functions for M64 based on IODA version, IODA3 uses
  440. * the IODA2 code.
  441. */
  442. if (phb->type == PNV_PHB_IODA1)
  443. phb->init_m64 = pnv_ioda1_init_m64;
  444. else
  445. phb->init_m64 = pnv_ioda2_init_m64;
  446. }
  447. static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
  448. {
  449. struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
  450. struct pnv_ioda_pe *slave;
  451. s64 rc;
  452. /* Fetch master PE */
  453. if (pe->flags & PNV_IODA_PE_SLAVE) {
  454. pe = pe->master;
  455. if (WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER)))
  456. return;
  457. pe_no = pe->pe_number;
  458. }
  459. /* Freeze master PE */
  460. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  461. pe_no,
  462. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  463. if (rc != OPAL_SUCCESS) {
  464. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  465. __func__, rc, phb->hose->global_number, pe_no);
  466. return;
  467. }
  468. /* Freeze slave PEs */
  469. if (!(pe->flags & PNV_IODA_PE_MASTER))
  470. return;
  471. list_for_each_entry(slave, &pe->slaves, list) {
  472. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  473. slave->pe_number,
  474. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  475. if (rc != OPAL_SUCCESS)
  476. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  477. __func__, rc, phb->hose->global_number,
  478. slave->pe_number);
  479. }
  480. }
  481. static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
  482. {
  483. struct pnv_ioda_pe *pe, *slave;
  484. s64 rc;
  485. /* Find master PE */
  486. pe = &phb->ioda.pe_array[pe_no];
  487. if (pe->flags & PNV_IODA_PE_SLAVE) {
  488. pe = pe->master;
  489. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  490. pe_no = pe->pe_number;
  491. }
  492. /* Clear frozen state for master PE */
  493. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
  494. if (rc != OPAL_SUCCESS) {
  495. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  496. __func__, rc, opt, phb->hose->global_number, pe_no);
  497. return -EIO;
  498. }
  499. if (!(pe->flags & PNV_IODA_PE_MASTER))
  500. return 0;
  501. /* Clear frozen state for slave PEs */
  502. list_for_each_entry(slave, &pe->slaves, list) {
  503. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  504. slave->pe_number,
  505. opt);
  506. if (rc != OPAL_SUCCESS) {
  507. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  508. __func__, rc, opt, phb->hose->global_number,
  509. slave->pe_number);
  510. return -EIO;
  511. }
  512. }
  513. return 0;
  514. }
  515. static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
  516. {
  517. struct pnv_ioda_pe *slave, *pe;
  518. u8 fstate = 0, state;
  519. __be16 pcierr = 0;
  520. s64 rc;
  521. /* Sanity check on PE number */
  522. if (pe_no < 0 || pe_no >= phb->ioda.total_pe_num)
  523. return OPAL_EEH_STOPPED_PERM_UNAVAIL;
  524. /*
  525. * Fetch the master PE and the PE instance might be
  526. * not initialized yet.
  527. */
  528. pe = &phb->ioda.pe_array[pe_no];
  529. if (pe->flags & PNV_IODA_PE_SLAVE) {
  530. pe = pe->master;
  531. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  532. pe_no = pe->pe_number;
  533. }
  534. /* Check the master PE */
  535. rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
  536. &state, &pcierr, NULL);
  537. if (rc != OPAL_SUCCESS) {
  538. pr_warn("%s: Failure %lld getting "
  539. "PHB#%x-PE#%x state\n",
  540. __func__, rc,
  541. phb->hose->global_number, pe_no);
  542. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  543. }
  544. /* Check the slave PE */
  545. if (!(pe->flags & PNV_IODA_PE_MASTER))
  546. return state;
  547. list_for_each_entry(slave, &pe->slaves, list) {
  548. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  549. slave->pe_number,
  550. &fstate,
  551. &pcierr,
  552. NULL);
  553. if (rc != OPAL_SUCCESS) {
  554. pr_warn("%s: Failure %lld getting "
  555. "PHB#%x-PE#%x state\n",
  556. __func__, rc,
  557. phb->hose->global_number, slave->pe_number);
  558. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  559. }
  560. /*
  561. * Override the result based on the ascending
  562. * priority.
  563. */
  564. if (fstate > state)
  565. state = fstate;
  566. }
  567. return state;
  568. }
  569. struct pnv_ioda_pe *pnv_pci_bdfn_to_pe(struct pnv_phb *phb, u16 bdfn)
  570. {
  571. int pe_number = phb->ioda.pe_rmap[bdfn];
  572. if (pe_number == IODA_INVALID_PE)
  573. return NULL;
  574. return &phb->ioda.pe_array[pe_number];
  575. }
  576. struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
  577. {
  578. struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
  579. struct pci_dn *pdn = pci_get_pdn(dev);
  580. if (!pdn)
  581. return NULL;
  582. if (pdn->pe_number == IODA_INVALID_PE)
  583. return NULL;
  584. return &phb->ioda.pe_array[pdn->pe_number];
  585. }
  586. static int pnv_ioda_set_one_peltv(struct pnv_phb *phb,
  587. struct pnv_ioda_pe *parent,
  588. struct pnv_ioda_pe *child,
  589. bool is_add)
  590. {
  591. const char *desc = is_add ? "adding" : "removing";
  592. uint8_t op = is_add ? OPAL_ADD_PE_TO_DOMAIN :
  593. OPAL_REMOVE_PE_FROM_DOMAIN;
  594. struct pnv_ioda_pe *slave;
  595. long rc;
  596. /* Parent PE affects child PE */
  597. rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
  598. child->pe_number, op);
  599. if (rc != OPAL_SUCCESS) {
  600. pe_warn(child, "OPAL error %ld %s to parent PELTV\n",
  601. rc, desc);
  602. return -ENXIO;
  603. }
  604. if (!(child->flags & PNV_IODA_PE_MASTER))
  605. return 0;
  606. /* Compound case: parent PE affects slave PEs */
  607. list_for_each_entry(slave, &child->slaves, list) {
  608. rc = opal_pci_set_peltv(phb->opal_id, parent->pe_number,
  609. slave->pe_number, op);
  610. if (rc != OPAL_SUCCESS) {
  611. pe_warn(slave, "OPAL error %ld %s to parent PELTV\n",
  612. rc, desc);
  613. return -ENXIO;
  614. }
  615. }
  616. return 0;
  617. }
  618. static int pnv_ioda_set_peltv(struct pnv_phb *phb,
  619. struct pnv_ioda_pe *pe,
  620. bool is_add)
  621. {
  622. struct pnv_ioda_pe *slave;
  623. struct pci_dev *pdev = NULL;
  624. int ret;
  625. /*
  626. * Clear PE frozen state. If it's master PE, we need
  627. * clear slave PE frozen state as well.
  628. */
  629. if (is_add) {
  630. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  631. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  632. if (pe->flags & PNV_IODA_PE_MASTER) {
  633. list_for_each_entry(slave, &pe->slaves, list)
  634. opal_pci_eeh_freeze_clear(phb->opal_id,
  635. slave->pe_number,
  636. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  637. }
  638. }
  639. /*
  640. * Associate PE in PELT. We need add the PE into the
  641. * corresponding PELT-V as well. Otherwise, the error
  642. * originated from the PE might contribute to other
  643. * PEs.
  644. */
  645. ret = pnv_ioda_set_one_peltv(phb, pe, pe, is_add);
  646. if (ret)
  647. return ret;
  648. /* For compound PEs, any one affects all of them */
  649. if (pe->flags & PNV_IODA_PE_MASTER) {
  650. list_for_each_entry(slave, &pe->slaves, list) {
  651. ret = pnv_ioda_set_one_peltv(phb, slave, pe, is_add);
  652. if (ret)
  653. return ret;
  654. }
  655. }
  656. if (pe->flags & (PNV_IODA_PE_BUS_ALL | PNV_IODA_PE_BUS))
  657. pdev = pe->pbus->self;
  658. else if (pe->flags & PNV_IODA_PE_DEV)
  659. pdev = pe->pdev->bus->self;
  660. #ifdef CONFIG_PCI_IOV
  661. else if (pe->flags & PNV_IODA_PE_VF)
  662. pdev = pe->parent_dev;
  663. #endif /* CONFIG_PCI_IOV */
  664. while (pdev) {
  665. struct pci_dn *pdn = pci_get_pdn(pdev);
  666. struct pnv_ioda_pe *parent;
  667. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  668. parent = &phb->ioda.pe_array[pdn->pe_number];
  669. ret = pnv_ioda_set_one_peltv(phb, parent, pe, is_add);
  670. if (ret)
  671. return ret;
  672. }
  673. pdev = pdev->bus->self;
  674. }
  675. return 0;
  676. }
  677. static void pnv_ioda_unset_peltv(struct pnv_phb *phb,
  678. struct pnv_ioda_pe *pe,
  679. struct pci_dev *parent)
  680. {
  681. int64_t rc;
  682. while (parent) {
  683. struct pci_dn *pdn = pci_get_pdn(parent);
  684. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  685. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  686. pe->pe_number,
  687. OPAL_REMOVE_PE_FROM_DOMAIN);
  688. /* XXX What to do in case of error ? */
  689. }
  690. parent = parent->bus->self;
  691. }
  692. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  693. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  694. /* Disassociate PE in PELT */
  695. rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
  696. pe->pe_number, OPAL_REMOVE_PE_FROM_DOMAIN);
  697. if (rc)
  698. pe_warn(pe, "OPAL error %lld remove self from PELTV\n", rc);
  699. }
  700. int pnv_ioda_deconfigure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  701. {
  702. struct pci_dev *parent;
  703. uint8_t bcomp, dcomp, fcomp;
  704. int64_t rc;
  705. long rid_end, rid;
  706. /* Currently, we just deconfigure VF PE. Bus PE will always there.*/
  707. if (pe->pbus) {
  708. int count;
  709. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  710. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  711. parent = pe->pbus->self;
  712. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  713. count = resource_size(&pe->pbus->busn_res);
  714. else
  715. count = 1;
  716. switch(count) {
  717. case 1: bcomp = OpalPciBusAll; break;
  718. case 2: bcomp = OpalPciBus7Bits; break;
  719. case 4: bcomp = OpalPciBus6Bits; break;
  720. case 8: bcomp = OpalPciBus5Bits; break;
  721. case 16: bcomp = OpalPciBus4Bits; break;
  722. case 32: bcomp = OpalPciBus3Bits; break;
  723. default:
  724. dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
  725. count);
  726. /* Do an exact match only */
  727. bcomp = OpalPciBusAll;
  728. }
  729. rid_end = pe->rid + (count << 8);
  730. } else {
  731. #ifdef CONFIG_PCI_IOV
  732. if (pe->flags & PNV_IODA_PE_VF)
  733. parent = pe->parent_dev;
  734. else
  735. #endif
  736. parent = pe->pdev->bus->self;
  737. bcomp = OpalPciBusAll;
  738. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  739. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  740. rid_end = pe->rid + 1;
  741. }
  742. /* Clear the reverse map */
  743. for (rid = pe->rid; rid < rid_end; rid++)
  744. phb->ioda.pe_rmap[rid] = IODA_INVALID_PE;
  745. /*
  746. * Release from all parents PELT-V. NPUs don't have a PELTV
  747. * table
  748. */
  749. if (phb->type != PNV_PHB_NPU_OCAPI)
  750. pnv_ioda_unset_peltv(phb, pe, parent);
  751. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  752. bcomp, dcomp, fcomp, OPAL_UNMAP_PE);
  753. if (rc)
  754. pe_err(pe, "OPAL error %lld trying to setup PELT table\n", rc);
  755. pe->pbus = NULL;
  756. pe->pdev = NULL;
  757. #ifdef CONFIG_PCI_IOV
  758. pe->parent_dev = NULL;
  759. #endif
  760. return 0;
  761. }
  762. int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  763. {
  764. uint8_t bcomp, dcomp, fcomp;
  765. long rc, rid_end, rid;
  766. /* Bus validation ? */
  767. if (pe->pbus) {
  768. int count;
  769. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  770. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  771. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  772. count = resource_size(&pe->pbus->busn_res);
  773. else
  774. count = 1;
  775. switch(count) {
  776. case 1: bcomp = OpalPciBusAll; break;
  777. case 2: bcomp = OpalPciBus7Bits; break;
  778. case 4: bcomp = OpalPciBus6Bits; break;
  779. case 8: bcomp = OpalPciBus5Bits; break;
  780. case 16: bcomp = OpalPciBus4Bits; break;
  781. case 32: bcomp = OpalPciBus3Bits; break;
  782. default:
  783. dev_err(&pe->pbus->dev, "Number of subordinate buses %d unsupported\n",
  784. count);
  785. /* Do an exact match only */
  786. bcomp = OpalPciBusAll;
  787. }
  788. rid_end = pe->rid + (count << 8);
  789. } else {
  790. bcomp = OpalPciBusAll;
  791. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  792. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  793. rid_end = pe->rid + 1;
  794. }
  795. /*
  796. * Associate PE in PELT. We need add the PE into the
  797. * corresponding PELT-V as well. Otherwise, the error
  798. * originated from the PE might contribute to other
  799. * PEs.
  800. */
  801. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  802. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  803. if (rc) {
  804. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  805. return -ENXIO;
  806. }
  807. /*
  808. * Configure PELTV. NPUs don't have a PELTV table so skip
  809. * configuration on them.
  810. */
  811. if (phb->type != PNV_PHB_NPU_OCAPI)
  812. pnv_ioda_set_peltv(phb, pe, true);
  813. /* Setup reverse map */
  814. for (rid = pe->rid; rid < rid_end; rid++)
  815. phb->ioda.pe_rmap[rid] = pe->pe_number;
  816. /* Setup one MVTs on IODA1 */
  817. if (phb->type != PNV_PHB_IODA1) {
  818. pe->mve_number = 0;
  819. goto out;
  820. }
  821. pe->mve_number = pe->pe_number;
  822. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number, pe->pe_number);
  823. if (rc != OPAL_SUCCESS) {
  824. pe_err(pe, "OPAL error %ld setting up MVE %x\n",
  825. rc, pe->mve_number);
  826. pe->mve_number = -1;
  827. } else {
  828. rc = opal_pci_set_mve_enable(phb->opal_id,
  829. pe->mve_number, OPAL_ENABLE_MVE);
  830. if (rc) {
  831. pe_err(pe, "OPAL error %ld enabling MVE %x\n",
  832. rc, pe->mve_number);
  833. pe->mve_number = -1;
  834. }
  835. }
  836. out:
  837. return 0;
  838. }
  839. static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  840. {
  841. struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
  842. struct pci_dn *pdn = pci_get_pdn(dev);
  843. struct pnv_ioda_pe *pe;
  844. if (!pdn) {
  845. pr_err("%s: Device tree node not associated properly\n",
  846. pci_name(dev));
  847. return NULL;
  848. }
  849. if (pdn->pe_number != IODA_INVALID_PE)
  850. return NULL;
  851. pe = pnv_ioda_alloc_pe(phb, 1);
  852. if (!pe) {
  853. pr_warn("%s: Not enough PE# available, disabling device\n",
  854. pci_name(dev));
  855. return NULL;
  856. }
  857. /* NOTE: We don't get a reference for the pointer in the PE
  858. * data structure, both the device and PE structures should be
  859. * destroyed at the same time.
  860. *
  861. * At some point we want to remove the PDN completely anyways
  862. */
  863. pdn->pe_number = pe->pe_number;
  864. pe->flags = PNV_IODA_PE_DEV;
  865. pe->pdev = dev;
  866. pe->pbus = NULL;
  867. pe->mve_number = -1;
  868. pe->rid = dev->bus->number << 8 | pdn->devfn;
  869. pe->device_count++;
  870. pe_info(pe, "Associated device to PE\n");
  871. if (pnv_ioda_configure_pe(phb, pe)) {
  872. /* XXX What do we do here ? */
  873. pnv_ioda_free_pe(pe);
  874. pdn->pe_number = IODA_INVALID_PE;
  875. pe->pdev = NULL;
  876. return NULL;
  877. }
  878. /* Put PE to the list */
  879. mutex_lock(&phb->ioda.pe_list_mutex);
  880. list_add_tail(&pe->list, &phb->ioda.pe_list);
  881. mutex_unlock(&phb->ioda.pe_list_mutex);
  882. return pe;
  883. }
  884. /*
  885. * There're 2 types of PCI bus sensitive PEs: One that is compromised of
  886. * single PCI bus. Another one that contains the primary PCI bus and its
  887. * subordinate PCI devices and buses. The second type of PE is normally
  888. * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
  889. */
  890. static struct pnv_ioda_pe *pnv_ioda_setup_bus_PE(struct pci_bus *bus, bool all)
  891. {
  892. struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
  893. struct pnv_ioda_pe *pe = NULL;
  894. unsigned int pe_num;
  895. /*
  896. * In partial hotplug case, the PE instance might be still alive.
  897. * We should reuse it instead of allocating a new one.
  898. */
  899. pe_num = phb->ioda.pe_rmap[bus->number << 8];
  900. if (WARN_ON(pe_num != IODA_INVALID_PE)) {
  901. pe = &phb->ioda.pe_array[pe_num];
  902. return NULL;
  903. }
  904. /* PE number for root bus should have been reserved */
  905. if (pci_is_root_bus(bus))
  906. pe = &phb->ioda.pe_array[phb->ioda.root_pe_idx];
  907. /* Check if PE is determined by M64 */
  908. if (!pe)
  909. pe = pnv_ioda_pick_m64_pe(bus, all);
  910. /* The PE number isn't pinned by M64 */
  911. if (!pe)
  912. pe = pnv_ioda_alloc_pe(phb, 1);
  913. if (!pe) {
  914. pr_warn("%s: Not enough PE# available for PCI bus %04x:%02x\n",
  915. __func__, pci_domain_nr(bus), bus->number);
  916. return NULL;
  917. }
  918. pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
  919. pe->pbus = bus;
  920. pe->pdev = NULL;
  921. pe->mve_number = -1;
  922. pe->rid = bus->busn_res.start << 8;
  923. if (all)
  924. pe_info(pe, "Secondary bus %pad..%pad associated with PE#%x\n",
  925. &bus->busn_res.start, &bus->busn_res.end,
  926. pe->pe_number);
  927. else
  928. pe_info(pe, "Secondary bus %pad associated with PE#%x\n",
  929. &bus->busn_res.start, pe->pe_number);
  930. if (pnv_ioda_configure_pe(phb, pe)) {
  931. /* XXX What do we do here ? */
  932. pnv_ioda_free_pe(pe);
  933. pe->pbus = NULL;
  934. return NULL;
  935. }
  936. /* Put PE to the list */
  937. list_add_tail(&pe->list, &phb->ioda.pe_list);
  938. return pe;
  939. }
  940. static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
  941. struct pnv_ioda_pe *pe);
  942. static void pnv_pci_ioda_dma_dev_setup(struct pci_dev *pdev)
  943. {
  944. struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
  945. struct pci_dn *pdn = pci_get_pdn(pdev);
  946. struct pnv_ioda_pe *pe;
  947. /* Check if the BDFN for this device is associated with a PE yet */
  948. pe = pnv_pci_bdfn_to_pe(phb, pdev->devfn | (pdev->bus->number << 8));
  949. if (!pe) {
  950. /* VF PEs should be pre-configured in pnv_pci_sriov_enable() */
  951. if (WARN_ON(pdev->is_virtfn))
  952. return;
  953. pnv_pci_configure_bus(pdev->bus);
  954. pe = pnv_pci_bdfn_to_pe(phb, pdev->devfn | (pdev->bus->number << 8));
  955. pci_info(pdev, "Configured PE#%x\n", pe ? pe->pe_number : 0xfffff);
  956. /*
  957. * If we can't setup the IODA PE something has gone horribly
  958. * wrong and we can't enable DMA for the device.
  959. */
  960. if (WARN_ON(!pe))
  961. return;
  962. } else {
  963. pci_info(pdev, "Added to existing PE#%x\n", pe->pe_number);
  964. }
  965. /*
  966. * We assume that bridges *probably* don't need to do any DMA so we can
  967. * skip allocating a TCE table, etc unless we get a non-bridge device.
  968. */
  969. if (!pe->dma_setup_done && !pci_is_bridge(pdev)) {
  970. switch (phb->type) {
  971. case PNV_PHB_IODA1:
  972. pnv_pci_ioda1_setup_dma_pe(phb, pe);
  973. break;
  974. case PNV_PHB_IODA2:
  975. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  976. break;
  977. default:
  978. pr_warn("%s: No DMA for PHB#%x (type %d)\n",
  979. __func__, phb->hose->global_number, phb->type);
  980. }
  981. }
  982. if (pdn)
  983. pdn->pe_number = pe->pe_number;
  984. pe->device_count++;
  985. WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
  986. pdev->dev.archdata.dma_offset = pe->tce_bypass_base;
  987. set_iommu_table_base(&pdev->dev, pe->table_group.tables[0]);
  988. /* PEs with a DMA weight of zero won't have a group */
  989. if (pe->table_group.group)
  990. iommu_add_device(&pe->table_group, &pdev->dev);
  991. }
  992. /*
  993. * Reconfigure TVE#0 to be usable as 64-bit DMA space.
  994. *
  995. * The first 4GB of virtual memory for a PE is reserved for 32-bit accesses.
  996. * Devices can only access more than that if bit 59 of the PCI address is set
  997. * by hardware, which indicates TVE#1 should be used instead of TVE#0.
  998. * Many PCI devices are not capable of addressing that many bits, and as a
  999. * result are limited to the 4GB of virtual memory made available to 32-bit
  1000. * devices in TVE#0.
  1001. *
  1002. * In order to work around this, reconfigure TVE#0 to be suitable for 64-bit
  1003. * devices by configuring the virtual memory past the first 4GB inaccessible
  1004. * by 64-bit DMAs. This should only be used by devices that want more than
  1005. * 4GB, and only on PEs that have no 32-bit devices.
  1006. *
  1007. * Currently this will only work on PHB3 (POWER8).
  1008. */
  1009. static int pnv_pci_ioda_dma_64bit_bypass(struct pnv_ioda_pe *pe)
  1010. {
  1011. u64 window_size, table_size, tce_count, addr;
  1012. struct page *table_pages;
  1013. u64 tce_order = 28; /* 256MB TCEs */
  1014. __be64 *tces;
  1015. s64 rc;
  1016. /*
  1017. * Window size needs to be a power of two, but needs to account for
  1018. * shifting memory by the 4GB offset required to skip 32bit space.
  1019. */
  1020. window_size = roundup_pow_of_two(memory_hotplug_max() + (1ULL << 32));
  1021. tce_count = window_size >> tce_order;
  1022. table_size = tce_count << 3;
  1023. if (table_size < PAGE_SIZE)
  1024. table_size = PAGE_SIZE;
  1025. table_pages = alloc_pages_node(pe->phb->hose->node, GFP_KERNEL,
  1026. get_order(table_size));
  1027. if (!table_pages)
  1028. goto err;
  1029. tces = page_address(table_pages);
  1030. if (!tces)
  1031. goto err;
  1032. memset(tces, 0, table_size);
  1033. for (addr = 0; addr < memory_hotplug_max(); addr += (1 << tce_order)) {
  1034. tces[(addr + (1ULL << 32)) >> tce_order] =
  1035. cpu_to_be64(addr | TCE_PCI_READ | TCE_PCI_WRITE);
  1036. }
  1037. rc = opal_pci_map_pe_dma_window(pe->phb->opal_id,
  1038. pe->pe_number,
  1039. /* reconfigure window 0 */
  1040. (pe->pe_number << 1) + 0,
  1041. 1,
  1042. __pa(tces),
  1043. table_size,
  1044. 1 << tce_order);
  1045. if (rc == OPAL_SUCCESS) {
  1046. pe_info(pe, "Using 64-bit DMA iommu bypass (through TVE#0)\n");
  1047. return 0;
  1048. }
  1049. err:
  1050. pe_err(pe, "Error configuring 64-bit DMA bypass\n");
  1051. return -EIO;
  1052. }
  1053. static bool pnv_pci_ioda_iommu_bypass_supported(struct pci_dev *pdev,
  1054. u64 dma_mask)
  1055. {
  1056. struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
  1057. struct pci_dn *pdn = pci_get_pdn(pdev);
  1058. struct pnv_ioda_pe *pe;
  1059. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  1060. return false;
  1061. pe = &phb->ioda.pe_array[pdn->pe_number];
  1062. if (pe->tce_bypass_enabled) {
  1063. u64 top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
  1064. if (dma_mask >= top)
  1065. return true;
  1066. }
  1067. /*
  1068. * If the device can't set the TCE bypass bit but still wants
  1069. * to access 4GB or more, on PHB3 we can reconfigure TVE#0 to
  1070. * bypass the 32-bit region and be usable for 64-bit DMAs.
  1071. * The device needs to be able to address all of this space.
  1072. */
  1073. if (dma_mask >> 32 &&
  1074. dma_mask > (memory_hotplug_max() + (1ULL << 32)) &&
  1075. /* pe->pdev should be set if it's a single device, pe->pbus if not */
  1076. (pe->device_count == 1 || !pe->pbus) &&
  1077. phb->model == PNV_PHB_MODEL_PHB3) {
  1078. /* Configure the bypass mode */
  1079. s64 rc = pnv_pci_ioda_dma_64bit_bypass(pe);
  1080. if (rc)
  1081. return false;
  1082. /* 4GB offset bypasses 32-bit space */
  1083. pdev->dev.archdata.dma_offset = (1ULL << 32);
  1084. return true;
  1085. }
  1086. return false;
  1087. }
  1088. static inline __be64 __iomem *pnv_ioda_get_inval_reg(struct pnv_phb *phb)
  1089. {
  1090. return phb->regs + 0x210;
  1091. }
  1092. static void pnv_pci_p7ioc_tce_invalidate(struct iommu_table *tbl,
  1093. unsigned long index, unsigned long npages)
  1094. {
  1095. struct iommu_table_group_link *tgl = list_first_entry_or_null(
  1096. &tbl->it_group_list, struct iommu_table_group_link,
  1097. next);
  1098. struct pnv_ioda_pe *pe = container_of(tgl->table_group,
  1099. struct pnv_ioda_pe, table_group);
  1100. __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);
  1101. unsigned long start, end, inc;
  1102. start = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset);
  1103. end = __pa(((__be64 *)tbl->it_base) + index - tbl->it_offset +
  1104. npages - 1);
  1105. /* p7ioc-style invalidation, 2 TCEs per write */
  1106. start |= (1ull << 63);
  1107. end |= (1ull << 63);
  1108. inc = 16;
  1109. end |= inc - 1; /* round up end to be different than start */
  1110. mb(); /* Ensure above stores are visible */
  1111. while (start <= end) {
  1112. __raw_writeq_be(start, invalidate);
  1113. start += inc;
  1114. }
  1115. /*
  1116. * The iommu layer will do another mb() for us on build()
  1117. * and we don't care on free()
  1118. */
  1119. }
  1120. static int pnv_ioda1_tce_build(struct iommu_table *tbl, long index,
  1121. long npages, unsigned long uaddr,
  1122. enum dma_data_direction direction,
  1123. unsigned long attrs)
  1124. {
  1125. int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
  1126. attrs);
  1127. if (!ret)
  1128. pnv_pci_p7ioc_tce_invalidate(tbl, index, npages);
  1129. return ret;
  1130. }
  1131. #ifdef CONFIG_IOMMU_API
  1132. /* Common for IODA1 and IODA2 */
  1133. static int pnv_ioda_tce_xchg_no_kill(struct iommu_table *tbl, long index,
  1134. unsigned long *hpa, enum dma_data_direction *direction)
  1135. {
  1136. return pnv_tce_xchg(tbl, index, hpa, direction);
  1137. }
  1138. #endif
  1139. static void pnv_ioda1_tce_free(struct iommu_table *tbl, long index,
  1140. long npages)
  1141. {
  1142. pnv_tce_free(tbl, index, npages);
  1143. pnv_pci_p7ioc_tce_invalidate(tbl, index, npages);
  1144. }
  1145. static struct iommu_table_ops pnv_ioda1_iommu_ops = {
  1146. .set = pnv_ioda1_tce_build,
  1147. #ifdef CONFIG_IOMMU_API
  1148. .xchg_no_kill = pnv_ioda_tce_xchg_no_kill,
  1149. .tce_kill = pnv_pci_p7ioc_tce_invalidate,
  1150. .useraddrptr = pnv_tce_useraddrptr,
  1151. #endif
  1152. .clear = pnv_ioda1_tce_free,
  1153. .get = pnv_tce_get,
  1154. };
  1155. #define PHB3_TCE_KILL_INVAL_ALL PPC_BIT(0)
  1156. #define PHB3_TCE_KILL_INVAL_PE PPC_BIT(1)
  1157. #define PHB3_TCE_KILL_INVAL_ONE PPC_BIT(2)
  1158. static inline void pnv_pci_phb3_tce_invalidate_pe(struct pnv_ioda_pe *pe)
  1159. {
  1160. /* 01xb - invalidate TCEs that match the specified PE# */
  1161. __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);
  1162. unsigned long val = PHB3_TCE_KILL_INVAL_PE | (pe->pe_number & 0xFF);
  1163. mb(); /* Ensure above stores are visible */
  1164. __raw_writeq_be(val, invalidate);
  1165. }
  1166. static void pnv_pci_phb3_tce_invalidate(struct pnv_ioda_pe *pe,
  1167. unsigned shift, unsigned long index,
  1168. unsigned long npages)
  1169. {
  1170. __be64 __iomem *invalidate = pnv_ioda_get_inval_reg(pe->phb);
  1171. unsigned long start, end, inc;
  1172. /* We'll invalidate DMA address in PE scope */
  1173. start = PHB3_TCE_KILL_INVAL_ONE;
  1174. start |= (pe->pe_number & 0xFF);
  1175. end = start;
  1176. /* Figure out the start, end and step */
  1177. start |= (index << shift);
  1178. end |= ((index + npages - 1) << shift);
  1179. inc = (0x1ull << shift);
  1180. mb();
  1181. while (start <= end) {
  1182. __raw_writeq_be(start, invalidate);
  1183. start += inc;
  1184. }
  1185. }
  1186. static inline void pnv_pci_ioda2_tce_invalidate_pe(struct pnv_ioda_pe *pe)
  1187. {
  1188. struct pnv_phb *phb = pe->phb;
  1189. if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
  1190. pnv_pci_phb3_tce_invalidate_pe(pe);
  1191. else
  1192. opal_pci_tce_kill(phb->opal_id, OPAL_PCI_TCE_KILL_PE,
  1193. pe->pe_number, 0, 0, 0);
  1194. }
  1195. static void pnv_pci_ioda2_tce_invalidate(struct iommu_table *tbl,
  1196. unsigned long index, unsigned long npages)
  1197. {
  1198. struct iommu_table_group_link *tgl;
  1199. list_for_each_entry_lockless(tgl, &tbl->it_group_list, next) {
  1200. struct pnv_ioda_pe *pe = container_of(tgl->table_group,
  1201. struct pnv_ioda_pe, table_group);
  1202. struct pnv_phb *phb = pe->phb;
  1203. unsigned int shift = tbl->it_page_shift;
  1204. if (phb->model == PNV_PHB_MODEL_PHB3 && phb->regs)
  1205. pnv_pci_phb3_tce_invalidate(pe, shift,
  1206. index, npages);
  1207. else
  1208. opal_pci_tce_kill(phb->opal_id,
  1209. OPAL_PCI_TCE_KILL_PAGES,
  1210. pe->pe_number, 1u << shift,
  1211. index << shift, npages);
  1212. }
  1213. }
  1214. static int pnv_ioda2_tce_build(struct iommu_table *tbl, long index,
  1215. long npages, unsigned long uaddr,
  1216. enum dma_data_direction direction,
  1217. unsigned long attrs)
  1218. {
  1219. int ret = pnv_tce_build(tbl, index, npages, uaddr, direction,
  1220. attrs);
  1221. if (!ret)
  1222. pnv_pci_ioda2_tce_invalidate(tbl, index, npages);
  1223. return ret;
  1224. }
  1225. static void pnv_ioda2_tce_free(struct iommu_table *tbl, long index,
  1226. long npages)
  1227. {
  1228. pnv_tce_free(tbl, index, npages);
  1229. pnv_pci_ioda2_tce_invalidate(tbl, index, npages);
  1230. }
  1231. static struct iommu_table_ops pnv_ioda2_iommu_ops = {
  1232. .set = pnv_ioda2_tce_build,
  1233. #ifdef CONFIG_IOMMU_API
  1234. .xchg_no_kill = pnv_ioda_tce_xchg_no_kill,
  1235. .tce_kill = pnv_pci_ioda2_tce_invalidate,
  1236. .useraddrptr = pnv_tce_useraddrptr,
  1237. #endif
  1238. .clear = pnv_ioda2_tce_free,
  1239. .get = pnv_tce_get,
  1240. .free = pnv_pci_ioda2_table_free_pages,
  1241. };
  1242. static int pnv_pci_ioda_dev_dma_weight(struct pci_dev *dev, void *data)
  1243. {
  1244. unsigned int *weight = (unsigned int *)data;
  1245. /* This is quite simplistic. The "base" weight of a device
  1246. * is 10. 0 means no DMA is to be accounted for it.
  1247. */
  1248. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  1249. return 0;
  1250. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  1251. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  1252. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  1253. *weight += 3;
  1254. else if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  1255. *weight += 15;
  1256. else
  1257. *weight += 10;
  1258. return 0;
  1259. }
  1260. static unsigned int pnv_pci_ioda_pe_dma_weight(struct pnv_ioda_pe *pe)
  1261. {
  1262. unsigned int weight = 0;
  1263. /* SRIOV VF has same DMA32 weight as its PF */
  1264. #ifdef CONFIG_PCI_IOV
  1265. if ((pe->flags & PNV_IODA_PE_VF) && pe->parent_dev) {
  1266. pnv_pci_ioda_dev_dma_weight(pe->parent_dev, &weight);
  1267. return weight;
  1268. }
  1269. #endif
  1270. if ((pe->flags & PNV_IODA_PE_DEV) && pe->pdev) {
  1271. pnv_pci_ioda_dev_dma_weight(pe->pdev, &weight);
  1272. } else if ((pe->flags & PNV_IODA_PE_BUS) && pe->pbus) {
  1273. struct pci_dev *pdev;
  1274. list_for_each_entry(pdev, &pe->pbus->devices, bus_list)
  1275. pnv_pci_ioda_dev_dma_weight(pdev, &weight);
  1276. } else if ((pe->flags & PNV_IODA_PE_BUS_ALL) && pe->pbus) {
  1277. pci_walk_bus(pe->pbus, pnv_pci_ioda_dev_dma_weight, &weight);
  1278. }
  1279. return weight;
  1280. }
  1281. static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
  1282. struct pnv_ioda_pe *pe)
  1283. {
  1284. struct page *tce_mem = NULL;
  1285. struct iommu_table *tbl;
  1286. unsigned int weight, total_weight = 0;
  1287. unsigned int tce32_segsz, base, segs, avail, i;
  1288. int64_t rc;
  1289. void *addr;
  1290. /* XXX FIXME: Handle 64-bit only DMA devices */
  1291. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  1292. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  1293. weight = pnv_pci_ioda_pe_dma_weight(pe);
  1294. if (!weight)
  1295. return;
  1296. pci_walk_bus(phb->hose->bus, pnv_pci_ioda_dev_dma_weight,
  1297. &total_weight);
  1298. segs = (weight * phb->ioda.dma32_count) / total_weight;
  1299. if (!segs)
  1300. segs = 1;
  1301. /*
  1302. * Allocate contiguous DMA32 segments. We begin with the expected
  1303. * number of segments. With one more attempt, the number of DMA32
  1304. * segments to be allocated is decreased by one until one segment
  1305. * is allocated successfully.
  1306. */
  1307. do {
  1308. for (base = 0; base <= phb->ioda.dma32_count - segs; base++) {
  1309. for (avail = 0, i = base; i < base + segs; i++) {
  1310. if (phb->ioda.dma32_segmap[i] ==
  1311. IODA_INVALID_PE)
  1312. avail++;
  1313. }
  1314. if (avail == segs)
  1315. goto found;
  1316. }
  1317. } while (--segs);
  1318. if (!segs) {
  1319. pe_warn(pe, "No available DMA32 segments\n");
  1320. return;
  1321. }
  1322. found:
  1323. tbl = pnv_pci_table_alloc(phb->hose->node);
  1324. if (WARN_ON(!tbl))
  1325. return;
  1326. iommu_register_group(&pe->table_group, phb->hose->global_number,
  1327. pe->pe_number);
  1328. pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
  1329. /* Grab a 32-bit TCE table */
  1330. pe_info(pe, "DMA weight %d (%d), assigned (%d) %d DMA32 segments\n",
  1331. weight, total_weight, base, segs);
  1332. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  1333. base * PNV_IODA1_DMA32_SEGSIZE,
  1334. (base + segs) * PNV_IODA1_DMA32_SEGSIZE - 1);
  1335. /* XXX Currently, we allocate one big contiguous table for the
  1336. * TCEs. We only really need one chunk per 256M of TCE space
  1337. * (ie per segment) but that's an optimization for later, it
  1338. * requires some added smarts with our get/put_tce implementation
  1339. *
  1340. * Each TCE page is 4KB in size and each TCE entry occupies 8
  1341. * bytes
  1342. */
  1343. tce32_segsz = PNV_IODA1_DMA32_SEGSIZE >> (IOMMU_PAGE_SHIFT_4K - 3);
  1344. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  1345. get_order(tce32_segsz * segs));
  1346. if (!tce_mem) {
  1347. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  1348. goto fail;
  1349. }
  1350. addr = page_address(tce_mem);
  1351. memset(addr, 0, tce32_segsz * segs);
  1352. /* Configure HW */
  1353. for (i = 0; i < segs; i++) {
  1354. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  1355. pe->pe_number,
  1356. base + i, 1,
  1357. __pa(addr) + tce32_segsz * i,
  1358. tce32_segsz, IOMMU_PAGE_SIZE_4K);
  1359. if (rc) {
  1360. pe_err(pe, " Failed to configure 32-bit TCE table, err %lld\n",
  1361. rc);
  1362. goto fail;
  1363. }
  1364. }
  1365. /* Setup DMA32 segment mapping */
  1366. for (i = base; i < base + segs; i++)
  1367. phb->ioda.dma32_segmap[i] = pe->pe_number;
  1368. /* Setup linux iommu table */
  1369. pnv_pci_setup_iommu_table(tbl, addr, tce32_segsz * segs,
  1370. base * PNV_IODA1_DMA32_SEGSIZE,
  1371. IOMMU_PAGE_SHIFT_4K);
  1372. tbl->it_ops = &pnv_ioda1_iommu_ops;
  1373. pe->table_group.tce32_start = tbl->it_offset << tbl->it_page_shift;
  1374. pe->table_group.tce32_size = tbl->it_size << tbl->it_page_shift;
  1375. tbl->it_index = (phb->hose->global_number << 16) | pe->pe_number;
  1376. if (!iommu_init_table(tbl, phb->hose->node, 0, 0))
  1377. panic("Failed to initialize iommu table");
  1378. pe->dma_setup_done = true;
  1379. return;
  1380. fail:
  1381. /* XXX Failure: Try to fallback to 64-bit only ? */
  1382. if (tce_mem)
  1383. __free_pages(tce_mem, get_order(tce32_segsz * segs));
  1384. if (tbl) {
  1385. pnv_pci_unlink_table_and_group(tbl, &pe->table_group);
  1386. iommu_tce_table_put(tbl);
  1387. }
  1388. }
  1389. static long pnv_pci_ioda2_set_window(struct iommu_table_group *table_group,
  1390. int num, struct iommu_table *tbl)
  1391. {
  1392. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  1393. table_group);
  1394. struct pnv_phb *phb = pe->phb;
  1395. int64_t rc;
  1396. const unsigned long size = tbl->it_indirect_levels ?
  1397. tbl->it_level_size : tbl->it_size;
  1398. const __u64 start_addr = tbl->it_offset << tbl->it_page_shift;
  1399. const __u64 win_size = tbl->it_size << tbl->it_page_shift;
  1400. pe_info(pe, "Setting up window#%d %llx..%llx pg=%lx\n",
  1401. num, start_addr, start_addr + win_size - 1,
  1402. IOMMU_PAGE_SIZE(tbl));
  1403. /*
  1404. * Map TCE table through TVT. The TVE index is the PE number
  1405. * shifted by 1 bit for 32-bits DMA space.
  1406. */
  1407. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  1408. pe->pe_number,
  1409. (pe->pe_number << 1) + num,
  1410. tbl->it_indirect_levels + 1,
  1411. __pa(tbl->it_base),
  1412. size << 3,
  1413. IOMMU_PAGE_SIZE(tbl));
  1414. if (rc) {
  1415. pe_err(pe, "Failed to configure TCE table, err %lld\n", rc);
  1416. return rc;
  1417. }
  1418. pnv_pci_link_table_and_group(phb->hose->node, num,
  1419. tbl, &pe->table_group);
  1420. pnv_pci_ioda2_tce_invalidate_pe(pe);
  1421. return 0;
  1422. }
  1423. static void pnv_pci_ioda2_set_bypass(struct pnv_ioda_pe *pe, bool enable)
  1424. {
  1425. uint16_t window_id = (pe->pe_number << 1 ) + 1;
  1426. int64_t rc;
  1427. pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
  1428. if (enable) {
  1429. phys_addr_t top = memblock_end_of_DRAM();
  1430. top = roundup_pow_of_two(top);
  1431. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  1432. pe->pe_number,
  1433. window_id,
  1434. pe->tce_bypass_base,
  1435. top);
  1436. } else {
  1437. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  1438. pe->pe_number,
  1439. window_id,
  1440. pe->tce_bypass_base,
  1441. 0);
  1442. }
  1443. if (rc)
  1444. pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
  1445. else
  1446. pe->tce_bypass_enabled = enable;
  1447. }
  1448. static long pnv_pci_ioda2_create_table(struct iommu_table_group *table_group,
  1449. int num, __u32 page_shift, __u64 window_size, __u32 levels,
  1450. bool alloc_userspace_copy, struct iommu_table **ptbl)
  1451. {
  1452. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  1453. table_group);
  1454. int nid = pe->phb->hose->node;
  1455. __u64 bus_offset = num ? pe->tce_bypass_base : table_group->tce32_start;
  1456. long ret;
  1457. struct iommu_table *tbl;
  1458. tbl = pnv_pci_table_alloc(nid);
  1459. if (!tbl)
  1460. return -ENOMEM;
  1461. tbl->it_ops = &pnv_ioda2_iommu_ops;
  1462. ret = pnv_pci_ioda2_table_alloc_pages(nid,
  1463. bus_offset, page_shift, window_size,
  1464. levels, alloc_userspace_copy, tbl);
  1465. if (ret) {
  1466. iommu_tce_table_put(tbl);
  1467. return ret;
  1468. }
  1469. *ptbl = tbl;
  1470. return 0;
  1471. }
  1472. static long pnv_pci_ioda2_setup_default_config(struct pnv_ioda_pe *pe)
  1473. {
  1474. struct iommu_table *tbl = NULL;
  1475. long rc;
  1476. unsigned long res_start, res_end;
  1477. /*
  1478. * crashkernel= specifies the kdump kernel's maximum memory at
  1479. * some offset and there is no guaranteed the result is a power
  1480. * of 2, which will cause errors later.
  1481. */
  1482. const u64 max_memory = __rounddown_pow_of_two(memory_hotplug_max());
  1483. /*
  1484. * In memory constrained environments, e.g. kdump kernel, the
  1485. * DMA window can be larger than available memory, which will
  1486. * cause errors later.
  1487. */
  1488. const u64 maxblock = 1UL << (PAGE_SHIFT + MAX_ORDER - 1);
  1489. /*
  1490. * We create the default window as big as we can. The constraint is
  1491. * the max order of allocation possible. The TCE table is likely to
  1492. * end up being multilevel and with on-demand allocation in place,
  1493. * the initial use is not going to be huge as the default window aims
  1494. * to support crippled devices (i.e. not fully 64bit DMAble) only.
  1495. */
  1496. /* iommu_table::it_map uses 1 bit per IOMMU page, hence 8 */
  1497. const u64 window_size = min((maxblock * 8) << PAGE_SHIFT, max_memory);
  1498. /* Each TCE level cannot exceed maxblock so go multilevel if needed */
  1499. unsigned long tces_order = ilog2(window_size >> PAGE_SHIFT);
  1500. unsigned long tcelevel_order = ilog2(maxblock >> 3);
  1501. unsigned int levels = tces_order / tcelevel_order;
  1502. if (tces_order % tcelevel_order)
  1503. levels += 1;
  1504. /*
  1505. * We try to stick to default levels (which is >1 at the moment) in
  1506. * order to save memory by relying on on-demain TCE level allocation.
  1507. */
  1508. levels = max_t(unsigned int, levels, POWERNV_IOMMU_DEFAULT_LEVELS);
  1509. rc = pnv_pci_ioda2_create_table(&pe->table_group, 0, PAGE_SHIFT,
  1510. window_size, levels, false, &tbl);
  1511. if (rc) {
  1512. pe_err(pe, "Failed to create 32-bit TCE table, err %ld",
  1513. rc);
  1514. return rc;
  1515. }
  1516. /* We use top part of 32bit space for MMIO so exclude it from DMA */
  1517. res_start = 0;
  1518. res_end = 0;
  1519. if (window_size > pe->phb->ioda.m32_pci_base) {
  1520. res_start = pe->phb->ioda.m32_pci_base >> tbl->it_page_shift;
  1521. res_end = min(window_size, SZ_4G) >> tbl->it_page_shift;
  1522. }
  1523. tbl->it_index = (pe->phb->hose->global_number << 16) | pe->pe_number;
  1524. if (iommu_init_table(tbl, pe->phb->hose->node, res_start, res_end))
  1525. rc = pnv_pci_ioda2_set_window(&pe->table_group, 0, tbl);
  1526. else
  1527. rc = -ENOMEM;
  1528. if (rc) {
  1529. pe_err(pe, "Failed to configure 32-bit TCE table, err %ld\n", rc);
  1530. iommu_tce_table_put(tbl);
  1531. tbl = NULL; /* This clears iommu_table_base below */
  1532. }
  1533. if (!pnv_iommu_bypass_disabled)
  1534. pnv_pci_ioda2_set_bypass(pe, true);
  1535. /*
  1536. * Set table base for the case of IOMMU DMA use. Usually this is done
  1537. * from dma_dev_setup() which is not called when a device is returned
  1538. * from VFIO so do it here.
  1539. */
  1540. if (pe->pdev)
  1541. set_iommu_table_base(&pe->pdev->dev, tbl);
  1542. return 0;
  1543. }
  1544. static long pnv_pci_ioda2_unset_window(struct iommu_table_group *table_group,
  1545. int num)
  1546. {
  1547. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  1548. table_group);
  1549. struct pnv_phb *phb = pe->phb;
  1550. long ret;
  1551. pe_info(pe, "Removing DMA window #%d\n", num);
  1552. ret = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  1553. (pe->pe_number << 1) + num,
  1554. 0/* levels */, 0/* table address */,
  1555. 0/* table size */, 0/* page size */);
  1556. if (ret)
  1557. pe_warn(pe, "Unmapping failed, ret = %ld\n", ret);
  1558. else
  1559. pnv_pci_ioda2_tce_invalidate_pe(pe);
  1560. pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
  1561. return ret;
  1562. }
  1563. #ifdef CONFIG_IOMMU_API
  1564. unsigned long pnv_pci_ioda2_get_table_size(__u32 page_shift,
  1565. __u64 window_size, __u32 levels)
  1566. {
  1567. unsigned long bytes = 0;
  1568. const unsigned window_shift = ilog2(window_size);
  1569. unsigned entries_shift = window_shift - page_shift;
  1570. unsigned table_shift = entries_shift + 3;
  1571. unsigned long tce_table_size = max(0x1000UL, 1UL << table_shift);
  1572. unsigned long direct_table_size;
  1573. if (!levels || (levels > POWERNV_IOMMU_MAX_LEVELS) ||
  1574. !is_power_of_2(window_size))
  1575. return 0;
  1576. /* Calculate a direct table size from window_size and levels */
  1577. entries_shift = (entries_shift + levels - 1) / levels;
  1578. table_shift = entries_shift + 3;
  1579. table_shift = max_t(unsigned, table_shift, PAGE_SHIFT);
  1580. direct_table_size = 1UL << table_shift;
  1581. for ( ; levels; --levels) {
  1582. bytes += ALIGN(tce_table_size, direct_table_size);
  1583. tce_table_size /= direct_table_size;
  1584. tce_table_size <<= 3;
  1585. tce_table_size = max_t(unsigned long,
  1586. tce_table_size, direct_table_size);
  1587. }
  1588. return bytes + bytes; /* one for HW table, one for userspace copy */
  1589. }
  1590. static long pnv_pci_ioda2_create_table_userspace(
  1591. struct iommu_table_group *table_group,
  1592. int num, __u32 page_shift, __u64 window_size, __u32 levels,
  1593. struct iommu_table **ptbl)
  1594. {
  1595. long ret = pnv_pci_ioda2_create_table(table_group,
  1596. num, page_shift, window_size, levels, true, ptbl);
  1597. if (!ret)
  1598. (*ptbl)->it_allocated_size = pnv_pci_ioda2_get_table_size(
  1599. page_shift, window_size, levels);
  1600. return ret;
  1601. }
  1602. static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe, struct pci_bus *bus)
  1603. {
  1604. struct pci_dev *dev;
  1605. list_for_each_entry(dev, &bus->devices, bus_list) {
  1606. set_iommu_table_base(&dev->dev, pe->table_group.tables[0]);
  1607. dev->dev.archdata.dma_offset = pe->tce_bypass_base;
  1608. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  1609. pnv_ioda_setup_bus_dma(pe, dev->subordinate);
  1610. }
  1611. }
  1612. static void pnv_ioda2_take_ownership(struct iommu_table_group *table_group)
  1613. {
  1614. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  1615. table_group);
  1616. /* Store @tbl as pnv_pci_ioda2_unset_window() resets it */
  1617. struct iommu_table *tbl = pe->table_group.tables[0];
  1618. pnv_pci_ioda2_set_bypass(pe, false);
  1619. pnv_pci_ioda2_unset_window(&pe->table_group, 0);
  1620. if (pe->pbus)
  1621. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  1622. else if (pe->pdev)
  1623. set_iommu_table_base(&pe->pdev->dev, NULL);
  1624. iommu_tce_table_put(tbl);
  1625. }
  1626. static void pnv_ioda2_release_ownership(struct iommu_table_group *table_group)
  1627. {
  1628. struct pnv_ioda_pe *pe = container_of(table_group, struct pnv_ioda_pe,
  1629. table_group);
  1630. pnv_pci_ioda2_setup_default_config(pe);
  1631. if (pe->pbus)
  1632. pnv_ioda_setup_bus_dma(pe, pe->pbus);
  1633. }
  1634. static struct iommu_table_group_ops pnv_pci_ioda2_ops = {
  1635. .get_table_size = pnv_pci_ioda2_get_table_size,
  1636. .create_table = pnv_pci_ioda2_create_table_userspace,
  1637. .set_window = pnv_pci_ioda2_set_window,
  1638. .unset_window = pnv_pci_ioda2_unset_window,
  1639. .take_ownership = pnv_ioda2_take_ownership,
  1640. .release_ownership = pnv_ioda2_release_ownership,
  1641. };
  1642. #endif
  1643. void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  1644. struct pnv_ioda_pe *pe)
  1645. {
  1646. int64_t rc;
  1647. /* TVE #1 is selected by PCI address bit 59 */
  1648. pe->tce_bypass_base = 1ull << 59;
  1649. /* The PE will reserve all possible 32-bits space */
  1650. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  1651. phb->ioda.m32_pci_base);
  1652. /* Setup linux iommu table */
  1653. pe->table_group.tce32_start = 0;
  1654. pe->table_group.tce32_size = phb->ioda.m32_pci_base;
  1655. pe->table_group.max_dynamic_windows_supported =
  1656. IOMMU_TABLE_GROUP_MAX_TABLES;
  1657. pe->table_group.max_levels = POWERNV_IOMMU_MAX_LEVELS;
  1658. pe->table_group.pgsizes = pnv_ioda_parse_tce_sizes(phb);
  1659. rc = pnv_pci_ioda2_setup_default_config(pe);
  1660. if (rc)
  1661. return;
  1662. #ifdef CONFIG_IOMMU_API
  1663. pe->table_group.ops = &pnv_pci_ioda2_ops;
  1664. iommu_register_group(&pe->table_group, phb->hose->global_number,
  1665. pe->pe_number);
  1666. #endif
  1667. pe->dma_setup_done = true;
  1668. }
  1669. /*
  1670. * Called from KVM in real mode to EOI passthru interrupts. The ICP
  1671. * EOI is handled directly in KVM in kvmppc_deliver_irq_passthru().
  1672. *
  1673. * The IRQ data is mapped in the PCI-MSI domain and the EOI OPAL call
  1674. * needs an HW IRQ number mapped in the XICS IRQ domain. The HW IRQ
  1675. * numbers of the in-the-middle MSI domain are vector numbers and it's
  1676. * good enough for OPAL. Use that.
  1677. */
  1678. int64_t pnv_opal_pci_msi_eoi(struct irq_data *d)
  1679. {
  1680. struct pci_controller *hose = irq_data_get_irq_chip_data(d->parent_data);
  1681. struct pnv_phb *phb = hose->private_data;
  1682. return opal_pci_msi_eoi(phb->opal_id, d->parent_data->hwirq);
  1683. }
  1684. /*
  1685. * The IRQ data is mapped in the XICS domain, with OPAL HW IRQ numbers
  1686. */
  1687. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  1688. {
  1689. int64_t rc;
  1690. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1691. struct pci_controller *hose = irq_data_get_irq_chip_data(d);
  1692. struct pnv_phb *phb = hose->private_data;
  1693. rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
  1694. WARN_ON_ONCE(rc);
  1695. icp_native_eoi(d);
  1696. }
  1697. /* P8/CXL only */
  1698. void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
  1699. {
  1700. struct irq_data *idata;
  1701. struct irq_chip *ichip;
  1702. /* The MSI EOI OPAL call is only needed on PHB3 */
  1703. if (phb->model != PNV_PHB_MODEL_PHB3)
  1704. return;
  1705. if (!phb->ioda.irq_chip_init) {
  1706. /*
  1707. * First time we setup an MSI IRQ, we need to setup the
  1708. * corresponding IRQ chip to route correctly.
  1709. */
  1710. idata = irq_get_irq_data(virq);
  1711. ichip = irq_data_get_irq_chip(idata);
  1712. phb->ioda.irq_chip_init = 1;
  1713. phb->ioda.irq_chip = *ichip;
  1714. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  1715. }
  1716. irq_set_chip(virq, &phb->ioda.irq_chip);
  1717. irq_set_chip_data(virq, phb->hose);
  1718. }
  1719. static struct irq_chip pnv_pci_msi_irq_chip;
  1720. /*
  1721. * Returns true iff chip is something that we could call
  1722. * pnv_opal_pci_msi_eoi for.
  1723. */
  1724. bool is_pnv_opal_msi(struct irq_chip *chip)
  1725. {
  1726. return chip == &pnv_pci_msi_irq_chip;
  1727. }
  1728. EXPORT_SYMBOL_GPL(is_pnv_opal_msi);
  1729. static int __pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  1730. unsigned int xive_num,
  1731. unsigned int is_64, struct msi_msg *msg)
  1732. {
  1733. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  1734. __be32 data;
  1735. int rc;
  1736. dev_dbg(&dev->dev, "%s: setup %s-bit MSI for vector #%d\n", __func__,
  1737. is_64 ? "64" : "32", xive_num);
  1738. /* No PE assigned ? bail out ... no MSI for you ! */
  1739. if (pe == NULL)
  1740. return -ENXIO;
  1741. /* Check if we have an MVE */
  1742. if (pe->mve_number < 0)
  1743. return -ENXIO;
  1744. /* Force 32-bit MSI on some broken devices */
  1745. if (dev->no_64bit_msi)
  1746. is_64 = 0;
  1747. /* Assign XIVE to PE */
  1748. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  1749. if (rc) {
  1750. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  1751. pci_name(dev), rc, xive_num);
  1752. return -EIO;
  1753. }
  1754. if (is_64) {
  1755. __be64 addr64;
  1756. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  1757. &addr64, &data);
  1758. if (rc) {
  1759. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  1760. pci_name(dev), rc);
  1761. return -EIO;
  1762. }
  1763. msg->address_hi = be64_to_cpu(addr64) >> 32;
  1764. msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
  1765. } else {
  1766. __be32 addr32;
  1767. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  1768. &addr32, &data);
  1769. if (rc) {
  1770. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  1771. pci_name(dev), rc);
  1772. return -EIO;
  1773. }
  1774. msg->address_hi = 0;
  1775. msg->address_lo = be32_to_cpu(addr32);
  1776. }
  1777. msg->data = be32_to_cpu(data);
  1778. return 0;
  1779. }
  1780. /*
  1781. * The msi_free() op is called before irq_domain_free_irqs_top() when
  1782. * the handler data is still available. Use that to clear the XIVE
  1783. * controller.
  1784. */
  1785. static void pnv_msi_ops_msi_free(struct irq_domain *domain,
  1786. struct msi_domain_info *info,
  1787. unsigned int irq)
  1788. {
  1789. if (xive_enabled())
  1790. xive_irq_free_data(irq);
  1791. }
  1792. static struct msi_domain_ops pnv_pci_msi_domain_ops = {
  1793. .msi_free = pnv_msi_ops_msi_free,
  1794. };
  1795. static void pnv_msi_shutdown(struct irq_data *d)
  1796. {
  1797. d = d->parent_data;
  1798. if (d->chip->irq_shutdown)
  1799. d->chip->irq_shutdown(d);
  1800. }
  1801. static void pnv_msi_mask(struct irq_data *d)
  1802. {
  1803. pci_msi_mask_irq(d);
  1804. irq_chip_mask_parent(d);
  1805. }
  1806. static void pnv_msi_unmask(struct irq_data *d)
  1807. {
  1808. pci_msi_unmask_irq(d);
  1809. irq_chip_unmask_parent(d);
  1810. }
  1811. static struct irq_chip pnv_pci_msi_irq_chip = {
  1812. .name = "PNV-PCI-MSI",
  1813. .irq_shutdown = pnv_msi_shutdown,
  1814. .irq_mask = pnv_msi_mask,
  1815. .irq_unmask = pnv_msi_unmask,
  1816. .irq_eoi = irq_chip_eoi_parent,
  1817. };
  1818. static struct msi_domain_info pnv_msi_domain_info = {
  1819. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  1820. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  1821. .ops = &pnv_pci_msi_domain_ops,
  1822. .chip = &pnv_pci_msi_irq_chip,
  1823. };
  1824. static void pnv_msi_compose_msg(struct irq_data *d, struct msi_msg *msg)
  1825. {
  1826. struct msi_desc *entry = irq_data_get_msi_desc(d);
  1827. struct pci_dev *pdev = msi_desc_to_pci_dev(entry);
  1828. struct pci_controller *hose = irq_data_get_irq_chip_data(d);
  1829. struct pnv_phb *phb = hose->private_data;
  1830. int rc;
  1831. rc = __pnv_pci_ioda_msi_setup(phb, pdev, d->hwirq,
  1832. entry->pci.msi_attrib.is_64, msg);
  1833. if (rc)
  1834. dev_err(&pdev->dev, "Failed to setup %s-bit MSI #%ld : %d\n",
  1835. entry->pci.msi_attrib.is_64 ? "64" : "32", d->hwirq, rc);
  1836. }
  1837. /*
  1838. * The IRQ data is mapped in the MSI domain in which HW IRQ numbers
  1839. * correspond to vector numbers.
  1840. */
  1841. static void pnv_msi_eoi(struct irq_data *d)
  1842. {
  1843. struct pci_controller *hose = irq_data_get_irq_chip_data(d);
  1844. struct pnv_phb *phb = hose->private_data;
  1845. if (phb->model == PNV_PHB_MODEL_PHB3) {
  1846. /*
  1847. * The EOI OPAL call takes an OPAL HW IRQ number but
  1848. * since it is translated into a vector number in
  1849. * OPAL, use that directly.
  1850. */
  1851. WARN_ON_ONCE(opal_pci_msi_eoi(phb->opal_id, d->hwirq));
  1852. }
  1853. irq_chip_eoi_parent(d);
  1854. }
  1855. static struct irq_chip pnv_msi_irq_chip = {
  1856. .name = "PNV-MSI",
  1857. .irq_shutdown = pnv_msi_shutdown,
  1858. .irq_mask = irq_chip_mask_parent,
  1859. .irq_unmask = irq_chip_unmask_parent,
  1860. .irq_eoi = pnv_msi_eoi,
  1861. .irq_set_affinity = irq_chip_set_affinity_parent,
  1862. .irq_compose_msi_msg = pnv_msi_compose_msg,
  1863. };
  1864. static int pnv_irq_parent_domain_alloc(struct irq_domain *domain,
  1865. unsigned int virq, int hwirq)
  1866. {
  1867. struct irq_fwspec parent_fwspec;
  1868. int ret;
  1869. parent_fwspec.fwnode = domain->parent->fwnode;
  1870. parent_fwspec.param_count = 2;
  1871. parent_fwspec.param[0] = hwirq;
  1872. parent_fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  1873. ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &parent_fwspec);
  1874. if (ret)
  1875. return ret;
  1876. return 0;
  1877. }
  1878. static int pnv_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1879. unsigned int nr_irqs, void *arg)
  1880. {
  1881. struct pci_controller *hose = domain->host_data;
  1882. struct pnv_phb *phb = hose->private_data;
  1883. msi_alloc_info_t *info = arg;
  1884. struct pci_dev *pdev = msi_desc_to_pci_dev(info->desc);
  1885. int hwirq;
  1886. int i, ret;
  1887. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, nr_irqs);
  1888. if (hwirq < 0) {
  1889. dev_warn(&pdev->dev, "failed to find a free MSI\n");
  1890. return -ENOSPC;
  1891. }
  1892. dev_dbg(&pdev->dev, "%s bridge %pOF %d/%x #%d\n", __func__,
  1893. hose->dn, virq, hwirq, nr_irqs);
  1894. for (i = 0; i < nr_irqs; i++) {
  1895. ret = pnv_irq_parent_domain_alloc(domain, virq + i,
  1896. phb->msi_base + hwirq + i);
  1897. if (ret)
  1898. goto out;
  1899. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  1900. &pnv_msi_irq_chip, hose);
  1901. }
  1902. return 0;
  1903. out:
  1904. irq_domain_free_irqs_parent(domain, virq, i - 1);
  1905. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq, nr_irqs);
  1906. return ret;
  1907. }
  1908. static void pnv_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  1909. unsigned int nr_irqs)
  1910. {
  1911. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  1912. struct pci_controller *hose = irq_data_get_irq_chip_data(d);
  1913. struct pnv_phb *phb = hose->private_data;
  1914. pr_debug("%s bridge %pOF %d/%lx #%d\n", __func__, hose->dn,
  1915. virq, d->hwirq, nr_irqs);
  1916. msi_bitmap_free_hwirqs(&phb->msi_bmp, d->hwirq, nr_irqs);
  1917. /* XIVE domain is cleared through ->msi_free() */
  1918. }
  1919. static const struct irq_domain_ops pnv_irq_domain_ops = {
  1920. .alloc = pnv_irq_domain_alloc,
  1921. .free = pnv_irq_domain_free,
  1922. };
  1923. static int __init pnv_msi_allocate_domains(struct pci_controller *hose, unsigned int count)
  1924. {
  1925. struct pnv_phb *phb = hose->private_data;
  1926. struct irq_domain *parent = irq_get_default_host();
  1927. hose->fwnode = irq_domain_alloc_named_id_fwnode("PNV-MSI", phb->opal_id);
  1928. if (!hose->fwnode)
  1929. return -ENOMEM;
  1930. hose->dev_domain = irq_domain_create_hierarchy(parent, 0, count,
  1931. hose->fwnode,
  1932. &pnv_irq_domain_ops, hose);
  1933. if (!hose->dev_domain) {
  1934. pr_err("PCI: failed to create IRQ domain bridge %pOF (domain %d)\n",
  1935. hose->dn, hose->global_number);
  1936. irq_domain_free_fwnode(hose->fwnode);
  1937. return -ENOMEM;
  1938. }
  1939. hose->msi_domain = pci_msi_create_irq_domain(of_node_to_fwnode(hose->dn),
  1940. &pnv_msi_domain_info,
  1941. hose->dev_domain);
  1942. if (!hose->msi_domain) {
  1943. pr_err("PCI: failed to create MSI IRQ domain bridge %pOF (domain %d)\n",
  1944. hose->dn, hose->global_number);
  1945. irq_domain_free_fwnode(hose->fwnode);
  1946. irq_domain_remove(hose->dev_domain);
  1947. return -ENOMEM;
  1948. }
  1949. return 0;
  1950. }
  1951. static void __init pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  1952. {
  1953. unsigned int count;
  1954. const __be32 *prop = of_get_property(phb->hose->dn,
  1955. "ibm,opal-msi-ranges", NULL);
  1956. if (!prop) {
  1957. /* BML Fallback */
  1958. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  1959. }
  1960. if (!prop)
  1961. return;
  1962. phb->msi_base = be32_to_cpup(prop);
  1963. count = be32_to_cpup(prop + 1);
  1964. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  1965. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  1966. phb->hose->global_number);
  1967. return;
  1968. }
  1969. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  1970. count, phb->msi_base);
  1971. pnv_msi_allocate_domains(phb->hose, count);
  1972. }
  1973. static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
  1974. struct resource *res)
  1975. {
  1976. struct pnv_phb *phb = pe->phb;
  1977. struct pci_bus_region region;
  1978. int index;
  1979. int64_t rc;
  1980. if (!res || !res->flags || res->start > res->end)
  1981. return;
  1982. if (res->flags & IORESOURCE_IO) {
  1983. region.start = res->start - phb->ioda.io_pci_base;
  1984. region.end = res->end - phb->ioda.io_pci_base;
  1985. index = region.start / phb->ioda.io_segsize;
  1986. while (index < phb->ioda.total_pe_num &&
  1987. region.start <= region.end) {
  1988. phb->ioda.io_segmap[index] = pe->pe_number;
  1989. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  1990. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  1991. if (rc != OPAL_SUCCESS) {
  1992. pr_err("%s: Error %lld mapping IO segment#%d to PE#%x\n",
  1993. __func__, rc, index, pe->pe_number);
  1994. break;
  1995. }
  1996. region.start += phb->ioda.io_segsize;
  1997. index++;
  1998. }
  1999. } else if ((res->flags & IORESOURCE_MEM) &&
  2000. !pnv_pci_is_m64(phb, res)) {
  2001. region.start = res->start -
  2002. phb->hose->mem_offset[0] -
  2003. phb->ioda.m32_pci_base;
  2004. region.end = res->end -
  2005. phb->hose->mem_offset[0] -
  2006. phb->ioda.m32_pci_base;
  2007. index = region.start / phb->ioda.m32_segsize;
  2008. while (index < phb->ioda.total_pe_num &&
  2009. region.start <= region.end) {
  2010. phb->ioda.m32_segmap[index] = pe->pe_number;
  2011. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2012. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  2013. if (rc != OPAL_SUCCESS) {
  2014. pr_err("%s: Error %lld mapping M32 segment#%d to PE#%x",
  2015. __func__, rc, index, pe->pe_number);
  2016. break;
  2017. }
  2018. region.start += phb->ioda.m32_segsize;
  2019. index++;
  2020. }
  2021. }
  2022. }
  2023. /*
  2024. * This function is supposed to be called on basis of PE from top
  2025. * to bottom style. So the I/O or MMIO segment assigned to
  2026. * parent PE could be overridden by its child PEs if necessary.
  2027. */
  2028. static void pnv_ioda_setup_pe_seg(struct pnv_ioda_pe *pe)
  2029. {
  2030. struct pci_dev *pdev;
  2031. int i;
  2032. /*
  2033. * NOTE: We only care PCI bus based PE for now. For PCI
  2034. * device based PE, for example SRIOV sensitive VF should
  2035. * be figured out later.
  2036. */
  2037. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  2038. list_for_each_entry(pdev, &pe->pbus->devices, bus_list) {
  2039. for (i = 0; i <= PCI_ROM_RESOURCE; i++)
  2040. pnv_ioda_setup_pe_res(pe, &pdev->resource[i]);
  2041. /*
  2042. * If the PE contains all subordinate PCI buses, the
  2043. * windows of the child bridges should be mapped to
  2044. * the PE as well.
  2045. */
  2046. if (!(pe->flags & PNV_IODA_PE_BUS_ALL) || !pci_is_bridge(pdev))
  2047. continue;
  2048. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++)
  2049. pnv_ioda_setup_pe_res(pe,
  2050. &pdev->resource[PCI_BRIDGE_RESOURCES + i]);
  2051. }
  2052. }
  2053. #ifdef CONFIG_DEBUG_FS
  2054. static int pnv_pci_diag_data_set(void *data, u64 val)
  2055. {
  2056. struct pnv_phb *phb = data;
  2057. s64 ret;
  2058. /* Retrieve the diag data from firmware */
  2059. ret = opal_pci_get_phb_diag_data2(phb->opal_id, phb->diag_data,
  2060. phb->diag_data_size);
  2061. if (ret != OPAL_SUCCESS)
  2062. return -EIO;
  2063. /* Print the diag data to the kernel log */
  2064. pnv_pci_dump_phb_diag_data(phb->hose, phb->diag_data);
  2065. return 0;
  2066. }
  2067. DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_diag_data_fops, NULL, pnv_pci_diag_data_set,
  2068. "%llu\n");
  2069. static int pnv_pci_ioda_pe_dump(void *data, u64 val)
  2070. {
  2071. struct pnv_phb *phb = data;
  2072. int pe_num;
  2073. for (pe_num = 0; pe_num < phb->ioda.total_pe_num; pe_num++) {
  2074. struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_num];
  2075. if (!test_bit(pe_num, phb->ioda.pe_alloc))
  2076. continue;
  2077. pe_warn(pe, "rid: %04x dev count: %2d flags: %s%s%s%s%s%s\n",
  2078. pe->rid, pe->device_count,
  2079. (pe->flags & PNV_IODA_PE_DEV) ? "dev " : "",
  2080. (pe->flags & PNV_IODA_PE_BUS) ? "bus " : "",
  2081. (pe->flags & PNV_IODA_PE_BUS_ALL) ? "all " : "",
  2082. (pe->flags & PNV_IODA_PE_MASTER) ? "master " : "",
  2083. (pe->flags & PNV_IODA_PE_SLAVE) ? "slave " : "",
  2084. (pe->flags & PNV_IODA_PE_VF) ? "vf " : "");
  2085. }
  2086. return 0;
  2087. }
  2088. DEFINE_DEBUGFS_ATTRIBUTE(pnv_pci_ioda_pe_dump_fops, NULL,
  2089. pnv_pci_ioda_pe_dump, "%llu\n");
  2090. #endif /* CONFIG_DEBUG_FS */
  2091. static void pnv_pci_ioda_create_dbgfs(void)
  2092. {
  2093. #ifdef CONFIG_DEBUG_FS
  2094. struct pci_controller *hose, *tmp;
  2095. struct pnv_phb *phb;
  2096. char name[16];
  2097. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  2098. phb = hose->private_data;
  2099. sprintf(name, "PCI%04x", hose->global_number);
  2100. phb->dbgfs = debugfs_create_dir(name, arch_debugfs_dir);
  2101. debugfs_create_file_unsafe("dump_diag_regs", 0200, phb->dbgfs,
  2102. phb, &pnv_pci_diag_data_fops);
  2103. debugfs_create_file_unsafe("dump_ioda_pe_state", 0200, phb->dbgfs,
  2104. phb, &pnv_pci_ioda_pe_dump_fops);
  2105. }
  2106. #endif /* CONFIG_DEBUG_FS */
  2107. }
  2108. static void pnv_pci_enable_bridge(struct pci_bus *bus)
  2109. {
  2110. struct pci_dev *dev = bus->self;
  2111. struct pci_bus *child;
  2112. /* Empty bus ? bail */
  2113. if (list_empty(&bus->devices))
  2114. return;
  2115. /*
  2116. * If there's a bridge associated with that bus enable it. This works
  2117. * around races in the generic code if the enabling is done during
  2118. * parallel probing. This can be removed once those races have been
  2119. * fixed.
  2120. */
  2121. if (dev) {
  2122. int rc = pci_enable_device(dev);
  2123. if (rc)
  2124. pci_err(dev, "Error enabling bridge (%d)\n", rc);
  2125. pci_set_master(dev);
  2126. }
  2127. /* Perform the same to child busses */
  2128. list_for_each_entry(child, &bus->children, node)
  2129. pnv_pci_enable_bridge(child);
  2130. }
  2131. static void pnv_pci_enable_bridges(void)
  2132. {
  2133. struct pci_controller *hose;
  2134. list_for_each_entry(hose, &hose_list, list_node)
  2135. pnv_pci_enable_bridge(hose->bus);
  2136. }
  2137. static void pnv_pci_ioda_fixup(void)
  2138. {
  2139. pnv_pci_ioda_create_dbgfs();
  2140. pnv_pci_enable_bridges();
  2141. #ifdef CONFIG_EEH
  2142. pnv_eeh_post_init();
  2143. #endif
  2144. }
  2145. /*
  2146. * Returns the alignment for I/O or memory windows for P2P
  2147. * bridges. That actually depends on how PEs are segmented.
  2148. * For now, we return I/O or M32 segment size for PE sensitive
  2149. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  2150. * 1MiB for memory) will be returned.
  2151. *
  2152. * The current PCI bus might be put into one PE, which was
  2153. * create against the parent PCI bridge. For that case, we
  2154. * needn't enlarge the alignment so that we can save some
  2155. * resources.
  2156. */
  2157. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  2158. unsigned long type)
  2159. {
  2160. struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
  2161. int num_pci_bridges = 0;
  2162. struct pci_dev *bridge;
  2163. bridge = bus->self;
  2164. while (bridge) {
  2165. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  2166. num_pci_bridges++;
  2167. if (num_pci_bridges >= 2)
  2168. return 1;
  2169. }
  2170. bridge = bridge->bus->self;
  2171. }
  2172. /*
  2173. * We fall back to M32 if M64 isn't supported. We enforce the M64
  2174. * alignment for any 64-bit resource, PCIe doesn't care and
  2175. * bridges only do 64-bit prefetchable anyway.
  2176. */
  2177. if (phb->ioda.m64_segsize && pnv_pci_is_m64_flags(type))
  2178. return phb->ioda.m64_segsize;
  2179. if (type & IORESOURCE_MEM)
  2180. return phb->ioda.m32_segsize;
  2181. return phb->ioda.io_segsize;
  2182. }
  2183. /*
  2184. * We are updating root port or the upstream port of the
  2185. * bridge behind the root port with PHB's windows in order
  2186. * to accommodate the changes on required resources during
  2187. * PCI (slot) hotplug, which is connected to either root
  2188. * port or the downstream ports of PCIe switch behind the
  2189. * root port.
  2190. */
  2191. static void pnv_pci_fixup_bridge_resources(struct pci_bus *bus,
  2192. unsigned long type)
  2193. {
  2194. struct pci_controller *hose = pci_bus_to_host(bus);
  2195. struct pnv_phb *phb = hose->private_data;
  2196. struct pci_dev *bridge = bus->self;
  2197. struct resource *r, *w;
  2198. bool msi_region = false;
  2199. int i;
  2200. /* Check if we need apply fixup to the bridge's windows */
  2201. if (!pci_is_root_bus(bridge->bus) &&
  2202. !pci_is_root_bus(bridge->bus->self->bus))
  2203. return;
  2204. /* Fixup the resources */
  2205. for (i = 0; i < PCI_BRIDGE_RESOURCE_NUM; i++) {
  2206. r = &bridge->resource[PCI_BRIDGE_RESOURCES + i];
  2207. if (!r->flags || !r->parent)
  2208. continue;
  2209. w = NULL;
  2210. if (r->flags & type & IORESOURCE_IO)
  2211. w = &hose->io_resource;
  2212. else if (pnv_pci_is_m64(phb, r) &&
  2213. (type & IORESOURCE_PREFETCH) &&
  2214. phb->ioda.m64_segsize)
  2215. w = &hose->mem_resources[1];
  2216. else if (r->flags & type & IORESOURCE_MEM) {
  2217. w = &hose->mem_resources[0];
  2218. msi_region = true;
  2219. }
  2220. r->start = w->start;
  2221. r->end = w->end;
  2222. /* The 64KB 32-bits MSI region shouldn't be included in
  2223. * the 32-bits bridge window. Otherwise, we can see strange
  2224. * issues. One of them is EEH error observed on Garrison.
  2225. *
  2226. * Exclude top 1MB region which is the minimal alignment of
  2227. * 32-bits bridge window.
  2228. */
  2229. if (msi_region) {
  2230. r->end += 0x10000;
  2231. r->end -= 0x100000;
  2232. }
  2233. }
  2234. }
  2235. static void pnv_pci_configure_bus(struct pci_bus *bus)
  2236. {
  2237. struct pci_dev *bridge = bus->self;
  2238. struct pnv_ioda_pe *pe;
  2239. bool all = (bridge && pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE);
  2240. dev_info(&bus->dev, "Configuring PE for bus\n");
  2241. /* Don't assign PE to PCI bus, which doesn't have subordinate devices */
  2242. if (WARN_ON(list_empty(&bus->devices)))
  2243. return;
  2244. /* Reserve PEs according to used M64 resources */
  2245. pnv_ioda_reserve_m64_pe(bus, NULL, all);
  2246. /*
  2247. * Assign PE. We might run here because of partial hotplug.
  2248. * For the case, we just pick up the existing PE and should
  2249. * not allocate resources again.
  2250. */
  2251. pe = pnv_ioda_setup_bus_PE(bus, all);
  2252. if (!pe)
  2253. return;
  2254. pnv_ioda_setup_pe_seg(pe);
  2255. }
  2256. static resource_size_t pnv_pci_default_alignment(void)
  2257. {
  2258. return PAGE_SIZE;
  2259. }
  2260. /* Prevent enabling devices for which we couldn't properly
  2261. * assign a PE
  2262. */
  2263. static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
  2264. {
  2265. struct pci_dn *pdn;
  2266. pdn = pci_get_pdn(dev);
  2267. if (!pdn || pdn->pe_number == IODA_INVALID_PE) {
  2268. pci_err(dev, "pci_enable_device() blocked, no PE assigned.\n");
  2269. return false;
  2270. }
  2271. return true;
  2272. }
  2273. static bool pnv_ocapi_enable_device_hook(struct pci_dev *dev)
  2274. {
  2275. struct pci_dn *pdn;
  2276. struct pnv_ioda_pe *pe;
  2277. pdn = pci_get_pdn(dev);
  2278. if (!pdn)
  2279. return false;
  2280. if (pdn->pe_number == IODA_INVALID_PE) {
  2281. pe = pnv_ioda_setup_dev_PE(dev);
  2282. if (!pe)
  2283. return false;
  2284. }
  2285. return true;
  2286. }
  2287. static long pnv_pci_ioda1_unset_window(struct iommu_table_group *table_group,
  2288. int num)
  2289. {
  2290. struct pnv_ioda_pe *pe = container_of(table_group,
  2291. struct pnv_ioda_pe, table_group);
  2292. struct pnv_phb *phb = pe->phb;
  2293. unsigned int idx;
  2294. long rc;
  2295. pe_info(pe, "Removing DMA window #%d\n", num);
  2296. for (idx = 0; idx < phb->ioda.dma32_count; idx++) {
  2297. if (phb->ioda.dma32_segmap[idx] != pe->pe_number)
  2298. continue;
  2299. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  2300. idx, 0, 0ul, 0ul, 0ul);
  2301. if (rc != OPAL_SUCCESS) {
  2302. pe_warn(pe, "Failure %ld unmapping DMA32 segment#%d\n",
  2303. rc, idx);
  2304. return rc;
  2305. }
  2306. phb->ioda.dma32_segmap[idx] = IODA_INVALID_PE;
  2307. }
  2308. pnv_pci_unlink_table_and_group(table_group->tables[num], table_group);
  2309. return OPAL_SUCCESS;
  2310. }
  2311. static void pnv_pci_ioda1_release_pe_dma(struct pnv_ioda_pe *pe)
  2312. {
  2313. struct iommu_table *tbl = pe->table_group.tables[0];
  2314. int64_t rc;
  2315. if (!pe->dma_setup_done)
  2316. return;
  2317. rc = pnv_pci_ioda1_unset_window(&pe->table_group, 0);
  2318. if (rc != OPAL_SUCCESS)
  2319. return;
  2320. pnv_pci_p7ioc_tce_invalidate(tbl, tbl->it_offset, tbl->it_size);
  2321. if (pe->table_group.group) {
  2322. iommu_group_put(pe->table_group.group);
  2323. WARN_ON(pe->table_group.group);
  2324. }
  2325. free_pages(tbl->it_base, get_order(tbl->it_size << 3));
  2326. iommu_tce_table_put(tbl);
  2327. }
  2328. void pnv_pci_ioda2_release_pe_dma(struct pnv_ioda_pe *pe)
  2329. {
  2330. struct iommu_table *tbl = pe->table_group.tables[0];
  2331. int64_t rc;
  2332. if (!pe->dma_setup_done)
  2333. return;
  2334. rc = pnv_pci_ioda2_unset_window(&pe->table_group, 0);
  2335. if (rc)
  2336. pe_warn(pe, "OPAL error %lld release DMA window\n", rc);
  2337. pnv_pci_ioda2_set_bypass(pe, false);
  2338. if (pe->table_group.group) {
  2339. iommu_group_put(pe->table_group.group);
  2340. WARN_ON(pe->table_group.group);
  2341. }
  2342. iommu_tce_table_put(tbl);
  2343. }
  2344. static void pnv_ioda_free_pe_seg(struct pnv_ioda_pe *pe,
  2345. unsigned short win,
  2346. unsigned int *map)
  2347. {
  2348. struct pnv_phb *phb = pe->phb;
  2349. int idx;
  2350. int64_t rc;
  2351. for (idx = 0; idx < phb->ioda.total_pe_num; idx++) {
  2352. if (map[idx] != pe->pe_number)
  2353. continue;
  2354. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  2355. phb->ioda.reserved_pe_idx, win, 0, idx);
  2356. if (rc != OPAL_SUCCESS)
  2357. pe_warn(pe, "Error %lld unmapping (%d) segment#%d\n",
  2358. rc, win, idx);
  2359. map[idx] = IODA_INVALID_PE;
  2360. }
  2361. }
  2362. static void pnv_ioda_release_pe_seg(struct pnv_ioda_pe *pe)
  2363. {
  2364. struct pnv_phb *phb = pe->phb;
  2365. if (phb->type == PNV_PHB_IODA1) {
  2366. pnv_ioda_free_pe_seg(pe, OPAL_IO_WINDOW_TYPE,
  2367. phb->ioda.io_segmap);
  2368. pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
  2369. phb->ioda.m32_segmap);
  2370. /* M64 is pre-configured by pnv_ioda1_init_m64() */
  2371. } else if (phb->type == PNV_PHB_IODA2) {
  2372. pnv_ioda_free_pe_seg(pe, OPAL_M32_WINDOW_TYPE,
  2373. phb->ioda.m32_segmap);
  2374. }
  2375. }
  2376. static void pnv_ioda_release_pe(struct pnv_ioda_pe *pe)
  2377. {
  2378. struct pnv_phb *phb = pe->phb;
  2379. struct pnv_ioda_pe *slave, *tmp;
  2380. pe_info(pe, "Releasing PE\n");
  2381. mutex_lock(&phb->ioda.pe_list_mutex);
  2382. list_del(&pe->list);
  2383. mutex_unlock(&phb->ioda.pe_list_mutex);
  2384. switch (phb->type) {
  2385. case PNV_PHB_IODA1:
  2386. pnv_pci_ioda1_release_pe_dma(pe);
  2387. break;
  2388. case PNV_PHB_IODA2:
  2389. pnv_pci_ioda2_release_pe_dma(pe);
  2390. break;
  2391. case PNV_PHB_NPU_OCAPI:
  2392. break;
  2393. default:
  2394. WARN_ON(1);
  2395. }
  2396. pnv_ioda_release_pe_seg(pe);
  2397. pnv_ioda_deconfigure_pe(pe->phb, pe);
  2398. /* Release slave PEs in the compound PE */
  2399. if (pe->flags & PNV_IODA_PE_MASTER) {
  2400. list_for_each_entry_safe(slave, tmp, &pe->slaves, list) {
  2401. list_del(&slave->list);
  2402. pnv_ioda_free_pe(slave);
  2403. }
  2404. }
  2405. /*
  2406. * The PE for root bus can be removed because of hotplug in EEH
  2407. * recovery for fenced PHB error. We need to mark the PE dead so
  2408. * that it can be populated again in PCI hot add path. The PE
  2409. * shouldn't be destroyed as it's the global reserved resource.
  2410. */
  2411. if (phb->ioda.root_pe_idx == pe->pe_number)
  2412. return;
  2413. pnv_ioda_free_pe(pe);
  2414. }
  2415. static void pnv_pci_release_device(struct pci_dev *pdev)
  2416. {
  2417. struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
  2418. struct pci_dn *pdn = pci_get_pdn(pdev);
  2419. struct pnv_ioda_pe *pe;
  2420. /* The VF PE state is torn down when sriov_disable() is called */
  2421. if (pdev->is_virtfn)
  2422. return;
  2423. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  2424. return;
  2425. #ifdef CONFIG_PCI_IOV
  2426. /*
  2427. * FIXME: Try move this to sriov_disable(). It's here since we allocate
  2428. * the iov state at probe time since we need to fiddle with the IOV
  2429. * resources.
  2430. */
  2431. if (pdev->is_physfn)
  2432. kfree(pdev->dev.archdata.iov_data);
  2433. #endif
  2434. /*
  2435. * PCI hotplug can happen as part of EEH error recovery. The @pdn
  2436. * isn't removed and added afterwards in this scenario. We should
  2437. * set the PE number in @pdn to an invalid one. Otherwise, the PE's
  2438. * device count is decreased on removing devices while failing to
  2439. * be increased on adding devices. It leads to unbalanced PE's device
  2440. * count and eventually make normal PCI hotplug path broken.
  2441. */
  2442. pe = &phb->ioda.pe_array[pdn->pe_number];
  2443. pdn->pe_number = IODA_INVALID_PE;
  2444. WARN_ON(--pe->device_count < 0);
  2445. if (pe->device_count == 0)
  2446. pnv_ioda_release_pe(pe);
  2447. }
  2448. static void pnv_pci_ioda_shutdown(struct pci_controller *hose)
  2449. {
  2450. struct pnv_phb *phb = hose->private_data;
  2451. opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
  2452. OPAL_ASSERT_RESET);
  2453. }
  2454. static void pnv_pci_ioda_dma_bus_setup(struct pci_bus *bus)
  2455. {
  2456. struct pnv_phb *phb = pci_bus_to_pnvhb(bus);
  2457. struct pnv_ioda_pe *pe;
  2458. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  2459. if (!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)))
  2460. continue;
  2461. if (!pe->pbus)
  2462. continue;
  2463. if (bus->number == ((pe->rid >> 8) & 0xFF)) {
  2464. pe->pbus = bus;
  2465. break;
  2466. }
  2467. }
  2468. }
  2469. static const struct pci_controller_ops pnv_pci_ioda_controller_ops = {
  2470. .dma_dev_setup = pnv_pci_ioda_dma_dev_setup,
  2471. .dma_bus_setup = pnv_pci_ioda_dma_bus_setup,
  2472. .iommu_bypass_supported = pnv_pci_ioda_iommu_bypass_supported,
  2473. .enable_device_hook = pnv_pci_enable_device_hook,
  2474. .release_device = pnv_pci_release_device,
  2475. .window_alignment = pnv_pci_window_alignment,
  2476. .setup_bridge = pnv_pci_fixup_bridge_resources,
  2477. .reset_secondary_bus = pnv_pci_reset_secondary_bus,
  2478. .shutdown = pnv_pci_ioda_shutdown,
  2479. };
  2480. static const struct pci_controller_ops pnv_npu_ocapi_ioda_controller_ops = {
  2481. .enable_device_hook = pnv_ocapi_enable_device_hook,
  2482. .release_device = pnv_pci_release_device,
  2483. .window_alignment = pnv_pci_window_alignment,
  2484. .reset_secondary_bus = pnv_pci_reset_secondary_bus,
  2485. .shutdown = pnv_pci_ioda_shutdown,
  2486. };
  2487. static void __init pnv_pci_init_ioda_phb(struct device_node *np,
  2488. u64 hub_id, int ioda_type)
  2489. {
  2490. struct pci_controller *hose;
  2491. struct pnv_phb *phb;
  2492. unsigned long size, m64map_off, m32map_off, pemap_off;
  2493. unsigned long iomap_off = 0, dma32map_off = 0;
  2494. struct pnv_ioda_pe *root_pe;
  2495. struct resource r;
  2496. const __be64 *prop64;
  2497. const __be32 *prop32;
  2498. int len;
  2499. unsigned int segno;
  2500. u64 phb_id;
  2501. void *aux;
  2502. long rc;
  2503. if (!of_device_is_available(np))
  2504. return;
  2505. pr_info("Initializing %s PHB (%pOF)\n", pnv_phb_names[ioda_type], np);
  2506. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  2507. if (!prop64) {
  2508. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  2509. return;
  2510. }
  2511. phb_id = be64_to_cpup(prop64);
  2512. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  2513. phb = kzalloc(sizeof(*phb), GFP_KERNEL);
  2514. if (!phb)
  2515. panic("%s: Failed to allocate %zu bytes\n", __func__,
  2516. sizeof(*phb));
  2517. /* Allocate PCI controller */
  2518. phb->hose = hose = pcibios_alloc_controller(np);
  2519. if (!phb->hose) {
  2520. pr_err(" Can't allocate PCI controller for %pOF\n",
  2521. np);
  2522. memblock_free(phb, sizeof(struct pnv_phb));
  2523. return;
  2524. }
  2525. spin_lock_init(&phb->lock);
  2526. prop32 = of_get_property(np, "bus-range", &len);
  2527. if (prop32 && len == 8) {
  2528. hose->first_busno = be32_to_cpu(prop32[0]);
  2529. hose->last_busno = be32_to_cpu(prop32[1]);
  2530. } else {
  2531. pr_warn(" Broken <bus-range> on %pOF\n", np);
  2532. hose->first_busno = 0;
  2533. hose->last_busno = 0xff;
  2534. }
  2535. hose->private_data = phb;
  2536. phb->hub_id = hub_id;
  2537. phb->opal_id = phb_id;
  2538. phb->type = ioda_type;
  2539. mutex_init(&phb->ioda.pe_alloc_mutex);
  2540. /* Detect specific models for error handling */
  2541. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  2542. phb->model = PNV_PHB_MODEL_P7IOC;
  2543. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  2544. phb->model = PNV_PHB_MODEL_PHB3;
  2545. else
  2546. phb->model = PNV_PHB_MODEL_UNKNOWN;
  2547. /* Initialize diagnostic data buffer */
  2548. prop32 = of_get_property(np, "ibm,phb-diag-data-size", NULL);
  2549. if (prop32)
  2550. phb->diag_data_size = be32_to_cpup(prop32);
  2551. else
  2552. phb->diag_data_size = PNV_PCI_DIAG_BUF_SIZE;
  2553. phb->diag_data = kzalloc(phb->diag_data_size, GFP_KERNEL);
  2554. if (!phb->diag_data)
  2555. panic("%s: Failed to allocate %u bytes\n", __func__,
  2556. phb->diag_data_size);
  2557. /* Parse 32-bit and IO ranges (if any) */
  2558. pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
  2559. /* Get registers */
  2560. if (!of_address_to_resource(np, 0, &r)) {
  2561. phb->regs_phys = r.start;
  2562. phb->regs = ioremap(r.start, resource_size(&r));
  2563. if (phb->regs == NULL)
  2564. pr_err(" Failed to map registers !\n");
  2565. }
  2566. /* Initialize more IODA stuff */
  2567. phb->ioda.total_pe_num = 1;
  2568. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  2569. if (prop32)
  2570. phb->ioda.total_pe_num = be32_to_cpup(prop32);
  2571. prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
  2572. if (prop32)
  2573. phb->ioda.reserved_pe_idx = be32_to_cpup(prop32);
  2574. /* Invalidate RID to PE# mapping */
  2575. for (segno = 0; segno < ARRAY_SIZE(phb->ioda.pe_rmap); segno++)
  2576. phb->ioda.pe_rmap[segno] = IODA_INVALID_PE;
  2577. /* Parse 64-bit MMIO range */
  2578. pnv_ioda_parse_m64_window(phb);
  2579. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  2580. /* FW Has already off top 64k of M32 space (MSI space) */
  2581. phb->ioda.m32_size += 0x10000;
  2582. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe_num;
  2583. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  2584. phb->ioda.io_size = hose->pci_io_size;
  2585. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe_num;
  2586. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  2587. /* Calculate how many 32-bit TCE segments we have */
  2588. phb->ioda.dma32_count = phb->ioda.m32_pci_base /
  2589. PNV_IODA1_DMA32_SEGSIZE;
  2590. /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
  2591. size = ALIGN(max_t(unsigned, phb->ioda.total_pe_num, 8) / 8,
  2592. sizeof(unsigned long));
  2593. m64map_off = size;
  2594. size += phb->ioda.total_pe_num * sizeof(phb->ioda.m64_segmap[0]);
  2595. m32map_off = size;
  2596. size += phb->ioda.total_pe_num * sizeof(phb->ioda.m32_segmap[0]);
  2597. if (phb->type == PNV_PHB_IODA1) {
  2598. iomap_off = size;
  2599. size += phb->ioda.total_pe_num * sizeof(phb->ioda.io_segmap[0]);
  2600. dma32map_off = size;
  2601. size += phb->ioda.dma32_count *
  2602. sizeof(phb->ioda.dma32_segmap[0]);
  2603. }
  2604. pemap_off = size;
  2605. size += phb->ioda.total_pe_num * sizeof(struct pnv_ioda_pe);
  2606. aux = kzalloc(size, GFP_KERNEL);
  2607. if (!aux)
  2608. panic("%s: Failed to allocate %lu bytes\n", __func__, size);
  2609. phb->ioda.pe_alloc = aux;
  2610. phb->ioda.m64_segmap = aux + m64map_off;
  2611. phb->ioda.m32_segmap = aux + m32map_off;
  2612. for (segno = 0; segno < phb->ioda.total_pe_num; segno++) {
  2613. phb->ioda.m64_segmap[segno] = IODA_INVALID_PE;
  2614. phb->ioda.m32_segmap[segno] = IODA_INVALID_PE;
  2615. }
  2616. if (phb->type == PNV_PHB_IODA1) {
  2617. phb->ioda.io_segmap = aux + iomap_off;
  2618. for (segno = 0; segno < phb->ioda.total_pe_num; segno++)
  2619. phb->ioda.io_segmap[segno] = IODA_INVALID_PE;
  2620. phb->ioda.dma32_segmap = aux + dma32map_off;
  2621. for (segno = 0; segno < phb->ioda.dma32_count; segno++)
  2622. phb->ioda.dma32_segmap[segno] = IODA_INVALID_PE;
  2623. }
  2624. phb->ioda.pe_array = aux + pemap_off;
  2625. /*
  2626. * Choose PE number for root bus, which shouldn't have
  2627. * M64 resources consumed by its child devices. To pick
  2628. * the PE number adjacent to the reserved one if possible.
  2629. */
  2630. pnv_ioda_reserve_pe(phb, phb->ioda.reserved_pe_idx);
  2631. if (phb->ioda.reserved_pe_idx == 0) {
  2632. phb->ioda.root_pe_idx = 1;
  2633. pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
  2634. } else if (phb->ioda.reserved_pe_idx == (phb->ioda.total_pe_num - 1)) {
  2635. phb->ioda.root_pe_idx = phb->ioda.reserved_pe_idx - 1;
  2636. pnv_ioda_reserve_pe(phb, phb->ioda.root_pe_idx);
  2637. } else {
  2638. /* otherwise just allocate one */
  2639. root_pe = pnv_ioda_alloc_pe(phb, 1);
  2640. phb->ioda.root_pe_idx = root_pe->pe_number;
  2641. }
  2642. INIT_LIST_HEAD(&phb->ioda.pe_list);
  2643. mutex_init(&phb->ioda.pe_list_mutex);
  2644. /* Calculate how many 32-bit TCE segments we have */
  2645. phb->ioda.dma32_count = phb->ioda.m32_pci_base /
  2646. PNV_IODA1_DMA32_SEGSIZE;
  2647. #if 0 /* We should really do that ... */
  2648. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  2649. window_type,
  2650. window_num,
  2651. starting_real_address,
  2652. starting_pci_address,
  2653. segment_size);
  2654. #endif
  2655. pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
  2656. phb->ioda.total_pe_num, phb->ioda.reserved_pe_idx,
  2657. phb->ioda.m32_size, phb->ioda.m32_segsize);
  2658. if (phb->ioda.m64_size)
  2659. pr_info(" M64: 0x%lx [segment=0x%lx]\n",
  2660. phb->ioda.m64_size, phb->ioda.m64_segsize);
  2661. if (phb->ioda.io_size)
  2662. pr_info(" IO: 0x%x [segment=0x%x]\n",
  2663. phb->ioda.io_size, phb->ioda.io_segsize);
  2664. phb->hose->ops = &pnv_pci_ops;
  2665. phb->get_pe_state = pnv_ioda_get_pe_state;
  2666. phb->freeze_pe = pnv_ioda_freeze_pe;
  2667. phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
  2668. /* Setup MSI support */
  2669. pnv_pci_init_ioda_msis(phb);
  2670. /*
  2671. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  2672. * to let the PCI core do resource assignment. It's supposed
  2673. * that the PCI core will do correct I/O and MMIO alignment
  2674. * for the P2P bridge bars so that each PCI bus (excluding
  2675. * the child P2P bridges) can form individual PE.
  2676. */
  2677. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  2678. switch (phb->type) {
  2679. case PNV_PHB_NPU_OCAPI:
  2680. hose->controller_ops = pnv_npu_ocapi_ioda_controller_ops;
  2681. break;
  2682. default:
  2683. hose->controller_ops = pnv_pci_ioda_controller_ops;
  2684. }
  2685. ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;
  2686. #ifdef CONFIG_PCI_IOV
  2687. ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov;
  2688. ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
  2689. ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;
  2690. ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;
  2691. #endif
  2692. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  2693. /* Reset IODA tables to a clean state */
  2694. rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
  2695. if (rc)
  2696. pr_warn(" OPAL Error %ld performing IODA table reset !\n", rc);
  2697. /*
  2698. * If we're running in kdump kernel, the previous kernel never
  2699. * shutdown PCI devices correctly. We already got IODA table
  2700. * cleaned out. So we have to issue PHB reset to stop all PCI
  2701. * transactions from previous kernel. The ppc_pci_reset_phbs
  2702. * kernel parameter will force this reset too. Additionally,
  2703. * if the IODA reset above failed then use a bigger hammer.
  2704. * This can happen if we get a PHB fatal error in very early
  2705. * boot.
  2706. */
  2707. if (is_kdump_kernel() || pci_reset_phbs || rc) {
  2708. pr_info(" Issue PHB reset ...\n");
  2709. pnv_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
  2710. pnv_eeh_phb_reset(hose, EEH_RESET_DEACTIVATE);
  2711. }
  2712. /* Remove M64 resource if we can't configure it successfully */
  2713. if (!phb->init_m64 || phb->init_m64(phb))
  2714. hose->mem_resources[1].flags = 0;
  2715. /* create pci_dn's for DT nodes under this PHB */
  2716. pci_devs_phb_init_dynamic(hose);
  2717. }
  2718. void __init pnv_pci_init_ioda2_phb(struct device_node *np)
  2719. {
  2720. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
  2721. }
  2722. void __init pnv_pci_init_npu2_opencapi_phb(struct device_node *np)
  2723. {
  2724. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_NPU_OCAPI);
  2725. }
  2726. static void pnv_npu2_opencapi_cfg_size_fixup(struct pci_dev *dev)
  2727. {
  2728. struct pnv_phb *phb = pci_bus_to_pnvhb(dev->bus);
  2729. if (!machine_is(powernv))
  2730. return;
  2731. if (phb->type == PNV_PHB_NPU_OCAPI)
  2732. dev->cfg_size = PCI_CFG_SPACE_EXP_SIZE;
  2733. }
  2734. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pnv_npu2_opencapi_cfg_size_fixup);
  2735. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  2736. {
  2737. struct device_node *phbn;
  2738. const __be64 *prop64;
  2739. u64 hub_id;
  2740. pr_info("Probing IODA IO-Hub %pOF\n", np);
  2741. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  2742. if (!prop64) {
  2743. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  2744. return;
  2745. }
  2746. hub_id = be64_to_cpup(prop64);
  2747. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  2748. /* Count child PHBs */
  2749. for_each_child_of_node(np, phbn) {
  2750. /* Look for IODA1 PHBs */
  2751. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  2752. pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
  2753. }
  2754. }