summaryrefslogtreecommitdiff
path: root/src/netbsd_pci.c
blob: c9bd052fa7813aea1d823fc6c326479e023b18e8 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
 * Copyright (c) 2008 Juan Romero Pardines
 * Copyright (c) 2008 Mark Kettenis
 * Copyright (c) 2009 Michael Lorenz
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/types.h>

#ifdef HAVE_MTRR
#include <machine/sysarch.h>
#include <machine/mtrr.h>
#define netbsd_set_mtrr(mr, num)	_X86_SYSARCH_L(set_mtrr)(mr, num)
#endif

#include <dev/pci/pcidevs.h>
#include <dev/pci/pciio.h>
#include <dev/pci/pcireg.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#include <pci.h>
#include <dev/wscons/wsconsio.h>

#include "pciaccess.h"
#include "pciaccess_private.h"

typedef struct _pcibus {
	int fd;		/* /dev/pci* */
	int num;	/* bus number */
	int maxdevs;	/* maximum number of devices */
} PciBus;

static PciBus buses[32];	/* indexed by pci_device.domain */
static int nbuses = 0;		/* number of buses found */

/*
 * NetBSD's userland has a /dev/pci* entry for each bus but userland has no way
 * to tell if a bus is a subordinate of another one or if it's on a different
 * host bridge. On some architectures ( macppc for example ) all root buses have
 * bus number 0 but on sparc64 for example the two roots in an Ultra60 have
 * different bus numbers - one is 0 and the other 128.
 * With each /dev/pci* we can map everything on the same root and we can also
 * see all devices on the same root, trying to do that causes problems though:
 * - since we can't tell which /dev/pci* is a subordinate we would find some
 *   devices more than once
 * - we would have to guess subordinate bus numbers which is a waste of time
 *   since we can ask each /dev/pci* for its bus number so we can scan only the
 *   buses we know exist, not all 256 which may exist in each domain.
 * - some bus_space_mmap() methods may limit mappings to address ranges which
 *   belong to known devices on that bus only.
 * Each host bridge may or may not have its own IO range, to avoid guesswork
 * here each /dev/pci* will let userland map its appropriate IO range at
 * PCI_MAGIC_IO_RANGE if defined in <machine/param.h>
 * With all this we should be able to use any PCI graphics device on any PCI
 * bus on any architecture as long as Xorg has a driver, without allowing
 * arbitrary mappings via /dev/mem and without userland having to know or care
 * about translating bus addresses to physical addresses or the other way
 * around.
 */

static int
pci_read(int domain, int bus, int dev, int func, uint32_t reg, uint32_t *val)
{
	uint32_t rval;

	if ((domain < 0) || (domain > nbuses))
		return -1;

	if (pcibus_conf_read(buses[domain].fd, (unsigned int)bus,
	    (unsigned int)dev, (unsigned int)func, reg, &rval) == -1)
		return (-1);

	*val = rval;

	return 0;
}

static int
pci_write(int domain, int bus, int dev, int func, uint32_t reg, uint32_t val)
{

	if ((domain < 0) || (domain > nbuses))
		return -1;

	return pcibus_conf_write(buses[domain].fd, (unsigned int)bus,
	    (unsigned int)dev, (unsigned int)func, reg, val);
}

static int
pci_nfuncs(int domain, int bus, int dev)
{
	uint32_t hdr;

	if ((domain < 0) || (domain > nbuses))
		return -1;

	if (pci_read(domain, bus, dev, 0, PCI_BHLC_REG, &hdr) != 0)
		return -1;

	return (PCI_HDRTYPE_MULTIFN(hdr) ? 8 : 1);
}

/*ARGSUSED*/
static int
pci_device_netbsd_map_range(struct pci_device *dev,
    struct pci_device_mapping *map)
{
#ifdef HAVE_MTRR
	struct mtrr m;
	int n = 1;
#endif
	int prot, ret = 0;

	prot = PROT_READ;

	if (map->flags & PCI_DEV_MAP_FLAG_WRITABLE)
		prot |= PROT_WRITE;
	map->memory = mmap(NULL, (size_t)map->size, prot, MAP_SHARED,
	    buses[dev->domain].fd, (off_t)map->base);
	if (map->memory == MAP_FAILED)
		return errno;

#ifdef HAVE_MTRR
	memset(&m, 0, sizeof(m));

	/* No need to set an MTRR if it's the default mode. */
	if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
	    (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
		m.base = map->base;
		m.flags = MTRR_VALID | MTRR_PRIVATE;
		m.len = map->size;
		m.owner = getpid();
		if (map->flags & PCI_DEV_MAP_FLAG_CACHABLE)
			m.type = MTRR_TYPE_WB;
		if (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)
			m.type = MTRR_TYPE_WC;

		if ((netbsd_set_mtrr(&m, &n)) == -1) {
			fprintf(stderr, "mtrr set failed: %s\n",
			    strerror(errno));
		}
	}
#endif

	return ret;
}

static int
pci_device_netbsd_unmap_range(struct pci_device *dev,
    struct pci_device_mapping *map)
{
#ifdef HAVE_MTRR
	struct mtrr m;
	int n = 1;

	memset(&m, 0, sizeof(m));

	if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
	    (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
		m.base = map->base;
		m.flags = 0;
		m.len = map->size;
		m.type = MTRR_TYPE_UC;
		(void)netbsd_set_mtrr(&m, &n);
	}
#endif

	return pci_device_generic_unmap_range(dev, map);
}

static int
pci_device_netbsd_read(struct pci_device *dev, void *data,
    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read)
{
	u_int reg, rval;

	*bytes_read = 0;
	while (size > 0) {
		size_t toread = MIN(size, 4 - (offset & 0x3));

		reg = (u_int)(offset & ~0x3);

		if ((pcibus_conf_read(buses[dev->domain].fd,
		    (unsigned int)dev->bus, (unsigned int)dev->dev,
		    (unsigned int)dev->func, reg, &rval)) == -1)
			return errno;

		rval = htole32(rval);
		rval >>= ((offset & 0x3) * 8);

		memcpy(data, &rval, toread);

		offset += toread;
		data = (char *)data + toread;
		size -= toread;
		*bytes_read += toread;
	}

	return 0;
}

static int
pci_device_netbsd_write(struct pci_device *dev, const void *data,
    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written)
{
	u_int reg, val;

	if ((offset % 4) != 0 || (size % 4) != 0)
		return EINVAL;

	*bytes_written = 0;
	while (size > 0) {
		reg = (u_int)offset;
		memcpy(&val, data, 4);

		if ((pcibus_conf_write(buses[dev->domain].fd,
		    (unsigned int)dev->bus, (unsigned int)dev->dev,
		    (unsigned int)dev->func, reg, val)) == -1)
			return errno;

		offset += 4;
		data = (const char *)data + 4;
		size -= 4;
		*bytes_written += 4;
	}

	return 0;
}

#if defined(WSDISPLAYIO_GET_BUSID)
static int
pci_device_netbsd_boot_vga(struct pci_device *dev)
{
	int ret;
	struct wsdisplayio_bus_id busid;
	int fd;

	fd = open("/dev/ttyE0", O_RDONLY);
	if (fd == -1) {
		fprintf(stderr, "failed to open /dev/ttyE0: %s\n",
		    strerror(errno));
		return 0;
	}

	ret = ioctl(fd, WSDISPLAYIO_GET_BUSID, &busid);
	close(fd);
	if (ret == -1) {
		fprintf(stderr, "ioctl WSDISPLAYIO_GET_BUSID failed: %s\n",
		    strerror(errno));
		return 0;
	}

	if (busid.bus_type != WSDISPLAYIO_BUS_PCI)
		return 0;

	if (busid.ubus.pci.domain != dev->domain)
		return 0;
	if (busid.ubus.pci.bus != dev->bus)
		return 0;
	if (busid.ubus.pci.device != dev->dev)
		return 0;
	if (busid.ubus.pci.function != dev->func)
		return 0;

	return 1;
}
#endif

static void
pci_system_netbsd_destroy(void)
{
	int i;

	for (i = 0; i < nbuses; i++) {
		close(buses[i].fd);
	}
	free(pci_sys);
	pci_sys = NULL;
}

static int
pci_device_netbsd_probe(struct pci_device *device)
{
	struct pci_device_private *priv =
	    (struct pci_device_private *)(void *)device;
	struct pci_mem_region *region;
	uint64_t reg64, size64;
	uint32_t bar, reg, size;
	int bus, dev, func, err, domain;

	domain = device->domain;
	bus = device->bus;
	dev = device->dev;
	func = device->func;

	/* Enable the device if necessary */
	err = pci_read(domain, bus, dev, func, PCI_COMMAND_STATUS_REG, &reg);
	if (err)
		return err;
	if ((reg & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) !=
	    (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
		reg |= PCI_COMMAND_IO_ENABLE |
		       PCI_COMMAND_MEM_ENABLE |
		       PCI_COMMAND_MASTER_ENABLE;
		err = pci_write(domain, bus, dev, func, PCI_COMMAND_STATUS_REG,
				reg);
		if (err)
			return err;
	}

	err = pci_read(domain, bus, dev, func, PCI_BHLC_REG, &reg);
	if (err)
		return err;

	priv->header_type = PCI_HDRTYPE_TYPE(reg);
	if (priv->header_type != 0)
		return 0;

	region = device->regions;
	for (bar = PCI_MAPREG_START; bar < PCI_MAPREG_END;
	     bar += sizeof(uint32_t), region++) {
		err = pci_read(domain, bus, dev, func, bar, &reg);
		if (err)
			return err;

		/* Probe the size of the region. */
		err = pci_write(domain, bus, dev, func, bar, (unsigned int)~0);
		if (err)
			return err;
		pci_read(domain, bus, dev, func, bar, &size);
		pci_write(domain, bus, dev, func, bar, reg);

		if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
			region->is_IO = 1;
			region->base_addr = PCI_MAPREG_IO_ADDR(reg);
			region->size = PCI_MAPREG_IO_SIZE(size);
		} else {
			if (PCI_MAPREG_MEM_PREFETCHABLE(reg))
				region->is_prefetchable = 1;
			switch(PCI_MAPREG_MEM_TYPE(reg)) {
			case PCI_MAPREG_MEM_TYPE_32BIT:
			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
				region->base_addr = PCI_MAPREG_MEM_ADDR(reg);
				region->size = PCI_MAPREG_MEM_SIZE(size);
				break;
			case PCI_MAPREG_MEM_TYPE_64BIT:
				region->is_64 = 1;

				reg64 = reg;
				size64 = size;

				bar += sizeof(uint32_t);

				err = pci_read(domain, bus, dev, func, bar, &reg);
				if (err)
					return err;
				reg64 |= (uint64_t)reg << 32;

				err = pci_write(domain, bus, dev, func, bar,
				    (unsigned int)~0);
				if (err)
					return err;
				pci_read(domain, bus, dev, func, bar, &size);
				pci_write(domain, bus, dev, func, bar,
				    (unsigned int)(reg64 >> 32));
				size64 |= (uint64_t)size << 32;

				region->base_addr =
				    (unsigned long)PCI_MAPREG_MEM64_ADDR(reg64);
				region->size =
				    (unsigned long)PCI_MAPREG_MEM64_SIZE(size64);
				region++;
				break;
			}
		}
	}

	/* Probe expansion ROM if present */
	err = pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &reg);
	if (err)
		return err;
	if (reg != 0) {
		err = pci_write(domain, bus, dev, func, PCI_MAPREG_ROM,
		    (uint32_t)(~PCI_MAPREG_ROM_ENABLE));
		if (err)
			return err;
		pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &size);
		pci_write(domain, bus, dev, func, PCI_MAPREG_ROM, reg);
		if ((reg & PCI_MAPREG_MEM_ADDR_MASK) != 0) {
			priv->rom_base = reg & PCI_MAPREG_MEM_ADDR_MASK;
			device->rom_size = -(size & PCI_MAPREG_MEM_ADDR_MASK);
		}
	}

	return 0;
}

/**
 * Read a VGA rom using the 0xc0000 mapping.
 *
 * This function should be extended to handle access through PCI resources,
 * which should be more reliable when available.
 */
static int
pci_device_netbsd_read_rom(struct pci_device *dev, void *buffer)
{
    struct pci_device_private *priv = (struct pci_device_private *)(void *)dev;
    void *bios;
    pciaddr_t rom_base;
    size_t rom_size;
    uint32_t bios_val, command_val;
    int pci_rom;

    if (((priv->base.device_class >> 16) & 0xff) != PCI_CLASS_DISPLAY ||
	((priv->base.device_class >> 8) & 0xff) != PCI_SUBCLASS_DISPLAY_VGA)
	return ENOSYS;

    if (priv->rom_base == 0) {
#if defined(__amd64__) || defined(__i386__)
	/*
	 * We need a way to detect when this isn't the console and reject
	 * this request outright.
	 */
	rom_base = 0xc0000;
	rom_size = 0x10000;
	pci_rom = 0;
#else
	return ENOSYS;
#endif
    } else {
	rom_base = priv->rom_base;
	rom_size = dev->rom_size;
	pci_rom = 1;
	if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
	    (unsigned int)dev->dev, (unsigned int)dev->func,
	    PCI_COMMAND_STATUS_REG, &command_val)) == -1)
	    return errno;
	if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
	    if ((pcibus_conf_write(buses[dev->domain].fd,
	        (unsigned int)dev->bus, (unsigned int)dev->dev,
		(unsigned int)dev->func, PCI_COMMAND_STATUS_REG,
		command_val | PCI_COMMAND_MEM_ENABLE)) == -1)
		return errno;
	}
	if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
	    (unsigned int)dev->dev, (unsigned int)dev->func,
	    PCI_MAPREG_ROM, &bios_val)) == -1)
	    return errno;
	if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
	    if ((pcibus_conf_write(buses[dev->domain].fd,
	        (unsigned int)dev->bus,
		(unsigned int)dev->dev, (unsigned int)dev->func,
		PCI_MAPREG_ROM, bios_val | PCI_MAPREG_ROM_ENABLE)) == -1)
		return errno;
	}
    }

    fprintf(stderr, "Using rom_base = 0x%lx 0x%lx (pci_rom=%d)\n",
        (long)rom_base, (long)rom_size, pci_rom);

    bios = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, buses[dev->domain].fd,
        (off_t)rom_base);
    if (bios == MAP_FAILED) {
	int serrno = errno;
	return serrno;
    }

    memcpy(buffer, bios, rom_size);

    munmap(bios, rom_size);

    if (pci_rom) {
	if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
	    if ((pcibus_conf_write(buses[dev->domain].fd,
	        (unsigned int)dev->bus,
		(unsigned int)dev->dev, (unsigned int)dev->func,
		PCI_COMMAND_STATUS_REG, command_val)) == -1)
		return errno;
	}
	if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
	    if ((pcibus_conf_write(buses[dev->domain].fd,
	        (unsigned int)dev->bus,
		(unsigned int)dev->dev, (unsigned int)dev->func,
		PCI_MAPREG_ROM, bios_val)) == -1)
		return errno;
	}
    }

    return 0;
}

static const struct pci_system_methods netbsd_pci_methods = {
	.destroy = pci_system_netbsd_destroy,
	.destroy_device = NULL,
	.read_rom = pci_device_netbsd_read_rom,
	.probe = pci_device_netbsd_probe,
	.map_range = pci_device_netbsd_map_range,
	.unmap_range = pci_device_netbsd_unmap_range,
	.read = pci_device_netbsd_read,
	.write = pci_device_netbsd_write,
	.fill_capabilities = pci_fill_capabilities_generic,
#if defined(WSDISPLAYIO_GET_BUSID)
	.boot_vga = pci_device_netbsd_boot_vga,
#else
	.boot_vga = NULL,
#endif
};

int
pci_system_netbsd_create(void)
{
	struct pci_device_private *device;
	int bus, dev, func, ndevs, nfuncs, domain, pcifd;
	uint32_t reg;
	char netbsd_devname[32];
	struct pciio_businfo businfo;

	pci_sys = calloc(1, sizeof(struct pci_system));

	pci_sys->methods = &netbsd_pci_methods;

	ndevs = 0;
	nbuses = 0;
	snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
	pcifd = open(netbsd_devname, O_RDWR | O_CLOEXEC);
	while (pcifd > 0) {
		ioctl(pcifd, PCI_IOC_BUSINFO, &businfo);
		buses[nbuses].fd = pcifd;
		buses[nbuses].num = bus = businfo.busno;
		buses[nbuses].maxdevs = businfo.maxdevs;
		domain = nbuses;
		nbuses++;
		for (dev = 0; dev < businfo.maxdevs; dev++) {
			nfuncs = pci_nfuncs(domain, bus, dev);
			for (func = 0; func < nfuncs; func++) {
				if (pci_read(domain, bus, dev, func, PCI_ID_REG,
				    &reg) != 0)
					continue;
				if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
				    PCI_VENDOR(reg) == 0)
					continue;

				ndevs++;
			}
		}
		snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
		pcifd = open(netbsd_devname, O_RDWR);
	}

	pci_sys->num_devices = ndevs;
	pci_sys->devices = calloc(ndevs, sizeof(struct pci_device_private));
	if (pci_sys->devices == NULL) {
		int i;

		for (i = 0; i < nbuses; i++)
			close(buses[i].fd);
		free(pci_sys);
		return ENOMEM;
	}

	device = pci_sys->devices;
	for (domain = 0; domain < nbuses; domain++) {
		bus = buses[domain].num;
		for (dev = 0; dev < buses[domain].maxdevs; dev++) {
			nfuncs = pci_nfuncs(domain, bus, dev);
			for (func = 0; func < nfuncs; func++) {
				if (pci_read(domain, bus, dev, func,
				    PCI_ID_REG, &reg) != 0)
					continue;
				if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
				    PCI_VENDOR(reg) == 0)
					continue;

				device->base.domain = domain;
				device->base.bus = bus;
				device->base.dev = dev;
				device->base.func = func;
				device->base.vendor_id = PCI_VENDOR(reg);
				device->base.device_id = PCI_PRODUCT(reg);

				if (pci_read(domain, bus, dev, func,
				    PCI_CLASS_REG, &reg) != 0)
					continue;

				device->base.device_class =
				    PCI_INTERFACE(reg) | PCI_CLASS(reg) << 16 |
				    PCI_SUBCLASS(reg) << 8;
				device->base.revision = PCI_REVISION(reg);

				if (pci_read(domain, bus, dev, func,
				    PCI_SUBSYS_ID_REG, &reg) != 0)
					continue;

				device->base.subvendor_id = PCI_VENDOR(reg);
				device->base.subdevice_id = PCI_PRODUCT(reg);

				device++;
			}
		}
	}

	return 0;
}