Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OpenRISC Linux
4 *
5 * Linux architectural port borrowing liberally from similar works of
6 * others. All original copyrights apply as per the original source
7 * declaration.
8 *
9 * Modifications for the OpenRISC architecture:
10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12 *
13 * DMA mapping callbacks...
14 */
15
16#include <linux/dma-map-ops.h>
17#include <linux/pagewalk.h>
18
19#include <asm/cpuinfo.h>
20#include <asm/spr_defs.h>
21#include <asm/tlbflush.h>
22
23static int
24page_set_nocache(pte_t *pte, unsigned long addr,
25 unsigned long next, struct mm_walk *walk)
26{
27 unsigned long cl;
28 struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
29
30 pte_val(*pte) |= _PAGE_CI;
31
32 /*
33 * Flush the page out of the TLB so that the new page flags get
34 * picked up next time there's an access
35 */
36 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
37
38 /* Flush page out of dcache */
39 for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo->dcache_block_size)
40 mtspr(SPR_DCBFR, cl);
41
42 return 0;
43}
44
45static const struct mm_walk_ops set_nocache_walk_ops = {
46 .pte_entry = page_set_nocache,
47};
48
49static int
50page_clear_nocache(pte_t *pte, unsigned long addr,
51 unsigned long next, struct mm_walk *walk)
52{
53 pte_val(*pte) &= ~_PAGE_CI;
54
55 /*
56 * Flush the page out of the TLB so that the new page flags get
57 * picked up next time there's an access
58 */
59 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
60
61 return 0;
62}
63
64static const struct mm_walk_ops clear_nocache_walk_ops = {
65 .pte_entry = page_clear_nocache,
66};
67
68void *arch_dma_set_uncached(void *cpu_addr, size_t size)
69{
70 unsigned long va = (unsigned long)cpu_addr;
71 int error;
72
73 /*
74 * We need to iterate through the pages, clearing the dcache for
75 * them and setting the cache-inhibit bit.
76 */
77 mmap_write_lock(&init_mm);
78 error = walk_page_range_novma(&init_mm, va, va + size,
79 &set_nocache_walk_ops, NULL, NULL);
80 mmap_write_unlock(&init_mm);
81
82 if (error)
83 return ERR_PTR(error);
84 return cpu_addr;
85}
86
87void arch_dma_clear_uncached(void *cpu_addr, size_t size)
88{
89 unsigned long va = (unsigned long)cpu_addr;
90
91 mmap_write_lock(&init_mm);
92 /* walk_page_range shouldn't be able to fail here */
93 WARN_ON(walk_page_range_novma(&init_mm, va, va + size,
94 &clear_nocache_walk_ops, NULL, NULL));
95 mmap_write_unlock(&init_mm);
96}
97
98void arch_sync_dma_for_device(phys_addr_t addr, size_t size,
99 enum dma_data_direction dir)
100{
101 unsigned long cl;
102 struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
103
104 switch (dir) {
105 case DMA_TO_DEVICE:
106 /* Flush the dcache for the requested range */
107 for (cl = addr; cl < addr + size;
108 cl += cpuinfo->dcache_block_size)
109 mtspr(SPR_DCBFR, cl);
110 break;
111 case DMA_FROM_DEVICE:
112 /* Invalidate the dcache for the requested range */
113 for (cl = addr; cl < addr + size;
114 cl += cpuinfo->dcache_block_size)
115 mtspr(SPR_DCBIR, cl);
116 break;
117 default:
118 /*
119 * NOTE: If dir == DMA_BIDIRECTIONAL then there's no need to
120 * flush nor invalidate the cache here as the area will need
121 * to be manually synced anyway.
122 */
123 break;
124 }
125}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OpenRISC Linux
4 *
5 * Linux architectural port borrowing liberally from similar works of
6 * others. All original copyrights apply as per the original source
7 * declaration.
8 *
9 * Modifications for the OpenRISC architecture:
10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12 *
13 * DMA mapping callbacks...
14 * As alloc_coherent is the only DMA callback being used currently, that's
15 * the only thing implemented properly. The rest need looking into...
16 */
17
18#include <linux/dma-noncoherent.h>
19#include <linux/pagewalk.h>
20
21#include <asm/cpuinfo.h>
22#include <asm/spr_defs.h>
23#include <asm/tlbflush.h>
24
25static int
26page_set_nocache(pte_t *pte, unsigned long addr,
27 unsigned long next, struct mm_walk *walk)
28{
29 unsigned long cl;
30 struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
31
32 pte_val(*pte) |= _PAGE_CI;
33
34 /*
35 * Flush the page out of the TLB so that the new page flags get
36 * picked up next time there's an access
37 */
38 flush_tlb_page(NULL, addr);
39
40 /* Flush page out of dcache */
41 for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo->dcache_block_size)
42 mtspr(SPR_DCBFR, cl);
43
44 return 0;
45}
46
47static const struct mm_walk_ops set_nocache_walk_ops = {
48 .pte_entry = page_set_nocache,
49};
50
51static int
52page_clear_nocache(pte_t *pte, unsigned long addr,
53 unsigned long next, struct mm_walk *walk)
54{
55 pte_val(*pte) &= ~_PAGE_CI;
56
57 /*
58 * Flush the page out of the TLB so that the new page flags get
59 * picked up next time there's an access
60 */
61 flush_tlb_page(NULL, addr);
62
63 return 0;
64}
65
66static const struct mm_walk_ops clear_nocache_walk_ops = {
67 .pte_entry = page_clear_nocache,
68};
69
70/*
71 * Alloc "coherent" memory, which for OpenRISC means simply uncached.
72 *
73 * This function effectively just calls __get_free_pages, sets the
74 * cache-inhibit bit on those pages, and makes sure that the pages are
75 * flushed out of the cache before they are used.
76 *
77 * If the NON_CONSISTENT attribute is set, then this function just
78 * returns "normal", cachable memory.
79 *
80 * There are additional flags WEAK_ORDERING and WRITE_COMBINE to take
81 * into consideration here, too. All current known implementations of
82 * the OR1K support only strongly ordered memory accesses, so that flag
83 * is being ignored for now; uncached but write-combined memory is a
84 * missing feature of the OR1K.
85 */
86void *
87arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
88 gfp_t gfp, unsigned long attrs)
89{
90 unsigned long va;
91 void *page;
92
93 page = alloc_pages_exact(size, gfp | __GFP_ZERO);
94 if (!page)
95 return NULL;
96
97 /* This gives us the real physical address of the first page. */
98 *dma_handle = __pa(page);
99
100 va = (unsigned long)page;
101
102 /*
103 * We need to iterate through the pages, clearing the dcache for
104 * them and setting the cache-inhibit bit.
105 */
106 if (walk_page_range(&init_mm, va, va + size, &set_nocache_walk_ops,
107 NULL)) {
108 free_pages_exact(page, size);
109 return NULL;
110 }
111
112 return (void *)va;
113}
114
115void
116arch_dma_free(struct device *dev, size_t size, void *vaddr,
117 dma_addr_t dma_handle, unsigned long attrs)
118{
119 unsigned long va = (unsigned long)vaddr;
120
121 /* walk_page_range shouldn't be able to fail here */
122 WARN_ON(walk_page_range(&init_mm, va, va + size,
123 &clear_nocache_walk_ops, NULL));
124
125 free_pages_exact(vaddr, size);
126}
127
128void arch_sync_dma_for_device(struct device *dev, phys_addr_t addr, size_t size,
129 enum dma_data_direction dir)
130{
131 unsigned long cl;
132 struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
133
134 switch (dir) {
135 case DMA_TO_DEVICE:
136 /* Flush the dcache for the requested range */
137 for (cl = addr; cl < addr + size;
138 cl += cpuinfo->dcache_block_size)
139 mtspr(SPR_DCBFR, cl);
140 break;
141 case DMA_FROM_DEVICE:
142 /* Invalidate the dcache for the requested range */
143 for (cl = addr; cl < addr + size;
144 cl += cpuinfo->dcache_block_size)
145 mtspr(SPR_DCBIR, cl);
146 break;
147 default:
148 /*
149 * NOTE: If dir == DMA_BIDIRECTIONAL then there's no need to
150 * flush nor invalidate the cache here as the area will need
151 * to be manually synced anyway.
152 */
153 break;
154 }
155}