2019-11-04 02:45:05 +01:00
|
|
|
/****************************************************************************
|
2022-03-11 15:13:17 +01:00
|
|
|
* drivers/misc/addrenv.c
|
2019-11-04 02:45:05 +01:00
|
|
|
*
|
2020-08-22 18:36:57 +02: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 02:45:05 +01:00
|
|
|
*
|
2020-08-22 18:36:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2019-11-04 02:45:05 +01:00
|
|
|
*
|
2020-08-22 18:36:57 +02: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 02:45:05 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2021-04-30 14:12:54 +02:00
|
|
|
#include <nuttx/list.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
2019-11-04 02:45:05 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <nuttx/drivers/addrenv.h>
|
|
|
|
|
2021-04-30 14:12:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct simple_addrenv_node_s
|
|
|
|
{
|
|
|
|
struct list_node node;
|
|
|
|
FAR const struct simple_addrenv_s *addrenv;
|
|
|
|
};
|
|
|
|
|
2019-11-04 02:45:05 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-30 14:12:54 +02:00
|
|
|
static struct list_node g_addrenv_list = LIST_INITIAL_VALUE(g_addrenv_list);
|
2019-11-04 02:45:05 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-30 14:12:54 +02:00
|
|
|
void simple_addrenv_initialize(FAR const struct simple_addrenv_s *addrenv)
|
2019-11-04 02:45:05 +01:00
|
|
|
{
|
2021-04-30 14:12:54 +02: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 02:45:05 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 14:12:54 +02:00
|
|
|
FAR void *up_addrenv_pa_to_va(uintptr_t pa)
|
2019-11-04 02:45:05 +01:00
|
|
|
{
|
2021-04-30 14:12:54 +02:00
|
|
|
FAR struct simple_addrenv_node_s *node;
|
|
|
|
FAR const struct simple_addrenv_s *addrenv;
|
2019-11-04 02:45:05 +01:00
|
|
|
uint32_t i;
|
|
|
|
|
2021-04-30 14:12:54 +02:00
|
|
|
list_for_every_entry(&g_addrenv_list, node,
|
|
|
|
struct simple_addrenv_node_s, node)
|
2019-11-04 02:45:05 +01:00
|
|
|
{
|
2021-04-30 14:12:54 +02:00
|
|
|
addrenv = node->addrenv;
|
|
|
|
for (i = 0; addrenv[i].size; i++)
|
2019-11-04 02:45:05 +01:00
|
|
|
{
|
2021-04-30 14:12:54 +02:00
|
|
|
if (pa - addrenv[i].pa < addrenv[i].size)
|
|
|
|
{
|
2021-12-08 05:20:35 +01:00
|
|
|
return (FAR void *)(addrenv[i].va + pa - addrenv[i].pa);
|
2021-04-30 14:12:54 +02:00
|
|
|
}
|
2019-11-04 02:45:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 05:20:35 +01:00
|
|
|
return (FAR void *)pa;
|
2019-11-04 02:45:05 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 14:12:54 +02:00
|
|
|
uintptr_t up_addrenv_va_to_pa(FAR void *va_)
|
2019-11-04 02:45:05 +01:00
|
|
|
{
|
2021-04-30 14:12:54 +02:00
|
|
|
FAR struct simple_addrenv_node_s *node;
|
|
|
|
FAR const struct simple_addrenv_s *addrenv;
|
2021-12-08 05:20:35 +01:00
|
|
|
uintptr_t va = (uintptr_t)va_;
|
2019-11-04 02:45:05 +01:00
|
|
|
uint32_t i;
|
|
|
|
|
2021-04-30 14:12:54 +02:00
|
|
|
list_for_every_entry(&g_addrenv_list, node,
|
|
|
|
struct simple_addrenv_node_s, node)
|
2019-11-04 02:45:05 +01:00
|
|
|
{
|
2021-04-30 14:12:54 +02:00
|
|
|
addrenv = node->addrenv;
|
|
|
|
for (i = 0; addrenv[i].size; i++)
|
2019-11-04 02:45:05 +01:00
|
|
|
{
|
2021-12-08 05:20:35 +01:00
|
|
|
uintptr_t tmp = addrenv[i].va;
|
2021-04-30 14:12:54 +02:00
|
|
|
if (va - tmp < addrenv[i].size)
|
|
|
|
{
|
|
|
|
return addrenv[i].pa + (va - tmp);
|
|
|
|
}
|
2019-11-04 02:45:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return va;
|
|
|
|
}
|