#include "target.h" less wildly
[fw/openocd] / src / target / armv4_5_mmu.c
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
24 #include "log.h"
25 #include "target.h"
26 #include "armv4_5_mmu.h"
27
28
29 uint32_t armv4mmu_translate_va(struct target *target, struct armv4_5_mmu_common *armv4_5_mmu, uint32_t va, int *type, uint32_t *cb, int *domain, uint32_t *ap);
30
31 char* armv4_5_mmu_page_type_names[] =
32 {
33         "section", "large page", "small page", "tiny page"
34 };
35
36 uint32_t armv4_5_mmu_translate_va(struct target *target, struct armv4_5_mmu_common *armv4_5_mmu, uint32_t va, int *type, uint32_t *cb, int *domain, uint32_t *ap)
37 {
38         uint32_t first_lvl_descriptor = 0x0;
39         uint32_t second_lvl_descriptor = 0x0;
40         uint32_t ttb = armv4_5_mmu->get_ttb(target);
41
42         armv4_5_mmu_read_physical(target, armv4_5_mmu,
43                 (ttb & 0xffffc000) | ((va & 0xfff00000) >> 18),
44                 4, 1, (uint8_t*)&first_lvl_descriptor);
45         first_lvl_descriptor = target_buffer_get_u32(target, (uint8_t*)&first_lvl_descriptor);
46
47         LOG_DEBUG("1st lvl desc: %8.8" PRIx32 "", first_lvl_descriptor);
48
49         if ((first_lvl_descriptor & 0x3) == 0)
50         {
51                 *type = -1;
52                 LOG_ERROR("Address translation failure");
53                 return ERROR_TARGET_TRANSLATION_FAULT;
54         }
55
56         if (!armv4_5_mmu->has_tiny_pages && ((first_lvl_descriptor & 0x3) == 3))
57         {
58                 *type = -1;
59                 LOG_ERROR("Address translation failure");
60                 return ERROR_TARGET_TRANSLATION_FAULT;
61         }
62
63         /* domain is always specified in bits 8-5 */
64         *domain = (first_lvl_descriptor & 0x1e0) >> 5;
65
66         if ((first_lvl_descriptor & 0x3) == 2)
67         {
68                 /* section descriptor */
69                 *type = ARMV4_5_SECTION;
70                 *cb = (first_lvl_descriptor & 0xc) >> 2;
71                 *ap = (first_lvl_descriptor & 0xc00) >> 10;
72                 return (first_lvl_descriptor & 0xfff00000) | (va & 0x000fffff);
73         }
74
75         if ((first_lvl_descriptor & 0x3) == 1)
76         {
77                 /* coarse page table */
78                 armv4_5_mmu_read_physical(target, armv4_5_mmu,
79                         (first_lvl_descriptor & 0xfffffc00) | ((va & 0x000ff000) >> 10),
80                         4, 1, (uint8_t*)&second_lvl_descriptor);
81         }
82         else if ((first_lvl_descriptor & 0x3) == 3)
83         {
84                 /* fine page table */
85                 armv4_5_mmu_read_physical(target, armv4_5_mmu,
86                         (first_lvl_descriptor & 0xfffff000) | ((va & 0x000ffc00) >> 8),
87                         4, 1, (uint8_t*)&second_lvl_descriptor);
88         }
89
90         second_lvl_descriptor = target_buffer_get_u32(target, (uint8_t*)&second_lvl_descriptor);
91
92         LOG_DEBUG("2nd lvl desc: %8.8" PRIx32 "", second_lvl_descriptor);
93
94         if ((second_lvl_descriptor & 0x3) == 0)
95         {
96                 *type = -1;
97                 LOG_ERROR("Address translation failure");
98                 return ERROR_TARGET_TRANSLATION_FAULT;
99         }
100
101         /* cacheable/bufferable is always specified in bits 3-2 */
102         *cb = (second_lvl_descriptor & 0xc) >> 2;
103
104         if ((second_lvl_descriptor & 0x3) == 1)
105         {
106                 /* large page descriptor */
107                 *type = ARMV4_5_LARGE_PAGE;
108                 *ap = (second_lvl_descriptor & 0xff0) >> 4;
109                 return (second_lvl_descriptor & 0xffff0000) | (va & 0x0000ffff);
110         }
111
112         if ((second_lvl_descriptor & 0x3) == 2)
113         {
114                 /* small page descriptor */
115                 *type = ARMV4_5_SMALL_PAGE;
116                 *ap = (second_lvl_descriptor & 0xff0) >> 4;
117                 return (second_lvl_descriptor & 0xfffff000) | (va & 0x00000fff);
118         }
119
120         if ((second_lvl_descriptor & 0x3) == 3)
121         {
122                 /* tiny page descriptor */
123                 *type = ARMV4_5_TINY_PAGE;
124                 *ap = (second_lvl_descriptor & 0x30) >> 4;
125                 return (second_lvl_descriptor & 0xfffffc00) | (va & 0x000003ff);
126         }
127
128         /* should not happen */
129         *type = -1;
130         LOG_ERROR("Address translation failure");
131         return ERROR_TARGET_TRANSLATION_FAULT;
132 }
133
134 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)
135 {
136         int retval;
137
138         if (target->state != TARGET_HALTED)
139                 return ERROR_TARGET_NOT_HALTED;
140
141         /* disable MMU and data (or unified) cache */
142         armv4_5_mmu->disable_mmu_caches(target, 1, 1, 0);
143
144         retval = armv4_5_mmu->read_memory(target, address, size, count, buffer);
145
146         /* reenable MMU / cache */
147         armv4_5_mmu->enable_mmu_caches(target, armv4_5_mmu->mmu_enabled,
148                 armv4_5_mmu->armv4_5_cache.d_u_cache_enabled,
149                 armv4_5_mmu->armv4_5_cache.i_cache_enabled);
150
151         return retval;
152 }
153
154 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, uint8_t *buffer)
155 {
156         int retval;
157
158         if (target->state != TARGET_HALTED)
159                 return ERROR_TARGET_NOT_HALTED;
160
161         /* disable MMU and data (or unified) cache */
162         armv4_5_mmu->disable_mmu_caches(target, 1, 1, 0);
163
164         retval = armv4_5_mmu->write_memory(target, address, size, count, buffer);
165
166         /* reenable MMU / cache */
167         armv4_5_mmu->enable_mmu_caches(target, armv4_5_mmu->mmu_enabled,
168                 armv4_5_mmu->armv4_5_cache.d_u_cache_enabled,
169                 armv4_5_mmu->armv4_5_cache.i_cache_enabled);
170
171         return retval;
172 }