| .. | .. |
|---|
| 38 | 38 | #include <linux/proc_fs.h> |
|---|
| 39 | 39 | #include <linux/seq_file.h> |
|---|
| 40 | 40 | #include <linux/scatterlist.h> |
|---|
| 41 | | -#include <linux/dma-noncoherent.h> |
|---|
| 41 | +#include <linux/dma-map-ops.h> |
|---|
| 42 | 42 | #include <linux/of_device.h> |
|---|
| 43 | 43 | |
|---|
| 44 | 44 | #include <asm/io.h> |
|---|
| .. | .. |
|---|
| 51 | 51 | #include <asm/iommu.h> |
|---|
| 52 | 52 | #include <asm/io-unit.h> |
|---|
| 53 | 53 | #include <asm/leon.h> |
|---|
| 54 | | - |
|---|
| 55 | | -const struct sparc32_dma_ops *sparc32_dma_ops; |
|---|
| 56 | 54 | |
|---|
| 57 | 55 | /* This function must make sure that caches and memory are coherent after DMA |
|---|
| 58 | 56 | * On LEON systems without cache snooping it flushes the entire D-CACHE. |
|---|
| .. | .. |
|---|
| 247 | 245 | release_resource(res); |
|---|
| 248 | 246 | } |
|---|
| 249 | 247 | |
|---|
| 248 | +unsigned long sparc_dma_alloc_resource(struct device *dev, size_t len) |
|---|
| 249 | +{ |
|---|
| 250 | + struct resource *res; |
|---|
| 251 | + |
|---|
| 252 | + res = kzalloc(sizeof(*res), GFP_KERNEL); |
|---|
| 253 | + if (!res) |
|---|
| 254 | + return 0; |
|---|
| 255 | + res->name = dev->of_node->full_name; |
|---|
| 256 | + |
|---|
| 257 | + if (allocate_resource(&_sparc_dvma, res, len, _sparc_dvma.start, |
|---|
| 258 | + _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { |
|---|
| 259 | + printk("%s: cannot occupy 0x%zx", __func__, len); |
|---|
| 260 | + kfree(res); |
|---|
| 261 | + return 0; |
|---|
| 262 | + } |
|---|
| 263 | + |
|---|
| 264 | + return res->start; |
|---|
| 265 | +} |
|---|
| 266 | + |
|---|
| 267 | +bool sparc_dma_free_resource(void *cpu_addr, size_t size) |
|---|
| 268 | +{ |
|---|
| 269 | + unsigned long addr = (unsigned long)cpu_addr; |
|---|
| 270 | + struct resource *res; |
|---|
| 271 | + |
|---|
| 272 | + res = lookup_resource(&_sparc_dvma, addr); |
|---|
| 273 | + if (!res) { |
|---|
| 274 | + printk("%s: cannot free %p\n", __func__, cpu_addr); |
|---|
| 275 | + return false; |
|---|
| 276 | + } |
|---|
| 277 | + |
|---|
| 278 | + if ((addr & (PAGE_SIZE - 1)) != 0) { |
|---|
| 279 | + printk("%s: unaligned va %p\n", __func__, cpu_addr); |
|---|
| 280 | + return false; |
|---|
| 281 | + } |
|---|
| 282 | + |
|---|
| 283 | + size = PAGE_ALIGN(size); |
|---|
| 284 | + if (resource_size(res) != size) { |
|---|
| 285 | + printk("%s: region 0x%lx asked 0x%zx\n", |
|---|
| 286 | + __func__, (long)resource_size(res), size); |
|---|
| 287 | + return false; |
|---|
| 288 | + } |
|---|
| 289 | + |
|---|
| 290 | + release_resource(res); |
|---|
| 291 | + kfree(res); |
|---|
| 292 | + return true; |
|---|
| 293 | +} |
|---|
| 294 | + |
|---|
| 250 | 295 | #ifdef CONFIG_SBUS |
|---|
| 251 | 296 | |
|---|
| 252 | 297 | void sbus_set_sbus64(struct device *dev, int x) |
|---|
| .. | .. |
|---|
| 254 | 299 | printk("sbus_set_sbus64: unsupported\n"); |
|---|
| 255 | 300 | } |
|---|
| 256 | 301 | EXPORT_SYMBOL(sbus_set_sbus64); |
|---|
| 257 | | - |
|---|
| 258 | | -/* |
|---|
| 259 | | - * Allocate a chunk of memory suitable for DMA. |
|---|
| 260 | | - * Typically devices use them for control blocks. |
|---|
| 261 | | - * CPU may access them without any explicit flushing. |
|---|
| 262 | | - */ |
|---|
| 263 | | -static void *sbus_alloc_coherent(struct device *dev, size_t len, |
|---|
| 264 | | - dma_addr_t *dma_addrp, gfp_t gfp, |
|---|
| 265 | | - unsigned long attrs) |
|---|
| 266 | | -{ |
|---|
| 267 | | - struct platform_device *op = to_platform_device(dev); |
|---|
| 268 | | - unsigned long len_total = PAGE_ALIGN(len); |
|---|
| 269 | | - unsigned long va; |
|---|
| 270 | | - struct resource *res; |
|---|
| 271 | | - int order; |
|---|
| 272 | | - |
|---|
| 273 | | - /* XXX why are some lengths signed, others unsigned? */ |
|---|
| 274 | | - if (len <= 0) { |
|---|
| 275 | | - return NULL; |
|---|
| 276 | | - } |
|---|
| 277 | | - /* XXX So what is maxphys for us and how do drivers know it? */ |
|---|
| 278 | | - if (len > 256*1024) { /* __get_free_pages() limit */ |
|---|
| 279 | | - return NULL; |
|---|
| 280 | | - } |
|---|
| 281 | | - |
|---|
| 282 | | - order = get_order(len_total); |
|---|
| 283 | | - va = __get_free_pages(gfp, order); |
|---|
| 284 | | - if (va == 0) |
|---|
| 285 | | - goto err_nopages; |
|---|
| 286 | | - |
|---|
| 287 | | - if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) |
|---|
| 288 | | - goto err_nomem; |
|---|
| 289 | | - |
|---|
| 290 | | - if (allocate_resource(&_sparc_dvma, res, len_total, |
|---|
| 291 | | - _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { |
|---|
| 292 | | - printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total); |
|---|
| 293 | | - goto err_nova; |
|---|
| 294 | | - } |
|---|
| 295 | | - |
|---|
| 296 | | - // XXX The sbus_map_dma_area does this for us below, see comments. |
|---|
| 297 | | - // srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total); |
|---|
| 298 | | - /* |
|---|
| 299 | | - * XXX That's where sdev would be used. Currently we load |
|---|
| 300 | | - * all iommu tables with the same translations. |
|---|
| 301 | | - */ |
|---|
| 302 | | - if (sbus_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0) |
|---|
| 303 | | - goto err_noiommu; |
|---|
| 304 | | - |
|---|
| 305 | | - res->name = op->dev.of_node->name; |
|---|
| 306 | | - |
|---|
| 307 | | - return (void *)(unsigned long)res->start; |
|---|
| 308 | | - |
|---|
| 309 | | -err_noiommu: |
|---|
| 310 | | - release_resource(res); |
|---|
| 311 | | -err_nova: |
|---|
| 312 | | - kfree(res); |
|---|
| 313 | | -err_nomem: |
|---|
| 314 | | - free_pages(va, order); |
|---|
| 315 | | -err_nopages: |
|---|
| 316 | | - return NULL; |
|---|
| 317 | | -} |
|---|
| 318 | | - |
|---|
| 319 | | -static void sbus_free_coherent(struct device *dev, size_t n, void *p, |
|---|
| 320 | | - dma_addr_t ba, unsigned long attrs) |
|---|
| 321 | | -{ |
|---|
| 322 | | - struct resource *res; |
|---|
| 323 | | - struct page *pgv; |
|---|
| 324 | | - |
|---|
| 325 | | - if ((res = lookup_resource(&_sparc_dvma, |
|---|
| 326 | | - (unsigned long)p)) == NULL) { |
|---|
| 327 | | - printk("sbus_free_consistent: cannot free %p\n", p); |
|---|
| 328 | | - return; |
|---|
| 329 | | - } |
|---|
| 330 | | - |
|---|
| 331 | | - if (((unsigned long)p & (PAGE_SIZE-1)) != 0) { |
|---|
| 332 | | - printk("sbus_free_consistent: unaligned va %p\n", p); |
|---|
| 333 | | - return; |
|---|
| 334 | | - } |
|---|
| 335 | | - |
|---|
| 336 | | - n = PAGE_ALIGN(n); |
|---|
| 337 | | - if (resource_size(res) != n) { |
|---|
| 338 | | - printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n", |
|---|
| 339 | | - (long)resource_size(res), n); |
|---|
| 340 | | - return; |
|---|
| 341 | | - } |
|---|
| 342 | | - |
|---|
| 343 | | - release_resource(res); |
|---|
| 344 | | - kfree(res); |
|---|
| 345 | | - |
|---|
| 346 | | - pgv = virt_to_page(p); |
|---|
| 347 | | - sbus_unmap_dma_area(dev, ba, n); |
|---|
| 348 | | - |
|---|
| 349 | | - __free_pages(pgv, get_order(n)); |
|---|
| 350 | | -} |
|---|
| 351 | | - |
|---|
| 352 | | -/* |
|---|
| 353 | | - * Map a chunk of memory so that devices can see it. |
|---|
| 354 | | - * CPU view of this memory may be inconsistent with |
|---|
| 355 | | - * a device view and explicit flushing is necessary. |
|---|
| 356 | | - */ |
|---|
| 357 | | -static dma_addr_t sbus_map_page(struct device *dev, struct page *page, |
|---|
| 358 | | - unsigned long offset, size_t len, |
|---|
| 359 | | - enum dma_data_direction dir, |
|---|
| 360 | | - unsigned long attrs) |
|---|
| 361 | | -{ |
|---|
| 362 | | - void *va = page_address(page) + offset; |
|---|
| 363 | | - |
|---|
| 364 | | - /* XXX why are some lengths signed, others unsigned? */ |
|---|
| 365 | | - if (len <= 0) { |
|---|
| 366 | | - return 0; |
|---|
| 367 | | - } |
|---|
| 368 | | - /* XXX So what is maxphys for us and how do drivers know it? */ |
|---|
| 369 | | - if (len > 256*1024) { /* __get_free_pages() limit */ |
|---|
| 370 | | - return 0; |
|---|
| 371 | | - } |
|---|
| 372 | | - return mmu_get_scsi_one(dev, va, len); |
|---|
| 373 | | -} |
|---|
| 374 | | - |
|---|
| 375 | | -static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n, |
|---|
| 376 | | - enum dma_data_direction dir, unsigned long attrs) |
|---|
| 377 | | -{ |
|---|
| 378 | | - mmu_release_scsi_one(dev, ba, n); |
|---|
| 379 | | -} |
|---|
| 380 | | - |
|---|
| 381 | | -static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, |
|---|
| 382 | | - enum dma_data_direction dir, unsigned long attrs) |
|---|
| 383 | | -{ |
|---|
| 384 | | - mmu_get_scsi_sgl(dev, sg, n); |
|---|
| 385 | | - return n; |
|---|
| 386 | | -} |
|---|
| 387 | | - |
|---|
| 388 | | -static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, |
|---|
| 389 | | - enum dma_data_direction dir, unsigned long attrs) |
|---|
| 390 | | -{ |
|---|
| 391 | | - mmu_release_scsi_sgl(dev, sg, n); |
|---|
| 392 | | -} |
|---|
| 393 | | - |
|---|
| 394 | | -static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, |
|---|
| 395 | | - int n, enum dma_data_direction dir) |
|---|
| 396 | | -{ |
|---|
| 397 | | - BUG(); |
|---|
| 398 | | -} |
|---|
| 399 | | - |
|---|
| 400 | | -static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg, |
|---|
| 401 | | - int n, enum dma_data_direction dir) |
|---|
| 402 | | -{ |
|---|
| 403 | | - BUG(); |
|---|
| 404 | | -} |
|---|
| 405 | | - |
|---|
| 406 | | -static int sbus_dma_supported(struct device *dev, u64 mask) |
|---|
| 407 | | -{ |
|---|
| 408 | | - return 0; |
|---|
| 409 | | -} |
|---|
| 410 | | - |
|---|
| 411 | | -static const struct dma_map_ops sbus_dma_ops = { |
|---|
| 412 | | - .alloc = sbus_alloc_coherent, |
|---|
| 413 | | - .free = sbus_free_coherent, |
|---|
| 414 | | - .map_page = sbus_map_page, |
|---|
| 415 | | - .unmap_page = sbus_unmap_page, |
|---|
| 416 | | - .map_sg = sbus_map_sg, |
|---|
| 417 | | - .unmap_sg = sbus_unmap_sg, |
|---|
| 418 | | - .sync_sg_for_cpu = sbus_sync_sg_for_cpu, |
|---|
| 419 | | - .sync_sg_for_device = sbus_sync_sg_for_device, |
|---|
| 420 | | - .dma_supported = sbus_dma_supported, |
|---|
| 421 | | -}; |
|---|
| 422 | 302 | |
|---|
| 423 | 303 | static int __init sparc_register_ioport(void) |
|---|
| 424 | 304 | { |
|---|
| .. | .. |
|---|
| 438 | 318 | void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, |
|---|
| 439 | 319 | gfp_t gfp, unsigned long attrs) |
|---|
| 440 | 320 | { |
|---|
| 441 | | - unsigned long len_total = PAGE_ALIGN(size); |
|---|
| 321 | + unsigned long addr; |
|---|
| 442 | 322 | void *va; |
|---|
| 443 | | - struct resource *res; |
|---|
| 444 | | - int order; |
|---|
| 445 | 323 | |
|---|
| 446 | | - if (size == 0) { |
|---|
| 324 | + if (!size || size > 256 * 1024) /* __get_free_pages() limit */ |
|---|
| 447 | 325 | return NULL; |
|---|
| 448 | | - } |
|---|
| 449 | | - if (size > 256*1024) { /* __get_free_pages() limit */ |
|---|
| 326 | + |
|---|
| 327 | + size = PAGE_ALIGN(size); |
|---|
| 328 | + va = (void *) __get_free_pages(gfp | __GFP_ZERO, get_order(size)); |
|---|
| 329 | + if (!va) { |
|---|
| 330 | + printk("%s: no %zd pages\n", __func__, size >> PAGE_SHIFT); |
|---|
| 450 | 331 | return NULL; |
|---|
| 451 | 332 | } |
|---|
| 452 | 333 | |
|---|
| 453 | | - order = get_order(len_total); |
|---|
| 454 | | - va = (void *) __get_free_pages(gfp, order); |
|---|
| 455 | | - if (va == NULL) { |
|---|
| 456 | | - printk("%s: no %ld pages\n", __func__, len_total>>PAGE_SHIFT); |
|---|
| 457 | | - goto err_nopages; |
|---|
| 458 | | - } |
|---|
| 459 | | - |
|---|
| 460 | | - if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) { |
|---|
| 461 | | - printk("%s: no core\n", __func__); |
|---|
| 334 | + addr = sparc_dma_alloc_resource(dev, size); |
|---|
| 335 | + if (!addr) |
|---|
| 462 | 336 | goto err_nomem; |
|---|
| 463 | | - } |
|---|
| 464 | 337 | |
|---|
| 465 | | - if (allocate_resource(&_sparc_dvma, res, len_total, |
|---|
| 466 | | - _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { |
|---|
| 467 | | - printk("%s: cannot occupy 0x%lx", __func__, len_total); |
|---|
| 468 | | - goto err_nova; |
|---|
| 469 | | - } |
|---|
| 470 | | - srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total); |
|---|
| 338 | + srmmu_mapiorange(0, virt_to_phys(va), addr, size); |
|---|
| 471 | 339 | |
|---|
| 472 | 340 | *dma_handle = virt_to_phys(va); |
|---|
| 473 | | - return (void *) res->start; |
|---|
| 341 | + return (void *)addr; |
|---|
| 474 | 342 | |
|---|
| 475 | | -err_nova: |
|---|
| 476 | | - kfree(res); |
|---|
| 477 | 343 | err_nomem: |
|---|
| 478 | | - free_pages((unsigned long)va, order); |
|---|
| 479 | | -err_nopages: |
|---|
| 344 | + free_pages((unsigned long)va, get_order(size)); |
|---|
| 480 | 345 | return NULL; |
|---|
| 481 | 346 | } |
|---|
| 482 | 347 | |
|---|
| .. | .. |
|---|
| 491 | 356 | void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, |
|---|
| 492 | 357 | dma_addr_t dma_addr, unsigned long attrs) |
|---|
| 493 | 358 | { |
|---|
| 494 | | - struct resource *res; |
|---|
| 495 | | - |
|---|
| 496 | | - if ((res = lookup_resource(&_sparc_dvma, |
|---|
| 497 | | - (unsigned long)cpu_addr)) == NULL) { |
|---|
| 498 | | - printk("%s: cannot free %p\n", __func__, cpu_addr); |
|---|
| 499 | | - return; |
|---|
| 500 | | - } |
|---|
| 501 | | - |
|---|
| 502 | | - if (((unsigned long)cpu_addr & (PAGE_SIZE-1)) != 0) { |
|---|
| 503 | | - printk("%s: unaligned va %p\n", __func__, cpu_addr); |
|---|
| 504 | | - return; |
|---|
| 505 | | - } |
|---|
| 506 | | - |
|---|
| 507 | 359 | size = PAGE_ALIGN(size); |
|---|
| 508 | | - if (resource_size(res) != size) { |
|---|
| 509 | | - printk("%s: region 0x%lx asked 0x%zx\n", __func__, |
|---|
| 510 | | - (long)resource_size(res), size); |
|---|
| 360 | + |
|---|
| 361 | + if (!sparc_dma_free_resource(cpu_addr, size)) |
|---|
| 511 | 362 | return; |
|---|
| 512 | | - } |
|---|
| 513 | 363 | |
|---|
| 514 | 364 | dma_make_coherent(dma_addr, size); |
|---|
| 515 | 365 | srmmu_unmapiorange((unsigned long)cpu_addr, size); |
|---|
| 516 | | - |
|---|
| 517 | | - release_resource(res); |
|---|
| 518 | | - kfree(res); |
|---|
| 519 | 366 | free_pages((unsigned long)phys_to_virt(dma_addr), get_order(size)); |
|---|
| 520 | 367 | } |
|---|
| 521 | 368 | |
|---|
| 522 | 369 | /* IIep is write-through, not flushing on cpu to device transfer. */ |
|---|
| 523 | 370 | |
|---|
| 524 | | -void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr, |
|---|
| 525 | | - size_t size, enum dma_data_direction dir) |
|---|
| 371 | +void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, |
|---|
| 372 | + enum dma_data_direction dir) |
|---|
| 526 | 373 | { |
|---|
| 527 | 374 | if (dir != PCI_DMA_TODEVICE) |
|---|
| 528 | 375 | dma_make_coherent(paddr, PAGE_ALIGN(size)); |
|---|
| 529 | 376 | } |
|---|
| 530 | | - |
|---|
| 531 | | -const struct dma_map_ops *dma_ops = &sbus_dma_ops; |
|---|
| 532 | | -EXPORT_SYMBOL(dma_ops); |
|---|
| 533 | 377 | |
|---|
| 534 | 378 | #ifdef CONFIG_PROC_FS |
|---|
| 535 | 379 | |
|---|