1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Utility/wrappers for communicating with the SPUs.
37 #include "pipe/p_format.h"
38 #include "pipe/p_state.h"
39 #include "util/u_memory.h"
40 #include "cell/common.h"
45 /opt/ibm/cell-sdk/prototype/src/include/ppu/cbe_mfc.h
50 * Cell/SPU info that's not per-context.
52 struct cell_global_info cell_global;
56 * Scan /proc/cpuinfo to determine the timebase for the system.
57 * This is used by the SPUs to convert 'decrementer' ticks to seconds.
58 * There may be a better way to get this value...
63 FILE *f = fopen("/proc/cpuinfo", "r");
69 fgets(line, sizeof(line), f);
70 if (strncmp(line, "timebase", 8) == 0) {
71 char *colon = strchr(line, ':');
73 timebase = atoi(colon + 2);
85 * Write a 1-word message to the given SPE mailbox.
88 send_mbox_message(spe_context_ptr_t ctx, unsigned int msg)
90 spe_in_mbox_write(ctx, &msg, 1, SPE_MBOX_ALL_BLOCKING);
95 * Wait for a 1-word message to arrive in given mailbox.
98 wait_mbox_message(spe_context_ptr_t ctx)
102 int count = spe_out_mbox_read(ctx, &data, 1);
116 * Called by pthread_create() to spawn an SPU thread.
119 cell_thread_function(void *arg)
121 struct cell_init_info *init = (struct cell_init_info *) arg;
122 unsigned entry = SPE_DEFAULT_ENTRY;
124 ASSERT_ALIGN16(init);
126 if (spe_context_run(cell_global.spe_contexts[init->id], &entry, 0,
127 init, NULL, NULL) < 0) {
128 fprintf(stderr, "spe_context_run() failed\n");
137 * Create the SPU threads. This is done once during driver initialization.
138 * This involves setting the "init" message which is sent to each SPU.
139 * The init message specifies an SPU id, total number of SPUs, location
140 * and number of batch buffers, etc.
143 cell_start_spus(struct cell_context *cell)
145 static boolean one_time_init = FALSE;
147 uint timebase = get_timebase();
150 fprintf(stderr, "PPU: Multiple rendering contexts not yet supported "
155 one_time_init = TRUE;
157 assert(cell->num_spus <= CELL_MAX_SPUS);
159 ASSERT_ALIGN16(&cell_global.inits[0]);
160 ASSERT_ALIGN16(&cell_global.inits[1]);
163 * Initialize the global 'inits' structure for each SPU.
164 * A pointer to the init struct will be passed to each SPU.
165 * The SPUs will then each grab their init info with mfc_get().
167 for (i = 0; i < cell->num_spus; i++) {
168 cell_global.inits[i].id = i;
169 cell_global.inits[i].num_spus = cell->num_spus;
170 cell_global.inits[i].debug_flags = cell->debug_flags;
171 cell_global.inits[i].inv_timebase = 1000.0f / timebase;
173 for (j = 0; j < CELL_NUM_BUFFERS; j++) {
174 cell_global.inits[i].buffers[j] = cell->buffer[j];
176 cell_global.inits[i].buffer_status = &cell->buffer_status[0][0][0];
178 cell_global.inits[i].spu_functions = &cell->spu_functions;
180 cell_global.spe_contexts[i] = spe_context_create(0, NULL);
181 if (!cell_global.spe_contexts[i]) {
182 fprintf(stderr, "spe_context_create() failed\n");
186 if (spe_program_load(cell_global.spe_contexts[i], &g3d_spu)) {
187 fprintf(stderr, "spe_program_load() failed\n");
191 pthread_create(&cell_global.spe_threads[i], /* returned thread handle */
192 NULL, /* pthread attribs */
193 &cell_thread_function, /* start routine */
194 &cell_global.inits[i]); /* thread argument */
200 * Tell all the SPUs to stop/exit.
201 * This is done when the driver's exiting / cleaning up.
204 cell_spu_exit(struct cell_context *cell)
208 for (i = 0; i < cell->num_spus; i++) {
209 send_mbox_message(cell_global.spe_contexts[i], CELL_CMD_EXIT);
212 /* wait for threads to exit */
213 for (i = 0; i < cell->num_spus; i++) {
215 pthread_join(cell_global.spe_threads[i], &value);
216 cell_global.spe_threads[i] = 0;
217 cell_global.spe_contexts[i] = 0;