mpfs-auto-update.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip Polarfire SoC "Auto Update" FPGA reprogramming.
  4. *
  5. * Documentation of this functionality is available in the "PolarFire® FPGA and
  6. * PolarFire SoC FPGA Programming" User Guide.
  7. *
  8. * Copyright (c) 2022-2023 Microchip Corporation. All rights reserved.
  9. *
  10. * Author: Conor Dooley <conor.dooley@microchip.com>
  11. */
  12. #include <linux/cleanup.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/firmware.h>
  15. #include <linux/math.h>
  16. #include <linux/module.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/sizes.h>
  20. #include <soc/microchip/mpfs.h>
  21. #define AUTO_UPDATE_DEFAULT_MBOX_OFFSET 0u
  22. #define AUTO_UPDATE_DEFAULT_RESP_OFFSET 0u
  23. #define AUTO_UPDATE_FEATURE_CMD_OPCODE 0x05u
  24. #define AUTO_UPDATE_FEATURE_CMD_DATA_SIZE 0u
  25. #define AUTO_UPDATE_FEATURE_RESP_SIZE 33u
  26. #define AUTO_UPDATE_FEATURE_CMD_DATA NULL
  27. #define AUTO_UPDATE_FEATURE_ENABLED BIT(5)
  28. #define AUTO_UPDATE_AUTHENTICATE_CMD_OPCODE 0x22u
  29. #define AUTO_UPDATE_AUTHENTICATE_CMD_DATA_SIZE 0u
  30. #define AUTO_UPDATE_AUTHENTICATE_RESP_SIZE 1u
  31. #define AUTO_UPDATE_AUTHENTICATE_CMD_DATA NULL
  32. #define AUTO_UPDATE_PROGRAM_CMD_OPCODE 0x46u
  33. #define AUTO_UPDATE_PROGRAM_CMD_DATA_SIZE 0u
  34. #define AUTO_UPDATE_PROGRAM_RESP_SIZE 1u
  35. #define AUTO_UPDATE_PROGRAM_CMD_DATA NULL
  36. /*
  37. * SPI Flash layout example:
  38. * |------------------------------| 0x0000000
  39. * | 1 KiB |
  40. * | SPI "directories" |
  41. * |------------------------------| 0x0000400
  42. * | 1 MiB |
  43. * | Reserved area |
  44. * | Used for bitstream info |
  45. * |------------------------------| 0x0100400
  46. * | 20 MiB |
  47. * | Golden Image |
  48. * |------------------------------| 0x1500400
  49. * | 20 MiB |
  50. * | Auto Upgrade Image |
  51. * |------------------------------| 0x2900400
  52. * | 20 MiB |
  53. * | Reserved for multi-image IAP |
  54. * | Unused for Auto Upgrade |
  55. * |------------------------------| 0x3D00400
  56. * | ? B |
  57. * | Unused |
  58. * |------------------------------| 0x?
  59. */
  60. #define AUTO_UPDATE_DIRECTORY_BASE 0u
  61. #define AUTO_UPDATE_DIRECTORY_WIDTH 4u
  62. #define AUTO_UPDATE_GOLDEN_INDEX 0u
  63. #define AUTO_UPDATE_UPGRADE_INDEX 1u
  64. #define AUTO_UPDATE_BLANK_INDEX 2u
  65. #define AUTO_UPDATE_GOLDEN_DIRECTORY (AUTO_UPDATE_DIRECTORY_WIDTH * AUTO_UPDATE_GOLDEN_INDEX)
  66. #define AUTO_UPDATE_UPGRADE_DIRECTORY (AUTO_UPDATE_DIRECTORY_WIDTH * AUTO_UPDATE_UPGRADE_INDEX)
  67. #define AUTO_UPDATE_BLANK_DIRECTORY (AUTO_UPDATE_DIRECTORY_WIDTH * AUTO_UPDATE_BLANK_INDEX)
  68. #define AUTO_UPDATE_DIRECTORY_SIZE SZ_1K
  69. #define AUTO_UPDATE_INFO_BASE AUTO_UPDATE_DIRECTORY_SIZE
  70. #define AUTO_UPDATE_INFO_SIZE SZ_1M
  71. #define AUTO_UPDATE_BITSTREAM_BASE (AUTO_UPDATE_DIRECTORY_SIZE + AUTO_UPDATE_INFO_SIZE)
  72. struct mpfs_auto_update_priv {
  73. struct mpfs_sys_controller *sys_controller;
  74. struct device *dev;
  75. struct mtd_info *flash;
  76. struct fw_upload *fw_uploader;
  77. size_t size_per_bitstream;
  78. bool cancel_request;
  79. };
  80. static bool mpfs_auto_update_is_bitstream_info(const u8 *data, u32 size)
  81. {
  82. if (size < 4)
  83. return false;
  84. if (data[0] == 0x4d && data[1] == 0x43 && data[2] == 0x48 && data[3] == 0x50)
  85. return true;
  86. return false;
  87. }
  88. static enum fw_upload_err mpfs_auto_update_prepare(struct fw_upload *fw_uploader, const u8 *data,
  89. u32 size)
  90. {
  91. struct mpfs_auto_update_priv *priv = fw_uploader->dd_handle;
  92. size_t erase_size = AUTO_UPDATE_DIRECTORY_SIZE;
  93. /*
  94. * Verifying the Golden Image is idealistic. It will be evaluated
  95. * against the currently programmed image and thus may fail - due to
  96. * either rollback protection (if its an older version than that in use)
  97. * or if the version is the same as that of the in-use image.
  98. * Extracting the information as to why a failure occurred is not
  99. * currently possible due to limitations of the system controller
  100. * driver. If those are fixed, verification of the Golden Image should
  101. * be added here.
  102. */
  103. priv->flash = mpfs_sys_controller_get_flash(priv->sys_controller);
  104. if (!priv->flash)
  105. return FW_UPLOAD_ERR_HW_ERROR;
  106. erase_size = round_up(erase_size, (u64)priv->flash->erasesize);
  107. /*
  108. * We need to calculate if we have enough space in the flash for the
  109. * new image.
  110. * First, chop off the first 1 KiB as it's reserved for the directory.
  111. * The 1 MiB reserved for design info needs to be ignored also.
  112. * All that remains is carved into 3 & rounded down to the erasesize.
  113. * If this is smaller than the image size, we abort.
  114. * There's also no need to consume more than 20 MiB per image.
  115. */
  116. priv->size_per_bitstream = priv->flash->size - SZ_1K - SZ_1M;
  117. priv->size_per_bitstream = round_down(priv->size_per_bitstream / 3, erase_size);
  118. if (priv->size_per_bitstream > 20 * SZ_1M)
  119. priv->size_per_bitstream = 20 * SZ_1M;
  120. if (priv->size_per_bitstream < size) {
  121. dev_err(priv->dev,
  122. "flash device has insufficient capacity to store this bitstream\n");
  123. return FW_UPLOAD_ERR_INVALID_SIZE;
  124. }
  125. priv->cancel_request = false;
  126. return FW_UPLOAD_ERR_NONE;
  127. }
  128. static void mpfs_auto_update_cancel(struct fw_upload *fw_uploader)
  129. {
  130. struct mpfs_auto_update_priv *priv = fw_uploader->dd_handle;
  131. priv->cancel_request = true;
  132. }
  133. static enum fw_upload_err mpfs_auto_update_poll_complete(struct fw_upload *fw_uploader)
  134. {
  135. return FW_UPLOAD_ERR_NONE;
  136. }
  137. static int mpfs_auto_update_verify_image(struct fw_upload *fw_uploader)
  138. {
  139. struct mpfs_auto_update_priv *priv = fw_uploader->dd_handle;
  140. u32 *response_msg __free(kfree) =
  141. kzalloc(AUTO_UPDATE_FEATURE_RESP_SIZE * sizeof(*response_msg), GFP_KERNEL);
  142. struct mpfs_mss_response *response __free(kfree) =
  143. kzalloc(sizeof(struct mpfs_mss_response), GFP_KERNEL);
  144. struct mpfs_mss_msg *message __free(kfree) =
  145. kzalloc(sizeof(struct mpfs_mss_msg), GFP_KERNEL);
  146. int ret;
  147. if (!response_msg || !response || !message)
  148. return -ENOMEM;
  149. /*
  150. * The system controller can verify that an image in the flash is valid.
  151. * Rather than duplicate the check in this driver, call the relevant
  152. * service from the system controller instead.
  153. * This service has no command data and no response data. It overloads
  154. * mbox_offset with the image index in the flash's SPI directory where
  155. * the bitstream is located.
  156. */
  157. response->resp_msg = response_msg;
  158. response->resp_size = AUTO_UPDATE_AUTHENTICATE_RESP_SIZE;
  159. message->cmd_opcode = AUTO_UPDATE_AUTHENTICATE_CMD_OPCODE;
  160. message->cmd_data_size = AUTO_UPDATE_AUTHENTICATE_CMD_DATA_SIZE;
  161. message->response = response;
  162. message->cmd_data = AUTO_UPDATE_AUTHENTICATE_CMD_DATA;
  163. message->mbox_offset = AUTO_UPDATE_UPGRADE_INDEX;
  164. message->resp_offset = AUTO_UPDATE_DEFAULT_RESP_OFFSET;
  165. dev_info(priv->dev, "Running verification of Upgrade Image\n");
  166. ret = mpfs_blocking_transaction(priv->sys_controller, message);
  167. if (ret | response->resp_status) {
  168. dev_warn(priv->dev, "Verification of Upgrade Image failed!\n");
  169. return ret ? ret : -EBADMSG;
  170. }
  171. dev_info(priv->dev, "Verification of Upgrade Image passed!\n");
  172. return 0;
  173. }
  174. static int mpfs_auto_update_set_image_address(struct mpfs_auto_update_priv *priv,
  175. u32 image_address, loff_t directory_address)
  176. {
  177. struct erase_info erase;
  178. size_t erase_size = round_up(AUTO_UPDATE_DIRECTORY_SIZE, (u64)priv->flash->erasesize);
  179. size_t bytes_written = 0, bytes_read = 0;
  180. char *buffer __free(kfree) = kzalloc(erase_size, GFP_KERNEL);
  181. int ret;
  182. if (!buffer)
  183. return -ENOMEM;
  184. erase.addr = AUTO_UPDATE_DIRECTORY_BASE;
  185. erase.len = erase_size;
  186. /*
  187. * We need to write the "SPI DIRECTORY" to the first 1 KiB, telling
  188. * the system controller where to find the actual bitstream. Since
  189. * this is spi-nor, we have to read the first eraseblock, erase that
  190. * portion of the flash, modify the data and then write it back.
  191. * There's no need to do this though if things are already the way they
  192. * should be, so check and save the write in that case.
  193. */
  194. ret = mtd_read(priv->flash, AUTO_UPDATE_DIRECTORY_BASE, erase_size, &bytes_read,
  195. (u_char *)buffer);
  196. if (ret)
  197. return ret;
  198. if (bytes_read != erase_size)
  199. return -EIO;
  200. if ((*(u32 *)(buffer + AUTO_UPDATE_UPGRADE_DIRECTORY) == image_address) &&
  201. !(*(u32 *)(buffer + AUTO_UPDATE_BLANK_DIRECTORY)))
  202. return 0;
  203. ret = mtd_erase(priv->flash, &erase);
  204. if (ret)
  205. return ret;
  206. /*
  207. * Populate the image address and then zero out the next directory so
  208. * that the system controller doesn't complain if in "Single Image"
  209. * mode.
  210. */
  211. memcpy(buffer + AUTO_UPDATE_UPGRADE_DIRECTORY, &image_address,
  212. AUTO_UPDATE_DIRECTORY_WIDTH);
  213. memset(buffer + AUTO_UPDATE_BLANK_DIRECTORY, 0x0, AUTO_UPDATE_DIRECTORY_WIDTH);
  214. dev_info(priv->dev, "Writing the image address (0x%x) to the flash directory (0x%llx)\n",
  215. image_address, directory_address);
  216. ret = mtd_write(priv->flash, 0x0, erase_size, &bytes_written, (u_char *)buffer);
  217. if (ret)
  218. return ret;
  219. if (bytes_written != erase_size)
  220. return -EIO;
  221. return 0;
  222. }
  223. static int mpfs_auto_update_write_bitstream(struct fw_upload *fw_uploader, const u8 *data,
  224. u32 offset, u32 size, u32 *written)
  225. {
  226. struct mpfs_auto_update_priv *priv = fw_uploader->dd_handle;
  227. struct erase_info erase;
  228. loff_t directory_address = AUTO_UPDATE_UPGRADE_DIRECTORY;
  229. size_t erase_size = AUTO_UPDATE_DIRECTORY_SIZE;
  230. size_t bytes_written = 0;
  231. bool is_info = mpfs_auto_update_is_bitstream_info(data, size);
  232. u32 image_address;
  233. int ret;
  234. erase_size = round_up(erase_size, (u64)priv->flash->erasesize);
  235. if (is_info)
  236. image_address = AUTO_UPDATE_INFO_BASE;
  237. else
  238. image_address = AUTO_UPDATE_BITSTREAM_BASE +
  239. AUTO_UPDATE_UPGRADE_INDEX * priv->size_per_bitstream;
  240. /*
  241. * For bitstream info, the descriptor is written to a fixed offset,
  242. * so there is no need to set the image address.
  243. */
  244. if (!is_info) {
  245. ret = mpfs_auto_update_set_image_address(priv, image_address, directory_address);
  246. if (ret) {
  247. dev_err(priv->dev, "failed to set image address in the SPI directory: %d\n", ret);
  248. return ret;
  249. }
  250. } else {
  251. if (size > AUTO_UPDATE_INFO_SIZE) {
  252. dev_err(priv->dev, "bitstream info exceeds permitted size\n");
  253. return -ENOSPC;
  254. }
  255. }
  256. /*
  257. * Now the .spi image itself can be written to the flash. Preservation
  258. * of contents here is not important here, unlike the spi "directory"
  259. * which must be RMWed.
  260. */
  261. erase.len = round_up(size, (size_t)priv->flash->erasesize);
  262. erase.addr = image_address;
  263. dev_info(priv->dev, "Erasing the flash at address (0x%x)\n", image_address);
  264. ret = mtd_erase(priv->flash, &erase);
  265. if (ret)
  266. return ret;
  267. /*
  268. * No parsing etc of the bitstream is required. The system controller
  269. * will do all of that itself - including verifying that the bitstream
  270. * is valid.
  271. */
  272. dev_info(priv->dev, "Writing the image to the flash at address (0x%x)\n", image_address);
  273. ret = mtd_write(priv->flash, (loff_t)image_address, size, &bytes_written, data);
  274. if (ret)
  275. return ret;
  276. if (bytes_written != size)
  277. return -EIO;
  278. *written = bytes_written;
  279. dev_info(priv->dev, "Wrote 0x%zx bytes to the flash\n", bytes_written);
  280. return 0;
  281. }
  282. static enum fw_upload_err mpfs_auto_update_write(struct fw_upload *fw_uploader, const u8 *data,
  283. u32 offset, u32 size, u32 *written)
  284. {
  285. struct mpfs_auto_update_priv *priv = fw_uploader->dd_handle;
  286. int ret;
  287. ret = mpfs_auto_update_write_bitstream(fw_uploader, data, offset, size, written);
  288. if (ret)
  289. return FW_UPLOAD_ERR_RW_ERROR;
  290. if (priv->cancel_request)
  291. return FW_UPLOAD_ERR_CANCELED;
  292. if (mpfs_auto_update_is_bitstream_info(data, size))
  293. return FW_UPLOAD_ERR_NONE;
  294. ret = mpfs_auto_update_verify_image(fw_uploader);
  295. if (ret)
  296. return FW_UPLOAD_ERR_FW_INVALID;
  297. return FW_UPLOAD_ERR_NONE;
  298. }
  299. static const struct fw_upload_ops mpfs_auto_update_ops = {
  300. .prepare = mpfs_auto_update_prepare,
  301. .write = mpfs_auto_update_write,
  302. .poll_complete = mpfs_auto_update_poll_complete,
  303. .cancel = mpfs_auto_update_cancel,
  304. };
  305. static int mpfs_auto_update_available(struct mpfs_auto_update_priv *priv)
  306. {
  307. u32 *response_msg __free(kfree) =
  308. kzalloc(AUTO_UPDATE_FEATURE_RESP_SIZE * sizeof(*response_msg), GFP_KERNEL);
  309. struct mpfs_mss_response *response __free(kfree) =
  310. kzalloc(sizeof(struct mpfs_mss_response), GFP_KERNEL);
  311. struct mpfs_mss_msg *message __free(kfree) =
  312. kzalloc(sizeof(struct mpfs_mss_msg), GFP_KERNEL);
  313. int ret;
  314. if (!response_msg || !response || !message)
  315. return -ENOMEM;
  316. /*
  317. * To verify that Auto Update is possible, the "Query Security Service
  318. * Request" is performed.
  319. * This service has no command data & does not overload mbox_offset.
  320. */
  321. response->resp_msg = response_msg;
  322. response->resp_size = AUTO_UPDATE_FEATURE_RESP_SIZE;
  323. message->cmd_opcode = AUTO_UPDATE_FEATURE_CMD_OPCODE;
  324. message->cmd_data_size = AUTO_UPDATE_FEATURE_CMD_DATA_SIZE;
  325. message->response = response;
  326. message->cmd_data = AUTO_UPDATE_FEATURE_CMD_DATA;
  327. message->mbox_offset = AUTO_UPDATE_DEFAULT_MBOX_OFFSET;
  328. message->resp_offset = AUTO_UPDATE_DEFAULT_RESP_OFFSET;
  329. ret = mpfs_blocking_transaction(priv->sys_controller, message);
  330. if (ret)
  331. return ret;
  332. /*
  333. * Currently, the system controller's firmware does not generate any
  334. * interrupts for failed services, so mpfs_blocking_transaction() should
  335. * time out & therefore return an error.
  336. * Hitting this check is highly unlikely at present, but if the system
  337. * controller's behaviour changes so that it does generate interrupts
  338. * for failed services, it will be required.
  339. */
  340. if (response->resp_status)
  341. return -EIO;
  342. /*
  343. * Bit 5 of byte 1 is "UL_Auto Update" & if it is set, Auto Update is
  344. * not possible.
  345. */
  346. if (response_msg[1] & AUTO_UPDATE_FEATURE_ENABLED)
  347. return -EPERM;
  348. return 0;
  349. }
  350. static int mpfs_auto_update_probe(struct platform_device *pdev)
  351. {
  352. struct device *dev = &pdev->dev;
  353. struct mpfs_auto_update_priv *priv;
  354. struct fw_upload *fw_uploader;
  355. int ret;
  356. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  357. if (!priv)
  358. return -ENOMEM;
  359. priv->sys_controller = mpfs_sys_controller_get(dev);
  360. if (IS_ERR(priv->sys_controller))
  361. return dev_err_probe(dev, PTR_ERR(priv->sys_controller),
  362. "Could not register as a sub device of the system controller\n");
  363. priv->dev = dev;
  364. platform_set_drvdata(pdev, priv);
  365. ret = mpfs_auto_update_available(priv);
  366. if (ret)
  367. return dev_err_probe(dev, ret,
  368. "The current bitstream does not support auto-update\n");
  369. fw_uploader = firmware_upload_register(THIS_MODULE, dev, "mpfs-auto-update",
  370. &mpfs_auto_update_ops, priv);
  371. if (IS_ERR(fw_uploader))
  372. return dev_err_probe(dev, PTR_ERR(fw_uploader),
  373. "Failed to register the bitstream uploader\n");
  374. priv->fw_uploader = fw_uploader;
  375. return 0;
  376. }
  377. static void mpfs_auto_update_remove(struct platform_device *pdev)
  378. {
  379. struct mpfs_auto_update_priv *priv = platform_get_drvdata(pdev);
  380. firmware_upload_unregister(priv->fw_uploader);
  381. }
  382. static struct platform_driver mpfs_auto_update_driver = {
  383. .driver = {
  384. .name = "mpfs-auto-update",
  385. },
  386. .probe = mpfs_auto_update_probe,
  387. .remove_new = mpfs_auto_update_remove,
  388. };
  389. module_platform_driver(mpfs_auto_update_driver);
  390. MODULE_LICENSE("GPL");
  391. MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
  392. MODULE_DESCRIPTION("PolarFire SoC Auto Update FPGA reprogramming");