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 * Returns 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
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.
78 static void vfio_ap_wait_for_irqclear(int apqn)
80 struct ap_queue_status status;
84 status = ap_tapq(apqn, NULL);
85 switch (status.response_code) {
86 case AP_RESPONSE_NORMAL:
87 case AP_RESPONSE_RESET_IN_PROGRESS:
88 if (!status.irq_enabled)
91 case AP_RESPONSE_BUSY:
94 case AP_RESPONSE_Q_NOT_AVAIL:
95 case AP_RESPONSE_DECONFIGURED:
96 case AP_RESPONSE_CHECKSTOPPED:
98 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
99 status.response_code, apqn);
104 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
105 __func__, status.response_code, apqn);
109 * vfio_ap_free_aqic_resources
110 * @q: The vfio_ap_queue
112 * Unregisters the ISC in the GIB when the saved ISC not invalid.
113 * Unpin the guest's page holding the NIB when it exist.
114 * Reset the saved_pfn and saved_isc to invalid values.
117 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
121 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
122 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
123 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
124 q->saved_isc = VFIO_AP_ISC_INVALID;
126 if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) {
127 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev),
134 * vfio_ap_irq_disable
135 * @q: The vfio_ap_queue
137 * Uses ap_aqic to disable the interruption and in case of success, reset
138 * in progress or IRQ disable command already proceeded: calls
139 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
140 * and calls vfio_ap_free_aqic_resources() to free the resources associated
141 * with the AP interrupt handling.
143 * In the case the AP is busy, or a reset is in progress,
144 * retries after 20ms, up to 5 times.
146 * Returns if ap_aqic function failed with invalid, deconfigured or
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_setirq: Enable Interruption for a APQN
189 * @dev: the device associated with the ap_queue
190 * @q: the vfio_ap_queue holding AQIC parameters
192 * Pin the NIB saved in *q
193 * Register the guest ISC to GIB interface and retrieve the
194 * host ISC to issue the host side PQAP/AQIC
196 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
197 * vfio_pin_pages failed.
199 * Otherwise return the ap_queue_status returned by the ap_aqic(),
200 * all retry handling will be done by the guest.
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);
298 /* If the there is no guest using the mdev, there is nothing to do */
299 if (!matrix_mdev->kvm)
302 q = vfio_ap_get_queue(matrix_mdev, apqn);
306 status = vcpu->run->s.regs.gprs[1];
308 /* If IR bit(16) is set we enable the interrupt */
309 if ((status >> (63 - 16)) & 0x01)
310 qstatus = vfio_ap_irq_enable(q, status & 0x07,
311 vcpu->run->s.regs.gprs[2]);
313 qstatus = vfio_ap_irq_disable(q);
316 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
317 vcpu->run->s.regs.gprs[1] >>= 32;
318 mutex_unlock(&matrix_dev->lock);
322 static void vfio_ap_matrix_init(struct ap_config_info *info,
323 struct ap_matrix *matrix)
325 matrix->apm_max = info->apxa ? info->Na : 63;
326 matrix->aqm_max = info->apxa ? info->Nd : 15;
327 matrix->adm_max = info->apxa ? info->Nd : 15;
330 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
332 struct ap_matrix_mdev *matrix_mdev;
335 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
338 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
341 goto err_dec_available;
343 vfio_init_group_dev(&matrix_mdev->vdev, &mdev->dev,
344 &vfio_ap_matrix_dev_ops);
346 matrix_mdev->mdev = mdev;
347 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
348 matrix_mdev->pqap_hook = handle_pqap;
349 mutex_lock(&matrix_dev->lock);
350 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
351 mutex_unlock(&matrix_dev->lock);
353 ret = vfio_register_group_dev(&matrix_mdev->vdev);
356 dev_set_drvdata(&mdev->dev, matrix_mdev);
360 mutex_lock(&matrix_dev->lock);
361 list_del(&matrix_mdev->node);
362 mutex_unlock(&matrix_dev->lock);
365 atomic_inc(&matrix_dev->available_instances);
369 static void vfio_ap_mdev_remove(struct mdev_device *mdev)
371 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
373 vfio_unregister_group_dev(&matrix_mdev->vdev);
375 mutex_lock(&matrix_dev->lock);
376 vfio_ap_mdev_reset_queues(matrix_mdev);
377 list_del(&matrix_mdev->node);
379 atomic_inc(&matrix_dev->available_instances);
380 mutex_unlock(&matrix_dev->lock);
383 static ssize_t name_show(struct mdev_type *mtype,
384 struct mdev_type_attribute *attr, char *buf)
386 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
389 static MDEV_TYPE_ATTR_RO(name);
391 static ssize_t available_instances_show(struct mdev_type *mtype,
392 struct mdev_type_attribute *attr,
395 return sprintf(buf, "%d\n",
396 atomic_read(&matrix_dev->available_instances));
399 static MDEV_TYPE_ATTR_RO(available_instances);
401 static ssize_t device_api_show(struct mdev_type *mtype,
402 struct mdev_type_attribute *attr, char *buf)
404 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
407 static MDEV_TYPE_ATTR_RO(device_api);
409 static struct attribute *vfio_ap_mdev_type_attrs[] = {
410 &mdev_type_attr_name.attr,
411 &mdev_type_attr_device_api.attr,
412 &mdev_type_attr_available_instances.attr,
416 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
417 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
418 .attrs = vfio_ap_mdev_type_attrs,
421 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
422 &vfio_ap_mdev_hwvirt_type_group,
426 struct vfio_ap_queue_reserved {
435 * @dev: an AP queue device
436 * @data: a struct vfio_ap_queue_reserved reference
438 * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
439 * apid or apqi specified in @data:
441 * - If @data contains both an apid and apqi value, then @data will be flagged
442 * as reserved if the APID and APQI fields for the AP queue device matches
444 * - If @data contains only an apid value, @data will be flagged as
445 * reserved if the APID field in the AP queue device matches
447 * - If @data contains only an apqi value, @data will be flagged as
448 * reserved if the APQI field in the AP queue device matches
450 * Returns 0 to indicate the input to function succeeded. Returns -EINVAL if
451 * @data does not contain either an apid or apqi.
453 static int vfio_ap_has_queue(struct device *dev, void *data)
455 struct vfio_ap_queue_reserved *qres = data;
456 struct ap_queue *ap_queue = to_ap_queue(dev);
460 if (qres->apid && qres->apqi) {
461 qid = AP_MKQID(*qres->apid, *qres->apqi);
462 if (qid == ap_queue->qid)
463 qres->reserved = true;
464 } else if (qres->apid && !qres->apqi) {
465 id = AP_QID_CARD(ap_queue->qid);
466 if (id == *qres->apid)
467 qres->reserved = true;
468 } else if (!qres->apid && qres->apqi) {
469 id = AP_QID_QUEUE(ap_queue->qid);
470 if (id == *qres->apqi)
471 qres->reserved = true;
480 * vfio_ap_verify_queue_reserved
482 * @matrix_dev: a mediated matrix device
483 * @apid: an AP adapter ID
484 * @apqi: an AP queue index
486 * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
487 * driver according to the following rules:
489 * - If both @apid and @apqi are not NULL, then there must be an AP queue
490 * device bound to the vfio_ap driver with the APQN identified by @apid and
493 * - If only @apid is not NULL, then there must be an AP queue device bound
494 * to the vfio_ap driver with an APQN containing @apid
496 * - If only @apqi is not NULL, then there must be an AP queue device bound
497 * to the vfio_ap driver with an APQN containing @apqi
499 * Returns 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
501 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
505 struct vfio_ap_queue_reserved qres;
509 qres.reserved = false;
511 ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
512 &qres, vfio_ap_has_queue);
519 return -EADDRNOTAVAIL;
523 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
528 unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
530 if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
531 return vfio_ap_verify_queue_reserved(&apid, NULL);
533 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
534 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
543 * vfio_ap_mdev_verify_no_sharing
545 * Verifies that the APQNs derived from the cross product of the AP adapter IDs
546 * and AP queue indexes comprising the AP matrix are not configured for another
547 * mediated device. AP queue sharing is not allowed.
549 * @matrix_mdev: the mediated matrix device
551 * Returns 0 if the APQNs are not shared, otherwise; returns -EADDRINUSE.
553 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
555 struct ap_matrix_mdev *lstdev;
556 DECLARE_BITMAP(apm, AP_DEVICES);
557 DECLARE_BITMAP(aqm, AP_DOMAINS);
559 list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
560 if (matrix_mdev == lstdev)
563 memset(apm, 0, sizeof(apm));
564 memset(aqm, 0, sizeof(aqm));
567 * We work on full longs, as we can only exclude the leftover
568 * bits in non-inverse order. The leftover is all zeros.
570 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
571 lstdev->matrix.apm, AP_DEVICES))
574 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
575 lstdev->matrix.aqm, AP_DOMAINS))
585 * assign_adapter_store
587 * @dev: the matrix device
588 * @attr: the mediated matrix device's assign_adapter attribute
589 * @buf: a buffer containing the AP adapter number (APID) to
591 * @count: the number of bytes in @buf
593 * Parses the APID from @buf and sets the corresponding bit in the mediated
594 * matrix device's APM.
596 * Returns the number of bytes processed if the APID is valid; otherwise,
597 * returns one of the following errors:
600 * The APID is not a valid number
603 * The APID exceeds the maximum value configured for the system
606 * An APQN derived from the cross product of the APID being assigned
607 * and the APQIs previously assigned is not bound to the vfio_ap device
608 * driver; or, if no APQIs have yet been assigned, the APID is not
609 * contained in an APQN bound to the vfio_ap device driver.
612 * An APQN derived from the cross product of the APID being assigned
613 * and the APQIs previously assigned is being used by another mediated
616 static ssize_t assign_adapter_store(struct device *dev,
617 struct device_attribute *attr,
618 const char *buf, size_t count)
622 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
624 mutex_lock(&matrix_dev->lock);
626 /* If the KVM guest is running, disallow assignment of adapter */
627 if (matrix_mdev->kvm) {
632 ret = kstrtoul(buf, 0, &apid);
636 if (apid > matrix_mdev->matrix.apm_max) {
642 * Set the bit in the AP mask (APM) corresponding to the AP adapter
643 * number (APID). The bits in the mask, from most significant to least
644 * significant bit, correspond to APIDs 0-255.
646 ret = vfio_ap_mdev_verify_queues_reserved_for_apid(matrix_mdev, apid);
650 set_bit_inv(apid, matrix_mdev->matrix.apm);
652 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
660 clear_bit_inv(apid, matrix_mdev->matrix.apm);
662 mutex_unlock(&matrix_dev->lock);
666 static DEVICE_ATTR_WO(assign_adapter);
669 * unassign_adapter_store
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 * Parses the APID from @buf and clears the corresponding bit in the mediated
677 * matrix device's APM.
679 * Returns the number of bytes processed if the APID is valid; otherwise,
680 * returns one of the following errors:
681 * -EINVAL if the APID is not a number
682 * -ENODEV if the APID it exceeds the maximum value configured for the
685 static ssize_t unassign_adapter_store(struct device *dev,
686 struct device_attribute *attr,
687 const char *buf, size_t count)
691 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
693 mutex_lock(&matrix_dev->lock);
695 /* If the KVM guest is running, disallow unassignment of adapter */
696 if (matrix_mdev->kvm) {
701 ret = kstrtoul(buf, 0, &apid);
705 if (apid > matrix_mdev->matrix.apm_max) {
710 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
713 mutex_unlock(&matrix_dev->lock);
716 static DEVICE_ATTR_WO(unassign_adapter);
719 vfio_ap_mdev_verify_queues_reserved_for_apqi(struct ap_matrix_mdev *matrix_mdev,
724 unsigned long nbits = matrix_mdev->matrix.apm_max + 1;
726 if (find_first_bit_inv(matrix_mdev->matrix.apm, nbits) >= nbits)
727 return vfio_ap_verify_queue_reserved(NULL, &apqi);
729 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, nbits) {
730 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
739 * assign_domain_store
741 * @dev: the matrix device
742 * @attr: the mediated matrix device's assign_domain attribute
743 * @buf: a buffer containing the AP queue index (APQI) of the domain to
745 * @count: the number of bytes in @buf
747 * Parses the APQI from @buf and sets the corresponding bit in the mediated
748 * matrix device's AQM.
750 * Returns 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 ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
777 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
779 mutex_lock(&matrix_dev->lock);
781 /* If the KVM guest is running, disallow assignment of domain */
782 if (matrix_mdev->kvm) {
787 ret = kstrtoul(buf, 0, &apqi);
790 if (apqi > max_apqi) {
795 ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
799 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
801 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
809 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
811 mutex_unlock(&matrix_dev->lock);
815 static DEVICE_ATTR_WO(assign_domain);
819 * unassign_domain_store
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 * Parses the APQI from @buf and clears the corresponding bit in the
828 * mediated matrix device's AQM.
830 * Returns the number of bytes processed if the APQI is valid; otherwise,
831 * returns one of the following errors:
832 * -EINVAL if the APQI is not a number
833 * -ENODEV if the APQI exceeds the maximum value configured for the system
835 static ssize_t unassign_domain_store(struct device *dev,
836 struct device_attribute *attr,
837 const char *buf, size_t count)
841 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
843 mutex_lock(&matrix_dev->lock);
845 /* If the KVM guest is running, disallow unassignment of domain */
846 if (matrix_mdev->kvm) {
851 ret = kstrtoul(buf, 0, &apqi);
855 if (apqi > matrix_mdev->matrix.aqm_max) {
860 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
864 mutex_unlock(&matrix_dev->lock);
867 static DEVICE_ATTR_WO(unassign_domain);
870 * assign_control_domain_store
872 * @dev: the matrix device
873 * @attr: the mediated matrix device's assign_control_domain attribute
874 * @buf: a buffer containing the domain ID to be assigned
875 * @count: the number of bytes in @buf
877 * Parses the domain ID from @buf and sets the corresponding bit in the mediated
878 * matrix device's ADM.
880 * Returns the number of bytes processed if the domain ID is valid; otherwise,
881 * returns one of the following errors:
882 * -EINVAL if the ID is not a number
883 * -ENODEV if the ID exceeds the maximum value configured for the system
885 static ssize_t assign_control_domain_store(struct device *dev,
886 struct device_attribute *attr,
887 const char *buf, size_t count)
891 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
893 mutex_lock(&matrix_dev->lock);
895 /* If the KVM guest is running, disallow assignment of control domain */
896 if (matrix_mdev->kvm) {
901 ret = kstrtoul(buf, 0, &id);
905 if (id > matrix_mdev->matrix.adm_max) {
910 /* Set the bit in the ADM (bitmask) corresponding to the AP control
911 * domain number (id). The bits in the mask, from most significant to
912 * least significant, correspond to IDs 0 up to the one less than the
913 * number of control domains that can be assigned.
915 set_bit_inv(id, matrix_mdev->matrix.adm);
918 mutex_unlock(&matrix_dev->lock);
921 static DEVICE_ATTR_WO(assign_control_domain);
924 * unassign_control_domain_store
926 * @dev: the matrix device
927 * @attr: the mediated matrix device's unassign_control_domain attribute
928 * @buf: a buffer containing the domain ID to be unassigned
929 * @count: the number of bytes in @buf
931 * Parses the domain ID from @buf and clears the corresponding bit in the
932 * mediated matrix device's ADM.
934 * Returns the number of bytes processed if the domain ID is valid; otherwise,
935 * returns one of the following errors:
936 * -EINVAL if the ID is not a number
937 * -ENODEV if the ID exceeds the maximum value configured for the system
939 static ssize_t unassign_control_domain_store(struct device *dev,
940 struct device_attribute *attr,
941 const char *buf, size_t count)
945 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
946 unsigned long max_domid = matrix_mdev->matrix.adm_max;
948 mutex_lock(&matrix_dev->lock);
950 /* If a KVM guest is running, disallow unassignment of control domain */
951 if (matrix_mdev->kvm) {
956 ret = kstrtoul(buf, 0, &domid);
959 if (domid > max_domid) {
964 clear_bit_inv(domid, matrix_mdev->matrix.adm);
967 mutex_unlock(&matrix_dev->lock);
970 static DEVICE_ATTR_WO(unassign_control_domain);
972 static ssize_t control_domains_show(struct device *dev,
973 struct device_attribute *dev_attr,
980 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
981 unsigned long max_domid = matrix_mdev->matrix.adm_max;
983 mutex_lock(&matrix_dev->lock);
984 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
985 n = sprintf(bufpos, "%04lx\n", id);
989 mutex_unlock(&matrix_dev->lock);
993 static DEVICE_ATTR_RO(control_domains);
995 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
998 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
1002 unsigned long apid1;
1003 unsigned long apqi1;
1004 unsigned long napm_bits = matrix_mdev->matrix.apm_max + 1;
1005 unsigned long naqm_bits = matrix_mdev->matrix.aqm_max + 1;
1009 apid1 = find_first_bit_inv(matrix_mdev->matrix.apm, napm_bits);
1010 apqi1 = find_first_bit_inv(matrix_mdev->matrix.aqm, naqm_bits);
1012 mutex_lock(&matrix_dev->lock);
1014 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1015 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1016 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1018 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1024 } else if (apid1 < napm_bits) {
1025 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1026 n = sprintf(bufpos, "%02lx.\n", apid);
1030 } else if (apqi1 < naqm_bits) {
1031 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, naqm_bits) {
1032 n = sprintf(bufpos, ".%04lx\n", apqi);
1038 mutex_unlock(&matrix_dev->lock);
1042 static DEVICE_ATTR_RO(matrix);
1044 static struct attribute *vfio_ap_mdev_attrs[] = {
1045 &dev_attr_assign_adapter.attr,
1046 &dev_attr_unassign_adapter.attr,
1047 &dev_attr_assign_domain.attr,
1048 &dev_attr_unassign_domain.attr,
1049 &dev_attr_assign_control_domain.attr,
1050 &dev_attr_unassign_control_domain.attr,
1051 &dev_attr_control_domains.attr,
1052 &dev_attr_matrix.attr,
1056 static struct attribute_group vfio_ap_mdev_attr_group = {
1057 .attrs = vfio_ap_mdev_attrs
1060 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1061 &vfio_ap_mdev_attr_group,
1066 * vfio_ap_mdev_set_kvm
1068 * @matrix_mdev: a mediated matrix device
1069 * @kvm: reference to KVM instance
1071 * Sets all data for @matrix_mdev that are needed to manage AP resources
1072 * for the guest whose state is represented by @kvm.
1074 * Note: The matrix_dev->lock must be taken prior to calling
1075 * this function; however, the lock will be temporarily released while the
1076 * guest's AP configuration is set to avoid a potential lockdep splat.
1077 * The kvm->lock is taken to set the guest's AP configuration which, under
1078 * certain circumstances, will result in a circular lock dependency if this is
1079 * done under the @matrix_mdev->lock.
1081 * Return 0 if no other mediated matrix device has a reference to @kvm;
1082 * otherwise, returns an -EPERM.
1084 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1087 struct ap_matrix_mdev *m;
1089 if (kvm->arch.crypto.crycbd) {
1090 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1091 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1092 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1094 mutex_lock(&kvm->lock);
1095 mutex_lock(&matrix_dev->lock);
1097 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1098 if (m != matrix_mdev && m->kvm == kvm) {
1099 mutex_unlock(&kvm->lock);
1100 mutex_unlock(&matrix_dev->lock);
1106 matrix_mdev->kvm = kvm;
1107 kvm_arch_crypto_set_masks(kvm,
1108 matrix_mdev->matrix.apm,
1109 matrix_mdev->matrix.aqm,
1110 matrix_mdev->matrix.adm);
1112 mutex_unlock(&kvm->lock);
1113 mutex_unlock(&matrix_dev->lock);
1120 * vfio_ap_mdev_iommu_notifier: IOMMU notifier callback
1122 * @nb: The notifier block
1123 * @action: Action to be taken
1124 * @data: data associated with the request
1126 * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
1127 * pinned before). Other requests are ignored.
1130 static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
1131 unsigned long action, void *data)
1133 struct ap_matrix_mdev *matrix_mdev;
1135 matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
1137 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
1138 struct vfio_iommu_type1_dma_unmap *unmap = data;
1139 unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
1141 vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
1149 * vfio_ap_mdev_unset_kvm
1151 * @matrix_mdev: a matrix mediated device
1153 * Performs clean-up of resources no longer needed by @matrix_mdev.
1155 * Note: The matrix_dev->lock must be taken prior to calling
1156 * this function; however, the lock will be temporarily released while the
1157 * guest's AP configuration is cleared to avoid a potential lockdep splat.
1158 * The kvm->lock is taken to clear the guest's AP configuration which, under
1159 * certain circumstances, will result in a circular lock dependency if this is
1160 * done under the @matrix_mdev->lock.
1163 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev,
1166 if (kvm && kvm->arch.crypto.crycbd) {
1167 down_write(&kvm->arch.crypto.pqap_hook_rwsem);
1168 kvm->arch.crypto.pqap_hook = NULL;
1169 up_write(&kvm->arch.crypto.pqap_hook_rwsem);
1171 mutex_lock(&kvm->lock);
1172 mutex_lock(&matrix_dev->lock);
1174 kvm_arch_crypto_clear_masks(kvm);
1175 vfio_ap_mdev_reset_queues(matrix_mdev);
1177 matrix_mdev->kvm = NULL;
1179 mutex_unlock(&kvm->lock);
1180 mutex_unlock(&matrix_dev->lock);
1184 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1185 unsigned long action, void *data)
1187 int notify_rc = NOTIFY_OK;
1188 struct ap_matrix_mdev *matrix_mdev;
1190 if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1193 matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1196 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1197 else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1198 notify_rc = NOTIFY_DONE;
1203 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1206 struct vfio_ap_queue *q = NULL;
1208 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1211 q = dev_get_drvdata(dev);
1218 int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1221 struct ap_queue_status status;
1229 status = ap_zapq(q->apqn);
1230 switch (status.response_code) {
1231 case AP_RESPONSE_NORMAL:
1234 case AP_RESPONSE_RESET_IN_PROGRESS:
1241 case AP_RESPONSE_Q_NOT_AVAIL:
1242 case AP_RESPONSE_DECONFIGURED:
1243 case AP_RESPONSE_CHECKSTOPPED:
1244 WARN_ON_ONCE(status.irq_enabled);
1246 goto free_resources;
1248 /* things are really broken, give up */
1249 WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1250 status.response_code);
1254 /* wait for the reset to take effect */
1256 if (status.queue_empty && !status.irq_enabled)
1259 status = ap_tapq(q->apqn, NULL);
1261 WARN_ON_ONCE(retry2 <= 0);
1264 vfio_ap_free_aqic_resources(q);
1269 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1273 unsigned long apid, apqi;
1274 struct vfio_ap_queue *q;
1276 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1277 matrix_mdev->matrix.apm_max + 1) {
1278 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1279 matrix_mdev->matrix.aqm_max + 1) {
1280 q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1281 ret = vfio_ap_mdev_reset_queue(q, 1);
1283 * Regardless whether a queue turns out to be busy, or
1284 * is not operational, we need to continue resetting
1285 * the remaining queues.
1295 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
1297 struct ap_matrix_mdev *matrix_mdev =
1298 container_of(vdev, struct ap_matrix_mdev, vdev);
1299 unsigned long events;
1302 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1303 events = VFIO_GROUP_NOTIFY_SET_KVM;
1305 ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1306 &events, &matrix_mdev->group_notifier);
1310 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1311 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1312 ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1313 &events, &matrix_mdev->iommu_notifier);
1315 goto out_unregister_group;
1318 out_unregister_group:
1319 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1320 &matrix_mdev->group_notifier);
1324 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
1326 struct ap_matrix_mdev *matrix_mdev =
1327 container_of(vdev, struct ap_matrix_mdev, vdev);
1329 vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY,
1330 &matrix_mdev->iommu_notifier);
1331 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1332 &matrix_mdev->group_notifier);
1333 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1336 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1338 unsigned long minsz;
1339 struct vfio_device_info info;
1341 minsz = offsetofend(struct vfio_device_info, num_irqs);
1343 if (copy_from_user(&info, (void __user *)arg, minsz))
1346 if (info.argsz < minsz)
1349 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1350 info.num_regions = 0;
1353 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1356 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1357 unsigned int cmd, unsigned long arg)
1359 struct ap_matrix_mdev *matrix_mdev =
1360 container_of(vdev, struct ap_matrix_mdev, vdev);
1363 mutex_lock(&matrix_dev->lock);
1365 case VFIO_DEVICE_GET_INFO:
1366 ret = vfio_ap_mdev_get_device_info(arg);
1368 case VFIO_DEVICE_RESET:
1369 ret = vfio_ap_mdev_reset_queues(matrix_mdev);
1375 mutex_unlock(&matrix_dev->lock);
1380 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
1381 .open_device = vfio_ap_mdev_open_device,
1382 .close_device = vfio_ap_mdev_close_device,
1383 .ioctl = vfio_ap_mdev_ioctl,
1386 static struct mdev_driver vfio_ap_matrix_driver = {
1388 .name = "vfio_ap_mdev",
1389 .owner = THIS_MODULE,
1390 .mod_name = KBUILD_MODNAME,
1391 .dev_groups = vfio_ap_mdev_attr_groups,
1393 .probe = vfio_ap_mdev_probe,
1394 .remove = vfio_ap_mdev_remove,
1397 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1398 .owner = THIS_MODULE,
1399 .device_driver = &vfio_ap_matrix_driver,
1400 .supported_type_groups = vfio_ap_mdev_type_groups,
1403 int vfio_ap_mdev_register(void)
1407 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1409 ret = mdev_register_driver(&vfio_ap_matrix_driver);
1413 ret = mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1419 mdev_unregister_driver(&vfio_ap_matrix_driver);
1423 void vfio_ap_mdev_unregister(void)
1425 mdev_unregister_device(&matrix_dev->device);
1426 mdev_unregister_driver(&vfio_ap_matrix_driver);