xhci-mem.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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->dequeue = ring->enqueue;
  155. /* The ring is initialized to 0. The producer must write 1 to the cycle
  156. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  157. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  158. */
  159. ring->cycle_state = 1;
  160. return ring;
  161. fail:
  162. xhci_ring_free(xhci, ring);
  163. return 0;
  164. }
  165. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  166. {
  167. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  168. int size;
  169. /* XXX: Free all the segments in the various rings */
  170. /* Free the Event Ring Segment Table and the actual Event Ring */
  171. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  172. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
  173. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[0]);
  174. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
  175. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[0]);
  176. size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
  177. if (xhci->erst.entries)
  178. pci_free_consistent(pdev, size,
  179. xhci->erst.entries, xhci->erst.erst_dma_addr);
  180. xhci->erst.entries = NULL;
  181. xhci_dbg(xhci, "Freed ERST\n");
  182. if (xhci->event_ring)
  183. xhci_ring_free(xhci, xhci->event_ring);
  184. xhci->event_ring = NULL;
  185. xhci_dbg(xhci, "Freed event ring\n");
  186. xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[1]);
  187. xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[0]);
  188. if (xhci->cmd_ring)
  189. xhci_ring_free(xhci, xhci->cmd_ring);
  190. xhci->cmd_ring = NULL;
  191. xhci_dbg(xhci, "Freed command ring\n");
  192. if (xhci->segment_pool)
  193. dma_pool_destroy(xhci->segment_pool);
  194. xhci->segment_pool = NULL;
  195. xhci_dbg(xhci, "Freed segment pool\n");
  196. xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[1]);
  197. xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[0]);
  198. if (xhci->dcbaa)
  199. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  200. xhci->dcbaa, xhci->dcbaa->dma);
  201. xhci->dcbaa = NULL;
  202. xhci->page_size = 0;
  203. xhci->page_shift = 0;
  204. }
  205. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  206. {
  207. dma_addr_t dma;
  208. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  209. unsigned int val, val2;
  210. struct xhci_segment *seg;
  211. u32 page_size;
  212. int i;
  213. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  214. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  215. for (i = 0; i < 16; i++) {
  216. if ((0x1 & page_size) != 0)
  217. break;
  218. page_size = page_size >> 1;
  219. }
  220. if (i < 16)
  221. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  222. else
  223. xhci_warn(xhci, "WARN: no supported page size\n");
  224. /* Use 4K pages, since that's common and the minimum the HC supports */
  225. xhci->page_shift = 12;
  226. xhci->page_size = 1 << xhci->page_shift;
  227. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  228. /*
  229. * Program the Number of Device Slots Enabled field in the CONFIG
  230. * register with the max value of slots the HC can handle.
  231. */
  232. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  233. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  234. (unsigned int) val);
  235. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  236. val |= (val2 & ~HCS_SLOTS_MASK);
  237. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  238. (unsigned int) val);
  239. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  240. /*
  241. * Section 5.4.8 - doorbell array must be
  242. * "physically contiguous and 64-byte (cache line) aligned".
  243. */
  244. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  245. sizeof(*xhci->dcbaa), &dma);
  246. if (!xhci->dcbaa)
  247. goto fail;
  248. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  249. xhci->dcbaa->dma = dma;
  250. xhci_dbg(xhci, "// Setting device context base array address to 0x%x\n",
  251. xhci->dcbaa->dma);
  252. xhci_writel(xhci, (u32) 0, &xhci->op_regs->dcbaa_ptr[1]);
  253. xhci_writel(xhci, dma, &xhci->op_regs->dcbaa_ptr[0]);
  254. /*
  255. * Initialize the ring segment pool. The ring must be a contiguous
  256. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  257. * however, the command ring segment needs 64-byte aligned segments,
  258. * so we pick the greater alignment need.
  259. */
  260. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  261. SEGMENT_SIZE, 64, xhci->page_size);
  262. if (!xhci->segment_pool)
  263. goto fail;
  264. /* Set up the command ring to have one segments for now. */
  265. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  266. if (!xhci->cmd_ring)
  267. goto fail;
  268. xhci_dbg(xhci, "Allocated command ring at 0x%x\n", (unsigned int) xhci->cmd_ring);
  269. xhci_dbg(xhci, "First segment DMA is 0x%x\n", (unsigned int) xhci->cmd_ring->first_seg->dma);
  270. /* Set the address in the Command Ring Control register */
  271. val = xhci_readl(xhci, &xhci->op_regs->cmd_ring[0]);
  272. val = (val & ~CMD_RING_ADDR_MASK) |
  273. (xhci->cmd_ring->first_seg->dma & CMD_RING_ADDR_MASK) |
  274. xhci->cmd_ring->cycle_state;
  275. xhci_dbg(xhci, "// Setting command ring address high bits to 0x0\n");
  276. xhci_writel(xhci, (u32) 0, &xhci->op_regs->cmd_ring[1]);
  277. xhci_dbg(xhci, "// Setting command ring address low bits to 0x%x\n", val);
  278. xhci_writel(xhci, val, &xhci->op_regs->cmd_ring[0]);
  279. xhci_dbg_cmd_ptrs(xhci);
  280. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  281. val &= DBOFF_MASK;
  282. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  283. " from cap regs base addr\n", val);
  284. xhci->dba = (void *) xhci->cap_regs + val;
  285. xhci_dbg_regs(xhci);
  286. xhci_print_run_regs(xhci);
  287. /* Set ir_set to interrupt register set 0 */
  288. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  289. /*
  290. * Event ring setup: Allocate a normal ring, but also setup
  291. * the event ring segment table (ERST). Section 4.9.3.
  292. */
  293. xhci_dbg(xhci, "// Allocating event ring\n");
  294. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  295. if (!xhci->event_ring)
  296. goto fail;
  297. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  298. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  299. if (!xhci->erst.entries)
  300. goto fail;
  301. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%x\n", dma);
  302. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  303. xhci->erst.num_entries = ERST_NUM_SEGS;
  304. xhci->erst.erst_dma_addr = dma;
  305. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = 0x%x, dma addr = 0x%x\n",
  306. xhci->erst.num_entries,
  307. (unsigned int) xhci->erst.entries,
  308. xhci->erst.erst_dma_addr);
  309. /* set ring base address and size for each segment table entry */
  310. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  311. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  312. entry->seg_addr[1] = 0;
  313. entry->seg_addr[0] = seg->dma;
  314. entry->seg_size = TRBS_PER_SEGMENT;
  315. entry->rsvd = 0;
  316. seg = seg->next;
  317. }
  318. /* set ERST count with the number of entries in the segment table */
  319. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  320. val &= ERST_SIZE_MASK;
  321. val |= ERST_NUM_SEGS;
  322. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  323. val);
  324. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  325. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  326. /* set the segment table base address */
  327. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%x\n",
  328. xhci->erst.erst_dma_addr);
  329. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
  330. val = xhci_readl(xhci, &xhci->ir_set->erst_base[0]);
  331. val &= ERST_PTR_MASK;
  332. val |= (xhci->erst.erst_dma_addr & ~ERST_PTR_MASK);
  333. xhci_writel(xhci, val, &xhci->ir_set->erst_base[0]);
  334. /* Set the event ring dequeue address */
  335. xhci_dbg(xhci, "// Set ERST dequeue address for ir_set 0 = 0x%x%x\n",
  336. xhci->erst.entries[0].seg_addr[1], xhci->erst.entries[0].seg_addr[0]);
  337. val = xhci_readl(xhci, &xhci->run_regs->ir_set[0].erst_dequeue[0]);
  338. val &= ERST_PTR_MASK;
  339. val |= (xhci->erst.entries[0].seg_addr[0] & ~ERST_PTR_MASK);
  340. xhci_writel(xhci, val, &xhci->run_regs->ir_set[0].erst_dequeue[0]);
  341. xhci_writel(xhci, xhci->erst.entries[0].seg_addr[1],
  342. &xhci->run_regs->ir_set[0].erst_dequeue[1]);
  343. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  344. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  345. /*
  346. * XXX: Might need to set the Interrupter Moderation Register to
  347. * something other than the default (~1ms minimum between interrupts).
  348. * See section 5.5.1.2.
  349. */
  350. return 0;
  351. fail:
  352. xhci_warn(xhci, "Couldn't initialize memory\n");
  353. xhci_mem_cleanup(xhci);
  354. return -ENOMEM;
  355. }