Example #1
0
void TunersDB::initVendors()
{
    Tuners *tuners;
    struct Tuner tuner;
    struct pci_access *pacc;
    char devbuf[128];

    pacc = pci_alloc();

    for (int i = 0; card_tuners[i].tuner_name != NULL; i++) {

	QString vendor = (card_tuners[i].vendor_id == 0xffff ? i18n("Other") : 
			  pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_VENDOR, card_tuners[i].vendor_id, 0));

	tuner.tuner_id = card_tuners[i].tuner_id;
	tuner.tuner_name = card_tuners[i].tuner_name;

	if (!m_vendors.contains(vendor)) {
	    tuners = new Tuners();
	    m_vendors[vendor] = tuners;
	} else {
	    tuners = m_vendors[vendor];
	}

	tuners->push_back(tuner);
    }

    pci_cleanup(pacc);
}
Example #2
0
void CardsDB::addCards(struct card_info *card_infos, ChipSet chipset)
{
    Cards *cards;
    struct Card card;
    struct pci_access *pacc;
    char devbuf[128];

    pacc = pci_alloc();

    for (int i = 0; card_infos[i].card_name != NULL; i++) {

	QString vendor = (card_infos[i].vendor_id == 0xffff ? i18n("Other") : 
			  pci_lookup_name(pacc, devbuf, sizeof(devbuf), PCI_LOOKUP_VENDOR, card_infos[i].vendor_id, 0));

	card.card_id = card_infos[i].card_id;
	card.card_name = card_infos[i].card_name;
	card.chipset = chipset;

	if (!m_vendors.contains(vendor)) {
	    cards = new Cards();
	    m_vendors[vendor] = cards;
	} else {
	    cards = m_vendors[vendor];
	}

	cards->push_back(card);
    }

    pci_cleanup(pacc);
}
Example #3
0
int main(int argc, char *argv[])
{
	if (argc != 2) {
		fprintf(stderr, "Usage: %s <uio-dev>\n", argv[0]);
		return 1;
	}
	const int fd = open(argv[1], O_RDWR);
	if (fd < 0) {
		perror("Failed to open uio device");
		return 2;
	}
	ehci_bar =
		mmap(NULL, 1 << 8, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (MAP_FAILED == ehci_bar) {
		perror("Failed to map ehci bar");
		close(fd);
		return 3;
	}

	ioperm(0x80, 1, 1);

	pci_access = pci_alloc();
	pci_init(pci_access);

	usbdebug_init();

	pci_cleanup(pci_access);
	munmap(ehci_bar, 1 << 8);
	close(fd);
	return 0;
}
Example #4
0
/* --- implementations --- */
int detect_pci22(void){
  
  unsigned int c;
  char counter=0;

  
  pacc = pci_alloc();           /* Get the pci_access structure */
  
  pci_init(pacc);               /* Initialize the PCI library */
  pci_scan_bus(pacc);           /* We want to get the list of devices */


  /* Iterate over all PCI devices */
  for(pci22_dev=pacc->devices; pci22_dev; pci22_dev=pci22_dev->next){
    
    pci_fill_info(pci22_dev, PCI_FILL_IDENT | PCI_FILL_BASES); 
    
    // Detect the specified device
    if( (pci22_dev->vendor_id == PCI22_PCI_VENDOR_ID) && 
	(pci22_dev->device_id == PCI22_PCI_DEVICE_ID) 
	){
      break;
    }

  }
  if (pci22_dev==NULL){
    printf("\n\nERROR: PCI22 card not detected\n\n\n");
    exit(0);
  }


  printf("\n\nPCI22 card detected on %02x:%02x.%d\nVendorID=%04x DeviceID=%04x irq=%d\n",
	 pci22_dev->bus,pci22_dev->dev,
	 pci22_dev->func,pci22_dev->vendor_id,
	 pci22_dev->device_id,
	 pci22_dev->irq);


  printf("\n\n--- Baseaddresses ---\n");
  printf("BAR1 %x\n",pci22_dev->base_addr[0]);
  printf("BAR2 %x\n",pci22_dev->base_addr[1]);
  printf("BAR3 %x\n",pci22_dev->base_addr[2]);
  printf("BAR4 %x\n",pci22_dev->base_addr[3]);
  printf("BAR5 %x\n",pci22_dev->base_addr[4]);
  printf("BAR6 %x\n",pci22_dev->base_addr[6]);
  printf("\n");


  //printPCIDevice(pci22_dev);

/* The baseaddress of the COM20022 is at BAR2 */

//g_pci22dipswitch = pci22_dev->base_addr[1]; //Nonworking
g_pci22base = pci22_dev->base_addr[2];

  /* Close everything */
  pci_cleanup(pacc);

  return 0;
}
Example #5
0
File: pci.c Project: vmiklos/vmexam
int iface_desc(const char *iface, char *desc, int size)
{
	struct ifreq ifr;
	int fd, err, len, device, vendor;
	struct ethtool_drvinfo drvinfo;
	char buf[512], path[PATH_MAX];
	struct pci_access *pacc;

	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, iface);

	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0)
	{
		perror("Cannot get control socket");
		return 1;
	}
	drvinfo.cmd = ETHTOOL_GDRVINFO;
	ifr.ifr_data = (caddr_t)&drvinfo;
	err = ioctl(fd, SIOCETHTOOL, &ifr);
	if (err < 0)
	{
		perror("Cannot get driver information");
		printf("%d\n", errno);
		return 2;
	}
	close(fd);
	snprintf(path, PATH_MAX-1, "/sys/bus/pci/devices/%s/vendor", drvinfo.bus_info);
	fd = open(path, O_RDONLY);
	if (fd < 0)
	{
		perror("Cannot open the vendor file");
		return 3;
	}
	len = read(fd, buf, sizeof(buf));
	buf[len-1] = '\0';
	close(fd);
	sscanf(buf,"%X", &vendor);
	snprintf(path, PATH_MAX, "/sys/bus/pci/devices/%s/device", drvinfo.bus_info);
	fd = open(path, O_RDONLY);
	if (fd < 0)
	{
		perror("Cannot open the device file");
		return 3;
	}
	len = read(fd, buf, sizeof(buf));
	buf[len-1] = '\0';
	close(fd);
	sscanf(buf,"%X", &device);
	pacc = pci_alloc();
	pci_init(pacc);
	pci_lookup_name(pacc, desc, size,
			PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
			vendor, device);
	pci_cleanup(pacc);
	return(0);
}
Example #6
0
static int
pci_connect( device_t *igb_dev )
{
	struct  pci_access      *pacc;
	struct  pci_dev *dev;
	int             err;
	char    devpath[IGB_BIND_NAMESZ];

	memset( igb_dev, 0, sizeof(device_t));

	pacc    =       pci_alloc();
	pci_init(pacc);
	pci_scan_bus(pacc);
	for     (dev=pacc->devices;     dev;    dev=dev->next)
    {
		pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
      
		igb_dev->pci_vendor_id = dev->vendor_id;
		igb_dev->pci_device_id = dev->device_id;
		igb_dev->domain = dev->domain;
		igb_dev->bus = dev->bus;
		igb_dev->dev = dev->dev;
		igb_dev->func = dev->func;
		
		snprintf
			(devpath, IGB_BIND_NAMESZ, "%04x:%02x:%02x.%d", dev->domain,
			 dev->bus, dev->dev, dev->func );

		err = igb_probe( igb_dev );
      
		if (err) {
			continue;
		}

		printf ("attaching to %s\n", devpath);
		err = igb_attach( devpath, igb_dev );
      
		if (err) {
			printf ("attach failed! (%s)\n", strerror(errno));
			continue;
		}
		goto out;
    }

	pci_cleanup(pacc);
	return  ENXIO;
  
out:
	pci_cleanup(pacc);
	return  0;
}
Example #7
0
int pci_init_common(void)
{
	if (pacc != NULL) {
		msg_perr("%s: Tried to allocate a new PCI context, but there is still an old one!\n"
			 "Please report a bug at [email protected]\n", __func__);
		return 1;
	}
	pacc = pci_alloc();     /* Get the pci_access structure */
	pci_init(pacc);         /* Initialize the PCI library */
	if (register_shutdown(pcidev_shutdown, NULL))
		return 1;
	pci_scan_bus(pacc);     /* We want to get the list of devices */
	return 0;
}
Example #8
0
void vbetool_init(void)
{
	if (!LRMI_init()) {
		fprintf(stderr, "Failed to initialise LRMI (Linux Real-Mode Interface).\n");
		exit(1);
	}

	ioperm(0, 1024, 1);
	iopl(3);

	pacc = pci_alloc();
	pacc->numeric_ids = 1;
	pci_init(pacc);
}
Example #9
0
int main (int argc, char** argv) {
	int vendor = (int)strtol(argv[1], NULL, 16);
	int device = (int)strtol(argv[2], NULL, 16);

	char buf[512];
	pci_access* acc = pci_alloc();
	acc->numeric_ids = 0;
	acc->debugging = 0;
	acc->writeable = 0;
	acc->id_file_name = PCI_PATH_IDS;
	pci_init (acc);
	printf ("%s\n", pci_lookup_name(acc, buf,  512, PCI_LOOKUP_DEVICE, vendor, device));

	pci_cleanup(acc);
	return 0;
}
Example #10
0
int
main(int argc, char **argv)
{
  int i;

  pacc = pci_alloc();
  pacc->error = die;
  i = parse_options(argc, argv);

  pci_init(pacc);
  pci_scan_bus(pacc);

  parse_ops(argc, argv, i);
  scan_ops(first_op);
  execute(first_op);

  return 0;
}
Example #11
0
int create_pci(int method, struct pci_access **pci_out)  {
  struct pci_access *pci = pci_alloc();
  pci->method = method;
  pci_init(pci);
  pci_scan_bus(pci);

  struct pci_dev *d31f0 = pci_find_dev(pci, 0, 31, 0);
  if(!d31f0) {
    ERR("Cannot find D31:F0");
  }

  pci_fill_info(d31f0, PCI_FILL_IDENT | PCI_FILL_BASES);
  if(d31f0->vendor_id != 0x8086) {
    ERR("Vendor of D31:F0 is not Intel");
  }

  *pci_out = pci;
  return 0;
}
Example #12
0
/*
 * pci_acc_init
 *
 * PCI access helper function depending on libpci
 *
 * **pacc : if a valid pci_dev is returned
 *         *pacc must be passed to pci_acc_cleanup to free it
 *
 * domain: domain
 * bus:    bus
 * slot:   slot
 * func:   func
 * vendor: vendor
 * device: device
 * Pass -1 for one of the six above to match any
 *
 * Returns :
 * struct pci_dev which can be used with pci_{read,write}_* functions
 *                to access the PCI config space of matching pci devices
 */
struct pci_dev *pci_acc_init(struct pci_access **pacc, int domain, int bus,
			     int slot, int func, int vendor, int dev)
{
	struct pci_filter filter_nb_link = { domain, bus, slot, func,
					     vendor, dev };
	struct pci_dev *device;

	*pacc = pci_alloc();
	if (*pacc == NULL)
		return NULL;

	pci_init(*pacc);
	pci_scan_bus(*pacc);

	for (device = (*pacc)->devices; device; device = device->next) {
		if (pci_filter_match(&filter_nb_link, device))
			return device;
	}
	pci_cleanup(*pacc);
	return NULL;
}
Example #13
0
int show_pci(void)
{
  struct pci_access *pacc;
  struct pci_dev *dev;
  unsigned int c;

  pacc = pci_alloc();		/* Get the pci_access structure */
  /* Set all options you want -- here we stick with the defaults */
  pacc->debugging=1;
  pci_init(pacc);		/* Initialize the PCI library */
  pci_scan_bus(pacc);		/* We want to get the list of devices */
  for(dev=pacc->devices; dev; dev=dev->next)	/* Iterate over all devices */
    {
      pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);	/* Fill in header info we need */
      c = pci_read_word(dev, PCI_CLASS_DEVICE);	/* Read config register directly */
      printf("%02x:%02x.%d vendor=%04x device=%04x class=%04x irq=%d base0=%lx\n",
	     dev->bus, dev->dev, dev->func, dev->vendor_id, dev->device_id,
	     c, dev->irq, dev->base_addr[0]);
    }
  pci_cleanup(pacc);		/* Close everything */
  return 0;
}
nsresult nsSystemInfo::Init()
{
    struct pci_access *pacc;
    struct pci_dev *p;
    pciaddr_t ram = 0;
    char buf[128];
    int i, found, v, n;
    Display *dpy;
    char *display = NULL;
    Window root;
    XVisualInfo *info, templ;
    XWindowAttributes wts;
    XPixmapFormatValues *pf;
    XSetWindowAttributes attr;
    Window win;

    mWidth = 0;
    mHeight = 0;
    mDepth = 0;

    pacc = pci_alloc();
    pci_init(pacc);

    pci_scan_bus(pacc);
    for (p = pacc->devices; p; p=p->next) {
        pci_fill_info(p,
                      PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_BASES | PCI_FILL_SIZES);
        if (p->device_class == PCI_CLASS_DISPLAY_VGA) {
            pci_lookup_name(pacc, buf, sizeof(buf),
                            PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
                            p->vendor_id, p->device_id);
            mDeviceName.AssignLiteral(buf);
            sprintf(buf, "0x%04X", p->vendor_id);
            mVendorID.AssignLiteral(buf);
            sprintf(buf, "0x%04X", p->device_id);
            mDeviceID.AssignLiteral(buf);
            for (i=0; i<6; i++) {
                pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
                if (len > ram) ram = len;
            }
            vram = ram / 1024 / 1024;
        }
        else {
            Log("Grafx Bot: No PCI VGA device found");
        }
    }

    pci_cleanup(pacc);

    if (NULL != (display = getenv("DISPLAY"))) {
        if (display[0] != ':') {
            display = strchr(display, ':');
            if (NULL == display) {
                Log("Grafx Bot: unable to find display");
                return NS_OK;
            }
        }
        if (NULL == (dpy = XOpenDisplay(display))) {
            Log("Grafx Bot: unable to find X display");
            return NS_OK;
        }
        root = DefaultRootWindow(dpy);
        XGetWindowAttributes(dpy, root, &wts);
        mWidth = wts.width;
        mHeight = wts.height;

        templ.screen = XDefaultScreen(dpy);
        info = XGetVisualInfo(dpy, VisualScreenMask, &templ, &found);
        v = -1;
        for (i = 0; v == -1 && i < found; i++) {
            if (info[i].depth >= 15)
                v = i;
        }
        for (i = 0; v == -1 && i < found; i++) {
            if (info[i].depth == 8)
                v = i;
        }
        if (-1 == v) {
            Log("Grafx Bot: can't find visual");
            return NS_OK;
        }

        pf = XListPixmapFormats(dpy, &n);
        for (i = 0; i < n; i++) {
            if (pf[i].depth == info[v].depth) {
                mDepth = pf[i].depth;
            }
        }

        if (gGLXWrap.OpenLibrary("libGL.so.1") && gGLXWrap.Init()) {
            attr.background_pixel = 0;
            attr.border_pixel = 0;
            attr.colormap  = XCreateColormap(dpy, root, info[v].visual, AllocNone);
            attr.event_mask = StructureNotifyMask | ExposureMask;
            win = XCreateWindow(dpy, root, 0, 0, 100, 100, 0, info[v].depth,
                                InputOutput, info[v].visual,
                                CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &attr);
            GLXContext ctx = gGLXWrap.fCreateContext(dpy, info, NULL, true);
            if (ctx) {
                if (gGLXWrap.fMakeCurrent(dpy, win, ctx)) {
                    mDriverVersion.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VERSION));
                    mDriver.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_RENDERER));
                    mDriver.AppendLiteral(" (");
                    mDriver.AppendLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VENDOR));
                    mDriver.AppendLiteral(")");
                }
                else {
                    Log("Grafx Bot: unable to make current");
                }
                gGLXWrap.fDestroyContext(dpy, ctx);
            }
            else {
                Log("Grafx Bot: unable to create context");
            }
            XDestroyWindow(dpy, win);
        }
        else {
            Log("Grafx Bot: can't init libGL.so.1");
        }
    }

    return NS_OK;
}
Example #15
0
int main (int argc, char *argv[])
{
	struct pci_access *pacc;
	struct pci_dev *dev;
	struct pci_filter filter;
	char *msg;
	uint16_t command;
	int v1_1 = 0, i2c;

	if (argc != 2 &&
	    ((argc != 3) ||
	     (!(v1_1 = !strcmp(argv[2], "1.1")) && strcmp(argv[2], "1.0")))) {
		fprintf(stderr,
			"VT6307 OHCI mode config\n"
			"Version 0.9\n"
			"Copyright (C) 2007 Krzysztof Halasa <*****@*****.**>\n"
			"\n"
			"Usage: vt6307ohciver <pci_device> [ 1.0 | 1.1 ]\n");
		exit(1);
	}

	if (iopl(3)) {
		fprintf(stderr, "iopl() failed (must be root)\n");
		exit(1);
	}

	pacc = pci_alloc();
	pci_filter_init(pacc, &filter);
	if ((msg = pci_filter_parse_slot(&filter, argv[1]))) {
		fprintf(stderr, "Invalid pci_device\n");
		exit(1);
	}

	filter.vendor = VENDOR_ID;
	filter.device = DEVICE_ID;

	pci_init(pacc);
	pci_scan_bus(pacc);

	for (dev = pacc->devices; dev; dev = dev->next)
		if (pci_filter_match(&filter, dev))
			break;

	if (!dev) {
		fprintf(stderr, "Device %s not found\n", argv[2]);
		exit(1);
	}

	pci_fill_info(dev, PCI_FILL_BASES | PCI_FILL_SIZES);

	if (dev->size[0] != MEM_SIZE || dev->size[1] != IO_SIZE) {
		fprintf(stderr, "Unexpected MEM/IO region size, is it"
			" VT6307 chip?\n");
		exit(1);
	}

	command = pci_read_word(dev, PCI_COMMAND);

	if ((command & PCI_COMMAND_IO) == 0) {
		fprintf(stderr, "Device disabled, trying to enable it\n");
		pci_write_word(dev, PCI_COMMAND, command | PCI_COMMAND_IO);
	}

	io_ports = dev->base_addr[1] & PCI_BASE_ADDRESS_IO_MASK;

	i2c = (inl(io_ports + 0x20) & 0x80) ? 0 : 1;

	fprintf(stderr, "I/O region #1 is at %04X\n", io_ports);
	fprintf(stderr, "It seems your VT6307 chip is connected to %s "
		"EEPROM\n", i2c ? "I^2C (24c01 or similar)" : "93c46");

	if (argc == 3) {
		/* enable direct access to pins */
		outl_p(inl(io_ports) | 0x80, io_ports);
		if (i2c)
			write_i2c(0x22, v1_1 ? 8 : 0);
		else
			write_4w(0x11, v1_1 ? 8 : 0);
		/* disable direct access to pins */
		outl_p(0x20, io_ports + 0x20);
		fprintf(stderr, "Please reboot\n");
	} else
		display(dev->base_addr[0] & PCI_BASE_ADDRESS_MEM_MASK);

	if ((command & PCI_COMMAND_IO) == 0) {
		fprintf(stderr, "Disabling device\n");
		pci_write_word(dev, PCI_COMMAND, command);
	}

	exit(0);
}
Example #16
0
int main(int argc, char *argv[])
{
	bool has_intel = 0;
	bool has_nvidia = 0;
	bool abi_compat = 0;
	int ret = 0;
	int c = 0;

	while ((c = getopt_long(argc, argv, "lxVh", longopts, 0)) != EOF)
		switch (c) {
		case 'l':
			opt_list = true;
			break;
		case 'x':
			opt_xorg = true;
			break;
		case 'V':
			version();
			exit(0);
		case 'h':
			usage();
			exit(0);
		default:
			usage();
			exit(0);
		}

	pacc = pci_alloc();		/* Get the pci_access structure */
	pci_init(pacc);			/* Initialize the PCI library */

	if (opt_list) {
		list_all_nvidia_devices();
		goto exit;
	}

	pci_scan_bus(pacc);		/* Scan the bus for devices */

	printf("Probing for supported NVIDIA devices...\n");

	/* Iterate over all devices */
	for (dev=pacc->devices; dev; dev=dev->next) {

		if (!dev->device_class) {
			fprintf(stderr, "Error getting device_class\n");
			ret = -1;
			goto exit;
		}

		if ((dev->device_class & 0xff00) == 0x0300) {

			/* Get the name of the device */
			name = pci_lookup_name(pacc, namebuf, sizeof(namebuf),
				PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
				dev->vendor_id, dev->device_id);

			printf("[%04x:%04x] %s\n",
				dev->vendor_id, dev->device_id, name);

			/* Find NVIDIA device */
			if (dev->vendor_id == PCI_VENDOR_ID_NVIDIA) {
				has_nvidia = true;
				ret = nv_lookup_device_id(dev->device_id);
			}

			/* 
			 * Find Intel device for simplistic detection
			 * of Optimus hardware configurations
			 */
			if (dev->vendor_id == PCI_VENDOR_ID_INTEL)
				has_intel = true;

		}	/* End of device_class */

	}		/* End iteration of devices */

	/* Check Xorg ABI compatibility */
	if (ret > 0) {
		if (opt_xorg)
			printf("\nChecking ABI compatibility with Xorg Server...\n");

		abi_compat = check_xorg_abi_compat(ret);

		if (!abi_compat)
			printf("WARNING: The driver for this device "
			"does not support the current Xorg version\n");
		else
			if (opt_xorg)
				printf("ABI compatibility check passed\n"); 
	}

	/* Check for Optimus hardware */
	if (has_intel && has_nvidia)
		has_optimus();

	/* Catch cases where no NVIDIA devices were detected */
	if (!has_nvidia)
		printf("No NVIDIA devices were found.\n");

exit:
	pci_cleanup(pacc);	/* Close everything */

	exit(ret);
}
Example #17
0
int main(int argc, char *argv[]) {
	char c;
	int ret = 1;
	const struct sysdef *s;
	const struct targetdef *t;
	uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
	uint32_t addr = 0;
	const char *streamfn = NULL, *difffn = NULL;
	struct msr msrval = MSR2(-1, -1);
	while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
		switch (c) {
		case 'h':
			syntax(argv);
			return 0;
		case 'q':
			quiet = 1;
			break;
		case 'v':
			++verbose;
			break;
		case 'r':
			reserved = 1;
			break;
		case 'k':
			listknown = 1;
			break;
		case 'l':
			listmsrs = 1;
			break;
		case 'c':
			cpu = atoi(optarg);
			break;
		case 'm':
			for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
				if (!strcmp(s->name, optarg)) {
					sys = s;
					break;
				}
			break;
		case 't':
			for (t = alltargets; !TARGET_ISEOT(*t); t++)
				if (!strcmp(t->name, optarg)) {
					add_target(t);
					break;
				}
			break;
		case 'i':
			input = 1;
			addr = msraddrbyname(optarg);
			optarg = strchr(optarg, '=');
			if (NULL == optarg) {
				fprintf(stderr, "missing value in -i argument!\n");
				break;
			}
			if (!str2msr(++optarg, &msrval, NULL))
				fprintf(stderr, "invalid value in -i argument!\n");
			break;
		case 's':
			streamfn = optarg;
			break;
		case 'd':
			difffn = optarg;
			break;
		default:
			break;
		}

	printf_quiet("msrtool %s\n", VERSION);

	pacc = pci_alloc();
	if (NULL == pacc) {
		fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
		return 1;
	}
	pci_init(pacc);
	pci_scan_bus(pacc);

	if (!sys && !input && !listknown)
		for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
			printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
			if (!sys->probe(sys))
				continue;
			printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
			break;
		}

	if (targets)
		for (tn = 0; tn < targets_found; tn++)
			printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
	else
		for (t = alltargets; !TARGET_ISEOT(*t); t++) {
			printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
			if (!t->probe(t))
				continue;
			printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
			add_target(t);
		}

	printf_quiet("\n");
	fflush(stdout);

	if (listknown) {
		printf("Known systems:\n");
		for (s = allsystems; s->name; s++)
			printf("%s: %s\n", s->name, s->prettyname);
		printf("\nKnown targets:\n");
		for (t = alltargets; t->name; t++) {
			if (listmsrs && alltargets != t)
				printf("\n");
			printf("%s: %s\n", t->name, t->prettyname);
			if (listmsrs)
				dumpmsrdefs(t);
		}
		printf("\n");
		return 0;
	}

	if (!targets_found || !targets) {
		fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
		fprintf(stderr, "Please send a report or patch to [email protected]. Thanks for your help!\n");
		fprintf(stderr, "\n");
		return 1;
	}

	if (input) {
		decodemsr(cpu, addr, msrval);
		return 0;
	}

	if (listmsrs) {
		if (streamfn)
			return do_stream(streamfn, 1);
		for (tn = 0; tn < targets_found; tn++) {
			if (tn)
				printf("\n");
			dumpmsrdefs(targets[tn]);
		}
		printf("\n");
		return 0;
	}

	if (streamfn)
		return do_stream(streamfn, 0);

	if (difffn) {
		ret = do_diff(difffn);
		goto done;
	}

	if (optind == argc) {
		syntax(argv);
		printf("\nNo mode or address(es) specified!\n");
		goto done;
	}

	if (!found_system())
		return 1;
	if (!sys->open(cpu, SYS_RDONLY))
		return 1;

	for (; optind < argc; optind++) {
		addr = msraddrbyname(argv[optind]);
		if (!sys->rdmsr(cpu, addr, &msrval))
			break;
		decodemsr(cpu, addr, msrval);
	}
	ret = 0;
done:
	sys->close(cpu);
	return ret;
}
int
main(int argc, char* argv[])
{
    int err = EXIT_FAILURE;

    struct cmd_line cmd_line;

    setlocale(LC_ALL, "");

    if (parse_cmd_line(argc, argv, &cmd_line) < 0)
        goto arg_check_failed;

    struct pci_access * pci = pci_alloc();
    if (! pci)
        goto pci_alloc_failed;

    /* This access bypass the kernel and use a memory mapping
     * to PCI configuration registers */
    pci->method = PCI_ACCESS_I386_TYPE1;

    struct pci_dev * dev = create_pci_dev(pci, cmd_line.slot);
    if (! dev)
        goto create_pci_dev_failed;

    print_device_name(pci, dev);

    unsigned long * timestamps
            = malloc(sizeof(*timestamps) * cmd_line.iteration_count);
    if (! timestamps)
    {
        fprintf(stderr, "Can't allocate timestamp storage (%s)\n",
                strerror(errno));
        goto malloc_failed;
    }

    struct timestamp t;
    read_timestamp_counter(&t);

    perform_reads(dev,
                  timestamps,

                  cmd_line.iteration_count,
                  cmd_line.wait_time_us);

    unsigned long test_duration_cycles = cycle_since_timestamp(&t);

    double cpu_mhz = get_cpu_mhz();
    if (cpu_mhz < 0)
        goto get_cpu_mhz_failed;

    print_results(cpu_mhz,
                  timestamps,
                  cmd_line.iteration_count,
                  test_duration_cycles,
                  cmd_line.limit_ns);


    err = EXIT_SUCCESS;

get_cpu_mhz_failed:
    free(timestamps);
malloc_failed:
    pci_free_dev(dev);
create_pci_dev_failed:
    pci_cleanup(pci);
pci_alloc_failed:
arg_check_failed:
    return err;
}
Example #19
0
static int
hwloc_look_pci(struct hwloc_backend *backend)
{
  struct hwloc_topology *topology = backend->topology;
  struct hwloc_obj *first_obj = NULL, *last_obj = NULL;
#ifdef HWLOC_HAVE_LIBPCIACCESS
  int ret;
  struct pci_device_iterator *iter;
  struct pci_device *pcidev;
#else /* HWLOC_HAVE_PCIUTILS */
  struct pci_access *pciaccess;
  struct pci_dev *pcidev;
#endif

  if (!(hwloc_topology_get_flags(topology) & (HWLOC_TOPOLOGY_FLAG_IO_DEVICES|HWLOC_TOPOLOGY_FLAG_WHOLE_IO)))
    return 0;

  if (hwloc_get_next_pcidev(topology, NULL)) {
    hwloc_debug("%s", "PCI objects already added, ignoring pci backend.\n");
    return 0;
  }

  if (!hwloc_topology_is_thissystem(topology)) {
    hwloc_debug("%s", "\nno PCI detection (not thissystem)\n");
    return 0;
  }

  hwloc_debug("%s", "\nScanning PCI buses...\n");

  /* initialize PCI scanning */
#ifdef HWLOC_HAVE_LIBPCIACCESS
  ret = pci_system_init();
  if (ret) {
    hwloc_debug("%s", "Can not initialize libpciaccess\n");
    return -1;
  }

  iter = pci_slot_match_iterator_create(NULL);
#else /* HWLOC_HAVE_PCIUTILS */
  pciaccess = pci_alloc();
  pciaccess->error = hwloc_pci_error;
  pciaccess->warning = hwloc_pci_warning;

  if (setjmp(err_buf)) {
    pci_cleanup(pciaccess);
    return -1;
  }

  pci_init(pciaccess);
  pci_scan_bus(pciaccess);
#endif

  /* iterate over devices */
#ifdef HWLOC_HAVE_LIBPCIACCESS
  for (pcidev = pci_device_next(iter);
       pcidev;
       pcidev = pci_device_next(iter))
#else /* HWLOC_HAVE_PCIUTILS */
  for (pcidev = pciaccess->devices;
       pcidev;
       pcidev = pcidev->next)
#endif
  {
    const char *vendorname, *devicename, *fullname;
    unsigned char config_space_cache[CONFIG_SPACE_CACHESIZE];
    struct hwloc_obj *obj;
    unsigned os_index;
    unsigned domain;
    unsigned device_class;
    unsigned short tmp16;
    char name[128];
    unsigned offset;
#ifdef HWLOC_HAVE_PCI_FIND_CAP
    struct pci_cap *cap;
#endif

    /* initialize the config space in case we fail to read it (missing permissions, etc). */
    memset(config_space_cache, 0xff, CONFIG_SPACE_CACHESIZE);
#ifdef HWLOC_HAVE_LIBPCIACCESS
    pci_device_probe(pcidev);
    pci_device_cfg_read(pcidev, config_space_cache, 0, CONFIG_SPACE_CACHESIZE, NULL);
#else /* HWLOC_HAVE_PCIUTILS */
    pci_read_block(pcidev, 0, config_space_cache, CONFIG_SPACE_CACHESIZE); /* doesn't even tell how much it actually reads */
#endif

    /* try to read the domain */
#if (defined HWLOC_HAVE_LIBPCIACCESS) || (defined HWLOC_HAVE_PCIDEV_DOMAIN)
    domain = pcidev->domain;
#else
    domain = 0; /* default domain number */
#endif

    /* try to read the device_class */
#ifdef HWLOC_HAVE_LIBPCIACCESS
    device_class = pcidev->device_class >> 8;
#else /* HWLOC_HAVE_PCIUTILS */
#ifdef HWLOC_HAVE_PCIDEV_DEVICE_CLASS
    device_class = pcidev->device_class;
#else
    device_class = config_space_cache[PCI_CLASS_DEVICE] | (config_space_cache[PCI_CLASS_DEVICE+1] << 8);
#endif
#endif

    /* might be useful for debugging (note that domain might be truncated) */
    os_index = (domain << 20) + (pcidev->bus << 12) + (pcidev->dev << 4) + pcidev->func;

    obj = hwloc_alloc_setup_object(HWLOC_OBJ_PCI_DEVICE, os_index);
    obj->attr->pcidev.domain = domain;
    obj->attr->pcidev.bus = pcidev->bus;
    obj->attr->pcidev.dev = pcidev->dev;
    obj->attr->pcidev.func = pcidev->func;
    obj->attr->pcidev.vendor_id = pcidev->vendor_id;
    obj->attr->pcidev.device_id = pcidev->device_id;
    obj->attr->pcidev.class_id = device_class;
    obj->attr->pcidev.revision = config_space_cache[PCI_REVISION_ID];

    obj->attr->pcidev.linkspeed = 0; /* unknown */
#ifdef HWLOC_HAVE_PCI_FIND_CAP
    cap = pci_find_cap(pcidev, PCI_CAP_ID_EXP, PCI_CAP_NORMAL);
    offset = cap ? cap->addr : 0;
#else
    offset = hwloc_pci_find_cap(config_space_cache, PCI_CAP_ID_EXP);
#endif /* HWLOC_HAVE_PCI_FIND_CAP */

    if (0xffff == pcidev->vendor_id && 0xffff == pcidev->device_id) {
      /* SR-IOV puts ffff:ffff in Virtual Function config space.
       * The actual VF device ID is stored at a special (dynamic) location in the Physical Function config space.
       * VF and PF have the same vendor ID.
       *
       * libpciaccess just returns ffff:ffff, needs to be fixed.
       * linuxpci is OK because sysfs files are already fixed the kernel.
       * pciutils is OK when it uses those Linux sysfs files.
       *
       * Reading these files is an easy way to work around the libpciaccess issue on Linux,
       * but we have no way to know if this is caused by SR-IOV or not.
       *
       * TODO:
       *  If PF has CAP_ID_PCIX or CAP_ID_EXP (offset>0),
       *  look for extended capability PCI_EXT_CAP_ID_SRIOV (need extended config space (more than 256 bytes)),
       *  then read the VF device ID after it (PCI_IOV_DID bytes later).
       *  Needs access to extended config space (needs root on Linux).
       * TODO:
       *  Add string info attributes in VF and PF objects?
       */
#ifdef HWLOC_LINUX_SYS
      /* Workaround for Linux (the kernel returns the VF device/vendor IDs). */
      char path[64];
      char value[16];
      FILE *file;
      snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/vendor",
	       domain, pcidev->bus, pcidev->dev, pcidev->func);
      file = fopen(path, "r");
      if (file) {
	fread(value, sizeof(value), 1, file);
	fclose(file);
	obj->attr->pcidev.vendor_id = strtoul(value, NULL, 16);
      }
      snprintf(path, sizeof(path), "/sys/bus/pci/devices/%04x:%02x:%02x.%01x/device",
	       domain, pcidev->bus, pcidev->dev, pcidev->func);
      file = fopen(path, "r");
      if (file) {
	fread(value, sizeof(value), 1, file);
	fclose(file);
	obj->attr->pcidev.device_id = strtoul(value, NULL, 16);
      }
#endif
    }

    if (offset > 0 && offset + 20 /* size of PCI express block up to link status */ <= CONFIG_SPACE_CACHESIZE)
      hwloc_pci_find_linkspeed(config_space_cache, offset, &obj->attr->pcidev.linkspeed);

    hwloc_pci_prepare_bridge(obj, config_space_cache);

    if (obj->type == HWLOC_OBJ_PCI_DEVICE) {
      memcpy(&tmp16, &config_space_cache[PCI_SUBSYSTEM_VENDOR_ID], sizeof(tmp16));
      obj->attr->pcidev.subvendor_id = tmp16;
      memcpy(&tmp16, &config_space_cache[PCI_SUBSYSTEM_ID], sizeof(tmp16));
      obj->attr->pcidev.subdevice_id = tmp16;
    } else {
      /* TODO:
       * bridge must lookup PCI_CAP_ID_SSVID and then look at offset+PCI_SSVID_VENDOR/DEVICE_ID
       * cardbus must look at PCI_CB_SUBSYSTEM_VENDOR_ID and PCI_CB_SUBSYSTEM_ID
       */
    }

    /* starting from pciutils 2.2, pci_lookup_name() takes a variable number
     * of arguments, and supports the PCI_LOOKUP_NO_NUMBERS flag.
     */

    /* get the vendor name */
#ifdef HWLOC_HAVE_LIBPCIACCESS
    vendorname = pci_device_get_vendor_name(pcidev);
#else /* HWLOC_HAVE_PCIUTILS */
    vendorname = pci_lookup_name(pciaccess, name, sizeof(name),
#if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS
			      PCI_LOOKUP_VENDOR|PCI_LOOKUP_NO_NUMBERS,
			      pcidev->vendor_id
#else
			      PCI_LOOKUP_VENDOR,
			      pcidev->vendor_id, 0, 0, 0
#endif
			      );
#endif /* HWLOC_HAVE_PCIUTILS */
    if (vendorname && *vendorname)
      hwloc_obj_add_info(obj, "PCIVendor", vendorname);

    /* get the device name */
#ifdef HWLOC_HAVE_LIBPCIACCESS
    devicename = pci_device_get_device_name(pcidev);
#else /* HWLOC_HAVE_PCIUTILS */
    devicename = pci_lookup_name(pciaccess, name, sizeof(name),
#if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS
			      PCI_LOOKUP_DEVICE|PCI_LOOKUP_NO_NUMBERS,
			      pcidev->vendor_id, pcidev->device_id
#else
			      PCI_LOOKUP_DEVICE,
			      pcidev->vendor_id, pcidev->device_id, 0, 0
#endif
			      );
#endif /* HWLOC_HAVE_PCIUTILS */
    if (devicename && *devicename)
      hwloc_obj_add_info(obj, "PCIDevice", devicename);

    /* generate or get the fullname */
#ifdef HWLOC_HAVE_LIBPCIACCESS
    snprintf(name, sizeof(name), "%s%s%s",
	     vendorname ? vendorname : "",
	     vendorname && devicename ? " " : "",
	     devicename ? devicename : "");
    fullname = name;
    if (*name)
      obj->name = strdup(name);
#else /* HWLOC_HAVE_PCIUTILS */
    fullname = pci_lookup_name(pciaccess, name, sizeof(name),
#if HAVE_DECL_PCI_LOOKUP_NO_NUMBERS
			      PCI_LOOKUP_VENDOR|PCI_LOOKUP_DEVICE|PCI_LOOKUP_NO_NUMBERS,
			      pcidev->vendor_id, pcidev->device_id
#else
			      PCI_LOOKUP_VENDOR|PCI_LOOKUP_DEVICE,
			      pcidev->vendor_id, pcidev->device_id, 0, 0
#endif
			      );
    if (fullname && *fullname)
      obj->name = strdup(fullname);
#endif /* HWLOC_HAVE_PCIUTILS */
    hwloc_debug("  %04x:%02x:%02x.%01x %04x %04x:%04x %s\n",
		domain, pcidev->bus, pcidev->dev, pcidev->func,
		device_class, pcidev->vendor_id, pcidev->device_id,
		fullname && *fullname ? fullname : "??");

    /* queue the object for now */
    if (first_obj)
      last_obj->next_sibling = obj;
    else
      first_obj = obj;
    last_obj = obj;
  }

  /* finalize device scanning */
#ifdef HWLOC_HAVE_LIBPCIACCESS
  pci_iterator_destroy(iter);
  pci_system_cleanup();
#else /* HWLOC_HAVE_PCIUTILS */
  pci_cleanup(pciaccess);
#endif

  return hwloc_insert_pci_device_list(backend, first_obj);
}
int
main (int argc, char *argv[])
{
 	off_t address = 0;
 	size_t length = 0;
 	int fd;
 	int state;
	int retval = 0;
	struct pci_access *pacc;
 	struct pci_dev *dev;

	DBusError err;

	setup_logger ();
	udi = getenv ("UDI");

	HAL_DEBUG (("udi=%s", udi));
	if (udi == NULL) {
		HAL_ERROR (("No device specified"));
		return -2;
	}

	dbus_error_init (&err);
	if ((halctx = libhal_ctx_init_direct (&err)) == NULL) {
		HAL_ERROR (("Cannot connect to hald"));
		retval = -3;
		goto out;
	}

	if (!libhal_device_addon_is_ready (halctx, udi, &err)) {
		retval = -4;
		goto out;
	}


	conn = libhal_ctx_get_dbus_connection (halctx);
	dbus_connection_setup_with_g_main (conn, NULL);
	dbus_connection_set_exit_on_disconnect (conn, 0);

	dbus_connection_add_filter (conn, filter_function, NULL, NULL);

 	/* Search for the graphics card. */
 	/* Default values: */
 	/* address = 0x90300000; */
 	/* length = 0x20000; */

 	pacc = pci_alloc();
 	pci_init(pacc);
 	pci_scan_bus(pacc);
 	for(dev=pacc->devices; dev; dev=dev->next) {	/* Iterate over all devices */
 		pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
 		if ((dev->vendor_id == 0x1002) && (dev->device_id == 0x71c5)) { // ATI X1600
 			address = dev->base_addr[2];
 			length = dev->size[2];
 		}
 	}
 	pci_cleanup(pacc);

	HAL_DEBUG (("addr 0x%x len=%d", address, length));
 
 	if (!address) {
 		HAL_DEBUG (("Failed to detect ATI X1600, aborting..."));
		retval = 1;
		goto out;
 	}
 
 	fd = open ("/dev/mem", O_RDWR);
 	
 	if (fd < 0) {
 		HAL_DEBUG (("cannot open /dev/mem"));
		retval = 1;
		goto out;
 	}
 
 	memory = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, address);
 
 	if (memory == MAP_FAILED) {
 		HAL_ERROR (("mmap failed"));
		retval = 2;
		goto out;
 	}
 
 	/* Is it really necessary ? */
 	OUTREG(0x4dc, 0x00000005);
 	state = INREG(0x7ae4);
 	OUTREG(0x7ae4, state);

	/* Allow access to porta 0x300 through 0x304 */
	if (ioperm (0x300, 5, 1) < 0) {
		HAL_ERROR (("ioperm failed (you should be root)."));
		exit(1);
	}

	/* this works because we hardcoded the udi's in the <spawn> in the fdi files */
	if (!libhal_device_claim_interface (halctx, 
					    "/org/freedesktop/Hal/devices/macbook_pro_lcd_panel", 
					    "org.freedesktop.Hal.Device.LaptopPanel", 
					    "    <method name=\"SetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
					    "      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n"
					    "    <method name=\"GetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n",
					    &err)) {
		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LaptopPanel'"));
		retval = -4;
		goto out;
	}
	if (!libhal_device_claim_interface (halctx, 
					    "/org/freedesktop/Hal/devices/macbook_pro_light_sensor",
					    "org.freedesktop.Hal.Device.LightSensor", 
					    "    <method name=\"GetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"out\" type=\"ai\"/>\n"
					    "    </method>\n",
					    &err)) {
		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LightSensor'"));
		retval = -4;
		goto out;
	}
	if (!libhal_device_claim_interface (halctx, 
					    "/org/freedesktop/Hal/devices/macbook_pro_keyboard_backlight",
					    "org.freedesktop.Hal.Device.KeyboardBacklight", 
					    "    <method name=\"GetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n"
					    "    <method name=\"SetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
					    "    </method>\n",
					    &err)) {
		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.KeyboardBacklight'"));
		retval = -4;
		goto out;
	}

	main_loop = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (main_loop);
	return 0;

out:
        HAL_DEBUG (("An error occured, exiting cleanly"));

        LIBHAL_FREE_DBUS_ERROR (&err);

        if (halctx != NULL) {
                libhal_ctx_shutdown (halctx, &err);
                LIBHAL_FREE_DBUS_ERROR (&err);
                libhal_ctx_free (halctx);
        }

        return retval;
}
Example #21
0
int main(int argc, char *argv[])
{
	int i, j, opt, option_index;
#if defined(__FreeBSD__)
	int io_fd;
#endif

	static const struct option long_options[] = {
		{"dump",		no_argument, NULL, 'd'},
		{"extra-dump",		no_argument, NULL, 'e'},
		{"list-supported",	no_argument, NULL, 'l'},
		{"verbose",		no_argument, NULL, 'V'},
		{"version",		no_argument, NULL, 'v'},
		{"help",		no_argument, NULL, 'h'},
		{0, 0, 0, 0}
	};

	while ((opt = getopt_long(argc, argv, "delVvh",
				  long_options, &option_index)) != EOF) {
		switch (opt) {
		case 'd':
			dump = 1;
			break;
		case 'e':
			extra_dump = 1;
			break;
		case 'l':
			print_list_of_supported_chips();
			exit(0);
			break;
		case 'V':
			verbose = 1;
			break;
		case 'v':
			print_version();
			exit(0);
			break;
		case 'h':
			printf(USAGE);
			printf(USAGE_INFO);
			exit(0);
			break;
		default:
			/* Unknown option. */
			exit(1);
			break;
		}
	}

#if defined(__FreeBSD__)
	if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
		perror("/dev/io");
#else
	if (iopl(3) < 0) {
		perror("iopl");
#endif
		printf("Superiotool must be run as root.\n");
		exit(1);
	}

	print_version();

#ifdef PCI_SUPPORT
	/* Do some basic libpci init. */
	pacc = pci_alloc();
	pci_init(pacc);
	pci_scan_bus(pacc);
#endif

	for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
		for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
			superio_ports_table[i].probe_idregs(
				superio_ports_table[i].ports[j]);
	}

	if (!chip_found)
		printf("No Super I/O found\n");

#if defined(__FreeBSD__)
	close(io_fd);
#endif
	return 0;
}
Example #22
0
File: pci.c Project: cbm-fles/pda
static inline
PdaDebugReturnCode
PciDevice_invoke_libpci
(
    PciDevice  *device,
    const char *bus_id
)
{
    DEBUG_PRINTF(PDADEBUG_ENTER, "");

    if(device == NULL)
    { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); }

    if(bus_id == NULL)
    { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); }

    if
    (
        (strlen(bus_id) != 12) ||
        (bus_id[4]  != ':')    ||
        (bus_id[7]  != ':')    ||
        (bus_id[10] != '.')
    )
    { RETURN( ERROR( EINVAL, "Invalid bus ID string!\n") ); }

    struct pci_access *pci_access_handler = pci_alloc();
    pci_init(pci_access_handler);
    pci_scan_bus(pci_access_handler);
    for
    (
        struct pci_dev *pci_device = pci_access_handler->devices;
        pci_device;
        pci_device = pci_device->next
    )
    {
        pci_fill_info(pci_device,
                      PCI_FILL_IDENT      |
                      PCI_FILL_IRQ        |
                      PCI_FILL_BASES      |
                      PCI_FILL_ROM_BASE   |
                      PCI_FILL_SIZES      |
                      PCI_FILL_CLASS      |
                      PCI_FILL_CAPS       |
                      PCI_FILL_EXT_CAPS   |
                      PCI_FILL_PHYS_SLOT
                      );

        char device_bus_id[] = "0000:00:00.0";
        snprintf(device_bus_id, 13,"%04x:%02x:%02x.%d", pci_device->domain, pci_device->bus,
                pci_device->dev, pci_device->func);

        if(strcmp(device_bus_id, bus_id) == 0)
        {
            device->domain_id   = pci_device->domain;
            device->bus_id      = pci_device->bus;
            device->device_id   = pci_device->dev;
            device->function_id = pci_device->func;

            for(uint32_t i = 0; i < PDA_MAX_PCI_32_BARS; i++)
            {
                uint32_t bar       = pci_device->base_addr[i];

                DEBUG_PRINTF(PDADEBUG_VALUE, "bar%d : 0x%x\n", i, bar);

                uint8_t  config[64];
                pci_read_block(pci_device, 0, config, 64);
                uint64_t addr      = PCI_BASE_ADDRESS_0 + (4*i);
                uint32_t virt_flag =
                    (config[addr+0]       ) |
                    (config[addr+1] << 8  ) |
                    (config[addr+2] << 16 ) |
                    (config[addr+3] << 24 ) ;

                if(bar && !virt_flag)
                {
                    device->bar_types[i] = PCIBARTYPES_NOT_MAPPED;
                    DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "bar%u is virtual\n", i);
                    continue;
                }

                if(bar == 0x0)
                {
                    if(device->bar_types[i] != PCIBARTYPES_NOT_MAPPED)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_NOT_MAPPED;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }

                if( (bar & 0x00000001) == 0x1)
                {
                    if(device->bar_types[i] != PCIBARTYPES_IO)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_IO;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }

                uint32_t width_bit = bar & 0x00000006;
                if(width_bit == 0x00000000)
                {
                    if(device->bar_types[i] != PCIBARTYPES_BAR32)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_BAR32;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }

                if(width_bit == 0x00000004)
                {
                    if(device->bar_types[i] != PCIBARTYPES_BAR64)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_BAR64;
                        device->bar_types[i + 1] = PCIBARTYPES_NOT_MAPPED;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }
            }

            /** Older versions of libpci leak memory when this function is called. **/
#if PCI_LIB_VERSION >= 0x030301
            pci_lookup_name(pci_access_handler, device->description, PDA_STRING_LIMIT,
                            PCI_LOOKUP_DEVICE, pci_device->vendor_id,
                            pci_device->device_id);
#else
            #warning "At least version 3.3.1 of libpci is needed. Otherwise the device description can't be returned."
            strcpy(device->description, "");
#endif

            DEBUG_PRINTF(PDADEBUG_VALUE, " (%s) %s\n", device->description, device_bus_id);
        }
    }
    pci_cleanup(pci_access_handler);

    RETURN(PDA_SUCCESS);
}
Example #23
0
sbone_err_t sbone_dev_open_pcie
(sbone_dev_t* dev, int dom, int bus, int _dev, int fun)
{
  static const size_t nbar = sizeof(dev->bar_addrs) / sizeof(dev->bar_addrs[0]);
  int mem_fd;
  size_t i;
  struct pci_access* pci_access;
  struct pci_dev* pci_dev;
  int err = -1;

  /* zero device */

  for (i = 0; i < nbar; ++i) dev->bar_sizes[i] = 0;

  dev->nid = -1;

  /* find the pci device */

  pci_access = pci_alloc();
  if (pci_access == NULL) { PERROR(); goto on_error_0; }
  pci_init(pci_access);
  pci_scan_bus(pci_access);

  pci_dev = pci_get_dev(pci_access, dom, bus, _dev, fun);
  if (pci_dev == NULL) { PERROR(); goto on_error_1; }
  pci_fill_info(pci_dev, PCI_FILL_IDENT | PCI_FILL_BASES);

  /* map bars */

  mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (mem_fd == -1) { PERROR(); goto on_error_2; }

  for (i = 0; i < nbar; ++i)
  {
#if 1 /* FIXME: pci_dev->size[i] not detected by pci_fill_info ... */
    if (pci_dev->base_addr[i] != 0)
      pci_dev->size[i] = get_bar_size(pci_access, pci_dev, i);
#endif /* FIXME */

    dev->bar_sizes[i] = pci_dev->size[i];
    if (dev->bar_sizes[i] == 0) continue ;

    dev->bar_addrs[i] = (uintptr_t)mmap
    (
     NULL, pci_dev->size[i],
     PROT_READ | PROT_WRITE,
     MAP_SHARED | MAP_NORESERVE,
     mem_fd,
     pci_dev->base_addr[i]
    );

    if (dev->bar_addrs[i] == (uintptr_t)MAP_FAILED)
    {
      size_t j;
      for (j = 0; j < i; ++j)
	munmap((void*)dev->bar_addrs[i], dev->bar_sizes[i]);
      PERROR();
      goto on_error_3;
    }
  }

  if ((dev->fd = open("/dev/sbone", O_RDWR)) == -1)
  {
    PERROR();
    goto on_error_3;
  }

  /* success */

  err = 0;

 on_error_3:
  close(mem_fd);
 on_error_2:
  pci_free_dev(pci_dev);
 on_error_1:
  pci_cleanup(pci_access);
 on_error_0:
  return err;

}
Example #24
0
pci::pci(std::string proc_pci_path) : _pacc(pci_alloc()) {
    pci_init(_pacc);
    _pacc->numeric_ids = 0;
    pci_set_param(_pacc, const_cast<char*>("proc.path"), const_cast<char*>(proc_pci_path.c_str()));
}
Example #25
0
/*
 * Find all suitable cards, then find their memory space and get memory information.
 */
int main()
{
	struct pci_access *pci;
	struct pci_dev *dev;
	int i, meminfo;
	u32 subvendor, subdevice;
	off_t base;
	int *pcimem;
	int fd;
	char *devname;
	int fail =0, manufacturer, model;

	printf("AMDMemInfo by Zuikkis <*****@*****.**>\n");

	pci = pci_alloc();
	pci_init(pci);

	pci_scan_bus(pci);	


	for(dev = pci->devices; dev; dev = dev->next) {
	    if(dev->device_class == PCI_CLASS_DISPLAY_VGA &&
	      dev->vendor_id == 0x1002 && 
	    ( dev->device_id == 0x679a ||   // hd7950
	      dev->device_id == 0x6798 ||   // hd7970 / r9 280x
	      dev->device_id == 0x679b ||   // hd7990
	      dev->device_id == 0x67b1 ||   // r9 290
	      dev->device_id == 0x67b0 ||   // r9 290
	      dev->device_id == 0x6811 ||   // r9 270
              dev->device_id == 0x6719 ||   // hd6950
              dev->device_id == 0x6778 ||   // HD6450 device id 1
	      dev->device_id == 0x6779 ||   // HD6450 device id 2
	      dev->device_id == 0x6819 ||   // hd7850
              dev->device_id == 0x6818))    // hd7870
	    {
			switch (dev->device_id) {
				case 0x679a: devname="Radeon HD7950"; 				break;
				case 0x6798: devname="Radeon HD7970 / R9 280x"; 	break;
				case 0x679b: devname="Radeon HD7990";		 		break;
				case 0x67b1: devname="Radeon R9 290"; 				break;
				case 0x67b0: devname="Radeon R9 290x"; 				break;
				case 0x6811: devname="Radeon R9 270";				break;
				case 0x6818: devname="Radeon HD7870";				break;
				case 0x6819: devname="Radeon HD7850";				break;
				case 0x6719: devname="Radeon HD6950";				break;
				case 0x6778: devname="Radeon HD6450";				break;
				case 0x6779: devname="Radeon HD6450";				break;
				default: devname="Unknown"; break;
			}

			subvendor = pci_read_word(dev, PCI_SUBSYSTEM_VENDOR_ID);
			subdevice = pci_read_word(dev, PCI_SUBSYSTEM_ID);

			printf(	"-----------------------------------\n"
					"Found card:  %04x:%04x (AMD %s)\n"
					"Subvendor:   0x%x\n"
					"Subdevice:   0x%x\n",
					dev->vendor_id, dev->device_id, devname, subvendor, subdevice);

			for (i=0;i<6;i++) {
			    if (dev->size[i]==0x40000) {
				base=(dev->base_addr[i] & 0xfffffff0);

     			  	fd = open ( "/dev/mem", O_RDONLY);
  			  	pcimem = (int *) mmap(NULL, 0x20000, PROT_READ, MAP_SHARED, fd, base);
				if (pcimem == MAP_FAILED) {
				    fail++;
				} else {
				    meminfo=pcimem[(0x2a00)/4];
		 		    manufacturer=(meminfo & 0xf00)>>8;
				    model=(meminfo & 0xf000)>>12;
				    printf("Memory type: ");
				    switch(manufacturer) {
					case 1:
					    printf("Samsung K4G20325FD\n"); break;
					case 3:
					    printf("Elpida EDW2032BBBG\n"); break;
					case 6:
					    switch(model) {
						case 2: printf("SK Hynix H5GQ2H24MFR\n"); break;
						case 3: printf("SK Hynix H5GQ2H24AFR\n"); break;
						default: printf("SK Hynix unknown model %d\n",model);
					    }
					    break;
					default:
					    printf("Unknown manufacturer %d\n",manufacturer);
				    }

	 	 		    munmap(pcimem, 0x20000);
				}
				close(fd);
				}
			}
		}
	}