Host1x context isolation support
Subsystems (6)
Mikko Perttunen
maintainerv2 cover Host1x context isolation support
Hi all,
***
New in v2:
Added support for Tegra194
Use standard iommu-map property instead of custom mechanism
***
this series adds support for Host1x 'context isolation'. Since when programming engines through Host1x, userspace can program in any addresses it wants, we need some way to isolate the engines' memory spaces. Traditionally this has either been done imperfectly with a single shared IOMMU domain, or by copying and verifying the programming command stream at submit time (Host1x firewall).
Since Tegra186 there is a privileged (only usable by kernel) Host1x opcode that allows setting the stream ID sent by the engine to the SMMU. So, by allocating a number of context banks and stream IDs for this purpose, and using this opcode at the beginning of each job, we can implement isolation. Due to the limited number of context banks only each process gets its own context, and not each channel.
This feature also allows sharing engines among multiple VMs when used with Host1x's hardware virtualization support - up to 8 VMs can be configured with a subset of allowed stream IDs, enforced at hardware level.
To implement this, this series adds a new host1x context bus, which will contain the 'struct device's corresponding to each context bank / stream ID, changes to device tree and SMMU code to allow registering the devices and using the bus, as well as the Host1x stream ID programming code and support in TegraDRM.
Device tree bindings are not updated yet pending consensus that the proposed changes make sense.
Thanks,
Mikko
Mikko Perttunen (8):
gpu: host1x: Add context bus
gpu: host1x: Add context device management code
gpu: host1x: Program context stream ID on submission iommu/arm-smmu: Attach to host1x context device bus
arm64: tegra: Add Host1x context stream IDs on Tegra186+ drm/tegra: falcon: Set DMACTX field on DMA transactions drm/tegra: vic: Implement get_streamid_offset drm/tegra: Support context isolation
arch/arm64/boot/dts/nvidia/tegra186.dtsi | 12 ++ arch/arm64/boot/dts/nvidia/tegra194.dtsi | 12 ++ drivers/gpu/Makefile | 3 +- drivers/gpu/drm/tegra/drm.h | 2 + drivers/gpu/drm/tegra/falcon.c | 8 + drivers/gpu/drm/tegra/falcon.h | 1 + drivers/gpu/drm/tegra/submit.c | 13 ++ drivers/gpu/drm/tegra/uapi.c | 34 ++++- drivers/gpu/drm/tegra/vic.c | 38 +++++ drivers/gpu/host1x/Kconfig | 5 + drivers/gpu/host1x/Makefile | 2 + drivers/gpu/host1x/context.c | 174 ++++++++++++++++++++++ drivers/gpu/host1x/context.h | 27 ++++ drivers/gpu/host1x/context_bus.c | 31 ++++ drivers/gpu/host1x/dev.c | 12 +- drivers/gpu/host1x/dev.h | 2 + drivers/gpu/host1x/hw/channel_hw.c | 52 ++++++- drivers/gpu/host1x/hw/host1x06_hardware.h | 10 ++ drivers/gpu/host1x/hw/host1x07_hardware.h | 10 ++ drivers/iommu/arm/arm-smmu/arm-smmu.c | 13 ++ include/linux/host1x.h | 21 +++ include/linux/host1x_context_bus.h | 15 ++ 22 files changed, 488 insertions(+), 9 deletions(-) create mode 100644 drivers/gpu/host1x/context.c create mode 100644 drivers/gpu/host1x/context.h create mode 100644 drivers/gpu/host1x/context_bus.c create mode 100644 include/linux/host1x_context_bus.h
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 1/8 gpu: host1x: Add context bus
The context bus is a "dummy" bus that contains struct devices that correspond to IOMMU contexts assigned through Host1x to processes.
Even when host1x itself is built as a module, the bus is registered in built-in code so that the built-in ARM SMMU driver is able to reference it.
--- drivers/gpu/Makefile | 3 +-- drivers/gpu/host1x/Kconfig | 5 +++++ drivers/gpu/host1x/Makefile | 1 + drivers/gpu/host1x/context_bus.c | 31 ++++++++++++++++++++++++++++++ include/linux/host1x_context_bus.h | 15 +++++++++++++++ 5 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 drivers/gpu/host1x/context_bus.c create mode 100644 include/linux/host1x_context_bus.h
drivers/gpu/Makefile +1 -2
@@ -2,7 +2,6 @@ # drm/tegra depends on host1x, so if both drivers are built-in care must be # taken to initialize them in the correct order. Link order is the only way # to ensure this currently. -obj-$(CONFIG_TEGRA_HOST1X) += host1x/ -obj-y += drm/ vga/ +obj-y += host1x/ drm/ vga/ obj-$(CONFIG_IMX_IPUV3_CORE) += ipu-v3/ obj-$(CONFIG_TRACE_GPU_MEM) += trace/
drivers/gpu/host1x/Kconfig +5 -0
@@ -1,7 +1,12 @@ # SPDX-License-Identifier: GPL-2.0-only + +config TEGRA_HOST1X_CONTEXT_BUS + bool + config TEGRA_HOST1X tristate "NVIDIA Tegra host1x driver" depends on ARCH_TEGRA || (ARM && COMPILE_TEST) + select TEGRA_HOST1X_CONTEXT_BUS select IOMMU_IOVA help Driver for the NVIDIA Tegra host1x hardware.
drivers/gpu/host1x/Makefile +1 -0
@@ -18,3 +18,4 @@ host1x-y = \ hw/host1x07.o obj-$(CONFIG_TEGRA_HOST1X) += host1x.o +obj-$(CONFIG_TEGRA_HOST1X_CONTEXT_BUS) += context_bus.o
drivers/gpu/host1x/context_bus.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (c) 2021, NVIDIA Corporation. +*/ + +#include <linux/device.h> +#include <linux/of.h> + +struct bus_type host1x_context_device_bus_type = { + .name ="host1x-context" , +}; +EXPORT_SYMBOL (host1x_context_device_bus_type); + +static int __init host1x_context_device_bus_init (void ) +{ +int err; + +if (!of_machine_is_compatible ("nvidia,tegra186" ) && + !of_machine_is_compatible ("nvidia,tegra194" ) && + !of_machine_is_compatible ("nvidia,tegra234" )) +return 0 ; + + err =bus_register (&host1x_context_device_bus_type); +if (err <0 ) { +pr_err ("bus type registration failed: %d\n" , err); +return err; + } + +return 0 ; +} +postcore_initcall (host1x_context_device_bus_init);
include/linux/host1x_context_bus.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (c) 2021, NVIDIA Corporation. All rights reserved. +*/ + +#ifndef __LINUX_HOST1X_CONTEXT_BUS_H +#define __LINUX_HOST1X_CONTEXT_BUS_H + +#include <linux/device.h> + +#ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS +extern struct bus_type host1x_context_device_bus_type; +#endif + +#endif
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 2/8 gpu: host1x: Add context device management code
In reply to Mikko PerttunenAdd code to register context devices from device tree, allocate them out and manage their refcounts.
---
v2:
* Directly set DMA mask instead of inheriting from Host1x.
* Use iommu-map instead of custom DT property.
--- drivers/gpu/host1x/Makefile | 1 + drivers/gpu/host1x/context.c | 174 +++++++++++++++++++++++++++++++++++ drivers/gpu/host1x/context.h | 27 ++++++ drivers/gpu/host1x/dev.c | 12 ++- drivers/gpu/host1x/dev.h | 2 + include/linux/host1x.h | 17 ++++ 6 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 drivers/gpu/host1x/context.c create mode 100644 drivers/gpu/host1x/context.h
drivers/gpu/host1x/Makefile +1 -0
@@ -10,6 +10,7 @@ host1x-y = \ debug.o \ mipi.o \ fence.o \ + context.o \ hw/host1x01.o \ hw/host1x02.o \ hw/host1x04.o \
drivers/gpu/host1x/context.c new file +174 -0
174 lines not on this page · raw message
drivers/gpu/host1x/context.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Host1x context devices +* +* Copyright (c) 2020, NVIDIA Corporation. +*/ + +#ifndef __HOST1X_CONTEXT_H +#define __HOST1X_CONTEXT_H + +#include <linux/mutex.h> +#include <linux/refcount.h> + +struct host1x ; + +extern struct bus_type host1x_context_device_bus_type; + +struct host1x_context_list { +struct mutex lock; +struct host1x_context *devs; +unsigned int len; +}; + +int host1x_context_list_init (struct host1x *host1x); +void host1x_context_list_free (struct host1x_context_list *cdl); + +#endif
drivers/gpu/host1x/dev.c +11 -1
@@ -20,6 +20,7 @@#include "bus.h" #include "channel.h" +#include "context.h" #include "debug.h" #include "dev.h" #include "intr.h"
@@ -461,10 +462,16 @@ static int host1x_probe(struct platform_device *pdev) goto iommu_exit; } + err =host1x_context_list_init (host); +if (err) { +dev_err (&pdev->dev,"failed to initialize context list\n" ); + goto free_channels; + } + err =clk_prepare_enable (host->clk);if (err <0 ) {dev_err (&pdev->dev,"failed to enable clock\n" ); - goto free_channels; + goto free_contexts; } err =reset_control_deassert (host->rst);
@@ -511,6 +518,8 @@ static int host1x_probe(struct platform_device *pdev)reset_control_assert (host->rst); unprepare_disable:clk_disable_unprepare (host->clk); +free_contexts: +host1x_context_list_free (&host->context_list); free_channels:host1x_channel_list_free (&host->channel_list); iommu_exit:
@@ -529,6 +538,7 @@ static int host1x_remove(struct platform_device *pdev)host1x_syncpt_deinit (host);reset_control_assert (host->rst);clk_disable_unprepare (host->clk); +host1x_context_list_free (&host->context_list);host1x_iommu_exit (host);return 0 ;
drivers/gpu/host1x/dev.h +2 -0
@@ -14,6 +14,7 @@#include "cdma.h" #include "channel.h" +#include "context.h" #include "intr.h" #include "job.h" #include "syncpt.h"
@@ -140,6 +141,7 @@ struct host1x {struct mutex syncpt_mutex;struct host1x_channel_list channel_list; +struct host1x_context_list context_list;struct dentry *debugfs;
include/linux/host1x.h +17 -0
@@ -396,4 +396,21 @@ int tegra_mipi_disable(struct tegra_mipi_device *device);int tegra_mipi_start_calibration (struct tegra_mipi_device *device);int tegra_mipi_finish_calibration (struct tegra_mipi_device *device); +/* host1x context devices */ + +struct host1x_context { +struct host1x *host; + +refcount_t ref; +struct pid *owner; + +struct device dev; +u32 stream_id; +}; + +struct host1x_context *host1x_context_alloc (struct host1x *host1x, +struct pid *pid); +void host1x_context_get (struct host1x_context *cd); +void host1x_context_put (struct host1x_context *cd); +#endif
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 3/8 gpu: host1x: Program context stream ID on submission
In reply to Mikko PerttunenAdd code to do stream ID switching at the beginning of a job. The stream ID is switched to the stream ID specified by the context passed in the job structure.
Before switching the stream ID, an OP_DONE wait is done on the channel's engine to ensure that there is no residual ongoing work that might do DMA using the new stream ID.
--- drivers/gpu/host1x/hw/channel_hw.c | 52 +++++++++++++++++++++-- drivers/gpu/host1x/hw/host1x06_hardware.h | 10 +++++ drivers/gpu/host1x/hw/host1x07_hardware.h | 10 +++++ include/linux/host1x.h | 4 ++ 4 files changed, 72 insertions(+), 4 deletions(-)
drivers/gpu/host1x/hw/channel_hw.c +48 -4
@@ -159,6 +159,45 @@ static void host1x_channel_set_streamid(struct host1x_channel *channel)#endif } +static void host1x_channel_program_engine_streamid (struct host1x_job *job) +{ +#if HOST1X_HW >=6 +u32 fence; + +if (!job->context) +return ; + + fence =host1x_syncpt_incr_max (job->syncpt,1 ); + +/* First, increment a syncpoint on OP_DONE condition.. */ + +host1x_cdma_push (&job->channel->cdma, +host1x_opcode_nonincr (HOST1X_UCLASS_INCR_SYNCPT ,1 ), +HOST1X_UCLASS_INCR_SYNCPT_INDX_F (job->syncpt->id) | +HOST1X_UCLASS_INCR_SYNCPT_COND_F (1 )); + +/* Wait for syncpoint to increment */ + +host1x_cdma_push (&job->channel->cdma, +host1x_opcode_setclass (HOST1X_CLASS_HOST1X , +host1x_uclass_wait_syncpt_r (),1 ), +host1x_class_host_wait_syncpt (job->syncpt->id, fence)); + +/* +* Now that we know the engine is idle, return to class and +* change stream ID. +*/ + +host1x_cdma_push (&job->channel->cdma, +host1x_opcode_setclass (job->class,0 ,0 ), +HOST1X_OPCODE_NOP ); + +host1x_cdma_push (&job->channel->cdma, +host1x_opcode_setpayload (job->context->stream_id), +host1x_opcode_setstreamid (job->engine_streamid_offset /4 )); +#endif +} +static int channel_submit (struct host1x_job *job) {struct host1x_channel *ch = job->channel;
@@ -214,18 +253,23 @@ static int channel_submit(struct host1x_job *job)if (sp->base)synchronize_syncpt_base (job); - syncval =host1x_syncpt_incr_max (sp, user_syncpt_incrs); -host1x_hw_syncpt_assign_to_channel (host, sp, ch); - job->syncpt_end = syncval; -/* add a setclass for modules that require it */ if (job->class)host1x_cdma_push (&ch->cdma,host1x_opcode_setclass (job->class,0 ,0 ),HOST1X_OPCODE_NOP ); +/* +* Ensure engine DMA is idle and set new stream ID. May increment +* syncpt max. +*/ +host1x_channel_program_engine_streamid (job); + + syncval =host1x_syncpt_incr_max (sp, user_syncpt_incrs); + job->syncpt_end = syncval; +submit_gathers (job, syncval - user_syncpt_incrs);/* end CDMA submit & stash pinned hMems into sync queue */
drivers/gpu/host1x/hw/host1x06_hardware.h +10 -0
@@ -127,6 +127,16 @@ static inline u32 host1x_opcode_gather_incr(unsigned offset, unsigned count)return (6 <<28 ) | (offset <<16 ) |BIT (15 ) |BIT (14 ) | count; } +static inline u32 host1x_opcode_setstreamid (unsigned streamid) +{ +return (7 <<28 ) | streamid; +} + +static inline u32 host1x_opcode_setpayload (unsigned payload) +{ +return (9 <<28 ) | payload; +} +static inline u32 host1x_opcode_gather_wide (unsigned count) {return (12 <<28 ) | count;
drivers/gpu/host1x/hw/host1x07_hardware.h +10 -0
@@ -127,6 +127,16 @@ static inline u32 host1x_opcode_gather_incr(unsigned offset, unsigned count)return (6 <<28 ) | (offset <<16 ) |BIT (15 ) |BIT (14 ) | count; } +static inline u32 host1x_opcode_setstreamid (unsigned streamid) +{ +return (7 <<28 ) | streamid; +} + +static inline u32 host1x_opcode_setpayload (unsigned payload) +{ +return (9 <<28 ) | payload; +} +static inline u32 host1x_opcode_gather_wide (unsigned count) {return (12 <<28 ) | count;
include/linux/host1x.h +4 -0
@@ -277,6 +277,10 @@ struct host1x_job {/* Whether host1x-side firewall should be ran for this job or not */ bool enable_firewall; + +/* Options for configuring engine data stream ID */ +struct host1x_context *context; +u32 engine_streamid_offset; }; struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 4/8 iommu/arm-smmu: Attach to host1x context device bus
In reply to Mikko PerttunenSet itself as the IOMMU for the host1x context device bus, containing "dummy" devices used for Host1x context isolation.
--- drivers/iommu/arm/arm-smmu/arm-smmu.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/iommu/arm/arm-smmu/arm-smmu.c +13 -0
@@ -39,6 +39,7 @@#include <linux/amba/bus.h> #include <linux/fsl/mc.h> +#include <linux/host1x_context_bus.h> #include "arm-smmu.h"
@@ -2051,8 +2052,20 @@ static int arm_smmu_bus_init(struct iommu_ops *ops) goto err_reset_pci_ops; }#endif +#ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS +if (!iommu_present (&host1x_context_device_bus_type)) { + err =bus_set_iommu (&host1x_context_device_bus_type, ops); +if (err) + goto err_reset_fsl_mc_ops; + } +#endif +return 0 ; +err_reset_fsl_mc_ops:__maybe_unused ; +#ifdef CONFIG_FSL_MC_BUS +bus_set_iommu (&fsl_mc_bus_type,NULL ); +#endif err_reset_pci_ops:__maybe_unused ;#ifdef CONFIG_PCI bus_set_iommu (&pci_bus_type,NULL );
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 5/8 arm64: tegra: Add Host1x context stream IDs on Tegra186+
In reply to Mikko PerttunenAdd Host1x context stream IDs on systems that support Host1x context isolation. Host1x and attached engines can use these stream IDs to allow isolation between memory used by different processes.
The specified stream IDs must match those configured by the hypervisor, if one is present.
---
v2:
* Added context devices on T194.
* Use iommu-map instead of custom property.
--- arch/arm64/boot/dts/nvidia/tegra186.dtsi | 12 ++++++++++++ arch/arm64/boot/dts/nvidia/tegra194.dtsi | 12 ++++++++++++ 2 files changed, 24 insertions(+)
arch/arm64/boot/dts/nvidia/tegra186.dtsi +12 -0
@@ -1270,6 +1270,18 @@ host1x@13e00000 { iommus = <&smmu TEGRA186_SID_HOST1X>; + memory-contexts { + iommu-map = < + 0 &smmu TEGRA186_SID_HOST1X_CTX0 1 + 1 &smmu TEGRA186_SID_HOST1X_CTX1 1 + 2 &smmu TEGRA186_SID_HOST1X_CTX2 1 + 3 &smmu TEGRA186_SID_HOST1X_CTX3 1 + 4 &smmu TEGRA186_SID_HOST1X_CTX4 1 + 5 &smmu TEGRA186_SID_HOST1X_CTX5 1 + 6 &smmu TEGRA186_SID_HOST1X_CTX6 1 + 7 &smmu TEGRA186_SID_HOST1X_CTX7 1>; + }; + dpaux1: dpaux@15040000 { compatible = "nvidia,tegra186-dpaux"; reg = <0x15040000 0x10000>;
arch/arm64/boot/dts/nvidia/tegra194.dtsi +12 -0
@@ -1412,6 +1412,18 @@ host1x@13e00000 { interconnect-names = "dma-mem"; iommus = <&smmu TEGRA194_SID_HOST1X>; + memory-contexts { + iommu-map = < + 0 &smmu TEGRA194_SID_HOST1X_CTX0 1 + 1 &smmu TEGRA194_SID_HOST1X_CTX1 1 + 2 &smmu TEGRA194_SID_HOST1X_CTX2 1 + 3 &smmu TEGRA194_SID_HOST1X_CTX3 1 + 4 &smmu TEGRA194_SID_HOST1X_CTX4 1 + 5 &smmu TEGRA194_SID_HOST1X_CTX5 1 + 6 &smmu TEGRA194_SID_HOST1X_CTX6 1 + 7 &smmu TEGRA194_SID_HOST1X_CTX7 1>; + }; + nvdec@15140000 { compatible = "nvidia,tegra194-nvdec"; reg = <0x15140000 0x00040000>;
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 6/8 drm/tegra: falcon: Set DMACTX field on DMA transactions
In reply to Mikko PerttunenThe DMACTX field determines which context, as specified in the TRANSCFG register, is used. While during boot it doesn't matter which is used, later on it matters and this value is reused by the firmware.
--- drivers/gpu/drm/tegra/falcon.c | 8 ++++++++ drivers/gpu/drm/tegra/falcon.h | 1 + 2 files changed, 9 insertions(+)
drivers/gpu/drm/tegra/falcon.c +8 -0
@@ -48,6 +48,14 @@ static int falcon_copy_chunk(struct falcon *falcon,if (target ==FALCON_MEMORY_IMEM ) cmd |=FALCON_DMATRFCMD_IMEM ; +/* +* Use second DMA context (i.e. the one for firmware). Strictly +* speaking, at this point both DMA contexts point to the firmware +* stream ID, but this register's value will be reused by the firmware +* for later DMA transactions, so we need to use the correct value. +*/ + cmd |=FALCON_DMATRFCMD_DMACTX (1 ); +falcon_writel (falcon, offset,FALCON_DMATRFMOFFS );falcon_writel (falcon, base,FALCON_DMATRFFBOFFS );falcon_writel (falcon, cmd,FALCON_DMATRFCMD );
drivers/gpu/drm/tegra/falcon.h +1 -0
@@ -50,6 +50,7 @@#define FALCON_DMATRFCMD_IDLE (1 << 1)#define FALCON_DMATRFCMD_IMEM (1 << 4)#define FALCON_DMATRFCMD_SIZE_256B (6 << 8) +#define FALCON_DMATRFCMD_DMACTX (v) (((v) & 0x7) << 12)#define FALCON_DMATRFFBOFFS 0x0000111c
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 7/8 drm/tegra: vic: Implement get_streamid_offset
In reply to Mikko PerttunenImplement the get_streamid_offset required for supporting context isolation. Since old firmware cannot support context isolation without hacks that we don't want to implement, check the firmware binary to see if context isolation should be enabled.
--- drivers/gpu/drm/tegra/vic.c | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
drivers/gpu/drm/tegra/vic.c +38 -0
@@ -37,6 +37,8 @@ struct vic {struct clk *clk;struct reset_control *rst; +bool can_use_context; +/* Platform configuration */ const struct vic_config *config; };
@@ -216,6 +218,7 @@ static int vic_load_firmware(struct vic *vic) {struct host1x_client *client = &vic->client.base;struct tegra_drm *tegra = vic->client.drm; +u32 fce_bin_data_offset;dma_addr_t iova;size_t size;void *virt;
@@ -264,6 +267,25 @@ static int vic_load_firmware(struct vic *vic) vic->falcon.firmware.phys = phys; } +/* +* Check if firmware is new enough to not require mapping firmware +* to data buffer domains. +*/ + fce_bin_data_offset = *(u32 *)(virt +VIC_UCODE_FCE_DATA_OFFSET ); + +if (!vic->config->supports_sid) { + vic->can_use_context = false; + }else if (fce_bin_data_offset !=0x0 && fce_bin_data_offset !=0xa5a5a5a5 ) { +/* +* Firmware will access FCE through STREAMID0, so context +* isolation cannot be used. +*/ + vic->can_use_context = false; +dev_warn_once (vic->dev,"context isolation disabled due to old firmware\n" ); + }else { + vic->can_use_context = true; + } +return 0 ; cleanup:
@@ -353,10 +375,26 @@ static void vic_close_channel(struct tegra_drm_context *context)pm_runtime_put (vic->dev); } +static int vic_get_streamid_offset (struct tegra_drm_client *client) +{ +struct vic *vic =to_vic (client); +int err; + + err =vic_load_firmware (vic); +if (err <0 ) +return err; + +if (vic->can_use_context) +return 0x30 ; +else +return -ENOTSUPP ; +} +static const struct tegra_drm_client_ops vic_ops = { .open_channel = vic_open_channel, .close_channel = vic_close_channel, .submit = tegra_drm_submit, + .get_streamid_offset = vic_get_streamid_offset, };#define NVIDIA_TEGRA_124_VIC_FIRMWARE "nvidia/tegra124/vic03_ucode.bin"
Signature · 1 line
2.32.0
Mikko Perttunen
maintainerPATCH v2 8/8 drm/tegra: Support context isolation
In reply to Mikko PerttunenFor engines that support context isolation, allocate a context when opening a channel, and set up stream ID offset and context fields when submitting a job.
--- drivers/gpu/drm/tegra/drm.h | 2 ++ drivers/gpu/drm/tegra/submit.c | 13 +++++++++++++ drivers/gpu/drm/tegra/uapi.c | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-)
drivers/gpu/drm/tegra/drm.h +2 -0
@@ -80,6 +80,7 @@ struct tegra_drm_context {/* Only used by new UAPI. */ struct xarray mappings; +struct host1x_context *memory_context; };struct tegra_drm_client_ops {
@@ -91,6 +92,7 @@ struct tegra_drm_client_ops {int (*submit)(struct tegra_drm_context *context,struct drm_tegra_submit *args,struct drm_device *drm,struct drm_file *file); +int (*get_streamid_offset)(struct tegra_drm_client *client); }; int tegra_drm_submit(struct tegra_drm_context *context,
drivers/gpu/drm/tegra/submit.c +13 -0
@@ -469,6 +469,9 @@ static void release_job(struct host1x_job *job)struct tegra_drm_submit_data *job_data = job->user_data;u32 i; +if (job->context) +host1x_context_put (job->context); +for (i =0 ; i < job_data->num_used_mappings; i++)tegra_drm_mapping_put (job_data->used_mappings[i].mapping);
@@ -572,6 +575,16 @@ int tegra_drm_ioctl_channel_submit(struct drm_device *drm, void *data, job->release = release_job; job->timeout =10000 ; +if (context->memory_context && context->client->ops->get_streamid_offset) { +int offset = context->client->ops->get_streamid_offset (context->client); + +if (offset >=0 ) { + job->context = context->memory_context; + job->engine_streamid_offset = offset; +host1x_context_get (job->context); + } + } +/* * job_data is now part of job reference counting, so don't release * it from here.
drivers/gpu/drm/tegra/uapi.c +32 -2
@@ -37,6 +37,9 @@ static void tegra_drm_channel_context_close(struct tegra_drm_context *context)struct tegra_drm_mapping *mapping;unsigned long id; +if (context->memory_context) +host1x_context_put (context->memory_context); +xa_for_each (&context->mappings, id, mapping)tegra_drm_mapping_put (mapping);
@@ -76,6 +79,7 @@ static struct tegra_drm_client *tegra_drm_find_client(struct tegra_drm *tegra, uint tegra_drm_ioctl_channel_open (struct drm_device *drm,void *data,struct drm_file *file) { +struct host1x *host =tegra_drm_to_host1x (drm->dev_private);struct tegra_drm_file *fpriv = file->driver_priv;struct tegra_drm *tegra = drm->dev_private;struct drm_tegra_channel_open *args = data;
@@ -106,10 +110,29 @@ int tegra_drm_ioctl_channel_open(struct drm_device *drm, void *data, struct drm_ } } +/* Only allocate context if the engine supports context isolation. */ +if (client->ops->get_streamid_offset && + client->ops->get_streamid_offset (client) >=0 ) { + context->memory_context = +host1x_context_alloc (host,get_task_pid (current,PIDTYPE_TGID )); +if (IS_ERR (context->memory_context)) { +if (PTR_ERR (context->memory_context) != -EOPNOTSUPP ) { + err =PTR_ERR (context->memory_context); + goto put_channel; + }else { +/* +* OK, HW does not support contexts or contexts +* are disabled. +*/ + context->memory_context =NULL ; + } + } + } + err =xa_alloc (&fpriv->contexts, &args->context, context,XA_LIMIT (1 ,U32_MAX ),GFP_KERNEL );if (err <0 ) - goto put_channel; + goto put_memctx; context->client = client;xa_init_flags (&context->mappings,XA_FLAGS_ALLOC1 );
@@ -122,6 +145,9 @@ int tegra_drm_ioctl_channel_open(struct drm_device *drm, void *data, struct drm_return 0 ; +put_memctx: +if (context->memory_context) +host1x_context_put (context->memory_context); put_channel:host1x_channel_put (context->channel); free:
@@ -180,7 +206,11 @@ int tegra_drm_ioctl_channel_map(struct drm_device *drm, void *data, struct drm_fkref_init (&mapping->ref); - mapping->dev = context->client->base.dev; +if (context->memory_context) + mapping->dev = &context->memory_context->dev; +else + mapping->dev = context->client->base.dev; + mapping->bo =tegra_gem_lookup (file, args->handle);if (!mapping->bo) { err = -EINVAL ;
Signature · 1 line
2.32.0
Quoted 74 lines · Mikko Perttunen
Hi all,
***
New in v2:Added support for Tegra194
Use standard iommu-map property instead of custom mechanism
***this series adds support for Host1x 'context isolation'. Since when programming engines through Host1x, userspace can program in any addresses it wants, we need some way to isolate the engines' memory spaces. Traditionally this has either been done imperfectly with a single shared IOMMU domain, or by copying and verifying the programming command stream at submit time (Host1x firewall).
Since Tegra186 there is a privileged (only usable by kernel) Host1x opcode that allows setting the stream ID sent by the engine to the SMMU. So, by allocating a number of context banks and stream IDs for this purpose, and using this opcode at the beginning of each job, we can implement isolation. Due to the limited number of context banks only each process gets its own context, and not each channel.
This feature also allows sharing engines among multiple VMs when used with Host1x's hardware virtualization support - up to 8 VMs can be configured with a subset of allowed stream IDs, enforced at hardware level.
To implement this, this series adds a new host1x context bus, which will contain the 'struct device's corresponding to each context bank / stream ID, changes to device tree and SMMU code to allow registering the devices and using the bus, as well as the Host1x stream ID programming code and support in TegraDRM.
Device tree bindings are not updated yet pending consensus that the proposed changes make sense.
Thanks,
MikkoMikko Perttunen (8):
gpu: host1x: Add context bus
gpu: host1x: Add context device management code
gpu: host1x: Program context stream ID on submission iommu/arm-smmu: Attach to host1x context device bus
arm64: tegra: Add Host1x context stream IDs on Tegra186+ drm/tegra: falcon: Set DMACTX field on DMA transactions drm/tegra: vic: Implementget_streamid_offsetdrm/tegra: Support context isolationarch/arm64/boot/dts/nvidia/tegra186.dtsi | 12 ++ arch/arm64/boot/dts/nvidia/tegra194.dtsi | 12 ++ drivers/gpu/Makefile | 3 +- drivers/gpu/drm/tegra/drm.h | 2 + drivers/gpu/drm/tegra/falcon.c | 8 + drivers/gpu/drm/tegra/falcon.h | 1 + drivers/gpu/drm/tegra/submit.c | 13 ++ drivers/gpu/drm/tegra/uapi.c | 34 ++++- drivers/gpu/drm/tegra/vic.c | 38 +++++ drivers/gpu/host1x/Kconfig | 5 + drivers/gpu/host1x/Makefile | 2 + drivers/gpu/host1x/context.c | 174 ++++++++++++++++++++++ drivers/gpu/host1x/context.h | 27 ++++ drivers/gpu/host1x/context_bus.c | 31 ++++ drivers/gpu/host1x/dev.c | 12 +- drivers/gpu/host1x/dev.h | 2 + drivers/gpu/host1x/hw/channel_hw.c | 52 ++++++- drivers/gpu/host1x/hw/host1x06_hardware.h | 10 ++ drivers/gpu/host1x/hw/host1x07_hardware.h | 10 ++ drivers/iommu/arm/arm-smmu/arm-smmu.c | 13 ++ include/linux/host1x.h | 21 +++ include/linux/host1x_context_bus.h | 15 ++ 22 files changed, 488 insertions(+), 9 deletions(-)
Quoted 5 lines
create mode 100644
drivers/gpu/host1x/context.ccreate mode 100644drivers/gpu/host1x/context.hcreate mode 100644drivers/gpu/host1x/context_bus.ccreate mode 100644include/linux/host1x_context_bus.h
IOMMU/DT folks, any thoughts about this approach? The patches that are of interest outside of Host1x/TegraDRM specifics are patches 1, 2, 4, and 5.
Thanks,
Mikko
Jon Hunter
maintainerWill, Joerg, Rob,
Quoted 81 lines · Mikko Perttunen
Quoted 73 lines · Mikko Perttunen
Hi all,
***
New in v2:Added support for Tegra194
Use standard iommu-map property instead of custom mechanism
***this series adds support for Host1x 'context isolation'. Since when programming engines through Host1x, userspace can program in any addresses it wants, we need some way to isolate the engines' memory spaces. Traditionally this has either been done imperfectly with a single shared IOMMU domain, or by copying and verifying the programming command stream at submit time (Host1x firewall).
Since Tegra186 there is a privileged (only usable by kernel) Host1x opcode that allows setting the stream ID sent by the engine to the SMMU. So, by allocating a number of context banks and stream IDs for this purpose, and using this opcode at the beginning of each job, we can implement isolation. Due to the limited number of context banks only each process gets its own context, and not each channel.
This feature also allows sharing engines among multiple VMs when used with Host1x's hardware virtualization support - up to 8 VMs can be configured with a subset of allowed stream IDs, enforced at hardware level.
To implement this, this series adds a new host1x context bus, which will contain the 'struct device's corresponding to each context bank / stream ID, changes to device tree and SMMU code to allow registering the devices and using the bus, as well as the Host1x stream ID programming code and support in TegraDRM.
Device tree bindings are not updated yet pending consensus that the proposed changes make sense.
Thanks,
MikkoMikko Perttunen (8):
gpu: host1x: Add context bus
gpu: host1x: Add context device management code
gpu: host1x: Program context stream ID on submission iommu/arm-smmu: Attach to host1x context device bus
arm64: tegra: Add Host1x context stream IDs on Tegra186+ drm/tegra: falcon: Set DMACTX field on DMA transactions drm/tegra: vic: Implementget_streamid_offsetdrm/tegra: Support context isolationarch/arm64/boot/dts/nvidia/tegra186.dtsi | 12 ++ arch/arm64/boot/dts/nvidia/tegra194.dtsi | 12 ++ drivers/gpu/Makefile | 3 +- drivers/gpu/drm/tegra/drm.h | 2 +
drivers/gpu/drm/tegra/falcon.c | 8 + drivers/gpu/drm/tegra/falcon.h | 1 + drivers/gpu/drm/tegra/submit.c | 13 ++drivers/gpu/drm/tegra/uapi.c | 34 ++++- drivers/gpu/drm/tegra/vic.c | 38 +++++ drivers/gpu/host1x/Kconfig | 5 + drivers/gpu/host1x/Makefile | 2 + drivers/gpu/host1x/context.c | 174 ++++++++++++++++++++++ drivers/gpu/host1x/context.h | 27 ++++ drivers/gpu/host1x/context_bus.c | 31 ++++ drivers/gpu/host1x/dev.c | 12 +- drivers/gpu/host1x/dev.h | 2 + drivers/gpu/host1x/hw/channel_hw.c | 52 ++++++-
drivers/gpu/host1x/hw/host1x06_hardware.h| 10 ++drivers/gpu/host1x/hw/host1x07_hardware.h| 10 ++ drivers/iommu/arm/arm-smmu/arm-smmu.c | 13 ++ include/linux/host1x.h | 21 +++ include/linux/host1x_context_bus.h | 15 ++Quoted 6 lines
22 files changed, 488 insertions(+), 9 deletions(-)create mode 100644
drivers/gpu/host1x/context.ccreate mode 100644drivers/gpu/host1x/context.hcreate mode 100644drivers/gpu/host1x/context_bus.ccreate mode 100644include/linux/host1x_context_bus.h
Quoted 1 line
IOMMU/DT folks, any thoughts about this approach? The patches that are of interest outside of Host1x/TegraDRM specifics are patches 1, 2, 4, and 5.
Any feedback on this?
Jon
Signature · 1 line
nvpublic
Jon Hunter
maintainerHi all,
Still no response on this :-(
Quoted 91 lines · Jon Hunter
Will, Joerg, Rob,
Quoted 81 lines · Mikko Perttunen
Quoted 73 lines · Mikko Perttunen
Hi all,
***
New in v2:Added support for Tegra194
Use standard iommu-map property instead of custom mechanism
***this series adds support for Host1x 'context isolation'. Since when programming engines through Host1x, userspace can program in any addresses it wants, we need some way to isolate the engines' memory spaces. Traditionally this has either been done imperfectly with a single shared IOMMU domain, or by copying and verifying the programming command stream at submit time (Host1x firewall).
Since Tegra186 there is a privileged (only usable by kernel) Host1x opcode that allows setting the stream ID sent by the engine to the SMMU. So, by allocating a number of context banks and stream IDs for this purpose, and using this opcode at the beginning of each job, we can implement isolation. Due to the limited number of context banks only each process gets its own context, and not each channel.
This feature also allows sharing engines among multiple VMs when used with Host1x's hardware virtualization support - up to 8 VMs can be configured with a subset of allowed stream IDs, enforced at hardware level.
To implement this, this series adds a new host1x context bus, which will contain the 'struct device's corresponding to each context bank / stream ID, changes to device tree and SMMU code to allow registering the devices and using the bus, as well as the Host1x stream ID programming code and support in TegraDRM.
Device tree bindings are not updated yet pending consensus that the proposed changes make sense.
Thanks,
MikkoMikko Perttunen (8):
gpu: host1x: Add context bus
gpu: host1x: Add context device management code
gpu: host1x: Program context stream ID on submission iommu/arm-smmu: Attach to host1x context device bus
arm64: tegra: Add Host1x context stream IDs on Tegra186+ drm/tegra: falcon: Set DMACTX field on DMA transactions drm/tegra: vic: Implementget_streamid_offsetdrm/tegra: Support context isolationarch/arm64/boot/dts/nvidia/tegra186.dtsi | 12 ++ arch/arm64/boot/dts/nvidia/tegra194.dtsi | 12 ++ drivers/gpu/Makefile | 3 +- drivers/gpu/drm/tegra/drm.h | 2 +
drivers/gpu/drm/tegra/falcon.c | 8 + drivers/gpu/drm/tegra/falcon.h | 1 + drivers/gpu/drm/tegra/submit.c | 13 ++drivers/gpu/drm/tegra/uapi.c | 34 ++++- drivers/gpu/drm/tegra/vic.c | 38 +++++ drivers/gpu/host1x/Kconfig | 5 + drivers/gpu/host1x/Makefile | 2 + drivers/gpu/host1x/context.c | 174 ++++++++++++++++++++++ drivers/gpu/host1x/context.h | 27 ++++ drivers/gpu/host1x/context_bus.c | 31 ++++ drivers/gpu/host1x/dev.c | 12 +- drivers/gpu/host1x/dev.h | 2 + drivers/gpu/host1x/hw/channel_hw.c | 52 ++++++-
drivers/gpu/host1x/hw/host1x06_hardware.h| 10 ++drivers/gpu/host1x/hw/host1x07_hardware.h| 10 ++ drivers/iommu/arm/arm-smmu/arm-smmu.c | 13 ++ include/linux/host1x.h | 21 +++ include/linux/host1x_context_bus.h | 15 ++Quoted 6 lines
22 files changed, 488 insertions(+), 9 deletions(-)create mode 100644
drivers/gpu/host1x/context.ccreate mode 100644drivers/gpu/host1x/context.hcreate mode 100644drivers/gpu/host1x/context_bus.ccreate mode 100644include/linux/host1x_context_bus.hQuoted 1 line
IOMMU/DT folks, any thoughts about this approach? The patches that are of interest outside of Host1x/TegraDRM specifics are patches 1, 2, 4, and 5.
Any feedback on this?
Jon
Signature · 1 line
nvpublic
14.12.2021 11:05, Jon Hunter пишет:
Quoted 3 lines
Hi all,
Still no response on this :-(
I see only two patches on Tegra ML and others on DRI ML. Might be good to start with re-sending this whole series and CCing MLs properly.
Quoted 8 lines · Dmitry Osipenko
14.12.2021 11:05, Jon Hunter пишет:
Quoted 3 lines
Hi all,
Still no response on this :-(
I see only two patches on Tegra ML and others on DRI ML. Might be good to start with re-sending this whole series and CCing MLs properly.
All patches should have been sent to the same set of addresses. At least LWN's archive seems to agree..
Mikko
14.12.2021 17:53, Mikko Perttunen пишет:
Quoted 12 lines · Dmitry Osipenko
Quoted 8 lines
14.12.2021 11:05, Jon Hunter пишет:
Quoted 3 lines
Hi all,
Still no response on this :-(
I see only two patches on Tegra ML and others on DRI ML. Might be good to start with re-sending this whole series and CCing MLs properly.
All patches should have been sent to the same set of addresses. At least LWN's archive seems to agree..
Indeed, I see that Tegra ML was CCed and I see all patches on Tegra patchwork, but I don't see them all on lore and gmane.
Robin Murphy
reviewerQuoted 81 lines · Mikko Perttunen
Quoted 73 lines · Mikko Perttunen
Hi all,
***
New in v2:Added support for Tegra194
Use standard iommu-map property instead of custom mechanism
***this series adds support for Host1x 'context isolation'. Since when programming engines through Host1x, userspace can program in any addresses it wants, we need some way to isolate the engines' memory spaces. Traditionally this has either been done imperfectly with a single shared IOMMU domain, or by copying and verifying the programming command stream at submit time (Host1x firewall).
Since Tegra186 there is a privileged (only usable by kernel) Host1x opcode that allows setting the stream ID sent by the engine to the SMMU. So, by allocating a number of context banks and stream IDs for this purpose, and using this opcode at the beginning of each job, we can implement isolation. Due to the limited number of context banks only each process gets its own context, and not each channel.
This feature also allows sharing engines among multiple VMs when used with Host1x's hardware virtualization support - up to 8 VMs can be configured with a subset of allowed stream IDs, enforced at hardware level.
To implement this, this series adds a new host1x context bus, which will contain the 'struct device's corresponding to each context bank / stream ID, changes to device tree and SMMU code to allow registering the devices and using the bus, as well as the Host1x stream ID programming code and support in TegraDRM.
Device tree bindings are not updated yet pending consensus that the proposed changes make sense.
Thanks,
MikkoMikko Perttunen (8):
gpu: host1x: Add context bus
gpu: host1x: Add context device management code
gpu: host1x: Program context stream ID on submission iommu/arm-smmu: Attach to host1x context device bus
arm64: tegra: Add Host1x context stream IDs on Tegra186+ drm/tegra: falcon: Set DMACTX field on DMA transactions drm/tegra: vic: Implementget_streamid_offsetdrm/tegra: Support context isolationarch/arm64/boot/dts/nvidia/tegra186.dtsi | 12 ++ arch/arm64/boot/dts/nvidia/tegra194.dtsi | 12 ++ drivers/gpu/Makefile | 3 +- drivers/gpu/drm/tegra/drm.h | 2 +
drivers/gpu/drm/tegra/falcon.c | 8 + drivers/gpu/drm/tegra/falcon.h | 1 + drivers/gpu/drm/tegra/submit.c | 13 ++drivers/gpu/drm/tegra/uapi.c | 34 ++++- drivers/gpu/drm/tegra/vic.c | 38 +++++ drivers/gpu/host1x/Kconfig | 5 + drivers/gpu/host1x/Makefile | 2 + drivers/gpu/host1x/context.c | 174 ++++++++++++++++++++++ drivers/gpu/host1x/context.h | 27 ++++ drivers/gpu/host1x/context_bus.c | 31 ++++ drivers/gpu/host1x/dev.c | 12 +- drivers/gpu/host1x/dev.h | 2 + drivers/gpu/host1x/hw/channel_hw.c | 52 ++++++-
drivers/gpu/host1x/hw/host1x06_hardware.h| 10 ++drivers/gpu/host1x/hw/host1x07_hardware.h| 10 ++ drivers/iommu/arm/arm-smmu/arm-smmu.c | 13 ++ include/linux/host1x.h | 21 +++ include/linux/host1x_context_bus.h | 15 ++Quoted 6 lines
22 files changed, 488 insertions(+), 9 deletions(-)create mode 100644
drivers/gpu/host1x/context.ccreate mode 100644drivers/gpu/host1x/context.hcreate mode 100644drivers/gpu/host1x/context_bus.ccreate mode 100644include/linux/host1x_context_bus.h
Quoted 1 line
IOMMU/DT folks, any thoughts about this approach? The patches that are of interest outside of Host1x/TegraDRM specifics are patches 1, 2, 4, and 5.
FWIW it looks fairly innocuous to me. I don't understand host1x - neither hardware nor driver abstractions - well enough to meaningfully review it all (e.g. maybe it's deliberate that the bus .dma_configure method isn't used?), but the SMMU patch seems fine given the Kconfig solution to avoid module linkage problems.
Cheers,
Robin.
Jon Hunter
maintainerOn 14/12/2021 15:38, Robin Murphy wrote:
...
Quoted 3 lines
Quoted 1 line
IOMMU/DT folks, any thoughts about this approach? The patches that are of interest outside of Host1x/TegraDRM specifics are patches 1, 2, 4, and 5.
FWIW it looks fairly innocuous to me. I don't understand host1x - neither hardware nor driver abstractions - well enough to meaningfully review it all (e.g. maybe it's deliberate that the bus .dma_configure method isn't used?), but the SMMU patch seems fine given the
Kconfigsolution to avoid module linkage problems.
Thanks Robin! Will, Joerg, is OK with you?
Mikko, I believe we are missing a dt-binding change to document the 'memory-contexts' node which I assume you will add if everyone is OK with this?
Cheers
Jon
Signature · 1 line
nvpublic