Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020, Arm Limited. All rights reserved. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 8 | #include <ffa_helpers.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 9 | #include <plat_topology.h> |
| 10 | #include <platform.h> |
| 11 | #include <power_management.h> |
| 12 | #include <test_helpers.h> |
| 13 | #include <tftf_lib.h> |
| 14 | |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 15 | static struct mailbox_buffers test_mb = {.send = NULL, .recv = NULL}; |
| 16 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 17 | int is_sys_suspend_state_ready(void) |
| 18 | { |
| 19 | int aff_info; |
| 20 | unsigned int target_node; |
| 21 | u_register_t target_mpid; |
| 22 | u_register_t current_mpid = read_mpidr_el1() & MPID_MASK; |
| 23 | |
| 24 | for_each_cpu(target_node) { |
| 25 | target_mpid = tftf_get_mpidr_from_node(target_node); |
| 26 | |
| 27 | /* Skip current CPU, as it is powered on */ |
| 28 | if (target_mpid == current_mpid) |
| 29 | continue; |
| 30 | |
| 31 | aff_info = tftf_psci_affinity_info(target_mpid, MPIDR_AFFLVL0); |
| 32 | if (aff_info != PSCI_STATE_OFF) |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | void psci_system_reset(void) |
| 40 | { |
| 41 | smc_args args = { SMC_PSCI_SYSTEM_RESET }; |
| 42 | smc_ret_values ret; |
| 43 | |
| 44 | ret = tftf_smc(&args); |
| 45 | |
| 46 | /* The PSCI SYSTEM_RESET call is not supposed to return */ |
| 47 | tftf_testcase_printf("System didn't reboot properly (%d)\n", |
| 48 | (unsigned int)ret.ret0); |
| 49 | } |
| 50 | |
| 51 | int psci_mem_protect(int val) |
| 52 | { |
| 53 | smc_args args = { SMC_PSCI_MEM_PROTECT}; |
| 54 | smc_ret_values ret; |
| 55 | |
| 56 | args.arg1 = val; |
| 57 | ret = tftf_smc(&args); |
| 58 | |
| 59 | return ret.ret0; |
| 60 | } |
| 61 | |
| 62 | int psci_mem_protect_check(uintptr_t addr, size_t size) |
| 63 | { |
| 64 | smc_args args = { SMC_PSCI_MEM_PROTECT_CHECK }; |
| 65 | smc_ret_values ret; |
| 66 | |
| 67 | args.arg1 = addr; |
| 68 | args.arg2 = size; |
| 69 | ret = tftf_smc(&args); |
| 70 | return ret.ret0; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * This function returns an address that can be used as |
| 75 | * sentinel for mem_protect functions. The logic behind |
| 76 | * it is that it has to search one region that doesn't intersect |
| 77 | * with the memory used by TFTF. |
| 78 | */ |
| 79 | unsigned char *psci_mem_prot_get_sentinel(void) |
| 80 | { |
| 81 | const mem_region_t *ranges, *rp, *lim; |
| 82 | int nranges; |
| 83 | IMPORT_SYM(uintptr_t, __TFTF_BASE__, tftf_base); |
| 84 | IMPORT_SYM(uintptr_t, __TFTF_END__, tftf_end); |
| 85 | uintptr_t p = 0; |
| 86 | |
| 87 | ranges = plat_get_prot_regions(&nranges); |
| 88 | if (!ranges) |
| 89 | return NULL; |
| 90 | |
| 91 | lim = &ranges[nranges]; |
| 92 | for (rp = ranges ; rp < lim; rp++) { |
| 93 | p = rp->addr; |
| 94 | if (p < tftf_base || p > tftf_end) |
| 95 | break; |
| 96 | p = p + (rp->size - 1); |
| 97 | if (p < tftf_base || p > tftf_end) |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | return (rp == lim) ? NULL : (unsigned char *) p; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * This function maps the memory region before the |
| 106 | * test and unmap it after the test is run |
| 107 | */ |
| 108 | test_result_t map_test_unmap(const map_args_unmap_t *args, |
| 109 | test_function_arg_t test) |
| 110 | { |
| 111 | int mmap_ret; |
| 112 | test_result_t test_ret; |
| 113 | |
| 114 | mmap_ret = mmap_add_dynamic_region(args->addr, args->addr, |
| 115 | args->size, args->attr); |
| 116 | |
| 117 | if (mmap_ret != 0) { |
| 118 | tftf_testcase_printf("Couldn't map memory (ret = %d)\n", |
| 119 | mmap_ret); |
| 120 | return TEST_RESULT_FAIL; |
| 121 | } |
| 122 | |
| 123 | test_ret = (*test)(args->arg); |
| 124 | |
| 125 | mmap_ret = mmap_remove_dynamic_region(args->addr, args->size); |
| 126 | if (mmap_ret != 0) { |
| 127 | tftf_testcase_printf("Couldn't unmap memory (ret = %d)\n", |
| 128 | mmap_ret); |
| 129 | return TEST_RESULT_FAIL; |
| 130 | } |
| 131 | |
| 132 | return test_ret; |
| 133 | } |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 134 | |
| 135 | void set_tftf_mailbox(const struct mailbox_buffers *mb) |
| 136 | { |
| 137 | if (mb != NULL) { |
| 138 | test_mb = *mb; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | bool get_tftf_mailbox(struct mailbox_buffers *mb) |
| 143 | { |
| 144 | if ((test_mb.recv != NULL) && (test_mb.send != NULL)) { |
| 145 | *mb = test_mb; |
| 146 | return true; |
| 147 | } |
| 148 | return false; |
| 149 | } |