Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / drm / drm_vm.h
CommitLineData
984263bc
MD
1/*
2 * $FreeBSD: src/sys/dev/drm/drm_vm.h,v 1.6.2.1 2003/04/26 07:05:29 anholt Exp $
3 */
4
5#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
6static int DRM(dma_mmap)(dev_t kdev, vm_offset_t offset, vm_paddr_t *paddr,
7 int prot)
8#elif defined(__FreeBSD__)
9static int DRM(dma_mmap)(dev_t kdev, vm_offset_t offset, int prot)
10#elif defined(__NetBSD__)
11static paddr_t DRM(dma_mmap)(dev_t kdev, vm_offset_t offset, int prot)
12#endif
13{
14 DRM_DEVICE;
15 drm_device_dma_t *dma = dev->dma;
16 unsigned long physical;
17 unsigned long page;
18
19 if (!dma) return -1; /* Error */
20 if (!dma->pagelist) return -1; /* Nothing allocated */
21
22 page = offset >> PAGE_SHIFT;
23 physical = dma->pagelist[page];
24
25 DRM_DEBUG("0x%08lx (page %lu) => 0x%08lx\n", (long)offset, page, physical);
26#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
27 *paddr = physical;
28 return 0;
29#else
30 return atop(physical);
31#endif
32}
33
34#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
35int DRM(mmap)(dev_t kdev, vm_offset_t offset, vm_paddr_t *paddr,
36 int prot)
37#elif defined(__FreeBSD__)
38int DRM(mmap)(dev_t kdev, vm_offset_t offset, int prot)
39#elif defined(__NetBSD__)
40paddr_t DRM(mmap)(dev_t kdev, off_t offset, int prot)
41#endif
42{
43 DRM_DEVICE;
44 drm_local_map_t *map = NULL;
45 drm_map_list_entry_t *listentry=NULL;
46 drm_file_t *priv;
47
48 priv = DRM(find_file_by_proc)(dev, DRM_CURPROC);
49 if (!priv) {
50 DRM_DEBUG("can't find authenticator\n");
51 return EINVAL;
52 }
53
54 if (!priv->authenticated)
55 return DRM_ERR(EACCES);
56
57 if (dev->dma
58 && offset >= 0
59 && offset < ptoa(dev->dma->page_count))
60#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
61 return DRM(dma_mmap)(kdev, offset, paddr, prot);
62#else
63 return DRM(dma_mmap)(kdev, offset, prot);
64#endif
65
66 /* A sequential search of a linked list is
67 fine here because: 1) there will only be
68 about 5-10 entries in the list and, 2) a
69 DRI client only has to do this mapping
70 once, so it doesn't have to be optimized
71 for performance, even if the list was a
72 bit longer. */
73 TAILQ_FOREACH(listentry, dev->maplist, link) {
74 map = listentry->map;
75/* DRM_DEBUG("considering 0x%x..0x%x\n", map->offset, map->offset + map->size - 1);*/
76 if (offset >= map->offset
77 && offset < map->offset + map->size) break;
78 }
79
80 if (!listentry) {
81 DRM_DEBUG("can't find map\n");
82 return -1;
83 }
84 if (((map->flags&_DRM_RESTRICTED) && DRM_SUSER(DRM_CURPROC))) {
85 DRM_DEBUG("restricted map\n");
86 return -1;
87 }
88
89 switch (map->type) {
90 case _DRM_FRAME_BUFFER:
91 case _DRM_REGISTERS:
92 case _DRM_AGP:
93#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
94 *paddr = offset;
95 return 0;
96#else
97 return atop(offset);
98#endif
99 case _DRM_SCATTER_GATHER:
100 case _DRM_SHM:
101#if defined(__FreeBSD__) && __FreeBSD_version >= 500102
102 *paddr = vtophys(offset);
103 return 0;
104#else
105 return atop(vtophys(offset));
106#endif
107 default:
108 return -1; /* This should never happen. */
109 }
110 DRM_DEBUG("bailing out\n");
111
112 return -1;
113}
114