blob: 6d1aaff164af39dd961a2e95f1ceb3a7ff9cba30 [file] [log] [blame]
Daniel Vettereb805622015-05-04 14:58:44 +02001/*
2 * Copyright © 2014 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24#include <linux/firmware.h>
25#include "i915_drv.h"
26#include "i915_reg.h"
27
Animesh Mannaaa9145c2015-05-13 22:13:29 +053028/**
29 * DOC: csr support for dmc
30 *
31 * Display Context Save and Restore (CSR) firmware support added from gen9
32 * onwards to drive newly added DMC (Display microcontroller) in display
33 * engine to save and restore the state of display engine when it enter into
34 * low-power state and comes back to normal.
35 *
36 * Firmware loading status will be one of the below states: FW_UNINITIALIZED,
37 * FW_LOADED, FW_FAILED.
38 *
39 * Once the firmware is written into the registers status will be moved from
40 * FW_UNINITIALIZED to FW_LOADED and for any erroneous condition status will
41 * be moved to FW_FAILED.
42 */
43
Rodrigo Vivibf546f82015-06-03 16:50:19 -070044#define I915_CSR_SKL "i915/skl_dmc_ver1.bin"
Animesh Manna18c237c2015-08-04 22:02:41 +053045#define I915_CSR_BXT "i915/bxt_dmc_ver1.bin"
Daniel Vettereb805622015-05-04 14:58:44 +020046
47MODULE_FIRMWARE(I915_CSR_SKL);
Animesh Manna18c237c2015-08-04 22:02:41 +053048MODULE_FIRMWARE(I915_CSR_BXT);
Daniel Vettereb805622015-05-04 14:58:44 +020049
50/*
51* SKL CSR registers for DC5 and DC6
52*/
53#define CSR_PROGRAM_BASE 0x80000
54#define CSR_SSP_BASE_ADDR_GEN9 0x00002FC0
55#define CSR_HTP_ADDR_SKL 0x00500034
56#define CSR_SSP_BASE 0x8F074
57#define CSR_HTP_SKL 0x8F004
58#define CSR_LAST_WRITE 0x8F034
59#define CSR_LAST_WRITE_VALUE 0xc003b400
60/* MMIO address range for CSR program (0x80000 - 0x82FFF) */
61#define CSR_MAX_FW_SIZE 0x2FFF
62#define CSR_DEFAULT_FW_OFFSET 0xFFFFFFFF
63#define CSR_MMIO_START_RANGE 0x80000
64#define CSR_MMIO_END_RANGE 0x8FFFF
65
66struct intel_css_header {
67 /* 0x09 for DMC */
68 uint32_t module_type;
69
70 /* Includes the DMC specific header in dwords */
71 uint32_t header_len;
72
73 /* always value would be 0x10000 */
74 uint32_t header_ver;
75
76 /* Not used */
77 uint32_t module_id;
78
79 /* Not used */
80 uint32_t module_vendor;
81
82 /* in YYYYMMDD format */
83 uint32_t date;
84
85 /* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */
86 uint32_t size;
87
88 /* Not used */
89 uint32_t key_size;
90
91 /* Not used */
92 uint32_t modulus_size;
93
94 /* Not used */
95 uint32_t exponent_size;
96
97 /* Not used */
98 uint32_t reserved1[12];
99
100 /* Major Minor */
101 uint32_t version;
102
103 /* Not used */
104 uint32_t reserved2[8];
105
106 /* Not used */
107 uint32_t kernel_header_info;
108} __packed;
109
110struct intel_fw_info {
111 uint16_t reserved1;
112
113 /* Stepping (A, B, C, ..., *). * is a wildcard */
114 char stepping;
115
116 /* Sub-stepping (0, 1, ..., *). * is a wildcard */
117 char substepping;
118
119 uint32_t offset;
120 uint32_t reserved2;
121} __packed;
122
123struct intel_package_header {
124 /* DMC container header length in dwords */
125 unsigned char header_len;
126
127 /* always value would be 0x01 */
128 unsigned char header_ver;
129
130 unsigned char reserved[10];
131
132 /* Number of valid entries in the FWInfo array below */
133 uint32_t num_entries;
134
135 struct intel_fw_info fw_info[20];
136} __packed;
137
138struct intel_dmc_header {
139 /* always value would be 0x40403E3E */
140 uint32_t signature;
141
142 /* DMC binary header length */
143 unsigned char header_len;
144
145 /* 0x01 */
146 unsigned char header_ver;
147
148 /* Reserved */
149 uint16_t dmcc_ver;
150
151 /* Major, Minor */
152 uint32_t project;
153
154 /* Firmware program size (excluding header) in dwords */
155 uint32_t fw_size;
156
157 /* Major Minor version */
158 uint32_t fw_version;
159
160 /* Number of valid MMIO cycles present. */
161 uint32_t mmio_count;
162
163 /* MMIO address */
164 uint32_t mmioaddr[8];
165
166 /* MMIO data */
167 uint32_t mmiodata[8];
168
169 /* FW filename */
170 unsigned char dfile[32];
171
172 uint32_t reserved1[2];
173} __packed;
174
175struct stepping_info {
176 char stepping;
177 char substepping;
178};
179
180static const struct stepping_info skl_stepping_info[] = {
181 {'A', '0'}, {'B', '0'}, {'C', '0'},
182 {'D', '0'}, {'E', '0'}, {'F', '0'},
183 {'G', '0'}, {'H', '0'}, {'I', '0'}
184};
185
186static char intel_get_stepping(struct drm_device *dev)
187{
188 if (IS_SKYLAKE(dev) && (dev->pdev->revision <
189 ARRAY_SIZE(skl_stepping_info)))
190 return skl_stepping_info[dev->pdev->revision].stepping;
191 else
192 return -ENODATA;
193}
194
195static char intel_get_substepping(struct drm_device *dev)
196{
197 if (IS_SKYLAKE(dev) && (dev->pdev->revision <
198 ARRAY_SIZE(skl_stepping_info)))
199 return skl_stepping_info[dev->pdev->revision].substepping;
200 else
201 return -ENODATA;
202}
203
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530204/**
205 * intel_csr_load_status_get() - to get firmware loading status.
206 * @dev_priv: i915 device.
207 *
208 * This function helps to get the firmware loading status.
209 *
210 * Return: Firmware loading status.
211 */
Suketu Shahdc174302015-04-17 19:46:16 +0530212enum csr_state intel_csr_load_status_get(struct drm_i915_private *dev_priv)
213{
214 enum csr_state state;
215
216 mutex_lock(&dev_priv->csr_lock);
217 state = dev_priv->csr.state;
218 mutex_unlock(&dev_priv->csr_lock);
219
220 return state;
221}
222
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530223/**
224 * intel_csr_load_status_set() - help to set firmware loading status.
225 * @dev_priv: i915 device.
226 * @state: enumeration of firmware loading status.
227 *
228 * Set the firmware loading status.
229 */
Suketu Shahdc174302015-04-17 19:46:16 +0530230void intel_csr_load_status_set(struct drm_i915_private *dev_priv,
231 enum csr_state state)
232{
233 mutex_lock(&dev_priv->csr_lock);
234 dev_priv->csr.state = state;
235 mutex_unlock(&dev_priv->csr_lock);
236}
237
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530238/**
239 * intel_csr_load_program() - write the firmware from memory to register.
240 * @dev: drm device.
241 *
242 * CSR firmware is read from a .bin file and kept in internal memory one time.
243 * Everytime display comes back from low power state this function is called to
244 * copy the firmware from internal memory to registers.
245 */
Daniel Vettereb805622015-05-04 14:58:44 +0200246void intel_csr_load_program(struct drm_device *dev)
247{
248 struct drm_i915_private *dev_priv = dev->dev_private;
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530249 u32 *payload = dev_priv->csr.dmc_payload;
Daniel Vettereb805622015-05-04 14:58:44 +0200250 uint32_t i, fw_size;
251
252 if (!IS_GEN9(dev)) {
253 DRM_ERROR("No CSR support available for this platform\n");
254 return;
255 }
256
257 mutex_lock(&dev_priv->csr_lock);
258 fw_size = dev_priv->csr.dmc_fw_size;
259 for (i = 0; i < fw_size; i++)
260 I915_WRITE(CSR_PROGRAM_BASE + i * 4,
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530261 payload[i]);
Daniel Vettereb805622015-05-04 14:58:44 +0200262
263 for (i = 0; i < dev_priv->csr.mmio_count; i++) {
264 I915_WRITE(dev_priv->csr.mmioaddr[i],
265 dev_priv->csr.mmiodata[i]);
266 }
Suketu Shahdc174302015-04-17 19:46:16 +0530267
268 dev_priv->csr.state = FW_LOADED;
Daniel Vettereb805622015-05-04 14:58:44 +0200269 mutex_unlock(&dev_priv->csr_lock);
270}
271
272static void finish_csr_load(const struct firmware *fw, void *context)
273{
274 struct drm_i915_private *dev_priv = context;
275 struct drm_device *dev = dev_priv->dev;
276 struct intel_css_header *css_header;
277 struct intel_package_header *package_header;
278 struct intel_dmc_header *dmc_header;
279 struct intel_csr *csr = &dev_priv->csr;
280 char stepping = intel_get_stepping(dev);
281 char substepping = intel_get_substepping(dev);
282 uint32_t dmc_offset = CSR_DEFAULT_FW_OFFSET, readcount = 0, nbytes;
283 uint32_t i;
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530284 uint32_t *dmc_payload;
Suketu Shahdc174302015-04-17 19:46:16 +0530285 bool fw_loaded = false;
Daniel Vettereb805622015-05-04 14:58:44 +0200286
287 if (!fw) {
288 i915_firmware_load_error_print(csr->fw_path, 0);
289 goto out;
290 }
291
292 if ((stepping == -ENODATA) || (substepping == -ENODATA)) {
293 DRM_ERROR("Unknown stepping info, firmware loading failed\n");
294 goto out;
295 }
296
297 /* Extract CSS Header information*/
298 css_header = (struct intel_css_header *)fw->data;
299 if (sizeof(struct intel_css_header) !=
300 (css_header->header_len * 4)) {
301 DRM_ERROR("Firmware has wrong CSS header length %u bytes\n",
302 (css_header->header_len * 4));
303 goto out;
304 }
305 readcount += sizeof(struct intel_css_header);
306
307 /* Extract Package Header information*/
308 package_header = (struct intel_package_header *)
309 &fw->data[readcount];
310 if (sizeof(struct intel_package_header) !=
311 (package_header->header_len * 4)) {
312 DRM_ERROR("Firmware has wrong package header length %u bytes\n",
313 (package_header->header_len * 4));
314 goto out;
315 }
316 readcount += sizeof(struct intel_package_header);
317
318 /* Search for dmc_offset to find firware binary. */
319 for (i = 0; i < package_header->num_entries; i++) {
320 if (package_header->fw_info[i].substepping == '*' &&
321 stepping == package_header->fw_info[i].stepping) {
322 dmc_offset = package_header->fw_info[i].offset;
323 break;
324 } else if (stepping == package_header->fw_info[i].stepping &&
325 substepping == package_header->fw_info[i].substepping) {
326 dmc_offset = package_header->fw_info[i].offset;
327 break;
328 } else if (package_header->fw_info[i].stepping == '*' &&
329 package_header->fw_info[i].substepping == '*')
330 dmc_offset = package_header->fw_info[i].offset;
331 }
332 if (dmc_offset == CSR_DEFAULT_FW_OFFSET) {
333 DRM_ERROR("Firmware not supported for %c stepping\n", stepping);
334 goto out;
335 }
336 readcount += dmc_offset;
337
338 /* Extract dmc_header information. */
339 dmc_header = (struct intel_dmc_header *)&fw->data[readcount];
340 if (sizeof(struct intel_dmc_header) != (dmc_header->header_len)) {
341 DRM_ERROR("Firmware has wrong dmc header length %u bytes\n",
342 (dmc_header->header_len));
343 goto out;
344 }
345 readcount += sizeof(struct intel_dmc_header);
346
347 /* Cache the dmc header info. */
348 if (dmc_header->mmio_count > ARRAY_SIZE(csr->mmioaddr)) {
349 DRM_ERROR("Firmware has wrong mmio count %u\n",
350 dmc_header->mmio_count);
351 goto out;
352 }
353 csr->mmio_count = dmc_header->mmio_count;
354 for (i = 0; i < dmc_header->mmio_count; i++) {
355 if (dmc_header->mmioaddr[i] < CSR_MMIO_START_RANGE &&
356 dmc_header->mmioaddr[i] > CSR_MMIO_END_RANGE) {
357 DRM_ERROR(" Firmware has wrong mmio address 0x%x\n",
358 dmc_header->mmioaddr[i]);
359 goto out;
360 }
361 csr->mmioaddr[i] = dmc_header->mmioaddr[i];
362 csr->mmiodata[i] = dmc_header->mmiodata[i];
363 }
364
365 /* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
366 nbytes = dmc_header->fw_size * 4;
367 if (nbytes > CSR_MAX_FW_SIZE) {
368 DRM_ERROR("CSR firmware too big (%u) bytes\n", nbytes);
369 goto out;
370 }
371 csr->dmc_fw_size = dmc_header->fw_size;
372
373 csr->dmc_payload = kmalloc(nbytes, GFP_KERNEL);
374 if (!csr->dmc_payload) {
375 DRM_ERROR("Memory allocation failed for dmc payload\n");
376 goto out;
377 }
378
379 dmc_payload = csr->dmc_payload;
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530380 memcpy(dmc_payload, &fw->data[readcount], nbytes);
Daniel Vettereb805622015-05-04 14:58:44 +0200381
382 /* load csr program during system boot, as needed for DC states */
383 intel_csr_load_program(dev);
Suketu Shahdc174302015-04-17 19:46:16 +0530384 fw_loaded = true;
385
Damien Lespiauabd41dc2015-06-04 16:42:16 +0100386 DRM_DEBUG_KMS("Finished loading %s\n", dev_priv->csr.fw_path);
Daniel Vettereb805622015-05-04 14:58:44 +0200387out:
Suketu Shahdc174302015-04-17 19:46:16 +0530388 if (fw_loaded)
389 intel_runtime_pm_put(dev_priv);
390 else
391 intel_csr_load_status_set(dev_priv, FW_FAILED);
392
Daniel Vettereb805622015-05-04 14:58:44 +0200393 release_firmware(fw);
394}
395
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530396/**
397 * intel_csr_ucode_init() - initialize the firmware loading.
398 * @dev: drm device.
399 *
400 * This function is called at the time of loading the display driver to read
401 * firmware from a .bin file and copied into a internal memory.
402 */
Daniel Vettereb805622015-05-04 14:58:44 +0200403void intel_csr_ucode_init(struct drm_device *dev)
404{
405 struct drm_i915_private *dev_priv = dev->dev_private;
406 struct intel_csr *csr = &dev_priv->csr;
407 int ret;
408
409 if (!HAS_CSR(dev))
410 return;
411
412 if (IS_SKYLAKE(dev))
413 csr->fw_path = I915_CSR_SKL;
Animesh Manna18c237c2015-08-04 22:02:41 +0530414 else if (IS_BROXTON(dev_priv))
415 csr->fw_path = I915_CSR_BXT;
Daniel Vettereb805622015-05-04 14:58:44 +0200416 else {
417 DRM_ERROR("Unexpected: no known CSR firmware for platform\n");
Suketu Shahdc174302015-04-17 19:46:16 +0530418 intel_csr_load_status_set(dev_priv, FW_FAILED);
Daniel Vettereb805622015-05-04 14:58:44 +0200419 return;
420 }
421
Damien Lespiauabd41dc2015-06-04 16:42:16 +0100422 DRM_DEBUG_KMS("Loading %s\n", csr->fw_path);
423
Suketu Shahdc174302015-04-17 19:46:16 +0530424 /*
425 * Obtain a runtime pm reference, until CSR is loaded,
426 * to avoid entering runtime-suspend.
427 */
428 intel_runtime_pm_get(dev_priv);
429
Daniel Vettereb805622015-05-04 14:58:44 +0200430 /* CSR supported for platform, load firmware */
431 ret = request_firmware_nowait(THIS_MODULE, true, csr->fw_path,
432 &dev_priv->dev->pdev->dev,
433 GFP_KERNEL, dev_priv,
434 finish_csr_load);
Suketu Shahdc174302015-04-17 19:46:16 +0530435 if (ret) {
Daniel Vettereb805622015-05-04 14:58:44 +0200436 i915_firmware_load_error_print(csr->fw_path, ret);
Suketu Shahdc174302015-04-17 19:46:16 +0530437 intel_csr_load_status_set(dev_priv, FW_FAILED);
438 }
Daniel Vettereb805622015-05-04 14:58:44 +0200439}
440
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530441/**
442 * intel_csr_ucode_fini() - unload the CSR firmware.
443 * @dev: drm device.
444 *
445 * Firmmware unloading includes freeing the internal momory and reset the
446 * firmware loading status.
447 */
Daniel Vettereb805622015-05-04 14:58:44 +0200448void intel_csr_ucode_fini(struct drm_device *dev)
449{
450 struct drm_i915_private *dev_priv = dev->dev_private;
451
452 if (!HAS_CSR(dev))
453 return;
454
Suketu Shahdc174302015-04-17 19:46:16 +0530455 intel_csr_load_status_set(dev_priv, FW_FAILED);
Daniel Vettereb805622015-05-04 14:58:44 +0200456 kfree(dev_priv->csr.dmc_payload);
457}
Suketu Shah5aefb232015-04-16 14:22:10 +0530458
459void assert_csr_loaded(struct drm_i915_private *dev_priv)
460{
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700461 WARN_ONCE(intel_csr_load_status_get(dev_priv) != FW_LOADED,
462 "CSR is not loaded.\n");
463 WARN_ONCE(!I915_READ(CSR_PROGRAM_BASE),
464 "CSR program storage start is NULL\n");
465 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
466 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530467}