2 * Copyright (C) 2017 Oracle Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
24 * Authors: Hans de Goede <hdegoede@redhat.com>
28 #include "vboxvideo_vbe.h"
29 #include "hgsmi_defs.h"
31 /* One-at-a-Time Hash from http://www.burtleburtle.net/bob/hash/doobs.html */
32 static u32
hgsmi_hash_process(u32 hash
, const u8
*data
, int size
)
43 static u32
hgsmi_hash_end(u32 hash
)
52 /* Not really a checksum but that is the naming used in all vbox code */
53 static u32
hgsmi_checksum(u32 offset
,
54 const struct hgsmi_buffer_header
*header
,
55 const struct hgsmi_buffer_tail
*tail
)
59 checksum
= hgsmi_hash_process(0, (u8
*)&offset
, sizeof(offset
));
60 checksum
= hgsmi_hash_process(checksum
, (u8
*)header
, sizeof(*header
));
61 /* 4 -> Do not checksum the checksum itself */
62 checksum
= hgsmi_hash_process(checksum
, (u8
*)tail
, 4);
64 return hgsmi_hash_end(checksum
);
67 void *hgsmi_buffer_alloc(struct gen_pool
*guest_pool
, size_t size
,
68 u8 channel
, u16 channel_info
)
70 struct hgsmi_buffer_header
*h
;
71 struct hgsmi_buffer_tail
*t
;
75 total_size
= size
+ sizeof(*h
) + sizeof(*t
);
76 h
= gen_pool_dma_alloc(guest_pool
, total_size
, &offset
);
80 t
= (struct hgsmi_buffer_tail
*)((u8
*)h
+ sizeof(*h
) + size
);
82 h
->flags
= HGSMI_BUFFER_HEADER_F_SEQ_SINGLE
;
85 h
->channel_info
= channel_info
;
86 memset(&h
->u
.header_data
, 0, sizeof(h
->u
.header_data
));
89 t
->checksum
= hgsmi_checksum(offset
, h
, t
);
91 return (u8
*)h
+ sizeof(*h
);
94 void hgsmi_buffer_free(struct gen_pool
*guest_pool
, void *buf
)
96 struct hgsmi_buffer_header
*h
=
97 (struct hgsmi_buffer_header
*)((u8
*)buf
- sizeof(*h
));
98 size_t total_size
= h
->data_size
+ sizeof(*h
) +
99 sizeof(struct hgsmi_buffer_tail
);
101 gen_pool_free(guest_pool
, (unsigned long)h
, total_size
);
104 int hgsmi_buffer_submit(struct gen_pool
*guest_pool
, void *buf
)
108 offset
= gen_pool_virt_to_phys(guest_pool
, (unsigned long)buf
-
109 sizeof(struct hgsmi_buffer_header
));
110 outl(offset
, VGA_PORT_HGSMI_GUEST
);
111 /* Make the compiler aware that the host has changed memory. */