1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include "io_operations.h"
13 #include "register_ops.h"
14 #include <stddef.h>
15
16 #define ALIGN_SIZE 8
17 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
18
read_from_io(void * to,const volatile void * from,unsigned long count)19 void read_from_io(void *to, const volatile void *from, unsigned long count)
20 {
21 if (to == NULL || from == NULL)
22 return;
23 while (count && !IS_ALIGNED((uintptr_t)from, 8)) {
24 *(uint8_t *)to = u8_read(from);
25 from++;
26 to++;
27 count--;
28 }
29
30 while (count >= ALIGN_SIZE) {
31 *(uint64_t *)to = u64_read(from);
32 from += ALIGN_SIZE;
33 to += ALIGN_SIZE;
34 count -= ALIGN_SIZE;
35 }
36
37 while (count) {
38 *(uint8_t *)to = u8_read(from);
39 from++;
40 to++;
41 count--;
42 }
43 }
44
write_to_io(volatile void * to,const void * from,unsigned long count)45 void write_to_io(volatile void *to, const void *from, unsigned long count)
46 {
47 if (to == NULL || from == NULL)
48 return;
49 while (count && !IS_ALIGNED((uintptr_t)to, 8)) {
50 u8_write(*(uint8_t *)from, to);
51 from++;
52 to++;
53 count--;
54 }
55
56 while (count >= ALIGN_SIZE) {
57 u64_write(*(uint64_t *)from, to);
58 from += ALIGN_SIZE;
59 to += ALIGN_SIZE;
60 count -= ALIGN_SIZE;
61 }
62
63 while (count) {
64 u8_write(*(uint8_t *)from, to);
65 from++;
66 to++;
67 count--;
68 }
69 }
70