vfio_pci_igd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VFIO PCI Intel Graphics support
  4. *
  5. * Copyright (C) 2016 Red Hat, Inc. All rights reserved.
  6. * Author: Alex Williamson <[email protected]>
  7. *
  8. * Register a device specific region through which to provide read-only
  9. * access to the Intel IGD opregion. The register defining the opregion
  10. * address is also virtualized to prevent user modification.
  11. */
  12. #include <linux/io.h>
  13. #include <linux/pci.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/vfio.h>
  16. #include "vfio_pci_priv.h"
  17. #define OPREGION_SIGNATURE "IntelGraphicsMem"
  18. #define OPREGION_SIZE (8 * 1024)
  19. #define OPREGION_PCI_ADDR 0xfc
  20. #define OPREGION_RVDA 0x3ba
  21. #define OPREGION_RVDS 0x3c2
  22. #define OPREGION_VERSION 0x16
  23. struct igd_opregion_vbt {
  24. void *opregion;
  25. void *vbt_ex;
  26. };
  27. /**
  28. * igd_opregion_shift_copy() - Copy OpRegion to user buffer and shift position.
  29. * @dst: User buffer ptr to copy to.
  30. * @off: Offset to user buffer ptr. Increased by bytes on return.
  31. * @src: Source buffer to copy from.
  32. * @pos: Increased by bytes on return.
  33. * @remaining: Decreased by bytes on return.
  34. * @bytes: Bytes to copy and adjust off, pos and remaining.
  35. *
  36. * Copy OpRegion to offset from specific source ptr and shift the offset.
  37. *
  38. * Return: 0 on success, -EFAULT otherwise.
  39. *
  40. */
  41. static inline unsigned long igd_opregion_shift_copy(char __user *dst,
  42. loff_t *off,
  43. void *src,
  44. loff_t *pos,
  45. size_t *remaining,
  46. size_t bytes)
  47. {
  48. if (copy_to_user(dst + (*off), src, bytes))
  49. return -EFAULT;
  50. *off += bytes;
  51. *pos += bytes;
  52. *remaining -= bytes;
  53. return 0;
  54. }
  55. static ssize_t vfio_pci_igd_rw(struct vfio_pci_core_device *vdev,
  56. char __user *buf, size_t count, loff_t *ppos,
  57. bool iswrite)
  58. {
  59. unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS;
  60. struct igd_opregion_vbt *opregionvbt = vdev->region[i].data;
  61. loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK, off = 0;
  62. size_t remaining;
  63. if (pos >= vdev->region[i].size || iswrite)
  64. return -EINVAL;
  65. count = min_t(size_t, count, vdev->region[i].size - pos);
  66. remaining = count;
  67. /* Copy until OpRegion version */
  68. if (remaining && pos < OPREGION_VERSION) {
  69. size_t bytes = min_t(size_t, remaining, OPREGION_VERSION - pos);
  70. if (igd_opregion_shift_copy(buf, &off,
  71. opregionvbt->opregion + pos, &pos,
  72. &remaining, bytes))
  73. return -EFAULT;
  74. }
  75. /* Copy patched (if necessary) OpRegion version */
  76. if (remaining && pos < OPREGION_VERSION + sizeof(__le16)) {
  77. size_t bytes = min_t(size_t, remaining,
  78. OPREGION_VERSION + sizeof(__le16) - pos);
  79. __le16 version = *(__le16 *)(opregionvbt->opregion +
  80. OPREGION_VERSION);
  81. /* Patch to 2.1 if OpRegion 2.0 has extended VBT */
  82. if (le16_to_cpu(version) == 0x0200 && opregionvbt->vbt_ex)
  83. version = cpu_to_le16(0x0201);
  84. if (igd_opregion_shift_copy(buf, &off,
  85. (u8 *)&version +
  86. (pos - OPREGION_VERSION),
  87. &pos, &remaining, bytes))
  88. return -EFAULT;
  89. }
  90. /* Copy until RVDA */
  91. if (remaining && pos < OPREGION_RVDA) {
  92. size_t bytes = min_t(size_t, remaining, OPREGION_RVDA - pos);
  93. if (igd_opregion_shift_copy(buf, &off,
  94. opregionvbt->opregion + pos, &pos,
  95. &remaining, bytes))
  96. return -EFAULT;
  97. }
  98. /* Copy modified (if necessary) RVDA */
  99. if (remaining && pos < OPREGION_RVDA + sizeof(__le64)) {
  100. size_t bytes = min_t(size_t, remaining,
  101. OPREGION_RVDA + sizeof(__le64) - pos);
  102. __le64 rvda = cpu_to_le64(opregionvbt->vbt_ex ?
  103. OPREGION_SIZE : 0);
  104. if (igd_opregion_shift_copy(buf, &off,
  105. (u8 *)&rvda + (pos - OPREGION_RVDA),
  106. &pos, &remaining, bytes))
  107. return -EFAULT;
  108. }
  109. /* Copy the rest of OpRegion */
  110. if (remaining && pos < OPREGION_SIZE) {
  111. size_t bytes = min_t(size_t, remaining, OPREGION_SIZE - pos);
  112. if (igd_opregion_shift_copy(buf, &off,
  113. opregionvbt->opregion + pos, &pos,
  114. &remaining, bytes))
  115. return -EFAULT;
  116. }
  117. /* Copy extended VBT if exists */
  118. if (remaining &&
  119. copy_to_user(buf + off, opregionvbt->vbt_ex + (pos - OPREGION_SIZE),
  120. remaining))
  121. return -EFAULT;
  122. *ppos += count;
  123. return count;
  124. }
  125. static void vfio_pci_igd_release(struct vfio_pci_core_device *vdev,
  126. struct vfio_pci_region *region)
  127. {
  128. struct igd_opregion_vbt *opregionvbt = region->data;
  129. if (opregionvbt->vbt_ex)
  130. memunmap(opregionvbt->vbt_ex);
  131. memunmap(opregionvbt->opregion);
  132. kfree(opregionvbt);
  133. }
  134. static const struct vfio_pci_regops vfio_pci_igd_regops = {
  135. .rw = vfio_pci_igd_rw,
  136. .release = vfio_pci_igd_release,
  137. };
  138. static int vfio_pci_igd_opregion_init(struct vfio_pci_core_device *vdev)
  139. {
  140. __le32 *dwordp = (__le32 *)(vdev->vconfig + OPREGION_PCI_ADDR);
  141. u32 addr, size;
  142. struct igd_opregion_vbt *opregionvbt;
  143. int ret;
  144. u16 version;
  145. ret = pci_read_config_dword(vdev->pdev, OPREGION_PCI_ADDR, &addr);
  146. if (ret)
  147. return ret;
  148. if (!addr || !(~addr))
  149. return -ENODEV;
  150. opregionvbt = kzalloc(sizeof(*opregionvbt), GFP_KERNEL);
  151. if (!opregionvbt)
  152. return -ENOMEM;
  153. opregionvbt->opregion = memremap(addr, OPREGION_SIZE, MEMREMAP_WB);
  154. if (!opregionvbt->opregion) {
  155. kfree(opregionvbt);
  156. return -ENOMEM;
  157. }
  158. if (memcmp(opregionvbt->opregion, OPREGION_SIGNATURE, 16)) {
  159. memunmap(opregionvbt->opregion);
  160. kfree(opregionvbt);
  161. return -EINVAL;
  162. }
  163. size = le32_to_cpu(*(__le32 *)(opregionvbt->opregion + 16));
  164. if (!size) {
  165. memunmap(opregionvbt->opregion);
  166. kfree(opregionvbt);
  167. return -EINVAL;
  168. }
  169. size *= 1024; /* In KB */
  170. /*
  171. * OpRegion and VBT:
  172. * When VBT data doesn't exceed 6KB, it's stored in Mailbox #4.
  173. * When VBT data exceeds 6KB size, Mailbox #4 is no longer large enough
  174. * to hold the VBT data, the Extended VBT region is introduced since
  175. * OpRegion 2.0 to hold the VBT data. Since OpRegion 2.0, RVDA/RVDS are
  176. * introduced to define the extended VBT data location and size.
  177. * OpRegion 2.0: RVDA defines the absolute physical address of the
  178. * extended VBT data, RVDS defines the VBT data size.
  179. * OpRegion 2.1 and above: RVDA defines the relative address of the
  180. * extended VBT data to OpRegion base, RVDS defines the VBT data size.
  181. *
  182. * Due to the RVDA definition diff in OpRegion VBT (also the only diff
  183. * between 2.0 and 2.1), exposing OpRegion and VBT as a contiguous range
  184. * for OpRegion 2.0 and above makes it possible to support the
  185. * non-contiguous VBT through a single vfio region. From r/w ops view,
  186. * only contiguous VBT after OpRegion with version 2.1+ is exposed,
  187. * regardless the host OpRegion is 2.0 or non-contiguous 2.1+. The r/w
  188. * ops will on-the-fly shift the actural offset into VBT so that data at
  189. * correct position can be returned to the requester.
  190. */
  191. version = le16_to_cpu(*(__le16 *)(opregionvbt->opregion +
  192. OPREGION_VERSION));
  193. if (version >= 0x0200) {
  194. u64 rvda = le64_to_cpu(*(__le64 *)(opregionvbt->opregion +
  195. OPREGION_RVDA));
  196. u32 rvds = le32_to_cpu(*(__le32 *)(opregionvbt->opregion +
  197. OPREGION_RVDS));
  198. /* The extended VBT is valid only when RVDA/RVDS are non-zero */
  199. if (rvda && rvds) {
  200. size += rvds;
  201. /*
  202. * Extended VBT location by RVDA:
  203. * Absolute physical addr for 2.0.
  204. * Relative addr to OpRegion header for 2.1+.
  205. */
  206. if (version == 0x0200)
  207. addr = rvda;
  208. else
  209. addr += rvda;
  210. opregionvbt->vbt_ex = memremap(addr, rvds, MEMREMAP_WB);
  211. if (!opregionvbt->vbt_ex) {
  212. memunmap(opregionvbt->opregion);
  213. kfree(opregionvbt);
  214. return -ENOMEM;
  215. }
  216. }
  217. }
  218. ret = vfio_pci_core_register_dev_region(vdev,
  219. PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
  220. VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &vfio_pci_igd_regops,
  221. size, VFIO_REGION_INFO_FLAG_READ, opregionvbt);
  222. if (ret) {
  223. if (opregionvbt->vbt_ex)
  224. memunmap(opregionvbt->vbt_ex);
  225. memunmap(opregionvbt->opregion);
  226. kfree(opregionvbt);
  227. return ret;
  228. }
  229. /* Fill vconfig with the hw value and virtualize register */
  230. *dwordp = cpu_to_le32(addr);
  231. memset(vdev->pci_config_map + OPREGION_PCI_ADDR,
  232. PCI_CAP_ID_INVALID_VIRT, 4);
  233. return ret;
  234. }
  235. static ssize_t vfio_pci_igd_cfg_rw(struct vfio_pci_core_device *vdev,
  236. char __user *buf, size_t count, loff_t *ppos,
  237. bool iswrite)
  238. {
  239. unsigned int i = VFIO_PCI_OFFSET_TO_INDEX(*ppos) - VFIO_PCI_NUM_REGIONS;
  240. struct pci_dev *pdev = vdev->region[i].data;
  241. loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK;
  242. size_t size;
  243. int ret;
  244. if (pos >= vdev->region[i].size || iswrite)
  245. return -EINVAL;
  246. size = count = min(count, (size_t)(vdev->region[i].size - pos));
  247. if ((pos & 1) && size) {
  248. u8 val;
  249. ret = pci_user_read_config_byte(pdev, pos, &val);
  250. if (ret)
  251. return ret;
  252. if (copy_to_user(buf + count - size, &val, 1))
  253. return -EFAULT;
  254. pos++;
  255. size--;
  256. }
  257. if ((pos & 3) && size > 2) {
  258. u16 val;
  259. __le16 lval;
  260. ret = pci_user_read_config_word(pdev, pos, &val);
  261. if (ret)
  262. return ret;
  263. lval = cpu_to_le16(val);
  264. if (copy_to_user(buf + count - size, &lval, 2))
  265. return -EFAULT;
  266. pos += 2;
  267. size -= 2;
  268. }
  269. while (size > 3) {
  270. u32 val;
  271. __le32 lval;
  272. ret = pci_user_read_config_dword(pdev, pos, &val);
  273. if (ret)
  274. return ret;
  275. lval = cpu_to_le32(val);
  276. if (copy_to_user(buf + count - size, &lval, 4))
  277. return -EFAULT;
  278. pos += 4;
  279. size -= 4;
  280. }
  281. while (size >= 2) {
  282. u16 val;
  283. __le16 lval;
  284. ret = pci_user_read_config_word(pdev, pos, &val);
  285. if (ret)
  286. return ret;
  287. lval = cpu_to_le16(val);
  288. if (copy_to_user(buf + count - size, &lval, 2))
  289. return -EFAULT;
  290. pos += 2;
  291. size -= 2;
  292. }
  293. while (size) {
  294. u8 val;
  295. ret = pci_user_read_config_byte(pdev, pos, &val);
  296. if (ret)
  297. return ret;
  298. if (copy_to_user(buf + count - size, &val, 1))
  299. return -EFAULT;
  300. pos++;
  301. size--;
  302. }
  303. *ppos += count;
  304. return count;
  305. }
  306. static void vfio_pci_igd_cfg_release(struct vfio_pci_core_device *vdev,
  307. struct vfio_pci_region *region)
  308. {
  309. struct pci_dev *pdev = region->data;
  310. pci_dev_put(pdev);
  311. }
  312. static const struct vfio_pci_regops vfio_pci_igd_cfg_regops = {
  313. .rw = vfio_pci_igd_cfg_rw,
  314. .release = vfio_pci_igd_cfg_release,
  315. };
  316. static int vfio_pci_igd_cfg_init(struct vfio_pci_core_device *vdev)
  317. {
  318. struct pci_dev *host_bridge, *lpc_bridge;
  319. int ret;
  320. host_bridge = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0, 0));
  321. if (!host_bridge)
  322. return -ENODEV;
  323. if (host_bridge->vendor != PCI_VENDOR_ID_INTEL ||
  324. host_bridge->class != (PCI_CLASS_BRIDGE_HOST << 8)) {
  325. pci_dev_put(host_bridge);
  326. return -EINVAL;
  327. }
  328. ret = vfio_pci_core_register_dev_region(vdev,
  329. PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
  330. VFIO_REGION_SUBTYPE_INTEL_IGD_HOST_CFG,
  331. &vfio_pci_igd_cfg_regops, host_bridge->cfg_size,
  332. VFIO_REGION_INFO_FLAG_READ, host_bridge);
  333. if (ret) {
  334. pci_dev_put(host_bridge);
  335. return ret;
  336. }
  337. lpc_bridge = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0x1f, 0));
  338. if (!lpc_bridge)
  339. return -ENODEV;
  340. if (lpc_bridge->vendor != PCI_VENDOR_ID_INTEL ||
  341. lpc_bridge->class != (PCI_CLASS_BRIDGE_ISA << 8)) {
  342. pci_dev_put(lpc_bridge);
  343. return -EINVAL;
  344. }
  345. ret = vfio_pci_core_register_dev_region(vdev,
  346. PCI_VENDOR_ID_INTEL | VFIO_REGION_TYPE_PCI_VENDOR_TYPE,
  347. VFIO_REGION_SUBTYPE_INTEL_IGD_LPC_CFG,
  348. &vfio_pci_igd_cfg_regops, lpc_bridge->cfg_size,
  349. VFIO_REGION_INFO_FLAG_READ, lpc_bridge);
  350. if (ret) {
  351. pci_dev_put(lpc_bridge);
  352. return ret;
  353. }
  354. return 0;
  355. }
  356. int vfio_pci_igd_init(struct vfio_pci_core_device *vdev)
  357. {
  358. int ret;
  359. ret = vfio_pci_igd_opregion_init(vdev);
  360. if (ret)
  361. return ret;
  362. ret = vfio_pci_igd_cfg_init(vdev);
  363. if (ret)
  364. return ret;
  365. return 0;
  366. }