pci-ioda.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. /*
  2. * Support PCI/PCIe on PowerNV platforms
  3. *
  4. * Copyright 2011 Benjamin Herrenschmidt, IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #undef DEBUG
  12. #include <linux/kernel.h>
  13. #include <linux/pci.h>
  14. #include <linux/crash_dump.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/string.h>
  18. #include <linux/init.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/irq.h>
  21. #include <linux/io.h>
  22. #include <linux/msi.h>
  23. #include <linux/memblock.h>
  24. #include <asm/sections.h>
  25. #include <asm/io.h>
  26. #include <asm/prom.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/machdep.h>
  29. #include <asm/msi_bitmap.h>
  30. #include <asm/ppc-pci.h>
  31. #include <asm/opal.h>
  32. #include <asm/iommu.h>
  33. #include <asm/tce.h>
  34. #include <asm/xics.h>
  35. #include <asm/debug.h>
  36. #include <asm/firmware.h>
  37. #include <asm/pnv-pci.h>
  38. #include <misc/cxl.h>
  39. #include "powernv.h"
  40. #include "pci.h"
  41. static void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
  42. const char *fmt, ...)
  43. {
  44. struct va_format vaf;
  45. va_list args;
  46. char pfix[32];
  47. va_start(args, fmt);
  48. vaf.fmt = fmt;
  49. vaf.va = &args;
  50. if (pe->pdev)
  51. strlcpy(pfix, dev_name(&pe->pdev->dev), sizeof(pfix));
  52. else
  53. sprintf(pfix, "%04x:%02x ",
  54. pci_domain_nr(pe->pbus), pe->pbus->number);
  55. printk("%spci %s: [PE# %.3d] %pV",
  56. level, pfix, pe->pe_number, &vaf);
  57. va_end(args);
  58. }
  59. #define pe_err(pe, fmt, ...) \
  60. pe_level_printk(pe, KERN_ERR, fmt, ##__VA_ARGS__)
  61. #define pe_warn(pe, fmt, ...) \
  62. pe_level_printk(pe, KERN_WARNING, fmt, ##__VA_ARGS__)
  63. #define pe_info(pe, fmt, ...) \
  64. pe_level_printk(pe, KERN_INFO, fmt, ##__VA_ARGS__)
  65. /*
  66. * stdcix is only supposed to be used in hypervisor real mode as per
  67. * the architecture spec
  68. */
  69. static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
  70. {
  71. __asm__ __volatile__("stdcix %0,0,%1"
  72. : : "r" (val), "r" (paddr) : "memory");
  73. }
  74. static inline bool pnv_pci_is_mem_pref_64(unsigned long flags)
  75. {
  76. return ((flags & (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH)) ==
  77. (IORESOURCE_MEM_64 | IORESOURCE_PREFETCH));
  78. }
  79. static int pnv_ioda_alloc_pe(struct pnv_phb *phb)
  80. {
  81. unsigned long pe;
  82. do {
  83. pe = find_next_zero_bit(phb->ioda.pe_alloc,
  84. phb->ioda.total_pe, 0);
  85. if (pe >= phb->ioda.total_pe)
  86. return IODA_INVALID_PE;
  87. } while(test_and_set_bit(pe, phb->ioda.pe_alloc));
  88. phb->ioda.pe_array[pe].phb = phb;
  89. phb->ioda.pe_array[pe].pe_number = pe;
  90. return pe;
  91. }
  92. static void pnv_ioda_free_pe(struct pnv_phb *phb, int pe)
  93. {
  94. WARN_ON(phb->ioda.pe_array[pe].pdev);
  95. memset(&phb->ioda.pe_array[pe], 0, sizeof(struct pnv_ioda_pe));
  96. clear_bit(pe, phb->ioda.pe_alloc);
  97. }
  98. /* The default M64 BAR is shared by all PEs */
  99. static int pnv_ioda2_init_m64(struct pnv_phb *phb)
  100. {
  101. const char *desc;
  102. struct resource *r;
  103. s64 rc;
  104. /* Configure the default M64 BAR */
  105. rc = opal_pci_set_phb_mem_window(phb->opal_id,
  106. OPAL_M64_WINDOW_TYPE,
  107. phb->ioda.m64_bar_idx,
  108. phb->ioda.m64_base,
  109. 0, /* unused */
  110. phb->ioda.m64_size);
  111. if (rc != OPAL_SUCCESS) {
  112. desc = "configuring";
  113. goto fail;
  114. }
  115. /* Enable the default M64 BAR */
  116. rc = opal_pci_phb_mmio_enable(phb->opal_id,
  117. OPAL_M64_WINDOW_TYPE,
  118. phb->ioda.m64_bar_idx,
  119. OPAL_ENABLE_M64_SPLIT);
  120. if (rc != OPAL_SUCCESS) {
  121. desc = "enabling";
  122. goto fail;
  123. }
  124. /* Mark the M64 BAR assigned */
  125. set_bit(phb->ioda.m64_bar_idx, &phb->ioda.m64_bar_alloc);
  126. /*
  127. * Strip off the segment used by the reserved PE, which is
  128. * expected to be 0 or last one of PE capabicity.
  129. */
  130. r = &phb->hose->mem_resources[1];
  131. if (phb->ioda.reserved_pe == 0)
  132. r->start += phb->ioda.m64_segsize;
  133. else if (phb->ioda.reserved_pe == (phb->ioda.total_pe - 1))
  134. r->end -= phb->ioda.m64_segsize;
  135. else
  136. pr_warn(" Cannot strip M64 segment for reserved PE#%d\n",
  137. phb->ioda.reserved_pe);
  138. return 0;
  139. fail:
  140. pr_warn(" Failure %lld %s M64 BAR#%d\n",
  141. rc, desc, phb->ioda.m64_bar_idx);
  142. opal_pci_phb_mmio_enable(phb->opal_id,
  143. OPAL_M64_WINDOW_TYPE,
  144. phb->ioda.m64_bar_idx,
  145. OPAL_DISABLE_M64);
  146. return -EIO;
  147. }
  148. static void pnv_ioda2_alloc_m64_pe(struct pnv_phb *phb)
  149. {
  150. resource_size_t sgsz = phb->ioda.m64_segsize;
  151. struct pci_dev *pdev;
  152. struct resource *r;
  153. int base, step, i;
  154. /*
  155. * Root bus always has full M64 range and root port has
  156. * M64 range used in reality. So we're checking root port
  157. * instead of root bus.
  158. */
  159. list_for_each_entry(pdev, &phb->hose->bus->devices, bus_list) {
  160. for (i = PCI_BRIDGE_RESOURCES;
  161. i <= PCI_BRIDGE_RESOURCE_END; i++) {
  162. r = &pdev->resource[i];
  163. if (!r->parent ||
  164. !pnv_pci_is_mem_pref_64(r->flags))
  165. continue;
  166. base = (r->start - phb->ioda.m64_base) / sgsz;
  167. for (step = 0; step < resource_size(r) / sgsz; step++)
  168. set_bit(base + step, phb->ioda.pe_alloc);
  169. }
  170. }
  171. }
  172. static int pnv_ioda2_pick_m64_pe(struct pnv_phb *phb,
  173. struct pci_bus *bus, int all)
  174. {
  175. resource_size_t segsz = phb->ioda.m64_segsize;
  176. struct pci_dev *pdev;
  177. struct resource *r;
  178. struct pnv_ioda_pe *master_pe, *pe;
  179. unsigned long size, *pe_alloc;
  180. bool found;
  181. int start, i, j;
  182. /* Root bus shouldn't use M64 */
  183. if (pci_is_root_bus(bus))
  184. return IODA_INVALID_PE;
  185. /* We support only one M64 window on each bus */
  186. found = false;
  187. pci_bus_for_each_resource(bus, r, i) {
  188. if (r && r->parent &&
  189. pnv_pci_is_mem_pref_64(r->flags)) {
  190. found = true;
  191. break;
  192. }
  193. }
  194. /* No M64 window found ? */
  195. if (!found)
  196. return IODA_INVALID_PE;
  197. /* Allocate bitmap */
  198. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  199. pe_alloc = kzalloc(size, GFP_KERNEL);
  200. if (!pe_alloc) {
  201. pr_warn("%s: Out of memory !\n",
  202. __func__);
  203. return IODA_INVALID_PE;
  204. }
  205. /*
  206. * Figure out reserved PE numbers by the PE
  207. * the its child PEs.
  208. */
  209. start = (r->start - phb->ioda.m64_base) / segsz;
  210. for (i = 0; i < resource_size(r) / segsz; i++)
  211. set_bit(start + i, pe_alloc);
  212. if (all)
  213. goto done;
  214. /*
  215. * If the PE doesn't cover all subordinate buses,
  216. * we need subtract from reserved PEs for children.
  217. */
  218. list_for_each_entry(pdev, &bus->devices, bus_list) {
  219. if (!pdev->subordinate)
  220. continue;
  221. pci_bus_for_each_resource(pdev->subordinate, r, i) {
  222. if (!r || !r->parent ||
  223. !pnv_pci_is_mem_pref_64(r->flags))
  224. continue;
  225. start = (r->start - phb->ioda.m64_base) / segsz;
  226. for (j = 0; j < resource_size(r) / segsz ; j++)
  227. clear_bit(start + j, pe_alloc);
  228. }
  229. }
  230. /*
  231. * the current bus might not own M64 window and that's all
  232. * contributed by its child buses. For the case, we needn't
  233. * pick M64 dependent PE#.
  234. */
  235. if (bitmap_empty(pe_alloc, phb->ioda.total_pe)) {
  236. kfree(pe_alloc);
  237. return IODA_INVALID_PE;
  238. }
  239. /*
  240. * Figure out the master PE and put all slave PEs to master
  241. * PE's list to form compound PE.
  242. */
  243. done:
  244. master_pe = NULL;
  245. i = -1;
  246. while ((i = find_next_bit(pe_alloc, phb->ioda.total_pe, i + 1)) <
  247. phb->ioda.total_pe) {
  248. pe = &phb->ioda.pe_array[i];
  249. pe->phb = phb;
  250. pe->pe_number = i;
  251. if (!master_pe) {
  252. pe->flags |= PNV_IODA_PE_MASTER;
  253. INIT_LIST_HEAD(&pe->slaves);
  254. master_pe = pe;
  255. } else {
  256. pe->flags |= PNV_IODA_PE_SLAVE;
  257. pe->master = master_pe;
  258. list_add_tail(&pe->list, &master_pe->slaves);
  259. }
  260. }
  261. kfree(pe_alloc);
  262. return master_pe->pe_number;
  263. }
  264. static void __init pnv_ioda_parse_m64_window(struct pnv_phb *phb)
  265. {
  266. struct pci_controller *hose = phb->hose;
  267. struct device_node *dn = hose->dn;
  268. struct resource *res;
  269. const u32 *r;
  270. u64 pci_addr;
  271. if (!firmware_has_feature(FW_FEATURE_OPALv3)) {
  272. pr_info(" Firmware too old to support M64 window\n");
  273. return;
  274. }
  275. r = of_get_property(dn, "ibm,opal-m64-window", NULL);
  276. if (!r) {
  277. pr_info(" No <ibm,opal-m64-window> on %s\n",
  278. dn->full_name);
  279. return;
  280. }
  281. /* FIXME: Support M64 for P7IOC */
  282. if (phb->type != PNV_PHB_IODA2) {
  283. pr_info(" Not support M64 window\n");
  284. return;
  285. }
  286. res = &hose->mem_resources[1];
  287. res->start = of_translate_address(dn, r + 2);
  288. res->end = res->start + of_read_number(r + 4, 2) - 1;
  289. res->flags = (IORESOURCE_MEM | IORESOURCE_MEM_64 | IORESOURCE_PREFETCH);
  290. pci_addr = of_read_number(r, 2);
  291. hose->mem_offset[1] = res->start - pci_addr;
  292. phb->ioda.m64_size = resource_size(res);
  293. phb->ioda.m64_segsize = phb->ioda.m64_size / phb->ioda.total_pe;
  294. phb->ioda.m64_base = pci_addr;
  295. /* Use last M64 BAR to cover M64 window */
  296. phb->ioda.m64_bar_idx = 15;
  297. phb->init_m64 = pnv_ioda2_init_m64;
  298. phb->alloc_m64_pe = pnv_ioda2_alloc_m64_pe;
  299. phb->pick_m64_pe = pnv_ioda2_pick_m64_pe;
  300. }
  301. static void pnv_ioda_freeze_pe(struct pnv_phb *phb, int pe_no)
  302. {
  303. struct pnv_ioda_pe *pe = &phb->ioda.pe_array[pe_no];
  304. struct pnv_ioda_pe *slave;
  305. s64 rc;
  306. /* Fetch master PE */
  307. if (pe->flags & PNV_IODA_PE_SLAVE) {
  308. pe = pe->master;
  309. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  310. pe_no = pe->pe_number;
  311. }
  312. /* Freeze master PE */
  313. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  314. pe_no,
  315. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  316. if (rc != OPAL_SUCCESS) {
  317. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  318. __func__, rc, phb->hose->global_number, pe_no);
  319. return;
  320. }
  321. /* Freeze slave PEs */
  322. if (!(pe->flags & PNV_IODA_PE_MASTER))
  323. return;
  324. list_for_each_entry(slave, &pe->slaves, list) {
  325. rc = opal_pci_eeh_freeze_set(phb->opal_id,
  326. slave->pe_number,
  327. OPAL_EEH_ACTION_SET_FREEZE_ALL);
  328. if (rc != OPAL_SUCCESS)
  329. pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
  330. __func__, rc, phb->hose->global_number,
  331. slave->pe_number);
  332. }
  333. }
  334. static int pnv_ioda_unfreeze_pe(struct pnv_phb *phb, int pe_no, int opt)
  335. {
  336. struct pnv_ioda_pe *pe, *slave;
  337. s64 rc;
  338. /* Find master PE */
  339. pe = &phb->ioda.pe_array[pe_no];
  340. if (pe->flags & PNV_IODA_PE_SLAVE) {
  341. pe = pe->master;
  342. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  343. pe_no = pe->pe_number;
  344. }
  345. /* Clear frozen state for master PE */
  346. rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe_no, opt);
  347. if (rc != OPAL_SUCCESS) {
  348. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  349. __func__, rc, opt, phb->hose->global_number, pe_no);
  350. return -EIO;
  351. }
  352. if (!(pe->flags & PNV_IODA_PE_MASTER))
  353. return 0;
  354. /* Clear frozen state for slave PEs */
  355. list_for_each_entry(slave, &pe->slaves, list) {
  356. rc = opal_pci_eeh_freeze_clear(phb->opal_id,
  357. slave->pe_number,
  358. opt);
  359. if (rc != OPAL_SUCCESS) {
  360. pr_warn("%s: Failure %lld clear %d on PHB#%x-PE#%x\n",
  361. __func__, rc, opt, phb->hose->global_number,
  362. slave->pe_number);
  363. return -EIO;
  364. }
  365. }
  366. return 0;
  367. }
  368. static int pnv_ioda_get_pe_state(struct pnv_phb *phb, int pe_no)
  369. {
  370. struct pnv_ioda_pe *slave, *pe;
  371. u8 fstate, state;
  372. __be16 pcierr;
  373. s64 rc;
  374. /* Sanity check on PE number */
  375. if (pe_no < 0 || pe_no >= phb->ioda.total_pe)
  376. return OPAL_EEH_STOPPED_PERM_UNAVAIL;
  377. /*
  378. * Fetch the master PE and the PE instance might be
  379. * not initialized yet.
  380. */
  381. pe = &phb->ioda.pe_array[pe_no];
  382. if (pe->flags & PNV_IODA_PE_SLAVE) {
  383. pe = pe->master;
  384. WARN_ON(!pe || !(pe->flags & PNV_IODA_PE_MASTER));
  385. pe_no = pe->pe_number;
  386. }
  387. /* Check the master PE */
  388. rc = opal_pci_eeh_freeze_status(phb->opal_id, pe_no,
  389. &state, &pcierr, NULL);
  390. if (rc != OPAL_SUCCESS) {
  391. pr_warn("%s: Failure %lld getting "
  392. "PHB#%x-PE#%x state\n",
  393. __func__, rc,
  394. phb->hose->global_number, pe_no);
  395. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  396. }
  397. /* Check the slave PE */
  398. if (!(pe->flags & PNV_IODA_PE_MASTER))
  399. return state;
  400. list_for_each_entry(slave, &pe->slaves, list) {
  401. rc = opal_pci_eeh_freeze_status(phb->opal_id,
  402. slave->pe_number,
  403. &fstate,
  404. &pcierr,
  405. NULL);
  406. if (rc != OPAL_SUCCESS) {
  407. pr_warn("%s: Failure %lld getting "
  408. "PHB#%x-PE#%x state\n",
  409. __func__, rc,
  410. phb->hose->global_number, slave->pe_number);
  411. return OPAL_EEH_STOPPED_TEMP_UNAVAIL;
  412. }
  413. /*
  414. * Override the result based on the ascending
  415. * priority.
  416. */
  417. if (fstate > state)
  418. state = fstate;
  419. }
  420. return state;
  421. }
  422. /* Currently those 2 are only used when MSIs are enabled, this will change
  423. * but in the meantime, we need to protect them to avoid warnings
  424. */
  425. #ifdef CONFIG_PCI_MSI
  426. static struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev)
  427. {
  428. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  429. struct pnv_phb *phb = hose->private_data;
  430. struct pci_dn *pdn = pci_get_pdn(dev);
  431. if (!pdn)
  432. return NULL;
  433. if (pdn->pe_number == IODA_INVALID_PE)
  434. return NULL;
  435. return &phb->ioda.pe_array[pdn->pe_number];
  436. }
  437. #endif /* CONFIG_PCI_MSI */
  438. static int pnv_ioda_configure_pe(struct pnv_phb *phb, struct pnv_ioda_pe *pe)
  439. {
  440. struct pci_dev *parent;
  441. uint8_t bcomp, dcomp, fcomp;
  442. long rc, rid_end, rid;
  443. /* Bus validation ? */
  444. if (pe->pbus) {
  445. int count;
  446. dcomp = OPAL_IGNORE_RID_DEVICE_NUMBER;
  447. fcomp = OPAL_IGNORE_RID_FUNCTION_NUMBER;
  448. parent = pe->pbus->self;
  449. if (pe->flags & PNV_IODA_PE_BUS_ALL)
  450. count = pe->pbus->busn_res.end - pe->pbus->busn_res.start + 1;
  451. else
  452. count = 1;
  453. switch(count) {
  454. case 1: bcomp = OpalPciBusAll; break;
  455. case 2: bcomp = OpalPciBus7Bits; break;
  456. case 4: bcomp = OpalPciBus6Bits; break;
  457. case 8: bcomp = OpalPciBus5Bits; break;
  458. case 16: bcomp = OpalPciBus4Bits; break;
  459. case 32: bcomp = OpalPciBus3Bits; break;
  460. default:
  461. pr_err("%s: Number of subordinate busses %d"
  462. " unsupported\n",
  463. pci_name(pe->pbus->self), count);
  464. /* Do an exact match only */
  465. bcomp = OpalPciBusAll;
  466. }
  467. rid_end = pe->rid + (count << 8);
  468. } else {
  469. parent = pe->pdev->bus->self;
  470. bcomp = OpalPciBusAll;
  471. dcomp = OPAL_COMPARE_RID_DEVICE_NUMBER;
  472. fcomp = OPAL_COMPARE_RID_FUNCTION_NUMBER;
  473. rid_end = pe->rid + 1;
  474. }
  475. /*
  476. * Associate PE in PELT. We need add the PE into the
  477. * corresponding PELT-V as well. Otherwise, the error
  478. * originated from the PE might contribute to other
  479. * PEs.
  480. */
  481. rc = opal_pci_set_pe(phb->opal_id, pe->pe_number, pe->rid,
  482. bcomp, dcomp, fcomp, OPAL_MAP_PE);
  483. if (rc) {
  484. pe_err(pe, "OPAL error %ld trying to setup PELT table\n", rc);
  485. return -ENXIO;
  486. }
  487. rc = opal_pci_set_peltv(phb->opal_id, pe->pe_number,
  488. pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
  489. if (rc)
  490. pe_warn(pe, "OPAL error %d adding self to PELTV\n", rc);
  491. opal_pci_eeh_freeze_clear(phb->opal_id, pe->pe_number,
  492. OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
  493. /* Add to all parents PELT-V */
  494. while (parent) {
  495. struct pci_dn *pdn = pci_get_pdn(parent);
  496. if (pdn && pdn->pe_number != IODA_INVALID_PE) {
  497. rc = opal_pci_set_peltv(phb->opal_id, pdn->pe_number,
  498. pe->pe_number, OPAL_ADD_PE_TO_DOMAIN);
  499. /* XXX What to do in case of error ? */
  500. }
  501. parent = parent->bus->self;
  502. }
  503. /* Setup reverse map */
  504. for (rid = pe->rid; rid < rid_end; rid++)
  505. phb->ioda.pe_rmap[rid] = pe->pe_number;
  506. /* Setup one MVTs on IODA1 */
  507. if (phb->type == PNV_PHB_IODA1) {
  508. pe->mve_number = pe->pe_number;
  509. rc = opal_pci_set_mve(phb->opal_id, pe->mve_number,
  510. pe->pe_number);
  511. if (rc) {
  512. pe_err(pe, "OPAL error %ld setting up MVE %d\n",
  513. rc, pe->mve_number);
  514. pe->mve_number = -1;
  515. } else {
  516. rc = opal_pci_set_mve_enable(phb->opal_id,
  517. pe->mve_number, OPAL_ENABLE_MVE);
  518. if (rc) {
  519. pe_err(pe, "OPAL error %ld enabling MVE %d\n",
  520. rc, pe->mve_number);
  521. pe->mve_number = -1;
  522. }
  523. }
  524. } else if (phb->type == PNV_PHB_IODA2)
  525. pe->mve_number = 0;
  526. return 0;
  527. }
  528. static void pnv_ioda_link_pe_by_weight(struct pnv_phb *phb,
  529. struct pnv_ioda_pe *pe)
  530. {
  531. struct pnv_ioda_pe *lpe;
  532. list_for_each_entry(lpe, &phb->ioda.pe_dma_list, dma_link) {
  533. if (lpe->dma_weight < pe->dma_weight) {
  534. list_add_tail(&pe->dma_link, &lpe->dma_link);
  535. return;
  536. }
  537. }
  538. list_add_tail(&pe->dma_link, &phb->ioda.pe_dma_list);
  539. }
  540. static unsigned int pnv_ioda_dma_weight(struct pci_dev *dev)
  541. {
  542. /* This is quite simplistic. The "base" weight of a device
  543. * is 10. 0 means no DMA is to be accounted for it.
  544. */
  545. /* If it's a bridge, no DMA */
  546. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  547. return 0;
  548. /* Reduce the weight of slow USB controllers */
  549. if (dev->class == PCI_CLASS_SERIAL_USB_UHCI ||
  550. dev->class == PCI_CLASS_SERIAL_USB_OHCI ||
  551. dev->class == PCI_CLASS_SERIAL_USB_EHCI)
  552. return 3;
  553. /* Increase the weight of RAID (includes Obsidian) */
  554. if ((dev->class >> 8) == PCI_CLASS_STORAGE_RAID)
  555. return 15;
  556. /* Default */
  557. return 10;
  558. }
  559. #if 0
  560. static struct pnv_ioda_pe *pnv_ioda_setup_dev_PE(struct pci_dev *dev)
  561. {
  562. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  563. struct pnv_phb *phb = hose->private_data;
  564. struct pci_dn *pdn = pci_get_pdn(dev);
  565. struct pnv_ioda_pe *pe;
  566. int pe_num;
  567. if (!pdn) {
  568. pr_err("%s: Device tree node not associated properly\n",
  569. pci_name(dev));
  570. return NULL;
  571. }
  572. if (pdn->pe_number != IODA_INVALID_PE)
  573. return NULL;
  574. /* PE#0 has been pre-set */
  575. if (dev->bus->number == 0)
  576. pe_num = 0;
  577. else
  578. pe_num = pnv_ioda_alloc_pe(phb);
  579. if (pe_num == IODA_INVALID_PE) {
  580. pr_warning("%s: Not enough PE# available, disabling device\n",
  581. pci_name(dev));
  582. return NULL;
  583. }
  584. /* NOTE: We get only one ref to the pci_dev for the pdn, not for the
  585. * pointer in the PE data structure, both should be destroyed at the
  586. * same time. However, this needs to be looked at more closely again
  587. * once we actually start removing things (Hotplug, SR-IOV, ...)
  588. *
  589. * At some point we want to remove the PDN completely anyways
  590. */
  591. pe = &phb->ioda.pe_array[pe_num];
  592. pci_dev_get(dev);
  593. pdn->pcidev = dev;
  594. pdn->pe_number = pe_num;
  595. pe->pdev = dev;
  596. pe->pbus = NULL;
  597. pe->tce32_seg = -1;
  598. pe->mve_number = -1;
  599. pe->rid = dev->bus->number << 8 | pdn->devfn;
  600. pe_info(pe, "Associated device to PE\n");
  601. if (pnv_ioda_configure_pe(phb, pe)) {
  602. /* XXX What do we do here ? */
  603. if (pe_num)
  604. pnv_ioda_free_pe(phb, pe_num);
  605. pdn->pe_number = IODA_INVALID_PE;
  606. pe->pdev = NULL;
  607. pci_dev_put(dev);
  608. return NULL;
  609. }
  610. /* Assign a DMA weight to the device */
  611. pe->dma_weight = pnv_ioda_dma_weight(dev);
  612. if (pe->dma_weight != 0) {
  613. phb->ioda.dma_weight += pe->dma_weight;
  614. phb->ioda.dma_pe_count++;
  615. }
  616. /* Link the PE */
  617. pnv_ioda_link_pe_by_weight(phb, pe);
  618. return pe;
  619. }
  620. #endif /* Useful for SRIOV case */
  621. static void pnv_ioda_setup_same_PE(struct pci_bus *bus, struct pnv_ioda_pe *pe)
  622. {
  623. struct pci_dev *dev;
  624. list_for_each_entry(dev, &bus->devices, bus_list) {
  625. struct pci_dn *pdn = pci_get_pdn(dev);
  626. if (pdn == NULL) {
  627. pr_warn("%s: No device node associated with device !\n",
  628. pci_name(dev));
  629. continue;
  630. }
  631. pdn->pcidev = dev;
  632. pdn->pe_number = pe->pe_number;
  633. pe->dma_weight += pnv_ioda_dma_weight(dev);
  634. if ((pe->flags & PNV_IODA_PE_BUS_ALL) && dev->subordinate)
  635. pnv_ioda_setup_same_PE(dev->subordinate, pe);
  636. }
  637. }
  638. /*
  639. * There're 2 types of PCI bus sensitive PEs: One that is compromised of
  640. * single PCI bus. Another one that contains the primary PCI bus and its
  641. * subordinate PCI devices and buses. The second type of PE is normally
  642. * orgiriated by PCIe-to-PCI bridge or PLX switch downstream ports.
  643. */
  644. static void pnv_ioda_setup_bus_PE(struct pci_bus *bus, int all)
  645. {
  646. struct pci_controller *hose = pci_bus_to_host(bus);
  647. struct pnv_phb *phb = hose->private_data;
  648. struct pnv_ioda_pe *pe;
  649. int pe_num = IODA_INVALID_PE;
  650. /* Check if PE is determined by M64 */
  651. if (phb->pick_m64_pe)
  652. pe_num = phb->pick_m64_pe(phb, bus, all);
  653. /* The PE number isn't pinned by M64 */
  654. if (pe_num == IODA_INVALID_PE)
  655. pe_num = pnv_ioda_alloc_pe(phb);
  656. if (pe_num == IODA_INVALID_PE) {
  657. pr_warning("%s: Not enough PE# available for PCI bus %04x:%02x\n",
  658. __func__, pci_domain_nr(bus), bus->number);
  659. return;
  660. }
  661. pe = &phb->ioda.pe_array[pe_num];
  662. pe->flags |= (all ? PNV_IODA_PE_BUS_ALL : PNV_IODA_PE_BUS);
  663. pe->pbus = bus;
  664. pe->pdev = NULL;
  665. pe->tce32_seg = -1;
  666. pe->mve_number = -1;
  667. pe->rid = bus->busn_res.start << 8;
  668. pe->dma_weight = 0;
  669. if (all)
  670. pe_info(pe, "Secondary bus %d..%d associated with PE#%d\n",
  671. bus->busn_res.start, bus->busn_res.end, pe_num);
  672. else
  673. pe_info(pe, "Secondary bus %d associated with PE#%d\n",
  674. bus->busn_res.start, pe_num);
  675. if (pnv_ioda_configure_pe(phb, pe)) {
  676. /* XXX What do we do here ? */
  677. if (pe_num)
  678. pnv_ioda_free_pe(phb, pe_num);
  679. pe->pbus = NULL;
  680. return;
  681. }
  682. /* Associate it with all child devices */
  683. pnv_ioda_setup_same_PE(bus, pe);
  684. /* Put PE to the list */
  685. list_add_tail(&pe->list, &phb->ioda.pe_list);
  686. /* Account for one DMA PE if at least one DMA capable device exist
  687. * below the bridge
  688. */
  689. if (pe->dma_weight != 0) {
  690. phb->ioda.dma_weight += pe->dma_weight;
  691. phb->ioda.dma_pe_count++;
  692. }
  693. /* Link the PE */
  694. pnv_ioda_link_pe_by_weight(phb, pe);
  695. }
  696. static void pnv_ioda_setup_PEs(struct pci_bus *bus)
  697. {
  698. struct pci_dev *dev;
  699. pnv_ioda_setup_bus_PE(bus, 0);
  700. list_for_each_entry(dev, &bus->devices, bus_list) {
  701. if (dev->subordinate) {
  702. if (pci_pcie_type(dev) == PCI_EXP_TYPE_PCI_BRIDGE)
  703. pnv_ioda_setup_bus_PE(dev->subordinate, 1);
  704. else
  705. pnv_ioda_setup_PEs(dev->subordinate);
  706. }
  707. }
  708. }
  709. /*
  710. * Configure PEs so that the downstream PCI buses and devices
  711. * could have their associated PE#. Unfortunately, we didn't
  712. * figure out the way to identify the PLX bridge yet. So we
  713. * simply put the PCI bus and the subordinate behind the root
  714. * port to PE# here. The game rule here is expected to be changed
  715. * as soon as we can detected PLX bridge correctly.
  716. */
  717. static void pnv_pci_ioda_setup_PEs(void)
  718. {
  719. struct pci_controller *hose, *tmp;
  720. struct pnv_phb *phb;
  721. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  722. phb = hose->private_data;
  723. /* M64 layout might affect PE allocation */
  724. if (phb->alloc_m64_pe)
  725. phb->alloc_m64_pe(phb);
  726. pnv_ioda_setup_PEs(hose->bus);
  727. }
  728. }
  729. static void pnv_pci_ioda_dma_dev_setup(struct pnv_phb *phb, struct pci_dev *pdev)
  730. {
  731. struct pci_dn *pdn = pci_get_pdn(pdev);
  732. struct pnv_ioda_pe *pe;
  733. /*
  734. * The function can be called while the PE#
  735. * hasn't been assigned. Do nothing for the
  736. * case.
  737. */
  738. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  739. return;
  740. pe = &phb->ioda.pe_array[pdn->pe_number];
  741. WARN_ON(get_dma_ops(&pdev->dev) != &dma_iommu_ops);
  742. set_iommu_table_base_and_group(&pdev->dev, &pe->tce32_table);
  743. }
  744. static int pnv_pci_ioda_dma_set_mask(struct pnv_phb *phb,
  745. struct pci_dev *pdev, u64 dma_mask)
  746. {
  747. struct pci_dn *pdn = pci_get_pdn(pdev);
  748. struct pnv_ioda_pe *pe;
  749. uint64_t top;
  750. bool bypass = false;
  751. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  752. return -ENODEV;;
  753. pe = &phb->ioda.pe_array[pdn->pe_number];
  754. if (pe->tce_bypass_enabled) {
  755. top = pe->tce_bypass_base + memblock_end_of_DRAM() - 1;
  756. bypass = (dma_mask >= top);
  757. }
  758. if (bypass) {
  759. dev_info(&pdev->dev, "Using 64-bit DMA iommu bypass\n");
  760. set_dma_ops(&pdev->dev, &dma_direct_ops);
  761. set_dma_offset(&pdev->dev, pe->tce_bypass_base);
  762. } else {
  763. dev_info(&pdev->dev, "Using 32-bit DMA via iommu\n");
  764. set_dma_ops(&pdev->dev, &dma_iommu_ops);
  765. set_iommu_table_base(&pdev->dev, &pe->tce32_table);
  766. }
  767. *pdev->dev.dma_mask = dma_mask;
  768. return 0;
  769. }
  770. static u64 pnv_pci_ioda_dma_get_required_mask(struct pnv_phb *phb,
  771. struct pci_dev *pdev)
  772. {
  773. struct pci_dn *pdn = pci_get_pdn(pdev);
  774. struct pnv_ioda_pe *pe;
  775. u64 end, mask;
  776. if (WARN_ON(!pdn || pdn->pe_number == IODA_INVALID_PE))
  777. return 0;
  778. pe = &phb->ioda.pe_array[pdn->pe_number];
  779. if (!pe->tce_bypass_enabled)
  780. return __dma_get_required_mask(&pdev->dev);
  781. end = pe->tce_bypass_base + memblock_end_of_DRAM();
  782. mask = 1ULL << (fls64(end) - 1);
  783. mask += mask - 1;
  784. return mask;
  785. }
  786. static void pnv_ioda_setup_bus_dma(struct pnv_ioda_pe *pe,
  787. struct pci_bus *bus,
  788. bool add_to_iommu_group)
  789. {
  790. struct pci_dev *dev;
  791. list_for_each_entry(dev, &bus->devices, bus_list) {
  792. if (add_to_iommu_group)
  793. set_iommu_table_base_and_group(&dev->dev,
  794. &pe->tce32_table);
  795. else
  796. set_iommu_table_base(&dev->dev, &pe->tce32_table);
  797. if (dev->subordinate)
  798. pnv_ioda_setup_bus_dma(pe, dev->subordinate,
  799. add_to_iommu_group);
  800. }
  801. }
  802. static void pnv_pci_ioda1_tce_invalidate(struct pnv_ioda_pe *pe,
  803. struct iommu_table *tbl,
  804. __be64 *startp, __be64 *endp, bool rm)
  805. {
  806. __be64 __iomem *invalidate = rm ?
  807. (__be64 __iomem *)pe->tce_inval_reg_phys :
  808. (__be64 __iomem *)tbl->it_index;
  809. unsigned long start, end, inc;
  810. const unsigned shift = tbl->it_page_shift;
  811. start = __pa(startp);
  812. end = __pa(endp);
  813. /* BML uses this case for p6/p7/galaxy2: Shift addr and put in node */
  814. if (tbl->it_busno) {
  815. start <<= shift;
  816. end <<= shift;
  817. inc = 128ull << shift;
  818. start |= tbl->it_busno;
  819. end |= tbl->it_busno;
  820. } else if (tbl->it_type & TCE_PCI_SWINV_PAIR) {
  821. /* p7ioc-style invalidation, 2 TCEs per write */
  822. start |= (1ull << 63);
  823. end |= (1ull << 63);
  824. inc = 16;
  825. } else {
  826. /* Default (older HW) */
  827. inc = 128;
  828. }
  829. end |= inc - 1; /* round up end to be different than start */
  830. mb(); /* Ensure above stores are visible */
  831. while (start <= end) {
  832. if (rm)
  833. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  834. else
  835. __raw_writeq(cpu_to_be64(start), invalidate);
  836. start += inc;
  837. }
  838. /*
  839. * The iommu layer will do another mb() for us on build()
  840. * and we don't care on free()
  841. */
  842. }
  843. static void pnv_pci_ioda2_tce_invalidate(struct pnv_ioda_pe *pe,
  844. struct iommu_table *tbl,
  845. __be64 *startp, __be64 *endp, bool rm)
  846. {
  847. unsigned long start, end, inc;
  848. __be64 __iomem *invalidate = rm ?
  849. (__be64 __iomem *)pe->tce_inval_reg_phys :
  850. (__be64 __iomem *)tbl->it_index;
  851. const unsigned shift = tbl->it_page_shift;
  852. /* We'll invalidate DMA address in PE scope */
  853. start = 0x2ull << 60;
  854. start |= (pe->pe_number & 0xFF);
  855. end = start;
  856. /* Figure out the start, end and step */
  857. inc = tbl->it_offset + (((u64)startp - tbl->it_base) / sizeof(u64));
  858. start |= (inc << shift);
  859. inc = tbl->it_offset + (((u64)endp - tbl->it_base) / sizeof(u64));
  860. end |= (inc << shift);
  861. inc = (0x1ull << shift);
  862. mb();
  863. while (start <= end) {
  864. if (rm)
  865. __raw_rm_writeq(cpu_to_be64(start), invalidate);
  866. else
  867. __raw_writeq(cpu_to_be64(start), invalidate);
  868. start += inc;
  869. }
  870. }
  871. void pnv_pci_ioda_tce_invalidate(struct iommu_table *tbl,
  872. __be64 *startp, __be64 *endp, bool rm)
  873. {
  874. struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
  875. tce32_table);
  876. struct pnv_phb *phb = pe->phb;
  877. if (phb->type == PNV_PHB_IODA1)
  878. pnv_pci_ioda1_tce_invalidate(pe, tbl, startp, endp, rm);
  879. else
  880. pnv_pci_ioda2_tce_invalidate(pe, tbl, startp, endp, rm);
  881. }
  882. static void pnv_pci_ioda_setup_dma_pe(struct pnv_phb *phb,
  883. struct pnv_ioda_pe *pe, unsigned int base,
  884. unsigned int segs)
  885. {
  886. struct page *tce_mem = NULL;
  887. const __be64 *swinvp;
  888. struct iommu_table *tbl;
  889. unsigned int i;
  890. int64_t rc;
  891. void *addr;
  892. /* 256M DMA window, 4K TCE pages, 8 bytes TCE */
  893. #define TCE32_TABLE_SIZE ((0x10000000 / 0x1000) * 8)
  894. /* XXX FIXME: Handle 64-bit only DMA devices */
  895. /* XXX FIXME: Provide 64-bit DMA facilities & non-4K TCE tables etc.. */
  896. /* XXX FIXME: Allocate multi-level tables on PHB3 */
  897. /* We shouldn't already have a 32-bit DMA associated */
  898. if (WARN_ON(pe->tce32_seg >= 0))
  899. return;
  900. /* Grab a 32-bit TCE table */
  901. pe->tce32_seg = base;
  902. pe_info(pe, " Setting up 32-bit TCE table at %08x..%08x\n",
  903. (base << 28), ((base + segs) << 28) - 1);
  904. /* XXX Currently, we allocate one big contiguous table for the
  905. * TCEs. We only really need one chunk per 256M of TCE space
  906. * (ie per segment) but that's an optimization for later, it
  907. * requires some added smarts with our get/put_tce implementation
  908. */
  909. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  910. get_order(TCE32_TABLE_SIZE * segs));
  911. if (!tce_mem) {
  912. pe_err(pe, " Failed to allocate a 32-bit TCE memory\n");
  913. goto fail;
  914. }
  915. addr = page_address(tce_mem);
  916. memset(addr, 0, TCE32_TABLE_SIZE * segs);
  917. /* Configure HW */
  918. for (i = 0; i < segs; i++) {
  919. rc = opal_pci_map_pe_dma_window(phb->opal_id,
  920. pe->pe_number,
  921. base + i, 1,
  922. __pa(addr) + TCE32_TABLE_SIZE * i,
  923. TCE32_TABLE_SIZE, 0x1000);
  924. if (rc) {
  925. pe_err(pe, " Failed to configure 32-bit TCE table,"
  926. " err %ld\n", rc);
  927. goto fail;
  928. }
  929. }
  930. /* Setup linux iommu table */
  931. tbl = &pe->tce32_table;
  932. pnv_pci_setup_iommu_table(tbl, addr, TCE32_TABLE_SIZE * segs,
  933. base << 28, IOMMU_PAGE_SHIFT_4K);
  934. /* OPAL variant of P7IOC SW invalidated TCEs */
  935. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  936. if (swinvp) {
  937. /* We need a couple more fields -- an address and a data
  938. * to or. Since the bus is only printed out on table free
  939. * errors, and on the first pass the data will be a relative
  940. * bus number, print that out instead.
  941. */
  942. pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
  943. tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
  944. 8);
  945. tbl->it_type |= (TCE_PCI_SWINV_CREATE |
  946. TCE_PCI_SWINV_FREE |
  947. TCE_PCI_SWINV_PAIR);
  948. }
  949. iommu_init_table(tbl, phb->hose->node);
  950. iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
  951. if (pe->pdev)
  952. set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
  953. else
  954. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  955. return;
  956. fail:
  957. /* XXX Failure: Try to fallback to 64-bit only ? */
  958. if (pe->tce32_seg >= 0)
  959. pe->tce32_seg = -1;
  960. if (tce_mem)
  961. __free_pages(tce_mem, get_order(TCE32_TABLE_SIZE * segs));
  962. }
  963. static void pnv_pci_ioda2_set_bypass(struct iommu_table *tbl, bool enable)
  964. {
  965. struct pnv_ioda_pe *pe = container_of(tbl, struct pnv_ioda_pe,
  966. tce32_table);
  967. uint16_t window_id = (pe->pe_number << 1 ) + 1;
  968. int64_t rc;
  969. pe_info(pe, "%sabling 64-bit DMA bypass\n", enable ? "En" : "Dis");
  970. if (enable) {
  971. phys_addr_t top = memblock_end_of_DRAM();
  972. top = roundup_pow_of_two(top);
  973. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  974. pe->pe_number,
  975. window_id,
  976. pe->tce_bypass_base,
  977. top);
  978. } else {
  979. rc = opal_pci_map_pe_dma_window_real(pe->phb->opal_id,
  980. pe->pe_number,
  981. window_id,
  982. pe->tce_bypass_base,
  983. 0);
  984. /*
  985. * EEH needs the mapping between IOMMU table and group
  986. * of those VFIO/KVM pass-through devices. We can postpone
  987. * resetting DMA ops until the DMA mask is configured in
  988. * host side.
  989. */
  990. if (pe->pdev)
  991. set_iommu_table_base(&pe->pdev->dev, tbl);
  992. else
  993. pnv_ioda_setup_bus_dma(pe, pe->pbus, false);
  994. }
  995. if (rc)
  996. pe_err(pe, "OPAL error %lld configuring bypass window\n", rc);
  997. else
  998. pe->tce_bypass_enabled = enable;
  999. }
  1000. static void pnv_pci_ioda2_setup_bypass_pe(struct pnv_phb *phb,
  1001. struct pnv_ioda_pe *pe)
  1002. {
  1003. /* TVE #1 is selected by PCI address bit 59 */
  1004. pe->tce_bypass_base = 1ull << 59;
  1005. /* Install set_bypass callback for VFIO */
  1006. pe->tce32_table.set_bypass = pnv_pci_ioda2_set_bypass;
  1007. /* Enable bypass by default */
  1008. pnv_pci_ioda2_set_bypass(&pe->tce32_table, true);
  1009. }
  1010. static void pnv_pci_ioda2_setup_dma_pe(struct pnv_phb *phb,
  1011. struct pnv_ioda_pe *pe)
  1012. {
  1013. struct page *tce_mem = NULL;
  1014. void *addr;
  1015. const __be64 *swinvp;
  1016. struct iommu_table *tbl;
  1017. unsigned int tce_table_size, end;
  1018. int64_t rc;
  1019. /* We shouldn't already have a 32-bit DMA associated */
  1020. if (WARN_ON(pe->tce32_seg >= 0))
  1021. return;
  1022. /* The PE will reserve all possible 32-bits space */
  1023. pe->tce32_seg = 0;
  1024. end = (1 << ilog2(phb->ioda.m32_pci_base));
  1025. tce_table_size = (end / 0x1000) * 8;
  1026. pe_info(pe, "Setting up 32-bit TCE table at 0..%08x\n",
  1027. end);
  1028. /* Allocate TCE table */
  1029. tce_mem = alloc_pages_node(phb->hose->node, GFP_KERNEL,
  1030. get_order(tce_table_size));
  1031. if (!tce_mem) {
  1032. pe_err(pe, "Failed to allocate a 32-bit TCE memory\n");
  1033. goto fail;
  1034. }
  1035. addr = page_address(tce_mem);
  1036. memset(addr, 0, tce_table_size);
  1037. /*
  1038. * Map TCE table through TVT. The TVE index is the PE number
  1039. * shifted by 1 bit for 32-bits DMA space.
  1040. */
  1041. rc = opal_pci_map_pe_dma_window(phb->opal_id, pe->pe_number,
  1042. pe->pe_number << 1, 1, __pa(addr),
  1043. tce_table_size, 0x1000);
  1044. if (rc) {
  1045. pe_err(pe, "Failed to configure 32-bit TCE table,"
  1046. " err %ld\n", rc);
  1047. goto fail;
  1048. }
  1049. /* Setup linux iommu table */
  1050. tbl = &pe->tce32_table;
  1051. pnv_pci_setup_iommu_table(tbl, addr, tce_table_size, 0,
  1052. IOMMU_PAGE_SHIFT_4K);
  1053. /* OPAL variant of PHB3 invalidated TCEs */
  1054. swinvp = of_get_property(phb->hose->dn, "ibm,opal-tce-kill", NULL);
  1055. if (swinvp) {
  1056. /* We need a couple more fields -- an address and a data
  1057. * to or. Since the bus is only printed out on table free
  1058. * errors, and on the first pass the data will be a relative
  1059. * bus number, print that out instead.
  1060. */
  1061. pe->tce_inval_reg_phys = be64_to_cpup(swinvp);
  1062. tbl->it_index = (unsigned long)ioremap(pe->tce_inval_reg_phys,
  1063. 8);
  1064. tbl->it_type |= (TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE);
  1065. }
  1066. iommu_init_table(tbl, phb->hose->node);
  1067. iommu_register_group(tbl, phb->hose->global_number, pe->pe_number);
  1068. if (pe->pdev)
  1069. set_iommu_table_base_and_group(&pe->pdev->dev, tbl);
  1070. else
  1071. pnv_ioda_setup_bus_dma(pe, pe->pbus, true);
  1072. /* Also create a bypass window */
  1073. pnv_pci_ioda2_setup_bypass_pe(phb, pe);
  1074. return;
  1075. fail:
  1076. if (pe->tce32_seg >= 0)
  1077. pe->tce32_seg = -1;
  1078. if (tce_mem)
  1079. __free_pages(tce_mem, get_order(tce_table_size));
  1080. }
  1081. static void pnv_ioda_setup_dma(struct pnv_phb *phb)
  1082. {
  1083. struct pci_controller *hose = phb->hose;
  1084. unsigned int residual, remaining, segs, tw, base;
  1085. struct pnv_ioda_pe *pe;
  1086. /* If we have more PE# than segments available, hand out one
  1087. * per PE until we run out and let the rest fail. If not,
  1088. * then we assign at least one segment per PE, plus more based
  1089. * on the amount of devices under that PE
  1090. */
  1091. if (phb->ioda.dma_pe_count > phb->ioda.tce32_count)
  1092. residual = 0;
  1093. else
  1094. residual = phb->ioda.tce32_count -
  1095. phb->ioda.dma_pe_count;
  1096. pr_info("PCI: Domain %04x has %ld available 32-bit DMA segments\n",
  1097. hose->global_number, phb->ioda.tce32_count);
  1098. pr_info("PCI: %d PE# for a total weight of %d\n",
  1099. phb->ioda.dma_pe_count, phb->ioda.dma_weight);
  1100. /* Walk our PE list and configure their DMA segments, hand them
  1101. * out one base segment plus any residual segments based on
  1102. * weight
  1103. */
  1104. remaining = phb->ioda.tce32_count;
  1105. tw = phb->ioda.dma_weight;
  1106. base = 0;
  1107. list_for_each_entry(pe, &phb->ioda.pe_dma_list, dma_link) {
  1108. if (!pe->dma_weight)
  1109. continue;
  1110. if (!remaining) {
  1111. pe_warn(pe, "No DMA32 resources available\n");
  1112. continue;
  1113. }
  1114. segs = 1;
  1115. if (residual) {
  1116. segs += ((pe->dma_weight * residual) + (tw / 2)) / tw;
  1117. if (segs > remaining)
  1118. segs = remaining;
  1119. }
  1120. /*
  1121. * For IODA2 compliant PHB3, we needn't care about the weight.
  1122. * The all available 32-bits DMA space will be assigned to
  1123. * the specific PE.
  1124. */
  1125. if (phb->type == PNV_PHB_IODA1) {
  1126. pe_info(pe, "DMA weight %d, assigned %d DMA32 segments\n",
  1127. pe->dma_weight, segs);
  1128. pnv_pci_ioda_setup_dma_pe(phb, pe, base, segs);
  1129. } else {
  1130. pe_info(pe, "Assign DMA32 space\n");
  1131. segs = 0;
  1132. pnv_pci_ioda2_setup_dma_pe(phb, pe);
  1133. }
  1134. remaining -= segs;
  1135. base += segs;
  1136. }
  1137. }
  1138. #ifdef CONFIG_PCI_MSI
  1139. static void pnv_ioda2_msi_eoi(struct irq_data *d)
  1140. {
  1141. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  1142. struct irq_chip *chip = irq_data_get_irq_chip(d);
  1143. struct pnv_phb *phb = container_of(chip, struct pnv_phb,
  1144. ioda.irq_chip);
  1145. int64_t rc;
  1146. rc = opal_pci_msi_eoi(phb->opal_id, hw_irq);
  1147. WARN_ON_ONCE(rc);
  1148. icp_native_eoi(d);
  1149. }
  1150. static void set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq)
  1151. {
  1152. struct irq_data *idata;
  1153. struct irq_chip *ichip;
  1154. if (phb->type != PNV_PHB_IODA2)
  1155. return;
  1156. if (!phb->ioda.irq_chip_init) {
  1157. /*
  1158. * First time we setup an MSI IRQ, we need to setup the
  1159. * corresponding IRQ chip to route correctly.
  1160. */
  1161. idata = irq_get_irq_data(virq);
  1162. ichip = irq_data_get_irq_chip(idata);
  1163. phb->ioda.irq_chip_init = 1;
  1164. phb->ioda.irq_chip = *ichip;
  1165. phb->ioda.irq_chip.irq_eoi = pnv_ioda2_msi_eoi;
  1166. }
  1167. irq_set_chip(virq, &phb->ioda.irq_chip);
  1168. }
  1169. #ifdef CONFIG_CXL_BASE
  1170. struct device_node *pnv_pci_to_phb_node(struct pci_dev *dev)
  1171. {
  1172. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1173. return hose->dn;
  1174. }
  1175. EXPORT_SYMBOL(pnv_pci_to_phb_node);
  1176. int pnv_phb_to_cxl(struct pci_dev *dev)
  1177. {
  1178. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1179. struct pnv_phb *phb = hose->private_data;
  1180. struct pnv_ioda_pe *pe;
  1181. int rc;
  1182. pe = pnv_ioda_get_pe(dev);
  1183. if (!pe)
  1184. return -ENODEV;
  1185. pe_info(pe, "Switching PHB to CXL\n");
  1186. rc = opal_pci_set_phb_cxl_mode(phb->opal_id, 1, pe->pe_number);
  1187. if (rc)
  1188. dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
  1189. return rc;
  1190. }
  1191. EXPORT_SYMBOL(pnv_phb_to_cxl);
  1192. /* Find PHB for cxl dev and allocate MSI hwirqs?
  1193. * Returns the absolute hardware IRQ number
  1194. */
  1195. int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
  1196. {
  1197. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1198. struct pnv_phb *phb = hose->private_data;
  1199. int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
  1200. if (hwirq < 0) {
  1201. dev_warn(&dev->dev, "Failed to find a free MSI\n");
  1202. return -ENOSPC;
  1203. }
  1204. return phb->msi_base + hwirq;
  1205. }
  1206. EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
  1207. void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
  1208. {
  1209. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1210. struct pnv_phb *phb = hose->private_data;
  1211. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
  1212. }
  1213. EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
  1214. void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
  1215. struct pci_dev *dev)
  1216. {
  1217. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1218. struct pnv_phb *phb = hose->private_data;
  1219. int i, hwirq;
  1220. for (i = 1; i < CXL_IRQ_RANGES; i++) {
  1221. if (!irqs->range[i])
  1222. continue;
  1223. pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
  1224. i, irqs->offset[i],
  1225. irqs->range[i]);
  1226. hwirq = irqs->offset[i] - phb->msi_base;
  1227. msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
  1228. irqs->range[i]);
  1229. }
  1230. }
  1231. EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
  1232. int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
  1233. struct pci_dev *dev, int num)
  1234. {
  1235. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1236. struct pnv_phb *phb = hose->private_data;
  1237. int i, hwirq, try;
  1238. memset(irqs, 0, sizeof(struct cxl_irq_ranges));
  1239. /* 0 is reserved for the multiplexed PSL DSI interrupt */
  1240. for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
  1241. try = num;
  1242. while (try) {
  1243. hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
  1244. if (hwirq >= 0)
  1245. break;
  1246. try /= 2;
  1247. }
  1248. if (!try)
  1249. goto fail;
  1250. irqs->offset[i] = phb->msi_base + hwirq;
  1251. irqs->range[i] = try;
  1252. pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
  1253. i, irqs->offset[i], irqs->range[i]);
  1254. num -= try;
  1255. }
  1256. if (num)
  1257. goto fail;
  1258. return 0;
  1259. fail:
  1260. pnv_cxl_release_hwirq_ranges(irqs, dev);
  1261. return -ENOSPC;
  1262. }
  1263. EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
  1264. int pnv_cxl_get_irq_count(struct pci_dev *dev)
  1265. {
  1266. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1267. struct pnv_phb *phb = hose->private_data;
  1268. return phb->msi_bmp.irq_count;
  1269. }
  1270. EXPORT_SYMBOL(pnv_cxl_get_irq_count);
  1271. int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
  1272. unsigned int virq)
  1273. {
  1274. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1275. struct pnv_phb *phb = hose->private_data;
  1276. unsigned int xive_num = hwirq - phb->msi_base;
  1277. struct pnv_ioda_pe *pe;
  1278. int rc;
  1279. if (!(pe = pnv_ioda_get_pe(dev)))
  1280. return -ENODEV;
  1281. /* Assign XIVE to PE */
  1282. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  1283. if (rc) {
  1284. pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
  1285. "hwirq 0x%x XIVE 0x%x PE\n",
  1286. pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
  1287. return -EIO;
  1288. }
  1289. set_msi_irq_chip(phb, virq);
  1290. return 0;
  1291. }
  1292. EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
  1293. #endif
  1294. static int pnv_pci_ioda_msi_setup(struct pnv_phb *phb, struct pci_dev *dev,
  1295. unsigned int hwirq, unsigned int virq,
  1296. unsigned int is_64, struct msi_msg *msg)
  1297. {
  1298. struct pnv_ioda_pe *pe = pnv_ioda_get_pe(dev);
  1299. struct pci_dn *pdn = pci_get_pdn(dev);
  1300. unsigned int xive_num = hwirq - phb->msi_base;
  1301. __be32 data;
  1302. int rc;
  1303. /* No PE assigned ? bail out ... no MSI for you ! */
  1304. if (pe == NULL)
  1305. return -ENXIO;
  1306. /* Check if we have an MVE */
  1307. if (pe->mve_number < 0)
  1308. return -ENXIO;
  1309. /* Force 32-bit MSI on some broken devices */
  1310. if (pdn && pdn->force_32bit_msi)
  1311. is_64 = 0;
  1312. /* Assign XIVE to PE */
  1313. rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
  1314. if (rc) {
  1315. pr_warn("%s: OPAL error %d setting XIVE %d PE\n",
  1316. pci_name(dev), rc, xive_num);
  1317. return -EIO;
  1318. }
  1319. if (is_64) {
  1320. __be64 addr64;
  1321. rc = opal_get_msi_64(phb->opal_id, pe->mve_number, xive_num, 1,
  1322. &addr64, &data);
  1323. if (rc) {
  1324. pr_warn("%s: OPAL error %d getting 64-bit MSI data\n",
  1325. pci_name(dev), rc);
  1326. return -EIO;
  1327. }
  1328. msg->address_hi = be64_to_cpu(addr64) >> 32;
  1329. msg->address_lo = be64_to_cpu(addr64) & 0xfffffffful;
  1330. } else {
  1331. __be32 addr32;
  1332. rc = opal_get_msi_32(phb->opal_id, pe->mve_number, xive_num, 1,
  1333. &addr32, &data);
  1334. if (rc) {
  1335. pr_warn("%s: OPAL error %d getting 32-bit MSI data\n",
  1336. pci_name(dev), rc);
  1337. return -EIO;
  1338. }
  1339. msg->address_hi = 0;
  1340. msg->address_lo = be32_to_cpu(addr32);
  1341. }
  1342. msg->data = be32_to_cpu(data);
  1343. set_msi_irq_chip(phb, virq);
  1344. pr_devel("%s: %s-bit MSI on hwirq %x (xive #%d),"
  1345. " address=%x_%08x data=%x PE# %d\n",
  1346. pci_name(dev), is_64 ? "64" : "32", hwirq, xive_num,
  1347. msg->address_hi, msg->address_lo, data, pe->pe_number);
  1348. return 0;
  1349. }
  1350. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb)
  1351. {
  1352. unsigned int count;
  1353. const __be32 *prop = of_get_property(phb->hose->dn,
  1354. "ibm,opal-msi-ranges", NULL);
  1355. if (!prop) {
  1356. /* BML Fallback */
  1357. prop = of_get_property(phb->hose->dn, "msi-ranges", NULL);
  1358. }
  1359. if (!prop)
  1360. return;
  1361. phb->msi_base = be32_to_cpup(prop);
  1362. count = be32_to_cpup(prop + 1);
  1363. if (msi_bitmap_alloc(&phb->msi_bmp, count, phb->hose->dn)) {
  1364. pr_err("PCI %d: Failed to allocate MSI bitmap !\n",
  1365. phb->hose->global_number);
  1366. return;
  1367. }
  1368. phb->msi_setup = pnv_pci_ioda_msi_setup;
  1369. phb->msi32_support = 1;
  1370. pr_info(" Allocated bitmap for %d MSIs (base IRQ 0x%x)\n",
  1371. count, phb->msi_base);
  1372. }
  1373. #else
  1374. static void pnv_pci_init_ioda_msis(struct pnv_phb *phb) { }
  1375. #endif /* CONFIG_PCI_MSI */
  1376. /*
  1377. * This function is supposed to be called on basis of PE from top
  1378. * to bottom style. So the the I/O or MMIO segment assigned to
  1379. * parent PE could be overrided by its child PEs if necessary.
  1380. */
  1381. static void pnv_ioda_setup_pe_seg(struct pci_controller *hose,
  1382. struct pnv_ioda_pe *pe)
  1383. {
  1384. struct pnv_phb *phb = hose->private_data;
  1385. struct pci_bus_region region;
  1386. struct resource *res;
  1387. int i, index;
  1388. int rc;
  1389. /*
  1390. * NOTE: We only care PCI bus based PE for now. For PCI
  1391. * device based PE, for example SRIOV sensitive VF should
  1392. * be figured out later.
  1393. */
  1394. BUG_ON(!(pe->flags & (PNV_IODA_PE_BUS | PNV_IODA_PE_BUS_ALL)));
  1395. pci_bus_for_each_resource(pe->pbus, res, i) {
  1396. if (!res || !res->flags ||
  1397. res->start > res->end)
  1398. continue;
  1399. if (res->flags & IORESOURCE_IO) {
  1400. region.start = res->start - phb->ioda.io_pci_base;
  1401. region.end = res->end - phb->ioda.io_pci_base;
  1402. index = region.start / phb->ioda.io_segsize;
  1403. while (index < phb->ioda.total_pe &&
  1404. region.start <= region.end) {
  1405. phb->ioda.io_segmap[index] = pe->pe_number;
  1406. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  1407. pe->pe_number, OPAL_IO_WINDOW_TYPE, 0, index);
  1408. if (rc != OPAL_SUCCESS) {
  1409. pr_err("%s: OPAL error %d when mapping IO "
  1410. "segment #%d to PE#%d\n",
  1411. __func__, rc, index, pe->pe_number);
  1412. break;
  1413. }
  1414. region.start += phb->ioda.io_segsize;
  1415. index++;
  1416. }
  1417. } else if (res->flags & IORESOURCE_MEM) {
  1418. region.start = res->start -
  1419. hose->mem_offset[0] -
  1420. phb->ioda.m32_pci_base;
  1421. region.end = res->end -
  1422. hose->mem_offset[0] -
  1423. phb->ioda.m32_pci_base;
  1424. index = region.start / phb->ioda.m32_segsize;
  1425. while (index < phb->ioda.total_pe &&
  1426. region.start <= region.end) {
  1427. phb->ioda.m32_segmap[index] = pe->pe_number;
  1428. rc = opal_pci_map_pe_mmio_window(phb->opal_id,
  1429. pe->pe_number, OPAL_M32_WINDOW_TYPE, 0, index);
  1430. if (rc != OPAL_SUCCESS) {
  1431. pr_err("%s: OPAL error %d when mapping M32 "
  1432. "segment#%d to PE#%d",
  1433. __func__, rc, index, pe->pe_number);
  1434. break;
  1435. }
  1436. region.start += phb->ioda.m32_segsize;
  1437. index++;
  1438. }
  1439. }
  1440. }
  1441. }
  1442. static void pnv_pci_ioda_setup_seg(void)
  1443. {
  1444. struct pci_controller *tmp, *hose;
  1445. struct pnv_phb *phb;
  1446. struct pnv_ioda_pe *pe;
  1447. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1448. phb = hose->private_data;
  1449. list_for_each_entry(pe, &phb->ioda.pe_list, list) {
  1450. pnv_ioda_setup_pe_seg(hose, pe);
  1451. }
  1452. }
  1453. }
  1454. static void pnv_pci_ioda_setup_DMA(void)
  1455. {
  1456. struct pci_controller *hose, *tmp;
  1457. struct pnv_phb *phb;
  1458. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1459. pnv_ioda_setup_dma(hose->private_data);
  1460. /* Mark the PHB initialization done */
  1461. phb = hose->private_data;
  1462. phb->initialized = 1;
  1463. }
  1464. }
  1465. static void pnv_pci_ioda_create_dbgfs(void)
  1466. {
  1467. #ifdef CONFIG_DEBUG_FS
  1468. struct pci_controller *hose, *tmp;
  1469. struct pnv_phb *phb;
  1470. char name[16];
  1471. list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
  1472. phb = hose->private_data;
  1473. sprintf(name, "PCI%04x", hose->global_number);
  1474. phb->dbgfs = debugfs_create_dir(name, powerpc_debugfs_root);
  1475. if (!phb->dbgfs)
  1476. pr_warning("%s: Error on creating debugfs on PHB#%x\n",
  1477. __func__, hose->global_number);
  1478. }
  1479. #endif /* CONFIG_DEBUG_FS */
  1480. }
  1481. static void pnv_pci_ioda_fixup(void)
  1482. {
  1483. pnv_pci_ioda_setup_PEs();
  1484. pnv_pci_ioda_setup_seg();
  1485. pnv_pci_ioda_setup_DMA();
  1486. pnv_pci_ioda_create_dbgfs();
  1487. #ifdef CONFIG_EEH
  1488. eeh_init();
  1489. eeh_addr_cache_build();
  1490. #endif
  1491. }
  1492. /*
  1493. * Returns the alignment for I/O or memory windows for P2P
  1494. * bridges. That actually depends on how PEs are segmented.
  1495. * For now, we return I/O or M32 segment size for PE sensitive
  1496. * P2P bridges. Otherwise, the default values (4KiB for I/O,
  1497. * 1MiB for memory) will be returned.
  1498. *
  1499. * The current PCI bus might be put into one PE, which was
  1500. * create against the parent PCI bridge. For that case, we
  1501. * needn't enlarge the alignment so that we can save some
  1502. * resources.
  1503. */
  1504. static resource_size_t pnv_pci_window_alignment(struct pci_bus *bus,
  1505. unsigned long type)
  1506. {
  1507. struct pci_dev *bridge;
  1508. struct pci_controller *hose = pci_bus_to_host(bus);
  1509. struct pnv_phb *phb = hose->private_data;
  1510. int num_pci_bridges = 0;
  1511. bridge = bus->self;
  1512. while (bridge) {
  1513. if (pci_pcie_type(bridge) == PCI_EXP_TYPE_PCI_BRIDGE) {
  1514. num_pci_bridges++;
  1515. if (num_pci_bridges >= 2)
  1516. return 1;
  1517. }
  1518. bridge = bridge->bus->self;
  1519. }
  1520. /* We fail back to M32 if M64 isn't supported */
  1521. if (phb->ioda.m64_segsize &&
  1522. pnv_pci_is_mem_pref_64(type))
  1523. return phb->ioda.m64_segsize;
  1524. if (type & IORESOURCE_MEM)
  1525. return phb->ioda.m32_segsize;
  1526. return phb->ioda.io_segsize;
  1527. }
  1528. /* Prevent enabling devices for which we couldn't properly
  1529. * assign a PE
  1530. */
  1531. static int pnv_pci_enable_device_hook(struct pci_dev *dev)
  1532. {
  1533. struct pci_controller *hose = pci_bus_to_host(dev->bus);
  1534. struct pnv_phb *phb = hose->private_data;
  1535. struct pci_dn *pdn;
  1536. /* The function is probably called while the PEs have
  1537. * not be created yet. For example, resource reassignment
  1538. * during PCI probe period. We just skip the check if
  1539. * PEs isn't ready.
  1540. */
  1541. if (!phb->initialized)
  1542. return 0;
  1543. pdn = pci_get_pdn(dev);
  1544. if (!pdn || pdn->pe_number == IODA_INVALID_PE)
  1545. return -EINVAL;
  1546. return 0;
  1547. }
  1548. static u32 pnv_ioda_bdfn_to_pe(struct pnv_phb *phb, struct pci_bus *bus,
  1549. u32 devfn)
  1550. {
  1551. return phb->ioda.pe_rmap[(bus->number << 8) | devfn];
  1552. }
  1553. static void pnv_pci_ioda_shutdown(struct pnv_phb *phb)
  1554. {
  1555. opal_pci_reset(phb->opal_id, OPAL_RESET_PCI_IODA_TABLE,
  1556. OPAL_ASSERT_RESET);
  1557. }
  1558. static void __init pnv_pci_init_ioda_phb(struct device_node *np,
  1559. u64 hub_id, int ioda_type)
  1560. {
  1561. struct pci_controller *hose;
  1562. struct pnv_phb *phb;
  1563. unsigned long size, m32map_off, pemap_off, iomap_off = 0;
  1564. const __be64 *prop64;
  1565. const __be32 *prop32;
  1566. int len;
  1567. u64 phb_id;
  1568. void *aux;
  1569. long rc;
  1570. pr_info("Initializing IODA%d OPAL PHB %s\n", ioda_type, np->full_name);
  1571. prop64 = of_get_property(np, "ibm,opal-phbid", NULL);
  1572. if (!prop64) {
  1573. pr_err(" Missing \"ibm,opal-phbid\" property !\n");
  1574. return;
  1575. }
  1576. phb_id = be64_to_cpup(prop64);
  1577. pr_debug(" PHB-ID : 0x%016llx\n", phb_id);
  1578. phb = alloc_bootmem(sizeof(struct pnv_phb));
  1579. if (!phb) {
  1580. pr_err(" Out of memory !\n");
  1581. return;
  1582. }
  1583. /* Allocate PCI controller */
  1584. memset(phb, 0, sizeof(struct pnv_phb));
  1585. phb->hose = hose = pcibios_alloc_controller(np);
  1586. if (!phb->hose) {
  1587. pr_err(" Can't allocate PCI controller for %s\n",
  1588. np->full_name);
  1589. free_bootmem((unsigned long)phb, sizeof(struct pnv_phb));
  1590. return;
  1591. }
  1592. spin_lock_init(&phb->lock);
  1593. prop32 = of_get_property(np, "bus-range", &len);
  1594. if (prop32 && len == 8) {
  1595. hose->first_busno = be32_to_cpu(prop32[0]);
  1596. hose->last_busno = be32_to_cpu(prop32[1]);
  1597. } else {
  1598. pr_warn(" Broken <bus-range> on %s\n", np->full_name);
  1599. hose->first_busno = 0;
  1600. hose->last_busno = 0xff;
  1601. }
  1602. hose->private_data = phb;
  1603. phb->hub_id = hub_id;
  1604. phb->opal_id = phb_id;
  1605. phb->type = ioda_type;
  1606. /* Detect specific models for error handling */
  1607. if (of_device_is_compatible(np, "ibm,p7ioc-pciex"))
  1608. phb->model = PNV_PHB_MODEL_P7IOC;
  1609. else if (of_device_is_compatible(np, "ibm,power8-pciex"))
  1610. phb->model = PNV_PHB_MODEL_PHB3;
  1611. else
  1612. phb->model = PNV_PHB_MODEL_UNKNOWN;
  1613. /* Parse 32-bit and IO ranges (if any) */
  1614. pci_process_bridge_OF_ranges(hose, np, !hose->global_number);
  1615. /* Get registers */
  1616. phb->regs = of_iomap(np, 0);
  1617. if (phb->regs == NULL)
  1618. pr_err(" Failed to map registers !\n");
  1619. /* Initialize more IODA stuff */
  1620. phb->ioda.total_pe = 1;
  1621. prop32 = of_get_property(np, "ibm,opal-num-pes", NULL);
  1622. if (prop32)
  1623. phb->ioda.total_pe = be32_to_cpup(prop32);
  1624. prop32 = of_get_property(np, "ibm,opal-reserved-pe", NULL);
  1625. if (prop32)
  1626. phb->ioda.reserved_pe = be32_to_cpup(prop32);
  1627. /* Parse 64-bit MMIO range */
  1628. pnv_ioda_parse_m64_window(phb);
  1629. phb->ioda.m32_size = resource_size(&hose->mem_resources[0]);
  1630. /* FW Has already off top 64k of M32 space (MSI space) */
  1631. phb->ioda.m32_size += 0x10000;
  1632. phb->ioda.m32_segsize = phb->ioda.m32_size / phb->ioda.total_pe;
  1633. phb->ioda.m32_pci_base = hose->mem_resources[0].start - hose->mem_offset[0];
  1634. phb->ioda.io_size = hose->pci_io_size;
  1635. phb->ioda.io_segsize = phb->ioda.io_size / phb->ioda.total_pe;
  1636. phb->ioda.io_pci_base = 0; /* XXX calculate this ? */
  1637. /* Allocate aux data & arrays. We don't have IO ports on PHB3 */
  1638. size = _ALIGN_UP(phb->ioda.total_pe / 8, sizeof(unsigned long));
  1639. m32map_off = size;
  1640. size += phb->ioda.total_pe * sizeof(phb->ioda.m32_segmap[0]);
  1641. if (phb->type == PNV_PHB_IODA1) {
  1642. iomap_off = size;
  1643. size += phb->ioda.total_pe * sizeof(phb->ioda.io_segmap[0]);
  1644. }
  1645. pemap_off = size;
  1646. size += phb->ioda.total_pe * sizeof(struct pnv_ioda_pe);
  1647. aux = alloc_bootmem(size);
  1648. memset(aux, 0, size);
  1649. phb->ioda.pe_alloc = aux;
  1650. phb->ioda.m32_segmap = aux + m32map_off;
  1651. if (phb->type == PNV_PHB_IODA1)
  1652. phb->ioda.io_segmap = aux + iomap_off;
  1653. phb->ioda.pe_array = aux + pemap_off;
  1654. set_bit(phb->ioda.reserved_pe, phb->ioda.pe_alloc);
  1655. INIT_LIST_HEAD(&phb->ioda.pe_dma_list);
  1656. INIT_LIST_HEAD(&phb->ioda.pe_list);
  1657. /* Calculate how many 32-bit TCE segments we have */
  1658. phb->ioda.tce32_count = phb->ioda.m32_pci_base >> 28;
  1659. #if 0 /* We should really do that ... */
  1660. rc = opal_pci_set_phb_mem_window(opal->phb_id,
  1661. window_type,
  1662. window_num,
  1663. starting_real_address,
  1664. starting_pci_address,
  1665. segment_size);
  1666. #endif
  1667. pr_info(" %03d (%03d) PE's M32: 0x%x [segment=0x%x]\n",
  1668. phb->ioda.total_pe, phb->ioda.reserved_pe,
  1669. phb->ioda.m32_size, phb->ioda.m32_segsize);
  1670. if (phb->ioda.m64_size)
  1671. pr_info(" M64: 0x%lx [segment=0x%lx]\n",
  1672. phb->ioda.m64_size, phb->ioda.m64_segsize);
  1673. if (phb->ioda.io_size)
  1674. pr_info(" IO: 0x%x [segment=0x%x]\n",
  1675. phb->ioda.io_size, phb->ioda.io_segsize);
  1676. phb->hose->ops = &pnv_pci_ops;
  1677. phb->get_pe_state = pnv_ioda_get_pe_state;
  1678. phb->freeze_pe = pnv_ioda_freeze_pe;
  1679. phb->unfreeze_pe = pnv_ioda_unfreeze_pe;
  1680. #ifdef CONFIG_EEH
  1681. phb->eeh_ops = &ioda_eeh_ops;
  1682. #endif
  1683. /* Setup RID -> PE mapping function */
  1684. phb->bdfn_to_pe = pnv_ioda_bdfn_to_pe;
  1685. /* Setup TCEs */
  1686. phb->dma_dev_setup = pnv_pci_ioda_dma_dev_setup;
  1687. phb->dma_set_mask = pnv_pci_ioda_dma_set_mask;
  1688. phb->dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask;
  1689. /* Setup shutdown function for kexec */
  1690. phb->shutdown = pnv_pci_ioda_shutdown;
  1691. /* Setup MSI support */
  1692. pnv_pci_init_ioda_msis(phb);
  1693. /*
  1694. * We pass the PCI probe flag PCI_REASSIGN_ALL_RSRC here
  1695. * to let the PCI core do resource assignment. It's supposed
  1696. * that the PCI core will do correct I/O and MMIO alignment
  1697. * for the P2P bridge bars so that each PCI bus (excluding
  1698. * the child P2P bridges) can form individual PE.
  1699. */
  1700. ppc_md.pcibios_fixup = pnv_pci_ioda_fixup;
  1701. ppc_md.pcibios_enable_device_hook = pnv_pci_enable_device_hook;
  1702. ppc_md.pcibios_window_alignment = pnv_pci_window_alignment;
  1703. ppc_md.pcibios_reset_secondary_bus = pnv_pci_reset_secondary_bus;
  1704. pci_add_flags(PCI_REASSIGN_ALL_RSRC);
  1705. /* Reset IODA tables to a clean state */
  1706. rc = opal_pci_reset(phb_id, OPAL_RESET_PCI_IODA_TABLE, OPAL_ASSERT_RESET);
  1707. if (rc)
  1708. pr_warning(" OPAL Error %ld performing IODA table reset !\n", rc);
  1709. /* If we're running in kdump kerenl, the previous kerenl never
  1710. * shutdown PCI devices correctly. We already got IODA table
  1711. * cleaned out. So we have to issue PHB reset to stop all PCI
  1712. * transactions from previous kerenl.
  1713. */
  1714. if (is_kdump_kernel()) {
  1715. pr_info(" Issue PHB reset ...\n");
  1716. ioda_eeh_phb_reset(hose, EEH_RESET_FUNDAMENTAL);
  1717. ioda_eeh_phb_reset(hose, OPAL_DEASSERT_RESET);
  1718. }
  1719. /* Configure M64 window */
  1720. if (phb->init_m64 && phb->init_m64(phb))
  1721. hose->mem_resources[1].flags = 0;
  1722. }
  1723. void __init pnv_pci_init_ioda2_phb(struct device_node *np)
  1724. {
  1725. pnv_pci_init_ioda_phb(np, 0, PNV_PHB_IODA2);
  1726. }
  1727. void __init pnv_pci_init_ioda_hub(struct device_node *np)
  1728. {
  1729. struct device_node *phbn;
  1730. const __be64 *prop64;
  1731. u64 hub_id;
  1732. pr_info("Probing IODA IO-Hub %s\n", np->full_name);
  1733. prop64 = of_get_property(np, "ibm,opal-hubid", NULL);
  1734. if (!prop64) {
  1735. pr_err(" Missing \"ibm,opal-hubid\" property !\n");
  1736. return;
  1737. }
  1738. hub_id = be64_to_cpup(prop64);
  1739. pr_devel(" HUB-ID : 0x%016llx\n", hub_id);
  1740. /* Count child PHBs */
  1741. for_each_child_of_node(np, phbn) {
  1742. /* Look for IODA1 PHBs */
  1743. if (of_device_is_compatible(phbn, "ibm,ioda-phb"))
  1744. pnv_pci_init_ioda_phb(phbn, hub_id, PNV_PHB_IODA1);
  1745. }
  1746. }