2019-05-28 10:10:04 -07:00
// SPDX-License-Identifier: GPL-2.0-only
2013-03-22 16:34:03 +02:00
/*
* Tegra host1x Job
*
2016-11-08 19:51:32 +02:00
* Copyright ( c ) 2010 - 2015 , NVIDIA Corporation .
2013-03-22 16:34:03 +02:00
*/
# include <linux/dma-mapping.h>
# include <linux/err.h>
2013-09-24 16:30:32 +02:00
# include <linux/host1x.h>
2020-02-04 14:59:25 +01:00
# include <linux/iommu.h>
2013-03-22 16:34:03 +02:00
# include <linux/kref.h>
# include <linux/module.h>
# include <linux/scatterlist.h>
# include <linux/slab.h>
# include <linux/vmalloc.h>
# include <trace/events/host1x.h>
# include "channel.h"
# include "dev.h"
# include "job.h"
# include "syncpt.h"
2017-06-15 02:18:39 +03:00
# define HOST1X_WAIT_SYNCPT_OFFSET 0x8
2013-03-22 16:34:03 +02:00
struct host1x_job * host1x_job_alloc ( struct host1x_channel * ch ,
2021-06-10 14:04:46 +03:00
u32 num_cmdbufs , u32 num_relocs ,
bool skip_firewall )
2013-03-22 16:34:03 +02:00
{
struct host1x_job * job = NULL ;
2020-06-29 06:18:39 +03:00
unsigned int num_unpins = num_relocs ;
2021-06-10 14:04:46 +03:00
bool enable_firewall ;
2013-03-22 16:34:03 +02:00
u64 total ;
void * mem ;
2021-06-10 14:04:46 +03:00
enable_firewall = IS_ENABLED ( CONFIG_TEGRA_HOST1X_FIREWALL ) & & ! skip_firewall ;
if ( ! enable_firewall )
2020-06-29 06:18:39 +03:00
num_unpins + = num_cmdbufs ;
2013-03-22 16:34:03 +02:00
/* Check that we're not going to overflow */
total = sizeof ( struct host1x_job ) +
2013-08-23 13:18:25 +03:00
( u64 ) num_relocs * sizeof ( struct host1x_reloc ) +
( u64 ) num_unpins * sizeof ( struct host1x_job_unpin_data ) +
2021-06-10 14:04:45 +03:00
( u64 ) num_cmdbufs * sizeof ( struct host1x_job_cmd ) +
2013-08-23 13:18:25 +03:00
( u64 ) num_unpins * sizeof ( dma_addr_t ) +
( u64 ) num_unpins * sizeof ( u32 * ) ;
2013-03-22 16:34:03 +02:00
if ( total > ULONG_MAX )
return NULL ;
mem = job = kzalloc ( total , GFP_KERNEL ) ;
if ( ! job )
return NULL ;
2021-06-10 14:04:46 +03:00
job - > enable_firewall = enable_firewall ;
2013-03-22 16:34:03 +02:00
kref_init ( & job - > ref ) ;
job - > channel = ch ;
/* Redistribute memory to the structs */
mem + = sizeof ( struct host1x_job ) ;
2018-05-16 16:58:44 +02:00
job - > relocs = num_relocs ? mem : NULL ;
2013-03-22 16:34:03 +02:00
mem + = num_relocs * sizeof ( struct host1x_reloc ) ;
job - > unpins = num_unpins ? mem : NULL ;
mem + = num_unpins * sizeof ( struct host1x_job_unpin_data ) ;
2021-06-10 14:04:45 +03:00
job - > cmds = num_cmdbufs ? mem : NULL ;
mem + = num_cmdbufs * sizeof ( struct host1x_job_cmd ) ;
2013-03-22 16:34:03 +02:00
job - > addr_phys = num_unpins ? mem : NULL ;
job - > reloc_addr_phys = job - > addr_phys ;
job - > gather_addr_phys = & job - > addr_phys [ num_relocs ] ;
return job ;
}
2013-11-08 11:41:42 +01:00
EXPORT_SYMBOL ( host1x_job_alloc ) ;
2013-03-22 16:34:03 +02:00
struct host1x_job * host1x_job_get ( struct host1x_job * job )
{
kref_get ( & job - > ref ) ;
return job ;
}
2013-11-08 11:41:42 +01:00
EXPORT_SYMBOL ( host1x_job_get ) ;
2013-03-22 16:34:03 +02:00
static void job_free ( struct kref * ref )
{
struct host1x_job * job = container_of ( ref , struct host1x_job , ref ) ;
2021-06-10 14:04:44 +03:00
if ( job - > release )
job - > release ( job ) ;
2023-01-19 15:09:19 +02:00
if ( job - > fence ) {
/*
* remove_callback is atomic w . r . t . fence signaling , so
* after the call returns , we know that the callback is not
* in execution , and the fence can be safely freed .
*/
dma_fence_remove_callback ( job - > fence , & job - > fence_cb ) ;
dma_fence_put ( job - > fence ) ;
}
gpu: host1x: Add no-recovery mode
Add a new property for jobs to enable or disable recovery i.e.
CPU increments of syncpoints to max value on job timeout. This
allows for a more solid model for hanged jobs, where userspace
doesn't need to guess if a syncpoint increment happened because
the job completed, or because job timeout was triggered.
On job timeout, we stop the channel, NOP all future jobs on the
channel using the same syncpoint, mark the syncpoint as locked
and resume the channel from the next job, if any.
The future jobs are NOPed, since because we don't do the CPU
increments, the value of the syncpoint is no longer synchronized,
and any waiters would become confused if a future job incremented
the syncpoint. The syncpoint is marked locked to ensure that any
future jobs cannot increment the syncpoint either, until the
application has recognized the situation and reallocated the
syncpoint.
Signed-off-by: Mikko Perttunen <mperttunen@nvidia.com>
Signed-off-by: Thierry Reding <treding@nvidia.com>
2021-06-10 14:04:43 +03:00
2021-03-29 16:38:32 +03:00
if ( job - > syncpt )
host1x_syncpt_put ( job - > syncpt ) ;
2013-03-22 16:34:03 +02:00
kfree ( job ) ;
}
void host1x_job_put ( struct host1x_job * job )
{
kref_put ( & job - > ref , job_free ) ;
}
2013-11-08 11:41:42 +01:00
EXPORT_SYMBOL ( host1x_job_put ) ;
2013-03-22 16:34:03 +02:00
void host1x_job_add_gather ( struct host1x_job * job , struct host1x_bo * bo ,
2018-05-16 17:01:43 +02:00
unsigned int words , unsigned int offset )
2013-03-22 16:34:03 +02:00
{
2021-06-10 14:04:45 +03:00
struct host1x_job_gather * gather = & job - > cmds [ job - > num_cmds ] . gather ;
2018-05-16 17:01:43 +02:00
gather - > words = words ;
gather - > bo = bo ;
gather - > offset = offset ;
2013-03-22 16:34:03 +02:00
2021-06-10 14:04:45 +03:00
job - > num_cmds + + ;
2013-03-22 16:34:03 +02:00
}
2013-11-08 11:41:42 +01:00
EXPORT_SYMBOL ( host1x_job_add_gather ) ;
2013-03-22 16:34:03 +02:00
2021-06-10 14:04:45 +03:00
void host1x_job_add_wait ( struct host1x_job * job , u32 id , u32 thresh ,
bool relative , u32 next_class )
{
struct host1x_job_cmd * cmd = & job - > cmds [ job - > num_cmds ] ;
cmd - > is_wait = true ;
cmd - > wait . id = id ;
cmd - > wait . threshold = thresh ;
cmd - > wait . next_class = next_class ;
cmd - > wait . relative = relative ;
job - > num_cmds + + ;
}
EXPORT_SYMBOL ( host1x_job_add_wait ) ;
2016-12-14 13:16:14 +02:00
static unsigned int pin_job ( struct host1x * host , struct host1x_job * job )
2013-03-22 16:34:03 +02:00
{
2021-09-09 15:51:24 +02:00
unsigned long mask = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE ;
2019-10-28 13:37:13 +01:00
struct host1x_client * client = job - > client ;
struct device * dev = client - > dev ;
2020-06-29 06:18:40 +03:00
struct host1x_job_gather * g ;
2013-03-22 16:34:03 +02:00
unsigned int i ;
2016-12-14 13:16:14 +02:00
int err ;
2013-03-22 16:34:03 +02:00
job - > num_unpins = 0 ;
for ( i = 0 ; i < job - > num_relocs ; i + + ) {
2018-05-16 16:58:44 +02:00
struct host1x_reloc * reloc = & job - > relocs [ i ] ;
2021-09-09 15:51:24 +02:00
enum dma_data_direction direction ;
struct host1x_bo_mapping * map ;
struct host1x_bo * bo ;
2013-03-22 16:34:03 +02:00
2014-06-10 10:25:00 +02:00
reloc - > target . bo = host1x_bo_get ( reloc - > target . bo ) ;
2016-12-14 13:16:14 +02:00
if ( ! reloc - > target . bo ) {
err = - EINVAL ;
2013-03-22 16:34:03 +02:00
goto unpin ;
2016-12-14 13:16:14 +02:00
}
2013-03-22 16:34:03 +02:00
2021-09-09 15:51:24 +02:00
bo = reloc - > target . bo ;
2013-03-22 16:34:03 +02:00
2021-09-09 15:51:24 +02:00
switch ( reloc - > flags & mask ) {
case HOST1X_RELOC_READ :
direction = DMA_TO_DEVICE ;
break ;
2019-10-28 13:37:13 +01:00
2021-09-09 15:51:24 +02:00
case HOST1X_RELOC_WRITE :
direction = DMA_FROM_DEVICE ;
break ;
2019-10-28 13:37:13 +01:00
2021-09-09 15:51:24 +02:00
case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE :
direction = DMA_BIDIRECTIONAL ;
break ;
2019-10-28 13:37:13 +01:00
2021-09-09 15:51:24 +02:00
default :
err = - EINVAL ;
goto unpin ;
}
2019-10-28 13:37:13 +01:00
2022-03-24 11:30:25 +01:00
map = host1x_bo_pin ( dev , bo , direction , NULL ) ;
2021-09-09 15:51:24 +02:00
if ( IS_ERR ( map ) ) {
err = PTR_ERR ( map ) ;
goto unpin ;
}
2019-10-28 13:37:13 +01:00
2021-09-09 15:51:24 +02:00
/*
* host1x clients are generally not able to do scatter - gather themselves , so fail
* if the buffer is discontiguous and we fail to map its SG table to a single
* contiguous chunk of I / O virtual memory .
*/
if ( map - > chunks > 1 ) {
err = - EINVAL ;
goto unpin ;
2019-10-28 13:37:13 +01:00
}
2021-09-09 15:51:24 +02:00
job - > addr_phys [ job - > num_unpins ] = map - > phys ;
job - > unpins [ job - > num_unpins ] . map = map ;
2013-03-22 16:34:03 +02:00
job - > num_unpins + + ;
}
2020-06-29 06:18:39 +03:00
/*
* We will copy gathers BO content later , so there is no need to
* hold and pin them .
*/
2021-06-10 14:04:46 +03:00
if ( job - > enable_firewall )
2020-06-29 06:18:39 +03:00
return 0 ;
2021-06-10 14:04:45 +03:00
for ( i = 0 ; i < job - > num_cmds ; i + + ) {
2021-09-09 15:51:24 +02:00
struct host1x_bo_mapping * map ;
2016-12-14 13:16:14 +02:00
size_t gather_size = 0 ;
struct scatterlist * sg ;
unsigned long shift ;
struct iova * alloc ;
unsigned int j ;
2013-03-22 16:34:03 +02:00
2021-06-10 14:04:45 +03:00
if ( job - > cmds [ i ] . is_wait )
continue ;
g = & job - > cmds [ i ] . gather ;
2013-03-22 16:34:03 +02:00
g - > bo = host1x_bo_get ( g - > bo ) ;
2016-12-14 13:16:14 +02:00
if ( ! g - > bo ) {
err = - EINVAL ;
2013-03-22 16:34:03 +02:00
goto unpin ;
2016-12-14 13:16:14 +02:00
}
2013-03-22 16:34:03 +02:00
2022-03-24 11:30:25 +01:00
map = host1x_bo_pin ( host - > dev , g - > bo , DMA_TO_DEVICE , NULL ) ;
2021-09-09 15:51:24 +02:00
if ( IS_ERR ( map ) ) {
err = PTR_ERR ( map ) ;
goto unpin ;
gpu: host1x: Overhaul host1x_bo_{pin,unpin}() API
The host1x_bo_pin() and host1x_bo_unpin() APIs are used to pin and unpin
buffers during host1x job submission. Pinning currently returns the SG
table and the DMA address (an IOVA if an IOMMU is used or a physical
address if no IOMMU is used) of the buffer. The DMA address is only used
for buffers that are relocated, whereas the host1x driver will map
gather buffers into its own IOVA space so that they can be processed by
the CDMA engine.
This approach has a couple of issues. On one hand it's not very useful
to return a DMA address for the buffer if host1x doesn't need it. On the
other hand, returning the SG table of the buffer is suboptimal because a
single SG table cannot be shared for multiple mappings, because the DMA
address is stored within the SG table, and the DMA address may be
different for different devices.
Subsequent patches will move the host1x driver over to the DMA API which
doesn't work with a single shared SG table. Fix this by returning a new
SG table each time a buffer is pinned. This allows the buffer to be
referenced by multiple jobs for different engines.
Change the prototypes of host1x_bo_pin() and host1x_bo_unpin() to take a
struct device *, specifying the device for which the buffer should be
pinned. This is required in order to be able to properly construct the
SG table. While at it, make host1x_bo_pin() return the SG table because
that allows us to return an ERR_PTR()-encoded error code if we need to,
or return NULL to signal that we don't need the SG table to be remapped
and can simply use the DMA address as-is. At the same time, returning
the DMA address is made optional because in the example of command
buffers, host1x doesn't need to know the DMA address since it will have
to create its own mapping anyway.
Signed-off-by: Thierry Reding <treding@nvidia.com>
2019-10-28 13:37:09 +01:00
}
2016-12-14 13:16:14 +02:00
2020-06-29 06:18:39 +03:00
if ( host - > domain ) {
2021-09-09 15:51:24 +02:00
for_each_sgtable_sg ( map - > sgt , sg , j )
2016-12-14 13:16:14 +02:00
gather_size + = sg - > length ;
2021-09-09 15:51:24 +02:00
2016-12-14 13:16:14 +02:00
gather_size = iova_align ( & host - > iova , gather_size ) ;
shift = iova_shift ( & host - > iova ) ;
alloc = alloc_iova ( & host - > iova , gather_size > > shift ,
host - > iova_end > > shift , true ) ;
if ( ! alloc ) {
err = - ENOMEM ;
2020-06-29 06:18:40 +03:00
goto put ;
2016-12-14 13:16:14 +02:00
}
2021-09-09 15:51:24 +02:00
err = iommu_map_sgtable ( host - > domain , iova_dma_addr ( & host - > iova , alloc ) ,
map - > sgt , IOMMU_READ ) ;
2016-12-14 13:16:14 +02:00
if ( err = = 0 ) {
__free_iova ( & host - > iova , alloc ) ;
err = - EINVAL ;
2020-06-29 06:18:40 +03:00
goto put ;
2016-12-14 13:16:14 +02:00
}
2021-09-09 15:51:24 +02:00
map - > phys = iova_dma_addr ( & host - > iova , alloc ) ;
map - > size = gather_size ;
2016-12-14 13:16:14 +02:00
}
2021-09-09 15:51:24 +02:00
job - > addr_phys [ job - > num_unpins ] = map - > phys ;
job - > unpins [ job - > num_unpins ] . map = map ;
2013-03-22 16:34:03 +02:00
job - > num_unpins + + ;
2021-09-09 15:51:24 +02:00
job - > gather_addr_phys [ i ] = map - > phys ;
2013-03-22 16:34:03 +02:00
}
2016-12-14 13:16:14 +02:00
return 0 ;
2013-03-22 16:34:03 +02:00
2020-06-29 06:18:40 +03:00
put :
host1x_bo_put ( g - > bo ) ;
2013-03-22 16:34:03 +02:00
unpin :
host1x_job_unpin ( job ) ;
2016-12-14 13:16:14 +02:00
return err ;
2013-03-22 16:34:03 +02:00
}
2017-06-15 02:18:34 +03:00
static int do_relocs ( struct host1x_job * job , struct host1x_job_gather * g )
2013-03-22 16:34:03 +02:00
{
2019-11-18 11:35:22 +01:00
void * cmdbuf_addr = NULL ;
2017-06-15 02:18:34 +03:00
struct host1x_bo * cmdbuf = g - > bo ;
2018-03-23 13:31:24 +01:00
unsigned int i ;
2013-03-22 16:34:03 +02:00
/* pin & patch the relocs for one gather */
2013-05-29 13:26:05 +03:00
for ( i = 0 ; i < job - > num_relocs ; i + + ) {
2018-05-16 16:58:44 +02:00
struct host1x_reloc * reloc = & job - > relocs [ i ] ;
2013-03-22 16:34:03 +02:00
u32 reloc_addr = ( job - > reloc_addr_phys [ i ] +
2014-06-10 10:25:00 +02:00
reloc - > target . offset ) > > reloc - > shift ;
2013-03-22 16:34:03 +02:00
u32 * target ;
/* skip all other gathers */
2014-06-10 10:25:00 +02:00
if ( cmdbuf ! = reloc - > cmdbuf . bo )
2013-03-22 16:34:03 +02:00
continue ;
2021-06-10 14:04:46 +03:00
if ( job - > enable_firewall ) {
2017-06-15 02:18:34 +03:00
target = ( u32 * ) job - > gather_copy_mapped +
reloc - > cmdbuf . offset / sizeof ( u32 ) +
g - > offset / sizeof ( u32 ) ;
goto patch_reloc ;
}
2019-11-18 11:35:22 +01:00
if ( ! cmdbuf_addr ) {
cmdbuf_addr = host1x_bo_mmap ( cmdbuf ) ;
2013-03-22 16:34:03 +02:00
2019-11-18 11:35:22 +01:00
if ( unlikely ( ! cmdbuf_addr ) ) {
2013-03-22 16:34:03 +02:00
pr_err ( " Could not map cmdbuf for relocation \n " ) ;
return - ENOMEM ;
}
}
2019-11-18 11:35:22 +01:00
target = cmdbuf_addr + reloc - > cmdbuf . offset ;
2017-06-15 02:18:34 +03:00
patch_reloc :
2013-03-22 16:34:03 +02:00
* target = reloc_addr ;
}
2019-11-18 11:35:22 +01:00
if ( cmdbuf_addr )
host1x_bo_munmap ( cmdbuf , cmdbuf_addr ) ;
2013-03-22 16:34:03 +02:00
return 0 ;
}
2013-05-29 13:26:03 +03:00
static bool check_reloc ( struct host1x_reloc * reloc , struct host1x_bo * cmdbuf ,
2013-10-10 10:17:45 +02:00
unsigned int offset )
2013-03-22 16:34:03 +02:00
{
offset * = sizeof ( u32 ) ;
2014-06-10 10:25:00 +02:00
if ( reloc - > cmdbuf . bo ! = cmdbuf | | reloc - > cmdbuf . offset ! = offset )
2013-05-29 13:26:03 +03:00
return false ;
2013-03-22 16:34:03 +02:00
2017-06-15 02:18:35 +03:00
/* relocation shift value validation isn't implemented yet */
if ( reloc - > shift )
return false ;
2013-05-29 13:26:03 +03:00
return true ;
2013-03-22 16:34:03 +02:00
}
struct host1x_firewall {
struct host1x_job * job ;
struct device * dev ;
unsigned int num_relocs ;
struct host1x_reloc * reloc ;
2013-10-10 10:21:58 +02:00
struct host1x_bo * cmdbuf ;
2013-03-22 16:34:03 +02:00
unsigned int offset ;
u32 words ;
u32 class ;
u32 reg ;
u32 mask ;
u32 count ;
} ;
2013-10-10 10:24:04 +02:00
static int check_register ( struct host1x_firewall * fw , unsigned long offset )
{
2017-06-15 02:18:37 +03:00
if ( ! fw - > job - > is_addr_reg )
return 0 ;
2013-10-10 10:24:04 +02:00
if ( fw - > job - > is_addr_reg ( fw - > dev , fw - > class , offset ) ) {
if ( ! fw - > num_relocs )
return - EINVAL ;
if ( ! check_reloc ( fw - > reloc , fw - > cmdbuf , fw - > offset ) )
return - EINVAL ;
fw - > num_relocs - - ;
fw - > reloc + + ;
}
return 0 ;
}
2017-06-15 02:18:37 +03:00
static int check_class ( struct host1x_firewall * fw , u32 class )
{
if ( ! fw - > job - > is_valid_class ) {
if ( fw - > class ! = class )
return - EINVAL ;
} else {
if ( ! fw - > job - > is_valid_class ( fw - > class ) )
return - EINVAL ;
}
return 0 ;
}
2013-03-22 16:34:03 +02:00
static int check_mask ( struct host1x_firewall * fw )
{
u32 mask = fw - > mask ;
u32 reg = fw - > reg ;
2013-10-10 10:24:04 +02:00
int ret ;
2013-03-22 16:34:03 +02:00
while ( mask ) {
if ( fw - > words = = 0 )
return - EINVAL ;
if ( mask & 1 ) {
2013-10-10 10:24:04 +02:00
ret = check_register ( fw , reg ) ;
if ( ret < 0 )
return ret ;
2013-03-22 16:34:03 +02:00
fw - > words - - ;
fw - > offset + + ;
}
mask > > = 1 ;
reg + + ;
}
return 0 ;
}
static int check_incr ( struct host1x_firewall * fw )
{
u32 count = fw - > count ;
u32 reg = fw - > reg ;
2013-10-10 10:24:04 +02:00
int ret ;
2013-03-22 16:34:03 +02:00
2013-05-29 13:26:02 +03:00
while ( count ) {
2013-03-22 16:34:03 +02:00
if ( fw - > words = = 0 )
return - EINVAL ;
2013-10-10 10:24:04 +02:00
ret = check_register ( fw , reg ) ;
if ( ret < 0 )
return ret ;
2013-03-22 16:34:03 +02:00
reg + + ;
fw - > words - - ;
fw - > offset + + ;
count - - ;
}
return 0 ;
}
static int check_nonincr ( struct host1x_firewall * fw )
{
u32 count = fw - > count ;
2013-10-10 10:24:04 +02:00
int ret ;
2013-03-22 16:34:03 +02:00
while ( count ) {
if ( fw - > words = = 0 )
return - EINVAL ;
2013-10-10 10:24:04 +02:00
ret = check_register ( fw , fw - > reg ) ;
if ( ret < 0 )
return ret ;
2013-03-22 16:34:03 +02:00
fw - > words - - ;
fw - > offset + + ;
count - - ;
}
return 0 ;
}
2013-05-29 13:26:04 +03:00
static int validate ( struct host1x_firewall * fw , struct host1x_job_gather * g )
2013-03-22 16:34:03 +02:00
{
2013-05-29 13:26:05 +03:00
u32 * cmdbuf_base = ( u32 * ) fw - > job - > gather_copy_mapped +
( g - > offset / sizeof ( u32 ) ) ;
2017-06-15 02:18:37 +03:00
u32 job_class = fw - > class ;
2013-03-22 16:34:03 +02:00
int err = 0 ;
2013-05-29 13:26:04 +03:00
fw - > words = g - > words ;
2013-10-10 10:21:58 +02:00
fw - > cmdbuf = g - > bo ;
2013-05-29 13:26:04 +03:00
fw - > offset = 0 ;
2013-03-22 16:34:03 +02:00
2013-05-29 13:26:04 +03:00
while ( fw - > words & & ! err ) {
u32 word = cmdbuf_base [ fw - > offset ] ;
2013-03-22 16:34:03 +02:00
u32 opcode = ( word & 0xf0000000 ) > > 28 ;
2013-05-29 13:26:04 +03:00
fw - > mask = 0 ;
fw - > reg = 0 ;
fw - > count = 0 ;
fw - > words - - ;
fw - > offset + + ;
2013-03-22 16:34:03 +02:00
switch ( opcode ) {
case 0 :
2013-05-29 13:26:04 +03:00
fw - > class = word > > 6 & 0x3ff ;
fw - > mask = word & 0x3f ;
fw - > reg = word > > 16 & 0xfff ;
2017-06-15 02:18:37 +03:00
err = check_class ( fw , job_class ) ;
if ( ! err )
err = check_mask ( fw ) ;
2013-03-22 16:34:03 +02:00
if ( err )
goto out ;
break ;
case 1 :
2013-05-29 13:26:04 +03:00
fw - > reg = word > > 16 & 0xfff ;
fw - > count = word & 0xffff ;
err = check_incr ( fw ) ;
2013-03-22 16:34:03 +02:00
if ( err )
goto out ;
break ;
case 2 :
2013-05-29 13:26:04 +03:00
fw - > reg = word > > 16 & 0xfff ;
fw - > count = word & 0xffff ;
err = check_nonincr ( fw ) ;
2013-03-22 16:34:03 +02:00
if ( err )
goto out ;
break ;
case 3 :
2013-05-29 13:26:04 +03:00
fw - > mask = word & 0xffff ;
fw - > reg = word > > 16 & 0xfff ;
err = check_mask ( fw ) ;
2013-03-22 16:34:03 +02:00
if ( err )
goto out ;
break ;
case 4 :
case 14 :
break ;
default :
err = - EINVAL ;
break ;
}
}
out :
return err ;
}
2019-10-28 13:37:12 +01:00
static inline int copy_gathers ( struct device * host , struct host1x_job * job ,
struct device * dev )
2013-03-22 16:34:03 +02:00
{
2013-05-29 13:26:05 +03:00
struct host1x_firewall fw ;
2013-03-22 16:34:03 +02:00
size_t size = 0 ;
size_t offset = 0 ;
2018-03-23 13:31:24 +01:00
unsigned int i ;
2013-03-22 16:34:03 +02:00
2013-05-29 13:26:05 +03:00
fw . job = job ;
fw . dev = dev ;
2018-05-16 16:58:44 +02:00
fw . reloc = job - > relocs ;
2013-05-29 13:26:05 +03:00
fw . num_relocs = job - > num_relocs ;
2017-06-15 02:18:32 +03:00
fw . class = job - > class ;
2013-05-29 13:26:05 +03:00
2021-06-10 14:04:45 +03:00
for ( i = 0 ; i < job - > num_cmds ; i + + ) {
struct host1x_job_gather * g ;
if ( job - > cmds [ i ] . is_wait )
continue ;
g = & job - > cmds [ i ] . gather ;
2016-06-23 11:33:31 +02:00
2013-03-22 16:34:03 +02:00
size + = g - > words * sizeof ( u32 ) ;
}
2017-06-15 02:18:43 +03:00
/*
* Try a non - blocking allocation from a higher priority pools first ,
* as awaiting for the allocation here is a major performance hit .
*/
2019-10-28 13:37:12 +01:00
job - > gather_copy_mapped = dma_alloc_wc ( host , size , & job - > gather_copy ,
2017-06-15 02:18:43 +03:00
GFP_NOWAIT ) ;
/* the higher priority allocation failed, try the generic-blocking */
if ( ! job - > gather_copy_mapped )
2019-10-28 13:37:12 +01:00
job - > gather_copy_mapped = dma_alloc_wc ( host , size ,
2017-06-15 02:18:43 +03:00
& job - > gather_copy ,
GFP_KERNEL ) ;
if ( ! job - > gather_copy_mapped )
2013-08-23 13:19:11 +03:00
return - ENOMEM ;
2013-03-22 16:34:03 +02:00
job - > gather_copy_size = size ;
2021-06-10 14:04:45 +03:00
for ( i = 0 ; i < job - > num_cmds ; i + + ) {
struct host1x_job_gather * g ;
2013-03-22 16:34:03 +02:00
void * gather ;
2021-06-10 14:04:45 +03:00
if ( job - > cmds [ i ] . is_wait )
continue ;
g = & job - > cmds [ i ] . gather ;
2013-05-29 13:26:05 +03:00
/* Copy the gather */
2013-03-22 16:34:03 +02:00
gather = host1x_bo_mmap ( g - > bo ) ;
memcpy ( job - > gather_copy_mapped + offset , gather + g - > offset ,
g - > words * sizeof ( u32 ) ) ;
host1x_bo_munmap ( g - > bo , gather ) ;
2013-05-29 13:26:05 +03:00
/* Store the location in the buffer */
2013-03-22 16:34:03 +02:00
g - > base = job - > gather_copy ;
g - > offset = offset ;
2013-05-29 13:26:05 +03:00
/* Validate the job */
if ( validate ( & fw , g ) )
return - EINVAL ;
2013-03-22 16:34:03 +02:00
offset + = g - > words * sizeof ( u32 ) ;
}
2018-05-05 08:45:47 +02:00
/* No relocs should remain at this point */
if ( fw . num_relocs )
2013-10-04 20:18:33 +00:00
return - EINVAL ;
2013-03-22 16:34:03 +02:00
return 0 ;
}
int host1x_job_pin ( struct host1x_job * job , struct device * dev )
{
int err ;
unsigned int i , j ;
struct host1x * host = dev_get_drvdata ( dev - > parent ) ;
/* pin memory */
2016-12-14 13:16:14 +02:00
err = pin_job ( host , job ) ;
if ( err )
2013-03-22 16:34:03 +02:00
goto out ;
2021-06-10 14:04:46 +03:00
if ( job - > enable_firewall ) {
2019-10-28 13:37:12 +01:00
err = copy_gathers ( host - > dev , job , dev ) ;
2017-06-15 02:18:34 +03:00
if ( err )
goto out ;
}
2013-03-22 16:34:03 +02:00
/* patch gathers */
2021-06-10 14:04:45 +03:00
for ( i = 0 ; i < job - > num_cmds ; i + + ) {
struct host1x_job_gather * g ;
if ( job - > cmds [ i ] . is_wait )
continue ;
g = & job - > cmds [ i ] . gather ;
2013-03-22 16:34:03 +02:00
/* process each gather mem only once */
if ( g - > handled )
continue ;
2017-06-15 02:18:34 +03:00
/* copy_gathers() sets gathers base if firewall is enabled */
2021-06-10 14:04:46 +03:00
if ( ! job - > enable_firewall )
2017-06-15 02:18:34 +03:00
g - > base = job - > gather_addr_phys [ i ] ;
2013-03-22 16:34:03 +02:00
2021-06-10 14:04:45 +03:00
for ( j = i + 1 ; j < job - > num_cmds ; j + + ) {
if ( ! job - > cmds [ j ] . is_wait & &
job - > cmds [ j ] . gather . bo = = g - > bo ) {
job - > cmds [ j ] . gather . handled = true ;
job - > cmds [ j ] . gather . base = g - > base ;
2016-11-08 19:51:32 +02:00
}
}
2013-03-22 16:34:03 +02:00
2017-06-15 02:18:34 +03:00
err = do_relocs ( job , g ) ;
2013-03-22 16:34:03 +02:00
if ( err )
2017-06-15 02:18:34 +03:00
break ;
2013-03-22 16:34:03 +02:00
}
out :
2017-06-15 02:18:33 +03:00
if ( err )
host1x_job_unpin ( job ) ;
2013-03-22 16:34:03 +02:00
wmb ( ) ;
return err ;
}
2013-11-08 11:41:42 +01:00
EXPORT_SYMBOL ( host1x_job_pin ) ;
2013-03-22 16:34:03 +02:00
void host1x_job_unpin ( struct host1x_job * job )
{
2016-12-14 13:16:14 +02:00
struct host1x * host = dev_get_drvdata ( job - > channel - > dev - > parent ) ;
2013-03-22 16:34:03 +02:00
unsigned int i ;
for ( i = 0 ; i < job - > num_unpins ; i + + ) {
2021-09-09 15:51:24 +02:00
struct host1x_bo_mapping * map = job - > unpins [ i ] . map ;
struct host1x_bo * bo = map - > bo ;
2016-12-14 13:16:14 +02:00
2021-09-09 15:51:24 +02:00
if ( ! job - > enable_firewall & & map - > size & & host - > domain ) {
iommu_unmap ( host - > domain , job - > addr_phys [ i ] , map - > size ) ;
free_iova ( & host - > iova , iova_pfn ( & host - > iova , job - > addr_phys [ i ] ) ) ;
}
2019-10-28 13:37:13 +01:00
2021-09-09 15:51:24 +02:00
host1x_bo_unpin ( map ) ;
host1x_bo_put ( bo ) ;
2013-03-22 16:34:03 +02:00
}
2016-06-23 11:35:50 +02:00
2013-03-22 16:34:03 +02:00
job - > num_unpins = 0 ;
if ( job - > gather_copy_size )
2019-10-28 13:37:12 +01:00
dma_free_wc ( host - > dev , job - > gather_copy_size ,
2016-06-23 11:35:50 +02:00
job - > gather_copy_mapped , job - > gather_copy ) ;
2013-03-22 16:34:03 +02:00
}
2013-11-08 11:41:42 +01:00
EXPORT_SYMBOL ( host1x_job_unpin ) ;
2013-03-22 16:34:03 +02:00
/*
* Debug routine used to dump job entries
*/
void host1x_job_dump ( struct device * dev , struct host1x_job * job )
{
2021-03-29 16:38:32 +03:00
dev_dbg ( dev , " SYNCPT_ID %d \n " , job - > syncpt - > id ) ;
2013-03-22 16:34:03 +02:00
dev_dbg ( dev , " SYNCPT_VAL %d \n " , job - > syncpt_end ) ;
dev_dbg ( dev , " FIRST_GET 0x%x \n " , job - > first_get ) ;
dev_dbg ( dev , " TIMEOUT %d \n " , job - > timeout ) ;
dev_dbg ( dev , " NUM_SLOTS %d \n " , job - > num_slots ) ;
dev_dbg ( dev , " NUM_HANDLES %d \n " , job - > num_unpins ) ;
}