goldfish_pipe.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Copyright (C) 2011 Google, Inc.
  3. * Copyright (C) 2012 Intel, Inc.
  4. * Copyright (C) 2013 Intel, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. /* This source file contains the implementation of a special device driver
  17. * that intends to provide a *very* fast communication channel between the
  18. * guest system and the QEMU emulator.
  19. *
  20. * Usage from the guest is simply the following (error handling simplified):
  21. *
  22. * int fd = open("/dev/qemu_pipe",O_RDWR);
  23. * .... write() or read() through the pipe.
  24. *
  25. * This driver doesn't deal with the exact protocol used during the session.
  26. * It is intended to be as simple as something like:
  27. *
  28. * // do this _just_ after opening the fd to connect to a specific
  29. * // emulator service.
  30. * const char* msg = "<pipename>";
  31. * if (write(fd, msg, strlen(msg)+1) < 0) {
  32. * ... could not connect to <pipename> service
  33. * close(fd);
  34. * }
  35. *
  36. * // after this, simply read() and write() to communicate with the
  37. * // service. Exact protocol details left as an exercise to the reader.
  38. *
  39. * This driver is very fast because it doesn't copy any data through
  40. * intermediate buffers, since the emulator is capable of translating
  41. * guest user addresses into host ones.
  42. *
  43. * Note that we must however ensure that each user page involved in the
  44. * exchange is properly mapped during a transfer.
  45. */
  46. #include <linux/module.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/kernel.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/miscdevice.h>
  51. #include <linux/platform_device.h>
  52. #include <linux/poll.h>
  53. #include <linux/sched.h>
  54. #include <linux/bitops.h>
  55. #include <linux/slab.h>
  56. #include <linux/io.h>
  57. /*
  58. * IMPORTANT: The following constants must match the ones used and defined
  59. * in external/qemu/hw/goldfish_pipe.c in the Android source tree.
  60. */
  61. /* pipe device registers */
  62. #define PIPE_REG_COMMAND 0x00 /* write: value = command */
  63. #define PIPE_REG_STATUS 0x04 /* read */
  64. #define PIPE_REG_CHANNEL 0x08 /* read/write: channel id */
  65. #define PIPE_REG_SIZE 0x0c /* read/write: buffer size */
  66. #define PIPE_REG_ADDRESS 0x10 /* write: physical address */
  67. #define PIPE_REG_WAKES 0x14 /* read: wake flags */
  68. #define PIPE_REG_PARAMS_ADDR_LOW 0x18 /* read/write: batch data address */
  69. #define PIPE_REG_PARAMS_ADDR_HIGH 0x1c /* read/write: batch data address */
  70. #define PIPE_REG_ACCESS_PARAMS 0x20 /* write: batch access */
  71. /* list of commands for PIPE_REG_COMMAND */
  72. #define CMD_OPEN 1 /* open new channel */
  73. #define CMD_CLOSE 2 /* close channel (from guest) */
  74. #define CMD_POLL 3 /* poll read/write status */
  75. /* List of bitflags returned in status of CMD_POLL command */
  76. #define PIPE_POLL_IN (1 << 0)
  77. #define PIPE_POLL_OUT (1 << 1)
  78. #define PIPE_POLL_HUP (1 << 2)
  79. /* The following commands are related to write operations */
  80. #define CMD_WRITE_BUFFER 4 /* send a user buffer to the emulator */
  81. #define CMD_WAKE_ON_WRITE 5 /* tell the emulator to wake us when writing
  82. is possible */
  83. /* The following commands are related to read operations, they must be
  84. * listed in the same order than the corresponding write ones, since we
  85. * will use (CMD_READ_BUFFER - CMD_WRITE_BUFFER) as a special offset
  86. * in goldfish_pipe_read_write() below.
  87. */
  88. #define CMD_READ_BUFFER 6 /* receive a user buffer from the emulator */
  89. #define CMD_WAKE_ON_READ 7 /* tell the emulator to wake us when reading
  90. * is possible */
  91. /* Possible status values used to signal errors - see goldfish_pipe_error_convert */
  92. #define PIPE_ERROR_INVAL -1
  93. #define PIPE_ERROR_AGAIN -2
  94. #define PIPE_ERROR_NOMEM -3
  95. #define PIPE_ERROR_IO -4
  96. /* Bit-flags used to signal events from the emulator */
  97. #define PIPE_WAKE_CLOSED (1 << 0) /* emulator closed pipe */
  98. #define PIPE_WAKE_READ (1 << 1) /* pipe can now be read from */
  99. #define PIPE_WAKE_WRITE (1 << 2) /* pipe can now be written to */
  100. struct access_params {
  101. u32 channel;
  102. u32 size;
  103. u32 address;
  104. u32 cmd;
  105. u32 result;
  106. /* reserved for future extension */
  107. u32 flags;
  108. };
  109. /* The global driver data. Holds a reference to the i/o page used to
  110. * communicate with the emulator, and a wake queue for blocked tasks
  111. * waiting to be awoken.
  112. */
  113. struct goldfish_pipe_dev {
  114. spinlock_t lock;
  115. unsigned char __iomem *base;
  116. struct access_params *aps;
  117. int irq;
  118. };
  119. static struct goldfish_pipe_dev pipe_dev[1];
  120. /* This data type models a given pipe instance */
  121. struct goldfish_pipe {
  122. struct goldfish_pipe_dev *dev;
  123. struct mutex lock;
  124. unsigned long flags;
  125. wait_queue_head_t wake_queue;
  126. };
  127. /* Bit flags for the 'flags' field */
  128. enum {
  129. BIT_CLOSED_ON_HOST = 0, /* pipe closed by host */
  130. BIT_WAKE_ON_WRITE = 1, /* want to be woken on writes */
  131. BIT_WAKE_ON_READ = 2, /* want to be woken on reads */
  132. };
  133. static u32 goldfish_cmd_status(struct goldfish_pipe *pipe, u32 cmd)
  134. {
  135. unsigned long flags;
  136. u32 status;
  137. struct goldfish_pipe_dev *dev = pipe->dev;
  138. spin_lock_irqsave(&dev->lock, flags);
  139. writel((u32)pipe, dev->base + PIPE_REG_CHANNEL);
  140. writel(cmd, dev->base + PIPE_REG_COMMAND);
  141. status = readl(dev->base + PIPE_REG_STATUS);
  142. spin_unlock_irqrestore(&dev->lock, flags);
  143. return status;
  144. }
  145. static void goldfish_cmd(struct goldfish_pipe *pipe, u32 cmd)
  146. {
  147. unsigned long flags;
  148. struct goldfish_pipe_dev *dev = pipe->dev;
  149. spin_lock_irqsave(&dev->lock, flags);
  150. writel((u32)pipe, dev->base + PIPE_REG_CHANNEL);
  151. writel(cmd, dev->base + PIPE_REG_COMMAND);
  152. spin_unlock_irqrestore(&dev->lock, flags);
  153. }
  154. /* This function converts an error code returned by the emulator through
  155. * the PIPE_REG_STATUS i/o register into a valid negative errno value.
  156. */
  157. static int goldfish_pipe_error_convert(int status)
  158. {
  159. switch (status) {
  160. case PIPE_ERROR_AGAIN:
  161. return -EAGAIN;
  162. case PIPE_ERROR_NOMEM:
  163. return -ENOMEM;
  164. case PIPE_ERROR_IO:
  165. return -EIO;
  166. default:
  167. return -EINVAL;
  168. }
  169. }
  170. /*
  171. * Notice: QEMU will return 0 for un-known register access, indicating
  172. * param_acess is supported or not
  173. */
  174. static int valid_batchbuffer_addr(struct goldfish_pipe_dev *dev,
  175. struct access_params *aps)
  176. {
  177. u32 aph, apl;
  178. u64 paddr;
  179. aph = readl(dev->base + PIPE_REG_PARAMS_ADDR_HIGH);
  180. apl = readl(dev->base + PIPE_REG_PARAMS_ADDR_LOW);
  181. paddr = ((u64)aph << 32) | apl;
  182. if (paddr != (__pa(aps)))
  183. return 0;
  184. return 1;
  185. }
  186. /* 0 on success */
  187. static int setup_access_params_addr(struct platform_device *pdev,
  188. struct goldfish_pipe_dev *dev)
  189. {
  190. u64 paddr;
  191. struct access_params *aps;
  192. aps = devm_kzalloc(&pdev->dev, sizeof(struct access_params), GFP_KERNEL);
  193. if (!aps)
  194. return -1;
  195. /* FIXME */
  196. paddr = __pa(aps);
  197. writel((u32)(paddr >> 32), dev->base + PIPE_REG_PARAMS_ADDR_HIGH);
  198. writel((u32)paddr, dev->base + PIPE_REG_PARAMS_ADDR_LOW);
  199. if (valid_batchbuffer_addr(dev, aps)) {
  200. dev->aps = aps;
  201. return 0;
  202. } else
  203. return -1;
  204. }
  205. /* A value that will not be set by qemu emulator */
  206. #define INITIAL_BATCH_RESULT (0xdeadbeaf)
  207. static int access_with_param(struct goldfish_pipe_dev *dev, const int cmd,
  208. unsigned long address, unsigned long avail,
  209. struct goldfish_pipe *pipe, int *status)
  210. {
  211. struct access_params *aps = dev->aps;
  212. if (aps == NULL)
  213. return -1;
  214. aps->result = INITIAL_BATCH_RESULT;
  215. aps->channel = (unsigned long)pipe;
  216. aps->size = avail;
  217. aps->address = address;
  218. aps->cmd = cmd;
  219. writel(cmd, dev->base + PIPE_REG_ACCESS_PARAMS);
  220. /*
  221. * If the aps->result has not changed, that means
  222. * that the batch command failed
  223. */
  224. if (aps->result == INITIAL_BATCH_RESULT)
  225. return -1;
  226. *status = aps->result;
  227. return 0;
  228. }
  229. /* This function is used for both reading from and writing to a given
  230. * pipe.
  231. */
  232. static ssize_t goldfish_pipe_read_write(struct file *filp, char __user *buffer,
  233. size_t bufflen, int is_write)
  234. {
  235. unsigned long irq_flags;
  236. struct goldfish_pipe *pipe = filp->private_data;
  237. struct goldfish_pipe_dev *dev = pipe->dev;
  238. const int cmd_offset = is_write ? 0
  239. : (CMD_READ_BUFFER - CMD_WRITE_BUFFER);
  240. unsigned long address, address_end;
  241. int ret = 0;
  242. /* If the emulator already closed the pipe, no need to go further */
  243. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  244. return -EIO;
  245. /* Null reads or writes succeeds */
  246. if (unlikely(bufflen) == 0)
  247. return 0;
  248. /* Check the buffer range for access */
  249. if (!access_ok(is_write ? VERIFY_WRITE : VERIFY_READ,
  250. buffer, bufflen))
  251. return -EFAULT;
  252. /* Serialize access to the pipe */
  253. if (mutex_lock_interruptible(&pipe->lock))
  254. return -ERESTARTSYS;
  255. address = (unsigned long)(void *)buffer;
  256. address_end = address + bufflen;
  257. while (address < address_end) {
  258. unsigned long page_end = (address & PAGE_MASK) + PAGE_SIZE;
  259. unsigned long next = page_end < address_end ? page_end
  260. : address_end;
  261. unsigned long avail = next - address;
  262. int status, wakeBit;
  263. /* Ensure that the corresponding page is properly mapped */
  264. /* FIXME: this isn't safe or sufficient - use get_user_pages */
  265. if (is_write) {
  266. char c;
  267. /* Ensure that the page is mapped and readable */
  268. if (__get_user(c, (char __user *)address)) {
  269. if (!ret)
  270. ret = -EFAULT;
  271. break;
  272. }
  273. } else {
  274. /* Ensure that the page is mapped and writable */
  275. if (__put_user(0, (char __user *)address)) {
  276. if (!ret)
  277. ret = -EFAULT;
  278. break;
  279. }
  280. }
  281. /* Now, try to transfer the bytes in the current page */
  282. spin_lock_irqsave(&dev->lock, irq_flags);
  283. if (access_with_param(dev, CMD_WRITE_BUFFER + cmd_offset,
  284. address, avail, pipe, &status)) {
  285. writel((u32)pipe, dev->base + PIPE_REG_CHANNEL);
  286. writel(avail, dev->base + PIPE_REG_SIZE);
  287. writel(address, dev->base + PIPE_REG_ADDRESS);
  288. writel(CMD_WRITE_BUFFER + cmd_offset,
  289. dev->base + PIPE_REG_COMMAND);
  290. status = readl(dev->base + PIPE_REG_STATUS);
  291. }
  292. spin_unlock_irqrestore(&dev->lock, irq_flags);
  293. if (status > 0) { /* Correct transfer */
  294. ret += status;
  295. address += status;
  296. continue;
  297. }
  298. if (status == 0) /* EOF */
  299. break;
  300. /* An error occured. If we already transfered stuff, just
  301. * return with its count. We expect the next call to return
  302. * an error code */
  303. if (ret > 0)
  304. break;
  305. /* If the error is not PIPE_ERROR_AGAIN, or if we are not in
  306. * non-blocking mode, just return the error code.
  307. */
  308. if (status != PIPE_ERROR_AGAIN ||
  309. (filp->f_flags & O_NONBLOCK) != 0) {
  310. ret = goldfish_pipe_error_convert(status);
  311. break;
  312. }
  313. /* We will have to wait until more data/space is available.
  314. * First, mark the pipe as waiting for a specific wake signal.
  315. */
  316. wakeBit = is_write ? BIT_WAKE_ON_WRITE : BIT_WAKE_ON_READ;
  317. set_bit(wakeBit, &pipe->flags);
  318. /* Tell the emulator we're going to wait for a wake event */
  319. goldfish_cmd(pipe, CMD_WAKE_ON_WRITE + cmd_offset);
  320. /* Unlock the pipe, then wait for the wake signal */
  321. mutex_unlock(&pipe->lock);
  322. while (test_bit(wakeBit, &pipe->flags)) {
  323. if (wait_event_interruptible(
  324. pipe->wake_queue,
  325. !test_bit(wakeBit, &pipe->flags)))
  326. return -ERESTARTSYS;
  327. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  328. return -EIO;
  329. }
  330. /* Try to re-acquire the lock */
  331. if (mutex_lock_interruptible(&pipe->lock))
  332. return -ERESTARTSYS;
  333. /* Try the transfer again */
  334. continue;
  335. }
  336. mutex_unlock(&pipe->lock);
  337. return ret;
  338. }
  339. static ssize_t goldfish_pipe_read(struct file *filp, char __user *buffer,
  340. size_t bufflen, loff_t *ppos)
  341. {
  342. return goldfish_pipe_read_write(filp, buffer, bufflen, 0);
  343. }
  344. static ssize_t goldfish_pipe_write(struct file *filp,
  345. const char __user *buffer, size_t bufflen,
  346. loff_t *ppos)
  347. {
  348. return goldfish_pipe_read_write(filp, (char __user *)buffer,
  349. bufflen, 1);
  350. }
  351. static unsigned int goldfish_pipe_poll(struct file *filp, poll_table *wait)
  352. {
  353. struct goldfish_pipe *pipe = filp->private_data;
  354. unsigned int mask = 0;
  355. int status;
  356. mutex_lock(&pipe->lock);
  357. poll_wait(filp, &pipe->wake_queue, wait);
  358. status = goldfish_cmd_status(pipe, CMD_POLL);
  359. mutex_unlock(&pipe->lock);
  360. if (status & PIPE_POLL_IN)
  361. mask |= POLLIN | POLLRDNORM;
  362. if (status & PIPE_POLL_OUT)
  363. mask |= POLLOUT | POLLWRNORM;
  364. if (status & PIPE_POLL_HUP)
  365. mask |= POLLHUP;
  366. if (test_bit(BIT_CLOSED_ON_HOST, &pipe->flags))
  367. mask |= POLLERR;
  368. return mask;
  369. }
  370. static irqreturn_t goldfish_pipe_interrupt(int irq, void *dev_id)
  371. {
  372. struct goldfish_pipe_dev *dev = dev_id;
  373. unsigned long irq_flags;
  374. int count = 0;
  375. /* We're going to read from the emulator a list of (channel,flags)
  376. * pairs corresponding to the wake events that occured on each
  377. * blocked pipe (i.e. channel).
  378. */
  379. spin_lock_irqsave(&dev->lock, irq_flags);
  380. for (;;) {
  381. /* First read the channel, 0 means the end of the list */
  382. struct goldfish_pipe *pipe;
  383. unsigned long wakes;
  384. unsigned long channel = readl(dev->base + PIPE_REG_CHANNEL);
  385. if (channel == 0)
  386. break;
  387. /* Convert channel to struct pipe pointer + read wake flags */
  388. wakes = readl(dev->base + PIPE_REG_WAKES);
  389. pipe = (struct goldfish_pipe *)(ptrdiff_t)channel;
  390. /* Did the emulator just closed a pipe? */
  391. if (wakes & PIPE_WAKE_CLOSED) {
  392. set_bit(BIT_CLOSED_ON_HOST, &pipe->flags);
  393. wakes |= PIPE_WAKE_READ | PIPE_WAKE_WRITE;
  394. }
  395. if (wakes & PIPE_WAKE_READ)
  396. clear_bit(BIT_WAKE_ON_READ, &pipe->flags);
  397. if (wakes & PIPE_WAKE_WRITE)
  398. clear_bit(BIT_WAKE_ON_WRITE, &pipe->flags);
  399. wake_up_interruptible(&pipe->wake_queue);
  400. count++;
  401. }
  402. spin_unlock_irqrestore(&dev->lock, irq_flags);
  403. return (count == 0) ? IRQ_NONE : IRQ_HANDLED;
  404. }
  405. /**
  406. * goldfish_pipe_open - open a channel to the AVD
  407. * @inode: inode of device
  408. * @file: file struct of opener
  409. *
  410. * Create a new pipe link between the emulator and the use application.
  411. * Each new request produces a new pipe.
  412. *
  413. * Note: we use the pipe ID as a mux. All goldfish emulations are 32bit
  414. * right now so this is fine. A move to 64bit will need this addressing
  415. */
  416. static int goldfish_pipe_open(struct inode *inode, struct file *file)
  417. {
  418. struct goldfish_pipe *pipe;
  419. struct goldfish_pipe_dev *dev = pipe_dev;
  420. int32_t status;
  421. /* Allocate new pipe kernel object */
  422. pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
  423. if (pipe == NULL)
  424. return -ENOMEM;
  425. pipe->dev = dev;
  426. mutex_init(&pipe->lock);
  427. init_waitqueue_head(&pipe->wake_queue);
  428. /*
  429. * Now, tell the emulator we're opening a new pipe. We use the
  430. * pipe object's address as the channel identifier for simplicity.
  431. */
  432. status = goldfish_cmd_status(pipe, CMD_OPEN);
  433. if (status < 0) {
  434. kfree(pipe);
  435. return status;
  436. }
  437. /* All is done, save the pipe into the file's private data field */
  438. file->private_data = pipe;
  439. return 0;
  440. }
  441. static int goldfish_pipe_release(struct inode *inode, struct file *filp)
  442. {
  443. struct goldfish_pipe *pipe = filp->private_data;
  444. /* The guest is closing the channel, so tell the emulator right now */
  445. goldfish_cmd(pipe, CMD_CLOSE);
  446. kfree(pipe);
  447. filp->private_data = NULL;
  448. return 0;
  449. }
  450. static const struct file_operations goldfish_pipe_fops = {
  451. .owner = THIS_MODULE,
  452. .read = goldfish_pipe_read,
  453. .write = goldfish_pipe_write,
  454. .poll = goldfish_pipe_poll,
  455. .open = goldfish_pipe_open,
  456. .release = goldfish_pipe_release,
  457. };
  458. static struct miscdevice goldfish_pipe_device = {
  459. .minor = MISC_DYNAMIC_MINOR,
  460. .name = "goldfish_pipe",
  461. .fops = &goldfish_pipe_fops,
  462. };
  463. static int goldfish_pipe_probe(struct platform_device *pdev)
  464. {
  465. int err;
  466. struct resource *r;
  467. struct goldfish_pipe_dev *dev = pipe_dev;
  468. /* not thread safe, but this should not happen */
  469. WARN_ON(dev->base != NULL);
  470. spin_lock_init(&dev->lock);
  471. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  472. if (r == NULL || resource_size(r) < PAGE_SIZE) {
  473. dev_err(&pdev->dev, "can't allocate i/o page\n");
  474. return -EINVAL;
  475. }
  476. dev->base = devm_ioremap(&pdev->dev, r->start, PAGE_SIZE);
  477. if (dev->base == NULL) {
  478. dev_err(&pdev->dev, "ioremap failed\n");
  479. return -EINVAL;
  480. }
  481. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  482. if (r == NULL) {
  483. err = -EINVAL;
  484. goto error;
  485. }
  486. dev->irq = r->start;
  487. err = devm_request_irq(&pdev->dev, dev->irq, goldfish_pipe_interrupt,
  488. IRQF_SHARED, "goldfish_pipe", dev);
  489. if (err) {
  490. dev_err(&pdev->dev, "unable to allocate IRQ\n");
  491. goto error;
  492. }
  493. err = misc_register(&goldfish_pipe_device);
  494. if (err) {
  495. dev_err(&pdev->dev, "unable to register device\n");
  496. goto error;
  497. }
  498. setup_access_params_addr(pdev, dev);
  499. return 0;
  500. error:
  501. dev->base = NULL;
  502. return err;
  503. }
  504. static int goldfish_pipe_remove(struct platform_device *pdev)
  505. {
  506. struct goldfish_pipe_dev *dev = pipe_dev;
  507. misc_deregister(&goldfish_pipe_device);
  508. dev->base = NULL;
  509. return 0;
  510. }
  511. static struct platform_driver goldfish_pipe = {
  512. .probe = goldfish_pipe_probe,
  513. .remove = goldfish_pipe_remove,
  514. .driver = {
  515. .name = "goldfish_pipe"
  516. }
  517. };
  518. module_platform_driver(goldfish_pipe);
  519. MODULE_AUTHOR("David Turner <digit@google.com>");
  520. MODULE_LICENSE("GPL");