SPI core refresh
Subsystems (4)
cover SPI core refresh
Greetings,
this message fill be followed by the following four ones:
1) updated SPI core from Dmitry Pervushin/Vitaly Wool
2) Atmel MTD dataflash driver port for this core
3) SPI controller driver for Philips SPI controller
4) dumb EEPROM driver for EEPROM chip on SPI bus
This SPI core features:
* multiple SPI controller support
* multiple devices on the same bus support
* DMA support
* synchronous and asynchronous transfers
* library for asynchronous transfers on the bus using kernel threads
* character device interface
* custom lightweight SPI message allocation mechanism
The SPI core is now closer to the other one, David Brownell's. For instance, spi_driver structure is now the same, and some API functions are mostly quite similar. Even the footprint is now similar (~2k .text on ARM) :)
However, there are some differences I'd like to summarize:
* this core doesn't support hotplug, and David's does (we'll post this support later in a separate patch)
* our core is transparent for DMA transfers (allocation of DMA-safe buffer is triggered by the message flag set by the caller) and David's one implies limitations on tx/rx buffers passed (which can be easily resolved in the controller driver though)
* our core implements thread-based asynchronous message handling (as an option!) which seems to be the best way to go for most cases. i. e. PXA SSP driver from Stephen would be a lot more lightweight and will gain more performance if it used this method instead of multiple tasklets scheduling (as of now)
* our core hides the SPI message internals and David's exposes them to the outer world. This is in way more robust since the only way to allocate the message is through the API
* our core uses uniform message structure for all transfers and "chains" such messages, and David's uses separate spi_transfer array which hold the chain and is attached to the message structure. It's arguable which one is better; at least ours allows to easily change the message inetrnals w/o affecting actual drivers and use versatile message allocation techniques
* our core allows to omit callbacks for sync transfers which is a reasonable thing IMO (if it's a sync transfer, then the caller will just wait, so having a default callback that just waits-for-completion is rational)
* our core provides more straightfoward means (leading to improved readability of the actual drivers) to express the same things, i. e instead of
Quoted 4 lines
message->status = -EIO;
if (message->complete) {
message->complete(message->context);
}one is only to do
Quoted 1 line
spimsg_complete(message, -EIO);
or, instead of
Quoted 2 lines
if (!message->transfers[state->index].cs_change) state->cs_control(PXA2XX_CS_DEASSERT);
one is to do
Quoted 2 lines
if (spimsg_get_flags(cur_msg) & SPI_M_CSKEEP) state->cs_control(PXA2XX_CS_DEASSERT);
* other minor differences... maybe David's gonna add some here.
Vitaly
PATCH 1/4 SPI core refresh: SPI core patch
Documentation/spi.txt | 134 +++++++++ arch/arm/Kconfig | 2 drivers/Kconfig | 2 drivers/Makefile | 1 drivers/spi/Kconfig | 46 +++ drivers/spi/Makefile | 19 + drivers/spi/spi-alloc.c | 101 +++++++ drivers/spi/spi-alloc.h | 54 +++ drivers/spi/spi-core.c | 659 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/spi/spi-core.h | 43 +++ drivers/spi/spi-dev.c | 209 ++++++++++++++ drivers/spi/spi-thread.c | 187 +++++++++++++ drivers/spi/spi-thread.h | 42 ++ include/linux/spi.h | 250 +++++++++++++++++ 14 files changed, 1749 insertions(+)
Documentation/spi.txt new file +134 -0
@@ -0,0 +1,134 @@ +Documentation/spi.txt +======================================================== +Table of contents +1. Introduction -- what is SPI ? +2. Purposes of this code +3. SPI devices stack +3.1 SPI outline +3.2 How the SPI devices gets discovered and probed ? +3.3 DMA and SPI messages +4. SPI functions and structures reference +5. How to contact authors +======================================================== + +1. What is SPI ? +---------------- +SPI stands for "Serial Peripheral Interface", a full-duplex synchronous +serial interface for connecting low-/medium-bandwidth external devices +using four wires. SPI devices communicate using a master/slave relation- +ship over two data lines and two control lines: +- Master Out Slave In (MOSI): supplies the output data from the master + to the inputs of the slaves; +- Master In Slave Out (MISO): supplies the output data from a slave to + the input of the master. It is important to note that there can be no + more than one slave that is transmitting data during any particular + transfer; +- Serial Clock (SCLK): a control line driven by the master, regulating + the flow of data bits; +- Slave Select (SS): a control line that allows slaves to be turned on + and off with hardware control. +More information is also available at http://en.wikipedia.org/wiki/Serial_Peripheral_Interface + +2. Purposes of this code +------------------------ +The supplied patch is starting point for implementing drivers for various +SPI busses as well as devices connected to these busses. Currently, the +SPI core supports only for MASTER mode for system running Linux. + +3. SPI devices stack +-------------------- + +3.1 The SPI outline + +The SPI infrastructure deals with several levels of abstraction. They are +"SPI bus", "SPI bus driver", "SPI slave device" and "SPI device driver". The +"SPI bus" is hardware device, which usually called "SPI adapter", and has +"SPI slave devices" connected. From the Linux' point of view, the "SPI bus" is +structure of type platform_device, and "SPI slave device" is structure of type +spi_device. The "SPI bus driver" is the driver which controls the whole +SPI bus (and, particularly, creates and destroys "SPI slave devices" on the bus), +and "SPI device driver" is driver that controls the only device on the SPI +bus, controlled by "SPI bus driver". "SPI device driver" can indirectly +call "SPI bus driver" to send/receive messages using API provided by SPI +core, and provide its own interface to the kernel and/or userland. +So, the device stack looks as follows: + + +--------------+ +---------+ + | some_bus | | spi_bus | + +--------------+ +---------+ + |..| | + |..|--------+ +---------------+ + +------------+| is parent to | SPI devices | + | SPI busses |+-------------> | | + +------------+ +---------------+ + | | + +----------------+ +----------------------+ + | SPI bus driver | | SPI device driver | + +----------------+ +----------------------+ + +3.2 How do the SPI devices get discovered and probed ? + +In general, the SPI bus driver cannot effectively discover devices +on its bus. Fortunately, the devices on SPI bus usually implemented +onboard, so you need to create array of structures spi_device_desc and +pass this array to function spi_bus_populate, like this: + struct spi_device_desc spi_slaves[] = { + [0] = { + .name = "device1", + .param = device1_params, + }, + [1] = { + .name = "device2", + .param = NULL, + } + [2] = { + NULL, NULL + }; + err = spi_bus_populate( the_spi_bus, spi_slaves, callback ); + +3.3. DMA and SPI messages +------------------------- + +The core provides additional robustness when the buffer suppiled is not +DMA-safe. If it is, the core will allocate DMA-safe buffer and copy user- +supplied buffer to it (before operation in WRITE case, and after in READ case). +This two situations are distinguished by specific flag SPI_M_DMAUNSAFE. +Bus driver should use spimsg_get_buffer and spimsg_put_buffer to access buffer. +The buffer received from spimsg_get_buffer will be always DMA-safe and suitable for +DMA mapping. + +4. SPI functions are structures reference +----------------------------------------- +Please refer to kerneldocs for the information. To create it, use command + $ scripts/kernel-doc -html drivers/spi/* > spi.html + +5. Getting the latest sources +----------------------------- +The stable snapshots are available at + http://spi-devel.sourceforge.net/downloads +The most recent sources can be obtained via the CVS. +Use the following CVS setup to grab them: +* set CVSROOT environment variable to + :pserver:[email protected]:/cvsroot/spi-devel, i. e. +$ export CVSROOT=:pserver:[email protected]:/cvsroot/spi-devel +* login into the anonymous CVS using 'cvs login'. When prompted for password, + just type Enter, i. e. +$ cvs login +Logging in to :pserver:[email protected]:2401/cvsroot/spi-devel +CVS password: +$ +* checkout the 'spi-core' CVS repo: +$ cvs co cpi-core +You'll get a set of patches to be applied to the most recent 2.6-git kernel. + +6. How to contact authors +------------------------- +Do you have any comments ? Enhancements ? Device driver ? Feel free +to contact me: + [email protected] + [email protected] +Visit our project page: + http://spi-devel.sourceforge.net +Subscribe to mailing list: + [email protected] +
arch/arm/Kconfig +2 -0
@@ -753,6 +753,8 @@ source "drivers/usb/Kconfig" source "drivers/mmc/Kconfig" +source "drivers/spi/Kconfig" + endmenu source "fs/Kconfig"
drivers/Kconfig +2 -0
@@ -44,6 +44,8 @@ source "drivers/char/Kconfig" source "drivers/i2c/Kconfig" +source "drivers/spi/Kconfig" + source "drivers/w1/Kconfig" source "drivers/hwmon/Kconfig"
drivers/Makefile +1 -0
@@ -69,3 +69,4 @@ obj-$(CONFIG_SGI_IOC4) += sn/ obj-y += firmware/ obj-$(CONFIG_CRYPTO) += crypto/ obj-$(CONFIG_SUPERH) += sh/ +obj-$(CONFIG_SPI) += spi/
drivers/spi/Kconfig new file +46 -0
46 lines not on this page · raw message
drivers/spi/Makefile new file +19 -0
@@ -0,0 +1,19 @@ +# +# Makefile for the kernel spi bus driver. +# + +spi-y += spi-core.o +spi-$(CONFIG_SPI_CHARDEV) += spi-dev.o +spi-$(CONFIG_SPI_THREAD) += spi-thread.o +spi-$(CONFIG_SPI_CUSTOM_ALLOC) += spi-alloc.o +# bus drivers +obj-$(CONFIG_SPI_PNX) += spipnx.o +# ...functional drivers +obj-$(CONFIG_SPI_PNX4008_EEPROM) += pnx4008-eeprom.o +# ...and the common spi-dev driver +obj-$(CONFIG_SPI) += spi.o + +ifeq ($(CONFIG_SPI_DEBUG),y) +EXTRA_CFLAGS += -DDEBUG +endif +
drivers/spi/spi-core.c new file +659 -0
659 lines not on this page · raw message
drivers/spi/spi-core.h new file +43 -0
43 lines not on this page · raw message
drivers/spi/spi-dev.c new file +209 -0
209 lines not on this page · raw message
drivers/spi/spi-thread.c new file +187 -0
187 lines not on this page · raw message
drivers/spi/spi-thread.h new file +42 -0
42 lines not on this page · raw message
include/linux/spi.h new file +250 -0
250 lines not on this page · raw message
drivers/spi/spi-alloc.c new file +101 -0
101 lines not on this page · raw message
drivers/spi/spi-alloc.h new file +54 -0
54 lines not on this page · raw message
Re: [PATCH 2.6-git 1/4] SPI core refresh: SPI core patch
Only two comments this time.
drivers/spi/spi-core.c line 234
Quoted 5 lines · Vitaly Wool
drivers/spi/spi-core.c new file +5
-0+static int spi_bus_suspend (struct device * dev,pm_message_t message) +{ +int ret =0 ; + +if (dev && dev->driver &&TO_SPI_DRIVER (dev->driver)->suspend ) {
dev will always be non-NULL here.
drivers/spi/spi-core.c line 252
Quoted 5 lines
drivers/spi/spi-core.c new file +5
-0+static int spi_bus_resume (struct device * dev) +{ +int ret =0 ; + +if (dev && dev->driver &&TO_SPI_DRIVER (dev->driver)->suspend ) {
Ditto.
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of: 2.6 Serial core
Re: [PATCH 2.6-git 2/4] SPI core refresh: MTD dataflash driver
In reply to Vitaly Wooldrivers/mtd/devices/Kconfig | 8 drivers/mtd/devices/Makefile | 1 drivers/mtd/devices/mtd_dataflash.c | 601 ++++++++++++++++++++++++++++++++++++ include/linux/spi/flash.h | 27 + 4 files changed, 637 insertions(+)
drivers/mtd/devices/Kconfig +8 -0
@@ -255,5 +255,13 @@ config MTD_DOCPROBE_55AA LinuxBIOS or if you need to recover a DiskOnChip Millennium on which you have managed to wipe the first block. +config MTD_DATAFLASH + tristate "Support for AT45xxx DataFlash" + depends on MTD && SPI && EXPERIMENTAL + help + This enables access to AT45xxx DataFlash chips, using SPI. + Sometimes DataFlash chips are packaged inside MMC-format + cards; at this writing, the MMC stack won't handle those. + endmenu
drivers/mtd/devices/Makefile +1 -0
@@ -23,3 +23,4 @@ obj-$(CONFIG_MTD_MTDRAM) += mtdram.o obj-$(CONFIG_MTD_LART) += lart.o obj-$(CONFIG_MTD_BLKMTD) += blkmtd.o obj-$(CONFIG_MTD_BLOCK2MTD) += block2mtd.o +obj-$(CONFIG_MTD_DATAFLASH) += mtd_dataflash.o
drivers/mtd/devices/mtd_dataflash.c new file +601 -0
601 lines not on this page · raw message
include/linux/spi/flash.h new file +27 -0
@@ -0,0 +1,27 @@ +#ifndef LINUX_SPI_FLASH_H +#define LINUX_SPI_FLASH_H + +struct mtd_partition ; + +/** +* struct flash_platform_data: board-specific flash data +* @name: optional flash device name (eg, as used with mtdparts=) +* @parts: optional array of mtd_partitions for static partitioning +* @nr_parts: number of mtd_partitions for static partitoning +* +* Board init code (in arch/.../mach-xxx/board-yyy.c files) can +* provide information about SPI flash parts (such as DataFlash) to +* help set up the device and its appropriate default partitioning. +* +* Note that for DataFlash, sizes for pages, blocks, and sectors are +* rarely powers of two; and partitions should be sector-aligned. +*/ +struct flash_platform_data { +char *name; +struct mtd_partition *parts; +unsigned int nr_parts; + +/* we'll likely add more ... use JEDEC IDs, etc */ +}; + +#endif
PATCH 3/4 SPI core refresh: SPI/PNX controller
In reply to Vitaly WoolKconfig | 8 pnx4008-eeprom.c | 121 +++++++++++++ spipnx-init.h | 9 + spipnx.c | 488 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ spipnx.h | 58 ++++++ 5 files changed, 684 insertions(+)
drivers/spi/Kconfig +8 -0
@@ -42,5 +42,13 @@ config SPI_CHARDEV Say Y here to use /dev/spiNN device files. They make it possible to have user-space programs use the SPI bus. +config SPI_PNX + tristate "PNX SPI bus support" + depends on ARCH_PNX4008 && SPI + +config SPI_PNX4008_EEPROM + tristate "Dummy EEPROM driver" + depends on SPI && SPI_PNX && ARCH_PNX4008 + endmenu
drivers/spi/pnx4008-eeprom.c new file +121 -0
121 lines not on this page · raw message
drivers/spi/spipnx-init.h new file +9 -0
@@ -0,0 +1,9 @@ +#include <linux/kernel.h> +#include <asm/arch/spi.h> + +#ifdef CONFIG_ARCH_PNX4008 +struct spipnx_wifi_params spipnx_wifi_params = { + .gpio_cs_line =GPIO_03 , +}; +#endif +
drivers/spi/spipnx.c new file +488 -0
488 lines not on this page · raw message
drivers/spi/spipnx.h new file +58 -0
58 lines not on this page · raw message
PATCH 4/4 SPI core refresh: dumb EEPROM driver
In reply to Vitaly Woolpnx4008-eeprom.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+)
drivers/spi/pnx4008-eeprom.c new file +121 -0
@@ -0,0 +1,121 @@ +#include <linux/kernel.h> +#include <linux/init.h> +#include <linux/module.h> +#include <linux/slab.h> +#include <linux/sched.h> +#include <linux/device.h> +#include <linux/spi.h> +#include <linux/proc_fs.h> +#include <linux/ctype.h> + +#include "spipnx.h" + +#define EEPROM_SIZE 256 +#define DRIVER_NAME "EEPROM" +#define READ_BUFF_SIZE 160 + +static int __init spi_eeprom_init (void ); +static void __exit spi_eeprom_cleanup(void); + +static int spiee_read_block (struct device *d,void *block) +{ +struct spi_device *device =TO_SPI_DEV (d); +char cmd[2 ]; +struct spi_msg *msg =spimsg_alloc (device, +SPI_M_CS |SPI_M_CSREL , +NULL , +0 , +NULL ); +struct spi_msg *msg_cmd =spimsg_chain (msg,SPI_M_CS |SPI_M_WR |SPI_M_DMAUNSAFE , cmd,2 ,NULL ); +spimsg_chain (msg_cmd,SPI_M_RD |SPI_M_CSREL , block,256 ,NULL ); + + cmd[0 ] =0x03 ; + cmd[1 ] =0x00 ; + +spimsg_set_clock (msg,2000000 );/* 2 MHz */ +spi_transfer (msg,NULL ); +spimsg_free (msg); +return 256 ; +} +static ssize_t blk_show (struct device *d,struct device_attribute *attr,char *text ) +{ +char *rdbuff =kmalloc (256 ,SLAB_KERNEL ); +char line1[80 ],line2[80 ]; +char item1[5 ], item2[5 ]; +int bytes, i, x, blen; + + blen =spiee_read_block (d, rdbuff); + + bytes =0 ; + +strcpy (text,"" ); +for (i =0 ; i < blen; i +=8 ) { +strcpy (line1,"" ); +strcpy (line2,"" ); +for (x = i; x < i +8 ; x++) { +if (x > blen) { +sprintf (item1," " ); +sprintf (item2," " ); + }else { +sprintf (item1,"%02x " , rdbuff[x]); +if (isprint (rdbuff[x])) { +sprintf (item2,"%c" , rdbuff[x]); + }else { +sprintf (item2,"." ); + } + } +strcat (line1, item1); +strcat (line2, item2); + } + +strcat (text, line1); +strcat (text,"| " ); +strcat (text, line2); +strcat (text,"\n" ); + + bytes += (strlen (line1 ) +strlen (line2) +4 ); + } + +kfree (rdbuff); + +return bytes +1 ; +} + +static DEVICE_ATTR(blk, S_IRUGO, blk_show, NULL ); + + +static int spiee_probe (struct spi_device *this_dev) +{ +device_create_file (&this_dev->dev, &dev_attr_blk); +return 0 ; +} + +static int spiee_remove (struct spi_device *this_dev) +{ +device_remove_file (&this_dev->dev, &dev_attr_blk); +return 0 ; +} + +static struct spi_driver eeprom_driver = { + .driver = { + .name =DRIVER_NAME , + }, + .probe = spiee_probe, + .remove = spiee_remove, +}; + +static int __init spi_eeprom_init (void ) +{ +return spi_driver_add (&eeprom_driver); +} +static void __exit spi_eeprom_cleanup(void) +{ +spi_driver_del (&eeprom_driver); +} + +module_init (spi_eeprom_init); +module_exit (spi_eeprom_cleanup); + +MODULE_LICENSE ("GPL" ); +MODULE_AUTHOR ("dmitry pervushin <[email protected]>" ); +MODULE_DESCRIPTION ("SPI EEPROM driver" );
Quoted 1 line · Vitaly Wool
Greetings,
Hi,
Quoted 6 lines
this message fill be followed by the following four ones:
1) updated SPI core from Dmitry Pervushin/Vitaly Wool
2) Atmel MTD dataflash driver port for this core
3) SPI controller driver for Philips SPI controller
4) dumb EEPROM driver for EEPROM chip on SPI bus
Quoted 8 lines
This SPI core features:
* multiple SPI controller support
* multiple devices on the same bus support
* DMA support
* synchronous and asynchronous transfers
* library for asynchronous transfers on the bus using kernel threads
* character device interface
* custom lightweight SPI message allocation mechanism
[snipped]
One problem I still have with this implementation (well, if I remember correctly David's has the same problem) is that it's not possible to read/write from/to the SPI bus in interrupt context.
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
Rui
Quoted 4 lines · Rui Sousa
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
Hmm... what do you mean by "cannot read/write" ? Normally you can write/read registers in interrupt context, and then set the flag/complete the completion/what else ?
In other words, could you please share the code that causes problems ?
Signature · 1 line
cheers, dmitry pervushin
Quoted 5 lines · Rui Sousa
Quoted 4 lines
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
I don't understand the question. The SPI controller may be busy handling transactions for a different device, so the best you could do from _any_ IRQ handler is to queue some messages that will get to the IRQ-issuing device sometime later. It's not possible to make the other transactions finish any faster, or abort them safely mid-transfer.
The way the ads7846 driver handles it, for example, is to issue a transaction collecting a bunch of touchscreen sensor readings. And disable its (nonsharable) irq, since the interrupt will be raised for some time and there's no portable way for Linux to force the IRQ to trigger only on the relevant edge.
See the 2.6.15-rc5-mm1 patches...
- Dave
Quoted 8 lines · David Brownell
Quoted 5 lines · Rui Sousa
Quoted 4 lines
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
I don't understand the question. The SPI controller may be busy handling transactions for a different device,
If you provide an async API to start with... if everything is sync then this problem goes away[1]. When I started writing the SPI layer for my platform (before any SPI patches started circulating) I was going to use an async API and then decided against it. For the type of device I'm using, a SLIC device, where only control data is exchanged on the SPI bus (read register, check some bit, write register, ...) and very small amounts of data are transferred at a time (4 bytes maximum) there is really no point to an async API.
Once you give the possibility to transfer data asynchronously on the SPI bus, and even if you add a pseudo sync interface, you loose the possibility to call the transfer function (synchronously) from hard/soft interrupt context. One way or the other you will eventually need to sleep.
This forces a simple code like:
u8 device_read_reg(int reg)
{
u8 buf;
buf = reg;
spi_write(dev, &buf, 1);
spi_read(dev, &buf, 1);
return buf[0];
}to be moved to user context or to become much more complex (state machine + async callbacks).
I guess what I'm trying to say is that for a simple device like the one I'm using, having an async API adds more trouble that what it's worth it. Unfortunately I don't have a better solution to put forward.
Quoted 10 lines
so the best you
could do from _any_ IRQ handler is to queue some messages that will get to the IRQ-issuing device sometime later.
It's not
possible to make the other transactions finish any faster, or abort them safely mid-transfer.The way the ads7846 driver handles it, for example, is to issue a transaction collecting a bunch of touchscreen sensor readings. And disable its (nonsharable) irq,
nonsharable is the magic word. I thought of this solution, leaving the interrupt handler with the interrupt masked and do all the handling in a workqueue... but my GPIO pin used to generate the interrupt is being shared with another device.
Quoted 9 lines
since the interrupt will be
raised for some time and there's no portable way for Linux to force the IRQ to trigger only on the relevant edge.See the 2.6.15-rc5-mm1 patches...
- Dave
Rui
[1] I know this is not a reasonable solution for a number of cases.
Quoted 7 lines · dmitry pervushin
Quoted 4 lines · Rui Sousa
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
Hmm... what do you mean by "cannot read/write" ? Normally you can write/read registers in interrupt context
The registers I want to read are from a SPI device (a SPI slave attached to a SPI bus). I need to use SPI bus transfers to access them.
Quoted 2 lines
, and then set the
flag/complete the completion/what else ?
If I read the API correctly reading/writing a byte from the SPI bus (synchronously) always implies putting the task doing the read to sleep:
int spi_transfer(struct spi_msg *msg, void (*callback) (struct spi_msg *, int))
{
...
err = TO_SPI_BUS_DRIVER(bus->driver)->queue(msg); wait_for_completion(&msg->sync);
...
}
So, how can I, from an interrupt handler, read/write a couple of bytes from my SPI device using this API?
Quoted 2 lines
In other words, could you please share the code that causes problems ?
Rui
Quoted 9 lines · Rui Sousa
Quoted 7 lines · dmitry pervushin
Quoted 4 lines · Rui Sousa
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
Hmm... what do you mean by "cannot read/write" ? Normally you can write/read registers in interrupt context
I expect the answer from David Brownell too; and, because both discussed frameworks are going to be the only (and the best), please also pay attention to his (possible) answer. But I'd comment your code :)
Quoted 7 lines
If I read the API correctly reading/writing a byte from the SPI bus (synchronously) always implies putting the task doing the read to sleep:
int
spi_transfer(struct spi_msg *msg, void (*callback) (struct spi_msg *, int))
{
Quoted 8 lines
...
err = TO_SPI_BUS_DRIVER(bus->driver)->queue(msg); wait_for_completion(&msg->sync);...
}So, how can I, from an interrupt handler, read/write a couple of bytes from my SPI device using this API?
You can issue the request (in terms of core that you are using, queue the message) in interrupt context, and perform the rest of processing in `status' callback. It will be called when message is processed. You can ack the irq here, and continue your processing.
Looks like we'll have to remove explicit wait_for_completion from spi_trasnfer to make it (potentially) work. Rui, your turn will then be
a) not to use thread async message handling which is the default but
b) use some kind of PIO method for reading from the SPI bus.
The other solution which looks preferable to me is to have a pretty simple interrupt handler which just wakes up a thread which in turn reads what it wants not caring much about sleeps (as it's not gonna be interrupt context any more).
Vitaly
Quoted 55 lines · Rui Sousa
Quoted 13 lines · dmitry pervushin
Quoted 6 lines · Rui Sousa
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
Hmm... what do you mean by "cannot read/write" ? Normally you can write/read registers in interrupt context
The registers I want to read are from a SPI device (a SPI slave attached to a SPI bus). I need to use SPI bus transfers to access them.
Quoted 4 lines
, and then set the
flag/complete the completion/what else ?If I read the API correctly reading/writing a byte from the SPI bus (synchronously) always implies putting the task doing the read to sleep:
int
spi_transfer(struct spi_msg *msg, void (*callback) (struct spi_msg *, int))
{...
err = TO_SPI_BUS_DRIVER(bus->driver)->queue(msg); wait_for_completion(&msg->sync);...
}So, how can I, from an interrupt handler, read/write a couple of bytes from my SPI device using this API?
Quoted 4 lines
In other words, could you please share the code that causes problems ?
Rui
Quoted 5 lines · Vitaly Wool
Looks like we'll have to remove explicit
wait_for_completionfromspi_trasnferto make it (potentially) work.
Rui, your turn will then be
a) not to use thread async message handling which is the default but
b) use some kind of PIO method for reading from the SPI bus.
This is what I'm doing now (sync + PIO). In any case the SPI bus controller I'm using only supports PIO.
Quoted 4 lines
The other solution which looks preferable to me is to have a pretty simple interrupt handler which just wakes up a thread which in turn reads what it wants not caring much about sleeps (as it's not gonna be interrupt context any more).
The main problem is that just to be able to ack the interrupt I need to write on the SPI bus. So now I need to (1) modify my GPIO interrupt handler so that it let's me exit before acking the interrupt on the device (2) no longer share this GPIO with other devices interrupts...
It's possible to use the proposed API, it's just that in my case it will increase the SPI slave driver complexity.
Quoted 1 line
Vitaly
Rui
Quoted 59 lines
Quoted 55 lines · Rui Sousa
Quoted 13 lines · dmitry pervushin
Quoted 6 lines · Rui Sousa
How do you handle IRQ's generated by a SPI device (e.g ack the interrupt, check if it was the SPI device that generated the interrupt, ...) if you can't read/write on the SPI bus from interrupt context?
Hmm... what do you mean by "cannot read/write" ? Normally you can write/read registers in interrupt context
The registers I want to read are from a SPI device (a SPI slave attached to a SPI bus). I need to use SPI bus transfers to access them.
Quoted 4 lines
, and then set the
flag/complete the completion/what else ?If I read the API correctly reading/writing a byte from the SPI bus (synchronously) always implies putting the task doing the read to sleep:
int
spi_transfer(struct spi_msg *msg, void (*callback) (struct spi_msg *, int))
{...
err = TO_SPI_BUS_DRIVER(bus->driver)->queue(msg); wait_for_completion(&msg->sync);...
}So, how can I, from an interrupt handler, read/write a couple of bytes from my SPI device using this API?
Quoted 4 lines
In other words, could you please share the code that causes problems ?
Rui
[PATCH/RFC] SPI: add async message handing library to David Brownell's core
In reply to Vitaly WoolGreetings,
as a part of the convergence process between two SPI cores (one is developed by David Brownell and the other one -- by Dmitry Pervushin and me) I'd like to post here one more result of mindwork :). Despite our previous activities of that kind which were more related to porting drivers from one core to the other, this is a port of the library for handling async SPI messages using kernel threads, one per each SPI controller, from our core to David's.
This thingie hasn't been thoroughly tested yet, but it's lightweight and easy to understand so I don't think solving the problems that may arise will take long. Though I haven't actually done that yet, I'm sure that Stephen's PXA SSP driver will become easier to understand and less in footprint and will work faster when it's rewritten using this library. (Yes, I do expect performance improvement here as the current implementation schedules multiple tasklets, so scheduling penalty is high.)
This patch performes just minimal intrusion to the SPI core itself, adding a 'void *context' field to the spi_master structure which seems to be a good thing anyway.
drivers/spi/Kconfig | 9 + drivers/spi/Makefile | 3 drivers/spi/spi-thread.c | 198 +++++++++++++++++++++++++++++++++++++++++ include/linux/spi/spi-thread.h | 27 +++++ include/linux/spi/spi.h | 3 5 files changed, 240 insertions(+)
drivers/spi/Kconfig +9 -0
@@ -13,6 +13,7 @@ config SPI_ARCH_HAS_MASTER default y if ARCH_AT91 default y if ARCH_OMAP default y if ARCH_PXA + default y if ARCH_PNX4008 default y if X86 # devel hack only!! (ICH7 can...) config SPI_ARCH_HAS_SLAVE
@@ -63,6 +64,14 @@ config SPI_MASTER controller and the protocol drivers for the SPI slave chips that are connected. +config SPI_THREAD + boolean "Library for threaded asynchronous message processing" + depends on SPI_MASTER + help + Choose this if you want to use thread-based asynchronous + message processing library. Using kernel threads for + asynchronous data processing is the most common option. + comment "SPI Master Controller Drivers" depends on SPI_MASTER
drivers/spi/Makefile +3 -0
@@ -10,6 +10,9 @@ endif # config declarations into driver model code obj-$(CONFIG_SPI_MASTER) += spi.o +# thread-based async message handling library +obj-$(CONFIG_SPI_THREAD) += spi-thread.o + # SPI master controller drivers (bus) obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o # ... add above this line ...
drivers/spi/spi-thread.c new file +198 -0
198 lines not on this page · raw message
include/linux/spi/spi-thread.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* linux/drivers/spi/spi-thread.h +* +* Copyright (C) 2005 MontaVista Software, Inc <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License. +* +*/ +#ifndef __SPI_THREAD_H +#define __SPI_THREAD_H + + +#if defined (CONFIG_SPI_THREAD ) +extern int spi_start_async (struct spi_master *,int (*)(struct spi_master *,struct spi_message *)); +extern void spi_stop_async (struct spi_master *); +#else +static inline int spi_start_async (struct spi_master *master,int (*xfer)(struct spi_master *,struct spi_message *)) +{ +return -EINVAL ; +} +static inline void spi_stop_async (struct spi_master *master) +{ +} +#endif /* CONFIG_SPI_THREAD */ +#endif /* __SPI_THREAD_H */
include/linux/spi/spi.h +3 -0
@@ -152,6 +152,7 @@ static inline void spi_unregister_driver* device's SPI controller; protocol code may call this. * @transfer: adds a message to the controller's transfer queue. * @cleanup: frees controller-specific state +* @context: controller-specific data * * Each SPI master controller can communicate with one or more spi_device * children. These make a small bus, sharing MOSI, MISO and SCK signals
@@ -207,6 +208,8 @@ struct spi_master {/* called on release() to free memory provided by spi_master */ void (*cleanup)(const struct spi_device *spi); + +void *context; };/* the spi driver core manages memory for the spi_master classdev */
[PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Greetings,
this patch removes nasty buffer allocation in spi core for sync message transfers. (And thus solves the problem of possible priority inversion I've pointed out before).
The write_then_read function sets the flags singalling for buffers not being DMA-safe and it's absolutely up to the controller whatta do with that.
I. e. it will prolly allocate/copy if it performs DMA transfers or just don't do anything special if it's working in PIO. It can even use a single DMA-safe buffer as David's core did (although I wouldn't encourage anyone to do that! :)) This is of coure more flexible and also more optimal as it will *not* require _redundant_ memcpy's and semaphores protection in case of PIO transfers.
Please note that this is a key patch for us in some aspects, although it's rather small! :)
drivers/spi/spi.c | 35 ++++++----------------------------- include/linux/spi/spi.h | 7 +++++++ 2 files changed, 13 insertions(+), 29 deletions(-)
drivers/spi/spi.c +6 -29
@@ -507,10 +507,6 @@ int spi_sync(struct spi_device *spi, str }EXPORT_SYMBOL_GPL (spi_sync); -#define SPI_BUFSIZ (SMP_CACHE_BYTES) - -static u8 *buf; -/** * spi_write_then_read - SPI synchronous write followed by read * @spi: device with which data will be exchanged
@@ -532,39 +528,28 @@ int spi_write_then_read(struct spi_devic const u8 *txbuf, unsigned n_tx, u8 *rxbuf, unsigned n_rx) { - static DECLARE_MUTEX(lock); - int status;struct spi_message message;struct spi_transfer x[2 ]; -/* Use preallocated DMA-safe buffer. We can't avoid copying here, -* (as a pure convenience thing), but we can keep heap costs -* out of the hot path. -*/ -if ((n_tx + n_rx) >SPI_BUFSIZ ) -return -EINVAL ; - -down (&lock);memset (x,0 ,sizeof x); +memset (&message,0 ,sizeof message); -memcpy (buf, txbuf, n_tx); - x[0 ].tx_buf = buf; + x[0 ].tx_buf = tx_buf; x[0 ].len = n_tx; + x[0 ].tx_dma_unsafe =1 ; - x[1 ].rx_buf = buf + n_tx; + x[1 ].rx_buf = rx_buf; x[1 ].len = n_rx; + x[1 ].rx_dma_unsafe =1 ;/* do the i/o */ message.transfers = x; message.n_transfer =ARRAY_SIZE (x); status =spi_sync (spi, &message); -if (status ==0 ) { -memcpy (rxbuf, x[1 ].rx_buf, n_rx); +if (status ==0 ) status = message.status; - } -up (&lock);return status; }EXPORT_SYMBOL_GPL (spi_write_then_read);
@@ -575,12 +560,6 @@ static int __init spi_init(void) {int status; - buf =kmalloc (SPI_BUFSIZ ,SLAB_KERNEL ); -if (!buf) { - status = -ENOMEM ; - goto err0; - } - status =bus_register (&spi_bus_type);if (status <0 ) goto err1;
@@ -593,9 +572,7 @@ static int __init spi_init(void) err2:bus_unregister (&spi_bus_type); err1: -kfree (buf); buf =NULL ; -err0:return status; }
include/linux/spi/spi.h +7 -0
@@ -248,6 +248,10 @@ extern struct spi_master *spi_busnum_to_* @rx_dma: DMA address of buffer, if spi_message.is_dma_mapped * @len: size of rx and tx buffers (in bytes) * @cs_change: affects chipselect after this transfer completes +* @tx_dma_unsafe: signals to controller driver that the buffer +* is not DMA-safe +* @rx_dma_unsafe: signals to controller driver that the buffer +* is not DMA-safe * @delay_usecs: microseconds to delay after this transfer before * (optionally) changing the chipselect status, then starting * the next transfer or completing this spi_message.
@@ -288,6 +292,9 @@ struct spi_transfer {dma_addr_t rx_dma; unsigned cs_change:1; + unsigned rx_dma_unsafe:1; + unsigned tx_dma_unsafe:1; +u16 delay_usecs; };
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 2 lines · Vitaly Wool
this patch removes nasty buffer allocation in spi core for sync message transfers.
That's not "core" in the normal "everyone must use this" sense. Even the folk that do use synchronous transfers aren't always going to use that particular codepath.
Neither is it "remove" in the normal sense either. The hot path never had an allocation before ... but it could easily have one now, because that sort of bounce-buffer semantic is what a DMA_UNSAFE flag demands from the lower levels. (That's a key part part of the proposed change that's not included in this patch... making the chage look much smaller.)
How much of the reason you're arguing for this is because you have that WLAN stack that does some static allocation for I/O buffers? Changing that to use dynamic allocation -- the more usual style -- should be easy. But instead, you want all code in the SPI stack to need to worry about two different kinds of I/O memory: the normal stuff, and the DMA-unsafe kind. Not just this WLAN code which for some reason started out using a nonportable scheme for allocating I/O buffers.
It'd take a lot more persuasion to make me think that's a good idea. That's the kind of subtle confusion that really promotes hard-to-find bugs in drivers, and lots of developer confusion. And all that to support a new less-portable I/O buffer model...
It's way better to just insist that all I/O buffers (in all generic APIs) be DMA-safe. AFAICT that's a pretty standard rule everywhere in Linux.
- Dave
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 3 lines · David Brownell
It's way better to just insist that all I/O buffers (in all generic APIs) be DMA-safe. AFAICT that's a pretty standard rule everywhere in Linux.
I agree.
greg k-h
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 12 lines · Greg KH
Quoted 5 lines · David Brownell
It's way better to just insist that all I/O buffers (in all generic APIs) be DMA-safe. AFAICT that's a pretty standard rule everywhere in Linux.
I agree.
Well, why then David doesn't insist on that in his own code?
His synchronous transfer functions are allocating transfer buffers on stack which is not DMA-safe.
Then he starts messing with allocate-or-use-preallocated stuff etc. etc.
Why isn't he just kmalloc'ing/kfree'ing buffers each time these functions are called (as he proposes for upper layer drivers to do)? That's a significant inconsistency. Is it also the thing you agree with?
And they way he does it implies redundant memcpy's and kmalloc's: suppose we have two controller drivers working in two threads and calling write_then_read in such a way that the one called later has to allocate a new buffer. Suppose also that both controller drivers are working in *PIO* mode. In this situation you have one redundant kmalloc and two redundant memcpy's, not speaking about overhead brought up by mutexes.
The thing is that only controller driver is aware whether DMA is needed or not, so it's controller driver that should work it out. Requesting all the buffers to be DMA-safe will make a significant performance drop on all small transfers!
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 11 lines · Vitaly Wool
Quoted 9 lines · Greg KH
Quoted 5 lines · David Brownell
It's way better to just insist that all I/O buffers (in all generic APIs) be DMA-safe. AFAICT that's a pretty standard rule everywhere in Linux.
Quoted 4 lines
Quoted 3 lines
I agree.
Well, why then David doesn't insist on that in his own code?
Heh, I don't know, only David can answer that :)
Quoted 15 lines
His synchronous transfer functions are allocating transfer buffers on stack which is not DMA-safe.
Then he starts messing with allocate-or-use-preallocated stuff etc. etc. Why isn't he just kmalloc'ing/kfree'ing buffers each time these functions are called (as he proposes for upper layer drivers to do)? That's a significant inconsistency. Is it also the thing you agree with?And they way he does it implies redundant memcpy's and kmalloc's:
suppose we have two controller drivers working in two threads and callingwrite_then_readin such a way that the one called later has to allocate a new buffer. Suppose also that both controller drivers are working in *PIO* mode. In this situation you have one redundant kmalloc and two redundant memcpy's, not speaking about overhead brought up by mutexes.
Quoted 4 lines
The thing is that only controller driver is aware whether DMA is needed or not, so it's controller driver that should work it out. Requesting all the buffers to be DMA-safe will make a significant performance drop on all small transfers!
What is the speed of your SPI bus?
And what are your preformance requirements?
thanks,
greg k-h
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 5 lines · Greg KH
What is the speed of your SPI bus?
And what are your preformance requirements?
The maximum frequency for the SPI bus is 26 MHz, WLAN driver is to work at true 10 Mbit/sec.
Vitaly
P. S. I'm speaking not about this particular case during most part of this conversation. Sound cards behind the SPI bus will suffer a lot more since it's their path to use wXrY functions (lotsa small transfers) rather than WLAN's.
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 3 lines · Vitaly Wool
Sound cards behind the SPI bus will suffer a lot more
since it's their path to use wXrY functions (lotsa small transfers) rather than WLAN's.
No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
Remember too that sending audio data over SPI (rather than say I2S, McBSP etc) is a different case than using it for the mixer controls.
- Dave
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 4 lines · David Brownell
On Wednesday 14 December 2005 9:53 am, Vitaly Wool wrote:
Quoted 9 lines
Quoted 4 lines
Sound cards behind the SPI bus will suffer a lot more
since it's their path to use wXrY functions (lotsa small transfers) rather than WLAN's.No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
So just answer please yes or no: are your spi_wXrY functions intended for usage at all or not? Maybe you should remove them completely?
Vitaly
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 3 lines
So just answer please yes or no: are your
spi_wXrYfunctions intended for usage at all or not?
Certainly. Not _mis_used though.
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
In reply to David BrownellQuoted 4 lines · David Brownell
On Wednesday 14 December 2005 9:53 am, Vitaly Wool wrote:
Quoted 9 lines
Quoted 4 lines
Sound cards behind the SPI bus will suffer a lot more
since it's their path to use wXrY functions (lotsa small transfers) rather than WLAN's.No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
Two more cents: ads7846 has a bunch of code just related to the need to do something from the interrupt context. Without this constraint, I don't see why w8r8 can not be used in most cases (of course, with the changes I propose).
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
In reply to David BrownellQuoted 4 lines · David Brownell
No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe.
Making it DMA-safe in thread that does the very message processing is a good way of overcoming this.
Using preallocated buffer is not a good way, since it may well be already used by another interrupt or not yet processed by the worker thread (or tasklet, or whatever).
The way it's done in this ads7846 driver is not quite acceptable. Losing the transfer if the previous one is still processed is *not* the way to go in some cases. One can not predict how many transfers are gonna be dropped due to "previous trransfer is being processed" problem, it depends on the system load. And though it's not a problem for touchscreen, it *will* be a problem if it were MMC, for instance.
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 6 lines · Vitaly Wool
Quoted 4 lines · David Brownell
No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
Quoted 7 lines
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe. Making it DMA-safe in thread that does the very message processing is a good way of overcoming this.
Using preallocated buffer is not a good way, since it may well be already used by another interrupt or not yet processed by the worker thread (or tasklet, or whatever).
Yes it is a good way. That's the way USB currently works in the kernel, and it works just fine. It keeps the rules simple and everyone knows what needs to be done.
thanks,
greg k-h
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 21 lines · Greg KH
Quoted 10 lines · Vitaly Wool
Quoted 6 lines · David Brownell
No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
Quoted 7 lines
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe. Making it DMA-safe in thread that does the very message processing is a good way of overcoming this. Using preallocated buffer is not a good way, since it may well be
already used by another interrupt or not yet processed by the worker
thread (or tasklet, or whatever).
Quoted 5 lines
Yes it is a good way. That's the way USB currently works in the kernel, and it works just fine. It keeps the rules simple and everyone knows what needs to be done.
Looking at my usbnet stuff, I can't share that opinion :-/
Are you really ready to lower the performance and quality of service just for approach uniformity?
And, can you please point me out the examples of devices behind USB bus that need to write registers from an interrupt context?
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 25 lines · Vitaly Wool
Quoted 23 lines · Greg KH
Quoted 19 lines · Vitaly Wool
Quoted 6 lines · David Brownell
No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe. Making it DMA-safe in thread that does the very message processing is a good way of overcoming this.
Using preallocated buffer is not a good way, since it may well be already used by another interrupt or not yet processed by the worker thread (or tasklet, or whatever).
Quoted 8 lines
Quoted 5 lines
Yes it is a good way. That's the way USB currently works in the kernel, and it works just fine. It keeps the rules simple and everyone knows what needs to be done.
Looking at my usbnet stuff, I can't share that opinion :-/ Are you really ready to lower the performance and quality of service just for approach uniformity?
What performance issues? As an example, USB has this rule, and we can saturate a 480Mbit line with a _userspace_ driver (loads of memcopy calles involved there.)
Quoted 2 lines
And, can you please point me out the examples of devices behind USB bus that need to write registers from an interrupt context?
usb to serial drivers need to allocate buffers for their write functions as they can be called in irq context from a tty line dicipline, which causes a USB packet to be dynamically created and sent to the USB core. I also think the USB network and ATM drivers have these requirements too, just search for GFP_ATOMIC in the drivers/usb/ directory to find these instances.
Anyway, this is my last response about this issue. Please consider it over.
thanks,
greg k-h
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 42 lines · Greg KH
Quoted 39 lines · Vitaly Wool
Quoted 35 lines · Greg KH
Quoted 14 lines · Vitaly Wool
Quoted 8 lines · David Brownell
No, "stupid drivers will suffer"; nothing new. Just observe how the ads7846 touchscreen driver does small async transfers.
Quoted 9 lines
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe. Making it DMA-safe in thread that does the very message processing is a good way of overcoming this. Using preallocated buffer is not a good way, since it may well be
already used by another interrupt or not yet processed by the worker
thread (or tasklet, or whatever).Yes it is a good way. That's the way USB currently works in the kernel, and it works just fine. It keeps the rules simple and everyone knows what needs to be done.
Quoted 10 lines
Quoted 4 lines
Looking at my usbnet stuff, I can't share that opinion :-/
Are you really ready to lower the performance and quality of service just for approach uniformity?What performance issues? As an example, USB has this rule, and we can saturate a 480Mbit line with a _userspace_ driver (loads of memcopy calles involved there.)
What CPU is used there? I guess it's not 144 MHz ARM ;-)
Quoted 6 lines
Quoted 5 lines
And, can you please point me out the examples of devices behind USB bus that need to write registers from an interrupt context?
Quoted 8 lines
usb to serial drivers need to allocate buffers for their write functions as they can be called in irq context from a tty line dicipline, which causes a USB packet to be dynamically created and sent to the USB core. I also think the USB network and ATM drivers have these requirements too, just search for
GFP_ATOMICin thedrivers/usb/directory to find these instances.
Oh BTW... I'm experiencing constant problems with root filesystem over NFS over usbnet on my target I'm getting "server not responding, still trying" error whenever the system (208-MHz ARM926 board) is under heavy load. I think it may well be related to the thing we discuss.
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 5 lines · Vitaly Wool
Quoted 4 lines
What performance issues? As an example, USB has this rule, and we can saturate a 480Mbit line with a _userspace_ driver (loads of memcopy calles involved there.)
What CPU is used there? I guess it's not 144 MHz ARM ;-)
I don't think so either, but don't remember the exact cpu (but it was an embedded system...)
Quoted 11 lines
Quoted 11 lines
Quoted 3 lines
And, can you please point me out the examples of devices behind USB bus that need to write registers from an interrupt context?
usb to serial drivers need to allocate buffers for their write functions as they can be called in irq context from a tty line dicipline, which causes a USB packet to be dynamically created and sent to the USB core. I also think the USB network and ATM drivers have these requirements too, just search for
GFP_ATOMICin thedrivers/usb/directory to find these instances.
Quoted 5 lines
Oh BTW... I'm experiencing constant problems with root filesystem over NFS over usbnet on my target
I'm getting "server not responding, still trying" error whenever the system (208-MHz ARM926 board) is under heavy load.
I think it may well be related to the thing we discuss.
I really doubt it. See David's other message to you about this. And if you have problems, please report them on the linux-usb-devel mailing list. We have never had bug reports due to issues involving the DMA requirements (with the exception of people finding drivers that were trying to send data off of the stack...)
thanks,
greg k-h
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 3 lines
Quoted 3 lines
Oh BTW... I'm experiencing constant problems with root filesystem over NFS over usbnet on my target
As you clarified off-line ... it's "pegasus", not "usbnet", which is giving you problems. That's entirely different code; although "pegasus" could be modified to run on top of the core that "usbnet" provides.
The problems I've seen with "pegasus" have more to do with wierd chip behaviors (and weak fault recovery logic) than with anything DMA-related.
- Dave
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
In reply to Vitaly WoolQuoted 2 lines · Vitaly Wool
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe.
kmalloc(..., GFP_ATOMIC) is the way to allocate memory in irq context. It's done that way throughout the kernel.
Quoted 2 lines
Making it DMA-safe in thread that does the very message processing is a good way of overcoming this.
The rest of Linux appears to work fine without needing such mechanisms...
I really fail to see why you think SPI needs that. USB isn't the only counterexample, but it's particularly relevant since both USB and SPI use asynchronous message passing over serial links ... and USB has a
rather complete driver stack over it. (None of the USB based WLAN
drivers need those static buffers you worry about, by the way...)
Quoted 3 lines
Using preallocated buffer is not a good way, since it may well be already used by another interrupt or not yet processed by the worker thread (or tasklet, or whatever).
We would call those "driver bugs" and expect patches. :)
Quoted 3 lines
The way it's done in this ads7846 driver is not quite acceptable. Losing the transfer if the previous one is still processed is *not* the way to go in some cases.
That's not the way it works though. And you seem to have made up a "losing the transfer" failure mode out of whole cloth; I have no idea where that came from.
Quoted 4 lines
One can not predict how many transfers are
gonna be dropped due to "previous trransfer is being processed" problem, it depends on the system load. And though it's not a problem for touchscreen, it *will* be a problem if it were MMC, for instance.
Huh, well I've already seen one nice "mmc/sd over SPI" driver, using a slightly earlier version of the framework than is found in mm3. It's being used for root filesystems.
So demonstrably there is no problem for MMC/SD, either.
- Dave
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 4 lines · David Brownell
On Wednesday 14 December 2005 10:47 pm, Vitaly Wool wrote:
Quoted 8 lines
Quoted 3 lines
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe.
kmalloc(..., GFP_ATOMIC)is the way to allocate memory in irq context. It's done that way throughout the kernel.
It's not applicable within the RT-related changes. kmalloc anyway takes mutexes, so allocationg it in interrupt context is buggy. *Legacy* kernel code does that but why produce a new code with that?
Quoted 10 lines
Quoted 6 lines
Making it DMA-safe in thread that does the very message processing is a good way of overcoming this.
The rest of Linux appears to work fine without needing such mechanisms...
The rest of Linux still has a lot of bugs. Noone I guess is ready to argue that.
Quoted 7 lines
I really fail to see why you think SPI needs that. USB isn't the only counterexample, but it's particularly relevant since both USB and SPI use asynchronous message passing over serial links ... and USB has a
rather complete driver stack over it. (None of the USB based WLAN
drivers need those static buffers you worry about, by the way...)
I haven't heard of USB device registers needing to be written in IRQ context. I'm not that well familiar with USB, so if you give such an example, that'd be fine.
Quoted 8 lines
Quoted 3 lines
Using preallocated buffer is not a good way, since it may well be already used by another interrupt or not yet processed by the worker thread (or tasklet, or whatever).
We would call those "driver bugs" and expect patches. :)
Well, I know one such patch ;)
Quoted 11 lines
Quoted 3 lines
One can not predict how many transfers are gonna be dropped due to "previous trransfer is being processed" problem, it depends on the system load. And though it's not a problem for touchscreen, it *will* be a problem if it were MMC, for instance.
Huh, well I've already seen one nice "mmc/sd over SPI" driver, using a slightly earlier version of the framework than is found in mm3. It's being used for root filesystems.
So demonstrably there is no problem for MMC/SD, either.
No problem... Has the driver been tested in stress conditions? If not, then I guess you can't say this for sure :)
Vitaly
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 11 lines · Vitaly Wool
Quoted 9 lines · David Brownell
Quoted 4 lines · Vitaly Wool
One cannot allocate memory in interrupt context, so the way to go is allocating it on stack, thus the buffer is not DMA-safe.
Quoted 6 lines
Quoted 4 lines
kmalloc(..., GFP_ATOMIC)is the way to allocate memory in irq context. It's done that way throughout the kernel.It's not applicable within the RT-related changes. kmalloc anyway takes mutexes, so allocationg it in interrupt context is buggy.
What RT-related changes cause this?
Quoted 1 line
*Legacy* kernel code does that but why produce a new code with that?
In this terminoligy, you are calling 2.6.15-rc5 "legacy". Which is not true.
Quoted 10 lines
Quoted 8 lines
Quoted 4 lines
Making it DMA-safe in thread that does the very message processing is a good way of overcoming this.
The rest of Linux appears to work fine without needing such mechanisms...
The rest of Linux still has a lot of bugs. Noone I guess is ready to argue that.
Huh? Please point out these bugs in the mainline tree and we will be glad to fix them.
Quoted 10 lines
Quoted 7 lines
I really fail to see why you think SPI needs that. USB isn't the only counterexample, but it's particularly relevant since both USB and SPI use asynchronous message passing over serial links ... and USB has a
rather complete driver stack over it. (None of the USB based WLAN
drivers need those static buffers you worry about, by the way...)I haven't heard of USB device registers needing to be written in IRQ context. I'm not that well familiar with USB, so if you give such an example, that'd be fine.
The USB host controller drivers routienly allocate memory in irq context as they are being asked to submit a new "packet" from a driver which was called in irq context. Lots of USB drivers also allocate buffers in irq context too.
So, please, drop this line of argument, it will not go any further.
greg k-h
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 11 lines · Greg KH
Quoted 3 lines · Vitaly Wool
I haven't heard of USB device registers needing to be written in IRQ context. I'm not that well familiar with USB, so if you give such an example, that'd be fine.
The USB host controller drivers routienly allocate memory in irq context as they are being asked to submit a new "packet" from a driver which was called in irq context. Lots of USB drivers also allocate buffers in irq context too.
So, please, drop this line of argument, it will not go any further.
I know almost nothing about SPI, so I could be completely wrong here, but I think I might have an inkling about the problem Vitaly is trying to solve. Note that I haven't actually had to make a design like this work, so I'm not intimately familiar with the issues, but I have seen designs that I believe would suffer from the following problem.
Suppose you have a chip (temp sensor for example) controlled via serial bus (I2C is what I'm familiar with, from what I know this scenario would apply to SPI too) that *also* drives an interrupt into the CPU. You want to be able to share the interrupt, so you need to be able to turn off the interrupt from IRQ context (when the driver decides that it is going to handle the interrupt). But the only way to turn off the interrupt on the peripheral chip is to send a message via the serial bus. Now if the IRQ gets entered while the serial bus is busy servicing another device, the device driver author is in trouble - she can't return from the irq handler until she's acked the IRQ and the device is no longer asserting the interrupt, but she can't ack the IRQ without sending a command on the serial bus, which she can't do because the bus is currently in use higher up the call stack.
Now one could argue that this design is broken (requiring a slow serial bus access to ack an irq means that you end up with very high-latency interrupt handlers) but it's my impression that such designs are not unheard of in the embedded world.
You end up having to leave the interrupt masked on return from the irq_handler routine, ack it at a higher level at some later point, and then re-enable it.
-andy
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 4 lines · Andy Isaacson
Now one could argue that this design is broken (requiring a slow serial bus access to ack an irq means that you end up with very high-latency interrupt handlers) but it's my impression that such designs are not unheard of in the embedded world.
Then you just atomically allocate a buffer like all of the current kernel drivers do :)
Come on people, this really isn't an issue...
thanks,
greg k-h
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
In reply to Vitaly WoolQuoted 9 lines · Vitaly Wool
Quoted 5 lines · Greg KH
What is the speed of your SPI bus?
And what are your preformance requirements?
The maximum frequency for the SPI bus is 26 MHz, WLAN driver is to work at true 10 Mbit/sec.
Then you should be fine with the copying data and memset stuff, based on the workload the rest of the kernel does for other busses which have this same requirement of DMAable buffers.
And I'm sure David will be glad to have you point out any places in his code where it accidentally takes data off of the stack instead of allocating it.
thanks,
greg k-h
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 18 lines · Greg KH
Quoted 4 lines · Vitaly Wool
Greg KH wrote:
Quoted 10 lines
Quoted 7 lines
What is the speed of your SPI bus?
And what are your preformance requirements?
The maximum frequency for the SPI bus is 26 MHz, WLAN driver is to work at true 10 Mbit/sec.
Quoted 9 lines
Then you should be fine with the copying data and memset stuff, based on the workload the rest of the kernel does for other busses which have this same requirement of DMAable buffers.
And I'm sure David will be glad to have you point out any places in his code where it accidentally takes data off of the stack instead of allocating it.
Hmm, I recall I've already posted some pieces of code with that stuff.
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
In reply to Vitaly WoolQuoted 9 lines · Vitaly Wool
Quoted 5 lines · Greg KH
What is the speed of your SPI bus?
And what are your preformance requirements?
The maximum frequency for the SPI bus is 26 MHz, WLAN driver is to work at true 10 Mbit/sec.
Some SPI flash chips are rated at 60 MHz ... there's no "official" standard placing such limits on SPI.
The various IEEE 802.15.4 (ZigBee) transceivers seem to take up to 10 MHz clocks, FWIW. USB controllers, 12 MHz (surprise! they are of course full speed, 12 MHz).
Sensors with A-to-D converters are often lower rate; only 100K samples per second, say, at maybe 12 bits each; more like 2 MHz.
- Dave
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 14 lines · David Brownell
Quoted 11 lines · Vitaly Wool
Quoted 7 lines · Greg KH
What is the speed of your SPI bus?
And what are your preformance requirements?
Quoted 8 lines
Quoted 3 lines
The maximum frequency for the SPI bus is 26 MHz, WLAN driver is to work at true 10 Mbit/sec.
Some SPI flash chips are rated at 60 MHz ... there's no "official" standard placing such limits on SPI.
David, can you please stop treating me as an idiot? Of course I meant the specific SPI bus we're working with which is quite evident from the context. Thanks.
Vitaly
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
In reply to Vitaly WoolQuoted 9 lines · Vitaly Wool
Quoted 5 lines · Greg KH
What is the speed of your SPI bus?
And what are your preformance requirements?
The maximum frequency for the SPI bus is 26 MHz, WLAN driver is to work at true 10 Mbit/sec.
My two cents: the faster is better; SPI bus itself can work on 52MHz, and AFAIK WLAN developers limit the speed due to their firmware reqiorements.
Quoted 20 lines
Vitaly
P. S. I'm speaking not about this particular case during most part of this conversation. Sound cards behind the SPI bus will suffer a lot more since it's their path to use wXrY functions (lotsa small transfers) rather than WLAN's.
-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files for problems? Stop! Download the new AJAX search engine that makes searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
spi-devel-general mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/spi-devel-general
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
In reply to Vitaly WoolQuoted 8 lines · Vitaly Wool
Quoted 5 lines
Quoted 3 lines
It's way better to just insist that all I/O buffers (in all generic APIs) be DMA-safe. AFAICT that's a pretty standard rule everywhere in Linux.
I agree.
Well, why then David doesn't insist on that in his own code? His synchronous transfer functions
You seem to be referring to one non-generic function that's documented as OK to pass DMA-unsafe buffers to. There are several other synchronous transfer calls that don't make such guarantees.
Quoted 2 lines
are allocating transfer buffers on
stack which is not DMA-safe.
I think the very first version did that, but nothing since has taken that shortcut. (Several months now.) It uses a buffer that's allocated on the heap.
Quoted 3 lines
Then he starts messing with allocate-or-use-preallocated stuff etc. etc. Why isn't he just kmalloc'ing/kfree'ing buffers each time these functions are called
So that the typical case, with little SPI contention, doesn't hit the heap? That's sure what I thought ... though I can't speak for what other people may think I thought. You were the one that wanted to optimize the atypical case to remove a blocking path!
Quoted 1 line
(as he proposes for upper layer drivers to do)?
No I didn't. I actually said that the upper layer drivers should just not use DMA-unsafe areas for I/O buffers in the first place. Places like stacks or static data. Doing that means there's never a need for a new kmalloc buffer, unless maybe you're marshaling things into a scratch buffer.
Quoted 2 lines
The thing is that only controller driver is aware whether DMA is needed or not, so it's controller driver that should work it out.
Given the policy that all code avoids DMA-unsafe areas for I/O buffers, the issue has already been worked out globally.
- Dave
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 14 lines · David Brownell
Quoted 11 lines · Vitaly Wool
Quoted 8 lines
Quoted 5 lines
It's way better to just insist that all I/O buffers (in all generic APIs) be DMA-safe. AFAICT that's a pretty standard rule everywhere in Linux.
I agree.
Well, why then David doesn't insist on that in his own code? His synchronous transfer functions
Quoted 9 lines
Quoted 3 lines
are allocating transfer buffers on stack which is not DMA-safe.
I think the very first version did that, but nothing since has taken that shortcut. (Several months now.) It uses a buffer that's allocated on the heap.
You're wrong.
Isn't it a part of your code:
static inline ssize_t spi_w8r8(struct spi_device *spi, u8 cmd)
{
ssize_t status;
u8 result;
status = spi_write_then_read(spi, &cmd, 1, &result, 1);
/* return negative errno or unsigned value */
return (status < 0) ? status : result;
}You're allocating u8 var on stack, then allocate a 1-byte-long buffer and copy the data instead of letting the controller driver decide whether this allocation/copy is necessary or not. And in 99% cases it's not gonna be necessary as any controller driver w/o brain damage will transfer this as PIO. So the overhead for sending/receiving 1 byte will be _very big_ (trylock, kmalloc, memcpy). See now what I'm talking about?
Quoted 8 lines
Quoted 7 lines
Then he starts messing with allocate-or-use-preallocated stuff etc. etc. Why isn't he just kmalloc'ing/kfree'ing buffers each time these functions are called
Quoted 6 lines
So that the typical case, with little SPI contention, doesn't hit the heap? That's sure what I thought ... though I can't speak for what other people may think I thought. You were the one that wanted to optimize the atypical case to remove a blocking path!
I meant kmalloc'ing/kfree'ing buffers is spi_w8r8/spi_w8r16/etc.
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 12 lines
static inline
ssize_tspi_w8r8(struct spi_device *spi, u8 cmd){ ssize_t status; u8 result; status = spi_write_then_read(spi, &cmd, 1, &result, 1); /* return negative errno or unsigned value */ return (status < 0) ? status : result; }
Quoted 3 lines
You're allocating u8 var on stack, then allocate a 1-byte-long buffer and copy the data instead of letting the controller driver decide whether this allocation/copy is necessary or not.
Yeah, like that matters in the face of the overhead to queue the message, get to the head of the SPI transfer queue, go through that queue, then finally wake up the task that was synchronously blocking in write_then_read(). Oh, and since that's inlined, GCC may be re-using existing state...
If folk want an "it looks simple" convenient/friendly API, there is always a price to pay. In this case, that cost is dwarfed by the mere fact that they're using a synchronous model to access shared resources (the SPI controller).
Quoted 10 lines
Quoted 8 lines
Quoted 3 lines
Then he starts messing with allocate-or-use-preallocated stuff etc. etc. Why isn't he just kmalloc'ing/kfree'ing buffers each time these functions are called
So that the typical case, with little SPI contention, doesn't hit the heap? That's sure what I thought ... though I can't speak for what other people may think I thought. You were the one that wanted to optimize the atypical case to remove a blocking path!
I meant kmalloc'ing/kfree'ing buffers is spi_w8r8/spi_w8r16/etc.
As I said: so the _typical_ case doesn't hit the heap. There are
inherent overheads for such RPC-style calls. But there's also no point in gratuitously increasing them.
- Dave
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog
Quoted 21 lines · David Brownell
Quoted 11 lines
static inline
ssize_tspi_w8r8(struct spi_device *spi, u8 cmd){ ssize_t status; u8 result; status = spi_write_then_read(spi, &cmd, 1, &result, 1); /* return negative errno or unsigned value */ return (status < 0) ? status : result; }Quoted 3 lines
You're allocating u8 var on stack, then allocate a 1-byte-long buffer and copy the data instead of letting the controller driver decide whether this allocation/copy is necessary or not.
Yeah, like that matters in the face of the overhead to queue the message, get to the head of the SPI transfer queue, go through that queue, then finally wake up the task that was synchronously blocking in
write_then_read(). Oh, and since that's inlined, GCC may be re-using existing state...
Quoted 6 lines
If folk want an "it looks simple" convenient/friendly API, there is always a price to pay. In this case, that cost is dwarfed by the mere fact that they're using a synchronous model to access shared resources (the SPI controller).
It's just words; the patch I'm proposing cuts this price to nothing but you're just being too stubborn to accept that. :(
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 7 lines
Then he starts messing with allocate-or-use-preallocated stuff etc. etc. Why isn't he just kmalloc'ing/kfree'ing buffers each time these functions are called
So that the typical case, with little SPI contention, doesn't hit the heap? That's sure what I thought ... though I can't speak for what other people may think I thought. You were the one that wanted to optimize the atypical case to remove a blocking path!
Quoted 8 lines
Quoted 3 lines
I meant kmalloc'ing/kfree'ing buffers is spi_w8r8/spi_w8r16/etc.
As I said: so the _typical_ case doesn't hit the heap. There are
inherent overheads for such RPC-style calls. But there's also no point in gratuitously increasing
Sorry, increasing what?
Okay, lemme summarize: I've spent a day working out the minimal changes I'd like to have added to your core and two days trying to convince those changes are necessary. You just don't want to listen to me. So the conclusion is that the convergence process has been deadlocked, and the only way out for us is -- we'll continue to work on our core, as there're things we'd like to have in SPI core and there're other people using our core.
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
In reply to David BrownellQuoted 8 lines · David Brownell
Neither is it "remove" in the normal sense either. The hot path never had an allocation before ... but it could easily have one now, because that sort of bounce-buffer semantic is what a
DMA_UNSAFEflag demands from the lower levels. (That's a key part part of the proposed change that's not included in this patch... making the chage look much smaller.)
Makes no sense to me, sorry. What 'not included' changes are you talking about?
Quoted 9 lines
How much of the reason you're arguing for this is because you have that WLAN stack that does some static allocation for I/O buffers? Changing that to use dynamic allocation -- the more usual style -- should be easy. But instead, you want all code in the SPI stack to need to worry about two different kinds of I/O memory: the normal stuff, and the DMA-unsafe kind. Not
Err, this change also allows to get rid of ugly stuff in write_then_read.
It also allows to keep track of memory allocation in SPI controller driver withough hacky tricks. And... it's not *all* the code; if it doesn't provide such means, it's also fine.
Quoted 4 lines
just this WLAN code which for some reason started out using a nonportable scheme for allocating I/O buffers.
I'm afraid I just can't understand what you mean by 'portable' here.
Quoted 5 lines
It'd take a lot more persuasion to make me think that's a good idea. That's the kind of subtle confusion that really promotes hard-to-find bugs in drivers, and lots of developer confusion.
What hard-to-find bugs, I wonder?
And... I'm afraid that your core is unacceptable for us w/o the changes proposed.
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
In reply to David BrownellQuoted 5 lines · David Brownell
That's not "core" in the normal "everyone must use this" sense. Even the folk that do use synchronous transfers aren't always going to use that particular codepath.
Lemme explain once more why what you have is a lot worse than what I suggest.
Take for instance spi_w8r8 function used in lotsa places in the drivers you and Stephen have posted.
This function has a) *implicit memcpy* inherited from spi_write_then_read b) *implicit priority inversion* inherited from the same place.
On the other hand, any smart enough SPI controller driver wouldn't ever want to send and receive one byte via DMA, especially chaining messages because the overhead will be great; memcpy's also adding overhead. It's gonna use PIO instead.
So this is a serious reason to let controller driver decide things related to memory allocation. It doesn't concern only static-allocated buffers but also, say, stack-allocated (used in your core in many places). And controller driver needs something to track whether it has allocated a buffer for transfer for DMA safety or not. Don't you want a linked list of allocated buffers in the controller driver?! And the flag I'm adding within the patch does that with minimal intrusion to your core.
Hope that helps understand my reasons.
Vitaly
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 5 lines · Vitaly Wool
Take for instance
spi_w8r8function used in lotsa places in the drivers you and Stephen have posted.
This function has a) *implicit memcpy* inherited fromspi_write_then_readb) *implicit priority inversion* inherited from the same place.
No, (a) is explicit, along with comments not to use that family of calls when such things matter more than the convenience of those RPC-ish calls. And (b) was fixed a small patch, now merged.
- Dave
Re: [PATCH/RFC] SPI: add DMAUNSAFE analog to David Brownell's core
Quoted 4 lines · David Brownell
On Tuesday 13 December 2005 2:15 pm, Vitaly Wool wrote:
Quoted 10 lines
Quoted 4 lines
Take for instance
spi_w8r8function used in lotsa places in the drivers you and Stephen have posted.
This function has a) *implicit memcpy* inherited fromspi_write_then_readb) *implicit priority inversion* inherited from the same place.No, (a) is explicit, along with comments not to use that family of calls when such things matter more than the convenience of those RPC-ish calls. And (b) was fixed a small patch, now merged.
Hmm. Why not use a) when it's convenient? You're placing an artificial requirement then. Yep, saw patch for b). One more kmalloc introduces which can be avoided easily with my patch ;)
Vitaly
Re: [PATCH/RFC] SPI: add async message handing library to David Brownell's core
In reply to Vitaly WoolQuoted 10 lines · Vitaly Wool
Greetings,
This thingie hasn't been thoroughly tested yet, but it's lightweight and easy to understand so I don't think solving the problems that may arise will take long. Though I haven't actually done that yet, I'm sure that Stephen's PXA SSP driver will become easier to understand and less in footprint and will work faster when it's rewritten using this library. (Yes, I do expect performance improvement here as the current implementation schedules multiple tasklets, so scheduling penalty is high.)
Is this really true? Is tasklet scheduling "harder" than kernal thread scheduling? A close look at my PXA SSP SPI implementation will reveal that my design is nearly lock-less and callable from any execution context (i.e. interrupt context).
In general, the "pump_transfer" and "pump_message" tasklets should mutually exclusive (anything else is a programming mistake). I certainly could collapse the tasklets into a single one if you think this would be better?
Quoted 17 lines
+ *
spi_queue- (internal) queue the message to be processed asynchronously
+ * @spi: SPI device to perform transfer to/from
+ * @msg: message to be sent
+ * Description:
+ * This function queues the message to SPI controller's queue.
+ */drivers/spi/spi-thread.c new file +11
-0+static int spi_queue (struct spi_device *spi,struct spi_message *msg) +{ +struct threaded_async_data *td = spi->master->context; +int rc =0 ; + +if (!td) { + rc = -EINVAL ; + goto out; + } + + msg->spi = spi;
drivers/spi/spi-thread.c line 198
Quoted 10 lines
drivers/spi/spi-thread.c new file +10
-0+down (&td->lock); +list_add_tail (&msg->queue, &td->msgs); +dev_dbg (spi->dev.parent,"message has been queued\n" ); +up (&td->lock); +wake_up_interruptible (&td->wq); + +out: +return rc; +} +
This can not be invoke this from "interrupt context" which is a requirement for my SPI devices (CS8415A, CS8405A, CS4341).
Re: [PATCH/RFC] SPI: add async message handing library to David Brownell's core
Quoted 10 lines · Stephen Street
Quoted 6 lines · Vitaly Wool
Greetings,
This thingie hasn't been thoroughly tested yet, but it's lightweight
and easy to understand so I don't think solving the problems that may arise will take long. Though I haven't actually done that yet, I'm sure that Stephen's PXA SSP driver will become easier to understand and less in footprint and will work faster when it's rewritten using this library. (Yes, I do expect performance improvement here as the current implementation schedules multiple tasklets, so scheduling penalty is high.)
Quoted 6 lines
Is this really true? Is tasklet scheduling "harder" than kernal thread scheduling? A close look at my PXA SSP SPI implementation will reveal that my design is nearly lock-less and callable from any execution context (i.e. interrupt context).
It's harder in your case because the tasklet is created each time it's scheduled again, as far as I see it in your impleemntation. Each SPI controller thread is created only once so it's more lightweight than what you do.
Quoted 24 lines
Quoted 24 lines
+ *
spi_queue- (internal) queue the message to be processed asynchronously
+ * @spi: SPI device to perform transfer to/from
+ * @msg: message to be sent
+ * Description:
+ * This function queues the message to SPI controller's queue.
+ */drivers/spi/spi-thread.c new file +18
-0+static int spi_queue (struct spi_device *spi,struct spi_message *msg) +{ +struct threaded_async_data *td = spi->master->context; +int rc =0 ; + +if (!td) { + rc = -EINVAL ; + goto out; + } + + msg->spi = spi; +down (&td->lock); +list_add_tail (&msg->queue, &td->msgs); +dev_dbg (spi->dev.parent,"message has been queued\n" ); +up (&td->lock); +wake_up_interruptible (&td->wq); + +out:
Quoted 12 lines
drivers/spi/spi-thread.c line 76
Quoted 5 lines
drivers/spi/spi-thread.c new file +3
-0+return rc; +} +This can not be invoke this from "interrupt context" which is a requirement for my SPI devices (CS8415A, CS8405A, CS4341).
Okay, not a major issue though. Change mutexes to spin_lock_irq/spin_unlock_irq and it's callable from an interrupt context, right?
Vitaly
Re: [PATCH/RFC] SPI: add async message handing library to David Brownell's core
Quoted 11 lines · Vitaly Wool
Quoted 6 lines
Is this really true? Is tasklet scheduling "harder" than kernal thread scheduling? A close look at my PXA SSP SPI implementation will reveal that my design is nearly lock-less and callable from any execution context (i.e. interrupt context).
It's harder in your case because the tasklet is created each time it's scheduled again, as far as I see it in your impleemntation. Each SPI controller thread is created only once so it's more lightweight than what you do.
I'm not sure what you mean by "create". The tasklet structures are created and initialized once in the driver probe function. I'm not an expert but I looked into the implementation (softirq.c) of tasklets and found the following design:
1) Tasklets are run by a softirq.
2) A softirq is really a kernel thread allocated on a per cpu basis.
3) A "scheduled" tasklet is simply a member of a link list maintained by the softirq thread.
My driver implementation has the following features:
1) Uses only one kernel thread for all SPI controllers.
2) Reuses existing performance tuned kernel infrastructure (i.e. tasklets)
3) Implements a low latency locking scheme for dispatching SPI transfers via tasklet's serial scheduling guarantees.
IMHO, from a system load perspective, my approach is lighter and simpler than adding a dedicated kernel thread for each SPI controller.
Stephen
Re: [PATCH/RFC] SPI: add async message handing library
In reply to Vitaly WoolQuoted 7 lines · Vitaly Wool
Greetings,
as a part of the convergence process between two SPI cores ... I'd like to post here one more result of mindwork :). ...
this is a port of the library
for handling async SPI messages using kernel threads, one per each SPI controller, from our core to David's.
I'll have to take some time to look at this; I had planned to evolve the spi_bitbang framework a bit more. It's already working (parport/pc), but needs some improvements.
Just wanted you to not expect a response Real Soon Now, since there are other things I have to do too. :)
- Dave
[PATCH/RFC] SPI: async message handing library update
In reply to Vitaly WoolThis is just an update of the library for handling async SPI messages using kernel threads, one per each SPI controller. The only change is that thread's mutex has been changed to spinlock, in order to make it possible to call transfer function from the interrupt context.
drivers/spi/Kconfig | 9 + drivers/spi/Makefile | 3 drivers/spi/spi-thread.c | 198 +++++++++++++++++++++++++++++++++++++++++ include/linux/spi/spi-thread.h | 27 +++++ include/linux/spi/spi.h | 3 5 files changed, 240 insertions(+)
drivers/spi/Kconfig +9 -0
@@ -13,6 +13,7 @@ config SPI_ARCH_HAS_MASTER default y if ARCH_AT91 default y if ARCH_OMAP default y if ARCH_PXA + default y if ARCH_PNX4008 default y if X86 # devel hack only!! (ICH7 can...) config SPI_ARCH_HAS_SLAVE
@@ -63,6 +64,14 @@ config SPI_MASTER controller and the protocol drivers for the SPI slave chips that are connected. +config SPI_THREAD + boolean "Library for threaded asynchronous message processing" + depends on SPI_MASTER + help + Choose this if you want to use thread-based asynchronous + message processing library. Using kernel threads for + asynchronous data processing is the most common option. + comment "SPI Master Controller Drivers" depends on SPI_MASTER
drivers/spi/Makefile +3 -0
@@ -10,6 +10,9 @@ endif # config declarations into driver model code obj-$(CONFIG_SPI_MASTER) += spi.o +# thread-based async message handling library +obj-$(CONFIG_SPI_THREAD) += spi-thread.o + # SPI master controller drivers (bus) obj-$(CONFIG_SPI_BITBANG) += spi_bitbang.o # ... add above this line ...
drivers/spi/spi-thread.c new file +198 -0
198 lines not on this page · raw message
include/linux/spi/spi-thread.h new file +27 -0
@@ -0,0 +1,27 @@ +/* +* linux/drivers/spi/spi-thread.h +* +* Copyright (C) 2005 MontaVista Software, Inc <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License. +* +*/ +#ifndef __SPI_THREAD_H +#define __SPI_THREAD_H + + +#if defined (CONFIG_SPI_THREAD ) +extern int spi_start_async (struct spi_master *,int (*)(struct spi_master *,struct spi_message *)); +extern void spi_stop_async (struct spi_master *); +#else +static inline int spi_start_async (struct spi_master *master,int (*xfer)(struct spi_master *,struct spi_message *)) +{ +return -EINVAL ; +} +static inline void spi_stop_async (struct spi_master *master) +{ +} +#endif /* CONFIG_SPI_THREAD */ +#endif /* __SPI_THREAD_H */
include/linux/spi/spi.h +3 -0
@@ -152,6 +152,7 @@ static inline void spi_unregister_driver* device's SPI controller; protocol code may call this. * @transfer: adds a message to the controller's transfer queue. * @cleanup: frees controller-specific state +* @context: controller-specific data * * Each SPI master controller can communicate with one or more spi_device * children. These make a small bus, sharing MOSI, MISO and SCK signals
@@ -207,6 +208,8 @@ struct spi_master {/* called on release() to free memory provided by spi_master */ void (*cleanup)(const struct spi_device *spi); + +void *context; };/* the spi driver core manages memory for the spi_master classdev */
Re: [PATCH/RFC] SPI: async message handing library update
OK, I made some time to look at that. As I've mentioned already, the "spi_bitbang" code needed to morph in this direction, and that was in fact the patch I was hoping you'd be sending. So I finished that up instead, and have sent it separately ... but I've also attached the C file to this response.
drivers/spi/spi-thread.c line 36
Quoted 5 lines
drivers/spi/spi-thread.c new file +5
-0+struct threaded_async_data { +atomic_t exiting; +struct spi_master *master; +struct task_struct *thread; +wait_queue_head_t wq;
I just kept the workqueue, named after its device. It makes the code look a lot simpler!
Quoted 12 lines
drivers/spi/spi-thread.c new file +11
-0+struct list_head msgs; +spinlock_t lock; +int (*xfer) (struct spi_master *,struct spi_message *); +}; + +/** +* spi_start_async - start the thread +* @master: SPI controller structure which the thread is related to +* @return: abstract pointer to the thread context +*/ +int spi_start_async (struct spi_master *master, int (*xfer)(struct spi_master *,
Quoted 1 line
struct
spi_message*))
I did this differently, but liked your start/stop names. :) So:
int spi_bitbang_start(struct spi_bitbang *bitbang); int spi_bitbang_stop(struct spi_bitbang *bitbang);
to start and stop processing the queue associated with the bitbanged spi_master. Callbacks just get stored in the structure; simpler, and the return value is just a normal zero-or-negative-errno.
include/linux/spi/spi.h static inline void spi_unregister_driver · line 158
Quoted 8 lines
include/linux/spi/spi.h +1
-0@@ -152,6 +152,7 @@ static inline void spi_unregister_driver* device's SPI controller; protocol code may call this. * @transfer: adds a message to the controller's transfer queue. * @cleanup: frees controller-specific state +* @context: controller-specific data * * Each SPI master controller can communicate with one or more spi_device * children. These make a small bus, sharing MOSI, MISO and SCK signals
I've not seen a need for that yet; the class_get_devdata() is doing that already. And it's now wrapped up as spi_master_get_devdata().
- Dave
/*
* spi_bitbang.c - polling/bitbanging SPI master controller driver utilities *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <linux/config.h>
#include <linux/init.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <linux/spi/spi_bitbang.h>
/*----------------------------------------------------------------------*/
/*
* FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
* Use this for GPIO or shift-register level hardware APIs.
*
* spi_bitbang_cs is in spi_device->controller_state, which is unavailable
* to glue code. These bitbang setup() and cleanup() routines are always
* used, though maybe they're called from controller-aware code. *
* chipselect() and friends may use use spi_device->controller_data and
* controller registers as appropriate.
*
*
* NOTE: SPI controller pins can often be used as GPIO pins instead,
* which means you could use a bitbang driver either to get hardware
* working quickly, or testing for differences that aren't speed related. */
struct spi_bitbang_cs {
unsigned nsecs; /* (clock cycle time)/2 */
u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
u32 word, u8 bits);
unsigned (*txrx_bufs)(struct spi_device *,
u32 (*txrx_word)(
struct spi_device *spi,
unsigned nsecs,
u32 word, u8 bits),
unsigned, struct spi_transfer *);
};static unsigned bitbang_txrx_8(
struct spi_device *spi,
u32 (*txrx_word)(struct spi_device *spi,
unsigned nsecs,
u32 word, u8 bits),
unsigned ns,
struct spi_transfer *t
) {
unsigned bits = spi->bits_per_word;
unsigned count = t->len;
const u8 *tx = t->tx_buf;
u8 *rx = t->rx_buf;
while (likely(count > 0)) {
u8 word = 0;
if (tx)
word = *tx++;
word = txrx_word(spi, ns, word, bits);
if (rx)
*rx++ = word;
count -= 1;
}
return t->len - count;
}static unsigned bitbang_txrx_16(
struct spi_device *spi,
u32 (*txrx_word)(struct spi_device *spi,
unsigned nsecs,
u32 word, u8 bits),
unsigned ns,
struct spi_transfer *t
) {
unsigned bits = spi->bits_per_word;
unsigned count = t->len;
const u16 *tx = t->tx_buf;
u16 *rx = t->rx_buf;
while (likely(count > 1)) {
u16 word = 0;
if (tx)
word = *tx++;
word = txrx_word(spi, ns, word, bits);
if (rx)
*rx++ = word;
count -= 2;
}
return t->len - count;
}static unsigned bitbang_txrx_32(
struct spi_device *spi,
u32 (*txrx_word)(struct spi_device *spi,
unsigned nsecs,
u32 word, u8 bits),
unsigned ns,
struct spi_transfer *t
) {
unsigned bits = spi->bits_per_word;
unsigned count = t->len;
const u32 *tx = t->tx_buf;
u32 *rx = t->rx_buf;
while (likely(count > 3)) {
u32 word = 0;
if (tx)
word = *tx++;
word = txrx_word(spi, ns, word, bits);
if (rx)
*rx++ = word;
count -= 4;
}
return t->len - count;
}/**
* spi_bitbang_setup - default setup for per-word I/O loops */
int spi_bitbang_setup(struct spi_device *spi)
{
struct spi_bitbang_cs *cs = spi->controller_state;
struct spi_bitbang *bitbang;
if (!cs) {
cs = kzalloc(sizeof *cs, SLAB_KERNEL);
if (!cs)
return -ENOMEM;
spi->controller_state = cs;
}
bitbang = spi_master_get_devdata(spi->master);
if (!spi->bits_per_word)
spi->bits_per_word = 8;
/* spi_transfer level calls that work per-word */
if (spi->bits_per_word <= 8)
cs->txrx_bufs = bitbang_txrx_8;
else if (spi->bits_per_word <= 16)
cs->txrx_bufs = bitbang_txrx_16;
else if (spi->bits_per_word <= 32)
cs->txrx_bufs = bitbang_txrx_32;
else
return -EINVAL;
/* per-word shift register access, in hardware or bitbanging */
cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
if (!cs->txrx_word)
return -EINVAL;
if (!spi->max_speed_hz)
spi->max_speed_hz = 500 * 1000;
/* nsecs = max(50, (clock period)/2), be optimistic */
cs->nsecs = (1000000000/2) / (spi->max_speed_hz);
if (cs->nsecs < 50)
cs->nsecs = 50;
if (cs->nsecs > MAX_UDELAY_MS * 1000)
return -EINVAL;
dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n",
__FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
spi->bits_per_word, 2 * cs->nsecs);
/* NOTE we _need_ to call chipselect() early, ideally with adapter
* setup, unless the hardware defaults cooperate to avoid confusion
* between normal (active low) and inverted chipselects.
*/
/* deselect chip (low or high) */
spin_lock(&bitbang->lock);
if (!bitbang->busy) {
bitbang->chipselect(spi, 0);
ndelay(cs->nsecs);
}
spin_unlock(&bitbang->lock);
return 0;
}EXPORT_SYMBOL_GPL(spi_bitbang_setup);
/**
* spi_bitbang_cleanup - default cleanup for per-word I/O loops */
void spi_bitbang_cleanup(const struct spi_device *spi)
{
kfree(spi->controller_state);
}EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
{
struct spi_bitbang_cs *cs = spi->controller_state;
unsigned nsecs = cs->nsecs;
return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t);
}/*----------------------------------------------------------------------*/
/*
* SECOND PART ... simple transfer queue runner.
*
* This costs a task context per controller, running the queue by
* performing each transfer in sequence. Smarter hardware can queue
* several DMA transfers at once, and process several controller queues
* in parallel; this driver doesn't match such hardware very well. *
* Drivers can provide word-at-a-time i/o primitives, or provide
* transfer-at-a-time ones to leverage dma or fifo hardware. */
static void bitbang_work(void *_bitbang)
{
struct spi_bitbang *bitbang = _bitbang;
unsigned long flags;
spin_lock_irqsave(&bitbang->lock, flags);
bitbang->busy = 1;
while (!list_empty(&bitbang->queue)) {
struct spi_message *m;
struct spi_device *spi;
unsigned nsecs;
struct spi_transfer *t;
unsigned tmp;
unsigned chipselect;
int status;
m = container_of(bitbang->queue.next, struct spi_message,
queue);
list_del_init(&m->queue);
spin_unlock_irqrestore(&bitbang->lock, flags);// FIXME this is made-up
nsecs = 100;
spi = m->spi;
t = m->transfers;
tmp = 0;
chipselect = 0;
status = 0;
for (;;t++) {
if (bitbang->shutdown) {
status = -ESHUTDOWN;
break;
}
/* set up default clock polarity, and activate chip */
if (!chipselect) {
bitbang->chipselect(spi, 1);
ndelay(nsecs);
}
if (!t->tx_buf && !t->rx_buf && t->len) {
status = -EINVAL;
break;
}
/* transfer data */
if (t->len) {
/* FIXME if bitbang->use_dma, dma_map_single()
* before the transfer, and dma_unmap_single()
* afterwards, for either or both buffers...
*/
status = bitbang->txrx_bufs(spi, t);
}
if (status != t->len) {
if (status > 0)
status = -EMSGSIZE;
break;
}
m->actual_length += status;
status = 0;
/* protocol tweaks before next transfer */
if (t->delay_usecs)
udelay(t->delay_usecs);
tmp++;
if (tmp >= m->n_transfer)
break;
chipselect = !t->cs_change;
if (chipselect);
continue;
bitbang->chipselect(spi, 0);
/* REVISIT do we want the udelay here instead? */
msleep(1);
}
tmp = m->n_transfer - 1;
tmp = m->transfers[tmp].cs_change;
m->status = status;
m->complete(m->context);
ndelay(2 * nsecs);
bitbang->chipselect(spi, status == 0 && tmp);
ndelay(nsecs);
spin_lock_irqsave(&bitbang->lock, flags);
}
bitbang->busy = 0;
spin_unlock_irqrestore(&bitbang->lock, flags);
}/**
* spi_bitbang_transfer - default submit to transfer queue */
int spi_bitbang_transfer(struct spi_device *spi, struct spi_message *m)
{
struct spi_bitbang *bitbang;
unsigned long flags;
m->actual_length = 0;
m->status = -EINPROGRESS;
bitbang = spi_master_get_devdata(spi->master);
if (bitbang->shutdown)
return -ESHUTDOWN;
spin_lock_irqsave(&bitbang->lock, flags);
list_add_tail(&m->queue, &bitbang->queue);
queue_work(bitbang->workqueue, &bitbang->work);
spin_unlock_irqrestore(&bitbang->lock, flags);
return 0;
}EXPORT_SYMBOL_GPL(spi_bitbang_transfer);
/*----------------------------------------------------------------------*/
/**
* spi_bitbang_start - start up a polled/bitbanging SPI master driver
* @bitbang: driver handle
*
* Caller should have zero-initialized all parts of the structure, and then
* provided callbacks for chip selection and I/O loops. If the master has
* a transfer method, its final step should call spi_bitbang_transfer; or,
* that's the default if the transfer routine is not initialized. It should
* also set up the bus number and number of chipselects.
*
* For i/o loops, provide callbacks either per-word (for bitbanging, or for
* hardware that basically exposes a shift register) or per-spi_transfer
* (which takes better advantage of hardware like fifos or DMA engines). *
* Drivers using per-word I/O loops should use (or call) spi_bitbang_setup and
* spi_bitbang_cleanup to handle those spi master methods. Those methods are
* the defaults if the bitbang->txrx_bufs routine isn't initialized. *
* This routine registers the spi_master, which will process requests in a
* dedicated task, keeping IRQs unblocked most of the time. To stop
* processing those requests, call spi_bitbang_stop().
*/
int spi_bitbang_start(struct spi_bitbang *bitbang)
{
int status;
if (!bitbang->master || !bitbang->chipselect)
return -EINVAL;
INIT_WORK(&bitbang->work, bitbang_work, bitbang);
spin_lock_init(&bitbang->lock);
INIT_LIST_HEAD(&bitbang->queue);
if (!bitbang->master->transfer)
bitbang->master->transfer = spi_bitbang_transfer;
if (!bitbang->txrx_bufs) {
bitbang->use_dma = 0;
bitbang->txrx_bufs = spi_bitbang_bufs;
if (!bitbang->master->setup) {
bitbang->master->setup = spi_bitbang_setup;
bitbang->master->cleanup = spi_bitbang_cleanup;
}
} else if (!bitbang->master->setup)
return -EINVAL;
/* this task is the only thing to touch the SPI bits */
bitbang->busy = 0;
bitbang->workqueue = create_singlethread_workqueue(
bitbang->master->cdev.dev->bus_id);
if (bitbang->workqueue == NULL) {
status = -EBUSY;
goto err1;
}
/* driver may get busy before register() returns, especially
* if someone registered boardinfo for devices
*/
status = spi_register_master(bitbang->master);
if (status < 0)
goto err2;
return status;err2:
destroy_workqueue(bitbang->workqueue); err1: return status; }
EXPORT_SYMBOL_GPL(spi_bitbang_start);
/**
* spi_bitbang_stop - stops the task providing spi communication */
int spi_bitbang_stop(struct spi_bitbang *bitbang)
{
unsigned limit = 500;
spin_lock_irq(&bitbang->lock);
bitbang->shutdown = 0;
while (!list_empty(&bitbang->queue) && limit--) {
spin_unlock_irq(&bitbang->lock);
dev_dbg(bitbang->master->cdev.dev, "wait for queue\n");
msleep(10);
spin_lock_irq(&bitbang->lock);
}
spin_unlock_irq(&bitbang->lock);
if (!list_empty(&bitbang->queue)) {
dev_err(bitbang->master->cdev.dev, "queue didn't empty\n");
return -EBUSY;
}
destroy_workqueue(bitbang->workqueue);
spi_unregister_master(bitbang->master);
return 0;
}EXPORT_SYMBOL_GPL(spi_bitbang_stop);
MODULE_LICENSE("GPL");
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: async message handing library update
Quoted 9 lines · David Brownell
chipselect = !t->cs_change;
if (chipselect);
continue;
bitbang->chipselect(spi, 0);
/* REVISIT do we want the udelay here instead?
*/
msleep(1);Ohhh. Have you intentionally put the semicolon after "if (chipselect)" ?
Re: [spi-devel-general] Re: [PATCH/RFC] SPI: async message handing library update
Quoted 5 lines · dmitry pervushin
Quoted 4 lines · David Brownell
chipselect = !t->cs_change; if (chipselect); continue;
Quoted 7 lines
Quoted 5 lines
bitbang->chipselect(spi, 0); /* REVISIT do we want the udelay here instead? */ msleep(1);Ohhh. Have you intentionally put the semicolon after "if (chipselect)" ?
Nope. Good catch ... haven't had a chance to try that yet with that driver; not all drivers actually need to drop chipselect like that.
Any other comments about that approach to the "let someone else manage the queue"? This one's more clearly "implementation utilities" than what you had seemed to be talking about before, but I think it does address a point where I think we were agreeing: it should be easy for people to whip together a controller driver by providing just some lower level I/O calls.
- Dave
Re: [PATCH/RFC] SPI: async message handing library update
In reply to David BrownellHi David --
just a cuple of notes here and below...
General one: how is it supposed to set SPI bus clock in this model? I guess that the only option is to set it in txrx_*.
That is not optimal since it means setting clock for each transfer which is not an optimal solution, better have a function (bitbang->set_clock or whatever) )to set clock per message.
Quoted 9 lines
if (!spi->max_speed_hz) spi->max_speed_hz = 500 * 1000; /* nsecs = max(50, (clock period)/2), be optimistic */ cs->nsecs = (1000000000/2) / (spi->max_speed_hz); if (cs->nsecs < 50) cs->nsecs = 50;
Suggest not to hardcode values here.
Quoted 6 lines
/* set up default clock polarity, and activate chip */
if (!chipselect) {
bitbang->chipselect(spi, 1);
ndelay(nsecs);Suggest special enum/define for chipselect value.
Quoted 5 lines
/* protocol tweaks before next transfer */ if (t->delay_usecs) udelay(t->delay_usecs);
Suggest nsecs here as well.
Generic note: haven't tested that with DMA, will have more comments prolly...
Another one: I just feel comfortabel with using 'bitbang' term for the variety of SPI stuff which this library suits.
Vitaly
Re: [PATCH/RFC] SPI: async message handing library update
Quoted 6 lines · Vitaly Wool
Hi David --
just a cuple of notes here and below...
General one: how is it supposed to set SPI bus clock in this model? I guess that the only option is to set it in txrx_*.
No, as I said separately: the standard spi_master.setup() call does this. Things are arranged so that drivers using this code can splice in their own code for it ... true bitbangers won't, because the delays in the I/O loops suffice. But drivers using "word-at-a-time" controllers (hardware-assisted shift registers!) will, as will ones that do "buffer-at-a-time" I/O (e.g. to stuff/unstuff fifos, or set up non-queued DMA).
Quoted 10 lines
Quoted 9 lines
if (!spi->max_speed_hz) spi->max_speed_hz = 500 * 1000; /* nsecs = max(50, (clock period)/2), be optimistic */ cs->nsecs = (1000000000/2) / (spi->max_speed_hz); if (cs->nsecs < 50) cs->nsecs = 50;Suggest not to hardcode values here.
I suppose it'd make sense to just fail if max_speed_hz is invalid, and if there's some board that an bitbang at over 10MHz we should avoid getting in its way.
Quoted 7 lines
Quoted 6 lines
/* set up default clock polarity, and activate chip */ if (!chipselect) { bitbang->chipselect(spi, 1); ndelay(nsecs);Suggest special enum/define for chipselect value.
Added. Instead of "1", "BITBANG_CS_ACTIVE" ... which will normally pull the nCS line low (after setting clock to match CPOL).
Quoted 6 lines
Quoted 5 lines
/* protocol tweaks before next transfer */ if (t->delay_usecs) udelay(t->delay_usecs);Suggest nsecs here as well.
The relevant chip delays seem to be specified in usecs ... I don't like using nsecs for the clock timings, but without doing that it'd be impractical to define rates at the levels the hardware actually uses. There are still some "nsec" leakages out of the real-bitbang code up to the next level, fixable over time.
Quoted 7 lines
Quoted 4 lines
/* FIXME if
bitbang->use_dma,dma_map_single()
* before the transfer, anddma_unmap_single()
* afterwards, for either or both buffers... */please *please* *_please_*!!! don't do it! :)
Let the controller driver do it *only in case it's not working in PIO!*
OK. That'd be more work for the controller driver, but you're right that a lot of the drivers using these utilities are rather likely to be PIO-oriented. If they want DMA speedups, they can do the mappings themselves (in cases where the driver didn't do them already).
Quoted 2 lines
Another one: I just feel comfortabel with using 'bitbang' term for the variety of SPI stuff which this library suits.
You _do_ feel comfortable with it? I actually feel a bit odd, since only one of the three driver types is really bitbanging. And in fact it still bothers me that the other two tie down a task, but that's the price for reusing common infrastructure.
- Dave
Re: [PATCH/RFC] SPI: async message handing library update
Quoted 11 lines · David Brownell
Quoted 11 lines
Quoted 11 lines
if (!spi->max_speed_hz) spi->max_speed_hz = 500 * 1000; /* nsecs = max(50, (clock period)/2), be optimistic */ cs->nsecs = (1000000000/2) / (spi->max_speed_hz); if (cs->nsecs < 50) cs->nsecs = 50;
Quoted 9 lines
Quoted 3 lines
Suggest not to hardcode values here.
I suppose it'd make sense to just fail if
max_speed_hzis invalid, and if there's some board that an bitbang at over 10MHz we should avoid getting in its way.
Well, why do this?
Quoted 11 lines
Quoted 10 lines
Quoted 7 lines
/* protocol tweaks before next transfer */ if (t->delay_usecs) udelay(t->delay_usecs);Suggest nsecs here as well.
Quoted 8 lines
The relevant chip delays seem to be specified in usecs ... I don't like using nsecs for the clock timings, but without doing that it'd be impractical to define rates at the levels the hardware actually uses. There are still some "nsec" leakages out of the real-bitbang code up to the next level, fixable over time.
Ok.
Quoted 14 lines
Quoted 13 lines
Quoted 9 lines
/* FIXME if
bitbang->use_dma,dma_map_single()
* before the transfer, anddma_unmap_single()
* afterwards, for either or both buffers... */please *please* *_please_*!!! don't do it! :)
Let the controller driver do it *only in case it's not working in PIO!*
Quoted 7 lines
OK. That'd be more work for the controller driver, but you're right that a lot of the drivers using these utilities are rather likely to be PIO-oriented. If they want DMA speedups, they can do the mappings themselves (in cases where the driver didn't do them already).
Agreed :)
Quoted 13 lines
Quoted 6 lines
Another one: I just feel comfortabel with using 'bitbang' term for the variety of SPI stuff which this library suits.
You _do_ feel comfortable with it? I actually feel a bit odd, since only one of the three driver types is really bitbanging. And in fact it still bothers me that the other two tie down a task, but that's the price for reusing common infrastructure.
Oh sorry, of course I meant "I just don't feel comfortable..."
BTW: the message handling is one per-transfer basis for bitbang. But in this case it's not possible to imlement chained DMA transfers (2 channels, one for Rx, one for Tx, basically that's your sample use case :)
Vitaly
Re: [PATCH/RFC] SPI: async message handing library update
Quoted 4 lines
BTW: the message handling is one per-transfer basis for bitbang. But in this case it's not possible to imlement chained DMA transfers (2 channels, one for Rx, one for Tx, basically that's your sample use case :)
This library code is intended to help folk get some functional and correct drivers quickly ... with "chained DMA" support being explicitly a non-goal.
If you want a top performing driver, you'd go about it differently ... you would handle all the transfers directly, and not use library code like this. Every SOC seems to have its own preferred way to do DMA chaining, so likely the driver would just implement the three spi_master methods directly and map most spi_message objects into single SOC-specific DMA chains.
- Dave
Re: [PATCH/RFC] SPI: async message handing library update
In reply to David BrownellDavid,
Quoted 7 lines
if (t->len) {
/* FIXME if bitbang->use_dma, dma_map_single()
* before the transfer, and dma_unmap_single()
* afterwards, for either or both buffers...
*/please *please* *_please_*!!! don't do it! :)
Let the controller driver do it *only in case it's not working in PIO!*
Vitaly