intel_csr.c 13 KB

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