From nobody Fri Nov 22 02:00:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1722857804; cv=none; d=zohomail.com; s=zohoarc; b=J3/o9qGyV7I3+3LfCErfrbFqzODUJ3E0tHP2G45NPUrIeAHiuCqxYHBRAiKz8HbUNo++4dHW8aEsyYWBN9e+n9xUvZgHU5PXfF51nKJ+kHMdAnT/1kQMRwDlZhMRmEjZYgVk9GuXZ7zhJYKjBH9iM6/uKqwsZUbl/ePsprr4v24= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722857804; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=+uwf35f2uh+P4NAcFH5aNGBxpK9iZWsUOlZHCgKO8Bw=; b=jcRmdk8Mng9JXa+VIYWUfsJHgswPD0xpeou7EbsmHXc226wzXxNrwQzOSP+854/1Iwk6cyhsfJN7fQp2hJl8X+TIUyFXifhf52+XlAEr7j1hPChzgbEmhUUBtzY3fnINt2zVTGTpg9xHvqhDS3xUG5DOf8+SUTrFIfoIj0Py/xg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1722857804401875.5201998817348; Mon, 5 Aug 2024 04:36:44 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.772066.1182521 (Exim 4.92) (envelope-from ) id 1saw0o-0003DQ-HM; Mon, 05 Aug 2024 11:36:26 +0000 Received: by outflank-mailman (output) from mailman id 772066.1182521; Mon, 05 Aug 2024 11:36:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1saw0o-0003DJ-DX; Mon, 05 Aug 2024 11:36:26 +0000 Received: by outflank-mailman (input) for mailman id 772066; Mon, 05 Aug 2024 11:36:24 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1saw0m-0002gX-1D for xen-devel@lists.xenproject.org; Mon, 05 Aug 2024 11:36:24 +0000 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [2a00:1450:4864:20::633]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id efd89e98-531e-11ef-bc03-fd08da9f4363; Mon, 05 Aug 2024 13:36:21 +0200 (CEST) Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-a7aa086b077so909875166b.0 for ; Mon, 05 Aug 2024 04:36:21 -0700 (PDT) Received: from fedora.. ([94.75.70.14]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7dc9bc3d09sm447203566b.17.2024.08.05.04.36.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Aug 2024 04:36:18 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: efd89e98-531e-11ef-bc03-fd08da9f4363 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722857780; x=1723462580; darn=lists.xenproject.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+uwf35f2uh+P4NAcFH5aNGBxpK9iZWsUOlZHCgKO8Bw=; b=c7F9+QtOKxGYfFQhx+w5dfxh6s+bu8aviajo5NSkI2pG1mqC98Zl1mBSZ39WhfmgE+ LK/88EAX54sLngAiujigNxbRy0OWbDGTdTCjbcffxHyNeXheVpX7wJ5BqKw0KtpxQiWg j2lCrG71mNT6gQoZlxUg2+kCalUH2g2CMeR3L8FsZ7lOw1AK+TBOelfcm5v7pCren2rY Vkj7vNG0iczdKQsjH6QJsN8n6VyDNkBf1S169ADOQK/5+6TCwoPUGGmx2kEnGLvpmU2F E69W5fHpuMJAVGh4NiGgc4BsiEYKCO28xZI7lbeKF09Qo5La0LGII10UImDd2w8tmG28 Uh8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722857780; x=1723462580; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+uwf35f2uh+P4NAcFH5aNGBxpK9iZWsUOlZHCgKO8Bw=; b=Wuvoz2zlHGeM0WzAovVfO7aaKQpsH0Uj+MplC/a78AQ9Srma1EN3uEV82Oq+7LztIi MgiiTW2qIkhYkOB42QaGW496pmJDCpIL1hQgiSHBXzT3DRkbYS5mIXJNUZ1Zym/F6OFa 0QDyXMcOOULCZw+K0hor6UUdI6a5tVg0+LhB6Qkukz3vN1YkhQNDShs+kS5qx5ithtWv ifOF2kznLs03IRMmuysoq5QHL8lB1X0UIJtRFb16et11BTIgeOIW0jrH4hOkI3YfLXqu WFN6ITBxFfPtzA59TgwTzjaODycbT7HdoWDLC6ZYQTGLZmg6l3Q0pkmDz9enmqapwoFP 0ZHA== X-Gm-Message-State: AOJu0YzYqkBZIkLmu7B3eJwtYtpNi6+WRmNSTVGO+05DV8wtwV6xUi47 YMROarNh5xCn1+aPY0TVodEHmtUskLjmLuYDfNeWc4aOwxkpsHepGngIJQ== X-Google-Smtp-Source: AGHT+IHFInaNVckdbcZwW/fxMAd4ox59V2w8M55zeT7L4zWL+LVCTPw1NzaIh5aRRcZMiGPBGgSTeg== X-Received: by 2002:a17:907:724b:b0:a77:b516:3023 with SMTP id a640c23a62f3a-a7dc4db2a1emr801298866b.4.1722857778733; Mon, 05 Aug 2024 04:36:18 -0700 (PDT) From: Oleksii Kurochko To: xen-devel@lists.xenproject.org Cc: Shawn Anastasio , Andrew Cooper , Jan Beulich , Julien Grall , Stefano Stabellini , Bertrand Marquis , Michal Orzel , Volodymyr Babchuk , "Daniel P. Smith" , Oleksii Kurochko , Julien Grall Subject: [PATCH v8 1/2] xen/device-tree: Move Arm's setup.c bootinfo functions to common Date: Mon, 5 Aug 2024 13:33:49 +0200 Message-ID: <712aebb98a689b248f29e783eb8e72a5e7568e5b.1722856894.git.oleksii.kurochko@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1722857805414116600 Content-Type: text/plain; charset="utf-8" From: Shawn Anastasio Arm's setup.c contains a collection of functions for parsing memory map and other boot information from a device tree. Since these routines are generally useful on any architecture that supports device tree booting, move them into xen/common/device-tree. Also, common/device_tree.c has been moved to the device-tree folder with the corresponding updates to common/Makefile and common/device-tree/Makefil= e. Mentioning of arm32 is changed to CONFIG_SEPARATE_XENHEAP in comparison with original ARM's code as now it is moved in common code. Suggested-by: Julien Grall Signed-off-by: Shawn Anastasio Signed-off-by: Oleksii Kurochko Acked-by: Julien Grall --- Changes in V8: - add the version of Xen from which bootinfo.c was derived from. - update the commit message. - add Acked-by: Julien Grall . --- Changes in V7: - move obj-$(CONFIG_HAS_DEVICE_TREE) +=3D device-tree/ to proper place in = common/Makefile. - rename macros __XEN_BOOTFDT_H__ to XEN_BOOTFDT_H to not violate MISRA ru= le 21.1. - drop definition of "#define MAX_FDT_SIZE SZ_2M" in xen/bootfdt.h as it i= s expected to be arch-specific. Back "#define MAX_FDT_SIZE SZ_2M" to arm/setup.h where= it was before these changes. - git mv xen/common/device_tree.c xen/common/device-tree/ and update corre= spondingly Makefiles of common/ and common/device-tree - update the commit message --- Changes in V6: - update the version of the patch to v6. =20 --- Changes in V5: - After rebase the Shawn's patch v4 on top of current staging the following was done: - add xen/include/xen/bootfdt.h to MAINTAINERS file. - drop message "Early device tree parsing and". - After rebase on top of the current staging the following changes were = done: - init bootinfo variable in with BOOTI= NFO_INIT; - update the code of dt_unreserved_regions(): CONFIG_STATIC_SHM related changes and getting of reserved_mem bootinfo_get_shmem() - update the code of meminfo_overlap_check(): add check ( INVALID_PADDR =3D=3D bank_start ) to if case. - update the code of check_reserved_regions_overlap(): CONFIG_STATIC_SHM related changes. - struct bootinfo was updated ( CONFIG_STATIC_SHM changes ) - add shared_meminfo ( because of CONFIG_STATIC_SHM ) - struct struct membanks was update with __struct group so is neeeded to be included in bootfdt.h - move BOOTINFO_ACPI_INIT, BOOTINFO_SHMEM_INIT, BOOTINFO_INIT to gener= ic bootfdt.h - bootinfo_get_reserved_mem(), bootinfo_get_mem(), bootinfo_get_acpi(), bootinfo_get_shmem() and bootinfo_get_shmem_extra() were moved to xe= n/bootfdt.h - s/arm32/CONFIG_SEPARATE_XENHEAP/ - add inclusion of because there are function in which are using container_of(). --- Changes in v4: - create new xen/include/bootinfo.h rather than relying on arch's asm/setup.h to provide required definitions for bootinfo.c - build bootinfo.c as .init.o - clean up and sort bootinfo.c's #includes - use CONFIG_SEPARATE_XENHEAP rather than CONFIG_ARM_32 to guard xenheap-specific behavior of populate_boot_allocator - (MAINTAINERS) include all of common/device-tree rather than just bootinfo.c --- MAINTAINERS | 2 + xen/arch/arm/include/asm/setup.h | 185 +-- xen/arch/arm/setup.c | 432 ----- xen/common/Makefile | 2 +- xen/common/device-tree/Makefile | 2 + xen/common/device-tree/bootinfo.c | 459 ++++++ xen/common/device-tree/device_tree.c | 2253 ++++++++++++++++++++++++++ xen/common/device_tree.c | 2253 -------------------------- xen/include/xen/bootfdt.h | 195 +++ 9 files changed, 2913 insertions(+), 2870 deletions(-) create mode 100644 xen/common/device-tree/Makefile create mode 100644 xen/common/device-tree/bootinfo.c create mode 100644 xen/common/device-tree/device_tree.c delete mode 100644 xen/common/device_tree.c create mode 100644 xen/include/xen/bootfdt.h diff --git a/MAINTAINERS b/MAINTAINERS index 7c524a8a93..96adf60671 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -291,9 +291,11 @@ M: Bertrand Marquis M: Michal Orzel S: Supported F: xen/common/libfdt/ +F: xen/common/device-tree/ F: xen/common/device_tree.c F: xen/common/dt-overlay.c F: xen/include/xen/libfdt/ +F: xen/include/xen/bootfdt.h F: xen/include/xen/device_tree.h F: xen/drivers/passthrough/device_tree.c =20 diff --git a/xen/arch/arm/include/asm/setup.h b/xen/arch/arm/include/asm/se= tup.h index c34179da93..1748be29e5 100644 --- a/xen/arch/arm/include/asm/setup.h +++ b/xen/arch/arm/include/asm/setup.h @@ -3,159 +3,11 @@ =20 #include #include +#include #include =20 -#define MIN_FDT_ALIGN 8 #define MAX_FDT_SIZE SZ_2M =20 -#define NR_MEM_BANKS 256 -#define NR_SHMEM_BANKS 32 - -#define MAX_MODULES 32 /* Current maximum useful modules */ - -typedef enum { - BOOTMOD_XEN, - BOOTMOD_FDT, - BOOTMOD_KERNEL, - BOOTMOD_RAMDISK, - BOOTMOD_XSM, - BOOTMOD_GUEST_DTB, - BOOTMOD_UNKNOWN -} bootmodule_kind; - -enum membank_type { - /* - * The MEMBANK_DEFAULT type refers to either reserved memory for the - * device/firmware (when the bank is in 'reserved_mem') or any RAM (wh= en - * the bank is in 'mem'). - */ - MEMBANK_DEFAULT, - /* - * The MEMBANK_STATIC_DOMAIN type is used to indicate whether the memo= ry - * bank is bound to a static Xen domain. It is only valid when the bank - * is in reserved_mem. - */ - MEMBANK_STATIC_DOMAIN, - /* - * The MEMBANK_STATIC_HEAP type is used to indicate whether the memory - * bank is reserved as static heap. It is only valid when the bank is - * in reserved_mem. - */ - MEMBANK_STATIC_HEAP, - /* - * The MEMBANK_FDT_RESVMEM type is used to indicate whether the memory - * bank is from the FDT reserve map. - */ - MEMBANK_FDT_RESVMEM, -}; - -/* Indicates the maximum number of characters(\0 included) for shm_id */ -#define MAX_SHM_ID_LENGTH 16 - -struct shmem_membank_extra { - char shm_id[MAX_SHM_ID_LENGTH]; - unsigned int nr_shm_borrowers; -}; - -struct membank { - paddr_t start; - paddr_t size; - union { - enum membank_type type; -#ifdef CONFIG_STATIC_SHM - struct shmem_membank_extra *shmem_extra; -#endif - }; -}; - -struct membanks { - __struct_group(membanks_hdr, common, , - unsigned int nr_banks; - unsigned int max_banks; - ); - struct membank bank[]; -}; - -struct meminfo { - struct membanks_hdr common; - struct membank bank[NR_MEM_BANKS]; -}; - -struct shared_meminfo { - struct membanks_hdr common; - struct membank bank[NR_SHMEM_BANKS]; - struct shmem_membank_extra extra[NR_SHMEM_BANKS]; -}; - -/* - * The domU flag is set for kernels and ramdisks of "xen,domain" nodes. - * The purpose of the domU flag is to avoid getting confused in - * kernel_probe, where we try to guess which is the dom0 kernel and - * initrd to be compatible with all versions of the multiboot spec.=20 - */ -#define BOOTMOD_MAX_CMDLINE 1024 -struct bootmodule { - bootmodule_kind kind; - bool domU; - paddr_t start; - paddr_t size; -}; - -/* DT_MAX_NAME is the node name max length according the DT spec */ -#define DT_MAX_NAME 41 -struct bootcmdline { - bootmodule_kind kind; - bool domU; - paddr_t start; - char dt_name[DT_MAX_NAME]; - char cmdline[BOOTMOD_MAX_CMDLINE]; -}; - -struct bootmodules { - int nr_mods; - struct bootmodule module[MAX_MODULES]; -}; - -struct bootcmdlines { - unsigned int nr_mods; - struct bootcmdline cmdline[MAX_MODULES]; -}; - -struct bootinfo { - struct meminfo mem; - /* The reserved regions are only used when booting using Device-Tree */ - struct meminfo reserved_mem; - struct bootmodules modules; - struct bootcmdlines cmdlines; -#ifdef CONFIG_ACPI - struct meminfo acpi; -#endif -#ifdef CONFIG_STATIC_SHM - struct shared_meminfo shmem; -#endif - bool static_heap; -}; - -#ifdef CONFIG_ACPI -#define BOOTINFO_ACPI_INIT .acpi.common.max_banks =3D NR_MEM_BANKS, -#else -#define BOOTINFO_ACPI_INIT -#endif - -#ifdef CONFIG_STATIC_SHM -#define BOOTINFO_SHMEM_INIT .shmem.common.max_banks =3D NR_SHMEM_BANKS, -#else -#define BOOTINFO_SHMEM_INIT -#endif - -#define BOOTINFO_INIT \ -{ \ - .mem.common.max_banks =3D NR_MEM_BANKS, \ - .reserved_mem.common.max_banks =3D NR_MEM_BANKS, \ - BOOTINFO_ACPI_INIT \ - BOOTINFO_SHMEM_INIT \ -} - struct map_range_data { struct domain *d; @@ -167,39 +19,8 @@ struct map_range_data struct rangeset *irq_ranges; }; =20 -extern struct bootinfo bootinfo; - extern domid_t max_init_domid; =20 -static inline struct membanks *bootinfo_get_mem(void) -{ - return container_of(&bootinfo.mem.common, struct membanks, common); -} - -static inline struct membanks *bootinfo_get_reserved_mem(void) -{ - return container_of(&bootinfo.reserved_mem.common, struct membanks, co= mmon); -} - -#ifdef CONFIG_ACPI -static inline struct membanks *bootinfo_get_acpi(void) -{ - return container_of(&bootinfo.acpi.common, struct membanks, common); -} -#endif - -#ifdef CONFIG_STATIC_SHM -static inline struct membanks *bootinfo_get_shmem(void) -{ - return container_of(&bootinfo.shmem.common, struct membanks, common); -} - -static inline struct shmem_membank_extra *bootinfo_get_shmem_extra(void) -{ - return bootinfo.shmem.extra; -} -#endif - void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len); =20 size_t estimate_efi_size(unsigned int mem_nr_banks); @@ -220,9 +41,6 @@ void fw_unreserved_regions(paddr_t s, paddr_t e, void (*cb)(paddr_t ps, paddr_t pe), unsigned int first); =20 -size_t boot_fdt_info(const void *fdt, paddr_t paddr); -const char *boot_fdt_cmdline(const void *fdt); - bool check_reserved_regions_overlap(paddr_t region_start, paddr_t region_s= ize); =20 struct bootmodule *add_boot_module(bootmodule_kind kind, @@ -237,7 +55,6 @@ struct bootcmdline * boot_cmdline_find_by_name(const cha= r *name); const char *boot_module_kind_as_string(bootmodule_kind kind); =20 void init_pdx(void); -void populate_boot_allocator(void); void setup_mm(void); =20 extern uint32_t hyp_traps_vector[]; diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c index 0c2fdaceaf..cb2c0a16b8 100644 --- a/xen/arch/arm/setup.c +++ b/xen/arch/arm/setup.c @@ -48,8 +48,6 @@ #include #include =20 -struct bootinfo __initdata bootinfo =3D BOOTINFO_INIT; - /* * Sanitized version of cpuinfo containing only features available on all * cores (only on arm64 as there is no sanitization support on arm32). @@ -203,321 +201,6 @@ static void __init processor_id(void) processor_setup(); } =20 -static void __init dt_unreserved_regions(paddr_t s, paddr_t e, - void (*cb)(paddr_t ps, paddr_t pe= ), - unsigned int first) -{ - const struct membanks *reserved_mem =3D bootinfo_get_reserved_mem(); -#ifdef CONFIG_STATIC_SHM - const struct membanks *shmem =3D bootinfo_get_shmem(); - unsigned int offset; -#endif - unsigned int i; - - /* - * i is the current bootmodule we are evaluating across all possible - * kinds. - */ - for ( i =3D first; i < reserved_mem->nr_banks; i++ ) - { - paddr_t r_s =3D reserved_mem->bank[i].start; - paddr_t r_e =3D r_s + reserved_mem->bank[i].size; - - if ( s < r_e && r_s < e ) - { - dt_unreserved_regions(r_e, e, cb, i + 1); - dt_unreserved_regions(s, r_s, cb, i + 1); - return; - } - } - -#ifdef CONFIG_STATIC_SHM - /* - * When retrieving the corresponding shared memory addresses - * below, we need to index the shmem->bank starting from 0, hence - * we need to use i - reserved_mem->nr_banks. - */ - offset =3D reserved_mem->nr_banks; - for ( ; i - offset < shmem->nr_banks; i++ ) - { - paddr_t r_s, r_e; - - r_s =3D shmem->bank[i - offset].start; - - /* Shared memory banks can contain INVALID_PADDR as start */ - if ( INVALID_PADDR =3D=3D r_s ) - continue; - - r_e =3D r_s + shmem->bank[i - offset].size; - - if ( s < r_e && r_s < e ) - { - dt_unreserved_regions(r_e, e, cb, i + 1); - dt_unreserved_regions(s, r_s, cb, i + 1); - return; - } - } -#endif - - cb(s, e); -} - -/* - * TODO: '*_end' could be 0 if the bank/region is at the end of the physic= al - * address space. This is for now not handled as it requires more rework. - */ -static bool __init meminfo_overlap_check(const struct membanks *mem, - paddr_t region_start, - paddr_t region_size) -{ - paddr_t bank_start =3D INVALID_PADDR, bank_end =3D 0; - paddr_t region_end =3D region_start + region_size; - unsigned int i, bank_num =3D mem->nr_banks; - - for ( i =3D 0; i < bank_num; i++ ) - { - bank_start =3D mem->bank[i].start; - bank_end =3D bank_start + mem->bank[i].size; - - if ( INVALID_PADDR =3D=3D bank_start || region_end <=3D bank_start= || - region_start >=3D bank_end ) - continue; - else - { - printk("Region: [%#"PRIpaddr", %#"PRIpaddr") overlapping with = bank[%u]: [%#"PRIpaddr", %#"PRIpaddr")\n", - region_start, region_end, i, bank_start, bank_end); - return true; - } - } - - return false; -} - -/* - * TODO: '*_end' could be 0 if the module/region is at the end of the phys= ical - * address space. This is for now not handled as it requires more rework. - */ -static bool __init bootmodules_overlap_check(struct bootmodules *bootmodul= es, - paddr_t region_start, - paddr_t region_size) -{ - paddr_t mod_start =3D INVALID_PADDR, mod_end =3D 0; - paddr_t region_end =3D region_start + region_size; - unsigned int i, mod_num =3D bootmodules->nr_mods; - - for ( i =3D 0; i < mod_num; i++ ) - { - mod_start =3D bootmodules->module[i].start; - mod_end =3D mod_start + bootmodules->module[i].size; - - if ( region_end <=3D mod_start || region_start >=3D mod_end ) - continue; - else - { - printk("Region: [%#"PRIpaddr", %#"PRIpaddr") overlapping with = mod[%u]: [%#"PRIpaddr", %#"PRIpaddr")\n", - region_start, region_end, i, mod_start, mod_end); - return true; - } - } - - return false; -} - -void __init fw_unreserved_regions(paddr_t s, paddr_t e, - void (*cb)(paddr_t ps, paddr_t pe), - unsigned int first) -{ - if ( acpi_disabled ) - dt_unreserved_regions(s, e, cb, first); - else - cb(s, e); -} - -/* - * Given an input physical address range, check if this range is overlappi= ng - * with the existing reserved memory regions defined in bootinfo. - * Return true if the input physical address range is overlapping with any - * existing reserved memory regions, otherwise false. - */ -bool __init check_reserved_regions_overlap(paddr_t region_start, - paddr_t region_size) -{ - const struct membanks *mem_banks[] =3D { - bootinfo_get_reserved_mem(), -#ifdef CONFIG_ACPI - bootinfo_get_acpi(), -#endif -#ifdef CONFIG_STATIC_SHM - bootinfo_get_shmem(), -#endif - }; - unsigned int i; - - /* - * Check if input region is overlapping with reserved memory banks or - * ACPI EfiACPIReclaimMemory (when ACPI feature is enabled) or static - * shared memory banks (when static shared memory feature is enabled) - */ - for ( i =3D 0; i < ARRAY_SIZE(mem_banks); i++ ) - if ( meminfo_overlap_check(mem_banks[i], region_start, region_size= ) ) - return true; - - /* Check if input region is overlapping with bootmodules */ - if ( bootmodules_overlap_check(&bootinfo.modules, - region_start, region_size) ) - return true; - - return false; -} - -struct bootmodule __init *add_boot_module(bootmodule_kind kind, - paddr_t start, paddr_t size, - bool domU) -{ - struct bootmodules *mods =3D &bootinfo.modules; - struct bootmodule *mod; - unsigned int i; - - if ( mods->nr_mods =3D=3D MAX_MODULES ) - { - printk("Ignoring %s boot module at %"PRIpaddr"-%"PRIpaddr" (too ma= ny)\n", - boot_module_kind_as_string(kind), start, start + size); - return NULL; - } - - if ( check_reserved_regions_overlap(start, size) ) - return NULL; - - for ( i =3D 0 ; i < mods->nr_mods ; i++ ) - { - mod =3D &mods->module[i]; - if ( mod->kind =3D=3D kind && mod->start =3D=3D start ) - { - if ( !domU ) - mod->domU =3D false; - return mod; - } - } - - mod =3D &mods->module[mods->nr_mods++]; - mod->kind =3D kind; - mod->start =3D start; - mod->size =3D size; - mod->domU =3D domU; - - return mod; -} - -/* - * boot_module_find_by_kind can only be used to return Xen modules (e.g - * XSM, DTB) or Dom0 modules. This is not suitable for looking up guest - * modules. - */ -struct bootmodule * __init boot_module_find_by_kind(bootmodule_kind kind) -{ - struct bootmodules *mods =3D &bootinfo.modules; - struct bootmodule *mod; - int i; - for (i =3D 0 ; i < mods->nr_mods ; i++ ) - { - mod =3D &mods->module[i]; - if ( mod->kind =3D=3D kind && !mod->domU ) - return mod; - } - return NULL; -} - -void __init add_boot_cmdline(const char *name, const char *cmdline, - bootmodule_kind kind, paddr_t start, bool dom= U) -{ - struct bootcmdlines *cmds =3D &bootinfo.cmdlines; - struct bootcmdline *cmd; - - if ( cmds->nr_mods =3D=3D MAX_MODULES ) - { - printk("Ignoring %s cmdline (too many)\n", name); - return; - } - - cmd =3D &cmds->cmdline[cmds->nr_mods++]; - cmd->kind =3D kind; - cmd->domU =3D domU; - cmd->start =3D start; - - ASSERT(strlen(name) <=3D DT_MAX_NAME); - safe_strcpy(cmd->dt_name, name); - - if ( strlen(cmdline) > BOOTMOD_MAX_CMDLINE ) - panic("module %s command line too long\n", name); - safe_strcpy(cmd->cmdline, cmdline); -} - -/* - * boot_cmdline_find_by_kind can only be used to return Xen modules (e.g - * XSM, DTB) or Dom0 modules. This is not suitable for looking up guest - * modules. - */ -struct bootcmdline * __init boot_cmdline_find_by_kind(bootmodule_kind kind) -{ - struct bootcmdlines *cmds =3D &bootinfo.cmdlines; - struct bootcmdline *cmd; - int i; - - for ( i =3D 0 ; i < cmds->nr_mods ; i++ ) - { - cmd =3D &cmds->cmdline[i]; - if ( cmd->kind =3D=3D kind && !cmd->domU ) - return cmd; - } - return NULL; -} - -struct bootcmdline * __init boot_cmdline_find_by_name(const char *name) -{ - struct bootcmdlines *mods =3D &bootinfo.cmdlines; - struct bootcmdline *mod; - unsigned int i; - - for (i =3D 0 ; i < mods->nr_mods ; i++ ) - { - mod =3D &mods->cmdline[i]; - if ( strcmp(mod->dt_name, name) =3D=3D 0 ) - return mod; - } - return NULL; -} - -struct bootmodule * __init boot_module_find_by_addr_and_kind(bootmodule_ki= nd kind, - paddr_t start) -{ - struct bootmodules *mods =3D &bootinfo.modules; - struct bootmodule *mod; - unsigned int i; - - for (i =3D 0 ; i < mods->nr_mods ; i++ ) - { - mod =3D &mods->module[i]; - if ( mod->kind =3D=3D kind && mod->start =3D=3D start ) - return mod; - } - return NULL; -} - -const char * __init boot_module_kind_as_string(bootmodule_kind kind) -{ - switch ( kind ) - { - case BOOTMOD_XEN: return "Xen"; - case BOOTMOD_FDT: return "Device Tree"; - case BOOTMOD_KERNEL: return "Kernel"; - case BOOTMOD_RAMDISK: return "Ramdisk"; - case BOOTMOD_XSM: return "XSM"; - case BOOTMOD_GUEST_DTB: return "DTB"; - case BOOTMOD_UNKNOWN: return "Unknown"; - default: BUG(); - } -} - void __init discard_initial_modules(void) { struct bootmodules *mi =3D &bootinfo.modules; @@ -556,40 +239,6 @@ static void * __init relocate_fdt(paddr_t dtb_paddr, s= ize_t dtb_size) return fdt; } =20 -/* - * Return the end of the non-module region starting at s. In other - * words return s the start of the next modules after s. - * - * On input *end is the end of the region which should be considered - * and it is updated to reflect the end of the module, clipped to the - * end of the region if it would run over. - */ -static paddr_t __init next_module(paddr_t s, paddr_t *end) -{ - struct bootmodules *mi =3D &bootinfo.modules; - paddr_t lowest =3D ~(paddr_t)0; - int i; - - for ( i =3D 0; i < mi->nr_mods; i++ ) - { - paddr_t mod_s =3D mi->module[i].start; - paddr_t mod_e =3D mod_s + mi->module[i].size; - - if ( !mi->module[i].size ) - continue; - - if ( mod_s < s ) - continue; - if ( mod_s > lowest ) - continue; - if ( mod_s > *end ) - continue; - lowest =3D mod_s; - *end =3D min(*end, mod_e); - } - return lowest; -} - void __init init_pdx(void) { const struct membanks *mem =3D bootinfo_get_mem(); @@ -635,87 +284,6 @@ void __init init_pdx(void) } } =20 -/* - * Populate the boot allocator. - * If a static heap was not provided by the admin, all the RAM but the - * following regions will be added: - * - Modules (e.g., Xen, Kernel) - * - Reserved regions - * - Xenheap (arm32 only) - * If a static heap was provided by the admin, populate the boot - * allocator with the corresponding regions only, but with Xenheap excluded - * on arm32. - */ -void __init populate_boot_allocator(void) -{ - unsigned int i; - const struct membanks *banks =3D bootinfo_get_mem(); - const struct membanks *reserved_mem =3D bootinfo_get_reserved_mem(); - paddr_t s, e; - - if ( bootinfo.static_heap ) - { - for ( i =3D 0 ; i < reserved_mem->nr_banks; i++ ) - { - if ( reserved_mem->bank[i].type !=3D MEMBANK_STATIC_HEAP ) - continue; - - s =3D reserved_mem->bank[i].start; - e =3D s + reserved_mem->bank[i].size; -#ifdef CONFIG_ARM_32 - /* Avoid the xenheap, note that the xenheap cannot across a ba= nk */ - if ( s <=3D mfn_to_maddr(directmap_mfn_start) && - e >=3D mfn_to_maddr(directmap_mfn_end) ) - { - init_boot_pages(s, mfn_to_maddr(directmap_mfn_start)); - init_boot_pages(mfn_to_maddr(directmap_mfn_end), e); - } - else -#endif - init_boot_pages(s, e); - } - - return; - } - - for ( i =3D 0; i < banks->nr_banks; i++ ) - { - const struct membank *bank =3D &banks->bank[i]; - paddr_t bank_end =3D bank->start + bank->size; - - s =3D bank->start; - while ( s < bank_end ) - { - paddr_t n =3D bank_end; - - e =3D next_module(s, &n); - - if ( e =3D=3D ~(paddr_t)0 ) - e =3D n =3D bank_end; - - /* - * Module in a RAM bank other than the one which we are - * not dealing with here. - */ - if ( e > bank_end ) - e =3D bank_end; - -#ifdef CONFIG_ARM_32 - /* Avoid the xenheap */ - if ( s < mfn_to_maddr(directmap_mfn_end) && - mfn_to_maddr(directmap_mfn_start) < e ) - { - e =3D mfn_to_maddr(directmap_mfn_start); - n =3D mfn_to_maddr(directmap_mfn_end); - } -#endif - - fw_unreserved_regions(s, e, init_boot_pages, 0); - s =3D n; - } - } -} - size_t __read_mostly dcache_line_bytes; =20 /* C entry point for boot CPU */ diff --git a/xen/common/Makefile b/xen/common/Makefile index f12a474d40..7e66802a9e 100644 --- a/xen/common/Makefile +++ b/xen/common/Makefile @@ -6,7 +6,7 @@ obj-$(CONFIG_HYPFS_CONFIG) +=3D config_data.o obj-$(CONFIG_CORE_PARKING) +=3D core_parking.o obj-y +=3D cpu.o obj-$(CONFIG_DEBUG_TRACE) +=3D debugtrace.o -obj-$(CONFIG_HAS_DEVICE_TREE) +=3D device_tree.o +obj-$(CONFIG_HAS_DEVICE_TREE) +=3D device-tree/ obj-$(CONFIG_IOREQ_SERVER) +=3D dm.o obj-y +=3D domain.o obj-$(CONFIG_OVERLAY_DTB) +=3D dt-overlay.o diff --git a/xen/common/device-tree/Makefile b/xen/common/device-tree/Makef= ile new file mode 100644 index 0000000000..da892dd55d --- /dev/null +++ b/xen/common/device-tree/Makefile @@ -0,0 +1,2 @@ +obj-y +=3D bootinfo.init.o +obj-y +=3D device_tree.o diff --git a/xen/common/device-tree/bootinfo.c b/xen/common/device-tree/boo= tinfo.c new file mode 100644 index 0000000000..f2e6a1145b --- /dev/null +++ b/xen/common/device-tree/bootinfo.c @@ -0,0 +1,459 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Derived from Xen 4.19's $xen/arch/arm/setup.c. + * + * bookkeeping routines. + * + * Tim Deegan + * Copyright (c) 2011 Citrix Systems. + * Copyright (c) 2024 Raptor Engineering LLC + */ + +#include +#include +#include +#include +#include +#include +#include + +struct bootinfo __initdata bootinfo =3D BOOTINFO_INIT; + +const char * __init boot_module_kind_as_string(bootmodule_kind kind) +{ + switch ( kind ) + { + case BOOTMOD_XEN: return "Xen"; + case BOOTMOD_FDT: return "Device Tree"; + case BOOTMOD_KERNEL: return "Kernel"; + case BOOTMOD_RAMDISK: return "Ramdisk"; + case BOOTMOD_XSM: return "XSM"; + case BOOTMOD_GUEST_DTB: return "DTB"; + case BOOTMOD_UNKNOWN: return "Unknown"; + default: BUG(); + } +} + +static void __init dt_unreserved_regions(paddr_t s, paddr_t e, + void (*cb)(paddr_t ps, paddr_t pe= ), + unsigned int first) +{ + const struct membanks *reserved_mem =3D bootinfo_get_reserved_mem(); +#ifdef CONFIG_STATIC_SHM + const struct membanks *shmem =3D bootinfo_get_shmem(); + unsigned int offset; +#endif + unsigned int i; + + /* + * i is the current bootmodule we are evaluating across all possible + * kinds. + */ + for ( i =3D first; i < reserved_mem->nr_banks; i++ ) + { + paddr_t r_s =3D reserved_mem->bank[i].start; + paddr_t r_e =3D r_s + reserved_mem->bank[i].size; + + if ( s < r_e && r_s < e ) + { + dt_unreserved_regions(r_e, e, cb, i + 1); + dt_unreserved_regions(s, r_s, cb, i + 1); + return; + } + } + +#ifdef CONFIG_STATIC_SHM + /* + * When retrieving the corresponding shared memory addresses + * below, we need to index the shmem->bank starting from 0, hence + * we need to use i - reserved_mem->nr_banks. + */ + offset =3D reserved_mem->nr_banks; + for ( ; i - offset < shmem->nr_banks; i++ ) + { + paddr_t r_s, r_e; + + r_s =3D shmem->bank[i - offset].start; + + /* Shared memory banks can contain INVALID_PADDR as start */ + if ( INVALID_PADDR =3D=3D r_s ) + continue; + + r_e =3D r_s + shmem->bank[i - offset].size; + + if ( s < r_e && r_s < e ) + { + dt_unreserved_regions(r_e, e, cb, i + 1); + dt_unreserved_regions(s, r_s, cb, i + 1); + return; + } + } +#endif + + cb(s, e); +} + +/* + * TODO: '*_end' could be 0 if the bank/region is at the end of the physic= al + * address space. This is for now not handled as it requires more rework. + */ +static bool __init meminfo_overlap_check(const struct membanks *mem, + paddr_t region_start, + paddr_t region_size) +{ + paddr_t bank_start =3D INVALID_PADDR, bank_end =3D 0; + paddr_t region_end =3D region_start + region_size; + unsigned int i, bank_num =3D mem->nr_banks; + + for ( i =3D 0; i < bank_num; i++ ) + { + bank_start =3D mem->bank[i].start; + bank_end =3D bank_start + mem->bank[i].size; + + if ( INVALID_PADDR =3D=3D bank_start || region_end <=3D bank_start= || + region_start >=3D bank_end ) + continue; + else + { + printk("Region: [%#"PRIpaddr", %#"PRIpaddr") overlapping with = bank[%u]: [%#"PRIpaddr", %#"PRIpaddr")\n", + region_start, region_end, i, bank_start, bank_end); + return true; + } + } + + return false; +} + +/* + * TODO: '*_end' could be 0 if the module/region is at the end of the phys= ical + * address space. This is for now not handled as it requires more rework. + */ +static bool __init bootmodules_overlap_check(struct bootmodules *bootmodul= es, + paddr_t region_start, + paddr_t region_size) +{ + paddr_t mod_start =3D INVALID_PADDR, mod_end =3D 0; + paddr_t region_end =3D region_start + region_size; + unsigned int i, mod_num =3D bootmodules->nr_mods; + + for ( i =3D 0; i < mod_num; i++ ) + { + mod_start =3D bootmodules->module[i].start; + mod_end =3D mod_start + bootmodules->module[i].size; + + if ( region_end <=3D mod_start || region_start >=3D mod_end ) + continue; + else + { + printk("Region: [%#"PRIpaddr", %#"PRIpaddr") overlapping with = mod[%u]: [%#"PRIpaddr", %#"PRIpaddr")\n", + region_start, region_end, i, mod_start, mod_end); + return true; + } + } + + return false; +} + +void __init fw_unreserved_regions(paddr_t s, paddr_t e, + void (*cb)(paddr_t ps, paddr_t pe), + unsigned int first) +{ + if ( acpi_disabled ) + dt_unreserved_regions(s, e, cb, first); + else + cb(s, e); +} + +/* + * Given an input physical address range, check if this range is overlappi= ng + * with the existing reserved memory regions defined in bootinfo. + * Return true if the input physical address range is overlapping with any + * existing reserved memory regions, otherwise false. + */ +bool __init check_reserved_regions_overlap(paddr_t region_start, + paddr_t region_size) +{ + const struct membanks *mem_banks[] =3D { + bootinfo_get_reserved_mem(), +#ifdef CONFIG_ACPI + bootinfo_get_acpi(), +#endif +#ifdef CONFIG_STATIC_SHM + bootinfo_get_shmem(), +#endif + }; + unsigned int i; + + /* + * Check if input region is overlapping with reserved memory banks or + * ACPI EfiACPIReclaimMemory (when ACPI feature is enabled) or static + * shared memory banks (when static shared memory feature is enabled) + */ + for ( i =3D 0; i < ARRAY_SIZE(mem_banks); i++ ) + if ( meminfo_overlap_check(mem_banks[i], region_start, region_size= ) ) + return true; + + /* Check if input region is overlapping with bootmodules */ + if ( bootmodules_overlap_check(&bootinfo.modules, + region_start, region_size) ) + return true; + + return false; +} + +struct bootmodule __init *add_boot_module(bootmodule_kind kind, + paddr_t start, paddr_t size, + bool domU) +{ + struct bootmodules *mods =3D &bootinfo.modules; + struct bootmodule *mod; + unsigned int i; + + if ( mods->nr_mods =3D=3D MAX_MODULES ) + { + printk("Ignoring %s boot module at %"PRIpaddr"-%"PRIpaddr" (too ma= ny)\n", + boot_module_kind_as_string(kind), start, start + size); + return NULL; + } + + if ( check_reserved_regions_overlap(start, size) ) + return NULL; + + for ( i =3D 0 ; i < mods->nr_mods ; i++ ) + { + mod =3D &mods->module[i]; + if ( mod->kind =3D=3D kind && mod->start =3D=3D start ) + { + if ( !domU ) + mod->domU =3D false; + return mod; + } + } + + mod =3D &mods->module[mods->nr_mods++]; + mod->kind =3D kind; + mod->start =3D start; + mod->size =3D size; + mod->domU =3D domU; + + return mod; +} + +/* + * boot_module_find_by_kind can only be used to return Xen modules (e.g + * XSM, DTB) or Dom0 modules. This is not suitable for looking up guest + * modules. + */ +struct bootmodule * __init boot_module_find_by_kind(bootmodule_kind kind) +{ + struct bootmodules *mods =3D &bootinfo.modules; + struct bootmodule *mod; + int i; + for (i =3D 0 ; i < mods->nr_mods ; i++ ) + { + mod =3D &mods->module[i]; + if ( mod->kind =3D=3D kind && !mod->domU ) + return mod; + } + return NULL; +} + +void __init add_boot_cmdline(const char *name, const char *cmdline, + bootmodule_kind kind, paddr_t start, bool dom= U) +{ + struct bootcmdlines *cmds =3D &bootinfo.cmdlines; + struct bootcmdline *cmd; + + if ( cmds->nr_mods =3D=3D MAX_MODULES ) + { + printk("Ignoring %s cmdline (too many)\n", name); + return; + } + + cmd =3D &cmds->cmdline[cmds->nr_mods++]; + cmd->kind =3D kind; + cmd->domU =3D domU; + cmd->start =3D start; + + ASSERT(strlen(name) <=3D DT_MAX_NAME); + safe_strcpy(cmd->dt_name, name); + + if ( strlen(cmdline) > BOOTMOD_MAX_CMDLINE ) + panic("module %s command line too long\n", name); + safe_strcpy(cmd->cmdline, cmdline); +} + +/* + * boot_cmdline_find_by_kind can only be used to return Xen modules (e.g + * XSM, DTB) or Dom0 modules. This is not suitable for looking up guest + * modules. + */ +struct bootcmdline * __init boot_cmdline_find_by_kind(bootmodule_kind kind) +{ + struct bootcmdlines *cmds =3D &bootinfo.cmdlines; + struct bootcmdline *cmd; + int i; + + for ( i =3D 0 ; i < cmds->nr_mods ; i++ ) + { + cmd =3D &cmds->cmdline[i]; + if ( cmd->kind =3D=3D kind && !cmd->domU ) + return cmd; + } + return NULL; +} + +struct bootcmdline * __init boot_cmdline_find_by_name(const char *name) +{ + struct bootcmdlines *mods =3D &bootinfo.cmdlines; + struct bootcmdline *mod; + unsigned int i; + + for (i =3D 0 ; i < mods->nr_mods ; i++ ) + { + mod =3D &mods->cmdline[i]; + if ( strcmp(mod->dt_name, name) =3D=3D 0 ) + return mod; + } + return NULL; +} + +struct bootmodule * __init boot_module_find_by_addr_and_kind(bootmodule_ki= nd kind, + paddr_t start) +{ + struct bootmodules *mods =3D &bootinfo.modules; + struct bootmodule *mod; + unsigned int i; + + for (i =3D 0 ; i < mods->nr_mods ; i++ ) + { + mod =3D &mods->module[i]; + if ( mod->kind =3D=3D kind && mod->start =3D=3D start ) + return mod; + } + return NULL; +} + +/* + * Return the end of the non-module region starting at s. In other + * words return s the start of the next modules after s. + * + * On input *end is the end of the region which should be considered + * and it is updated to reflect the end of the module, clipped to the + * end of the region if it would run over. + */ +static paddr_t __init next_module(paddr_t s, paddr_t *end) +{ + struct bootmodules *mi =3D &bootinfo.modules; + paddr_t lowest =3D ~(paddr_t)0; + int i; + + for ( i =3D 0; i < mi->nr_mods; i++ ) + { + paddr_t mod_s =3D mi->module[i].start; + paddr_t mod_e =3D mod_s + mi->module[i].size; + + if ( !mi->module[i].size ) + continue; + + if ( mod_s < s ) + continue; + if ( mod_s > lowest ) + continue; + if ( mod_s > *end ) + continue; + lowest =3D mod_s; + *end =3D min(*end, mod_e); + } + return lowest; +} + +/* + * Populate the boot allocator. + * If a static heap was not provided by the admin, all the RAM but the + * following regions will be added: + * - Modules (e.g., Xen, Kernel) + * - Reserved regions + * - Xenheap (CONFIG_SEPARATE_XENHEAP only) + * If a static heap was provided by the admin, populate the boot + * allocator with the corresponding regions only, but with Xenheap excluded + * on CONFIG_SEPARATE_XENHEAP. + */ +void __init populate_boot_allocator(void) +{ + unsigned int i; + const struct membanks *banks =3D bootinfo_get_mem(); + const struct membanks *reserved_mem =3D bootinfo_get_reserved_mem(); + paddr_t s, e; + + if ( bootinfo.static_heap ) + { + for ( i =3D 0 ; i < reserved_mem->nr_banks; i++ ) + { + if ( reserved_mem->bank[i].type !=3D MEMBANK_STATIC_HEAP ) + continue; + + s =3D reserved_mem->bank[i].start; + e =3D s + reserved_mem->bank[i].size; +#ifdef CONFIG_SEPARATE_XENHEAP + /* Avoid the xenheap, note that the xenheap cannot across a ba= nk */ + if ( s <=3D mfn_to_maddr(directmap_mfn_start) && + e >=3D mfn_to_maddr(directmap_mfn_end) ) + { + init_boot_pages(s, mfn_to_maddr(directmap_mfn_start)); + init_boot_pages(mfn_to_maddr(directmap_mfn_end), e); + } + else +#endif + init_boot_pages(s, e); + } + + return; + } + + for ( i =3D 0; i < banks->nr_banks; i++ ) + { + const struct membank *bank =3D &banks->bank[i]; + paddr_t bank_end =3D bank->start + bank->size; + + s =3D bank->start; + while ( s < bank_end ) + { + paddr_t n =3D bank_end; + + e =3D next_module(s, &n); + + if ( e =3D=3D ~(paddr_t)0 ) + e =3D n =3D bank_end; + + /* + * Module in a RAM bank other than the one which we are + * not dealing with here. + */ + if ( e > bank_end ) + e =3D bank_end; + +#ifdef CONFIG_SEPARATE_XENHEAP + /* Avoid the xenheap */ + if ( s < mfn_to_maddr(directmap_mfn_end) && + mfn_to_maddr(directmap_mfn_start) < e ) + { + e =3D mfn_to_maddr(directmap_mfn_start); + n =3D mfn_to_maddr(directmap_mfn_end); + } +#endif + + fw_unreserved_regions(s, e, init_boot_pages, 0); + s =3D n; + } + } +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/common/device-tree/device_tree.c b/xen/common/device-tree/= device_tree.c new file mode 100644 index 0000000000..8d1017a49d --- /dev/null +++ b/xen/common/device-tree/device_tree.c @@ -0,0 +1,2253 @@ +/* + * Device Tree + * + * Copyright (C) 2012 Citrix Systems, Inc. + * Copyright 2009 Benjamin Herrenschmidt, IBM Corp + * benh@kernel.crashing.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const void *device_tree_flattened; +dt_irq_xlate_func dt_irq_xlate; +/* Host device tree */ +struct dt_device_node *dt_host; +/* Interrupt controller node*/ +const struct dt_device_node *dt_interrupt_controller; +DEFINE_RWLOCK(dt_host_lock); + +/** + * struct dt_alias_prop - Alias property in 'aliases' node + * @link: List node to link the structure in aliases_lookup list + * @alias: Alias property name + * @np: Pointer to device_node that the alias stands for + * @id: Index value from end of alias name + * @stem: Alias string without the index + * + * The structure represents one alias property of 'aliases' node as + * an entry in aliases_lookup list. + */ +struct dt_alias_prop { + struct list_head link; + const char *alias; + struct dt_device_node *np; + int id; + char stem[0]; +}; + +static LIST_HEAD(aliases_lookup); + +#ifdef CONFIG_DEVICE_TREE_DEBUG +static void dt_dump_addr(const char *s, const __be32 *addr, int na) +{ + dt_dprintk("%s", s); + while ( na-- ) + dt_dprintk(" %08x", be32_to_cpu(*(addr++))); + dt_dprintk("\n"); +} +#else +static void dt_dump_addr(const char *s, const __be32 *addr, int na) { } +#endif + +#define DT_BAD_ADDR ((u64)-1) + +/* Max address size we deal with */ +#define DT_MAX_ADDR_CELLS 4 +#define DT_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <=3D DT_MAX_ADDR_CELLS) +#define DT_CHECK_COUNTS(na, ns) (DT_CHECK_ADDR_COUNT(na) && (ns) > 0) + +/* Callbacks for bus specific translators */ +struct dt_bus +{ + const char *name; + const char *addresses; + bool (*match)(const struct dt_device_node *node); + void (*count_cells)(const struct dt_device_node *child, + int *addrc, int *sizec); + u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna); + int (*translate)(__be32 *addr, u64 offset, int na); + unsigned int (*get_flags)(const __be32 *addr); +}; + +void dt_get_range(const __be32 **cellp, const struct dt_device_node *np, + u64 *address, u64 *size) +{ + *address =3D dt_next_cell(dt_n_addr_cells(np), cellp); + *size =3D dt_next_cell(dt_n_size_cells(np), cellp); +} + +void dt_set_cell(__be32 **cellp, int size, u64 val) +{ + int cells =3D size; + + while ( size-- ) + { + (*cellp)[size] =3D cpu_to_fdt32(val); + val >>=3D 32; + } + + (*cellp) +=3D cells; +} + +void dt_set_range(__be32 **cellp, const struct dt_device_node *np, + u64 address, u64 size) +{ + dt_set_cell(cellp, dt_n_addr_cells(np), address); + dt_set_cell(cellp, dt_n_size_cells(np), size); +} + +void dt_child_set_range(__be32 **cellp, int addrcells, int sizecells, + u64 address, u64 size) +{ + dt_set_cell(cellp, addrcells, address); + dt_set_cell(cellp, sizecells, size); +} + +static void __init *unflatten_dt_alloc(unsigned long *mem, unsigned long s= ize, + unsigned long align) +{ + void *res; + + *mem =3D ROUNDUP(*mem, align); + res =3D (void *)*mem; + *mem +=3D size; + + return res; +} + +/* Find a property with a given name for a given node and return it. */ +const struct dt_property *dt_find_property(const struct dt_device_node *np, + const char *name, u32 *lenp) +{ + const struct dt_property *pp; + + if ( !np ) + return NULL; + + for ( pp =3D np->properties; pp; pp =3D pp->next ) + { + if ( dt_prop_cmp(pp->name, name) =3D=3D 0 ) + { + if ( lenp ) + *lenp =3D pp->length; + break; + } + } + + return pp; +} + +const void *dt_get_property(const struct dt_device_node *np, + const char *name, u32 *lenp) +{ + const struct dt_property *pp =3D dt_find_property(np, name, lenp); + + return pp ? pp->value : NULL; +} + +bool dt_property_read_u32(const struct dt_device_node *np, + const char *name, u32 *out_value) +{ + u32 len; + const __be32 *val; + + val =3D dt_get_property(np, name, &len); + if ( !val || len < sizeof(*out_value) ) + return 0; + + *out_value =3D be32_to_cpup(val); + + return 1; +} + + +bool dt_property_read_u64(const struct dt_device_node *np, + const char *name, u64 *out_value) +{ + u32 len; + const __be32 *val; + + val =3D dt_get_property(np, name, &len); + if ( !val || len < sizeof(*out_value) ) + return 0; + + *out_value =3D dt_read_number(val, 2); + + return 1; +} +int dt_property_read_string(const struct dt_device_node *np, + const char *propname, const char **out_string) +{ + const struct dt_property *pp =3D dt_find_property(np, propname, NULL); + + if ( !pp ) + return -EINVAL; + if ( !pp->length ) + return -ENODATA; + if ( strnlen(pp->value, pp->length) >=3D pp->length ) + return -EILSEQ; + + *out_string =3D pp->value; + + return 0; +} + +/** + * dt_find_property_value_of_size + * + * @np: device node from which the property value is to be read. + * @propname: name of the property to be searched. + * @min: minimum allowed length of property value + * @max: maximum allowed length of property value (0 means unlimited) + * @len: if !=3DNULL, actual length is written to here + * + * Search for a property in a device node and valid the requested size. + * + * Return: The property value on success, -EINVAL if the property does not + * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the + * property data is too small or too large. + */ +static void *dt_find_property_value_of_size(const struct dt_device_node *n= p, + const char *propname, u32 min, + u32 max, size_t *len) +{ + const struct dt_property *prop =3D dt_find_property(np, propname, NULL= ); + + if ( !prop ) + return ERR_PTR(-EINVAL); + if ( !prop->value ) + return ERR_PTR(-ENODATA); + if ( prop->length < min ) + return ERR_PTR(-EOVERFLOW); + if ( max && prop->length > max ) + return ERR_PTR(-EOVERFLOW); + + if ( len ) + *len =3D prop->length; + + return prop->value; +} + +int dt_property_read_variable_u32_array(const struct dt_device_node *np, + const char *propname, u32 *out_val= ues, + size_t sz_min, size_t sz_max) +{ + size_t sz, count; + const __be32 *val =3D dt_find_property_value_of_size(np, propname, + (sz_min * sizeof(*out_values)), + (sz_max * sizeof(*out_values)), + &sz); + + if ( IS_ERR(val) ) + return PTR_ERR(val); + + if ( !sz_max ) + sz =3D sz_min; + else + sz /=3D sizeof(*out_values); + + count =3D sz; + while ( count-- ) + *out_values++ =3D be32_to_cpup(val++); + + return sz; +} + +int dt_property_match_string(const struct dt_device_node *np, + const char *propname, const char *string) +{ + const struct dt_property *dtprop =3D dt_find_property(np, propname, NU= LL); + size_t l; + int i; + const char *p, *end; + + if ( !dtprop ) + return -EINVAL; + if ( !dtprop->value ) + return -ENODATA; + + p =3D dtprop->value; + end =3D p + dtprop->length; + + for ( i =3D 0; p < end; i++, p +=3D l ) + { + l =3D strnlen(p, end - p) + 1; + if ( p + l > end ) + return -EILSEQ; + if ( strcmp(string, p) =3D=3D 0 ) + return i; /* Found it; return index */ + } + return -ENODATA; +} + +bool dt_device_is_compatible(const struct dt_device_node *device, + const char *compat) +{ + const char* cp; + u32 cplen, l; + + cp =3D dt_get_property(device, "compatible", &cplen); + if ( cp =3D=3D NULL ) + return 0; + while ( cplen > 0 ) + { + if ( dt_compat_cmp(cp, compat) =3D=3D 0 ) + return 1; + l =3D strlen(cp) + 1; + cp +=3D l; + cplen -=3D l; + } + + return 0; +} + +bool dt_machine_is_compatible(const char *compat) +{ + const struct dt_device_node *root; + bool rc =3D false; + + root =3D dt_find_node_by_path("/"); + if ( root ) + { + rc =3D dt_device_is_compatible(root, compat); + } + return rc; +} + +struct dt_device_node *dt_find_node_by_name(struct dt_device_node *from, + const char *name) +{ + struct dt_device_node *np; + struct dt_device_node *dt; + + dt =3D from ? from->allnext : dt_host; + dt_for_each_device_node(dt, np) + if ( np->name && (dt_node_cmp(np->name, name) =3D=3D 0) ) + break; + + return np; +} + +struct dt_device_node *dt_find_node_by_type(struct dt_device_node *from, + const char *type) +{ + struct dt_device_node *np; + struct dt_device_node *dt; + + dt =3D from ? from->allnext : dt_host; + dt_for_each_device_node(dt, np) + if ( np->type && (dt_node_cmp(np->type, type) =3D=3D 0) ) + break; + + return np; +} + +struct dt_device_node *dt_find_node_by_path_from(struct dt_device_node *fr= om, + const char *path) +{ + struct dt_device_node *np; + + dt_for_each_device_node(from, np) + if ( np->full_name && (dt_node_cmp(np->full_name, path) =3D=3D 0) ) + break; + + return np; +} + +int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen, + struct dt_device_node **node) +{ + char *path; + + path =3D safe_copy_string_from_guest(u_path, u_plen, PAGE_SIZE); + if ( IS_ERR(path) ) + return PTR_ERR(path); + + *node =3D dt_find_node_by_path(path); + + xfree(path); + + return (*node =3D=3D NULL) ? -ESRCH : 0; +} + +struct dt_device_node *dt_find_node_by_alias(const char *alias) +{ + const struct dt_alias_prop *app; + + list_for_each_entry( app, &aliases_lookup, link ) + { + if ( !strcmp(app->alias, alias) ) + return app->np; + } + + return NULL; +} + +const struct dt_device_match * +dt_match_node(const struct dt_device_match *matches, + const struct dt_device_node *node) +{ + if ( !matches ) + return NULL; + + while ( matches->path || matches->type || + matches->compatible || matches->not_available || matches->prop= ) + { + bool match =3D true; + + if ( matches->path ) + match &=3D dt_node_path_is_equal(node, matches->path); + + if ( matches->type ) + match &=3D dt_device_type_is_equal(node, matches->type); + + if ( matches->compatible ) + match &=3D dt_device_is_compatible(node, matches->compatible); + + if ( matches->not_available ) + match &=3D !dt_device_is_available(node); + + if ( matches->prop ) + match &=3D dt_find_property(node, matches->prop, NULL) !=3D NU= LL; + + if ( match ) + return matches; + matches++; + } + + return NULL; +} + +const struct dt_device_node *dt_get_parent(const struct dt_device_node *no= de) +{ + if ( !node ) + return NULL; + + return node->parent; +} + +struct dt_device_node * +dt_find_compatible_node(struct dt_device_node *from, + const char *type, + const char *compatible) +{ + struct dt_device_node *np; + struct dt_device_node *dt; + + dt =3D from ? from->allnext : dt_host; + dt_for_each_device_node(dt, np) + { + if ( type + && !(np->type && (dt_node_cmp(np->type, type) =3D=3D 0)) ) + continue; + if ( dt_device_is_compatible(np, compatible) ) + break; + } + + return np; +} + +struct dt_device_node * +dt_find_matching_node(struct dt_device_node *from, + const struct dt_device_match *matches) +{ + struct dt_device_node *np; + struct dt_device_node *dt; + + dt =3D from ? from->allnext : dt_host; + dt_for_each_device_node(dt, np) + { + if ( dt_match_node(matches, np) ) + return np; + } + + return NULL; +} + +static int __dt_n_addr_cells(const struct dt_device_node *np, bool parent) +{ + const __be32 *ip; + + do { + if ( np->parent && !parent ) + np =3D np->parent; + parent =3D false; + + ip =3D dt_get_property(np, "#address-cells", NULL); + if ( ip ) + return be32_to_cpup(ip); + } while ( np->parent ); + /* No #address-cells property for the root node */ + return DT_ROOT_NODE_ADDR_CELLS_DEFAULT; +} + +static int __dt_n_size_cells(const struct dt_device_node *np, bool parent) +{ + const __be32 *ip; + + do { + if ( np->parent && !parent ) + np =3D np->parent; + parent =3D false; + + ip =3D dt_get_property(np, "#size-cells", NULL); + if ( ip ) + return be32_to_cpup(ip); + } while ( np->parent ); + /* No #address-cells property for the root node */ + return DT_ROOT_NODE_SIZE_CELLS_DEFAULT; +} + +int dt_n_addr_cells(const struct dt_device_node *np) +{ + return __dt_n_addr_cells(np, false); +} + +int dt_n_size_cells(const struct dt_device_node *np) +{ + return __dt_n_size_cells(np, false); +} + +int dt_child_n_addr_cells(const struct dt_device_node *parent) +{ + return __dt_n_addr_cells(parent, true); +} + +int dt_child_n_size_cells(const struct dt_device_node *parent) +{ + return __dt_n_size_cells(parent, true); +} + +/* + * These are defined in Linux where much of this code comes from, but + * are currently unused outside this file in the context of Xen. + */ +#define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */ + +#define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */ +#define IORESOURCE_IO 0x00000100 /* PCI/ISA I/O ports */ +#define IORESOURCE_MEM 0x00000200 +#define IORESOURCE_REG 0x00000300 /* Register offsets */ +#define IORESOURCE_IRQ 0x00000400 +#define IORESOURCE_DMA 0x00000800 +#define IORESOURCE_BUS 0x00001000 + +#define IORESOURCE_PREFETCH 0x00002000 /* No side effects */ +#define IORESOURCE_READONLY 0x00004000 +#define IORESOURCE_CACHEABLE 0x00008000 +#define IORESOURCE_RANGELENGTH 0x00010000 +#define IORESOURCE_SHADOWABLE 0x00020000 + +/* + * Default translator (generic bus) + */ +static bool dt_bus_default_match(const struct dt_device_node *node) +{ + /* Root node doesn't have "ranges" property */ + if ( node->parent =3D=3D NULL ) + return 1; + + /* The default bus is only used when the "ranges" property exists. + * Otherwise we can't translate the address + */ + return (dt_get_property(node, "ranges", NULL) !=3D NULL); +} + +static void dt_bus_default_count_cells(const struct dt_device_node *dev, + int *addrc, int *sizec) +{ + if ( addrc ) + *addrc =3D dt_n_addr_cells(dev); + if ( sizec ) + *sizec =3D dt_n_size_cells(dev); +} + +static u64 dt_bus_default_map(__be32 *addr, const __be32 *range, + int na, int ns, int pna) +{ + u64 cp, s, da; + + cp =3D dt_read_number(range, na); + s =3D dt_read_number(range + na + pna, ns); + da =3D dt_read_number(addr, na); + + dt_dprintk("DT: default map, cp=3D%llx, s=3D%llx, da=3D%llx\n", + (unsigned long long)cp, (unsigned long long)s, + (unsigned long long)da); + + /* + * If the number of address cells is larger than 2 we assume the + * mapping doesn't specify a physical address. Rather, the address + * specifies an identifier that must match exactly. + */ + if ( na > 2 && memcmp(range, addr, na * 4) !=3D 0 ) + return DT_BAD_ADDR; + + if ( da < cp || da >=3D (cp + s) ) + return DT_BAD_ADDR; + return da - cp; +} + +static int dt_bus_default_translate(__be32 *addr, u64 offset, int na) +{ + u64 a =3D dt_read_number(addr, na); + + memset(addr, 0, na * 4); + a +=3D offset; + if ( na > 1 ) + addr[na - 2] =3D cpu_to_be32(a >> 32); + addr[na - 1] =3D cpu_to_be32(a & 0xffffffffu); + + return 0; +} +static unsigned int dt_bus_default_get_flags(const __be32 *addr) +{ + return IORESOURCE_MEM; +} + +/* + * PCI bus specific translator + */ + +static bool dt_node_is_pci(const struct dt_device_node *np) +{ + bool is_pci =3D !strcmp(np->name, "pcie") || !strcmp(np->name, "pci"); + + if ( is_pci ) + printk(XENLOG_WARNING "%s: Missing device_type\n", np->full_name); + + return is_pci; +} + +static bool dt_bus_pci_match(const struct dt_device_node *np) +{ + /* + * "pciex" is PCI Express "vci" is for the /chaos bridge on 1st-gen PCI + * powermacs "ht" is hypertransport + * + * If none of the device_type match, and that the node name is + * "pcie" or "pci", accept the device as PCI (with a warning). + */ + return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") || + !strcmp(np->type, "vci") || !strcmp(np->type, "ht") || + dt_node_is_pci(np); +} + +static void dt_bus_pci_count_cells(const struct dt_device_node *np, + int *addrc, int *sizec) +{ + if (addrc) + *addrc =3D 3; + if (sizec) + *sizec =3D 2; +} + +static unsigned int dt_bus_pci_get_flags(const __be32 *addr) +{ + unsigned int flags =3D 0; + u32 w =3D be32_to_cpup(addr); + + switch((w >> 24) & 0x03) { + case 0x01: + flags |=3D IORESOURCE_IO; + break; + case 0x02: /* 32 bits */ + case 0x03: /* 64 bits */ + flags |=3D IORESOURCE_MEM; + break; + } + if (w & 0x40000000) + flags |=3D IORESOURCE_PREFETCH; + return flags; +} + +static u64 dt_bus_pci_map(__be32 *addr, const __be32 *range, int na, int n= s, + int pna) +{ + u64 cp, s, da; + unsigned int af, rf; + + af =3D dt_bus_pci_get_flags(addr); + rf =3D dt_bus_pci_get_flags(range); + + /* Check address type match */ + if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) + return DT_BAD_ADDR; + + /* Read address values, skipping high cell */ + cp =3D dt_read_number(range + 1, na - 1); + s =3D dt_read_number(range + na + pna, ns); + da =3D dt_read_number(addr + 1, na - 1); + + dt_dprintk("DT: PCI map, cp=3D%llx, s=3D%llx, da=3D%llx\n", + (unsigned long long)cp, (unsigned long long)s, + (unsigned long long)da); + + if (da < cp || da >=3D (cp + s)) + return DT_BAD_ADDR; + return da - cp; +} + +static int dt_bus_pci_translate(__be32 *addr, u64 offset, int na) +{ + return dt_bus_default_translate(addr + 1, offset, na - 1); +} + +/* + * Array of bus specific translators + */ +static const struct dt_bus dt_busses[] =3D +{ + /* PCI */ + { + .name =3D "pci", + .addresses =3D "assigned-addresses", + .match =3D dt_bus_pci_match, + .count_cells =3D dt_bus_pci_count_cells, + .map =3D dt_bus_pci_map, + .translate =3D dt_bus_pci_translate, + .get_flags =3D dt_bus_pci_get_flags, + }, + /* Default */ + { + .name =3D "default", + .addresses =3D "reg", + .match =3D dt_bus_default_match, + .count_cells =3D dt_bus_default_count_cells, + .map =3D dt_bus_default_map, + .translate =3D dt_bus_default_translate, + .get_flags =3D dt_bus_default_get_flags, + }, +}; + +static const struct dt_bus *dt_match_bus(const struct dt_device_node *np) +{ + int i; + + for ( i =3D 0; i < ARRAY_SIZE(dt_busses); i++ ) + if ( !dt_busses[i].match || dt_busses[i].match(np) ) + return &dt_busses[i]; + + return NULL; +} + +static const __be32 *dt_get_address(const struct dt_device_node *dev, + unsigned int index, u64 *size, + unsigned int *flags) +{ + const __be32 *prop; + u32 psize; + const struct dt_device_node *parent; + const struct dt_bus *bus; + int onesize, i, na, ns; + + /* Get parent & match bus type */ + parent =3D dt_get_parent(dev); + if ( parent =3D=3D NULL ) + return NULL; + + bus =3D dt_match_bus(parent); + if ( !bus ) + return NULL; + bus->count_cells(dev, &na, &ns); + + if ( !DT_CHECK_ADDR_COUNT(na) ) + return NULL; + + /* Get "reg" or "assigned-addresses" property */ + prop =3D dt_get_property(dev, bus->addresses, &psize); + if ( prop =3D=3D NULL ) + return NULL; + psize /=3D 4; + + onesize =3D na + ns; + for ( i =3D 0; psize >=3D onesize; psize -=3D onesize, prop +=3D onesi= ze, i++ ) + { + if ( i =3D=3D index ) + { + if ( size ) + *size =3D dt_read_number(prop + na, ns); + if ( flags ) + *flags =3D bus->get_flags(prop); + return prop; + } + } + return NULL; +} + +static int dt_translate_one(const struct dt_device_node *parent, + const struct dt_bus *bus, + const struct dt_bus *pbus, + __be32 *addr, int na, int ns, + int pna, const char *rprop) +{ + const __be32 *ranges; + unsigned int rlen; + int rone; + u64 offset =3D DT_BAD_ADDR; + + ranges =3D dt_get_property(parent, rprop, &rlen); + if ( ranges =3D=3D NULL ) + { + printk(XENLOG_ERR "DT: no ranges; cannot translate\n"); + return 1; + } + if ( rlen =3D=3D 0 ) + { + offset =3D dt_read_number(addr, na); + memset(addr, 0, pna * 4); + dt_dprintk("DT: empty ranges; 1:1 translation\n"); + goto finish; + } + + dt_dprintk("DT: walking ranges...\n"); + + /* Now walk through the ranges */ + rlen /=3D 4; + rone =3D na + pna + ns; + for ( ; rlen >=3D rone; rlen -=3D rone, ranges +=3D rone ) + { + offset =3D bus->map(addr, ranges, na, ns, pna); + if ( offset !=3D DT_BAD_ADDR ) + break; + } + if ( offset =3D=3D DT_BAD_ADDR ) + { + dt_dprintk("DT: not found !\n"); + return 1; + } + memcpy(addr, ranges + na, 4 * pna); + +finish: + dt_dump_addr("DT: parent translation for:", addr, pna); + dt_dprintk("DT: with offset: %llx\n", (unsigned long long)offset); + + /* Translate it into parent bus space */ + return pbus->translate(addr, offset, pna); +} + +/* + * Translate an address from the device-tree into a CPU physical address, + * this walks up the tree and applies the various bus mappings on the + * way. + * + * Note: We consider that crossing any level with #size-cells =3D=3D 0 to = mean + * that translation is impossible (that is we are not dealing with a value + * that can be mapped to a cpu physical address). This is not really speci= fied + * that way, but this is traditionally the way IBM at least do things + */ +static u64 __dt_translate_address(const struct dt_device_node *dev, + const __be32 *in_addr, const char *rprop) +{ + const struct dt_device_node *parent =3D NULL; + const struct dt_bus *bus, *pbus; + __be32 addr[DT_MAX_ADDR_CELLS]; + int na, ns, pna, pns; + u64 result =3D DT_BAD_ADDR; + + dt_dprintk("DT: ** translation for device %s **\n", dev->full_name); + + /* Get parent & match bus type */ + parent =3D dt_get_parent(dev); + if ( parent =3D=3D NULL ) + goto bail; + bus =3D dt_match_bus(parent); + if ( !bus ) + goto bail; + + /* Count address cells & copy address locally */ + bus->count_cells(dev, &na, &ns); + if ( !DT_CHECK_COUNTS(na, ns) ) + { + printk(XENLOG_ERR "dt_parse: Bad cell count for device %s\n", + dev->full_name); + goto bail; + } + memcpy(addr, in_addr, na * 4); + + dt_dprintk("DT: bus is %s (na=3D%d, ns=3D%d) on %s\n", + bus->name, na, ns, parent->full_name); + dt_dump_addr("DT: translating address:", addr, na); + + /* Translate */ + for ( ;; ) + { + /* Switch to parent bus */ + dev =3D parent; + parent =3D dt_get_parent(dev); + + /* If root, we have finished */ + if ( parent =3D=3D NULL ) + { + dt_dprintk("DT: reached root node\n"); + result =3D dt_read_number(addr, na); + break; + } + + /* Get new parent bus and counts */ + pbus =3D dt_match_bus(parent); + if ( pbus =3D=3D NULL ) + { + printk("DT: %s is not a valid bus\n", parent->full_name); + break; + } + pbus->count_cells(dev, &pna, &pns); + if ( !DT_CHECK_COUNTS(pna, pns) ) + { + printk(XENLOG_ERR "dt_parse: Bad cell count for parent %s\n", + dev->full_name); + break; + } + + dt_dprintk("DT: parent bus is %s (na=3D%d, ns=3D%d) on %s\n", + pbus->name, pna, pns, parent->full_name); + + /* Apply bus translation */ + if ( dt_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop) ) + break; + + /* Complete the move up one level */ + na =3D pna; + ns =3D pns; + bus =3D pbus; + + dt_dump_addr("DT: one level translation:", addr, na); + } + +bail: + return result; +} + +/* dt_device_address - Translate device tree address and return it */ +int dt_device_get_address(const struct dt_device_node *dev, unsigned int i= ndex, + u64 *addr, u64 *size) +{ + const __be32 *addrp; + unsigned int flags; + + addrp =3D dt_get_address(dev, index, size, &flags); + if ( addrp =3D=3D NULL ) + return -EINVAL; + + if ( !addr ) + return -EINVAL; + + *addr =3D __dt_translate_address(dev, addrp, "ranges"); + + if ( *addr =3D=3D DT_BAD_ADDR ) + return -EINVAL; + + return 0; +} + +int dt_device_get_paddr(const struct dt_device_node *dev, unsigned int ind= ex, + paddr_t *addr, paddr_t *size) +{ + uint64_t dt_addr, dt_size; + int ret; + + ret =3D dt_device_get_address(dev, index, &dt_addr, &dt_size); + if ( ret ) + return ret; + + if ( !addr ) + return -EINVAL; + + if ( dt_addr !=3D (paddr_t)dt_addr ) + { + printk("Error: Physical address 0x%"PRIx64" for node=3D%s is great= er than max width (%zu bytes) supported\n", + dt_addr, dev->name, sizeof(paddr_t)); + return -ERANGE; + } + + *addr =3D dt_addr; + + if ( size ) + { + if ( dt_size !=3D (paddr_t)dt_size ) + { + printk("Error: Physical size 0x%"PRIx64" for node=3D%s is grea= ter than max width (%zu bytes) supported\n", + dt_size, dev->name, sizeof(paddr_t)); + return -ERANGE; + } + + *size =3D dt_size; + } + + return ret; +} + +int dt_for_each_range(const struct dt_device_node *dev, + int (*cb)(const struct dt_device_node *dev, + uint64_t addr, uint64_t length, + void *data), + void *data) +{ + const struct dt_device_node *parent =3D NULL; + const struct dt_bus *bus, *pbus; + const __be32 *ranges; + __be32 addr[DT_MAX_ADDR_CELLS]; + unsigned int rlen; + int na, ns, pna, pns, rone; + + bus =3D dt_match_bus(dev); + if ( !bus ) + return 0; /* device is not a bus */ + + parent =3D dt_get_parent(dev); + if ( parent =3D=3D NULL ) + return -EINVAL; + + ranges =3D dt_get_property(dev, "ranges", &rlen); + if ( ranges =3D=3D NULL ) + { + printk(XENLOG_ERR "DT: no ranges; cannot enumerate %s\n", + dev->full_name); + return -EINVAL; + } + if ( rlen =3D=3D 0 ) /* Nothing to do */ + return 0; + + bus->count_cells(dev, &na, &ns); + if ( !DT_CHECK_COUNTS(na, ns) ) + { + printk(XENLOG_ERR "dt_parse: Bad cell count for device %s\n", + dev->full_name); + return -EINVAL; + } + + pbus =3D dt_match_bus(parent); + if ( pbus =3D=3D NULL ) + { + printk("DT: %s is not a valid bus\n", parent->full_name); + return -EINVAL; + } + + pbus->count_cells(dev, &pna, &pns); + if ( !DT_CHECK_COUNTS(pna, pns) ) + { + printk(XENLOG_ERR "dt_parse: Bad cell count for parent %s\n", + dev->full_name); + return -EINVAL; + } + + /* Now walk through the ranges */ + rlen /=3D 4; + rone =3D na + pna + ns; + + dt_dprintk("%s: dev=3D%s, bus=3D%s, parent=3D%s, rlen=3D%d, rone=3D%d\= n", + __func__, + dt_node_name(dev), bus->name, + dt_node_name(parent), rlen, rone); + + for ( ; rlen >=3D rone; rlen -=3D rone, ranges +=3D rone ) + { + uint64_t a, s; + int ret; + + memcpy(addr, ranges + na, 4 * pna); + + a =3D __dt_translate_address(dev, addr, "ranges"); + s =3D dt_read_number(ranges + na + pna, ns); + + ret =3D cb(dev, a, s, data); + if ( ret ) + { + dt_dprintk(" -> callback failed=3D%d\n", ret); + return ret; + } + + } + + return 0; +} + +/** + * dt_find_node_by_phandle - Find a node given a phandle + * @handle: phandle of the node to find + * + * Returns a node pointer. + */ +struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle) +{ + struct dt_device_node *np; + + dt_for_each_device_node(dt_host, np) + if ( np->phandle =3D=3D handle ) + break; + + return np; +} + +/** + * dt_irq_find_parent - Given a device node, find its interrupt parent node + * @child: pointer to device node + * + * Returns a pointer to the interrupt parent node, or NULL if the interrupt + * parent could not be determined. + */ +static const struct dt_device_node * +dt_irq_find_parent(const struct dt_device_node *child) +{ + const struct dt_device_node *p; + const __be32 *parp; + + do + { + parp =3D dt_get_property(child, "interrupt-parent", NULL); + if ( parp =3D=3D NULL ) + p =3D dt_get_parent(child); + else + p =3D dt_find_node_by_phandle(be32_to_cpup(parp)); + child =3D p; + } while ( p && dt_get_property(p, "#interrupt-cells", NULL) =3D=3D NUL= L ); + + return p; +} + +unsigned int dt_number_of_irq(const struct dt_device_node *device) +{ + const struct dt_device_node *p; + const __be32 *intspec, *tmp; + u32 intsize, intlen; + int intnum; + + dt_dprintk("dt_irq_number: dev=3D%s\n", device->full_name); + + /* Try the new-style interrupts-extended first */ + intnum =3D dt_count_phandle_with_args(device, "interrupts-extended", + "#interrupt-cells"); + if ( intnum >=3D 0 ) + { + dt_dprintk(" using 'interrupts-extended' property\n"); + dt_dprintk(" intnum=3D%d\n", intnum); + return intnum; + } + + /* Get the interrupts property */ + intspec =3D dt_get_property(device, "interrupts", &intlen); + if ( intspec =3D=3D NULL ) + return 0; + intlen /=3D sizeof(*intspec); + + dt_dprintk(" using 'interrupts' property\n"); + dt_dprintk(" intspec=3D%d intlen=3D%d\n", be32_to_cpup(intspec), intle= n); + + /* Look for the interrupt parent. */ + p =3D dt_irq_find_parent(device); + if ( p =3D=3D NULL ) + return 0; + + /* Get size of interrupt specifier */ + tmp =3D dt_get_property(p, "#interrupt-cells", NULL); + if ( tmp =3D=3D NULL ) + return 0; + intsize =3D be32_to_cpu(*tmp); + + dt_dprintk(" intsize=3D%d intlen=3D%d\n", intsize, intlen); + + return (intlen / intsize); +} + +unsigned int dt_number_of_address(const struct dt_device_node *dev) +{ + const __be32 *prop; + u32 psize; + const struct dt_device_node *parent; + const struct dt_bus *bus; + int onesize, na, ns; + + /* Get parent & match bus type */ + parent =3D dt_get_parent(dev); + if ( parent =3D=3D NULL ) + return 0; + + bus =3D dt_match_bus(parent); + if ( !bus ) + return 0; + bus->count_cells(dev, &na, &ns); + + if ( !DT_CHECK_COUNTS(na, ns) ) + return 0; + + /* Get "reg" or "assigned-addresses" property */ + prop =3D dt_get_property(dev, bus->addresses, &psize); + if ( prop =3D=3D NULL ) + return 0; + + psize /=3D 4; + onesize =3D na + ns; + + return (psize / onesize); +} + +int dt_for_each_irq_map(const struct dt_device_node *dev, + int (*cb)(const struct dt_device_node *dev, + const struct dt_irq *dt_irq, + void *data), + void *data) +{ + const struct dt_device_node *ipar, *tnode, *old =3D NULL; + const __be32 *tmp, *imap; + u32 intsize =3D 1, addrsize, pintsize =3D 0, paddrsize =3D 0; + u32 imaplen; + int i, ret; + + struct dt_raw_irq dt_raw_irq; + struct dt_irq dt_irq; + + dt_dprintk("%s: par=3D%s cb=3D%p data=3D%p\n", __func__, + dev->full_name, cb, data); + + ipar =3D dev; + + /* First get the #interrupt-cells property of the current cursor + * that tells us how to interpret the passed-in intspec. If there + * is none, we are nice and just walk up the tree + */ + do { + tmp =3D dt_get_property(ipar, "#interrupt-cells", NULL); + if ( tmp !=3D NULL ) + { + intsize =3D be32_to_cpu(*tmp); + break; + } + tnode =3D ipar; + ipar =3D dt_irq_find_parent(ipar); + } while ( ipar ); + if ( ipar =3D=3D NULL ) + { + dt_dprintk(" -> no parent found !\n"); + goto fail; + } + + dt_dprintk("%s: ipar=3D%s, size=3D%d\n", __func__, ipar->full_name, in= tsize); + + if ( intsize > DT_MAX_IRQ_SPEC ) + { + dt_dprintk(" -> too many irq specifier cells\n"); + goto fail; + } + + /* Look for this #address-cells. We have to implement the old linux + * trick of looking for the parent here as some device-trees rely on it + */ + old =3D ipar; + do { + tmp =3D dt_get_property(old, "#address-cells", NULL); + tnode =3D dt_get_parent(old); + old =3D tnode; + } while ( old && tmp =3D=3D NULL ); + + old =3D NULL; + addrsize =3D (tmp =3D=3D NULL) ? 2 : be32_to_cpu(*tmp); + + dt_dprintk(" -> addrsize=3D%d\n", addrsize); + + /* Now look for an interrupt-map */ + imap =3D dt_get_property(dev, "interrupt-map", &imaplen); + /* No interrupt-map found. Ignore */ + if ( imap =3D=3D NULL ) + { + dt_dprintk(" -> no map, ignoring\n"); + return 0; + } + imaplen /=3D sizeof(u32); + + /* Parse interrupt-map */ + while ( imaplen > (addrsize + intsize + 1) ) + { + /* skip child unit address and child interrupt specifier */ + imap +=3D addrsize + intsize; + imaplen -=3D addrsize + intsize; + + /* Get the interrupt parent */ + ipar =3D dt_find_node_by_phandle(be32_to_cpup(imap)); + imap++; + --imaplen; + + /* Check if not found */ + if ( ipar =3D=3D NULL ) + { + dt_dprintk(" -> imap parent not found !\n"); + goto fail; + } + + dt_dprintk(" -> ipar %s\n", dt_node_name(ipar)); + + /* Get #interrupt-cells and #address-cells of new + * parent + */ + tmp =3D dt_get_property(ipar, "#interrupt-cells", NULL); + if ( tmp =3D=3D NULL ) + { + dt_dprintk(" -> parent lacks #interrupt-cells!\n"); + goto fail; + } + pintsize =3D be32_to_cpu(*tmp); + tmp =3D dt_get_property(ipar, "#address-cells", NULL); + paddrsize =3D (tmp =3D=3D NULL) ? 0 : be32_to_cpu(*tmp); + + dt_dprintk(" -> pintsize=3D%d, paddrsize=3D%d\n", + pintsize, paddrsize); + + if ( pintsize > DT_MAX_IRQ_SPEC ) + { + dt_dprintk(" -> too many irq specifier cells in parent\n"); + goto fail; + } + + /* Check for malformed properties */ + if ( imaplen < (paddrsize + pintsize) ) + goto fail; + + imap +=3D paddrsize; + imaplen -=3D paddrsize; + + dt_raw_irq.controller =3D ipar; + dt_raw_irq.size =3D pintsize; + for ( i =3D 0; i < pintsize; i++ ) + dt_raw_irq.specifier[i] =3D dt_read_number(imap + i, 1); + + if ( dt_raw_irq.controller !=3D dt_interrupt_controller ) + { + /* + * We don't map IRQs connected to secondary IRQ controllers as + * these IRQs have no meaning to us until they connect to the + * primary controller. + * + * Secondary IRQ controllers will at some point connect to + * the primary controller (possibly via other IRQ controllers). + * We map the IRQs at that last connection point. + */ + imap +=3D pintsize; + imaplen -=3D pintsize; + dt_dprintk(" -> Skipped IRQ for secondary IRQ controller\n"); + continue; + } + + ret =3D dt_irq_translate(&dt_raw_irq, &dt_irq); + if ( ret ) + { + dt_dprintk(" -> failed to translate IRQ: %d\n", ret); + return ret; + } + + ret =3D cb(dev, &dt_irq, data); + if ( ret ) + { + dt_dprintk(" -> callback failed=3D%d\n", ret); + return ret; + } + + imap +=3D pintsize; + imaplen -=3D pintsize; + + dt_dprintk(" -> imaplen=3D%d\n", imaplen); + } + + return 0; + +fail: + return -EINVAL; +} + +/** + * dt_irq_map_raw - Low level interrupt tree parsing + * @parent: the device interrupt parent + * @intspec: interrupt specifier ("interrupts" property of the device) + * @ointsize: size of the passed in interrupt specifier + * @addr: address specifier (start of "reg" property of the device) + * @oirq: structure dt_raw_irq filled by this function + * + * Returns 0 on success and a negative number on error + * + * This function is a low-level interrupt tree walking function. It + * can be used to do a partial walk with synthesized reg and interrupts + * properties, for example when resolving PCI interrupts when no device + * node exist for the parent. + */ +static int dt_irq_map_raw(const struct dt_device_node *parent, + const __be32 *intspec, u32 ointsize, + const __be32 *addr, + struct dt_raw_irq *oirq) +{ + const struct dt_device_node *ipar, *tnode, *old =3D NULL, *newpar =3D = NULL; + const __be32 *tmp, *imap, *imask; + u32 intsize =3D 1, addrsize, newintsize =3D 0, newaddrsize =3D 0; + u32 imaplen; + int match, i; + + dt_dprintk("dt_irq_map_raw: par=3D%s,intspec=3D[0x%08x 0x%08x...],oint= size=3D%d\n", + parent->full_name, be32_to_cpup(intspec), + be32_to_cpup(intspec + 1), ointsize); + + ipar =3D parent; + + /* First get the #interrupt-cells property of the current cursor + * that tells us how to interpret the passed-in intspec. If there + * is none, we are nice and just walk up the tree + */ + do { + tmp =3D dt_get_property(ipar, "#interrupt-cells", NULL); + if ( tmp !=3D NULL ) + { + intsize =3D be32_to_cpu(*tmp); + break; + } + tnode =3D ipar; + ipar =3D dt_irq_find_parent(ipar); + } while ( ipar ); + if ( ipar =3D=3D NULL ) + { + dt_dprintk(" -> no parent found !\n"); + goto fail; + } + + dt_dprintk("dt_irq_map_raw: ipar=3D%s, size=3D%d\n", ipar->full_name, = intsize); + + if ( ointsize !=3D intsize ) + return -EINVAL; + + /* Look for this #address-cells. We have to implement the old linux + * trick of looking for the parent here as some device-trees rely on it + */ + old =3D ipar; + do { + tmp =3D dt_get_property(old, "#address-cells", NULL); + tnode =3D dt_get_parent(old); + old =3D tnode; + } while ( old && tmp =3D=3D NULL ); + + old =3D NULL; + addrsize =3D (tmp =3D=3D NULL) ? 2 : be32_to_cpu(*tmp); + + dt_dprintk(" -> addrsize=3D%d\n", addrsize); + + /* Now start the actual "proper" walk of the interrupt tree */ + while ( ipar !=3D NULL ) + { + /* Now check if cursor is an interrupt-controller and if it is + * then we are done + */ + if ( dt_get_property(ipar, "interrupt-controller", NULL) !=3D NULL= ) + { + dt_dprintk(" -> got it !\n"); + if ( intsize > DT_MAX_IRQ_SPEC ) + { + dt_dprintk(" -> intsize(%u) greater than DT_MAX_IRQ_SPEC(%= u)\n", + intsize, DT_MAX_IRQ_SPEC); + goto fail; + } + for ( i =3D 0; i < intsize; i++ ) + oirq->specifier[i] =3D dt_read_number(intspec + i, 1); + oirq->size =3D intsize; + oirq->controller =3D ipar; + return 0; + } + + /* Now look for an interrupt-map */ + imap =3D dt_get_property(ipar, "interrupt-map", &imaplen); + /* No interrupt map, check for an interrupt parent */ + if ( imap =3D=3D NULL ) + { + dt_dprintk(" -> no map, getting parent\n"); + newpar =3D dt_irq_find_parent(ipar); + goto skiplevel; + } + imaplen /=3D sizeof(u32); + + /* Look for a mask */ + imask =3D dt_get_property(ipar, "interrupt-map-mask", NULL); + + /* If we were passed no "reg" property and we attempt to parse + * an interrupt-map, then #address-cells must be 0. + * Fail if it's not. + */ + if ( addr =3D=3D NULL && addrsize !=3D 0 ) + { + dt_dprintk(" -> no reg passed in when needed !\n"); + goto fail; + } + + /* Parse interrupt-map */ + match =3D 0; + while ( imaplen > (addrsize + intsize + 1) && !match ) + { + /* Compare specifiers */ + match =3D 1; + for ( i =3D 0; i < addrsize && match; ++i ) + { + __be32 mask =3D imask ? imask[i] : cpu_to_be32(0xffffffffu= ); + match =3D ((addr[i] ^ imap[i]) & mask) =3D=3D 0; + } + for ( ; i < (addrsize + intsize) && match; ++i ) + { + __be32 mask =3D imask ? imask[i] : cpu_to_be32(0xffffffffu= ); + match =3D ((intspec[i-addrsize] ^ imap[i]) & mask) =3D=3D = 0; + } + imap +=3D addrsize + intsize; + imaplen -=3D addrsize + intsize; + + dt_dprintk(" -> match=3D%d (imaplen=3D%d)\n", match, imaplen); + + /* Get the interrupt parent */ + newpar =3D dt_find_node_by_phandle(be32_to_cpup(imap)); + imap++; + --imaplen; + + /* Check if not found */ + if ( newpar =3D=3D NULL ) + { + dt_dprintk(" -> imap parent not found !\n"); + goto fail; + } + + /* Get #interrupt-cells and #address-cells of new + * parent + */ + tmp =3D dt_get_property(newpar, "#interrupt-cells", NULL); + if ( tmp =3D=3D NULL ) + { + dt_dprintk(" -> parent lacks #interrupt-cells!\n"); + goto fail; + } + newintsize =3D be32_to_cpu(*tmp); + tmp =3D dt_get_property(newpar, "#address-cells", NULL); + newaddrsize =3D (tmp =3D=3D NULL) ? 0 : be32_to_cpu(*tmp); + + dt_dprintk(" -> newintsize=3D%d, newaddrsize=3D%d\n", + newintsize, newaddrsize); + + /* Check for malformed properties */ + if ( imaplen < (newaddrsize + newintsize) ) + goto fail; + + imap +=3D newaddrsize + newintsize; + imaplen -=3D newaddrsize + newintsize; + + dt_dprintk(" -> imaplen=3D%d\n", imaplen); + } + if ( !match ) + goto fail; + + old =3D newpar; + addrsize =3D newaddrsize; + intsize =3D newintsize; + intspec =3D imap - intsize; + addr =3D intspec - addrsize; + + skiplevel: + /* Iterate again with new parent */ + dt_dprintk(" -> new parent: %s\n", dt_node_full_name(newpar)); + ipar =3D newpar; + newpar =3D NULL; + } +fail: + return -EINVAL; +} + +int dt_device_get_raw_irq(const struct dt_device_node *device, + unsigned int index, + struct dt_raw_irq *out_irq) +{ + const struct dt_device_node *p; + const __be32 *intspec, *tmp, *addr; + u32 intsize, intlen; + int res =3D -EINVAL; + struct dt_phandle_args args; + int i; + + dt_dprintk("dt_device_get_raw_irq: dev=3D%s, index=3D%u\n", + device->full_name, index); + + /* Get the reg property (if any) */ + addr =3D dt_get_property(device, "reg", NULL); + + /* Try the new-style interrupts-extended first */ + res =3D dt_parse_phandle_with_args(device, "interrupts-extended", + "#interrupt-cells", index, &args); + if ( !res ) + { + dt_dprintk(" using 'interrupts-extended' property\n"); + dt_dprintk(" intspec=3D%d intsize=3D%d\n", args.args[0], args.args= _count); + + for ( i =3D 0; i < args.args_count; i++ ) + args.args[i] =3D cpu_to_be32(args.args[i]); + + return dt_irq_map_raw(args.np, args.args, args.args_count, + addr, out_irq); + } + + /* Get the interrupts property */ + intspec =3D dt_get_property(device, "interrupts", &intlen); + if ( intspec =3D=3D NULL ) + return -EINVAL; + intlen /=3D sizeof(*intspec); + + dt_dprintk(" using 'interrupts' property\n"); + dt_dprintk(" intspec=3D%d intlen=3D%d\n", be32_to_cpup(intspec), intle= n); + + /* Look for the interrupt parent. */ + p =3D dt_irq_find_parent(device); + if ( p =3D=3D NULL ) + return -EINVAL; + + /* Get size of interrupt specifier */ + tmp =3D dt_get_property(p, "#interrupt-cells", NULL); + if ( tmp =3D=3D NULL ) + goto out; + intsize =3D be32_to_cpu(*tmp); + + dt_dprintk(" intsize=3D%d intlen=3D%d\n", intsize, intlen); + + /* Check index */ + if ( (index + 1) * intsize > intlen ) + goto out; + + /* Get new specifier and map it */ + res =3D dt_irq_map_raw(p, intspec + index * intsize, intsize, + addr, out_irq); + if ( res ) + goto out; +out: + return res; +} + +int dt_irq_translate(const struct dt_raw_irq *raw, + struct dt_irq *out_irq) +{ + ASSERT(dt_irq_xlate !=3D NULL); + ASSERT(dt_interrupt_controller !=3D NULL); + + /* + * TODO: Retrieve the right irq_xlate. This is only works for the prim= ary + * interrupt controller. + */ + if ( raw->controller !=3D dt_interrupt_controller ) + return -EINVAL; + + return dt_irq_xlate(raw->specifier, raw->size, + &out_irq->irq, &out_irq->type); +} + +int dt_device_get_irq(const struct dt_device_node *device, unsigned int in= dex, + struct dt_irq *out_irq) +{ + struct dt_raw_irq raw; + int res; + + res =3D dt_device_get_raw_irq(device, index, &raw); + + if ( res ) + return res; + + return dt_irq_translate(&raw, out_irq); +} + +bool dt_device_is_available(const struct dt_device_node *device) +{ + const char *status; + u32 statlen; + + status =3D dt_get_property(device, "status", &statlen); + if ( status =3D=3D NULL ) + return 1; + + if ( statlen > 0 ) + { + if ( !strcmp(status, "okay") || !strcmp(status, "ok") ) + return 1; + } + + return 0; +} + +bool dt_device_for_passthrough(const struct dt_device_node *device) +{ + return (dt_find_property(device, "xen,passthrough", NULL) !=3D NULL); + +} + +static int __dt_parse_phandle_with_args(const struct dt_device_node *np, + const char *list_name, + const char *cells_name, + int cell_count, int index, + struct dt_phandle_args *out_args) +{ + const __be32 *list, *list_end; + int rc =3D 0, cur_index =3D 0; + u32 size, count =3D 0; + struct dt_device_node *node =3D NULL; + dt_phandle phandle; + + /* Retrieve the phandle list property */ + list =3D dt_get_property(np, list_name, &size); + if ( !list ) + return -ENOENT; + list_end =3D list + size / sizeof(*list); + + /* Loop over the phandles until all the requested entry is found */ + while ( list < list_end ) + { + rc =3D -EINVAL; + count =3D 0; + + /* + * If phandle is 0, then it is an empty entry with no + * arguments. Skip forward to the next entry. + * */ + phandle =3D be32_to_cpup(list++); + if ( phandle ) + { + /* + * Find the provider node and parse the #*-cells + * property to determine the argument length. + * + * This is not needed if the cell count is hard-coded + * (i.e. cells_name not set, but cell_count is set), + * except when we're going to return the found node + * below. + */ + if ( cells_name || cur_index =3D=3D index ) + { + node =3D dt_find_node_by_phandle(phandle); + if ( !node ) + { + printk(XENLOG_ERR "%s: could not find phandle\n", + np->full_name); + goto err; + } + } + + if ( cells_name ) + { + if ( !dt_property_read_u32(node, cells_name, &count) ) + { + printk("%s: could not get %s for %s\n", + np->full_name, cells_name, node->full_name); + goto err; + } + } + else + count =3D cell_count; + + /* + * Make sure that the arguments actually fit in the + * remaining property data length + */ + if ( list + count > list_end ) + { + printk(XENLOG_ERR "%s: arguments longer than property\n", + np->full_name); + goto err; + } + } + + /* + * All of the error cases above bail out of the loop, so at + * this point, the parsing is successful. If the requested + * index matches, then fill the out_args structure and return, + * or return -ENOENT for an empty entry. + */ + rc =3D -ENOENT; + if ( cur_index =3D=3D index ) + { + if (!phandle) + goto err; + + if ( out_args ) + { + int i; + + WARN_ON(count > MAX_PHANDLE_ARGS); + if (count > MAX_PHANDLE_ARGS) + count =3D MAX_PHANDLE_ARGS; + out_args->np =3D node; + out_args->args_count =3D count; + for ( i =3D 0; i < count; i++ ) + out_args->args[i] =3D be32_to_cpup(list++); + } + + /* Found it! return success */ + return 0; + } + + node =3D NULL; + list +=3D count; + cur_index++; + } + + /* + * Returning result will be one of: + * -ENOENT : index is for empty phandle + * -EINVAL : parsing error on data + * [1..n] : Number of phandle (count mode; when index =3D -1) + */ + rc =3D index < 0 ? cur_index : -ENOENT; +err: + return rc; +} + +struct dt_device_node *dt_parse_phandle(const struct dt_device_node *np, + const char *phandle_name, int inde= x) +{ + struct dt_phandle_args args; + + if (index < 0) + return NULL; + + if (__dt_parse_phandle_with_args(np, phandle_name, NULL, 0, + index, &args)) + return NULL; + + return args.np; +} + + +int dt_parse_phandle_with_args(const struct dt_device_node *np, + const char *list_name, + const char *cells_name, int index, + struct dt_phandle_args *out_args) +{ + if ( index < 0 ) + return -EINVAL; + return __dt_parse_phandle_with_args(np, list_name, cells_name, 0, + index, out_args); +} + +int dt_count_phandle_with_args(const struct dt_device_node *np, + const char *list_name, + const char *cells_name) +{ + return __dt_parse_phandle_with_args(np, list_name, cells_name, 0, -1, = NULL); +} + +/** + * unflatten_dt_node - Alloc and populate a device_node from the flat tree + * @fdt: The parent device tree blob + * @mem: Memory chunk to use for allocating device nodes and properties + * @p: pointer to node in flat tree + * @dad: Parent struct device_node + * @allnextpp: pointer to ->allnext from last allocated device_node + * @fpsize: Size of the node path up at the current depth. + */ +static unsigned long unflatten_dt_node(const void *fdt, + unsigned long mem, + unsigned long *p, + struct dt_device_node *dad, + struct dt_device_node ***allnextpp, + unsigned long fpsize) +{ + struct dt_device_node *np; + struct dt_property *pp, **prev_pp =3D NULL; + char *pathp; + u32 tag; + unsigned int l, allocl; + int has_name =3D 0; + int new_format =3D 0; + + tag =3D be32_to_cpup((__be32 *)(*p)); + if ( tag !=3D FDT_BEGIN_NODE ) + { + printk(XENLOG_WARNING "Weird tag at start of node: %x\n", tag); + return mem; + } + *p +=3D 4; + pathp =3D (char *)*p; + l =3D allocl =3D strlen(pathp) + 1; + *p =3D ROUNDUP(*p + l, 4); + + /* version 0x10 has a more compact unit name here instead of the full + * path. we accumulate the full path size using "fpsize", we'll rebuild + * it later. We detect this because the first character of the name is + * not '/'. + */ + if ( (*pathp) !=3D '/' ) + { + new_format =3D 1; + if ( fpsize =3D=3D 0 ) + { + /* root node: special case. fpsize accounts for path + * plus terminating zero. root node only has '/', so + * fpsize should be 2, but we want to avoid the first + * level nodes to have two '/' so we use fpsize 1 here + */ + fpsize =3D 1; + allocl =3D 2; + } + else + { + /* account for '/' and path size minus terminal 0 + * already in 'l' + */ + fpsize +=3D l; + allocl =3D fpsize; + } + } + + np =3D unflatten_dt_alloc(&mem, sizeof(struct dt_device_node) + allocl, + __alignof__(struct dt_device_node)); + if ( allnextpp ) + { + memset(np, 0, sizeof(*np)); + np->full_name =3D ((char *)np) + sizeof(struct dt_device_node); + /* By default dom0 owns the device */ + np->used_by =3D 0; + /* By default the device is not protected */ + np->is_protected =3D false; + INIT_LIST_HEAD(&np->domain_list); + + if ( new_format ) + { + char *fn =3D np->full_name; + /* rebuild full path for new format */ + if ( dad && dad->parent ) + { + strlcpy(fn, dad->full_name, allocl); +#ifdef DEBUG_DT + if ( (strlen(fn) + l + 1) !=3D allocl ) + { + dt_dprintk("%s: p: %d, l: %d, a: %d\n", + pathp, (int)strlen(fn), + l, allocl); + } +#endif + fn +=3D strlen(fn); + } + *(fn++) =3D '/'; + memcpy(fn, pathp, l); + } + else + memcpy(np->full_name, pathp, l); + prev_pp =3D &np->properties; + **allnextpp =3D np; + *allnextpp =3D &np->allnext; + if ( dad !=3D NULL ) + { + np->parent =3D dad; + /* we temporarily use the next field as `last_child'*/ + if ( dad->next =3D=3D NULL ) + dad->child =3D np; + else + dad->next->sibling =3D np; + dad->next =3D np; + } + } + /* process properties */ + while ( 1 ) + { + u32 sz, noff; + const char *pname; + + tag =3D be32_to_cpup((__be32 *)(*p)); + if ( tag =3D=3D FDT_NOP ) + { + *p +=3D 4; + continue; + } + if ( tag !=3D FDT_PROP ) + break; + *p +=3D 4; + sz =3D be32_to_cpup((__be32 *)(*p)); + noff =3D be32_to_cpup((__be32 *)((*p) + 4)); + *p +=3D 8; + if ( fdt_version(fdt) < 0x10 ) + *p =3D ROUNDUP(*p, sz >=3D 8 ? 8 : 4); + + pname =3D fdt_string(fdt, noff); + if ( pname =3D=3D NULL ) + { + dt_dprintk("Can't find property name in list!\n"); + break; + } + if ( strcmp(pname, "name") =3D=3D 0 ) + has_name =3D 1; + l =3D strlen(pname) + 1; + pp =3D unflatten_dt_alloc(&mem, sizeof(struct dt_property), + __alignof__(struct dt_property)); + if ( allnextpp ) + { + /* We accept flattened tree phandles either in + * ePAPR-style "phandle" properties, or the + * legacy "linux,phandle" properties. If both + * appear and have different values, things + * will get weird. Don't do that. */ + if ( (strcmp(pname, "phandle") =3D=3D 0) || + (strcmp(pname, "linux,phandle") =3D=3D 0) ) + { + if ( np->phandle =3D=3D 0 ) + np->phandle =3D be32_to_cpup((__be32*)*p); + } + /* And we process the "ibm,phandle" property + * used in pSeries dynamic device tree + * stuff */ + if ( strcmp(pname, "ibm,phandle") =3D=3D 0 ) + np->phandle =3D be32_to_cpup((__be32 *)*p); + pp->name =3D pname; + pp->length =3D sz; + pp->value =3D (void *)*p; + *prev_pp =3D pp; + prev_pp =3D &pp->next; + } + *p =3D ROUNDUP((*p) + sz, 4); + } + /* with version 0x10 we may not have the name property, recreate + * it here from the unit name if absent + */ + if ( !has_name ) + { + char *p1 =3D pathp, *ps =3D pathp, *pa =3D NULL; + int sz; + + while ( *p1 ) + { + if ( (*p1) =3D=3D '@' ) + pa =3D p1; + if ( (*p1) =3D=3D '/' ) + ps =3D p1 + 1; + p1++; + } + if ( pa < ps ) + pa =3D p1; + sz =3D (pa - ps) + 1; + pp =3D unflatten_dt_alloc(&mem, sizeof(struct dt_property) + sz, + __alignof__(struct dt_property)); + if ( allnextpp ) + { + pp->name =3D "name"; + pp->length =3D sz; + pp->value =3D pp + 1; + /* + * The device tree creation code assume that the property + * "name" is not a fake. + * To avoid a big divergence with Linux code, only remove + * property link. In this case we will lose a bit of memory + */ +#if 0 + *prev_pp =3D pp; + prev_pp =3D &pp->next; +#endif + np->name =3D pp->value; + memcpy(pp->value, ps, sz - 1); + ((char *)pp->value)[sz - 1] =3D 0; + dt_dprintk("fixed up name for %s -> %s\n", pathp, + (char *)pp->value); + /* Generic device initialization */ + np->dev.type =3D DEV_DT; + np->dev.of_node =3D np; + } + } + if ( allnextpp ) + { + *prev_pp =3D NULL; + np->name =3D (np->name) ? : dt_get_property(np, "name", NULL); + np->type =3D dt_get_property(np, "device_type", NULL); + + if ( !np->name ) + np->name =3D ""; + if ( !np->type ) + np->type =3D ""; + } + while ( tag =3D=3D FDT_BEGIN_NODE || tag =3D=3D FDT_NOP ) + { + if ( tag =3D=3D FDT_NOP ) + *p +=3D 4; + else + mem =3D unflatten_dt_node(fdt, mem, p, np, allnextpp, fpsize); + tag =3D be32_to_cpup((__be32 *)(*p)); + } + if ( tag !=3D FDT_END_NODE ) + { + printk(XENLOG_WARNING "Weird tag at end of node: %x\n", tag); + return mem; + } + + *p +=3D 4; + return mem; +} + +int unflatten_device_tree(const void *fdt, struct dt_device_node **mynodes) +{ + unsigned long start, mem, size; + struct dt_device_node **allnextp =3D mynodes; + + dt_dprintk(" -> unflatten_device_tree()\n"); + + dt_dprintk("Unflattening device tree:\n"); + dt_dprintk("magic: %#08x\n", fdt_magic(fdt)); + dt_dprintk("size: %#08x\n", fdt_totalsize(fdt)); + dt_dprintk("version: %#08x\n", fdt_version(fdt)); + + /* First pass, scan for size */ + start =3D ((unsigned long)fdt) + fdt_off_dt_struct(fdt); + size =3D unflatten_dt_node(fdt, 0, &start, NULL, NULL, 0); + if ( !size ) + return -EINVAL; + + size =3D (size | 3) + 1; + + dt_dprintk(" size is %#lx allocating...\n", size); + + /* Allocate memory for the expanded device tree */ + mem =3D (unsigned long)_xmalloc (size + 4, __alignof__(struct dt_devic= e_node)); + if ( !mem ) + return -ENOMEM; + + ((__be32 *)mem)[size / 4] =3D cpu_to_be32(0xdeadbeefU); + + dt_dprintk(" unflattening %lx...\n", mem); + + /* Second pass, do actual unflattening */ + start =3D ((unsigned long)fdt) + fdt_off_dt_struct(fdt); + unflatten_dt_node(fdt, mem, &start, NULL, &allnextp, 0); + if ( be32_to_cpup((__be32 *)start) !=3D FDT_END ) + { + printk(XENLOG_ERR "Weird tag at end of tree: %08x\n", + *((u32 *)start)); + xfree((void *)mem); + return -EINVAL; + } + + if ( be32_to_cpu(((__be32 *)mem)[size / 4]) !=3D 0xdeadbeefU ) + { + printk(XENLOG_ERR "End of tree marker overwritten: %08x\n", + be32_to_cpu(((__be32 *)mem)[size / 4])); + xfree((void *)mem); + return -EINVAL; + } + + *allnextp =3D NULL; + + dt_dprintk(" <- unflatten_device_tree()\n"); + + return 0; +} + +static void dt_alias_add(struct dt_alias_prop *ap, + struct dt_device_node *np, + int id, const char *stem, int stem_len) +{ + ap->np =3D np; + ap->id =3D id; + strlcpy(ap->stem, stem, stem_len + 1); + list_add_tail(&ap->link, &aliases_lookup); + dt_dprintk("adding DT alias:%s: stem=3D%s id=3D%d node=3D%s\n", + ap->alias, ap->stem, ap->id, dt_node_full_name(np)); +} + +/** + * dt_alias_scan - Scan all properties of 'aliases' node + * + * The function scans all the properties of 'aliases' node and populate + * the the global lookup table with the properties. It returns the + * number of alias_prop found, or error code in error case. + */ +static void __init dt_alias_scan(void) +{ + const struct dt_property *pp; + const struct dt_device_node *aliases; + + aliases =3D dt_find_node_by_path("/aliases"); + if ( !aliases ) + return; + + dt_for_each_property_node( aliases, pp ) + { + const char *start =3D pp->name; + const char *end =3D start + strlen(start); + struct dt_device_node *np; + struct dt_alias_prop *ap; + int id, len; + + /* Skip those we do not want to proceed */ + if ( !strcmp(pp->name, "name") || + !strcmp(pp->name, "phandle") || + !strcmp(pp->name, "linux,phandle") ) + continue; + + np =3D dt_find_node_by_path(pp->value); + if ( !np ) + continue; + + /* walk the alias backwards to extract the id and work out + * the 'stem' string */ + while ( isdigit(*(end-1)) && end > start ) + end--; + len =3D end - start; + + id =3D simple_strtoll(end, NULL, 10); + + /* Allocate an alias_prop with enough space for the stem */ + ap =3D _xmalloc(sizeof(*ap) + len + 1, 4); + if ( !ap ) + continue; + ap->alias =3D start; + dt_alias_add(ap, np, id, start, len); + } +} + +struct dt_device_node * __init +dt_find_interrupt_controller(const struct dt_device_match *matches) +{ + struct dt_device_node *np =3D NULL; + + while ( (np =3D dt_find_matching_node(np, matches)) ) + { + if ( !dt_find_property(np, "interrupt-controller", NULL) ) + continue; + + if ( dt_get_parent(np) ) + break; + } + + return np; +} + +void __init dt_unflatten_host_device_tree(void) +{ + int error =3D unflatten_device_tree(device_tree_flattened, &dt_host); + + if ( error ) + panic("unflatten_device_tree failed with error %d\n", error); + + dt_alias_scan(); +} + +int dt_get_pci_domain_nr(struct dt_device_node *node) +{ + u32 domain; + int error; + + error =3D dt_property_read_u32(node, "linux,pci-domain", &domain); + if ( !error ) + return -EINVAL; + + return (u16)domain; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/common/device_tree.c b/xen/common/device_tree.c deleted file mode 100644 index 8d1017a49d..0000000000 --- a/xen/common/device_tree.c +++ /dev/null @@ -1,2253 +0,0 @@ -/* - * Device Tree - * - * Copyright (C) 2012 Citrix Systems, Inc. - * Copyright 2009 Benjamin Herrenschmidt, IBM Corp - * benh@kernel.crashing.org - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -const void *device_tree_flattened; -dt_irq_xlate_func dt_irq_xlate; -/* Host device tree */ -struct dt_device_node *dt_host; -/* Interrupt controller node*/ -const struct dt_device_node *dt_interrupt_controller; -DEFINE_RWLOCK(dt_host_lock); - -/** - * struct dt_alias_prop - Alias property in 'aliases' node - * @link: List node to link the structure in aliases_lookup list - * @alias: Alias property name - * @np: Pointer to device_node that the alias stands for - * @id: Index value from end of alias name - * @stem: Alias string without the index - * - * The structure represents one alias property of 'aliases' node as - * an entry in aliases_lookup list. - */ -struct dt_alias_prop { - struct list_head link; - const char *alias; - struct dt_device_node *np; - int id; - char stem[0]; -}; - -static LIST_HEAD(aliases_lookup); - -#ifdef CONFIG_DEVICE_TREE_DEBUG -static void dt_dump_addr(const char *s, const __be32 *addr, int na) -{ - dt_dprintk("%s", s); - while ( na-- ) - dt_dprintk(" %08x", be32_to_cpu(*(addr++))); - dt_dprintk("\n"); -} -#else -static void dt_dump_addr(const char *s, const __be32 *addr, int na) { } -#endif - -#define DT_BAD_ADDR ((u64)-1) - -/* Max address size we deal with */ -#define DT_MAX_ADDR_CELLS 4 -#define DT_CHECK_ADDR_COUNT(na) ((na) > 0 && (na) <=3D DT_MAX_ADDR_CELLS) -#define DT_CHECK_COUNTS(na, ns) (DT_CHECK_ADDR_COUNT(na) && (ns) > 0) - -/* Callbacks for bus specific translators */ -struct dt_bus -{ - const char *name; - const char *addresses; - bool (*match)(const struct dt_device_node *node); - void (*count_cells)(const struct dt_device_node *child, - int *addrc, int *sizec); - u64 (*map)(__be32 *addr, const __be32 *range, int na, int ns, int pna); - int (*translate)(__be32 *addr, u64 offset, int na); - unsigned int (*get_flags)(const __be32 *addr); -}; - -void dt_get_range(const __be32 **cellp, const struct dt_device_node *np, - u64 *address, u64 *size) -{ - *address =3D dt_next_cell(dt_n_addr_cells(np), cellp); - *size =3D dt_next_cell(dt_n_size_cells(np), cellp); -} - -void dt_set_cell(__be32 **cellp, int size, u64 val) -{ - int cells =3D size; - - while ( size-- ) - { - (*cellp)[size] =3D cpu_to_fdt32(val); - val >>=3D 32; - } - - (*cellp) +=3D cells; -} - -void dt_set_range(__be32 **cellp, const struct dt_device_node *np, - u64 address, u64 size) -{ - dt_set_cell(cellp, dt_n_addr_cells(np), address); - dt_set_cell(cellp, dt_n_size_cells(np), size); -} - -void dt_child_set_range(__be32 **cellp, int addrcells, int sizecells, - u64 address, u64 size) -{ - dt_set_cell(cellp, addrcells, address); - dt_set_cell(cellp, sizecells, size); -} - -static void __init *unflatten_dt_alloc(unsigned long *mem, unsigned long s= ize, - unsigned long align) -{ - void *res; - - *mem =3D ROUNDUP(*mem, align); - res =3D (void *)*mem; - *mem +=3D size; - - return res; -} - -/* Find a property with a given name for a given node and return it. */ -const struct dt_property *dt_find_property(const struct dt_device_node *np, - const char *name, u32 *lenp) -{ - const struct dt_property *pp; - - if ( !np ) - return NULL; - - for ( pp =3D np->properties; pp; pp =3D pp->next ) - { - if ( dt_prop_cmp(pp->name, name) =3D=3D 0 ) - { - if ( lenp ) - *lenp =3D pp->length; - break; - } - } - - return pp; -} - -const void *dt_get_property(const struct dt_device_node *np, - const char *name, u32 *lenp) -{ - const struct dt_property *pp =3D dt_find_property(np, name, lenp); - - return pp ? pp->value : NULL; -} - -bool dt_property_read_u32(const struct dt_device_node *np, - const char *name, u32 *out_value) -{ - u32 len; - const __be32 *val; - - val =3D dt_get_property(np, name, &len); - if ( !val || len < sizeof(*out_value) ) - return 0; - - *out_value =3D be32_to_cpup(val); - - return 1; -} - - -bool dt_property_read_u64(const struct dt_device_node *np, - const char *name, u64 *out_value) -{ - u32 len; - const __be32 *val; - - val =3D dt_get_property(np, name, &len); - if ( !val || len < sizeof(*out_value) ) - return 0; - - *out_value =3D dt_read_number(val, 2); - - return 1; -} -int dt_property_read_string(const struct dt_device_node *np, - const char *propname, const char **out_string) -{ - const struct dt_property *pp =3D dt_find_property(np, propname, NULL); - - if ( !pp ) - return -EINVAL; - if ( !pp->length ) - return -ENODATA; - if ( strnlen(pp->value, pp->length) >=3D pp->length ) - return -EILSEQ; - - *out_string =3D pp->value; - - return 0; -} - -/** - * dt_find_property_value_of_size - * - * @np: device node from which the property value is to be read. - * @propname: name of the property to be searched. - * @min: minimum allowed length of property value - * @max: maximum allowed length of property value (0 means unlimited) - * @len: if !=3DNULL, actual length is written to here - * - * Search for a property in a device node and valid the requested size. - * - * Return: The property value on success, -EINVAL if the property does not - * exist, -ENODATA if property does not have a value, and -EOVERFLOW if the - * property data is too small or too large. - */ -static void *dt_find_property_value_of_size(const struct dt_device_node *n= p, - const char *propname, u32 min, - u32 max, size_t *len) -{ - const struct dt_property *prop =3D dt_find_property(np, propname, NULL= ); - - if ( !prop ) - return ERR_PTR(-EINVAL); - if ( !prop->value ) - return ERR_PTR(-ENODATA); - if ( prop->length < min ) - return ERR_PTR(-EOVERFLOW); - if ( max && prop->length > max ) - return ERR_PTR(-EOVERFLOW); - - if ( len ) - *len =3D prop->length; - - return prop->value; -} - -int dt_property_read_variable_u32_array(const struct dt_device_node *np, - const char *propname, u32 *out_val= ues, - size_t sz_min, size_t sz_max) -{ - size_t sz, count; - const __be32 *val =3D dt_find_property_value_of_size(np, propname, - (sz_min * sizeof(*out_values)), - (sz_max * sizeof(*out_values)), - &sz); - - if ( IS_ERR(val) ) - return PTR_ERR(val); - - if ( !sz_max ) - sz =3D sz_min; - else - sz /=3D sizeof(*out_values); - - count =3D sz; - while ( count-- ) - *out_values++ =3D be32_to_cpup(val++); - - return sz; -} - -int dt_property_match_string(const struct dt_device_node *np, - const char *propname, const char *string) -{ - const struct dt_property *dtprop =3D dt_find_property(np, propname, NU= LL); - size_t l; - int i; - const char *p, *end; - - if ( !dtprop ) - return -EINVAL; - if ( !dtprop->value ) - return -ENODATA; - - p =3D dtprop->value; - end =3D p + dtprop->length; - - for ( i =3D 0; p < end; i++, p +=3D l ) - { - l =3D strnlen(p, end - p) + 1; - if ( p + l > end ) - return -EILSEQ; - if ( strcmp(string, p) =3D=3D 0 ) - return i; /* Found it; return index */ - } - return -ENODATA; -} - -bool dt_device_is_compatible(const struct dt_device_node *device, - const char *compat) -{ - const char* cp; - u32 cplen, l; - - cp =3D dt_get_property(device, "compatible", &cplen); - if ( cp =3D=3D NULL ) - return 0; - while ( cplen > 0 ) - { - if ( dt_compat_cmp(cp, compat) =3D=3D 0 ) - return 1; - l =3D strlen(cp) + 1; - cp +=3D l; - cplen -=3D l; - } - - return 0; -} - -bool dt_machine_is_compatible(const char *compat) -{ - const struct dt_device_node *root; - bool rc =3D false; - - root =3D dt_find_node_by_path("/"); - if ( root ) - { - rc =3D dt_device_is_compatible(root, compat); - } - return rc; -} - -struct dt_device_node *dt_find_node_by_name(struct dt_device_node *from, - const char *name) -{ - struct dt_device_node *np; - struct dt_device_node *dt; - - dt =3D from ? from->allnext : dt_host; - dt_for_each_device_node(dt, np) - if ( np->name && (dt_node_cmp(np->name, name) =3D=3D 0) ) - break; - - return np; -} - -struct dt_device_node *dt_find_node_by_type(struct dt_device_node *from, - const char *type) -{ - struct dt_device_node *np; - struct dt_device_node *dt; - - dt =3D from ? from->allnext : dt_host; - dt_for_each_device_node(dt, np) - if ( np->type && (dt_node_cmp(np->type, type) =3D=3D 0) ) - break; - - return np; -} - -struct dt_device_node *dt_find_node_by_path_from(struct dt_device_node *fr= om, - const char *path) -{ - struct dt_device_node *np; - - dt_for_each_device_node(from, np) - if ( np->full_name && (dt_node_cmp(np->full_name, path) =3D=3D 0) ) - break; - - return np; -} - -int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen, - struct dt_device_node **node) -{ - char *path; - - path =3D safe_copy_string_from_guest(u_path, u_plen, PAGE_SIZE); - if ( IS_ERR(path) ) - return PTR_ERR(path); - - *node =3D dt_find_node_by_path(path); - - xfree(path); - - return (*node =3D=3D NULL) ? -ESRCH : 0; -} - -struct dt_device_node *dt_find_node_by_alias(const char *alias) -{ - const struct dt_alias_prop *app; - - list_for_each_entry( app, &aliases_lookup, link ) - { - if ( !strcmp(app->alias, alias) ) - return app->np; - } - - return NULL; -} - -const struct dt_device_match * -dt_match_node(const struct dt_device_match *matches, - const struct dt_device_node *node) -{ - if ( !matches ) - return NULL; - - while ( matches->path || matches->type || - matches->compatible || matches->not_available || matches->prop= ) - { - bool match =3D true; - - if ( matches->path ) - match &=3D dt_node_path_is_equal(node, matches->path); - - if ( matches->type ) - match &=3D dt_device_type_is_equal(node, matches->type); - - if ( matches->compatible ) - match &=3D dt_device_is_compatible(node, matches->compatible); - - if ( matches->not_available ) - match &=3D !dt_device_is_available(node); - - if ( matches->prop ) - match &=3D dt_find_property(node, matches->prop, NULL) !=3D NU= LL; - - if ( match ) - return matches; - matches++; - } - - return NULL; -} - -const struct dt_device_node *dt_get_parent(const struct dt_device_node *no= de) -{ - if ( !node ) - return NULL; - - return node->parent; -} - -struct dt_device_node * -dt_find_compatible_node(struct dt_device_node *from, - const char *type, - const char *compatible) -{ - struct dt_device_node *np; - struct dt_device_node *dt; - - dt =3D from ? from->allnext : dt_host; - dt_for_each_device_node(dt, np) - { - if ( type - && !(np->type && (dt_node_cmp(np->type, type) =3D=3D 0)) ) - continue; - if ( dt_device_is_compatible(np, compatible) ) - break; - } - - return np; -} - -struct dt_device_node * -dt_find_matching_node(struct dt_device_node *from, - const struct dt_device_match *matches) -{ - struct dt_device_node *np; - struct dt_device_node *dt; - - dt =3D from ? from->allnext : dt_host; - dt_for_each_device_node(dt, np) - { - if ( dt_match_node(matches, np) ) - return np; - } - - return NULL; -} - -static int __dt_n_addr_cells(const struct dt_device_node *np, bool parent) -{ - const __be32 *ip; - - do { - if ( np->parent && !parent ) - np =3D np->parent; - parent =3D false; - - ip =3D dt_get_property(np, "#address-cells", NULL); - if ( ip ) - return be32_to_cpup(ip); - } while ( np->parent ); - /* No #address-cells property for the root node */ - return DT_ROOT_NODE_ADDR_CELLS_DEFAULT; -} - -static int __dt_n_size_cells(const struct dt_device_node *np, bool parent) -{ - const __be32 *ip; - - do { - if ( np->parent && !parent ) - np =3D np->parent; - parent =3D false; - - ip =3D dt_get_property(np, "#size-cells", NULL); - if ( ip ) - return be32_to_cpup(ip); - } while ( np->parent ); - /* No #address-cells property for the root node */ - return DT_ROOT_NODE_SIZE_CELLS_DEFAULT; -} - -int dt_n_addr_cells(const struct dt_device_node *np) -{ - return __dt_n_addr_cells(np, false); -} - -int dt_n_size_cells(const struct dt_device_node *np) -{ - return __dt_n_size_cells(np, false); -} - -int dt_child_n_addr_cells(const struct dt_device_node *parent) -{ - return __dt_n_addr_cells(parent, true); -} - -int dt_child_n_size_cells(const struct dt_device_node *parent) -{ - return __dt_n_size_cells(parent, true); -} - -/* - * These are defined in Linux where much of this code comes from, but - * are currently unused outside this file in the context of Xen. - */ -#define IORESOURCE_BITS 0x000000ff /* Bus-specific bits */ - -#define IORESOURCE_TYPE_BITS 0x00001f00 /* Resource type */ -#define IORESOURCE_IO 0x00000100 /* PCI/ISA I/O ports */ -#define IORESOURCE_MEM 0x00000200 -#define IORESOURCE_REG 0x00000300 /* Register offsets */ -#define IORESOURCE_IRQ 0x00000400 -#define IORESOURCE_DMA 0x00000800 -#define IORESOURCE_BUS 0x00001000 - -#define IORESOURCE_PREFETCH 0x00002000 /* No side effects */ -#define IORESOURCE_READONLY 0x00004000 -#define IORESOURCE_CACHEABLE 0x00008000 -#define IORESOURCE_RANGELENGTH 0x00010000 -#define IORESOURCE_SHADOWABLE 0x00020000 - -/* - * Default translator (generic bus) - */ -static bool dt_bus_default_match(const struct dt_device_node *node) -{ - /* Root node doesn't have "ranges" property */ - if ( node->parent =3D=3D NULL ) - return 1; - - /* The default bus is only used when the "ranges" property exists. - * Otherwise we can't translate the address - */ - return (dt_get_property(node, "ranges", NULL) !=3D NULL); -} - -static void dt_bus_default_count_cells(const struct dt_device_node *dev, - int *addrc, int *sizec) -{ - if ( addrc ) - *addrc =3D dt_n_addr_cells(dev); - if ( sizec ) - *sizec =3D dt_n_size_cells(dev); -} - -static u64 dt_bus_default_map(__be32 *addr, const __be32 *range, - int na, int ns, int pna) -{ - u64 cp, s, da; - - cp =3D dt_read_number(range, na); - s =3D dt_read_number(range + na + pna, ns); - da =3D dt_read_number(addr, na); - - dt_dprintk("DT: default map, cp=3D%llx, s=3D%llx, da=3D%llx\n", - (unsigned long long)cp, (unsigned long long)s, - (unsigned long long)da); - - /* - * If the number of address cells is larger than 2 we assume the - * mapping doesn't specify a physical address. Rather, the address - * specifies an identifier that must match exactly. - */ - if ( na > 2 && memcmp(range, addr, na * 4) !=3D 0 ) - return DT_BAD_ADDR; - - if ( da < cp || da >=3D (cp + s) ) - return DT_BAD_ADDR; - return da - cp; -} - -static int dt_bus_default_translate(__be32 *addr, u64 offset, int na) -{ - u64 a =3D dt_read_number(addr, na); - - memset(addr, 0, na * 4); - a +=3D offset; - if ( na > 1 ) - addr[na - 2] =3D cpu_to_be32(a >> 32); - addr[na - 1] =3D cpu_to_be32(a & 0xffffffffu); - - return 0; -} -static unsigned int dt_bus_default_get_flags(const __be32 *addr) -{ - return IORESOURCE_MEM; -} - -/* - * PCI bus specific translator - */ - -static bool dt_node_is_pci(const struct dt_device_node *np) -{ - bool is_pci =3D !strcmp(np->name, "pcie") || !strcmp(np->name, "pci"); - - if ( is_pci ) - printk(XENLOG_WARNING "%s: Missing device_type\n", np->full_name); - - return is_pci; -} - -static bool dt_bus_pci_match(const struct dt_device_node *np) -{ - /* - * "pciex" is PCI Express "vci" is for the /chaos bridge on 1st-gen PCI - * powermacs "ht" is hypertransport - * - * If none of the device_type match, and that the node name is - * "pcie" or "pci", accept the device as PCI (with a warning). - */ - return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex") || - !strcmp(np->type, "vci") || !strcmp(np->type, "ht") || - dt_node_is_pci(np); -} - -static void dt_bus_pci_count_cells(const struct dt_device_node *np, - int *addrc, int *sizec) -{ - if (addrc) - *addrc =3D 3; - if (sizec) - *sizec =3D 2; -} - -static unsigned int dt_bus_pci_get_flags(const __be32 *addr) -{ - unsigned int flags =3D 0; - u32 w =3D be32_to_cpup(addr); - - switch((w >> 24) & 0x03) { - case 0x01: - flags |=3D IORESOURCE_IO; - break; - case 0x02: /* 32 bits */ - case 0x03: /* 64 bits */ - flags |=3D IORESOURCE_MEM; - break; - } - if (w & 0x40000000) - flags |=3D IORESOURCE_PREFETCH; - return flags; -} - -static u64 dt_bus_pci_map(__be32 *addr, const __be32 *range, int na, int n= s, - int pna) -{ - u64 cp, s, da; - unsigned int af, rf; - - af =3D dt_bus_pci_get_flags(addr); - rf =3D dt_bus_pci_get_flags(range); - - /* Check address type match */ - if ((af ^ rf) & (IORESOURCE_MEM | IORESOURCE_IO)) - return DT_BAD_ADDR; - - /* Read address values, skipping high cell */ - cp =3D dt_read_number(range + 1, na - 1); - s =3D dt_read_number(range + na + pna, ns); - da =3D dt_read_number(addr + 1, na - 1); - - dt_dprintk("DT: PCI map, cp=3D%llx, s=3D%llx, da=3D%llx\n", - (unsigned long long)cp, (unsigned long long)s, - (unsigned long long)da); - - if (da < cp || da >=3D (cp + s)) - return DT_BAD_ADDR; - return da - cp; -} - -static int dt_bus_pci_translate(__be32 *addr, u64 offset, int na) -{ - return dt_bus_default_translate(addr + 1, offset, na - 1); -} - -/* - * Array of bus specific translators - */ -static const struct dt_bus dt_busses[] =3D -{ - /* PCI */ - { - .name =3D "pci", - .addresses =3D "assigned-addresses", - .match =3D dt_bus_pci_match, - .count_cells =3D dt_bus_pci_count_cells, - .map =3D dt_bus_pci_map, - .translate =3D dt_bus_pci_translate, - .get_flags =3D dt_bus_pci_get_flags, - }, - /* Default */ - { - .name =3D "default", - .addresses =3D "reg", - .match =3D dt_bus_default_match, - .count_cells =3D dt_bus_default_count_cells, - .map =3D dt_bus_default_map, - .translate =3D dt_bus_default_translate, - .get_flags =3D dt_bus_default_get_flags, - }, -}; - -static const struct dt_bus *dt_match_bus(const struct dt_device_node *np) -{ - int i; - - for ( i =3D 0; i < ARRAY_SIZE(dt_busses); i++ ) - if ( !dt_busses[i].match || dt_busses[i].match(np) ) - return &dt_busses[i]; - - return NULL; -} - -static const __be32 *dt_get_address(const struct dt_device_node *dev, - unsigned int index, u64 *size, - unsigned int *flags) -{ - const __be32 *prop; - u32 psize; - const struct dt_device_node *parent; - const struct dt_bus *bus; - int onesize, i, na, ns; - - /* Get parent & match bus type */ - parent =3D dt_get_parent(dev); - if ( parent =3D=3D NULL ) - return NULL; - - bus =3D dt_match_bus(parent); - if ( !bus ) - return NULL; - bus->count_cells(dev, &na, &ns); - - if ( !DT_CHECK_ADDR_COUNT(na) ) - return NULL; - - /* Get "reg" or "assigned-addresses" property */ - prop =3D dt_get_property(dev, bus->addresses, &psize); - if ( prop =3D=3D NULL ) - return NULL; - psize /=3D 4; - - onesize =3D na + ns; - for ( i =3D 0; psize >=3D onesize; psize -=3D onesize, prop +=3D onesi= ze, i++ ) - { - if ( i =3D=3D index ) - { - if ( size ) - *size =3D dt_read_number(prop + na, ns); - if ( flags ) - *flags =3D bus->get_flags(prop); - return prop; - } - } - return NULL; -} - -static int dt_translate_one(const struct dt_device_node *parent, - const struct dt_bus *bus, - const struct dt_bus *pbus, - __be32 *addr, int na, int ns, - int pna, const char *rprop) -{ - const __be32 *ranges; - unsigned int rlen; - int rone; - u64 offset =3D DT_BAD_ADDR; - - ranges =3D dt_get_property(parent, rprop, &rlen); - if ( ranges =3D=3D NULL ) - { - printk(XENLOG_ERR "DT: no ranges; cannot translate\n"); - return 1; - } - if ( rlen =3D=3D 0 ) - { - offset =3D dt_read_number(addr, na); - memset(addr, 0, pna * 4); - dt_dprintk("DT: empty ranges; 1:1 translation\n"); - goto finish; - } - - dt_dprintk("DT: walking ranges...\n"); - - /* Now walk through the ranges */ - rlen /=3D 4; - rone =3D na + pna + ns; - for ( ; rlen >=3D rone; rlen -=3D rone, ranges +=3D rone ) - { - offset =3D bus->map(addr, ranges, na, ns, pna); - if ( offset !=3D DT_BAD_ADDR ) - break; - } - if ( offset =3D=3D DT_BAD_ADDR ) - { - dt_dprintk("DT: not found !\n"); - return 1; - } - memcpy(addr, ranges + na, 4 * pna); - -finish: - dt_dump_addr("DT: parent translation for:", addr, pna); - dt_dprintk("DT: with offset: %llx\n", (unsigned long long)offset); - - /* Translate it into parent bus space */ - return pbus->translate(addr, offset, pna); -} - -/* - * Translate an address from the device-tree into a CPU physical address, - * this walks up the tree and applies the various bus mappings on the - * way. - * - * Note: We consider that crossing any level with #size-cells =3D=3D 0 to = mean - * that translation is impossible (that is we are not dealing with a value - * that can be mapped to a cpu physical address). This is not really speci= fied - * that way, but this is traditionally the way IBM at least do things - */ -static u64 __dt_translate_address(const struct dt_device_node *dev, - const __be32 *in_addr, const char *rprop) -{ - const struct dt_device_node *parent =3D NULL; - const struct dt_bus *bus, *pbus; - __be32 addr[DT_MAX_ADDR_CELLS]; - int na, ns, pna, pns; - u64 result =3D DT_BAD_ADDR; - - dt_dprintk("DT: ** translation for device %s **\n", dev->full_name); - - /* Get parent & match bus type */ - parent =3D dt_get_parent(dev); - if ( parent =3D=3D NULL ) - goto bail; - bus =3D dt_match_bus(parent); - if ( !bus ) - goto bail; - - /* Count address cells & copy address locally */ - bus->count_cells(dev, &na, &ns); - if ( !DT_CHECK_COUNTS(na, ns) ) - { - printk(XENLOG_ERR "dt_parse: Bad cell count for device %s\n", - dev->full_name); - goto bail; - } - memcpy(addr, in_addr, na * 4); - - dt_dprintk("DT: bus is %s (na=3D%d, ns=3D%d) on %s\n", - bus->name, na, ns, parent->full_name); - dt_dump_addr("DT: translating address:", addr, na); - - /* Translate */ - for ( ;; ) - { - /* Switch to parent bus */ - dev =3D parent; - parent =3D dt_get_parent(dev); - - /* If root, we have finished */ - if ( parent =3D=3D NULL ) - { - dt_dprintk("DT: reached root node\n"); - result =3D dt_read_number(addr, na); - break; - } - - /* Get new parent bus and counts */ - pbus =3D dt_match_bus(parent); - if ( pbus =3D=3D NULL ) - { - printk("DT: %s is not a valid bus\n", parent->full_name); - break; - } - pbus->count_cells(dev, &pna, &pns); - if ( !DT_CHECK_COUNTS(pna, pns) ) - { - printk(XENLOG_ERR "dt_parse: Bad cell count for parent %s\n", - dev->full_name); - break; - } - - dt_dprintk("DT: parent bus is %s (na=3D%d, ns=3D%d) on %s\n", - pbus->name, pna, pns, parent->full_name); - - /* Apply bus translation */ - if ( dt_translate_one(dev, bus, pbus, addr, na, ns, pna, rprop) ) - break; - - /* Complete the move up one level */ - na =3D pna; - ns =3D pns; - bus =3D pbus; - - dt_dump_addr("DT: one level translation:", addr, na); - } - -bail: - return result; -} - -/* dt_device_address - Translate device tree address and return it */ -int dt_device_get_address(const struct dt_device_node *dev, unsigned int i= ndex, - u64 *addr, u64 *size) -{ - const __be32 *addrp; - unsigned int flags; - - addrp =3D dt_get_address(dev, index, size, &flags); - if ( addrp =3D=3D NULL ) - return -EINVAL; - - if ( !addr ) - return -EINVAL; - - *addr =3D __dt_translate_address(dev, addrp, "ranges"); - - if ( *addr =3D=3D DT_BAD_ADDR ) - return -EINVAL; - - return 0; -} - -int dt_device_get_paddr(const struct dt_device_node *dev, unsigned int ind= ex, - paddr_t *addr, paddr_t *size) -{ - uint64_t dt_addr, dt_size; - int ret; - - ret =3D dt_device_get_address(dev, index, &dt_addr, &dt_size); - if ( ret ) - return ret; - - if ( !addr ) - return -EINVAL; - - if ( dt_addr !=3D (paddr_t)dt_addr ) - { - printk("Error: Physical address 0x%"PRIx64" for node=3D%s is great= er than max width (%zu bytes) supported\n", - dt_addr, dev->name, sizeof(paddr_t)); - return -ERANGE; - } - - *addr =3D dt_addr; - - if ( size ) - { - if ( dt_size !=3D (paddr_t)dt_size ) - { - printk("Error: Physical size 0x%"PRIx64" for node=3D%s is grea= ter than max width (%zu bytes) supported\n", - dt_size, dev->name, sizeof(paddr_t)); - return -ERANGE; - } - - *size =3D dt_size; - } - - return ret; -} - -int dt_for_each_range(const struct dt_device_node *dev, - int (*cb)(const struct dt_device_node *dev, - uint64_t addr, uint64_t length, - void *data), - void *data) -{ - const struct dt_device_node *parent =3D NULL; - const struct dt_bus *bus, *pbus; - const __be32 *ranges; - __be32 addr[DT_MAX_ADDR_CELLS]; - unsigned int rlen; - int na, ns, pna, pns, rone; - - bus =3D dt_match_bus(dev); - if ( !bus ) - return 0; /* device is not a bus */ - - parent =3D dt_get_parent(dev); - if ( parent =3D=3D NULL ) - return -EINVAL; - - ranges =3D dt_get_property(dev, "ranges", &rlen); - if ( ranges =3D=3D NULL ) - { - printk(XENLOG_ERR "DT: no ranges; cannot enumerate %s\n", - dev->full_name); - return -EINVAL; - } - if ( rlen =3D=3D 0 ) /* Nothing to do */ - return 0; - - bus->count_cells(dev, &na, &ns); - if ( !DT_CHECK_COUNTS(na, ns) ) - { - printk(XENLOG_ERR "dt_parse: Bad cell count for device %s\n", - dev->full_name); - return -EINVAL; - } - - pbus =3D dt_match_bus(parent); - if ( pbus =3D=3D NULL ) - { - printk("DT: %s is not a valid bus\n", parent->full_name); - return -EINVAL; - } - - pbus->count_cells(dev, &pna, &pns); - if ( !DT_CHECK_COUNTS(pna, pns) ) - { - printk(XENLOG_ERR "dt_parse: Bad cell count for parent %s\n", - dev->full_name); - return -EINVAL; - } - - /* Now walk through the ranges */ - rlen /=3D 4; - rone =3D na + pna + ns; - - dt_dprintk("%s: dev=3D%s, bus=3D%s, parent=3D%s, rlen=3D%d, rone=3D%d\= n", - __func__, - dt_node_name(dev), bus->name, - dt_node_name(parent), rlen, rone); - - for ( ; rlen >=3D rone; rlen -=3D rone, ranges +=3D rone ) - { - uint64_t a, s; - int ret; - - memcpy(addr, ranges + na, 4 * pna); - - a =3D __dt_translate_address(dev, addr, "ranges"); - s =3D dt_read_number(ranges + na + pna, ns); - - ret =3D cb(dev, a, s, data); - if ( ret ) - { - dt_dprintk(" -> callback failed=3D%d\n", ret); - return ret; - } - - } - - return 0; -} - -/** - * dt_find_node_by_phandle - Find a node given a phandle - * @handle: phandle of the node to find - * - * Returns a node pointer. - */ -struct dt_device_node *dt_find_node_by_phandle(dt_phandle handle) -{ - struct dt_device_node *np; - - dt_for_each_device_node(dt_host, np) - if ( np->phandle =3D=3D handle ) - break; - - return np; -} - -/** - * dt_irq_find_parent - Given a device node, find its interrupt parent node - * @child: pointer to device node - * - * Returns a pointer to the interrupt parent node, or NULL if the interrupt - * parent could not be determined. - */ -static const struct dt_device_node * -dt_irq_find_parent(const struct dt_device_node *child) -{ - const struct dt_device_node *p; - const __be32 *parp; - - do - { - parp =3D dt_get_property(child, "interrupt-parent", NULL); - if ( parp =3D=3D NULL ) - p =3D dt_get_parent(child); - else - p =3D dt_find_node_by_phandle(be32_to_cpup(parp)); - child =3D p; - } while ( p && dt_get_property(p, "#interrupt-cells", NULL) =3D=3D NUL= L ); - - return p; -} - -unsigned int dt_number_of_irq(const struct dt_device_node *device) -{ - const struct dt_device_node *p; - const __be32 *intspec, *tmp; - u32 intsize, intlen; - int intnum; - - dt_dprintk("dt_irq_number: dev=3D%s\n", device->full_name); - - /* Try the new-style interrupts-extended first */ - intnum =3D dt_count_phandle_with_args(device, "interrupts-extended", - "#interrupt-cells"); - if ( intnum >=3D 0 ) - { - dt_dprintk(" using 'interrupts-extended' property\n"); - dt_dprintk(" intnum=3D%d\n", intnum); - return intnum; - } - - /* Get the interrupts property */ - intspec =3D dt_get_property(device, "interrupts", &intlen); - if ( intspec =3D=3D NULL ) - return 0; - intlen /=3D sizeof(*intspec); - - dt_dprintk(" using 'interrupts' property\n"); - dt_dprintk(" intspec=3D%d intlen=3D%d\n", be32_to_cpup(intspec), intle= n); - - /* Look for the interrupt parent. */ - p =3D dt_irq_find_parent(device); - if ( p =3D=3D NULL ) - return 0; - - /* Get size of interrupt specifier */ - tmp =3D dt_get_property(p, "#interrupt-cells", NULL); - if ( tmp =3D=3D NULL ) - return 0; - intsize =3D be32_to_cpu(*tmp); - - dt_dprintk(" intsize=3D%d intlen=3D%d\n", intsize, intlen); - - return (intlen / intsize); -} - -unsigned int dt_number_of_address(const struct dt_device_node *dev) -{ - const __be32 *prop; - u32 psize; - const struct dt_device_node *parent; - const struct dt_bus *bus; - int onesize, na, ns; - - /* Get parent & match bus type */ - parent =3D dt_get_parent(dev); - if ( parent =3D=3D NULL ) - return 0; - - bus =3D dt_match_bus(parent); - if ( !bus ) - return 0; - bus->count_cells(dev, &na, &ns); - - if ( !DT_CHECK_COUNTS(na, ns) ) - return 0; - - /* Get "reg" or "assigned-addresses" property */ - prop =3D dt_get_property(dev, bus->addresses, &psize); - if ( prop =3D=3D NULL ) - return 0; - - psize /=3D 4; - onesize =3D na + ns; - - return (psize / onesize); -} - -int dt_for_each_irq_map(const struct dt_device_node *dev, - int (*cb)(const struct dt_device_node *dev, - const struct dt_irq *dt_irq, - void *data), - void *data) -{ - const struct dt_device_node *ipar, *tnode, *old =3D NULL; - const __be32 *tmp, *imap; - u32 intsize =3D 1, addrsize, pintsize =3D 0, paddrsize =3D 0; - u32 imaplen; - int i, ret; - - struct dt_raw_irq dt_raw_irq; - struct dt_irq dt_irq; - - dt_dprintk("%s: par=3D%s cb=3D%p data=3D%p\n", __func__, - dev->full_name, cb, data); - - ipar =3D dev; - - /* First get the #interrupt-cells property of the current cursor - * that tells us how to interpret the passed-in intspec. If there - * is none, we are nice and just walk up the tree - */ - do { - tmp =3D dt_get_property(ipar, "#interrupt-cells", NULL); - if ( tmp !=3D NULL ) - { - intsize =3D be32_to_cpu(*tmp); - break; - } - tnode =3D ipar; - ipar =3D dt_irq_find_parent(ipar); - } while ( ipar ); - if ( ipar =3D=3D NULL ) - { - dt_dprintk(" -> no parent found !\n"); - goto fail; - } - - dt_dprintk("%s: ipar=3D%s, size=3D%d\n", __func__, ipar->full_name, in= tsize); - - if ( intsize > DT_MAX_IRQ_SPEC ) - { - dt_dprintk(" -> too many irq specifier cells\n"); - goto fail; - } - - /* Look for this #address-cells. We have to implement the old linux - * trick of looking for the parent here as some device-trees rely on it - */ - old =3D ipar; - do { - tmp =3D dt_get_property(old, "#address-cells", NULL); - tnode =3D dt_get_parent(old); - old =3D tnode; - } while ( old && tmp =3D=3D NULL ); - - old =3D NULL; - addrsize =3D (tmp =3D=3D NULL) ? 2 : be32_to_cpu(*tmp); - - dt_dprintk(" -> addrsize=3D%d\n", addrsize); - - /* Now look for an interrupt-map */ - imap =3D dt_get_property(dev, "interrupt-map", &imaplen); - /* No interrupt-map found. Ignore */ - if ( imap =3D=3D NULL ) - { - dt_dprintk(" -> no map, ignoring\n"); - return 0; - } - imaplen /=3D sizeof(u32); - - /* Parse interrupt-map */ - while ( imaplen > (addrsize + intsize + 1) ) - { - /* skip child unit address and child interrupt specifier */ - imap +=3D addrsize + intsize; - imaplen -=3D addrsize + intsize; - - /* Get the interrupt parent */ - ipar =3D dt_find_node_by_phandle(be32_to_cpup(imap)); - imap++; - --imaplen; - - /* Check if not found */ - if ( ipar =3D=3D NULL ) - { - dt_dprintk(" -> imap parent not found !\n"); - goto fail; - } - - dt_dprintk(" -> ipar %s\n", dt_node_name(ipar)); - - /* Get #interrupt-cells and #address-cells of new - * parent - */ - tmp =3D dt_get_property(ipar, "#interrupt-cells", NULL); - if ( tmp =3D=3D NULL ) - { - dt_dprintk(" -> parent lacks #interrupt-cells!\n"); - goto fail; - } - pintsize =3D be32_to_cpu(*tmp); - tmp =3D dt_get_property(ipar, "#address-cells", NULL); - paddrsize =3D (tmp =3D=3D NULL) ? 0 : be32_to_cpu(*tmp); - - dt_dprintk(" -> pintsize=3D%d, paddrsize=3D%d\n", - pintsize, paddrsize); - - if ( pintsize > DT_MAX_IRQ_SPEC ) - { - dt_dprintk(" -> too many irq specifier cells in parent\n"); - goto fail; - } - - /* Check for malformed properties */ - if ( imaplen < (paddrsize + pintsize) ) - goto fail; - - imap +=3D paddrsize; - imaplen -=3D paddrsize; - - dt_raw_irq.controller =3D ipar; - dt_raw_irq.size =3D pintsize; - for ( i =3D 0; i < pintsize; i++ ) - dt_raw_irq.specifier[i] =3D dt_read_number(imap + i, 1); - - if ( dt_raw_irq.controller !=3D dt_interrupt_controller ) - { - /* - * We don't map IRQs connected to secondary IRQ controllers as - * these IRQs have no meaning to us until they connect to the - * primary controller. - * - * Secondary IRQ controllers will at some point connect to - * the primary controller (possibly via other IRQ controllers). - * We map the IRQs at that last connection point. - */ - imap +=3D pintsize; - imaplen -=3D pintsize; - dt_dprintk(" -> Skipped IRQ for secondary IRQ controller\n"); - continue; - } - - ret =3D dt_irq_translate(&dt_raw_irq, &dt_irq); - if ( ret ) - { - dt_dprintk(" -> failed to translate IRQ: %d\n", ret); - return ret; - } - - ret =3D cb(dev, &dt_irq, data); - if ( ret ) - { - dt_dprintk(" -> callback failed=3D%d\n", ret); - return ret; - } - - imap +=3D pintsize; - imaplen -=3D pintsize; - - dt_dprintk(" -> imaplen=3D%d\n", imaplen); - } - - return 0; - -fail: - return -EINVAL; -} - -/** - * dt_irq_map_raw - Low level interrupt tree parsing - * @parent: the device interrupt parent - * @intspec: interrupt specifier ("interrupts" property of the device) - * @ointsize: size of the passed in interrupt specifier - * @addr: address specifier (start of "reg" property of the device) - * @oirq: structure dt_raw_irq filled by this function - * - * Returns 0 on success and a negative number on error - * - * This function is a low-level interrupt tree walking function. It - * can be used to do a partial walk with synthesized reg and interrupts - * properties, for example when resolving PCI interrupts when no device - * node exist for the parent. - */ -static int dt_irq_map_raw(const struct dt_device_node *parent, - const __be32 *intspec, u32 ointsize, - const __be32 *addr, - struct dt_raw_irq *oirq) -{ - const struct dt_device_node *ipar, *tnode, *old =3D NULL, *newpar =3D = NULL; - const __be32 *tmp, *imap, *imask; - u32 intsize =3D 1, addrsize, newintsize =3D 0, newaddrsize =3D 0; - u32 imaplen; - int match, i; - - dt_dprintk("dt_irq_map_raw: par=3D%s,intspec=3D[0x%08x 0x%08x...],oint= size=3D%d\n", - parent->full_name, be32_to_cpup(intspec), - be32_to_cpup(intspec + 1), ointsize); - - ipar =3D parent; - - /* First get the #interrupt-cells property of the current cursor - * that tells us how to interpret the passed-in intspec. If there - * is none, we are nice and just walk up the tree - */ - do { - tmp =3D dt_get_property(ipar, "#interrupt-cells", NULL); - if ( tmp !=3D NULL ) - { - intsize =3D be32_to_cpu(*tmp); - break; - } - tnode =3D ipar; - ipar =3D dt_irq_find_parent(ipar); - } while ( ipar ); - if ( ipar =3D=3D NULL ) - { - dt_dprintk(" -> no parent found !\n"); - goto fail; - } - - dt_dprintk("dt_irq_map_raw: ipar=3D%s, size=3D%d\n", ipar->full_name, = intsize); - - if ( ointsize !=3D intsize ) - return -EINVAL; - - /* Look for this #address-cells. We have to implement the old linux - * trick of looking for the parent here as some device-trees rely on it - */ - old =3D ipar; - do { - tmp =3D dt_get_property(old, "#address-cells", NULL); - tnode =3D dt_get_parent(old); - old =3D tnode; - } while ( old && tmp =3D=3D NULL ); - - old =3D NULL; - addrsize =3D (tmp =3D=3D NULL) ? 2 : be32_to_cpu(*tmp); - - dt_dprintk(" -> addrsize=3D%d\n", addrsize); - - /* Now start the actual "proper" walk of the interrupt tree */ - while ( ipar !=3D NULL ) - { - /* Now check if cursor is an interrupt-controller and if it is - * then we are done - */ - if ( dt_get_property(ipar, "interrupt-controller", NULL) !=3D NULL= ) - { - dt_dprintk(" -> got it !\n"); - if ( intsize > DT_MAX_IRQ_SPEC ) - { - dt_dprintk(" -> intsize(%u) greater than DT_MAX_IRQ_SPEC(%= u)\n", - intsize, DT_MAX_IRQ_SPEC); - goto fail; - } - for ( i =3D 0; i < intsize; i++ ) - oirq->specifier[i] =3D dt_read_number(intspec + i, 1); - oirq->size =3D intsize; - oirq->controller =3D ipar; - return 0; - } - - /* Now look for an interrupt-map */ - imap =3D dt_get_property(ipar, "interrupt-map", &imaplen); - /* No interrupt map, check for an interrupt parent */ - if ( imap =3D=3D NULL ) - { - dt_dprintk(" -> no map, getting parent\n"); - newpar =3D dt_irq_find_parent(ipar); - goto skiplevel; - } - imaplen /=3D sizeof(u32); - - /* Look for a mask */ - imask =3D dt_get_property(ipar, "interrupt-map-mask", NULL); - - /* If we were passed no "reg" property and we attempt to parse - * an interrupt-map, then #address-cells must be 0. - * Fail if it's not. - */ - if ( addr =3D=3D NULL && addrsize !=3D 0 ) - { - dt_dprintk(" -> no reg passed in when needed !\n"); - goto fail; - } - - /* Parse interrupt-map */ - match =3D 0; - while ( imaplen > (addrsize + intsize + 1) && !match ) - { - /* Compare specifiers */ - match =3D 1; - for ( i =3D 0; i < addrsize && match; ++i ) - { - __be32 mask =3D imask ? imask[i] : cpu_to_be32(0xffffffffu= ); - match =3D ((addr[i] ^ imap[i]) & mask) =3D=3D 0; - } - for ( ; i < (addrsize + intsize) && match; ++i ) - { - __be32 mask =3D imask ? imask[i] : cpu_to_be32(0xffffffffu= ); - match =3D ((intspec[i-addrsize] ^ imap[i]) & mask) =3D=3D = 0; - } - imap +=3D addrsize + intsize; - imaplen -=3D addrsize + intsize; - - dt_dprintk(" -> match=3D%d (imaplen=3D%d)\n", match, imaplen); - - /* Get the interrupt parent */ - newpar =3D dt_find_node_by_phandle(be32_to_cpup(imap)); - imap++; - --imaplen; - - /* Check if not found */ - if ( newpar =3D=3D NULL ) - { - dt_dprintk(" -> imap parent not found !\n"); - goto fail; - } - - /* Get #interrupt-cells and #address-cells of new - * parent - */ - tmp =3D dt_get_property(newpar, "#interrupt-cells", NULL); - if ( tmp =3D=3D NULL ) - { - dt_dprintk(" -> parent lacks #interrupt-cells!\n"); - goto fail; - } - newintsize =3D be32_to_cpu(*tmp); - tmp =3D dt_get_property(newpar, "#address-cells", NULL); - newaddrsize =3D (tmp =3D=3D NULL) ? 0 : be32_to_cpu(*tmp); - - dt_dprintk(" -> newintsize=3D%d, newaddrsize=3D%d\n", - newintsize, newaddrsize); - - /* Check for malformed properties */ - if ( imaplen < (newaddrsize + newintsize) ) - goto fail; - - imap +=3D newaddrsize + newintsize; - imaplen -=3D newaddrsize + newintsize; - - dt_dprintk(" -> imaplen=3D%d\n", imaplen); - } - if ( !match ) - goto fail; - - old =3D newpar; - addrsize =3D newaddrsize; - intsize =3D newintsize; - intspec =3D imap - intsize; - addr =3D intspec - addrsize; - - skiplevel: - /* Iterate again with new parent */ - dt_dprintk(" -> new parent: %s\n", dt_node_full_name(newpar)); - ipar =3D newpar; - newpar =3D NULL; - } -fail: - return -EINVAL; -} - -int dt_device_get_raw_irq(const struct dt_device_node *device, - unsigned int index, - struct dt_raw_irq *out_irq) -{ - const struct dt_device_node *p; - const __be32 *intspec, *tmp, *addr; - u32 intsize, intlen; - int res =3D -EINVAL; - struct dt_phandle_args args; - int i; - - dt_dprintk("dt_device_get_raw_irq: dev=3D%s, index=3D%u\n", - device->full_name, index); - - /* Get the reg property (if any) */ - addr =3D dt_get_property(device, "reg", NULL); - - /* Try the new-style interrupts-extended first */ - res =3D dt_parse_phandle_with_args(device, "interrupts-extended", - "#interrupt-cells", index, &args); - if ( !res ) - { - dt_dprintk(" using 'interrupts-extended' property\n"); - dt_dprintk(" intspec=3D%d intsize=3D%d\n", args.args[0], args.args= _count); - - for ( i =3D 0; i < args.args_count; i++ ) - args.args[i] =3D cpu_to_be32(args.args[i]); - - return dt_irq_map_raw(args.np, args.args, args.args_count, - addr, out_irq); - } - - /* Get the interrupts property */ - intspec =3D dt_get_property(device, "interrupts", &intlen); - if ( intspec =3D=3D NULL ) - return -EINVAL; - intlen /=3D sizeof(*intspec); - - dt_dprintk(" using 'interrupts' property\n"); - dt_dprintk(" intspec=3D%d intlen=3D%d\n", be32_to_cpup(intspec), intle= n); - - /* Look for the interrupt parent. */ - p =3D dt_irq_find_parent(device); - if ( p =3D=3D NULL ) - return -EINVAL; - - /* Get size of interrupt specifier */ - tmp =3D dt_get_property(p, "#interrupt-cells", NULL); - if ( tmp =3D=3D NULL ) - goto out; - intsize =3D be32_to_cpu(*tmp); - - dt_dprintk(" intsize=3D%d intlen=3D%d\n", intsize, intlen); - - /* Check index */ - if ( (index + 1) * intsize > intlen ) - goto out; - - /* Get new specifier and map it */ - res =3D dt_irq_map_raw(p, intspec + index * intsize, intsize, - addr, out_irq); - if ( res ) - goto out; -out: - return res; -} - -int dt_irq_translate(const struct dt_raw_irq *raw, - struct dt_irq *out_irq) -{ - ASSERT(dt_irq_xlate !=3D NULL); - ASSERT(dt_interrupt_controller !=3D NULL); - - /* - * TODO: Retrieve the right irq_xlate. This is only works for the prim= ary - * interrupt controller. - */ - if ( raw->controller !=3D dt_interrupt_controller ) - return -EINVAL; - - return dt_irq_xlate(raw->specifier, raw->size, - &out_irq->irq, &out_irq->type); -} - -int dt_device_get_irq(const struct dt_device_node *device, unsigned int in= dex, - struct dt_irq *out_irq) -{ - struct dt_raw_irq raw; - int res; - - res =3D dt_device_get_raw_irq(device, index, &raw); - - if ( res ) - return res; - - return dt_irq_translate(&raw, out_irq); -} - -bool dt_device_is_available(const struct dt_device_node *device) -{ - const char *status; - u32 statlen; - - status =3D dt_get_property(device, "status", &statlen); - if ( status =3D=3D NULL ) - return 1; - - if ( statlen > 0 ) - { - if ( !strcmp(status, "okay") || !strcmp(status, "ok") ) - return 1; - } - - return 0; -} - -bool dt_device_for_passthrough(const struct dt_device_node *device) -{ - return (dt_find_property(device, "xen,passthrough", NULL) !=3D NULL); - -} - -static int __dt_parse_phandle_with_args(const struct dt_device_node *np, - const char *list_name, - const char *cells_name, - int cell_count, int index, - struct dt_phandle_args *out_args) -{ - const __be32 *list, *list_end; - int rc =3D 0, cur_index =3D 0; - u32 size, count =3D 0; - struct dt_device_node *node =3D NULL; - dt_phandle phandle; - - /* Retrieve the phandle list property */ - list =3D dt_get_property(np, list_name, &size); - if ( !list ) - return -ENOENT; - list_end =3D list + size / sizeof(*list); - - /* Loop over the phandles until all the requested entry is found */ - while ( list < list_end ) - { - rc =3D -EINVAL; - count =3D 0; - - /* - * If phandle is 0, then it is an empty entry with no - * arguments. Skip forward to the next entry. - * */ - phandle =3D be32_to_cpup(list++); - if ( phandle ) - { - /* - * Find the provider node and parse the #*-cells - * property to determine the argument length. - * - * This is not needed if the cell count is hard-coded - * (i.e. cells_name not set, but cell_count is set), - * except when we're going to return the found node - * below. - */ - if ( cells_name || cur_index =3D=3D index ) - { - node =3D dt_find_node_by_phandle(phandle); - if ( !node ) - { - printk(XENLOG_ERR "%s: could not find phandle\n", - np->full_name); - goto err; - } - } - - if ( cells_name ) - { - if ( !dt_property_read_u32(node, cells_name, &count) ) - { - printk("%s: could not get %s for %s\n", - np->full_name, cells_name, node->full_name); - goto err; - } - } - else - count =3D cell_count; - - /* - * Make sure that the arguments actually fit in the - * remaining property data length - */ - if ( list + count > list_end ) - { - printk(XENLOG_ERR "%s: arguments longer than property\n", - np->full_name); - goto err; - } - } - - /* - * All of the error cases above bail out of the loop, so at - * this point, the parsing is successful. If the requested - * index matches, then fill the out_args structure and return, - * or return -ENOENT for an empty entry. - */ - rc =3D -ENOENT; - if ( cur_index =3D=3D index ) - { - if (!phandle) - goto err; - - if ( out_args ) - { - int i; - - WARN_ON(count > MAX_PHANDLE_ARGS); - if (count > MAX_PHANDLE_ARGS) - count =3D MAX_PHANDLE_ARGS; - out_args->np =3D node; - out_args->args_count =3D count; - for ( i =3D 0; i < count; i++ ) - out_args->args[i] =3D be32_to_cpup(list++); - } - - /* Found it! return success */ - return 0; - } - - node =3D NULL; - list +=3D count; - cur_index++; - } - - /* - * Returning result will be one of: - * -ENOENT : index is for empty phandle - * -EINVAL : parsing error on data - * [1..n] : Number of phandle (count mode; when index =3D -1) - */ - rc =3D index < 0 ? cur_index : -ENOENT; -err: - return rc; -} - -struct dt_device_node *dt_parse_phandle(const struct dt_device_node *np, - const char *phandle_name, int inde= x) -{ - struct dt_phandle_args args; - - if (index < 0) - return NULL; - - if (__dt_parse_phandle_with_args(np, phandle_name, NULL, 0, - index, &args)) - return NULL; - - return args.np; -} - - -int dt_parse_phandle_with_args(const struct dt_device_node *np, - const char *list_name, - const char *cells_name, int index, - struct dt_phandle_args *out_args) -{ - if ( index < 0 ) - return -EINVAL; - return __dt_parse_phandle_with_args(np, list_name, cells_name, 0, - index, out_args); -} - -int dt_count_phandle_with_args(const struct dt_device_node *np, - const char *list_name, - const char *cells_name) -{ - return __dt_parse_phandle_with_args(np, list_name, cells_name, 0, -1, = NULL); -} - -/** - * unflatten_dt_node - Alloc and populate a device_node from the flat tree - * @fdt: The parent device tree blob - * @mem: Memory chunk to use for allocating device nodes and properties - * @p: pointer to node in flat tree - * @dad: Parent struct device_node - * @allnextpp: pointer to ->allnext from last allocated device_node - * @fpsize: Size of the node path up at the current depth. - */ -static unsigned long unflatten_dt_node(const void *fdt, - unsigned long mem, - unsigned long *p, - struct dt_device_node *dad, - struct dt_device_node ***allnextpp, - unsigned long fpsize) -{ - struct dt_device_node *np; - struct dt_property *pp, **prev_pp =3D NULL; - char *pathp; - u32 tag; - unsigned int l, allocl; - int has_name =3D 0; - int new_format =3D 0; - - tag =3D be32_to_cpup((__be32 *)(*p)); - if ( tag !=3D FDT_BEGIN_NODE ) - { - printk(XENLOG_WARNING "Weird tag at start of node: %x\n", tag); - return mem; - } - *p +=3D 4; - pathp =3D (char *)*p; - l =3D allocl =3D strlen(pathp) + 1; - *p =3D ROUNDUP(*p + l, 4); - - /* version 0x10 has a more compact unit name here instead of the full - * path. we accumulate the full path size using "fpsize", we'll rebuild - * it later. We detect this because the first character of the name is - * not '/'. - */ - if ( (*pathp) !=3D '/' ) - { - new_format =3D 1; - if ( fpsize =3D=3D 0 ) - { - /* root node: special case. fpsize accounts for path - * plus terminating zero. root node only has '/', so - * fpsize should be 2, but we want to avoid the first - * level nodes to have two '/' so we use fpsize 1 here - */ - fpsize =3D 1; - allocl =3D 2; - } - else - { - /* account for '/' and path size minus terminal 0 - * already in 'l' - */ - fpsize +=3D l; - allocl =3D fpsize; - } - } - - np =3D unflatten_dt_alloc(&mem, sizeof(struct dt_device_node) + allocl, - __alignof__(struct dt_device_node)); - if ( allnextpp ) - { - memset(np, 0, sizeof(*np)); - np->full_name =3D ((char *)np) + sizeof(struct dt_device_node); - /* By default dom0 owns the device */ - np->used_by =3D 0; - /* By default the device is not protected */ - np->is_protected =3D false; - INIT_LIST_HEAD(&np->domain_list); - - if ( new_format ) - { - char *fn =3D np->full_name; - /* rebuild full path for new format */ - if ( dad && dad->parent ) - { - strlcpy(fn, dad->full_name, allocl); -#ifdef DEBUG_DT - if ( (strlen(fn) + l + 1) !=3D allocl ) - { - dt_dprintk("%s: p: %d, l: %d, a: %d\n", - pathp, (int)strlen(fn), - l, allocl); - } -#endif - fn +=3D strlen(fn); - } - *(fn++) =3D '/'; - memcpy(fn, pathp, l); - } - else - memcpy(np->full_name, pathp, l); - prev_pp =3D &np->properties; - **allnextpp =3D np; - *allnextpp =3D &np->allnext; - if ( dad !=3D NULL ) - { - np->parent =3D dad; - /* we temporarily use the next field as `last_child'*/ - if ( dad->next =3D=3D NULL ) - dad->child =3D np; - else - dad->next->sibling =3D np; - dad->next =3D np; - } - } - /* process properties */ - while ( 1 ) - { - u32 sz, noff; - const char *pname; - - tag =3D be32_to_cpup((__be32 *)(*p)); - if ( tag =3D=3D FDT_NOP ) - { - *p +=3D 4; - continue; - } - if ( tag !=3D FDT_PROP ) - break; - *p +=3D 4; - sz =3D be32_to_cpup((__be32 *)(*p)); - noff =3D be32_to_cpup((__be32 *)((*p) + 4)); - *p +=3D 8; - if ( fdt_version(fdt) < 0x10 ) - *p =3D ROUNDUP(*p, sz >=3D 8 ? 8 : 4); - - pname =3D fdt_string(fdt, noff); - if ( pname =3D=3D NULL ) - { - dt_dprintk("Can't find property name in list!\n"); - break; - } - if ( strcmp(pname, "name") =3D=3D 0 ) - has_name =3D 1; - l =3D strlen(pname) + 1; - pp =3D unflatten_dt_alloc(&mem, sizeof(struct dt_property), - __alignof__(struct dt_property)); - if ( allnextpp ) - { - /* We accept flattened tree phandles either in - * ePAPR-style "phandle" properties, or the - * legacy "linux,phandle" properties. If both - * appear and have different values, things - * will get weird. Don't do that. */ - if ( (strcmp(pname, "phandle") =3D=3D 0) || - (strcmp(pname, "linux,phandle") =3D=3D 0) ) - { - if ( np->phandle =3D=3D 0 ) - np->phandle =3D be32_to_cpup((__be32*)*p); - } - /* And we process the "ibm,phandle" property - * used in pSeries dynamic device tree - * stuff */ - if ( strcmp(pname, "ibm,phandle") =3D=3D 0 ) - np->phandle =3D be32_to_cpup((__be32 *)*p); - pp->name =3D pname; - pp->length =3D sz; - pp->value =3D (void *)*p; - *prev_pp =3D pp; - prev_pp =3D &pp->next; - } - *p =3D ROUNDUP((*p) + sz, 4); - } - /* with version 0x10 we may not have the name property, recreate - * it here from the unit name if absent - */ - if ( !has_name ) - { - char *p1 =3D pathp, *ps =3D pathp, *pa =3D NULL; - int sz; - - while ( *p1 ) - { - if ( (*p1) =3D=3D '@' ) - pa =3D p1; - if ( (*p1) =3D=3D '/' ) - ps =3D p1 + 1; - p1++; - } - if ( pa < ps ) - pa =3D p1; - sz =3D (pa - ps) + 1; - pp =3D unflatten_dt_alloc(&mem, sizeof(struct dt_property) + sz, - __alignof__(struct dt_property)); - if ( allnextpp ) - { - pp->name =3D "name"; - pp->length =3D sz; - pp->value =3D pp + 1; - /* - * The device tree creation code assume that the property - * "name" is not a fake. - * To avoid a big divergence with Linux code, only remove - * property link. In this case we will lose a bit of memory - */ -#if 0 - *prev_pp =3D pp; - prev_pp =3D &pp->next; -#endif - np->name =3D pp->value; - memcpy(pp->value, ps, sz - 1); - ((char *)pp->value)[sz - 1] =3D 0; - dt_dprintk("fixed up name for %s -> %s\n", pathp, - (char *)pp->value); - /* Generic device initialization */ - np->dev.type =3D DEV_DT; - np->dev.of_node =3D np; - } - } - if ( allnextpp ) - { - *prev_pp =3D NULL; - np->name =3D (np->name) ? : dt_get_property(np, "name", NULL); - np->type =3D dt_get_property(np, "device_type", NULL); - - if ( !np->name ) - np->name =3D ""; - if ( !np->type ) - np->type =3D ""; - } - while ( tag =3D=3D FDT_BEGIN_NODE || tag =3D=3D FDT_NOP ) - { - if ( tag =3D=3D FDT_NOP ) - *p +=3D 4; - else - mem =3D unflatten_dt_node(fdt, mem, p, np, allnextpp, fpsize); - tag =3D be32_to_cpup((__be32 *)(*p)); - } - if ( tag !=3D FDT_END_NODE ) - { - printk(XENLOG_WARNING "Weird tag at end of node: %x\n", tag); - return mem; - } - - *p +=3D 4; - return mem; -} - -int unflatten_device_tree(const void *fdt, struct dt_device_node **mynodes) -{ - unsigned long start, mem, size; - struct dt_device_node **allnextp =3D mynodes; - - dt_dprintk(" -> unflatten_device_tree()\n"); - - dt_dprintk("Unflattening device tree:\n"); - dt_dprintk("magic: %#08x\n", fdt_magic(fdt)); - dt_dprintk("size: %#08x\n", fdt_totalsize(fdt)); - dt_dprintk("version: %#08x\n", fdt_version(fdt)); - - /* First pass, scan for size */ - start =3D ((unsigned long)fdt) + fdt_off_dt_struct(fdt); - size =3D unflatten_dt_node(fdt, 0, &start, NULL, NULL, 0); - if ( !size ) - return -EINVAL; - - size =3D (size | 3) + 1; - - dt_dprintk(" size is %#lx allocating...\n", size); - - /* Allocate memory for the expanded device tree */ - mem =3D (unsigned long)_xmalloc (size + 4, __alignof__(struct dt_devic= e_node)); - if ( !mem ) - return -ENOMEM; - - ((__be32 *)mem)[size / 4] =3D cpu_to_be32(0xdeadbeefU); - - dt_dprintk(" unflattening %lx...\n", mem); - - /* Second pass, do actual unflattening */ - start =3D ((unsigned long)fdt) + fdt_off_dt_struct(fdt); - unflatten_dt_node(fdt, mem, &start, NULL, &allnextp, 0); - if ( be32_to_cpup((__be32 *)start) !=3D FDT_END ) - { - printk(XENLOG_ERR "Weird tag at end of tree: %08x\n", - *((u32 *)start)); - xfree((void *)mem); - return -EINVAL; - } - - if ( be32_to_cpu(((__be32 *)mem)[size / 4]) !=3D 0xdeadbeefU ) - { - printk(XENLOG_ERR "End of tree marker overwritten: %08x\n", - be32_to_cpu(((__be32 *)mem)[size / 4])); - xfree((void *)mem); - return -EINVAL; - } - - *allnextp =3D NULL; - - dt_dprintk(" <- unflatten_device_tree()\n"); - - return 0; -} - -static void dt_alias_add(struct dt_alias_prop *ap, - struct dt_device_node *np, - int id, const char *stem, int stem_len) -{ - ap->np =3D np; - ap->id =3D id; - strlcpy(ap->stem, stem, stem_len + 1); - list_add_tail(&ap->link, &aliases_lookup); - dt_dprintk("adding DT alias:%s: stem=3D%s id=3D%d node=3D%s\n", - ap->alias, ap->stem, ap->id, dt_node_full_name(np)); -} - -/** - * dt_alias_scan - Scan all properties of 'aliases' node - * - * The function scans all the properties of 'aliases' node and populate - * the the global lookup table with the properties. It returns the - * number of alias_prop found, or error code in error case. - */ -static void __init dt_alias_scan(void) -{ - const struct dt_property *pp; - const struct dt_device_node *aliases; - - aliases =3D dt_find_node_by_path("/aliases"); - if ( !aliases ) - return; - - dt_for_each_property_node( aliases, pp ) - { - const char *start =3D pp->name; - const char *end =3D start + strlen(start); - struct dt_device_node *np; - struct dt_alias_prop *ap; - int id, len; - - /* Skip those we do not want to proceed */ - if ( !strcmp(pp->name, "name") || - !strcmp(pp->name, "phandle") || - !strcmp(pp->name, "linux,phandle") ) - continue; - - np =3D dt_find_node_by_path(pp->value); - if ( !np ) - continue; - - /* walk the alias backwards to extract the id and work out - * the 'stem' string */ - while ( isdigit(*(end-1)) && end > start ) - end--; - len =3D end - start; - - id =3D simple_strtoll(end, NULL, 10); - - /* Allocate an alias_prop with enough space for the stem */ - ap =3D _xmalloc(sizeof(*ap) + len + 1, 4); - if ( !ap ) - continue; - ap->alias =3D start; - dt_alias_add(ap, np, id, start, len); - } -} - -struct dt_device_node * __init -dt_find_interrupt_controller(const struct dt_device_match *matches) -{ - struct dt_device_node *np =3D NULL; - - while ( (np =3D dt_find_matching_node(np, matches)) ) - { - if ( !dt_find_property(np, "interrupt-controller", NULL) ) - continue; - - if ( dt_get_parent(np) ) - break; - } - - return np; -} - -void __init dt_unflatten_host_device_tree(void) -{ - int error =3D unflatten_device_tree(device_tree_flattened, &dt_host); - - if ( error ) - panic("unflatten_device_tree failed with error %d\n", error); - - dt_alias_scan(); -} - -int dt_get_pci_domain_nr(struct dt_device_node *node) -{ - u32 domain; - int error; - - error =3D dt_property_read_u32(node, "linux,pci-domain", &domain); - if ( !error ) - return -EINVAL; - - return (u16)domain; -} - -/* - * Local variables: - * mode: C - * c-file-style: "BSD" - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - */ diff --git a/xen/include/xen/bootfdt.h b/xen/include/xen/bootfdt.h new file mode 100644 index 0000000000..c39428d5f5 --- /dev/null +++ b/xen/include/xen/bootfdt.h @@ -0,0 +1,195 @@ +#ifndef XEN_BOOTFDT_H +#define XEN_BOOTFDT_H + +#include +#include +#include + +#define MIN_FDT_ALIGN 8 + +#define NR_MEM_BANKS 256 +#define NR_SHMEM_BANKS 32 + +#define MAX_MODULES 32 /* Current maximum useful modules */ + +typedef enum { + BOOTMOD_XEN, + BOOTMOD_FDT, + BOOTMOD_KERNEL, + BOOTMOD_RAMDISK, + BOOTMOD_XSM, + BOOTMOD_GUEST_DTB, + BOOTMOD_UNKNOWN +} bootmodule_kind; + +enum membank_type { + /* + * The MEMBANK_DEFAULT type refers to either reserved memory for the + * device/firmware (when the bank is in 'reserved_mem') or any RAM (wh= en + * the bank is in 'mem'). + */ + MEMBANK_DEFAULT, + /* + * The MEMBANK_STATIC_DOMAIN type is used to indicate whether the memo= ry + * bank is bound to a static Xen domain. It is only valid when the bank + * is in reserved_mem. + */ + MEMBANK_STATIC_DOMAIN, + /* + * The MEMBANK_STATIC_HEAP type is used to indicate whether the memory + * bank is reserved as static heap. It is only valid when the bank is + * in reserved_mem. + */ + MEMBANK_STATIC_HEAP, + /* + * The MEMBANK_FDT_RESVMEM type is used to indicate whether the memory + * bank is from the FDT reserve map. + */ + MEMBANK_FDT_RESVMEM, +}; + +/* Indicates the maximum number of characters(\0 included) for shm_id */ +#define MAX_SHM_ID_LENGTH 16 + +struct shmem_membank_extra { + char shm_id[MAX_SHM_ID_LENGTH]; + unsigned int nr_shm_borrowers; +}; + +struct membank { + paddr_t start; + paddr_t size; + union { + enum membank_type type; +#ifdef CONFIG_STATIC_SHM + struct shmem_membank_extra *shmem_extra; +#endif + }; +}; + +struct membanks { + __struct_group(membanks_hdr, common, , + unsigned int nr_banks; + unsigned int max_banks; + ); + struct membank bank[]; +}; + +struct meminfo { + struct membanks_hdr common; + struct membank bank[NR_MEM_BANKS]; +}; + +struct shared_meminfo { + struct membanks_hdr common; + struct membank bank[NR_SHMEM_BANKS]; + struct shmem_membank_extra extra[NR_SHMEM_BANKS]; +}; + +/* + * The domU flag is set for kernels and ramdisks of "xen,domain" nodes. + * The purpose of the domU flag is to avoid getting confused in + * kernel_probe, where we try to guess which is the dom0 kernel and + * initrd to be compatible with all versions of the multiboot spec. + */ +#define BOOTMOD_MAX_CMDLINE 1024 +struct bootmodule { + bootmodule_kind kind; + bool domU; + paddr_t start; + paddr_t size; +}; + +/* DT_MAX_NAME is the node name max length according the DT spec */ +#define DT_MAX_NAME 41 +struct bootcmdline { + bootmodule_kind kind; + bool domU; + paddr_t start; + char dt_name[DT_MAX_NAME]; + char cmdline[BOOTMOD_MAX_CMDLINE]; +}; + +struct bootmodules { + int nr_mods; + struct bootmodule module[MAX_MODULES]; +}; + +struct bootcmdlines { + unsigned int nr_mods; + struct bootcmdline cmdline[MAX_MODULES]; +}; + +struct bootinfo { + struct meminfo mem; + /* The reserved regions are only used when booting using Device-Tree */ + struct meminfo reserved_mem; + struct bootmodules modules; + struct bootcmdlines cmdlines; +#ifdef CONFIG_ACPI + struct meminfo acpi; +#endif +#ifdef CONFIG_STATIC_SHM + struct shared_meminfo shmem; +#endif + bool static_heap; +}; + +#ifdef CONFIG_ACPI +#define BOOTINFO_ACPI_INIT .acpi.common.max_banks =3D NR_MEM_BANKS, +#else +#define BOOTINFO_ACPI_INIT +#endif + +#ifdef CONFIG_STATIC_SHM +#define BOOTINFO_SHMEM_INIT .shmem.common.max_banks =3D NR_SHMEM_BANKS, +#else +#define BOOTINFO_SHMEM_INIT +#endif + +#define BOOTINFO_INIT \ +{ \ + .mem.common.max_banks =3D NR_MEM_BANKS, \ + .reserved_mem.common.max_banks =3D NR_MEM_BANKS, \ + BOOTINFO_ACPI_INIT \ + BOOTINFO_SHMEM_INIT \ +} + +extern struct bootinfo bootinfo; + +void populate_boot_allocator(void); + +size_t boot_fdt_info(const void *fdt, paddr_t paddr); + +const char *boot_fdt_cmdline(const void *fdt); + +static inline struct membanks *bootinfo_get_reserved_mem(void) +{ + return container_of(&bootinfo.reserved_mem.common, struct membanks, co= mmon); +} + +static inline struct membanks *bootinfo_get_mem(void) +{ + return container_of(&bootinfo.mem.common, struct membanks, common); +} + +#ifdef CONFIG_ACPI +static inline struct membanks *bootinfo_get_acpi(void) +{ + return container_of(&bootinfo.acpi.common, struct membanks, common); +} +#endif + +#ifdef CONFIG_STATIC_SHM +static inline struct membanks *bootinfo_get_shmem(void) +{ + return container_of(&bootinfo.shmem.common, struct membanks, common); +} + +static inline struct shmem_membank_extra *bootinfo_get_shmem_extra(void) +{ + return bootinfo.shmem.extra; +} +#endif + +#endif /* XEN_BOOTFDT_H */ --=20 2.45.2