dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cell / lib / spu / FastCommunication.h
1 /*
2  * FastCommunication.h
3  *
4  *  Created on: Mar 3, 2009
5  *      Author: lschor
6  */
7
8 #ifndef FASTCOMMUNICATION_H_
9 #define FASTCOMMUNICATION_H_
10
11 // CBE includes
12 #include <spu_intrinsics.h>
13 #include <spu_mfcio.h>
14
15 // C++ includes
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <ctype.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <new>
22
23 // Local includes
24 #include "Fifo.h"
25 #include "WindowedFifo.h"
26 #include "../common.h"
27
28 // For external Mailbox Communication
29 #include "../spu_mfcio_ext.h"
30
31 // Include to allocate/free using for DMA transfers
32 #include "../lib/malloc_align.h"
33 #include "../lib/free_align.h"
34
35 //Cell Macros
36 #define waittag(tag_id) mfc_write_tag_mask(1<<tag_id);    mfc_read_tag_status_all();
37
38 #define MAXNOREQ 256
39
40 // This is a full Request
41 typedef struct _comRequest {
42     bool valid;              // If the request is valid
43     uint32_t status;      // Status of the request (read_..., ...)
44     
45     uint32_t tag_id;      // Tag ID
46     uint32_t queue;       // The queue which is used by the request
47     
48     bool iswfifo;         // True if it is a WFIFO
49     Fifo* fifo;           // Pointer to the used FIFO
50     WindowedFifo* wfifo;  // Pointer to the WFIFO
51
52     uint32_t len;           // The len of the data
53     char* data;           // Point to the data which are stored    
54     uint32_t offset;      // Offset generated by alignment
55 } comRequest;
56
57 // Struct to store a FIFO
58 typedef struct _fifoCollection {
59     int type;               // local, in or out
60     int queue;            // Corresponding queue number
61
62     bool iswfifo;         // True if it is a WFIFO
63     WindowedFifo* wfifo;  // Pointer to the WFIFO
64     Fifo* fifo;           // Pointer to the FIFO   
65 } fifoCollection;
66
67 /**
68  * The Communication Class
69  */
70 class FastCommunication {
71 public:
72     FastCommunication(int nrOfQueues, uint64_t ea_base, uint64_t *ea_base_all, int32_t * queueFromSPE, int32_t * queueOnSPE, uint64_t * fifoTails);
73     virtual ~FastCommunication();
74
75     bool addFifo(int fifoNr, Fifo* fifo, int type, int queue);
76     bool addWFifo(int fifoNr, WindowedFifo* fifo, int type, int queue);
77     
78     bool update();
79     bool empty();
80
81     static int const local = 0;
82     static int const in = 1;
83     static int const out = 2;
84
85     static int const comIn = 0;
86     static int const comOut = 1;
87
88     static int const read_request_sent = 0;
89     static int const read_started = 1;
90     static int const read_pending = 2;
91
92 protected:
93     // To send messages
94     void sendMessage(uint32_t message, int32_t process);
95     int testMessage(int32_t process);
96
97     // Requests
98     comRequest* newRequest();
99     void deleteRequest(comRequest* request);
100     comRequest* getRequest(uint8_t status, uint32_t queue);
101
102     // Queues
103     int _nrOfQueues;
104     fifoCollection * _fifos;
105
106     // Request
107     int _nrOfRequest;           // Need only 2 requests per queue (others are blocked...)
108     comRequest _request[MAXNOREQ];    // Stores at maximum 12 requests
109     uint8_t _currentRequest;    // The current request (so that we can start in the next iteration by the next one)
110     
111     // Pointers for communication
112     uint64_t _ea_base;          // This is my base address
113     uint64_t * _ea_base_all;    // All base addresses of the control blocks of the other SPEs (for sending message to)
114     int32_t * queueFromSPE;     // Array which tells us from which processor a queue comes
115     int32_t * queueOnSPE;       // Array which tells us to which processor a queue goes
116     uint64_t * _fifoTails;      // Pointers to the tail of the other queues
117 };
118
119 #endif /* FASTCOMMUNICATION_H_ */