nhi.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Thunderbolt driver - NHI driver
  4. *
  5. * The NHI (native host interface) is the pci device that allows us to send and
  6. * receive frames from the thunderbolt bus.
  7. *
  8. * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  9. * Copyright (C) 2018, Intel Corporation
  10. */
  11. #include <linux/pm_runtime.h>
  12. #include <linux/slab.h>
  13. #include <linux/errno.h>
  14. #include <linux/pci.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iommu.h>
  18. #include <linux/module.h>
  19. #include <linux/delay.h>
  20. #include <linux/property.h>
  21. #include <linux/string_helpers.h>
  22. #include "nhi.h"
  23. #include "nhi_regs.h"
  24. #include "tb.h"
  25. #define RING_TYPE(ring) ((ring)->is_tx ? "TX ring" : "RX ring")
  26. #define RING_FIRST_USABLE_HOPID 1
  27. /*
  28. * Used with QUIRK_E2E to specify an unused HopID the Rx credits are
  29. * transferred.
  30. */
  31. #define RING_E2E_RESERVED_HOPID RING_FIRST_USABLE_HOPID
  32. /*
  33. * Minimal number of vectors when we use MSI-X. Two for control channel
  34. * Rx/Tx and the rest four are for cross domain DMA paths.
  35. */
  36. #define MSIX_MIN_VECS 6
  37. #define MSIX_MAX_VECS 16
  38. #define NHI_MAILBOX_TIMEOUT 500 /* ms */
  39. /* Host interface quirks */
  40. #define QUIRK_AUTO_CLEAR_INT BIT(0)
  41. #define QUIRK_E2E BIT(1)
  42. static bool host_reset = true;
  43. module_param(host_reset, bool, 0444);
  44. MODULE_PARM_DESC(host_reset, "reset USB4 host router (default: true)");
  45. static int ring_interrupt_index(const struct tb_ring *ring)
  46. {
  47. int bit = ring->hop;
  48. if (!ring->is_tx)
  49. bit += ring->nhi->hop_count;
  50. return bit;
  51. }
  52. static void nhi_mask_interrupt(struct tb_nhi *nhi, int mask, int ring)
  53. {
  54. if (nhi->quirks & QUIRK_AUTO_CLEAR_INT) {
  55. u32 val;
  56. val = ioread32(nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
  57. iowrite32(val & ~mask, nhi->iobase + REG_RING_INTERRUPT_BASE + ring);
  58. } else {
  59. iowrite32(mask, nhi->iobase + REG_RING_INTERRUPT_MASK_CLEAR_BASE + ring);
  60. }
  61. }
  62. static void nhi_clear_interrupt(struct tb_nhi *nhi, int ring)
  63. {
  64. if (nhi->quirks & QUIRK_AUTO_CLEAR_INT)
  65. ioread32(nhi->iobase + REG_RING_NOTIFY_BASE + ring);
  66. else
  67. iowrite32(~0, nhi->iobase + REG_RING_INT_CLEAR + ring);
  68. }
  69. /*
  70. * ring_interrupt_active() - activate/deactivate interrupts for a single ring
  71. *
  72. * ring->nhi->lock must be held.
  73. */
  74. static void ring_interrupt_active(struct tb_ring *ring, bool active)
  75. {
  76. int index = ring_interrupt_index(ring) / 32 * 4;
  77. int reg = REG_RING_INTERRUPT_BASE + index;
  78. int interrupt_bit = ring_interrupt_index(ring) & 31;
  79. int mask = 1 << interrupt_bit;
  80. u32 old, new;
  81. if (ring->irq > 0) {
  82. u32 step, shift, ivr, misc;
  83. void __iomem *ivr_base;
  84. int auto_clear_bit;
  85. int index;
  86. if (ring->is_tx)
  87. index = ring->hop;
  88. else
  89. index = ring->hop + ring->nhi->hop_count;
  90. /*
  91. * Intel routers support a bit that isn't part of
  92. * the USB4 spec to ask the hardware to clear
  93. * interrupt status bits automatically since
  94. * we already know which interrupt was triggered.
  95. *
  96. * Other routers explicitly disable auto-clear
  97. * to prevent conditions that may occur where two
  98. * MSIX interrupts are simultaneously active and
  99. * reading the register clears both of them.
  100. */
  101. misc = ioread32(ring->nhi->iobase + REG_DMA_MISC);
  102. if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
  103. auto_clear_bit = REG_DMA_MISC_INT_AUTO_CLEAR;
  104. else
  105. auto_clear_bit = REG_DMA_MISC_DISABLE_AUTO_CLEAR;
  106. if (!(misc & auto_clear_bit))
  107. iowrite32(misc | auto_clear_bit,
  108. ring->nhi->iobase + REG_DMA_MISC);
  109. ivr_base = ring->nhi->iobase + REG_INT_VEC_ALLOC_BASE;
  110. step = index / REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  111. shift = index % REG_INT_VEC_ALLOC_REGS * REG_INT_VEC_ALLOC_BITS;
  112. ivr = ioread32(ivr_base + step);
  113. ivr &= ~(REG_INT_VEC_ALLOC_MASK << shift);
  114. if (active)
  115. ivr |= ring->vector << shift;
  116. iowrite32(ivr, ivr_base + step);
  117. }
  118. old = ioread32(ring->nhi->iobase + reg);
  119. if (active)
  120. new = old | mask;
  121. else
  122. new = old & ~mask;
  123. dev_dbg(&ring->nhi->pdev->dev,
  124. "%s interrupt at register %#x bit %d (%#x -> %#x)\n",
  125. active ? "enabling" : "disabling", reg, interrupt_bit, old, new);
  126. if (new == old)
  127. dev_WARN(&ring->nhi->pdev->dev,
  128. "interrupt for %s %d is already %s\n",
  129. RING_TYPE(ring), ring->hop,
  130. active ? "enabled" : "disabled");
  131. if (active)
  132. iowrite32(new, ring->nhi->iobase + reg);
  133. else
  134. nhi_mask_interrupt(ring->nhi, mask, index);
  135. }
  136. /*
  137. * nhi_disable_interrupts() - disable interrupts for all rings
  138. *
  139. * Use only during init and shutdown.
  140. */
  141. static void nhi_disable_interrupts(struct tb_nhi *nhi)
  142. {
  143. int i = 0;
  144. /* disable interrupts */
  145. for (i = 0; i < RING_INTERRUPT_REG_COUNT(nhi); i++)
  146. nhi_mask_interrupt(nhi, ~0, 4 * i);
  147. /* clear interrupt status bits */
  148. for (i = 0; i < RING_NOTIFY_REG_COUNT(nhi); i++)
  149. nhi_clear_interrupt(nhi, 4 * i);
  150. }
  151. /* ring helper methods */
  152. static void __iomem *ring_desc_base(struct tb_ring *ring)
  153. {
  154. void __iomem *io = ring->nhi->iobase;
  155. io += ring->is_tx ? REG_TX_RING_BASE : REG_RX_RING_BASE;
  156. io += ring->hop * 16;
  157. return io;
  158. }
  159. static void __iomem *ring_options_base(struct tb_ring *ring)
  160. {
  161. void __iomem *io = ring->nhi->iobase;
  162. io += ring->is_tx ? REG_TX_OPTIONS_BASE : REG_RX_OPTIONS_BASE;
  163. io += ring->hop * 32;
  164. return io;
  165. }
  166. static void ring_iowrite_cons(struct tb_ring *ring, u16 cons)
  167. {
  168. /*
  169. * The other 16-bits in the register is read-only and writes to it
  170. * are ignored by the hardware so we can save one ioread32() by
  171. * filling the read-only bits with zeroes.
  172. */
  173. iowrite32(cons, ring_desc_base(ring) + 8);
  174. }
  175. static void ring_iowrite_prod(struct tb_ring *ring, u16 prod)
  176. {
  177. /* See ring_iowrite_cons() above for explanation */
  178. iowrite32(prod << 16, ring_desc_base(ring) + 8);
  179. }
  180. static void ring_iowrite32desc(struct tb_ring *ring, u32 value, u32 offset)
  181. {
  182. iowrite32(value, ring_desc_base(ring) + offset);
  183. }
  184. static void ring_iowrite64desc(struct tb_ring *ring, u64 value, u32 offset)
  185. {
  186. iowrite32(value, ring_desc_base(ring) + offset);
  187. iowrite32(value >> 32, ring_desc_base(ring) + offset + 4);
  188. }
  189. static void ring_iowrite32options(struct tb_ring *ring, u32 value, u32 offset)
  190. {
  191. iowrite32(value, ring_options_base(ring) + offset);
  192. }
  193. static bool ring_full(struct tb_ring *ring)
  194. {
  195. return ((ring->head + 1) % ring->size) == ring->tail;
  196. }
  197. static bool ring_empty(struct tb_ring *ring)
  198. {
  199. return ring->head == ring->tail;
  200. }
  201. /*
  202. * ring_write_descriptors() - post frames from ring->queue to the controller
  203. *
  204. * ring->lock is held.
  205. */
  206. static void ring_write_descriptors(struct tb_ring *ring)
  207. {
  208. struct ring_frame *frame, *n;
  209. struct ring_desc *descriptor;
  210. list_for_each_entry_safe(frame, n, &ring->queue, list) {
  211. if (ring_full(ring))
  212. break;
  213. list_move_tail(&frame->list, &ring->in_flight);
  214. descriptor = &ring->descriptors[ring->head];
  215. descriptor->phys = frame->buffer_phy;
  216. descriptor->time = 0;
  217. descriptor->flags = RING_DESC_POSTED | RING_DESC_INTERRUPT;
  218. if (ring->is_tx) {
  219. descriptor->length = frame->size;
  220. descriptor->eof = frame->eof;
  221. descriptor->sof = frame->sof;
  222. }
  223. ring->head = (ring->head + 1) % ring->size;
  224. if (ring->is_tx)
  225. ring_iowrite_prod(ring, ring->head);
  226. else
  227. ring_iowrite_cons(ring, ring->head);
  228. }
  229. }
  230. /*
  231. * ring_work() - progress completed frames
  232. *
  233. * If the ring is shutting down then all frames are marked as canceled and
  234. * their callbacks are invoked.
  235. *
  236. * Otherwise we collect all completed frame from the ring buffer, write new
  237. * frame to the ring buffer and invoke the callbacks for the completed frames.
  238. */
  239. static void ring_work(struct work_struct *work)
  240. {
  241. struct tb_ring *ring = container_of(work, typeof(*ring), work);
  242. struct ring_frame *frame;
  243. bool canceled = false;
  244. unsigned long flags;
  245. LIST_HEAD(done);
  246. spin_lock_irqsave(&ring->lock, flags);
  247. if (!ring->running) {
  248. /* Move all frames to done and mark them as canceled. */
  249. list_splice_tail_init(&ring->in_flight, &done);
  250. list_splice_tail_init(&ring->queue, &done);
  251. canceled = true;
  252. goto invoke_callback;
  253. }
  254. while (!ring_empty(ring)) {
  255. if (!(ring->descriptors[ring->tail].flags
  256. & RING_DESC_COMPLETED))
  257. break;
  258. frame = list_first_entry(&ring->in_flight, typeof(*frame),
  259. list);
  260. list_move_tail(&frame->list, &done);
  261. if (!ring->is_tx) {
  262. frame->size = ring->descriptors[ring->tail].length;
  263. frame->eof = ring->descriptors[ring->tail].eof;
  264. frame->sof = ring->descriptors[ring->tail].sof;
  265. frame->flags = ring->descriptors[ring->tail].flags;
  266. }
  267. ring->tail = (ring->tail + 1) % ring->size;
  268. }
  269. ring_write_descriptors(ring);
  270. invoke_callback:
  271. /* allow callbacks to schedule new work */
  272. spin_unlock_irqrestore(&ring->lock, flags);
  273. while (!list_empty(&done)) {
  274. frame = list_first_entry(&done, typeof(*frame), list);
  275. /*
  276. * The callback may reenqueue or delete frame.
  277. * Do not hold on to it.
  278. */
  279. list_del_init(&frame->list);
  280. if (frame->callback)
  281. frame->callback(ring, frame, canceled);
  282. }
  283. }
  284. int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame)
  285. {
  286. unsigned long flags;
  287. int ret = 0;
  288. spin_lock_irqsave(&ring->lock, flags);
  289. if (ring->running) {
  290. list_add_tail(&frame->list, &ring->queue);
  291. ring_write_descriptors(ring);
  292. } else {
  293. ret = -ESHUTDOWN;
  294. }
  295. spin_unlock_irqrestore(&ring->lock, flags);
  296. return ret;
  297. }
  298. EXPORT_SYMBOL_GPL(__tb_ring_enqueue);
  299. /**
  300. * tb_ring_poll() - Poll one completed frame from the ring
  301. * @ring: Ring to poll
  302. *
  303. * This function can be called when @start_poll callback of the @ring
  304. * has been called. It will read one completed frame from the ring and
  305. * return it to the caller. Returns %NULL if there is no more completed
  306. * frames.
  307. */
  308. struct ring_frame *tb_ring_poll(struct tb_ring *ring)
  309. {
  310. struct ring_frame *frame = NULL;
  311. unsigned long flags;
  312. spin_lock_irqsave(&ring->lock, flags);
  313. if (!ring->running)
  314. goto unlock;
  315. if (ring_empty(ring))
  316. goto unlock;
  317. if (ring->descriptors[ring->tail].flags & RING_DESC_COMPLETED) {
  318. frame = list_first_entry(&ring->in_flight, typeof(*frame),
  319. list);
  320. list_del_init(&frame->list);
  321. if (!ring->is_tx) {
  322. frame->size = ring->descriptors[ring->tail].length;
  323. frame->eof = ring->descriptors[ring->tail].eof;
  324. frame->sof = ring->descriptors[ring->tail].sof;
  325. frame->flags = ring->descriptors[ring->tail].flags;
  326. }
  327. ring->tail = (ring->tail + 1) % ring->size;
  328. }
  329. unlock:
  330. spin_unlock_irqrestore(&ring->lock, flags);
  331. return frame;
  332. }
  333. EXPORT_SYMBOL_GPL(tb_ring_poll);
  334. static void __ring_interrupt_mask(struct tb_ring *ring, bool mask)
  335. {
  336. int idx = ring_interrupt_index(ring);
  337. int reg = REG_RING_INTERRUPT_BASE + idx / 32 * 4;
  338. int bit = idx % 32;
  339. u32 val;
  340. val = ioread32(ring->nhi->iobase + reg);
  341. if (mask)
  342. val &= ~BIT(bit);
  343. else
  344. val |= BIT(bit);
  345. iowrite32(val, ring->nhi->iobase + reg);
  346. }
  347. /* Both @nhi->lock and @ring->lock should be held */
  348. static void __ring_interrupt(struct tb_ring *ring)
  349. {
  350. if (!ring->running)
  351. return;
  352. if (ring->start_poll) {
  353. __ring_interrupt_mask(ring, true);
  354. ring->start_poll(ring->poll_data);
  355. } else {
  356. schedule_work(&ring->work);
  357. }
  358. }
  359. /**
  360. * tb_ring_poll_complete() - Re-start interrupt for the ring
  361. * @ring: Ring to re-start the interrupt
  362. *
  363. * This will re-start (unmask) the ring interrupt once the user is done
  364. * with polling.
  365. */
  366. void tb_ring_poll_complete(struct tb_ring *ring)
  367. {
  368. unsigned long flags;
  369. spin_lock_irqsave(&ring->nhi->lock, flags);
  370. spin_lock(&ring->lock);
  371. if (ring->start_poll)
  372. __ring_interrupt_mask(ring, false);
  373. spin_unlock(&ring->lock);
  374. spin_unlock_irqrestore(&ring->nhi->lock, flags);
  375. }
  376. EXPORT_SYMBOL_GPL(tb_ring_poll_complete);
  377. static void ring_clear_msix(const struct tb_ring *ring)
  378. {
  379. int bit;
  380. if (ring->nhi->quirks & QUIRK_AUTO_CLEAR_INT)
  381. return;
  382. bit = ring_interrupt_index(ring) & 31;
  383. if (ring->is_tx)
  384. iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR);
  385. else
  386. iowrite32(BIT(bit), ring->nhi->iobase + REG_RING_INT_CLEAR +
  387. 4 * (ring->nhi->hop_count / 32));
  388. }
  389. static irqreturn_t ring_msix(int irq, void *data)
  390. {
  391. struct tb_ring *ring = data;
  392. spin_lock(&ring->nhi->lock);
  393. ring_clear_msix(ring);
  394. spin_lock(&ring->lock);
  395. __ring_interrupt(ring);
  396. spin_unlock(&ring->lock);
  397. spin_unlock(&ring->nhi->lock);
  398. return IRQ_HANDLED;
  399. }
  400. static int ring_request_msix(struct tb_ring *ring, bool no_suspend)
  401. {
  402. struct tb_nhi *nhi = ring->nhi;
  403. unsigned long irqflags;
  404. int ret;
  405. if (!nhi->pdev->msix_enabled)
  406. return 0;
  407. ret = ida_alloc_max(&nhi->msix_ida, MSIX_MAX_VECS - 1, GFP_KERNEL);
  408. if (ret < 0)
  409. return ret;
  410. ring->vector = ret;
  411. ret = pci_irq_vector(ring->nhi->pdev, ring->vector);
  412. if (ret < 0)
  413. goto err_ida_remove;
  414. ring->irq = ret;
  415. irqflags = no_suspend ? IRQF_NO_SUSPEND : 0;
  416. ret = request_irq(ring->irq, ring_msix, irqflags, "thunderbolt", ring);
  417. if (ret)
  418. goto err_ida_remove;
  419. return 0;
  420. err_ida_remove:
  421. ida_free(&nhi->msix_ida, ring->vector);
  422. return ret;
  423. }
  424. static void ring_release_msix(struct tb_ring *ring)
  425. {
  426. if (ring->irq <= 0)
  427. return;
  428. free_irq(ring->irq, ring);
  429. ida_free(&ring->nhi->msix_ida, ring->vector);
  430. ring->vector = 0;
  431. ring->irq = 0;
  432. }
  433. static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
  434. {
  435. unsigned int start_hop = RING_FIRST_USABLE_HOPID;
  436. int ret = 0;
  437. if (nhi->quirks & QUIRK_E2E) {
  438. start_hop = RING_FIRST_USABLE_HOPID + 1;
  439. if (ring->flags & RING_FLAG_E2E && !ring->is_tx) {
  440. dev_dbg(&nhi->pdev->dev, "quirking E2E TX HopID %u -> %u\n",
  441. ring->e2e_tx_hop, RING_E2E_RESERVED_HOPID);
  442. ring->e2e_tx_hop = RING_E2E_RESERVED_HOPID;
  443. }
  444. }
  445. spin_lock_irq(&nhi->lock);
  446. if (ring->hop < 0) {
  447. unsigned int i;
  448. /*
  449. * Automatically allocate HopID from the non-reserved
  450. * range 1 .. hop_count - 1.
  451. */
  452. for (i = start_hop; i < nhi->hop_count; i++) {
  453. if (ring->is_tx) {
  454. if (!nhi->tx_rings[i]) {
  455. ring->hop = i;
  456. break;
  457. }
  458. } else {
  459. if (!nhi->rx_rings[i]) {
  460. ring->hop = i;
  461. break;
  462. }
  463. }
  464. }
  465. }
  466. if (ring->hop > 0 && ring->hop < start_hop) {
  467. dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
  468. ret = -EINVAL;
  469. goto err_unlock;
  470. }
  471. if (ring->hop < 0 || ring->hop >= nhi->hop_count) {
  472. dev_warn(&nhi->pdev->dev, "invalid hop: %d\n", ring->hop);
  473. ret = -EINVAL;
  474. goto err_unlock;
  475. }
  476. if (ring->is_tx && nhi->tx_rings[ring->hop]) {
  477. dev_warn(&nhi->pdev->dev, "TX hop %d already allocated\n",
  478. ring->hop);
  479. ret = -EBUSY;
  480. goto err_unlock;
  481. }
  482. if (!ring->is_tx && nhi->rx_rings[ring->hop]) {
  483. dev_warn(&nhi->pdev->dev, "RX hop %d already allocated\n",
  484. ring->hop);
  485. ret = -EBUSY;
  486. goto err_unlock;
  487. }
  488. if (ring->is_tx)
  489. nhi->tx_rings[ring->hop] = ring;
  490. else
  491. nhi->rx_rings[ring->hop] = ring;
  492. err_unlock:
  493. spin_unlock_irq(&nhi->lock);
  494. return ret;
  495. }
  496. static struct tb_ring *tb_ring_alloc(struct tb_nhi *nhi, u32 hop, int size,
  497. bool transmit, unsigned int flags,
  498. int e2e_tx_hop, u16 sof_mask, u16 eof_mask,
  499. void (*start_poll)(void *),
  500. void *poll_data)
  501. {
  502. struct tb_ring *ring = NULL;
  503. dev_dbg(&nhi->pdev->dev, "allocating %s ring %d of size %d\n",
  504. transmit ? "TX" : "RX", hop, size);
  505. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  506. if (!ring)
  507. return NULL;
  508. spin_lock_init(&ring->lock);
  509. INIT_LIST_HEAD(&ring->queue);
  510. INIT_LIST_HEAD(&ring->in_flight);
  511. INIT_WORK(&ring->work, ring_work);
  512. ring->nhi = nhi;
  513. ring->hop = hop;
  514. ring->is_tx = transmit;
  515. ring->size = size;
  516. ring->flags = flags;
  517. ring->e2e_tx_hop = e2e_tx_hop;
  518. ring->sof_mask = sof_mask;
  519. ring->eof_mask = eof_mask;
  520. ring->head = 0;
  521. ring->tail = 0;
  522. ring->running = false;
  523. ring->start_poll = start_poll;
  524. ring->poll_data = poll_data;
  525. ring->descriptors = dma_alloc_coherent(&ring->nhi->pdev->dev,
  526. size * sizeof(*ring->descriptors),
  527. &ring->descriptors_dma, GFP_KERNEL | __GFP_ZERO);
  528. if (!ring->descriptors)
  529. goto err_free_ring;
  530. if (ring_request_msix(ring, flags & RING_FLAG_NO_SUSPEND))
  531. goto err_free_descs;
  532. if (nhi_alloc_hop(nhi, ring))
  533. goto err_release_msix;
  534. return ring;
  535. err_release_msix:
  536. ring_release_msix(ring);
  537. err_free_descs:
  538. dma_free_coherent(&ring->nhi->pdev->dev,
  539. ring->size * sizeof(*ring->descriptors),
  540. ring->descriptors, ring->descriptors_dma);
  541. err_free_ring:
  542. kfree(ring);
  543. return NULL;
  544. }
  545. /**
  546. * tb_ring_alloc_tx() - Allocate DMA ring for transmit
  547. * @nhi: Pointer to the NHI the ring is to be allocated
  548. * @hop: HopID (ring) to allocate
  549. * @size: Number of entries in the ring
  550. * @flags: Flags for the ring
  551. */
  552. struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
  553. unsigned int flags)
  554. {
  555. return tb_ring_alloc(nhi, hop, size, true, flags, 0, 0, 0, NULL, NULL);
  556. }
  557. EXPORT_SYMBOL_GPL(tb_ring_alloc_tx);
  558. /**
  559. * tb_ring_alloc_rx() - Allocate DMA ring for receive
  560. * @nhi: Pointer to the NHI the ring is to be allocated
  561. * @hop: HopID (ring) to allocate. Pass %-1 for automatic allocation.
  562. * @size: Number of entries in the ring
  563. * @flags: Flags for the ring
  564. * @e2e_tx_hop: Transmit HopID when E2E is enabled in @flags
  565. * @sof_mask: Mask of PDF values that start a frame
  566. * @eof_mask: Mask of PDF values that end a frame
  567. * @start_poll: If not %NULL the ring will call this function when an
  568. * interrupt is triggered and masked, instead of callback
  569. * in each Rx frame.
  570. * @poll_data: Optional data passed to @start_poll
  571. */
  572. struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
  573. unsigned int flags, int e2e_tx_hop,
  574. u16 sof_mask, u16 eof_mask,
  575. void (*start_poll)(void *), void *poll_data)
  576. {
  577. return tb_ring_alloc(nhi, hop, size, false, flags, e2e_tx_hop, sof_mask, eof_mask,
  578. start_poll, poll_data);
  579. }
  580. EXPORT_SYMBOL_GPL(tb_ring_alloc_rx);
  581. /**
  582. * tb_ring_start() - enable a ring
  583. * @ring: Ring to start
  584. *
  585. * Must not be invoked in parallel with tb_ring_stop().
  586. */
  587. void tb_ring_start(struct tb_ring *ring)
  588. {
  589. u16 frame_size;
  590. u32 flags;
  591. spin_lock_irq(&ring->nhi->lock);
  592. spin_lock(&ring->lock);
  593. if (ring->nhi->going_away)
  594. goto err;
  595. if (ring->running) {
  596. dev_WARN(&ring->nhi->pdev->dev, "ring already started\n");
  597. goto err;
  598. }
  599. dev_dbg(&ring->nhi->pdev->dev, "starting %s %d\n",
  600. RING_TYPE(ring), ring->hop);
  601. if (ring->flags & RING_FLAG_FRAME) {
  602. /* Means 4096 */
  603. frame_size = 0;
  604. flags = RING_FLAG_ENABLE;
  605. } else {
  606. frame_size = TB_FRAME_SIZE;
  607. flags = RING_FLAG_ENABLE | RING_FLAG_RAW;
  608. }
  609. ring_iowrite64desc(ring, ring->descriptors_dma, 0);
  610. if (ring->is_tx) {
  611. ring_iowrite32desc(ring, ring->size, 12);
  612. ring_iowrite32options(ring, 0, 4); /* time releated ? */
  613. ring_iowrite32options(ring, flags, 0);
  614. } else {
  615. u32 sof_eof_mask = ring->sof_mask << 16 | ring->eof_mask;
  616. ring_iowrite32desc(ring, (frame_size << 16) | ring->size, 12);
  617. ring_iowrite32options(ring, sof_eof_mask, 4);
  618. ring_iowrite32options(ring, flags, 0);
  619. }
  620. /*
  621. * Now that the ring valid bit is set we can configure E2E if
  622. * enabled for the ring.
  623. */
  624. if (ring->flags & RING_FLAG_E2E) {
  625. if (!ring->is_tx) {
  626. u32 hop;
  627. hop = ring->e2e_tx_hop << REG_RX_OPTIONS_E2E_HOP_SHIFT;
  628. hop &= REG_RX_OPTIONS_E2E_HOP_MASK;
  629. flags |= hop;
  630. dev_dbg(&ring->nhi->pdev->dev,
  631. "enabling E2E for %s %d with TX HopID %d\n",
  632. RING_TYPE(ring), ring->hop, ring->e2e_tx_hop);
  633. } else {
  634. dev_dbg(&ring->nhi->pdev->dev, "enabling E2E for %s %d\n",
  635. RING_TYPE(ring), ring->hop);
  636. }
  637. flags |= RING_FLAG_E2E_FLOW_CONTROL;
  638. ring_iowrite32options(ring, flags, 0);
  639. }
  640. ring_interrupt_active(ring, true);
  641. ring->running = true;
  642. err:
  643. spin_unlock(&ring->lock);
  644. spin_unlock_irq(&ring->nhi->lock);
  645. }
  646. EXPORT_SYMBOL_GPL(tb_ring_start);
  647. /**
  648. * tb_ring_stop() - shutdown a ring
  649. * @ring: Ring to stop
  650. *
  651. * Must not be invoked from a callback.
  652. *
  653. * This method will disable the ring. Further calls to
  654. * tb_ring_tx/tb_ring_rx will return -ESHUTDOWN until ring_stop has been
  655. * called.
  656. *
  657. * All enqueued frames will be canceled and their callbacks will be executed
  658. * with frame->canceled set to true (on the callback thread). This method
  659. * returns only after all callback invocations have finished.
  660. */
  661. void tb_ring_stop(struct tb_ring *ring)
  662. {
  663. spin_lock_irq(&ring->nhi->lock);
  664. spin_lock(&ring->lock);
  665. dev_dbg(&ring->nhi->pdev->dev, "stopping %s %d\n",
  666. RING_TYPE(ring), ring->hop);
  667. if (ring->nhi->going_away)
  668. goto err;
  669. if (!ring->running) {
  670. dev_WARN(&ring->nhi->pdev->dev, "%s %d already stopped\n",
  671. RING_TYPE(ring), ring->hop);
  672. goto err;
  673. }
  674. ring_interrupt_active(ring, false);
  675. ring_iowrite32options(ring, 0, 0);
  676. ring_iowrite64desc(ring, 0, 0);
  677. ring_iowrite32desc(ring, 0, 8);
  678. ring_iowrite32desc(ring, 0, 12);
  679. ring->head = 0;
  680. ring->tail = 0;
  681. ring->running = false;
  682. err:
  683. spin_unlock(&ring->lock);
  684. spin_unlock_irq(&ring->nhi->lock);
  685. /*
  686. * schedule ring->work to invoke callbacks on all remaining frames.
  687. */
  688. schedule_work(&ring->work);
  689. flush_work(&ring->work);
  690. }
  691. EXPORT_SYMBOL_GPL(tb_ring_stop);
  692. /*
  693. * tb_ring_free() - free ring
  694. *
  695. * When this method returns all invocations of ring->callback will have
  696. * finished.
  697. *
  698. * Ring must be stopped.
  699. *
  700. * Must NOT be called from ring_frame->callback!
  701. */
  702. void tb_ring_free(struct tb_ring *ring)
  703. {
  704. spin_lock_irq(&ring->nhi->lock);
  705. /*
  706. * Dissociate the ring from the NHI. This also ensures that
  707. * nhi_interrupt_work cannot reschedule ring->work.
  708. */
  709. if (ring->is_tx)
  710. ring->nhi->tx_rings[ring->hop] = NULL;
  711. else
  712. ring->nhi->rx_rings[ring->hop] = NULL;
  713. if (ring->running) {
  714. dev_WARN(&ring->nhi->pdev->dev, "%s %d still running\n",
  715. RING_TYPE(ring), ring->hop);
  716. }
  717. spin_unlock_irq(&ring->nhi->lock);
  718. ring_release_msix(ring);
  719. dma_free_coherent(&ring->nhi->pdev->dev,
  720. ring->size * sizeof(*ring->descriptors),
  721. ring->descriptors, ring->descriptors_dma);
  722. ring->descriptors = NULL;
  723. ring->descriptors_dma = 0;
  724. dev_dbg(&ring->nhi->pdev->dev, "freeing %s %d\n", RING_TYPE(ring),
  725. ring->hop);
  726. /*
  727. * ring->work can no longer be scheduled (it is scheduled only
  728. * by nhi_interrupt_work, ring_stop and ring_msix). Wait for it
  729. * to finish before freeing the ring.
  730. */
  731. flush_work(&ring->work);
  732. kfree(ring);
  733. }
  734. EXPORT_SYMBOL_GPL(tb_ring_free);
  735. /**
  736. * nhi_mailbox_cmd() - Send a command through NHI mailbox
  737. * @nhi: Pointer to the NHI structure
  738. * @cmd: Command to send
  739. * @data: Data to be send with the command
  740. *
  741. * Sends mailbox command to the firmware running on NHI. Returns %0 in
  742. * case of success and negative errno in case of failure.
  743. */
  744. int nhi_mailbox_cmd(struct tb_nhi *nhi, enum nhi_mailbox_cmd cmd, u32 data)
  745. {
  746. ktime_t timeout;
  747. u32 val;
  748. iowrite32(data, nhi->iobase + REG_INMAIL_DATA);
  749. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  750. val &= ~(REG_INMAIL_CMD_MASK | REG_INMAIL_ERROR);
  751. val |= REG_INMAIL_OP_REQUEST | cmd;
  752. iowrite32(val, nhi->iobase + REG_INMAIL_CMD);
  753. timeout = ktime_add_ms(ktime_get(), NHI_MAILBOX_TIMEOUT);
  754. do {
  755. val = ioread32(nhi->iobase + REG_INMAIL_CMD);
  756. if (!(val & REG_INMAIL_OP_REQUEST))
  757. break;
  758. usleep_range(10, 20);
  759. } while (ktime_before(ktime_get(), timeout));
  760. if (val & REG_INMAIL_OP_REQUEST)
  761. return -ETIMEDOUT;
  762. if (val & REG_INMAIL_ERROR)
  763. return -EIO;
  764. return 0;
  765. }
  766. /**
  767. * nhi_mailbox_mode() - Return current firmware operation mode
  768. * @nhi: Pointer to the NHI structure
  769. *
  770. * The function reads current firmware operation mode using NHI mailbox
  771. * registers and returns it to the caller.
  772. */
  773. enum nhi_fw_mode nhi_mailbox_mode(struct tb_nhi *nhi)
  774. {
  775. u32 val;
  776. val = ioread32(nhi->iobase + REG_OUTMAIL_CMD);
  777. val &= REG_OUTMAIL_CMD_OPMODE_MASK;
  778. val >>= REG_OUTMAIL_CMD_OPMODE_SHIFT;
  779. return (enum nhi_fw_mode)val;
  780. }
  781. static void nhi_interrupt_work(struct work_struct *work)
  782. {
  783. struct tb_nhi *nhi = container_of(work, typeof(*nhi), interrupt_work);
  784. int value = 0; /* Suppress uninitialized usage warning. */
  785. int bit;
  786. int hop = -1;
  787. int type = 0; /* current interrupt type 0: TX, 1: RX, 2: RX overflow */
  788. struct tb_ring *ring;
  789. spin_lock_irq(&nhi->lock);
  790. /*
  791. * Starting at REG_RING_NOTIFY_BASE there are three status bitfields
  792. * (TX, RX, RX overflow). We iterate over the bits and read a new
  793. * dwords as required. The registers are cleared on read.
  794. */
  795. for (bit = 0; bit < 3 * nhi->hop_count; bit++) {
  796. if (bit % 32 == 0)
  797. value = ioread32(nhi->iobase
  798. + REG_RING_NOTIFY_BASE
  799. + 4 * (bit / 32));
  800. if (++hop == nhi->hop_count) {
  801. hop = 0;
  802. type++;
  803. }
  804. if ((value & (1 << (bit % 32))) == 0)
  805. continue;
  806. if (type == 2) {
  807. dev_warn(&nhi->pdev->dev,
  808. "RX overflow for ring %d\n",
  809. hop);
  810. continue;
  811. }
  812. if (type == 0)
  813. ring = nhi->tx_rings[hop];
  814. else
  815. ring = nhi->rx_rings[hop];
  816. if (ring == NULL) {
  817. dev_warn(&nhi->pdev->dev,
  818. "got interrupt for inactive %s ring %d\n",
  819. type ? "RX" : "TX",
  820. hop);
  821. continue;
  822. }
  823. spin_lock(&ring->lock);
  824. __ring_interrupt(ring);
  825. spin_unlock(&ring->lock);
  826. }
  827. spin_unlock_irq(&nhi->lock);
  828. }
  829. static irqreturn_t nhi_msi(int irq, void *data)
  830. {
  831. struct tb_nhi *nhi = data;
  832. schedule_work(&nhi->interrupt_work);
  833. return IRQ_HANDLED;
  834. }
  835. static int __nhi_suspend_noirq(struct device *dev, bool wakeup)
  836. {
  837. struct pci_dev *pdev = to_pci_dev(dev);
  838. struct tb *tb = pci_get_drvdata(pdev);
  839. struct tb_nhi *nhi = tb->nhi;
  840. int ret;
  841. ret = tb_domain_suspend_noirq(tb);
  842. if (ret)
  843. return ret;
  844. if (nhi->ops && nhi->ops->suspend_noirq) {
  845. ret = nhi->ops->suspend_noirq(tb->nhi, wakeup);
  846. if (ret)
  847. return ret;
  848. }
  849. return 0;
  850. }
  851. static int nhi_suspend_noirq(struct device *dev)
  852. {
  853. return __nhi_suspend_noirq(dev, device_may_wakeup(dev));
  854. }
  855. static int nhi_freeze_noirq(struct device *dev)
  856. {
  857. struct pci_dev *pdev = to_pci_dev(dev);
  858. struct tb *tb = pci_get_drvdata(pdev);
  859. return tb_domain_freeze_noirq(tb);
  860. }
  861. static int nhi_thaw_noirq(struct device *dev)
  862. {
  863. struct pci_dev *pdev = to_pci_dev(dev);
  864. struct tb *tb = pci_get_drvdata(pdev);
  865. return tb_domain_thaw_noirq(tb);
  866. }
  867. static bool nhi_wake_supported(struct pci_dev *pdev)
  868. {
  869. u8 val;
  870. /*
  871. * If power rails are sustainable for wakeup from S4 this
  872. * property is set by the BIOS.
  873. */
  874. if (device_property_read_u8(&pdev->dev, "WAKE_SUPPORTED", &val))
  875. return !!val;
  876. return true;
  877. }
  878. static int nhi_poweroff_noirq(struct device *dev)
  879. {
  880. struct pci_dev *pdev = to_pci_dev(dev);
  881. bool wakeup;
  882. wakeup = device_may_wakeup(dev) && nhi_wake_supported(pdev);
  883. return __nhi_suspend_noirq(dev, wakeup);
  884. }
  885. static void nhi_enable_int_throttling(struct tb_nhi *nhi)
  886. {
  887. /* Throttling is specified in 256ns increments */
  888. u32 throttle = DIV_ROUND_UP(128 * NSEC_PER_USEC, 256);
  889. unsigned int i;
  890. /*
  891. * Configure interrupt throttling for all vectors even if we
  892. * only use few.
  893. */
  894. for (i = 0; i < MSIX_MAX_VECS; i++) {
  895. u32 reg = REG_INT_THROTTLING_RATE + i * 4;
  896. iowrite32(throttle, nhi->iobase + reg);
  897. }
  898. }
  899. static int nhi_resume_noirq(struct device *dev)
  900. {
  901. struct pci_dev *pdev = to_pci_dev(dev);
  902. struct tb *tb = pci_get_drvdata(pdev);
  903. struct tb_nhi *nhi = tb->nhi;
  904. int ret;
  905. /*
  906. * Check that the device is still there. It may be that the user
  907. * unplugged last device which causes the host controller to go
  908. * away on PCs.
  909. */
  910. if (!pci_device_is_present(pdev)) {
  911. nhi->going_away = true;
  912. } else {
  913. if (nhi->ops && nhi->ops->resume_noirq) {
  914. ret = nhi->ops->resume_noirq(nhi);
  915. if (ret)
  916. return ret;
  917. }
  918. nhi_enable_int_throttling(tb->nhi);
  919. }
  920. return tb_domain_resume_noirq(tb);
  921. }
  922. static int nhi_suspend(struct device *dev)
  923. {
  924. struct pci_dev *pdev = to_pci_dev(dev);
  925. struct tb *tb = pci_get_drvdata(pdev);
  926. return tb_domain_suspend(tb);
  927. }
  928. static void nhi_complete(struct device *dev)
  929. {
  930. struct pci_dev *pdev = to_pci_dev(dev);
  931. struct tb *tb = pci_get_drvdata(pdev);
  932. /*
  933. * If we were runtime suspended when system suspend started,
  934. * schedule runtime resume now. It should bring the domain back
  935. * to functional state.
  936. */
  937. if (pm_runtime_suspended(&pdev->dev))
  938. pm_runtime_resume(&pdev->dev);
  939. else
  940. tb_domain_complete(tb);
  941. }
  942. static int nhi_runtime_suspend(struct device *dev)
  943. {
  944. struct pci_dev *pdev = to_pci_dev(dev);
  945. struct tb *tb = pci_get_drvdata(pdev);
  946. struct tb_nhi *nhi = tb->nhi;
  947. int ret;
  948. ret = tb_domain_runtime_suspend(tb);
  949. if (ret)
  950. return ret;
  951. if (nhi->ops && nhi->ops->runtime_suspend) {
  952. ret = nhi->ops->runtime_suspend(tb->nhi);
  953. if (ret)
  954. return ret;
  955. }
  956. return 0;
  957. }
  958. static int nhi_runtime_resume(struct device *dev)
  959. {
  960. struct pci_dev *pdev = to_pci_dev(dev);
  961. struct tb *tb = pci_get_drvdata(pdev);
  962. struct tb_nhi *nhi = tb->nhi;
  963. int ret;
  964. if (nhi->ops && nhi->ops->runtime_resume) {
  965. ret = nhi->ops->runtime_resume(nhi);
  966. if (ret)
  967. return ret;
  968. }
  969. nhi_enable_int_throttling(nhi);
  970. return tb_domain_runtime_resume(tb);
  971. }
  972. static void nhi_shutdown(struct tb_nhi *nhi)
  973. {
  974. int i;
  975. dev_dbg(&nhi->pdev->dev, "shutdown\n");
  976. for (i = 0; i < nhi->hop_count; i++) {
  977. if (nhi->tx_rings[i])
  978. dev_WARN(&nhi->pdev->dev,
  979. "TX ring %d is still active\n", i);
  980. if (nhi->rx_rings[i])
  981. dev_WARN(&nhi->pdev->dev,
  982. "RX ring %d is still active\n", i);
  983. }
  984. nhi_disable_interrupts(nhi);
  985. /*
  986. * We have to release the irq before calling flush_work. Otherwise an
  987. * already executing IRQ handler could call schedule_work again.
  988. */
  989. if (!nhi->pdev->msix_enabled) {
  990. devm_free_irq(&nhi->pdev->dev, nhi->pdev->irq, nhi);
  991. flush_work(&nhi->interrupt_work);
  992. }
  993. ida_destroy(&nhi->msix_ida);
  994. if (nhi->ops && nhi->ops->shutdown)
  995. nhi->ops->shutdown(nhi);
  996. }
  997. static void nhi_check_quirks(struct tb_nhi *nhi)
  998. {
  999. if (nhi->pdev->vendor == PCI_VENDOR_ID_INTEL) {
  1000. /*
  1001. * Intel hardware supports auto clear of the interrupt
  1002. * status register right after interrupt is being
  1003. * issued.
  1004. */
  1005. nhi->quirks |= QUIRK_AUTO_CLEAR_INT;
  1006. switch (nhi->pdev->device) {
  1007. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
  1008. case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
  1009. /*
  1010. * Falcon Ridge controller needs the end-to-end
  1011. * flow control workaround to avoid losing Rx
  1012. * packets when RING_FLAG_E2E is set.
  1013. */
  1014. nhi->quirks |= QUIRK_E2E;
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. static int nhi_check_iommu_pdev(struct pci_dev *pdev, void *data)
  1020. {
  1021. if (!pdev->external_facing ||
  1022. !device_iommu_capable(&pdev->dev, IOMMU_CAP_PRE_BOOT_PROTECTION))
  1023. return 0;
  1024. *(bool *)data = true;
  1025. return 1; /* Stop walking */
  1026. }
  1027. static void nhi_check_iommu(struct tb_nhi *nhi)
  1028. {
  1029. struct pci_bus *bus = nhi->pdev->bus;
  1030. bool port_ok = false;
  1031. /*
  1032. * Ideally what we'd do here is grab every PCI device that
  1033. * represents a tunnelling adapter for this NHI and check their
  1034. * status directly, but unfortunately USB4 seems to make it
  1035. * obnoxiously difficult to reliably make any correlation.
  1036. *
  1037. * So for now we'll have to bodge it... Hoping that the system
  1038. * is at least sane enough that an adapter is in the same PCI
  1039. * segment as its NHI, if we can find *something* on that segment
  1040. * which meets the requirements for Kernel DMA Protection, we'll
  1041. * take that to imply that firmware is aware and has (hopefully)
  1042. * done the right thing in general. We need to know that the PCI
  1043. * layer has seen the ExternalFacingPort property which will then
  1044. * inform the IOMMU layer to enforce the complete "untrusted DMA"
  1045. * flow, but also that the IOMMU driver itself can be trusted not
  1046. * to have been subverted by a pre-boot DMA attack.
  1047. */
  1048. while (bus->parent)
  1049. bus = bus->parent;
  1050. pci_walk_bus(bus, nhi_check_iommu_pdev, &port_ok);
  1051. nhi->iommu_dma_protection = port_ok;
  1052. dev_dbg(&nhi->pdev->dev, "IOMMU DMA protection is %s\n",
  1053. str_enabled_disabled(port_ok));
  1054. }
  1055. static void nhi_reset(struct tb_nhi *nhi)
  1056. {
  1057. ktime_t timeout;
  1058. u32 val;
  1059. val = ioread32(nhi->iobase + REG_CAPS);
  1060. /* Reset only v2 and later routers */
  1061. if (FIELD_GET(REG_CAPS_VERSION_MASK, val) < REG_CAPS_VERSION_2)
  1062. return;
  1063. if (!host_reset) {
  1064. dev_dbg(&nhi->pdev->dev, "skipping host router reset\n");
  1065. return;
  1066. }
  1067. iowrite32(REG_RESET_HRR, nhi->iobase + REG_RESET);
  1068. msleep(100);
  1069. timeout = ktime_add_ms(ktime_get(), 500);
  1070. do {
  1071. val = ioread32(nhi->iobase + REG_RESET);
  1072. if (!(val & REG_RESET_HRR)) {
  1073. dev_warn(&nhi->pdev->dev, "host router reset successful\n");
  1074. return;
  1075. }
  1076. usleep_range(10, 20);
  1077. } while (ktime_before(ktime_get(), timeout));
  1078. dev_warn(&nhi->pdev->dev, "timeout resetting host router\n");
  1079. }
  1080. static int nhi_init_msi(struct tb_nhi *nhi)
  1081. {
  1082. struct pci_dev *pdev = nhi->pdev;
  1083. struct device *dev = &pdev->dev;
  1084. int res, irq, nvec;
  1085. /* In case someone left them on. */
  1086. nhi_disable_interrupts(nhi);
  1087. nhi_enable_int_throttling(nhi);
  1088. ida_init(&nhi->msix_ida);
  1089. /*
  1090. * The NHI has 16 MSI-X vectors or a single MSI. We first try to
  1091. * get all MSI-X vectors and if we succeed, each ring will have
  1092. * one MSI-X. If for some reason that does not work out, we
  1093. * fallback to a single MSI.
  1094. */
  1095. nvec = pci_alloc_irq_vectors(pdev, MSIX_MIN_VECS, MSIX_MAX_VECS,
  1096. PCI_IRQ_MSIX);
  1097. if (nvec < 0) {
  1098. nvec = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
  1099. if (nvec < 0)
  1100. return nvec;
  1101. INIT_WORK(&nhi->interrupt_work, nhi_interrupt_work);
  1102. irq = pci_irq_vector(nhi->pdev, 0);
  1103. if (irq < 0)
  1104. return irq;
  1105. res = devm_request_irq(&pdev->dev, irq, nhi_msi,
  1106. IRQF_NO_SUSPEND, "thunderbolt", nhi);
  1107. if (res)
  1108. return dev_err_probe(dev, res, "request_irq failed, aborting\n");
  1109. }
  1110. return 0;
  1111. }
  1112. static bool nhi_imr_valid(struct pci_dev *pdev)
  1113. {
  1114. u8 val;
  1115. if (!device_property_read_u8(&pdev->dev, "IMR_VALID", &val))
  1116. return !!val;
  1117. return true;
  1118. }
  1119. static struct tb *nhi_select_cm(struct tb_nhi *nhi)
  1120. {
  1121. struct tb *tb;
  1122. /*
  1123. * USB4 case is simple. If we got control of any of the
  1124. * capabilities, we use software CM.
  1125. */
  1126. if (tb_acpi_is_native())
  1127. return tb_probe(nhi);
  1128. /*
  1129. * Either firmware based CM is running (we did not get control
  1130. * from the firmware) or this is pre-USB4 PC so try first
  1131. * firmware CM and then fallback to software CM.
  1132. */
  1133. tb = icm_probe(nhi);
  1134. if (!tb)
  1135. tb = tb_probe(nhi);
  1136. return tb;
  1137. }
  1138. static int nhi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1139. {
  1140. struct device *dev = &pdev->dev;
  1141. struct tb_nhi *nhi;
  1142. struct tb *tb;
  1143. int res;
  1144. if (!nhi_imr_valid(pdev))
  1145. return dev_err_probe(dev, -ENODEV, "firmware image not valid, aborting\n");
  1146. res = pcim_enable_device(pdev);
  1147. if (res)
  1148. return dev_err_probe(dev, res, "cannot enable PCI device, aborting\n");
  1149. res = pcim_iomap_regions(pdev, 1 << 0, "thunderbolt");
  1150. if (res)
  1151. return dev_err_probe(dev, res, "cannot obtain PCI resources, aborting\n");
  1152. nhi = devm_kzalloc(&pdev->dev, sizeof(*nhi), GFP_KERNEL);
  1153. if (!nhi)
  1154. return -ENOMEM;
  1155. nhi->pdev = pdev;
  1156. nhi->ops = (const struct tb_nhi_ops *)id->driver_data;
  1157. /* cannot fail - table is allocated in pcim_iomap_regions */
  1158. nhi->iobase = pcim_iomap_table(pdev)[0];
  1159. nhi->hop_count = ioread32(nhi->iobase + REG_CAPS) & 0x3ff;
  1160. dev_dbg(dev, "total paths: %d\n", nhi->hop_count);
  1161. nhi->tx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  1162. sizeof(*nhi->tx_rings), GFP_KERNEL);
  1163. nhi->rx_rings = devm_kcalloc(&pdev->dev, nhi->hop_count,
  1164. sizeof(*nhi->rx_rings), GFP_KERNEL);
  1165. if (!nhi->tx_rings || !nhi->rx_rings)
  1166. return -ENOMEM;
  1167. nhi_check_quirks(nhi);
  1168. nhi_check_iommu(nhi);
  1169. nhi_reset(nhi);
  1170. res = nhi_init_msi(nhi);
  1171. if (res)
  1172. return dev_err_probe(dev, res, "cannot enable MSI, aborting\n");
  1173. spin_lock_init(&nhi->lock);
  1174. res = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  1175. if (res)
  1176. return dev_err_probe(dev, res, "failed to set DMA mask\n");
  1177. pci_set_master(pdev);
  1178. if (nhi->ops && nhi->ops->init) {
  1179. res = nhi->ops->init(nhi);
  1180. if (res)
  1181. return res;
  1182. }
  1183. tb = nhi_select_cm(nhi);
  1184. if (!tb)
  1185. return dev_err_probe(dev, -ENODEV,
  1186. "failed to determine connection manager, aborting\n");
  1187. dev_dbg(dev, "NHI initialized, starting thunderbolt\n");
  1188. res = tb_domain_add(tb, host_reset);
  1189. if (res) {
  1190. /*
  1191. * At this point the RX/TX rings might already have been
  1192. * activated. Do a proper shutdown.
  1193. */
  1194. tb_domain_put(tb);
  1195. nhi_shutdown(nhi);
  1196. return res;
  1197. }
  1198. pci_set_drvdata(pdev, tb);
  1199. device_wakeup_enable(&pdev->dev);
  1200. pm_runtime_allow(&pdev->dev);
  1201. pm_runtime_set_autosuspend_delay(&pdev->dev, TB_AUTOSUSPEND_DELAY);
  1202. pm_runtime_use_autosuspend(&pdev->dev);
  1203. pm_runtime_put_autosuspend(&pdev->dev);
  1204. return 0;
  1205. }
  1206. static void nhi_remove(struct pci_dev *pdev)
  1207. {
  1208. struct tb *tb = pci_get_drvdata(pdev);
  1209. struct tb_nhi *nhi = tb->nhi;
  1210. pm_runtime_get_sync(&pdev->dev);
  1211. pm_runtime_dont_use_autosuspend(&pdev->dev);
  1212. pm_runtime_forbid(&pdev->dev);
  1213. tb_domain_remove(tb);
  1214. nhi_shutdown(nhi);
  1215. }
  1216. /*
  1217. * The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
  1218. * the tunnels asap. A corresponding pci quirk blocks the downstream bridges
  1219. * resume_noirq until we are done.
  1220. */
  1221. static const struct dev_pm_ops nhi_pm_ops = {
  1222. .suspend_noirq = nhi_suspend_noirq,
  1223. .resume_noirq = nhi_resume_noirq,
  1224. .freeze_noirq = nhi_freeze_noirq, /*
  1225. * we just disable hotplug, the
  1226. * pci-tunnels stay alive.
  1227. */
  1228. .thaw_noirq = nhi_thaw_noirq,
  1229. .restore_noirq = nhi_resume_noirq,
  1230. .suspend = nhi_suspend,
  1231. .poweroff_noirq = nhi_poweroff_noirq,
  1232. .poweroff = nhi_suspend,
  1233. .complete = nhi_complete,
  1234. .runtime_suspend = nhi_runtime_suspend,
  1235. .runtime_resume = nhi_runtime_resume,
  1236. };
  1237. static struct pci_device_id nhi_ids[] = {
  1238. /*
  1239. * We have to specify class, the TB bridges use the same device and
  1240. * vendor (sub)id on gen 1 and gen 2 controllers.
  1241. */
  1242. {
  1243. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1244. .vendor = PCI_VENDOR_ID_INTEL,
  1245. .device = PCI_DEVICE_ID_INTEL_LIGHT_RIDGE,
  1246. .subvendor = 0x2222, .subdevice = 0x1111,
  1247. },
  1248. {
  1249. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1250. .vendor = PCI_VENDOR_ID_INTEL,
  1251. .device = PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C,
  1252. .subvendor = 0x2222, .subdevice = 0x1111,
  1253. },
  1254. {
  1255. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1256. .vendor = PCI_VENDOR_ID_INTEL,
  1257. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI,
  1258. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  1259. },
  1260. {
  1261. .class = PCI_CLASS_SYSTEM_OTHER << 8, .class_mask = ~0,
  1262. .vendor = PCI_VENDOR_ID_INTEL,
  1263. .device = PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI,
  1264. .subvendor = PCI_ANY_ID, .subdevice = PCI_ANY_ID,
  1265. },
  1266. /* Thunderbolt 3 */
  1267. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_NHI) },
  1268. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_NHI) },
  1269. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_USBONLY_NHI) },
  1270. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_NHI) },
  1271. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_USBONLY_NHI) },
  1272. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_NHI) },
  1273. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_NHI) },
  1274. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_USBONLY_NHI) },
  1275. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_NHI) },
  1276. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_NHI) },
  1277. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI0),
  1278. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1279. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ICL_NHI1),
  1280. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1281. /* Thunderbolt 4 */
  1282. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI0),
  1283. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1284. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_NHI1),
  1285. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1286. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI0),
  1287. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1288. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_TGL_H_NHI1),
  1289. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1290. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI0),
  1291. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1292. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_ADL_NHI1),
  1293. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1294. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI0),
  1295. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1296. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_RPL_NHI1),
  1297. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1298. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_M_NHI0),
  1299. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1300. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI0),
  1301. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1302. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_MTL_P_NHI1),
  1303. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1304. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_LNL_NHI0),
  1305. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1306. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_LNL_NHI1),
  1307. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1308. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_PTL_M_NHI0),
  1309. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1310. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_PTL_M_NHI1),
  1311. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1312. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_PTL_P_NHI0),
  1313. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1314. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_PTL_P_NHI1),
  1315. .driver_data = (kernel_ulong_t)&icl_nhi_ops },
  1316. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_80G_NHI) },
  1317. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_BARLOW_RIDGE_HOST_40G_NHI) },
  1318. /* Any USB4 compliant host */
  1319. { PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_USB4, ~0) },
  1320. { 0,}
  1321. };
  1322. MODULE_DEVICE_TABLE(pci, nhi_ids);
  1323. MODULE_DESCRIPTION("Thunderbolt/USB4 core driver");
  1324. MODULE_LICENSE("GPL");
  1325. static struct pci_driver nhi_driver = {
  1326. .name = "thunderbolt",
  1327. .id_table = nhi_ids,
  1328. .probe = nhi_probe,
  1329. .remove = nhi_remove,
  1330. .shutdown = nhi_remove,
  1331. .driver.pm = &nhi_pm_ops,
  1332. };
  1333. static int __init nhi_init(void)
  1334. {
  1335. int ret;
  1336. ret = tb_domain_init();
  1337. if (ret)
  1338. return ret;
  1339. ret = pci_register_driver(&nhi_driver);
  1340. if (ret)
  1341. tb_domain_exit();
  1342. return ret;
  1343. }
  1344. static void __exit nhi_unload(void)
  1345. {
  1346. pci_unregister_driver(&nhi_driver);
  1347. tb_domain_exit();
  1348. }
  1349. rootfs_initcall(nhi_init);
  1350. module_exit(nhi_unload);