intel_csr.c 14 KB

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