| 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 void hw_res_parse_add_io_range(hw_res_list_parsed_t *out,
|
|---|
| 83 | const hw_resource_t *res, int flags)
|
|---|
| 84 | {
|
|---|
| 85 | assert(res && (res->type == IO_RANGE));
|
|---|
| 86 |
|
|---|
| 87 | uint64_t address = res->res.io_range.address;
|
|---|
| 88 | endianness_t endianness = res->res.io_range.endianness;
|
|---|
| 89 | size_t size = res->res.io_range.size;
|
|---|
| 90 |
|
|---|
| 91 | if ((size == 0) && (!(flags & HW_RES_KEEP_ZERO_AREA)))
|
|---|
| 92 | return;
|
|---|
| 93 |
|
|---|
| 94 | int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
|
|---|
| 95 | size_t count = out->io_ranges.count;
|
|---|
| 96 |
|
|---|
| 97 | if (!keep_duplicit) {
|
|---|
| 98 | for (size_t i = 0; i < count; i++) {
|
|---|
| 99 | uint64_t s_address = out->io_ranges.ranges[i].address;
|
|---|
| 100 | size_t s_size = out->io_ranges.ranges[i].size;
|
|---|
| 101 |
|
|---|
| 102 | if ((address == s_address) && (size == s_size))
|
|---|
| 103 | return;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | out->io_ranges.ranges[count].address = address;
|
|---|
| 108 | out->io_ranges.ranges[count].endianness = endianness;
|
|---|
| 109 | out->io_ranges.ranges[count].size = size;
|
|---|
| 110 | out->io_ranges.count++;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | static void hw_res_parse_add_mem_range(hw_res_list_parsed_t *out,
|
|---|
| 114 | const hw_resource_t *res, int flags)
|
|---|
| 115 | {
|
|---|
| 116 | assert(res && (res->type == MEM_RANGE));
|
|---|
| 117 |
|
|---|
| 118 | uint64_t address = res->res.mem_range.address;
|
|---|
| 119 | endianness_t endianness = res->res.mem_range.endianness;
|
|---|
| 120 | size_t size = res->res.mem_range.size;
|
|---|
| 121 |
|
|---|
| 122 | if ((size == 0) && (!(flags & HW_RES_KEEP_ZERO_AREA)))
|
|---|
| 123 | return;
|
|---|
| 124 |
|
|---|
| 125 | int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
|
|---|
| 126 | size_t count = out->mem_ranges.count;
|
|---|
| 127 |
|
|---|
| 128 | if (!keep_duplicit) {
|
|---|
| 129 | for (size_t i = 0; i < count; ++i) {
|
|---|
| 130 | uint64_t s_address = out->mem_ranges.ranges[i].address;
|
|---|
| 131 | size_t s_size = out->mem_ranges.ranges[i].size;
|
|---|
| 132 |
|
|---|
| 133 | if ((address == s_address) && (size == s_size))
|
|---|
| 134 | return;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | out->mem_ranges.ranges[count].address = address;
|
|---|
| 139 | out->mem_ranges.ranges[count].endianness = endianness;
|
|---|
| 140 | out->mem_ranges.ranges[count].size = size;
|
|---|
| 141 | out->mem_ranges.count++;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /** Parse list of hardware resources
|
|---|
| 145 | *
|
|---|
| 146 | * @param hw_resources Original structure resource
|
|---|
| 147 | * @param[out] out Output parsed resources
|
|---|
| 148 | * @param flags Flags of the parsing.
|
|---|
| 149 | * HW_RES_KEEP_ZERO_AREA for keeping
|
|---|
| 150 | * zero-size areas, HW_RES_KEEP_DUPLICITIES
|
|---|
| 151 | * for keep duplicit areas
|
|---|
| 152 | *
|
|---|
| 153 | * @return EOK if succeed, error code otherwise.
|
|---|
| 154 | *
|
|---|
| 155 | */
|
|---|
| 156 | int hw_res_list_parse(const hw_resource_list_t *hw_resources,
|
|---|
| 157 | hw_res_list_parsed_t *out, int flags)
|
|---|
| 158 | {
|
|---|
| 159 | if ((!hw_resources) || (!out))
|
|---|
| 160 | return EINVAL;
|
|---|
| 161 |
|
|---|
| 162 | size_t res_count = hw_resources->count;
|
|---|
| 163 | hw_res_list_parsed_clean(out);
|
|---|
| 164 |
|
|---|
| 165 | out->irqs.irqs = calloc(res_count, sizeof(int));
|
|---|
| 166 | out->dma_channels.channels = calloc(res_count, sizeof(int));
|
|---|
| 167 | out->io_ranges.ranges = calloc(res_count, sizeof(io_range_t));
|
|---|
| 168 | out->mem_ranges.ranges = calloc(res_count, sizeof(mem_range_t));
|
|---|
| 169 | if (!out->irqs.irqs || !out->dma_channels.channels ||
|
|---|
| 170 | !out->io_ranges.ranges || !out->mem_ranges.ranges) {
|
|---|
| 171 | hw_res_list_parsed_clean(out);
|
|---|
| 172 | return ENOMEM;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | for (size_t i = 0; i < res_count; ++i) {
|
|---|
| 176 | const hw_resource_t *resource = &(hw_resources->resources[i]);
|
|---|
| 177 |
|
|---|
| 178 | switch (resource->type) {
|
|---|
| 179 | case INTERRUPT:
|
|---|
| 180 | hw_res_parse_add_irq(out, resource, flags);
|
|---|
| 181 | break;
|
|---|
| 182 | case IO_RANGE:
|
|---|
| 183 | hw_res_parse_add_io_range(out, resource, flags);
|
|---|
| 184 | break;
|
|---|
| 185 | case MEM_RANGE:
|
|---|
| 186 | hw_res_parse_add_mem_range(out, resource, flags);
|
|---|
| 187 | break;
|
|---|
| 188 | case DMA_CHANNEL_8:
|
|---|
| 189 | case DMA_CHANNEL_16:
|
|---|
| 190 | hw_res_parse_add_dma_channel(out, resource, flags);
|
|---|
| 191 | break;
|
|---|
| 192 | default:
|
|---|
| 193 | hw_res_list_parsed_clean(out);
|
|---|
| 194 | return EINVAL;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | return EOK;
|
|---|
| 199 | };
|
|---|
| 200 |
|
|---|
| 201 | /** Get hw_resources from the parent device.
|
|---|
| 202 | *
|
|---|
| 203 | * The output must be inited, will be cleared
|
|---|
| 204 | *
|
|---|
| 205 | * @see get_hw_resources
|
|---|
| 206 | * @see hw_resources_parse
|
|---|
| 207 | *
|
|---|
| 208 | * @param sess Session to the parent device
|
|---|
| 209 | * @param hw_resources_parsed Output list
|
|---|
| 210 | * @param flags Parsing flags
|
|---|
| 211 | *
|
|---|
| 212 | * @return EOK if succeed, error code otherwise
|
|---|
| 213 | *
|
|---|
| 214 | */
|
|---|
| 215 | int hw_res_get_list_parsed(async_sess_t *sess,
|
|---|
| 216 | hw_res_list_parsed_t *hw_res_parsed, int flags)
|
|---|
| 217 | {
|
|---|
| 218 | if (!hw_res_parsed)
|
|---|
| 219 | return EBADMEM;
|
|---|
| 220 |
|
|---|
| 221 | hw_resource_list_t hw_resources;
|
|---|
| 222 | hw_res_list_parsed_clean(hw_res_parsed);
|
|---|
| 223 | memset(&hw_resources, 0, sizeof(hw_resource_list_t));
|
|---|
| 224 |
|
|---|
| 225 | int rc = hw_res_get_resource_list(sess, &hw_resources);
|
|---|
| 226 | if (rc != EOK)
|
|---|
| 227 | return rc;
|
|---|
| 228 |
|
|---|
| 229 | rc = hw_res_list_parse(&hw_resources, hw_res_parsed, flags);
|
|---|
| 230 | hw_res_clean_resource_list(&hw_resources);
|
|---|
| 231 |
|
|---|
| 232 | return rc;
|
|---|
| 233 | };
|
|---|
| 234 |
|
|---|
| 235 | /** @}
|
|---|
| 236 | */
|
|---|