xhci-mem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. 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. INIT_LIST_HEAD(&ring->td_list);
  129. if (num_segs == 0)
  130. return ring;
  131. ring->first_seg = xhci_segment_alloc(xhci, flags);
  132. if (!ring->first_seg)
  133. goto fail;
  134. num_segs--;
  135. prev = ring->first_seg;
  136. while (num_segs > 0) {
  137. struct xhci_segment *next;
  138. next = xhci_segment_alloc(xhci, flags);
  139. if (!next)
  140. goto fail;
  141. xhci_link_segments(xhci, prev, next, link_trbs);
  142. prev = next;
  143. num_segs--;
  144. }
  145. xhci_link_segments(xhci, prev, ring->first_seg, link_trbs);
  146. if (link_trbs) {
  147. /* See section 4.9.2.1 and 6.4.4.1 */
  148. prev->trbs[TRBS_PER_SEGMENT-1].link.control |= (LINK_TOGGLE);
  149. xhci_dbg(xhci, "Wrote link toggle flag to"
  150. " segment 0x%x (virtual), 0x%x (DMA)\n",
  151. (unsigned int) prev, (u32) prev->dma);
  152. }
  153. /* The ring is empty, so the enqueue pointer == dequeue pointer */
  154. ring->enqueue = ring->first_seg->trbs;
  155. ring->enq_seg = ring->first_seg;
  156. ring->dequeue = ring->enqueue;
  157. ring->deq_seg = ring->first_seg;
  158. /* The ring is initialized to 0. The producer must write 1 to the cycle
  159. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  160. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  161. */
  162. ring->cycle_state = 1;
  163. return ring;
  164. fail:
  165. xhci_ring_free(xhci, ring);
  166. return 0;
  167. }
  168. /* All the xhci_tds in the ring's TD list should be freed at this point */
  169. void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
  170. {
  171. struct xhci_virt_device *dev;
  172. int i;
  173. /* Slot ID 0 is reserved */
  174. if (slot_id == 0 || !xhci->devs[slot_id])
  175. return;
  176. dev = xhci->devs[slot_id];
  177. xhci->dcbaa->dev_context_ptrs[2*slot_id] = 0;
  178. xhci->dcbaa->dev_context_ptrs[2*slot_id + 1] = 0;
  179. if (!dev)
  180. return;
  181. for (i = 0; i < 31; ++i)
  182. if (dev->ep_rings[i])
  183. xhci_ring_free(xhci, dev->ep_rings[i]);
  184. if (dev->in_ctx)
  185. dma_pool_free(xhci->device_pool,
  186. dev->in_ctx, dev->in_ctx_dma);
  187. if (dev->out_ctx)
  188. dma_pool_free(xhci->device_pool,
  189. dev->out_ctx, dev->out_ctx_dma);
  190. kfree(xhci->devs[slot_id]);
  191. xhci->devs[slot_id] = 0;
  192. }
  193. int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
  194. struct usb_device *udev, gfp_t flags)
  195. {
  196. dma_addr_t dma;
  197. struct xhci_virt_device *dev;
  198. /* Slot ID 0 is reserved */
  199. if (slot_id == 0 || xhci->devs[slot_id]) {
  200. xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
  201. return 0;
  202. }
  203. xhci->devs[slot_id] = kzalloc(sizeof(*xhci->devs[slot_id]), flags);
  204. if (!xhci->devs[slot_id])
  205. return 0;
  206. dev = xhci->devs[slot_id];
  207. /* Allocate the (output) device context that will be used in the HC */
  208. dev->out_ctx = dma_pool_alloc(xhci->device_pool, flags, &dma);
  209. if (!dev->out_ctx)
  210. goto fail;
  211. dev->out_ctx_dma = dma;
  212. xhci_dbg(xhci, "Slot %d output ctx = 0x%x (dma)\n", slot_id, dma);
  213. memset(dev->out_ctx, 0, sizeof(*dev->out_ctx));
  214. /* Allocate the (input) device context for address device command */
  215. dev->in_ctx = dma_pool_alloc(xhci->device_pool, flags, &dma);
  216. if (!dev->in_ctx)
  217. goto fail;
  218. dev->in_ctx_dma = dma;
  219. xhci_dbg(xhci, "Slot %d input ctx = 0x%x (dma)\n", slot_id, dma);
  220. memset(dev->in_ctx, 0, sizeof(*dev->in_ctx));
  221. /* Allocate endpoint 0 ring */
  222. dev->ep_rings[0] = xhci_ring_alloc(xhci, 1, true, flags);
  223. if (!dev->ep_rings[0])
  224. goto fail;
  225. init_completion(&dev->cmd_completion);
  226. /*
  227. * Point to output device context in dcbaa; skip the output control
  228. * context, which is eight 32 bit fields (or 32 bytes long)
  229. */
  230. xhci->dcbaa->dev_context_ptrs[2*slot_id] =
  231. (u32) dev->out_ctx_dma + (32);
  232. xhci_dbg(xhci, "Set slot id %d dcbaa entry 0x%x to 0x%x\n",
  233. slot_id,
  234. (unsigned int) &xhci->dcbaa->dev_context_ptrs[2*slot_id],
  235. dev->out_ctx_dma);
  236. xhci->dcbaa->dev_context_ptrs[2*slot_id + 1] = 0;
  237. return 1;
  238. fail:
  239. xhci_free_virt_device(xhci, slot_id);
  240. return 0;
  241. }
  242. /* Setup an xHCI virtual device for a Set Address command */
  243. int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
  244. {
  245. struct xhci_virt_device *dev;
  246. struct xhci_ep_ctx *ep0_ctx;
  247. struct usb_device *top_dev;
  248. dev = xhci->devs[udev->slot_id];
  249. /* Slot ID 0 is reserved */
  250. if (udev->slot_id == 0 || !dev) {
  251. xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
  252. udev->slot_id);
  253. return -EINVAL;
  254. }
  255. ep0_ctx = &dev->in_ctx->ep[0];
  256. /* 2) New slot context and endpoint 0 context are valid*/
  257. dev->in_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
  258. /* 3) Only the control endpoint is valid - one endpoint context */
  259. dev->in_ctx->slot.dev_info |= LAST_CTX(1);
  260. switch (udev->speed) {
  261. case USB_SPEED_SUPER:
  262. dev->in_ctx->slot.dev_info |= (u32) udev->route;
  263. dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_SS;
  264. break;
  265. case USB_SPEED_HIGH:
  266. dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_HS;
  267. break;
  268. case USB_SPEED_FULL:
  269. dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_FS;
  270. break;
  271. case USB_SPEED_LOW:
  272. dev->in_ctx->slot.dev_info |= (u32) SLOT_SPEED_LS;
  273. break;
  274. case USB_SPEED_VARIABLE:
  275. xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
  276. return -EINVAL;
  277. break;
  278. default:
  279. /* Speed was set earlier, this shouldn't happen. */
  280. BUG();
  281. }
  282. /* Find the root hub port this device is under */
  283. for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
  284. top_dev = top_dev->parent)
  285. /* Found device below root hub */;
  286. dev->in_ctx->slot.dev_info2 |= (u32) ROOT_HUB_PORT(top_dev->portnum);
  287. xhci_dbg(xhci, "Set root hub portnum to %d\n", top_dev->portnum);
  288. /* Is this a LS/FS device under a HS hub? */
  289. /*
  290. * FIXME: I don't think this is right, where does the TT info for the
  291. * roothub or parent hub come from?
  292. */
  293. if ((udev->speed == USB_SPEED_LOW || udev->speed == USB_SPEED_FULL) &&
  294. udev->tt) {
  295. dev->in_ctx->slot.tt_info = udev->tt->hub->slot_id;
  296. dev->in_ctx->slot.tt_info |= udev->ttport << 8;
  297. }
  298. xhci_dbg(xhci, "udev->tt = 0x%x\n", (unsigned int) udev->tt);
  299. xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
  300. /* Step 4 - ring already allocated */
  301. /* Step 5 */
  302. ep0_ctx->ep_info2 = EP_TYPE(CTRL_EP);
  303. /*
  304. * See section 4.3 bullet 6:
  305. * The default Max Packet size for ep0 is "8 bytes for a USB2
  306. * LS/FS/HS device or 512 bytes for a USB3 SS device"
  307. * XXX: Not sure about wireless USB devices.
  308. */
  309. if (udev->speed == USB_SPEED_SUPER)
  310. ep0_ctx->ep_info2 |= MAX_PACKET(512);
  311. else
  312. ep0_ctx->ep_info2 |= MAX_PACKET(8);
  313. /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
  314. ep0_ctx->ep_info2 |= MAX_BURST(0);
  315. ep0_ctx->ep_info2 |= ERROR_COUNT(3);
  316. ep0_ctx->deq[0] =
  317. dev->ep_rings[0]->first_seg->dma;
  318. ep0_ctx->deq[0] |= dev->ep_rings[0]->cycle_state;
  319. ep0_ctx->deq[1] = 0;
  320. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  321. return 0;
  322. }
  323. /* Return the polling or NAK interval.
  324. *
  325. * The polling interval is expressed in "microframes". If xHCI's Interval field
  326. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  327. *
  328. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  329. * is set to 0.
  330. */
  331. static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  332. struct usb_host_endpoint *ep)
  333. {
  334. unsigned int interval = 0;
  335. switch (udev->speed) {
  336. case USB_SPEED_HIGH:
  337. /* Max NAK rate */
  338. if (usb_endpoint_xfer_control(&ep->desc) ||
  339. usb_endpoint_xfer_bulk(&ep->desc))
  340. interval = ep->desc.bInterval;
  341. /* Fall through - SS and HS isoc/int have same decoding */
  342. case USB_SPEED_SUPER:
  343. if (usb_endpoint_xfer_int(&ep->desc) ||
  344. usb_endpoint_xfer_isoc(&ep->desc)) {
  345. if (ep->desc.bInterval == 0)
  346. interval = 0;
  347. else
  348. interval = ep->desc.bInterval - 1;
  349. if (interval > 15)
  350. interval = 15;
  351. if (interval != ep->desc.bInterval + 1)
  352. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  353. ep->desc.bEndpointAddress, 1 << interval);
  354. }
  355. break;
  356. /* Convert bInterval (in 1-255 frames) to microframes and round down to
  357. * nearest power of 2.
  358. */
  359. case USB_SPEED_FULL:
  360. case USB_SPEED_LOW:
  361. if (usb_endpoint_xfer_int(&ep->desc) ||
  362. usb_endpoint_xfer_isoc(&ep->desc)) {
  363. interval = fls(8*ep->desc.bInterval) - 1;
  364. if (interval > 10)
  365. interval = 10;
  366. if (interval < 3)
  367. interval = 3;
  368. if ((1 << interval) != 8*ep->desc.bInterval)
  369. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  370. ep->desc.bEndpointAddress, 1 << interval);
  371. }
  372. break;
  373. default:
  374. BUG();
  375. }
  376. return EP_INTERVAL(interval);
  377. }
  378. static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
  379. struct usb_host_endpoint *ep)
  380. {
  381. int in;
  382. u32 type;
  383. in = usb_endpoint_dir_in(&ep->desc);
  384. if (usb_endpoint_xfer_control(&ep->desc)) {
  385. type = EP_TYPE(CTRL_EP);
  386. } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
  387. if (in)
  388. type = EP_TYPE(BULK_IN_EP);
  389. else
  390. type = EP_TYPE(BULK_OUT_EP);
  391. } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
  392. if (in)
  393. type = EP_TYPE(ISOC_IN_EP);
  394. else
  395. type = EP_TYPE(ISOC_OUT_EP);
  396. } else if (usb_endpoint_xfer_int(&ep->desc)) {
  397. if (in)
  398. type = EP_TYPE(INT_IN_EP);
  399. else
  400. type = EP_TYPE(INT_OUT_EP);
  401. } else {
  402. BUG();
  403. }
  404. return type;
  405. }
  406. int xhci_endpoint_init(struct xhci_hcd *xhci,
  407. struct xhci_virt_device *virt_dev,
  408. struct usb_device *udev,
  409. struct usb_host_endpoint *ep)
  410. {
  411. unsigned int ep_index;
  412. struct xhci_ep_ctx *ep_ctx;
  413. struct xhci_ring *ep_ring;
  414. unsigned int max_packet;
  415. unsigned int max_burst;
  416. ep_index = xhci_get_endpoint_index(&ep->desc);
  417. ep_ctx = &virt_dev->in_ctx->ep[ep_index];
  418. /* Set up the endpoint ring */
  419. virt_dev->new_ep_rings[ep_index] = xhci_ring_alloc(xhci, 1, true, GFP_KERNEL);
  420. if (!virt_dev->new_ep_rings[ep_index])
  421. return -ENOMEM;
  422. ep_ring = virt_dev->new_ep_rings[ep_index];
  423. ep_ctx->deq[1] = 0;
  424. ep_ctx->deq[0] = ep_ring->first_seg->dma | ep_ring->cycle_state;
  425. ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
  426. /* FIXME dig Mult and streams info out of ep companion desc */
  427. /* Allow 3 retries for everything but isoc */
  428. if (!usb_endpoint_xfer_isoc(&ep->desc))
  429. ep_ctx->ep_info2 = ERROR_COUNT(3);
  430. else
  431. ep_ctx->ep_info2 = ERROR_COUNT(0);
  432. ep_ctx->ep_info2 |= xhci_get_endpoint_type(udev, ep);
  433. /* Set the max packet size and max burst */
  434. switch (udev->speed) {
  435. case USB_SPEED_SUPER:
  436. max_packet = ep->desc.wMaxPacketSize;
  437. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  438. /* FIXME dig out burst from ep companion desc */
  439. break;
  440. case USB_SPEED_HIGH:
  441. /* bits 11:12 specify the number of additional transaction
  442. * opportunities per microframe (USB 2.0, section 9.6.6)
  443. */
  444. if (usb_endpoint_xfer_isoc(&ep->desc) ||
  445. usb_endpoint_xfer_int(&ep->desc)) {
  446. max_burst = (ep->desc.wMaxPacketSize & 0x1800) >> 11;
  447. ep_ctx->ep_info2 |= MAX_BURST(max_burst);
  448. }
  449. /* Fall through */
  450. case USB_SPEED_FULL:
  451. case USB_SPEED_LOW:
  452. max_packet = ep->desc.wMaxPacketSize & 0x3ff;
  453. ep_ctx->ep_info2 |= MAX_PACKET(max_packet);
  454. break;
  455. default:
  456. BUG();
  457. }
  458. /* FIXME Debug endpoint context */
  459. return 0;
  460. }
  461. void xhci_endpoint_zero(struct xhci_hcd *xhci,
  462. struct xhci_virt_device *virt_dev,
  463. struct usb_host_endpoint *ep)
  464. {
  465. unsigned int ep_index;
  466. struct xhci_ep_ctx *ep_ctx;
  467. ep_index = xhci_get_endpoint_index(&ep->desc);
  468. ep_ctx = &virt_dev->in_ctx->ep[ep_index];
  469. ep_ctx->ep_info = 0;
  470. ep_ctx->ep_info2 = 0;
  471. ep_ctx->deq[1] = 0;
  472. ep_ctx->deq[0] = 0;
  473. ep_ctx->tx_info = 0;
  474. /* Don't free the endpoint ring until the set interface or configuration
  475. * request succeeds.
  476. */
  477. }
  478. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  479. {
  480. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  481. int size;
  482. int i;
  483. /* Free the Event Ring Segment Table and the actual Event Ring */
  484. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  485. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
  486. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[0]);
  487. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[1]);
  488. xhci_writel(xhci, 0, &xhci->ir_set->erst_dequeue[0]);
  489. size = sizeof(struct xhci_erst_entry)*(xhci->erst.num_entries);
  490. if (xhci->erst.entries)
  491. pci_free_consistent(pdev, size,
  492. xhci->erst.entries, xhci->erst.erst_dma_addr);
  493. xhci->erst.entries = NULL;
  494. xhci_dbg(xhci, "Freed ERST\n");
  495. if (xhci->event_ring)
  496. xhci_ring_free(xhci, xhci->event_ring);
  497. xhci->event_ring = NULL;
  498. xhci_dbg(xhci, "Freed event ring\n");
  499. xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[1]);
  500. xhci_writel(xhci, 0, &xhci->op_regs->cmd_ring[0]);
  501. if (xhci->cmd_ring)
  502. xhci_ring_free(xhci, xhci->cmd_ring);
  503. xhci->cmd_ring = NULL;
  504. xhci_dbg(xhci, "Freed command ring\n");
  505. for (i = 1; i < MAX_HC_SLOTS; ++i)
  506. xhci_free_virt_device(xhci, i);
  507. if (xhci->segment_pool)
  508. dma_pool_destroy(xhci->segment_pool);
  509. xhci->segment_pool = NULL;
  510. xhci_dbg(xhci, "Freed segment pool\n");
  511. if (xhci->device_pool)
  512. dma_pool_destroy(xhci->device_pool);
  513. xhci->device_pool = NULL;
  514. xhci_dbg(xhci, "Freed device context pool\n");
  515. xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[1]);
  516. xhci_writel(xhci, 0, &xhci->op_regs->dcbaa_ptr[0]);
  517. if (xhci->dcbaa)
  518. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  519. xhci->dcbaa, xhci->dcbaa->dma);
  520. xhci->dcbaa = NULL;
  521. xhci->page_size = 0;
  522. xhci->page_shift = 0;
  523. }
  524. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  525. {
  526. dma_addr_t dma;
  527. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  528. unsigned int val, val2;
  529. struct xhci_segment *seg;
  530. u32 page_size;
  531. int i;
  532. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  533. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  534. for (i = 0; i < 16; i++) {
  535. if ((0x1 & page_size) != 0)
  536. break;
  537. page_size = page_size >> 1;
  538. }
  539. if (i < 16)
  540. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  541. else
  542. xhci_warn(xhci, "WARN: no supported page size\n");
  543. /* Use 4K pages, since that's common and the minimum the HC supports */
  544. xhci->page_shift = 12;
  545. xhci->page_size = 1 << xhci->page_shift;
  546. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  547. /*
  548. * Program the Number of Device Slots Enabled field in the CONFIG
  549. * register with the max value of slots the HC can handle.
  550. */
  551. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  552. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  553. (unsigned int) val);
  554. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  555. val |= (val2 & ~HCS_SLOTS_MASK);
  556. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  557. (unsigned int) val);
  558. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  559. /*
  560. * Section 5.4.8 - doorbell array must be
  561. * "physically contiguous and 64-byte (cache line) aligned".
  562. */
  563. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  564. sizeof(*xhci->dcbaa), &dma);
  565. if (!xhci->dcbaa)
  566. goto fail;
  567. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  568. xhci->dcbaa->dma = dma;
  569. xhci_dbg(xhci, "// Device context base array address = 0x%x (DMA), 0x%x (virt)\n",
  570. xhci->dcbaa->dma, (unsigned int) xhci->dcbaa);
  571. xhci_writel(xhci, (u32) 0, &xhci->op_regs->dcbaa_ptr[1]);
  572. xhci_writel(xhci, dma, &xhci->op_regs->dcbaa_ptr[0]);
  573. /*
  574. * Initialize the ring segment pool. The ring must be a contiguous
  575. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  576. * however, the command ring segment needs 64-byte aligned segments,
  577. * so we pick the greater alignment need.
  578. */
  579. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  580. SEGMENT_SIZE, 64, xhci->page_size);
  581. /* See Table 46 and Note on Figure 55 */
  582. /* FIXME support 64-byte contexts */
  583. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  584. sizeof(struct xhci_device_control),
  585. 64, xhci->page_size);
  586. if (!xhci->segment_pool || !xhci->device_pool)
  587. goto fail;
  588. /* Set up the command ring to have one segments for now. */
  589. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  590. if (!xhci->cmd_ring)
  591. goto fail;
  592. xhci_dbg(xhci, "Allocated command ring at 0x%x\n", (unsigned int) xhci->cmd_ring);
  593. xhci_dbg(xhci, "First segment DMA is 0x%x\n", (unsigned int) xhci->cmd_ring->first_seg->dma);
  594. /* Set the address in the Command Ring Control register */
  595. val = xhci_readl(xhci, &xhci->op_regs->cmd_ring[0]);
  596. val = (val & ~CMD_RING_ADDR_MASK) |
  597. (xhci->cmd_ring->first_seg->dma & CMD_RING_ADDR_MASK) |
  598. xhci->cmd_ring->cycle_state;
  599. xhci_dbg(xhci, "// Setting command ring address high bits to 0x0\n");
  600. xhci_writel(xhci, (u32) 0, &xhci->op_regs->cmd_ring[1]);
  601. xhci_dbg(xhci, "// Setting command ring address low bits to 0x%x\n", val);
  602. xhci_writel(xhci, val, &xhci->op_regs->cmd_ring[0]);
  603. xhci_dbg_cmd_ptrs(xhci);
  604. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  605. val &= DBOFF_MASK;
  606. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  607. " from cap regs base addr\n", val);
  608. xhci->dba = (void *) xhci->cap_regs + val;
  609. xhci_dbg_regs(xhci);
  610. xhci_print_run_regs(xhci);
  611. /* Set ir_set to interrupt register set 0 */
  612. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  613. /*
  614. * Event ring setup: Allocate a normal ring, but also setup
  615. * the event ring segment table (ERST). Section 4.9.3.
  616. */
  617. xhci_dbg(xhci, "// Allocating event ring\n");
  618. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  619. if (!xhci->event_ring)
  620. goto fail;
  621. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  622. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  623. if (!xhci->erst.entries)
  624. goto fail;
  625. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%x\n", dma);
  626. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  627. xhci->erst.num_entries = ERST_NUM_SEGS;
  628. xhci->erst.erst_dma_addr = dma;
  629. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = 0x%x, dma addr = 0x%x\n",
  630. xhci->erst.num_entries,
  631. (unsigned int) xhci->erst.entries,
  632. xhci->erst.erst_dma_addr);
  633. /* set ring base address and size for each segment table entry */
  634. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  635. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  636. entry->seg_addr[1] = 0;
  637. entry->seg_addr[0] = seg->dma;
  638. entry->seg_size = TRBS_PER_SEGMENT;
  639. entry->rsvd = 0;
  640. seg = seg->next;
  641. }
  642. /* set ERST count with the number of entries in the segment table */
  643. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  644. val &= ERST_SIZE_MASK;
  645. val |= ERST_NUM_SEGS;
  646. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  647. val);
  648. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  649. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  650. /* set the segment table base address */
  651. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%x\n",
  652. xhci->erst.erst_dma_addr);
  653. xhci_writel(xhci, 0, &xhci->ir_set->erst_base[1]);
  654. val = xhci_readl(xhci, &xhci->ir_set->erst_base[0]);
  655. val &= ERST_PTR_MASK;
  656. val |= (xhci->erst.erst_dma_addr & ~ERST_PTR_MASK);
  657. xhci_writel(xhci, val, &xhci->ir_set->erst_base[0]);
  658. /* Set the event ring dequeue address */
  659. set_hc_event_deq(xhci);
  660. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  661. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  662. /*
  663. * XXX: Might need to set the Interrupter Moderation Register to
  664. * something other than the default (~1ms minimum between interrupts).
  665. * See section 5.5.1.2.
  666. */
  667. init_completion(&xhci->addr_dev);
  668. for (i = 0; i < MAX_HC_SLOTS; ++i)
  669. xhci->devs[i] = 0;
  670. return 0;
  671. fail:
  672. xhci_warn(xhci, "Couldn't initialize memory\n");
  673. xhci_mem_cleanup(xhci);
  674. return -ENOMEM;
  675. }