2021-03-22 10:57:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package nvpci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-03-22 11:32:52 +00:00
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/mmio"
|
2021-03-22 10:57:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pmcEndianRegister = 0x4
|
|
|
|
pmcLittleEndian = 0x0
|
|
|
|
pmcBigEndian = 0x01000001
|
|
|
|
)
|
|
|
|
|
2022-02-07 11:15:46 +00:00
|
|
|
// MemoryResource represents a mmio region
|
2021-03-22 10:57:07 +00:00
|
|
|
type MemoryResource struct {
|
|
|
|
Start uintptr
|
|
|
|
End uintptr
|
|
|
|
Flags uint64
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:44:32 +00:00
|
|
|
// OpenRW read write mmio region
|
|
|
|
func (mr *MemoryResource) OpenRW() (mmio.Mmio, error) {
|
2021-03-22 10:57:07 +00:00
|
|
|
rw, err := mmio.OpenRW(mr.Path, 0, int(mr.End-mr.Start+1))
|
|
|
|
if err != nil {
|
2022-02-07 11:15:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to open file for mmio: %v", err)
|
2021-03-22 10:57:07 +00:00
|
|
|
}
|
|
|
|
switch rw.Read32(pmcEndianRegister) {
|
|
|
|
case pmcBigEndian:
|
|
|
|
return rw.BigEndian(), nil
|
|
|
|
case pmcLittleEndian:
|
|
|
|
return rw.LittleEndian(), nil
|
|
|
|
}
|
2022-02-07 11:15:46 +00:00
|
|
|
return nil, fmt.Errorf("unknown endianness for mmio: %v", err)
|
2021-03-22 10:57:07 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 09:44:32 +00:00
|
|
|
// OpenRO read only mmio region
|
|
|
|
func (mr *MemoryResource) OpenRO() (mmio.Mmio, error) {
|
2021-03-22 10:57:07 +00:00
|
|
|
ro, err := mmio.OpenRO(mr.Path, 0, int(mr.End-mr.Start+1))
|
|
|
|
if err != nil {
|
2022-02-07 11:15:46 +00:00
|
|
|
return nil, fmt.Errorf("failed to open file for mmio: %v", err)
|
2021-03-22 10:57:07 +00:00
|
|
|
}
|
|
|
|
switch ro.Read32(pmcEndianRegister) {
|
|
|
|
case pmcBigEndian:
|
|
|
|
return ro.BigEndian(), nil
|
|
|
|
case pmcLittleEndian:
|
|
|
|
return ro.LittleEndian(), nil
|
|
|
|
}
|
2022-02-07 11:15:46 +00:00
|
|
|
return nil, fmt.Errorf("unknown endianness for mmio: %v", err)
|
2021-03-22 10:57:07 +00:00
|
|
|
}
|