intel_csr.c 15 KB

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