summaryrefslogtreecommitdiffstats
path: root/core/pci-virt.c
blob: b531470bbae7bbf88baead8abe78b9bf04f9f033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* Copyright 2013-2016 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <skiboot.h>
#include <pci.h>
#include <pci-virt.h>

void pci_virt_cfg_read_raw(struct pci_virt_device *pvd,
			   uint32_t space, uint32_t offset,
			   uint32_t size, uint32_t *data)
{
	uint32_t i;

	if (space >= PCI_VIRT_CFG_MAX || !pvd->config[space])
		return;

	for (*data = 0, i = 0; i < size; i++)
		*data |= ((uint32_t)(pvd->config[space][offset + i]) << (i * 8));
}

void pci_virt_cfg_write_raw(struct pci_virt_device *pvd,
			    uint32_t space, uint32_t offset,
			    uint32_t size, uint32_t data)
{
	int i;

	if (space >= PCI_VIRT_CFG_MAX || !pvd->config[space])
		return;

	for (i = 0; i < size; i++) {
		pvd->config[space][offset + i] = data;
		data = (data >> 8);
	}
}

static struct pci_cfg_reg_filter *pci_virt_find_filter(
					struct pci_virt_device *pvd,
					uint32_t start, uint32_t len)
{
	struct pci_cfg_reg_filter *pcrf;

	if (!pvd || !len || start >= pvd->cfg_size)
		return NULL;

	/* Return filter if there is overlapped region. We don't
	 * require strict matching for more flexibility. It also
	 * means the associated handler should validate the register
	 * offset and length.
	 */
	list_for_each(&pvd->pcrf, pcrf, link) {
		if (start < (pcrf->start + pcrf->len) &&
		    (start + len) > pcrf->start)
			return pcrf;
	}

	return NULL;
}

struct pci_cfg_reg_filter *pci_virt_add_filter(struct pci_virt_device *pvd,
					       uint32_t start,
					       uint32_t len,
					       uint32_t flags,
					       pci_cfg_reg_func func,
					       void *data)
{
	struct pci_cfg_reg_filter *pcrf;

	if (!pvd || !len || (start + len) >= pvd->cfg_size)
		return NULL;
	if (!(flags & PCI_REG_FLAG_MASK))
		return NULL;

	pcrf = pci_virt_find_filter(pvd, start, len);
	if (pcrf) {
		prlog(PR_ERR, "%s: Filter [%x, %x] overlapped with [%x, %x]\n",
		      __func__, start, len, pcrf->start, pcrf->len);
		return NULL;
	}

	pcrf = zalloc(sizeof(*pcrf));
	if (!pcrf) {
		prlog(PR_ERR, "%s: Out of memory!\n", __func__);
		return NULL;
	}

	pcrf->start = start;
	pcrf->len   = len;
	pcrf->flags = flags;
	pcrf->func  = func;
	pcrf->data  = data;
	list_add_tail(&pvd->pcrf, &pcrf->link);

	return pcrf;
}

struct pci_virt_device *pci_virt_find_device(struct phb *phb,
					     uint32_t bdfn)
{
	struct pci_virt_device *pvd;

	list_for_each(&phb->virt_devices, pvd, node) {
		if (pvd->bdfn == bdfn)
			return pvd;
	}

	return NULL;
}

static inline bool pci_virt_cfg_valid(struct pci_virt_device *pvd,
				      uint32_t offset, uint32_t size)
{
	if ((offset + size) > pvd->cfg_size)
		return false;

	if (!size || (size > 4))
		return false;

	if ((size & (size - 1)) || (offset & (size - 1)))
		return false;

	return true;
}

int64_t pci_virt_cfg_read(struct phb *phb, uint32_t bdfn,
			  uint32_t offset, uint32_t size,
			  uint32_t *data)
{
	struct pci_virt_device *pvd;
	struct pci_cfg_reg_filter *pcrf;
	int64_t ret = OPAL_SUCCESS;

	*data = 0xffffffff;

	/* Search for PCI virtual device */
	pvd = pci_virt_find_device(phb, bdfn);
	if (!pvd)
		return OPAL_PARAMETER;

	/* Check if config address is valid or not */
	if (!pci_virt_cfg_valid(pvd, offset, size))
		return OPAL_PARAMETER;

	/* The value is fetched from the normal config space when the
	 * trap handler returns OPAL_PARTIAL. Otherwise, the trap handler
	 * should provide the return value.
	 */
	pcrf = pci_virt_find_filter(pvd, offset, size);
	if (!pcrf || !pcrf->func || !(pcrf->flags & PCI_REG_FLAG_READ))
		goto out;

	ret = pcrf->func(pvd, pcrf, offset, size, data, false);
	if (ret != OPAL_PARTIAL)
		return ret;
out:
	pci_virt_cfg_read_raw(pvd, PCI_VIRT_CFG_NORMAL, offset, size, data);
	return OPAL_SUCCESS;
}

int64_t pci_virt_cfg_write(struct phb *phb, uint32_t bdfn,
			   uint32_t offset, uint32_t size,
			   uint32_t data)
{
	struct pci_virt_device *pvd;
	struct pci_cfg_reg_filter *pcrf;
	uint32_t val, v, r, c, i;
	int64_t ret = OPAL_SUCCESS;

	/* Search for PCI virtual device */
	pvd = pci_virt_find_device(phb, bdfn);
	if (!pvd)
		return OPAL_PARAMETER;

	/* Check if config address is valid or not */
	if (!pci_virt_cfg_valid(pvd, offset, size))
		return OPAL_PARAMETER;

	/* The value is written to the config space if the trap handler
	 * returns OPAL_PARTIAL. Otherwise, the value to be written is
	 * dropped.
	 */
	pcrf = pci_virt_find_filter(pvd, offset, size);
	if (!pcrf || !pcrf->func || !(pcrf->flags & PCI_REG_FLAG_WRITE))
		goto out;

	ret = pcrf->func(pvd, pcrf, offset, size, &data, true);
	if (ret != OPAL_PARTIAL)
		return ret;
out:
	val = data;
	for (i = 0; i < size; i++) {
		PCI_VIRT_CFG_NORMAL_RD(pvd, offset + i, 1, &v);
		PCI_VIRT_CFG_RDONLY_RD(pvd, offset + i, 1, &r);
		PCI_VIRT_CFG_W1CLR_RD(pvd, offset + i, 1, &c);

		/* Drop read-only bits */
		val &= ~(r << (i * 8));
		val |= (r & v) << (i * 8);

		/* Drop W1C bits */
		val &= ~(val & ((c & v) << (i * 8)));
	}

	PCI_VIRT_CFG_NORMAL_WR(pvd, offset, size, val);
	return OPAL_SUCCESS;
}

struct pci_virt_device *pci_virt_add_device(struct phb *phb, uint32_t bdfn,
					    uint32_t cfg_size, void *data)
{
	struct pci_virt_device *pvd;
	uint8_t *cfg;
	uint32_t i;

	/* The standard config header size is 64 bytes */
	if (!phb || (bdfn & 0xffff0000) || (cfg_size < 64))
		return NULL;

	/* Check if the bdfn is available */
	pvd = pci_virt_find_device(phb, bdfn);
	if (pvd) {
		prlog(PR_ERR, "%s: bdfn 0x%x was reserved\n",
		      __func__, bdfn);
		return NULL;
	}

	/* Populate the PCI virtual device */
	pvd = zalloc(sizeof(*pvd));
	if (!pvd) {
		prlog(PR_ERR, "%s: Cannot alloate PCI virtual device (0x%x)\n",
		      __func__, bdfn);
		return NULL;
	}

	cfg = zalloc(cfg_size * PCI_VIRT_CFG_MAX);
	if (!cfg) {
		prlog(PR_ERR, "%s: Cannot allocate config space (0x%x)\n",
		      __func__, bdfn);
		free(pvd);
		return NULL;
	}

	for (i = 0; i < PCI_VIRT_CFG_MAX; i++, cfg += cfg_size)
		pvd->config[i] = cfg;

	pvd->bdfn     = bdfn;
	pvd->cfg_size = cfg_size;
	pvd->data     = data;
	list_head_init(&pvd->pcrf);
	list_add_tail(&phb->virt_devices, &pvd->node);

	return pvd;
}
OpenPOWER on IntegriCloud