Try to make buffer object / fence object ioctl args 64-bit safe.
[platform/upstream/libdrm.git] / linux-core / via_buffer.c
1 /**************************************************************************
2  *
3  * Copyright (c) 2007 Tungsten Graphics, Inc., Cedar Park, TX., USA,
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
29  */
30
31 #include "drmP.h"
32 #include "via_drm.h"
33 #include "via_drv.h"
34
35 drm_ttm_backend_t *via_create_ttm_backend_entry(drm_device_t * dev)
36 {
37         return drm_agp_init_ttm(dev);
38 }
39
40 int via_fence_types(drm_buffer_object_t *bo, uint32_t * type)
41 {
42         *type = 3;
43         return 0;
44 }
45
46 int via_invalidate_caches(drm_device_t * dev, uint64_t flags)
47 {
48         /*
49          * FIXME: Invalidate texture caches here.
50          */
51
52         return 0;
53 }
54
55
56 static int via_vram_info(drm_device_t *dev,
57                          unsigned long *offset,
58                          unsigned long *size)
59 {
60         struct pci_dev *pdev = dev->pdev;
61         unsigned long flags;
62
63         int ret = DRM_ERR(EINVAL);
64         int i;
65         for (i=0; i<6; ++i) {
66                 flags = pci_resource_flags(pdev, i);
67                 if ((flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) ==
68                     (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
69                         ret = 0;
70                         break;
71                 }
72         }
73
74         if (ret) {
75                 DRM_ERROR("Could not find VRAM PCI resource\n");
76                 return ret;
77         }
78
79         *offset = pci_resource_start(pdev, i);
80         *size = pci_resource_end(pdev, i) - *offset + 1;
81         return 0;
82 }
83
84 int via_init_mem_type(drm_device_t * dev, uint32_t type,
85                        drm_mem_type_manager_t * man)
86 {
87         switch (type) {
88         case DRM_BO_MEM_LOCAL:
89                 /* System memory */
90
91                 man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE |
92                         _DRM_FLAG_MEMTYPE_CACHED;
93                 man->drm_bus_maptype = 0;
94                 break;
95
96         case DRM_BO_MEM_TT: 
97                 /* Dynamic agpgart memory */
98                 
99                 if (!(drm_core_has_AGP(dev) && dev->agp)) {
100                         DRM_ERROR("AGP is not enabled for memory type %u\n",
101                                   (unsigned)type);
102                         return -EINVAL;
103                 }
104                 man->io_offset = dev->agp->agp_info.aper_base;
105                 man->io_size = dev->agp->agp_info.aper_size * 1024 * 1024;
106                 man->io_addr = NULL;
107                 man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE | _DRM_FLAG_NEEDS_IOREMAP;
108
109                 /* Only to get pte protection right. */
110
111                 man->drm_bus_maptype = _DRM_AGP; 
112                 break;
113
114         case DRM_BO_MEM_VRAM: 
115                 /* "On-card" video ram */
116                 
117                 man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE | _DRM_FLAG_NEEDS_IOREMAP;
118                 man->drm_bus_maptype = _DRM_FRAME_BUFFER;
119                 man->io_addr = NULL;
120                 return via_vram_info(dev, &man->io_offset, &man->io_size);
121                 break;
122
123         case DRM_BO_MEM_PRIV0: 
124                 /* Pre-bound agpgart memory */
125                 
126                 if (!(drm_core_has_AGP(dev) && dev->agp)) {
127                         DRM_ERROR("AGP is not enabled for memory type %u\n",
128                                   (unsigned)type);
129                         return -EINVAL;
130                 }
131                 man->io_offset = dev->agp->agp_info.aper_base;
132                 man->io_size = dev->agp->agp_info.aper_size * 1024 * 1024;
133                 man->io_addr = NULL;
134                 man->flags =  _DRM_FLAG_MEMTYPE_MAPPABLE |
135                     _DRM_FLAG_MEMTYPE_FIXED | _DRM_FLAG_NEEDS_IOREMAP;
136                 man->drm_bus_maptype = _DRM_AGP;
137                 break;
138
139         default:
140                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
141                 return -EINVAL;
142         }
143         return 0;
144 }
145
146 uint32_t via_evict_mask(drm_buffer_object_t *bo)
147 {
148         switch (bo->mem.mem_type) {
149         case DRM_BO_MEM_LOCAL:
150         case DRM_BO_MEM_TT:
151                 return DRM_BO_FLAG_MEM_LOCAL; /* Evict TT to local */
152         case DRM_BO_MEM_PRIV0: /* Evict pre-bound AGP to TT */
153                 return DRM_BO_MEM_TT;
154         case DRM_BO_MEM_VRAM:
155                 if (bo->mem.num_pages > 128)
156                         return DRM_BO_MEM_TT;
157                 else
158                         return DRM_BO_MEM_LOCAL;
159         default:
160                 return DRM_BO_MEM_LOCAL;
161         }
162 }