1d2912cb1SThomas Gleixner/* SPDX-License-Identifier: GPL-2.0-only */
24c6315f8SAnshuman Khandual/*
34c6315f8SAnshuman Khandual * POWER Data Stream Control Register (DSCR)
44c6315f8SAnshuman Khandual *
54c6315f8SAnshuman Khandual * This header file contains helper functions and macros
64c6315f8SAnshuman Khandual * required for all the DSCR related test cases.
74c6315f8SAnshuman Khandual *
84c6315f8SAnshuman Khandual * Copyright 2012, Anton Blanchard, IBM Corporation.
94c6315f8SAnshuman Khandual * Copyright 2015, Anshuman Khandual, IBM Corporation.
104c6315f8SAnshuman Khandual */
114c6315f8SAnshuman Khandual#ifndef _SELFTESTS_POWERPC_DSCR_DSCR_H
124c6315f8SAnshuman Khandual#define _SELFTESTS_POWERPC_DSCR_DSCR_H
134c6315f8SAnshuman Khandual
144c6315f8SAnshuman Khandual#include <unistd.h>
154c6315f8SAnshuman Khandual#include <stdio.h>
164c6315f8SAnshuman Khandual#include <stdlib.h>
174c6315f8SAnshuman Khandual#include <string.h>
184c6315f8SAnshuman Khandual#include <fcntl.h>
194c6315f8SAnshuman Khandual#include <dirent.h>
204c6315f8SAnshuman Khandual#include <pthread.h>
214c6315f8SAnshuman Khandual#include <sched.h>
224c6315f8SAnshuman Khandual#include <sys/types.h>
234c6315f8SAnshuman Khandual#include <sys/stat.h>
244c6315f8SAnshuman Khandual#include <sys/wait.h>
254c6315f8SAnshuman Khandual
264c6315f8SAnshuman Khandual#include "utils.h"
274c6315f8SAnshuman Khandual
284c6315f8SAnshuman Khandual#define THREADS		100	/* Max threads */
294c6315f8SAnshuman Khandual#define COUNT		100	/* Max iterations */
304c6315f8SAnshuman Khandual#define DSCR_MAX	16	/* Max DSCR value */
314c6315f8SAnshuman Khandual#define LEN_MAX		100	/* Max name length */
324c6315f8SAnshuman Khandual
334c6315f8SAnshuman Khandual#define DSCR_DEFAULT	"/sys/devices/system/cpu/dscr_default"
344c6315f8SAnshuman Khandual#define CPU_PATH	"/sys/devices/system/cpu/"
354c6315f8SAnshuman Khandual
364c6315f8SAnshuman Khandual#define rmb()  asm volatile("lwsync":::"memory")
374c6315f8SAnshuman Khandual#define wmb()  asm volatile("lwsync":::"memory")
384c6315f8SAnshuman Khandual
39564cbc87SMark Rutland#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
404c6315f8SAnshuman Khandual
414c6315f8SAnshuman Khandual/* Prilvilege state DSCR access */
424c6315f8SAnshuman Khandualinline unsigned long get_dscr(void)
434c6315f8SAnshuman Khandual{
444c6315f8SAnshuman Khandual	unsigned long ret;
454c6315f8SAnshuman Khandual
46efe71a67SAnshuman Khandual	asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR_PRIV));
474c6315f8SAnshuman Khandual
484c6315f8SAnshuman Khandual	return ret;
494c6315f8SAnshuman Khandual}
504c6315f8SAnshuman Khandual
514c6315f8SAnshuman Khandualinline void set_dscr(unsigned long val)
524c6315f8SAnshuman Khandual{
53efe71a67SAnshuman Khandual	asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR_PRIV));
544c6315f8SAnshuman Khandual}
554c6315f8SAnshuman Khandual
564c6315f8SAnshuman Khandual/* Problem state DSCR access */
574c6315f8SAnshuman Khandualinline unsigned long get_dscr_usr(void)
584c6315f8SAnshuman Khandual{
594c6315f8SAnshuman Khandual	unsigned long ret;
604c6315f8SAnshuman Khandual
61efe71a67SAnshuman Khandual	asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR));
624c6315f8SAnshuman Khandual
634c6315f8SAnshuman Khandual	return ret;
644c6315f8SAnshuman Khandual}
654c6315f8SAnshuman Khandual
664c6315f8SAnshuman Khandualinline void set_dscr_usr(unsigned long val)
674c6315f8SAnshuman Khandual{
68efe71a67SAnshuman Khandual	asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));
694c6315f8SAnshuman Khandual}
704c6315f8SAnshuman Khandual
714c6315f8SAnshuman Khandual/* Default DSCR access */
724c6315f8SAnshuman Khandualunsigned long get_default_dscr(void)
734c6315f8SAnshuman Khandual{
744c6315f8SAnshuman Khandual	int fd = -1, ret;
754c6315f8SAnshuman Khandual	char buf[16];
764c6315f8SAnshuman Khandual	unsigned long val;
774c6315f8SAnshuman Khandual
784c6315f8SAnshuman Khandual	if (fd == -1) {
794c6315f8SAnshuman Khandual		fd = open(DSCR_DEFAULT, O_RDONLY);
804c6315f8SAnshuman Khandual		if (fd == -1) {
814c6315f8SAnshuman Khandual			perror("open() failed");
824c6315f8SAnshuman Khandual			exit(1);
834c6315f8SAnshuman Khandual		}
844c6315f8SAnshuman Khandual	}
854c6315f8SAnshuman Khandual	memset(buf, 0, sizeof(buf));
864c6315f8SAnshuman Khandual	lseek(fd, 0, SEEK_SET);
874c6315f8SAnshuman Khandual	ret = read(fd, buf, sizeof(buf));
884c6315f8SAnshuman Khandual	if (ret == -1) {
894c6315f8SAnshuman Khandual		perror("read() failed");
904c6315f8SAnshuman Khandual		exit(1);
914c6315f8SAnshuman Khandual	}
924c6315f8SAnshuman Khandual	sscanf(buf, "%lx", &val);
934c6315f8SAnshuman Khandual	close(fd);
944c6315f8SAnshuman Khandual	return val;
954c6315f8SAnshuman Khandual}
964c6315f8SAnshuman Khandual
974c6315f8SAnshuman Khandualvoid set_default_dscr(unsigned long val)
984c6315f8SAnshuman Khandual{
994c6315f8SAnshuman Khandual	int fd = -1, ret;
1004c6315f8SAnshuman Khandual	char buf[16];
1014c6315f8SAnshuman Khandual
1024c6315f8SAnshuman Khandual	if (fd == -1) {
1034c6315f8SAnshuman Khandual		fd = open(DSCR_DEFAULT, O_RDWR);
1044c6315f8SAnshuman Khandual		if (fd == -1) {
1054c6315f8SAnshuman Khandual			perror("open() failed");
1064c6315f8SAnshuman Khandual			exit(1);
1074c6315f8SAnshuman Khandual		}
1084c6315f8SAnshuman Khandual	}
1094c6315f8SAnshuman Khandual	sprintf(buf, "%lx\n", val);
1104c6315f8SAnshuman Khandual	ret = write(fd, buf, strlen(buf));
1114c6315f8SAnshuman Khandual	if (ret == -1) {
1124c6315f8SAnshuman Khandual		perror("write() failed");
1134c6315f8SAnshuman Khandual		exit(1);
1144c6315f8SAnshuman Khandual	}
1154c6315f8SAnshuman Khandual	close(fd);
1164c6315f8SAnshuman Khandual}
1174c6315f8SAnshuman Khandual
1184c6315f8SAnshuman Khandualdouble uniform_deviate(int seed)
1194c6315f8SAnshuman Khandual{
1204c6315f8SAnshuman Khandual	return seed * (1.0 / (RAND_MAX + 1.0));
1214c6315f8SAnshuman Khandual}
1224c6315f8SAnshuman Khandual#endif	/* _SELFTESTS_POWERPC_DSCR_DSCR_H */
123