drm_random.c 982 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // SPDX-License-Identifier: GPL-2.0
  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. #include "drm_random.h"
  8. static inline u32 drm_prandom_u32_max_state(u32 ep_ro, struct rnd_state *state)
  9. {
  10. return upper_32_bits((u64)prandom_u32_state(state) * ep_ro);
  11. }
  12. void drm_random_reorder(unsigned int *order, unsigned int count,
  13. struct rnd_state *state)
  14. {
  15. unsigned int i, j;
  16. for (i = 0; i < count; ++i) {
  17. BUILD_BUG_ON(sizeof(unsigned int) > sizeof(u32));
  18. j = drm_prandom_u32_max_state(count, state);
  19. swap(order[i], order[j]);
  20. }
  21. }
  22. EXPORT_SYMBOL(drm_random_reorder);
  23. unsigned int *drm_random_order(unsigned int count, struct rnd_state *state)
  24. {
  25. unsigned int *order, i;
  26. order = kmalloc_array(count, sizeof(*order), GFP_KERNEL);
  27. if (!order)
  28. return order;
  29. for (i = 0; i < count; i++)
  30. order[i] = i;
  31. drm_random_reorder(order, count, state);
  32. return order;
  33. }
  34. EXPORT_SYMBOL(drm_random_order);