Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * KVM coalesced MMIO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2008 Bull S.A.S.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright 2009 Red Hat, Inc. and/or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  Author: Laurent Vivier <Laurent.Vivier@bull.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <kvm/iodev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/kvm_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/kvm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "coalesced_mmio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 				   gpa_t addr, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	/* is it in a batchable area ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	 * (addr,len) is fully included in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	 * (zone->addr, zone->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	if (addr + len < addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (addr < dev->zone.addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	if (addr + len > dev->zone.addr + dev->zone.size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev, u32 last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct kvm_coalesced_mmio_ring *ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	unsigned avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	/* Are we able to batch it ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	/* last is the first free entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	 * check if we don't meet the first used entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	 * there is always one unused entry in the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	ring = dev->kvm->coalesced_mmio_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	avail = (ring->first - last - 1) % KVM_COALESCED_MMIO_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (avail == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		/* full */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int coalesced_mmio_write(struct kvm_vcpu *vcpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 				struct kvm_io_device *this, gpa_t addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 				int len, const void *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	__u32 insert;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!coalesced_mmio_in_range(dev, addr, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	spin_lock(&dev->kvm->ring_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	insert = READ_ONCE(ring->last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (!coalesced_mmio_has_room(dev, insert) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	    insert >= KVM_COALESCED_MMIO_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		spin_unlock(&dev->kvm->ring_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	/* copy data in first free entry of the ring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	ring->coalesced_mmio[insert].phys_addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	ring->coalesced_mmio[insert].len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	memcpy(ring->coalesced_mmio[insert].data, val, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	ring->coalesced_mmio[insert].pio = dev->zone.pio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	ring->last = (insert + 1) % KVM_COALESCED_MMIO_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	spin_unlock(&dev->kvm->ring_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static void coalesced_mmio_destructor(struct kvm_io_device *this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	list_del(&dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static const struct kvm_io_device_ops coalesced_mmio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	.write      = coalesced_mmio_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	.destructor = coalesced_mmio_destructor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int kvm_coalesced_mmio_init(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	page = alloc_page(GFP_KERNEL | __GFP_ZERO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	kvm->coalesced_mmio_ring = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 * We're using this spinlock to sync access to the coalesced ring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 * The list doesn't need its own lock since device registration and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 * unregistration should only happen when kvm->slots_lock is held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	spin_lock_init(&kvm->ring_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	INIT_LIST_HEAD(&kvm->coalesced_zones);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void kvm_coalesced_mmio_free(struct kvm *kvm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (kvm->coalesced_mmio_ring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		free_page((unsigned long)kvm->coalesced_mmio_ring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 					 struct kvm_coalesced_mmio_zone *zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	struct kvm_coalesced_mmio_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (zone->pio != 1 && zone->pio != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		      GFP_KERNEL_ACCOUNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	dev->kvm = kvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	dev->zone = *zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	mutex_lock(&kvm->slots_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	ret = kvm_io_bus_register_dev(kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				zone->addr, zone->size, &dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto out_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	list_add_tail(&dev->list, &kvm->coalesced_zones);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	mutex_unlock(&kvm->slots_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) out_free_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	mutex_unlock(&kvm->slots_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 					   struct kvm_coalesced_mmio_zone *zone)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct kvm_coalesced_mmio_dev *dev, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (zone->pio != 1 && zone->pio != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	mutex_lock(&kvm->slots_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	list_for_each_entry_safe(dev, tmp, &kvm->coalesced_zones, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		if (zone->pio == dev->zone.pio &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		    coalesced_mmio_in_range(dev, zone->addr, zone->size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 			r = kvm_io_bus_unregister_dev(kvm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				zone->pio ? KVM_PIO_BUS : KVM_MMIO_BUS, &dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			 * On failure, unregister destroys all devices on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			 * bus _except_ the target device, i.e. coalesced_zones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 			 * has been modified.  No need to restart the walk as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 			 * there aren't any zones left.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			kvm_iodevice_destructor(&dev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	mutex_unlock(&kvm->slots_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 * Ignore the result of kvm_io_bus_unregister_dev(), from userspace's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	 * perspective, the coalesced MMIO is most definitely unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }