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