summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcdrv/src/ipmbox.c
blob: f2cf7786a23675920969c519b689d392bcfd6559 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/* Cleopatre project {{{
 *
 * Copyright (C) 2011 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/ipmbox.c
 * \brief   Interfaces for IPMBox layer.
 * \ingroup plcdrv
 */

#include <linux/bug.h>
#include <linux/dma-mapping.h>
#include <linux/io.h>
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/kernel.h>
#include <linux/errno.h>

#include "registers.h"
#include "plcdrv.h"
#include "ipmbox.h"
#include "frame.h"
#include "common/ipmbox/queue.h"
#include "common/ipmbox/protocol.h"
#include "common/ipmbox/msg.h"

/**
 * Size in words for each queue.
 */
static const unsigned int
ipmbox_queue_size[IPMBOX_QUEUE_TYPE_NB][IPMBOX_QUEUE_DIRECTION_NB] =
{
    { IPMBOX_PROTOCOL_QUEUE_SIZE_A2L_MBX,
        IPMBOX_PROTOCOL_QUEUE_SIZE_L2A_MBX },
    { IPMBOX_PROTOCOL_QUEUE_SIZE_A2L_EMPTY_BUF,
        IPMBOX_PROTOCOL_QUEUE_SIZE_L2A_EMPTY_BUF },
    { IPMBOX_PROTOCOL_QUEUE_SIZE_A2L_DATA,
        IPMBOX_PROTOCOL_QUEUE_SIZE_L2A_DATA },
};

/**
 * Prototype for function to receive message from a queue.
 * \param  priv  PLC device private context
 * \param  queue  queue to treat
 * \param  budget  available budget for this queue
 * \return  consumed budget
 */
typedef unsigned int (*ipmbox_queue_receive_t) (plcdrv_t *priv,
                                                ipmbox_queue_t *queue,
                                                unsigned int budget);

/**
 * Treat EMPTY_BUF RX queue.
 * \see ipmbox_queue_receive_t
 */
static inline unsigned int
ipmbox_receive_empty_buf (plcdrv_t *priv, ipmbox_queue_t *queue,
                          unsigned int budget);

/**
 * Treat DATA RX queue.
 * \see ipmbox_queue_receive_t
 */
static inline unsigned int
ipmbox_receive_data (plcdrv_t *priv, ipmbox_queue_t *queue,
                     unsigned int budget);

/**
 * Treat MBX RX queue.
 * \see ipmbox_queue_receive_t
 */
static inline unsigned int
ipmbox_receive_mbx (plcdrv_t *priv, ipmbox_queue_t *queue,
                    unsigned int budget);

static inline unsigned int
ipmbox_receive_empty_buf (plcdrv_t *priv, ipmbox_queue_t *queue,
                          unsigned int budget)
{
    ipmbox_msg_empty_buf_t msg[IPMBOX_PROTOCOL_QUEUE_SIZE_L2A_EMPTY_BUF
        / IPMBOX_MSG_EMPTY_BUF_WORDS - 1];
    unsigned int i, msg_nb;

    msg_nb = ipmbox_queue_get_used_space (queue) / IPMBOX_MSG_EMPTY_BUF_WORDS;

    /* Empty queue. */
    if (!msg_nb)
        return 0;

    /* Get message from queue. */
    ipmbox_queue_copy_from (queue, (uint32_t *) msg,
                            msg_nb * IPMBOX_MSG_EMPTY_BUF_WORDS);

    for (i = 0; i < msg_nb; i++)
    {
        /* Call Linux driver layer to free received buffer. */
        frame_buffer_free (priv, msg[i].buffer_addr);
    }

    /* As buffers has been freed, it may resume the queue. */
    frame_wake_queue (priv);

    /* We do not consume any budget. */
    return 0;
}

static inline unsigned int
ipmbox_receive_data (plcdrv_t *priv, ipmbox_queue_t *queue,
                     unsigned int budget)
{
    ipmbox_msg_data_t msg[IPMBOX_RX_BUDGET];
    unsigned int i, msg_nb;
    uint32_t length;

    msg_nb = ipmbox_queue_get_used_space (queue) / IPMBOX_MSG_DATA_WORDS;

    /* Empty queue. */
    if (!msg_nb)
        return 0;

    msg_nb = min (msg_nb, budget);

    ipmbox_queue_copy_from (queue, (uint32_t *) msg,
                            msg_nb * IPMBOX_MSG_DATA_WORDS);

    for (i = 0; i < msg_nb; i++)
    {
        /* Get length and send to upper layer. */
        length = ipmbox_msg_get_data_length (msg[i].header);
        frame_rx_data (priv, msg[i].buffer_addr, length);
    }

    /* Reallocate as many buffers as we just received. */
    frame_buffer_alloc (priv);

    return msg_nb;
}

static inline unsigned int
ipmbox_receive_mbx (plcdrv_t *priv, ipmbox_queue_t *queue,
                    unsigned int budget)
{
    ipmbox_msg_mbx_t msg[IPMBOX_RX_BUDGET];
    unsigned int i, msg_nb;
    uint32_t msg_type, length;

    msg_nb = ipmbox_queue_get_used_space (queue) / IPMBOX_MSG_MBX_WORDS;

    /* Empty queue. */
    if (!msg_nb)
        return 0;

    msg_nb = min (msg_nb, budget);

    ipmbox_queue_copy_from (queue, (uint32_t *) msg,
                            msg_nb * IPMBOX_MSG_MBX_WORDS);

    for (i = 0; i < msg_nb; i++)
    {
        /* Get message type. */
        msg_type = ipmbox_msg_get_mbx_type (msg[i].header);
        if (msg_type == IPMBOX_MSG_MBX_TYPE_MME_PRIV)
        {
            /* Get data length. */
            length = ipmbox_msg_get_mme_priv_length (msg[i].header);
            frame_rx_mme_priv (priv, msg[i].buffer_addr, length);
        }
        else if (msg_type == IPMBOX_MSG_MBX_TYPE_DEBUG_DUMP)
        {
            /* Get data length. */
            length = ipmbox_msg_get_debug_dump_length (msg[i].header);
            frame_rx_debug_dump (priv, msg[i].buffer_addr, length);
        }
        else
            /* Unsupported message type. */
            BUG ();
    }

    /* Reallocate as many buffers as we just received (private MME only). */
    frame_buffer_alloc (priv);

    return msg_nb;
}

int
ipmbox_init (ipmbox_ctx_t *ctx, struct net_device *dev,
             irq_handler_t it_mbx_handler, irq_handler_t it_wd_handler)
{
    uint32_t *shared_mem_base_ptr;
    uint32_t virt_to_phys_offset;
    volatile ipmbox_protocol_init_t *init_struct;
    int i, j, ret;

    /* Check parameters. */
    BUG_ON (!ctx);
    BUG_ON (!dev);

    /* Map registers. */
    dev->base_addr = (unsigned long) ioremap (IPMBOX_BASE_ADDR,
                                              sizeof (ipmbox_registers_t));
    BUG_ON (!dev->base_addr);

    /* Map registers. */
    ctx->regs = (ipmbox_registers_t *) dev->base_addr;

    /* Mask all interrupts. */
    ctx->regs->l2a_it_mask = IPMBOX_L2A_IT | IPMBOX_L2A_IT_ACK
        | IPMBOX_L2A_IT_WKP | IPMBOX_L2A_IT_WD;
    /* Clear all interrupts. */
    ctx->regs->l2a_it = IPMBOX_L2A_IT | IPMBOX_L2A_IT_WD;

    /* Initialise queue sizes. */
    for (i = 0; i < IPMBOX_QUEUE_TYPE_NB; i++)
        for (j = 0; j < IPMBOX_QUEUE_DIRECTION_NB; j++)
            ctx->queue[i][j].size = ipmbox_queue_size[i][j];

    /* Compute shared memory size for queues and queues management pointers
     * (except for data queue which is managed using registers). */
    ctx->shared_mem_size = 0;
    for (i = 0; i < IPMBOX_QUEUE_TYPE_NB; i++)
        for (j = 0; j < IPMBOX_QUEUE_DIRECTION_NB; j++)
            ctx->shared_mem_size += ctx->queue[i][j].size * sizeof (uint32_t);
    ctx->shared_mem_size += sizeof (ipmbox_queue_ptr_t)
        * IPMBOX_QUEUE_TYPE_NB * IPMBOX_QUEUE_DIRECTION_NB;

    /* Allocate shared memory. */
    ctx->shared_mem_virt_base_ptr = dma_alloc_coherent
        (NULL, ctx->shared_mem_size, &ctx->shared_mem_phy_base_addr,
         (GFP_ATOMIC | GFP_DMA));
    if (!ctx->shared_mem_virt_base_ptr)
    {
        ret = -ENOMEM;
        goto cl_iounmap;
    }
    virt_to_phys_offset = ctx->shared_mem_phy_base_addr
        - (uint32_t) ctx->shared_mem_virt_base_ptr;

    /* Map queues pointers (but not for data, already done in register). */
    shared_mem_base_ptr = ctx->shared_mem_virt_base_ptr;
    for (i = 0; i < IPMBOX_QUEUE_TYPE_NB; i++)
    {
        for (j = 0; j < IPMBOX_QUEUE_DIRECTION_NB; j++)
        {
            ctx->queue[i][j].ptr = (ipmbox_queue_ptr_t *) shared_mem_base_ptr;
            shared_mem_base_ptr += sizeof (ipmbox_queue_ptr_t)
                / sizeof (uint32_t);
        }
    }

    /* Map queues buffers and initialise queues pointers at the same time.*/
    for (i = 0; i < IPMBOX_QUEUE_TYPE_NB; i++)
    {
        for (j = 0; j < IPMBOX_QUEUE_DIRECTION_NB; j++)
        {
            ipmbox_queue_t *q = &ctx->queue[i][j];
            q->base_ptr = shared_mem_base_ptr;
            q->virt_to_phys_offset = virt_to_phys_offset;
            BUG_ON (
                ipmbox_queue_virt_to_phys (q, ctx->shared_mem_virt_base_ptr)
                != ctx->shared_mem_phy_base_addr);
            BUG_ON (
                ipmbox_queue_phys_to_virt (q, ctx->shared_mem_phy_base_addr)
                != ctx->shared_mem_virt_base_ptr);
            q->ptr->head = ipmbox_queue_virt_to_phys (q, shared_mem_base_ptr);
            shared_mem_base_ptr += q->size;
            q->end_ptr = shared_mem_base_ptr;
            q->ptr->tail
                = ipmbox_queue_virt_to_phys (q, shared_mem_base_ptr - 1);
        }
    }

    /* Fill initialisation structure. */
    init_struct = ipmbox_queue_phys_to_virt (
        &ctx->queue[IPMBOX_QUEUE_DATA][IPMBOX_L2A],
        ctx->queue[IPMBOX_QUEUE_DATA][IPMBOX_L2A].ptr->head);
    init_struct->version = IPMBOX_PROTOCOL_VERSION;
    init_struct->shared_mem = (void *) ctx->shared_mem_phy_base_addr;
    for (i = 0; i < IPMBOX_QUEUE_TYPE_NB; i++)
        for (j = 0; j < IPMBOX_QUEUE_DIRECTION_NB; j++)
            init_struct->queue_size[i][j] = ctx->queue[i][j].size;

    /* Use L2A head register to pass init info structure address to Cesar. */
    ctx->regs->l2a_head = ctx->queue[IPMBOX_QUEUE_DATA][IPMBOX_L2A].ptr->head;

    /* Initialise lock. */
    spin_lock_init (&ctx->a2l_mbx_queue_lock);

    /* Register each interrupt. */
    ret = request_irq (INT_MBX, it_mbx_handler, 0, dev->name, dev);
    BUG_ON (ret);
    ret = request_irq (INT_MBX_WD, it_wd_handler, 0, dev->name, dev);
    BUG_ON (ret);

    /* Unmask used interrupts. */
    ctx->regs->l2a_it_mask &= ~(IPMBOX_L2A_IT | IPMBOX_L2A_IT_WD);

    return 0;

cl_iounmap:
    iounmap ((void *) dev->base_addr);
    return ret;
}

void
ipmbox_uninit (ipmbox_ctx_t *ctx, struct net_device *dev)
{
    /* Check arguments. */
    BUG_ON (!ctx);
    BUG_ON (!dev);

    /* Mask all interrupts. */
    ctx->regs->l2a_it_mask = IPMBOX_L2A_IT | IPMBOX_L2A_IT_ACK
        | IPMBOX_L2A_IT_WKP | IPMBOX_L2A_IT_WD;

    /* Unregister interrupts. */
    free_irq (INT_MBX, dev);
    free_irq (INT_MBX_WD, dev);

    /* Free shared memory. */
    dma_free_coherent (NULL, ctx->shared_mem_size,
                       ctx->shared_mem_virt_base_ptr,
                       ctx->shared_mem_phy_base_addr);

    /* Unmap registers. */
    iounmap ((void *) dev->base_addr);
}

bool
ipmbox_is_synchronized (ipmbox_ctx_t *ctx)
{
    int i, j;

    BUG_ON (!ctx);

    for (i = 0; i < IPMBOX_QUEUE_TYPE_NB; i++)
    {
        for (j = 0; j < IPMBOX_QUEUE_DIRECTION_NB; j++)
        {
            if (ipmbox_queue_get_used_space (
                    &ctx->queue[IPMBOX_QUEUE_DATA][IPMBOX_A2L]))
                return false;
        }
    }
    return true;
}

int
ipmbox_receive (struct napi_struct *napi, int budget)
{
    static const ipmbox_queue_receive_t rcv_func_ptr[IPMBOX_QUEUE_TYPE_NB]
        = { ipmbox_receive_mbx, ipmbox_receive_empty_buf,
            ipmbox_receive_data };
    plcdrv_t *priv;
    struct net_device *dev;
    ipmbox_ctx_t *ctx;
    ipmbox_queue_t *queue;
    int i;
    int budget_orig = budget;

    BUG_ON (budget > IPMBOX_RX_BUDGET);

    /* Initialise local variables. */
    priv = container_of (napi, plcdrv_t, napi);
    ctx = &priv->ipmbox;
    dev = priv->dev;

    do
    {
        /* Clear interrupt before getting message count from any queues in
         * case an interrupt is triggered after message count is read. */
        ctx->regs->l2a_it = IPMBOX_L2A_IT;

        for (i = 0; (i < IPMBOX_QUEUE_TYPE_NB) && budget; i++)
        {
            /* Get queue. */
            queue = &ctx->queue[i][IPMBOX_L2A];

            /* Call the function to handle message from the mailbox. */
            budget -= rcv_func_ptr[i] (priv, queue, budget);
        }
        /* If there is still some budgets and interrupt have been triggered,
         * we can still do some work. */
    } while (budget && (ctx->regs->l2a_it & IPMBOX_L2A_IT));

    /* If we still have some budget, this mean there is no more work in
     * queues, let's rest. */
    if (budget)
    {
        /* Exit polling mode. */
        netif_rx_complete (dev, napi);
        /* Unmask interrupt for future reception. */
        ctx->regs->l2a_it_mask &= ~IPMBOX_L2A_IT;
    }

    /* Return how much work has been done. */
    return budget_orig - budget;
}

void
ipmbox_send_empty_buf (ipmbox_ctx_t *ctx, uint32_t buffer_addr[],
                       unsigned int buffer_nb)
{
    ipmbox_queue_t *queue;

    queue = &ctx->queue[IPMBOX_QUEUE_EMPTY_BUF][IPMBOX_A2L];
    buffer_nb *= IPMBOX_MSG_EMPTY_BUF_WORDS;

    /* Check free space in queue. */
    BUG_ON (ipmbox_queue_get_free_space (queue) < buffer_nb);

    /* Copy data to queue. */
    ipmbox_queue_copy_to (queue, buffer_addr, buffer_nb);

    /* Trigger an IT to wake up LEON. */
    ctx->regs->a2l_it = IPMBOX_A2L_IT_ACK;
}

void
ipmbox_send_data (ipmbox_ctx_t *ctx, uint32_t buffer_addr, uint32_t header)
{
    ipmbox_msg_data_t msg;
    ipmbox_queue_t *queue;

    queue = &ctx->queue[IPMBOX_QUEUE_DATA][IPMBOX_A2L];

    /* Build message. */
    msg.header = header;
    msg.buffer_addr = buffer_addr;

    /* Check free space in queue. */
    BUG_ON (ipmbox_queue_get_free_space (queue) < IPMBOX_MSG_DATA_WORDS);

    /* Copy data to queue. */
    ipmbox_queue_copy_to (queue, (uint32_t *) &msg, IPMBOX_MSG_DATA_WORDS);

    /* Trigger an IT to wake up LEON. */
    ctx->regs->a2l_it = IPMBOX_A2L_IT;
}

void
ipmbox_send_mbx (ipmbox_ctx_t *ctx, uint32_t buffer_addr, uint32_t header)
{
    ipmbox_queue_t *queue;
    ipmbox_msg_mbx_t msg;

    queue = &ctx->queue[IPMBOX_QUEUE_MBX][IPMBOX_A2L];

    /* Build message. */
    msg.header = header;
    msg.buffer_addr = buffer_addr;

    /* This can be called for netlink debug dump in task context, lock is
     * needed. */
    spin_lock_bh (&ctx->a2l_mbx_queue_lock);

    /* Check free space in queue. */
    BUG_ON (ipmbox_queue_get_free_space (queue) < IPMBOX_MSG_MBX_WORDS);

    /* Copy data to queue. */
    ipmbox_queue_copy_to (queue, (uint32_t *) &msg, IPMBOX_MSG_MBX_WORDS);

    spin_unlock_bh (&ctx->a2l_mbx_queue_lock);

    /* Trigger an IT to wake up LEON. */
    ctx->regs->a2l_it = IPMBOX_A2L_IT;
}