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
25953c7f82SChris Wilson #include <linux/bitops.h>
26953c7f82SChris Wilson #include <linux/kernel.h>
27953c7f82SChris Wilson #include <linux/random.h>
28953c7f82SChris Wilson #include <linux/slab.h>
29953c7f82SChris Wilson #include <linux/types.h>
30953c7f82SChris Wilson
31953c7f82SChris Wilson #include "i915_random.h"
32*dfe324f3SChris Wilson #include "i915_utils.h"
33953c7f82SChris Wilson
i915_prandom_u64_state(struct rnd_state * rnd)3447979480SChris Wilson u64 i915_prandom_u64_state(struct rnd_state *rnd)
3547979480SChris Wilson {
3647979480SChris Wilson u64 x;
3747979480SChris Wilson
3847979480SChris Wilson x = prandom_u32_state(rnd);
3947979480SChris Wilson x <<= 32;
4047979480SChris Wilson x |= prandom_u32_state(rnd);
4147979480SChris Wilson
4247979480SChris Wilson return x;
4347979480SChris Wilson }
4447979480SChris Wilson
i915_prandom_shuffle(void * arr,size_t elsz,size_t count,struct rnd_state * state)458ba306a6SChris Wilson void i915_prandom_shuffle(void *arr, size_t elsz, size_t count,
468ba306a6SChris Wilson struct rnd_state *state)
478ba306a6SChris Wilson {
488ba306a6SChris Wilson char stack[128];
498ba306a6SChris Wilson
508ba306a6SChris Wilson if (WARN_ON(elsz > sizeof(stack) || count > U32_MAX))
518ba306a6SChris Wilson return;
528ba306a6SChris Wilson
538ba306a6SChris Wilson if (!elsz || !count)
548ba306a6SChris Wilson return;
558ba306a6SChris Wilson
568ba306a6SChris Wilson /* Fisher-Yates shuffle courtesy of Knuth */
578ba306a6SChris Wilson while (--count) {
588ba306a6SChris Wilson size_t swp;
598ba306a6SChris Wilson
608ba306a6SChris Wilson swp = i915_prandom_u32_max_state(count + 1, state);
618ba306a6SChris Wilson if (swp == count)
628ba306a6SChris Wilson continue;
638ba306a6SChris Wilson
648ba306a6SChris Wilson memcpy(stack, arr + count * elsz, elsz);
658ba306a6SChris Wilson memcpy(arr + count * elsz, arr + swp * elsz, elsz);
668ba306a6SChris Wilson memcpy(arr + swp * elsz, stack, elsz);
678ba306a6SChris Wilson }
688ba306a6SChris Wilson }
698ba306a6SChris Wilson
i915_random_reorder(unsigned int * order,unsigned int count,struct rnd_state * state)70953c7f82SChris Wilson void i915_random_reorder(unsigned int *order, unsigned int count,
71953c7f82SChris Wilson struct rnd_state *state)
72953c7f82SChris Wilson {
738ba306a6SChris Wilson i915_prandom_shuffle(order, sizeof(*order), count, state);
74953c7f82SChris Wilson }
75953c7f82SChris Wilson
i915_random_order(unsigned int count,struct rnd_state * state)76953c7f82SChris Wilson unsigned int *i915_random_order(unsigned int count, struct rnd_state *state)
77953c7f82SChris Wilson {
78953c7f82SChris Wilson unsigned int *order, i;
79953c7f82SChris Wilson
809861b668SChris Wilson order = kmalloc_array(count, sizeof(*order),
819861b668SChris Wilson GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
82953c7f82SChris Wilson if (!order)
83953c7f82SChris Wilson return order;
84953c7f82SChris Wilson
85953c7f82SChris Wilson for (i = 0; i < count; i++)
86953c7f82SChris Wilson order[i] = i;
87953c7f82SChris Wilson
88953c7f82SChris Wilson i915_random_reorder(order, count, state);
89953c7f82SChris Wilson return order;
90953c7f82SChris Wilson }
91*dfe324f3SChris Wilson
igt_random_offset(struct rnd_state * state,u64 start,u64 end,u64 len,u64 align)92*dfe324f3SChris Wilson u64 igt_random_offset(struct rnd_state *state,
93*dfe324f3SChris Wilson u64 start, u64 end,
94*dfe324f3SChris Wilson u64 len, u64 align)
95*dfe324f3SChris Wilson {
96*dfe324f3SChris Wilson u64 range, addr;
97*dfe324f3SChris Wilson
98*dfe324f3SChris Wilson BUG_ON(range_overflows(start, len, end));
99*dfe324f3SChris Wilson BUG_ON(round_up(start, align) > round_down(end - len, align));
100*dfe324f3SChris Wilson
101*dfe324f3SChris Wilson range = round_down(end - len, align) - round_up(start, align);
102*dfe324f3SChris Wilson if (range) {
103*dfe324f3SChris Wilson addr = i915_prandom_u64_state(state);
104*dfe324f3SChris Wilson div64_u64_rem(addr, range, &addr);
105*dfe324f3SChris Wilson start += addr;
106*dfe324f3SChris Wilson }
107*dfe324f3SChris Wilson
108*dfe324f3SChris Wilson return round_up(start, align);
109*dfe324f3SChris Wilson }
110