1 // SPDX-License-Identifier: GPL-2.0+
3 * Adjunct processor matrix VFIO device driver callbacks.
5 * Copyright IBM Corp. 2018
7 * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
8 * Halil Pasic <pasic@linux.ibm.com>
9 * Pierre Morel <pmorel@linux.ibm.com>
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>
20 #include <asm/zcrypt.h>
22 #include "vfio_ap_private.h"
24 #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
25 #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
27 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
28 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
29 static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
31 static int match_apqn(struct device *dev, const void *data)
33 struct vfio_ap_queue *q = dev_get_drvdata(dev);
35 return (q->apqn == *(int *)(data)) ? 1 : 0;
39 * vfio_ap_get_queue - retrieve a queue with a specific APQN from a list
40 * @matrix_mdev: the associated mediated matrix
41 * @apqn: The queue APQN
43 * Retrieve a queue with a specific APQN from the list of the
44 * devices of the vfio_ap_drv.
45 * Verify that the APID and the APQI are set in the matrix.
47 * Return: the pointer to the associated vfio_ap_queue
49 static struct vfio_ap_queue *vfio_ap_get_queue(
50 struct ap_matrix_mdev *matrix_mdev,
53 struct vfio_ap_queue *q;
55 if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
57 if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
60 q = vfio_ap_find_queue(apqn);
62 q->matrix_mdev = matrix_mdev;
68 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
69 * @apqn: The AP Queue number
71 * Checks the IRQ bit for the status of this APQN using ap_tapq.
72 * Returns if the ap_tapq function succeeded and the bit is clear.
73 * Returns if ap_tapq function failed with invalid, deconfigured or
75 * Otherwise retries up to 5 times after waiting 20ms.
77 static void vfio_ap_wait_for_irqclear(int apqn)
79 struct ap_queue_status status;
83 status = ap_tapq(apqn, NULL);
84 switch (status.response_code) {
85 case AP_RESPONSE_NORMAL:
86 case AP_RESPONSE_RESET_IN_PROGRESS:
87 if (!status.irq_enabled)
90 case AP_RESPONSE_BUSY:
93 case AP_RESPONSE_Q_NOT_AVAIL:
94 case AP_RESPONSE_DECONFIGURED:
95 case AP_RESPONSE_CHECKSTOPPED:
97 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
98 status.response_code, apqn);
103 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
104 __func__, status.response_code, apqn);
108 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
109 * @q: The vfio_ap_queue
111 * Unregisters the ISC in the GIB when the saved ISC not invalid.
112 * Unpins the guest's page holding the NIB when it exists.
113 * Resets the saved_pfn and saved_isc to invalid values.
115 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
119 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
120 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
121 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
122 q->saved_isc = VFIO_AP_ISC_INVALID;
124 if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) {
125 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev),
132 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
133 * @q: The vfio_ap_queue
135 * Uses ap_aqic to disable the interruption and in case of success, reset
136 * in progress or IRQ disable command already proceeded: calls
137 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
138 * and calls vfio_ap_free_aqic_resources() to free the resources associated
139 * with the AP interrupt handling.
141 * In the case the AP is busy, or a reset is in progress,
142 * retries after 20ms, up to 5 times.
144 * Returns if ap_aqic function failed with invalid, deconfigured or
147 * Return: &struct ap_queue_status
149 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
151 struct ap_qirq_ctrl aqic_gisa = {};
152 struct ap_queue_status status;
156 status = ap_aqic(q->apqn, aqic_gisa, NULL);
157 switch (status.response_code) {
158 case AP_RESPONSE_OTHERWISE_CHANGED:
159 case AP_RESPONSE_NORMAL:
160 vfio_ap_wait_for_irqclear(q->apqn);
162 case AP_RESPONSE_RESET_IN_PROGRESS:
163 case AP_RESPONSE_BUSY:
166 case AP_RESPONSE_Q_NOT_AVAIL:
167 case AP_RESPONSE_DECONFIGURED:
168 case AP_RESPONSE_CHECKSTOPPED:
169 case AP_RESPONSE_INVALID_ADDRESS:
171 /* All cases in default means AP not operational */
172 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
173 status.response_code);
178 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
179 status.response_code);
181 vfio_ap_free_aqic_resources(q);
182 q->matrix_mdev = NULL;
187 * vfio_ap_irq_enable - Enable Interruption for a APQN
189 * @q: the vfio_ap_queue holding AQIC parameters
191 * Pin the NIB saved in *q
192 * Register the guest ISC to GIB interface and retrieve the
193 * host ISC to issue the host side PQAP/AQIC
195 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
196 * vfio_pin_pages failed.
198 * Otherwise return the ap_queue_status returned by the ap_aqic(),
199 * all retry handling will be done by the guest.
201 * Return: &struct ap_queue_status
203 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
207 struct ap_qirq_ctrl aqic_gisa = {};
208 struct ap_queue_status status = {};
209 struct kvm_s390_gisa *gisa;
211 unsigned long h_nib, g_pfn, h_pfn;
214 g_pfn = nib >> PAGE_SHIFT;
215 ret = vfio_pin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1,
216 IOMMU_READ | IOMMU_WRITE, &h_pfn);
221 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
225 kvm = q->matrix_mdev->kvm;
226 gisa = kvm->arch.gisa_int.origin;
228 h_nib = (h_pfn << PAGE_SHIFT) | (nib & ~PAGE_MASK);
229 aqic_gisa.gisc = isc;
230 aqic_gisa.isc = kvm_s390_gisc_register(kvm, isc);
232 aqic_gisa.gisa = (uint64_t)gisa >> 4;
234 status = ap_aqic(q->apqn, aqic_gisa, (void *)h_nib);
235 switch (status.response_code) {
236 case AP_RESPONSE_NORMAL:
237 /* See if we did clear older IRQ configuration */
238 vfio_ap_free_aqic_resources(q);
239 q->saved_pfn = g_pfn;
242 case AP_RESPONSE_OTHERWISE_CHANGED:
243 /* We could not modify IRQ setings: clear new configuration */
244 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1);
245 kvm_s390_gisc_unregister(kvm, isc);
248 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
249 status.response_code);
250 vfio_ap_irq_disable(q);
258 * handle_pqap - PQAP instruction callback
260 * @vcpu: The vcpu on which we received the PQAP instruction
262 * Get the general register contents to initialize internal variables.
267 * Response.status may be set to following Response Code:
268 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
269 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
270 * - AP_RESPONSE_NORMAL (0) : in case of successs
271 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
272 * We take the matrix_dev lock to ensure serialization on queues and
273 * mediated device access.
275 * Return: 0 if we could handle the request inside KVM.
276 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
278 static int handle_pqap(struct kvm_vcpu *vcpu)
282 struct vfio_ap_queue *q;
283 struct ap_queue_status qstatus = {
284 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
285 struct ap_matrix_mdev *matrix_mdev;
287 /* If we do not use the AIV facility just go to userland */
288 if (!(vcpu->arch.sie_block->eca & ECA_AIV))
291 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
292 mutex_lock(&matrix_dev->lock);
294 if (!vcpu->kvm->arch.crypto.pqap_hook)
296 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
297 struct ap_matrix_mdev, pqap_hook);
299 /* If the there is no guest using the mdev, there is nothing to do */
300 if (!matrix_mdev->kvm)
303 q = vfio_ap_get_queue(matrix_mdev, apqn);
307 status = vcpu->run->s.regs.gprs[1];
309 /* If IR bit(16) is set we enable the interrupt */
310 if ((status >> (63 - 16)) & 0x01)
311 qstatus = vfio_ap_irq_enable(q, status & 0x07,
312 vcpu->run->s.regs.gprs[2]);
314 qstatus = vfio_ap_irq_disable(q);
317 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
318 vcpu->run->s.regs.gprs[1] >>= 32;
319 mutex_unlock(&matrix_dev->lock);
323 static void vfio_ap_matrix_init(struct ap_config_info *info,
324 struct ap_matrix *matrix)
326 matrix->apm_max = info->apxa ? info->Na : 63;
327 matrix->aqm_max = info->apxa ? info->Nd : 15;
328 matrix->adm_max = info->apxa ? info->Nd : 15;
331 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
333 struct ap_matrix_mdev *matrix_mdev;
336 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
339 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
342 goto err_dec_available;
344 vfio_init_group_dev(&matrix_mdev->vdev, &mdev->dev,
345 &vfio_ap_matrix_dev_ops);
347 matrix_mdev->mdev = mdev;
348 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
349 matrix_mdev->pqap_hook = handle_pqap;
350 mutex_lock(&matrix_dev->lock);
351 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
352 mutex_unlock(&matrix_dev->lock);
354 ret = vfio_register_group_dev(&matrix_mdev->vdev);
357 dev_set_drvdata(&mdev->dev, matrix_mdev);
361 mutex_lock(&matrix_dev->lock);
362 list_del(&matrix_mdev->node);
363 mutex_unlock(&matrix_dev->lock);
366 atomic_inc(&matrix_dev->available_instances);
370 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
372 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
374 vfio_unregister_group_dev(&matrix_mdev->vdev);
376 mutex_lock(&matrix_dev->lock);
377 vfio_ap_mdev_reset_queues(matrix_mdev);
378 list_del(&matrix_mdev->node);
380 atomic_inc(&matrix_dev->available_instances);
381 mutex_unlock(&matrix_dev->lock);
384 static ssize_t name_show(struct mdev_type *mtype,
385 struct mdev_type_attribute *attr, char *buf)
387 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
390 static MDEV_TYPE_ATTR_RO(name);
392 static ssize_t available_instances_show(struct mdev_type *mtype,
393 struct mdev_type_attribute *attr,
396 return sprintf(buf, "%d\n",
397 atomic_read(&matrix_dev->available_instances));
400 static MDEV_TYPE_ATTR_RO(available_instances);
402 static ssize_t device_api_show(struct mdev_type *mtype,
403 struct mdev_type_attribute *attr, char *buf)
405 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
408 static MDEV_TYPE_ATTR_RO(device_api);
410 static struct attribute *vfio_ap_mdev_type_attrs[] = {
411 &mdev_type_attr_name.attr,
412 &mdev_type_attr_device_api.attr,
413 &mdev_type_attr_available_instances.attr,
417 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
418 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
419 .attrs = vfio_ap_mdev_type_attrs,
422 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
423 &vfio_ap_mdev_hwvirt_type_group,
427 struct vfio_ap_queue_reserved {
434 * vfio_ap_has_queue - determines if the AP queue containing the target in @data
436 * @dev: an AP queue device
437 * @data: a struct vfio_ap_queue_reserved reference
439 * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
440 * apid or apqi specified in @data:
442 * - If @data contains both an apid and apqi value, then @data will be flagged
443 * as reserved if the APID and APQI fields for the AP queue device matches
445 * - If @data contains only an apid value, @data will be flagged as
446 * reserved if the APID field in the AP queue device matches
448 * - If @data contains only an apqi value, @data will be flagged as
449 * reserved if the APQI field in the AP queue device matches
451 * Return: 0 to indicate the input to function succeeded. Returns -EINVAL if
452 * @data does not contain either an apid or apqi.
454 static int vfio_ap_has_queue(struct device *dev, void *data)
456 struct vfio_ap_queue_reserved *qres = data;
457 struct ap_queue *ap_queue = to_ap_queue(dev);
461 if (qres->apid && qres->apqi) {
462 qid = AP_MKQID(*qres->apid, *qres->apqi);
463 if (qid == ap_queue->qid)
464 qres->reserved = true;
465 } else if (qres->apid && !qres->apqi) {
466 id = AP_QID_CARD(ap_queue->qid);
467 if (id == *qres->apid)
468 qres->reserved = true;
469 } else if (!qres->apid && qres->apqi) {
470 id = AP_QID_QUEUE(ap_queue->qid);
471 if (id == *qres->apqi)
472 qres->reserved = true;
481 * vfio_ap_verify_queue_reserved - verifies that the AP queue containing
482 * @apid or @aqpi is reserved
484 * @apid: an AP adapter ID
485 * @apqi: an AP queue index
487 * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
488 * driver according to the following rules:
490 * - If both @apid and @apqi are not NULL, then there must be an AP queue
491 * device bound to the vfio_ap driver with the APQN identified by @apid and
494 * - If only @apid is not NULL, then there must be an AP queue device bound
495 * to the vfio_ap driver with an APQN containing @apid
497 * - If only @apqi is not NULL, then there must be an AP queue device bound
498 * to the vfio_ap driver with an APQN containing @apqi
500 * Return: 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
502 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
506 struct vfio_ap_queue_reserved qres;
510 qres.reserved = false;
512 ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
513 &qres, vfio_ap_has_queue);
520 return -EADDRNOTAVAIL;
524 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
529 unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
531 if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
532 return vfio_ap_verify_queue_reserved(&apid, NULL);
534 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
535 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
544 * vfio_ap_mdev_verify_no_sharing - verifies that the AP matrix is not configured
546 * @matrix_mdev: the mediated matrix device
548 * Verifies that the APQNs derived from the cross product of the AP adapter IDs
549 * and AP queue indexes comprising the AP matrix are not configured for another
550 * mediated device. AP queue sharing is not allowed.
552 * Return: 0 if the APQNs are not shared; otherwise returns -EADDRINUSE.
554 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
556 struct ap_matrix_mdev *lstdev;
557 DECLARE_BITMAP(apm, AP_DEVICES);
558 DECLARE_BITMAP(aqm, AP_DOMAINS);
560 list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
561 if (matrix_mdev == lstdev)
564 memset(apm, 0, sizeof(apm));
565 memset(aqm, 0, sizeof(aqm));
568 * We work on full longs, as we can only exclude the leftover
569 * bits in non-inverse order. The leftover is all zeros.
571 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
572 lstdev->matrix.apm, AP_DEVICES))
575 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
576 lstdev->matrix.aqm, AP_DOMAINS))
586 * assign_adapter_store - parses the APID from @buf and sets the
587 * corresponding bit in the mediated matrix device's APM
589 * @dev: the matrix device
590 * @attr: the mediated matrix device's assign_adapter attribute
591 * @buf: a buffer containing the AP adapter number (APID) to
593 * @count: the number of bytes in @buf
595 * Return: the number of bytes processed if the APID is valid; otherwise,
596 * returns one of the following errors:
599 * The APID is not a valid number
602 * The APID exceeds the maximum value configured for the system
605 * An APQN derived from the cross product of the APID being assigned
606 * and the APQIs previously assigned is not bound to the vfio_ap device
607 * driver; or, if no APQIs have yet been assigned, the APID is not
608 * contained in an APQN bound to the vfio_ap device driver.
611 * An APQN derived from the cross product of the APID being assigned
612 * and the APQIs previously assigned is being used by another mediated
615 static ssize_t assign_adapter_store(struct device *dev,
616 struct device_attribute *attr,
617 const char *buf, size_t count)
621 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
623 mutex_lock(&matrix_dev->lock);
625 /* If the KVM guest is running, disallow assignment of adapter */
626 if (matrix_mdev->kvm) {
631 ret = kstrtoul(buf, 0, &apid);
635 if (apid > matrix_mdev->matrix.apm_max) {
641 * Set the bit in the AP mask (APM) corresponding to the AP adapter
642 * number (APID). The bits in the mask, from most significant to least
643 * significant bit, correspond to APIDs 0-255.
645 ret = vfio_ap_mdev_verify_queues_reserved_for_apid(matrix_mdev, apid);
649 set_bit_inv(apid, matrix_mdev->matrix.apm);
651 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
659 clear_bit_inv(apid, matrix_mdev->matrix.apm);
661 mutex_unlock(&matrix_dev->lock);
665 static DEVICE_ATTR_WO(assign_adapter);
668 * unassign_adapter_store - parses the APID from @buf and clears the
669 * corresponding bit in the mediated matrix device's APM
671 * @dev: the matrix device
672 * @attr: the mediated matrix device's unassign_adapter attribute
673 * @buf: a buffer containing the adapter number (APID) to be unassigned
674 * @count: the number of bytes in @buf
676 * Return: the number of bytes processed if the APID is valid; otherwise,
677 * returns one of the following errors:
678 * -EINVAL if the APID is not a number
679 * -ENODEV if the APID it exceeds the maximum value configured for the
682 static ssize_t unassign_adapter_store(struct device *dev,
683 struct device_attribute *attr,
684 const char *buf, size_t count)
688 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
690 mutex_lock(&matrix_dev->lock);
692 /* If the KVM guest is running, disallow unassignment of adapter */
693 if (matrix_mdev->kvm) {
698 ret = kstrtoul(buf, 0, &apid);
702 if (apid > matrix_mdev->matrix.apm_max) {
707 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
710 mutex_unlock(&matrix_dev->lock);
713 static DEVICE_ATTR_WO(unassign_adapter);
716 vfio_ap_mdev_verify_queues_reserved_for_apqi(struct ap_matrix_mdev *matrix_mdev,
721 unsigned long nbits = matrix_mdev->matrix.apm_max + 1;
723 if (find_first_bit_inv(matrix_mdev->matrix.apm, nbits) >= nbits)
724 return vfio_ap_verify_queue_reserved(NULL, &apqi);
726 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, nbits) {
727 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
736 * assign_domain_store - parses the APQI from @buf and sets the
737 * corresponding bit in the mediated matrix device's AQM
740 * @dev: the matrix device
741 * @attr: the mediated matrix device's assign_domain attribute
742 * @buf: a buffer containing the AP queue index (APQI) of the domain to
744 * @count: the number of bytes in @buf
746 * Return: the number of bytes processed if the APQI is valid; otherwise returns
747 * one of the following errors:
750 * The APQI is not a valid number
753 * The APQI exceeds the maximum value configured for the system
756 * An APQN derived from the cross product of the APQI being assigned
757 * and the APIDs previously assigned is not bound to the vfio_ap device
758 * driver; or, if no APIDs have yet been assigned, the APQI is not
759 * contained in an APQN bound to the vfio_ap device driver.
762 * An APQN derived from the cross product of the APQI being assigned
763 * and the APIDs previously assigned is being used by another mediated
766 static ssize_t assign_domain_store(struct device *dev,
767 struct device_attribute *attr,
768 const char *buf, size_t count)
772 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
773 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
775 mutex_lock(&matrix_dev->lock);
777 /* If the KVM guest is running, disallow assignment of domain */
778 if (matrix_mdev->kvm) {
783 ret = kstrtoul(buf, 0, &apqi);
786 if (apqi > max_apqi) {
791 ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
795 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
797 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
805 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
807 mutex_unlock(&matrix_dev->lock);
811 static DEVICE_ATTR_WO(assign_domain);
815 * unassign_domain_store - parses the APQI from @buf and clears the
816 * corresponding bit in the mediated matrix device's AQM
818 * @dev: the matrix device
819 * @attr: the mediated matrix device's unassign_domain attribute
820 * @buf: a buffer containing the AP queue index (APQI) of the domain to
822 * @count: the number of bytes in @buf
824 * Return: the number of bytes processed if the APQI is valid; otherwise,
825 * returns one of the following errors:
826 * -EINVAL if the APQI is not a number
827 * -ENODEV if the APQI exceeds the maximum value configured for the system
829 static ssize_t unassign_domain_store(struct device *dev,
830 struct device_attribute *attr,
831 const char *buf, size_t count)
835 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
837 mutex_lock(&matrix_dev->lock);
839 /* If the KVM guest is running, disallow unassignment of domain */
840 if (matrix_mdev->kvm) {
845 ret = kstrtoul(buf, 0, &apqi);
849 if (apqi > matrix_mdev->matrix.aqm_max) {
854 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
858 mutex_unlock(&matrix_dev->lock);
861 static DEVICE_ATTR_WO(unassign_domain);
864 * assign_control_domain_store - parses the domain ID from @buf and sets
865 * the corresponding bit in the mediated matrix device's ADM
868 * @dev: the matrix device
869 * @attr: the mediated matrix device's assign_control_domain attribute
870 * @buf: a buffer containing the domain ID to be assigned
871 * @count: the number of bytes in @buf
873 * Return: the number of bytes processed if the domain ID is valid; otherwise,
874 * returns one of the following errors:
875 * -EINVAL if the ID is not a number
876 * -ENODEV if the ID exceeds the maximum value configured for the system
878 static ssize_t assign_control_domain_store(struct device *dev,
879 struct device_attribute *attr,
880 const char *buf, size_t count)
884 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
886 mutex_lock(&matrix_dev->lock);
888 /* If the KVM guest is running, disallow assignment of control domain */
889 if (matrix_mdev->kvm) {
894 ret = kstrtoul(buf, 0, &id);
898 if (id > matrix_mdev->matrix.adm_max) {
903 /* Set the bit in the ADM (bitmask) corresponding to the AP control
904 * domain number (id). The bits in the mask, from most significant to
905 * least significant, correspond to IDs 0 up to the one less than the
906 * number of control domains that can be assigned.
908 set_bit_inv(id, matrix_mdev->matrix.adm);
911 mutex_unlock(&matrix_dev->lock);
914 static DEVICE_ATTR_WO(assign_control_domain);
917 * unassign_control_domain_store - parses the domain ID from @buf and
918 * clears the corresponding bit in the mediated matrix device's ADM
920 * @dev: the matrix device
921 * @attr: the mediated matrix device's unassign_control_domain attribute
922 * @buf: a buffer containing the domain ID to be unassigned
923 * @count: the number of bytes in @buf
925 * Return: the number of bytes processed if the domain ID is valid; otherwise,
926 * returns one of the following errors:
927 * -EINVAL if the ID is not a number
928 * -ENODEV if the ID exceeds the maximum value configured for the system
930 static ssize_t unassign_control_domain_store(struct device *dev,
931 struct device_attribute *attr,
932 const char *buf, size_t count)
936 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
937 unsigned long max_domid = matrix_mdev->matrix.adm_max;
939 mutex_lock(&matrix_dev->lock);
941 /* If a KVM guest is running, disallow unassignment of control domain */
942 if (matrix_mdev->kvm) {
947 ret = kstrtoul(buf, 0, &domid);
950 if (domid > max_domid) {
955 clear_bit_inv(domid, matrix_mdev->matrix.adm);
958 mutex_unlock(&matrix_dev->lock);
961 static DEVICE_ATTR_WO(unassign_control_domain);
963 static ssize_t control_domains_show(struct device *dev,
964 struct device_attribute *dev_attr,
971 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
972 unsigned long max_domid = matrix_mdev->matrix.adm_max;
974 mutex_lock(&matrix_dev->lock);
975 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
976 n = sprintf(bufpos, "%04lx\n", id);
980 mutex_unlock(&matrix_dev->lock);
984 static DEVICE_ATTR_RO(control_domains);
986 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
989 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
995 unsigned long napm_bits = matrix_mdev->matrix.apm_max + 1;
996 unsigned long naqm_bits = matrix_mdev->matrix.aqm_max + 1;
1000 apid1 = find_first_bit_inv(matrix_mdev->matrix.apm, napm_bits);
1001 apqi1 = find_first_bit_inv(matrix_mdev->matrix.aqm, naqm_bits);
1003 mutex_lock(&matrix_dev->lock);
1005 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1006 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1007 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1009 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1015 } else if (apid1 < napm_bits) {
1016 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1017 n = sprintf(bufpos, "%02lx.\n", apid);
1021 } else if (apqi1 < naqm_bits) {
1022 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, naqm_bits) {
1023 n = sprintf(bufpos, ".%04lx\n", apqi);
1029 mutex_unlock(&matrix_dev->lock);
1033 static DEVICE_ATTR_RO(matrix);
1035 static struct attribute *vfio_ap_mdev_attrs[] = {
1036 &dev_attr_assign_adapter.attr,
1037 &dev_attr_unassign_adapter.attr,
1038 &dev_attr_assign_domain.attr,
1039 &dev_attr_unassign_domain.attr,
1040 &dev_attr_assign_control_domain.attr,
1041 &dev_attr_unassign_control_domain.attr,
1042 &dev_attr_control_domains.attr,
1043 &dev_attr_matrix.attr,
1047 static struct attribute_group vfio_ap_mdev_attr_group = {
1048 .attrs = vfio_ap_mdev_attrs
1051 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1052 &vfio_ap_mdev_attr_group,
1057 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1058 * to manage AP resources for the guest whose state is represented by @kvm
1060 * @matrix_mdev: a mediated matrix device
1061 * @kvm: reference to KVM instance
1063 * Note: The matrix_dev->lock must be taken prior to calling
1064 * this function; however, the lock will be temporarily released while the
1065 * guest's AP configuration is set to avoid a potential lockdep splat.
1066 * The kvm->lock is taken to set the guest's AP configuration which, under
1067 * certain circumstances, will result in a circular lock dependency if this is
1068 * done under the @matrix_mdev->lock.
1070 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1071 * otherwise, returns an -EPERM.
1073 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1076 struct ap_matrix_mdev *m;
1078 if (kvm->arch.crypto.crycbd) {
1079 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1080 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1081 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1083 mutex_lock(&kvm->lock);
1084 mutex_lock(&matrix_dev->lock);
1086 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1087 if (m != matrix_mdev && m->kvm == kvm) {
1088 mutex_unlock(&kvm->lock);
1089 mutex_unlock(&matrix_dev->lock);
1095 matrix_mdev->kvm = kvm;
1096 kvm_arch_crypto_set_masks(kvm,
1097 matrix_mdev->matrix.apm,
1098 matrix_mdev->matrix.aqm,
1099 matrix_mdev->matrix.adm);
1101 mutex_unlock(&kvm->lock);
1102 mutex_unlock(&matrix_dev->lock);
1109 * vfio_ap_mdev_iommu_notifier - IOMMU notifier callback
1111 * @nb: The notifier block
1112 * @action: Action to be taken
1113 * @data: data associated with the request
1115 * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
1116 * pinned before). Other requests are ignored.
1118 * Return: for an UNMAP request, NOFITY_OK; otherwise NOTIFY_DONE.
1120 static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
1121 unsigned long action, void *data)
1123 struct ap_matrix_mdev *matrix_mdev;
1125 matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
1127 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
1128 struct vfio_iommu_type1_dma_unmap *unmap = data;
1129 unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
1131 vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
1139 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1142 * @matrix_mdev: a matrix mediated device
1144 * Note: The matrix_dev->lock must be taken prior to calling
1145 * this function; however, the lock will be temporarily released while the
1146 * guest's AP configuration is cleared to avoid a potential lockdep splat.
1147 * The kvm->lock is taken to clear the guest's AP configuration which, under
1148 * certain circumstances, will result in a circular lock dependency if this is
1149 * done under the @matrix_mdev->lock.
1151 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev,
1154 if (kvm && kvm->arch.crypto.crycbd) {
1155 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1156 kvm->arch.crypto.pqap_hook = NULL;
1157 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1159 mutex_lock(&kvm->lock);
1160 mutex_lock(&matrix_dev->lock);
1162 kvm_arch_crypto_clear_masks(kvm);
1163 vfio_ap_mdev_reset_queues(matrix_mdev);
1165 matrix_mdev->kvm = NULL;
1167 mutex_unlock(&kvm->lock);
1168 mutex_unlock(&matrix_dev->lock);
1172 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1173 unsigned long action, void *data)
1175 int notify_rc = NOTIFY_OK;
1176 struct ap_matrix_mdev *matrix_mdev;
1178 if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1181 matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1184 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1185 else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1186 notify_rc = NOTIFY_DONE;
1191 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1194 struct vfio_ap_queue *q = NULL;
1196 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1199 q = dev_get_drvdata(dev);
1206 int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1209 struct ap_queue_status status;
1217 status = ap_zapq(q->apqn);
1218 switch (status.response_code) {
1219 case AP_RESPONSE_NORMAL:
1222 case AP_RESPONSE_RESET_IN_PROGRESS:
1229 case AP_RESPONSE_Q_NOT_AVAIL:
1230 case AP_RESPONSE_DECONFIGURED:
1231 case AP_RESPONSE_CHECKSTOPPED:
1232 WARN_ON_ONCE(status.irq_enabled);
1234 goto free_resources;
1236 /* things are really broken, give up */
1237 WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1238 status.response_code);
1242 /* wait for the reset to take effect */
1244 if (status.queue_empty && !status.irq_enabled)
1247 status = ap_tapq(q->apqn, NULL);
1249 WARN_ON_ONCE(retry2 <= 0);
1252 vfio_ap_free_aqic_resources(q);
1257 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1261 unsigned long apid, apqi;
1262 struct vfio_ap_queue *q;
1264 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1265 matrix_mdev->matrix.apm_max + 1) {
1266 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1267 matrix_mdev->matrix.aqm_max + 1) {
1268 q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1269 ret = vfio_ap_mdev_reset_queue(q, 1);
1271 * Regardless whether a queue turns out to be busy, or
1272 * is not operational, we need to continue resetting
1273 * the remaining queues.
1283 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1285 struct ap_matrix_mdev *matrix_mdev =
1286 container_of(vdev, struct ap_matrix_mdev, vdev);
1287 unsigned long events;
1290 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1291 events = VFIO_GROUP_NOTIFY_SET_KVM;
1293 ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1294 &events, &matrix_mdev->group_notifier);
1298 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1299 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1300 ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1301 &events, &matrix_mdev->iommu_notifier);
1303 goto out_unregister_group;
1306 out_unregister_group:
1307 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1308 &matrix_mdev->group_notifier);
1312 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1314 struct ap_matrix_mdev *matrix_mdev =
1315 container_of(vdev, struct ap_matrix_mdev, vdev);
1317 vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1318 &matrix_mdev->iommu_notifier);
1319 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1320 &matrix_mdev->group_notifier);
1321 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1324 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1326 unsigned long minsz;
1327 struct vfio_device_info info;
1329 minsz = offsetofend(struct vfio_device_info, num_irqs);
1331 if (copy_from_user(&info, (void __user *)arg, minsz))
1334 if (info.argsz < minsz)
1337 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1338 info.num_regions = 0;
1341 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1344 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1345 unsigned int cmd, unsigned long arg)
1347 struct ap_matrix_mdev *matrix_mdev =
1348 container_of(vdev, struct ap_matrix_mdev, vdev);
1351 mutex_lock(&matrix_dev->lock);
1353 case VFIO_DEVICE_GET_INFO:
1354 ret = vfio_ap_mdev_get_device_info(arg);
1356 case VFIO_DEVICE_RESET:
1357 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
1363 mutex_unlock(&matrix_dev->lock);
1368 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1369 .open_device = vfio_ap_mdev_open_device,
1370 .close_device = vfio_ap_mdev_close_device,
1371 .ioctl = vfio_ap_mdev_ioctl,
1374 static struct mdev_driver vfio_ap_matrix_driver = {
1376 .name = "vfio_ap_mdev",
1377 .owner = THIS_MODULE,
1378 .mod_name = KBUILD_MODNAME,
1379 .dev_groups = vfio_ap_mdev_attr_groups,
1381 .probe = vfio_ap_mdev_probe,
1382 .remove = vfio_ap_mdev_remove,
1385 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1386 .owner = THIS_MODULE,
1387 .device_driver = &vfio_ap_matrix_driver,
1388 .supported_type_groups = vfio_ap_mdev_type_groups,
1391 int vfio_ap_mdev_register(void)
1395 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1397 ret = mdev_register_driver(&vfio_ap_matrix_driver);
1401 ret = mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1407 mdev_unregister_driver(&vfio_ap_matrix_driver);
1411 void vfio_ap_mdev_unregister(void)
1413 mdev_unregister_device(&matrix_dev->device);
1414 mdev_unregister_driver(&vfio_ap_matrix_driver);