blob: 728e2d4ce50ca46f4a3b4cd06194ab16ea769aa9 [file] [log] [blame]
Isaku Yamahata4f5e19e2009-10-30 21:21:06 +09001/*
2 * pci_host.c
3 *
4 * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
5 * VA Linux Systems Japan K.K.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License along
Blue Swirl70539e12010-03-07 15:48:43 +000018 * with this program; if not, see <http://www.gnu.org/licenses/>.
Isaku Yamahata4f5e19e2009-10-30 21:21:06 +090019 */
20
21#include "pci.h"
22#include "pci_host.h"
23
24/* debug PCI */
25//#define DEBUG_PCI
26
27#ifdef DEBUG_PCI
28#define PCI_DPRINTF(fmt, ...) \
29do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
30#else
31#define PCI_DPRINTF(fmt, ...)
32#endif
33
Isaku Yamahata766347c2009-10-30 21:21:15 +090034/*
35 * PCI address
36 * bit 16 - 24: bus number
37 * bit 8 - 15: devfun number
38 * bit 0 - 7: offset in configuration space of a given pci device
39 */
40
41/* the helper functio to get a PCIDeice* for a given pci address */
Isaku Yamahata8d6514f2009-11-12 13:17:23 +020042static inline PCIDevice *pci_dev_find_by_addr(PCIBus *bus, uint32_t addr)
Isaku Yamahata766347c2009-10-30 21:21:15 +090043{
Isaku Yamahata42331e92009-11-12 14:58:37 +090044 uint8_t bus_num = addr >> 16;
45 uint8_t devfn = addr >> 8;
46
Isaku Yamahata5256d8b2011-01-27 15:56:36 +090047 return pci_find_device(bus, bus_num, devfn);
Isaku Yamahata766347c2009-10-30 21:21:15 +090048}
49
Isaku Yamahatace195fb2009-10-30 21:21:16 +090050void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, int len)
Isaku Yamahata766347c2009-10-30 21:21:15 +090051{
Isaku Yamahata8d6514f2009-11-12 13:17:23 +020052 PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
Isaku Yamahata7ac901c2009-11-12 14:58:32 +090053 uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
Isaku Yamahata766347c2009-10-30 21:21:15 +090054
55 if (!pci_dev)
56 return;
57
Blue Swirl0b987f12010-01-10 20:54:38 +000058 PCI_DPRINTF("%s: %s: addr=%02" PRIx32 " val=%08" PRIx32 " len=%d\n",
Isaku Yamahata766347c2009-10-30 21:21:15 +090059 __func__, pci_dev->name, config_addr, val, len);
60 pci_dev->config_write(pci_dev, config_addr, val, len);
61}
62
Isaku Yamahatace195fb2009-10-30 21:21:16 +090063uint32_t pci_data_read(PCIBus *s, uint32_t addr, int len)
Isaku Yamahata766347c2009-10-30 21:21:15 +090064{
Isaku Yamahata8d6514f2009-11-12 13:17:23 +020065 PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
Isaku Yamahata7ac901c2009-11-12 14:58:32 +090066 uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
Isaku Yamahata766347c2009-10-30 21:21:15 +090067 uint32_t val;
68
Michael S. Tsirkin4677d8e2009-11-12 14:58:31 +090069 assert(len == 1 || len == 2 || len == 4);
Isaku Yamahata766347c2009-10-30 21:21:15 +090070 if (!pci_dev) {
Michael S. Tsirkin4677d8e2009-11-12 14:58:31 +090071 return ~0x0;
Isaku Yamahata766347c2009-10-30 21:21:15 +090072 }
73
Michael S. Tsirkin4677d8e2009-11-12 14:58:31 +090074 val = pci_dev->config_read(pci_dev, config_addr, len);
75 PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRIx32" len=%d\n",
76 __func__, pci_dev->name, config_addr, val, len);
77
Isaku Yamahata766347c2009-10-30 21:21:15 +090078 return val;
79}
80
Alexander Graf6ebf5902010-12-08 12:05:40 +010081static void pci_host_config_write(ReadWriteHandler *handler,
82 pcibus_t addr, uint32_t val, int len)
Isaku Yamahataa4557832009-10-30 21:21:07 +090083{
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +020084 PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
Isaku Yamahataa4557832009-10-30 21:21:07 +090085
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +020086 PCI_DPRINTF("%s addr %" FMT_PCIBUS " %d val %"PRIx32"\n",
87 __func__, addr, len, val);
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +020088 s->config_reg = val;
89}
90
Alexander Graf6ebf5902010-12-08 12:05:40 +010091static uint32_t pci_host_config_read(ReadWriteHandler *handler,
92 pcibus_t addr, int len)
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +020093{
Alexander Graf6ebf5902010-12-08 12:05:40 +010094 PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +020095 uint32_t val = s->config_reg;
96
97 PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n",
98 __func__, addr, len, val);
99 return val;
100}
101
Alexander Graf6ebf5902010-12-08 12:05:40 +0100102static void pci_host_data_write(ReadWriteHandler *handler,
103 pcibus_t addr, uint32_t val, int len)
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200104{
105 PCIHostState *s = container_of(handler, PCIHostState, data_handler);
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200106 PCI_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n",
107 addr, len, val);
108 if (s->config_reg & (1u << 31))
109 pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
110}
111
Alexander Graf6ebf5902010-12-08 12:05:40 +0100112static uint32_t pci_host_data_read(ReadWriteHandler *handler,
113 pcibus_t addr, int len)
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200114{
115 PCIHostState *s = container_of(handler, PCIHostState, data_handler);
116 uint32_t val;
117 if (!(s->config_reg & (1 << 31)))
118 return 0xffffffff;
119 val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
120 PCI_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n",
121 addr, len, val);
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200122 return val;
123}
124
125static void pci_host_init(PCIHostState *s)
126{
Alexander Graf6ebf5902010-12-08 12:05:40 +0100127 s->conf_handler.write = pci_host_config_write;
128 s->conf_handler.read = pci_host_config_read;
129 s->data_handler.write = pci_host_data_write;
130 s->data_handler.read = pci_host_data_read;
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200131}
Isaku Yamahataa4557832009-10-30 21:21:07 +0900132
Alexander Graf6ebf5902010-12-08 12:05:40 +0100133int pci_host_conf_register_mmio(PCIHostState *s, int endian)
Isaku Yamahataa4557832009-10-30 21:21:07 +0900134{
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200135 pci_host_init(s);
Alexander Graf6ebf5902010-12-08 12:05:40 +0100136 return cpu_register_io_memory_simple(&s->conf_handler, endian);
Isaku Yamahataa4557832009-10-30 21:21:07 +0900137}
138
Isaku Yamahataf08b32f2009-11-12 14:58:34 +0900139void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s)
Isaku Yamahataa4557832009-10-30 21:21:07 +0900140{
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200141 pci_host_init(s);
Alexander Graf6ebf5902010-12-08 12:05:40 +0100142 register_ioport_simple(&s->conf_handler, ioport, 4, 4);
Gleb Natapovc646f742010-12-08 13:35:00 +0200143 sysbus_init_ioports(&s->busdev, ioport, 4);
Isaku Yamahataa4557832009-10-30 21:21:07 +0900144}
145
Alexander Graf6ebf5902010-12-08 12:05:40 +0100146int pci_host_data_register_mmio(PCIHostState *s, int endian)
Isaku Yamahata4f5e19e2009-10-30 21:21:06 +0900147{
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200148 pci_host_init(s);
Alexander Graf6ebf5902010-12-08 12:05:40 +0100149 return cpu_register_io_memory_simple(&s->data_handler, endian);
Isaku Yamahata4f5e19e2009-10-30 21:21:06 +0900150}
151
Isaku Yamahata4f5e19e2009-10-30 21:21:06 +0900152void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s)
153{
Michael S. Tsirkin9f6f0422010-01-16 19:20:07 +0200154 pci_host_init(s);
Alexander Graf6ebf5902010-12-08 12:05:40 +0100155 register_ioport_simple(&s->data_handler, ioport, 4, 1);
156 register_ioport_simple(&s->data_handler, ioport, 4, 2);
157 register_ioport_simple(&s->data_handler, ioport, 4, 4);
Gleb Natapovc646f742010-12-08 13:35:00 +0200158 sysbus_init_ioports(&s->busdev, ioport, 4);
Isaku Yamahata4f5e19e2009-10-30 21:21:06 +0900159}