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