b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * VFIO PCI interrupt handling |
| 4 | * |
| 5 | * Copyright (C) 2012 Red Hat, Inc. All rights reserved. |
| 6 | * Author: Alex Williamson <alex.williamson@redhat.com> |
| 7 | * |
| 8 | * Derived from original vfio: |
| 9 | * Copyright 2010 Cisco Systems, Inc. All rights reserved. |
| 10 | * Author: Tom Lyon, pugs@cisco.com |
| 11 | */ |
| 12 | |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/eventfd.h> |
| 16 | #include <linux/msi.h> |
| 17 | #include <linux/pci.h> |
| 18 | #include <linux/file.h> |
| 19 | #include <linux/vfio.h> |
| 20 | #include <linux/wait.h> |
| 21 | #include <linux/slab.h> |
| 22 | |
| 23 | #include "vfio_pci_private.h" |
| 24 | |
| 25 | /* |
| 26 | * INTx |
| 27 | */ |
| 28 | static void vfio_send_intx_eventfd(void *opaque, void *unused) |
| 29 | { |
| 30 | struct vfio_pci_device *vdev = opaque; |
| 31 | |
| 32 | if (likely(is_intx(vdev) && !vdev->virq_disabled)) { |
| 33 | struct eventfd_ctx *trigger; |
| 34 | |
| 35 | trigger = READ_ONCE(vdev->ctx[0].trigger); |
| 36 | if (likely(trigger)) |
| 37 | eventfd_signal(trigger, 1); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | static void __vfio_pci_intx_mask(struct vfio_pci_device *vdev) |
| 42 | { |
| 43 | struct pci_dev *pdev = vdev->pdev; |
| 44 | unsigned long flags; |
| 45 | |
| 46 | lockdep_assert_held(&vdev->igate); |
| 47 | |
| 48 | spin_lock_irqsave(&vdev->irqlock, flags); |
| 49 | |
| 50 | /* |
| 51 | * Masking can come from interrupt, ioctl, or config space |
| 52 | * via INTx disable. The latter means this can get called |
| 53 | * even when not using intx delivery. In this case, just |
| 54 | * try to have the physical bit follow the virtual bit. |
| 55 | */ |
| 56 | if (unlikely(!is_intx(vdev))) { |
| 57 | if (vdev->pci_2_3) |
| 58 | pci_intx(pdev, 0); |
| 59 | } else if (!vdev->ctx[0].masked) { |
| 60 | /* |
| 61 | * Can't use check_and_mask here because we always want to |
| 62 | * mask, not just when something is pending. |
| 63 | */ |
| 64 | if (vdev->pci_2_3) |
| 65 | pci_intx(pdev, 0); |
| 66 | else |
| 67 | disable_irq_nosync(pdev->irq); |
| 68 | |
| 69 | vdev->ctx[0].masked = true; |
| 70 | } |
| 71 | |
| 72 | spin_unlock_irqrestore(&vdev->irqlock, flags); |
| 73 | } |
| 74 | |
| 75 | void vfio_pci_intx_mask(struct vfio_pci_device *vdev) |
| 76 | { |
| 77 | mutex_lock(&vdev->igate); |
| 78 | __vfio_pci_intx_mask(vdev); |
| 79 | mutex_unlock(&vdev->igate); |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * If this is triggered by an eventfd, we can't call eventfd_signal |
| 84 | * or else we'll deadlock on the eventfd wait queue. Return >0 when |
| 85 | * a signal is necessary, which can then be handled via a work queue |
| 86 | * or directly depending on the caller. |
| 87 | */ |
| 88 | static int vfio_pci_intx_unmask_handler(void *opaque, void *unused) |
| 89 | { |
| 90 | struct vfio_pci_device *vdev = opaque; |
| 91 | struct pci_dev *pdev = vdev->pdev; |
| 92 | unsigned long flags; |
| 93 | int ret = 0; |
| 94 | |
| 95 | spin_lock_irqsave(&vdev->irqlock, flags); |
| 96 | |
| 97 | /* |
| 98 | * Unmasking comes from ioctl or config, so again, have the |
| 99 | * physical bit follow the virtual even when not using INTx. |
| 100 | */ |
| 101 | if (unlikely(!is_intx(vdev))) { |
| 102 | if (vdev->pci_2_3) |
| 103 | pci_intx(pdev, 1); |
| 104 | } else if (vdev->ctx[0].masked && !vdev->virq_disabled) { |
| 105 | /* |
| 106 | * A pending interrupt here would immediately trigger, |
| 107 | * but we can avoid that overhead by just re-sending |
| 108 | * the interrupt to the user. |
| 109 | */ |
| 110 | if (vdev->pci_2_3) { |
| 111 | if (!pci_check_and_unmask_intx(pdev)) |
| 112 | ret = 1; |
| 113 | } else |
| 114 | enable_irq(pdev->irq); |
| 115 | |
| 116 | vdev->ctx[0].masked = (ret > 0); |
| 117 | } |
| 118 | |
| 119 | spin_unlock_irqrestore(&vdev->irqlock, flags); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | static void __vfio_pci_intx_unmask(struct vfio_pci_device *vdev) |
| 125 | { |
| 126 | lockdep_assert_held(&vdev->igate); |
| 127 | |
| 128 | if (vfio_pci_intx_unmask_handler(vdev, NULL) > 0) |
| 129 | vfio_send_intx_eventfd(vdev, NULL); |
| 130 | } |
| 131 | |
| 132 | void vfio_pci_intx_unmask(struct vfio_pci_device *vdev) |
| 133 | { |
| 134 | mutex_lock(&vdev->igate); |
| 135 | __vfio_pci_intx_unmask(vdev); |
| 136 | mutex_unlock(&vdev->igate); |
| 137 | } |
| 138 | |
| 139 | static irqreturn_t vfio_intx_handler(int irq, void *dev_id) |
| 140 | { |
| 141 | struct vfio_pci_device *vdev = dev_id; |
| 142 | unsigned long flags; |
| 143 | int ret = IRQ_NONE; |
| 144 | |
| 145 | spin_lock_irqsave(&vdev->irqlock, flags); |
| 146 | |
| 147 | if (!vdev->pci_2_3) { |
| 148 | disable_irq_nosync(vdev->pdev->irq); |
| 149 | vdev->ctx[0].masked = true; |
| 150 | ret = IRQ_HANDLED; |
| 151 | } else if (!vdev->ctx[0].masked && /* may be shared */ |
| 152 | pci_check_and_mask_intx(vdev->pdev)) { |
| 153 | vdev->ctx[0].masked = true; |
| 154 | ret = IRQ_HANDLED; |
| 155 | } |
| 156 | |
| 157 | spin_unlock_irqrestore(&vdev->irqlock, flags); |
| 158 | |
| 159 | if (ret == IRQ_HANDLED) |
| 160 | vfio_send_intx_eventfd(vdev, NULL); |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static int vfio_intx_enable(struct vfio_pci_device *vdev, |
| 166 | struct eventfd_ctx *trigger) |
| 167 | { |
| 168 | struct pci_dev *pdev = vdev->pdev; |
| 169 | unsigned long irqflags; |
| 170 | char *name; |
| 171 | int ret; |
| 172 | |
| 173 | if (!is_irq_none(vdev)) |
| 174 | return -EINVAL; |
| 175 | |
| 176 | if (!pdev->irq) |
| 177 | return -ENODEV; |
| 178 | |
| 179 | name = kasprintf(GFP_KERNEL, "vfio-intx(%s)", pci_name(pdev)); |
| 180 | if (!name) |
| 181 | return -ENOMEM; |
| 182 | |
| 183 | vdev->ctx = kzalloc(sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL); |
| 184 | if (!vdev->ctx) |
| 185 | return -ENOMEM; |
| 186 | |
| 187 | vdev->num_ctx = 1; |
| 188 | |
| 189 | vdev->ctx[0].name = name; |
| 190 | vdev->ctx[0].trigger = trigger; |
| 191 | |
| 192 | /* |
| 193 | * Fill the initial masked state based on virq_disabled. After |
| 194 | * enable, changing the DisINTx bit in vconfig directly changes INTx |
| 195 | * masking. igate prevents races during setup, once running masked |
| 196 | * is protected via irqlock. |
| 197 | * |
| 198 | * Devices supporting DisINTx also reflect the current mask state in |
| 199 | * the physical DisINTx bit, which is not affected during IRQ setup. |
| 200 | * |
| 201 | * Devices without DisINTx support require an exclusive interrupt. |
| 202 | * IRQ masking is performed at the IRQ chip. Again, igate protects |
| 203 | * against races during setup and IRQ handlers and irqfds are not |
| 204 | * yet active, therefore masked is stable and can be used to |
| 205 | * conditionally auto-enable the IRQ. |
| 206 | * |
| 207 | * irq_type must be stable while the IRQ handler is registered, |
| 208 | * therefore it must be set before request_irq(). |
| 209 | */ |
| 210 | vdev->ctx[0].masked = vdev->virq_disabled; |
| 211 | if (vdev->pci_2_3) { |
| 212 | pci_intx(pdev, !vdev->ctx[0].masked); |
| 213 | irqflags = IRQF_SHARED; |
| 214 | } else { |
| 215 | irqflags = vdev->ctx[0].masked ? IRQF_NO_AUTOEN : 0; |
| 216 | } |
| 217 | |
| 218 | vdev->irq_type = VFIO_PCI_INTX_IRQ_INDEX; |
| 219 | |
| 220 | ret = request_irq(pdev->irq, vfio_intx_handler, |
| 221 | irqflags, vdev->ctx[0].name, vdev); |
| 222 | if (ret) { |
| 223 | vdev->irq_type = VFIO_PCI_NUM_IRQS; |
| 224 | kfree(name); |
| 225 | vdev->num_ctx = 0; |
| 226 | kfree(vdev->ctx); |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int vfio_intx_set_signal(struct vfio_pci_device *vdev, |
| 234 | struct eventfd_ctx *trigger) |
| 235 | { |
| 236 | struct pci_dev *pdev = vdev->pdev; |
| 237 | struct eventfd_ctx *old; |
| 238 | |
| 239 | old = vdev->ctx[0].trigger; |
| 240 | |
| 241 | WRITE_ONCE(vdev->ctx[0].trigger, trigger); |
| 242 | |
| 243 | /* Releasing an old ctx requires synchronizing in-flight users */ |
| 244 | if (old) { |
| 245 | synchronize_irq(pdev->irq); |
| 246 | vfio_virqfd_flush_thread(&vdev->ctx[0].unmask); |
| 247 | eventfd_ctx_put(old); |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static void vfio_intx_disable(struct vfio_pci_device *vdev) |
| 254 | { |
| 255 | struct pci_dev *pdev = vdev->pdev; |
| 256 | |
| 257 | vfio_virqfd_disable(&vdev->ctx[0].unmask); |
| 258 | vfio_virqfd_disable(&vdev->ctx[0].mask); |
| 259 | free_irq(pdev->irq, vdev); |
| 260 | if (vdev->ctx[0].trigger) |
| 261 | eventfd_ctx_put(vdev->ctx[0].trigger); |
| 262 | kfree(vdev->ctx[0].name); |
| 263 | vdev->irq_type = VFIO_PCI_NUM_IRQS; |
| 264 | vdev->num_ctx = 0; |
| 265 | kfree(vdev->ctx); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * MSI/MSI-X |
| 270 | */ |
| 271 | static irqreturn_t vfio_msihandler(int irq, void *arg) |
| 272 | { |
| 273 | struct eventfd_ctx *trigger = arg; |
| 274 | |
| 275 | eventfd_signal(trigger, 1); |
| 276 | return IRQ_HANDLED; |
| 277 | } |
| 278 | |
| 279 | static int vfio_msi_enable(struct vfio_pci_device *vdev, int nvec, bool msix) |
| 280 | { |
| 281 | struct pci_dev *pdev = vdev->pdev; |
| 282 | unsigned int flag = msix ? PCI_IRQ_MSIX : PCI_IRQ_MSI; |
| 283 | int ret; |
| 284 | u16 cmd; |
| 285 | |
| 286 | if (!is_irq_none(vdev)) |
| 287 | return -EINVAL; |
| 288 | |
| 289 | vdev->ctx = kcalloc(nvec, sizeof(struct vfio_pci_irq_ctx), GFP_KERNEL); |
| 290 | if (!vdev->ctx) |
| 291 | return -ENOMEM; |
| 292 | |
| 293 | /* return the number of supported vectors if we can't get all: */ |
| 294 | cmd = vfio_pci_memory_lock_and_enable(vdev); |
| 295 | ret = pci_alloc_irq_vectors(pdev, 1, nvec, flag); |
| 296 | if (ret < nvec) { |
| 297 | if (ret > 0) |
| 298 | pci_free_irq_vectors(pdev); |
| 299 | vfio_pci_memory_unlock_and_restore(vdev, cmd); |
| 300 | kfree(vdev->ctx); |
| 301 | return ret; |
| 302 | } |
| 303 | vfio_pci_memory_unlock_and_restore(vdev, cmd); |
| 304 | |
| 305 | vdev->num_ctx = nvec; |
| 306 | vdev->irq_type = msix ? VFIO_PCI_MSIX_IRQ_INDEX : |
| 307 | VFIO_PCI_MSI_IRQ_INDEX; |
| 308 | |
| 309 | if (!msix) { |
| 310 | /* |
| 311 | * Compute the virtual hardware field for max msi vectors - |
| 312 | * it is the log base 2 of the number of vectors. |
| 313 | */ |
| 314 | vdev->msi_qmax = fls(nvec * 2 - 1) - 1; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int vfio_msi_set_vector_signal(struct vfio_pci_device *vdev, |
| 321 | int vector, int fd, bool msix) |
| 322 | { |
| 323 | struct pci_dev *pdev = vdev->pdev; |
| 324 | struct eventfd_ctx *trigger; |
| 325 | int irq, ret; |
| 326 | u16 cmd; |
| 327 | |
| 328 | if (vector < 0 || vector >= vdev->num_ctx) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | irq = pci_irq_vector(pdev, vector); |
| 332 | |
| 333 | if (vdev->ctx[vector].trigger) { |
| 334 | irq_bypass_unregister_producer(&vdev->ctx[vector].producer); |
| 335 | |
| 336 | cmd = vfio_pci_memory_lock_and_enable(vdev); |
| 337 | free_irq(irq, vdev->ctx[vector].trigger); |
| 338 | vfio_pci_memory_unlock_and_restore(vdev, cmd); |
| 339 | |
| 340 | kfree(vdev->ctx[vector].name); |
| 341 | eventfd_ctx_put(vdev->ctx[vector].trigger); |
| 342 | vdev->ctx[vector].trigger = NULL; |
| 343 | } |
| 344 | |
| 345 | if (fd < 0) |
| 346 | return 0; |
| 347 | |
| 348 | vdev->ctx[vector].name = kasprintf(GFP_KERNEL, "vfio-msi%s[%d](%s)", |
| 349 | msix ? "x" : "", vector, |
| 350 | pci_name(pdev)); |
| 351 | if (!vdev->ctx[vector].name) |
| 352 | return -ENOMEM; |
| 353 | |
| 354 | trigger = eventfd_ctx_fdget(fd); |
| 355 | if (IS_ERR(trigger)) { |
| 356 | kfree(vdev->ctx[vector].name); |
| 357 | return PTR_ERR(trigger); |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * The MSIx vector table resides in device memory which may be cleared |
| 362 | * via backdoor resets. We don't allow direct access to the vector |
| 363 | * table so even if a userspace driver attempts to save/restore around |
| 364 | * such a reset it would be unsuccessful. To avoid this, restore the |
| 365 | * cached value of the message prior to enabling. |
| 366 | */ |
| 367 | cmd = vfio_pci_memory_lock_and_enable(vdev); |
| 368 | if (msix) { |
| 369 | struct msi_msg msg; |
| 370 | |
| 371 | get_cached_msi_msg(irq, &msg); |
| 372 | pci_write_msi_msg(irq, &msg); |
| 373 | } |
| 374 | |
| 375 | ret = request_irq(irq, vfio_msihandler, 0, |
| 376 | vdev->ctx[vector].name, trigger); |
| 377 | vfio_pci_memory_unlock_and_restore(vdev, cmd); |
| 378 | if (ret) { |
| 379 | kfree(vdev->ctx[vector].name); |
| 380 | eventfd_ctx_put(trigger); |
| 381 | return ret; |
| 382 | } |
| 383 | |
| 384 | vdev->ctx[vector].producer.token = trigger; |
| 385 | vdev->ctx[vector].producer.irq = irq; |
| 386 | ret = irq_bypass_register_producer(&vdev->ctx[vector].producer); |
| 387 | if (unlikely(ret)) { |
| 388 | dev_info(&pdev->dev, |
| 389 | "irq bypass producer (token %p) registration fails: %d\n", |
| 390 | vdev->ctx[vector].producer.token, ret); |
| 391 | |
| 392 | vdev->ctx[vector].producer.token = NULL; |
| 393 | } |
| 394 | vdev->ctx[vector].trigger = trigger; |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static int vfio_msi_set_block(struct vfio_pci_device *vdev, unsigned start, |
| 400 | unsigned count, int32_t *fds, bool msix) |
| 401 | { |
| 402 | int i, j, ret = 0; |
| 403 | |
| 404 | if (start >= vdev->num_ctx || start + count > vdev->num_ctx) |
| 405 | return -EINVAL; |
| 406 | |
| 407 | for (i = 0, j = start; i < count && !ret; i++, j++) { |
| 408 | int fd = fds ? fds[i] : -1; |
| 409 | ret = vfio_msi_set_vector_signal(vdev, j, fd, msix); |
| 410 | } |
| 411 | |
| 412 | if (ret) { |
| 413 | for (--j; j >= (int)start; j--) |
| 414 | vfio_msi_set_vector_signal(vdev, j, -1, msix); |
| 415 | } |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | static void vfio_msi_disable(struct vfio_pci_device *vdev, bool msix) |
| 421 | { |
| 422 | struct pci_dev *pdev = vdev->pdev; |
| 423 | int i; |
| 424 | u16 cmd; |
| 425 | |
| 426 | for (i = 0; i < vdev->num_ctx; i++) { |
| 427 | vfio_virqfd_disable(&vdev->ctx[i].unmask); |
| 428 | vfio_virqfd_disable(&vdev->ctx[i].mask); |
| 429 | } |
| 430 | |
| 431 | vfio_msi_set_block(vdev, 0, vdev->num_ctx, NULL, msix); |
| 432 | |
| 433 | cmd = vfio_pci_memory_lock_and_enable(vdev); |
| 434 | pci_free_irq_vectors(pdev); |
| 435 | vfio_pci_memory_unlock_and_restore(vdev, cmd); |
| 436 | |
| 437 | /* |
| 438 | * Both disable paths above use pci_intx_for_msi() to clear DisINTx |
| 439 | * via their shutdown paths. Restore for NoINTx devices. |
| 440 | */ |
| 441 | if (vdev->nointx) |
| 442 | pci_intx(pdev, 0); |
| 443 | |
| 444 | vdev->irq_type = VFIO_PCI_NUM_IRQS; |
| 445 | vdev->num_ctx = 0; |
| 446 | kfree(vdev->ctx); |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * IOCTL support |
| 451 | */ |
| 452 | static int vfio_pci_set_intx_unmask(struct vfio_pci_device *vdev, |
| 453 | unsigned index, unsigned start, |
| 454 | unsigned count, uint32_t flags, void *data) |
| 455 | { |
| 456 | if (!is_intx(vdev) || start != 0 || count != 1) |
| 457 | return -EINVAL; |
| 458 | |
| 459 | if (flags & VFIO_IRQ_SET_DATA_NONE) { |
| 460 | __vfio_pci_intx_unmask(vdev); |
| 461 | } else if (flags & VFIO_IRQ_SET_DATA_BOOL) { |
| 462 | uint8_t unmask = *(uint8_t *)data; |
| 463 | if (unmask) |
| 464 | __vfio_pci_intx_unmask(vdev); |
| 465 | } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { |
| 466 | int32_t fd = *(int32_t *)data; |
| 467 | if (fd >= 0) |
| 468 | return vfio_virqfd_enable((void *) vdev, |
| 469 | vfio_pci_intx_unmask_handler, |
| 470 | vfio_send_intx_eventfd, NULL, |
| 471 | &vdev->ctx[0].unmask, fd); |
| 472 | |
| 473 | vfio_virqfd_disable(&vdev->ctx[0].unmask); |
| 474 | } |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static int vfio_pci_set_intx_mask(struct vfio_pci_device *vdev, |
| 480 | unsigned index, unsigned start, |
| 481 | unsigned count, uint32_t flags, void *data) |
| 482 | { |
| 483 | if (!is_intx(vdev) || start != 0 || count != 1) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | if (flags & VFIO_IRQ_SET_DATA_NONE) { |
| 487 | __vfio_pci_intx_mask(vdev); |
| 488 | } else if (flags & VFIO_IRQ_SET_DATA_BOOL) { |
| 489 | uint8_t mask = *(uint8_t *)data; |
| 490 | if (mask) |
| 491 | __vfio_pci_intx_mask(vdev); |
| 492 | } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { |
| 493 | return -ENOTTY; /* XXX implement me */ |
| 494 | } |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static int vfio_pci_set_intx_trigger(struct vfio_pci_device *vdev, |
| 500 | unsigned index, unsigned start, |
| 501 | unsigned count, uint32_t flags, void *data) |
| 502 | { |
| 503 | if (is_intx(vdev) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) { |
| 504 | vfio_intx_disable(vdev); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | if (!(is_intx(vdev) || is_irq_none(vdev)) || start != 0 || count != 1) |
| 509 | return -EINVAL; |
| 510 | |
| 511 | if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { |
| 512 | struct eventfd_ctx *trigger = NULL; |
| 513 | int32_t fd = *(int32_t *)data; |
| 514 | int ret; |
| 515 | |
| 516 | if (fd >= 0) { |
| 517 | trigger = eventfd_ctx_fdget(fd); |
| 518 | if (IS_ERR(trigger)) |
| 519 | return PTR_ERR(trigger); |
| 520 | } |
| 521 | |
| 522 | if (is_intx(vdev)) |
| 523 | ret = vfio_intx_set_signal(vdev, trigger); |
| 524 | else |
| 525 | ret = vfio_intx_enable(vdev, trigger); |
| 526 | |
| 527 | if (ret && trigger) |
| 528 | eventfd_ctx_put(trigger); |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
| 533 | if (!is_intx(vdev)) |
| 534 | return -EINVAL; |
| 535 | |
| 536 | if (flags & VFIO_IRQ_SET_DATA_NONE) { |
| 537 | vfio_send_intx_eventfd(vdev, NULL); |
| 538 | } else if (flags & VFIO_IRQ_SET_DATA_BOOL) { |
| 539 | uint8_t trigger = *(uint8_t *)data; |
| 540 | if (trigger) |
| 541 | vfio_send_intx_eventfd(vdev, NULL); |
| 542 | } |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | static int vfio_pci_set_msi_trigger(struct vfio_pci_device *vdev, |
| 547 | unsigned index, unsigned start, |
| 548 | unsigned count, uint32_t flags, void *data) |
| 549 | { |
| 550 | int i; |
| 551 | bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false; |
| 552 | |
| 553 | if (irq_is(vdev, index) && !count && (flags & VFIO_IRQ_SET_DATA_NONE)) { |
| 554 | vfio_msi_disable(vdev, msix); |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | if (!(irq_is(vdev, index) || is_irq_none(vdev))) |
| 559 | return -EINVAL; |
| 560 | |
| 561 | if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { |
| 562 | int32_t *fds = data; |
| 563 | int ret; |
| 564 | |
| 565 | if (vdev->irq_type == index) |
| 566 | return vfio_msi_set_block(vdev, start, count, |
| 567 | fds, msix); |
| 568 | |
| 569 | ret = vfio_msi_enable(vdev, start + count, msix); |
| 570 | if (ret) |
| 571 | return ret; |
| 572 | |
| 573 | ret = vfio_msi_set_block(vdev, start, count, fds, msix); |
| 574 | if (ret) |
| 575 | vfio_msi_disable(vdev, msix); |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | if (!irq_is(vdev, index) || start + count > vdev->num_ctx) |
| 581 | return -EINVAL; |
| 582 | |
| 583 | for (i = start; i < start + count; i++) { |
| 584 | if (!vdev->ctx[i].trigger) |
| 585 | continue; |
| 586 | if (flags & VFIO_IRQ_SET_DATA_NONE) { |
| 587 | eventfd_signal(vdev->ctx[i].trigger, 1); |
| 588 | } else if (flags & VFIO_IRQ_SET_DATA_BOOL) { |
| 589 | uint8_t *bools = data; |
| 590 | if (bools[i - start]) |
| 591 | eventfd_signal(vdev->ctx[i].trigger, 1); |
| 592 | } |
| 593 | } |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static int vfio_pci_set_ctx_trigger_single(struct eventfd_ctx **ctx, |
| 598 | unsigned int count, uint32_t flags, |
| 599 | void *data) |
| 600 | { |
| 601 | /* DATA_NONE/DATA_BOOL enables loopback testing */ |
| 602 | if (flags & VFIO_IRQ_SET_DATA_NONE) { |
| 603 | if (*ctx) { |
| 604 | if (count) { |
| 605 | eventfd_signal(*ctx, 1); |
| 606 | } else { |
| 607 | eventfd_ctx_put(*ctx); |
| 608 | *ctx = NULL; |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | } else if (flags & VFIO_IRQ_SET_DATA_BOOL) { |
| 613 | uint8_t trigger; |
| 614 | |
| 615 | if (!count) |
| 616 | return -EINVAL; |
| 617 | |
| 618 | trigger = *(uint8_t *)data; |
| 619 | if (trigger && *ctx) |
| 620 | eventfd_signal(*ctx, 1); |
| 621 | |
| 622 | return 0; |
| 623 | } else if (flags & VFIO_IRQ_SET_DATA_EVENTFD) { |
| 624 | int32_t fd; |
| 625 | |
| 626 | if (!count) |
| 627 | return -EINVAL; |
| 628 | |
| 629 | fd = *(int32_t *)data; |
| 630 | if (fd == -1) { |
| 631 | if (*ctx) |
| 632 | eventfd_ctx_put(*ctx); |
| 633 | *ctx = NULL; |
| 634 | } else if (fd >= 0) { |
| 635 | struct eventfd_ctx *efdctx; |
| 636 | |
| 637 | efdctx = eventfd_ctx_fdget(fd); |
| 638 | if (IS_ERR(efdctx)) |
| 639 | return PTR_ERR(efdctx); |
| 640 | |
| 641 | if (*ctx) |
| 642 | eventfd_ctx_put(*ctx); |
| 643 | |
| 644 | *ctx = efdctx; |
| 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | return -EINVAL; |
| 650 | } |
| 651 | |
| 652 | static int vfio_pci_set_err_trigger(struct vfio_pci_device *vdev, |
| 653 | unsigned index, unsigned start, |
| 654 | unsigned count, uint32_t flags, void *data) |
| 655 | { |
| 656 | if (index != VFIO_PCI_ERR_IRQ_INDEX || start != 0 || count > 1) |
| 657 | return -EINVAL; |
| 658 | |
| 659 | return vfio_pci_set_ctx_trigger_single(&vdev->err_trigger, |
| 660 | count, flags, data); |
| 661 | } |
| 662 | |
| 663 | static int vfio_pci_set_req_trigger(struct vfio_pci_device *vdev, |
| 664 | unsigned index, unsigned start, |
| 665 | unsigned count, uint32_t flags, void *data) |
| 666 | { |
| 667 | if (index != VFIO_PCI_REQ_IRQ_INDEX || start != 0 || count > 1) |
| 668 | return -EINVAL; |
| 669 | |
| 670 | return vfio_pci_set_ctx_trigger_single(&vdev->req_trigger, |
| 671 | count, flags, data); |
| 672 | } |
| 673 | |
| 674 | int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev, uint32_t flags, |
| 675 | unsigned index, unsigned start, unsigned count, |
| 676 | void *data) |
| 677 | { |
| 678 | int (*func)(struct vfio_pci_device *vdev, unsigned index, |
| 679 | unsigned start, unsigned count, uint32_t flags, |
| 680 | void *data) = NULL; |
| 681 | |
| 682 | switch (index) { |
| 683 | case VFIO_PCI_INTX_IRQ_INDEX: |
| 684 | switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) { |
| 685 | case VFIO_IRQ_SET_ACTION_MASK: |
| 686 | func = vfio_pci_set_intx_mask; |
| 687 | break; |
| 688 | case VFIO_IRQ_SET_ACTION_UNMASK: |
| 689 | func = vfio_pci_set_intx_unmask; |
| 690 | break; |
| 691 | case VFIO_IRQ_SET_ACTION_TRIGGER: |
| 692 | func = vfio_pci_set_intx_trigger; |
| 693 | break; |
| 694 | } |
| 695 | break; |
| 696 | case VFIO_PCI_MSI_IRQ_INDEX: |
| 697 | case VFIO_PCI_MSIX_IRQ_INDEX: |
| 698 | switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) { |
| 699 | case VFIO_IRQ_SET_ACTION_MASK: |
| 700 | case VFIO_IRQ_SET_ACTION_UNMASK: |
| 701 | /* XXX Need masking support exported */ |
| 702 | break; |
| 703 | case VFIO_IRQ_SET_ACTION_TRIGGER: |
| 704 | func = vfio_pci_set_msi_trigger; |
| 705 | break; |
| 706 | } |
| 707 | break; |
| 708 | case VFIO_PCI_ERR_IRQ_INDEX: |
| 709 | switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) { |
| 710 | case VFIO_IRQ_SET_ACTION_TRIGGER: |
| 711 | if (pci_is_pcie(vdev->pdev)) |
| 712 | func = vfio_pci_set_err_trigger; |
| 713 | break; |
| 714 | } |
| 715 | break; |
| 716 | case VFIO_PCI_REQ_IRQ_INDEX: |
| 717 | switch (flags & VFIO_IRQ_SET_ACTION_TYPE_MASK) { |
| 718 | case VFIO_IRQ_SET_ACTION_TRIGGER: |
| 719 | func = vfio_pci_set_req_trigger; |
| 720 | break; |
| 721 | } |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | if (!func) |
| 726 | return -ENOTTY; |
| 727 | |
| 728 | return func(vdev, index, start, count, flags, data); |
| 729 | } |