1 /* This is just an experiment. Full automatic porting
2 is probably not possible but a lot can be automated. */
21 type PCIDevData
struct {
28 type PCIDevice
interface {
29 Scan(ctx Context
, addr PCIDevData
)
32 type InteltoolData
struct {
33 GPIO
map[uint16]uint32
34 RCBA
map[uint16]uint32
45 type AzaliaCodec
struct {
50 PinConfig
map[int]uint32
53 type DevReader
interface {
54 GetPCIList() []PCIDevData
56 GetInteltool() InteltoolData
57 GetAzaliaCodecs() []AzaliaCodec
58 GetACPI() map[string][]byte
59 GetCPUModel() []uint32
61 GetIOPorts() []IOPorts
71 type SouthBridger
interface {
72 GetGPIOHeader() string
76 NeedRouteGPIOManually()
79 var SouthBridge SouthBridger
80 var BootBlockFiles
map[string]string = map[string]string{}
81 var ROMStageFiles
map[string]string = map[string]string{}
82 var RAMStageFiles
map[string]string = map[string]string{}
83 var SMMFiles
map[string]string = map[string]string{}
84 var MainboardInit
string
85 var MainboardEnable
string
86 var MainboardIncludes
[]string
98 type IOAPICIRQ
struct {
103 var IOAPICIRQs
map[PCIAddr
]IOAPICIRQ
= map[PCIAddr
]IOAPICIRQ
{}
104 var KconfigBool
map[string]bool = map[string]bool{}
105 var KconfigComment
map[string]string = map[string]string{}
106 var KconfigString
map[string]string = map[string]string{}
107 var KconfigHex
map[string]uint32 = map[string]uint32{}
108 var KconfigInt
map[string]int = map[string]int{}
111 var FlashROMSupport
= ""
113 func GetLE16(inp
[]byte) uint16 {
114 return uint16(inp
[0]) |
(uint16(inp
[1]) << 8)
117 func FormatHexLE16(inp
[]byte) string {
118 return fmt
.Sprintf("0x%04x", GetLE16(inp
))
121 func FormatHex32(u
uint32) string {
122 return fmt
.Sprintf("0x%08x", u
)
125 func FormatHex8(u
uint8) string {
126 return fmt
.Sprintf("0x%02x", u
)
129 func FormatInt32(u
uint32) string {
130 return fmt
.Sprintf("%d", u
)
133 func FormatHexLE32(d
[]uint8) string {
134 u
:= uint32(d
[0]) |
(uint32(d
[1]) << 8) |
(uint32(d
[2]) << 16) |
(uint32(d
[3]) << 24)
135 return FormatHex32(u
)
138 func FormatBool(inp
bool) string {
146 func sanitize(inp
string) string {
147 result
:= strings
.ToLower(inp
)
148 result
= strings
.Replace(result
, " ", "_", -1)
149 result
= strings
.Replace(result
, ",", "_", -1)
150 result
= strings
.Replace(result
, "-", "_", -1)
151 for strings
.HasSuffix(result
, ".") {
152 result
= result
[0 : len(result
)-1]
157 func AddBootBlockFile(Name
string, Condition
string) {
158 BootBlockFiles
[Name
] = Condition
161 func AddROMStageFile(Name
string, Condition
string) {
162 ROMStageFiles
[Name
] = Condition
165 func AddRAMStageFile(Name
string, Condition
string) {
166 RAMStageFiles
[Name
] = Condition
169 func AddSMMFile(Name
string, Condition
string) {
170 SMMFiles
[Name
] = Condition
173 func IsIOPortUsedBy(ctx Context
, port
uint16, name
string) bool {
174 for _
, io
:= range ctx
.InfoSource
.GetIOPorts() {
175 if io
.Start
<= port
&& port
<= io
.End
&& io
.Usage
== name
{
182 var FlagOutDir
= flag
.String("coreboot_dir", ".", "Resulting coreboot directory")
184 func writeMF(mf
*os
.File
, files
map[string]string, category
string) {
186 for file
, _
:= range files
{
187 keys
= append(keys
, file
)
192 for _
, file
:= range keys
{
193 condition
:= files
[file
]
195 fmt
.Fprintf(mf
, "%s-y += %s\n", category
, file
)
197 fmt
.Fprintf(mf
, "%s-$(%s) += %s\n", category
, condition
, file
)
202 func Create(ctx Context
, name
string) *os
.File
{
203 li
:= strings
.LastIndex(name
, "/")
205 os
.MkdirAll(ctx
.BaseDirectory
+"/"+name
[0:li
], 0700)
207 mf
, err
:= os
.Create(ctx
.BaseDirectory
+ "/" + name
)
214 func Add_gpl(f
*os
.File
) {
215 fmt
.Fprintln(f
, "/* SPDX-License-Identifier: GPL-2.0-only */")
219 func RestorePCI16Simple(f
*os
.File
, pcidev PCIDevData
, addr
uint16) {
220 fmt
.Fprintf(f
, " pci_write_config16(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x);\n",
221 pcidev
.Bus
, pcidev
.Dev
, pcidev
.Func
, addr
,
222 pcidev
.ConfigDump
[addr
+1],
223 pcidev
.ConfigDump
[addr
])
226 func RestorePCI32Simple(f
*os
.File
, pcidev PCIDevData
, addr
uint16) {
227 fmt
.Fprintf(f
, " pci_write_config32(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x%02x%02x);\n",
228 pcidev
.Bus
, pcidev
.Dev
, pcidev
.Func
, addr
,
229 pcidev
.ConfigDump
[addr
+3],
230 pcidev
.ConfigDump
[addr
+2],
231 pcidev
.ConfigDump
[addr
+1],
232 pcidev
.ConfigDump
[addr
])
235 func RestoreRCBA32(f
*os
.File
, inteltool InteltoolData
, addr
uint16) {
236 fmt
.Fprintf(f
, "\tRCBA32(0x%04x) = 0x%08x;\n", addr
, inteltool
.RCBA
[addr
])
239 type PCISlot
struct {
241 additionalComment
string
245 type DevTreeNode
struct {
250 Registers
map[string]string
251 IOs
map[uint16]uint16
252 Children
[]DevTreeNode
263 var DevTree DevTreeNode
264 var MissingChildren
map[string][]DevTreeNode
= map[string][]DevTreeNode
{}
265 var unmatchedPCIChips
map[PCIAddr
]DevTreeNode
= map[PCIAddr
]DevTreeNode
{}
266 var unmatchedPCIDevices
map[PCIAddr
]DevTreeNode
= map[PCIAddr
]DevTreeNode
{}
268 func Offset(dt
*os
.File
, offset
int) {
269 for i
:= 0; i
< offset
; i
++ {
270 fmt
.Fprintf(dt
, "\t")
274 func MatchDev(dev
*DevTreeNode
) {
275 for idx
:= range dev
.Children
{
276 MatchDev(&dev
.Children
[idx
])
279 for _
, slot
:= range dev
.PCISlots
{
280 slotChip
, ok
:= unmatchedPCIChips
[slot
.PCIAddr
]
286 if slot
.additionalComment
!= "" && slotChip
.Comment
!= "" {
287 slotChip
.Comment
= slot
.additionalComment
+ " " + slotChip
.Comment
289 slotChip
.Comment
= slot
.additionalComment
+ slotChip
.Comment
292 delete(unmatchedPCIChips
, slot
.PCIAddr
)
294 dev
.Children
= append(dev
.Children
, slotChip
)
297 if dev
.PCIController
{
298 for slot
, slotDev
:= range unmatchedPCIChips
{
299 if slot
.Bus
== dev
.ChildPCIBus
{
300 delete(unmatchedPCIChips
, slot
)
302 dev
.Children
= append(dev
.Children
, slotDev
)
307 for _
, slot
:= range dev
.PCISlots
{
308 slotDev
, ok
:= unmatchedPCIDevices
[slot
.PCIAddr
]
311 dev
.Children
= append(dev
.Children
,
313 Registers
: map[string]string{},
318 Comment
: slot
.additionalComment
,
326 if slot
.additionalComment
!= "" && slotDev
.Comment
!= "" {
327 slotDev
.Comment
= slot
.additionalComment
+ " " + slotDev
.Comment
329 slotDev
.Comment
= slot
.additionalComment
+ slotDev
.Comment
333 dev
.Children
= append(dev
.Children
, slotDev
)
334 delete(unmatchedPCIDevices
, slot
.PCIAddr
)
337 if dev
.MissingParent
!= "" {
338 for _
, child
:= range MissingChildren
[dev
.MissingParent
] {
340 dev
.Children
= append(dev
.Children
, child
)
342 delete(MissingChildren
, dev
.MissingParent
)
345 if dev
.PCIController
{
346 for slot
, slotDev
:= range unmatchedPCIDevices
{
347 if slot
.Bus
== dev
.ChildPCIBus
{
349 dev
.Children
= append(dev
.Children
, slotDev
)
350 delete(unmatchedPCIDevices
, slot
)
356 func writeOn(dt
*os
.File
, dev DevTreeNode
) {
358 fmt
.Fprintf(dt
, "off")
360 fmt
.Fprintf(dt
, "on")
364 func WriteDev(dt
*os
.File
, offset
int, dev DevTreeNode
) {
367 case "cpu_cluster", "lapic", "domain", "ioapic":
368 fmt
.Fprintf(dt
, "device %s 0x%x ", dev
.Chip
, dev
.Dev
)
371 fmt
.Fprintf(dt
, "device %s %02x.%x ", dev
.Chip
, dev
.Dev
, dev
.Func
)
374 fmt
.Fprintf(dt
, "device %s %02x ", dev
.Chip
, dev
.Dev
)
377 fmt
.Fprintf(dt
, "chip %s", dev
.Chip
)
379 if dev
.Comment
!= "" {
380 fmt
.Fprintf(dt
, " # %s", dev
.Comment
)
382 fmt
.Fprintf(dt
, "\n")
383 if dev
.Chip
== "pci" && dev
.SubSystem
!= 0 && dev
.SubVendor
!= 0 {
385 fmt
.Fprintf(dt
, "subsystemid 0x%04x 0x%04x\n", dev
.SubVendor
, dev
.SubSystem
)
388 ioapic
, ok
:= IOAPICIRQs
[PCIAddr
{Bus
: dev
.Bus
, Dev
: dev
.Dev
, Func
: dev
.Func
}]
389 if dev
.Chip
== "pci" && ok
{
390 for pin
, irq
:= range ioapic
.IRQNO
{
393 fmt
.Fprintf(dt
, "ioapic_irq %d INT%c 0x%x\n", ioapic
.APICID
, 'A'+pin
, irq
)
399 for reg
, _
:= range dev
.Registers
{
400 keys
= append(keys
, reg
)
405 for _
, reg
:= range keys
{
406 val
:= dev
.Registers
[reg
]
408 fmt
.Fprintf(dt
, "register \"%s\" = \"%s\"\n", reg
, val
)
412 for reg
, _
:= range dev
.IOs
{
413 ios
= append(ios
, int(reg
))
418 for _
, reg
:= range ios
{
419 val
:= dev
.IOs
[uint16(reg
)]
421 fmt
.Fprintf(dt
, "io 0x%x = 0x%x\n", reg
, val
)
424 for _
, child
:= range dev
.Children
{
425 WriteDev(dt
, offset
+1, child
)
429 fmt
.Fprintf(dt
, "end\n")
432 func PutChip(domain
string, cur DevTreeNode
) {
433 MissingChildren
[domain
] = append(MissingChildren
[domain
], cur
)
436 func PutPCIChip(addr PCIDevData
, cur DevTreeNode
) {
437 unmatchedPCIChips
[addr
.PCIAddr
] = cur
440 func PutPCIDevParent(addr PCIDevData
, comment
string, parent
string) {
442 Registers
: map[string]string{},
447 MissingParent
: parent
,
450 if addr
.ConfigDump
[0xa] == 0x04 && addr
.ConfigDump
[0xb] == 0x06 {
451 cur
.PCIController
= true
452 cur
.ChildPCIBus
= int(addr
.ConfigDump
[0x19])
455 for capPtr
:= addr
.ConfigDump
[0x34]; capPtr
!= 0; capPtr
= addr
.ConfigDump
[capPtr
+1] {
456 /* Avoid hangs. There are only 0x100 different possible values for capPtr.
457 If we iterate longer than that, we're in endless loop. */
462 if addr
.ConfigDump
[capPtr
] == 0x0d {
463 cur
.SubVendor
= GetLE16(addr
.ConfigDump
[capPtr
+4 : capPtr
+6])
464 cur
.SubSystem
= GetLE16(addr
.ConfigDump
[capPtr
+6 : capPtr
+8])
468 cur
.SubVendor
= GetLE16(addr
.ConfigDump
[0x2c:0x2e])
469 cur
.SubSystem
= GetLE16(addr
.ConfigDump
[0x2e:0x30])
471 unmatchedPCIDevices
[addr
.PCIAddr
] = cur
474 func PutPCIDev(addr PCIDevData
, comment
string) {
475 PutPCIDevParent(addr
, comment
, "")
478 type GenericPCI
struct {
484 type GenericVGA
struct {
488 type DSDTInclude
struct {
493 type DSDTDefine
struct {
499 var DSDTIncludes
[]DSDTInclude
500 var DSDTPCI0Includes
[]DSDTInclude
501 var DSDTDefines
[]DSDTDefine
503 func (g GenericPCI
) Scan(ctx Context
, addr PCIDevData
) {
504 PutPCIDevParent(addr
, g
.Comment
, g
.MissingParent
)
507 var IGDEnabled
bool = false
509 func (g GenericVGA
) Scan(ctx Context
, addr PCIDevData
) {
510 KconfigString
["VGA_BIOS_ID"] = fmt
.Sprintf("%04x,%04x",
513 PutPCIDevParent(addr
, g
.Comment
, g
.MissingParent
)
517 func makeKconfigName(ctx Context
) {
518 kn
:= Create(ctx
, "Kconfig.name")
521 fmt
.Fprintf(kn
, "config %s\n\tbool \"%s\"\n", ctx
.KconfigName
, ctx
.Model
)
524 func makeComment(name
string) string {
525 cmt
, ok
:= KconfigComment
[name
]
532 func makeKconfig(ctx Context
) {
533 kc
:= Create(ctx
, "Kconfig")
536 fmt
.Fprintf(kc
, "if %s\n\n", ctx
.KconfigName
)
538 fmt
.Fprintf(kc
, "config BOARD_SPECIFIC_OPTIONS\n\tdef_bool y\n")
540 for name
, val
:= range KconfigBool
{
542 keys
= append(keys
, name
)
548 for _
, name
:= range keys
{
549 fmt
.Fprintf(kc
, "\tselect %s%s\n", name
, makeComment(name
))
553 for name
, val
:= range KconfigBool
{
555 keys
= append(keys
, name
)
561 for _
, name
:= range keys
{
566 `, name
, makeComment(name
))
570 for name
, _
:= range KconfigString
{
571 keys
= append(keys
, name
)
576 for _
, name
:= range keys
{
581 `, name
, makeComment(name
), KconfigString
[name
])
585 for name
, _
:= range KconfigHex
{
586 keys
= append(keys
, name
)
591 for _
, name
:= range keys
{
596 `, name
, makeComment(name
), KconfigHex
[name
])
600 for name
, _
:= range KconfigInt
{
601 keys
= append(keys
, name
)
606 for _
, name
:= range keys
{
611 `, name
, makeComment(name
), KconfigInt
[name
])
614 fmt
.Fprintf(kc
, "endif\n")
617 const MoboDir
= "/src/mainboard/"
619 func makeVendor(ctx Context
) {
621 vendorSane
:= ctx
.SaneVendor
622 vendorDir
:= *FlagOutDir
+ MoboDir
+ vendorSane
623 vendorUpper
:= strings
.ToUpper(vendorSane
)
624 kconfig
:= vendorDir
+ "/Kconfig"
625 if _
, err
:= os
.Stat(kconfig
); os
.IsNotExist(err
) {
626 f
, err
:= os
.Create(kconfig
)
631 f
.WriteString(`if VENDOR_` + vendorUpper
+ `
634 prompt "Mainboard model"
636 source "src/mainboard/` + vendorSane
+ `/*/Kconfig.name"
640 source "src/mainboard/` + vendorSane
+ `/*/Kconfig"
642 config MAINBOARD_VENDOR
644 default "` + vendor
+ `"
646 endif # VENDOR_` + vendorUpper
+ "\n")
648 kconfigName
:= vendorDir
+ "/Kconfig.name"
649 if _
, err
:= os
.Stat(kconfigName
); os
.IsNotExist(err
) {
650 f
, err
:= os
.Create(kconfigName
)
655 f
.WriteString(`config VENDOR_` + vendorUpper
+ `
656 bool "` + vendor
+ `"
662 func GuessECGPE(ctx Context
) int {
663 /* FIXME:XX Use iasl -d and/or better parsing */
664 dsdt
:= ctx
.InfoSource
.GetACPI()["DSDT"]
665 idx
:= bytes
.Index(dsdt
, []byte{0x08, '_', 'G', 'P', 'E', 0x0a}) /* Name (_GPE, byte). */
667 return int(dsdt
[idx
+6])
672 func GuessSPDMap(ctx Context
) []uint8 {
673 dmi
:= ctx
.InfoSource
.GetDMI()
675 if dmi
.Vendor
== "LENOVO" {
676 return []uint8{0x50, 0x52, 0x51, 0x53}
678 return []uint8{0x50, 0x51, 0x52, 0x53}
686 ctx
.InfoSource
= MakeLogReader()
688 dmi
:= ctx
.InfoSource
.GetDMI()
690 ctx
.Vendor
= dmi
.Vendor
692 if dmi
.Vendor
== "LENOVO" {
693 ctx
.Model
= dmi
.Version
695 ctx
.Model
= dmi
.Model
699 KconfigBool
["SYSTEM_TYPE_LAPTOP"] = true
701 ctx
.SaneVendor
= sanitize(ctx
.Vendor
)
703 last
:= ctx
.SaneVendor
704 for _
, suf
:= range []string{"_inc", "_co", "_corp"} {
705 ctx
.SaneVendor
= strings
.TrimSuffix(ctx
.SaneVendor
, suf
)
707 if last
== ctx
.SaneVendor
{
711 ctx
.MoboID
= ctx
.SaneVendor
+ "/" + sanitize(ctx
.Model
)
712 ctx
.KconfigName
= "BOARD_" + strings
.ToUpper(ctx
.SaneVendor
+"_"+sanitize(ctx
.Model
))
713 ctx
.BaseDirectory
= *FlagOutDir
+ MoboDir
+ ctx
.MoboID
714 KconfigString
["MAINBOARD_DIR"] = ctx
.MoboID
715 KconfigString
["MAINBOARD_PART_NUMBER"] = ctx
.Model
717 os
.MkdirAll(ctx
.BaseDirectory
, 0700)
724 KconfigBool
["MAINBOARD_HAS_LIBGFXINIT"] = true
725 KconfigComment
["MAINBOARD_HAS_LIBGFXINIT"] = "FIXME: check this"
726 AddRAMStageFile("gma-mainboard.ads", "CONFIG_MAINBOARD_USE_LIBGFXINIT")
729 if len(BootBlockFiles
) > 0 ||
len(ROMStageFiles
) > 0 ||
len(RAMStageFiles
) > 0 ||
len(SMMFiles
) > 0 {
730 mf
:= Create(ctx
, "Makefile.inc")
732 writeMF(mf
, BootBlockFiles
, "bootblock")
733 writeMF(mf
, ROMStageFiles
, "romstage")
734 writeMF(mf
, RAMStageFiles
, "ramstage")
735 writeMF(mf
, SMMFiles
, "smm")
738 devtree
:= Create(ctx
, "devicetree.cb")
739 defer devtree
.Close()
742 WriteDev(devtree
, 0, DevTree
)
744 if MainboardInit
!= "" || MainboardEnable
!= "" || MainboardIncludes
!= nil {
745 mainboard
:= Create(ctx
, "mainboard.c")
746 defer mainboard
.Close()
748 mainboard
.WriteString("#include <device/device.h>\n")
749 for _
, include
:= range MainboardIncludes
{
750 mainboard
.WriteString("#include <" + include
+ ">\n")
752 mainboard
.WriteString("\n")
753 if MainboardInit
!= "" {
754 mainboard
.WriteString(`static void mainboard_init(struct device *dev)
756 ` + MainboardInit
+ "}\n\n")
758 if MainboardInit
!= "" || MainboardEnable
!= "" {
759 mainboard
.WriteString("static void mainboard_enable(struct device *dev)\n{\n")
760 if MainboardInit
!= "" {
761 mainboard
.WriteString("\tdev->ops->init = mainboard_init;\n\n")
763 mainboard
.WriteString(MainboardEnable
)
764 mainboard
.WriteString("}\n\n")
765 mainboard
.WriteString(`struct chip_operations mainboard_ops = {
766 .enable_dev = mainboard_enable,
772 bi
:= Create(ctx
, "board_info.txt")
778 bi
.WriteString("Category: laptop\n")
780 bi
.WriteString("Category: desktop\n")
781 fixme
+= "check category, "
784 missing
:= "ROM package, ROM socketed"
786 if ROMProtocol
!= "" {
787 fmt
.Fprintf(bi
, "ROM protocol: %s\n", ROMProtocol
)
789 missing
+= ", ROM protocol"
792 if FlashROMSupport
!= "" {
793 fmt
.Fprintf(bi
, "Flashrom support: %s\n", FlashROMSupport
)
795 missing
+= ", Flashrom support"
798 missing
+= ", Release year"
801 fmt
.Fprintf(bi
, "FIXME: %s, put %s\n", fixme
, missing
)
803 fmt
.Fprintf(bi
, "FIXME: put %s\n", missing
)
807 KconfigBool
["BOARD_ROMSIZE_KB_2048"] = true
808 KconfigComment
["BOARD_ROMSIZE_KB_2048"] = "FIXME: correct this"
810 KconfigBool
[fmt
.Sprintf("BOARD_ROMSIZE_KB_%d", ROMSizeKB
)] = true
816 dsdt
:= Create(ctx
, "dsdt.asl")
819 for _
, define
:= range DSDTDefines
{
820 if define
.Comment
!= "" {
821 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", define
.Comment
)
823 dsdt
.WriteString("#define " + define
.Key
+ " " + define
.Value
+ "\n")
829 #include <acpi/acpi.h>
837 0x20141018 /* OEM revision */
840 #include <acpi/dsdt_top.asl>
841 #include "acpi/platform.asl"
844 for _
, x
:= range DSDTIncludes
{
846 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", x
.Comment
)
848 fmt
.Fprintf(dsdt
, "\t#include <%s>\n", x
.File
)
855 for _
, x
:= range DSDTPCI0Includes
{
857 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", x
.Comment
)
859 fmt
.Fprintf(dsdt
, "\t\t#include <%s>\n", x
.File
)
867 gma
:= Create(ctx
, "gma-mainboard.ads")
870 gma
.WriteString(`-- SPDX-License-Identifier: GPL-2.0-or-later
873 with HW.GFX.GMA.Display_Probing;
876 use HW.GFX.GMA.Display_Probing;
878 private package GMA.Mainboard is
881 ports : constant Port_List :=