xhci-mem.c 23 KB

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