iommu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * IOMMU implementation for Cell Broadband Processor Architecture
  3. * We just establish a linear mapping at boot by setting all the
  4. * IOPT cache entries in the CPU.
  5. * The mapping functions should be identical to pci_direct_iommu,
  6. * except for the handling of the high order bit that is required
  7. * by the Spider bridge. These should be split into a separate
  8. * file at the point where we get a different bridge chip.
  9. *
  10. * Copyright (C) 2005 IBM Deutschland Entwicklung GmbH,
  11. * Arnd Bergmann <arndb@de.ibm.com>
  12. *
  13. * Based on linear mapping
  14. * Copyright (C) 2003 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. */
  21. #undef DEBUG
  22. #include <linux/kernel.h>
  23. #include <linux/pci.h>
  24. #include <linux/delay.h>
  25. #include <linux/string.h>
  26. #include <linux/init.h>
  27. #include <linux/bootmem.h>
  28. #include <linux/mm.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/kernel.h>
  31. #include <linux/compiler.h>
  32. #include <asm/sections.h>
  33. #include <asm/iommu.h>
  34. #include <asm/io.h>
  35. #include <asm/prom.h>
  36. #include <asm/pci-bridge.h>
  37. #include <asm/machdep.h>
  38. #include <asm/pmac_feature.h>
  39. #include <asm/abs_addr.h>
  40. #include <asm/system.h>
  41. #include <asm/ppc-pci.h>
  42. #include <asm/udbg.h>
  43. #include "iommu.h"
  44. static inline unsigned long
  45. get_iopt_entry(unsigned long real_address, unsigned long ioid,
  46. unsigned long prot)
  47. {
  48. return (prot & IOPT_PROT_MASK)
  49. | (IOPT_COHERENT)
  50. | (IOPT_ORDER_VC)
  51. | (real_address & IOPT_RPN_MASK)
  52. | (ioid & IOPT_IOID_MASK);
  53. }
  54. typedef struct {
  55. unsigned long val;
  56. } ioste;
  57. static inline ioste
  58. mk_ioste(unsigned long val)
  59. {
  60. ioste ioste = { .val = val, };
  61. return ioste;
  62. }
  63. static inline ioste
  64. get_iost_entry(unsigned long iopt_base, unsigned long io_address, unsigned page_size)
  65. {
  66. unsigned long ps;
  67. unsigned long iostep;
  68. unsigned long nnpt;
  69. unsigned long shift;
  70. switch (page_size) {
  71. case 0x1000000:
  72. ps = IOST_PS_16M;
  73. nnpt = 0; /* one page per segment */
  74. shift = 5; /* segment has 16 iopt entries */
  75. break;
  76. case 0x100000:
  77. ps = IOST_PS_1M;
  78. nnpt = 0; /* one page per segment */
  79. shift = 1; /* segment has 256 iopt entries */
  80. break;
  81. case 0x10000:
  82. ps = IOST_PS_64K;
  83. nnpt = 0x07; /* 8 pages per io page table */
  84. shift = 0; /* all entries are used */
  85. break;
  86. case 0x1000:
  87. ps = IOST_PS_4K;
  88. nnpt = 0x7f; /* 128 pages per io page table */
  89. shift = 0; /* all entries are used */
  90. break;
  91. default: /* not a known compile time constant */
  92. {
  93. /* BUILD_BUG_ON() is not usable here */
  94. extern void __get_iost_entry_bad_page_size(void);
  95. __get_iost_entry_bad_page_size();
  96. }
  97. break;
  98. }
  99. iostep = iopt_base +
  100. /* need 8 bytes per iopte */
  101. (((io_address / page_size * 8)
  102. /* align io page tables on 4k page boundaries */
  103. << shift)
  104. /* nnpt+1 pages go into each iopt */
  105. & ~(nnpt << 12));
  106. nnpt++; /* this seems to work, but the documentation is not clear
  107. about wether we put nnpt or nnpt-1 into the ioste bits.
  108. In theory, this can't work for 4k pages. */
  109. return mk_ioste(IOST_VALID_MASK
  110. | (iostep & IOST_PT_BASE_MASK)
  111. | ((nnpt << 5) & IOST_NNPT_MASK)
  112. | (ps & IOST_PS_MASK));
  113. }
  114. /* compute the address of an io pte */
  115. static inline unsigned long
  116. get_ioptep(ioste iost_entry, unsigned long io_address)
  117. {
  118. unsigned long iopt_base;
  119. unsigned long page_size;
  120. unsigned long page_number;
  121. unsigned long iopt_offset;
  122. iopt_base = iost_entry.val & IOST_PT_BASE_MASK;
  123. page_size = iost_entry.val & IOST_PS_MASK;
  124. /* decode page size to compute page number */
  125. page_number = (io_address & 0x0fffffff) >> (10 + 2 * page_size);
  126. /* page number is an offset into the io page table */
  127. iopt_offset = (page_number << 3) & 0x7fff8ul;
  128. return iopt_base + iopt_offset;
  129. }
  130. /* compute the tag field of the iopt cache entry */
  131. static inline unsigned long
  132. get_ioc_tag(ioste iost_entry, unsigned long io_address)
  133. {
  134. unsigned long iopte = get_ioptep(iost_entry, io_address);
  135. return IOPT_VALID_MASK
  136. | ((iopte & 0x00000000000000ff8ul) >> 3)
  137. | ((iopte & 0x0000003fffffc0000ul) >> 9);
  138. }
  139. /* compute the hashed 6 bit index for the 4-way associative pte cache */
  140. static inline unsigned long
  141. get_ioc_hash(ioste iost_entry, unsigned long io_address)
  142. {
  143. unsigned long iopte = get_ioptep(iost_entry, io_address);
  144. return ((iopte & 0x000000000000001f8ul) >> 3)
  145. ^ ((iopte & 0x00000000000020000ul) >> 17)
  146. ^ ((iopte & 0x00000000000010000ul) >> 15)
  147. ^ ((iopte & 0x00000000000008000ul) >> 13)
  148. ^ ((iopte & 0x00000000000004000ul) >> 11)
  149. ^ ((iopte & 0x00000000000002000ul) >> 9)
  150. ^ ((iopte & 0x00000000000001000ul) >> 7);
  151. }
  152. /* same as above, but pretend that we have a simpler 1-way associative
  153. pte cache with an 8 bit index */
  154. static inline unsigned long
  155. get_ioc_hash_1way(ioste iost_entry, unsigned long io_address)
  156. {
  157. unsigned long iopte = get_ioptep(iost_entry, io_address);
  158. return ((iopte & 0x000000000000001f8ul) >> 3)
  159. ^ ((iopte & 0x00000000000020000ul) >> 17)
  160. ^ ((iopte & 0x00000000000010000ul) >> 15)
  161. ^ ((iopte & 0x00000000000008000ul) >> 13)
  162. ^ ((iopte & 0x00000000000004000ul) >> 11)
  163. ^ ((iopte & 0x00000000000002000ul) >> 9)
  164. ^ ((iopte & 0x00000000000001000ul) >> 7)
  165. ^ ((iopte & 0x0000000000000c000ul) >> 8);
  166. }
  167. static inline ioste
  168. get_iost_cache(void __iomem *base, unsigned long index)
  169. {
  170. unsigned long __iomem *p = (base + IOC_ST_CACHE_DIR);
  171. return mk_ioste(in_be64(&p[index]));
  172. }
  173. static inline void
  174. set_iost_cache(void __iomem *base, unsigned long index, ioste ste)
  175. {
  176. unsigned long __iomem *p = (base + IOC_ST_CACHE_DIR);
  177. pr_debug("ioste %02lx was %016lx, store %016lx", index,
  178. get_iost_cache(base, index).val, ste.val);
  179. out_be64(&p[index], ste.val);
  180. pr_debug(" now %016lx\n", get_iost_cache(base, index).val);
  181. }
  182. static inline unsigned long
  183. get_iopt_cache(void __iomem *base, unsigned long index, unsigned long *tag)
  184. {
  185. unsigned long __iomem *tags = (void *)(base + IOC_PT_CACHE_DIR);
  186. unsigned long __iomem *p = (void *)(base + IOC_PT_CACHE_REG);
  187. *tag = tags[index];
  188. rmb();
  189. return *p;
  190. }
  191. static inline void
  192. set_iopt_cache(void __iomem *base, unsigned long index,
  193. unsigned long tag, unsigned long val)
  194. {
  195. unsigned long __iomem *tags = base + IOC_PT_CACHE_DIR;
  196. unsigned long __iomem *p = base + IOC_PT_CACHE_REG;
  197. out_be64(p, val);
  198. out_be64(&tags[index], tag);
  199. }
  200. static inline void
  201. set_iost_origin(void __iomem *base)
  202. {
  203. unsigned long __iomem *p = base + IOC_ST_ORIGIN;
  204. unsigned long origin = IOSTO_ENABLE | IOSTO_SW;
  205. pr_debug("iost_origin %016lx, now %016lx\n", in_be64(p), origin);
  206. out_be64(p, origin);
  207. }
  208. static inline void
  209. set_iocmd_config(void __iomem *base)
  210. {
  211. unsigned long __iomem *p = base + 0xc00;
  212. unsigned long conf;
  213. conf = in_be64(p);
  214. pr_debug("iost_conf %016lx, now %016lx\n", conf, conf | IOCMD_CONF_TE);
  215. out_be64(p, conf | IOCMD_CONF_TE);
  216. }
  217. static void enable_mapping(void __iomem *base, void __iomem *mmio_base)
  218. {
  219. set_iocmd_config(base);
  220. set_iost_origin(mmio_base);
  221. }
  222. struct cell_iommu {
  223. unsigned long base;
  224. unsigned long mmio_base;
  225. void __iomem *mapped_base;
  226. void __iomem *mapped_mmio_base;
  227. };
  228. static struct cell_iommu cell_iommus[NR_CPUS];
  229. /* initialize the iommu to support a simple linear mapping
  230. * for each DMA window used by any device. For now, we
  231. * happen to know that there is only one DMA window in use,
  232. * starting at iopt_phys_offset. */
  233. static void cell_do_map_iommu(struct cell_iommu *iommu,
  234. unsigned int ioid,
  235. unsigned long map_start,
  236. unsigned long map_size)
  237. {
  238. unsigned long io_address, real_address;
  239. void __iomem *ioc_base, *ioc_mmio_base;
  240. ioste ioste;
  241. unsigned long index;
  242. /* we pretend the io page table was at a very high address */
  243. const unsigned long fake_iopt = 0x10000000000ul;
  244. const unsigned long io_page_size = 0x1000000; /* use 16M pages */
  245. const unsigned long io_segment_size = 0x10000000; /* 256M */
  246. ioc_base = iommu->mapped_base;
  247. ioc_mmio_base = iommu->mapped_mmio_base;
  248. for (real_address = 0, io_address = map_start;
  249. io_address <= map_start + map_size;
  250. real_address += io_page_size, io_address += io_page_size) {
  251. ioste = get_iost_entry(fake_iopt, io_address, io_page_size);
  252. if ((real_address % io_segment_size) == 0) /* segment start */
  253. set_iost_cache(ioc_mmio_base,
  254. io_address >> 28, ioste);
  255. index = get_ioc_hash_1way(ioste, io_address);
  256. pr_debug("addr %08lx, index %02lx, ioste %016lx\n",
  257. io_address, index, ioste.val);
  258. set_iopt_cache(ioc_mmio_base,
  259. get_ioc_hash_1way(ioste, io_address),
  260. get_ioc_tag(ioste, io_address),
  261. get_iopt_entry(real_address, ioid, IOPT_PROT_RW));
  262. }
  263. }
  264. static void pci_dma_cell_bus_setup(struct pci_bus *b)
  265. {
  266. const unsigned int *ioid;
  267. unsigned long map_start, map_size, token;
  268. const unsigned long *dma_window;
  269. struct cell_iommu *iommu;
  270. struct device_node *d;
  271. d = pci_bus_to_OF_node(b);
  272. ioid = get_property(d, "ioid", NULL);
  273. if (!ioid)
  274. pr_debug("No ioid entry found !\n");
  275. dma_window = get_property(d, "ibm,dma-window", NULL);
  276. if (!dma_window)
  277. pr_debug("No ibm,dma-window entry found !\n");
  278. map_start = dma_window[1];
  279. map_size = dma_window[2];
  280. token = dma_window[0] >> 32;
  281. iommu = &cell_iommus[token];
  282. cell_do_map_iommu(iommu, *ioid, map_start, map_size);
  283. }
  284. static int cell_map_iommu_hardcoded(int num_nodes)
  285. {
  286. struct cell_iommu *iommu = NULL;
  287. pr_debug("%s(%d): Using hardcoded defaults\n", __FUNCTION__, __LINE__);
  288. /* node 0 */
  289. iommu = &cell_iommus[0];
  290. iommu->mapped_base = ioremap(0x20000511000ul, 0x1000);
  291. iommu->mapped_mmio_base = ioremap(0x20000510000ul, 0x1000);
  292. enable_mapping(iommu->mapped_base, iommu->mapped_mmio_base);
  293. cell_do_map_iommu(iommu, 0x048a,
  294. 0x20000000ul,0x20000000ul);
  295. if (num_nodes < 2)
  296. return 0;
  297. /* node 1 */
  298. iommu = &cell_iommus[1];
  299. iommu->mapped_base = ioremap(0x30000511000ul, 0x1000);
  300. iommu->mapped_mmio_base = ioremap(0x30000510000ul, 0x1000);
  301. enable_mapping(iommu->mapped_base, iommu->mapped_mmio_base);
  302. cell_do_map_iommu(iommu, 0x048a,
  303. 0x20000000,0x20000000ul);
  304. return 0;
  305. }
  306. static int cell_map_iommu(void)
  307. {
  308. unsigned int num_nodes = 0;
  309. const unsigned int *node_id;
  310. const unsigned long *base, *mmio_base;
  311. struct device_node *dn;
  312. struct cell_iommu *iommu = NULL;
  313. /* determine number of nodes (=iommus) */
  314. pr_debug("%s(%d): determining number of nodes...", __FUNCTION__, __LINE__);
  315. for(dn = of_find_node_by_type(NULL, "cpu");
  316. dn;
  317. dn = of_find_node_by_type(dn, "cpu")) {
  318. node_id = get_property(dn, "node-id", NULL);
  319. if (num_nodes < *node_id)
  320. num_nodes = *node_id;
  321. }
  322. num_nodes++;
  323. pr_debug("%i found.\n", num_nodes);
  324. /* map the iommu registers for each node */
  325. pr_debug("%s(%d): Looping through nodes\n", __FUNCTION__, __LINE__);
  326. for(dn = of_find_node_by_type(NULL, "cpu");
  327. dn;
  328. dn = of_find_node_by_type(dn, "cpu")) {
  329. node_id = get_property(dn, "node-id", NULL);
  330. base = get_property(dn, "ioc-cache", NULL);
  331. mmio_base = get_property(dn, "ioc-translation", NULL);
  332. if (!base || !mmio_base || !node_id)
  333. return cell_map_iommu_hardcoded(num_nodes);
  334. iommu = &cell_iommus[*node_id];
  335. iommu->base = *base;
  336. iommu->mmio_base = *mmio_base;
  337. iommu->mapped_base = ioremap(*base, 0x1000);
  338. iommu->mapped_mmio_base = ioremap(*mmio_base, 0x1000);
  339. enable_mapping(iommu->mapped_base,
  340. iommu->mapped_mmio_base);
  341. /* everything else will be done in iommu_bus_setup */
  342. }
  343. return 1;
  344. }
  345. static void *cell_alloc_coherent(struct device *hwdev, size_t size,
  346. dma_addr_t *dma_handle, gfp_t flag)
  347. {
  348. void *ret;
  349. ret = (void *)__get_free_pages(flag, get_order(size));
  350. if (ret != NULL) {
  351. memset(ret, 0, size);
  352. *dma_handle = virt_to_abs(ret) | CELL_DMA_VALID;
  353. }
  354. return ret;
  355. }
  356. static void cell_free_coherent(struct device *hwdev, size_t size,
  357. void *vaddr, dma_addr_t dma_handle)
  358. {
  359. free_pages((unsigned long)vaddr, get_order(size));
  360. }
  361. static dma_addr_t cell_map_single(struct device *hwdev, void *ptr,
  362. size_t size, enum dma_data_direction direction)
  363. {
  364. return virt_to_abs(ptr) | CELL_DMA_VALID;
  365. }
  366. static void cell_unmap_single(struct device *hwdev, dma_addr_t dma_addr,
  367. size_t size, enum dma_data_direction direction)
  368. {
  369. }
  370. static int cell_map_sg(struct device *hwdev, struct scatterlist *sg,
  371. int nents, enum dma_data_direction direction)
  372. {
  373. int i;
  374. for (i = 0; i < nents; i++, sg++) {
  375. sg->dma_address = (page_to_phys(sg->page) + sg->offset)
  376. | CELL_DMA_VALID;
  377. sg->dma_length = sg->length;
  378. }
  379. return nents;
  380. }
  381. static void cell_unmap_sg(struct device *hwdev, struct scatterlist *sg,
  382. int nents, enum dma_data_direction direction)
  383. {
  384. }
  385. static int cell_dma_supported(struct device *dev, u64 mask)
  386. {
  387. return mask < 0x100000000ull;
  388. }
  389. static struct dma_mapping_ops cell_iommu_ops = {
  390. .alloc_coherent = cell_alloc_coherent,
  391. .free_coherent = cell_free_coherent,
  392. .map_single = cell_map_single,
  393. .unmap_single = cell_unmap_single,
  394. .map_sg = cell_map_sg,
  395. .unmap_sg = cell_unmap_sg,
  396. .dma_supported = cell_dma_supported,
  397. };
  398. void cell_init_iommu(void)
  399. {
  400. int setup_bus = 0;
  401. if (of_find_node_by_path("/mambo")) {
  402. pr_info("Not using iommu on systemsim\n");
  403. } else {
  404. if (!(of_chosen &&
  405. get_property(of_chosen, "linux,iommu-off", NULL)))
  406. setup_bus = cell_map_iommu();
  407. if (setup_bus) {
  408. pr_debug("%s: IOMMU mapping activated\n", __FUNCTION__);
  409. ppc_md.pci_dma_bus_setup = pci_dma_cell_bus_setup;
  410. } else {
  411. pr_debug("%s: IOMMU mapping activated, "
  412. "no device action necessary\n", __FUNCTION__);
  413. /* Direct I/O, IOMMU off */
  414. }
  415. }
  416. pci_dma_ops = &cell_iommu_ops;
  417. }