mic_boot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2013 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 Host driver.
  19. *
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/firmware.h>
  23. #include <linux/pci.h>
  24. #include <linux/kmod.h>
  25. #include <linux/mic_common.h>
  26. #include <linux/mic_bus.h>
  27. #include "../bus/scif_bus.h"
  28. #include "../common/mic_dev.h"
  29. #include "mic_device.h"
  30. #include "mic_smpt.h"
  31. #include "mic_virtio.h"
  32. static inline struct mic_device *scdev_to_mdev(struct scif_hw_dev *scdev)
  33. {
  34. return dev_get_drvdata(scdev->dev.parent);
  35. }
  36. static void *__mic_dma_alloc(struct device *dev, size_t size,
  37. dma_addr_t *dma_handle, gfp_t gfp,
  38. struct dma_attrs *attrs)
  39. {
  40. struct scif_hw_dev *scdev = dev_get_drvdata(dev);
  41. struct mic_device *mdev = scdev_to_mdev(scdev);
  42. dma_addr_t tmp;
  43. void *va = kmalloc(size, gfp);
  44. if (va) {
  45. tmp = mic_map_single(mdev, va, size);
  46. if (dma_mapping_error(dev, tmp)) {
  47. kfree(va);
  48. va = NULL;
  49. } else {
  50. *dma_handle = tmp;
  51. }
  52. }
  53. return va;
  54. }
  55. static void __mic_dma_free(struct device *dev, size_t size, void *vaddr,
  56. dma_addr_t dma_handle, struct dma_attrs *attrs)
  57. {
  58. struct scif_hw_dev *scdev = dev_get_drvdata(dev);
  59. struct mic_device *mdev = scdev_to_mdev(scdev);
  60. mic_unmap_single(mdev, dma_handle, size);
  61. kfree(vaddr);
  62. }
  63. static dma_addr_t
  64. __mic_dma_map_page(struct device *dev, struct page *page, unsigned long offset,
  65. size_t size, enum dma_data_direction dir,
  66. struct dma_attrs *attrs)
  67. {
  68. void *va = phys_to_virt(page_to_phys(page)) + offset;
  69. struct scif_hw_dev *scdev = dev_get_drvdata(dev);
  70. struct mic_device *mdev = scdev_to_mdev(scdev);
  71. return mic_map_single(mdev, va, size);
  72. }
  73. static void
  74. __mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  75. size_t size, enum dma_data_direction dir,
  76. struct dma_attrs *attrs)
  77. {
  78. struct scif_hw_dev *scdev = dev_get_drvdata(dev);
  79. struct mic_device *mdev = scdev_to_mdev(scdev);
  80. mic_unmap_single(mdev, dma_addr, size);
  81. }
  82. static int __mic_dma_map_sg(struct device *dev, struct scatterlist *sg,
  83. int nents, enum dma_data_direction dir,
  84. struct dma_attrs *attrs)
  85. {
  86. struct scif_hw_dev *scdev = dev_get_drvdata(dev);
  87. struct mic_device *mdev = scdev_to_mdev(scdev);
  88. struct scatterlist *s;
  89. int i, j, ret;
  90. dma_addr_t da;
  91. ret = dma_map_sg(&mdev->pdev->dev, sg, nents, dir);
  92. if (ret <= 0)
  93. return 0;
  94. for_each_sg(sg, s, nents, i) {
  95. da = mic_map(mdev, sg_dma_address(s) + s->offset, s->length);
  96. if (!da)
  97. goto err;
  98. sg_dma_address(s) = da;
  99. }
  100. return nents;
  101. err:
  102. for_each_sg(sg, s, i, j) {
  103. mic_unmap(mdev, sg_dma_address(s), s->length);
  104. sg_dma_address(s) = mic_to_dma_addr(mdev, sg_dma_address(s));
  105. }
  106. dma_unmap_sg(&mdev->pdev->dev, sg, nents, dir);
  107. return 0;
  108. }
  109. static void __mic_dma_unmap_sg(struct device *dev,
  110. struct scatterlist *sg, int nents,
  111. enum dma_data_direction dir,
  112. struct dma_attrs *attrs)
  113. {
  114. struct scif_hw_dev *scdev = dev_get_drvdata(dev);
  115. struct mic_device *mdev = scdev_to_mdev(scdev);
  116. struct scatterlist *s;
  117. dma_addr_t da;
  118. int i;
  119. for_each_sg(sg, s, nents, i) {
  120. da = mic_to_dma_addr(mdev, sg_dma_address(s));
  121. mic_unmap(mdev, sg_dma_address(s), s->length);
  122. sg_dma_address(s) = da;
  123. }
  124. dma_unmap_sg(&mdev->pdev->dev, sg, nents, dir);
  125. }
  126. static struct dma_map_ops __mic_dma_ops = {
  127. .alloc = __mic_dma_alloc,
  128. .free = __mic_dma_free,
  129. .map_page = __mic_dma_map_page,
  130. .unmap_page = __mic_dma_unmap_page,
  131. .map_sg = __mic_dma_map_sg,
  132. .unmap_sg = __mic_dma_unmap_sg,
  133. };
  134. static struct mic_irq *
  135. ___mic_request_irq(struct scif_hw_dev *scdev,
  136. irqreturn_t (*func)(int irq, void *data),
  137. const char *name,
  138. void *data, int db)
  139. {
  140. struct mic_device *mdev = scdev_to_mdev(scdev);
  141. return mic_request_threaded_irq(mdev, func, NULL, name, data,
  142. db, MIC_INTR_DB);
  143. }
  144. static void
  145. ___mic_free_irq(struct scif_hw_dev *scdev,
  146. struct mic_irq *cookie, void *data)
  147. {
  148. struct mic_device *mdev = scdev_to_mdev(scdev);
  149. return mic_free_irq(mdev, cookie, data);
  150. }
  151. static void ___mic_ack_interrupt(struct scif_hw_dev *scdev, int num)
  152. {
  153. struct mic_device *mdev = scdev_to_mdev(scdev);
  154. mdev->ops->intr_workarounds(mdev);
  155. }
  156. static int ___mic_next_db(struct scif_hw_dev *scdev)
  157. {
  158. struct mic_device *mdev = scdev_to_mdev(scdev);
  159. return mic_next_db(mdev);
  160. }
  161. static void ___mic_send_intr(struct scif_hw_dev *scdev, int db)
  162. {
  163. struct mic_device *mdev = scdev_to_mdev(scdev);
  164. mdev->ops->send_intr(mdev, db);
  165. }
  166. static void __iomem *___mic_ioremap(struct scif_hw_dev *scdev,
  167. phys_addr_t pa, size_t len)
  168. {
  169. struct mic_device *mdev = scdev_to_mdev(scdev);
  170. return mdev->aper.va + pa;
  171. }
  172. static void ___mic_iounmap(struct scif_hw_dev *scdev, void __iomem *va)
  173. {
  174. /* nothing to do */
  175. }
  176. static struct scif_hw_ops scif_hw_ops = {
  177. .request_irq = ___mic_request_irq,
  178. .free_irq = ___mic_free_irq,
  179. .ack_interrupt = ___mic_ack_interrupt,
  180. .next_db = ___mic_next_db,
  181. .send_intr = ___mic_send_intr,
  182. .ioremap = ___mic_ioremap,
  183. .iounmap = ___mic_iounmap,
  184. };
  185. static inline struct mic_device *mbdev_to_mdev(struct mbus_device *mbdev)
  186. {
  187. return dev_get_drvdata(mbdev->dev.parent);
  188. }
  189. static dma_addr_t
  190. mic_dma_map_page(struct device *dev, struct page *page,
  191. unsigned long offset, size_t size, enum dma_data_direction dir,
  192. struct dma_attrs *attrs)
  193. {
  194. void *va = phys_to_virt(page_to_phys(page)) + offset;
  195. struct mic_device *mdev = dev_get_drvdata(dev->parent);
  196. return mic_map_single(mdev, va, size);
  197. }
  198. static void
  199. mic_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
  200. size_t size, enum dma_data_direction dir,
  201. struct dma_attrs *attrs)
  202. {
  203. struct mic_device *mdev = dev_get_drvdata(dev->parent);
  204. mic_unmap_single(mdev, dma_addr, size);
  205. }
  206. static struct dma_map_ops mic_dma_ops = {
  207. .map_page = mic_dma_map_page,
  208. .unmap_page = mic_dma_unmap_page,
  209. };
  210. static struct mic_irq *
  211. _mic_request_threaded_irq(struct mbus_device *mbdev,
  212. irq_handler_t handler, irq_handler_t thread_fn,
  213. const char *name, void *data, int intr_src)
  214. {
  215. return mic_request_threaded_irq(mbdev_to_mdev(mbdev), handler,
  216. thread_fn, name, data,
  217. intr_src, MIC_INTR_DMA);
  218. }
  219. static void _mic_free_irq(struct mbus_device *mbdev,
  220. struct mic_irq *cookie, void *data)
  221. {
  222. return mic_free_irq(mbdev_to_mdev(mbdev), cookie, data);
  223. }
  224. static void _mic_ack_interrupt(struct mbus_device *mbdev, int num)
  225. {
  226. struct mic_device *mdev = mbdev_to_mdev(mbdev);
  227. mdev->ops->intr_workarounds(mdev);
  228. }
  229. static struct mbus_hw_ops mbus_hw_ops = {
  230. .request_threaded_irq = _mic_request_threaded_irq,
  231. .free_irq = _mic_free_irq,
  232. .ack_interrupt = _mic_ack_interrupt,
  233. };
  234. /* Initialize the MIC bootparams */
  235. void mic_bootparam_init(struct mic_device *mdev)
  236. {
  237. struct mic_bootparam *bootparam = mdev->dp;
  238. bootparam->magic = cpu_to_le32(MIC_MAGIC);
  239. bootparam->h2c_config_db = -1;
  240. bootparam->node_id = mdev->id + 1;
  241. bootparam->scif_host_dma_addr = 0x0;
  242. bootparam->scif_card_dma_addr = 0x0;
  243. bootparam->c2h_scif_db = -1;
  244. bootparam->h2c_scif_db = -1;
  245. }
  246. static inline struct mic_device *cosmdev_to_mdev(struct cosm_device *cdev)
  247. {
  248. return dev_get_drvdata(cdev->dev.parent);
  249. }
  250. static void _mic_reset(struct cosm_device *cdev)
  251. {
  252. struct mic_device *mdev = cosmdev_to_mdev(cdev);
  253. mdev->ops->reset_fw_ready(mdev);
  254. mdev->ops->reset(mdev);
  255. }
  256. static bool _mic_ready(struct cosm_device *cdev)
  257. {
  258. struct mic_device *mdev = cosmdev_to_mdev(cdev);
  259. return mdev->ops->is_fw_ready(mdev);
  260. }
  261. /**
  262. * mic_request_dma_chans - Request DMA channels
  263. * @mdev: pointer to mic_device instance
  264. *
  265. * returns number of DMA channels acquired
  266. */
  267. static int mic_request_dma_chans(struct mic_device *mdev)
  268. {
  269. dma_cap_mask_t mask;
  270. struct dma_chan *chan;
  271. request_module("mic_x100_dma");
  272. dma_cap_zero(mask);
  273. dma_cap_set(DMA_MEMCPY, mask);
  274. do {
  275. chan = dma_request_channel(mask, mdev->ops->dma_filter,
  276. &mdev->pdev->dev);
  277. if (chan) {
  278. mdev->dma_ch[mdev->num_dma_ch++] = chan;
  279. if (mdev->num_dma_ch >= MIC_MAX_DMA_CHAN)
  280. break;
  281. }
  282. } while (chan);
  283. dev_info(&mdev->pdev->dev, "DMA channels # %d\n", mdev->num_dma_ch);
  284. return mdev->num_dma_ch;
  285. }
  286. /**
  287. * mic_free_dma_chans - release DMA channels
  288. * @mdev: pointer to mic_device instance
  289. *
  290. * returns none
  291. */
  292. static void mic_free_dma_chans(struct mic_device *mdev)
  293. {
  294. int i = 0;
  295. for (i = 0; i < mdev->num_dma_ch; i++) {
  296. dma_release_channel(mdev->dma_ch[i]);
  297. mdev->dma_ch[i] = NULL;
  298. }
  299. mdev->num_dma_ch = 0;
  300. }
  301. /**
  302. * _mic_start - Start the MIC.
  303. * @cdev: pointer to cosm_device instance
  304. * @id: MIC device id/index provided by COSM used in other drivers like SCIF
  305. *
  306. * This function prepares an MIC for boot and initiates boot.
  307. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  308. *
  309. * For all cosm_hw_ops the caller holds a mutex to ensure serialization.
  310. */
  311. static int _mic_start(struct cosm_device *cdev, int id)
  312. {
  313. struct mic_device *mdev = cosmdev_to_mdev(cdev);
  314. int rc;
  315. mic_bootparam_init(mdev);
  316. mdev->dma_mbdev = mbus_register_device(&mdev->pdev->dev,
  317. MBUS_DEV_DMA_HOST, &mic_dma_ops,
  318. &mbus_hw_ops, id, mdev->mmio.va);
  319. if (IS_ERR(mdev->dma_mbdev)) {
  320. rc = PTR_ERR(mdev->dma_mbdev);
  321. goto unlock_ret;
  322. }
  323. if (!mic_request_dma_chans(mdev)) {
  324. rc = -ENODEV;
  325. goto dma_remove;
  326. }
  327. mdev->scdev = scif_register_device(&mdev->pdev->dev, MIC_SCIF_DEV,
  328. &__mic_dma_ops, &scif_hw_ops,
  329. id + 1, 0, &mdev->mmio,
  330. &mdev->aper, mdev->dp, NULL,
  331. mdev->dma_ch, mdev->num_dma_ch,
  332. true);
  333. if (IS_ERR(mdev->scdev)) {
  334. rc = PTR_ERR(mdev->scdev);
  335. goto dma_free;
  336. }
  337. rc = mdev->ops->load_mic_fw(mdev, NULL);
  338. if (rc)
  339. goto scif_remove;
  340. mic_smpt_restore(mdev);
  341. mic_intr_restore(mdev);
  342. mdev->intr_ops->enable_interrupts(mdev);
  343. mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
  344. mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
  345. mdev->ops->send_firmware_intr(mdev);
  346. goto unlock_ret;
  347. scif_remove:
  348. scif_unregister_device(mdev->scdev);
  349. dma_free:
  350. mic_free_dma_chans(mdev);
  351. dma_remove:
  352. mbus_unregister_device(mdev->dma_mbdev);
  353. unlock_ret:
  354. return rc;
  355. }
  356. /**
  357. * _mic_stop - Prepare the MIC for reset and trigger reset.
  358. * @cdev: pointer to cosm_device instance
  359. * @force: force a MIC to reset even if it is already offline.
  360. *
  361. * RETURNS: None.
  362. */
  363. static void _mic_stop(struct cosm_device *cdev, bool force)
  364. {
  365. struct mic_device *mdev = cosmdev_to_mdev(cdev);
  366. /*
  367. * Since SCIF handles card shutdown and reset (using COSM), it will
  368. * will be the first to be registered and the last to be
  369. * unregistered.
  370. */
  371. mic_virtio_reset_devices(mdev);
  372. scif_unregister_device(mdev->scdev);
  373. mic_free_dma_chans(mdev);
  374. mbus_unregister_device(mdev->dma_mbdev);
  375. mic_bootparam_init(mdev);
  376. }
  377. static ssize_t _mic_family(struct cosm_device *cdev, char *buf)
  378. {
  379. struct mic_device *mdev = cosmdev_to_mdev(cdev);
  380. static const char *family[MIC_FAMILY_LAST] = { "x100", "Unknown" };
  381. return scnprintf(buf, PAGE_SIZE, "%s\n", family[mdev->family]);
  382. }
  383. static ssize_t _mic_stepping(struct cosm_device *cdev, char *buf)
  384. {
  385. struct mic_device *mdev = cosmdev_to_mdev(cdev);
  386. const char *string = "??";
  387. switch (mdev->stepping) {
  388. case MIC_A0_STEP:
  389. string = "A0";
  390. break;
  391. case MIC_B0_STEP:
  392. string = "B0";
  393. break;
  394. case MIC_B1_STEP:
  395. string = "B1";
  396. break;
  397. case MIC_C0_STEP:
  398. string = "C0";
  399. break;
  400. default:
  401. break;
  402. }
  403. return scnprintf(buf, PAGE_SIZE, "%s\n", string);
  404. }
  405. static struct mic_mw *_mic_aper(struct cosm_device *cdev)
  406. {
  407. struct mic_device *mdev = cosmdev_to_mdev(cdev);
  408. return &mdev->aper;
  409. }
  410. struct cosm_hw_ops cosm_hw_ops = {
  411. .reset = _mic_reset,
  412. .force_reset = _mic_reset,
  413. .post_reset = NULL,
  414. .ready = _mic_ready,
  415. .start = _mic_start,
  416. .stop = _mic_stop,
  417. .family = _mic_family,
  418. .stepping = _mic_stepping,
  419. .aper = _mic_aper,
  420. };