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);
364 vfio_uninit_group_dev(&matrix_mdev->vdev);
367 atomic_inc(&matrix_dev->available_instances);
371 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
373 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
375 vfio_unregister_group_dev(&matrix_mdev->vdev);
377 mutex_lock(&matrix_dev->lock);
378 vfio_ap_mdev_reset_queues(matrix_mdev);
379 list_del(&matrix_mdev->node);
380 mutex_unlock(&matrix_dev->lock);
381 vfio_uninit_group_dev(&matrix_mdev->vdev);
383 atomic_inc(&matrix_dev->available_instances);
386 static ssize_t name_show(struct mdev_type *mtype,
387 struct mdev_type_attribute *attr, char *buf)
389 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
392 static MDEV_TYPE_ATTR_RO(name);
394 static ssize_t available_instances_show(struct mdev_type *mtype,
395 struct mdev_type_attribute *attr,
398 return sprintf(buf, "%d\n",
399 atomic_read(&matrix_dev->available_instances));
402 static MDEV_TYPE_ATTR_RO(available_instances);
404 static ssize_t device_api_show(struct mdev_type *mtype,
405 struct mdev_type_attribute *attr, char *buf)
407 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
410 static MDEV_TYPE_ATTR_RO(device_api);
412 static struct attribute *vfio_ap_mdev_type_attrs[] = {
413 &mdev_type_attr_name.attr,
414 &mdev_type_attr_device_api.attr,
415 &mdev_type_attr_available_instances.attr,
419 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
420 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
421 .attrs = vfio_ap_mdev_type_attrs,
424 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
425 &vfio_ap_mdev_hwvirt_type_group,
429 struct vfio_ap_queue_reserved {
436 * vfio_ap_has_queue - determines if the AP queue containing the target in @data
438 * @dev: an AP queue device
439 * @data: a struct vfio_ap_queue_reserved reference
441 * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
442 * apid or apqi specified in @data:
444 * - If @data contains both an apid and apqi value, then @data will be flagged
445 * as reserved if the APID and APQI fields for the AP queue device matches
447 * - If @data contains only an apid value, @data will be flagged as
448 * reserved if the APID field in the AP queue device matches
450 * - If @data contains only an apqi value, @data will be flagged as
451 * reserved if the APQI field in the AP queue device matches
453 * Return: 0 to indicate the input to function succeeded. Returns -EINVAL if
454 * @data does not contain either an apid or apqi.
456 static int vfio_ap_has_queue(struct device *dev, void *data)
458 struct vfio_ap_queue_reserved *qres = data;
459 struct ap_queue *ap_queue = to_ap_queue(dev);
463 if (qres->apid && qres->apqi) {
464 qid = AP_MKQID(*qres->apid, *qres->apqi);
465 if (qid == ap_queue->qid)
466 qres->reserved = true;
467 } else if (qres->apid && !qres->apqi) {
468 id = AP_QID_CARD(ap_queue->qid);
469 if (id == *qres->apid)
470 qres->reserved = true;
471 } else if (!qres->apid && qres->apqi) {
472 id = AP_QID_QUEUE(ap_queue->qid);
473 if (id == *qres->apqi)
474 qres->reserved = true;
483 * vfio_ap_verify_queue_reserved - verifies that the AP queue containing
484 * @apid or @aqpi is reserved
486 * @apid: an AP adapter ID
487 * @apqi: an AP queue index
489 * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
490 * driver according to the following rules:
492 * - If both @apid and @apqi are not NULL, then there must be an AP queue
493 * device bound to the vfio_ap driver with the APQN identified by @apid and
496 * - If only @apid is not NULL, then there must be an AP queue device bound
497 * to the vfio_ap driver with an APQN containing @apid
499 * - If only @apqi is not NULL, then there must be an AP queue device bound
500 * to the vfio_ap driver with an APQN containing @apqi
502 * Return: 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
504 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
508 struct vfio_ap_queue_reserved qres;
512 qres.reserved = false;
514 ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
515 &qres, vfio_ap_has_queue);
522 return -EADDRNOTAVAIL;
526 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
531 unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
533 if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
534 return vfio_ap_verify_queue_reserved(&apid, NULL);
536 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
537 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
546 * vfio_ap_mdev_verify_no_sharing - verifies that the AP matrix is not configured
548 * @matrix_mdev: the mediated matrix device
550 * Verifies that the APQNs derived from the cross product of the AP adapter IDs
551 * and AP queue indexes comprising the AP matrix are not configured for another
552 * mediated device. AP queue sharing is not allowed.
554 * Return: 0 if the APQNs are not shared; otherwise returns -EADDRINUSE.
556 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
558 struct ap_matrix_mdev *lstdev;
559 DECLARE_BITMAP(apm, AP_DEVICES);
560 DECLARE_BITMAP(aqm, AP_DOMAINS);
562 list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
563 if (matrix_mdev == lstdev)
566 memset(apm, 0, sizeof(apm));
567 memset(aqm, 0, sizeof(aqm));
570 * We work on full longs, as we can only exclude the leftover
571 * bits in non-inverse order. The leftover is all zeros.
573 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
574 lstdev->matrix.apm, AP_DEVICES))
577 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
578 lstdev->matrix.aqm, AP_DOMAINS))
588 * assign_adapter_store - parses the APID from @buf and sets the
589 * corresponding bit in the mediated matrix device's APM
591 * @dev: the matrix device
592 * @attr: the mediated matrix device's assign_adapter attribute
593 * @buf: a buffer containing the AP adapter number (APID) to
595 * @count: the number of bytes in @buf
597 * Return: the number of bytes processed if the APID is valid; otherwise,
598 * returns one of the following errors:
601 * The APID is not a valid number
604 * The APID exceeds the maximum value configured for the system
607 * An APQN derived from the cross product of the APID being assigned
608 * and the APQIs previously assigned is not bound to the vfio_ap device
609 * driver; or, if no APQIs have yet been assigned, the APID is not
610 * contained in an APQN bound to the vfio_ap device driver.
613 * An APQN derived from the cross product of the APID being assigned
614 * and the APQIs previously assigned is being used by another mediated
617 static ssize_t assign_adapter_store(struct device *dev,
618 struct device_attribute *attr,
619 const char *buf, size_t count)
623 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
625 mutex_lock(&matrix_dev->lock);
627 /* If the KVM guest is running, disallow assignment of adapter */
628 if (matrix_mdev->kvm) {
633 ret = kstrtoul(buf, 0, &apid);
637 if (apid > matrix_mdev->matrix.apm_max) {
643 * Set the bit in the AP mask (APM) corresponding to the AP adapter
644 * number (APID). The bits in the mask, from most significant to least
645 * significant bit, correspond to APIDs 0-255.
647 ret = vfio_ap_mdev_verify_queues_reserved_for_apid(matrix_mdev, apid);
651 set_bit_inv(apid, matrix_mdev->matrix.apm);
653 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
661 clear_bit_inv(apid, matrix_mdev->matrix.apm);
663 mutex_unlock(&matrix_dev->lock);
667 static DEVICE_ATTR_WO(assign_adapter);
670 * unassign_adapter_store - parses the APID from @buf and clears the
671 * corresponding bit in the mediated matrix device's APM
673 * @dev: the matrix device
674 * @attr: the mediated matrix device's unassign_adapter attribute
675 * @buf: a buffer containing the adapter number (APID) to be unassigned
676 * @count: the number of bytes in @buf
678 * Return: the number of bytes processed if the APID is valid; otherwise,
679 * returns one of the following errors:
680 * -EINVAL if the APID is not a number
681 * -ENODEV if the APID it exceeds the maximum value configured for the
684 static ssize_t unassign_adapter_store(struct device *dev,
685 struct device_attribute *attr,
686 const char *buf, size_t count)
690 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
692 mutex_lock(&matrix_dev->lock);
694 /* If the KVM guest is running, disallow unassignment of adapter */
695 if (matrix_mdev->kvm) {
700 ret = kstrtoul(buf, 0, &apid);
704 if (apid > matrix_mdev->matrix.apm_max) {
709 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
712 mutex_unlock(&matrix_dev->lock);
715 static DEVICE_ATTR_WO(unassign_adapter);
718 vfio_ap_mdev_verify_queues_reserved_for_apqi(struct ap_matrix_mdev *matrix_mdev,
723 unsigned long nbits = matrix_mdev->matrix.apm_max + 1;
725 if (find_first_bit_inv(matrix_mdev->matrix.apm, nbits) >= nbits)
726 return vfio_ap_verify_queue_reserved(NULL, &apqi);
728 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, nbits) {
729 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
738 * assign_domain_store - parses the APQI from @buf and sets the
739 * corresponding bit in the mediated matrix device's AQM
742 * @dev: the matrix device
743 * @attr: the mediated matrix device's assign_domain attribute
744 * @buf: a buffer containing the AP queue index (APQI) of the domain to
746 * @count: the number of bytes in @buf
748 * Return: the number of bytes processed if the APQI is valid; otherwise returns
749 * one of the following errors:
752 * The APQI is not a valid number
755 * The APQI exceeds the maximum value configured for the system
758 * An APQN derived from the cross product of the APQI being assigned
759 * and the APIDs previously assigned is not bound to the vfio_ap device
760 * driver; or, if no APIDs have yet been assigned, the APQI is not
761 * contained in an APQN bound to the vfio_ap device driver.
764 * An APQN derived from the cross product of the APQI being assigned
765 * and the APIDs previously assigned is being used by another mediated
768 static ssize_t assign_domain_store(struct device *dev,
769 struct device_attribute *attr,
770 const char *buf, size_t count)
774 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
775 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
777 mutex_lock(&matrix_dev->lock);
779 /* If the KVM guest is running, disallow assignment of domain */
780 if (matrix_mdev->kvm) {
785 ret = kstrtoul(buf, 0, &apqi);
788 if (apqi > max_apqi) {
793 ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
797 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
799 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
807 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
809 mutex_unlock(&matrix_dev->lock);
813 static DEVICE_ATTR_WO(assign_domain);
817 * unassign_domain_store - parses the APQI from @buf and clears the
818 * corresponding bit in the mediated matrix device's AQM
820 * @dev: the matrix device
821 * @attr: the mediated matrix device's unassign_domain attribute
822 * @buf: a buffer containing the AP queue index (APQI) of the domain to
824 * @count: the number of bytes in @buf
826 * Return: the number of bytes processed if the APQI is valid; otherwise,
827 * returns one of the following errors:
828 * -EINVAL if the APQI is not a number
829 * -ENODEV if the APQI exceeds the maximum value configured for the system
831 static ssize_t unassign_domain_store(struct device *dev,
832 struct device_attribute *attr,
833 const char *buf, size_t count)
837 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
839 mutex_lock(&matrix_dev->lock);
841 /* If the KVM guest is running, disallow unassignment of domain */
842 if (matrix_mdev->kvm) {
847 ret = kstrtoul(buf, 0, &apqi);
851 if (apqi > matrix_mdev->matrix.aqm_max) {
856 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
860 mutex_unlock(&matrix_dev->lock);
863 static DEVICE_ATTR_WO(unassign_domain);
866 * assign_control_domain_store - parses the domain ID from @buf and sets
867 * the corresponding bit in the mediated matrix device's ADM
870 * @dev: the matrix device
871 * @attr: the mediated matrix device's assign_control_domain attribute
872 * @buf: a buffer containing the domain ID to be assigned
873 * @count: the number of bytes in @buf
875 * Return: the number of bytes processed if the domain ID is valid; otherwise,
876 * returns one of the following errors:
877 * -EINVAL if the ID is not a number
878 * -ENODEV if the ID exceeds the maximum value configured for the system
880 static ssize_t assign_control_domain_store(struct device *dev,
881 struct device_attribute *attr,
882 const char *buf, size_t count)
886 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
888 mutex_lock(&matrix_dev->lock);
890 /* If the KVM guest is running, disallow assignment of control domain */
891 if (matrix_mdev->kvm) {
896 ret = kstrtoul(buf, 0, &id);
900 if (id > matrix_mdev->matrix.adm_max) {
905 /* Set the bit in the ADM (bitmask) corresponding to the AP control
906 * domain number (id). The bits in the mask, from most significant to
907 * least significant, correspond to IDs 0 up to the one less than the
908 * number of control domains that can be assigned.
910 set_bit_inv(id, matrix_mdev->matrix.adm);
913 mutex_unlock(&matrix_dev->lock);
916 static DEVICE_ATTR_WO(assign_control_domain);
919 * unassign_control_domain_store - parses the domain ID from @buf and
920 * clears the corresponding bit in the mediated matrix device's ADM
922 * @dev: the matrix device
923 * @attr: the mediated matrix device's unassign_control_domain attribute
924 * @buf: a buffer containing the domain ID to be unassigned
925 * @count: the number of bytes in @buf
927 * Return: the number of bytes processed if the domain ID is valid; otherwise,
928 * returns one of the following errors:
929 * -EINVAL if the ID is not a number
930 * -ENODEV if the ID exceeds the maximum value configured for the system
932 static ssize_t unassign_control_domain_store(struct device *dev,
933 struct device_attribute *attr,
934 const char *buf, size_t count)
938 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
939 unsigned long max_domid = matrix_mdev->matrix.adm_max;
941 mutex_lock(&matrix_dev->lock);
943 /* If a KVM guest is running, disallow unassignment of control domain */
944 if (matrix_mdev->kvm) {
949 ret = kstrtoul(buf, 0, &domid);
952 if (domid > max_domid) {
957 clear_bit_inv(domid, matrix_mdev->matrix.adm);
960 mutex_unlock(&matrix_dev->lock);
963 static DEVICE_ATTR_WO(unassign_control_domain);
965 static ssize_t control_domains_show(struct device *dev,
966 struct device_attribute *dev_attr,
973 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
974 unsigned long max_domid = matrix_mdev->matrix.adm_max;
976 mutex_lock(&matrix_dev->lock);
977 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
978 n = sprintf(bufpos, "%04lx\n", id);
982 mutex_unlock(&matrix_dev->lock);
986 static DEVICE_ATTR_RO(control_domains);
988 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
991 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
997 unsigned long napm_bits = matrix_mdev->matrix.apm_max + 1;
998 unsigned long naqm_bits = matrix_mdev->matrix.aqm_max + 1;
1002 apid1 = find_first_bit_inv(matrix_mdev->matrix.apm, napm_bits);
1003 apqi1 = find_first_bit_inv(matrix_mdev->matrix.aqm, naqm_bits);
1005 mutex_lock(&matrix_dev->lock);
1007 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1008 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1009 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1011 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1017 } else if (apid1 < napm_bits) {
1018 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1019 n = sprintf(bufpos, "%02lx.\n", apid);
1023 } else if (apqi1 < naqm_bits) {
1024 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, naqm_bits) {
1025 n = sprintf(bufpos, ".%04lx\n", apqi);
1031 mutex_unlock(&matrix_dev->lock);
1035 static DEVICE_ATTR_RO(matrix);
1037 static struct attribute *vfio_ap_mdev_attrs[] = {
1038 &dev_attr_assign_adapter.attr,
1039 &dev_attr_unassign_adapter.attr,
1040 &dev_attr_assign_domain.attr,
1041 &dev_attr_unassign_domain.attr,
1042 &dev_attr_assign_control_domain.attr,
1043 &dev_attr_unassign_control_domain.attr,
1044 &dev_attr_control_domains.attr,
1045 &dev_attr_matrix.attr,
1049 static struct attribute_group vfio_ap_mdev_attr_group = {
1050 .attrs = vfio_ap_mdev_attrs
1053 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1054 &vfio_ap_mdev_attr_group,
1059 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1060 * to manage AP resources for the guest whose state is represented by @kvm
1062 * @matrix_mdev: a mediated matrix device
1063 * @kvm: reference to KVM instance
1065 * Note: The matrix_dev->lock must be taken prior to calling
1066 * this function; however, the lock will be temporarily released while the
1067 * guest's AP configuration is set to avoid a potential lockdep splat.
1068 * The kvm->lock is taken to set the guest's AP configuration which, under
1069 * certain circumstances, will result in a circular lock dependency if this is
1070 * done under the @matrix_mdev->lock.
1072 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1073 * otherwise, returns an -EPERM.
1075 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1078 struct ap_matrix_mdev *m;
1080 if (kvm->arch.crypto.crycbd) {
1081 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1082 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1083 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1085 mutex_lock(&kvm->lock);
1086 mutex_lock(&matrix_dev->lock);
1088 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1089 if (m != matrix_mdev && m->kvm == kvm) {
1090 mutex_unlock(&kvm->lock);
1091 mutex_unlock(&matrix_dev->lock);
1097 matrix_mdev->kvm = kvm;
1098 kvm_arch_crypto_set_masks(kvm,
1099 matrix_mdev->matrix.apm,
1100 matrix_mdev->matrix.aqm,
1101 matrix_mdev->matrix.adm);
1103 mutex_unlock(&kvm->lock);
1104 mutex_unlock(&matrix_dev->lock);
1111 * vfio_ap_mdev_iommu_notifier - IOMMU notifier callback
1113 * @nb: The notifier block
1114 * @action: Action to be taken
1115 * @data: data associated with the request
1117 * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
1118 * pinned before). Other requests are ignored.
1120 * Return: for an UNMAP request, NOFITY_OK; otherwise NOTIFY_DONE.
1122 static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
1123 unsigned long action, void *data)
1125 struct ap_matrix_mdev *matrix_mdev;
1127 matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
1129 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
1130 struct vfio_iommu_type1_dma_unmap *unmap = data;
1131 unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
1133 vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
1141 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1144 * @matrix_mdev: a matrix mediated device
1146 * Note: The matrix_dev->lock must be taken prior to calling
1147 * this function; however, the lock will be temporarily released while the
1148 * guest's AP configuration is cleared to avoid a potential lockdep splat.
1149 * The kvm->lock is taken to clear the guest's AP configuration which, under
1150 * certain circumstances, will result in a circular lock dependency if this is
1151 * done under the @matrix_mdev->lock.
1153 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev,
1156 if (kvm && kvm->arch.crypto.crycbd) {
1157 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1158 kvm->arch.crypto.pqap_hook = NULL;
1159 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1161 mutex_lock(&kvm->lock);
1162 mutex_lock(&matrix_dev->lock);
1164 kvm_arch_crypto_clear_masks(kvm);
1165 vfio_ap_mdev_reset_queues(matrix_mdev);
1167 matrix_mdev->kvm = NULL;
1169 mutex_unlock(&kvm->lock);
1170 mutex_unlock(&matrix_dev->lock);
1174 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1175 unsigned long action, void *data)
1177 int notify_rc = NOTIFY_OK;
1178 struct ap_matrix_mdev *matrix_mdev;
1180 if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1183 matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1186 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1187 else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1188 notify_rc = NOTIFY_DONE;
1193 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1196 struct vfio_ap_queue *q = NULL;
1198 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1201 q = dev_get_drvdata(dev);
1208 int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1211 struct ap_queue_status status;
1219 status = ap_zapq(q->apqn);
1220 switch (status.response_code) {
1221 case AP_RESPONSE_NORMAL:
1224 case AP_RESPONSE_RESET_IN_PROGRESS:
1231 case AP_RESPONSE_Q_NOT_AVAIL:
1232 case AP_RESPONSE_DECONFIGURED:
1233 case AP_RESPONSE_CHECKSTOPPED:
1234 WARN_ON_ONCE(status.irq_enabled);
1236 goto free_resources;
1238 /* things are really broken, give up */
1239 WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1240 status.response_code);
1244 /* wait for the reset to take effect */
1246 if (status.queue_empty && !status.irq_enabled)
1249 status = ap_tapq(q->apqn, NULL);
1251 WARN_ON_ONCE(retry2 <= 0);
1254 vfio_ap_free_aqic_resources(q);
1259 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1263 unsigned long apid, apqi;
1264 struct vfio_ap_queue *q;
1266 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1267 matrix_mdev->matrix.apm_max + 1) {
1268 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1269 matrix_mdev->matrix.aqm_max + 1) {
1270 q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1271 ret = vfio_ap_mdev_reset_queue(q, 1);
1273 * Regardless whether a queue turns out to be busy, or
1274 * is not operational, we need to continue resetting
1275 * the remaining queues.
1285 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1287 struct ap_matrix_mdev *matrix_mdev =
1288 container_of(vdev, struct ap_matrix_mdev, vdev);
1289 unsigned long events;
1292 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1293 events = VFIO_GROUP_NOTIFY_SET_KVM;
1295 ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1296 &events, &matrix_mdev->group_notifier);
1300 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1301 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1302 ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1303 &events, &matrix_mdev->iommu_notifier);
1305 goto out_unregister_group;
1308 out_unregister_group:
1309 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1310 &matrix_mdev->group_notifier);
1314 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1316 struct ap_matrix_mdev *matrix_mdev =
1317 container_of(vdev, struct ap_matrix_mdev, vdev);
1319 vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1320 &matrix_mdev->iommu_notifier);
1321 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1322 &matrix_mdev->group_notifier);
1323 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1326 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1328 unsigned long minsz;
1329 struct vfio_device_info info;
1331 minsz = offsetofend(struct vfio_device_info, num_irqs);
1333 if (copy_from_user(&info, (void __user *)arg, minsz))
1336 if (info.argsz < minsz)
1339 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1340 info.num_regions = 0;
1343 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1346 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1347 unsigned int cmd, unsigned long arg)
1349 struct ap_matrix_mdev *matrix_mdev =
1350 container_of(vdev, struct ap_matrix_mdev, vdev);
1353 mutex_lock(&matrix_dev->lock);
1355 case VFIO_DEVICE_GET_INFO:
1356 ret = vfio_ap_mdev_get_device_info(arg);
1358 case VFIO_DEVICE_RESET:
1359 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
1365 mutex_unlock(&matrix_dev->lock);
1370 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1371 .open_device = vfio_ap_mdev_open_device,
1372 .close_device = vfio_ap_mdev_close_device,
1373 .ioctl = vfio_ap_mdev_ioctl,
1376 static struct mdev_driver vfio_ap_matrix_driver = {
1378 .name = "vfio_ap_mdev",
1379 .owner = THIS_MODULE,
1380 .mod_name = KBUILD_MODNAME,
1381 .dev_groups = vfio_ap_mdev_attr_groups,
1383 .probe = vfio_ap_mdev_probe,
1384 .remove = vfio_ap_mdev_remove,
1387 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1388 .owner = THIS_MODULE,
1389 .device_driver = &vfio_ap_matrix_driver,
1390 .supported_type_groups = vfio_ap_mdev_type_groups,
1393 int vfio_ap_mdev_register(void)
1397 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1399 ret = mdev_register_driver(&vfio_ap_matrix_driver);
1403 ret = mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1409 mdev_unregister_driver(&vfio_ap_matrix_driver);
1413 void vfio_ap_mdev_unregister(void)
1415 mdev_unregister_device(&matrix_dev->device);
1416 mdev_unregister_driver(&vfio_ap_matrix_driver);