linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: vinod.koul@intel.com (Vinod Koul)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH 04/18] dmaengine: st_fdma: Add xp70 firmware loading mechanism.
Date: Tue, 26 Apr 2016 22:30:48 +0530	[thread overview]
Message-ID: <20160426170048.GS2274@localhost> (raw)
In-Reply-To: <1461236675-10176-5-git-send-email-peter.griffin@linaro.org>

On Thu, Apr 21, 2016 at 12:04:21PM +0100, Peter Griffin wrote:
> +static int
> +st_fdma_elf_sanity_check(struct st_fdma_dev *fdev, const struct firmware *fw)
> +{
> +	const char *fw_name = fdev->fw_name;
> +	struct elf32_hdr *ehdr;
> +	char class;
> +
> +	if (!fw) {
> +		dev_err(fdev->dev, "failed to load %s\n", fw_name);
> +		return -EINVAL;
> +	}
> +
> +	if (fw->size < sizeof(*ehdr)) {
> +		dev_err(fdev->dev, "Image is too small\n");
> +		return -EINVAL;
> +	}
> +
> +	ehdr = (struct elf32_hdr *)fw->data;
> +
> +	/* We only support ELF32 at this point */
> +	class = ehdr->e_ident[EI_CLASS];
> +	if (class != ELFCLASS32) {
> +		dev_err(fdev->dev, "Unsupported class: %d\n", class);
> +		return -EINVAL;
> +	}
> +
> +	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
> +		dev_err(fdev->dev, "Unsupported firmware endianness"
> +			"(%d) expected (%d)\n", ehdr->e_ident[EI_DATA],
> +			ELFDATA2LSB);
> +		return -EINVAL;
> +	}
> +
> +	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
> +		dev_err(fdev->dev, "Image is too small (%u)\n", fw->size);
> +		return -EINVAL;
> +	}
> +
> +	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
> +		dev_err(fdev->dev, "Image is corrupted (bad magic)\n");
> +		return -EINVAL;
> +	}
> +
> +	if (ehdr->e_phnum != fdev->drvdata->num_mem) {
> +		dev_err(fdev->dev, "spurious nb of segments (%d) expected (%d)"
> +			"\n", ehdr->e_phnum, fdev->drvdata->num_mem);
> +		return -EINVAL;
> +	}
> +
> +	if (ehdr->e_type != ET_EXEC) {
> +		dev_err(fdev->dev, "Unsupported ELF header type (%d) expected"
> +			" (%d)\n", ehdr->e_type, ET_EXEC);
> +		return -EINVAL;
> +	}
> +
> +	if (ehdr->e_machine != EM_SLIM) {
> +		dev_err(fdev->dev, "Unsupported ELF header machine (%d) "
> +			"expected (%d)\n", ehdr->e_machine, EM_SLIM);
> +		return -EINVAL;
> +	}
> +	if (ehdr->e_phoff > fw->size) {
> +		dev_err(fdev->dev, "Firmware size is too small\n");
> +		return -EINVAL;
> +	}
> +
> +	return 0;
> +}
> +
> +static int
> +st_fdma_elf_load_segments(struct st_fdma_dev *fdev, const struct firmware *fw)
> +{
> +	struct device *dev = fdev->dev;
> +	struct elf32_hdr *ehdr;
> +	struct elf32_phdr *phdr;
> +	int i, mem_loaded = 0;
> +	const u8 *elf_data = fw->data;
> +
> +	ehdr = (struct elf32_hdr *)elf_data;
> +	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
> +
> +	/*
> +	 * go through the available ELF segments
> +	 * the program header's paddr member to contain device addresses.
> +	 * We then go through the physically contiguous memory regions which we
> +	 * allocated (and mapped) earlier on the probe,
> +	 * and "translate" device address to kernel addresses,
> +	 * so we can copy the segments where they are expected.
> +	 */
> +	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
> +		u32 da = phdr->p_paddr;
> +		u32 memsz = phdr->p_memsz;
> +		u32 filesz = phdr->p_filesz;
> +		u32 offset = phdr->p_offset;
> +		void *dst;
> +
> +		if (phdr->p_type != PT_LOAD)
> +			continue;
> +
> +		dev_dbg(dev, "phdr: type %d da %#x ofst:%#x memsz %#x filesz %#x\n",
> +			phdr->p_type, da, offset, memsz, filesz);
> +
> +		if (filesz > memsz) {
> +			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
> +				filesz, memsz);
> +			break;
> +		}
> +
> +		if (offset + filesz > fw->size) {
> +			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
> +				offset + filesz, fw->size);
> +			break;
> +		}
> +
> +		dst = st_fdma_seg_to_mem(fdev, da, memsz);
> +		if (!dst) {
> +			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
> +			break;
> +		}
> +
> +		if (phdr->p_filesz)
> +			memcpy(dst, elf_data + phdr->p_offset, filesz);
> +
> +		if (memsz > filesz)
> +			memset(dst + filesz, 0, memsz - filesz);
> +
> +		mem_loaded++;
> +	}
> +
> +	return (mem_loaded != fdev->drvdata->num_mem) ? -EIO : 0;
> +}

Above two seem to be generic code and should be moved to core, this way
other drivers using ELF firmware binaries can reuse...

> @@ -738,6 +925,15 @@ static int st_fdma_slave_config(struct dma_chan *chan,
>  				struct dma_slave_config *slave_cfg)
>  {
>  	struct st_fdma_chan *fchan = to_st_fdma_chan(chan);
> +	int ret;
> +
> +	if (!atomic_read(&fchan->fdev->fw_loaded)) {
> +		ret = st_fdma_get_fw(fchan->fdev);

this seems quite an odd place to load firmware, can you explain why

-- 
~Vinod

  reply	other threads:[~2016-04-26 17:00 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-21 11:04 [PATCH 00/18] Add support for FDMA DMA controller found on STi chipsets Peter Griffin
2016-04-21 11:04 ` [PATCH 01/18] dmaengine: st_fdma: Add STMicroelectronics FDMA DT binding documentation Peter Griffin
2016-04-21 11:25   ` Arnd Bergmann
2016-04-26 12:00     ` Peter Griffin
2016-04-21 11:04 ` [PATCH 02/18] dmaengine: st_fdma: Add STMicroelectronics FDMA driver header file Peter Griffin
2016-04-21 11:20   ` Arnd Bergmann
2016-04-21 11:04 ` [PATCH 03/18] dmaengine: st_fdma: Add STMicroelectronics FDMA engine driver support Peter Griffin
2016-04-21 11:24   ` Arnd Bergmann
2016-04-21 11:26   ` Appana Durga Kedareswara Rao
2016-04-21 14:58     ` Peter Griffin
2016-04-25  9:04     ` Lee Jones
2016-04-26 16:56   ` Vinod Koul
2016-04-27 12:59     ` Peter Griffin
2016-05-02  9:30       ` Vinod Koul
2016-05-09 17:30         ` Peter Griffin
2016-04-21 11:04 ` [PATCH 04/18] dmaengine: st_fdma: Add xp70 firmware loading mechanism Peter Griffin
2016-04-26 17:00   ` Vinod Koul [this message]
2016-05-11  7:57     ` Peter Griffin
2016-05-12  5:40       ` Vinod Koul
2016-04-21 11:04 ` [PATCH 05/18] dmaengine: st_fdma: Add fdma suspend and resume callbacks Peter Griffin
2016-04-21 11:04 ` [PATCH 06/18] ARM: STi: DT: STiH407: Add FDMA driver dt nodes Peter Griffin
2016-04-21 11:04 ` [PATCH 07/18] MAINTAINERS: Add FDMA driver files to STi section Peter Griffin
2016-04-21 11:04 ` [PATCH 08/18] ARM: multi_v7_defconfig: Enable STi FDMA driver Peter Griffin
2016-04-21 11:25   ` Arnd Bergmann
2016-04-26 10:42     ` Peter Griffin
2016-04-21 11:04 ` [PATCH 09/18] ASoC: sti: Update DT example to match the driver code Peter Griffin
2016-04-21 11:27   ` Arnd Bergmann
2016-04-26 10:11     ` Peter Griffin
2016-04-26 10:58       ` Arnd Bergmann
2016-04-26 11:15         ` Peter Griffin
2016-04-26 11:44           ` Arnd Bergmann
2016-05-04  7:52             ` Arnaud Pouliquen
2016-05-04  9:05               ` Arnd Bergmann
2016-04-21 15:57   ` Mark Brown
2016-04-26 11:02     ` Peter Griffin
2016-05-03 15:46       ` Arnaud Pouliquen
2016-04-21 11:04 ` [PATCH 10/18] ASoC: sti: Update example to include assigned-clocks and mclk-fs Peter Griffin
2016-04-21 15:49   ` Mark Brown
2016-04-26 11:52     ` Peter Griffin
2016-04-26 14:23       ` Mark Brown
2016-04-26 14:51         ` Peter Griffin
2016-04-26 15:03           ` Mark Brown
2016-04-26 16:14             ` Peter Griffin
2016-04-26 16:41               ` Mark Brown
2016-04-26 17:49                 ` Peter Griffin
2016-04-21 11:04 ` [PATCH 11/18] ARM: multi_v7_defconfig: Enable STi and simple-card drivers Peter Griffin
2016-04-21 11:04 ` [PATCH 12/18] ARM: DT: STiH407: Add i2s_out pinctrl configuration Peter Griffin
2016-04-21 11:04 ` [PATCH 13/18] ARM: DT: STiH407: Add i2s_in " Peter Griffin
2016-04-21 11:04 ` [PATCH 14/18] ARM: DT: STiH407: Add spdif_out pinctrl config Peter Griffin
2016-04-21 11:04 ` [PATCH 15/18] ARM: STi: DT: STiH407: Add sti-sasg-codec dt node Peter Griffin
2016-04-21 11:04 ` [PATCH 16/18] ARM: STi: DT: STiH407: Add uniperif player dt nodes Peter Griffin
2016-04-21 11:04 ` [PATCH 17/18] ARM: STi: DT: STiH407: Add uniperif reader " Peter Griffin
2016-04-21 11:04 ` [PATCH 18/18] ARM: DT: STi: stihxxx-b2120: Add DT nodes for STi audio card Peter Griffin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160426170048.GS2274@localhost \
    --to=vinod.koul@intel.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).