18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * POWER Data Stream Control Register (DSCR)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This header file contains helper functions and macros
68c2ecf20Sopenharmony_ci * required for all the DSCR related test cases.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright 2012, Anton Blanchard, IBM Corporation.
98c2ecf20Sopenharmony_ci * Copyright 2015, Anshuman Khandual, IBM Corporation.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#ifndef _SELFTESTS_POWERPC_DSCR_DSCR_H
128c2ecf20Sopenharmony_ci#define _SELFTESTS_POWERPC_DSCR_DSCR_H
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <unistd.h>
158c2ecf20Sopenharmony_ci#include <stdio.h>
168c2ecf20Sopenharmony_ci#include <stdlib.h>
178c2ecf20Sopenharmony_ci#include <string.h>
188c2ecf20Sopenharmony_ci#include <fcntl.h>
198c2ecf20Sopenharmony_ci#include <dirent.h>
208c2ecf20Sopenharmony_ci#include <pthread.h>
218c2ecf20Sopenharmony_ci#include <sched.h>
228c2ecf20Sopenharmony_ci#include <sys/types.h>
238c2ecf20Sopenharmony_ci#include <sys/stat.h>
248c2ecf20Sopenharmony_ci#include <sys/wait.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "utils.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define THREADS		100	/* Max threads */
298c2ecf20Sopenharmony_ci#define COUNT		100	/* Max iterations */
308c2ecf20Sopenharmony_ci#define DSCR_MAX	16	/* Max DSCR value */
318c2ecf20Sopenharmony_ci#define LEN_MAX		100	/* Max name length */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define DSCR_DEFAULT	"/sys/devices/system/cpu/dscr_default"
348c2ecf20Sopenharmony_ci#define CPU_PATH	"/sys/devices/system/cpu/"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define rmb()  asm volatile("lwsync":::"memory")
378c2ecf20Sopenharmony_ci#define wmb()  asm volatile("lwsync":::"memory")
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define READ_ONCE(x) (*(volatile typeof(x) *)&(x))
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Prilvilege state DSCR access */
428c2ecf20Sopenharmony_ciinline unsigned long get_dscr(void)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	unsigned long ret;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR_PRIV));
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	return ret;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciinline void set_dscr(unsigned long val)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR_PRIV));
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Problem state DSCR access */
578c2ecf20Sopenharmony_ciinline unsigned long get_dscr_usr(void)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	unsigned long ret;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	asm volatile("mfspr %0,%1" : "=r" (ret) : "i" (SPRN_DSCR));
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	return ret;
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciinline void set_dscr_usr(unsigned long val)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	asm volatile("mtspr %1,%0" : : "r" (val), "i" (SPRN_DSCR));
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* Default DSCR access */
728c2ecf20Sopenharmony_ciunsigned long get_default_dscr(void)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	int fd = -1, ret;
758c2ecf20Sopenharmony_ci	char buf[16];
768c2ecf20Sopenharmony_ci	unsigned long val;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (fd == -1) {
798c2ecf20Sopenharmony_ci		fd = open(DSCR_DEFAULT, O_RDONLY);
808c2ecf20Sopenharmony_ci		if (fd == -1) {
818c2ecf20Sopenharmony_ci			perror("open() failed");
828c2ecf20Sopenharmony_ci			exit(1);
838c2ecf20Sopenharmony_ci		}
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci	memset(buf, 0, sizeof(buf));
868c2ecf20Sopenharmony_ci	lseek(fd, 0, SEEK_SET);
878c2ecf20Sopenharmony_ci	ret = read(fd, buf, sizeof(buf));
888c2ecf20Sopenharmony_ci	if (ret == -1) {
898c2ecf20Sopenharmony_ci		perror("read() failed");
908c2ecf20Sopenharmony_ci		exit(1);
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci	sscanf(buf, "%lx", &val);
938c2ecf20Sopenharmony_ci	close(fd);
948c2ecf20Sopenharmony_ci	return val;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_civoid set_default_dscr(unsigned long val)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	int fd = -1, ret;
1008c2ecf20Sopenharmony_ci	char buf[16];
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	if (fd == -1) {
1038c2ecf20Sopenharmony_ci		fd = open(DSCR_DEFAULT, O_RDWR);
1048c2ecf20Sopenharmony_ci		if (fd == -1) {
1058c2ecf20Sopenharmony_ci			perror("open() failed");
1068c2ecf20Sopenharmony_ci			exit(1);
1078c2ecf20Sopenharmony_ci		}
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci	sprintf(buf, "%lx\n", val);
1108c2ecf20Sopenharmony_ci	ret = write(fd, buf, strlen(buf));
1118c2ecf20Sopenharmony_ci	if (ret == -1) {
1128c2ecf20Sopenharmony_ci		perror("write() failed");
1138c2ecf20Sopenharmony_ci		exit(1);
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci	close(fd);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cidouble uniform_deviate(int seed)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	return seed * (1.0 / (RAND_MAX + 1.0));
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci#endif	/* _SELFTESTS_POWERPC_DSCR_DSCR_H */
123