• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3  *
4  * (C) COPYRIGHT 2017, 2020-2021 ARM Limited. All rights reserved.
5  *
6  * This program is free software and is provided to you under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation, and any use by you of this program is subject to the terms
9  * of such GNU license.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you can access it online at
18  * http://www.gnu.org/licenses/gpl-2.0.html.
19  *
20  */
21 
22 /* Kernel UTF test helpers */
23 #include <kutf/kutf_helpers.h>
24 #include <linux/err.h>
25 #include <linux/jiffies.h>
26 #include <linux/sched.h>
27 #include <linux/preempt.h>
28 #include <linux/wait.h>
29 #include <linux/uaccess.h>
30 #include <linux/export.h>
31 #include <linux/io.h>
32 #include <linux/delay.h>
33 #include "gpu/mali_kbase_gpu_regmap.h"
34 #include <device/mali_kbase_device.h>
35 
36 static DEFINE_SPINLOCK(kutf_input_lock);
37 
kutf_helper_pending_input(struct kutf_context * context)38 bool kutf_helper_pending_input(struct kutf_context *context)
39 {
40 	bool input_pending;
41 
42 	spin_lock(&kutf_input_lock);
43 
44 	input_pending = !list_empty(&context->userdata.input_head);
45 
46 	spin_unlock(&kutf_input_lock);
47 
48 	return input_pending;
49 }
50 EXPORT_SYMBOL(kutf_helper_pending_input);
51 
kutf_helper_input_dequeue(struct kutf_context * context,size_t * str_size)52 char *kutf_helper_input_dequeue(struct kutf_context *context, size_t *str_size)
53 {
54 	struct kutf_userdata_line *line;
55 
56 	spin_lock(&kutf_input_lock);
57 
58 	while (list_empty(&context->userdata.input_head)) {
59 		int err;
60 
61 		kutf_set_waiting_for_input(context->result_set);
62 
63 		spin_unlock(&kutf_input_lock);
64 
65 		err = wait_event_interruptible(context->userdata.input_waitq,
66 				kutf_helper_pending_input(context));
67 
68 		if (err)
69 			return ERR_PTR(-EINTR);
70 
71 		spin_lock(&kutf_input_lock);
72 	}
73 
74 	line = list_first_entry(&context->userdata.input_head,
75 			struct kutf_userdata_line, node);
76 	if (line->str) {
77 		/*
78 		 * Unless it is the end-of-input marker,
79 		 * remove it from the list
80 		 */
81 		list_del(&line->node);
82 	}
83 
84 	spin_unlock(&kutf_input_lock);
85 
86 	if (str_size)
87 		*str_size = line->size;
88 	return line->str;
89 }
90 
kutf_helper_input_enqueue(struct kutf_context * context,const char __user * str,size_t size)91 int kutf_helper_input_enqueue(struct kutf_context *context,
92 		const char __user *str, size_t size)
93 {
94 	struct kutf_userdata_line *line;
95 
96 	line = kutf_mempool_alloc(&context->fixture_pool,
97 			sizeof(*line) + size + 1);
98 	if (!line)
99 		return -ENOMEM;
100 	if (str) {
101 		unsigned long bytes_not_copied;
102 
103 		line->size = size;
104 		line->str = (void *)(line + 1);
105 		bytes_not_copied = copy_from_user(line->str, str, size);
106 		if (bytes_not_copied != 0)
107 			return -EFAULT;
108 		/* Zero terminate the string */
109 		line->str[size] = '\0';
110 	} else {
111 		/* This is used to mark the end of input */
112 		WARN_ON(size);
113 		line->size = 0;
114 		line->str = NULL;
115 	}
116 
117 	spin_lock(&kutf_input_lock);
118 
119 	list_add_tail(&line->node, &context->userdata.input_head);
120 
121 	kutf_clear_waiting_for_input(context->result_set);
122 
123 	spin_unlock(&kutf_input_lock);
124 
125 	wake_up(&context->userdata.input_waitq);
126 
127 	return 0;
128 }
129 
kutf_helper_input_enqueue_end_of_data(struct kutf_context * context)130 void kutf_helper_input_enqueue_end_of_data(struct kutf_context *context)
131 {
132 	kutf_helper_input_enqueue(context, NULL, 0);
133 }
134 
135 /* Values are taken from juno-fpga.dtsi */
136 #define FPGA_SYSCTL_START_ADDR ((resource_size_t)0x6f020000)
137 #define FPGA_SYSCTL_SIZE ((size_t)0xCC)
138 
139 /* Offset of FPGA_SYSCTL_GPU_RESET_REG register */
140 #define FPGA_SYSCTL_GPU_RESET_REG 0x64
141 #define GPU_RESET_HIGH 0x1
142 #define GPU_RESET_LOW 0x0
143 
kutf_helper_external_reset_gpu(void)144 int kutf_helper_external_reset_gpu(void)
145 {
146 	void __iomem *regs = NULL;
147 	void __iomem *gpu_reset_reg = NULL;
148 	int error = -ENXIO;
149 	int repeat = 100;
150 
151 	regs = ioremap(FPGA_SYSCTL_START_ADDR, FPGA_SYSCTL_SIZE);
152 	if (!regs)
153 		return -ENOMEM;
154 
155 	/* Reset GPU via SYSCTL_GPU_RESET by rising & falling the reset signal */
156 	gpu_reset_reg = regs + FPGA_SYSCTL_GPU_RESET_REG;
157 	while (error && repeat--) {
158 		writel(GPU_RESET_HIGH, gpu_reset_reg);
159 		if (readl(gpu_reset_reg) == GPU_RESET_HIGH) {
160 			mdelay(100);
161 			writel(GPU_RESET_LOW, gpu_reset_reg);
162 			mdelay(100);
163 
164 			/* Succeed in resetting GPU */
165 			if (readl(gpu_reset_reg) == GPU_RESET_LOW)
166 				error = 0;
167 		}
168 	}
169 
170 	iounmap(regs);
171 
172 	return error;
173 }
174 EXPORT_SYMBOL(kutf_helper_external_reset_gpu);
175