gallium: fix refcount bug introduced in eb20e2984
[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 "cell/common.h"
40
41
42 /*
43 helpful headers:
44 /opt/ibm/cell-sdk/prototype/src/include/ppu/cbe_mfc.h
45 */
46
47
48 /**
49 * Cell/SPU info that's not per-context.
50 */
51 struct cell_global_info cell_global;
52
53
54 /**
55 * Write a 1-word message to the given SPE mailbox.
56 */
57 void
58 send_mbox_message(spe_context_ptr_t ctx, unsigned int msg)
59 {
60 spe_in_mbox_write(ctx, &msg, 1, SPE_MBOX_ALL_BLOCKING);
61 }
62
63
64 /**
65 * Wait for a 1-word message to arrive in given mailbox.
66 */
67 uint
68 wait_mbox_message(spe_context_ptr_t ctx)
69 {
70 do {
71 unsigned data;
72 int count = spe_out_mbox_read(ctx, &data, 1);
73
74 if (count == 1) {
75 return data;
76 }
77
78 if (count < 0) {
79 /* error */ ;
80 }
81 } while (1);
82 }
83
84
85 /**
86 * Called by pthread_create() to spawn an SPU thread.
87 */
88 static void *
89 cell_thread_function(void *arg)
90 {
91 struct cell_init_info *init = (struct cell_init_info *) arg;
92 unsigned entry = SPE_DEFAULT_ENTRY;
93
94 ASSERT_ALIGN16(init);
95
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");
99 exit(1);
100 }
101
102 pthread_exit(NULL);
103 }
104
105
106 /**
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.
111 */
112 void
113 cell_start_spus(struct cell_context *cell)
114 {
115 static boolean one_time_init = FALSE;
116 uint i, j;
117
118 if (one_time_init) {
119 fprintf(stderr, "PPU: Multiple rendering contexts not yet supported "
120 "on Cell.\n");
121 abort();
122 }
123
124 one_time_init = TRUE;
125
126 assert(cell->num_spus <= MAX_SPUS);
127
128 ASSERT_ALIGN16(&cell_global.command[0]);
129 ASSERT_ALIGN16(&cell_global.command[1]);
130
131 ASSERT_ALIGN16(&cell_global.inits[0]);
132 ASSERT_ALIGN16(&cell_global.inits[1]);
133
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];
141 }
142 cell_global.inits[i].buffer_status = &cell->buffer_status[0][0][0];
143
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");
147 exit(1);
148 }
149
150 if (spe_program_load(cell_global.spe_contexts[i], &g3d_spu)) {
151 fprintf(stderr, "spe_program_load() failed\n");
152 exit(1);
153 }
154
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 */
159 }
160 }
161
162
163 /**
164 * Tell all the SPUs to stop/exit.
165 * This is done when the driver's exiting / cleaning up.
166 */
167 void
168 cell_spu_exit(struct cell_context *cell)
169 {
170 uint i;
171
172 for (i = 0; i < cell->num_spus; i++) {
173 send_mbox_message(cell_global.spe_contexts[i], CELL_CMD_EXIT);
174 }
175
176 /* wait for threads to exit */
177 for (i = 0; i < cell->num_spus; i++) {
178 void *value;
179 pthread_join(cell_global.spe_threads[i], &value);
180 cell_global.spe_threads[i] = 0;
181 cell_global.spe_contexts[i] = 0;
182 }
183 }