aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/host/xhci-mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/host/xhci-mem.c')
-rw-r--r--drivers/usb/host/xhci-mem.c379
1 files changed, 378 insertions, 1 deletions
diff --git a/drivers/usb/host/xhci-mem.c b/drivers/usb/host/xhci-mem.c
index d64f5724bfc4..d299ffad806b 100644
--- a/drivers/usb/host/xhci-mem.c
+++ b/drivers/usb/host/xhci-mem.c
@@ -304,6 +304,350 @@ struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
304 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params))); 304 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
305} 305}
306 306
307
308/***************** Streams structures manipulation *************************/
309
310void xhci_free_stream_ctx(struct xhci_hcd *xhci,
311 unsigned int num_stream_ctxs,
312 struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
313{
314 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
315
316 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
317 pci_free_consistent(pdev,
318 sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
319 stream_ctx, dma);
320 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
321 return dma_pool_free(xhci->small_streams_pool,
322 stream_ctx, dma);
323 else
324 return dma_pool_free(xhci->medium_streams_pool,
325 stream_ctx, dma);
326}
327
328/*
329 * The stream context array for each endpoint with bulk streams enabled can
330 * vary in size, based on:
331 * - how many streams the endpoint supports,
332 * - the maximum primary stream array size the host controller supports,
333 * - and how many streams the device driver asks for.
334 *
335 * The stream context array must be a power of 2, and can be as small as
336 * 64 bytes or as large as 1MB.
337 */
338struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
339 unsigned int num_stream_ctxs, dma_addr_t *dma,
340 gfp_t mem_flags)
341{
342 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
343
344 if (num_stream_ctxs > MEDIUM_STREAM_ARRAY_SIZE)
345 return pci_alloc_consistent(pdev,
346 sizeof(struct xhci_stream_ctx)*num_stream_ctxs,
347 dma);
348 else if (num_stream_ctxs <= SMALL_STREAM_ARRAY_SIZE)
349 return dma_pool_alloc(xhci->small_streams_pool,
350 mem_flags, dma);
351 else
352 return dma_pool_alloc(xhci->medium_streams_pool,
353 mem_flags, dma);
354}
355
356#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
357struct xhci_ring *dma_to_stream_ring(
358 struct xhci_stream_info *stream_info,
359 u64 address)
360{
361 return radix_tree_lookup(&stream_info->trb_address_map,
362 address >> SEGMENT_SHIFT);
363}
364#endif /* CONFIG_USB_XHCI_HCD_DEBUGGING */
365
366#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
367static int xhci_test_radix_tree(struct xhci_hcd *xhci,
368 unsigned int num_streams,
369 struct xhci_stream_info *stream_info)
370{
371 u32 cur_stream;
372 struct xhci_ring *cur_ring;
373 u64 addr;
374
375 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
376 struct xhci_ring *mapped_ring;
377 int trb_size = sizeof(union xhci_trb);
378
379 cur_ring = stream_info->stream_rings[cur_stream];
380 for (addr = cur_ring->first_seg->dma;
381 addr < cur_ring->first_seg->dma + SEGMENT_SIZE;
382 addr += trb_size) {
383 mapped_ring = dma_to_stream_ring(stream_info, addr);
384 if (cur_ring != mapped_ring) {
385 xhci_warn(xhci, "WARN: DMA address 0x%08llx "
386 "didn't map to stream ID %u; "
387 "mapped to ring %p\n",
388 (unsigned long long) addr,
389 cur_stream,
390 mapped_ring);
391 return -EINVAL;
392 }
393 }
394 /* One TRB after the end of the ring segment shouldn't return a
395 * pointer to the current ring (although it may be a part of a
396 * different ring).
397 */
398 mapped_ring = dma_to_stream_ring(stream_info, addr);
399 if (mapped_ring != cur_ring) {
400 /* One TRB before should also fail */
401 addr = cur_ring->first_seg->dma - trb_size;
402 mapped_ring = dma_to_stream_ring(stream_info, addr);
403 }
404 if (mapped_ring == cur_ring) {
405 xhci_warn(xhci, "WARN: Bad DMA address 0x%08llx "
406 "mapped to valid stream ID %u; "
407 "mapped ring = %p\n",
408 (unsigned long long) addr,
409 cur_stream,
410 mapped_ring);
411 return -EINVAL;
412 }
413 }
414 return 0;
415}
416#endif /* CONFIG_USB_XHCI_HCD_DEBUGGING */
417
418/*
419 * Change an endpoint's internal structure so it supports stream IDs. The
420 * number of requested streams includes stream 0, which cannot be used by device
421 * drivers.
422 *
423 * The number of stream contexts in the stream context array may be bigger than
424 * the number of streams the driver wants to use. This is because the number of
425 * stream context array entries must be a power of two.
426 *
427 * We need a radix tree for mapping physical addresses of TRBs to which stream
428 * ID they belong to. We need to do this because the host controller won't tell
429 * us which stream ring the TRB came from. We could store the stream ID in an
430 * event data TRB, but that doesn't help us for the cancellation case, since the
431 * endpoint may stop before it reaches that event data TRB.
432 *
433 * The radix tree maps the upper portion of the TRB DMA address to a ring
434 * segment that has the same upper portion of DMA addresses. For example, say I
435 * have segments of size 1KB, that are always 64-byte aligned. A segment may
436 * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
437 * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
438 * pass the radix tree a key to get the right stream ID:
439 *
440 * 0x10c90fff >> 10 = 0x43243
441 * 0x10c912c0 >> 10 = 0x43244
442 * 0x10c91400 >> 10 = 0x43245
443 *
444 * Obviously, only those TRBs with DMA addresses that are within the segment
445 * will make the radix tree return the stream ID for that ring.
446 *
447 * Caveats for the radix tree:
448 *
449 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
450 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
451 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
452 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
453 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
454 * extended systems (where the DMA address can be bigger than 32-bits),
455 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
456 */
457struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
458 unsigned int num_stream_ctxs,
459 unsigned int num_streams, gfp_t mem_flags)
460{
461 struct xhci_stream_info *stream_info;
462 u32 cur_stream;
463 struct xhci_ring *cur_ring;
464 unsigned long key;
465 u64 addr;
466 int ret;
467
468 xhci_dbg(xhci, "Allocating %u streams and %u "
469 "stream context array entries.\n",
470 num_streams, num_stream_ctxs);
471 if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) {
472 xhci_dbg(xhci, "Command ring has no reserved TRBs available\n");
473 return NULL;
474 }
475 xhci->cmd_ring_reserved_trbs++;
476
477 stream_info = kzalloc(sizeof(struct xhci_stream_info), mem_flags);
478 if (!stream_info)
479 goto cleanup_trbs;
480
481 stream_info->num_streams = num_streams;
482 stream_info->num_stream_ctxs = num_stream_ctxs;
483
484 /* Initialize the array of virtual pointers to stream rings. */
485 stream_info->stream_rings = kzalloc(
486 sizeof(struct xhci_ring *)*num_streams,
487 mem_flags);
488 if (!stream_info->stream_rings)
489 goto cleanup_info;
490
491 /* Initialize the array of DMA addresses for stream rings for the HW. */
492 stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci,
493 num_stream_ctxs, &stream_info->ctx_array_dma,
494 mem_flags);
495 if (!stream_info->stream_ctx_array)
496 goto cleanup_ctx;
497 memset(stream_info->stream_ctx_array, 0,
498 sizeof(struct xhci_stream_ctx)*num_stream_ctxs);
499
500 /* Allocate everything needed to free the stream rings later */
501 stream_info->free_streams_command =
502 xhci_alloc_command(xhci, true, true, mem_flags);
503 if (!stream_info->free_streams_command)
504 goto cleanup_ctx;
505
506 INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
507
508 /* Allocate rings for all the streams that the driver will use,
509 * and add their segment DMA addresses to the radix tree.
510 * Stream 0 is reserved.
511 */
512 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
513 stream_info->stream_rings[cur_stream] =
514 xhci_ring_alloc(xhci, 1, true, mem_flags);
515 cur_ring = stream_info->stream_rings[cur_stream];
516 if (!cur_ring)
517 goto cleanup_rings;
518 /* Set deq ptr, cycle bit, and stream context type */
519 addr = cur_ring->first_seg->dma |
520 SCT_FOR_CTX(SCT_PRI_TR) |
521 cur_ring->cycle_state;
522 stream_info->stream_ctx_array[cur_stream].stream_ring = addr;
523 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
524 cur_stream, (unsigned long long) addr);
525
526 key = (unsigned long)
527 (cur_ring->first_seg->dma >> SEGMENT_SHIFT);
528 ret = radix_tree_insert(&stream_info->trb_address_map,
529 key, cur_ring);
530 if (ret) {
531 xhci_ring_free(xhci, cur_ring);
532 stream_info->stream_rings[cur_stream] = NULL;
533 goto cleanup_rings;
534 }
535 }
536 /* Leave the other unused stream ring pointers in the stream context
537 * array initialized to zero. This will cause the xHC to give us an
538 * error if the device asks for a stream ID we don't have setup (if it
539 * was any other way, the host controller would assume the ring is
540 * "empty" and wait forever for data to be queued to that stream ID).
541 */
542#if XHCI_DEBUG
543 /* Do a little test on the radix tree to make sure it returns the
544 * correct values.
545 */
546 if (xhci_test_radix_tree(xhci, num_streams, stream_info))
547 goto cleanup_rings;
548#endif
549
550 return stream_info;
551
552cleanup_rings:
553 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
554 cur_ring = stream_info->stream_rings[cur_stream];
555 if (cur_ring) {
556 addr = cur_ring->first_seg->dma;
557 radix_tree_delete(&stream_info->trb_address_map,
558 addr >> SEGMENT_SHIFT);
559 xhci_ring_free(xhci, cur_ring);
560 stream_info->stream_rings[cur_stream] = NULL;
561 }
562 }
563 xhci_free_command(xhci, stream_info->free_streams_command);
564cleanup_ctx:
565 kfree(stream_info->stream_rings);
566cleanup_info:
567 kfree(stream_info);
568cleanup_trbs:
569 xhci->cmd_ring_reserved_trbs--;
570 return NULL;
571}
572/*
573 * Sets the MaxPStreams field and the Linear Stream Array field.
574 * Sets the dequeue pointer to the stream context array.
575 */
576void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
577 struct xhci_ep_ctx *ep_ctx,
578 struct xhci_stream_info *stream_info)
579{
580 u32 max_primary_streams;
581 /* MaxPStreams is the number of stream context array entries, not the
582 * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
583 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
584 */
585 max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
586 xhci_dbg(xhci, "Setting number of stream ctx array entries to %u\n",
587 1 << (max_primary_streams + 1));
588 ep_ctx->ep_info &= ~EP_MAXPSTREAMS_MASK;
589 ep_ctx->ep_info |= EP_MAXPSTREAMS(max_primary_streams);
590 ep_ctx->ep_info |= EP_HAS_LSA;
591 ep_ctx->deq = stream_info->ctx_array_dma;
592}
593
594/*
595 * Sets the MaxPStreams field and the Linear Stream Array field to 0.
596 * Reinstalls the "normal" endpoint ring (at its previous dequeue mark,
597 * not at the beginning of the ring).
598 */
599void xhci_setup_no_streams_ep_input_ctx(struct xhci_hcd *xhci,
600 struct xhci_ep_ctx *ep_ctx,
601 struct xhci_virt_ep *ep)
602{
603 dma_addr_t addr;
604 ep_ctx->ep_info &= ~EP_MAXPSTREAMS_MASK;
605 ep_ctx->ep_info &= ~EP_HAS_LSA;
606 addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue);
607 ep_ctx->deq = addr | ep->ring->cycle_state;
608}
609
610/* Frees all stream contexts associated with the endpoint,
611 *
612 * Caller should fix the endpoint context streams fields.
613 */
614void xhci_free_stream_info(struct xhci_hcd *xhci,
615 struct xhci_stream_info *stream_info)
616{
617 int cur_stream;
618 struct xhci_ring *cur_ring;
619 dma_addr_t addr;
620
621 if (!stream_info)
622 return;
623
624 for (cur_stream = 1; cur_stream < stream_info->num_streams;
625 cur_stream++) {
626 cur_ring = stream_info->stream_rings[cur_stream];
627 if (cur_ring) {
628 addr = cur_ring->first_seg->dma;
629 radix_tree_delete(&stream_info->trb_address_map,
630 addr >> SEGMENT_SHIFT);
631 xhci_ring_free(xhci, cur_ring);
632 stream_info->stream_rings[cur_stream] = NULL;
633 }
634 }
635 xhci_free_command(xhci, stream_info->free_streams_command);
636 xhci->cmd_ring_reserved_trbs--;
637 if (stream_info->stream_ctx_array)
638 xhci_free_stream_ctx(xhci,
639 stream_info->num_stream_ctxs,
640 stream_info->stream_ctx_array,
641 stream_info->ctx_array_dma);
642
643 if (stream_info)
644 kfree(stream_info->stream_rings);
645 kfree(stream_info);
646}
647
648
649/***************** Device context manipulation *************************/
650
307static void xhci_init_endpoint_timer(struct xhci_hcd *xhci, 651static void xhci_init_endpoint_timer(struct xhci_hcd *xhci,
308 struct xhci_virt_ep *ep) 652 struct xhci_virt_ep *ep)
309{ 653{
@@ -328,9 +672,13 @@ void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
328 if (!dev) 672 if (!dev)
329 return; 673 return;
330 674
331 for (i = 0; i < 31; ++i) 675 for (i = 0; i < 31; ++i) {
332 if (dev->eps[i].ring) 676 if (dev->eps[i].ring)
333 xhci_ring_free(xhci, dev->eps[i].ring); 677 xhci_ring_free(xhci, dev->eps[i].ring);
678 if (dev->eps[i].stream_info)
679 xhci_free_stream_info(xhci,
680 dev->eps[i].stream_info);
681 }
334 682
335 if (dev->ring_cache) { 683 if (dev->ring_cache) {
336 for (i = 0; i < dev->num_rings_cached; i++) 684 for (i = 0; i < dev->num_rings_cached; i++)
@@ -655,6 +1003,9 @@ static inline u32 xhci_get_max_esit_payload(struct xhci_hcd *xhci,
655 return max_packet * (max_burst + 1); 1003 return max_packet * (max_burst + 1);
656} 1004}
657 1005
1006/* Set up an endpoint with one ring segment. Do not allocate stream rings.
1007 * Drivers will have to call usb_alloc_streams() to do that.
1008 */
658int xhci_endpoint_init(struct xhci_hcd *xhci, 1009int xhci_endpoint_init(struct xhci_hcd *xhci,
659 struct xhci_virt_device *virt_dev, 1010 struct xhci_virt_device *virt_dev,
660 struct usb_device *udev, 1011 struct usb_device *udev,
@@ -1003,6 +1354,16 @@ void xhci_mem_cleanup(struct xhci_hcd *xhci)
1003 xhci->device_pool = NULL; 1354 xhci->device_pool = NULL;
1004 xhci_dbg(xhci, "Freed device context pool\n"); 1355 xhci_dbg(xhci, "Freed device context pool\n");
1005 1356
1357 if (xhci->small_streams_pool)
1358 dma_pool_destroy(xhci->small_streams_pool);
1359 xhci->small_streams_pool = NULL;
1360 xhci_dbg(xhci, "Freed small stream array pool\n");
1361
1362 if (xhci->medium_streams_pool)
1363 dma_pool_destroy(xhci->medium_streams_pool);
1364 xhci->medium_streams_pool = NULL;
1365 xhci_dbg(xhci, "Freed medium stream array pool\n");
1366
1006 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr); 1367 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
1007 if (xhci->dcbaa) 1368 if (xhci->dcbaa)
1008 pci_free_consistent(pdev, sizeof(*xhci->dcbaa), 1369 pci_free_consistent(pdev, sizeof(*xhci->dcbaa),
@@ -1239,6 +1600,22 @@ int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
1239 if (!xhci->segment_pool || !xhci->device_pool) 1600 if (!xhci->segment_pool || !xhci->device_pool)
1240 goto fail; 1601 goto fail;
1241 1602
1603 /* Linear stream context arrays don't have any boundary restrictions,
1604 * and only need to be 16-byte aligned.
1605 */
1606 xhci->small_streams_pool =
1607 dma_pool_create("xHCI 256 byte stream ctx arrays",
1608 dev, SMALL_STREAM_ARRAY_SIZE, 16, 0);
1609 xhci->medium_streams_pool =
1610 dma_pool_create("xHCI 1KB stream ctx arrays",
1611 dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
1612 /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
1613 * will be allocated with pci_alloc_consistent()
1614 */
1615
1616 if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
1617 goto fail;
1618
1242 /* Set up the command ring to have one segments for now. */ 1619 /* Set up the command ring to have one segments for now. */
1243 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags); 1620 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, true, flags);
1244 if (!xhci->cmd_ring) 1621 if (!xhci->cmd_ring)