Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp451033pxa; Tue, 4 Aug 2020 09:19:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXcg4qbG+q5HytQVFd6dRR1jEFrouYDMZnhOKdZ8cwm1sp7ggF90Amedu89NEXN4/I7mTt X-Received: by 2002:a17:906:af51:: with SMTP id ly17mr21567304ejb.81.1596557986601; Tue, 04 Aug 2020 09:19:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1596557986; cv=pass; d=google.com; s=arc-20160816; b=cktZKoc14SZibiLYXviE1ePX4gfpR5Yp9AHxbTThdvzFkW0hugS7LNiidFx/Q01tAs CS0jtzfKi9Sq5sFggnZhsUR8viPXkDSJcHbaT/3ofJgHx4/PvxKEn/GsQggdh/ABq93G AMG+X50xTBglgR2AIgTLK6HbA78RXEFqLawAnoeF6/M15CG3g/hlC6Oq8CSg/MQDd/qo pOdOxR/Ca0H8Lp5MrqzBI2slxeSxfO6hakLzKN8tzceIl2Z9uDC9zqfdkYnneQCZxuEa L/F5Fp0ZGL0wfmcqd0ZgirS+qb0Y01LapuE1D5/0P2aDxVCsbmNXDL7SCc1b692WS3VG 9cdg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-language:in-reply-to:user-agent:date:message-id:from :references:cc:to:subject:dkim-signature; bh=vfd94lMqUs5pZhvwnisUhLwtasfM7T1LBvVYia5xzHw=; b=T6D8Mm+TvQ+nd6YC6tXLkgSKensbPgYZijjxmFjf4dpTJJ3zQod+bmQjjra4Ji9opt pEmSyzH3FObPLop9nySMWlZgoGxjGS2raLY9k3Js1QVVQoNyYjiAtZ8SHmSBLT5IigZN QyWeQCZ9srR/jGXTTzORuNjcv5kvlkqwo0IiWQljEjVpy7x+xmb1BIlYVipch0bqrG5p yQLfD32lyxs0xlBr9MNVnILmRHJSkjvuxnh2BoRrVQCkIjV2/IYQiK+uYIIvEDWGAN0V 7qU9dRPnCQYlS0tCLk3ntO2dxixjpReO5APbCdqKAnhx31fYl2WJSRZ75SmWigr4RCSy M9Ew== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=k0UUQGaN; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ov30si12902835ejb.181.2020.08.04.09.19.09; Tue, 04 Aug 2020 09:19:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=k0UUQGaN; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728678AbgHDQS2 (ORCPT + 99 others); Tue, 4 Aug 2020 12:18:28 -0400 Received: from mail-mw2nam10on2045.outbound.protection.outlook.com ([40.107.94.45]:26369 "EHLO NAM10-MW2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728271AbgHDQSW (ORCPT ); Tue, 4 Aug 2020 12:18:22 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Ec/c63t4dcYjm6c7ykudn/di/RBXWutYU5cdER/1BbZbJ6sn0tqALthNXBeLyG6ZcAo0ueV1iVc8fTfSV1vfa6vwjQ+Osd2OGn+Cd4I0LWFAwxRnEPlNSIbqBLhkl3QHD7S6794i7wlJF/FRXUoLtkHx0OdXfKFWdqq39iZj3Z/Ua/ApkpUeQPfLg9hgE1rGTBqooKh/ZHEv1zFODkAUtPThfRjghjiLwMqNnOnzNBMgqJ7b7TsnTzARVie6hcvDFvYJOS3WKhxDtqNEyN9yBZqo4WhTL1S4Eu98bTwgkDxQC5HYi09scX1t8zxbSsQ1yCQULwmNc7vGeh+83H8LKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vfd94lMqUs5pZhvwnisUhLwtasfM7T1LBvVYia5xzHw=; b=N1U2m++EXwfjYSq4VE8ad/iYXK+Q44nn6NFJpyJTrHonm2hhHpYmeuwUiTUzGVXBUmlfeSO5te3+MLicKddJTtG8Bk3uJWQ1AiF6X+CF6HNCMj20kDyz4eHoL7qKzobX+rx4VUd22xBTeK7n4M25TSEWqhIwiOhGaukp/wYP0P6WHv1vhn34EALxqGqzvi8Dd9qO26ool+9IpyBxm90rHXD6NFmj8KggiuSbPH8V7J1LqidlLuGR9GrMiZG8K0DbdEEXTj5tvQOQibp/DnDWB7qXzZC6ngd/6XbpUEZge6ZoDMvT4Xc2SG3Y08jTjU1tLwW7gT6duJcUjXPDETKNMw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vfd94lMqUs5pZhvwnisUhLwtasfM7T1LBvVYia5xzHw=; b=k0UUQGaNlXtcBKPDBR1NEXMP8REXlyrHks5Lx7u2EjVHzL6N5buGCXen1Q109nDULvZqFl0Wqf4W7C3tNrDeVxV8Hgw9PokvjdTJVrrlCw+ur0ewmZ+/Az9TGtArV3Pqq3PF6TMdtUNcuXcpb3O8k3zcFOOE2AQRvhXc8CJqBUE= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=amd.com; Received: from BYAPR12MB3560.namprd12.prod.outlook.com (2603:10b6:a03:ae::10) by BYAPR12MB3575.namprd12.prod.outlook.com (2603:10b6:a03:ab::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.17; Tue, 4 Aug 2020 16:18:18 +0000 Received: from BYAPR12MB3560.namprd12.prod.outlook.com ([fe80::7d42:c932:e35f:71b1]) by BYAPR12MB3560.namprd12.prod.outlook.com ([fe80::7d42:c932:e35f:71b1%7]) with mapi id 15.20.3239.021; Tue, 4 Aug 2020 16:18:18 +0000 Subject: Re: [PATCH] drm/amd/display: use correct scale for actual_brightness To: Alexander Monakov , amd-gfx@lists.freedesktop.org Cc: Alex Deucher , linux-kernel@vger.kernel.org References: <20200803200218.2167-1-amonakov@ispras.ru> From: "Kazlauskas, Nicholas" Message-ID: <35b551fe-7c14-3d8e-4056-39975bd75105@amd.com> Date: Tue, 4 Aug 2020 12:18:11 -0400 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0 In-Reply-To: <20200803200218.2167-1-amonakov@ispras.ru> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-ClientProxiedBy: YT1PR01CA0002.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01::15) To BYAPR12MB3560.namprd12.prod.outlook.com (2603:10b6:a03:ae::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [172.31.148.234] (165.204.55.211) by YT1PR01CA0002.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.16 via Frontend Transport; Tue, 4 Aug 2020 16:18:17 +0000 X-Originating-IP: [165.204.55.211] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 6b012456-7d34-40bc-d131-08d83891ff55 X-MS-TrafficTypeDiagnostic: BYAPR12MB3575: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hSrCVOuKf7XdBEjxZCD2aGgwxkdmqYSEzbdLay9dcoRaM6Z7eQpsFgf9Kpx3IhrzgUlAO7B6l78pQEBhqsa+BqtDpeGhq16Yi7JDMtMfssmWqM5KY4lNF95KFNXJGONtU3DxAB4cOVS7bkZhPHJlj+AHn+s8tLV4ZBIBz0j5oIS7ipPWbXhvTFfxu1fIxHUyJ6lnmexqSPPio85vgBAkTcVaK+a1wtzyChJshtiHm4u7sgarh8oOJYTvZl4F+SdcYWtCuAhjaxFPTSeV11fLhViQb/tT0TQbsVcXEo9/xTpQNYdCYziPdSnn5/s5y+uMfHe3uQl9Hig1Dcjlbelb7aw2gBjr2moGToixhFJUnJWyYjfixn+73eMUnTpf1Jjxps+CPt/50OHXpTVRkRwoZhuGzYyrJxl1tFNB2AUt01ftUZaITKTH8YgHQvaiil8cLinCgK/oP3pG6RGoqBJ/ow== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB3560.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(4636009)(39860400002)(346002)(376002)(396003)(136003)(366004)(6666004)(31686004)(31696002)(16576012)(8676002)(5660300002)(478600001)(966005)(316002)(8936002)(6486002)(86362001)(66556008)(66946007)(66476007)(53546011)(36756003)(26005)(4326008)(52116002)(956004)(16526019)(2616005)(186003)(83380400001)(2906002)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 4vOZarAZ5s8u7gC+YygzMPmPEwFJVsf9E9enD9ogyb53OZl41Ku1yuU5uBV0QD+nr154vLXrBER0n12EdiDiWRVygE2tQ764B/Nrm1kePK4tTAwDqW7HZFn4U8LzOEGzaa4l3R8rrxb1SQHy1PS+oh3YCxiUNJn8KhLwp7ikcyYb/0LNKpp1MNHtEEPZGyslchwOz/QnKQaLa24kOl8x8g8qEt5usWrnbe1mmNQrHaDNiyBCzAKnQvnpP0i3f+qCMtaRL73M76UOYUSV0+/pFZ58zvpZfR+9v97rSF9XixOgkJy9Wefiav3LeBBY+/MgoUZNb5R8UpfrDJS6C3ZheXFTTTdf61NvuOqT5xrbNOyOcVm7e6JHH1RZ5qiW3ITCGICf/wiLMduC+/G2b/Y3HDcu4igFfvkfFfNE4mW12XXzTbaVYk+c9Q/pG7GyJMw3UIx9ny5K1YOblIM1KiCeoRo6h8t+65NM2cQQYMdk/PZILKHR2K2ASLwtC/j6YbAfSrDTy5dVur4M+QgkbVjLORlZC7AW73rpDC4HyBY7bcNRdnyW7R3mUp2TW5xcdoH5Bw9EpRQjj8UIUSV1CqEIP5fnxOWCet3dRFu4n0HKGDUt+Yz1SJwz2GYH3IEYFZYZWZnMtvtx4Uc01rLPRv5WiQ== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6b012456-7d34-40bc-d131-08d83891ff55 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB3560.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Aug 2020 16:18:18.1560 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: MxS2+1/Y8wShdfzkOKL6kx1Kz0JRD5or/SuYX1CZWfFuf3zJ2NvmcJGPoZDIZtlXZxkFayTi+jnno8lRaXCE7g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB3575 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This is a cleaner change the other proposed patch since it doesn't need to modify the exist conversion functions but I'd be worried about broken userspace relying on 0-255 as the only acceptable range. Not an expert on existing implementations to point out a specific one though. Regards, Nicholas Kazlauskas On 2020-08-03 4:02 p.m., Alexander Monakov wrote: > Documentation for sysfs backlight level interface requires that > values in both 'brightness' and 'actual_brightness' files are > interpreted to be in range from 0 to the value given in the > 'max_brightness' file. > > With amdgpu, max_brightness gives 255, and values written by the user > into 'brightness' are internally rescaled to a wider range. However, > reading from 'actual_brightness' gives the raw register value without > inverse rescaling. This causes issues for various userspace tools such > as PowerTop and systemd that expect the value to be in the correct > range. > > Introduce a helper to retrieve internal backlight range. Extend the > existing 'convert_brightness' function to handle conversion in both > directions. > > Bug: https://bugzilla.kernel.org/show_bug.cgi?id=203905 > Bug: https://gitlab.freedesktop.org/drm/amd/-/issues/1242 > Cc: Alex Deucher > Signed-off-by: Alexander Monakov > --- > .../gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 73 ++++++++----------- > 1 file changed, 32 insertions(+), 41 deletions(-) > > diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c > index 710edc70e37e..03e21e7b7917 100644 > --- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c > +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c > @@ -2881,51 +2881,42 @@ static int set_backlight_via_aux(struct dc_link *link, uint32_t brightness) > return rc ? 0 : 1; > } > > -static u32 convert_brightness(const struct amdgpu_dm_backlight_caps *caps, > - const uint32_t user_brightness) > +static int get_brightness_range(const struct amdgpu_dm_backlight_caps *caps, > + unsigned *min, unsigned *max) > { > - u32 min, max, conversion_pace; > - u32 brightness = user_brightness; > - > if (!caps) > - goto out; > + return 0; > > - if (!caps->aux_support) { > - max = caps->max_input_signal; > - min = caps->min_input_signal; > - /* > - * The brightness input is in the range 0-255 > - * It needs to be rescaled to be between the > - * requested min and max input signal > - * It also needs to be scaled up by 0x101 to > - * match the DC interface which has a range of > - * 0 to 0xffff > - */ > - conversion_pace = 0x101; > - brightness = > - user_brightness > - * conversion_pace > - * (max - min) > - / AMDGPU_MAX_BL_LEVEL > - + min * conversion_pace; > + if (caps->aux_support) { > + // Firmware limits are in nits, DC API wants millinits. > + *max = 1000 * caps->aux_max_input_signal; > + *min = 1000 * caps->aux_min_input_signal; > } else { > - /* TODO > - * We are doing a linear interpolation here, which is OK but > - * does not provide the optimal result. We probably want > - * something close to the Perceptual Quantizer (PQ) curve. > - */ > - max = caps->aux_max_input_signal; > - min = caps->aux_min_input_signal; > - > - brightness = (AMDGPU_MAX_BL_LEVEL - user_brightness) * min > - + user_brightness * max; > - // Multiple the value by 1000 since we use millinits > - brightness *= 1000; > - brightness = DIV_ROUND_CLOSEST(brightness, AMDGPU_MAX_BL_LEVEL); > + // Firmware limits are 8-bit, PWM control is 16-bit. > + *max = 0x101 * caps->max_input_signal; > + *min = 0x101 * caps->min_input_signal; > } > + return 1; > +} > > -out: > - return brightness; > +static u32 convert_brightness(const struct amdgpu_dm_backlight_caps *caps, > + const uint32_t brightness, int from_user) > +{ > + unsigned min, max; > + > + if (!get_brightness_range(caps, &min, &max)) > + return brightness; > + > + if (from_user) > + // Rescale 0..255 to min..max > + return min + DIV_ROUND_CLOSEST((max - min) * brightness, > + AMDGPU_MAX_BL_LEVEL); > + > + if (brightness < min) > + return 0; > + // Rescale min..max to 0..255 > + return DIV_ROUND_CLOSEST(AMDGPU_MAX_BL_LEVEL * (brightness - min), > + max - min); > } > > static int amdgpu_dm_backlight_update_status(struct backlight_device *bd) > @@ -2941,7 +2932,7 @@ static int amdgpu_dm_backlight_update_status(struct backlight_device *bd) > > link = (struct dc_link *)dm->backlight_link; > > - brightness = convert_brightness(&caps, bd->props.brightness); > + brightness = convert_brightness(&caps, bd->props.brightness, 1); > // Change brightness based on AUX property > if (caps.aux_support) > return set_backlight_via_aux(link, brightness); > @@ -2958,7 +2949,7 @@ static int amdgpu_dm_backlight_get_brightness(struct backlight_device *bd) > > if (ret == DC_ERROR_UNEXPECTED) > return bd->props.brightness; > - return ret; > + return convert_brightness(&dm->backlight_caps, ret, 0); > } > > static const struct backlight_ops amdgpu_dm_backlight_ops = { > > base-commit: bcf876870b95592b52519ed4aafcf9d95999bc9c >