summaryrefslogtreecommitdiffstats
path: root/drivers/ap1000/ringbuf.c
blob: ee33a03d4cea47587d05831dcd6577751b5f4555 (plain)
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
  /*
   * Copyright 1996 The Australian National University.
   * Copyright 1996 Fujitsu Laboratories Limited
   * 
   * This software may be distributed under the terms of the Gnu
   * Public License version 2 or later
  */
/*
 *  linux/drivers/ap1000/ringbuf.c
 *
 * This provides the /proc/XX/ringbuf interface to the Tnet ring buffer
 */
#define _APLIB_
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <linux/mm.h>

#include <asm/page.h>
#include <asm/segment.h>
#include <asm/io.h>
#include <asm/pgtable.h>
#include <asm/segment.h>
#include <asm/uaccess.h>

#include <asm/ap1000/pgtapmmu.h>
#include <asm/ap1000/apreg.h>
#include <asm/ap1000/apservice.h>


/* we have a small number of reserved ring buffers to ensure that at
   least one parallel program can always run */
#define RBUF_RESERVED 4
#define RBUF_RESERVED_ORDER 5
static struct {
	char *rb_ptr;
	char *shared_ptr;
	int used;
} reserved_ringbuf[RBUF_RESERVED];


void ap_ringbuf_init(void)
{
	int i,j;
	char *rb_ptr, *shared_ptr;
	int rb_size = PAGE_SIZE * (1<<RBUF_RESERVED_ORDER);

	/* preallocate some ringbuffers */
	for (i=0;i<RBUF_RESERVED;i++) {
		if (!(rb_ptr = (char *)__get_free_pages(GFP_ATOMIC,RBUF_RESERVED_ORDER,0))) {
			printk("failed to preallocate ringbuf %d\n",i);
			return;
		}
		for (j = MAP_NR(rb_ptr); j <= MAP_NR(rb_ptr+rb_size-1); j++) {
			set_bit(PG_reserved,&mem_map[j].flags);
		}

		if (!(shared_ptr = (char *)__get_free_page(GFP_ATOMIC))) {
			printk("failed to preallocate shared ptr %d\n",i);
                        return;
		}
		set_bit(PG_reserved,&mem_map[MAP_NR(shared_ptr)].flags);

		reserved_ringbuf[i].used = 0;
		reserved_ringbuf[i].rb_ptr = rb_ptr;
		reserved_ringbuf[i].shared_ptr = shared_ptr;
	}
}



void exit_ringbuf(struct task_struct *tsk)
{
	int i;
	
	if (!tsk->ringbuf) return;
	
	if (tsk->ringbuf->ringbuf) {
		char *rb_ptr = tsk->ringbuf->ringbuf;
		char *shared_ptr = tsk->ringbuf->shared;
		int order = tsk->ringbuf->order;
		int rb_size = PAGE_SIZE * (1<<order);

		for (i=0;i<RBUF_RESERVED;i++)
			if (rb_ptr == reserved_ringbuf[i].rb_ptr) break;
		
		if (i < RBUF_RESERVED) {
			reserved_ringbuf[i].used = 0;
		} else {
			for (i = MAP_NR(rb_ptr); i <= MAP_NR(rb_ptr+rb_size-1); i++) {
				clear_bit(PG_reserved, &mem_map[i].flags);
			}
			free_pages((unsigned)rb_ptr,order);

			i = MAP_NR(shared_ptr);
			clear_bit(PG_reserved,&mem_map[i]);
			free_page((unsigned)shared_ptr);
		}
	}

	kfree_s(tsk->ringbuf,sizeof(*(tsk->ringbuf)));
	tsk->ringbuf = NULL;
}


/*
 * map the ring buffer into users memory
 */
static int cap_map(int rb_size)
{
	struct task_struct *tsk=current;
	int i;
	char *rb_ptr=NULL;
	char *shared_ptr=NULL;
	int order = 0;
	int error,old_uid;

	error = verify_area(VERIFY_WRITE,(char *)RBUF_VBASE,rb_size);
	if (error) return error;
	
	if (!MPP_IS_PAR_TASK(tsk->taskid)) {
		printk("ringbuf_mmap called from non-parallel task\n");
		return -EINVAL;
	}

	
	if (tsk->ringbuf) return -EINVAL;

	rb_size -= RBUF_RING_BUFFER_OFFSET;
	rb_size >>= 1;

	switch (rb_size/1024) {
	case 128:
		order = 5;
		break;
	case 512:
		order = 7;
		break;
	case 2048:
		order = 9;
		break;
	case 8192:
		order = 11;
		break;
	default:
		printk("ringbuf_mmap with invalid size %d\n",rb_size);
		return -EINVAL;
	}
	  
	if (order == RBUF_RESERVED_ORDER) {
		for (i=0;i<RBUF_RESERVED;i++) 
			if (!reserved_ringbuf[i].used) {
				rb_ptr = reserved_ringbuf[i].rb_ptr;
				shared_ptr = reserved_ringbuf[i].shared_ptr;
				reserved_ringbuf[i].used = 1;
				break;
			}
	}
	  
	if (!rb_ptr) {
		rb_ptr = (char *)__get_free_pages(GFP_USER,order,0);
		if (!rb_ptr) return -ENOMEM;

		for (i = MAP_NR(rb_ptr); i <= MAP_NR(rb_ptr+rb_size-1); i++) {
			set_bit(PG_reserved,&mem_map[i].flags);
		}
		  
		shared_ptr = (char *)__get_free_page(GFP_USER);
		if (!shared_ptr)
			return -ENOMEM;
		set_bit(PG_reserved,&mem_map[MAP_NR(shared_ptr)].flags);
	}

	if (!rb_ptr)
		return -ENOMEM;
  
	memset(rb_ptr,0,rb_size);
	memset(shared_ptr,0,PAGE_SIZE);

	if (remap_page_range(RBUF_VBASE + RBUF_RING_BUFFER_OFFSET, 
			     mmu_v2p((unsigned)rb_ptr),
			     rb_size,APMMU_PAGE_SHARED))
		return -EAGAIN;

	if (remap_page_range(RBUF_VBASE + RBUF_RING_BUFFER_OFFSET + rb_size, 
			     mmu_v2p((unsigned)rb_ptr),
			     rb_size,APMMU_PAGE_SHARED))
		return -EAGAIN;
  
	/* the shared area */
	if (remap_page_range(RBUF_VBASE + RBUF_SHARED_PAGE_OFF,
			     mmu_v2p((unsigned)shared_ptr),
			     PAGE_SIZE,APMMU_PAGE_SHARED))
		return -EAGAIN;

#if 0
	/* lock the ringbuffer in memory */
	old_uid = current->euid;
	current->euid = 0;
	error = sys_mlock(RBUF_VBASE,2*rb_size+RBUF_RING_BUFFER_OFFSET);
	current->euid = old_uid;
	if (error) {
		printk("ringbuffer mlock failed\n");
		return error;
	}
#endif

	/* the queue pages */
#define MAP_QUEUE(offset,phys) \
	io_remap_page_range(RBUF_VBASE + offset, \
			    phys<<PAGE_SHIFT,PAGE_SIZE,APMMU_PAGE_SHARED,0xa)
	
	MAP_QUEUE(RBUF_PUT_QUEUE,  0x00000);
	MAP_QUEUE(RBUF_GET_QUEUE,  0x00001);
	MAP_QUEUE(RBUF_SEND_QUEUE, 0x00040);
	
	MAP_QUEUE(RBUF_XY_QUEUE,   0x00640);
	MAP_QUEUE(RBUF_X_QUEUE,    0x00240);
	MAP_QUEUE(RBUF_Y_QUEUE,    0x00440);
	MAP_QUEUE(RBUF_XYG_QUEUE,  0x00600);
	MAP_QUEUE(RBUF_XG_QUEUE,   0x00200);
	MAP_QUEUE(RBUF_YG_QUEUE,   0x00400);  
	MAP_QUEUE(RBUF_CSI_QUEUE,  0x02004);  
	MAP_QUEUE(RBUF_FOP_QUEUE,  0x02005);  

#undef MAP_QUEUE

	if (!tsk->ringbuf) {
		tsk->ringbuf = (void *)kmalloc(sizeof(*(tsk->ringbuf)),GFP_ATOMIC);
		if (!tsk->ringbuf)
			return -ENOMEM;    
	}
  
	memset(tsk->ringbuf,0,sizeof(*tsk->ringbuf));
	tsk->ringbuf->ringbuf = rb_ptr;
	tsk->ringbuf->shared = shared_ptr;
	tsk->ringbuf->order = order;
	tsk->ringbuf->write_ptr = mmu_v2p((unsigned)rb_ptr)<<1;
	tsk->ringbuf->vaddr = RBUF_VBASE;
  
	memset(tsk->ringbuf->vaddr+RBUF_SHARED_PAGE_OFF,0,PAGE_SIZE);
	{
		struct _kernel_cap_shared *_kernel = 
			(struct _kernel_cap_shared *)tsk->ringbuf->vaddr;
		_kernel->rbuf_read_ptr = (rb_size>>5) - 1;
	}
  
	return 0;
}


static int 
ringbuf_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
	int numcells, *phys_cells;      
	extern struct cap_init cap_init;

	switch (cmd) {
	case CAP_GETINIT:
		if (copy_to_user((char *)arg,(char *)&cap_init,sizeof(cap_init)))
			return -EFAULT;
		break;

	case CAP_SYNC:
		if (verify_area(VERIFY_READ, (void *) arg, sizeof(int)*2))
			return -EFAULT;
		if (get_user(numcells,(int *)arg)) return -EFAULT;
		if (get_user((unsigned)phys_cells,
			     ((int *)arg)+1)) return -EFAULT;
		if (verify_area(VERIFY_READ,phys_cells,sizeof(int)*numcells))
			return -EFAULT;
		return ap_sync(numcells,phys_cells);
		break;

	case CAP_SETGANG:
		{
			int v;
			if (get_user(v,(int *)arg)) return -EFAULT;
			mpp_set_gang_factor(v);
			break;
		}

	case CAP_MAP:
		return cap_map(arg);
		
	default:
		printk("unknown ringbuf ioctl %d\n",cmd);
		return -EINVAL;
	}
	return 0;
}


static struct file_operations proc_ringbuf_operations = {
	NULL,
	NULL,
	NULL,
	NULL,		/* readdir */
	NULL,		/* poll */
	ringbuf_ioctl,	/* ioctl */
	NULL,           /* mmap */
	NULL,		/* no special open code */
	NULL,		/* no special release code */
	NULL		/* can't fsync */
};

struct inode_operations proc_ringbuf_inode_operations = {
	&proc_ringbuf_operations,	/* default base directory file-ops */
	NULL,			/* create */
	NULL,			/* lookup */
	NULL,			/* link */
	NULL,			/* unlink */
	NULL,			/* symlink */
	NULL,			/* mkdir */
	NULL,			/* rmdir */
	NULL,			/* mknod */
	NULL,			/* rename */
	NULL,			/* readlink */
	NULL,			/* readpage */
	NULL,			/* writepage */
	NULL,			/* bmap */
	NULL,			/* truncate */
	NULL			/* permission */
};