xref: /openbmc/linux/drivers/gpu/drm/lib/drm_random.c (revision 726bd223)
1 #include <linux/bitops.h>
2 #include <linux/kernel.h>
3 #include <linux/random.h>
4 #include <linux/slab.h>
5 #include <linux/types.h>
6 
7 #include "drm_random.h"
8 
9 static inline u32 drm_prandom_u32_max_state(u32 ep_ro, struct rnd_state *state)
10 {
11 	return upper_32_bits((u64)prandom_u32_state(state) * ep_ro);
12 }
13 
14 void drm_random_reorder(unsigned int *order, unsigned int count,
15 			struct rnd_state *state)
16 {
17 	unsigned int i, j;
18 
19 	for (i = 0; i < count; ++i) {
20 		BUILD_BUG_ON(sizeof(unsigned int) > sizeof(u32));
21 		j = drm_prandom_u32_max_state(count, state);
22 		swap(order[i], order[j]);
23 	}
24 }
25 EXPORT_SYMBOL(drm_random_reorder);
26 
27 unsigned int *drm_random_order(unsigned int count, struct rnd_state *state)
28 {
29 	unsigned int *order, i;
30 
31 	order = kmalloc_array(count, sizeof(*order), GFP_TEMPORARY);
32 	if (!order)
33 		return order;
34 
35 	for (i = 0; i < count; i++)
36 		order[i] = i;
37 
38 	drm_random_reorder(order, count, state);
39 	return order;
40 }
41 EXPORT_SYMBOL(drm_random_order);
42