spapr_pci.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  1. /*
  2. * QEMU sPAPR PCI host originated from Uninorth PCI host
  3. *
  4. * Copyright (c) 2011 Alexey Kardashevskiy, IBM Corporation.
  5. * Copyright (C) 2011 David Gibson, IBM Corporation.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "hw/irq.h"
  28. #include "hw/sysbus.h"
  29. #include "migration/vmstate.h"
  30. #include "hw/pci/pci.h"
  31. #include "hw/pci/msi.h"
  32. #include "hw/pci/msix.h"
  33. #include "hw/pci/pci_host.h"
  34. #include "hw/ppc/spapr.h"
  35. #include "hw/pci-host/spapr.h"
  36. #include "exec/ram_addr.h"
  37. #include <libfdt.h>
  38. #include "trace.h"
  39. #include "qemu/error-report.h"
  40. #include "qemu/module.h"
  41. #include "hw/ppc/fdt.h"
  42. #include "hw/pci/pci_bridge.h"
  43. #include "hw/pci/pci_bus.h"
  44. #include "hw/pci/pci_ids.h"
  45. #include "hw/ppc/spapr_drc.h"
  46. #include "hw/qdev-properties.h"
  47. #include "system/device_tree.h"
  48. #include "system/kvm.h"
  49. #include "system/hostmem.h"
  50. #include "system/numa.h"
  51. #include "hw/ppc/spapr_numa.h"
  52. #include "qemu/log.h"
  53. /* Copied from the kernel arch/powerpc/platforms/pseries/msi.c */
  54. #define RTAS_QUERY_FN 0
  55. #define RTAS_CHANGE_FN 1
  56. #define RTAS_RESET_FN 2
  57. #define RTAS_CHANGE_MSI_FN 3
  58. #define RTAS_CHANGE_MSIX_FN 4
  59. /* Interrupt types to return on RTAS_CHANGE_* */
  60. #define RTAS_TYPE_MSI 1
  61. #define RTAS_TYPE_MSIX 2
  62. SpaprPhbState *spapr_pci_find_phb(SpaprMachineState *spapr, uint64_t buid)
  63. {
  64. SpaprPhbState *sphb;
  65. QLIST_FOREACH(sphb, &spapr->phbs, list) {
  66. if (sphb->buid != buid) {
  67. continue;
  68. }
  69. return sphb;
  70. }
  71. return NULL;
  72. }
  73. PCIDevice *spapr_pci_find_dev(SpaprMachineState *spapr, uint64_t buid,
  74. uint32_t config_addr)
  75. {
  76. SpaprPhbState *sphb = spapr_pci_find_phb(spapr, buid);
  77. PCIHostState *phb = PCI_HOST_BRIDGE(sphb);
  78. int bus_num = (config_addr >> 16) & 0xFF;
  79. int devfn = (config_addr >> 8) & 0xFF;
  80. if (!phb) {
  81. return NULL;
  82. }
  83. return pci_find_device(phb->bus, bus_num, devfn);
  84. }
  85. static uint32_t rtas_pci_cfgaddr(uint32_t arg)
  86. {
  87. /* This handles the encoding of extended config space addresses */
  88. return ((arg >> 20) & 0xf00) | (arg & 0xff);
  89. }
  90. static void finish_read_pci_config(SpaprMachineState *spapr, uint64_t buid,
  91. uint32_t addr, uint32_t size,
  92. target_ulong rets)
  93. {
  94. PCIDevice *pci_dev;
  95. uint32_t val;
  96. if ((size != 1) && (size != 2) && (size != 4)) {
  97. /* access must be 1, 2 or 4 bytes */
  98. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  99. return;
  100. }
  101. pci_dev = spapr_pci_find_dev(spapr, buid, addr);
  102. addr = rtas_pci_cfgaddr(addr);
  103. if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
  104. /* Access must be to a valid device, within bounds and
  105. * naturally aligned */
  106. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  107. return;
  108. }
  109. val = pci_host_config_read_common(pci_dev, addr,
  110. pci_config_size(pci_dev), size);
  111. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  112. rtas_st(rets, 1, val);
  113. }
  114. static void rtas_ibm_read_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr,
  115. uint32_t token, uint32_t nargs,
  116. target_ulong args,
  117. uint32_t nret, target_ulong rets)
  118. {
  119. uint64_t buid;
  120. uint32_t size, addr;
  121. if ((nargs != 4) || (nret != 2)) {
  122. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  123. return;
  124. }
  125. buid = rtas_ldq(args, 1);
  126. size = rtas_ld(args, 3);
  127. addr = rtas_ld(args, 0);
  128. finish_read_pci_config(spapr, buid, addr, size, rets);
  129. }
  130. static void rtas_read_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr,
  131. uint32_t token, uint32_t nargs,
  132. target_ulong args,
  133. uint32_t nret, target_ulong rets)
  134. {
  135. uint32_t size, addr;
  136. if ((nargs != 2) || (nret != 2)) {
  137. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  138. return;
  139. }
  140. size = rtas_ld(args, 1);
  141. addr = rtas_ld(args, 0);
  142. finish_read_pci_config(spapr, 0, addr, size, rets);
  143. }
  144. static void finish_write_pci_config(SpaprMachineState *spapr, uint64_t buid,
  145. uint32_t addr, uint32_t size,
  146. uint32_t val, target_ulong rets)
  147. {
  148. PCIDevice *pci_dev;
  149. if ((size != 1) && (size != 2) && (size != 4)) {
  150. /* access must be 1, 2 or 4 bytes */
  151. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  152. return;
  153. }
  154. pci_dev = spapr_pci_find_dev(spapr, buid, addr);
  155. addr = rtas_pci_cfgaddr(addr);
  156. if (!pci_dev || (addr % size) || (addr >= pci_config_size(pci_dev))) {
  157. /* Access must be to a valid device, within bounds and
  158. * naturally aligned */
  159. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  160. return;
  161. }
  162. pci_host_config_write_common(pci_dev, addr, pci_config_size(pci_dev),
  163. val, size);
  164. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  165. }
  166. static void rtas_ibm_write_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr,
  167. uint32_t token, uint32_t nargs,
  168. target_ulong args,
  169. uint32_t nret, target_ulong rets)
  170. {
  171. uint64_t buid;
  172. uint32_t val, size, addr;
  173. if ((nargs != 5) || (nret != 1)) {
  174. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  175. return;
  176. }
  177. buid = rtas_ldq(args, 1);
  178. val = rtas_ld(args, 4);
  179. size = rtas_ld(args, 3);
  180. addr = rtas_ld(args, 0);
  181. finish_write_pci_config(spapr, buid, addr, size, val, rets);
  182. }
  183. static void rtas_write_pci_config(PowerPCCPU *cpu, SpaprMachineState *spapr,
  184. uint32_t token, uint32_t nargs,
  185. target_ulong args,
  186. uint32_t nret, target_ulong rets)
  187. {
  188. uint32_t val, size, addr;
  189. if ((nargs != 3) || (nret != 1)) {
  190. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  191. return;
  192. }
  193. val = rtas_ld(args, 2);
  194. size = rtas_ld(args, 1);
  195. addr = rtas_ld(args, 0);
  196. finish_write_pci_config(spapr, 0, addr, size, val, rets);
  197. }
  198. /*
  199. * Set MSI/MSIX message data.
  200. * This is required for msi_notify()/msix_notify() which
  201. * will write at the addresses via spapr_msi_write().
  202. *
  203. * If hwaddr == 0, all entries will have .data == first_irq i.e.
  204. * table will be reset.
  205. */
  206. static void spapr_msi_setmsg(PCIDevice *pdev, hwaddr addr, bool msix,
  207. unsigned first_irq, unsigned req_num)
  208. {
  209. unsigned i;
  210. MSIMessage msg = { .address = addr, .data = first_irq };
  211. if (!msix) {
  212. msi_set_message(pdev, msg);
  213. trace_spapr_pci_msi_setup(pdev->name, 0, msg.address);
  214. return;
  215. }
  216. for (i = 0; i < req_num; ++i) {
  217. msix_set_message(pdev, i, msg);
  218. trace_spapr_pci_msi_setup(pdev->name, i, msg.address);
  219. if (addr) {
  220. ++msg.data;
  221. }
  222. }
  223. }
  224. static void rtas_ibm_change_msi(PowerPCCPU *cpu, SpaprMachineState *spapr,
  225. uint32_t token, uint32_t nargs,
  226. target_ulong args, uint32_t nret,
  227. target_ulong rets)
  228. {
  229. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  230. uint32_t config_addr = rtas_ld(args, 0);
  231. uint64_t buid = rtas_ldq(args, 1);
  232. unsigned int func = rtas_ld(args, 3);
  233. unsigned int req_num = rtas_ld(args, 4); /* 0 == remove all */
  234. unsigned int seq_num = rtas_ld(args, 5);
  235. unsigned int ret_intr_type;
  236. unsigned int irq, max_irqs = 0;
  237. SpaprPhbState *phb = NULL;
  238. PCIDevice *pdev = NULL;
  239. SpaprPciMsi *msi;
  240. int *config_addr_key;
  241. Error *err = NULL;
  242. int i;
  243. /* Fins SpaprPhbState */
  244. phb = spapr_pci_find_phb(spapr, buid);
  245. if (phb) {
  246. pdev = spapr_pci_find_dev(spapr, buid, config_addr);
  247. }
  248. if (!phb || !pdev) {
  249. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  250. return;
  251. }
  252. switch (func) {
  253. case RTAS_CHANGE_FN:
  254. if (msi_present(pdev)) {
  255. ret_intr_type = RTAS_TYPE_MSI;
  256. } else if (msix_present(pdev)) {
  257. ret_intr_type = RTAS_TYPE_MSIX;
  258. } else {
  259. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  260. return;
  261. }
  262. break;
  263. case RTAS_CHANGE_MSI_FN:
  264. if (msi_present(pdev)) {
  265. ret_intr_type = RTAS_TYPE_MSI;
  266. } else {
  267. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  268. return;
  269. }
  270. break;
  271. case RTAS_CHANGE_MSIX_FN:
  272. if (msix_present(pdev)) {
  273. ret_intr_type = RTAS_TYPE_MSIX;
  274. } else {
  275. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  276. return;
  277. }
  278. break;
  279. default:
  280. error_report("rtas_ibm_change_msi(%u) is not implemented", func);
  281. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  282. return;
  283. }
  284. msi = (SpaprPciMsi *) g_hash_table_lookup(phb->msi, &config_addr);
  285. /* Releasing MSIs */
  286. if (!req_num) {
  287. if (!msi) {
  288. trace_spapr_pci_msi("Releasing wrong config", config_addr);
  289. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  290. return;
  291. }
  292. if (msi_present(pdev)) {
  293. spapr_msi_setmsg(pdev, 0, false, 0, 0);
  294. }
  295. if (msix_present(pdev)) {
  296. spapr_msi_setmsg(pdev, 0, true, 0, 0);
  297. }
  298. g_hash_table_remove(phb->msi, &config_addr);
  299. trace_spapr_pci_msi("Released MSIs", config_addr);
  300. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  301. rtas_st(rets, 1, 0);
  302. return;
  303. }
  304. /* Enabling MSI */
  305. /* Check if the device supports as many IRQs as requested */
  306. if (ret_intr_type == RTAS_TYPE_MSI) {
  307. max_irqs = msi_nr_vectors_allocated(pdev);
  308. } else if (ret_intr_type == RTAS_TYPE_MSIX) {
  309. max_irqs = pdev->msix_entries_nr;
  310. }
  311. if (!max_irqs) {
  312. error_report("Requested interrupt type %d is not enabled for device %x",
  313. ret_intr_type, config_addr);
  314. rtas_st(rets, 0, -1); /* Hardware error */
  315. return;
  316. }
  317. /* Correct the number if the guest asked for too many */
  318. if (req_num > max_irqs) {
  319. trace_spapr_pci_msi_retry(config_addr, req_num, max_irqs);
  320. req_num = max_irqs;
  321. irq = 0; /* to avoid misleading trace */
  322. goto out;
  323. }
  324. /* Allocate MSIs */
  325. if (smc->legacy_irq_allocation) {
  326. irq = spapr_irq_find(spapr, req_num, ret_intr_type == RTAS_TYPE_MSI,
  327. &err);
  328. } else {
  329. irq = spapr_irq_msi_alloc(spapr, req_num,
  330. ret_intr_type == RTAS_TYPE_MSI, &err);
  331. }
  332. if (err) {
  333. error_reportf_err(err, "Can't allocate MSIs for device %x: ",
  334. config_addr);
  335. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  336. return;
  337. }
  338. for (i = 0; i < req_num; i++) {
  339. spapr_irq_claim(spapr, irq + i, false, &err);
  340. if (err) {
  341. if (i) {
  342. spapr_irq_free(spapr, irq, i);
  343. }
  344. if (!smc->legacy_irq_allocation) {
  345. spapr_irq_msi_free(spapr, irq, req_num);
  346. }
  347. error_reportf_err(err, "Can't allocate MSIs for device %x: ",
  348. config_addr);
  349. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  350. return;
  351. }
  352. }
  353. /* Release previous MSIs */
  354. if (msi) {
  355. g_hash_table_remove(phb->msi, &config_addr);
  356. }
  357. /* Setup MSI/MSIX vectors in the device (via cfgspace or MSIX BAR) */
  358. spapr_msi_setmsg(pdev, SPAPR_PCI_MSI_WINDOW, ret_intr_type == RTAS_TYPE_MSIX,
  359. irq, req_num);
  360. /* Add MSI device to cache */
  361. msi = g_new(SpaprPciMsi, 1);
  362. msi->first_irq = irq;
  363. msi->num = req_num;
  364. config_addr_key = g_new(int, 1);
  365. *config_addr_key = config_addr;
  366. g_hash_table_insert(phb->msi, config_addr_key, msi);
  367. out:
  368. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  369. rtas_st(rets, 1, req_num);
  370. rtas_st(rets, 2, ++seq_num);
  371. if (nret > 3) {
  372. rtas_st(rets, 3, ret_intr_type);
  373. }
  374. trace_spapr_pci_rtas_ibm_change_msi(config_addr, func, req_num, irq);
  375. }
  376. static void rtas_ibm_query_interrupt_source_number(PowerPCCPU *cpu,
  377. SpaprMachineState *spapr,
  378. uint32_t token,
  379. uint32_t nargs,
  380. target_ulong args,
  381. uint32_t nret,
  382. target_ulong rets)
  383. {
  384. uint32_t config_addr = rtas_ld(args, 0);
  385. uint64_t buid = rtas_ldq(args, 1);
  386. unsigned int intr_src_num = -1, ioa_intr_num = rtas_ld(args, 3);
  387. SpaprPhbState *phb = NULL;
  388. PCIDevice *pdev = NULL;
  389. SpaprPciMsi *msi;
  390. /* Find SpaprPhbState */
  391. phb = spapr_pci_find_phb(spapr, buid);
  392. if (phb) {
  393. pdev = spapr_pci_find_dev(spapr, buid, config_addr);
  394. }
  395. if (!phb || !pdev) {
  396. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  397. return;
  398. }
  399. /* Find device descriptor and start IRQ */
  400. msi = (SpaprPciMsi *) g_hash_table_lookup(phb->msi, &config_addr);
  401. if (!msi || !msi->first_irq || !msi->num || (ioa_intr_num >= msi->num)) {
  402. trace_spapr_pci_msi("Failed to return vector", config_addr);
  403. rtas_st(rets, 0, RTAS_OUT_HW_ERROR);
  404. return;
  405. }
  406. intr_src_num = msi->first_irq + ioa_intr_num;
  407. trace_spapr_pci_rtas_ibm_query_interrupt_source_number(ioa_intr_num,
  408. intr_src_num);
  409. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  410. rtas_st(rets, 1, intr_src_num);
  411. rtas_st(rets, 2, 1);/* 0 == level; 1 == edge */
  412. }
  413. static void rtas_ibm_set_eeh_option(PowerPCCPU *cpu,
  414. SpaprMachineState *spapr,
  415. uint32_t token, uint32_t nargs,
  416. target_ulong args, uint32_t nret,
  417. target_ulong rets)
  418. {
  419. SpaprPhbState *sphb;
  420. uint32_t addr, option;
  421. uint64_t buid;
  422. int ret;
  423. if ((nargs != 4) || (nret != 1)) {
  424. goto param_error_exit;
  425. }
  426. buid = rtas_ldq(args, 1);
  427. addr = rtas_ld(args, 0);
  428. option = rtas_ld(args, 3);
  429. sphb = spapr_pci_find_phb(spapr, buid);
  430. if (!sphb) {
  431. goto param_error_exit;
  432. }
  433. if (!spapr_phb_eeh_available(sphb)) {
  434. goto param_error_exit;
  435. }
  436. ret = spapr_phb_vfio_eeh_set_option(sphb, addr, option);
  437. rtas_st(rets, 0, ret);
  438. return;
  439. param_error_exit:
  440. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  441. }
  442. static void rtas_ibm_get_config_addr_info2(PowerPCCPU *cpu,
  443. SpaprMachineState *spapr,
  444. uint32_t token, uint32_t nargs,
  445. target_ulong args, uint32_t nret,
  446. target_ulong rets)
  447. {
  448. SpaprPhbState *sphb;
  449. PCIDevice *pdev;
  450. uint32_t addr, option;
  451. uint64_t buid;
  452. if ((nargs != 4) || (nret != 2)) {
  453. goto param_error_exit;
  454. }
  455. buid = rtas_ldq(args, 1);
  456. sphb = spapr_pci_find_phb(spapr, buid);
  457. if (!sphb) {
  458. goto param_error_exit;
  459. }
  460. if (!spapr_phb_eeh_available(sphb)) {
  461. goto param_error_exit;
  462. }
  463. /*
  464. * We always have PE address of form "00BB0001". "BB"
  465. * represents the bus number of PE's primary bus.
  466. */
  467. option = rtas_ld(args, 3);
  468. switch (option) {
  469. case RTAS_GET_PE_ADDR:
  470. addr = rtas_ld(args, 0);
  471. pdev = spapr_pci_find_dev(spapr, buid, addr);
  472. if (!pdev) {
  473. goto param_error_exit;
  474. }
  475. rtas_st(rets, 1, (pci_bus_num(pci_get_bus(pdev)) << 16) + 1);
  476. break;
  477. case RTAS_GET_PE_MODE:
  478. rtas_st(rets, 1, RTAS_PE_MODE_SHARED);
  479. break;
  480. default:
  481. goto param_error_exit;
  482. }
  483. rtas_st(rets, 0, RTAS_OUT_SUCCESS);
  484. return;
  485. param_error_exit:
  486. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  487. }
  488. static void rtas_ibm_read_slot_reset_state2(PowerPCCPU *cpu,
  489. SpaprMachineState *spapr,
  490. uint32_t token, uint32_t nargs,
  491. target_ulong args, uint32_t nret,
  492. target_ulong rets)
  493. {
  494. SpaprPhbState *sphb;
  495. uint64_t buid;
  496. int state, ret;
  497. if ((nargs != 3) || (nret != 4 && nret != 5)) {
  498. goto param_error_exit;
  499. }
  500. buid = rtas_ldq(args, 1);
  501. sphb = spapr_pci_find_phb(spapr, buid);
  502. if (!sphb) {
  503. goto param_error_exit;
  504. }
  505. if (!spapr_phb_eeh_available(sphb)) {
  506. goto param_error_exit;
  507. }
  508. ret = spapr_phb_vfio_eeh_get_state(sphb, &state);
  509. rtas_st(rets, 0, ret);
  510. if (ret != RTAS_OUT_SUCCESS) {
  511. return;
  512. }
  513. rtas_st(rets, 1, state);
  514. rtas_st(rets, 2, RTAS_EEH_SUPPORT);
  515. rtas_st(rets, 3, RTAS_EEH_PE_UNAVAIL_INFO);
  516. if (nret >= 5) {
  517. rtas_st(rets, 4, RTAS_EEH_PE_RECOVER_INFO);
  518. }
  519. return;
  520. param_error_exit:
  521. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  522. }
  523. static void rtas_ibm_set_slot_reset(PowerPCCPU *cpu,
  524. SpaprMachineState *spapr,
  525. uint32_t token, uint32_t nargs,
  526. target_ulong args, uint32_t nret,
  527. target_ulong rets)
  528. {
  529. SpaprPhbState *sphb;
  530. uint32_t option;
  531. uint64_t buid;
  532. int ret;
  533. if ((nargs != 4) || (nret != 1)) {
  534. goto param_error_exit;
  535. }
  536. buid = rtas_ldq(args, 1);
  537. option = rtas_ld(args, 3);
  538. sphb = spapr_pci_find_phb(spapr, buid);
  539. if (!sphb) {
  540. goto param_error_exit;
  541. }
  542. if (!spapr_phb_eeh_available(sphb)) {
  543. goto param_error_exit;
  544. }
  545. ret = spapr_phb_vfio_eeh_reset(sphb, option);
  546. rtas_st(rets, 0, ret);
  547. return;
  548. param_error_exit:
  549. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  550. }
  551. static void rtas_ibm_configure_pe(PowerPCCPU *cpu,
  552. SpaprMachineState *spapr,
  553. uint32_t token, uint32_t nargs,
  554. target_ulong args, uint32_t nret,
  555. target_ulong rets)
  556. {
  557. SpaprPhbState *sphb;
  558. uint64_t buid;
  559. int ret;
  560. if ((nargs != 3) || (nret != 1)) {
  561. goto param_error_exit;
  562. }
  563. buid = rtas_ldq(args, 1);
  564. sphb = spapr_pci_find_phb(spapr, buid);
  565. if (!sphb) {
  566. goto param_error_exit;
  567. }
  568. if (!spapr_phb_eeh_available(sphb)) {
  569. goto param_error_exit;
  570. }
  571. ret = spapr_phb_vfio_eeh_configure(sphb);
  572. rtas_st(rets, 0, ret);
  573. return;
  574. param_error_exit:
  575. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  576. }
  577. /* To support it later */
  578. static void rtas_ibm_slot_error_detail(PowerPCCPU *cpu,
  579. SpaprMachineState *spapr,
  580. uint32_t token, uint32_t nargs,
  581. target_ulong args, uint32_t nret,
  582. target_ulong rets)
  583. {
  584. SpaprPhbState *sphb;
  585. int option;
  586. uint64_t buid;
  587. if ((nargs != 8) || (nret != 1)) {
  588. goto param_error_exit;
  589. }
  590. buid = rtas_ldq(args, 1);
  591. sphb = spapr_pci_find_phb(spapr, buid);
  592. if (!sphb) {
  593. goto param_error_exit;
  594. }
  595. if (!spapr_phb_eeh_available(sphb)) {
  596. goto param_error_exit;
  597. }
  598. option = rtas_ld(args, 7);
  599. switch (option) {
  600. case RTAS_SLOT_TEMP_ERR_LOG:
  601. case RTAS_SLOT_PERM_ERR_LOG:
  602. break;
  603. default:
  604. goto param_error_exit;
  605. }
  606. /* We don't have error log yet */
  607. rtas_st(rets, 0, RTAS_OUT_NO_ERRORS_FOUND);
  608. return;
  609. param_error_exit:
  610. rtas_st(rets, 0, RTAS_OUT_PARAM_ERROR);
  611. }
  612. static void pci_spapr_set_irq(void *opaque, int irq_num, int level)
  613. {
  614. /*
  615. * Here we use the number returned by pci_swizzle_map_irq_fn to find a
  616. * corresponding qemu_irq.
  617. */
  618. SpaprPhbState *phb = opaque;
  619. SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
  620. trace_spapr_pci_lsi_set(phb->dtbusname, irq_num, phb->lsi_table[irq_num].irq);
  621. qemu_set_irq(spapr_qirq(spapr, phb->lsi_table[irq_num].irq), level);
  622. }
  623. static PCIINTxRoute spapr_route_intx_pin_to_irq(void *opaque, int pin)
  624. {
  625. SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(opaque);
  626. PCIINTxRoute route;
  627. route.mode = PCI_INTX_ENABLED;
  628. route.irq = sphb->lsi_table[pin].irq;
  629. return route;
  630. }
  631. static uint64_t spapr_msi_read(void *opaque, hwaddr addr, unsigned size)
  632. {
  633. qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid access\n", __func__);
  634. return 0;
  635. }
  636. /*
  637. * MSI/MSIX memory region implementation.
  638. * The handler handles both MSI and MSIX.
  639. * The vector number is encoded in least bits in data.
  640. */
  641. static void spapr_msi_write(void *opaque, hwaddr addr,
  642. uint64_t data, unsigned size)
  643. {
  644. SpaprMachineState *spapr = opaque;
  645. uint32_t irq = data;
  646. trace_spapr_pci_msi_write(addr, data, irq);
  647. qemu_irq_pulse(spapr_qirq(spapr, irq));
  648. }
  649. static const MemoryRegionOps spapr_msi_ops = {
  650. /*
  651. * .read result is undefined by PCI spec.
  652. * define .read method to avoid assert failure in memory_region_init_io
  653. */
  654. .read = spapr_msi_read,
  655. .write = spapr_msi_write,
  656. .endianness = DEVICE_LITTLE_ENDIAN
  657. };
  658. /*
  659. * PHB PCI device
  660. */
  661. static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
  662. {
  663. SpaprPhbState *phb = opaque;
  664. return &phb->iommu_as;
  665. }
  666. static const PCIIOMMUOps spapr_iommu_ops = {
  667. .get_address_space = spapr_pci_dma_iommu,
  668. };
  669. static char *spapr_phb_vfio_get_loc_code(SpaprPhbState *sphb, PCIDevice *pdev)
  670. {
  671. g_autofree char *path = NULL;
  672. g_autofree char *host = NULL;
  673. g_autofree char *devspec = NULL;
  674. char *buf = NULL;
  675. /* Get the PCI VFIO host id */
  676. host = object_property_get_str(OBJECT(pdev), "host", NULL);
  677. if (!host) {
  678. return NULL;
  679. }
  680. /* Construct the path of the file that will give us the DT location */
  681. path = g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host);
  682. if (!g_file_get_contents(path, &devspec, NULL, NULL)) {
  683. return NULL;
  684. }
  685. /* Construct and read from host device tree the loc-code */
  686. g_free(path);
  687. path = g_strdup_printf("/proc/device-tree%s/ibm,loc-code", devspec);
  688. if (!g_file_get_contents(path, &buf, NULL, NULL)) {
  689. return NULL;
  690. }
  691. return buf;
  692. }
  693. static char *spapr_phb_get_loc_code(SpaprPhbState *sphb, PCIDevice *pdev)
  694. {
  695. char *buf;
  696. const char *devtype = "qemu";
  697. uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))));
  698. if (object_dynamic_cast(OBJECT(pdev), "vfio-pci")) {
  699. buf = spapr_phb_vfio_get_loc_code(sphb, pdev);
  700. if (buf) {
  701. return buf;
  702. }
  703. devtype = "vfio";
  704. }
  705. /*
  706. * For emulated devices and VFIO-failure case, make up
  707. * the loc-code.
  708. */
  709. buf = g_strdup_printf("%s_%s:%04x:%02x:%02x.%x",
  710. devtype, pdev->name, sphb->index, busnr,
  711. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  712. return buf;
  713. }
  714. /* Macros to operate with address in OF binding to PCI */
  715. #define b_x(x, p, l) (((x) & ((1<<(l))-1)) << (p))
  716. #define b_n(x) b_x((x), 31, 1) /* 0 if relocatable */
  717. #define b_p(x) b_x((x), 30, 1) /* 1 if prefetchable */
  718. #define b_t(x) b_x((x), 29, 1) /* 1 if the address is aliased */
  719. #define b_ss(x) b_x((x), 24, 2) /* the space code */
  720. #define b_bbbbbbbb(x) b_x((x), 16, 8) /* bus number */
  721. #define b_ddddd(x) b_x((x), 11, 5) /* device number */
  722. #define b_fff(x) b_x((x), 8, 3) /* function number */
  723. #define b_rrrrrrrr(x) b_x((x), 0, 8) /* register number */
  724. /* for 'reg' OF properties */
  725. #define RESOURCE_CELLS_SIZE 2
  726. #define RESOURCE_CELLS_ADDRESS 3
  727. typedef struct ResourceFields {
  728. uint32_t phys_hi;
  729. uint32_t phys_mid;
  730. uint32_t phys_lo;
  731. uint32_t size_hi;
  732. uint32_t size_lo;
  733. } QEMU_PACKED ResourceFields;
  734. typedef struct ResourceProps {
  735. ResourceFields reg[8];
  736. uint32_t reg_len;
  737. } ResourceProps;
  738. /* fill in the 'reg' OF properties for
  739. * a PCI device. 'reg' describes resource requirements for a
  740. * device's IO/MEM regions.
  741. *
  742. * the property is an array of ('phys-addr', 'size') pairs describing
  743. * the addressable regions of the PCI device, where 'phys-addr' is a
  744. * RESOURCE_CELLS_ADDRESS-tuple of 32-bit integers corresponding to
  745. * (phys.hi, phys.mid, phys.lo), and 'size' is a
  746. * RESOURCE_CELLS_SIZE-tuple corresponding to (size.hi, size.lo).
  747. *
  748. * phys.hi = 0xYYXXXXZZ, where:
  749. * 0xYY = npt000ss
  750. * ||| |
  751. * ||| +-- space code
  752. * ||| |
  753. * ||| + 00 if configuration space
  754. * ||| + 01 if IO region,
  755. * ||| + 10 if 32-bit MEM region
  756. * ||| + 11 if 64-bit MEM region
  757. * |||
  758. * ||+------ for non-relocatable IO: 1 if aliased
  759. * || for relocatable IO: 1 if below 64KB
  760. * || for MEM: 1 if below 1MB
  761. * |+------- 1 if region is prefetchable
  762. * +-------- 1 if region is non-relocatable
  763. * 0xXXXX = bbbbbbbb dddddfff, encoding bus, slot, and function
  764. * bits respectively
  765. * 0xZZ = rrrrrrrr, the register number of the BAR corresponding
  766. * to the region
  767. *
  768. * phys.mid and phys.lo correspond respectively to the hi/lo portions
  769. * of the actual address of the region.
  770. *
  771. * note also that addresses defined in this property are, at least
  772. * for PAPR guests, relative to the PHBs IO/MEM windows, and
  773. * correspond directly to the addresses in the BARs.
  774. *
  775. * in accordance with PCI Bus Binding to Open Firmware,
  776. * IEEE Std 1275-1994, section 4.1.1, as implemented by PAPR+ v2.7,
  777. * Appendix C.
  778. */
  779. static void populate_resource_props(PCIDevice *d, ResourceProps *rp)
  780. {
  781. int bus_num = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(d))));
  782. uint32_t dev_id = (b_bbbbbbbb(bus_num) |
  783. b_ddddd(PCI_SLOT(d->devfn)) |
  784. b_fff(PCI_FUNC(d->devfn)));
  785. ResourceFields *reg;
  786. int i, reg_idx = 0;
  787. /* config space region */
  788. reg = &rp->reg[reg_idx++];
  789. reg->phys_hi = cpu_to_be32(dev_id);
  790. reg->phys_mid = 0;
  791. reg->phys_lo = 0;
  792. reg->size_hi = 0;
  793. reg->size_lo = 0;
  794. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  795. if (!d->io_regions[i].size) {
  796. continue;
  797. }
  798. reg = &rp->reg[reg_idx++];
  799. reg->phys_hi = cpu_to_be32(dev_id | b_rrrrrrrr(pci_bar(d, i)));
  800. if (d->io_regions[i].type & PCI_BASE_ADDRESS_SPACE_IO) {
  801. reg->phys_hi |= cpu_to_be32(b_ss(1));
  802. } else if (d->io_regions[i].type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  803. reg->phys_hi |= cpu_to_be32(b_ss(3));
  804. } else {
  805. reg->phys_hi |= cpu_to_be32(b_ss(2));
  806. }
  807. reg->phys_mid = 0;
  808. reg->phys_lo = 0;
  809. reg->size_hi = cpu_to_be32(d->io_regions[i].size >> 32);
  810. reg->size_lo = cpu_to_be32(d->io_regions[i].size);
  811. }
  812. rp->reg_len = reg_idx * sizeof(ResourceFields);
  813. }
  814. typedef struct PCIClass PCIClass;
  815. typedef struct PCISubClass PCISubClass;
  816. typedef struct PCIIFace PCIIFace;
  817. struct PCIIFace {
  818. int iface;
  819. const char *name;
  820. };
  821. struct PCISubClass {
  822. int subclass;
  823. const char *name;
  824. const PCIIFace *iface;
  825. };
  826. struct PCIClass {
  827. const char *name;
  828. const PCISubClass *subc;
  829. };
  830. static const PCISubClass undef_subclass[] = {
  831. { PCI_CLASS_NOT_DEFINED_VGA, "display", NULL },
  832. { 0xFF, NULL, NULL },
  833. };
  834. static const PCISubClass mass_subclass[] = {
  835. { PCI_CLASS_STORAGE_SCSI, "scsi", NULL },
  836. { PCI_CLASS_STORAGE_IDE, "ide", NULL },
  837. { PCI_CLASS_STORAGE_FLOPPY, "fdc", NULL },
  838. { PCI_CLASS_STORAGE_IPI, "ipi", NULL },
  839. { PCI_CLASS_STORAGE_RAID, "raid", NULL },
  840. { PCI_CLASS_STORAGE_ATA, "ata", NULL },
  841. { PCI_CLASS_STORAGE_SATA, "sata", NULL },
  842. { PCI_CLASS_STORAGE_SAS, "sas", NULL },
  843. { 0xFF, NULL, NULL },
  844. };
  845. static const PCISubClass net_subclass[] = {
  846. { PCI_CLASS_NETWORK_ETHERNET, "ethernet", NULL },
  847. { PCI_CLASS_NETWORK_TOKEN_RING, "token-ring", NULL },
  848. { PCI_CLASS_NETWORK_FDDI, "fddi", NULL },
  849. { PCI_CLASS_NETWORK_ATM, "atm", NULL },
  850. { PCI_CLASS_NETWORK_ISDN, "isdn", NULL },
  851. { PCI_CLASS_NETWORK_WORLDFIP, "worldfip", NULL },
  852. { PCI_CLASS_NETWORK_PICMG214, "picmg", NULL },
  853. { 0xFF, NULL, NULL },
  854. };
  855. static const PCISubClass displ_subclass[] = {
  856. { PCI_CLASS_DISPLAY_VGA, "vga", NULL },
  857. { PCI_CLASS_DISPLAY_XGA, "xga", NULL },
  858. { PCI_CLASS_DISPLAY_3D, "3d-controller", NULL },
  859. { 0xFF, NULL, NULL },
  860. };
  861. static const PCISubClass media_subclass[] = {
  862. { PCI_CLASS_MULTIMEDIA_VIDEO, "video", NULL },
  863. { PCI_CLASS_MULTIMEDIA_AUDIO, "sound", NULL },
  864. { PCI_CLASS_MULTIMEDIA_PHONE, "telephony", NULL },
  865. { 0xFF, NULL, NULL },
  866. };
  867. static const PCISubClass mem_subclass[] = {
  868. { PCI_CLASS_MEMORY_RAM, "memory", NULL },
  869. { PCI_CLASS_MEMORY_FLASH, "flash", NULL },
  870. { 0xFF, NULL, NULL },
  871. };
  872. static const PCISubClass bridg_subclass[] = {
  873. { PCI_CLASS_BRIDGE_HOST, "host", NULL },
  874. { PCI_CLASS_BRIDGE_ISA, "isa", NULL },
  875. { PCI_CLASS_BRIDGE_EISA, "eisa", NULL },
  876. { PCI_CLASS_BRIDGE_MC, "mca", NULL },
  877. { PCI_CLASS_BRIDGE_PCI, "pci", NULL },
  878. { PCI_CLASS_BRIDGE_PCMCIA, "pcmcia", NULL },
  879. { PCI_CLASS_BRIDGE_NUBUS, "nubus", NULL },
  880. { PCI_CLASS_BRIDGE_CARDBUS, "cardbus", NULL },
  881. { PCI_CLASS_BRIDGE_RACEWAY, "raceway", NULL },
  882. { PCI_CLASS_BRIDGE_PCI_SEMITP, "semi-transparent-pci", NULL },
  883. { PCI_CLASS_BRIDGE_IB_PCI, "infiniband", NULL },
  884. { 0xFF, NULL, NULL },
  885. };
  886. static const PCISubClass comm_subclass[] = {
  887. { PCI_CLASS_COMMUNICATION_SERIAL, "serial", NULL },
  888. { PCI_CLASS_COMMUNICATION_PARALLEL, "parallel", NULL },
  889. { PCI_CLASS_COMMUNICATION_MULTISERIAL, "multiport-serial", NULL },
  890. { PCI_CLASS_COMMUNICATION_MODEM, "modem", NULL },
  891. { PCI_CLASS_COMMUNICATION_GPIB, "gpib", NULL },
  892. { PCI_CLASS_COMMUNICATION_SC, "smart-card", NULL },
  893. { 0xFF, NULL, NULL, },
  894. };
  895. static const PCIIFace pic_iface[] = {
  896. { PCI_CLASS_SYSTEM_PIC_IOAPIC, "io-apic" },
  897. { PCI_CLASS_SYSTEM_PIC_IOXAPIC, "io-xapic" },
  898. { 0xFF, NULL },
  899. };
  900. static const PCISubClass sys_subclass[] = {
  901. { PCI_CLASS_SYSTEM_PIC, "interrupt-controller", pic_iface },
  902. { PCI_CLASS_SYSTEM_DMA, "dma-controller", NULL },
  903. { PCI_CLASS_SYSTEM_TIMER, "timer", NULL },
  904. { PCI_CLASS_SYSTEM_RTC, "rtc", NULL },
  905. { PCI_CLASS_SYSTEM_PCI_HOTPLUG, "hot-plug-controller", NULL },
  906. { PCI_CLASS_SYSTEM_SDHCI, "sd-host-controller", NULL },
  907. { 0xFF, NULL, NULL },
  908. };
  909. static const PCISubClass inp_subclass[] = {
  910. { PCI_CLASS_INPUT_KEYBOARD, "keyboard", NULL },
  911. { PCI_CLASS_INPUT_PEN, "pen", NULL },
  912. { PCI_CLASS_INPUT_MOUSE, "mouse", NULL },
  913. { PCI_CLASS_INPUT_SCANNER, "scanner", NULL },
  914. { PCI_CLASS_INPUT_GAMEPORT, "gameport", NULL },
  915. { 0xFF, NULL, NULL },
  916. };
  917. static const PCISubClass dock_subclass[] = {
  918. { PCI_CLASS_DOCKING_GENERIC, "dock", NULL },
  919. { 0xFF, NULL, NULL },
  920. };
  921. static const PCISubClass cpu_subclass[] = {
  922. { PCI_CLASS_PROCESSOR_PENTIUM, "pentium", NULL },
  923. { PCI_CLASS_PROCESSOR_POWERPC, "powerpc", NULL },
  924. { PCI_CLASS_PROCESSOR_MIPS, "mips", NULL },
  925. { PCI_CLASS_PROCESSOR_CO, "co-processor", NULL },
  926. { 0xFF, NULL, NULL },
  927. };
  928. static const PCIIFace usb_iface[] = {
  929. { PCI_CLASS_SERIAL_USB_UHCI, "usb-uhci" },
  930. { PCI_CLASS_SERIAL_USB_OHCI, "usb-ohci", },
  931. { PCI_CLASS_SERIAL_USB_EHCI, "usb-ehci" },
  932. { PCI_CLASS_SERIAL_USB_XHCI, "usb-xhci" },
  933. { PCI_CLASS_SERIAL_USB_UNKNOWN, "usb-unknown" },
  934. { PCI_CLASS_SERIAL_USB_DEVICE, "usb-device" },
  935. { 0xFF, NULL },
  936. };
  937. static const PCISubClass ser_subclass[] = {
  938. { PCI_CLASS_SERIAL_FIREWIRE, "firewire", NULL },
  939. { PCI_CLASS_SERIAL_ACCESS, "access-bus", NULL },
  940. { PCI_CLASS_SERIAL_SSA, "ssa", NULL },
  941. { PCI_CLASS_SERIAL_USB, "usb", usb_iface },
  942. { PCI_CLASS_SERIAL_FIBER, "fibre-channel", NULL },
  943. { PCI_CLASS_SERIAL_SMBUS, "smb", NULL },
  944. { PCI_CLASS_SERIAL_IB, "infiniband", NULL },
  945. { PCI_CLASS_SERIAL_IPMI, "ipmi", NULL },
  946. { PCI_CLASS_SERIAL_SERCOS, "sercos", NULL },
  947. { PCI_CLASS_SERIAL_CANBUS, "canbus", NULL },
  948. { 0xFF, NULL, NULL },
  949. };
  950. static const PCISubClass wrl_subclass[] = {
  951. { PCI_CLASS_WIRELESS_IRDA, "irda", NULL },
  952. { PCI_CLASS_WIRELESS_CIR, "consumer-ir", NULL },
  953. { PCI_CLASS_WIRELESS_RF_CONTROLLER, "rf-controller", NULL },
  954. { PCI_CLASS_WIRELESS_BLUETOOTH, "bluetooth", NULL },
  955. { PCI_CLASS_WIRELESS_BROADBAND, "broadband", NULL },
  956. { 0xFF, NULL, NULL },
  957. };
  958. static const PCISubClass sat_subclass[] = {
  959. { PCI_CLASS_SATELLITE_TV, "satellite-tv", NULL },
  960. { PCI_CLASS_SATELLITE_AUDIO, "satellite-audio", NULL },
  961. { PCI_CLASS_SATELLITE_VOICE, "satellite-voice", NULL },
  962. { PCI_CLASS_SATELLITE_DATA, "satellite-data", NULL },
  963. { 0xFF, NULL, NULL },
  964. };
  965. static const PCISubClass crypt_subclass[] = {
  966. { PCI_CLASS_CRYPT_NETWORK, "network-encryption", NULL },
  967. { PCI_CLASS_CRYPT_ENTERTAINMENT,
  968. "entertainment-encryption", NULL },
  969. { 0xFF, NULL, NULL },
  970. };
  971. static const PCISubClass spc_subclass[] = {
  972. { PCI_CLASS_SP_DPIO, "dpio", NULL },
  973. { PCI_CLASS_SP_PERF, "counter", NULL },
  974. { PCI_CLASS_SP_SYNCH, "measurement", NULL },
  975. { PCI_CLASS_SP_MANAGEMENT, "management-card", NULL },
  976. { 0xFF, NULL, NULL },
  977. };
  978. static const PCIClass pci_classes[] = {
  979. { "legacy-device", undef_subclass },
  980. { "mass-storage", mass_subclass },
  981. { "network", net_subclass },
  982. { "display", displ_subclass, },
  983. { "multimedia-device", media_subclass },
  984. { "memory-controller", mem_subclass },
  985. { "unknown-bridge", bridg_subclass },
  986. { "communication-controller", comm_subclass},
  987. { "system-peripheral", sys_subclass },
  988. { "input-controller", inp_subclass },
  989. { "docking-station", dock_subclass },
  990. { "cpu", cpu_subclass },
  991. { "serial-bus", ser_subclass },
  992. { "wireless-controller", wrl_subclass },
  993. { "intelligent-io", NULL },
  994. { "satellite-device", sat_subclass },
  995. { "encryption", crypt_subclass },
  996. { "data-processing-controller", spc_subclass },
  997. };
  998. static const char *dt_name_from_class(uint8_t class, uint8_t subclass,
  999. uint8_t iface)
  1000. {
  1001. const PCIClass *pclass;
  1002. const PCISubClass *psubclass;
  1003. const PCIIFace *piface;
  1004. const char *name;
  1005. if (class >= ARRAY_SIZE(pci_classes)) {
  1006. return "pci";
  1007. }
  1008. pclass = pci_classes + class;
  1009. name = pclass->name;
  1010. if (pclass->subc == NULL) {
  1011. return name;
  1012. }
  1013. psubclass = pclass->subc;
  1014. while ((psubclass->subclass & 0xff) != 0xff) {
  1015. if ((psubclass->subclass & 0xff) == subclass) {
  1016. name = psubclass->name;
  1017. break;
  1018. }
  1019. psubclass++;
  1020. }
  1021. piface = psubclass->iface;
  1022. if (piface == NULL) {
  1023. return name;
  1024. }
  1025. while ((piface->iface & 0xff) != 0xff) {
  1026. if ((piface->iface & 0xff) == iface) {
  1027. name = piface->name;
  1028. break;
  1029. }
  1030. piface++;
  1031. }
  1032. return name;
  1033. }
  1034. /*
  1035. * DRC helper functions
  1036. */
  1037. static uint32_t drc_id_from_devfn(SpaprPhbState *phb,
  1038. uint8_t chassis, int32_t devfn)
  1039. {
  1040. return (phb->index << 16) | (chassis << 8) | devfn;
  1041. }
  1042. static SpaprDrc *drc_from_devfn(SpaprPhbState *phb,
  1043. uint8_t chassis, int32_t devfn)
  1044. {
  1045. return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI,
  1046. drc_id_from_devfn(phb, chassis, devfn));
  1047. }
  1048. static uint8_t chassis_from_bus(PCIBus *bus)
  1049. {
  1050. if (pci_bus_is_root(bus)) {
  1051. return 0;
  1052. } else {
  1053. PCIDevice *bridge = pci_bridge_get_device(bus);
  1054. return object_property_get_uint(OBJECT(bridge), "chassis_nr",
  1055. &error_abort);
  1056. }
  1057. }
  1058. static SpaprDrc *drc_from_dev(SpaprPhbState *phb, PCIDevice *dev)
  1059. {
  1060. uint8_t chassis = chassis_from_bus(pci_get_bus(dev));
  1061. return drc_from_devfn(phb, chassis, dev->devfn);
  1062. }
  1063. static void add_drcs(SpaprPhbState *phb, PCIBus *bus)
  1064. {
  1065. Object *owner;
  1066. int i;
  1067. uint8_t chassis;
  1068. chassis = chassis_from_bus(bus);
  1069. if (pci_bus_is_root(bus)) {
  1070. owner = OBJECT(phb);
  1071. } else {
  1072. owner = OBJECT(pci_bridge_get_device(bus));
  1073. }
  1074. for (i = 0; i < PCI_SLOT_MAX * PCI_FUNC_MAX; i++) {
  1075. spapr_dr_connector_new(owner, TYPE_SPAPR_DRC_PCI,
  1076. drc_id_from_devfn(phb, chassis, i));
  1077. }
  1078. }
  1079. static void remove_drcs(SpaprPhbState *phb, PCIBus *bus)
  1080. {
  1081. int i;
  1082. uint8_t chassis;
  1083. chassis = chassis_from_bus(bus);
  1084. for (i = PCI_SLOT_MAX * PCI_FUNC_MAX - 1; i >= 0; i--) {
  1085. SpaprDrc *drc = drc_from_devfn(phb, chassis, i);
  1086. if (drc) {
  1087. object_unparent(OBJECT(drc));
  1088. }
  1089. }
  1090. }
  1091. typedef struct PciWalkFdt {
  1092. void *fdt;
  1093. int offset;
  1094. SpaprPhbState *sphb;
  1095. int err;
  1096. } PciWalkFdt;
  1097. static int spapr_dt_pci_device(SpaprPhbState *sphb, PCIDevice *dev,
  1098. void *fdt, int parent_offset);
  1099. static void spapr_dt_pci_device_cb(PCIBus *bus, PCIDevice *pdev,
  1100. void *opaque)
  1101. {
  1102. PciWalkFdt *p = opaque;
  1103. int err;
  1104. if (p->err || !pdev->enabled) {
  1105. return;
  1106. }
  1107. err = spapr_dt_pci_device(p->sphb, pdev, p->fdt, p->offset);
  1108. if (err < 0) {
  1109. p->err = err;
  1110. }
  1111. }
  1112. /* Augment PCI device node with bridge specific information */
  1113. static int spapr_dt_pci_bus(SpaprPhbState *sphb, PCIBus *bus,
  1114. void *fdt, int offset)
  1115. {
  1116. Object *owner;
  1117. PciWalkFdt cbinfo = {
  1118. .fdt = fdt,
  1119. .offset = offset,
  1120. .sphb = sphb,
  1121. .err = 0,
  1122. };
  1123. int ret;
  1124. _FDT(fdt_setprop_cell(fdt, offset, "#address-cells",
  1125. RESOURCE_CELLS_ADDRESS));
  1126. _FDT(fdt_setprop_cell(fdt, offset, "#size-cells",
  1127. RESOURCE_CELLS_SIZE));
  1128. assert(bus);
  1129. pci_for_each_device_under_bus_reverse(bus, spapr_dt_pci_device_cb, &cbinfo);
  1130. if (cbinfo.err) {
  1131. return cbinfo.err;
  1132. }
  1133. if (pci_bus_is_root(bus)) {
  1134. owner = OBJECT(sphb);
  1135. } else {
  1136. owner = OBJECT(pci_bridge_get_device(bus));
  1137. }
  1138. ret = spapr_dt_drc(fdt, offset, owner,
  1139. SPAPR_DR_CONNECTOR_TYPE_PCI);
  1140. if (ret) {
  1141. return ret;
  1142. }
  1143. return offset;
  1144. }
  1145. char *spapr_pci_fw_dev_name(PCIDevice *dev)
  1146. {
  1147. const gchar *basename;
  1148. int slot = PCI_SLOT(dev->devfn);
  1149. int func = PCI_FUNC(dev->devfn);
  1150. uint32_t ccode = pci_default_read_config(dev, PCI_CLASS_PROG, 3);
  1151. basename = dt_name_from_class((ccode >> 16) & 0xff, (ccode >> 8) & 0xff,
  1152. ccode & 0xff);
  1153. if (func != 0) {
  1154. return g_strdup_printf("%s@%x,%x", basename, slot, func);
  1155. } else {
  1156. return g_strdup_printf("%s@%x", basename, slot);
  1157. }
  1158. }
  1159. /* create OF node for pci device and required OF DT properties */
  1160. static int spapr_dt_pci_device(SpaprPhbState *sphb, PCIDevice *dev,
  1161. void *fdt, int parent_offset)
  1162. {
  1163. int offset;
  1164. g_autofree gchar *nodename = spapr_pci_fw_dev_name(dev);
  1165. ResourceProps rp;
  1166. SpaprDrc *drc = drc_from_dev(sphb, dev);
  1167. uint32_t vendor_id = pci_default_read_config(dev, PCI_VENDOR_ID, 2);
  1168. uint32_t device_id = pci_default_read_config(dev, PCI_DEVICE_ID, 2);
  1169. uint32_t revision_id = pci_default_read_config(dev, PCI_REVISION_ID, 1);
  1170. uint32_t ccode = pci_default_read_config(dev, PCI_CLASS_PROG, 3);
  1171. uint32_t irq_pin = pci_default_read_config(dev, PCI_INTERRUPT_PIN, 1);
  1172. uint32_t subsystem_id = pci_default_read_config(dev, PCI_SUBSYSTEM_ID, 2);
  1173. uint32_t subsystem_vendor_id =
  1174. pci_default_read_config(dev, PCI_SUBSYSTEM_VENDOR_ID, 2);
  1175. uint32_t cache_line_size =
  1176. pci_default_read_config(dev, PCI_CACHE_LINE_SIZE, 1);
  1177. uint32_t pci_status = pci_default_read_config(dev, PCI_STATUS, 2);
  1178. gchar *loc_code;
  1179. _FDT(offset = fdt_add_subnode(fdt, parent_offset, nodename));
  1180. /* in accordance with PAPR+ v2.7 13.6.3, Table 181 */
  1181. _FDT(fdt_setprop_cell(fdt, offset, "vendor-id", vendor_id));
  1182. _FDT(fdt_setprop_cell(fdt, offset, "device-id", device_id));
  1183. _FDT(fdt_setprop_cell(fdt, offset, "revision-id", revision_id));
  1184. _FDT(fdt_setprop_cell(fdt, offset, "class-code", ccode));
  1185. if (irq_pin) {
  1186. _FDT(fdt_setprop_cell(fdt, offset, "interrupts", irq_pin));
  1187. }
  1188. if (subsystem_id) {
  1189. _FDT(fdt_setprop_cell(fdt, offset, "subsystem-id", subsystem_id));
  1190. }
  1191. if (subsystem_vendor_id) {
  1192. _FDT(fdt_setprop_cell(fdt, offset, "subsystem-vendor-id",
  1193. subsystem_vendor_id));
  1194. }
  1195. _FDT(fdt_setprop_cell(fdt, offset, "cache-line-size", cache_line_size));
  1196. /* the following fdt cells are masked off the pci status register */
  1197. _FDT(fdt_setprop_cell(fdt, offset, "devsel-speed",
  1198. PCI_STATUS_DEVSEL_MASK & pci_status));
  1199. if (pci_status & PCI_STATUS_FAST_BACK) {
  1200. _FDT(fdt_setprop(fdt, offset, "fast-back-to-back", NULL, 0));
  1201. }
  1202. if (pci_status & PCI_STATUS_66MHZ) {
  1203. _FDT(fdt_setprop(fdt, offset, "66mhz-capable", NULL, 0));
  1204. }
  1205. if (pci_status & PCI_STATUS_UDF) {
  1206. _FDT(fdt_setprop(fdt, offset, "udf-supported", NULL, 0));
  1207. }
  1208. loc_code = spapr_phb_get_loc_code(sphb, dev);
  1209. _FDT(fdt_setprop_string(fdt, offset, "ibm,loc-code", loc_code));
  1210. g_free(loc_code);
  1211. if (drc) {
  1212. _FDT(fdt_setprop_cell(fdt, offset, "ibm,my-drc-index",
  1213. spapr_drc_index(drc)));
  1214. }
  1215. if (msi_present(dev)) {
  1216. uint32_t max_msi = msi_nr_vectors_allocated(dev);
  1217. if (max_msi) {
  1218. _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi", max_msi));
  1219. }
  1220. }
  1221. if (msix_present(dev)) {
  1222. uint32_t max_msix = dev->msix_entries_nr;
  1223. if (max_msix) {
  1224. _FDT(fdt_setprop_cell(fdt, offset, "ibm,req#msi-x", max_msix));
  1225. }
  1226. }
  1227. populate_resource_props(dev, &rp);
  1228. _FDT(fdt_setprop(fdt, offset, "reg", (uint8_t *)rp.reg, rp.reg_len));
  1229. if (sphb->pcie_ecs && pci_is_express(dev)) {
  1230. _FDT(fdt_setprop_cell(fdt, offset, "ibm,pci-config-space-type", 0x1));
  1231. }
  1232. if (!IS_PCI_BRIDGE(dev)) {
  1233. /* Properties only for non-bridges */
  1234. uint32_t min_grant = pci_default_read_config(dev, PCI_MIN_GNT, 1);
  1235. uint32_t max_latency = pci_default_read_config(dev, PCI_MAX_LAT, 1);
  1236. _FDT(fdt_setprop_cell(fdt, offset, "min-grant", min_grant));
  1237. _FDT(fdt_setprop_cell(fdt, offset, "max-latency", max_latency));
  1238. return offset;
  1239. } else {
  1240. PCIBus *sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(dev));
  1241. return spapr_dt_pci_bus(sphb, sec_bus, fdt, offset);
  1242. }
  1243. }
  1244. /* Callback to be called during DRC release. */
  1245. void spapr_phb_remove_pci_device_cb(DeviceState *dev)
  1246. {
  1247. HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
  1248. hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
  1249. object_unparent(OBJECT(dev));
  1250. }
  1251. int spapr_pci_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
  1252. void *fdt, int *fdt_start_offset, Error **errp)
  1253. {
  1254. HotplugHandler *plug_handler = qdev_get_hotplug_handler(drc->dev);
  1255. SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(plug_handler);
  1256. PCIDevice *pdev = PCI_DEVICE(drc->dev);
  1257. *fdt_start_offset = spapr_dt_pci_device(sphb, pdev, fdt, 0);
  1258. return 0;
  1259. }
  1260. static void spapr_pci_bridge_plug(SpaprPhbState *phb,
  1261. PCIBridge *bridge)
  1262. {
  1263. PCIBus *bus = pci_bridge_get_sec_bus(bridge);
  1264. add_drcs(phb, bus);
  1265. }
  1266. /* Returns non-zero if the value of "chassis_nr" is already in use */
  1267. static int check_chassis_nr(Object *obj, void *opaque)
  1268. {
  1269. int new_chassis_nr =
  1270. object_property_get_uint(opaque, "chassis_nr", &error_abort);
  1271. int chassis_nr =
  1272. object_property_get_uint(obj, "chassis_nr", NULL);
  1273. if (!object_dynamic_cast(obj, TYPE_PCI_BRIDGE)) {
  1274. return 0;
  1275. }
  1276. /* Skip unsupported bridge types */
  1277. if (!chassis_nr) {
  1278. return 0;
  1279. }
  1280. /* Skip self */
  1281. if (obj == opaque) {
  1282. return 0;
  1283. }
  1284. return chassis_nr == new_chassis_nr;
  1285. }
  1286. static bool bridge_has_valid_chassis_nr(Object *bridge, Error **errp)
  1287. {
  1288. int chassis_nr =
  1289. object_property_get_uint(bridge, "chassis_nr", NULL);
  1290. /*
  1291. * slotid_cap_init() already ensures that "chassis_nr" isn't null for
  1292. * standard PCI bridges, so this really tells if "chassis_nr" is present
  1293. * or not.
  1294. */
  1295. if (!chassis_nr) {
  1296. error_setg(errp, "PCI Bridge lacks a \"chassis_nr\" property");
  1297. error_append_hint(errp, "Try -device pci-bridge instead.\n");
  1298. return false;
  1299. }
  1300. /* We want unique values for "chassis_nr" */
  1301. if (object_child_foreach_recursive(object_get_root(), check_chassis_nr,
  1302. bridge)) {
  1303. error_setg(errp, "Bridge chassis %d already in use", chassis_nr);
  1304. return false;
  1305. }
  1306. return true;
  1307. }
  1308. static void spapr_pci_pre_plug(HotplugHandler *plug_handler,
  1309. DeviceState *plugged_dev, Error **errp)
  1310. {
  1311. SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
  1312. PCIDevice *pdev = PCI_DEVICE(plugged_dev);
  1313. SpaprDrc *drc = drc_from_dev(phb, pdev);
  1314. PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev)));
  1315. uint32_t slotnr = PCI_SLOT(pdev->devfn);
  1316. if (IS_PCI_BRIDGE(plugged_dev)) {
  1317. if (!bridge_has_valid_chassis_nr(OBJECT(plugged_dev), errp)) {
  1318. return;
  1319. }
  1320. }
  1321. /* Following the QEMU convention used for PCIe multifunction
  1322. * hotplug, we do not allow functions to be hotplugged to a
  1323. * slot that already has function 0 present
  1324. */
  1325. if (plugged_dev->hotplugged &&
  1326. !pci_is_vf(pdev) &&
  1327. bus->devices[PCI_DEVFN(slotnr, 0)] &&
  1328. PCI_FUNC(pdev->devfn) != 0) {
  1329. error_setg(errp, "PCI: slot %d function 0 already occupied by %s,"
  1330. " additional functions can no longer be exposed to guest.",
  1331. slotnr, bus->devices[PCI_DEVFN(slotnr, 0)]->name);
  1332. }
  1333. if (drc && drc->dev) {
  1334. error_setg(errp, "PCI: slot %d already occupied by %s", slotnr,
  1335. pci_get_function_0(PCI_DEVICE(drc->dev))->name);
  1336. return;
  1337. }
  1338. }
  1339. static void spapr_pci_plug(HotplugHandler *plug_handler,
  1340. DeviceState *plugged_dev, Error **errp)
  1341. {
  1342. SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
  1343. PCIDevice *pdev = PCI_DEVICE(plugged_dev);
  1344. SpaprDrc *drc = drc_from_dev(phb, pdev);
  1345. uint32_t slotnr = PCI_SLOT(pdev->devfn);
  1346. /*
  1347. * If DR or the PCI device is disabled we don't need to do anything
  1348. * in the case of hotplug or coldplug callbacks.
  1349. */
  1350. if (!pdev->enabled) {
  1351. return;
  1352. }
  1353. g_assert(drc);
  1354. if (IS_PCI_BRIDGE(plugged_dev)) {
  1355. spapr_pci_bridge_plug(phb, PCI_BRIDGE(plugged_dev));
  1356. }
  1357. /* spapr_pci_pre_plug() already checked the DRC is attachable */
  1358. spapr_drc_attach(drc, DEVICE(pdev));
  1359. /* If this is function 0, signal hotplug for all the device functions.
  1360. * Otherwise defer sending the hotplug event.
  1361. */
  1362. if (!spapr_drc_hotplugged(plugged_dev)) {
  1363. spapr_drc_reset(drc);
  1364. } else if (PCI_FUNC(pdev->devfn) == 0) {
  1365. int i;
  1366. uint8_t chassis = chassis_from_bus(pci_get_bus(pdev));
  1367. for (i = 0; i < 8; i++) {
  1368. SpaprDrc *func_drc;
  1369. SpaprDrcClass *func_drck;
  1370. SpaprDREntitySense state;
  1371. func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i));
  1372. func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
  1373. state = func_drck->dr_entity_sense(func_drc);
  1374. if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
  1375. spapr_hotplug_req_add_by_index(func_drc);
  1376. }
  1377. }
  1378. }
  1379. }
  1380. static void spapr_pci_bridge_unplug(SpaprPhbState *phb,
  1381. PCIBridge *bridge)
  1382. {
  1383. PCIBus *bus = pci_bridge_get_sec_bus(bridge);
  1384. remove_drcs(phb, bus);
  1385. }
  1386. static void spapr_pci_unplug(HotplugHandler *plug_handler,
  1387. DeviceState *plugged_dev, Error **errp)
  1388. {
  1389. SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
  1390. /* some version guests do not wait for completion of a device
  1391. * cleanup (generally done asynchronously by the kernel) before
  1392. * signaling to QEMU that the device is safe, but instead sleep
  1393. * for some 'safe' period of time. unfortunately on a busy host
  1394. * this sleep isn't guaranteed to be long enough, resulting in
  1395. * bad things like IRQ lines being left asserted during final
  1396. * device removal. to deal with this we call reset just prior
  1397. * to finalizing the device, which will put the device back into
  1398. * an 'idle' state, as the device cleanup code expects.
  1399. */
  1400. pci_device_reset(PCI_DEVICE(plugged_dev));
  1401. if (IS_PCI_BRIDGE(plugged_dev)) {
  1402. spapr_pci_bridge_unplug(phb, PCI_BRIDGE(plugged_dev));
  1403. return;
  1404. }
  1405. qdev_unrealize(plugged_dev);
  1406. }
  1407. static void spapr_pci_unplug_request(HotplugHandler *plug_handler,
  1408. DeviceState *plugged_dev, Error **errp)
  1409. {
  1410. SpaprPhbState *phb = SPAPR_PCI_HOST_BRIDGE(DEVICE(plug_handler));
  1411. PCIDevice *pdev = PCI_DEVICE(plugged_dev);
  1412. SpaprDrc *drc = drc_from_dev(phb, pdev);
  1413. g_assert(drc);
  1414. if (!drc->dev) {
  1415. return;
  1416. }
  1417. g_assert(drc->dev == plugged_dev);
  1418. if (!spapr_drc_unplug_requested(drc)) {
  1419. uint32_t slotnr = PCI_SLOT(pdev->devfn);
  1420. SpaprDrc *func_drc;
  1421. SpaprDrcClass *func_drck;
  1422. SpaprDREntitySense state;
  1423. int i;
  1424. uint8_t chassis = chassis_from_bus(pci_get_bus(pdev));
  1425. if (IS_PCI_BRIDGE(plugged_dev)) {
  1426. error_setg(errp, "PCI: Hot unplug of PCI bridges not supported");
  1427. return;
  1428. }
  1429. if (object_property_get_uint(OBJECT(pdev), "nvlink2-tgt", NULL)) {
  1430. error_setg(errp, "PCI: Cannot unplug NVLink2 devices");
  1431. return;
  1432. }
  1433. /* ensure any other present functions are pending unplug */
  1434. if (PCI_FUNC(pdev->devfn) == 0) {
  1435. for (i = 1; i < 8; i++) {
  1436. func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i));
  1437. func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
  1438. state = func_drck->dr_entity_sense(func_drc);
  1439. if (state == SPAPR_DR_ENTITY_SENSE_PRESENT
  1440. && !spapr_drc_unplug_requested(func_drc)) {
  1441. /*
  1442. * Attempting to remove function 0 of a multifunction
  1443. * device will will cascade into removing all child
  1444. * functions, even if their unplug weren't requested
  1445. * beforehand.
  1446. */
  1447. spapr_drc_unplug_request(func_drc);
  1448. }
  1449. }
  1450. }
  1451. spapr_drc_unplug_request(drc);
  1452. /* if this isn't func 0, defer unplug event. otherwise signal removal
  1453. * for all present functions
  1454. */
  1455. if (PCI_FUNC(pdev->devfn) == 0) {
  1456. for (i = 7; i >= 0; i--) {
  1457. func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i));
  1458. func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc);
  1459. state = func_drck->dr_entity_sense(func_drc);
  1460. if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {
  1461. spapr_hotplug_req_remove_by_index(func_drc);
  1462. }
  1463. }
  1464. }
  1465. } else {
  1466. error_setg(errp,
  1467. "PCI device unplug already in progress for device %s",
  1468. drc->dev->id);
  1469. }
  1470. }
  1471. static void spapr_phb_finalizefn(Object *obj)
  1472. {
  1473. SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(obj);
  1474. g_free(sphb->dtbusname);
  1475. sphb->dtbusname = NULL;
  1476. }
  1477. static void spapr_phb_unrealize(DeviceState *dev)
  1478. {
  1479. SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
  1480. SysBusDevice *s = SYS_BUS_DEVICE(dev);
  1481. PCIHostState *phb = PCI_HOST_BRIDGE(s);
  1482. SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(phb);
  1483. SpaprTceTable *tcet;
  1484. int i;
  1485. const unsigned windows_supported = spapr_phb_windows_supported(sphb);
  1486. if (sphb->msi) {
  1487. g_hash_table_unref(sphb->msi);
  1488. sphb->msi = NULL;
  1489. }
  1490. /*
  1491. * Remove IO/MMIO subregions and aliases, rest should get cleaned
  1492. * via PHB's unrealize->object_finalize
  1493. */
  1494. for (i = windows_supported - 1; i >= 0; i--) {
  1495. tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[i]);
  1496. if (tcet) {
  1497. memory_region_del_subregion(&sphb->iommu_root,
  1498. spapr_tce_get_iommu(tcet));
  1499. }
  1500. }
  1501. remove_drcs(sphb, phb->bus);
  1502. for (i = PCI_NUM_PINS - 1; i >= 0; i--) {
  1503. if (sphb->lsi_table[i].irq) {
  1504. spapr_irq_free(spapr, sphb->lsi_table[i].irq, 1);
  1505. sphb->lsi_table[i].irq = 0;
  1506. }
  1507. }
  1508. QLIST_REMOVE(sphb, list);
  1509. memory_region_del_subregion(&sphb->iommu_root, &sphb->msiwindow);
  1510. /*
  1511. * An attached PCI device may have memory listeners, eg. VFIO PCI. We have
  1512. * unmapped all sections. Remove the listeners now, before destroying the
  1513. * address space.
  1514. */
  1515. address_space_remove_listeners(&sphb->iommu_as);
  1516. address_space_destroy(&sphb->iommu_as);
  1517. qbus_set_hotplug_handler(BUS(phb->bus), NULL);
  1518. pci_unregister_root_bus(phb->bus);
  1519. memory_region_del_subregion(get_system_memory(), &sphb->iowindow);
  1520. if (sphb->mem64_win_pciaddr != (hwaddr)-1) {
  1521. memory_region_del_subregion(get_system_memory(), &sphb->mem64window);
  1522. }
  1523. memory_region_del_subregion(get_system_memory(), &sphb->mem32window);
  1524. }
  1525. static void spapr_phb_destroy_msi(gpointer opaque)
  1526. {
  1527. SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
  1528. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  1529. SpaprPciMsi *msi = opaque;
  1530. if (!smc->legacy_irq_allocation) {
  1531. spapr_irq_msi_free(spapr, msi->first_irq, msi->num);
  1532. }
  1533. spapr_irq_free(spapr, msi->first_irq, msi->num);
  1534. g_free(msi);
  1535. }
  1536. static void spapr_phb_realize(DeviceState *dev, Error **errp)
  1537. {
  1538. ERRP_GUARD();
  1539. /* We don't use SPAPR_MACHINE() in order to exit gracefully if the user
  1540. * tries to add a sPAPR PHB to a non-pseries machine.
  1541. */
  1542. SpaprMachineState *spapr =
  1543. (SpaprMachineState *) object_dynamic_cast(qdev_get_machine(),
  1544. TYPE_SPAPR_MACHINE);
  1545. SpaprMachineClass *smc = spapr ? SPAPR_MACHINE_GET_CLASS(spapr) : NULL;
  1546. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1547. SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(sbd);
  1548. PCIHostState *phb = PCI_HOST_BRIDGE(sbd);
  1549. MachineState *ms = MACHINE(spapr);
  1550. char *namebuf;
  1551. int i;
  1552. PCIBus *bus;
  1553. uint64_t msi_window_size = 4096;
  1554. SpaprTceTable *tcet;
  1555. const unsigned windows_supported = spapr_phb_windows_supported(sphb);
  1556. if (!spapr) {
  1557. error_setg(errp, TYPE_SPAPR_PCI_HOST_BRIDGE " needs a pseries machine");
  1558. return;
  1559. }
  1560. assert(sphb->index != (uint32_t)-1); /* checked in spapr_phb_pre_plug() */
  1561. if (sphb->mem_win_size > SPAPR_PCI_MEM32_WIN_SIZE) {
  1562. error_setg(errp, "32-bit memory window of size 0x%"HWADDR_PRIx
  1563. " (max 2 GiB)", sphb->mem_win_size);
  1564. return;
  1565. }
  1566. /* 64-bit window defaults to identity mapping */
  1567. sphb->mem64_win_pciaddr = sphb->mem64_win_addr;
  1568. if (spapr_pci_find_phb(spapr, sphb->buid)) {
  1569. SpaprPhbState *s;
  1570. error_setg(errp, "PCI host bridges must have unique indexes");
  1571. error_append_hint(errp, "The following indexes are already in use:");
  1572. QLIST_FOREACH(s, &spapr->phbs, list) {
  1573. error_append_hint(errp, " %d", s->index);
  1574. }
  1575. error_append_hint(errp, "\nTry another value for the index property\n");
  1576. return;
  1577. }
  1578. if (sphb->numa_node != -1 &&
  1579. (sphb->numa_node >= MAX_NODES ||
  1580. !ms->numa_state->nodes[sphb->numa_node].present)) {
  1581. error_setg(errp, "Invalid NUMA node ID for PCI host bridge");
  1582. return;
  1583. }
  1584. sphb->dtbusname = g_strdup_printf("pci@%" PRIx64, sphb->buid);
  1585. /* Initialize memory regions */
  1586. namebuf = g_strdup_printf("%s.mmio", sphb->dtbusname);
  1587. memory_region_init(&sphb->memspace, OBJECT(sphb), namebuf, UINT64_MAX);
  1588. g_free(namebuf);
  1589. namebuf = g_strdup_printf("%s.mmio32-alias", sphb->dtbusname);
  1590. memory_region_init_alias(&sphb->mem32window, OBJECT(sphb),
  1591. namebuf, &sphb->memspace,
  1592. SPAPR_PCI_MEM_WIN_BUS_OFFSET, sphb->mem_win_size);
  1593. g_free(namebuf);
  1594. memory_region_add_subregion(get_system_memory(), sphb->mem_win_addr,
  1595. &sphb->mem32window);
  1596. if (sphb->mem64_win_size != 0) {
  1597. namebuf = g_strdup_printf("%s.mmio64-alias", sphb->dtbusname);
  1598. memory_region_init_alias(&sphb->mem64window, OBJECT(sphb),
  1599. namebuf, &sphb->memspace,
  1600. sphb->mem64_win_pciaddr, sphb->mem64_win_size);
  1601. g_free(namebuf);
  1602. memory_region_add_subregion(get_system_memory(),
  1603. sphb->mem64_win_addr,
  1604. &sphb->mem64window);
  1605. }
  1606. /* Initialize IO regions */
  1607. namebuf = g_strdup_printf("%s.io", sphb->dtbusname);
  1608. memory_region_init(&sphb->iospace, OBJECT(sphb),
  1609. namebuf, SPAPR_PCI_IO_WIN_SIZE);
  1610. g_free(namebuf);
  1611. namebuf = g_strdup_printf("%s.io-alias", sphb->dtbusname);
  1612. memory_region_init_alias(&sphb->iowindow, OBJECT(sphb), namebuf,
  1613. &sphb->iospace, 0, SPAPR_PCI_IO_WIN_SIZE);
  1614. g_free(namebuf);
  1615. memory_region_add_subregion(get_system_memory(), sphb->io_win_addr,
  1616. &sphb->iowindow);
  1617. bus = pci_register_root_bus(dev, NULL,
  1618. pci_spapr_set_irq, pci_swizzle_map_irq_fn, sphb,
  1619. &sphb->memspace, &sphb->iospace,
  1620. PCI_DEVFN(0, 0), PCI_NUM_PINS,
  1621. TYPE_PCI_BUS);
  1622. /*
  1623. * Despite resembling a vanilla PCI bus in most ways, the PAPR
  1624. * para-virtualized PCI bus *does* permit PCI-E extended config
  1625. * space access
  1626. */
  1627. if (sphb->pcie_ecs) {
  1628. bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE;
  1629. }
  1630. phb->bus = bus;
  1631. qbus_set_hotplug_handler(BUS(phb->bus), OBJECT(sphb));
  1632. /*
  1633. * Initialize PHB address space.
  1634. * By default there will be at least one subregion for default
  1635. * 32bit DMA window.
  1636. * Later the guest might want to create another DMA window
  1637. * which will become another memory subregion.
  1638. */
  1639. namebuf = g_strdup_printf("%s.iommu-root", sphb->dtbusname);
  1640. memory_region_init(&sphb->iommu_root, OBJECT(sphb),
  1641. namebuf, UINT64_MAX);
  1642. g_free(namebuf);
  1643. address_space_init(&sphb->iommu_as, &sphb->iommu_root,
  1644. sphb->dtbusname);
  1645. /*
  1646. * As MSI/MSIX interrupts trigger by writing at MSI/MSIX vectors,
  1647. * we need to allocate some memory to catch those writes coming
  1648. * from msi_notify()/msix_notify().
  1649. * As MSIMessage:addr is going to be the same and MSIMessage:data
  1650. * is going to be a VIRQ number, 4 bytes of the MSI MR will only
  1651. * be used.
  1652. *
  1653. * For KVM we want to ensure that this memory is a full page so that
  1654. * our memory slot is of page size granularity.
  1655. */
  1656. if (kvm_enabled()) {
  1657. msi_window_size = qemu_real_host_page_size();
  1658. }
  1659. memory_region_init_io(&sphb->msiwindow, OBJECT(sphb), &spapr_msi_ops, spapr,
  1660. "msi", msi_window_size);
  1661. memory_region_add_subregion(&sphb->iommu_root, SPAPR_PCI_MSI_WINDOW,
  1662. &sphb->msiwindow);
  1663. pci_setup_iommu(bus, &spapr_iommu_ops, sphb);
  1664. pci_bus_set_route_irq_fn(bus, spapr_route_intx_pin_to_irq);
  1665. QLIST_INSERT_HEAD(&spapr->phbs, sphb, list);
  1666. /* Initialize the LSI table */
  1667. for (i = 0; i < PCI_NUM_PINS; i++) {
  1668. int irq = SPAPR_IRQ_PCI_LSI + sphb->index * PCI_NUM_PINS + i;
  1669. if (smc->legacy_irq_allocation) {
  1670. irq = spapr_irq_findone(spapr, errp);
  1671. if (irq < 0) {
  1672. error_prepend(errp, "can't allocate LSIs: ");
  1673. /*
  1674. * Older machines will never support PHB hotplug, ie, this is an
  1675. * init only path and QEMU will terminate. No need to rollback.
  1676. */
  1677. return;
  1678. }
  1679. }
  1680. if (spapr_irq_claim(spapr, irq, true, errp) < 0) {
  1681. error_prepend(errp, "can't allocate LSIs: ");
  1682. goto unrealize;
  1683. }
  1684. sphb->lsi_table[i].irq = irq;
  1685. }
  1686. /* allocate connectors for child PCI devices */
  1687. add_drcs(sphb, phb->bus);
  1688. /* DMA setup */
  1689. for (i = 0; i < windows_supported; ++i) {
  1690. tcet = spapr_tce_new_table(DEVICE(sphb), sphb->dma_liobn[i]);
  1691. if (!tcet) {
  1692. error_setg(errp, "Creating window#%d failed for %s",
  1693. i, sphb->dtbusname);
  1694. goto unrealize;
  1695. }
  1696. memory_region_add_subregion(&sphb->iommu_root, 0,
  1697. spapr_tce_get_iommu(tcet));
  1698. }
  1699. sphb->msi = g_hash_table_new_full(g_int_hash, g_int_equal, g_free,
  1700. spapr_phb_destroy_msi);
  1701. return;
  1702. unrealize:
  1703. spapr_phb_unrealize(dev);
  1704. }
  1705. static int spapr_phb_children_reset(Object *child, void *opaque)
  1706. {
  1707. DeviceState *dev = (DeviceState *) object_dynamic_cast(child, TYPE_DEVICE);
  1708. if (dev) {
  1709. device_cold_reset(dev);
  1710. }
  1711. return 0;
  1712. }
  1713. void spapr_phb_dma_reset(SpaprPhbState *sphb)
  1714. {
  1715. int i;
  1716. SpaprTceTable *tcet;
  1717. for (i = 0; i < SPAPR_PCI_DMA_MAX_WINDOWS; ++i) {
  1718. tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[i]);
  1719. if (tcet && tcet->nb_table) {
  1720. spapr_tce_table_disable(tcet);
  1721. }
  1722. }
  1723. /* Register default 32bit DMA window */
  1724. tcet = spapr_tce_find_by_liobn(sphb->dma_liobn[0]);
  1725. spapr_tce_table_enable(tcet, SPAPR_TCE_PAGE_SHIFT, sphb->dma_win_addr,
  1726. sphb->dma_win_size >> SPAPR_TCE_PAGE_SHIFT);
  1727. tcet->def_win = true;
  1728. }
  1729. static void spapr_phb_reset(DeviceState *qdev)
  1730. {
  1731. SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(qdev);
  1732. spapr_phb_dma_reset(sphb);
  1733. /* Reset the IOMMU state */
  1734. object_child_foreach(OBJECT(qdev), spapr_phb_children_reset, NULL);
  1735. if (spapr_phb_eeh_available(SPAPR_PCI_HOST_BRIDGE(qdev))) {
  1736. spapr_phb_vfio_reset(qdev);
  1737. }
  1738. g_hash_table_remove_all(sphb->msi);
  1739. }
  1740. static const Property spapr_phb_properties[] = {
  1741. DEFINE_PROP_UINT32("index", SpaprPhbState, index, -1),
  1742. DEFINE_PROP_UINT64("mem_win_size", SpaprPhbState, mem_win_size,
  1743. SPAPR_PCI_MEM32_WIN_SIZE),
  1744. DEFINE_PROP_UINT64("mem64_win_size", SpaprPhbState, mem64_win_size,
  1745. SPAPR_PCI_MEM64_WIN_SIZE),
  1746. DEFINE_PROP_UINT64("io_win_size", SpaprPhbState, io_win_size,
  1747. SPAPR_PCI_IO_WIN_SIZE),
  1748. /* Default DMA window is 0..1GB */
  1749. DEFINE_PROP_UINT64("dma_win_addr", SpaprPhbState, dma_win_addr, 0),
  1750. DEFINE_PROP_UINT64("dma_win_size", SpaprPhbState, dma_win_size, 0x40000000),
  1751. DEFINE_PROP_UINT64("dma64_win_addr", SpaprPhbState, dma64_win_addr,
  1752. 0x800000000000000ULL),
  1753. DEFINE_PROP_BOOL("ddw", SpaprPhbState, ddw_enabled, true),
  1754. DEFINE_PROP_UINT64("pgsz", SpaprPhbState, page_size_mask,
  1755. (1ULL << 12) | (1ULL << 16)
  1756. | (1ULL << 21) | (1ULL << 24)),
  1757. DEFINE_PROP_UINT32("numa_node", SpaprPhbState, numa_node, -1),
  1758. DEFINE_PROP_BOOL("pcie-extended-configuration-space", SpaprPhbState,
  1759. pcie_ecs, true),
  1760. DEFINE_PROP_BOOL("pre-5.1-associativity", SpaprPhbState,
  1761. pre_5_1_assoc, false),
  1762. };
  1763. static const VMStateDescription vmstate_spapr_pci_lsi = {
  1764. .name = "spapr_pci/lsi",
  1765. .version_id = 1,
  1766. .minimum_version_id = 1,
  1767. .fields = (const VMStateField[]) {
  1768. VMSTATE_UINT32_EQUAL(irq, SpaprPciLsi, NULL),
  1769. VMSTATE_END_OF_LIST()
  1770. },
  1771. };
  1772. static const VMStateDescription vmstate_spapr_pci_msi = {
  1773. .name = "spapr_pci/msi",
  1774. .version_id = 1,
  1775. .minimum_version_id = 1,
  1776. .fields = (const VMStateField []) {
  1777. VMSTATE_UINT32(key, SpaprPciMsiMig),
  1778. VMSTATE_UINT32(value.first_irq, SpaprPciMsiMig),
  1779. VMSTATE_UINT32(value.num, SpaprPciMsiMig),
  1780. VMSTATE_END_OF_LIST()
  1781. },
  1782. };
  1783. static int spapr_pci_pre_save(void *opaque)
  1784. {
  1785. SpaprPhbState *sphb = opaque;
  1786. GHashTableIter iter;
  1787. gpointer key, value;
  1788. int i;
  1789. g_free(sphb->msi_devs);
  1790. sphb->msi_devs = NULL;
  1791. sphb->msi_devs_num = g_hash_table_size(sphb->msi);
  1792. if (!sphb->msi_devs_num) {
  1793. return 0;
  1794. }
  1795. sphb->msi_devs = g_new(SpaprPciMsiMig, sphb->msi_devs_num);
  1796. g_hash_table_iter_init(&iter, sphb->msi);
  1797. for (i = 0; g_hash_table_iter_next(&iter, &key, &value); ++i) {
  1798. sphb->msi_devs[i].key = *(uint32_t *) key;
  1799. sphb->msi_devs[i].value = *(SpaprPciMsi *) value;
  1800. }
  1801. return 0;
  1802. }
  1803. static int spapr_pci_post_save(void *opaque)
  1804. {
  1805. SpaprPhbState *sphb = opaque;
  1806. g_free(sphb->msi_devs);
  1807. sphb->msi_devs = NULL;
  1808. sphb->msi_devs_num = 0;
  1809. return 0;
  1810. }
  1811. static int spapr_pci_post_load(void *opaque, int version_id)
  1812. {
  1813. SpaprPhbState *sphb = opaque;
  1814. gpointer key, value;
  1815. int i;
  1816. for (i = 0; i < sphb->msi_devs_num; ++i) {
  1817. key = g_memdup2(&sphb->msi_devs[i].key, sizeof(sphb->msi_devs[i].key));
  1818. value = g_memdup2(&sphb->msi_devs[i].value,
  1819. sizeof(sphb->msi_devs[i].value));
  1820. g_hash_table_insert(sphb->msi, key, value);
  1821. }
  1822. g_free(sphb->msi_devs);
  1823. sphb->msi_devs = NULL;
  1824. sphb->msi_devs_num = 0;
  1825. return 0;
  1826. }
  1827. static const VMStateDescription vmstate_spapr_pci = {
  1828. .name = "spapr_pci",
  1829. .version_id = 2,
  1830. .minimum_version_id = 2,
  1831. .pre_save = spapr_pci_pre_save,
  1832. .post_save = spapr_pci_post_save,
  1833. .post_load = spapr_pci_post_load,
  1834. .fields = (const VMStateField[]) {
  1835. VMSTATE_UINT64_EQUAL(buid, SpaprPhbState, NULL),
  1836. VMSTATE_STRUCT_ARRAY(lsi_table, SpaprPhbState, PCI_NUM_PINS, 0,
  1837. vmstate_spapr_pci_lsi, SpaprPciLsi),
  1838. VMSTATE_INT32(msi_devs_num, SpaprPhbState),
  1839. VMSTATE_STRUCT_VARRAY_ALLOC(msi_devs, SpaprPhbState, msi_devs_num, 0,
  1840. vmstate_spapr_pci_msi, SpaprPciMsiMig),
  1841. VMSTATE_END_OF_LIST()
  1842. },
  1843. };
  1844. static const char *spapr_phb_root_bus_path(PCIHostState *host_bridge,
  1845. PCIBus *rootbus)
  1846. {
  1847. SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(host_bridge);
  1848. return sphb->dtbusname;
  1849. }
  1850. static void spapr_phb_class_init(ObjectClass *klass, void *data)
  1851. {
  1852. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
  1853. DeviceClass *dc = DEVICE_CLASS(klass);
  1854. HotplugHandlerClass *hp = HOTPLUG_HANDLER_CLASS(klass);
  1855. hc->root_bus_path = spapr_phb_root_bus_path;
  1856. dc->realize = spapr_phb_realize;
  1857. dc->unrealize = spapr_phb_unrealize;
  1858. device_class_set_props(dc, spapr_phb_properties);
  1859. device_class_set_legacy_reset(dc, spapr_phb_reset);
  1860. dc->vmsd = &vmstate_spapr_pci;
  1861. /* Supported by TYPE_SPAPR_MACHINE */
  1862. dc->user_creatable = true;
  1863. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  1864. hp->pre_plug = spapr_pci_pre_plug;
  1865. hp->plug = spapr_pci_plug;
  1866. hp->unplug = spapr_pci_unplug;
  1867. hp->unplug_request = spapr_pci_unplug_request;
  1868. }
  1869. static const TypeInfo spapr_phb_info = {
  1870. .name = TYPE_SPAPR_PCI_HOST_BRIDGE,
  1871. .parent = TYPE_PCI_HOST_BRIDGE,
  1872. .instance_size = sizeof(SpaprPhbState),
  1873. .instance_finalize = spapr_phb_finalizefn,
  1874. .class_init = spapr_phb_class_init,
  1875. .interfaces = (InterfaceInfo[]) {
  1876. { TYPE_HOTPLUG_HANDLER },
  1877. { }
  1878. }
  1879. };
  1880. static void spapr_phb_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev,
  1881. void *opaque)
  1882. {
  1883. unsigned int *bus_no = opaque;
  1884. PCIBus *sec_bus = NULL;
  1885. if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
  1886. PCI_HEADER_TYPE_BRIDGE)) {
  1887. return;
  1888. }
  1889. (*bus_no)++;
  1890. pci_default_write_config(pdev, PCI_PRIMARY_BUS, pci_dev_bus_num(pdev), 1);
  1891. pci_default_write_config(pdev, PCI_SECONDARY_BUS, *bus_no, 1);
  1892. pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
  1893. sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
  1894. if (!sec_bus) {
  1895. return;
  1896. }
  1897. pci_for_each_device_under_bus(sec_bus, spapr_phb_pci_enumerate_bridge,
  1898. bus_no);
  1899. pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, *bus_no, 1);
  1900. }
  1901. static void spapr_phb_pci_enumerate(SpaprPhbState *phb)
  1902. {
  1903. PCIBus *bus = PCI_HOST_BRIDGE(phb)->bus;
  1904. unsigned int bus_no = 0;
  1905. pci_for_each_device_under_bus(bus, spapr_phb_pci_enumerate_bridge,
  1906. &bus_no);
  1907. }
  1908. int spapr_dt_phb(SpaprMachineState *spapr, SpaprPhbState *phb,
  1909. uint32_t intc_phandle, void *fdt, int *node_offset)
  1910. {
  1911. int bus_off, i, j, ret;
  1912. uint32_t bus_range[] = { cpu_to_be32(0), cpu_to_be32(0xff) };
  1913. struct {
  1914. uint32_t hi;
  1915. uint64_t child;
  1916. uint64_t parent;
  1917. uint64_t size;
  1918. } QEMU_PACKED ranges[] = {
  1919. {
  1920. cpu_to_be32(b_ss(1)), cpu_to_be64(0),
  1921. cpu_to_be64(phb->io_win_addr),
  1922. cpu_to_be64(memory_region_size(&phb->iospace)),
  1923. },
  1924. {
  1925. cpu_to_be32(b_ss(2)), cpu_to_be64(SPAPR_PCI_MEM_WIN_BUS_OFFSET),
  1926. cpu_to_be64(phb->mem_win_addr),
  1927. cpu_to_be64(phb->mem_win_size),
  1928. },
  1929. {
  1930. cpu_to_be32(b_ss(3)), cpu_to_be64(phb->mem64_win_pciaddr),
  1931. cpu_to_be64(phb->mem64_win_addr),
  1932. cpu_to_be64(phb->mem64_win_size),
  1933. },
  1934. };
  1935. const unsigned sizeof_ranges =
  1936. (phb->mem64_win_size ? 3 : 2) * sizeof(ranges[0]);
  1937. uint64_t bus_reg[] = { cpu_to_be64(phb->buid), 0 };
  1938. uint32_t interrupt_map_mask[] = {
  1939. cpu_to_be32(b_ddddd(-1)|b_fff(0)), 0x0, 0x0, cpu_to_be32(-1)};
  1940. uint32_t interrupt_map[PCI_SLOT_MAX * PCI_NUM_PINS][7];
  1941. uint32_t ddw_applicable[] = {
  1942. cpu_to_be32(RTAS_IBM_QUERY_PE_DMA_WINDOW),
  1943. cpu_to_be32(RTAS_IBM_CREATE_PE_DMA_WINDOW),
  1944. cpu_to_be32(RTAS_IBM_REMOVE_PE_DMA_WINDOW)
  1945. };
  1946. uint32_t ddw_extensions[] = {
  1947. cpu_to_be32(2),
  1948. cpu_to_be32(RTAS_IBM_RESET_PE_DMA_WINDOW),
  1949. cpu_to_be32(1), /* 1: ibm,query-pe-dma-window 6 outputs, PAPR 2.8 */
  1950. };
  1951. SpaprTceTable *tcet;
  1952. SpaprDrc *drc;
  1953. /* Start populating the FDT */
  1954. _FDT(bus_off = fdt_add_subnode(fdt, 0, phb->dtbusname));
  1955. if (node_offset) {
  1956. *node_offset = bus_off;
  1957. }
  1958. /* Write PHB properties */
  1959. _FDT(fdt_setprop_string(fdt, bus_off, "device_type", "pci"));
  1960. _FDT(fdt_setprop_string(fdt, bus_off, "compatible", "IBM,Logical_PHB"));
  1961. _FDT(fdt_setprop_cell(fdt, bus_off, "#interrupt-cells", 0x1));
  1962. _FDT(fdt_setprop(fdt, bus_off, "used-by-rtas", NULL, 0));
  1963. _FDT(fdt_setprop(fdt, bus_off, "bus-range", &bus_range, sizeof(bus_range)));
  1964. _FDT(fdt_setprop(fdt, bus_off, "ranges", &ranges, sizeof_ranges));
  1965. _FDT(fdt_setprop(fdt, bus_off, "reg", &bus_reg, sizeof(bus_reg)));
  1966. _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pci-config-space-type", 0x1));
  1967. _FDT(fdt_setprop_cell(fdt, bus_off, "ibm,pe-total-#msi",
  1968. spapr_irq_nr_msis(spapr)));
  1969. /* Dynamic DMA window */
  1970. if (phb->ddw_enabled) {
  1971. _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-applicable", &ddw_applicable,
  1972. sizeof(ddw_applicable)));
  1973. _FDT(fdt_setprop(fdt, bus_off, "ibm,ddw-extensions",
  1974. &ddw_extensions, sizeof(ddw_extensions)));
  1975. }
  1976. /* Advertise NUMA via ibm,associativity */
  1977. if (phb->numa_node != -1) {
  1978. spapr_numa_write_associativity_dt(spapr, fdt, bus_off, phb->numa_node);
  1979. }
  1980. /* Build the interrupt-map, this must matches what is done
  1981. * in pci_swizzle_map_irq_fn
  1982. */
  1983. _FDT(fdt_setprop(fdt, bus_off, "interrupt-map-mask",
  1984. &interrupt_map_mask, sizeof(interrupt_map_mask)));
  1985. for (i = 0; i < PCI_SLOT_MAX; i++) {
  1986. for (j = 0; j < PCI_NUM_PINS; j++) {
  1987. uint32_t *irqmap = interrupt_map[i*PCI_NUM_PINS + j];
  1988. int lsi_num = pci_swizzle(i, j);
  1989. irqmap[0] = cpu_to_be32(b_ddddd(i)|b_fff(0));
  1990. irqmap[1] = 0;
  1991. irqmap[2] = 0;
  1992. irqmap[3] = cpu_to_be32(j+1);
  1993. irqmap[4] = cpu_to_be32(intc_phandle);
  1994. spapr_dt_irq(&irqmap[5], phb->lsi_table[lsi_num].irq, true);
  1995. }
  1996. }
  1997. /* Write interrupt map */
  1998. _FDT(fdt_setprop(fdt, bus_off, "interrupt-map", &interrupt_map,
  1999. sizeof(interrupt_map)));
  2000. tcet = spapr_tce_find_by_liobn(phb->dma_liobn[0]);
  2001. if (!tcet) {
  2002. return -1;
  2003. }
  2004. spapr_dma_dt(fdt, bus_off, "ibm,dma-window",
  2005. tcet->liobn, tcet->bus_offset,
  2006. tcet->nb_table << tcet->page_shift);
  2007. drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PHB, phb->index);
  2008. if (drc) {
  2009. uint32_t drc_index = cpu_to_be32(spapr_drc_index(drc));
  2010. _FDT(fdt_setprop(fdt, bus_off, "ibm,my-drc-index", &drc_index,
  2011. sizeof(drc_index)));
  2012. }
  2013. /* Walk the bridges and program the bus numbers*/
  2014. spapr_phb_pci_enumerate(phb);
  2015. _FDT(fdt_setprop_cell(fdt, bus_off, "qemu,phb-enumerated", 0x1));
  2016. /* Walk the bridge and subordinate buses */
  2017. ret = spapr_dt_pci_bus(phb, PCI_HOST_BRIDGE(phb)->bus, fdt, bus_off);
  2018. if (ret < 0) {
  2019. return ret;
  2020. }
  2021. return 0;
  2022. }
  2023. void spapr_pci_rtas_init(void)
  2024. {
  2025. spapr_rtas_register(RTAS_READ_PCI_CONFIG, "read-pci-config",
  2026. rtas_read_pci_config);
  2027. spapr_rtas_register(RTAS_WRITE_PCI_CONFIG, "write-pci-config",
  2028. rtas_write_pci_config);
  2029. spapr_rtas_register(RTAS_IBM_READ_PCI_CONFIG, "ibm,read-pci-config",
  2030. rtas_ibm_read_pci_config);
  2031. spapr_rtas_register(RTAS_IBM_WRITE_PCI_CONFIG, "ibm,write-pci-config",
  2032. rtas_ibm_write_pci_config);
  2033. if (msi_nonbroken) {
  2034. spapr_rtas_register(RTAS_IBM_QUERY_INTERRUPT_SOURCE_NUMBER,
  2035. "ibm,query-interrupt-source-number",
  2036. rtas_ibm_query_interrupt_source_number);
  2037. spapr_rtas_register(RTAS_IBM_CHANGE_MSI, "ibm,change-msi",
  2038. rtas_ibm_change_msi);
  2039. }
  2040. spapr_rtas_register(RTAS_IBM_SET_EEH_OPTION,
  2041. "ibm,set-eeh-option",
  2042. rtas_ibm_set_eeh_option);
  2043. spapr_rtas_register(RTAS_IBM_GET_CONFIG_ADDR_INFO2,
  2044. "ibm,get-config-addr-info2",
  2045. rtas_ibm_get_config_addr_info2);
  2046. spapr_rtas_register(RTAS_IBM_READ_SLOT_RESET_STATE2,
  2047. "ibm,read-slot-reset-state2",
  2048. rtas_ibm_read_slot_reset_state2);
  2049. spapr_rtas_register(RTAS_IBM_SET_SLOT_RESET,
  2050. "ibm,set-slot-reset",
  2051. rtas_ibm_set_slot_reset);
  2052. spapr_rtas_register(RTAS_IBM_CONFIGURE_PE,
  2053. "ibm,configure-pe",
  2054. rtas_ibm_configure_pe);
  2055. spapr_rtas_register(RTAS_IBM_SLOT_ERROR_DETAIL,
  2056. "ibm,slot-error-detail",
  2057. rtas_ibm_slot_error_detail);
  2058. }
  2059. static void spapr_pci_register_types(void)
  2060. {
  2061. type_register_static(&spapr_phb_info);
  2062. }
  2063. type_init(spapr_pci_register_types)
  2064. static int spapr_switch_one_vga(DeviceState *dev, void *opaque)
  2065. {
  2066. bool be = *(bool *)opaque;
  2067. if (object_dynamic_cast(OBJECT(dev), "VGA")
  2068. || object_dynamic_cast(OBJECT(dev), "secondary-vga")
  2069. || object_dynamic_cast(OBJECT(dev), "bochs-display")
  2070. || object_dynamic_cast(OBJECT(dev), "virtio-vga")) {
  2071. object_property_set_bool(OBJECT(dev), "big-endian-framebuffer", be,
  2072. &error_abort);
  2073. }
  2074. return 0;
  2075. }
  2076. void spapr_pci_switch_vga(SpaprMachineState *spapr, bool big_endian)
  2077. {
  2078. SpaprPhbState *sphb;
  2079. /*
  2080. * For backward compatibility with existing guests, we switch
  2081. * the endianness of the VGA controller when changing the guest
  2082. * interrupt mode
  2083. */
  2084. QLIST_FOREACH(sphb, &spapr->phbs, list) {
  2085. BusState *bus = &PCI_HOST_BRIDGE(sphb)->bus->qbus;
  2086. qbus_walk_children(bus, spapr_switch_one_vga, NULL, NULL, NULL,
  2087. &big_endian);
  2088. }
  2089. }