ntb_hw_amd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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) << 3);
  120. limit_reg = AMD_BAR23LMT_OFFSET + ((bar - 2) << 3);
  121. /* Set the limit if supported */
  122. limit = base_addr + 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. /* split bar addr range must all be 32 bit */
  142. if (addr & (~0ull << 32))
  143. return -EINVAL;
  144. if ((addr + size) & (~0ull << 32))
  145. return -EINVAL;
  146. /* Set the limit if supported */
  147. limit = base_addr + 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, mmio + limit_reg);
  157. reg_val = readl(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_link_is_up(struct amd_ntb_dev *ndev)
  167. {
  168. if (!ndev->peer_sta)
  169. return NTB_LNK_STA_ACTIVE(ndev->cntl_sta);
  170. /* If peer_sta is reset or D0 event, the ISR has
  171. * started a timer to check link status of hardware.
  172. * So here just clear status bit. And if peer_sta is
  173. * D3 or PME_TO, D0/reset event will be happened when
  174. * system wakeup/poweron, so do nothing here.
  175. */
  176. if (ndev->peer_sta & AMD_PEER_RESET_EVENT)
  177. ndev->peer_sta &= ~AMD_PEER_RESET_EVENT;
  178. else if (ndev->peer_sta & AMD_PEER_D0_EVENT)
  179. ndev->peer_sta = 0;
  180. return 0;
  181. }
  182. static int amd_ntb_link_is_up(struct ntb_dev *ntb,
  183. enum ntb_speed *speed,
  184. enum ntb_width *width)
  185. {
  186. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  187. int ret = 0;
  188. if (amd_link_is_up(ndev)) {
  189. if (speed)
  190. *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
  191. if (width)
  192. *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
  193. dev_dbg(ndev_dev(ndev), "link is up.\n");
  194. ret = 1;
  195. } else {
  196. if (speed)
  197. *speed = NTB_SPEED_NONE;
  198. if (width)
  199. *width = NTB_WIDTH_NONE;
  200. dev_dbg(ndev_dev(ndev), "link is down.\n");
  201. }
  202. return ret;
  203. }
  204. static int amd_ntb_link_enable(struct ntb_dev *ntb,
  205. enum ntb_speed max_speed,
  206. enum ntb_width max_width)
  207. {
  208. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  209. void __iomem *mmio = ndev->self_mmio;
  210. u32 ntb_ctl;
  211. /* Enable event interrupt */
  212. ndev->int_mask &= ~AMD_EVENT_INTMASK;
  213. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  214. if (ndev->ntb.topo == NTB_TOPO_SEC)
  215. return -EINVAL;
  216. dev_dbg(ndev_dev(ndev), "Enabling Link.\n");
  217. ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
  218. ntb_ctl |= (PMM_REG_CTL | SMM_REG_CTL);
  219. writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
  220. return 0;
  221. }
  222. static int amd_ntb_link_disable(struct ntb_dev *ntb)
  223. {
  224. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  225. void __iomem *mmio = ndev->self_mmio;
  226. u32 ntb_ctl;
  227. /* Disable event interrupt */
  228. ndev->int_mask |= AMD_EVENT_INTMASK;
  229. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  230. if (ndev->ntb.topo == NTB_TOPO_SEC)
  231. return -EINVAL;
  232. dev_dbg(ndev_dev(ndev), "Enabling Link.\n");
  233. ntb_ctl = readl(mmio + AMD_CNTL_OFFSET);
  234. ntb_ctl &= ~(PMM_REG_CTL | SMM_REG_CTL);
  235. writel(ntb_ctl, mmio + AMD_CNTL_OFFSET);
  236. return 0;
  237. }
  238. static u64 amd_ntb_db_valid_mask(struct ntb_dev *ntb)
  239. {
  240. return ntb_ndev(ntb)->db_valid_mask;
  241. }
  242. static int amd_ntb_db_vector_count(struct ntb_dev *ntb)
  243. {
  244. return ntb_ndev(ntb)->db_count;
  245. }
  246. static u64 amd_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
  247. {
  248. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  249. if (db_vector < 0 || db_vector > ndev->db_count)
  250. return 0;
  251. return ntb_ndev(ntb)->db_valid_mask & (1 << db_vector);
  252. }
  253. static u64 amd_ntb_db_read(struct ntb_dev *ntb)
  254. {
  255. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  256. void __iomem *mmio = ndev->self_mmio;
  257. return (u64)readw(mmio + AMD_DBSTAT_OFFSET);
  258. }
  259. static int amd_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
  260. {
  261. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  262. void __iomem *mmio = ndev->self_mmio;
  263. writew((u16)db_bits, mmio + AMD_DBSTAT_OFFSET);
  264. return 0;
  265. }
  266. static int amd_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
  267. {
  268. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  269. void __iomem *mmio = ndev->self_mmio;
  270. unsigned long flags;
  271. if (db_bits & ~ndev->db_valid_mask)
  272. return -EINVAL;
  273. spin_lock_irqsave(&ndev->db_mask_lock, flags);
  274. ndev->db_mask |= db_bits;
  275. writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  276. spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
  277. return 0;
  278. }
  279. static int amd_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
  280. {
  281. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  282. void __iomem *mmio = ndev->self_mmio;
  283. unsigned long flags;
  284. if (db_bits & ~ndev->db_valid_mask)
  285. return -EINVAL;
  286. spin_lock_irqsave(&ndev->db_mask_lock, flags);
  287. ndev->db_mask &= ~db_bits;
  288. writew((u16)ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  289. spin_unlock_irqrestore(&ndev->db_mask_lock, flags);
  290. return 0;
  291. }
  292. static int amd_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
  293. {
  294. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  295. void __iomem *mmio = ndev->self_mmio;
  296. writew((u16)db_bits, mmio + AMD_DBREQ_OFFSET);
  297. return 0;
  298. }
  299. static int amd_ntb_spad_count(struct ntb_dev *ntb)
  300. {
  301. return ntb_ndev(ntb)->spad_count;
  302. }
  303. static u32 amd_ntb_spad_read(struct ntb_dev *ntb, int idx)
  304. {
  305. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  306. void __iomem *mmio = ndev->self_mmio;
  307. u32 offset;
  308. if (idx < 0 || idx >= ndev->spad_count)
  309. return 0;
  310. offset = ndev->self_spad + (idx << 2);
  311. return readl(mmio + AMD_SPAD_OFFSET + offset);
  312. }
  313. static int amd_ntb_spad_write(struct ntb_dev *ntb,
  314. int idx, u32 val)
  315. {
  316. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  317. void __iomem *mmio = ndev->self_mmio;
  318. u32 offset;
  319. if (idx < 0 || idx >= ndev->spad_count)
  320. return -EINVAL;
  321. offset = ndev->self_spad + (idx << 2);
  322. writel(val, mmio + AMD_SPAD_OFFSET + offset);
  323. return 0;
  324. }
  325. static u32 amd_ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
  326. {
  327. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  328. void __iomem *mmio = ndev->self_mmio;
  329. u32 offset;
  330. if (idx < 0 || idx >= ndev->spad_count)
  331. return -EINVAL;
  332. offset = ndev->peer_spad + (idx << 2);
  333. return readl(mmio + AMD_SPAD_OFFSET + offset);
  334. }
  335. static int amd_ntb_peer_spad_write(struct ntb_dev *ntb,
  336. int idx, u32 val)
  337. {
  338. struct amd_ntb_dev *ndev = ntb_ndev(ntb);
  339. void __iomem *mmio = ndev->self_mmio;
  340. u32 offset;
  341. if (idx < 0 || idx >= ndev->spad_count)
  342. return -EINVAL;
  343. offset = ndev->peer_spad + (idx << 2);
  344. writel(val, mmio + AMD_SPAD_OFFSET + offset);
  345. return 0;
  346. }
  347. static const struct ntb_dev_ops amd_ntb_ops = {
  348. .mw_count = amd_ntb_mw_count,
  349. .mw_get_range = amd_ntb_mw_get_range,
  350. .mw_set_trans = amd_ntb_mw_set_trans,
  351. .link_is_up = amd_ntb_link_is_up,
  352. .link_enable = amd_ntb_link_enable,
  353. .link_disable = amd_ntb_link_disable,
  354. .db_valid_mask = amd_ntb_db_valid_mask,
  355. .db_vector_count = amd_ntb_db_vector_count,
  356. .db_vector_mask = amd_ntb_db_vector_mask,
  357. .db_read = amd_ntb_db_read,
  358. .db_clear = amd_ntb_db_clear,
  359. .db_set_mask = amd_ntb_db_set_mask,
  360. .db_clear_mask = amd_ntb_db_clear_mask,
  361. .peer_db_set = amd_ntb_peer_db_set,
  362. .spad_count = amd_ntb_spad_count,
  363. .spad_read = amd_ntb_spad_read,
  364. .spad_write = amd_ntb_spad_write,
  365. .peer_spad_read = amd_ntb_peer_spad_read,
  366. .peer_spad_write = amd_ntb_peer_spad_write,
  367. };
  368. static void amd_ack_smu(struct amd_ntb_dev *ndev, u32 bit)
  369. {
  370. void __iomem *mmio = ndev->self_mmio;
  371. int reg;
  372. reg = readl(mmio + AMD_SMUACK_OFFSET);
  373. reg |= bit;
  374. writel(reg, mmio + AMD_SMUACK_OFFSET);
  375. ndev->peer_sta |= bit;
  376. }
  377. static void amd_handle_event(struct amd_ntb_dev *ndev, int vec)
  378. {
  379. void __iomem *mmio = ndev->self_mmio;
  380. u32 status;
  381. status = readl(mmio + AMD_INTSTAT_OFFSET);
  382. if (!(status & AMD_EVENT_INTMASK))
  383. return;
  384. dev_dbg(ndev_dev(ndev), "status = 0x%x and vec = %d\n", status, vec);
  385. status &= AMD_EVENT_INTMASK;
  386. switch (status) {
  387. case AMD_PEER_FLUSH_EVENT:
  388. dev_info(ndev_dev(ndev), "Flush is done.\n");
  389. break;
  390. case AMD_PEER_RESET_EVENT:
  391. amd_ack_smu(ndev, AMD_PEER_RESET_EVENT);
  392. /* link down first */
  393. ntb_link_event(&ndev->ntb);
  394. /* polling peer status */
  395. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  396. break;
  397. case AMD_PEER_D3_EVENT:
  398. case AMD_PEER_PMETO_EVENT:
  399. amd_ack_smu(ndev, status);
  400. /* link down */
  401. ntb_link_event(&ndev->ntb);
  402. break;
  403. case AMD_PEER_D0_EVENT:
  404. mmio = ndev->peer_mmio;
  405. status = readl(mmio + AMD_PMESTAT_OFFSET);
  406. /* check if this is WAKEUP event */
  407. if (status & 0x1)
  408. dev_info(ndev_dev(ndev), "Wakeup is done.\n");
  409. amd_ack_smu(ndev, AMD_PEER_D0_EVENT);
  410. /* start a timer to poll link status */
  411. schedule_delayed_work(&ndev->hb_timer,
  412. AMD_LINK_HB_TIMEOUT);
  413. break;
  414. default:
  415. dev_info(ndev_dev(ndev), "event status = 0x%x.\n", status);
  416. break;
  417. }
  418. }
  419. static irqreturn_t ndev_interrupt(struct amd_ntb_dev *ndev, int vec)
  420. {
  421. dev_dbg(ndev_dev(ndev), "vec %d\n", vec);
  422. if (vec > (AMD_DB_CNT - 1) || (ndev->msix_vec_count == 1))
  423. amd_handle_event(ndev, vec);
  424. if (vec < AMD_DB_CNT)
  425. ntb_db_event(&ndev->ntb, vec);
  426. return IRQ_HANDLED;
  427. }
  428. static irqreturn_t ndev_vec_isr(int irq, void *dev)
  429. {
  430. struct amd_ntb_vec *nvec = dev;
  431. return ndev_interrupt(nvec->ndev, nvec->num);
  432. }
  433. static irqreturn_t ndev_irq_isr(int irq, void *dev)
  434. {
  435. struct amd_ntb_dev *ndev = dev;
  436. return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
  437. }
  438. static int ndev_init_isr(struct amd_ntb_dev *ndev,
  439. int msix_min, int msix_max)
  440. {
  441. struct pci_dev *pdev;
  442. int rc, i, msix_count, node;
  443. pdev = ndev_pdev(ndev);
  444. node = dev_to_node(&pdev->dev);
  445. ndev->db_mask = ndev->db_valid_mask;
  446. /* Try to set up msix irq */
  447. ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
  448. GFP_KERNEL, node);
  449. if (!ndev->vec)
  450. goto err_msix_vec_alloc;
  451. ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
  452. GFP_KERNEL, node);
  453. if (!ndev->msix)
  454. goto err_msix_alloc;
  455. for (i = 0; i < msix_max; ++i)
  456. ndev->msix[i].entry = i;
  457. msix_count = pci_enable_msix_range(pdev, ndev->msix,
  458. msix_min, msix_max);
  459. if (msix_count < 0)
  460. goto err_msix_enable;
  461. /* NOTE: Disable MSIX if msix count is less than 16 because of
  462. * hardware limitation.
  463. */
  464. if (msix_count < msix_min) {
  465. pci_disable_msix(pdev);
  466. goto err_msix_enable;
  467. }
  468. for (i = 0; i < msix_count; ++i) {
  469. ndev->vec[i].ndev = ndev;
  470. ndev->vec[i].num = i;
  471. rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
  472. "ndev_vec_isr", &ndev->vec[i]);
  473. if (rc)
  474. goto err_msix_request;
  475. }
  476. dev_dbg(ndev_dev(ndev), "Using msix interrupts\n");
  477. ndev->db_count = msix_min;
  478. ndev->msix_vec_count = msix_max;
  479. return 0;
  480. err_msix_request:
  481. while (i-- > 0)
  482. free_irq(ndev->msix[i].vector, ndev);
  483. pci_disable_msix(pdev);
  484. err_msix_enable:
  485. kfree(ndev->msix);
  486. err_msix_alloc:
  487. kfree(ndev->vec);
  488. err_msix_vec_alloc:
  489. ndev->msix = NULL;
  490. ndev->vec = NULL;
  491. /* Try to set up msi irq */
  492. rc = pci_enable_msi(pdev);
  493. if (rc)
  494. goto err_msi_enable;
  495. rc = request_irq(pdev->irq, ndev_irq_isr, 0,
  496. "ndev_irq_isr", ndev);
  497. if (rc)
  498. goto err_msi_request;
  499. dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
  500. ndev->db_count = 1;
  501. ndev->msix_vec_count = 1;
  502. return 0;
  503. err_msi_request:
  504. pci_disable_msi(pdev);
  505. err_msi_enable:
  506. /* Try to set up intx irq */
  507. pci_intx(pdev, 1);
  508. rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
  509. "ndev_irq_isr", ndev);
  510. if (rc)
  511. goto err_intx_request;
  512. dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
  513. ndev->db_count = 1;
  514. ndev->msix_vec_count = 1;
  515. return 0;
  516. err_intx_request:
  517. return rc;
  518. }
  519. static void ndev_deinit_isr(struct amd_ntb_dev *ndev)
  520. {
  521. struct pci_dev *pdev;
  522. void __iomem *mmio = ndev->self_mmio;
  523. int i;
  524. pdev = ndev_pdev(ndev);
  525. /* Mask all doorbell interrupts */
  526. ndev->db_mask = ndev->db_valid_mask;
  527. writel(ndev->db_mask, mmio + AMD_DBMASK_OFFSET);
  528. if (ndev->msix) {
  529. i = ndev->msix_vec_count;
  530. while (i--)
  531. free_irq(ndev->msix[i].vector, &ndev->vec[i]);
  532. pci_disable_msix(pdev);
  533. kfree(ndev->msix);
  534. kfree(ndev->vec);
  535. } else {
  536. free_irq(pdev->irq, ndev);
  537. if (pci_dev_msi_enabled(pdev))
  538. pci_disable_msi(pdev);
  539. else
  540. pci_intx(pdev, 0);
  541. }
  542. }
  543. static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
  544. size_t count, loff_t *offp)
  545. {
  546. struct amd_ntb_dev *ndev;
  547. void __iomem *mmio;
  548. char *buf;
  549. size_t buf_size;
  550. ssize_t ret, off;
  551. union { u64 v64; u32 v32; u16 v16; } u;
  552. ndev = filp->private_data;
  553. mmio = ndev->self_mmio;
  554. buf_size = min(count, 0x800ul);
  555. buf = kmalloc(buf_size, GFP_KERNEL);
  556. if (!buf)
  557. return -ENOMEM;
  558. off = 0;
  559. off += scnprintf(buf + off, buf_size - off,
  560. "NTB Device Information:\n");
  561. off += scnprintf(buf + off, buf_size - off,
  562. "Connection Topology -\t%s\n",
  563. ntb_topo_string(ndev->ntb.topo));
  564. off += scnprintf(buf + off, buf_size - off,
  565. "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
  566. if (!amd_link_is_up(ndev)) {
  567. off += scnprintf(buf + off, buf_size - off,
  568. "Link Status -\t\tDown\n");
  569. } else {
  570. off += scnprintf(buf + off, buf_size - off,
  571. "Link Status -\t\tUp\n");
  572. off += scnprintf(buf + off, buf_size - off,
  573. "Link Speed -\t\tPCI-E Gen %u\n",
  574. NTB_LNK_STA_SPEED(ndev->lnk_sta));
  575. off += scnprintf(buf + off, buf_size - off,
  576. "Link Width -\t\tx%u\n",
  577. NTB_LNK_STA_WIDTH(ndev->lnk_sta));
  578. }
  579. off += scnprintf(buf + off, buf_size - off,
  580. "Memory Window Count -\t%u\n", ndev->mw_count);
  581. off += scnprintf(buf + off, buf_size - off,
  582. "Scratchpad Count -\t%u\n", ndev->spad_count);
  583. off += scnprintf(buf + off, buf_size - off,
  584. "Doorbell Count -\t%u\n", ndev->db_count);
  585. off += scnprintf(buf + off, buf_size - off,
  586. "MSIX Vector Count -\t%u\n", ndev->msix_vec_count);
  587. off += scnprintf(buf + off, buf_size - off,
  588. "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
  589. u.v32 = readl(ndev->self_mmio + AMD_DBMASK_OFFSET);
  590. off += scnprintf(buf + off, buf_size - off,
  591. "Doorbell Mask -\t\t\t%#06x\n", u.v32);
  592. u.v32 = readl(mmio + AMD_DBSTAT_OFFSET);
  593. off += scnprintf(buf + off, buf_size - off,
  594. "Doorbell Bell -\t\t\t%#06x\n", u.v32);
  595. off += scnprintf(buf + off, buf_size - off,
  596. "\nNTB Incoming XLAT:\n");
  597. u.v64 = read64(mmio + AMD_BAR1XLAT_OFFSET);
  598. off += scnprintf(buf + off, buf_size - off,
  599. "XLAT1 -\t\t%#018llx\n", u.v64);
  600. u.v64 = read64(ndev->self_mmio + AMD_BAR23XLAT_OFFSET);
  601. off += scnprintf(buf + off, buf_size - off,
  602. "XLAT23 -\t\t%#018llx\n", u.v64);
  603. u.v64 = read64(ndev->self_mmio + AMD_BAR45XLAT_OFFSET);
  604. off += scnprintf(buf + off, buf_size - off,
  605. "XLAT45 -\t\t%#018llx\n", u.v64);
  606. u.v32 = readl(mmio + AMD_BAR1LMT_OFFSET);
  607. off += scnprintf(buf + off, buf_size - off,
  608. "LMT1 -\t\t\t%#06x\n", u.v32);
  609. u.v64 = read64(ndev->self_mmio + AMD_BAR23LMT_OFFSET);
  610. off += scnprintf(buf + off, buf_size - off,
  611. "LMT23 -\t\t\t%#018llx\n", u.v64);
  612. u.v64 = read64(ndev->self_mmio + AMD_BAR45LMT_OFFSET);
  613. off += scnprintf(buf + off, buf_size - off,
  614. "LMT45 -\t\t\t%#018llx\n", u.v64);
  615. ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
  616. kfree(buf);
  617. return ret;
  618. }
  619. static void ndev_init_debugfs(struct amd_ntb_dev *ndev)
  620. {
  621. if (!debugfs_dir) {
  622. ndev->debugfs_dir = NULL;
  623. ndev->debugfs_info = NULL;
  624. } else {
  625. ndev->debugfs_dir =
  626. debugfs_create_dir(ndev_name(ndev), debugfs_dir);
  627. if (!ndev->debugfs_dir)
  628. ndev->debugfs_info = NULL;
  629. else
  630. ndev->debugfs_info =
  631. debugfs_create_file("info", S_IRUSR,
  632. ndev->debugfs_dir, ndev,
  633. &amd_ntb_debugfs_info);
  634. }
  635. }
  636. static void ndev_deinit_debugfs(struct amd_ntb_dev *ndev)
  637. {
  638. debugfs_remove_recursive(ndev->debugfs_dir);
  639. }
  640. static inline void ndev_init_struct(struct amd_ntb_dev *ndev,
  641. struct pci_dev *pdev)
  642. {
  643. ndev->ntb.pdev = pdev;
  644. ndev->ntb.topo = NTB_TOPO_NONE;
  645. ndev->ntb.ops = &amd_ntb_ops;
  646. ndev->int_mask = AMD_EVENT_INTMASK;
  647. spin_lock_init(&ndev->db_mask_lock);
  648. }
  649. static int amd_poll_link(struct amd_ntb_dev *ndev)
  650. {
  651. void __iomem *mmio = ndev->peer_mmio;
  652. u32 reg, stat;
  653. int rc;
  654. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  655. reg &= NTB_LIN_STA_ACTIVE_BIT;
  656. dev_dbg(ndev_dev(ndev), "%s: reg_val = 0x%x.\n", __func__, reg);
  657. if (reg == ndev->cntl_sta)
  658. return 0;
  659. ndev->cntl_sta = reg;
  660. rc = pci_read_config_dword(ndev->ntb.pdev,
  661. AMD_LINK_STATUS_OFFSET, &stat);
  662. if (rc)
  663. return 0;
  664. ndev->lnk_sta = stat;
  665. return 1;
  666. }
  667. static void amd_link_hb(struct work_struct *work)
  668. {
  669. struct amd_ntb_dev *ndev = hb_ndev(work);
  670. if (amd_poll_link(ndev))
  671. ntb_link_event(&ndev->ntb);
  672. if (!amd_link_is_up(ndev))
  673. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  674. }
  675. static int amd_init_isr(struct amd_ntb_dev *ndev)
  676. {
  677. return ndev_init_isr(ndev, AMD_DB_CNT, AMD_MSIX_VECTOR_CNT);
  678. }
  679. static void amd_init_side_info(struct amd_ntb_dev *ndev)
  680. {
  681. void __iomem *mmio = ndev->self_mmio;
  682. unsigned int reg;
  683. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  684. if (!(reg & AMD_SIDE_READY)) {
  685. reg |= AMD_SIDE_READY;
  686. writel(reg, mmio + AMD_SIDEINFO_OFFSET);
  687. }
  688. }
  689. static void amd_deinit_side_info(struct amd_ntb_dev *ndev)
  690. {
  691. void __iomem *mmio = ndev->self_mmio;
  692. unsigned int reg;
  693. reg = readl(mmio + AMD_SIDEINFO_OFFSET);
  694. if (reg & AMD_SIDE_READY) {
  695. reg &= ~AMD_SIDE_READY;
  696. writel(reg, mmio + AMD_SIDEINFO_OFFSET);
  697. readl(mmio + AMD_SIDEINFO_OFFSET);
  698. }
  699. }
  700. static int amd_init_ntb(struct amd_ntb_dev *ndev)
  701. {
  702. void __iomem *mmio = ndev->self_mmio;
  703. ndev->mw_count = AMD_MW_CNT;
  704. ndev->spad_count = AMD_SPADS_CNT;
  705. ndev->db_count = AMD_DB_CNT;
  706. switch (ndev->ntb.topo) {
  707. case NTB_TOPO_PRI:
  708. case NTB_TOPO_SEC:
  709. ndev->spad_count >>= 1;
  710. if (ndev->ntb.topo == NTB_TOPO_PRI) {
  711. ndev->self_spad = 0;
  712. ndev->peer_spad = 0x20;
  713. } else {
  714. ndev->self_spad = 0x20;
  715. ndev->peer_spad = 0;
  716. }
  717. INIT_DELAYED_WORK(&ndev->hb_timer, amd_link_hb);
  718. schedule_delayed_work(&ndev->hb_timer, AMD_LINK_HB_TIMEOUT);
  719. break;
  720. default:
  721. dev_err(ndev_dev(ndev), "AMD NTB does not support B2B mode.\n");
  722. return -EINVAL;
  723. }
  724. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  725. /* Mask event interrupts */
  726. writel(ndev->int_mask, mmio + AMD_INTMASK_OFFSET);
  727. return 0;
  728. }
  729. static enum ntb_topo amd_get_topo(struct amd_ntb_dev *ndev)
  730. {
  731. void __iomem *mmio = ndev->self_mmio;
  732. u32 info;
  733. info = readl(mmio + AMD_SIDEINFO_OFFSET);
  734. if (info & AMD_SIDE_MASK)
  735. return NTB_TOPO_SEC;
  736. else
  737. return NTB_TOPO_PRI;
  738. }
  739. static int amd_init_dev(struct amd_ntb_dev *ndev)
  740. {
  741. struct pci_dev *pdev;
  742. int rc = 0;
  743. pdev = ndev_pdev(ndev);
  744. ndev->ntb.topo = amd_get_topo(ndev);
  745. dev_dbg(ndev_dev(ndev), "AMD NTB topo is %s\n",
  746. ntb_topo_string(ndev->ntb.topo));
  747. rc = amd_init_ntb(ndev);
  748. if (rc)
  749. return rc;
  750. rc = amd_init_isr(ndev);
  751. if (rc) {
  752. dev_err(ndev_dev(ndev), "fail to init isr.\n");
  753. return rc;
  754. }
  755. ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
  756. return 0;
  757. }
  758. static void amd_deinit_dev(struct amd_ntb_dev *ndev)
  759. {
  760. cancel_delayed_work_sync(&ndev->hb_timer);
  761. ndev_deinit_isr(ndev);
  762. }
  763. static int amd_ntb_init_pci(struct amd_ntb_dev *ndev,
  764. struct pci_dev *pdev)
  765. {
  766. int rc;
  767. pci_set_drvdata(pdev, ndev);
  768. rc = pci_enable_device(pdev);
  769. if (rc)
  770. goto err_pci_enable;
  771. rc = pci_request_regions(pdev, NTB_NAME);
  772. if (rc)
  773. goto err_pci_regions;
  774. pci_set_master(pdev);
  775. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  776. if (rc) {
  777. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  778. if (rc)
  779. goto err_dma_mask;
  780. dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
  781. }
  782. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  783. if (rc) {
  784. rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  785. if (rc)
  786. goto err_dma_mask;
  787. dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
  788. }
  789. ndev->self_mmio = pci_iomap(pdev, 0, 0);
  790. if (!ndev->self_mmio) {
  791. rc = -EIO;
  792. goto err_dma_mask;
  793. }
  794. ndev->peer_mmio = ndev->self_mmio + AMD_PEER_OFFSET;
  795. return 0;
  796. err_dma_mask:
  797. pci_clear_master(pdev);
  798. err_pci_regions:
  799. pci_disable_device(pdev);
  800. err_pci_enable:
  801. pci_set_drvdata(pdev, NULL);
  802. return rc;
  803. }
  804. static void amd_ntb_deinit_pci(struct amd_ntb_dev *ndev)
  805. {
  806. struct pci_dev *pdev = ndev_pdev(ndev);
  807. pci_iounmap(pdev, ndev->self_mmio);
  808. pci_clear_master(pdev);
  809. pci_release_regions(pdev);
  810. pci_disable_device(pdev);
  811. pci_set_drvdata(pdev, NULL);
  812. }
  813. static int amd_ntb_pci_probe(struct pci_dev *pdev,
  814. const struct pci_device_id *id)
  815. {
  816. struct amd_ntb_dev *ndev;
  817. int rc, node;
  818. node = dev_to_node(&pdev->dev);
  819. ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
  820. if (!ndev) {
  821. rc = -ENOMEM;
  822. goto err_ndev;
  823. }
  824. ndev_init_struct(ndev, pdev);
  825. rc = amd_ntb_init_pci(ndev, pdev);
  826. if (rc)
  827. goto err_init_pci;
  828. rc = amd_init_dev(ndev);
  829. if (rc)
  830. goto err_init_dev;
  831. /* write side info */
  832. amd_init_side_info(ndev);
  833. amd_poll_link(ndev);
  834. ndev_init_debugfs(ndev);
  835. rc = ntb_register_device(&ndev->ntb);
  836. if (rc)
  837. goto err_register;
  838. dev_info(&pdev->dev, "NTB device registered.\n");
  839. return 0;
  840. err_register:
  841. ndev_deinit_debugfs(ndev);
  842. amd_deinit_dev(ndev);
  843. err_init_dev:
  844. amd_ntb_deinit_pci(ndev);
  845. err_init_pci:
  846. kfree(ndev);
  847. err_ndev:
  848. return rc;
  849. }
  850. static void amd_ntb_pci_remove(struct pci_dev *pdev)
  851. {
  852. struct amd_ntb_dev *ndev = pci_get_drvdata(pdev);
  853. ntb_unregister_device(&ndev->ntb);
  854. ndev_deinit_debugfs(ndev);
  855. amd_deinit_side_info(ndev);
  856. amd_deinit_dev(ndev);
  857. amd_ntb_deinit_pci(ndev);
  858. kfree(ndev);
  859. }
  860. static const struct file_operations amd_ntb_debugfs_info = {
  861. .owner = THIS_MODULE,
  862. .open = simple_open,
  863. .read = ndev_debugfs_read,
  864. };
  865. static const struct pci_device_id amd_ntb_pci_tbl[] = {
  866. {PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_NTB)},
  867. {0}
  868. };
  869. MODULE_DEVICE_TABLE(pci, amd_ntb_pci_tbl);
  870. static struct pci_driver amd_ntb_pci_driver = {
  871. .name = KBUILD_MODNAME,
  872. .id_table = amd_ntb_pci_tbl,
  873. .probe = amd_ntb_pci_probe,
  874. .remove = amd_ntb_pci_remove,
  875. };
  876. static int __init amd_ntb_pci_driver_init(void)
  877. {
  878. pr_info("%s %s\n", NTB_DESC, NTB_VER);
  879. if (debugfs_initialized())
  880. debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  881. return pci_register_driver(&amd_ntb_pci_driver);
  882. }
  883. module_init(amd_ntb_pci_driver_init);
  884. static void __exit amd_ntb_pci_driver_exit(void)
  885. {
  886. pci_unregister_driver(&amd_ntb_pci_driver);
  887. debugfs_remove_recursive(debugfs_dir);
  888. }
  889. module_exit(amd_ntb_pci_driver_exit);