2019-06-03 07:44:50 +02:00
// SPDX-License-Identifier: GPL-2.0-only
2017-07-10 18:07:09 -07:00
/*
* Copied from arch / arm64 / kernel / cpufeature . c
*
* Copyright ( C ) 2015 ARM Ltd .
* Copyright ( C ) 2017 SiFive
*/
2023-05-15 11:19:21 +05:30
# include <linux/acpi.h>
2020-04-24 10:29:27 +05:30
# include <linux/bitmap.h>
2022-03-14 13:38:41 -07:00
# include <linux/ctype.h>
2022-11-29 15:34:47 +01:00
# include <linux/log2.h>
2023-02-12 19:47:36 +00:00
# include <linux/memory.h>
2022-05-11 21:29:18 +02:00
# include <linux/module.h>
2017-07-10 18:07:09 -07:00
# include <linux/of.h>
2023-05-15 11:19:21 +05:30
# include <asm/acpi.h>
2022-05-11 21:29:18 +02:00
# include <asm/alternative.h>
2022-07-07 01:15:35 +02:00
# include <asm/cacheflush.h>
2023-04-20 19:30:16 +01:00
# include <asm/cpufeature.h>
2017-07-10 18:07:09 -07:00
# include <asm/hwcap.h>
RISC-V: Probe for unaligned access speed
Rather than deferring unaligned access speed determinations to a vendor
function, let's probe them and find out how fast they are. If we
determine that an unaligned word access is faster than N byte accesses,
mark the hardware's unaligned access as "fast". Otherwise, we mark
accesses as slow.
The algorithm itself runs for a fixed amount of jiffies. Within each
iteration it attempts to time a single loop, and then keeps only the best
(fastest) loop it saw. This algorithm was found to have lower variance from
run to run than my first attempt, which counted the total number of
iterations that could be done in that fixed amount of jiffies. By taking
only the best iteration in the loop, assuming at least one loop wasn't
perturbed by an interrupt, we eliminate the effects of interrupts and
other "warm up" factors like branch prediction. The only downside is it
depends on having an rdtime granular and accurate enough to measure a
single copy. If we ever manage to complete a loop in 0 rdtime ticks, we
leave the unaligned setting at UNKNOWN.
There is a slight change in user-visible behavior here. Previously, all
boards except the THead C906 reported misaligned access speed of
UNKNOWN. C906 reported FAST. With this change, since we're now measuring
misaligned access speed on each hart, all RISC-V systems will have this
key set as either FAST or SLOW.
Currently, we don't have a way to confidently measure the difference between
SLOW and EMULATED, so we label anything not fast as SLOW. This will
mislabel some systems that are actually EMULATED as SLOW. When we get
support for delegating misaligned access traps to the kernel (as opposed
to the firmware quietly handling it), we can explicitly test in Linux to
see if unaligned accesses trap. Those systems will start to report
EMULATED, though older (today's) systems without that new SBI mechanism
will continue to report SLOW.
I've updated the documentation for those hwprobe values to reflect
this, specifically: SLOW may or may not be emulated by software, and FAST
represents means being faster than equivalent byte accesses. The change
in documentation is accurate with respect to both the former and current
behavior.
Signed-off-by: Evan Green <evan@rivosinc.com>
Acked-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230818194136.4084400-2-evan@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-08-18 12:41:35 -07:00
# include <asm/hwprobe.h>
2022-05-11 21:29:18 +02:00
# include <asm/patch.h>
# include <asm/processor.h>
2023-06-05 11:07:05 +00:00
# include <asm/vector.h>
2017-07-10 18:07:09 -07:00
RISC-V: Probe for unaligned access speed
Rather than deferring unaligned access speed determinations to a vendor
function, let's probe them and find out how fast they are. If we
determine that an unaligned word access is faster than N byte accesses,
mark the hardware's unaligned access as "fast". Otherwise, we mark
accesses as slow.
The algorithm itself runs for a fixed amount of jiffies. Within each
iteration it attempts to time a single loop, and then keeps only the best
(fastest) loop it saw. This algorithm was found to have lower variance from
run to run than my first attempt, which counted the total number of
iterations that could be done in that fixed amount of jiffies. By taking
only the best iteration in the loop, assuming at least one loop wasn't
perturbed by an interrupt, we eliminate the effects of interrupts and
other "warm up" factors like branch prediction. The only downside is it
depends on having an rdtime granular and accurate enough to measure a
single copy. If we ever manage to complete a loop in 0 rdtime ticks, we
leave the unaligned setting at UNKNOWN.
There is a slight change in user-visible behavior here. Previously, all
boards except the THead C906 reported misaligned access speed of
UNKNOWN. C906 reported FAST. With this change, since we're now measuring
misaligned access speed on each hart, all RISC-V systems will have this
key set as either FAST or SLOW.
Currently, we don't have a way to confidently measure the difference between
SLOW and EMULATED, so we label anything not fast as SLOW. This will
mislabel some systems that are actually EMULATED as SLOW. When we get
support for delegating misaligned access traps to the kernel (as opposed
to the firmware quietly handling it), we can explicitly test in Linux to
see if unaligned accesses trap. Those systems will start to report
EMULATED, though older (today's) systems without that new SBI mechanism
will continue to report SLOW.
I've updated the documentation for those hwprobe values to reflect
this, specifically: SLOW may or may not be emulated by software, and FAST
represents means being faster than equivalent byte accesses. The change
in documentation is accurate with respect to both the former and current
behavior.
Signed-off-by: Evan Green <evan@rivosinc.com>
Acked-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230818194136.4084400-2-evan@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-08-18 12:41:35 -07:00
# include "copy-unaligned.h"
2022-03-14 13:38:40 -07:00
# define NUM_ALPHA_EXTS ('z' - 'a' + 1)
RISC-V: Probe for unaligned access speed
Rather than deferring unaligned access speed determinations to a vendor
function, let's probe them and find out how fast they are. If we
determine that an unaligned word access is faster than N byte accesses,
mark the hardware's unaligned access as "fast". Otherwise, we mark
accesses as slow.
The algorithm itself runs for a fixed amount of jiffies. Within each
iteration it attempts to time a single loop, and then keeps only the best
(fastest) loop it saw. This algorithm was found to have lower variance from
run to run than my first attempt, which counted the total number of
iterations that could be done in that fixed amount of jiffies. By taking
only the best iteration in the loop, assuming at least one loop wasn't
perturbed by an interrupt, we eliminate the effects of interrupts and
other "warm up" factors like branch prediction. The only downside is it
depends on having an rdtime granular and accurate enough to measure a
single copy. If we ever manage to complete a loop in 0 rdtime ticks, we
leave the unaligned setting at UNKNOWN.
There is a slight change in user-visible behavior here. Previously, all
boards except the THead C906 reported misaligned access speed of
UNKNOWN. C906 reported FAST. With this change, since we're now measuring
misaligned access speed on each hart, all RISC-V systems will have this
key set as either FAST or SLOW.
Currently, we don't have a way to confidently measure the difference between
SLOW and EMULATED, so we label anything not fast as SLOW. This will
mislabel some systems that are actually EMULATED as SLOW. When we get
support for delegating misaligned access traps to the kernel (as opposed
to the firmware quietly handling it), we can explicitly test in Linux to
see if unaligned accesses trap. Those systems will start to report
EMULATED, though older (today's) systems without that new SBI mechanism
will continue to report SLOW.
I've updated the documentation for those hwprobe values to reflect
this, specifically: SLOW may or may not be emulated by software, and FAST
represents means being faster than equivalent byte accesses. The change
in documentation is accurate with respect to both the former and current
behavior.
Signed-off-by: Evan Green <evan@rivosinc.com>
Acked-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230818194136.4084400-2-evan@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-08-18 12:41:35 -07:00
# define MISALIGNED_ACCESS_JIFFIES_LG2 1
# define MISALIGNED_BUFFER_SIZE 0x4000
# define MISALIGNED_COPY_SIZE ((MISALIGNED_BUFFER_SIZE / 2) - 0x80)
2017-07-10 18:07:09 -07:00
unsigned long elf_hwcap __read_mostly ;
2020-04-24 10:29:27 +05:30
/* Host ISA bitmap */
static DECLARE_BITMAP ( riscv_isa , RISCV_ISA_EXT_MAX ) __read_mostly ;
2023-05-09 11:25:02 -07:00
/* Per-cpu ISA extensions. */
struct riscv_isainfo hart_isa [ NR_CPUS ] ;
2023-04-07 16:11:01 -07:00
/* Performance information */
DEFINE_PER_CPU ( long , misaligned_access_speed ) ;
2020-04-24 10:29:27 +05:30
/**
* riscv_isa_extension_base ( ) - Get base extension word
*
* @ isa_bitmap : ISA bitmap to use
* Return : base extension word as unsigned long value
*
* NOTE : If isa_bitmap is NULL then Host ISA bitmap will be used .
*/
unsigned long riscv_isa_extension_base ( const unsigned long * isa_bitmap )
{
if ( ! isa_bitmap )
return riscv_isa [ 0 ] ;
return isa_bitmap [ 0 ] ;
}
EXPORT_SYMBOL_GPL ( riscv_isa_extension_base ) ;
/**
* __riscv_isa_extension_available ( ) - Check whether given extension
* is available or not
*
* @ isa_bitmap : ISA bitmap to use
* @ bit : bit position of the desired extension
* Return : true or false
*
* NOTE : If isa_bitmap is NULL then Host ISA bitmap will be used .
*/
bool __riscv_isa_extension_available ( const unsigned long * isa_bitmap , int bit )
{
const unsigned long * bmap = ( isa_bitmap ) ? isa_bitmap : riscv_isa ;
if ( bit > = RISCV_ISA_EXT_MAX )
return false ;
return test_bit ( bit , bmap ) ? true : false ;
}
EXPORT_SYMBOL_GPL ( __riscv_isa_extension_available ) ;
2022-11-29 15:34:46 +01:00
static bool riscv_isa_extension_check ( int id )
{
2022-11-29 15:34:47 +01:00
switch ( id ) {
case RISCV_ISA_EXT_ZICBOM :
if ( ! riscv_cbom_block_size ) {
2023-03-17 13:45:12 +00:00
pr_err ( " Zicbom detected in ISA string, disabling as no cbom-block-size found \n " ) ;
2022-11-29 15:34:47 +01:00
return false ;
} else if ( ! is_power_of_2 ( riscv_cbom_block_size ) ) {
2023-03-17 13:45:12 +00:00
pr_err ( " Zicbom disabled as cbom-block-size present, but is not a power-of-2 \n " ) ;
2022-11-29 15:34:47 +01:00
return false ;
}
return true ;
2023-02-24 17:26:27 +01:00
case RISCV_ISA_EXT_ZICBOZ :
if ( ! riscv_cboz_block_size ) {
2023-09-18 15:15:20 +02:00
pr_err ( " Zicboz detected in ISA string, disabling as no cboz-block-size found \n " ) ;
2023-02-24 17:26:27 +01:00
return false ;
} else if ( ! is_power_of_2 ( riscv_cboz_block_size ) ) {
2023-09-18 15:15:20 +02:00
pr_err ( " Zicboz disabled as cboz-block-size present, but is not a power-of-2 \n " ) ;
2023-02-24 17:26:27 +01:00
return false ;
}
return true ;
2022-11-29 15:34:47 +01:00
}
2022-11-29 15:34:46 +01:00
return true ;
}
2023-07-13 13:11:03 +01:00
# define __RISCV_ISA_EXT_DATA(_name, _id) { \
. name = # _name , \
2023-07-13 13:11:07 +01:00
. property = # _name , \
2023-07-13 13:11:03 +01:00
. id = _id , \
}
2023-07-13 13:11:02 +01:00
/*
* The canonical order of ISA extension names in the ISA string is defined in
* chapter 27 of the unprivileged specification .
*
* Ordinarily , for in - kernel data structures , this order is unimportant but
* isa_ext_arr defines the order of the ISA string in / proc / cpuinfo .
*
* The specification uses vague wording , such as should , when it comes to
* ordering , so for our purposes the following rules apply :
*
* 1. All multi - letter extensions must be separated from other extensions by an
* underscore .
*
* 2. Additional standard extensions ( starting with ' Z ' ) must be sorted after
* single - letter extensions and before any higher - privileged extensions .
*
* 3. The first letter following the ' Z ' conventionally indicates the most
* closely related alphabetical extension category , IMAFDQLCBKJTPVH .
* If multiple ' Z ' extensions are named , they must be ordered first by
* category , then alphabetically within a category .
*
* 3. Standard supervisor - level extensions ( starting with ' S ' ) must be listed
* after standard unprivileged extensions . If multiple supervisor - level
* extensions are listed , they must be ordered alphabetically .
*
* 4. Standard machine - level extensions ( starting with ' Zxm ' ) must be listed
* after any lower - privileged , standard extensions . If multiple
* machine - level extensions are listed , they must be ordered
* alphabetically .
*
* 5. Non - standard extensions ( starting with ' X ' ) must be listed after all
* standard extensions . If multiple non - standard extensions are listed , they
* must be ordered alphabetically .
*
* An example string following the order is :
* rv64imadc_zifoo_zigoo_zafoo_sbar_scar_zxmbaz_xqux_xrux
*
* New entries to this struct should follow the ordering rules described above .
*/
const struct riscv_isa_ext_data riscv_isa_ext [ ] = {
2023-07-13 13:11:05 +01:00
__RISCV_ISA_EXT_DATA ( i , RISCV_ISA_EXT_i ) ,
__RISCV_ISA_EXT_DATA ( m , RISCV_ISA_EXT_m ) ,
__RISCV_ISA_EXT_DATA ( a , RISCV_ISA_EXT_a ) ,
__RISCV_ISA_EXT_DATA ( f , RISCV_ISA_EXT_f ) ,
__RISCV_ISA_EXT_DATA ( d , RISCV_ISA_EXT_d ) ,
__RISCV_ISA_EXT_DATA ( q , RISCV_ISA_EXT_q ) ,
__RISCV_ISA_EXT_DATA ( c , RISCV_ISA_EXT_c ) ,
__RISCV_ISA_EXT_DATA ( b , RISCV_ISA_EXT_b ) ,
__RISCV_ISA_EXT_DATA ( k , RISCV_ISA_EXT_k ) ,
__RISCV_ISA_EXT_DATA ( j , RISCV_ISA_EXT_j ) ,
__RISCV_ISA_EXT_DATA ( p , RISCV_ISA_EXT_p ) ,
__RISCV_ISA_EXT_DATA ( v , RISCV_ISA_EXT_v ) ,
__RISCV_ISA_EXT_DATA ( h , RISCV_ISA_EXT_h ) ,
2023-07-13 13:11:02 +01:00
__RISCV_ISA_EXT_DATA ( zicbom , RISCV_ISA_EXT_ZICBOM ) ,
__RISCV_ISA_EXT_DATA ( zicboz , RISCV_ISA_EXT_ZICBOZ ) ,
__RISCV_ISA_EXT_DATA ( zicntr , RISCV_ISA_EXT_ZICNTR ) ,
__RISCV_ISA_EXT_DATA ( zicsr , RISCV_ISA_EXT_ZICSR ) ,
__RISCV_ISA_EXT_DATA ( zifencei , RISCV_ISA_EXT_ZIFENCEI ) ,
__RISCV_ISA_EXT_DATA ( zihintpause , RISCV_ISA_EXT_ZIHINTPAUSE ) ,
__RISCV_ISA_EXT_DATA ( zihpm , RISCV_ISA_EXT_ZIHPM ) ,
__RISCV_ISA_EXT_DATA ( zba , RISCV_ISA_EXT_ZBA ) ,
__RISCV_ISA_EXT_DATA ( zbb , RISCV_ISA_EXT_ZBB ) ,
__RISCV_ISA_EXT_DATA ( zbs , RISCV_ISA_EXT_ZBS ) ,
__RISCV_ISA_EXT_DATA ( smaia , RISCV_ISA_EXT_SMAIA ) ,
__RISCV_ISA_EXT_DATA ( ssaia , RISCV_ISA_EXT_SSAIA ) ,
__RISCV_ISA_EXT_DATA ( sscofpmf , RISCV_ISA_EXT_SSCOFPMF ) ,
__RISCV_ISA_EXT_DATA ( sstc , RISCV_ISA_EXT_SSTC ) ,
__RISCV_ISA_EXT_DATA ( svinval , RISCV_ISA_EXT_SVINVAL ) ,
__RISCV_ISA_EXT_DATA ( svnapot , RISCV_ISA_EXT_SVNAPOT ) ,
__RISCV_ISA_EXT_DATA ( svpbmt , RISCV_ISA_EXT_SVPBMT ) ,
} ;
const size_t riscv_isa_ext_count = ARRAY_SIZE ( riscv_isa_ext ) ;
2023-07-13 13:11:06 +01:00
static void __init riscv_parse_isa_string ( unsigned long * this_hwcap , struct riscv_isainfo * isainfo ,
unsigned long * isa2hwcap , const char * isa )
{
/*
* For all possible cpus , we have already validated in
* the boot process that they at least contain " rv " and
* whichever of " 32 " / " 64 " this kernel supports , and so this
* section can be skipped .
*/
isa + = 4 ;
while ( * isa ) {
const char * ext = isa + + ;
const char * ext_end = isa ;
bool ext_long = false , ext_err = false ;
switch ( * ext ) {
case ' s ' :
/*
2023-07-26 05:44:16 +00:00
* Workaround for invalid single - letter ' s ' & ' u ' ( QEMU ) .
2023-07-13 13:11:06 +01:00
* No need to set the bit in riscv_isa as ' s ' & ' u ' are
2023-07-26 05:44:16 +00:00
* not valid ISA extensions . It works unless the first
* multi - letter extension in the ISA string begins with
* " Su " and is not prefixed with an underscore .
2023-07-13 13:11:06 +01:00
*/
if ( ext [ - 1 ] ! = ' _ ' & & ext [ 1 ] = = ' u ' ) {
+ + isa ;
ext_err = true ;
break ;
}
fallthrough ;
case ' S ' :
case ' x ' :
case ' X ' :
case ' z ' :
case ' Z ' :
/*
* Before attempting to parse the extension itself , we find its end .
* As multi - letter extensions must be split from other multi - letter
* extensions with an " _ " , the end of a multi - letter extension will
* either be the null character or the " _ " at the start of the next
* multi - letter extension .
*
* Next , as the extensions version is currently ignored , we
* eliminate that portion . This is done by parsing backwards from
* the end of the extension , removing any numbers . This may be a
* major or minor number however , so the process is repeated if a
* minor number was found .
*
* ext_end is intended to represent the first character * after * the
* name portion of an extension , but will be decremented to the last
* character itself while eliminating the extensions version number .
* A simple re - increment solves this problem .
*/
ext_long = true ;
for ( ; * isa & & * isa ! = ' _ ' ; + + isa )
if ( unlikely ( ! isalnum ( * isa ) ) )
ext_err = true ;
ext_end = isa ;
if ( unlikely ( ext_err ) )
break ;
if ( ! isdigit ( ext_end [ - 1 ] ) )
break ;
while ( isdigit ( * - - ext_end ) )
;
if ( tolower ( ext_end [ 0 ] ) ! = ' p ' | | ! isdigit ( ext_end [ - 1 ] ) ) {
+ + ext_end ;
break ;
}
while ( isdigit ( * - - ext_end ) )
;
+ + ext_end ;
break ;
default :
/*
* Things are a little easier for single - letter extensions , as they
* are parsed forwards .
*
* After checking that our starting position is valid , we need to
* ensure that , when isa was incremented at the start of the loop ,
* that it arrived at the start of the next extension .
*
* If we are already on a non - digit , there is nothing to do . Either
* we have a multi - letter extension ' s _ , or the start of an
* extension .
*
* Otherwise we have found the current extension ' s major version
* number . Parse past it , and a subsequent p / minor version number
* if present . The ` p ` extension must not appear immediately after
* a number , so there is no fear of missing it .
*
*/
if ( unlikely ( ! isalpha ( * ext ) ) ) {
ext_err = true ;
break ;
}
if ( ! isdigit ( * isa ) )
break ;
while ( isdigit ( * + + isa ) )
;
if ( tolower ( * isa ) ! = ' p ' )
break ;
if ( ! isdigit ( * + + isa ) ) {
- - isa ;
break ;
}
while ( isdigit ( * + + isa ) )
;
break ;
}
/*
* The parser expects that at the start of an iteration isa points to the
* first character of the next extension . As we stop parsing an extension
* on meeting a non - alphanumeric character , an extra increment is needed
* where the succeeding extension is a multi - letter prefixed with an " _ " .
*/
if ( * isa = = ' _ ' )
+ + isa ;
# define SET_ISA_EXT_MAP(name, bit) \
do { \
if ( ( ext_end - ext = = strlen ( name ) ) & & \
! strncasecmp ( ext , name , strlen ( name ) ) & & \
riscv_isa_extension_check ( bit ) ) \
set_bit ( bit , isainfo - > isa ) ; \
} while ( false ) \
if ( unlikely ( ext_err ) )
continue ;
if ( ! ext_long ) {
int nr = tolower ( * ext ) - ' a ' ;
if ( riscv_isa_extension_check ( nr ) ) {
* this_hwcap | = isa2hwcap [ nr ] ;
set_bit ( nr , isainfo - > isa ) ;
}
} else {
for ( int i = 0 ; i < riscv_isa_ext_count ; i + + )
SET_ISA_EXT_MAP ( riscv_isa_ext [ i ] . name ,
riscv_isa_ext [ i ] . id ) ;
}
# undef SET_ISA_EXT_MAP
}
}
static void __init riscv_fill_hwcap_from_isa_string ( unsigned long * isa2hwcap )
2017-07-10 18:07:09 -07:00
{
2019-01-18 15:03:08 +01:00
struct device_node * node ;
2017-07-10 18:07:09 -07:00
const char * isa ;
2023-07-13 13:11:06 +01:00
int rc ;
2023-05-15 11:19:21 +05:30
struct acpi_table_header * rhct ;
acpi_status status ;
2023-05-15 11:19:20 +05:30
unsigned int cpu ;
2017-07-10 18:07:09 -07:00
2023-05-15 11:19:21 +05:30
if ( ! acpi_disabled ) {
status = acpi_get_table ( ACPI_SIG_RHCT , 0 , & rhct ) ;
if ( ACPI_FAILURE ( status ) )
return ;
}
2023-05-15 11:19:20 +05:30
for_each_possible_cpu ( cpu ) {
2023-06-19 14:34:40 -07:00
struct riscv_isainfo * isainfo = & hart_isa [ cpu ] ;
2019-02-22 11:41:40 -08:00
unsigned long this_hwcap = 0 ;
2017-07-10 18:07:09 -07:00
2023-05-15 11:19:21 +05:30
if ( acpi_disabled ) {
node = of_cpu_device_node_get ( cpu ) ;
if ( ! node ) {
pr_warn ( " Unable to find cpu node \n " ) ;
continue ;
}
2017-07-10 18:07:09 -07:00
2023-05-15 11:19:21 +05:30
rc = of_property_read_string ( node , " riscv,isa " , & isa ) ;
of_node_put ( node ) ;
if ( rc ) {
pr_warn ( " Unable to find \" riscv,isa \" devicetree entry \n " ) ;
continue ;
}
} else {
rc = acpi_get_riscv_isa ( rhct , cpu , & isa ) ;
if ( rc < 0 ) {
pr_warn ( " Unable to get ISA for the hart - %d \n " , cpu ) ;
continue ;
}
2019-02-22 11:41:40 -08:00
}
2023-07-13 13:11:06 +01:00
riscv_parse_isa_string ( & this_hwcap , isainfo , isa2hwcap , isa ) ;
2019-02-22 11:41:40 -08:00
2023-06-07 21:28:31 +01:00
/*
* These ones were as they were part of the base ISA when the
* port & dt - bindings were upstreamed , and so can be set
* unconditionally where ` i ` is in riscv , isa on DT systems .
*/
if ( acpi_disabled ) {
2023-07-11 15:46:00 -07:00
set_bit ( RISCV_ISA_EXT_ZICSR , isainfo - > isa ) ;
set_bit ( RISCV_ISA_EXT_ZIFENCEI , isainfo - > isa ) ;
2023-06-21 07:49:09 -07:00
set_bit ( RISCV_ISA_EXT_ZICNTR , isainfo - > isa ) ;
set_bit ( RISCV_ISA_EXT_ZIHPM , isainfo - > isa ) ;
2023-06-07 21:28:31 +01:00
}
2019-02-22 11:41:40 -08:00
/*
* All " okay " hart should have same isa . Set HWCAP based on
* common capabilities of every " okay " hart , in case they don ' t
* have .
*/
if ( elf_hwcap )
elf_hwcap & = this_hwcap ;
else
elf_hwcap = this_hwcap ;
2020-04-24 10:29:27 +05:30
2022-05-08 07:49:50 -07:00
if ( bitmap_empty ( riscv_isa , RISCV_ISA_EXT_MAX ) )
2023-05-09 11:25:02 -07:00
bitmap_copy ( riscv_isa , isainfo - > isa , RISCV_ISA_EXT_MAX ) ;
2022-05-08 07:49:50 -07:00
else
2023-05-09 11:25:02 -07:00
bitmap_and ( riscv_isa , riscv_isa , isainfo - > isa , RISCV_ISA_EXT_MAX ) ;
2019-02-22 11:41:40 -08:00
}
2017-07-10 18:07:09 -07:00
2023-05-15 11:19:21 +05:30
if ( ! acpi_disabled & & rhct )
acpi_put_table ( ( struct acpi_table_header * ) rhct ) ;
2023-07-13 13:11:06 +01:00
}
2023-07-13 13:11:07 +01:00
static int __init riscv_fill_hwcap_from_ext_list ( unsigned long * isa2hwcap )
{
unsigned int cpu ;
for_each_possible_cpu ( cpu ) {
unsigned long this_hwcap = 0 ;
struct device_node * cpu_node ;
struct riscv_isainfo * isainfo = & hart_isa [ cpu ] ;
cpu_node = of_cpu_device_node_get ( cpu ) ;
if ( ! cpu_node ) {
pr_warn ( " Unable to find cpu node \n " ) ;
continue ;
}
if ( ! of_property_present ( cpu_node , " riscv,isa-extensions " ) ) {
of_node_put ( cpu_node ) ;
continue ;
}
for ( int i = 0 ; i < riscv_isa_ext_count ; i + + ) {
if ( of_property_match_string ( cpu_node , " riscv,isa-extensions " ,
riscv_isa_ext [ i ] . property ) < 0 )
continue ;
if ( ! riscv_isa_extension_check ( riscv_isa_ext [ i ] . id ) )
continue ;
/* Only single letter extensions get set in hwcap */
if ( strnlen ( riscv_isa_ext [ i ] . name , 2 ) = = 1 )
this_hwcap | = isa2hwcap [ riscv_isa_ext [ i ] . id ] ;
set_bit ( riscv_isa_ext [ i ] . id , isainfo - > isa ) ;
}
of_node_put ( cpu_node ) ;
/*
* All " okay " harts should have same isa . Set HWCAP based on
* common capabilities of every " okay " hart , in case they don ' t .
*/
if ( elf_hwcap )
elf_hwcap & = this_hwcap ;
else
elf_hwcap = this_hwcap ;
if ( bitmap_empty ( riscv_isa , RISCV_ISA_EXT_MAX ) )
bitmap_copy ( riscv_isa , isainfo - > isa , RISCV_ISA_EXT_MAX ) ;
else
bitmap_and ( riscv_isa , riscv_isa , isainfo - > isa , RISCV_ISA_EXT_MAX ) ;
}
if ( bitmap_empty ( riscv_isa , RISCV_ISA_EXT_MAX ) )
return - ENOENT ;
return 0 ;
}
2023-07-13 13:11:09 +01:00
# ifdef CONFIG_RISCV_ISA_FALLBACK
bool __initdata riscv_isa_fallback = true ;
# else
bool __initdata riscv_isa_fallback ;
static int __init riscv_isa_fallback_setup ( char * __unused )
{
riscv_isa_fallback = true ;
return 1 ;
}
early_param ( " riscv_isa_fallback " , riscv_isa_fallback_setup ) ;
# endif
2023-07-13 13:11:06 +01:00
void __init riscv_fill_hwcap ( void )
{
char print_str [ NUM_ALPHA_EXTS + 1 ] ;
unsigned long isa2hwcap [ 26 ] = { 0 } ;
2023-07-13 13:11:07 +01:00
int i , j ;
2023-07-13 13:11:06 +01:00
isa2hwcap [ ' i ' - ' a ' ] = COMPAT_HWCAP_ISA_I ;
isa2hwcap [ ' m ' - ' a ' ] = COMPAT_HWCAP_ISA_M ;
isa2hwcap [ ' a ' - ' a ' ] = COMPAT_HWCAP_ISA_A ;
isa2hwcap [ ' f ' - ' a ' ] = COMPAT_HWCAP_ISA_F ;
isa2hwcap [ ' d ' - ' a ' ] = COMPAT_HWCAP_ISA_D ;
isa2hwcap [ ' c ' - ' a ' ] = COMPAT_HWCAP_ISA_C ;
isa2hwcap [ ' v ' - ' a ' ] = COMPAT_HWCAP_ISA_V ;
2023-07-13 13:11:07 +01:00
if ( ! acpi_disabled ) {
riscv_fill_hwcap_from_isa_string ( isa2hwcap ) ;
} else {
int ret = riscv_fill_hwcap_from_ext_list ( isa2hwcap ) ;
2023-05-15 11:19:21 +05:30
2023-07-13 13:11:09 +01:00
if ( ret & & riscv_isa_fallback ) {
2023-07-13 13:11:07 +01:00
pr_info ( " Falling back to deprecated \" riscv,isa \" \n " ) ;
riscv_fill_hwcap_from_isa_string ( isa2hwcap ) ;
}
}
2023-05-15 11:19:21 +05:30
2023-07-13 13:11:07 +01:00
/*
* We don ' t support systems with F but without D , so mask those out
* here .
*/
2018-08-27 14:42:53 -07:00
if ( ( elf_hwcap & COMPAT_HWCAP_ISA_F ) & & ! ( elf_hwcap & COMPAT_HWCAP_ISA_D ) ) {
2019-01-18 15:03:04 +01:00
pr_info ( " This kernel does not support systems with F but not D \n " ) ;
2018-08-27 14:42:53 -07:00
elf_hwcap & = ~ COMPAT_HWCAP_ISA_F ;
}
2023-06-05 11:06:59 +00:00
if ( elf_hwcap & COMPAT_HWCAP_ISA_V ) {
2023-06-05 11:07:05 +00:00
riscv_v_setup_vsize ( ) ;
2023-06-05 11:06:59 +00:00
/*
* ISA string in device tree might have ' v ' flag , but
* CONFIG_RISCV_ISA_V is disabled in kernel .
* Clear V flag in elf_hwcap if CONFIG_RISCV_ISA_V is disabled .
*/
if ( ! IS_ENABLED ( CONFIG_RISCV_ISA_V ) )
elf_hwcap & = ~ COMPAT_HWCAP_ISA_V ;
}
2020-04-24 10:29:27 +05:30
memset ( print_str , 0 , sizeof ( print_str ) ) ;
2022-03-14 13:38:40 -07:00
for ( i = 0 , j = 0 ; i < NUM_ALPHA_EXTS ; i + + )
2020-04-24 10:29:27 +05:30
if ( riscv_isa [ 0 ] & BIT_MASK ( i ) )
print_str [ j + + ] = ( char ) ( ' a ' + i ) ;
2022-03-14 13:38:43 -07:00
pr_info ( " riscv: base ISA extensions %s \n " , print_str ) ;
2020-04-24 10:29:27 +05:30
memset ( print_str , 0 , sizeof ( print_str ) ) ;
2022-03-14 13:38:40 -07:00
for ( i = 0 , j = 0 ; i < NUM_ALPHA_EXTS ; i + + )
2020-04-24 10:29:27 +05:30
if ( elf_hwcap & BIT_MASK ( i ) )
print_str [ j + + ] = ( char ) ( ' a ' + i ) ;
pr_info ( " riscv: ELF capabilities %s \n " , print_str ) ;
2017-07-10 18:07:09 -07:00
}
2022-05-11 21:29:18 +02:00
2023-06-05 11:07:17 +00:00
unsigned long riscv_get_elf_hwcap ( void )
{
2023-06-05 11:07:18 +00:00
unsigned long hwcap ;
hwcap = ( elf_hwcap & ( ( 1UL < < RISCV_ISA_EXT_BASE ) - 1 ) ) ;
if ( ! riscv_v_vstate_ctrl_user_allowed ( ) )
hwcap & = ~ COMPAT_HWCAP_ISA_V ;
return hwcap ;
2023-06-05 11:07:17 +00:00
}
RISC-V: Probe for unaligned access speed
Rather than deferring unaligned access speed determinations to a vendor
function, let's probe them and find out how fast they are. If we
determine that an unaligned word access is faster than N byte accesses,
mark the hardware's unaligned access as "fast". Otherwise, we mark
accesses as slow.
The algorithm itself runs for a fixed amount of jiffies. Within each
iteration it attempts to time a single loop, and then keeps only the best
(fastest) loop it saw. This algorithm was found to have lower variance from
run to run than my first attempt, which counted the total number of
iterations that could be done in that fixed amount of jiffies. By taking
only the best iteration in the loop, assuming at least one loop wasn't
perturbed by an interrupt, we eliminate the effects of interrupts and
other "warm up" factors like branch prediction. The only downside is it
depends on having an rdtime granular and accurate enough to measure a
single copy. If we ever manage to complete a loop in 0 rdtime ticks, we
leave the unaligned setting at UNKNOWN.
There is a slight change in user-visible behavior here. Previously, all
boards except the THead C906 reported misaligned access speed of
UNKNOWN. C906 reported FAST. With this change, since we're now measuring
misaligned access speed on each hart, all RISC-V systems will have this
key set as either FAST or SLOW.
Currently, we don't have a way to confidently measure the difference between
SLOW and EMULATED, so we label anything not fast as SLOW. This will
mislabel some systems that are actually EMULATED as SLOW. When we get
support for delegating misaligned access traps to the kernel (as opposed
to the firmware quietly handling it), we can explicitly test in Linux to
see if unaligned accesses trap. Those systems will start to report
EMULATED, though older (today's) systems without that new SBI mechanism
will continue to report SLOW.
I've updated the documentation for those hwprobe values to reflect
this, specifically: SLOW may or may not be emulated by software, and FAST
represents means being faster than equivalent byte accesses. The change
in documentation is accurate with respect to both the former and current
behavior.
Signed-off-by: Evan Green <evan@rivosinc.com>
Acked-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230818194136.4084400-2-evan@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-08-18 12:41:35 -07:00
void check_unaligned_access ( int cpu )
{
u64 start_cycles , end_cycles ;
u64 word_cycles ;
u64 byte_cycles ;
int ratio ;
unsigned long start_jiffies , now ;
struct page * page ;
void * dst ;
void * src ;
long speed = RISCV_HWPROBE_MISALIGNED_SLOW ;
page = alloc_pages ( GFP_NOWAIT , get_order ( MISALIGNED_BUFFER_SIZE ) ) ;
if ( ! page ) {
pr_warn ( " Can't alloc pages to measure memcpy performance " ) ;
return ;
}
/* Make an unaligned destination buffer. */
dst = ( void * ) ( ( unsigned long ) page_address ( page ) | 0x1 ) ;
/* Unalign src as well, but differently (off by 1 + 2 = 3). */
src = dst + ( MISALIGNED_BUFFER_SIZE / 2 ) ;
src + = 2 ;
word_cycles = - 1ULL ;
/* Do a warmup. */
__riscv_copy_words_unaligned ( dst , src , MISALIGNED_COPY_SIZE ) ;
preempt_disable ( ) ;
start_jiffies = jiffies ;
while ( ( now = jiffies ) = = start_jiffies )
cpu_relax ( ) ;
/*
* For a fixed amount of time , repeatedly try the function , and take
* the best time in cycles as the measurement .
*/
while ( time_before ( jiffies , now + ( 1 < < MISALIGNED_ACCESS_JIFFIES_LG2 ) ) ) {
start_cycles = get_cycles64 ( ) ;
/* Ensure the CSR read can't reorder WRT to the copy. */
mb ( ) ;
__riscv_copy_words_unaligned ( dst , src , MISALIGNED_COPY_SIZE ) ;
/* Ensure the copy ends before the end time is snapped. */
mb ( ) ;
end_cycles = get_cycles64 ( ) ;
if ( ( end_cycles - start_cycles ) < word_cycles )
word_cycles = end_cycles - start_cycles ;
}
byte_cycles = - 1ULL ;
__riscv_copy_bytes_unaligned ( dst , src , MISALIGNED_COPY_SIZE ) ;
start_jiffies = jiffies ;
while ( ( now = jiffies ) = = start_jiffies )
cpu_relax ( ) ;
while ( time_before ( jiffies , now + ( 1 < < MISALIGNED_ACCESS_JIFFIES_LG2 ) ) ) {
start_cycles = get_cycles64 ( ) ;
mb ( ) ;
__riscv_copy_bytes_unaligned ( dst , src , MISALIGNED_COPY_SIZE ) ;
mb ( ) ;
end_cycles = get_cycles64 ( ) ;
if ( ( end_cycles - start_cycles ) < byte_cycles )
byte_cycles = end_cycles - start_cycles ;
}
preempt_enable ( ) ;
/* Don't divide by zero. */
if ( ! word_cycles | | ! byte_cycles ) {
pr_warn ( " cpu%d: rdtime lacks granularity needed to measure unaligned access speed \n " ,
cpu ) ;
goto out ;
}
if ( word_cycles < byte_cycles )
speed = RISCV_HWPROBE_MISALIGNED_FAST ;
ratio = div_u64 ( ( byte_cycles * 100 ) , word_cycles ) ;
pr_info ( " cpu%d: Ratio of byte access time to unaligned word access is %d.%02d, unaligned accesses are %s \n " ,
cpu ,
ratio / 100 ,
ratio % 100 ,
( speed = = RISCV_HWPROBE_MISALIGNED_FAST ) ? " fast " : " slow " ) ;
per_cpu ( misaligned_access_speed , cpu ) = speed ;
out :
__free_pages ( page , get_order ( MISALIGNED_BUFFER_SIZE ) ) ;
}
static int check_unaligned_access_boot_cpu ( void )
{
check_unaligned_access ( 0 ) ;
return 0 ;
}
arch_initcall ( check_unaligned_access_boot_cpu ) ;
RISC-V: Enable cbo.zero in usermode
When Zicboz is present, enable its instruction (cbo.zero) in
usermode by setting its respective senvcfg bit. We don't bother
trying to set this bit per-task, which would also require an
interface for tasks to request enabling and/or disabling. Instead,
permanently set the bit for each hart which has the extension when
bringing it online.
This patch also introduces riscv_cpu_has_extension_[un]likely()
functions to check a specific hart's ISA bitmap for extensions.
Prior to checking the specific hart's bitmap in these functions
we try the bitmap which represents the LCD of extensions, but only
when we know it will use its optimized, alternatives path by gating
its call on CONFIG_RISCV_ALTERNATIVE. When alternatives are used, the
compiler ensures that the invocation of the LCD search becomes a
constant true or false. When it's true, even the new functions will
completely vanish from their callsites. OTOH, when the LCD check is
false, we need to do a search of the hart's ISA bitmap. Had we also
checked the LCD bitmap without the use of alternatives, then we would
have ended up with two bitmap searches instead of one.
Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Link: https://lore.kernel.org/r/20230918131518.56803-10-ajones@ventanamicro.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-09-18 15:15:21 +02:00
void riscv_user_isa_enable ( void )
{
if ( riscv_cpu_has_extension_unlikely ( smp_processor_id ( ) , RISCV_ISA_EXT_ZICBOZ ) )
csr_set ( CSR_SENVCFG , ENVCFG_CBZE ) ;
}
2022-05-11 21:29:18 +02:00
# ifdef CONFIG_RISCV_ALTERNATIVE
2023-02-24 17:26:28 +01:00
/*
* Alternative patch sites consider 48 bits when determining when to patch
* the old instruction sequence with the new . These bits are broken into a
* 16 - bit vendor ID and a 32 - bit patch ID . A non - zero vendor ID means the
* patch site is for an erratum , identified by the 32 - bit patch ID . When
* the vendor ID is zero , the patch site is for a cpufeature . cpufeatures
* further break down patch ID into two 16 - bit numbers . The lower 16 bits
* are the cpufeature ID and the upper 16 bits are used for a value specific
* to the cpufeature and patch site . If the upper 16 bits are zero , then it
* implies no specific value is specified . cpufeatures that want to control
* patching on a per - site basis will provide non - zero values and implement
* checks here . The checks return true when patching should be done , and
* false otherwise .
*/
static bool riscv_cpufeature_patch_check ( u16 id , u16 value )
{
if ( ! value )
return true ;
2023-02-24 17:26:29 +01:00
switch ( id ) {
case RISCV_ISA_EXT_ZICBOZ :
/*
* Zicboz alternative applications provide the maximum
* supported block size order , or zero when it doesn ' t
* matter . If the current block size exceeds the maximum ,
* then the alternative cannot be applied .
*/
return riscv_cboz_block_size < = ( 1U < < value ) ;
}
2023-02-24 17:26:28 +01:00
return false ;
}
2022-05-11 21:29:18 +02:00
void __init_or_module riscv_cpufeature_patch_func ( struct alt_entry * begin ,
struct alt_entry * end ,
unsigned int stage )
{
struct alt_entry * alt ;
2023-01-29 01:28:52 +08:00
void * oldptr , * altptr ;
2023-02-24 17:26:28 +01:00
u16 id , value ;
2022-05-11 21:29:18 +02:00
2023-01-29 01:28:45 +08:00
if ( stage = = RISCV_ALTERNATIVES_EARLY_BOOT )
return ;
2022-05-11 21:29:18 +02:00
for ( alt = begin ; alt < end ; alt + + ) {
if ( alt - > vendor_id ! = 0 )
continue ;
2023-02-24 17:26:28 +01:00
id = PATCH_ID_CPUFEATURE_ID ( alt - > patch_id ) ;
if ( id > = RISCV_ISA_EXT_MAX ) {
WARN ( 1 , " This extension id:%d is not in ISA extension list " , id ) ;
2022-05-11 21:29:18 +02:00
continue ;
}
2023-02-24 17:26:28 +01:00
if ( ! __riscv_isa_extension_available ( NULL , id ) )
continue ;
value = PATCH_ID_CPUFEATURE_VALUE ( alt - > patch_id ) ;
if ( ! riscv_cpufeature_patch_check ( id , value ) )
2023-01-29 01:28:47 +08:00
continue ;
2023-01-29 01:28:52 +08:00
oldptr = ALT_OLD_PTR ( alt ) ;
altptr = ALT_ALT_PTR ( alt ) ;
2023-02-12 19:47:36 +00:00
mutex_lock ( & text_mutex ) ;
2023-01-29 01:28:52 +08:00
patch_text_nosync ( oldptr , altptr , alt - > alt_len ) ;
riscv_alternative_fix_offsets ( oldptr , alt - > alt_len , oldptr - altptr ) ;
2023-02-12 19:47:36 +00:00
mutex_unlock ( & text_mutex ) ;
2022-05-11 21:29:18 +02:00
}
}
# endif