1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2017 Oracle Corporation
4 * Authors: Hans de Goede <hdegoede@redhat.com>
8 #include "vboxvideo_vbe.h"
9 #include "hgsmi_defs.h"
11 /* One-at-a-Time Hash from https://www.burtleburtle.net/bob/hash/doobs.html */
12 static u32
hgsmi_hash_process(u32 hash
, const u8
*data
, int size
)
23 static u32
hgsmi_hash_end(u32 hash
)
32 /* Not really a checksum but that is the naming used in all vbox code */
33 static u32
hgsmi_checksum(u32 offset
,
34 const struct hgsmi_buffer_header
*header
,
35 const struct hgsmi_buffer_tail
*tail
)
39 checksum
= hgsmi_hash_process(0, (u8
*)&offset
, sizeof(offset
));
40 checksum
= hgsmi_hash_process(checksum
, (u8
*)header
, sizeof(*header
));
41 /* 4 -> Do not checksum the checksum itself */
42 checksum
= hgsmi_hash_process(checksum
, (u8
*)tail
, 4);
44 return hgsmi_hash_end(checksum
);
47 void *hgsmi_buffer_alloc(struct gen_pool
*guest_pool
, size_t size
,
48 u8 channel
, u16 channel_info
)
50 struct hgsmi_buffer_header
*h
;
51 struct hgsmi_buffer_tail
*t
;
55 total_size
= size
+ sizeof(*h
) + sizeof(*t
);
56 h
= gen_pool_dma_alloc(guest_pool
, total_size
, &offset
);
60 t
= (struct hgsmi_buffer_tail
*)((u8
*)h
+ sizeof(*h
) + size
);
62 h
->flags
= HGSMI_BUFFER_HEADER_F_SEQ_SINGLE
;
65 h
->channel_info
= channel_info
;
66 memset(&h
->u
.header_data
, 0, sizeof(h
->u
.header_data
));
69 t
->checksum
= hgsmi_checksum(offset
, h
, t
);
71 return (u8
*)h
+ sizeof(*h
);
74 void hgsmi_buffer_free(struct gen_pool
*guest_pool
, void *buf
)
76 struct hgsmi_buffer_header
*h
=
77 (struct hgsmi_buffer_header
*)((u8
*)buf
- sizeof(*h
));
78 size_t total_size
= h
->data_size
+ sizeof(*h
) +
79 sizeof(struct hgsmi_buffer_tail
);
81 gen_pool_free(guest_pool
, (unsigned long)h
, total_size
);
84 int hgsmi_buffer_submit(struct gen_pool
*guest_pool
, void *buf
)
88 offset
= gen_pool_virt_to_phys(guest_pool
, (unsigned long)buf
-
89 sizeof(struct hgsmi_buffer_header
));
90 outl(offset
, VGA_PORT_HGSMI_GUEST
);
91 /* Make the compiler aware that the host has changed memory. */