1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* drivers/media/platform/exynos/astc/astc-helper.c
*
* Implementation of helper functions for Samsung ASTC encoder driver
*
* Copyright (c) 2017 Samsung Electronics Co., Ltd.
* http://www.samsung.com
*
* Author: Andrea Bernabei <a.bernabei@samsung.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
//#define DEBUG
#include <linux/kernel.h>
#include <linux/exynos_iovmm.h>
#include <linux/exynos_ion.h>
#include <linux/mm.h>
#include <linux/bitmap.h>
#include <asm/bitops.h>
#include "astc-helper.h"
#include "astc-exynos9810-workaround.h"
/* Pixel format to bits-per-pixel */
int ftm_to_bpp(struct device *dev, struct hwASTC_img_info *img_info) {
switch (img_info->fmt) {
case ASTC_ENCODE_INPUT_PIXEL_FORMAT_RGBA8888: /* RGBA 8888 */
case ASTC_ENCODE_INPUT_PIXEL_FORMAT_ARGB8888: /* ARGB 8888 */
case ASTC_ENCODE_INPUT_PIXEL_FORMAT_BGRA8888: /* BGRA 8888 */
case ASTC_ENCODE_INPUT_PIXEL_FORMAT_ABGR8888: /* ABGR 8888 */
return 4;
break;
default:
dev_err(dev, "invalid input color format (%u)", img_info->fmt);
return -EINVAL;
}
}
/* Convert blksize enum to the corresponding block size in integer */
u32 blksize_enum_to_int(enum hwASTC_blk_size blk_size, int x_or_y)
{
switch (blk_size) {
case ASTC_ENCODE_MODE_BLK_SIZE_4x4: return 4;
case ASTC_ENCODE_MODE_BLK_SIZE_6x6: return 6;
case ASTC_ENCODE_MODE_BLK_SIZE_8x8: return 8;
/*
* Example of future case, once the hw supports different block sizes for x/y
* case ASTC_ENCODE_MODE_BLK_SIZE_4x6: return x_or_y ? 6 : 4;
*/
}
/* unsupported block size */
return 0;
}
u32 blksize_enum_to_x(enum hwASTC_blk_size blk_size) {
return blksize_enum_to_int(blk_size, 0);
}
u32 blksize_enum_to_y(enum hwASTC_blk_size blk_size) {
return blksize_enum_to_int(blk_size, 1);
}
/**
* astc_map_dma_attachment() - Map the DMA attachment, for buffers backed by a dmabuf
* @dev: the platform device
* @plane: the internal representation of the buffer
* @dir: the direction of the DMA transfer that involves the buffer @plane
*
* This is only needed for buffers backed by a dmabuf.
*
* Allocate and return a scatterlist mapped in the device
* address space. The scatterlist point to physical pages.
* We will then use IOMMU to map a CONTIGUOUS chunk of virtual
* memory to it, that we will use for the DMA transfers.
*
* Return:
* 0 on success,
* a PTR_ERR value otherwise
*/
int astc_map_dma_attachment(struct device *dev,
struct astc_buffer_plane_dma *plane,
enum dma_data_direction dir)
{
dev_dbg(dev, "%s: mapping dmabuf %p, attachment %p, DMA_TO_DEVICE? %d\n"
, __func__, plane->dmabuf, plane->attachment
, dir == DMA_TO_DEVICE);
if (plane->dmabuf) {
plane->sgt = dma_buf_map_attachment(plane->attachment, dir);
if (IS_ERR(plane->sgt)) {
dev_err(dev, "%s: failed to map attacment of dma_buf\n",
__func__);
return PTR_ERR(plane->sgt);
}
dev_dbg(dev, "%s: successfully got scatterlist %p for dmabuf %p, DMA_TO_DEVICE? %d\n"
, __func__, plane->sgt, plane->dmabuf
, dir == DMA_TO_DEVICE);
}
return 0;
}
/**
* astc_unmap_dma_attachment() - Unmap the DMA attachment, for buffers backed by a dmabuf
* @dev: the platform device
* @plane: the internal representation of the buffer
* @dir: the direction of the DMA transfer that involves the buffer @plane
*
* This is only needed for buffers backed by a dmabuf.
* Unmap the DMA attachment to tell the exporter that we are done using
* the dma buffer.
*/
void astc_unmap_dma_attachment(struct device *dev,
struct astc_buffer_plane_dma *plane,
enum dma_data_direction dir)
{
dev_dbg(dev, "%s: unmapping dmabuf %p, attachment %p, scatter table %p, DMA_TO_DEVICE? %d\n"
, __func__, plane->dmabuf, plane->attachment, plane->sgt
, dir == DMA_TO_DEVICE);
/* tell the exporter of the DMA buffer that we are done using it */
if (plane->dmabuf)
dma_buf_unmap_attachment(plane->attachment, plane->sgt, dir);
}
/**
* astc_map_buf_to_device() - Use IOVMM to map a memory region to device address space
* @astc_device: the astc device struct
* @task: the task that is being setup
* @buf: internal representation of the buffer
* @dir: the direction of the DMA transfer that will involve @buffer
*
* Use IOVMM to map the memory region of @buffer to the device address
* space and get the DMA address to it, so that the device can do
* DMA transfers from/to it.
*
* When the user buffer is represented by a pointer to user memory and is
* not backed by a dmabuf, use the Exynos IOVMM to map it to the device
* address space via the IOMMU.
* Otherwise, when it is backed by a dmabuf attachment,
* use ION IOVMM to map it to device address space.
*
* When the driver is running on Exynos9810 SoC, this also takes
* care of mapping additional pages (if needed) to workaround a H/W bug.
*
* TODO: add workaround for Exynos9810 H/W bug for buffers backed by
* a dmabuf.
*
* Return:
* 0 on success,
* <0 error code otherwise
*/
int astc_map_buf_to_device(struct astc_dev *astc_device,
struct astc_task *task,
struct astc_buffer_dma *buf,
enum dma_data_direction dir)
{
struct device *dev = astc_device->dev;
struct astc_buffer_plane_dma *plane = &buf->plane;
int prot = IOMMU_READ;
dma_addr_t iova;
if (!task) {
dev_err(dev, "%s: null task\n", __func__);
/*
* return a value outside of ERRNO, to avoid confusion
* with the values returned by exynos_iovmm_map_userptr
*/
return (-MAX_ERRNO - 1);
}
dev_dbg(dev, "%s: using iovmm to get dma address, DMA_TO_DEVICE? %d\n"
, __func__, dir == DMA_TO_DEVICE);
if (dir != DMA_TO_DEVICE)
prot |= IOMMU_WRITE;
/*
* IOMMU_CACHE will cause IOMMU to toggle a specific bit in the page
* descriptors of the buffer to signal coherence managers/protocols that
* those pages are shared so the transfers need to be coherent.
*
* NOTE: the assumption here is that there is something in the device or
* between the device and the rest of the SoC that will pick the hint up
* and will enforce coherence.
*
* In Exynos9810 the ASTC device itself is not I/O coherent (AXI),
* BUT it is connected to an ACE-LITE adapter, which provides coherence,
* so the combination is coherent and we can use this prot.
*/
if (device_get_dma_attr(dev) == DEV_DMA_COHERENT)
prot |= IOMMU_CACHE;
if (plane->dmabuf) {
dev_dbg(dev, "%s: getting dma addr of plane, DMA attachment %p bytes %zu, DMA_TO_DEVICE? %d\n"
, __func__, plane->attachment, plane->bytes_used
, dir == DMA_TO_DEVICE);
iova = ion_iovmm_map(plane->attachment, 0,
plane->bytes_used, dir, prot);
} else {
dev_dbg(dev, "%s: getting dma addr of plane, userptr %lu bytes %zu, DMA_TO_DEVICE? %d\n"
, __func__, buf->buffer->userptr
, plane->bytes_used, dir == DMA_TO_DEVICE);
down_read(¤t->mm->mmap_sem);
iova = exynos_iovmm_map_userptr(dev,
buf->buffer->userptr,
plane->bytes_used, prot);
up_read(¤t->mm->mmap_sem);
}
if (IS_ERR_VALUE(iova)) {
if (is_exynos9810bug_workaround_active(task)) {
dev_err(dev, "%s: the image buffer is probably too small. The ASTC encoder of this SoC requires the image buffer size to be big enough to hold data of an image where width and height are rounded up to be multiple of block size.\n", __func__);
}
return (int)iova;
}
buf->plane.dma_addr = iova + plane->offset;
/* Check that the addition did not wrap around */
if (buf->plane.dma_addr < iova) {
/* undo mapping */
if (plane->dmabuf) {
dev_err(dev, "%s: WRAPAROUND UNMAPPING NOT IMPLEMENTED\n", __func__);
} else {
exynos_iovmm_unmap_userptr(dev, iova);
}
dev_err(dev, "%s: dma addr computation wrapped around\n", __func__);
return (-MAX_ERRNO - 4);
}
dev_dbg(dev, "%s: got dma address %pad, DMA_TO_DEVICE? %d\n"
, __func__, &buf->plane.dma_addr, dir == DMA_TO_DEVICE);
return 0;
}
/**
* astc_unmap_buf_from_device() - Use IOVMM to unmap the buffer at a given DMA address
* @dev: the platform device struct
* @buf: internal representation of the buffer
*
* Unmap the device-space buffer which is at the DMA address held in the
* @buf struct.
*
* When the user buffer is represented by a pointer to user memory which is
* not backed by a dmabuf, use the Exynos IOVMM to unmap it.
* Otherwise, use ION IOVMM to unmap it.
*
* Return:
* 0 on success,
* -ERRNO if the mapping fails,
* values < -ERRNO for other errors.
*/
int astc_unmap_buf_from_device(struct device *dev,
struct astc_buffer_dma *buf)
{
struct astc_buffer_plane_dma *plane = &buf->plane;
dma_addr_t dma_addr = plane->dma_addr - plane->offset;
if (dma_addr > plane->dma_addr) {
dev_err(dev, "%s: dma addr computation wrapped around\n", __func__);
return -1;
}
dev_dbg(dev, "%s: umapping dma address %pad\n"
, __func__, &dma_addr);
if (plane->dmabuf)
ion_iovmm_unmap(plane->attachment, dma_addr);
else
exynos_iovmm_unmap_userptr(dev, dma_addr);
plane->dma_addr = 0;
return 0;
}
/* CPU cache sync procedures, needed if the device is not dma-coherent.
* Unused on Exynos981, as HW ASTC device on Exynos9810 is DMA coherent. */
void astc_sync_for_device(struct device *dev,
struct astc_buffer_plane_dma *plane,
enum dma_data_direction dir)
{
if (plane->dmabuf)
exynos_ion_sync_dmabuf_for_device(dev, plane->dmabuf,
plane->bytes_used, dir);
else
exynos_iommu_sync_for_device(dev, plane->dma_addr,
plane->bytes_used, dir);
}
void astc_sync_for_cpu(struct device *dev,
struct astc_buffer_plane_dma *plane,
enum dma_data_direction dir)
{
if (plane->dmabuf)
exynos_ion_sync_dmabuf_for_cpu(dev, plane->dmabuf,
plane->bytes_used, dir);
else
exynos_iommu_sync_for_cpu(dev, plane->dma_addr,
plane->bytes_used, dir);
}
|