mic_boot.c 15 KB

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