source: mainline/uspace/drv/bus/usb/xhci/scratchpad.c@ 834d354

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 834d354 was 834d354, checked in by Petr Manek <petr.manek@…>, 8 years ago

Corrected cstyle.

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 * Copyright (c) 2017 Jaroslav Jindrak
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 * Scratchpad buffer array bookkeeping.
34 */
35
36#include <errno.h>
37#include <usb/debug.h>
38#include <usb/host/utils/malloc32.h>
39#include "hc.h"
40#include "hw_struct/regs.h"
41#include "scratchpad.h"
42
43static inline unsigned xhci_scratchpad_count(xhci_hc_t *hc)
44{
45 unsigned lo, hi;
46
47 lo = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SPBUF_LO);
48 hi = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SPBUF_HI);
49
50 return (hi << 5) | lo;
51}
52
53int xhci_scratchpad_alloc(xhci_hc_t *hc)
54{
55 unsigned num_bufs, allocated;
56 xhci_scratchpad_t *bufs;
57
58 num_bufs = xhci_scratchpad_count(hc);
59
60 if (!num_bufs)
61 return EOK;
62
63 bufs = malloc32(sizeof(xhci_scratchpad_t));
64 if (!bufs)
65 return ENOMEM;
66
67 allocated = 0;
68
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;
76
77 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;
81
82 if (buf != NULL)
83 ++allocated;
84 else
85 goto err_page_alloc;
86
87 memset(buf, 0, PAGE_SIZE);
88 }
89
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;
96
97 usb_log_debug2("Allocated %d scratchpad buffers.", num_bufs);
98
99 return EOK;
100
101err_page_alloc:
102 for (unsigned i = 0; i < allocated; ++i)
103 free32((void *) virt_array[i]);
104 free32(virt_array);
105
106err_virt_array:
107 free32(phys_array);
108
109err_phys_array:
110 free32(bufs);
111
112 return ENOMEM;
113}
114
115void xhci_scratchpad_free(xhci_hc_t *hc)
116{
117 unsigned num_bufs;
118 xhci_scratchpad_t *scratchpad;
119 uint64_t *virt_array;
120
121 num_bufs = xhci_scratchpad_count(hc);
122 if (!num_bufs)
123 return;
124
125 scratchpad = hc->scratchpad;
126 virt_array = (uint64_t *) scratchpad->virt_ptr;
127
128 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 hc->dcbaa_virt[0] = NULL;
135 return;
136}
137
138/**
139 * @}
140 */
Note: See TracBrowser for help on using the repository browser.