bootrom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * BOOTROM Greybus driver.
  3. *
  4. * Copyright 2016 Google Inc.
  5. * Copyright 2016 Linaro Ltd.
  6. *
  7. * Released under the GPLv2 only.
  8. */
  9. #include <linux/firmware.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/mutex.h>
  12. #include <linux/workqueue.h>
  13. #include "greybus.h"
  14. #include "firmware.h"
  15. /* Timeout, in jiffies, within which the next request must be received */
  16. #define NEXT_REQ_TIMEOUT_MS 1000
  17. /*
  18. * FIXME: Reduce this timeout once svc core handles parallel processing of
  19. * events from the SVC, which are handled sequentially today.
  20. */
  21. #define MODE_SWITCH_TIMEOUT_MS 10000
  22. enum next_request_type {
  23. NEXT_REQ_FIRMWARE_SIZE,
  24. NEXT_REQ_GET_FIRMWARE,
  25. NEXT_REQ_READY_TO_BOOT,
  26. NEXT_REQ_MODE_SWITCH,
  27. };
  28. struct gb_bootrom {
  29. struct gb_connection *connection;
  30. const struct firmware *fw;
  31. u8 protocol_major;
  32. u8 protocol_minor;
  33. enum next_request_type next_request;
  34. struct delayed_work dwork;
  35. struct mutex mutex; /* Protects bootrom->fw */
  36. };
  37. static void free_firmware(struct gb_bootrom *bootrom)
  38. {
  39. if (!bootrom->fw)
  40. return;
  41. release_firmware(bootrom->fw);
  42. bootrom->fw = NULL;
  43. }
  44. static void gb_bootrom_timedout(struct work_struct *work)
  45. {
  46. struct delayed_work *dwork = to_delayed_work(work);
  47. struct gb_bootrom *bootrom = container_of(dwork, struct gb_bootrom, dwork);
  48. struct device *dev = &bootrom->connection->bundle->dev;
  49. const char *reason;
  50. switch (bootrom->next_request) {
  51. case NEXT_REQ_FIRMWARE_SIZE:
  52. reason = "Firmware Size Request";
  53. break;
  54. case NEXT_REQ_GET_FIRMWARE:
  55. reason = "Get Firmware Request";
  56. break;
  57. case NEXT_REQ_READY_TO_BOOT:
  58. reason = "Ready to Boot Request";
  59. break;
  60. case NEXT_REQ_MODE_SWITCH:
  61. reason = "Interface Mode Switch";
  62. break;
  63. default:
  64. reason = NULL;
  65. dev_err(dev, "Invalid next-request: %u", bootrom->next_request);
  66. break;
  67. }
  68. dev_err(dev, "Timed out waiting for %s from the Module\n", reason);
  69. mutex_lock(&bootrom->mutex);
  70. free_firmware(bootrom);
  71. mutex_unlock(&bootrom->mutex);
  72. /* TODO: Power-off Module ? */
  73. }
  74. static void gb_bootrom_set_timeout(struct gb_bootrom *bootrom,
  75. enum next_request_type next, unsigned long timeout)
  76. {
  77. bootrom->next_request = next;
  78. schedule_delayed_work(&bootrom->dwork, msecs_to_jiffies(timeout));
  79. }
  80. static void gb_bootrom_cancel_timeout(struct gb_bootrom *bootrom)
  81. {
  82. cancel_delayed_work_sync(&bootrom->dwork);
  83. }
  84. /*
  85. * The es2 chip doesn't have VID/PID programmed into the hardware and we need to
  86. * hack that up to distinguish different modules and their firmware blobs.
  87. *
  88. * This fetches VID/PID (over bootrom protocol) for es2 chip only, when VID/PID
  89. * already sent during hotplug are 0.
  90. *
  91. * Otherwise, we keep intf->vendor_id/product_id same as what's passed
  92. * during hotplug.
  93. */
  94. static void bootrom_es2_fixup_vid_pid(struct gb_bootrom *bootrom)
  95. {
  96. struct gb_bootrom_get_vid_pid_response response;
  97. struct gb_connection *connection = bootrom->connection;
  98. struct gb_interface *intf = connection->bundle->intf;
  99. int ret;
  100. if (!(intf->quirks & GB_INTERFACE_QUIRK_NO_GMP_IDS))
  101. return;
  102. ret = gb_operation_sync(connection, GB_BOOTROM_TYPE_GET_VID_PID,
  103. NULL, 0, &response, sizeof(response));
  104. if (ret) {
  105. dev_err(&connection->bundle->dev,
  106. "Bootrom get vid/pid operation failed (%d)\n", ret);
  107. return;
  108. }
  109. /*
  110. * NOTE: This is hacked, so that the same values of VID/PID can be used
  111. * by next firmware level as well. The uevent for bootrom will still
  112. * have VID/PID as 0, though after this point the sysfs files will start
  113. * showing the updated values. But yeah, that's a bit racy as the same
  114. * sysfs files would be showing 0 before this point.
  115. */
  116. intf->vendor_id = le32_to_cpu(response.vendor_id);
  117. intf->product_id = le32_to_cpu(response.product_id);
  118. dev_dbg(&connection->bundle->dev, "Bootrom got vid (0x%x)/pid (0x%x)\n",
  119. intf->vendor_id, intf->product_id);
  120. }
  121. /* This returns path of the firmware blob on the disk */
  122. static int find_firmware(struct gb_bootrom *bootrom, u8 stage)
  123. {
  124. struct gb_connection *connection = bootrom->connection;
  125. struct gb_interface *intf = connection->bundle->intf;
  126. char firmware_name[49];
  127. int rc;
  128. /* Already have a firmware, free it */
  129. free_firmware(bootrom);
  130. /* Bootrom protocol is only supported for loading Stage 2 firmware */
  131. if (stage != 2) {
  132. dev_err(&connection->bundle->dev, "Invalid boot stage: %u\n",
  133. stage);
  134. return -EINVAL;
  135. }
  136. /*
  137. * Create firmware name
  138. *
  139. * XXX Name it properly..
  140. */
  141. snprintf(firmware_name, sizeof(firmware_name),
  142. FW_NAME_PREFIX "/*(DEBLOBBED)*/",
  143. intf->ddbl1_manufacturer_id, intf->ddbl1_product_id,
  144. intf->vendor_id, intf->product_id);
  145. // FIXME:
  146. // Turn to dev_dbg later after everyone has valid bootloaders with good
  147. // ids, but leave this as dev_info for now to make it easier to track
  148. // down "empty" vid/pid modules.
  149. dev_info(&connection->bundle->dev, "Firmware file '%s' requested\n",
  150. firmware_name);
  151. rc = reject_firmware(&bootrom->fw, firmware_name,
  152. &connection->bundle->dev);
  153. if (rc) {
  154. dev_err(&connection->bundle->dev,
  155. "failed to find %s firmware (%d)\n", firmware_name, rc);
  156. }
  157. return rc;
  158. }
  159. static int gb_bootrom_firmware_size_request(struct gb_operation *op)
  160. {
  161. struct gb_bootrom *bootrom = gb_connection_get_data(op->connection);
  162. struct gb_bootrom_firmware_size_request *size_request = op->request->payload;
  163. struct gb_bootrom_firmware_size_response *size_response;
  164. struct device *dev = &op->connection->bundle->dev;
  165. int ret;
  166. /* Disable timeouts */
  167. gb_bootrom_cancel_timeout(bootrom);
  168. if (op->request->payload_size != sizeof(*size_request)) {
  169. dev_err(dev, "%s: illegal size of firmware size request (%zu != %zu)\n",
  170. __func__, op->request->payload_size,
  171. sizeof(*size_request));
  172. ret = -EINVAL;
  173. goto queue_work;
  174. }
  175. mutex_lock(&bootrom->mutex);
  176. ret = find_firmware(bootrom, size_request->stage);
  177. if (ret)
  178. goto unlock;
  179. if (!gb_operation_response_alloc(op, sizeof(*size_response),
  180. GFP_KERNEL)) {
  181. dev_err(dev, "%s: error allocating response\n", __func__);
  182. free_firmware(bootrom);
  183. ret = -ENOMEM;
  184. goto unlock;
  185. }
  186. size_response = op->response->payload;
  187. size_response->size = cpu_to_le32(bootrom->fw->size);
  188. dev_dbg(dev, "%s: firmware size %d bytes\n", __func__, size_response->size);
  189. unlock:
  190. mutex_unlock(&bootrom->mutex);
  191. queue_work:
  192. if (!ret) {
  193. /* Refresh timeout */
  194. gb_bootrom_set_timeout(bootrom, NEXT_REQ_GET_FIRMWARE,
  195. NEXT_REQ_TIMEOUT_MS);
  196. }
  197. return ret;
  198. }
  199. static int gb_bootrom_get_firmware(struct gb_operation *op)
  200. {
  201. struct gb_bootrom *bootrom = gb_connection_get_data(op->connection);
  202. const struct firmware *fw;
  203. struct gb_bootrom_get_firmware_request *firmware_request;
  204. struct gb_bootrom_get_firmware_response *firmware_response;
  205. struct device *dev = &op->connection->bundle->dev;
  206. unsigned int offset, size;
  207. enum next_request_type next_request;
  208. int ret = 0;
  209. /* Disable timeouts */
  210. gb_bootrom_cancel_timeout(bootrom);
  211. if (op->request->payload_size != sizeof(*firmware_request)) {
  212. dev_err(dev, "%s: Illegal size of get firmware request (%zu %zu)\n",
  213. __func__, op->request->payload_size,
  214. sizeof(*firmware_request));
  215. ret = -EINVAL;
  216. goto queue_work;
  217. }
  218. mutex_lock(&bootrom->mutex);
  219. fw = bootrom->fw;
  220. if (!fw) {
  221. dev_err(dev, "%s: firmware not available\n", __func__);
  222. ret = -EINVAL;
  223. goto unlock;
  224. }
  225. firmware_request = op->request->payload;
  226. offset = le32_to_cpu(firmware_request->offset);
  227. size = le32_to_cpu(firmware_request->size);
  228. if (offset >= fw->size || size > fw->size - offset) {
  229. dev_warn(dev, "bad firmware request (offs = %u, size = %u)\n",
  230. offset, size);
  231. ret = -EINVAL;
  232. goto unlock;
  233. }
  234. if (!gb_operation_response_alloc(op, sizeof(*firmware_response) + size,
  235. GFP_KERNEL)) {
  236. dev_err(dev, "%s: error allocating response\n", __func__);
  237. ret = -ENOMEM;
  238. goto unlock;
  239. }
  240. firmware_response = op->response->payload;
  241. memcpy(firmware_response->data, fw->data + offset, size);
  242. dev_dbg(dev, "responding with firmware (offs = %u, size = %u)\n", offset,
  243. size);
  244. unlock:
  245. mutex_unlock(&bootrom->mutex);
  246. queue_work:
  247. /* Refresh timeout */
  248. if (!ret && (offset + size == fw->size))
  249. next_request = NEXT_REQ_READY_TO_BOOT;
  250. else
  251. next_request = NEXT_REQ_GET_FIRMWARE;
  252. gb_bootrom_set_timeout(bootrom, next_request, NEXT_REQ_TIMEOUT_MS);
  253. return ret;
  254. }
  255. static int gb_bootrom_ready_to_boot(struct gb_operation *op)
  256. {
  257. struct gb_connection *connection = op->connection;
  258. struct gb_bootrom *bootrom = gb_connection_get_data(connection);
  259. struct gb_bootrom_ready_to_boot_request *rtb_request;
  260. struct device *dev = &connection->bundle->dev;
  261. u8 status;
  262. int ret = 0;
  263. /* Disable timeouts */
  264. gb_bootrom_cancel_timeout(bootrom);
  265. if (op->request->payload_size != sizeof(*rtb_request)) {
  266. dev_err(dev, "%s: Illegal size of ready to boot request (%zu %zu)\n",
  267. __func__, op->request->payload_size,
  268. sizeof(*rtb_request));
  269. ret = -EINVAL;
  270. goto queue_work;
  271. }
  272. rtb_request = op->request->payload;
  273. status = rtb_request->status;
  274. /* Return error if the blob was invalid */
  275. if (status == GB_BOOTROM_BOOT_STATUS_INVALID) {
  276. ret = -EINVAL;
  277. goto queue_work;
  278. }
  279. /*
  280. * XXX Should we return error for insecure firmware?
  281. */
  282. dev_dbg(dev, "ready to boot: 0x%x, 0\n", status);
  283. queue_work:
  284. /*
  285. * Refresh timeout, the Interface shall load the new personality and
  286. * send a new hotplug request, which shall get rid of the bootrom
  287. * connection. As that can take some time, increase the timeout a bit.
  288. */
  289. gb_bootrom_set_timeout(bootrom, NEXT_REQ_MODE_SWITCH,
  290. MODE_SWITCH_TIMEOUT_MS);
  291. return ret;
  292. }
  293. static int gb_bootrom_request_handler(struct gb_operation *op)
  294. {
  295. u8 type = op->type;
  296. switch (type) {
  297. case GB_BOOTROM_TYPE_FIRMWARE_SIZE:
  298. return gb_bootrom_firmware_size_request(op);
  299. case GB_BOOTROM_TYPE_GET_FIRMWARE:
  300. return gb_bootrom_get_firmware(op);
  301. case GB_BOOTROM_TYPE_READY_TO_BOOT:
  302. return gb_bootrom_ready_to_boot(op);
  303. default:
  304. dev_err(&op->connection->bundle->dev,
  305. "unsupported request: %u\n", type);
  306. return -EINVAL;
  307. }
  308. }
  309. static int gb_bootrom_get_version(struct gb_bootrom *bootrom)
  310. {
  311. struct gb_bundle *bundle = bootrom->connection->bundle;
  312. struct gb_bootrom_version_request request;
  313. struct gb_bootrom_version_response response;
  314. int ret;
  315. request.major = GB_BOOTROM_VERSION_MAJOR;
  316. request.minor = GB_BOOTROM_VERSION_MINOR;
  317. ret = gb_operation_sync(bootrom->connection,
  318. GB_BOOTROM_TYPE_VERSION,
  319. &request, sizeof(request), &response,
  320. sizeof(response));
  321. if (ret) {
  322. dev_err(&bundle->dev,
  323. "failed to get protocol version: %d\n",
  324. ret);
  325. return ret;
  326. }
  327. if (response.major > request.major) {
  328. dev_err(&bundle->dev,
  329. "unsupported major protocol version (%u > %u)\n",
  330. response.major, request.major);
  331. return -ENOTSUPP;
  332. }
  333. bootrom->protocol_major = response.major;
  334. bootrom->protocol_minor = response.minor;
  335. dev_dbg(&bundle->dev, "%s - %u.%u\n", __func__, response.major,
  336. response.minor);
  337. return 0;
  338. }
  339. static int gb_bootrom_probe(struct gb_bundle *bundle,
  340. const struct greybus_bundle_id *id)
  341. {
  342. struct greybus_descriptor_cport *cport_desc;
  343. struct gb_connection *connection;
  344. struct gb_bootrom *bootrom;
  345. int ret;
  346. if (bundle->num_cports != 1)
  347. return -ENODEV;
  348. cport_desc = &bundle->cport_desc[0];
  349. if (cport_desc->protocol_id != GREYBUS_PROTOCOL_BOOTROM)
  350. return -ENODEV;
  351. bootrom = kzalloc(sizeof(*bootrom), GFP_KERNEL);
  352. if (!bootrom)
  353. return -ENOMEM;
  354. connection = gb_connection_create(bundle,
  355. le16_to_cpu(cport_desc->id),
  356. gb_bootrom_request_handler);
  357. if (IS_ERR(connection)) {
  358. ret = PTR_ERR(connection);
  359. goto err_free_bootrom;
  360. }
  361. gb_connection_set_data(connection, bootrom);
  362. bootrom->connection = connection;
  363. mutex_init(&bootrom->mutex);
  364. INIT_DELAYED_WORK(&bootrom->dwork, gb_bootrom_timedout);
  365. greybus_set_drvdata(bundle, bootrom);
  366. ret = gb_connection_enable_tx(connection);
  367. if (ret)
  368. goto err_connection_destroy;
  369. ret = gb_bootrom_get_version(bootrom);
  370. if (ret)
  371. goto err_connection_disable;
  372. bootrom_es2_fixup_vid_pid(bootrom);
  373. ret = gb_connection_enable(connection);
  374. if (ret)
  375. goto err_connection_disable;
  376. /* Refresh timeout */
  377. gb_bootrom_set_timeout(bootrom, NEXT_REQ_FIRMWARE_SIZE,
  378. NEXT_REQ_TIMEOUT_MS);
  379. /* Tell bootrom we're ready. */
  380. ret = gb_operation_sync(connection, GB_BOOTROM_TYPE_AP_READY, NULL, 0,
  381. NULL, 0);
  382. if (ret) {
  383. dev_err(&connection->bundle->dev,
  384. "failed to send AP READY: %d\n", ret);
  385. goto err_cancel_timeout;
  386. }
  387. dev_dbg(&bundle->dev, "AP_READY sent\n");
  388. return 0;
  389. err_cancel_timeout:
  390. gb_bootrom_cancel_timeout(bootrom);
  391. err_connection_disable:
  392. gb_connection_disable(connection);
  393. err_connection_destroy:
  394. gb_connection_destroy(connection);
  395. err_free_bootrom:
  396. kfree(bootrom);
  397. return ret;
  398. }
  399. static void gb_bootrom_disconnect(struct gb_bundle *bundle)
  400. {
  401. struct gb_bootrom *bootrom = greybus_get_drvdata(bundle);
  402. dev_dbg(&bundle->dev, "%s\n", __func__);
  403. gb_connection_disable(bootrom->connection);
  404. /* Disable timeouts */
  405. gb_bootrom_cancel_timeout(bootrom);
  406. /*
  407. * Release firmware:
  408. *
  409. * As the connection and the delayed work are already disabled, we don't
  410. * need to lock access to bootrom->fw here.
  411. */
  412. free_firmware(bootrom);
  413. gb_connection_destroy(bootrom->connection);
  414. kfree(bootrom);
  415. }
  416. static const struct greybus_bundle_id gb_bootrom_id_table[] = {
  417. { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_BOOTROM) },
  418. { }
  419. };
  420. static struct greybus_driver gb_bootrom_driver = {
  421. .name = "bootrom",
  422. .probe = gb_bootrom_probe,
  423. .disconnect = gb_bootrom_disconnect,
  424. .id_table = gb_bootrom_id_table,
  425. };
  426. module_greybus_driver(gb_bootrom_driver);
  427. MODULE_LICENSE("GPL v2");