Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2472374rdb; Wed, 4 Oct 2023 02:08:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFM5lJB/v3d+cJU1qV4JZDX92Hly7GOBKHcVzmR4y7ozCJCy9lhL1t+GzU9XWLiVViV60m2 X-Received: by 2002:a17:902:e843:b0:1c5:cc30:7329 with SMTP id t3-20020a170902e84300b001c5cc307329mr1987482plg.54.1696410503429; Wed, 04 Oct 2023 02:08:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696410503; cv=none; d=google.com; s=arc-20160816; b=NsX0X5S969YKO0twK9/5IzPyZIT6dyqvFCoUOFtKtZiWZtI7iJ3Abt/RbdDldSJWnz psigE4QkjtE2KLlC5+ldWh4wR9KKTT6t2OJ5ld3AqIn+M0ALBsdQh0kOjgi11brCf/lw USd62r2vtvj0iwiVRzvWK0s5J06FDVwyOx60T9JM8ZkchFu8sc5EOKOHZFhudbKYuNPx uZObvh7boDYfmcI2enxTKHDZfenTK9CkKPS9JVMQUUINenkq3uORdQIs8+eURfjJX3D8 jiu++k/lNc4hV6dWYUePGBVm+VuEw1wqn8AxZgqLQ0caCFfIUAmySmgVhMiJqcNJaJZS 1gJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=HTImhnCBUlgVnxT2gov/gnz4eDUCQxkYZCNPZyxmkJA=; fh=gJLdGyXCci1qqvuJX+uVvchjhcXPP1Lzo0CVVbfNb5I=; b=zj/fjMAzm2s/BJ+EXS6ZtdvogRnjFqfIxvYrcJcQrsMNq65/wPTapXUTFWuGLCycah WKuQIMTxSDIjkieQvA4DsCvE8NeKjlKemGMTwjsZJBQui17NlEVe9KsQd3Bj+MGdEXPl PTSX1ncI3pLNeK84W3zkZc364Zw+bkALExGzJHN5V7k532UQIG1wJDgFIS14UwPsu3+X F3+tGkpfC5HAjOkytO67VU58zEpFZVXOKNVpe3r70vrEK+DVtmNeR1vkiG1KZCwzpjXb TZ6G383ABkVbHAUj6Lv3aSLiPobcisV+K94lx+chwyRkGu0UFWTpVlKffzfdJ5tprfjI /sqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=MwyGCsQG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id r21-20020a170902e3d500b001c6182d7d43si3172974ple.21.2023.10.04.02.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 02:08:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=MwyGCsQG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 18E30807387A; Wed, 4 Oct 2023 02:08:20 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241944AbjJDJH3 (ORCPT + 99 others); Wed, 4 Oct 2023 05:07:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241818AbjJDJHR (ORCPT ); Wed, 4 Oct 2023 05:07:17 -0400 Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3EE9EA; Wed, 4 Oct 2023 02:07:05 -0700 (PDT) Received: by mail.gandi.net (Postfix) with ESMTPSA id 8420C1C001A; Wed, 4 Oct 2023 09:07:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1696410424; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HTImhnCBUlgVnxT2gov/gnz4eDUCQxkYZCNPZyxmkJA=; b=MwyGCsQGVsRylVpnHadTw8kEdGld09pYQYGlBx7ct7J4ONwlTxQkIcdp6tXrD1IdRJGuUU Apczz0FMbAcAY45od84+YTLTClyF8R64AeFSjYB9cMscNRrYfOglQFkSdW0bZkbsQ1IthV XHrrU1UJ/kyA3GHvXMJMAvZFx3yMgXpN5z20k/WL88J0LS344mTmgU2VU0EfX8HyPMLJpl siUD8rcvPZP/DAxyst1hNJyz20hdwCI9Lk7epz1/AGIODdNvy0zvfxFjjhDoFAzO7trhB6 YyNPTIn7AQKl2OwLw3WH/aM2cfniJfJGz91LOUSqgSP54DhBROsAF5uWOBLu6g== Date: Wed, 4 Oct 2023 11:07:01 +0200 From: Herve Codina To: Rob Herring Cc: Lizhi Hou , Frank Rowand , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni Subject: Re: [PATCH 1/1] of: address: Fix address translation when address-size is greater than 2 Message-ID: <20231004110701.0c9aa467@bootlin.com> In-Reply-To: References: <20231003065236.121987-1-herve.codina@bootlin.com> Organization: Bootlin X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-GND-Sasl: herve.codina@bootlin.com X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Wed, 04 Oct 2023 02:08:20 -0700 (PDT) On Tue, 3 Oct 2023 16:12:25 -0500 Rob Herring wrote: > On Tue, Oct 3, 2023 at 1:53 AM Herve Codina wrote: > > > > With the recent addition of of_pci_prop_ranges() in commit 407d1a51921e > > ("PCI: Create device tree node for bridge"), the ranges property can > > have a 3 cells child address, a 3 cells parent address and a 2 cells > > child size. > > Sigh. I'm starting to regret applying this for 6.6... You failed to Cc > the AMD folks too. Lizhi now added. > > What's different here from the test cases? The having 3 cells in > parent and child? Are you talking about of_unittest_pci_node()? I so, only BAR0 is used and the DT overlay is fragment@0 { target-path=""; __overlay__ { #address-cells = <3>; #size-cells = <2>; pci-ep-bus@0 { compatible = "simple-bus"; #address-cells = <1>; #size-cells = <1>; ranges = <0x0 0x0 0x0 0x0 0x1000>; reg = <0 0 0 0 0>; unittest-pci@100 { compatible = "unittest-pci"; reg = <0x100 0x200>; }; }; }; }; > > > > > A range item property for a PCI device is filled as follow: > > 0 0 > > <-- Child --> <-- Parent (PCI definition) --> <- BAR size (64bit) --> > > > > This allow to translate BAR addresses from the DT. For instance: > > pci@0,0 { > > #address-cells = <0x03>; > > #size-cells = <0x02>; > > device_type = "pci"; > > compatible = "pci11ab,100\0pciclass,060400\0pciclass,0604"; > > ranges = <0x82000000 0x00 0xe8000000 > > 0x82000000 0x00 0xe8000000 > > 0x00 0x4400000>; > > ... > > dev@0,0 { > > #address-cells = <0x03>; > > #size-cells = <0x02>; > > compatible = "pci1055,9660\0pciclass,020000\0pciclass,0200"; > > /* Translations for BAR0 to BAR5 */ > > ranges = <0x00 0x00 0x00 0x82010000 0x00 0xe8000000 0x00 0x2000000 > > 0x01 0x00 0x00 0x82010000 0x00 0xea000000 0x00 0x1000000 > > 0x02 0x00 0x00 0x82010000 0x00 0xeb000000 0x00 0x800000 > > 0x03 0x00 0x00 0x82010000 0x00 0xeb800000 0x00 0x800000 > > 0x04 0x00 0x00 0x82010000 0x00 0xec000000 0x00 0x20000 > > 0x05 0x00 0x00 0x82010000 0x00 0xec020000 0x00 0x2000>; > > ... > > pci-ep-bus@0 { > > #address-cells = <0x01>; > > #size-cells = <0x01>; > > compatible = "simple-bus"; > > /* Translate 0xe2000000 to BAR0 and 0xe0000000 to BAR1 */ > > ranges = <0xe2000000 0x00 0x00 0x00 0x20000000 > > 0xe0000000 0x01 0x00 0x00 0x1000000>; > > Why are you reusing a PCI bus address value for the child bus? I'm > wondering if this is some hackery because the child devices need PCI > addresses to work. What address does a device need for DMA for > example? I don't think I re-use a PCI bus address. In my device datasheet, 0xe2000000 to 0xe3ffffff are mapped to BAR0 and 0xe0000000 to 0xe0FFFFFF are mapped to BAR1. And so, all devices use this kind of addresses in their 'reg' property. > > Also, I think each BAR should be a separate child. We need to > formalize this BAR addressing in a schema. I am not sure that we should describe the hardware with a tree based on BAR. On my system, some devices use two BARs. For instance, I have a 'microchip,lan966x-switch' https://elixir.bootlin.com/linux/v6.5/source/Documentation/devicetree/bindings/net/microchip,lan966x-switch.yaml This devices use two values (two reg set) for its reg property. On my system, this device is accessed through the PCI bus and one reg set should be accessed using BAR0 and the other set using BAR1. Having a tree based on BARs will break this kind of devices. > > > ... > > }; > > }; > > }; > > > > During the translation process, the "default-flags" map() function is > > used to select the matching item in the ranges table and determine the > > address offset from this matching item. > > This map() function simply calls of_read_number() and when address-size > > is greater than 2, the map() function skips the extra high address part > > (ie part over 64bit). This lead to a wrong matching item and a wrong > > offset computation. > > Also during the translation itself, the extra high part related to the > > parent address is not present in the translated address. > > > > Fix the "default-flags" map() and translate() in order to take into > > account the child extra high address part in map() and the parent extra > > high address part in translate() and so having a correct address > > translation for ranges patterns such as the one given in the example > > above. > > Please add a test case for this. Should I add it in the of_unittest_pci_node() or create a new unittest dedicated to this translation test? > > > > > Signed-off-by: Herve Codina > > --- > > drivers/of/address.c | 56 ++++++++++++++++++++++++++++++++++++++++++-- > > 1 file changed, 54 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/of/address.c b/drivers/of/address.c > > index e692809ff822..8665de3a6344 100644 > > --- a/drivers/of/address.c > > +++ b/drivers/of/address.c > > @@ -100,6 +100,58 @@ static unsigned int of_bus_default_get_flags(const __be32 *addr) > > return IORESOURCE_MEM; > > } > > > > +static bool of_addr_is_equal(const __be32 *addr1, const __be32 *addr2, int na) > > +{ > > + int i; > > + u32 a1; > > + u32 a2; > > + > > + for (i = 0; i < na; i++) { > > + a1 = be32_to_cpup(addr1 + i); > > + a2 = be32_to_cpup(addr2 + i); > > You don't need to do the swap. Then isn't this just a memcmp? Indeed, I will remove the swap in the next iteration. > > > + if (a1 == a2) > > + continue; > > + return false; > > + } > > + return true; > > +} > > + > > +static u64 of_bus_default_flags_map(__be32 *addr, const __be32 *range, int na, > > + int ns, int pna) > > +{ > > + u64 cp, s, da; > > + int extra = 0; > > + > > + if (na > 2) { > > I believe this clause works for na==2. It wouldn't for na==1 though. > All you really need is: > > if (na == 3) > extra = 1; > > But at this point, don't we know we have a 3rd cell? We've only > matched if we do, right? If "extra" is always 1, then this function is > pretty much the same as the PCI and ISA versions after the flags > comparison. Right. I will simplify this function in the next iteration. > > > + /* > > + * Given address contains more than 2 cells. > > + * The address high extra part must match the range extra part > > + * and must be filtered-out from 64bit offset computation. > > + */ > > + extra = na - 2; > > + if (!of_addr_is_equal(addr, range, extra)) > > A bit misleading as all you are doing is comparing the 3rd (top) cell. Will simplify. > > > + return OF_BAD_ADDR; > > + > > + cp = of_read_number(range + extra, na - extra); > > + s = of_read_number(range + na + pna, ns); > > + da = of_read_number(addr + extra, na - extra); > > + } else { > > + cp = of_read_number(range, na); > > + s = of_read_number(range + na + pna, ns); > > + da = of_read_number(addr, na); > > + } > > + pr_debug("default flags map, extra=%d cp=%llx, s=%llx, da=%llx\n", extra, cp, s, da); > > + > > + if (da < cp || da >= (cp + s)) > > + return OF_BAD_ADDR; > > + return da - cp; > > +} > > + > > +static int of_bus_default_flags_translate(__be32 *addr, u64 offset, int na) > > Same as of_bus_pci_translate() and of_bus_isa_translate(). 3rd case > gets to rename and consolidate. > > > +{ > > + /* Keep "flags" part in translated address */ > > + return of_bus_default_translate(addr + 1, offset, na - 1); > > +} I would keep of_bus_default_flags_translate() and use this one for PCI and ISA translation. Indeed, I think the term default_flags_translate makes sense in PCI and ISA as > > > > #ifdef CONFIG_PCI > > static unsigned int of_bus_pci_get_flags(const __be32 *addr) > > @@ -374,8 +426,8 @@ static struct of_bus of_busses[] = { > > .addresses = "reg", > > .match = of_bus_default_flags_match, > > .count_cells = of_bus_default_count_cells, > > - .map = of_bus_default_map, > > - .translate = of_bus_default_translate, > > + .map = of_bus_default_flags_map, > > + .translate = of_bus_default_flags_translate, > > .has_flags = true, > > .get_flags = of_bus_default_flags_get_flags, > > }, > > -- > > 2.41.0 > > Thanks for your feedback. Best regards, Hervé