intel_csr.c 15 KB

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