firmware.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Zhi Wang <zhi.a.wang@intel.com>
  25. *
  26. * Contributors:
  27. * Changbin Du <changbin.du@intel.com>
  28. *
  29. */
  30. #include <linux/firmware.h>
  31. #include <linux/crc32.h>
  32. #include "i915_drv.h"
  33. #include "gvt.h"
  34. #include "i915_pvinfo.h"
  35. #define FIRMWARE_VERSION (0x0)
  36. struct gvt_firmware_header {
  37. u64 magic;
  38. u32 crc32; /* protect the data after this field */
  39. u32 version;
  40. u64 cfg_space_size;
  41. u64 cfg_space_offset; /* offset in the file */
  42. u64 mmio_size;
  43. u64 mmio_offset; /* offset in the file */
  44. unsigned char data[1];
  45. };
  46. #define dev_to_drm_minor(d) dev_get_drvdata((d))
  47. static ssize_t
  48. gvt_firmware_read(struct file *filp, struct kobject *kobj,
  49. struct bin_attribute *attr, char *buf,
  50. loff_t offset, size_t count)
  51. {
  52. memcpy(buf, attr->private + offset, count);
  53. return count;
  54. }
  55. static struct bin_attribute firmware_attr = {
  56. .attr = {.name = "gvt_firmware", .mode = (S_IRUSR)},
  57. .read = gvt_firmware_read,
  58. .write = NULL,
  59. .mmap = NULL,
  60. };
  61. static int mmio_snapshot_handler(struct intel_gvt *gvt, u32 offset, void *data)
  62. {
  63. struct drm_i915_private *dev_priv = gvt->dev_priv;
  64. *(u32 *)(data + offset) = I915_READ_NOTRACE(_MMIO(offset));
  65. return 0;
  66. }
  67. static int expose_firmware_sysfs(struct intel_gvt *gvt)
  68. {
  69. struct intel_gvt_device_info *info = &gvt->device_info;
  70. struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
  71. struct gvt_firmware_header *h;
  72. void *firmware;
  73. void *p;
  74. unsigned long size, crc32_start;
  75. int i, ret;
  76. size = sizeof(*h) + info->mmio_size + info->cfg_space_size;
  77. firmware = vzalloc(size);
  78. if (!firmware)
  79. return -ENOMEM;
  80. h = firmware;
  81. h->magic = VGT_MAGIC;
  82. h->version = FIRMWARE_VERSION;
  83. h->cfg_space_size = info->cfg_space_size;
  84. h->cfg_space_offset = offsetof(struct gvt_firmware_header, data);
  85. h->mmio_size = info->mmio_size;
  86. h->mmio_offset = h->cfg_space_offset + h->cfg_space_size;
  87. p = firmware + h->cfg_space_offset;
  88. for (i = 0; i < h->cfg_space_size; i += 4)
  89. pci_read_config_dword(pdev, i, p + i);
  90. memcpy(gvt->firmware.cfg_space, p, info->cfg_space_size);
  91. p = firmware + h->mmio_offset;
  92. /* Take a snapshot of hw mmio registers. */
  93. intel_gvt_for_each_tracked_mmio(gvt, mmio_snapshot_handler, p);
  94. memcpy(gvt->firmware.mmio, p, info->mmio_size);
  95. crc32_start = offsetof(struct gvt_firmware_header, crc32) + 4;
  96. h->crc32 = crc32_le(0, firmware + crc32_start, size - crc32_start);
  97. firmware_attr.size = size;
  98. firmware_attr.private = firmware;
  99. ret = device_create_bin_file(&pdev->dev, &firmware_attr);
  100. if (ret) {
  101. vfree(firmware);
  102. return ret;
  103. }
  104. return 0;
  105. }
  106. static void clean_firmware_sysfs(struct intel_gvt *gvt)
  107. {
  108. struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
  109. device_remove_bin_file(&pdev->dev, &firmware_attr);
  110. vfree(firmware_attr.private);
  111. }
  112. /**
  113. * intel_gvt_free_firmware - free GVT firmware
  114. * @gvt: intel gvt device
  115. *
  116. */
  117. void intel_gvt_free_firmware(struct intel_gvt *gvt)
  118. {
  119. if (!gvt->firmware.firmware_loaded)
  120. clean_firmware_sysfs(gvt);
  121. kfree(gvt->firmware.cfg_space);
  122. kfree(gvt->firmware.mmio);
  123. }
  124. static int verify_firmware(struct intel_gvt *gvt,
  125. const struct firmware *fw)
  126. {
  127. struct intel_gvt_device_info *info = &gvt->device_info;
  128. struct drm_i915_private *dev_priv = gvt->dev_priv;
  129. struct pci_dev *pdev = dev_priv->drm.pdev;
  130. struct gvt_firmware_header *h;
  131. unsigned long id, crc32_start;
  132. const void *mem;
  133. const char *item;
  134. u64 file, request;
  135. h = (struct gvt_firmware_header *)fw->data;
  136. crc32_start = offsetof(struct gvt_firmware_header, crc32) + 4;
  137. mem = fw->data + crc32_start;
  138. #define VERIFY(s, a, b) do { \
  139. item = (s); file = (u64)(a); request = (u64)(b); \
  140. if ((a) != (b)) \
  141. goto invalid_firmware; \
  142. } while (0)
  143. VERIFY("magic number", h->magic, VGT_MAGIC);
  144. VERIFY("version", h->version, FIRMWARE_VERSION);
  145. VERIFY("crc32", h->crc32, crc32_le(0, mem, fw->size - crc32_start));
  146. VERIFY("cfg space size", h->cfg_space_size, info->cfg_space_size);
  147. VERIFY("mmio size", h->mmio_size, info->mmio_size);
  148. mem = (fw->data + h->cfg_space_offset);
  149. id = *(u16 *)(mem + PCI_VENDOR_ID);
  150. VERIFY("vender id", id, pdev->vendor);
  151. id = *(u16 *)(mem + PCI_DEVICE_ID);
  152. VERIFY("device id", id, pdev->device);
  153. id = *(u8 *)(mem + PCI_REVISION_ID);
  154. VERIFY("revision id", id, pdev->revision);
  155. #undef VERIFY
  156. return 0;
  157. invalid_firmware:
  158. gvt_dbg_core("Invalid firmware: %s [file] 0x%llx [request] 0x%llx\n",
  159. item, file, request);
  160. return -EINVAL;
  161. }
  162. #define GVT_FIRMWARE_PATH "i915/gvt"
  163. /**
  164. * intel_gvt_load_firmware - load GVT firmware
  165. * @gvt: intel gvt device
  166. *
  167. */
  168. int intel_gvt_load_firmware(struct intel_gvt *gvt)
  169. {
  170. struct intel_gvt_device_info *info = &gvt->device_info;
  171. struct drm_i915_private *dev_priv = gvt->dev_priv;
  172. struct pci_dev *pdev = dev_priv->drm.pdev;
  173. struct intel_gvt_firmware *firmware = &gvt->firmware;
  174. struct gvt_firmware_header *h;
  175. const struct firmware *fw;
  176. char *path;
  177. void *mem;
  178. int ret;
  179. path = kmalloc(PATH_MAX, GFP_KERNEL);
  180. if (!path)
  181. return -ENOMEM;
  182. mem = kmalloc(info->cfg_space_size, GFP_KERNEL);
  183. if (!mem) {
  184. kfree(path);
  185. return -ENOMEM;
  186. }
  187. firmware->cfg_space = mem;
  188. mem = kmalloc(info->mmio_size, GFP_KERNEL);
  189. if (!mem) {
  190. kfree(path);
  191. kfree(firmware->cfg_space);
  192. return -ENOMEM;
  193. }
  194. firmware->mmio = mem;
  195. sprintf(path, "%s/vid_0x%04x_did_0x%04x_rid_0x%02x.golden_hw_state",
  196. GVT_FIRMWARE_PATH, pdev->vendor, pdev->device,
  197. pdev->revision);
  198. gvt_dbg_core("request hw state firmware %s...\n", path);
  199. ret = request_firmware(&fw, path, &dev_priv->drm.pdev->dev);
  200. kfree(path);
  201. if (ret)
  202. goto expose_firmware;
  203. gvt_dbg_core("success.\n");
  204. ret = verify_firmware(gvt, fw);
  205. if (ret)
  206. goto out_free_fw;
  207. gvt_dbg_core("verified.\n");
  208. h = (struct gvt_firmware_header *)fw->data;
  209. memcpy(firmware->cfg_space, fw->data + h->cfg_space_offset,
  210. h->cfg_space_size);
  211. memcpy(firmware->mmio, fw->data + h->mmio_offset,
  212. h->mmio_size);
  213. release_firmware(fw);
  214. firmware->firmware_loaded = true;
  215. return 0;
  216. out_free_fw:
  217. release_firmware(fw);
  218. expose_firmware:
  219. expose_firmware_sysfs(gvt);
  220. return 0;
  221. }