ntb_hw_amd.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. * Copyright (C) 2016 T-Platforms. All Rights Reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * BSD LICENSE
  15. *
  16. * Copyright (C) 2016 Advanced Micro Devices, Inc. All Rights Reserved.
  17. * Copyright (C) 2016 T-Platforms. All Rights Reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. *
  23. * * Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * * Redistributions in binary form must reproduce the above copy
  26. * notice, this list of conditions and the following disclaimer in
  27. * the documentation and/or other materials provided with the
  28. * distribution.
  29. * * Neither the name of AMD Corporation nor the names of its
  30. * contributors may be used to endorse or promote products derived
  31. * from this software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  34. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  35. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  36. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  37. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  39. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  40. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  41. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  43. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. * AMD PCIe NTB Linux driver
  46. *
  47. * Contact Information:
  48. * Xiangliang Yu <Xiangliang.Yu@amd.com>
  49. */
  50. #include <linux/debugfs.h>
  51. #include <linux/delay.h>
  52. #include <linux/init.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/module.h>
  55. #include <linux/acpi.h>
  56. #include <linux/pci.h>
  57. #include <linux/random.h>
  58. #include <linux/slab.h>
  59. #include <linux/ntb.h>
  60. #include "ntb_hw_amd.h"
  61. #define NTB_NAME "ntb_hw_amd"
  62. #define NTB_DESC "AMD(R) PCI-E Non-Transparent Bridge Driver"
  63. #define NTB_VER "1.0"
  64. MODULE_DESCRIPTION(NTB_DESC);
  65. MODULE_VERSION(NTB_VER);
  66. MODULE_LICENSE("Dual BSD/GPL");
  67. MODULE_AUTHOR("AMD Inc.");
  68. static const struct file_operations amd_ntb_debugfs_info;
  69. static struct dentry *debugfs_dir;
  70. static int ndev_mw_to_bar(struct amd_ntb_dev *ndev, int idx)
  71. {
  72. if (idx < 0 || idx > ndev->mw_count)
  73. return -EINVAL;
  74. return ndev->dev_data->mw_idx << idx;
  75. }
  76. static int amd_ntb_mw_count(struct ntb_dev *ntb, int pidx)
  77. {
  78. if (pidx != NTB_DEF_PEER_IDX)
  79. return -EINVAL;
  80. return ntb_ndev(ntb)->mw_count;
  81. }
  82. static int amd_ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int idx,
  83. resource_size_t *addr_align,
  84. resource_size_t *size_align,
  85. resource_size_t *size_max)
  86. {
  87. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  88. int bar;
  89. if (pidx != NTB_DEF_PEER_IDX)
  90. return -EINVAL;
  91. bar = ndev_mw_to_bar(ndev, idx);
  92. if (bar < 0)
  93. return bar;
  94. if (addr_align)
  95. *addr_align = SZ_4K;
  96. if (size_align)
  97. *size_align = 1;
  98. if (size_max)
  99. *size_max = pci_resource_len(ndev->ntb.pdev, bar);
  100. return 0;
  101. }
  102. static int amd_ntb_mw_set_trans(struct ntb_dev *ntb, int pidx, int idx,
  103. dma_addr_t addr, resource_size_t size)
  104. {
  105. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  106. unsigned long xlat_reg, limit_reg = 0;
  107. resource_size_t mw_size;
  108. void __iomem *mmio, *peer_mmio;
  109. u64 base_addr, limit, reg_val;
  110. int bar;
  111. if (pidx != NTB_DEF_PEER_IDX)
  112. return -EINVAL;
  113. bar = ndev_mw_to_bar(ndev, idx);
  114. if (bar < 0)
  115. return bar;
  116. mw_size = pci_resource_len(ntb->pdev, bar);
  117. /* make sure the range fits in the usable mw size */
  118. if (size > mw_size)
  119. return -EINVAL;
  120. mmio = ndev->self_mmio;
  121. peer_mmio = ndev->peer_mmio;
  122. base_addr = pci_resource_start(ntb->pdev, bar);
  123. if (bar != 1) {
  124. xlat_reg = AMD_BAR23XLAT_OFFSET + ((bar - 2) << 2);
  125. limit_reg = AMD_BAR23LMT_OFFSET + ((bar - 2) << 2);
  126. /* Set the limit if supported */
  127. limit = size;
  128. /* set and verify setting the translation address */
  129. write64(addr, peer_mmio + xlat_reg);
  130. reg_val = read64(peer_mmio + xlat_reg);
  131. if (reg_val != addr) {
  132. write64(0, peer_mmio + xlat_reg);
  133. return -EIO;
  134. }
  135. /* set and verify setting the limit */
  136. write64(limit, peer_mmio + limit_reg);
  137. reg_val = read64(peer_mmio + limit_reg);
  138. if (reg_val != limit) {
  139. write64(base_addr, mmio + limit_reg);
  140. write64(0, peer_mmio + xlat_reg);
  141. return -EIO;
  142. }
  143. } else {
  144. xlat_reg = AMD_BAR1XLAT_OFFSET;
  145. limit_reg = AMD_BAR1LMT_OFFSET;
  146. /* Set the limit if supported */
  147. limit = size;
  148. /* set and verify setting the translation address */
  149. write64(addr, peer_mmio + xlat_reg);
  150. reg_val = read64(peer_mmio + xlat_reg);
  151. if (reg_val != addr) {
  152. write64(0, peer_mmio + xlat_reg);
  153. return -EIO;
  154. }
  155. /* set and verify setting the limit */
  156. writel(limit, peer_mmio + limit_reg);
  157. reg_val = readl(peer_mmio + limit_reg);
  158. if (reg_val != limit) {
  159. writel(base_addr, mmio + limit_reg);
  160. writel(0, peer_mmio + xlat_reg);
  161. return -EIO;
  162. }
  163. }
  164. return 0;
  165. }
  166. static int amd_ntb_get_link_status(struct amd_ntb_dev *ndev)
  167. {
  168. struct pci_dev *pdev = ndev->ntb.pdev;
  169. struct pci_dev *pci_swds = NULL;
  170. struct pci_dev *pci_swus = NULL;
  171. u32 stat;
  172. int rc;
  173. if (ndev->ntb.topo == NTB_TOPO_SEC) {
  174. if (ndev->dev_data->is_endpoint) {
  175. rc = pcie_capability_read_dword(pdev, PCI_EXP_LNKCTL, &stat);
  176. if (rc)
  177. return rc;
  178. ndev->lnk_sta = stat;
  179. return 0;
  180. }
  181. /* Locate the pointer to Downstream Switch for this device */
  182. pci_swds = pci_upstream_bridge(ndev->ntb.pdev);
  183. if (pci_swds) {
  184. /*
  185. * Locate the pointer to Upstream Switch for
  186. * the Downstream Switch.
  187. */
  188. pci_swus = pci_upstream_bridge(pci_swds);
  189. if (pci_swus) {
  190. rc = pcie_capability_read_dword(pci_swus,
  191. PCI_EXP_LNKCTL,
  192. &stat);
  193. if (rc)
  194. return 0;
  195. } else {
  196. return 0;
  197. }
  198. } else {
  199. return 0;
  200. }
  201. } else if (ndev->ntb.topo == NTB_TOPO_PRI) {
  202. /*
  203. * For NTB primary, we simply read the Link Status and control
  204. * register of the NTB device itself.
  205. */
  206. pdev = ndev->ntb.pdev;
  207. rc = pcie_capability_read_dword(pdev, PCI_EXP_LNKCTL, &stat);
  208. if (rc)
  209. return 0;
  210. } else {
  211. /* Catch all for everything else */
  212. return 0;
  213. }
  214. ndev->lnk_sta = stat;
  215. return 1;
  216. }
  217. static int amd_link_is_up(struct amd_ntb_dev *ndev)
  218. {
  219. int ret;
  220. /*
  221. * We consider the link to be up under two conditions:
  222. *
  223. * - When a link-up event is received. This is indicated by
  224. * AMD_LINK_UP_EVENT set in peer_sta.
  225. * - When driver on both sides of the link have been loaded.
  226. * This is indicated by bit 1 being set in the peer
  227. * SIDEINFO register.
  228. *
  229. * This function should return 1 when the latter of the above
  230. * two conditions is true.
  231. *
  232. * Now consider the sequence of events - Link-Up event occurs,
  233. * then the peer side driver loads. In this case, we would have
  234. * received LINK_UP event and bit 1 of peer SIDEINFO is also
  235. * set. What happens now if the link goes down? Bit 1 of
  236. * peer SIDEINFO remains set, but LINK_DOWN bit is set in
  237. * peer_sta. So we should return 0 from this function. Not only
  238. * that, we clear bit 1 of peer SIDEINFO to 0, since the peer
  239. * side driver did not even get a chance to clear it before
  240. * the link went down. This can be the case of surprise link
  241. * removal.
  242. *
  243. * LINK_UP event will always occur before the peer side driver
  244. * gets loaded the very first time. So there can be a case when
  245. * the LINK_UP event has occurred, but the peer side driver hasn't
  246. * yet loaded. We return 0 in that case.
  247. *
  248. * There is also a special case when the primary side driver is
  249. * unloaded and then loaded again. Since there is no change in
  250. * the status of NTB secondary in this case, there is no Link-Up
  251. * or Link-Down notification received. We recognize this condition
  252. * with peer_sta being set to 0.
  253. *
  254. * If bit 1 of peer SIDEINFO register is not set, then we
  255. * simply return 0 irrespective of the link up or down status
  256. * set in peer_sta.
  257. */
  258. ret = amd_poll_link(ndev);
  259. if (ret) {
  260. /*
  261. * We need to check the below only for NTB primary. For NTB
  262. * secondary, simply checking the result of PSIDE_INFO
  263. * register will suffice.
  264. */
  265. if (ndev->ntb.topo == NTB_TOPO_PRI) {
  266. if ((ndev->peer_sta & AMD_LINK_UP_EVENT) ||
  267. (ndev->peer_sta == 0))
  268. return ret;
  269. else if (ndev->peer_sta & AMD_LINK_DOWN_EVENT) {
  270. /* Clear peer sideinfo register */
  271. amd_clear_side_info_reg(ndev, true);
  272. return 0;
  273. }
  274. } else { /* NTB_TOPO_SEC */
  275. return ret;
  276. }
  277. }
  278. return 0;
  279. }
  280. static u64 amd_ntb_link_is_up(struct ntb_dev *ntb,
  281. enum ntb_speed *speed,
  282. enum ntb_width *width)
  283. {
  284. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  285. int ret = 0;
  286. if (amd_link_is_up(ndev)) {
  287. if (speed)
  288. *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
  289. if (width)
  290. *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
  291. dev_dbg(&ntb->pdev->dev, "link is up.\n");
  292. ret = 1;
  293. } else {
  294. if (speed)
  295. *speed = NTB_SPEED_NONE;
  296. if (width)
  297. *width = NTB_WIDTH_NONE;
  298. dev_dbg(&ntb->pdev->dev, "link is down.\n");
  299. }
  300. return ret;
  301. }
  302. static int amd_ntb_link_enable(struct ntb_dev *ntb,
  303. enum ntb_speed max_speed,
  304. enum ntb_width max_width)
  305. {
  306. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  307. void __iomem *mmio = ndev->self_mmio;
  308. /* Enable event interrupt */
  309. ndev->int_mask &= ~AMD_EVENT_INTMASK;
  310. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  311. if (ndev->ntb.topo == NTB_TOPO_SEC)
  312. return -EINVAL;
  313. dev_dbg(&ntb->pdev->dev, "Enabling Link.\n");
  314. return 0;
  315. }
  316. static int amd_ntb_link_disable(struct ntb_dev *ntb)
  317. {
  318. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  319. void __iomem *mmio = ndev->self_mmio;
  320. /* Disable event interrupt */
  321. ndev->int_mask |= AMD_EVENT_INTMASK;
  322. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  323. if (ndev->ntb.topo == NTB_TOPO_SEC)
  324. return -EINVAL;
  325. dev_dbg(&ntb->pdev->dev, "Enabling Link.\n");
  326. return 0;
  327. }
  328. static int amd_ntb_peer_mw_count(struct ntb_dev *ntb)
  329. {
  330. /* The same as for inbound MWs */
  331. return ntb_ndev(ntb)->mw_count;
  332. }
  333. static int amd_ntb_peer_mw_get_addr(struct ntb_dev *ntb, int idx,
  334. phys_addr_t *base, resource_size_t *size)
  335. {
  336. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  337. int bar;
  338. bar = ndev_mw_to_bar(ndev, idx);
  339. if (bar < 0)
  340. return bar;
  341. if (base)
  342. *base = pci_resource_start(ndev->ntb.pdev, bar);
  343. if (size)
  344. *size = pci_resource_len(ndev->ntb.pdev, bar);
  345. return 0;
  346. }
  347. static u64 amd_ntb_db_valid_mask(struct ntb_dev *ntb)
  348. {
  349. return ntb_ndev(ntb)->db_valid_mask;
  350. }
  351. static int amd_ntb_db_vector_count(struct ntb_dev *ntb)
  352. {
  353. return ntb_ndev(ntb)->db_count;
  354. }
  355. static u64 amd_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
  356. {
  357. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  358. if (db_vector < 0 || db_vector > ndev->db_count)
  359. return 0;
  360. return ntb_ndev(ntb)->db_valid_mask & (1ULL << db_vector);
  361. }
  362. static u64 amd_ntb_db_read(struct ntb_dev *ntb)
  363. {
  364. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  365. void __iomem *mmio = ndev->self_mmio;
  366. return (u64)readw(mmio + AMD_DBSTAT_OFFSET);
  367. }
  368. static int amd_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
  369. {
  370. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  371. void __iomem *mmio = ndev->self_mmio;
  372. writew((u16)db_bits, mmio + AMD_DBSTAT_OFFSET);
  373. return 0;
  374. }
  375. static int amd_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  376. {
  377. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  378. void __iomem *mmio = ndev->self_mmio;
  379. unsigned long flags;
  380. if (db_bits & ~ndev->db_valid_mask)
  381. return -EINVAL;
  382. spin_lock_irqsave(&ndev->db_mask_lock, flags);
  383. ndev->db_mask |= db_bits;
  384. writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  385. spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
  386. return 0;
  387. }
  388. static int amd_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  389. {
  390. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  391. void __iomem *mmio = ndev->self_mmio;
  392. unsigned long flags;
  393. if (db_bits & ~ndev->db_valid_mask)
  394. return -EINVAL;
  395. spin_lock_irqsave(&ndev->db_mask_lock, flags);
  396. ndev->db_mask &= ~db_bits;
  397. writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  398. spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
  399. return 0;
  400. }
  401. static int amd_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  402. {
  403. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  404. void __iomem *mmio = ndev->self_mmio;
  405. writew((u16)db_bits, mmio + AMD_DBREQ_OFFSET);
  406. return 0;
  407. }
  408. static int amd_ntb_spad_count(struct ntb_dev *ntb)
  409. {
  410. return ntb_ndev(ntb)->spad_count;
  411. }
  412. static u32 amd_ntb_spad_read(struct ntb_dev *ntb, int idx)
  413. {
  414. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  415. void __iomem *mmio = ndev->self_mmio;
  416. u32 offset;
  417. if (idx < 0 || idx >= ndev->spad_count)
  418. return 0;
  419. offset = ndev->self_spad + (idx << 2);
  420. return readl(mmio + AMD_SPAD_OFFSET + offset);
  421. }
  422. static int amd_ntb_spad_write(struct ntb_dev *ntb,
  423. int idx, u32 val)
  424. {
  425. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  426. void __iomem *mmio = ndev->self_mmio;
  427. u32 offset;
  428. if (idx < 0 || idx >= ndev->spad_count)
  429. return -EINVAL;
  430. offset = ndev->self_spad + (idx << 2);
  431. writel(val, mmio + AMD_SPAD_OFFSET + offset);
  432. return 0;
  433. }
  434. static u32 amd_ntb_peer_spad_read(struct ntb_dev *ntb, int pidx, int sidx)
  435. {
  436. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  437. void __iomem *mmio = ndev->self_mmio;
  438. u32 offset;
  439. if (sidx < 0 || sidx >= ndev->spad_count)
  440. return -EINVAL;
  441. offset = ndev->peer_spad + (sidx << 2);
  442. return readl(mmio + AMD_SPAD_OFFSET + offset);
  443. }
  444. static int amd_ntb_peer_spad_write(struct ntb_dev *ntb, int pidx,
  445. int sidx, u32 val)
  446. {
  447. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  448. void __iomem *mmio = ndev->self_mmio;
  449. u32 offset;
  450. if (sidx < 0 || sidx >= ndev->spad_count)
  451. return -EINVAL;
  452. offset = ndev->peer_spad + (sidx << 2);
  453. writel(val, mmio + AMD_SPAD_OFFSET + offset);
  454. return 0;
  455. }
  456. static const struct ntb_dev_ops amd_ntb_ops = {
  457. .mw_count = amd_ntb_mw_count,
  458. .mw_get_align = amd_ntb_mw_get_align,
  459. .mw_set_trans = amd_ntb_mw_set_trans,
  460. .peer_mw_count = amd_ntb_peer_mw_count,
  461. .peer_mw_get_addr = amd_ntb_peer_mw_get_addr,
  462. .link_is_up = amd_ntb_link_is_up,
  463. .link_enable = amd_ntb_link_enable,
  464. .link_disable = amd_ntb_link_disable,
  465. .db_valid_mask = amd_ntb_db_valid_mask,
  466. .db_vector_count = amd_ntb_db_vector_count,
  467. .db_vector_mask = amd_ntb_db_vector_mask,
  468. .db_read = amd_ntb_db_read,
  469. .db_clear = amd_ntb_db_clear,
  470. .db_set_mask = amd_ntb_db_set_mask,
  471. .db_clear_mask = amd_ntb_db_clear_mask,
  472. .peer_db_set = amd_ntb_peer_db_set,
  473. .spad_count = amd_ntb_spad_count,
  474. .spad_read = amd_ntb_spad_read,
  475. .spad_write = amd_ntb_spad_write,
  476. .peer_spad_read = amd_ntb_peer_spad_read,
  477. .peer_spad_write = amd_ntb_peer_spad_write,
  478. };
  479. static void amd_ack_smu(struct amd_ntb_dev *ndev, u32 bit)
  480. {
  481. void __iomem *mmio = ndev->self_mmio;
  482. int reg;
  483. reg = readl(mmio + AMD_SMUACK_OFFSET);
  484. reg |= bit;
  485. writel(reg, mmio + AMD_SMUACK_OFFSET);
  486. }
  487. static void amd_handle_event(struct amd_ntb_dev *ndev, int vec)
  488. {
  489. void __iomem *mmio = ndev->self_mmio;
  490. struct device *dev = &ndev->ntb.pdev->dev;
  491. u32 status;
  492. status = readl(mmio + AMD_INTSTAT_OFFSET);
  493. if (!(status & AMD_EVENT_INTMASK))
  494. return;
  495. dev_dbg(dev, "status = 0x%x and vec = %d\n", status, vec);
  496. status &= AMD_EVENT_INTMASK;
  497. switch (status) {
  498. case AMD_PEER_FLUSH_EVENT:
  499. ndev->peer_sta |= AMD_PEER_FLUSH_EVENT;
  500. dev_info(dev, "Flush is done.\n");
  501. break;
  502. case AMD_PEER_RESET_EVENT:
  503. case AMD_LINK_DOWN_EVENT:
  504. ndev->peer_sta |= status;
  505. if (status == AMD_LINK_DOWN_EVENT)
  506. ndev->peer_sta &= ~AMD_LINK_UP_EVENT;
  507. amd_ack_smu(ndev, status);
  508. /* link down first */
  509. ntb_link_event(&ndev->ntb);
  510. /* polling peer status */
  511. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  512. break;
  513. case AMD_PEER_D3_EVENT:
  514. case AMD_PEER_PMETO_EVENT:
  515. case AMD_LINK_UP_EVENT:
  516. ndev->peer_sta |= status;
  517. if (status == AMD_LINK_UP_EVENT)
  518. ndev->peer_sta &= ~AMD_LINK_DOWN_EVENT;
  519. else if (status == AMD_PEER_D3_EVENT)
  520. ndev->peer_sta &= ~AMD_PEER_D0_EVENT;
  521. amd_ack_smu(ndev, status);
  522. /* link down */
  523. ntb_link_event(&ndev->ntb);
  524. break;
  525. case AMD_PEER_D0_EVENT:
  526. mmio = ndev->peer_mmio;
  527. status = readl(mmio + AMD_PMESTAT_OFFSET);
  528. /* check if this is WAKEUP event */
  529. if (status & 0x1)
  530. dev_info(dev, "Wakeup is done.\n");
  531. ndev->peer_sta |= AMD_PEER_D0_EVENT;
  532. ndev->peer_sta &= ~AMD_PEER_D3_EVENT;
  533. amd_ack_smu(ndev, AMD_PEER_D0_EVENT);
  534. /* start a timer to poll link status */
  535. schedule_delayed_work(&ndev->hb_timer,
  536. AMD_LINK_HB_TIMEOUT);
  537. break;
  538. default:
  539. dev_info(dev, "event status = 0x%x.\n", status);
  540. break;
  541. }
  542. /* Clear the interrupt status */
  543. writel(status, mmio + AMD_INTSTAT_OFFSET);
  544. }
  545. static void amd_handle_db_event(struct amd_ntb_dev *ndev, int vec)
  546. {
  547. struct device *dev = &ndev->ntb.pdev->dev;
  548. u64 status;
  549. status = amd_ntb_db_read(&ndev->ntb);
  550. dev_dbg(dev, "status = 0x%llx and vec = %d\n", status, vec);
  551. /*
  552. * Since we had reserved highest order bit of DB for signaling peer of
  553. * a special event, this is the only status bit we should be concerned
  554. * here now.
  555. */
  556. if (status & BIT(ndev->db_last_bit)) {
  557. ntb_db_clear(&ndev->ntb, BIT(ndev->db_last_bit));
  558. /* send link down event notification */
  559. ntb_link_event(&ndev->ntb);
  560. /*
  561. * If we are here, that means the peer has signalled a special
  562. * event which notifies that the peer driver has been
  563. * un-loaded for some reason. Since there is a chance that the
  564. * peer will load its driver again sometime, we schedule link
  565. * polling routine.
  566. */
  567. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  568. }
  569. }
  570. static irqreturn_t ndev_interrupt(struct amd_ntb_dev *ndev, int vec)
  571. {
  572. dev_dbg(&ndev->ntb.pdev->dev, "vec %d\n", vec);
  573. if (vec > (AMD_DB_CNT - 1) || (ndev->msix_vec_count == 1))
  574. amd_handle_event(ndev, vec);
  575. if (vec < AMD_DB_CNT) {
  576. amd_handle_db_event(ndev, vec);
  577. ntb_db_event(&ndev->ntb, vec);
  578. }
  579. return IRQ_HANDLED;
  580. }
  581. static irqreturn_t ndev_vec_isr(int irq, void *dev)
  582. {
  583. struct amd_ntb_vec *nvec = dev;
  584. return ndev_interrupt(nvec->ndev, nvec->num);
  585. }
  586. static irqreturn_t ndev_irq_isr(int irq, void *dev)
  587. {
  588. struct amd_ntb_dev *ndev = dev;
  589. return ndev_interrupt(ndev, irq - ndev->ntb.pdev->irq);
  590. }
  591. static int ndev_init_isr(struct amd_ntb_dev *ndev,
  592. int msix_min, int msix_max)
  593. {
  594. struct pci_dev *pdev;
  595. int rc, i, msix_count, node;
  596. pdev = ndev->ntb.pdev;
  597. node = dev_to_node(&pdev->dev);
  598. ndev->db_mask = ndev->db_valid_mask;
  599. /* Try to set up msix irq */
  600. ndev->vec = kcalloc_node(msix_max, sizeof(*ndev->vec),
  601. GFP_KERNEL, node);
  602. if (!ndev->vec)
  603. goto err_msix_vec_alloc;
  604. ndev->msix = kcalloc_node(msix_max, sizeof(*ndev->msix),
  605. GFP_KERNEL, node);
  606. if (!ndev->msix)
  607. goto err_msix_alloc;
  608. for (i = 0; i < msix_max; ++i)
  609. ndev->msix[i].entry = i;
  610. msix_count = pci_enable_msix_range(pdev, ndev->msix,
  611. msix_min, msix_max);
  612. if (msix_count < 0)
  613. goto err_msix_enable;
  614. /* NOTE: Disable MSIX if msix count is less than 16 because of
  615. * hardware limitation.
  616. */
  617. if (msix_count < msix_min) {
  618. pci_disable_msix(pdev);
  619. goto err_msix_enable;
  620. }
  621. for (i = 0; i < msix_count; ++i) {
  622. ndev->vec[i].ndev = ndev;
  623. ndev->vec[i].num = i;
  624. rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
  625. "ndev_vec_isr", &ndev->vec[i]);
  626. if (rc)
  627. goto err_msix_request;
  628. }
  629. dev_dbg(&pdev->dev, "Using msix interrupts\n");
  630. ndev->db_count = msix_min;
  631. ndev->msix_vec_count = msix_max;
  632. return 0;
  633. err_msix_request:
  634. while (i-- > 0)
  635. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  636. pci_disable_msix(pdev);
  637. err_msix_enable:
  638. kfree(ndev->msix);
  639. err_msix_alloc:
  640. kfree(ndev->vec);
  641. err_msix_vec_alloc:
  642. ndev->msix = NULL;
  643. ndev->vec = NULL;
  644. /* Try to set up msi irq */
  645. rc = pci_enable_msi(pdev);
  646. if (rc)
  647. goto err_msi_enable;
  648. rc = request_irq(pdev->irq, ndev_irq_isr, 0,
  649. "ndev_irq_isr", ndev);
  650. if (rc)
  651. goto err_msi_request;
  652. dev_dbg(&pdev->dev, "Using msi interrupts\n");
  653. ndev->db_count = 1;
  654. ndev->msix_vec_count = 1;
  655. return 0;
  656. err_msi_request:
  657. pci_disable_msi(pdev);
  658. err_msi_enable:
  659. /* Try to set up intx irq */
  660. pci_intx(pdev, 1);
  661. rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
  662. "ndev_irq_isr", ndev);
  663. if (rc)
  664. goto err_intx_request;
  665. dev_dbg(&pdev->dev, "Using intx interrupts\n");
  666. ndev->db_count = 1;
  667. ndev->msix_vec_count = 1;
  668. return 0;
  669. err_intx_request:
  670. return rc;
  671. }
  672. static void ndev_deinit_isr(struct amd_ntb_dev *ndev)
  673. {
  674. struct pci_dev *pdev;
  675. void __iomem *mmio = ndev->self_mmio;
  676. int i;
  677. pdev = ndev->ntb.pdev;
  678. /* Mask all doorbell interrupts */
  679. ndev->db_mask = ndev->db_valid_mask;
  680. writel(ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  681. if (ndev->msix) {
  682. i = ndev->msix_vec_count;
  683. while (i--)
  684. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  685. pci_disable_msix(pdev);
  686. kfree(ndev->msix);
  687. kfree(ndev->vec);
  688. } else {
  689. free_irq(pdev->irq, ndev);
  690. if (pci_dev_msi_enabled(pdev))
  691. pci_disable_msi(pdev);
  692. else
  693. pci_intx(pdev, 0);
  694. }
  695. }
  696. static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
  697. size_t count, loff_t *offp)
  698. {
  699. struct amd_ntb_dev *ndev;
  700. void __iomem *mmio;
  701. char *buf;
  702. size_t buf_size;
  703. ssize_t ret, off;
  704. union { u64 v64; u32 v32; u16 v16; } u;
  705. ndev = filp->private_data;
  706. mmio = ndev->self_mmio;
  707. buf_size = min(count, 0x800ul);
  708. buf = kmalloc(buf_size, GFP_KERNEL);
  709. if (!buf)
  710. return -ENOMEM;
  711. off = 0;
  712. off += scnprintf(buf + off, buf_size - off,
  713. "NTB Device Information:\n");
  714. off += scnprintf(buf + off, buf_size - off,
  715. "Connection Topology -\t%s\n",
  716. ntb_topo_string(ndev->ntb.topo));
  717. off += scnprintf(buf + off, buf_size - off,
  718. "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
  719. if (!amd_link_is_up(ndev)) {
  720. off += scnprintf(buf + off, buf_size - off,
  721. "Link Status -\t\tDown\n");
  722. } else {
  723. off += scnprintf(buf + off, buf_size - off,
  724. "Link Status -\t\tUp\n");
  725. off += scnprintf(buf + off, buf_size - off,
  726. "Link Speed -\t\tPCI-E Gen %u\n",
  727. NTB_LNK_STA_SPEED(ndev->lnk_sta));
  728. off += scnprintf(buf + off, buf_size - off,
  729. "Link Width -\t\tx%u\n",
  730. NTB_LNK_STA_WIDTH(ndev->lnk_sta));
  731. }
  732. off += scnprintf(buf + off, buf_size - off,
  733. "Memory Window Count -\t%u\n", ndev->mw_count);
  734. off += scnprintf(buf + off, buf_size - off,
  735. "Scratchpad Count -\t%u\n", ndev->spad_count);
  736. off += scnprintf(buf + off, buf_size - off,
  737. "Doorbell Count -\t%u\n", ndev->db_count);
  738. off += scnprintf(buf + off, buf_size - off,
  739. "MSIX Vector Count -\t%u\n", ndev->msix_vec_count);
  740. off += scnprintf(buf + off, buf_size - off,
  741. "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
  742. u.v32 = readl(ndev->self_mmio + AMD_DBMASK_OFFSET);
  743. off += scnprintf(buf + off, buf_size - off,
  744. "Doorbell Mask -\t\t\t%#06x\n", u.v32);
  745. u.v32 = readl(mmio + AMD_DBSTAT_OFFSET);
  746. off += scnprintf(buf + off, buf_size - off,
  747. "Doorbell Bell -\t\t\t%#06x\n", u.v32);
  748. off += scnprintf(buf + off, buf_size - off,
  749. "\nNTB Incoming XLAT:\n");
  750. u.v64 = read64(mmio + AMD_BAR1XLAT_OFFSET);
  751. off += scnprintf(buf + off, buf_size - off,
  752. "XLAT1 -\t\t%#018llx\n", u.v64);
  753. u.v64 = read64(ndev->self_mmio + AMD_BAR23XLAT_OFFSET);
  754. off += scnprintf(buf + off, buf_size - off,
  755. "XLAT23 -\t\t%#018llx\n", u.v64);
  756. u.v64 = read64(ndev->self_mmio + AMD_BAR45XLAT_OFFSET);
  757. off += scnprintf(buf + off, buf_size - off,
  758. "XLAT45 -\t\t%#018llx\n", u.v64);
  759. u.v32 = readl(mmio + AMD_BAR1LMT_OFFSET);
  760. off += scnprintf(buf + off, buf_size - off,
  761. "LMT1 -\t\t\t%#06x\n", u.v32);
  762. u.v64 = read64(ndev->self_mmio + AMD_BAR23LMT_OFFSET);
  763. off += scnprintf(buf + off, buf_size - off,
  764. "LMT23 -\t\t\t%#018llx\n", u.v64);
  765. u.v64 = read64(ndev->self_mmio + AMD_BAR45LMT_OFFSET);
  766. off += scnprintf(buf + off, buf_size - off,
  767. "LMT45 -\t\t\t%#018llx\n", u.v64);
  768. ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
  769. kfree(buf);
  770. return ret;
  771. }
  772. static void ndev_init_debugfs(struct amd_ntb_dev *ndev)
  773. {
  774. if (!debugfs_dir) {
  775. ndev->debugfs_dir = NULL;
  776. ndev->debugfs_info = NULL;
  777. } else {
  778. ndev->debugfs_dir =
  779. debugfs_create_dir(pci_name(ndev->ntb.pdev),
  780. debugfs_dir);
  781. ndev->debugfs_info =
  782. debugfs_create_file("info", S_IRUSR,
  783. ndev->debugfs_dir, ndev,
  784. &amd_ntb_debugfs_info);
  785. }
  786. }
  787. static void ndev_deinit_debugfs(struct amd_ntb_dev *ndev)
  788. {
  789. debugfs_remove_recursive(ndev->debugfs_dir);
  790. }
  791. static inline void ndev_init_struct(struct amd_ntb_dev *ndev,
  792. struct pci_dev *pdev)
  793. {
  794. ndev->ntb.pdev = pdev;
  795. ndev->ntb.topo = NTB_TOPO_NONE;
  796. ndev->ntb.ops = &amd_ntb_ops;
  797. ndev->int_mask = AMD_EVENT_INTMASK;
  798. spin_lock_init(&ndev->db_mask_lock);
  799. }
  800. static int amd_poll_link(struct amd_ntb_dev *ndev)
  801. {
  802. void __iomem *mmio = ndev->peer_mmio;
  803. u32 reg;
  804. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  805. reg &= AMD_SIDE_READY;
  806. dev_dbg(&ndev->ntb.pdev->dev, "%s: reg_val = 0x%x.\n", __func__, reg);
  807. ndev->cntl_sta = reg;
  808. amd_ntb_get_link_status(ndev);
  809. return ndev->cntl_sta;
  810. }
  811. static void amd_link_hb(struct work_struct *work)
  812. {
  813. struct amd_ntb_dev *ndev = hb_ndev(work);
  814. if (amd_poll_link(ndev))
  815. ntb_link_event(&ndev->ntb);
  816. if (!amd_link_is_up(ndev))
  817. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  818. }
  819. static int amd_init_isr(struct amd_ntb_dev *ndev)
  820. {
  821. return ndev_init_isr(ndev, AMD_DB_CNT, AMD_MSIX_VECTOR_CNT);
  822. }
  823. static void amd_set_side_info_reg(struct amd_ntb_dev *ndev, bool peer)
  824. {
  825. void __iomem *mmio = NULL;
  826. unsigned int reg;
  827. if (peer)
  828. mmio = ndev->peer_mmio;
  829. else
  830. mmio = ndev->self_mmio;
  831. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  832. if (!(reg & AMD_SIDE_READY)) {
  833. reg |= AMD_SIDE_READY;
  834. writel(reg, mmio + AMD_SIDEINFO_OFFSET);
  835. }
  836. }
  837. static void amd_clear_side_info_reg(struct amd_ntb_dev *ndev, bool peer)
  838. {
  839. void __iomem *mmio = NULL;
  840. unsigned int reg;
  841. if (peer)
  842. mmio = ndev->peer_mmio;
  843. else
  844. mmio = ndev->self_mmio;
  845. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  846. if (reg & AMD_SIDE_READY) {
  847. reg &= ~AMD_SIDE_READY;
  848. writel(reg, mmio + AMD_SIDEINFO_OFFSET);
  849. readl(mmio + AMD_SIDEINFO_OFFSET);
  850. }
  851. }
  852. static void amd_init_side_info(struct amd_ntb_dev *ndev)
  853. {
  854. void __iomem *mmio = ndev->self_mmio;
  855. u32 ntb_ctl;
  856. amd_set_side_info_reg(ndev, false);
  857. ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
  858. ntb_ctl |= (PMM_REG_CTL | SMM_REG_CTL);
  859. writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
  860. }
  861. static void amd_deinit_side_info(struct amd_ntb_dev *ndev)
  862. {
  863. void __iomem *mmio = ndev->self_mmio;
  864. u32 ntb_ctl;
  865. amd_clear_side_info_reg(ndev, false);
  866. ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
  867. ntb_ctl &= ~(PMM_REG_CTL | SMM_REG_CTL);
  868. writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
  869. }
  870. static int amd_init_ntb(struct amd_ntb_dev *ndev)
  871. {
  872. void __iomem *mmio = ndev->self_mmio;
  873. ndev->mw_count = ndev->dev_data->mw_count;
  874. ndev->spad_count = AMD_SPADS_CNT;
  875. ndev->db_count = AMD_DB_CNT;
  876. switch (ndev->ntb.topo) {
  877. case NTB_TOPO_PRI:
  878. case NTB_TOPO_SEC:
  879. ndev->spad_count >>= 1;
  880. if (ndev->ntb.topo == NTB_TOPO_PRI) {
  881. ndev->self_spad = 0;
  882. ndev->peer_spad = 0x20;
  883. } else {
  884. ndev->self_spad = 0x20;
  885. ndev->peer_spad = 0;
  886. }
  887. INIT_DELAYED_WORK(&ndev->hb_timer, amd_link_hb);
  888. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  889. break;
  890. default:
  891. dev_err(&ndev->ntb.pdev->dev,
  892. "AMD NTB does not support B2B mode.\n");
  893. return -EINVAL;
  894. }
  895. /* Mask event interrupts */
  896. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  897. return 0;
  898. }
  899. static enum ntb_topo amd_get_topo(struct amd_ntb_dev *ndev)
  900. {
  901. void __iomem *mmio = ndev->self_mmio;
  902. u32 info;
  903. info = readl(mmio + AMD_SIDEINFO_OFFSET);
  904. if (info & AMD_SIDE_MASK)
  905. return NTB_TOPO_SEC;
  906. else
  907. return NTB_TOPO_PRI;
  908. }
  909. static int amd_init_dev(struct amd_ntb_dev *ndev)
  910. {
  911. void __iomem *mmio = ndev->self_mmio;
  912. struct pci_dev *pdev;
  913. int rc = 0;
  914. pdev = ndev->ntb.pdev;
  915. ndev->ntb.topo = amd_get_topo(ndev);
  916. dev_dbg(&pdev->dev, "AMD NTB topo is %s\n",
  917. ntb_topo_string(ndev->ntb.topo));
  918. rc = amd_init_ntb(ndev);
  919. if (rc)
  920. return rc;
  921. rc = amd_init_isr(ndev);
  922. if (rc) {
  923. dev_err(&pdev->dev, "fail to init isr.\n");
  924. return rc;
  925. }
  926. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  927. /*
  928. * We reserve the highest order bit of the DB register which will
  929. * be used to notify peer when the driver on this side is being
  930. * un-loaded.
  931. */
  932. ndev->db_last_bit =
  933. find_last_bit((unsigned long *)&ndev->db_valid_mask,
  934. hweight64(ndev->db_valid_mask));
  935. writew((u16)~BIT(ndev->db_last_bit), mmio + AMD_DBMASK_OFFSET);
  936. /*
  937. * Since now there is one less bit to account for, the DB count
  938. * and DB mask should be adjusted accordingly.
  939. */
  940. ndev->db_count -= 1;
  941. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  942. /* Enable Link-Up and Link-Down event interrupts */
  943. ndev->int_mask &= ~(AMD_LINK_UP_EVENT | AMD_LINK_DOWN_EVENT);
  944. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  945. return 0;
  946. }
  947. static void amd_deinit_dev(struct amd_ntb_dev *ndev)
  948. {
  949. cancel_delayed_work_sync(&ndev->hb_timer);
  950. ndev_deinit_isr(ndev);
  951. }
  952. static int amd_ntb_init_pci(struct amd_ntb_dev *ndev,
  953. struct pci_dev *pdev)
  954. {
  955. int rc;
  956. pci_set_drvdata(pdev, ndev);
  957. rc = pci_enable_device(pdev);
  958. if (rc)
  959. goto err_pci_enable;
  960. rc = pci_request_regions(pdev, NTB_NAME);
  961. if (rc)
  962. goto err_pci_regions;
  963. pci_set_master(pdev);
  964. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  965. if (rc) {
  966. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  967. if (rc)
  968. goto err_dma_mask;
  969. dev_warn(&pdev->dev, "Cannot DMA highmem\n");
  970. }
  971. ndev->self_mmio = pci_iomap(pdev, 0, 0);
  972. if (!ndev->self_mmio) {
  973. rc = -EIO;
  974. goto err_dma_mask;
  975. }
  976. ndev->peer_mmio = ndev->self_mmio + AMD_PEER_OFFSET;
  977. return 0;
  978. err_dma_mask:
  979. pci_release_regions(pdev);
  980. err_pci_regions:
  981. pci_disable_device(pdev);
  982. err_pci_enable:
  983. pci_set_drvdata(pdev, NULL);
  984. return rc;
  985. }
  986. static void amd_ntb_deinit_pci(struct amd_ntb_dev *ndev)
  987. {
  988. struct pci_dev *pdev = ndev->ntb.pdev;
  989. pci_iounmap(pdev, ndev->self_mmio);
  990. pci_release_regions(pdev);
  991. pci_disable_device(pdev);
  992. pci_set_drvdata(pdev, NULL);
  993. }
  994. static int amd_ntb_pci_probe(struct pci_dev *pdev,
  995. const struct pci_device_id *id)
  996. {
  997. struct amd_ntb_dev *ndev;
  998. int rc, node;
  999. node = dev_to_node(&pdev->dev);
  1000. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  1001. if (!ndev) {
  1002. rc = -ENOMEM;
  1003. goto err_ndev;
  1004. }
  1005. ndev->dev_data = (struct ntb_dev_data *)id->driver_data;
  1006. ndev_init_struct(ndev, pdev);
  1007. rc = amd_ntb_init_pci(ndev, pdev);
  1008. if (rc)
  1009. goto err_init_pci;
  1010. rc = amd_init_dev(ndev);
  1011. if (rc)
  1012. goto err_init_dev;
  1013. /* write side info */
  1014. amd_init_side_info(ndev);
  1015. amd_poll_link(ndev);
  1016. ndev_init_debugfs(ndev);
  1017. rc = ntb_register_device(&ndev->ntb);
  1018. if (rc)
  1019. goto err_register;
  1020. dev_info(&pdev->dev, "NTB device registered.\n");
  1021. return 0;
  1022. err_register:
  1023. ndev_deinit_debugfs(ndev);
  1024. amd_deinit_dev(ndev);
  1025. err_init_dev:
  1026. amd_ntb_deinit_pci(ndev);
  1027. err_init_pci:
  1028. kfree(ndev);
  1029. err_ndev:
  1030. return rc;
  1031. }
  1032. static void amd_ntb_pci_remove(struct pci_dev *pdev)
  1033. {
  1034. struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
  1035. /*
  1036. * Clear the READY bit in SIDEINFO register before sending DB event
  1037. * to the peer. This will make sure that when the peer handles the
  1038. * DB event, it correctly reads this bit as being 0.
  1039. */
  1040. amd_deinit_side_info(ndev);
  1041. ntb_peer_db_set(&ndev->ntb, BIT_ULL(ndev->db_last_bit));
  1042. ntb_unregister_device(&ndev->ntb);
  1043. ndev_deinit_debugfs(ndev);
  1044. amd_deinit_dev(ndev);
  1045. amd_ntb_deinit_pci(ndev);
  1046. kfree(ndev);
  1047. }
  1048. static void amd_ntb_pci_shutdown(struct pci_dev *pdev)
  1049. {
  1050. struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
  1051. /* Send link down notification */
  1052. ntb_link_event(&ndev->ntb);
  1053. amd_deinit_side_info(ndev);
  1054. ntb_peer_db_set(&ndev->ntb, BIT_ULL(ndev->db_last_bit));
  1055. ntb_unregister_device(&ndev->ntb);
  1056. ndev_deinit_debugfs(ndev);
  1057. amd_deinit_dev(ndev);
  1058. amd_ntb_deinit_pci(ndev);
  1059. kfree(ndev);
  1060. }
  1061. static const struct file_operations amd_ntb_debugfs_info = {
  1062. .owner = THIS_MODULE,
  1063. .open = simple_open,
  1064. .read = ndev_debugfs_read,
  1065. };
  1066. static const struct ntb_dev_data dev_data[] = {
  1067. { /* for device 145b */
  1068. .mw_count = 3,
  1069. .mw_idx = 1,
  1070. },
  1071. { /* for device 148b */
  1072. .mw_count = 2,
  1073. .mw_idx = 2,
  1074. },
  1075. { /* for device 0x17d7 */
  1076. .mw_count = 2,
  1077. .mw_idx = 2,
  1078. .is_endpoint = true,
  1079. },
  1080. };
  1081. static const struct pci_device_id amd_ntb_pci_tbl[] = {
  1082. { PCI_VDEVICE(AMD, 0x145b), (kernel_ulong_t)&dev_data[0] },
  1083. { PCI_VDEVICE(AMD, 0x148b), (kernel_ulong_t)&dev_data[1] },
  1084. { PCI_VDEVICE(AMD, 0x14c0), (kernel_ulong_t)&dev_data[1] },
  1085. { PCI_VDEVICE(AMD, 0x14c3), (kernel_ulong_t)&dev_data[1] },
  1086. { PCI_VDEVICE(AMD, 0x155a), (kernel_ulong_t)&dev_data[1] },
  1087. { PCI_VDEVICE(AMD, 0x17d4), (kernel_ulong_t)&dev_data[1] },
  1088. { PCI_VDEVICE(AMD, 0x17d7), (kernel_ulong_t)&dev_data[2] },
  1089. { PCI_VDEVICE(HYGON, 0x145b), (kernel_ulong_t)&dev_data[0] },
  1090. { 0, }
  1091. };
  1092. MODULE_DEVICE_TABLE(pci, amd_ntb_pci_tbl);
  1093. static struct pci_driver amd_ntb_pci_driver = {
  1094. .name = KBUILD_MODNAME,
  1095. .id_table = amd_ntb_pci_tbl,
  1096. .probe = amd_ntb_pci_probe,
  1097. .remove = amd_ntb_pci_remove,
  1098. .shutdown = amd_ntb_pci_shutdown,
  1099. };
  1100. static int __init amd_ntb_pci_driver_init(void)
  1101. {
  1102. int ret;
  1103. pr_info("%s %s\n", NTB_DESC, NTB_VER);
  1104. if (debugfs_initialized())
  1105. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  1106. ret = pci_register_driver(&amd_ntb_pci_driver);
  1107. if (ret)
  1108. debugfs_remove_recursive(debugfs_dir);
  1109. return ret;
  1110. }
  1111. module_init(amd_ntb_pci_driver_init);
  1112. static void __exit amd_ntb_pci_driver_exit(void)
  1113. {
  1114. pci_unregister_driver(&amd_ntb_pci_driver);
  1115. debugfs_remove_recursive(debugfs_dir);
  1116. }
  1117. module_exit(amd_ntb_pci_driver_exit);