s390-pci-bus.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /*
  2. * s390 PCI BUS
  3. *
  4. * Copyright 2014 IBM Corp.
  5. * Author(s): Frank Blaschka <frank.blaschka@de.ibm.com>
  6. * Hong Bo Li <lihbbj@cn.ibm.com>
  7. * Yi Min Zhao <zyimin@cn.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or (at
  10. * your option) any later version. See the COPYING file in the top-level
  11. * directory.
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qapi/error.h"
  15. #include "qapi/visitor.h"
  16. #include "hw/s390x/s390-pci-bus.h"
  17. #include "hw/s390x/s390-pci-inst.h"
  18. #include "hw/s390x/s390-pci-kvm.h"
  19. #include "hw/s390x/s390-pci-vfio.h"
  20. #include "hw/s390x/s390-virtio-ccw.h"
  21. #include "hw/boards.h"
  22. #include "hw/pci/pci_bus.h"
  23. #include "hw/qdev-properties.h"
  24. #include "hw/pci/pci_bridge.h"
  25. #include "hw/pci/msi.h"
  26. #include "qemu/error-report.h"
  27. #include "qemu/module.h"
  28. #include "system/reset.h"
  29. #include "system/runstate.h"
  30. #include "trace.h"
  31. S390pciState *s390_get_phb(void)
  32. {
  33. static S390pciState *phb;
  34. if (!phb) {
  35. phb = S390_PCI_HOST_BRIDGE(
  36. object_resolve_path(TYPE_S390_PCI_HOST_BRIDGE, NULL));
  37. assert(phb != NULL);
  38. }
  39. return phb;
  40. }
  41. int pci_chsc_sei_nt2_get_event(void *res)
  42. {
  43. ChscSeiNt2Res *nt2_res = (ChscSeiNt2Res *)res;
  44. PciCcdfAvail *accdf;
  45. PciCcdfErr *eccdf;
  46. int rc = 1;
  47. SeiContainer *sei_cont;
  48. S390pciState *s = s390_get_phb();
  49. sei_cont = QTAILQ_FIRST(&s->pending_sei);
  50. if (sei_cont) {
  51. QTAILQ_REMOVE(&s->pending_sei, sei_cont, link);
  52. nt2_res->nt = 2;
  53. nt2_res->cc = sei_cont->cc;
  54. nt2_res->length = cpu_to_be16(sizeof(ChscSeiNt2Res));
  55. switch (sei_cont->cc) {
  56. case 1: /* error event */
  57. eccdf = (PciCcdfErr *)nt2_res->ccdf;
  58. eccdf->fid = cpu_to_be32(sei_cont->fid);
  59. eccdf->fh = cpu_to_be32(sei_cont->fh);
  60. eccdf->e = cpu_to_be32(sei_cont->e);
  61. eccdf->faddr = cpu_to_be64(sei_cont->faddr);
  62. eccdf->pec = cpu_to_be16(sei_cont->pec);
  63. break;
  64. case 2: /* availability event */
  65. accdf = (PciCcdfAvail *)nt2_res->ccdf;
  66. accdf->fid = cpu_to_be32(sei_cont->fid);
  67. accdf->fh = cpu_to_be32(sei_cont->fh);
  68. accdf->pec = cpu_to_be16(sei_cont->pec);
  69. break;
  70. default:
  71. abort();
  72. }
  73. g_free(sei_cont);
  74. rc = 0;
  75. }
  76. return rc;
  77. }
  78. int pci_chsc_sei_nt2_have_event(void)
  79. {
  80. S390pciState *s = s390_get_phb();
  81. return !QTAILQ_EMPTY(&s->pending_sei);
  82. }
  83. S390PCIBusDevice *s390_pci_find_next_avail_dev(S390pciState *s,
  84. S390PCIBusDevice *pbdev)
  85. {
  86. S390PCIBusDevice *ret = pbdev ? QTAILQ_NEXT(pbdev, link) :
  87. QTAILQ_FIRST(&s->zpci_devs);
  88. while (ret && ret->state == ZPCI_FS_RESERVED) {
  89. ret = QTAILQ_NEXT(ret, link);
  90. }
  91. return ret;
  92. }
  93. S390PCIBusDevice *s390_pci_find_dev_by_fid(S390pciState *s, uint32_t fid)
  94. {
  95. S390PCIBusDevice *pbdev;
  96. QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
  97. if (pbdev->fid == fid) {
  98. return pbdev;
  99. }
  100. }
  101. return NULL;
  102. }
  103. void s390_pci_sclp_configure(SCCB *sccb)
  104. {
  105. IoaCfgSccb *psccb = (IoaCfgSccb *)sccb;
  106. S390PCIBusDevice *pbdev = s390_pci_find_dev_by_fid(s390_get_phb(),
  107. be32_to_cpu(psccb->aid));
  108. uint16_t rc;
  109. if (!pbdev) {
  110. trace_s390_pci_sclp_nodev("configure", be32_to_cpu(psccb->aid));
  111. rc = SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED;
  112. goto out;
  113. }
  114. switch (pbdev->state) {
  115. case ZPCI_FS_RESERVED:
  116. rc = SCLP_RC_ADAPTER_IN_RESERVED_STATE;
  117. break;
  118. case ZPCI_FS_STANDBY:
  119. pbdev->state = ZPCI_FS_DISABLED;
  120. rc = SCLP_RC_NORMAL_COMPLETION;
  121. break;
  122. default:
  123. rc = SCLP_RC_NO_ACTION_REQUIRED;
  124. }
  125. out:
  126. psccb->header.response_code = cpu_to_be16(rc);
  127. }
  128. static void s390_pci_shutdown_notifier(Notifier *n, void *opaque)
  129. {
  130. S390PCIBusDevice *pbdev = container_of(n, S390PCIBusDevice,
  131. shutdown_notifier);
  132. pci_device_reset(pbdev->pdev);
  133. }
  134. static void s390_pci_perform_unplug(S390PCIBusDevice *pbdev)
  135. {
  136. HotplugHandler *hotplug_ctrl;
  137. if (pbdev->pft == ZPCI_PFT_ISM) {
  138. notifier_remove(&pbdev->shutdown_notifier);
  139. }
  140. /* Unplug the PCI device */
  141. if (pbdev->pdev) {
  142. DeviceState *pdev = DEVICE(pbdev->pdev);
  143. hotplug_ctrl = qdev_get_hotplug_handler(pdev);
  144. hotplug_handler_unplug(hotplug_ctrl, pdev, &error_abort);
  145. object_unparent(OBJECT(pdev));
  146. }
  147. /* Unplug the zPCI device */
  148. hotplug_ctrl = qdev_get_hotplug_handler(DEVICE(pbdev));
  149. hotplug_handler_unplug(hotplug_ctrl, DEVICE(pbdev), &error_abort);
  150. object_unparent(OBJECT(pbdev));
  151. }
  152. void s390_pci_sclp_deconfigure(SCCB *sccb)
  153. {
  154. IoaCfgSccb *psccb = (IoaCfgSccb *)sccb;
  155. S390PCIBusDevice *pbdev = s390_pci_find_dev_by_fid(s390_get_phb(),
  156. be32_to_cpu(psccb->aid));
  157. uint16_t rc;
  158. if (!pbdev) {
  159. trace_s390_pci_sclp_nodev("deconfigure", be32_to_cpu(psccb->aid));
  160. rc = SCLP_RC_ADAPTER_ID_NOT_RECOGNIZED;
  161. goto out;
  162. }
  163. switch (pbdev->state) {
  164. case ZPCI_FS_RESERVED:
  165. rc = SCLP_RC_ADAPTER_IN_RESERVED_STATE;
  166. break;
  167. case ZPCI_FS_STANDBY:
  168. rc = SCLP_RC_NO_ACTION_REQUIRED;
  169. break;
  170. default:
  171. if (pbdev->interp && (pbdev->fh & FH_MASK_ENABLE)) {
  172. /* Interpreted devices were using interrupt forwarding */
  173. s390_pci_kvm_aif_disable(pbdev);
  174. } else if (pbdev->summary_ind) {
  175. pci_dereg_irqs(pbdev);
  176. }
  177. if (pbdev->iommu->enabled) {
  178. pci_dereg_ioat(pbdev->iommu);
  179. }
  180. pbdev->state = ZPCI_FS_STANDBY;
  181. rc = SCLP_RC_NORMAL_COMPLETION;
  182. if (pbdev->unplug_requested) {
  183. s390_pci_perform_unplug(pbdev);
  184. }
  185. }
  186. out:
  187. psccb->header.response_code = cpu_to_be16(rc);
  188. }
  189. static S390PCIBusDevice *s390_pci_find_dev_by_uid(S390pciState *s, uint16_t uid)
  190. {
  191. S390PCIBusDevice *pbdev;
  192. QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
  193. if (pbdev->uid == uid) {
  194. return pbdev;
  195. }
  196. }
  197. return NULL;
  198. }
  199. S390PCIBusDevice *s390_pci_find_dev_by_target(S390pciState *s,
  200. const char *target)
  201. {
  202. S390PCIBusDevice *pbdev;
  203. if (!target) {
  204. return NULL;
  205. }
  206. QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
  207. if (!strcmp(pbdev->target, target)) {
  208. return pbdev;
  209. }
  210. }
  211. return NULL;
  212. }
  213. static S390PCIBusDevice *s390_pci_find_dev_by_pci(S390pciState *s,
  214. PCIDevice *pci_dev)
  215. {
  216. S390PCIBusDevice *pbdev;
  217. if (!pci_dev) {
  218. return NULL;
  219. }
  220. QTAILQ_FOREACH(pbdev, &s->zpci_devs, link) {
  221. if (pbdev->pdev == pci_dev) {
  222. return pbdev;
  223. }
  224. }
  225. return NULL;
  226. }
  227. S390PCIBusDevice *s390_pci_find_dev_by_idx(S390pciState *s, uint32_t idx)
  228. {
  229. return g_hash_table_lookup(s->zpci_table, &idx);
  230. }
  231. S390PCIBusDevice *s390_pci_find_dev_by_fh(S390pciState *s, uint32_t fh)
  232. {
  233. uint32_t idx = FH_MASK_INDEX & fh;
  234. S390PCIBusDevice *pbdev = s390_pci_find_dev_by_idx(s, idx);
  235. if (pbdev && pbdev->fh == fh) {
  236. return pbdev;
  237. }
  238. return NULL;
  239. }
  240. static void s390_pci_generate_event(uint8_t cc, uint16_t pec, uint32_t fh,
  241. uint32_t fid, uint64_t faddr, uint32_t e)
  242. {
  243. SeiContainer *sei_cont;
  244. S390pciState *s = s390_get_phb();
  245. sei_cont = g_new0(SeiContainer, 1);
  246. sei_cont->fh = fh;
  247. sei_cont->fid = fid;
  248. sei_cont->cc = cc;
  249. sei_cont->pec = pec;
  250. sei_cont->faddr = faddr;
  251. sei_cont->e = e;
  252. QTAILQ_INSERT_TAIL(&s->pending_sei, sei_cont, link);
  253. css_generate_css_crws(0);
  254. }
  255. static void s390_pci_generate_plug_event(uint16_t pec, uint32_t fh,
  256. uint32_t fid)
  257. {
  258. s390_pci_generate_event(2, pec, fh, fid, 0, 0);
  259. }
  260. void s390_pci_generate_error_event(uint16_t pec, uint32_t fh, uint32_t fid,
  261. uint64_t faddr, uint32_t e)
  262. {
  263. s390_pci_generate_event(1, pec, fh, fid, faddr, e);
  264. }
  265. static void s390_pci_set_irq(void *opaque, int irq, int level)
  266. {
  267. /* nothing to do */
  268. }
  269. static int s390_pci_map_irq(PCIDevice *pci_dev, int irq_num)
  270. {
  271. /* nothing to do */
  272. return 0;
  273. }
  274. static uint64_t s390_pci_get_table_origin(uint64_t iota)
  275. {
  276. return iota & ~ZPCI_IOTA_RTTO_FLAG;
  277. }
  278. static unsigned int calc_rtx(dma_addr_t ptr)
  279. {
  280. return ((unsigned long) ptr >> ZPCI_RT_SHIFT) & ZPCI_INDEX_MASK;
  281. }
  282. static unsigned int calc_sx(dma_addr_t ptr)
  283. {
  284. return ((unsigned long) ptr >> ZPCI_ST_SHIFT) & ZPCI_INDEX_MASK;
  285. }
  286. static unsigned int calc_px(dma_addr_t ptr)
  287. {
  288. return ((unsigned long) ptr >> TARGET_PAGE_BITS) & ZPCI_PT_MASK;
  289. }
  290. static uint64_t get_rt_sto(uint64_t entry)
  291. {
  292. return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_RTX)
  293. ? (entry & ZPCI_RTE_ADDR_MASK)
  294. : 0;
  295. }
  296. static uint64_t get_st_pto(uint64_t entry)
  297. {
  298. return ((entry & ZPCI_TABLE_TYPE_MASK) == ZPCI_TABLE_TYPE_SX)
  299. ? (entry & ZPCI_STE_ADDR_MASK)
  300. : 0;
  301. }
  302. static bool rt_entry_isvalid(uint64_t entry)
  303. {
  304. return (entry & ZPCI_TABLE_VALID_MASK) == ZPCI_TABLE_VALID;
  305. }
  306. static bool pt_entry_isvalid(uint64_t entry)
  307. {
  308. return (entry & ZPCI_PTE_VALID_MASK) == ZPCI_PTE_VALID;
  309. }
  310. static bool entry_isprotected(uint64_t entry)
  311. {
  312. return (entry & ZPCI_TABLE_PROT_MASK) == ZPCI_TABLE_PROTECTED;
  313. }
  314. /* ett is expected table type, -1 page table, 0 segment table, 1 region table */
  315. static uint64_t get_table_index(uint64_t iova, int8_t ett)
  316. {
  317. switch (ett) {
  318. case ZPCI_ETT_PT:
  319. return calc_px(iova);
  320. case ZPCI_ETT_ST:
  321. return calc_sx(iova);
  322. case ZPCI_ETT_RT:
  323. return calc_rtx(iova);
  324. }
  325. return -1;
  326. }
  327. static bool entry_isvalid(uint64_t entry, int8_t ett)
  328. {
  329. switch (ett) {
  330. case ZPCI_ETT_PT:
  331. return pt_entry_isvalid(entry);
  332. case ZPCI_ETT_ST:
  333. case ZPCI_ETT_RT:
  334. return rt_entry_isvalid(entry);
  335. }
  336. return false;
  337. }
  338. /* Return true if address translation is done */
  339. static bool translate_iscomplete(uint64_t entry, int8_t ett)
  340. {
  341. switch (ett) {
  342. case 0:
  343. return (entry & ZPCI_TABLE_FC) ? true : false;
  344. case 1:
  345. return false;
  346. }
  347. return true;
  348. }
  349. static uint64_t get_frame_size(int8_t ett)
  350. {
  351. switch (ett) {
  352. case ZPCI_ETT_PT:
  353. return 1ULL << 12;
  354. case ZPCI_ETT_ST:
  355. return 1ULL << 20;
  356. case ZPCI_ETT_RT:
  357. return 1ULL << 31;
  358. }
  359. return 0;
  360. }
  361. static uint64_t get_next_table_origin(uint64_t entry, int8_t ett)
  362. {
  363. switch (ett) {
  364. case ZPCI_ETT_PT:
  365. return entry & ZPCI_PTE_ADDR_MASK;
  366. case ZPCI_ETT_ST:
  367. return get_st_pto(entry);
  368. case ZPCI_ETT_RT:
  369. return get_rt_sto(entry);
  370. }
  371. return 0;
  372. }
  373. /**
  374. * table_translate: do translation within one table and return the following
  375. * table origin
  376. *
  377. * @entry: the entry being translated, the result is stored in this.
  378. * @to: the address of table origin.
  379. * @ett: expected table type, 1 region table, 0 segment table and -1 page table.
  380. * @error: error code
  381. */
  382. static uint64_t table_translate(S390IOTLBEntry *entry, uint64_t to, int8_t ett,
  383. uint16_t *error)
  384. {
  385. uint64_t tx, te, nto = 0;
  386. uint16_t err = 0;
  387. tx = get_table_index(entry->iova, ett);
  388. te = address_space_ldq(&address_space_memory, to + tx * sizeof(uint64_t),
  389. MEMTXATTRS_UNSPECIFIED, NULL);
  390. if (!te) {
  391. err = ERR_EVENT_INVALTE;
  392. goto out;
  393. }
  394. if (!entry_isvalid(te, ett)) {
  395. entry->perm &= IOMMU_NONE;
  396. goto out;
  397. }
  398. if (ett == ZPCI_ETT_RT && ((te & ZPCI_TABLE_LEN_RTX) != ZPCI_TABLE_LEN_RTX
  399. || te & ZPCI_TABLE_OFFSET_MASK)) {
  400. err = ERR_EVENT_INVALTL;
  401. goto out;
  402. }
  403. nto = get_next_table_origin(te, ett);
  404. if (!nto) {
  405. err = ERR_EVENT_TT;
  406. goto out;
  407. }
  408. if (entry_isprotected(te)) {
  409. entry->perm &= IOMMU_RO;
  410. } else {
  411. entry->perm &= IOMMU_RW;
  412. }
  413. if (translate_iscomplete(te, ett)) {
  414. switch (ett) {
  415. case ZPCI_ETT_PT:
  416. entry->translated_addr = te & ZPCI_PTE_ADDR_MASK;
  417. break;
  418. case ZPCI_ETT_ST:
  419. entry->translated_addr = (te & ZPCI_SFAA_MASK) |
  420. (entry->iova & ~ZPCI_SFAA_MASK);
  421. break;
  422. }
  423. nto = 0;
  424. }
  425. out:
  426. if (err) {
  427. entry->perm = IOMMU_NONE;
  428. *error = err;
  429. }
  430. entry->len = get_frame_size(ett);
  431. return nto;
  432. }
  433. uint16_t s390_guest_io_table_walk(uint64_t g_iota, hwaddr addr,
  434. S390IOTLBEntry *entry)
  435. {
  436. uint64_t to = s390_pci_get_table_origin(g_iota);
  437. int8_t ett = 1;
  438. uint16_t error = 0;
  439. entry->iova = addr & TARGET_PAGE_MASK;
  440. entry->translated_addr = 0;
  441. entry->perm = IOMMU_RW;
  442. if (entry_isprotected(g_iota)) {
  443. entry->perm &= IOMMU_RO;
  444. }
  445. while (to) {
  446. to = table_translate(entry, to, ett--, &error);
  447. }
  448. return error;
  449. }
  450. static IOMMUTLBEntry s390_translate_iommu(IOMMUMemoryRegion *mr, hwaddr addr,
  451. IOMMUAccessFlags flag, int iommu_idx)
  452. {
  453. S390PCIIOMMU *iommu = container_of(mr, S390PCIIOMMU, iommu_mr);
  454. S390IOTLBEntry *entry;
  455. uint64_t iova = addr & TARGET_PAGE_MASK;
  456. uint16_t error = 0;
  457. IOMMUTLBEntry ret = {
  458. .target_as = &address_space_memory,
  459. .iova = 0,
  460. .translated_addr = 0,
  461. .addr_mask = ~(hwaddr)0,
  462. .perm = IOMMU_NONE,
  463. };
  464. switch (iommu->pbdev->state) {
  465. case ZPCI_FS_ENABLED:
  466. case ZPCI_FS_BLOCKED:
  467. if (!iommu->enabled) {
  468. return ret;
  469. }
  470. break;
  471. default:
  472. return ret;
  473. }
  474. trace_s390_pci_iommu_xlate(addr);
  475. if (addr < iommu->pba || addr > iommu->pal) {
  476. error = ERR_EVENT_OORANGE;
  477. goto err;
  478. }
  479. entry = g_hash_table_lookup(iommu->iotlb, &iova);
  480. if (entry) {
  481. ret.iova = entry->iova;
  482. ret.translated_addr = entry->translated_addr;
  483. ret.addr_mask = entry->len - 1;
  484. ret.perm = entry->perm;
  485. } else {
  486. ret.iova = iova;
  487. ret.addr_mask = ~TARGET_PAGE_MASK;
  488. ret.perm = IOMMU_NONE;
  489. }
  490. if (flag != IOMMU_NONE && !(flag & ret.perm)) {
  491. error = ERR_EVENT_TPROTE;
  492. }
  493. err:
  494. if (error) {
  495. iommu->pbdev->state = ZPCI_FS_ERROR;
  496. s390_pci_generate_error_event(error, iommu->pbdev->fh,
  497. iommu->pbdev->fid, addr, 0);
  498. }
  499. return ret;
  500. }
  501. static void s390_pci_iommu_replay(IOMMUMemoryRegion *iommu,
  502. IOMMUNotifier *notifier)
  503. {
  504. /* It's impossible to plug a pci device on s390x that already has iommu
  505. * mappings which need to be replayed, that is due to the "one iommu per
  506. * zpci device" construct. But when we support migration of vfio-pci
  507. * devices in future, we need to revisit this.
  508. */
  509. return;
  510. }
  511. static S390PCIIOMMU *s390_pci_get_iommu(S390pciState *s, PCIBus *bus,
  512. int devfn)
  513. {
  514. uint64_t key = (uintptr_t)bus;
  515. S390PCIIOMMUTable *table = g_hash_table_lookup(s->iommu_table, &key);
  516. S390PCIIOMMU *iommu;
  517. if (!table) {
  518. table = g_new0(S390PCIIOMMUTable, 1);
  519. table->key = key;
  520. g_hash_table_insert(s->iommu_table, &table->key, table);
  521. }
  522. iommu = table->iommu[PCI_SLOT(devfn)];
  523. if (!iommu) {
  524. iommu = S390_PCI_IOMMU(object_new(TYPE_S390_PCI_IOMMU));
  525. char *mr_name = g_strdup_printf("iommu-root-%02x:%02x.%01x",
  526. pci_bus_num(bus),
  527. PCI_SLOT(devfn),
  528. PCI_FUNC(devfn));
  529. char *as_name = g_strdup_printf("iommu-pci-%02x:%02x.%01x",
  530. pci_bus_num(bus),
  531. PCI_SLOT(devfn),
  532. PCI_FUNC(devfn));
  533. memory_region_init(&iommu->mr, OBJECT(iommu), mr_name, UINT64_MAX);
  534. address_space_init(&iommu->as, &iommu->mr, as_name);
  535. iommu->iotlb = g_hash_table_new_full(g_int64_hash, g_int64_equal,
  536. NULL, g_free);
  537. table->iommu[PCI_SLOT(devfn)] = iommu;
  538. g_free(mr_name);
  539. g_free(as_name);
  540. }
  541. return iommu;
  542. }
  543. static AddressSpace *s390_pci_dma_iommu(PCIBus *bus, void *opaque, int devfn)
  544. {
  545. S390pciState *s = opaque;
  546. S390PCIIOMMU *iommu = s390_pci_get_iommu(s, bus, devfn);
  547. return &iommu->as;
  548. }
  549. static const PCIIOMMUOps s390_iommu_ops = {
  550. .get_address_space = s390_pci_dma_iommu,
  551. };
  552. static uint8_t set_ind_atomic(uint64_t ind_loc, uint8_t to_be_set)
  553. {
  554. uint8_t expected, actual;
  555. hwaddr len = 1;
  556. /* avoid multiple fetches */
  557. uint8_t volatile *ind_addr;
  558. ind_addr = cpu_physical_memory_map(ind_loc, &len, true);
  559. if (!ind_addr) {
  560. s390_pci_generate_error_event(ERR_EVENT_AIRERR, 0, 0, 0, 0);
  561. return -1;
  562. }
  563. actual = *ind_addr;
  564. do {
  565. expected = actual;
  566. actual = qatomic_cmpxchg(ind_addr, expected, expected | to_be_set);
  567. } while (actual != expected);
  568. cpu_physical_memory_unmap((void *)ind_addr, len, 1, len);
  569. return actual;
  570. }
  571. static void s390_msi_ctrl_write(void *opaque, hwaddr addr, uint64_t data,
  572. unsigned int size)
  573. {
  574. S390PCIBusDevice *pbdev = opaque;
  575. uint32_t vec = data & ZPCI_MSI_VEC_MASK;
  576. uint64_t ind_bit;
  577. uint32_t sum_bit;
  578. assert(pbdev);
  579. trace_s390_pci_msi_ctrl_write(data, pbdev->idx, vec);
  580. if (pbdev->state != ZPCI_FS_ENABLED) {
  581. return;
  582. }
  583. ind_bit = pbdev->routes.adapter.ind_offset;
  584. sum_bit = pbdev->routes.adapter.summary_offset;
  585. set_ind_atomic(pbdev->routes.adapter.ind_addr + (ind_bit + vec) / 8,
  586. 0x80 >> ((ind_bit + vec) % 8));
  587. if (!set_ind_atomic(pbdev->routes.adapter.summary_addr + sum_bit / 8,
  588. 0x80 >> (sum_bit % 8))) {
  589. css_adapter_interrupt(CSS_IO_ADAPTER_PCI, pbdev->isc);
  590. }
  591. }
  592. static uint64_t s390_msi_ctrl_read(void *opaque, hwaddr addr, unsigned size)
  593. {
  594. return 0xffffffff;
  595. }
  596. static const MemoryRegionOps s390_msi_ctrl_ops = {
  597. .write = s390_msi_ctrl_write,
  598. .read = s390_msi_ctrl_read,
  599. .endianness = DEVICE_LITTLE_ENDIAN,
  600. };
  601. void s390_pci_iommu_enable(S390PCIIOMMU *iommu)
  602. {
  603. /*
  604. * The iommu region is initialized against a 0-mapped address space,
  605. * so the smallest IOMMU region we can define runs from 0 to the end
  606. * of the PCI address space.
  607. */
  608. char *name = g_strdup_printf("iommu-s390-%04x", iommu->pbdev->uid);
  609. memory_region_init_iommu(&iommu->iommu_mr, sizeof(iommu->iommu_mr),
  610. TYPE_S390_IOMMU_MEMORY_REGION, OBJECT(&iommu->mr),
  611. name, iommu->pal + 1);
  612. iommu->enabled = true;
  613. memory_region_add_subregion(&iommu->mr, 0, MEMORY_REGION(&iommu->iommu_mr));
  614. g_free(name);
  615. }
  616. void s390_pci_iommu_direct_map_enable(S390PCIIOMMU *iommu)
  617. {
  618. MachineState *ms = MACHINE(qdev_get_machine());
  619. S390CcwMachineState *s390ms = S390_CCW_MACHINE(ms);
  620. /*
  621. * For direct-mapping we must map the entire guest address space. Rather
  622. * than using an iommu, create a memory region alias that maps GPA X to
  623. * IOVA X + SDMA. VFIO will handle pinning via its memory listener.
  624. */
  625. g_autofree char *name = g_strdup_printf("iommu-dm-s390-%04x",
  626. iommu->pbdev->uid);
  627. iommu->dm_mr = g_malloc0(sizeof(*iommu->dm_mr));
  628. memory_region_init_alias(iommu->dm_mr, OBJECT(&iommu->mr), name,
  629. get_system_memory(), 0,
  630. s390_get_memory_limit(s390ms));
  631. iommu->enabled = true;
  632. memory_region_add_subregion(&iommu->mr, iommu->pbdev->zpci_fn.sdma,
  633. iommu->dm_mr);
  634. }
  635. void s390_pci_iommu_disable(S390PCIIOMMU *iommu)
  636. {
  637. iommu->enabled = false;
  638. g_hash_table_remove_all(iommu->iotlb);
  639. if (iommu->dm_mr) {
  640. memory_region_del_subregion(&iommu->mr, iommu->dm_mr);
  641. object_unparent(OBJECT(iommu->dm_mr));
  642. g_free(iommu->dm_mr);
  643. iommu->dm_mr = NULL;
  644. } else {
  645. memory_region_del_subregion(&iommu->mr,
  646. MEMORY_REGION(&iommu->iommu_mr));
  647. object_unparent(OBJECT(&iommu->iommu_mr));
  648. }
  649. }
  650. static void s390_pci_iommu_free(S390pciState *s, PCIBus *bus, int32_t devfn)
  651. {
  652. uint64_t key = (uintptr_t)bus;
  653. S390PCIIOMMUTable *table = g_hash_table_lookup(s->iommu_table, &key);
  654. S390PCIIOMMU *iommu = table ? table->iommu[PCI_SLOT(devfn)] : NULL;
  655. if (!table || !iommu) {
  656. return;
  657. }
  658. table->iommu[PCI_SLOT(devfn)] = NULL;
  659. g_hash_table_destroy(iommu->iotlb);
  660. /*
  661. * An attached PCI device may have memory listeners, eg. VFIO PCI.
  662. * The associated subregion will already have been unmapped in
  663. * s390_pci_iommu_disable in response to the guest deconfigure request.
  664. * Remove the listeners now before destroying the address space.
  665. */
  666. address_space_remove_listeners(&iommu->as);
  667. address_space_destroy(&iommu->as);
  668. object_unparent(OBJECT(&iommu->mr));
  669. object_unparent(OBJECT(iommu));
  670. object_unref(OBJECT(iommu));
  671. }
  672. S390PCIGroup *s390_group_create(int id, int host_id)
  673. {
  674. S390PCIGroup *group;
  675. S390pciState *s = s390_get_phb();
  676. group = g_new0(S390PCIGroup, 1);
  677. group->id = id;
  678. group->host_id = host_id;
  679. QTAILQ_INSERT_TAIL(&s->zpci_groups, group, link);
  680. return group;
  681. }
  682. S390PCIGroup *s390_group_find(int id)
  683. {
  684. S390PCIGroup *group;
  685. S390pciState *s = s390_get_phb();
  686. QTAILQ_FOREACH(group, &s->zpci_groups, link) {
  687. if (group->id == id) {
  688. return group;
  689. }
  690. }
  691. return NULL;
  692. }
  693. S390PCIGroup *s390_group_find_host_sim(int host_id)
  694. {
  695. S390PCIGroup *group;
  696. S390pciState *s = s390_get_phb();
  697. QTAILQ_FOREACH(group, &s->zpci_groups, link) {
  698. if (group->id >= ZPCI_SIM_GRP_START && group->host_id == host_id) {
  699. return group;
  700. }
  701. }
  702. return NULL;
  703. }
  704. static void s390_pci_init_default_group(void)
  705. {
  706. S390PCIGroup *group;
  707. ClpRspQueryPciGrp *resgrp;
  708. group = s390_group_create(ZPCI_DEFAULT_FN_GRP, ZPCI_DEFAULT_FN_GRP);
  709. resgrp = &group->zpci_group;
  710. resgrp->fr = 1;
  711. resgrp->dasm = 0;
  712. resgrp->msia = ZPCI_MSI_ADDR;
  713. resgrp->mui = DEFAULT_MUI;
  714. resgrp->i = 128;
  715. resgrp->maxstbl = 128;
  716. resgrp->version = 0;
  717. resgrp->dtsm = ZPCI_DTSM;
  718. }
  719. static void set_pbdev_info(S390PCIBusDevice *pbdev)
  720. {
  721. pbdev->zpci_fn.sdma = ZPCI_SDMA_ADDR;
  722. pbdev->zpci_fn.edma = ZPCI_EDMA_ADDR;
  723. pbdev->zpci_fn.pchid = 0;
  724. pbdev->zpci_fn.pfgid = ZPCI_DEFAULT_FN_GRP;
  725. pbdev->zpci_fn.fid = pbdev->fid;
  726. pbdev->zpci_fn.uid = pbdev->uid;
  727. pbdev->pci_group = s390_group_find(ZPCI_DEFAULT_FN_GRP);
  728. }
  729. static void s390_pcihost_realize(DeviceState *dev, Error **errp)
  730. {
  731. PCIBus *b;
  732. BusState *bus;
  733. PCIHostState *phb = PCI_HOST_BRIDGE(dev);
  734. S390pciState *s = S390_PCI_HOST_BRIDGE(dev);
  735. trace_s390_pcihost("realize");
  736. b = pci_register_root_bus(dev, NULL, s390_pci_set_irq, s390_pci_map_irq,
  737. NULL, get_system_memory(), get_system_io(), 0,
  738. 64, TYPE_PCI_BUS);
  739. pci_setup_iommu(b, &s390_iommu_ops, s);
  740. bus = BUS(b);
  741. qbus_set_hotplug_handler(bus, OBJECT(dev));
  742. phb->bus = b;
  743. s->bus = S390_PCI_BUS(qbus_new(TYPE_S390_PCI_BUS, dev, NULL));
  744. qbus_set_hotplug_handler(BUS(s->bus), OBJECT(dev));
  745. s->iommu_table = g_hash_table_new_full(g_int64_hash, g_int64_equal,
  746. NULL, g_free);
  747. s->zpci_table = g_hash_table_new_full(g_int_hash, g_int_equal, NULL, NULL);
  748. s->bus_no = 0;
  749. s->next_sim_grp = ZPCI_SIM_GRP_START;
  750. QTAILQ_INIT(&s->pending_sei);
  751. QTAILQ_INIT(&s->zpci_devs);
  752. QTAILQ_INIT(&s->zpci_dma_limit);
  753. QTAILQ_INIT(&s->zpci_groups);
  754. s390_pci_init_default_group();
  755. css_register_io_adapters(CSS_IO_ADAPTER_PCI, true, false,
  756. S390_ADAPTER_SUPPRESSIBLE, errp);
  757. }
  758. static void s390_pcihost_unrealize(DeviceState *dev)
  759. {
  760. S390PCIGroup *group;
  761. S390pciState *s = S390_PCI_HOST_BRIDGE(dev);
  762. while (!QTAILQ_EMPTY(&s->zpci_groups)) {
  763. group = QTAILQ_FIRST(&s->zpci_groups);
  764. QTAILQ_REMOVE(&s->zpci_groups, group, link);
  765. }
  766. }
  767. static int s390_pci_msix_init(S390PCIBusDevice *pbdev)
  768. {
  769. char *name;
  770. uint8_t pos;
  771. uint16_t ctrl;
  772. uint32_t table, pba;
  773. pos = pci_find_capability(pbdev->pdev, PCI_CAP_ID_MSIX);
  774. if (!pos) {
  775. return -1;
  776. }
  777. ctrl = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_FLAGS,
  778. pci_config_size(pbdev->pdev), sizeof(ctrl));
  779. table = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_TABLE,
  780. pci_config_size(pbdev->pdev), sizeof(table));
  781. pba = pci_host_config_read_common(pbdev->pdev, pos + PCI_MSIX_PBA,
  782. pci_config_size(pbdev->pdev), sizeof(pba));
  783. pbdev->msix.table_bar = table & PCI_MSIX_FLAGS_BIRMASK;
  784. pbdev->msix.table_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
  785. pbdev->msix.pba_bar = pba & PCI_MSIX_FLAGS_BIRMASK;
  786. pbdev->msix.pba_offset = pba & ~PCI_MSIX_FLAGS_BIRMASK;
  787. pbdev->msix.entries = (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
  788. name = g_strdup_printf("msix-s390-%04x", pbdev->uid);
  789. memory_region_init_io(&pbdev->msix_notify_mr, OBJECT(pbdev),
  790. &s390_msi_ctrl_ops, pbdev, name, TARGET_PAGE_SIZE);
  791. memory_region_add_subregion(&pbdev->iommu->mr,
  792. pbdev->pci_group->zpci_group.msia,
  793. &pbdev->msix_notify_mr);
  794. g_free(name);
  795. return 0;
  796. }
  797. static void s390_pci_msix_free(S390PCIBusDevice *pbdev)
  798. {
  799. if (pbdev->msix.entries == 0) {
  800. return;
  801. }
  802. memory_region_del_subregion(&pbdev->iommu->mr, &pbdev->msix_notify_mr);
  803. object_unparent(OBJECT(&pbdev->msix_notify_mr));
  804. }
  805. static S390PCIBusDevice *s390_pci_device_new(S390pciState *s,
  806. const char *target, Error **errp)
  807. {
  808. Error *local_err = NULL;
  809. DeviceState *dev;
  810. dev = qdev_try_new(TYPE_S390_PCI_DEVICE);
  811. if (!dev) {
  812. error_setg(errp, "zPCI device could not be created");
  813. return NULL;
  814. }
  815. if (!object_property_set_str(OBJECT(dev), "target", target, &local_err)) {
  816. object_unparent(OBJECT(dev));
  817. error_propagate_prepend(errp, local_err,
  818. "zPCI device could not be created: ");
  819. return NULL;
  820. }
  821. if (!qdev_realize_and_unref(dev, BUS(s->bus), &local_err)) {
  822. object_unparent(OBJECT(dev));
  823. error_propagate_prepend(errp, local_err,
  824. "zPCI device could not be created: ");
  825. return NULL;
  826. }
  827. return S390_PCI_DEVICE(dev);
  828. }
  829. static bool s390_pci_alloc_idx(S390pciState *s, S390PCIBusDevice *pbdev)
  830. {
  831. uint32_t idx;
  832. idx = s->next_idx;
  833. while (s390_pci_find_dev_by_idx(s, idx)) {
  834. idx = (idx + 1) & FH_MASK_INDEX;
  835. if (idx == s->next_idx) {
  836. return false;
  837. }
  838. }
  839. pbdev->idx = idx;
  840. return true;
  841. }
  842. static void s390_pcihost_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
  843. Error **errp)
  844. {
  845. S390pciState *s = S390_PCI_HOST_BRIDGE(hotplug_dev);
  846. if (!s390_has_feat(S390_FEAT_ZPCI)) {
  847. warn_report("Plugging a PCI/zPCI device without the 'zpci' CPU "
  848. "feature enabled; the guest will not be able to see/use "
  849. "this device");
  850. }
  851. if (object_dynamic_cast(OBJECT(dev), TYPE_S390_PCI_DEVICE)) {
  852. S390PCIBusDevice *pbdev = S390_PCI_DEVICE(dev);
  853. if (!s390_pci_alloc_idx(s, pbdev)) {
  854. error_setg(errp, "no slot for plugging zpci device");
  855. return;
  856. }
  857. }
  858. }
  859. static void s390_pci_update_subordinate(PCIDevice *dev, uint32_t nr)
  860. {
  861. uint32_t old_nr;
  862. pci_default_write_config(dev, PCI_SUBORDINATE_BUS, nr, 1);
  863. while (!pci_bus_is_root(pci_get_bus(dev))) {
  864. dev = pci_get_bus(dev)->parent_dev;
  865. old_nr = pci_default_read_config(dev, PCI_SUBORDINATE_BUS, 1);
  866. if (old_nr < nr) {
  867. pci_default_write_config(dev, PCI_SUBORDINATE_BUS, nr, 1);
  868. }
  869. }
  870. }
  871. static int s390_pci_interp_plug(S390pciState *s, S390PCIBusDevice *pbdev)
  872. {
  873. uint32_t idx, fh;
  874. if (!s390_pci_get_host_fh(pbdev, &fh)) {
  875. return -EPERM;
  876. }
  877. /*
  878. * The host device is already in an enabled state, but we always present
  879. * the initial device state to the guest as disabled (ZPCI_FS_DISABLED).
  880. * Therefore, mask off the enable bit from the passthrough handle until
  881. * the guest issues a CLP SET PCI FN later to enable the device.
  882. */
  883. pbdev->fh = fh & ~FH_MASK_ENABLE;
  884. /* Next, see if the idx is already in-use */
  885. idx = pbdev->fh & FH_MASK_INDEX;
  886. if (pbdev->idx != idx) {
  887. if (s390_pci_find_dev_by_idx(s, idx)) {
  888. return -EINVAL;
  889. }
  890. /*
  891. * Update the idx entry with the passed through idx
  892. * If the relinquished idx is lower than next_idx, use it
  893. * to replace next_idx
  894. */
  895. g_hash_table_remove(s->zpci_table, &pbdev->idx);
  896. if (idx < s->next_idx) {
  897. s->next_idx = idx;
  898. }
  899. pbdev->idx = idx;
  900. g_hash_table_insert(s->zpci_table, &pbdev->idx, pbdev);
  901. }
  902. return 0;
  903. }
  904. static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
  905. Error **errp)
  906. {
  907. S390pciState *s = S390_PCI_HOST_BRIDGE(hotplug_dev);
  908. PCIDevice *pdev = NULL;
  909. S390PCIBusDevice *pbdev = NULL;
  910. int rc;
  911. if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
  912. PCIBridge *pb = PCI_BRIDGE(dev);
  913. pdev = PCI_DEVICE(dev);
  914. pci_bridge_map_irq(pb, dev->id, s390_pci_map_irq);
  915. pci_setup_iommu(&pb->sec_bus, &s390_iommu_ops, s);
  916. qbus_set_hotplug_handler(BUS(&pb->sec_bus), OBJECT(s));
  917. if (dev->hotplugged) {
  918. pci_default_write_config(pdev, PCI_PRIMARY_BUS,
  919. pci_dev_bus_num(pdev), 1);
  920. s->bus_no += 1;
  921. pci_default_write_config(pdev, PCI_SECONDARY_BUS, s->bus_no, 1);
  922. s390_pci_update_subordinate(pdev, s->bus_no);
  923. }
  924. } else if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
  925. pdev = PCI_DEVICE(dev);
  926. /*
  927. * Multifunction is not supported due to the lack of CLP. However,
  928. * do not check for multifunction capability for SR-IOV devices because
  929. * SR-IOV devices automatically add the multifunction capability whether
  930. * the user intends to use the functions other than the PF.
  931. */
  932. if (pdev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION &&
  933. !pdev->exp.sriov_cap) {
  934. error_setg(errp, "multifunction not supported in s390");
  935. return;
  936. }
  937. if (!dev->id) {
  938. /* In the case the PCI device does not define an id */
  939. /* we generate one based on the PCI address */
  940. dev->id = g_strdup_printf("auto_%02x:%02x.%01x",
  941. pci_dev_bus_num(pdev),
  942. PCI_SLOT(pdev->devfn),
  943. PCI_FUNC(pdev->devfn));
  944. }
  945. pbdev = s390_pci_find_dev_by_target(s, dev->id);
  946. if (!pbdev) {
  947. /*
  948. * VFs are automatically created by PF, and creating zpci for them
  949. * will result in unexpected usage of fids. Currently QEMU does not
  950. * support multifunction for s390x so we don't need zpci for VFs
  951. * anyway.
  952. */
  953. if (pci_is_vf(pdev)) {
  954. return;
  955. }
  956. pbdev = s390_pci_device_new(s, dev->id, errp);
  957. if (!pbdev) {
  958. return;
  959. }
  960. }
  961. pbdev->pdev = pdev;
  962. pbdev->iommu = s390_pci_get_iommu(s, pci_get_bus(pdev), pdev->devfn);
  963. pbdev->iommu->pbdev = pbdev;
  964. pbdev->state = ZPCI_FS_DISABLED;
  965. set_pbdev_info(pbdev);
  966. if (object_dynamic_cast(OBJECT(dev), "vfio-pci")) {
  967. /*
  968. * By default, interpretation is always requested; if the available
  969. * facilities indicate it is not available, fallback to the
  970. * interception model.
  971. */
  972. if (pbdev->interp) {
  973. if (s390_pci_kvm_interp_allowed()) {
  974. rc = s390_pci_interp_plug(s, pbdev);
  975. if (rc) {
  976. error_setg(errp, "Plug failed for zPCI device in "
  977. "interpretation mode: %d", rc);
  978. return;
  979. }
  980. } else {
  981. trace_s390_pcihost("zPCI interpretation missing");
  982. pbdev->interp = false;
  983. pbdev->forwarding_assist = false;
  984. }
  985. }
  986. pbdev->iommu->dma_limit = s390_pci_start_dma_count(s, pbdev);
  987. /* Fill in CLP information passed via the vfio region */
  988. s390_pci_get_clp_info(pbdev);
  989. if (!pbdev->interp) {
  990. /* Do vfio passthrough but intercept for I/O */
  991. pbdev->fh |= FH_SHM_VFIO;
  992. pbdev->forwarding_assist = false;
  993. }
  994. /* Register shutdown notifier and reset callback for ISM devices */
  995. if (pbdev->pft == ZPCI_PFT_ISM) {
  996. pbdev->shutdown_notifier.notify = s390_pci_shutdown_notifier;
  997. qemu_register_shutdown_notifier(&pbdev->shutdown_notifier);
  998. }
  999. } else {
  1000. pbdev->fh |= FH_SHM_EMUL;
  1001. /* Always intercept emulated devices */
  1002. pbdev->interp = false;
  1003. pbdev->forwarding_assist = false;
  1004. pbdev->rtr_avail = false;
  1005. }
  1006. if (s390_pci_msix_init(pbdev) && !pbdev->interp) {
  1007. error_setg(errp, "MSI-X support is mandatory "
  1008. "in the S390 architecture");
  1009. return;
  1010. }
  1011. if (dev->hotplugged) {
  1012. s390_pci_generate_plug_event(HP_EVENT_TO_CONFIGURED ,
  1013. pbdev->fh, pbdev->fid);
  1014. }
  1015. } else if (object_dynamic_cast(OBJECT(dev), TYPE_S390_PCI_DEVICE)) {
  1016. pbdev = S390_PCI_DEVICE(dev);
  1017. /* the allocated idx is actually getting used */
  1018. s->next_idx = (pbdev->idx + 1) & FH_MASK_INDEX;
  1019. pbdev->fh = pbdev->idx;
  1020. QTAILQ_INSERT_TAIL(&s->zpci_devs, pbdev, link);
  1021. g_hash_table_insert(s->zpci_table, &pbdev->idx, pbdev);
  1022. } else {
  1023. g_assert_not_reached();
  1024. }
  1025. }
  1026. static void s390_pcihost_unplug(HotplugHandler *hotplug_dev, DeviceState *dev,
  1027. Error **errp)
  1028. {
  1029. S390pciState *s = S390_PCI_HOST_BRIDGE(hotplug_dev);
  1030. S390PCIBusDevice *pbdev = NULL;
  1031. if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
  1032. PCIDevice *pci_dev = PCI_DEVICE(dev);
  1033. PCIBus *bus;
  1034. int32_t devfn;
  1035. pbdev = s390_pci_find_dev_by_pci(s, PCI_DEVICE(dev));
  1036. if (!pbdev) {
  1037. g_assert(pci_is_vf(pci_dev));
  1038. return;
  1039. }
  1040. s390_pci_generate_plug_event(HP_EVENT_STANDBY_TO_RESERVED,
  1041. pbdev->fh, pbdev->fid);
  1042. bus = pci_get_bus(pci_dev);
  1043. devfn = pci_dev->devfn;
  1044. qdev_unrealize(dev);
  1045. s390_pci_msix_free(pbdev);
  1046. s390_pci_iommu_free(s, bus, devfn);
  1047. pbdev->pdev = NULL;
  1048. pbdev->state = ZPCI_FS_RESERVED;
  1049. } else if (object_dynamic_cast(OBJECT(dev), TYPE_S390_PCI_DEVICE)) {
  1050. pbdev = S390_PCI_DEVICE(dev);
  1051. pbdev->fid = 0;
  1052. QTAILQ_REMOVE(&s->zpci_devs, pbdev, link);
  1053. g_hash_table_remove(s->zpci_table, &pbdev->idx);
  1054. if (pbdev->iommu->dma_limit) {
  1055. s390_pci_end_dma_count(s, pbdev->iommu->dma_limit);
  1056. }
  1057. qdev_unrealize(dev);
  1058. }
  1059. }
  1060. static void s390_pcihost_unplug_request(HotplugHandler *hotplug_dev,
  1061. DeviceState *dev,
  1062. Error **errp)
  1063. {
  1064. S390pciState *s = S390_PCI_HOST_BRIDGE(hotplug_dev);
  1065. S390PCIBusDevice *pbdev;
  1066. if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
  1067. error_setg(errp, "PCI bridge hot unplug currently not supported");
  1068. } else if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
  1069. /*
  1070. * Redirect the unplug request to the zPCI device and remember that
  1071. * we've checked the PCI device already (to prevent endless recursion).
  1072. */
  1073. pbdev = s390_pci_find_dev_by_pci(s, PCI_DEVICE(dev));
  1074. if (!pbdev) {
  1075. g_assert(pci_is_vf(PCI_DEVICE(dev)));
  1076. return;
  1077. }
  1078. pbdev->pci_unplug_request_processed = true;
  1079. qdev_unplug(DEVICE(pbdev), errp);
  1080. } else if (object_dynamic_cast(OBJECT(dev), TYPE_S390_PCI_DEVICE)) {
  1081. pbdev = S390_PCI_DEVICE(dev);
  1082. /*
  1083. * If unplug was initially requested for the zPCI device, we
  1084. * first have to redirect to the PCI device, which will in return
  1085. * redirect back to us after performing its checks (if the request
  1086. * is not blocked, e.g. because it's a PCI bridge).
  1087. */
  1088. if (pbdev->pdev && !pbdev->pci_unplug_request_processed) {
  1089. qdev_unplug(DEVICE(pbdev->pdev), errp);
  1090. return;
  1091. }
  1092. pbdev->pci_unplug_request_processed = false;
  1093. switch (pbdev->state) {
  1094. case ZPCI_FS_STANDBY:
  1095. case ZPCI_FS_RESERVED:
  1096. s390_pci_perform_unplug(pbdev);
  1097. break;
  1098. default:
  1099. /*
  1100. * Allow to send multiple requests, e.g. if the guest crashed
  1101. * before releasing the device, we would not be able to send
  1102. * another request to the same VM (e.g. fresh OS).
  1103. */
  1104. pbdev->unplug_requested = true;
  1105. s390_pci_generate_plug_event(HP_EVENT_DECONFIGURE_REQUEST,
  1106. pbdev->fh, pbdev->fid);
  1107. }
  1108. } else {
  1109. g_assert_not_reached();
  1110. }
  1111. }
  1112. static void s390_pci_enumerate_bridge(PCIBus *bus, PCIDevice *pdev,
  1113. void *opaque)
  1114. {
  1115. S390pciState *s = opaque;
  1116. PCIBus *sec_bus = NULL;
  1117. if ((pci_default_read_config(pdev, PCI_HEADER_TYPE, 1) !=
  1118. PCI_HEADER_TYPE_BRIDGE)) {
  1119. return;
  1120. }
  1121. (s->bus_no)++;
  1122. pci_default_write_config(pdev, PCI_PRIMARY_BUS, pci_dev_bus_num(pdev), 1);
  1123. pci_default_write_config(pdev, PCI_SECONDARY_BUS, s->bus_no, 1);
  1124. pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, s->bus_no, 1);
  1125. sec_bus = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
  1126. if (!sec_bus) {
  1127. return;
  1128. }
  1129. /* Assign numbers to all child bridges. The last is the highest number. */
  1130. pci_for_each_device_under_bus(sec_bus, s390_pci_enumerate_bridge, s);
  1131. pci_default_write_config(pdev, PCI_SUBORDINATE_BUS, s->bus_no, 1);
  1132. }
  1133. void s390_pci_ism_reset(void)
  1134. {
  1135. S390pciState *s = s390_get_phb();
  1136. S390PCIBusDevice *pbdev, *next;
  1137. /* Trigger reset event for each passthrough ISM device currently in-use */
  1138. QTAILQ_FOREACH_SAFE(pbdev, &s->zpci_devs, link, next) {
  1139. if (pbdev->interp && pbdev->pft == ZPCI_PFT_ISM &&
  1140. pbdev->fh & FH_MASK_ENABLE) {
  1141. s390_pci_kvm_aif_disable(pbdev);
  1142. pci_device_reset(pbdev->pdev);
  1143. }
  1144. }
  1145. }
  1146. static void s390_pcihost_reset(DeviceState *dev)
  1147. {
  1148. S390pciState *s = S390_PCI_HOST_BRIDGE(dev);
  1149. PCIBus *bus = s->parent_obj.bus;
  1150. S390PCIBusDevice *pbdev, *next;
  1151. /* Process all pending unplug requests */
  1152. QTAILQ_FOREACH_SAFE(pbdev, &s->zpci_devs, link, next) {
  1153. if (pbdev->unplug_requested) {
  1154. if (pbdev->interp && (pbdev->fh & FH_MASK_ENABLE)) {
  1155. /* Interpreted devices were using interrupt forwarding */
  1156. s390_pci_kvm_aif_disable(pbdev);
  1157. } else if (pbdev->summary_ind) {
  1158. pci_dereg_irqs(pbdev);
  1159. }
  1160. if (pbdev->iommu->enabled) {
  1161. pci_dereg_ioat(pbdev->iommu);
  1162. }
  1163. pbdev->state = ZPCI_FS_STANDBY;
  1164. s390_pci_perform_unplug(pbdev);
  1165. }
  1166. }
  1167. /*
  1168. * When resetting a PCI bridge, the assigned numbers are set to 0. So
  1169. * on every system reset, we also have to reassign numbers.
  1170. */
  1171. s->bus_no = 0;
  1172. pci_for_each_device_under_bus(bus, s390_pci_enumerate_bridge, s);
  1173. }
  1174. static void s390_pcihost_class_init(ObjectClass *klass, void *data)
  1175. {
  1176. DeviceClass *dc = DEVICE_CLASS(klass);
  1177. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  1178. device_class_set_legacy_reset(dc, s390_pcihost_reset);
  1179. dc->realize = s390_pcihost_realize;
  1180. dc->unrealize = s390_pcihost_unrealize;
  1181. hc->pre_plug = s390_pcihost_pre_plug;
  1182. hc->plug = s390_pcihost_plug;
  1183. hc->unplug_request = s390_pcihost_unplug_request;
  1184. hc->unplug = s390_pcihost_unplug;
  1185. msi_nonbroken = true;
  1186. }
  1187. static const TypeInfo s390_pcihost_info = {
  1188. .name = TYPE_S390_PCI_HOST_BRIDGE,
  1189. .parent = TYPE_PCI_HOST_BRIDGE,
  1190. .instance_size = sizeof(S390pciState),
  1191. .class_init = s390_pcihost_class_init,
  1192. .interfaces = (InterfaceInfo[]) {
  1193. { TYPE_HOTPLUG_HANDLER },
  1194. { }
  1195. }
  1196. };
  1197. static const TypeInfo s390_pcibus_info = {
  1198. .name = TYPE_S390_PCI_BUS,
  1199. .parent = TYPE_BUS,
  1200. .instance_size = sizeof(S390PCIBus),
  1201. };
  1202. static uint16_t s390_pci_generate_uid(S390pciState *s)
  1203. {
  1204. uint16_t uid = 0;
  1205. do {
  1206. uid++;
  1207. if (!s390_pci_find_dev_by_uid(s, uid)) {
  1208. return uid;
  1209. }
  1210. } while (uid < ZPCI_MAX_UID);
  1211. return UID_UNDEFINED;
  1212. }
  1213. static uint32_t s390_pci_generate_fid(S390pciState *s, Error **errp)
  1214. {
  1215. uint32_t fid = 0;
  1216. do {
  1217. if (!s390_pci_find_dev_by_fid(s, fid)) {
  1218. return fid;
  1219. }
  1220. } while (fid++ != ZPCI_MAX_FID);
  1221. error_setg(errp, "no free fid could be found");
  1222. return 0;
  1223. }
  1224. static void s390_pci_device_realize(DeviceState *dev, Error **errp)
  1225. {
  1226. S390PCIBusDevice *zpci = S390_PCI_DEVICE(dev);
  1227. S390pciState *s = s390_get_phb();
  1228. if (!zpci->target) {
  1229. error_setg(errp, "target must be defined");
  1230. return;
  1231. }
  1232. if (s390_pci_find_dev_by_target(s, zpci->target)) {
  1233. error_setg(errp, "target %s already has an associated zpci device",
  1234. zpci->target);
  1235. return;
  1236. }
  1237. if (zpci->uid == UID_UNDEFINED) {
  1238. zpci->uid = s390_pci_generate_uid(s);
  1239. if (!zpci->uid) {
  1240. error_setg(errp, "no free uid could be found");
  1241. return;
  1242. }
  1243. } else if (s390_pci_find_dev_by_uid(s, zpci->uid)) {
  1244. error_setg(errp, "uid %u already in use", zpci->uid);
  1245. return;
  1246. }
  1247. if (!zpci->fid_defined) {
  1248. Error *local_error = NULL;
  1249. zpci->fid = s390_pci_generate_fid(s, &local_error);
  1250. if (local_error) {
  1251. error_propagate(errp, local_error);
  1252. return;
  1253. }
  1254. } else if (s390_pci_find_dev_by_fid(s, zpci->fid)) {
  1255. error_setg(errp, "fid %u already in use", zpci->fid);
  1256. return;
  1257. }
  1258. zpci->state = ZPCI_FS_RESERVED;
  1259. zpci->fmb.format = ZPCI_FMB_FORMAT;
  1260. }
  1261. static void s390_pci_device_reset(DeviceState *dev)
  1262. {
  1263. S390PCIBusDevice *pbdev = S390_PCI_DEVICE(dev);
  1264. switch (pbdev->state) {
  1265. case ZPCI_FS_RESERVED:
  1266. return;
  1267. case ZPCI_FS_STANDBY:
  1268. break;
  1269. default:
  1270. pbdev->fh &= ~FH_MASK_ENABLE;
  1271. pbdev->state = ZPCI_FS_DISABLED;
  1272. break;
  1273. }
  1274. if (pbdev->interp && (pbdev->fh & FH_MASK_ENABLE)) {
  1275. /* Interpreted devices were using interrupt forwarding */
  1276. s390_pci_kvm_aif_disable(pbdev);
  1277. } else if (pbdev->summary_ind) {
  1278. pci_dereg_irqs(pbdev);
  1279. }
  1280. if (pbdev->iommu->enabled) {
  1281. pci_dereg_ioat(pbdev->iommu);
  1282. }
  1283. fmb_timer_free(pbdev);
  1284. }
  1285. static void s390_pci_get_fid(Object *obj, Visitor *v, const char *name,
  1286. void *opaque, Error **errp)
  1287. {
  1288. const Property *prop = opaque;
  1289. uint32_t *ptr = object_field_prop_ptr(obj, prop);
  1290. visit_type_uint32(v, name, ptr, errp);
  1291. }
  1292. static void s390_pci_set_fid(Object *obj, Visitor *v, const char *name,
  1293. void *opaque, Error **errp)
  1294. {
  1295. S390PCIBusDevice *zpci = S390_PCI_DEVICE(obj);
  1296. const Property *prop = opaque;
  1297. uint32_t *ptr = object_field_prop_ptr(obj, prop);
  1298. if (!visit_type_uint32(v, name, ptr, errp)) {
  1299. return;
  1300. }
  1301. zpci->fid_defined = true;
  1302. }
  1303. static const PropertyInfo s390_pci_fid_propinfo = {
  1304. .type = "uint32",
  1305. .description = "zpci_fid",
  1306. .get = s390_pci_get_fid,
  1307. .set = s390_pci_set_fid,
  1308. };
  1309. #define DEFINE_PROP_S390_PCI_FID(_n, _s, _f) \
  1310. DEFINE_PROP(_n, _s, _f, s390_pci_fid_propinfo, uint32_t)
  1311. static const Property s390_pci_device_properties[] = {
  1312. DEFINE_PROP_UINT16("uid", S390PCIBusDevice, uid, UID_UNDEFINED),
  1313. DEFINE_PROP_S390_PCI_FID("fid", S390PCIBusDevice, fid),
  1314. DEFINE_PROP_STRING("target", S390PCIBusDevice, target),
  1315. DEFINE_PROP_BOOL("interpret", S390PCIBusDevice, interp, true),
  1316. DEFINE_PROP_BOOL("forwarding-assist", S390PCIBusDevice, forwarding_assist,
  1317. true),
  1318. DEFINE_PROP_BOOL("relaxed-translation", S390PCIBusDevice, rtr_avail,
  1319. true),
  1320. };
  1321. static const VMStateDescription s390_pci_device_vmstate = {
  1322. .name = TYPE_S390_PCI_DEVICE,
  1323. /*
  1324. * TODO: add state handling here, so migration works at least with
  1325. * emulated pci devices on s390x
  1326. */
  1327. .unmigratable = 1,
  1328. };
  1329. static void s390_pci_device_class_init(ObjectClass *klass, void *data)
  1330. {
  1331. DeviceClass *dc = DEVICE_CLASS(klass);
  1332. dc->desc = "zpci device";
  1333. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  1334. device_class_set_legacy_reset(dc, s390_pci_device_reset);
  1335. dc->bus_type = TYPE_S390_PCI_BUS;
  1336. dc->realize = s390_pci_device_realize;
  1337. device_class_set_props(dc, s390_pci_device_properties);
  1338. dc->vmsd = &s390_pci_device_vmstate;
  1339. }
  1340. static const TypeInfo s390_pci_device_info = {
  1341. .name = TYPE_S390_PCI_DEVICE,
  1342. .parent = TYPE_DEVICE,
  1343. .instance_size = sizeof(S390PCIBusDevice),
  1344. .class_init = s390_pci_device_class_init,
  1345. };
  1346. static const TypeInfo s390_pci_iommu_info = {
  1347. .name = TYPE_S390_PCI_IOMMU,
  1348. .parent = TYPE_OBJECT,
  1349. .instance_size = sizeof(S390PCIIOMMU),
  1350. };
  1351. static void s390_iommu_memory_region_class_init(ObjectClass *klass, void *data)
  1352. {
  1353. IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
  1354. imrc->translate = s390_translate_iommu;
  1355. imrc->replay = s390_pci_iommu_replay;
  1356. }
  1357. static const TypeInfo s390_iommu_memory_region_info = {
  1358. .parent = TYPE_IOMMU_MEMORY_REGION,
  1359. .name = TYPE_S390_IOMMU_MEMORY_REGION,
  1360. .class_init = s390_iommu_memory_region_class_init,
  1361. };
  1362. static void s390_pci_register_types(void)
  1363. {
  1364. type_register_static(&s390_pcihost_info);
  1365. type_register_static(&s390_pcibus_info);
  1366. type_register_static(&s390_pci_device_info);
  1367. type_register_static(&s390_pci_iommu_info);
  1368. type_register_static(&s390_iommu_memory_region_info);
  1369. }
  1370. type_init(s390_pci_register_types)