2019-11-04 09:45:05 +08:00
|
|
|
/****************************************************************************
|
2022-03-11 22:13:17 +08:00
|
|
|
* drivers/misc/addrenv.c
|
2019-11-04 09:45:05 +08:00
|
|
|
*
|
2020-08-23 00:36:57 +08:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2019-11-04 09:45:05 +08:00
|
|
|
*
|
2020-08-23 00:36:57 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-11-04 09:45:05 +08:00
|
|
|
*
|
2020-08-23 00:36:57 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2019-11-04 09:45:05 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2021-04-30 20:12:54 +08:00
|
|
|
#include <nuttx/list.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
2019-11-04 09:45:05 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nuttx/drivers/addrenv.h>
|
|
|
|
|
2021-04-30 20:12:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct simple_addrenv_node_s
|
|
|
|
{
|
|
|
|
struct list_node node;
|
|
|
|
FAR const struct simple_addrenv_s *addrenv;
|
|
|
|
};
|
|
|
|
|
2019-11-04 09:45:05 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-30 20:12:54 +08:00
|
|
|
static struct list_node g_addrenv_list = LIST_INITIAL_VALUE(g_addrenv_list);
|
2019-11-04 09:45:05 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-30 20:12:54 +08:00
|
|
|
void simple_addrenv_initialize(FAR const struct simple_addrenv_s *addrenv)
|
2019-11-04 09:45:05 +08:00
|
|
|
{
|
2021-04-30 20:12:54 +08:00
|
|
|
FAR struct simple_addrenv_node_s *node;
|
|
|
|
|
|
|
|
if (addrenv != NULL)
|
|
|
|
{
|
|
|
|
node = kmm_malloc(sizeof(*node));
|
|
|
|
if (node != NULL)
|
|
|
|
{
|
|
|
|
node->addrenv = addrenv;
|
|
|
|
list_add_tail(&g_addrenv_list, &node->node);
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 09:45:05 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 20:12:54 +08:00
|
|
|
FAR void *up_addrenv_pa_to_va(uintptr_t pa)
|
2019-11-04 09:45:05 +08:00
|
|
|
{
|
2021-04-30 20:12:54 +08:00
|
|
|
FAR struct simple_addrenv_node_s *node;
|
|
|
|
FAR const struct simple_addrenv_s *addrenv;
|
2019-11-04 09:45:05 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
2021-04-30 20:12:54 +08:00
|
|
|
list_for_every_entry(&g_addrenv_list, node,
|
|
|
|
struct simple_addrenv_node_s, node)
|
2019-11-04 09:45:05 +08:00
|
|
|
{
|
2021-04-30 20:12:54 +08:00
|
|
|
addrenv = node->addrenv;
|
|
|
|
for (i = 0; addrenv[i].size; i++)
|
2019-11-04 09:45:05 +08:00
|
|
|
{
|
2021-04-30 20:12:54 +08:00
|
|
|
if (pa - addrenv[i].pa < addrenv[i].size)
|
|
|
|
{
|
2021-12-08 12:20:35 +08:00
|
|
|
return (FAR void *)(addrenv[i].va + pa - addrenv[i].pa);
|
2021-04-30 20:12:54 +08:00
|
|
|
}
|
2019-11-04 09:45:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 12:20:35 +08:00
|
|
|
return (FAR void *)pa;
|
2019-11-04 09:45:05 +08:00
|
|
|
}
|
|
|
|
|
2021-04-30 20:12:54 +08:00
|
|
|
uintptr_t up_addrenv_va_to_pa(FAR void *va_)
|
2019-11-04 09:45:05 +08:00
|
|
|
{
|
2021-04-30 20:12:54 +08:00
|
|
|
FAR struct simple_addrenv_node_s *node;
|
|
|
|
FAR const struct simple_addrenv_s *addrenv;
|
2021-12-08 12:20:35 +08:00
|
|
|
uintptr_t va = (uintptr_t)va_;
|
2019-11-04 09:45:05 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
2021-04-30 20:12:54 +08:00
|
|
|
list_for_every_entry(&g_addrenv_list, node,
|
|
|
|
struct simple_addrenv_node_s, node)
|
2019-11-04 09:45:05 +08:00
|
|
|
{
|
2021-04-30 20:12:54 +08:00
|
|
|
addrenv = node->addrenv;
|
|
|
|
for (i = 0; addrenv[i].size; i++)
|
2019-11-04 09:45:05 +08:00
|
|
|
{
|
2021-12-08 12:20:35 +08:00
|
|
|
uintptr_t tmp = addrenv[i].va;
|
2021-04-30 20:12:54 +08:00
|
|
|
if (va - tmp < addrenv[i].size)
|
|
|
|
{
|
|
|
|
return addrenv[i].pa + (va - tmp);
|
|
|
|
}
|
2019-11-04 09:45:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return va;
|
|
|
|
}
|