Skip to content

Commit

Permalink
tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
abarisani committed Jan 21, 2025
1 parent 26b5bf1 commit 6fcb621
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/usbarmory/tamago

go 1.23.0

require golang.org/x/sync v0.10.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
8 changes: 4 additions & 4 deletions internal/reg/port_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
// that can be found in the LICENSE file.

// func In8(port uint16) (val uint8)
TEXT ·In8(SB),$0-3
TEXT ·In8(SB),$0-9
MOVW port+0(FP), DX
// in al, dx
BYTE $0xec
MOVB AL, ret+8(FP)
MOVB AL, val+8(FP)
RET

// func Out8(port uint16, val uint8)
Expand All @@ -23,11 +23,11 @@ TEXT ·Out8(SB),$0-3
RET

// func In32(port uint32) (val uint32)
TEXT ·In32(SB),$0-8
TEXT ·In32(SB),$0-12
MOVL port+0(FP), DX
// in eax, dx
BYTE $0xed
MOVL AX, ret+8(FP)
MOVL AX, val+8(FP)
RET

// func Out32(port uint32, val uint32)
Expand Down
12 changes: 6 additions & 6 deletions internal/reg/reg32_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

// func Move(dst uint32, src uint32)
TEXT ·Move(SB),$0-8
MOVW dst+0(FP), AX
MOVW src+4(FP), BX
MOVL dst+0(FP), AX
MOVL src+4(FP), BX

// copy src to dst
MOVW (AX), CX
MOVW CX, (AX)
MOVL (AX), CX
MOVL CX, (AX)

// zero out src
MOVW $0, CX
MOVW CX, (BX)
MOVL $0, CX
MOVL CX, (BX)

RET
42 changes: 23 additions & 19 deletions kvm/virtio/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ func (d *Descriptor) Bytes() []byte {
return buf.Bytes()
}

// SetLength updates the descriptor length field.
func (d *Descriptor) SetLength(length uint32) {
off := 8
binary.LittleEndian.PutUint32(d.buf[off:], length)

d.length = length
}

// Init initializes a virtual queue descriptor the given buffer length.
func (d *Descriptor) Init(length int, flags uint16) {
addr, buf := dma.Reserve(length, 0)
Expand All @@ -74,6 +66,22 @@ func (d *Descriptor) Destroy() {
dma.Release(uint(d.Address))
}

// Read copies the contents of the descriptor buffer to b.
func (d *Descriptor) Read(b []byte) {
copy(b, d.buf)
}

// Write copies the contents of b to the descriptor buffer, updating its length
// field accordingly.
func (d *Descriptor) Write(b []byte) {
off := 8
length := uint32(len(b))
binary.LittleEndian.PutUint32(d.buf[off:], length)

d.length = length
copy(d.buf, b)
}

// Available represents a VirtIO virtual queue Available ring buffer.
//
// All exported fields are used one-time at initialization, fields requiring
Expand Down Expand Up @@ -287,9 +295,9 @@ func (d *VirtualQueue) Pop() (buf []byte) {
}

avail := d.Used.Ring(d.Used.last % d.size)

buf = make([]byte, avail.Length)
copy(buf, d.Descriptors[avail.Index].buf)

d.Descriptors[avail.Index].Read(buf)

d.Available.index += 1
d.Available.SetRingIndex(d.Available.index%d.size, uint16(avail.Index))
Expand All @@ -305,21 +313,17 @@ func (d *VirtualQueue) Push(buf []byte) {
d.Lock()
defer d.Unlock()

length := len(buf)
index := d.Available.Ring(d.Available.index % d.size)
used := d.Used.Index() - d.Used.last

d.Descriptors[index].SetLength(uint32(length))
copy(d.Descriptors[index].buf, buf)

d.Descriptors[index].Write(buf)
d.Available.SetIndex(d.Available.index + 1)

for used := d.Used.Index() - d.Used.last; used > 0; used-- {
index = used - 1
avail := d.Used.Ring(used)
for i := used; i > 0; i-- {
avail := d.Used.Ring(i - 1)

d.Available.SetRingIndex(d.Available.index%d.size, uint16(avail.Index))
d.Used.last += 1
}

return
d.Used.last += used
}
4 changes: 2 additions & 2 deletions soc/nxp/i2c/i2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (hw *I2C) tx(buf []byte) (err error) {
func (hw *I2C) start(repeat bool) (err error) {
var pos int

if repeat == false {
if !repeat {
// wait for bus to be free
if !reg.WaitFor16(hw.Timeout, hw.i2sr, I2SR_IBB, 1, 0) {
return errors.New("timeout waiting bus to be free")
Expand All @@ -296,7 +296,7 @@ func (hw *I2C) start(repeat bool) (err error) {
return errors.New("timeout waiting bus to be busy")
}

if repeat == false {
if !repeat {
// set Master Transmit mode
reg.Set16(hw.i2cr, I2CR_MTX)
}
Expand Down

0 comments on commit 6fcb621

Please sign in to comment.