File: dma.c

package info (click to toggle)
libspe2 2.2.80-95-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 940 kB
  • ctags: 1,214
  • sloc: ansic: 9,316; makefile: 551; ada: 448; sh: 24
file content (382 lines) | stat: -rw-r--r-- 10,885 bytes parent folder | download
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * libspe2 - A wrapper library to adapt the JSRE SPU usage model to SPUFS
 * Copyright (C) 2005 IBM Corp.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include <sys/poll.h>

#include "create.h"
#include "dma.h"

static int spe_read_tag_status_block(spe_context_ptr_t spectx, unsigned int mask, unsigned int *tag_status);
static int spe_read_tag_status_noblock(spe_context_ptr_t spectx, unsigned int mask, unsigned int *tag_status);

static int issue_mfc_command(spe_context_ptr_t spectx, unsigned lsa, void *ea,
			     unsigned size, unsigned tag, unsigned tid, unsigned rid,
			     enum mfc_cmd cmd)
{
	int ret;

	DEBUG_PRINTF("queuing DMA %x %lx %x %x %x %x %x\n", lsa,
		(unsigned long)ea, size, tag, tid, rid, (unsigned)cmd);

	/* tag 16-31 are reserved by kernel */
	if (tag > 0x0f || tid > 0xff || rid > 0xff) {
		errno = EINVAL;
		return -1;
	}

	if (spectx->base_private->flags & SPE_MAP_PS) {
		volatile struct spe_mfc_command_area *cmd_area =
			spectx->base_private->mfc_mmap_base;
		unsigned int eal = (uintptr_t) ea & 0xFFFFFFFF;
		unsigned int eah = (unsigned long long)(uintptr_t) ea >> 32;
		_base_spe_context_lock(spectx, FD_MFC);
		spectx->base_private->active_tagmask |= 1 << tag;
		DEBUG_PRINTF("set active tagmask = 0x%04x, tag=%i\n",spectx->base_private->active_tagmask,tag);
		while ((cmd_area->MFC_QStatus & 0x0000FFFF) == 0) ;
		do {
			cmd_area->MFC_LSA         = lsa;
			cmd_area->MFC_EAH         = eah;
			cmd_area->MFC_EAL         = eal;
			cmd_area->MFC_Size_Tag    = (size << 16) | tag;
			cmd_area->MFC_ClassID_CMD = (tid << 24) | (rid << 16) | cmd;

			ret = cmd_area->MFC_CMDStatus & 0x00000003;
		} while (ret); // at least one of the two bits is set
		_base_spe_context_unlock(spectx, FD_MFC);
		return 0;
	}
	else {
		int fd;
		fd = _base_spe_open_if_closed(spectx, FD_MFC, 0);
		if (fd != -1) {
			struct mfc_command_parameter_area parm = {
				.lsa   = lsa,
				.ea    = (unsigned long) ea,
				.size  = size,
				.tag   = tag,
				.class = (tid << 8) | rid,
				.cmd   = cmd,
			};
			ret = write(fd, &parm, sizeof (parm));
			if ((ret < 0) && (errno != EIO)) {
				perror("spe_do_mfc_put: internal error");
			}
			return ret < 0 ? -1 : 0;
		}
	}
	/* the kernel does not support DMA */
	return 1;
}

static int spe_do_mfc_put(spe_context_ptr_t spectx, unsigned src, void *dst,
			  unsigned size, unsigned tag, unsigned tid, unsigned rid,
			  enum mfc_cmd cmd)
{
	int ret;
	ret = issue_mfc_command(spectx, src, dst, size, tag, tid, rid, cmd);
	if (ret <= 0) {
		return ret;
	}
	else {
		/* the kernel does not support DMA, so just copy directly */
		memcpy(dst, spectx->base_private->mem_mmap_base + src, size);
		return 0;
	}
}

static int spe_do_mfc_get(spe_context_ptr_t spectx, unsigned int dst, void *src,
			  unsigned int size, unsigned int tag, unsigned tid, unsigned rid,
			  enum mfc_cmd cmd)
{
	int ret;
	ret = issue_mfc_command(spectx, dst, src, size, tag, tid, rid, cmd);
	if (ret <= 0) {
		return ret;
	}
	else {
		/* the kernel does not support DMA, so just copy directly */
		memcpy(spectx->base_private->mem_mmap_base + dst, src, size);
		return 0;
	}
}

int _base_spe_mfcio_put(spe_context_ptr_t spectx, 
                        unsigned int ls, 
                        void *ea, 
                        unsigned int size, 
                        unsigned int tag, 
                        unsigned int tid, 
                        unsigned int rid)
{
	return spe_do_mfc_put(spectx, ls, ea, size, tag, tid, rid, MFC_CMD_PUT);
}

int _base_spe_mfcio_putb(spe_context_ptr_t spectx, 
                        unsigned int ls, 
                        void *ea, 
                        unsigned int size, 
                        unsigned int tag, 
                        unsigned int tid, 
                        unsigned int rid)
{
	return spe_do_mfc_put(spectx, ls, ea, size, tag, tid, rid, MFC_CMD_PUTB);
}

int _base_spe_mfcio_putf(spe_context_ptr_t spectx, 
                        unsigned int ls, 
                        void *ea, 
                        unsigned int size, 
                        unsigned int tag, 
                        unsigned int tid, 
                        unsigned int rid)
{
	return spe_do_mfc_put(spectx, ls, ea, size, tag, tid, rid, MFC_CMD_PUTF);
}


int _base_spe_mfcio_get(spe_context_ptr_t spectx, 
                        unsigned int ls, 
                        void *ea, 
                        unsigned int size, 
                        unsigned int tag, 
                        unsigned int tid, 
                        unsigned int rid)
{
	return spe_do_mfc_get(spectx, ls, ea, size, tag, tid, rid, MFC_CMD_GET);
}

int _base_spe_mfcio_getb(spe_context_ptr_t spectx, 
                        unsigned int ls, 
                        void *ea, 
                        unsigned int size, 
                        unsigned int tag, 
                        unsigned int tid, 
                        unsigned int rid)
{
	return spe_do_mfc_get(spectx, ls, ea, size, tag, rid, rid, MFC_CMD_GETB);
}

int _base_spe_mfcio_getf(spe_context_ptr_t spectx, 
                        unsigned int ls, 
                        void *ea, 
                        unsigned int size, 
                        unsigned int tag, 
                        unsigned int tid, 
                        unsigned int rid)
{
	return spe_do_mfc_get(spectx, ls, ea, size, tag, tid, rid, MFC_CMD_GETF);
}

static int spe_mfcio_tag_status_read_all(spe_context_ptr_t spectx, 
                        unsigned int mask, unsigned int *tag_status)
{
	int fd;

	if (spectx->base_private->flags & SPE_MAP_PS) {
		return spe_read_tag_status_block(spectx, mask, tag_status);
	} else {
		fd = _base_spe_open_if_closed(spectx, FD_MFC, 0);
		if (fd == -1) {
			return -1;
		}

		if (fsync(fd) != 0) {
			return -1;
		}

		return spe_read_tag_status_block(spectx, mask, tag_status);
	}
}

static int spe_mfcio_tag_status_read_any(spe_context_ptr_t spectx,
					unsigned int mask, unsigned int *tag_status)
{
	return spe_read_tag_status_block(spectx, mask, tag_status);
}

static int spe_mfcio_tag_status_read_immediate(spe_context_ptr_t spectx,
						     unsigned int mask, unsigned int *tag_status)
{
	return spe_read_tag_status_noblock(spectx, mask, tag_status);
}



/* MFC Read tag status functions
 *
 */
static int spe_read_tag_status_block(spe_context_ptr_t spectx, unsigned int mask, unsigned int *tag_status)
{
	if (spectx->base_private->flags & SPE_MAP_PS) {
		volatile struct spe_mfc_command_area *cmd_area =
			spectx->base_private->mfc_mmap_base;
		_base_spe_context_lock(spectx, FD_MFC);
		cmd_area->Prxy_QueryMask = mask;
		__asm__ ("eieio");
		do {
			*tag_status = cmd_area->Prxy_TagStatus;
			spectx->base_private->active_tagmask ^= *tag_status;
			DEBUG_PRINTF("unset active tagmask = 0x%04x, tag_status = 0x%04x\n",
							spectx->base_private->active_tagmask,*tag_status);
		} while (*tag_status ^ mask);
		_base_spe_context_unlock(spectx, FD_MFC);
		return 0;
	} else {
		int fd;
		fd = _base_spe_open_if_closed(spectx, FD_MFC, 0);
		if (fd == -1) {
			return -1;
		}
		
		if (read(fd,tag_status,4) == 4) {
			return 0;
		}
	}
	return -1;
}

static int spe_read_tag_status_noblock(spe_context_ptr_t spectx, unsigned int mask, unsigned int *tag_status)
{
	unsigned int ret;

	if (spectx->base_private->flags & SPE_MAP_PS) {
		volatile struct spe_mfc_command_area *cmd_area =
			spectx->base_private->mfc_mmap_base;

		_base_spe_context_lock(spectx, FD_MFC);
		cmd_area->Prxy_QueryMask = mask;
		__asm__ ("eieio");
		*tag_status =  cmd_area->Prxy_TagStatus;
		spectx->base_private->active_tagmask ^= *tag_status;
		DEBUG_PRINTF("unset active tagmask = 0x%04x, tag_status = 0x%04x\n",
						spectx->base_private->active_tagmask,*tag_status);
		_base_spe_context_unlock(spectx, FD_MFC);
		return 0;
	} else {
		struct pollfd poll_fd;
		int fd;

		fd = _base_spe_open_if_closed(spectx, FD_MFC, 0);
		if (fd == -1) {
			return -1;
		}
		
		poll_fd.fd = fd;
		poll_fd.events = POLLIN;

		ret = poll(&poll_fd, 1, 0);

		if (ret < 0)
			return -1;

		if (ret == 0 || !(poll_fd.revents & POLLIN)) {
			*tag_status = 0;
			return 0;
		}
	
		if (read(fd,tag_status,4) == 4) {
			return 0;
		}
	}
	return -1;
}

int _base_spe_mfcio_tag_status_read(spe_context_ptr_t spectx, unsigned int mask, unsigned int behavior, unsigned int *tag_status)
{
	if ( mask != 0 ) {
		if (!(spectx->base_private->flags & SPE_MAP_PS)) 
			mask = 0;
	} else {
		if ((spectx->base_private->flags & SPE_MAP_PS))
			mask = spectx->base_private->active_tagmask;
	}

	if (!tag_status) {
		errno = EINVAL;
		return -1;
	}

	switch (behavior) {
	case SPE_TAG_ALL:
		return spe_mfcio_tag_status_read_all(spectx, mask, tag_status);
	case SPE_TAG_ANY:
		return spe_mfcio_tag_status_read_any(spectx, mask, tag_status);
	case SPE_TAG_IMMEDIATE:
		return spe_mfcio_tag_status_read_immediate(spectx, mask, tag_status);
	default:
		errno = EINVAL;
		return -1;
	}
}

int _base_spe_mssync_start(spe_context_ptr_t spectx)
{
	int ret, fd;
	unsigned int data = 1; /* Any value can be written here */

	volatile struct spe_mssync_area *mss_area = 
                spectx->base_private->mssync_mmap_base;

	if (spectx->base_private->flags & SPE_MAP_PS) {
		mss_area->MFC_MSSync = data; 
		return 0;
	} else {
		fd = _base_spe_open_if_closed(spectx, FD_MSS, 0);
		if (fd != -1) {
			ret = write(fd, &data, sizeof (data));
			if ((ret < 0) && (errno != EIO)) {
				perror("spe_mssync_start: internal error");
			}
			return ret < 0 ? -1 : 0;
		} else 
			return -1;
	}
}

int _base_spe_mssync_status(spe_context_ptr_t spectx)
{
	int ret, fd;
	unsigned int data;

	volatile struct spe_mssync_area *mss_area = 
                spectx->base_private->mssync_mmap_base;

	if (spectx->base_private->flags & SPE_MAP_PS) {
		return  mss_area->MFC_MSSync;
	} else {
		fd = _base_spe_open_if_closed(spectx, FD_MSS, 0);
		if (fd != -1) {
			ret = read(fd, &data, sizeof (data));
			if ((ret < 0) && (errno != EIO)) {
				perror("spe_mssync_start: internal error");
			}
			return ret < 0 ? -1 : data;
		} else 
			return -1;
	}
}