1/*-
2 * Copyright (c) 2013 Antti Kantee.  All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
14 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <mini-os/os.h>
27#include <mini-os/ioremap.h>
28#include <mini-os/pcifront.h>
29#include <mini-os/events.h>
30#include <mini-os/mm.h>
31#include <mini-os/hypervisor.h>
32
33#include <bmk-core/errno.h>
34#include <bmk-core/memalloc.h>
35
36#include "pci_user.h"
37
38void *
39rumpcomp_pci_map(unsigned long addr, unsigned long len)
40{
41
42	return minios_ioremap_nocache(addr, len);
43}
44
45int
46rumpcomp_pci_confread(unsigned bus, unsigned dev, unsigned fun,
47	int reg, unsigned int *rv)
48{
49
50	return pcifront_conf_read(NULL, 0, bus, dev, fun, reg, 4, rv);
51}
52
53int
54rumpcomp_pci_confwrite(unsigned bus, unsigned dev, unsigned fun,
55	int reg, unsigned int v)
56{
57
58	return pcifront_conf_write(NULL, 0, bus, dev, fun, reg, 4, v);
59}
60
61struct ihandler {
62	int (*i_handler)(void *);
63	void *i_data;
64	evtchn_port_t i_prt;
65};
66
67static void
68hyperhandler(evtchn_port_t prt, struct pt_regs *regs, void *data)
69{
70	struct ihandler *ihan = data;
71
72	/* XXX: not correct, might not even have rump kernel context now */
73	ihan->i_handler(ihan->i_data);
74}
75
76/* XXXXX */
77static int myintr;
78static unsigned mycookie;
79
80int
81rumpcomp_pci_irq_map(unsigned bus, unsigned device, unsigned fun,
82	int intrline, unsigned cookie)
83{
84
85	/* XXX */
86	myintr = intrline;
87	mycookie = cookie;
88
89	return 0;
90}
91
92void *
93rumpcomp_pci_irq_establish(unsigned cookie, int (*handler)(void *), void *data)
94{
95	struct ihandler *ihan;
96	evtchn_port_t prt;
97	int pirq;
98
99	if (cookie != mycookie)
100		return NULL;
101	pirq = myintr;
102
103	ihan = bmk_memalloc(sizeof(*ihan), 0, BMK_MEMWHO_WIREDBMK);
104	if (!ihan)
105		return NULL;
106	ihan->i_handler = handler;
107	ihan->i_data = data;
108
109	prt = minios_bind_pirq(pirq, 1, hyperhandler, ihan);
110	minios_unmask_evtchn(prt);
111	ihan->i_prt = prt;
112
113	return ihan;
114}
115