[igt-dev] [PATCH i-g-t v2 2/3] lib/igt_device: add igt_map_bar_region

Chris Wilson chris at chris-wilson.co.uk
Wed Apr 10 09:21:20 UTC 2019


Quoting Daniel Mrzyglod (2019-04-10 09:28:38)
> +void *igt_device_map_pci_bar_region(int fd, int  mmio_bar, int mmio_size)
> +{
> +       struct igt_pci_addr pci_addr;
> +       char *filepath = NULL;
> +       int newfd;
> +       void *igt_mmio = NULL;

Consider what does need initialising and whitespace carefully.

> +       if (igt_device_get_pci_addr(fd, &pci_addr)) {
> +               igt_warn("Unable to find device PCI address\n");
> +               return NULL;
> +       }
> +
> +       asprintf(&filepath, "/sys/devices/pci%.4x:%.2x/%.4x:%.2x:%.2x.%.1x/resource%.1x",
> +                pci_addr.domain, pci_addr.bus, pci_addr.domain, pci_addr.bus,
> +                pci_addr.device, pci_addr.function, mmio_bar);
> +
> +       newfd = open(filepath, O_RDWR | O_SYNC);
> +       igt_mmio = mmap(0, mmio_size, PROT_READ | PROT_WRITE, MAP_SHARED, newfd, 0);
> +       free(filepath);
> +       close(newfd);

Stick to the onion. Make it a habit.

> +
> +       igt_fail_on_f(igt_mmio == MAP_FAILED,
> +                     "Couldn't map MMIO region\n");

Bonus points for saying what couldn't be mmapped.

But don't use igt_fail if not inside a test.
-Chris


More information about the igt-dev mailing list