xhci-dbc.c 24 KB

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