netbsd_pci.c revision 2e46f441
1/*
2 * Copyright (c) 2008 Juan Romero Pardines
3 * Copyright (c) 2008 Mark Kettenis
4 * Copyright (c) 2009 Michael Lorenz
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/param.h>
20#include <sys/ioctl.h>
21#include <sys/mman.h>
22#include <sys/types.h>
23
24#ifdef HAVE_MTRR
25#include <machine/sysarch.h>
26#include <machine/mtrr.h>
27#define netbsd_set_mtrr(mr, num)	_X86_SYSARCH_L(set_mtrr)(mr, num)
28#endif
29
30#include <dev/pci/pcidevs.h>
31#include <dev/pci/pciio.h>
32#include <dev/pci/pcireg.h>
33
34#include <errno.h>
35#include <fcntl.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41
42#include <pci.h>
43#include <dev/wscons/wsconsio.h>
44
45#include "pciaccess.h"
46#include "pciaccess_private.h"
47
48typedef struct _pcibus {
49	int fd;		/* /dev/pci* */
50	int num;	/* bus number */
51	int maxdevs;	/* maximum number of devices */
52} PciBus;
53
54static PciBus buses[32];	/* indexed by pci_device.domain */
55static int nbuses = 0;		/* number of buses found */
56
57/*
58 * NetBSD's userland has a /dev/pci* entry for each bus but userland has no way
59 * to tell if a bus is a subordinate of another one or if it's on a different
60 * host bridge. On some architectures ( macppc for example ) all root buses have
61 * bus number 0 but on sparc64 for example the two roots in an Ultra60 have
62 * different bus numbers - one is 0 and the other 128.
63 * With each /dev/pci* we can map everything on the same root and we can also
64 * see all devices on the same root, trying to do that causes problems though:
65 * - since we can't tell which /dev/pci* is a subordinate we would find some
66 *   devices more than once
67 * - we would have to guess subordinate bus numbers which is a waste of time
68 *   since we can ask each /dev/pci* for its bus number so we can scan only the
69 *   buses we know exist, not all 256 which may exist in each domain.
70 * - some bus_space_mmap() methods may limit mappings to address ranges which
71 *   belong to known devices on that bus only.
72 * Each host bridge may or may not have its own IO range, to avoid guesswork
73 * here each /dev/pci* will let userland map its appropriate IO range at
74 * PCI_MAGIC_IO_RANGE if defined in <machine/param.h>
75 * With all this we should be able to use any PCI graphics device on any PCI
76 * bus on any architecture as long as Xorg has a driver, without allowing
77 * arbitrary mappings via /dev/mem and without userland having to know or care
78 * about translating bus addresses to physical addresses or the other way
79 * around.
80 */
81
82static int
83pci_read(int domain, int bus, int dev, int func, uint32_t reg, uint32_t *val)
84{
85	uint32_t rval;
86
87	if ((domain < 0) || (domain > nbuses))
88		return -1;
89
90	if (pcibus_conf_read(buses[domain].fd, (unsigned int)bus,
91	    (unsigned int)dev, (unsigned int)func, reg, &rval) == -1)
92		return (-1);
93
94	*val = rval;
95
96	return 0;
97}
98
99static int
100pci_write(int domain, int bus, int dev, int func, uint32_t reg, uint32_t val)
101{
102
103	if ((domain < 0) || (domain > nbuses))
104		return -1;
105
106	return pcibus_conf_write(buses[domain].fd, (unsigned int)bus,
107	    (unsigned int)dev, (unsigned int)func, reg, val);
108}
109
110static int
111pci_nfuncs(int domain, int bus, int dev)
112{
113	uint32_t hdr;
114
115	if ((domain < 0) || (domain > nbuses))
116		return -1;
117
118	if (pci_read(domain, bus, dev, 0, PCI_BHLC_REG, &hdr) != 0)
119		return -1;
120
121	return (PCI_HDRTYPE_MULTIFN(hdr) ? 8 : 1);
122}
123
124/*ARGSUSED*/
125static int
126pci_device_netbsd_map_range(struct pci_device *dev,
127    struct pci_device_mapping *map)
128{
129#ifdef HAVE_MTRR
130	struct mtrr m;
131	int n = 1;
132#endif
133	int prot, ret = 0;
134
135	prot = PROT_READ;
136
137	if (map->flags & PCI_DEV_MAP_FLAG_WRITABLE)
138		prot |= PROT_WRITE;
139	map->memory = mmap(NULL, (size_t)map->size, prot, MAP_SHARED,
140	    buses[dev->domain].fd, (off_t)map->base);
141	if (map->memory == MAP_FAILED)
142		return errno;
143
144#ifdef HAVE_MTRR
145	memset(&m, 0, sizeof(m));
146
147	/* No need to set an MTRR if it's the default mode. */
148	if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
149	    (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
150		m.base = map->base;
151		m.flags = MTRR_VALID | MTRR_PRIVATE;
152		m.len = map->size;
153		m.owner = getpid();
154		if (map->flags & PCI_DEV_MAP_FLAG_CACHABLE)
155			m.type = MTRR_TYPE_WB;
156		if (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)
157			m.type = MTRR_TYPE_WC;
158
159		if ((netbsd_set_mtrr(&m, &n)) == -1) {
160			fprintf(stderr, "mtrr set failed: %s\n",
161			    strerror(errno));
162		}
163	}
164#endif
165
166	return ret;
167}
168
169static int
170pci_device_netbsd_unmap_range(struct pci_device *dev,
171    struct pci_device_mapping *map)
172{
173#ifdef HAVE_MTRR
174	struct mtrr m;
175	int n = 1;
176
177	memset(&m, 0, sizeof(m));
178
179	if ((map->flags & PCI_DEV_MAP_FLAG_CACHABLE) ||
180	    (map->flags & PCI_DEV_MAP_FLAG_WRITE_COMBINE)) {
181		m.base = map->base;
182		m.flags = 0;
183		m.len = map->size;
184		m.type = MTRR_TYPE_UC;
185		(void)netbsd_set_mtrr(&m, &n);
186	}
187#endif
188
189	return pci_device_generic_unmap_range(dev, map);
190}
191
192static int
193pci_device_netbsd_read(struct pci_device *dev, void *data,
194    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_read)
195{
196	u_int reg, rval;
197
198	*bytes_read = 0;
199	while (size > 0) {
200		size_t toread = MIN(size, 4 - (offset & 0x3));
201
202		reg = (u_int)(offset & ~0x3);
203
204		if ((pcibus_conf_read(buses[dev->domain].fd,
205		    (unsigned int)dev->bus, (unsigned int)dev->dev,
206		    (unsigned int)dev->func, reg, &rval)) == -1)
207			return errno;
208
209		rval = htole32(rval);
210		rval >>= ((offset & 0x3) * 8);
211
212		memcpy(data, &rval, toread);
213
214		offset += toread;
215		data = (char *)data + toread;
216		size -= toread;
217		*bytes_read += toread;
218	}
219
220	return 0;
221}
222
223static int
224pci_device_netbsd_write(struct pci_device *dev, const void *data,
225    pciaddr_t offset, pciaddr_t size, pciaddr_t *bytes_written)
226{
227	u_int reg, val;
228
229	if ((offset % 4) != 0 || (size % 4) != 0)
230		return EINVAL;
231
232	*bytes_written = 0;
233	while (size > 0) {
234		reg = (u_int)offset;
235		memcpy(&val, data, 4);
236
237		if ((pcibus_conf_write(buses[dev->domain].fd,
238		    (unsigned int)dev->bus, (unsigned int)dev->dev,
239		    (unsigned int)dev->func, reg, val)) == -1)
240			return errno;
241
242		offset += 4;
243		data = (const char *)data + 4;
244		size -= 4;
245		*bytes_written += 4;
246	}
247
248	return 0;
249}
250
251static int
252pci_device_netbsd_boot_vga(struct pci_device *dev)
253{
254	int ret;
255	struct wsdisplayio_bus_id busid;
256	int fd;
257
258	fd = open("/dev/ttyE0", O_RDONLY);
259	if (fd == -1) {
260		fprintf(stderr, "failed to open /dev/ttyE0: %s\n",
261		    strerror(errno));
262		return 0;
263	}
264
265	ret = ioctl(fd, WSDISPLAYIO_GET_BUSID, &busid);
266	close(fd);
267	if (ret == -1) {
268		fprintf(stderr, "ioctl WSDISPLAYIO_GET_BUSID failed: %s\n",
269		    strerror(errno));
270		return 0;
271	}
272
273	if (busid.bus_type != WSDISPLAYIO_BUS_PCI)
274		return 0;
275
276	if (busid.ubus.pci.domain != dev->domain)
277		return 0;
278	if (busid.ubus.pci.bus != dev->bus)
279		return 0;
280	if (busid.ubus.pci.device != dev->dev)
281		return 0;
282	if (busid.ubus.pci.function != dev->func)
283		return 0;
284
285	return 1;
286}
287
288static void
289pci_system_netbsd_destroy(void)
290{
291	int i;
292
293	for (i = 0; i < nbuses; i++) {
294		close(buses[i].fd);
295	}
296	free(pci_sys);
297	pci_sys = NULL;
298}
299
300static int
301pci_device_netbsd_probe(struct pci_device *device)
302{
303	struct pci_device_private *priv =
304	    (struct pci_device_private *)(void *)device;
305	struct pci_mem_region *region;
306	uint64_t reg64, size64;
307	uint32_t bar, reg, size;
308	int bus, dev, func, err, domain;
309
310	domain = device->domain;
311	bus = device->bus;
312	dev = device->dev;
313	func = device->func;
314
315	/* Enable the device if necessary */
316	err = pci_read(domain, bus, dev, func, PCI_COMMAND_STATUS_REG, &reg);
317	if (err)
318		return err;
319	if ((reg & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) !=
320	    (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE)) {
321		reg |= PCI_COMMAND_IO_ENABLE |
322		       PCI_COMMAND_MEM_ENABLE |
323		       PCI_COMMAND_MASTER_ENABLE;
324		err = pci_write(domain, bus, dev, func, PCI_COMMAND_STATUS_REG,
325				reg);
326		if (err)
327			return err;
328	}
329
330	err = pci_read(domain, bus, dev, func, PCI_BHLC_REG, &reg);
331	if (err)
332		return err;
333
334	priv->header_type = PCI_HDRTYPE_TYPE(reg);
335	if (priv->header_type != 0)
336		return 0;
337
338	region = device->regions;
339	for (bar = PCI_MAPREG_START; bar < PCI_MAPREG_END;
340	     bar += sizeof(uint32_t), region++) {
341		err = pci_read(domain, bus, dev, func, bar, &reg);
342		if (err)
343			return err;
344
345		/* Probe the size of the region. */
346		err = pci_write(domain, bus, dev, func, bar, (unsigned int)~0);
347		if (err)
348			return err;
349		pci_read(domain, bus, dev, func, bar, &size);
350		pci_write(domain, bus, dev, func, bar, reg);
351
352		if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
353			region->is_IO = 1;
354			region->base_addr = PCI_MAPREG_IO_ADDR(reg);
355			region->size = PCI_MAPREG_IO_SIZE(size);
356		} else {
357			if (PCI_MAPREG_MEM_PREFETCHABLE(reg))
358				region->is_prefetchable = 1;
359			switch(PCI_MAPREG_MEM_TYPE(reg)) {
360			case PCI_MAPREG_MEM_TYPE_32BIT:
361			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
362				region->base_addr = PCI_MAPREG_MEM_ADDR(reg);
363				region->size = PCI_MAPREG_MEM_SIZE(size);
364				break;
365			case PCI_MAPREG_MEM_TYPE_64BIT:
366				region->is_64 = 1;
367
368				reg64 = reg;
369				size64 = size;
370
371				bar += sizeof(uint32_t);
372
373				err = pci_read(domain, bus, dev, func, bar, &reg);
374				if (err)
375					return err;
376				reg64 |= (uint64_t)reg << 32;
377
378				err = pci_write(domain, bus, dev, func, bar,
379				    (unsigned int)~0);
380				if (err)
381					return err;
382				pci_read(domain, bus, dev, func, bar, &size);
383				pci_write(domain, bus, dev, func, bar,
384				    (unsigned int)(reg64 >> 32));
385				size64 |= (uint64_t)size << 32;
386
387				region->base_addr =
388				    (unsigned long)PCI_MAPREG_MEM64_ADDR(reg64);
389				region->size =
390				    (unsigned long)PCI_MAPREG_MEM64_SIZE(size64);
391				region++;
392				break;
393			}
394		}
395	}
396
397	/* Probe expansion ROM if present */
398	err = pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &reg);
399	if (err)
400		return err;
401	if (reg != 0) {
402		err = pci_write(domain, bus, dev, func, PCI_MAPREG_ROM,
403		    (uint32_t)(~PCI_MAPREG_ROM_ENABLE));
404		if (err)
405			return err;
406		pci_read(domain, bus, dev, func, PCI_MAPREG_ROM, &size);
407		pci_write(domain, bus, dev, func, PCI_MAPREG_ROM, reg);
408		if ((reg & PCI_MAPREG_MEM_ADDR_MASK) != 0) {
409			priv->rom_base = reg & PCI_MAPREG_MEM_ADDR_MASK;
410			device->rom_size = -(size & PCI_MAPREG_MEM_ADDR_MASK);
411		}
412	}
413
414	return 0;
415}
416
417/**
418 * Read a VGA rom using the 0xc0000 mapping.
419 *
420 * This function should be extended to handle access through PCI resources,
421 * which should be more reliable when available.
422 */
423static int
424pci_device_netbsd_read_rom(struct pci_device *dev, void *buffer)
425{
426    struct pci_device_private *priv = (struct pci_device_private *)(void *)dev;
427    void *bios;
428    pciaddr_t rom_base;
429    size_t rom_size;
430    uint32_t bios_val, command_val;
431    int pci_rom;
432
433    if (((priv->base.device_class >> 16) & 0xff) != PCI_CLASS_DISPLAY ||
434	((priv->base.device_class >> 8) & 0xff) != PCI_SUBCLASS_DISPLAY_VGA)
435	return ENOSYS;
436
437    if (priv->rom_base == 0) {
438#if defined(__amd64__) || defined(__i386__)
439	/*
440	 * We need a way to detect when this isn't the console and reject
441	 * this request outright.
442	 */
443	rom_base = 0xc0000;
444	rom_size = 0x10000;
445	pci_rom = 0;
446#else
447	return ENOSYS;
448#endif
449    } else {
450	rom_base = priv->rom_base;
451	rom_size = dev->rom_size;
452	pci_rom = 1;
453	if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
454	    (unsigned int)dev->dev, (unsigned int)dev->func,
455	    PCI_COMMAND_STATUS_REG, &command_val)) == -1)
456	    return errno;
457	if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
458	    if ((pcibus_conf_write(buses[dev->domain].fd,
459	        (unsigned int)dev->bus, (unsigned int)dev->dev,
460		(unsigned int)dev->func, PCI_COMMAND_STATUS_REG,
461		command_val | PCI_COMMAND_MEM_ENABLE)) == -1)
462		return errno;
463	}
464	if ((pcibus_conf_read(buses[dev->domain].fd, (unsigned int)dev->bus,
465	    (unsigned int)dev->dev, (unsigned int)dev->func,
466	    PCI_MAPREG_ROM, &bios_val)) == -1)
467	    return errno;
468	if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
469	    if ((pcibus_conf_write(buses[dev->domain].fd,
470	        (unsigned int)dev->bus,
471		(unsigned int)dev->dev, (unsigned int)dev->func,
472		PCI_MAPREG_ROM, bios_val | PCI_MAPREG_ROM_ENABLE)) == -1)
473		return errno;
474	}
475    }
476
477    fprintf(stderr, "Using rom_base = 0x%lx 0x%lx (pci_rom=%d)\n",
478        (long)rom_base, (long)rom_size, pci_rom);
479
480    bios = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, buses[dev->domain].fd,
481        (off_t)rom_base);
482    if (bios == MAP_FAILED) {
483	int serrno = errno;
484	return serrno;
485    }
486
487    memcpy(buffer, bios, rom_size);
488
489    munmap(bios, rom_size);
490
491    if (pci_rom) {
492	if ((command_val & PCI_COMMAND_MEM_ENABLE) == 0) {
493	    if ((pcibus_conf_write(buses[dev->domain].fd,
494	        (unsigned int)dev->bus,
495		(unsigned int)dev->dev, (unsigned int)dev->func,
496		PCI_COMMAND_STATUS_REG, command_val)) == -1)
497		return errno;
498	}
499	if ((bios_val & PCI_MAPREG_ROM_ENABLE) == 0) {
500	    if ((pcibus_conf_write(buses[dev->domain].fd,
501	        (unsigned int)dev->bus,
502		(unsigned int)dev->dev, (unsigned int)dev->func,
503		PCI_MAPREG_ROM, bios_val)) == -1)
504		return errno;
505	}
506    }
507
508    return 0;
509}
510
511static const struct pci_system_methods netbsd_pci_methods = {
512	.destroy = pci_system_netbsd_destroy,
513	.destroy_device = NULL,
514	.read_rom = pci_device_netbsd_read_rom,
515	.probe = pci_device_netbsd_probe,
516	.map_range = pci_device_netbsd_map_range,
517	.unmap_range = pci_device_netbsd_unmap_range,
518	.read = pci_device_netbsd_read,
519	.write = pci_device_netbsd_write,
520	.fill_capabilities = pci_fill_capabilities_generic,
521	.boot_vga = pci_device_netbsd_boot_vga,
522};
523
524int
525pci_system_netbsd_create(void)
526{
527	struct pci_device_private *device;
528	int bus, dev, func, ndevs, nfuncs, domain, pcifd;
529	uint32_t reg;
530	char netbsd_devname[32];
531	struct pciio_businfo businfo;
532
533	pci_sys = calloc(1, sizeof(struct pci_system));
534
535	pci_sys->methods = &netbsd_pci_methods;
536
537	ndevs = 0;
538	nbuses = 0;
539	snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
540	pcifd = open(netbsd_devname, O_RDWR | O_CLOEXEC);
541	while (pcifd > 0) {
542		ioctl(pcifd, PCI_IOC_BUSINFO, &businfo);
543		buses[nbuses].fd = pcifd;
544		buses[nbuses].num = bus = businfo.busno;
545		buses[nbuses].maxdevs = businfo.maxdevs;
546		domain = nbuses;
547		nbuses++;
548		for (dev = 0; dev < businfo.maxdevs; dev++) {
549			nfuncs = pci_nfuncs(domain, bus, dev);
550			for (func = 0; func < nfuncs; func++) {
551				if (pci_read(domain, bus, dev, func, PCI_ID_REG,
552				    &reg) != 0)
553					continue;
554				if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
555				    PCI_VENDOR(reg) == 0)
556					continue;
557
558				ndevs++;
559			}
560		}
561		snprintf(netbsd_devname, 32, "/dev/pci%d", nbuses);
562		pcifd = open(netbsd_devname, O_RDWR);
563	}
564
565	pci_sys->num_devices = ndevs;
566	pci_sys->devices = calloc(ndevs, sizeof(struct pci_device_private));
567	if (pci_sys->devices == NULL) {
568		int i;
569
570		for (i = 0; i < nbuses; i++)
571			close(buses[i].fd);
572		free(pci_sys);
573		return ENOMEM;
574	}
575
576	device = pci_sys->devices;
577	for (domain = 0; domain < nbuses; domain++) {
578		bus = buses[domain].num;
579		for (dev = 0; dev < buses[domain].maxdevs; dev++) {
580			nfuncs = pci_nfuncs(domain, bus, dev);
581			for (func = 0; func < nfuncs; func++) {
582				if (pci_read(domain, bus, dev, func,
583				    PCI_ID_REG, &reg) != 0)
584					continue;
585				if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID ||
586				    PCI_VENDOR(reg) == 0)
587					continue;
588
589				device->base.domain = domain;
590				device->base.bus = bus;
591				device->base.dev = dev;
592				device->base.func = func;
593				device->base.vendor_id = PCI_VENDOR(reg);
594				device->base.device_id = PCI_PRODUCT(reg);
595
596				if (pci_read(domain, bus, dev, func,
597				    PCI_CLASS_REG, &reg) != 0)
598					continue;
599
600				device->base.device_class =
601				    PCI_INTERFACE(reg) | PCI_CLASS(reg) << 16 |
602				    PCI_SUBCLASS(reg) << 8;
603				device->base.revision = PCI_REVISION(reg);
604
605				if (pci_read(domain, bus, dev, func,
606				    PCI_SUBSYS_ID_REG, &reg) != 0)
607					continue;
608
609				device->base.subvendor_id = PCI_VENDOR(reg);
610				device->base.subdevice_id = PCI_PRODUCT(reg);
611
612				device++;
613			}
614		}
615	}
616
617	return 0;
618}
619