intel_csr.c revision 1.4.2.3 1 /* $NetBSD: intel_csr.c,v 1.4.2.3 2018/09/30 01:45:53 pgoyette Exp $ */
2
3 /*
4 * Copyright 2014 Intel Corporation
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
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: intel_csr.c,v 1.4.2.3 2018/09/30 01:45:53 pgoyette Exp $");
28
29 #include <linux/firmware.h>
30 #include <linux/module.h>
31 #include "i915_drv.h"
32 #include "i915_reg.h"
33 #include "intel_drv.h"
34
35 /**
36 * DOC: csr support for dmc
37 *
38 * Display Context Save and Restore (CSR) firmware support added from gen9
39 * onwards to drive newly added DMC (Display microcontroller) in display
40 * engine to save and restore the state of display engine when it enter into
41 * low-power state and comes back to normal.
42 *
43 * Firmware loading status will be one of the below states: FW_UNINITIALIZED,
44 * FW_LOADED, FW_FAILED.
45 *
46 * Once the firmware is written into the registers status will be moved from
47 * FW_UNINITIALIZED to FW_LOADED and for any erroneous condition status will
48 * be moved to FW_FAILED.
49 */
50
51 #define I915_CSR_KBL "i915/kbl_dmc_ver1.bin"
52 #define I915_CSR_SKL "i915/skl_dmc_ver1.bin"
53 #define I915_CSR_BXT "i915/bxt_dmc_ver1.bin"
54
55 MODULE_FIRMWARE(I915_CSR_KBL);
56 MODULE_FIRMWARE(I915_CSR_SKL);
57 MODULE_FIRMWARE(I915_CSR_BXT);
58
59 /*
60 * SKL CSR registers for DC5 and DC6
61 */
62 #define CSR_PROGRAM(i) (0x80000 + (i) * 4)
63 #define CSR_SSP_BASE_ADDR_GEN9 0x00002FC0
64 #define CSR_HTP_ADDR_SKL 0x00500034
65 #define CSR_SSP_BASE 0x8F074
66 #define CSR_HTP_SKL 0x8F004
67 #define CSR_LAST_WRITE 0x8F034
68 #define CSR_LAST_WRITE_VALUE 0xc003b400
69 /* MMIO address range for CSR program (0x80000 - 0x82FFF) */
70 #define CSR_MAX_FW_SIZE 0x2FFF
71 #define CSR_DEFAULT_FW_OFFSET 0xFFFFFFFF
72 #define CSR_MMIO_START_RANGE 0x80000
73 #define CSR_MMIO_END_RANGE 0x8FFFF
74
75 struct intel_css_header {
76 /* 0x09 for DMC */
77 uint32_t module_type;
78
79 /* Includes the DMC specific header in dwords */
80 uint32_t header_len;
81
82 /* always value would be 0x10000 */
83 uint32_t header_ver;
84
85 /* Not used */
86 uint32_t module_id;
87
88 /* Not used */
89 uint32_t module_vendor;
90
91 /* in YYYYMMDD format */
92 uint32_t date;
93
94 /* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */
95 uint32_t size;
96
97 /* Not used */
98 uint32_t key_size;
99
100 /* Not used */
101 uint32_t modulus_size;
102
103 /* Not used */
104 uint32_t exponent_size;
105
106 /* Not used */
107 uint32_t reserved1[12];
108
109 /* Major Minor */
110 uint32_t version;
111
112 /* Not used */
113 uint32_t reserved2[8];
114
115 /* Not used */
116 uint32_t kernel_header_info;
117 } __packed;
118
119 struct intel_fw_info {
120 uint16_t reserved1;
121
122 /* Stepping (A, B, C, ..., *). * is a wildcard */
123 char stepping;
124
125 /* Sub-stepping (0, 1, ..., *). * is a wildcard */
126 char substepping;
127
128 uint32_t offset;
129 uint32_t reserved2;
130 } __packed;
131
132 struct intel_package_header {
133 /* DMC container header length in dwords */
134 unsigned char header_len;
135
136 /* always value would be 0x01 */
137 unsigned char header_ver;
138
139 unsigned char reserved[10];
140
141 /* Number of valid entries in the FWInfo array below */
142 uint32_t num_entries;
143
144 struct intel_fw_info fw_info[20];
145 } __packed;
146
147 struct intel_dmc_header {
148 /* always value would be 0x40403E3E */
149 uint32_t signature;
150
151 /* DMC binary header length */
152 unsigned char header_len;
153
154 /* 0x01 */
155 unsigned char header_ver;
156
157 /* Reserved */
158 uint16_t dmcc_ver;
159
160 /* Major, Minor */
161 uint32_t project;
162
163 /* Firmware program size (excluding header) in dwords */
164 uint32_t fw_size;
165
166 /* Major Minor version */
167 uint32_t fw_version;
168
169 /* Number of valid MMIO cycles present. */
170 uint32_t mmio_count;
171
172 /* MMIO address */
173 uint32_t mmioaddr[8];
174
175 /* MMIO data */
176 uint32_t mmiodata[8];
177
178 /* FW filename */
179 unsigned char dfile[32];
180
181 uint32_t reserved1[2];
182 } __packed;
183
184 struct stepping_info {
185 char stepping;
186 char substepping;
187 };
188
189 /*
190 * Kabylake derivated from Skylake H0, so SKL H0
191 * is the right firmware for KBL A0 (revid 0).
192 */
193 static const struct stepping_info kbl_stepping_info[] = {
194 {'H', '0'}, {'I', '0'}
195 };
196
197 static const struct stepping_info skl_stepping_info[] = {
198 {'A', '0'}, {'B', '0'}, {'C', '0'},
199 {'D', '0'}, {'E', '0'}, {'F', '0'},
200 {'G', '0'}, {'H', '0'}, {'I', '0'},
201 {'J', '0'}, {'K', '0'}
202 };
203
204 static struct stepping_info bxt_stepping_info[] = {
205 {'A', '0'}, {'A', '1'}, {'A', '2'},
206 {'B', '0'}, {'B', '1'}, {'B', '2'}
207 };
208
209 static char intel_get_stepping(struct drm_device *dev)
210 {
211 if (IS_KABYLAKE(dev) && (dev->pdev->revision <
212 ARRAY_SIZE(kbl_stepping_info)))
213 return kbl_stepping_info[dev->pdev->revision].stepping;
214 else if (IS_SKYLAKE(dev) && (dev->pdev->revision <
215 ARRAY_SIZE(skl_stepping_info)))
216 return skl_stepping_info[dev->pdev->revision].stepping;
217 else if (IS_BROXTON(dev) && (dev->pdev->revision <
218 ARRAY_SIZE(bxt_stepping_info)))
219 return bxt_stepping_info[dev->pdev->revision].stepping;
220 else
221 return -ENODATA;
222 }
223
224 static char intel_get_substepping(struct drm_device *dev)
225 {
226 if (IS_KABYLAKE(dev) && (dev->pdev->revision <
227 ARRAY_SIZE(kbl_stepping_info)))
228 return kbl_stepping_info[dev->pdev->revision].substepping;
229 else if (IS_SKYLAKE(dev) && (dev->pdev->revision <
230 ARRAY_SIZE(skl_stepping_info)))
231 return skl_stepping_info[dev->pdev->revision].substepping;
232 else if (IS_BROXTON(dev) && (dev->pdev->revision <
233 ARRAY_SIZE(bxt_stepping_info)))
234 return bxt_stepping_info[dev->pdev->revision].substepping;
235 else
236 return -ENODATA;
237 }
238
239 /**
240 * intel_csr_load_status_get() - to get firmware loading status.
241 * @dev_priv: i915 device.
242 *
243 * This function helps to get the firmware loading status.
244 *
245 * Return: Firmware loading status.
246 */
247 enum csr_state intel_csr_load_status_get(struct drm_i915_private *dev_priv)
248 {
249 enum csr_state state;
250
251 mutex_lock(&dev_priv->csr_lock);
252 state = dev_priv->csr.state;
253 mutex_unlock(&dev_priv->csr_lock);
254
255 return state;
256 }
257
258 /**
259 * intel_csr_load_status_set() - help to set firmware loading status.
260 * @dev_priv: i915 device.
261 * @state: enumeration of firmware loading status.
262 *
263 * Set the firmware loading status.
264 */
265 void intel_csr_load_status_set(struct drm_i915_private *dev_priv,
266 enum csr_state state)
267 {
268 mutex_lock(&dev_priv->csr_lock);
269 dev_priv->csr.state = state;
270 mutex_unlock(&dev_priv->csr_lock);
271 }
272
273 /**
274 * intel_csr_load_program() - write the firmware from memory to register.
275 * @dev: drm device.
276 *
277 * CSR firmware is read from a .bin file and kept in internal memory one time.
278 * Everytime display comes back from low power state this function is called to
279 * copy the firmware from internal memory to registers.
280 */
281 void intel_csr_load_program(struct drm_device *dev)
282 {
283 struct drm_i915_private *dev_priv = dev->dev_private;
284 u32 *payload = dev_priv->csr.dmc_payload;
285 uint32_t i, fw_size;
286
287 if (!IS_GEN9(dev)) {
288 DRM_ERROR("No CSR support available for this platform\n");
289 return;
290 }
291
292 /*
293 * FIXME: Firmware gets lost on S3/S4, but not when entering system
294 * standby or suspend-to-idle (which is just like forced runtime pm).
295 * Unfortunately the ACPI subsystem doesn't yet give us a way to
296 * differentiate this, hence figure it out with this hack.
297 */
298 if (I915_READ(CSR_PROGRAM(0)))
299 return;
300
301 mutex_lock(&dev_priv->csr_lock);
302 fw_size = dev_priv->csr.dmc_fw_size;
303 for (i = 0; i < fw_size; i++)
304 I915_WRITE(CSR_PROGRAM(i), payload[i]);
305
306 for (i = 0; i < dev_priv->csr.mmio_count; i++) {
307 I915_WRITE(dev_priv->csr.mmioaddr[i],
308 dev_priv->csr.mmiodata[i]);
309 }
310
311 dev_priv->csr.state = FW_LOADED;
312 mutex_unlock(&dev_priv->csr_lock);
313 }
314
315 static void finish_csr_load(const struct firmware *fw, void *context)
316 {
317 struct drm_i915_private *dev_priv = context;
318 struct drm_device *dev = dev_priv->dev;
319 struct intel_css_header *css_header;
320 struct intel_package_header *package_header;
321 struct intel_dmc_header *dmc_header;
322 struct intel_csr *csr = &dev_priv->csr;
323 char stepping = intel_get_stepping(dev);
324 char substepping = intel_get_substepping(dev);
325 uint32_t dmc_offset = CSR_DEFAULT_FW_OFFSET, readcount = 0, nbytes;
326 uint32_t i;
327 uint32_t *dmc_payload;
328 bool fw_loaded = false;
329
330 if (!fw) {
331 i915_firmware_load_error_print(csr->fw_path, 0);
332 goto out;
333 }
334
335 if ((stepping == -ENODATA) || (substepping == -ENODATA)) {
336 DRM_ERROR("Unknown stepping info, firmware loading failed\n");
337 goto out;
338 }
339
340 /* Extract CSS Header information*/
341 css_header = (struct intel_css_header *)fw->data;
342 if (sizeof(struct intel_css_header) !=
343 (css_header->header_len * 4)) {
344 DRM_ERROR("Firmware has wrong CSS header length %u bytes\n",
345 (css_header->header_len * 4));
346 goto out;
347 }
348 readcount += sizeof(struct intel_css_header);
349
350 /* Extract Package Header information*/
351 package_header = (struct intel_package_header *)
352 &fw->data[readcount];
353 if (sizeof(struct intel_package_header) !=
354 (package_header->header_len * 4)) {
355 DRM_ERROR("Firmware has wrong package header length %u bytes\n",
356 (package_header->header_len * 4));
357 goto out;
358 }
359 readcount += sizeof(struct intel_package_header);
360
361 /* Search for dmc_offset to find firware binary. */
362 for (i = 0; i < package_header->num_entries; i++) {
363 if (package_header->fw_info[i].substepping == '*' &&
364 stepping == package_header->fw_info[i].stepping) {
365 dmc_offset = package_header->fw_info[i].offset;
366 break;
367 } else if (stepping == package_header->fw_info[i].stepping &&
368 substepping == package_header->fw_info[i].substepping) {
369 dmc_offset = package_header->fw_info[i].offset;
370 break;
371 } else if (package_header->fw_info[i].stepping == '*' &&
372 package_header->fw_info[i].substepping == '*')
373 dmc_offset = package_header->fw_info[i].offset;
374 }
375 if (dmc_offset == CSR_DEFAULT_FW_OFFSET) {
376 DRM_ERROR("Firmware not supported for %c stepping\n", stepping);
377 goto out;
378 }
379 readcount += dmc_offset;
380
381 /* Extract dmc_header information. */
382 dmc_header = (struct intel_dmc_header *)&fw->data[readcount];
383 if (sizeof(struct intel_dmc_header) != (dmc_header->header_len)) {
384 DRM_ERROR("Firmware has wrong dmc header length %u bytes\n",
385 (dmc_header->header_len));
386 goto out;
387 }
388 readcount += sizeof(struct intel_dmc_header);
389
390 /* Cache the dmc header info. */
391 if (dmc_header->mmio_count > ARRAY_SIZE(csr->mmioaddr)) {
392 DRM_ERROR("Firmware has wrong mmio count %u\n",
393 dmc_header->mmio_count);
394 goto out;
395 }
396 csr->mmio_count = dmc_header->mmio_count;
397 for (i = 0; i < dmc_header->mmio_count; i++) {
398 if (dmc_header->mmioaddr[i] < CSR_MMIO_START_RANGE ||
399 dmc_header->mmioaddr[i] > CSR_MMIO_END_RANGE) {
400 DRM_ERROR(" Firmware has wrong mmio address 0x%x\n",
401 dmc_header->mmioaddr[i]);
402 goto out;
403 }
404 csr->mmioaddr[i] = dmc_header->mmioaddr[i];
405 csr->mmiodata[i] = dmc_header->mmiodata[i];
406 }
407
408 /* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
409 nbytes = dmc_header->fw_size * 4;
410 if (nbytes > CSR_MAX_FW_SIZE) {
411 DRM_ERROR("CSR firmware too big (%u) bytes\n", nbytes);
412 goto out;
413 }
414 csr->dmc_fw_size = dmc_header->fw_size;
415
416 csr->dmc_payload = kmalloc(nbytes, GFP_KERNEL);
417 if (!csr->dmc_payload) {
418 DRM_ERROR("Memory allocation failed for dmc payload\n");
419 goto out;
420 }
421
422 dmc_payload = csr->dmc_payload;
423 memcpy(dmc_payload, &fw->data[readcount], nbytes);
424
425 /* load csr program during system boot, as needed for DC states */
426 intel_csr_load_program(dev);
427 fw_loaded = true;
428
429 DRM_DEBUG_KMS("Finished loading %s\n", dev_priv->csr.fw_path);
430 out:
431 if (fw_loaded)
432 intel_runtime_pm_put(dev_priv);
433 else
434 intel_csr_load_status_set(dev_priv, FW_FAILED);
435
436 release_firmware(fw);
437 }
438
439 /**
440 * intel_csr_ucode_init() - initialize the firmware loading.
441 * @dev: drm device.
442 *
443 * This function is called at the time of loading the display driver to read
444 * firmware from a .bin file and copied into a internal memory.
445 */
446 void intel_csr_ucode_init(struct drm_device *dev)
447 {
448 struct drm_i915_private *dev_priv = dev->dev_private;
449 struct intel_csr *csr = &dev_priv->csr;
450 int ret;
451
452 if (!HAS_CSR(dev))
453 return;
454
455 if (IS_KABYLAKE(dev))
456 csr->fw_path = I915_CSR_KBL;
457 else if (IS_SKYLAKE(dev))
458 csr->fw_path = I915_CSR_SKL;
459 else if (IS_BROXTON(dev_priv))
460 csr->fw_path = I915_CSR_BXT;
461 else {
462 DRM_ERROR("Unexpected: no known CSR firmware for platform\n");
463 intel_csr_load_status_set(dev_priv, FW_FAILED);
464 return;
465 }
466
467 DRM_DEBUG_KMS("Loading %s\n", csr->fw_path);
468
469 /*
470 * Obtain a runtime pm reference, until CSR is loaded,
471 * to avoid entering runtime-suspend.
472 */
473 intel_runtime_pm_get(dev_priv);
474
475 /* CSR supported for platform, load firmware */
476 ret = request_firmware_nowait(THIS_MODULE, true, csr->fw_path,
477 dev_priv->dev->dev,
478 GFP_KERNEL, dev_priv,
479 finish_csr_load);
480 if (ret) {
481 i915_firmware_load_error_print(csr->fw_path, ret);
482 intel_csr_load_status_set(dev_priv, FW_FAILED);
483 }
484 }
485
486 /**
487 * intel_csr_ucode_fini() - unload the CSR firmware.
488 * @dev: drm device.
489 *
490 * Firmmware unloading includes freeing the internal momory and reset the
491 * firmware loading status.
492 */
493 void intel_csr_ucode_fini(struct drm_device *dev)
494 {
495 struct drm_i915_private *dev_priv = dev->dev_private;
496
497 if (!HAS_CSR(dev))
498 return;
499
500 intel_csr_load_status_set(dev_priv, FW_FAILED);
501 kfree(dev_priv->csr.dmc_payload);
502 }
503
504 void assert_csr_loaded(struct drm_i915_private *dev_priv)
505 {
506 WARN_ONCE(intel_csr_load_status_get(dev_priv) != FW_LOADED,
507 "CSR is not loaded.\n");
508 WARN_ONCE(!I915_READ(CSR_PROGRAM(0)),
509 "CSR program storage start is NULL\n");
510 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
511 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
512 }
513