You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

177 lines
5.7 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <helper/log.h>
  24. #include "target.h"
  25. #include "armv4_5_mmu.h"
  26. int armv4_5_mmu_translate_va(struct target *target, struct armv4_5_mmu_common *armv4_5_mmu, uint32_t va, uint32_t *cb, uint32_t *val)
  27. {
  28. uint32_t first_lvl_descriptor = 0x0;
  29. uint32_t second_lvl_descriptor = 0x0;
  30. uint32_t ttb;
  31. int retval;
  32. retval = armv4_5_mmu->get_ttb(target, &ttb);
  33. if (retval != ERROR_OK)
  34. return retval;
  35. retval = armv4_5_mmu_read_physical(target, armv4_5_mmu,
  36. (ttb & 0xffffc000) | ((va & 0xfff00000) >> 18),
  37. 4, 1, (uint8_t*)&first_lvl_descriptor);
  38. if (retval != ERROR_OK)
  39. return retval;
  40. first_lvl_descriptor = target_buffer_get_u32(target, (uint8_t*)&first_lvl_descriptor);
  41. LOG_DEBUG("1st lvl desc: %8.8" PRIx32 "", first_lvl_descriptor);
  42. if ((first_lvl_descriptor & 0x3) == 0)
  43. {
  44. LOG_ERROR("Address translation failure");
  45. return ERROR_TARGET_TRANSLATION_FAULT;
  46. }
  47. if (!armv4_5_mmu->has_tiny_pages && ((first_lvl_descriptor & 0x3) == 3))
  48. {
  49. LOG_ERROR("Address translation failure");
  50. return ERROR_TARGET_TRANSLATION_FAULT;
  51. }
  52. if ((first_lvl_descriptor & 0x3) == 2)
  53. {
  54. /* section descriptor */
  55. *cb = (first_lvl_descriptor & 0xc) >> 2;
  56. *val = (first_lvl_descriptor & 0xfff00000) | (va & 0x000fffff);
  57. return ERROR_OK;
  58. }
  59. if ((first_lvl_descriptor & 0x3) == 1)
  60. {
  61. /* coarse page table */
  62. retval = armv4_5_mmu_read_physical(target, armv4_5_mmu,
  63. (first_lvl_descriptor & 0xfffffc00) | ((va & 0x000ff000) >> 10),
  64. 4, 1, (uint8_t*)&second_lvl_descriptor);
  65. if (retval != ERROR_OK)
  66. return retval;
  67. }
  68. else if ((first_lvl_descriptor & 0x3) == 3)
  69. {
  70. /* fine page table */
  71. retval = armv4_5_mmu_read_physical(target, armv4_5_mmu,
  72. (first_lvl_descriptor & 0xfffff000) | ((va & 0x000ffc00) >> 8),
  73. 4, 1, (uint8_t*)&second_lvl_descriptor);
  74. if (retval != ERROR_OK)
  75. return retval;
  76. }
  77. second_lvl_descriptor = target_buffer_get_u32(target, (uint8_t*)&second_lvl_descriptor);
  78. LOG_DEBUG("2nd lvl desc: %8.8" PRIx32 "", second_lvl_descriptor);
  79. if ((second_lvl_descriptor & 0x3) == 0)
  80. {
  81. LOG_ERROR("Address translation failure");
  82. return ERROR_TARGET_TRANSLATION_FAULT;
  83. }
  84. /* cacheable/bufferable is always specified in bits 3-2 */
  85. *cb = (second_lvl_descriptor & 0xc) >> 2;
  86. if ((second_lvl_descriptor & 0x3) == 1)
  87. {
  88. /* large page descriptor */
  89. *val = (second_lvl_descriptor & 0xffff0000) | (va & 0x0000ffff);
  90. return ERROR_OK;
  91. }
  92. if ((second_lvl_descriptor & 0x3) == 2)
  93. {
  94. /* small page descriptor */
  95. *val = (second_lvl_descriptor & 0xfffff000) | (va & 0x00000fff);
  96. return ERROR_OK;
  97. }
  98. if ((second_lvl_descriptor & 0x3) == 3)
  99. {
  100. /* tiny page descriptor */
  101. *val = (second_lvl_descriptor & 0xfffffc00) | (va & 0x000003ff);
  102. return ERROR_OK;
  103. }
  104. /* should not happen */
  105. LOG_ERROR("Address translation failure");
  106. return ERROR_TARGET_TRANSLATION_FAULT;
  107. }
  108. int armv4_5_mmu_read_physical(struct target *target, struct armv4_5_mmu_common *armv4_5_mmu, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
  109. {
  110. int retval;
  111. if (target->state != TARGET_HALTED)
  112. return ERROR_TARGET_NOT_HALTED;
  113. /* disable MMU and data (or unified) cache */
  114. retval = armv4_5_mmu->disable_mmu_caches(target, 1, 1, 0);
  115. if (retval !=ERROR_OK)
  116. return retval;
  117. retval = armv4_5_mmu->read_memory(target, address, size, count, buffer);
  118. if (retval !=ERROR_OK)
  119. return retval;
  120. /* reenable MMU / cache */
  121. retval = armv4_5_mmu->enable_mmu_caches(target, armv4_5_mmu->mmu_enabled,
  122. armv4_5_mmu->armv4_5_cache.d_u_cache_enabled,
  123. armv4_5_mmu->armv4_5_cache.i_cache_enabled);
  124. if (retval !=ERROR_OK)
  125. return retval;
  126. return retval;
  127. }
  128. int armv4_5_mmu_write_physical(struct target *target, struct armv4_5_mmu_common *armv4_5_mmu, uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
  129. {
  130. int retval;
  131. if (target->state != TARGET_HALTED)
  132. return ERROR_TARGET_NOT_HALTED;
  133. /* disable MMU and data (or unified) cache */
  134. retval = armv4_5_mmu->disable_mmu_caches(target, 1, 1, 0);
  135. if (retval !=ERROR_OK)
  136. return retval;
  137. retval = armv4_5_mmu->write_memory(target, address, size, count, buffer);
  138. if (retval !=ERROR_OK)
  139. return retval;
  140. /* reenable MMU / cache */
  141. retval = armv4_5_mmu->enable_mmu_caches(target, armv4_5_mmu->mmu_enabled,
  142. armv4_5_mmu->armv4_5_cache.d_u_cache_enabled,
  143. armv4_5_mmu->armv4_5_cache.i_cache_enabled);
  144. if (retval !=ERROR_OK)
  145. return retval;
  146. return retval;
  147. }