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 "cell/common.h"
44 /opt/ibm/cell-sdk/prototype/src/include/ppu/cbe_mfc.h
49 * Cell/SPU info that's not per-context.
51 struct cell_global_info cell_global
;
55 * Write a 1-word message to the given SPE mailbox.
58 send_mbox_message(spe_context_ptr_t ctx
, unsigned int msg
)
60 spe_in_mbox_write(ctx
, &msg
, 1, SPE_MBOX_ALL_BLOCKING
);
65 * Wait for a 1-word message to arrive in given mailbox.
68 wait_mbox_message(spe_context_ptr_t ctx
)
72 int count
= spe_out_mbox_read(ctx
, &data
, 1);
86 * Called by pthread_create() to spawn an SPU thread.
89 cell_thread_function(void *arg
)
91 struct cell_init_info
*init
= (struct cell_init_info
*) arg
;
92 unsigned entry
= SPE_DEFAULT_ENTRY
;
96 if (spe_context_run(cell_global
.spe_contexts
[init
->id
], &entry
, 0,
97 init
, NULL
, NULL
) < 0) {
98 fprintf(stderr
, "spe_context_run() failed\n");
107 * Create the SPU threads. This is done once during driver initialization.
108 * This involves setting the the "init" message which is sent to each SPU.
109 * The init message specifies an SPU id, total number of SPUs, location
110 * and number of batch buffers, etc.
113 cell_start_spus(struct cell_context
*cell
)
115 static boolean one_time_init
= FALSE
;
119 fprintf(stderr
, "PPU: Multiple rendering contexts not yet supported "
124 one_time_init
= TRUE
;
126 assert(cell
->num_spus
<= MAX_SPUS
);
128 ASSERT_ALIGN16(&cell_global
.command
[0]);
129 ASSERT_ALIGN16(&cell_global
.command
[1]);
131 ASSERT_ALIGN16(&cell_global
.inits
[0]);
132 ASSERT_ALIGN16(&cell_global
.inits
[1]);
134 for (i
= 0; i
< cell
->num_spus
; i
++) {
135 cell_global
.inits
[i
].id
= i
;
136 cell_global
.inits
[i
].num_spus
= cell
->num_spus
;
137 cell_global
.inits
[i
].debug_flags
= cell
->debug_flags
;
138 cell_global
.inits
[i
].cmd
= &cell_global
.command
[i
];
139 for (j
= 0; j
< CELL_NUM_BUFFERS
; j
++) {
140 cell_global
.inits
[i
].buffers
[j
] = cell
->buffer
[j
];
142 cell_global
.inits
[i
].buffer_status
= &cell
->buffer_status
[0][0][0];
144 cell_global
.spe_contexts
[i
] = spe_context_create(0, NULL
);
145 if (!cell_global
.spe_contexts
[i
]) {
146 fprintf(stderr
, "spe_context_create() failed\n");
150 if (spe_program_load(cell_global
.spe_contexts
[i
], &g3d_spu
)) {
151 fprintf(stderr
, "spe_program_load() failed\n");
155 pthread_create(&cell_global
.spe_threads
[i
], /* returned thread handle */
156 NULL
, /* pthread attribs */
157 &cell_thread_function
, /* start routine */
158 &cell_global
.inits
[i
]); /* thread argument */
164 * Tell all the SPUs to stop/exit.
165 * This is done when the driver's exiting / cleaning up.
168 cell_spu_exit(struct cell_context
*cell
)
172 for (i
= 0; i
< cell
->num_spus
; i
++) {
173 send_mbox_message(cell_global
.spe_contexts
[i
], CELL_CMD_EXIT
);
176 /* wait for threads to exit */
177 for (i
= 0; i
< cell
->num_spus
; i
++) {
179 pthread_join(cell_global
.spe_threads
[i
], &value
);
180 cell_global
.spe_threads
[i
] = 0;
181 cell_global
.spe_contexts
[i
] = 0;