added set -e to scripts
[reconos.git] / lib / arch / arch_zynq_linux.c
blob01be64aa99b2786f32205224a1ab14205553c3b2
1 /*
2 * ____ _____
3 * ________ _________ ____ / __ \/ ___/
4 * / ___/ _ \/ ___/ __ \/ __ \/ / / /\__ \
5 * / / / __/ /__/ /_/ / / / / /_/ /___/ /
6 * /_/ \___/\___/\____/_/ /_/\____//____/
8 * ======================================================================
10 * title: Architecture specific code - Zynq, Linux
12 * project: ReconOS
13 * author: Christoph RĂ¼thing, University of Paderborn
14 * description: Functions needed for ReconOS which are architecure
15 * specific and are implemented here.
17 * ======================================================================
21 #ifdef RECONOS_ARCH_zynq
22 #ifdef RECONOS_OS_linux
24 #include "arch.h"
26 #include "../../linux/driver/include/reconos.h"
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include "pthread.h"
34 #define PROC_CONTROL_DEV "/dev/reconos/proc-control"
37 /* == OSIF related functions ============================================ */
39 int reconos_osif_open(int num) {
40 char dev[25];
41 int fd;
43 if (num < 0)
44 return -1;
46 snprintf(dev, 25, "/dev/reconos/osif-%d", num);
47 fd = open(dev, O_RDWR);
48 if (fd < 0)
49 panic("[reconos_core] error while opening osif %d\n", num);
51 return fd;
54 uint32_t reconos_osif_read(int fd) {
55 uint32_t data;
56 int ret;
58 ret = read(fd, &data, sizeof(data));
59 if (ret < 0)
60 panic("[reconos-core] error reading from osif\n");
62 return data;
65 void reconos_osif_write(int fd, uint32_t data) {
66 int ret;
68 ret = write(fd, &data, sizeof(data));
69 if (ret < 0)
70 panic("[reconos-core] error writing to osif\n");
73 void reconos_osif_close(int fd) {
74 close(fd);
78 /* == Proc control related functions ==================================== */
80 int reconos_proc_control_open() {
81 int fd;
83 fd = open(PROC_CONTROL_DEV, O_RDWR);
84 if (fd < 0)
85 panic("[reconos_core] error while opening proc control\n");
87 return fd;
90 int reconos_proc_control_get_num_hwts(int fd) {
91 int data;
93 ioctl(fd, RECONOS_PROC_CONTROL_GET_NUM_HWTS, &data);
95 return data;
98 int reconos_proc_control_get_tlb_hits(int fd) {
99 int data;
101 ioctl(fd, RECONOS_PROC_CONTROL_GET_TLB_HITS, &data);
103 return data;
106 int reconos_proc_control_get_tlb_misses(int fd) {
107 int data;
109 ioctl(fd, RECONOS_PROC_CONTROL_GET_TLB_MISSES, &data);
111 return data;
114 uint32_t reconos_proc_control_get_fault_addr(int fd) {
115 uint32_t data;
117 ioctl(fd, RECONOS_PROC_CONTROL_GET_FAULT_ADDR, &data);
119 return data;
122 void reconos_proc_control_clear_page_fault(int fd) {
123 ioctl(fd, RECONOS_PROC_CONTROL_CLEAR_PAGE_FAULT, NULL);
126 void reconos_proc_control_set_pgd(int fd) {
127 ioctl(fd, RECONOS_PROC_CONTROL_SET_PGD_ADDR, NULL);
130 void reconos_proc_control_sys_reset(int fd) {
131 ioctl(fd, RECONOS_PROC_CONTROL_SYS_RESET, NULL);
134 void reconos_proc_control_hwt_reset(int fd, int num, int reset) {
135 if (reset)
136 ioctl(fd, RECONOS_PROC_CONTROL_SET_HWT_RESET, &num);
137 else
138 ioctl(fd, RECONOS_PROC_CONTROL_CLEAR_HWT_RESET, &num);
141 void reconos_proc_control_cache_flush(int fd) {
142 ioctl(fd, RECONOS_PROC_CONTROL_CACHE_FLUSH, NULL);
145 void reconos_proc_control_close(int fd) {
146 close(fd);
150 /* == Reconfiguration related functions ================================= */
152 int is_configured = 0;
153 pthread_mutex_t mutex;
155 inline void init_xdevcfg() {
156 if (!is_configured) {
157 pthread_mutex_init(&mutex, NULL);
158 is_configured = 1;
162 int load_partial_bitstream(uint32_t *bitstream, unsigned int bitstream_length) {
163 int fd;
164 char d = '1';
166 init_xdevcfg();
168 //printf("... Programming FPGA with partial bitstream\n");
169 //printf("... Bitstream has size of %d bytes and begins with 0x%x 0x%x 0x%x\n", bitstream_length * 4, bitstream[0], bitstream[1], bitstream[2]);
171 pthread_mutex_lock(&mutex);
173 fd = open("/sys/class/xdevcfg/xdevcfg/device/is_partial_bitstream", O_WRONLY);
174 if (!fd) {
175 printf("[xdevcfg lib] failed to open\n");
176 exit(EXIT_FAILURE);
178 write(fd, &d, 1);
179 close(fd);
181 fd = open("/dev/xdevcfg", O_WRONLY);
182 if (!fd) {
183 printf("[xdevcfg lib] failed to open\n");
184 exit(EXIT_FAILURE);
186 write(fd, bitstream, bitstream_length * 4);
187 close(fd);
189 fd = open("/sys/class/xdevcfg/xdevcfg/device/prog_done", O_RDONLY);
190 if (!fd) {
191 printf("[xdevcfg lib] failed to open\n");
192 exit(EXIT_FAILURE);
194 do {
195 read(fd, &d, 1);
196 //printf("... Waiting for programming to finish, currently reading %c\n", d);
197 } while(d != '1');
198 close(fd);
200 pthread_mutex_unlock(&mutex);
202 return 0;
206 /* == Initialization function =========================================== */
208 void reconos_drv_init() {
209 // nothing to do here
212 #endif
213 #endif