summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/cell/ppu/cell_spu.c
blob: 9508227e29809f53c6cca362fd440c140f6e87a4 (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
/**************************************************************************
 * 
 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/


/**
 * Utility/wrappers for communicating with the SPUs.
 */


#include <pthread.h>

#include "cell_spu.h"
#include "pipe/p_format.h"
#include "pipe/p_state.h"
#include "cell/common.h"


/*
helpful headers:
/opt/ibm/cell-sdk/prototype/src/include/ppu/cbe_mfc.h
*/


/**
 * Cell/SPU info that's not per-context.
 */
struct cell_global_info cell_global;


/**
 * Write a 1-word message to the given SPE mailbox.
 */
void
send_mbox_message(spe_context_ptr_t ctx, unsigned int msg)
{
   spe_in_mbox_write(ctx, &msg, 1, SPE_MBOX_ALL_BLOCKING);
}


/**
 * Wait for a 1-word message to arrive in given mailbox.
 */
uint
wait_mbox_message(spe_context_ptr_t ctx)
{
   do {
      unsigned data;
      int count = spe_out_mbox_read(ctx, &data, 1);

      if (count == 1) {
	 return data;
      }
      
      if (count < 0) {
	 /* error */ ;
      }
   } while (1);
}


/**
 * Called by pthread_create() to spawn an SPU thread.
 */
static void *
cell_thread_function(void *arg)
{
   struct cell_init_info *init = (struct cell_init_info *) arg;
   unsigned entry = SPE_DEFAULT_ENTRY;

   ASSERT_ALIGN16(init);

   if (spe_context_run(cell_global.spe_contexts[init->id], &entry, 0,
                       init, NULL, NULL) < 0) {
      fprintf(stderr, "spe_context_run() failed\n");
      exit(1);
   }

   pthread_exit(NULL);
}


/**
 * Create the SPU threads.  This is done once during driver initialization.
 * This involves setting the the "init" message which is sent to each SPU.
 * The init message specifies an SPU id, total number of SPUs, location
 * and number of batch buffers, etc.
 */
void
cell_start_spus(struct cell_context *cell)
{
   static boolean one_time_init = FALSE;
   uint i, j;

   if (one_time_init) {
      fprintf(stderr, "PPU: Multiple rendering contexts not yet supported "
	      "on Cell.\n");
      abort();
   }

   one_time_init = TRUE;

   assert(cell->num_spus <= MAX_SPUS);

   ASSERT_ALIGN16(&cell_global.command[0]);
   ASSERT_ALIGN16(&cell_global.command[1]);

   ASSERT_ALIGN16(&cell_global.inits[0]);
   ASSERT_ALIGN16(&cell_global.inits[1]);

   for (i = 0; i < cell->num_spus; i++) {
      cell_global.inits[i].id = i;
      cell_global.inits[i].num_spus = cell->num_spus;
      cell_global.inits[i].debug_flags = cell->debug_flags;
      cell_global.inits[i].cmd = &cell_global.command[i];
      for (j = 0; j < CELL_NUM_BUFFERS; j++) {
         cell_global.inits[i].buffers[j] = cell->buffer[j];
      }
      cell_global.inits[i].buffer_status = &cell->buffer_status[0][0][0];

      cell_global.spe_contexts[i] = spe_context_create(0, NULL);
      if (!cell_global.spe_contexts[i]) {
         fprintf(stderr, "spe_context_create() failed\n");
         exit(1);
      }

      if (spe_program_load(cell_global.spe_contexts[i], &g3d_spu)) {
         fprintf(stderr, "spe_program_load() failed\n");
         exit(1);
      }
      
      pthread_create(&cell_global.spe_threads[i], /* returned thread handle */
                     NULL,                        /* pthread attribs */
                     &cell_thread_function,       /* start routine */
		     &cell_global.inits[i]);      /* thread argument */
   }
}


/**
 * Tell all the SPUs to stop/exit.
 * This is done when the driver's exiting / cleaning up.
 */
void
cell_spu_exit(struct cell_context *cell)
{
   uint i;

   for (i = 0; i < cell->num_spus; i++) {
      send_mbox_message(cell_global.spe_contexts[i], CELL_CMD_EXIT);
   }

   /* wait for threads to exit */
   for (i = 0; i < cell->num_spus; i++) {
      void *value;
      pthread_join(cell_global.spe_threads[i], &value);
      cell_global.spe_threads[i] = 0;
      cell_global.spe_contexts[i] = 0;
   }
}