| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Michalec
|
|---|
| 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 libc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <device/hw_res_parsed.h>
|
|---|
| 36 | #include <malloc.h>
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 |
|
|---|
| 40 | static void hw_res_parse_add_dma_channel(hw_res_list_parsed_t *out,
|
|---|
| 41 | const hw_resource_t *res, int flags)
|
|---|
| 42 | {
|
|---|
| 43 | assert(res);
|
|---|
| 44 | assert((res->type == DMA_CHANNEL_8) || (res->type == DMA_CHANNEL_16));
|
|---|
| 45 |
|
|---|
| 46 | const unsigned channel = (res->type == DMA_CHANNEL_8) ?
|
|---|
| 47 | res->res.dma_channel.dma8 : res->res.dma_channel.dma16;
|
|---|
| 48 | const size_t count = out->dma_channels.count;
|
|---|
| 49 | const int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
|
|---|
| 50 |
|
|---|
| 51 | if (!keep_duplicit) {
|
|---|
| 52 | for (size_t i = 0; i < count; ++i) {
|
|---|
| 53 | if (out->dma_channels.channels[i] == channel)
|
|---|
| 54 | return;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | out->dma_channels.channels[count] = channel;
|
|---|
| 59 | ++out->dma_channels.count;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static void hw_res_parse_add_irq(hw_res_list_parsed_t *out,
|
|---|
| 63 | const hw_resource_t *res, int flags)
|
|---|
| 64 | {
|
|---|
| 65 | assert(res && (res->type == INTERRUPT));
|
|---|
| 66 |
|
|---|
| 67 | int irq = res->res.interrupt.irq;
|
|---|
| 68 | size_t count = out->irqs.count;
|
|---|
| 69 | int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
|
|---|
| 70 |
|
|---|
| 71 | if (!keep_duplicit) {
|
|---|
| 72 | for (size_t i = 0; i < count; i++) {
|
|---|
| 73 | if (out->irqs.irqs[i] == irq)
|
|---|
| 74 | return;
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | out->irqs.irqs[count] = irq;
|
|---|
| 79 | out->irqs.count++;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static uint64_t absolutize(uint64_t addr, bool relative, uint64_t base)
|
|---|
| 83 | {
|
|---|
| 84 | if (!relative)
|
|---|
| 85 | return addr;
|
|---|
| 86 | else
|
|---|
| 87 | return addr + base;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | static uint64_t relativize(uint64_t addr, bool relative, uint64_t base)
|
|---|
| 91 | {
|
|---|
| 92 | if (relative)
|
|---|
| 93 | return addr;
|
|---|
| 94 | else
|
|---|
| 95 | return addr - base;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | static void hw_res_parse_add_io_range(hw_res_list_parsed_t *out,
|
|---|
| 99 | const pio_window_t *win, const hw_resource_t *res, int flags)
|
|---|
| 100 | {
|
|---|
| 101 | endianness_t endianness;
|
|---|
| 102 | uint64_t absolute;
|
|---|
| 103 | uint64_t relative;
|
|---|
| 104 | size_t size;
|
|---|
| 105 |
|
|---|
| 106 | assert(res && (res->type == IO_RANGE));
|
|---|
| 107 |
|
|---|
| 108 | absolute = absolutize(res->res.io_range.address,
|
|---|
| 109 | res->res.io_range.relative, win->io.base);
|
|---|
| 110 | relative = relativize(res->res.io_range.address,
|
|---|
| 111 | res->res.io_range.relative, win->io.base);
|
|---|
| 112 | size = res->res.io_range.size;
|
|---|
| 113 | endianness = res->res.io_range.endianness;
|
|---|
| 114 |
|
|---|
| 115 | if ((size == 0) && (!(flags & HW_RES_KEEP_ZERO_AREA)))
|
|---|
| 116 | return;
|
|---|
| 117 |
|
|---|
| 118 | int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
|
|---|
| 119 | size_t count = out->io_ranges.count;
|
|---|
| 120 |
|
|---|
| 121 | if (!keep_duplicit) {
|
|---|
| 122 | for (size_t i = 0; i < count; i++) {
|
|---|
| 123 | uint64_t s_address;
|
|---|
| 124 | size_t s_size;
|
|---|
| 125 |
|
|---|
| 126 | s_address = RNGABS(out->io_ranges.ranges[i]);
|
|---|
| 127 | s_size = RNGSZ(out->io_ranges.ranges[i]);
|
|---|
| 128 |
|
|---|
| 129 | if ((absolute == s_address) && (size == s_size))
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | RNGABS(out->io_ranges.ranges[count]) = absolute;
|
|---|
| 135 | RNGREL(out->io_ranges.ranges[count]) = relative;
|
|---|
| 136 | RNGSZ(out->io_ranges.ranges[count]) = size;
|
|---|
| 137 | out->io_ranges.ranges[count].endianness = endianness;
|
|---|
| 138 | out->io_ranges.count++;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | static void hw_res_parse_add_mem_range(hw_res_list_parsed_t *out,
|
|---|
| 142 | const pio_window_t *win, const hw_resource_t *res, int flags)
|
|---|
| 143 | {
|
|---|
| 144 | endianness_t endianness;
|
|---|
| 145 | uint64_t absolute;
|
|---|
| 146 | uint64_t relative;
|
|---|
| 147 | size_t size;
|
|---|
| 148 |
|
|---|
| 149 | assert(res && (res->type == MEM_RANGE));
|
|---|
| 150 |
|
|---|
| 151 | absolute = absolutize(res->res.mem_range.address,
|
|---|
| 152 | res->res.mem_range.relative, win->mem.base);
|
|---|
| 153 | relative = relativize(res->res.mem_range.address,
|
|---|
| 154 | res->res.mem_range.relative, win->mem.base);
|
|---|
| 155 | size = res->res.mem_range.size;
|
|---|
| 156 | endianness = res->res.mem_range.endianness;
|
|---|
| 157 |
|
|---|
| 158 | if ((size == 0) && (!(flags & HW_RES_KEEP_ZERO_AREA)))
|
|---|
| 159 | return;
|
|---|
| 160 |
|
|---|
| 161 | int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
|
|---|
| 162 | size_t count = out->mem_ranges.count;
|
|---|
| 163 |
|
|---|
| 164 | if (!keep_duplicit) {
|
|---|
| 165 | for (size_t i = 0; i < count; ++i) {
|
|---|
| 166 | uint64_t s_address;
|
|---|
| 167 | size_t s_size;
|
|---|
| 168 |
|
|---|
| 169 | s_address = RNGABS(out->mem_ranges.ranges[i]);;
|
|---|
| 170 | s_size = RNGSZ(out->mem_ranges.ranges[i]);
|
|---|
| 171 |
|
|---|
| 172 | if ((absolute == s_address) && (size == s_size))
|
|---|
| 173 | return;
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | RNGABS(out->mem_ranges.ranges[count]) = absolute;
|
|---|
| 178 | RNGREL(out->mem_ranges.ranges[count]) = relative;
|
|---|
| 179 | RNGSZ(out->mem_ranges.ranges[count]) = size;
|
|---|
| 180 | out->mem_ranges.ranges[count].endianness = endianness;
|
|---|
| 181 | out->mem_ranges.count++;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /** Parse list of hardware resources
|
|---|
| 185 | *
|
|---|
| 186 | * @param win PIO window.
|
|---|
| 187 | * @param res Original structure resource.
|
|---|
| 188 | * @param[out] out Output parsed resources.
|
|---|
| 189 | * @param flags Flags of the parsing:
|
|---|
| 190 | * HW_RES_KEEP_ZERO_AREA for keeping zero-size areas,
|
|---|
| 191 | * HW_RES_KEEP_DUPLICITIES to keep duplicit areas.
|
|---|
| 192 | *
|
|---|
| 193 | * @return EOK if succeed, error code otherwise.
|
|---|
| 194 | *
|
|---|
| 195 | */
|
|---|
| 196 | int hw_res_list_parse(const pio_window_t *win,
|
|---|
| 197 | const hw_resource_list_t *res, hw_res_list_parsed_t *out, int flags)
|
|---|
| 198 | {
|
|---|
| 199 | if (!res || !out)
|
|---|
| 200 | return EINVAL;
|
|---|
| 201 |
|
|---|
| 202 | size_t res_count = res->count;
|
|---|
| 203 | hw_res_list_parsed_clean(out);
|
|---|
| 204 |
|
|---|
| 205 | out->irqs.irqs = calloc(res_count, sizeof(int));
|
|---|
| 206 | out->dma_channels.channels = calloc(res_count, sizeof(int));
|
|---|
| 207 | out->io_ranges.ranges = calloc(res_count, sizeof(io_range_t));
|
|---|
| 208 | out->mem_ranges.ranges = calloc(res_count, sizeof(mem_range_t));
|
|---|
| 209 | if (!out->irqs.irqs || !out->dma_channels.channels ||
|
|---|
| 210 | !out->io_ranges.ranges || !out->mem_ranges.ranges) {
|
|---|
| 211 | hw_res_list_parsed_clean(out);
|
|---|
| 212 | return ENOMEM;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | for (size_t i = 0; i < res_count; ++i) {
|
|---|
| 216 | const hw_resource_t *resource = &res->resources[i];
|
|---|
| 217 |
|
|---|
| 218 | switch (resource->type) {
|
|---|
| 219 | case INTERRUPT:
|
|---|
| 220 | hw_res_parse_add_irq(out, resource, flags);
|
|---|
| 221 | break;
|
|---|
| 222 | case IO_RANGE:
|
|---|
| 223 | hw_res_parse_add_io_range(out, win, resource, flags);
|
|---|
| 224 | break;
|
|---|
| 225 | case MEM_RANGE:
|
|---|
| 226 | hw_res_parse_add_mem_range(out, win, resource, flags);
|
|---|
| 227 | break;
|
|---|
| 228 | case DMA_CHANNEL_8:
|
|---|
| 229 | case DMA_CHANNEL_16:
|
|---|
| 230 | hw_res_parse_add_dma_channel(out, resource, flags);
|
|---|
| 231 | break;
|
|---|
| 232 | default:
|
|---|
| 233 | hw_res_list_parsed_clean(out);
|
|---|
| 234 | return EINVAL;
|
|---|
| 235 | }
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | return EOK;
|
|---|
| 239 | };
|
|---|
| 240 |
|
|---|
| 241 | /** Get hw_resources from the parent device.
|
|---|
| 242 | *
|
|---|
| 243 | * The output must be inited, will be cleared
|
|---|
| 244 | *
|
|---|
| 245 | * @see get_hw_resources
|
|---|
| 246 | * @see hw_resources_parse
|
|---|
| 247 | *
|
|---|
| 248 | * @param sess Session to the parent device
|
|---|
| 249 | * @param hw_resources_parsed Output list
|
|---|
| 250 | * @param flags Parsing flags
|
|---|
| 251 | *
|
|---|
| 252 | * @return EOK if succeed, error code otherwise
|
|---|
| 253 | *
|
|---|
| 254 | */
|
|---|
| 255 | int hw_res_get_list_parsed(async_sess_t *sess,
|
|---|
| 256 | hw_res_list_parsed_t *hw_res_parsed, int flags)
|
|---|
| 257 | {
|
|---|
| 258 | pio_window_t pio_window;
|
|---|
| 259 | int rc;
|
|---|
| 260 |
|
|---|
| 261 | if (!hw_res_parsed)
|
|---|
| 262 | return EBADMEM;
|
|---|
| 263 |
|
|---|
| 264 | hw_resource_list_t hw_resources;
|
|---|
| 265 | hw_res_list_parsed_clean(hw_res_parsed);
|
|---|
| 266 | memset(&hw_resources, 0, sizeof(hw_resource_list_t));
|
|---|
| 267 |
|
|---|
| 268 | rc = pio_window_get(sess, &pio_window);
|
|---|
| 269 | if (rc != EOK)
|
|---|
| 270 | return rc;
|
|---|
| 271 |
|
|---|
| 272 | rc = hw_res_get_resource_list(sess, &hw_resources);
|
|---|
| 273 | if (rc != EOK)
|
|---|
| 274 | return rc;
|
|---|
| 275 |
|
|---|
| 276 | rc = hw_res_list_parse(&pio_window, &hw_resources, hw_res_parsed,
|
|---|
| 277 | flags);
|
|---|
| 278 | hw_res_clean_resource_list(&hw_resources);
|
|---|
| 279 |
|
|---|
| 280 | return rc;
|
|---|
| 281 | };
|
|---|
| 282 |
|
|---|
| 283 | /** @}
|
|---|
| 284 | */
|
|---|