Skip to contentSkip to conversation

drm/amd/amdgpu: add support for iova_to_phys to replace TTM trace (v4)

PATCH In review Tom St Denis 2 participants 2 messages Sep 19, 2017 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amd/amdgpu: add support for iova_to_phys to replace TTM trace (v4)

(v2): Add domain to iova debugfs
(v3): Add true read/write methods to access system memory of pages
mapped to the device
(v4): Move get_domain call out of loop and return on error

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 114 ++++++++++++++++++++++++++++++++
 1 file changed, 114 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +114 -0
index 50d20903de4f..71a2fdc91a85 100644
@@ -43,6 +43,7 @@
 #include <linux/swap.h>
 #include <linux/pagemap.h>
 #include <linux/debugfs.h>
+#include <linux/iommu.h>
 #include "amdgpu.h"
 #include "amdgpu_trace.h"
 #include "bif/bif_4_1_d.h"
@@ -1810,6 +1811,118 @@ static const struct file_operations amdgpu_ttm_gtt_fops = {
 #endif
 
 
+static void *transform_page(uint64_t phys)
+{
+	if (PageHighMem(pfn_to_page(PFN_DOWN(phys))))
+		return kmap(pfn_to_page(PFN_DOWN(phys)));
+	else
+		return __va(phys);
+}
+
+static void untransform_page(uint64_t phys)
+{
+	if (PageHighMem(pfn_to_page(PFN_DOWN(phys))))
+		return kunmap(pfn_to_page(PFN_DOWN(phys)));
+}
+
+static ssize_t amdgpu_iova_to_phys_read(struct file *f, char __user *buf,
+				   size_t size, loff_t *pos)
+{
+	struct amdgpu_device *adev = file_inode(f)->i_private;
+	ssize_t result, n;
+	int r;
+	uint64_t phys;
+	void *ptr;
+	struct iommu_domain *dom;
+
+	dom = iommu_get_domain_for_dev(adev->dev);
+	if (!dom)
+		return -EFAULT;
+
+	result = 0;
+	while (size) {
+		// get physical address and map
+		phys = iommu_iova_to_phys(dom, *pos);
+
+		// copy upto one page
+		if (size > PAGE_SIZE)
+			n = PAGE_SIZE;
+		else
+			n = size;
+
+		// to end of the page
+		if (((*pos & (PAGE_SIZE - 1)) + n) >= PAGE_SIZE)
+			n = PAGE_SIZE - (*pos & (PAGE_SIZE - 1));
+
+		ptr = transform_page(phys);
+		if (!ptr)
+			return -EFAULT;
+
+		r = copy_to_user(buf, ptr, n);
+		untransform_page(phys);
+		if (r)
+			return -EFAULT;
+
+		*pos += n;
+		size -= n;
+		result += n;
+	}
+
+	return result;
+}
+
+static ssize_t amdgpu_iova_to_phys_write(struct file *f, const char __user *buf,
+				   size_t size, loff_t *pos)
+{
+	struct amdgpu_device *adev = file_inode(f)->i_private;
+	ssize_t result, n;
+	int r;
+	uint64_t phys;
+	void *ptr;
+	struct iommu_domain *dom;
+
+	dom = iommu_get_domain_for_dev(adev->dev);
+	if (!dom)
+		return -EFAULT;
+
+	result = 0;
+	while (size) {
+		// get physical address and map
+		phys = iommu_iova_to_phys(dom, *pos);
+
+		// copy upto one page
+		if (size > PAGE_SIZE)
+			n = PAGE_SIZE;
+		else
+			n = size;
+
+		// to end of the page
+		if (((*pos & (PAGE_SIZE - 1)) + n) >= PAGE_SIZE)
+			n = PAGE_SIZE - (*pos & (PAGE_SIZE - 1));
+
+		ptr = transform_page(phys);
+		if (!ptr)
+			return -EFAULT;
+
+		r = copy_from_user(ptr, buf, n);
+		untransform_page(phys);
+		if (r)
+			return -EFAULT;
+
+		*pos += n;
+		size -= n;
+		result += n;
+	}
+
+	return result;
+}
+
+static const struct file_operations amdgpu_ttm_iova_fops = {
+	.owner = THIS_MODULE,
+	.read = amdgpu_iova_to_phys_read,
+	.write = amdgpu_iova_to_phys_write,
+	.llseek = default_llseek
+};
 
 static const struct {
 	char *name;
@@ -1820,6 +1933,7 @@ static const struct {
 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
 	{ "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
 #endif
+	{ "amdgpu_iova", &amdgpu_ttm_iova_fops, TTM_PL_SYSTEM },
 };
 
 #endif
Signature · 6 lines
2.12.0

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw

Am 19.09.2017 um 13:20 schrieb Tom St Denis:

Quoted 29 lines

(v2): Add domain to iova debugfs
(v3): Add true read/write methods to access system memory of pages
mapped to the device
(v4): Move get_domain call out of loop and return on error

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 114 ++++++++++++++++++++++++++++++++
  1 file changed, 114 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
index 50d20903de4f..71a2fdc91a85 100644
@@ -43,6 +43,7 @@
  #include <linux/swap.h>
  #include <linux/pagemap.h>
  #include <linux/debugfs.h>
+#include <linux/iommu.h>
  #include "amdgpu.h"
  #include "amdgpu_trace.h"
  #include "bif/bif_4_1_d.h"
@@ -1810,6 +1811,118 @@ static const struct file_operations amdgpu_ttm_gtt_fops = {
  #endif
 
 
+static void *transform_page(uint64_t phys)
+{
+	if (PageHighMem(pfn_to_page(PFN_DOWN(phys))))

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static const struct file_operations amdgpu_ttm_gtt_fops = { · line 1826

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+		return kmap(pfn_to_page(PFN_DOWN(phys)));
+	else
+		return __va(phys);
+}
+
+static void untransform_page(uint64_t phys)
+{
+	if (PageHighMem(pfn_to_page(PFN_DOWN(phys))))
+		return kunmap(pfn_to_page(PFN_DOWN(phys)));
+}

I mentioned that in the previous patch as well, why not using kmap()/kunmap() here directly?

See kmap() for x86 for example:

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
 void  *kmap <http://elixir.free-electrons.com/linux/v4.2/ident/kmap>(struct  page  *page)
 {
 	might_sleep 

<http://elixir.free-electrons.com/linux/v4.2/ident/might_sleep>();
if (!PageHighMem
<http://elixir.free-electrons.com/linux/v4.2/ident/PageHighMem>(page))
return page_address
<http://elixir.free-electrons.com/linux/v4.2/ident/page_address>(page);
return kmap_high
<http://elixir.free-electrons.com/linux/v4.2/ident/kmap_high>(page);
}

Both normal as well as highmem are correctly handled here.

Regards,
Christian.

Quoted 103 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +99 -0
+
+static ssize_t amdgpu_iova_to_phys_read(struct file *f, char __user *buf,
+				   size_t size, loff_t *pos)
+{
+	struct amdgpu_device *adev = file_inode(f)->i_private;
+	ssize_t result, n;
+	int r;
+	uint64_t phys;
+	void *ptr;
+	struct iommu_domain *dom;
+
+	dom = iommu_get_domain_for_dev(adev->dev);
+	if (!dom)
+		return -EFAULT;
+
+	result = 0;
+	while (size) {
+		// get physical address and map
+		phys = iommu_iova_to_phys(dom, *pos);
+
+		// copy upto one page
+		if (size > PAGE_SIZE)
+			n = PAGE_SIZE;
+		else
+			n = size;
+
+		// to end of the page
+		if (((*pos & (PAGE_SIZE - 1)) + n) >= PAGE_SIZE)
+			n = PAGE_SIZE - (*pos & (PAGE_SIZE - 1));
+
+		ptr = transform_page(phys);
+		if (!ptr)
+			return -EFAULT;
+
+		r = copy_to_user(buf, ptr, n);
+		untransform_page(phys);
+		if (r)
+			return -EFAULT;
+
+		*pos += n;
+		size -= n;
+		result += n;
+	}
+
+	return result;
+}
+
+static ssize_t amdgpu_iova_to_phys_write(struct file *f, const char __user *buf,
+				   size_t size, loff_t *pos)
+{
+	struct amdgpu_device *adev = file_inode(f)->i_private;
+	ssize_t result, n;
+	int r;
+	uint64_t phys;
+	void *ptr;
+	struct iommu_domain *dom;
+
+	dom = iommu_get_domain_for_dev(adev->dev);
+	if (!dom)
+		return -EFAULT;
+
+	result = 0;
+	while (size) {
+		// get physical address and map
+		phys = iommu_iova_to_phys(dom, *pos);
+
+		// copy upto one page
+		if (size > PAGE_SIZE)
+			n = PAGE_SIZE;
+		else
+			n = size;
+
+		// to end of the page
+		if (((*pos & (PAGE_SIZE - 1)) + n) >= PAGE_SIZE)
+			n = PAGE_SIZE - (*pos & (PAGE_SIZE - 1));
+
+		ptr = transform_page(phys);
+		if (!ptr)
+			return -EFAULT;
+
+		r = copy_from_user(ptr, buf, n);
+		untransform_page(phys);
+		if (r)
+			return -EFAULT;
+
+		*pos += n;
+		size -= n;
+		result += n;
+	}
+
+	return result;
+}
+
+static const struct file_operations amdgpu_ttm_iova_fops = {
+	.owner = THIS_MODULE,
+	.read = amdgpu_iova_to_phys_read,
+	.write = amdgpu_iova_to_phys_write,
+	.llseek = default_llseek
+};
 
  static const struct {
  	char *name;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static const struct { · line 1939

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
@@ -1820,6 +1933,7 @@ static const struct {
  #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
  	{ "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
  #endif
+	{ "amdgpu_iova", &amdgpu_ttm_iova_fops, TTM_PL_SYSTEM },
  };
 
  #endif

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx