Changeset b80c1ab in mainline for uspace/drv/bus/usb/xhci/hc.c


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.