virtio_mmio: fix config space access using 1,2,4-byte memory access

Change-Id: I596da650be81a596efcbe7e3abecf2a4da3a8459
Reviewed-on: https://review.haiku-os.org/c/haiku/+/5747
Reviewed-by: Jérôme Duval <jerome.duval@gmail.com>
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
This commit is contained in:
X512 2022-10-17 02:08:44 +09:00 committed by Jérôme Duval
parent 56761ebfeb
commit fe084f0b03

View File

@ -409,9 +409,26 @@ virtio_device_read_device_config(virtio_device cookie, uint8 offset,
{
TRACE("virtio_device_read_device_config(%p, %d, %" B_PRIuSIZE ")\n", cookie,
offset, bufferSize);
VirtioDevice* dev = (VirtioDevice*)cookie;
memcpy(buffer, (void*)(dev->fRegs->config + offset), bufferSize);
// TODO: check ARM support, ARM seems support only 32 bit aligned MMIO access.
vuint8* src = &dev->fRegs->config[offset];
uint8* dst = (uint8*)buffer;
while (bufferSize > 0) {
uint8 size = 4;
if (bufferSize == 1) {
size = 1;
*dst = *src;
} else if (bufferSize <= 3) {
size = 2;
*(uint16*)dst = *(vuint16*)src;
} else
*(uint32*)dst = *(vuint32*)src;
dst += size;
bufferSize -= size;
src += size;
}
return B_OK;
}
@ -424,7 +441,26 @@ virtio_device_write_device_config(virtio_device cookie, uint8 offset,
TRACE("virtio_device_write_device_config(%p, %d, %" B_PRIuSIZE ")\n",
cookie, offset, bufferSize);
VirtioDevice* dev = (VirtioDevice*)cookie;
memcpy((void*)(dev->fRegs->config + offset), buffer, bufferSize);
// See virtio_device_read_device_config
uint8* src = (uint8*)buffer;
vuint8* dst = &dev->fRegs->config[offset];
while (bufferSize > 0) {
uint8 size = 4;
if (bufferSize == 1) {
size = 1;
*dst = *src;
} else if (bufferSize <= 3) {
size = 2;
*(vuint16*)dst = *(uint16*)src;
} else
*(vuint32*)dst = *(uint32*)src;
dst += size;
bufferSize -= size;
src += size;
}
return B_OK;
}