xhci-dbc.c 24 KB

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