blob: 765dfcd157276d6f5d9fb3506a1ab59ec72afbb8 [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"
Daniel Vettereb805622015-05-04 14:58:44 +020045
46MODULE_FIRMWARE(I915_CSR_SKL);
47
48/*
49* SKL CSR registers for DC5 and DC6
50*/
51#define CSR_PROGRAM_BASE 0x80000
52#define CSR_SSP_BASE_ADDR_GEN9 0x00002FC0
53#define CSR_HTP_ADDR_SKL 0x00500034
54#define CSR_SSP_BASE 0x8F074
55#define CSR_HTP_SKL 0x8F004
56#define CSR_LAST_WRITE 0x8F034
57#define CSR_LAST_WRITE_VALUE 0xc003b400
58/* MMIO address range for CSR program (0x80000 - 0x82FFF) */
59#define CSR_MAX_FW_SIZE 0x2FFF
60#define CSR_DEFAULT_FW_OFFSET 0xFFFFFFFF
61#define CSR_MMIO_START_RANGE 0x80000
62#define CSR_MMIO_END_RANGE 0x8FFFF
63
64struct intel_css_header {
65 /* 0x09 for DMC */
66 uint32_t module_type;
67
68 /* Includes the DMC specific header in dwords */
69 uint32_t header_len;
70
71 /* always value would be 0x10000 */
72 uint32_t header_ver;
73
74 /* Not used */
75 uint32_t module_id;
76
77 /* Not used */
78 uint32_t module_vendor;
79
80 /* in YYYYMMDD format */
81 uint32_t date;
82
83 /* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */
84 uint32_t size;
85
86 /* Not used */
87 uint32_t key_size;
88
89 /* Not used */
90 uint32_t modulus_size;
91
92 /* Not used */
93 uint32_t exponent_size;
94
95 /* Not used */
96 uint32_t reserved1[12];
97
98 /* Major Minor */
99 uint32_t version;
100
101 /* Not used */
102 uint32_t reserved2[8];
103
104 /* Not used */
105 uint32_t kernel_header_info;
106} __packed;
107
108struct intel_fw_info {
109 uint16_t reserved1;
110
111 /* Stepping (A, B, C, ..., *). * is a wildcard */
112 char stepping;
113
114 /* Sub-stepping (0, 1, ..., *). * is a wildcard */
115 char substepping;
116
117 uint32_t offset;
118 uint32_t reserved2;
119} __packed;
120
121struct intel_package_header {
122 /* DMC container header length in dwords */
123 unsigned char header_len;
124
125 /* always value would be 0x01 */
126 unsigned char header_ver;
127
128 unsigned char reserved[10];
129
130 /* Number of valid entries in the FWInfo array below */
131 uint32_t num_entries;
132
133 struct intel_fw_info fw_info[20];
134} __packed;
135
136struct intel_dmc_header {
137 /* always value would be 0x40403E3E */
138 uint32_t signature;
139
140 /* DMC binary header length */
141 unsigned char header_len;
142
143 /* 0x01 */
144 unsigned char header_ver;
145
146 /* Reserved */
147 uint16_t dmcc_ver;
148
149 /* Major, Minor */
150 uint32_t project;
151
152 /* Firmware program size (excluding header) in dwords */
153 uint32_t fw_size;
154
155 /* Major Minor version */
156 uint32_t fw_version;
157
158 /* Number of valid MMIO cycles present. */
159 uint32_t mmio_count;
160
161 /* MMIO address */
162 uint32_t mmioaddr[8];
163
164 /* MMIO data */
165 uint32_t mmiodata[8];
166
167 /* FW filename */
168 unsigned char dfile[32];
169
170 uint32_t reserved1[2];
171} __packed;
172
173struct stepping_info {
174 char stepping;
175 char substepping;
176};
177
178static const struct stepping_info skl_stepping_info[] = {
179 {'A', '0'}, {'B', '0'}, {'C', '0'},
180 {'D', '0'}, {'E', '0'}, {'F', '0'},
181 {'G', '0'}, {'H', '0'}, {'I', '0'}
182};
183
184static char intel_get_stepping(struct drm_device *dev)
185{
186 if (IS_SKYLAKE(dev) && (dev->pdev->revision <
187 ARRAY_SIZE(skl_stepping_info)))
188 return skl_stepping_info[dev->pdev->revision].stepping;
189 else
190 return -ENODATA;
191}
192
193static char intel_get_substepping(struct drm_device *dev)
194{
195 if (IS_SKYLAKE(dev) && (dev->pdev->revision <
196 ARRAY_SIZE(skl_stepping_info)))
197 return skl_stepping_info[dev->pdev->revision].substepping;
198 else
199 return -ENODATA;
200}
201
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530202/**
203 * intel_csr_load_status_get() - to get firmware loading status.
204 * @dev_priv: i915 device.
205 *
206 * This function helps to get the firmware loading status.
207 *
208 * Return: Firmware loading status.
209 */
Suketu Shahdc174302015-04-17 19:46:16 +0530210enum csr_state intel_csr_load_status_get(struct drm_i915_private *dev_priv)
211{
212 enum csr_state state;
213
214 mutex_lock(&dev_priv->csr_lock);
215 state = dev_priv->csr.state;
216 mutex_unlock(&dev_priv->csr_lock);
217
218 return state;
219}
220
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530221/**
222 * intel_csr_load_status_set() - help to set firmware loading status.
223 * @dev_priv: i915 device.
224 * @state: enumeration of firmware loading status.
225 *
226 * Set the firmware loading status.
227 */
Suketu Shahdc174302015-04-17 19:46:16 +0530228void intel_csr_load_status_set(struct drm_i915_private *dev_priv,
229 enum csr_state state)
230{
231 mutex_lock(&dev_priv->csr_lock);
232 dev_priv->csr.state = state;
233 mutex_unlock(&dev_priv->csr_lock);
234}
235
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530236/**
237 * intel_csr_load_program() - write the firmware from memory to register.
238 * @dev: drm device.
239 *
240 * CSR firmware is read from a .bin file and kept in internal memory one time.
241 * Everytime display comes back from low power state this function is called to
242 * copy the firmware from internal memory to registers.
243 */
Daniel Vettereb805622015-05-04 14:58:44 +0200244void intel_csr_load_program(struct drm_device *dev)
245{
246 struct drm_i915_private *dev_priv = dev->dev_private;
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530247 u32 *payload = dev_priv->csr.dmc_payload;
Daniel Vettereb805622015-05-04 14:58:44 +0200248 uint32_t i, fw_size;
249
250 if (!IS_GEN9(dev)) {
251 DRM_ERROR("No CSR support available for this platform\n");
252 return;
253 }
254
255 mutex_lock(&dev_priv->csr_lock);
256 fw_size = dev_priv->csr.dmc_fw_size;
257 for (i = 0; i < fw_size; i++)
258 I915_WRITE(CSR_PROGRAM_BASE + i * 4,
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530259 payload[i]);
Daniel Vettereb805622015-05-04 14:58:44 +0200260
261 for (i = 0; i < dev_priv->csr.mmio_count; i++) {
262 I915_WRITE(dev_priv->csr.mmioaddr[i],
263 dev_priv->csr.mmiodata[i]);
264 }
Suketu Shahdc174302015-04-17 19:46:16 +0530265
266 dev_priv->csr.state = FW_LOADED;
Daniel Vettereb805622015-05-04 14:58:44 +0200267 mutex_unlock(&dev_priv->csr_lock);
268}
269
270static void finish_csr_load(const struct firmware *fw, void *context)
271{
272 struct drm_i915_private *dev_priv = context;
273 struct drm_device *dev = dev_priv->dev;
274 struct intel_css_header *css_header;
275 struct intel_package_header *package_header;
276 struct intel_dmc_header *dmc_header;
277 struct intel_csr *csr = &dev_priv->csr;
278 char stepping = intel_get_stepping(dev);
279 char substepping = intel_get_substepping(dev);
280 uint32_t dmc_offset = CSR_DEFAULT_FW_OFFSET, readcount = 0, nbytes;
281 uint32_t i;
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530282 uint32_t *dmc_payload;
Suketu Shahdc174302015-04-17 19:46:16 +0530283 bool fw_loaded = false;
Daniel Vettereb805622015-05-04 14:58:44 +0200284
285 if (!fw) {
286 i915_firmware_load_error_print(csr->fw_path, 0);
287 goto out;
288 }
289
290 if ((stepping == -ENODATA) || (substepping == -ENODATA)) {
291 DRM_ERROR("Unknown stepping info, firmware loading failed\n");
292 goto out;
293 }
294
295 /* Extract CSS Header information*/
296 css_header = (struct intel_css_header *)fw->data;
297 if (sizeof(struct intel_css_header) !=
298 (css_header->header_len * 4)) {
299 DRM_ERROR("Firmware has wrong CSS header length %u bytes\n",
300 (css_header->header_len * 4));
301 goto out;
302 }
303 readcount += sizeof(struct intel_css_header);
304
305 /* Extract Package Header information*/
306 package_header = (struct intel_package_header *)
307 &fw->data[readcount];
308 if (sizeof(struct intel_package_header) !=
309 (package_header->header_len * 4)) {
310 DRM_ERROR("Firmware has wrong package header length %u bytes\n",
311 (package_header->header_len * 4));
312 goto out;
313 }
314 readcount += sizeof(struct intel_package_header);
315
316 /* Search for dmc_offset to find firware binary. */
317 for (i = 0; i < package_header->num_entries; i++) {
318 if (package_header->fw_info[i].substepping == '*' &&
319 stepping == package_header->fw_info[i].stepping) {
320 dmc_offset = package_header->fw_info[i].offset;
321 break;
322 } else if (stepping == package_header->fw_info[i].stepping &&
323 substepping == package_header->fw_info[i].substepping) {
324 dmc_offset = package_header->fw_info[i].offset;
325 break;
326 } else if (package_header->fw_info[i].stepping == '*' &&
327 package_header->fw_info[i].substepping == '*')
328 dmc_offset = package_header->fw_info[i].offset;
329 }
330 if (dmc_offset == CSR_DEFAULT_FW_OFFSET) {
331 DRM_ERROR("Firmware not supported for %c stepping\n", stepping);
332 goto out;
333 }
334 readcount += dmc_offset;
335
336 /* Extract dmc_header information. */
337 dmc_header = (struct intel_dmc_header *)&fw->data[readcount];
338 if (sizeof(struct intel_dmc_header) != (dmc_header->header_len)) {
339 DRM_ERROR("Firmware has wrong dmc header length %u bytes\n",
340 (dmc_header->header_len));
341 goto out;
342 }
343 readcount += sizeof(struct intel_dmc_header);
344
345 /* Cache the dmc header info. */
346 if (dmc_header->mmio_count > ARRAY_SIZE(csr->mmioaddr)) {
347 DRM_ERROR("Firmware has wrong mmio count %u\n",
348 dmc_header->mmio_count);
349 goto out;
350 }
351 csr->mmio_count = dmc_header->mmio_count;
352 for (i = 0; i < dmc_header->mmio_count; i++) {
353 if (dmc_header->mmioaddr[i] < CSR_MMIO_START_RANGE &&
354 dmc_header->mmioaddr[i] > CSR_MMIO_END_RANGE) {
355 DRM_ERROR(" Firmware has wrong mmio address 0x%x\n",
356 dmc_header->mmioaddr[i]);
357 goto out;
358 }
359 csr->mmioaddr[i] = dmc_header->mmioaddr[i];
360 csr->mmiodata[i] = dmc_header->mmiodata[i];
361 }
362
363 /* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
364 nbytes = dmc_header->fw_size * 4;
365 if (nbytes > CSR_MAX_FW_SIZE) {
366 DRM_ERROR("CSR firmware too big (%u) bytes\n", nbytes);
367 goto out;
368 }
369 csr->dmc_fw_size = dmc_header->fw_size;
370
371 csr->dmc_payload = kmalloc(nbytes, GFP_KERNEL);
372 if (!csr->dmc_payload) {
373 DRM_ERROR("Memory allocation failed for dmc payload\n");
374 goto out;
375 }
376
377 dmc_payload = csr->dmc_payload;
Animesh Mannaa7f749f2015-08-03 21:55:32 +0530378 memcpy(dmc_payload, &fw->data[readcount], nbytes);
Daniel Vettereb805622015-05-04 14:58:44 +0200379
380 /* load csr program during system boot, as needed for DC states */
381 intel_csr_load_program(dev);
Suketu Shahdc174302015-04-17 19:46:16 +0530382 fw_loaded = true;
383
Damien Lespiauabd41dc2015-06-04 16:42:16 +0100384 DRM_DEBUG_KMS("Finished loading %s\n", dev_priv->csr.fw_path);
Daniel Vettereb805622015-05-04 14:58:44 +0200385out:
Suketu Shahdc174302015-04-17 19:46:16 +0530386 if (fw_loaded)
387 intel_runtime_pm_put(dev_priv);
388 else
389 intel_csr_load_status_set(dev_priv, FW_FAILED);
390
Daniel Vettereb805622015-05-04 14:58:44 +0200391 release_firmware(fw);
392}
393
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530394/**
395 * intel_csr_ucode_init() - initialize the firmware loading.
396 * @dev: drm device.
397 *
398 * This function is called at the time of loading the display driver to read
399 * firmware from a .bin file and copied into a internal memory.
400 */
Daniel Vettereb805622015-05-04 14:58:44 +0200401void intel_csr_ucode_init(struct drm_device *dev)
402{
403 struct drm_i915_private *dev_priv = dev->dev_private;
404 struct intel_csr *csr = &dev_priv->csr;
405 int ret;
406
407 if (!HAS_CSR(dev))
408 return;
409
410 if (IS_SKYLAKE(dev))
411 csr->fw_path = I915_CSR_SKL;
412 else {
413 DRM_ERROR("Unexpected: no known CSR firmware for platform\n");
Suketu Shahdc174302015-04-17 19:46:16 +0530414 intel_csr_load_status_set(dev_priv, FW_FAILED);
Daniel Vettereb805622015-05-04 14:58:44 +0200415 return;
416 }
417
Damien Lespiauabd41dc2015-06-04 16:42:16 +0100418 DRM_DEBUG_KMS("Loading %s\n", csr->fw_path);
419
Suketu Shahdc174302015-04-17 19:46:16 +0530420 /*
421 * Obtain a runtime pm reference, until CSR is loaded,
422 * to avoid entering runtime-suspend.
423 */
424 intel_runtime_pm_get(dev_priv);
425
Daniel Vettereb805622015-05-04 14:58:44 +0200426 /* CSR supported for platform, load firmware */
427 ret = request_firmware_nowait(THIS_MODULE, true, csr->fw_path,
428 &dev_priv->dev->pdev->dev,
429 GFP_KERNEL, dev_priv,
430 finish_csr_load);
Suketu Shahdc174302015-04-17 19:46:16 +0530431 if (ret) {
Daniel Vettereb805622015-05-04 14:58:44 +0200432 i915_firmware_load_error_print(csr->fw_path, ret);
Suketu Shahdc174302015-04-17 19:46:16 +0530433 intel_csr_load_status_set(dev_priv, FW_FAILED);
434 }
Daniel Vettereb805622015-05-04 14:58:44 +0200435}
436
Animesh Mannaaa9145c2015-05-13 22:13:29 +0530437/**
438 * intel_csr_ucode_fini() - unload the CSR firmware.
439 * @dev: drm device.
440 *
441 * Firmmware unloading includes freeing the internal momory and reset the
442 * firmware loading status.
443 */
Daniel Vettereb805622015-05-04 14:58:44 +0200444void intel_csr_ucode_fini(struct drm_device *dev)
445{
446 struct drm_i915_private *dev_priv = dev->dev_private;
447
448 if (!HAS_CSR(dev))
449 return;
450
Suketu Shahdc174302015-04-17 19:46:16 +0530451 intel_csr_load_status_set(dev_priv, FW_FAILED);
Daniel Vettereb805622015-05-04 14:58:44 +0200452 kfree(dev_priv->csr.dmc_payload);
453}
Suketu Shah5aefb232015-04-16 14:22:10 +0530454
455void assert_csr_loaded(struct drm_i915_private *dev_priv)
456{
Jesse Barnes6ff8ab02015-09-10 08:20:28 -0700457 WARN_ONCE(intel_csr_load_status_get(dev_priv) != FW_LOADED,
458 "CSR is not loaded.\n");
459 WARN_ONCE(!I915_READ(CSR_PROGRAM_BASE),
460 "CSR program storage start is NULL\n");
461 WARN_ONCE(!I915_READ(CSR_SSP_BASE), "CSR SSP Base Not fine\n");
462 WARN_ONCE(!I915_READ(CSR_HTP_SKL), "CSR HTP Not fine\n");
Suketu Shah5aefb232015-04-16 14:22:10 +0530463}