cosm_main.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC Coprocessor State Management (COSM) Driver
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/idr.h>
  24. #include <linux/slab.h>
  25. #include <linux/cred.h>
  26. #include "cosm_main.h"
  27. static const char cosm_driver_name[] = "mic";
  28. /* COSM ID allocator */
  29. static struct ida g_cosm_ida;
  30. /* Class of MIC devices for sysfs accessibility. */
  31. static struct class *g_cosm_class;
  32. /* Number of MIC devices */
  33. static atomic_t g_num_dev;
  34. /**
  35. * cosm_hw_reset - Issue a HW reset for the MIC device
  36. * @cdev: pointer to cosm_device instance
  37. */
  38. static void cosm_hw_reset(struct cosm_device *cdev, bool force)
  39. {
  40. int i;
  41. #define MIC_RESET_TO (45)
  42. if (force && cdev->hw_ops->force_reset)
  43. cdev->hw_ops->force_reset(cdev);
  44. else
  45. cdev->hw_ops->reset(cdev);
  46. for (i = 0; i < MIC_RESET_TO; i++) {
  47. if (cdev->hw_ops->ready(cdev)) {
  48. cosm_set_state(cdev, MIC_READY);
  49. return;
  50. }
  51. /*
  52. * Resets typically take 10s of seconds to complete.
  53. * Since an MMIO read is required to check if the
  54. * firmware is ready or not, a 1 second delay works nicely.
  55. */
  56. msleep(1000);
  57. }
  58. cosm_set_state(cdev, MIC_RESET_FAILED);
  59. }
  60. /**
  61. * cosm_start - Start the MIC
  62. * @cdev: pointer to cosm_device instance
  63. *
  64. * This function prepares an MIC for boot and initiates boot.
  65. * RETURNS: An appropriate -ERRNO error value on error, or 0 for success.
  66. */
  67. int cosm_start(struct cosm_device *cdev)
  68. {
  69. const struct cred *orig_cred;
  70. struct cred *override_cred;
  71. int rc;
  72. mutex_lock(&cdev->cosm_mutex);
  73. if (!cdev->bootmode) {
  74. dev_err(&cdev->dev, "%s %d bootmode not set\n",
  75. __func__, __LINE__);
  76. rc = -EINVAL;
  77. goto unlock_ret;
  78. }
  79. retry:
  80. if (cdev->state != MIC_READY) {
  81. dev_err(&cdev->dev, "%s %d MIC state not READY\n",
  82. __func__, __LINE__);
  83. rc = -EINVAL;
  84. goto unlock_ret;
  85. }
  86. if (!cdev->hw_ops->ready(cdev)) {
  87. cosm_hw_reset(cdev, false);
  88. /*
  89. * The state will either be MIC_READY if the reset succeeded
  90. * or MIC_RESET_FAILED if the firmware reset failed.
  91. */
  92. goto retry;
  93. }
  94. /*
  95. * Set credentials to root to allow non-root user to download initramsfs
  96. * with 600 permissions
  97. */
  98. override_cred = prepare_creds();
  99. if (!override_cred) {
  100. dev_err(&cdev->dev, "%s %d prepare_creds failed\n",
  101. __func__, __LINE__);
  102. rc = -ENOMEM;
  103. goto unlock_ret;
  104. }
  105. override_cred->fsuid = GLOBAL_ROOT_UID;
  106. orig_cred = override_creds(override_cred);
  107. rc = cdev->hw_ops->start(cdev, cdev->index);
  108. revert_creds(orig_cred);
  109. put_cred(override_cred);
  110. if (rc)
  111. goto unlock_ret;
  112. /*
  113. * If linux is being booted, card is treated 'online' only
  114. * when the scif interface in the card is up. If anything else
  115. * is booted, we set card to 'online' immediately.
  116. */
  117. if (!strcmp(cdev->bootmode, "linux"))
  118. cosm_set_state(cdev, MIC_BOOTING);
  119. else
  120. cosm_set_state(cdev, MIC_ONLINE);
  121. unlock_ret:
  122. mutex_unlock(&cdev->cosm_mutex);
  123. if (rc)
  124. dev_err(&cdev->dev, "cosm_start failed rc %d\n", rc);
  125. return rc;
  126. }
  127. /**
  128. * cosm_stop - Prepare the MIC for reset and trigger reset
  129. * @cdev: pointer to cosm_device instance
  130. * @force: force a MIC to reset even if it is already reset and ready.
  131. *
  132. * RETURNS: None
  133. */
  134. void cosm_stop(struct cosm_device *cdev, bool force)
  135. {
  136. mutex_lock(&cdev->cosm_mutex);
  137. if (cdev->state != MIC_READY || force) {
  138. /*
  139. * Don't call hw_ops if they have been called previously.
  140. * stop(..) calls device_unregister and will crash the system if
  141. * called multiple times.
  142. */
  143. u8 state = cdev->state == MIC_RESETTING ?
  144. cdev->prev_state : cdev->state;
  145. bool call_hw_ops = state != MIC_RESET_FAILED &&
  146. state != MIC_READY;
  147. if (cdev->state != MIC_RESETTING)
  148. cosm_set_state(cdev, MIC_RESETTING);
  149. cdev->heartbeat_watchdog_enable = false;
  150. if (call_hw_ops)
  151. cdev->hw_ops->stop(cdev, force);
  152. cosm_hw_reset(cdev, force);
  153. cosm_set_shutdown_status(cdev, MIC_NOP);
  154. if (call_hw_ops && cdev->hw_ops->post_reset)
  155. cdev->hw_ops->post_reset(cdev, cdev->state);
  156. }
  157. mutex_unlock(&cdev->cosm_mutex);
  158. flush_work(&cdev->scif_work);
  159. }
  160. /**
  161. * cosm_reset_trigger_work - Trigger MIC reset
  162. * @work: The work structure
  163. *
  164. * This work is scheduled whenever the host wants to reset the MIC.
  165. */
  166. static void cosm_reset_trigger_work(struct work_struct *work)
  167. {
  168. struct cosm_device *cdev = container_of(work, struct cosm_device,
  169. reset_trigger_work);
  170. cosm_stop(cdev, false);
  171. }
  172. /**
  173. * cosm_reset - Schedule MIC reset
  174. * @cdev: pointer to cosm_device instance
  175. *
  176. * RETURNS: An -EINVAL if the card is already READY or 0 for success.
  177. */
  178. int cosm_reset(struct cosm_device *cdev)
  179. {
  180. int rc = 0;
  181. mutex_lock(&cdev->cosm_mutex);
  182. if (cdev->state != MIC_READY) {
  183. if (cdev->state != MIC_RESETTING) {
  184. cdev->prev_state = cdev->state;
  185. cosm_set_state(cdev, MIC_RESETTING);
  186. schedule_work(&cdev->reset_trigger_work);
  187. }
  188. } else {
  189. dev_err(&cdev->dev, "%s %d MIC is READY\n", __func__, __LINE__);
  190. rc = -EINVAL;
  191. }
  192. mutex_unlock(&cdev->cosm_mutex);
  193. return rc;
  194. }
  195. /**
  196. * cosm_shutdown - Initiate MIC shutdown.
  197. * @cdev: pointer to cosm_device instance
  198. *
  199. * RETURNS: None
  200. */
  201. int cosm_shutdown(struct cosm_device *cdev)
  202. {
  203. struct cosm_msg msg = { .id = COSM_MSG_SHUTDOWN };
  204. int rc = 0;
  205. mutex_lock(&cdev->cosm_mutex);
  206. if (cdev->state != MIC_ONLINE) {
  207. rc = -EINVAL;
  208. dev_err(&cdev->dev, "%s %d skipping shutdown in state: %s\n",
  209. __func__, __LINE__, cosm_state_string[cdev->state]);
  210. goto err;
  211. }
  212. if (!cdev->epd) {
  213. rc = -ENOTCONN;
  214. dev_err(&cdev->dev, "%s %d scif endpoint not connected rc %d\n",
  215. __func__, __LINE__, rc);
  216. goto err;
  217. }
  218. rc = scif_send(cdev->epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
  219. if (rc < 0) {
  220. dev_err(&cdev->dev, "%s %d scif_send failed rc %d\n",
  221. __func__, __LINE__, rc);
  222. goto err;
  223. }
  224. cdev->heartbeat_watchdog_enable = false;
  225. cosm_set_state(cdev, MIC_SHUTTING_DOWN);
  226. rc = 0;
  227. err:
  228. mutex_unlock(&cdev->cosm_mutex);
  229. return rc;
  230. }
  231. static int cosm_driver_probe(struct cosm_device *cdev)
  232. {
  233. int rc;
  234. /* Initialize SCIF server at first probe */
  235. if (atomic_add_return(1, &g_num_dev) == 1) {
  236. rc = cosm_scif_init();
  237. if (rc)
  238. goto scif_exit;
  239. }
  240. mutex_init(&cdev->cosm_mutex);
  241. INIT_WORK(&cdev->reset_trigger_work, cosm_reset_trigger_work);
  242. INIT_WORK(&cdev->scif_work, cosm_scif_work);
  243. cdev->sysfs_heartbeat_enable = true;
  244. cosm_sysfs_init(cdev);
  245. cdev->sdev = device_create_with_groups(g_cosm_class, cdev->dev.parent,
  246. MKDEV(0, cdev->index), cdev, cdev->attr_group,
  247. "mic%d", cdev->index);
  248. if (IS_ERR(cdev->sdev)) {
  249. rc = PTR_ERR(cdev->sdev);
  250. dev_err(&cdev->dev, "device_create_with_groups failed rc %d\n",
  251. rc);
  252. goto scif_exit;
  253. }
  254. cdev->state_sysfs = sysfs_get_dirent(cdev->sdev->kobj.sd,
  255. "state");
  256. if (!cdev->state_sysfs) {
  257. rc = -ENODEV;
  258. dev_err(&cdev->dev, "sysfs_get_dirent failed rc %d\n", rc);
  259. goto destroy_device;
  260. }
  261. cosm_create_debug_dir(cdev);
  262. return 0;
  263. destroy_device:
  264. device_destroy(g_cosm_class, MKDEV(0, cdev->index));
  265. scif_exit:
  266. if (atomic_dec_and_test(&g_num_dev))
  267. cosm_scif_exit();
  268. return rc;
  269. }
  270. static void cosm_driver_remove(struct cosm_device *cdev)
  271. {
  272. cosm_delete_debug_dir(cdev);
  273. sysfs_put(cdev->state_sysfs);
  274. device_destroy(g_cosm_class, MKDEV(0, cdev->index));
  275. flush_work(&cdev->reset_trigger_work);
  276. cosm_stop(cdev, false);
  277. if (atomic_dec_and_test(&g_num_dev))
  278. cosm_scif_exit();
  279. /* These sysfs entries might have allocated */
  280. kfree(cdev->cmdline);
  281. kfree(cdev->firmware);
  282. kfree(cdev->ramdisk);
  283. kfree(cdev->bootmode);
  284. }
  285. static int cosm_suspend(struct device *dev)
  286. {
  287. struct cosm_device *cdev = dev_to_cosm(dev);
  288. mutex_lock(&cdev->cosm_mutex);
  289. switch (cdev->state) {
  290. /**
  291. * Suspend/freeze hooks in userspace have already shutdown the card.
  292. * Card should be 'ready' in most cases. It is however possible that
  293. * some userspace application initiated a boot. In those cases, we
  294. * simply reset the card.
  295. */
  296. case MIC_ONLINE:
  297. case MIC_BOOTING:
  298. case MIC_SHUTTING_DOWN:
  299. mutex_unlock(&cdev->cosm_mutex);
  300. cosm_stop(cdev, false);
  301. break;
  302. default:
  303. mutex_unlock(&cdev->cosm_mutex);
  304. break;
  305. }
  306. return 0;
  307. }
  308. static const struct dev_pm_ops cosm_pm_ops = {
  309. .suspend = cosm_suspend,
  310. .freeze = cosm_suspend
  311. };
  312. static struct cosm_driver cosm_driver = {
  313. .driver = {
  314. .name = KBUILD_MODNAME,
  315. .owner = THIS_MODULE,
  316. .pm = &cosm_pm_ops,
  317. },
  318. .probe = cosm_driver_probe,
  319. .remove = cosm_driver_remove
  320. };
  321. static int __init cosm_init(void)
  322. {
  323. int ret;
  324. cosm_init_debugfs();
  325. g_cosm_class = class_create(THIS_MODULE, cosm_driver_name);
  326. if (IS_ERR(g_cosm_class)) {
  327. ret = PTR_ERR(g_cosm_class);
  328. pr_err("class_create failed ret %d\n", ret);
  329. goto cleanup_debugfs;
  330. }
  331. ida_init(&g_cosm_ida);
  332. ret = cosm_register_driver(&cosm_driver);
  333. if (ret) {
  334. pr_err("cosm_register_driver failed ret %d\n", ret);
  335. goto ida_destroy;
  336. }
  337. return 0;
  338. ida_destroy:
  339. ida_destroy(&g_cosm_ida);
  340. class_destroy(g_cosm_class);
  341. cleanup_debugfs:
  342. cosm_exit_debugfs();
  343. return ret;
  344. }
  345. static void __exit cosm_exit(void)
  346. {
  347. cosm_unregister_driver(&cosm_driver);
  348. ida_destroy(&g_cosm_ida);
  349. class_destroy(g_cosm_class);
  350. cosm_exit_debugfs();
  351. }
  352. module_init(cosm_init);
  353. module_exit(cosm_exit);
  354. MODULE_AUTHOR("Intel Corporation");
  355. MODULE_DESCRIPTION("Intel(R) MIC Coprocessor State Management (COSM) Driver");
  356. MODULE_LICENSE("GPL v2");