Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTC: add basic RTC support in OP-TEE #5179

Merged
merged 5 commits into from
Mar 2, 2022
Merged

Conversation

clementleger
Copy link
Contributor

This PR adds support for the following:

  • RTC API
  • sama5d2 RTC driver
  • RTC PTA

On sama5d2, the RTC is contained in a SYS peripheral group which contains the WDT, the RSTC, the PIT and the RTC. This group can only be set secure as a whole and if doing so, the RTC won't be accessible anymore by the non-secure world. This PR adds support to expose the RTC through a PTA which will allow to communicate with a non-secure world driver (see [1]).

[1] (clementleger/linux@78c118b)

@clementleger clementleger force-pushed the rtc branch 2 times, most recently from d59cf83 to 335bc00 Compare February 17, 2022 10:44
@clementleger
Copy link
Contributor Author

New version:

  • Rebased on master
  • Remove DIV_ROUND_NEAREST commit
  • Use UDIV_ROUND_NEAREST function instead in RTC driver

core/drivers/atmel_rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated
if (!time)
return TEE_ERROR_BAD_PARAMETERS;

rtc_set_time(time);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the info in time doesn't make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually depends on the driver. The AT91 RTC will actually return an error while programming it. Maybe I could add a sanity check to at least avoid total non sense but it will probably be a bit difficult (leap year, february duration and so on)

core/include/drivers/rtc.h Show resolved Hide resolved
@clementleger clementleger force-pushed the rtc branch 2 times, most recently from a9267ad to b3d791c Compare February 17, 2022 14:18
@clementleger
Copy link
Contributor Author

New version:

  • Use SHIFT_U32 were applicable in atmel_rtc driver
  • Removed all debug print in rtc_pta.c
  • Use MIN() in atmel_rtc driver
  • Update copyrights

core/pta/rtc.c Show resolved Hide resolved
/*
* PTA_CMD_RTC_SET_OFFSET - Set RTC offset
*
* param[0] (in value) - value.a: RTC offset to be set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the offset be negative?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the offset can be negative.

core/pta/rtc.c Outdated
return TEE_ERROR_BAD_PARAMETERS;
}

rtc_set_offset(params[0].value.a);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since an offset can be negative I suppose that even though the value is a uint32_t it should be casted into a int32_t in order to be properly upgraded to a long on 64bit platforms.

@clementleger
Copy link
Contributor Author

clementleger commented Feb 21, 2022

New version:

  • Split RTC API definition and RTC PTA structs and defines into specific types for each.
  • Cast offset into int32_t
  • Packed PTA RTC structs to guarantee layout

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments. The API looks good to me, only some descriptions are missing.
As for file rtc_pta_client.h. most PTA header file (where their API/ABI is defined) are named pta_foo.h. Prefer renaming it to pta_rtc.h?

core/include/drivers/rtc.h Show resolved Hide resolved
core/include/drivers/rtc.h Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
lib/libutee/include/rtc_pta_client.h Outdated Show resolved Hide resolved
lib/libutee/include/rtc_pta_client.h Outdated Show resolved Hide resolved
core/pta/rtc.c Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
lib/libutee/include/rtc_pta_client.h Outdated Show resolved Hide resolved
core/drivers/atmel_rtc.c Outdated Show resolved Hide resolved
core/drivers/atmel_rtc.c Outdated Show resolved Hide resolved
@clementleger
Copy link
Contributor Author

clementleger commented Feb 22, 2022

Some comments. The API looks good to me, only some descriptions are missing. As for file rtc_pta_client.h. most PTA header file (where their API/ABI is defined) are named pta_foo.h. Prefer renaming it to pta_rtc.h?

I named it after the rng pta one which is name rng_pta_client.h. I can rename it but it won't be coherent with what already exists.

Edit: But since all others are named differently, I will rename it

@clementleger
Copy link
Contributor Author

clementleger commented Feb 22, 2022

New version:

  • Fixed Etienne comments
  • Rework RTC feature to be set based on set/get_offset presence
  • Add a check for rtc ops to be set in rtc_register()
  • Rename rtc_pta_client.h to pta_rtc.h

@clementleger
Copy link
Contributor Author

New version:

  • Removed return value from rtc_register() and add assert for checking ops. Using a returned value does not really makes sense. The fact that a RTC driver provides the correct operations should be done during development and thus, there is no need to check that at runtime.

@clementleger
Copy link
Contributor Author

Also, renamed CFG_RTC to CFG_DRIVERS_RTC to match what was done for other frameworks. (Unfortunately, I missed that for the watchdog)

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @clementleger, I though I posted some comments but they were still drafts. Here there are. Only minor comments.

core/drivers/rtc/rtc.c Show resolved Hide resolved
lib/libutee/include/pta_rtc.h Outdated Show resolved Hide resolved
lib/libutee/include/pta_rtc.h Outdated Show resolved Hide resolved
lib/libutee/include/pta_rtc.h Outdated Show resolved Hide resolved
lib/libutee/include/pta_rtc.h Outdated Show resolved Hide resolved
lib/libutee/include/pta_rtc.h Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/drivers/atmel_rtc.c Outdated Show resolved Hide resolved
core/drivers/atmel_rtc.c Outdated Show resolved Hide resolved
Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ability the set date and offset is opened to any client application invoking the PTA. I wonder if should there be some identification passed to restrict such accesses upon 'root' or like login identification?

core/include/drivers/rtc.h Outdated Show resolved Hide resolved
core/drivers/atmel_rtc.c Outdated Show resolved Hide resolved
core/include/drivers/rtc.h Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Show resolved Hide resolved
core/pta/rtc.c Outdated Show resolved Hide resolved
core/pta/rtc.c Show resolved Hide resolved
uint32_t tm_mon;
uint32_t tm_year;
uint32_t tm_wday;
} __packed;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is __packed needed? I remember @jenswi-linaro worrying about such useless attribute.

Copy link
Contributor Author

@clementleger clementleger Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not needed since both OP-TEE and the OS runs on the same platform. I added them because I was asked to on some other PR which add the same kind of communication pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will wait for @jenswi-linaro answer before removing them.

@clementleger
Copy link
Contributor Author

The ability the set date and offset is opened to any client application invoking the PTA. I wonder if should there be some identification passed to restrict such accesses upon 'root' or like login identification?

Hum I'm not sure how I could do that properly. Do you have any example that I could look at if needed ?

@etienne-lms
Copy link
Contributor

On Linux side, we can see 2 ways to set the RTC data. Either using a kernel driver plugged to /dev/rtc* standard tools or an application that would straight invoke the PTA.
If you plan to use the first case, the kernel driver can open the session with TEE_IOCTL_LOGIN_REE_KERNEL ID while the PTA would restrict set time/offset commands to the sessions opened with TEE_LOGIN_REE_KERNEL. The kernel uses CAP_SYS_TIME to identify applications allowed to alter time reference.
In the second case, I think the PTA needs some CFG_RTC_PTA_LOGIN_USER_ID or CFG_RTC_PTA_LOGIN_GROUP_ID or like configuration means.

@clementleger
Copy link
Contributor Author

On Linux side, we can see 2 ways to set the RTC data. Either using a kernel driver plugged to /dev/rtc* standard tools or an application that would straight invoke the PTA. If you plan to use the first case, the kernel driver can open the session with TEE_IOCTL_LOGIN_REE_KERNEL ID while the PTA would restrict set time/offset commands to the sessions opened with TEE_LOGIN_REE_KERNEL. The kernel uses CAP_SYS_TIME to identify applications allowed to alter time reference.

Ok great, thanks for the explanations ! I already have a kernel driver for that RTC curently using TEE_IOCTL_LOGIN_PUBLIC so I will consider to open the session using TEE_IOCTL_LOGIN_REE_KERNEL.

@clementleger
Copy link
Contributor Author

New version:

  • Fix trailing newline
  • Add open_session callback to allow only kernel clients.

@clementleger
Copy link
Contributor Author

New version:

  • Remove TIME_INIT() macro and reorder optee_rtc_time struct members to be more logical

@jenswi-linaro
Copy link
Contributor

Acked-by: Jens Wiklander <jens.wiklander@linaro.org>

@clementleger
Copy link
Contributor Author

New version:

  • Added Acked-by Jens

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org> for commits
"drivers: rtc: add RTC API"
"pta: add PTA for RTC"

Acked-by: Etienne Carriere <etienne.carriere@linaro.org> for commits:
"driver: atmel_rtc: add driver for atmel RTC" (with minor comment)
"dts: sama5d2: set RTC as secure"
"plat-sam: enable RTC support"

static struct rtc atmel_rtc = {
.ops = &atmel_rtc_ops,
.range_min = {1900, 1, 1, 0, 0, 0, 0},
.range_max = {2099, 12, 31, 23, 59, 59, 0},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: add space char in braces:

	.range_min = { 1900, 1, 1, 0, 0, 0, 0 },
	.range_max = { 2099, 12, 31, 23, 59, 59, 0 },

This API allows to interact with a RTC registered as the system RTC.

Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Clément Léger <clement.leger@bootlin.com>
On some systems, when the RTC is secured, there is no way for the
normal world to access it. This PTA uses the RTC API to allow a
Linux OP-TEE based RTC driver to communicate with the RTC that is
secured.

Reviewed-by: Etienne Carriere <etienne.carriere@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Clément Léger <clement.leger@bootlin.com>
On sama5d2, the RTC is included in a larger block of devices that can
only be secured as a whole (RSTC, WDT, etc). Since these other
peripherals needs to be secured, in order to still allow the RTC to be
used from non-secure world, add a driver for the RTC which will be
registered as the system RTC. The RTc PTA will then used this RTC to
set/get time from Linux using a RTC driver that uses the TEE subsystem.

Acked-by: Etienne Carriere <etienne.carriere@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Clément Léger <clement.leger@bootlin.com>
The RTC on sama5d2 is actually securing both RSTC, WDT and RTC register
access. Enable secure mode for the RTC to ensure the WDT register
accesses are secured.

Acked-by: Etienne Carriere <etienne.carriere@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Clément Léger <clement.leger@bootlin.com>
Enable RTC API, RTC PTA and Atmel RTC driver for sama5d2.

Acked-by: Etienne Carriere <etienne.carriere@linaro.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Clément Léger <clement.leger@bootlin.com>
@clementleger
Copy link
Contributor Author

clementleger commented Mar 1, 2022

New version:

  • Added Acked/Reviewed-By Etienne
  • Added spaces before/after braces

@jforissier
Copy link
Contributor

@clementleger thanks!

@jforissier jforissier merged commit 0f6bd1d into OP-TEE:master Mar 2, 2022
fengguang pushed a commit to 0day-ci/linux that referenced this pull request Mar 2, 2022
This drivers allows to communicate with a RTC PTA handled by OP-TEE [1].
This PTA allows to query RTC information, set/get time and set/get
offset depending on the supported features.

[1] OP-TEE/optee_os#5179

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
@OP-TEE OP-TEE deleted a comment Mar 3, 2022
clementleger added a commit to clementleger/linux that referenced this pull request Mar 3, 2022
This drivers allows to communicate with a RTC PTA handled by OP-TEE [1].
This PTA allows to query RTC information, set/get time and set/get
offset depending on the supported features.

[1] OP-TEE/optee_os#5179

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
intel-lab-lkp pushed a commit to intel-lab-lkp/linux that referenced this pull request Mar 26, 2022
This drivers allows to communicate with a RTC PTA handled by OP-TEE [1].
This PTA allows to query RTC information, set/get time and set/get
offset depending on the supported features.

[1] OP-TEE/optee_os#5179

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20220308133505.471601-1-clement.leger@bootlin.com
cristibirsan pushed a commit to linux4microchip/linux that referenced this pull request Oct 7, 2022
This drivers allows to communicate with a RTC PTA handled by OP-TEE [1].
This PTA allows to query RTC information, set/get time and set/get
offset depending on the supported features.

[1] OP-TEE/optee_os#5179

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20220308133505.471601-1-clement.leger@bootlin.com
(cherry picked from commit 81c2f05)
Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
IsaacJT pushed a commit to IsaacJT/linux that referenced this pull request Feb 8, 2023
This drivers allows to communicate with a RTC PTA handled by OP-TEE [1].
This PTA allows to query RTC information, set/get time and set/get
offset depending on the supported features.

[1] OP-TEE/optee_os#5179

Signed-off-by: Clément Léger <clement.leger@bootlin.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
Link: https://lore.kernel.org/r/20220308133505.471601-1-clement.leger@bootlin.com
(cherry picked from commit 81c2f05)
Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
Signed-off-by: Isaac True <isaac.true@canonical.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants