spapr_pci.c 79 KB

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