[f4eb6c93] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Ondrej Hlavaty
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup drvusbxhci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * TRB Ring is a data structure for communication between HC and software.
|
---|
| 34 | *
|
---|
| 35 | * Despite this description, it is not used as an hardware structure - all but
|
---|
| 36 | * the Event ring is used as buffer of the TRBs itself, linked by Link TRB to
|
---|
| 37 | * form a (possibly multi-segment) circular buffer.
|
---|
| 38 | *
|
---|
| 39 | * This data structure abstracts this behavior.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | #ifndef XHCI_TRB_RING_H
|
---|
| 43 | #define XHCI_TRB_RING_H
|
---|
| 44 |
|
---|
| 45 | #include <adt/list.h>
|
---|
[a2b0ba3] | 46 | #include <fibril_synch.h>
|
---|
[f4eb6c93] | 47 | #include <libarch/config.h>
|
---|
| 48 |
|
---|
| 49 | typedef struct trb_segment trb_segment_t;
|
---|
[cb89430] | 50 | typedef struct xhci_hc xhci_hc_t;
|
---|
| 51 | typedef struct xhci_trb xhci_trb_t;
|
---|
| 52 | typedef struct xhci_erst_entry xhci_erst_entry_t;
|
---|
[f4eb6c93] | 53 |
|
---|
| 54 | /**
|
---|
| 55 | * A TRB ring of which the software is a producer - command / transfer.
|
---|
| 56 | */
|
---|
| 57 | typedef struct xhci_trb_ring {
|
---|
| 58 | list_t segments; /* List of assigned segments */
|
---|
[cb89430] | 59 | int segment_count; /* Number of segments assigned */
|
---|
[f4eb6c93] | 60 |
|
---|
| 61 | /*
|
---|
| 62 | * As the link TRBs connect physical addresses, we need to keep track of
|
---|
| 63 | * active segment in virtual memory. The enqueue ptr should always belong
|
---|
| 64 | * to the enqueue segment.
|
---|
| 65 | */
|
---|
| 66 | trb_segment_t *enqueue_segment;
|
---|
| 67 | xhci_trb_t *enqueue_trb;
|
---|
| 68 |
|
---|
| 69 | uintptr_t dequeue; /* Last reported position of the dequeue pointer */
|
---|
| 70 | bool pcs; /* Producer Cycle State: section 4.9.2 */
|
---|
[a2b0ba3] | 71 |
|
---|
| 72 | fibril_mutex_t guard;
|
---|
[f4eb6c93] | 73 | } xhci_trb_ring_t;
|
---|
| 74 |
|
---|
[cb89430] | 75 | int xhci_trb_ring_init(xhci_trb_ring_t *, xhci_hc_t *);
|
---|
| 76 | int xhci_trb_ring_fini(xhci_trb_ring_t *);
|
---|
[548c123] | 77 | int xhci_trb_ring_enqueue(xhci_trb_ring_t *, xhci_trb_t *, uintptr_t *);
|
---|
[f4eb6c93] | 78 |
|
---|
| 79 | /**
|
---|
[cb89430] | 80 | * Get the initial value to fill into CRCR.
|
---|
[f4eb6c93] | 81 | */
|
---|
[cb89430] | 82 | static inline uintptr_t xhci_trb_ring_get_dequeue_ptr(xhci_trb_ring_t *ring)
|
---|
| 83 | {
|
---|
| 84 | return ring->dequeue;
|
---|
| 85 | }
|
---|
[f4eb6c93] | 86 |
|
---|
| 87 | /**
|
---|
| 88 | * When an event is received by the upper layer, it needs to update the dequeue
|
---|
| 89 | * pointer inside the ring. Otherwise, the ring will soon show up as full.
|
---|
| 90 | */
|
---|
[cb89430] | 91 | void xhci_trb_ring_update_dequeue(xhci_trb_ring_t *, uintptr_t);
|
---|
| 92 | uintptr_t xhci_trb_ring_get_dequeue_ptr(xhci_trb_ring_t *);
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * A TRB ring of which the software is a consumer (event rings).
|
---|
| 96 | */
|
---|
| 97 | typedef struct xhci_event_ring {
|
---|
| 98 | list_t segments; /* List of assigned segments */
|
---|
| 99 | int segment_count; /* Number of segments assigned */
|
---|
| 100 |
|
---|
| 101 | trb_segment_t *dequeue_segment; /* Current segment of the dequeue ptr */
|
---|
| 102 | xhci_trb_t *dequeue_trb; /* Next TRB to be processed */
|
---|
| 103 | uintptr_t dequeue_ptr; /* Physical address of the ERDP to be reported to the HC */
|
---|
| 104 |
|
---|
| 105 | xhci_erst_entry_t *erst; /* ERST given to the HC */
|
---|
| 106 |
|
---|
| 107 | bool ccs; /* Consumer Cycle State: section 4.9.2 */
|
---|
[a1eb7c67] | 108 |
|
---|
| 109 | fibril_mutex_t guard;
|
---|
[cb89430] | 110 | } xhci_event_ring_t;
|
---|
[f4eb6c93] | 111 |
|
---|
[cb89430] | 112 | int xhci_event_ring_init(xhci_event_ring_t *, xhci_hc_t *);
|
---|
| 113 | int xhci_event_ring_fini(xhci_event_ring_t *);
|
---|
| 114 | int xhci_event_ring_dequeue(xhci_event_ring_t *, xhci_trb_t *);
|
---|
[f4eb6c93] | 115 |
|
---|
| 116 | #endif
|
---|