mic_x100.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * Disclaimer: The codes contained in these modules may be specific to
  19. * the Intel Software Development Platform codenamed: Knights Ferry, and
  20. * the Intel product codenamed: Knights Corner, and are not backward
  21. * compatible with other Intel products. Additionally, Intel will NOT
  22. * support the codes or instruction set in future products.
  23. *
  24. * Intel MIC Card driver.
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/platform_device.h>
  30. #include "../common/mic_dev.h"
  31. #include "mic_device.h"
  32. #include "mic_x100.h"
  33. static const char mic_driver_name[] = "mic";
  34. static struct mic_driver g_drv;
  35. /**
  36. * mic_read_spad - read from the scratchpad register
  37. * @mdev: pointer to mic_device instance
  38. * @idx: index to scratchpad register, 0 based
  39. *
  40. * This function allows reading of the 32bit scratchpad register.
  41. *
  42. * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
  43. */
  44. u32 mic_read_spad(struct mic_device *mdev, unsigned int idx)
  45. {
  46. return mic_mmio_read(&mdev->mmio,
  47. MIC_X100_SBOX_BASE_ADDRESS +
  48. MIC_X100_SBOX_SPAD0 + idx * 4);
  49. }
  50. /**
  51. * __mic_send_intr - Send interrupt to Host.
  52. * @mdev: pointer to mic_device instance
  53. * @doorbell: Doorbell number.
  54. */
  55. void mic_send_intr(struct mic_device *mdev, int doorbell)
  56. {
  57. struct mic_mw *mw = &mdev->mmio;
  58. if (doorbell > MIC_X100_MAX_DOORBELL_IDX)
  59. return;
  60. /* Ensure that the interrupt is ordered w.r.t previous stores. */
  61. wmb();
  62. mic_mmio_write(mw, MIC_X100_SBOX_SDBIC0_DBREQ_BIT,
  63. MIC_X100_SBOX_BASE_ADDRESS +
  64. (MIC_X100_SBOX_SDBIC0 + (4 * doorbell)));
  65. }
  66. /*
  67. * mic_x100_send_sbox_intr - Send an MIC_X100_SBOX interrupt to MIC.
  68. */
  69. static void mic_x100_send_sbox_intr(struct mic_mw *mw, int doorbell)
  70. {
  71. u64 apic_icr_offset = MIC_X100_SBOX_APICICR0 + doorbell * 8;
  72. u32 apicicr_low = mic_mmio_read(mw, MIC_X100_SBOX_BASE_ADDRESS +
  73. apic_icr_offset);
  74. /* for MIC we need to make sure we "hit" the send_icr bit (13) */
  75. apicicr_low = (apicicr_low | (1 << 13));
  76. /*
  77. * Ensure that the interrupt is ordered w.r.t. previous stores
  78. * to main memory. Fence instructions are not implemented in X100
  79. * since execution is in order but a compiler barrier is still
  80. * required.
  81. */
  82. wmb();
  83. mic_mmio_write(mw, apicicr_low,
  84. MIC_X100_SBOX_BASE_ADDRESS + apic_icr_offset);
  85. }
  86. static void mic_x100_send_rdmasr_intr(struct mic_mw *mw, int doorbell)
  87. {
  88. int rdmasr_offset = MIC_X100_SBOX_RDMASR0 + (doorbell << 2);
  89. /*
  90. * Ensure that the interrupt is ordered w.r.t. previous stores
  91. * to main memory. Fence instructions are not implemented in X100
  92. * since execution is in order but a compiler barrier is still
  93. * required.
  94. */
  95. wmb();
  96. mic_mmio_write(mw, 0, MIC_X100_SBOX_BASE_ADDRESS + rdmasr_offset);
  97. }
  98. /**
  99. * mic_ack_interrupt - Device specific interrupt handling.
  100. * @mdev: pointer to mic_device instance
  101. *
  102. * Returns: bitmask of doorbell events triggered.
  103. */
  104. u32 mic_ack_interrupt(struct mic_device *mdev)
  105. {
  106. return 0;
  107. }
  108. static inline int mic_get_sbox_irq(int db)
  109. {
  110. return MIC_X100_IRQ_BASE + db;
  111. }
  112. static inline int mic_get_rdmasr_irq(int index)
  113. {
  114. return MIC_X100_RDMASR_IRQ_BASE + index;
  115. }
  116. void mic_send_p2p_intr(int db, struct mic_mw *mw)
  117. {
  118. int rdmasr_index;
  119. if (db < MIC_X100_NUM_SBOX_IRQ) {
  120. mic_x100_send_sbox_intr(mw, db);
  121. } else {
  122. rdmasr_index = db - MIC_X100_NUM_SBOX_IRQ;
  123. mic_x100_send_rdmasr_intr(mw, rdmasr_index);
  124. }
  125. }
  126. /**
  127. * mic_hw_intr_init - Initialize h/w specific interrupt
  128. * information.
  129. * @mdrv: pointer to mic_driver
  130. */
  131. void mic_hw_intr_init(struct mic_driver *mdrv)
  132. {
  133. mdrv->intr_info.num_intr = MIC_X100_NUM_SBOX_IRQ +
  134. MIC_X100_NUM_RDMASR_IRQ;
  135. }
  136. /**
  137. * mic_db_to_irq - Retrieve irq number corresponding to a doorbell.
  138. * @mdrv: pointer to mic_driver
  139. * @db: The doorbell obtained for which the irq is needed. Doorbell
  140. * may correspond to an sbox doorbell or an rdmasr index.
  141. *
  142. * Returns the irq corresponding to the doorbell.
  143. */
  144. int mic_db_to_irq(struct mic_driver *mdrv, int db)
  145. {
  146. int rdmasr_index;
  147. /*
  148. * The total number of doorbell interrupts on the card are 16. Indices
  149. * 0-8 falls in the SBOX category and 8-15 fall in the RDMASR category.
  150. */
  151. if (db < MIC_X100_NUM_SBOX_IRQ) {
  152. return mic_get_sbox_irq(db);
  153. } else {
  154. rdmasr_index = db - MIC_X100_NUM_SBOX_IRQ;
  155. return mic_get_rdmasr_irq(rdmasr_index);
  156. }
  157. }
  158. /*
  159. * mic_card_map - Allocate virtual address for a remote memory region.
  160. * @mdev: pointer to mic_device instance.
  161. * @addr: Remote DMA address.
  162. * @size: Size of the region.
  163. *
  164. * Returns: Virtual address backing the remote memory region.
  165. */
  166. void __iomem *
  167. mic_card_map(struct mic_device *mdev, dma_addr_t addr, size_t size)
  168. {
  169. return ioremap(addr, size);
  170. }
  171. /*
  172. * mic_card_unmap - Unmap the virtual address for a remote memory region.
  173. * @mdev: pointer to mic_device instance.
  174. * @addr: Virtual address for remote memory region.
  175. *
  176. * Returns: None.
  177. */
  178. void mic_card_unmap(struct mic_device *mdev, void __iomem *addr)
  179. {
  180. iounmap(addr);
  181. }
  182. static inline struct mic_driver *mbdev_to_mdrv(struct mbus_device *mbdev)
  183. {
  184. return dev_get_drvdata(mbdev->dev.parent);
  185. }
  186. static struct mic_irq *
  187. _mic_request_threaded_irq(struct mbus_device *mbdev,
  188. irq_handler_t handler, irq_handler_t thread_fn,
  189. const char *name, void *data, int intr_src)
  190. {
  191. int rc = 0;
  192. unsigned int irq = intr_src;
  193. unsigned long cookie = irq;
  194. rc = request_threaded_irq(irq, handler, thread_fn, 0, name, data);
  195. if (rc) {
  196. dev_err(mbdev_to_mdrv(mbdev)->dev,
  197. "request_threaded_irq failed rc = %d\n", rc);
  198. return ERR_PTR(rc);
  199. }
  200. return (struct mic_irq *)cookie;
  201. }
  202. static void _mic_free_irq(struct mbus_device *mbdev,
  203. struct mic_irq *cookie, void *data)
  204. {
  205. unsigned long irq = (unsigned long)cookie;
  206. free_irq(irq, data);
  207. }
  208. static void _mic_ack_interrupt(struct mbus_device *mbdev, int num)
  209. {
  210. mic_ack_interrupt(&mbdev_to_mdrv(mbdev)->mdev);
  211. }
  212. static struct mbus_hw_ops mbus_hw_ops = {
  213. .request_threaded_irq = _mic_request_threaded_irq,
  214. .free_irq = _mic_free_irq,
  215. .ack_interrupt = _mic_ack_interrupt,
  216. };
  217. static int __init mic_probe(struct platform_device *pdev)
  218. {
  219. struct mic_driver *mdrv = &g_drv;
  220. struct mic_device *mdev = &mdrv->mdev;
  221. int rc = 0;
  222. mdrv->dev = &pdev->dev;
  223. snprintf(mdrv->name, sizeof(mic_driver_name), mic_driver_name);
  224. /* FIXME: use dma_set_mask_and_coherent() and check result */
  225. dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  226. mdev->mmio.pa = MIC_X100_MMIO_BASE;
  227. mdev->mmio.len = MIC_X100_MMIO_LEN;
  228. mdev->mmio.va = devm_ioremap(&pdev->dev, MIC_X100_MMIO_BASE,
  229. MIC_X100_MMIO_LEN);
  230. if (!mdev->mmio.va) {
  231. dev_err(&pdev->dev, "Cannot remap MMIO BAR\n");
  232. rc = -EIO;
  233. goto done;
  234. }
  235. mic_hw_intr_init(mdrv);
  236. platform_set_drvdata(pdev, mdrv);
  237. mdrv->dma_mbdev = mbus_register_device(mdrv->dev, MBUS_DEV_DMA_MIC,
  238. NULL, &mbus_hw_ops, 0,
  239. mdrv->mdev.mmio.va);
  240. if (IS_ERR(mdrv->dma_mbdev)) {
  241. rc = PTR_ERR(mdrv->dma_mbdev);
  242. dev_err(&pdev->dev, "mbus_add_device failed rc %d\n", rc);
  243. goto done;
  244. }
  245. rc = mic_driver_init(mdrv);
  246. if (rc) {
  247. dev_err(&pdev->dev, "mic_driver_init failed rc %d\n", rc);
  248. goto remove_dma;
  249. }
  250. done:
  251. return rc;
  252. remove_dma:
  253. mbus_unregister_device(mdrv->dma_mbdev);
  254. return rc;
  255. }
  256. static int mic_remove(struct platform_device *pdev)
  257. {
  258. struct mic_driver *mdrv = &g_drv;
  259. mic_driver_uninit(mdrv);
  260. mbus_unregister_device(mdrv->dma_mbdev);
  261. return 0;
  262. }
  263. static void mic_platform_shutdown(struct platform_device *pdev)
  264. {
  265. mic_remove(pdev);
  266. }
  267. static struct platform_driver __refdata mic_platform_driver = {
  268. .probe = mic_probe,
  269. .remove = mic_remove,
  270. .shutdown = mic_platform_shutdown,
  271. .driver = {
  272. .name = mic_driver_name,
  273. },
  274. };
  275. static struct platform_device *mic_platform_dev;
  276. static int __init mic_init(void)
  277. {
  278. int ret;
  279. struct cpuinfo_x86 *c = &cpu_data(0);
  280. if (!(c->x86 == 11 && c->x86_model == 1)) {
  281. ret = -ENODEV;
  282. pr_err("%s not running on X100 ret %d\n", __func__, ret);
  283. goto done;
  284. }
  285. request_module("mic_x100_dma");
  286. mic_init_card_debugfs();
  287. mic_platform_dev = platform_device_register_simple(mic_driver_name,
  288. 0, NULL, 0);
  289. ret = PTR_ERR_OR_ZERO(mic_platform_dev);
  290. if (ret) {
  291. pr_err("platform_device_register_full ret %d\n", ret);
  292. goto cleanup_debugfs;
  293. }
  294. ret = platform_driver_register(&mic_platform_driver);
  295. if (ret) {
  296. pr_err("platform_driver_register ret %d\n", ret);
  297. goto device_unregister;
  298. }
  299. return ret;
  300. device_unregister:
  301. platform_device_unregister(mic_platform_dev);
  302. cleanup_debugfs:
  303. mic_exit_card_debugfs();
  304. done:
  305. return ret;
  306. }
  307. static void __exit mic_exit(void)
  308. {
  309. platform_driver_unregister(&mic_platform_driver);
  310. platform_device_unregister(mic_platform_dev);
  311. mic_exit_card_debugfs();
  312. }
  313. module_init(mic_init);
  314. module_exit(mic_exit);
  315. MODULE_AUTHOR("Intel Corporation");
  316. MODULE_DESCRIPTION("Intel(R) MIC X100 Card driver");
  317. MODULE_LICENSE("GPL v2");