1b7cb133eSJeenu Viswambharan /* 2*e6381f9cSJohn Powell * Copyright (c) 2017-2022, Arm Limited and Contributors. All rights reserved. 3b7cb133eSJeenu Viswambharan * 4b7cb133eSJeenu Viswambharan * SPDX-License-Identifier: BSD-3-Clause 5b7cb133eSJeenu Viswambharan */ 6b7cb133eSJeenu Viswambharan 7b7cb133eSJeenu Viswambharan #include <assert.h> 809d40e0eSAntonio Nino Diaz 909d40e0eSAntonio Nino Diaz #include <lib/utils.h> 1009d40e0eSAntonio Nino Diaz 11b7cb133eSJeenu Viswambharan #include "sdei_private.h" 12b7cb133eSJeenu Viswambharan 13b7cb133eSJeenu Viswambharan #define MAP_OFF(_map, _mapping) ((_map) - (_mapping)->map) 14b7cb133eSJeenu Viswambharan 15b7cb133eSJeenu Viswambharan /* 16b7cb133eSJeenu Viswambharan * Get SDEI entry with the given mapping: on success, returns pointer to SDEI 17b7cb133eSJeenu Viswambharan * entry. On error, returns NULL. 18b7cb133eSJeenu Viswambharan * 19b7cb133eSJeenu Viswambharan * Both shared and private maps are stored in single-dimensional array. Private 20b7cb133eSJeenu Viswambharan * event entries are kept for each PE forming a 2D array. 21b7cb133eSJeenu Viswambharan */ 22b7cb133eSJeenu Viswambharan sdei_entry_t *get_event_entry(sdei_ev_map_t *map) 23b7cb133eSJeenu Viswambharan { 24b7cb133eSJeenu Viswambharan const sdei_mapping_t *mapping; 25b7cb133eSJeenu Viswambharan sdei_entry_t *cpu_priv_base; 26ba6e5ca6SJeenu Viswambharan unsigned int base_idx; 27ba6e5ca6SJeenu Viswambharan long int idx; 28b7cb133eSJeenu Viswambharan 29b7cb133eSJeenu Viswambharan if (is_event_private(map)) { 30b7cb133eSJeenu Viswambharan /* 31b7cb133eSJeenu Viswambharan * For a private map, find the index of the mapping in the 32b7cb133eSJeenu Viswambharan * array. 33b7cb133eSJeenu Viswambharan */ 34b7cb133eSJeenu Viswambharan mapping = SDEI_PRIVATE_MAPPING(); 35b7cb133eSJeenu Viswambharan idx = MAP_OFF(map, mapping); 36b7cb133eSJeenu Viswambharan 37b7cb133eSJeenu Viswambharan /* Base of private mappings for this CPU */ 38ba6e5ca6SJeenu Viswambharan base_idx = plat_my_core_pos() * ((unsigned int) mapping->num_maps); 39b7cb133eSJeenu Viswambharan cpu_priv_base = &sdei_private_event_table[base_idx]; 40b7cb133eSJeenu Viswambharan 41b7cb133eSJeenu Viswambharan /* 42b7cb133eSJeenu Viswambharan * Return the address of the entry at the same index in the 43b7cb133eSJeenu Viswambharan * per-CPU event entry. 44b7cb133eSJeenu Viswambharan */ 45b7cb133eSJeenu Viswambharan return &cpu_priv_base[idx]; 46b7cb133eSJeenu Viswambharan } else { 47b7cb133eSJeenu Viswambharan mapping = SDEI_SHARED_MAPPING(); 48b7cb133eSJeenu Viswambharan idx = MAP_OFF(map, mapping); 49b7cb133eSJeenu Viswambharan 50b7cb133eSJeenu Viswambharan return &sdei_shared_event_table[idx]; 51b7cb133eSJeenu Viswambharan } 52b7cb133eSJeenu Viswambharan } 53b7cb133eSJeenu Viswambharan 54b7cb133eSJeenu Viswambharan /* 55b7cb133eSJeenu Viswambharan * Find event mapping for a given interrupt number: On success, returns pointer 56b7cb133eSJeenu Viswambharan * to the event mapping. On error, returns NULL. 57b7cb133eSJeenu Viswambharan */ 58ba6e5ca6SJeenu Viswambharan sdei_ev_map_t *find_event_map_by_intr(unsigned int intr_num, bool shared) 59b7cb133eSJeenu Viswambharan { 60b7cb133eSJeenu Viswambharan const sdei_mapping_t *mapping; 61b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 62b7cb133eSJeenu Viswambharan unsigned int i; 63b7cb133eSJeenu Viswambharan 64b7cb133eSJeenu Viswambharan /* 65b7cb133eSJeenu Viswambharan * Look for a match in private and shared mappings, as requested. This 66b7cb133eSJeenu Viswambharan * is a linear search. However, if the mappings are required to be 67b7cb133eSJeenu Viswambharan * sorted, for large maps, we could consider binary search. 68b7cb133eSJeenu Viswambharan */ 69b7cb133eSJeenu Viswambharan mapping = shared ? SDEI_SHARED_MAPPING() : SDEI_PRIVATE_MAPPING(); 70b7cb133eSJeenu Viswambharan iterate_mapping(mapping, i, map) { 71b7cb133eSJeenu Viswambharan if (map->intr == intr_num) 72b7cb133eSJeenu Viswambharan return map; 73b7cb133eSJeenu Viswambharan } 74b7cb133eSJeenu Viswambharan 75b7cb133eSJeenu Viswambharan return NULL; 76b7cb133eSJeenu Viswambharan } 77b7cb133eSJeenu Viswambharan 78b7cb133eSJeenu Viswambharan /* 79b7cb133eSJeenu Viswambharan * Find event mapping for a given event number: On success returns pointer to 80b7cb133eSJeenu Viswambharan * the event mapping. On error, returns NULL. 81b7cb133eSJeenu Viswambharan */ 82b7cb133eSJeenu Viswambharan sdei_ev_map_t *find_event_map(int ev_num) 83b7cb133eSJeenu Viswambharan { 84b7cb133eSJeenu Viswambharan const sdei_mapping_t *mapping; 85b7cb133eSJeenu Viswambharan sdei_ev_map_t *map; 86b7cb133eSJeenu Viswambharan unsigned int i, j; 87b7cb133eSJeenu Viswambharan 88b7cb133eSJeenu Viswambharan /* 89b7cb133eSJeenu Viswambharan * Iterate through mappings to find a match. This is a linear search. 90b7cb133eSJeenu Viswambharan * However, if the mappings are required to be sorted, for large maps, 91b7cb133eSJeenu Viswambharan * we could consider binary search. 92b7cb133eSJeenu Viswambharan */ 93b7cb133eSJeenu Viswambharan for_each_mapping_type(i, mapping) { 94b7cb133eSJeenu Viswambharan iterate_mapping(mapping, j, map) { 95b7cb133eSJeenu Viswambharan if (map->ev_num == ev_num) 96b7cb133eSJeenu Viswambharan return map; 97b7cb133eSJeenu Viswambharan } 98b7cb133eSJeenu Viswambharan } 99b7cb133eSJeenu Viswambharan 100b7cb133eSJeenu Viswambharan return NULL; 101b7cb133eSJeenu Viswambharan } 102*e6381f9cSJohn Powell 103*e6381f9cSJohn Powell /* 104*e6381f9cSJohn Powell * Return the total number of currently registered SDEI events. 105*e6381f9cSJohn Powell */ 106*e6381f9cSJohn Powell int sdei_get_registered_event_count(void) 107*e6381f9cSJohn Powell { 108*e6381f9cSJohn Powell const sdei_mapping_t *mapping; 109*e6381f9cSJohn Powell sdei_ev_map_t *map; 110*e6381f9cSJohn Powell unsigned int i; 111*e6381f9cSJohn Powell unsigned int j; 112*e6381f9cSJohn Powell int count = 0; 113*e6381f9cSJohn Powell 114*e6381f9cSJohn Powell /* Add up reg counts for each mapping. */ 115*e6381f9cSJohn Powell for_each_mapping_type(i, mapping) { 116*e6381f9cSJohn Powell iterate_mapping(mapping, j, map) { 117*e6381f9cSJohn Powell count += map->reg_count; 118*e6381f9cSJohn Powell } 119*e6381f9cSJohn Powell } 120*e6381f9cSJohn Powell 121*e6381f9cSJohn Powell return count; 122*e6381f9cSJohn Powell } 123