nhi.c 41 KB

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