vfio_pci_rdwr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. return -EINVAL;
  106. end = pci_resource_len(pdev, bar);
  107. if (pos >= end)
  108. return -EINVAL;
  109. count = min(count, (size_t)(end - pos));
  110. if (bar == PCI_ROM_RESOURCE) {
  111. /*
  112. * The ROM can fill less space than the BAR, so we start the
  113. * excluded range at the end of the actual ROM. This makes
  114. * filling large ROM BARs much faster.
  115. */
  116. io = pci_map_rom(pdev, &x_start);
  117. if (!io)
  118. return -ENOMEM;
  119. x_end = end;
  120. } else if (!vdev->barmap[bar]) {
  121. int ret;
  122. ret = pci_request_selected_regions(pdev, 1 << bar, "vfio");
  123. if (ret)
  124. return ret;
  125. io = pci_iomap(pdev, bar, 0);
  126. if (!io) {
  127. pci_release_selected_regions(pdev, 1 << bar);
  128. return -ENOMEM;
  129. }
  130. vdev->barmap[bar] = io;
  131. } else
  132. io = vdev->barmap[bar];
  133. if (bar == vdev->msix_bar) {
  134. x_start = vdev->msix_offset;
  135. x_end = vdev->msix_offset + vdev->msix_size;
  136. }
  137. done = do_io_rw(io, buf, pos, count, x_start, x_end, iswrite);
  138. if (done >= 0)
  139. *ppos += done;
  140. if (bar == PCI_ROM_RESOURCE)
  141. pci_unmap_rom(pdev, io);
  142. return done;
  143. }
  144. ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
  145. size_t count, loff_t *ppos, bool iswrite)
  146. {
  147. int ret;
  148. loff_t off, pos = *ppos & VFIO_PCI_OFFSET_MASK;
  149. void __iomem *iomem = NULL;
  150. unsigned int rsrc;
  151. bool is_ioport;
  152. ssize_t done;
  153. if (!vdev->has_vga)
  154. return -EINVAL;
  155. if (pos > 0xbfffful)
  156. return -EINVAL;
  157. switch ((u32)pos) {
  158. case 0xa0000 ... 0xbffff:
  159. count = min(count, (size_t)(0xc0000 - pos));
  160. iomem = ioremap_nocache(0xa0000, 0xbffff - 0xa0000 + 1);
  161. off = pos - 0xa0000;
  162. rsrc = VGA_RSRC_LEGACY_MEM;
  163. is_ioport = false;
  164. break;
  165. case 0x3b0 ... 0x3bb:
  166. count = min(count, (size_t)(0x3bc - pos));
  167. iomem = ioport_map(0x3b0, 0x3bb - 0x3b0 + 1);
  168. off = pos - 0x3b0;
  169. rsrc = VGA_RSRC_LEGACY_IO;
  170. is_ioport = true;
  171. break;
  172. case 0x3c0 ... 0x3df:
  173. count = min(count, (size_t)(0x3e0 - pos));
  174. iomem = ioport_map(0x3c0, 0x3df - 0x3c0 + 1);
  175. off = pos - 0x3c0;
  176. rsrc = VGA_RSRC_LEGACY_IO;
  177. is_ioport = true;
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. if (!iomem)
  183. return -ENOMEM;
  184. ret = vga_get_interruptible(vdev->pdev, rsrc);
  185. if (ret) {
  186. is_ioport ? ioport_unmap(iomem) : iounmap(iomem);
  187. return ret;
  188. }
  189. done = do_io_rw(iomem, buf, off, count, 0, 0, iswrite);
  190. vga_put(vdev->pdev, rsrc);
  191. is_ioport ? ioport_unmap(iomem) : iounmap(iomem);
  192. if (done >= 0)
  193. *ppos += done;
  194. return done;
  195. }