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