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 mdev_device *mdev);
28 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
30 static int match_apqn(struct device *dev, const void *data)
32 struct vfio_ap_queue *q = dev_get_drvdata(dev);
34 return (q->apqn == *(int *)(data)) ? 1 : 0;
38 * vfio_ap_get_queue - retrieve a queue with a specific APQN from a list
39 * @matrix_mdev: the associated mediated matrix
40 * @apqn: The queue APQN
42 * Retrieve a queue with a specific APQN from the list of the
43 * devices of the vfio_ap_drv.
44 * Verify that the APID and the APQI are set in the matrix.
46 * Return: the pointer to the associated vfio_ap_queue
48 static struct vfio_ap_queue *vfio_ap_get_queue(
49 struct ap_matrix_mdev *matrix_mdev,
52 struct vfio_ap_queue *q;
54 if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
56 if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
59 q = vfio_ap_find_queue(apqn);
61 q->matrix_mdev = matrix_mdev;
67 * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
68 * @apqn: The AP Queue number
70 * Checks the IRQ bit for the status of this APQN using ap_tapq.
71 * Returns if the ap_tapq function succeeded and the bit is clear.
72 * Returns if ap_tapq function failed with invalid, deconfigured or
74 * Otherwise retries up to 5 times after waiting 20ms.
76 static void vfio_ap_wait_for_irqclear(int apqn)
78 struct ap_queue_status status;
82 status = ap_tapq(apqn, NULL);
83 switch (status.response_code) {
84 case AP_RESPONSE_NORMAL:
85 case AP_RESPONSE_RESET_IN_PROGRESS:
86 if (!status.irq_enabled)
89 case AP_RESPONSE_BUSY:
92 case AP_RESPONSE_Q_NOT_AVAIL:
93 case AP_RESPONSE_DECONFIGURED:
94 case AP_RESPONSE_CHECKSTOPPED:
96 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
97 status.response_code, apqn);
102 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
103 __func__, status.response_code, apqn);
107 * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
108 * @q: The vfio_ap_queue
110 * Unregisters the ISC in the GIB when the saved ISC not invalid.
111 * Unpins the guest's page holding the NIB when it exists.
112 * Resets the saved_pfn and saved_isc to invalid values.
114 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
118 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
119 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
120 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
121 q->saved_isc = VFIO_AP_ISC_INVALID;
123 if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) {
124 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev),
131 * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
132 * @q: The vfio_ap_queue
134 * Uses ap_aqic to disable the interruption and in case of success, reset
135 * in progress or IRQ disable command already proceeded: calls
136 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
137 * and calls vfio_ap_free_aqic_resources() to free the resources associated
138 * with the AP interrupt handling.
140 * In the case the AP is busy, or a reset is in progress,
141 * retries after 20ms, up to 5 times.
143 * Returns if ap_aqic function failed with invalid, deconfigured or
146 * Return: &struct ap_queue_status
148 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
150 struct ap_qirq_ctrl aqic_gisa = {};
151 struct ap_queue_status status;
155 status = ap_aqic(q->apqn, aqic_gisa, NULL);
156 switch (status.response_code) {
157 case AP_RESPONSE_OTHERWISE_CHANGED:
158 case AP_RESPONSE_NORMAL:
159 vfio_ap_wait_for_irqclear(q->apqn);
161 case AP_RESPONSE_RESET_IN_PROGRESS:
162 case AP_RESPONSE_BUSY:
165 case AP_RESPONSE_Q_NOT_AVAIL:
166 case AP_RESPONSE_DECONFIGURED:
167 case AP_RESPONSE_CHECKSTOPPED:
168 case AP_RESPONSE_INVALID_ADDRESS:
170 /* All cases in default means AP not operational */
171 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
172 status.response_code);
177 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
178 status.response_code);
180 vfio_ap_free_aqic_resources(q);
181 q->matrix_mdev = NULL;
186 * vfio_ap_irq_enable - Enable Interruption for a APQN
188 * @q: the vfio_ap_queue holding AQIC parameters
190 * Pin the NIB saved in *q
191 * Register the guest ISC to GIB interface and retrieve the
192 * host ISC to issue the host side PQAP/AQIC
194 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
195 * vfio_pin_pages failed.
197 * Otherwise return the ap_queue_status returned by the ap_aqic(),
198 * all retry handling will be done by the guest.
200 * Return: &struct ap_queue_status
202 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
206 struct ap_qirq_ctrl aqic_gisa = {};
207 struct ap_queue_status status = {};
208 struct kvm_s390_gisa *gisa;
210 unsigned long h_nib, g_pfn, h_pfn;
213 g_pfn = nib >> PAGE_SHIFT;
214 ret = vfio_pin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1,
215 IOMMU_READ | IOMMU_WRITE, &h_pfn);
220 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
224 kvm = q->matrix_mdev->kvm;
225 gisa = kvm->arch.gisa_int.origin;
227 h_nib = (h_pfn << PAGE_SHIFT) | (nib & ~PAGE_MASK);
228 aqic_gisa.gisc = isc;
229 aqic_gisa.isc = kvm_s390_gisc_register(kvm, isc);
231 aqic_gisa.gisa = (uint64_t)gisa >> 4;
233 status = ap_aqic(q->apqn, aqic_gisa, (void *)h_nib);
234 switch (status.response_code) {
235 case AP_RESPONSE_NORMAL:
236 /* See if we did clear older IRQ configuration */
237 vfio_ap_free_aqic_resources(q);
238 q->saved_pfn = g_pfn;
241 case AP_RESPONSE_OTHERWISE_CHANGED:
242 /* We could not modify IRQ setings: clear new configuration */
243 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1);
244 kvm_s390_gisc_unregister(kvm, isc);
247 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
248 status.response_code);
249 vfio_ap_irq_disable(q);
257 * handle_pqap - PQAP instruction callback
259 * @vcpu: The vcpu on which we received the PQAP instruction
261 * Get the general register contents to initialize internal variables.
266 * Response.status may be set to following Response Code:
267 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
268 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
269 * - AP_RESPONSE_NORMAL (0) : in case of successs
270 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
271 * We take the matrix_dev lock to ensure serialization on queues and
272 * mediated device access.
274 * Return: 0 if we could handle the request inside KVM.
275 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
277 static int handle_pqap(struct kvm_vcpu *vcpu)
281 struct vfio_ap_queue *q;
282 struct ap_queue_status qstatus = {
283 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
284 struct ap_matrix_mdev *matrix_mdev;
286 /* If we do not use the AIV facility just go to userland */
287 if (!(vcpu->arch.sie_block->eca & ECA_AIV))
290 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
291 mutex_lock(&matrix_dev->lock);
293 if (!vcpu->kvm->arch.crypto.pqap_hook)
295 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
296 struct ap_matrix_mdev, pqap_hook);
299 * If the KVM pointer is in the process of being set, wait until the
300 * process has completed.
302 wait_event_cmd(matrix_mdev->wait_for_kvm,
303 !matrix_mdev->kvm_busy,
304 mutex_unlock(&matrix_dev->lock),
305 mutex_lock(&matrix_dev->lock));
307 /* If the there is no guest using the mdev, there is nothing to do */
308 if (!matrix_mdev->kvm)
311 q = vfio_ap_get_queue(matrix_mdev, apqn);
315 status = vcpu->run->s.regs.gprs[1];
317 /* If IR bit(16) is set we enable the interrupt */
318 if ((status >> (63 - 16)) & 0x01)
319 qstatus = vfio_ap_irq_enable(q, status & 0x07,
320 vcpu->run->s.regs.gprs[2]);
322 qstatus = vfio_ap_irq_disable(q);
325 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
326 vcpu->run->s.regs.gprs[1] >>= 32;
327 mutex_unlock(&matrix_dev->lock);
331 static void vfio_ap_matrix_init(struct ap_config_info *info,
332 struct ap_matrix *matrix)
334 matrix->apm_max = info->apxa ? info->Na : 63;
335 matrix->aqm_max = info->apxa ? info->Nd : 15;
336 matrix->adm_max = info->apxa ? info->Nd : 15;
339 static int vfio_ap_mdev_create(struct mdev_device *mdev)
341 struct ap_matrix_mdev *matrix_mdev;
343 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
346 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
348 atomic_inc(&matrix_dev->available_instances);
352 matrix_mdev->mdev = mdev;
353 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
354 init_waitqueue_head(&matrix_mdev->wait_for_kvm);
355 mdev_set_drvdata(mdev, matrix_mdev);
356 matrix_mdev->pqap_hook.hook = handle_pqap;
357 matrix_mdev->pqap_hook.owner = THIS_MODULE;
358 mutex_lock(&matrix_dev->lock);
359 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
360 mutex_unlock(&matrix_dev->lock);
365 static int vfio_ap_mdev_remove(struct mdev_device *mdev)
367 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
369 mutex_lock(&matrix_dev->lock);
370 vfio_ap_mdev_reset_queues(mdev);
371 list_del(&matrix_mdev->node);
373 mdev_set_drvdata(mdev, NULL);
374 atomic_inc(&matrix_dev->available_instances);
375 mutex_unlock(&matrix_dev->lock);
380 static ssize_t name_show(struct mdev_type *mtype,
381 struct mdev_type_attribute *attr, char *buf)
383 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
386 static MDEV_TYPE_ATTR_RO(name);
388 static ssize_t available_instances_show(struct mdev_type *mtype,
389 struct mdev_type_attribute *attr,
392 return sprintf(buf, "%d\n",
393 atomic_read(&matrix_dev->available_instances));
396 static MDEV_TYPE_ATTR_RO(available_instances);
398 static ssize_t device_api_show(struct mdev_type *mtype,
399 struct mdev_type_attribute *attr, char *buf)
401 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
404 static MDEV_TYPE_ATTR_RO(device_api);
406 static struct attribute *vfio_ap_mdev_type_attrs[] = {
407 &mdev_type_attr_name.attr,
408 &mdev_type_attr_device_api.attr,
409 &mdev_type_attr_available_instances.attr,
413 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
414 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
415 .attrs = vfio_ap_mdev_type_attrs,
418 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
419 &vfio_ap_mdev_hwvirt_type_group,
423 struct vfio_ap_queue_reserved {
430 * vfio_ap_has_queue - determines if the AP queue containing the target in @data
432 * @dev: an AP queue device
433 * @data: a struct vfio_ap_queue_reserved reference
435 * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
436 * apid or apqi specified in @data:
438 * - If @data contains both an apid and apqi value, then @data will be flagged
439 * as reserved if the APID and APQI fields for the AP queue device matches
441 * - If @data contains only an apid value, @data will be flagged as
442 * reserved if the APID field in the AP queue device matches
444 * - If @data contains only an apqi value, @data will be flagged as
445 * reserved if the APQI field in the AP queue device matches
447 * Return: 0 to indicate the input to function succeeded. Returns -EINVAL if
448 * @data does not contain either an apid or apqi.
450 static int vfio_ap_has_queue(struct device *dev, void *data)
452 struct vfio_ap_queue_reserved *qres = data;
453 struct ap_queue *ap_queue = to_ap_queue(dev);
457 if (qres->apid && qres->apqi) {
458 qid = AP_MKQID(*qres->apid, *qres->apqi);
459 if (qid == ap_queue->qid)
460 qres->reserved = true;
461 } else if (qres->apid && !qres->apqi) {
462 id = AP_QID_CARD(ap_queue->qid);
463 if (id == *qres->apid)
464 qres->reserved = true;
465 } else if (!qres->apid && qres->apqi) {
466 id = AP_QID_QUEUE(ap_queue->qid);
467 if (id == *qres->apqi)
468 qres->reserved = true;
477 * vfio_ap_verify_queue_reserved - verifies that the AP queue containing
478 * @apid or @aqpi is reserved
480 * @apid: an AP adapter ID
481 * @apqi: an AP queue index
483 * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
484 * driver according to the following rules:
486 * - If both @apid and @apqi are not NULL, then there must be an AP queue
487 * device bound to the vfio_ap driver with the APQN identified by @apid and
490 * - If only @apid is not NULL, then there must be an AP queue device bound
491 * to the vfio_ap driver with an APQN containing @apid
493 * - If only @apqi is not NULL, then there must be an AP queue device bound
494 * to the vfio_ap driver with an APQN containing @apqi
496 * Return: 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
498 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
502 struct vfio_ap_queue_reserved qres;
506 qres.reserved = false;
508 ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
509 &qres, vfio_ap_has_queue);
516 return -EADDRNOTAVAIL;
520 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
525 unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
527 if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
528 return vfio_ap_verify_queue_reserved(&apid, NULL);
530 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
531 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
540 * vfio_ap_mdev_verify_no_sharing - verifies that the AP matrix is not configured
542 * @matrix_mdev: the mediated matrix device
544 * Verifies that the APQNs derived from the cross product of the AP adapter IDs
545 * and AP queue indexes comprising the AP matrix are not configured for another
546 * mediated device. AP queue sharing is not allowed.
548 * Return: 0 if the APQNs are not shared; otherwise returns -EADDRINUSE.
550 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
552 struct ap_matrix_mdev *lstdev;
553 DECLARE_BITMAP(apm, AP_DEVICES);
554 DECLARE_BITMAP(aqm, AP_DOMAINS);
556 list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
557 if (matrix_mdev == lstdev)
560 memset(apm, 0, sizeof(apm));
561 memset(aqm, 0, sizeof(aqm));
564 * We work on full longs, as we can only exclude the leftover
565 * bits in non-inverse order. The leftover is all zeros.
567 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
568 lstdev->matrix.apm, AP_DEVICES))
571 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
572 lstdev->matrix.aqm, AP_DOMAINS))
582 * assign_adapter_store - parses the APID from @buf and sets the
583 * corresponding bit in the mediated matrix device's APM
585 * @dev: the matrix device
586 * @attr: the mediated matrix device's assign_adapter attribute
587 * @buf: a buffer containing the AP adapter number (APID) to
589 * @count: the number of bytes in @buf
591 * Return: the number of bytes processed if the APID is valid; otherwise,
592 * returns one of the following errors:
595 * The APID is not a valid number
598 * The APID exceeds the maximum value configured for the system
601 * An APQN derived from the cross product of the APID being assigned
602 * and the APQIs previously assigned is not bound to the vfio_ap device
603 * driver; or, if no APQIs have yet been assigned, the APID is not
604 * contained in an APQN bound to the vfio_ap device driver.
607 * An APQN derived from the cross product of the APID being assigned
608 * and the APQIs previously assigned is being used by another mediated
611 static ssize_t assign_adapter_store(struct device *dev,
612 struct device_attribute *attr,
613 const char *buf, size_t count)
617 struct mdev_device *mdev = mdev_from_dev(dev);
618 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
620 mutex_lock(&matrix_dev->lock);
623 * If the KVM pointer is in flux or the guest is running, disallow
624 * un-assignment of adapter
626 if (matrix_mdev->kvm_busy || 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 mdev_device *mdev = mdev_from_dev(dev);
689 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
691 mutex_lock(&matrix_dev->lock);
694 * If the KVM pointer is in flux or the guest is running, disallow
695 * un-assignment of adapter
697 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
702 ret = kstrtoul(buf, 0, &apid);
706 if (apid > matrix_mdev->matrix.apm_max) {
711 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
714 mutex_unlock(&matrix_dev->lock);
717 static DEVICE_ATTR_WO(unassign_adapter);
720 vfio_ap_mdev_verify_queues_reserved_for_apqi(struct ap_matrix_mdev *matrix_mdev,
725 unsigned long nbits = matrix_mdev->matrix.apm_max + 1;
727 if (find_first_bit_inv(matrix_mdev->matrix.apm, nbits) >= nbits)
728 return vfio_ap_verify_queue_reserved(NULL, &apqi);
730 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, nbits) {
731 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
740 * assign_domain_store - parses the APQI from @buf and sets the
741 * corresponding bit in the mediated matrix device's AQM
744 * @dev: the matrix device
745 * @attr: the mediated matrix device's assign_domain attribute
746 * @buf: a buffer containing the AP queue index (APQI) of the domain to
748 * @count: the number of bytes in @buf
750 * Return: the number of bytes processed if the APQI is valid; otherwise returns
751 * one of the following errors:
754 * The APQI is not a valid number
757 * The APQI exceeds the maximum value configured for the system
760 * An APQN derived from the cross product of the APQI being assigned
761 * and the APIDs previously assigned is not bound to the vfio_ap device
762 * driver; or, if no APIDs have yet been assigned, the APQI is not
763 * contained in an APQN bound to the vfio_ap device driver.
766 * An APQN derived from the cross product of the APQI being assigned
767 * and the APIDs previously assigned is being used by another mediated
770 static ssize_t assign_domain_store(struct device *dev,
771 struct device_attribute *attr,
772 const char *buf, size_t count)
776 struct mdev_device *mdev = mdev_from_dev(dev);
777 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
778 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
780 mutex_lock(&matrix_dev->lock);
783 * If the KVM pointer is in flux or the guest is running, disallow
784 * assignment of domain
786 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
791 ret = kstrtoul(buf, 0, &apqi);
794 if (apqi > max_apqi) {
799 ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
803 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
805 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
813 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
815 mutex_unlock(&matrix_dev->lock);
819 static DEVICE_ATTR_WO(assign_domain);
823 * unassign_domain_store - parses the APQI from @buf and clears the
824 * corresponding bit in the mediated matrix device's AQM
826 * @dev: the matrix device
827 * @attr: the mediated matrix device's unassign_domain attribute
828 * @buf: a buffer containing the AP queue index (APQI) of the domain to
830 * @count: the number of bytes in @buf
832 * Return: the number of bytes processed if the APQI is valid; otherwise,
833 * returns one of the following errors:
834 * -EINVAL if the APQI is not a number
835 * -ENODEV if the APQI exceeds the maximum value configured for the system
837 static ssize_t unassign_domain_store(struct device *dev,
838 struct device_attribute *attr,
839 const char *buf, size_t count)
843 struct mdev_device *mdev = mdev_from_dev(dev);
844 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
846 mutex_lock(&matrix_dev->lock);
849 * If the KVM pointer is in flux or the guest is running, disallow
850 * un-assignment of domain
852 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
857 ret = kstrtoul(buf, 0, &apqi);
861 if (apqi > matrix_mdev->matrix.aqm_max) {
866 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
870 mutex_unlock(&matrix_dev->lock);
873 static DEVICE_ATTR_WO(unassign_domain);
876 * assign_control_domain_store - parses the domain ID from @buf and sets
877 * the corresponding bit in the mediated matrix device's ADM
880 * @dev: the matrix device
881 * @attr: the mediated matrix device's assign_control_domain attribute
882 * @buf: a buffer containing the domain ID to be assigned
883 * @count: the number of bytes in @buf
885 * Return: the number of bytes processed if the domain ID is valid; otherwise,
886 * returns one of the following errors:
887 * -EINVAL if the ID is not a number
888 * -ENODEV if the ID exceeds the maximum value configured for the system
890 static ssize_t assign_control_domain_store(struct device *dev,
891 struct device_attribute *attr,
892 const char *buf, size_t count)
896 struct mdev_device *mdev = mdev_from_dev(dev);
897 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
899 mutex_lock(&matrix_dev->lock);
902 * If the KVM pointer is in flux or the guest is running, disallow
903 * assignment of control domain.
905 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
910 ret = kstrtoul(buf, 0, &id);
914 if (id > matrix_mdev->matrix.adm_max) {
919 /* Set the bit in the ADM (bitmask) corresponding to the AP control
920 * domain number (id). The bits in the mask, from most significant to
921 * least significant, correspond to IDs 0 up to the one less than the
922 * number of control domains that can be assigned.
924 set_bit_inv(id, matrix_mdev->matrix.adm);
927 mutex_unlock(&matrix_dev->lock);
930 static DEVICE_ATTR_WO(assign_control_domain);
933 * unassign_control_domain_store - parses the domain ID from @buf and
934 * clears the corresponding bit in the mediated matrix device's ADM
936 * @dev: the matrix device
937 * @attr: the mediated matrix device's unassign_control_domain attribute
938 * @buf: a buffer containing the domain ID to be unassigned
939 * @count: the number of bytes in @buf
941 * Return: the number of bytes processed if the domain ID is valid; otherwise,
942 * returns one of the following errors:
943 * -EINVAL if the ID is not a number
944 * -ENODEV if the ID exceeds the maximum value configured for the system
946 static ssize_t unassign_control_domain_store(struct device *dev,
947 struct device_attribute *attr,
948 const char *buf, size_t count)
952 struct mdev_device *mdev = mdev_from_dev(dev);
953 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
954 unsigned long max_domid = matrix_mdev->matrix.adm_max;
956 mutex_lock(&matrix_dev->lock);
959 * If the KVM pointer is in flux or the guest is running, disallow
960 * un-assignment of control domain.
962 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
967 ret = kstrtoul(buf, 0, &domid);
970 if (domid > max_domid) {
975 clear_bit_inv(domid, matrix_mdev->matrix.adm);
978 mutex_unlock(&matrix_dev->lock);
981 static DEVICE_ATTR_WO(unassign_control_domain);
983 static ssize_t control_domains_show(struct device *dev,
984 struct device_attribute *dev_attr,
991 struct mdev_device *mdev = mdev_from_dev(dev);
992 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
993 unsigned long max_domid = matrix_mdev->matrix.adm_max;
995 mutex_lock(&matrix_dev->lock);
996 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
997 n = sprintf(bufpos, "%04lx\n", id);
1001 mutex_unlock(&matrix_dev->lock);
1005 static DEVICE_ATTR_RO(control_domains);
1007 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1010 struct mdev_device *mdev = mdev_from_dev(dev);
1011 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1015 unsigned long apid1;
1016 unsigned long apqi1;
1017 unsigned long napm_bits = matrix_mdev->matrix.apm_max + 1;
1018 unsigned long naqm_bits = matrix_mdev->matrix.aqm_max + 1;
1022 apid1 = find_first_bit_inv(matrix_mdev->matrix.apm, napm_bits);
1023 apqi1 = find_first_bit_inv(matrix_mdev->matrix.aqm, naqm_bits);
1025 mutex_lock(&matrix_dev->lock);
1027 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1028 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1029 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1031 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1037 } else if (apid1 < napm_bits) {
1038 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1039 n = sprintf(bufpos, "%02lx.\n", apid);
1043 } else if (apqi1 < naqm_bits) {
1044 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, naqm_bits) {
1045 n = sprintf(bufpos, ".%04lx\n", apqi);
1051 mutex_unlock(&matrix_dev->lock);
1055 static DEVICE_ATTR_RO(matrix);
1057 static struct attribute *vfio_ap_mdev_attrs[] = {
1058 &dev_attr_assign_adapter.attr,
1059 &dev_attr_unassign_adapter.attr,
1060 &dev_attr_assign_domain.attr,
1061 &dev_attr_unassign_domain.attr,
1062 &dev_attr_assign_control_domain.attr,
1063 &dev_attr_unassign_control_domain.attr,
1064 &dev_attr_control_domains.attr,
1065 &dev_attr_matrix.attr,
1069 static struct attribute_group vfio_ap_mdev_attr_group = {
1070 .attrs = vfio_ap_mdev_attrs
1073 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1074 &vfio_ap_mdev_attr_group,
1079 * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
1080 * to manage AP resources for the guest whose state is represented by @kvm
1082 * @matrix_mdev: a mediated matrix device
1083 * @kvm: reference to KVM instance
1085 * Note: The matrix_dev->lock must be taken prior to calling
1086 * this function; however, the lock will be temporarily released while the
1087 * guest's AP configuration is set to avoid a potential lockdep splat.
1088 * The kvm->lock is taken to set the guest's AP configuration which, under
1089 * certain circumstances, will result in a circular lock dependency if this is
1090 * done under the @matrix_mdev->lock.
1092 * Return: 0 if no other mediated matrix device has a reference to @kvm;
1093 * otherwise, returns an -EPERM.
1095 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1098 struct ap_matrix_mdev *m;
1100 if (kvm->arch.crypto.crycbd) {
1101 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1102 if (m != matrix_mdev && m->kvm == kvm)
1107 matrix_mdev->kvm_busy = true;
1108 mutex_unlock(&matrix_dev->lock);
1109 kvm_arch_crypto_set_masks(kvm,
1110 matrix_mdev->matrix.apm,
1111 matrix_mdev->matrix.aqm,
1112 matrix_mdev->matrix.adm);
1113 mutex_lock(&matrix_dev->lock);
1114 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1115 matrix_mdev->kvm = kvm;
1116 matrix_mdev->kvm_busy = false;
1117 wake_up_all(&matrix_mdev->wait_for_kvm);
1124 * vfio_ap_mdev_iommu_notifier - IOMMU notifier callback
1126 * @nb: The notifier block
1127 * @action: Action to be taken
1128 * @data: data associated with the request
1130 * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
1131 * pinned before). Other requests are ignored.
1133 * Return: for an UNMAP request, NOFITY_OK; otherwise NOTIFY_DONE.
1135 static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
1136 unsigned long action, void *data)
1138 struct ap_matrix_mdev *matrix_mdev;
1140 matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
1142 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
1143 struct vfio_iommu_type1_dma_unmap *unmap = data;
1144 unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
1146 vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
1154 * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
1157 * @matrix_mdev: a matrix mediated device
1159 * Note: The matrix_dev->lock must be taken prior to calling
1160 * this function; however, the lock will be temporarily released while the
1161 * guest's AP configuration is cleared to avoid a potential lockdep splat.
1162 * The kvm->lock is taken to clear the guest's AP configuration which, under
1163 * certain circumstances, will result in a circular lock dependency if this is
1164 * done under the @matrix_mdev->lock.
1166 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1169 * If the KVM pointer is in the process of being set, wait until the
1170 * process has completed.
1172 wait_event_cmd(matrix_mdev->wait_for_kvm,
1173 !matrix_mdev->kvm_busy,
1174 mutex_unlock(&matrix_dev->lock),
1175 mutex_lock(&matrix_dev->lock));
1177 if (matrix_mdev->kvm) {
1178 matrix_mdev->kvm_busy = true;
1179 mutex_unlock(&matrix_dev->lock);
1180 kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
1181 mutex_lock(&matrix_dev->lock);
1182 vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
1183 matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
1184 kvm_put_kvm(matrix_mdev->kvm);
1185 matrix_mdev->kvm = NULL;
1186 matrix_mdev->kvm_busy = false;
1187 wake_up_all(&matrix_mdev->wait_for_kvm);
1191 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1192 unsigned long action, void *data)
1194 int notify_rc = NOTIFY_OK;
1195 struct ap_matrix_mdev *matrix_mdev;
1197 if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1200 mutex_lock(&matrix_dev->lock);
1201 matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1204 vfio_ap_mdev_unset_kvm(matrix_mdev);
1205 else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1206 notify_rc = NOTIFY_DONE;
1208 mutex_unlock(&matrix_dev->lock);
1213 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1216 struct vfio_ap_queue *q = NULL;
1218 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1221 q = dev_get_drvdata(dev);
1228 int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1231 struct ap_queue_status status;
1239 status = ap_zapq(q->apqn);
1240 switch (status.response_code) {
1241 case AP_RESPONSE_NORMAL:
1244 case AP_RESPONSE_RESET_IN_PROGRESS:
1251 case AP_RESPONSE_Q_NOT_AVAIL:
1252 case AP_RESPONSE_DECONFIGURED:
1253 case AP_RESPONSE_CHECKSTOPPED:
1254 WARN_ON_ONCE(status.irq_enabled);
1256 goto free_resources;
1258 /* things are really broken, give up */
1259 WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1260 status.response_code);
1264 /* wait for the reset to take effect */
1266 if (status.queue_empty && !status.irq_enabled)
1269 status = ap_tapq(q->apqn, NULL);
1271 WARN_ON_ONCE(retry2 <= 0);
1274 vfio_ap_free_aqic_resources(q);
1279 static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev)
1283 unsigned long apid, apqi;
1284 struct vfio_ap_queue *q;
1285 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1287 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1288 matrix_mdev->matrix.apm_max + 1) {
1289 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1290 matrix_mdev->matrix.aqm_max + 1) {
1291 q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1292 ret = vfio_ap_mdev_reset_queue(q, 1);
1294 * Regardless whether a queue turns out to be busy, or
1295 * is not operational, we need to continue resetting
1296 * the remaining queues.
1306 static int vfio_ap_mdev_open(struct mdev_device *mdev)
1308 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1309 unsigned long events;
1313 if (!try_module_get(THIS_MODULE))
1316 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1317 events = VFIO_GROUP_NOTIFY_SET_KVM;
1319 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1320 &events, &matrix_mdev->group_notifier);
1322 module_put(THIS_MODULE);
1326 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1327 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1328 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
1329 &events, &matrix_mdev->iommu_notifier);
1333 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1334 &matrix_mdev->group_notifier);
1335 module_put(THIS_MODULE);
1339 static void vfio_ap_mdev_release(struct mdev_device *mdev)
1341 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1343 mutex_lock(&matrix_dev->lock);
1344 vfio_ap_mdev_unset_kvm(matrix_mdev);
1345 mutex_unlock(&matrix_dev->lock);
1347 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
1348 &matrix_mdev->iommu_notifier);
1349 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1350 &matrix_mdev->group_notifier);
1351 module_put(THIS_MODULE);
1354 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1356 unsigned long minsz;
1357 struct vfio_device_info info;
1359 minsz = offsetofend(struct vfio_device_info, num_irqs);
1361 if (copy_from_user(&info, (void __user *)arg, minsz))
1364 if (info.argsz < minsz)
1367 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1368 info.num_regions = 0;
1371 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1374 static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev,
1375 unsigned int cmd, unsigned long arg)
1378 struct ap_matrix_mdev *matrix_mdev;
1380 mutex_lock(&matrix_dev->lock);
1382 case VFIO_DEVICE_GET_INFO:
1383 ret = vfio_ap_mdev_get_device_info(arg);
1385 case VFIO_DEVICE_RESET:
1386 matrix_mdev = mdev_get_drvdata(mdev);
1387 if (WARN(!matrix_mdev, "Driver data missing from mdev!!")) {
1393 * If the KVM pointer is in the process of being set, wait until
1394 * the process has completed.
1396 wait_event_cmd(matrix_mdev->wait_for_kvm,
1397 !matrix_mdev->kvm_busy,
1398 mutex_unlock(&matrix_dev->lock),
1399 mutex_lock(&matrix_dev->lock));
1401 ret = vfio_ap_mdev_reset_queues(mdev);
1407 mutex_unlock(&matrix_dev->lock);
1412 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1413 .owner = THIS_MODULE,
1414 .supported_type_groups = vfio_ap_mdev_type_groups,
1415 .mdev_attr_groups = vfio_ap_mdev_attr_groups,
1416 .create = vfio_ap_mdev_create,
1417 .remove = vfio_ap_mdev_remove,
1418 .open = vfio_ap_mdev_open,
1419 .release = vfio_ap_mdev_release,
1420 .ioctl = vfio_ap_mdev_ioctl,
1423 int vfio_ap_mdev_register(void)
1425 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1427 return mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1430 void vfio_ap_mdev_unregister(void)
1432 mdev_unregister_device(&matrix_dev->device);