dma-swiotlb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Contains routines needed to support swiotlb for ppc.
  3. *
  4. * Copyright (C) 2009 Becky Bruce, Freescale Semiconductor
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. */
  12. #include <linux/dma-mapping.h>
  13. #include <linux/pfn.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pci.h>
  17. #include <asm/machdep.h>
  18. #include <asm/swiotlb.h>
  19. #include <asm/dma.h>
  20. #include <asm/abs_addr.h>
  21. int swiotlb __read_mostly;
  22. unsigned int ppc_swiotlb_enable;
  23. /*
  24. * Determine if an address is reachable by a pci device, or if we must bounce.
  25. */
  26. static int
  27. swiotlb_pci_addr_needs_map(struct device *hwdev, dma_addr_t addr, size_t size)
  28. {
  29. dma_addr_t max;
  30. struct pci_controller *hose;
  31. struct pci_dev *pdev = to_pci_dev(hwdev);
  32. hose = pci_bus_to_host(pdev->bus);
  33. max = hose->dma_window_base_cur + hose->dma_window_size;
  34. /* check that we're within mapped pci window space */
  35. if ((addr + size > max) | (addr < hose->dma_window_base_cur))
  36. return 1;
  37. return 0;
  38. }
  39. /*
  40. * At the moment, all platforms that use this code only require
  41. * swiotlb to be used if we're operating on HIGHMEM. Since
  42. * we don't ever call anything other than map_sg, unmap_sg,
  43. * map_page, and unmap_page on highmem, use normal dma_ops
  44. * for everything else.
  45. */
  46. struct dma_mapping_ops swiotlb_dma_ops = {
  47. .alloc_coherent = dma_direct_alloc_coherent,
  48. .free_coherent = dma_direct_free_coherent,
  49. .map_sg = swiotlb_map_sg_attrs,
  50. .unmap_sg = swiotlb_unmap_sg_attrs,
  51. .dma_supported = swiotlb_dma_supported,
  52. .map_page = swiotlb_map_page,
  53. .unmap_page = swiotlb_unmap_page,
  54. .sync_single_range_for_cpu = swiotlb_sync_single_range_for_cpu,
  55. .sync_single_range_for_device = swiotlb_sync_single_range_for_device,
  56. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  57. .sync_sg_for_device = swiotlb_sync_sg_for_device
  58. };
  59. struct dma_mapping_ops swiotlb_pci_dma_ops = {
  60. .alloc_coherent = dma_direct_alloc_coherent,
  61. .free_coherent = dma_direct_free_coherent,
  62. .map_sg = swiotlb_map_sg_attrs,
  63. .unmap_sg = swiotlb_unmap_sg_attrs,
  64. .dma_supported = swiotlb_dma_supported,
  65. .map_page = swiotlb_map_page,
  66. .unmap_page = swiotlb_unmap_page,
  67. .addr_needs_map = swiotlb_pci_addr_needs_map,
  68. .sync_single_range_for_cpu = swiotlb_sync_single_range_for_cpu,
  69. .sync_single_range_for_device = swiotlb_sync_single_range_for_device,
  70. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  71. .sync_sg_for_device = swiotlb_sync_sg_for_device
  72. };
  73. static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
  74. unsigned long action, void *data)
  75. {
  76. struct device *dev = data;
  77. /* We are only intereted in device addition */
  78. if (action != BUS_NOTIFY_ADD_DEVICE)
  79. return 0;
  80. /* May need to bounce if the device can't address all of DRAM */
  81. if (dma_get_mask(dev) < lmb_end_of_DRAM())
  82. set_dma_ops(dev, &swiotlb_dma_ops);
  83. return NOTIFY_DONE;
  84. }
  85. static struct notifier_block ppc_swiotlb_plat_bus_notifier = {
  86. .notifier_call = ppc_swiotlb_bus_notify,
  87. .priority = 0,
  88. };
  89. static struct notifier_block ppc_swiotlb_of_bus_notifier = {
  90. .notifier_call = ppc_swiotlb_bus_notify,
  91. .priority = 0,
  92. };
  93. int __init swiotlb_setup_bus_notifier(void)
  94. {
  95. bus_register_notifier(&platform_bus_type,
  96. &ppc_swiotlb_plat_bus_notifier);
  97. bus_register_notifier(&of_platform_bus_type,
  98. &ppc_swiotlb_of_bus_notifier);
  99. return 0;
  100. }