iohub.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Remote IO Hub
  3. *
  4. * Copyright © 2018, 2021 Oracle and/or its affiliates.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include "qemu/osdep.h"
  11. #include "hw/pci/pci.h"
  12. #include "hw/pci/pci_ids.h"
  13. #include "hw/pci/pci_bus.h"
  14. #include "qemu/thread.h"
  15. #include "hw/remote/machine.h"
  16. #include "hw/remote/iohub.h"
  17. #include "qemu/main-loop.h"
  18. void remote_iohub_init(RemoteIOHubState *iohub)
  19. {
  20. int pirq;
  21. memset(&iohub->irqfds, 0, sizeof(iohub->irqfds));
  22. memset(&iohub->resamplefds, 0, sizeof(iohub->resamplefds));
  23. for (pirq = 0; pirq < REMOTE_IOHUB_NB_PIRQS; pirq++) {
  24. qemu_mutex_init(&iohub->irq_level_lock[pirq]);
  25. iohub->irq_level[pirq] = 0;
  26. event_notifier_init_fd(&iohub->irqfds[pirq], -1);
  27. event_notifier_init_fd(&iohub->resamplefds[pirq], -1);
  28. }
  29. }
  30. int remote_iohub_map_irq(PCIDevice *pci_dev, int intx)
  31. {
  32. return pci_dev->devfn;
  33. }
  34. void remote_iohub_set_irq(void *opaque, int pirq, int level)
  35. {
  36. RemoteIOHubState *iohub = opaque;
  37. assert(pirq >= 0);
  38. assert(pirq < PCI_DEVFN_MAX);
  39. QEMU_LOCK_GUARD(&iohub->irq_level_lock[pirq]);
  40. if (level) {
  41. if (++iohub->irq_level[pirq] == 1) {
  42. event_notifier_set(&iohub->irqfds[pirq]);
  43. }
  44. } else if (iohub->irq_level[pirq] > 0) {
  45. iohub->irq_level[pirq]--;
  46. }
  47. }
  48. static void intr_resample_handler(void *opaque)
  49. {
  50. ResampleToken *token = opaque;
  51. RemoteIOHubState *iohub = token->iohub;
  52. int pirq, s;
  53. pirq = token->pirq;
  54. s = event_notifier_test_and_clear(&iohub->resamplefds[pirq]);
  55. assert(s >= 0);
  56. QEMU_LOCK_GUARD(&iohub->irq_level_lock[pirq]);
  57. if (iohub->irq_level[pirq]) {
  58. event_notifier_set(&iohub->irqfds[pirq]);
  59. }
  60. }
  61. void process_set_irqfd_msg(PCIDevice *pci_dev, MPQemuMsg *msg)
  62. {
  63. RemoteMachineState *machine = REMOTE_MACHINE(current_machine);
  64. RemoteIOHubState *iohub = &machine->iohub;
  65. int pirq, intx;
  66. intx = pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
  67. pirq = remote_iohub_map_irq(pci_dev, intx);
  68. if (event_notifier_get_fd(&iohub->irqfds[pirq]) != -1) {
  69. qemu_set_fd_handler(event_notifier_get_fd(&iohub->resamplefds[pirq]),
  70. NULL, NULL, NULL);
  71. event_notifier_cleanup(&iohub->irqfds[pirq]);
  72. event_notifier_cleanup(&iohub->resamplefds[pirq]);
  73. memset(&iohub->token[pirq], 0, sizeof(ResampleToken));
  74. }
  75. event_notifier_init_fd(&iohub->irqfds[pirq], msg->fds[0]);
  76. event_notifier_init_fd(&iohub->resamplefds[pirq], msg->fds[1]);
  77. iohub->token[pirq].iohub = iohub;
  78. iohub->token[pirq].pirq = pirq;
  79. qemu_set_fd_handler(msg->fds[1], intr_resample_handler, NULL,
  80. &iohub->token[pirq]);
  81. }