sof-utils.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018-2022 Intel Corporation. All rights reserved.
  7. //
  8. // Author: Keyon Jie <[email protected]>
  9. //
  10. #include <asm/unaligned.h>
  11. #include <linux/io-64-nonatomic-lo-hi.h>
  12. #include <linux/device.h>
  13. #include <sound/memalloc.h>
  14. #include <linux/module.h>
  15. #include "sof-utils.h"
  16. /*
  17. * Generic buffer page table creation.
  18. * Take the each physical page address and drop the least significant unused
  19. * bits from each (based on PAGE_SIZE). Then pack valid page address bits
  20. * into compressed page table.
  21. */
  22. int snd_sof_create_page_table(struct device *dev,
  23. struct snd_dma_buffer *dmab,
  24. unsigned char *page_table, size_t size)
  25. {
  26. int i, pages;
  27. pages = snd_sgbuf_aligned_pages(size);
  28. dev_dbg(dev, "generating page table for %p size 0x%zx pages %d\n",
  29. dmab->area, size, pages);
  30. for (i = 0; i < pages; i++) {
  31. /*
  32. * The number of valid address bits for each page is 20.
  33. * idx determines the byte position within page_table
  34. * where the current page's address is stored
  35. * in the compressed page_table.
  36. * This can be calculated by multiplying the page number by 2.5.
  37. */
  38. u32 idx = (5 * i) >> 1;
  39. u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
  40. u8 *pg_table;
  41. pg_table = (u8 *)(page_table + idx);
  42. /*
  43. * pagetable compression:
  44. * byte 0 byte 1 byte 2 byte 3 byte 4 byte 5
  45. * ___________pfn 0__________ __________pfn 1___________ _pfn 2...
  46. * .... .... .... .... .... .... .... .... .... .... ....
  47. * It is created by:
  48. * 1. set current location to 0, PFN index i to 0
  49. * 2. put pfn[i] at current location in Little Endian byte order
  50. * 3. calculate an intermediate value as
  51. * x = (pfn[i+1] << 4) | (pfn[i] & 0xf)
  52. * 4. put x at offset (current location + 2) in LE byte order
  53. * 5. increment current location by 5 bytes, increment i by 2
  54. * 6. continue to (2)
  55. */
  56. if (i & 1)
  57. put_unaligned_le32((pg_table[0] & 0xf) | pfn << 4,
  58. pg_table);
  59. else
  60. put_unaligned_le32(pfn, pg_table);
  61. }
  62. return pages;
  63. }
  64. EXPORT_SYMBOL(snd_sof_create_page_table);
  65. MODULE_LICENSE("Dual BSD/GPL");