addrenv.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /****************************************************************************
  2. * drivers/addrenv.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <nuttx/list.h>
  25. #include <nuttx/kmalloc.h>
  26. #include <string.h>
  27. #include <nuttx/drivers/addrenv.h>
  28. /****************************************************************************
  29. * Private Types
  30. ****************************************************************************/
  31. struct simple_addrenv_node_s
  32. {
  33. struct list_node node;
  34. FAR const struct simple_addrenv_s *addrenv;
  35. };
  36. /****************************************************************************
  37. * Private Data
  38. ****************************************************************************/
  39. static struct list_node g_addrenv_list = LIST_INITIAL_VALUE(g_addrenv_list);
  40. /****************************************************************************
  41. * Public Functions
  42. ****************************************************************************/
  43. void simple_addrenv_initialize(FAR const struct simple_addrenv_s *addrenv)
  44. {
  45. FAR struct simple_addrenv_node_s *node;
  46. if (addrenv != NULL)
  47. {
  48. node = kmm_malloc(sizeof(*node));
  49. if (node != NULL)
  50. {
  51. node->addrenv = addrenv;
  52. list_add_tail(&g_addrenv_list, &node->node);
  53. }
  54. }
  55. }
  56. FAR void *up_addrenv_pa_to_va(uintptr_t pa)
  57. {
  58. FAR struct simple_addrenv_node_s *node;
  59. FAR const struct simple_addrenv_s *addrenv;
  60. uint32_t i;
  61. list_for_every_entry(&g_addrenv_list, node,
  62. struct simple_addrenv_node_s, node)
  63. {
  64. addrenv = node->addrenv;
  65. for (i = 0; addrenv[i].size; i++)
  66. {
  67. if (pa - addrenv[i].pa < addrenv[i].size)
  68. {
  69. return (FAR void *)(addrenv[i].va +
  70. B2C(pa - addrenv[i].pa));
  71. }
  72. }
  73. }
  74. return (FAR void *)B2C(pa);
  75. }
  76. uintptr_t up_addrenv_va_to_pa(FAR void *va_)
  77. {
  78. FAR struct simple_addrenv_node_s *node;
  79. FAR const struct simple_addrenv_s *addrenv;
  80. uintptr_t va = C2B((uintptr_t)va_);
  81. uint32_t i;
  82. list_for_every_entry(&g_addrenv_list, node,
  83. struct simple_addrenv_node_s, node)
  84. {
  85. addrenv = node->addrenv;
  86. for (i = 0; addrenv[i].size; i++)
  87. {
  88. uintptr_t tmp = C2B(addrenv[i].va);
  89. if (va - tmp < addrenv[i].size)
  90. {
  91. return addrenv[i].pa + (va - tmp);
  92. }
  93. }
  94. }
  95. return va;
  96. }