Commit 71ce0463 authored by Zack Rusin's avatar Zack Rusin

drm/ttm: Make sure the mapped tt pages are decrypted when needed

Some drivers require the mapped tt pages to be decrypted. In an ideal
world this would have been handled by the dma layer, but the TTM page
fault handling would have to be rewritten to able to do that.

A side-effect of the TTM page fault handling is using a dma allocation
per order (via ttm_pool_alloc_page) which makes it impossible to just
trivially use dma_mmap_attrs. As a result ttm has to be very careful
about trying to make its pgprot for the mapped tt pages match what
the dma layer thinks it is. At the ttm layer it's possible to
deduce the requirement to have tt pages decrypted by checking
whether coherent dma allocations have been requested and the system
is running with confidential computing technologies.

This approach isn't ideal but keeping TTM matching DMAs expectations
for the page properties is in general fragile, unfortunately proper
fix would require a rewrite of TTM's page fault handling.

Fixes vmwgfx with SEV enabled.

v2: Explicitly include cc_platform.h
v3: Use CC_ATTR_GUEST_MEM_ENCRYPT instead of CC_ATTR_MEM_ENCRYPT to
limit the scope to guests and log when memory decryption is enabled.
Signed-off-by: default avatarZack Rusin <zack.rusin@broadcom.com>
Fixes: 3bf3710e ("drm/ttm: Add a generic TTM memcpy move for page-based iomem")
Reviewed-by: default avatarThomas Hellström <thomas.hellstrom@linux.intel.com>
Acked-by: default avatarChristian König <christian.koenig@amd.com>
Cc: Huang Rui <ray.huang@amd.com>
Cc: dri-devel@lists.freedesktop.org
Cc: linux-kernel@vger.kernel.org
Cc: <stable@vger.kernel.org> # v5.14+
Link: https://patchwork.freedesktop.org/patch/msgid/20230926040359.3040017-1-zack@kde.org
parent 517621b7
...@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res, ...@@ -294,7 +294,13 @@ pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
enum ttm_caching caching; enum ttm_caching caching;
man = ttm_manager_type(bo->bdev, res->mem_type); man = ttm_manager_type(bo->bdev, res->mem_type);
caching = man->use_tt ? bo->ttm->caching : res->bus.caching; if (man->use_tt) {
caching = bo->ttm->caching;
if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
tmp = pgprot_decrypted(tmp);
} else {
caching = res->bus.caching;
}
return ttm_prot_from_caching(caching, tmp); return ttm_prot_from_caching(caching, tmp);
} }
...@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo, ...@@ -337,6 +343,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
.no_wait_gpu = false .no_wait_gpu = false
}; };
struct ttm_tt *ttm = bo->ttm; struct ttm_tt *ttm = bo->ttm;
struct ttm_resource_manager *man =
ttm_manager_type(bo->bdev, bo->resource->mem_type);
pgprot_t prot; pgprot_t prot;
int ret; int ret;
...@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo, ...@@ -346,7 +354,8 @@ static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
if (ret) if (ret)
return ret; return ret;
if (num_pages == 1 && ttm->caching == ttm_cached) { if (num_pages == 1 && ttm->caching == ttm_cached &&
!(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
/* /*
* We're mapping a single page, and the desired * We're mapping a single page, and the desired
* page protection is consistent with the bo. * page protection is consistent with the bo.
......
...@@ -31,11 +31,13 @@ ...@@ -31,11 +31,13 @@
#define pr_fmt(fmt) "[TTM] " fmt #define pr_fmt(fmt) "[TTM] " fmt
#include <linux/cc_platform.h>
#include <linux/sched.h> #include <linux/sched.h>
#include <linux/shmem_fs.h> #include <linux/shmem_fs.h>
#include <linux/file.h> #include <linux/file.h>
#include <linux/module.h> #include <linux/module.h>
#include <drm/drm_cache.h> #include <drm/drm_cache.h>
#include <drm/drm_device.h>
#include <drm/drm_util.h> #include <drm/drm_util.h>
#include <drm/ttm/ttm_bo.h> #include <drm/ttm/ttm_bo.h>
#include <drm/ttm/ttm_tt.h> #include <drm/ttm/ttm_tt.h>
...@@ -61,6 +63,7 @@ static atomic_long_t ttm_dma32_pages_allocated; ...@@ -61,6 +63,7 @@ static atomic_long_t ttm_dma32_pages_allocated;
int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc) int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
{ {
struct ttm_device *bdev = bo->bdev; struct ttm_device *bdev = bo->bdev;
struct drm_device *ddev = bo->base.dev;
uint32_t page_flags = 0; uint32_t page_flags = 0;
dma_resv_assert_held(bo->base.resv); dma_resv_assert_held(bo->base.resv);
...@@ -82,6 +85,15 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc) ...@@ -82,6 +85,15 @@ int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc)
pr_err("Illegal buffer object type\n"); pr_err("Illegal buffer object type\n");
return -EINVAL; return -EINVAL;
} }
/*
* When using dma_alloc_coherent with memory encryption the
* mapped TT pages need to be decrypted or otherwise the drivers
* will end up sending encrypted mem to the gpu.
*/
if (bdev->pool.use_dma_alloc && cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
page_flags |= TTM_TT_FLAG_DECRYPTED;
drm_info(ddev, "TT memory decryption enabled.");
}
bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags); bo->ttm = bdev->funcs->ttm_tt_create(bo, page_flags);
if (unlikely(bo->ttm == NULL)) if (unlikely(bo->ttm == NULL))
......
...@@ -79,6 +79,12 @@ struct ttm_tt { ...@@ -79,6 +79,12 @@ struct ttm_tt {
* page_flags = TTM_TT_FLAG_EXTERNAL | * page_flags = TTM_TT_FLAG_EXTERNAL |
* TTM_TT_FLAG_EXTERNAL_MAPPABLE; * TTM_TT_FLAG_EXTERNAL_MAPPABLE;
* *
* TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as
* not encrypted. The framework will try to match what the dma layer
* is doing, but note that it is a little fragile because ttm page
* fault handling abuses the DMA api a bit and dma_map_attrs can't be
* used to assure pgprot always matches.
*
* TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
* set by TTM after ttm_tt_populate() has successfully returned, and is * set by TTM after ttm_tt_populate() has successfully returned, and is
* then unset when TTM calls ttm_tt_unpopulate(). * then unset when TTM calls ttm_tt_unpopulate().
...@@ -87,8 +93,9 @@ struct ttm_tt { ...@@ -87,8 +93,9 @@ struct ttm_tt {
#define TTM_TT_FLAG_ZERO_ALLOC BIT(1) #define TTM_TT_FLAG_ZERO_ALLOC BIT(1)
#define TTM_TT_FLAG_EXTERNAL BIT(2) #define TTM_TT_FLAG_EXTERNAL BIT(2)
#define TTM_TT_FLAG_EXTERNAL_MAPPABLE BIT(3) #define TTM_TT_FLAG_EXTERNAL_MAPPABLE BIT(3)
#define TTM_TT_FLAG_DECRYPTED BIT(4)
#define TTM_TT_FLAG_PRIV_POPULATED BIT(4) #define TTM_TT_FLAG_PRIV_POPULATED BIT(5)
uint32_t page_flags; uint32_t page_flags;
/** @num_pages: Number of pages in the page array. */ /** @num_pages: Number of pages in the page array. */
uint32_t num_pages; uint32_t num_pages;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment