firmware.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 RD(offset) (readl(mmio + offset.reg))
  47. #define WR(v, offset) (writel(v, mmio + offset.reg))
  48. static void bdw_forcewake_get(void __iomem *mmio)
  49. {
  50. WR(_MASKED_BIT_DISABLE(0xffff), FORCEWAKE_MT);
  51. RD(ECOBUS);
  52. if (wait_for((RD(FORCEWAKE_ACK_HSW) & FORCEWAKE_KERNEL) == 0, 50))
  53. gvt_err("fail to wait forcewake idle\n");
  54. WR(_MASKED_BIT_ENABLE(FORCEWAKE_KERNEL), FORCEWAKE_MT);
  55. if (wait_for((RD(FORCEWAKE_ACK_HSW) & FORCEWAKE_KERNEL), 50))
  56. gvt_err("fail to wait forcewake ack\n");
  57. if (wait_for((RD(GEN6_GT_THREAD_STATUS_REG) &
  58. GEN6_GT_THREAD_STATUS_CORE_MASK) == 0, 50))
  59. gvt_err("fail to wait c0 wake up\n");
  60. }
  61. #undef RD
  62. #undef WR
  63. #define dev_to_drm_minor(d) dev_get_drvdata((d))
  64. static ssize_t
  65. gvt_firmware_read(struct file *filp, struct kobject *kobj,
  66. struct bin_attribute *attr, char *buf,
  67. loff_t offset, size_t count)
  68. {
  69. memcpy(buf, attr->private + offset, count);
  70. return count;
  71. }
  72. static struct bin_attribute firmware_attr = {
  73. .attr = {.name = "gvt_firmware", .mode = (S_IRUSR)},
  74. .read = gvt_firmware_read,
  75. .write = NULL,
  76. .mmap = NULL,
  77. };
  78. static int expose_firmware_sysfs(struct intel_gvt *gvt,
  79. void __iomem *mmio)
  80. {
  81. struct intel_gvt_device_info *info = &gvt->device_info;
  82. struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
  83. struct intel_gvt_mmio_info *e;
  84. struct gvt_firmware_header *h;
  85. void *firmware;
  86. void *p;
  87. unsigned long size;
  88. int i;
  89. int ret;
  90. size = sizeof(*h) + info->mmio_size + info->cfg_space_size - 1;
  91. firmware = vmalloc(size);
  92. if (!firmware)
  93. return -ENOMEM;
  94. h = firmware;
  95. h->magic = VGT_MAGIC;
  96. h->version = FIRMWARE_VERSION;
  97. h->cfg_space_size = info->cfg_space_size;
  98. h->cfg_space_offset = offsetof(struct gvt_firmware_header, data);
  99. h->mmio_size = info->mmio_size;
  100. h->mmio_offset = h->cfg_space_offset + h->cfg_space_size;
  101. p = firmware + h->cfg_space_offset;
  102. for (i = 0; i < h->cfg_space_size; i += 4)
  103. pci_read_config_dword(pdev, i, p + i);
  104. memcpy(gvt->firmware.cfg_space, p, info->cfg_space_size);
  105. p = firmware + h->mmio_offset;
  106. hash_for_each(gvt->mmio.mmio_info_table, i, e, node) {
  107. int j;
  108. for (j = 0; j < e->length; j += 4)
  109. *(u32 *)(p + e->offset + j) =
  110. readl(mmio + e->offset + j);
  111. }
  112. memcpy(gvt->firmware.mmio, p, info->mmio_size);
  113. firmware_attr.size = size;
  114. firmware_attr.private = firmware;
  115. ret = device_create_bin_file(&pdev->dev, &firmware_attr);
  116. if (ret) {
  117. vfree(firmware);
  118. return ret;
  119. }
  120. return 0;
  121. }
  122. static void clean_firmware_sysfs(struct intel_gvt *gvt)
  123. {
  124. struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
  125. device_remove_bin_file(&pdev->dev, &firmware_attr);
  126. vfree(firmware_attr.private);
  127. }
  128. /**
  129. * intel_gvt_free_firmware - free GVT firmware
  130. * @gvt: intel gvt device
  131. *
  132. */
  133. void intel_gvt_free_firmware(struct intel_gvt *gvt)
  134. {
  135. if (!gvt->firmware.firmware_loaded)
  136. clean_firmware_sysfs(gvt);
  137. kfree(gvt->firmware.cfg_space);
  138. kfree(gvt->firmware.mmio);
  139. }
  140. static int verify_firmware(struct intel_gvt *gvt,
  141. const struct firmware *fw)
  142. {
  143. struct intel_gvt_device_info *info = &gvt->device_info;
  144. struct drm_i915_private *dev_priv = gvt->dev_priv;
  145. struct pci_dev *pdev = dev_priv->drm.pdev;
  146. struct gvt_firmware_header *h;
  147. unsigned long id, crc32_start;
  148. const void *mem;
  149. const char *item;
  150. u64 file, request;
  151. h = (struct gvt_firmware_header *)fw->data;
  152. crc32_start = offsetof(struct gvt_firmware_header, crc32) + 4;
  153. mem = fw->data + crc32_start;
  154. #define VERIFY(s, a, b) do { \
  155. item = (s); file = (u64)(a); request = (u64)(b); \
  156. if ((a) != (b)) \
  157. goto invalid_firmware; \
  158. } while (0)
  159. VERIFY("magic number", h->magic, VGT_MAGIC);
  160. VERIFY("version", h->version, FIRMWARE_VERSION);
  161. VERIFY("crc32", h->crc32, crc32_le(0, mem, fw->size - crc32_start));
  162. VERIFY("cfg space size", h->cfg_space_size, info->cfg_space_size);
  163. VERIFY("mmio size", h->mmio_size, info->mmio_size);
  164. mem = (fw->data + h->cfg_space_offset);
  165. id = *(u16 *)(mem + PCI_VENDOR_ID);
  166. VERIFY("vender id", id, pdev->vendor);
  167. id = *(u16 *)(mem + PCI_DEVICE_ID);
  168. VERIFY("device id", id, pdev->device);
  169. id = *(u8 *)(mem + PCI_REVISION_ID);
  170. VERIFY("revision id", id, pdev->revision);
  171. #undef VERIFY
  172. return 0;
  173. invalid_firmware:
  174. gvt_dbg_core("Invalid firmware: %s [file] 0x%llx [request] 0x%llx\n",
  175. item, file, request);
  176. return -EINVAL;
  177. }
  178. #define GVT_FIRMWARE_PATH "i915/gvt"
  179. /**
  180. * intel_gvt_load_firmware - load GVT firmware
  181. * @gvt: intel gvt device
  182. *
  183. */
  184. int intel_gvt_load_firmware(struct intel_gvt *gvt)
  185. {
  186. struct intel_gvt_device_info *info = &gvt->device_info;
  187. struct drm_i915_private *dev_priv = gvt->dev_priv;
  188. struct pci_dev *pdev = dev_priv->drm.pdev;
  189. struct intel_gvt_firmware *firmware = &gvt->firmware;
  190. struct gvt_firmware_header *h;
  191. const struct firmware *fw;
  192. char *path;
  193. void __iomem *mmio;
  194. void *mem;
  195. int ret;
  196. path = kmalloc(PATH_MAX, GFP_KERNEL);
  197. if (!path)
  198. return -ENOMEM;
  199. mem = kmalloc(info->cfg_space_size, GFP_KERNEL);
  200. if (!mem) {
  201. kfree(path);
  202. return -ENOMEM;
  203. }
  204. firmware->cfg_space = mem;
  205. mem = kmalloc(info->mmio_size, GFP_KERNEL);
  206. if (!mem) {
  207. kfree(path);
  208. kfree(firmware->cfg_space);
  209. return -ENOMEM;
  210. }
  211. firmware->mmio = mem;
  212. mmio = pci_iomap(pdev, info->mmio_bar, info->mmio_size);
  213. if (!mmio) {
  214. kfree(path);
  215. kfree(firmware->cfg_space);
  216. kfree(firmware->mmio);
  217. return -EINVAL;
  218. }
  219. if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv))
  220. bdw_forcewake_get(mmio);
  221. sprintf(path, "%s/vid_0x%04x_did_0x%04x_rid_0x%04x.golden_hw_state",
  222. GVT_FIRMWARE_PATH, pdev->vendor, pdev->device,
  223. pdev->revision);
  224. gvt_dbg_core("request hw state firmware %s...\n", path);
  225. ret = request_firmware(&fw, path, &dev_priv->drm.pdev->dev);
  226. kfree(path);
  227. if (ret)
  228. goto expose_firmware;
  229. gvt_dbg_core("success.\n");
  230. ret = verify_firmware(gvt, fw);
  231. if (ret)
  232. goto out_free_fw;
  233. gvt_dbg_core("verified.\n");
  234. h = (struct gvt_firmware_header *)fw->data;
  235. memcpy(firmware->cfg_space, fw->data + h->cfg_space_offset,
  236. h->cfg_space_size);
  237. memcpy(firmware->mmio, fw->data + h->mmio_offset,
  238. h->mmio_size);
  239. release_firmware(fw);
  240. firmware->firmware_loaded = true;
  241. pci_iounmap(pdev, mmio);
  242. return 0;
  243. out_free_fw:
  244. release_firmware(fw);
  245. expose_firmware:
  246. expose_firmware_sysfs(gvt, mmio);
  247. pci_iounmap(pdev, mmio);
  248. return 0;
  249. }