intel_csr.c 13 KB

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