Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: ioport.c,v 1.45 2001/10/30 04:54:21 davem Exp $ |
| 2 | * ioport.c: Simple io mapping allocator. |
| 3 | * |
| 4 | * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) |
| 5 | * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx) |
| 6 | * |
| 7 | * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev. |
| 8 | * |
| 9 | * 2000/01/29 |
| 10 | * <rth> zait: as long as pci_alloc_consistent produces something addressable, |
| 11 | * things are ok. |
| 12 | * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a |
| 13 | * pointer into the big page mapping |
| 14 | * <rth> zait: so what? |
| 15 | * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page())) |
| 16 | * <zaitcev> Hmm |
| 17 | * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())). |
| 18 | * So far so good. |
| 19 | * <zaitcev> Now, driver calls pci_free_consistent(with result of |
| 20 | * remap_it_my_way()). |
| 21 | * <zaitcev> How do you find the address to pass to free_pages()? |
| 22 | * <rth> zait: walk the page tables? It's only two or three level after all. |
| 23 | * <rth> zait: you have to walk them anyway to remove the mapping. |
| 24 | * <zaitcev> Hmm |
| 25 | * <zaitcev> Sounds reasonable |
| 26 | */ |
| 27 | |
| 28 | #include <linux/config.h> |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/errno.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/ioport.h> |
| 34 | #include <linux/mm.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/pci.h> /* struct pci_dev */ |
| 37 | #include <linux/proc_fs.h> |
| 38 | |
| 39 | #include <asm/io.h> |
| 40 | #include <asm/vaddrs.h> |
| 41 | #include <asm/oplib.h> |
| 42 | #include <asm/page.h> |
| 43 | #include <asm/pgalloc.h> |
| 44 | #include <asm/dma.h> |
| 45 | |
| 46 | #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */ |
| 47 | |
| 48 | struct resource *_sparc_find_resource(struct resource *r, unsigned long); |
| 49 | |
| 50 | static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz); |
| 51 | static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, |
| 52 | unsigned long size, char *name); |
| 53 | static void _sparc_free_io(struct resource *res); |
| 54 | |
| 55 | /* This points to the next to use virtual memory for DVMA mappings */ |
| 56 | static struct resource _sparc_dvma = { |
| 57 | .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1 |
| 58 | }; |
| 59 | /* This points to the start of I/O mappings, cluable from outside. */ |
| 60 | /*ext*/ struct resource sparc_iomap = { |
| 61 | .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1 |
| 62 | }; |
| 63 | |
| 64 | /* |
| 65 | * Our mini-allocator... |
| 66 | * Boy this is gross! We need it because we must map I/O for |
| 67 | * timers and interrupt controller before the kmalloc is available. |
| 68 | */ |
| 69 | |
| 70 | #define XNMLN 15 |
| 71 | #define XNRES 10 /* SS-10 uses 8 */ |
| 72 | |
| 73 | struct xresource { |
| 74 | struct resource xres; /* Must be first */ |
| 75 | int xflag; /* 1 == used */ |
| 76 | char xname[XNMLN+1]; |
| 77 | }; |
| 78 | |
| 79 | static struct xresource xresv[XNRES]; |
| 80 | |
| 81 | static struct xresource *xres_alloc(void) { |
| 82 | struct xresource *xrp; |
| 83 | int n; |
| 84 | |
| 85 | xrp = xresv; |
| 86 | for (n = 0; n < XNRES; n++) { |
| 87 | if (xrp->xflag == 0) { |
| 88 | xrp->xflag = 1; |
| 89 | return xrp; |
| 90 | } |
| 91 | xrp++; |
| 92 | } |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | static void xres_free(struct xresource *xrp) { |
| 97 | xrp->xflag = 0; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * These are typically used in PCI drivers |
| 102 | * which are trying to be cross-platform. |
| 103 | * |
| 104 | * Bus type is always zero on IIep. |
| 105 | */ |
| 106 | void __iomem *ioremap(unsigned long offset, unsigned long size) |
| 107 | { |
| 108 | char name[14]; |
| 109 | |
| 110 | sprintf(name, "phys_%08x", (u32)offset); |
| 111 | return _sparc_alloc_io(0, offset, size, name); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Comlimentary to ioremap(). |
| 116 | */ |
| 117 | void iounmap(volatile void __iomem *virtual) |
| 118 | { |
| 119 | unsigned long vaddr = (unsigned long) virtual & PAGE_MASK; |
| 120 | struct resource *res; |
| 121 | |
| 122 | if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) { |
| 123 | printk("free_io/iounmap: cannot free %lx\n", vaddr); |
| 124 | return; |
| 125 | } |
| 126 | _sparc_free_io(res); |
| 127 | |
| 128 | if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) { |
| 129 | xres_free((struct xresource *)res); |
| 130 | } else { |
| 131 | kfree(res); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | */ |
| 137 | void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset, |
| 138 | unsigned long size, char *name) |
| 139 | { |
| 140 | return _sparc_alloc_io(phyres->flags & 0xF, |
| 141 | phyres->start + offset, size, name); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | */ |
| 146 | void sbus_iounmap(volatile void __iomem *addr, unsigned long size) |
| 147 | { |
| 148 | iounmap(addr); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Meat of mapping |
| 153 | */ |
| 154 | static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, |
| 155 | unsigned long size, char *name) |
| 156 | { |
| 157 | static int printed_full; |
| 158 | struct xresource *xres; |
| 159 | struct resource *res; |
| 160 | char *tack; |
| 161 | int tlen; |
| 162 | void __iomem *va; /* P3 diag */ |
| 163 | |
| 164 | if (name == NULL) name = "???"; |
| 165 | |
| 166 | if ((xres = xres_alloc()) != 0) { |
| 167 | tack = xres->xname; |
| 168 | res = &xres->xres; |
| 169 | } else { |
| 170 | if (!printed_full) { |
| 171 | printk("ioremap: done with statics, switching to malloc\n"); |
| 172 | printed_full = 1; |
| 173 | } |
| 174 | tlen = strlen(name); |
| 175 | tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL); |
| 176 | if (tack == NULL) return NULL; |
| 177 | memset(tack, 0, sizeof(struct resource)); |
| 178 | res = (struct resource *) tack; |
| 179 | tack += sizeof (struct resource); |
| 180 | } |
| 181 | |
| 182 | strlcpy(tack, name, XNMLN+1); |
| 183 | res->name = tack; |
| 184 | |
| 185 | va = _sparc_ioremap(res, busno, phys, size); |
| 186 | /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */ |
| 187 | return va; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | */ |
| 192 | static void __iomem * |
| 193 | _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz) |
| 194 | { |
| 195 | unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK); |
| 196 | |
| 197 | if (allocate_resource(&sparc_iomap, res, |
| 198 | (offset + sz + PAGE_SIZE-1) & PAGE_MASK, |
| 199 | sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) { |
| 200 | /* Usually we cannot see printks in this case. */ |
| 201 | prom_printf("alloc_io_res(%s): cannot occupy\n", |
| 202 | (res->name != NULL)? res->name: "???"); |
| 203 | prom_halt(); |
| 204 | } |
| 205 | |
| 206 | pa &= PAGE_MASK; |
| 207 | sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1); |
| 208 | |
| 209 | return (void __iomem *) (res->start + offset); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Comlimentary to _sparc_ioremap(). |
| 214 | */ |
| 215 | static void _sparc_free_io(struct resource *res) |
| 216 | { |
| 217 | unsigned long plen; |
| 218 | |
| 219 | plen = res->end - res->start + 1; |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 220 | BUG_ON((plen & (PAGE_SIZE-1)) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | sparc_unmapiorange(res->start, plen); |
| 222 | release_resource(res); |
| 223 | } |
| 224 | |
| 225 | #ifdef CONFIG_SBUS |
| 226 | |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame^] | 227 | void sbus_set_sbus64(struct sbus_dev *sdev, int x) |
| 228 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | printk("sbus_set_sbus64: unsupported\n"); |
| 230 | } |
| 231 | |
David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame^] | 232 | extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq); |
| 233 | void __init sbus_fill_device_irq(struct sbus_dev *sdev) |
| 234 | { |
| 235 | struct linux_prom_irqs irqs[PROMINTR_MAX]; |
| 236 | int len; |
| 237 | |
| 238 | len = prom_getproperty(sdev->prom_node, "intr", |
| 239 | (char *)irqs, sizeof(irqs)); |
| 240 | if (len != -1) { |
| 241 | sdev->num_irqs = len / 8; |
| 242 | if (sdev->num_irqs == 0) { |
| 243 | sdev->irqs[0] = 0; |
| 244 | } else if (sparc_cpu_model == sun4d) { |
| 245 | for (len = 0; len < sdev->num_irqs; len++) |
| 246 | sdev->irqs[len] = |
| 247 | sun4d_build_irq(sdev, irqs[len].pri); |
| 248 | } else { |
| 249 | for (len = 0; len < sdev->num_irqs; len++) |
| 250 | sdev->irqs[len] = irqs[len].pri; |
| 251 | } |
| 252 | } else { |
| 253 | int interrupts[PROMINTR_MAX]; |
| 254 | |
| 255 | /* No "intr" node found-- check for "interrupts" node. |
| 256 | * This node contains SBus interrupt levels, not IPLs |
| 257 | * as in "intr", and no vector values. We convert |
| 258 | * SBus interrupt levels to PILs (platform specific). |
| 259 | */ |
| 260 | len = prom_getproperty(sdev->prom_node, "interrupts", |
| 261 | (char *)interrupts, sizeof(interrupts)); |
| 262 | if (len == -1) { |
| 263 | sdev->irqs[0] = 0; |
| 264 | sdev->num_irqs = 0; |
| 265 | } else { |
| 266 | sdev->num_irqs = len / sizeof(int); |
| 267 | for (len = 0; len < sdev->num_irqs; len++) { |
| 268 | sdev->irqs[len] = |
| 269 | sbint_to_irq(sdev, interrupts[len]); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | /* |
| 276 | * Allocate a chunk of memory suitable for DMA. |
| 277 | * Typically devices use them for control blocks. |
| 278 | * CPU may access them without any explicit flushing. |
| 279 | * |
| 280 | * XXX Some clever people know that sdev is not used and supply NULL. Watch. |
| 281 | */ |
| 282 | void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp) |
| 283 | { |
| 284 | unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; |
| 285 | unsigned long va; |
| 286 | struct resource *res; |
| 287 | int order; |
| 288 | |
| 289 | /* XXX why are some lenghts signed, others unsigned? */ |
| 290 | if (len <= 0) { |
| 291 | return NULL; |
| 292 | } |
| 293 | /* XXX So what is maxphys for us and how do drivers know it? */ |
| 294 | if (len > 256*1024) { /* __get_free_pages() limit */ |
| 295 | return NULL; |
| 296 | } |
| 297 | |
| 298 | order = get_order(len_total); |
Hugh Dickins | f3d48f0 | 2005-11-21 21:32:22 -0800 | [diff] [blame] | 299 | if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | goto err_nopages; |
| 301 | |
| 302 | if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) |
| 303 | goto err_nomem; |
| 304 | memset((char*)res, 0, sizeof(struct resource)); |
| 305 | |
| 306 | if (allocate_resource(&_sparc_dvma, res, len_total, |
| 307 | _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { |
| 308 | printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total); |
| 309 | goto err_nova; |
| 310 | } |
| 311 | mmu_inval_dma_area(va, len_total); |
| 312 | // XXX The mmu_map_dma_area does this for us below, see comments. |
| 313 | // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total); |
| 314 | /* |
| 315 | * XXX That's where sdev would be used. Currently we load |
| 316 | * all iommu tables with the same translations. |
| 317 | */ |
| 318 | if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0) |
| 319 | goto err_noiommu; |
| 320 | |
Martin Habets | 4cfbd7e | 2006-05-07 23:43:19 -0700 | [diff] [blame] | 321 | /* Set the resource name, if known. */ |
| 322 | if (sdev) { |
| 323 | res->name = sdev->prom_name; |
| 324 | } |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return (void *)res->start; |
| 327 | |
| 328 | err_noiommu: |
| 329 | release_resource(res); |
| 330 | err_nova: |
| 331 | free_pages(va, order); |
| 332 | err_nomem: |
| 333 | kfree(res); |
| 334 | err_nopages: |
| 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba) |
| 339 | { |
| 340 | struct resource *res; |
| 341 | struct page *pgv; |
| 342 | |
| 343 | if ((res = _sparc_find_resource(&_sparc_dvma, |
| 344 | (unsigned long)p)) == NULL) { |
| 345 | printk("sbus_free_consistent: cannot free %p\n", p); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | if (((unsigned long)p & (PAGE_SIZE-1)) != 0) { |
| 350 | printk("sbus_free_consistent: unaligned va %p\n", p); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | n = (n + PAGE_SIZE-1) & PAGE_MASK; |
| 355 | if ((res->end-res->start)+1 != n) { |
| 356 | printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n", |
| 357 | (long)((res->end-res->start)+1), n); |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | release_resource(res); |
| 362 | kfree(res); |
| 363 | |
| 364 | /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */ |
| 365 | pgv = mmu_translate_dvma(ba); |
| 366 | mmu_unmap_dma_area(ba, n); |
| 367 | |
| 368 | __free_pages(pgv, get_order(n)); |
| 369 | } |
| 370 | |
| 371 | /* |
| 372 | * Map a chunk of memory so that devices can see it. |
| 373 | * CPU view of this memory may be inconsistent with |
| 374 | * a device view and explicit flushing is necessary. |
| 375 | */ |
| 376 | dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction) |
| 377 | { |
| 378 | /* XXX why are some lenghts signed, others unsigned? */ |
| 379 | if (len <= 0) { |
| 380 | return 0; |
| 381 | } |
| 382 | /* XXX So what is maxphys for us and how do drivers know it? */ |
| 383 | if (len > 256*1024) { /* __get_free_pages() limit */ |
| 384 | return 0; |
| 385 | } |
| 386 | return mmu_get_scsi_one(va, len, sdev->bus); |
| 387 | } |
| 388 | |
| 389 | void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction) |
| 390 | { |
| 391 | mmu_release_scsi_one(ba, n, sdev->bus); |
| 392 | } |
| 393 | |
| 394 | int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) |
| 395 | { |
| 396 | mmu_get_scsi_sgl(sg, n, sdev->bus); |
| 397 | |
| 398 | /* |
| 399 | * XXX sparc64 can return a partial length here. sun4c should do this |
| 400 | * but it currently panics if it can't fulfill the request - Anton |
| 401 | */ |
| 402 | return n; |
| 403 | } |
| 404 | |
| 405 | void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) |
| 406 | { |
| 407 | mmu_release_scsi_sgl(sg, n, sdev->bus); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | */ |
| 412 | void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction) |
| 413 | { |
| 414 | #if 0 |
| 415 | unsigned long va; |
| 416 | struct resource *res; |
| 417 | |
| 418 | /* We do not need the resource, just print a message if invalid. */ |
| 419 | res = _sparc_find_resource(&_sparc_dvma, ba); |
| 420 | if (res == NULL) |
| 421 | panic("sbus_dma_sync_single: 0x%x\n", ba); |
| 422 | |
| 423 | va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */ |
| 424 | /* |
| 425 | * XXX This bogosity will be fixed with the iommu rewrite coming soon |
| 426 | * to a kernel near you. - Anton |
| 427 | */ |
| 428 | /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */ |
| 429 | #endif |
| 430 | } |
| 431 | |
| 432 | void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction) |
| 433 | { |
| 434 | #if 0 |
| 435 | unsigned long va; |
| 436 | struct resource *res; |
| 437 | |
| 438 | /* We do not need the resource, just print a message if invalid. */ |
| 439 | res = _sparc_find_resource(&_sparc_dvma, ba); |
| 440 | if (res == NULL) |
| 441 | panic("sbus_dma_sync_single: 0x%x\n", ba); |
| 442 | |
| 443 | va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */ |
| 444 | /* |
| 445 | * XXX This bogosity will be fixed with the iommu rewrite coming soon |
| 446 | * to a kernel near you. - Anton |
| 447 | */ |
| 448 | /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */ |
| 449 | #endif |
| 450 | } |
| 451 | |
| 452 | void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) |
| 453 | { |
| 454 | printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n"); |
| 455 | } |
| 456 | |
| 457 | void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction) |
| 458 | { |
| 459 | printk("sbus_dma_sync_sg_for_device: not implemented yet\n"); |
| 460 | } |
| 461 | #endif /* CONFIG_SBUS */ |
| 462 | |
| 463 | #ifdef CONFIG_PCI |
| 464 | |
| 465 | /* Allocate and map kernel buffer using consistent mode DMA for a device. |
| 466 | * hwdev should be valid struct pci_dev pointer for PCI devices. |
| 467 | */ |
| 468 | void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba) |
| 469 | { |
| 470 | unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; |
| 471 | unsigned long va; |
| 472 | struct resource *res; |
| 473 | int order; |
| 474 | |
| 475 | if (len == 0) { |
| 476 | return NULL; |
| 477 | } |
| 478 | if (len > 256*1024) { /* __get_free_pages() limit */ |
| 479 | return NULL; |
| 480 | } |
| 481 | |
| 482 | order = get_order(len_total); |
| 483 | va = __get_free_pages(GFP_KERNEL, order); |
| 484 | if (va == 0) { |
| 485 | printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT); |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) { |
| 490 | free_pages(va, order); |
| 491 | printk("pci_alloc_consistent: no core\n"); |
| 492 | return NULL; |
| 493 | } |
| 494 | memset((char*)res, 0, sizeof(struct resource)); |
| 495 | |
| 496 | if (allocate_resource(&_sparc_dvma, res, len_total, |
| 497 | _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { |
| 498 | printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total); |
| 499 | free_pages(va, order); |
| 500 | kfree(res); |
| 501 | return NULL; |
| 502 | } |
| 503 | mmu_inval_dma_area(va, len_total); |
| 504 | #if 0 |
| 505 | /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n", |
| 506 | (long)va, (long)res->start, (long)virt_to_phys(va), len_total); |
| 507 | #endif |
| 508 | sparc_mapiorange(0, virt_to_phys(va), res->start, len_total); |
| 509 | |
| 510 | *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */ |
| 511 | return (void *) res->start; |
| 512 | } |
| 513 | |
| 514 | /* Free and unmap a consistent DMA buffer. |
| 515 | * cpu_addr is what was returned from pci_alloc_consistent, |
| 516 | * size must be the same as what as passed into pci_alloc_consistent, |
| 517 | * and likewise dma_addr must be the same as what *dma_addrp was set to. |
| 518 | * |
| 519 | * References to the memory and mappings assosciated with cpu_addr/dma_addr |
| 520 | * past this call are illegal. |
| 521 | */ |
| 522 | void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba) |
| 523 | { |
| 524 | struct resource *res; |
| 525 | unsigned long pgp; |
| 526 | |
| 527 | if ((res = _sparc_find_resource(&_sparc_dvma, |
| 528 | (unsigned long)p)) == NULL) { |
| 529 | printk("pci_free_consistent: cannot free %p\n", p); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | if (((unsigned long)p & (PAGE_SIZE-1)) != 0) { |
| 534 | printk("pci_free_consistent: unaligned va %p\n", p); |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | n = (n + PAGE_SIZE-1) & PAGE_MASK; |
| 539 | if ((res->end-res->start)+1 != n) { |
| 540 | printk("pci_free_consistent: region 0x%lx asked 0x%lx\n", |
| 541 | (long)((res->end-res->start)+1), (long)n); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */ |
| 546 | mmu_inval_dma_area(pgp, n); |
| 547 | sparc_unmapiorange((unsigned long)p, n); |
| 548 | |
| 549 | release_resource(res); |
| 550 | kfree(res); |
| 551 | |
| 552 | free_pages(pgp, get_order(n)); |
| 553 | } |
| 554 | |
| 555 | /* Map a single buffer of the indicated size for DMA in streaming mode. |
| 556 | * The 32-bit bus address to use is returned. |
| 557 | * |
| 558 | * Once the device is given the dma address, the device owns this memory |
| 559 | * until either pci_unmap_single or pci_dma_sync_single_* is performed. |
| 560 | */ |
| 561 | dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size, |
| 562 | int direction) |
| 563 | { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 564 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | /* IIep is write-through, not flushing. */ |
| 566 | return virt_to_phys(ptr); |
| 567 | } |
| 568 | |
| 569 | /* Unmap a single streaming mode DMA translation. The dma_addr and size |
| 570 | * must match what was provided for in a previous pci_map_single call. All |
| 571 | * other usages are undefined. |
| 572 | * |
| 573 | * After this call, reads by the cpu to the buffer are guaranteed to see |
| 574 | * whatever the device wrote there. |
| 575 | */ |
| 576 | void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size, |
| 577 | int direction) |
| 578 | { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 579 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | if (direction != PCI_DMA_TODEVICE) { |
| 581 | mmu_inval_dma_area((unsigned long)phys_to_virt(ba), |
| 582 | (size + PAGE_SIZE-1) & PAGE_MASK); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * Same as pci_map_single, but with pages. |
| 588 | */ |
| 589 | dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page, |
| 590 | unsigned long offset, size_t size, int direction) |
| 591 | { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 592 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | /* IIep is write-through, not flushing. */ |
| 594 | return page_to_phys(page) + offset; |
| 595 | } |
| 596 | |
| 597 | void pci_unmap_page(struct pci_dev *hwdev, |
| 598 | dma_addr_t dma_address, size_t size, int direction) |
| 599 | { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 600 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /* mmu_inval_dma_area XXX */ |
| 602 | } |
| 603 | |
| 604 | /* Map a set of buffers described by scatterlist in streaming |
| 605 | * mode for DMA. This is the scather-gather version of the |
| 606 | * above pci_map_single interface. Here the scatter gather list |
| 607 | * elements are each tagged with the appropriate dma address |
| 608 | * and length. They are obtained via sg_dma_{address,length}(SG). |
| 609 | * |
| 610 | * NOTE: An implementation may be able to use a smaller number of |
| 611 | * DMA address/length pairs than there are SG table elements. |
| 612 | * (for example via virtual mapping capabilities) |
| 613 | * The routine returns the number of addr/length pairs actually |
| 614 | * used, at most nents. |
| 615 | * |
| 616 | * Device ownership issues as mentioned above for pci_map_single are |
| 617 | * the same here. |
| 618 | */ |
| 619 | int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, |
| 620 | int direction) |
| 621 | { |
| 622 | int n; |
| 623 | |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 624 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | /* IIep is write-through, not flushing. */ |
| 626 | for (n = 0; n < nents; n++) { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 627 | BUG_ON(page_address(sg->page) == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | sg->dvma_address = virt_to_phys(page_address(sg->page)); |
| 629 | sg->dvma_length = sg->length; |
| 630 | sg++; |
| 631 | } |
| 632 | return nents; |
| 633 | } |
| 634 | |
| 635 | /* Unmap a set of streaming mode DMA translations. |
| 636 | * Again, cpu read rules concerning calls here are the same as for |
| 637 | * pci_unmap_single() above. |
| 638 | */ |
| 639 | void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, |
| 640 | int direction) |
| 641 | { |
| 642 | int n; |
| 643 | |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 644 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (direction != PCI_DMA_TODEVICE) { |
| 646 | for (n = 0; n < nents; n++) { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 647 | BUG_ON(page_address(sg->page) == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | mmu_inval_dma_area( |
| 649 | (unsigned long) page_address(sg->page), |
| 650 | (sg->length + PAGE_SIZE-1) & PAGE_MASK); |
| 651 | sg++; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | /* Make physical memory consistent for a single |
| 657 | * streaming mode DMA translation before or after a transfer. |
| 658 | * |
| 659 | * If you perform a pci_map_single() but wish to interrogate the |
| 660 | * buffer using the cpu, yet do not wish to teardown the PCI dma |
| 661 | * mapping, you must call this function before doing so. At the |
| 662 | * next point you give the PCI dma address back to the card, you |
| 663 | * must first perform a pci_dma_sync_for_device, and then the |
| 664 | * device again owns the buffer. |
| 665 | */ |
| 666 | void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction) |
| 667 | { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 668 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if (direction != PCI_DMA_TODEVICE) { |
| 670 | mmu_inval_dma_area((unsigned long)phys_to_virt(ba), |
| 671 | (size + PAGE_SIZE-1) & PAGE_MASK); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction) |
| 676 | { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 677 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (direction != PCI_DMA_TODEVICE) { |
| 679 | mmu_inval_dma_area((unsigned long)phys_to_virt(ba), |
| 680 | (size + PAGE_SIZE-1) & PAGE_MASK); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /* Make physical memory consistent for a set of streaming |
| 685 | * mode DMA translations after a transfer. |
| 686 | * |
| 687 | * The same as pci_dma_sync_single_* but for a scatter-gather list, |
| 688 | * same rules and usage. |
| 689 | */ |
| 690 | void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction) |
| 691 | { |
| 692 | int n; |
| 693 | |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 694 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | if (direction != PCI_DMA_TODEVICE) { |
| 696 | for (n = 0; n < nents; n++) { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 697 | BUG_ON(page_address(sg->page) == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | mmu_inval_dma_area( |
| 699 | (unsigned long) page_address(sg->page), |
| 700 | (sg->length + PAGE_SIZE-1) & PAGE_MASK); |
| 701 | sg++; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction) |
| 707 | { |
| 708 | int n; |
| 709 | |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 710 | BUG_ON(direction == PCI_DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | if (direction != PCI_DMA_TODEVICE) { |
| 712 | for (n = 0; n < nents; n++) { |
Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 713 | BUG_ON(page_address(sg->page) == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | mmu_inval_dma_area( |
| 715 | (unsigned long) page_address(sg->page), |
| 716 | (sg->length + PAGE_SIZE-1) & PAGE_MASK); |
| 717 | sg++; |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | #endif /* CONFIG_PCI */ |
| 722 | |
| 723 | #ifdef CONFIG_PROC_FS |
| 724 | |
| 725 | static int |
| 726 | _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof, |
| 727 | void *data) |
| 728 | { |
| 729 | char *p = buf, *e = buf + length; |
| 730 | struct resource *r; |
| 731 | const char *nm; |
| 732 | |
| 733 | for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) { |
| 734 | if (p + 32 >= e) /* Better than nothing */ |
| 735 | break; |
| 736 | if ((nm = r->name) == 0) nm = "???"; |
| 737 | p += sprintf(p, "%08lx-%08lx: %s\n", r->start, r->end, nm); |
| 738 | } |
| 739 | |
| 740 | return p-buf; |
| 741 | } |
| 742 | |
| 743 | #endif /* CONFIG_PROC_FS */ |
| 744 | |
| 745 | /* |
| 746 | * This is a version of find_resource and it belongs to kernel/resource.c. |
| 747 | * Until we have agreement with Linus and Martin, it lingers here. |
| 748 | * |
| 749 | * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case. |
| 750 | * This probably warrants some sort of hashing. |
| 751 | */ |
| 752 | struct resource * |
| 753 | _sparc_find_resource(struct resource *root, unsigned long hit) |
| 754 | { |
| 755 | struct resource *tmp; |
| 756 | |
| 757 | for (tmp = root->child; tmp != 0; tmp = tmp->sibling) { |
| 758 | if (tmp->start <= hit && tmp->end >= hit) |
| 759 | return tmp; |
| 760 | } |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | void register_proc_sparc_ioport(void) |
| 765 | { |
| 766 | #ifdef CONFIG_PROC_FS |
| 767 | create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap); |
| 768 | create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma); |
| 769 | #endif |
| 770 | } |