source: rtems-libbsd/rtemsbsd/rtems/rtems-bsd-nexus.c @ e96e008

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since e96e008 was e96e008, checked in by Sebastian Huber <sebastian.huber@…>, on 08/29/14 at 09:15:00

nexus: Add DISABLE_INTERRUPT_EXTENSION

Add an easy way to avoid the interrupt extension API for BSPs that do
not support it.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2009-2013 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-kernel-space.h>
41#include <machine/rtems-bsd-thread.h>
42
43#include <rtems/bsd/sys/param.h>
44#include <rtems/bsd/sys/types.h>
45#include <sys/systm.h>
46#include <sys/bus.h>
47#include <sys/kernel.h>
48#include <sys/module.h>
49#include <sys/rman.h>
50#include <sys/malloc.h>
51
52#include <rtems/bsd/bsd.h>
53#include <rtems/irq-extension.h>
54
55/* #define DISABLE_INTERRUPT_EXTENSION */
56
57RTEMS_STATIC_ASSERT(SYS_RES_MEMORY == RTEMS_BSD_RES_MEMORY, RTEMS_BSD_RES_MEMORY);
58
59RTEMS_STATIC_ASSERT(SYS_RES_IRQ == RTEMS_BSD_RES_IRQ, RTEMS_BSD_RES_IRQ);
60
61static struct rman mem_rman;
62
63static struct rman irq_rman;
64
65static int
66nexus_probe(device_t dev)
67{
68        rtems_status_code status;
69        int err;
70        size_t i;
71
72        device_set_desc(dev, "RTEMS Nexus device");
73
74#ifndef DISABLE_INTERRUPT_EXTENSION
75        status = rtems_interrupt_server_initialize(
76                BSD_TASK_PRIORITY_INTERRUPT,
77                BSD_MINIMUM_TASK_STACK_SIZE,
78                RTEMS_DEFAULT_MODES,
79                RTEMS_DEFAULT_ATTRIBUTES,
80                NULL
81        );
82        BSD_ASSERT(status == RTEMS_SUCCESSFUL);
83#endif
84
85        mem_rman.rm_start = 0;
86        mem_rman.rm_end = ~0UL;
87        mem_rman.rm_type = RMAN_ARRAY;
88        mem_rman.rm_descr = "I/O memory addresses";
89        err = rman_init(&mem_rman) != 0;
90        BSD_ASSERT(err == 0);
91        err = rman_manage_region(&mem_rman, mem_rman.rm_start, mem_rman.rm_end);
92        BSD_ASSERT(err == 0);
93
94        irq_rman.rm_start = 0;
95        irq_rman.rm_end = ~0UL;
96        irq_rman.rm_type = RMAN_ARRAY;
97        irq_rman.rm_descr = "Interrupt vectors";
98        err = rman_init(&irq_rman) != 0;
99        BSD_ASSERT(err == 0);
100        err = rman_manage_region(&irq_rman, irq_rman.rm_start, irq_rman.rm_end);
101        BSD_ASSERT(err == 0);
102
103        for (i = 0; i < rtems_bsd_nexus_device_count; ++i) {
104                const rtems_bsd_device *nd = &rtems_bsd_nexus_devices[i];
105
106                device_add_child(dev, nd->name, nd->unit);
107        }
108
109        return (0);
110}
111
112static bool
113nexus_get_start(const rtems_bsd_device *nd, int type, u_long *start)
114{
115        u_long sr = *start;
116        size_t i;
117
118        for (i = 0; i < nd->resource_count; ++i) {
119                const rtems_bsd_device_resource *dr = &nd->resources[i];
120
121                if (dr->type == type && dr->start_request == sr) {
122                        *start = dr->start_actual;
123
124                        return (true);
125                }
126        }
127
128        return (false);
129}
130
131static struct resource *
132nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
133    u_long start, u_long end, u_long count, u_int flags)
134{
135        struct resource *rv;
136        struct rman *rm;
137        size_t i;
138
139        switch (type) {
140        case SYS_RES_MEMORY:
141                rm = &mem_rman;
142                break;
143        case SYS_RES_IRQ:
144                rm = &irq_rman;
145                break;
146        default:
147                return (NULL);
148        }
149
150        for (i = 0; i < rtems_bsd_nexus_device_count; ++i) {
151                const rtems_bsd_device *nd = &rtems_bsd_nexus_devices[i];
152
153                if (strcmp(device_get_name(child), nd->name) == 0
154                    && device_get_unit(child) == nd->unit) {
155                        if (!nexus_get_start(nd, type, &start)) {
156                                return (NULL);
157                        };
158                } else {
159                        return (NULL);
160                }
161        }
162
163        rv = rman_reserve_resource(rm, start, end, count, flags, child);
164        if (rv != NULL) {
165                rman_set_rid(rv, *rid);
166                rman_set_bushandle(rv, rman_get_start(rv));
167        }
168
169        return (rv);
170}
171
172static int
173nexus_release_resource(device_t bus, device_t child, int type, int rid,
174    struct resource *res)
175{
176        return (rman_release_resource(res));
177}
178
179struct nexus_intr {
180        driver_filter_t *filt;
181        driver_intr_t *intr;
182        void *arg;
183};
184
185static void
186nexus_intr_with_filter(void *arg)
187{
188        struct nexus_intr *ni;
189        int status;
190
191        ni = arg;
192
193        status = (*ni->filt)(ni->arg);
194        if ((status & FILTER_SCHEDULE_THREAD) != 0) {
195                (*ni->intr)(ni->arg);
196        }
197}
198
199static int
200nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
201    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
202{
203        int err;
204#ifndef DISABLE_INTERRUPT_EXTENSION
205        struct nexus_intr *ni;
206
207        ni = malloc(sizeof(*ni), M_TEMP, M_WAITOK);
208        if (ni != NULL) {
209                rtems_status_code sc;
210                rtems_interrupt_handler rh;
211                void *ra;
212
213                ni->filt = filt;
214                ni->intr = intr;
215                ni->arg = arg;
216
217                *cookiep = ni;
218
219                if (filt == NULL) {
220                        rh = intr;
221                        ra = arg;
222                } else {
223                        rh = nexus_intr_with_filter;
224                        ra = ni;
225                }
226
227                sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE,
228                    rman_get_start(res), device_get_nameunit(child),
229                    RTEMS_INTERRUPT_UNIQUE, rh, ra);
230                if (sc == RTEMS_SUCCESSFUL) {
231                        err = 0;
232                } else {
233                        free(ni, M_TEMP);
234
235                        err = EINVAL;
236                }
237        } else {
238                err = ENOMEM;
239        }
240#else
241        err = EINVAL;
242#endif
243
244        return (err);
245}
246
247static int
248nexus_teardown_intr(device_t dev, device_t child, struct resource *res,
249    void *cookie)
250{
251        int err;
252#ifndef DISABLE_INTERRUPT_EXTENSION
253        struct nexus_intr *ni;
254        rtems_status_code sc;
255        rtems_interrupt_handler rh;
256        void *ra;
257
258        ni = cookie;
259
260        if (ni->filt == NULL) {
261                rh = ni->intr;
262                ra = ni->arg;
263        } else {
264                rh = nexus_intr_with_filter;
265                ra = ni->arg;
266        }
267
268        sc = rtems_interrupt_server_handler_install(RTEMS_ID_NONE,
269            rman_get_start(res), device_get_nameunit(child),
270            RTEMS_INTERRUPT_UNIQUE, rh, ra);
271        err = sc == RTEMS_SUCCESSFUL ? 0 : EINVAL;
272#else
273        err = EINVAL;
274#endif
275
276        return (err);
277}
278
279static device_method_t nexus_methods[] = {
280        /* Device interface */
281        DEVMETHOD(device_probe, nexus_probe),
282        DEVMETHOD(device_attach, bus_generic_attach),
283        DEVMETHOD(device_detach, bus_generic_detach),
284        DEVMETHOD(device_shutdown, bus_generic_shutdown),
285        DEVMETHOD(device_suspend, bus_generic_suspend),
286        DEVMETHOD(device_resume, bus_generic_resume),
287
288        /* Bus interface */
289        DEVMETHOD(bus_print_child, bus_generic_print_child),
290        DEVMETHOD(bus_add_child, bus_generic_add_child),
291        DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
292        DEVMETHOD(bus_release_resource, nexus_release_resource),
293        DEVMETHOD(bus_setup_intr, nexus_setup_intr),
294        DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
295
296        { 0, 0 }
297};
298
299static driver_t nexus_driver = {
300        .name = "nexus",
301        .methods = nexus_methods,
302        .size = 0
303};
304
305static devclass_t nexus_devclass;
306
307DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
Note: See TracBrowser for help on using the repository browser.