2 * Copyright (C) 2010-2014, 2016 ARM Limited. All rights reserved.
4 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
7 * A copy of the licence is included with the program, and can also be obtained from Free Software
8 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
11 #include "mali_kernel_common.h"
13 #include "mali_osk_list.h"
17 #include "mali_hw_core.h"
18 #include "mali_group.h"
19 #include "mali_mmu_page_directory.h"
22 * Size of the MMU registers in bytes
24 #define MALI_MMU_REGISTERS_SIZE 0x24
28 * These are the commands that can be sent
31 typedef enum mali_mmu_command {
32 MALI_MMU_COMMAND_ENABLE_PAGING = 0x00, /**< Enable paging (memory translation) */
33 MALI_MMU_COMMAND_DISABLE_PAGING = 0x01, /**< Disable paging (memory translation) */
34 MALI_MMU_COMMAND_ENABLE_STALL = 0x02, /**< Enable stall on page fault */
35 MALI_MMU_COMMAND_DISABLE_STALL = 0x03, /**< Disable stall on page fault */
36 MALI_MMU_COMMAND_ZAP_CACHE = 0x04, /**< Zap the entire page table cache */
37 MALI_MMU_COMMAND_PAGE_FAULT_DONE = 0x05, /**< Page fault processed */
38 MALI_MMU_COMMAND_HARD_RESET = 0x06 /**< Reset the MMU back to power-on settings */
41 static void mali_mmu_probe_trigger(void *data);
42 static _mali_osk_errcode_t mali_mmu_probe_ack(void *data);
44 MALI_STATIC_INLINE _mali_osk_errcode_t mali_mmu_raw_reset(struct mali_mmu_core *mmu);
46 /* page fault queue flush helper pages
47 * note that the mapping pointers are currently unused outside of the initialization functions */
48 static mali_dma_addr mali_page_fault_flush_page_directory = MALI_INVALID_PAGE;
49 static mali_io_address mali_page_fault_flush_page_directory_mapping = NULL;
50 static mali_dma_addr mali_page_fault_flush_page_table = MALI_INVALID_PAGE;
51 static mali_io_address mali_page_fault_flush_page_table_mapping = NULL;
52 static mali_dma_addr mali_page_fault_flush_data_page = MALI_INVALID_PAGE;
53 static mali_io_address mali_page_fault_flush_data_page_mapping = NULL;
55 /* an empty page directory (no address valid) which is active on any MMU not currently marked as in use */
56 static mali_dma_addr mali_empty_page_directory_phys = MALI_INVALID_PAGE;
57 static mali_io_address mali_empty_page_directory_virt = NULL;
60 _mali_osk_errcode_t mali_mmu_initialize(void)
62 /* allocate the helper pages */
63 mali_empty_page_directory_phys = mali_allocate_empty_page(&mali_empty_page_directory_virt);
64 if (0 == mali_empty_page_directory_phys) {
65 MALI_DEBUG_PRINT_ERROR(("Mali MMU: Could not allocate empty page directory.\n"));
66 mali_empty_page_directory_phys = MALI_INVALID_PAGE;
67 return _MALI_OSK_ERR_NOMEM;
70 if (_MALI_OSK_ERR_OK != mali_create_fault_flush_pages(&mali_page_fault_flush_page_directory,
71 &mali_page_fault_flush_page_directory_mapping,
72 &mali_page_fault_flush_page_table,
73 &mali_page_fault_flush_page_table_mapping,
74 &mali_page_fault_flush_data_page,
75 &mali_page_fault_flush_data_page_mapping)) {
76 MALI_DEBUG_PRINT_ERROR(("Mali MMU: Could not allocate fault flush pages\n"));
77 mali_free_empty_page(mali_empty_page_directory_phys, mali_empty_page_directory_virt);
78 mali_empty_page_directory_phys = MALI_INVALID_PAGE;
79 mali_empty_page_directory_virt = NULL;
80 return _MALI_OSK_ERR_NOMEM;
83 return _MALI_OSK_ERR_OK;
86 void mali_mmu_terminate(void)
88 MALI_DEBUG_PRINT(3, ("Mali MMU: terminating\n"));
90 /* Free global helper pages */
91 mali_free_empty_page(mali_empty_page_directory_phys, mali_empty_page_directory_virt);
92 mali_empty_page_directory_phys = MALI_INVALID_PAGE;
93 mali_empty_page_directory_virt = NULL;
95 /* Free the page fault flush pages */
96 mali_destroy_fault_flush_pages(&mali_page_fault_flush_page_directory,
97 &mali_page_fault_flush_page_directory_mapping,
98 &mali_page_fault_flush_page_table,
99 &mali_page_fault_flush_page_table_mapping,
100 &mali_page_fault_flush_data_page,
101 &mali_page_fault_flush_data_page_mapping);
104 struct mali_mmu_core *mali_mmu_create(_mali_osk_resource_t *resource, struct mali_group *group, mali_bool is_virtual)
106 struct mali_mmu_core *mmu = NULL;
108 MALI_DEBUG_ASSERT_POINTER(resource);
110 MALI_DEBUG_PRINT(2, ("Mali MMU: Creating Mali MMU: %s\n", resource->description));
112 mmu = _mali_osk_calloc(1, sizeof(struct mali_mmu_core));
114 if (_MALI_OSK_ERR_OK == mali_hw_core_create(&mmu->hw_core, resource, MALI_MMU_REGISTERS_SIZE)) {
115 if (_MALI_OSK_ERR_OK == mali_group_add_mmu_core(group, mmu)) {
117 /* Skip reset and IRQ setup for virtual MMU */
121 if (_MALI_OSK_ERR_OK == mali_mmu_reset(mmu)) {
122 /* Setup IRQ handlers (which will do IRQ probing if needed) */
123 mmu->irq = _mali_osk_irq_init(resource->irq,
124 mali_group_upper_half_mmu,
126 mali_mmu_probe_trigger,
129 resource->description);
130 if (NULL != mmu->irq) {
133 MALI_PRINT_ERROR(("Mali MMU: Failed to setup interrupt handlers for MMU %s\n", mmu->hw_core.description));
136 mali_group_remove_mmu_core(group);
138 MALI_PRINT_ERROR(("Mali MMU: Failed to add core %s to group\n", mmu->hw_core.description));
140 mali_hw_core_delete(&mmu->hw_core);
145 MALI_PRINT_ERROR(("Failed to allocate memory for MMU\n"));
151 void mali_mmu_delete(struct mali_mmu_core *mmu)
153 if (NULL != mmu->irq) {
154 _mali_osk_irq_term(mmu->irq);
157 mali_hw_core_delete(&mmu->hw_core);
161 static void mali_mmu_enable_paging(struct mali_mmu_core *mmu)
165 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_ENABLE_PAGING);
167 for (i = 0; i < MALI_REG_POLL_COUNT_FAST; ++i) {
168 if (mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS) & MALI_MMU_STATUS_BIT_PAGING_ENABLED) {
172 if (MALI_REG_POLL_COUNT_FAST == i) {
173 MALI_PRINT_ERROR(("Enable paging request failed, MMU status is 0x%08X\n", mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS)));
178 * Issues the enable stall command to the MMU and waits for HW to complete the request
179 * @param mmu The MMU to enable paging for
180 * @return MALI_TRUE if HW stall was successfully engaged, otherwise MALI_FALSE (req timed out)
182 static mali_bool mali_mmu_enable_stall(struct mali_mmu_core *mmu)
185 u32 mmu_status = mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS);
187 if (0 == (mmu_status & MALI_MMU_STATUS_BIT_PAGING_ENABLED)) {
188 MALI_DEBUG_PRINT(4, ("MMU stall is implicit when Paging is not enabled.\n"));
192 if (mmu_status & MALI_MMU_STATUS_BIT_PAGE_FAULT_ACTIVE) {
193 MALI_DEBUG_PRINT(3, ("Aborting MMU stall request since it is in pagefault state.\n"));
197 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_ENABLE_STALL);
199 for (i = 0; i < MALI_REG_POLL_COUNT_FAST; ++i) {
200 mmu_status = mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS);
201 if (mmu_status & MALI_MMU_STATUS_BIT_PAGE_FAULT_ACTIVE) {
204 if ((mmu_status & MALI_MMU_STATUS_BIT_STALL_ACTIVE) && (0 == (mmu_status & MALI_MMU_STATUS_BIT_STALL_NOT_ACTIVE))) {
207 if (0 == (mmu_status & (MALI_MMU_STATUS_BIT_PAGING_ENABLED))) {
211 if (MALI_REG_POLL_COUNT_FAST == i) {
212 MALI_DEBUG_PRINT(2, ("Enable stall request failed, MMU status is 0x%08X\n", mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS)));
216 if (mmu_status & MALI_MMU_STATUS_BIT_PAGE_FAULT_ACTIVE) {
217 MALI_DEBUG_PRINT(2, ("Aborting MMU stall request since it has a pagefault.\n"));
225 * Issues the disable stall command to the MMU and waits for HW to complete the request
226 * @param mmu The MMU to enable paging for
228 static void mali_mmu_disable_stall(struct mali_mmu_core *mmu)
231 u32 mmu_status = mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS);
233 if (0 == (mmu_status & MALI_MMU_STATUS_BIT_PAGING_ENABLED)) {
234 MALI_DEBUG_PRINT(3, ("MMU disable skipped since it was not enabled.\n"));
237 if (mmu_status & MALI_MMU_STATUS_BIT_PAGE_FAULT_ACTIVE) {
238 MALI_DEBUG_PRINT(2, ("Aborting MMU disable stall request since it is in pagefault state.\n"));
242 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_DISABLE_STALL);
244 for (i = 0; i < MALI_REG_POLL_COUNT_FAST; ++i) {
245 u32 status = mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS);
246 if (0 == (status & MALI_MMU_STATUS_BIT_STALL_ACTIVE)) {
249 if (status & MALI_MMU_STATUS_BIT_PAGE_FAULT_ACTIVE) {
252 if (0 == (mmu_status & MALI_MMU_STATUS_BIT_PAGING_ENABLED)) {
256 if (MALI_REG_POLL_COUNT_FAST == i) MALI_DEBUG_PRINT(1, ("Disable stall request failed, MMU status is 0x%08X\n", mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS)));
259 void mali_mmu_page_fault_done(struct mali_mmu_core *mmu)
261 MALI_DEBUG_PRINT(4, ("Mali MMU: %s: Leaving page fault mode\n", mmu->hw_core.description));
262 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_PAGE_FAULT_DONE);
265 MALI_STATIC_INLINE _mali_osk_errcode_t mali_mmu_raw_reset(struct mali_mmu_core *mmu)
269 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_DTE_ADDR, 0xCAFEBABE);
270 MALI_DEBUG_ASSERT(0xCAFEB000 == mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_DTE_ADDR));
271 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_HARD_RESET);
273 for (i = 0; i < MALI_REG_POLL_COUNT_FAST; ++i) {
274 if (mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_DTE_ADDR) == 0) {
278 if (MALI_REG_POLL_COUNT_FAST == i) {
279 MALI_PRINT_ERROR(("Reset request failed, MMU status is 0x%08X\n", mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS)));
280 return _MALI_OSK_ERR_FAULT;
283 return _MALI_OSK_ERR_OK;
286 _mali_osk_errcode_t mali_mmu_reset(struct mali_mmu_core *mmu)
288 _mali_osk_errcode_t err = _MALI_OSK_ERR_FAULT;
289 mali_bool stall_success;
290 MALI_DEBUG_ASSERT_POINTER(mmu);
292 stall_success = mali_mmu_enable_stall(mmu);
293 if (!stall_success) {
294 err = _MALI_OSK_ERR_BUSY;
297 MALI_DEBUG_PRINT(3, ("Mali MMU: mali_kernel_mmu_reset: %s\n", mmu->hw_core.description));
299 if (_MALI_OSK_ERR_OK == mali_mmu_raw_reset(mmu)) {
300 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_INT_MASK, MALI_MMU_INTERRUPT_PAGE_FAULT | MALI_MMU_INTERRUPT_READ_BUS_ERROR);
301 /* no session is active, so just activate the empty page directory */
302 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_DTE_ADDR, mali_empty_page_directory_phys);
303 mali_mmu_enable_paging(mmu);
304 err = _MALI_OSK_ERR_OK;
306 mali_mmu_disable_stall(mmu);
311 mali_bool mali_mmu_zap_tlb(struct mali_mmu_core *mmu)
313 mali_bool stall_success = mali_mmu_enable_stall(mmu);
315 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_ZAP_CACHE);
317 if (MALI_FALSE == stall_success) {
318 /* False means that it is in Pagefault state. Not possible to disable_stall then */
322 mali_mmu_disable_stall(mmu);
326 void mali_mmu_zap_tlb_without_stall(struct mali_mmu_core *mmu)
328 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_ZAP_CACHE);
332 void mali_mmu_invalidate_page(struct mali_mmu_core *mmu, u32 mali_address)
334 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_ZAP_ONE_LINE, MALI_MMU_PDE_ENTRY(mali_address));
337 static void mali_mmu_activate_address_space(struct mali_mmu_core *mmu, u32 page_directory)
339 /* The MMU must be in stalled or page fault mode, for this writing to work */
340 MALI_DEBUG_ASSERT(0 != (mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS)
341 & (MALI_MMU_STATUS_BIT_STALL_ACTIVE | MALI_MMU_STATUS_BIT_PAGE_FAULT_ACTIVE)));
342 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_DTE_ADDR, page_directory);
343 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_COMMAND, MALI_MMU_COMMAND_ZAP_CACHE);
347 void mali_mmu_activate_page_directory(struct mali_mmu_core *mmu, struct mali_page_directory *pagedir)
349 mali_bool stall_success;
350 MALI_DEBUG_ASSERT_POINTER(mmu);
352 MALI_DEBUG_PRINT(5, ("Asked to activate page directory 0x%x on MMU %s\n", pagedir, mmu->hw_core.description));
354 stall_success = mali_mmu_enable_stall(mmu);
355 MALI_DEBUG_ASSERT(stall_success);
356 MALI_IGNORE(stall_success);
357 mali_mmu_activate_address_space(mmu, pagedir->page_directory);
358 mali_mmu_disable_stall(mmu);
361 void mali_mmu_activate_empty_page_directory(struct mali_mmu_core *mmu)
363 mali_bool stall_success;
365 MALI_DEBUG_ASSERT_POINTER(mmu);
366 MALI_DEBUG_PRINT(3, ("Activating the empty page directory on MMU %s\n", mmu->hw_core.description));
368 stall_success = mali_mmu_enable_stall(mmu);
370 /* This function can only be called when the core is idle, so it could not fail. */
371 MALI_DEBUG_ASSERT(stall_success);
372 MALI_IGNORE(stall_success);
374 mali_mmu_activate_address_space(mmu, mali_empty_page_directory_phys);
375 mali_mmu_disable_stall(mmu);
378 void mali_mmu_activate_fault_flush_page_directory(struct mali_mmu_core *mmu)
380 mali_bool stall_success;
381 MALI_DEBUG_ASSERT_POINTER(mmu);
383 MALI_DEBUG_PRINT(3, ("Activating the page fault flush page directory on MMU %s\n", mmu->hw_core.description));
384 stall_success = mali_mmu_enable_stall(mmu);
385 /* This function is expect to fail the stalling, since it might be in PageFault mode when it is called */
386 mali_mmu_activate_address_space(mmu, mali_page_fault_flush_page_directory);
387 if (MALI_TRUE == stall_success) mali_mmu_disable_stall(mmu);
390 /* Is called when we want the mmu to give an interrupt */
391 static void mali_mmu_probe_trigger(void *data)
393 struct mali_mmu_core *mmu = (struct mali_mmu_core *)data;
394 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_INT_RAWSTAT, MALI_MMU_INTERRUPT_PAGE_FAULT | MALI_MMU_INTERRUPT_READ_BUS_ERROR);
397 /* Is called when the irq probe wants the mmu to acknowledge an interrupt from the hw */
398 static _mali_osk_errcode_t mali_mmu_probe_ack(void *data)
400 struct mali_mmu_core *mmu = (struct mali_mmu_core *)data;
403 int_stat = mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_INT_STATUS);
405 MALI_DEBUG_PRINT(2, ("mali_mmu_probe_irq_acknowledge: intstat 0x%x\n", int_stat));
406 if (int_stat & MALI_MMU_INTERRUPT_PAGE_FAULT) {
407 MALI_DEBUG_PRINT(2, ("Probe: Page fault detect: PASSED\n"));
408 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_INT_CLEAR, MALI_MMU_INTERRUPT_PAGE_FAULT);
410 MALI_DEBUG_PRINT(1, ("Probe: Page fault detect: FAILED\n"));
413 if (int_stat & MALI_MMU_INTERRUPT_READ_BUS_ERROR) {
414 MALI_DEBUG_PRINT(2, ("Probe: Bus read error detect: PASSED\n"));
415 mali_hw_core_register_write(&mmu->hw_core, MALI_MMU_REGISTER_INT_CLEAR, MALI_MMU_INTERRUPT_READ_BUS_ERROR);
417 MALI_DEBUG_PRINT(1, ("Probe: Bus read error detect: FAILED\n"));
420 if ((int_stat & (MALI_MMU_INTERRUPT_PAGE_FAULT | MALI_MMU_INTERRUPT_READ_BUS_ERROR)) ==
421 (MALI_MMU_INTERRUPT_PAGE_FAULT | MALI_MMU_INTERRUPT_READ_BUS_ERROR)) {
422 return _MALI_OSK_ERR_OK;
425 return _MALI_OSK_ERR_FAULT;
429 void mali_mmu_print_state(struct mali_mmu_core *mmu)
431 MALI_DEBUG_PRINT(2, ("MMU: State of %s is 0x%08x\n", mmu->hw_core.description, mali_hw_core_register_read(&mmu->hw_core, MALI_MMU_REGISTER_STATUS)));