xhci-dbc.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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/bootmem.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/version.h>
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/kthread.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 = alloc_bootmem_pages_nopanic(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. do {
  109. result = readl(ptr);
  110. result &= mask;
  111. if (result == done)
  112. return 0;
  113. udelay(delay);
  114. wait -= delay;
  115. } while (wait > 0);
  116. return -ETIMEDOUT;
  117. }
  118. static void __init xdbc_bios_handoff(void)
  119. {
  120. int offset, timeout;
  121. u32 val;
  122. offset = xhci_find_next_ext_cap(xdbc.xhci_base, 0, XHCI_EXT_CAPS_LEGACY);
  123. val = readl(xdbc.xhci_base + offset);
  124. if (val & XHCI_HC_BIOS_OWNED) {
  125. writel(val | XHCI_HC_OS_OWNED, xdbc.xhci_base + offset);
  126. timeout = handshake(xdbc.xhci_base + offset, XHCI_HC_BIOS_OWNED, 0, 5000, 10);
  127. if (timeout) {
  128. pr_notice("failed to hand over xHCI control from BIOS\n");
  129. writel(val & ~XHCI_HC_BIOS_OWNED, xdbc.xhci_base + offset);
  130. }
  131. }
  132. /* Disable BIOS SMIs and clear all SMI events: */
  133. val = readl(xdbc.xhci_base + offset + XHCI_LEGACY_CONTROL_OFFSET);
  134. val &= XHCI_LEGACY_DISABLE_SMI;
  135. val |= XHCI_LEGACY_SMI_EVENTS;
  136. writel(val, xdbc.xhci_base + offset + XHCI_LEGACY_CONTROL_OFFSET);
  137. }
  138. static int __init
  139. xdbc_alloc_ring(struct xdbc_segment *seg, struct xdbc_ring *ring)
  140. {
  141. seg->trbs = xdbc_get_page(&seg->dma);
  142. if (!seg->trbs)
  143. return -ENOMEM;
  144. ring->segment = seg;
  145. return 0;
  146. }
  147. static void __init xdbc_free_ring(struct xdbc_ring *ring)
  148. {
  149. struct xdbc_segment *seg = ring->segment;
  150. if (!seg)
  151. return;
  152. free_bootmem(seg->dma, PAGE_SIZE);
  153. ring->segment = NULL;
  154. }
  155. static void xdbc_reset_ring(struct xdbc_ring *ring)
  156. {
  157. struct xdbc_segment *seg = ring->segment;
  158. struct xdbc_trb *link_trb;
  159. memset(seg->trbs, 0, PAGE_SIZE);
  160. ring->enqueue = seg->trbs;
  161. ring->dequeue = seg->trbs;
  162. ring->cycle_state = 1;
  163. if (ring != &xdbc.evt_ring) {
  164. link_trb = &seg->trbs[XDBC_TRBS_PER_SEGMENT - 1];
  165. link_trb->field[0] = cpu_to_le32(lower_32_bits(seg->dma));
  166. link_trb->field[1] = cpu_to_le32(upper_32_bits(seg->dma));
  167. link_trb->field[3] = cpu_to_le32(TRB_TYPE(TRB_LINK)) | cpu_to_le32(LINK_TOGGLE);
  168. }
  169. }
  170. static inline void xdbc_put_utf16(u16 *s, const char *c, size_t size)
  171. {
  172. int i;
  173. for (i = 0; i < size; i++)
  174. s[i] = cpu_to_le16(c[i]);
  175. }
  176. static void xdbc_mem_init(void)
  177. {
  178. struct xdbc_ep_context *ep_in, *ep_out;
  179. struct usb_string_descriptor *s_desc;
  180. struct xdbc_erst_entry *entry;
  181. struct xdbc_strings *strings;
  182. struct xdbc_context *ctx;
  183. unsigned int max_burst;
  184. u32 string_length;
  185. int index = 0;
  186. u32 dev_info;
  187. xdbc_reset_ring(&xdbc.evt_ring);
  188. xdbc_reset_ring(&xdbc.in_ring);
  189. xdbc_reset_ring(&xdbc.out_ring);
  190. memset(xdbc.table_base, 0, PAGE_SIZE);
  191. memset(xdbc.out_buf, 0, PAGE_SIZE);
  192. /* Initialize event ring segment table: */
  193. xdbc.erst_size = 16;
  194. xdbc.erst_base = xdbc.table_base + index * XDBC_TABLE_ENTRY_SIZE;
  195. xdbc.erst_dma = xdbc.table_dma + index * XDBC_TABLE_ENTRY_SIZE;
  196. index += XDBC_ERST_ENTRY_NUM;
  197. entry = (struct xdbc_erst_entry *)xdbc.erst_base;
  198. entry->seg_addr = cpu_to_le64(xdbc.evt_seg.dma);
  199. entry->seg_size = cpu_to_le32(XDBC_TRBS_PER_SEGMENT);
  200. entry->__reserved_0 = 0;
  201. /* Initialize ERST registers: */
  202. writel(1, &xdbc.xdbc_reg->ersts);
  203. xdbc_write64(xdbc.erst_dma, &xdbc.xdbc_reg->erstba);
  204. xdbc_write64(xdbc.evt_seg.dma, &xdbc.xdbc_reg->erdp);
  205. /* Debug capability contexts: */
  206. xdbc.dbcc_size = 64 * 3;
  207. xdbc.dbcc_base = xdbc.table_base + index * XDBC_TABLE_ENTRY_SIZE;
  208. xdbc.dbcc_dma = xdbc.table_dma + index * XDBC_TABLE_ENTRY_SIZE;
  209. index += XDBC_DBCC_ENTRY_NUM;
  210. /* Popluate the strings: */
  211. xdbc.string_size = sizeof(struct xdbc_strings);
  212. xdbc.string_base = xdbc.table_base + index * XDBC_TABLE_ENTRY_SIZE;
  213. xdbc.string_dma = xdbc.table_dma + index * XDBC_TABLE_ENTRY_SIZE;
  214. strings = (struct xdbc_strings *)xdbc.string_base;
  215. index += XDBC_STRING_ENTRY_NUM;
  216. /* Serial string: */
  217. s_desc = (struct usb_string_descriptor *)strings->serial;
  218. s_desc->bLength = (strlen(XDBC_STRING_SERIAL) + 1) * 2;
  219. s_desc->bDescriptorType = USB_DT_STRING;
  220. xdbc_put_utf16(s_desc->wData, XDBC_STRING_SERIAL, strlen(XDBC_STRING_SERIAL));
  221. string_length = s_desc->bLength;
  222. string_length <<= 8;
  223. /* Product string: */
  224. s_desc = (struct usb_string_descriptor *)strings->product;
  225. s_desc->bLength = (strlen(XDBC_STRING_PRODUCT) + 1) * 2;
  226. s_desc->bDescriptorType = USB_DT_STRING;
  227. xdbc_put_utf16(s_desc->wData, XDBC_STRING_PRODUCT, strlen(XDBC_STRING_PRODUCT));
  228. string_length += s_desc->bLength;
  229. string_length <<= 8;
  230. /* Manufacture string: */
  231. s_desc = (struct usb_string_descriptor *)strings->manufacturer;
  232. s_desc->bLength = (strlen(XDBC_STRING_MANUFACTURER) + 1) * 2;
  233. s_desc->bDescriptorType = USB_DT_STRING;
  234. xdbc_put_utf16(s_desc->wData, XDBC_STRING_MANUFACTURER, strlen(XDBC_STRING_MANUFACTURER));
  235. string_length += s_desc->bLength;
  236. string_length <<= 8;
  237. /* String0: */
  238. strings->string0[0] = 4;
  239. strings->string0[1] = USB_DT_STRING;
  240. strings->string0[2] = 0x09;
  241. strings->string0[3] = 0x04;
  242. string_length += 4;
  243. /* Populate info Context: */
  244. ctx = (struct xdbc_context *)xdbc.dbcc_base;
  245. ctx->info.string0 = cpu_to_le64(xdbc.string_dma);
  246. ctx->info.manufacturer = cpu_to_le64(xdbc.string_dma + XDBC_MAX_STRING_LENGTH);
  247. ctx->info.product = cpu_to_le64(xdbc.string_dma + XDBC_MAX_STRING_LENGTH * 2);
  248. ctx->info.serial = cpu_to_le64(xdbc.string_dma + XDBC_MAX_STRING_LENGTH * 3);
  249. ctx->info.length = cpu_to_le32(string_length);
  250. /* Populate bulk out endpoint context: */
  251. max_burst = DEBUG_MAX_BURST(readl(&xdbc.xdbc_reg->control));
  252. ep_out = (struct xdbc_ep_context *)&ctx->out;
  253. ep_out->ep_info1 = 0;
  254. ep_out->ep_info2 = cpu_to_le32(EP_TYPE(BULK_OUT_EP) | MAX_PACKET(1024) | MAX_BURST(max_burst));
  255. ep_out->deq = cpu_to_le64(xdbc.out_seg.dma | xdbc.out_ring.cycle_state);
  256. /* Populate bulk in endpoint context: */
  257. ep_in = (struct xdbc_ep_context *)&ctx->in;
  258. ep_in->ep_info1 = 0;
  259. ep_in->ep_info2 = cpu_to_le32(EP_TYPE(BULK_IN_EP) | MAX_PACKET(1024) | MAX_BURST(max_burst));
  260. ep_in->deq = cpu_to_le64(xdbc.in_seg.dma | xdbc.in_ring.cycle_state);
  261. /* Set DbC context and info registers: */
  262. xdbc_write64(xdbc.dbcc_dma, &xdbc.xdbc_reg->dccp);
  263. dev_info = cpu_to_le32((XDBC_VENDOR_ID << 16) | XDBC_PROTOCOL);
  264. writel(dev_info, &xdbc.xdbc_reg->devinfo1);
  265. dev_info = cpu_to_le32((XDBC_DEVICE_REV << 16) | XDBC_PRODUCT_ID);
  266. writel(dev_info, &xdbc.xdbc_reg->devinfo2);
  267. xdbc.in_buf = xdbc.out_buf + XDBC_MAX_PACKET;
  268. xdbc.in_dma = xdbc.out_dma + XDBC_MAX_PACKET;
  269. }
  270. static void xdbc_do_reset_debug_port(u32 id, u32 count)
  271. {
  272. void __iomem *ops_reg;
  273. void __iomem *portsc;
  274. u32 val, cap_length;
  275. int i;
  276. cap_length = readl(xdbc.xhci_base) & 0xff;
  277. ops_reg = xdbc.xhci_base + cap_length;
  278. id--;
  279. for (i = id; i < (id + count); i++) {
  280. portsc = ops_reg + 0x400 + i * 0x10;
  281. val = readl(portsc);
  282. if (!(val & PORT_CONNECT))
  283. writel(val | PORT_RESET, portsc);
  284. }
  285. }
  286. static void xdbc_reset_debug_port(void)
  287. {
  288. u32 val, port_offset, port_count;
  289. int offset = 0;
  290. do {
  291. offset = xhci_find_next_ext_cap(xdbc.xhci_base, offset, XHCI_EXT_CAPS_PROTOCOL);
  292. if (!offset)
  293. break;
  294. val = readl(xdbc.xhci_base + offset);
  295. if (XHCI_EXT_PORT_MAJOR(val) != 0x3)
  296. continue;
  297. val = readl(xdbc.xhci_base + offset + 8);
  298. port_offset = XHCI_EXT_PORT_OFF(val);
  299. port_count = XHCI_EXT_PORT_COUNT(val);
  300. xdbc_do_reset_debug_port(port_offset, port_count);
  301. } while (1);
  302. }
  303. static void
  304. xdbc_queue_trb(struct xdbc_ring *ring, u32 field1, u32 field2, u32 field3, u32 field4)
  305. {
  306. struct xdbc_trb *trb, *link_trb;
  307. trb = ring->enqueue;
  308. trb->field[0] = cpu_to_le32(field1);
  309. trb->field[1] = cpu_to_le32(field2);
  310. trb->field[2] = cpu_to_le32(field3);
  311. trb->field[3] = cpu_to_le32(field4);
  312. ++(ring->enqueue);
  313. if (ring->enqueue >= &ring->segment->trbs[TRBS_PER_SEGMENT - 1]) {
  314. link_trb = ring->enqueue;
  315. if (ring->cycle_state)
  316. link_trb->field[3] |= cpu_to_le32(TRB_CYCLE);
  317. else
  318. link_trb->field[3] &= cpu_to_le32(~TRB_CYCLE);
  319. ring->enqueue = ring->segment->trbs;
  320. ring->cycle_state ^= 1;
  321. }
  322. }
  323. static void xdbc_ring_doorbell(int target)
  324. {
  325. writel(DOOR_BELL_TARGET(target), &xdbc.xdbc_reg->doorbell);
  326. }
  327. static int xdbc_start(void)
  328. {
  329. u32 ctrl, status;
  330. int ret;
  331. ctrl = readl(&xdbc.xdbc_reg->control);
  332. writel(ctrl | CTRL_DBC_ENABLE | CTRL_PORT_ENABLE, &xdbc.xdbc_reg->control);
  333. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_ENABLE, CTRL_DBC_ENABLE, 100000, 100);
  334. if (ret) {
  335. xdbc_trace("failed to initialize hardware\n");
  336. return ret;
  337. }
  338. /* Reset port to avoid bus hang: */
  339. if (xdbc.vendor == PCI_VENDOR_ID_INTEL)
  340. xdbc_reset_debug_port();
  341. /* Wait for port connection: */
  342. ret = handshake(&xdbc.xdbc_reg->portsc, PORTSC_CONN_STATUS, PORTSC_CONN_STATUS, 5000000, 100);
  343. if (ret) {
  344. xdbc_trace("waiting for connection timed out\n");
  345. return ret;
  346. }
  347. /* Wait for debug device to be configured: */
  348. ret = handshake(&xdbc.xdbc_reg->control, CTRL_DBC_RUN, CTRL_DBC_RUN, 5000000, 100);
  349. if (ret) {
  350. xdbc_trace("waiting for device configuration timed out\n");
  351. return ret;
  352. }
  353. /* Check port number: */
  354. status = readl(&xdbc.xdbc_reg->status);
  355. if (!DCST_DEBUG_PORT(status)) {
  356. xdbc_trace("invalid root hub port number\n");
  357. return -ENODEV;
  358. }
  359. xdbc.port_number = DCST_DEBUG_PORT(status);
  360. xdbc_trace("DbC is running now, control 0x%08x port ID %d\n",
  361. readl(&xdbc.xdbc_reg->control), xdbc.port_number);
  362. return 0;
  363. }
  364. static int xdbc_bulk_transfer(void *data, int size, bool read)
  365. {
  366. struct xdbc_ring *ring;
  367. struct xdbc_trb *trb;
  368. u32 length, control;
  369. u32 cycle;
  370. u64 addr;
  371. if (size > XDBC_MAX_PACKET) {
  372. xdbc_trace("bad parameter, size %d\n", size);
  373. return -EINVAL;
  374. }
  375. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED) ||
  376. !(xdbc.flags & XDBC_FLAGS_CONFIGURED) ||
  377. (!read && (xdbc.flags & XDBC_FLAGS_OUT_STALL)) ||
  378. (read && (xdbc.flags & XDBC_FLAGS_IN_STALL))) {
  379. xdbc_trace("connection not ready, flags %08x\n", xdbc.flags);
  380. return -EIO;
  381. }
  382. ring = (read ? &xdbc.in_ring : &xdbc.out_ring);
  383. trb = ring->enqueue;
  384. cycle = ring->cycle_state;
  385. length = TRB_LEN(size);
  386. control = TRB_TYPE(TRB_NORMAL) | TRB_IOC;
  387. if (cycle)
  388. control &= cpu_to_le32(~TRB_CYCLE);
  389. else
  390. control |= cpu_to_le32(TRB_CYCLE);
  391. if (read) {
  392. memset(xdbc.in_buf, 0, XDBC_MAX_PACKET);
  393. addr = xdbc.in_dma;
  394. xdbc.flags |= XDBC_FLAGS_IN_PROCESS;
  395. } else {
  396. memset(xdbc.out_buf, 0, XDBC_MAX_PACKET);
  397. memcpy(xdbc.out_buf, data, size);
  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. mmiowb();
  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. mmiowb();
  463. ret = xdbc_start();
  464. if (ret < 0) {
  465. writel(0, &xdbc.xdbc_reg->control);
  466. return ret;
  467. }
  468. xdbc.flags |= XDBC_FLAGS_INITIALIZED | XDBC_FLAGS_CONFIGURED;
  469. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  470. return 0;
  471. }
  472. int __init early_xdbc_parse_parameter(char *s)
  473. {
  474. unsigned long dbgp_num = 0;
  475. u32 bus, dev, func, offset;
  476. int ret;
  477. if (!early_pci_allowed())
  478. return -EPERM;
  479. if (strstr(s, "keep"))
  480. early_console_keep = true;
  481. if (xdbc.xdbc_reg)
  482. return 0;
  483. if (*s && kstrtoul(s, 0, &dbgp_num))
  484. dbgp_num = 0;
  485. pr_notice("dbgp_num: %lu\n", dbgp_num);
  486. /* Locate the host controller: */
  487. ret = xdbc_find_dbgp(dbgp_num, &bus, &dev, &func);
  488. if (ret) {
  489. pr_notice("failed to locate xhci host\n");
  490. return -ENODEV;
  491. }
  492. xdbc.vendor = read_pci_config_16(bus, dev, func, PCI_VENDOR_ID);
  493. xdbc.device = read_pci_config_16(bus, dev, func, PCI_DEVICE_ID);
  494. xdbc.bus = bus;
  495. xdbc.dev = dev;
  496. xdbc.func = func;
  497. /* Map the IO memory: */
  498. xdbc.xhci_base = xdbc_map_pci_mmio(bus, dev, func);
  499. if (!xdbc.xhci_base)
  500. return -EINVAL;
  501. /* Locate DbC registers: */
  502. offset = xhci_find_next_ext_cap(xdbc.xhci_base, 0, XHCI_EXT_CAPS_DEBUG);
  503. if (!offset) {
  504. pr_notice("xhci host doesn't support debug capability\n");
  505. early_iounmap(xdbc.xhci_base, xdbc.xhci_length);
  506. xdbc.xhci_base = NULL;
  507. xdbc.xhci_length = 0;
  508. return -ENODEV;
  509. }
  510. xdbc.xdbc_reg = (struct xdbc_regs __iomem *)(xdbc.xhci_base + offset);
  511. return 0;
  512. }
  513. int __init early_xdbc_setup_hardware(void)
  514. {
  515. int ret;
  516. if (!xdbc.xdbc_reg)
  517. return -ENODEV;
  518. xdbc_bios_handoff();
  519. raw_spin_lock_init(&xdbc.lock);
  520. ret = xdbc_early_setup();
  521. if (ret) {
  522. pr_notice("failed to setup the connection to host\n");
  523. xdbc_free_ring(&xdbc.evt_ring);
  524. xdbc_free_ring(&xdbc.out_ring);
  525. xdbc_free_ring(&xdbc.in_ring);
  526. if (xdbc.table_dma)
  527. free_bootmem(xdbc.table_dma, PAGE_SIZE);
  528. if (xdbc.out_dma)
  529. free_bootmem(xdbc.out_dma, PAGE_SIZE);
  530. xdbc.table_base = NULL;
  531. xdbc.out_buf = NULL;
  532. }
  533. return ret;
  534. }
  535. static void xdbc_handle_port_status(struct xdbc_trb *evt_trb)
  536. {
  537. u32 port_reg;
  538. port_reg = readl(&xdbc.xdbc_reg->portsc);
  539. if (port_reg & PORTSC_CONN_CHANGE) {
  540. xdbc_trace("connect status change event\n");
  541. /* Check whether cable unplugged: */
  542. if (!(port_reg & PORTSC_CONN_STATUS)) {
  543. xdbc.flags = 0;
  544. xdbc_trace("cable unplugged\n");
  545. }
  546. }
  547. if (port_reg & PORTSC_RESET_CHANGE)
  548. xdbc_trace("port reset change event\n");
  549. if (port_reg & PORTSC_LINK_CHANGE)
  550. xdbc_trace("port link status change event\n");
  551. if (port_reg & PORTSC_CONFIG_CHANGE)
  552. xdbc_trace("config error change\n");
  553. /* Write back the value to clear RW1C bits: */
  554. writel(port_reg, &xdbc.xdbc_reg->portsc);
  555. }
  556. static void xdbc_handle_tx_event(struct xdbc_trb *evt_trb)
  557. {
  558. size_t remain_length;
  559. u32 comp_code;
  560. int ep_id;
  561. comp_code = GET_COMP_CODE(le32_to_cpu(evt_trb->field[2]));
  562. remain_length = EVENT_TRB_LEN(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. remain_length = 0;
  567. case COMP_SHORT_PACKET:
  568. break;
  569. case COMP_TRB_ERROR:
  570. case COMP_BABBLE_DETECTED_ERROR:
  571. case COMP_USB_TRANSACTION_ERROR:
  572. case COMP_STALL_ERROR:
  573. default:
  574. if (ep_id == XDBC_EPID_OUT || ep_id == XDBC_EPID_OUT_INTEL)
  575. xdbc.flags |= XDBC_FLAGS_OUT_STALL;
  576. if (ep_id == XDBC_EPID_IN || ep_id == XDBC_EPID_IN_INTEL)
  577. xdbc.flags |= XDBC_FLAGS_IN_STALL;
  578. xdbc_trace("endpoint %d stalled\n", ep_id);
  579. break;
  580. }
  581. if (ep_id == XDBC_EPID_IN || ep_id == XDBC_EPID_IN_INTEL) {
  582. xdbc.flags &= ~XDBC_FLAGS_IN_PROCESS;
  583. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  584. } else if (ep_id == XDBC_EPID_OUT || ep_id == XDBC_EPID_OUT_INTEL) {
  585. xdbc.flags &= ~XDBC_FLAGS_OUT_PROCESS;
  586. } else {
  587. xdbc_trace("invalid endpoint id %d\n", ep_id);
  588. }
  589. }
  590. static void xdbc_handle_events(void)
  591. {
  592. struct xdbc_trb *evt_trb;
  593. bool update_erdp = false;
  594. u32 reg;
  595. u8 cmd;
  596. cmd = read_pci_config_byte(xdbc.bus, xdbc.dev, xdbc.func, PCI_COMMAND);
  597. if (!(cmd & PCI_COMMAND_MASTER)) {
  598. cmd |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
  599. write_pci_config_byte(xdbc.bus, xdbc.dev, xdbc.func, PCI_COMMAND, cmd);
  600. }
  601. if (!(xdbc.flags & XDBC_FLAGS_INITIALIZED))
  602. return;
  603. /* Handle external reset events: */
  604. reg = readl(&xdbc.xdbc_reg->control);
  605. if (!(reg & CTRL_DBC_ENABLE)) {
  606. if (xdbc_handle_external_reset()) {
  607. xdbc_trace("failed to recover connection\n");
  608. return;
  609. }
  610. }
  611. /* Handle configure-exit event: */
  612. reg = readl(&xdbc.xdbc_reg->control);
  613. if (reg & CTRL_DBC_RUN_CHANGE) {
  614. writel(reg, &xdbc.xdbc_reg->control);
  615. if (reg & CTRL_DBC_RUN)
  616. xdbc.flags |= XDBC_FLAGS_CONFIGURED;
  617. else
  618. xdbc.flags &= ~XDBC_FLAGS_CONFIGURED;
  619. }
  620. /* Handle endpoint stall event: */
  621. reg = readl(&xdbc.xdbc_reg->control);
  622. if (reg & CTRL_HALT_IN_TR) {
  623. xdbc.flags |= XDBC_FLAGS_IN_STALL;
  624. } else {
  625. xdbc.flags &= ~XDBC_FLAGS_IN_STALL;
  626. if (!(xdbc.flags & XDBC_FLAGS_IN_PROCESS))
  627. xdbc_bulk_transfer(NULL, XDBC_MAX_PACKET, true);
  628. }
  629. if (reg & CTRL_HALT_OUT_TR)
  630. xdbc.flags |= XDBC_FLAGS_OUT_STALL;
  631. else
  632. xdbc.flags &= ~XDBC_FLAGS_OUT_STALL;
  633. /* Handle the events in the event ring: */
  634. evt_trb = xdbc.evt_ring.dequeue;
  635. while ((le32_to_cpu(evt_trb->field[3]) & TRB_CYCLE) == xdbc.evt_ring.cycle_state) {
  636. /*
  637. * Add a barrier between reading the cycle flag and any
  638. * reads of the event's flags/data below:
  639. */
  640. rmb();
  641. switch ((le32_to_cpu(evt_trb->field[3]) & TRB_TYPE_BITMASK)) {
  642. case TRB_TYPE(TRB_PORT_STATUS):
  643. xdbc_handle_port_status(evt_trb);
  644. break;
  645. case TRB_TYPE(TRB_TRANSFER):
  646. xdbc_handle_tx_event(evt_trb);
  647. break;
  648. default:
  649. break;
  650. }
  651. ++(xdbc.evt_ring.dequeue);
  652. if (xdbc.evt_ring.dequeue == &xdbc.evt_seg.trbs[TRBS_PER_SEGMENT]) {
  653. xdbc.evt_ring.dequeue = xdbc.evt_seg.trbs;
  654. xdbc.evt_ring.cycle_state ^= 1;
  655. }
  656. evt_trb = xdbc.evt_ring.dequeue;
  657. update_erdp = true;
  658. }
  659. /* Update event ring dequeue pointer: */
  660. if (update_erdp)
  661. xdbc_write64(__pa(xdbc.evt_ring.dequeue), &xdbc.xdbc_reg->erdp);
  662. }
  663. static int xdbc_bulk_write(const char *bytes, int size)
  664. {
  665. int ret, timeout = 0;
  666. unsigned long flags;
  667. retry:
  668. if (in_nmi()) {
  669. if (!raw_spin_trylock_irqsave(&xdbc.lock, flags))
  670. return -EAGAIN;
  671. } else {
  672. raw_spin_lock_irqsave(&xdbc.lock, flags);
  673. }
  674. xdbc_handle_events();
  675. /* Check completion of the previous request: */
  676. if ((xdbc.flags & XDBC_FLAGS_OUT_PROCESS) && (timeout < 2000000)) {
  677. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  678. udelay(100);
  679. timeout += 100;
  680. goto retry;
  681. }
  682. if (xdbc.flags & XDBC_FLAGS_OUT_PROCESS) {
  683. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  684. xdbc_trace("previous transfer not completed yet\n");
  685. return -ETIMEDOUT;
  686. }
  687. ret = xdbc_bulk_transfer((void *)bytes, size, false);
  688. raw_spin_unlock_irqrestore(&xdbc.lock, flags);
  689. return ret;
  690. }
  691. static void early_xdbc_write(struct console *con, const char *str, u32 n)
  692. {
  693. static char buf[XDBC_MAX_PACKET];
  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_nocache(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. free_bootmem(xdbc.table_dma, PAGE_SIZE);
  795. free_bootmem(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);