* Re: Question on supporting multiple HW versions with a single driver (warning: long post)
From: Bruce_Leonard @ 2011-02-08 2:37 UTC (permalink / raw)
To: David Gibson; +Cc: linuxppc-dev
In-Reply-To: <20110208021616.GO30616@yookeroo>
>
> There are a number of drivers which already have this sort of dual bus
> binding.
>
Thanks for the feedback David, I appreciate it. Could you point me to one
of those drivers that has "this sort of dual bus binding" so can see an
example of what I'm trying to do?
Bruce
^ permalink raw reply
* RE: [PATCH][v1] RTC driver(Linux) for PT7C4338 chip.
From: Jain Priyanka-B32167 @ 2011-02-08 4:31 UTC (permalink / raw)
To: Alessandro Zummo, Paul Gortmaker, rtc-linux@googlegroups.com,
linuxppc-dev@lists.ozlabs.org
Cc: Gupta Maneesh-B18878, Aggrwal Poonam-B10812, Gala Kumar-B11780,
Jain Priyanka-B32167
Hi,
Please review and acknowledge this patch.
Regards
Priyanka
> -----Original Message-----
> From: Jain Priyanka-B32167
> Sent: Monday, November 22, 2010 9:24 AM
> To: 'Alessandro Zummo'
> Cc: 'rtc-linux@googlegroups.com'
> Subject: FW: [PATCH][v1] RTC driver(Linux) for PT7C4338 chip.
>=20
> Hi,
>=20
> Please review and acknowledge this patch.
>=20
> Thanks and Regards
> Priyanka
>=20
>=20
>=20
> -----Original Message-----
> From: Jain Priyanka-B32167
> Sent: Thursday, November 11, 2010 9:36 AM
> To: Jain Priyanka-B32167; rtc-linux@googlegroups.com; linuxppc-
> dev@lists.ozlabs.org
> Subject: RE: [PATCH][v1] RTC driver(Linux) for PT7C4338 chip.
>=20
> Hi,
>=20
> I am waiting for review comments and acknowledgement that this patch will
> be pick up for open source.
> Please acknowledge.
>=20
> Regards
> Priyanka Jain
>=20
> -----Original Message-----
> From: Jain Priyanka-B32167
> Sent: Tuesday, October 26, 2010 3:12 PM
> To: rtc-linux@googlegroups.com; linuxppc-dev@lists.ozlabs.org
> Cc: Jain Priyanka-B32167
> Subject: [PATCH][v1] RTC driver(Linux) for PT7C4338 chip.
>=20
> PT7C4338 chip is being manufactured by Pericom Technology Inc.
> It is a serial real-time clock which provides:
> 1)Low-power clock/calendar.
> 2)Programmable square-wave output.
> It has 56 bytes of nonvolatile RAM.
>=20
> Signed-off-by: Priyanka Jain <Priyanka.Jain@freescale.com>
> ---
> PT7C4338 RTC driver is verified on Freescale P1010RDB.
> Changes for v1:
> Incoperated Kumar Gala's review comments
> -Removing board name from commit messages.
>=20
> drivers/rtc/Kconfig | 9 ++
> drivers/rtc/Makefile | 1 +
> drivers/rtc/rtc-pt7c4338.c | 215
> ++++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 225 insertions(+), 0 deletions(-) create mode 100644
> drivers/rtc/rtc-pt7c4338.c
>=20
> diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index
> 10ba12c..6ff0901 100644
> --- a/drivers/rtc/Kconfig
> +++ b/drivers/rtc/Kconfig
> @@ -324,6 +324,15 @@ config RTC_DRV_RX8025
> This driver can also be built as a module. If so, the module
> will be called rtc-rx8025.
>=20
> +config RTC_DRV_PT7C4338
> + tristate "Pericom Technology Inc. PT7C4338 RTC"
> + help
> + If you say yes here you get support for the Pericom Technology
> + Inc. PT7C4338 RTC chip.
> +
> + This driver can also be built as a module. If so, the module
> + will be called rtc-pt7c4338.
> +
> endif # I2C
>=20
> comment "SPI RTC drivers"
> diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile index
> 5adbba7..4014607 100644
> --- a/drivers/rtc/Makefile
> +++ b/drivers/rtc/Makefile
> @@ -70,6 +70,7 @@ obj-$(CONFIG_RTC_DRV_PCF50633) +=3D rtc-pcf50633.o
> obj-$(CONFIG_RTC_DRV_PL030) +=3D rtc-pl030.o
> obj-$(CONFIG_RTC_DRV_PL031) +=3D rtc-pl031.o
> obj-$(CONFIG_RTC_DRV_PS3) +=3D rtc-ps3.o
> +obj-$(CONFIG_RTC_DRV_PT7C4338) +=3D rtc-pt7c4338.o
> obj-$(CONFIG_RTC_DRV_PXA) +=3D rtc-pxa.o
> obj-$(CONFIG_RTC_DRV_R9701) +=3D rtc-r9701.o
> obj-$(CONFIG_RTC_DRV_RP5C01) +=3D rtc-rp5c01.o
> diff --git a/drivers/rtc/rtc-pt7c4338.c b/drivers/rtc/rtc-pt7c4338.c new
> file mode 100644 index 0000000..fca52cd
> --- /dev/null
> +++ b/drivers/rtc/rtc-pt7c4338.c
> @@ -0,0 +1,215 @@
> +/*
> + * Copyright 2010 Freescale Semiconductor, Inc.
> + *
> + * Author: Priyanka Jain <Priyanka.Jain@freescale.com>
> + *
> + * See file CREDITS for list of people who contributed to this
> + * project.
> + *
> + * 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
> + */
> +
> +/*
> + * This file provides Date & Time support (no alarms) for PT7C4338 chip.
> + *
> + * This file is based on drivers/rtc/rtc-ds1307.c
> + *
> + * PT7C4338 chip is manufactured by Pericom Technology Inc.
> + * It is a serial real-time clock which provides
> + * 1)Low-power clock/calendar.
> + * 2)Programmable square-wave output.
> + * It has 56 bytes of nonvolatile RAM.
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/i2c.h>
> +#include <linux/rtc.h>
> +#include <linux/bcd.h>
> +
> +/* RTC register addresses */
> +#define PT7C4338_REG_SECONDS 0x00
> +#define PT7C4338_REG_MINUTES 0x01
> +#define PT7C4338_REG_HOURS 0x02
> +#define PT7C4338_REG_AMPM 0x02
> +#define PT7C4338_REG_DAY 0x03
> +#define PT7C4338_REG_DATE 0x04
> +#define PT7C4338_REG_MONTH 0x05
> +#define PT7C4338_REG_YEAR 0x06
> +#define PT7C4338_REG_CTRL_STAT 0x07
> +
> +/* RTC second register address bit */
> +#define PT7C4338_SEC_BIT_CH 0x80 /*Clock Halt (in Register
> 0)*/
> +
> +/* RTC control and status register bits */
> +#define PT7C4338_CTRL_STAT_BIT_RS0 0x1 /*Rate select 0*/
> +#define PT7C4338_CTRL_STAT_BIT_RS1 0x2 /*Rate select 1*/
> +#define PT7C4338_CTRL_STAT_BIT_SQWE 0x10 /*Square Wave Enable*/
> +#define PT7C4338_CTRL_STAT_BIT_OSF 0x20 /*Oscillator Stop Flag*/
> +#define PT7C4338_CTRL_STAT_BIT_OUT 0x80 /*Output Level Control*/
> +
> +static const struct i2c_device_id pt7c4338_id[] =3D {
> + { "pt7c4338", 0 },
> + { }
> +};
> +MODULE_DEVICE_TABLE(i2c, pt7c4338_id);
> +
> +struct pt7c4338{
> + struct i2c_client *client;
> + struct rtc_device *rtc;
> +};
> +
> +static int pt7c4338_read_time(struct device *dev, struct rtc_time
> +*time) {
> + struct i2c_client *client =3D to_i2c_client(dev);
> + int ret;
> + u8 buf[7];
> + u8 year, month, day, hour, minute, second;
> + u8 week, twelve_hr, am_pm;
> +
> + ret =3D i2c_smbus_read_i2c_block_data(client,
> + PT7C4338_REG_SECONDS, 7, buf);
> + if (ret < 0)
> + return ret;
> + if (ret < 7)
> + return -EIO;
> +
> + second =3D buf[0];
> + minute =3D buf[1];
> + hour =3D buf[2];
> + week =3D buf[3];
> + day =3D buf[4];
> + month =3D buf[5];
> + year =3D buf[6];
> +
> + /* Extract additional information for AM/PM */
> + twelve_hr =3D hour & 0x40;
> + am_pm =3D hour & 0x20;
> +
> + /* Write to rtc_time structure */
> + time->tm_sec =3D bcd2bin(second & 0x7f);
> + time->tm_min =3D bcd2bin(minute & 0x7f);
> + if (twelve_hr) {
> + /* Convert to 24 hr */
> + if (am_pm)
> + time->tm_hour =3D bcd2bin(hour & 0x10) + 12;
> + else
> + time->tm_hour =3D bcd2bin(hour & 0xBF);
> + } else {
> + time->tm_hour =3D bcd2bin(hour);
> + }
> +
> + time->tm_wday =3D bcd2bin(week & 0x07) - 1;
> + time->tm_mday =3D bcd2bin(day & 0x3f);
> + time->tm_mon =3D bcd2bin(month & 0x1F) - 1;
> + /* assume 20YY not 19YY */
> + time->tm_year =3D bcd2bin(year) + 100;
> +
> + return 0;
> +}
> +
> +static int pt7c4338_set_time(struct device *dev, struct rtc_time *time)
> +{
> + struct i2c_client *client =3D to_i2c_client(dev);
> + u8 buf[7];
> +
> + /* Extract time from rtc_time and load into pt7c4338*/
> + buf[0] =3D bin2bcd(time->tm_sec);
> + buf[1] =3D bin2bcd(time->tm_min);
> + buf[2] =3D bin2bcd(time->tm_hour);
> + buf[3] =3D bin2bcd(time->tm_wday + 1); /* Day of the week */
> + buf[4] =3D bin2bcd(time->tm_mday); /* Date */
> + buf[5] =3D bin2bcd(time->tm_mon + 1);
> +
> + /* assume 20YY not 19YY */
> + if (time->tm_year >=3D 100)
> + buf[6] =3D bin2bcd(time->tm_year - 100);
> + else
> + buf[6] =3D bin2bcd(time->tm_year);
> +
> + return i2c_smbus_write_i2c_block_data(client,
> + PT7C4338_REG_SECONDS, 7, buf);
> +}
> +
> +static const struct rtc_class_ops pt7c4338_rtc_ops =3D {
> + .read_time =3D pt7c4338_read_time,
> + .set_time =3D pt7c4338_set_time,
> +};
> +
> +static int pt7c4338_probe(struct i2c_client *client,
> + const struct i2c_device_id *id)
> +{
> + struct pt7c4338 *pt7c4338;
> + struct i2c_adapter *adapter =3D to_i2c_adapter(client->dev.parent);
> + int ret;
> +
> + pt7c4338 =3D kzalloc(sizeof(struct pt7c4338), GFP_KERNEL);
> + if (!pt7c4338)
> + return -ENOMEM;
> +
> + pt7c4338->client =3D client;
> + i2c_set_clientdata(client, pt7c4338);
> + pt7c4338->rtc =3D rtc_device_register(client->name, &client->dev,
> + &pt7c4338_rtc_ops, THIS_MODULE);
> + if (IS_ERR(pt7c4338->rtc)) {
> + ret =3D PTR_ERR(pt7c4338->rtc);
> + dev_err(&client->dev, "unable to register the class
> device\n");
> + goto out_free;
> + }
> +
> + return 0;
> +out_free:
> + i2c_set_clientdata(client, NULL);
> + kfree(pt7c4338);
> + return ret;
> +}
> +
> +static int __devexit pt7c4338_remove(struct i2c_client *client) {
> + struct pt7c4338 *pt7c4338 =3D i2c_get_clientdata(client);
> +
> + rtc_device_unregister(pt7c4338->rtc);
> + i2c_set_clientdata(client, NULL);
> + kfree(pt7c4338);
> + return 0;
> +}
> +
> +static struct i2c_driver pt7c4338_driver =3D {
> + .driver =3D {
> + .name =3D "rtc-pt7c4338",
> + .owner =3D THIS_MODULE,
> + },
> + .probe =3D pt7c4338_probe,
> + .remove =3D __devexit_p(pt7c4338_remove),
> + .id_table =3D pt7c4338_id,
> +};
> +
> +static int __init pt7c4338_init(void)
> +{
> + return i2c_add_driver(&pt7c4338_driver); }
> +
> +static void __exit pt7c4338_exit(void)
> +{
> + i2c_del_driver(&pt7c4338_driver);
> +}
> +
> +module_init(pt7c4338_init);
> +module_exit(pt7c4338_exit);
> +
> +MODULE_AUTHOR("Priyanka Jain <Priyanka.Jain@freescale.com>");
> +MODULE_DESCRIPTION("pericom Technology Inc. PT7C4338 RTC Driver");
> +MODULE_LICENSE("GPL");
> --
> 1.6.5.6
^ permalink raw reply
* Re: Question on supporting multiple HW versions with a single driver (warning: long post)
From: Bill Gatliff @ 2011-02-08 5:13 UTC (permalink / raw)
To: Bruce_Leonard; +Cc: linuxppc-dev, David Gibson
In-Reply-To: <OF0A8BBBE2.4D143142-ON88257831.000E1C77-88257831.000E6FD6@selinc.com>
Guys:
I think the Silicon Motion SM501 driver might provide a useful
example, since the chip comes in both memory-mapped and PCI versions.
Unfortunately the chip is implemented as a multi-function driver
(mfd), so the code is not un-complicated. Still fairly
straightforward and well-written once you learn your way around it,
though.
Basically, it implements a core set of functionality to talk to the
actual chip registers, which is bus-agnostic. Then the bus-specific
drivers use these functions when they actually want to touch the chip
itself. In other words, exactly what David suggested.
b.g.
On Mon, Feb 7, 2011 at 8:37 PM, <Bruce_Leonard@selinc.com> wrote:
>>
>> There are a number of drivers which already have this sort of dual bus
>> binding.
>>
>
> Thanks for the feedback David, I appreciate it. =A0Could you point me to =
one
> of those drivers that has "this sort of dual bus binding" so can see an
> example of what I'm trying to do?
>
> Bruce
> _______________________________________________
> Linuxppc-dev mailing list
> Linuxppc-dev@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/linuxppc-dev
>
--=20
Bill Gatliff
bgat@billgatliff.com
^ permalink raw reply
* Re: [PATCH 1/2] misc: add CARMA DATA-FPGA Access Driver
From: Dmitry Torokhov @ 2011-02-08 7:33 UTC (permalink / raw)
To: Ira W. Snyder; +Cc: linuxppc-dev, linux-kernel
In-Reply-To: <1297121021-3522-2-git-send-email-iws@ovro.caltech.edu>
Hi Ira,
On Mon, Feb 07, 2011 at 03:23:40PM -0800, Ira W. Snyder wrote:
> This driver allows userspace to access the data processing FPGAs on the
> OVRO CARMA board. It has two modes of operation:
>
> 1) random access
>
> This allows users to poke any DATA-FPGA registers by using mmap to map
> the address region directly into their memory map.
>
> 2) correlation dumping
>
> When correlating, the DATA-FPGA's have special requirements for getting
> the data out of their memory before the next correlation. This nominally
> happens at 64Hz (every 15.625ms). If the data is not dumped before the
> next correlation, data is lost.
>
> The data dumping driver handles buffering up to 1 second worth of
> correlation data from the FPGAs. This lowers the realtime scheduling
> requirements for the userspace process reading the device.
Kind of a fly-by review but it looks like the locking in the driver
needs work.
>
> Signed-off-by: Ira W. Snyder <iws@ovro.caltech.edu>
> ---
> drivers/misc/Kconfig | 1 +
> drivers/misc/Makefile | 1 +
> drivers/misc/carma/Kconfig | 9 +
> drivers/misc/carma/Makefile | 1 +
> drivers/misc/carma/carma-fpga.c | 1446 +++++++++++++++++++++++++++++++++++++++
> 5 files changed, 1458 insertions(+), 0 deletions(-)
> create mode 100644 drivers/misc/carma/Kconfig
> create mode 100644 drivers/misc/carma/Makefile
> create mode 100644 drivers/misc/carma/carma-fpga.c
>
> diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
> index 4d073f1..f457f14 100644
> --- a/drivers/misc/Kconfig
> +++ b/drivers/misc/Kconfig
> @@ -457,5 +457,6 @@ source "drivers/misc/eeprom/Kconfig"
> source "drivers/misc/cb710/Kconfig"
> source "drivers/misc/iwmc3200top/Kconfig"
> source "drivers/misc/ti-st/Kconfig"
> +source "drivers/misc/carma/Kconfig"
>
> endif # MISC_DEVICES
> diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
> index 98009cc..2c1610e 100644
> --- a/drivers/misc/Makefile
> +++ b/drivers/misc/Makefile
> @@ -42,3 +42,4 @@ obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
> obj-$(CONFIG_PCH_PHUB) += pch_phub.o
> obj-y += ti-st/
> obj-$(CONFIG_AB8500_PWM) += ab8500-pwm.o
> +obj-y += carma/
> diff --git a/drivers/misc/carma/Kconfig b/drivers/misc/carma/Kconfig
> new file mode 100644
> index 0000000..4be183f
> --- /dev/null
> +++ b/drivers/misc/carma/Kconfig
> @@ -0,0 +1,9 @@
> +config CARMA_FPGA
> + tristate "CARMA DATA-FPGA Access Driver"
> + depends on FSL_SOC && PPC_83xx && MEDIA_SUPPORT && HAS_DMA && FSL_DMA
> + select VIDEOBUF_DMA_SG
> + default n
> + help
> + Say Y here to include support for communicating with the data
> + processing FPGAs on the OVRO CARMA board.
> +
> diff --git a/drivers/misc/carma/Makefile b/drivers/misc/carma/Makefile
> new file mode 100644
> index 0000000..0b69fa7
> --- /dev/null
> +++ b/drivers/misc/carma/Makefile
> @@ -0,0 +1 @@
> +obj-$(CONFIG_CARMA_FPGA) += carma-fpga.o
> diff --git a/drivers/misc/carma/carma-fpga.c b/drivers/misc/carma/carma-fpga.c
> new file mode 100644
> index 0000000..52620b3
> --- /dev/null
> +++ b/drivers/misc/carma/carma-fpga.c
> @@ -0,0 +1,1446 @@
> +/*
> + * CARMA DATA-FPGA Access Driver
> + *
> + * Copyright (c) 2009-2010 Ira W. Snyder <iws@ovro.caltech.edu>
> + *
> + * 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.
> + */
> +
> +/*
> + * FPGA Memory Dump Format
> + *
> + * FPGA #0 control registers (32 x 32-bit words)
> + * FPGA #1 control registers (32 x 32-bit words)
> + * FPGA #2 control registers (32 x 32-bit words)
> + * FPGA #3 control registers (32 x 32-bit words)
> + * SYSFPGA control registers (32 x 32-bit words)
> + * FPGA #0 correlation array (NUM_CORL0 correlation blocks)
> + * FPGA #1 correlation array (NUM_CORL1 correlation blocks)
> + * FPGA #2 correlation array (NUM_CORL2 correlation blocks)
> + * FPGA #3 correlation array (NUM_CORL3 correlation blocks)
> + *
> + * Each correlation array consists of:
> + *
> + * Correlation Data (2 x NUM_LAGSn x 32-bit words)
> + * Pipeline Metadata (2 x NUM_METAn x 32-bit words)
> + * Quantization Counters (2 x NUM_QCNTn x 32-bit words)
> + *
> + * The NUM_CORLn, NUM_LAGSn, NUM_METAn, and NUM_QCNTn values come from
> + * the FPGA configuration registers. They do not change once the FPGA's
> + * have been programmed, they only change on re-programming.
> + */
> +
> +/*
> + * Basic Description:
> + *
> + * This driver is used to capture correlation spectra off of the four data
> + * processing FPGAs. The FPGAs are often reprogrammed at runtime, therefore
> + * this driver supports dynamic enable/disable of capture while the device
> + * remains open.
> + *
> + * The nominal capture rate is 64Hz (every 15.625ms). To facilitate this fast
> + * capture rate, all buffers are pre-allocated to avoid any potentially long
> + * running memory allocations while capturing.
> + *
> + * There are three lists which are used to keep track of the different states
> + * of data buffers.
> + *
> + * 1) free list
> + * This list holds all empty data buffers which are ready to receive data.
> + *
> + * 2) inflight list
> + * This list holds data buffers which are currently waiting for a DMA operation
> + * to complete.
> + *
> + * 3) used list
> + * This list holds data buffers which have been filled, and are waiting to be
> + * read by userspace.
> + *
> + * All buffers start life on the free list, then move successively to the
> + * inflight list, and then to the used list. After they have been read by
> + * userspace, they are moved back to the free list. The cycle repeats as long
> + * as necessary.
> + */
> +
> +/*
> + * Notes on the IRQ masking scheme:
> + *
> + * The IRQ masking scheme here is different than most other hardware. The only
> + * way for the DATA-FPGAs to detect if the kernel has taken too long to copy
> + * the data is if the status registers are not cleared before the next
> + * correlation data dump is ready.
> + *
> + * The interrupt line is connected to the status registers, such that when they
> + * are cleared, the interrupt is de-asserted. Therein lies our problem. We need
> + * to schedule a long-running DMA operation and return from the interrupt
> + * handler quickly, but we cannot clear the status registers.
> + *
> + * To handle this, the system controller FPGA has the capability to connect the
> + * interrupt line to a user-controlled GPIO pin. This pin is driven high
> + * (unasserted) and left that way. To mask the interrupt, we change the
> + * interrupt source to the GPIO pin. Tada, we hid the interrupt. :)
> + */
> +
> +#include <linux/of_platform.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/miscdevice.h>
> +#include <linux/interrupt.h>
> +#include <linux/dmaengine.h>
> +#include <linux/highmem.h>
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/poll.h>
> +#include <linux/init.h>
> +#include <linux/slab.h>
> +#include <linux/io.h>
> +
> +#include <media/videobuf-dma-sg.h>
> +
> +/* system controller registers */
> +#define SYS_IRQ_SOURCE_CTL 0x24
> +#define SYS_IRQ_OUTPUT_EN 0x28
> +#define SYS_IRQ_OUTPUT_DATA 0x2C
> +#define SYS_IRQ_INPUT_DATA 0x30
> +
> +/* GPIO IRQ line assignment */
> +#define IRQ_CORL_DONE 0x10
> +
> +/* FPGA registers */
> +#define MMAP_REG_VERSION 0x00
> +#define MMAP_REG_CORL_CONF1 0x08
> +#define MMAP_REG_CORL_CONF2 0x0C
> +#define MMAP_REG_STATUS 0x48
> +
> +#define SYS_FPGA_BLOCK 0xF0000000
> +
> +static const char drv_name[] = "carma-fpga";
> +
> +#define NUM_FPGA 4
> +
> +#define MIN_DATA_BUFS 8
> +#define MAX_DATA_BUFS 64
> +
> +struct fpga_info {
> + unsigned int num_lag_ram;
> + unsigned int blk_size;
> +};
> +
> +struct data_buf {
> + struct list_head entry;
> + struct videobuf_dmabuf vb;
> + bool mapped;
> + size_t size;
> +};
> +
> +struct fpga_device {
> + struct miscdevice miscdev;
> + struct device *dev;
> + struct mutex mutex;
> +
> + /* FPGA registers and information */
> + struct fpga_info info[NUM_FPGA];
> + void __iomem *regs;
> + int irq;
> +
> + /* FPGA Physical Address/Size Information */
> + resource_size_t phys_addr;
> + size_t phys_size;
> +
> + /* DMA structures */
> + struct sg_table corl_table;
> + unsigned int corl_nents;
> + struct dma_chan *chan;
> +
> + /* Protection for all members below */
> + spinlock_t lock;
> +
> + /* Device enable/disable flag */
> + bool enabled;
> +
> + /* Correlation data buffers */
> + wait_queue_head_t wait;
> + struct list_head free;
> + struct list_head used;
> + struct list_head inflight;
> +
> + /* Information about data buffers */
> + unsigned int num_dropped;
> + unsigned int num_buffers;
> + size_t bufsize;
> +};
> +
> +struct fpga_reader {
> + struct fpga_device *priv;
> + struct data_buf *buf;
> + off_t buf_start;
> +};
> +
> +#define inode_to_dev(inode) container_of(inode->i_cdev, struct fpga_device, cdev)
> +
> +/*
> + * Data Buffer Allocation Helpers
> + */
> +
> +static int data_map_buffer(struct device *dev, struct data_buf *buf)
> +{
> + int ret;
> +
> + /* if the buffer is already mapped, we're done */
> + if (buf->mapped)
> + return 0;
> +
This is a local function, not library. Can't we keep track whether a
buffer is mapped or not?
> + ret = videobuf_dma_map(dev, &buf->vb);
> + if (ret)
> + return ret;
> +
> + buf->mapped = true;
> + return 0;
> +}
> +
> +static void data_unmap_buffer(struct device *dev, struct data_buf *buf)
> +{
> + /* the buffer is already unmapped, we're done */
> + if (!buf->mapped)
> + return;
> +
> + videobuf_dma_unmap(dev, &buf->vb);
> + buf->mapped = false;
> +}
> +
> +/**
> + * data_free_buffer() - free a single data buffer and all allocated memory
> + * @dev: the DMA device to map for
> + * @buf: the buffer to free
> + *
> + * This will free all of the pages allocated to the given data buffer, and
> + * then free the structure itself
> + */
> +static void data_free_buffer(struct device *dev, struct data_buf *buf)
> +{
> + /* It is ok to free a NULL buffer */
> + if (!buf)
> + return;
> +
> + /* Make sure the buffer is not on any list */
> + list_del_init(&buf->entry);
And what happens if it is? Should it be WARN_ON(!list_empty()) instead?
> +
> + /* unmap it for DMA */
> + data_unmap_buffer(dev, buf);
> +
> + /* free all memory */
> + videobuf_dma_free(&buf->vb);
> + kfree(buf);
> +}
> +
> +/**
> + * data_alloc_buffer() - allocate and fill a data buffer with pages
> + * @dev: the DMA device to map for
> + * @bytes: the number of bytes required
> + *
> + * This allocates all space needed for a data buffer, and gets it ready to be
> + * used in a DMA transaction. It only needs to be used, never mapped before
> + * use. This avoids calling vmalloc in hardirq context.
> + *
> + * Returns NULL on failure
> + */
> +static struct data_buf *data_alloc_buffer(struct device *dev, const size_t bytes)
> +{
> + unsigned int nr_pages;
> + struct data_buf *buf;
> + int ret;
> +
> + /* calculate the number of pages necessary */
> + nr_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
> +
> + /* allocate the buffer structure */
> + buf = kzalloc(sizeof(*buf), GFP_KERNEL);
> + if (!buf)
> + goto out_return;
> +
> + /* initialize internal fields */
> + INIT_LIST_HEAD(&buf->entry);
> + buf->size = bytes;
> +
> + /* allocate the videobuf */
> + videobuf_dma_init(&buf->vb);
> + ret = videobuf_dma_init_kernel(&buf->vb, DMA_FROM_DEVICE, nr_pages);
> + if (ret)
> + goto out_free_buf;
> +
> + /* map it for DMA */
> + ret = data_map_buffer(dev, buf);
> + if (ret)
> + goto out_free_videobuf;
> +
> + return buf;
> +
> +out_free_videobuf:
> + videobuf_dma_free(&buf->vb);
> +out_free_buf:
> + kfree(buf);
> +out_return:
> + return NULL;
> +}
> +
> +/**
> + * data_free_buffers() - free all allocated buffers
> + * @priv: the driver's private data structure
> + *
> + * Free all buffers allocated by the driver (except those currently in the
> + * process of being read by userspace).
> + *
> + * LOCKING: must hold dev->mutex
> + * CONTEXT: user
> + */
> +static void data_free_buffers(struct fpga_device *priv)
> +{
> + struct data_buf *buf, *tmp;
> +
> + spin_lock_irq(&priv->lock);
> + BUG_ON(!list_empty(&priv->inflight));
> +
> + list_for_each_entry_safe(buf, tmp, &priv->free, entry) {
> + list_del_init(&buf->entry);
> + spin_unlock_irq(&priv->lock);
> + data_free_buffer(priv->dev, buf);
> + spin_lock_irq(&priv->lock);
> + }
This is messed up. If there is concurrent access to the free list then
it is not safe to continue iterating list after releasing the lock, you
need to do:
spin_lock_irq(&priv->lock);
while (!list_empty(&priv->free)) {
buf = list_first_entry(&priv->free, struct data_buf, entry);
list_del_init(&buf->entry);
spin_unlock_irq(&priv->lock);
data_free_buffer(priv->dev, buf);
spin_lock_irq(&priv->lock);
}
BUT, the function is only called when you disable (or fail to enable) device
which, at this point, should be quiesced, thus all this locking is not
really needed.
> +
> + list_for_each_entry_safe(buf, tmp, &priv->used, entry) {
> + list_del_init(&buf->entry);
> + spin_unlock_irq(&priv->lock);
> + data_free_buffer(priv->dev, buf);
> + spin_lock_irq(&priv->lock);
> + }
> +
> + priv->num_buffers = 0;
> + priv->bufsize = 0;
> +
> + spin_unlock_irq(&priv->lock);
> +}
> +
> +/**
> + * data_alloc_buffers() - allocate 1 seconds worth of data buffers
> + * @priv: the driver's private data structure
> + *
> + * Allocate enough buffers for a whole second worth of data
> + *
> + * This routine will attempt to degrade nicely by succeeding even if a full
> + * second worth of data buffers could not be allocated, as long as a minimum
> + * number were allocated. In this case, it will print a message to the kernel
> + * log.
> + *
> + * CONTEXT: user
> + * LOCKING: must hold dev->mutex
> + *
> + * Returns 0 on success, -ERRNO otherwise
> + */
> +static int data_alloc_buffers(struct fpga_device *priv)
> +{
> + struct data_buf *buf;
> + int i;
> +
> + for (i = 0; i < MAX_DATA_BUFS; i++) {
> + buf = data_alloc_buffer(priv->dev, priv->bufsize);
> + if (!buf)
> + break;
> +
> + spin_lock_irq(&priv->lock);
> + list_add_tail(&buf->entry, &priv->free);
> + spin_unlock_irq(&priv->lock);
Again, can someone be accessing this list aleady?
> + }
> +
> + /* Make sure we allocated the minimum required number of buffers */
> + if (i < MIN_DATA_BUFS) {
> + dev_err(priv->dev, "Unable to allocate enough data buffers\n");
> + data_free_buffers(priv);
> + return -ENOMEM;
> + }
> +
> + /* Warn if we are running in a degraded state, but do not fail */
> + if (i < MAX_DATA_BUFS) {
> + dev_warn(priv->dev, "Unable to allocate one second worth of "
> + "buffers, using %d buffers instead\n", i);
> + }
> +
> + priv->num_buffers = i;
> + return 0;
> +}
> +
> +/*
> + * DMA Operations Helpers
> + */
> +
> +/**
> + * fpga_start_addr() - get the physical address a DATA-FPGA
> + * @priv: the driver's private data structure
> + * @fpga: the DATA-FPGA number (zero based)
> + */
> +static dma_addr_t fpga_start_addr(struct fpga_device *priv, unsigned int fpga)
> +{
> + return priv->phys_addr + 0x400000 + (0x80000 * fpga);
> +}
> +
> +/**
> + * fpga_block_addr() - get the physical address of a correlation data block
> + * @priv: the driver's private data structure
> + * @fpga: the DATA-FPGA number (zero based)
> + * @blknum: the correlation block number (zero based)
> + */
> +static dma_addr_t fpga_block_addr(struct fpga_device *priv, unsigned int fpga,
> + unsigned int blknum)
> +{
> + return fpga_start_addr(priv, fpga) + (0x10000 * (1 + blknum));
> +}
> +
> +#define REG_BLOCK_SIZE (32 * 4)
> +
> +/**
> + * data_setup_corl_table() - create the scatterlist for correlation dumps
> + * @priv: the driver's private data structure
> + *
> + * Create the scatterlist for transferring a correlation dump from the
> + * DATA FPGAs. This structure will be reused for each buffer than needs
> + * to be filled with correlation data.
> + *
> + * Returns 0 on success, -ERRNO otherwise
> + */
> +static int data_setup_corl_table(struct fpga_device *priv)
> +{
> + struct sg_table *table = &priv->corl_table;
> + struct scatterlist *sg;
> + struct fpga_info *info;
> + int i, j, ret;
> +
> + /* Calculate the number of entries needed */
> + priv->corl_nents = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
> + for (i = 0; i < NUM_FPGA; i++)
> + priv->corl_nents += priv->info[i].num_lag_ram;
> +
> + /* Allocate the scatterlist table */
> + ret = sg_alloc_table(table, priv->corl_nents, GFP_KERNEL);
> + if (ret) {
> + dev_err(priv->dev, "unable to allocate DMA table\n");
> + return ret;
> + }
> +
> + /* Add the DATA FPGA registers to the scatterlist */
> + sg = table->sgl;
> + for (i = 0; i < NUM_FPGA; i++) {
> + sg_dma_address(sg) = fpga_start_addr(priv, i);
> + sg_dma_len(sg) = REG_BLOCK_SIZE;
> + sg = sg_next(sg);
> + }
> +
> + /* Add the SYS-FPGA registers to the scatterlist */
> + sg_dma_address(sg) = SYS_FPGA_BLOCK;
> + sg_dma_len(sg) = REG_BLOCK_SIZE;
> + sg = sg_next(sg);
> +
> + /* Add the FPGA correlation data blocks to the scatterlist */
> + for (i = 0; i < NUM_FPGA; i++) {
> + info = &priv->info[i];
> + for (j = 0; j < info->num_lag_ram; j++) {
> + sg_dma_address(sg) = fpga_block_addr(priv, i, j);
> + sg_dma_len(sg) = info->blk_size;
> + sg = sg_next(sg);
> + }
> + }
> +
> + /*
> + * All physical addresses and lengths are present in the structure
> + * now. It can be reused for every FPGA DATA interrupt
> + */
> + return 0;
> +}
> +
> +/*
> + * FPGA Register Access Helpers
> + */
> +
> +static void fpga_write_reg(struct fpga_device *priv, unsigned int fpga,
> + unsigned int reg, u32 val)
> +{
> + iowrite32be(val, priv->regs + 0x400000 + (fpga * 0x80000) + reg);
> +}
> +
> +static u32 fpga_read_reg(struct fpga_device *priv, unsigned int fpga,
> + unsigned int reg)
> +{
> + return ioread32be(priv->regs + 0x400000 + (fpga * 0x80000) + reg);
> +}
> +
> +/**
> + * data_calculate_bufsize() - calculate the data buffer size required
> + * @priv: the driver's private data structure
> + *
> + * Calculate the total buffer size needed to hold a single block
> + * of correlation data
> + *
> + * CONTEXT: user
> + *
> + * Returns 0 on success, -ERRNO otherwise
> + */
> +static int data_calculate_bufsize(struct fpga_device *priv)
> +{
> + u32 num_corl, num_lags, num_meta, num_qcnt, num_pack;
> + u32 conf1, conf2, version;
> + u32 num_lag_ram, blk_size;
> + int i;
> +
> + /* Each buffer starts with the 5 FPGA register areas */
> + priv->bufsize = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
> +
> + /* Read and store the configuration data for each FPGA */
> + for (i = 0; i < NUM_FPGA; i++) {
> + version = fpga_read_reg(priv, i, MMAP_REG_VERSION);
> + conf1 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF1);
> + conf2 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF2);
> +
> + /* minor version 2 and later */
> + if ((version & 0x000000FF) >= 2) {
> + num_corl = (conf1 & 0x000000F0) >> 4;
> + num_pack = (conf1 & 0x00000F00) >> 8;
> + num_lags = (conf1 & 0x00FFF000) >> 12;
> + num_meta = (conf1 & 0x7F000000) >> 24;
> + num_qcnt = (conf2 & 0x00000FFF) >> 0;
> + } else {
> + num_corl = (conf1 & 0x000000F0) >> 4;
> + num_pack = 1; /* implied */
> + num_lags = (conf1 & 0x000FFF00) >> 8;
> + num_meta = (conf1 & 0x7FF00000) >> 20;
> + num_qcnt = (conf2 & 0x00000FFF) >> 0;
> + }
> +
> + num_lag_ram = (num_corl + num_pack - 1) / num_pack;
> + blk_size = ((num_pack * num_lags) + num_meta + num_qcnt) * 8;
> +
> + priv->info[i].num_lag_ram = num_lag_ram;
> + priv->info[i].blk_size = blk_size;
> + priv->bufsize += num_lag_ram * blk_size;
> +
> + dev_dbg(priv->dev, "FPGA %d NUM_CORL: %d\n", i, num_corl);
> + dev_dbg(priv->dev, "FPGA %d NUM_PACK: %d\n", i, num_pack);
> + dev_dbg(priv->dev, "FPGA %d NUM_LAGS: %d\n", i, num_lags);
> + dev_dbg(priv->dev, "FPGA %d NUM_META: %d\n", i, num_meta);
> + dev_dbg(priv->dev, "FPGA %d NUM_QCNT: %d\n", i, num_qcnt);
> + dev_dbg(priv->dev, "FPGA %d BLK_SIZE: %d\n", i, blk_size);
> + }
> +
> + dev_dbg(priv->dev, "TOTAL BUFFER SIZE: %zu bytes\n", priv->bufsize);
> + return 0;
> +}
> +
> +/*
> + * Interrupt Handling
> + */
> +
> +/**
> + * data_disable_interrupts() - stop the device from generating interrupts
> + * @priv: the driver's private data structure
> + *
> + * Hide interrupts by switching to GPIO interrupt source
> + *
> + * LOCKING: must hold dev->lock
> + */
> +static void data_disable_interrupts(struct fpga_device *priv)
> +{
> + /* hide the interrupt by switching the IRQ driver to GPIO */
> + iowrite32be(0x2F, priv->regs + SYS_IRQ_SOURCE_CTL);
> +}
> +
> +/**
> + * data_enable_interrupts() - allow the device to generate interrupts
> + * @priv: the driver's private data structure
> + *
> + * Unhide interrupts by switching to the FPGA interrupt source. At the
> + * same time, clear the DATA-FPGA status registers.
> + *
> + * LOCKING: must hold dev->lock
> + */
> +static void data_enable_interrupts(struct fpga_device *priv)
> +{
> + /* clear the actual FPGA corl_done interrupt */
> + fpga_write_reg(priv, 0, MMAP_REG_STATUS, 0x0);
> + fpga_write_reg(priv, 1, MMAP_REG_STATUS, 0x0);
> + fpga_write_reg(priv, 2, MMAP_REG_STATUS, 0x0);
> + fpga_write_reg(priv, 3, MMAP_REG_STATUS, 0x0);
> +
> + /* flush the writes */
> + fpga_read_reg(priv, 0, MMAP_REG_STATUS);
> +
> + /* switch back to the external interrupt source */
> + iowrite32be(0x3F, priv->regs + SYS_IRQ_SOURCE_CTL);
> +}
> +
> +/**
> + * data_dma_cb() - DMAEngine callback for DMA completion
> + * @data: the driver's private data structure
> + *
> + * Complete a DMA transfer from the DATA-FPGA's
> + *
> + * This is called via the DMA callback mechanism, and will handle moving the
> + * completed DMA transaction to the used list, and then wake any processes
> + * waiting for new data
> + *
> + * CONTEXT: any, softirq expected
> + */
> +static void data_dma_cb(void *data)
> +{
> + struct fpga_device *priv = data;
> + struct data_buf *buf;
> + unsigned long flags;
> +
> + spin_lock_irqsave(&priv->lock, flags);
> +
> + /* clear the FPGA status and re-enable interrupts */
> + data_enable_interrupts(priv);
> +
> + /* If the inflight list is empty, we've got a bug */
> + BUG_ON(list_empty(&priv->inflight));
> +
> + /* Grab the first buffer from the inflight list */
> + buf = list_first_entry(&priv->inflight, struct data_buf, entry);
> + list_del_init(&buf->entry);
> +
> + /* Add it to the used list */
> + list_add_tail(&buf->entry, &priv->used);
> +
> + spin_unlock_irqrestore(&priv->lock, flags);
> +
> + /* We've changed both the inflight and used lists, so we need
> + * to wake up any processes that are blocking for those events */
> + wake_up(&priv->wait);
> +}
> +
> +/**
> + * data_submit_dma() - prepare and submit the required DMA to fill a buffer
> + * @priv: the driver's private data structure
> + * @buf: the data buffer
> + *
> + * Prepare and submit the necessary DMA transactions to fill a correlation
> + * data buffer.
> + *
> + * LOCKING: must hold dev->lock
> + * CONTEXT: hardirq only
> + *
> + * Returns 0 on success, -ERRNO otherwise
> + */
> +static int data_submit_dma(struct fpga_device *priv, struct data_buf *buf)
> +{
> + struct scatterlist *dst_sg, *src_sg;
> + unsigned int dst_nents, src_nents;
> + struct dma_chan *chan = priv->chan;
> + struct dma_async_tx_descriptor *tx;
> + dma_cookie_t cookie;
> + dma_addr_t dst, src;
> +
> + dst_sg = buf->vb.sglist;
> + dst_nents = buf->vb.sglen;
> +
> + src_sg = priv->corl_table.sgl;
> + src_nents = priv->corl_nents;
> +
> + /*
> + * All buffers passed to this function should be ready and mapped
> + * for DMA already. Therefore, we don't need to do anything except
> + * submit it to the Freescale DMA Engine for processing
> + */
> +
> + /* setup the scatterlist to scatterlist transfer */
> + tx = chan->device->device_prep_dma_sg(chan,
> + dst_sg, dst_nents,
> + src_sg, src_nents,
> + 0);
> + if (!tx) {
> + dev_err(priv->dev, "unable to prep scatterlist DMA\n");
> + return -ENOMEM;
> + }
> +
> + /* submit the transaction to the DMA controller */
> + cookie = tx->tx_submit(tx);
> + if (dma_submit_error(cookie)) {
> + dev_err(priv->dev, "unable to submit scatterlist DMA\n");
> + return -ENOMEM;
> + }
> +
> + /* Prepare the re-read of the SYS-FPGA block */
> + dst = sg_dma_address(dst_sg) + (NUM_FPGA * REG_BLOCK_SIZE);
> + src = SYS_FPGA_BLOCK;
> + tx = chan->device->device_prep_dma_memcpy(chan, dst, src,
> + REG_BLOCK_SIZE,
> + DMA_PREP_INTERRUPT);
> + if (!tx) {
> + dev_err(priv->dev, "unable to prep SYS-FPGA DMA\n");
> + return -ENOMEM;
> + }
> +
> + /* Setup the callback */
> + tx->callback = data_dma_cb;
> + tx->callback_param = priv;
> +
> + /* submit the transaction to the DMA controller */
> + cookie = tx->tx_submit(tx);
> + if (dma_submit_error(cookie)) {
> + dev_err(priv->dev, "unable to submit SYS-FPGA DMA\n");
> + return -ENOMEM;
> + }
> +
> + return 0;
> +}
> +
> +#define CORL_DONE 0x1
> +#define CORL_ERR 0x2
> +
> +static irqreturn_t data_irq(int irq, void *dev_id)
> +{
> + struct fpga_device *priv = dev_id;
> + struct data_buf *buf;
> + u32 status;
> + int i;
> +
> + /* detect spurious interrupts via FPGA status */
> + for (i = 0; i < 4; i++) {
> + status = fpga_read_reg(priv, i, MMAP_REG_STATUS);
> + if (!(status & (CORL_DONE | CORL_ERR))) {
> + dev_err(priv->dev, "spurious irq detected (FPGA)\n");
> + return IRQ_NONE;
> + }
> + }
> +
> + /* detect spurious interrupts via raw IRQ pin readback */
> + status = ioread32be(priv->regs + SYS_IRQ_INPUT_DATA);
> + if (status & IRQ_CORL_DONE) {
> + dev_err(priv->dev, "spurious irq detected (IRQ)\n");
> + return IRQ_NONE;
> + }
> +
> + spin_lock(&priv->lock);
> +
> + /* hide the interrupt by switching the IRQ driver to GPIO */
> + data_disable_interrupts(priv);
> +
> + /* Check that we actually have a free buffer */
> + if (list_empty(&priv->free)) {
> + priv->num_dropped++;
> + data_enable_interrupts(priv);
> + goto out_unlock;
> + }
> +
> + buf = list_first_entry(&priv->free, struct data_buf, entry);
> + list_del_init(&buf->entry);
> +
> + /* Check the buffer size */
> + BUG_ON(buf->size != priv->bufsize);
> +
> + /* Submit a DMA transfer to get the correlation data */
> + if (data_submit_dma(priv, buf)) {
> + dev_err(priv->dev, "Unable to setup DMA transfer\n");
> + list_add_tail(&buf->entry, &priv->free);
> + data_enable_interrupts(priv);
> + goto out_unlock;
> + }
> +
> + /* DMA setup succeeded, GO!!! */
> + list_add_tail(&buf->entry, &priv->inflight);
> + dma_async_memcpy_issue_pending(priv->chan);
> +
> +out_unlock:
> + spin_unlock(&priv->lock);
> + return IRQ_HANDLED;
> +}
> +
> +/*
> + * Realtime Device Enable Helpers
> + */
> +
> +/**
> + * data_device_enable() - enable the device for buffered dumping
> + * @priv: the driver's private data structure
> + *
> + * Enable the device for buffered dumping. Allocates buffers and hooks up
> + * the interrupt handler. When this finishes, data will come pouring in.
> + *
> + * LOCKING: must hold dev->mutex
> + * CONTEXT: user context only
> + *
> + * Returns 0 on success, -ERRNO otherwise
> + */
> +static int data_device_enable(struct fpga_device *priv)
> +{
> + u32 val;
> + int ret;
> +
> + /* multiple enables are safe: they do nothing */
> + if (priv->enabled)
> + return 0;
> +
> + /* check that the FPGAs are programmed */
> + val = ioread32be(priv->regs + 0x44);
> + if (!(val & (1 << 18))) {
> + dev_err(priv->dev, "DATA-FPGAs are not enabled\n");
> + return -ENODATA;
> + }
> +
> + /* read the FPGAs to calculate the buffer size */
> + ret = data_calculate_bufsize(priv);
> + if (ret) {
> + dev_err(priv->dev, "unable to calculate buffer size\n");
> + goto out_error;
> + }
> +
> + /* allocate the correlation data buffers */
> + ret = data_alloc_buffers(priv);
> + if (ret) {
> + dev_err(priv->dev, "unable to allocate buffers\n");
> + goto out_error;
> + }
> +
> + /* setup the source scatterlist for dumping correlation data */
> + ret = data_setup_corl_table(priv);
> + if (ret) {
> + dev_err(priv->dev, "unable to setup correlation DMA table\n");
> + goto out_error;
> + }
> +
> + /* switch to the external FPGA IRQ line */
> + data_enable_interrupts(priv);
> +
> + /* hookup the irq handler */
> + ret = request_irq(priv->irq, data_irq, IRQF_SHARED, drv_name, priv);
> + if (ret) {
> + dev_err(priv->dev, "unable to request IRQ handler\n");
> + goto out_error;
> + }
> +
> + /* success, we're enabled */
> + priv->enabled = true;
> + return 0;
> +
> +out_error:
> + sg_free_table(&priv->corl_table);
> + priv->corl_nents = 0;
> +
> + data_free_buffers(priv);
> + return ret;
> +}
> +
> +/**
> + * data_device_disable() - disable the device for buffered dumping
> + * @priv: the driver's private data structure
> + *
> + * Disable the device for buffered dumping. Stops new DMA transactions from
> + * being generated, waits for all outstanding DMA to complete, and then frees
> + * all buffers.
> + *
> + * LOCKING: must hold dev->mutex
> + * CONTEXT: user only
> + *
> + * Returns 0 on success, -ERRNO otherwise
> + */
> +static int data_device_disable(struct fpga_device *priv)
> +{
> + struct list_head *list;
> + int ret;
> +
> + /* allow multiple disable */
> + if (!priv->enabled)
> + return 0;
> +
> + /* switch to the internal GPIO IRQ line */
> + data_disable_interrupts(priv);
> +
> + /* unhook the irq handler */
> + free_irq(priv->irq, priv);
> +
> + /* wait for all outstanding DMA to complete */
> + list = &priv->inflight;
> +
> + spin_lock_irq(&priv->lock);
> + while (!list_empty(list)) {
> + spin_unlock_irq(&priv->lock);
> +
> + ret = wait_event_interruptible(priv->wait, list_empty(list));
> + if (ret)
> + return -ERESTARTSYS;
> +
> + spin_lock_irq(&priv->lock);
> + }
> + spin_unlock_irq(&priv->lock);
Locking is not needed - if you disable interrupyts what would put more
stuff on the list?
> +
> + /* free the correlation table */
> + sg_free_table(&priv->corl_table);
> + priv->corl_nents = 0;
> +
> + /* free all of the buffers */
> + data_free_buffers(priv);
> + priv->enabled = false;
> + return 0;
> +}
> +
> +/*
> + * SYSFS Attributes
> + */
> +
> +/*
> + * Count the number of entries in the given list
> + */
> +static unsigned int list_num_entries(struct list_head *list)
> +{
> + struct list_head *entry;
> + unsigned int ret = 0;
> +
> + list_for_each(entry, list)
> + ret++;
> +
> + return ret;
> +}
> +
> +static ssize_t data_num_buffers_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + unsigned int num;
> +
> + spin_lock_irq(&priv->lock);
> + num = priv->num_buffers;
> + spin_unlock_irq(&priv->lock);
This spin lock is pointless, priv->num_buffers might be already changed
here, you can't guarantee that you show accurate data.
> +
> + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> +}
> +
> +static ssize_t data_bufsize_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + size_t num;
> +
> + spin_lock_irq(&priv->lock);
> + num = priv->bufsize;
> + spin_unlock_irq(&priv->lock);
Same here.
> +
> + return snprintf(buf, PAGE_SIZE, "%zu\n", num);
> +}
> +
> +static ssize_t data_inflight_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + unsigned int num;
> +
> + spin_lock_irq(&priv->lock);
> + num = list_num_entries(&priv->inflight);
> + spin_unlock_irq(&priv->lock);
And here.
> +
> + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> +}
> +
> +static ssize_t data_free_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + unsigned int num;
> +
> + spin_lock_irq(&priv->lock);
> + num = list_num_entries(&priv->free);
> + spin_unlock_irq(&priv->lock);
> +
And here.
> + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> +}
> +
> +static ssize_t data_used_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + unsigned int num;
> +
> + spin_lock_irq(&priv->lock);
> + num = list_num_entries(&priv->used);
> + spin_unlock_irq(&priv->lock);
> +
Ditto.
> + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> +}
> +
> +static ssize_t data_num_dropped_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + unsigned int num;
> +
> + spin_lock_irq(&priv->lock);
> + num = priv->num_dropped;
> + spin_unlock_irq(&priv->lock);
> +
Yep..
> + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> +}
> +
> +static ssize_t data_en_show(struct device *dev, struct device_attribute *attr,
> + char *buf)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + ssize_t count;
> +
> + if (mutex_lock_interruptible(&priv->mutex))
> + return -ERESTARTSYS;
> +
> + count = snprintf(buf, PAGE_SIZE, "%u\n", priv->enabled);
> + mutex_unlock(&priv->mutex);
By the time buf gets all the way to userspace, yep you guessed it...
> + return count;
> +}
> +
> +static ssize_t data_en_set(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct fpga_device *priv = dev_get_drvdata(dev);
> + unsigned long enable;
> + int ret;
> +
> + ret = strict_strtoul(buf, 0, &enable);
> + if (ret) {
> + dev_err(priv->dev, "unable to parse enable input\n");
> + return -EINVAL;
> + }
> +
> + if (mutex_lock_interruptible(&priv->mutex))
> + return -ERESTARTSYS;
Why don't
error = mutex_lock_interruptible(&priv->mutex);
if (error)
return error;
- do not clobber perfectly valid error codes.
> +
> + if (enable)
> + ret = data_device_enable(priv);
> + else
> + ret = data_device_disable(priv);
> +
> + if (ret) {
> + dev_err(priv->dev, "device %s failed\n",
> + enable ? "enable" : "disable");
> + count = ret;
> + goto out_unlock;
> + }
> +
> +out_unlock:
> + mutex_unlock(&priv->mutex);
> + return count;
> +}
> +
> +static DEVICE_ATTR(num_buffers, S_IRUGO, data_num_buffers_show, NULL);
> +static DEVICE_ATTR(buffer_size, S_IRUGO, data_bufsize_show, NULL);
> +static DEVICE_ATTR(num_inflight, S_IRUGO, data_inflight_show, NULL);
> +static DEVICE_ATTR(num_free, S_IRUGO, data_free_show, NULL);
> +static DEVICE_ATTR(num_used, S_IRUGO, data_used_show, NULL);
> +static DEVICE_ATTR(num_dropped, S_IRUGO, data_num_dropped_show, NULL);
> +static DEVICE_ATTR(enable, S_IWUGO | S_IRUGO, data_en_show, data_en_set);
> +
> +static struct attribute *data_sysfs_attrs[] = {
> + &dev_attr_num_buffers.attr,
> + &dev_attr_buffer_size.attr,
> + &dev_attr_num_inflight.attr,
> + &dev_attr_num_free.attr,
> + &dev_attr_num_used.attr,
> + &dev_attr_num_dropped.attr,
> + &dev_attr_enable.attr,
> + NULL,
> +};
Are all of these really needed or most of them are for debug?
> +
> +static const struct attribute_group rt_sysfs_attr_group = {
> + .attrs = data_sysfs_attrs,
> +};
> +
> +/*
> + * FPGA Realtime Data Character Device
> + */
> +
> +static int data_open(struct inode *inode, struct file *filp)
> +{
> + /*
> + * The miscdevice layer puts our struct miscdevice into the
> + * filp->private_data field. We use this to find our private
> + * data and then overwrite it with our own private structure.
> + */
> + struct fpga_device *priv = container_of(filp->private_data,
> + struct fpga_device, miscdev);
> + struct fpga_reader *reader;
> + int ret;
> +
> + /* allocate private data */
> + reader = kzalloc(sizeof(*reader), GFP_KERNEL);
> + if (!reader)
> + return -ENOMEM;
> +
> + reader->priv = priv;
> + reader->buf = NULL;
> +
> + filp->private_data = reader;
> + ret = nonseekable_open(inode, filp);
> + if (ret) {
> + dev_err(priv->dev, "nonseekable-open failed\n");
> + kfree(reader);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int data_release(struct inode *inode, struct file *filp)
> +{
> + struct fpga_reader *reader = filp->private_data;
> + struct fpga_device *priv = reader->priv;
> +
> + /* free the per-reader structure */
> + data_free_buffer(priv->dev, reader->buf);
> + kfree(reader);
> + filp->private_data = NULL;
> + return 0;
> +}
> +
> +static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
> + loff_t *f_pos)
> +{
> + struct fpga_reader *reader = filp->private_data;
> + struct fpga_device *priv = reader->priv;
> + struct list_head *used = &priv->used;
> + struct data_buf *dbuf;
> + size_t avail;
> + void *data;
> + int ret;
> +
> + /* check if we already have a partial buffer */
> + if (reader->buf) {
> + dbuf = reader->buf;
> + goto have_buffer;
> + }
> +
> + spin_lock_irq(&priv->lock);
> +
> + /* Block until there is at least one buffer on the used list */
> + while (list_empty(used)) {
> + spin_unlock_irq(&priv->lock);
> +
> + if (filp->f_flags & O_NONBLOCK)
> + return -EAGAIN;
> +
> + if (wait_event_interruptible(priv->wait, !list_empty(used)))
> + return -ERESTARTSYS;
> +
And somebody grabs that entry here...
> + spin_lock_irq(&priv->lock);
> + }
> +
> + /* Grab the first buffer off of the used list */
> + dbuf = list_first_entry(used, struct data_buf, entry);
And list is empty so you grabgarbage.
> + list_del_init(&dbuf->entry);
> +
> + spin_unlock_irq(&priv->lock);
Shoudl be:
struct data_buf *dbuf = NULL;
...
if (list_empty(&priv->used) && (filp->f_flags & O_NONBLOCK))
return -EAGAIN;
error = wait_event_interruptible(priv->wait, !list_empty(&priv->used);
if (error)
return error;
spin_lock_irq(&priv->lock);
if (!list_empty(&priv->used)) {
buf = list_first_entry(&priv->used, struct data_buf, entry);
list_del_init(&dbuf->entry);
}
spin_unlock_irq(&priv->lock);
if (dbuf) {
.. deal with the buffer
}
> +
> + /* Buffers are always mapped: unmap it */
> + data_unmap_buffer(priv->dev, dbuf);
> +
> + /* save the buffer for later */
> + reader->buf = dbuf;
> + reader->buf_start = 0;
> +
> + /* we removed a buffer from the used list: wake any waiters */
> + wake_up(&priv->wait);
> +
> +have_buffer:
> + /* Get the number of bytes available */
> + avail = dbuf->size - reader->buf_start;
> + data = dbuf->vb.vaddr + reader->buf_start;
> +
> + /* Get the number of bytes we can transfer */
> + count = min(count, avail);
> +
> + /* Copy the data to the userspace buffer */
> + if (copy_to_user(ubuf, data, count))
> + return -EFAULT;
> +
> + /* Update the amount of available space */
> + avail -= count;
> +
> + /* Lock against concurrent enable/disable */
> + if (mutex_lock_interruptible(&priv->mutex))
> + return -ERESTARTSYS;
> +
> + /* Still some space available: save the buffer for later */
> + if (avail != 0) {
> + reader->buf_start += count;
> + reader->buf = dbuf;
> + goto out_unlock;
> + }
> +
> + /*
> + * No space is available in this buffer
> + *
> + * This is a complicated decision:
> + * - if the device is not enabled: free the buffer
> + * - if the buffer is too small: free the buffer
> + */
> + if (!priv->enabled || dbuf->size != priv->bufsize) {
> + data_free_buffer(priv->dev, dbuf);
> + reader->buf = NULL;
> + goto out_unlock;
> + }
> +
> + /*
> + * The buffer is safe to recycle: remap it and finish
> + *
> + * If this fails, we pretend that the read never happened, and return
> + * -EFAULT to userspace. They'll retry the read again.
> + */
> + ret = data_map_buffer(priv->dev, dbuf);
> + if (ret) {
> + dev_err(priv->dev, "unable to remap buffer for DMA\n");
> + count = -EFAULT;
> + goto out_unlock;
> + }
> +
> + /* Add the buffer back to the free list */
> + reader->buf = NULL;
> + spin_lock_irq(&priv->lock);
> + list_add_tail(&dbuf->entry, &priv->free);
> + spin_unlock_irq(&priv->lock);
> +
> +out_unlock:
> + mutex_unlock(&priv->mutex);
> + return count;
> +}
> +
> +static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
> +{
> + struct fpga_reader *reader = filp->private_data;
> + struct fpga_device *priv = reader->priv;
> + unsigned int mask = 0;
> +
> + poll_wait(filp, &priv->wait, tbl);
> +
> + spin_lock_irq(&priv->lock);
> +
> + if (!list_empty(&priv->used))
> + mask |= POLLIN | POLLRDNORM;
> +
> + spin_unlock_irq(&priv->lock);
No lock is needed.
> + return mask;
> +}
> +
> +static int data_mmap(struct file *filp, struct vm_area_struct *vma)
> +{
> + struct fpga_reader *reader = filp->private_data;
> + struct fpga_device *priv = reader->priv;
> + unsigned long offset, vsize, psize, addr;
> +
> + /* VMA properties */
> + offset = vma->vm_pgoff << PAGE_SHIFT;
> + vsize = vma->vm_end - vma->vm_start;
> + psize = priv->phys_size - offset;
> + addr = (priv->phys_addr + offset) >> PAGE_SHIFT;
> +
> + /* Check against the FPGA region's physical memory size */
> + if (vsize > psize) {
> + dev_err(priv->dev, "requested mmap mapping too large\n");
> + return -EINVAL;
> + }
> +
> + /* IO memory (stop cacheing) */
> + vma->vm_flags |= VM_IO | VM_RESERVED;
> + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
> +
> + return io_remap_pfn_range(vma, vma->vm_start, addr, vsize,
> + vma->vm_page_prot);
> +}
> +
> +static const struct file_operations data_fops = {
> + .owner = THIS_MODULE,
> + .open = data_open,
> + .release = data_release,
> + .read = data_read,
> + .poll = data_poll,
> + .mmap = data_mmap,
> + .llseek = no_llseek,
> +};
> +
> +/*
> + * OpenFirmware Device Subsystem
> + */
> +
> +static bool dma_filter(struct dma_chan *chan, void *data)
> +{
> + /*
> + * DMA Channel #0 is used for the FPGA Programmer, so ignore it
> + *
> + * This probably won't survive an unload/load cycle of the Freescale
> + * DMAEngine driver, but that won't be a problem
> + */
> + if (chan->chan_id == 0 && chan->device->dev_id == 0)
> + return false;
> +
> + return true;
> +}
> +
> +static int data_of_probe(struct platform_device *op,
> + const struct of_device_id *match)
> +{
> + struct device_node *of_node = op->dev.of_node;
> + struct device *this_device;
> + struct fpga_device *priv;
> + struct resource res;
> + dma_cap_mask_t mask;
> + int ret;
> +
> + /* Allocate private data */
> + priv = kzalloc(sizeof(*priv), GFP_KERNEL);
> + if (!priv) {
> + dev_err(&op->dev, "Unable to allocate device private data\n");
> + ret = -ENOMEM;
> + goto out_return;
> + }
> +
> + dev_set_drvdata(&op->dev, priv);
> + priv->dev = &op->dev;
> +
> + /* Setup the misc device */
> + priv->miscdev.minor = MISC_DYNAMIC_MINOR;
> + priv->miscdev.name = drv_name;
> + priv->miscdev.fops = &data_fops;
> +
> + /* Get the physical address of the FPGA registers */
> + ret = of_address_to_resource(of_node, 0, &res);
> + if (ret) {
> + dev_err(&op->dev, "Unable to find FPGA physical address\n");
> + ret = -ENODEV;
> + goto out_free_priv;
> + }
> +
> + priv->phys_addr = res.start;
> + priv->phys_size = resource_size(&res);
> +
> + /* ioremap the registers for use */
> + priv->regs = of_iomap(of_node, 0);
> + if (!priv->regs) {
> + dev_err(&op->dev, "Unable to ioremap registers\n");
> + ret = -ENOMEM;
> + goto out_free_priv;
> + }
> +
> + dma_cap_zero(mask);
> + dma_cap_set(DMA_MEMCPY, mask);
> + dma_cap_set(DMA_INTERRUPT, mask);
> + dma_cap_set(DMA_SLAVE, mask);
> + dma_cap_set(DMA_SG, mask);
> +
> + /* Request a DMA channel */
> + priv->chan = dma_request_channel(mask, dma_filter, NULL);
> + if (!priv->chan) {
> + dev_err(&op->dev, "Unable to request DMA channel\n");
> + ret = -ENODEV;
> + goto out_unmap_regs;
> + }
> +
> + /* Find the correct IRQ number */
> + priv->irq = irq_of_parse_and_map(of_node, 0);
> + if (priv->irq == NO_IRQ) {
> + dev_err(&op->dev, "Unable to find IRQ line\n");
> + ret = -ENODEV;
> + goto out_release_dma;
> + }
> +
> + dev_set_drvdata(priv->dev, priv);
> + mutex_init(&priv->mutex);
> + spin_lock_init(&priv->lock);
> + INIT_LIST_HEAD(&priv->free);
> + INIT_LIST_HEAD(&priv->used);
> + INIT_LIST_HEAD(&priv->inflight);
> + init_waitqueue_head(&priv->wait);
> +
> + /* Drive the GPIO for FPGA IRQ high (no interrupt) */
> + iowrite32be(IRQ_CORL_DONE, priv->regs + SYS_IRQ_OUTPUT_DATA);
> +
> + /* Register the miscdevice */
> + ret = misc_register(&priv->miscdev);
> + if (ret) {
> + dev_err(&op->dev, "Unable to register miscdevice\n");
> + goto out_irq_dispose_mapping;
> + }
> +
> + /* Create the sysfs files */
> + this_device = priv->miscdev.this_device;
> + dev_set_drvdata(this_device, priv);
> + ret = sysfs_create_group(&this_device->kobj, &rt_sysfs_attr_group);
> + if (ret) {
> + dev_err(&op->dev, "Unable to create sysfs files\n");
> + goto out_misc_deregister;
> + }
> +
> + dev_info(&op->dev, "CARMA FPGA Realtime Data Driver Loaded\n");
> + return 0;
> +
> +out_misc_deregister:
> + misc_deregister(&priv->miscdev);
> +out_irq_dispose_mapping:
> + irq_dispose_mapping(priv->irq);
> +out_release_dma:
> + dma_release_channel(priv->chan);
> +out_unmap_regs:
> + iounmap(priv->regs);
> +out_free_priv:
> + mutex_destroy(&priv->mutex);
> + kfree(priv);
> +out_return:
> + return ret;
> +}
> +
> +static int data_of_remove(struct platform_device *op)
> +{
> + struct fpga_device *priv = dev_get_drvdata(&op->dev);
> + struct device *this_device = priv->miscdev.this_device;
> +
> + /* make sure the IRQ line is disabled */
> + mutex_lock(&priv->mutex);
> + data_device_disable(priv);
> + mutex_unlock(&priv->mutex);
Remove attributes first and lose the mutex.
> +
> + sysfs_remove_group(&this_device->kobj, &rt_sysfs_attr_group);
> + misc_deregister(&priv->miscdev);
> + irq_dispose_mapping(priv->irq);
> + dma_release_channel(priv->chan);
> + iounmap(priv->regs);
> + mutex_destroy(&priv->mutex);
> + kfree(priv);
> +
> + return 0;
> +}
> +
> +static struct of_device_id data_of_match[] = {
> + { .compatible = "carma,carma-fpga", },
> + {},
> +};
> +
> +static struct of_platform_driver data_of_driver = {
> + .probe = data_of_probe,
> + .remove = data_of_remove,
> + .driver = {
> + .name = drv_name,
> + .of_match_table = data_of_match,
> + .owner = THIS_MODULE,
> + },
> +};
> +
> +/*
> + * Module Init / Exit
> + */
> +
> +static int __init data_init(void)
> +{
> + return of_register_platform_driver(&data_of_driver);
> +}
> +
> +static void __exit data_exit(void)
> +{
> + of_unregister_platform_driver(&data_of_driver);
> +}
> +
> +MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
> +MODULE_DESCRIPTION("CARMA DATA-FPGA Access Driver");
> +MODULE_LICENSE("GPL");
> +
> +module_init(data_init);
> +module_exit(data_exit);
Thanks.
--
Dmitry
^ permalink raw reply
* RE: [PATCH V8 03/10] USB/ppc4xx: Add Synopsys DWC OTG Core InterfaceLayer
From: David Laight @ 2011-02-08 8:58 UTC (permalink / raw)
Cc: linux-usb, linuxppc-dev
In-Reply-To: <4d3a1de3bf1ff8a4f69bf2d90da0a1cf@mail.gmail.com>
=20
> [Marri] If u32 is 8bytes isn't pointer type would be 8bytes=20
> as well.
If u32 is 8bytes 'char' would only be 4 bits!
(IIRC the X code has a fubar that requires a type with 32
in its name to be 64 bits!)
David
^ permalink raw reply
* Re: [PATCH 5/6] ftrace, powerpc: Implement raw syscall tracepoints on PowerPC
From: Benjamin Herrenschmidt @ 2011-02-08 10:07 UTC (permalink / raw)
To: Steven Rostedt
Cc: Andreas Dilger, Andreas Schwab, Dave Kleikamp, Namhyung Kim,
Jiri Kosina, Jason Baron, linux-kernel, Alexander Graf,
Ingo Molnar, Paul Mackerras, Ian Munsie, KOSAKI Motohiro,
Frederic Weisbecker, Scott Wood, Nathan Lynch, Andrew Morton,
David Gibson, linuxppc-dev, Avantika Mathur
In-Reply-To: <1297131028.23343.7.camel@gandalf.stny.rr.com>
On Mon, 2011-02-07 at 21:10 -0500, Steven Rostedt wrote:
> Ben,
>
> How do you want to work this? Do you want to ack it and I take this
> patch with the others through the tracing tree, or do you want to wait
> till the tracing tree gets updated into mainline and then pull this in
> though the ppc tree. As this touches a bit of the ppc side.
I was waiting to see what you were up to vs. the rest of the series :-)
I still need to review that one closely, at which point I'm happy for
you to take it.
Cheers,
Ben.
> -- Steve
>
>
> On Thu, 2011-02-03 at 14:27 +1100, Ian Munsie wrote:
> > From: Ian Munsie <imunsie@au.ibm.com>
> >
> > This patch implements the raw syscall tracepoints on PowerPC and exports
> > them for ftrace syscalls to use.
> >
> > To minimise reworking existing code, I slightly re-ordered the thread
> > info flags such that the new TIF_SYSCALL_TRACEPOINT bit would still fit
> > within the 16 bits of the andi. instruction's UI field. The instructions
> > in question are in /arch/powerpc/kernel/entry_{32,64}.S to and the
> > _TIF_SYSCALL_T_OR_A with the thread flags to see if system call tracing
> > is enabled.
> >
> > In the case of 64bit PowerPC, arch_syscall_addr and
> > arch_syscall_match_sym_name are overridden to allow ftrace syscalls to
> > work given the unusual system call table structure and symbol names that
> > start with a period.
> >
> > Signed-off-by: Ian Munsie <imunsie@au1.ibm.com>
> > ---
> > arch/powerpc/Kconfig | 1 +
> > arch/powerpc/include/asm/ftrace.h | 14 ++++++++++++++
> > arch/powerpc/include/asm/syscall.h | 5 +++++
> > arch/powerpc/include/asm/thread_info.h | 7 +++++--
> > arch/powerpc/kernel/Makefile | 1 +
> > arch/powerpc/kernel/ftrace.c | 8 ++++++++
> > arch/powerpc/kernel/ptrace.c | 10 ++++++++++
> > 7 files changed, 44 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
> > index 7d69e9b..a0e8e02 100644
> > --- a/arch/powerpc/Kconfig
> > +++ b/arch/powerpc/Kconfig
> > @@ -134,6 +134,7 @@ config PPC
> > select HAVE_GENERIC_HARDIRQS
> > select HAVE_SPARSE_IRQ
> > select IRQ_PER_CPU
> > + select HAVE_SYSCALL_TRACEPOINTS
> >
> > config EARLY_PRINTK
> > bool
> > diff --git a/arch/powerpc/include/asm/ftrace.h b/arch/powerpc/include/asm/ftrace.h
> > index dde1296..169d039 100644
> > --- a/arch/powerpc/include/asm/ftrace.h
> > +++ b/arch/powerpc/include/asm/ftrace.h
> > @@ -60,4 +60,18 @@ struct dyn_arch_ftrace {
> >
> > #endif
> >
> > +#if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64) && !defined(__ASSEMBLY__)
> > +#define ARCH_HAS_SYSCALL_MATCH_SYM_NAME
> > +static inline bool arch_syscall_match_sym_name(const char *sym, const char *name)
> > +{
> > + /*
> > + * Compare the symbol name with the system call name. Skip the .sys or .SyS
> > + * prefix from the symbol name and the sys prefix from the system call name and
> > + * just match the rest. This is only needed on ppc64 since symbol names on
> > + * 32bit do not start with a period so the generic function will work.
> > + */
> > + return !strcmp(sym + 4, name + 3);
> > +}
> > +#endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 && !__ASSEMBLY__ */
> > +
> > #endif /* _ASM_POWERPC_FTRACE */
> > diff --git a/arch/powerpc/include/asm/syscall.h b/arch/powerpc/include/asm/syscall.h
> > index 23913e9..b54b2ad 100644
> > --- a/arch/powerpc/include/asm/syscall.h
> > +++ b/arch/powerpc/include/asm/syscall.h
> > @@ -15,6 +15,11 @@
> >
> > #include <linux/sched.h>
> >
> > +/* ftrace syscalls requires exporting the sys_call_table */
> > +#ifdef CONFIG_FTRACE_SYSCALLS
> > +extern const unsigned long *sys_call_table;
> > +#endif /* CONFIG_FTRACE_SYSCALLS */
> > +
> > static inline long syscall_get_nr(struct task_struct *task,
> > struct pt_regs *regs)
> > {
> > diff --git a/arch/powerpc/include/asm/thread_info.h b/arch/powerpc/include/asm/thread_info.h
> > index 65eb859..4403f09 100644
> > --- a/arch/powerpc/include/asm/thread_info.h
> > +++ b/arch/powerpc/include/asm/thread_info.h
> > @@ -110,7 +110,8 @@ static inline struct thread_info *current_thread_info(void)
> > #define TIF_NOERROR 12 /* Force successful syscall return */
> > #define TIF_NOTIFY_RESUME 13 /* callback before returning to user */
> > #define TIF_FREEZE 14 /* Freezing for suspend */
> > -#define TIF_RUNLATCH 15 /* Is the runlatch enabled? */
> > +#define TIF_SYSCALL_TRACEPOINT 15 /* syscall tracepoint instrumentation */
> > +#define TIF_RUNLATCH 16 /* Is the runlatch enabled? */
> >
> > /* as above, but as bit values */
> > #define _TIF_SYSCALL_TRACE (1<<TIF_SYSCALL_TRACE)
> > @@ -127,8 +128,10 @@ static inline struct thread_info *current_thread_info(void)
> > #define _TIF_NOERROR (1<<TIF_NOERROR)
> > #define _TIF_NOTIFY_RESUME (1<<TIF_NOTIFY_RESUME)
> > #define _TIF_FREEZE (1<<TIF_FREEZE)
> > +#define _TIF_SYSCALL_TRACEPOINT (1<<TIF_SYSCALL_TRACEPOINT)
> > #define _TIF_RUNLATCH (1<<TIF_RUNLATCH)
> > -#define _TIF_SYSCALL_T_OR_A (_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT|_TIF_SECCOMP)
> > +#define _TIF_SYSCALL_T_OR_A (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \
> > + _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT)
> >
> > #define _TIF_USER_WORK_MASK (_TIF_SIGPENDING | _TIF_NEED_RESCHED | \
> > _TIF_NOTIFY_RESUME)
> > diff --git a/arch/powerpc/kernel/Makefile b/arch/powerpc/kernel/Makefile
> > index 3bb2a3e..fe1ac47 100644
> > --- a/arch/powerpc/kernel/Makefile
> > +++ b/arch/powerpc/kernel/Makefile
> > @@ -105,6 +105,7 @@ obj64-$(CONFIG_AUDIT) += compat_audit.o
> >
> > obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
> > obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
> > +obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
> > obj-$(CONFIG_PERF_EVENTS) += perf_callchain.o
> >
> > obj-$(CONFIG_PPC_PERF_CTRS) += perf_event.o
> > diff --git a/arch/powerpc/kernel/ftrace.c b/arch/powerpc/kernel/ftrace.c
> > index ce1f3e4..bf99cfa 100644
> > --- a/arch/powerpc/kernel/ftrace.c
> > +++ b/arch/powerpc/kernel/ftrace.c
> > @@ -22,6 +22,7 @@
> > #include <asm/cacheflush.h>
> > #include <asm/code-patching.h>
> > #include <asm/ftrace.h>
> > +#include <asm/syscall.h>
> >
> >
> > #ifdef CONFIG_DYNAMIC_FTRACE
> > @@ -600,3 +601,10 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
> > }
> > }
> > #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
> > +
> > +#if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_PPC64)
> > +unsigned long __init arch_syscall_addr(int nr)
> > +{
> > + return sys_call_table[nr*2];
> > +}
> > +#endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_PPC64 */
> > diff --git a/arch/powerpc/kernel/ptrace.c b/arch/powerpc/kernel/ptrace.c
> > index 9065369..b6ff0cb 100644
> > --- a/arch/powerpc/kernel/ptrace.c
> > +++ b/arch/powerpc/kernel/ptrace.c
> > @@ -29,6 +29,7 @@
> > #include <linux/signal.h>
> > #include <linux/seccomp.h>
> > #include <linux/audit.h>
> > +#include <trace/syscall.h>
> > #ifdef CONFIG_PPC32
> > #include <linux/module.h>
> > #endif
> > @@ -40,6 +41,9 @@
> > #include <asm/pgtable.h>
> > #include <asm/system.h>
> >
> > +#define CREATE_TRACE_POINTS
> > +#include <trace/events/syscalls.h>
> > +
> > /*
> > * The parameter save area on the stack is used to store arguments being passed
> > * to callee function and is located at fixed offset from stack pointer.
> > @@ -1691,6 +1695,9 @@ long do_syscall_trace_enter(struct pt_regs *regs)
> > */
> > ret = -1L;
> >
> > + if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
> > + trace_sys_enter(regs, regs->gpr[0]);
> > +
> > if (unlikely(current->audit_context)) {
> > #ifdef CONFIG_PPC64
> > if (!is_32bit_task())
> > @@ -1719,6 +1726,9 @@ void do_syscall_trace_leave(struct pt_regs *regs)
> > audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
> > regs->result);
> >
> > + if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
> > + trace_sys_exit(regs, regs->result);
> > +
> > step = test_thread_flag(TIF_SINGLESTEP);
> > if (step || test_thread_flag(TIF_SYSCALL_TRACE))
> > tracehook_report_syscall_exit(regs, step);
>
^ permalink raw reply
* Re: [PATCH] PM: Hide CONFIG_PM from users
From: Mark Brown @ 2011-02-08 11:18 UTC (permalink / raw)
To: Ray Lee
Cc: Stephen Rothwell, linux-embedded, Len Brown, Dmitry Torokhov,
linux-kernel, Rafael J. Wysocki, Alan Stern, Geert Uytterhoeven,
linux-pm, ppc-dev, Andrew Morton
In-Reply-To: <AANLkTimh=8pSmQ6i-5H5+agSso7dCOcOrUJ6Uq_fu7nh@mail.gmail.com>
On Mon, Feb 07, 2011 at 05:17:59PM -0800, Ray Lee wrote:
> On Mon, Feb 7, 2011 at 7:49 AM, Mark Brown
> > I'm rather hoping that they'll notice the mailing list thread or that
> > someone else who knows what's going on with them does
> Surely you're joking. I mean, do _you_ scan every message that comes
> through lkml and its various sister lists?
Actually I do at least scan most of the lists.
> Do a git blame and add them to the CC:. It's the polite thing to do.
It's also going to result in the mail not going to the mailing lists as
there's a limit on the number of people you can CC enforced by vger
which probably isn't constructive. It's moot now but as I said in the
text you've helpfully cut I'd have suggested contacting them after the
thread had come to a conclusion.
^ permalink raw reply
* Re: [PATCH V9 09/10] USB/ppc4xx:Synopsys DWC OTG driver enable gadget support
From: Sergei Shtylyov @ 2011-02-08 12:44 UTC (permalink / raw)
To: tmarri; +Cc: Mark Miesfeld, greg, linux-usb, linuxppc-dev, Fushen Chen
In-Reply-To: <1297119240-28531-1-git-send-email-tmarri@apm.com>
Hello.
On 08.02.2011 1:54, tmarri@apm.com wrote:
> From: Tirumala Marri<tmarri@apm.com>
> Enable gadget support
> Signed-off-by: Tirumala R Marri<tmarri@apm.com>
> Signed-off-by: Fushen Chen<fchen@apm.com>
> Signed-off-by: Mark Miesfeld<mmiesfeld@apm.com>
> ---
> drivers/usb/gadget/Kconfig | 22 ++++++++++++++++++++++
> drivers/usb/gadget/gadget_chips.h | 9 +++++++++
> 2 files changed, 31 insertions(+), 0 deletions(-)
> diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig
> index 5df8a8d..de7c10b 100644
> --- a/drivers/usb/gadget/Kconfig
> +++ b/drivers/usb/gadget/Kconfig
> @@ -365,6 +365,28 @@ config USB_GADGET_MUSB_HDRC
> This OTG-capable silicon IP is used in dual designs including
> the TI DaVinci, OMAP 243x, OMAP 343x, TUSB 6010, and ADI Blackfin
>
> +# dwc_otg builds in ../dwc_otg along with host support
> +config USB_GADGET_DWC_HDRC
> + boolean "DesignWare USB Peripheral"
> + depends on DWC_OTG_MODE || DWC_DEVICE_ONLY
Patch 10 should precede this one as it defines DWC_OTG_MODE and
DWC_DEVICE_ONLY.
> + select USB_GADGET_DUALSPEED
> + select USB_GADGET_SELECTED
> + select USB_GADGET_DWC_OTG
> + help
> + This OTG-capable Designware USB IP
> +
> +config USB_GADGET_DWC_OTG
> + boolean "OTG Support"
> + depends on USB_GADGET_DWC_HDRC
What's the point of this option if it gets auto-selected by
USB_GADGET_DWC_HDRC anyway?
WBR, Sergei
^ permalink raw reply
* Re: [PATCH v2 3/3] powerpc: Add IO event interrupt support
From: Tseng-Hui (Frank) Lin @ 2011-02-08 14:29 UTC (permalink / raw)
To: Benjamin Herrenschmidt; +Cc: tsenglin, linuxppc-dev
In-Reply-To: <1297115180.14982.83.camel@pasglop>
On Tue, 2011-02-08 at 08:46 +1100, Benjamin Herrenschmidt wrote:
> On Mon, 2011-02-07 at 15:40 -0600, Tseng-Hui (Frank) Lin wrote:
> > > We are -really- re-inventing interrupts and/or notifiers here, which
> > > I find a tad annoying..
> >
> > Agree. This is ugly. We could have just used the regular interrupt
> > mechanism if the firmware didn't use shared rtas call and interrupt
> > to report multiple types of events.
>
> A notifier wouldn't have worked ?
Yes. A notifier should have worked. Will change to use a notifier for
the chain.
>
> Cheers,
> Ben.
>
>
> _______________________________________________
> Linuxppc-dev mailing list
> Linuxppc-dev@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/linuxppc-dev
^ permalink raw reply
* RE: [PATCH v3 1/4] powerpc: Removing support for 'protected-sources'
From: Yoder Stuart-B08248 @ 2011-02-08 15:13 UTC (permalink / raw)
To: Benjamin Herrenschmidt, Meador Inge
Cc: linuxppc-dev@lists.ozlabs.org,
devicetree-discuss@lists.ozlabs.org, Hollis Blanchard
In-Reply-To: <1297115136.14982.82.camel@pasglop>
> -----Original Message-----
> From: devicetree-discuss-
> bounces+stuart.yoder=3Dfreescale.com@lists.ozlabs.org [mailto:devicetree-
> discuss-bounces+stuart.yoder=3Dfreescale.com@lists.ozlabs.org] On Behalf =
Of
> Benjamin Herrenschmidt
> Sent: Monday, February 07, 2011 3:46 PM
> To: Meador Inge
> Cc: Hollis Blanchard; devicetree-discuss@lists.ozlabs.org; linuxppc-
> dev@lists.ozlabs.org
> Subject: Re: [PATCH v3 1/4] powerpc: Removing support for 'protected-
> sources'
>=20
>=20
> > In my previous reply I said that "it is not so much as a need as it is
> > a potential simplification." After further reflection, I don't think
> > that is completely true. As we get into AMP systems with higher core
> > counts, then implementing this functionality using the existing
> > "protected-sources" implementation versus the new "pic-no-reset" work
> > is going to be harder to maintain.
>=20
> I'm not arguing that your approach isn't more suitable for AMP systems, I
> just want to leave the existing protected-sources mechanism alone. I'm no=
t
> opposing adding a new, better, mechanism for newer platforms.
>=20
> However, I'd name it differently. "pic-no-reset" doesn't carry enough
> meaning in that case. What we want to point out here is that the PIC has
> been pre-initialized.
>=20
> Another option, which may be cleaner, is to stick to "no-reset" (no need
> for pic- prefix) and make it do just that (prevent the reset), and then u=
se
> a positive variant of "protected-sources", call it "allowed-sources". May=
be
> even make it a series of ranges. Then have the MPIC only access these.
>=20
> I think this is more robust as it would also prevent "accidental" use of
> the wrong sources (bad device-tree, drivers that let you muck around with
> irq numbers, etc...).
What is the use case for "allowed-sources"? For AMP the only device
nodes in your device tree should be your AMP partition's devices,
thus any interrupt specifiers in your dev tree are "allowed".
The MPIC is a shared device and thus the need for no-reset.
So, all newer platforms should need is "no-reset".
Stuart
^ permalink raw reply
* Re: [PATCH] watchdog: booke_wdt: clean up status messages
From: Timur Tabi @ 2011-02-08 16:33 UTC (permalink / raw)
To: Benjamin Herrenschmidt; +Cc: linuxppc-dev, wim, linux-watchdog
In-Reply-To: <1297040772.14982.25.camel@pasglop>
Benjamin Herrenschmidt wrote:
> If you're going to cleanup the messages, then I think displaying:
>
> PowerPC Book-E Watchdog Timer Loaded
> booke_wdt: watchdog enabled (timeout = xxx sec)
>
> Isn't very nice.
>
> Lacks consistency ... you can prefix both lines the same way, or
> maybe better print only one line with all the necessary info.
Yeah, I see your point. I'll post a v2.
--
Timur Tabi
Linux kernel developer at Freescale
^ permalink raw reply
* Re: [PATCH 5/6] ftrace, powerpc: Implement raw syscall tracepoints on PowerPC
From: Steven Rostedt @ 2011-02-08 16:54 UTC (permalink / raw)
To: Benjamin Herrenschmidt
Cc: Andreas Dilger, Andreas Schwab, Dave Kleikamp, Namhyung Kim,
Jiri Kosina, Jason Baron, linux-kernel, Alexander Graf,
Ingo Molnar, Paul Mackerras, Ian Munsie, KOSAKI Motohiro,
Frederic Weisbecker, Scott Wood, Nathan Lynch, Andrew Morton,
David Gibson, linuxppc-dev, Avantika Mathur
In-Reply-To: <1297159672.14982.205.camel@pasglop>
On Tue, 2011-02-08 at 21:07 +1100, Benjamin Herrenschmidt wrote:
> On Mon, 2011-02-07 at 21:10 -0500, Steven Rostedt wrote:
> > Ben,
> >
> > How do you want to work this? Do you want to ack it and I take this
> > patch with the others through the tracing tree, or do you want to wait
> > till the tracing tree gets updated into mainline and then pull this in
> > though the ppc tree. As this touches a bit of the ppc side.
>
> I was waiting to see what you were up to vs. the rest of the series :-)
>
> I still need to review that one closely, at which point I'm happy for
> you to take it.
It survived my tests, I'm continuing with other patches for 39 and I
have no problem with Ian's patches.
-- Steve
^ permalink raw reply
* Re: Question on supporting multiple HW versions with a single driver (warning: long post)
From: Bruce_Leonard @ 2011-02-08 16:59 UTC (permalink / raw)
To: Bill Gatliff; +Cc: linuxppc-dev, David Gibson
In-Reply-To: <AANLkTin5B02PuVrDH_5FaeYEK+amnopKzS36wyjEafFZ@mail.gmail.com>
Ah, okay. Taking a look at that it makes sense now. Thanks to both of
you for the help.
Bruce
From:
Bill Gatliff <bgat@billgatliff.com>
To:
Bruce_Leonard@selinc.com
Cc:
David Gibson <david@gibson.dropbear.id.au>, linuxppc-dev@lists.ozlabs.org
Date:
02/07/2011 09:13 PM
Subject:
Re: Question on supporting multiple HW versions with a single driver
(warning: long post)
Guys:
I think the Silicon Motion SM501 driver might provide a useful
example, since the chip comes in both memory-mapped and PCI versions.
Unfortunately the chip is implemented as a multi-function driver
(mfd), so the code is not un-complicated. Still fairly
straightforward and well-written once you learn your way around it,
though.
Basically, it implements a core set of functionality to talk to the
actual chip registers, which is bus-agnostic. Then the bus-specific
drivers use these functions when they actually want to touch the chip
itself. In other words, exactly what David suggested.
b.g.
On Mon, Feb 7, 2011 at 8:37 PM, <Bruce_Leonard@selinc.com> wrote:
>>
>> There are a number of drivers which already have this sort of dual bus
>> binding.
>>
>
> Thanks for the feedback David, I appreciate it. Could you point me to
one
> of those drivers that has "this sort of dual bus binding" so can see an
> example of what I'm trying to do?
>
> Bruce
> _______________________________________________
> Linuxppc-dev mailing list
> Linuxppc-dev@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/linuxppc-dev
>
--
Bill Gatliff
bgat@billgatliff.com
^ permalink raw reply
* Re: [PATCH 1/2] misc: add CARMA DATA-FPGA Access Driver
From: Ira W. Snyder @ 2011-02-08 17:20 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linuxppc-dev, linux-kernel
In-Reply-To: <20110208073309.GB906@core.coreip.homeip.net>
On Mon, Feb 07, 2011 at 11:33:10PM -0800, Dmitry Torokhov wrote:
> Hi Ira,
>
> On Mon, Feb 07, 2011 at 03:23:40PM -0800, Ira W. Snyder wrote:
> > This driver allows userspace to access the data processing FPGAs on the
> > OVRO CARMA board. It has two modes of operation:
> >
> > 1) random access
> >
> > This allows users to poke any DATA-FPGA registers by using mmap to map
> > the address region directly into their memory map.
> >
> > 2) correlation dumping
> >
> > When correlating, the DATA-FPGA's have special requirements for getting
> > the data out of their memory before the next correlation. This nominally
> > happens at 64Hz (every 15.625ms). If the data is not dumped before the
> > next correlation, data is lost.
> >
> > The data dumping driver handles buffering up to 1 second worth of
> > correlation data from the FPGAs. This lowers the realtime scheduling
> > requirements for the userspace process reading the device.
>
> Kind of a fly-by review but it looks like the locking in the driver
> needs work.
>
Hi Dmitry,
Thanks for the review. I have a few comments inline below.
> >
> > Signed-off-by: Ira W. Snyder <iws@ovro.caltech.edu>
> > ---
> > drivers/misc/Kconfig | 1 +
> > drivers/misc/Makefile | 1 +
> > drivers/misc/carma/Kconfig | 9 +
> > drivers/misc/carma/Makefile | 1 +
> > drivers/misc/carma/carma-fpga.c | 1446 +++++++++++++++++++++++++++++++++++++++
> > 5 files changed, 1458 insertions(+), 0 deletions(-)
> > create mode 100644 drivers/misc/carma/Kconfig
> > create mode 100644 drivers/misc/carma/Makefile
> > create mode 100644 drivers/misc/carma/carma-fpga.c
> >
> > diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
> > index 4d073f1..f457f14 100644
> > --- a/drivers/misc/Kconfig
> > +++ b/drivers/misc/Kconfig
> > @@ -457,5 +457,6 @@ source "drivers/misc/eeprom/Kconfig"
> > source "drivers/misc/cb710/Kconfig"
> > source "drivers/misc/iwmc3200top/Kconfig"
> > source "drivers/misc/ti-st/Kconfig"
> > +source "drivers/misc/carma/Kconfig"
> >
> > endif # MISC_DEVICES
> > diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
> > index 98009cc..2c1610e 100644
> > --- a/drivers/misc/Makefile
> > +++ b/drivers/misc/Makefile
> > @@ -42,3 +42,4 @@ obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
> > obj-$(CONFIG_PCH_PHUB) += pch_phub.o
> > obj-y += ti-st/
> > obj-$(CONFIG_AB8500_PWM) += ab8500-pwm.o
> > +obj-y += carma/
> > diff --git a/drivers/misc/carma/Kconfig b/drivers/misc/carma/Kconfig
> > new file mode 100644
> > index 0000000..4be183f
> > --- /dev/null
> > +++ b/drivers/misc/carma/Kconfig
> > @@ -0,0 +1,9 @@
> > +config CARMA_FPGA
> > + tristate "CARMA DATA-FPGA Access Driver"
> > + depends on FSL_SOC && PPC_83xx && MEDIA_SUPPORT && HAS_DMA && FSL_DMA
> > + select VIDEOBUF_DMA_SG
> > + default n
> > + help
> > + Say Y here to include support for communicating with the data
> > + processing FPGAs on the OVRO CARMA board.
> > +
> > diff --git a/drivers/misc/carma/Makefile b/drivers/misc/carma/Makefile
> > new file mode 100644
> > index 0000000..0b69fa7
> > --- /dev/null
> > +++ b/drivers/misc/carma/Makefile
> > @@ -0,0 +1 @@
> > +obj-$(CONFIG_CARMA_FPGA) += carma-fpga.o
> > diff --git a/drivers/misc/carma/carma-fpga.c b/drivers/misc/carma/carma-fpga.c
> > new file mode 100644
> > index 0000000..52620b3
> > --- /dev/null
> > +++ b/drivers/misc/carma/carma-fpga.c
> > @@ -0,0 +1,1446 @@
> > +/*
> > + * CARMA DATA-FPGA Access Driver
> > + *
> > + * Copyright (c) 2009-2010 Ira W. Snyder <iws@ovro.caltech.edu>
> > + *
> > + * 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.
> > + */
> > +
> > +/*
> > + * FPGA Memory Dump Format
> > + *
> > + * FPGA #0 control registers (32 x 32-bit words)
> > + * FPGA #1 control registers (32 x 32-bit words)
> > + * FPGA #2 control registers (32 x 32-bit words)
> > + * FPGA #3 control registers (32 x 32-bit words)
> > + * SYSFPGA control registers (32 x 32-bit words)
> > + * FPGA #0 correlation array (NUM_CORL0 correlation blocks)
> > + * FPGA #1 correlation array (NUM_CORL1 correlation blocks)
> > + * FPGA #2 correlation array (NUM_CORL2 correlation blocks)
> > + * FPGA #3 correlation array (NUM_CORL3 correlation blocks)
> > + *
> > + * Each correlation array consists of:
> > + *
> > + * Correlation Data (2 x NUM_LAGSn x 32-bit words)
> > + * Pipeline Metadata (2 x NUM_METAn x 32-bit words)
> > + * Quantization Counters (2 x NUM_QCNTn x 32-bit words)
> > + *
> > + * The NUM_CORLn, NUM_LAGSn, NUM_METAn, and NUM_QCNTn values come from
> > + * the FPGA configuration registers. They do not change once the FPGA's
> > + * have been programmed, they only change on re-programming.
> > + */
> > +
> > +/*
> > + * Basic Description:
> > + *
> > + * This driver is used to capture correlation spectra off of the four data
> > + * processing FPGAs. The FPGAs are often reprogrammed at runtime, therefore
> > + * this driver supports dynamic enable/disable of capture while the device
> > + * remains open.
> > + *
> > + * The nominal capture rate is 64Hz (every 15.625ms). To facilitate this fast
> > + * capture rate, all buffers are pre-allocated to avoid any potentially long
> > + * running memory allocations while capturing.
> > + *
> > + * There are three lists which are used to keep track of the different states
> > + * of data buffers.
> > + *
> > + * 1) free list
> > + * This list holds all empty data buffers which are ready to receive data.
> > + *
> > + * 2) inflight list
> > + * This list holds data buffers which are currently waiting for a DMA operation
> > + * to complete.
> > + *
> > + * 3) used list
> > + * This list holds data buffers which have been filled, and are waiting to be
> > + * read by userspace.
> > + *
> > + * All buffers start life on the free list, then move successively to the
> > + * inflight list, and then to the used list. After they have been read by
> > + * userspace, they are moved back to the free list. The cycle repeats as long
> > + * as necessary.
> > + */
> > +
> > +/*
> > + * Notes on the IRQ masking scheme:
> > + *
> > + * The IRQ masking scheme here is different than most other hardware. The only
> > + * way for the DATA-FPGAs to detect if the kernel has taken too long to copy
> > + * the data is if the status registers are not cleared before the next
> > + * correlation data dump is ready.
> > + *
> > + * The interrupt line is connected to the status registers, such that when they
> > + * are cleared, the interrupt is de-asserted. Therein lies our problem. We need
> > + * to schedule a long-running DMA operation and return from the interrupt
> > + * handler quickly, but we cannot clear the status registers.
> > + *
> > + * To handle this, the system controller FPGA has the capability to connect the
> > + * interrupt line to a user-controlled GPIO pin. This pin is driven high
> > + * (unasserted) and left that way. To mask the interrupt, we change the
> > + * interrupt source to the GPIO pin. Tada, we hid the interrupt. :)
> > + */
> > +
> > +#include <linux/of_platform.h>
> > +#include <linux/dma-mapping.h>
> > +#include <linux/miscdevice.h>
> > +#include <linux/interrupt.h>
> > +#include <linux/dmaengine.h>
> > +#include <linux/highmem.h>
> > +#include <linux/kernel.h>
> > +#include <linux/module.h>
> > +#include <linux/poll.h>
> > +#include <linux/init.h>
> > +#include <linux/slab.h>
> > +#include <linux/io.h>
> > +
> > +#include <media/videobuf-dma-sg.h>
> > +
> > +/* system controller registers */
> > +#define SYS_IRQ_SOURCE_CTL 0x24
> > +#define SYS_IRQ_OUTPUT_EN 0x28
> > +#define SYS_IRQ_OUTPUT_DATA 0x2C
> > +#define SYS_IRQ_INPUT_DATA 0x30
> > +
> > +/* GPIO IRQ line assignment */
> > +#define IRQ_CORL_DONE 0x10
> > +
> > +/* FPGA registers */
> > +#define MMAP_REG_VERSION 0x00
> > +#define MMAP_REG_CORL_CONF1 0x08
> > +#define MMAP_REG_CORL_CONF2 0x0C
> > +#define MMAP_REG_STATUS 0x48
> > +
> > +#define SYS_FPGA_BLOCK 0xF0000000
> > +
> > +static const char drv_name[] = "carma-fpga";
> > +
> > +#define NUM_FPGA 4
> > +
> > +#define MIN_DATA_BUFS 8
> > +#define MAX_DATA_BUFS 64
> > +
> > +struct fpga_info {
> > + unsigned int num_lag_ram;
> > + unsigned int blk_size;
> > +};
> > +
> > +struct data_buf {
> > + struct list_head entry;
> > + struct videobuf_dmabuf vb;
> > + bool mapped;
> > + size_t size;
> > +};
> > +
> > +struct fpga_device {
> > + struct miscdevice miscdev;
> > + struct device *dev;
> > + struct mutex mutex;
> > +
> > + /* FPGA registers and information */
> > + struct fpga_info info[NUM_FPGA];
> > + void __iomem *regs;
> > + int irq;
> > +
> > + /* FPGA Physical Address/Size Information */
> > + resource_size_t phys_addr;
> > + size_t phys_size;
> > +
> > + /* DMA structures */
> > + struct sg_table corl_table;
> > + unsigned int corl_nents;
> > + struct dma_chan *chan;
> > +
> > + /* Protection for all members below */
> > + spinlock_t lock;
> > +
> > + /* Device enable/disable flag */
> > + bool enabled;
> > +
> > + /* Correlation data buffers */
> > + wait_queue_head_t wait;
> > + struct list_head free;
> > + struct list_head used;
> > + struct list_head inflight;
> > +
> > + /* Information about data buffers */
> > + unsigned int num_dropped;
> > + unsigned int num_buffers;
> > + size_t bufsize;
> > +};
> > +
> > +struct fpga_reader {
> > + struct fpga_device *priv;
> > + struct data_buf *buf;
> > + off_t buf_start;
> > +};
> > +
> > +#define inode_to_dev(inode) container_of(inode->i_cdev, struct fpga_device, cdev)
> > +
> > +/*
> > + * Data Buffer Allocation Helpers
> > + */
> > +
> > +static int data_map_buffer(struct device *dev, struct data_buf *buf)
> > +{
> > + int ret;
> > +
> > + /* if the buffer is already mapped, we're done */
> > + if (buf->mapped)
> > + return 0;
> > +
>
> This is a local function, not library. Can't we keep track whether a
> buffer is mapped or not?
>
Sure. I'll review the driver so I don't need this variable anymore.
> > + ret = videobuf_dma_map(dev, &buf->vb);
> > + if (ret)
> > + return ret;
> > +
> > + buf->mapped = true;
> > + return 0;
> > +}
> > +
> > +static void data_unmap_buffer(struct device *dev, struct data_buf *buf)
> > +{
> > + /* the buffer is already unmapped, we're done */
> > + if (!buf->mapped)
> > + return;
> > +
> > + videobuf_dma_unmap(dev, &buf->vb);
> > + buf->mapped = false;
> > +}
> > +
> > +/**
> > + * data_free_buffer() - free a single data buffer and all allocated memory
> > + * @dev: the DMA device to map for
> > + * @buf: the buffer to free
> > + *
> > + * This will free all of the pages allocated to the given data buffer, and
> > + * then free the structure itself
> > + */
> > +static void data_free_buffer(struct device *dev, struct data_buf *buf)
> > +{
> > + /* It is ok to free a NULL buffer */
> > + if (!buf)
> > + return;
> > +
> > + /* Make sure the buffer is not on any list */
> > + list_del_init(&buf->entry);
>
> And what happens if it is? Should it be WARN_ON(!list_empty()) instead?
>
This was only defensive programming. Everywhere this function is called,
the buffer has already been removed from the list.
> > +
> > + /* unmap it for DMA */
> > + data_unmap_buffer(dev, buf);
> > +
> > + /* free all memory */
> > + videobuf_dma_free(&buf->vb);
> > + kfree(buf);
> > +}
> > +
> > +/**
> > + * data_alloc_buffer() - allocate and fill a data buffer with pages
> > + * @dev: the DMA device to map for
> > + * @bytes: the number of bytes required
> > + *
> > + * This allocates all space needed for a data buffer, and gets it ready to be
> > + * used in a DMA transaction. It only needs to be used, never mapped before
> > + * use. This avoids calling vmalloc in hardirq context.
> > + *
> > + * Returns NULL on failure
> > + */
> > +static struct data_buf *data_alloc_buffer(struct device *dev, const size_t bytes)
> > +{
> > + unsigned int nr_pages;
> > + struct data_buf *buf;
> > + int ret;
> > +
> > + /* calculate the number of pages necessary */
> > + nr_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
> > +
> > + /* allocate the buffer structure */
> > + buf = kzalloc(sizeof(*buf), GFP_KERNEL);
> > + if (!buf)
> > + goto out_return;
> > +
> > + /* initialize internal fields */
> > + INIT_LIST_HEAD(&buf->entry);
> > + buf->size = bytes;
> > +
> > + /* allocate the videobuf */
> > + videobuf_dma_init(&buf->vb);
> > + ret = videobuf_dma_init_kernel(&buf->vb, DMA_FROM_DEVICE, nr_pages);
> > + if (ret)
> > + goto out_free_buf;
> > +
> > + /* map it for DMA */
> > + ret = data_map_buffer(dev, buf);
> > + if (ret)
> > + goto out_free_videobuf;
> > +
> > + return buf;
> > +
> > +out_free_videobuf:
> > + videobuf_dma_free(&buf->vb);
> > +out_free_buf:
> > + kfree(buf);
> > +out_return:
> > + return NULL;
> > +}
> > +
> > +/**
> > + * data_free_buffers() - free all allocated buffers
> > + * @priv: the driver's private data structure
> > + *
> > + * Free all buffers allocated by the driver (except those currently in the
> > + * process of being read by userspace).
> > + *
> > + * LOCKING: must hold dev->mutex
> > + * CONTEXT: user
> > + */
> > +static void data_free_buffers(struct fpga_device *priv)
> > +{
> > + struct data_buf *buf, *tmp;
> > +
> > + spin_lock_irq(&priv->lock);
> > + BUG_ON(!list_empty(&priv->inflight));
> > +
> > + list_for_each_entry_safe(buf, tmp, &priv->free, entry) {
> > + list_del_init(&buf->entry);
> > + spin_unlock_irq(&priv->lock);
> > + data_free_buffer(priv->dev, buf);
> > + spin_lock_irq(&priv->lock);
> > + }
>
> This is messed up. If there is concurrent access to the free list then
> it is not safe to continue iterating list after releasing the lock, you
> need to do:
>
> spin_lock_irq(&priv->lock);
> while (!list_empty(&priv->free)) {
> buf = list_first_entry(&priv->free, struct data_buf, entry);
> list_del_init(&buf->entry);
> spin_unlock_irq(&priv->lock);
> data_free_buffer(priv->dev, buf);
> spin_lock_irq(&priv->lock);
> }
>
> BUT, the function is only called when you disable (or fail to enable) device
> which, at this point, should be quiesced, thus all this locking is not
> really needed.
>
Correct.
I thought it would be clearer to reviewers if I always used the lock to
protect a data structure, even when it isn't technically needed.
> > +
> > + list_for_each_entry_safe(buf, tmp, &priv->used, entry) {
> > + list_del_init(&buf->entry);
> > + spin_unlock_irq(&priv->lock);
> > + data_free_buffer(priv->dev, buf);
> > + spin_lock_irq(&priv->lock);
> > + }
> > +
> > + priv->num_buffers = 0;
> > + priv->bufsize = 0;
> > +
> > + spin_unlock_irq(&priv->lock);
> > +}
> > +
> > +/**
> > + * data_alloc_buffers() - allocate 1 seconds worth of data buffers
> > + * @priv: the driver's private data structure
> > + *
> > + * Allocate enough buffers for a whole second worth of data
> > + *
> > + * This routine will attempt to degrade nicely by succeeding even if a full
> > + * second worth of data buffers could not be allocated, as long as a minimum
> > + * number were allocated. In this case, it will print a message to the kernel
> > + * log.
> > + *
> > + * CONTEXT: user
> > + * LOCKING: must hold dev->mutex
> > + *
> > + * Returns 0 on success, -ERRNO otherwise
> > + */
> > +static int data_alloc_buffers(struct fpga_device *priv)
> > +{
> > + struct data_buf *buf;
> > + int i;
> > +
> > + for (i = 0; i < MAX_DATA_BUFS; i++) {
> > + buf = data_alloc_buffer(priv->dev, priv->bufsize);
> > + if (!buf)
> > + break;
> > +
> > + spin_lock_irq(&priv->lock);
> > + list_add_tail(&buf->entry, &priv->free);
> > + spin_unlock_irq(&priv->lock);
>
> Again, can someone be accessing this list aleady?
>
Nope. The list needs to be protected from concurrent access when the
device is running, but not when it is stopped. Same as above.
> > + }
> > +
> > + /* Make sure we allocated the minimum required number of buffers */
> > + if (i < MIN_DATA_BUFS) {
> > + dev_err(priv->dev, "Unable to allocate enough data buffers\n");
> > + data_free_buffers(priv);
> > + return -ENOMEM;
> > + }
> > +
> > + /* Warn if we are running in a degraded state, but do not fail */
> > + if (i < MAX_DATA_BUFS) {
> > + dev_warn(priv->dev, "Unable to allocate one second worth of "
> > + "buffers, using %d buffers instead\n", i);
> > + }
> > +
> > + priv->num_buffers = i;
> > + return 0;
> > +}
> > +
> > +/*
> > + * DMA Operations Helpers
> > + */
> > +
> > +/**
> > + * fpga_start_addr() - get the physical address a DATA-FPGA
> > + * @priv: the driver's private data structure
> > + * @fpga: the DATA-FPGA number (zero based)
> > + */
> > +static dma_addr_t fpga_start_addr(struct fpga_device *priv, unsigned int fpga)
> > +{
> > + return priv->phys_addr + 0x400000 + (0x80000 * fpga);
> > +}
> > +
> > +/**
> > + * fpga_block_addr() - get the physical address of a correlation data block
> > + * @priv: the driver's private data structure
> > + * @fpga: the DATA-FPGA number (zero based)
> > + * @blknum: the correlation block number (zero based)
> > + */
> > +static dma_addr_t fpga_block_addr(struct fpga_device *priv, unsigned int fpga,
> > + unsigned int blknum)
> > +{
> > + return fpga_start_addr(priv, fpga) + (0x10000 * (1 + blknum));
> > +}
> > +
> > +#define REG_BLOCK_SIZE (32 * 4)
> > +
> > +/**
> > + * data_setup_corl_table() - create the scatterlist for correlation dumps
> > + * @priv: the driver's private data structure
> > + *
> > + * Create the scatterlist for transferring a correlation dump from the
> > + * DATA FPGAs. This structure will be reused for each buffer than needs
> > + * to be filled with correlation data.
> > + *
> > + * Returns 0 on success, -ERRNO otherwise
> > + */
> > +static int data_setup_corl_table(struct fpga_device *priv)
> > +{
> > + struct sg_table *table = &priv->corl_table;
> > + struct scatterlist *sg;
> > + struct fpga_info *info;
> > + int i, j, ret;
> > +
> > + /* Calculate the number of entries needed */
> > + priv->corl_nents = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
> > + for (i = 0; i < NUM_FPGA; i++)
> > + priv->corl_nents += priv->info[i].num_lag_ram;
> > +
> > + /* Allocate the scatterlist table */
> > + ret = sg_alloc_table(table, priv->corl_nents, GFP_KERNEL);
> > + if (ret) {
> > + dev_err(priv->dev, "unable to allocate DMA table\n");
> > + return ret;
> > + }
> > +
> > + /* Add the DATA FPGA registers to the scatterlist */
> > + sg = table->sgl;
> > + for (i = 0; i < NUM_FPGA; i++) {
> > + sg_dma_address(sg) = fpga_start_addr(priv, i);
> > + sg_dma_len(sg) = REG_BLOCK_SIZE;
> > + sg = sg_next(sg);
> > + }
> > +
> > + /* Add the SYS-FPGA registers to the scatterlist */
> > + sg_dma_address(sg) = SYS_FPGA_BLOCK;
> > + sg_dma_len(sg) = REG_BLOCK_SIZE;
> > + sg = sg_next(sg);
> > +
> > + /* Add the FPGA correlation data blocks to the scatterlist */
> > + for (i = 0; i < NUM_FPGA; i++) {
> > + info = &priv->info[i];
> > + for (j = 0; j < info->num_lag_ram; j++) {
> > + sg_dma_address(sg) = fpga_block_addr(priv, i, j);
> > + sg_dma_len(sg) = info->blk_size;
> > + sg = sg_next(sg);
> > + }
> > + }
> > +
> > + /*
> > + * All physical addresses and lengths are present in the structure
> > + * now. It can be reused for every FPGA DATA interrupt
> > + */
> > + return 0;
> > +}
> > +
> > +/*
> > + * FPGA Register Access Helpers
> > + */
> > +
> > +static void fpga_write_reg(struct fpga_device *priv, unsigned int fpga,
> > + unsigned int reg, u32 val)
> > +{
> > + iowrite32be(val, priv->regs + 0x400000 + (fpga * 0x80000) + reg);
> > +}
> > +
> > +static u32 fpga_read_reg(struct fpga_device *priv, unsigned int fpga,
> > + unsigned int reg)
> > +{
> > + return ioread32be(priv->regs + 0x400000 + (fpga * 0x80000) + reg);
> > +}
> > +
> > +/**
> > + * data_calculate_bufsize() - calculate the data buffer size required
> > + * @priv: the driver's private data structure
> > + *
> > + * Calculate the total buffer size needed to hold a single block
> > + * of correlation data
> > + *
> > + * CONTEXT: user
> > + *
> > + * Returns 0 on success, -ERRNO otherwise
> > + */
> > +static int data_calculate_bufsize(struct fpga_device *priv)
> > +{
> > + u32 num_corl, num_lags, num_meta, num_qcnt, num_pack;
> > + u32 conf1, conf2, version;
> > + u32 num_lag_ram, blk_size;
> > + int i;
> > +
> > + /* Each buffer starts with the 5 FPGA register areas */
> > + priv->bufsize = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
> > +
> > + /* Read and store the configuration data for each FPGA */
> > + for (i = 0; i < NUM_FPGA; i++) {
> > + version = fpga_read_reg(priv, i, MMAP_REG_VERSION);
> > + conf1 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF1);
> > + conf2 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF2);
> > +
> > + /* minor version 2 and later */
> > + if ((version & 0x000000FF) >= 2) {
> > + num_corl = (conf1 & 0x000000F0) >> 4;
> > + num_pack = (conf1 & 0x00000F00) >> 8;
> > + num_lags = (conf1 & 0x00FFF000) >> 12;
> > + num_meta = (conf1 & 0x7F000000) >> 24;
> > + num_qcnt = (conf2 & 0x00000FFF) >> 0;
> > + } else {
> > + num_corl = (conf1 & 0x000000F0) >> 4;
> > + num_pack = 1; /* implied */
> > + num_lags = (conf1 & 0x000FFF00) >> 8;
> > + num_meta = (conf1 & 0x7FF00000) >> 20;
> > + num_qcnt = (conf2 & 0x00000FFF) >> 0;
> > + }
> > +
> > + num_lag_ram = (num_corl + num_pack - 1) / num_pack;
> > + blk_size = ((num_pack * num_lags) + num_meta + num_qcnt) * 8;
> > +
> > + priv->info[i].num_lag_ram = num_lag_ram;
> > + priv->info[i].blk_size = blk_size;
> > + priv->bufsize += num_lag_ram * blk_size;
> > +
> > + dev_dbg(priv->dev, "FPGA %d NUM_CORL: %d\n", i, num_corl);
> > + dev_dbg(priv->dev, "FPGA %d NUM_PACK: %d\n", i, num_pack);
> > + dev_dbg(priv->dev, "FPGA %d NUM_LAGS: %d\n", i, num_lags);
> > + dev_dbg(priv->dev, "FPGA %d NUM_META: %d\n", i, num_meta);
> > + dev_dbg(priv->dev, "FPGA %d NUM_QCNT: %d\n", i, num_qcnt);
> > + dev_dbg(priv->dev, "FPGA %d BLK_SIZE: %d\n", i, blk_size);
> > + }
> > +
> > + dev_dbg(priv->dev, "TOTAL BUFFER SIZE: %zu bytes\n", priv->bufsize);
> > + return 0;
> > +}
> > +
> > +/*
> > + * Interrupt Handling
> > + */
> > +
> > +/**
> > + * data_disable_interrupts() - stop the device from generating interrupts
> > + * @priv: the driver's private data structure
> > + *
> > + * Hide interrupts by switching to GPIO interrupt source
> > + *
> > + * LOCKING: must hold dev->lock
> > + */
> > +static void data_disable_interrupts(struct fpga_device *priv)
> > +{
> > + /* hide the interrupt by switching the IRQ driver to GPIO */
> > + iowrite32be(0x2F, priv->regs + SYS_IRQ_SOURCE_CTL);
> > +}
> > +
> > +/**
> > + * data_enable_interrupts() - allow the device to generate interrupts
> > + * @priv: the driver's private data structure
> > + *
> > + * Unhide interrupts by switching to the FPGA interrupt source. At the
> > + * same time, clear the DATA-FPGA status registers.
> > + *
> > + * LOCKING: must hold dev->lock
> > + */
> > +static void data_enable_interrupts(struct fpga_device *priv)
> > +{
> > + /* clear the actual FPGA corl_done interrupt */
> > + fpga_write_reg(priv, 0, MMAP_REG_STATUS, 0x0);
> > + fpga_write_reg(priv, 1, MMAP_REG_STATUS, 0x0);
> > + fpga_write_reg(priv, 2, MMAP_REG_STATUS, 0x0);
> > + fpga_write_reg(priv, 3, MMAP_REG_STATUS, 0x0);
> > +
> > + /* flush the writes */
> > + fpga_read_reg(priv, 0, MMAP_REG_STATUS);
> > +
> > + /* switch back to the external interrupt source */
> > + iowrite32be(0x3F, priv->regs + SYS_IRQ_SOURCE_CTL);
> > +}
> > +
> > +/**
> > + * data_dma_cb() - DMAEngine callback for DMA completion
> > + * @data: the driver's private data structure
> > + *
> > + * Complete a DMA transfer from the DATA-FPGA's
> > + *
> > + * This is called via the DMA callback mechanism, and will handle moving the
> > + * completed DMA transaction to the used list, and then wake any processes
> > + * waiting for new data
> > + *
> > + * CONTEXT: any, softirq expected
> > + */
> > +static void data_dma_cb(void *data)
> > +{
> > + struct fpga_device *priv = data;
> > + struct data_buf *buf;
> > + unsigned long flags;
> > +
> > + spin_lock_irqsave(&priv->lock, flags);
> > +
> > + /* clear the FPGA status and re-enable interrupts */
> > + data_enable_interrupts(priv);
> > +
> > + /* If the inflight list is empty, we've got a bug */
> > + BUG_ON(list_empty(&priv->inflight));
> > +
> > + /* Grab the first buffer from the inflight list */
> > + buf = list_first_entry(&priv->inflight, struct data_buf, entry);
> > + list_del_init(&buf->entry);
> > +
> > + /* Add it to the used list */
> > + list_add_tail(&buf->entry, &priv->used);
> > +
> > + spin_unlock_irqrestore(&priv->lock, flags);
> > +
> > + /* We've changed both the inflight and used lists, so we need
> > + * to wake up any processes that are blocking for those events */
> > + wake_up(&priv->wait);
> > +}
> > +
> > +/**
> > + * data_submit_dma() - prepare and submit the required DMA to fill a buffer
> > + * @priv: the driver's private data structure
> > + * @buf: the data buffer
> > + *
> > + * Prepare and submit the necessary DMA transactions to fill a correlation
> > + * data buffer.
> > + *
> > + * LOCKING: must hold dev->lock
> > + * CONTEXT: hardirq only
> > + *
> > + * Returns 0 on success, -ERRNO otherwise
> > + */
> > +static int data_submit_dma(struct fpga_device *priv, struct data_buf *buf)
> > +{
> > + struct scatterlist *dst_sg, *src_sg;
> > + unsigned int dst_nents, src_nents;
> > + struct dma_chan *chan = priv->chan;
> > + struct dma_async_tx_descriptor *tx;
> > + dma_cookie_t cookie;
> > + dma_addr_t dst, src;
> > +
> > + dst_sg = buf->vb.sglist;
> > + dst_nents = buf->vb.sglen;
> > +
> > + src_sg = priv->corl_table.sgl;
> > + src_nents = priv->corl_nents;
> > +
> > + /*
> > + * All buffers passed to this function should be ready and mapped
> > + * for DMA already. Therefore, we don't need to do anything except
> > + * submit it to the Freescale DMA Engine for processing
> > + */
> > +
> > + /* setup the scatterlist to scatterlist transfer */
> > + tx = chan->device->device_prep_dma_sg(chan,
> > + dst_sg, dst_nents,
> > + src_sg, src_nents,
> > + 0);
> > + if (!tx) {
> > + dev_err(priv->dev, "unable to prep scatterlist DMA\n");
> > + return -ENOMEM;
> > + }
> > +
> > + /* submit the transaction to the DMA controller */
> > + cookie = tx->tx_submit(tx);
> > + if (dma_submit_error(cookie)) {
> > + dev_err(priv->dev, "unable to submit scatterlist DMA\n");
> > + return -ENOMEM;
> > + }
> > +
> > + /* Prepare the re-read of the SYS-FPGA block */
> > + dst = sg_dma_address(dst_sg) + (NUM_FPGA * REG_BLOCK_SIZE);
> > + src = SYS_FPGA_BLOCK;
> > + tx = chan->device->device_prep_dma_memcpy(chan, dst, src,
> > + REG_BLOCK_SIZE,
> > + DMA_PREP_INTERRUPT);
> > + if (!tx) {
> > + dev_err(priv->dev, "unable to prep SYS-FPGA DMA\n");
> > + return -ENOMEM;
> > + }
> > +
> > + /* Setup the callback */
> > + tx->callback = data_dma_cb;
> > + tx->callback_param = priv;
> > +
> > + /* submit the transaction to the DMA controller */
> > + cookie = tx->tx_submit(tx);
> > + if (dma_submit_error(cookie)) {
> > + dev_err(priv->dev, "unable to submit SYS-FPGA DMA\n");
> > + return -ENOMEM;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +#define CORL_DONE 0x1
> > +#define CORL_ERR 0x2
> > +
> > +static irqreturn_t data_irq(int irq, void *dev_id)
> > +{
> > + struct fpga_device *priv = dev_id;
> > + struct data_buf *buf;
> > + u32 status;
> > + int i;
> > +
> > + /* detect spurious interrupts via FPGA status */
> > + for (i = 0; i < 4; i++) {
> > + status = fpga_read_reg(priv, i, MMAP_REG_STATUS);
> > + if (!(status & (CORL_DONE | CORL_ERR))) {
> > + dev_err(priv->dev, "spurious irq detected (FPGA)\n");
> > + return IRQ_NONE;
> > + }
> > + }
> > +
> > + /* detect spurious interrupts via raw IRQ pin readback */
> > + status = ioread32be(priv->regs + SYS_IRQ_INPUT_DATA);
> > + if (status & IRQ_CORL_DONE) {
> > + dev_err(priv->dev, "spurious irq detected (IRQ)\n");
> > + return IRQ_NONE;
> > + }
> > +
> > + spin_lock(&priv->lock);
> > +
> > + /* hide the interrupt by switching the IRQ driver to GPIO */
> > + data_disable_interrupts(priv);
> > +
> > + /* Check that we actually have a free buffer */
> > + if (list_empty(&priv->free)) {
> > + priv->num_dropped++;
> > + data_enable_interrupts(priv);
> > + goto out_unlock;
> > + }
> > +
> > + buf = list_first_entry(&priv->free, struct data_buf, entry);
> > + list_del_init(&buf->entry);
> > +
> > + /* Check the buffer size */
> > + BUG_ON(buf->size != priv->bufsize);
> > +
> > + /* Submit a DMA transfer to get the correlation data */
> > + if (data_submit_dma(priv, buf)) {
> > + dev_err(priv->dev, "Unable to setup DMA transfer\n");
> > + list_add_tail(&buf->entry, &priv->free);
> > + data_enable_interrupts(priv);
> > + goto out_unlock;
> > + }
> > +
> > + /* DMA setup succeeded, GO!!! */
> > + list_add_tail(&buf->entry, &priv->inflight);
> > + dma_async_memcpy_issue_pending(priv->chan);
> > +
> > +out_unlock:
> > + spin_unlock(&priv->lock);
> > + return IRQ_HANDLED;
> > +}
> > +
> > +/*
> > + * Realtime Device Enable Helpers
> > + */
> > +
> > +/**
> > + * data_device_enable() - enable the device for buffered dumping
> > + * @priv: the driver's private data structure
> > + *
> > + * Enable the device for buffered dumping. Allocates buffers and hooks up
> > + * the interrupt handler. When this finishes, data will come pouring in.
> > + *
> > + * LOCKING: must hold dev->mutex
> > + * CONTEXT: user context only
> > + *
> > + * Returns 0 on success, -ERRNO otherwise
> > + */
> > +static int data_device_enable(struct fpga_device *priv)
> > +{
> > + u32 val;
> > + int ret;
> > +
> > + /* multiple enables are safe: they do nothing */
> > + if (priv->enabled)
> > + return 0;
> > +
> > + /* check that the FPGAs are programmed */
> > + val = ioread32be(priv->regs + 0x44);
> > + if (!(val & (1 << 18))) {
> > + dev_err(priv->dev, "DATA-FPGAs are not enabled\n");
> > + return -ENODATA;
> > + }
> > +
> > + /* read the FPGAs to calculate the buffer size */
> > + ret = data_calculate_bufsize(priv);
> > + if (ret) {
> > + dev_err(priv->dev, "unable to calculate buffer size\n");
> > + goto out_error;
> > + }
> > +
> > + /* allocate the correlation data buffers */
> > + ret = data_alloc_buffers(priv);
> > + if (ret) {
> > + dev_err(priv->dev, "unable to allocate buffers\n");
> > + goto out_error;
> > + }
> > +
> > + /* setup the source scatterlist for dumping correlation data */
> > + ret = data_setup_corl_table(priv);
> > + if (ret) {
> > + dev_err(priv->dev, "unable to setup correlation DMA table\n");
> > + goto out_error;
> > + }
> > +
> > + /* switch to the external FPGA IRQ line */
> > + data_enable_interrupts(priv);
> > +
> > + /* hookup the irq handler */
> > + ret = request_irq(priv->irq, data_irq, IRQF_SHARED, drv_name, priv);
> > + if (ret) {
> > + dev_err(priv->dev, "unable to request IRQ handler\n");
> > + goto out_error;
> > + }
> > +
> > + /* success, we're enabled */
> > + priv->enabled = true;
> > + return 0;
> > +
> > +out_error:
> > + sg_free_table(&priv->corl_table);
> > + priv->corl_nents = 0;
> > +
> > + data_free_buffers(priv);
> > + return ret;
> > +}
> > +
> > +/**
> > + * data_device_disable() - disable the device for buffered dumping
> > + * @priv: the driver's private data structure
> > + *
> > + * Disable the device for buffered dumping. Stops new DMA transactions from
> > + * being generated, waits for all outstanding DMA to complete, and then frees
> > + * all buffers.
> > + *
> > + * LOCKING: must hold dev->mutex
> > + * CONTEXT: user only
> > + *
> > + * Returns 0 on success, -ERRNO otherwise
> > + */
> > +static int data_device_disable(struct fpga_device *priv)
> > +{
> > + struct list_head *list;
> > + int ret;
> > +
> > + /* allow multiple disable */
> > + if (!priv->enabled)
> > + return 0;
> > +
> > + /* switch to the internal GPIO IRQ line */
> > + data_disable_interrupts(priv);
> > +
> > + /* unhook the irq handler */
> > + free_irq(priv->irq, priv);
> > +
> > + /* wait for all outstanding DMA to complete */
> > + list = &priv->inflight;
> > +
> > + spin_lock_irq(&priv->lock);
> > + while (!list_empty(list)) {
> > + spin_unlock_irq(&priv->lock);
> > +
> > + ret = wait_event_interruptible(priv->wait, list_empty(list));
> > + if (ret)
> > + return -ERESTARTSYS;
> > +
> > + spin_lock_irq(&priv->lock);
> > + }
> > + spin_unlock_irq(&priv->lock);
>
> Locking is not needed - if you disable interrupyts what would put more
> stuff on the list?
>
The locking is definitely needed.
You've missed a critical piece of information. There are *two* devices
we are interacting with here, and BOTH generate interrupts.
1) DATA-FPGA (generates interrupts for dumping)
2) Freescale DMA Engine (generates interrupts at DMA completion)
The data_disable_interrupts() routine disables DATA-FPGA interrupts.
This loop waits until the DMA engine has completed.
The driver goes like this:
- several buffers on the free list
- DATA-FPGA interrupt occurs data_irq()
- disable DATA-FPGA interrupts
- take a buffer from the free list
- setup DMA data_submit_dma()
- put buffer on the inflight list back in data_irq()
- start DMA in data_irq()
- DMA finishes, DMA interrupt occurs data_dma_cb()
- re-enable DATA-FPGA interrupts
- take first buffer off the inflight list
- put buffer on the used list
> > +
> > + /* free the correlation table */
> > + sg_free_table(&priv->corl_table);
> > + priv->corl_nents = 0;
> > +
> > + /* free all of the buffers */
> > + data_free_buffers(priv);
> > + priv->enabled = false;
> > + return 0;
> > +}
> > +
> > +/*
> > + * SYSFS Attributes
> > + */
> > +
> > +/*
> > + * Count the number of entries in the given list
> > + */
> > +static unsigned int list_num_entries(struct list_head *list)
> > +{
> > + struct list_head *entry;
> > + unsigned int ret = 0;
> > +
> > + list_for_each(entry, list)
> > + ret++;
> > +
> > + return ret;
> > +}
> > +
> > +static ssize_t data_num_buffers_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + unsigned int num;
> > +
> > + spin_lock_irq(&priv->lock);
> > + num = priv->num_buffers;
> > + spin_unlock_irq(&priv->lock);
>
> This spin lock is pointless, priv->num_buffers might be already changed
> here, you can't guarantee that you show accurate data.
>
Correct, I know this. I just wanted to protect the data structure at all
points of use in the driver. Would an atomic_t be better for this, or
should I just remove the locking completely?
Personally, it seems very confusing to me when a data structure is
protected by a lock in some places, and not in others.
> > +
> > + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> > +}
> > +
> > +static ssize_t data_bufsize_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + size_t num;
> > +
> > + spin_lock_irq(&priv->lock);
> > + num = priv->bufsize;
> > + spin_unlock_irq(&priv->lock);
>
> Same here.
>
> > +
> > + return snprintf(buf, PAGE_SIZE, "%zu\n", num);
> > +}
> > +
> > +static ssize_t data_inflight_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + unsigned int num;
> > +
> > + spin_lock_irq(&priv->lock);
> > + num = list_num_entries(&priv->inflight);
> > + spin_unlock_irq(&priv->lock);
>
> And here.
>
> > +
> > + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> > +}
> > +
> > +static ssize_t data_free_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + unsigned int num;
> > +
> > + spin_lock_irq(&priv->lock);
> > + num = list_num_entries(&priv->free);
> > + spin_unlock_irq(&priv->lock);
> > +
>
> And here.
>
> > + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> > +}
> > +
> > +static ssize_t data_used_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + unsigned int num;
> > +
> > + spin_lock_irq(&priv->lock);
> > + num = list_num_entries(&priv->used);
> > + spin_unlock_irq(&priv->lock);
> > +
>
> Ditto.
>
> > + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> > +}
> > +
> > +static ssize_t data_num_dropped_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + unsigned int num;
> > +
> > + spin_lock_irq(&priv->lock);
> > + num = priv->num_dropped;
> > + spin_unlock_irq(&priv->lock);
> > +
>
> Yep..
>
> > + return snprintf(buf, PAGE_SIZE, "%u\n", num);
> > +}
> > +
> > +static ssize_t data_en_show(struct device *dev, struct device_attribute *attr,
> > + char *buf)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + ssize_t count;
> > +
> > + if (mutex_lock_interruptible(&priv->mutex))
> > + return -ERESTARTSYS;
> > +
> > + count = snprintf(buf, PAGE_SIZE, "%u\n", priv->enabled);
> > + mutex_unlock(&priv->mutex);
>
> By the time buf gets all the way to userspace, yep you guessed it...
>
> > + return count;
> > +}
> > +
> > +static ssize_t data_en_set(struct device *dev, struct device_attribute *attr,
> > + const char *buf, size_t count)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(dev);
> > + unsigned long enable;
> > + int ret;
> > +
> > + ret = strict_strtoul(buf, 0, &enable);
> > + if (ret) {
> > + dev_err(priv->dev, "unable to parse enable input\n");
> > + return -EINVAL;
> > + }
> > +
> > + if (mutex_lock_interruptible(&priv->mutex))
> > + return -ERESTARTSYS;
>
> Why don't
>
> error = mutex_lock_interruptible(&priv->mutex);
> if (error)
> return error;
>
> - do not clobber perfectly valid error codes.
>
That's what the Linux Device Drivers 3rd Edition book does. See page
112. I will change it to fix the return code.
> > +
> > + if (enable)
> > + ret = data_device_enable(priv);
> > + else
> > + ret = data_device_disable(priv);
> > +
> > + if (ret) {
> > + dev_err(priv->dev, "device %s failed\n",
> > + enable ? "enable" : "disable");
> > + count = ret;
> > + goto out_unlock;
> > + }
> > +
> > +out_unlock:
> > + mutex_unlock(&priv->mutex);
> > + return count;
> > +}
> > +
> > +static DEVICE_ATTR(num_buffers, S_IRUGO, data_num_buffers_show, NULL);
> > +static DEVICE_ATTR(buffer_size, S_IRUGO, data_bufsize_show, NULL);
> > +static DEVICE_ATTR(num_inflight, S_IRUGO, data_inflight_show, NULL);
> > +static DEVICE_ATTR(num_free, S_IRUGO, data_free_show, NULL);
> > +static DEVICE_ATTR(num_used, S_IRUGO, data_used_show, NULL);
> > +static DEVICE_ATTR(num_dropped, S_IRUGO, data_num_dropped_show, NULL);
> > +static DEVICE_ATTR(enable, S_IWUGO | S_IRUGO, data_en_show, data_en_set);
> > +
> > +static struct attribute *data_sysfs_attrs[] = {
> > + &dev_attr_num_buffers.attr,
> > + &dev_attr_buffer_size.attr,
> > + &dev_attr_num_inflight.attr,
> > + &dev_attr_num_free.attr,
> > + &dev_attr_num_used.attr,
> > + &dev_attr_num_dropped.attr,
> > + &dev_attr_enable.attr,
> > + NULL,
> > +};
>
> Are all of these really needed or most of them are for debug?
>
Most are for debugging. They have proved useful a few times in
production to track down bugs.
> > +
> > +static const struct attribute_group rt_sysfs_attr_group = {
> > + .attrs = data_sysfs_attrs,
> > +};
> > +
> > +/*
> > + * FPGA Realtime Data Character Device
> > + */
> > +
> > +static int data_open(struct inode *inode, struct file *filp)
> > +{
> > + /*
> > + * The miscdevice layer puts our struct miscdevice into the
> > + * filp->private_data field. We use this to find our private
> > + * data and then overwrite it with our own private structure.
> > + */
> > + struct fpga_device *priv = container_of(filp->private_data,
> > + struct fpga_device, miscdev);
> > + struct fpga_reader *reader;
> > + int ret;
> > +
> > + /* allocate private data */
> > + reader = kzalloc(sizeof(*reader), GFP_KERNEL);
> > + if (!reader)
> > + return -ENOMEM;
> > +
> > + reader->priv = priv;
> > + reader->buf = NULL;
> > +
> > + filp->private_data = reader;
> > + ret = nonseekable_open(inode, filp);
> > + if (ret) {
> > + dev_err(priv->dev, "nonseekable-open failed\n");
> > + kfree(reader);
> > + return ret;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int data_release(struct inode *inode, struct file *filp)
> > +{
> > + struct fpga_reader *reader = filp->private_data;
> > + struct fpga_device *priv = reader->priv;
> > +
> > + /* free the per-reader structure */
> > + data_free_buffer(priv->dev, reader->buf);
> > + kfree(reader);
> > + filp->private_data = NULL;
> > + return 0;
> > +}
> > +
> > +static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
> > + loff_t *f_pos)
> > +{
> > + struct fpga_reader *reader = filp->private_data;
> > + struct fpga_device *priv = reader->priv;
> > + struct list_head *used = &priv->used;
> > + struct data_buf *dbuf;
> > + size_t avail;
> > + void *data;
> > + int ret;
> > +
> > + /* check if we already have a partial buffer */
> > + if (reader->buf) {
> > + dbuf = reader->buf;
> > + goto have_buffer;
> > + }
> > +
> > + spin_lock_irq(&priv->lock);
> > +
> > + /* Block until there is at least one buffer on the used list */
> > + while (list_empty(used)) {
> > + spin_unlock_irq(&priv->lock);
> > +
> > + if (filp->f_flags & O_NONBLOCK)
> > + return -EAGAIN;
> > +
> > + if (wait_event_interruptible(priv->wait, !list_empty(used)))
> > + return -ERESTARTSYS;
> > +
>
> And somebody grabs that entry here...
>
> > + spin_lock_irq(&priv->lock);
> > + }
> > +
> > + /* Grab the first buffer off of the used list */
> > + dbuf = list_first_entry(used, struct data_buf, entry);
>
> And list is empty so you grabgarbage.
>
> > + list_del_init(&dbuf->entry);
> > +
> > + spin_unlock_irq(&priv->lock);
>
> Shoudl be:
>
> struct data_buf *dbuf = NULL;
> ...
>
> if (list_empty(&priv->used) && (filp->f_flags & O_NONBLOCK))
> return -EAGAIN;
>
> error = wait_event_interruptible(priv->wait, !list_empty(&priv->used);
> if (error)
> return error;
>
> spin_lock_irq(&priv->lock);
> if (!list_empty(&priv->used)) {
> buf = list_first_entry(&priv->used, struct data_buf, entry);
> list_del_init(&dbuf->entry);
> }
> spin_unlock_irq(&priv->lock);
>
> if (dbuf) {
> .. deal with the buffer
> }
>
I'm pretty sure you're wrong. Go back and re-think my loop. This is a
common idiom straight of out LDD3 pages 153-154.
You should note that it is only possible to exit the loop with the lock
held AND !list_empty(used). The lock protects the used list, and
therefore, there must be a buffer on the list.
> > +
> > + /* Buffers are always mapped: unmap it */
> > + data_unmap_buffer(priv->dev, dbuf);
> > +
> > + /* save the buffer for later */
> > + reader->buf = dbuf;
> > + reader->buf_start = 0;
> > +
> > + /* we removed a buffer from the used list: wake any waiters */
> > + wake_up(&priv->wait);
> > +
> > +have_buffer:
> > + /* Get the number of bytes available */
> > + avail = dbuf->size - reader->buf_start;
> > + data = dbuf->vb.vaddr + reader->buf_start;
> > +
> > + /* Get the number of bytes we can transfer */
> > + count = min(count, avail);
> > +
> > + /* Copy the data to the userspace buffer */
> > + if (copy_to_user(ubuf, data, count))
> > + return -EFAULT;
> > +
> > + /* Update the amount of available space */
> > + avail -= count;
> > +
> > + /* Lock against concurrent enable/disable */
> > + if (mutex_lock_interruptible(&priv->mutex))
> > + return -ERESTARTSYS;
> > +
> > + /* Still some space available: save the buffer for later */
> > + if (avail != 0) {
> > + reader->buf_start += count;
> > + reader->buf = dbuf;
> > + goto out_unlock;
> > + }
> > +
> > + /*
> > + * No space is available in this buffer
> > + *
> > + * This is a complicated decision:
> > + * - if the device is not enabled: free the buffer
> > + * - if the buffer is too small: free the buffer
> > + */
> > + if (!priv->enabled || dbuf->size != priv->bufsize) {
> > + data_free_buffer(priv->dev, dbuf);
> > + reader->buf = NULL;
> > + goto out_unlock;
> > + }
> > +
> > + /*
> > + * The buffer is safe to recycle: remap it and finish
> > + *
> > + * If this fails, we pretend that the read never happened, and return
> > + * -EFAULT to userspace. They'll retry the read again.
> > + */
> > + ret = data_map_buffer(priv->dev, dbuf);
> > + if (ret) {
> > + dev_err(priv->dev, "unable to remap buffer for DMA\n");
> > + count = -EFAULT;
> > + goto out_unlock;
> > + }
> > +
> > + /* Add the buffer back to the free list */
> > + reader->buf = NULL;
> > + spin_lock_irq(&priv->lock);
> > + list_add_tail(&dbuf->entry, &priv->free);
> > + spin_unlock_irq(&priv->lock);
> > +
> > +out_unlock:
> > + mutex_unlock(&priv->mutex);
> > + return count;
> > +}
> > +
> > +static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
> > +{
> > + struct fpga_reader *reader = filp->private_data;
> > + struct fpga_device *priv = reader->priv;
> > + unsigned int mask = 0;
> > +
> > + poll_wait(filp, &priv->wait, tbl);
> > +
> > + spin_lock_irq(&priv->lock);
> > +
> > + if (!list_empty(&priv->used))
> > + mask |= POLLIN | POLLRDNORM;
> > +
> > + spin_unlock_irq(&priv->lock);
>
> No lock is needed.
>
Why not? For example, there are two readers:
1) blocked in poll()
2) blocked in poll()
A single buffer gets added to the used list. I should only unblock one
of them with (POLLIN | POLLRDNORM), correct? Otherwise one of them must
have a spurious wakeup. I think that's incorrect (or undesirable)
behavior.
Again, it seems very inconsistent and confusing to me to protect the
used list with a spinlock in some places and not in others.
> > + return mask;
> > +}
> > +
> > +static int data_mmap(struct file *filp, struct vm_area_struct *vma)
> > +{
> > + struct fpga_reader *reader = filp->private_data;
> > + struct fpga_device *priv = reader->priv;
> > + unsigned long offset, vsize, psize, addr;
> > +
> > + /* VMA properties */
> > + offset = vma->vm_pgoff << PAGE_SHIFT;
> > + vsize = vma->vm_end - vma->vm_start;
> > + psize = priv->phys_size - offset;
> > + addr = (priv->phys_addr + offset) >> PAGE_SHIFT;
> > +
> > + /* Check against the FPGA region's physical memory size */
> > + if (vsize > psize) {
> > + dev_err(priv->dev, "requested mmap mapping too large\n");
> > + return -EINVAL;
> > + }
> > +
> > + /* IO memory (stop cacheing) */
> > + vma->vm_flags |= VM_IO | VM_RESERVED;
> > + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
> > +
> > + return io_remap_pfn_range(vma, vma->vm_start, addr, vsize,
> > + vma->vm_page_prot);
> > +}
> > +
> > +static const struct file_operations data_fops = {
> > + .owner = THIS_MODULE,
> > + .open = data_open,
> > + .release = data_release,
> > + .read = data_read,
> > + .poll = data_poll,
> > + .mmap = data_mmap,
> > + .llseek = no_llseek,
> > +};
> > +
> > +/*
> > + * OpenFirmware Device Subsystem
> > + */
> > +
> > +static bool dma_filter(struct dma_chan *chan, void *data)
> > +{
> > + /*
> > + * DMA Channel #0 is used for the FPGA Programmer, so ignore it
> > + *
> > + * This probably won't survive an unload/load cycle of the Freescale
> > + * DMAEngine driver, but that won't be a problem
> > + */
> > + if (chan->chan_id == 0 && chan->device->dev_id == 0)
> > + return false;
> > +
> > + return true;
> > +}
> > +
> > +static int data_of_probe(struct platform_device *op,
> > + const struct of_device_id *match)
> > +{
> > + struct device_node *of_node = op->dev.of_node;
> > + struct device *this_device;
> > + struct fpga_device *priv;
> > + struct resource res;
> > + dma_cap_mask_t mask;
> > + int ret;
> > +
> > + /* Allocate private data */
> > + priv = kzalloc(sizeof(*priv), GFP_KERNEL);
> > + if (!priv) {
> > + dev_err(&op->dev, "Unable to allocate device private data\n");
> > + ret = -ENOMEM;
> > + goto out_return;
> > + }
> > +
> > + dev_set_drvdata(&op->dev, priv);
> > + priv->dev = &op->dev;
> > +
> > + /* Setup the misc device */
> > + priv->miscdev.minor = MISC_DYNAMIC_MINOR;
> > + priv->miscdev.name = drv_name;
> > + priv->miscdev.fops = &data_fops;
> > +
> > + /* Get the physical address of the FPGA registers */
> > + ret = of_address_to_resource(of_node, 0, &res);
> > + if (ret) {
> > + dev_err(&op->dev, "Unable to find FPGA physical address\n");
> > + ret = -ENODEV;
> > + goto out_free_priv;
> > + }
> > +
> > + priv->phys_addr = res.start;
> > + priv->phys_size = resource_size(&res);
> > +
> > + /* ioremap the registers for use */
> > + priv->regs = of_iomap(of_node, 0);
> > + if (!priv->regs) {
> > + dev_err(&op->dev, "Unable to ioremap registers\n");
> > + ret = -ENOMEM;
> > + goto out_free_priv;
> > + }
> > +
> > + dma_cap_zero(mask);
> > + dma_cap_set(DMA_MEMCPY, mask);
> > + dma_cap_set(DMA_INTERRUPT, mask);
> > + dma_cap_set(DMA_SLAVE, mask);
> > + dma_cap_set(DMA_SG, mask);
> > +
> > + /* Request a DMA channel */
> > + priv->chan = dma_request_channel(mask, dma_filter, NULL);
> > + if (!priv->chan) {
> > + dev_err(&op->dev, "Unable to request DMA channel\n");
> > + ret = -ENODEV;
> > + goto out_unmap_regs;
> > + }
> > +
> > + /* Find the correct IRQ number */
> > + priv->irq = irq_of_parse_and_map(of_node, 0);
> > + if (priv->irq == NO_IRQ) {
> > + dev_err(&op->dev, "Unable to find IRQ line\n");
> > + ret = -ENODEV;
> > + goto out_release_dma;
> > + }
> > +
> > + dev_set_drvdata(priv->dev, priv);
> > + mutex_init(&priv->mutex);
> > + spin_lock_init(&priv->lock);
> > + INIT_LIST_HEAD(&priv->free);
> > + INIT_LIST_HEAD(&priv->used);
> > + INIT_LIST_HEAD(&priv->inflight);
> > + init_waitqueue_head(&priv->wait);
> > +
> > + /* Drive the GPIO for FPGA IRQ high (no interrupt) */
> > + iowrite32be(IRQ_CORL_DONE, priv->regs + SYS_IRQ_OUTPUT_DATA);
> > +
> > + /* Register the miscdevice */
> > + ret = misc_register(&priv->miscdev);
> > + if (ret) {
> > + dev_err(&op->dev, "Unable to register miscdevice\n");
> > + goto out_irq_dispose_mapping;
> > + }
> > +
> > + /* Create the sysfs files */
> > + this_device = priv->miscdev.this_device;
> > + dev_set_drvdata(this_device, priv);
> > + ret = sysfs_create_group(&this_device->kobj, &rt_sysfs_attr_group);
> > + if (ret) {
> > + dev_err(&op->dev, "Unable to create sysfs files\n");
> > + goto out_misc_deregister;
> > + }
> > +
> > + dev_info(&op->dev, "CARMA FPGA Realtime Data Driver Loaded\n");
> > + return 0;
> > +
> > +out_misc_deregister:
> > + misc_deregister(&priv->miscdev);
> > +out_irq_dispose_mapping:
> > + irq_dispose_mapping(priv->irq);
> > +out_release_dma:
> > + dma_release_channel(priv->chan);
> > +out_unmap_regs:
> > + iounmap(priv->regs);
> > +out_free_priv:
> > + mutex_destroy(&priv->mutex);
> > + kfree(priv);
> > +out_return:
> > + return ret;
> > +}
> > +
> > +static int data_of_remove(struct platform_device *op)
> > +{
> > + struct fpga_device *priv = dev_get_drvdata(&op->dev);
> > + struct device *this_device = priv->miscdev.this_device;
> > +
> > + /* make sure the IRQ line is disabled */
> > + mutex_lock(&priv->mutex);
> > + data_device_disable(priv);
> > + mutex_unlock(&priv->mutex);
>
> Remove attributes first and lose the mutex.
>
Ok.
> > +
> > + sysfs_remove_group(&this_device->kobj, &rt_sysfs_attr_group);
> > + misc_deregister(&priv->miscdev);
> > + irq_dispose_mapping(priv->irq);
> > + dma_release_channel(priv->chan);
> > + iounmap(priv->regs);
> > + mutex_destroy(&priv->mutex);
> > + kfree(priv);
> > +
> > + return 0;
> > +}
> > +
> > +static struct of_device_id data_of_match[] = {
> > + { .compatible = "carma,carma-fpga", },
> > + {},
> > +};
> > +
> > +static struct of_platform_driver data_of_driver = {
> > + .probe = data_of_probe,
> > + .remove = data_of_remove,
> > + .driver = {
> > + .name = drv_name,
> > + .of_match_table = data_of_match,
> > + .owner = THIS_MODULE,
> > + },
> > +};
> > +
> > +/*
> > + * Module Init / Exit
> > + */
> > +
> > +static int __init data_init(void)
> > +{
> > + return of_register_platform_driver(&data_of_driver);
> > +}
> > +
> > +static void __exit data_exit(void)
> > +{
> > + of_unregister_platform_driver(&data_of_driver);
> > +}
> > +
> > +MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
> > +MODULE_DESCRIPTION("CARMA DATA-FPGA Access Driver");
> > +MODULE_LICENSE("GPL");
> > +
> > +module_init(data_init);
> > +module_exit(data_exit);
>
> Thanks.
>
Thanks again for the review. I'd love to read your replies to some of my
comments. :)
Ira
^ permalink raw reply
* Re: [PATCH 1/2] misc: add CARMA DATA-FPGA Access Driver
From: Dave Jones @ 2011-02-08 17:29 UTC (permalink / raw)
To: Ira W. Snyder; +Cc: Dmitry Torokhov, linuxppc-dev, linux-kernel
In-Reply-To: <20110208172046.GB10125@ovro.caltech.edu>
On Tue, Feb 08, 2011 at 09:20:46AM -0800, Ira W. Snyder wrote:
> > > +static DEVICE_ATTR(enable, S_IWUGO | S_IRUGO, data_en_show, data_en_set);
> >
> > Are all of these really needed or most of them are for debug?
> >
>
> Most are for debugging. They have proved useful a few times in
> production to track down bugs.
File mode should probably not be world writable.
(checkpatch.pl should warn you about this now btw)
Dave
^ permalink raw reply
* Re: [PATCH 1/2] misc: add CARMA DATA-FPGA Access Driver
From: Dmitry Torokhov @ 2011-02-08 17:50 UTC (permalink / raw)
To: Ira W. Snyder; +Cc: linuxppc-dev, linux-kernel
In-Reply-To: <20110208172046.GB10125@ovro.caltech.edu>
On Tue, Feb 08, 2011 at 09:20:46AM -0800, Ira W. Snyder wrote:
> On Mon, Feb 07, 2011 at 11:33:10PM -0800, Dmitry Torokhov wrote:
> > > +static void data_free_buffer(struct device *dev, struct data_buf *buf)
> > > +{
> > > + /* It is ok to free a NULL buffer */
> > > + if (!buf)
> > > + return;
> > > +
> > > + /* Make sure the buffer is not on any list */
> > > + list_del_init(&buf->entry);
> >
> > And what happens if it is? Should it be WARN_ON(!list_empty()) instead?
> >
>
> This was only defensive programming. Everywhere this function is called,
> the buffer has already been removed from the list.
I am concerned as sometimes defencive programming is the sign that we
arenot quite sure how the code works. I believe defensive programming
should be used when providing library-like code, not in local cases.
> > > +
> > > + list_for_each_entry_safe(buf, tmp, &priv->free, entry) {
> > > + list_del_init(&buf->entry);
> > > + spin_unlock_irq(&priv->lock);
> > > + data_free_buffer(priv->dev, buf);
> > > + spin_lock_irq(&priv->lock);
> > > + }
> >
> > This is messed up. If there is concurrent access to the free list then
> > it is not safe to continue iterating list after releasing the lock, you
> > need to do:
> >
> > spin_lock_irq(&priv->lock);
> > while (!list_empty(&priv->free)) {
> > buf = list_first_entry(&priv->free, struct data_buf, entry);
> > list_del_init(&buf->entry);
> > spin_unlock_irq(&priv->lock);
> > data_free_buffer(priv->dev, buf);
> > spin_lock_irq(&priv->lock);
> > }
> >
> > BUT, the function is only called when you disable (or fail to enable) device
> > which, at this point, should be quiesced, thus all this locking is not
> > really needed.
> >
>
> Correct.
>
> I thought it would be clearer to reviewers if I always used the lock to
> protect a data structure, even when it isn't technically needed.
No, locks should protect wehat needs to be protected. The rest just
muddles water.
> > > +
> > > + spin_lock_irq(&priv->lock);
> > > + while (!list_empty(list)) {
> > > + spin_unlock_irq(&priv->lock);
> > > +
> > > + ret = wait_event_interruptible(priv->wait, list_empty(list));
> > > + if (ret)
> > > + return -ERESTARTSYS;
> > > +
> > > + spin_lock_irq(&priv->lock);
> > > + }
> > > + spin_unlock_irq(&priv->lock);
> >
> > Locking is not needed - if you disable interrupyts what would put more
> > stuff on the list?
> >
>
> The locking is definitely needed.
>
> You've missed a critical piece of information. There are *two* devices
> we are interacting with here, and BOTH generate interrupts.
>
No, I did not miss this fact. The point is that when we get to this code
the device _putting_ items on wauiting list is stopped and we only need
to wait for the list to drain. Nobody puts more stuff on it. You can
check fir list_empty() condition without locking.
And if someone _is_ putting more stuff on the list - you are screwed
since list may become non-empty the moment you release the lock.
> > > +
> > > +static ssize_t data_num_buffers_show(struct device *dev,
> > > + struct device_attribute *attr, char *buf)
> > > +{
> > > + struct fpga_device *priv = dev_get_drvdata(dev);
> > > + unsigned int num;
> > > +
> > > + spin_lock_irq(&priv->lock);
> > > + num = priv->num_buffers;
> > > + spin_unlock_irq(&priv->lock);
> >
> > This spin lock is pointless, priv->num_buffers might be already changed
> > here, you can't guarantee that you show accurate data.
> >
>
> Correct, I know this. I just wanted to protect the data structure at all
> points of use in the driver.
Protect from what? integer reads are guaranteed to be complete and you
are not concerned with missing updates as information is obsolete the
moment you release trhe lock.
> Would an atomic_t be better for this, or
> should I just remove the locking completely?
Just remove the locking.
> > > +
> > > + if (mutex_lock_interruptible(&priv->mutex))
> > > + return -ERESTARTSYS;
> >
> > Why don't
> >
> > error = mutex_lock_interruptible(&priv->mutex);
> > if (error)
> > return error;
> >
> > - do not clobber perfectly valid error codes.
> >
>
> That's what the Linux Device Drivers 3rd Edition book does. See page
> 112. I will change it to fix the return code.
>
LDD3 is quite old by now...
> > > +
> > > +static struct attribute *data_sysfs_attrs[] = {
> > > + &dev_attr_num_buffers.attr,
> > > + &dev_attr_buffer_size.attr,
> > > + &dev_attr_num_inflight.attr,
> > > + &dev_attr_num_free.attr,
> > > + &dev_attr_num_used.attr,
> > > + &dev_attr_num_dropped.attr,
> > > + &dev_attr_enable.attr,
> > > + NULL,
> > > +};
> >
> > Are all of these really needed or most of them are for debug?
> >
>
> Most are for debugging. They have proved useful a few times in
> production to track down bugs.
>
Consider switching over to debugfs then. Or, if you believe the device
is sufficiently bug-free just cut the code.
> > > +
> > > +static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
> > > + loff_t *f_pos)
> > > +{
> > > + struct fpga_reader *reader = filp->private_data;
> > > + struct fpga_device *priv = reader->priv;
> > > + struct list_head *used = &priv->used;
> > > + struct data_buf *dbuf;
> > > + size_t avail;
> > > + void *data;
> > > + int ret;
> > > +
> > > + /* check if we already have a partial buffer */
> > > + if (reader->buf) {
> > > + dbuf = reader->buf;
> > > + goto have_buffer;
> > > + }
> > > +
> > > + spin_lock_irq(&priv->lock);
> > > +
> > > + /* Block until there is at least one buffer on the used list */
> > > + while (list_empty(used)) {
> > > + spin_unlock_irq(&priv->lock);
> > > +
> > > + if (filp->f_flags & O_NONBLOCK)
> > > + return -EAGAIN;
> > > +
> > > + if (wait_event_interruptible(priv->wait, !list_empty(used)))
> > > + return -ERESTARTSYS;
> > > +
> >
> > And somebody grabs that entry here...
> >
> > > + spin_lock_irq(&priv->lock);
> > > + }
> > > +
> > > + /* Grab the first buffer off of the used list */
> > > + dbuf = list_first_entry(used, struct data_buf, entry);
> >
> > And list is empty so you grabgarbage.
> >
> > > + list_del_init(&dbuf->entry);
> > > +
> > > + spin_unlock_irq(&priv->lock);
> >
> > Shoudl be:
> >
> > struct data_buf *dbuf = NULL;
> > ...
> >
> > if (list_empty(&priv->used) && (filp->f_flags & O_NONBLOCK))
> > return -EAGAIN;
> >
> > error = wait_event_interruptible(priv->wait, !list_empty(&priv->used);
> > if (error)
> > return error;
> >
> > spin_lock_irq(&priv->lock);
> > if (!list_empty(&priv->used)) {
> > buf = list_first_entry(&priv->used, struct data_buf, entry);
> > list_del_init(&dbuf->entry);
> > }
> > spin_unlock_irq(&priv->lock);
> >
> > if (dbuf) {
> > .. deal with the buffer
> > }
> >
>
> I'm pretty sure you're wrong.
And I am certain I am correct. Given that I've even given example how you
can lose your list entry I don't know why you think I did not understand
your code.
> Go back and re-think my loop. This is a
> common idiom straight of out LDD3 pages 153-154.
>
> You should note that it is only possible to exit the loop with the lock
> held AND !list_empty(used). The lock protects the used list, and
> therefore, there must be a buffer on the list.
No, because you are woken up while not holding the lock so another
reader is free to take it off the list.
> > > +
> > > +static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
> > > +{
> > > + struct fpga_reader *reader = filp->private_data;
> > > + struct fpga_device *priv = reader->priv;
> > > + unsigned int mask = 0;
> > > +
> > > + poll_wait(filp, &priv->wait, tbl);
> > > +
> > > + spin_lock_irq(&priv->lock);
> > > +
> > > + if (!list_empty(&priv->used))
> > > + mask |= POLLIN | POLLRDNORM;
> > > +
> > > + spin_unlock_irq(&priv->lock);
> >
> > No lock is needed.
> >
>
> Why not? For example, there are two readers:
> 1) blocked in poll()
> 2) blocked in poll()
>
> A single buffer gets added to the used list. I should only unblock one
> of them with (POLLIN | POLLRDNORM), correct? Otherwise one of them must
> have a spurious wakeup. I think that's incorrect (or undesirable)
> behavior.
And how your lock prevents both of them wakign up? You are not resetting
wakeup condition in any way.
>
> Again, it seems very inconsistent and confusing to me to protect the
> used list with a spinlock in some places and not in others.
Please try to think about what exactly you protecting and whether lock
adds any protection or not and it will put your mind at ease.
Thanks.
--
Dmitry
^ permalink raw reply
* Re: [PATCH V9 10/10] USB ppc4xx: Add Synopsys DWC OTG driver kernel configuration and Makefile
From: Sergei Shtylyov @ 2011-02-08 18:27 UTC (permalink / raw)
To: tmarri; +Cc: Mark Miesfeld, greg, linux-usb, linuxppc-dev, Fushen Chen
In-Reply-To: <1297119242-28562-1-git-send-email-tmarri@apm.com>
Hello.
tmarri@apm.com wrote:
> From: Tirumala Marri <tmarri@apm.com>
> Add Synopsys DesignWare HS USB OTG driver kernel configuration.
> Synopsys OTG driver may operate in host only, device only, or OTG mode.
> The driver also allows user configure the core to use its internal DMA
> or Slave (PIO) mode.
> Signed-off-by: Tirumala R Marri <tmarri@apm.com>
> Signed-off-by: Fushen Chen <fchen@apm.com>
> Signed-off-by: Mark Miesfeld <mmiesfeld@apm.com>
[...]
> diff --git a/drivers/Makefile b/drivers/Makefile
> index 2cbb4b7..3bfc728 100644
> --- a/drivers/Makefile
> +++ b/drivers/Makefile
> @@ -65,6 +65,7 @@ obj-$(CONFIG_PARIDE) += block/paride/
> obj-$(CONFIG_TC) += tc/
> obj-$(CONFIG_UWB) += uwb/
> obj-$(CONFIG_USB_OTG_UTILS) += usb/otg/
> +obj-$(CONFIG_USB_DWC_OTG) += usb/otg/dwc/
Hardly a good place for this...
> obj-$(CONFIG_USB) += usb/
> obj-$(CONFIG_USB_MUSB_HDRC) += usb/musb/
> obj-$(CONFIG_PCI) += usb/
> @@ -105,6 +106,7 @@ obj-$(CONFIG_ARCH_SHMOBILE) += sh/
> ifndef CONFIG_ARCH_USES_GETTIMEOFFSET
> obj-y += clocksource/
> endif
> +obj-$(CONFIG_DMA_ENGINE) += dma/
How is this change related? Moreover, it's already present several lines ago
in this file...
> diff --git a/drivers/usb/Kconfig b/drivers/usb/Kconfig
> index 41b6e51..887f702 100644
> --- a/drivers/usb/Kconfig
> +++ b/drivers/usb/Kconfig
> @@ -55,7 +55,6 @@ config USB_ARCH_HAS_OHCI
> config USB_ARCH_HAS_EHCI
> boolean
> default y if PPC_83xx
> - default y if PPC_MPC512x
How is this change related?
> diff --git a/drivers/usb/otg/dwc/Kconfig b/drivers/usb/otg/dwc/Kconfig
> new file mode 100644
> index 0000000..4d33d72
> --- /dev/null
> +++ b/drivers/usb/otg/dwc/Kconfig
> @@ -0,0 +1,96 @@
> +#
> +# USB Dual Role (OTG-ready) Controller Drivers
> +# for silicon based on Synopsys DesignWare IP
> +#
> +
> +comment "Enable Host or Gadget support for DesignWare OTG controller"
> + depends on !USB && USB_GADGET=n
> +
> +config USB_DWC_OTG
> + depends on (USB || USB_GADGET)
Parens not necessary.
> + select NOP_USB_XCEIV
> + select USB_OTG_UTILS
> + tristate "Synopsys DWC OTG Controller"
"tristate" should come before "depends" I think -- at least that's how
everybody does...
> + default USB_GADGET
> + help
> + This driver provides USB Device Controller support for the
> + Synopsys DesignWare USB OTG Core used on the AppliedMicro PowerPC SoC.
[...]
> +choice
> + prompt "DWC Mode Selection"
> + depends on USB_DWC_OTG
> + default DWC_HOST_ONLY
> + help
> + Select the DWC Core in OTG, Host only, or Device only mode.
> +
> +config DWC_HOST_ONLY
> + bool "DWC Host Only Mode"
> +
> +config DWC_OTG_MODE
> + bool "DWC OTG Mode"
> + select USB_GADGET_SELECTED
> +
> +config DWC_DEVICE_ONLY
> + bool "DWC Device Only Mode"
> + select USB_GADGET_SELECTED
> +
> +endchoice
So this is tri-modal driver after all... how come we place it in
drivers/usb/otg/dwc/, while the same tri-modal MUSB driver was placed in
drivers/usb/musb/?
> +config USB_OTG_WHITELIST
> + bool "Rely on OTG Targeted Peripherals List"
> + depends on !USB_SUSPEND && USB_DWC_OTG
> + default n
> + help
> + This is the same flag as in ../core/Kconfig.
> + It is here for easy deselect.
What?! Option duplication isn't allowed, I think.
> +config DWC_OTG_REG_LE
> + depends on USB_DWC_OTG
> + bool "DWC Little Endian Register"
"bool" should come before "depends", I think.
> + default y
> + help
> + OTG core register access is Little-Endian.
> +
> +config DWC_OTG_FIFO_LE
> + depends on USB_DWC_OTG
> + bool "DWC FIFO Little Endian"
Same here.
> + default n
> + help
> + OTG core FIFO access is Little-Endian.
> +
> +config DWC_LIMITED_XFER_SIZE
> + depends on USB_GADGET_DWC_HDRC
> + bool "DWC Endpoint Limited Xfer Size"
... and here.
> + default n
> + help
> + Bit fields in the Device EP Transfer Size Register is 11 bits.
s/is/are/
Also, such things should better be passed via the platform data, I think.
WBR, Sergei
^ permalink raw reply
* Re: [PATCH 1/2] misc: add CARMA DATA-FPGA Access Driver
From: Ira W. Snyder @ 2011-02-08 19:11 UTC (permalink / raw)
To: Dmitry Torokhov; +Cc: linuxppc-dev, linux-kernel
In-Reply-To: <20110208175028.GD4476@core.coreip.homeip.net>
On Tue, Feb 08, 2011 at 09:50:29AM -0800, Dmitry Torokhov wrote:
> On Tue, Feb 08, 2011 at 09:20:46AM -0800, Ira W. Snyder wrote:
> > On Mon, Feb 07, 2011 at 11:33:10PM -0800, Dmitry Torokhov wrote:
> > > > +static void data_free_buffer(struct device *dev, struct data_buf *buf)
> > > > +{
> > > > + /* It is ok to free a NULL buffer */
> > > > + if (!buf)
> > > > + return;
> > > > +
> > > > + /* Make sure the buffer is not on any list */
> > > > + list_del_init(&buf->entry);
> > >
> > > And what happens if it is? Should it be WARN_ON(!list_empty()) instead?
> > >
> >
> > This was only defensive programming. Everywhere this function is called,
> > the buffer has already been removed from the list.
>
> I am concerned as sometimes defencive programming is the sign that we
> arenot quite sure how the code works. I believe defensive programming
> should be used when providing library-like code, not in local cases.
>
Ok.
> > > > +
> > > > + list_for_each_entry_safe(buf, tmp, &priv->free, entry) {
> > > > + list_del_init(&buf->entry);
> > > > + spin_unlock_irq(&priv->lock);
> > > > + data_free_buffer(priv->dev, buf);
> > > > + spin_lock_irq(&priv->lock);
> > > > + }
> > >
> > > This is messed up. If there is concurrent access to the free list then
> > > it is not safe to continue iterating list after releasing the lock, you
> > > need to do:
> > >
> > > spin_lock_irq(&priv->lock);
> > > while (!list_empty(&priv->free)) {
> > > buf = list_first_entry(&priv->free, struct data_buf, entry);
> > > list_del_init(&buf->entry);
> > > spin_unlock_irq(&priv->lock);
> > > data_free_buffer(priv->dev, buf);
> > > spin_lock_irq(&priv->lock);
> > > }
> > >
> > > BUT, the function is only called when you disable (or fail to enable) device
> > > which, at this point, should be quiesced, thus all this locking is not
> > > really needed.
> > >
> >
> > Correct.
> >
> > I thought it would be clearer to reviewers if I always used the lock to
> > protect a data structure, even when it isn't technically needed.
>
> No, locks should protect wehat needs to be protected. The rest just
> muddles water.
>
Ok.
> > > > +
> > > > + spin_lock_irq(&priv->lock);
> > > > + while (!list_empty(list)) {
> > > > + spin_unlock_irq(&priv->lock);
> > > > +
> > > > + ret = wait_event_interruptible(priv->wait, list_empty(list));
> > > > + if (ret)
> > > > + return -ERESTARTSYS;
> > > > +
> > > > + spin_lock_irq(&priv->lock);
> > > > + }
> > > > + spin_unlock_irq(&priv->lock);
> > >
> > > Locking is not needed - if you disable interrupyts what would put more
> > > stuff on the list?
> > >
> >
> > The locking is definitely needed.
> >
> > You've missed a critical piece of information. There are *two* devices
> > we are interacting with here, and BOTH generate interrupts.
> >
>
> No, I did not miss this fact. The point is that when we get to this code
> the device _putting_ items on wauiting list is stopped and we only need
> to wait for the list to drain. Nobody puts more stuff on it. You can
> check fir list_empty() condition without locking.
>
> And if someone _is_ putting more stuff on the list - you are screwed
> since list may become non-empty the moment you release the lock.
>
Ok, I understand what you mean now. You are correct, nothing else can
add things to the list. Thanks for clarifying this for me. :)
> > > > +
> > > > +static ssize_t data_num_buffers_show(struct device *dev,
> > > > + struct device_attribute *attr, char *buf)
> > > > +{
> > > > + struct fpga_device *priv = dev_get_drvdata(dev);
> > > > + unsigned int num;
> > > > +
> > > > + spin_lock_irq(&priv->lock);
> > > > + num = priv->num_buffers;
> > > > + spin_unlock_irq(&priv->lock);
> > >
> > > This spin lock is pointless, priv->num_buffers might be already changed
> > > here, you can't guarantee that you show accurate data.
> > >
> >
> > Correct, I know this. I just wanted to protect the data structure at all
> > points of use in the driver.
>
> Protect from what? integer reads are guaranteed to be complete and you
> are not concerned with missing updates as information is obsolete the
> moment you release trhe lock.
>
> > Would an atomic_t be better for this, or
> > should I just remove the locking completely?
>
> Just remove the locking.
>
Ok.
> > > > +
> > > > + if (mutex_lock_interruptible(&priv->mutex))
> > > > + return -ERESTARTSYS;
> > >
> > > Why don't
> > >
> > > error = mutex_lock_interruptible(&priv->mutex);
> > > if (error)
> > > return error;
> > >
> > > - do not clobber perfectly valid error codes.
> > >
> >
> > That's what the Linux Device Drivers 3rd Edition book does. See page
> > 112. I will change it to fix the return code.
> >
>
> LDD3 is quite old by now...
>
I know, but it is still the best written reference I have. Reviewers
like yourself are better, but I can't look up your advice in a book. :)
I'll return the error code.
> > > > +
> > > > +static struct attribute *data_sysfs_attrs[] = {
> > > > + &dev_attr_num_buffers.attr,
> > > > + &dev_attr_buffer_size.attr,
> > > > + &dev_attr_num_inflight.attr,
> > > > + &dev_attr_num_free.attr,
> > > > + &dev_attr_num_used.attr,
> > > > + &dev_attr_num_dropped.attr,
> > > > + &dev_attr_enable.attr,
> > > > + NULL,
> > > > +};
> > >
> > > Are all of these really needed or most of them are for debug?
> > >
> >
> > Most are for debugging. They have proved useful a few times in
> > production to track down bugs.
> >
>
> Consider switching over to debugfs then. Or, if you believe the device
> is sufficiently bug-free just cut the code.
>
I've never used debugfs. I'll look into it. I think the device is almost
bug free, though.
> > > > +
> > > > +static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
> > > > + loff_t *f_pos)
> > > > +{
> > > > + struct fpga_reader *reader = filp->private_data;
> > > > + struct fpga_device *priv = reader->priv;
> > > > + struct list_head *used = &priv->used;
> > > > + struct data_buf *dbuf;
> > > > + size_t avail;
> > > > + void *data;
> > > > + int ret;
> > > > +
> > > > + /* check if we already have a partial buffer */
> > > > + if (reader->buf) {
> > > > + dbuf = reader->buf;
> > > > + goto have_buffer;
> > > > + }
> > > > +
> > > > + spin_lock_irq(&priv->lock);
> > > > +
> > > > + /* Block until there is at least one buffer on the used list */
> > > > + while (list_empty(used)) {
> > > > + spin_unlock_irq(&priv->lock);
> > > > +
> > > > + if (filp->f_flags & O_NONBLOCK)
> > > > + return -EAGAIN;
> > > > +
> > > > + if (wait_event_interruptible(priv->wait, !list_empty(used)))
> > > > + return -ERESTARTSYS;
> > > > +
> > >
> > > And somebody grabs that entry here...
> > >
> > > > + spin_lock_irq(&priv->lock);
> > > > + }
> > > > +
> > > > + /* Grab the first buffer off of the used list */
> > > > + dbuf = list_first_entry(used, struct data_buf, entry);
> > >
> > > And list is empty so you grabgarbage.
> > >
> > > > + list_del_init(&dbuf->entry);
> > > > +
> > > > + spin_unlock_irq(&priv->lock);
> > >
> > > Shoudl be:
> > >
> > > struct data_buf *dbuf = NULL;
> > > ...
> > >
> > > if (list_empty(&priv->used) && (filp->f_flags & O_NONBLOCK))
> > > return -EAGAIN;
> > >
> > > error = wait_event_interruptible(priv->wait, !list_empty(&priv->used);
> > > if (error)
> > > return error;
> > >
> > > spin_lock_irq(&priv->lock);
> > > if (!list_empty(&priv->used)) {
> > > buf = list_first_entry(&priv->used, struct data_buf, entry);
> > > list_del_init(&dbuf->entry);
> > > }
> > > spin_unlock_irq(&priv->lock);
> > >
> > > if (dbuf) {
> > > .. deal with the buffer
> > > }
> > >
> >
> > I'm pretty sure you're wrong.
>
> And I am certain I am correct. Given that I've even given example how you
> can lose your list entry I don't know why you think I did not understand
> your code.
>
> > Go back and re-think my loop. This is a
> > common idiom straight of out LDD3 pages 153-154.
> >
> > You should note that it is only possible to exit the loop with the lock
> > held AND !list_empty(used). The lock protects the used list, and
> > therefore, there must be a buffer on the list.
>
> No, because you are woken up while not holding the lock so another
> reader is free to take it off the list.
>
Correct. But then I go around the loop and check list_empty() again
before exiting the loop. The list MUST NOT be empty before the loop will
terminate.
I still don't understand. Both of our solutions are equivalent, but
yours has worse error handling. Let's examine your example, line by
line:
> + spin_lock_irq(&priv->lock);
Locked.
> +
> + /* Block until there is at least one buffer on the used list */
> + while (list_empty(used)) {
We ONLY enter the loop if the list is empty.
> + spin_unlock_irq(&priv->lock);
> +
Unlock.
> + if (filp->f_flags & O_NONBLOCK)
> + return -EAGAIN;
> +
> + if (wait_event_interruptible(priv->wait, !list_empty(used)))
> + return -ERESTARTSYS;
> +
Block until the the list is NOT empty. Assume at this point that there
is another reader racing against us. The other reader wins, and the list
is empty BEFORE I grab the lock on the next line. This is exactly what
you describe above.
> + spin_lock_irq(&priv->lock);
Locked. Now we re-check the loop condition. The list is EMPTY and start
the loop again.
> + }
> +
When we get here, there are two true statements:
1) we hold priv->lock spinlock
2) list_empty(used) == false
> + /* Grab the first buffer off of the used list */
> + dbuf = list_first_entry(used, struct data_buf, entry);
> + list_del_init(&dbuf->entry);
> +
Therefore list_first_entry() CAN NOT return garbage. We MUST have
something in the list.
> + spin_unlock_irq(&priv->lock);
Perhaps if I write the loop a different way, it will become more clear?
This loop is exactly equivalent to the loop in my code.
while (1) {
spin_lock_irq(&priv->lock);
if (!list_empty(used))
break;
spin_unlock_irq(&priv->lock);
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
if (wait_event_interruptible(priv->wait, !list_empty(used))
return -ERESTARTSYS;
}
/* grab the first buffer off the used list */
dbuf = list_first_entry(used, struct data_buf, entry);
list_del_init(&dbuf->entry);
spin_unlock_irq(&priv->lock);
I must not understand some part of the code. In my best understanding,
my original code cannot fail in the way you expect. This is because of
the while loop, which rechecks its condition before exiting. Can you
please try to clarify where my misunderstanding is?
(I'm not trying to argue. Hopefully this will make my future work
better.)
> > > > +
> > > > +static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
> > > > +{
> > > > + struct fpga_reader *reader = filp->private_data;
> > > > + struct fpga_device *priv = reader->priv;
> > > > + unsigned int mask = 0;
> > > > +
> > > > + poll_wait(filp, &priv->wait, tbl);
> > > > +
> > > > + spin_lock_irq(&priv->lock);
> > > > +
> > > > + if (!list_empty(&priv->used))
> > > > + mask |= POLLIN | POLLRDNORM;
> > > > +
> > > > + spin_unlock_irq(&priv->lock);
> > >
> > > No lock is needed.
> > >
> >
> > Why not? For example, there are two readers:
> > 1) blocked in poll()
> > 2) blocked in poll()
> >
> > A single buffer gets added to the used list. I should only unblock one
> > of them with (POLLIN | POLLRDNORM), correct? Otherwise one of them must
> > have a spurious wakeup. I think that's incorrect (or undesirable)
> > behavior.
>
> And how your lock prevents both of them wakign up? You are not resetting
> wakeup condition in any way.
>
> >
> > Again, it seems very inconsistent and confusing to me to protect the
> > used list with a spinlock in some places and not in others.
>
> Please try to think about what exactly you protecting and whether lock
> adds any protection or not and it will put your mind at ease.
Ok.
Thanks again for the comments,
Ira
^ permalink raw reply
* help debugging linux setting PLX Gen2 PCIe switches to gen1 speed
From: Ayman El-Khashab @ 2011-02-08 19:19 UTC (permalink / raw)
To: linuxppc-dev
I've got a custom 460SX board with PLX gen2 (8616) switches.
When u-boot comes up everything is fine and my switches are indicating they
are in Gen2 PCIe mode. If I let it boot my linux kernel (denx 2.6.36.1), boot
almost immediately, the LEDs on my switches start to blink indicating they
have are now at Gen1 speed. I've turned on debug in ppc4xx_pci.c but don't see
anything obvious. As far as when it happens, it looks like the last message
to the console is "uncompressing kernel" and then the LEDs start blinking.
seems that it indicates it is very early. I've also tried removing all the PCIe
from the device tree, but that did not matter.
In the data sheet it indicates that PEDSRn_RCEI can change the advertised rate.
I've disabled that in the code, but it did not make any difference.
I am just not sure where things are getting switched over. Any help would be
appreciated.
best
ayman
^ permalink raw reply
* Re: [PATCH 1/2] misc: add CARMA DATA-FPGA Access Driver
From: Dmitry Torokhov @ 2011-02-08 19:33 UTC (permalink / raw)
To: Ira W. Snyder; +Cc: linuxppc-dev, linux-kernel
In-Reply-To: <20110208191143.GC10125@ovro.caltech.edu>
On Tue, Feb 08, 2011 at 11:11:44AM -0800, Ira W. Snyder wrote:
> On Tue, Feb 08, 2011 at 09:50:29AM -0800, Dmitry Torokhov wrote:
> > On Tue, Feb 08, 2011 at 09:20:46AM -0800, Ira W. Snyder wrote:
> >
> > > Go back and re-think my loop. This is a
> > > common idiom straight of out LDD3 pages 153-154.
> > >
> > > You should note that it is only possible to exit the loop with the lock
> > > held AND !list_empty(used). The lock protects the used list, and
> > > therefore, there must be a buffer on the list.
> >
> > No, because you are woken up while not holding the lock so another
> > reader is free to take it off the list.
> >
>
> Correct. But then I go around the loop and check list_empty() again
> before exiting the loop. The list MUST NOT be empty before the loop will
> terminate.
Yes, you are right, I competely missed the fact that we'd loop around
and check the condition again. I'll go grab another coffee now.
--
Dmitry
^ permalink raw reply
* [PATCH RFCv5 0/2] CARMA Board Support
From: Ira W. Snyder @ 2011-02-08 23:37 UTC (permalink / raw)
To: linuxppc-dev; +Cc: dmitry.torokhov, linux-kernel, Ira W. Snyder
Hello everyone,
This is the fifth posting of these drivers, taking into account comments from
earlier postings. I would appreciate as much review as you can offer.
RFCv4 -> RFCv5:
- remove unecessary locking per review comments
- do not clobber return values from *_interruptible()
- explicitly track buffer DMA mapping
- use #defines instead of raw hex addresses
- change enable sysfs attribute to root-writeable only
RFCv3 -> RFCv4:
- updates for DATA-FPGA version 2
RFCv2 -> RFCv3:
- use miscdevice framework (removing the carma class)
- add bitfile readback capability to the programmer
RFCv1 -> RFCv2:
- change comments to kerneldoc format
- Kconfig improvements
- use the videobuf_dma_sg API in the programmer
- updates for Freescale DMAEngine DMA_SLAVE API changes
Information about the CARMA board:
The CARMA board is essentially an MPC8349EA MDS reference design with a
1GHz ADC and 4 high powered data processing FPGAs connected to the local
bus. It is all packed into a compact PCI form factor. It is used at the
Owens Valley Radio Observatory as the main component in the correlator
system.
For board information, see:
http://www.mmarray.org/~dwh/carma_board/index.html
For DATA-FPGA register layout, see:
http://www.mmarray.org/memos/carma_memo46.pdf
These drivers are the necessary pieces to get the data processing FPGAs
working and producing data. Despite the fact that the hardware is custom
and we are the only users, I'd still like to get the drivers upstream.
Several people have suggested that this is possible.
Some further patches will be forthcoming. I have a driver for the LED
subsystem and the PPS subsystem. The LED register layout is expected to
change soon, so I won't post the driver until that is finished. The PPS
driver will be posted seperately from this patch series; it is very
generic.
Thanks to everyone who has provided comments on earlier versions!
Ira W. Snyder (2):
misc: add CARMA DATA-FPGA Access Driver
misc: add CARMA DATA-FPGA Programmer support
drivers/misc/Kconfig | 1 +
drivers/misc/Makefile | 1 +
drivers/misc/carma/Kconfig | 18 +
drivers/misc/carma/Makefile | 2 +
drivers/misc/carma/carma-fpga-program.c | 1084 ++++++++++++++++++++++++
drivers/misc/carma/carma-fpga.c | 1396 +++++++++++++++++++++++++++++++
6 files changed, 2502 insertions(+), 0 deletions(-)
create mode 100644 drivers/misc/carma/Kconfig
create mode 100644 drivers/misc/carma/Makefile
create mode 100644 drivers/misc/carma/carma-fpga-program.c
create mode 100644 drivers/misc/carma/carma-fpga.c
--
1.7.3.4
^ permalink raw reply
* [PATCH 1/2] misc: add CARMA DATA-FPGA Access Driver
From: Ira W. Snyder @ 2011-02-08 23:37 UTC (permalink / raw)
To: linuxppc-dev; +Cc: dmitry.torokhov, linux-kernel, Ira W. Snyder
In-Reply-To: <1297208267-27087-1-git-send-email-iws@ovro.caltech.edu>
This driver allows userspace to access the data processing FPGAs on the
OVRO CARMA board. It has two modes of operation:
1) random access
This allows users to poke any DATA-FPGA registers by using mmap to map
the address region directly into their memory map.
2) correlation dumping
When correlating, the DATA-FPGA's have special requirements for getting
the data out of their memory before the next correlation. This nominally
happens at 64Hz (every 15.625ms). If the data is not dumped before the
next correlation, data is lost.
The data dumping driver handles buffering up to 1 second worth of
correlation data from the FPGAs. This lowers the realtime scheduling
requirements for the userspace process reading the device.
Signed-off-by: Ira W. Snyder <iws@ovro.caltech.edu>
---
drivers/misc/Kconfig | 1 +
drivers/misc/Makefile | 1 +
drivers/misc/carma/Kconfig | 9 +
drivers/misc/carma/Makefile | 1 +
drivers/misc/carma/carma-fpga.c | 1396 +++++++++++++++++++++++++++++++++++++++
5 files changed, 1408 insertions(+), 0 deletions(-)
create mode 100644 drivers/misc/carma/Kconfig
create mode 100644 drivers/misc/carma/Makefile
create mode 100644 drivers/misc/carma/carma-fpga.c
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index cc8e49d..93cf1e6 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -457,5 +457,6 @@ source "drivers/misc/eeprom/Kconfig"
source "drivers/misc/cb710/Kconfig"
source "drivers/misc/iwmc3200top/Kconfig"
source "drivers/misc/ti-st/Kconfig"
+source "drivers/misc/carma/Kconfig"
endif # MISC_DEVICES
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 98009cc..2c1610e 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -42,3 +42,4 @@ obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
obj-$(CONFIG_PCH_PHUB) += pch_phub.o
obj-y += ti-st/
obj-$(CONFIG_AB8500_PWM) += ab8500-pwm.o
+obj-y += carma/
diff --git a/drivers/misc/carma/Kconfig b/drivers/misc/carma/Kconfig
new file mode 100644
index 0000000..4be183f
--- /dev/null
+++ b/drivers/misc/carma/Kconfig
@@ -0,0 +1,9 @@
+config CARMA_FPGA
+ tristate "CARMA DATA-FPGA Access Driver"
+ depends on FSL_SOC && PPC_83xx && MEDIA_SUPPORT && HAS_DMA && FSL_DMA
+ select VIDEOBUF_DMA_SG
+ default n
+ help
+ Say Y here to include support for communicating with the data
+ processing FPGAs on the OVRO CARMA board.
+
diff --git a/drivers/misc/carma/Makefile b/drivers/misc/carma/Makefile
new file mode 100644
index 0000000..0b69fa7
--- /dev/null
+++ b/drivers/misc/carma/Makefile
@@ -0,0 +1 @@
+obj-$(CONFIG_CARMA_FPGA) += carma-fpga.o
diff --git a/drivers/misc/carma/carma-fpga.c b/drivers/misc/carma/carma-fpga.c
new file mode 100644
index 0000000..4ea473a
--- /dev/null
+++ b/drivers/misc/carma/carma-fpga.c
@@ -0,0 +1,1396 @@
+/*
+ * CARMA DATA-FPGA Access Driver
+ *
+ * Copyright (c) 2009-2011 Ira W. Snyder <iws@ovro.caltech.edu>
+ *
+ * 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.
+ */
+
+/*
+ * FPGA Memory Dump Format
+ *
+ * FPGA #0 control registers (32 x 32-bit words)
+ * FPGA #1 control registers (32 x 32-bit words)
+ * FPGA #2 control registers (32 x 32-bit words)
+ * FPGA #3 control registers (32 x 32-bit words)
+ * SYSFPGA control registers (32 x 32-bit words)
+ * FPGA #0 correlation array (NUM_CORL0 correlation blocks)
+ * FPGA #1 correlation array (NUM_CORL1 correlation blocks)
+ * FPGA #2 correlation array (NUM_CORL2 correlation blocks)
+ * FPGA #3 correlation array (NUM_CORL3 correlation blocks)
+ *
+ * Each correlation array consists of:
+ *
+ * Correlation Data (2 x NUM_LAGSn x 32-bit words)
+ * Pipeline Metadata (2 x NUM_METAn x 32-bit words)
+ * Quantization Counters (2 x NUM_QCNTn x 32-bit words)
+ *
+ * The NUM_CORLn, NUM_LAGSn, NUM_METAn, and NUM_QCNTn values come from
+ * the FPGA configuration registers. They do not change once the FPGA's
+ * have been programmed, they only change on re-programming.
+ */
+
+/*
+ * Basic Description:
+ *
+ * This driver is used to capture correlation spectra off of the four data
+ * processing FPGAs. The FPGAs are often reprogrammed at runtime, therefore
+ * this driver supports dynamic enable/disable of capture while the device
+ * remains open.
+ *
+ * The nominal capture rate is 64Hz (every 15.625ms). To facilitate this fast
+ * capture rate, all buffers are pre-allocated to avoid any potentially long
+ * running memory allocations while capturing.
+ *
+ * There are three lists which are used to keep track of the different states
+ * of data buffers.
+ *
+ * 1) free list
+ * This list holds all empty data buffers which are ready to receive data.
+ *
+ * 2) inflight list
+ * This list holds data buffers which are currently waiting for a DMA operation
+ * to complete.
+ *
+ * 3) used list
+ * This list holds data buffers which have been filled, and are waiting to be
+ * read by userspace.
+ *
+ * All buffers start life on the free list, then move successively to the
+ * inflight list, and then to the used list. After they have been read by
+ * userspace, they are moved back to the free list. The cycle repeats as long
+ * as necessary.
+ *
+ * It should be noted that all buffers are mapped and ready for DMA when they
+ * are on any of the three lists. They are only unmapped when they are in the
+ * process of being read by userspace.
+ */
+
+/*
+ * Notes on the IRQ masking scheme:
+ *
+ * The IRQ masking scheme here is different than most other hardware. The only
+ * way for the DATA-FPGAs to detect if the kernel has taken too long to copy
+ * the data is if the status registers are not cleared before the next
+ * correlation data dump is ready.
+ *
+ * The interrupt line is connected to the status registers, such that when they
+ * are cleared, the interrupt is de-asserted. Therein lies our problem. We need
+ * to schedule a long-running DMA operation and return from the interrupt
+ * handler quickly, but we cannot clear the status registers.
+ *
+ * To handle this, the system controller FPGA has the capability to connect the
+ * interrupt line to a user-controlled GPIO pin. This pin is driven high
+ * (unasserted) and left that way. To mask the interrupt, we change the
+ * interrupt source to the GPIO pin. Tada, we hid the interrupt. :)
+ */
+
+#include <linux/of_platform.h>
+#include <linux/dma-mapping.h>
+#include <linux/miscdevice.h>
+#include <linux/interrupt.h>
+#include <linux/dmaengine.h>
+#include <linux/highmem.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/poll.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/io.h>
+
+#include <media/videobuf-dma-sg.h>
+
+/* system controller registers */
+#define SYS_IRQ_SOURCE_CTL 0x24
+#define SYS_IRQ_OUTPUT_EN 0x28
+#define SYS_IRQ_OUTPUT_DATA 0x2C
+#define SYS_IRQ_INPUT_DATA 0x30
+#define SYS_FPGA_CONFIG_STATUS 0x44
+
+/* GPIO IRQ line assignment */
+#define IRQ_CORL_DONE 0x10
+
+/* FPGA registers */
+#define MMAP_REG_VERSION 0x00
+#define MMAP_REG_CORL_CONF1 0x08
+#define MMAP_REG_CORL_CONF2 0x0C
+#define MMAP_REG_STATUS 0x48
+
+#define SYS_FPGA_BLOCK 0xF0000000
+
+#define DATA_FPGA_START 0x400000
+#define DATA_FPGA_SIZE 0x80000
+
+static const char drv_name[] = "carma-fpga";
+
+#define NUM_FPGA 4
+
+#define MIN_DATA_BUFS 8
+#define MAX_DATA_BUFS 64
+
+struct fpga_info {
+ unsigned int num_lag_ram;
+ unsigned int blk_size;
+};
+
+struct data_buf {
+ struct list_head entry;
+ struct videobuf_dmabuf vb;
+ size_t size;
+};
+
+struct fpga_device {
+ struct miscdevice miscdev;
+ struct device *dev;
+ struct mutex mutex;
+
+ /* FPGA registers and information */
+ struct fpga_info info[NUM_FPGA];
+ void __iomem *regs;
+ int irq;
+
+ /* FPGA Physical Address/Size Information */
+ resource_size_t phys_addr;
+ size_t phys_size;
+
+ /* DMA structures */
+ struct sg_table corl_table;
+ unsigned int corl_nents;
+ struct dma_chan *chan;
+
+ /* Protection for all members below */
+ spinlock_t lock;
+
+ /* Device enable/disable flag */
+ bool enabled;
+
+ /* Correlation data buffers */
+ wait_queue_head_t wait;
+ struct list_head free;
+ struct list_head used;
+ struct list_head inflight;
+
+ /* Information about data buffers */
+ unsigned int num_dropped;
+ unsigned int num_buffers;
+ size_t bufsize;
+};
+
+struct fpga_reader {
+ struct fpga_device *priv;
+ struct data_buf *buf;
+ off_t buf_start;
+};
+
+#define inode_to_dev(inode) container_of(inode->i_cdev, struct fpga_device, cdev)
+
+/*
+ * Data Buffer Allocation Helpers
+ */
+
+static int data_map_buffer(struct device *dev, struct data_buf *buf)
+{
+ return videobuf_dma_map(dev, &buf->vb);
+}
+
+static void data_unmap_buffer(struct device *dev, struct data_buf *buf)
+{
+ videobuf_dma_unmap(dev, &buf->vb);
+}
+
+/**
+ * data_free_buffer() - free a single data buffer and all allocated memory
+ * @buf: the buffer to free
+ *
+ * This will free all of the pages allocated to the given data buffer, and
+ * then free the structure itself
+ */
+static void data_free_buffer(struct data_buf *buf)
+{
+ /* It is ok to free a NULL buffer */
+ if (!buf)
+ return;
+
+ /* free all memory */
+ videobuf_dma_free(&buf->vb);
+ kfree(buf);
+}
+
+/**
+ * data_alloc_buffer() - allocate and fill a data buffer with pages
+ * @bytes: the number of bytes required
+ *
+ * This allocates all space needed for a data buffer. It must be mapped before
+ * use in a DMA transaction using data_map_buffer().
+ *
+ * Returns NULL on failure
+ */
+static struct data_buf *data_alloc_buffer(const size_t bytes)
+{
+ unsigned int nr_pages;
+ struct data_buf *buf;
+ int ret;
+
+ /* calculate the number of pages necessary */
+ nr_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
+
+ /* allocate the buffer structure */
+ buf = kzalloc(sizeof(*buf), GFP_KERNEL);
+ if (!buf)
+ goto out_return;
+
+ /* initialize internal fields */
+ INIT_LIST_HEAD(&buf->entry);
+ buf->size = bytes;
+
+ /* allocate the videobuf */
+ videobuf_dma_init(&buf->vb);
+ ret = videobuf_dma_init_kernel(&buf->vb, DMA_FROM_DEVICE, nr_pages);
+ if (ret)
+ goto out_free_buf;
+
+ return buf;
+
+out_free_buf:
+ kfree(buf);
+out_return:
+ return NULL;
+}
+
+/**
+ * data_free_buffers() - free all allocated buffers
+ * @priv: the driver's private data structure
+ *
+ * Free all buffers allocated by the driver (except those currently in the
+ * process of being read by userspace).
+ *
+ * LOCKING: must hold dev->mutex
+ * CONTEXT: user
+ */
+static void data_free_buffers(struct fpga_device *priv)
+{
+ struct data_buf *buf, *tmp;
+
+ BUG_ON(!list_empty(&priv->inflight));
+
+ list_for_each_entry_safe(buf, tmp, &priv->free, entry) {
+ list_del_init(&buf->entry);
+ data_unmap_buffer(priv->dev, buf);
+ data_free_buffer(buf);
+ }
+
+ list_for_each_entry_safe(buf, tmp, &priv->used, entry) {
+ list_del_init(&buf->entry);
+ data_unmap_buffer(priv->dev, buf);
+ data_free_buffer(buf);
+ }
+
+ priv->num_buffers = 0;
+ priv->bufsize = 0;
+}
+
+/**
+ * data_alloc_buffers() - allocate 1 seconds worth of data buffers
+ * @priv: the driver's private data structure
+ *
+ * Allocate enough buffers for a whole second worth of data
+ *
+ * This routine will attempt to degrade nicely by succeeding even if a full
+ * second worth of data buffers could not be allocated, as long as a minimum
+ * number were allocated. In this case, it will print a message to the kernel
+ * log.
+ *
+ * The device must not be modifying any lists when this is called.
+ *
+ * CONTEXT: user
+ * LOCKING: must hold dev->mutex
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int data_alloc_buffers(struct fpga_device *priv)
+{
+ struct data_buf *buf;
+ int i, ret;
+
+ for (i = 0; i < MAX_DATA_BUFS; i++) {
+
+ /* allocate a buffer */
+ buf = data_alloc_buffer(priv->bufsize);
+ if (!buf)
+ continue;
+
+ /* map it for DMA */
+ ret = data_map_buffer(priv->dev, buf);
+ if (ret) {
+ data_free_buffer(buf);
+ continue;
+ }
+
+ /* add it to the list of free buffers */
+ list_add_tail(&buf->entry, &priv->free);
+ priv->num_buffers++;
+ }
+
+ /* Make sure we allocated the minimum required number of buffers */
+ if (priv->num_buffers < MIN_DATA_BUFS) {
+ dev_err(priv->dev, "Unable to allocate enough data buffers\n");
+ data_free_buffers(priv);
+ return -ENOMEM;
+ }
+
+ /* Warn if we are running in a degraded state, but do not fail */
+ if (priv->num_buffers < MAX_DATA_BUFS) {
+ dev_warn(priv->dev, "Unable to allocate one second worth of "
+ "buffers, using %d buffers instead\n", i);
+ }
+
+ return 0;
+}
+
+/*
+ * DMA Operations Helpers
+ */
+
+/**
+ * fpga_start_addr() - get the physical address a DATA-FPGA
+ * @priv: the driver's private data structure
+ * @fpga: the DATA-FPGA number (zero based)
+ */
+static dma_addr_t fpga_start_addr(struct fpga_device *priv, unsigned int fpga)
+{
+ return priv->phys_addr + 0x400000 + (0x80000 * fpga);
+}
+
+/**
+ * fpga_block_addr() - get the physical address of a correlation data block
+ * @priv: the driver's private data structure
+ * @fpga: the DATA-FPGA number (zero based)
+ * @blknum: the correlation block number (zero based)
+ */
+static dma_addr_t fpga_block_addr(struct fpga_device *priv, unsigned int fpga,
+ unsigned int blknum)
+{
+ return fpga_start_addr(priv, fpga) + (0x10000 * (1 + blknum));
+}
+
+#define REG_BLOCK_SIZE (32 * 4)
+
+/**
+ * data_setup_corl_table() - create the scatterlist for correlation dumps
+ * @priv: the driver's private data structure
+ *
+ * Create the scatterlist for transferring a correlation dump from the
+ * DATA FPGAs. This structure will be reused for each buffer than needs
+ * to be filled with correlation data.
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int data_setup_corl_table(struct fpga_device *priv)
+{
+ struct sg_table *table = &priv->corl_table;
+ struct scatterlist *sg;
+ struct fpga_info *info;
+ int i, j, ret;
+
+ /* Calculate the number of entries needed */
+ priv->corl_nents = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
+ for (i = 0; i < NUM_FPGA; i++)
+ priv->corl_nents += priv->info[i].num_lag_ram;
+
+ /* Allocate the scatterlist table */
+ ret = sg_alloc_table(table, priv->corl_nents, GFP_KERNEL);
+ if (ret) {
+ dev_err(priv->dev, "unable to allocate DMA table\n");
+ return ret;
+ }
+
+ /* Add the DATA FPGA registers to the scatterlist */
+ sg = table->sgl;
+ for (i = 0; i < NUM_FPGA; i++) {
+ sg_dma_address(sg) = fpga_start_addr(priv, i);
+ sg_dma_len(sg) = REG_BLOCK_SIZE;
+ sg = sg_next(sg);
+ }
+
+ /* Add the SYS-FPGA registers to the scatterlist */
+ sg_dma_address(sg) = SYS_FPGA_BLOCK;
+ sg_dma_len(sg) = REG_BLOCK_SIZE;
+ sg = sg_next(sg);
+
+ /* Add the FPGA correlation data blocks to the scatterlist */
+ for (i = 0; i < NUM_FPGA; i++) {
+ info = &priv->info[i];
+ for (j = 0; j < info->num_lag_ram; j++) {
+ sg_dma_address(sg) = fpga_block_addr(priv, i, j);
+ sg_dma_len(sg) = info->blk_size;
+ sg = sg_next(sg);
+ }
+ }
+
+ /*
+ * All physical addresses and lengths are present in the structure
+ * now. It can be reused for every FPGA DATA interrupt
+ */
+ return 0;
+}
+
+/*
+ * FPGA Register Access Helpers
+ */
+
+static void fpga_write_reg(struct fpga_device *priv, unsigned int fpga,
+ unsigned int reg, u32 val)
+{
+ const int fpga_start = DATA_FPGA_START + (fpga * DATA_FPGA_SIZE);
+ iowrite32be(val, priv->regs + fpga_start + reg);
+}
+
+static u32 fpga_read_reg(struct fpga_device *priv, unsigned int fpga,
+ unsigned int reg)
+{
+ const int fpga_start = DATA_FPGA_START + (fpga * DATA_FPGA_SIZE);
+ return ioread32be(priv->regs + fpga_start + reg);
+}
+
+/**
+ * data_calculate_bufsize() - calculate the data buffer size required
+ * @priv: the driver's private data structure
+ *
+ * Calculate the total buffer size needed to hold a single block
+ * of correlation data
+ *
+ * CONTEXT: user
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int data_calculate_bufsize(struct fpga_device *priv)
+{
+ u32 num_corl, num_lags, num_meta, num_qcnt, num_pack;
+ u32 conf1, conf2, version;
+ u32 num_lag_ram, blk_size;
+ int i;
+
+ /* Each buffer starts with the 5 FPGA register areas */
+ priv->bufsize = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
+
+ /* Read and store the configuration data for each FPGA */
+ for (i = 0; i < NUM_FPGA; i++) {
+ version = fpga_read_reg(priv, i, MMAP_REG_VERSION);
+ conf1 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF1);
+ conf2 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF2);
+
+ /* minor version 2 and later */
+ if ((version & 0x000000FF) >= 2) {
+ num_corl = (conf1 & 0x000000F0) >> 4;
+ num_pack = (conf1 & 0x00000F00) >> 8;
+ num_lags = (conf1 & 0x00FFF000) >> 12;
+ num_meta = (conf1 & 0x7F000000) >> 24;
+ num_qcnt = (conf2 & 0x00000FFF) >> 0;
+ } else {
+ num_corl = (conf1 & 0x000000F0) >> 4;
+ num_pack = 1; /* implied */
+ num_lags = (conf1 & 0x000FFF00) >> 8;
+ num_meta = (conf1 & 0x7FF00000) >> 20;
+ num_qcnt = (conf2 & 0x00000FFF) >> 0;
+ }
+
+ num_lag_ram = (num_corl + num_pack - 1) / num_pack;
+ blk_size = ((num_pack * num_lags) + num_meta + num_qcnt) * 8;
+
+ priv->info[i].num_lag_ram = num_lag_ram;
+ priv->info[i].blk_size = blk_size;
+ priv->bufsize += num_lag_ram * blk_size;
+
+ dev_dbg(priv->dev, "FPGA %d NUM_CORL: %d\n", i, num_corl);
+ dev_dbg(priv->dev, "FPGA %d NUM_PACK: %d\n", i, num_pack);
+ dev_dbg(priv->dev, "FPGA %d NUM_LAGS: %d\n", i, num_lags);
+ dev_dbg(priv->dev, "FPGA %d NUM_META: %d\n", i, num_meta);
+ dev_dbg(priv->dev, "FPGA %d NUM_QCNT: %d\n", i, num_qcnt);
+ dev_dbg(priv->dev, "FPGA %d BLK_SIZE: %d\n", i, blk_size);
+ }
+
+ dev_dbg(priv->dev, "TOTAL BUFFER SIZE: %zu bytes\n", priv->bufsize);
+ return 0;
+}
+
+/*
+ * Interrupt Handling
+ */
+
+/**
+ * data_disable_interrupts() - stop the device from generating interrupts
+ * @priv: the driver's private data structure
+ *
+ * Hide interrupts by switching to GPIO interrupt source
+ *
+ * LOCKING: must hold dev->lock
+ */
+static void data_disable_interrupts(struct fpga_device *priv)
+{
+ /* hide the interrupt by switching the IRQ driver to GPIO */
+ iowrite32be(0x2F, priv->regs + SYS_IRQ_SOURCE_CTL);
+}
+
+/**
+ * data_enable_interrupts() - allow the device to generate interrupts
+ * @priv: the driver's private data structure
+ *
+ * Unhide interrupts by switching to the FPGA interrupt source. At the
+ * same time, clear the DATA-FPGA status registers.
+ *
+ * LOCKING: must hold dev->lock
+ */
+static void data_enable_interrupts(struct fpga_device *priv)
+{
+ /* clear the actual FPGA corl_done interrupt */
+ fpga_write_reg(priv, 0, MMAP_REG_STATUS, 0x0);
+ fpga_write_reg(priv, 1, MMAP_REG_STATUS, 0x0);
+ fpga_write_reg(priv, 2, MMAP_REG_STATUS, 0x0);
+ fpga_write_reg(priv, 3, MMAP_REG_STATUS, 0x0);
+
+ /* flush the writes */
+ fpga_read_reg(priv, 0, MMAP_REG_STATUS);
+
+ /* switch back to the external interrupt source */
+ iowrite32be(0x3F, priv->regs + SYS_IRQ_SOURCE_CTL);
+}
+
+/**
+ * data_dma_cb() - DMAEngine callback for DMA completion
+ * @data: the driver's private data structure
+ *
+ * Complete a DMA transfer from the DATA-FPGA's
+ *
+ * This is called via the DMA callback mechanism, and will handle moving the
+ * completed DMA transaction to the used list, and then wake any processes
+ * waiting for new data
+ *
+ * CONTEXT: any, softirq expected
+ */
+static void data_dma_cb(void *data)
+{
+ struct fpga_device *priv = data;
+ struct data_buf *buf;
+ unsigned long flags;
+
+ spin_lock_irqsave(&priv->lock, flags);
+
+ /* clear the FPGA status and re-enable interrupts */
+ data_enable_interrupts(priv);
+
+ /* If the inflight list is empty, we've got a bug */
+ BUG_ON(list_empty(&priv->inflight));
+
+ /* Grab the first buffer from the inflight list */
+ buf = list_first_entry(&priv->inflight, struct data_buf, entry);
+ list_del_init(&buf->entry);
+
+ /* Add it to the used list */
+ list_add_tail(&buf->entry, &priv->used);
+
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+ /* We've changed both the inflight and used lists, so we need
+ * to wake up any processes that are blocking for those events */
+ wake_up(&priv->wait);
+}
+
+/**
+ * data_submit_dma() - prepare and submit the required DMA to fill a buffer
+ * @priv: the driver's private data structure
+ * @buf: the data buffer
+ *
+ * Prepare and submit the necessary DMA transactions to fill a correlation
+ * data buffer.
+ *
+ * LOCKING: must hold dev->lock
+ * CONTEXT: hardirq only
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int data_submit_dma(struct fpga_device *priv, struct data_buf *buf)
+{
+ struct scatterlist *dst_sg, *src_sg;
+ unsigned int dst_nents, src_nents;
+ struct dma_chan *chan = priv->chan;
+ struct dma_async_tx_descriptor *tx;
+ dma_cookie_t cookie;
+ dma_addr_t dst, src;
+
+ dst_sg = buf->vb.sglist;
+ dst_nents = buf->vb.sglen;
+
+ src_sg = priv->corl_table.sgl;
+ src_nents = priv->corl_nents;
+
+ /*
+ * All buffers passed to this function should be ready and mapped
+ * for DMA already. Therefore, we don't need to do anything except
+ * submit it to the Freescale DMA Engine for processing
+ */
+
+ /* setup the scatterlist to scatterlist transfer */
+ tx = chan->device->device_prep_dma_sg(chan,
+ dst_sg, dst_nents,
+ src_sg, src_nents,
+ 0);
+ if (!tx) {
+ dev_err(priv->dev, "unable to prep scatterlist DMA\n");
+ return -ENOMEM;
+ }
+
+ /* submit the transaction to the DMA controller */
+ cookie = tx->tx_submit(tx);
+ if (dma_submit_error(cookie)) {
+ dev_err(priv->dev, "unable to submit scatterlist DMA\n");
+ return -ENOMEM;
+ }
+
+ /* Prepare the re-read of the SYS-FPGA block */
+ dst = sg_dma_address(dst_sg) + (NUM_FPGA * REG_BLOCK_SIZE);
+ src = SYS_FPGA_BLOCK;
+ tx = chan->device->device_prep_dma_memcpy(chan, dst, src,
+ REG_BLOCK_SIZE,
+ DMA_PREP_INTERRUPT);
+ if (!tx) {
+ dev_err(priv->dev, "unable to prep SYS-FPGA DMA\n");
+ return -ENOMEM;
+ }
+
+ /* Setup the callback */
+ tx->callback = data_dma_cb;
+ tx->callback_param = priv;
+
+ /* submit the transaction to the DMA controller */
+ cookie = tx->tx_submit(tx);
+ if (dma_submit_error(cookie)) {
+ dev_err(priv->dev, "unable to submit SYS-FPGA DMA\n");
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
+#define CORL_DONE 0x1
+#define CORL_ERR 0x2
+
+static irqreturn_t data_irq(int irq, void *dev_id)
+{
+ struct fpga_device *priv = dev_id;
+ struct data_buf *buf;
+ u32 status;
+ int i;
+
+ /* detect spurious interrupts via FPGA status */
+ for (i = 0; i < 4; i++) {
+ status = fpga_read_reg(priv, i, MMAP_REG_STATUS);
+ if (!(status & (CORL_DONE | CORL_ERR))) {
+ dev_err(priv->dev, "spurious irq detected (FPGA)\n");
+ return IRQ_NONE;
+ }
+ }
+
+ /* detect spurious interrupts via raw IRQ pin readback */
+ status = ioread32be(priv->regs + SYS_IRQ_INPUT_DATA);
+ if (status & IRQ_CORL_DONE) {
+ dev_err(priv->dev, "spurious irq detected (IRQ)\n");
+ return IRQ_NONE;
+ }
+
+ spin_lock(&priv->lock);
+
+ /* hide the interrupt by switching the IRQ driver to GPIO */
+ data_disable_interrupts(priv);
+
+ /* Check that we actually have a free buffer */
+ if (list_empty(&priv->free)) {
+ priv->num_dropped++;
+ data_enable_interrupts(priv);
+ goto out_unlock;
+ }
+
+ buf = list_first_entry(&priv->free, struct data_buf, entry);
+ list_del_init(&buf->entry);
+
+ /* Check the buffer size */
+ BUG_ON(buf->size != priv->bufsize);
+
+ /* Submit a DMA transfer to get the correlation data */
+ if (data_submit_dma(priv, buf)) {
+ dev_err(priv->dev, "Unable to setup DMA transfer\n");
+ list_add_tail(&buf->entry, &priv->free);
+ data_enable_interrupts(priv);
+ goto out_unlock;
+ }
+
+ /* DMA setup succeeded, GO!!! */
+ list_add_tail(&buf->entry, &priv->inflight);
+ dma_async_memcpy_issue_pending(priv->chan);
+
+out_unlock:
+ spin_unlock(&priv->lock);
+ return IRQ_HANDLED;
+}
+
+/*
+ * Realtime Device Enable Helpers
+ */
+
+/**
+ * data_device_enable() - enable the device for buffered dumping
+ * @priv: the driver's private data structure
+ *
+ * Enable the device for buffered dumping. Allocates buffers and hooks up
+ * the interrupt handler. When this finishes, data will come pouring in.
+ *
+ * LOCKING: must hold dev->mutex
+ * CONTEXT: user context only
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int data_device_enable(struct fpga_device *priv)
+{
+ u32 val;
+ int ret;
+
+ /* multiple enables are safe: they do nothing */
+ if (priv->enabled)
+ return 0;
+
+ /* check that the FPGAs are programmed */
+ val = ioread32be(priv->regs + SYS_FPGA_CONFIG_STATUS);
+ if (!(val & (1 << 18))) {
+ dev_err(priv->dev, "DATA-FPGAs are not enabled\n");
+ return -ENODATA;
+ }
+
+ /* read the FPGAs to calculate the buffer size */
+ ret = data_calculate_bufsize(priv);
+ if (ret) {
+ dev_err(priv->dev, "unable to calculate buffer size\n");
+ goto out_error;
+ }
+
+ /* allocate the correlation data buffers */
+ ret = data_alloc_buffers(priv);
+ if (ret) {
+ dev_err(priv->dev, "unable to allocate buffers\n");
+ goto out_error;
+ }
+
+ /* setup the source scatterlist for dumping correlation data */
+ ret = data_setup_corl_table(priv);
+ if (ret) {
+ dev_err(priv->dev, "unable to setup correlation DMA table\n");
+ goto out_error;
+ }
+
+ /* switch to the external FPGA IRQ line */
+ data_enable_interrupts(priv);
+
+ /* hookup the irq handler */
+ ret = request_irq(priv->irq, data_irq, IRQF_SHARED, drv_name, priv);
+ if (ret) {
+ dev_err(priv->dev, "unable to request IRQ handler\n");
+ goto out_error;
+ }
+
+ /* success, we're enabled */
+ priv->enabled = true;
+ return 0;
+
+out_error:
+ sg_free_table(&priv->corl_table);
+ priv->corl_nents = 0;
+
+ data_free_buffers(priv);
+ return ret;
+}
+
+/**
+ * data_device_disable() - disable the device for buffered dumping
+ * @priv: the driver's private data structure
+ *
+ * Disable the device for buffered dumping. Stops new DMA transactions from
+ * being generated, waits for all outstanding DMA to complete, and then frees
+ * all buffers.
+ *
+ * LOCKING: must hold dev->mutex
+ * CONTEXT: user only
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int data_device_disable(struct fpga_device *priv)
+{
+ struct list_head *list;
+ int ret;
+
+ /* allow multiple disable */
+ if (!priv->enabled)
+ return 0;
+
+ /* switch to the internal GPIO IRQ line */
+ data_disable_interrupts(priv);
+
+ /* unhook the irq handler */
+ free_irq(priv->irq, priv);
+
+ /*
+ * wait for all outstanding DMA to complete
+ *
+ * Device interrupts are disabled, so no more buffers can be added to
+ * the inflight list. Therefore we do not need any locking.
+ */
+ list = &priv->inflight;
+ while (!list_empty(list)) {
+ ret = wait_event_interruptible(priv->wait, list_empty(list));
+ if (ret)
+ return ret;
+ }
+
+ /* free the correlation table */
+ sg_free_table(&priv->corl_table);
+ priv->corl_nents = 0;
+
+ /* free all of the buffers */
+ data_free_buffers(priv);
+ priv->enabled = false;
+ return 0;
+}
+
+/*
+ * SYSFS Attributes
+ */
+
+/*
+ * Count the number of entries in the given list
+ */
+static unsigned int list_num_entries(struct list_head *list)
+{
+ struct list_head *entry;
+ unsigned int ret = 0;
+
+ list_for_each(entry, list)
+ ret++;
+
+ return ret;
+}
+
+static ssize_t data_num_buffers_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ return snprintf(buf, PAGE_SIZE, "%u\n", priv->num_buffers);
+}
+
+static ssize_t data_bufsize_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ return snprintf(buf, PAGE_SIZE, "%zu\n", priv->bufsize);
+}
+
+static ssize_t data_inflight_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ unsigned int num = list_num_entries(&priv->inflight);
+ return snprintf(buf, PAGE_SIZE, "%u\n", num);
+}
+
+static ssize_t data_free_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ unsigned int num = list_num_entries(&priv->free);
+ return snprintf(buf, PAGE_SIZE, "%u\n", num);
+}
+
+static ssize_t data_used_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ unsigned int num = list_num_entries(&priv->used);
+ return snprintf(buf, PAGE_SIZE, "%u\n", num);
+}
+
+static ssize_t data_num_dropped_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ return snprintf(buf, PAGE_SIZE, "%u\n", priv->num_dropped);
+}
+
+static ssize_t data_en_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ ssize_t count;
+
+ count = mutex_lock_interruptible(&priv->mutex);
+ if (count)
+ return count;
+
+ count = snprintf(buf, PAGE_SIZE, "%u\n", priv->enabled);
+ mutex_unlock(&priv->mutex);
+ return count;
+}
+
+static ssize_t data_en_set(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct fpga_device *priv = dev_get_drvdata(dev);
+ unsigned long enable;
+ int ret;
+
+ ret = strict_strtoul(buf, 0, &enable);
+ if (ret) {
+ dev_err(priv->dev, "unable to parse enable input\n");
+ return -EINVAL;
+ }
+
+ ret = mutex_lock_interruptible(&priv->mutex);
+ if (ret)
+ return ret;
+
+ if (enable)
+ ret = data_device_enable(priv);
+ else
+ ret = data_device_disable(priv);
+
+ if (ret) {
+ dev_err(priv->dev, "device %s failed\n",
+ enable ? "enable" : "disable");
+ count = ret;
+ goto out_unlock;
+ }
+
+out_unlock:
+ mutex_unlock(&priv->mutex);
+ return count;
+}
+
+static DEVICE_ATTR(num_buffers, S_IRUGO, data_num_buffers_show, NULL);
+static DEVICE_ATTR(buffer_size, S_IRUGO, data_bufsize_show, NULL);
+static DEVICE_ATTR(num_inflight, S_IRUGO, data_inflight_show, NULL);
+static DEVICE_ATTR(num_free, S_IRUGO, data_free_show, NULL);
+static DEVICE_ATTR(num_used, S_IRUGO, data_used_show, NULL);
+static DEVICE_ATTR(num_dropped, S_IRUGO, data_num_dropped_show, NULL);
+static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO, data_en_show, data_en_set);
+
+static struct attribute *data_sysfs_attrs[] = {
+ &dev_attr_num_buffers.attr,
+ &dev_attr_buffer_size.attr,
+ &dev_attr_num_inflight.attr,
+ &dev_attr_num_free.attr,
+ &dev_attr_num_used.attr,
+ &dev_attr_num_dropped.attr,
+ &dev_attr_enable.attr,
+ NULL,
+};
+
+static const struct attribute_group rt_sysfs_attr_group = {
+ .attrs = data_sysfs_attrs,
+};
+
+/*
+ * FPGA Realtime Data Character Device
+ */
+
+static int data_open(struct inode *inode, struct file *filp)
+{
+ /*
+ * The miscdevice layer puts our struct miscdevice into the
+ * filp->private_data field. We use this to find our private
+ * data and then overwrite it with our own private structure.
+ */
+ struct fpga_device *priv = container_of(filp->private_data,
+ struct fpga_device, miscdev);
+ struct fpga_reader *reader;
+ int ret;
+
+ /* allocate private data */
+ reader = kzalloc(sizeof(*reader), GFP_KERNEL);
+ if (!reader)
+ return -ENOMEM;
+
+ reader->priv = priv;
+ reader->buf = NULL;
+
+ filp->private_data = reader;
+ ret = nonseekable_open(inode, filp);
+ if (ret) {
+ dev_err(priv->dev, "nonseekable-open failed\n");
+ kfree(reader);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int data_release(struct inode *inode, struct file *filp)
+{
+ struct fpga_reader *reader = filp->private_data;
+
+ /* free the per-reader structure */
+ data_free_buffer(reader->buf);
+ kfree(reader);
+ filp->private_data = NULL;
+ return 0;
+}
+
+static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
+ loff_t *f_pos)
+{
+ struct fpga_reader *reader = filp->private_data;
+ struct fpga_device *priv = reader->priv;
+ struct list_head *used = &priv->used;
+ struct data_buf *dbuf;
+ size_t avail;
+ void *data;
+ int ret;
+
+ /* check if we already have a partial buffer */
+ if (reader->buf) {
+ dbuf = reader->buf;
+ goto have_buffer;
+ }
+
+ spin_lock_irq(&priv->lock);
+
+ /* Block until there is at least one buffer on the used list */
+ while (list_empty(used)) {
+ spin_unlock_irq(&priv->lock);
+
+ if (filp->f_flags & O_NONBLOCK)
+ return -EAGAIN;
+
+ ret = wait_event_interruptible(priv->wait, !list_empty(used));
+ if (ret)
+ return ret;
+
+ spin_lock_irq(&priv->lock);
+ }
+
+ /* Grab the first buffer off of the used list */
+ dbuf = list_first_entry(used, struct data_buf, entry);
+ list_del_init(&dbuf->entry);
+
+ spin_unlock_irq(&priv->lock);
+
+ /* Buffers are always mapped: unmap it */
+ data_unmap_buffer(priv->dev, dbuf);
+
+ /* save the buffer for later */
+ reader->buf = dbuf;
+ reader->buf_start = 0;
+
+ /* we removed a buffer from the used list: wake any waiters */
+ wake_up(&priv->wait);
+
+have_buffer:
+ /* Get the number of bytes available */
+ avail = dbuf->size - reader->buf_start;
+ data = dbuf->vb.vaddr + reader->buf_start;
+
+ /* Get the number of bytes we can transfer */
+ count = min(count, avail);
+
+ /* Copy the data to the userspace buffer */
+ if (copy_to_user(ubuf, data, count))
+ return -EFAULT;
+
+ /* Update the amount of available space */
+ avail -= count;
+
+ /* Lock against concurrent enable/disable */
+ ret = mutex_lock_interruptible(&priv->mutex);
+ if (ret)
+ return ret;
+
+ /* Still some space available: save the buffer for later */
+ if (avail != 0) {
+ reader->buf_start += count;
+ reader->buf = dbuf;
+ goto out_unlock;
+ }
+
+ /*
+ * No space is available in this buffer
+ *
+ * This is a complicated decision:
+ * - if the device is not enabled: free the buffer
+ * - if the buffer is too small: free the buffer
+ */
+ if (!priv->enabled || dbuf->size != priv->bufsize) {
+ data_free_buffer(dbuf);
+ reader->buf = NULL;
+ goto out_unlock;
+ }
+
+ /*
+ * The buffer is safe to recycle: remap it and finish
+ *
+ * If this fails, we pretend that the read never happened, and return
+ * -EFAULT to userspace. They'll retry the read again.
+ */
+ ret = data_map_buffer(priv->dev, dbuf);
+ if (ret) {
+ dev_err(priv->dev, "unable to remap buffer for DMA\n");
+ count = -EFAULT;
+ goto out_unlock;
+ }
+
+ /* Add the buffer back to the free list */
+ reader->buf = NULL;
+ spin_lock_irq(&priv->lock);
+ list_add_tail(&dbuf->entry, &priv->free);
+ spin_unlock_irq(&priv->lock);
+
+out_unlock:
+ mutex_unlock(&priv->mutex);
+ return count;
+}
+
+static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
+{
+ struct fpga_reader *reader = filp->private_data;
+ struct fpga_device *priv = reader->priv;
+ unsigned int mask = 0;
+
+ poll_wait(filp, &priv->wait, tbl);
+
+ spin_lock_irq(&priv->lock);
+
+ if (!list_empty(&priv->used))
+ mask |= POLLIN | POLLRDNORM;
+
+ spin_unlock_irq(&priv->lock);
+ return mask;
+}
+
+static int data_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+ struct fpga_reader *reader = filp->private_data;
+ struct fpga_device *priv = reader->priv;
+ unsigned long offset, vsize, psize, addr;
+
+ /* VMA properties */
+ offset = vma->vm_pgoff << PAGE_SHIFT;
+ vsize = vma->vm_end - vma->vm_start;
+ psize = priv->phys_size - offset;
+ addr = (priv->phys_addr + offset) >> PAGE_SHIFT;
+
+ /* Check against the FPGA region's physical memory size */
+ if (vsize > psize) {
+ dev_err(priv->dev, "requested mmap mapping too large\n");
+ return -EINVAL;
+ }
+
+ /* IO memory (stop cacheing) */
+ vma->vm_flags |= VM_IO | VM_RESERVED;
+ vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+ return io_remap_pfn_range(vma, vma->vm_start, addr, vsize,
+ vma->vm_page_prot);
+}
+
+static const struct file_operations data_fops = {
+ .owner = THIS_MODULE,
+ .open = data_open,
+ .release = data_release,
+ .read = data_read,
+ .poll = data_poll,
+ .mmap = data_mmap,
+ .llseek = no_llseek,
+};
+
+/*
+ * OpenFirmware Device Subsystem
+ */
+
+static bool dma_filter(struct dma_chan *chan, void *data)
+{
+ /*
+ * DMA Channel #0 is used for the FPGA Programmer, so ignore it
+ *
+ * This probably won't survive an unload/load cycle of the Freescale
+ * DMAEngine driver, but that won't be a problem
+ */
+ if (chan->chan_id == 0 && chan->device->dev_id == 0)
+ return false;
+
+ return true;
+}
+
+static int data_of_probe(struct platform_device *op,
+ const struct of_device_id *match)
+{
+ struct device_node *of_node = op->dev.of_node;
+ struct device *this_device;
+ struct fpga_device *priv;
+ struct resource res;
+ dma_cap_mask_t mask;
+ int ret;
+
+ /* Allocate private data */
+ priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+ if (!priv) {
+ dev_err(&op->dev, "Unable to allocate device private data\n");
+ ret = -ENOMEM;
+ goto out_return;
+ }
+
+ dev_set_drvdata(&op->dev, priv);
+ priv->dev = &op->dev;
+
+ /* Setup the misc device */
+ priv->miscdev.minor = MISC_DYNAMIC_MINOR;
+ priv->miscdev.name = drv_name;
+ priv->miscdev.fops = &data_fops;
+
+ /* Get the physical address of the FPGA registers */
+ ret = of_address_to_resource(of_node, 0, &res);
+ if (ret) {
+ dev_err(&op->dev, "Unable to find FPGA physical address\n");
+ ret = -ENODEV;
+ goto out_free_priv;
+ }
+
+ priv->phys_addr = res.start;
+ priv->phys_size = resource_size(&res);
+
+ /* ioremap the registers for use */
+ priv->regs = of_iomap(of_node, 0);
+ if (!priv->regs) {
+ dev_err(&op->dev, "Unable to ioremap registers\n");
+ ret = -ENOMEM;
+ goto out_free_priv;
+ }
+
+ dma_cap_zero(mask);
+ dma_cap_set(DMA_MEMCPY, mask);
+ dma_cap_set(DMA_INTERRUPT, mask);
+ dma_cap_set(DMA_SLAVE, mask);
+ dma_cap_set(DMA_SG, mask);
+
+ /* Request a DMA channel */
+ priv->chan = dma_request_channel(mask, dma_filter, NULL);
+ if (!priv->chan) {
+ dev_err(&op->dev, "Unable to request DMA channel\n");
+ ret = -ENODEV;
+ goto out_unmap_regs;
+ }
+
+ /* Find the correct IRQ number */
+ priv->irq = irq_of_parse_and_map(of_node, 0);
+ if (priv->irq == NO_IRQ) {
+ dev_err(&op->dev, "Unable to find IRQ line\n");
+ ret = -ENODEV;
+ goto out_release_dma;
+ }
+
+ dev_set_drvdata(priv->dev, priv);
+ mutex_init(&priv->mutex);
+ spin_lock_init(&priv->lock);
+ INIT_LIST_HEAD(&priv->free);
+ INIT_LIST_HEAD(&priv->used);
+ INIT_LIST_HEAD(&priv->inflight);
+ init_waitqueue_head(&priv->wait);
+
+ /* Drive the GPIO for FPGA IRQ high (no interrupt) */
+ iowrite32be(IRQ_CORL_DONE, priv->regs + SYS_IRQ_OUTPUT_DATA);
+
+ /* Register the miscdevice */
+ ret = misc_register(&priv->miscdev);
+ if (ret) {
+ dev_err(&op->dev, "Unable to register miscdevice\n");
+ goto out_irq_dispose_mapping;
+ }
+
+ /* Create the sysfs files */
+ this_device = priv->miscdev.this_device;
+ dev_set_drvdata(this_device, priv);
+ ret = sysfs_create_group(&this_device->kobj, &rt_sysfs_attr_group);
+ if (ret) {
+ dev_err(&op->dev, "Unable to create sysfs files\n");
+ goto out_misc_deregister;
+ }
+
+ dev_info(&op->dev, "CARMA FPGA Realtime Data Driver Loaded\n");
+ return 0;
+
+out_misc_deregister:
+ misc_deregister(&priv->miscdev);
+out_irq_dispose_mapping:
+ irq_dispose_mapping(priv->irq);
+out_release_dma:
+ dma_release_channel(priv->chan);
+out_unmap_regs:
+ iounmap(priv->regs);
+out_free_priv:
+ mutex_destroy(&priv->mutex);
+ kfree(priv);
+out_return:
+ return ret;
+}
+
+static int data_of_remove(struct platform_device *op)
+{
+ struct fpga_device *priv = dev_get_drvdata(&op->dev);
+ struct device *this_device = priv->miscdev.this_device;
+
+ /* remove all sysfs files, now the device cannot be re-enabled */
+ sysfs_remove_group(&this_device->kobj, &rt_sysfs_attr_group);
+
+ /* disable the device from generating data */
+ data_device_disable(priv);
+
+ misc_deregister(&priv->miscdev);
+ irq_dispose_mapping(priv->irq);
+ dma_release_channel(priv->chan);
+ iounmap(priv->regs);
+ mutex_destroy(&priv->mutex);
+ kfree(priv);
+
+ return 0;
+}
+
+static struct of_device_id data_of_match[] = {
+ { .compatible = "carma,carma-fpga", },
+ {},
+};
+
+static struct of_platform_driver data_of_driver = {
+ .probe = data_of_probe,
+ .remove = data_of_remove,
+ .driver = {
+ .name = drv_name,
+ .of_match_table = data_of_match,
+ .owner = THIS_MODULE,
+ },
+};
+
+/*
+ * Module Init / Exit
+ */
+
+static int __init data_init(void)
+{
+ return of_register_platform_driver(&data_of_driver);
+}
+
+static void __exit data_exit(void)
+{
+ of_unregister_platform_driver(&data_of_driver);
+}
+
+MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
+MODULE_DESCRIPTION("CARMA DATA-FPGA Access Driver");
+MODULE_LICENSE("GPL");
+
+module_init(data_init);
+module_exit(data_exit);
--
1.7.3.4
^ permalink raw reply related
* [PATCH 2/2] misc: add CARMA DATA-FPGA Programmer support
From: Ira W. Snyder @ 2011-02-08 23:37 UTC (permalink / raw)
To: linuxppc-dev; +Cc: dmitry.torokhov, linux-kernel, Ira W. Snyder
In-Reply-To: <1297208267-27087-1-git-send-email-iws@ovro.caltech.edu>
This adds support for programming the data processing FPGAs on the OVRO
CARMA board. These FPGAs have a special programming sequence that
requires that we program the Freescale DMA engine, which is only
available inside the kernel.
Signed-off-by: Ira W. Snyder <iws@ovro.caltech.edu>
---
drivers/misc/carma/Kconfig | 9 +
drivers/misc/carma/Makefile | 1 +
drivers/misc/carma/carma-fpga-program.c | 1084 +++++++++++++++++++++++++++++++
3 files changed, 1094 insertions(+), 0 deletions(-)
create mode 100644 drivers/misc/carma/carma-fpga-program.c
diff --git a/drivers/misc/carma/Kconfig b/drivers/misc/carma/Kconfig
index 4be183f..e57a9d3 100644
--- a/drivers/misc/carma/Kconfig
+++ b/drivers/misc/carma/Kconfig
@@ -7,3 +7,12 @@ config CARMA_FPGA
Say Y here to include support for communicating with the data
processing FPGAs on the OVRO CARMA board.
+config CARMA_FPGA_PROGRAM
+ tristate "CARMA DATA-FPGA Programmer"
+ depends on FSL_SOC && PPC_83xx && MEDIA_SUPPORT && HAS_DMA && FSL_DMA
+ select VIDEOBUF_DMA_SG
+ default n
+ help
+ Say Y here to include support for programming the data processing
+ FPGAs on the OVRO CARMA board.
+
diff --git a/drivers/misc/carma/Makefile b/drivers/misc/carma/Makefile
index 0b69fa7..ff36ac2 100644
--- a/drivers/misc/carma/Makefile
+++ b/drivers/misc/carma/Makefile
@@ -1 +1,2 @@
obj-$(CONFIG_CARMA_FPGA) += carma-fpga.o
+obj-$(CONFIG_CARMA_FPGA_PROGRAM) += carma-fpga-program.o
diff --git a/drivers/misc/carma/carma-fpga-program.c b/drivers/misc/carma/carma-fpga-program.c
new file mode 100644
index 0000000..ef16cb3
--- /dev/null
+++ b/drivers/misc/carma/carma-fpga-program.c
@@ -0,0 +1,1084 @@
+/*
+ * CARMA Board DATA-FPGA Programmer
+ *
+ * Copyright (c) 2009-2010 Ira W. Snyder <iws@ovro.caltech.edu>
+ *
+ * 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.
+ */
+
+#include <linux/dma-mapping.h>
+#include <linux/of_platform.h>
+#include <linux/completion.h>
+#include <linux/miscdevice.h>
+#include <linux/dmaengine.h>
+#include <linux/interrupt.h>
+#include <linux/highmem.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/leds.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/io.h>
+
+#include <media/videobuf-dma-sg.h>
+
+/* MPC8349EMDS specific get_immrbase() */
+#include <sysdev/fsl_soc.h>
+
+static const char drv_name[] = "carma-fpga-program";
+
+/*
+ * Maximum firmware size
+ *
+ * 12849552 bytes for a CARMA Digitizer Board
+ * 18662880 bytes for a CARMA Correlator Board
+ */
+#define FW_SIZE_EP2S90 12849552
+#define FW_SIZE_EP2S130 18662880
+
+struct fpga_dev {
+ struct miscdevice miscdev;
+
+ /* Device Registers */
+ struct device *dev;
+ void __iomem *regs;
+ void __iomem *immr;
+
+ /* Freescale DMA Device */
+ struct dma_chan *chan;
+
+ /* Interrupts */
+ int irq, status;
+ struct completion completion;
+
+ /* FPGA Bitfile */
+ struct mutex lock;
+
+ struct videobuf_dmabuf vb;
+ bool vb_allocated;
+
+ /* max size and written bytes */
+ size_t fw_size;
+ size_t bytes;
+};
+
+/*
+ * FPGA Bitfile Helpers
+ */
+
+/**
+ * fpga_drop_firmware_data() - drop the bitfile image from memory
+ * @priv: the driver's private data structure
+ *
+ * LOCKING: must hold priv->lock
+ */
+static void fpga_drop_firmware_data(struct fpga_dev *priv)
+{
+ videobuf_dma_free(&priv->vb);
+ priv->vb_allocated = false;
+ priv->bytes = 0;
+}
+
+/*
+ * LED Trigger (could be a seperate module)
+ */
+
+/*
+ * NOTE: this whole thing does have the problem that whenever the led's are
+ * NOTE: first set to use the fpga trigger, they could be in the wrong state
+ */
+
+DEFINE_LED_TRIGGER(ledtrig_fpga);
+
+static void ledtrig_fpga_programmed(bool enabled)
+{
+ if (enabled)
+ led_trigger_event(ledtrig_fpga, LED_FULL);
+ else
+ led_trigger_event(ledtrig_fpga, LED_OFF);
+}
+
+/*
+ * FPGA Register Helpers
+ */
+
+/* Register Definitions */
+#define FPGA_CONFIG_CONTROL 0x40
+#define FPGA_CONFIG_STATUS 0x44
+#define FPGA_CONFIG_FIFO_SIZE 0x48
+#define FPGA_CONFIG_FIFO_USED 0x4C
+#define FPGA_CONFIG_TOTAL_BYTE_COUNT 0x50
+#define FPGA_CONFIG_CUR_BYTE_COUNT 0x54
+
+#define FPGA_FIFO_ADDRESS 0x3000
+
+static int fpga_fifo_size(void __iomem *regs)
+{
+ return ioread32be(regs + FPGA_CONFIG_FIFO_SIZE);
+}
+
+static int fpga_config_error(void __iomem *regs)
+{
+ return ioread32be(regs + FPGA_CONFIG_STATUS) & 0xFFFE;
+}
+
+static int fpga_fifo_empty(void __iomem *regs)
+{
+ return ioread32be(regs + FPGA_CONFIG_FIFO_USED) == 0;
+}
+
+static void fpga_fifo_write(void __iomem *regs, u32 val)
+{
+ iowrite32be(val, regs + FPGA_FIFO_ADDRESS);
+}
+
+static void fpga_set_byte_count(void __iomem *regs, u32 count)
+{
+ iowrite32be(count, regs + FPGA_CONFIG_TOTAL_BYTE_COUNT);
+}
+
+static void fpga_programmer_enable(struct fpga_dev *priv, bool dma)
+{
+ if (dma)
+ iowrite32be(0x5, priv->regs + FPGA_CONFIG_CONTROL);
+ else
+ iowrite32be(0x1, priv->regs + FPGA_CONFIG_CONTROL);
+}
+
+static void fpga_programmer_disable(struct fpga_dev *priv)
+{
+ iowrite32be(0x0, priv->regs + FPGA_CONFIG_CONTROL);
+}
+
+static void fpga_dump_registers(struct fpga_dev *priv)
+{
+ /* good status: do nothing */
+ if (priv->status == 0)
+ return;
+
+ /* Dump all status registers */
+ dev_err(priv->dev, "Configuration failed, dumping status registers\n");
+ dev_err(priv->dev, "Control: 0x%.8x\n", ioread32be(priv->regs + FPGA_CONFIG_CONTROL));
+ dev_err(priv->dev, "Status: 0x%.8x\n", ioread32be(priv->regs + FPGA_CONFIG_STATUS));
+ dev_err(priv->dev, "FIFO Size: 0x%.8x\n", ioread32be(priv->regs + FPGA_CONFIG_FIFO_SIZE));
+ dev_err(priv->dev, "FIFO Used: 0x%.8x\n", ioread32be(priv->regs + FPGA_CONFIG_FIFO_USED));
+ dev_err(priv->dev, "FIFO Total: 0x%.8x\n", ioread32be(priv->regs + FPGA_CONFIG_TOTAL_BYTE_COUNT));
+ dev_err(priv->dev, "FIFO Curr: 0x%.8x\n", ioread32be(priv->regs + FPGA_CONFIG_CUR_BYTE_COUNT));
+}
+
+/*
+ * FPGA Power Supply Code
+ */
+
+#define CTL_PWR_CONTROL 0x2006
+#define CTL_PWR_STATUS 0x200A
+#define CTL_PWR_FAIL 0x200B
+
+/*
+ * Determine if the FPGA power is good for all supplies
+ */
+static bool fpga_power_good(struct fpga_dev *priv)
+{
+ u8 val;
+
+ val = ioread8(priv->regs + CTL_PWR_STATUS);
+ if (val & 0x10)
+ return false;
+
+ return val == 0x0F;
+}
+
+/*
+ * Disable the FPGA power supplies
+ */
+static void fpga_disable_power_supplies(struct fpga_dev *priv)
+{
+ unsigned long start;
+ u8 val;
+
+ iowrite8(0x00, priv->regs + CTL_PWR_CONTROL);
+
+ /*
+ * Wait 500ms for the power rails to discharge
+ *
+ * Without this delay, the CTL-CPLD state machine can get into a
+ * state where it is waiting for the power-goods to assert, but they
+ * never do. This only happens when enabling and disabling the
+ * power sequencer very rapidly.
+ *
+ * The loop below will also wait for the power goods to de-assert,
+ * but testing has shown that they are always disabled by the time
+ * the sleep completes. However, omitting the sleep and only waiting
+ * for the power-goods to de-assert was not sufficient to ensure
+ * that the power sequencer would not wedge itself.
+ */
+ msleep(500);
+
+ start = jiffies;
+ while (time_before(jiffies, start + HZ)) {
+ val = ioread8(priv->regs + CTL_PWR_STATUS);
+ if (!(val & 0x0f))
+ break;
+
+ msleep(10);
+ }
+
+ val = ioread8(priv->regs + CTL_PWR_STATUS);
+ if (val & 0x0f) {
+ dev_err(priv->dev, "power disable failed: "
+ "power goods: status 0x%.2x\n", val);
+ }
+
+ if (val & 0x10) {
+ dev_err(priv->dev, "power disable failed: "
+ "alarm bit set: status 0x%.2x\n", val);
+ }
+}
+
+/**
+ * fpga_enable_power_supplies() - enable the DATA-FPGA power supplies
+ * @priv: the driver's private data structure
+ *
+ * Enable the DATA-FPGA power supplies, waiting up to 1 second for
+ * them to enable successfully.
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int fpga_enable_power_supplies(struct fpga_dev *priv)
+{
+ unsigned long start = jiffies;
+
+ if (fpga_power_good(priv)) {
+ dev_dbg(priv->dev, "power was already good\n");
+ return 0;
+ }
+
+ iowrite8(0x01, priv->regs + CTL_PWR_CONTROL);
+ while (time_before(jiffies, start + HZ)) {
+ if (fpga_power_good(priv))
+ return 0;
+
+ msleep(10);
+ }
+
+ return fpga_power_good(priv) ? 0 : -EBUSY;
+}
+
+/*
+ * Determine if the FPGA power supplies are all enabled
+ */
+static bool fpga_power_enabled(struct fpga_dev *priv)
+{
+ u8 val;
+
+ val = ioread8(priv->regs + CTL_PWR_CONTROL);
+ if (val & 0x01)
+ return true;
+
+ return false;
+}
+
+/*
+ * Determine if the FPGA's are programmed and running correctly
+ */
+static bool fpga_running(struct fpga_dev *priv)
+{
+ if (!fpga_power_good(priv))
+ return false;
+
+ /* Check the config done bit */
+ return ioread32be(priv->regs + FPGA_CONFIG_STATUS) & (1 << 18);
+}
+
+/*
+ * FPGA Programming Code
+ */
+
+/**
+ * fpga_program_block() - put a block of data into the programmer's FIFO
+ * @priv: the driver's private data structure
+ * @buf: the data to program
+ * @count: the length of data to program (must be a multiple of 4 bytes)
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static int fpga_program_block(struct fpga_dev *priv, void *buf, size_t count)
+{
+ u32 *data = buf;
+ int size = fpga_fifo_size(priv->regs);
+ int i, len;
+ unsigned long timeout;
+
+ /* FIXME: BUG_ON instead */
+ WARN_ON_ONCE(count % 4 != 0);
+
+ while (count > 0) {
+
+ /* Get the size of the block to write (maximum is FIFO_SIZE) */
+ len = min_t(size_t, count, size);
+ timeout = jiffies + HZ / 4;
+
+ /* Write the block */
+ for (i = 0; i < len / 4; i++)
+ fpga_fifo_write(priv->regs, data[i]);
+
+ /* Update the amounts left */
+ count -= len;
+ data += len / 4;
+
+ /* Wait for the fifo to empty */
+ while (true) {
+
+ if (fpga_fifo_empty(priv->regs)) {
+ break;
+ } else {
+ dev_dbg(priv->dev, "Fifo not empty\n");
+ cpu_relax();
+ }
+
+ if (fpga_config_error(priv->regs)) {
+ dev_err(priv->dev, "Error detected\n");
+ return -EIO;
+ }
+
+ if (time_after(jiffies, timeout)) {
+ dev_err(priv->dev, "Fifo drain timeout\n");
+ return -ETIMEDOUT;
+ }
+
+ msleep(10);
+ }
+ }
+
+ return 0;
+}
+
+/**
+ * fpga_program_cpu() - program the DATA-FPGA's using the CPU
+ * @priv: the driver's private data structure
+ *
+ * This is useful when the DMA programming method fails. It is possible to
+ * wedge the Freescale DMA controller such that the DMA programming method
+ * always fails. This method has always succeeded.
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static noinline int fpga_program_cpu(struct fpga_dev *priv)
+{
+ int ret;
+
+ /* Disable the programmer */
+ fpga_programmer_disable(priv);
+
+ /* Set the total byte count */
+ fpga_set_byte_count(priv->regs, priv->bytes);
+ dev_dbg(priv->dev, "total byte count %u bytes\n", priv->bytes);
+
+ /* Enable the controller for programming */
+ fpga_programmer_enable(priv, false);
+ dev_dbg(priv->dev, "enabled the controller\n");
+
+ /* Write each chunk of the FPGA bitfile to FPGA programmer */
+ ret = fpga_program_block(priv, priv->vb.vaddr, priv->bytes);
+ if (ret)
+ goto out_disable_controller;
+
+ /* Wait for the interrupt handler to notify us that programming finished */
+ ret = wait_for_completion_timeout(&priv->completion, 2 * HZ);
+ if (!ret) {
+ dev_err(priv->dev, "Timed out waiting for completion\n");
+ ret = -ETIMEDOUT;
+ goto out_disable_controller;
+ }
+
+ /* Retrieve the status from the interrupt handler */
+ ret = priv->status;
+
+out_disable_controller:
+ fpga_programmer_disable(priv);
+ return ret;
+}
+
+/**
+ * fpga_program_dma() - program the DATA-FPGA's using the DMA engine
+ * @priv: the driver's private data structure
+ *
+ * Program the DATA-FPGA's using the Freescale DMA engine. This requires that
+ * the engine is programmed such that the hardware DMA request lines can
+ * control the entire DMA transaction. The system controller FPGA then
+ * completely offloads the programming from the CPU.
+ *
+ * Returns 0 on success, -ERRNO otherwise
+ */
+static noinline int fpga_program_dma(struct fpga_dev *priv)
+{
+ struct videobuf_dmabuf *vb = &priv->vb;
+ struct dma_chan *chan = priv->chan;
+ struct dma_async_tx_descriptor *tx;
+ size_t num_pages, len, avail = 0;
+ struct dma_slave_config config;
+ struct scatterlist *sg;
+ struct sg_table table;
+ dma_cookie_t cookie;
+ int ret, i;
+
+ /* Disable the programmer */
+ fpga_programmer_disable(priv);
+
+ /* Allocate a scatterlist for the DMA destination */
+ num_pages = DIV_ROUND_UP(priv->bytes, 0x1000);
+ ret = sg_alloc_table(&table, num_pages, GFP_KERNEL);
+ if (ret) {
+ dev_err(priv->dev, "Unable to allocate dst scatterlist\n");
+ ret = -ENOMEM;
+ goto out_return;
+ }
+
+ /*
+ * This is an ugly hack
+ *
+ * We fill in a scatterlist as if it were mapped for DMA. This is
+ * necessary because there exists no better structure for this
+ * inside the kernel code.
+ *
+ * As an added bonus, we can use the DMAEngine API for all of this,
+ * rather than inventing another extremely similar API.
+ */
+ avail = priv->bytes;
+ for_each_sg(table.sgl, sg, num_pages, i) {
+ len = min_t(size_t, avail, 0x1000);
+ sg_dma_address(sg) = 0xf0003000;
+ sg_dma_len(sg) = len;
+
+ avail -= len;
+ }
+
+ /* Map the buffer for DMA */
+ ret = videobuf_dma_map(priv->dev, &priv->vb);
+ if (ret) {
+ dev_err(priv->dev, "Unable to map buffer for DMA\n");
+ goto out_free_table;
+ }
+
+ /*
+ * Configure the DMA channel to transfer FIFO_SIZE / 2 bytes per
+ * transaction, and then put it under external control
+ */
+ memset(&config, 0, sizeof(config));
+ config.direction = DMA_TO_DEVICE;
+ config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+ config.dst_maxburst = fpga_fifo_size(priv->regs) / 2 / 4;
+ ret = chan->device->device_control(chan, DMA_SLAVE_CONFIG,
+ (unsigned long)&config);
+ if (ret) {
+ dev_err(priv->dev, "DMA slave configuration failed\n");
+ goto out_dma_unmap;
+ }
+
+ ret = chan->device->device_control(chan, FSLDMA_EXTERNAL_START, 1);
+ if (ret) {
+ dev_err(priv->dev, "DMA external control setup failed\n");
+ goto out_dma_unmap;
+ }
+
+ /* setup and submit the DMA transaction */
+ tx = chan->device->device_prep_dma_sg(chan,
+ table.sgl, num_pages,
+ vb->sglist, vb->sglen, 0);
+ if (!tx) {
+ dev_err(priv->dev, "Unable to prep DMA transaction\n");
+ ret = -ENOMEM;
+ goto out_dma_unmap;
+ }
+
+ cookie = tx->tx_submit(tx);
+ if (dma_submit_error(cookie)) {
+ dev_err(priv->dev, "Unable to submit DMA transaction\n");
+ ret = -ENOMEM;
+ goto out_dma_unmap;
+ }
+
+ dma_async_memcpy_issue_pending(chan);
+
+ /* Set the total byte count */
+ fpga_set_byte_count(priv->regs, priv->bytes);
+ dev_dbg(priv->dev, "total byte count %u bytes\n", priv->bytes);
+
+ /* Enable the controller for DMA programming */
+ fpga_programmer_enable(priv, true);
+ dev_dbg(priv->dev, "enabled the controller\n");
+
+ /* Wait for the interrupt handler to notify us that programming finished */
+ ret = wait_for_completion_timeout(&priv->completion, 2 * HZ);
+ if (!ret) {
+ dev_err(priv->dev, "Timed out waiting for completion\n");
+ ret = -ETIMEDOUT;
+ goto out_disable_controller;
+ }
+
+ /* Retrieve the status from the interrupt handler */
+ ret = priv->status;
+
+out_disable_controller:
+ fpga_programmer_disable(priv);
+out_dma_unmap:
+ videobuf_dma_unmap(priv->dev, vb);
+out_free_table:
+ sg_free_table(&table);
+out_return:
+ return ret;
+}
+
+/*
+ * Interrupt Handling
+ */
+
+static irqreturn_t fpga_interrupt(int irq, void *dev_id)
+{
+ struct fpga_dev *priv = dev_id;
+
+ /* Save the status */
+ priv->status = fpga_config_error(priv->regs) ? -EIO : 0;
+ dev_dbg(priv->dev, "INTERRUPT status %d\n", priv->status);
+ fpga_dump_registers(priv);
+
+ /* Disabling the programmer clears the interrupt */
+ fpga_programmer_disable(priv);
+
+ /* Notify any waiters */
+ complete(&priv->completion);
+
+ return IRQ_HANDLED;
+}
+
+/*
+ * SYSFS Helpers
+ */
+
+/**
+ * fpga_do_stop() - deconfigure (reset) the DATA-FPGA's
+ * @priv: the driver's private data structure
+ *
+ * LOCKING: must hold priv->lock
+ */
+static int fpga_do_stop(struct fpga_dev *priv)
+{
+ /* Set the led to unprogrammed */
+ ledtrig_fpga_programmed(false);
+
+ /* Pulse the config line to reset the FPGA's */
+ iowrite32be(0x3, priv->regs + FPGA_CONFIG_CONTROL);
+ iowrite32be(0x0, priv->regs + FPGA_CONFIG_CONTROL);
+
+ return 0;
+}
+
+static noinline int fpga_do_program(struct fpga_dev *priv)
+{
+ int ret;
+
+ if (priv->bytes != priv->fw_size) {
+ dev_err(priv->dev, "Incorrect bitfile size: got %zu bytes, "
+ "should be %zu bytes\n",
+ priv->bytes, priv->fw_size);
+ return -EINVAL;
+ }
+
+ if (!fpga_power_enabled(priv)) {
+ dev_err(priv->dev, "Power not enabled\n");
+ return -EINVAL;
+ }
+
+ if (!fpga_power_good(priv)) {
+ dev_err(priv->dev, "Power not good\n");
+ return -EINVAL;
+ }
+
+ /* Set the LED to unprogrammed */
+ ledtrig_fpga_programmed(false);
+
+ /* Try to program the FPGA's using DMA */
+ ret = fpga_program_dma(priv);
+
+ /* If DMA failed or doesn't exist, try with CPU */
+ if (ret) {
+ dev_warn(priv->dev, "Falling back to CPU programming\n");
+ ret = fpga_program_cpu(priv);
+ }
+
+ if (ret) {
+ dev_err(priv->dev, "Unable to program FPGA's\n");
+ return ret;
+ }
+
+ /* Drop the firmware bitfile from memory */
+ fpga_drop_firmware_data(priv);
+
+ dev_dbg(priv->dev, "FPGA programming successful\n");
+ ledtrig_fpga_programmed(true);
+
+ return 0;
+}
+
+/*
+ * File Operations
+ */
+
+static int fpga_open(struct inode *inode, struct file *filp)
+{
+ /*
+ * The miscdevice layer puts our struct miscdevice into the
+ * filp->private_data field. We use this to find our private
+ * data and then overwrite it with our own private structure.
+ */
+ struct fpga_dev *priv = container_of(filp->private_data,
+ struct fpga_dev, miscdev);
+ unsigned int nr_pages;
+ int ret;
+
+ /* We only allow one process at a time */
+ if (mutex_lock_interruptible(&priv->lock))
+ return -ERESTARTSYS;
+
+ filp->private_data = priv;
+
+ /* Truncation: drop any existing data */
+ if (filp->f_flags & O_TRUNC)
+ priv->bytes = 0;
+
+ /* Check if we have already allocated a buffer */
+ if (priv->vb_allocated)
+ return 0;
+
+ /* Allocate a buffer to hold enough data for the bitfile */
+ nr_pages = DIV_ROUND_UP(priv->fw_size, PAGE_SIZE);
+ ret = videobuf_dma_init_kernel(&priv->vb, DMA_TO_DEVICE, nr_pages);
+ if (ret) {
+ dev_err(priv->dev, "unable to allocate data buffer\n");
+ mutex_unlock(&priv->lock);
+ return ret;
+ }
+
+ priv->vb_allocated = true;
+ return 0;
+}
+
+static int fpga_release(struct inode *inode, struct file *filp)
+{
+ struct fpga_dev *priv = filp->private_data;
+
+ mutex_unlock(&priv->lock);
+ return 0;
+}
+
+static ssize_t fpga_write(struct file *filp, const char __user *buf,
+ size_t count, loff_t *f_pos)
+{
+ struct fpga_dev *priv = filp->private_data;
+
+ /* FPGA bitfiles have an exact size: disallow anything else */
+ if (priv->bytes >= priv->fw_size)
+ return -ENOSPC;
+
+ count = min_t(size_t, priv->fw_size - priv->bytes, count);
+ if (copy_from_user(priv->vb.vaddr + priv->bytes, buf, count))
+ return -EFAULT;
+
+ priv->bytes += count;
+ return count;
+}
+
+static ssize_t fpga_read(struct file *filp, char __user *buf, size_t count,
+ loff_t *f_pos)
+{
+ struct fpga_dev *priv = filp->private_data;
+
+ count = min_t(size_t, priv->bytes - *f_pos, count);
+ if (copy_to_user(buf, priv->vb.vaddr + *f_pos, count))
+ return -EFAULT;
+
+ *f_pos += count;
+ return count;
+}
+
+static loff_t fpga_llseek(struct file *filp, loff_t offset, int origin)
+{
+ struct fpga_dev *priv = filp->private_data;
+ loff_t newpos;
+
+ /* only read-only opens are allowed to seek */
+ if ((filp->f_flags & O_ACCMODE) != O_RDONLY)
+ return -EINVAL;
+
+ switch (origin) {
+ case SEEK_SET: /* seek relative to the beginning of the file */
+ newpos = offset;
+ break;
+ case SEEK_CUR: /* seek relative to current position in the file */
+ newpos = filp->f_pos + offset;
+ break;
+ case SEEK_END: /* seek relative to the end of the file */
+ newpos = priv->fw_size - offset;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ /* check for sanity */
+ if (newpos > priv->fw_size)
+ return -EINVAL;
+
+ filp->f_pos = newpos;
+ return newpos;
+}
+
+static const struct file_operations fpga_fops = {
+ .open = fpga_open,
+ .release = fpga_release,
+ .write = fpga_write,
+ .read = fpga_read,
+ .llseek = fpga_llseek,
+};
+
+/*
+ * Device Attributes
+ */
+
+static ssize_t pfail_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct fpga_dev *priv = dev_get_drvdata(dev);
+ u8 val;
+
+ val = ioread8(priv->regs + CTL_PWR_FAIL);
+ return snprintf(buf, PAGE_SIZE, "0x%.2x\n", val);
+}
+
+static ssize_t pgood_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct fpga_dev *priv = dev_get_drvdata(dev);
+ return snprintf(buf, PAGE_SIZE, "%d\n", fpga_power_good(priv));
+}
+
+static ssize_t penable_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct fpga_dev *priv = dev_get_drvdata(dev);
+ return snprintf(buf, PAGE_SIZE, "%d\n", fpga_power_enabled(priv));
+}
+
+static ssize_t penable_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct fpga_dev *priv = dev_get_drvdata(dev);
+ unsigned long val;
+ int ret;
+
+ if (strict_strtoul(buf, 0, &val))
+ return -EINVAL;
+
+ if (val) {
+ ret = fpga_enable_power_supplies(priv);
+ if (ret)
+ return ret;
+ } else {
+ fpga_do_stop(priv);
+ fpga_disable_power_supplies(priv);
+ }
+
+ return count;
+}
+
+static ssize_t program_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct fpga_dev *priv = dev_get_drvdata(dev);
+ return snprintf(buf, PAGE_SIZE, "%d\n", fpga_running(priv));
+}
+
+static ssize_t program_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct fpga_dev *priv = dev_get_drvdata(dev);
+ unsigned long val;
+ int ret;
+
+ if (strict_strtoul(buf, 0, &val))
+ return -EINVAL;
+
+ /* We can't have an image writer and be programming simultaneously */
+ if (mutex_lock_interruptible(&priv->lock))
+ return -ERESTARTSYS;
+
+ /* Program or Reset the FPGA's */
+ ret = val ? fpga_do_program(priv) : fpga_do_stop(priv);
+ if (ret)
+ goto out_unlock;
+
+ /* Success */
+ ret = count;
+
+out_unlock:
+ mutex_unlock(&priv->lock);
+ return ret;
+}
+
+static DEVICE_ATTR(power_fail, S_IRUGO, pfail_show, NULL);
+static DEVICE_ATTR(power_good, S_IRUGO, pgood_show, NULL);
+static DEVICE_ATTR(power_enable, S_IRUGO | S_IWUGO, penable_show, penable_store);
+static DEVICE_ATTR(program, S_IRUGO | S_IWUGO, program_show, program_store);
+
+static struct attribute *fpga_attributes[] = {
+ &dev_attr_power_fail.attr,
+ &dev_attr_power_good.attr,
+ &dev_attr_power_enable.attr,
+ &dev_attr_program.attr,
+ NULL,
+};
+
+static const struct attribute_group fpga_attr_group = {
+ .attrs = fpga_attributes,
+};
+
+/*
+ * OpenFirmware Device Subsystem
+ */
+
+#define SYS_REG_VERSION 0x00
+#define SYS_REG_GEOGRAPHIC 0x10
+
+static bool dma_filter(struct dma_chan *chan, void *data)
+{
+ /*
+ * DMA Channel #0 is the only acceptable device
+ *
+ * This probably won't survive an unload/load cycle of the Freescale
+ * DMAEngine driver, but that won't be a problem
+ */
+ return chan->chan_id == 0 && chan->device->dev_id == 0;
+}
+
+static int fpga_of_remove(struct platform_device *op)
+{
+ struct fpga_dev *priv = dev_get_drvdata(&op->dev);
+ struct device *this_device = priv->miscdev.this_device;
+
+ sysfs_remove_group(&this_device->kobj, &fpga_attr_group);
+ misc_deregister(&priv->miscdev);
+
+ free_irq(priv->irq, priv);
+ iounmap(priv->immr);
+
+ fpga_disable_power_supplies(priv);
+ iounmap(priv->regs);
+
+ dma_release_channel(priv->chan);
+
+ /* Free any firmware image that has not been programmed */
+ fpga_drop_firmware_data(priv);
+
+ mutex_destroy(&priv->lock);
+ kfree(priv);
+
+ return 0;
+}
+
+static int fpga_of_probe(struct platform_device *op, const struct of_device_id *match)
+{
+ struct device_node *of_node = op->dev.of_node;
+ struct device *this_device;
+ struct fpga_dev *priv;
+ dma_cap_mask_t mask;
+ u32 ver;
+ int ret;
+
+ /* Allocate private data */
+ priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+ if (!priv) {
+ dev_err(&op->dev, "Unable to allocate private data\n");
+ ret = -ENOMEM;
+ goto out_return;
+ }
+
+ /* Setup the miscdevice */
+ priv->miscdev.minor = MISC_DYNAMIC_MINOR;
+ priv->miscdev.name = drv_name;
+ priv->miscdev.fops = &fpga_fops;
+
+ dev_set_drvdata(&op->dev, priv);
+ priv->dev = &op->dev;
+ mutex_init(&priv->lock);
+ init_completion(&priv->completion);
+ videobuf_dma_init(&priv->vb);
+
+ dev_set_drvdata(priv->dev, priv);
+ dma_cap_zero(mask);
+ dma_cap_set(DMA_MEMCPY, mask);
+ dma_cap_set(DMA_INTERRUPT, mask);
+ dma_cap_set(DMA_SLAVE, mask);
+ dma_cap_set(DMA_SG, mask);
+
+ /* Get control of DMA channel #0 */
+ priv->chan = dma_request_channel(mask, dma_filter, NULL);
+ if (!priv->chan) {
+ dev_err(&op->dev, "Unable to acquire DMA channel #0\n");
+ ret = -ENODEV;
+ goto out_free_priv;
+ }
+
+ /* Remap the registers for use */
+ priv->regs = of_iomap(of_node, 0);
+ if (!priv->regs) {
+ dev_err(&op->dev, "Unable to ioremap registers\n");
+ ret = -ENOMEM;
+ goto out_dma_release_channel;
+ }
+
+ /* Remap the IMMR for use */
+ priv->immr = ioremap(get_immrbase(), 0x100000);
+ if (!priv->immr) {
+ dev_err(&op->dev, "Unable to ioremap IMMR\n");
+ ret = -ENOMEM;
+ goto out_unmap_regs;
+ }
+
+ /*
+ * Check that external DMA is configured
+ *
+ * U-Boot does this for us, but we should check it and bail out if
+ * there is a problem. Failing to have this register setup correctly
+ * will cause the DMA controller to transfer a single cacheline
+ * worth of data, then wedge itself.
+ */
+ if ((ioread32be(priv->immr + 0x114) & 0xE00) != 0xE00) {
+ dev_err(&op->dev, "External DMA control not configured\n");
+ ret = -ENODEV;
+ goto out_unmap_immr;
+ }
+
+ /*
+ * Check the CTL-CPLD version
+ *
+ * This driver uses the CTL-CPLD DATA-FPGA power sequencer, and we
+ * don't want to run on any version of the CTL-CPLD that does not use
+ * a compatible register layout.
+ *
+ * v2: changed register layout, added power sequencer
+ * v3: added glitch filter on the i2c overcurrent/overtemp outputs
+ */
+ ver = ioread8(priv->regs + 0x2000);
+ if (ver != 0x02 && ver != 0x03) {
+ dev_err(&op->dev, "CTL-CPLD is not version 0x02 or 0x03!\n");
+ ret = -ENODEV;
+ goto out_unmap_immr;
+ }
+
+ /* Set the exact size that the firmware image should be */
+ ver = ioread32be(priv->regs + SYS_REG_VERSION);
+ priv->fw_size = (ver & (1 << 18)) ? FW_SIZE_EP2S130 : FW_SIZE_EP2S90;
+
+ /* Find the correct IRQ number */
+ priv->irq = irq_of_parse_and_map(of_node, 0);
+ if (priv->irq == NO_IRQ) {
+ dev_err(&op->dev, "Unable to find IRQ line\n");
+ ret = -ENODEV;
+ goto out_unmap_immr;
+ }
+
+ /* Request the IRQ */
+ ret = request_irq(priv->irq, fpga_interrupt, IRQF_SHARED, drv_name, priv);
+ if (ret) {
+ dev_err(&op->dev, "Unable to request IRQ %d\n", priv->irq);
+ ret = -ENODEV;
+ goto out_irq_dispose_mapping;
+ }
+
+ /* Reset and stop the FPGA's, just in case */
+ fpga_do_stop(priv);
+
+ /* Register the miscdevice */
+ ret = misc_register(&priv->miscdev);
+ if (ret) {
+ dev_err(&op->dev, "Unable to register miscdevice\n");
+ goto out_free_irq;
+ }
+
+ /* Create the sysfs files */
+ this_device = priv->miscdev.this_device;
+ dev_set_drvdata(this_device, priv);
+ ret = sysfs_create_group(&this_device->kobj, &fpga_attr_group);
+ if (ret) {
+ dev_err(&op->dev, "Unable to create sysfs files\n");
+ goto out_misc_deregister;
+ }
+
+ dev_info(priv->dev, "CARMA FPGA Programmer: %s rev%s with %s FPGAs\n",
+ (ver & (1 << 17)) ? "Correlator" : "Digitizer",
+ (ver & (1 << 16)) ? "B" : "A",
+ (ver & (1 << 18)) ? "EP2S130" : "EP2S90");
+
+ return 0;
+
+out_misc_deregister:
+ misc_deregister(&priv->miscdev);
+out_free_irq:
+ free_irq(priv->irq, priv);
+out_irq_dispose_mapping:
+ irq_dispose_mapping(priv->irq);
+out_unmap_immr:
+ iounmap(priv->immr);
+out_unmap_regs:
+ iounmap(priv->regs);
+out_dma_release_channel:
+ dma_release_channel(priv->chan);
+out_free_priv:
+ mutex_destroy(&priv->lock);
+ kfree(priv);
+out_return:
+ return ret;
+}
+
+static struct of_device_id fpga_of_match[] = {
+ { .compatible = "carma,fpga-programmer", },
+ {},
+};
+
+static struct of_platform_driver fpga_of_driver = {
+ .probe = fpga_of_probe,
+ .remove = fpga_of_remove,
+ .driver = {
+ .name = drv_name,
+ .of_match_table = fpga_of_match,
+ .owner = THIS_MODULE,
+ },
+};
+
+/*
+ * Module Init / Exit
+ */
+
+static int __init fpga_init(void)
+{
+ led_trigger_register_simple("fpga", &ledtrig_fpga);
+ return of_register_platform_driver(&fpga_of_driver);
+}
+
+static void __exit fpga_exit(void)
+{
+ of_unregister_platform_driver(&fpga_of_driver);
+ led_trigger_unregister_simple(ledtrig_fpga);
+}
+
+MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
+MODULE_DESCRIPTION("CARMA Board DATA-FPGA Programmer");
+MODULE_LICENSE("GPL");
+
+module_init(fpga_init);
+module_exit(fpga_exit);
--
1.7.3.4
^ permalink raw reply related
* [PATCH] [v2] watchdog: booke_wdt: clean up status messages
From: Timur Tabi @ 2011-02-08 23:39 UTC (permalink / raw)
To: benh, linuxppc-dev, wim, linux-watchdog
Improve the status messages that are displayed during some operations of the
PowerPC watchdog timer driver. When the watchdog is enabled, the timeout is
displayed as a number of seconds, instead of an obscure "period". The "period"
is the position of a bit in a 64-bit timer register. The higher the value,
the quicker the watchdog timeout occurs. Some people chose a high "period"
value for the timer and get confused as to why the board resets within a
few seconds.
Messages displayed during open and close are now debug messages, so that they
don't clutter the console by default. Finally, printk() is replaced with the
pr_xxx() equivalent.
Signed-off-by: Timur Tabi <timur@freescale.com>
---
drivers/watchdog/booke_wdt.c | 19 +++++++++----------
1 files changed, 9 insertions(+), 10 deletions(-)
diff --git a/drivers/watchdog/booke_wdt.c b/drivers/watchdog/booke_wdt.c
index 7e7ec9c..337265b 100644
--- a/drivers/watchdog/booke_wdt.c
+++ b/drivers/watchdog/booke_wdt.c
@@ -4,7 +4,7 @@
* Author: Matthew McClintock
* Maintainer: Kumar Gala <galak@kernel.crashing.org>
*
- * Copyright 2005, 2008, 2010 Freescale Semiconductor Inc.
+ * Copyright 2005, 2008, 2010-2011 Freescale Semiconductor Inc.
*
* 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
@@ -221,9 +221,8 @@ static int booke_wdt_open(struct inode *inode, struct file *file)
if (booke_wdt_enabled == 0) {
booke_wdt_enabled = 1;
on_each_cpu(__booke_wdt_enable, NULL, 0);
- printk(KERN_INFO
- "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
- booke_wdt_period);
+ pr_debug("booke_wdt: watchdog enabled (timeout = %llu sec)\n",
+ period_to_sec(booke_wdt_period));
}
spin_unlock(&booke_wdt_lock);
@@ -240,6 +239,7 @@ static int booke_wdt_release(struct inode *inode, struct file *file)
*/
on_each_cpu(__booke_wdt_disable, NULL, 0);
booke_wdt_enabled = 0;
+ pr_debug("booke_wdt: watchdog disabled\n");
#endif
clear_bit(0, &wdt_is_active);
@@ -271,21 +271,20 @@ static int __init booke_wdt_init(void)
{
int ret = 0;
- printk(KERN_INFO "PowerPC Book-E Watchdog Timer Loaded\n");
+ pr_info("booke_wdt: powerpc book-e watchdog driver loaded\n");
ident.firmware_version = cur_cpu_spec->pvr_value;
ret = misc_register(&booke_wdt_miscdev);
if (ret) {
- printk(KERN_CRIT "Cannot register miscdev on minor=%d: %d\n",
- WATCHDOG_MINOR, ret);
+ pr_err("booke_wdt: cannot register device (minor=%u, ret=%i)\n",
+ WATCHDOG_MINOR, ret);
return ret;
}
spin_lock(&booke_wdt_lock);
if (booke_wdt_enabled == 1) {
- printk(KERN_INFO
- "PowerPC Book-E Watchdog Timer Enabled (wdt_period=%d)\n",
- booke_wdt_period);
+ pr_info("booke_wdt: watchdog enabled (timeout = %llu sec)\n",
+ period_to_sec(booke_wdt_period));
on_each_cpu(__booke_wdt_enable, NULL, 0);
}
spin_unlock(&booke_wdt_lock);
--
1.7.3.4
^ permalink raw reply related
* Re: [PATCH] sched: provide scheduler_ipi() callback in response to smp_send_reschedule()
From: Benjamin Herrenschmidt @ 2011-02-09 6:14 UTC (permalink / raw)
To: Peter Zijlstra
Cc: linux-m32r-ja, linux-mips, linux-ia64, linux-sh, H. Peter Anvin,
Heiko Carstens, Paul Mackerras, Helge Deller, sparclinux,
Linux-Arch, linux-s390, Jesper Nilsson, Jeremy Fitzhardinge,
Russell King, Hirokazu Takata, x86, James E.J. Bottomley,
virtualization, Ingo Molnar, Matt Turner, Fenghua Yu,
Mike Frysinger, user-mode-linux-devel, Konrad Rzeszutek Wilk,
Jeff Dike, Chris Metcalf, xen-devel, Mikael Starvik, linux-m32r,
Ivan Kokshaysky, user-mode-linux-user, uclinux-dist-devel,
Thomas Gleixner, linux-arm-kernel, Richard Henderson, Tony Luck,
linux-parisc, linux-cris-kernel, linux-am33-list, linux-kernel,
Ralf Baechle, Kyle McMartin, Paul Mundt, linux-alpha,
Martin Schwidefsky, linux390, Koichi Yasutake, linuxppc-dev,
David S. Miller
In-Reply-To: <1297086859.13327.16.camel@laptop>
On Mon, 2011-02-07 at 14:54 +0100, Peter Zijlstra wrote:
> On Mon, 2011-02-07 at 10:26 +1100, Benjamin Herrenschmidt wrote:
> > You missed:
> >
> > diff --git a/arch/powerpc/kernel/smp.c b/arch/powerpc/kernel/smp.c
> > index 9813605..467d122 100644
> > --- a/arch/powerpc/kernel/smp.c
> > +++ b/arch/powerpc/kernel/smp.c
> > @@ -98,6 +98,7 @@ void smp_message_recv(int msg)
> > break;
> > case PPC_MSG_RESCHEDULE:
> > /* we notice need_resched on exit */
> > + scheduler_ipi();
> > break;
> > case PPC_MSG_CALL_FUNC_SINGLE:
> > generic_smp_call_function_single_interrupt();
> >
> > Fold that in and add:
> >
> > Acked-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
>
> Thanks Ben!
BTW. I we lurking at some of our CPU hotplug code and I think I cannot
totally guarantee that this won't be called on an offline CPU. If that's
a problem, you may want to add a test for that.
IE. The call function IPIs are normally not going to be sent to an
offlined CPU, and stop_machine should be a good enough fence here, but
we do abuse reschedule for a number of things (including in some case
to wake up a sleeping CPU that was pseudo-offlined :-)
Cheers,
Ben.
^ permalink raw reply
page: next (older) | prev (newer) | latest
- recent:[subjects (threaded)|topics (new)|topics (active)]
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox