xhci-mem.c 23 KB

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