From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E083C433FE for ; Thu, 23 Sep 2021 16:54:53 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2D63261248 for ; Thu, 23 Sep 2021 16:54:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2D63261248 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 395E783409; Thu, 23 Sep 2021 18:53:52 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20210112.gappssmtp.com header.i=@baylibre-com.20210112.gappssmtp.com header.b="mD2PiK22"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CF199833CF; Thu, 23 Sep 2021 18:47:45 +0200 (CEST) Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 42FFE833CE for ; Thu, 23 Sep 2021 18:47:36 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=aouledameur@baylibre.com Received: by mail-ej1-x632.google.com with SMTP id z24so510516ejf.5 for ; Thu, 23 Sep 2021 09:47:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=KZrIwXLc50cR7INL/99xkYQxFuXwERiwlvZnWSII0DQ=; b=mD2PiK22IjtcZmlnMwdUQdRobsdnA4s1m739fjo7KXEmfSfWfE/weG0IhjvPRZvdc+ kbm59ptoyTvP0JQKQyG32mnCPZDifUzXb489FWaDhHGMes3GhMTrjwdXPdpGNRpwMcS2 GNPwhaEUvz418lB4D1sRYd7zMngK5bMs6Fsnc7LfZMUjysrAMsVe2Ms6lU0Ygm6wY8BY S2ZPUSusU1PVNVGi7dBjh5cUqO8xdCGHfwGyvRHA3eJyD1uPOxjgZigGhQHL44PsYkPh eYxSGqOLG51Gwq+EHgsUjovC1R+AUuesszd6ylEBf+Hp3ZevvDCN1mqYqMT0UTGrJsYx WdLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KZrIwXLc50cR7INL/99xkYQxFuXwERiwlvZnWSII0DQ=; b=BKJzGajtv260+ygOOcsVWuwAORFdSNlNTZ3rVFKpeYQEWqkKKjSMj+Bi1BvVHrh4US emCyfKwc3xzZa3fPkQ2sGD1DHEvjk1lyW35mSvfhVBIs3D0MDhTQLuIgS2D29TpSmF6p IJu+VN1b3XRkZiX2APVWSre4KJg5H4mjSX2fKzEZI83OrhE9C5/4DYJfmx0rm3OC+ygx S+UiuAmltxevUvoQcdXXwtzpKlPrIzcULXNaAD9320lrLxOBCyfhiMhbf4Hf8KzwGWrk 2beC6F5fI0VnWE94mqVD9GDWK8vwUNvRWhK0TLp8DOUolDCN8pTR3bAFjNhKX54CHhuR U+mA== X-Gm-Message-State: AOAM531oVKn5UtouSADXOBBSsExusaZGdSVxGIlwRztGKSA/OJ5MJAHc /uxDBprfOFb7N/ePI1b9GSrKDja0eag5cg== X-Google-Smtp-Source: ABdhPJzsRz/4v6XN63eOZob4Y6g/eZCCaMfCJhtuRW7RC2ehHb3U+iRd/cd1gDfA+/G8ioeT4txIyA== X-Received: by 2002:a17:906:3fc8:: with SMTP id k8mr5917298ejj.217.1632415655541; Thu, 23 Sep 2021 09:47:35 -0700 (PDT) Received: from localhost.localdomain (203.247.120.78.rev.sfr.net. [78.120.247.203]) by smtp.googlemail.com with ESMTPSA id u4sm3323277ejc.19.2021.09.23.09.47.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Sep 2021 09:47:35 -0700 (PDT) From: Amjad Ouled-Ameur To: u-boot@lists.denx.de Cc: Keerthy , Amjad Ouled-Ameur , Andy Shevchenko , Patrick Delaunay , Pratyush Yadav , Sean Anderson , Simon Glass Subject: [PATCH 05/10] remoteproc: uclass: Add remoteproc resource handling helpers Date: Thu, 23 Sep 2021 18:46:44 +0200 Message-Id: <20210923164649.214770-6-aouledameur@baylibre.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210923164649.214770-1-aouledameur@baylibre.com> References: <20210923164649.214770-1-aouledameur@baylibre.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Thu, 23 Sep 2021 18:53:13 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean From: Keerthy Add remoteproc resource handling helpers. These functions are primarily to parse the resource table and to handle different types of resources. Carveout, devmem, trace & vring resources are handled. Signed-off-by: Keerthy [Amjad: fix redefinition of "struct resource_table" and compile warnings ] Signed-off-by: Amjad Ouled-Ameur --- drivers/remoteproc/rproc-uclass.c | 562 ++++++++++++++++++++++++++++++ include/remoteproc.h | 384 +++++++++++++++++++- 2 files changed, 945 insertions(+), 1 deletion(-) diff --git a/drivers/remoteproc/rproc-uclass.c b/drivers/remoteproc/rproc-uclass.c index 64c47c1e7225..3f7096045548 100644 --- a/drivers/remoteproc/rproc-uclass.c +++ b/drivers/remoteproc/rproc-uclass.c @@ -8,6 +8,7 @@ #define pr_fmt(fmt) "%s: " fmt, __func__ #include +#include #include #include #include @@ -19,9 +20,21 @@ #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; +struct resource_table { + u32 ver; + u32 num; + u32 reserved[2]; + u32 offset[0]; +} __packed; + +typedef int (*handle_resource_t) (struct udevice *, void *, int offset, int avail); + +static struct resource_table *rsc_table; + /** * for_each_remoteproc_device() - iterate through the list of rproc devices * @fn: check function to call per match, if this function returns fail, @@ -208,6 +221,86 @@ static int rproc_post_probe(struct udevice *dev) return 0; } +/** + * rproc_add_res() - After parsing the resource table add the mappings + * @dev: device we finished probing + * @mapping: rproc_mem_entry for the resource + * + * Return: if the remote proc driver has a add_res routine, invokes it and + * hands over the return value. overall, 0 if all went well, else appropriate + * error value. + */ +static int rproc_add_res(struct udevice *dev, struct rproc_mem_entry *mapping) +{ + const struct dm_rproc_ops *ops; + + ops = rproc_get_ops(dev); + if (!ops) { + debug("%s driver has no ops?\n", dev->name); + return -EINVAL; + } + + if (ops->add_res) + return ops->add_res(dev, mapping); + + return 0; +} + +/** + * rproc_alloc_mem() - After parsing the resource table allocat mem + * @dev: device we finished probing + * @len: rproc_mem_entry for the resource + * @align: alignment for the resource + * + * Return: if the remote proc driver has a add_res routine, invokes it and + * hands over the return value. overall, 0 if all went well, else appropriate + * error value. + */ +static void *rproc_alloc_mem(struct udevice *dev, unsigned long len, + unsigned long align) +{ + const struct dm_rproc_ops *ops; + + ops = rproc_get_ops(dev); + if (!ops) { + debug("%s driver has no ops?\n", dev->name); + return NULL; + } + + if (ops->alloc_mem) + return ops->alloc_mem(dev, len, align); + + return NULL; +} + +/** + * rproc_config_pagetable() - Configure page table for remote processor + * @dev: device we finished probing + * @virt: Virtual address of the resource + * @phys: Physical address the resource + * @len: length the resource + * + * Return: if the remote proc driver has a add_res routine, invokes it and + * hands over the return value. overall, 0 if all went well, else appropriate + * error value. + */ +static int rproc_config_pagetable(struct udevice *dev, unsigned int virt, + unsigned int phys, unsigned int len) +{ + const struct dm_rproc_ops *ops; + + ops = rproc_get_ops(dev); + if (!ops) { + debug("%s driver has no ops?\n", dev->name); + return -EINVAL; + } + + if (ops->config_pagetable) + return ops->config_pagetable(dev, virt, phys, len); + + return 0; +} + UCLASS_DRIVER(rproc) = { .id = UCLASS_REMOTEPROC, .name = "remoteproc", @@ -438,3 +531,472 @@ int rproc_is_running(int id) { return _rproc_ops_wrapper(id, RPROC_RUNNING); }; + +/* + * Virtio ring descriptors: 16 bytes. These can chain together via + * "next". + */ +struct vring_desc { + u64 addr; + u32 len; + u16 flags; + u16 next; +}; + +/* + * u32 is used here for ids for padding reasons. + */ +struct vring_used_elem { + u32 id; + u32 len; +}; + +static unsigned int vring_size(unsigned int num, unsigned long align) +{ + return ((sizeof(struct vring_desc) * num + sizeof(u16) * (3 + num) + + align - 1) & ~(align - 1)) + + sizeof(u16) * 3 + sizeof(struct vring_used_elem) * num; +} + +static int handle_trace(struct udevice *dev, struct fw_rsc_trace *rsc, + int offset, int avail) +{ + if (sizeof(*rsc) > avail) { + debug("trace rsc is truncated\n"); + return -EINVAL; + } + + /* + * make sure reserved bytes are zeroes + */ + if (rsc->reserved) { + debug("trace rsc has non zero reserved bytes\n"); + return -EINVAL; + } + + debug("trace rsc: da 0x%x, len 0x%x\n", rsc->da, rsc->len); + + return 0; +} + +static int handle_devmem(struct udevice *dev, struct fw_rsc_devmem *rsc, + int offset, int avail) +{ + struct rproc_mem_entry *mapping; + + if (sizeof(*rsc) > avail) { + debug("devmem rsc is truncated\n"); + return -EINVAL; + } + + /* + * make sure reserved bytes are zeroes + */ + if (rsc->reserved) { + debug("devmem rsc has non zero reserved bytes\n"); + return -EINVAL; + } + + debug("devmem rsc: pa 0x%x, da 0x%x, len 0x%x\n", + rsc->pa, rsc->da, rsc->len); + + rproc_config_pagetable(dev, rsc->da, rsc->pa, rsc->len); + + mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); + if (!mapping) + return -ENOMEM; + + /* + * We'll need this info later when we'll want to unmap everything + * (e.g. on shutdown). + * + * We can't trust the remote processor not to change the resource + * table, so we must maintain this info independently. + */ + mapping->dma = rsc->pa; + mapping->da = rsc->da; + mapping->len = rsc->len; + rproc_add_res(dev, mapping); + + debug("mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", + rsc->pa, rsc->da, rsc->len); + + return 0; +} + +static int handle_carveout(struct udevice *dev, struct fw_rsc_carveout *rsc, + int offset, int avail) +{ + struct rproc_mem_entry *mapping; + + if (sizeof(*rsc) > avail) { + debug("carveout rsc is truncated\n"); + return -EINVAL; + } + + /* + * make sure reserved bytes are zeroes + */ + if (rsc->reserved) { + debug("carveout rsc has non zero reserved bytes\n"); + return -EINVAL; + } + + debug("carveout rsc: da %x, pa %x, len %x, flags %x\n", + rsc->da, rsc->pa, rsc->len, rsc->flags); + + rsc->pa = (uintptr_t)rproc_alloc_mem(dev, rsc->len, 8); + if (!rsc->pa) { + debug + ("failed to allocate carveout rsc: da %x, pa %x, len %x, flags %x\n", + rsc->da, rsc->pa, rsc->len, rsc->flags); + return -ENOMEM; + } + rproc_config_pagetable(dev, rsc->da, rsc->pa, rsc->len); + + /* + * Ok, this is non-standard. + * + * Sometimes we can't rely on the generic iommu-based DMA API + * to dynamically allocate the device address and then set the IOMMU + * tables accordingly, because some remote processors might + * _require_ us to use hard coded device addresses that their + * firmware was compiled with. + * + * In this case, we must use the IOMMU API directly and map + * the memory to the device address as expected by the remote + * processor. + * + * Obviously such remote processor devices should not be configured + * to use the iommu-based DMA API: we expect 'dma' to contain the + * physical address in this case. + */ + mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); + if (!mapping) + return -ENOMEM; + + /* + * We'll need this info later when we'll want to unmap + * everything (e.g. on shutdown). + * + * We can't trust the remote processor not to change the + * resource table, so we must maintain this info independently. + */ + mapping->dma = rsc->pa; + mapping->da = rsc->da; + mapping->len = rsc->len; + rproc_add_res(dev, mapping); + + debug("carveout mapped 0x%x to 0x%x\n", rsc->da, rsc->pa); + + return 0; +} + +#define RPROC_PAGE_SHIFT 12 +#define RPROC_PAGE_SIZE BIT(RPROC_PAGE_SHIFT) +#define RPROC_PAGE_ALIGN(x) (((x) + (RPROC_PAGE_SIZE - 1)) & ~(RPROC_PAGE_SIZE - 1)) + +static int alloc_vring(struct udevice *dev, struct fw_rsc_vdev *rsc, int i) +{ + struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; + int size; + int order; + void *pa; + + debug("vdev rsc: vring%d: da %x, qsz %d, align %d\n", + i, vring->da, vring->num, vring->align); + + /* + * verify queue size and vring alignment are sane + */ + if (!vring->num || !vring->align) { + debug("invalid qsz (%d) or alignment (%d)\n", vring->num, + vring->align); + return -EINVAL; + } + + /* + * actual size of vring (in bytes) + */ + size = RPROC_PAGE_ALIGN(vring_size(vring->num, vring->align)); + order = vring->align >> RPROC_PAGE_SHIFT; + + pa = rproc_alloc_mem(dev, size, order); + if (!pa) { + debug("failed to allocate vring rsc\n"); + return -ENOMEM; + } + debug("alloc_mem(%#x, %d): %p\n", size, order, pa); + vring->da = (uintptr_t)pa; + + return !pa; +} + +static int handle_vdev(struct udevice *dev, struct fw_rsc_vdev *rsc, + int offset, int avail) +{ + int i, ret; + void *pa; + + /* + * make sure resource isn't truncated + */ + if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) + + rsc->config_len > avail) { + debug("vdev rsc is truncated\n"); + return -EINVAL; + } + + /* + * make sure reserved bytes are zeroes + */ + if (rsc->reserved[0] || rsc->reserved[1]) { + debug("vdev rsc has non zero reserved bytes\n"); + return -EINVAL; + } + + debug("vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n", + rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); + + /* + * we currently support only two vrings per rvdev + */ + if (rsc->num_of_vrings > 2) { + debug("too many vrings: %d\n", rsc->num_of_vrings); + return -EINVAL; + } + + /* + * allocate the vrings + */ + for (i = 0; i < rsc->num_of_vrings; i++) { + ret = alloc_vring(dev, rsc, i); + if (ret) + goto alloc_error; + } + + pa = rproc_alloc_mem(dev, RPMSG_TOTAL_BUF_SPACE, 6); + if (!pa) { + debug("failed to allocate vdev rsc\n"); + return -ENOMEM; + } + debug("vring buffer alloc_mem(%#x, 6): %p\n", RPMSG_TOTAL_BUF_SPACE, + pa); + + return 0; + + alloc_error: + return ret; +} + +/* + * A lookup table for resource handlers. The indices are defined in + * enum fw_resource_type. + */ +static handle_resource_t loading_handlers[RSC_LAST] = { + [RSC_CARVEOUT] = (handle_resource_t)handle_carveout, + [RSC_DEVMEM] = (handle_resource_t)handle_devmem, + [RSC_TRACE] = (handle_resource_t)handle_trace, + [RSC_VDEV] = (handle_resource_t)handle_vdev, +}; + +/* + * handle firmware resource entries before booting the remote processor + */ +static int handle_resources(struct udevice *dev, int len, + handle_resource_t handlers[RSC_LAST]) +{ + handle_resource_t handler; + int ret = 0, i; + + for (i = 0; i < rsc_table->num; i++) { + int offset = rsc_table->offset[i]; + struct fw_rsc_hdr *hdr = (void *)rsc_table + offset; + int avail = len - offset - sizeof(*hdr); + void *rsc = (void *)hdr + sizeof(*hdr); + + /* + * make sure table isn't truncated + */ + if (avail < 0) { + debug("rsc table is truncated\n"); + return -EINVAL; + } + + debug("rsc: type %d\n", hdr->type); + + if (hdr->type >= RSC_LAST) { + debug("unsupported resource %d\n", hdr->type); + continue; + } + + handler = handlers[hdr->type]; + if (!handler) + continue; + + ret = handler(dev, rsc, offset + sizeof(*hdr), avail); + if (ret) + break; + } + + return ret; +} + +static int +handle_intmem_to_l3_mapping(struct udevice *dev, + struct rproc_intmem_to_l3_mapping *l3_mapping) +{ + u32 i = 0; + + for (i = 0; i < l3_mapping->num_entries; i++) { + struct l3_map *curr_map = &l3_mapping->mappings[i]; + struct rproc_mem_entry *mapping; + + mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); + if (!mapping) + return -ENOMEM; + + mapping->dma = curr_map->l3_addr; + mapping->da = curr_map->priv_addr; + mapping->len = curr_map->len; + rproc_add_res(dev, mapping); + } + + return 0; +} + +static Elf32_Shdr *rproc_find_table(unsigned int addr) +{ + Elf32_Ehdr *ehdr; /* Elf header structure pointer */ + Elf32_Shdr *shdr; /* Section header structure pointer */ + Elf32_Shdr sectionheader; + int i; + u8 *elf_data; + char *name_table; + struct resource_table *ptable; + + ehdr = (Elf32_Ehdr *)(uintptr_t)addr; + elf_data = (u8 *)ehdr; + shdr = (Elf32_Shdr *)(elf_data + ehdr->e_shoff); + memcpy(§ionheader, &shdr[ehdr->e_shstrndx], sizeof(sectionheader)); + name_table = (char *)(elf_data + sectionheader.sh_offset); + + for (i = 0; i < ehdr->e_shnum; i++, shdr++) { + memcpy(§ionheader, shdr, sizeof(sectionheader)); + u32 size = sectionheader.sh_size; + u32 offset = sectionheader.sh_offset; + + if (strcmp + (name_table + sectionheader.sh_name, ".resource_table")) + continue; + + ptable = (struct resource_table *)(elf_data + offset); + + /* + * make sure table has at least the header + */ + if (sizeof(struct resource_table) > size) { + debug("header-less resource table\n"); + return NULL; + } + + /* + * we don't support any version beyond the first + */ + if (ptable->ver != 1) { + debug("unsupported fw ver: %d\n", ptable->ver); + return NULL; + } + + /* + * make sure reserved bytes are zeroes + */ + if (ptable->reserved[0] || ptable->reserved[1]) { + debug("non zero reserved bytes\n"); + return NULL; + } + + /* + * make sure the offsets array isn't truncated + */ + if (ptable->num * sizeof(ptable->offset[0]) + + sizeof(struct resource_table) > size) { + debug("resource table incomplete\n"); + return NULL; + } + + return shdr; + } + + return NULL; +} + +struct resource_table *rproc_find_resource_table(struct udevice *dev, + unsigned int addr, + int *tablesz) +{ + Elf32_Shdr *shdr; + Elf32_Shdr sectionheader; + struct resource_table *ptable; + u8 *elf_data = (u8 *)(uintptr_t)addr; + + shdr = rproc_find_table(addr); + if (!shdr) { + debug("%s: failed to get resource section header\n", __func__); + return NULL; + } + + memcpy(§ionheader, shdr, sizeof(sectionheader)); + ptable = (struct resource_table *)(elf_data + sectionheader.sh_offset); + if (tablesz) + *tablesz = sectionheader.sh_size; + + return ptable; +} + +unsigned long rproc_parse_resource_table(struct udevice *dev, struct rproc *cfg) +{ + struct resource_table *ptable = NULL; + int tablesz; + int ret; + unsigned long addr; + + addr = cfg->load_addr; + + ptable = rproc_find_resource_table(dev, addr, &tablesz); + if (!ptable) { + debug("%s : failed to find resource table\n", __func__); + return 0; + } + + debug("%s : found resource table\n", __func__); + rsc_table = kzalloc(tablesz, GFP_KERNEL); + if (!rsc_table) { + debug("resource table alloc failed!\n"); + return 0; + } + + /* + * Copy the resource table into a local buffer before handling the + * resource table. + */ + memcpy(rsc_table, ptable, tablesz); + if (cfg->intmem_to_l3_mapping) + handle_intmem_to_l3_mapping(dev, cfg->intmem_to_l3_mapping); + ret = handle_resources(dev, tablesz, loading_handlers); + if (ret) { + debug("handle_resources failed: %d\n", ret); + return 0; + } + + /* + * Instead of trying to mimic the kernel flow of copying the + * processed resource table into its post ELF load location in DDR + * copying it into its original location. + */ + memcpy(ptable, rsc_table, tablesz); + free(rsc_table); + rsc_table = NULL; + + return 1; +} diff --git a/include/remoteproc.h b/include/remoteproc.h index 089131f65fde..1a970bc23c52 100644 --- a/include/remoteproc.h +++ b/include/remoteproc.h @@ -1,4 +1,4 @@ -/* SPDX-License-Identifier: GPL-2.0+ */ +/* SPDX-License-Identifier: GPL-2.0 */ /* * (C) Copyright 2015 * Texas Instruments Incorporated - http://www.ti.com/ @@ -15,6 +15,375 @@ */ #include /* For platform data support - non dt world */ +/** + * struct fw_rsc_hdr - firmware resource entry header + * @type: resource type + * @data: resource data + * + * Every resource entry begins with a 'struct fw_rsc_hdr' header providing + * its @type. The content of the entry itself will immediately follow + * this header, and it should be parsed according to the resource type. + */ +struct fw_rsc_hdr { + u32 type; + u8 data[0]; +}; + +/** + * enum fw_resource_type - types of resource entries + * + * @RSC_CARVEOUT: request for allocation of a physically contiguous + * memory region. + * @RSC_DEVMEM: request to iommu_map a memory-based peripheral. + * @RSC_TRACE: announces the availability of a trace buffer into which + * the remote processor will be writing logs. + * @RSC_VDEV: declare support for a virtio device, and serve as its + * virtio header. + * @RSC_PRELOAD_VENDOR: a vendor resource type that needs to be handled by + * remoteproc implementations before loading + * @RSC_POSTLOAD_VENDOR: a vendor resource type that needs to be handled by + * remoteproc implementations after loading + * @RSC_LAST: just keep this one at the end + * + * For more details regarding a specific resource type, please see its + * dedicated structure below. + * + * Please note that these values are used as indices to the rproc_handle_rsc + * lookup table, so please keep them sane. Moreover, @RSC_LAST is used to + * check the validity of an index before the lookup table is accessed, so + * please update it as needed. + */ +enum fw_resource_type { + RSC_CARVEOUT = 0, + RSC_DEVMEM = 1, + RSC_TRACE = 2, + RSC_VDEV = 3, + RSC_PRELOAD_VENDOR = 4, + RSC_POSTLOAD_VENDOR = 5, + RSC_LAST = 6, +}; + +#define FW_RSC_ADDR_ANY (-1) + +/** + * struct fw_rsc_carveout - physically contiguous memory request + * @da: device address + * @pa: physical address + * @len: length (in bytes) + * @flags: iommu protection flags + * @reserved: reserved (must be zero) + * @name: human-readable name of the requested memory region + * + * This resource entry requests the host to allocate a physically contiguous + * memory region. + * + * These request entries should precede other firmware resource entries, + * as other entries might request placing other data objects inside + * these memory regions (e.g. data/code segments, trace resource entries, ...). + * + * Allocating memory this way helps utilizing the reserved physical memory + * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries + * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB + * pressure is important; it may have a substantial impact on performance. + * + * If the firmware is compiled with static addresses, then @da should specify + * the expected device address of this memory region. If @da is set to + * FW_RSC_ADDR_ANY, then the host will dynamically allocate it, and then + * overwrite @da with the dynamically allocated address. + * + * We will always use @da to negotiate the device addresses, even if it + * isn't using an iommu. In that case, though, it will obviously contain + * physical addresses. + * + * Some remote processors needs to know the allocated physical address + * even if they do use an iommu. This is needed, e.g., if they control + * hardware accelerators which access the physical memory directly (this + * is the case with OMAP4 for instance). In that case, the host will + * overwrite @pa with the dynamically allocated physical address. + * Generally we don't want to expose physical addresses if we don't have to + * (remote processors are generally _not_ trusted), so we might want to + * change this to happen _only_ when explicitly required by the hardware. + * + * @flags is used to provide IOMMU protection flags, and @name should + * (optionally) contain a human readable name of this carveout region + * (mainly for debugging purposes). + */ +struct fw_rsc_carveout { + u32 da; + u32 pa; + u32 len; + u32 flags; + u32 reserved; + u8 name[32]; +}; + +/** + * struct fw_rsc_devmem - iommu mapping request + * @da: device address + * @pa: physical address + * @len: length (in bytes) + * @flags: iommu protection flags + * @reserved: reserved (must be zero) + * @name: human-readable name of the requested region to be mapped + * + * This resource entry requests the host to iommu map a physically contiguous + * memory region. This is needed in case the remote processor requires + * access to certain memory-based peripherals; _never_ use it to access + * regular memory. + * + * This is obviously only needed if the remote processor is accessing memory + * via an iommu. + * + * @da should specify the required device address, @pa should specify + * the physical address we want to map, @len should specify the size of + * the mapping and @flags is the IOMMU protection flags. As always, @name may + * (optionally) contain a human readable name of this mapping (mainly for + * debugging purposes). + * + * Note: at this point we just "trust" those devmem entries to contain valid + * physical addresses, but this isn't safe and will be changed: eventually we + * want remoteproc implementations to provide us ranges of physical addresses + * the firmware is allowed to request, and not allow firmwares to request + * access to physical addresses that are outside those ranges. + */ +struct fw_rsc_devmem { + u32 da; + u32 pa; + u32 len; + u32 flags; + u32 reserved; + u8 name[32]; +}; + +/** + * struct fw_rsc_trace - trace buffer declaration + * @da: device address + * @len: length (in bytes) + * @reserved: reserved (must be zero) + * @name: human-readable name of the trace buffer + * + * This resource entry provides the host information about a trace buffer + * into which the remote processor will write log messages. + * + * @da specifies the device address of the buffer, @len specifies + * its size, and @name may contain a human readable name of the trace buffer. + * + * After booting the remote processor, the trace buffers are exposed to the + * user via debugfs entries (called trace0, trace1, etc..). + */ +struct fw_rsc_trace { + u32 da; + u32 len; + u32 reserved; + u8 name[32]; +}; + +/** + * struct fw_rsc_vdev_vring - vring descriptor entry + * @da: device address + * @align: the alignment between the consumer and producer parts of the vring + * @num: num of buffers supported by this vring (must be power of two) + * @notifyid is a unique rproc-wide notify index for this vring. This notify + * index is used when kicking a remote processor, to let it know that this + * vring is triggered. + * @pa: physical address + * + * This descriptor is not a resource entry by itself; it is part of the + * vdev resource type (see below). + * + * Note that @da should either contain the device address where + * the remote processor is expecting the vring, or indicate that + * dynamically allocation of the vring's device address is supported. + */ +struct fw_rsc_vdev_vring { + u32 da; + u32 align; + u32 num; + u32 notifyid; + u32 pa; +}; + +/** + * struct fw_rsc_vdev - virtio device header + * @id: virtio device id (as in virtio_ids.h) + * @notifyid is a unique rproc-wide notify index for this vdev. This notify + * index is used when kicking a remote processor, to let it know that the + * status/features of this vdev have changes. + * @dfeatures specifies the virtio device features supported by the firmware + * @gfeatures is a place holder used by the host to write back the + * negotiated features that are supported by both sides. + * @config_len is the size of the virtio config space of this vdev. The config + * space lies in the resource table immediate after this vdev header. + * @status is a place holder where the host will indicate its virtio progress. + * @num_of_vrings indicates how many vrings are described in this vdev header + * @reserved: reserved (must be zero) + * @vring is an array of @num_of_vrings entries of 'struct fw_rsc_vdev_vring'. + * + * This resource is a virtio device header: it provides information about + * the vdev, and is then used by the host and its peer remote processors + * to negotiate and share certain virtio properties. + * + * By providing this resource entry, the firmware essentially asks remoteproc + * to statically allocate a vdev upon registration of the rproc (dynamic vdev + * allocation is not yet supported). + * + * Note: unlike virtualization systems, the term 'host' here means + * the Linux side which is running remoteproc to control the remote + * processors. We use the name 'gfeatures' to comply with virtio's terms, + * though there isn't really any virtualized guest OS here: it's the host + * which is responsible for negotiating the final features. + * Yeah, it's a bit confusing. + * + * Note: immediately following this structure is the virtio config space for + * this vdev (which is specific to the vdev; for more info, read the virtio + * spec). the size of the config space is specified by @config_len. + */ +struct fw_rsc_vdev { + u32 id; + u32 notifyid; + u32 dfeatures; + u32 gfeatures; + u32 config_len; + u8 status; + u8 num_of_vrings; + u8 reserved[2]; + struct fw_rsc_vdev_vring vring[0]; +}; + +/** + * struct rproc_mem_entry - memory entry descriptor + * @va: virtual address + * @dma: dma address + * @len: length, in bytes + * @da: device address + * @priv: associated data + * @name: associated memory region name (optional) + * @node: list node + */ +struct rproc_mem_entry { + void *va; + dma_addr_t dma; + int len; + u32 da; + void *priv; + char name[32]; + struct list_head node; +}; + +struct rproc; + +typedef u32(*init_func_proto) (u32 core_id, struct rproc *cfg); + +struct l3_map { + u32 priv_addr; + u32 l3_addr; + u32 len; +}; + +struct rproc_intmem_to_l3_mapping { + u32 num_entries; + struct l3_map mappings[16]; +}; + +/** + * enum rproc_crash_type - remote processor crash types + * @RPROC_MMUFAULT: iommu fault + * @RPROC_WATCHDOG: watchdog bite + * @RPROC_FATAL_ERROR fatal error + * + * Each element of the enum is used as an array index. So that, the value of + * the elements should be always something sane. + * + * Feel free to add more types when needed. + */ +enum rproc_crash_type { + RPROC_MMUFAULT, + RPROC_WATCHDOG, + RPROC_FATAL_ERROR, +}; + +/* we currently support only two vrings per rvdev */ +#define RVDEV_NUM_VRINGS 2 + +#define RPMSG_NUM_BUFS (512) +#define RPMSG_BUF_SIZE (512) +#define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE) + +/** + * struct rproc_vring - remoteproc vring state + * @va: virtual address + * @dma: dma address + * @len: length, in bytes + * @da: device address + * @align: vring alignment + * @notifyid: rproc-specific unique vring index + * @rvdev: remote vdev + * @vq: the virtqueue of this vring + */ +struct rproc_vring { + void *va; + dma_addr_t dma; + int len; + u32 da; + u32 align; + int notifyid; + struct rproc_vdev *rvdev; + struct virtqueue *vq; +}; + +/** struct rproc - structure with all processor specific information for + * loading remotecore from boot loader. + * + * @num_iommus: Number of IOMMUs for this remote core. Zero indicates that the + * processor does not have an IOMMU. + * + * @cma_base: Base address of the carveout for this remotecore. + * + * @cma_size: Length of the carveout in bytes. + * + * @page_table_addr: array with the physical address of the page table. We are + * using the same page table for both IOMMU's. There is currently no strong + * usecase for maintaining different page tables for different MMU's servicing + * the same CPU. + * + * @mmu_base_addr: base address of the MMU + * + * @entry_point: address that is the entry point for the remote core. This + * address is in the memory view of the remotecore. + * + * @load_addr: Address to which the bootloader loads the firmware from + * persistent storage before invoking the ELF loader. Keeping this address + * configurable allows future optimizations such as loading the firmware from + * storage for remotecore2 via EDMA while the CPU is processing the ELF image + * of remotecore1. This address is in the memory view of the A15. + * + * @firmware_name: Name of the file that is expected to contain the ELF image. + * + * @has_rsc_table: Flag populated after parsing the ELF binary on target. + */ + +struct rproc { + u32 num_iommus; + unsigned long cma_base; + u32 cma_size; + unsigned long page_table_addr; + unsigned long mmu_base_addr[2]; + unsigned long load_addr; + unsigned long entry_point; + char *core_name; + char *firmware_name; + char *ptn; + init_func_proto start_clocks; + init_func_proto config_mmu; + init_func_proto config_peripherals; + init_func_proto start_core; + u32 has_rsc_table; + struct rproc_intmem_to_l3_mapping *intmem_to_l3_mapping; + u32 trace_pa; + u32 trace_len; +}; + +extern struct rproc *rproc_cfg_arr[2]; /** * enum rproc_mem_type - What type of memory model does the rproc use * @RPROC_INTERNAL_MEMORY_MAPPED: Remote processor uses own memory and is memory @@ -126,6 +495,12 @@ struct dm_rproc_ops { * @return virtual address. */ void * (*device_to_virt)(struct udevice *dev, ulong da, ulong size); + int (*add_res)(struct udevice *dev, + struct rproc_mem_entry *mapping); + void * (*alloc_mem)(struct udevice *dev, unsigned long len, + unsigned long align); + unsigned int (*config_pagetable)(struct udevice *dev, unsigned int virt, + unsigned int phys, unsigned int len); }; /* Accessor */ @@ -322,6 +697,13 @@ int rproc_elf64_load_rsc_table(struct udevice *dev, ulong fw_addr, */ int rproc_elf_load_rsc_table(struct udevice *dev, ulong fw_addr, ulong fw_size, ulong *rsc_addr, ulong *rsc_size); + +unsigned long rproc_parse_resource_table(struct udevice *dev, + struct rproc *cfg); + +struct resource_table *rproc_find_resource_table(struct udevice *dev, + unsigned int addr, + int *tablesz); #else static inline int rproc_init(void) { return -ENOSYS; } static inline int rproc_dev_init(int id) { return -ENOSYS; } -- 2.25.1