forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/gpu/drm/i915/selftests/i915_random.c
....@@ -29,6 +29,7 @@
2929 #include <linux/types.h>
3030
3131 #include "i915_random.h"
32
+#include "i915_utils.h"
3233
3334 u64 i915_prandom_u64_state(struct rnd_state *rnd)
3435 {
....@@ -41,16 +42,35 @@
4142 return x;
4243 }
4344
45
+void i915_prandom_shuffle(void *arr, size_t elsz, size_t count,
46
+ struct rnd_state *state)
47
+{
48
+ char stack[128];
49
+
50
+ if (WARN_ON(elsz > sizeof(stack) || count > U32_MAX))
51
+ return;
52
+
53
+ if (!elsz || !count)
54
+ return;
55
+
56
+ /* Fisher-Yates shuffle courtesy of Knuth */
57
+ while (--count) {
58
+ size_t swp;
59
+
60
+ swp = i915_prandom_u32_max_state(count + 1, state);
61
+ if (swp == count)
62
+ continue;
63
+
64
+ memcpy(stack, arr + count * elsz, elsz);
65
+ memcpy(arr + count * elsz, arr + swp * elsz, elsz);
66
+ memcpy(arr + swp * elsz, stack, elsz);
67
+ }
68
+}
69
+
4470 void i915_random_reorder(unsigned int *order, unsigned int count,
4571 struct rnd_state *state)
4672 {
47
- unsigned int i, j;
48
-
49
- for (i = 0; i < count; i++) {
50
- BUILD_BUG_ON(sizeof(unsigned int) > sizeof(u32));
51
- j = i915_prandom_u32_max_state(count, state);
52
- swap(order[i], order[j]);
53
- }
73
+ i915_prandom_shuffle(order, sizeof(*order), count, state);
5474 }
5575
5676 unsigned int *i915_random_order(unsigned int count, struct rnd_state *state)
....@@ -68,3 +88,22 @@
6888 i915_random_reorder(order, count, state);
6989 return order;
7090 }
91
+
92
+u64 igt_random_offset(struct rnd_state *state,
93
+ u64 start, u64 end,
94
+ u64 len, u64 align)
95
+{
96
+ u64 range, addr;
97
+
98
+ BUG_ON(range_overflows(start, len, end));
99
+ BUG_ON(round_up(start, align) > round_down(end - len, align));
100
+
101
+ range = round_down(end - len, align) - round_up(start, align);
102
+ if (range) {
103
+ addr = i915_prandom_u64_state(state);
104
+ div64_u64_rem(addr, range, &addr);
105
+ start += addr;
106
+ }
107
+
108
+ return round_up(start, align);
109
+}