2 * Copyright © 2014 Intel Corporation
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:
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
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
24 #include <linux/firmware.h>
28 #define I915_CSR_SKL "i915/skl_dmc_ver4.bin"
30 MODULE_FIRMWARE(I915_CSR_SKL
);
33 * SKL CSR registers for DC5 and DC6
35 #define CSR_PROGRAM_BASE 0x80000
36 #define CSR_SSP_BASE_ADDR_GEN9 0x00002FC0
37 #define CSR_HTP_ADDR_SKL 0x00500034
38 #define CSR_SSP_BASE 0x8F074
39 #define CSR_HTP_SKL 0x8F004
40 #define CSR_LAST_WRITE 0x8F034
41 #define CSR_LAST_WRITE_VALUE 0xc003b400
42 /* MMIO address range for CSR program (0x80000 - 0x82FFF) */
43 #define CSR_MAX_FW_SIZE 0x2FFF
44 #define CSR_DEFAULT_FW_OFFSET 0xFFFFFFFF
45 #define CSR_MMIO_START_RANGE 0x80000
46 #define CSR_MMIO_END_RANGE 0x8FFFF
48 struct intel_css_header
{
52 /* Includes the DMC specific header in dwords */
55 /* always value would be 0x10000 */
62 uint32_t module_vendor
;
64 /* in YYYYMMDD format */
67 /* Size in dwords (CSS_Headerlen + PackageHeaderLen + dmc FWsLen)/4 */
74 uint32_t modulus_size
;
77 uint32_t exponent_size
;
80 uint32_t reserved1
[12];
86 uint32_t reserved2
[8];
89 uint32_t kernel_header_info
;
92 struct intel_fw_info
{
95 /* Stepping (A, B, C, ..., *). * is a wildcard */
98 /* Sub-stepping (0, 1, ..., *). * is a wildcard */
105 struct intel_package_header
{
106 /* DMC container header length in dwords */
107 unsigned char header_len
;
109 /* always value would be 0x01 */
110 unsigned char header_ver
;
112 unsigned char reserved
[10];
114 /* Number of valid entries in the FWInfo array below */
115 uint32_t num_entries
;
117 struct intel_fw_info fw_info
[20];
120 struct intel_dmc_header
{
121 /* always value would be 0x40403E3E */
124 /* DMC binary header length */
125 unsigned char header_len
;
128 unsigned char header_ver
;
136 /* Firmware program size (excluding header) in dwords */
139 /* Major Minor version */
142 /* Number of valid MMIO cycles present. */
146 uint32_t mmioaddr
[8];
149 uint32_t mmiodata
[8];
152 unsigned char dfile
[32];
154 uint32_t reserved1
[2];
157 struct stepping_info
{
162 static const struct stepping_info skl_stepping_info
[] = {
163 {'A', '0'}, {'B', '0'}, {'C', '0'},
164 {'D', '0'}, {'E', '0'}, {'F', '0'},
165 {'G', '0'}, {'H', '0'}, {'I', '0'}
168 static char intel_get_stepping(struct drm_device
*dev
)
170 if (IS_SKYLAKE(dev
) && (dev
->pdev
->revision
<
171 ARRAY_SIZE(skl_stepping_info
)))
172 return skl_stepping_info
[dev
->pdev
->revision
].stepping
;
177 static char intel_get_substepping(struct drm_device
*dev
)
179 if (IS_SKYLAKE(dev
) && (dev
->pdev
->revision
<
180 ARRAY_SIZE(skl_stepping_info
)))
181 return skl_stepping_info
[dev
->pdev
->revision
].substepping
;
186 enum csr_state
intel_csr_load_status_get(struct drm_i915_private
*dev_priv
)
188 enum csr_state state
;
190 mutex_lock(&dev_priv
->csr_lock
);
191 state
= dev_priv
->csr
.state
;
192 mutex_unlock(&dev_priv
->csr_lock
);
197 void intel_csr_load_status_set(struct drm_i915_private
*dev_priv
,
198 enum csr_state state
)
200 mutex_lock(&dev_priv
->csr_lock
);
201 dev_priv
->csr
.state
= state
;
202 mutex_unlock(&dev_priv
->csr_lock
);
205 void intel_csr_load_program(struct drm_device
*dev
)
207 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
208 __be32
*payload
= dev_priv
->csr
.dmc_payload
;
212 DRM_ERROR("No CSR support available for this platform\n");
216 mutex_lock(&dev_priv
->csr_lock
);
217 fw_size
= dev_priv
->csr
.dmc_fw_size
;
218 for (i
= 0; i
< fw_size
; i
++)
219 I915_WRITE(CSR_PROGRAM_BASE
+ i
* 4,
220 (u32 __force
)payload
[i
]);
222 for (i
= 0; i
< dev_priv
->csr
.mmio_count
; i
++) {
223 I915_WRITE(dev_priv
->csr
.mmioaddr
[i
],
224 dev_priv
->csr
.mmiodata
[i
]);
227 dev_priv
->csr
.state
= FW_LOADED
;
228 mutex_unlock(&dev_priv
->csr_lock
);
231 static void finish_csr_load(const struct firmware
*fw
, void *context
)
233 struct drm_i915_private
*dev_priv
= context
;
234 struct drm_device
*dev
= dev_priv
->dev
;
235 struct intel_css_header
*css_header
;
236 struct intel_package_header
*package_header
;
237 struct intel_dmc_header
*dmc_header
;
238 struct intel_csr
*csr
= &dev_priv
->csr
;
239 char stepping
= intel_get_stepping(dev
);
240 char substepping
= intel_get_substepping(dev
);
241 uint32_t dmc_offset
= CSR_DEFAULT_FW_OFFSET
, readcount
= 0, nbytes
;
244 bool fw_loaded
= false;
247 i915_firmware_load_error_print(csr
->fw_path
, 0);
251 if ((stepping
== -ENODATA
) || (substepping
== -ENODATA
)) {
252 DRM_ERROR("Unknown stepping info, firmware loading failed\n");
256 /* Extract CSS Header information*/
257 css_header
= (struct intel_css_header
*)fw
->data
;
258 if (sizeof(struct intel_css_header
) !=
259 (css_header
->header_len
* 4)) {
260 DRM_ERROR("Firmware has wrong CSS header length %u bytes\n",
261 (css_header
->header_len
* 4));
264 readcount
+= sizeof(struct intel_css_header
);
266 /* Extract Package Header information*/
267 package_header
= (struct intel_package_header
*)
268 &fw
->data
[readcount
];
269 if (sizeof(struct intel_package_header
) !=
270 (package_header
->header_len
* 4)) {
271 DRM_ERROR("Firmware has wrong package header length %u bytes\n",
272 (package_header
->header_len
* 4));
275 readcount
+= sizeof(struct intel_package_header
);
277 /* Search for dmc_offset to find firware binary. */
278 for (i
= 0; i
< package_header
->num_entries
; i
++) {
279 if (package_header
->fw_info
[i
].substepping
== '*' &&
280 stepping
== package_header
->fw_info
[i
].stepping
) {
281 dmc_offset
= package_header
->fw_info
[i
].offset
;
283 } else if (stepping
== package_header
->fw_info
[i
].stepping
&&
284 substepping
== package_header
->fw_info
[i
].substepping
) {
285 dmc_offset
= package_header
->fw_info
[i
].offset
;
287 } else if (package_header
->fw_info
[i
].stepping
== '*' &&
288 package_header
->fw_info
[i
].substepping
== '*')
289 dmc_offset
= package_header
->fw_info
[i
].offset
;
291 if (dmc_offset
== CSR_DEFAULT_FW_OFFSET
) {
292 DRM_ERROR("Firmware not supported for %c stepping\n", stepping
);
295 readcount
+= dmc_offset
;
297 /* Extract dmc_header information. */
298 dmc_header
= (struct intel_dmc_header
*)&fw
->data
[readcount
];
299 if (sizeof(struct intel_dmc_header
) != (dmc_header
->header_len
)) {
300 DRM_ERROR("Firmware has wrong dmc header length %u bytes\n",
301 (dmc_header
->header_len
));
304 readcount
+= sizeof(struct intel_dmc_header
);
306 /* Cache the dmc header info. */
307 if (dmc_header
->mmio_count
> ARRAY_SIZE(csr
->mmioaddr
)) {
308 DRM_ERROR("Firmware has wrong mmio count %u\n",
309 dmc_header
->mmio_count
);
312 csr
->mmio_count
= dmc_header
->mmio_count
;
313 for (i
= 0; i
< dmc_header
->mmio_count
; i
++) {
314 if (dmc_header
->mmioaddr
[i
] < CSR_MMIO_START_RANGE
&&
315 dmc_header
->mmioaddr
[i
] > CSR_MMIO_END_RANGE
) {
316 DRM_ERROR(" Firmware has wrong mmio address 0x%x\n",
317 dmc_header
->mmioaddr
[i
]);
320 csr
->mmioaddr
[i
] = dmc_header
->mmioaddr
[i
];
321 csr
->mmiodata
[i
] = dmc_header
->mmiodata
[i
];
324 /* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
325 nbytes
= dmc_header
->fw_size
* 4;
326 if (nbytes
> CSR_MAX_FW_SIZE
) {
327 DRM_ERROR("CSR firmware too big (%u) bytes\n", nbytes
);
330 csr
->dmc_fw_size
= dmc_header
->fw_size
;
332 csr
->dmc_payload
= kmalloc(nbytes
, GFP_KERNEL
);
333 if (!csr
->dmc_payload
) {
334 DRM_ERROR("Memory allocation failed for dmc payload\n");
338 dmc_payload
= csr
->dmc_payload
;
339 for (i
= 0; i
< dmc_header
->fw_size
; i
++) {
340 uint32_t *tmp
= (u32
*)&fw
->data
[readcount
+ i
* 4];
342 * The firmware payload is an array of 32 bit words stored in
343 * little-endian format in the firmware image and programmed
344 * as 32 bit big-endian format to memory.
346 dmc_payload
[i
] = cpu_to_be32(*tmp
);
349 /* load csr program during system boot, as needed for DC states */
350 intel_csr_load_program(dev
);
355 intel_runtime_pm_put(dev_priv
);
357 intel_csr_load_status_set(dev_priv
, FW_FAILED
);
359 release_firmware(fw
);
362 void intel_csr_ucode_init(struct drm_device
*dev
)
364 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
365 struct intel_csr
*csr
= &dev_priv
->csr
;
372 csr
->fw_path
= I915_CSR_SKL
;
374 DRM_ERROR("Unexpected: no known CSR firmware for platform\n");
375 intel_csr_load_status_set(dev_priv
, FW_FAILED
);
380 * Obtain a runtime pm reference, until CSR is loaded,
381 * to avoid entering runtime-suspend.
383 intel_runtime_pm_get(dev_priv
);
385 /* CSR supported for platform, load firmware */
386 ret
= request_firmware_nowait(THIS_MODULE
, true, csr
->fw_path
,
387 &dev_priv
->dev
->pdev
->dev
,
388 GFP_KERNEL
, dev_priv
,
391 i915_firmware_load_error_print(csr
->fw_path
, ret
);
392 intel_csr_load_status_set(dev_priv
, FW_FAILED
);
396 void intel_csr_ucode_fini(struct drm_device
*dev
)
398 struct drm_i915_private
*dev_priv
= dev
->dev_private
;
403 intel_csr_load_status_set(dev_priv
, FW_FAILED
);
404 kfree(dev_priv
->csr
.dmc_payload
);
407 void assert_csr_loaded(struct drm_i915_private
*dev_priv
)
409 WARN((intel_csr_load_status_get(dev_priv
) != FW_LOADED
), "CSR is not loaded.\n");
410 WARN(!I915_READ(CSR_PROGRAM_BASE
),
411 "CSR program storage start is NULL\n");
412 WARN(!I915_READ(CSR_SSP_BASE
), "CSR SSP Base Not fine\n");
413 WARN(!I915_READ(CSR_HTP_SKL
), "CSR HTP Not fine\n");