2 * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3 * Alexander Warg <warg@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
6 * This file is part of TUD:OS and distributed under the terms of the
7 * GNU General Public License 2.
8 * Please see the COPYING-GPL-2 file for details.
10 #include "hw_device.h"
18 pci_conf_addr0(l4_uint32_t bus, l4_uint32_t dev,
19 l4_uint32_t fn, l4_uint32_t reg)
20 { return (bus << 16) | (dev << 11) | (fn << 8) | (reg & ~3); }
22 class Pci_iomem_root_bridge : public Pci_root_bridge, public Hw::Device
25 typedef Hw::Pci::Cfg_width Cfg_width;
27 Pci_iomem_root_bridge()
28 : Pci_root_bridge(this), _iobase_virt(0), _iobase_phys(~0UL),
29 _dev_start(~0UL), _dev_end(~0UL), _iosize(0)
31 set_discover_bus_if(this);
34 int cfg_read(unsigned bus, l4_uint32_t devfn, l4_uint32_t reg,
35 l4_uint32_t *value, Cfg_width);
37 int cfg_write(unsigned bus, l4_uint32_t devfn, l4_uint32_t reg,
38 l4_uint32_t value, Cfg_width);
44 int set_property(cxx::String const &prop, Prop_val const &val);
46 int getval(const char *s, cxx::String const &prop,
47 Prop_val const &val, T *rval);
48 int int_map(int i) const { return _int_map[i]; }
51 l4_addr_t _iobase_virt, _iobase_phys, _dev_start, _dev_end;
56 // Irq router that maps INTA-D to GSI
57 class Irq_router_rs : public Resource_space
60 bool request(Resource *parent, Device *, Resource *child, Device *cdev);
61 bool alloc(Resource *, Device *, Resource *, Device *, bool)
66 Pci_iomem_root_bridge::init()
68 if (_iobase_phys == ~0UL)
70 printf("ERROR: Pci_root_bridge: 'iobase' not set.\n");
76 printf("ERROR: Pci_root_bridge: 'iosize' not set.\n");
80 if (_dev_start == ~0UL || _dev_end == ~0UL)
82 printf("ERROR: Pci_root_bridge: 'dev_start' and/or 'dev_end' not set.\n");
86 _iobase_virt = res_map_iomem(_iobase_phys, _iosize);
90 add_resource(new Adr_resource(Resource::Mmio_res
91 | Resource::F_fixed_size
92 | Resource::F_fixed_addr,
94 _iobase_phys + _iosize - 1));
96 Adr_resource *r = new Adr_resource_provider(Resource::Mmio_res
97 | Resource::F_fixed_size
98 | Resource::F_fixed_addr);
99 r->alignment(0xfffff);
100 r->start_end(_dev_start, _dev_end);
103 r = new Adr_resource_provider(Resource::Io_res
104 | Resource::F_fixed_size
105 | Resource::F_fixed_addr);
106 r->start_end(0, 0xffff);
109 add_resource(new Pci_irq_router_res<Irq_router_rs>());
115 Pci_iomem_root_bridge::scan_bus()
119 Pci_root_bridge::scan_bus();
123 Pci_iomem_root_bridge::cfg_read(unsigned bus, l4_uint32_t devfn,
125 l4_uint32_t *value, Cfg_width order)
132 l4_uint32_t a = _iobase_virt + pci_conf_addr0(bus, devfn >> 16, devfn & 0xffff, reg);
135 case Pci::Cfg_byte: *value = *(volatile unsigned char *)(a + (reg & 3)); break;
136 case Pci::Cfg_short: *value = *(volatile unsigned short *)(a + (reg & 2)); break;
137 case Pci::Cfg_long: *value = *(volatile unsigned int *)a; break;
141 printf("Pci_iomem_root_bridge::cfg_read(%x, %x, %x, %x, %d)\n",
142 bus, devfn, reg, *value, order);
148 Pci_iomem_root_bridge::cfg_write(unsigned bus, l4_uint32_t devfn,
150 l4_uint32_t value, Cfg_width order)
158 printf("Pci_iomem_root_bridge::cfg_write(%x, %x, %x, %x, %d)\n",
159 bus, devfn, reg, value, order);
161 l4_uint32_t a = _iobase_virt + pci_conf_addr0(bus, devfn >> 16, devfn & 0xffff, reg);
164 case Pci::Cfg_byte: *(volatile unsigned char *)(a + (reg & 3)) = value; break;
165 case Pci::Cfg_short: *(volatile unsigned short *)(a + (reg & 2)) = value; break;
166 case Pci::Cfg_long: *(volatile unsigned int *)a = value; break;
171 template< typename T>
173 Pci_iomem_root_bridge::getval(const char *s, cxx::String const &prop,
174 Prop_val const &val, T *rval)
178 if (val.type != Prop_val::Int)
181 *rval = val.val.integer;
188 Pci_iomem_root_bridge::set_property(cxx::String const &prop, Prop_val const &val)
192 if ((r = getval("iobase", prop, val, &_iobase_phys)) != -E_no_prop)
194 else if ((r = getval("iosize", prop, val, &_iosize)) != -E_no_prop)
196 else if ((r = getval("dev_start", prop, val, &_dev_start)) != -E_no_prop)
198 else if ((r = getval("dev_end", prop, val, &_dev_end)) != -E_no_prop)
200 else if ((r = getval("int_a", prop, val, &_int_map[0])) != -E_no_prop)
202 else if ((r = getval("int_b", prop, val, &_int_map[1])) != -E_no_prop)
204 else if ((r = getval("int_c", prop, val, &_int_map[2])) != -E_no_prop)
206 else if ((r = getval("int_d", prop, val, &_int_map[3])) != -E_no_prop)
209 return Hw::Device::set_property(prop, val);
212 static Hw::Device_factory_t<Pci_iomem_root_bridge>
213 __hw_pci_root_bridge_factory("Pci_iomem_root_bridge");
215 bool Irq_router_rs::request(Resource *parent, Device *pdev,
216 Resource *child, Device *cdev)
218 Adr_resource *cr = dynamic_cast<Adr_resource*>(child);
222 child->parent(parent);
226 Hw::Device *cd = dynamic_cast<Hw::Device*>(cdev);
233 int i = (cr->start() + (cd->adr() >> 16)) & 3;
235 Pci_iomem_root_bridge *pd = dynamic_cast<Pci_iomem_root_bridge *>(pdev);
240 cr->del_flags(Resource::F_relative);
241 cr->start(pd->int_map(i));
242 cr->del_flags(Resource::Irq_info_base * 3);
243 cr->add_flags(Resource::Irq_level | Resource::Irq_low);