18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright © 2016 Intel Corporation 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation 78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next 128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 138c2ecf20Sopenharmony_ci * Software. 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 208c2ecf20Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 218c2ecf20Sopenharmony_ci * IN THE SOFTWARE. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include <linux/bitops.h> 268c2ecf20Sopenharmony_ci#include <linux/kernel.h> 278c2ecf20Sopenharmony_ci#include <linux/random.h> 288c2ecf20Sopenharmony_ci#include <linux/slab.h> 298c2ecf20Sopenharmony_ci#include <linux/types.h> 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#include "i915_random.h" 328c2ecf20Sopenharmony_ci#include "i915_utils.h" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciu64 i915_prandom_u64_state(struct rnd_state *rnd) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci u64 x; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci x = prandom_u32_state(rnd); 398c2ecf20Sopenharmony_ci x <<= 32; 408c2ecf20Sopenharmony_ci x |= prandom_u32_state(rnd); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci return x; 438c2ecf20Sopenharmony_ci} 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_civoid i915_prandom_shuffle(void *arr, size_t elsz, size_t count, 468c2ecf20Sopenharmony_ci struct rnd_state *state) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci char stack[128]; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (WARN_ON(elsz > sizeof(stack) || count > U32_MAX)) 518c2ecf20Sopenharmony_ci return; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci if (!elsz || !count) 548c2ecf20Sopenharmony_ci return; 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci /* Fisher-Yates shuffle courtesy of Knuth */ 578c2ecf20Sopenharmony_ci while (--count) { 588c2ecf20Sopenharmony_ci size_t swp; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci swp = i915_prandom_u32_max_state(count + 1, state); 618c2ecf20Sopenharmony_ci if (swp == count) 628c2ecf20Sopenharmony_ci continue; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci memcpy(stack, arr + count * elsz, elsz); 658c2ecf20Sopenharmony_ci memcpy(arr + count * elsz, arr + swp * elsz, elsz); 668c2ecf20Sopenharmony_ci memcpy(arr + swp * elsz, stack, elsz); 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci} 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_civoid i915_random_reorder(unsigned int *order, unsigned int count, 718c2ecf20Sopenharmony_ci struct rnd_state *state) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci i915_prandom_shuffle(order, sizeof(*order), count, state); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ciunsigned int *i915_random_order(unsigned int count, struct rnd_state *state) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci unsigned int *order, i; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci order = kmalloc_array(count, sizeof(*order), 818c2ecf20Sopenharmony_ci GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 828c2ecf20Sopenharmony_ci if (!order) 838c2ecf20Sopenharmony_ci return order; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) 868c2ecf20Sopenharmony_ci order[i] = i; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci i915_random_reorder(order, count, state); 898c2ecf20Sopenharmony_ci return order; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ciu64 igt_random_offset(struct rnd_state *state, 938c2ecf20Sopenharmony_ci u64 start, u64 end, 948c2ecf20Sopenharmony_ci u64 len, u64 align) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci u64 range, addr; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci BUG_ON(range_overflows(start, len, end)); 998c2ecf20Sopenharmony_ci BUG_ON(round_up(start, align) > round_down(end - len, align)); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci range = round_down(end - len, align) - round_up(start, align); 1028c2ecf20Sopenharmony_ci if (range) { 1038c2ecf20Sopenharmony_ci addr = i915_prandom_u64_state(state); 1048c2ecf20Sopenharmony_ci div64_u64_rem(addr, range, &addr); 1058c2ecf20Sopenharmony_ci start += addr; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci return round_up(start, align); 1098c2ecf20Sopenharmony_ci} 110