xhci-dbc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xhci-dbc.c - xHCI debug capability early driver
  4. *
  5. * Copyright (C) 2016 Intel Corporation
  6. *
  7. * Author: Lu Baolu <[email protected]>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  10. #include <linux/console.h>
  11. #include <linux/pci_regs.h>
  12. #include <linux/pci_ids.h>
  13. #include <linux/memblock.h>
  14. #include <linux/io.h>
  15. #include <asm/pci-direct.h>
  16. #include <asm/fixmap.h>
  17. #include <linux/bcd.h>
  18. #include <linux/export.h>
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/kthread.h>
  22. #include <linux/usb/xhci-dbgp.h>
  23. #include "../host/xhci.h"
  24. #include "xhci-dbc.h"
  25. static struct xdbc_state xdbc;
  26. static bool early_console_keep;
  27. #ifdef XDBC_TRACE
  28. #define xdbc_trace trace_printk
  29. #else
  30. static inline void xdbc_trace(const char *fmt, ...) { }
  31. #endif /* XDBC_TRACE */
  32. static void __iomem * __init xdbc_map_pci_mmio(u32 bus, u32 dev, u32 func)
  33. {
  34. u64 val64, sz64, mask64;
  35. void __iomem *base;
  36. u32 val, sz;
  37. u8 byte;
  38. val = read_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0);
  39. write_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0, ~0);
  40. sz = read_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0);
  41. write_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0, val);
  42. if (val == 0xffffffff || sz == 0xffffffff) {
  43. pr_notice("invalid mmio bar\n");
  44. return NULL;
  45. }
  46. val64 = val & PCI_BASE_ADDRESS_MEM_MASK;
  47. sz64 = sz & PCI_BASE_ADDRESS_MEM_MASK;
  48. mask64 = PCI_BASE_ADDRESS_MEM_MASK;
  49. if ((val & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64) {
  50. val = read_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0 + 4);
  51. write_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0 + 4, ~0);
  52. sz = read_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0 + 4);
  53. write_pci_config(bus, dev, func, PCI_BASE_ADDRESS_0 + 4, val);
  54. val64 |= (u64)val << 32;
  55. sz64 |= (u64)sz << 32;
  56. mask64 |= ~0ULL << 32;
  57. }
  58. sz64 &= mask64;
  59. if (!sz64) {
  60. pr_notice("invalid mmio address\n");
  61. return NULL;
  62. }
  63. sz64 = 1ULL << __ffs64(sz64);
  64. /* Check if the mem space is enabled: */
  65. byte = read_pci_config_byte(bus, dev, func, PCI_COMMAND);
  66. if (!(byte & PCI_COMMAND_MEMORY)) {
  67. byte |= PCI_COMMAND_MEMORY;
  68. write_pci_config_byte(bus, dev, func, PCI_COMMAND, byte);
  69. }
  70. xdbc.xhci_start = val64;
  71. xdbc.xhci_length = sz64;
  72. base = early_ioremap(val64, sz64);
  73. return base;
  74. }
  75. static void * __init xdbc_get_page(dma_addr_t *dma_addr)
  76. {
  77. void *virt;
  78. virt = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
  79. if (!virt)
  80. return NULL;
  81. if (dma_addr)
  82. *dma_addr = (dma_addr_t)__pa(virt);
  83. return virt;
  84. }
  85. static u32 __init xdbc_find_dbgp(int xdbc_num, u32 *b, u32 *d, u32 *f)
  86. {
  87. u32 bus, dev, func, class;
  88. for (bus = 0; bus < XDBC_PCI_MAX_BUSES; bus++) {
  89. for (dev = 0; dev < XDBC_PCI_MAX_DEVICES; dev++) {
  90. for (func = 0; func < XDBC_PCI_MAX_FUNCTION; func++) {
  91. class = read_pci_config(bus, dev, func, PCI_CLASS_REVISION);
  92. if ((class >> 8) != PCI_CLASS_SERIAL_USB_XHCI)
  93. continue;
  94. if (xdbc_num-- != 0)
  95. continue;
  96. *b = bus;
  97. *d = dev;
  98. *f = func;
  99. return 0;
  100. }
  101. }
  102. }
  103. return -1;
  104. }
  105. static int handshake(void __iomem *ptr, u32 mask, u32 done, int wait, int delay)
  106. {
  107. u32 result;
  108. /* Can not use readl_poll_timeout_atomic() for early boot things */
  109. do {
  110. result = readl(ptr);
  111. result &= mask;
  112. if (result == done)
  113. return 0;
  114. udelay(delay);
  115. wait -= delay;
  116. } while (wait > 0);
  117. return -ETIMEDOUT;
  118. }
  119. static void __init xdbc_bios_handoff(void)
  120. {
  121. int offset, timeout;
  122. u32 val;
  123. offset = xhci_find_next_ext_cap(xdbc.xhci_base, 0, XHCI_EXT_CAPS_LEGACY);
  124. val = readl(xdbc.xhci_base + offset);
  125. if (val & XHCI_HC_BIOS_OWNED) {
  126. writel(val | XHCI_HC_OS_OWNED, xdbc.xhci_base + offset);
  127. timeout = handshake(xdbc.xhci_base + offset, XHCI_HC_BIOS_OWNED, 0, 5000, 10);
  128. if (timeout) {
  129. pr_notice("failed to hand over xHCI control from BIOS\n");
  130. writel(val & ~XHCI_HC_BIOS_OWNED, xdbc.xhci_base + offset);
  131. }
  132. }
  133. /* Disable BIOS SMIs and clear all SMI events: */
  134. val = readl(xdbc.xhci_base + offset + XHCI_LEGACY_CONTROL_OFFSET);
  135. val &= XHCI_LEGACY_DISABLE_SMI;
  136. val |= XHCI_LEGACY_SMI_EVENTS;
  137. writel(val, xdbc.xhci_base + offset + XHCI_LEGACY_CONTROL_OFFSET);
  138. }
  139. static int __init
  140. xdbc_alloc_ring(struct xdbc_segment *seg, struct xdbc_ring *ring)
  141. {
  142. seg->trbs = xdbc_get_page(&seg->dma);
  143. if (!seg->trbs)
  144. return -ENOMEM;
  145. ring->segment = seg;
  146. return 0;
  147. }
  148. static void __init xdbc_free_ring(struct xdbc_ring *ring)
  149. {
  150. struct xdbc_segment *seg = ring->segment;
  151. if (!seg)
  152. return;
  153. memblock_phys_free(seg->dma, PAGE_SIZE);
  154. ring->segment = NULL;
  155. }
  156. static void xdbc_reset_ring(struct xdbc_ring *ring)
  157. {
  158. struct xdbc_segment *seg = ring->segment;
  159. struct xdbc_trb *link_trb;
  160. memset(seg->trbs, 0, PAGE_SIZE);
  161. ring->enqueue = seg->trbs;
  162. ring->dequeue = seg->trbs;
  163. ring->cycle_state = 1;
  164. if (ring != &xdbc.evt_ring) {
  165. link_trb = &seg->trbs[XDBC_TRBS_PER_SEGMENT - 1];
  166. link_trb->field[0] = cpu_to_le32(lower_32_bits(seg->dma));
  167. link_trb->field[1] = cpu_to_le32(upper_32_bits(seg->dma));
  168. link_trb->field[3] = cpu_to_le32(TRB_TYPE(TRB_LINK)) | cpu_to_le32(LINK_TOGGLE);
  169. }
  170. }
  171. static inline void xdbc_put_utf16(u16 *s, const char *c, size_t size)
  172. {
  173. int i;
  174. for (i = 0; i < size; i++)
  175. s[i] = cpu_to_le16(c[i]);
  176. }
  177. static void xdbc_mem_init(void)
  178. {
  179. struct xdbc_ep_context *ep_in, *ep_out;
  180. struct usb_string_descriptor *s_desc;
  181. struct xdbc_erst_entry *entry;
  182. struct xdbc_strings *strings;
  183. struct xdbc_context *ctx;
  184. unsigned int max_burst;
  185. u32 string_length;
  186. int index = 0;
  187. u32 dev_info;
  188. xdbc_reset_ring(&xdbc.evt_ring);
  189. xdbc_reset_ring(&xdbc.in_ring);
  190. xdbc_reset_ring(&xdbc.out_ring);
  191. memset(xdbc.table_base, 0, PAGE_SIZE);
  192. memset(xdbc.out_buf, 0, PAGE_SIZE);
  193. /* Initialize event ring segment table: */
  194. xdbc.erst_size = 16;
  195. xdbc.erst_base = xdbc.table_base + index * XDBC_TABLE_ENTRY_SIZE;
  196. xdbc.erst_dma = xdbc.table_dma + index * XDBC_TABLE_ENTRY_SIZE;
  197. index += XDBC_ERST_ENTRY_NUM;
  198. entry = (struct xdbc_erst_entry *)xdbc.erst_base;
  199. entry->seg_addr = cpu_to_le64(xdbc.evt_seg.dma);
  200. entry->seg_size = cpu_to_le32(XDBC_TRBS_PER_SEGMENT);
  201. entry->__reserved_0 = 0;
  202. /* Initialize ERST registers: */
  203. writel(1, &xdbc.xdbc_reg->ersts);
  204. xdbc_write64(xdbc.erst_dma, &xdbc.xdbc_reg->erstba);
  205. xdbc_write64(xdbc.evt_seg.dma, &xdbc.xdbc_reg->erdp);
  206. /* Debug capability contexts: */
  207. xdbc.dbcc_size = 64 * 3;
  208. xdbc.dbcc_base = xdbc.table_base + index * XDBC_TABLE_ENTRY_SIZE;
  209. xdbc.dbcc_dma = xdbc.table_dma + index * XDBC_TABLE_ENTRY_SIZE;
  210. index += XDBC_DBCC_ENTRY_NUM;
  211. /* Popluate the strings: */
  212. xdbc.string_size = sizeof(struct xdbc_strings);
  213. xdbc.string_base = xdbc.table_base + index * XDBC_TABLE_ENTRY_SIZE;
  214. xdbc.string_dma = xdbc.table_dma + index * XDBC_TABLE_ENTRY_SIZE;
  215. strings = (struct xdbc_strings *)xdbc.string_base;
  216. index += XDBC_STRING_ENTRY_NUM;
  217. /* Serial string: */
  218. s_desc = (struct usb_string_descriptor *)strings->serial;
  219. s_desc->bLength = (strlen(XDBC_STRING_SERIAL) + 1) * 2;
  220. s_desc->bDescriptorType = USB_DT_STRING;
  221. xdbc_put_utf16(s_desc->wData, XDBC_STRING_SERIAL, strlen(XDBC_STRING_SERIAL));
  222. string_length = s_desc->bLength;
  223. string_length <<= 8;
  224. /* Product string: */
  225. s_desc = (struct usb_string_descriptor *)strings->product;
  226. s_desc->bLength = (strlen(XDBC_STRING_PRODUCT) + 1) * 2;
  227. s_desc->bDescriptorType = USB_DT_STRING;
  228. xdbc_put_utf16(s_desc->wData, XDBC_STRING_PRODUCT, strlen(XDBC_STRING_PRODUCT));
  229. string_length += s_desc->bLength;
  230. string_length <<= 8;
  231. /* Manufacture string: */
  232. s_desc = (struct usb_string_descriptor *)strings->manufacturer;
  233. s_desc->bLength = (strlen(XDBC_STRING_MANUFACTURER) + 1) * 2;
  234. s_desc->bDescriptorType = USB_DT_STRING;
  235. xdbc_put_utf16(s_desc->wData, XDBC_STRING_MANUFACTURER, strlen(XDBC_STRING_MANUFACTURER));
  236. string_length += s_desc->bLength;
  237. string_length <<= 8;
  238. /* String0: */
  239. strings->string0[0] = 4;
  240. strings->string0[1] = USB_DT_STRING;
  241. strings->string0[2] = 0x09;
  242. strings->string0[3] = 0x04;
  243. string_length += 4;
  244. /* Populate info Context: */
  245. ctx = (struct xdbc_context *)xdbc.dbcc_base;
  246. ctx->info.string0 = cpu_to_le64(xdbc.string_dma);
  247. ctx->info.manufacturer = cpu_to_le64(xdbc.string_dma + XDBC_MAX_STRING_LENGTH);
  248. ctx->info.product = cpu_to_le64(xdbc.string_dma + XDBC_MAX_STRING_LENGTH * 2);
  249. ctx->info.serial = cpu_to_le64(xdbc.string_dma + XDBC_MAX_STRING_LENGTH * 3);
  250. ctx->info.length = cpu_to_le32(string_length);
  251. /* Populate bulk out endpoint context: */
  252. max_burst = DEBUG_MAX_BURST(readl(&xdbc.xdbc_reg->control));
  253. ep_out = (struct xdbc_ep_context *)&ctx->out;
  254. ep_out->ep_info1 = 0;
  255. ep_out->ep_info2 = cpu_to_le32(EP_TYPE(BULK_OUT_EP) | MAX_PACKET(1024) | MAX_BURST(max_burst));
  256. ep_out->deq = cpu_to_le64(xdbc.out_seg.dma | xdbc.out_ring.cycle_state);
  257. /* Populate bulk in endpoint context: */
  258. ep_in = (struct xdbc_ep_context *)&ctx->in;
  259. ep_in->ep_info1 = 0;
  260. ep_in->ep_info2 = cpu_to_le32(EP_TYPE(BULK_IN_EP) | MAX_PACKET(1024) | MAX_BURST(max_burst));
  261. ep_in->deq = cpu_to_le64(xdbc.in_seg.dma | xdbc.in_ring.cycle_state);
  262. /* Set DbC context and info registers: */
  263. xdbc_write64(xdbc.dbcc_dma, &xdbc.xdbc_reg->dccp);
  264. dev_info = cpu_to_le32((XDBC_VENDOR_ID << 16) | XDBC_PROTOCOL);
  265. writel(dev_info, &xdbc.xdbc_reg->devinfo1);
  266. dev_info = cpu_to_le32((XDBC_DEVICE_REV << 16) | XDBC_PRODUCT_ID);
  267. writel(dev_info, &xdbc.xdbc_reg->devinfo2);
  268. xdbc.in_buf = xdbc.out_buf + XDBC_MAX_PACKET;
  269. xdbc.in_dma = xdbc.out_dma + XDBC_MAX_PACKET;
  270. }
  271. static void xdbc_do_reset_debug_port(u32 id, u32 count)
  272. {
  273. void __iomem *ops_reg;
  274. void __iomem *portsc;
  275. u32 val, cap_length;
  276. int i;
  277. cap_length = readl(xdbc.xhci_base) & 0xff;
  278. ops_reg = xdbc.xhci_base + cap_length;
  279. id--;
  280. for (i = id; i < (id + count); i++) {
  281. portsc = ops_reg + 0x400 + i * 0x10;
  282. val = readl(portsc);
  283. if (!(val & PORT_CONNECT))
  284. writel(val | PORT_RESET, portsc);
  285. }
  286. }
  287. static void xdbc_reset_debug_port(void)
  288. {
  289. u32 val, port_offset, port_count;
  290. int offset = 0;
  291. do {
  292. offset = xhci_find_next_ext_cap(xdbc.xhci_base, offset, XHCI_EXT_CAPS_PROTOCOL);
  293. if (!offset)
  294. break;
  295. val = readl(xdbc.xhci_base + offset);
  296. if (XHCI_EXT_PORT_MAJOR(val) != 0x3)
  297. continue;
  298. val = readl(xdbc.xhci_base + offset + 8);
  299. port_offset = XHCI_EXT_PORT_OFF(val);
  300. port_count = XHCI_EXT_PORT_COUNT(val);
  301. xdbc_do_reset_debug_port(port_offset, port_count);
  302. } while (1);
  303. }
  304. static void
  305. xdbc_queue_trb(struct xdbc_ring *ring, u32 field1, u32 field2, u32 field3, u32 field4)
  306. {
  307. struct xdbc_trb *trb, *link_trb;
  308. trb = ring->enqueue;
  309. trb->field[0] = cpu_to_le32(field1);
  310. trb->field[1] = cpu_to_le32(field2);
  311. trb->field[2] = cpu_to_le32(field3);
  312. trb->field[3] = cpu_to_le32(field4);
  313. ++(ring->enqueue);
  314. if (ring->enqueue >= &ring->segment->trbs[TRBS_PER_SEGMENT - 1]) {
  315. link_trb = ring->enqueue;
  316. if (ring->cycle_state)
  317. link_trb->field[3] |= cpu_to_le32(TRB_CYCLE);
  318. else
  319. link_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
  320. ring->enqueue = ring->segment->trbs;
  321. ring->cycle_state ^= 1;
  322. }
  323. }
  324. static void xdbc_ring_doorbell(int target)
  325. {
  326. writel(DOOR_BELL_TARGET(target), &xdbc.xdbc_reg->doorbell);
  327. }
  328. static int xdbc_start(void)
  329. {
  330. u32 ctrl, status;
  331. int ret;
  332. ctrl = readl(&xdbc.xdbc_reg->control);
  333. writel(ctrl | CTRL_DBC_ENABLE | CTRL_PORT_ENABLE, &xdbc.xdbc_reg->control);
  334. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_ENABLE, CTRL_DBC_ENABLE, 100000, 100);
  335. if (ret) {
  336. xdbc_trace("failed to initialize hardware\n");
  337. return ret;
  338. }
  339. /* Reset port to avoid bus hang: */
  340. if (xdbc.vendor == PCI_VENDOR_ID_INTEL)
  341. xdbc_reset_debug_port();
  342. /* Wait for port connection: */
  343. ret = handshake(&xdbc.xdbc_reg->portsc, PORTSC_CONN_STATUS, PORTSC_CONN_STATUS, 5000000, 100);
  344. if (ret) {
  345. xdbc_trace("waiting for connection timed out\n");
  346. return ret;
  347. }
  348. /* Wait for debug device to be configured: */
  349. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_RUN, CTRL_DBC_RUN, 5000000, 100);
  350. if (ret) {
  351. xdbc_trace("waiting for device configuration timed out\n");
  352. return ret;
  353. }
  354. /* Check port number: */
  355. status = readl(&xdbc.xdbc_reg->status);
  356. if (!DCST_DEBUG_PORT(status)) {
  357. xdbc_trace("invalid root hub port number\n");
  358. return -ENODEV;
  359. }
  360. xdbc.port_number = DCST_DEBUG_PORT(status);
  361. xdbc_trace("DbC is running now, control 0x%08x port ID %d\n",
  362. readl(&xdbc.xdbc_reg->control), xdbc.port_number);
  363. return 0;
  364. }
  365. static int xdbc_bulk_transfer(void *data, int size, bool read)
  366. {
  367. struct xdbc_ring *ring;
  368. struct xdbc_trb *trb;
  369. u32 length, control;
  370. u32 cycle;
  371. u64 addr;
  372. if (size > XDBC_MAX_PACKET) {
  373. xdbc_trace("bad parameter, size %d\n", size);
  374. return -EINVAL;
  375. }
  376. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED) ||
  377. !(xdbc.flags & XDBC_FLAGS_CONFIGURED) ||
  378. (!read && (xdbc.flags & XDBC_FLAGS_OUT_STALL)) ||
  379. (read && (xdbc.flags & XDBC_FLAGS_IN_STALL))) {
  380. xdbc_trace("connection not ready, flags %08x\n", xdbc.flags);
  381. return -EIO;
  382. }
  383. ring = (read ? &xdbc.in_ring : &xdbc.out_ring);
  384. trb = ring->enqueue;
  385. cycle = ring->cycle_state;
  386. length = TRB_LEN(size);
  387. control = TRB_TYPE(TRB_NORMAL) | TRB_IOC;
  388. if (cycle)
  389. control &= cpu_to_le32(~TRB_CYCLE);
  390. else
  391. control |= cpu_to_le32(TRB_CYCLE);
  392. if (read) {
  393. memset(xdbc.in_buf, 0, XDBC_MAX_PACKET);
  394. addr = xdbc.in_dma;
  395. xdbc.flags |= XDBC_FLAGS_IN_PROCESS;
  396. } else {
  397. memset(xdbc.out_buf, 0, XDBC_MAX_PACKET);
  398. memcpy(xdbc.out_buf, data, size);
  399. addr = xdbc.out_dma;
  400. xdbc.flags |= XDBC_FLAGS_OUT_PROCESS;
  401. }
  402. xdbc_queue_trb(ring, lower_32_bits(addr), upper_32_bits(addr), length, control);
  403. /*
  404. * Add a barrier between writes of trb fields and flipping
  405. * the cycle bit:
  406. */
  407. wmb();
  408. if (cycle)
  409. trb->field[3] |= cpu_to_le32(cycle);
  410. else
  411. trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
  412. xdbc_ring_doorbell(read ? IN_EP_DOORBELL : OUT_EP_DOORBELL);
  413. return size;
  414. }
  415. static int xdbc_handle_external_reset(void)
  416. {
  417. int ret = 0;
  418. xdbc.flags = 0;
  419. writel(0, &xdbc.xdbc_reg->control);
  420. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_ENABLE, 0, 100000, 10);
  421. if (ret)
  422. goto reset_out;
  423. xdbc_mem_init();
  424. ret = xdbc_start();
  425. if (ret < 0)
  426. goto reset_out;
  427. xdbc_trace("dbc recovered\n");
  428. xdbc.flags |= XDBC_FLAGS_INITIALIZED | XDBC_FLAGS_CONFIGURED;
  429. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  430. return 0;
  431. reset_out:
  432. xdbc_trace("failed to recover from external reset\n");
  433. return ret;
  434. }
  435. static int __init xdbc_early_setup(void)
  436. {
  437. int ret;
  438. writel(0, &xdbc.xdbc_reg->control);
  439. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_ENABLE, 0, 100000, 100);
  440. if (ret)
  441. return ret;
  442. /* Allocate the table page: */
  443. xdbc.table_base = xdbc_get_page(&xdbc.table_dma);
  444. if (!xdbc.table_base)
  445. return -ENOMEM;
  446. /* Get and store the transfer buffer: */
  447. xdbc.out_buf = xdbc_get_page(&xdbc.out_dma);
  448. if (!xdbc.out_buf)
  449. return -ENOMEM;
  450. /* Allocate the event ring: */
  451. ret = xdbc_alloc_ring(&xdbc.evt_seg, &xdbc.evt_ring);
  452. if (ret < 0)
  453. return ret;
  454. /* Allocate IN/OUT endpoint transfer rings: */
  455. ret = xdbc_alloc_ring(&xdbc.in_seg, &xdbc.in_ring);
  456. if (ret < 0)
  457. return ret;
  458. ret = xdbc_alloc_ring(&xdbc.out_seg, &xdbc.out_ring);
  459. if (ret < 0)
  460. return ret;
  461. xdbc_mem_init();
  462. ret = xdbc_start();
  463. if (ret < 0) {
  464. writel(0, &xdbc.xdbc_reg->control);
  465. return ret;
  466. }
  467. xdbc.flags |= XDBC_FLAGS_INITIALIZED | XDBC_FLAGS_CONFIGURED;
  468. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  469. return 0;
  470. }
  471. int __init early_xdbc_parse_parameter(char *s, int keep_early)
  472. {
  473. unsigned long dbgp_num = 0;
  474. u32 bus, dev, func, offset;
  475. char *e;
  476. int ret;
  477. if (!early_pci_allowed())
  478. return -EPERM;
  479. early_console_keep = keep_early;
  480. if (xdbc.xdbc_reg)
  481. return 0;
  482. if (*s) {
  483. dbgp_num = simple_strtoul(s, &e, 10);
  484. if (s == e)
  485. dbgp_num = 0;
  486. }
  487. pr_notice("dbgp_num: %lu\n", dbgp_num);
  488. /* Locate the host controller: */
  489. ret = xdbc_find_dbgp(dbgp_num, &bus, &dev, &func);
  490. if (ret) {
  491. pr_notice("failed to locate xhci host\n");
  492. return -ENODEV;
  493. }
  494. xdbc.vendor = read_pci_config_16(bus, dev, func, PCI_VENDOR_ID);
  495. xdbc.device = read_pci_config_16(bus, dev, func, PCI_DEVICE_ID);
  496. xdbc.bus = bus;
  497. xdbc.dev = dev;
  498. xdbc.func = func;
  499. /* Map the IO memory: */
  500. xdbc.xhci_base = xdbc_map_pci_mmio(bus, dev, func);
  501. if (!xdbc.xhci_base)
  502. return -EINVAL;
  503. /* Locate DbC registers: */
  504. offset = xhci_find_next_ext_cap(xdbc.xhci_base, 0, XHCI_EXT_CAPS_DEBUG);
  505. if (!offset) {
  506. pr_notice("xhci host doesn't support debug capability\n");
  507. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  508. xdbc.xhci_base = NULL;
  509. xdbc.xhci_length = 0;
  510. return -ENODEV;
  511. }
  512. xdbc.xdbc_reg = (struct xdbc_regs __iomem *)(xdbc.xhci_base + offset);
  513. return 0;
  514. }
  515. int __init early_xdbc_setup_hardware(void)
  516. {
  517. int ret;
  518. if (!xdbc.xdbc_reg)
  519. return -ENODEV;
  520. xdbc_bios_handoff();
  521. raw_spin_lock_init(&xdbc.lock);
  522. ret = xdbc_early_setup();
  523. if (ret) {
  524. pr_notice("failed to setup the connection to host\n");
  525. xdbc_free_ring(&xdbc.evt_ring);
  526. xdbc_free_ring(&xdbc.out_ring);
  527. xdbc_free_ring(&xdbc.in_ring);
  528. if (xdbc.table_dma)
  529. memblock_phys_free(xdbc.table_dma, PAGE_SIZE);
  530. if (xdbc.out_dma)
  531. memblock_phys_free(xdbc.out_dma, PAGE_SIZE);
  532. xdbc.table_base = NULL;
  533. xdbc.out_buf = NULL;
  534. }
  535. return ret;
  536. }
  537. static void xdbc_handle_port_status(struct xdbc_trb *evt_trb)
  538. {
  539. u32 port_reg;
  540. port_reg = readl(&xdbc.xdbc_reg->portsc);
  541. if (port_reg & PORTSC_CONN_CHANGE) {
  542. xdbc_trace("connect status change event\n");
  543. /* Check whether cable unplugged: */
  544. if (!(port_reg & PORTSC_CONN_STATUS)) {
  545. xdbc.flags = 0;
  546. xdbc_trace("cable unplugged\n");
  547. }
  548. }
  549. if (port_reg & PORTSC_RESET_CHANGE)
  550. xdbc_trace("port reset change event\n");
  551. if (port_reg & PORTSC_LINK_CHANGE)
  552. xdbc_trace("port link status change event\n");
  553. if (port_reg & PORTSC_CONFIG_CHANGE)
  554. xdbc_trace("config error change\n");
  555. /* Write back the value to clear RW1C bits: */
  556. writel(port_reg, &xdbc.xdbc_reg->portsc);
  557. }
  558. static void xdbc_handle_tx_event(struct xdbc_trb *evt_trb)
  559. {
  560. u32 comp_code;
  561. int ep_id;
  562. comp_code = GET_COMP_CODE(le32_to_cpu(evt_trb->field[2]));
  563. ep_id = TRB_TO_EP_ID(le32_to_cpu(evt_trb->field[3]));
  564. switch (comp_code) {
  565. case COMP_SUCCESS:
  566. case COMP_SHORT_PACKET:
  567. break;
  568. case COMP_TRB_ERROR:
  569. case COMP_BABBLE_DETECTED_ERROR:
  570. case COMP_USB_TRANSACTION_ERROR:
  571. case COMP_STALL_ERROR:
  572. default:
  573. if (ep_id == XDBC_EPID_OUT || ep_id == XDBC_EPID_OUT_INTEL)
  574. xdbc.flags |= XDBC_FLAGS_OUT_STALL;
  575. if (ep_id == XDBC_EPID_IN || ep_id == XDBC_EPID_IN_INTEL)
  576. xdbc.flags |= XDBC_FLAGS_IN_STALL;
  577. xdbc_trace("endpoint %d stalled\n", ep_id);
  578. break;
  579. }
  580. if (ep_id == XDBC_EPID_IN || ep_id == XDBC_EPID_IN_INTEL) {
  581. xdbc.flags &= ~XDBC_FLAGS_IN_PROCESS;
  582. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  583. } else if (ep_id == XDBC_EPID_OUT || ep_id == XDBC_EPID_OUT_INTEL) {
  584. xdbc.flags &= ~XDBC_FLAGS_OUT_PROCESS;
  585. } else {
  586. xdbc_trace("invalid endpoint id %d\n", ep_id);
  587. }
  588. }
  589. static void xdbc_handle_events(void)
  590. {
  591. struct xdbc_trb *evt_trb;
  592. bool update_erdp = false;
  593. u32 reg;
  594. u8 cmd;
  595. cmd = read_pci_config_byte(xdbc.bus, xdbc.dev, xdbc.func, PCI_COMMAND);
  596. if (!(cmd & PCI_COMMAND_MASTER)) {
  597. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  598. write_pci_config_byte(xdbc.bus, xdbc.dev, xdbc.func, PCI_COMMAND, cmd);
  599. }
  600. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED))
  601. return;
  602. /* Handle external reset events: */
  603. reg = readl(&xdbc.xdbc_reg->control);
  604. if (!(reg & CTRL_DBC_ENABLE)) {
  605. if (xdbc_handle_external_reset()) {
  606. xdbc_trace("failed to recover connection\n");
  607. return;
  608. }
  609. }
  610. /* Handle configure-exit event: */
  611. reg = readl(&xdbc.xdbc_reg->control);
  612. if (reg & CTRL_DBC_RUN_CHANGE) {
  613. writel(reg, &xdbc.xdbc_reg->control);
  614. if (reg & CTRL_DBC_RUN)
  615. xdbc.flags |= XDBC_FLAGS_CONFIGURED;
  616. else
  617. xdbc.flags &= ~XDBC_FLAGS_CONFIGURED;
  618. }
  619. /* Handle endpoint stall event: */
  620. reg = readl(&xdbc.xdbc_reg->control);
  621. if (reg & CTRL_HALT_IN_TR) {
  622. xdbc.flags |= XDBC_FLAGS_IN_STALL;
  623. } else {
  624. xdbc.flags &= ~XDBC_FLAGS_IN_STALL;
  625. if (!(xdbc.flags & XDBC_FLAGS_IN_PROCESS))
  626. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  627. }
  628. if (reg & CTRL_HALT_OUT_TR)
  629. xdbc.flags |= XDBC_FLAGS_OUT_STALL;
  630. else
  631. xdbc.flags &= ~XDBC_FLAGS_OUT_STALL;
  632. /* Handle the events in the event ring: */
  633. evt_trb = xdbc.evt_ring.dequeue;
  634. while ((le32_to_cpu(evt_trb->field[3]) & TRB_CYCLE) == xdbc.evt_ring.cycle_state) {
  635. /*
  636. * Add a barrier between reading the cycle flag and any
  637. * reads of the event's flags/data below:
  638. */
  639. rmb();
  640. switch ((le32_to_cpu(evt_trb->field[3]) & TRB_TYPE_BITMASK)) {
  641. case TRB_TYPE(TRB_PORT_STATUS):
  642. xdbc_handle_port_status(evt_trb);
  643. break;
  644. case TRB_TYPE(TRB_TRANSFER):
  645. xdbc_handle_tx_event(evt_trb);
  646. break;
  647. default:
  648. break;
  649. }
  650. ++(xdbc.evt_ring.dequeue);
  651. if (xdbc.evt_ring.dequeue == &xdbc.evt_seg.trbs[TRBS_PER_SEGMENT]) {
  652. xdbc.evt_ring.dequeue = xdbc.evt_seg.trbs;
  653. xdbc.evt_ring.cycle_state ^= 1;
  654. }
  655. evt_trb = xdbc.evt_ring.dequeue;
  656. update_erdp = true;
  657. }
  658. /* Update event ring dequeue pointer: */
  659. if (update_erdp)
  660. xdbc_write64(__pa(xdbc.evt_ring.dequeue), &xdbc.xdbc_reg->erdp);
  661. }
  662. static int xdbc_bulk_write(const char *bytes, int size)
  663. {
  664. int ret, timeout = 0;
  665. unsigned long flags;
  666. retry:
  667. if (in_nmi()) {
  668. if (!raw_spin_trylock_irqsave(&xdbc.lock, flags))
  669. return -EAGAIN;
  670. } else {
  671. raw_spin_lock_irqsave(&xdbc.lock, flags);
  672. }
  673. xdbc_handle_events();
  674. /* Check completion of the previous request: */
  675. if ((xdbc.flags & XDBC_FLAGS_OUT_PROCESS) && (timeout < 2000000)) {
  676. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  677. udelay(100);
  678. timeout += 100;
  679. goto retry;
  680. }
  681. if (xdbc.flags & XDBC_FLAGS_OUT_PROCESS) {
  682. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  683. xdbc_trace("previous transfer not completed yet\n");
  684. return -ETIMEDOUT;
  685. }
  686. ret = xdbc_bulk_transfer((void *)bytes, size, false);
  687. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  688. return ret;
  689. }
  690. static void early_xdbc_write(struct console *con, const char *str, u32 n)
  691. {
  692. /* static variables are zeroed, so buf is always NULL terminated */
  693. static char buf[XDBC_MAX_PACKET + 1];
  694. int chunk, ret;
  695. int use_cr = 0;
  696. if (!xdbc.xdbc_reg)
  697. return;
  698. memset(buf, 0, XDBC_MAX_PACKET);
  699. while (n > 0) {
  700. for (chunk = 0; chunk < XDBC_MAX_PACKET && n > 0; str++, chunk++, n--) {
  701. if (!use_cr && *str == '\n') {
  702. use_cr = 1;
  703. buf[chunk] = '\r';
  704. str--;
  705. n++;
  706. continue;
  707. }
  708. if (use_cr)
  709. use_cr = 0;
  710. buf[chunk] = *str;
  711. }
  712. if (chunk > 0) {
  713. ret = xdbc_bulk_write(buf, chunk);
  714. if (ret < 0)
  715. xdbc_trace("missed message {%s}\n", buf);
  716. }
  717. }
  718. }
  719. static struct console early_xdbc_console = {
  720. .name = "earlyxdbc",
  721. .write = early_xdbc_write,
  722. .flags = CON_PRINTBUFFER,
  723. .index = -1,
  724. };
  725. void __init early_xdbc_register_console(void)
  726. {
  727. if (early_console)
  728. return;
  729. early_console = &early_xdbc_console;
  730. if (early_console_keep)
  731. early_console->flags &= ~CON_BOOT;
  732. else
  733. early_console->flags |= CON_BOOT;
  734. register_console(early_console);
  735. }
  736. static void xdbc_unregister_console(void)
  737. {
  738. if (early_xdbc_console.flags & CON_ENABLED)
  739. unregister_console(&early_xdbc_console);
  740. }
  741. static int xdbc_scrub_function(void *ptr)
  742. {
  743. unsigned long flags;
  744. while (true) {
  745. raw_spin_lock_irqsave(&xdbc.lock, flags);
  746. xdbc_handle_events();
  747. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED)) {
  748. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  749. break;
  750. }
  751. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  752. schedule_timeout_interruptible(1);
  753. }
  754. xdbc_unregister_console();
  755. writel(0, &xdbc.xdbc_reg->control);
  756. xdbc_trace("dbc scrub function exits\n");
  757. return 0;
  758. }
  759. static int __init xdbc_init(void)
  760. {
  761. unsigned long flags;
  762. void __iomem *base;
  763. int ret = 0;
  764. u32 offset;
  765. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED))
  766. return 0;
  767. /*
  768. * It's time to shut down the DbC, so that the debug
  769. * port can be reused by the host controller:
  770. */
  771. if (early_xdbc_console.index == -1 ||
  772. (early_xdbc_console.flags & CON_BOOT)) {
  773. xdbc_trace("hardware not used anymore\n");
  774. goto free_and_quit;
  775. }
  776. base = ioremap(xdbc.xhci_start, xdbc.xhci_length);
  777. if (!base) {
  778. xdbc_trace("failed to remap the io address\n");
  779. ret = -ENOMEM;
  780. goto free_and_quit;
  781. }
  782. raw_spin_lock_irqsave(&xdbc.lock, flags);
  783. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  784. xdbc.xhci_base = base;
  785. offset = xhci_find_next_ext_cap(xdbc.xhci_base, 0, XHCI_EXT_CAPS_DEBUG);
  786. xdbc.xdbc_reg = (struct xdbc_regs __iomem *)(xdbc.xhci_base + offset);
  787. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  788. kthread_run(xdbc_scrub_function, NULL, "%s", "xdbc");
  789. return 0;
  790. free_and_quit:
  791. xdbc_free_ring(&xdbc.evt_ring);
  792. xdbc_free_ring(&xdbc.out_ring);
  793. xdbc_free_ring(&xdbc.in_ring);
  794. memblock_phys_free(xdbc.table_dma, PAGE_SIZE);
  795. memblock_phys_free(xdbc.out_dma, PAGE_SIZE);
  796. writel(0, &xdbc.xdbc_reg->control);
  797. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  798. return ret;
  799. }
  800. subsys_initcall(xdbc_init);