1 // SPDX-License-Identifier: GPL-2.0+
3 * Adjunct processor matrix VFIO device driver callbacks.
5 * Copyright IBM Corp. 2018
11 #include <linux/string.h>
12 #include <linux/vfio.h>
13 #include <linux/device.h>
14 #include <linux/list.h>
15 #include <linux/ctype.h>
16 #include <linux/bitops.h>
17 #include <linux/kvm_host.h>
18 #include <linux/module.h>
19 #include <linux/uuid.h>
21 #include <asm/zcrypt.h>
23 #include "vfio_ap_private.h"
24 #include "vfio_ap_debug.h"
26 #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
27 #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
29 #define AP_QUEUE_ASSIGNED "assigned"
30 #define AP_QUEUE_UNASSIGNED "unassigned"
31 #define AP_QUEUE_IN_USE "in use"
33 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable);
34 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
35 static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
36 static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q, unsigned int retry);
39 * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
40 * KVM guest's APCB in the proper order.
42 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
44 * The proper locking order is:
45 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
47 * 2. kvm->lock: required to update a guest's APCB
48 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
50 * Note: If @kvm is NULL, the KVM lock will not be taken.
52 static inline void get_update_locks_for_kvm(struct kvm *kvm)
54 mutex_lock(&matrix_dev->guests_lock);
56 mutex_lock(&kvm->lock);
57 mutex_lock(&matrix_dev->mdevs_lock);
61 * release_update_locks_for_kvm: Release the locks used to dynamically update a
62 * KVM guest's APCB in the proper order.
64 * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
66 * The proper unlocking order is:
67 * 1. matrix_dev->mdevs_lock
69 * 3. matrix_dev->guests_lock
71 * Note: If @kvm is NULL, the KVM lock will not be released.
73 static inline void release_update_locks_for_kvm(struct kvm *kvm)
75 mutex_unlock(&matrix_dev->mdevs_lock);
77 mutex_unlock(&kvm->lock);
78 mutex_unlock(&matrix_dev->guests_lock);
82 * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
83 * KVM guest's APCB in the proper order.
85 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
86 * configuration data to use to update a KVM guest's APCB.
88 * The proper locking order is:
89 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
91 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
92 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
94 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
95 * lock will not be taken.
97 static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
99 mutex_lock(&matrix_dev->guests_lock);
100 if (matrix_mdev && matrix_mdev->kvm)
101 mutex_lock(&matrix_mdev->kvm->lock);
102 mutex_lock(&matrix_dev->mdevs_lock);
106 * release_update_locks_for_mdev: Release the locks used to dynamically update a
107 * KVM guest's APCB in the proper order.
109 * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
110 * configuration data to use to update a KVM guest's APCB.
112 * The proper unlocking order is:
113 * 1. matrix_dev->mdevs_lock
114 * 2. matrix_mdev->kvm->lock
115 * 3. matrix_dev->guests_lock
117 * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
118 * lock will not be released.
120 static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
122 mutex_unlock(&matrix_dev->mdevs_lock);
123 if (matrix_mdev && matrix_mdev->kvm)
124 mutex_unlock(&matrix_mdev->kvm->lock);
125 mutex_unlock(&matrix_dev->guests_lock);
129 * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
130 * acquire the locks required to update the APCB of
131 * the KVM guest to which the mdev is attached.
133 * @apqn: the APQN of a queue device.
135 * The proper locking order is:
136 * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
138 * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
139 * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
141 * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
144 * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn
145 * is not assigned to an ap_matrix_mdev.
147 static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
149 struct ap_matrix_mdev *matrix_mdev;
151 mutex_lock(&matrix_dev->guests_lock);
153 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
154 if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) &&
155 test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) {
156 if (matrix_mdev->kvm)
157 mutex_lock(&matrix_mdev->kvm->lock);
159 mutex_lock(&matrix_dev->mdevs_lock);
165 mutex_lock(&matrix_dev->mdevs_lock);
171 * get_update_locks_for_queue: get the locks required to update the APCB of the
172 * KVM guest to which the matrix mdev linked to a
173 * vfio_ap_queue object is attached.
175 * @q: a pointer to a vfio_ap_queue object.
177 * The proper locking order is:
178 * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
180 * 2. q->matrix_mdev->kvm->lock: required to update a guest's APCB
181 * 3. matrix_dev->mdevs_lock: required to access data stored in matrix_mdev
183 * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
186 static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
188 mutex_lock(&matrix_dev->guests_lock);
189 if (q->matrix_mdev && q->matrix_mdev->kvm)
190 mutex_lock(&q->matrix_mdev->kvm->lock);
191 mutex_lock(&matrix_dev->mdevs_lock);
195 * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
196 * hash table of queues assigned to a matrix mdev
197 * @matrix_mdev: the matrix mdev
198 * @apqn: The APQN of a queue device
200 * Return: the pointer to the vfio_ap_queue struct representing the queue or
201 * NULL if the queue is not assigned to @matrix_mdev
203 static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
204 struct ap_matrix_mdev *matrix_mdev,
207 struct vfio_ap_queue *q;
209 hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
211 if (q && q->apqn == apqn)
219 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
220 * @apqn: The AP Queue number
222 * Checks the IRQ bit for the status of this APQN using ap_tapq.
223 * Returns if the ap_tapq function succeeded and the bit is clear.
224 * Returns if ap_tapq function failed with invalid, deconfigured or
226 * Otherwise retries up to 5 times after waiting 20ms.
228 static void vfio_ap_wait_for_irqclear(int apqn)
230 struct ap_queue_status status;
234 status = ap_tapq(apqn, NULL);
235 switch (status.response_code) {
236 case AP_RESPONSE_NORMAL:
237 case AP_RESPONSE_RESET_IN_PROGRESS:
238 if (!status.irq_enabled)
241 case AP_RESPONSE_BUSY:
244 case AP_RESPONSE_Q_NOT_AVAIL:
245 case AP_RESPONSE_DECONFIGURED:
246 case AP_RESPONSE_CHECKSTOPPED:
248 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
249 status.response_code, apqn);
254 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
255 __func__, status.response_code, apqn);
259 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
260 * @q: The vfio_ap_queue
262 * Unregisters the ISC in the GIB when the saved ISC not invalid.
263 * Unpins the guest's page holding the NIB when it exists.
264 * Resets the saved_iova and saved_isc to invalid values.
266 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
270 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
271 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
272 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
273 q->saved_isc = VFIO_AP_ISC_INVALID;
275 if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
276 vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
282 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
283 * @q: The vfio_ap_queue
285 * Uses ap_aqic to disable the interruption and in case of success, reset
286 * in progress or IRQ disable command already proceeded: calls
287 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
288 * and calls vfio_ap_free_aqic_resources() to free the resources associated
289 * with the AP interrupt handling.
291 * In the case the AP is busy, or a reset is in progress,
292 * retries after 20ms, up to 5 times.
294 * Returns if ap_aqic function failed with invalid, deconfigured or
297 * Return: &struct ap_queue_status
299 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
301 struct ap_qirq_ctrl aqic_gisa = {};
302 struct ap_queue_status status;
306 status = ap_aqic(q->apqn, aqic_gisa, 0);
307 switch (status.response_code) {
308 case AP_RESPONSE_OTHERWISE_CHANGED:
309 case AP_RESPONSE_NORMAL:
310 vfio_ap_wait_for_irqclear(q->apqn);
312 case AP_RESPONSE_RESET_IN_PROGRESS:
313 case AP_RESPONSE_BUSY:
316 case AP_RESPONSE_Q_NOT_AVAIL:
317 case AP_RESPONSE_DECONFIGURED:
318 case AP_RESPONSE_CHECKSTOPPED:
319 case AP_RESPONSE_INVALID_ADDRESS:
321 /* All cases in default means AP not operational */
322 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
323 status.response_code);
328 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
329 status.response_code);
331 vfio_ap_free_aqic_resources(q);
336 * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
338 * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
339 * @nib: the location for storing the nib address.
341 * When the PQAP(AQIC) instruction is executed, general register 2 contains the
342 * address of the notification indicator byte (nib) used for IRQ notification.
343 * This function parses and validates the nib from gr2.
345 * Return: returns zero if the nib address is a valid; otherwise, returns
348 static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
350 *nib = vcpu->run->s.regs.gprs[2];
352 if (kvm_is_error_hva(gfn_to_hva(vcpu->kvm, *nib >> PAGE_SHIFT)))
359 * vfio_ap_irq_enable - Enable Interruption for a APQN
361 * @q: the vfio_ap_queue holding AQIC parameters
362 * @isc: the guest ISC to register with the GIB interface
363 * @vcpu: the vcpu object containing the registers specifying the parameters
364 * passed to the PQAP(AQIC) instruction.
366 * Pin the NIB saved in *q
367 * Register the guest ISC to GIB interface and retrieve the
368 * host ISC to issue the host side PQAP/AQIC
370 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
371 * vfio_pin_pages failed.
373 * Otherwise return the ap_queue_status returned by the ap_aqic(),
374 * all retry handling will be done by the guest.
376 * Return: &struct ap_queue_status
378 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
380 struct kvm_vcpu *vcpu)
382 struct ap_qirq_ctrl aqic_gisa = {};
383 struct ap_queue_status status = {};
384 struct kvm_s390_gisa *gisa;
392 /* Verify that the notification indicator byte address is valid */
393 if (vfio_ap_validate_nib(vcpu, &nib)) {
394 VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
395 __func__, &nib, q->apqn);
397 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
401 ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
402 IOMMU_READ | IOMMU_WRITE, &h_page);
407 VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
408 "nib=%pad, apqn=%#04x\n",
409 __func__, ret, &nib, q->apqn);
411 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
415 kvm = q->matrix_mdev->kvm;
416 gisa = kvm->arch.gisa_int.origin;
418 h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
419 aqic_gisa.gisc = isc;
421 nisc = kvm_s390_gisc_register(kvm, isc);
423 VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
424 __func__, nisc, isc, q->apqn);
426 status.response_code = AP_RESPONSE_INVALID_GISA;
430 aqic_gisa.isc = nisc;
432 aqic_gisa.gisa = (uint64_t)gisa >> 4;
434 status = ap_aqic(q->apqn, aqic_gisa, h_nib);
435 switch (status.response_code) {
436 case AP_RESPONSE_NORMAL:
437 /* See if we did clear older IRQ configuration */
438 vfio_ap_free_aqic_resources(q);
442 case AP_RESPONSE_OTHERWISE_CHANGED:
443 /* We could not modify IRQ setings: clear new configuration */
444 vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
445 kvm_s390_gisc_unregister(kvm, isc);
448 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
449 status.response_code);
450 vfio_ap_irq_disable(q);
454 if (status.response_code != AP_RESPONSE_NORMAL) {
455 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
456 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
457 "gisa=%#x, isc=%#x, apqn=%#04x\n",
458 __func__, status.response_code,
459 aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
460 aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
468 * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
469 * of big endian elements that can be passed by
470 * value to an s390dbf sprintf event function to
471 * format a UUID string.
473 * @guid: the object containing the little endian guid
474 * @uuid: a six-element array of long values that can be passed by value as
475 * arguments for a formatting string specifying a UUID.
477 * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
478 * event functions if the memory for the passed string is available as long as
479 * the debug feature exists. Since a mediated device can be removed at any
480 * time, it's name can not be used because %s passes the reference to the string
481 * in memory and the reference will go stale once the device is removed .
483 * The s390dbf string formatting function allows a maximum of 9 arguments for a
484 * message to be displayed in the 'sprintf' view. In order to use the bytes
485 * comprising the mediated device's UUID to display the mediated device name,
486 * they will have to be converted into an array whose elements can be passed by
487 * value to sprintf. For example:
489 * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
490 * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
491 * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
492 * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
494 static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
497 * The input guid is ordered in little endian, so it needs to be
498 * reordered for displaying a UUID as a string. This specifies the
499 * guid indices in proper order.
501 uuid[0] = le32_to_cpup((__le32 *)guid);
502 uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
503 uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
504 uuid[3] = *((__u16 *)&guid->b[8]);
505 uuid[4] = *((__u16 *)&guid->b[10]);
506 uuid[5] = *((__u32 *)&guid->b[12]);
510 * handle_pqap - PQAP instruction callback
512 * @vcpu: The vcpu on which we received the PQAP instruction
514 * Get the general register contents to initialize internal variables.
519 * Response.status may be set to following Response Code:
520 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
521 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
522 * - AP_RESPONSE_NORMAL (0) : in case of successs
523 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
524 * We take the matrix_dev lock to ensure serialization on queues and
525 * mediated device access.
527 * Return: 0 if we could handle the request inside KVM.
528 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
530 static int handle_pqap(struct kvm_vcpu *vcpu)
534 unsigned long uuid[6];
535 struct vfio_ap_queue *q;
536 struct ap_queue_status qstatus = {
537 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
538 struct ap_matrix_mdev *matrix_mdev;
540 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
542 /* If we do not use the AIV facility just go to userland */
543 if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
544 VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
545 __func__, apqn, vcpu->arch.sie_block->eca);
550 mutex_lock(&matrix_dev->mdevs_lock);
552 if (!vcpu->kvm->arch.crypto.pqap_hook) {
553 VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
559 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
560 struct ap_matrix_mdev, pqap_hook);
562 /* If the there is no guest using the mdev, there is nothing to do */
563 if (!matrix_mdev->kvm) {
564 vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
565 VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
566 __func__, uuid[0], uuid[1], uuid[2],
567 uuid[3], uuid[4], uuid[5], apqn);
571 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
573 VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
574 __func__, AP_QID_CARD(apqn),
579 status = vcpu->run->s.regs.gprs[1];
581 /* If IR bit(16) is set we enable the interrupt */
582 if ((status >> (63 - 16)) & 0x01)
583 qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
585 qstatus = vfio_ap_irq_disable(q);
588 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
589 vcpu->run->s.regs.gprs[1] >>= 32;
590 mutex_unlock(&matrix_dev->mdevs_lock);
594 static void vfio_ap_matrix_init(struct ap_config_info *info,
595 struct ap_matrix *matrix)
597 matrix->apm_max = info->apxa ? info->Na : 63;
598 matrix->aqm_max = info->apxa ? info->Nd : 15;
599 matrix->adm_max = info->apxa ? info->Nd : 15;
602 static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
604 if (matrix_mdev->kvm)
605 kvm_arch_crypto_set_masks(matrix_mdev->kvm,
606 matrix_mdev->shadow_apcb.apm,
607 matrix_mdev->shadow_apcb.aqm,
608 matrix_mdev->shadow_apcb.adm);
611 static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
613 DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
615 bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
616 bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
617 (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
619 return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
624 * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
625 * to ensure no queue devices are passed through to
626 * the guest that are not bound to the vfio_ap
629 * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
631 * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
632 * driver, its APID will be filtered from the guest's APCB. The matrix
633 * structure precludes filtering an individual APQN, so its APID will be
636 * Return: a boolean value indicating whether the KVM guest's APCB was changed
637 * by the filtering or not.
639 static bool vfio_ap_mdev_filter_matrix(unsigned long *apm, unsigned long *aqm,
640 struct ap_matrix_mdev *matrix_mdev)
642 unsigned long apid, apqi, apqn;
643 DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
644 DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
645 struct vfio_ap_queue *q;
647 bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
648 bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
649 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
652 * Copy the adapters, domains and control domains to the shadow_apcb
653 * from the matrix mdev, but only those that are assigned to the host's
656 bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
657 (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
658 bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
659 (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
661 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
662 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
664 * If the APQN is not bound to the vfio_ap device
665 * driver, then we can't assign it to the guest's
666 * AP configuration. The AP architecture won't
667 * allow filtering of a single APQN, so let's filter
668 * the APID since an adapter represents a physical
671 apqn = AP_MKQID(apid, apqi);
672 q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
673 if (!q || q->reset_rc) {
675 matrix_mdev->shadow_apcb.apm);
681 return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
683 !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
687 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
689 struct ap_matrix_mdev *matrix_mdev;
692 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
695 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
698 goto err_dec_available;
700 vfio_init_group_dev(&matrix_mdev->vdev, &mdev->dev,
701 &vfio_ap_matrix_dev_ops);
703 matrix_mdev->mdev = mdev;
704 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
705 matrix_mdev->pqap_hook = handle_pqap;
706 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
707 hash_init(matrix_mdev->qtable.queues);
709 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
712 dev_set_drvdata(&mdev->dev, matrix_mdev);
713 mutex_lock(&matrix_dev->mdevs_lock);
714 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
715 mutex_unlock(&matrix_dev->mdevs_lock);
719 vfio_uninit_group_dev(&matrix_mdev->vdev);
722 atomic_inc(&matrix_dev->available_instances);
726 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
727 struct vfio_ap_queue *q)
730 q->matrix_mdev = matrix_mdev;
731 hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
735 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
737 struct vfio_ap_queue *q;
739 q = vfio_ap_find_queue(apqn);
740 vfio_ap_mdev_link_queue(matrix_mdev, q);
743 static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
745 hash_del(&q->mdev_qnode);
748 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
750 q->matrix_mdev = NULL;
753 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
755 struct vfio_ap_queue *q;
756 unsigned long apid, apqi;
758 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
759 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
761 q = vfio_ap_mdev_get_queue(matrix_mdev,
762 AP_MKQID(apid, apqi));
764 q->matrix_mdev = NULL;
769 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
771 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
773 vfio_unregister_group_dev(&matrix_mdev->vdev);
775 mutex_lock(&matrix_dev->guests_lock);
776 mutex_lock(&matrix_dev->mdevs_lock);
777 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
778 vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
779 list_del(&matrix_mdev->node);
780 mutex_unlock(&matrix_dev->mdevs_lock);
781 mutex_unlock(&matrix_dev->guests_lock);
782 vfio_uninit_group_dev(&matrix_mdev->vdev);
784 atomic_inc(&matrix_dev->available_instances);
787 static ssize_t name_show(struct mdev_type *mtype,
788 struct mdev_type_attribute *attr, char *buf)
790 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
793 static MDEV_TYPE_ATTR_RO(name);
795 static ssize_t available_instances_show(struct mdev_type *mtype,
796 struct mdev_type_attribute *attr,
799 return sprintf(buf, "%d\n",
800 atomic_read(&matrix_dev->available_instances));
803 static MDEV_TYPE_ATTR_RO(available_instances);
805 static ssize_t device_api_show(struct mdev_type *mtype,
806 struct mdev_type_attribute *attr, char *buf)
808 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
811 static MDEV_TYPE_ATTR_RO(device_api);
813 static struct attribute *vfio_ap_mdev_type_attrs[] = {
814 &mdev_type_attr_name.attr,
815 &mdev_type_attr_device_api.attr,
816 &mdev_type_attr_available_instances.attr,
820 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
821 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
822 .attrs = vfio_ap_mdev_type_attrs,
825 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
826 &vfio_ap_mdev_hwvirt_type_group,
830 #define MDEV_SHARING_ERR "Userspace may not re-assign queue %02lx.%04lx " \
831 "already assigned to %s"
833 static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *matrix_mdev,
837 unsigned long apid, apqi;
838 const struct device *dev = mdev_dev(matrix_mdev->mdev);
839 const char *mdev_name = dev_name(dev);
841 for_each_set_bit_inv(apid, apm, AP_DEVICES)
842 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
843 dev_warn(dev, MDEV_SHARING_ERR, apid, apqi, mdev_name);
847 * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
849 * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
850 * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
852 * Verifies that each APQN derived from the Cartesian product of a bitmap of
853 * AP adapter IDs and AP queue indexes is not configured for any matrix
854 * mediated device. AP queue sharing is not allowed.
856 * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
858 static int vfio_ap_mdev_verify_no_sharing(unsigned long *mdev_apm,
859 unsigned long *mdev_aqm)
861 struct ap_matrix_mdev *matrix_mdev;
862 DECLARE_BITMAP(apm, AP_DEVICES);
863 DECLARE_BITMAP(aqm, AP_DOMAINS);
865 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
867 * If the input apm and aqm are fields of the matrix_mdev
868 * object, then move on to the next matrix_mdev.
870 if (mdev_apm == matrix_mdev->matrix.apm &&
871 mdev_aqm == matrix_mdev->matrix.aqm)
874 memset(apm, 0, sizeof(apm));
875 memset(aqm, 0, sizeof(aqm));
878 * We work on full longs, as we can only exclude the leftover
879 * bits in non-inverse order. The leftover is all zeros.
881 if (!bitmap_and(apm, mdev_apm, matrix_mdev->matrix.apm,
885 if (!bitmap_and(aqm, mdev_aqm, matrix_mdev->matrix.aqm,
889 vfio_ap_mdev_log_sharing_err(matrix_mdev, apm, aqm);
898 * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
899 * not reserved for the default zcrypt driver and
900 * are not assigned to another mdev.
902 * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
904 * Return: One of the following values:
905 * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
906 * most likely -EBUSY indicating the ap_perms_mutex lock is already held.
907 * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
908 * zcrypt default driver.
909 * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
910 * o A zero indicating validation succeeded.
912 static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
914 if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
915 matrix_mdev->matrix.aqm))
916 return -EADDRNOTAVAIL;
918 return vfio_ap_mdev_verify_no_sharing(matrix_mdev->matrix.apm,
919 matrix_mdev->matrix.aqm);
922 static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
927 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
928 vfio_ap_mdev_link_apqn(matrix_mdev,
929 AP_MKQID(apid, apqi));
933 * assign_adapter_store - parses the APID from @buf and sets the
934 * corresponding bit in the mediated matrix device's APM
936 * @dev: the matrix device
937 * @attr: the mediated matrix device's assign_adapter attribute
938 * @buf: a buffer containing the AP adapter number (APID) to
940 * @count: the number of bytes in @buf
942 * Return: the number of bytes processed if the APID is valid; otherwise,
943 * returns one of the following errors:
946 * The APID is not a valid number
949 * The APID exceeds the maximum value configured for the system
952 * An APQN derived from the cross product of the APID being assigned
953 * and the APQIs previously assigned is not bound to the vfio_ap device
954 * driver; or, if no APQIs have yet been assigned, the APID is not
955 * contained in an APQN bound to the vfio_ap device driver.
958 * An APQN derived from the cross product of the APID being assigned
959 * and the APQIs previously assigned is being used by another mediated
963 * A lock required to validate the mdev's AP configuration could not
966 static ssize_t assign_adapter_store(struct device *dev,
967 struct device_attribute *attr,
968 const char *buf, size_t count)
972 DECLARE_BITMAP(apm_delta, AP_DEVICES);
973 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
975 mutex_lock(&ap_perms_mutex);
976 get_update_locks_for_mdev(matrix_mdev);
978 ret = kstrtoul(buf, 0, &apid);
982 if (apid > matrix_mdev->matrix.apm_max) {
987 set_bit_inv(apid, matrix_mdev->matrix.apm);
989 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
991 clear_bit_inv(apid, matrix_mdev->matrix.apm);
995 vfio_ap_mdev_link_adapter(matrix_mdev, apid);
996 memset(apm_delta, 0, sizeof(apm_delta));
997 set_bit_inv(apid, apm_delta);
999 if (vfio_ap_mdev_filter_matrix(apm_delta,
1000 matrix_mdev->matrix.aqm, matrix_mdev))
1001 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1005 release_update_locks_for_mdev(matrix_mdev);
1006 mutex_unlock(&ap_perms_mutex);
1010 static DEVICE_ATTR_WO(assign_adapter);
1012 static struct vfio_ap_queue
1013 *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
1014 unsigned long apid, unsigned long apqi)
1016 struct vfio_ap_queue *q = NULL;
1018 q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
1019 /* If the queue is assigned to the matrix mdev, unlink it. */
1021 vfio_ap_unlink_queue_fr_mdev(q);
1027 * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
1028 * adapter from the matrix mdev to which the
1029 * adapter was assigned.
1030 * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
1031 * @apid: the APID of the unassigned adapter.
1032 * @qtable: table for storing queues associated with unassigned adapter.
1034 static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
1036 struct ap_queue_table *qtable)
1039 struct vfio_ap_queue *q;
1041 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
1042 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1045 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1046 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1047 hash_add(qtable->queues, &q->mdev_qnode,
1053 static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
1057 struct vfio_ap_queue *q;
1058 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1060 hash_init(qtable->queues);
1061 vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, qtable);
1063 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
1064 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1065 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1068 vfio_ap_mdev_reset_queues(qtable);
1070 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1071 vfio_ap_unlink_mdev_fr_queue(q);
1072 hash_del(&q->mdev_qnode);
1079 * unassign_adapter_store - parses the APID from @buf and clears the
1080 * corresponding bit in the mediated matrix device's APM
1082 * @dev: the matrix device
1083 * @attr: the mediated matrix device's unassign_adapter attribute
1084 * @buf: a buffer containing the adapter number (APID) to be unassigned
1085 * @count: the number of bytes in @buf
1087 * Return: the number of bytes processed if the APID is valid; otherwise,
1088 * returns one of the following errors:
1089 * -EINVAL if the APID is not a number
1090 * -ENODEV if the APID it exceeds the maximum value configured for the
1093 static ssize_t unassign_adapter_store(struct device *dev,
1094 struct device_attribute *attr,
1095 const char *buf, size_t count)
1099 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1101 get_update_locks_for_mdev(matrix_mdev);
1103 ret = kstrtoul(buf, 0, &apid);
1107 if (apid > matrix_mdev->matrix.apm_max) {
1112 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
1113 vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
1116 release_update_locks_for_mdev(matrix_mdev);
1119 static DEVICE_ATTR_WO(unassign_adapter);
1121 static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
1126 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
1127 vfio_ap_mdev_link_apqn(matrix_mdev,
1128 AP_MKQID(apid, apqi));
1132 * assign_domain_store - parses the APQI from @buf and sets the
1133 * corresponding bit in the mediated matrix device's AQM
1135 * @dev: the matrix device
1136 * @attr: the mediated matrix device's assign_domain attribute
1137 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1139 * @count: the number of bytes in @buf
1141 * Return: the number of bytes processed if the APQI is valid; otherwise returns
1142 * one of the following errors:
1145 * The APQI is not a valid number
1148 * The APQI exceeds the maximum value configured for the system
1151 * An APQN derived from the cross product of the APQI being assigned
1152 * and the APIDs previously assigned is not bound to the vfio_ap device
1153 * driver; or, if no APIDs have yet been assigned, the APQI is not
1154 * contained in an APQN bound to the vfio_ap device driver.
1157 * An APQN derived from the cross product of the APQI being assigned
1158 * and the APIDs previously assigned is being used by another mediated
1162 * The lock required to validate the mdev's AP configuration could not
1165 static ssize_t assign_domain_store(struct device *dev,
1166 struct device_attribute *attr,
1167 const char *buf, size_t count)
1171 DECLARE_BITMAP(aqm_delta, AP_DOMAINS);
1172 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1174 mutex_lock(&ap_perms_mutex);
1175 get_update_locks_for_mdev(matrix_mdev);
1177 ret = kstrtoul(buf, 0, &apqi);
1181 if (apqi > matrix_mdev->matrix.aqm_max) {
1186 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
1188 ret = vfio_ap_mdev_validate_masks(matrix_mdev);
1190 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
1194 vfio_ap_mdev_link_domain(matrix_mdev, apqi);
1195 memset(aqm_delta, 0, sizeof(aqm_delta));
1196 set_bit_inv(apqi, aqm_delta);
1198 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm, aqm_delta,
1200 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1204 release_update_locks_for_mdev(matrix_mdev);
1205 mutex_unlock(&ap_perms_mutex);
1209 static DEVICE_ATTR_WO(assign_domain);
1211 static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
1213 struct ap_queue_table *qtable)
1216 struct vfio_ap_queue *q;
1218 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
1219 q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
1222 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1223 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
1224 hash_add(qtable->queues, &q->mdev_qnode,
1230 static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
1234 struct vfio_ap_queue *q;
1235 struct ap_queue_table *qtable = kzalloc(sizeof(*qtable), GFP_KERNEL);
1237 hash_init(qtable->queues);
1238 vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, qtable);
1240 if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1241 clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
1242 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1245 vfio_ap_mdev_reset_queues(qtable);
1247 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1248 vfio_ap_unlink_mdev_fr_queue(q);
1249 hash_del(&q->mdev_qnode);
1256 * unassign_domain_store - parses the APQI from @buf and clears the
1257 * corresponding bit in the mediated matrix device's AQM
1259 * @dev: the matrix device
1260 * @attr: the mediated matrix device's unassign_domain attribute
1261 * @buf: a buffer containing the AP queue index (APQI) of the domain to
1263 * @count: the number of bytes in @buf
1265 * Return: the number of bytes processed if the APQI is valid; otherwise,
1266 * returns one of the following errors:
1267 * -EINVAL if the APQI is not a number
1268 * -ENODEV if the APQI exceeds the maximum value configured for the system
1270 static ssize_t unassign_domain_store(struct device *dev,
1271 struct device_attribute *attr,
1272 const char *buf, size_t count)
1276 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1278 get_update_locks_for_mdev(matrix_mdev);
1280 ret = kstrtoul(buf, 0, &apqi);
1284 if (apqi > matrix_mdev->matrix.aqm_max) {
1289 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
1290 vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
1294 release_update_locks_for_mdev(matrix_mdev);
1297 static DEVICE_ATTR_WO(unassign_domain);
1300 * assign_control_domain_store - parses the domain ID from @buf and sets
1301 * the corresponding bit in the mediated matrix device's ADM
1303 * @dev: the matrix device
1304 * @attr: the mediated matrix device's assign_control_domain attribute
1305 * @buf: a buffer containing the domain ID to be assigned
1306 * @count: the number of bytes in @buf
1308 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1309 * returns one of the following errors:
1310 * -EINVAL if the ID is not a number
1311 * -ENODEV if the ID exceeds the maximum value configured for the system
1313 static ssize_t assign_control_domain_store(struct device *dev,
1314 struct device_attribute *attr,
1315 const char *buf, size_t count)
1319 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1321 get_update_locks_for_mdev(matrix_mdev);
1323 ret = kstrtoul(buf, 0, &id);
1327 if (id > matrix_mdev->matrix.adm_max) {
1332 /* Set the bit in the ADM (bitmask) corresponding to the AP control
1333 * domain number (id). The bits in the mask, from most significant to
1334 * least significant, correspond to IDs 0 up to the one less than the
1335 * number of control domains that can be assigned.
1337 set_bit_inv(id, matrix_mdev->matrix.adm);
1338 if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
1339 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1343 release_update_locks_for_mdev(matrix_mdev);
1346 static DEVICE_ATTR_WO(assign_control_domain);
1349 * unassign_control_domain_store - parses the domain ID from @buf and
1350 * clears the corresponding bit in the mediated matrix device's ADM
1352 * @dev: the matrix device
1353 * @attr: the mediated matrix device's unassign_control_domain attribute
1354 * @buf: a buffer containing the domain ID to be unassigned
1355 * @count: the number of bytes in @buf
1357 * Return: the number of bytes processed if the domain ID is valid; otherwise,
1358 * returns one of the following errors:
1359 * -EINVAL if the ID is not a number
1360 * -ENODEV if the ID exceeds the maximum value configured for the system
1362 static ssize_t unassign_control_domain_store(struct device *dev,
1363 struct device_attribute *attr,
1364 const char *buf, size_t count)
1367 unsigned long domid;
1368 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1370 get_update_locks_for_mdev(matrix_mdev);
1372 ret = kstrtoul(buf, 0, &domid);
1376 if (domid > matrix_mdev->matrix.adm_max) {
1381 clear_bit_inv(domid, matrix_mdev->matrix.adm);
1383 if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
1384 clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
1385 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1390 release_update_locks_for_mdev(matrix_mdev);
1393 static DEVICE_ATTR_WO(unassign_control_domain);
1395 static ssize_t control_domains_show(struct device *dev,
1396 struct device_attribute *dev_attr,
1403 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1404 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1406 mutex_lock(&matrix_dev->mdevs_lock);
1407 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1408 n = sprintf(bufpos, "%04lx\n", id);
1412 mutex_unlock(&matrix_dev->mdevs_lock);
1416 static DEVICE_ATTR_RO(control_domains);
1418 static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
1423 unsigned long apid1;
1424 unsigned long apqi1;
1425 unsigned long napm_bits = matrix->apm_max + 1;
1426 unsigned long naqm_bits = matrix->aqm_max + 1;
1430 apid1 = find_first_bit_inv(matrix->apm, napm_bits);
1431 apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
1433 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1434 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1435 for_each_set_bit_inv(apqi, matrix->aqm,
1437 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1443 } else if (apid1 < napm_bits) {
1444 for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
1445 n = sprintf(bufpos, "%02lx.\n", apid);
1449 } else if (apqi1 < naqm_bits) {
1450 for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits) {
1451 n = sprintf(bufpos, ".%04lx\n", apqi);
1460 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1464 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1466 mutex_lock(&matrix_dev->mdevs_lock);
1467 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
1468 mutex_unlock(&matrix_dev->mdevs_lock);
1472 static DEVICE_ATTR_RO(matrix);
1474 static ssize_t guest_matrix_show(struct device *dev,
1475 struct device_attribute *attr, char *buf)
1478 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1480 mutex_lock(&matrix_dev->mdevs_lock);
1481 nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
1482 mutex_unlock(&matrix_dev->mdevs_lock);
1486 static DEVICE_ATTR_RO(guest_matrix);
1488 static struct attribute *vfio_ap_mdev_attrs[] = {
1489 &dev_attr_assign_adapter.attr,
1490 &dev_attr_unassign_adapter.attr,
1491 &dev_attr_assign_domain.attr,
1492 &dev_attr_unassign_domain.attr,
1493 &dev_attr_assign_control_domain.attr,
1494 &dev_attr_unassign_control_domain.attr,
1495 &dev_attr_control_domains.attr,
1496 &dev_attr_matrix.attr,
1497 &dev_attr_guest_matrix.attr,
1501 static struct attribute_group vfio_ap_mdev_attr_group = {
1502 .attrs = vfio_ap_mdev_attrs
1505 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1506 &vfio_ap_mdev_attr_group,
1511 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1512 * to manage AP resources for the guest whose state is represented by @kvm
1514 * @matrix_mdev: a mediated matrix device
1515 * @kvm: reference to KVM instance
1517 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1518 * otherwise, returns an -EPERM.
1520 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1523 struct ap_matrix_mdev *m;
1525 if (kvm->arch.crypto.crycbd) {
1526 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1527 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1528 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1530 get_update_locks_for_kvm(kvm);
1532 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1533 if (m != matrix_mdev && m->kvm == kvm) {
1534 release_update_locks_for_kvm(kvm);
1540 matrix_mdev->kvm = kvm;
1541 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1543 release_update_locks_for_kvm(kvm);
1549 static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
1552 struct ap_matrix_mdev *matrix_mdev =
1553 container_of(vdev, struct ap_matrix_mdev, vdev);
1555 vfio_unpin_pages(&matrix_mdev->vdev, iova, 1);
1559 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1562 * @matrix_mdev: a matrix mediated device
1564 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1566 struct kvm *kvm = matrix_mdev->kvm;
1568 if (kvm && kvm->arch.crypto.crycbd) {
1569 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1570 kvm->arch.crypto.pqap_hook = NULL;
1571 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1573 get_update_locks_for_kvm(kvm);
1575 kvm_arch_crypto_clear_masks(kvm);
1576 vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1578 matrix_mdev->kvm = NULL;
1580 release_update_locks_for_kvm(kvm);
1584 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1586 struct ap_queue *queue;
1587 struct vfio_ap_queue *q = NULL;
1589 queue = ap_get_qdev(apqn);
1593 if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
1594 q = dev_get_drvdata(&queue->ap_dev.device);
1596 put_device(&queue->ap_dev.device);
1601 static int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1604 struct ap_queue_status status;
1611 status = ap_zapq(q->apqn);
1612 q->reset_rc = status.response_code;
1613 switch (status.response_code) {
1614 case AP_RESPONSE_NORMAL:
1617 case AP_RESPONSE_RESET_IN_PROGRESS:
1624 case AP_RESPONSE_Q_NOT_AVAIL:
1625 case AP_RESPONSE_DECONFIGURED:
1626 case AP_RESPONSE_CHECKSTOPPED:
1627 WARN_ONCE(status.irq_enabled,
1628 "PQAP/ZAPQ for %02x.%04x failed with rc=%u while IRQ enabled",
1629 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1630 status.response_code);
1632 goto free_resources;
1634 /* things are really broken, give up */
1636 "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
1637 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
1638 status.response_code);
1642 /* wait for the reset to take effect */
1644 if (status.queue_empty && !status.irq_enabled)
1647 status = ap_tapq(q->apqn, NULL);
1649 WARN_ONCE(retry2 <= 0, "unable to verify reset of queue %02x.%04x",
1650 AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn));
1653 vfio_ap_free_aqic_resources(q);
1658 static int vfio_ap_mdev_reset_queues(struct ap_queue_table *qtable)
1660 int ret, loop_cursor, rc = 0;
1661 struct vfio_ap_queue *q;
1663 hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
1664 ret = vfio_ap_mdev_reset_queue(q, 1);
1666 * Regardless whether a queue turns out to be busy, or
1667 * is not operational, we need to continue resetting
1668 * the remaining queues.
1677 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1679 struct ap_matrix_mdev *matrix_mdev =
1680 container_of(vdev, struct ap_matrix_mdev, vdev);
1685 return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
1688 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1690 struct ap_matrix_mdev *matrix_mdev =
1691 container_of(vdev, struct ap_matrix_mdev, vdev);
1693 vfio_ap_mdev_unset_kvm(matrix_mdev);
1696 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1698 unsigned long minsz;
1699 struct vfio_device_info info;
1701 minsz = offsetofend(struct vfio_device_info, num_irqs);
1703 if (copy_from_user(&info, (void __user *)arg, minsz))
1706 if (info.argsz < minsz)
1709 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1710 info.num_regions = 0;
1713 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1716 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1717 unsigned int cmd, unsigned long arg)
1719 struct ap_matrix_mdev *matrix_mdev =
1720 container_of(vdev, struct ap_matrix_mdev, vdev);
1723 mutex_lock(&matrix_dev->mdevs_lock);
1725 case VFIO_DEVICE_GET_INFO:
1726 ret = vfio_ap_mdev_get_device_info(arg);
1728 case VFIO_DEVICE_RESET:
1729 ret = vfio_ap_mdev_reset_queues(&matrix_mdev->qtable);
1735 mutex_unlock(&matrix_dev->mdevs_lock);
1740 static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
1742 struct ap_matrix_mdev *matrix_mdev;
1743 unsigned long apid = AP_QID_CARD(q->apqn);
1744 unsigned long apqi = AP_QID_QUEUE(q->apqn);
1746 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
1747 if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
1748 test_bit_inv(apqi, matrix_mdev->matrix.aqm))
1755 static ssize_t status_show(struct device *dev,
1756 struct device_attribute *attr,
1760 struct vfio_ap_queue *q;
1761 struct ap_matrix_mdev *matrix_mdev;
1762 struct ap_device *apdev = to_ap_dev(dev);
1764 mutex_lock(&matrix_dev->mdevs_lock);
1765 q = dev_get_drvdata(&apdev->device);
1766 matrix_mdev = vfio_ap_mdev_for_queue(q);
1769 if (matrix_mdev->kvm)
1770 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1773 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1776 nchars = scnprintf(buf, PAGE_SIZE, "%s\n",
1777 AP_QUEUE_UNASSIGNED);
1780 mutex_unlock(&matrix_dev->mdevs_lock);
1785 static DEVICE_ATTR_RO(status);
1787 static struct attribute *vfio_queue_attrs[] = {
1788 &dev_attr_status.attr,
1792 static const struct attribute_group vfio_queue_attr_group = {
1793 .attrs = vfio_queue_attrs,
1796 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1797 .open_device = vfio_ap_mdev_open_device,
1798 .close_device = vfio_ap_mdev_close_device,
1799 .ioctl = vfio_ap_mdev_ioctl,
1800 .dma_unmap = vfio_ap_mdev_dma_unmap,
1803 static struct mdev_driver vfio_ap_matrix_driver = {
1805 .name = "vfio_ap_mdev",
1806 .owner = THIS_MODULE,
1807 .mod_name = KBUILD_MODNAME,
1808 .dev_groups = vfio_ap_mdev_attr_groups,
1810 .probe = vfio_ap_mdev_probe,
1811 .remove = vfio_ap_mdev_remove,
1812 .supported_type_groups = vfio_ap_mdev_type_groups,
1815 int vfio_ap_mdev_register(void)
1819 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1821 ret = mdev_register_driver(&vfio_ap_matrix_driver);
1825 ret = mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_driver);
1831 mdev_unregister_driver(&vfio_ap_matrix_driver);
1835 void vfio_ap_mdev_unregister(void)
1837 mdev_unregister_device(&matrix_dev->device);
1838 mdev_unregister_driver(&vfio_ap_matrix_driver);
1841 int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
1844 struct vfio_ap_queue *q;
1845 struct ap_matrix_mdev *matrix_mdev;
1847 ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
1851 q = kzalloc(sizeof(*q), GFP_KERNEL);
1855 q->apqn = to_ap_queue(&apdev->device)->qid;
1856 q->saved_isc = VFIO_AP_ISC_INVALID;
1857 matrix_mdev = get_update_locks_by_apqn(q->apqn);
1860 vfio_ap_mdev_link_queue(matrix_mdev, q);
1862 if (vfio_ap_mdev_filter_matrix(matrix_mdev->matrix.apm,
1863 matrix_mdev->matrix.aqm,
1865 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1867 dev_set_drvdata(&apdev->device, q);
1868 release_update_locks_for_mdev(matrix_mdev);
1873 void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
1875 unsigned long apid, apqi;
1876 struct vfio_ap_queue *q;
1877 struct ap_matrix_mdev *matrix_mdev;
1879 sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
1880 q = dev_get_drvdata(&apdev->device);
1881 get_update_locks_for_queue(q);
1882 matrix_mdev = q->matrix_mdev;
1885 vfio_ap_unlink_queue_fr_mdev(q);
1887 apid = AP_QID_CARD(q->apqn);
1888 apqi = AP_QID_QUEUE(q->apqn);
1891 * If the queue is assigned to the guest's APCB, then remove
1892 * the adapter's APID from the APCB and hot it into the guest.
1894 if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
1895 test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
1896 clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
1897 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1901 vfio_ap_mdev_reset_queue(q, 1);
1902 dev_set_drvdata(&apdev->device, NULL);
1904 release_update_locks_for_mdev(matrix_mdev);
1908 * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
1909 * assigned to a mediated device under the control
1910 * of the vfio_ap device driver.
1912 * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
1913 * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
1916 * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
1917 * assigned to a mediated device under the control of the vfio_ap
1919 * * Otherwise, return 0.
1921 int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
1925 mutex_lock(&matrix_dev->guests_lock);
1926 mutex_lock(&matrix_dev->mdevs_lock);
1927 ret = vfio_ap_mdev_verify_no_sharing(apm, aqm);
1928 mutex_unlock(&matrix_dev->mdevs_lock);
1929 mutex_unlock(&matrix_dev->guests_lock);
1935 * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
1936 * domains that have been removed from the host's
1937 * AP configuration from a guest.
1939 * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
1940 * @aprem: the adapters that have been removed from the host's AP configuration
1941 * @aqrem: the domains that have been removed from the host's AP configuration
1942 * @cdrem: the control domains that have been removed from the host's AP
1945 static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
1946 unsigned long *aprem,
1947 unsigned long *aqrem,
1948 unsigned long *cdrem)
1952 if (!bitmap_empty(aprem, AP_DEVICES)) {
1953 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
1954 matrix_mdev->shadow_apcb.apm,
1958 if (!bitmap_empty(aqrem, AP_DOMAINS)) {
1959 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
1960 matrix_mdev->shadow_apcb.aqm,
1964 if (!bitmap_empty(cdrem, AP_DOMAINS))
1965 do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
1966 matrix_mdev->shadow_apcb.adm,
1970 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
1974 * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
1975 * domains and control domains that have been removed
1976 * from the host AP configuration and unplugs them
1977 * from those guests.
1979 * @ap_remove: bitmap specifying which adapters have been removed from the host
1981 * @aq_remove: bitmap specifying which domains have been removed from the host
1983 * @cd_remove: bitmap specifying which control domains have been removed from
1986 static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
1987 unsigned long *aq_remove,
1988 unsigned long *cd_remove)
1990 struct ap_matrix_mdev *matrix_mdev;
1991 DECLARE_BITMAP(aprem, AP_DEVICES);
1992 DECLARE_BITMAP(aqrem, AP_DOMAINS);
1993 DECLARE_BITMAP(cdrem, AP_DOMAINS);
1996 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
1997 mutex_lock(&matrix_mdev->kvm->lock);
1998 mutex_lock(&matrix_dev->mdevs_lock);
2000 do_remove |= bitmap_and(aprem, ap_remove,
2001 matrix_mdev->matrix.apm,
2003 do_remove |= bitmap_and(aqrem, aq_remove,
2004 matrix_mdev->matrix.aqm,
2006 do_remove |= bitmap_andnot(cdrem, cd_remove,
2007 matrix_mdev->matrix.adm,
2011 vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
2014 mutex_unlock(&matrix_dev->mdevs_lock);
2015 mutex_unlock(&matrix_mdev->kvm->lock);
2020 * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
2021 * control domains from the host AP configuration
2022 * by unplugging them from the guests that are
2024 * @cur_config_info: the current host AP configuration information
2025 * @prev_config_info: the previous host AP configuration information
2027 static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
2028 struct ap_config_info *prev_config_info)
2031 DECLARE_BITMAP(aprem, AP_DEVICES);
2032 DECLARE_BITMAP(aqrem, AP_DOMAINS);
2033 DECLARE_BITMAP(cdrem, AP_DOMAINS);
2035 do_remove = bitmap_andnot(aprem,
2036 (unsigned long *)prev_config_info->apm,
2037 (unsigned long *)cur_config_info->apm,
2039 do_remove |= bitmap_andnot(aqrem,
2040 (unsigned long *)prev_config_info->aqm,
2041 (unsigned long *)cur_config_info->aqm,
2043 do_remove |= bitmap_andnot(cdrem,
2044 (unsigned long *)prev_config_info->adm,
2045 (unsigned long *)cur_config_info->adm,
2049 vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
2053 * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
2054 * are older than AP type 10 (CEX4).
2055 * @apm: a bitmap of the APIDs to examine
2056 * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
2058 static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
2061 struct ap_queue_status status;
2062 unsigned long apid, apqi, info;
2063 int qtype, qtype_mask = 0xff000000;
2065 for_each_set_bit_inv(apid, apm, AP_DEVICES) {
2066 apid_cleared = false;
2068 for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
2069 status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
2070 switch (status.response_code) {
2072 * According to the architecture in each case
2073 * below, the queue's info should be filled.
2075 case AP_RESPONSE_NORMAL:
2076 case AP_RESPONSE_RESET_IN_PROGRESS:
2077 case AP_RESPONSE_DECONFIGURED:
2078 case AP_RESPONSE_CHECKSTOPPED:
2079 case AP_RESPONSE_BUSY:
2080 qtype = info & qtype_mask;
2083 * The vfio_ap device driver only
2084 * supports CEX4 and newer adapters, so
2085 * remove the APID if the adapter is
2086 * older than a CEX4.
2088 if (qtype < AP_DEVICE_TYPE_CEX4) {
2089 clear_bit_inv(apid, apm);
2090 apid_cleared = true;
2097 * If we don't know the adapter type,
2098 * clear its APID since it can't be
2099 * determined whether the vfio_ap
2100 * device driver supports it.
2102 clear_bit_inv(apid, apm);
2103 apid_cleared = true;
2108 * If we've already cleared the APID from the apm, there
2109 * is no need to continue examining the remainin AP
2110 * queues to determine the type of the adapter.
2119 * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
2120 * control domains that have been added to the host's
2121 * AP configuration for each matrix mdev to which they
2124 * @apm_add: a bitmap specifying the adapters that have been added to the AP
2126 * @aqm_add: a bitmap specifying the domains that have been added to the AP
2128 * @adm_add: a bitmap specifying the control domains that have been added to the
2131 static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
2132 unsigned long *adm_add)
2134 struct ap_matrix_mdev *matrix_mdev;
2136 if (list_empty(&matrix_dev->mdev_list))
2139 vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
2141 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2142 bitmap_and(matrix_mdev->apm_add,
2143 matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
2144 bitmap_and(matrix_mdev->aqm_add,
2145 matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
2146 bitmap_and(matrix_mdev->adm_add,
2147 matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
2152 * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
2153 * control domains to the host AP configuration
2154 * by updating the bitmaps that specify what adapters,
2155 * domains and control domains have been added so they
2156 * can be hot plugged into the guest when the AP bus
2157 * scan completes (see vfio_ap_on_scan_complete
2159 * @cur_config_info: the current AP configuration information
2160 * @prev_config_info: the previous AP configuration information
2162 static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
2163 struct ap_config_info *prev_config_info)
2166 DECLARE_BITMAP(apm_add, AP_DEVICES);
2167 DECLARE_BITMAP(aqm_add, AP_DOMAINS);
2168 DECLARE_BITMAP(adm_add, AP_DOMAINS);
2170 do_add = bitmap_andnot(apm_add,
2171 (unsigned long *)cur_config_info->apm,
2172 (unsigned long *)prev_config_info->apm,
2174 do_add |= bitmap_andnot(aqm_add,
2175 (unsigned long *)cur_config_info->aqm,
2176 (unsigned long *)prev_config_info->aqm,
2178 do_add |= bitmap_andnot(adm_add,
2179 (unsigned long *)cur_config_info->adm,
2180 (unsigned long *)prev_config_info->adm,
2184 vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
2188 * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
2191 * @cur_cfg_info: the current host AP configuration
2192 * @prev_cfg_info: the previous host AP configuration
2194 void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
2195 struct ap_config_info *prev_cfg_info)
2197 if (!cur_cfg_info || !prev_cfg_info)
2200 mutex_lock(&matrix_dev->guests_lock);
2202 vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
2203 vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
2204 memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
2206 mutex_unlock(&matrix_dev->guests_lock);
2209 static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
2211 bool do_hotplug = false;
2212 int filter_domains = 0;
2213 int filter_adapters = 0;
2214 DECLARE_BITMAP(apm, AP_DEVICES);
2215 DECLARE_BITMAP(aqm, AP_DOMAINS);
2217 mutex_lock(&matrix_mdev->kvm->lock);
2218 mutex_lock(&matrix_dev->mdevs_lock);
2220 filter_adapters = bitmap_and(apm, matrix_mdev->matrix.apm,
2221 matrix_mdev->apm_add, AP_DEVICES);
2222 filter_domains = bitmap_and(aqm, matrix_mdev->matrix.aqm,
2223 matrix_mdev->aqm_add, AP_DOMAINS);
2225 if (filter_adapters && filter_domains)
2226 do_hotplug |= vfio_ap_mdev_filter_matrix(apm, aqm, matrix_mdev);
2227 else if (filter_adapters)
2229 vfio_ap_mdev_filter_matrix(apm,
2230 matrix_mdev->shadow_apcb.aqm,
2234 vfio_ap_mdev_filter_matrix(matrix_mdev->shadow_apcb.apm,
2237 if (bitmap_intersects(matrix_mdev->matrix.adm, matrix_mdev->adm_add,
2239 do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
2242 vfio_ap_mdev_update_guest_apcb(matrix_mdev);
2244 mutex_unlock(&matrix_dev->mdevs_lock);
2245 mutex_unlock(&matrix_mdev->kvm->lock);
2248 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
2249 struct ap_config_info *old_config_info)
2251 struct ap_matrix_mdev *matrix_mdev;
2253 mutex_lock(&matrix_dev->guests_lock);
2255 list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
2256 if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
2257 bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
2258 bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
2261 vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
2262 bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
2263 bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
2264 bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
2267 mutex_unlock(&matrix_dev->guests_lock);