cosm_main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. bool call_hw_ops = cdev->state != MIC_RESET_FAILED &&
  144. cdev->state != MIC_READY;
  145. if (cdev->state != MIC_RESETTING)
  146. cosm_set_state(cdev, MIC_RESETTING);
  147. cdev->heartbeat_watchdog_enable = false;
  148. if (call_hw_ops)
  149. cdev->hw_ops->stop(cdev, force);
  150. cosm_hw_reset(cdev, force);
  151. cosm_set_shutdown_status(cdev, MIC_NOP);
  152. if (call_hw_ops && cdev->hw_ops->post_reset)
  153. cdev->hw_ops->post_reset(cdev, cdev->state);
  154. }
  155. mutex_unlock(&cdev->cosm_mutex);
  156. flush_work(&cdev->scif_work);
  157. }
  158. /**
  159. * cosm_reset_trigger_work - Trigger MIC reset
  160. * @work: The work structure
  161. *
  162. * This work is scheduled whenever the host wants to reset the MIC.
  163. */
  164. static void cosm_reset_trigger_work(struct work_struct *work)
  165. {
  166. struct cosm_device *cdev = container_of(work, struct cosm_device,
  167. reset_trigger_work);
  168. cosm_stop(cdev, false);
  169. }
  170. /**
  171. * cosm_reset - Schedule MIC reset
  172. * @cdev: pointer to cosm_device instance
  173. *
  174. * RETURNS: An -EINVAL if the card is already READY or 0 for success.
  175. */
  176. int cosm_reset(struct cosm_device *cdev)
  177. {
  178. int rc = 0;
  179. mutex_lock(&cdev->cosm_mutex);
  180. if (cdev->state != MIC_READY) {
  181. cosm_set_state(cdev, MIC_RESETTING);
  182. schedule_work(&cdev->reset_trigger_work);
  183. } else {
  184. dev_err(&cdev->dev, "%s %d MIC is READY\n", __func__, __LINE__);
  185. rc = -EINVAL;
  186. }
  187. mutex_unlock(&cdev->cosm_mutex);
  188. return rc;
  189. }
  190. /**
  191. * cosm_shutdown - Initiate MIC shutdown.
  192. * @cdev: pointer to cosm_device instance
  193. *
  194. * RETURNS: None
  195. */
  196. int cosm_shutdown(struct cosm_device *cdev)
  197. {
  198. struct cosm_msg msg = { .id = COSM_MSG_SHUTDOWN };
  199. int rc = 0;
  200. mutex_lock(&cdev->cosm_mutex);
  201. if (cdev->state != MIC_ONLINE) {
  202. rc = -EINVAL;
  203. dev_err(&cdev->dev, "%s %d skipping shutdown in state: %s\n",
  204. __func__, __LINE__, cosm_state_string[cdev->state]);
  205. goto err;
  206. }
  207. if (!cdev->epd) {
  208. rc = -ENOTCONN;
  209. dev_err(&cdev->dev, "%s %d scif endpoint not connected rc %d\n",
  210. __func__, __LINE__, rc);
  211. goto err;
  212. }
  213. rc = scif_send(cdev->epd, &msg, sizeof(msg), SCIF_SEND_BLOCK);
  214. if (rc < 0) {
  215. dev_err(&cdev->dev, "%s %d scif_send failed rc %d\n",
  216. __func__, __LINE__, rc);
  217. goto err;
  218. }
  219. cdev->heartbeat_watchdog_enable = false;
  220. cosm_set_state(cdev, MIC_SHUTTING_DOWN);
  221. rc = 0;
  222. err:
  223. mutex_unlock(&cdev->cosm_mutex);
  224. return rc;
  225. }
  226. static int cosm_driver_probe(struct cosm_device *cdev)
  227. {
  228. int rc;
  229. /* Initialize SCIF server at first probe */
  230. if (atomic_add_return(1, &g_num_dev) == 1) {
  231. rc = cosm_scif_init();
  232. if (rc)
  233. goto scif_exit;
  234. }
  235. mutex_init(&cdev->cosm_mutex);
  236. INIT_WORK(&cdev->reset_trigger_work, cosm_reset_trigger_work);
  237. INIT_WORK(&cdev->scif_work, cosm_scif_work);
  238. cdev->sysfs_heartbeat_enable = true;
  239. cosm_sysfs_init(cdev);
  240. cdev->sdev = device_create_with_groups(g_cosm_class, cdev->dev.parent,
  241. MKDEV(0, cdev->index), cdev, cdev->attr_group,
  242. "mic%d", cdev->index);
  243. if (IS_ERR(cdev->sdev)) {
  244. rc = PTR_ERR(cdev->sdev);
  245. dev_err(&cdev->dev, "device_create_with_groups failed rc %d\n",
  246. rc);
  247. goto scif_exit;
  248. }
  249. cdev->state_sysfs = sysfs_get_dirent(cdev->sdev->kobj.sd,
  250. "state");
  251. if (!cdev->state_sysfs) {
  252. rc = -ENODEV;
  253. dev_err(&cdev->dev, "sysfs_get_dirent failed rc %d\n", rc);
  254. goto destroy_device;
  255. }
  256. cosm_create_debug_dir(cdev);
  257. return 0;
  258. destroy_device:
  259. device_destroy(g_cosm_class, MKDEV(0, cdev->index));
  260. scif_exit:
  261. if (atomic_dec_and_test(&g_num_dev))
  262. cosm_scif_exit();
  263. return rc;
  264. }
  265. static void cosm_driver_remove(struct cosm_device *cdev)
  266. {
  267. cosm_delete_debug_dir(cdev);
  268. sysfs_put(cdev->state_sysfs);
  269. device_destroy(g_cosm_class, MKDEV(0, cdev->index));
  270. flush_work(&cdev->reset_trigger_work);
  271. cosm_stop(cdev, false);
  272. if (atomic_dec_and_test(&g_num_dev))
  273. cosm_scif_exit();
  274. /* These sysfs entries might have allocated */
  275. kfree(cdev->cmdline);
  276. kfree(cdev->firmware);
  277. kfree(cdev->ramdisk);
  278. kfree(cdev->bootmode);
  279. }
  280. static int cosm_suspend(struct device *dev)
  281. {
  282. struct cosm_device *cdev = dev_to_cosm(dev);
  283. mutex_lock(&cdev->cosm_mutex);
  284. switch (cdev->state) {
  285. /**
  286. * Suspend/freeze hooks in userspace have already shutdown the card.
  287. * Card should be 'ready' in most cases. It is however possible that
  288. * some userspace application initiated a boot. In those cases, we
  289. * simply reset the card.
  290. */
  291. case MIC_ONLINE:
  292. case MIC_BOOTING:
  293. case MIC_SHUTTING_DOWN:
  294. mutex_unlock(&cdev->cosm_mutex);
  295. cosm_stop(cdev, false);
  296. break;
  297. default:
  298. mutex_unlock(&cdev->cosm_mutex);
  299. break;
  300. }
  301. return 0;
  302. }
  303. static const struct dev_pm_ops cosm_pm_ops = {
  304. .suspend = cosm_suspend,
  305. .freeze = cosm_suspend
  306. };
  307. static struct cosm_driver cosm_driver = {
  308. .driver = {
  309. .name = KBUILD_MODNAME,
  310. .owner = THIS_MODULE,
  311. .pm = &cosm_pm_ops,
  312. },
  313. .probe = cosm_driver_probe,
  314. .remove = cosm_driver_remove
  315. };
  316. static int __init cosm_init(void)
  317. {
  318. int ret;
  319. cosm_init_debugfs();
  320. g_cosm_class = class_create(THIS_MODULE, cosm_driver_name);
  321. if (IS_ERR(g_cosm_class)) {
  322. ret = PTR_ERR(g_cosm_class);
  323. pr_err("class_create failed ret %d\n", ret);
  324. goto cleanup_debugfs;
  325. }
  326. ida_init(&g_cosm_ida);
  327. ret = cosm_register_driver(&cosm_driver);
  328. if (ret) {
  329. pr_err("cosm_register_driver failed ret %d\n", ret);
  330. goto ida_destroy;
  331. }
  332. return 0;
  333. ida_destroy:
  334. ida_destroy(&g_cosm_ida);
  335. class_destroy(g_cosm_class);
  336. cleanup_debugfs:
  337. cosm_exit_debugfs();
  338. return ret;
  339. }
  340. static void __exit cosm_exit(void)
  341. {
  342. cosm_unregister_driver(&cosm_driver);
  343. ida_destroy(&g_cosm_ida);
  344. class_destroy(g_cosm_class);
  345. cosm_exit_debugfs();
  346. }
  347. module_init(cosm_init);
  348. module_exit(cosm_exit);
  349. MODULE_AUTHOR("Intel Corporation");
  350. MODULE_DESCRIPTION("Intel(R) MIC Coprocessor State Management (COSM) Driver");
  351. MODULE_LICENSE("GPL v2");