4 * Copyright (c) 2008 Bull S.A.S.
6 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
12 #include <linux/kvm_host.h>
13 #include <linux/kvm.h>
15 #include "coalesced_mmio.h"
17 static int coalesced_mmio_in_range(struct kvm_io_device
*this,
18 gpa_t addr
, int len
, int is_write
)
20 struct kvm_coalesced_mmio_dev
*dev
=
21 (struct kvm_coalesced_mmio_dev
*)this->private;
22 struct kvm_coalesced_mmio_zone
*zone
;
29 /* kvm->lock is taken by the caller and must be not released before
33 /* Are we able to batch it ? */
35 /* last is the first free entry
36 * check if we don't meet the first used entry
37 * there is always one unused entry in the buffer
40 next
= (dev
->kvm
->coalesced_mmio_ring
->last
+ 1) %
41 KVM_COALESCED_MMIO_MAX
;
42 if (next
== dev
->kvm
->coalesced_mmio_ring
->first
) {
47 /* is it in a batchable area ? */
49 for (i
= 0; i
< dev
->nb_zones
; i
++) {
52 /* (addr,len) is fully included in
53 * (zone->addr, zone->size)
56 if (zone
->addr
<= addr
&&
57 addr
+ len
<= zone
->addr
+ zone
->size
)
63 static void coalesced_mmio_write(struct kvm_io_device
*this,
64 gpa_t addr
, int len
, const void *val
)
66 struct kvm_coalesced_mmio_dev
*dev
=
67 (struct kvm_coalesced_mmio_dev
*)this->private;
68 struct kvm_coalesced_mmio_ring
*ring
= dev
->kvm
->coalesced_mmio_ring
;
70 /* kvm->lock must be taken by caller before call to in_range()*/
72 /* copy data in first free entry of the ring */
74 ring
->coalesced_mmio
[ring
->last
].phys_addr
= addr
;
75 ring
->coalesced_mmio
[ring
->last
].len
= len
;
76 memcpy(ring
->coalesced_mmio
[ring
->last
].data
, val
, len
);
78 ring
->last
= (ring
->last
+ 1) % KVM_COALESCED_MMIO_MAX
;
81 static void coalesced_mmio_destructor(struct kvm_io_device
*this)
86 int kvm_coalesced_mmio_init(struct kvm
*kvm
)
88 struct kvm_coalesced_mmio_dev
*dev
;
90 dev
= kzalloc(sizeof(struct kvm_coalesced_mmio_dev
), GFP_KERNEL
);
93 dev
->dev
.write
= coalesced_mmio_write
;
94 dev
->dev
.in_range
= coalesced_mmio_in_range
;
95 dev
->dev
.destructor
= coalesced_mmio_destructor
;
96 dev
->dev
.private = dev
;
98 kvm
->coalesced_mmio_dev
= dev
;
99 kvm_io_bus_register_dev(&kvm
->mmio_bus
, &dev
->dev
);
104 int kvm_vm_ioctl_register_coalesced_mmio(struct kvm
*kvm
,
105 struct kvm_coalesced_mmio_zone
*zone
)
107 struct kvm_coalesced_mmio_dev
*dev
= kvm
->coalesced_mmio_dev
;
112 mutex_lock(&kvm
->lock
);
113 if (dev
->nb_zones
>= KVM_COALESCED_MMIO_ZONE_MAX
) {
114 mutex_unlock(&kvm
->lock
);
118 dev
->zone
[dev
->nb_zones
] = *zone
;
121 mutex_unlock(&kvm
->lock
);
125 int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm
*kvm
,
126 struct kvm_coalesced_mmio_zone
*zone
)
129 struct kvm_coalesced_mmio_dev
*dev
= kvm
->coalesced_mmio_dev
;
130 struct kvm_coalesced_mmio_zone
*z
;
135 mutex_lock(&kvm
->lock
);
139 z
= &dev
->zone
[i
- 1];
141 /* unregister all zones
142 * included in (zone->addr, zone->size)
145 if (zone
->addr
<= z
->addr
&&
146 z
->addr
+ z
->size
<= zone
->addr
+ zone
->size
) {
148 *z
= dev
->zone
[dev
->nb_zones
];
153 mutex_unlock(&kvm
->lock
);