iommu.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Support for IOMMU on Celleb platform.
  3. *
  4. * (C) Copyright 2006-2007 TOSHIBA CORPORATION
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/pci.h>
  24. #include <linux/of_platform.h>
  25. #include <asm/machdep.h>
  26. #include "beat_wrapper.h"
  27. #define DMA_FLAGS 0xf800000000000000UL /* r/w permitted, coherency required,
  28. strongest order */
  29. static int __init find_dma_window(u64 *io_space_id, u64 *ioid,
  30. u64 *base, u64 *size, u64 *io_page_size)
  31. {
  32. struct device_node *dn;
  33. const unsigned long *dma_window;
  34. for_each_node_by_type(dn, "ioif") {
  35. dma_window = of_get_property(dn, "toshiba,dma-window", NULL);
  36. if (dma_window) {
  37. *io_space_id = (dma_window[0] >> 32) & 0xffffffffUL;
  38. *ioid = dma_window[0] & 0x7ffUL;
  39. *base = dma_window[1];
  40. *size = dma_window[2];
  41. *io_page_size = 1 << dma_window[3];
  42. of_node_put(dn);
  43. return 1;
  44. }
  45. }
  46. return 0;
  47. }
  48. static void __init celleb_init_direct_mapping(void)
  49. {
  50. u64 lpar_addr, io_addr;
  51. u64 io_space_id, ioid, dma_base, dma_size, io_page_size;
  52. if (!find_dma_window(&io_space_id, &ioid, &dma_base, &dma_size,
  53. &io_page_size)) {
  54. pr_info("No dma window found !\n");
  55. return;
  56. }
  57. for (lpar_addr = 0; lpar_addr < dma_size; lpar_addr += io_page_size) {
  58. io_addr = lpar_addr + dma_base;
  59. (void)beat_put_iopte(io_space_id, io_addr, lpar_addr,
  60. ioid, DMA_FLAGS);
  61. }
  62. dma_direct_offset = dma_base;
  63. }
  64. static void celleb_dma_dev_setup(struct device *dev)
  65. {
  66. dev->archdata.dma_ops = get_pci_dma_ops();
  67. dev->archdata.dma_data = (void *)dma_direct_offset;
  68. }
  69. static void celleb_pci_dma_dev_setup(struct pci_dev *pdev)
  70. {
  71. celleb_dma_dev_setup(&pdev->dev);
  72. }
  73. static int celleb_of_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. celleb_dma_dev_setup(dev);
  81. return 0;
  82. }
  83. static struct notifier_block celleb_of_bus_notifier = {
  84. .notifier_call = celleb_of_bus_notify
  85. };
  86. static int __init celleb_init_iommu(void)
  87. {
  88. celleb_init_direct_mapping();
  89. set_pci_dma_ops(&dma_direct_ops);
  90. ppc_md.pci_dma_dev_setup = celleb_pci_dma_dev_setup;
  91. bus_register_notifier(&of_platform_bus_type, &celleb_of_bus_notifier);
  92. return 0;
  93. }
  94. machine_arch_initcall(celleb_beat, celleb_init_iommu);