xhci-dbc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 <baolu.lu@linux.intel.com>
  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. memcpy_and_pad(xdbc.out_buf, XDBC_MAX_PACKET, data, size, 0);
  398. addr = xdbc.out_dma;
  399. xdbc.flags |= XDBC_FLAGS_OUT_PROCESS;
  400. }
  401. xdbc_queue_trb(ring, lower_32_bits(addr), upper_32_bits(addr), length, control);
  402. /*
  403. * Add a barrier between writes of trb fields and flipping
  404. * the cycle bit:
  405. */
  406. wmb();
  407. if (cycle)
  408. trb->field[3] |= cpu_to_le32(cycle);
  409. else
  410. trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
  411. xdbc_ring_doorbell(read ? IN_EP_DOORBELL : OUT_EP_DOORBELL);
  412. return size;
  413. }
  414. static int xdbc_handle_external_reset(void)
  415. {
  416. int ret = 0;
  417. xdbc.flags = 0;
  418. writel(0, &xdbc.xdbc_reg->control);
  419. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_ENABLE, 0, 100000, 10);
  420. if (ret)
  421. goto reset_out;
  422. xdbc_mem_init();
  423. ret = xdbc_start();
  424. if (ret < 0)
  425. goto reset_out;
  426. xdbc_trace("dbc recovered\n");
  427. xdbc.flags |= XDBC_FLAGS_INITIALIZED | XDBC_FLAGS_CONFIGURED;
  428. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  429. return 0;
  430. reset_out:
  431. xdbc_trace("failed to recover from external reset\n");
  432. return ret;
  433. }
  434. static int __init xdbc_early_setup(void)
  435. {
  436. int ret;
  437. writel(0, &xdbc.xdbc_reg->control);
  438. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_ENABLE, 0, 100000, 100);
  439. if (ret)
  440. return ret;
  441. /* Allocate the table page: */
  442. xdbc.table_base = xdbc_get_page(&xdbc.table_dma);
  443. if (!xdbc.table_base)
  444. return -ENOMEM;
  445. /* Get and store the transfer buffer: */
  446. xdbc.out_buf = xdbc_get_page(&xdbc.out_dma);
  447. if (!xdbc.out_buf)
  448. return -ENOMEM;
  449. /* Allocate the event ring: */
  450. ret = xdbc_alloc_ring(&xdbc.evt_seg, &xdbc.evt_ring);
  451. if (ret < 0)
  452. return ret;
  453. /* Allocate IN/OUT endpoint transfer rings: */
  454. ret = xdbc_alloc_ring(&xdbc.in_seg, &xdbc.in_ring);
  455. if (ret < 0)
  456. return ret;
  457. ret = xdbc_alloc_ring(&xdbc.out_seg, &xdbc.out_ring);
  458. if (ret < 0)
  459. return ret;
  460. xdbc_mem_init();
  461. ret = xdbc_start();
  462. if (ret < 0) {
  463. writel(0, &xdbc.xdbc_reg->control);
  464. return ret;
  465. }
  466. xdbc.flags |= XDBC_FLAGS_INITIALIZED | XDBC_FLAGS_CONFIGURED;
  467. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  468. return 0;
  469. }
  470. int __init early_xdbc_parse_parameter(char *s, int keep_early)
  471. {
  472. unsigned long dbgp_num = 0;
  473. u32 bus, dev, func, offset;
  474. char *e;
  475. int ret;
  476. if (!early_pci_allowed())
  477. return -EPERM;
  478. early_console_keep = keep_early;
  479. if (xdbc.xdbc_reg)
  480. return 0;
  481. if (*s) {
  482. dbgp_num = simple_strtoul(s, &e, 10);
  483. if (s == e)
  484. dbgp_num = 0;
  485. }
  486. pr_notice("dbgp_num: %lu\n", dbgp_num);
  487. /* Locate the host controller: */
  488. ret = xdbc_find_dbgp(dbgp_num, &bus, &dev, &func);
  489. if (ret) {
  490. pr_notice("failed to locate xhci host\n");
  491. return -ENODEV;
  492. }
  493. xdbc.vendor = read_pci_config_16(bus, dev, func, PCI_VENDOR_ID);
  494. xdbc.device = read_pci_config_16(bus, dev, func, PCI_DEVICE_ID);
  495. xdbc.bus = bus;
  496. xdbc.dev = dev;
  497. xdbc.func = func;
  498. /* Map the IO memory: */
  499. xdbc.xhci_base = xdbc_map_pci_mmio(bus, dev, func);
  500. if (!xdbc.xhci_base)
  501. return -EINVAL;
  502. /* Locate DbC registers: */
  503. offset = xhci_find_next_ext_cap(xdbc.xhci_base, 0, XHCI_EXT_CAPS_DEBUG);
  504. if (!offset) {
  505. pr_notice("xhci host doesn't support debug capability\n");
  506. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  507. xdbc.xhci_base = NULL;
  508. xdbc.xhci_length = 0;
  509. return -ENODEV;
  510. }
  511. xdbc.xdbc_reg = (struct xdbc_regs __iomem *)(xdbc.xhci_base + offset);
  512. return 0;
  513. }
  514. int __init early_xdbc_setup_hardware(void)
  515. {
  516. int ret;
  517. if (!xdbc.xdbc_reg)
  518. return -ENODEV;
  519. xdbc_bios_handoff();
  520. raw_spin_lock_init(&xdbc.lock);
  521. ret = xdbc_early_setup();
  522. if (ret) {
  523. pr_notice("failed to setup the connection to host\n");
  524. xdbc_free_ring(&xdbc.evt_ring);
  525. xdbc_free_ring(&xdbc.out_ring);
  526. xdbc_free_ring(&xdbc.in_ring);
  527. if (xdbc.table_dma)
  528. memblock_phys_free(xdbc.table_dma, PAGE_SIZE);
  529. if (xdbc.out_dma)
  530. memblock_phys_free(xdbc.out_dma, PAGE_SIZE);
  531. xdbc.table_base = NULL;
  532. xdbc.out_buf = NULL;
  533. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  534. xdbc.xhci_base = NULL;
  535. xdbc.xhci_length = 0;
  536. }
  537. return ret;
  538. }
  539. static void xdbc_handle_port_status(struct xdbc_trb *evt_trb)
  540. {
  541. u32 port_reg;
  542. port_reg = readl(&xdbc.xdbc_reg->portsc);
  543. if (port_reg & PORTSC_CONN_CHANGE) {
  544. xdbc_trace("connect status change event\n");
  545. /* Check whether cable unplugged: */
  546. if (!(port_reg & PORTSC_CONN_STATUS)) {
  547. xdbc.flags = 0;
  548. xdbc_trace("cable unplugged\n");
  549. }
  550. }
  551. if (port_reg & PORTSC_RESET_CHANGE)
  552. xdbc_trace("port reset change event\n");
  553. if (port_reg & PORTSC_LINK_CHANGE)
  554. xdbc_trace("port link status change event\n");
  555. if (port_reg & PORTSC_CONFIG_CHANGE)
  556. xdbc_trace("config error change\n");
  557. /* Write back the value to clear RW1C bits: */
  558. writel(port_reg, &xdbc.xdbc_reg->portsc);
  559. }
  560. static void xdbc_handle_tx_event(struct xdbc_trb *evt_trb)
  561. {
  562. u32 comp_code;
  563. int ep_id;
  564. comp_code = GET_COMP_CODE(le32_to_cpu(evt_trb->field[2]));
  565. ep_id = TRB_TO_EP_ID(le32_to_cpu(evt_trb->field[3]));
  566. switch (comp_code) {
  567. case COMP_SUCCESS:
  568. case COMP_SHORT_PACKET:
  569. break;
  570. case COMP_TRB_ERROR:
  571. case COMP_BABBLE_DETECTED_ERROR:
  572. case COMP_USB_TRANSACTION_ERROR:
  573. case COMP_STALL_ERROR:
  574. default:
  575. if (ep_id == XDBC_EPID_OUT || ep_id == XDBC_EPID_OUT_INTEL)
  576. xdbc.flags |= XDBC_FLAGS_OUT_STALL;
  577. if (ep_id == XDBC_EPID_IN || ep_id == XDBC_EPID_IN_INTEL)
  578. xdbc.flags |= XDBC_FLAGS_IN_STALL;
  579. xdbc_trace("endpoint %d stalled\n", ep_id);
  580. break;
  581. }
  582. if (ep_id == XDBC_EPID_IN || ep_id == XDBC_EPID_IN_INTEL) {
  583. xdbc.flags &= ~XDBC_FLAGS_IN_PROCESS;
  584. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  585. } else if (ep_id == XDBC_EPID_OUT || ep_id == XDBC_EPID_OUT_INTEL) {
  586. xdbc.flags &= ~XDBC_FLAGS_OUT_PROCESS;
  587. } else {
  588. xdbc_trace("invalid endpoint id %d\n", ep_id);
  589. }
  590. }
  591. static void xdbc_handle_events(void)
  592. {
  593. struct xdbc_trb *evt_trb;
  594. bool update_erdp = false;
  595. u32 reg;
  596. u8 cmd;
  597. cmd = read_pci_config_byte(xdbc.bus, xdbc.dev, xdbc.func, PCI_COMMAND);
  598. if (!(cmd & PCI_COMMAND_MASTER)) {
  599. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  600. write_pci_config_byte(xdbc.bus, xdbc.dev, xdbc.func, PCI_COMMAND, cmd);
  601. }
  602. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED))
  603. return;
  604. /* Handle external reset events: */
  605. reg = readl(&xdbc.xdbc_reg->control);
  606. if (!(reg & CTRL_DBC_ENABLE)) {
  607. if (xdbc_handle_external_reset()) {
  608. xdbc_trace("failed to recover connection\n");
  609. return;
  610. }
  611. }
  612. /* Handle configure-exit event: */
  613. reg = readl(&xdbc.xdbc_reg->control);
  614. if (reg & CTRL_DBC_RUN_CHANGE) {
  615. writel(reg, &xdbc.xdbc_reg->control);
  616. if (reg & CTRL_DBC_RUN)
  617. xdbc.flags |= XDBC_FLAGS_CONFIGURED;
  618. else
  619. xdbc.flags &= ~XDBC_FLAGS_CONFIGURED;
  620. }
  621. /* Handle endpoint stall event: */
  622. reg = readl(&xdbc.xdbc_reg->control);
  623. if (reg & CTRL_HALT_IN_TR) {
  624. xdbc.flags |= XDBC_FLAGS_IN_STALL;
  625. } else {
  626. xdbc.flags &= ~XDBC_FLAGS_IN_STALL;
  627. if (!(xdbc.flags & XDBC_FLAGS_IN_PROCESS))
  628. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  629. }
  630. if (reg & CTRL_HALT_OUT_TR)
  631. xdbc.flags |= XDBC_FLAGS_OUT_STALL;
  632. else
  633. xdbc.flags &= ~XDBC_FLAGS_OUT_STALL;
  634. /* Handle the events in the event ring: */
  635. evt_trb = xdbc.evt_ring.dequeue;
  636. while ((le32_to_cpu(evt_trb->field[3]) & TRB_CYCLE) == xdbc.evt_ring.cycle_state) {
  637. /*
  638. * Add a barrier between reading the cycle flag and any
  639. * reads of the event's flags/data below:
  640. */
  641. rmb();
  642. switch ((le32_to_cpu(evt_trb->field[3]) & TRB_TYPE_BITMASK)) {
  643. case TRB_TYPE(TRB_PORT_STATUS):
  644. xdbc_handle_port_status(evt_trb);
  645. break;
  646. case TRB_TYPE(TRB_TRANSFER):
  647. xdbc_handle_tx_event(evt_trb);
  648. break;
  649. default:
  650. break;
  651. }
  652. ++(xdbc.evt_ring.dequeue);
  653. if (xdbc.evt_ring.dequeue == &xdbc.evt_seg.trbs[TRBS_PER_SEGMENT]) {
  654. xdbc.evt_ring.dequeue = xdbc.evt_seg.trbs;
  655. xdbc.evt_ring.cycle_state ^= 1;
  656. }
  657. evt_trb = xdbc.evt_ring.dequeue;
  658. update_erdp = true;
  659. }
  660. /* Update event ring dequeue pointer: */
  661. if (update_erdp)
  662. xdbc_write64(__pa(xdbc.evt_ring.dequeue), &xdbc.xdbc_reg->erdp);
  663. }
  664. static int xdbc_bulk_write(const char *bytes, int size)
  665. {
  666. int ret, timeout = 0;
  667. unsigned long flags;
  668. retry:
  669. if (in_nmi()) {
  670. if (!raw_spin_trylock_irqsave(&xdbc.lock, flags))
  671. return -EAGAIN;
  672. } else {
  673. raw_spin_lock_irqsave(&xdbc.lock, flags);
  674. }
  675. xdbc_handle_events();
  676. /* Check completion of the previous request: */
  677. if ((xdbc.flags & XDBC_FLAGS_OUT_PROCESS) && (timeout < 2000000)) {
  678. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  679. udelay(100);
  680. timeout += 100;
  681. goto retry;
  682. }
  683. if (xdbc.flags & XDBC_FLAGS_OUT_PROCESS) {
  684. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  685. xdbc_trace("previous transfer not completed yet\n");
  686. return -ETIMEDOUT;
  687. }
  688. ret = xdbc_bulk_transfer((void *)bytes, size, false);
  689. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  690. return ret;
  691. }
  692. static void early_xdbc_write(struct console *con, const char *str, u32 n)
  693. {
  694. /* static variables are zeroed, so buf is always NULL terminated */
  695. static char buf[XDBC_MAX_PACKET + 1];
  696. int chunk, ret;
  697. int use_cr = 0;
  698. if (!xdbc.xdbc_reg)
  699. return;
  700. while (n > 0) {
  701. for (chunk = 0; chunk < XDBC_MAX_PACKET && n > 0; str++, chunk++, n--) {
  702. if (!use_cr && *str == '\n') {
  703. use_cr = 1;
  704. buf[chunk] = '\r';
  705. str--;
  706. n++;
  707. continue;
  708. }
  709. if (use_cr)
  710. use_cr = 0;
  711. buf[chunk] = *str;
  712. }
  713. if (chunk > 0) {
  714. ret = xdbc_bulk_write(buf, chunk);
  715. if (ret < 0)
  716. xdbc_trace("missed message {%s}\n", buf);
  717. }
  718. }
  719. }
  720. static struct console early_xdbc_console = {
  721. .name = "earlyxdbc",
  722. .write = early_xdbc_write,
  723. .flags = CON_PRINTBUFFER,
  724. .index = -1,
  725. };
  726. void __init early_xdbc_register_console(void)
  727. {
  728. if (early_console)
  729. return;
  730. early_console = &early_xdbc_console;
  731. if (early_console_keep)
  732. early_console->flags &= ~CON_BOOT;
  733. else
  734. early_console->flags |= CON_BOOT;
  735. register_console(early_console);
  736. }
  737. static void xdbc_unregister_console(void)
  738. {
  739. if (console_is_registered(&early_xdbc_console))
  740. unregister_console(&early_xdbc_console);
  741. }
  742. static int xdbc_scrub_function(void *ptr)
  743. {
  744. unsigned long flags;
  745. while (true) {
  746. raw_spin_lock_irqsave(&xdbc.lock, flags);
  747. xdbc_handle_events();
  748. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED)) {
  749. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  750. break;
  751. }
  752. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  753. schedule_timeout_interruptible(1);
  754. }
  755. xdbc_unregister_console();
  756. writel(0, &xdbc.xdbc_reg->control);
  757. xdbc_trace("dbc scrub function exits\n");
  758. return 0;
  759. }
  760. static int __init xdbc_init(void)
  761. {
  762. unsigned long flags;
  763. void __iomem *base;
  764. int ret = 0;
  765. u32 offset;
  766. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED))
  767. return 0;
  768. /*
  769. * It's time to shut down the DbC, so that the debug
  770. * port can be reused by the host controller:
  771. */
  772. if (early_xdbc_console.index == -1 ||
  773. (early_xdbc_console.flags & CON_BOOT)) {
  774. xdbc_trace("hardware not used anymore\n");
  775. goto free_and_quit;
  776. }
  777. base = ioremap(xdbc.xhci_start, xdbc.xhci_length);
  778. if (!base) {
  779. xdbc_trace("failed to remap the io address\n");
  780. ret = -ENOMEM;
  781. goto free_and_quit;
  782. }
  783. raw_spin_lock_irqsave(&xdbc.lock, flags);
  784. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  785. xdbc.xhci_base = base;
  786. offset = xhci_find_next_ext_cap(xdbc.xhci_base, 0, XHCI_EXT_CAPS_DEBUG);
  787. xdbc.xdbc_reg = (struct xdbc_regs __iomem *)(xdbc.xhci_base + offset);
  788. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  789. kthread_run(xdbc_scrub_function, NULL, "%s", "xdbc");
  790. return 0;
  791. free_and_quit:
  792. xdbc_free_ring(&xdbc.evt_ring);
  793. xdbc_free_ring(&xdbc.out_ring);
  794. xdbc_free_ring(&xdbc.in_ring);
  795. memblock_phys_free(xdbc.table_dma, PAGE_SIZE);
  796. memblock_phys_free(xdbc.out_dma, PAGE_SIZE);
  797. writel(0, &xdbc.xdbc_reg->control);
  798. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  799. return ret;
  800. }
  801. subsys_initcall(xdbc_init);