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);
29 static int match_apqn(struct device *dev, void *data)
31 struct vfio_ap_queue *q = dev_get_drvdata(dev);
33 return (q->apqn == *(int *)(data)) ? 1 : 0;
37 * vfio_ap_get_queue: Retrieve a queue with a specific APQN from a list
38 * @matrix_mdev: the associated mediated matrix
39 * @apqn: The queue APQN
41 * Retrieve a queue with a specific APQN from the list of the
42 * devices of the vfio_ap_drv.
43 * Verify that the APID and the APQI are set in the matrix.
45 * Returns the pointer to the associated vfio_ap_queue
47 static struct vfio_ap_queue *vfio_ap_get_queue(
48 struct ap_matrix_mdev *matrix_mdev,
51 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 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
63 q = dev_get_drvdata(dev);
64 q->matrix_mdev = matrix_mdev;
71 * vfio_ap_wait_for_irqclear
72 * @apqn: The AP Queue number
74 * Checks the IRQ bit for the status of this APQN using ap_tapq.
75 * Returns if the ap_tapq function succeeded and the bit is clear.
76 * Returns if ap_tapq function failed with invalid, deconfigured or
78 * Otherwise retries up to 5 times after waiting 20ms.
81 static void vfio_ap_wait_for_irqclear(int apqn)
83 struct ap_queue_status status;
87 status = ap_tapq(apqn, NULL);
88 switch (status.response_code) {
89 case AP_RESPONSE_NORMAL:
90 case AP_RESPONSE_RESET_IN_PROGRESS:
91 if (!status.irq_enabled)
94 case AP_RESPONSE_BUSY:
97 case AP_RESPONSE_Q_NOT_AVAIL:
98 case AP_RESPONSE_DECONFIGURED:
99 case AP_RESPONSE_CHECKSTOPPED:
101 WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
102 status.response_code, apqn);
107 WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
108 __func__, status.response_code, apqn);
112 * vfio_ap_free_aqic_resources
113 * @q: The vfio_ap_queue
115 * Unregisters the ISC in the GIB when the saved ISC not invalid.
116 * Unpin the guest's page holding the NIB when it exist.
117 * Reset the saved_pfn and saved_isc to invalid values.
118 * Clear the pointer to the matrix mediated device.
121 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
123 if (q->saved_isc != VFIO_AP_ISC_INVALID && q->matrix_mdev)
124 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
125 if (q->saved_pfn && q->matrix_mdev)
126 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev),
129 q->saved_isc = VFIO_AP_ISC_INVALID;
130 q->matrix_mdev = NULL;
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 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);
186 * vfio_ap_setirq: Enable Interruption for a APQN
188 * @dev: the device associated with the ap_queue
189 * @q: the vfio_ap_queue holding AQIC parameters
191 * Pin the NIB saved in *q
192 * Register the guest ISC to GIB interface and retrieve the
193 * host ISC to issue the host side PQAP/AQIC
195 * Response.status may be set to AP_RESPONSE_INVALID_ADDRESS in case the
196 * vfio_pin_pages failed.
198 * Otherwise return the ap_queue_status returned by the ap_aqic(),
199 * all retry handling will be done by the guest.
201 static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
205 struct ap_qirq_ctrl aqic_gisa = {};
206 struct ap_queue_status status = {};
207 struct kvm_s390_gisa *gisa;
209 unsigned long h_nib, g_pfn, h_pfn;
212 g_pfn = nib >> PAGE_SHIFT;
213 ret = vfio_pin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1,
214 IOMMU_READ | IOMMU_WRITE, &h_pfn);
219 status.response_code = AP_RESPONSE_INVALID_ADDRESS;
223 kvm = q->matrix_mdev->kvm;
224 gisa = kvm->arch.gisa_int.origin;
226 h_nib = (h_pfn << PAGE_SHIFT) | (nib & ~PAGE_MASK);
227 aqic_gisa.gisc = isc;
228 aqic_gisa.isc = kvm_s390_gisc_register(kvm, isc);
230 aqic_gisa.gisa = (uint64_t)gisa >> 4;
232 status = ap_aqic(q->apqn, aqic_gisa, (void *)h_nib);
233 switch (status.response_code) {
234 case AP_RESPONSE_NORMAL:
235 /* See if we did clear older IRQ configuration */
236 vfio_ap_free_aqic_resources(q);
237 q->saved_pfn = g_pfn;
240 case AP_RESPONSE_OTHERWISE_CHANGED:
241 /* We could not modify IRQ setings: clear new configuration */
242 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev), &g_pfn, 1);
243 kvm_s390_gisc_unregister(kvm, isc);
246 pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
247 status.response_code);
248 vfio_ap_irq_disable(q);
256 * handle_pqap: PQAP instruction callback
258 * @vcpu: The vcpu on which we received the PQAP instruction
260 * Get the general register contents to initialize internal variables.
265 * Response.status may be set to following Response Code:
266 * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
267 * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
268 * - AP_RESPONSE_NORMAL (0) : in case of successs
269 * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
270 * We take the matrix_dev lock to ensure serialization on queues and
271 * mediated device access.
273 * Return 0 if we could handle the request inside KVM.
274 * otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
276 static int handle_pqap(struct kvm_vcpu *vcpu)
280 struct vfio_ap_queue *q;
281 struct ap_queue_status qstatus = {
282 .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
283 struct ap_matrix_mdev *matrix_mdev;
285 /* If we do not use the AIV facility just go to userland */
286 if (!(vcpu->arch.sie_block->eca & ECA_AIV))
289 apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
290 mutex_lock(&matrix_dev->lock);
292 if (!vcpu->kvm->arch.crypto.pqap_hook)
294 matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
295 struct ap_matrix_mdev, pqap_hook);
297 q = vfio_ap_get_queue(matrix_mdev, apqn);
301 status = vcpu->run->s.regs.gprs[1];
303 /* If IR bit(16) is set we enable the interrupt */
304 if ((status >> (63 - 16)) & 0x01)
305 qstatus = vfio_ap_irq_enable(q, status & 0x07,
306 vcpu->run->s.regs.gprs[2]);
308 qstatus = vfio_ap_irq_disable(q);
311 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
312 vcpu->run->s.regs.gprs[1] >>= 32;
313 mutex_unlock(&matrix_dev->lock);
317 static void vfio_ap_matrix_init(struct ap_config_info *info,
318 struct ap_matrix *matrix)
320 matrix->apm_max = info->apxa ? info->Na : 63;
321 matrix->aqm_max = info->apxa ? info->Nd : 15;
322 matrix->adm_max = info->apxa ? info->Nd : 15;
325 static int vfio_ap_mdev_create(struct kobject *kobj, struct mdev_device *mdev)
327 struct ap_matrix_mdev *matrix_mdev;
329 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
332 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
334 atomic_inc(&matrix_dev->available_instances);
338 matrix_mdev->mdev = mdev;
339 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
340 mdev_set_drvdata(mdev, matrix_mdev);
341 matrix_mdev->pqap_hook.hook = handle_pqap;
342 matrix_mdev->pqap_hook.owner = THIS_MODULE;
343 mutex_lock(&matrix_dev->lock);
344 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
345 mutex_unlock(&matrix_dev->lock);
350 static int vfio_ap_mdev_remove(struct mdev_device *mdev)
352 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
354 if (matrix_mdev->kvm)
357 mutex_lock(&matrix_dev->lock);
358 vfio_ap_mdev_reset_queues(mdev);
359 list_del(&matrix_mdev->node);
360 mutex_unlock(&matrix_dev->lock);
363 mdev_set_drvdata(mdev, NULL);
364 atomic_inc(&matrix_dev->available_instances);
369 static ssize_t name_show(struct kobject *kobj, struct device *dev, char *buf)
371 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
374 static MDEV_TYPE_ATTR_RO(name);
376 static ssize_t available_instances_show(struct kobject *kobj,
377 struct device *dev, char *buf)
379 return sprintf(buf, "%d\n",
380 atomic_read(&matrix_dev->available_instances));
383 static MDEV_TYPE_ATTR_RO(available_instances);
385 static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
388 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
391 static MDEV_TYPE_ATTR_RO(device_api);
393 static struct attribute *vfio_ap_mdev_type_attrs[] = {
394 &mdev_type_attr_name.attr,
395 &mdev_type_attr_device_api.attr,
396 &mdev_type_attr_available_instances.attr,
400 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
401 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
402 .attrs = vfio_ap_mdev_type_attrs,
405 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
406 &vfio_ap_mdev_hwvirt_type_group,
410 struct vfio_ap_queue_reserved {
419 * @dev: an AP queue device
420 * @data: a struct vfio_ap_queue_reserved reference
422 * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
423 * apid or apqi specified in @data:
425 * - If @data contains both an apid and apqi value, then @data will be flagged
426 * as reserved if the APID and APQI fields for the AP queue device matches
428 * - If @data contains only an apid value, @data will be flagged as
429 * reserved if the APID field in the AP queue device matches
431 * - If @data contains only an apqi value, @data will be flagged as
432 * reserved if the APQI field in the AP queue device matches
434 * Returns 0 to indicate the input to function succeeded. Returns -EINVAL if
435 * @data does not contain either an apid or apqi.
437 static int vfio_ap_has_queue(struct device *dev, void *data)
439 struct vfio_ap_queue_reserved *qres = data;
440 struct ap_queue *ap_queue = to_ap_queue(dev);
444 if (qres->apid && qres->apqi) {
445 qid = AP_MKQID(*qres->apid, *qres->apqi);
446 if (qid == ap_queue->qid)
447 qres->reserved = true;
448 } else if (qres->apid && !qres->apqi) {
449 id = AP_QID_CARD(ap_queue->qid);
450 if (id == *qres->apid)
451 qres->reserved = true;
452 } else if (!qres->apid && qres->apqi) {
453 id = AP_QID_QUEUE(ap_queue->qid);
454 if (id == *qres->apqi)
455 qres->reserved = true;
464 * vfio_ap_verify_queue_reserved
466 * @matrix_dev: a mediated matrix device
467 * @apid: an AP adapter ID
468 * @apqi: an AP queue index
470 * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
471 * driver according to the following rules:
473 * - If both @apid and @apqi are not NULL, then there must be an AP queue
474 * device bound to the vfio_ap driver with the APQN identified by @apid and
477 * - If only @apid is not NULL, then there must be an AP queue device bound
478 * to the vfio_ap driver with an APQN containing @apid
480 * - If only @apqi is not NULL, then there must be an AP queue device bound
481 * to the vfio_ap driver with an APQN containing @apqi
483 * Returns 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
485 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
489 struct vfio_ap_queue_reserved qres;
493 qres.reserved = false;
495 ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
496 &qres, vfio_ap_has_queue);
503 return -EADDRNOTAVAIL;
507 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
512 unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
514 if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
515 return vfio_ap_verify_queue_reserved(&apid, NULL);
517 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
518 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
527 * vfio_ap_mdev_verify_no_sharing
529 * Verifies that the APQNs derived from the cross product of the AP adapter IDs
530 * and AP queue indexes comprising the AP matrix are not configured for another
531 * mediated device. AP queue sharing is not allowed.
533 * @matrix_mdev: the mediated matrix device
535 * Returns 0 if the APQNs are not shared, otherwise; returns -EADDRINUSE.
537 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
539 struct ap_matrix_mdev *lstdev;
540 DECLARE_BITMAP(apm, AP_DEVICES);
541 DECLARE_BITMAP(aqm, AP_DOMAINS);
543 list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
544 if (matrix_mdev == lstdev)
547 memset(apm, 0, sizeof(apm));
548 memset(aqm, 0, sizeof(aqm));
551 * We work on full longs, as we can only exclude the leftover
552 * bits in non-inverse order. The leftover is all zeros.
554 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
555 lstdev->matrix.apm, AP_DEVICES))
558 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
559 lstdev->matrix.aqm, AP_DOMAINS))
569 * assign_adapter_store
571 * @dev: the matrix device
572 * @attr: the mediated matrix device's assign_adapter attribute
573 * @buf: a buffer containing the AP adapter number (APID) to
575 * @count: the number of bytes in @buf
577 * Parses the APID from @buf and sets the corresponding bit in the mediated
578 * matrix device's APM.
580 * Returns the number of bytes processed if the APID is valid; otherwise,
581 * returns one of the following errors:
584 * The APID is not a valid number
587 * The APID exceeds the maximum value configured for the system
590 * An APQN derived from the cross product of the APID being assigned
591 * and the APQIs previously assigned is not bound to the vfio_ap device
592 * driver; or, if no APQIs have yet been assigned, the APID is not
593 * contained in an APQN bound to the vfio_ap device driver.
596 * An APQN derived from the cross product of the APID being assigned
597 * and the APQIs previously assigned is being used by another mediated
600 static ssize_t assign_adapter_store(struct device *dev,
601 struct device_attribute *attr,
602 const char *buf, size_t count)
606 struct mdev_device *mdev = mdev_from_dev(dev);
607 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
609 /* If the guest is running, disallow assignment of adapter */
610 if (matrix_mdev->kvm)
613 ret = kstrtoul(buf, 0, &apid);
617 if (apid > matrix_mdev->matrix.apm_max)
621 * Set the bit in the AP mask (APM) corresponding to the AP adapter
622 * number (APID). The bits in the mask, from most significant to least
623 * significant bit, correspond to APIDs 0-255.
625 mutex_lock(&matrix_dev->lock);
627 ret = vfio_ap_mdev_verify_queues_reserved_for_apid(matrix_mdev, apid);
631 set_bit_inv(apid, matrix_mdev->matrix.apm);
633 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
641 clear_bit_inv(apid, matrix_mdev->matrix.apm);
643 mutex_unlock(&matrix_dev->lock);
647 static DEVICE_ATTR_WO(assign_adapter);
650 * unassign_adapter_store
652 * @dev: the matrix device
653 * @attr: the mediated matrix device's unassign_adapter attribute
654 * @buf: a buffer containing the adapter number (APID) to be unassigned
655 * @count: the number of bytes in @buf
657 * Parses the APID from @buf and clears the corresponding bit in the mediated
658 * matrix device's APM.
660 * Returns the number of bytes processed if the APID is valid; otherwise,
661 * returns one of the following errors:
662 * -EINVAL if the APID is not a number
663 * -ENODEV if the APID it exceeds the maximum value configured for the
666 static ssize_t unassign_adapter_store(struct device *dev,
667 struct device_attribute *attr,
668 const char *buf, size_t count)
672 struct mdev_device *mdev = mdev_from_dev(dev);
673 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
675 /* If the guest is running, disallow un-assignment of adapter */
676 if (matrix_mdev->kvm)
679 ret = kstrtoul(buf, 0, &apid);
683 if (apid > matrix_mdev->matrix.apm_max)
686 mutex_lock(&matrix_dev->lock);
687 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
688 mutex_unlock(&matrix_dev->lock);
692 static DEVICE_ATTR_WO(unassign_adapter);
695 vfio_ap_mdev_verify_queues_reserved_for_apqi(struct ap_matrix_mdev *matrix_mdev,
700 unsigned long nbits = matrix_mdev->matrix.apm_max + 1;
702 if (find_first_bit_inv(matrix_mdev->matrix.apm, nbits) >= nbits)
703 return vfio_ap_verify_queue_reserved(NULL, &apqi);
705 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, nbits) {
706 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
715 * assign_domain_store
717 * @dev: the matrix device
718 * @attr: the mediated matrix device's assign_domain attribute
719 * @buf: a buffer containing the AP queue index (APQI) of the domain to
721 * @count: the number of bytes in @buf
723 * Parses the APQI from @buf and sets the corresponding bit in the mediated
724 * matrix device's AQM.
726 * Returns the number of bytes processed if the APQI is valid; otherwise returns
727 * one of the following errors:
730 * The APQI is not a valid number
733 * The APQI exceeds the maximum value configured for the system
736 * An APQN derived from the cross product of the APQI being assigned
737 * and the APIDs previously assigned is not bound to the vfio_ap device
738 * driver; or, if no APIDs have yet been assigned, the APQI is not
739 * contained in an APQN bound to the vfio_ap device driver.
742 * An APQN derived from the cross product of the APQI being assigned
743 * and the APIDs previously assigned is being used by another mediated
746 static ssize_t assign_domain_store(struct device *dev,
747 struct device_attribute *attr,
748 const char *buf, size_t count)
752 struct mdev_device *mdev = mdev_from_dev(dev);
753 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
754 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
756 /* If the guest is running, disallow assignment of domain */
757 if (matrix_mdev->kvm)
760 ret = kstrtoul(buf, 0, &apqi);
766 mutex_lock(&matrix_dev->lock);
768 ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
772 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
774 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
782 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
784 mutex_unlock(&matrix_dev->lock);
788 static DEVICE_ATTR_WO(assign_domain);
792 * unassign_domain_store
794 * @dev: the matrix device
795 * @attr: the mediated matrix device's unassign_domain attribute
796 * @buf: a buffer containing the AP queue index (APQI) of the domain to
798 * @count: the number of bytes in @buf
800 * Parses the APQI from @buf and clears the corresponding bit in the
801 * mediated matrix device's AQM.
803 * Returns the number of bytes processed if the APQI is valid; otherwise,
804 * returns one of the following errors:
805 * -EINVAL if the APQI is not a number
806 * -ENODEV if the APQI exceeds the maximum value configured for the system
808 static ssize_t unassign_domain_store(struct device *dev,
809 struct device_attribute *attr,
810 const char *buf, size_t count)
814 struct mdev_device *mdev = mdev_from_dev(dev);
815 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
817 /* If the guest is running, disallow un-assignment of domain */
818 if (matrix_mdev->kvm)
821 ret = kstrtoul(buf, 0, &apqi);
825 if (apqi > matrix_mdev->matrix.aqm_max)
828 mutex_lock(&matrix_dev->lock);
829 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
830 mutex_unlock(&matrix_dev->lock);
834 static DEVICE_ATTR_WO(unassign_domain);
837 * assign_control_domain_store
839 * @dev: the matrix device
840 * @attr: the mediated matrix device's assign_control_domain attribute
841 * @buf: a buffer containing the domain ID to be assigned
842 * @count: the number of bytes in @buf
844 * Parses the domain ID from @buf and sets the corresponding bit in the mediated
845 * matrix device's ADM.
847 * Returns the number of bytes processed if the domain ID is valid; otherwise,
848 * returns one of the following errors:
849 * -EINVAL if the ID is not a number
850 * -ENODEV if the ID exceeds the maximum value configured for the system
852 static ssize_t assign_control_domain_store(struct device *dev,
853 struct device_attribute *attr,
854 const char *buf, size_t count)
858 struct mdev_device *mdev = mdev_from_dev(dev);
859 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
861 /* If the guest is running, disallow assignment of control domain */
862 if (matrix_mdev->kvm)
865 ret = kstrtoul(buf, 0, &id);
869 if (id > matrix_mdev->matrix.adm_max)
872 /* Set the bit in the ADM (bitmask) corresponding to the AP control
873 * domain number (id). The bits in the mask, from most significant to
874 * least significant, correspond to IDs 0 up to the one less than the
875 * number of control domains that can be assigned.
877 mutex_lock(&matrix_dev->lock);
878 set_bit_inv(id, matrix_mdev->matrix.adm);
879 mutex_unlock(&matrix_dev->lock);
883 static DEVICE_ATTR_WO(assign_control_domain);
886 * unassign_control_domain_store
888 * @dev: the matrix device
889 * @attr: the mediated matrix device's unassign_control_domain attribute
890 * @buf: a buffer containing the domain ID to be unassigned
891 * @count: the number of bytes in @buf
893 * Parses the domain ID from @buf and clears the corresponding bit in the
894 * mediated matrix device's ADM.
896 * Returns the number of bytes processed if the domain ID is valid; otherwise,
897 * returns one of the following errors:
898 * -EINVAL if the ID is not a number
899 * -ENODEV if the ID exceeds the maximum value configured for the system
901 static ssize_t unassign_control_domain_store(struct device *dev,
902 struct device_attribute *attr,
903 const char *buf, size_t count)
907 struct mdev_device *mdev = mdev_from_dev(dev);
908 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
909 unsigned long max_domid = matrix_mdev->matrix.adm_max;
911 /* If the guest is running, disallow un-assignment of control domain */
912 if (matrix_mdev->kvm)
915 ret = kstrtoul(buf, 0, &domid);
918 if (domid > max_domid)
921 mutex_lock(&matrix_dev->lock);
922 clear_bit_inv(domid, matrix_mdev->matrix.adm);
923 mutex_unlock(&matrix_dev->lock);
927 static DEVICE_ATTR_WO(unassign_control_domain);
929 static ssize_t control_domains_show(struct device *dev,
930 struct device_attribute *dev_attr,
937 struct mdev_device *mdev = mdev_from_dev(dev);
938 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
939 unsigned long max_domid = matrix_mdev->matrix.adm_max;
941 mutex_lock(&matrix_dev->lock);
942 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
943 n = sprintf(bufpos, "%04lx\n", id);
947 mutex_unlock(&matrix_dev->lock);
951 static DEVICE_ATTR_RO(control_domains);
953 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
956 struct mdev_device *mdev = mdev_from_dev(dev);
957 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
963 unsigned long napm_bits = matrix_mdev->matrix.apm_max + 1;
964 unsigned long naqm_bits = matrix_mdev->matrix.aqm_max + 1;
968 apid1 = find_first_bit_inv(matrix_mdev->matrix.apm, napm_bits);
969 apqi1 = find_first_bit_inv(matrix_mdev->matrix.aqm, naqm_bits);
971 mutex_lock(&matrix_dev->lock);
973 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
974 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
975 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
977 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
983 } else if (apid1 < napm_bits) {
984 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
985 n = sprintf(bufpos, "%02lx.\n", apid);
989 } else if (apqi1 < naqm_bits) {
990 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, naqm_bits) {
991 n = sprintf(bufpos, ".%04lx\n", apqi);
997 mutex_unlock(&matrix_dev->lock);
1001 static DEVICE_ATTR_RO(matrix);
1003 static struct attribute *vfio_ap_mdev_attrs[] = {
1004 &dev_attr_assign_adapter.attr,
1005 &dev_attr_unassign_adapter.attr,
1006 &dev_attr_assign_domain.attr,
1007 &dev_attr_unassign_domain.attr,
1008 &dev_attr_assign_control_domain.attr,
1009 &dev_attr_unassign_control_domain.attr,
1010 &dev_attr_control_domains.attr,
1011 &dev_attr_matrix.attr,
1015 static struct attribute_group vfio_ap_mdev_attr_group = {
1016 .attrs = vfio_ap_mdev_attrs
1019 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1020 &vfio_ap_mdev_attr_group,
1025 * vfio_ap_mdev_set_kvm
1027 * @matrix_mdev: a mediated matrix device
1028 * @kvm: reference to KVM instance
1030 * Verifies no other mediated matrix device has @kvm and sets a reference to
1031 * it in @matrix_mdev->kvm.
1033 * Return 0 if no other mediated matrix device has a reference to @kvm;
1034 * otherwise, returns an -EPERM.
1036 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1039 struct ap_matrix_mdev *m;
1041 mutex_lock(&matrix_dev->lock);
1043 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1044 if ((m != matrix_mdev) && (m->kvm == kvm)) {
1045 mutex_unlock(&matrix_dev->lock);
1050 matrix_mdev->kvm = kvm;
1052 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1053 mutex_unlock(&matrix_dev->lock);
1059 * vfio_ap_mdev_iommu_notifier: IOMMU notifier callback
1061 * @nb: The notifier block
1062 * @action: Action to be taken
1063 * @data: data associated with the request
1065 * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
1066 * pinned before). Other requests are ignored.
1069 static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
1070 unsigned long action, void *data)
1072 struct ap_matrix_mdev *matrix_mdev;
1074 matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
1076 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
1077 struct vfio_iommu_type1_dma_unmap *unmap = data;
1078 unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
1080 vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
1087 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1088 unsigned long action, void *data)
1091 struct ap_matrix_mdev *matrix_mdev;
1093 if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1096 matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1099 matrix_mdev->kvm = NULL;
1103 ret = vfio_ap_mdev_set_kvm(matrix_mdev, data);
1107 /* If there is no CRYCB pointer, then we can't copy the masks */
1108 if (!matrix_mdev->kvm->arch.crypto.crycbd)
1111 kvm_arch_crypto_set_masks(matrix_mdev->kvm, matrix_mdev->matrix.apm,
1112 matrix_mdev->matrix.aqm,
1113 matrix_mdev->matrix.adm);
1118 static void vfio_ap_irq_disable_apqn(int apqn)
1121 struct vfio_ap_queue *q;
1123 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1126 q = dev_get_drvdata(dev);
1127 vfio_ap_irq_disable(q);
1132 int vfio_ap_mdev_reset_queue(unsigned int apid, unsigned int apqi,
1135 struct ap_queue_status status;
1137 int apqn = AP_MKQID(apid, apqi);
1140 status = ap_zapq(apqn);
1141 switch (status.response_code) {
1142 case AP_RESPONSE_NORMAL:
1143 while (!status.queue_empty && retry2--) {
1145 status = ap_tapq(apqn, NULL);
1147 WARN_ON_ONCE(retry <= 0);
1149 case AP_RESPONSE_RESET_IN_PROGRESS:
1150 case AP_RESPONSE_BUSY:
1154 /* things are really broken, give up */
1162 static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev)
1166 unsigned long apid, apqi;
1167 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1169 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1170 matrix_mdev->matrix.apm_max + 1) {
1171 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1172 matrix_mdev->matrix.aqm_max + 1) {
1173 ret = vfio_ap_mdev_reset_queue(apid, apqi, 1);
1175 * Regardless whether a queue turns out to be busy, or
1176 * is not operational, we need to continue resetting
1177 * the remaining queues.
1181 vfio_ap_irq_disable_apqn(AP_MKQID(apid, apqi));
1188 static int vfio_ap_mdev_open(struct mdev_device *mdev)
1190 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1191 unsigned long events;
1195 if (!try_module_get(THIS_MODULE))
1198 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1199 events = VFIO_GROUP_NOTIFY_SET_KVM;
1201 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1202 &events, &matrix_mdev->group_notifier);
1204 module_put(THIS_MODULE);
1208 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1209 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1210 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
1211 &events, &matrix_mdev->iommu_notifier);
1215 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1216 &matrix_mdev->group_notifier);
1217 module_put(THIS_MODULE);
1221 static void vfio_ap_mdev_release(struct mdev_device *mdev)
1223 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1225 mutex_lock(&matrix_dev->lock);
1226 if (matrix_mdev->kvm) {
1227 kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
1228 matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
1229 vfio_ap_mdev_reset_queues(mdev);
1230 kvm_put_kvm(matrix_mdev->kvm);
1231 matrix_mdev->kvm = NULL;
1233 mutex_unlock(&matrix_dev->lock);
1235 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
1236 &matrix_mdev->iommu_notifier);
1237 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1238 &matrix_mdev->group_notifier);
1239 module_put(THIS_MODULE);
1242 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1244 unsigned long minsz;
1245 struct vfio_device_info info;
1247 minsz = offsetofend(struct vfio_device_info, num_irqs);
1249 if (copy_from_user(&info, (void __user *)arg, minsz))
1252 if (info.argsz < minsz)
1255 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1256 info.num_regions = 0;
1259 return copy_to_user((void __user *)arg, &info, minsz);
1262 static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev,
1263 unsigned int cmd, unsigned long arg)
1267 mutex_lock(&matrix_dev->lock);
1269 case VFIO_DEVICE_GET_INFO:
1270 ret = vfio_ap_mdev_get_device_info(arg);
1272 case VFIO_DEVICE_RESET:
1273 ret = vfio_ap_mdev_reset_queues(mdev);
1279 mutex_unlock(&matrix_dev->lock);
1284 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1285 .owner = THIS_MODULE,
1286 .supported_type_groups = vfio_ap_mdev_type_groups,
1287 .mdev_attr_groups = vfio_ap_mdev_attr_groups,
1288 .create = vfio_ap_mdev_create,
1289 .remove = vfio_ap_mdev_remove,
1290 .open = vfio_ap_mdev_open,
1291 .release = vfio_ap_mdev_release,
1292 .ioctl = vfio_ap_mdev_ioctl,
1295 int vfio_ap_mdev_register(void)
1297 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1299 return mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1302 void vfio_ap_mdev_unregister(void)
1304 mdev_unregister_device(&matrix_dev->device);