mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-11-17 07:05:31 +08:00
* [libcpu][risc-v] Refactor the RISC-V architecture port Introduce an opt-in unified RISC-V common layer for RV32 and RV64 while keeping existing BSPs on the legacy implementation during migration. - use ARCH_RISCV and ARCH_CPU_64BIT for architecture and XLEN selection - unify context, trap, MMU, cache, timer, SBI, SMP and ISA handling - add PLIC, CLIC, ACLINT, APLIC and IMSIC support - map physical hart IDs to contiguous logical CPU IDs - add T-Head vendor overrides while preserving legacy ports - migrate qemu-virt64-riscv with RV32 and RV64 support - update clock_time, MM, LWP, OFW, PCI and Rust integration Existing BSPs continue using legacy_common or common64 unless they select RT_USING_RISCV_NEW_COMMON. Signed-off-by: GuEe-GUI <2991707448@qq.com> * [libcpu][risc-v] skip disabled CPU nodes in early FDT walk Rbb666 noted that rt_hw_fdt_install_early() walked every /cpus child without checking status, so disabled cores still joined hart registration and MMU capability probing. Skip nodes that are not okay/ok via rt_fdt_device_is_available() before reading reg or mmu-type. Signed-off-by: seteiro <chenmoushun@163.com> * [libcpu][risc-v] Fix per-CPU page table mapping - reuse existing non-leaf page table entries when mapping per-CPU areas - reject leaf entry conflicts instead of overwriting mappings Signed-off-by: GuEe-GUI <2991707448@qq.com> --------- Signed-off-by: GuEe-GUI <2991707448@qq.com> Signed-off-by: seteiro <chenmoushun@163.com> Co-authored-by: seteiro <chenmoushun@163.com>
578 lines
13 KiB
C
Executable File
578 lines
13 KiB
C
Executable File
/*
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Change Logs:
|
|
* Date Author Notes
|
|
* 2022-08-25 GuEe-GUI first version
|
|
*/
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <drivers/ofw.h>
|
|
#include <drivers/ofw_io.h>
|
|
#include <drivers/ofw_fdt.h>
|
|
#include <drivers/misc.h>
|
|
|
|
#define DBG_TAG "rtdm.ofw"
|
|
#define DBG_LVL DBG_INFO
|
|
#include <rtdbg.h>
|
|
|
|
#include "ofw_internal.h"
|
|
|
|
static volatile rt_atomic_t _bus_ranges_idx = 0;
|
|
static struct bus_ranges *_bus_ranges[RT_USING_OFW_BUS_RANGES_NUMBER] = {};
|
|
|
|
static int ofw_bus_addr_cells(struct rt_ofw_node *np)
|
|
{
|
|
int res = OFW_ROOT_NODE_ADDR_CELLS_DEFAULT;
|
|
|
|
for (rt_uint32_t cells; np; np = np->parent)
|
|
{
|
|
if (!rt_ofw_prop_read_u32(np, "#address-cells", &cells))
|
|
{
|
|
res = cells;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
static int ofw_bus_size_cells(struct rt_ofw_node *np)
|
|
{
|
|
int res = OFW_ROOT_NODE_SIZE_CELLS_DEFAULT;
|
|
|
|
for (rt_uint32_t cells; np; np = np->parent)
|
|
{
|
|
if (!rt_ofw_prop_read_u32(np, "#size-cells", &cells))
|
|
{
|
|
res = cells;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
int rt_ofw_bus_addr_cells(struct rt_ofw_node *np)
|
|
{
|
|
return np ? ofw_bus_addr_cells(np) : -RT_EINVAL;
|
|
}
|
|
|
|
int rt_ofw_bus_size_cells(struct rt_ofw_node *np)
|
|
{
|
|
return np ? ofw_bus_size_cells(np) : -RT_EINVAL;
|
|
}
|
|
|
|
int rt_ofw_io_addr_cells(struct rt_ofw_node *np)
|
|
{
|
|
return np ? ofw_bus_addr_cells(np->parent ? np->parent : np) : -RT_EINVAL;
|
|
}
|
|
|
|
int rt_ofw_io_size_cells(struct rt_ofw_node *np)
|
|
{
|
|
return np ? ofw_bus_size_cells(np->parent ? np->parent : np) : -RT_EINVAL;
|
|
}
|
|
|
|
int rt_ofw_get_address_count(struct rt_ofw_node *np)
|
|
{
|
|
int count;
|
|
|
|
if (np)
|
|
{
|
|
rt_ssize_t len;
|
|
|
|
count = 0;
|
|
|
|
if (rt_ofw_get_prop(np, "reg", &len))
|
|
{
|
|
count = len / (sizeof(fdt32_t) * (rt_ofw_io_addr_cells(np) + rt_ofw_io_size_cells(np)));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
count = -RT_EINVAL;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
static rt_err_t ofw_get_address(struct rt_ofw_node *np, int index, rt_uint64_t *out_address, rt_uint64_t *out_size)
|
|
{
|
|
rt_ssize_t len;
|
|
rt_err_t err = RT_EOK;
|
|
int addr_cells = rt_ofw_io_addr_cells(np);
|
|
int size_cells = rt_ofw_io_size_cells(np);
|
|
int skip_cells = (addr_cells + size_cells) * index;
|
|
const fdt32_t *cell = rt_ofw_prop_read_raw(np, "reg", &len);
|
|
|
|
if (cell && skip_cells < (len / sizeof(*cell)))
|
|
{
|
|
cell += skip_cells;
|
|
*out_address = rt_fdt_next_cell(&cell, addr_cells);
|
|
*out_address = rt_ofw_translate_address(np, RT_NULL, *out_address);
|
|
*out_size = rt_fdt_read_number(cell, size_cells);
|
|
}
|
|
else
|
|
{
|
|
err = -RT_EINVAL;
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
rt_err_t rt_ofw_get_address(struct rt_ofw_node *np, int index, rt_uint64_t *out_address, rt_uint64_t *out_size)
|
|
{
|
|
rt_err_t err;
|
|
|
|
if (np && index >= 0 && (out_address || out_size))
|
|
{
|
|
rt_uint64_t address, size;
|
|
|
|
err = ofw_get_address(np, index, &address, &size);
|
|
|
|
if (!err)
|
|
{
|
|
if (out_address)
|
|
{
|
|
*out_address = address;
|
|
}
|
|
if (out_size)
|
|
{
|
|
*out_size = size;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
err = -RT_EINVAL;
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
static rt_err_t ofw_get_address_by_name(struct rt_ofw_node *np, const char *name,
|
|
rt_uint64_t *out_address, rt_uint64_t *out_size)
|
|
{
|
|
int index = 0;
|
|
rt_err_t err = -RT_EEMPTY;
|
|
const char *reg_name;
|
|
struct rt_ofw_prop *prop;
|
|
|
|
rt_ofw_foreach_prop_string(np, "reg-names", prop, reg_name)
|
|
{
|
|
if (!rt_strcmp(name, reg_name))
|
|
{
|
|
err = rt_ofw_get_address(np, index, out_address, out_size);
|
|
|
|
break;
|
|
}
|
|
|
|
++index;
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
rt_err_t rt_ofw_get_address_by_name(struct rt_ofw_node *np, const char *name,
|
|
rt_uint64_t *out_address, rt_uint64_t *out_size)
|
|
{
|
|
rt_err_t err;
|
|
|
|
if (np && name && (out_address || out_size))
|
|
{
|
|
rt_uint64_t address, size;
|
|
|
|
err = ofw_get_address_by_name(np, name, &address, &size);
|
|
|
|
if (!err)
|
|
{
|
|
if (out_address)
|
|
{
|
|
*out_address = address;
|
|
}
|
|
if (out_size)
|
|
{
|
|
*out_size = size;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
err = -RT_EINVAL;
|
|
}
|
|
|
|
return err;
|
|
}
|
|
|
|
int rt_ofw_get_address_array(struct rt_ofw_node *np, int nr, rt_uint64_t *out_regs)
|
|
{
|
|
int count;
|
|
|
|
if (np && nr > 0 && out_regs)
|
|
{
|
|
rt_ssize_t len;
|
|
int max_nr;
|
|
int addr_cells = rt_ofw_io_addr_cells(np);
|
|
int size_cells = rt_ofw_io_size_cells(np);
|
|
const fdt32_t *cell = rt_ofw_prop_read_raw(np, "reg", &len);
|
|
|
|
max_nr = len / (sizeof(*cell) * (addr_cells + size_cells));
|
|
|
|
if (nr > max_nr)
|
|
{
|
|
nr = max_nr;
|
|
}
|
|
|
|
count = nr;
|
|
|
|
while (nr-- > 0)
|
|
{
|
|
*out_regs = rt_fdt_next_cell(&cell, addr_cells);
|
|
*out_regs = rt_ofw_translate_address(np, RT_NULL, *out_regs);
|
|
++out_regs;
|
|
|
|
*out_regs = rt_fdt_next_cell(&cell, size_cells);
|
|
++out_regs;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
count = -RT_EINVAL;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
static rt_bool_t ofw_bus_is_pci(struct rt_ofw_node *bus)
|
|
{
|
|
rt_ssize_t len;
|
|
struct rt_ofw_prop *prop;
|
|
|
|
if (!bus)
|
|
{
|
|
return RT_FALSE;
|
|
}
|
|
|
|
prop = rt_ofw_get_prop(bus, "device_type", &len);
|
|
|
|
return prop && prop->value && len >= 3 &&
|
|
!rt_strncmp(prop->value, "pci", 3);
|
|
}
|
|
|
|
static rt_uint64_t ofw_read_bus_address(const fdt32_t **cell, int addr_cells,
|
|
struct rt_ofw_node *bus)
|
|
{
|
|
if (addr_cells == 3 && ofw_bus_is_pci(bus))
|
|
{
|
|
const fdt32_t *c = *cell;
|
|
|
|
*cell += 3;
|
|
|
|
return ((rt_uint64_t)fdt32_to_cpu(c[1]) << 32) | fdt32_to_cpu(c[2]);
|
|
}
|
|
|
|
return rt_fdt_next_cell(cell, addr_cells);
|
|
}
|
|
|
|
static struct bus_ranges *ofw_bus_ranges(struct rt_ofw_node *np, struct rt_ofw_prop *prop)
|
|
{
|
|
int id;
|
|
const fdt32_t *cell;
|
|
struct bus_ranges *ranges = RT_NULL;
|
|
int child_address_cells, child_size_cells, parent_address_cells, groups;
|
|
rt_uint64_t *child_addr, *parent_addr, *child_size;
|
|
|
|
/*
|
|
* Address Translation Example:
|
|
*
|
|
* / {
|
|
* #address-cells = <1>;
|
|
* #size-cells = <1>;
|
|
*
|
|
* soc {
|
|
* compatible = "simple-bus";
|
|
* #address-cells = <1>;
|
|
* #size-cells = <1>;
|
|
* ranges = <0x0 0xe0000000 0x00100000>;
|
|
*
|
|
* serial@4600 {
|
|
* device_type = "serial";
|
|
* reg = <0x4600 0x100>;
|
|
* clock-frequency = <0>;
|
|
* };
|
|
* };
|
|
* }
|
|
*
|
|
* The soc node specifies a ranges property of <0x0 0xe0000000 0x00100000>;
|
|
* This property value specifies that for a 1024 KB range of address space, a
|
|
* child node addressed at physical 0x0 maps to a parent address of physical
|
|
* 0xe0000000. With this mapping, the serial device node can be addressed by a
|
|
* load or store at address 0xe0004600, an offset of 0x4600 (specified in reg)
|
|
* plus the 0xe0000000 mapping specified in ranges:
|
|
*
|
|
* bus-address = parent-bus-address + (reg-address - child-bus-address)
|
|
*/
|
|
|
|
do
|
|
{
|
|
child_address_cells = rt_ofw_bus_addr_cells(np);
|
|
child_size_cells = rt_ofw_bus_size_cells(np);
|
|
parent_address_cells = rt_ofw_io_addr_cells(np);
|
|
|
|
if (child_address_cells < 0 || child_size_cells < 0 || parent_address_cells < 0)
|
|
{
|
|
LOG_D("%s read address/size cells fail: child[%d, %d] parent[%d]",
|
|
np->full_name, child_address_cells, child_size_cells, parent_address_cells);
|
|
|
|
break;
|
|
}
|
|
|
|
groups = prop->length / sizeof(*cell);
|
|
groups /= child_address_cells + child_size_cells + parent_address_cells;
|
|
|
|
ranges = rt_malloc(sizeof(*ranges) + sizeof(rt_uint64_t) * 3 * groups);
|
|
|
|
if (!ranges)
|
|
{
|
|
break;
|
|
}
|
|
|
|
ranges->nr = groups;
|
|
ranges->child_addr = (void *)ranges + sizeof(*ranges);
|
|
ranges->parent_addr = &ranges->child_addr[groups];
|
|
ranges->child_size = &ranges->parent_addr[groups];
|
|
|
|
cell = prop->value;
|
|
|
|
child_addr = ranges->child_addr;
|
|
parent_addr = ranges->parent_addr;
|
|
child_size = ranges->child_size;
|
|
|
|
while (groups-- > 0)
|
|
{
|
|
*child_addr++ = ofw_read_bus_address(&cell, child_address_cells, np);
|
|
*parent_addr++ = ofw_read_bus_address(&cell, parent_address_cells, np->parent);
|
|
*child_size++ = rt_fdt_next_cell(&cell, child_size_cells);
|
|
}
|
|
|
|
ranges->np = np;
|
|
ranges->range_type = prop->name;
|
|
|
|
id = (int)rt_atomic_add(&_bus_ranges_idx, 1);
|
|
RT_ASSERT(id < RT_ARRAY_SIZE(_bus_ranges));
|
|
|
|
_bus_ranges[id] = ranges;
|
|
} while (0);
|
|
|
|
return ranges;
|
|
}
|
|
|
|
static struct bus_ranges *ofw_get_bus_ranges(struct rt_ofw_node *bus, const char *range_type)
|
|
{
|
|
rt_ssize_t len;
|
|
struct rt_ofw_prop *prop;
|
|
struct bus_ranges *ranges = RT_NULL;
|
|
|
|
prop = rt_ofw_get_prop(bus, range_type, &len);
|
|
|
|
if (!prop || !len)
|
|
{
|
|
return RT_NULL;
|
|
}
|
|
|
|
for (int i = 0; i < RT_ARRAY_SIZE(_bus_ranges); ++i)
|
|
{
|
|
if (!_bus_ranges[i])
|
|
{
|
|
break;
|
|
}
|
|
|
|
if (_bus_ranges[i]->np == bus &&
|
|
!rt_strcmp(_bus_ranges[i]->range_type, range_type))
|
|
{
|
|
ranges = _bus_ranges[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!ranges)
|
|
{
|
|
ranges = ofw_bus_ranges(bus, prop);
|
|
}
|
|
|
|
return ranges;
|
|
}
|
|
|
|
static rt_uint64_t ofw_translate_at_bus(struct rt_ofw_node *bus, const char *range_type,
|
|
rt_uint64_t address)
|
|
{
|
|
struct bus_ranges *ranges = ofw_get_bus_ranges(bus, range_type);
|
|
|
|
if (!ranges)
|
|
{
|
|
return ~0ULL;
|
|
}
|
|
|
|
for (int i = 0; i < ranges->nr; ++i)
|
|
{
|
|
rt_uint64_t child_addr = ranges->child_addr[i];
|
|
rt_uint64_t child_size = ranges->child_size[i];
|
|
|
|
if (address >= child_addr && address < child_addr + child_size)
|
|
{
|
|
return address + (ranges->parent_addr[i] - child_addr);
|
|
}
|
|
}
|
|
|
|
return address;
|
|
}
|
|
|
|
static rt_uint64_t ofw_reverse_at_bus(struct rt_ofw_node *bus, const char *range_type,
|
|
rt_uint64_t address)
|
|
{
|
|
struct bus_ranges *ranges = ofw_get_bus_ranges(bus, range_type);
|
|
|
|
if (!ranges)
|
|
{
|
|
return ~0ULL;
|
|
}
|
|
|
|
for (int i = 0; i < ranges->nr; ++i)
|
|
{
|
|
rt_uint64_t parent_addr = ranges->parent_addr[i];
|
|
rt_uint64_t child_size = ranges->child_size[i];
|
|
|
|
if (address >= parent_addr && address < parent_addr + child_size)
|
|
{
|
|
return ranges->child_addr[i] + (address - parent_addr);
|
|
}
|
|
}
|
|
|
|
return address;
|
|
}
|
|
|
|
rt_uint64_t rt_ofw_translate_address(struct rt_ofw_node *np, const char *range_type, rt_uint64_t address)
|
|
{
|
|
struct rt_ofw_node *bus;
|
|
|
|
if (!range_type)
|
|
{
|
|
range_type = "ranges";
|
|
}
|
|
|
|
for (bus = np ? np->parent : RT_NULL; bus; bus = bus->parent)
|
|
{
|
|
rt_ssize_t len;
|
|
rt_uint64_t translated;
|
|
|
|
if (!rt_ofw_get_prop(bus, range_type, &len) || !len)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
translated = ofw_translate_at_bus(bus, range_type, address);
|
|
if (translated == ~0ULL)
|
|
{
|
|
return ~0ULL;
|
|
}
|
|
|
|
address = translated;
|
|
}
|
|
|
|
return address;
|
|
}
|
|
|
|
rt_uint64_t rt_ofw_reverse_address(struct rt_ofw_node *np, const char *range_type, rt_uint64_t address)
|
|
{
|
|
struct rt_ofw_node *bus;
|
|
struct rt_ofw_node *parents[OFW_NODE_MAX_DEPTH];
|
|
int count = 0;
|
|
|
|
if (!range_type)
|
|
{
|
|
range_type = "ranges";
|
|
}
|
|
|
|
for (bus = np ? np->parent : RT_NULL; bus; bus = bus->parent)
|
|
{
|
|
rt_ssize_t len;
|
|
|
|
if (!rt_ofw_get_prop(bus, range_type, &len) || !len)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (count < (int)RT_ARRAY_SIZE(parents))
|
|
{
|
|
parents[count++] = bus;
|
|
}
|
|
}
|
|
|
|
while (count-- > 0)
|
|
{
|
|
rt_uint64_t reversed = ofw_reverse_at_bus(parents[count], range_type, address);
|
|
|
|
if (reversed == ~0ULL)
|
|
{
|
|
return ~0ULL;
|
|
}
|
|
|
|
address = reversed;
|
|
}
|
|
|
|
return address;
|
|
}
|
|
|
|
static void *ofw_address_to_pointer(struct rt_ofw_node *np, rt_uint64_t address)
|
|
{
|
|
#ifndef ARCH_CPU_64BIT
|
|
if (rt_upper_32_bits(address))
|
|
{
|
|
LOG_W("%s found 64-bit address = %x%08x",
|
|
rt_ofw_node_full_name(np),
|
|
rt_upper_32_bits(address), rt_lower_32_bits(address));
|
|
}
|
|
#else
|
|
RT_UNUSED(np);
|
|
#endif
|
|
|
|
return (void *)(rt_ubase_t)address;
|
|
}
|
|
|
|
void *rt_ofw_iomap(struct rt_ofw_node *np, int index)
|
|
{
|
|
void *iomem = RT_NULL;
|
|
|
|
if (np)
|
|
{
|
|
rt_uint64_t regs[2];
|
|
|
|
if (!ofw_get_address(np, index, ®s[0], ®s[1]))
|
|
{
|
|
iomem = rt_ioremap(ofw_address_to_pointer(np, regs[0]), (size_t)regs[1]);
|
|
}
|
|
}
|
|
|
|
return iomem;
|
|
}
|
|
|
|
void *rt_ofw_iomap_by_name(struct rt_ofw_node *np, const char *name)
|
|
{
|
|
void *iomem = RT_NULL;
|
|
|
|
if (np)
|
|
{
|
|
rt_uint64_t regs[2];
|
|
|
|
if (!ofw_get_address_by_name(np, name, ®s[0], ®s[1]))
|
|
{
|
|
iomem = rt_ioremap(ofw_address_to_pointer(np, regs[0]), (size_t)regs[1]);
|
|
}
|
|
}
|
|
|
|
return iomem;
|
|
}
|