xref: /openbmc/linux/arch/loongarch/kernel/dma.c (revision 48ca54e3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #include <linux/init.h>
6 #include <linux/dma-direct.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/dma-map-ops.h>
9 #include <linux/swiotlb.h>
10 
11 #include <asm/bootinfo.h>
12 #include <asm/dma.h>
13 #include <asm/loongson.h>
14 
15 /*
16  * We extract 4bit node id (bit 44~47) from Loongson-3's
17  * 48bit physical address space and embed it into 40bit.
18  */
19 
20 static int node_id_offset;
21 
22 dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
23 {
24 	long nid = (paddr >> 44) & 0xf;
25 
26 	return ((nid << 44) ^ paddr) | (nid << node_id_offset);
27 }
28 
29 phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
30 {
31 	long nid = (daddr >> node_id_offset) & 0xf;
32 
33 	return ((nid << node_id_offset) ^ daddr) | (nid << 44);
34 }
35 
36 void __init plat_swiotlb_setup(void)
37 {
38 	swiotlb_init(true, SWIOTLB_VERBOSE);
39 	node_id_offset = ((readl(LS7A_DMA_CFG) & LS7A_DMA_NODE_MASK) >> LS7A_DMA_NODE_SHF) + 36;
40 }
41