Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // This file is provided under a dual BSD/GPLv2 license.  When using or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) // redistributing this file, you may do so under either license.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) // Copyright(c) 2018 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) // Author: Keyon Jie <yang.jie@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/io-64-nonatomic-lo-hi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sound/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <sound/sof.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "sof-priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * Register IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * The sof_io_xyz() wrappers are typically referenced in snd_sof_dsp_ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * structures and cannot be inlined.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) void sof_io_write(struct snd_sof_dev *sdev, void __iomem *addr, u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	writel(value, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) EXPORT_SYMBOL(sof_io_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) u32 sof_io_read(struct snd_sof_dev *sdev, void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return readl(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) EXPORT_SYMBOL(sof_io_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) void sof_io_write64(struct snd_sof_dev *sdev, void __iomem *addr, u64 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	writeq(value, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) EXPORT_SYMBOL(sof_io_write64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) u64 sof_io_read64(struct snd_sof_dev *sdev, void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	return readq(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) EXPORT_SYMBOL(sof_io_read64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * IPC Mailbox IO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) void sof_mailbox_write(struct snd_sof_dev *sdev, u32 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		       void *message, size_t bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	void __iomem *dest = sdev->bar[sdev->mailbox_bar] + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	memcpy_toio(dest, message, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) EXPORT_SYMBOL(sof_mailbox_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) void sof_mailbox_read(struct snd_sof_dev *sdev, u32 offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		      void *message, size_t bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	void __iomem *src = sdev->bar[sdev->mailbox_bar] + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	memcpy_fromio(message, src, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) EXPORT_SYMBOL(sof_mailbox_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * Memory copy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) void sof_block_write(struct snd_sof_dev *sdev, u32 bar, u32 offset, void *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		     size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	void __iomem *dest = sdev->bar[bar] + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	const u8 *src_byte = src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	u32 affected_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int m, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	m = size / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	n = size % 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	/* __iowrite32_copy use 32bit size values so divide by 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	__iowrite32_copy(dest, src, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		affected_mask = (1 << (8 * n)) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		/* first read the 32bit data of dest, then change affected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		 * bytes, and write back to dest. For unaffected bytes, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		 * should not be changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		tmp = ioread32(dest + m * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		tmp &= ~affected_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		tmp |= *(u32 *)(src_byte + m * 4) & affected_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		iowrite32(tmp, dest + m * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) EXPORT_SYMBOL(sof_block_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) void sof_block_read(struct snd_sof_dev *sdev, u32 bar, u32 offset, void *dest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		    size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	void __iomem *src = sdev->bar[bar] + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	memcpy_fromio(dest, src, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) EXPORT_SYMBOL(sof_block_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * Generic buffer page table creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * Take the each physical page address and drop the least significant unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * bits from each (based on PAGE_SIZE). Then pack valid page address bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * into compressed page table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int snd_sof_create_page_table(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			      struct snd_dma_buffer *dmab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			      unsigned char *page_table, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	int i, pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	pages = snd_sgbuf_aligned_pages(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	dev_dbg(dev, "generating page table for %p size 0x%zx pages %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		dmab->area, size, pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	for (i = 0; i < pages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		 * The number of valid address bits for each page is 20.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		 * idx determines the byte position within page_table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		 * where the current page's address is stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		 * in the compressed page_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		 * This can be calculated by multiplying the page number by 2.5.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		u32 idx = (5 * i) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		u32 pfn = snd_sgbuf_get_addr(dmab, i * PAGE_SIZE) >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		u8 *pg_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		dev_vdbg(dev, "pfn i %i idx %d pfn %x\n", i, idx, pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		pg_table = (u8 *)(page_table + idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		 * pagetable compression:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		 * byte 0     byte 1     byte 2     byte 3     byte 4     byte 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		 * ___________pfn 0__________ __________pfn 1___________  _pfn 2...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		 * .... ....  .... ....  .... ....  .... ....  .... ....  ....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		 * It is created by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		 * 1. set current location to 0, PFN index i to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		 * 2. put pfn[i] at current location in Little Endian byte order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		 * 3. calculate an intermediate value as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		 *    x = (pfn[i+1] << 4) | (pfn[i] & 0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		 * 4. put x at offset (current location + 2) in LE byte order
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		 * 5. increment current location by 5 bytes, increment i by 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		 * 6. continue to (2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		if (i & 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			put_unaligned_le32((pg_table[0] & 0xf) | pfn << 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 					   pg_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			put_unaligned_le32(pfn, pg_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	return pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) EXPORT_SYMBOL(snd_sof_create_page_table);