Skip to content

Commit 8b7c028

Browse files
mszyprowmchehab
authored andcommitted
media: videobuf2: use sgtable-based scatterlist wrappers
Use recently introduced common wrappers operating directly on the struct sg_table objects and scatterlist page iterators to make the code a bit more compact, robust, easier to follow and copy/paste safe. No functional change, because the code already properly did all the scatterlist related calls. Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> Reviewed-by: Robin Murphy <robin.murphy@arm.com> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
1 parent f95fc01 commit 8b7c028

3 files changed

Lines changed: 31 additions & 47 deletions

File tree

‎drivers/media/common/videobuf2/videobuf2-dma-contig.c‎

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
5858
unsigned int i;
5959
unsigned long size = 0;
6060

61-
for_each_sg(sgt->sgl, s, sgt->nents, i) {
61+
for_each_sgtable_dma_sg(sgt, s, i) {
6262
if (sg_dma_address(s) != expected)
6363
break;
64-
expected = sg_dma_address(s) + sg_dma_len(s);
64+
expected += sg_dma_len(s);
6565
size += sg_dma_len(s);
6666
}
6767
return size;
@@ -103,8 +103,7 @@ static void vb2_dc_prepare(void *buf_priv)
103103
if (!sgt)
104104
return;
105105

106-
dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
107-
buf->dma_dir);
106+
dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir);
108107
}
109108

110109
static void vb2_dc_finish(void *buf_priv)
@@ -115,7 +114,7 @@ static void vb2_dc_finish(void *buf_priv)
115114
if (!sgt)
116115
return;
117116

118-
dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
117+
dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir);
119118
}
120119

121120
/*********************************************/
@@ -275,8 +274,8 @@ static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
275274
* memory locations do not require any explicit cache
276275
* maintenance prior or after being used by the device.
277276
*/
278-
dma_unmap_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents,
279-
attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
277+
dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir,
278+
DMA_ATTR_SKIP_CPU_SYNC);
280279
sg_free_table(sgt);
281280
kfree(attach);
282281
db_attach->priv = NULL;
@@ -301,18 +300,17 @@ static struct sg_table *vb2_dc_dmabuf_ops_map(
301300

302301
/* release any previous cache */
303302
if (attach->dma_dir != DMA_NONE) {
304-
dma_unmap_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents,
305-
attach->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
303+
dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir,
304+
DMA_ATTR_SKIP_CPU_SYNC);
306305
attach->dma_dir = DMA_NONE;
307306
}
308307

309308
/*
310309
* mapping to the client with new direction, no cache sync
311310
* required see comment in vb2_dc_dmabuf_ops_detach()
312311
*/
313-
sgt->nents = dma_map_sg_attrs(db_attach->dev, sgt->sgl, sgt->orig_nents,
314-
dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
315-
if (!sgt->nents) {
312+
if (dma_map_sgtable(db_attach->dev, sgt, dma_dir,
313+
DMA_ATTR_SKIP_CPU_SYNC)) {
316314
pr_err("failed to map scatterlist\n");
317315
mutex_unlock(lock);
318316
return ERR_PTR(-EIO);
@@ -455,8 +453,8 @@ static void vb2_dc_put_userptr(void *buf_priv)
455453
* No need to sync to CPU, it's already synced to the CPU
456454
* since the finish() memop will have been called before this.
457455
*/
458-
dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
459-
buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
456+
dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir,
457+
DMA_ATTR_SKIP_CPU_SYNC);
460458
pages = frame_vector_pages(buf->vec);
461459
/* sgt should exist only if vector contains pages... */
462460
BUG_ON(IS_ERR(pages));
@@ -553,9 +551,8 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
553551
* No need to sync to the device, this will happen later when the
554552
* prepare() memop is called.
555553
*/
556-
sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
557-
buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
558-
if (sgt->nents <= 0) {
554+
if (dma_map_sgtable(buf->dev, sgt, buf->dma_dir,
555+
DMA_ATTR_SKIP_CPU_SYNC)) {
559556
pr_err("failed to map scatterlist\n");
560557
ret = -EIO;
561558
goto fail_sgt_init;
@@ -577,8 +574,7 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
577574
return buf;
578575

579576
fail_map_sg:
580-
dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
581-
buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
577+
dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
582578

583579
fail_sgt_init:
584580
sg_free_table(sgt);

‎drivers/media/common/videobuf2/videobuf2-dma-sg.c‎

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs,
148148
* No need to sync to the device, this will happen later when the
149149
* prepare() memop is called.
150150
*/
151-
sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
152-
buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
153-
if (!sgt->nents)
151+
if (dma_map_sgtable(buf->dev, sgt, buf->dma_dir,
152+
DMA_ATTR_SKIP_CPU_SYNC))
154153
goto fail_map;
155154

156155
buf->handler.refcount = &buf->refcount;
@@ -186,8 +185,8 @@ static void vb2_dma_sg_put(void *buf_priv)
186185
if (refcount_dec_and_test(&buf->refcount)) {
187186
dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
188187
buf->num_pages);
189-
dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
190-
buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
188+
dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir,
189+
DMA_ATTR_SKIP_CPU_SYNC);
191190
if (buf->vaddr)
192191
vm_unmap_ram(buf->vaddr, buf->num_pages);
193192
sg_free_table(buf->dma_sgt);
@@ -204,16 +203,15 @@ static void vb2_dma_sg_prepare(void *buf_priv)
204203
struct vb2_dma_sg_buf *buf = buf_priv;
205204
struct sg_table *sgt = buf->dma_sgt;
206205

207-
dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
208-
buf->dma_dir);
206+
dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir);
209207
}
210208

211209
static void vb2_dma_sg_finish(void *buf_priv)
212210
{
213211
struct vb2_dma_sg_buf *buf = buf_priv;
214212
struct sg_table *sgt = buf->dma_sgt;
215213

216-
dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
214+
dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir);
217215
}
218216

219217
static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
@@ -256,9 +254,8 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
256254
* No need to sync to the device, this will happen later when the
257255
* prepare() memop is called.
258256
*/
259-
sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
260-
buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
261-
if (!sgt->nents)
257+
if (dma_map_sgtable(buf->dev, sgt, buf->dma_dir,
258+
DMA_ATTR_SKIP_CPU_SYNC))
262259
goto userptr_fail_map;
263260

264261
return buf;
@@ -284,8 +281,7 @@ static void vb2_dma_sg_put_userptr(void *buf_priv)
284281

285282
dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
286283
__func__, buf->num_pages);
287-
dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
288-
DMA_ATTR_SKIP_CPU_SYNC);
284+
dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
289285
if (buf->vaddr)
290286
vm_unmap_ram(buf->vaddr, buf->num_pages);
291287
sg_free_table(buf->dma_sgt);
@@ -408,8 +404,7 @@ static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
408404

409405
/* release the scatterlist cache */
410406
if (attach->dma_dir != DMA_NONE)
411-
dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
412-
attach->dma_dir);
407+
dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
413408
sg_free_table(sgt);
414409
kfree(attach);
415410
db_attach->priv = NULL;
@@ -434,15 +429,12 @@ static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
434429

435430
/* release any previous cache */
436431
if (attach->dma_dir != DMA_NONE) {
437-
dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
438-
attach->dma_dir);
432+
dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
439433
attach->dma_dir = DMA_NONE;
440434
}
441435

442436
/* mapping to the client with new direction */
443-
sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
444-
dma_dir);
445-
if (!sgt->nents) {
437+
if (dma_map_sgtable(db_attach->dev, sgt, dma_dir, 0)) {
446438
pr_err("failed to map scatterlist\n");
447439
mutex_unlock(lock);
448440
return ERR_PTR(-EIO);

‎drivers/media/common/videobuf2/videobuf2-vmalloc.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static int vb2_vmalloc_dmabuf_ops_attach(struct dma_buf *dbuf,
229229
kfree(attach);
230230
return ret;
231231
}
232-
for_each_sg(sgt->sgl, sg, sgt->nents, i) {
232+
for_each_sgtable_sg(sgt, sg, i) {
233233
struct page *page = vmalloc_to_page(vaddr);
234234

235235
if (!page) {
@@ -259,8 +259,7 @@ static void vb2_vmalloc_dmabuf_ops_detach(struct dma_buf *dbuf,
259259

260260
/* release the scatterlist cache */
261261
if (attach->dma_dir != DMA_NONE)
262-
dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
263-
attach->dma_dir);
262+
dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
264263
sg_free_table(sgt);
265264
kfree(attach);
266265
db_attach->priv = NULL;
@@ -285,15 +284,12 @@ static struct sg_table *vb2_vmalloc_dmabuf_ops_map(
285284

286285
/* release any previous cache */
287286
if (attach->dma_dir != DMA_NONE) {
288-
dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
289-
attach->dma_dir);
287+
dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
290288
attach->dma_dir = DMA_NONE;
291289
}
292290

293291
/* mapping to the client with new direction */
294-
sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
295-
dma_dir);
296-
if (!sgt->nents) {
292+
if (dma_map_sgtable(db_attach->dev, sgt, dma_dir, 0)) {
297293
pr_err("failed to map scatterlist\n");
298294
mutex_unlock(lock);
299295
return ERR_PTR(-EIO);

0 commit comments

Comments
 (0)