Merge branch 'pre-superioctl-branch'
[profile/ivi/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 struct drm_ttm_backend *via_create_ttm_backend_entry(struct drm_device * dev)
36 {
37         return drm_agp_init_ttm(dev);
38 }
39
40 int via_fence_types(struct drm_buffer_object *bo, uint32_t * fclass,
41                     uint32_t * type)
42 {
43         *type = 3;
44         return 0;
45 }
46
47 int via_invalidate_caches(struct drm_device * dev, uint64_t flags)
48 {
49         /*
50          * FIXME: Invalidate texture caches here.
51          */
52
53         return 0;
54 }
55
56
57 static int via_vram_info(struct drm_device *dev,
58                          unsigned long *offset,
59                          unsigned long *size)
60 {
61         struct pci_dev *pdev = dev->pdev;
62         unsigned long flags;
63
64         int ret = -EINVAL;
65         int i;
66         for (i=0; i<6; ++i) {
67                 flags = pci_resource_flags(pdev, i);
68                 if ((flags & (IORESOURCE_MEM | IORESOURCE_PREFETCH)) ==
69                     (IORESOURCE_MEM | IORESOURCE_PREFETCH)) {
70                         ret = 0;
71                         break;
72                 }
73         }
74
75         if (ret) {
76                 DRM_ERROR("Could not find VRAM PCI resource\n");
77                 return ret;
78         }
79
80         *offset = pci_resource_start(pdev, i);
81         *size = pci_resource_end(pdev, i) - *offset + 1;
82         return 0;
83 }
84
85 int via_init_mem_type(struct drm_device * dev, uint32_t type,
86                        struct drm_mem_type_manager * man)
87 {
88         switch (type) {
89         case DRM_BO_MEM_LOCAL:
90                 /* System memory */
91
92                 man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE |
93                         _DRM_FLAG_MEMTYPE_CACHED;
94                 man->drm_bus_maptype = 0;
95                 break;
96
97         case DRM_BO_MEM_TT: 
98                 /* Dynamic agpgart memory */
99                 
100                 if (!(drm_core_has_AGP(dev) && dev->agp)) {
101                         DRM_ERROR("AGP is not enabled for memory type %u\n",
102                                   (unsigned)type);
103                         return -EINVAL;
104                 }
105                 man->io_offset = dev->agp->agp_info.aper_base;
106                 man->io_size = dev->agp->agp_info.aper_size * 1024 * 1024;
107                 man->io_addr = NULL;
108                 man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE | _DRM_FLAG_NEEDS_IOREMAP;
109
110                 /* Only to get pte protection right. */
111
112                 man->drm_bus_maptype = _DRM_AGP; 
113                 break;
114
115         case DRM_BO_MEM_VRAM: 
116                 /* "On-card" video ram */
117                 
118                 man->flags = _DRM_FLAG_MEMTYPE_MAPPABLE | _DRM_FLAG_NEEDS_IOREMAP;
119                 man->drm_bus_maptype = _DRM_FRAME_BUFFER;
120                 man->io_addr = NULL;
121                 return via_vram_info(dev, &man->io_offset, &man->io_size);
122                 break;
123
124         case DRM_BO_MEM_PRIV0: 
125                 /* Pre-bound agpgart memory */
126                 
127                 if (!(drm_core_has_AGP(dev) && dev->agp)) {
128                         DRM_ERROR("AGP is not enabled for memory type %u\n",
129                                   (unsigned)type);
130                         return -EINVAL;
131                 }
132                 man->io_offset = dev->agp->agp_info.aper_base;
133                 man->io_size = dev->agp->agp_info.aper_size * 1024 * 1024;
134                 man->io_addr = NULL;
135                 man->flags =  _DRM_FLAG_MEMTYPE_MAPPABLE |
136                     _DRM_FLAG_MEMTYPE_FIXED | _DRM_FLAG_NEEDS_IOREMAP;
137                 man->drm_bus_maptype = _DRM_AGP;
138                 break;
139
140         default:
141                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
142                 return -EINVAL;
143         }
144         return 0;
145 }
146
147 uint32_t via_evict_mask(struct drm_buffer_object *bo)
148 {
149         switch (bo->mem.mem_type) {
150         case DRM_BO_MEM_LOCAL:
151         case DRM_BO_MEM_TT:
152                 return DRM_BO_FLAG_MEM_LOCAL; /* Evict TT to local */
153         case DRM_BO_MEM_PRIV0: /* Evict pre-bound AGP to TT */
154                 return DRM_BO_MEM_TT;
155         case DRM_BO_MEM_VRAM:
156                 if (bo->mem.num_pages > 128)
157                         return DRM_BO_MEM_TT;
158                 else
159                         return DRM_BO_MEM_LOCAL;
160         default:
161                 return DRM_BO_MEM_LOCAL;
162         }
163 }