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
190 * @isc: the guest ISC to register with the GIB interface
191 * @nib: the notification indicator byte to pin.
193 * Pin the NIB saved in *q
194 * Register the guest ISC to GIB interface and retrieve the
195 * host ISC to issue the host side PQAP/AQIC
197 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
198 * vfio_pin_pages failed.
200 * Otherwise return the ap_queue_status returned by the ap_aqic(),
201 * all retry handling will be done by the guest.
203 * Return: &struct ap_queue_status
205 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
209 struct ap_qirq_ctrl aqic_gisa = {};
210 struct ap_queue_status status = {};
211 struct kvm_s390_gisa *gisa;
213 unsigned long h_nib, g_pfn, h_pfn;
216 g_pfn = nib >> PAGE_SHIFT;
217 ret = vfio_pin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1,
218 IOMMU_READ | IOMMU_WRITE, &h_pfn);
223 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
227 kvm = q->matrix_mdev->kvm;
228 gisa = kvm->arch.gisa_int.origin;
230 h_nib = (h_pfn << PAGE_SHIFT) | (nib & ~PAGE_MASK);
231 aqic_gisa.gisc = isc;
232 aqic_gisa.isc = kvm_s390_gisc_register(kvm, isc);
234 aqic_gisa.gisa = (uint64_t)gisa >> 4;
236 status = ap_aqic(q->apqn, aqic_gisa, (void *)h_nib);
237 switch (status.response_code) {
238 case AP_RESPONSE_NORMAL:
239 /* See if we did clear older IRQ configuration */
240 vfio_ap_free_aqic_resources(q);
241 q->saved_pfn = g_pfn;
244 case AP_RESPONSE_OTHERWISE_CHANGED:
245 /* We could not modify IRQ setings: clear new configuration */
246 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1);
247 kvm_s390_gisc_unregister(kvm, isc);
250 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
251 status.response_code);
252 vfio_ap_irq_disable(q);
260 * handle_pqap - PQAP instruction callback
262 * @vcpu: The vcpu on which we received the PQAP instruction
264 * Get the general register contents to initialize internal variables.
269 * Response.status may be set to following Response Code:
270 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
271 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
272 * - AP_RESPONSE_NORMAL (0) : in case of successs
273 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
274 * We take the matrix_dev lock to ensure serialization on queues and
275 * mediated device access.
277 * Return: 0 if we could handle the request inside KVM.
278 * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
280 static int handle_pqap(struct kvm_vcpu *vcpu)
284 struct vfio_ap_queue *q;
285 struct ap_queue_status qstatus = {
286 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
287 struct ap_matrix_mdev *matrix_mdev;
289 /* If we do not use the AIV facility just go to userland */
290 if (!(vcpu->arch.sie_block->eca & ECA_AIV))
293 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
294 mutex_lock(&matrix_dev->lock);
296 if (!vcpu->kvm->arch.crypto.pqap_hook)
298 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
299 struct ap_matrix_mdev, pqap_hook);
301 /* If the there is no guest using the mdev, there is nothing to do */
302 if (!matrix_mdev->kvm)
305 q = vfio_ap_get_queue(matrix_mdev, apqn);
309 status = vcpu->run->s.regs.gprs[1];
311 /* If IR bit(16) is set we enable the interrupt */
312 if ((status >> (63 - 16)) & 0x01)
313 qstatus = vfio_ap_irq_enable(q, status & 0x07,
314 vcpu->run->s.regs.gprs[2]);
316 qstatus = vfio_ap_irq_disable(q);
319 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
320 vcpu->run->s.regs.gprs[1] >>= 32;
321 mutex_unlock(&matrix_dev->lock);
325 static void vfio_ap_matrix_init(struct ap_config_info *info,
326 struct ap_matrix *matrix)
328 matrix->apm_max = info->apxa ? info->Na : 63;
329 matrix->aqm_max = info->apxa ? info->Nd : 15;
330 matrix->adm_max = info->apxa ? info->Nd : 15;
333 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
335 struct ap_matrix_mdev *matrix_mdev;
338 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
341 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
344 goto err_dec_available;
346 vfio_init_group_dev(&matrix_mdev->vdev, &mdev->dev,
347 &vfio_ap_matrix_dev_ops);
349 matrix_mdev->mdev = mdev;
350 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
351 matrix_mdev->pqap_hook = handle_pqap;
352 mutex_lock(&matrix_dev->lock);
353 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
354 mutex_unlock(&matrix_dev->lock);
356 ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
359 dev_set_drvdata(&mdev->dev, matrix_mdev);
363 mutex_lock(&matrix_dev->lock);
364 list_del(&matrix_mdev->node);
365 mutex_unlock(&matrix_dev->lock);
366 vfio_uninit_group_dev(&matrix_mdev->vdev);
369 atomic_inc(&matrix_dev->available_instances);
373 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
375 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
377 vfio_unregister_group_dev(&matrix_mdev->vdev);
379 mutex_lock(&matrix_dev->lock);
380 vfio_ap_mdev_reset_queues(matrix_mdev);
381 list_del(&matrix_mdev->node);
382 mutex_unlock(&matrix_dev->lock);
383 vfio_uninit_group_dev(&matrix_mdev->vdev);
385 atomic_inc(&matrix_dev->available_instances);
388 static ssize_t name_show(struct mdev_type *mtype,
389 struct mdev_type_attribute *attr, char *buf)
391 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
394 static MDEV_TYPE_ATTR_RO(name);
396 static ssize_t available_instances_show(struct mdev_type *mtype,
397 struct mdev_type_attribute *attr,
400 return sprintf(buf, "%d\n",
401 atomic_read(&matrix_dev->available_instances));
404 static MDEV_TYPE_ATTR_RO(available_instances);
406 static ssize_t device_api_show(struct mdev_type *mtype,
407 struct mdev_type_attribute *attr, char *buf)
409 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
412 static MDEV_TYPE_ATTR_RO(device_api);
414 static struct attribute *vfio_ap_mdev_type_attrs[] = {
415 &mdev_type_attr_name.attr,
416 &mdev_type_attr_device_api.attr,
417 &mdev_type_attr_available_instances.attr,
421 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
422 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
423 .attrs = vfio_ap_mdev_type_attrs,
426 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
427 &vfio_ap_mdev_hwvirt_type_group,
431 struct vfio_ap_queue_reserved {
438 * vfio_ap_has_queue - determines if the AP queue containing the target in @data
440 * @dev: an AP queue device
441 * @data: a struct vfio_ap_queue_reserved reference
443 * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
444 * apid or apqi specified in @data:
446 * - If @data contains both an apid and apqi value, then @data will be flagged
447 * as reserved if the APID and APQI fields for the AP queue device matches
449 * - If @data contains only an apid value, @data will be flagged as
450 * reserved if the APID field in the AP queue device matches
452 * - If @data contains only an apqi value, @data will be flagged as
453 * reserved if the APQI field in the AP queue device matches
455 * Return: 0 to indicate the input to function succeeded. Returns -EINVAL if
456 * @data does not contain either an apid or apqi.
458 static int vfio_ap_has_queue(struct device *dev, void *data)
460 struct vfio_ap_queue_reserved *qres = data;
461 struct ap_queue *ap_queue = to_ap_queue(dev);
465 if (qres->apid && qres->apqi) {
466 qid = AP_MKQID(*qres->apid, *qres->apqi);
467 if (qid == ap_queue->qid)
468 qres->reserved = true;
469 } else if (qres->apid && !qres->apqi) {
470 id = AP_QID_CARD(ap_queue->qid);
471 if (id == *qres->apid)
472 qres->reserved = true;
473 } else if (!qres->apid && qres->apqi) {
474 id = AP_QID_QUEUE(ap_queue->qid);
475 if (id == *qres->apqi)
476 qres->reserved = true;
485 * vfio_ap_verify_queue_reserved - verifies that the AP queue containing
486 * @apid or @aqpi is reserved
488 * @apid: an AP adapter ID
489 * @apqi: an AP queue index
491 * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
492 * driver according to the following rules:
494 * - If both @apid and @apqi are not NULL, then there must be an AP queue
495 * device bound to the vfio_ap driver with the APQN identified by @apid and
498 * - If only @apid is not NULL, then there must be an AP queue device bound
499 * to the vfio_ap driver with an APQN containing @apid
501 * - If only @apqi is not NULL, then there must be an AP queue device bound
502 * to the vfio_ap driver with an APQN containing @apqi
504 * Return: 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
506 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
510 struct vfio_ap_queue_reserved qres;
514 qres.reserved = false;
516 ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
517 &qres, vfio_ap_has_queue);
524 return -EADDRNOTAVAIL;
528 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
533 unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
535 if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
536 return vfio_ap_verify_queue_reserved(&apid, NULL);
538 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
539 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
548 * vfio_ap_mdev_verify_no_sharing - verifies that the AP matrix is not configured
550 * @matrix_mdev: the mediated matrix device
552 * Verifies that the APQNs derived from the cross product of the AP adapter IDs
553 * and AP queue indexes comprising the AP matrix are not configured for another
554 * mediated device. AP queue sharing is not allowed.
556 * Return: 0 if the APQNs are not shared; otherwise returns -EADDRINUSE.
558 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
560 struct ap_matrix_mdev *lstdev;
561 DECLARE_BITMAP(apm, AP_DEVICES);
562 DECLARE_BITMAP(aqm, AP_DOMAINS);
564 list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
565 if (matrix_mdev == lstdev)
568 memset(apm, 0, sizeof(apm));
569 memset(aqm, 0, sizeof(aqm));
572 * We work on full longs, as we can only exclude the leftover
573 * bits in non-inverse order. The leftover is all zeros.
575 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
576 lstdev->matrix.apm, AP_DEVICES))
579 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
580 lstdev->matrix.aqm, AP_DOMAINS))
590 * assign_adapter_store - parses the APID from @buf and sets the
591 * corresponding bit in the mediated matrix device's APM
593 * @dev: the matrix device
594 * @attr: the mediated matrix device's assign_adapter attribute
595 * @buf: a buffer containing the AP adapter number (APID) to
597 * @count: the number of bytes in @buf
599 * Return: the number of bytes processed if the APID is valid; otherwise,
600 * returns one of the following errors:
603 * The APID is not a valid number
606 * The APID exceeds the maximum value configured for the system
609 * An APQN derived from the cross product of the APID being assigned
610 * and the APQIs previously assigned is not bound to the vfio_ap device
611 * driver; or, if no APQIs have yet been assigned, the APID is not
612 * contained in an APQN bound to the vfio_ap device driver.
615 * An APQN derived from the cross product of the APID being assigned
616 * and the APQIs previously assigned is being used by another mediated
619 static ssize_t assign_adapter_store(struct device *dev,
620 struct device_attribute *attr,
621 const char *buf, size_t count)
625 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
627 mutex_lock(&matrix_dev->lock);
629 /* If the KVM guest is running, disallow assignment of adapter */
630 if (matrix_mdev->kvm) {
635 ret = kstrtoul(buf, 0, &apid);
639 if (apid > matrix_mdev->matrix.apm_max) {
645 * Set the bit in the AP mask (APM) corresponding to the AP adapter
646 * number (APID). The bits in the mask, from most significant to least
647 * significant bit, correspond to APIDs 0-255.
649 ret = vfio_ap_mdev_verify_queues_reserved_for_apid(matrix_mdev, apid);
653 set_bit_inv(apid, matrix_mdev->matrix.apm);
655 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
663 clear_bit_inv(apid, matrix_mdev->matrix.apm);
665 mutex_unlock(&matrix_dev->lock);
669 static DEVICE_ATTR_WO(assign_adapter);
672 * unassign_adapter_store - parses the APID from @buf and clears the
673 * corresponding bit in the mediated matrix device's APM
675 * @dev: the matrix device
676 * @attr: the mediated matrix device's unassign_adapter attribute
677 * @buf: a buffer containing the adapter number (APID) to be unassigned
678 * @count: the number of bytes in @buf
680 * Return: the number of bytes processed if the APID is valid; otherwise,
681 * returns one of the following errors:
682 * -EINVAL if the APID is not a number
683 * -ENODEV if the APID it exceeds the maximum value configured for the
686 static ssize_t unassign_adapter_store(struct device *dev,
687 struct device_attribute *attr,
688 const char *buf, size_t count)
692 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
694 mutex_lock(&matrix_dev->lock);
696 /* If the KVM guest is running, disallow unassignment of adapter */
697 if (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
743 * @dev: the matrix device
744 * @attr: the mediated matrix device's assign_domain attribute
745 * @buf: a buffer containing the AP queue index (APQI) of the domain to
747 * @count: the number of bytes in @buf
749 * Return: the number of bytes processed if the APQI is valid; otherwise returns
750 * one of the following errors:
753 * The APQI is not a valid number
756 * The APQI exceeds the maximum value configured for the system
759 * An APQN derived from the cross product of the APQI being assigned
760 * and the APIDs previously assigned is not bound to the vfio_ap device
761 * driver; or, if no APIDs have yet been assigned, the APQI is not
762 * contained in an APQN bound to the vfio_ap device driver.
765 * An APQN derived from the cross product of the APQI being assigned
766 * and the APIDs previously assigned is being used by another mediated
769 static ssize_t assign_domain_store(struct device *dev,
770 struct device_attribute *attr,
771 const char *buf, size_t count)
775 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
776 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
778 mutex_lock(&matrix_dev->lock);
780 /* If the KVM guest is running, disallow assignment of domain */
781 if (matrix_mdev->kvm) {
786 ret = kstrtoul(buf, 0, &apqi);
789 if (apqi > max_apqi) {
794 ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
798 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
800 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
808 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
810 mutex_unlock(&matrix_dev->lock);
814 static DEVICE_ATTR_WO(assign_domain);
818 * unassign_domain_store - parses the APQI from @buf and clears the
819 * corresponding bit in the mediated matrix device's AQM
821 * @dev: the matrix device
822 * @attr: the mediated matrix device's unassign_domain attribute
823 * @buf: a buffer containing the AP queue index (APQI) of the domain to
825 * @count: the number of bytes in @buf
827 * Return: the number of bytes processed if the APQI is valid; otherwise,
828 * returns one of the following errors:
829 * -EINVAL if the APQI is not a number
830 * -ENODEV if the APQI exceeds the maximum value configured for the system
832 static ssize_t unassign_domain_store(struct device *dev,
833 struct device_attribute *attr,
834 const char *buf, size_t count)
838 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
840 mutex_lock(&matrix_dev->lock);
842 /* If the KVM guest is running, disallow unassignment of domain */
843 if (matrix_mdev->kvm) {
848 ret = kstrtoul(buf, 0, &apqi);
852 if (apqi > matrix_mdev->matrix.aqm_max) {
857 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
861 mutex_unlock(&matrix_dev->lock);
864 static DEVICE_ATTR_WO(unassign_domain);
867 * assign_control_domain_store - parses the domain ID from @buf and sets
868 * 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
1145 * @kvm: the pointer to the kvm structure being unset.
1147 * Note: The matrix_dev->lock must be taken prior to calling
1148 * this function; however, the lock will be temporarily released while the
1149 * guest's AP configuration is cleared to avoid a potential lockdep splat.
1150 * The kvm->lock is taken to clear the guest's AP configuration which, under
1151 * certain circumstances, will result in a circular lock dependency if this is
1152 * done under the @matrix_mdev->lock.
1154 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev,
1157 if (kvm && kvm->arch.crypto.crycbd) {
1158 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1159 kvm->arch.crypto.pqap_hook = NULL;
1160 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1162 mutex_lock(&kvm->lock);
1163 mutex_lock(&matrix_dev->lock);
1165 kvm_arch_crypto_clear_masks(kvm);
1166 vfio_ap_mdev_reset_queues(matrix_mdev);
1168 matrix_mdev->kvm = NULL;
1170 mutex_unlock(&kvm->lock);
1171 mutex_unlock(&matrix_dev->lock);
1175 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1176 unsigned long action, void *data)
1178 int notify_rc = NOTIFY_OK;
1179 struct ap_matrix_mdev *matrix_mdev;
1181 if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1184 matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1187 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1188 else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1189 notify_rc = NOTIFY_DONE;
1194 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1197 struct vfio_ap_queue *q = NULL;
1199 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1202 q = dev_get_drvdata(dev);
1209 int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1212 struct ap_queue_status status;
1220 status = ap_zapq(q->apqn);
1221 switch (status.response_code) {
1222 case AP_RESPONSE_NORMAL:
1225 case AP_RESPONSE_RESET_IN_PROGRESS:
1232 case AP_RESPONSE_Q_NOT_AVAIL:
1233 case AP_RESPONSE_DECONFIGURED:
1234 case AP_RESPONSE_CHECKSTOPPED:
1235 WARN_ON_ONCE(status.irq_enabled);
1237 goto free_resources;
1239 /* things are really broken, give up */
1240 WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1241 status.response_code);
1245 /* wait for the reset to take effect */
1247 if (status.queue_empty && !status.irq_enabled)
1250 status = ap_tapq(q->apqn, NULL);
1252 WARN_ON_ONCE(retry2 <= 0);
1255 vfio_ap_free_aqic_resources(q);
1260 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1264 unsigned long apid, apqi;
1265 struct vfio_ap_queue *q;
1267 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1268 matrix_mdev->matrix.apm_max + 1) {
1269 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1270 matrix_mdev->matrix.aqm_max + 1) {
1271 q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1272 ret = vfio_ap_mdev_reset_queue(q, 1);
1274 * Regardless whether a queue turns out to be busy, or
1275 * is not operational, we need to continue resetting
1276 * the remaining queues.
1286 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1288 struct ap_matrix_mdev *matrix_mdev =
1289 container_of(vdev, struct ap_matrix_mdev, vdev);
1290 unsigned long events;
1293 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1294 events = VFIO_GROUP_NOTIFY_SET_KVM;
1296 ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1297 &events, &matrix_mdev->group_notifier);
1301 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1302 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1303 ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1304 &events, &matrix_mdev->iommu_notifier);
1306 goto out_unregister_group;
1309 out_unregister_group:
1310 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1311 &matrix_mdev->group_notifier);
1315 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1317 struct ap_matrix_mdev *matrix_mdev =
1318 container_of(vdev, struct ap_matrix_mdev, vdev);
1320 vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1321 &matrix_mdev->iommu_notifier);
1322 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1323 &matrix_mdev->group_notifier);
1324 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1327 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1329 unsigned long minsz;
1330 struct vfio_device_info info;
1332 minsz = offsetofend(struct vfio_device_info, num_irqs);
1334 if (copy_from_user(&info, (void __user *)arg, minsz))
1337 if (info.argsz < minsz)
1340 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1341 info.num_regions = 0;
1344 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1347 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1348 unsigned int cmd, unsigned long arg)
1350 struct ap_matrix_mdev *matrix_mdev =
1351 container_of(vdev, struct ap_matrix_mdev, vdev);
1354 mutex_lock(&matrix_dev->lock);
1356 case VFIO_DEVICE_GET_INFO:
1357 ret = vfio_ap_mdev_get_device_info(arg);
1359 case VFIO_DEVICE_RESET:
1360 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
1366 mutex_unlock(&matrix_dev->lock);
1371 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1372 .open_device = vfio_ap_mdev_open_device,
1373 .close_device = vfio_ap_mdev_close_device,
1374 .ioctl = vfio_ap_mdev_ioctl,
1377 static struct mdev_driver vfio_ap_matrix_driver = {
1379 .name = "vfio_ap_mdev",
1380 .owner = THIS_MODULE,
1381 .mod_name = KBUILD_MODNAME,
1382 .dev_groups = vfio_ap_mdev_attr_groups,
1384 .probe = vfio_ap_mdev_probe,
1385 .remove = vfio_ap_mdev_remove,
1388 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1389 .owner = THIS_MODULE,
1390 .device_driver = &vfio_ap_matrix_driver,
1391 .supported_type_groups = vfio_ap_mdev_type_groups,
1394 int vfio_ap_mdev_register(void)
1398 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1400 ret = mdev_register_driver(&vfio_ap_matrix_driver);
1404 ret = mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1410 mdev_unregister_driver(&vfio_ap_matrix_driver);
1414 void vfio_ap_mdev_unregister(void)
1416 mdev_unregister_device(&matrix_dev->device);
1417 mdev_unregister_driver(&vfio_ap_matrix_driver);