OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [baremetal/] [uart_service/] [main.c] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * UART service for userspace
3
 */
4
#include <l4lib/macros.h>
5
#include L4LIB_INC_ARCH(syslib.h)
6
#include L4LIB_INC_ARCH(syscalls.h)
7
#include <l4lib/exregs.h>
8
#include <l4lib/lib/addr.h>
9
#include <l4lib/ipcdefs.h>
10
#include <l4/api/errno.h>
11
#include <l4/api/capability.h>
12
#include <l4/generic/cap-types.h>
13
#include <l4/api/space.h>
14
#include <container.h>
15
#include <linker.h>
16
#include <uart.h>
17
#include <libdev/uart.h>
18
 
19
/* Capabilities of this service */
20
static struct capability caparray[32];
21
static int total_caps = 0;
22
 
23
/* Number of UARTS to be managed by this service */
24
#define UARTS_TOTAL             1
25
static struct uart uart[UARTS_TOTAL];
26
 
27
int cap_read_all()
28
{
29
        int ncaps;
30
        int err;
31
 
32
        /* Read number of capabilities */
33
        if ((err = l4_capability_control(CAP_CONTROL_NCAPS,
34
                                         0, &ncaps)) < 0) {
35
                printf("l4_capability_control() reading # of"
36
                       " capabilities failed.\n Could not "
37
                       "complete CAP_CONTROL_NCAPS request.\n");
38
                BUG();
39
        }
40
        total_caps = ncaps;
41
 
42
        /* Read all capabilities */
43
        if ((err = l4_capability_control(CAP_CONTROL_READ,
44
                                         0, caparray)) < 0) {
45
                printf("l4_capability_control() reading of "
46
                       "capabilities failed.\n Could not "
47
                       "complete CAP_CONTROL_READ_CAPS request.\n");
48
                BUG();
49
        }
50
 
51
        return 0;
52
}
53
 
54
int cap_share_all_with_space()
55
{
56
        int err;
57
        /* Share all capabilities */
58
        if ((err = l4_capability_control(CAP_CONTROL_SHARE,
59
                                         CAP_SHARE_ALL_SPACE, 0)) < 0) {
60
                printf("l4_capability_control() sharing of "
61
                       "capabilities failed.\n Could not "
62
                       "complete CAP_CONTROL_SHARE request. err=%d\n", err);
63
                BUG();
64
        }
65
 
66
        return 0;
67
}
68
 
69
/*
70
 * Scans for up to UARTS_TOTAL uart devices in capabilities.
71
 */
72
int uart_probe_devices(void)
73
{
74
        int uarts = 0;
75
 
76
        /* Scan for uart devices */
77
        for (int i = 0; i < total_caps; i++) {
78
                /* Match device type */
79
                if (cap_devtype(&caparray[i]) == CAP_DEVTYPE_UART) {
80
                        /* Copy to correct device index */
81
                        memcpy(&uart[cap_devnum(&caparray[i]) - 1].cap,
82
                               &caparray[i], sizeof(uart[0].cap));
83
                        uarts++;
84
                }
85
        }
86
 
87
        if (uarts != UARTS_TOTAL) {
88
                printf("%s: Error, not all uarts could be found. "
89
                       "total uarts=%d\n", __CONTAINER_NAME__, uarts);
90
                return -ENODEV;
91
        }
92
        return 0;
93
}
94
 
95
static struct uart uart[UARTS_TOTAL];
96
 
97
int uart_setup_devices(void)
98
{
99
        for (int i = 0; i < UARTS_TOTAL; i++) {
100
                /* Get one page from address pool */
101
                uart[i].base = (unsigned long)l4_new_virtual(1);
102
 
103
                /* Map uart to a virtual address region */
104
                if (IS_ERR(l4_map((void *)__pfn_to_addr(uart[i].cap.start),
105
                                  (void *)uart[i].base, uart[i].cap.size,
106
                                  MAP_USR_IO, self_tid()))) {
107
                        printf("%s: FATAL: Failed to map UART device "
108
                               "%d to a virtual address\n",
109
                               __CONTAINER_NAME__,
110
                               cap_devnum(&uart[i].cap));
111
                        BUG();
112
                }
113
 
114
                /* Initialize uart */
115
                uart_init(uart[i].base);
116
        }
117
        return 0;
118
}
119
 
120
/*
121
 * Declare a statically allocated char buffer
122
 * with enough bitmap size to cover given size
123
 */
124
#define DECLARE_IDPOOL(name, size)      \
125
                 char name[(sizeof(struct id_pool) + ((size >> 12) >> 3))]
126
 
127
#define PAGE_POOL_SIZE                  SZ_1MB
128
static struct address_pool device_vaddr_pool;
129
DECLARE_IDPOOL(device_id_pool, PAGE_POOL_SIZE);
130
 
131
/*
132
 * Initialize a virtual address pool
133
 * for mapping physical devices.
134
 */
135
void init_vaddr_pool(void)
136
{
137
        for (int i = 0; i < total_caps; i++) {
138
                /* Find the virtual memory region for this process */
139
                if (cap_type(&caparray[i]) == CAP_TYPE_MAP_VIRTMEM &&
140
                    __pfn_to_addr(caparray[i].start) ==
141
                    (unsigned long)vma_start) {
142
 
143
                        /*
144
                         * Do we have any unused virtual space
145
                         * where we run, and do we have enough
146
                         * pages of it to map all uarts?
147
                         */
148
                        if (__pfn(page_align_up(__end))
149
                            + UARTS_TOTAL <= caparray[i].end) {
150
                                /*
151
                                 * Yes. We initialize the device
152
                                 * virtual memory pool here.
153
                                 *
154
                                 * We may allocate virtual memory
155
                                 * addresses from this pool.
156
                                 */
157
                                address_pool_init(&device_vaddr_pool,
158
                                                  (struct id_pool *)&device_id_pool,
159
                                                  page_align_up(__end),
160
                                                  __pfn_to_addr(caparray[i].end));
161
                                return;
162
                        } else
163
                                goto out_err;
164
                }
165
        }
166
 
167
out_err:
168
        printf("%s: FATAL: No virtual memory "
169
               "region available to map "
170
               "devices.\n", __CONTAINER_NAME__);
171
        BUG();
172
}
173
 
174
void *l4_new_virtual(int npages)
175
{
176
        return address_new(&device_vaddr_pool, npages, PAGE_SIZE);
177
}
178
 
179
void uart_generic_tx(char c, int devno)
180
{
181
        uart_tx_char(uart[devno].base, c);
182
}
183
 
184
char uart_generic_rx(int devno)
185
{
186
        return uart_rx_char(uart[devno].base);
187
}
188
 
189
void handle_requests(void)
190
{
191
        u32 mr[MR_UNUSED_TOTAL];
192
        l4id_t senderid;
193
        u32 tag;
194
        int ret;
195
 
196
        printf("%s: Initiating ipc.\n", __CONTAINER__);
197
        if ((ret = l4_receive(L4_ANYTHREAD)) < 0) {
198
                printf("%s: %s: IPC Error: %d. Quitting...\n",
199
                       __CONTAINER__, __FUNCTION__, ret);
200
                BUG();
201
        }
202
 
203
        /* Syslib conventional ipc data which uses first few mrs. */
204
        tag = l4_get_tag();
205
        senderid = l4_get_sender();
206
 
207
        /* Read mrs not used by syslib */
208
        for (int i = 0; i < MR_UNUSED_TOTAL; i++)
209
                mr[i] = read_mr(MR_UNUSED_START + i);
210
 
211
        /*
212
         * TODO:
213
         *
214
         * Maybe add tags here that handle requests for sharing
215
         * of the requested uart device with the client?
216
         *
217
         * In order to be able to do that, we should have a
218
         * shareable/grantable capability to the device. Also
219
         * the request should (currently) come from a task
220
         * inside the current container
221
         */
222
 
223
        /*
224
          * FIXME: Right now we are talking to UART1 by default, we need to define protocol
225
          * for sommunication with UART service
226
          */
227
        switch (tag) {
228
        case L4_IPC_TAG_UART_SENDCHAR:
229
                printf("got L4_IPC_TAG_UART_SENDCHAR with char %d\n ", mr[0]);
230
                uart_generic_tx((char)mr[0], 0);
231
                break;
232
        case L4_IPC_TAG_UART_RECVCHAR:
233
                mr[0] = (int)uart_generic_rx(0);
234
                break;
235
        default:
236
                printf("%s: Error received ipc from 0x%x residing "
237
                       "in container %x with an unrecognized tag: "
238
                       "0x%x\n", __CONTAINER__, senderid,
239
                       __cid(senderid), tag);
240
        }
241
 
242
        /* Reply */
243
        if ((ret = l4_ipc_return(ret)) < 0) {
244
                printf("%s: IPC return error: %d.\n", __FUNCTION__, ret);
245
                BUG();
246
        }
247
}
248
 
249
/*
250
 * UTCB-size aligned utcb.
251
 *
252
 * BIG WARNING NOTE:
253
 * This in-place declaration is legal if we are running
254
 * in a disjoint virtual address space, where the utcb
255
 * declaration lies in a unique virtual address in
256
 * the system.
257
 */
258
#define DECLARE_UTCB(name) \
259
        struct utcb name ALIGN(sizeof(struct utcb))
260
 
261
DECLARE_UTCB(utcb);
262
 
263
/* Set up own utcb for ipc */
264
int l4_utcb_setup(void *utcb_address)
265
{
266
        struct task_ids ids;
267
        struct exregs_data exregs;
268
        int err;
269
 
270
        l4_getid(&ids);
271
 
272
        /* Clear utcb */
273
        memset(utcb_address, 0, sizeof(struct utcb));
274
 
275
        /* Setup exregs for utcb request */
276
        memset(&exregs, 0, sizeof(exregs));
277
        exregs_set_utcb(&exregs, (unsigned long)utcb_address);
278
 
279
        if ((err = l4_exchange_registers(&exregs, ids.tid)) < 0)
280
                return err;
281
 
282
        return 0;
283
}
284
 
285
void main(void)
286
{
287
        int err;
288
 
289
        /* Read all capabilities */
290
        cap_read_all();
291
 
292
        /* Share all with space */
293
        cap_share_all_with_space();
294
 
295
        /* Scan for uart devices in capabilities */
296
        uart_probe_devices();
297
 
298
        /* Initialize virtual address pool for uarts */
299
        init_vaddr_pool();
300
 
301
        /* Map and initialize uart devices */
302
        uart_setup_devices();
303
 
304
        /* Setup own utcb */
305
        if ((err = l4_utcb_setup(&utcb)) < 0) {
306
                printf("FATAL: Could not set up own utcb. "
307
                       "err=%d\n", err);
308
                BUG();
309
        }
310
 
311
        /* Listen for uart requests */
312
        while (1)
313
                handle_requests();
314
}
315
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.