Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 1 | /* This is just an experiment. Full automatic porting |
| 2 | is probably not possible but a lot can be automated. */ |
| 3 | package main |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "flag" |
| 8 | "fmt" |
| 9 | "log" |
| 10 | "os" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | type PCIAddr struct { |
| 16 | Bus int |
| 17 | Dev int |
| 18 | Func int |
| 19 | } |
| 20 | |
| 21 | type PCIDevData struct { |
| 22 | PCIAddr |
| 23 | PCIVenID uint16 |
| 24 | PCIDevID uint16 |
| 25 | ConfigDump []uint8 |
| 26 | } |
| 27 | |
| 28 | type PCIDevice interface { |
| 29 | Scan(ctx Context, addr PCIDevData) |
| 30 | } |
| 31 | |
| 32 | type InteltoolData struct { |
| 33 | GPIO map[uint16]uint32 |
| 34 | RCBA map[uint16]uint32 |
| 35 | IGD map[uint32]uint32 |
| 36 | } |
| 37 | |
| 38 | type DMIData struct { |
| 39 | Vendor string |
| 40 | Model string |
| 41 | Version string |
| 42 | IsLaptop bool |
| 43 | } |
| 44 | |
| 45 | type AzaliaCodec struct { |
| 46 | Name string |
| 47 | VendorID uint32 |
| 48 | SubsystemID uint32 |
| 49 | CodecNo int |
| 50 | PinConfig map[int]uint32 |
| 51 | } |
| 52 | |
| 53 | type DevReader interface { |
| 54 | GetPCIList() []PCIDevData |
| 55 | GetDMI() DMIData |
| 56 | GetInteltool() InteltoolData |
| 57 | GetAzaliaCodecs() []AzaliaCodec |
| 58 | GetACPI() map[string][]byte |
| 59 | GetCPUModel() []uint32 |
| 60 | GetEC() []byte |
| 61 | GetIOPorts() []IOPorts |
Vladimir Serbinenko | 91195c6 | 2015-05-29 23:53:37 +0200 | [diff] [blame] | 62 | HasPS2() bool |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | type IOPorts struct { |
| 66 | Start uint16 |
| 67 | End uint16 |
| 68 | Usage string |
| 69 | } |
| 70 | |
| 71 | type SouthBridger interface { |
| 72 | GetGPIOHeader() string |
| 73 | EncodeGPE(int) int |
| 74 | DecodeGPE(int) int |
| 75 | EnableGPE(int) |
| 76 | NeedRouteGPIOManually() |
| 77 | } |
| 78 | |
| 79 | var SouthBridge SouthBridger |
Angel Pons | 6779d23 | 2020-01-08 15:05:56 +0100 | [diff] [blame] | 80 | var BootBlockFiles map[string]string = map[string]string{} |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 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 |
| 87 | |
| 88 | type Context struct { |
| 89 | MoboID string |
| 90 | KconfigName string |
| 91 | Vendor string |
| 92 | Model string |
| 93 | BaseDirectory string |
| 94 | InfoSource DevReader |
Vladimir Serbinenko | 2d615e5 | 2015-05-29 21:43:51 +0200 | [diff] [blame] | 95 | SaneVendor string |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | type IOAPICIRQ struct { |
| 99 | APICID int |
| 100 | IRQNO [4]int |
| 101 | } |
| 102 | |
| 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{} |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 107 | var KconfigHex map[string]uint32 = map[string]uint32{} |
| 108 | var KconfigInt map[string]int = map[string]int{} |
| 109 | var ROMSizeKB = 0 |
| 110 | var ROMProtocol = "" |
| 111 | var FlashROMSupport = "" |
| 112 | |
| 113 | func GetLE16(inp []byte) uint16 { |
| 114 | return uint16(inp[0]) | (uint16(inp[1]) << 8) |
| 115 | } |
| 116 | |
| 117 | func FormatHexLE16(inp []byte) string { |
| 118 | return fmt.Sprintf("0x%04x", GetLE16(inp)) |
| 119 | } |
| 120 | |
| 121 | func FormatHex32(u uint32) string { |
| 122 | return fmt.Sprintf("0x%08x", u) |
| 123 | } |
| 124 | |
| 125 | func FormatHex8(u uint8) string { |
| 126 | return fmt.Sprintf("0x%02x", u) |
| 127 | } |
| 128 | |
| 129 | func FormatInt32(u uint32) string { |
| 130 | return fmt.Sprintf("%d", u) |
| 131 | } |
| 132 | |
| 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) |
| 136 | } |
| 137 | |
| 138 | func FormatBool(inp bool) string { |
| 139 | if inp { |
| 140 | return "1" |
| 141 | } else { |
| 142 | return "0" |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func sanitize(inp string) string { |
| 147 | result := strings.ToLower(inp) |
| 148 | result = strings.Replace(result, " ", "_", -1) |
| 149 | result = strings.Replace(result, ",", "_", -1) |
Arthur Heymans | c8c3aca | 2018-01-18 22:20:25 +0100 | [diff] [blame] | 150 | result = strings.Replace(result, "-", "_", -1) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 151 | for strings.HasSuffix(result, ".") { |
| 152 | result = result[0 : len(result)-1] |
| 153 | } |
| 154 | return result |
| 155 | } |
| 156 | |
Angel Pons | 6779d23 | 2020-01-08 15:05:56 +0100 | [diff] [blame] | 157 | func AddBootBlockFile(Name string, Condition string) { |
| 158 | BootBlockFiles[Name] = Condition |
| 159 | } |
| 160 | |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 161 | func AddROMStageFile(Name string, Condition string) { |
| 162 | ROMStageFiles[Name] = Condition |
| 163 | } |
| 164 | |
| 165 | func AddRAMStageFile(Name string, Condition string) { |
| 166 | RAMStageFiles[Name] = Condition |
| 167 | } |
| 168 | |
| 169 | func AddSMMFile(Name string, Condition string) { |
| 170 | SMMFiles[Name] = Condition |
| 171 | } |
| 172 | |
| 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 { |
| 176 | return true |
| 177 | } |
| 178 | } |
| 179 | return false |
| 180 | } |
| 181 | |
| 182 | var FlagOutDir = flag.String("coreboot_dir", ".", "Resulting coreboot directory") |
| 183 | |
| 184 | func writeMF(mf *os.File, files map[string]string, category string) { |
| 185 | keys := []string{} |
| 186 | for file, _ := range files { |
| 187 | keys = append(keys, file) |
| 188 | } |
| 189 | |
| 190 | sort.Strings(keys) |
| 191 | |
| 192 | for _, file := range keys { |
| 193 | condition := files[file] |
| 194 | if condition == "" { |
| 195 | fmt.Fprintf(mf, "%s-y += %s\n", category, file) |
| 196 | } else { |
Angel Pons | 6779d23 | 2020-01-08 15:05:56 +0100 | [diff] [blame] | 197 | fmt.Fprintf(mf, "%s-$(%s) += %s\n", category, condition, file) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func Create(ctx Context, name string) *os.File { |
| 203 | li := strings.LastIndex(name, "/") |
| 204 | if li > 0 { |
| 205 | os.MkdirAll(ctx.BaseDirectory+"/"+name[0:li], 0700) |
| 206 | } |
| 207 | mf, err := os.Create(ctx.BaseDirectory + "/" + name) |
| 208 | if err != nil { |
| 209 | log.Fatal(err) |
| 210 | } |
| 211 | return mf |
| 212 | } |
| 213 | |
Angel Pons | 3d5d6e8 | 2020-03-18 23:55:36 +0100 | [diff] [blame] | 214 | func Add_gpl(f *os.File) { |
| 215 | fmt.Fprintln(f, "/* SPDX-License-Identifier: GPL-2.0-only */") |
Angel Pons | 3d5d6e8 | 2020-03-18 23:55:36 +0100 | [diff] [blame] | 216 | fmt.Fprintln(f) |
Arthur Heymans | 5930285 | 2017-05-01 10:33:56 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 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]) |
| 224 | } |
| 225 | |
| 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]) |
| 233 | } |
| 234 | |
| 235 | func RestoreRCBA32(f *os.File, inteltool InteltoolData, addr uint16) { |
| 236 | fmt.Fprintf(f, "\tRCBA32(0x%04x) = 0x%08x;\n", addr, inteltool.RCBA[addr]) |
| 237 | } |
| 238 | |
| 239 | type PCISlot struct { |
| 240 | PCIAddr |
| 241 | additionalComment string |
| 242 | writeEmpty bool |
| 243 | } |
| 244 | |
| 245 | type DevTreeNode struct { |
| 246 | Bus int |
| 247 | Dev int |
| 248 | Func int |
| 249 | Disabled bool |
| 250 | Registers map[string]string |
| 251 | IOs map[uint16]uint16 |
| 252 | Children []DevTreeNode |
| 253 | PCISlots []PCISlot |
| 254 | PCIController bool |
| 255 | ChildPCIBus int |
| 256 | MissingParent string |
| 257 | SubVendor uint16 |
| 258 | SubSystem uint16 |
| 259 | Chip string |
| 260 | Comment string |
| 261 | } |
| 262 | |
| 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{} |
| 267 | |
| 268 | func Offset(dt *os.File, offset int) { |
| 269 | for i := 0; i < offset; i++ { |
| 270 | fmt.Fprintf(dt, "\t") |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | func MatchDev(dev *DevTreeNode) { |
| 275 | for idx := range dev.Children { |
| 276 | MatchDev(&dev.Children[idx]) |
| 277 | } |
| 278 | |
| 279 | for _, slot := range dev.PCISlots { |
| 280 | slotChip, ok := unmatchedPCIChips[slot.PCIAddr] |
| 281 | |
| 282 | if !ok { |
| 283 | continue |
| 284 | } |
| 285 | |
| 286 | if slot.additionalComment != "" && slotChip.Comment != "" { |
| 287 | slotChip.Comment = slot.additionalComment + " " + slotChip.Comment |
| 288 | } else { |
| 289 | slotChip.Comment = slot.additionalComment + slotChip.Comment |
| 290 | } |
| 291 | |
| 292 | delete(unmatchedPCIChips, slot.PCIAddr) |
| 293 | MatchDev(&slotChip) |
| 294 | dev.Children = append(dev.Children, slotChip) |
| 295 | } |
| 296 | |
| 297 | if dev.PCIController { |
| 298 | for slot, slotDev := range unmatchedPCIChips { |
| 299 | if slot.Bus == dev.ChildPCIBus { |
| 300 | delete(unmatchedPCIChips, slot) |
| 301 | MatchDev(&slotDev) |
| 302 | dev.Children = append(dev.Children, slotDev) |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | for _, slot := range dev.PCISlots { |
| 308 | slotDev, ok := unmatchedPCIDevices[slot.PCIAddr] |
| 309 | if !ok { |
| 310 | if slot.writeEmpty { |
| 311 | dev.Children = append(dev.Children, |
| 312 | DevTreeNode{ |
| 313 | Registers: map[string]string{}, |
| 314 | Chip: "pci", |
| 315 | Bus: slot.Bus, |
| 316 | Dev: slot.Dev, |
| 317 | Func: slot.Func, |
| 318 | Comment: slot.additionalComment, |
| 319 | Disabled: true, |
| 320 | }, |
| 321 | ) |
| 322 | } |
| 323 | continue |
| 324 | } |
| 325 | |
| 326 | if slot.additionalComment != "" && slotDev.Comment != "" { |
| 327 | slotDev.Comment = slot.additionalComment + " " + slotDev.Comment |
| 328 | } else { |
| 329 | slotDev.Comment = slot.additionalComment + slotDev.Comment |
| 330 | } |
| 331 | |
| 332 | MatchDev(&slotDev) |
| 333 | dev.Children = append(dev.Children, slotDev) |
| 334 | delete(unmatchedPCIDevices, slot.PCIAddr) |
| 335 | } |
| 336 | |
| 337 | if dev.MissingParent != "" { |
| 338 | for _, child := range MissingChildren[dev.MissingParent] { |
| 339 | MatchDev(&child) |
| 340 | dev.Children = append(dev.Children, child) |
| 341 | } |
| 342 | delete(MissingChildren, dev.MissingParent) |
| 343 | } |
| 344 | |
| 345 | if dev.PCIController { |
| 346 | for slot, slotDev := range unmatchedPCIDevices { |
| 347 | if slot.Bus == dev.ChildPCIBus { |
| 348 | MatchDev(&slotDev) |
| 349 | dev.Children = append(dev.Children, slotDev) |
| 350 | delete(unmatchedPCIDevices, slot) |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | func writeOn(dt *os.File, dev DevTreeNode) { |
| 357 | if dev.Disabled { |
| 358 | fmt.Fprintf(dt, "off") |
| 359 | } else { |
| 360 | fmt.Fprintf(dt, "on") |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | func WriteDev(dt *os.File, offset int, dev DevTreeNode) { |
| 365 | Offset(dt, offset) |
| 366 | switch dev.Chip { |
| 367 | case "cpu_cluster", "lapic", "domain", "ioapic": |
| 368 | fmt.Fprintf(dt, "device %s 0x%x ", dev.Chip, dev.Dev) |
| 369 | writeOn(dt, dev) |
| 370 | case "pci", "pnp": |
| 371 | fmt.Fprintf(dt, "device %s %02x.%x ", dev.Chip, dev.Dev, dev.Func) |
| 372 | writeOn(dt, dev) |
| 373 | case "i2c": |
| 374 | fmt.Fprintf(dt, "device %s %02x ", dev.Chip, dev.Dev) |
| 375 | writeOn(dt, dev) |
| 376 | default: |
| 377 | fmt.Fprintf(dt, "chip %s", dev.Chip) |
| 378 | } |
| 379 | if dev.Comment != "" { |
| 380 | fmt.Fprintf(dt, " # %s", dev.Comment) |
| 381 | } |
| 382 | fmt.Fprintf(dt, "\n") |
| 383 | if dev.Chip == "pci" && dev.SubSystem != 0 && dev.SubVendor != 0 { |
| 384 | Offset(dt, offset+1) |
| 385 | fmt.Fprintf(dt, "subsystemid 0x%04x 0x%04x\n", dev.SubVendor, dev.SubSystem) |
| 386 | } |
| 387 | |
| 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 { |
| 391 | if irq != 0 { |
| 392 | Offset(dt, offset+1) |
| 393 | fmt.Fprintf(dt, "ioapic_irq %d INT%c 0x%x\n", ioapic.APICID, 'A'+pin, irq) |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | keys := []string{} |
| 399 | for reg, _ := range dev.Registers { |
| 400 | keys = append(keys, reg) |
| 401 | } |
| 402 | |
| 403 | sort.Strings(keys) |
| 404 | |
| 405 | for _, reg := range keys { |
| 406 | val := dev.Registers[reg] |
| 407 | Offset(dt, offset+1) |
| 408 | fmt.Fprintf(dt, "register \"%s\" = \"%s\"\n", reg, val) |
| 409 | } |
| 410 | |
| 411 | ios := []int{} |
| 412 | for reg, _ := range dev.IOs { |
| 413 | ios = append(ios, int(reg)) |
| 414 | } |
| 415 | |
| 416 | sort.Ints(ios) |
| 417 | |
| 418 | for _, reg := range ios { |
| 419 | val := dev.IOs[uint16(reg)] |
| 420 | Offset(dt, offset+1) |
| 421 | fmt.Fprintf(dt, "io 0x%x = 0x%x\n", reg, val) |
| 422 | } |
| 423 | |
| 424 | for _, child := range dev.Children { |
| 425 | WriteDev(dt, offset+1, child) |
| 426 | } |
| 427 | |
| 428 | Offset(dt, offset) |
| 429 | fmt.Fprintf(dt, "end\n") |
| 430 | } |
| 431 | |
| 432 | func PutChip(domain string, cur DevTreeNode) { |
| 433 | MissingChildren[domain] = append(MissingChildren[domain], cur) |
| 434 | } |
| 435 | |
| 436 | func PutPCIChip(addr PCIDevData, cur DevTreeNode) { |
| 437 | unmatchedPCIChips[addr.PCIAddr] = cur |
| 438 | } |
| 439 | |
| 440 | func PutPCIDevParent(addr PCIDevData, comment string, parent string) { |
| 441 | cur := DevTreeNode{ |
| 442 | Registers: map[string]string{}, |
| 443 | Chip: "pci", |
| 444 | Bus: addr.Bus, |
| 445 | Dev: addr.Dev, |
| 446 | Func: addr.Func, |
| 447 | MissingParent: parent, |
| 448 | Comment: comment, |
| 449 | } |
| 450 | if addr.ConfigDump[0xa] == 0x04 && addr.ConfigDump[0xb] == 0x06 { |
| 451 | cur.PCIController = true |
| 452 | cur.ChildPCIBus = int(addr.ConfigDump[0x19]) |
| 453 | |
| 454 | loopCtr := 0 |
| 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. */ |
| 458 | loopCtr++ |
| 459 | if loopCtr > 0x100 { |
| 460 | break |
| 461 | } |
| 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]) |
| 465 | } |
| 466 | } |
| 467 | } else { |
| 468 | cur.SubVendor = GetLE16(addr.ConfigDump[0x2c:0x2e]) |
| 469 | cur.SubSystem = GetLE16(addr.ConfigDump[0x2e:0x30]) |
| 470 | } |
| 471 | unmatchedPCIDevices[addr.PCIAddr] = cur |
| 472 | } |
| 473 | |
| 474 | func PutPCIDev(addr PCIDevData, comment string) { |
| 475 | PutPCIDevParent(addr, comment, "") |
| 476 | } |
| 477 | |
| 478 | type GenericPCI struct { |
| 479 | Comment string |
| 480 | Bus0Subdiv string |
| 481 | MissingParent string |
| 482 | } |
| 483 | |
| 484 | type GenericVGA struct { |
| 485 | GenericPCI |
| 486 | } |
| 487 | |
| 488 | type DSDTInclude struct { |
| 489 | Comment string |
| 490 | File string |
| 491 | } |
| 492 | |
| 493 | type DSDTDefine struct { |
| 494 | Key string |
| 495 | Comment string |
| 496 | Value string |
| 497 | } |
| 498 | |
| 499 | var DSDTIncludes []DSDTInclude |
| 500 | var DSDTPCI0Includes []DSDTInclude |
| 501 | var DSDTDefines []DSDTDefine |
| 502 | |
| 503 | func (g GenericPCI) Scan(ctx Context, addr PCIDevData) { |
| 504 | PutPCIDevParent(addr, g.Comment, g.MissingParent) |
| 505 | } |
| 506 | |
Iru Cai | cd980ab | 2019-01-14 20:38:16 +0800 | [diff] [blame] | 507 | var IGDEnabled bool = false |
| 508 | |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 509 | func (g GenericVGA) Scan(ctx Context, addr PCIDevData) { |
| 510 | KconfigString["VGA_BIOS_ID"] = fmt.Sprintf("%04x,%04x", |
| 511 | addr.PCIVenID, |
| 512 | addr.PCIDevID) |
| 513 | KconfigString["VGA_BIOS_FILE"] = fmt.Sprintf("pci%04x,%04x.rom", |
| 514 | addr.PCIVenID, |
| 515 | addr.PCIDevID) |
| 516 | PutPCIDevParent(addr, g.Comment, g.MissingParent) |
Iru Cai | cd980ab | 2019-01-14 20:38:16 +0800 | [diff] [blame] | 517 | IGDEnabled = true |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | func makeKconfigName(ctx Context) { |
| 521 | kn := Create(ctx, "Kconfig.name") |
| 522 | defer kn.Close() |
| 523 | |
| 524 | fmt.Fprintf(kn, "config %s\n\tbool \"%s\"\n", ctx.KconfigName, ctx.Model) |
| 525 | } |
| 526 | |
| 527 | func makeComment(name string) string { |
| 528 | cmt, ok := KconfigComment[name] |
| 529 | if !ok { |
| 530 | return "" |
| 531 | } |
| 532 | return " # " + cmt |
| 533 | } |
| 534 | |
| 535 | func makeKconfig(ctx Context) { |
| 536 | kc := Create(ctx, "Kconfig") |
| 537 | defer kc.Close() |
| 538 | |
| 539 | fmt.Fprintf(kc, "if %s\n\n", ctx.KconfigName) |
| 540 | |
Elyes HAOUAS | f0c5be2 | 2018-11-27 20:36:44 +0100 | [diff] [blame] | 541 | fmt.Fprintf(kc, "config BOARD_SPECIFIC_OPTIONS\n\tdef_bool y\n") |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 542 | keys := []string{} |
| 543 | for name, val := range KconfigBool { |
| 544 | if val { |
| 545 | keys = append(keys, name) |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | sort.Strings(keys) |
| 550 | |
| 551 | for _, name := range keys { |
| 552 | fmt.Fprintf(kc, "\tselect %s%s\n", name, makeComment(name)) |
| 553 | } |
| 554 | |
| 555 | keys = nil |
| 556 | for name, val := range KconfigBool { |
| 557 | if !val { |
| 558 | keys = append(keys, name) |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | sort.Strings(keys) |
| 563 | |
| 564 | for _, name := range keys { |
| 565 | fmt.Fprintf(kc, ` |
| 566 | config %s%s |
| 567 | bool |
| 568 | default n |
| 569 | `, name, makeComment(name)) |
| 570 | } |
| 571 | |
| 572 | keys = nil |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 573 | for name, _ := range KconfigString { |
| 574 | keys = append(keys, name) |
| 575 | } |
| 576 | |
| 577 | sort.Strings(keys) |
| 578 | |
| 579 | for _, name := range keys { |
| 580 | fmt.Fprintf(kc, ` |
| 581 | config %s%s |
| 582 | string |
| 583 | default "%s" |
| 584 | `, name, makeComment(name), KconfigString[name]) |
| 585 | } |
| 586 | |
| 587 | keys = nil |
| 588 | for name, _ := range KconfigHex { |
| 589 | keys = append(keys, name) |
| 590 | } |
| 591 | |
| 592 | sort.Strings(keys) |
| 593 | |
| 594 | for _, name := range keys { |
| 595 | fmt.Fprintf(kc, ` |
| 596 | config %s%s |
| 597 | hex |
| 598 | default 0x%x |
| 599 | `, name, makeComment(name), KconfigHex[name]) |
| 600 | } |
| 601 | |
| 602 | keys = nil |
| 603 | for name, _ := range KconfigInt { |
| 604 | keys = append(keys, name) |
| 605 | } |
| 606 | |
| 607 | sort.Strings(keys) |
| 608 | |
| 609 | for _, name := range keys { |
| 610 | fmt.Fprintf(kc, ` |
| 611 | config %s%s |
| 612 | int |
| 613 | default %d |
| 614 | `, name, makeComment(name), KconfigInt[name]) |
| 615 | } |
| 616 | |
| 617 | fmt.Fprintf(kc, "endif\n") |
| 618 | } |
| 619 | |
| 620 | const MoboDir = "/src/mainboard/" |
| 621 | |
| 622 | func makeVendor(ctx Context) { |
| 623 | vendor := ctx.Vendor |
Vladimir Serbinenko | 2d615e5 | 2015-05-29 21:43:51 +0200 | [diff] [blame] | 624 | vendorSane := ctx.SaneVendor |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 625 | vendorDir := *FlagOutDir + MoboDir + vendorSane |
| 626 | vendorUpper := strings.ToUpper(vendorSane) |
| 627 | kconfig := vendorDir + "/Kconfig" |
| 628 | if _, err := os.Stat(kconfig); os.IsNotExist(err) { |
| 629 | f, err := os.Create(kconfig) |
| 630 | if err != nil { |
| 631 | log.Fatal(err) |
| 632 | } |
| 633 | defer f.Close() |
| 634 | f.WriteString(`if VENDOR_` + vendorUpper + ` |
| 635 | |
| 636 | choice |
| 637 | prompt "Mainboard model" |
| 638 | |
| 639 | source "src/mainboard/` + vendorSane + `/*/Kconfig.name" |
| 640 | |
| 641 | endchoice |
| 642 | |
| 643 | source "src/mainboard/` + vendorSane + `/*/Kconfig" |
| 644 | |
| 645 | config MAINBOARD_VENDOR |
| 646 | string |
| 647 | default "` + vendor + `" |
| 648 | |
| 649 | endif # VENDOR_` + vendorUpper + "\n") |
| 650 | } |
| 651 | kconfigName := vendorDir + "/Kconfig.name" |
| 652 | if _, err := os.Stat(kconfigName); os.IsNotExist(err) { |
| 653 | f, err := os.Create(kconfigName) |
| 654 | if err != nil { |
| 655 | log.Fatal(err) |
| 656 | } |
| 657 | defer f.Close() |
| 658 | f.WriteString(`config VENDOR_` + vendorUpper + ` |
| 659 | bool "` + vendor + `" |
| 660 | `) |
| 661 | } |
| 662 | |
| 663 | } |
| 664 | |
| 665 | func GuessECGPE(ctx Context) int { |
| 666 | /* FIXME:XX Use iasl -d and/or better parsing */ |
| 667 | dsdt := ctx.InfoSource.GetACPI()["DSDT"] |
| 668 | idx := bytes.Index(dsdt, []byte{0x08, '_', 'G', 'P', 'E', 0x0a}) /* Name (_GPE, byte). */ |
| 669 | if idx > 0 { |
| 670 | return int(dsdt[idx+6]) |
| 671 | } |
| 672 | return -1 |
| 673 | } |
| 674 | |
| 675 | func GuessSPDMap(ctx Context) []uint8 { |
| 676 | dmi := ctx.InfoSource.GetDMI() |
| 677 | |
| 678 | if dmi.Vendor == "LENOVO" { |
Vladimir Serbinenko | db0acf8 | 2015-05-29 22:09:06 +0200 | [diff] [blame] | 679 | return []uint8{0x50, 0x52, 0x51, 0x53} |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 680 | } |
Vladimir Serbinenko | db0acf8 | 2015-05-29 22:09:06 +0200 | [diff] [blame] | 681 | return []uint8{0x50, 0x51, 0x52, 0x53} |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | func main() { |
| 685 | flag.Parse() |
| 686 | |
| 687 | ctx := Context{} |
| 688 | |
| 689 | ctx.InfoSource = MakeLogReader() |
| 690 | |
| 691 | dmi := ctx.InfoSource.GetDMI() |
| 692 | |
| 693 | ctx.Vendor = dmi.Vendor |
| 694 | |
| 695 | if dmi.Vendor == "LENOVO" { |
| 696 | ctx.Model = dmi.Version |
| 697 | } else { |
| 698 | ctx.Model = dmi.Model |
| 699 | } |
| 700 | |
| 701 | if dmi.IsLaptop { |
| 702 | KconfigBool["SYSTEM_TYPE_LAPTOP"] = true |
| 703 | } |
Vladimir Serbinenko | 2d615e5 | 2015-05-29 21:43:51 +0200 | [diff] [blame] | 704 | ctx.SaneVendor = sanitize(ctx.Vendor) |
| 705 | for { |
| 706 | last := ctx.SaneVendor |
| 707 | for _, suf := range []string{"_inc", "_co", "_corp"} { |
| 708 | ctx.SaneVendor = strings.TrimSuffix(ctx.SaneVendor, suf) |
| 709 | } |
| 710 | if last == ctx.SaneVendor { |
| 711 | break |
| 712 | } |
| 713 | } |
| 714 | ctx.MoboID = ctx.SaneVendor + "/" + sanitize(ctx.Model) |
| 715 | ctx.KconfigName = "BOARD_" + strings.ToUpper(ctx.SaneVendor+"_"+sanitize(ctx.Model)) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 716 | ctx.BaseDirectory = *FlagOutDir + MoboDir + ctx.MoboID |
Iru Cai | 8c6d161 | 2020-09-07 20:21:16 +0800 | [diff] [blame] | 717 | KconfigString["MAINBOARD_DIR"] = ctx.MoboID |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 718 | KconfigString["MAINBOARD_PART_NUMBER"] = ctx.Model |
| 719 | |
| 720 | os.MkdirAll(ctx.BaseDirectory, 0700) |
| 721 | |
| 722 | makeVendor(ctx) |
| 723 | |
| 724 | ScanRoot(ctx) |
| 725 | |
Iru Cai | cd980ab | 2019-01-14 20:38:16 +0800 | [diff] [blame] | 726 | if IGDEnabled { |
| 727 | KconfigBool["MAINBOARD_HAS_LIBGFXINIT"] = true |
| 728 | KconfigComment["MAINBOARD_HAS_LIBGFXINIT"] = "FIXME: check this" |
| 729 | AddRAMStageFile("gma-mainboard.ads", "CONFIG_MAINBOARD_USE_LIBGFXINIT") |
| 730 | } |
| 731 | |
Angel Pons | 6779d23 | 2020-01-08 15:05:56 +0100 | [diff] [blame] | 732 | if len(BootBlockFiles) > 0 || len(ROMStageFiles) > 0 || len(RAMStageFiles) > 0 || len(SMMFiles) > 0 { |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 733 | mf := Create(ctx, "Makefile.inc") |
| 734 | defer mf.Close() |
Angel Pons | 6779d23 | 2020-01-08 15:05:56 +0100 | [diff] [blame] | 735 | writeMF(mf, BootBlockFiles, "bootblock") |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 736 | writeMF(mf, ROMStageFiles, "romstage") |
| 737 | writeMF(mf, RAMStageFiles, "ramstage") |
| 738 | writeMF(mf, SMMFiles, "smm") |
| 739 | } |
| 740 | |
| 741 | devtree := Create(ctx, "devicetree.cb") |
| 742 | defer devtree.Close() |
| 743 | |
| 744 | MatchDev(&DevTree) |
| 745 | WriteDev(devtree, 0, DevTree) |
| 746 | |
| 747 | if MainboardInit != "" || MainboardEnable != "" || MainboardIncludes != nil { |
| 748 | mainboard := Create(ctx, "mainboard.c") |
| 749 | defer mainboard.Close() |
Iru Cai | 16f213a | 2020-09-16 16:23:46 +0800 | [diff] [blame] | 750 | Add_gpl(mainboard) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 751 | mainboard.WriteString("#include <device/device.h>\n") |
| 752 | for _, include := range MainboardIncludes { |
| 753 | mainboard.WriteString("#include <" + include + ">\n") |
| 754 | } |
| 755 | mainboard.WriteString("\n") |
| 756 | if MainboardInit != "" { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 757 | mainboard.WriteString(`static void mainboard_init(struct device *dev) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 758 | { |
| 759 | ` + MainboardInit + "}\n\n") |
| 760 | } |
| 761 | if MainboardInit != "" || MainboardEnable != "" { |
Elyes HAOUAS | 68c851b | 2018-06-12 22:06:09 +0200 | [diff] [blame] | 762 | mainboard.WriteString("static void mainboard_enable(struct device *dev)\n{\n") |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 763 | if MainboardInit != "" { |
| 764 | mainboard.WriteString("\tdev->ops->init = mainboard_init;\n\n") |
| 765 | } |
| 766 | mainboard.WriteString(MainboardEnable) |
| 767 | mainboard.WriteString("}\n\n") |
| 768 | mainboard.WriteString(`struct chip_operations mainboard_ops = { |
| 769 | .enable_dev = mainboard_enable, |
| 770 | }; |
| 771 | `) |
| 772 | } |
| 773 | } |
| 774 | |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 775 | bi := Create(ctx, "board_info.txt") |
| 776 | defer bi.Close() |
| 777 | |
| 778 | fixme := "" |
| 779 | |
| 780 | if dmi.IsLaptop { |
| 781 | bi.WriteString("Category: laptop\n") |
| 782 | } else { |
| 783 | bi.WriteString("Category: desktop\n") |
| 784 | fixme += "check category, " |
| 785 | } |
| 786 | |
| 787 | missing := "ROM package, ROM socketed" |
| 788 | |
| 789 | if ROMProtocol != "" { |
| 790 | fmt.Fprintf(bi, "ROM protocol: %s\n", ROMProtocol) |
| 791 | } else { |
| 792 | missing += ", ROM protocol" |
| 793 | } |
| 794 | |
| 795 | if FlashROMSupport != "" { |
| 796 | fmt.Fprintf(bi, "Flashrom support: %s\n", FlashROMSupport) |
| 797 | } else { |
| 798 | missing += ", Flashrom support" |
| 799 | } |
| 800 | |
| 801 | missing += ", Release year" |
| 802 | |
| 803 | if fixme != "" { |
| 804 | fmt.Fprintf(bi, "FIXME: %s, put %s\n", fixme, missing) |
| 805 | } else { |
| 806 | fmt.Fprintf(bi, "FIXME: put %s\n", missing) |
| 807 | } |
| 808 | |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 809 | if ROMSizeKB == 0 { |
| 810 | KconfigBool["BOARD_ROMSIZE_KB_2048"] = true |
| 811 | KconfigComment["BOARD_ROMSIZE_KB_2048"] = "FIXME: correct this" |
| 812 | } else { |
| 813 | KconfigBool[fmt.Sprintf("BOARD_ROMSIZE_KB_%d", ROMSizeKB)] = true |
| 814 | } |
| 815 | |
| 816 | makeKconfig(ctx) |
| 817 | makeKconfigName(ctx) |
| 818 | |
| 819 | dsdt := Create(ctx, "dsdt.asl") |
| 820 | defer dsdt.Close() |
| 821 | |
| 822 | for _, define := range DSDTDefines { |
| 823 | if define.Comment != "" { |
Angel Pons | ca62334 | 2019-01-16 21:55:55 +0100 | [diff] [blame] | 824 | fmt.Fprintf(dsdt, "\t/* %s. */\n", define.Comment) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 825 | } |
| 826 | dsdt.WriteString("#define " + define.Key + " " + define.Value + "\n") |
| 827 | } |
| 828 | |
Iru Cai | 16f213a | 2020-09-16 16:23:46 +0800 | [diff] [blame] | 829 | Add_gpl(dsdt) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 830 | dsdt.WriteString( |
Angel Pons | ca62334 | 2019-01-16 21:55:55 +0100 | [diff] [blame] | 831 | ` |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 832 | #include <acpi/acpi.h> |
Angel Pons | 6779d23 | 2020-01-08 15:05:56 +0100 | [diff] [blame] | 833 | |
Elyes HAOUAS | 6d19a20 | 2018-11-22 11:15:29 +0100 | [diff] [blame] | 834 | DefinitionBlock( |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 835 | "dsdt.aml", |
| 836 | "DSDT", |
Elyes HAOUAS | 90d00de | 2020-10-05 16:38:53 +0200 | [diff] [blame] | 837 | ACPI_DSDT_REV_2, |
Elyes HAOUAS | 6d19a20 | 2018-11-22 11:15:29 +0100 | [diff] [blame] | 838 | OEM_ID, |
| 839 | ACPI_TABLE_CREATOR, |
Angel Pons | 6779d23 | 2020-01-08 15:05:56 +0100 | [diff] [blame] | 840 | 0x20141018 /* OEM revision */ |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 841 | ) |
| 842 | { |
Kyösti Mälkki | 6215e61 | 2021-02-25 08:00:21 +0200 | [diff] [blame] | 843 | #include <acpi/dsdt_top.asl> |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 844 | #include "acpi/platform.asl" |
| 845 | `) |
| 846 | |
| 847 | for _, x := range DSDTIncludes { |
| 848 | if x.Comment != "" { |
Angel Pons | ca62334 | 2019-01-16 21:55:55 +0100 | [diff] [blame] | 849 | fmt.Fprintf(dsdt, "\t/* %s. */\n", x.Comment) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 850 | } |
| 851 | fmt.Fprintf(dsdt, "\t#include <%s>\n", x.File) |
| 852 | } |
| 853 | |
| 854 | dsdt.WriteString(` |
Angel Pons | ca62334 | 2019-01-16 21:55:55 +0100 | [diff] [blame] | 855 | Device (\_SB.PCI0) |
| 856 | { |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 857 | `) |
| 858 | for _, x := range DSDTPCI0Includes { |
| 859 | if x.Comment != "" { |
Angel Pons | ca62334 | 2019-01-16 21:55:55 +0100 | [diff] [blame] | 860 | fmt.Fprintf(dsdt, "\t/* %s. */\n", x.Comment) |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 861 | } |
| 862 | fmt.Fprintf(dsdt, "\t\t#include <%s>\n", x.File) |
| 863 | } |
| 864 | dsdt.WriteString( |
Angel Pons | ca62334 | 2019-01-16 21:55:55 +0100 | [diff] [blame] | 865 | ` } |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 866 | } |
| 867 | `) |
| 868 | |
Iru Cai | cd980ab | 2019-01-14 20:38:16 +0800 | [diff] [blame] | 869 | if IGDEnabled { |
| 870 | gma := Create(ctx, "gma-mainboard.ads") |
| 871 | defer gma.Close() |
| 872 | |
Angel Pons | 3d5d6e8 | 2020-03-18 23:55:36 +0100 | [diff] [blame] | 873 | gma.WriteString(`-- SPDX-License-Identifier: GPL-2.0-or-later |
Iru Cai | cd980ab | 2019-01-14 20:38:16 +0800 | [diff] [blame] | 874 | |
| 875 | with HW.GFX.GMA; |
| 876 | with HW.GFX.GMA.Display_Probing; |
| 877 | |
| 878 | use HW.GFX.GMA; |
| 879 | use HW.GFX.GMA.Display_Probing; |
| 880 | |
| 881 | private package GMA.Mainboard is |
| 882 | |
| 883 | -- FIXME: check this |
| 884 | ports : constant Port_List := |
| 885 | (DP1, |
| 886 | DP2, |
| 887 | DP3, |
| 888 | HDMI1, |
| 889 | HDMI2, |
| 890 | HDMI3, |
| 891 | Analog, |
Nico Huber | 4ce5290 | 2020-02-15 17:56:01 +0100 | [diff] [blame] | 892 | LVDS, |
| 893 | eDP); |
Iru Cai | cd980ab | 2019-01-14 20:38:16 +0800 | [diff] [blame] | 894 | |
| 895 | end GMA.Mainboard; |
| 896 | `) |
| 897 | } |
Vladimir Serbinenko | 3129f79 | 2014-10-15 21:51:47 +0200 | [diff] [blame] | 898 | } |