Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Wilson | a33d42d | 2016-12-22 08:36:08 +0000 | [diff] [blame] | 2 | #include <linux/bitops.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/random.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/types.h> |
| 7 | |
| 8 | #include "drm_random.h" |
| 9 | |
| 10 | static inline u32 drm_prandom_u32_max_state(u32 ep_ro, struct rnd_state *state) |
| 11 | { |
| 12 | return upper_32_bits((u64)prandom_u32_state(state) * ep_ro); |
| 13 | } |
| 14 | |
| 15 | void drm_random_reorder(unsigned int *order, unsigned int count, |
| 16 | struct rnd_state *state) |
| 17 | { |
| 18 | unsigned int i, j; |
| 19 | |
| 20 | for (i = 0; i < count; ++i) { |
| 21 | BUILD_BUG_ON(sizeof(unsigned int) > sizeof(u32)); |
| 22 | j = drm_prandom_u32_max_state(count, state); |
| 23 | swap(order[i], order[j]); |
| 24 | } |
| 25 | } |
| 26 | EXPORT_SYMBOL(drm_random_reorder); |
| 27 | |
| 28 | unsigned int *drm_random_order(unsigned int count, struct rnd_state *state) |
| 29 | { |
| 30 | unsigned int *order, i; |
| 31 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 32 | order = kmalloc_array(count, sizeof(*order), GFP_KERNEL); |
Chris Wilson | a33d42d | 2016-12-22 08:36:08 +0000 | [diff] [blame] | 33 | if (!order) |
| 34 | return order; |
| 35 | |
| 36 | for (i = 0; i < count; i++) |
| 37 | order[i] = i; |
| 38 | |
| 39 | drm_random_reorder(order, count, state); |
| 40 | return order; |
| 41 | } |
| 42 | EXPORT_SYMBOL(drm_random_order); |