|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2024 OpenMV LLC. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + * |
| 26 | + * libmetal Alif port. |
| 27 | + */ |
| 28 | + |
| 29 | +#include ALIF_CMSIS_H |
| 30 | +#include "hwsem.h" |
| 31 | + |
| 32 | +#include "py/mperrno.h" |
| 33 | +#include "py/mphal.h" |
| 34 | + |
| 35 | +#include "metal/sys.h" |
| 36 | +#include "metal/utilities.h" |
| 37 | +#include "metal/device.h" |
| 38 | + |
| 39 | +struct metal_state _metal; |
| 40 | +static mp_sched_node_t rproc_notify_node; |
| 41 | + |
| 42 | +int metal_sys_init(const struct metal_init_params *params) { |
| 43 | + metal_unused(params); |
| 44 | + |
| 45 | + // Reset the hardware semaphore. |
| 46 | + hwsem_reset(METAL_HSEM_DEVICE); |
| 47 | + #if MICROPY_PY_OPENAMP_HOST |
| 48 | + hwsem_reset(METAL_HSEM_REMOTE); |
| 49 | + #endif |
| 50 | + |
| 51 | + // Enable the hardware semaphore IRQ. |
| 52 | + NVIC_ClearPendingIRQ(METAL_HSEM_IRQn); |
| 53 | + NVIC_SetPriority(METAL_HSEM_IRQn, IRQ_PRI_HWSEM); |
| 54 | + NVIC_EnableIRQ(METAL_HSEM_IRQn); |
| 55 | + |
| 56 | + // If cache management is not enabled, configure the MPU to disable |
| 57 | + // caching for the entire Open-AMP shared memory region. |
| 58 | + #ifndef VIRTIO_USE_DCACHE |
| 59 | + ARM_MPU_Disable(); |
| 60 | + // NOTE: The startup code uses the first 4 attributes. |
| 61 | + #define MEMATTR_IDX_NORMAL_NON_CACHEABLE 4 |
| 62 | + ARM_MPU_SetMemAttr(MEMATTR_IDX_NORMAL_NON_CACHEABLE, ARM_MPU_ATTR( |
| 63 | + ARM_MPU_ATTR_NON_CACHEABLE, |
| 64 | + ARM_MPU_ATTR_NON_CACHEABLE)); |
| 65 | + MPU->RNR = METAL_MPU_REGION_ID; |
| 66 | + MPU->RBAR = ARM_MPU_RBAR(METAL_MPU_REGION_BASE, ARM_MPU_SH_NON, 0, 1, 0); // RO-0, NP-1, XN-0 |
| 67 | + MPU->RLAR = ARM_MPU_RLAR(METAL_MPU_REGION_BASE + METAL_MPU_REGION_SIZE - 1, MEMATTR_IDX_NORMAL_NON_CACHEABLE); |
| 68 | + ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_HFNMIENA_Msk); |
| 69 | + #endif |
| 70 | + |
| 71 | + metal_bus_register(&metal_generic_bus); |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +void metal_sys_finish(void) { |
| 77 | + NVIC_DisableIRQ(METAL_HSEM_IRQn); |
| 78 | + NVIC_ClearPendingIRQ(METAL_HSEM_IRQn); |
| 79 | + hwsem_reset(METAL_HSEM_DEVICE); |
| 80 | + metal_bus_unregister(&metal_generic_bus); |
| 81 | +} |
| 82 | + |
| 83 | +unsigned int sys_irq_save_disable(void) { |
| 84 | + return disable_irq(); |
| 85 | +} |
| 86 | + |
| 87 | +void sys_irq_restore_enable(unsigned int state) { |
| 88 | + enable_irq(state); |
| 89 | +} |
| 90 | + |
| 91 | +void *metal_machine_io_mem_map(void *va, metal_phys_addr_t pa, |
| 92 | + size_t size, unsigned int flags) { |
| 93 | + metal_unused(pa); |
| 94 | + metal_unused(size); |
| 95 | + metal_unused(flags); |
| 96 | + return va; |
| 97 | +} |
| 98 | + |
| 99 | +void metal_machine_cache_flush(void *addr, unsigned int len) { |
| 100 | + SCB_CleanDCache_by_Addr(addr, len); |
| 101 | +} |
| 102 | + |
| 103 | +void metal_machine_cache_invalidate(void *addr, unsigned int len) { |
| 104 | + SCB_InvalidateDCache_by_Addr(addr, len); |
| 105 | +} |
| 106 | + |
| 107 | +int metal_rproc_notify(void *priv, uint32_t id) { |
| 108 | + // Release the HW semaphore to notify the other core. |
| 109 | + hwsem_release(METAL_HSEM_REMOTE, HWSEM_MASTERID); |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | +void METAL_HSEM_IRQ_HANDLER(void) { |
| 114 | + // Schedule the node only if the other core released the Semaphore. |
| 115 | + if (METAL_HSEM_DEVICE->HWSEM_REL_REG == 0) { |
| 116 | + mp_sched_schedule_node(&rproc_notify_node, openamp_remoteproc_notified); |
| 117 | + } |
| 118 | + hwsem_request(METAL_HSEM_DEVICE, METAL_HSEM_REMOTE_ID); |
| 119 | +} |
0 commit comments