]> Git Repo - J-linux.git/blob - drivers/gpu/drm/i915/gvt/firmware.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / drivers / gpu / drm / i915 / gvt / firmware.c
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 <[email protected]>
25  *
26  * Contributors:
27  *    Changbin Du <[email protected]>
28  *
29  */
30
31 #include <linux/firmware.h>
32 #include <linux/crc32.h>
33 #include <linux/vmalloc.h>
34
35 #include "i915_drv.h"
36 #include "gvt.h"
37 #include "i915_pvinfo.h"
38
39 #define FIRMWARE_VERSION (0x0)
40
41 struct gvt_firmware_header {
42         u64 magic;
43         u32 crc32;              /* protect the data after this field */
44         u32 version;
45         u64 cfg_space_size;
46         u64 cfg_space_offset;   /* offset in the file */
47         u64 mmio_size;
48         u64 mmio_offset;        /* offset in the file */
49         unsigned char data[];
50 };
51
52 #define dev_to_drm_minor(d) dev_get_drvdata((d))
53
54 static BIN_ATTR_SIMPLE_ADMIN_RO(gvt_firmware);
55
56 static int expose_firmware_sysfs(struct intel_gvt *gvt)
57 {
58         struct intel_gvt_device_info *info = &gvt->device_info;
59         struct drm_i915_private *i915 = gvt->gt->i915;
60         struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
61         struct gvt_firmware_header *h;
62         void *firmware;
63         void *p;
64         unsigned long size, crc32_start;
65         int ret;
66
67         size = offsetof(struct gvt_firmware_header, data) + info->mmio_size + info->cfg_space_size;
68         firmware = vzalloc(size);
69         if (!firmware)
70                 return -ENOMEM;
71
72         h = firmware;
73
74         h->magic = VGT_MAGIC;
75         h->version = FIRMWARE_VERSION;
76         h->cfg_space_size = info->cfg_space_size;
77         h->cfg_space_offset = offsetof(struct gvt_firmware_header, data);
78         h->mmio_size = info->mmio_size;
79         h->mmio_offset = h->cfg_space_offset + h->cfg_space_size;
80
81         p = firmware + h->cfg_space_offset;
82
83         memcpy(gvt->firmware.cfg_space, i915->vgpu.initial_cfg_space,
84                info->cfg_space_size);
85         memcpy(p, gvt->firmware.cfg_space, info->cfg_space_size);
86
87         p = firmware + h->mmio_offset;
88
89         memcpy(gvt->firmware.mmio, i915->vgpu.initial_mmio,
90                info->mmio_size);
91
92         memcpy(p, gvt->firmware.mmio, info->mmio_size);
93
94         crc32_start = offsetof(struct gvt_firmware_header, version);
95         h->crc32 = crc32_le(0, firmware + crc32_start, size - crc32_start);
96
97         bin_attr_gvt_firmware.size = size;
98         bin_attr_gvt_firmware.private = firmware;
99
100         ret = device_create_bin_file(&pdev->dev, &bin_attr_gvt_firmware);
101         if (ret) {
102                 vfree(firmware);
103                 return ret;
104         }
105         return 0;
106 }
107
108 static void clean_firmware_sysfs(struct intel_gvt *gvt)
109 {
110         struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
111
112         device_remove_bin_file(&pdev->dev, &bin_attr_gvt_firmware);
113         vfree(bin_attr_gvt_firmware.private);
114 }
115
116 /**
117  * intel_gvt_free_firmware - free GVT firmware
118  * @gvt: intel gvt device
119  *
120  */
121 void intel_gvt_free_firmware(struct intel_gvt *gvt)
122 {
123         if (!gvt->firmware.firmware_loaded)
124                 clean_firmware_sysfs(gvt);
125
126         kfree(gvt->firmware.cfg_space);
127         vfree(gvt->firmware.mmio);
128 }
129
130 static int verify_firmware(struct intel_gvt *gvt,
131                            const struct firmware *fw)
132 {
133         struct intel_gvt_device_info *info = &gvt->device_info;
134         struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
135         struct gvt_firmware_header *h;
136         unsigned long id, crc32_start;
137         const void *mem;
138         const char *item;
139         u64 file, request;
140
141         h = (struct gvt_firmware_header *)fw->data;
142
143         crc32_start = offsetofend(struct gvt_firmware_header, crc32);
144         mem = fw->data + crc32_start;
145
146 #define VERIFY(s, a, b) do { \
147         item = (s); file = (u64)(a); request = (u64)(b); \
148         if ((a) != (b)) \
149                 goto invalid_firmware; \
150 } while (0)
151
152         VERIFY("magic number", h->magic, VGT_MAGIC);
153         VERIFY("version", h->version, FIRMWARE_VERSION);
154         VERIFY("crc32", h->crc32, crc32_le(0, mem, fw->size - crc32_start));
155         VERIFY("cfg space size", h->cfg_space_size, info->cfg_space_size);
156         VERIFY("mmio size", h->mmio_size, info->mmio_size);
157
158         mem = (fw->data + h->cfg_space_offset);
159
160         id = *(u16 *)(mem + PCI_VENDOR_ID);
161         VERIFY("vendor id", id, pdev->vendor);
162
163         id = *(u16 *)(mem + PCI_DEVICE_ID);
164         VERIFY("device id", id, pdev->device);
165
166         id = *(u8 *)(mem + PCI_REVISION_ID);
167         VERIFY("revision id", id, pdev->revision);
168
169 #undef VERIFY
170         return 0;
171
172 invalid_firmware:
173         gvt_dbg_core("Invalid firmware: %s [file] 0x%llx [request] 0x%llx\n",
174                      item, file, request);
175         return -EINVAL;
176 }
177
178 #define GVT_FIRMWARE_PATH "i915/gvt"
179
180 /**
181  * intel_gvt_load_firmware - load GVT firmware
182  * @gvt: intel gvt device
183  *
184  */
185 int intel_gvt_load_firmware(struct intel_gvt *gvt)
186 {
187         struct intel_gvt_device_info *info = &gvt->device_info;
188         struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
189         struct intel_gvt_firmware *firmware = &gvt->firmware;
190         struct gvt_firmware_header *h;
191         const struct firmware *fw;
192         char *path;
193         void *mem;
194         int ret;
195
196         path = kmalloc(PATH_MAX, GFP_KERNEL);
197         if (!path)
198                 return -ENOMEM;
199
200         mem = kmalloc(info->cfg_space_size, GFP_KERNEL);
201         if (!mem) {
202                 kfree(path);
203                 return -ENOMEM;
204         }
205
206         firmware->cfg_space = mem;
207
208         mem = vmalloc(info->mmio_size);
209         if (!mem) {
210                 kfree(path);
211                 kfree(firmware->cfg_space);
212                 return -ENOMEM;
213         }
214
215         firmware->mmio = mem;
216
217         sprintf(path, "%s/vid_0x%04x_did_0x%04x_rid_0x%02x.golden_hw_state",
218                  GVT_FIRMWARE_PATH, pdev->vendor, pdev->device,
219                  pdev->revision);
220
221         gvt_dbg_core("request hw state firmware %s...\n", path);
222
223         ret = request_firmware(&fw, path, gvt->gt->i915->drm.dev);
224         kfree(path);
225
226         if (ret)
227                 goto expose_firmware;
228
229         gvt_dbg_core("success.\n");
230
231         ret = verify_firmware(gvt, fw);
232         if (ret)
233                 goto out_free_fw;
234
235         gvt_dbg_core("verified.\n");
236
237         h = (struct gvt_firmware_header *)fw->data;
238
239         memcpy(firmware->cfg_space, fw->data + h->cfg_space_offset,
240                h->cfg_space_size);
241         memcpy(firmware->mmio, fw->data + h->mmio_offset,
242                h->mmio_size);
243
244         release_firmware(fw);
245         firmware->firmware_loaded = true;
246         return 0;
247
248 out_free_fw:
249         release_firmware(fw);
250 expose_firmware:
251         expose_firmware_sysfs(gvt);
252         return 0;
253 }
This page took 0.040404 seconds and 4 git commands to generate.