Changeset View
Changeset View
Standalone View
Standalone View
sys/arm/mv/mv_ap806_gicp.c
Show All 28 Lines | |||||
#include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
__FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/systm.h> | #include <sys/systm.h> | ||||
#include <sys/bus.h> | #include <sys/bus.h> | ||||
#include <sys/bitset.h> | |||||
#include <sys/kernel.h> | #include <sys/kernel.h> | ||||
#include <sys/module.h> | #include <sys/module.h> | ||||
#include <sys/rman.h> | #include <sys/rman.h> | ||||
#include <sys/lock.h> | #include <sys/lock.h> | ||||
#include <sys/mutex.h> | #include <sys/mutex.h> | ||||
#include <machine/bus.h> | #include <machine/bus.h> | ||||
#include <machine/resource.h> | #include <machine/resource.h> | ||||
#include <machine/intr.h> | #include <machine/intr.h> | ||||
#include <dev/fdt/simplebus.h> | #include <dev/fdt/simplebus.h> | ||||
#include <dev/ofw/ofw_bus.h> | #include <dev/ofw/ofw_bus.h> | ||||
#include <dev/ofw/ofw_bus_subr.h> | #include <dev/ofw/ofw_bus_subr.h> | ||||
#include "pic_if.h" | #include "pic_if.h" | ||||
#include "mv_intr.h" | |||||
#define MV_AP806_GICP_MAX_NIRQS 207 | #define MV_AP806_GICP_MAX_NIRQS 207 | ||||
#define MV_AP806_GICP_SETSPI_OFFSET 0x0 | |||||
#define MV_AP806_GICP_CLRSPI_OFFSET 0x8 | |||||
MALLOC_DECLARE(M_GICP); | |||||
MALLOC_DEFINE(M_GICP, "gicp", "Marvell gicp driver"); | |||||
struct mv_ap806_gicp_softc { | struct mv_ap806_gicp_softc { | ||||
device_t dev; | device_t dev; | ||||
device_t parent; | device_t parent; | ||||
struct resource *res; | struct resource *res; | ||||
ssize_t spi_ranges_cnt; | ssize_t spi_ranges_cnt; | ||||
uint32_t *spi_ranges; | uint32_t *spi_ranges; | ||||
struct intr_map_data_fdt *parent_map_data; | struct intr_map_data_fdt *parent_map_data; | ||||
ssize_t spi_bitmap_size; /* Nr of bits in the bitmap. */ | |||||
BITSET_DEFINE_VAR() *spi_bitmap; | |||||
}; | }; | ||||
static struct ofw_compat_data compat_data[] = { | static struct ofw_compat_data compat_data[] = { | ||||
{"marvell,ap806-gicp", 1}, | {"marvell,ap806-gicp", 1}, | ||||
{NULL, 0} | {NULL, 0} | ||||
}; | }; | ||||
#define RD4(sc, reg) bus_read_4((sc)->res, (reg)) | #define RD4(sc, reg) bus_read_4((sc)->res, (reg)) | ||||
Show All 13 Lines | mv_ap806_gicp_probe(device_t dev) | ||||
return (BUS_PROBE_DEFAULT); | return (BUS_PROBE_DEFAULT); | ||||
} | } | ||||
static int | static int | ||||
mv_ap806_gicp_attach(device_t dev) | mv_ap806_gicp_attach(device_t dev) | ||||
{ | { | ||||
struct mv_ap806_gicp_softc *sc; | struct mv_ap806_gicp_softc *sc; | ||||
phandle_t node, xref, intr_parent; | phandle_t node, xref, intr_parent; | ||||
int i, rid; | |||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
sc->dev = dev; | sc->dev = dev; | ||||
node = ofw_bus_get_node(dev); | node = ofw_bus_get_node(dev); | ||||
/* Look for our parent */ | /* Look for our parent */ | ||||
if ((intr_parent = ofw_bus_find_iparent(node)) == 0) { | if ((intr_parent = ofw_bus_find_iparent(node)) == 0) { | ||||
device_printf(dev, | device_printf(dev, | ||||
"Cannot find our parent interrupt controller\n"); | "Cannot find our parent interrupt controller\n"); | ||||
return (ENXIO); | return (ENXIO); | ||||
} | } | ||||
if ((sc->parent = OF_device_from_xref(intr_parent)) == NULL) { | if ((sc->parent = OF_device_from_xref(intr_parent)) == NULL) { | ||||
device_printf(dev, | device_printf(dev, | ||||
"cannot find parent interrupt controller device\n"); | "cannot find parent interrupt controller device\n"); | ||||
return (ENXIO); | return (ENXIO); | ||||
} | } | ||||
rid = 0; | |||||
sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); | |||||
if (sc->res == NULL) { | |||||
device_printf(dev, "cannot allocate resources for device\n"); | |||||
return (ENXIO); | |||||
} | |||||
sc->spi_ranges_cnt = OF_getencprop_alloc(node, "marvell,spi-ranges", | sc->spi_ranges_cnt = OF_getencprop_alloc(node, "marvell,spi-ranges", | ||||
(void **)&sc->spi_ranges) / sizeof(*sc->spi_ranges); | (void **)&sc->spi_ranges) / sizeof(*sc->spi_ranges); | ||||
sc->spi_bitmap_size = 0; | |||||
for (i = 0; i < sc->spi_ranges_cnt; i += 2) | |||||
manu: Note that OF_getencprop_alloc doesn't returns the number of elements but the size in bytes.
So… | |||||
mwUnsubmitted Not Done Inline Actions
--- a/sys/arm/mv/mv_ap806_gicp.c +++ b/sys/arm/mv/mv_ap806_gicp.c @@ -130,6 +130,8 @@ mv_ap806_gicp_attach(device_t dev) sc->spi_ranges_cnt = OF_getencprop_alloc_multi(node, "marvell,spi-ranges", sizeof(*sc->spi_ranges), (void **)&sc->spi_ranges); + printf("%s: sc->spi_ranges_cnt = %zd\n\n", __func__, sc->spi_ranges_cnt); + sc->spi_bitmap_size = 0; for (i = 0; i < sc->spi_ranges_cnt; i += 2) sc->spi_bitmap_size += sc->spi_ranges[i + 1]; I see a sane value, matching the DT contents. Boot log snippet: mv_ap806_gicp0: <Marvell GICP> mem 0x3f0040-0x3f004f on simplebus1 mv_ap806_gicp_attach: sc->spi_ranges_cnt = 4 mw: 1. The upstream version of the allocation uses OF_getencprop_alloc_multi.
2. With below diff… | |||||
manuUnsubmitted Not Done Inline Actionssys/contrib/device-tree/src/arm64/marvell/armada-ap80x.dtsi have marvell,spi-ranges = <64 64>, <288 64> manu: sys/contrib/device-tree/src/arm64/marvell/armada-ap80x.dtsi have marvell,spi-ranges = <64 64>… | |||||
mwUnsubmitted Not Done Inline ActionsYes, but there was a division (/ sizeof(*sc->spi_ranges)), so the result was 4 in the old code too :) Anyway, with the new code we also get '4' so I think we can agree, there is no issue here, can't we? mw: Yes, but there was a division (`/ sizeof(*sc->spi_ranges)`), so the result was 4 in the old… | |||||
manuUnsubmitted Not Done Inline ActionsDamn, somehow I don't have it locally, I'll make sure that I have the full correct patch applied (wasn't applied by me ...) manu: Damn, somehow I don't have it locally, I'll make sure that I have the full correct patch… | |||||
sc->spi_bitmap_size += sc->spi_ranges[i + 1]; | |||||
/* | |||||
* Create a bitmap of all SPI IRQs that are available for us. | |||||
* It will be used to dynamically allocate IRQs when requested. | |||||
* Note that a simple counter won't do, since we also need to be able | |||||
* to free and reuse IRQs. | |||||
*/ | |||||
sc->spi_bitmap = BITSET_ALLOC(sc->spi_bitmap_size, M_GICP, M_WAITOK); | |||||
/* 1 - available, 0 - used. */ | |||||
BIT_FILL(sc->spi_bitmap_size, sc->spi_bitmap); | |||||
xref = OF_xref_from_node(node); | xref = OF_xref_from_node(node); | ||||
if (intr_pic_register(dev, xref) == NULL) { | if (intr_pic_register(dev, xref) == NULL) { | ||||
device_printf(dev, "Cannot register GICP\n"); | device_printf(dev, "Cannot register GICP\n"); | ||||
return (ENXIO); | return (ENXIO); | ||||
} | } | ||||
/* Allocate GIC compatible mapping entry (3 cells) */ | /* Allocate GIC compatible mapping entry (3 cells) */ | ||||
sc->parent_map_data = (struct intr_map_data_fdt *)intr_alloc_map_data( | sc->parent_map_data = (struct intr_map_data_fdt *)intr_alloc_map_data( | ||||
INTR_MAP_DATA_FDT, sizeof(struct intr_map_data_fdt) + | INTR_MAP_DATA_FDT, sizeof(struct intr_map_data_fdt) + | ||||
+ 3 * sizeof(phandle_t), M_WAITOK | M_ZERO); | + 3 * sizeof(phandle_t), M_WAITOK | M_ZERO); | ||||
OF_device_register_xref(xref, dev); | OF_device_register_xref(xref, dev); | ||||
return (0); | return (0); | ||||
} | } | ||||
static int | static int | ||||
mv_ap806_gicp_detach(device_t dev) | mv_ap806_gicp_detach(device_t dev) | ||||
{ | { | ||||
return (EBUSY); | return (EBUSY); | ||||
} | } | ||||
static int | |||||
mv_ap806_gicp_allocate_irq(struct mv_ap806_gicp_softc *sc, int *irq_no) | |||||
{ | |||||
int irq; | |||||
/* | |||||
* Find first available IRQ represented by first set bit in the bitmap. | |||||
* BIT_FFS starts the count from 1, 0 means that nothing was found. | |||||
*/ | |||||
irq = BIT_FFS(sc->spi_bitmap_size, sc->spi_bitmap); | |||||
if (irq == 0) | |||||
return (ENOMEM); | |||||
irq--; | |||||
BIT_CLR(sc->spi_bitmap_size, irq, sc->spi_bitmap); | |||||
*irq_no = irq; | |||||
return (0); | |||||
} | |||||
static void | |||||
mv_ap806_gicp_free_irq(struct mv_ap806_gicp_softc *sc, int irq_no) | |||||
{ | |||||
BIT_SET(sc->spi_bitmap_size, irq_no, sc->spi_bitmap); | |||||
} | |||||
static struct intr_map_data * | static struct intr_map_data * | ||||
mv_ap806_gicp_convert_map_data(struct mv_ap806_gicp_softc *sc, | mv_ap806_gicp_convert_map_data(struct mv_ap806_gicp_softc *sc, | ||||
struct intr_map_data *data) | struct intr_map_data *data) | ||||
{ | { | ||||
struct intr_map_data_fdt *daf; | struct intr_map_data_fdt *daf; | ||||
uint32_t i, irq_num, irq_type; | uint32_t i, irq_num, irq_type; | ||||
daf = (struct intr_map_data_fdt *)data; | daf = (struct intr_map_data_fdt *)data; | ||||
if (daf->ncells != 2) | if (daf->ncells != MV_INTRMAP_NCELLS_ICU) | ||||
return (NULL); | return (NULL); | ||||
irq_num = daf->cells[0]; | irq_num = daf->cells[0]; | ||||
irq_type = daf->cells[1]; | irq_type = daf->cells[1]; | ||||
if (irq_num >= MV_AP806_GICP_MAX_NIRQS) | if (irq_num >= MV_AP806_GICP_MAX_NIRQS) | ||||
return (NULL); | return (NULL); | ||||
/* Construct GIC compatible mapping. */ | /* Construct GIC compatible mapping. */ | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | mv_ap806_gicp_disable_intr(device_t dev, struct intr_irqsrc *isrc) | ||||
PIC_DISABLE_INTR(sc->parent, isrc); | PIC_DISABLE_INTR(sc->parent, isrc); | ||||
} | } | ||||
static int | static int | ||||
mv_ap806_gicp_map_intr(device_t dev, struct intr_map_data *data, | mv_ap806_gicp_map_intr(device_t dev, struct intr_map_data *data, | ||||
struct intr_irqsrc **isrcp) | struct intr_irqsrc **isrcp) | ||||
{ | { | ||||
struct mv_ap806_gicp_softc *sc; | struct mv_ap806_gicp_softc *sc; | ||||
struct intr_map_data_fdt *daf; | |||||
int irq_no, irq_type; | |||||
vm_paddr_t paddr; | |||||
int ret; | int ret; | ||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
if (data->type != INTR_MAP_DATA_FDT) | if (data->type != INTR_MAP_DATA_FDT) | ||||
return (ENOTSUP); | return (ENOTSUP); | ||||
data = mv_ap806_gicp_convert_map_data(sc, data); | daf = (struct intr_map_data_fdt*) data; | ||||
if (data == NULL) | if (daf->ncells != MV_INTRMAP_NCELLS_ICU) | ||||
return (EINVAL); | return (EINVAL); | ||||
ret = mv_ap806_gicp_allocate_irq(sc, &irq_no); | |||||
if (ret != 0) | |||||
return (ret); | |||||
daf->cells[0] = irq_no; | |||||
irq_type = daf->cells[1]; | |||||
/* Convert map data to GIC format and call the parent. */ | |||||
data = mv_ap806_gicp_convert_map_data(sc, data); | |||||
if (data == NULL) { | |||||
ret = EINVAL; | |||||
goto fail; | |||||
} | |||||
ret = PIC_MAP_INTR(sc->parent, data, isrcp); | ret = PIC_MAP_INTR(sc->parent, data, isrcp); | ||||
if (ret != 0) | |||||
goto fail; | |||||
/* | |||||
* Fill the map structure again, it will be read in the ICU. | |||||
* It includes irq number allocated above and | |||||
* initialization data. | |||||
*/ | |||||
daf->cells[0] = irq_no; | |||||
daf->cells[1] = irq_type; | |||||
paddr = rman_get_start(sc->res) + MV_AP806_GICP_SETSPI_OFFSET; | |||||
daf->cells[2] = (uint32_t)paddr; | |||||
daf->cells[3] = (uint32_t)((paddr >> 32) & UINT32_MAX); | |||||
paddr = rman_get_start(sc->res) + MV_AP806_GICP_CLRSPI_OFFSET; | |||||
daf->cells[4] = (uint32_t)paddr; | |||||
daf->cells[5] = (uint32_t)((paddr >> 32) & UINT32_MAX); | |||||
(*isrcp)->isrc_dev = sc->dev; | (*isrcp)->isrc_dev = sc->dev; | ||||
return (0); | |||||
fail: | |||||
mv_ap806_gicp_free_irq(sc, irq_no); | |||||
return(ret); | return(ret); | ||||
} | } | ||||
static int | static int | ||||
mv_ap806_gicp_deactivate_intr(device_t dev, struct intr_irqsrc *isrc, | mv_ap806_gicp_deactivate_intr(device_t dev, struct intr_irqsrc *isrc, | ||||
struct resource *res, struct intr_map_data *data) | struct resource *res, struct intr_map_data *data) | ||||
{ | { | ||||
struct mv_ap806_gicp_softc *sc; | struct mv_ap806_gicp_softc *sc; | ||||
struct intr_map_data_fdt *daf; | |||||
sc = device_get_softc(dev); | sc = device_get_softc(dev); | ||||
daf = (struct intr_map_data_fdt *)data; | |||||
if (daf->ncells != MV_INTRMAP_NCELLS_ICU) | |||||
return (EINVAL); | |||||
mv_ap806_gicp_free_irq(sc, daf->cells[0]); | |||||
data = mv_ap806_gicp_convert_map_data(sc, data); | data = mv_ap806_gicp_convert_map_data(sc, data); | ||||
if (data == NULL) | if (data == NULL) | ||||
return (EINVAL); | return (EINVAL); | ||||
return (PIC_DEACTIVATE_INTR(sc->parent, isrc, res, data)); | return (PIC_DEACTIVATE_INTR(sc->parent, isrc, res, data)); | ||||
} | } | ||||
static int | static int | ||||
▲ Show 20 Lines • Show All 88 Lines • Show Last 20 Lines |
Note that OF_getencprop_alloc doesn't returns the number of elements but the size in bytes.
So here we end up reading out of bound and in my case allocating a 1.8GB bitmap (yup you read right :P )