dma-swiotlb.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Contains routines needed to support swiotlb for ppc.
  3. *
  4. * Copyright (C) 2009-2010 Freescale Semiconductor, Inc.
  5. * Author: Becky Bruce
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. */
  13. #include <linux/dma-mapping.h>
  14. #include <linux/memblock.h>
  15. #include <linux/pfn.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pci.h>
  19. #include <asm/machdep.h>
  20. #include <asm/swiotlb.h>
  21. #include <asm/dma.h>
  22. unsigned int ppc_swiotlb_enable;
  23. static u64 swiotlb_powerpc_get_required(struct device *dev)
  24. {
  25. u64 end, mask, max_direct_dma_addr = dev->archdata.max_direct_dma_addr;
  26. end = memblock_end_of_DRAM();
  27. if (max_direct_dma_addr && end > max_direct_dma_addr)
  28. end = max_direct_dma_addr;
  29. end += get_dma_offset(dev);
  30. mask = 1ULL << (fls64(end) - 1);
  31. mask += mask - 1;
  32. return mask;
  33. }
  34. /*
  35. * At the moment, all platforms that use this code only require
  36. * swiotlb to be used if we're operating on HIGHMEM. Since
  37. * we don't ever call anything other than map_sg, unmap_sg,
  38. * map_page, and unmap_page on highmem, use normal dma_ops
  39. * for everything else.
  40. */
  41. struct dma_map_ops swiotlb_dma_ops = {
  42. .alloc = __dma_direct_alloc_coherent,
  43. .free = __dma_direct_free_coherent,
  44. .mmap = dma_direct_mmap_coherent,
  45. .map_sg = swiotlb_map_sg_attrs,
  46. .unmap_sg = swiotlb_unmap_sg_attrs,
  47. .dma_supported = swiotlb_dma_supported,
  48. .map_page = swiotlb_map_page,
  49. .unmap_page = swiotlb_unmap_page,
  50. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  51. .sync_single_for_device = swiotlb_sync_single_for_device,
  52. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  53. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  54. .mapping_error = swiotlb_dma_mapping_error,
  55. .get_required_mask = swiotlb_powerpc_get_required,
  56. };
  57. void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
  58. {
  59. struct pci_controller *hose;
  60. struct dev_archdata *sd;
  61. hose = pci_bus_to_host(pdev->bus);
  62. sd = &pdev->dev.archdata;
  63. sd->max_direct_dma_addr =
  64. hose->dma_window_base_cur + hose->dma_window_size;
  65. }
  66. static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
  67. unsigned long action, void *data)
  68. {
  69. struct device *dev = data;
  70. struct dev_archdata *sd;
  71. /* We are only intereted in device addition */
  72. if (action != BUS_NOTIFY_ADD_DEVICE)
  73. return 0;
  74. sd = &dev->archdata;
  75. sd->max_direct_dma_addr = 0;
  76. /* May need to bounce if the device can't address all of DRAM */
  77. if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM())
  78. set_dma_ops(dev, &swiotlb_dma_ops);
  79. return NOTIFY_DONE;
  80. }
  81. static struct notifier_block ppc_swiotlb_plat_bus_notifier = {
  82. .notifier_call = ppc_swiotlb_bus_notify,
  83. .priority = 0,
  84. };
  85. int __init swiotlb_setup_bus_notifier(void)
  86. {
  87. bus_register_notifier(&platform_bus_type,
  88. &ppc_swiotlb_plat_bus_notifier);
  89. return 0;
  90. }
  91. void __init swiotlb_detect_4g(void)
  92. {
  93. if ((memblock_end_of_DRAM() - 1) > 0xffffffff) {
  94. ppc_swiotlb_enable = 1;
  95. #ifdef CONFIG_ZONE_DMA32
  96. limit_zone_pfn(ZONE_DMA32, (1ULL << 32) >> PAGE_SHIFT);
  97. #endif
  98. }
  99. }
  100. static int __init check_swiotlb_enabled(void)
  101. {
  102. if (ppc_swiotlb_enable)
  103. swiotlb_print_info();
  104. else
  105. swiotlb_free();
  106. return 0;
  107. }
  108. subsys_initcall(check_swiotlb_enabled);