Changeset b80c1ab in mainline


Ignore:
Timestamp:
2017-11-14T23:17:54Z (6 years ago)
Author:
Aearsis <Hlavaty.Ondrej@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e76c0ea
Parents:
cfe4852
git-author:
Aearsis <Hlavaty.Ondrej@…> (2017-11-14 23:15:24)
git-committer:
Aearsis <Hlavaty.Ondrej@…> (2017-11-14 23:17:54)
Message:

xhci: use dma_buffers instead of malloc32 util

A bit of refactoring was needed to adapt scratchpad buffers.

Location:
uspace/drv/bus/usb/xhci
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/xhci/bus.c

    rcfe4852 rb80c1ab  
    3333 */
    3434
    35 #include <usb/host/utils/malloc32.h>
    3635#include <usb/host/ddf_helpers.h>
    3736#include <usb/host/endpoint.h>
  • uspace/drv/bus/usb/xhci/commands.c

    rcfe4852 rb80c1ab  
    3737#include <str_error.h>
    3838#include <usb/debug.h>
    39 #include <usb/host/utils/malloc32.h>
    4039#include "commands.h"
    4140#include "debug.h"
     
    5756 */
    5857#define TRB_SET_DEQUEUE_PTR(trb, dptr) (trb).parameter |= host2xhci(64, (dptr))
    59 #define TRB_SET_ICTX(trb, phys) (trb).parameter |= host2xhci(64, phys_addr & (~0xF))
     58#define TRB_SET_ICTX(trb, phys) (trb).parameter |= host2xhci(64, (phys) & (~0xF))
    6059
    6160#define TRB_GET_CODE(trb) XHCI_DWORD_EXTRACT((trb).status, 31, 24)
     
    9998        list_remove(&cmd->_header.link);
    10099
    101         if (cmd->input_ctx) {
    102                 free32(cmd->input_ctx);
    103         };
    104 
    105         if (cmd->bandwidth_ctx) {
    106                 free32(cmd->bandwidth_ctx);
    107         }
     100        dma_buffer_free(&cmd->input_ctx);
     101        dma_buffer_free(&cmd->bandwidth_ctx);
    108102
    109103        if (cmd->_header.async) {
     
    360354        assert(hc);
    361355        assert(cmd);
    362         assert(cmd->input_ctx);
     356        assert(dma_buffer_is_set(&cmd->input_ctx));
    363357
    364358        /**
     
    371365        xhci_trb_clean(&cmd->_header.trb);
    372366
    373         uint64_t phys_addr = (uint64_t) addr_to_phys(cmd->input_ctx);
    374         TRB_SET_ICTX(cmd->_header.trb, phys_addr);
     367        TRB_SET_ICTX(cmd->_header.trb, cmd->input_ctx.phys);
    375368
    376369        /**
     
    396389        if (!cmd->deconfigure) {
    397390                /* If the DC flag is on, input context is not evaluated. */
    398                 assert(cmd->input_ctx);
    399 
    400                 uint64_t phys_addr = (uint64_t) addr_to_phys(cmd->input_ctx);
    401                 TRB_SET_ICTX(cmd->_header.trb, phys_addr);
     391                assert(dma_buffer_is_set(&cmd->input_ctx));
     392
     393                TRB_SET_ICTX(cmd->_header.trb, cmd->input_ctx.phys);
    402394        }
    403395
     
    413405        assert(hc);
    414406        assert(cmd);
    415         assert(cmd->input_ctx);
     407        assert(dma_buffer_is_set(&cmd->input_ctx));
    416408
    417409        /**
     
    423415        xhci_trb_clean(&cmd->_header.trb);
    424416
    425         uint64_t phys_addr = (uint64_t) addr_to_phys(cmd->input_ctx);
    426         TRB_SET_ICTX(cmd->_header.trb, phys_addr);
     417        TRB_SET_ICTX(cmd->_header.trb, cmd->input_ctx.phys);
    427418
    428419        TRB_SET_TYPE(cmd->_header.trb, XHCI_TRB_TYPE_EVALUATE_CONTEXT_CMD);
     
    506497        xhci_trb_clean(&cmd->_header.trb);
    507498
    508         uint64_t phys_addr = (uint64_t) addr_to_phys(cmd->bandwidth_ctx);
    509         TRB_SET_ICTX(cmd->_header.trb, phys_addr);
     499        TRB_SET_ICTX(cmd->_header.trb, cmd->bandwidth_ctx.phys);
    510500
    511501        TRB_SET_TYPE(cmd->_header.trb, XHCI_TRB_TYPE_GET_PORT_BANDWIDTH_CMD);
  • uspace/drv/bus/usb/xhci/commands.h

    rcfe4852 rb80c1ab  
    4040#include <stdbool.h>
    4141#include <fibril_synch.h>
     42#include <usb/host/dma_buffer.h>
    4243#include "hw_struct/trb.h"
    4344
     
    9394        uint16_t stream_id;
    9495
    95         xhci_input_ctx_t *input_ctx;
    96         xhci_port_bandwidth_ctx_t *bandwidth_ctx;
     96        dma_buffer_t input_ctx, bandwidth_ctx;
    9797        uintptr_t dequeue_ptr;
    9898
  • uspace/drv/bus/usb/xhci/endpoint.c

    rcfe4852 rb80c1ab  
    3434 */
    3535
    36 #include <usb/host/utils/malloc32.h>
    3736#include <usb/host/endpoint.h>
    3837#include <usb/descriptor.h>
     
    135134
    136135        XHCI_EP_MAX_P_STREAMS_SET(*ctx, pstreams);
    137         XHCI_EP_TR_DPTR_SET(*ctx, addr_to_phys(xhci_ep->primary_stream_ctx_array));
     136        XHCI_EP_TR_DPTR_SET(*ctx, xhci_ep->primary_stream_ctx_dma.phys);
    138137        // TODO: set HID?
    139138        XHCI_EP_LSA_SET(*ctx, 1);
     
    167166                usb_log_debug2("Allocating primary stream context array of size %u for endpoint " XHCI_EP_FMT,
    168167                        count, XHCI_EP_ARGS(*xhci_ep));
    169                 xhci_ep->primary_stream_ctx_array = malloc32(count * sizeof(xhci_stream_ctx_t));
    170                 if (!xhci_ep->primary_stream_ctx_array) {
     168                if ((dma_buffer_alloc(&xhci_ep->primary_stream_ctx_dma, count * sizeof(xhci_stream_ctx_t))))
    171169                        return ENOMEM;
    172                 }
     170                xhci_ep->primary_stream_ctx_array = xhci_ep->primary_stream_ctx_dma.virt;
    173171
    174172                xhci_ep->primary_stream_rings = calloc(count, sizeof(xhci_trb_ring_t));
    175173                if (!xhci_ep->primary_stream_rings) {
    176                         free32(xhci_ep->primary_stream_ctx_array);
     174                        dma_buffer_free(&xhci_ep->primary_stream_ctx_dma);
    177175                        return ENOMEM;
    178176                }
     
    227225                        // FIXME: Get the trb ring associated with stream [index] and fini it
    228226                }
    229                 free32(xhci_ep->primary_stream_ctx_array);
     227                dma_buffer_free(&xhci_ep->primary_stream_ctx_dma);
    230228        } else {
    231229                usb_log_debug2("Freeing main transfer ring of endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
  • uspace/drv/bus/usb/xhci/endpoint.h

    rcfe4852 rb80c1ab  
    4040
    4141#include <usb/debug.h>
     42#include <usb/host/dma_buffer.h>
    4243#include <usb/host/endpoint.h>
    4344#include <usb/host/hcd.h>
     
    7273        /** Primary stream context array (or NULL if endpoint doesn't use streams). */
    7374        xhci_stream_ctx_t *primary_stream_ctx_array;
     75        dma_buffer_t primary_stream_ctx_dma;
    7476
    7577        /** Primary stream ring array (or NULL if endpoint doesn't use streams). */
     
    111113        uint32_t route_str;
    112114
    113         /** Place to store virtual address for allocated context */
    114         xhci_device_ctx_t *dev_ctx;
     115        /** Place to store the allocated context */
     116        dma_buffer_t dev_ctx;
    115117
    116118        /** All endpoints of the device. Dropped ones are NULL */
  • uspace/drv/bus/usb/xhci/hc.c

    rcfe4852 rb80c1ab  
    3838#include <usb/debug.h>
    3939#include <usb/host/endpoint.h>
    40 #include <usb/host/utils/malloc32.h>
    4140#include "debug.h"
    4241#include "hc.h"
     
    201200        int err;
    202201
    203         hc->dcbaa = malloc32((1 + hc->max_slots) * sizeof(uint64_t));
    204         if (!hc->dcbaa)
     202        if (dma_buffer_alloc(&hc->dcbaa_dma, (1 + hc->max_slots) * sizeof(uint64_t)))
    205203                return ENOMEM;
     204        hc->dcbaa = hc->dcbaa_dma.virt;
    206205
    207206        if ((err = xhci_trb_ring_init(&hc->command_ring)))
     
    237236        xhci_trb_ring_fini(&hc->command_ring);
    238237err_dcbaa:
    239         free32(hc->dcbaa);
     238        hc->dcbaa = NULL;
     239        dma_buffer_free(&hc->dcbaa_dma);
    240240        return err;
    241241}
     
    402402                async_usleep(1000);
    403403
    404         uint64_t dcbaaptr = addr_to_phys(hc->dcbaa);
     404        uint64_t dcbaaptr = hc->dcbaa_dma.phys;
    405405        XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_LO, LOWER32(dcbaaptr));
    406406        XHCI_REG_WR(hc->op_regs, XHCI_OP_DCBAAP_HI, UPPER32(dcbaaptr));
     
    411411        XHCI_REG_WR(hc->op_regs, XHCI_OP_CRCR_HI, UPPER32(crptr));
    412412
    413         uint64_t erstptr = addr_to_phys(hc->event_ring.erst);
    414         uint64_t erdp = hc->event_ring.dequeue_ptr;
    415413        xhci_interrupter_regs_t *intr0 = &hc->rt_regs->ir[0];
    416414        XHCI_REG_WR(intr0, XHCI_INTR_ERSTSZ, hc->event_ring.segment_count);
     415        uint64_t erdp = hc->event_ring.dequeue_ptr;
    417416        XHCI_REG_WR(intr0, XHCI_INTR_ERDP_LO, LOWER32(erdp));
    418417        XHCI_REG_WR(intr0, XHCI_INTR_ERDP_HI, UPPER32(erdp));
     418        uint64_t erstptr = hc->event_ring.erst.phys;
    419419        XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_LO, LOWER32(erstptr));
    420420        XHCI_REG_WR(intr0, XHCI_INTR_ERSTBA_HI, UPPER32(erstptr));
     
    572572{
    573573        xhci_scratchpad_free(hc);
    574         free32(hc->dcbaa);
     574        dma_buffer_free(&hc->dcbaa_dma);
    575575}
    576576
     
    628628        /* Free the device context. */
    629629        hc->dcbaa[dev->slot_id] = 0;
    630         if (dev->dev_ctx) {
    631                 free32(dev->dev_ctx);
    632                 dev->dev_ctx = NULL;
    633         }
     630        dma_buffer_free(&dev->dev_ctx);
    634631
    635632        /* Mark the slot as invalid. */
     
    639636}
    640637
    641 static int create_valid_input_ctx(xhci_input_ctx_t **out_ictx)
    642 {
    643         xhci_input_ctx_t *ictx = malloc32(sizeof(xhci_input_ctx_t));
    644         if (!ictx) {
    645                 return ENOMEM;
    646         }
    647 
     638static int create_valid_input_ctx(dma_buffer_t *dma_buf)
     639{
     640        const int err = dma_buffer_alloc(dma_buf, sizeof(xhci_input_ctx_t));
     641        if (err)
     642                return err;
     643
     644        xhci_input_ctx_t *ictx = dma_buf->virt;
    648645        memset(ictx, 0, sizeof(xhci_input_ctx_t));
    649646
     
    654651        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, 0);
    655652
    656         if (out_ictx) {
    657                 *out_ictx = ictx;
    658         }
    659 
    660653        return EOK;
    661654}
     
    675668
    676669        /* Setup and register device context */
    677         dev->dev_ctx = malloc32(sizeof(xhci_device_ctx_t));
    678         if (!dev->dev_ctx)
     670        if (dma_buffer_alloc(&dev->dev_ctx, sizeof(xhci_device_ctx_t)))
    679671                goto err;
    680         memset(dev->dev_ctx, 0, sizeof(xhci_device_ctx_t));
    681 
    682         hc->dcbaa[dev->slot_id] = addr_to_phys(dev->dev_ctx);
     672        memset(dev->dev_ctx.virt, 0, sizeof(xhci_device_ctx_t));
     673
     674        hc->dcbaa[dev->slot_id] = host2xhci(64, dev->dev_ctx.phys);
    683675
    684676        /* Issue configure endpoint command (sec 4.3.5). */
    685         xhci_input_ctx_t *ictx;
    686         if ((err = create_valid_input_ctx(&ictx))) {
     677        dma_buffer_t ictx_dma_buf;
     678        if ((err = create_valid_input_ctx(&ictx_dma_buf))) {
    687679                goto err_dev_ctx;
    688680        }
     681        xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
    689682
    690683        /* Initialize slot_ctx according to section 4.3.3 point 3. */
     
    705698
    706699        /* Issue Address Device command. */
    707         if ((err = xhci_cmd_sync_inline(hc, ADDRESS_DEVICE, .slot_id = dev->slot_id, .input_ctx = ictx))) {
     700        if ((err = xhci_cmd_sync_inline(hc, ADDRESS_DEVICE, .slot_id = dev->slot_id, .input_ctx = ictx_dma_buf))) {
    708701                goto err_dev_ctx;
    709702        }
    710703
    711         dev->base.address = XHCI_SLOT_DEVICE_ADDRESS(dev->dev_ctx->slot_ctx);
     704        xhci_device_ctx_t *dev_ctx = dev->dev_ctx.virt;
     705        dev->base.address = XHCI_SLOT_DEVICE_ADDRESS(dev_ctx->slot_ctx);
    712706        usb_log_debug2("Obtained USB address: %d.\n", dev->base.address);
    713707
     
    720714
    721715err_dev_ctx:
    722         free32(dev->dev_ctx);
    723716        hc->dcbaa[dev->slot_id] = 0;
     717        dma_buffer_free(&dev->dev_ctx);
    724718err:
    725719        return err;
     
    729723{
    730724        /* Issue configure endpoint command (sec 4.3.5). */
    731         xhci_input_ctx_t *ictx;
    732         const int err = create_valid_input_ctx(&ictx);
     725        dma_buffer_t ictx_dma_buf;
     726        const int err = create_valid_input_ctx(&ictx_dma_buf);
    733727        if (err)
    734728                return err;
     
    736730        // TODO: Set slot context and other flags. (probably forgot a lot of 'em)
    737731
    738         return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = slot_id, .input_ctx = ictx);
     732        return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = slot_id, .input_ctx = ictx_dma_buf);
    739733}
    740734
     
    748742{
    749743        /* Issue configure endpoint command (sec 4.3.5). */
    750         xhci_input_ctx_t *ictx;
    751         const int err = create_valid_input_ctx(&ictx);
     744        dma_buffer_t ictx_dma_buf;
     745        const int err = create_valid_input_ctx(&ictx_dma_buf);
    752746        if (err)
    753747                return err;
    754748
     749        xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
    755750        XHCI_INPUT_CTRL_CTX_ADD_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
    756751        memcpy(&ictx->endpoint_ctx[ep_idx], ep_ctx, sizeof(xhci_ep_ctx_t));
    757752        // TODO: Set slot context and other flags. (probably forgot a lot of 'em)
    758753
    759         return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = slot_id, .input_ctx = ictx);
     754        return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = slot_id, .input_ctx = ictx_dma_buf);
    760755}
    761756
     
    763758{
    764759        /* Issue configure endpoint command (sec 4.3.5). */
    765         xhci_input_ctx_t *ictx;
    766         const int err = create_valid_input_ctx(&ictx);
     760        dma_buffer_t ictx_dma_buf;
     761        const int err = create_valid_input_ctx(&ictx_dma_buf);
    767762        if (err)
    768763                return err;
    769764
     765        xhci_input_ctx_t *ictx = ictx_dma_buf.virt;
    770766        XHCI_INPUT_CTRL_CTX_DROP_SET(ictx->ctrl_ctx, ep_idx + 1); /* Preceded by slot ctx */
    771767        // TODO: Set slot context and other flags. (probably forgot a lot of 'em)
    772768
    773         return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = slot_id, .input_ctx = ictx);
     769        return xhci_cmd_sync_inline(hc, CONFIGURE_ENDPOINT, .slot_id = slot_id, .input_ctx = ictx_dma_buf);
    774770}
    775771
  • uspace/drv/bus/usb/xhci/hc.h

    rcfe4852 rb80c1ab  
    6464        xhci_event_ring_t event_ring;
    6565        uint64_t *dcbaa;
    66         xhci_scratchpad_t *scratchpad;
     66        dma_buffer_t dcbaa_dma;
     67        dma_buffer_t scratchpad_array;
     68        dma_buffer_t *scratchpad_buffers;
    6769
    6870        /* Root hub emulation */
  • uspace/drv/bus/usb/xhci/rh.c

    rcfe4852 rb80c1ab  
    3838#include <usb/request.h>
    3939#include <usb/debug.h>
    40 #include <usb/host/utils/malloc32.h>
    4140#include <usb/host/bus.h>
    4241#include <usb/host/ddf_helpers.h>
     42#include <usb/host/dma_buffer.h>
    4343#include <usb/host/hcd.h>
    4444
     
    305305}
    306306
    307 static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx) {
     307static inline int get_hub_available_bandwidth(xhci_hc_t *hc, xhci_device_t* dev, uint8_t speed, xhci_port_bandwidth_ctx_t *ctx)
     308{
     309        int err = EOK;
     310
    308311        // TODO: find a correct place for this function + API
    309312        // We need speed, because a root hub device has both USB 2 and USB 3 speeds
     
    313316        assert(ctx);
    314317
    315         xhci_port_bandwidth_ctx_t *in_ctx = malloc32(sizeof(xhci_port_bandwidth_ctx_t));
    316         if (!in_ctx) {
    317                 return ENOMEM;
    318         }
    319 
    320318        xhci_cmd_t cmd;
    321319        xhci_cmd_init(&cmd, XHCI_CMD_GET_PORT_BANDWIDTH);
    322320
    323         cmd.bandwidth_ctx = in_ctx;
     321        if ((err = dma_buffer_alloc(&cmd.bandwidth_ctx, sizeof(xhci_port_bandwidth_ctx_t))))
     322                goto end;
     323
    324324        cmd.device_speed = speed;
    325325
    326         int err;
    327         if ((err = xhci_cmd_sync(hc, &cmd))) {
     326        if ((err = xhci_cmd_sync(hc, &cmd)))
    328327                goto end;
    329         }
    330 
    331         memcpy(ctx, in_ctx, sizeof(xhci_port_bandwidth_ctx_t));
     328
     329        memcpy(ctx, cmd.bandwidth_ctx.virt, sizeof(xhci_port_bandwidth_ctx_t));
    332330
    333331end:
    334332        xhci_cmd_fini(&cmd);
    335         return EOK;
     333        return err;
    336334}
    337335
  • uspace/drv/bus/usb/xhci/scratchpad.c

    rcfe4852 rb80c1ab  
    3636#include <errno.h>
    3737#include <usb/debug.h>
    38 #include <usb/host/utils/malloc32.h>
    3938#include "hc.h"
    4039#include "hw_struct/regs.h"
     
    5352int xhci_scratchpad_alloc(xhci_hc_t *hc)
    5453{
    55         unsigned num_bufs, allocated;
    56         xhci_scratchpad_t *bufs;
    57 
    58         num_bufs = xhci_scratchpad_count(hc);
     54        const unsigned num_bufs = xhci_scratchpad_count(hc);
    5955
    6056        if (!num_bufs)
    6157                return EOK;
    6258
    63         bufs = malloc32(sizeof(xhci_scratchpad_t));
    64         if (!bufs)
     59        if (dma_buffer_alloc(&hc->scratchpad_array, num_bufs * sizeof(uint64_t)))
    6560                return ENOMEM;
     61        uint64_t *phys_array = hc->scratchpad_array.virt;
    6662
    67         allocated = 0;
     63        hc->dcbaa[0] = host2xhci(64, hc->scratchpad_array.phys);
    6864
    69         uint64_t *phys_array = malloc32(num_bufs * sizeof(uint64_t));
    70         if (phys_array == NULL)
    71                 goto err_phys_array;
    72 
    73         uint64_t *virt_array = malloc32(num_bufs * sizeof(uint64_t));
    74         if (virt_array == NULL)
    75                 goto err_virt_array;
     65        hc->scratchpad_buffers = calloc(num_bufs, sizeof(dma_buffer_t));
     66        if (!hc->scratchpad_buffers)
     67                goto err_dma;
    7668
    7769        for (unsigned i = 0; i < num_bufs; ++i) {
    78                 void *buf = malloc32(PAGE_SIZE);
    79                 phys_array[i] = host2xhci(64, (uint64_t) addr_to_phys(buf));
    80                 virt_array[i] = (uint64_t) buf;
     70                dma_buffer_t *cur = &hc->scratchpad_buffers[i];
     71                if (dma_buffer_alloc(cur, PAGE_SIZE))
     72                        goto err_buffers;
    8173
    82                 if (buf != NULL)
    83                         ++allocated;
    84                 else
    85                         goto err_page_alloc;
    86 
    87                 memset(buf, 0, PAGE_SIZE);
     74                memset(cur->virt, 0, PAGE_SIZE);
     75                phys_array[i] = host2xhci(64, cur->phys);
    8876        }
    8977
    90         bufs->phys_ptr = host2xhci(64, (uint64_t) addr_to_phys(phys_array));
    91         bufs->virt_ptr = (uint64_t) virt_array;
    92         bufs->phys_bck = (uint64_t) phys_array;
    93 
    94         hc->dcbaa[0] = bufs->phys_ptr;
    95         hc->scratchpad = bufs;
    9678
    9779        usb_log_debug2("Allocated %d scratchpad buffers.", num_bufs);
     
    9981        return EOK;
    10082
    101 err_page_alloc:
    102         for (unsigned i = 0; i < allocated; ++i)
    103                 free32((void *) virt_array[i]);
    104         free32(virt_array);
    105 
    106 err_virt_array:
    107         free32(phys_array);
    108 
    109 err_phys_array:
    110         free32(bufs);
    111 
     83err_buffers:
     84        for (unsigned i = 0; i < num_bufs; ++i)
     85                dma_buffer_free(&hc->scratchpad_buffers[i]);
     86        free(hc->scratchpad_buffers);
     87err_dma:
     88        hc->dcbaa[0] = 0;
     89        dma_buffer_free(&hc->scratchpad_array);
    11290        return ENOMEM;
    11391}
     
    11593void xhci_scratchpad_free(xhci_hc_t *hc)
    11694{
    117         unsigned num_bufs;
    118         xhci_scratchpad_t *scratchpad;
    119         uint64_t *virt_array;
     95        const unsigned num_bufs = xhci_scratchpad_count(hc);
    12096
    121         num_bufs = xhci_scratchpad_count(hc);
    12297        if (!num_bufs)
    12398                return;
    12499
    125         scratchpad =  hc->scratchpad;
    126         virt_array = (uint64_t *) scratchpad->virt_ptr;
    127 
     100        hc->dcbaa[0] = 0;
     101        dma_buffer_free(&hc->scratchpad_array);
    128102        for (unsigned i = 0; i < num_bufs; ++i)
    129                 free32((void *) virt_array[i]);
    130         free32((void *) scratchpad->virt_ptr);
    131         free32((void *) scratchpad->phys_bck);
    132 
    133         hc->dcbaa[0] = 0;
    134 
     103                dma_buffer_free(&hc->scratchpad_buffers[i]);
     104        free(hc->scratchpad_buffers);
    135105        return;
    136106}
  • uspace/drv/bus/usb/xhci/scratchpad.h

    rcfe4852 rb80c1ab  
    4141#define XHCI_SCRATCHPAD_H
    4242
     43#include <usb/host/dma_buffer.h>
     44
    4345typedef struct xhci_hc xhci_hc_t;
    44 
    45 typedef struct xhci_scratchpad {
    46         /* Pointers to scratchpad buffers used by the xHC. */
    47         uint64_t phys_ptr;
    48         /* Pointers to scratchpad buffers used for deallocation. */
    49         uint64_t virt_ptr;
    50         /* Pointers to the scratchpad array used for deallocation. */
    51         uint64_t phys_bck;
    52 } xhci_scratchpad_t;
    5346
    5447int xhci_scratchpad_alloc(xhci_hc_t *);
  • uspace/drv/bus/usb/xhci/transfers.c

    rcfe4852 rb80c1ab  
    3434 */
    3535
    36 #include <usb/host/utils/malloc32.h>
    3736#include <usb/debug.h>
    3837#include <usb/request.h>
     
    113112        assert(transfer);
    114113
    115         if (transfer->hc_buffer)
    116                 free32(transfer->hc_buffer);
     114        dma_buffer_free(&transfer->hc_buffer);
    117115}
    118116
     
    156154                xhci_trb_clean(trb_data);
    157155
    158                 trb_data->parameter = addr_to_phys(transfer->hc_buffer);
     156                trb_data->parameter = host2xhci(64, transfer->hc_buffer.phys);
    159157
    160158                // data size (sent for OUT, or buffer size)
     
    196194        xhci_trb_t trb;
    197195        xhci_trb_clean(&trb);
    198         trb.parameter = addr_to_phys(transfer->hc_buffer);
     196        trb.parameter = host2xhci(64, transfer->hc_buffer.phys);
    199197
    200198        // data size (sent for OUT, or buffer size)
     
    217215        xhci_trb_t trb;
    218216        xhci_trb_clean(&trb);
    219         trb.parameter = addr_to_phys(transfer->hc_buffer);
     217        trb.parameter = host2xhci(64, transfer->hc_buffer.phys);
    220218
    221219        // data size (sent for OUT, or buffer size)
     
    282280                assert(batch->buffer);
    283281                assert(batch->transfered_size <= batch->buffer_size);
    284                 memcpy(batch->buffer, transfer->hc_buffer, batch->transfered_size);
     282                memcpy(batch->buffer, transfer->hc_buffer.virt, batch->transfered_size);
    285283        }
    286284
     
    324322
    325323        if (batch->buffer_size > 0) {
    326                 transfer->hc_buffer = malloc32(batch->buffer_size);
    327                 if (!transfer->hc_buffer)
     324                if (dma_buffer_alloc(&transfer->hc_buffer, batch->buffer_size))
    328325                        return ENOMEM;
    329326        }
     
    331328        if (batch->dir != USB_DIRECTION_IN) {
    332329                // Sending stuff from host to device, we need to copy the actual data.
    333                 memcpy(transfer->hc_buffer, batch->buffer, batch->buffer_size);
     330                memcpy(transfer->hc_buffer.virt, batch->buffer, batch->buffer_size);
    334331        }
    335332
  • uspace/drv/bus/usb/xhci/transfers.h

    rcfe4852 rb80c1ab  
    5050        uint8_t direction;
    5151
    52         void* hc_buffer;                    /* Virtual address of the buffer start. */
    53         uintptr_t hc_buffer_phys;
     52        dma_buffer_t hc_buffer;
    5453
    5554        uintptr_t interrupt_trb_phys;
  • uspace/drv/bus/usb/xhci/trb_ring.c

    rcfe4852 rb80c1ab  
    3333#include <align.h>
    3434#include <usb/debug.h>
    35 #include <usb/host/utils/malloc32.h>
    3635#include "hw_struct/trb.h"
    3736#include "trb_ring.h"
     
    6867 * to DMAMEM_4GiB.
    6968 */
    70 static int trb_segment_allocate(trb_segment_t **segment)
    71 {
    72         uintptr_t phys;
    73         int err;
    74 
    75         *segment = AS_AREA_ANY;
    76         err = dmamem_map_anonymous(PAGE_SIZE,
    77             DMAMEM_4GiB, AS_AREA_READ | AS_AREA_WRITE, 0, &phys,
    78             (void *) segment);
    79 
    80         if (err == EOK) {
    81                 memset(*segment, 0, PAGE_SIZE);
    82                 (*segment)->phys = phys;
    83 
    84                 usb_log_debug2("Allocated new ring segment.");
    85         }
    86 
    87         return err;
     69static int trb_segment_alloc(trb_segment_t **segment)
     70{
     71        dma_buffer_t dbuf;
     72
     73        const int err = dma_buffer_alloc(&dbuf, PAGE_SIZE);
     74        if (err)
     75                return err;
     76
     77        *segment = dbuf.virt;
     78        memset(*segment, 0, PAGE_SIZE);
     79        (*segment)->phys = dbuf.phys;
     80        usb_log_debug2("Allocated new ring segment.");
     81        return EOK;
     82}
     83
     84static void trb_segment_free(trb_segment_t *segment)
     85{
     86        dma_buffer_t dbuf = { .virt = segment, .phys = segment->phys };
     87        dma_buffer_free(&dbuf);
    8888}
    8989
     
    9999        list_initialize(&ring->segments);
    100100
    101         if ((err = trb_segment_allocate(&segment)) != EOK)
     101        if ((err = trb_segment_alloc(&segment)) != EOK)
    102102                return err;
    103103
     
    127127        list_foreach_safe(ring->segments, cur, next) {
    128128                trb_segment_t *segment = list_get_instance(cur, trb_segment_t, segments_link);
    129                 dmamem_unmap_anonymous(segment);
     129                trb_segment_free(segment);
    130130        }
    131131}
     
    264264        list_initialize(&ring->segments);
    265265
    266         if ((err = trb_segment_allocate(&segment)) != EOK)
     266        if ((err = trb_segment_alloc(&segment)) != EOK)
    267267                return err;
    268268
     
    274274        ring->dequeue_ptr = segment->phys;
    275275
    276         ring->erst = malloc32(PAGE_SIZE);
    277         if (ring->erst == NULL)
     276        if (dma_buffer_alloc(&ring->erst, PAGE_SIZE))
    278277                return ENOMEM;
    279         memset(ring->erst, 0, PAGE_SIZE);
    280 
    281         xhci_fill_erst_entry(&ring->erst[0], segment->phys, SEGMENT_TRB_COUNT);
     278        xhci_erst_entry_t *erst = ring->erst.virt;
     279
     280        memset(erst, 0, PAGE_SIZE);
     281        xhci_fill_erst_entry(&erst[0], segment->phys, SEGMENT_TRB_COUNT);
    282282
    283283        ring->ccs = 1;
     
    297297        }
    298298
    299         if (ring->erst)
    300                 free32(ring->erst);
     299        dma_buffer_free(&ring->erst);
    301300}
    302301
  • uspace/drv/bus/usb/xhci/trb_ring.h

    rcfe4852 rb80c1ab  
    4646#include <fibril_synch.h>
    4747#include <libarch/config.h>
     48#include <usb/host/dma_buffer.h>
    4849
    4950typedef struct trb_segment trb_segment_t;
     
    104105        uintptr_t dequeue_ptr;          /* Physical address of the ERDP to be reported to the HC */
    105106
    106         xhci_erst_entry_t *erst;        /* ERST given to the HC */
     107        dma_buffer_t erst;              /* ERST given to the HC */
    107108
    108109        bool ccs;                       /* Consumer Cycle State: section 4.9.2 */
Note: See TracChangeset for help on using the changeset viewer.