1953c7f82SChris Wilson /*
2953c7f82SChris Wilson  * Copyright © 2016 Intel Corporation
3953c7f82SChris Wilson  *
4953c7f82SChris Wilson  * Permission is hereby granted, free of charge, to any person obtaining a
5953c7f82SChris Wilson  * copy of this software and associated documentation files (the "Software"),
6953c7f82SChris Wilson  * to deal in the Software without restriction, including without limitation
7953c7f82SChris Wilson  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8953c7f82SChris Wilson  * and/or sell copies of the Software, and to permit persons to whom the
9953c7f82SChris Wilson  * Software is furnished to do so, subject to the following conditions:
10953c7f82SChris Wilson  *
11953c7f82SChris Wilson  * The above copyright notice and this permission notice (including the next
12953c7f82SChris Wilson  * paragraph) shall be included in all copies or substantial portions of the
13953c7f82SChris Wilson  * Software.
14953c7f82SChris Wilson  *
15953c7f82SChris Wilson  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16953c7f82SChris Wilson  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17953c7f82SChris Wilson  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18953c7f82SChris Wilson  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19953c7f82SChris Wilson  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20953c7f82SChris Wilson  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21953c7f82SChris Wilson  * IN THE SOFTWARE.
22953c7f82SChris Wilson  */
23953c7f82SChris Wilson 
24953c7f82SChris Wilson #ifndef __I915_SELFTEST_H__
25953c7f82SChris Wilson #define __I915_SELFTEST_H__
26953c7f82SChris Wilson 
272e8de087SLucas De Marchi #include <linux/types.h>
282e8de087SLucas De Marchi 
29953c7f82SChris Wilson struct pci_dev;
30953c7f82SChris Wilson struct drm_i915_private;
31953c7f82SChris Wilson 
32953c7f82SChris Wilson struct i915_selftest {
33953c7f82SChris Wilson 	unsigned long timeout_jiffies;
34953c7f82SChris Wilson 	unsigned int timeout_ms;
35953c7f82SChris Wilson 	unsigned int random_seed;
3606039d98SChris Wilson 	char *filter;
37953c7f82SChris Wilson 	int mock;
38953c7f82SChris Wilson 	int live;
393c7a44bbSChris Wilson 	int perf;
40953c7f82SChris Wilson };
41953c7f82SChris Wilson 
42953c7f82SChris Wilson #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
43aae4a3d8SChris Wilson #include <linux/fault-inject.h>
44aae4a3d8SChris Wilson 
45953c7f82SChris Wilson extern struct i915_selftest i915_selftest;
46953c7f82SChris Wilson 
47953c7f82SChris Wilson int i915_mock_selftests(void);
48953c7f82SChris Wilson int i915_live_selftests(struct pci_dev *pdev);
493c7a44bbSChris Wilson int i915_perf_selftests(struct pci_dev *pdev);
50953c7f82SChris Wilson 
51953c7f82SChris Wilson /* We extract the function declarations from i915_mock_selftests.h and
52953c7f82SChris Wilson  * i915_live_selftests.h Add your unit test declarations there!
53953c7f82SChris Wilson  *
54953c7f82SChris Wilson  * Mock unit tests are run very early upon module load, before the driver
55953c7f82SChris Wilson  * is probed. All hardware interactions, as well as other subsystems, must
56953c7f82SChris Wilson  * be "mocked".
57953c7f82SChris Wilson  *
58953c7f82SChris Wilson  * Live unit tests are run after the driver is loaded - all hardware
59953c7f82SChris Wilson  * interactions are real.
60953c7f82SChris Wilson  */
61953c7f82SChris Wilson #define selftest(name, func) int func(void);
62953c7f82SChris Wilson #include "selftests/i915_mock_selftests.h"
63953c7f82SChris Wilson #undef selftest
64953c7f82SChris Wilson #define selftest(name, func) int func(struct drm_i915_private *i915);
65953c7f82SChris Wilson #include "selftests/i915_live_selftests.h"
663c7a44bbSChris Wilson #include "selftests/i915_perf_selftests.h"
67953c7f82SChris Wilson #undef selftest
68953c7f82SChris Wilson 
69953c7f82SChris Wilson struct i915_subtest {
70953c7f82SChris Wilson 	int (*func)(void *data);
71953c7f82SChris Wilson 	const char *name;
72953c7f82SChris Wilson };
73953c7f82SChris Wilson 
7463251685SChris Wilson int __i915_nop_setup(void *data);
7563251685SChris Wilson int __i915_nop_teardown(int err, void *data);
7663251685SChris Wilson 
7763251685SChris Wilson int __i915_live_setup(void *data);
7863251685SChris Wilson int __i915_live_teardown(int err, void *data);
7963251685SChris Wilson 
80cb823ed9SChris Wilson int __intel_gt_live_setup(void *data);
81cb823ed9SChris Wilson int __intel_gt_live_teardown(int err, void *data);
82cb823ed9SChris Wilson 
83953c7f82SChris Wilson int __i915_subtests(const char *caller,
8463251685SChris Wilson 		    int (*setup)(void *data),
8563251685SChris Wilson 		    int (*teardown)(int err, void *data),
86953c7f82SChris Wilson 		    const struct i915_subtest *st,
87953c7f82SChris Wilson 		    unsigned int count,
88953c7f82SChris Wilson 		    void *data);
89953c7f82SChris Wilson #define i915_subtests(T, data) \
9063251685SChris Wilson 	__i915_subtests(__func__, \
9163251685SChris Wilson 			__i915_nop_setup, __i915_nop_teardown, \
9263251685SChris Wilson 			T, ARRAY_SIZE(T), data)
9363251685SChris Wilson #define i915_live_subtests(T, data) ({ \
9463251685SChris Wilson 	typecheck(struct drm_i915_private *, data); \
95*83321094SMatthew Brost 	(data)->gt[0]->uc.guc.submission_state.sched_disable_delay_ms = 0; \
9663251685SChris Wilson 	__i915_subtests(__func__, \
9763251685SChris Wilson 			__i915_live_setup, __i915_live_teardown, \
9863251685SChris Wilson 			T, ARRAY_SIZE(T), data); \
9963251685SChris Wilson })
100cb823ed9SChris Wilson #define intel_gt_live_subtests(T, data) ({ \
101cb823ed9SChris Wilson 	typecheck(struct intel_gt *, data); \
102*83321094SMatthew Brost 	(data)->uc.guc.submission_state.sched_disable_delay_ms = 0; \
103cb823ed9SChris Wilson 	__i915_subtests(__func__, \
104cb823ed9SChris Wilson 			__intel_gt_live_setup, __intel_gt_live_teardown, \
105cb823ed9SChris Wilson 			T, ARRAY_SIZE(T), data); \
106cb823ed9SChris Wilson })
107953c7f82SChris Wilson 
108953c7f82SChris Wilson #define SUBTEST(x) { x, #x }
109953c7f82SChris Wilson 
110953c7f82SChris Wilson #define I915_SELFTEST_DECLARE(x) x
111953c7f82SChris Wilson #define I915_SELFTEST_ONLY(x) unlikely(x)
1122c8ab333SMaarten Lankhorst #define I915_SELFTEST_EXPORT
113953c7f82SChris Wilson 
114953c7f82SChris Wilson #else /* !IS_ENABLED(CONFIG_DRM_I915_SELFTEST) */
115953c7f82SChris Wilson 
i915_mock_selftests(void)116953c7f82SChris Wilson static inline int i915_mock_selftests(void) { return 0; }
i915_live_selftests(struct pci_dev * pdev)117953c7f82SChris Wilson static inline int i915_live_selftests(struct pci_dev *pdev) { return 0; }
i915_perf_selftests(struct pci_dev * pdev)1183c7a44bbSChris Wilson static inline int i915_perf_selftests(struct pci_dev *pdev) { return 0; }
119953c7f82SChris Wilson 
120953c7f82SChris Wilson #define I915_SELFTEST_DECLARE(x)
121953c7f82SChris Wilson #define I915_SELFTEST_ONLY(x) 0
1222c8ab333SMaarten Lankhorst #define I915_SELFTEST_EXPORT static
123953c7f82SChris Wilson 
124953c7f82SChris Wilson #endif
125953c7f82SChris Wilson 
126953c7f82SChris Wilson /* Using the i915_selftest_ prefix becomes a little unwieldy with the helpers.
127953c7f82SChris Wilson  * Instead we use the igt_ shorthand, in reference to the intel-gpu-tools
128953c7f82SChris Wilson  * suite of uabi test cases (which includes a test runner for our selftests).
129953c7f82SChris Wilson  */
130953c7f82SChris Wilson 
131953c7f82SChris Wilson #define IGT_TIMEOUT(name__) \
132953c7f82SChris Wilson 	unsigned long name__ = jiffies + i915_selftest.timeout_jiffies
133953c7f82SChris Wilson 
134953c7f82SChris Wilson __printf(2, 3)
135953c7f82SChris Wilson bool __igt_timeout(unsigned long timeout, const char *fmt, ...);
136953c7f82SChris Wilson 
137953c7f82SChris Wilson #define igt_timeout(t, fmt, ...) \
13855e4b859SChris Wilson 	__igt_timeout((t), KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
139953c7f82SChris Wilson 
140e3d29130SChris Wilson void igt_hexdump(const void *buf, size_t len);
141e3d29130SChris Wilson 
142953c7f82SChris Wilson #endif /* !__I915_SELFTEST_H__ */
143