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