1 // SPDX-License-Identifier: GPL-2.0+
3 * Adjunct processor matrix VFIO device driver callbacks.
5 * Copyright IBM Corp. 2018
11 #include <linux/string.h>
12 #include <linux/vfio.h>
13 #include <linux/device.h>
14 #include <linux/list.h>
15 #include <linux/ctype.h>
16 #include <linux/bitops.h>
17 #include <linux/kvm_host.h>
18 #include <linux/module.h>
20 #include <asm/zcrypt.h>
22 #include "vfio_ap_private.h"
24 #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
25 #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
27 static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev);
28 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
30 static int match_apqn(struct device *dev, const void *data)
32 struct vfio_ap_queue *q = dev_get_drvdata(dev);
34 return (q->apqn == *(int *)(data)) ? 1 : 0;
38 * vfio_ap_get_queue: Retrieve a queue with a specific APQN from a list
39 * @matrix_mdev: the associated mediated matrix
40 * @apqn: The queue APQN
42 * Retrieve a queue with a specific APQN from the list of the
43 * devices of the vfio_ap_drv.
44 * Verify that the APID and the APQI are set in the matrix.
46 * Returns the pointer to the associated vfio_ap_queue
48 static struct vfio_ap_queue *vfio_ap_get_queue(
49 struct ap_matrix_mdev *matrix_mdev,
52 struct vfio_ap_queue *q;
54 if (!test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm))
56 if (!test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm))
59 q = vfio_ap_find_queue(apqn);
61 q->matrix_mdev = matrix_mdev;
67 * vfio_ap_wait_for_irqclear
68 * @apqn: The AP Queue number
70 * Checks the IRQ bit for the status of this APQN using ap_tapq.
71 * Returns if the ap_tapq function succeeded and the bit is clear.
72 * Returns if ap_tapq function failed with invalid, deconfigured or
74 * Otherwise retries up to 5 times after waiting 20ms.
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
109 * @q: The vfio_ap_queue
111 * Unregisters the ISC in the GIB when the saved ISC not invalid.
112 * Unpin the guest's page holding the NIB when it exist.
113 * Reset the saved_pfn and saved_isc to invalid values.
116 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
120 if (q->saved_isc != VFIO_AP_ISC_INVALID &&
121 !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
122 kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
123 q->saved_isc = VFIO_AP_ISC_INVALID;
125 if (q->saved_pfn && !WARN_ON(!q->matrix_mdev)) {
126 vfio_unpin_pages(mdev_dev(q->matrix_mdev->mdev),
133 * vfio_ap_irq_disable
134 * @q: The vfio_ap_queue
136 * Uses ap_aqic to disable the interruption and in case of success, reset
137 * in progress or IRQ disable command already proceeded: calls
138 * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
139 * and calls vfio_ap_free_aqic_resources() to free the resources associated
140 * with the AP interrupt handling.
142 * In the case the AP is busy, or a reset is in progress,
143 * retries after 20ms, up to 5 times.
145 * Returns if ap_aqic function failed with invalid, deconfigured or
148 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
150 struct ap_qirq_ctrl aqic_gisa = {};
151 struct ap_queue_status status;
155 status = ap_aqic(q->apqn, aqic_gisa, NULL);
156 switch (status.response_code) {
157 case AP_RESPONSE_OTHERWISE_CHANGED:
158 case AP_RESPONSE_NORMAL:
159 vfio_ap_wait_for_irqclear(q->apqn);
161 case AP_RESPONSE_RESET_IN_PROGRESS:
162 case AP_RESPONSE_BUSY:
165 case AP_RESPONSE_Q_NOT_AVAIL:
166 case AP_RESPONSE_DECONFIGURED:
167 case AP_RESPONSE_CHECKSTOPPED:
168 case AP_RESPONSE_INVALID_ADDRESS:
170 /* All cases in default means AP not operational */
171 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
172 status.response_code);
177 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
178 status.response_code);
180 vfio_ap_free_aqic_resources(q);
181 q->matrix_mdev = NULL;
186 * vfio_ap_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);
298 * If the KVM pointer is in the process of being set, wait until the
299 * process has completed.
301 wait_event_cmd(matrix_mdev->wait_for_kvm,
302 !matrix_mdev->kvm_busy,
303 mutex_unlock(&matrix_dev->lock),
304 mutex_lock(&matrix_dev->lock));
306 /* If the there is no guest using the mdev, there is nothing to do */
307 if (!matrix_mdev->kvm)
310 q = vfio_ap_get_queue(matrix_mdev, apqn);
314 status = vcpu->run->s.regs.gprs[1];
316 /* If IR bit(16) is set we enable the interrupt */
317 if ((status >> (63 - 16)) & 0x01)
318 qstatus = vfio_ap_irq_enable(q, status & 0x07,
319 vcpu->run->s.regs.gprs[2]);
321 qstatus = vfio_ap_irq_disable(q);
324 memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
325 vcpu->run->s.regs.gprs[1] >>= 32;
326 mutex_unlock(&matrix_dev->lock);
330 static void vfio_ap_matrix_init(struct ap_config_info *info,
331 struct ap_matrix *matrix)
333 matrix->apm_max = info->apxa ? info->Na : 63;
334 matrix->aqm_max = info->apxa ? info->Nd : 15;
335 matrix->adm_max = info->apxa ? info->Nd : 15;
338 static int vfio_ap_mdev_create(struct mdev_device *mdev)
340 struct ap_matrix_mdev *matrix_mdev;
342 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
345 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
347 atomic_inc(&matrix_dev->available_instances);
351 matrix_mdev->mdev = mdev;
352 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
353 init_waitqueue_head(&matrix_mdev->wait_for_kvm);
354 mdev_set_drvdata(mdev, matrix_mdev);
355 matrix_mdev->pqap_hook.hook = handle_pqap;
356 matrix_mdev->pqap_hook.owner = THIS_MODULE;
357 mutex_lock(&matrix_dev->lock);
358 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
359 mutex_unlock(&matrix_dev->lock);
364 static int vfio_ap_mdev_remove(struct mdev_device *mdev)
366 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
368 mutex_lock(&matrix_dev->lock);
371 * If the KVM pointer is in flux or the guest is running, disallow
372 * un-assignment of control domain.
374 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
375 mutex_unlock(&matrix_dev->lock);
379 vfio_ap_mdev_reset_queues(mdev);
380 list_del(&matrix_mdev->node);
382 mdev_set_drvdata(mdev, NULL);
383 atomic_inc(&matrix_dev->available_instances);
384 mutex_unlock(&matrix_dev->lock);
389 static ssize_t name_show(struct mdev_type *mtype,
390 struct mdev_type_attribute *attr, char *buf)
392 return sprintf(buf, "%s\n", VFIO_AP_MDEV_NAME_HWVIRT);
395 static MDEV_TYPE_ATTR_RO(name);
397 static ssize_t available_instances_show(struct mdev_type *mtype,
398 struct mdev_type_attribute *attr,
401 return sprintf(buf, "%d\n",
402 atomic_read(&matrix_dev->available_instances));
405 static MDEV_TYPE_ATTR_RO(available_instances);
407 static ssize_t device_api_show(struct mdev_type *mtype,
408 struct mdev_type_attribute *attr, char *buf)
410 return sprintf(buf, "%s\n", VFIO_DEVICE_API_AP_STRING);
413 static MDEV_TYPE_ATTR_RO(device_api);
415 static struct attribute *vfio_ap_mdev_type_attrs[] = {
416 &mdev_type_attr_name.attr,
417 &mdev_type_attr_device_api.attr,
418 &mdev_type_attr_available_instances.attr,
422 static struct attribute_group vfio_ap_mdev_hwvirt_type_group = {
423 .name = VFIO_AP_MDEV_TYPE_HWVIRT,
424 .attrs = vfio_ap_mdev_type_attrs,
427 static struct attribute_group *vfio_ap_mdev_type_groups[] = {
428 &vfio_ap_mdev_hwvirt_type_group,
432 struct vfio_ap_queue_reserved {
441 * @dev: an AP queue device
442 * @data: a struct vfio_ap_queue_reserved reference
444 * Flags whether the AP queue device (@dev) has a queue ID containing the APQN,
445 * apid or apqi specified in @data:
447 * - If @data contains both an apid and apqi value, then @data will be flagged
448 * as reserved if the APID and APQI fields for the AP queue device matches
450 * - If @data contains only an apid value, @data will be flagged as
451 * reserved if the APID field in the AP queue device matches
453 * - If @data contains only an apqi value, @data will be flagged as
454 * reserved if the APQI field in the AP queue device matches
456 * Returns 0 to indicate the input to function succeeded. Returns -EINVAL if
457 * @data does not contain either an apid or apqi.
459 static int vfio_ap_has_queue(struct device *dev, void *data)
461 struct vfio_ap_queue_reserved *qres = data;
462 struct ap_queue *ap_queue = to_ap_queue(dev);
466 if (qres->apid && qres->apqi) {
467 qid = AP_MKQID(*qres->apid, *qres->apqi);
468 if (qid == ap_queue->qid)
469 qres->reserved = true;
470 } else if (qres->apid && !qres->apqi) {
471 id = AP_QID_CARD(ap_queue->qid);
472 if (id == *qres->apid)
473 qres->reserved = true;
474 } else if (!qres->apid && qres->apqi) {
475 id = AP_QID_QUEUE(ap_queue->qid);
476 if (id == *qres->apqi)
477 qres->reserved = true;
486 * vfio_ap_verify_queue_reserved
488 * @matrix_dev: a mediated matrix device
489 * @apid: an AP adapter ID
490 * @apqi: an AP queue index
492 * Verifies that the AP queue with @apid/@apqi is reserved by the VFIO AP device
493 * driver according to the following rules:
495 * - If both @apid and @apqi are not NULL, then there must be an AP queue
496 * device bound to the vfio_ap driver with the APQN identified by @apid and
499 * - If only @apid is not NULL, then there must be an AP queue device bound
500 * to the vfio_ap driver with an APQN containing @apid
502 * - If only @apqi is not NULL, then there must be an AP queue device bound
503 * to the vfio_ap driver with an APQN containing @apqi
505 * Returns 0 if the AP queue is reserved; otherwise, returns -EADDRNOTAVAIL.
507 static int vfio_ap_verify_queue_reserved(unsigned long *apid,
511 struct vfio_ap_queue_reserved qres;
515 qres.reserved = false;
517 ret = driver_for_each_device(&matrix_dev->vfio_ap_drv->driver, NULL,
518 &qres, vfio_ap_has_queue);
525 return -EADDRNOTAVAIL;
529 vfio_ap_mdev_verify_queues_reserved_for_apid(struct ap_matrix_mdev *matrix_mdev,
534 unsigned long nbits = matrix_mdev->matrix.aqm_max + 1;
536 if (find_first_bit_inv(matrix_mdev->matrix.aqm, nbits) >= nbits)
537 return vfio_ap_verify_queue_reserved(&apid, NULL);
539 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, nbits) {
540 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
549 * vfio_ap_mdev_verify_no_sharing
551 * Verifies that the APQNs derived from the cross product of the AP adapter IDs
552 * and AP queue indexes comprising the AP matrix are not configured for another
553 * mediated device. AP queue sharing is not allowed.
555 * @matrix_mdev: the mediated matrix device
557 * Returns 0 if the APQNs are not shared, otherwise; returns -EADDRINUSE.
559 static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *matrix_mdev)
561 struct ap_matrix_mdev *lstdev;
562 DECLARE_BITMAP(apm, AP_DEVICES);
563 DECLARE_BITMAP(aqm, AP_DOMAINS);
565 list_for_each_entry(lstdev, &matrix_dev->mdev_list, node) {
566 if (matrix_mdev == lstdev)
569 memset(apm, 0, sizeof(apm));
570 memset(aqm, 0, sizeof(aqm));
573 * We work on full longs, as we can only exclude the leftover
574 * bits in non-inverse order. The leftover is all zeros.
576 if (!bitmap_and(apm, matrix_mdev->matrix.apm,
577 lstdev->matrix.apm, AP_DEVICES))
580 if (!bitmap_and(aqm, matrix_mdev->matrix.aqm,
581 lstdev->matrix.aqm, AP_DOMAINS))
591 * assign_adapter_store
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 * Parses the APID from @buf and sets the corresponding bit in the mediated
600 * matrix device's APM.
602 * Returns the number of bytes processed if the APID is valid; otherwise,
603 * returns one of the following errors:
606 * The APID is not a valid number
609 * The APID exceeds the maximum value configured for the system
612 * An APQN derived from the cross product of the APID being assigned
613 * and the APQIs previously assigned is not bound to the vfio_ap device
614 * driver; or, if no APQIs have yet been assigned, the APID is not
615 * contained in an APQN bound to the vfio_ap device driver.
618 * An APQN derived from the cross product of the APID being assigned
619 * and the APQIs previously assigned is being used by another mediated
622 static ssize_t assign_adapter_store(struct device *dev,
623 struct device_attribute *attr,
624 const char *buf, size_t count)
628 struct mdev_device *mdev = mdev_from_dev(dev);
629 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
631 mutex_lock(&matrix_dev->lock);
634 * If the KVM pointer is in flux or the guest is running, disallow
635 * un-assignment of adapter
637 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
642 ret = kstrtoul(buf, 0, &apid);
646 if (apid > matrix_mdev->matrix.apm_max) {
652 * Set the bit in the AP mask (APM) corresponding to the AP adapter
653 * number (APID). The bits in the mask, from most significant to least
654 * significant bit, correspond to APIDs 0-255.
656 ret = vfio_ap_mdev_verify_queues_reserved_for_apid(matrix_mdev, apid);
660 set_bit_inv(apid, matrix_mdev->matrix.apm);
662 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
670 clear_bit_inv(apid, matrix_mdev->matrix.apm);
672 mutex_unlock(&matrix_dev->lock);
676 static DEVICE_ATTR_WO(assign_adapter);
679 * unassign_adapter_store
681 * @dev: the matrix device
682 * @attr: the mediated matrix device's unassign_adapter attribute
683 * @buf: a buffer containing the adapter number (APID) to be unassigned
684 * @count: the number of bytes in @buf
686 * Parses the APID from @buf and clears the corresponding bit in the mediated
687 * matrix device's APM.
689 * Returns the number of bytes processed if the APID is valid; otherwise,
690 * returns one of the following errors:
691 * -EINVAL if the APID is not a number
692 * -ENODEV if the APID it exceeds the maximum value configured for the
695 static ssize_t unassign_adapter_store(struct device *dev,
696 struct device_attribute *attr,
697 const char *buf, size_t count)
701 struct mdev_device *mdev = mdev_from_dev(dev);
702 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
704 mutex_lock(&matrix_dev->lock);
707 * If the KVM pointer is in flux or the guest is running, disallow
708 * un-assignment of adapter
710 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
715 ret = kstrtoul(buf, 0, &apid);
719 if (apid > matrix_mdev->matrix.apm_max) {
724 clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
727 mutex_unlock(&matrix_dev->lock);
730 static DEVICE_ATTR_WO(unassign_adapter);
733 vfio_ap_mdev_verify_queues_reserved_for_apqi(struct ap_matrix_mdev *matrix_mdev,
738 unsigned long nbits = matrix_mdev->matrix.apm_max + 1;
740 if (find_first_bit_inv(matrix_mdev->matrix.apm, nbits) >= nbits)
741 return vfio_ap_verify_queue_reserved(NULL, &apqi);
743 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, nbits) {
744 ret = vfio_ap_verify_queue_reserved(&apid, &apqi);
753 * assign_domain_store
755 * @dev: the matrix device
756 * @attr: the mediated matrix device's assign_domain attribute
757 * @buf: a buffer containing the AP queue index (APQI) of the domain to
759 * @count: the number of bytes in @buf
761 * Parses the APQI from @buf and sets the corresponding bit in the mediated
762 * matrix device's AQM.
764 * Returns the number of bytes processed if the APQI is valid; otherwise returns
765 * one of the following errors:
768 * The APQI is not a valid number
771 * The APQI exceeds the maximum value configured for the system
774 * An APQN derived from the cross product of the APQI being assigned
775 * and the APIDs previously assigned is not bound to the vfio_ap device
776 * driver; or, if no APIDs have yet been assigned, the APQI is not
777 * contained in an APQN bound to the vfio_ap device driver.
780 * An APQN derived from the cross product of the APQI being assigned
781 * and the APIDs previously assigned is being used by another mediated
784 static ssize_t assign_domain_store(struct device *dev,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
790 struct mdev_device *mdev = mdev_from_dev(dev);
791 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
792 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
794 mutex_lock(&matrix_dev->lock);
797 * If the KVM pointer is in flux or the guest is running, disallow
798 * assignment of domain
800 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
805 ret = kstrtoul(buf, 0, &apqi);
808 if (apqi > max_apqi) {
813 ret = vfio_ap_mdev_verify_queues_reserved_for_apqi(matrix_mdev, apqi);
817 set_bit_inv(apqi, matrix_mdev->matrix.aqm);
819 ret = vfio_ap_mdev_verify_no_sharing(matrix_mdev);
827 clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
829 mutex_unlock(&matrix_dev->lock);
833 static DEVICE_ATTR_WO(assign_domain);
837 * unassign_domain_store
839 * @dev: the matrix device
840 * @attr: the mediated matrix device's unassign_domain attribute
841 * @buf: a buffer containing the AP queue index (APQI) of the domain to
843 * @count: the number of bytes in @buf
845 * Parses the APQI from @buf and clears the corresponding bit in the
846 * mediated matrix device's AQM.
848 * Returns the number of bytes processed if the APQI is valid; otherwise,
849 * returns one of the following errors:
850 * -EINVAL if the APQI is not a number
851 * -ENODEV if the APQI exceeds the maximum value configured for the system
853 static ssize_t unassign_domain_store(struct device *dev,
854 struct device_attribute *attr,
855 const char *buf, size_t count)
859 struct mdev_device *mdev = mdev_from_dev(dev);
860 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
862 mutex_lock(&matrix_dev->lock);
865 * If the KVM pointer is in flux or the guest is running, disallow
866 * un-assignment of domain
868 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
873 ret = kstrtoul(buf, 0, &apqi);
877 if (apqi > matrix_mdev->matrix.aqm_max) {
882 clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
886 mutex_unlock(&matrix_dev->lock);
889 static DEVICE_ATTR_WO(unassign_domain);
892 * assign_control_domain_store
894 * @dev: the matrix device
895 * @attr: the mediated matrix device's assign_control_domain attribute
896 * @buf: a buffer containing the domain ID to be assigned
897 * @count: the number of bytes in @buf
899 * Parses the domain ID from @buf and sets the corresponding bit in the mediated
900 * matrix device's ADM.
902 * Returns the number of bytes processed if the domain ID is valid; otherwise,
903 * returns one of the following errors:
904 * -EINVAL if the ID is not a number
905 * -ENODEV if the ID exceeds the maximum value configured for the system
907 static ssize_t assign_control_domain_store(struct device *dev,
908 struct device_attribute *attr,
909 const char *buf, size_t count)
913 struct mdev_device *mdev = mdev_from_dev(dev);
914 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
916 mutex_lock(&matrix_dev->lock);
919 * If the KVM pointer is in flux or the guest is running, disallow
920 * assignment of control domain.
922 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
927 ret = kstrtoul(buf, 0, &id);
931 if (id > matrix_mdev->matrix.adm_max) {
936 /* Set the bit in the ADM (bitmask) corresponding to the AP control
937 * domain number (id). The bits in the mask, from most significant to
938 * least significant, correspond to IDs 0 up to the one less than the
939 * number of control domains that can be assigned.
941 set_bit_inv(id, matrix_mdev->matrix.adm);
944 mutex_unlock(&matrix_dev->lock);
947 static DEVICE_ATTR_WO(assign_control_domain);
950 * unassign_control_domain_store
952 * @dev: the matrix device
953 * @attr: the mediated matrix device's unassign_control_domain attribute
954 * @buf: a buffer containing the domain ID to be unassigned
955 * @count: the number of bytes in @buf
957 * Parses the domain ID from @buf and clears the corresponding bit in the
958 * mediated matrix device's ADM.
960 * Returns the number of bytes processed if the domain ID is valid; otherwise,
961 * returns one of the following errors:
962 * -EINVAL if the ID is not a number
963 * -ENODEV if the ID exceeds the maximum value configured for the system
965 static ssize_t unassign_control_domain_store(struct device *dev,
966 struct device_attribute *attr,
967 const char *buf, size_t count)
971 struct mdev_device *mdev = mdev_from_dev(dev);
972 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
973 unsigned long max_domid = matrix_mdev->matrix.adm_max;
975 mutex_lock(&matrix_dev->lock);
978 * If the KVM pointer is in flux or the guest is running, disallow
979 * un-assignment of control domain.
981 if (matrix_mdev->kvm_busy || matrix_mdev->kvm) {
986 ret = kstrtoul(buf, 0, &domid);
989 if (domid > max_domid) {
994 clear_bit_inv(domid, matrix_mdev->matrix.adm);
997 mutex_unlock(&matrix_dev->lock);
1000 static DEVICE_ATTR_WO(unassign_control_domain);
1002 static ssize_t control_domains_show(struct device *dev,
1003 struct device_attribute *dev_attr,
1010 struct mdev_device *mdev = mdev_from_dev(dev);
1011 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1012 unsigned long max_domid = matrix_mdev->matrix.adm_max;
1014 mutex_lock(&matrix_dev->lock);
1015 for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1) {
1016 n = sprintf(bufpos, "%04lx\n", id);
1020 mutex_unlock(&matrix_dev->lock);
1024 static DEVICE_ATTR_RO(control_domains);
1026 static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
1029 struct mdev_device *mdev = mdev_from_dev(dev);
1030 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1034 unsigned long apid1;
1035 unsigned long apqi1;
1036 unsigned long napm_bits = matrix_mdev->matrix.apm_max + 1;
1037 unsigned long naqm_bits = matrix_mdev->matrix.aqm_max + 1;
1041 apid1 = find_first_bit_inv(matrix_mdev->matrix.apm, napm_bits);
1042 apqi1 = find_first_bit_inv(matrix_mdev->matrix.aqm, naqm_bits);
1044 mutex_lock(&matrix_dev->lock);
1046 if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
1047 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1048 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1050 n = sprintf(bufpos, "%02lx.%04lx\n", apid,
1056 } else if (apid1 < napm_bits) {
1057 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, napm_bits) {
1058 n = sprintf(bufpos, "%02lx.\n", apid);
1062 } else if (apqi1 < naqm_bits) {
1063 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, naqm_bits) {
1064 n = sprintf(bufpos, ".%04lx\n", apqi);
1070 mutex_unlock(&matrix_dev->lock);
1074 static DEVICE_ATTR_RO(matrix);
1076 static struct attribute *vfio_ap_mdev_attrs[] = {
1077 &dev_attr_assign_adapter.attr,
1078 &dev_attr_unassign_adapter.attr,
1079 &dev_attr_assign_domain.attr,
1080 &dev_attr_unassign_domain.attr,
1081 &dev_attr_assign_control_domain.attr,
1082 &dev_attr_unassign_control_domain.attr,
1083 &dev_attr_control_domains.attr,
1084 &dev_attr_matrix.attr,
1088 static struct attribute_group vfio_ap_mdev_attr_group = {
1089 .attrs = vfio_ap_mdev_attrs
1092 static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
1093 &vfio_ap_mdev_attr_group,
1098 * vfio_ap_mdev_set_kvm
1100 * @matrix_mdev: a mediated matrix device
1101 * @kvm: reference to KVM instance
1103 * Sets all data for @matrix_mdev that are needed to manage AP resources
1104 * for the guest whose state is represented by @kvm.
1106 * Note: The matrix_dev->lock must be taken prior to calling
1107 * this function; however, the lock will be temporarily released while the
1108 * guest's AP configuration is set to avoid a potential lockdep splat.
1109 * The kvm->lock is taken to set the guest's AP configuration which, under
1110 * certain circumstances, will result in a circular lock dependency if this is
1111 * done under the @matrix_mdev->lock.
1113 * Return 0 if no other mediated matrix device has a reference to @kvm;
1114 * otherwise, returns an -EPERM.
1116 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
1119 struct ap_matrix_mdev *m;
1121 if (kvm->arch.crypto.crycbd) {
1122 list_for_each_entry(m, &matrix_dev->mdev_list, node) {
1123 if (m != matrix_mdev && m->kvm == kvm)
1128 matrix_mdev->kvm_busy = true;
1129 mutex_unlock(&matrix_dev->lock);
1130 kvm_arch_crypto_set_masks(kvm,
1131 matrix_mdev->matrix.apm,
1132 matrix_mdev->matrix.aqm,
1133 matrix_mdev->matrix.adm);
1134 mutex_lock(&matrix_dev->lock);
1135 kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
1136 matrix_mdev->kvm = kvm;
1137 matrix_mdev->kvm_busy = false;
1138 wake_up_all(&matrix_mdev->wait_for_kvm);
1145 * vfio_ap_mdev_iommu_notifier: IOMMU notifier callback
1147 * @nb: The notifier block
1148 * @action: Action to be taken
1149 * @data: data associated with the request
1151 * For an UNMAP request, unpin the guest IOVA (the NIB guest address we
1152 * pinned before). Other requests are ignored.
1155 static int vfio_ap_mdev_iommu_notifier(struct notifier_block *nb,
1156 unsigned long action, void *data)
1158 struct ap_matrix_mdev *matrix_mdev;
1160 matrix_mdev = container_of(nb, struct ap_matrix_mdev, iommu_notifier);
1162 if (action == VFIO_IOMMU_NOTIFY_DMA_UNMAP) {
1163 struct vfio_iommu_type1_dma_unmap *unmap = data;
1164 unsigned long g_pfn = unmap->iova >> PAGE_SHIFT;
1166 vfio_unpin_pages(mdev_dev(matrix_mdev->mdev), &g_pfn, 1);
1174 * vfio_ap_mdev_unset_kvm
1176 * @matrix_mdev: a matrix mediated device
1178 * Performs clean-up of resources no longer needed by @matrix_mdev.
1180 * Note: The matrix_dev->lock must be taken prior to calling
1181 * this function; however, the lock will be temporarily released while the
1182 * guest's AP configuration is cleared to avoid a potential lockdep splat.
1183 * The kvm->lock is taken to clear the guest's AP configuration which, under
1184 * certain circumstances, will result in a circular lock dependency if this is
1185 * done under the @matrix_mdev->lock.
1188 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
1191 * If the KVM pointer is in the process of being set, wait until the
1192 * process has completed.
1194 wait_event_cmd(matrix_mdev->wait_for_kvm,
1195 !matrix_mdev->kvm_busy,
1196 mutex_unlock(&matrix_dev->lock),
1197 mutex_lock(&matrix_dev->lock));
1199 if (matrix_mdev->kvm) {
1200 matrix_mdev->kvm_busy = true;
1201 mutex_unlock(&matrix_dev->lock);
1202 kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
1203 mutex_lock(&matrix_dev->lock);
1204 vfio_ap_mdev_reset_queues(matrix_mdev->mdev);
1205 matrix_mdev->kvm->arch.crypto.pqap_hook = NULL;
1206 kvm_put_kvm(matrix_mdev->kvm);
1207 matrix_mdev->kvm = NULL;
1208 matrix_mdev->kvm_busy = false;
1209 wake_up_all(&matrix_mdev->wait_for_kvm);
1213 static int vfio_ap_mdev_group_notifier(struct notifier_block *nb,
1214 unsigned long action, void *data)
1216 int notify_rc = NOTIFY_OK;
1217 struct ap_matrix_mdev *matrix_mdev;
1219 if (action != VFIO_GROUP_NOTIFY_SET_KVM)
1222 mutex_lock(&matrix_dev->lock);
1223 matrix_mdev = container_of(nb, struct ap_matrix_mdev, group_notifier);
1226 vfio_ap_mdev_unset_kvm(matrix_mdev);
1227 else if (vfio_ap_mdev_set_kvm(matrix_mdev, data))
1228 notify_rc = NOTIFY_DONE;
1230 mutex_unlock(&matrix_dev->lock);
1235 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
1238 struct vfio_ap_queue *q = NULL;
1240 dev = driver_find_device(&matrix_dev->vfio_ap_drv->driver, NULL,
1243 q = dev_get_drvdata(dev);
1250 int vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q,
1253 struct ap_queue_status status;
1261 status = ap_zapq(q->apqn);
1262 switch (status.response_code) {
1263 case AP_RESPONSE_NORMAL:
1266 case AP_RESPONSE_RESET_IN_PROGRESS:
1273 case AP_RESPONSE_Q_NOT_AVAIL:
1274 case AP_RESPONSE_DECONFIGURED:
1275 case AP_RESPONSE_CHECKSTOPPED:
1276 WARN_ON_ONCE(status.irq_enabled);
1278 goto free_resources;
1280 /* things are really broken, give up */
1281 WARN(true, "PQAP/ZAPQ completed with invalid rc (%x)\n",
1282 status.response_code);
1286 /* wait for the reset to take effect */
1288 if (status.queue_empty && !status.irq_enabled)
1291 status = ap_tapq(q->apqn, NULL);
1293 WARN_ON_ONCE(retry2 <= 0);
1296 vfio_ap_free_aqic_resources(q);
1301 static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev)
1305 unsigned long apid, apqi;
1306 struct vfio_ap_queue *q;
1307 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1309 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1310 matrix_mdev->matrix.apm_max + 1) {
1311 for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
1312 matrix_mdev->matrix.aqm_max + 1) {
1313 q = vfio_ap_find_queue(AP_MKQID(apid, apqi));
1314 ret = vfio_ap_mdev_reset_queue(q, 1);
1316 * Regardless whether a queue turns out to be busy, or
1317 * is not operational, we need to continue resetting
1318 * the remaining queues.
1328 static int vfio_ap_mdev_open(struct mdev_device *mdev)
1330 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1331 unsigned long events;
1335 if (!try_module_get(THIS_MODULE))
1338 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1339 events = VFIO_GROUP_NOTIFY_SET_KVM;
1341 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1342 &events, &matrix_mdev->group_notifier);
1344 module_put(THIS_MODULE);
1348 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1349 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
1350 ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
1351 &events, &matrix_mdev->iommu_notifier);
1355 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1356 &matrix_mdev->group_notifier);
1357 module_put(THIS_MODULE);
1361 static void vfio_ap_mdev_release(struct mdev_device *mdev)
1363 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1365 mutex_lock(&matrix_dev->lock);
1366 vfio_ap_mdev_unset_kvm(matrix_mdev);
1367 mutex_unlock(&matrix_dev->lock);
1369 vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY,
1370 &matrix_mdev->iommu_notifier);
1371 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,
1372 &matrix_mdev->group_notifier);
1373 module_put(THIS_MODULE);
1376 static int vfio_ap_mdev_get_device_info(unsigned long arg)
1378 unsigned long minsz;
1379 struct vfio_device_info info;
1381 minsz = offsetofend(struct vfio_device_info, num_irqs);
1383 if (copy_from_user(&info, (void __user *)arg, minsz))
1386 if (info.argsz < minsz)
1389 info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
1390 info.num_regions = 0;
1393 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1396 static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev,
1397 unsigned int cmd, unsigned long arg)
1400 struct ap_matrix_mdev *matrix_mdev;
1402 mutex_lock(&matrix_dev->lock);
1404 case VFIO_DEVICE_GET_INFO:
1405 ret = vfio_ap_mdev_get_device_info(arg);
1407 case VFIO_DEVICE_RESET:
1408 matrix_mdev = mdev_get_drvdata(mdev);
1409 if (WARN(!matrix_mdev, "Driver data missing from mdev!!")) {
1415 * If the KVM pointer is in the process of being set, wait until
1416 * the process has completed.
1418 wait_event_cmd(matrix_mdev->wait_for_kvm,
1419 !matrix_mdev->kvm_busy,
1420 mutex_unlock(&matrix_dev->lock),
1421 mutex_lock(&matrix_dev->lock));
1423 ret = vfio_ap_mdev_reset_queues(mdev);
1429 mutex_unlock(&matrix_dev->lock);
1434 static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1435 .owner = THIS_MODULE,
1436 .supported_type_groups = vfio_ap_mdev_type_groups,
1437 .mdev_attr_groups = vfio_ap_mdev_attr_groups,
1438 .create = vfio_ap_mdev_create,
1439 .remove = vfio_ap_mdev_remove,
1440 .open = vfio_ap_mdev_open,
1441 .release = vfio_ap_mdev_release,
1442 .ioctl = vfio_ap_mdev_ioctl,
1445 int vfio_ap_mdev_register(void)
1447 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);
1449 return mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops);
1452 void vfio_ap_mdev_unregister(void)
1454 mdev_unregister_device(&matrix_dev->device);