firmware.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 expose_firmware_sysfs(struct intel_gvt *gvt)
  62. {
  63. struct drm_i915_private *dev_priv = gvt->dev_priv;
  64. struct intel_gvt_device_info *info = &gvt->device_info;
  65. struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
  66. struct intel_gvt_mmio_info *e;
  67. struct gvt_firmware_header *h;
  68. void *firmware;
  69. void *p;
  70. unsigned long size;
  71. int i;
  72. int ret;
  73. size = sizeof(*h) + info->mmio_size + info->cfg_space_size - 1;
  74. firmware = vzalloc(size);
  75. if (!firmware)
  76. return -ENOMEM;
  77. h = firmware;
  78. h->magic = VGT_MAGIC;
  79. h->version = FIRMWARE_VERSION;
  80. h->cfg_space_size = info->cfg_space_size;
  81. h->cfg_space_offset = offsetof(struct gvt_firmware_header, data);
  82. h->mmio_size = info->mmio_size;
  83. h->mmio_offset = h->cfg_space_offset + h->cfg_space_size;
  84. p = firmware + h->cfg_space_offset;
  85. for (i = 0; i < h->cfg_space_size; i += 4)
  86. pci_read_config_dword(pdev, i, p + i);
  87. memcpy(gvt->firmware.cfg_space, p, info->cfg_space_size);
  88. p = firmware + h->mmio_offset;
  89. hash_for_each(gvt->mmio.mmio_info_table, i, e, node) {
  90. int j;
  91. for (j = 0; j < e->length; j += 4)
  92. *(u32 *)(p + e->offset + j) =
  93. I915_READ_NOTRACE(_MMIO(e->offset + j));
  94. }
  95. memcpy(gvt->firmware.mmio, p, info->mmio_size);
  96. firmware_attr.size = size;
  97. firmware_attr.private = firmware;
  98. ret = device_create_bin_file(&pdev->dev, &firmware_attr);
  99. if (ret) {
  100. vfree(firmware);
  101. return ret;
  102. }
  103. return 0;
  104. }
  105. static void clean_firmware_sysfs(struct intel_gvt *gvt)
  106. {
  107. struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
  108. device_remove_bin_file(&pdev->dev, &firmware_attr);
  109. vfree(firmware_attr.private);
  110. }
  111. /**
  112. * intel_gvt_free_firmware - free GVT firmware
  113. * @gvt: intel gvt device
  114. *
  115. */
  116. void intel_gvt_free_firmware(struct intel_gvt *gvt)
  117. {
  118. if (!gvt->firmware.firmware_loaded)
  119. clean_firmware_sysfs(gvt);
  120. kfree(gvt->firmware.cfg_space);
  121. kfree(gvt->firmware.mmio);
  122. }
  123. static int verify_firmware(struct intel_gvt *gvt,
  124. const struct firmware *fw)
  125. {
  126. struct intel_gvt_device_info *info = &gvt->device_info;
  127. struct drm_i915_private *dev_priv = gvt->dev_priv;
  128. struct pci_dev *pdev = dev_priv->drm.pdev;
  129. struct gvt_firmware_header *h;
  130. unsigned long id, crc32_start;
  131. const void *mem;
  132. const char *item;
  133. u64 file, request;
  134. h = (struct gvt_firmware_header *)fw->data;
  135. crc32_start = offsetof(struct gvt_firmware_header, crc32) + 4;
  136. mem = fw->data + crc32_start;
  137. #define VERIFY(s, a, b) do { \
  138. item = (s); file = (u64)(a); request = (u64)(b); \
  139. if ((a) != (b)) \
  140. goto invalid_firmware; \
  141. } while (0)
  142. VERIFY("magic number", h->magic, VGT_MAGIC);
  143. VERIFY("version", h->version, FIRMWARE_VERSION);
  144. VERIFY("crc32", h->crc32, crc32_le(0, mem, fw->size - crc32_start));
  145. VERIFY("cfg space size", h->cfg_space_size, info->cfg_space_size);
  146. VERIFY("mmio size", h->mmio_size, info->mmio_size);
  147. mem = (fw->data + h->cfg_space_offset);
  148. id = *(u16 *)(mem + PCI_VENDOR_ID);
  149. VERIFY("vender id", id, pdev->vendor);
  150. id = *(u16 *)(mem + PCI_DEVICE_ID);
  151. VERIFY("device id", id, pdev->device);
  152. id = *(u8 *)(mem + PCI_REVISION_ID);
  153. VERIFY("revision id", id, pdev->revision);
  154. #undef VERIFY
  155. return 0;
  156. invalid_firmware:
  157. gvt_dbg_core("Invalid firmware: %s [file] 0x%llx [request] 0x%llx\n",
  158. item, file, request);
  159. return -EINVAL;
  160. }
  161. #define GVT_FIRMWARE_PATH "i915/gvt"
  162. /**
  163. * intel_gvt_load_firmware - load GVT firmware
  164. * @gvt: intel gvt device
  165. *
  166. */
  167. int intel_gvt_load_firmware(struct intel_gvt *gvt)
  168. {
  169. struct intel_gvt_device_info *info = &gvt->device_info;
  170. struct drm_i915_private *dev_priv = gvt->dev_priv;
  171. struct pci_dev *pdev = dev_priv->drm.pdev;
  172. struct intel_gvt_firmware *firmware = &gvt->firmware;
  173. struct gvt_firmware_header *h;
  174. const struct firmware *fw;
  175. char *path;
  176. void *mem;
  177. int ret;
  178. path = kmalloc(PATH_MAX, GFP_KERNEL);
  179. if (!path)
  180. return -ENOMEM;
  181. mem = kmalloc(info->cfg_space_size, GFP_KERNEL);
  182. if (!mem) {
  183. kfree(path);
  184. return -ENOMEM;
  185. }
  186. firmware->cfg_space = mem;
  187. mem = kmalloc(info->mmio_size, GFP_KERNEL);
  188. if (!mem) {
  189. kfree(path);
  190. kfree(firmware->cfg_space);
  191. return -ENOMEM;
  192. }
  193. firmware->mmio = mem;
  194. sprintf(path, "%s/vid_0x%04x_did_0x%04x_rid_0x%04x.golden_hw_state",
  195. GVT_FIRMWARE_PATH, pdev->vendor, pdev->device,
  196. pdev->revision);
  197. gvt_dbg_core("request hw state firmware %s...\n", path);
  198. ret = request_firmware(&fw, path, &dev_priv->drm.pdev->dev);
  199. kfree(path);
  200. if (ret)
  201. goto expose_firmware;
  202. gvt_dbg_core("success.\n");
  203. ret = verify_firmware(gvt, fw);
  204. if (ret)
  205. goto out_free_fw;
  206. gvt_dbg_core("verified.\n");
  207. h = (struct gvt_firmware_header *)fw->data;
  208. memcpy(firmware->cfg_space, fw->data + h->cfg_space_offset,
  209. h->cfg_space_size);
  210. memcpy(firmware->mmio, fw->data + h->mmio_offset,
  211. h->mmio_size);
  212. release_firmware(fw);
  213. firmware->firmware_loaded = true;
  214. return 0;
  215. out_free_fw:
  216. release_firmware(fw);
  217. expose_firmware:
  218. expose_firmware_sysfs(gvt);
  219. return 0;
  220. }