Merge branch '7.8'
[mesa.git] / src / gallium / drivers / cell / ppu / cell_spu.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 /**
30 * Utility/wrappers for communicating with the SPUs.
31 */
32
33
34 #include <pthread.h>
35
36 #include "cell_spu.h"
37 #include "pipe/p_format.h"
38 #include "pipe/p_state.h"
39 #include "util/u_memory.h"
40 #include "cell/common.h"
41
42
43 /*
44 helpful headers:
45 /opt/ibm/cell-sdk/prototype/src/include/ppu/cbe_mfc.h
46 */
47
48
49 /**
50 * Cell/SPU info that's not per-context.
51 */
52 struct cell_global_info cell_global;
53
54
55 /**
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...
59 */
60 static unsigned
61 get_timebase(void)
62 {
63 FILE *f = fopen("/proc/cpuinfo", "r");
64 unsigned timebase;
65
66 assert(f);
67 while (!feof(f)) {
68 char line[80];
69 fgets(line, sizeof(line), f);
70 if (strncmp(line, "timebase", 8) == 0) {
71 char *colon = strchr(line, ':');
72 if (colon) {
73 timebase = atoi(colon + 2);
74 break;
75 }
76 }
77 }
78 fclose(f);
79
80 return timebase;
81 }
82
83
84 /**
85 * Write a 1-word message to the given SPE mailbox.
86 */
87 void
88 send_mbox_message(spe_context_ptr_t ctx, unsigned int msg)
89 {
90 spe_in_mbox_write(ctx, &msg, 1, SPE_MBOX_ALL_BLOCKING);
91 }
92
93
94 /**
95 * Wait for a 1-word message to arrive in given mailbox.
96 */
97 uint
98 wait_mbox_message(spe_context_ptr_t ctx)
99 {
100 do {
101 unsigned data;
102 int count = spe_out_mbox_read(ctx, &data, 1);
103
104 if (count == 1) {
105 return data;
106 }
107
108 if (count < 0) {
109 /* error */ ;
110 }
111 } while (1);
112 }
113
114
115 /**
116 * Called by pthread_create() to spawn an SPU thread.
117 */
118 static void *
119 cell_thread_function(void *arg)
120 {
121 struct cell_init_info *init = (struct cell_init_info *) arg;
122 unsigned entry = SPE_DEFAULT_ENTRY;
123
124 ASSERT_ALIGN16(init);
125
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");
129 exit(1);
130 }
131
132 pthread_exit(NULL);
133 }
134
135
136 /**
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.
141 */
142 void
143 cell_start_spus(struct cell_context *cell)
144 {
145 static boolean one_time_init = FALSE;
146 uint i, j;
147 uint timebase = get_timebase();
148
149 if (one_time_init) {
150 fprintf(stderr, "PPU: Multiple rendering contexts not yet supported "
151 "on Cell.\n");
152 abort();
153 }
154
155 one_time_init = TRUE;
156
157 assert(cell->num_spus <= CELL_MAX_SPUS);
158
159 ASSERT_ALIGN16(&cell_global.inits[0]);
160 ASSERT_ALIGN16(&cell_global.inits[1]);
161
162 /*
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().
166 */
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;
172
173 for (j = 0; j < CELL_NUM_BUFFERS; j++) {
174 cell_global.inits[i].buffers[j] = cell->buffer[j];
175 }
176 cell_global.inits[i].buffer_status = &cell->buffer_status[0][0][0];
177
178 cell_global.inits[i].spu_functions = &cell->spu_functions;
179
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");
183 exit(1);
184 }
185
186 if (spe_program_load(cell_global.spe_contexts[i], &g3d_spu)) {
187 fprintf(stderr, "spe_program_load() failed\n");
188 exit(1);
189 }
190
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 */
195 }
196 }
197
198
199 /**
200 * Tell all the SPUs to stop/exit.
201 * This is done when the driver's exiting / cleaning up.
202 */
203 void
204 cell_spu_exit(struct cell_context *cell)
205 {
206 uint i;
207
208 for (i = 0; i < cell->num_spus; i++) {
209 send_mbox_message(cell_global.spe_contexts[i], CELL_CMD_EXIT);
210 }
211
212 /* wait for threads to exit */
213 for (i = 0; i < cell->num_spus; i++) {
214 void *value;
215 pthread_join(cell_global.spe_threads[i], &value);
216 cell_global.spe_threads[i] = 0;
217 cell_global.spe_contexts[i] = 0;
218 }
219 }