2022-08-19 16:55:33 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
|
|
|
Copyright (C) 2018-22 divingkatae and maximum
|
|
|
|
(theweirdo) spatium
|
|
|
|
|
|
|
|
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <devices/common/hwcomponent.h>
|
2022-10-26 06:06:12 +00:00
|
|
|
#include <devices/common/pci/pcibridge.h>
|
2022-01-16 20:30:43 +00:00
|
|
|
#include <devices/common/pci/pcihost.h>
|
2023-11-03 07:21:33 +00:00
|
|
|
#include <devices/deviceregistry.h>
|
2022-01-16 20:30:43 +00:00
|
|
|
#include <devices/memctrl/memctrlbase.h>
|
|
|
|
#include <machines/machinebase.h>
|
2022-10-26 06:06:12 +00:00
|
|
|
#include <endianswap.h>
|
2022-08-19 16:55:33 +00:00
|
|
|
#include <loguru.hpp>
|
2022-01-16 20:30:43 +00:00
|
|
|
|
|
|
|
#include <cinttypes>
|
|
|
|
|
2023-02-05 08:37:29 +00:00
|
|
|
bool PCIHost::pci_register_device(int dev_fun_num, PCIDevice* dev_instance)
|
2022-01-16 20:30:43 +00:00
|
|
|
{
|
2022-10-26 06:06:12 +00:00
|
|
|
// return false if dev_fun_num already registered
|
2023-02-05 08:37:29 +00:00
|
|
|
if (this->dev_map.count(dev_fun_num))
|
2022-01-16 20:30:43 +00:00
|
|
|
return false;
|
|
|
|
|
2022-10-26 06:06:12 +00:00
|
|
|
int fun_num = dev_fun_num & 7;
|
|
|
|
int dev_num = (dev_fun_num >> 3) & 0x1f;
|
|
|
|
bool is_multi_function = fun_num != 0;
|
|
|
|
|
|
|
|
for (int other_fun_num = 0; other_fun_num < 8; other_fun_num++) {
|
|
|
|
if (this->dev_map.count(DEV_FUN(dev_num, other_fun_num))) {
|
|
|
|
is_multi_function = true;
|
|
|
|
if (is_multi_function && other_fun_num == 0) {
|
|
|
|
this->dev_map[DEV_FUN(dev_num, other_fun_num)]->set_multi_function(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 08:37:29 +00:00
|
|
|
this->dev_map[dev_fun_num] = dev_instance;
|
2022-01-16 20:30:43 +00:00
|
|
|
|
|
|
|
dev_instance->set_host(this);
|
2022-10-26 06:06:12 +00:00
|
|
|
if (is_multi_function && fun_num == 0) {
|
|
|
|
dev_instance->set_multi_function(true);
|
|
|
|
}
|
2022-01-16 20:30:43 +00:00
|
|
|
|
|
|
|
if (dev_instance->supports_io_space()) {
|
|
|
|
this->io_space_devs.push_back(dev_instance);
|
|
|
|
}
|
|
|
|
|
2022-10-26 06:06:12 +00:00
|
|
|
PCIBridge *bridge = dynamic_cast<PCIBridge*>(dev_instance);
|
|
|
|
if (bridge) {
|
|
|
|
this->bridge_devs.push_back(bridge);
|
|
|
|
}
|
|
|
|
|
2022-01-16 20:30:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PCIHost::pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj)
|
|
|
|
{
|
|
|
|
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
|
|
|
|
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
|
|
|
|
// FIXME: add sanity checks!
|
|
|
|
return mem_ctrl->add_mmio_region(start_addr, size, obj);
|
|
|
|
}
|
2022-08-19 16:55:33 +00:00
|
|
|
|
2022-12-12 08:16:59 +00:00
|
|
|
bool PCIHost::pci_unregister_mmio_region(uint32_t start_addr, uint32_t size, PCIDevice* obj)
|
|
|
|
{
|
|
|
|
MemCtrlBase *mem_ctrl = dynamic_cast<MemCtrlBase *>
|
|
|
|
(gMachineObj->get_comp_by_type(HWCompType::MEM_CTRL));
|
|
|
|
// FIXME: add sanity checks!
|
|
|
|
return mem_ctrl->remove_mmio_region(start_addr, size, obj);
|
|
|
|
}
|
|
|
|
|
2023-02-05 09:12:50 +00:00
|
|
|
void PCIHost::attach_pci_device(const std::string& dev_name, int slot_id)
|
|
|
|
{
|
|
|
|
this->attach_pci_device(dev_name, slot_id, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
PCIDevice *PCIHost::attach_pci_device(const std::string& dev_name, int slot_id, const std::string& dev_suffix)
|
2022-08-19 16:55:33 +00:00
|
|
|
{
|
|
|
|
if (!DeviceRegistry::device_registered(dev_name)) {
|
2023-02-05 09:12:50 +00:00
|
|
|
HWComponent *hwc = dynamic_cast<HWComponent*>(this);
|
|
|
|
LOG_F(
|
|
|
|
WARNING, "%s: specified PCI device %s doesn't exist",
|
|
|
|
hwc ? hwc->get_name().c_str() : "PCIHost", dev_name.c_str()
|
|
|
|
);
|
|
|
|
return NULL;
|
2022-08-19 16:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to create device object
|
|
|
|
auto dev_obj = DeviceRegistry::get_descriptor(dev_name).m_create_func();
|
|
|
|
|
|
|
|
if (!dev_obj->supports_type(HWCompType::PCI_DEV)) {
|
2023-02-05 09:12:50 +00:00
|
|
|
HWComponent *hwc = dynamic_cast<HWComponent*>(this);
|
|
|
|
LOG_F(
|
|
|
|
WARNING, "%s: cannot attach non-PCI device %s",
|
|
|
|
hwc ? hwc->get_name().c_str() : "PCIHost", dev_name.c_str()
|
|
|
|
);
|
|
|
|
|
|
|
|
return NULL;
|
2022-08-19 16:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add device to the machine object
|
2023-02-05 09:12:50 +00:00
|
|
|
gMachineObj->add_device(dev_name + dev_suffix, std::move(dev_obj));
|
|
|
|
|
|
|
|
PCIDevice *dev = dynamic_cast<PCIDevice*>(gMachineObj->get_comp_by_name(dev_name + dev_suffix));
|
2022-08-19 16:55:33 +00:00
|
|
|
|
|
|
|
// register device with the PCI host
|
2023-02-05 09:12:50 +00:00
|
|
|
this->pci_register_device(slot_id, dev);
|
|
|
|
|
|
|
|
return dev;
|
2022-08-19 16:55:33 +00:00
|
|
|
}
|
Fix PCI config r/w of byte and word and unaligned.
dingusppc could not read bytes from offset 1,2,3 or words from offset 2.
dingusppc did not read words from offset 1,3 and longs from offset 1,2,3 in the same way as a real Power Mac 8600 or B&W G3.
This commit fixes those issues.
- Added pci_cfg_rev_read. It takes a 32 bit value from offset 0 and returns a value of the specified size using bytes starting from the specified offset. Offsets 4,5, & 6 wrap around to 0,1, & 2 respectively. The result bytes are in flipped order as required by the read method (so a value of 0x12345678 is returned as 0x78563412)
A real Power Mac 8600 might return a random byte for offset 4, 5, 6 for vci0 but usually not for pci1. A B&W G3 seems to always wrap around correctly. We won't read random bytes, and we won't read a default such as 00 or FF. We'll do the wrap around which makes the most sense because writing 0x12345678 to any offset and reading from the same offset should produce the value that was written.
- Added pci_cfg_rev_write. It takes a 32 bit value from offset 0, and modifies a specified number of bytes starting at a specified offset with the offset wrapping around to 0 if it exceeds 3. The modified bytes take their new values from the flipped bytes passed to pci_cfg_write. When size is 4, the original value is not used since all bytes will be modified.
Basically, those two functions handle all the sizes and all the offsets and replace calls to BYTESWAP_32, read_mem or read_mem_rev, and write_mem or write_mem_rev.
read_mem_rev, as it was used by pcidevice and some other places, could read beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always read the wrong byte or word if they were not at offset 0. Same for read_mem as used by mpc106.
write_mem_rev, as it was used by pcidevice and some other places, could write beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always write the wrong byte or word if they were not at offset 0. Same for write_mem as used by mpc106.
pcidevice:
- The logging macros should be used to handle all config register access logging.
- Unaligned PCI config register accesses will be output as ERROR instead of WARNING.
- The logging macros include the offset and size. They also include the value for named registers or for writes.
- Added MMIODevice read and write methods so that PCIDevice is not abstract if a PCIDevice doesn't override the read and write method since some PCIDevices don't have MMIO.
pcihost:
- Added pci_find_device stub for handling PCI bridges in future commit.
bandit and mpc106:
- PCI host controllers will handle all PCI config access alignment and sizing. A PCIDevice will always access config registers as 32 bits on a 4 byte boundary. The AccessDetails passed to a PCIDevice config read or write method is there only for logging purposes.
bandit:
- Common MMIO code is moved to new BanditHost class so both Bandit and Chaos can use it. PCI related code is moved to new BanditPCI class.
- Simplify IDSEL to/from PCI device number conversion by removing the shift or subtract.
- Remove BANDIT_ID_SEL check. The IDSEL conversion to PCI device number can find the bandit PCI device.
- For logging, make best guess of PCI device number from invalid IDSEL - the result is always reasonable for device 0x00 to 0x0A when accessing config register 0x00 (as one would do when scanning for PCI devices like lspci does).
mpc106:
- Common config space code is put in cfg_setup. It handles extracting the offset.
- Added code to log access to unimplemented config registers of grackle.
- Don't call setup_ram when writing to config registers that setup_ram doesn't use.
- pci_cfg_read calls READ_DWORD_LE_A and pci_cfg_write calls WRITE_DWORD_LE_A. When reading or writing memory that is organized as little endian dwords, such as my_pci_cfg_hdr of mpc106, the function should explicitly state that it's little endian so that the emulator may be ported one day to a CPU architecture that is not little endian.
atirage:
- The changes correctly place user_cfg at byte 0x40 instead of 0x43 and writes the correct byte depending on size and offset.
2022-09-02 10:32:28 +00:00
|
|
|
|
2022-10-26 06:06:12 +00:00
|
|
|
bool PCIHost::pci_io_read_loop(uint32_t offset, int size, uint32_t &res)
|
|
|
|
{
|
|
|
|
for (auto& dev : this->io_space_devs) {
|
|
|
|
if (dev->pci_io_read(offset, size, &res)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PCIHost::pci_io_write_loop(uint32_t offset, int size, uint32_t value)
|
|
|
|
{
|
|
|
|
for (auto& dev : this->io_space_devs) {
|
|
|
|
if (dev->pci_io_write(offset, value, size)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t PCIHost::pci_io_read_broadcast(uint32_t offset, int size)
|
|
|
|
{
|
|
|
|
uint32_t res;
|
|
|
|
// broadcast I/O request to devices that support I/O space
|
|
|
|
// until a device returns true that means "request accepted"
|
|
|
|
if (pci_io_read_loop (offset, size, res)) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
HWComponent *hwc = dynamic_cast<HWComponent*>(this);
|
|
|
|
LOG_F(
|
|
|
|
ERROR, "%s: Attempt to read from unmapped PCI I/O space @%08x.%c",
|
|
|
|
hwc ? hwc->get_name().c_str() : "PCIHost", offset,
|
|
|
|
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size
|
|
|
|
);
|
|
|
|
// FIXME: add machine check exception (DEFAULT CATCH!, code=FFF00200)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCIHost::pci_io_write_broadcast(uint32_t offset, int size, uint32_t value)
|
|
|
|
{
|
|
|
|
// broadcast I/O request to devices that support I/O space
|
|
|
|
// until a device returns true that means "request accepted"
|
|
|
|
if (pci_io_write_loop(offset, size, value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
HWComponent *hwc = dynamic_cast<HWComponent*>(this);
|
|
|
|
LOG_F(
|
|
|
|
ERROR, "%s: Attempt to write to unmapped PCI I/O space @%08x.%c = %0*x",
|
|
|
|
hwc ? hwc->get_name().c_str() : "PCIHost", offset,
|
|
|
|
size == 4 ? 'l' : size == 2 ? 'w' : size == 1 ? 'b' : '0' + size,
|
|
|
|
size * 2, BYTESWAP_SIZED(value, size)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
Fix PCI config r/w of byte and word and unaligned.
dingusppc could not read bytes from offset 1,2,3 or words from offset 2.
dingusppc did not read words from offset 1,3 and longs from offset 1,2,3 in the same way as a real Power Mac 8600 or B&W G3.
This commit fixes those issues.
- Added pci_cfg_rev_read. It takes a 32 bit value from offset 0 and returns a value of the specified size using bytes starting from the specified offset. Offsets 4,5, & 6 wrap around to 0,1, & 2 respectively. The result bytes are in flipped order as required by the read method (so a value of 0x12345678 is returned as 0x78563412)
A real Power Mac 8600 might return a random byte for offset 4, 5, 6 for vci0 but usually not for pci1. A B&W G3 seems to always wrap around correctly. We won't read random bytes, and we won't read a default such as 00 or FF. We'll do the wrap around which makes the most sense because writing 0x12345678 to any offset and reading from the same offset should produce the value that was written.
- Added pci_cfg_rev_write. It takes a 32 bit value from offset 0, and modifies a specified number of bytes starting at a specified offset with the offset wrapping around to 0 if it exceeds 3. The modified bytes take their new values from the flipped bytes passed to pci_cfg_write. When size is 4, the original value is not used since all bytes will be modified.
Basically, those two functions handle all the sizes and all the offsets and replace calls to BYTESWAP_32, read_mem or read_mem_rev, and write_mem or write_mem_rev.
read_mem_rev, as it was used by pcidevice and some other places, could read beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always read the wrong byte or word if they were not at offset 0. Same for read_mem as used by mpc106.
write_mem_rev, as it was used by pcidevice and some other places, could write beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always write the wrong byte or word if they were not at offset 0. Same for write_mem as used by mpc106.
pcidevice:
- The logging macros should be used to handle all config register access logging.
- Unaligned PCI config register accesses will be output as ERROR instead of WARNING.
- The logging macros include the offset and size. They also include the value for named registers or for writes.
- Added MMIODevice read and write methods so that PCIDevice is not abstract if a PCIDevice doesn't override the read and write method since some PCIDevices don't have MMIO.
pcihost:
- Added pci_find_device stub for handling PCI bridges in future commit.
bandit and mpc106:
- PCI host controllers will handle all PCI config access alignment and sizing. A PCIDevice will always access config registers as 32 bits on a 4 byte boundary. The AccessDetails passed to a PCIDevice config read or write method is there only for logging purposes.
bandit:
- Common MMIO code is moved to new BanditHost class so both Bandit and Chaos can use it. PCI related code is moved to new BanditPCI class.
- Simplify IDSEL to/from PCI device number conversion by removing the shift or subtract.
- Remove BANDIT_ID_SEL check. The IDSEL conversion to PCI device number can find the bandit PCI device.
- For logging, make best guess of PCI device number from invalid IDSEL - the result is always reasonable for device 0x00 to 0x0A when accessing config register 0x00 (as one would do when scanning for PCI devices like lspci does).
mpc106:
- Common config space code is put in cfg_setup. It handles extracting the offset.
- Added code to log access to unimplemented config registers of grackle.
- Don't call setup_ram when writing to config registers that setup_ram doesn't use.
- pci_cfg_read calls READ_DWORD_LE_A and pci_cfg_write calls WRITE_DWORD_LE_A. When reading or writing memory that is organized as little endian dwords, such as my_pci_cfg_hdr of mpc106, the function should explicitly state that it's little endian so that the emulator may be ported one day to a CPU architecture that is not little endian.
atirage:
- The changes correctly place user_cfg at byte 0x40 instead of 0x43 and writes the correct byte depending on size and offset.
2022-09-02 10:32:28 +00:00
|
|
|
PCIDevice *PCIHost::pci_find_device(uint8_t bus_num, uint8_t dev_num, uint8_t fun_num)
|
|
|
|
{
|
2022-10-26 06:06:12 +00:00
|
|
|
for (auto& bridge : this->bridge_devs) {
|
|
|
|
if (bridge->secondary_bus <= bus_num) {
|
|
|
|
if (bridge->secondary_bus == bus_num) {
|
|
|
|
if (bridge->dev_map.count(DEV_FUN(dev_num, fun_num))) {
|
|
|
|
return bridge->dev_map[DEV_FUN(dev_num, fun_num)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (bridge->subordinate_bus >= bus_num) {
|
|
|
|
return bridge->pci_find_device(bus_num, dev_num, fun_num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Fix PCI config r/w of byte and word and unaligned.
dingusppc could not read bytes from offset 1,2,3 or words from offset 2.
dingusppc did not read words from offset 1,3 and longs from offset 1,2,3 in the same way as a real Power Mac 8600 or B&W G3.
This commit fixes those issues.
- Added pci_cfg_rev_read. It takes a 32 bit value from offset 0 and returns a value of the specified size using bytes starting from the specified offset. Offsets 4,5, & 6 wrap around to 0,1, & 2 respectively. The result bytes are in flipped order as required by the read method (so a value of 0x12345678 is returned as 0x78563412)
A real Power Mac 8600 might return a random byte for offset 4, 5, 6 for vci0 but usually not for pci1. A B&W G3 seems to always wrap around correctly. We won't read random bytes, and we won't read a default such as 00 or FF. We'll do the wrap around which makes the most sense because writing 0x12345678 to any offset and reading from the same offset should produce the value that was written.
- Added pci_cfg_rev_write. It takes a 32 bit value from offset 0, and modifies a specified number of bytes starting at a specified offset with the offset wrapping around to 0 if it exceeds 3. The modified bytes take their new values from the flipped bytes passed to pci_cfg_write. When size is 4, the original value is not used since all bytes will be modified.
Basically, those two functions handle all the sizes and all the offsets and replace calls to BYTESWAP_32, read_mem or read_mem_rev, and write_mem or write_mem_rev.
read_mem_rev, as it was used by pcidevice and some other places, could read beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always read the wrong byte or word if they were not at offset 0. Same for read_mem as used by mpc106.
write_mem_rev, as it was used by pcidevice and some other places, could write beyond offset 3 if it were ever passed a reg_offs value that did not have offset as 0. Since the offset was always zero, it would always write the wrong byte or word if they were not at offset 0. Same for write_mem as used by mpc106.
pcidevice:
- The logging macros should be used to handle all config register access logging.
- Unaligned PCI config register accesses will be output as ERROR instead of WARNING.
- The logging macros include the offset and size. They also include the value for named registers or for writes.
- Added MMIODevice read and write methods so that PCIDevice is not abstract if a PCIDevice doesn't override the read and write method since some PCIDevices don't have MMIO.
pcihost:
- Added pci_find_device stub for handling PCI bridges in future commit.
bandit and mpc106:
- PCI host controllers will handle all PCI config access alignment and sizing. A PCIDevice will always access config registers as 32 bits on a 4 byte boundary. The AccessDetails passed to a PCIDevice config read or write method is there only for logging purposes.
bandit:
- Common MMIO code is moved to new BanditHost class so both Bandit and Chaos can use it. PCI related code is moved to new BanditPCI class.
- Simplify IDSEL to/from PCI device number conversion by removing the shift or subtract.
- Remove BANDIT_ID_SEL check. The IDSEL conversion to PCI device number can find the bandit PCI device.
- For logging, make best guess of PCI device number from invalid IDSEL - the result is always reasonable for device 0x00 to 0x0A when accessing config register 0x00 (as one would do when scanning for PCI devices like lspci does).
mpc106:
- Common config space code is put in cfg_setup. It handles extracting the offset.
- Added code to log access to unimplemented config registers of grackle.
- Don't call setup_ram when writing to config registers that setup_ram doesn't use.
- pci_cfg_read calls READ_DWORD_LE_A and pci_cfg_write calls WRITE_DWORD_LE_A. When reading or writing memory that is organized as little endian dwords, such as my_pci_cfg_hdr of mpc106, the function should explicitly state that it's little endian so that the emulator may be ported one day to a CPU architecture that is not little endian.
atirage:
- The changes correctly place user_cfg at byte 0x40 instead of 0x43 and writes the correct byte depending on size and offset.
2022-09-02 10:32:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|