ntb_hw_amd.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright (C) 2016 Advanced Micro Devices, Inc. All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * BSD LICENSE
  14. *
  15. * Copyright (C) 2016 Advanced Micro Devices, Inc. All Rights Reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * * Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * * Redistributions in binary form must reproduce the above copy
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * * Neither the name of AMD Corporation nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  39. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  41. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * AMD PCIe NTB Linux driver
  44. *
  45. * Contact Information:
  46. * Xiangliang Yu <Xiangliang.Yu@amd.com>
  47. */
  48. #include <linux/debugfs.h>
  49. #include <linux/delay.h>
  50. #include <linux/init.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/module.h>
  53. #include <linux/acpi.h>
  54. #include <linux/pci.h>
  55. #include <linux/random.h>
  56. #include <linux/slab.h>
  57. #include <linux/ntb.h>
  58. #include "ntb_hw_amd.h"
  59. #define NTB_NAME "ntb_hw_amd"
  60. #define NTB_DESC "AMD(R) PCI-E Non-Transparent Bridge Driver"
  61. #define NTB_VER "1.0"
  62. MODULE_DESCRIPTION(NTB_DESC);
  63. MODULE_VERSION(NTB_VER);
  64. MODULE_LICENSE("Dual BSD/GPL");
  65. MODULE_AUTHOR("AMD Inc.");
  66. static const struct file_operations amd_ntb_debugfs_info;
  67. static struct dentry *debugfs_dir;
  68. static int ndev_mw_to_bar(struct amd_ntb_dev *ndev, int idx)
  69. {
  70. if (idx < 0 || idx > ndev->mw_count)
  71. return -EINVAL;
  72. return 1 << idx;
  73. }
  74. static int amd_ntb_mw_count(struct ntb_dev *ntb)
  75. {
  76. return ntb_ndev(ntb)->mw_count;
  77. }
  78. static int amd_ntb_mw_get_range(struct ntb_dev *ntb, int idx,
  79. phys_addr_t *base,
  80. resource_size_t *size,
  81. resource_size_t *align,
  82. resource_size_t *align_size)
  83. {
  84. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  85. int bar;
  86. bar = ndev_mw_to_bar(ndev, idx);
  87. if (bar < 0)
  88. return bar;
  89. if (base)
  90. *base = pci_resource_start(ndev->ntb.pdev, bar);
  91. if (size)
  92. *size = pci_resource_len(ndev->ntb.pdev, bar);
  93. if (align)
  94. *align = SZ_4K;
  95. if (align_size)
  96. *align_size = 1;
  97. return 0;
  98. }
  99. static int amd_ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
  100. dma_addr_t addr, resource_size_t size)
  101. {
  102. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  103. unsigned long xlat_reg, limit_reg = 0;
  104. resource_size_t mw_size;
  105. void __iomem *mmio, *peer_mmio;
  106. u64 base_addr, limit, reg_val;
  107. int bar;
  108. bar = ndev_mw_to_bar(ndev, idx);
  109. if (bar < 0)
  110. return bar;
  111. mw_size = pci_resource_len(ndev->ntb.pdev, bar);
  112. /* make sure the range fits in the usable mw size */
  113. if (size > mw_size)
  114. return -EINVAL;
  115. mmio = ndev->self_mmio;
  116. peer_mmio = ndev->peer_mmio;
  117. base_addr = pci_resource_start(ndev->ntb.pdev, bar);
  118. if (bar != 1) {
  119. xlat_reg = AMD_BAR23XLAT_OFFSET + ((bar - 2) << 2);
  120. limit_reg = AMD_BAR23LMT_OFFSET + ((bar - 2) << 2);
  121. /* Set the limit if supported */
  122. limit = size;
  123. /* set and verify setting the translation address */
  124. write64(addr, peer_mmio + xlat_reg);
  125. reg_val = read64(peer_mmio + xlat_reg);
  126. if (reg_val != addr) {
  127. write64(0, peer_mmio + xlat_reg);
  128. return -EIO;
  129. }
  130. /* set and verify setting the limit */
  131. write64(limit, mmio + limit_reg);
  132. reg_val = read64(mmio + limit_reg);
  133. if (reg_val != limit) {
  134. write64(base_addr, mmio + limit_reg);
  135. write64(0, peer_mmio + xlat_reg);
  136. return -EIO;
  137. }
  138. } else {
  139. xlat_reg = AMD_BAR1XLAT_OFFSET;
  140. limit_reg = AMD_BAR1LMT_OFFSET;
  141. /* Set the limit if supported */
  142. limit = size;
  143. /* set and verify setting the translation address */
  144. write64(addr, peer_mmio + xlat_reg);
  145. reg_val = read64(peer_mmio + xlat_reg);
  146. if (reg_val != addr) {
  147. write64(0, peer_mmio + xlat_reg);
  148. return -EIO;
  149. }
  150. /* set and verify setting the limit */
  151. writel(limit, mmio + limit_reg);
  152. reg_val = readl(mmio + limit_reg);
  153. if (reg_val != limit) {
  154. writel(base_addr, mmio + limit_reg);
  155. writel(0, peer_mmio + xlat_reg);
  156. return -EIO;
  157. }
  158. }
  159. return 0;
  160. }
  161. static int amd_link_is_up(struct amd_ntb_dev *ndev)
  162. {
  163. if (!ndev->peer_sta)
  164. return NTB_LNK_STA_ACTIVE(ndev->cntl_sta);
  165. if (ndev->peer_sta & AMD_LINK_UP_EVENT) {
  166. ndev->peer_sta = 0;
  167. return 1;
  168. }
  169. /* If peer_sta is reset or D0 event, the ISR has
  170. * started a timer to check link status of hardware.
  171. * So here just clear status bit. And if peer_sta is
  172. * D3 or PME_TO, D0/reset event will be happened when
  173. * system wakeup/poweron, so do nothing here.
  174. */
  175. if (ndev->peer_sta & AMD_PEER_RESET_EVENT)
  176. ndev->peer_sta &= ~AMD_PEER_RESET_EVENT;
  177. else if (ndev->peer_sta & (AMD_PEER_D0_EVENT | AMD_LINK_DOWN_EVENT))
  178. ndev->peer_sta = 0;
  179. return 0;
  180. }
  181. static int amd_ntb_link_is_up(struct ntb_dev *ntb,
  182. enum ntb_speed *speed,
  183. enum ntb_width *width)
  184. {
  185. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  186. int ret = 0;
  187. if (amd_link_is_up(ndev)) {
  188. if (speed)
  189. *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
  190. if (width)
  191. *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
  192. dev_dbg(ndev_dev(ndev), "link is up.\n");
  193. ret = 1;
  194. } else {
  195. if (speed)
  196. *speed = NTB_SPEED_NONE;
  197. if (width)
  198. *width = NTB_WIDTH_NONE;
  199. dev_dbg(ndev_dev(ndev), "link is down.\n");
  200. }
  201. return ret;
  202. }
  203. static int amd_ntb_link_enable(struct ntb_dev *ntb,
  204. enum ntb_speed max_speed,
  205. enum ntb_width max_width)
  206. {
  207. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  208. void __iomem *mmio = ndev->self_mmio;
  209. u32 ntb_ctl;
  210. /* Enable event interrupt */
  211. ndev->int_mask &= ~AMD_EVENT_INTMASK;
  212. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  213. if (ndev->ntb.topo == NTB_TOPO_SEC)
  214. return -EINVAL;
  215. dev_dbg(ndev_dev(ndev), "Enabling Link.\n");
  216. ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
  217. ntb_ctl |= (PMM_REG_CTL | SMM_REG_CTL);
  218. writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
  219. return 0;
  220. }
  221. static int amd_ntb_link_disable(struct ntb_dev *ntb)
  222. {
  223. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  224. void __iomem *mmio = ndev->self_mmio;
  225. u32 ntb_ctl;
  226. /* Disable event interrupt */
  227. ndev->int_mask |= AMD_EVENT_INTMASK;
  228. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  229. if (ndev->ntb.topo == NTB_TOPO_SEC)
  230. return -EINVAL;
  231. dev_dbg(ndev_dev(ndev), "Enabling Link.\n");
  232. ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
  233. ntb_ctl &= ~(PMM_REG_CTL | SMM_REG_CTL);
  234. writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
  235. return 0;
  236. }
  237. static u64 amd_ntb_db_valid_mask(struct ntb_dev *ntb)
  238. {
  239. return ntb_ndev(ntb)->db_valid_mask;
  240. }
  241. static int amd_ntb_db_vector_count(struct ntb_dev *ntb)
  242. {
  243. return ntb_ndev(ntb)->db_count;
  244. }
  245. static u64 amd_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
  246. {
  247. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  248. if (db_vector < 0 || db_vector > ndev->db_count)
  249. return 0;
  250. return ntb_ndev(ntb)->db_valid_mask & (1 << db_vector);
  251. }
  252. static u64 amd_ntb_db_read(struct ntb_dev *ntb)
  253. {
  254. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  255. void __iomem *mmio = ndev->self_mmio;
  256. return (u64)readw(mmio + AMD_DBSTAT_OFFSET);
  257. }
  258. static int amd_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
  259. {
  260. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  261. void __iomem *mmio = ndev->self_mmio;
  262. writew((u16)db_bits, mmio + AMD_DBSTAT_OFFSET);
  263. return 0;
  264. }
  265. static int amd_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  266. {
  267. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  268. void __iomem *mmio = ndev->self_mmio;
  269. unsigned long flags;
  270. if (db_bits & ~ndev->db_valid_mask)
  271. return -EINVAL;
  272. spin_lock_irqsave(&ndev->db_mask_lock, flags);
  273. ndev->db_mask |= db_bits;
  274. writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  275. spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
  276. return 0;
  277. }
  278. static int amd_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  279. {
  280. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  281. void __iomem *mmio = ndev->self_mmio;
  282. unsigned long flags;
  283. if (db_bits & ~ndev->db_valid_mask)
  284. return -EINVAL;
  285. spin_lock_irqsave(&ndev->db_mask_lock, flags);
  286. ndev->db_mask &= ~db_bits;
  287. writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  288. spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
  289. return 0;
  290. }
  291. static int amd_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  292. {
  293. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  294. void __iomem *mmio = ndev->self_mmio;
  295. writew((u16)db_bits, mmio + AMD_DBREQ_OFFSET);
  296. return 0;
  297. }
  298. static int amd_ntb_spad_count(struct ntb_dev *ntb)
  299. {
  300. return ntb_ndev(ntb)->spad_count;
  301. }
  302. static u32 amd_ntb_spad_read(struct ntb_dev *ntb, int idx)
  303. {
  304. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  305. void __iomem *mmio = ndev->self_mmio;
  306. u32 offset;
  307. if (idx < 0 || idx >= ndev->spad_count)
  308. return 0;
  309. offset = ndev->self_spad + (idx << 2);
  310. return readl(mmio + AMD_SPAD_OFFSET + offset);
  311. }
  312. static int amd_ntb_spad_write(struct ntb_dev *ntb,
  313. int idx, u32 val)
  314. {
  315. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  316. void __iomem *mmio = ndev->self_mmio;
  317. u32 offset;
  318. if (idx < 0 || idx >= ndev->spad_count)
  319. return -EINVAL;
  320. offset = ndev->self_spad + (idx << 2);
  321. writel(val, mmio + AMD_SPAD_OFFSET + offset);
  322. return 0;
  323. }
  324. static u32 amd_ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
  325. {
  326. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  327. void __iomem *mmio = ndev->self_mmio;
  328. u32 offset;
  329. if (idx < 0 || idx >= ndev->spad_count)
  330. return -EINVAL;
  331. offset = ndev->peer_spad + (idx << 2);
  332. return readl(mmio + AMD_SPAD_OFFSET + offset);
  333. }
  334. static int amd_ntb_peer_spad_write(struct ntb_dev *ntb,
  335. int idx, u32 val)
  336. {
  337. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  338. void __iomem *mmio = ndev->self_mmio;
  339. u32 offset;
  340. if (idx < 0 || idx >= ndev->spad_count)
  341. return -EINVAL;
  342. offset = ndev->peer_spad + (idx << 2);
  343. writel(val, mmio + AMD_SPAD_OFFSET + offset);
  344. return 0;
  345. }
  346. static const struct ntb_dev_ops amd_ntb_ops = {
  347. .mw_count = amd_ntb_mw_count,
  348. .mw_get_range = amd_ntb_mw_get_range,
  349. .mw_set_trans = amd_ntb_mw_set_trans,
  350. .link_is_up = amd_ntb_link_is_up,
  351. .link_enable = amd_ntb_link_enable,
  352. .link_disable = amd_ntb_link_disable,
  353. .db_valid_mask = amd_ntb_db_valid_mask,
  354. .db_vector_count = amd_ntb_db_vector_count,
  355. .db_vector_mask = amd_ntb_db_vector_mask,
  356. .db_read = amd_ntb_db_read,
  357. .db_clear = amd_ntb_db_clear,
  358. .db_set_mask = amd_ntb_db_set_mask,
  359. .db_clear_mask = amd_ntb_db_clear_mask,
  360. .peer_db_set = amd_ntb_peer_db_set,
  361. .spad_count = amd_ntb_spad_count,
  362. .spad_read = amd_ntb_spad_read,
  363. .spad_write = amd_ntb_spad_write,
  364. .peer_spad_read = amd_ntb_peer_spad_read,
  365. .peer_spad_write = amd_ntb_peer_spad_write,
  366. };
  367. static void amd_ack_smu(struct amd_ntb_dev *ndev, u32 bit)
  368. {
  369. void __iomem *mmio = ndev->self_mmio;
  370. int reg;
  371. reg = readl(mmio + AMD_SMUACK_OFFSET);
  372. reg |= bit;
  373. writel(reg, mmio + AMD_SMUACK_OFFSET);
  374. ndev->peer_sta |= bit;
  375. }
  376. static void amd_handle_event(struct amd_ntb_dev *ndev, int vec)
  377. {
  378. void __iomem *mmio = ndev->self_mmio;
  379. u32 status;
  380. status = readl(mmio + AMD_INTSTAT_OFFSET);
  381. if (!(status & AMD_EVENT_INTMASK))
  382. return;
  383. dev_dbg(ndev_dev(ndev), "status = 0x%x and vec = %d\n", status, vec);
  384. status &= AMD_EVENT_INTMASK;
  385. switch (status) {
  386. case AMD_PEER_FLUSH_EVENT:
  387. dev_info(ndev_dev(ndev), "Flush is done.\n");
  388. break;
  389. case AMD_PEER_RESET_EVENT:
  390. amd_ack_smu(ndev, AMD_PEER_RESET_EVENT);
  391. /* link down first */
  392. ntb_link_event(&ndev->ntb);
  393. /* polling peer status */
  394. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  395. break;
  396. case AMD_PEER_D3_EVENT:
  397. case AMD_PEER_PMETO_EVENT:
  398. case AMD_LINK_UP_EVENT:
  399. case AMD_LINK_DOWN_EVENT:
  400. amd_ack_smu(ndev, status);
  401. /* link down */
  402. ntb_link_event(&ndev->ntb);
  403. break;
  404. case AMD_PEER_D0_EVENT:
  405. mmio = ndev->peer_mmio;
  406. status = readl(mmio + AMD_PMESTAT_OFFSET);
  407. /* check if this is WAKEUP event */
  408. if (status & 0x1)
  409. dev_info(ndev_dev(ndev), "Wakeup is done.\n");
  410. amd_ack_smu(ndev, AMD_PEER_D0_EVENT);
  411. /* start a timer to poll link status */
  412. schedule_delayed_work(&ndev->hb_timer,
  413. AMD_LINK_HB_TIMEOUT);
  414. break;
  415. default:
  416. dev_info(ndev_dev(ndev), "event status = 0x%x.\n", status);
  417. break;
  418. }
  419. }
  420. static irqreturn_t ndev_interrupt(struct amd_ntb_dev *ndev, int vec)
  421. {
  422. dev_dbg(ndev_dev(ndev), "vec %d\n", vec);
  423. if (vec > (AMD_DB_CNT - 1) || (ndev->msix_vec_count == 1))
  424. amd_handle_event(ndev, vec);
  425. if (vec < AMD_DB_CNT)
  426. ntb_db_event(&ndev->ntb, vec);
  427. return IRQ_HANDLED;
  428. }
  429. static irqreturn_t ndev_vec_isr(int irq, void *dev)
  430. {
  431. struct amd_ntb_vec *nvec = dev;
  432. return ndev_interrupt(nvec->ndev, nvec->num);
  433. }
  434. static irqreturn_t ndev_irq_isr(int irq, void *dev)
  435. {
  436. struct amd_ntb_dev *ndev = dev;
  437. return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
  438. }
  439. static int ndev_init_isr(struct amd_ntb_dev *ndev,
  440. int msix_min, int msix_max)
  441. {
  442. struct pci_dev *pdev;
  443. int rc, i, msix_count, node;
  444. pdev = ndev_pdev(ndev);
  445. node = dev_to_node(&pdev->dev);
  446. ndev->db_mask = ndev->db_valid_mask;
  447. /* Try to set up msix irq */
  448. ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
  449. GFP_KERNEL, node);
  450. if (!ndev->vec)
  451. goto err_msix_vec_alloc;
  452. ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
  453. GFP_KERNEL, node);
  454. if (!ndev->msix)
  455. goto err_msix_alloc;
  456. for (i = 0; i < msix_max; ++i)
  457. ndev->msix[i].entry = i;
  458. msix_count = pci_enable_msix_range(pdev, ndev->msix,
  459. msix_min, msix_max);
  460. if (msix_count < 0)
  461. goto err_msix_enable;
  462. /* NOTE: Disable MSIX if msix count is less than 16 because of
  463. * hardware limitation.
  464. */
  465. if (msix_count < msix_min) {
  466. pci_disable_msix(pdev);
  467. goto err_msix_enable;
  468. }
  469. for (i = 0; i < msix_count; ++i) {
  470. ndev->vec[i].ndev = ndev;
  471. ndev->vec[i].num = i;
  472. rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
  473. "ndev_vec_isr", &ndev->vec[i]);
  474. if (rc)
  475. goto err_msix_request;
  476. }
  477. dev_dbg(ndev_dev(ndev), "Using msix interrupts\n");
  478. ndev->db_count = msix_min;
  479. ndev->msix_vec_count = msix_max;
  480. return 0;
  481. err_msix_request:
  482. while (i-- > 0)
  483. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  484. pci_disable_msix(pdev);
  485. err_msix_enable:
  486. kfree(ndev->msix);
  487. err_msix_alloc:
  488. kfree(ndev->vec);
  489. err_msix_vec_alloc:
  490. ndev->msix = NULL;
  491. ndev->vec = NULL;
  492. /* Try to set up msi irq */
  493. rc = pci_enable_msi(pdev);
  494. if (rc)
  495. goto err_msi_enable;
  496. rc = request_irq(pdev->irq, ndev_irq_isr, 0,
  497. "ndev_irq_isr", ndev);
  498. if (rc)
  499. goto err_msi_request;
  500. dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
  501. ndev->db_count = 1;
  502. ndev->msix_vec_count = 1;
  503. return 0;
  504. err_msi_request:
  505. pci_disable_msi(pdev);
  506. err_msi_enable:
  507. /* Try to set up intx irq */
  508. pci_intx(pdev, 1);
  509. rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
  510. "ndev_irq_isr", ndev);
  511. if (rc)
  512. goto err_intx_request;
  513. dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
  514. ndev->db_count = 1;
  515. ndev->msix_vec_count = 1;
  516. return 0;
  517. err_intx_request:
  518. return rc;
  519. }
  520. static void ndev_deinit_isr(struct amd_ntb_dev *ndev)
  521. {
  522. struct pci_dev *pdev;
  523. void __iomem *mmio = ndev->self_mmio;
  524. int i;
  525. pdev = ndev_pdev(ndev);
  526. /* Mask all doorbell interrupts */
  527. ndev->db_mask = ndev->db_valid_mask;
  528. writel(ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  529. if (ndev->msix) {
  530. i = ndev->msix_vec_count;
  531. while (i--)
  532. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  533. pci_disable_msix(pdev);
  534. kfree(ndev->msix);
  535. kfree(ndev->vec);
  536. } else {
  537. free_irq(pdev->irq, ndev);
  538. if (pci_dev_msi_enabled(pdev))
  539. pci_disable_msi(pdev);
  540. else
  541. pci_intx(pdev, 0);
  542. }
  543. }
  544. static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
  545. size_t count, loff_t *offp)
  546. {
  547. struct amd_ntb_dev *ndev;
  548. void __iomem *mmio;
  549. char *buf;
  550. size_t buf_size;
  551. ssize_t ret, off;
  552. union { u64 v64; u32 v32; u16 v16; } u;
  553. ndev = filp->private_data;
  554. mmio = ndev->self_mmio;
  555. buf_size = min(count, 0x800ul);
  556. buf = kmalloc(buf_size, GFP_KERNEL);
  557. if (!buf)
  558. return -ENOMEM;
  559. off = 0;
  560. off += scnprintf(buf + off, buf_size - off,
  561. "NTB Device Information:\n");
  562. off += scnprintf(buf + off, buf_size - off,
  563. "Connection Topology -\t%s\n",
  564. ntb_topo_string(ndev->ntb.topo));
  565. off += scnprintf(buf + off, buf_size - off,
  566. "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
  567. if (!amd_link_is_up(ndev)) {
  568. off += scnprintf(buf + off, buf_size - off,
  569. "Link Status -\t\tDown\n");
  570. } else {
  571. off += scnprintf(buf + off, buf_size - off,
  572. "Link Status -\t\tUp\n");
  573. off += scnprintf(buf + off, buf_size - off,
  574. "Link Speed -\t\tPCI-E Gen %u\n",
  575. NTB_LNK_STA_SPEED(ndev->lnk_sta));
  576. off += scnprintf(buf + off, buf_size - off,
  577. "Link Width -\t\tx%u\n",
  578. NTB_LNK_STA_WIDTH(ndev->lnk_sta));
  579. }
  580. off += scnprintf(buf + off, buf_size - off,
  581. "Memory Window Count -\t%u\n", ndev->mw_count);
  582. off += scnprintf(buf + off, buf_size - off,
  583. "Scratchpad Count -\t%u\n", ndev->spad_count);
  584. off += scnprintf(buf + off, buf_size - off,
  585. "Doorbell Count -\t%u\n", ndev->db_count);
  586. off += scnprintf(buf + off, buf_size - off,
  587. "MSIX Vector Count -\t%u\n", ndev->msix_vec_count);
  588. off += scnprintf(buf + off, buf_size - off,
  589. "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
  590. u.v32 = readl(ndev->self_mmio + AMD_DBMASK_OFFSET);
  591. off += scnprintf(buf + off, buf_size - off,
  592. "Doorbell Mask -\t\t\t%#06x\n", u.v32);
  593. u.v32 = readl(mmio + AMD_DBSTAT_OFFSET);
  594. off += scnprintf(buf + off, buf_size - off,
  595. "Doorbell Bell -\t\t\t%#06x\n", u.v32);
  596. off += scnprintf(buf + off, buf_size - off,
  597. "\nNTB Incoming XLAT:\n");
  598. u.v64 = read64(mmio + AMD_BAR1XLAT_OFFSET);
  599. off += scnprintf(buf + off, buf_size - off,
  600. "XLAT1 -\t\t%#018llx\n", u.v64);
  601. u.v64 = read64(ndev->self_mmio + AMD_BAR23XLAT_OFFSET);
  602. off += scnprintf(buf + off, buf_size - off,
  603. "XLAT23 -\t\t%#018llx\n", u.v64);
  604. u.v64 = read64(ndev->self_mmio + AMD_BAR45XLAT_OFFSET);
  605. off += scnprintf(buf + off, buf_size - off,
  606. "XLAT45 -\t\t%#018llx\n", u.v64);
  607. u.v32 = readl(mmio + AMD_BAR1LMT_OFFSET);
  608. off += scnprintf(buf + off, buf_size - off,
  609. "LMT1 -\t\t\t%#06x\n", u.v32);
  610. u.v64 = read64(ndev->self_mmio + AMD_BAR23LMT_OFFSET);
  611. off += scnprintf(buf + off, buf_size - off,
  612. "LMT23 -\t\t\t%#018llx\n", u.v64);
  613. u.v64 = read64(ndev->self_mmio + AMD_BAR45LMT_OFFSET);
  614. off += scnprintf(buf + off, buf_size - off,
  615. "LMT45 -\t\t\t%#018llx\n", u.v64);
  616. ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
  617. kfree(buf);
  618. return ret;
  619. }
  620. static void ndev_init_debugfs(struct amd_ntb_dev *ndev)
  621. {
  622. if (!debugfs_dir) {
  623. ndev->debugfs_dir = NULL;
  624. ndev->debugfs_info = NULL;
  625. } else {
  626. ndev->debugfs_dir =
  627. debugfs_create_dir(ndev_name(ndev), debugfs_dir);
  628. if (!ndev->debugfs_dir)
  629. ndev->debugfs_info = NULL;
  630. else
  631. ndev->debugfs_info =
  632. debugfs_create_file("info", S_IRUSR,
  633. ndev->debugfs_dir, ndev,
  634. &amd_ntb_debugfs_info);
  635. }
  636. }
  637. static void ndev_deinit_debugfs(struct amd_ntb_dev *ndev)
  638. {
  639. debugfs_remove_recursive(ndev->debugfs_dir);
  640. }
  641. static inline void ndev_init_struct(struct amd_ntb_dev *ndev,
  642. struct pci_dev *pdev)
  643. {
  644. ndev->ntb.pdev = pdev;
  645. ndev->ntb.topo = NTB_TOPO_NONE;
  646. ndev->ntb.ops = &amd_ntb_ops;
  647. ndev->int_mask = AMD_EVENT_INTMASK;
  648. spin_lock_init(&ndev->db_mask_lock);
  649. }
  650. static int amd_poll_link(struct amd_ntb_dev *ndev)
  651. {
  652. void __iomem *mmio = ndev->peer_mmio;
  653. u32 reg, stat;
  654. int rc;
  655. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  656. reg &= NTB_LIN_STA_ACTIVE_BIT;
  657. dev_dbg(ndev_dev(ndev), "%s: reg_val = 0x%x.\n", __func__, reg);
  658. if (reg == ndev->cntl_sta)
  659. return 0;
  660. ndev->cntl_sta = reg;
  661. rc = pci_read_config_dword(ndev->ntb.pdev,
  662. AMD_LINK_STATUS_OFFSET, &stat);
  663. if (rc)
  664. return 0;
  665. ndev->lnk_sta = stat;
  666. return 1;
  667. }
  668. static void amd_link_hb(struct work_struct *work)
  669. {
  670. struct amd_ntb_dev *ndev = hb_ndev(work);
  671. if (amd_poll_link(ndev))
  672. ntb_link_event(&ndev->ntb);
  673. if (!amd_link_is_up(ndev))
  674. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  675. }
  676. static int amd_init_isr(struct amd_ntb_dev *ndev)
  677. {
  678. return ndev_init_isr(ndev, AMD_DB_CNT, AMD_MSIX_VECTOR_CNT);
  679. }
  680. static void amd_init_side_info(struct amd_ntb_dev *ndev)
  681. {
  682. void __iomem *mmio = ndev->self_mmio;
  683. unsigned int reg;
  684. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  685. if (!(reg & AMD_SIDE_READY)) {
  686. reg |= AMD_SIDE_READY;
  687. writel(reg, mmio + AMD_SIDEINFO_OFFSET);
  688. }
  689. }
  690. static void amd_deinit_side_info(struct amd_ntb_dev *ndev)
  691. {
  692. void __iomem *mmio = ndev->self_mmio;
  693. unsigned int reg;
  694. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  695. if (reg & AMD_SIDE_READY) {
  696. reg &= ~AMD_SIDE_READY;
  697. writel(reg, mmio + AMD_SIDEINFO_OFFSET);
  698. readl(mmio + AMD_SIDEINFO_OFFSET);
  699. }
  700. }
  701. static int amd_init_ntb(struct amd_ntb_dev *ndev)
  702. {
  703. void __iomem *mmio = ndev->self_mmio;
  704. ndev->mw_count = AMD_MW_CNT;
  705. ndev->spad_count = AMD_SPADS_CNT;
  706. ndev->db_count = AMD_DB_CNT;
  707. switch (ndev->ntb.topo) {
  708. case NTB_TOPO_PRI:
  709. case NTB_TOPO_SEC:
  710. ndev->spad_count >>= 1;
  711. if (ndev->ntb.topo == NTB_TOPO_PRI) {
  712. ndev->self_spad = 0;
  713. ndev->peer_spad = 0x20;
  714. } else {
  715. ndev->self_spad = 0x20;
  716. ndev->peer_spad = 0;
  717. }
  718. INIT_DELAYED_WORK(&ndev->hb_timer, amd_link_hb);
  719. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  720. break;
  721. default:
  722. dev_err(ndev_dev(ndev), "AMD NTB does not support B2B mode.\n");
  723. return -EINVAL;
  724. }
  725. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  726. /* Mask event interrupts */
  727. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  728. return 0;
  729. }
  730. static enum ntb_topo amd_get_topo(struct amd_ntb_dev *ndev)
  731. {
  732. void __iomem *mmio = ndev->self_mmio;
  733. u32 info;
  734. info = readl(mmio + AMD_SIDEINFO_OFFSET);
  735. if (info & AMD_SIDE_MASK)
  736. return NTB_TOPO_SEC;
  737. else
  738. return NTB_TOPO_PRI;
  739. }
  740. static int amd_init_dev(struct amd_ntb_dev *ndev)
  741. {
  742. struct pci_dev *pdev;
  743. int rc = 0;
  744. pdev = ndev_pdev(ndev);
  745. ndev->ntb.topo = amd_get_topo(ndev);
  746. dev_dbg(ndev_dev(ndev), "AMD NTB topo is %s\n",
  747. ntb_topo_string(ndev->ntb.topo));
  748. rc = amd_init_ntb(ndev);
  749. if (rc)
  750. return rc;
  751. rc = amd_init_isr(ndev);
  752. if (rc) {
  753. dev_err(ndev_dev(ndev), "fail to init isr.\n");
  754. return rc;
  755. }
  756. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  757. return 0;
  758. }
  759. static void amd_deinit_dev(struct amd_ntb_dev *ndev)
  760. {
  761. cancel_delayed_work_sync(&ndev->hb_timer);
  762. ndev_deinit_isr(ndev);
  763. }
  764. static int amd_ntb_init_pci(struct amd_ntb_dev *ndev,
  765. struct pci_dev *pdev)
  766. {
  767. int rc;
  768. pci_set_drvdata(pdev, ndev);
  769. rc = pci_enable_device(pdev);
  770. if (rc)
  771. goto err_pci_enable;
  772. rc = pci_request_regions(pdev, NTB_NAME);
  773. if (rc)
  774. goto err_pci_regions;
  775. pci_set_master(pdev);
  776. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  777. if (rc) {
  778. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  779. if (rc)
  780. goto err_dma_mask;
  781. dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
  782. }
  783. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  784. if (rc) {
  785. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  786. if (rc)
  787. goto err_dma_mask;
  788. dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
  789. }
  790. ndev->self_mmio = pci_iomap(pdev, 0, 0);
  791. if (!ndev->self_mmio) {
  792. rc = -EIO;
  793. goto err_dma_mask;
  794. }
  795. ndev->peer_mmio = ndev->self_mmio + AMD_PEER_OFFSET;
  796. return 0;
  797. err_dma_mask:
  798. pci_clear_master(pdev);
  799. err_pci_regions:
  800. pci_disable_device(pdev);
  801. err_pci_enable:
  802. pci_set_drvdata(pdev, NULL);
  803. return rc;
  804. }
  805. static void amd_ntb_deinit_pci(struct amd_ntb_dev *ndev)
  806. {
  807. struct pci_dev *pdev = ndev_pdev(ndev);
  808. pci_iounmap(pdev, ndev->self_mmio);
  809. pci_clear_master(pdev);
  810. pci_release_regions(pdev);
  811. pci_disable_device(pdev);
  812. pci_set_drvdata(pdev, NULL);
  813. }
  814. static int amd_ntb_pci_probe(struct pci_dev *pdev,
  815. const struct pci_device_id *id)
  816. {
  817. struct amd_ntb_dev *ndev;
  818. int rc, node;
  819. node = dev_to_node(&pdev->dev);
  820. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  821. if (!ndev) {
  822. rc = -ENOMEM;
  823. goto err_ndev;
  824. }
  825. ndev_init_struct(ndev, pdev);
  826. rc = amd_ntb_init_pci(ndev, pdev);
  827. if (rc)
  828. goto err_init_pci;
  829. rc = amd_init_dev(ndev);
  830. if (rc)
  831. goto err_init_dev;
  832. /* write side info */
  833. amd_init_side_info(ndev);
  834. amd_poll_link(ndev);
  835. ndev_init_debugfs(ndev);
  836. rc = ntb_register_device(&ndev->ntb);
  837. if (rc)
  838. goto err_register;
  839. dev_info(&pdev->dev, "NTB device registered.\n");
  840. return 0;
  841. err_register:
  842. ndev_deinit_debugfs(ndev);
  843. amd_deinit_dev(ndev);
  844. err_init_dev:
  845. amd_ntb_deinit_pci(ndev);
  846. err_init_pci:
  847. kfree(ndev);
  848. err_ndev:
  849. return rc;
  850. }
  851. static void amd_ntb_pci_remove(struct pci_dev *pdev)
  852. {
  853. struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
  854. ntb_unregister_device(&ndev->ntb);
  855. ndev_deinit_debugfs(ndev);
  856. amd_deinit_side_info(ndev);
  857. amd_deinit_dev(ndev);
  858. amd_ntb_deinit_pci(ndev);
  859. kfree(ndev);
  860. }
  861. static const struct file_operations amd_ntb_debugfs_info = {
  862. .owner = THIS_MODULE,
  863. .open = simple_open,
  864. .read = ndev_debugfs_read,
  865. };
  866. static const struct pci_device_id amd_ntb_pci_tbl[] = {
  867. {PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NTB)},
  868. {0}
  869. };
  870. MODULE_DEVICE_TABLE(pci, amd_ntb_pci_tbl);
  871. static struct pci_driver amd_ntb_pci_driver = {
  872. .name = KBUILD_MODNAME,
  873. .id_table = amd_ntb_pci_tbl,
  874. .probe = amd_ntb_pci_probe,
  875. .remove = amd_ntb_pci_remove,
  876. };
  877. static int __init amd_ntb_pci_driver_init(void)
  878. {
  879. pr_info("%s %s\n", NTB_DESC, NTB_VER);
  880. if (debugfs_initialized())
  881. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  882. return pci_register_driver(&amd_ntb_pci_driver);
  883. }
  884. module_init(amd_ntb_pci_driver_init);
  885. static void __exit amd_ntb_pci_driver_exit(void)
  886. {
  887. pci_unregister_driver(&amd_ntb_pci_driver);
  888. debugfs_remove_recursive(debugfs_dir);
  889. }
  890. module_exit(amd_ntb_pci_driver_exit);