intel_csr.c 12 KB

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