Skip to content

Commit 49a3f51

Browse files
committed
drm/gem: Use struct dma_buf_map in GEM vmap ops and convert GEM backends
This patch replaces the vmap/vunmap's use of raw pointers in GEM object functions with instances of struct dma_buf_map. GEM backends are converted as well. For most of them, this simply changes the returned type. TTM-based drivers now return information about the location of the memory, either system or I/O memory. GEM VRAM helpers and qxl now use ttm_bo_vmap() et al. Amdgpu, nouveau and radeon use drm_gem_ttm_vmap() et al instead of implementing their own vmap callbacks. v7: * init QXL cursor to mapped BO buffer (kernel test robot) v5: * update vkms after switch to shmem v4: * use ttm_bo_vmap(), drm_gem_ttm_vmap(), et al. (Daniel, Christian) * fix a trailing { in drm_gem_vmap() * remove several empty functions instead of converting them (Daniel) * comment uses of raw pointers with a TODO (Daniel) * TODO list: convert more helpers to use struct dma_buf_map Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de> Acked-by: Christian König <christian.koenig@amd.com> Tested-by: Sam Ravnborg <sam@ravnborg.org> Link: https://patchwork.freedesktop.org/patch/msgid/20201103093015.1063-7-tzimmermann@suse.de
1 parent 4367660 commit 49a3f51

49 files changed

Lines changed: 351 additions & 308 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/gpu/todo.rst‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,24 @@ Contact: Ville Syrjälä, Daniel Vetter
450450

451451
Level: Intermediate
452452

453+
Use struct dma_buf_map throughout codebase
454+
------------------------------------------
455+
456+
Pointers to shared device memory are stored in struct dma_buf_map. Each
457+
instance knows whether it refers to system or I/O memory. Most of the DRM-wide
458+
interface have been converted to use struct dma_buf_map, but implementations
459+
often still use raw pointers.
460+
461+
The task is to use struct dma_buf_map where it makes sense.
462+
463+
* Memory managers should use struct dma_buf_map for dma-buf-imported buffers.
464+
* TTM might benefit from using struct dma_buf_map internally.
465+
* Framebuffer copying and blitting helpers should operate on struct dma_buf_map.
466+
467+
Contact: Thomas Zimmermann <tzimmermann@suse.de>, Christian König, Daniel Vetter
468+
469+
Level: Intermediate
470+
453471

454472
Core refactorings
455473
=================

‎drivers/gpu/drm/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ config DRM_RADEON
232232
select FW_LOADER
233233
select DRM_KMS_HELPER
234234
select DRM_TTM
235+
select DRM_TTM_HELPER
235236
select POWER_SUPPLY
236237
select HWMON
237238
select BACKLIGHT_CLASS_DEVICE
@@ -252,6 +253,7 @@ config DRM_AMDGPU
252253
select DRM_KMS_HELPER
253254
select DRM_SCHED
254255
select DRM_TTM
256+
select DRM_TTM_HELPER
255257
select POWER_SUPPLY
256258
select HWMON
257259
select BACKLIGHT_CLASS_DEVICE

‎drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,6 @@
4141
#include <linux/dma-fence-array.h>
4242
#include <linux/pci-p2pdma.h>
4343

44-
/**
45-
* amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
46-
* @obj: GEM BO
47-
*
48-
* Sets up an in-kernel virtual mapping of the BO's memory.
49-
*
50-
* Returns:
51-
* The virtual address of the mapping or an error pointer.
52-
*/
53-
void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
54-
{
55-
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
56-
int ret;
57-
58-
ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
59-
&bo->dma_buf_vmap);
60-
if (ret)
61-
return ERR_PTR(ret);
62-
63-
return bo->dma_buf_vmap.virtual;
64-
}
65-
66-
/**
67-
* amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
68-
* @obj: GEM BO
69-
* @vaddr: Virtual address (unused)
70-
*
71-
* Tears down the in-kernel virtual mapping of the BO's memory.
72-
*/
73-
void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
74-
{
75-
struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
76-
77-
ttm_bo_kunmap(&bo->dma_buf_vmap);
78-
}
79-
8044
/**
8145
* amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
8246
* @obj: GEM BO

‎drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
3131
struct dma_buf *dma_buf);
3232
bool amdgpu_dmabuf_is_xgmi_accessible(struct amdgpu_device *adev,
3333
struct amdgpu_bo *bo);
34-
void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj);
35-
void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
3634
int amdgpu_gem_prime_mmap(struct drm_gem_object *obj,
3735
struct vm_area_struct *vma);
3836

‎drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <drm/amdgpu_drm.h>
3535
#include <drm/drm_debugfs.h>
36+
#include <drm/drm_gem_ttm_helper.h>
3637

3738
#include "amdgpu.h"
3839
#include "amdgpu_display.h"
@@ -220,8 +221,8 @@ static const struct drm_gem_object_funcs amdgpu_gem_object_funcs = {
220221
.open = amdgpu_gem_object_open,
221222
.close = amdgpu_gem_object_close,
222223
.export = amdgpu_gem_prime_export,
223-
.vmap = amdgpu_gem_prime_vmap,
224-
.vunmap = amdgpu_gem_prime_vunmap,
224+
.vmap = drm_gem_ttm_vmap,
225+
.vunmap = drm_gem_ttm_vunmap,
225226
};
226227

227228
/*

‎drivers/gpu/drm/amd/amdgpu/amdgpu_object.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ struct amdgpu_bo {
100100
struct amdgpu_bo *parent;
101101
struct amdgpu_bo *shadow;
102102

103-
struct ttm_bo_kmap_obj dma_buf_vmap;
104103
struct amdgpu_mn *mn;
105104

106105

‎drivers/gpu/drm/ast/ast_cursor.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void ast_cursor_fini(struct ast_private *ast)
3939

4040
for (i = 0; i < ARRAY_SIZE(ast->cursor.gbo); ++i) {
4141
gbo = ast->cursor.gbo[i];
42-
drm_gem_vram_vunmap(gbo, ast->cursor.vaddr[i]);
42+
drm_gem_vram_vunmap(gbo, &ast->cursor.map[i]);
4343
drm_gem_vram_unpin(gbo);
4444
drm_gem_vram_put(gbo);
4545
}
@@ -60,7 +60,7 @@ int ast_cursor_init(struct ast_private *ast)
6060
struct drm_device *dev = &ast->base;
6161
size_t size, i;
6262
struct drm_gem_vram_object *gbo;
63-
void __iomem *vaddr;
63+
struct dma_buf_map map;
6464
int ret;
6565

6666
size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
@@ -77,16 +77,15 @@ int ast_cursor_init(struct ast_private *ast)
7777
drm_gem_vram_put(gbo);
7878
goto err_drm_gem_vram_put;
7979
}
80-
vaddr = drm_gem_vram_vmap(gbo);
81-
if (IS_ERR(vaddr)) {
82-
ret = PTR_ERR(vaddr);
80+
ret = drm_gem_vram_vmap(gbo, &map);
81+
if (ret) {
8382
drm_gem_vram_unpin(gbo);
8483
drm_gem_vram_put(gbo);
8584
goto err_drm_gem_vram_put;
8685
}
8786

8887
ast->cursor.gbo[i] = gbo;
89-
ast->cursor.vaddr[i] = vaddr;
88+
ast->cursor.map[i] = map;
9089
}
9190

9291
return drmm_add_action_or_reset(dev, ast_cursor_release, NULL);
@@ -95,7 +94,7 @@ int ast_cursor_init(struct ast_private *ast)
9594
while (i) {
9695
--i;
9796
gbo = ast->cursor.gbo[i];
98-
drm_gem_vram_vunmap(gbo, ast->cursor.vaddr[i]);
97+
drm_gem_vram_vunmap(gbo, &ast->cursor.map[i]);
9998
drm_gem_vram_unpin(gbo);
10099
drm_gem_vram_put(gbo);
101100
}
@@ -170,6 +169,7 @@ int ast_cursor_blit(struct ast_private *ast, struct drm_framebuffer *fb)
170169
{
171170
struct drm_device *dev = &ast->base;
172171
struct drm_gem_vram_object *gbo;
172+
struct dma_buf_map map;
173173
int ret;
174174
void *src;
175175
void __iomem *dst;
@@ -183,18 +183,17 @@ int ast_cursor_blit(struct ast_private *ast, struct drm_framebuffer *fb)
183183
ret = drm_gem_vram_pin(gbo, 0);
184184
if (ret)
185185
return ret;
186-
src = drm_gem_vram_vmap(gbo);
187-
if (IS_ERR(src)) {
188-
ret = PTR_ERR(src);
186+
ret = drm_gem_vram_vmap(gbo, &map);
187+
if (ret)
189188
goto err_drm_gem_vram_unpin;
190-
}
189+
src = map.vaddr; /* TODO: Use mapping abstraction properly */
191190

192-
dst = ast->cursor.vaddr[ast->cursor.next_index];
191+
dst = ast->cursor.map[ast->cursor.next_index].vaddr_iomem;
193192

194193
/* do data transfer to cursor BO */
195194
update_cursor_image(dst, src, fb->width, fb->height);
196195

197-
drm_gem_vram_vunmap(gbo, src);
196+
drm_gem_vram_vunmap(gbo, &map);
198197
drm_gem_vram_unpin(gbo);
199198

200199
return 0;
@@ -257,7 +256,7 @@ void ast_cursor_show(struct ast_private *ast, int x, int y,
257256
u8 __iomem *sig;
258257
u8 jreg;
259258

260-
dst = ast->cursor.vaddr[ast->cursor.next_index];
259+
dst = ast->cursor.map[ast->cursor.next_index].vaddr;
261260

262261
sig = dst + AST_HWC_SIZE;
263262
writel(x, sig + AST_HWC_SIGNATURE_X);

‎drivers/gpu/drm/ast/ast_drv.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
#ifndef __AST_DRV_H__
2929
#define __AST_DRV_H__
3030

31-
#include <linux/types.h>
32-
#include <linux/io.h>
31+
#include <linux/dma-buf-map.h>
3332
#include <linux/i2c.h>
3433
#include <linux/i2c-algo-bit.h>
34+
#include <linux/io.h>
35+
#include <linux/types.h>
3536

3637
#include <drm/drm_connector.h>
3738
#include <drm/drm_crtc.h>
@@ -131,7 +132,7 @@ struct ast_private {
131132

132133
struct {
133134
struct drm_gem_vram_object *gbo[AST_DEFAULT_HWC_NUM];
134-
void __iomem *vaddr[AST_DEFAULT_HWC_NUM];
135+
struct dma_buf_map map[AST_DEFAULT_HWC_NUM];
135136
unsigned int next_index;
136137
} cursor;
137138

‎drivers/gpu/drm/drm_gem.c‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/pagemap.h>
3737
#include <linux/shmem_fs.h>
3838
#include <linux/dma-buf.h>
39+
#include <linux/dma-buf-map.h>
3940
#include <linux/mem_encrypt.h>
4041
#include <linux/pagevec.h>
4142

@@ -1207,26 +1208,30 @@ void drm_gem_unpin(struct drm_gem_object *obj)
12071208

12081209
void *drm_gem_vmap(struct drm_gem_object *obj)
12091210
{
1210-
void *vaddr;
1211+
struct dma_buf_map map;
1212+
int ret;
12111213

1212-
if (obj->funcs->vmap)
1213-
vaddr = obj->funcs->vmap(obj);
1214-
else
1215-
vaddr = ERR_PTR(-EOPNOTSUPP);
1214+
if (!obj->funcs->vmap)
1215+
return ERR_PTR(-EOPNOTSUPP);
12161216

1217-
if (!vaddr)
1218-
vaddr = ERR_PTR(-ENOMEM);
1217+
ret = obj->funcs->vmap(obj, &map);
1218+
if (ret)
1219+
return ERR_PTR(ret);
1220+
else if (dma_buf_map_is_null(&map))
1221+
return ERR_PTR(-ENOMEM);
12191222

1220-
return vaddr;
1223+
return map.vaddr;
12211224
}
12221225

12231226
void drm_gem_vunmap(struct drm_gem_object *obj, void *vaddr)
12241227
{
1228+
struct dma_buf_map map = DMA_BUF_MAP_INIT_VADDR(vaddr);
1229+
12251230
if (!vaddr)
12261231
return;
12271232

12281233
if (obj->funcs->vunmap)
1229-
obj->funcs->vunmap(obj, vaddr);
1234+
obj->funcs->vunmap(obj, &map);
12301235
}
12311236

12321237
/**

‎drivers/gpu/drm/drm_gem_cma_helper.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
519519
* drm_gem_cma_prime_vmap - map a CMA GEM object into the kernel's virtual
520520
* address space
521521
* @obj: GEM object
522+
* @map: Returns the kernel virtual address of the CMA GEM object's backing
523+
* store.
522524
*
523525
* This function maps a buffer exported via DRM PRIME into the kernel's
524526
* virtual address space. Since the CMA buffers are already mapped into the
@@ -527,13 +529,15 @@ EXPORT_SYMBOL_GPL(drm_gem_cma_prime_mmap);
527529
* driver's &drm_gem_object_funcs.vmap callback.
528530
*
529531
* Returns:
530-
* The kernel virtual address of the CMA GEM object's backing store.
532+
* 0 on success, or a negative error code otherwise.
531533
*/
532-
void *drm_gem_cma_prime_vmap(struct drm_gem_object *obj)
534+
int drm_gem_cma_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
533535
{
534536
struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
535537

536-
return cma_obj->vaddr;
538+
dma_buf_map_set_vaddr(map, cma_obj->vaddr);
539+
540+
return 0;
537541
}
538542
EXPORT_SYMBOL_GPL(drm_gem_cma_prime_vmap);
539543

0 commit comments

Comments
 (0)