'Access GPU as PCI device

I am new to the GPU OS kernelspace world. I would like to access GPU memory as a PCI device through its memory exposed on the BAR using for exmaple dev_alloc () which is allocating memory and returns an address to this address.

void *addr;
addr = dev_alloc ()
//check that the allocated address is in the device bar range 
if(In_bar_range(virt_to_phy(addr))) {
WRITE_ONCE(addr[0], 0);
READ_ONCE(addr[0]);
}

I also tried mapping using pci_iomap as follows

  void* __iomap map;
  u32 val;
  map=pci_iomap(pci_dev,0,8);
  iowrite8(0, map);
  val=ioread8(map);
  dev_info(&dev " value = %ud ", val);

Trying to read from the values fails. It does not return 0. It returns a garbage value. I do not know whether there is a problem in addressing translation (IO_MMU) or if I am missing some kind of synchronization between the CPU and the GPU or if it is another problem?



Solution 1:[1]

Not exactly sure what you already have to work with, and it might vary based on the kernel version, but I'm assuming you already have a pointer to the device.

If you don't already have one, you will first need to obtain an iomem mapping through pci_iomap() (asm/pci_iomap.h) passing a pointer to the device (struct pci_dev) and the PCI BAR number. Then, as explained in DMA-API-HOWTO you should be able to read/write through this mapping using one of the io{read,write}*() functions defined in asm/iomap.h.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marco Bonelli