• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 Intel, Inc.
3  * Copyright (C) 2013 Intel, Inc.
4  * Copyright (C) 2014 Linaro Limited
5  * Copyright (C) 2011-2016 Google, Inc.
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 
18 /* This source file contains the implementation of a special device driver
19  * that intends to provide a *very* fast communication channel between the
20  * guest system and the QEMU emulator.
21  *
22  * Usage from the guest is simply the following (error handling simplified):
23  *
24  *	int  fd = open("/dev/qemu_pipe",O_RDWR);
25  *    .... write() or read() through the pipe.
26  *
27  * This driver doesn't deal with the exact protocol used during the session.
28  * It is intended to be as simple as something like:
29  *
30  *    // do this _just_ after opening the fd to connect to a specific
31  *    // emulator service.
32  *    const char*  msg = "<pipename>";
33  *    if (write(fd, msg, strlen(msg)+1) < 0) {
34  *       ... could not connect to <pipename> service
35  *       close(fd);
36  *    }
37  *
38  *    // after this, simply read() and write() to communicate with the
39  *    // service. Exact protocol details left as an exercise to the reader.
40  *
41  * This driver is very fast because it doesn't copy any data through
42  * intermediate buffers, since the emulator is capable of translating
43  * guest user addresses into host ones.
44  *
45  * Note that we must however ensure that each user page involved in the
46  * exchange is properly mapped during a transfer.
47  */
48 
49 #include "goldfish_pipe.h"
50 #include "goldfish_dma.h"
51 
52 #define ERR(...) printk(KERN_ERR __VA_ARGS__);
53 #define INFO(...) printk(KERN_INFO __VA_ARGS__);
54 #define DPRINT(...) pr_debug(__VA_ARGS__);
55 
56 /*
57  * Update this when something changes in the driver's behavior so the host
58  * can benefit from knowing it
59  * Note: version 2 was an intermediate release and isn't supported anymore.
60  */
61 enum {
62 	PIPE_DRIVER_VERSION = 4,
63 	PIPE_CURRENT_DEVICE_VERSION = 2
64 };
65 
66 /*
67  * IMPORTANT: The following constants must match the ones used and defined
68  * in external/qemu/hw/goldfish_pipe.c in the Android source tree.
69  */
70 
71 /* List of bitflags returned in status of CMD_POLL command */
72 enum PipePollFlags {
73 	PIPE_POLL_IN	= 1 << 0,
74 	PIPE_POLL_OUT	= 1 << 1,
75 	PIPE_POLL_HUP	= 1 << 2
76 };
77 
78 /* Possible status values used to signal errors - see goldfish_pipe_error_convert */
79 enum PipeErrors {
80 	PIPE_ERROR_INVAL  = -1,
81 	PIPE_ERROR_AGAIN  = -2,
82 	PIPE_ERROR_NOMEM  = -3,
83 	PIPE_ERROR_IO     = -4
84 };
85 
86 /* Bit-flags used to signal events from the emulator */
87 enum PipeWakeFlags {
88 	PIPE_WAKE_CLOSED = 1 << 0,  /* emulator closed pipe */
89 	PIPE_WAKE_READ   = 1 << 1,  /* pipe can now be read from */
90 	PIPE_WAKE_WRITE  = 1 << 2,  /* pipe can now be written to */
91 	PIPE_WAKE_UNLOCK_DMA  = 1 << 3, /* pipe's DMA buffer can be safely written to again */
92 };
93 
94 /* Bit flags for the 'flags' field */
95 enum PipeFlagsBits {
96 	BIT_CLOSED_ON_HOST = 0,  /* pipe closed by host */
97 	BIT_WAKE_ON_WRITE  = 1,  /* want to be woken on writes */
98 	BIT_WAKE_ON_READ   = 2,  /* want to be woken on reads */
99 	BIT_WAKE_ON_UNLOCK_DMA   = 3,  /* want to wait for unlock of the DMA buffer */
100 };
101 
102 enum PipeRegs {
103 	PIPE_REG_CMD = 0,
104 
105 	PIPE_REG_SIGNAL_BUFFER_HIGH = 4,
106 	PIPE_REG_SIGNAL_BUFFER = 8,
107 	PIPE_REG_SIGNAL_BUFFER_COUNT = 12,
108 
109 	PIPE_REG_OPEN_BUFFER_HIGH = 20,
110 	PIPE_REG_OPEN_BUFFER = 24,
111 
112 	PIPE_REG_VERSION = 36,
113 
114 	PIPE_REG_GET_SIGNALLED = 48,
115 };
116 
117 enum PipeCmdCode {
118 	PIPE_CMD_OPEN = 1,	/* to be used by the pipe device itself */
119 	PIPE_CMD_CLOSE,
120 	PIPE_CMD_POLL,
121 	PIPE_CMD_WRITE,
122 	PIPE_CMD_WAKE_ON_WRITE,
123 	PIPE_CMD_READ,
124 	PIPE_CMD_WAKE_ON_READ,
125 
126 	/*
127 	 * TODO(zyy): implement a deferred read/write execution to allow parallel
128 	 *  processing of pipe operations on the host.
129 	*/
130 	PIPE_CMD_WAKE_ON_DONE_IO,
131 	PIPE_CMD_DMA_HOST_MAP,
132 	PIPE_CMD_DMA_HOST_UNMAP,
133 };
134 
135 enum {
136 	MAX_BUFFERS_PER_COMMAND = 336,
137 	MAX_SIGNALLED_PIPES = 64,
138 	INITIAL_PIPES_CAPACITY = 64
139 };
140 
141 struct goldfish_pipe_dev;
142 struct goldfish_pipe;
143 struct goldfish_pipe_command;
144 
145 /* A per-pipe command structure, shared with the host */
146 struct goldfish_pipe_command {
147 	s32 cmd;		/* PipeCmdCode, guest -> host */
148 	s32 id;			/* pipe id, guest -> host */
149 	s32 status;		/* command execution status, host -> guest */
150 	s32 reserved;	/* to pad to 64-bit boundary */
151 	union {
152 		/* Parameters for PIPE_CMD_{READ,WRITE} */
153 		struct {
154 			u32 buffers_count;					/* number of buffers, guest -> host */
155 			s32 consumed_size;					/* number of consumed bytes, host -> guest */
156 			u64 ptrs[MAX_BUFFERS_PER_COMMAND]; 	/* buffer pointers, guest -> host */
157 			u32 sizes[MAX_BUFFERS_PER_COMMAND];	/* buffer sizes, guest -> host */
158 		} rw_params;
159 		/* Parameters for PIPE_CMD_DMA_HOST_(UN)MAP */
160 		struct {
161 			u64 dma_paddr;
162 			u64 sz;
163 		} dma_maphost_params;
164 	};
165 };
166 
167 /* A single signalled pipe information */
168 struct signalled_pipe_buffer {
169 	u32 id;
170 	u32 flags;
171 };
172 
173 /* Parameters for the PIPE_CMD_OPEN command */
174 struct open_command_param {
175 	u64 command_buffer_ptr;
176 	u32 rw_params_max_count;
177 };
178 
179 /* Device-level set of buffers shared with the host */
180 struct goldfish_pipe_dev_buffers {
181 	struct open_command_param open_command_params;
182 	struct signalled_pipe_buffer signalled_pipe_buffers[MAX_SIGNALLED_PIPES];
183 };
184 
185 /* This data type models a given pipe instance */
186 struct goldfish_pipe {
187 	u32 id;							/* pipe ID - index into goldfish_pipe_dev::pipes array */
188 	unsigned long flags;			/* The wake flags pipe is waiting for
189 									 * Note: not protected with any lock, uses atomic operations
190 									 *  and barriers to make it thread-safe.
191 									 */
192 	unsigned long signalled_flags;	/* wake flags host have signalled,
193 									 *  - protected by goldfish_pipe_dev::lock */
194 
195 	struct goldfish_pipe_command *command_buffer;	/* A pointer to command buffer */
196 
197 	/* doubly linked list of signalled pipes, protected by goldfish_pipe_dev::lock */
198 	struct goldfish_pipe *prev_signalled;
199 	struct goldfish_pipe *next_signalled;
200 
201 	/*
202 	 * A pipe's own lock. Protects the following:
203 	 *  - *command_buffer - makes sure a command can safely write its parameters
204 	 *    to the host and read the results back.
205 	 */
206 	struct mutex lock;
207 
208 	wait_queue_head_t wake_queue;	/* A wake queue for sleeping until host signals an event */
209 	struct goldfish_pipe_dev *dev;	/* Pointer to the parent goldfish_pipe_dev instance */
210 	struct goldfish_dma_context *dma; /* Holds information about reserved DMA region for this pipe */
211 };
212 
213 struct goldfish_pipe_dev goldfish_pipe_dev[1] = {};
214 
goldfish_pipe_cmd_locked(struct goldfish_pipe * pipe,enum PipeCmdCode cmd)215 static int goldfish_pipe_cmd_locked(struct goldfish_pipe *pipe, enum PipeCmdCode cmd)
216 {
217 	pipe->command_buffer->cmd = cmd;
218 	pipe->command_buffer->status = PIPE_ERROR_INVAL;	/* failure by default */
219 	writel(pipe->id, pipe->dev->base + PIPE_REG_CMD);
220 	return pipe->command_buffer->status;
221 }
222 
goldfish_pipe_cmd(struct goldfish_pipe * pipe,enum PipeCmdCode cmd)223 static int goldfish_pipe_cmd(struct goldfish_pipe *pipe, enum PipeCmdCode cmd)
224 {
225 	int status;
226 	if (mutex_lock_interruptible(&pipe->lock))
227 		return PIPE_ERROR_IO;
228 	status = goldfish_pipe_cmd_locked(pipe, cmd);
229 	mutex_unlock(&pipe->lock);
230 	return status;
231 }
232 
233 /*
234  * This function converts an error code returned by the emulator through
235  * the PIPE_REG_STATUS i/o register into a valid negative errno value.
236  */
goldfish_pipe_error_convert(int status)237 static int goldfish_pipe_error_convert(int status)
238 {
239 	switch (status) {
240 	case PIPE_ERROR_AGAIN:
241 		return -EAGAIN;
242 	case PIPE_ERROR_NOMEM:
243 		return -ENOMEM;
244 	case PIPE_ERROR_IO:
245 		return -EIO;
246 	default:
247 		return -EINVAL;
248 	}
249 }
250 
pin_user_pages(unsigned long first_page,unsigned long last_page,unsigned last_page_size,int is_write,struct page * pages[MAX_BUFFERS_PER_COMMAND],unsigned * iter_last_page_size)251 static int pin_user_pages(unsigned long first_page, unsigned long last_page,
252 	unsigned last_page_size, int is_write,
253 	struct page *pages[MAX_BUFFERS_PER_COMMAND], unsigned *iter_last_page_size)
254 {
255 	int ret;
256 	int requested_pages = ((last_page - first_page) >> PAGE_SHIFT) + 1;
257 	if (requested_pages > MAX_BUFFERS_PER_COMMAND) {
258 		requested_pages = MAX_BUFFERS_PER_COMMAND;
259 		*iter_last_page_size = PAGE_SIZE;
260 	} else {
261 		*iter_last_page_size = last_page_size;
262 	}
263 
264 	ret = get_user_pages_fast(
265 			first_page, requested_pages, !is_write, pages);
266 	if (ret <= 0)
267 		return -EFAULT;
268 	if (ret < requested_pages)
269 		*iter_last_page_size = PAGE_SIZE;
270 	return ret;
271 
272 }
273 
release_user_pages(struct page ** pages,int pages_count,int is_write,s32 consumed_size)274 static void release_user_pages(struct page **pages, int pages_count,
275 	int is_write, s32 consumed_size)
276 {
277 	int i;
278 	for (i = 0; i < pages_count; i++) {
279 		if (!is_write && consumed_size > 0) {
280 			set_page_dirty(pages[i]);
281 		}
282 		put_page(pages[i]);
283 	}
284 }
285 
286 /* Populate the call parameters, merging adjacent pages together */
populate_rw_params(struct page ** pages,int pages_count,unsigned long address,unsigned long address_end,unsigned long first_page,unsigned long last_page,unsigned iter_last_page_size,int is_write,struct goldfish_pipe_command * command)287 static void populate_rw_params(
288 	struct page **pages, int pages_count,
289 	unsigned long address, unsigned long address_end,
290 	unsigned long first_page, unsigned long last_page,
291 	unsigned iter_last_page_size, int is_write,
292 	struct goldfish_pipe_command *command)
293 {
294 	/*
295 	 * Process the first page separately - it's the only page that
296 	 * needs special handling for its start address.
297 	 */
298 	unsigned long xaddr = page_to_phys(pages[0]);
299 	unsigned long xaddr_prev = xaddr;
300 	int buffer_idx = 0;
301 	int i = 1;
302 	int size_on_page = first_page == last_page
303 			? (int)(address_end - address)
304 			: (PAGE_SIZE - (address & ~PAGE_MASK));
305 	command->rw_params.ptrs[0] = (u64)(xaddr | (address & ~PAGE_MASK));
306 	command->rw_params.sizes[0] = size_on_page;
307 	for (; i < pages_count; ++i) {
308 		xaddr = page_to_phys(pages[i]);
309 		size_on_page = (i == pages_count - 1) ? iter_last_page_size : PAGE_SIZE;
310 		if (xaddr == xaddr_prev + PAGE_SIZE) {
311 			command->rw_params.sizes[buffer_idx] += size_on_page;
312 		} else {
313 			++buffer_idx;
314 			command->rw_params.ptrs[buffer_idx] = (u64)xaddr;
315 			command->rw_params.sizes[buffer_idx] = size_on_page;
316 		}
317 		xaddr_prev = xaddr;
318 	}
319 	command->rw_params.buffers_count = buffer_idx + 1;
320 }
321 
transfer_max_buffers(struct goldfish_pipe * pipe,unsigned long address,unsigned long address_end,int is_write,unsigned long last_page,unsigned int last_page_size,s32 * consumed_size,int * status)322 static int transfer_max_buffers(struct goldfish_pipe *pipe,
323 	unsigned long address, unsigned long address_end, int is_write,
324 	unsigned long last_page, unsigned int last_page_size,
325 	s32* consumed_size, int *status)
326 {
327 	struct page *pages[MAX_BUFFERS_PER_COMMAND];
328 	unsigned long first_page = address & PAGE_MASK;
329 	unsigned int iter_last_page_size;
330 	int pages_count = pin_user_pages(first_page, last_page,
331 			last_page_size, is_write,
332 			pages, &iter_last_page_size);
333 	if (pages_count < 0)
334 		return pages_count;
335 
336 	/* Serialize access to the pipe command buffers */
337 	if (mutex_lock_interruptible(&pipe->lock))
338 		return -ERESTARTSYS;
339 
340 	populate_rw_params(pages, pages_count, address, address_end,
341 		first_page, last_page, iter_last_page_size, is_write,
342 		pipe->command_buffer);
343 
344 	/* Transfer the data */
345 	*status = goldfish_pipe_cmd_locked(pipe,
346 						is_write ? PIPE_CMD_WRITE : PIPE_CMD_READ);
347 
348 	*consumed_size = pipe->command_buffer->rw_params.consumed_size;
349 
350 	mutex_unlock(&pipe->lock);
351 
352 	release_user_pages(pages, pages_count, is_write, *consumed_size);
353 
354 	return 0;
355 }
356 
goldfish_pipe_wait_event(u32 wakeBit,struct goldfish_pipe * pipe)357 static int goldfish_pipe_wait_event(u32 wakeBit, struct goldfish_pipe *pipe) {
358 	while (test_bit(wakeBit, &pipe->flags)) {
359 		if (wait_event_interruptible(
360 				pipe->wake_queue,
361 				!test_bit(wakeBit, &pipe->flags)))
362 			return -ERESTARTSYS;
363 
364 		if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
365 			return -EIO;
366 	}
367 	return 0;
368 }
369 
wait_for_host_signal(struct goldfish_pipe * pipe,int is_write)370 static int wait_for_host_signal(struct goldfish_pipe *pipe, int is_write)
371 {
372 	u32 wakeBit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ;
373 	set_bit(wakeBit, &pipe->flags);
374 
375 	/* Tell the emulator we're going to wait for a wake event */
376 	(void)goldfish_pipe_cmd(pipe,
377 			is_write ? PIPE_CMD_WAKE_ON_WRITE : PIPE_CMD_WAKE_ON_READ);
378 
379 	return goldfish_pipe_wait_event(wakeBit, pipe);
380 }
381 
goldfish_pipe_read_write(struct file * filp,char __user * buffer,size_t bufflen,int is_write)382 static ssize_t goldfish_pipe_read_write(struct file *filp,
383 	char __user *buffer, size_t bufflen, int is_write)
384 {
385 	struct goldfish_pipe *pipe = filp->private_data;
386 	int count = 0, ret = -EINVAL;
387 	unsigned long address, address_end, last_page;
388 	unsigned int last_page_size;
389 
390 	/* If the emulator already closed the pipe, no need to go further */
391 	if (unlikely(test_bit(BIT_CLOSED_ON_HOST, &pipe->flags)))
392 		return -EIO;
393 	/* Null reads or writes succeeds */
394 	if (unlikely(bufflen == 0))
395 		return 0;
396 	/* Check the buffer range for access */
397 	if (unlikely(!access_ok(is_write ? VERIFY_WRITE : VERIFY_READ,
398 			buffer, bufflen)))
399 		return -EFAULT;
400 
401 	address = (unsigned long)buffer;
402 	address_end = address + bufflen;
403 	last_page = (address_end - 1) & PAGE_MASK;
404 	last_page_size = ((address_end - 1) & ~PAGE_MASK) + 1;
405 
406 	while (address < address_end) {
407 		s32 consumed_size;
408 		int status;
409 		ret = transfer_max_buffers(pipe, address, address_end, is_write,
410 				last_page, last_page_size, &consumed_size, &status);
411 		if (ret < 0)
412 			break;
413 
414 		if (consumed_size > 0) {
415 			/* No matter what's the status, we've transfered something */
416 			count += consumed_size;
417 			address += consumed_size;
418 		}
419 		if (status > 0)
420 			continue;
421 		if (status == 0) {
422 			/* EOF */
423 			ret = 0;
424 			break;
425 		}
426 		if (count > 0) {
427 			/*
428 			 * An error occured, but we already transfered
429 			 * something on one of the previous iterations.
430 			 * Just return what we already copied and log this
431 			 * err.
432 			 */
433 			if (status != PIPE_ERROR_AGAIN)
434 				pr_info_ratelimited("goldfish_pipe: backend error %d on %s\n",
435 									status, is_write ? "write" : "read");
436 			break;
437 		}
438 
439 		/*
440 		 * If the error is not PIPE_ERROR_AGAIN, or if we are in
441 		 * non-blocking mode, just return the error code.
442 		 */
443 		if (status != PIPE_ERROR_AGAIN || (filp->f_flags & O_NONBLOCK) != 0) {
444 			ret = goldfish_pipe_error_convert(status);
445 			break;
446 		}
447 
448 		status = wait_for_host_signal(pipe, is_write);
449 		if (status < 0)
450 			return status;
451 	}
452 
453 	if (count > 0)
454 		return count;
455 	return ret;
456 }
457 
goldfish_pipe_read(struct file * filp,char __user * buffer,size_t bufflen,loff_t * ppos)458 static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer,
459 				size_t bufflen, loff_t *ppos)
460 {
461 	return goldfish_pipe_read_write(filp, buffer, bufflen, /* is_write */ 0);
462 }
463 
goldfish_pipe_write(struct file * filp,const char __user * buffer,size_t bufflen,loff_t * ppos)464 static ssize_t goldfish_pipe_write(struct file *filp,
465 				const char __user *buffer, size_t bufflen,
466 				loff_t *ppos)
467 {
468 	return goldfish_pipe_read_write(filp,
469 			/* cast away the const */(char __user *)buffer, bufflen,
470 			/* is_write */ 1);
471 }
472 
goldfish_pipe_poll(struct file * filp,poll_table * wait)473 static unsigned int goldfish_pipe_poll(struct file *filp, poll_table *wait)
474 {
475 	struct goldfish_pipe *pipe = filp->private_data;
476 	unsigned int mask = 0;
477 	int status;
478 
479 	poll_wait(filp, &pipe->wake_queue, wait);
480 
481 	status = goldfish_pipe_cmd(pipe, PIPE_CMD_POLL);
482 	if (status < 0) {
483 		return -ERESTARTSYS;
484 	}
485 
486 	if (status & PIPE_POLL_IN)
487 		mask |= POLLIN | POLLRDNORM;
488 	if (status & PIPE_POLL_OUT)
489 		mask |= POLLOUT | POLLWRNORM;
490 	if (status & PIPE_POLL_HUP)
491 		mask |= POLLHUP;
492 	if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
493 		mask |= POLLERR;
494 
495 	return mask;
496 }
497 
signalled_pipes_add_locked(struct goldfish_pipe_dev * dev,u32 id,u32 flags)498 static void signalled_pipes_add_locked(struct goldfish_pipe_dev *dev,
499 	u32 id, u32 flags)
500 {
501 	struct goldfish_pipe *pipe;
502 
503 	BUG_ON(id >= dev->pipes_capacity);
504 
505 	pipe = dev->pipes[id];
506 	if (!pipe)
507 		return;
508 	pipe->signalled_flags |= flags;
509 
510 	if (pipe->prev_signalled || pipe->next_signalled
511 		|| dev->first_signalled_pipe == pipe)
512 		return;	/* already in the list */
513 	pipe->next_signalled = dev->first_signalled_pipe;
514 	if (dev->first_signalled_pipe) {
515 		dev->first_signalled_pipe->prev_signalled = pipe;
516 	}
517 	dev->first_signalled_pipe = pipe;
518 }
519 
signalled_pipes_remove_locked(struct goldfish_pipe_dev * dev,struct goldfish_pipe * pipe)520 static void signalled_pipes_remove_locked(struct goldfish_pipe_dev *dev,
521 	struct goldfish_pipe *pipe) {
522 	if (pipe->prev_signalled)
523 		pipe->prev_signalled->next_signalled = pipe->next_signalled;
524 	if (pipe->next_signalled)
525 		pipe->next_signalled->prev_signalled = pipe->prev_signalled;
526 	if (pipe == dev->first_signalled_pipe)
527 		dev->first_signalled_pipe = pipe->next_signalled;
528 	pipe->prev_signalled = NULL;
529 	pipe->next_signalled = NULL;
530 }
531 
signalled_pipes_pop_front(struct goldfish_pipe_dev * dev,int * wakes)532 static struct goldfish_pipe *signalled_pipes_pop_front(struct goldfish_pipe_dev *dev,
533 		int *wakes)
534 {
535 	struct goldfish_pipe *pipe;
536 	unsigned long flags;
537 	spin_lock_irqsave(&dev->lock, flags);
538 
539 	pipe = dev->first_signalled_pipe;
540 	if (pipe) {
541 		*wakes = pipe->signalled_flags;
542 		pipe->signalled_flags = 0;
543 		/*
544 		 * This is an optimized version of signalled_pipes_remove_locked() -
545 		 * we want to make it as fast as possible to wake the sleeping pipe
546 		 * operations faster
547 		 */
548 		dev->first_signalled_pipe = pipe->next_signalled;
549 		if (dev->first_signalled_pipe)
550 			dev->first_signalled_pipe->prev_signalled = NULL;
551 		pipe->next_signalled = NULL;
552 	}
553 
554 	spin_unlock_irqrestore(&dev->lock, flags);
555 	return pipe;
556 }
557 
goldfish_pipe_dma_clear_lock(struct goldfish_pipe * pipe)558 static void goldfish_pipe_dma_clear_lock(struct goldfish_pipe *pipe) {
559 	DPRINT("PIPE_WAKE_UNLOCK_DMA: unlock pipe dma for pipe 0x%p\n", pipe);
560 	if (pipe->dma) {
561 		WARN_ON(!pipe->dma->locked);
562 		clear_bit(BIT_WAKE_ON_UNLOCK_DMA, &pipe->flags);
563 		pipe->dma->locked = false;
564 		/* meant to be used with wake_up_interruptible---otherwise no signaling,
565 		 * and no write barrier! */
566 	}
567 }
568 
goldfish_interrupt_task(unsigned long unused)569 static void goldfish_interrupt_task(unsigned long unused)
570 {
571 	/* Iterate over the signalled pipes and wake them one by one */
572 	struct goldfish_pipe *pipe;
573 	int wakes;
574 	while ((pipe = signalled_pipes_pop_front(goldfish_pipe_dev, &wakes)) !=
575 			NULL) {
576 		if (wakes & PIPE_WAKE_CLOSED) {
577 			pipe->flags = 1 << BIT_CLOSED_ON_HOST;
578 		} else {
579 			if (wakes & PIPE_WAKE_UNLOCK_DMA)
580 				goldfish_pipe_dma_clear_lock(pipe);
581 			if (wakes & PIPE_WAKE_READ)
582 				clear_bit(BIT_WAKE_ON_READ, &pipe->flags);
583 			if (wakes & PIPE_WAKE_WRITE)
584 				clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags);
585 		}
586 		/*
587 		 * wake_up_interruptible() implies a write barrier, so don't explicitly
588 		 * add another one here.
589 		 */
590 		wake_up_interruptible(&pipe->wake_queue);
591 	}
592 }
593 DECLARE_TASKLET(goldfish_interrupt_tasklet, goldfish_interrupt_task, 0);
594 
595 /*
596  * The general idea of the interrupt handling:
597  *
598  *  1. device raises an interrupt if there's at least one signalled pipe
599  *  2. IRQ handler reads the signalled pipes and their count from the device
600  *  3. device writes them into a shared buffer and returns the count
601  *      it only resets the IRQ if it has returned all signalled pipes,
602  *      otherwise it leaves it raised, so IRQ handler will be called
603  *      again for the next chunk
604  *  4. IRQ handler adds all returned pipes to the device's signalled pipes list
605  *  5. IRQ handler launches a tasklet to process the signalled pipes from the
606  *      list in a separate context
607  */
goldfish_pipe_interrupt(int irq,void * dev_id)608 static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id)
609 {
610 	u32 count;
611 	u32 i;
612 	unsigned long flags;
613 	struct goldfish_pipe_dev *dev = dev_id;
614 	if (dev != goldfish_pipe_dev)
615 		return IRQ_NONE;
616 
617 	/* Request the signalled pipes from the device */
618 	spin_lock_irqsave(&dev->lock, flags);
619 
620 	count = readl(dev->base + PIPE_REG_GET_SIGNALLED);
621 	if (count == 0) {
622 		spin_unlock_irqrestore(&dev->lock, flags);
623 		return IRQ_NONE;
624 	}
625 	if (count > MAX_SIGNALLED_PIPES)
626 		count = MAX_SIGNALLED_PIPES;
627 
628 	for (i = 0; i < count; ++i)
629 		signalled_pipes_add_locked(dev,
630 			dev->buffers->signalled_pipe_buffers[i].id,
631 			dev->buffers->signalled_pipe_buffers[i].flags);
632 
633 	spin_unlock_irqrestore(&dev->lock, flags);
634 
635 	tasklet_schedule(&goldfish_interrupt_tasklet);
636 	return IRQ_HANDLED;
637 }
638 
get_free_pipe_id_locked(struct goldfish_pipe_dev * dev)639 static int get_free_pipe_id_locked(struct goldfish_pipe_dev *dev)
640 {
641 	int id;
642 	for (id = 0; id < dev->pipes_capacity; ++id)
643 		if (!dev->pipes[id])
644 			return id;
645 
646 	{
647 		/* Reallocate the array */
648 		u32 new_capacity = 2 * dev->pipes_capacity;
649 		struct goldfish_pipe **pipes =
650 				kcalloc(new_capacity, sizeof(*pipes), GFP_KERNEL);
651 		if (!pipes)
652 			return -ENOMEM;
653 		memcpy(pipes, dev->pipes, sizeof(*pipes) * dev->pipes_capacity);
654 		kfree(dev->pipes);
655 		dev->pipes = pipes;
656 		id = dev->pipes_capacity;
657 		dev->pipes_capacity = new_capacity;
658 	}
659 	return id;
660 }
661 
662 /**
663  *	goldfish_pipe_open - open a channel to the AVD
664  *	@inode: inode of device
665  *	@file: file struct of opener
666  *
667  *	Create a new pipe link between the emulator and the use application.
668  *	Each new request produces a new pipe.
669  *
670  *	Note: we use the pipe ID as a mux. All goldfish emulations are 32bit
671  *	right now so this is fine. A move to 64bit will need this addressing
672  */
goldfish_pipe_open(struct inode * inode,struct file * file)673 static int goldfish_pipe_open(struct inode *inode, struct file *file)
674 {
675 	struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
676 	unsigned long flags;
677 	int id;
678 	int status;
679 
680 	/* Allocate new pipe kernel object */
681 	struct goldfish_pipe *pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
682 	if (pipe == NULL) {
683 		ERR("Could not allocate new pipe!\n");
684 		return -ENOMEM;
685 	}
686 
687 	pipe->dev = dev;
688 	mutex_init(&pipe->lock);
689 	init_waitqueue_head(&pipe->wake_queue);
690 
691 	/*
692 	 * Command buffer needs to be allocated on its own page to make sure it is
693 	 * physically contiguous in host's address space.
694 	 */
695 	pipe->command_buffer =
696 			(struct goldfish_pipe_command *)__get_free_page(GFP_KERNEL);
697 	if (!pipe->command_buffer) {
698 		ERR("Could not alloc pipe command buffer!\n");
699 		status = -ENOMEM;
700 		goto err_pipe;
701 	}
702 
703 	spin_lock_irqsave(&dev->lock, flags);
704 
705 	id = get_free_pipe_id_locked(dev);
706 	if (id < 0) {
707 		ERR("Could not get free pipe id!\n");
708 		status = id;
709 		goto err_id_locked;
710 	}
711 
712 	dev->pipes[id] = pipe;
713 	pipe->id = id;
714 	pipe->command_buffer->id = id;
715 
716 	/* Now tell the emulator we're opening a new pipe. */
717 	dev->buffers->open_command_params.rw_params_max_count =
718 			MAX_BUFFERS_PER_COMMAND;
719 	dev->buffers->open_command_params.command_buffer_ptr =
720 			(u64)(unsigned long)__pa(pipe->command_buffer);
721 	status = goldfish_pipe_cmd_locked(pipe, PIPE_CMD_OPEN);
722 	spin_unlock_irqrestore(&dev->lock, flags);
723 	if (status < 0) {
724 		ERR("Could not tell host of new pipe! status=%d", status);
725 		goto err_cmd;
726 	}
727 
728 	pipe->dma = NULL;
729 
730 	/* All is done, save the pipe into the file's private data field */
731 	file->private_data = pipe;
732 	DPRINT("%s on 0x%p\n", __FUNCTION__, pipe);
733 	return 0;
734 
735 err_cmd:
736 	spin_lock_irqsave(&dev->lock, flags);
737 	dev->pipes[id] = NULL;
738 err_id_locked:
739 	spin_unlock_irqrestore(&dev->lock, flags);
740 	free_page((unsigned long)pipe->command_buffer);
741 err_pipe:
742 	kfree(pipe);
743 	return status;
744 }
745 
goldfish_pipe_dma_release(struct goldfish_pipe * pipe)746 static void goldfish_pipe_dma_release(struct goldfish_pipe *pipe) {
747 	struct goldfish_dma_context *dma = pipe->dma;
748 	if (!dma) return;
749 
750 	mutex_lock(&dma->mutex_lock);
751 	if (dma->dma_vaddr) {
752 		DPRINT("Last ref for dma region @ 0x%llx\n", dma->phys_begin);
753 		pipe->command_buffer->dma_maphost_params.dma_paddr = dma->phys_begin;
754 		pipe->command_buffer->dma_maphost_params.sz = dma->dma_size;
755 		goldfish_pipe_cmd(pipe, PIPE_CMD_DMA_HOST_UNMAP);
756 		DPRINT("Unmapped and freeing dma @ 0x%llx\n", dma->phys_begin);
757 		dma_free_coherent(
758 				dma->pdev_dev,
759 				dma->dma_size,
760 				dma->dma_vaddr,
761 				dma->phys_begin);
762 		pipe->dev->dma_alloc_total -= dma->dma_size;
763 	}
764 	DPRINT("after delete of dma @ 0x%llx: alloc total %llu\n",
765 			dma->phys_begin, pipe->dev->dma_alloc_total);
766 	mutex_unlock(&dma->mutex_lock);
767 	dma->locked = false;
768 	kfree(dma);
769 	pipe->dma = NULL;
770 }
771 
goldfish_pipe_release(struct inode * inode,struct file * filp)772 static int goldfish_pipe_release(struct inode *inode, struct file *filp)
773 {
774 	unsigned long flags;
775 	struct goldfish_pipe *pipe = filp->private_data;
776 	struct goldfish_pipe_dev *dev = pipe->dev;
777 
778 	DPRINT("%s on 0x%p\n", __FUNCTION__, pipe);
779 	/* Even if a fd is duped or involved in a forked process,
780 	 * open/release methods are called only once, ever.
781 	 * This makes goldfish_pipe_release a safe point
782 	 * to delete the DMA region. */
783 	goldfish_pipe_dma_release(pipe);
784 
785 	/* The guest is closing the channel, so tell the emulator right now */
786 	(void)goldfish_pipe_cmd(pipe, PIPE_CMD_CLOSE);
787 
788 	spin_lock_irqsave(&dev->lock, flags);
789 	dev->pipes[pipe->id] = NULL;
790 	signalled_pipes_remove_locked(dev, pipe);
791 	spin_unlock_irqrestore(&dev->lock, flags);
792 
793 	filp->private_data = NULL;
794 
795 	free_page((unsigned long)pipe->command_buffer);
796 	kfree(pipe);
797 
798 	return 0;
799 }
800 
801 /* VMA open/close are for debugging purposes only.
802  * One might think that fork() (and thus pure calls to open())
803  * will require some sort of bookkeeping or refcounting
804  * for dma contexts (incl. when to call dma_free_coherent),
805  * but |vm_private_data| field and |vma_open/close| are only
806  * for situations where the driver needs to interact with vma's
807  * directly with its own per-VMA data structure (which does
808  * need to be refcounted).
809  *
810  * Here, we just use the kernel's existing
811  * VMA processing; we don't do anything on our own.
812  * The only reason we would want to do so is if we had to do
813  * special processing for the virtual (not physical) memory
814  * already associated with DMA memory; it is much less related
815  * to the task of knowing when to alloc/dealloc DMA memory. */
goldfish_dma_vma_open(struct vm_area_struct * vma)816 static void goldfish_dma_vma_open(struct vm_area_struct *vma) {
817 	/* Not used */
818 }
819 
goldfish_dma_vma_close(struct vm_area_struct * vma)820 static void goldfish_dma_vma_close(struct vm_area_struct *vma) {
821 	/* Not used */
822 }
823 
824 static struct vm_operations_struct goldfish_dma_vm_ops = {
825 	.open = goldfish_dma_vma_open,
826 	.close = goldfish_dma_vma_close,
827 };
828 
is_page_size_multiple(unsigned long sz)829 static bool is_page_size_multiple(unsigned long sz) {
830 	return !(sz & (PAGE_SIZE - 1));
831 }
832 
goldfish_pipe_dma_alloc_locked(struct goldfish_pipe * pipe)833 static void goldfish_pipe_dma_alloc_locked(struct goldfish_pipe *pipe) {
834 	struct goldfish_dma_context *dma;
835 
836 	DPRINT("%s: try alloc dma for pipe 0x%p\n",
837 			__FUNCTION__, pipe);
838 
839 	dma = pipe->dma;
840 
841 	if (dma->dma_vaddr) {
842 		DPRINT("%s: already alloced, return.\n",
843 			__FUNCTION__);
844 		return;
845 	}
846 
847 	dma->phys_begin = 0;
848 	dma->dma_vaddr =
849 		dma_alloc_coherent(
850 				dma->pdev_dev,
851 				dma->dma_size,
852 				(dma_addr_t *)&dma->phys_begin,
853 				GFP_KERNEL);
854 	BUG_ON(!dma->dma_vaddr);
855 
856 	dma->phys_end = dma->phys_begin + dma->dma_size;
857 	dma->pfn = dma->phys_begin >> PAGE_SHIFT;
858 	pipe->dev->dma_alloc_total += dma->dma_size;
859 
860 	DPRINT("%s: got v/p addrs "
861 			"0x%p 0x%llx sz %llu total alloc %llu\n",
862 			__FUNCTION__,
863 			dma->dma_vaddr,
864 			dma->phys_begin,
865 			dma->dma_size,
866 			pipe->dev->dma_alloc_total);
867 	pipe->command_buffer->dma_maphost_params.dma_paddr = dma->phys_begin;
868 	pipe->command_buffer->dma_maphost_params.sz = dma->dma_size;
869 	goldfish_pipe_cmd(pipe, PIPE_CMD_DMA_HOST_MAP);
870 }
871 
872 /* When we call mmap() on a pipe fd, we obtain a pointer into
873  * the physically contiguous DMA region of the pipe device
874  * (Goldfish DMA). */
goldfish_dma_mmap(struct file * filp,struct vm_area_struct * vma)875 static int goldfish_dma_mmap(struct file *filp, struct vm_area_struct *vma) {
876 
877 	struct goldfish_pipe *pipe = (struct goldfish_pipe *)(filp->private_data);
878 	struct goldfish_dma_context *dma = pipe->dma;
879 	unsigned long sz_requested = vma->vm_end - vma->vm_start;
880 	int map_err;
881 
882 	if (!is_page_size_multiple(sz_requested)) {
883 		ERR("Cannot mmap dma buffer of size %lx (is not multiple of page size)\n",
884 			sz_requested);
885 		return -EINVAL;
886 	}
887 
888 	DPRINT("Mapping dma at 0x%llx\n", dma->phys_begin);
889 
890 	mutex_lock(&dma->mutex_lock);
891 	/* Alloc phys region if not allocated already. */
892 	goldfish_pipe_dma_alloc_locked(pipe);
893 	mutex_unlock(&dma->mutex_lock);
894 
895 	map_err =
896 		remap_pfn_range(
897 				vma,
898 				vma->vm_start,
899 				dma->phys_begin >> PAGE_SHIFT,
900 				sz_requested,
901 				vma->vm_page_prot);
902 
903 	if (map_err < 0) {
904 		ERR("Cannot remap pfn range....\n");
905 		mutex_unlock(&dma->mutex_lock);
906 		return -EAGAIN;
907 	}
908 
909 	vma->vm_ops = &goldfish_dma_vm_ops;
910 	DPRINT("goldfish_dma_mmap for host vaddr 0x%llx succeeded\n",
911 			dma->phys_begin);
912 
913 	return 0;
914 }
915 
goldfish_pipe_dma_create_region(struct goldfish_pipe * pipe,uint64_t size)916 static void goldfish_pipe_dma_create_region(
917 	struct goldfish_pipe *pipe,
918 	uint64_t size) {
919 
920 	struct goldfish_dma_context *dma =
921 		kzalloc(sizeof(struct goldfish_dma_context), GFP_KERNEL);
922 	if (!dma) {
923 		ERR("Could not allocate DMA context info!");
924 		return;
925 	}
926 	dma->dma_size = size;
927 	mutex_init(&dma->mutex_lock);
928 
929 	mutex_lock(&pipe->lock);
930 	pipe->dma = dma;
931 	pipe->dma->pdev_dev = pipe->dev->pdev_dev;
932 	mutex_unlock(&pipe->lock);
933 }
934 
goldfish_pipe_dma_acquire_lock(struct goldfish_pipe * pipe)935 static int goldfish_pipe_dma_acquire_lock(struct goldfish_pipe *pipe) {
936 	smp_mb();
937 	if (pipe->dma && pipe->dma->locked) {
938 		set_bit(BIT_WAKE_ON_UNLOCK_DMA, &pipe->flags);
939 		return goldfish_pipe_wait_event(BIT_WAKE_ON_UNLOCK_DMA, pipe);
940 	} else if (pipe->dma) {
941 		pipe->dma->locked = true;
942 	} else {
943 		ERR("No dma context for this pipe!");
944 		return -EINVAL;
945 	}
946 	return 0;
947 }
948 
goldfish_dma_ioctl(struct file * file,unsigned int cmd,unsigned long arg)949 static long goldfish_dma_ioctl(struct file *file,
950 								unsigned int cmd,
951 								unsigned long arg)
952 {
953 	struct goldfish_pipe *pipe;
954 	struct goldfish_dma_context *dma;
955 	struct goldfish_dma_ioctl_info ioctl_data;
956 	int ret = 0;
957 
958 	DPRINT("%s: call.", __FUNCTION__);
959 	pipe = (struct goldfish_pipe *)(file->private_data);
960 	DPRINT("%s: get dma ptr.", __FUNCTION__);
961 	dma = pipe->dma;
962 	DPRINT("%s: continuing", __FUNCTION__);
963 
964 	if (copy_from_user(&ioctl_data, (void __user *)arg, sizeof(ioctl_data))) {
965 		return -EFAULT;
966 	}
967 
968 	DPRINT("%s: copied ioctl data from user", __FUNCTION__);
969 
970 	switch (cmd) {
971 	case GOLDFISH_DMA_IOC_LOCK:
972 		DPRINT("LOCK_DMA for pipe 0x%p\n", pipe);
973 		ret = goldfish_pipe_dma_acquire_lock(pipe);
974 		if (ret == 0) {
975 			DPRINT("acquired lock, proceeding for pipe 0x%p\n", pipe);
976 		}
977 		return ret;
978 	case GOLDFISH_DMA_IOC_UNLOCK:
979 		DPRINT("UNLOCK_DMA for pipe 0x%p\n", pipe);
980 		goldfish_pipe_dma_clear_lock(pipe);
981 		wake_up_interruptible(&pipe->wake_queue);
982 		return 0;
983 	case GOLDFISH_DMA_IOC_GETOFF:
984 		DPRINT("DMA_GETOFF for pipe 0x%p\n", pipe);
985 		ioctl_data.phys_begin = dma->phys_begin;
986 		if (copy_to_user((void __user *)arg, &ioctl_data, sizeof(ioctl_data))) {
987 			return -EFAULT;
988 		}
989 		DPRINT("GOLDFISH_DMA_IOC_GETOFF: return 0x%llx", dma->phys_begin);
990 		return 0;
991 	case GOLDFISH_DMA_IOC_CREATE_REGION:
992 		DPRINT("DMA_CREATE_REGION for pipe 0x%p\n", pipe);
993 		if (!is_page_size_multiple(ioctl_data.size)) {
994 			ERR("DMA_CREATE_REGION: %llu not a multiple of page size!\n",
995 				ioctl_data.size);
996 			return -EINVAL;
997 		}
998 		goldfish_pipe_dma_create_region(pipe, ioctl_data.size);
999 		return 0;
1000 	default:
1001 		return -ENOTTY;
1002 	}
1003 }
1004 
1005 static const struct file_operations goldfish_pipe_fops = {
1006 	.owner = THIS_MODULE,
1007 	.read = goldfish_pipe_read,
1008 	.write = goldfish_pipe_write,
1009 	.poll = goldfish_pipe_poll,
1010 	.open = goldfish_pipe_open,
1011 	.release = goldfish_pipe_release,
1012 	/* DMA-related operations */
1013 	.mmap = goldfish_dma_mmap,
1014 	.unlocked_ioctl = goldfish_dma_ioctl,
1015 	.compat_ioctl = goldfish_dma_ioctl,
1016 };
1017 
1018 static struct miscdevice goldfish_pipe_miscdev = {
1019 	.minor = MISC_DYNAMIC_MINOR,
1020 	.name = "goldfish_pipe",
1021 	.fops = &goldfish_pipe_fops,
1022 };
1023 
goldfish_pipe_device_init_v2(struct platform_device * pdev)1024 static int goldfish_pipe_device_init_v2(struct platform_device *pdev)
1025 {
1026 	char *page;
1027 	struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1028 	struct device *pdev_dev = &pdev->dev;
1029 	int err = devm_request_irq(pdev_dev, dev->irq, goldfish_pipe_interrupt,
1030 				IRQF_SHARED, "goldfish_pipe", dev);
1031 	if (err) {
1032 		dev_err(pdev_dev, "unable to allocate IRQ for v2\n");
1033 		return err;
1034 	}
1035 
1036 	err = misc_register(&goldfish_pipe_miscdev);
1037 	if (err) {
1038 		dev_err(pdev_dev, "unable to register v2 device\n");
1039 		return err;
1040 	}
1041 
1042 	dev->pdev_dev = pdev_dev;
1043 	dev->first_signalled_pipe = NULL;
1044 	dev->pipes_capacity = INITIAL_PIPES_CAPACITY;
1045 	dev->pipes = kcalloc(dev->pipes_capacity, sizeof(*dev->pipes), GFP_KERNEL);
1046 	if (!dev->pipes)
1047 		return -ENOMEM;
1048 
1049 	/*
1050 	 * We're going to pass two buffers, open_command_params and
1051 	 * signalled_pipe_buffers, to the host. This means each of those buffers
1052 	 * needs to be contained in a single physical page. The easiest choice is
1053 	 * to just allocate a page and place the buffers in it.
1054 	 */
1055 	BUG_ON(sizeof(*dev->buffers) > PAGE_SIZE);
1056 	page = (char *)__get_free_page(GFP_KERNEL);
1057 	if (!page) {
1058 		kfree(dev->pipes);
1059 		return -ENOMEM;
1060 	}
1061 	dev->buffers = (struct goldfish_pipe_dev_buffers *)page;
1062 
1063 	/* Send the buffer addresses to the host */
1064 	{
1065 		u64 paddr = __pa(&dev->buffers->signalled_pipe_buffers);
1066 		writel((u32)(unsigned long)(paddr >> 32), dev->base + PIPE_REG_SIGNAL_BUFFER_HIGH);
1067 		writel((u32)(unsigned long)paddr, dev->base + PIPE_REG_SIGNAL_BUFFER);
1068 		writel((u32)MAX_SIGNALLED_PIPES, dev->base + PIPE_REG_SIGNAL_BUFFER_COUNT);
1069 
1070 		paddr = __pa(&dev->buffers->open_command_params);
1071 		writel((u32)(unsigned long)(paddr >> 32), dev->base + PIPE_REG_OPEN_BUFFER_HIGH);
1072 		writel((u32)(unsigned long)paddr, dev->base + PIPE_REG_OPEN_BUFFER);
1073 	}
1074 
1075 	/* Perform initial checks for pipe DMA */
1076 	BUG_ON(sizeof(dma_addr_t) > sizeof(u64)); /* Only support up to 64-bit dma_addr_t's */
1077 	return 0;
1078 }
1079 
goldfish_pipe_device_deinit_v2(struct platform_device * pdev)1080 static void goldfish_pipe_device_deinit_v2(struct platform_device *pdev) {
1081 	struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1082 	misc_deregister(&goldfish_pipe_miscdev);
1083 	kfree(dev->pipes);
1084 	free_page((unsigned long)dev->buffers);
1085 }
1086 
goldfish_pipe_probe(struct platform_device * pdev)1087 static int goldfish_pipe_probe(struct platform_device *pdev)
1088 {
1089 	int err;
1090 	struct resource *r;
1091 	struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1092 	struct device *pdev_dev = &pdev->dev;
1093 
1094 	BUG_ON(sizeof(struct goldfish_pipe_command) > PAGE_SIZE);
1095 
1096 	/* not thread safe, but this should not happen */
1097 	WARN_ON(dev->base != NULL);
1098 
1099 	spin_lock_init(&dev->lock);
1100 
1101 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1102 	if (r == NULL || resource_size(r) < PAGE_SIZE) {
1103 		dev_err(pdev_dev, "can't allocate i/o page\n");
1104 		return -EINVAL;
1105 	}
1106 	dev->base = devm_ioremap(pdev_dev, r->start, PAGE_SIZE);
1107 	if (dev->base == NULL) {
1108 		dev_err(pdev_dev, "ioremap failed\n");
1109 		return -EINVAL;
1110 	}
1111 
1112 	r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1113 	if (r == NULL) {
1114 		err = -EINVAL;
1115 		goto error;
1116 	}
1117 	dev->irq = r->start;
1118 
1119 	/*
1120 	 * Exchange the versions with the host device
1121 	 *
1122 	 * Note: v1 driver used to not report its version, so we write it before
1123 	 *  reading device version back: this allows the host implementation to
1124 	 *  detect the old driver (if there was no version write before read).
1125 	 */
1126 	writel((u32)PIPE_DRIVER_VERSION, dev->base + PIPE_REG_VERSION);
1127 	dev->version = readl(dev->base + PIPE_REG_VERSION);
1128 	if (dev->version < PIPE_CURRENT_DEVICE_VERSION) {
1129 		/* initialize the old device version */
1130 		err = goldfish_pipe_device_init_v1(pdev);
1131 	} else {
1132 		/* Host device supports the new interface */
1133 		err = goldfish_pipe_device_init_v2(pdev);
1134 	}
1135 	if (!err)
1136 		return 0;
1137 
1138 error:
1139 	dev->base = NULL;
1140 	return err;
1141 }
1142 
goldfish_pipe_remove(struct platform_device * pdev)1143 static int goldfish_pipe_remove(struct platform_device *pdev)
1144 {
1145 	struct goldfish_pipe_dev *dev = goldfish_pipe_dev;
1146 	if (dev->version < PIPE_CURRENT_DEVICE_VERSION)
1147 		goldfish_pipe_device_deinit_v1(pdev);
1148 	else
1149 		goldfish_pipe_device_deinit_v2(pdev);
1150 	dev->base = NULL;
1151 	return 0;
1152 }
1153 
1154 static const struct acpi_device_id goldfish_pipe_acpi_match[] = {
1155 	{ "GFSH0003", 0 },
1156 	{ },
1157 };
1158 MODULE_DEVICE_TABLE(acpi, goldfish_pipe_acpi_match);
1159 
1160 static const struct of_device_id goldfish_pipe_of_match[] = {
1161 	{ .compatible = "generic,android-pipe", },
1162 	{},
1163 };
1164 MODULE_DEVICE_TABLE(of, goldfish_pipe_of_match);
1165 
1166 static struct platform_driver goldfish_pipe_driver = {
1167 	.probe = goldfish_pipe_probe,
1168 	.remove = goldfish_pipe_remove,
1169 	.driver = {
1170 		.name = "goldfish_pipe",
1171 		.owner = THIS_MODULE,
1172 		.of_match_table = goldfish_pipe_of_match,
1173 		.acpi_match_table = ACPI_PTR(goldfish_pipe_acpi_match),
1174 	}
1175 };
1176 
1177 module_platform_driver(goldfish_pipe_driver);
1178 MODULE_AUTHOR("David Turner <digit@google.com>");
1179 MODULE_LICENSE("GPL");
1180