xref: /openbsd-src/sys/dev/pci/drm/i915/selftests/i915_random.h (revision c349dbc7938c71a30e13c1be4acc1976165f4630)
1*c349dbc7Sjsg /*
2*c349dbc7Sjsg  * Copyright © 2016 Intel Corporation
3*c349dbc7Sjsg  *
4*c349dbc7Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
5*c349dbc7Sjsg  * copy of this software and associated documentation files (the "Software"),
6*c349dbc7Sjsg  * to deal in the Software without restriction, including without limitation
7*c349dbc7Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*c349dbc7Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
9*c349dbc7Sjsg  * Software is furnished to do so, subject to the following conditions:
10*c349dbc7Sjsg  *
11*c349dbc7Sjsg  * The above copyright notice and this permission notice (including the next
12*c349dbc7Sjsg  * paragraph) shall be included in all copies or substantial portions of the
13*c349dbc7Sjsg  * Software.
14*c349dbc7Sjsg  *
15*c349dbc7Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*c349dbc7Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*c349dbc7Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*c349dbc7Sjsg  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*c349dbc7Sjsg  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*c349dbc7Sjsg  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*c349dbc7Sjsg  * IN THE SOFTWARE.
22*c349dbc7Sjsg  *
23*c349dbc7Sjsg  */
24*c349dbc7Sjsg 
25*c349dbc7Sjsg #ifndef __I915_SELFTESTS_RANDOM_H__
26*c349dbc7Sjsg #define __I915_SELFTESTS_RANDOM_H__
27*c349dbc7Sjsg 
28*c349dbc7Sjsg #include <linux/math64.h>
29*c349dbc7Sjsg #include <linux/random.h>
30*c349dbc7Sjsg 
31*c349dbc7Sjsg #include "../i915_selftest.h"
32*c349dbc7Sjsg 
33*c349dbc7Sjsg #define I915_RND_STATE_INITIALIZER(x) ({				\
34*c349dbc7Sjsg 	struct rnd_state state__;					\
35*c349dbc7Sjsg 	prandom_seed_state(&state__, (x));				\
36*c349dbc7Sjsg 	state__;							\
37*c349dbc7Sjsg })
38*c349dbc7Sjsg 
39*c349dbc7Sjsg #define I915_RND_STATE(name__) \
40*c349dbc7Sjsg 	struct rnd_state name__ = I915_RND_STATE_INITIALIZER(i915_selftest.random_seed)
41*c349dbc7Sjsg 
42*c349dbc7Sjsg #define I915_RND_SUBSTATE(name__, parent__) \
43*c349dbc7Sjsg 	struct rnd_state name__ = I915_RND_STATE_INITIALIZER(prandom_u32_state(&(parent__)))
44*c349dbc7Sjsg 
45*c349dbc7Sjsg u64 i915_prandom_u64_state(struct rnd_state *rnd);
46*c349dbc7Sjsg 
i915_prandom_u32_max_state(u32 ep_ro,struct rnd_state * state)47*c349dbc7Sjsg static inline u32 i915_prandom_u32_max_state(u32 ep_ro, struct rnd_state *state)
48*c349dbc7Sjsg {
49*c349dbc7Sjsg 	return upper_32_bits(mul_u32_u32(prandom_u32_state(state), ep_ro));
50*c349dbc7Sjsg }
51*c349dbc7Sjsg 
52*c349dbc7Sjsg unsigned int *i915_random_order(unsigned int count,
53*c349dbc7Sjsg 				struct rnd_state *state);
54*c349dbc7Sjsg void i915_random_reorder(unsigned int *order,
55*c349dbc7Sjsg 			 unsigned int count,
56*c349dbc7Sjsg 			 struct rnd_state *state);
57*c349dbc7Sjsg 
58*c349dbc7Sjsg void i915_prandom_shuffle(void *arr, size_t elsz, size_t count,
59*c349dbc7Sjsg 			  struct rnd_state *state);
60*c349dbc7Sjsg 
61*c349dbc7Sjsg u64 igt_random_offset(struct rnd_state *state,
62*c349dbc7Sjsg 		      u64 start, u64 end,
63*c349dbc7Sjsg 		      u64 len, u64 align);
64*c349dbc7Sjsg 
65*c349dbc7Sjsg #endif /* !__I915_SELFTESTS_RANDOM_H__ */
66