intel_csr.c 12 KB

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