1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/drivers/media/usb/stk1160/stk1160.h
Dafna Hirschfeld 68d0c3311e media: stk1160: use dma_alloc_noncontiguous API
Replace the urb buffers allocation to use the noncontiguous API.

This improves performance on ARM platforms where DMA coherent allocations
produce uncached mappings. Note that the noncontiguous API
requires the driver to handle synchronization.

This commit is similar to this one for the uvc driver:

  https://lkml.org/lkml/2021/3/12/1506

Performance tests on rock-pi4 (Arm64) shows about 15x
improvements:

== DMA NONCONTIGUOUS ==
total durations: 20.63678480 sec
urb processing durations: 0.286864889 sec
uS/qty: 286864/2508 avg: 114.379 min: 0.583 max: 155.461 (uS)
FPS: 24.92
lost: 0 done: 500
raw decode speed: 11.603 Gbits/s
bytes 414831228.000
bytes/urb: 165403

== DMA COHERENT ==
total durations: 20.73551767 sec
urb processing durations: 4.541559160 sec
uS/qty: 4541559/2509 avg: 1810.107 min: 0.583 max: 2113.163 (uS)
FPS: 24.90
lost: 0 done: 500
raw decode speed: 730.738 Mbits/s
bytes 414785444.000
bytes/urb: 165319

Performance tests on x86 laptop show no significant
difference:

== DMA NONCONTIGUOUS ==
total durations: 20.220590102 sec
urb processing durations: 0.63021818 sec
uS/qty: 63021/2512 avg: 25.088 min: 0.138 max: 146.750 (uS)
FPS: 24.72
lost: 0 done: 500
raw decode speed: 52.751 Gbits/s
bytes 415421032.000
bytes/urb: 165374

== DMA COHERENT ==
total durations: 20.220475614 sec
urb processing durations: 0.64751972 sec
uS/qty: 64751/2512 avg: 25.777 min: 0.168 max: 132.250 (uS)
FPS: 24.72
lost: 0 done: 500
raw decode speed: 51.927 Gbits/s
bytes 415422794.000
bytes/urb: 165375

[hverkuil: incorporated Ezequiel's suggestions from his review]

Signed-off-by: Dafna Hirschfeld <dafna.hirschfeld@collabora.com>
Reviewed-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
2022-03-07 11:01:22 +01:00

202 lines
4.7 KiB
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* STK1160 driver
*
* Copyright (C) 2012 Ezequiel Garcia
* <elezegarcia--a.t--gmail.com>
*
* Based on Easycap driver by R.M. Thomas
* Copyright (C) 2010 R.M. Thomas
* <rmthomas--a.t--sciolus.org>
*/
#include <linux/i2c.h>
#include <sound/core.h>
#include <sound/ac97_codec.h>
#include <media/videobuf2-v4l2.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#define STK1160_VERSION "0.9.5"
#define STK1160_VERSION_NUM 0x000905
/* Decide on number of packets for each buffer */
#define STK1160_NUM_PACKETS 64
/* Number of buffers for isoc transfers */
#define STK1160_NUM_BUFS 16
#define STK1160_MIN_BUFS 1
/* TODO: This endpoint address should be retrieved */
#define STK1160_EP_VIDEO 0x82
#define STK1160_EP_AUDIO 0x81
/* Max and min video buffers */
#define STK1160_MIN_VIDEO_BUFFERS 8
#define STK1160_MAX_VIDEO_BUFFERS 32
#define STK1160_MIN_PKT_SIZE 3072
#define STK1160_MAX_INPUT 4
#define STK1160_SVIDEO_INPUT 4
#define STK1160_AC97_TIMEOUT 50
#define STK1160_I2C_TIMEOUT 100
/* TODO: Print helpers
* I could use dev_xxx, pr_xxx, v4l2_xxx or printk.
* However, there isn't a solid consensus on which
* new drivers should use.
*
*/
#ifdef DEBUG
#define stk1160_dbg(fmt, args...) \
printk(KERN_DEBUG "stk1160: " fmt, ## args)
#else
#define stk1160_dbg(fmt, args...)
#endif
#define stk1160_info(fmt, args...) \
pr_info("stk1160: " fmt, ## args)
#define stk1160_warn(fmt, args...) \
pr_warn("stk1160: " fmt, ## args)
#define stk1160_err(fmt, args...) \
pr_err("stk1160: " fmt, ## args)
/* Buffer for one video frame */
struct stk1160_buffer {
/* common v4l buffer stuff -- must be first */
struct vb2_v4l2_buffer vb;
struct list_head list;
void *mem;
unsigned int length; /* buffer length */
unsigned int bytesused; /* bytes written */
int odd; /* current oddity */
/*
* Since we interlace two fields per frame,
* this is different from bytesused.
*/
unsigned int pos; /* current pos inside buffer */
};
struct stk1160_urb {
struct urb *urb;
char *transfer_buffer;
struct sg_table *sgt;
struct stk1160 *dev;
dma_addr_t dma;
};
struct stk1160_isoc_ctl {
/* max packet size of isoc transaction */
int max_pkt_size;
/* number of allocated urbs */
int num_bufs;
struct stk1160_urb urb_ctl[STK1160_NUM_BUFS];
/* current buffer */
struct stk1160_buffer *buf;
};
struct stk1160_fmt {
u32 fourcc; /* v4l2 format id */
int depth;
};
struct stk1160 {
struct v4l2_device v4l2_dev;
struct video_device vdev;
struct v4l2_ctrl_handler ctrl_handler;
struct device *dev;
struct usb_device *udev;
/* saa7115 subdev */
struct v4l2_subdev *sd_saa7115;
/* isoc control struct */
struct list_head avail_bufs;
/* video capture */
struct vb2_queue vb_vidq;
/* max packet size of isoc transaction */
int max_pkt_size;
/* array of wMaxPacketSize */
unsigned int *alt_max_pkt_size;
/* alternate */
int alt;
/* Number of alternative settings */
int num_alt;
struct stk1160_isoc_ctl isoc_ctl;
/* frame properties */
int width; /* current frame width */
int height; /* current frame height */
unsigned int ctl_input; /* selected input */
v4l2_std_id norm; /* current norm */
struct stk1160_fmt *fmt; /* selected format */
unsigned int sequence;
/* i2c i/o */
struct i2c_adapter i2c_adap;
struct i2c_client i2c_client;
struct mutex v4l_lock;
struct mutex vb_queue_lock;
spinlock_t buf_lock;
struct file *fh_owner; /* filehandle ownership */
/* EXPERIMENTAL */
struct snd_card *snd_card;
};
struct regval {
u16 reg;
u16 val;
};
/* Provided by stk1160-v4l.c */
int stk1160_vb2_setup(struct stk1160 *dev);
int stk1160_video_register(struct stk1160 *dev);
void stk1160_video_unregister(struct stk1160 *dev);
void stk1160_clear_queue(struct stk1160 *dev, enum vb2_buffer_state vb2_state);
/* Provided by stk1160-video.c */
int stk1160_alloc_isoc(struct stk1160 *dev);
void stk1160_free_isoc(struct stk1160 *dev);
void stk1160_cancel_isoc(struct stk1160 *dev);
void stk1160_uninit_isoc(struct stk1160 *dev);
/* Provided by stk1160-i2c.c */
int stk1160_i2c_register(struct stk1160 *dev);
int stk1160_i2c_unregister(struct stk1160 *dev);
/* Provided by stk1160-core.c */
int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value);
int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value);
int stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg,
char *buf, int len);
int stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg,
char *buf, int len);
void stk1160_select_input(struct stk1160 *dev);
/* Provided by stk1160-ac97.c */
void stk1160_ac97_setup(struct stk1160 *dev);
static inline struct device *stk1160_get_dmadev(struct stk1160 *dev)
{
return bus_to_hcd(dev->udev->bus)->self.sysdev;
}