|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright (c) 2017-2019, IBM Corporation. |
| 4 | + */ |
| 5 | + |
| 6 | +#define pr_fmt(fmt) "xive-kvm: " fmt |
| 7 | + |
| 8 | +#include <linux/kernel.h> |
| 9 | +#include <linux/kvm_host.h> |
| 10 | +#include <linux/err.h> |
| 11 | +#include <linux/gfp.h> |
| 12 | +#include <linux/spinlock.h> |
| 13 | +#include <linux/delay.h> |
| 14 | +#include <asm/uaccess.h> |
| 15 | +#include <asm/kvm_book3s.h> |
| 16 | +#include <asm/kvm_ppc.h> |
| 17 | +#include <asm/hvcall.h> |
| 18 | +#include <asm/xive.h> |
| 19 | +#include <asm/xive-regs.h> |
| 20 | +#include <asm/debug.h> |
| 21 | +#include <asm/debugfs.h> |
| 22 | +#include <asm/opal.h> |
| 23 | + |
| 24 | +#include <linux/debugfs.h> |
| 25 | +#include <linux/seq_file.h> |
| 26 | + |
| 27 | +#include "book3s_xive.h" |
| 28 | + |
| 29 | +static int kvmppc_xive_native_set_attr(struct kvm_device *dev, |
| 30 | + struct kvm_device_attr *attr) |
| 31 | +{ |
| 32 | + switch (attr->group) { |
| 33 | + case KVM_DEV_XIVE_GRP_CTRL: |
| 34 | + break; |
| 35 | + } |
| 36 | + return -ENXIO; |
| 37 | +} |
| 38 | + |
| 39 | +static int kvmppc_xive_native_get_attr(struct kvm_device *dev, |
| 40 | + struct kvm_device_attr *attr) |
| 41 | +{ |
| 42 | + return -ENXIO; |
| 43 | +} |
| 44 | + |
| 45 | +static int kvmppc_xive_native_has_attr(struct kvm_device *dev, |
| 46 | + struct kvm_device_attr *attr) |
| 47 | +{ |
| 48 | + switch (attr->group) { |
| 49 | + case KVM_DEV_XIVE_GRP_CTRL: |
| 50 | + break; |
| 51 | + } |
| 52 | + return -ENXIO; |
| 53 | +} |
| 54 | + |
| 55 | +static void kvmppc_xive_native_free(struct kvm_device *dev) |
| 56 | +{ |
| 57 | + struct kvmppc_xive *xive = dev->private; |
| 58 | + struct kvm *kvm = xive->kvm; |
| 59 | + |
| 60 | + debugfs_remove(xive->dentry); |
| 61 | + |
| 62 | + pr_devel("Destroying xive native device\n"); |
| 63 | + |
| 64 | + if (kvm) |
| 65 | + kvm->arch.xive = NULL; |
| 66 | + |
| 67 | + if (xive->vp_base != XIVE_INVALID_VP) |
| 68 | + xive_native_free_vp_block(xive->vp_base); |
| 69 | + |
| 70 | + kfree(xive); |
| 71 | + kfree(dev); |
| 72 | +} |
| 73 | + |
| 74 | +static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type) |
| 75 | +{ |
| 76 | + struct kvmppc_xive *xive; |
| 77 | + struct kvm *kvm = dev->kvm; |
| 78 | + int ret = 0; |
| 79 | + |
| 80 | + pr_devel("Creating xive native device\n"); |
| 81 | + |
| 82 | + if (kvm->arch.xive) |
| 83 | + return -EEXIST; |
| 84 | + |
| 85 | + xive = kzalloc(sizeof(*xive), GFP_KERNEL); |
| 86 | + if (!xive) |
| 87 | + return -ENOMEM; |
| 88 | + |
| 89 | + dev->private = xive; |
| 90 | + xive->dev = dev; |
| 91 | + xive->kvm = kvm; |
| 92 | + kvm->arch.xive = xive; |
| 93 | + |
| 94 | + /* |
| 95 | + * Allocate a bunch of VPs. KVM_MAX_VCPUS is a large value for |
| 96 | + * a default. Getting the max number of CPUs the VM was |
| 97 | + * configured with would improve our usage of the XIVE VP space. |
| 98 | + */ |
| 99 | + xive->vp_base = xive_native_alloc_vp_block(KVM_MAX_VCPUS); |
| 100 | + pr_devel("VP_Base=%x\n", xive->vp_base); |
| 101 | + |
| 102 | + if (xive->vp_base == XIVE_INVALID_VP) |
| 103 | + ret = -ENXIO; |
| 104 | + |
| 105 | + xive->single_escalation = xive_native_has_single_escalation(); |
| 106 | + |
| 107 | + if (ret) |
| 108 | + kfree(xive); |
| 109 | + |
| 110 | + return ret; |
| 111 | +} |
| 112 | + |
| 113 | +static int xive_native_debug_show(struct seq_file *m, void *private) |
| 114 | +{ |
| 115 | + struct kvmppc_xive *xive = m->private; |
| 116 | + struct kvm *kvm = xive->kvm; |
| 117 | + |
| 118 | + if (!kvm) |
| 119 | + return 0; |
| 120 | + |
| 121 | + return 0; |
| 122 | +} |
| 123 | + |
| 124 | +static int xive_native_debug_open(struct inode *inode, struct file *file) |
| 125 | +{ |
| 126 | + return single_open(file, xive_native_debug_show, inode->i_private); |
| 127 | +} |
| 128 | + |
| 129 | +static const struct file_operations xive_native_debug_fops = { |
| 130 | + .open = xive_native_debug_open, |
| 131 | + .read = seq_read, |
| 132 | + .llseek = seq_lseek, |
| 133 | + .release = single_release, |
| 134 | +}; |
| 135 | + |
| 136 | +static void xive_native_debugfs_init(struct kvmppc_xive *xive) |
| 137 | +{ |
| 138 | + char *name; |
| 139 | + |
| 140 | + name = kasprintf(GFP_KERNEL, "kvm-xive-%p", xive); |
| 141 | + if (!name) { |
| 142 | + pr_err("%s: no memory for name\n", __func__); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + xive->dentry = debugfs_create_file(name, 0444, powerpc_debugfs_root, |
| 147 | + xive, &xive_native_debug_fops); |
| 148 | + |
| 149 | + pr_debug("%s: created %s\n", __func__, name); |
| 150 | + kfree(name); |
| 151 | +} |
| 152 | + |
| 153 | +static void kvmppc_xive_native_init(struct kvm_device *dev) |
| 154 | +{ |
| 155 | + struct kvmppc_xive *xive = (struct kvmppc_xive *)dev->private; |
| 156 | + |
| 157 | + /* Register some debug interfaces */ |
| 158 | + xive_native_debugfs_init(xive); |
| 159 | +} |
| 160 | + |
| 161 | +struct kvm_device_ops kvm_xive_native_ops = { |
| 162 | + .name = "kvm-xive-native", |
| 163 | + .create = kvmppc_xive_native_create, |
| 164 | + .init = kvmppc_xive_native_init, |
| 165 | + .destroy = kvmppc_xive_native_free, |
| 166 | + .set_attr = kvmppc_xive_native_set_attr, |
| 167 | + .get_attr = kvmppc_xive_native_get_attr, |
| 168 | + .has_attr = kvmppc_xive_native_has_attr, |
| 169 | +}; |
| 170 | + |
| 171 | +void kvmppc_xive_native_init_module(void) |
| 172 | +{ |
| 173 | + ; |
| 174 | +} |
| 175 | + |
| 176 | +void kvmppc_xive_native_exit_module(void) |
| 177 | +{ |
| 178 | + ; |
| 179 | +} |
0 commit comments