Home | History | Annotate | Line # | Download | only in selftests
      1 /*	$NetBSD: i915_random.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2016 Intel Corporation
      5  *
      6  * Permission is hereby granted, free of charge, to any person obtaining a
      7  * copy of this software and associated documentation files (the "Software"),
      8  * to deal in the Software without restriction, including without limitation
      9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  * and/or sell copies of the Software, and to permit persons to whom the
     11  * Software is furnished to do so, subject to the following conditions:
     12  *
     13  * The above copyright notice and this permission notice (including the next
     14  * paragraph) shall be included in all copies or substantial portions of the
     15  * Software.
     16  *
     17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     23  * IN THE SOFTWARE.
     24  *
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: i915_random.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $");
     29 
     30 #include <linux/bitops.h>
     31 #include <linux/kernel.h>
     32 #include <linux/random.h>
     33 #include <linux/slab.h>
     34 #include <linux/types.h>
     35 
     36 #include "i915_random.h"
     37 #include "i915_utils.h"
     38 
     39 u64 i915_prandom_u64_state(struct rnd_state *rnd)
     40 {
     41 	u64 x;
     42 
     43 	x = prandom_u32_state(rnd);
     44 	x <<= 32;
     45 	x |= prandom_u32_state(rnd);
     46 
     47 	return x;
     48 }
     49 
     50 void i915_prandom_shuffle(void *arr, size_t elsz, size_t count,
     51 			  struct rnd_state *state)
     52 {
     53 	char stack[128];
     54 
     55 	if (WARN_ON(elsz > sizeof(stack) || count > U32_MAX))
     56 		return;
     57 
     58 	if (!elsz || !count)
     59 		return;
     60 
     61 	/* Fisher-Yates shuffle courtesy of Knuth */
     62 	while (--count) {
     63 		size_t swp;
     64 
     65 		swp = i915_prandom_u32_max_state(count + 1, state);
     66 		if (swp == count)
     67 			continue;
     68 
     69 		memcpy(stack, arr + count * elsz, elsz);
     70 		memcpy(arr + count * elsz, arr + swp * elsz, elsz);
     71 		memcpy(arr + swp * elsz, stack, elsz);
     72 	}
     73 }
     74 
     75 void i915_random_reorder(unsigned int *order, unsigned int count,
     76 			 struct rnd_state *state)
     77 {
     78 	i915_prandom_shuffle(order, sizeof(*order), count, state);
     79 }
     80 
     81 unsigned int *i915_random_order(unsigned int count, struct rnd_state *state)
     82 {
     83 	unsigned int *order, i;
     84 
     85 	order = kmalloc_array(count, sizeof(*order),
     86 			      GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
     87 	if (!order)
     88 		return order;
     89 
     90 	for (i = 0; i < count; i++)
     91 		order[i] = i;
     92 
     93 	i915_random_reorder(order, count, state);
     94 	return order;
     95 }
     96 
     97 u64 igt_random_offset(struct rnd_state *state,
     98 		      u64 start, u64 end,
     99 		      u64 len, u64 align)
    100 {
    101 	u64 range, addr;
    102 
    103 	BUG_ON(range_overflows(start, len, end));
    104 	BUG_ON(round_up(start, align) > round_down(end - len, align));
    105 
    106 	range = round_down(end - len, align) - round_up(start, align);
    107 	if (range) {
    108 		addr = i915_prandom_u64_state(state);
    109 		div64_u64_rem(addr, range, &addr);
    110 		start += addr;
    111 	}
    112 
    113 	return round_up(start, align);
    114 }
    115