source: mainline/uspace/lib/c/generic/device/hw_res_parsed.c

Last change on this file was 84239b1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

And there was much fixing.

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[00d7e1b]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 <assert.h>
37#include <errno.h>
[38d150e]38#include <stdlib.h>
[00d7e1b]39
[46d9d13]40static 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));
[a35b458]45
[722912e]46 const unsigned channel = (res->type == DMA_CHANNEL_8) ?
[46d9d13]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;
[a35b458]50
[46d9d13]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 }
[a35b458]57
[46d9d13]58 out->dma_channels.channels[count] = channel;
59 ++out->dma_channels.count;
60}
61
62static void hw_res_parse_add_irq(hw_res_list_parsed_t *out,
63 const hw_resource_t *res, int flags)
[00d7e1b]64{
65 assert(res && (res->type == INTERRUPT));
[a35b458]66
[00d7e1b]67 int irq = res->res.interrupt.irq;
68 size_t count = out->irqs.count;
69 int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
[a35b458]70
[00d7e1b]71 if (!keep_duplicit) {
72 for (size_t i = 0; i < count; i++) {
73 if (out->irqs.irqs[i] == irq)
74 return;
75 }
76 }
[a35b458]77
[00d7e1b]78 out->irqs.irqs[count] = irq;
79 out->irqs.count++;
80}
81
[8049b79]82static 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
90static 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
[00d7e1b]98static void hw_res_parse_add_io_range(hw_res_list_parsed_t *out,
[8049b79]99 const pio_window_t *win, const hw_resource_t *res, int flags)
[00d7e1b]100{
[8049b79]101 endianness_t endianness;
102 uint64_t absolute;
103 uint64_t relative;
104 size_t size;
105
[00d7e1b]106 assert(res && (res->type == IO_RANGE));
[a35b458]107
[8049b79]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;
[a35b458]114
[00d7e1b]115 if ((size == 0) && (!(flags & HW_RES_KEEP_ZERO_AREA)))
116 return;
[a35b458]117
[00d7e1b]118 int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
119 size_t count = out->io_ranges.count;
[a35b458]120
[00d7e1b]121 if (!keep_duplicit) {
122 for (size_t i = 0; i < count; i++) {
[8049b79]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]);
[a35b458]128
[8049b79]129 if ((absolute == s_address) && (size == s_size))
[00d7e1b]130 return;
131 }
132 }
[a35b458]133
[8049b79]134 RNGABS(out->io_ranges.ranges[count]) = absolute;
135 RNGREL(out->io_ranges.ranges[count]) = relative;
136 RNGSZ(out->io_ranges.ranges[count]) = size;
[00d7e1b]137 out->io_ranges.ranges[count].endianness = endianness;
138 out->io_ranges.count++;
139}
140
141static void hw_res_parse_add_mem_range(hw_res_list_parsed_t *out,
[8049b79]142 const pio_window_t *win, const hw_resource_t *res, int flags)
[00d7e1b]143{
[8049b79]144 endianness_t endianness;
145 uint64_t absolute;
146 uint64_t relative;
147 size_t size;
[a35b458]148
[00d7e1b]149 assert(res && (res->type == MEM_RANGE));
[a35b458]150
[8049b79]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;
[a35b458]157
[00d7e1b]158 if ((size == 0) && (!(flags & HW_RES_KEEP_ZERO_AREA)))
159 return;
[a35b458]160
[00d7e1b]161 int keep_duplicit = flags & HW_RES_KEEP_DUPLICIT;
162 size_t count = out->mem_ranges.count;
[a35b458]163
[00d7e1b]164 if (!keep_duplicit) {
165 for (size_t i = 0; i < count; ++i) {
[8049b79]166 uint64_t s_address;
167 size_t s_size;
168
[84239b1]169 s_address = RNGABS(out->mem_ranges.ranges[i]);
[8049b79]170 s_size = RNGSZ(out->mem_ranges.ranges[i]);
[a35b458]171
[8049b79]172 if ((absolute == s_address) && (size == s_size))
[00d7e1b]173 return;
174 }
175 }
[a35b458]176
[8049b79]177 RNGABS(out->mem_ranges.ranges[count]) = absolute;
178 RNGREL(out->mem_ranges.ranges[count]) = relative;
179 RNGSZ(out->mem_ranges.ranges[count]) = size;
[00d7e1b]180 out->mem_ranges.ranges[count].endianness = endianness;
181 out->mem_ranges.count++;
182}
183
184/** Parse list of hardware resources
185 *
[8049b79]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.
[00d7e1b]192 *
193 * @return EOK if succeed, error code otherwise.
194 *
195 */
[b7fd2a0]196errno_t hw_res_list_parse(const pio_window_t *win,
[8049b79]197 const hw_resource_list_t *res, hw_res_list_parsed_t *out, int flags)
[00d7e1b]198{
[8049b79]199 if (!res || !out)
[00d7e1b]200 return EINVAL;
[a35b458]201
[8049b79]202 size_t res_count = res->count;
[00d7e1b]203 hw_res_list_parsed_clean(out);
[a35b458]204
[46d9d13]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 }
[a35b458]214
[00d7e1b]215 for (size_t i = 0; i < res_count; ++i) {
[8049b79]216 const hw_resource_t *resource = &res->resources[i];
[a35b458]217
[00d7e1b]218 switch (resource->type) {
219 case INTERRUPT:
220 hw_res_parse_add_irq(out, resource, flags);
221 break;
222 case IO_RANGE:
[8049b79]223 hw_res_parse_add_io_range(out, win, resource, flags);
[00d7e1b]224 break;
225 case MEM_RANGE:
[8049b79]226 hw_res_parse_add_mem_range(out, win, resource, flags);
[00d7e1b]227 break;
[46d9d13]228 case DMA_CHANNEL_8:
229 case DMA_CHANNEL_16:
230 hw_res_parse_add_dma_channel(out, resource, flags);
231 break;
[00d7e1b]232 default:
[46d9d13]233 hw_res_list_parsed_clean(out);
[00d7e1b]234 return EINVAL;
235 }
236 }
[a35b458]237
[00d7e1b]238 return EOK;
[84239b1]239}
[00d7e1b]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 */
[b7fd2a0]255errno_t hw_res_get_list_parsed(async_sess_t *sess,
[00d7e1b]256 hw_res_list_parsed_t *hw_res_parsed, int flags)
257{
[8049b79]258 pio_window_t pio_window;
[b7fd2a0]259 errno_t rc;
[8049b79]260
[00d7e1b]261 if (!hw_res_parsed)
262 return EBADMEM;
[a35b458]263
[00d7e1b]264 hw_resource_list_t hw_resources;
265 hw_res_list_parsed_clean(hw_res_parsed);
[acdb5bac]266 memset(&hw_resources, 0, sizeof(hw_resource_list_t));
[8049b79]267
268 rc = pio_window_get(sess, &pio_window);
269 if (rc != EOK)
[1b20da0]270 return rc;
[a35b458]271
[8049b79]272 rc = hw_res_get_resource_list(sess, &hw_resources);
[00d7e1b]273 if (rc != EOK)
274 return rc;
[8049b79]275
276 rc = hw_res_list_parse(&pio_window, &hw_resources, hw_res_parsed,
277 flags);
[00d7e1b]278 hw_res_clean_resource_list(&hw_resources);
[a35b458]279
[00d7e1b]280 return rc;
[84239b1]281}
[00d7e1b]282
283/** @}
284 */
Note: See TracBrowser for help on using the repository browser.