conf_space_header.c 8.5 KB
Newer Older
1 2 3 4 5 6
/*
 * PCI Backend - Handles the virtual fields in the configuration space headers.
 *
 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
 */

J
Joe Perches 已提交
7 8
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <linux/kernel.h>
#include <linux/pci.h>
#include "pciback.h"
#include "conf_space.h"

struct pci_bar_info {
	u32 val;
	u32 len_val;
	int which;
};

#define is_enable_cmd(value) ((value)&(PCI_COMMAND_MEMORY|PCI_COMMAND_IO))
#define is_master_cmd(value) ((value)&PCI_COMMAND_MASTER)

23 24 25 26 27
static int command_read(struct pci_dev *dev, int offset, u16 *value, void *data)
{
	int i;
	int ret;

28
	ret = xen_pcibk_read_config_word(dev, offset, value, data);
29
	if (!pci_is_enabled(dev))
30 31 32 33 34 35 36 37 38 39 40 41
		return ret;

	for (i = 0; i < PCI_ROM_RESOURCE; i++) {
		if (dev->resource[i].flags & IORESOURCE_IO)
			*value |= PCI_COMMAND_IO;
		if (dev->resource[i].flags & IORESOURCE_MEM)
			*value |= PCI_COMMAND_MEMORY;
	}

	return ret;
}

42 43
static int command_write(struct pci_dev *dev, int offset, u16 value, void *data)
{
44
	struct xen_pcibk_dev_data *dev_data;
45 46
	int err;

47
	dev_data = pci_get_drvdata(dev);
48 49
	if (!pci_is_enabled(dev) && is_enable_cmd(value)) {
		if (unlikely(verbose_request))
50
			printk(KERN_DEBUG DRV_NAME ": %s: enable\n",
51 52 53 54
			       pci_name(dev));
		err = pci_enable_device(dev);
		if (err)
			return err;
55 56
		if (dev_data)
			dev_data->enable_intx = 1;
57 58
	} else if (pci_is_enabled(dev) && !is_enable_cmd(value)) {
		if (unlikely(verbose_request))
59
			printk(KERN_DEBUG DRV_NAME ": %s: disable\n",
60 61
			       pci_name(dev));
		pci_disable_device(dev);
62 63
		if (dev_data)
			dev_data->enable_intx = 0;
64 65 66 67
	}

	if (!dev->is_busmaster && is_master_cmd(value)) {
		if (unlikely(verbose_request))
68
			printk(KERN_DEBUG DRV_NAME ": %s: set bus master\n",
69 70 71 72 73 74 75
			       pci_name(dev));
		pci_set_master(dev);
	}

	if (value & PCI_COMMAND_INVALIDATE) {
		if (unlikely(verbose_request))
			printk(KERN_DEBUG
76
			       DRV_NAME ": %s: enable memory-write-invalidate\n",
77 78 79
			       pci_name(dev));
		err = pci_set_mwi(dev);
		if (err) {
J
Joe Perches 已提交
80 81
			pr_warn("%s: cannot enable memory-write-invalidate (%d)\n",
				pci_name(dev), err);
82 83 84 85 86 87 88 89 90 91 92 93
			value &= ~PCI_COMMAND_INVALIDATE;
		}
	}

	return pci_write_config_word(dev, offset, value);
}

static int rom_write(struct pci_dev *dev, int offset, u32 value, void *data)
{
	struct pci_bar_info *bar = data;

	if (unlikely(!bar)) {
J
Joe Perches 已提交
94
		pr_warn(DRV_NAME ": driver data not found for %s\n",
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
		       pci_name(dev));
		return XEN_PCI_ERR_op_failed;
	}

	/* A write to obtain the length must happen as a 32-bit write.
	 * This does not (yet) support writing individual bytes
	 */
	if (value == ~PCI_ROM_ADDRESS_ENABLE)
		bar->which = 1;
	else {
		u32 tmpval;
		pci_read_config_dword(dev, offset, &tmpval);
		if (tmpval != bar->val && value == bar->val) {
			/* Allow restoration of bar value. */
			pci_write_config_dword(dev, offset, bar->val);
		}
		bar->which = 0;
	}

	/* Do we need to support enabling/disabling the rom address here? */

	return 0;
}

/* For the BARs, only allow writes which write ~0 or
 * the correct resource information
 * (Needed for when the driver probes the resource usage)
 */
static int bar_write(struct pci_dev *dev, int offset, u32 value, void *data)
{
	struct pci_bar_info *bar = data;

	if (unlikely(!bar)) {
J
Joe Perches 已提交
128
		pr_warn(DRV_NAME ": driver data not found for %s\n",
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
		       pci_name(dev));
		return XEN_PCI_ERR_op_failed;
	}

	/* A write to obtain the length must happen as a 32-bit write.
	 * This does not (yet) support writing individual bytes
	 */
	if (value == ~0)
		bar->which = 1;
	else {
		u32 tmpval;
		pci_read_config_dword(dev, offset, &tmpval);
		if (tmpval != bar->val && value == bar->val) {
			/* Allow restoration of bar value. */
			pci_write_config_dword(dev, offset, bar->val);
		}
		bar->which = 0;
	}

	return 0;
}

static int bar_read(struct pci_dev *dev, int offset, u32 * value, void *data)
{
	struct pci_bar_info *bar = data;

	if (unlikely(!bar)) {
J
Joe Perches 已提交
156
		pr_warn(DRV_NAME ": driver data not found for %s\n",
157 158 159 160 161 162 163 164 165 166 167 168 169
		       pci_name(dev));
		return XEN_PCI_ERR_op_failed;
	}

	*value = bar->which ? bar->len_val : bar->val;

	return 0;
}

static inline void read_dev_bar(struct pci_dev *dev,
				struct pci_bar_info *bar_info, int offset,
				u32 len_mask)
{
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	int	pos;
	struct resource	*res = dev->resource;

	if (offset == PCI_ROM_ADDRESS || offset == PCI_ROM_ADDRESS1)
		pos = PCI_ROM_RESOURCE;
	else {
		pos = (offset - PCI_BASE_ADDRESS_0) / 4;
		if (pos && ((res[pos - 1].flags & (PCI_BASE_ADDRESS_SPACE |
				PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
			   (PCI_BASE_ADDRESS_SPACE_MEMORY |
				PCI_BASE_ADDRESS_MEM_TYPE_64))) {
			bar_info->val = res[pos - 1].start >> 32;
			bar_info->len_val = res[pos - 1].end >> 32;
			return;
		}
	}

	bar_info->val = res[pos].start |
			(res[pos].flags & PCI_REGION_FLAG_MASK);
T
Thomas Meyer 已提交
189
	bar_info->len_val = resource_size(&res[pos]);
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
}

static void *bar_init(struct pci_dev *dev, int offset)
{
	struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);

	if (!bar)
		return ERR_PTR(-ENOMEM);

	read_dev_bar(dev, bar, offset, ~0);
	bar->which = 0;

	return bar;
}

static void *rom_init(struct pci_dev *dev, int offset)
{
	struct pci_bar_info *bar = kmalloc(sizeof(*bar), GFP_KERNEL);

	if (!bar)
		return ERR_PTR(-ENOMEM);

	read_dev_bar(dev, bar, offset, ~PCI_ROM_ADDRESS_ENABLE);
	bar->which = 0;

	return bar;
}

static void bar_reset(struct pci_dev *dev, int offset, void *data)
{
	struct pci_bar_info *bar = data;

	bar->which = 0;
}

static void bar_release(struct pci_dev *dev, int offset, void *data)
{
	kfree(data);
}

230
static int xen_pcibk_read_vendor(struct pci_dev *dev, int offset,
231 232 233 234 235 236 237
			       u16 *value, void *data)
{
	*value = dev->vendor;

	return 0;
}

238
static int xen_pcibk_read_device(struct pci_dev *dev, int offset,
239 240 241 242 243 244 245
			       u16 *value, void *data)
{
	*value = dev->device;

	return 0;
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
static int interrupt_read(struct pci_dev *dev, int offset, u8 * value,
			  void *data)
{
	*value = (u8) dev->irq;

	return 0;
}

static int bist_write(struct pci_dev *dev, int offset, u8 value, void *data)
{
	u8 cur_value;
	int err;

	err = pci_read_config_byte(dev, offset, &cur_value);
	if (err)
		goto out;

	if ((cur_value & ~PCI_BIST_START) == (value & ~PCI_BIST_START)
	    || value == PCI_BIST_START)
		err = pci_write_config_byte(dev, offset, value);

out:
	return err;
}

static const struct config_field header_common[] = {
272 273 274
	{
	 .offset    = PCI_VENDOR_ID,
	 .size      = 2,
275
	 .u.w.read  = xen_pcibk_read_vendor,
276 277 278 279
	},
	{
	 .offset    = PCI_DEVICE_ID,
	 .size      = 2,
280
	 .u.w.read  = xen_pcibk_read_device,
281
	},
282 283 284
	{
	 .offset    = PCI_COMMAND,
	 .size      = 2,
285
	 .u.w.read  = command_read,
286 287 288 289 290 291 292 293 294 295
	 .u.w.write = command_write,
	},
	{
	 .offset    = PCI_INTERRUPT_LINE,
	 .size      = 1,
	 .u.b.read  = interrupt_read,
	},
	{
	 .offset    = PCI_INTERRUPT_PIN,
	 .size      = 1,
296
	 .u.b.read  = xen_pcibk_read_config_byte,
297 298 299 300 301
	},
	{
	 /* Any side effects of letting driver domain control cache line? */
	 .offset    = PCI_CACHE_LINE_SIZE,
	 .size      = 1,
302 303
	 .u.b.read  = xen_pcibk_read_config_byte,
	 .u.b.write = xen_pcibk_write_config_byte,
304 305 306 307
	},
	{
	 .offset    = PCI_LATENCY_TIMER,
	 .size      = 1,
308
	 .u.b.read  = xen_pcibk_read_config_byte,
309 310 311 312
	},
	{
	 .offset    = PCI_BIST,
	 .size      = 1,
313
	 .u.b.read  = xen_pcibk_read_config_byte,
314 315 316 317 318
	 .u.b.write = bist_write,
	},
	{}
};

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
#define CFG_FIELD_BAR(reg_offset)			\
	{						\
	.offset     = reg_offset,			\
	.size       = 4,				\
	.init       = bar_init,				\
	.reset      = bar_reset,			\
	.release    = bar_release,			\
	.u.dw.read  = bar_read,				\
	.u.dw.write = bar_write,			\
	}

#define CFG_FIELD_ROM(reg_offset)			\
	{						\
	.offset     = reg_offset,			\
	.size       = 4,				\
	.init       = rom_init,				\
	.reset      = bar_reset,			\
	.release    = bar_release,			\
	.u.dw.read  = bar_read,				\
	.u.dw.write = rom_write,			\
	}
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

static const struct config_field header_0[] = {
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_2),
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_3),
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_4),
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_5),
	CFG_FIELD_ROM(PCI_ROM_ADDRESS),
	{}
};

static const struct config_field header_1[] = {
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_0),
	CFG_FIELD_BAR(PCI_BASE_ADDRESS_1),
	CFG_FIELD_ROM(PCI_ROM_ADDRESS1),
	{}
};

359
int xen_pcibk_config_header_add_fields(struct pci_dev *dev)
360 361 362
{
	int err;

363
	err = xen_pcibk_config_add_fields(dev, header_common);
364 365 366 367 368
	if (err)
		goto out;

	switch (dev->hdr_type) {
	case PCI_HEADER_TYPE_NORMAL:
369
		err = xen_pcibk_config_add_fields(dev, header_0);
370 371 372
		break;

	case PCI_HEADER_TYPE_BRIDGE:
373
		err = xen_pcibk_config_add_fields(dev, header_1);
374 375 376 377
		break;

	default:
		err = -EINVAL;
J
Joe Perches 已提交
378
		pr_err("%s: Unsupported header type %d!\n",
379 380 381 382 383 384 385
		       pci_name(dev), dev->hdr_type);
		break;
	}

out:
	return err;
}