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