vfio_pci_rdwr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * VFIO PCI I/O Port & MMIO access
  3. *
  4. * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
  5. * Author: Alex Williamson <alex.williamson@redhat.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Derived from original vfio:
  12. * Copyright 2010 Cisco Systems, Inc. All rights reserved.
  13. * Author: Tom Lyon, pugs@cisco.com
  14. */
  15. #include <linux/fs.h>
  16. #include <linux/pci.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/io.h>
  19. #include <linux/vgaarb.h>
  20. #include "vfio_pci_private.h"
  21. /*
  22. * Read or write from an __iomem region (MMIO or I/O port) with an excluded
  23. * range which is inaccessible. The excluded range drops writes and fills
  24. * reads with -1. This is intended for handling MSI-X vector tables and
  25. * leftover space for ROM BARs.
  26. */
  27. static ssize_t do_io_rw(void __iomem *io, char __user *buf,
  28. loff_t off, size_t count, size_t x_start,
  29. size_t x_end, bool iswrite)
  30. {
  31. ssize_t done = 0;
  32. while (count) {
  33. size_t fillable, filled;
  34. if (off < x_start)
  35. fillable = min(count, (size_t)(x_start - off));
  36. else if (off >= x_end)
  37. fillable = count;
  38. else
  39. fillable = 0;
  40. if (fillable >= 4 && !(off % 4)) {
  41. __le32 val;
  42. if (iswrite) {
  43. if (copy_from_user(&val, buf, 4))
  44. return -EFAULT;
  45. iowrite32(le32_to_cpu(val), io + off);
  46. } else {
  47. val = cpu_to_le32(ioread32(io + off));
  48. if (copy_to_user(buf, &val, 4))
  49. return -EFAULT;
  50. }
  51. filled = 4;
  52. } else if (fillable >= 2 && !(off % 2)) {
  53. __le16 val;
  54. if (iswrite) {
  55. if (copy_from_user(&val, buf, 2))
  56. return -EFAULT;
  57. iowrite16(le16_to_cpu(val), io + off);
  58. } else {
  59. val = cpu_to_le16(ioread16(io + off));
  60. if (copy_to_user(buf, &val, 2))
  61. return -EFAULT;
  62. }
  63. filled = 2;
  64. } else if (fillable) {
  65. u8 val;
  66. if (iswrite) {
  67. if (copy_from_user(&val, buf, 1))
  68. return -EFAULT;
  69. iowrite8(val, io + off);
  70. } else {
  71. val = ioread8(io + off);
  72. if (copy_to_user(buf, &val, 1))
  73. return -EFAULT;
  74. }
  75. filled = 1;
  76. } else {
  77. /* Fill reads with -1, drop writes */
  78. filled = min(count, (size_t)(x_end - off));
  79. if (!iswrite) {
  80. u8 val = 0xFF;
  81. size_t i;
  82. for (i = 0; i < filled; i++)
  83. if (copy_to_user(buf + i, &val, 1))
  84. return -EFAULT;
  85. }
  86. }
  87. count -= filled;
  88. done += filled;
  89. off += filled;
  90. buf += filled;
  91. }
  92. return done;
  93. }
  94. ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
  95. size_t count, loff_t *ppos, bool iswrite)
  96. {
  97. struct pci_dev *pdev = vdev->pdev;
  98. loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
  99. int bar = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
  100. size_t x_start = 0, x_end = 0;
  101. resource_size_t end;
  102. void __iomem *io;
  103. ssize_t done;
  104. if (pci_resource_start(pdev, bar))
  105. end = pci_resource_len(pdev, bar);
  106. else if (bar == PCI_ROM_RESOURCE &&
  107. pdev->resource[bar].flags & IORESOURCE_ROM_SHADOW)
  108. end = 0x20000;
  109. else
  110. return -EINVAL;
  111. if (pos >= end)
  112. return -EINVAL;
  113. count = min(count, (size_t)(end - pos));
  114. if (bar == PCI_ROM_RESOURCE) {
  115. /*
  116. * The ROM can fill less space than the BAR, so we start the
  117. * excluded range at the end of the actual ROM. This makes
  118. * filling large ROM BARs much faster.
  119. */
  120. io = pci_map_rom(pdev, &x_start);
  121. if (!io)
  122. return -ENOMEM;
  123. x_end = end;
  124. } else if (!vdev->barmap[bar]) {
  125. int ret;
  126. ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
  127. if (ret)
  128. return ret;
  129. io = pci_iomap(pdev, bar, 0);
  130. if (!io) {
  131. pci_release_selected_regions(pdev, 1 << bar);
  132. return -ENOMEM;
  133. }
  134. vdev->barmap[bar] = io;
  135. } else
  136. io = vdev->barmap[bar];
  137. if (bar == vdev->msix_bar) {
  138. x_start = vdev->msix_offset;
  139. x_end = vdev->msix_offset + vdev->msix_size;
  140. }
  141. done = do_io_rw(io, buf, pos, count, x_start, x_end, iswrite);
  142. if (done >= 0)
  143. *ppos += done;
  144. if (bar == PCI_ROM_RESOURCE)
  145. pci_unmap_rom(pdev, io);
  146. return done;
  147. }
  148. ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
  149. size_t count, loff_t *ppos, bool iswrite)
  150. {
  151. int ret;
  152. loff_t off, pos = *ppos & VFIO_PCI_OFFSET_MASK;
  153. void __iomem *iomem = NULL;
  154. unsigned int rsrc;
  155. bool is_ioport;
  156. ssize_t done;
  157. if (!vdev->has_vga)
  158. return -EINVAL;
  159. if (pos > 0xbfffful)
  160. return -EINVAL;
  161. switch ((u32)pos) {
  162. case 0xa0000 ... 0xbffff:
  163. count = min(count, (size_t)(0xc0000 - pos));
  164. iomem = ioremap_nocache(0xa0000, 0xbffff - 0xa0000 + 1);
  165. off = pos - 0xa0000;
  166. rsrc = VGA_RSRC_LEGACY_MEM;
  167. is_ioport = false;
  168. break;
  169. case 0x3b0 ... 0x3bb:
  170. count = min(count, (size_t)(0x3bc - pos));
  171. iomem = ioport_map(0x3b0, 0x3bb - 0x3b0 + 1);
  172. off = pos - 0x3b0;
  173. rsrc = VGA_RSRC_LEGACY_IO;
  174. is_ioport = true;
  175. break;
  176. case 0x3c0 ... 0x3df:
  177. count = min(count, (size_t)(0x3e0 - pos));
  178. iomem = ioport_map(0x3c0, 0x3df - 0x3c0 + 1);
  179. off = pos - 0x3c0;
  180. rsrc = VGA_RSRC_LEGACY_IO;
  181. is_ioport = true;
  182. break;
  183. default:
  184. return -EINVAL;
  185. }
  186. if (!iomem)
  187. return -ENOMEM;
  188. ret = vga_get_interruptible(vdev->pdev, rsrc);
  189. if (ret) {
  190. is_ioport ? ioport_unmap(iomem) : iounmap(iomem);
  191. return ret;
  192. }
  193. done = do_io_rw(iomem, buf, off, count, 0, 0, iswrite);
  194. vga_put(vdev->pdev, rsrc);
  195. is_ioport ? ioport_unmap(iomem) : iounmap(iomem);
  196. if (done >= 0)
  197. *ppos += done;
  198. return done;
  199. }