Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@gendx
Copy link
Contributor

@gendx gendx commented Jun 15, 2020

Pull Request Overview

The mask fields don't need to be exposed publicly, as their use cases overlap with the existing functions to manipulate fields.

Testing Strategy

This pull request was tested by CI.

TODO or Help Wanted

  • These changes currently overlap with Prepare for 64 PMP config registers #1926, although are compatible with that PR, as discussed in Prepare for 64 PMP config registers #1926 (comment).
  • The Field::shift field should also be made private, but is currently used in chips/lowrisc/src/gpio.rs. That code should probably be refactored so that the shift is not exposed as-is.
    let shift = field.shift;
    let bit = if val { 1u32 } else { 0u32 };
    if shift < 16 {
    lower.write(mask_half::data.val(bit << shift) + mask_half::mask.val(1u32 << shift));
    } else {
    let upper_shift = shift - 16;
    upper.write(
    mask_half::data.val(bit << upper_shift) + mask_half::mask.val(1u32 << upper_shift),
    );
    }
    }
  • Likewise, the FieldValue::value field should be made private, but is currently used in
    #[inline]
    pub fn write(&self, field: FieldValue<T, R>) {
    self.set(field.value);
    }

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

These don't need to be exposed publicly, as their use cases overlap with
the existing functions to manipulate fields.
Copy link
Member

@ppannuto ppannuto left a comment

Choose a reason for hiding this comment

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

This makes a lot of sense to me.

@andre-richter, as our resident external library user: Any reliance on mask or subsequently shift being pub?

@bradjc bradjc added the blocked Waiting on something, like a different PR or a dependency. label Jun 15, 2020
@andre-richter
Copy link
Contributor

andre-richter commented Jun 16, 2020

@ppannuto Can currently only check on mobile, therefore a bit limited in my judgement. I am using mask for the CPU traits, but it looks like I only need to adapt the same way as libraries/riscv-csr/src/csr.rs in this PR has been changed to be good?

@ppannuto ppannuto requested a review from bradjc June 16, 2020 14:06
@bradjc bradjc removed the blocked Waiting on something, like a different PR or a dependency. label Jun 17, 2020
@bradjc
Copy link
Contributor

bradjc commented Jun 17, 2020

Do we merge this first? Or #1926?

@ppannuto
Copy link
Member

bors d=[alistair23, gendx]

I'm happy to let Alistair and Guillaume figure out whichever is easier

@ppannuto
Copy link
Member

bors d=alistair23,gendx

I was close bors.

@bors
Copy link
Contributor

bors bot commented Jun 17, 2020

✌️ alistair23 can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@bors
Copy link
Contributor

bors bot commented Jun 17, 2020

✌️ gendx can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@alistair23
Copy link
Contributor

bors r+

@bors
Copy link
Contributor

bors bot commented Jun 17, 2020

@bors bors bot merged commit 2bbef07 into tock:master Jun 17, 2020
@alistair23
Copy link
Contributor

I merged this one and rebased my PR

ppannuto added a commit that referenced this pull request Nov 11, 2020
Changes since v0.5:

 - #2095: Fix syntax errors and inconsistencies in documentation
 - #2071: Clarify bit widths in documentation examples
 - #2015: Use UnsafeCell in registers (see issue #2005)
 - #1939: Make the Field::mask and FieldValue::mask fields private
 - #1823: Allow large unsigned values as bitmasks + add bitmask! helper macro
 - #1554: Allow lifetime parameters for `register_structs! { Foo<'a> { ..`
 - #1661: Add `Aliased` register type for MMIO with differing R/W behavior

Thanks to @namyoonw, @brghena, @dabross, @gendx, @hudson-ayers, and @pfmooney
for contributions, fixes, and testing!

Closes #2138.
@ppannuto ppannuto mentioned this pull request Nov 11, 2020
2 tasks
lschuermann added a commit to lschuermann/tock that referenced this pull request Apr 2, 2021
This partially reverts commit fed7a58 ("Make the Field::mask
and FieldValue::mask fields private.") introduced through GitHub pull
request tock#1939.

In order to be able to implement atomic RISC-V CSR bit-field-set and
bit-field-clear operations which operate on tock-register's Fields,
access to the Field::mask field is required.

Signed-off-by: Leon Schuermann <[email protected]>
bors bot referenced this pull request Apr 5, 2021
2512: rv32i: use atomic CSR instructions for rv32i::support::atomic r=phil-levis a=lschuermann

### Pull Request Overview

This pull request contains a bunch of cleanly separated commits, enabling us to switch the `rv32i::support::atomic` to use RISC-V atomic CSR instructions.

Previously, the mstatus CSR was first read and then written to clear the machine mode interrupts. If an interrupt would arrive between these instructions, it could have changed mstatus, whereas this function would write back the previously read and modified register contents. Furthermore, if an interrupt handler would have cleared MIE between reading and writing the CSR, this function would set MIE again after execution of the passed in function.

As far as I can tell, this is not a real issue currently, due to the way interrupt handlers work and the fact that rv32i::support::atomic is already executed when the CPU is already in machine mode. However, it's best not to rely on this and use proper RISC-V instructions.

Additionally, this eliminates 7 instructions and one branch on rustc 1.52.0-nightly.

The other commits leading up to this are:

- riscv-csr: support atomic read/write & bit-field-set/clear ops

  Add atomic_replace, read_and_set_bits and read_and_clear_bits methods to the RISC-V CSR register wrappers to support the corresponding CSRRW, CSRRS and CSRRC RISV-C instructions.

  Using the atomic instructions can be required to prevent race conditions in interrupt contexts and context switches. Furthermore, using a combined read and write can often save instructions compared to a sequential read and write.

  The CSRR rd, csr and CSRW csr, rs1 instructions used in the RISC-V CSR register wrapper currently are assembler pseudo-instructions encoded as CSRRS rd, csr, x0 and CSRRW x0, csr, rs1 respectively (see RISC-V Specification v2.2, Volume I, 2.8 Control and Status Register Instructions).

- tock-registers: make Field::mask field public

  This partially reverts commit fed7a58 ("Make the Field::mask and FieldValue::mask fields private.") introduced through GitHub pull request tock#1939.

  In order to be able to implement atomic RISC-V CSR bit-field-set and bit-field-clear operations which operate on tock-register's Fields, access to the Field::mask field is required.

- tock-registers: fix Copy and Clone implementation on Field

  A Field contains a PhantomData over a generic R: RegisterLongName, and a PhantomData<R> is copyable regardless of whether R: Copy. However, using #[derive(Copy, Clone)] will generate

      #[automatically_derived]
      #[allow(unused_qualifications)]
      impl<T: ::core::marker::Copy + IntLike,
           R: ::core::marker::Copy + RegisterLongName>
              ::core::marker::Copy for Field<T, R> {}

  and hence Field will only implement Copy when R: Copy.

  Manually implementing Clone and Copy works around this issue. See rust-lang/rust#26925 for more information on this issue.

- riscv-csr: provide atomic read and set/clear field methods

  Add atomic read and set/clear methods for Fields on RISC-V CSR register wrappers. This wraps the read_and_set_bits and read_and_clear_bits into a more developer-friendly method that is compatible with the tock-registers types.


### Testing Strategy

This pull request was tested by compiling and looking at the generated assembly, as well as running litex/sim in Verilator and looking at the instruction trace.


### TODO or Help Wanted

N/A


### Documentation Updated

- [x] ~Updated the relevant files in `/docs`, or~ no updates are required.

### Formatting

- [x] Ran `make prepush`.


Co-authored-by: Leon Schuermann <[email protected]>
lschuermann added a commit to lschuermann/tock-registers that referenced this pull request Dec 18, 2021
This partially reverts commit 89505ae ("Make the Field::mask
and FieldValue::mask fields private.") introduced through GitHub pull
request tock/tock#1939.

In order to be able to implement atomic RISC-V CSR bit-field-set and
bit-field-clear operations which operate on tock-register's Fields,
access to the Field::mask field is required.

Signed-off-by: Leon Schuermann <[email protected]>
hudson-ayers pushed a commit that referenced this pull request Mar 7, 2022
This partially reverts commit fed7a58 ("Make the Field::mask
and FieldValue::mask fields private.") introduced through GitHub pull
request #1939.

In order to be able to implement atomic RISC-V CSR bit-field-set and
bit-field-clear operations which operate on tock-register's Fields,
access to the Field::mask field is required.

BUG=none
TEST=make

Signed-off-by: Leon Schuermann <[email protected]>
(cherry picked from commit 3fba562)
Change-Id: I01d414d8b317999387eca785bc26dbb0c2304b71
hudson-ayers pushed a commit that referenced this pull request Mar 7, 2022
Changes since v0.5:

 - #2095: Fix syntax errors and inconsistencies in documentation
 - #2071: Clarify bit widths in documentation examples
 - #2015: Use UnsafeCell in registers (see issue #2005)
 - #1939: Make the Field::mask and FieldValue::mask fields private
 - #1823: Allow large unsigned values as bitmasks + add bitmask! helper macro
 - #1554: Allow lifetime parameters for `register_structs! { Foo<'a> { ..`
 - #1661: Add `Aliased` register type for MMIO with differing R/W behavior

Thanks to @namyoonw, @brghena, @dabross, @gendx, @hudson-ayers, and @pfmooney
for contributions, fixes, and testing!

Closes #2138.

BUG=none
TEST=make

(cherry picked from commit 72a1e1e)
Change-Id: Ia2dbf69c950e3581a43f5d6601a5cf94abace750
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.

5 participants