xhci-mem.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 = 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[slot_id] = 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[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[slot_id],
  236. (unsigned long long)dev->out_ctx_dma);
  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 = %p\n", 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 =
  317. dev->ep_rings[0]->first_seg->dma;
  318. ep0_ctx->deq |= dev->ep_rings[0]->cycle_state;
  319. /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
  320. return 0;
  321. }
  322. /* Return the polling or NAK interval.
  323. *
  324. * The polling interval is expressed in "microframes". If xHCI's Interval field
  325. * is set to N, it will service the endpoint every 2^(Interval)*125us.
  326. *
  327. * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
  328. * is set to 0.
  329. */
  330. static inline unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
  331. struct usb_host_endpoint *ep)
  332. {
  333. unsigned int interval = 0;
  334. switch (udev->speed) {
  335. case USB_SPEED_HIGH:
  336. /* Max NAK rate */
  337. if (usb_endpoint_xfer_control(&ep->desc) ||
  338. usb_endpoint_xfer_bulk(&ep->desc))
  339. interval = ep->desc.bInterval;
  340. /* Fall through - SS and HS isoc/int have same decoding */
  341. case USB_SPEED_SUPER:
  342. if (usb_endpoint_xfer_int(&ep->desc) ||
  343. usb_endpoint_xfer_isoc(&ep->desc)) {
  344. if (ep->desc.bInterval == 0)
  345. interval = 0;
  346. else
  347. interval = ep->desc.bInterval - 1;
  348. if (interval > 15)
  349. interval = 15;
  350. if (interval != ep->desc.bInterval + 1)
  351. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  352. ep->desc.bEndpointAddress, 1 << interval);
  353. }
  354. break;
  355. /* Convert bInterval (in 1-255 frames) to microframes and round down to
  356. * nearest power of 2.
  357. */
  358. case USB_SPEED_FULL:
  359. case USB_SPEED_LOW:
  360. if (usb_endpoint_xfer_int(&ep->desc) ||
  361. usb_endpoint_xfer_isoc(&ep->desc)) {
  362. interval = fls(8*ep->desc.bInterval) - 1;
  363. if (interval > 10)
  364. interval = 10;
  365. if (interval < 3)
  366. interval = 3;
  367. if ((1 << interval) != 8*ep->desc.bInterval)
  368. dev_warn(&udev->dev, "ep %#x - rounding interval to %d microframes\n",
  369. ep->desc.bEndpointAddress, 1 << interval);
  370. }
  371. break;
  372. default:
  373. BUG();
  374. }
  375. return EP_INTERVAL(interval);
  376. }
  377. static inline u32 xhci_get_endpoint_type(struct usb_device *udev,
  378. struct usb_host_endpoint *ep)
  379. {
  380. int in;
  381. u32 type;
  382. in = usb_endpoint_dir_in(&ep->desc);
  383. if (usb_endpoint_xfer_control(&ep->desc)) {
  384. type = EP_TYPE(CTRL_EP);
  385. } else if (usb_endpoint_xfer_bulk(&ep->desc)) {
  386. if (in)
  387. type = EP_TYPE(BULK_IN_EP);
  388. else
  389. type = EP_TYPE(BULK_OUT_EP);
  390. } else if (usb_endpoint_xfer_isoc(&ep->desc)) {
  391. if (in)
  392. type = EP_TYPE(ISOC_IN_EP);
  393. else
  394. type = EP_TYPE(ISOC_OUT_EP);
  395. } else if (usb_endpoint_xfer_int(&ep->desc)) {
  396. if (in)
  397. type = EP_TYPE(INT_IN_EP);
  398. else
  399. type = EP_TYPE(INT_OUT_EP);
  400. } else {
  401. BUG();
  402. }
  403. return type;
  404. }
  405. int xhci_endpoint_init(struct xhci_hcd *xhci,
  406. struct xhci_virt_device *virt_dev,
  407. struct usb_device *udev,
  408. struct usb_host_endpoint *ep,
  409. gfp_t mem_flags)
  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, mem_flags);
  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 = ep_ring->first_seg->dma | ep_ring->cycle_state;
  424. ep_ctx->ep_info = xhci_get_endpoint_interval(udev, ep);
  425. /* FIXME dig Mult and streams info out of ep companion desc */
  426. /* Allow 3 retries for everything but isoc;
  427. * error count = 0 means infinite retries.
  428. */
  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(1);
  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->ss_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 = 0;
  475. ep_ctx->tx_info = 0;
  476. /* Don't free the endpoint ring until the set interface or configuration
  477. * request succeeds.
  478. */
  479. }
  480. void xhci_mem_cleanup(struct xhci_hcd *xhci)
  481. {
  482. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  483. int size;
  484. int i;
  485. /* Free the Event Ring Segment Table and the actual Event Ring */
  486. xhci_writel(xhci, 0, &xhci->ir_set->erst_size);
  487. xhci_write_64(xhci, 0, &xhci->ir_set->erst_base);
  488. xhci_write_64(xhci, 0, &xhci->ir_set->erst_dequeue);
  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_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
  500. if (xhci->cmd_ring)
  501. xhci_ring_free(xhci, xhci->cmd_ring);
  502. xhci->cmd_ring = NULL;
  503. xhci_dbg(xhci, "Freed command ring\n");
  504. for (i = 1; i < MAX_HC_SLOTS; ++i)
  505. xhci_free_virt_device(xhci, i);
  506. if (xhci->segment_pool)
  507. dma_pool_destroy(xhci->segment_pool);
  508. xhci->segment_pool = NULL;
  509. xhci_dbg(xhci, "Freed segment pool\n");
  510. if (xhci->device_pool)
  511. dma_pool_destroy(xhci->device_pool);
  512. xhci->device_pool = NULL;
  513. xhci_dbg(xhci, "Freed device context pool\n");
  514. xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
  515. if (xhci->dcbaa)
  516. pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
  517. xhci->dcbaa, xhci->dcbaa->dma);
  518. xhci->dcbaa = NULL;
  519. xhci->page_size = 0;
  520. xhci->page_shift = 0;
  521. }
  522. int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
  523. {
  524. dma_addr_t dma;
  525. struct device *dev = xhci_to_hcd(xhci)->self.controller;
  526. unsigned int val, val2;
  527. u64 val_64;
  528. struct xhci_segment *seg;
  529. u32 page_size;
  530. int i;
  531. page_size = xhci_readl(xhci, &xhci->op_regs->page_size);
  532. xhci_dbg(xhci, "Supported page size register = 0x%x\n", page_size);
  533. for (i = 0; i < 16; i++) {
  534. if ((0x1 & page_size) != 0)
  535. break;
  536. page_size = page_size >> 1;
  537. }
  538. if (i < 16)
  539. xhci_dbg(xhci, "Supported page size of %iK\n", (1 << (i+12)) / 1024);
  540. else
  541. xhci_warn(xhci, "WARN: no supported page size\n");
  542. /* Use 4K pages, since that's common and the minimum the HC supports */
  543. xhci->page_shift = 12;
  544. xhci->page_size = 1 << xhci->page_shift;
  545. xhci_dbg(xhci, "HCD page size set to %iK\n", xhci->page_size / 1024);
  546. /*
  547. * Program the Number of Device Slots Enabled field in the CONFIG
  548. * register with the max value of slots the HC can handle.
  549. */
  550. val = HCS_MAX_SLOTS(xhci_readl(xhci, &xhci->cap_regs->hcs_params1));
  551. xhci_dbg(xhci, "// xHC can handle at most %d device slots.\n",
  552. (unsigned int) val);
  553. val2 = xhci_readl(xhci, &xhci->op_regs->config_reg);
  554. val |= (val2 & ~HCS_SLOTS_MASK);
  555. xhci_dbg(xhci, "// Setting Max device slots reg = 0x%x.\n",
  556. (unsigned int) val);
  557. xhci_writel(xhci, val, &xhci->op_regs->config_reg);
  558. /*
  559. * Section 5.4.8 - doorbell array must be
  560. * "physically contiguous and 64-byte (cache line) aligned".
  561. */
  562. xhci->dcbaa = pci_alloc_consistent(to_pci_dev(dev),
  563. sizeof(*xhci->dcbaa), &dma);
  564. if (!xhci->dcbaa)
  565. goto fail;
  566. memset(xhci->dcbaa, 0, sizeof *(xhci->dcbaa));
  567. xhci->dcbaa->dma = dma;
  568. xhci_dbg(xhci, "// Device context base array address = 0x%llx (DMA), %p (virt)\n",
  569. (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
  570. xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
  571. /*
  572. * Initialize the ring segment pool. The ring must be a contiguous
  573. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  574. * however, the command ring segment needs 64-byte aligned segments,
  575. * so we pick the greater alignment need.
  576. */
  577. xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
  578. SEGMENT_SIZE, 64, xhci->page_size);
  579. /* See Table 46 and Note on Figure 55 */
  580. /* FIXME support 64-byte contexts */
  581. xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
  582. sizeof(struct xhci_device_control),
  583. 64, xhci->page_size);
  584. if (!xhci->segment_pool || !xhci->device_pool)
  585. goto fail;
  586. /* Set up the command ring to have one segments for now. */
  587. xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
  588. if (!xhci->cmd_ring)
  589. goto fail;
  590. xhci_dbg(xhci, "Allocated command ring at %p\n", xhci->cmd_ring);
  591. xhci_dbg(xhci, "First segment DMA is 0x%llx\n",
  592. (unsigned long long)xhci->cmd_ring->first_seg->dma);
  593. /* Set the address in the Command Ring Control register */
  594. val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
  595. val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
  596. (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
  597. xhci->cmd_ring->cycle_state;
  598. xhci_dbg(xhci, "// Setting command ring address to 0x%x\n", val);
  599. xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
  600. xhci_dbg_cmd_ptrs(xhci);
  601. val = xhci_readl(xhci, &xhci->cap_regs->db_off);
  602. val &= DBOFF_MASK;
  603. xhci_dbg(xhci, "// Doorbell array is located at offset 0x%x"
  604. " from cap regs base addr\n", val);
  605. xhci->dba = (void *) xhci->cap_regs + val;
  606. xhci_dbg_regs(xhci);
  607. xhci_print_run_regs(xhci);
  608. /* Set ir_set to interrupt register set 0 */
  609. xhci->ir_set = (void *) xhci->run_regs->ir_set;
  610. /*
  611. * Event ring setup: Allocate a normal ring, but also setup
  612. * the event ring segment table (ERST). Section 4.9.3.
  613. */
  614. xhci_dbg(xhci, "// Allocating event ring\n");
  615. xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, false, flags);
  616. if (!xhci->event_ring)
  617. goto fail;
  618. xhci->erst.entries = pci_alloc_consistent(to_pci_dev(dev),
  619. sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS, &dma);
  620. if (!xhci->erst.entries)
  621. goto fail;
  622. xhci_dbg(xhci, "// Allocated event ring segment table at 0x%llx\n",
  623. (unsigned long long)dma);
  624. memset(xhci->erst.entries, 0, sizeof(struct xhci_erst_entry)*ERST_NUM_SEGS);
  625. xhci->erst.num_entries = ERST_NUM_SEGS;
  626. xhci->erst.erst_dma_addr = dma;
  627. xhci_dbg(xhci, "Set ERST to 0; private num segs = %i, virt addr = %p, dma addr = 0x%llx\n",
  628. xhci->erst.num_entries,
  629. xhci->erst.entries,
  630. (unsigned long long)xhci->erst.erst_dma_addr);
  631. /* set ring base address and size for each segment table entry */
  632. for (val = 0, seg = xhci->event_ring->first_seg; val < ERST_NUM_SEGS; val++) {
  633. struct xhci_erst_entry *entry = &xhci->erst.entries[val];
  634. entry->seg_addr = seg->dma;
  635. entry->seg_size = TRBS_PER_SEGMENT;
  636. entry->rsvd = 0;
  637. seg = seg->next;
  638. }
  639. /* set ERST count with the number of entries in the segment table */
  640. val = xhci_readl(xhci, &xhci->ir_set->erst_size);
  641. val &= ERST_SIZE_MASK;
  642. val |= ERST_NUM_SEGS;
  643. xhci_dbg(xhci, "// Write ERST size = %i to ir_set 0 (some bits preserved)\n",
  644. val);
  645. xhci_writel(xhci, val, &xhci->ir_set->erst_size);
  646. xhci_dbg(xhci, "// Set ERST entries to point to event ring.\n");
  647. /* set the segment table base address */
  648. xhci_dbg(xhci, "// Set ERST base address for ir_set 0 = 0x%llx\n",
  649. (unsigned long long)xhci->erst.erst_dma_addr);
  650. val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
  651. val_64 &= ERST_PTR_MASK;
  652. val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
  653. xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
  654. /* Set the event ring dequeue address */
  655. xhci_set_hc_event_deq(xhci);
  656. xhci_dbg(xhci, "Wrote ERST address to ir_set 0.\n");
  657. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  658. /*
  659. * XXX: Might need to set the Interrupter Moderation Register to
  660. * something other than the default (~1ms minimum between interrupts).
  661. * See section 5.5.1.2.
  662. */
  663. init_completion(&xhci->addr_dev);
  664. for (i = 0; i < MAX_HC_SLOTS; ++i)
  665. xhci->devs[i] = 0;
  666. return 0;
  667. fail:
  668. xhci_warn(xhci, "Couldn't initialize memory\n");
  669. xhci_mem_cleanup(xhci);
  670. return -ENOMEM;
  671. }