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