Line data Source code
1 : #include <cstdint>
2 : #include <endian.h>
3 : #include <err.h>
4 : #include <errno.h>
5 : #include <fcntl.h>
6 : #include <iomanip>
7 : #include <iostream>
8 : #include <stdio.h>
9 : #include <string.h>
10 : #include <sys/mman.h>
11 : #include <unistd.h>
12 : #include <vector>
13 :
14 : /* /sys/bus/pci/devices/0000:<bus>:<dev>.<func>/resource<bar#> */
15 : #define get_syspath_bar_mmap(s, bus, dev, func, bar) \
16 : snprintf(s, sizeof(s), "/sys/bus/pci/devices/0000:%02x:%02x.%x/resource%u", bus, dev, func, bar)
17 :
18 : static uint32_t*
19 0 : mmap_bar(char* fname, size_t len, int prot)
20 : {
21 0 : int fd;
22 0 : void* bar;
23 :
24 0 : fd = open(fname, (prot & PROT_WRITE) ? O_RDWR : O_RDONLY);
25 0 : if (fd < 0)
26 : return NULL;
27 :
28 0 : bar = mmap(NULL, len, prot, MAP_SHARED, fd, 0);
29 0 : close(fd);
30 :
31 0 : return bar == MAP_FAILED ? NULL : (uint32_t*)bar;
32 : }
33 :
34 : uint32_t
35 0 : reg_read(uint64_t raddr)
36 : {
37 0 : char fname[256];
38 0 : uint32_t* bar;
39 :
40 0 : get_syspath_bar_mmap(fname, 0x41, 0, 0, 2);
41 :
42 : // std::cout << fname << std::endl;
43 :
44 0 : bar = mmap_bar(fname, raddr * 4 + 256, PROT_READ);
45 :
46 0 : uint32_t rval = le32toh(bar[raddr]);
47 0 : munmap(bar, raddr + 4);
48 :
49 0 : return rval;
50 : }
51 :
52 : static int32_t
53 0 : reg_write(uint64_t waddr, uint32_t wval)
54 : {
55 0 : uint32_t* bar;
56 0 : char fname[256];
57 :
58 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << waddr << " " << std::setw(8)
59 0 : << std::setfill('0') << wval << std::endl;
60 :
61 0 : get_syspath_bar_mmap(fname, 0x41, 0, 0, 2);
62 :
63 0 : bar = mmap_bar(fname, waddr * 4 + 4, PROT_WRITE);
64 :
65 0 : bar[waddr] = htole32(wval);
66 0 : munmap(bar, waddr + 4);
67 0 : return 0;
68 : }
69 :
70 : int
71 0 : main()
72 : {
73 0 : std::cout << "Hello World" << std::endl;
74 :
75 : // char fname[256];
76 : // uint32_t* bar;
77 :
78 : // uint64_t raddr = 0x0;
79 : // get_syspath_bar_mmap(fname, 0x41, 0, 0, 2);
80 :
81 : // std::cout << fname << std::endl;
82 :
83 : // bar = mmap_bar(fname, raddr+4, PROT_READ);
84 :
85 : // uint32_t rval = le32toh(bar[raddr]);
86 : // munmap(bar, raddr+4);
87 :
88 0 : uint32_t n_pages;
89 0 : uint32_t page_size;
90 0 : uint32_t next_write_index;
91 0 : uint32_t read_counts;
92 :
93 0 : std::vector<uint32_t> rd_buf(4);
94 :
95 0 : for (uint64_t i(0); i < 4; ++i) {
96 0 : uint32_t rval = reg_read(i);
97 0 : rd_buf[i] = rval;
98 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << rval << std::endl;
99 : }
100 :
101 0 : n_pages = rd_buf[0];
102 0 : page_size = rd_buf[1];
103 0 : next_write_index = rd_buf[2];
104 0 : read_counts = rd_buf[3];
105 :
106 0 : rd_buf.resize(page_size);
107 :
108 0 : std::cout << "n_pages " << n_pages << std::endl;
109 0 : std::cout << "page_size " << page_size << std::endl;
110 0 : std::cout << "next_write_index " << next_write_index << std::endl;
111 0 : std::cout << "read_counts " << read_counts << std::endl;
112 :
113 0 : uint64_t write_base = next_write_index * page_size;
114 0 : std::cout << "write base : 0x" << std::hex << std::setw(8) << std::setfill('0') << write_base << std::endl;
115 0 : reg_write(write_base + 0, 0x00010002);
116 0 : reg_write(write_base + 1, 0x200001F0);
117 0 : reg_write(write_base + 2, 0x2001010F);
118 0 : reg_write(write_base + 3, 0x00000001);
119 :
120 0 : for (uint64_t i(0); i < 4; ++i) {
121 0 : uint32_t rval = reg_read(i);
122 0 : rd_buf[i] = rval;
123 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i << " " << std::setw(8)
124 0 : << std::setfill('0') << rval << std::endl;
125 : }
126 :
127 0 : n_pages = rd_buf[0];
128 0 : page_size = rd_buf[1];
129 0 : next_write_index = rd_buf[2];
130 0 : read_counts = rd_buf[3];
131 :
132 0 : std::cout << "n_pages " << std::dec << n_pages << std::endl;
133 0 : std::cout << "page_size " << std::dec << page_size << std::endl;
134 0 : std::cout << "next_write_index " << std::dec << next_write_index << std::endl;
135 0 : std::cout << "read_counts " << std::dec << read_counts << std::endl;
136 :
137 0 : const size_t next_read_index = (next_write_index - 1 + n_pages) % n_pages;
138 0 : const size_t read_base = 4 + (next_read_index * page_size);
139 0 : std::cout << "read base : 0x" << std::hex << std::setw(8) << std::setfill('0') << read_base << std::endl;
140 :
141 0 : for (uint64_t i(0); i < 8; ++i) {
142 0 : uint32_t rval = reg_read(read_base + i);
143 : // rd_buf[i] = rval;
144 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << read_base + i << " " << std::setw(8)
145 0 : << std::setfill('0') << rval << std::endl;
146 : }
147 :
148 0 : for (uint64_t i(0); i < 4; ++i) {
149 0 : uint32_t rval = reg_read(i);
150 0 : std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i << " " << std::setw(8)
151 0 : << std::setfill('0') << rval << std::endl;
152 : }
153 :
154 : // std::cout << "---------------------" << std::endl;
155 : // for ( uint64_t i(0); i<8192; ++i) {
156 : // reg_write(i,i);
157 : // // std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i << " " << std::setw(8) <<
158 : // std::setfill('0') << rvail << std::endl;
159 : // }
160 :
161 : // std::cout << "---------------------" << std::endl;
162 : // for ( uint64_t i(4); i<(8192+4); ++i) {
163 : // uint32_t rval = reg_read(i);
164 : // // rd_buf[i] = rval;
165 : // std::cout << "0x" << std::hex << std::setw(8) << std::setfill('0') << i << " " << std::setw(8) <<
166 : // std::setfill('0') << rval << std::endl;
167 : // }
168 0 : }
|