2021-10-23 19:00:31 +00:00
|
|
|
/*
|
|
|
|
DingusPPC - The Experimental PowerPC Macintosh emulator
|
2023-06-08 14:09:29 +00:00
|
|
|
Copyright (C) 2018-23 divingkatae and maximum
|
2021-10-23 19:00:31 +00:00
|
|
|
(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/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-23 21:30:33 +00:00
|
|
|
#ifndef PCI_HOST_H
|
|
|
|
#define PCI_HOST_H
|
|
|
|
|
2023-01-31 22:20:31 +00:00
|
|
|
#include <core/bitops.h>
|
|
|
|
#include <endianswap.h>
|
2022-08-19 16:55:33 +00:00
|
|
|
|
2019-08-23 21:30:33 +00:00
|
|
|
#include <cinttypes>
|
2022-08-19 16:55:33 +00:00
|
|
|
#include <string>
|
2022-01-16 20:30:43 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2019-08-23 21:30: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
|
|
|
enum {
|
|
|
|
PCI_CONFIG_DIRECTION = 1,
|
|
|
|
PCI_CONFIG_READ = 0,
|
|
|
|
PCI_CONFIG_WRITE = 1,
|
|
|
|
|
|
|
|
PCI_CONFIG_TYPE = 4,
|
|
|
|
PCI_CONFIG_TYPE_0 = 0,
|
|
|
|
PCI_CONFIG_TYPE_1 = 4,
|
2023-06-08 14:09:29 +00:00
|
|
|
}; // PCIAccessFlags
|
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
|
|
|
|
|
|
|
/** PCI config space access details */
|
|
|
|
typedef struct AccessDetails {
|
|
|
|
uint8_t size;
|
|
|
|
uint8_t offset;
|
|
|
|
uint8_t flags;
|
|
|
|
} AccessDetails;
|
|
|
|
|
2023-02-05 08:37:29 +00:00
|
|
|
#define DEV_FUN(dev_num,fun_num) (((dev_num) << 3) | (fun_num))
|
|
|
|
|
2023-06-08 14:09:29 +00:00
|
|
|
class PCIBase;
|
|
|
|
class PCIBridgeBase;
|
2019-08-23 21:30:33 +00:00
|
|
|
|
|
|
|
class PCIHost {
|
|
|
|
public:
|
2022-01-16 20:30:43 +00:00
|
|
|
PCIHost() {
|
|
|
|
this->dev_map.clear();
|
|
|
|
io_space_devs.clear();
|
|
|
|
};
|
|
|
|
~PCIHost() = default;
|
|
|
|
|
2023-06-08 14:09:29 +00:00
|
|
|
virtual bool pci_register_device(int dev_fun_num, PCIBase* dev_instance);
|
2022-01-16 20:30:43 +00:00
|
|
|
|
2023-06-08 14:09:29 +00:00
|
|
|
virtual bool pci_register_mmio_region(uint32_t start_addr, uint32_t size, PCIBase* obj);
|
|
|
|
virtual bool pci_unregister_mmio_region(uint32_t start_addr, uint32_t size, PCIBase* obj);
|
2022-01-16 20:30:43 +00:00
|
|
|
|
2023-02-05 09:12:50 +00:00
|
|
|
virtual void attach_pci_device(const std::string& dev_name, int slot_id);
|
2023-06-08 14:09:29 +00:00
|
|
|
PCIBase *attach_pci_device(const std::string& dev_name, int slot_id,
|
|
|
|
const std::string& dev_suffix);
|
2022-08-19 16:55:33 +00:00
|
|
|
|
2022-10-26 06:06:12 +00:00
|
|
|
virtual bool pci_io_read_loop (uint32_t offset, int size, uint32_t &res);
|
|
|
|
virtual bool pci_io_write_loop(uint32_t offset, int size, uint32_t value);
|
|
|
|
|
|
|
|
virtual uint32_t pci_io_read_broadcast (uint32_t offset, int size);
|
|
|
|
virtual void pci_io_write_broadcast(uint32_t offset, int size, uint32_t value);
|
|
|
|
|
2023-06-08 14:09:29 +00:00
|
|
|
virtual PCIBase *pci_find_device(uint8_t bus_num, uint8_t dev_num, uint8_t fun_num);
|
2023-09-23 00:42:55 +00:00
|
|
|
virtual PCIBase *pci_find_device(uint8_t dev_num, uint8_t 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
|
|
|
|
2023-09-19 00:24:50 +00:00
|
|
|
virtual void pci_interrupt(uint8_t irq_line_state, PCIBase *dev) {}
|
2023-02-03 17:01:59 +00:00
|
|
|
|
2022-01-16 20:30:43 +00:00
|
|
|
protected:
|
2023-06-08 14:09:29 +00:00
|
|
|
std::unordered_map<int, PCIBase*> dev_map;
|
|
|
|
std::vector<PCIBase*> io_space_devs;
|
|
|
|
std::vector<PCIBridgeBase*> bridge_devs;
|
2019-08-23 21:30:33 +00:00
|
|
|
};
|
|
|
|
|
2023-02-02 01:21:23 +00:00
|
|
|
// Helpers for data conversion in the PCI Configuration space.
|
|
|
|
|
|
|
|
/**
|
2023-06-15 04:55:43 +00:00
|
|
|
Perform size dependent endian swapping for value that is dword from PCI config or any other dword little endian register.
|
2023-02-02 01:21:23 +00:00
|
|
|
|
2023-06-15 04:55:43 +00:00
|
|
|
Unaligned data is handled properly by using bytes from the next dword.
|
2023-02-02 01:21:23 +00:00
|
|
|
*/
|
2023-06-15 04:55:43 +00:00
|
|
|
inline uint32_t pci_conv_rd_data(uint32_t value, uint32_t value2, AccessDetails &details) {
|
2023-01-31 22:20:31 +00:00
|
|
|
switch (details.size << 2 | details.offset) {
|
|
|
|
// Bytes
|
|
|
|
case 0x04:
|
|
|
|
return value & 0xFF; // 0
|
|
|
|
case 0x05:
|
|
|
|
return (value >> 8) & 0xFF; // 1
|
|
|
|
case 0x06:
|
|
|
|
return (value >> 16) & 0xFF; // 2
|
|
|
|
case 0x07:
|
|
|
|
return (value >> 24) & 0xFF; // 3
|
|
|
|
|
|
|
|
// Words
|
|
|
|
case 0x08:
|
|
|
|
return BYTESWAP_16(value); // 0 1
|
|
|
|
case 0x09:
|
|
|
|
return BYTESWAP_16((value >> 8) & 0xFFFFU); // 1 2
|
|
|
|
case 0x0A:
|
|
|
|
return BYTESWAP_16((value >> 16) & 0xFFFFU); // 2 3
|
|
|
|
case 0x0B:
|
2023-06-15 04:55:43 +00:00
|
|
|
return ((value >> 16) & 0xFF00) | (value2 & 0xFF); // 3 4
|
2023-01-31 22:20:31 +00:00
|
|
|
|
|
|
|
// Dwords
|
|
|
|
case 0x10:
|
2023-06-15 04:55:43 +00:00
|
|
|
return BYTESWAP_32(value); // 0 1 2 3
|
2023-01-31 22:20:31 +00:00
|
|
|
case 0x11:
|
2023-06-15 04:55:43 +00:00
|
|
|
value = (uint32_t)((((uint64_t)value2 << 32) | value) >> 8);
|
|
|
|
return BYTESWAP_32(value); // 1 2 3 4
|
2023-01-31 22:20:31 +00:00
|
|
|
case 0x12:
|
2023-06-15 04:55:43 +00:00
|
|
|
value = (uint32_t)((((uint64_t)value2 << 32) | value) >> 16);
|
|
|
|
return BYTESWAP_32(value); // 2 3 4 5
|
2023-01-31 22:20:31 +00:00
|
|
|
case 0x13:
|
2023-06-15 04:55:43 +00:00
|
|
|
value = (uint32_t)((((uint64_t)value2 << 32) | value) >> 24);
|
|
|
|
return BYTESWAP_32(value); // 3 4 5 6
|
2023-01-31 22:20:31 +00:00
|
|
|
default:
|
|
|
|
return 0xFFFFFFFFUL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 01:21:23 +00:00
|
|
|
/**
|
|
|
|
Perform size dependent endian swapping for v2, then merge v2 with v1 under
|
|
|
|
control of a mask generated according with the size parameter.
|
|
|
|
|
|
|
|
Unaligned data is handled properly by wrapping around if needed.
|
|
|
|
*/
|
|
|
|
inline uint32_t pci_conv_wr_data(uint32_t v1, uint32_t v2, AccessDetails &details)
|
2023-01-31 22:20:31 +00:00
|
|
|
{
|
|
|
|
switch (details.size << 2 | details.offset) {
|
|
|
|
// Bytes
|
|
|
|
case 0x04:
|
|
|
|
return (v1 & ~0xFF) | (v2 & 0xFF); // 3 2 1 d0
|
|
|
|
case 0x05:
|
|
|
|
return (v1 & ~0xFF00) | ((v2 & 0xFF) << 8); // 3 2 d0 0
|
|
|
|
case 0x06:
|
|
|
|
return (v1 & ~0xFF0000) | ((v2 & 0xFF) << 16); // 3 d0 1 0
|
|
|
|
case 0x07:
|
|
|
|
return (v1 & 0x00FFFFFF) | ((v2 & 0xFF) << 24); // d0 2 1 0
|
|
|
|
|
|
|
|
// Words
|
|
|
|
case 0x08:
|
|
|
|
return (v1 & ~0xFFFF) | BYTESWAP_16(v2); // 3 2 d1 d0
|
|
|
|
case 0x09:
|
|
|
|
return (v1 & ~0xFFFF00) | (BYTESWAP_16(v2) << 8); // 3 d1 d0 0
|
|
|
|
case 0x0a:
|
|
|
|
return (v1 & 0x0000FFFF) | (BYTESWAP_16(v2) << 16); // d1 d0 1 0
|
|
|
|
case 0x0b:
|
|
|
|
return (v1 & 0x00FFFF00) | ((v2 & 0xFF00) << 16) |
|
|
|
|
(v2 & 0xFF); // d0 2 1 d1
|
|
|
|
|
|
|
|
// Dwords
|
|
|
|
case 0x10:
|
|
|
|
return BYTESWAP_32(v2); // d3 d2 d1 d0
|
|
|
|
case 0x11:
|
|
|
|
return ROTL_32(BYTESWAP_32(v2), 8); // d2 d1 d0 d3
|
|
|
|
case 0x12:
|
|
|
|
return ROTL_32(BYTESWAP_32(v2), 16); // d1 d0 d3 d2
|
|
|
|
case 0x13:
|
|
|
|
return ROTR_32(BYTESWAP_32(v2), 8); // d0 d3 d2 d1
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 0xFFFFFFFFUL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 21:30:33 +00:00
|
|
|
#endif /* PCI_HOST_H */
|