1 /*- 2 *Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 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 "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Rickard E. (Rik) Faith <faith (at) valinux.com> 27 * Gareth Hughes <gareth (at) valinux.com> 28 * 29 */ 30 31 /** @file drm_memory.c 32 * Wrappers for kernel memory allocation routines, and MTRR management support. 33 * 34 * This file previously implemented a memory consumption tracking system using 35 * the "area" argument for various different types of allocations, but that 36 * has been stripped out for now. 37 */ 38 39 #include "drmP.h" 40 41 42 #if defined(__NetBSD__) 43 # ifdef DRM_NO_AGP 44 # define NAGP_I810 0 45 # else 46 # if defined(_KERNEL_OPT) 47 # include "agp_i810.h" 48 # include "genfb.h" 49 # else 50 # define NAGP_I810 1 51 # define NGENFB 0 52 # endif 53 # endif 54 # if NAGP_I810 > 0 55 # include <dev/pci/agpvar.h> 56 # endif 57 # if NGENFB > 0 58 # include <dev/wsfb/genfbvar.h> 59 # endif 60 # if defined(_KERNEL_OPT) 61 # include "opt_mtrr.h" 62 # endif 63 #endif 64 65 MALLOC_DEFINE(DRM_MEM_DMA, "drm_dma", "DRM DMA Data Structures"); 66 MALLOC_DEFINE(DRM_MEM_SAREA, "drm_sarea", "DRM SAREA Data Structures"); 67 MALLOC_DEFINE(DRM_MEM_DRIVER, "drm_driver", "DRM DRIVER Data Structures"); 68 MALLOC_DEFINE(DRM_MEM_MAGIC, "drm_magic", "DRM MAGIC Data Structures"); 69 MALLOC_DEFINE(DRM_MEM_IOCTLS, "drm_ioctls", "DRM IOCTL Data Structures"); 70 MALLOC_DEFINE(DRM_MEM_MAPS, "drm_maps", "DRM MAP Data Structures"); 71 MALLOC_DEFINE(DRM_MEM_BUFS, "drm_bufs", "DRM BUFFER Data Structures"); 72 MALLOC_DEFINE(DRM_MEM_SEGS, "drm_segs", "DRM SEGMENTS Data Structures"); 73 MALLOC_DEFINE(DRM_MEM_PAGES, "drm_pages", "DRM PAGES Data Structures"); 74 MALLOC_DEFINE(DRM_MEM_FILES, "drm_files", "DRM FILE Data Structures"); 75 MALLOC_DEFINE(DRM_MEM_QUEUES, "drm_queues", "DRM QUEUE Data Structures"); 76 MALLOC_DEFINE(DRM_MEM_CMDS, "drm_cmds", "DRM COMMAND Data Structures"); 77 MALLOC_DEFINE(DRM_MEM_MAPPINGS, "drm_mapping", "DRM MAPPING Data Structures"); 78 MALLOC_DEFINE(DRM_MEM_BUFLISTS, "drm_buflists", "DRM BUFLISTS Data Structures"); 79 MALLOC_DEFINE(DRM_MEM_AGPLISTS, "drm_agplists", "DRM AGPLISTS Data Structures"); 80 MALLOC_DEFINE(DRM_MEM_CTXBITMAP, "drm_ctxbitmap", 81 "DRM CTXBITMAP Data Structures"); 82 MALLOC_DEFINE(DRM_MEM_SGLISTS, "drm_sglists", "DRM SGLISTS Data Structures"); 83 MALLOC_DEFINE(DRM_MEM_DRAWABLE, "drm_drawable", "DRM DRAWABLE Data Structures"); 84 MALLOC_DEFINE(DRM_MEM_MM, "drm_mm", "DRM MM Data Structures"); 85 86 void drm_mem_init(void) 87 { 88 } 89 90 void drm_mem_uninit(void) 91 { 92 } 93 94 #if defined(__NetBSD__) 95 static void * 96 drm_netbsd_ioremap(struct drm_device *dev, drm_local_map_t *map, int wc) 97 { 98 bus_space_handle_t h; 99 int i, reason; 100 for(i = 0; i<DRM_MAX_PCI_RESOURCE; i++) { 101 102 /* Does the requested mapping lie within this resource? */ 103 if ((dev->pci_map_data[i].maptype == PCI_MAPREG_TYPE_MEM || 104 dev->pci_map_data[i].maptype == 105 (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) && 106 map->offset >= dev->pci_map_data[i].base && 107 map->offset + map->size <= dev->pci_map_data[i].base + 108 dev->pci_map_data[i].size) 109 { 110 map->bst = dev->pa.pa_memt; 111 map->fullmap = &(dev->pci_map_data[i]); 112 map->mapsize = map->size; 113 dev->pci_map_data[i].mapped++; 114 115 /* If we've already mapped this resource in, handle 116 * submapping if needed, give caller a bus_space handle 117 * and pointer for the offest they asked for */ 118 if (dev->pci_map_data[i].mapped > 1) 119 { 120 if ((reason = bus_space_subregion( 121 dev->pa.pa_memt, 122 dev->pci_map_data[i].bsh, 123 map->offset - dev->pci_map_data[i].base, 124 map->size, &h)) != 0) { 125 DRM_DEBUG("ioremap failed to " 126 "bus_space_subregion: %d\n", 127 reason); 128 return NULL; 129 } 130 map->bsh = h; 131 map->handle = bus_space_vaddr(dev->pa.pa_memt, 132 h); 133 return map->handle; 134 } 135 136 /* Map in entirety of resource - full size and handle 137 * go in pci_map_data, specific mapping in callers 138 * drm_local_map_t */ 139 DRM_DEBUG("ioremap%s: flags %d\n", wc ? "_wc" : "", 140 dev->pci_map_data[i].flags); 141 if ((reason = bus_space_map(map->bst, 142 dev->pci_map_data[i].base, 143 dev->pci_map_data[i].size, 144 dev->pci_map_data[i].flags, 145 &dev->pci_map_data[i].bsh))) 146 { 147 dev->pci_map_data[i].mapped--; 148 #if NAGP_I810 > 0 /* XXX horrible kludge: agp might have mapped it */ 149 if (agp_i810_borrow(map->offset, map->size, 150 &map->bsh)) 151 return bus_space_vaddr(map->bst, map->bsh); 152 #endif 153 #if NGENFB > 0 154 if (genfb_borrow(map->offset, &map->bsh)) 155 return bus_space_vaddr(map->bst, map->bsh); 156 #endif 157 DRM_DEBUG("ioremap: failed to map (%d)\n", 158 reason); 159 return NULL; 160 } 161 162 dev->pci_map_data[i].vaddr = bus_space_vaddr(map->bst, 163 dev->pci_map_data[i].bsh); 164 165 /* Caller might have requested a submapping of that */ 166 if ((reason = bus_space_subregion( 167 dev->pa.pa_memt, 168 dev->pci_map_data[i].bsh, 169 map->offset - dev->pci_map_data[i].base, 170 map->size, &h)) != 0) { 171 DRM_DEBUG("ioremap failed to " 172 "bus_space_subregion: %d\n", 173 reason); 174 return NULL; 175 } 176 177 DRM_DEBUG("ioremap mem found for %lx, %lx: %p\n", 178 map->offset, map->size, 179 dev->agp_map_data[i].vaddr); 180 181 map->bsh = h; 182 map->handle = bus_space_vaddr(dev->pa.pa_memt, h); 183 return map->handle; 184 } 185 } 186 /* failed to find a valid mapping; all hope isn't lost though */ 187 for(i = 0; i<DRM_MAX_PCI_RESOURCE; i++) { 188 if (dev->agp_map_data[i].mapped > 0 && 189 dev->agp_map_data[i].base == map->offset && 190 dev->agp_map_data[i].size >= map->size) { 191 map->bst = dev->pa.pa_memt; 192 map->fullmap = &(dev->agp_map_data[i]); 193 map->mapsize = dev->agp_map_data[i].size; 194 dev->agp_map_data[i].mapped++; 195 map->bsh = dev->agp_map_data[i].bsh; 196 return dev->agp_map_data[i].vaddr; 197 } 198 if (dev->agp_map_data[i].mapped == 0) { 199 int rv; 200 201 map->bst = dev->pa.pa_memt; 202 dev->agp_map_data[i].mapped++; 203 dev->agp_map_data[i].base = map->offset; 204 dev->agp_map_data[i].size = map->size; 205 dev->agp_map_data[i].flags = BUS_SPACE_MAP_LINEAR | 206 BUS_SPACE_MAP_PREFETCHABLE; 207 dev->agp_map_data[i].maptype = PCI_MAPREG_TYPE_MEM; 208 map->fullmap = &(dev->agp_map_data[i]); 209 map->mapsize = dev->agp_map_data[i].size; 210 211 DRM_DEBUG("ioremap%s: flags %d\n", wc ? "_wc" : "", 212 dev->agp_map_data[i].flags); 213 rv = bus_space_map(map->bst, map->offset, 214 dev->agp_map_data[i].size, 215 dev->agp_map_data[i].flags, &map->bsh); 216 if (rv) { 217 dev->agp_map_data[i].mapped--; 218 DRM_DEBUG("ioremap: failed to map (%d)\n", rv); 219 return NULL; 220 } 221 dev->agp_map_data[i].bsh = map->bsh; 222 dev->agp_map_data[i].vaddr = 223 bus_space_vaddr(map->bst, map->bsh); 224 DRM_DEBUG("ioremap agp mem found for %lx, %lx: %p\n", 225 map->offset, map->size, 226 dev->agp_map_data[i].vaddr); 227 return dev->agp_map_data[i].vaddr; 228 } 229 } 230 231 /* now we can give up... */ 232 DRM_DEBUG("drm_ioremap failed: offset=%lx size=%lu\n", 233 map->offset, map->size); 234 return NULL; 235 } 236 #endif 237 238 void *drm_ioremap_wc(struct drm_device *dev, drm_local_map_t *map) 239 { 240 #if defined(__FreeBSD__) 241 return pmap_mapdev_attr(map->offset, map->size, PAT_WRITE_COMBINING); 242 #elif defined(__NetBSD__) 243 return drm_netbsd_ioremap(dev, map, 1); 244 #endif 245 } 246 247 void *drm_ioremap(struct drm_device *dev, drm_local_map_t *map) 248 { 249 #if defined(__FreeBSD__) 250 return pmap_mapdev(map->offset, map->size); 251 #elif defined(__NetBSD__) 252 return drm_netbsd_ioremap(dev, map, 0); 253 #endif 254 } 255 256 void drm_ioremapfree(drm_local_map_t *map) 257 { 258 #if defined(__FreeBSD__) 259 pmap_unmapdev((vm_offset_t) map->handle, map->size); 260 #elif defined(__NetBSD__) 261 if (map->fullmap == NULL) { 262 DRM_INFO("drm_ioremapfree called for unknown map\n"); 263 return; 264 } 265 266 if (map->fullmap->mapped > 0) { 267 map->fullmap->mapped--; 268 if(map->fullmap->mapped == 0) 269 bus_space_unmap(map->bst, map->fullmap->bsh, 270 map->fullmap->size); 271 } 272 #endif 273 } 274 275 #if defined(__FreeBSD__) 276 int 277 drm_mtrr_add(unsigned long offset, size_t size, int flags) 278 { 279 int act; 280 struct mem_range_desc mrdesc; 281 282 mrdesc.mr_base = offset; 283 mrdesc.mr_len = size; 284 mrdesc.mr_flags = flags; 285 act = MEMRANGE_SET_UPDATE; 286 strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner)); 287 return mem_range_attr_set(&mrdesc, &act); 288 } 289 290 int 291 drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags) 292 { 293 int act; 294 struct mem_range_desc mrdesc; 295 296 mrdesc.mr_base = offset; 297 mrdesc.mr_len = size; 298 mrdesc.mr_flags = flags; 299 act = MEMRANGE_SET_REMOVE; 300 strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner)); 301 return mem_range_attr_set(&mrdesc, &act); 302 } 303 #elif defined(__NetBSD__) 304 int 305 drm_mtrr_add(unsigned long offset, size_t size, int flags) 306 { 307 #if defined(MTRR) && defined(MTRR_GETSET_KERNEL) 308 struct mtrr mtrrmap; 309 int one = 1; 310 311 mtrrmap.base = offset; 312 mtrrmap.len = size; 313 mtrrmap.type = flags; 314 mtrrmap.flags = MTRR_VALID; 315 return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL); 316 #else 317 return 0; 318 #endif 319 } 320 321 int 322 drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags) 323 { 324 #if defined(MTRR) && defined(MTRR_GETSET_KERNEL) 325 struct mtrr mtrrmap; 326 int one = 1; 327 328 mtrrmap.base = offset; 329 mtrrmap.len = size; 330 mtrrmap.type = flags; 331 mtrrmap.flags = 0; 332 return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL); 333 #else 334 return 0; 335 #endif 336 } 337 #endif 338