|
| 1 | +/********************************************************************** |
| 2 | + Copyright (c) 2025 Institute of Software Chinese Academy of Sciences (ISCAS). |
| 3 | +
|
| 4 | + Redistribution and use in source and binary forms, with or without |
| 5 | + modification, are permitted provided that the following conditions |
| 6 | + are met: |
| 7 | + * Redistributions of source code must retain the above copyright |
| 8 | + notice, this list of conditions and the following disclaimer. |
| 9 | + * Redistributions in binary form must reproduce the above copyright |
| 10 | + notice, this list of conditions and the following disclaimer in |
| 11 | + the documentation and/or other materials provided with the |
| 12 | + distribution. |
| 13 | + * Neither the name of ISCAS Corporation nor the names of its |
| 14 | + contributors may be used to endorse or promote products derived |
| 15 | + from this software without specific prior written permission. |
| 16 | +
|
| 17 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 | + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | +**********************************************************************/ |
| 29 | +#ifndef __RISCV_MULTIBINARY_H__ |
| 30 | +#define __RISCV_MULTIBINARY_H__ |
| 31 | +#ifndef __riscv |
| 32 | +#error "This file is for riscv only" |
| 33 | +#endif |
| 34 | + |
| 35 | +#ifdef __ASSEMBLY__ |
| 36 | + |
| 37 | +/** |
| 38 | + * # mbin_interface : the wrapper layer for isal-l api |
| 39 | + * |
| 40 | + * ## references: |
| 41 | + * * aarch64_multibinary.h |
| 42 | + * ## Usage: |
| 43 | + * |
| 44 | + * 1. Define dispather function |
| 45 | + * 2. name must be \name\()_dispatcher |
| 46 | + * 3. Prototype should be *"void * \name\()_dispatcher"* |
| 47 | + * 4. The dispather should return the right function pointer , revision and a string information . |
| 48 | + **/ |
| 49 | +.macro mbin_interface name:req |
| 50 | + .section .data |
| 51 | + .align 3 |
| 52 | + .global \name\()_dispatcher_info |
| 53 | + .type \name\()_dispatcher_info, @object |
| 54 | +\name\()_dispatcher_info: |
| 55 | + .quad \name\()_mbinit |
| 56 | + .section .text |
| 57 | + .global \name\()_mbinit |
| 58 | +\name\()_mbinit: |
| 59 | + addi sp, sp, -32 |
| 60 | + sd ra, 24(sp) |
| 61 | + sd a0, 0(sp) |
| 62 | + sd a1, 8(sp) |
| 63 | + sd a2, 16(sp) |
| 64 | + call \name\()_dispatcher |
| 65 | + mv t2, a0 |
| 66 | + la t0, \name\()_dispatcher_info |
| 67 | + sd a0, 0(t0) |
| 68 | + ld ra, 24(sp) |
| 69 | + ld a0, 0(sp) |
| 70 | + ld a1, 8(sp) |
| 71 | + ld a2, 16(sp) |
| 72 | + addi sp, sp, 32 |
| 73 | + jr t2 |
| 74 | +.global \name\() |
| 75 | +.type \name,%function |
| 76 | +\name\(): |
| 77 | + la t0, \name\()_dispatcher_info |
| 78 | + ld t1, 0(t0) |
| 79 | + jr t1 |
| 80 | +.size \name,. - \name |
| 81 | +.endm |
| 82 | + |
| 83 | +/** |
| 84 | + * mbin_interface_base is used for the interfaces which have only |
| 85 | + * noarch implementation |
| 86 | + */ |
| 87 | +.macro mbin_interface_base name:req, base:req |
| 88 | + .extern \base |
| 89 | + .data |
| 90 | + .align 3 |
| 91 | + .global \name\()_dispatcher_info |
| 92 | + .type \name\()_dispatcher_info, @object |
| 93 | +\name\()_dispatcher_info: |
| 94 | + .dword \base |
| 95 | + .text |
| 96 | + .global \name |
| 97 | + .type \name, @function |
| 98 | +\name: |
| 99 | + la t0, \name\()_dispatcher_info |
| 100 | + ld t0, (t0) |
| 101 | + jr t0 |
| 102 | +.endm |
| 103 | +#else /* __ASSEMBLY__ */ |
| 104 | +#include <sys/auxv.h> |
| 105 | +#define HWCAP_RV(letter) (1ul << ((letter) - 'A')) |
| 106 | + |
| 107 | +#define DEFINE_INTERFACE_DISPATCHER(name) \ |
| 108 | + void * name##_dispatcher(void) |
| 109 | + |
| 110 | +#define PROVIDER_BASIC(name) \ |
| 111 | + PROVIDER_INFO(name##_base) |
| 112 | + |
| 113 | +#define DO_DIGNOSTIC(x) _Pragma GCC diagnostic ignored "-W"#x |
| 114 | +#define DO_PRAGMA(x) _Pragma (#x) |
| 115 | +#define DIGNOSTIC_IGNORE(x) DO_PRAGMA(GCC diagnostic ignored #x) |
| 116 | +#define DIGNOSTIC_PUSH() DO_PRAGMA(GCC diagnostic push) |
| 117 | +#define DIGNOSTIC_POP() DO_PRAGMA(GCC diagnostic pop) |
| 118 | + |
| 119 | + |
| 120 | +#define PROVIDER_INFO(_func_entry) \ |
| 121 | + ({ DIGNOSTIC_PUSH() \ |
| 122 | + DIGNOSTIC_IGNORE(-Wnested-externs) \ |
| 123 | + extern void _func_entry(void); \ |
| 124 | + DIGNOSTIC_POP() \ |
| 125 | + _func_entry; \ |
| 126 | + }) |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +#endif /* __ASSEMBLY__ */ |
| 131 | +#endif /* __RISCV_MULTIBINARY_H__ */ |
0 commit comments