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#include <linux/module.h>
49#include <linux/interrupt.h>
50#include <linux/kernel.h>
51#include <linux/spinlock.h>
52#include <linux/miscdevice.h>
53#include <linux/platform_device.h>
54#include <linux/poll.h>
55#include <linux/sched.h>
56#include <linux/bitops.h>
57#include <linux/slab.h>
58#include <linux/io.h>
59
60
61
62
63
64
65
66#define PIPE_REG_COMMAND 0x00
67#define PIPE_REG_STATUS 0x04
68#define PIPE_REG_CHANNEL 0x08
69#define PIPE_REG_SIZE 0x0c
70#define PIPE_REG_ADDRESS 0x10
71#define PIPE_REG_WAKES 0x14
72#define PIPE_REG_PARAMS_ADDR_LOW 0x18
73#define PIPE_REG_PARAMS_ADDR_HIGH 0x1c
74#define PIPE_REG_ACCESS_PARAMS 0x20
75
76
77#define CMD_OPEN 1
78#define CMD_CLOSE 2
79#define CMD_POLL 3
80
81
82#define PIPE_POLL_IN (1 << 0)
83#define PIPE_POLL_OUT (1 << 1)
84#define PIPE_POLL_HUP (1 << 2)
85
86
87#define CMD_WRITE_BUFFER 4
88#define CMD_WAKE_ON_WRITE 5
89
90
91
92
93
94
95
96#define CMD_READ_BUFFER 6
97#define CMD_WAKE_ON_READ 7
98
99
100
101#define PIPE_ERROR_INVAL -1
102#define PIPE_ERROR_AGAIN -2
103#define PIPE_ERROR_NOMEM -3
104#define PIPE_ERROR_IO -4
105
106
107#define PIPE_WAKE_CLOSED (1 << 0)
108#define PIPE_WAKE_READ (1 << 1)
109#define PIPE_WAKE_WRITE (1 << 2)
110
111struct access_params {
112 u32 channel;
113 u32 size;
114 u32 address;
115 u32 cmd;
116 u32 result;
117
118 u32 flags;
119};
120
121
122
123
124
125struct goldfish_pipe_dev {
126 spinlock_t lock;
127 unsigned char __iomem *base;
128 struct access_params *aps;
129 int irq;
130};
131
132static struct goldfish_pipe_dev pipe_dev[1];
133
134
135struct goldfish_pipe {
136 struct goldfish_pipe_dev *dev;
137 struct mutex lock;
138 unsigned long flags;
139 wait_queue_head_t wake_queue;
140};
141
142
143
144enum {
145 BIT_CLOSED_ON_HOST = 0,
146 BIT_WAKE_ON_WRITE = 1,
147 BIT_WAKE_ON_READ = 2,
148};
149
150
151static u32 goldfish_cmd_status(struct goldfish_pipe *pipe, u32 cmd)
152{
153 unsigned long flags;
154 u32 status;
155 struct goldfish_pipe_dev *dev = pipe->dev;
156
157 spin_lock_irqsave(&dev->lock, flags);
158 writel((u32)pipe, dev->base + PIPE_REG_CHANNEL);
159 writel(cmd, dev->base + PIPE_REG_COMMAND);
160 status = readl(dev->base + PIPE_REG_STATUS);
161 spin_unlock_irqrestore(&dev->lock, flags);
162 return status;
163}
164
165static void goldfish_cmd(struct goldfish_pipe *pipe, u32 cmd)
166{
167 unsigned long flags;
168 struct goldfish_pipe_dev *dev = pipe->dev;
169
170 spin_lock_irqsave(&dev->lock, flags);
171 writel((u32)pipe, dev->base + PIPE_REG_CHANNEL);
172 writel(cmd, dev->base + PIPE_REG_COMMAND);
173 spin_unlock_irqrestore(&dev->lock, flags);
174}
175
176
177
178
179static int goldfish_pipe_error_convert(int status)
180{
181 switch (status) {
182 case PIPE_ERROR_AGAIN:
183 return -EAGAIN;
184 case PIPE_ERROR_NOMEM:
185 return -ENOMEM;
186 case PIPE_ERROR_IO:
187 return -EIO;
188 default:
189 return -EINVAL;
190 }
191}
192
193
194
195
196
197static int valid_batchbuffer_addr(struct goldfish_pipe_dev *dev,
198 struct access_params *aps)
199{
200 u32 aph, apl;
201 u64 paddr;
202 aph = readl(dev->base + PIPE_REG_PARAMS_ADDR_HIGH);
203 apl = readl(dev->base + PIPE_REG_PARAMS_ADDR_LOW);
204
205 paddr = ((u64)aph << 32) | apl;
206 if (paddr != (__pa(aps)))
207 return 0;
208 return 1;
209}
210
211
212static int setup_access_params_addr(struct platform_device *pdev,
213 struct goldfish_pipe_dev *dev)
214{
215 u64 paddr;
216 struct access_params *aps;
217
218 aps = devm_kzalloc(&pdev->dev, sizeof(struct access_params), GFP_KERNEL);
219 if (!aps)
220 return -1;
221
222
223 paddr = __pa(aps);
224 writel((u32)(paddr >> 32), dev->base + PIPE_REG_PARAMS_ADDR_HIGH);
225 writel((u32)paddr, dev->base + PIPE_REG_PARAMS_ADDR_LOW);
226
227 if (valid_batchbuffer_addr(dev, aps)) {
228 dev->aps = aps;
229 return 0;
230 } else
231 return -1;
232}
233
234
235#define INITIAL_BATCH_RESULT (0xdeadbeaf)
236static int access_with_param(struct goldfish_pipe_dev *dev, const int cmd,
237 unsigned long address, unsigned long avail,
238 struct goldfish_pipe *pipe, int *status)
239{
240 struct access_params *aps = dev->aps;
241
242 if (aps == NULL)
243 return -1;
244
245 aps->result = INITIAL_BATCH_RESULT;
246 aps->channel = (unsigned long)pipe;
247 aps->size = avail;
248 aps->address = address;
249 aps->cmd = cmd;
250 writel(cmd, dev->base + PIPE_REG_ACCESS_PARAMS);
251
252
253
254
255 if (aps->result == INITIAL_BATCH_RESULT)
256 return -1;
257 *status = aps->result;
258 return 0;
259}
260
261
262
263
264static ssize_t goldfish_pipe_read_write(struct file *filp, char __user *buffer,
265 size_t bufflen, int is_write)
266{
267 unsigned long irq_flags;
268 struct goldfish_pipe *pipe = filp->private_data;
269 struct goldfish_pipe_dev *dev = pipe->dev;
270 const int cmd_offset = is_write ? 0
271 : (CMD_READ_BUFFER - CMD_WRITE_BUFFER);
272 unsigned long address, address_end;
273 int ret = 0;
274
275
276 if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
277 return -EIO;
278
279
280 if (unlikely(bufflen) == 0)
281 return 0;
282
283
284 if (!access_ok(is_write ? VERIFY_WRITE : VERIFY_READ,
285 buffer, bufflen))
286 return -EFAULT;
287
288
289 if (mutex_lock_interruptible(&pipe->lock))
290 return -ERESTARTSYS;
291
292 address = (unsigned long)(void *)buffer;
293 address_end = address + bufflen;
294
295 while (address < address_end) {
296 unsigned long page_end = (address & PAGE_MASK) + PAGE_SIZE;
297 unsigned long next = page_end < address_end ? page_end
298 : address_end;
299 unsigned long avail = next - address;
300 int status, wakeBit;
301
302
303
304 if (is_write) {
305 char c;
306
307 if (__get_user(c, (char __user *)address)) {
308 if (!ret)
309 ret = -EFAULT;
310 break;
311 }
312 } else {
313
314 if (__put_user(0, (char __user *)address)) {
315 if (!ret)
316 ret = -EFAULT;
317 break;
318 }
319 }
320
321
322 spin_lock_irqsave(&dev->lock, irq_flags);
323 if (access_with_param(dev, CMD_WRITE_BUFFER + cmd_offset,
324 address, avail, pipe, &status)) {
325 writel((u32)pipe, dev->base + PIPE_REG_CHANNEL);
326 writel(avail, dev->base + PIPE_REG_SIZE);
327 writel(address, dev->base + PIPE_REG_ADDRESS);
328 writel(CMD_WRITE_BUFFER + cmd_offset,
329 dev->base + PIPE_REG_COMMAND);
330 status = readl(dev->base + PIPE_REG_STATUS);
331 }
332 spin_unlock_irqrestore(&dev->lock, irq_flags);
333
334 if (status > 0) {
335 ret += status;
336 address += status;
337 continue;
338 }
339
340 if (status == 0)
341 break;
342
343
344
345
346 if (ret > 0)
347 break;
348
349
350
351
352 if (status != PIPE_ERROR_AGAIN ||
353 (filp->f_flags & O_NONBLOCK) != 0) {
354 ret = goldfish_pipe_error_convert(status);
355 break;
356 }
357
358
359
360
361 wakeBit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ;
362 set_bit(wakeBit, &pipe->flags);
363
364
365 goldfish_cmd(pipe, CMD_WAKE_ON_WRITE + cmd_offset);
366
367
368 mutex_unlock(&pipe->lock);
369
370 while (test_bit(wakeBit, &pipe->flags)) {
371 if (wait_event_interruptible(
372 pipe->wake_queue,
373 !test_bit(wakeBit, &pipe->flags)))
374 return -ERESTARTSYS;
375
376 if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
377 return -EIO;
378 }
379
380
381 if (mutex_lock_interruptible(&pipe->lock))
382 return -ERESTARTSYS;
383
384
385 continue;
386 }
387 mutex_unlock(&pipe->lock);
388 return ret;
389}
390
391static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer,
392 size_t bufflen, loff_t *ppos)
393{
394 return goldfish_pipe_read_write(filp, buffer, bufflen, 0);
395}
396
397static ssize_t goldfish_pipe_write(struct file *filp,
398 const char __user *buffer, size_t bufflen,
399 loff_t *ppos)
400{
401 return goldfish_pipe_read_write(filp, (char __user *)buffer,
402 bufflen, 1);
403}
404
405
406static unsigned int goldfish_pipe_poll(struct file *filp, poll_table *wait)
407{
408 struct goldfish_pipe *pipe = filp->private_data;
409 unsigned int mask = 0;
410 int status;
411
412 mutex_lock(&pipe->lock);
413
414 poll_wait(filp, &pipe->wake_queue, wait);
415
416 status = goldfish_cmd_status(pipe, CMD_POLL);
417
418 mutex_unlock(&pipe->lock);
419
420 if (status & PIPE_POLL_IN)
421 mask |= POLLIN | POLLRDNORM;
422
423 if (status & PIPE_POLL_OUT)
424 mask |= POLLOUT | POLLWRNORM;
425
426 if (status & PIPE_POLL_HUP)
427 mask |= POLLHUP;
428
429 if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
430 mask |= POLLERR;
431
432 return mask;
433}
434
435static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id)
436{
437 struct goldfish_pipe_dev *dev = dev_id;
438 unsigned long irq_flags;
439 int count = 0;
440
441
442
443
444
445 spin_lock_irqsave(&dev->lock, irq_flags);
446 for (;;) {
447
448 struct goldfish_pipe *pipe;
449 unsigned long wakes;
450 unsigned long channel = readl(dev->base + PIPE_REG_CHANNEL);
451
452 if (channel == 0)
453 break;
454
455
456 wakes = readl(dev->base + PIPE_REG_WAKES);
457 pipe = (struct goldfish_pipe *)(ptrdiff_t)channel;
458
459
460 if (wakes & PIPE_WAKE_CLOSED) {
461 set_bit(BIT_CLOSED_ON_HOST, &pipe->flags);
462 wakes |= PIPE_WAKE_READ | PIPE_WAKE_WRITE;
463 }
464 if (wakes & PIPE_WAKE_READ)
465 clear_bit(BIT_WAKE_ON_READ, &pipe->flags);
466 if (wakes & PIPE_WAKE_WRITE)
467 clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags);
468
469 wake_up_interruptible(&pipe->wake_queue);
470 count++;
471 }
472 spin_unlock_irqrestore(&dev->lock, irq_flags);
473
474 return (count == 0) ? IRQ_NONE : IRQ_HANDLED;
475}
476
477
478
479
480
481
482
483
484
485
486
487
488static int goldfish_pipe_open(struct inode *inode, struct file *file)
489{
490 struct goldfish_pipe *pipe;
491 struct goldfish_pipe_dev *dev = pipe_dev;
492 int32_t status;
493
494
495 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
496 if (pipe == NULL)
497 return -ENOMEM;
498
499 pipe->dev = dev;
500 mutex_init(&pipe->lock);
501 init_waitqueue_head(&pipe->wake_queue);
502
503
504
505
506
507
508 status = goldfish_cmd_status(pipe, CMD_OPEN);
509 if (status < 0) {
510 kfree(pipe);
511 return status;
512 }
513
514
515 file->private_data = pipe;
516 return 0;
517}
518
519static int goldfish_pipe_release(struct inode *inode, struct file *filp)
520{
521 struct goldfish_pipe *pipe = filp->private_data;
522
523
524 goldfish_cmd(pipe, CMD_CLOSE);
525 kfree(pipe);
526 filp->private_data = NULL;
527 return 0;
528}
529
530static const struct file_operations goldfish_pipe_fops = {
531 .owner = THIS_MODULE,
532 .read = goldfish_pipe_read,
533 .write = goldfish_pipe_write,
534 .poll = goldfish_pipe_poll,
535 .open = goldfish_pipe_open,
536 .release = goldfish_pipe_release,
537};
538
539static struct miscdevice goldfish_pipe_device = {
540 .minor = MISC_DYNAMIC_MINOR,
541 .name = "goldfish_pipe",
542 .fops = &goldfish_pipe_fops,
543};
544
545static int goldfish_pipe_probe(struct platform_device *pdev)
546{
547 int err;
548 struct resource *r;
549 struct goldfish_pipe_dev *dev = pipe_dev;
550
551
552 WARN_ON(dev->base != NULL);
553
554 spin_lock_init(&dev->lock);
555
556 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
557 if (r == NULL || resource_size(r) < PAGE_SIZE) {
558 dev_err(&pdev->dev, "can't allocate i/o page\n");
559 return -EINVAL;
560 }
561 dev->base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
562 if (dev->base == NULL) {
563 dev_err(&pdev->dev, "ioremap failed\n");
564 return -EINVAL;
565 }
566
567 r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
568 if (r == NULL) {
569 err = -EINVAL;
570 goto error;
571 }
572 dev->irq = r->start;
573
574 err = devm_request_irq(&pdev->dev, dev->irq, goldfish_pipe_interrupt,
575 IRQF_SHARED, "goldfish_pipe", dev);
576 if (err) {
577 dev_err(&pdev->dev, "unable to allocate IRQ\n");
578 goto error;
579 }
580
581 err = misc_register(&goldfish_pipe_device);
582 if (err) {
583 dev_err(&pdev->dev, "unable to register device\n");
584 goto error;
585 }
586 setup_access_params_addr(pdev, dev);
587 return 0;
588
589error:
590 dev->base = NULL;
591 return err;
592}
593
594static int goldfish_pipe_remove(struct platform_device *pdev)
595{
596 struct goldfish_pipe_dev *dev = pipe_dev;
597 misc_deregister(&goldfish_pipe_device);
598 dev->base = NULL;
599 return 0;
600}
601
602static struct platform_driver goldfish_pipe = {
603 .probe = goldfish_pipe_probe,
604 .remove = goldfish_pipe_remove,
605 .driver = {
606 .name = "goldfish_pipe"
607 }
608};
609
610module_platform_driver(goldfish_pipe);
611MODULE_AUTHOR("David Turner <digit@google.com>");
612MODULE_LICENSE("GPL");
613