Skip to content

Commit c0f83d1

Browse files
mszyprowalexdeucher
authored andcommitted
drm/prime: fix extracting of the DMA addresses from a scatterlist
Scatterlist elements contains both pages and DMA addresses, but one should not assume 1:1 relation between them. The sg->length is the size of the physical memory chunk described by the sg->page, while sg_dma_len(sg) is the size of the DMA (IO virtual) chunk described by the sg_dma_address(sg). The proper way of extracting both: pages and DMA addresses of the whole buffer described by a scatterlist it to iterate independently over the sg->pages/sg->length and sg_dma_address(sg)/sg_dma_len(sg) entries. Fixes: 42e67b4 ("drm/prime: use dma length macro when mapping sg") Signed-off-by: Marek Szyprowski <[email protected]> Reviewed-by: Alex Deucher <[email protected]> Signed-off-by: Alex Deucher <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected] Cc: [email protected]
1 parent cc46c03 commit c0f83d1

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

drivers/gpu/drm/drm_prime.c

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -962,27 +962,40 @@ int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
962962
unsigned count;
963963
struct scatterlist *sg;
964964
struct page *page;
965-
u32 len, index;
965+
u32 page_len, page_index;
966966
dma_addr_t addr;
967+
u32 dma_len, dma_index;
967968

968-
index = 0;
969+
/*
970+
* Scatterlist elements contains both pages and DMA addresses, but
971+
* one shoud not assume 1:1 relation between them. The sg->length is
972+
* the size of the physical memory chunk described by the sg->page,
973+
* while sg_dma_len(sg) is the size of the DMA (IO virtual) chunk
974+
* described by the sg_dma_address(sg).
975+
*/
976+
page_index = 0;
977+
dma_index = 0;
969978
for_each_sg(sgt->sgl, sg, sgt->nents, count) {
970-
len = sg_dma_len(sg);
979+
page_len = sg->length;
971980
page = sg_page(sg);
981+
dma_len = sg_dma_len(sg);
972982
addr = sg_dma_address(sg);
973983

974-
while (len > 0) {
975-
if (WARN_ON(index >= max_entries))
984+
while (pages && page_len > 0) {
985+
if (WARN_ON(page_index >= max_entries))
976986
return -1;
977-
if (pages)
978-
pages[index] = page;
979-
if (addrs)
980-
addrs[index] = addr;
981-
987+
pages[page_index] = page;
982988
page++;
989+
page_len -= PAGE_SIZE;
990+
page_index++;
991+
}
992+
while (addrs && dma_len > 0) {
993+
if (WARN_ON(dma_index >= max_entries))
994+
return -1;
995+
addrs[dma_index] = addr;
983996
addr += PAGE_SIZE;
984-
len -= PAGE_SIZE;
985-
index++;
997+
dma_len -= PAGE_SIZE;
998+
dma_index++;
986999
}
9871000
}
9881001
return 0;

0 commit comments

Comments
 (0)