iommu.c 14 KB

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