linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* mmap() implementation for pci_alloc_consistent() memory?
@ 2011-05-18 22:14 Leon Woestenberg
  2011-05-19  1:04 ` Leon Woestenberg
  2011-05-19 14:59 ` Konrad Rzeszutek Wilk
  0 siblings, 2 replies; 10+ messages in thread
From: Leon Woestenberg @ 2011-05-18 22:14 UTC (permalink / raw)
  To: linux-pci, linux-mm, linux-kernel

Hello,

I cannot get my driver's mmap() to work. I allocate 64 KiB ringbuffer
using pci_alloc_consistent(), then implement mmap() to allow programs
to map that memory into their user space.

My driver writes 0xDEADBEEF into the first 32-bit word of the memory
block. When I dump this word from my mmap.c program, it reads 0. It
seems a zero-page got mapped rather than the buffer.

This is the code, Ieft out all error checking but inserted comments to
show what I have verified.

int main(void)
{
  int fd = open("/device_node", O_RDWR | O_SYNC);
  uint32_t *addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  uint32_t data = *addr;
  printf("address 0x%p reads data 0x%08x\n", addr32, (unsigned int)data);
  munmap(addr, 4096);
  close(fd);
}


void ringbuffer_vma_open(struct vm_area_struct *vma)
{
}

void ringbuffer_vma_close(struct vm_area_struct *vma)
{
}

int ringbuffer_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
{
        /* the buffer allocated with pci_alloc_consistent() */
	void *vaddr = ringbuffer_virt;
	int ret;

	/* find the struct page that describes vaddr, the buffer
	 * allocated with pci_alloc_consistent() */
	struct page *page = virt_to_page(lro_char->engine->ringbuffer_virt);
	vmf->page = page;

        /*** I have verified that vaddr, page, and the pfn correspond
with vaddr = pci_alloc_consistent() ***/
	ret = vm_insert_pfn(vma, address, page_to_pfn(page));
	return ret;
}

static const struct vm_operations_struct ringbuffer_vm_ops = {
	.fault          = ringbuffer_vma_fault,
};

static int ringbuffer_mmap(struct file *file, struct vm_area_struct *vma)
{
        <...extract private data...>

	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);

	vma->vm_flags |= VM_RESERVED | VM_MIXEDMAP;
	vma->vm_private_data = file->private_data;
	vma->vm_ops = &ringbuffer_vm_ops;
	ringbuffer_vma_open(vma);
	return 0;
}

What did I miss?

Regards,
-- 
Leon

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2011-05-24 14:18 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-18 22:14 mmap() implementation for pci_alloc_consistent() memory? Leon Woestenberg
2011-05-19  1:04 ` Leon Woestenberg
2011-05-19 14:59 ` Konrad Rzeszutek Wilk
2011-05-19 15:58   ` Clemens Ladisch
2011-05-19 22:10     ` Leon Woestenberg
2011-05-20  6:51       ` Clemens Ladisch
2011-05-20  8:17         ` Takashi Iwai
2011-05-21 10:59           ` Leon Woestenberg
2011-05-23  8:30             ` Clemens Ladisch
2011-05-24 14:18               ` Leon Woestenberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).