xhci-mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * xHCI host controller driver
  3. *
  4. * Copyright (C) 2008 Intel Corp.
  5. *
  6. * Author: Sarah Sharp
  7. * Some code borrowed from the Linux EHCI driver.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  16. * for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/usb.h>
  23. #include <linux/pci.h>
  24. #include "xhci.h"
  25. /*
  26. * Allocates a generic ring segment from the ring pool, sets the dma address,
  27. * initializes the segment to zero, and sets the private next pointer to NULL.
  28. *
  29. * Section 4.11.1.1:
  30. * "All components of all Command and Transfer TRBs shall be initialized to '0'"
  31. */
  32. static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci, gfp_t flags)
  33. {
  34. struct xhci_segment *seg;
  35. dma_addr_t dma;
  36. seg = kzalloc(sizeof *seg, flags);
  37. if (!seg)
  38. return 0;
  39. xhci_dbg(xhci, "Allocating priv segment structure at 0x%x\n",
  40. (unsigned int) seg);
  41. seg->trbs = dma_pool_alloc(xhci->segment_pool, flags, &dma);
  42. if (!seg->trbs) {
  43. kfree(seg);
  44. return 0;
  45. }
  46. xhci_dbg(xhci, "// Allocating segment at 0x%x (virtual) 0x%x (DMA)\n",
  47. (unsigned int) seg->trbs, (u32) dma);
  48. memset(seg->trbs, 0, SEGMENT_SIZE);
  49. seg->dma = dma;
  50. seg->next = NULL;
  51. return seg;
  52. }
  53. static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
  54. {
  55. if (!seg)
  56. return;
  57. if (seg->trbs) {
  58. xhci_dbg(xhci, "Freeing DMA segment at 0x%x"
  59. " (virtual) 0x%x (DMA)\n",
  60. (unsigned int) seg->trbs, (u32) seg->dma);
  61. dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
  62. seg->trbs = NULL;
  63. }
  64. xhci_dbg(xhci, "Freeing priv segment structure at 0x%x\n",
  65. (unsigned int) seg);
  66. kfree(seg);
  67. }
  68. /*
  69. * Make the prev segment point to the next segment.
  70. *
  71. * Change the last TRB in the prev segment to be a Link TRB which points to the
  72. * DMA address of the next segment. The caller needs to set any Link TRB
  73. * related flags, such as End TRB, Toggle Cycle, and no snoop.
  74. */
  75. static void xhci_link_segments(struct xhci_hcd *xhci, struct xhci_segment *prev,
  76. struct xhci_segment *next, bool link_trbs)
  77. {
  78. u32 val;
  79. if (!prev || !next)
  80. return;
  81. prev->next = next;
  82. if (link_trbs) {
  83. prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr[0] = next->dma;
  84. /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
  85. val = prev->trbs[TRBS_PER_SEGMENT-1].link.control;
  86. val &= ~TRB_TYPE_BITMASK;
  87. val |= TRB_TYPE(TRB_LINK);
  88. prev->trbs[TRBS_PER_SEGMENT-1].link.control = val;
  89. }
  90. xhci_dbg(xhci, "Linking segment 0x%x to segment 0x%x (DMA)\n",
  91. prev->dma, next->dma);
  92. }
  93. /* XXX: Do we need the hcd structure in all these functions? */
  94. static void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
  95. {
  96. struct xhci_segment *seg;
  97. struct xhci_segment *first_seg;
  98. if (!ring || !ring->first_seg)
  99. return;
  100. first_seg = ring->first_seg;
  101. seg = first_seg->next;
  102. xhci_dbg(xhci, "Freeing ring at 0x%x\n", (unsigned int) ring);
  103. while (seg != first_seg) {
  104. struct xhci_segment *next = seg->next;
  105. xhci_segment_free(xhci, seg);
  106. seg = next;
  107. }
  108. xhci_segment_free(xhci, first_seg);
  109. ring->first_seg = NULL;
  110. kfree(ring);
  111. }
  112. /**
  113. * Create a new ring with zero or more segments.
  114. *
  115. * Link each segment together into a ring.
  116. * Set the end flag and the cycle toggle bit on the last segment.
  117. * See section 4.9.1 and figures 15 and 16.
  118. */
  119. static struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
  120. unsigned int num_segs, bool link_trbs, gfp_t flags)
  121. {
  122. struct xhci_ring *ring;
  123. struct xhci_segment *prev;
  124. ring = kzalloc(sizeof *(ring), flags);
  125. xhci_dbg(xhci, "Allocating ring at 0x%x\n", (unsigned int) ring);
  126. if (!ring)
  127. return 0;
  128. if (num_segs == 0)
  129. return ring;
  130. ring->first_seg = xhci_segment_alloc(xhci, flags);
  131. if (!ring->first_seg)
  132. goto fail;
  133. num_segs--;
  134. prev = ring->first_seg;
  135. while (num_segs > 0) {
  136. struct xhci_segment *next;
  137. next = xhci_segment_alloc(xhci, flags);
  138. if (!next)
  139. goto fail;
  140. xhci_link_segments(xhci, prev, next, link_trbs);
  141. prev = next;
  142. num_segs--;
  143. }
  144. xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
  145. if (link_trbs) {
  146. /* See section 4.9.2.1 and 6.4.4.1 */
  147. prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
  148. xhci_dbg(xhci, "Wrote link toggle flag to"
  149. " segment 0x%x (virtual), 0x%x (DMA)\n",
  150. (unsigned int) prev, (u32) prev->dma);
  151. }
  152. /* The ring is empty, so the enqueue pointer == dequeue pointer */
  153. ring->enqueue = ring->first_seg->trbs;
  154. ring->enq_seg = ring->first_seg;
  155. ring->dequeue = ring->enqueue;
  156. ring->deq_seg = ring->first_seg;
  157. /* The ring is initialized to 0. The producer must write 1 to the cycle
  158. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  159. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  160. */
  161. ring->cycle_state = 1;
  162. return ring;
  163. fail:
  164. xhci_ring_free(xhci, ring);
  165. return 0;
  166. }
  167. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  168. {
  169. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  170. int size;
  171. /* XXX: Free all the segments in the various rings */
  172. /* Free the Event Ring Segment Table and the actual Event Ring */
  173. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  174. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
  175. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[0]);
  176. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
  177. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[0]);
  178. size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
  179. if (xhci->erst.entries)
  180. pci_free_consistent(pdev, size,
  181. xhci->erst.entries, xhci->erst.erst_dma_addr);
  182. xhci->erst.entries = NULL;
  183. xhci_dbg(xhci, "Freed ERST\n");
  184. if (xhci->event_ring)
  185. xhci_ring_free(xhci, xhci->event_ring);
  186. xhci->event_ring = NULL;
  187. xhci_dbg(xhci, "Freed event ring\n");
  188. xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[1]);
  189. xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[0]);
  190. if (xhci->cmd_ring)
  191. xhci_ring_free(xhci, xhci->cmd_ring);
  192. xhci->cmd_ring = NULL;
  193. xhci_dbg(xhci, "Freed command ring\n");
  194. if (xhci->segment_pool)
  195. dma_pool_destroy(xhci->segment_pool);
  196. xhci->segment_pool = NULL;
  197. xhci_dbg(xhci, "Freed segment pool\n");
  198. xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[1]);
  199. xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[0]);
  200. if (xhci->dcbaa)
  201. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  202. xhci->dcbaa, xhci->dcbaa->dma);
  203. xhci->dcbaa = NULL;
  204. xhci->page_size = 0;
  205. xhci->page_shift = 0;
  206. }
  207. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  208. {
  209. dma_addr_t dma;
  210. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  211. unsigned int val, val2;
  212. struct xhci_segment *seg;
  213. u32 page_size;
  214. int i;
  215. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  216. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  217. for (i = 0; i < 16; i++) {
  218. if ((0x1 & page_size) != 0)
  219. break;
  220. page_size = page_size >> 1;
  221. }
  222. if (i < 16)
  223. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  224. else
  225. xhci_warn(xhci, "WARN: no supported page size\n");
  226. /* Use 4K pages, since that's common and the minimum the HC supports */
  227. xhci->page_shift = 12;
  228. xhci->page_size = 1 << xhci->page_shift;
  229. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  230. /*
  231. * Program the Number of Device Slots Enabled field in the CONFIG
  232. * register with the max value of slots the HC can handle.
  233. */
  234. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  235. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  236. (unsigned int) val);
  237. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  238. val |= (val2 & ~HCS_SLOTS_MASK);
  239. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  240. (unsigned int) val);
  241. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  242. /*
  243. * Section 5.4.8 - doorbell array must be
  244. * "physically contiguous and 64-byte (cache line) aligned".
  245. */
  246. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  247. sizeof(*xhci->dcbaa), &dma);
  248. if (!xhci->dcbaa)
  249. goto fail;
  250. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  251. xhci->dcbaa->dma = dma;
  252. xhci_dbg(xhci, "// Setting device context base array address to 0x%x\n",
  253. xhci->dcbaa->dma);
  254. xhci_writel(xhci, (u32) 0, &xhci->op_regs->dcbaa_ptr[1]);
  255. xhci_writel(xhci, dma, &xhci->op_regs->dcbaa_ptr[0]);
  256. /*
  257. * Initialize the ring segment pool. The ring must be a contiguous
  258. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  259. * however, the command ring segment needs 64-byte aligned segments,
  260. * so we pick the greater alignment need.
  261. */
  262. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  263. SEGMENT_SIZE, 64, xhci->page_size);
  264. if (!xhci->segment_pool)
  265. goto fail;
  266. /* Set up the command ring to have one segments for now. */
  267. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  268. if (!xhci->cmd_ring)
  269. goto fail;
  270. xhci_dbg(xhci, "Allocated command ring at 0x%x\n", (unsigned int) xhci->cmd_ring);
  271. xhci_dbg(xhci, "First segment DMA is 0x%x\n", (unsigned int) xhci->cmd_ring->first_seg->dma);
  272. /* Set the address in the Command Ring Control register */
  273. val = xhci_readl(xhci, &xhci->op_regs->cmd_ring[0]);
  274. val = (val & ~CMD_RING_ADDR_MASK) |
  275. (xhci->cmd_ring->first_seg->dma & CMD_RING_ADDR_MASK) |
  276. xhci->cmd_ring->cycle_state;
  277. xhci_dbg(xhci, "// Setting command ring address high bits to 0x0\n");
  278. xhci_writel(xhci, (u32) 0, &xhci->op_regs->cmd_ring[1]);
  279. xhci_dbg(xhci, "// Setting command ring address low bits to 0x%x\n", val);
  280. xhci_writel(xhci, val, &xhci->op_regs->cmd_ring[0]);
  281. xhci_dbg_cmd_ptrs(xhci);
  282. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  283. val &= DBOFF_MASK;
  284. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  285. " from cap regs base addr\n", val);
  286. xhci->dba = (void *) xhci->cap_regs + val;
  287. xhci_dbg_regs(xhci);
  288. xhci_print_run_regs(xhci);
  289. /* Set ir_set to interrupt register set 0 */
  290. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  291. /*
  292. * Event ring setup: Allocate a normal ring, but also setup
  293. * the event ring segment table (ERST). Section 4.9.3.
  294. */
  295. xhci_dbg(xhci, "// Allocating event ring\n");
  296. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  297. if (!xhci->event_ring)
  298. goto fail;
  299. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  300. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  301. if (!xhci->erst.entries)
  302. goto fail;
  303. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%x\n", dma);
  304. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  305. xhci->erst.num_entries = ERST_NUM_SEGS;
  306. xhci->erst.erst_dma_addr = dma;
  307. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = 0x%x, dma addr = 0x%x\n",
  308. xhci->erst.num_entries,
  309. (unsigned int) xhci->erst.entries,
  310. xhci->erst.erst_dma_addr);
  311. /* set ring base address and size for each segment table entry */
  312. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  313. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  314. entry->seg_addr[1] = 0;
  315. entry->seg_addr[0] = seg->dma;
  316. entry->seg_size = TRBS_PER_SEGMENT;
  317. entry->rsvd = 0;
  318. seg = seg->next;
  319. }
  320. /* set ERST count with the number of entries in the segment table */
  321. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  322. val &= ERST_SIZE_MASK;
  323. val |= ERST_NUM_SEGS;
  324. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  325. val);
  326. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  327. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  328. /* set the segment table base address */
  329. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%x\n",
  330. xhci->erst.erst_dma_addr);
  331. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
  332. val = xhci_readl(xhci, &xhci->ir_set->erst_base[0]);
  333. val &= ERST_PTR_MASK;
  334. val |= (xhci->erst.erst_dma_addr & ~ERST_PTR_MASK);
  335. xhci_writel(xhci, val, &xhci->ir_set->erst_base[0]);
  336. /* Set the event ring dequeue address */
  337. set_hc_event_deq(xhci);
  338. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  339. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  340. /*
  341. * XXX: Might need to set the Interrupter Moderation Register to
  342. * something other than the default (~1ms minimum between interrupts).
  343. * See section 5.5.1.2.
  344. */
  345. return 0;
  346. fail:
  347. xhci_warn(xhci, "Couldn't initialize memory\n");
  348. xhci_mem_cleanup(xhci);
  349. return -ENOMEM;
  350. }