Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * DAWR infrastructure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright 2019, Michael Neuling, IBM Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/machdep.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <asm/hvcall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) bool dawr_force_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) EXPORT_SYMBOL_GPL(dawr_force_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) int set_dawr(int nr, struct arch_hw_breakpoint *brk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	unsigned long dawr, dawrx, mrd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	dawr = brk->address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	dawrx  = (brk->type & (HW_BRK_TYPE_READ | HW_BRK_TYPE_WRITE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 		<< (63 - 58);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	dawrx |= ((brk->type & (HW_BRK_TYPE_TRANSLATE)) >> 2) << (63 - 59);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	dawrx |= (brk->type & (HW_BRK_TYPE_PRIV_ALL)) >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	 * DAWR length is stored in field MDR bits 48:53.  Matches range in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	 * doublewords (64 bits) baised by -1 eg. 0b000000=1DW and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	 * 0b111111=64DW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	 * brk->hw_len is in bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	 * This aligns up to double word size, shifts and does the bias.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	mrd = ((brk->hw_len + 7) >> 3) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	dawrx |= (mrd & 0x3f) << (63 - 53);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (ppc_md.set_dawr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return ppc_md.set_dawr(nr, dawr, dawrx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (nr == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		mtspr(SPRN_DAWR0, dawr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		mtspr(SPRN_DAWRX0, dawrx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		mtspr(SPRN_DAWR1, dawr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		mtspr(SPRN_DAWRX1, dawrx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static void disable_dawrs_cb(void *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct arch_hw_breakpoint null_brk = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	for (i = 0; i < nr_wp_slots(); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		set_dawr(i, &null_brk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static ssize_t dawr_write_file_bool(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 				    const char __user *user_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 				    size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	struct arch_hw_breakpoint null_brk = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	size_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	/* Send error to user if they hypervisor won't allow us to write DAWR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!dawr_force_enable &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	    firmware_has_feature(FW_FEATURE_LPAR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	    set_dawr(0, &null_brk) != H_SUCCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	rc = debugfs_write_file_bool(file, user_buf, count, ppos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	/* If we are clearing, make sure all CPUs have the DAWR cleared */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (!dawr_force_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		smp_call_function(disable_dawrs_cb, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static const struct file_operations dawr_enable_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	.read =		debugfs_read_file_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	.write =	dawr_write_file_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	.open =		simple_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	.llseek =	default_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static int __init dawr_force_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (cpu_has_feature(CPU_FTR_DAWR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		/* Don't setup sysfs file for user control on P8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		dawr_force_enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (PVR_VER(mfspr(SPRN_PVR)) == PVR_POWER9) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		/* Turn DAWR off by default, but allow admin to turn it on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		debugfs_create_file_unsafe("dawr_enable_dangerous", 0600,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 					   powerpc_debugfs_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 					   &dawr_force_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 					   &dawr_enable_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) arch_initcall(dawr_force_setup);